--- 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 ------------------------------ */