change textarea change events to include UiTextChangeEventData (GTK)

Fri, 29 May 2026 15:40:31 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 29 May 2026 15:40:31 +0200
changeset 1149
76b7664f951e
parent 1148
6ebe1b98f53b
child 1150
34b7b8a9acdf

change textarea change events to include UiTextChangeEventData (GTK)

ui/common/wrapper.c file | annotate | diff | comparison | revisions
ui/common/wrapper.h file | annotate | diff | comparison | revisions
ui/gtk/text.c file | annotate | diff | comparison | revisions
ui/gtk/text.h file | annotate | diff | comparison | revisions
ui/ui/text.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/common/wrapper.c	Thu May 28 21:18:33 2026 +0200
+++ b/ui/common/wrapper.c	Fri May 29 15:40:31 2026 +0200
@@ -28,6 +28,7 @@
 
 #include "wrapper.h"
 #include "types.h"
+#include "ui/text.h"
 #include <cx/list.h>
 #include <string.h>
 
@@ -296,6 +297,28 @@
     free(sel);
 }
 
+/* -------------------------- UiTextChangedEvent -------------------------- */
+
+int ui_text_change_event_get_type(UiTextChangeEventData *event) {
+    return event->type;
+}
+
+int ui_text_change_event_get_begin(UiTextChangeEventData *event) {
+    return event->begin;
+}
+
+int ui_text_change_event_get_end(UiTextChangeEventData *event) {
+    return event->end;
+}
+
+const char* ui_text_change_event_get_text(UiTextChangeEventData *event) {
+    return event->text;
+}
+
+int ui_text_change_event_get_length(UiTextChangeEventData *event) {
+    return event->length;
+}
+
 /* ---------------------------- UiFileList ---------------------------- */
 
 int ui_filelist_count(UiFileList *flist) {
--- a/ui/common/wrapper.h	Thu May 28 21:18:33 2026 +0200
+++ b/ui/common/wrapper.h	Fri May 29 15:40:31 2026 +0200
@@ -86,6 +86,12 @@
 UIEXPORT void ui_list_set_selected_indices(UiList *list, int *indices, int num);
 UIEXPORT void ui_list_selection_free(UiListSelection *sel);
 
+UIEXPORT int ui_text_change_event_get_type(UiTextChangeEventData *event);
+UIEXPORT int ui_text_change_event_get_begin(UiTextChangeEventData *event);
+UIEXPORT int ui_text_change_event_get_end(UiTextChangeEventData *event);
+UIEXPORT const char* ui_text_change_event_get_text(UiTextChangeEventData *event);
+UIEXPORT int ui_text_change_event_get_length(UiTextChangeEventData *event);
+
 UIEXPORT int ui_filelist_count(UiFileList *flist);
 UIEXPORT char* ui_filelist_get(UiFileList *flist, int index);
 
--- a/ui/gtk/text.c	Thu May 28 21:18:33 2026 +0200
+++ b/ui/gtk/text.c	Fri May 29 15:40:31 2026 +0200
@@ -97,11 +97,24 @@
 static GtkTextBuffer* create_textbuffer(UiTextArea *textarea) {
     GtkTextBuffer *buf = gtk_text_buffer_new(NULL);
     if(textarea) {
+        /*
         g_signal_connect(
                 buf,
                 "changed",
                 G_CALLBACK(ui_textbuf_changed),
                 textarea);
+        */
+        
+        g_signal_connect(
+                buf,
+                "insert-text",
+                G_CALLBACK(ui_textbuf_changed_insert),
+                textarea);
+        g_signal_connect(
+                buf,
+                "delete-range",
+                G_CALLBACK(ui_textbuf_changed_delete),
+                textarea); 
     } else {
         fprintf(stderr, "Error: create_textbuffer: textarea == NULL\n");
     }
@@ -450,8 +463,41 @@
 }
 
 
+void ui_textbuf_changed_insert(
+        GtkTextBuffer *textbuffer,
+        GtkTextIter *location,
+        char *text,
+        int length,
+        UiTextArea *textarea)
+{
+    UiTextChangeEventData event;
+    event.type = UI_TEXT_INSERT;
+    event.begin = gtk_text_iter_get_offset(location);
+    event.end = event.begin + length;
+    event.text = text;
+    event.length = length;
+    ui_textbuf_changed(textarea, &event);
+}
 
-void ui_textbuf_changed(GtkTextBuffer *textbuffer, UiTextArea *textarea) {
+void ui_textbuf_changed_delete(
+        GtkTextBuffer *self,
+        const GtkTextIter *start,
+        const GtkTextIter *end,
+        UiTextArea *textarea)
+{
+    UiTextChangeEventData event;
+    event.type = UI_TEXT_DELETE;
+    event.begin = gtk_text_iter_get_offset(start);
+    event.end = gtk_text_iter_get_offset(end);
+    event.text = NULL;
+    event.length = 0;
+    ui_textbuf_changed(textarea, &event);
+}
+
+
+// void ui_textbuf_changed(GtkTextBuffer *textbuffer, UiTextArea *textarea)
+
+void ui_textbuf_changed(UiTextArea *textarea, UiTextChangeEventData *data) {
     if(!ui_onchange_events_is_enabled()) {
         return;
     }
@@ -462,8 +508,8 @@
     e.obj = textarea->obj;
     e.window = e.obj->window;
     e.document = textarea->ctx->document;
-    e.eventdata = value;
-    e.eventdatatype = UI_EVENT_DATA_TEXT_VALUE;
+    e.eventdata = data;
+    e.eventdatatype = UI_EVENT_DATA_TEXT_CHANGED;
     e.intval = 0;
     e.set = ui_get_setop();
     
--- a/ui/gtk/text.h	Thu May 28 21:18:33 2026 +0200
+++ b/ui/gtk/text.h	Fri May 29 15:40:31 2026 +0200
@@ -131,7 +131,19 @@
 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_changed(GtkTextBuffer *textbuffer, UiTextArea *textarea);
+void ui_textbuf_changed_insert(
+        GtkTextBuffer *textbuffer,
+        GtkTextIter *location,
+        char *text,
+        int length,
+        UiTextArea *textarea);
+void ui_textbuf_changed_delete(
+        GtkTextBuffer *self,
+        const GtkTextIter *start,
+        const GtkTextIter *end,
+        UiTextArea *textarea);
+void ui_textbuf_changed(UiTextArea *textarea, UiTextChangeEventData *data);
 
 void ui_textbuf_insert(
         GtkTextBuffer *textbuffer,
--- a/ui/ui/text.h	Thu May 28 21:18:33 2026 +0200
+++ b/ui/ui/text.h	Fri May 29 15:40:31 2026 +0200
@@ -63,6 +63,19 @@
     const int *states;
     const int *visibility_states;
 } UiTextAreaArgs;
+
+typedef enum UiTextChangedEventType {
+    UI_TEXT_INSERT = 0,
+    UI_TEXT_DELETE
+} UiTextChangedEventType;
+
+typedef struct UiTextChangeEventData {
+    UiTextChangedEventType type;
+    int begin;
+    int end;
+    const char *text;
+    int length;
+} UiTextChangeEventData;
     
 typedef struct UiTextFieldArgs {
     UiBool fill;
--- a/ui/ui/toolkit.h	Thu May 28 21:18:33 2026 +0200
+++ b/ui/ui/toolkit.h	Fri May 29 15:40:31 2026 +0200
@@ -491,6 +491,7 @@
     UI_EVENT_DATA_TEXT_VALUE,
     UI_EVENT_DATA_DOUBLE_VALUE,
     UI_EVENT_DATA_RANGE_VALUE,
+    UI_EVENT_DATA_TEXT_CHANGED,
     UI_EVENT_DATA_LIST_SELECTION,
     UI_EVENT_DATA_LIST_ELM,
     UI_EVENT_DATA_DND,

mercurial