4 days ago
implement remaining textarea functions (QT)
ui/qt/text.cpp | file | annotate | diff | comparison | revisions | |
ui/qt/text.h | file | annotate | diff | comparison | revisions | |
ui/ui/toolkit.h | file | annotate | diff | comparison | revisions |
--- a/ui/qt/text.cpp Fri Mar 28 21:50:45 2025 +0100 +++ b/ui/qt/text.cpp Sat Mar 29 07:57:16 2025 +0100 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2014 Olaf Wintermann. All rights reserved. + * Copyright 2025 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -32,8 +32,11 @@ #include "../common/context.h" #include "../common/document.h" - -static QTextDocument* create_doc(UiText *value) { +/* + * Gets or creates a QTextDocument for the UiText value and initializes it + * with the UiText string value + */ +static QTextDocument* get_or_create_doc(UiText *value) { QTextDocument *document = nullptr; if(value->data1) { document = (QTextDocument*)value->data1; @@ -67,13 +70,7 @@ 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; + document = get_or_create_doc(value); value->save = ui_textarea_save; value->restore = ui_textarea_restore; @@ -84,6 +81,7 @@ value->insert = ui_textarea_insert; value->setposition = ui_textarea_setposition; value->position = ui_textarea_position; + value->setselection = ui_textarea_setselection; value->selection = ui_textarea_selection; value->length = ui_textarea_length; value->remove = ui_textarea_remove; @@ -104,24 +102,30 @@ void ui_textarea_restore(UiText *text) { QTextEdit *textarea = (QTextEdit*)text->obj; - QTextDocument *document = create_doc(text); + QTextDocument *document = get_or_create_doc(text); textarea->setDocument(document); } void ui_textarea_text_destroy(UiText *text) { - + QTextDocument *document = (QTextDocument*)text->data1; + if(document) { + delete document; + } } char* ui_textarea_get(UiText *text) { + // clean previous value if(text->value.free) { text->value.free(text->value.ptr); } + // get string QTextDocument *doc = (QTextDocument*)text->data1; QString str = doc->toPlainText(); QByteArray array = str.toUtf8(); const char *cstr = array.constData(); + // store a copy of the string in the UiText value text->value.ptr = strdup(cstr); text->value.free = free; return text->value.ptr; @@ -140,28 +144,60 @@ } char* ui_textarea_getsubstr(UiText *text, int begin, int end) { - QTextDocument *doc = (QTextDocument*)text->obj; - return NULL; // TODO + QTextDocument *doc = (QTextDocument*)text->data1; + QTextCursor cursor(doc); + cursor.setPosition(begin, QTextCursor::MoveAnchor); + cursor.setPosition(end, QTextCursor::KeepAnchor); + QString str = cursor.selectedText(); + QByteArray bytes = str.toUtf8(); + const char *cstr = bytes.constData(); + return cstr ? strdup(cstr) : NULL; } void ui_textarea_insert(UiText *text, int pos, char *str) { - // TODO + QTextDocument *doc = (QTextDocument*)text->data1; + QTextCursor cursor(doc); + cursor.setPosition(pos); + cursor.insertText(str); } void ui_textarea_setposition(UiText *text, int pos) { - // TODO + QTextEdit *textview = (QTextEdit*)text->obj; + QTextCursor cursor = textview->textCursor(); + cursor.setPosition(pos); + textview->setTextCursor(cursor); } int ui_textarea_position(UiText *text) { - return 0; // TODO + QTextEdit *textview = (QTextEdit*)text->obj; + QTextCursor cursor = textview->textCursor(); + return cursor.position(); } void ui_textarea_selection(UiText *text, int *begin, int *end) { - // TODO + QTextEdit *textview = (QTextEdit*)text->obj; + QTextCursor cursor = textview->textCursor(); + if(cursor.hasSelection()) { + if(begin) { + *begin = cursor.selectionStart(); + } + if(end) { + *end = cursor.selectionEnd(); + } + } +} + +void ui_textarea_setselection(UiText *text, int begin, int end) { + QTextEdit *textview = (QTextEdit*)text->obj; + QTextCursor cursor = textview->textCursor(); + cursor.setPosition(begin, QTextCursor::MoveAnchor); + cursor.setPosition(end, QTextCursor::KeepAnchor); + textview->setTextCursor(cursor); } int ui_textarea_length(UiText *text) { - return 0; // TODO + QTextDocument *doc = (QTextDocument*)text->data1; + return doc->characterCount(); } void ui_textarea_remove(UiText *text, int begin, int end) {
--- a/ui/qt/text.h Fri Mar 28 21:50:45 2025 +0100 +++ b/ui/qt/text.h Sat Mar 29 07:57:16 2025 +0100 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2014 Olaf Wintermann. All rights reserved. + * Copyright 2025 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -46,6 +46,7 @@ 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_setselection(UiText *text, int begin, int end); 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);
--- a/ui/ui/toolkit.h Fri Mar 28 21:50:45 2025 +0100 +++ b/ui/ui/toolkit.h Sat Mar 29 07:57:16 2025 +0100 @@ -377,6 +377,7 @@ void (*insert)(UiText*, int, char*); void (*setposition)(UiText*,int); int (*position)(UiText*); + void (*setselection)(UiText*, int, int); /* text, begin, end */ void (*selection)(UiText*, int*, int*); /* text, begin, end */ int (*length)(UiText*); void (*remove)(UiText*, int, int); /* text, begin, end */