add textarea (QT)

3 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 28 Mar 2025 21:50:45 +0100 (3 days ago)
changeset 527
7273a78e8e5b
parent 526
f6a6b0b08641
child 528
fb1dca7d83da

add textarea (QT)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/text.cpp file | annotate | diff | comparison | revisions
ui/qt/text.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Thu Mar 27 22:06:19 2025 +0100
+++ b/application/main.c	Fri Mar 28 21:50:45 2025 +0100
@@ -781,7 +781,7 @@
         ui_button(obj, .label = "Button end");
         ui_newline(obj);
         
-        ui_button(obj, .label = "Button 3", .vexpand = TRUE, .vfill = TRUE);
+        ui_textarea(obj, .vexpand = TRUE, .vfill = TRUE);
         ui_newline(obj);
         
         ui_button(obj, .label = "Button Y");
--- a/ui/qt/text.cpp	Thu Mar 27 22:06:19 2025 +0100
+++ b/ui/qt/text.cpp	Fri Mar 28 21:50:45 2025 +0100
@@ -33,7 +33,140 @@
 #include "../common/document.h"
 
 
+static QTextDocument* create_doc(UiText *value) {
+    QTextDocument *document = nullptr;
+    if(value->data1) {
+        document = (QTextDocument*)value->data1;
+    } else {
+        document = new QTextDocument();
+        if(value->value.ptr) {
+            QString str = QString::fromUtf8(value->value.ptr);
+            document->setPlainText(str);
+        }
+    }
+    
+    if(value->value.free) {
+        value->value.free(value->value.ptr);
+    }
+    value->value.ptr = NULL;
+    value->value.free = NULL;
+    
+    return document;
+}
 
+UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs args) {
+    UiContainerPrivate *ctn = ui_obj_container(obj);
+    UI_APPLY_LAYOUT(ctn->layout, args);
+    
+    QTextEdit *textarea = new QTextEdit();
+    ctn->add(textarea, true);
+    
+    QTextDocument *document = nullptr;
+    
+    UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING);
+    if(var) {
+        UiText *value = (UiText*)var->value;
+        
+        document = create_doc(value);
+        
+        if(value->value.free) {
+            value->value.free(value->value.ptr);
+        }
+        value->value.ptr = NULL;
+        value->value.free = NULL;
+        
+        value->save = ui_textarea_save;
+        value->restore = ui_textarea_restore;
+        value->destroy = ui_textarea_text_destroy;
+        value->get = ui_textarea_get;
+        value->set = ui_textarea_set;
+        value->getsubstr = ui_textarea_getsubstr;
+        value->insert = ui_textarea_insert;
+        value->setposition = ui_textarea_setposition;
+        value->position = ui_textarea_position;
+        value->selection = ui_textarea_selection;
+        value->length = ui_textarea_length;
+        value->remove = ui_textarea_remove;
+        value->obj = textarea;
+        value->data1 = document;
+    } else {
+        document = new QTextDocument();
+    }
+    
+    textarea->setDocument(document);
+    
+    return textarea;
+}
+
+void ui_textarea_save(UiText *text) {
+    // NOOP
+}
+
+void ui_textarea_restore(UiText *text) {
+    QTextEdit *textarea = (QTextEdit*)text->obj;
+    QTextDocument *document = create_doc(text);
+    textarea->setDocument(document);
+}
+
+void ui_textarea_text_destroy(UiText *text) {
+    
+}
+
+char* ui_textarea_get(UiText *text) {
+    if(text->value.free) {
+        text->value.free(text->value.ptr);
+    }
+    
+    QTextDocument *doc = (QTextDocument*)text->data1;
+    QString str = doc->toPlainText();
+    QByteArray array = str.toUtf8();
+    const char *cstr = array.constData();
+    
+    text->value.ptr = strdup(cstr);
+    text->value.free = free;
+    return text->value.ptr;
+}
+
+void ui_textarea_set(UiText *text, const char *str) {
+    if(text->value.free) {
+        text->value.free(text->value.ptr);
+    }
+    text->value.ptr = NULL;
+    text->value.free = NULL;
+    
+    QTextDocument *doc = (QTextDocument*)text->data1;
+    QString qstr = QString::fromUtf8(str);
+    doc->setPlainText(qstr);
+}
+
+char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
+    QTextDocument *doc = (QTextDocument*)text->obj;
+    return NULL; // TODO
+}
+
+void ui_textarea_insert(UiText *text, int pos, char *str) {
+    // TODO
+}
+
+void ui_textarea_setposition(UiText *text, int pos) {
+    // TODO
+}
+
+int ui_textarea_position(UiText *text) {
+    return 0; // TODO
+}
+
+void ui_textarea_selection(UiText *text, int *begin, int *end) {
+    // TODO
+}
+
+int ui_textarea_length(UiText *text) {
+    return 0; // TODO
+}
+
+void ui_textarea_remove(UiText *text, int begin, int end) {
+    // TODO
+}
 
 /* ------------------------------ TextField ------------------------------ */
 
--- a/ui/qt/text.h	Thu Mar 27 22:06:19 2025 +0100
+++ b/ui/qt/text.h	Fri Mar 28 21:50:45 2025 +0100
@@ -37,6 +37,19 @@
 // value implementations
 extern "C" {    
     
+void ui_textarea_save(UiText *text);
+void ui_textarea_restore(UiText *text);
+void ui_textarea_text_destroy(UiText *text);
+char* ui_textarea_get(UiText *text);
+void ui_textarea_set(UiText *text, const char *str);
+char* ui_textarea_getsubstr(UiText *text, int begin, int end);
+void ui_textarea_insert(UiText *text, int pos, char *str);
+void ui_textarea_setposition(UiText *text, int pos);
+int ui_textarea_position(UiText *text);
+void ui_textarea_selection(UiText *text, int *begin, int *end);
+int ui_textarea_length(UiText *text);
+void ui_textarea_remove(UiText *text, int begin, int end);
+    
 char* ui_textfield_get(UiString *str) ;
 void ui_textfield_set(UiString *str, const char *value);
 

mercurial