added some text functions

Sat, 29 Mar 2014 12:15:39 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 29 Mar 2014 12:15:39 +0100
changeset 12
fe94e0fb9ef3
parent 11
979c0e0ee853
child 13
2dbc56c2323b

added some text functions

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/text.c file | annotate | diff | comparison | revisions
ui/gtk/text.h file | annotate | diff | comparison | revisions
ui/motif/text.c file | annotate | diff | comparison | revisions
ui/motif/text.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Fri Mar 28 20:03:49 2014 +0100
+++ b/application/main.c	Sat Mar 29 12:15:39 2014 +0100
@@ -83,13 +83,13 @@
 void action_undo(UiEvent *event, void *data) {
     printf("undo\n");
     TestWindowData *wd = event->window;
-    //ui_text_undo(&wd->text);
+    ui_text_undo(&wd->text);
 }
 
 void action_redo(UiEvent *event, void *data) {
     printf("redo\n");
     TestWindowData *wd = event->window;
-    //ui_text_redo(&wd->text);
+    ui_text_redo(&wd->text);
 }
 
 
--- a/ui/gtk/text.c	Fri Mar 28 20:03:49 2014 +0100
+++ b/ui/gtk/text.c	Sat Mar 29 12:15:39 2014 +0100
@@ -66,6 +66,8 @@
     if(value) {
         value->get = ui_textarea_get;
         value->set = ui_textarea_set;
+        value->getsubstr = ui_textarea_getsubstr;
+        value->insert = ui_textarea_insert;
         value->value = NULL;
         GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area));
         value->obj = buf;
@@ -105,9 +107,33 @@
 void ui_textarea_set(UiText *text, char *str) {
     if(text->value) {
         g_free(text->value);
-        text->value = NULL;
+    }
+    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);
     }
-    gtk_text_buffer_set_text((GtkTextBuffer*)text->obj, str, -1);
+    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);
 }
 
 void ui_textarea_realize_event(GtkWidget *widget, gpointer data) {
--- a/ui/gtk/text.h	Fri Mar 28 20:03:49 2014 +0100
+++ b/ui/gtk/text.h	Sat Mar 29 12:15:39 2014 +0100
@@ -56,6 +56,9 @@
 
 char* ui_textarea_get(UiText *text);
 void ui_textarea_set(UiText *text, char *str);
+char* ui_textarea_getsubstr(UiText *text, int begin, int end);
+void ui_textarea_insert(UiText *text, int pos, char *str);
+
 void ui_textarea_realize_event(GtkWidget *widget, gpointer data);
 void ui_textbuf_insert(
         GtkTextBuffer *textbuffer,
--- a/ui/motif/text.c	Fri Mar 28 20:03:49 2014 +0100
+++ b/ui/motif/text.c	Sat Mar 29 12:15:39 2014 +0100
@@ -48,8 +48,10 @@
     
     // bind value
     if(value) {
+        value->set = ui_textarea_set;
         value->get = ui_textarea_get;
-        value->set = ui_textarea_set;
+        value->getsubstr = ui_textarea_getsubstr;
+        value->insert = ui_textarea_insert;
         value->value = NULL;
         value->obj = text_area;
         
@@ -79,12 +81,30 @@
 void ui_textarea_set(UiText *text, char *str) {
     if(text->value) {
         XtFree(text->value);
-        text->value = NULL;
     }
     text->value = NULL;
     XmTextSetString(text->obj, str);
 }
 
+char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
+    if(text->value) {
+        XtFree(text->value);
+    }
+    int length = end - begin;
+    char *str = XtMalloc(length + 1);
+    XmTextGetSubstring(text->obj, begin, length, length + 1, str);
+    text->value = str;
+    return str;
+}
+
+void ui_textarea_insert(UiText *text, int pos, char *str) {
+    if(text->value) {
+        XtFree(text->value);
+    }
+    text->value = NULL;
+    XmTextInsert(text->obj, pos, str);
+}
+
 UiUndoMgr* ui_create_undomgr() {
     UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
     mgr->begin = NULL;
--- a/ui/motif/text.h	Fri Mar 28 20:03:49 2014 +0100
+++ b/ui/motif/text.h	Sat Mar 29 12:15:39 2014 +0100
@@ -56,6 +56,8 @@
     
 char* ui_textarea_get(UiText *text);
 void ui_textarea_set(UiText *text, char *str);
+char* ui_textarea_getsubstr(UiText *text, int begin, int end);
+void ui_textarea_insert(UiText *text, int pos, char *str);
 
 UiUndoMgr* ui_create_undomgr();
 void ui_text_modify_callback(Widget widget, UiText *value, XtPointer data);
--- a/ui/ui/toolkit.h	Fri Mar 28 20:03:49 2014 +0100
+++ b/ui/ui/toolkit.h	Sat Mar 29 12:15:39 2014 +0100
@@ -68,6 +68,10 @@
 
 #define ui_getval(val) (val).get(&(val))
 #define ui_setval(val, v) (val).set(&(val), v)
+
+#define ui_getsubstr(text, begin, end) (text).getsubstr(&(val), begin, end)
+#define ui_insert(text, begin, str) (text).insert(&(text), begin, str)
+
     
 typedef void(*ui_callback)(UiEvent*, void*); // event, user data
 
@@ -120,8 +124,10 @@
 };
 
 struct UiText {
+    void  (*set)(UiText*, char*);
     char* (*get)(UiText*);
-    void  (*set)(UiText*, char*);
+    char* (*getsubstr)(UiText*, int, int); // text, begin, end
+    void  (*insert)(UiText*, int, char*);
     char* value;
     void  *obj;
     void  *undomgr;

mercurial