adds observer support for textarea and textfield (GTK)

Sat, 11 Nov 2017 11:59:06 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 11 Nov 2017 11:59:06 +0100
changeset 143
d499b29d7cb6
parent 142
46448d38885c
child 144
29d98cff4f56

adds observer support for textarea and textfield (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/types.c file | annotate | diff | comparison | revisions
ui/gtk/button.c file | annotate | diff | comparison | revisions
ui/gtk/text.c file | annotate | diff | comparison | revisions
ui/gtk/text.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sat Nov 11 08:34:06 2017 +0100
+++ b/application/main.c	Sat Nov 11 11:59:06 2017 +0100
@@ -76,10 +76,10 @@
     Document *doc = ui_document_new(sizeof(Document));
     UiContext *ctx = ui_document_context(doc);
     
-    //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->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->i = ui_int_new(ctx, "int");
     return doc;
@@ -93,6 +93,10 @@
     ui_set_document(event->obj, newd);
 }
 
+void observ(UiEvent *event, void *data) {
+    printf("observ: %s\n", (char*)data);
+}
+
 void application_startup(UiEvent *event, void *data) {
     //Document *doc = create_doc();
     d1 = create_doc();
@@ -106,6 +110,11 @@
     ui_radiobutton_nv(obj, "2", "int");
     ui_radiobutton_nv(obj, "3", "int");
     
+    ui_textfield_nv(obj, "t1");
+    ui_textarea_nv(obj, "text");
+    d1->t1->observers = ui_add_observer(d1->t1->observers, observ, "t1");
+    d1->text->observers = ui_add_observer(d1->text->observers, observ, "text");
+    
     ui_button(obj, "Switch Document", action_newdoc, NULL);
     
     ui_show(obj);
--- a/ui/common/context.c	Sat Nov 11 08:34:06 2017 +0100
+++ b/ui/common/context.c	Sat Nov 11 11:59:06 2017 +0100
@@ -245,6 +245,7 @@
         case UI_VAR_INTEGER: {
             UiInteger *f = from->value;
             UiInteger *t = to->value;
+            if(!f->obj) break;
             uic_int_copy(f, t);
             t->set(t, t->value);
             break;
@@ -252,6 +253,7 @@
         case UI_VAR_STRING: {
             UiString *f = from->value;
             UiString *t = to->value;
+            if(!f->obj) break;
             uic_string_copy(f, t);
             char *tvalue = t->value.ptr ? t->value.ptr : "";
             t->set(t, tvalue);
@@ -260,6 +262,7 @@
         case UI_VAR_TEXT: {
             UiText *f = from->value;
             UiText *t = to->value;
+            if(!f->obj) break;
             uic_text_copy(f, t);
             char *tvalue = t->value.ptr ? t->value.ptr : "";
             t->set(t, tvalue);
@@ -269,12 +272,14 @@
         case UI_VAR_LIST: {
             UiList *f = from->value;
             UiList *t = to->value;
+            if(!f->obj) break;
             uic_list_copy(f, t);
             t->update(t, -1);
         }
         case UI_VAR_RANGE: {
             UiRange *f = from->value;
             UiRange *t = to->value;
+            if(!f->obj) break;
             uic_range_copy(f, t);
             t->setextent(t, t->extent);
             t->setrange(t, t->min, t->max);
--- a/ui/common/types.c	Sat Nov 11 08:34:06 2017 +0100
+++ b/ui/common/types.c	Sat Nov 11 11:59:06 2017 +0100
@@ -285,19 +285,23 @@
 
 
 void uic_int_save(UiInteger *i) {
+    if(!i->obj) return;
     i->value = i->get(i);
 }
 
 void uic_string_save(UiString *s) {
+    if(!s->obj) return;
     s->get(s);
 }
 
 void uic_text_save(UiText *t) {
+    if(!t->obj) return;
     t->get(t);
     t->position(t);
 }
 
 void uic_range_save(UiRange *r) {
+    if(!r->obj) return;
     r->get(r);
 }
 
--- a/ui/gtk/button.c	Sat Nov 11 08:34:06 2017 +0100
+++ b/ui/gtk/button.c	Sat Nov 11 11:59:06 2017 +0100
@@ -39,9 +39,6 @@
     GtkWidget *button = gtk_button_new_with_label(label);
     
     if(f) {
-        //UiEventData *event = ucx_mempool_malloc(
-        //        obj->ctx->mempool,
-        //        sizeof(UiEventData));
         UiEventData *event = malloc(sizeof(UiEventData));
         event->obj = obj;
         event->userdata = data;
@@ -104,7 +101,7 @@
     e.obj = event->obj;
     e.window = event->obj->window;
     e.document = event->obj->ctx->document;
-    e.eventdata = NULL;
+    e.eventdata = event->var->value;
     e.intval = gtk_toggle_tool_button_get_active(widget);
     
     UiInteger *i = event->var->value;
--- a/ui/gtk/text.c	Sat Nov 11 08:34:06 2017 +0100
+++ b/ui/gtk/text.c	Sat Nov 11 11:59:06 2017 +0100
@@ -122,6 +122,12 @@
             value->undomgr = ui_create_undomgr();
         }
         
+        g_signal_connect(
+                buf,
+                "changed",
+                G_CALLBACK(ui_textbuf_changed),
+                uitext);
+        
         // register undo manager
         g_signal_connect(
                 buf,
@@ -132,8 +138,7 @@
                 buf,
                 "delete-range",
                 G_CALLBACK(ui_textbuf_delete),
-                var);
-        
+                var); 
         g_signal_connect(
                 buf,
                 "mark-set",
@@ -263,6 +268,18 @@
 
 
 
+void ui_textbuf_changed(GtkTextBuffer *textbuffer, UiTextArea *textarea) {
+    UiText *value = textarea->var->value;
+    if(value->observers) {
+        UiEvent e;
+        e.obj = textarea->ctx->obj;
+        e.window = e.obj->window;
+        e.document = textarea->ctx->document;
+        e.eventdata = value;
+        e.intval = 0;
+        ui_notify_evt(value->observers, &e);
+    }
+}
 
 // undo manager functions
 
@@ -525,6 +542,12 @@
         value->value.ptr = NULL;
         value->value.free = NULL;
         value->obj = GTK_ENTRY(textfield);
+        
+        g_signal_connect(
+                textfield,
+                "changed",
+                G_CALLBACK(ui_textfield_changed),
+                uitext);
     }
     
     return textfield;
@@ -555,6 +578,19 @@
     free(textfield);
 }
 
+void ui_textfield_changed(GtkEditable *editable, UiTextField *textfield) {
+    UiString *value = textfield->var->value;
+    if(value->observers) {
+        UiEvent e;
+        e.obj = textfield->ctx->obj;
+        e.window = e.obj->window;
+        e.document = textfield->ctx->document;
+        e.eventdata = value;
+        e.intval = 0;
+        ui_notify_evt(value->observers, &e);
+    }
+}
+
 UIWIDGET ui_textfield(UiObject *obj, UiString *value) {
     return create_textfield(obj, 0, FALSE, FALSE, value);
 }
--- a/ui/gtk/text.h	Sat Nov 11 08:34:06 2017 +0100
+++ b/ui/gtk/text.h	Sat Nov 11 11:59:06 2017 +0100
@@ -81,6 +81,8 @@
 void ui_textarea_remove(UiText *text, int begin, int end);
 
 void ui_textarea_realize_event(GtkWidget *widget, gpointer data);
+void ui_textbuf_changed(GtkTextBuffer *textbuffer, UiTextArea *textarea);
+
 void ui_textbuf_insert(
         GtkTextBuffer *textbuffer,
         GtkTextIter *location,
@@ -97,6 +99,7 @@
 int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen);
 
 void ui_textfield_destroy(GtkWidget *object, UiTextField *textfield);
+void ui_textfield_changed(GtkEditable *editable, UiTextField *textfield);
 
 char* ui_textfield_get(UiString *str);
 void ui_textfield_set(UiString *str, char *value);

mercurial