| 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 UIWIDGET ui_textarea(UiObject *obj, UiText *value) { |
35 /* |
| 36 QTextDocument *txtdoc = value && value->obj ? (QTextDocument*)value->obj : new QTextDocument(); |
36 * Gets or creates a QTextDocument for the UiText value and initializes it |
| 37 |
37 * with the UiText string value |
| 38 if(value) { |
38 */ |
| 39 if(value->value && value->obj) { |
39 static QTextDocument* get_or_create_doc(UiText *value) { |
| 40 QString str = QString::fromUtf8(value->value); |
40 QTextDocument *document = nullptr; |
| 41 txtdoc->setPlainText(str); |
41 if(value->data1) { |
| 42 } |
42 document = (QTextDocument*)value->data1; |
| |
43 } else { |
| |
44 document = new QTextDocument(); |
| |
45 if(value->value.ptr) { |
| |
46 QString str = QString::fromUtf8(value->value.ptr); |
| |
47 document->setPlainText(str); |
| |
48 } |
| |
49 } |
| |
50 |
| |
51 if(value->value.free) { |
| |
52 value->value.free(value->value.ptr); |
| |
53 } |
| |
54 value->value.ptr = NULL; |
| |
55 value->value.free = NULL; |
| |
56 |
| |
57 return document; |
| |
58 } |
| |
59 |
| |
60 UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs args) { |
| |
61 UiContainerPrivate *ctn = ui_obj_container(obj); |
| |
62 UI_APPLY_LAYOUT(ctn->layout, args); |
| |
63 |
| |
64 QTextEdit *textarea = new QTextEdit(); |
| |
65 ctn->add(textarea, true); |
| |
66 |
| |
67 QTextDocument *document = nullptr; |
| |
68 |
| |
69 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING); |
| |
70 if(var) { |
| |
71 UiText *value = (UiText*)var->value; |
| 43 |
72 |
| |
73 document = get_or_create_doc(value); |
| |
74 |
| |
75 value->save = ui_textarea_save; |
| |
76 value->restore = ui_textarea_restore; |
| |
77 value->destroy = ui_textarea_text_destroy; |
| 44 value->get = ui_textarea_get; |
78 value->get = ui_textarea_get; |
| 45 value->set = ui_textarea_set; |
79 value->set = ui_textarea_set; |
| 46 value->getsubstr = ui_textarea_getsubstr; |
80 value->getsubstr = ui_textarea_getsubstr; |
| 47 value->insert = ui_textarea_insert; |
81 value->insert = ui_textarea_insert; |
| 48 value->setposition = ui_textarea_setposition; |
82 value->setposition = ui_textarea_setposition; |
| 49 value->position = ui_textarea_position; |
83 value->position = ui_textarea_position; |
| |
84 value->setselection = ui_textarea_setselection; |
| 50 value->selection = ui_textarea_selection; |
85 value->selection = ui_textarea_selection; |
| 51 value->length = ui_textarea_length; |
86 value->length = ui_textarea_length; |
| 52 value->remove = ui_textarea_remove; |
87 value->remove = ui_textarea_remove; |
| 53 value->obj = txtdoc; |
88 value->obj = textarea; |
| 54 value->value = NULL; |
89 value->data1 = document; |
| 55 } |
|
| 56 |
|
| 57 UiContainer *ct = uic_get_current_container(obj); |
|
| 58 QTextEdit *textedit = new QTextEdit(); |
|
| 59 textedit->setDocument(txtdoc); |
|
| 60 ct->add(textedit, true); |
|
| 61 |
|
| 62 return textedit; |
|
| 63 } |
|
| 64 |
|
| 65 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) { |
|
| 66 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_TEXT); |
|
| 67 if(var) { |
|
| 68 UiText *value = (UiText*)var->value; |
|
| 69 return ui_textarea(obj, value); |
|
| 70 } else { |
90 } else { |
| 71 // TODO: error |
91 document = new QTextDocument(); |
| 72 } |
92 } |
| 73 return NULL; |
93 |
| 74 } |
94 textarea->setDocument(document); |
| 75 |
95 |
| |
96 return textarea; |
| |
97 } |
| |
98 |
| |
99 void ui_textarea_save(UiText *text) { |
| |
100 // NOOP |
| |
101 } |
| |
102 |
| |
103 void ui_textarea_restore(UiText *text) { |
| |
104 QTextEdit *textarea = (QTextEdit*)text->obj; |
| |
105 QTextDocument *document = get_or_create_doc(text); |
| |
106 textarea->setDocument(document); |
| |
107 } |
| |
108 |
| |
109 void ui_textarea_text_destroy(UiText *text) { |
| |
110 QTextDocument *document = (QTextDocument*)text->data1; |
| |
111 if(document) { |
| |
112 delete document; |
| |
113 } |
| |
114 } |
| 76 |
115 |
| 77 char* ui_textarea_get(UiText *text) { |
116 char* ui_textarea_get(UiText *text) { |
| 78 if(text->value) { |
117 // clean previous value |
| 79 free(text->value); |
118 if(text->value.free) { |
| 80 } |
119 text->value.free(text->value.ptr); |
| 81 |
120 } |
| 82 QTextDocument *doc = (QTextDocument*)text->obj; |
121 |
| |
122 // get string |
| |
123 QTextDocument *doc = (QTextDocument*)text->data1; |
| 83 QString str = doc->toPlainText(); |
124 QString str = doc->toPlainText(); |
| 84 QByteArray array = str.toLocal8Bit(); |
125 QByteArray array = str.toUtf8(); |
| 85 const char *cstr = array.constData(); |
126 const char *cstr = array.constData(); |
| 86 |
127 |
| 87 if(text->value) { |
128 // store a copy of the string in the UiText value |
| 88 free(text->value); |
129 text->value.ptr = strdup(cstr); |
| 89 } |
130 text->value.free = free; |
| 90 text->value = strdup(cstr); |
131 return text->value.ptr; |
| 91 return text->value; |
132 } |
| 92 } |
133 |
| 93 |
134 void ui_textarea_set(UiText *text, const char *str) { |
| 94 void ui_textarea_set(UiText *text, char *str) { |
135 if(text->value.free) { |
| 95 // set text |
136 text->value.free(text->value.ptr); |
| 96 QTextDocument *doc = (QTextDocument*)text->obj; |
137 } |
| |
138 text->value.ptr = NULL; |
| |
139 text->value.free = NULL; |
| |
140 |
| |
141 QTextDocument *doc = (QTextDocument*)text->data1; |
| 97 QString qstr = QString::fromUtf8(str); |
142 QString qstr = QString::fromUtf8(str); |
| 98 doc->setPlainText(qstr); |
143 doc->setPlainText(qstr); |
| 99 // cleanup |
|
| 100 if(text->value) { |
|
| 101 free(text->value); |
|
| 102 } |
|
| 103 text->value = NULL; |
|
| 104 } |
144 } |
| 105 |
145 |
| 106 char* ui_textarea_getsubstr(UiText *text, int begin, int end) { |
146 char* ui_textarea_getsubstr(UiText *text, int begin, int end) { |
| 107 QTextDocument *doc = (QTextDocument*)text->obj; |
147 QTextDocument *doc = (QTextDocument*)text->data1; |
| 108 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; |
| 109 } |
155 } |
| 110 |
156 |
| 111 void ui_textarea_insert(UiText *text, int pos, char *str) { |
157 void ui_textarea_insert(UiText *text, int pos, char *str) { |
| 112 QTextDocument *doc = (QTextDocument*)text->obj; |
158 QTextDocument *doc = (QTextDocument*)text->data1; |
| |
159 QTextCursor cursor(doc); |
| |
160 cursor.setPosition(pos); |
| |
161 cursor.insertText(str); |
| 113 } |
162 } |
| 114 |
163 |
| 115 void ui_textarea_setposition(UiText *text, int pos) { |
164 void ui_textarea_setposition(UiText *text, int pos) { |
| |
165 QTextEdit *textview = (QTextEdit*)text->obj; |
| |
166 QTextCursor cursor = textview->textCursor(); |
| |
167 cursor.setPosition(pos); |
| |
168 textview->setTextCursor(cursor); |
| |
169 } |
| |
170 |
| |
171 int ui_textarea_position(UiText *text) { |
| |
172 QTextEdit *textview = (QTextEdit*)text->obj; |
| |
173 QTextCursor cursor = textview->textCursor(); |
| |
174 return cursor.position(); |
| |
175 } |
| |
176 |
| |
177 void ui_textarea_selection(UiText *text, int *begin, int *end) { |
| |
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); |
| |
196 } |
| |
197 |
| |
198 int ui_textarea_length(UiText *text) { |
| |
199 QTextDocument *doc = (QTextDocument*)text->data1; |
| |
200 return doc->characterCount(); |
| |
201 } |
| |
202 |
| |
203 void ui_textarea_remove(UiText *text, int begin, int end) { |
| 116 // TODO |
204 // TODO |
| 117 } |
205 } |
| 118 |
206 |
| 119 int ui_textarea_position(UiText *text) { |
207 /* ------------------------------ TextField ------------------------------ */ |
| 120 QTextDocument *doc = (QTextDocument*)text->obj; |
208 |
| 121 return 0; // TODO |
209 static UIWIDGET create_textfield(UiObject *obj, UiTextFieldArgs args, bool password, bool frameless) { |
| 122 } |
210 UiContainerPrivate *ctn = ui_obj_container(obj); |
| 123 |
211 UI_APPLY_LAYOUT(ctn->layout, args); |
| 124 void ui_textarea_selection(UiText *text, int *begin, int *end) { |
212 |
| 125 QTextDocument *doc = (QTextDocument*)text->obj; |
|
| 126 } |
|
| 127 |
|
| 128 int ui_textarea_length(UiText *text) { |
|
| 129 QTextDocument *doc = (QTextDocument*)text->obj; |
|
| 130 return 0; // TODO |
|
| 131 } |
|
| 132 |
|
| 133 void ui_textarea_remove(UiText *text, int begin, int end) { |
|
| 134 QTextDocument *doc = (QTextDocument*)text->obj; |
|
| 135 } |
|
| 136 |
|
| 137 |
|
| 138 /* ------------------- TextField ------------------- */ |
|
| 139 |
|
| 140 UIWIDGET ui_textfield(UiObject *obj, UiString *value) { |
|
| 141 QLineEdit *textfield = new QLineEdit(); |
213 QLineEdit *textfield = new QLineEdit(); |
| 142 |
214 ctn->add(textfield, false); |
| 143 UiContainer *ct = uic_get_current_container(obj); |
215 |
| 144 ct->add(textfield, false); |
216 if(password) { |
| 145 |
217 textfield->setEchoMode(QLineEdit::Password); |
| 146 if(value) { |
218 } |
| 147 if(value->value) { |
219 |
| 148 QString str = QString::fromUtf8(value->value); |
220 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING); |
| |
221 if(var) { |
| |
222 UiString *value = (UiString*)var->value; |
| |
223 if(value->value.ptr) { |
| |
224 QString str = QString::fromUtf8(value->value.ptr); |
| 149 textfield->setText(str); |
225 textfield->setText(str); |
| 150 free(value->value); |
226 if(value->value.free) { |
| 151 value->value = NULL; |
227 value->value.free(value->value.ptr); |
| 152 } |
228 } |
| |
229 value->value.ptr = NULL; |
| |
230 } |
| |
231 |
| 153 value->set = ui_textfield_set; |
232 value->set = ui_textfield_set; |
| 154 value->get = ui_textfield_get; |
233 value->get = ui_textfield_get; |
| 155 value->obj = textfield; |
234 value->obj = textfield; |
| 156 } |
235 } |
| 157 |
236 |
| 158 return textfield; |
237 return textfield; |
| 159 } |
238 } |
| 160 |
239 |
| 161 UIWIDGET ui_textfield_nv(UiObject *obj, char *varname) { |
240 UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs args) { |
| 162 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_STRING); |
241 return create_textfield(obj, args, false, false); |
| 163 if(var) { |
242 } |
| 164 UiString *value = (UiString*)var->value; |
243 |
| 165 return ui_textfield(obj, value); |
244 UIWIDGET ui_frameless_textfield_create(UiObject* obj, UiTextFieldArgs args) { |
| 166 } else { |
245 return create_textfield(obj, args, false, true); |
| 167 // TODO: error |
246 } |
| 168 } |
247 |
| 169 return NULL; |
248 UIWIDGET ui_passwordfield_create(UiObject* obj, UiTextFieldArgs args) { |
| |
249 return create_textfield(obj, args, true, false); |
| 170 } |
250 } |
| 171 |
251 |
| 172 char* ui_textfield_get(UiString *str) { |
252 char* ui_textfield_get(UiString *str) { |
| 173 QLineEdit *textfield = (QLineEdit*)str->obj; |
253 QLineEdit *textfield = (QLineEdit*)str->obj; |
| 174 QString qstr = textfield->text(); |
254 QString qstr = textfield->text(); |
| 175 |
255 |
| 176 if(str->value) { |
256 if(str->value.free) { |
| 177 free(str->value); |
257 str->value.free(str->value.ptr); |
| 178 } |
258 } |
| 179 QByteArray array = qstr.toLocal8Bit(); |
259 QByteArray array = qstr.toUtf8(); |
| 180 const char *cstr = array.constData(); |
260 const char *cstr = array.constData(); |
| 181 str->value = strdup(cstr); |
261 str->value.ptr = strdup(cstr); |
| 182 |
262 str->value.free = free; |
| 183 return str->value; |
263 |
| 184 } |
264 return str->value.ptr; |
| 185 |
265 } |
| 186 void ui_textfield_set(UiString *str, char *value) { |
266 |
| |
267 void ui_textfield_set(UiString *str, const char *value) { |
| 187 QLineEdit *textfield = (QLineEdit*)str->obj; |
268 QLineEdit *textfield = (QLineEdit*)str->obj; |
| 188 QString qstr = QString::fromUtf8(value); |
269 QString qstr = QString::fromUtf8(value); |
| 189 textfield->setText(qstr); |
270 textfield->setText(qstr); |
| 190 |
271 |
| 191 if(str->value) { |
272 if(str->value.free) { |
| 192 free(str->value); |
273 str->value.free(str->value.ptr); |
| 193 } |
274 } |
| 194 str->value = NULL; |
275 str->value.ptr = NULL; |
| 195 } |
276 } |