Fri, 28 Mar 2014 20:03:01 +0100
added Cocoa implementation for menu check items (with bugs)
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2014 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "text.h" #include "container.h" UIWIDGET ui_textarea(UiObject *obj, UiText *value) { GtkWidget *text_area = gtk_text_view_new(); gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_area), GTK_WRAP_WORD_CHAR); g_signal_connect( text_area, "realize", G_CALLBACK(ui_textarea_realize_event), NULL); GtkWidget *scroll_area = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(scroll_area), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS gtk_container_add(GTK_CONTAINER(scroll_area), text_area); // font and padding PangoFontDescription *font; font = pango_font_description_from_string("Monospace"); gtk_widget_modify_font(text_area, font); pango_font_description_free(font); gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_area), 2); gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text_area), 2); // add UiContainer *ct = uic_get_current_container(obj); ct->add(ct, scroll_area); // bind value if(value) { value->get = ui_textarea_get; value->set = ui_textarea_set; value->value = NULL; GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area)); value->obj = buf; if(!value->undomgr) { value->undomgr = ui_create_undomgr(); } // register undo manager g_signal_connect( buf, "insert-text", G_CALLBACK(ui_textbuf_insert), value); g_signal_connect( buf, "delete-range", G_CALLBACK(ui_textbuf_delete), value); } return scroll_area; } char* ui_textarea_get(UiText *text) { if(text->value) { g_free(text->value); } GtkTextBuffer *buf = text->obj; GtkTextIter start; GtkTextIter end; gtk_text_buffer_get_bounds(buf, &start, &end); char *str = gtk_text_buffer_get_text(buf, &start, &end, FALSE); text->value = str; return str; } void ui_textarea_set(UiText *text, char *str) { if(text->value) { g_free(text->value); text->value = NULL; } gtk_text_buffer_set_text((GtkTextBuffer*)text->obj, str, -1); } void ui_textarea_realize_event(GtkWidget *widget, gpointer data) { gtk_widget_grab_focus(widget); } // undo manager functions void ui_textbuf_insert( GtkTextBuffer *textbuffer, GtkTextIter *location, char *text, int length, void *data) { UiText *value = data; UiUndoMgr *mgr = value->undomgr; if(!mgr->event) { return; } if(mgr->cur) { UcxList *elm = mgr->cur->next; while(elm) { ui_free_textbuf_op(elm->data); UcxList *next = elm->next; ucx_list_remove(mgr->begin, elm); elm = next; } UiTextBufOp *last_op = mgr->cur->data; if(ui_check_insertstr(last_op->text, last_op->len, text, length) == 0) { // append text to last op int ln = last_op->len; char *newtext = malloc(ln + length + 1); memcpy(newtext, last_op->text, ln); memcpy(newtext+ln, text, length); newtext[ln+length] = '\0'; last_op->text = newtext; last_op->len = ln + length; last_op->end += length; return; } } char *dpstr = malloc(length + 1); memcpy(dpstr, text, length); dpstr[length] = 0; UiTextBufOp *op = malloc(sizeof(UiTextBufOp)); op->type = UI_TEXTBUF_INSERT; op->start = gtk_text_iter_get_offset(location); op->end = op->start+length; op->len = length; op->text = dpstr; UcxList *elm = ucx_list_append(NULL, op); mgr->cur = elm; mgr->begin = ucx_list_concat(mgr->begin, elm); } void ui_textbuf_delete( GtkTextBuffer *textbuffer, GtkTextIter *start, GtkTextIter *end, void *data) { UiText *value = data; UiUndoMgr *mgr = value->undomgr; if(!mgr->event) { return; } if(mgr->cur) { UcxList *elm = mgr->cur->next; while(elm) { ui_free_textbuf_op(elm->data); UcxList *next = elm->next; ucx_list_remove(mgr->begin, elm); elm = next; } } char *text = gtk_text_buffer_get_text(value->obj, start, end, FALSE); UiTextBufOp *op = malloc(sizeof(UiTextBufOp)); op->type = UI_TEXTBUF_DELETE; op->start = gtk_text_iter_get_offset(start); op->end = gtk_text_iter_get_offset(end); op->len = op->end - op->start; char *dpstr = malloc(op->len + 1); memcpy(dpstr, text, op->len); dpstr[op->len] = 0; op->text = dpstr; UcxList *elm = ucx_list_append(NULL, op); mgr->cur = elm; mgr->begin = ucx_list_concat(mgr->begin, elm); } UiUndoMgr* ui_create_undomgr() { UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr)); mgr->begin = NULL; mgr->cur = NULL; mgr->length = 0; mgr->event = 1; return mgr; } void ui_free_textbuf_op(UiTextBufOp *op) { if(op->text) { g_free(op->text); } free(op); } int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen) { // return 1 if oldstr + newstr are one word int has_space = 0; for(int i=0;i<oldlen;i++) { if(oldstr[i] < 33) { has_space = 1; break; } } for(int i=0;i<newlen;i++) { if(has_space && newstr[i] > 32) { return 1; } } return 0; } void ui_text_undo(UiText *value) { UiUndoMgr *mgr = value->undomgr; if(mgr->cur) { UiTextBufOp *op = mgr->cur->data; mgr->event = 0; switch(op->type) { case UI_TEXTBUF_INSERT: { GtkTextIter begin; GtkTextIter end; gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start); gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end); gtk_text_buffer_delete(value->obj, &begin, &end); break; } case UI_TEXTBUF_DELETE: { GtkTextIter begin; GtkTextIter end; gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start); gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end); gtk_text_buffer_insert(value->obj, &begin, op->text, op->len); break; } } mgr->event = 1; mgr->cur = mgr->cur->prev; } } void ui_text_redo(UiText *value) { UiUndoMgr *mgr = value->undomgr; UcxList *elm = NULL; if(mgr->cur) { if(mgr->cur->next) { elm = mgr->cur->next; } } else if(mgr->begin) { elm = mgr->begin; } if(elm) { UiTextBufOp *op = elm->data; mgr->event = 0; switch(op->type) { case UI_TEXTBUF_INSERT: { GtkTextIter begin; GtkTextIter end; gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start); gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end); gtk_text_buffer_insert(value->obj, &begin, op->text, op->len); break; } case UI_TEXTBUF_DELETE: { GtkTextIter begin; GtkTextIter end; gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start); gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end); gtk_text_buffer_delete(value->obj, &begin, &end); break; } } mgr->event = 1; mgr->cur = elm; } }