ui/motif/text.c

branch
newapi
changeset 178
7c3ff86ee9d4
parent 176
bc63cb601f6d
child 253
087cc9216f28
--- a/ui/motif/text.c	Sun Apr 16 10:20:21 2023 +0200
+++ b/ui/motif/text.c	Tue May 23 11:11:28 2023 +0200
@@ -48,8 +48,8 @@
     ct->add(ct, XtParent(text_area));
     XtManageChild(text_area);
     
-    UiTextArea *uitext = ucx_mempool_malloc(
-            obj->ctx->mempool,
+    UiTextArea *uitext = cxMalloc(
+            obj->ctx->allocator,
             sizeof(UiTextArea));
     uitext->ctx = obj->ctx;
     uitext->last_selection_state = 0;
@@ -192,12 +192,26 @@
 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_text_selection_callback(
         Widget widget,
         UiTextArea *textarea,
@@ -238,37 +252,35 @@
     int length = txv->text->length;
     
     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;
             }
         }
         
-        if(type == UI_TEXTBUF_INSERT) {
-            UiTextBufOp *last_op = mgr->cur->data;
-            if(
-                last_op->type == UI_TEXTBUF_INSERT &&
-                ui_check_insertstr(last_op->text, last_op->len, text, length) == 0)
-            {
-                // append text to last op
-                int ln = last_op->len;
-                char *newtext = malloc(ln + length + 1);
-                memcpy(newtext, last_op->text, ln);
-                memcpy(newtext+ln, text, length);
-                newtext[ln+length] = '\0';
-                
-                last_op->text = newtext;
-                last_op->len = ln + length;
-                last_op->end += length;
-
-                return;
-            }
+        UiTextBufOp *last_op = mgr->cur;
+        if(
+            last_op->type == UI_TEXTBUF_INSERT &&
+            ui_check_insertstr(last_op->text, last_op->len, text, length) == 0)
+        {
+            // append text to last op       
+            int ln = last_op->len;
+            char *newtext = malloc(ln + length + 1);
+            memcpy(newtext, last_op->text, ln);
+            memcpy(newtext+ln, text, length);
+            newtext[ln+length] = '\0';
+            
+            last_op->text = newtext;
+            last_op->len = ln + length;
+            last_op->end += length;
+            
+            return;
         }
     }
     
@@ -284,15 +296,22 @@
     }
     
     UiTextBufOp *op = malloc(sizeof(UiTextBufOp));
+    op->prev = NULL;
+    op->next = NULL;
     op->type = type;
     op->start = txv->startPos;
     op->end = txv->endPos + 1;
     op->len = length;
     op->text = str;
     
-    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;
 }
 
 int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen) {
@@ -327,7 +346,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: {
@@ -347,7 +366,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;
@@ -357,7 +376,7 @@
     }
     
     if(elm) {
-        UiTextBufOp *op = elm->data;
+        UiTextBufOp *op = elm;
         mgr->event = 0;
         switch(op->type) {
             case UI_TEXTBUF_INSERT: {

mercurial