1 /* |
1 /* |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 * |
3 * |
4 * Copyright 2014 Olaf Wintermann. All rights reserved. |
4 * Copyright 2025 Olaf Wintermann. All rights reserved. |
5 * |
5 * |
6 * Redistribution and use in source and binary forms, with or without |
6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions are met: |
7 * modification, are permitted provided that the following conditions are met: |
8 * |
8 * |
9 * 1. Redistributions of source code must retain the above copyright |
9 * 1. Redistributions of source code must retain the above copyright |
30 #include "container.h" |
30 #include "container.h" |
31 |
31 |
32 #include "../common/context.h" |
32 #include "../common/context.h" |
33 #include "../common/document.h" |
33 #include "../common/document.h" |
34 |
34 |
35 |
35 /* |
36 static QTextDocument* create_doc(UiText *value) { |
36 * Gets or creates a QTextDocument for the UiText value and initializes it |
|
37 * with the UiText string value |
|
38 */ |
|
39 static QTextDocument* get_or_create_doc(UiText *value) { |
37 QTextDocument *document = nullptr; |
40 QTextDocument *document = nullptr; |
38 if(value->data1) { |
41 if(value->data1) { |
39 document = (QTextDocument*)value->data1; |
42 document = (QTextDocument*)value->data1; |
40 } else { |
43 } else { |
41 document = new QTextDocument(); |
44 document = new QTextDocument(); |
65 |
68 |
66 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING); |
69 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING); |
67 if(var) { |
70 if(var) { |
68 UiText *value = (UiText*)var->value; |
71 UiText *value = (UiText*)var->value; |
69 |
72 |
70 document = create_doc(value); |
73 document = get_or_create_doc(value); |
71 |
|
72 if(value->value.free) { |
|
73 value->value.free(value->value.ptr); |
|
74 } |
|
75 value->value.ptr = NULL; |
|
76 value->value.free = NULL; |
|
77 |
74 |
78 value->save = ui_textarea_save; |
75 value->save = ui_textarea_save; |
79 value->restore = ui_textarea_restore; |
76 value->restore = ui_textarea_restore; |
80 value->destroy = ui_textarea_text_destroy; |
77 value->destroy = ui_textarea_text_destroy; |
81 value->get = ui_textarea_get; |
78 value->get = ui_textarea_get; |
82 value->set = ui_textarea_set; |
79 value->set = ui_textarea_set; |
83 value->getsubstr = ui_textarea_getsubstr; |
80 value->getsubstr = ui_textarea_getsubstr; |
84 value->insert = ui_textarea_insert; |
81 value->insert = ui_textarea_insert; |
85 value->setposition = ui_textarea_setposition; |
82 value->setposition = ui_textarea_setposition; |
86 value->position = ui_textarea_position; |
83 value->position = ui_textarea_position; |
|
84 value->setselection = ui_textarea_setselection; |
87 value->selection = ui_textarea_selection; |
85 value->selection = ui_textarea_selection; |
88 value->length = ui_textarea_length; |
86 value->length = ui_textarea_length; |
89 value->remove = ui_textarea_remove; |
87 value->remove = ui_textarea_remove; |
90 value->obj = textarea; |
88 value->obj = textarea; |
91 value->data1 = document; |
89 value->data1 = document; |
102 // NOOP |
100 // NOOP |
103 } |
101 } |
104 |
102 |
105 void ui_textarea_restore(UiText *text) { |
103 void ui_textarea_restore(UiText *text) { |
106 QTextEdit *textarea = (QTextEdit*)text->obj; |
104 QTextEdit *textarea = (QTextEdit*)text->obj; |
107 QTextDocument *document = create_doc(text); |
105 QTextDocument *document = get_or_create_doc(text); |
108 textarea->setDocument(document); |
106 textarea->setDocument(document); |
109 } |
107 } |
110 |
108 |
111 void ui_textarea_text_destroy(UiText *text) { |
109 void ui_textarea_text_destroy(UiText *text) { |
112 |
110 QTextDocument *document = (QTextDocument*)text->data1; |
|
111 if(document) { |
|
112 delete document; |
|
113 } |
113 } |
114 } |
114 |
115 |
115 char* ui_textarea_get(UiText *text) { |
116 char* ui_textarea_get(UiText *text) { |
|
117 // clean previous value |
116 if(text->value.free) { |
118 if(text->value.free) { |
117 text->value.free(text->value.ptr); |
119 text->value.free(text->value.ptr); |
118 } |
120 } |
119 |
121 |
|
122 // get string |
120 QTextDocument *doc = (QTextDocument*)text->data1; |
123 QTextDocument *doc = (QTextDocument*)text->data1; |
121 QString str = doc->toPlainText(); |
124 QString str = doc->toPlainText(); |
122 QByteArray array = str.toUtf8(); |
125 QByteArray array = str.toUtf8(); |
123 const char *cstr = array.constData(); |
126 const char *cstr = array.constData(); |
124 |
127 |
|
128 // store a copy of the string in the UiText value |
125 text->value.ptr = strdup(cstr); |
129 text->value.ptr = strdup(cstr); |
126 text->value.free = free; |
130 text->value.free = free; |
127 return text->value.ptr; |
131 return text->value.ptr; |
128 } |
132 } |
129 |
133 |
138 QString qstr = QString::fromUtf8(str); |
142 QString qstr = QString::fromUtf8(str); |
139 doc->setPlainText(qstr); |
143 doc->setPlainText(qstr); |
140 } |
144 } |
141 |
145 |
142 char* ui_textarea_getsubstr(UiText *text, int begin, int end) { |
146 char* ui_textarea_getsubstr(UiText *text, int begin, int end) { |
143 QTextDocument *doc = (QTextDocument*)text->obj; |
147 QTextDocument *doc = (QTextDocument*)text->data1; |
144 return NULL; // TODO |
148 QTextCursor cursor(doc); |
|
149 cursor.setPosition(begin, QTextCursor::MoveAnchor); |
|
150 cursor.setPosition(end, QTextCursor::KeepAnchor); |
|
151 QString str = cursor.selectedText(); |
|
152 QByteArray bytes = str.toUtf8(); |
|
153 const char *cstr = bytes.constData(); |
|
154 return cstr ? strdup(cstr) : NULL; |
145 } |
155 } |
146 |
156 |
147 void ui_textarea_insert(UiText *text, int pos, char *str) { |
157 void ui_textarea_insert(UiText *text, int pos, char *str) { |
148 // TODO |
158 QTextDocument *doc = (QTextDocument*)text->data1; |
|
159 QTextCursor cursor(doc); |
|
160 cursor.setPosition(pos); |
|
161 cursor.insertText(str); |
149 } |
162 } |
150 |
163 |
151 void ui_textarea_setposition(UiText *text, int pos) { |
164 void ui_textarea_setposition(UiText *text, int pos) { |
152 // TODO |
165 QTextEdit *textview = (QTextEdit*)text->obj; |
|
166 QTextCursor cursor = textview->textCursor(); |
|
167 cursor.setPosition(pos); |
|
168 textview->setTextCursor(cursor); |
153 } |
169 } |
154 |
170 |
155 int ui_textarea_position(UiText *text) { |
171 int ui_textarea_position(UiText *text) { |
156 return 0; // TODO |
172 QTextEdit *textview = (QTextEdit*)text->obj; |
|
173 QTextCursor cursor = textview->textCursor(); |
|
174 return cursor.position(); |
157 } |
175 } |
158 |
176 |
159 void ui_textarea_selection(UiText *text, int *begin, int *end) { |
177 void ui_textarea_selection(UiText *text, int *begin, int *end) { |
160 // TODO |
178 QTextEdit *textview = (QTextEdit*)text->obj; |
|
179 QTextCursor cursor = textview->textCursor(); |
|
180 if(cursor.hasSelection()) { |
|
181 if(begin) { |
|
182 *begin = cursor.selectionStart(); |
|
183 } |
|
184 if(end) { |
|
185 *end = cursor.selectionEnd(); |
|
186 } |
|
187 } |
|
188 } |
|
189 |
|
190 void ui_textarea_setselection(UiText *text, int begin, int end) { |
|
191 QTextEdit *textview = (QTextEdit*)text->obj; |
|
192 QTextCursor cursor = textview->textCursor(); |
|
193 cursor.setPosition(begin, QTextCursor::MoveAnchor); |
|
194 cursor.setPosition(end, QTextCursor::KeepAnchor); |
|
195 textview->setTextCursor(cursor); |
161 } |
196 } |
162 |
197 |
163 int ui_textarea_length(UiText *text) { |
198 int ui_textarea_length(UiText *text) { |
164 return 0; // TODO |
199 QTextDocument *doc = (QTextDocument*)text->data1; |
|
200 return doc->characterCount(); |
165 } |
201 } |
166 |
202 |
167 void ui_textarea_remove(UiText *text, int begin, int end) { |
203 void ui_textarea_remove(UiText *text, int begin, int end) { |
168 // TODO |
204 // TODO |
169 } |
205 } |