ui/gtk/text.c

branch
newapi
changeset 178
7c3ff86ee9d4
parent 174
0358f1d9c506
child 253
087cc9216f28
--- a/ui/gtk/text.c	Sun Apr 16 10:20:21 2023 +0200
+++ b/ui/gtk/text.c	Tue May 23 11:11:28 2023 +0200
@@ -34,6 +34,8 @@
 #include "container.h"
 
 
+#include "../common/types.h"
+
 static void selection_handler(
         GtkTextBuffer *buf,
         GtkTextIter *location,
@@ -304,19 +306,19 @@
     }
     
     if(mgr->cur) {
-        UcxList *elm = mgr->cur->next;
+        UiTextBufOp *elm = mgr->cur->next;
         if(elm) {
             mgr->cur->next = NULL;
+            mgr->end = mgr->cur;
             while(elm) {
                 elm->prev = NULL;   
-                UcxList *next = elm->next;
-                ui_free_textbuf_op(elm->data);
-                free(elm);
+                UiTextBufOp *next = elm->next;
+                ui_free_textbuf_op(elm);
                 elm = next;
             }
         }
         
-        UiTextBufOp *last_op = mgr->cur->data;
+        UiTextBufOp *last_op = mgr->cur;
         if(
             last_op->type == UI_TEXTBUF_INSERT &&
             ui_check_insertstr(last_op->text, last_op->len, text, length) == 0)
@@ -341,15 +343,22 @@
     dpstr[length] = 0;
     
     UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
+    op->prev = NULL;
+    op->next = NULL;
     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);
+    cx_linked_list_add(
+            (void**)&mgr->begin,
+            (void**)&mgr->end,
+            offsetof(UiTextBufOp, prev),
+            offsetof(UiTextBufOp, next),
+            op);
+    
+    mgr->cur = op;
 }
 
 void ui_textbuf_delete(
@@ -369,14 +378,14 @@
     }
     
     if(mgr->cur) {
-        UcxList *elm = mgr->cur->next;
+        UiTextBufOp *elm = mgr->cur->next;
         if(elm) {
             mgr->cur->next = NULL;
+            mgr->end = mgr->cur;
             while(elm) {
                 elm->prev = NULL;   
-                UcxList *next = elm->next;
-                ui_free_textbuf_op(elm->data);
-                free(elm);
+                UiTextBufOp *next = elm->next;
+                ui_free_textbuf_op(elm);
                 elm = next;
             }
         }
@@ -385,6 +394,8 @@
     char *text = gtk_text_buffer_get_text(value->obj, start, end, FALSE);
     
     UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
+    op->prev = NULL;
+    op->next = NULL;
     op->type = UI_TEXTBUF_DELETE;
     op->start = gtk_text_iter_get_offset(start);
     op->end = gtk_text_iter_get_offset(end);
@@ -395,20 +406,39 @@
     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);
+    cx_linked_list_add(
+            (void**)&mgr->begin,
+            (void**)&mgr->end,
+            offsetof(UiTextBufOp, prev),
+            offsetof(UiTextBufOp, next),
+            op);
+    
+    mgr->cur = op;
 }
 
 UiUndoMgr* ui_create_undomgr() {
     UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
     mgr->begin = NULL;
+    mgr->end = NULL;
     mgr->cur = NULL;
     mgr->length = 0;
     mgr->event = 1;
     return mgr;
 }
 
+void ui_destroy_undomgr(UiUndoMgr *mgr) {
+    UiTextBufOp *op = mgr->begin;
+    while(op) {
+        UiTextBufOp *nextOp = op->next;
+        if(op->text) {
+            free(op->text);
+        }
+        free(op);
+        op = nextOp;
+    }
+    free(mgr);
+}
+
 void ui_free_textbuf_op(UiTextBufOp *op) {
     if(op->text) {
         free(op->text);
@@ -440,7 +470,7 @@
     UiUndoMgr *mgr = value->undomgr;
     
     if(mgr->cur) {
-        UiTextBufOp *op = mgr->cur->data;
+        UiTextBufOp *op = mgr->cur;
         mgr->event = 0;
         switch(op->type) {
             case UI_TEXTBUF_INSERT: {
@@ -468,7 +498,7 @@
 void ui_text_redo(UiText *value) {
     UiUndoMgr *mgr = value->undomgr;
     
-    UcxList *elm = NULL;
+    UiTextBufOp *elm = NULL;
     if(mgr->cur) {
         if(mgr->cur->next) {
             elm = mgr->cur->next;
@@ -478,7 +508,7 @@
     }
     
     if(elm) {
-        UiTextBufOp *op = elm->data;
+        UiTextBufOp *op = elm;
         mgr->event = 0;
         switch(op->type) {
             case UI_TEXTBUF_INSERT: {
@@ -580,6 +610,10 @@
 
 void ui_textfield_destroy(GtkWidget *object, UiTextField *textfield) {
     if(textfield->var) {
+        UiText *text = textfield->var->value;
+        if(text->undomgr) {
+            ui_destroy_undomgr(text->undomgr);
+        }
         ui_destroy_boundvar(textfield->ctx, textfield->var);
     }
     free(textfield);

mercurial