Tue, 22 Jul 2014 19:15:44 +0200
improved motif tabs
/* * 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" #include "../common/context.h" #include "../common/document.h" static void selection_handler( GtkTextBuffer *buf, GtkTextIter *location, GtkTextMark *mark, UiTextArea *textview) { const char *mname = gtk_text_mark_get_name(mark); if(mname) { GtkTextIter begin; GtkTextIter end; int sel = gtk_text_buffer_get_selection_bounds (buf, &begin, &end); if(sel != textview->last_selection_state) { if(sel) { ui_set_group(textview->ctx, UI_GROUP_SELECTION); } else { ui_unset_group(textview->ctx, UI_GROUP_SELECTION); } } textview->last_selection_state = sel; } } 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); g_signal_connect( text_area, "selection-clear-event", G_CALLBACK(selection_handler), NULL); UiTextArea *uitext = ucx_mempool_malloc( obj->ctx->mempool, sizeof(UiTextArea)); uitext->ctx = obj->ctx; uitext->last_selection_state = 0; 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) { GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area)); if(value->value) { gtk_text_buffer_set_text(buf, value->value, -1); } value->get = ui_textarea_get; value->set = ui_textarea_set; value->getsubstr = ui_textarea_getsubstr; value->insert = ui_textarea_insert; value->position = ui_textarea_position; value->selection = ui_textarea_selection; value->length = ui_textarea_length; value->remove = ui_textarea_remove; value->value = NULL; 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); g_signal_connect( buf, "mark-set", G_CALLBACK(selection_handler), uitext); } return scroll_area; } UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) { UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_TEXT); if(var) { UiText *value = var->value; return ui_textarea(obj, value); } else { // TODO: error } return NULL; } 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); } char* ui_textarea_getsubstr(UiText *text, int begin, int end) { if(text->value) { g_free(text->value); } GtkTextBuffer *buf = text->obj; GtkTextIter ib; GtkTextIter ie; gtk_text_buffer_get_iter_at_offset(text->obj, &ib, begin); gtk_text_buffer_get_iter_at_offset(text->obj, &ie, end); char *str = gtk_text_buffer_get_text(buf, &ib, &ie, FALSE); text->value = str; return str; } void ui_textarea_insert(UiText *text, int pos, char *str) { if(text->value) { g_free(text->value); } text->value = NULL; GtkTextIter offset; gtk_text_buffer_get_iter_at_offset(text->obj, &offset, pos); gtk_text_buffer_insert(text->obj, &offset, str, -1); } int ui_textarea_position(UiText *text) { GtkTextIter begin; GtkTextIter end; gtk_text_buffer_get_selection_bounds(text->obj, &begin, &end); return gtk_text_iter_get_offset(&begin); } void ui_textarea_selection(UiText *text, int *begin, int *end) { GtkTextIter b; GtkTextIter e; gtk_text_buffer_get_selection_bounds(text->obj, &b, &e); *begin = gtk_text_iter_get_offset(&b); *end = gtk_text_iter_get_offset(&e); } int ui_textarea_length(UiText *text) { GtkTextBuffer *buf = text->obj; GtkTextIter start; GtkTextIter end; gtk_text_buffer_get_bounds(buf, &start, &end); return gtk_text_iter_get_offset(&end); } void ui_textarea_remove(UiText *text, int begin, int end) { GtkTextBuffer *buf = text->obj; GtkTextIter ib; GtkTextIter ie; gtk_text_buffer_get_iter_at_offset(buf, &ib, begin); gtk_text_buffer_get_iter_at_offset(buf, &ie, end); gtk_text_buffer_delete(buf, &ib, &ie); } 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; if(elm) { mgr->cur->next = NULL; while(elm) { elm->prev = NULL; UcxList *next = elm->next; ui_free_textbuf_op(elm->data); free(elm); elm = next; } } UiTextBufOp *last_op = mgr->cur->data; if( last_op->type == UI_TEXTBUF_INSERT && 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; if(elm) { mgr->cur->next = NULL; while(elm) { elm->prev = NULL; UcxList *next = elm->next; ui_free_textbuf_op(elm->data); free(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) { 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; } }