fixes document switching with textareas (Motif)

Fri, 09 Nov 2018 17:11:12 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 09 Nov 2018 17:11:12 +0100
changeset 155
b907d8059290
parent 154
8a4451fcb736
child 156
62f1a55535e7

fixes document switching with textareas (Motif)

application/main.c file | annotate | diff | comparison | revisions
ui/common/types.c file | annotate | diff | comparison | revisions
ui/motif/text.c file | annotate | diff | comparison | revisions
ui/motif/text.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Fri Nov 09 15:43:46 2018 +0100
+++ b/application/main.c	Fri Nov 09 17:11:12 2018 +0100
@@ -97,26 +97,26 @@
     Document *doc = ui_document_new(sizeof(Document));
     UiContext *ctx = ui_document_context(doc);
     
-    //doc->text = ui_text_new(ctx, "text");
+    doc->text = ui_text_new(ctx, "text");
     doc->t1 = ui_string_new(ctx, "t1");
-    //doc->t2 = ui_string_new(ctx, "t2");
-    //doc->t3 = ui_string_new(ctx, "t3");
+    doc->t2 = ui_string_new(ctx, "t2");
+    doc->t3 = ui_string_new(ctx, "t3");
     
-    //doc->i = ui_int_new(ctx, "int");
-    //doc->d = ui_double_new(ctx, "d");
-    //->r = ui_range_new(ctx, "r");
+    doc->i = ui_int_new(ctx, "int");
+    doc->d = ui_double_new(ctx, "d");
+    doc->r = ui_range_new(ctx, "r");
     
-    //doc->progress = ui_double_new(ctx, "progress");
+    doc->progress = ui_double_new(ctx, "progress");
     
-    //doc->list = ui_list_new(ctx, "list");
+    doc->list = ui_list_new(ctx, "list");
     Entry *e1 = calloc(1, sizeof(Entry));
     e1->name = "test";
     e1->desc = "test file";
     Entry *e2 = calloc(1, sizeof(Entry));
     e2->name = str;
     e2->desc = "important document";
-    //ui_list_append(doc->list, e1);
-    //ui_list_append(doc->list, e2);
+    ui_list_append(doc->list, e1);
+    ui_list_append(doc->list, e2);
     
     return doc;
 }
@@ -162,7 +162,7 @@
     UiObject *obj = ui_window("Test", NULL);
     ui_set_document(obj, d1);
     
-    //ui_textarea_nv(obj, "text");
+    ui_textarea_nv(obj, "text");
     //ui_radiobutton_nv(obj, "1", "int");
     //ui_radiobutton_nv(obj, "2", "int");
     //ui_radiobutton_nv(obj, "3", "int");
--- a/ui/common/types.c	Fri Nov 09 15:43:46 2018 +0100
+++ b/ui/common/types.c	Fri Nov 09 17:11:12 2018 +0100
@@ -373,6 +373,7 @@
     t->length = NULL;
     t->remove = NULL;
     t->obj = NULL;
+    t->undomgr = NULL;
 }
 
 void uic_range_unbind(UiRange *r) {
--- a/ui/motif/text.c	Fri Nov 09 15:43:46 2018 +0100
+++ b/ui/motif/text.c	Fri Nov 09 17:11:12 2018 +0100
@@ -32,7 +32,8 @@
 #include "text.h"
 #include "container.h"
 
-UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
+
+UIWIDGET ui_textarea_var(UiObject *obj, UiVar *var) {
     UiContainer *ct = uic_get_current_container(obj);
     int n = 0;
     Arg args[16];
@@ -59,7 +60,8 @@
                 uitext);
     
     // bind value
-    if(value) {
+    if(var->value) {
+        UiText *value = var->value;
         if(value->value.ptr) {
             XmTextSetString(text_area, value->value.ptr);
             value->value.free(value->value.ptr);
@@ -84,17 +86,23 @@
                 text_area,
                 XmNmodifyVerifyCallback,
                 (XtCallbackProc)ui_text_modify_callback,
-                value);
+                var);
     }
     
     return text_area;
 }
 
+UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
+    UiVar *var = malloc(sizeof(UiVar));
+    var->value = value;
+    var->type = UI_VAR_SPECIAL;
+    return ui_textarea_var(obj, var);
+}
+
 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) {
     UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_TEXT);
     if(var) {
-        UiText *value = var->value;
-        return ui_textarea(obj, value);
+        return ui_textarea_var(obj, var);
     } else {
         // TODO: error
     }
@@ -112,11 +120,11 @@
 }
 
 void ui_textarea_set(UiText *text, char *str) {
+    XmTextSetString(text->obj, str);
     if(text->value.ptr) {
         text->value.free(text->value.ptr);
     }
     text->value.ptr = NULL;
-    XmTextSetString(text->obj, str);
 }
 
 char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
@@ -132,11 +140,11 @@
 }
 
 void ui_textarea_insert(UiText *text, int pos, char *str) {
+    text->value.ptr = NULL;
+    XmTextInsert(text->obj, pos, str);
     if(text->value.ptr) {
         text->value.free(text->value.ptr);
     }
-    text->value.ptr = NULL;
-    XmTextInsert(text->obj, pos, str);
 }
 
 void ui_textarea_setposition(UiText *text, int pos) {
@@ -209,7 +217,16 @@
     textarea->last_selection_state = sel;
 }
 
-void ui_text_modify_callback(Widget widget, UiText *value, XtPointer data) {
+void ui_text_modify_callback(Widget widget, UiVar *var, XtPointer data) {
+    UiText *value = var->value;
+    if(!value->obj) {
+        // TODO: bug, fix
+        return;
+    }
+    if(!value->undomgr) {
+        value->undomgr = ui_create_undomgr();
+    }
+    
     XmTextVerifyCallbackStruct *txv = (XmTextVerifyCallbackStruct*)data;
     int type = txv->text->length > 0 ? UI_TEXTBUF_INSERT : UI_TEXTBUF_DELETE;
     UiUndoMgr *mgr = value->undomgr;
--- a/ui/motif/text.h	Fri Nov 09 15:43:46 2018 +0100
+++ b/ui/motif/text.h	Fri Nov 09 17:11:12 2018 +0100
@@ -73,7 +73,7 @@
         Widget widget,
         UiTextArea *textarea,
         XtPointer data);
-void ui_text_modify_callback(Widget widget, UiText *value, XtPointer data);
+void ui_text_modify_callback(Widget widget, UiVar *var, XtPointer data);
 int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen);
 void ui_free_textbuf_op(UiTextBufOp *op);
 

mercurial