ui/qt/text.cpp

6 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 28 Mar 2025 21:50:45 +0100 (6 days ago)
changeset 527
7273a78e8e5b
parent 524
ce7a198b54b0
child 528
fb1dca7d83da
permissions
-rw-r--r--

add textarea (QT)

56
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "text.h"
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include "container.h"
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include "../common/context.h"
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "../common/document.h"
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
87e3a5dc66dd added text widget (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
527
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
36 static QTextDocument* create_doc(UiText *value) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
37 QTextDocument *document = nullptr;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
38 if(value->data1) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
39 document = (QTextDocument*)value->data1;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
40 } else {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
41 document = new QTextDocument();
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
42 if(value->value.ptr) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
43 QString str = QString::fromUtf8(value->value.ptr);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
44 document->setPlainText(str);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
45 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
46 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
47
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
48 if(value->value.free) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
49 value->value.free(value->value.ptr);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
50 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
51 value->value.ptr = NULL;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
52 value->value.free = NULL;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
53
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
54 return document;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
55 }
523
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
56
527
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
57 UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs args) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
58 UiContainerPrivate *ctn = ui_obj_container(obj);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
59 UI_APPLY_LAYOUT(ctn->layout, args);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
60
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
61 QTextEdit *textarea = new QTextEdit();
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
62 ctn->add(textarea, true);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
63
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
64 QTextDocument *document = nullptr;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
65
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
66 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
67 if(var) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
68 UiText *value = (UiText*)var->value;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
69
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
70 document = create_doc(value);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
71
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
72 if(value->value.free) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
73 value->value.free(value->value.ptr);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
74 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
75 value->value.ptr = NULL;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
76 value->value.free = NULL;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
77
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
78 value->save = ui_textarea_save;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
79 value->restore = ui_textarea_restore;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
80 value->destroy = ui_textarea_text_destroy;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
81 value->get = ui_textarea_get;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
82 value->set = ui_textarea_set;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
83 value->getsubstr = ui_textarea_getsubstr;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
84 value->insert = ui_textarea_insert;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
85 value->setposition = ui_textarea_setposition;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
86 value->position = ui_textarea_position;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
87 value->selection = ui_textarea_selection;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
88 value->length = ui_textarea_length;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
89 value->remove = ui_textarea_remove;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
90 value->obj = textarea;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
91 value->data1 = document;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
92 } else {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
93 document = new QTextDocument();
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
94 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
95
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
96 textarea->setDocument(document);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
97
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
98 return textarea;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
99 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
100
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
101 void ui_textarea_save(UiText *text) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
102 // NOOP
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
103 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
104
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
105 void ui_textarea_restore(UiText *text) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
106 QTextEdit *textarea = (QTextEdit*)text->obj;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
107 QTextDocument *document = create_doc(text);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
108 textarea->setDocument(document);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
109 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
110
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
111 void ui_textarea_text_destroy(UiText *text) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
112
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
113 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
114
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
115 char* ui_textarea_get(UiText *text) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
116 if(text->value.free) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
117 text->value.free(text->value.ptr);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
118 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
119
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
120 QTextDocument *doc = (QTextDocument*)text->data1;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
121 QString str = doc->toPlainText();
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
122 QByteArray array = str.toUtf8();
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
123 const char *cstr = array.constData();
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
124
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
125 text->value.ptr = strdup(cstr);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
126 text->value.free = free;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
127 return text->value.ptr;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
128 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
129
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
130 void ui_textarea_set(UiText *text, const char *str) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
131 if(text->value.free) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
132 text->value.free(text->value.ptr);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
133 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
134 text->value.ptr = NULL;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
135 text->value.free = NULL;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
136
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
137 QTextDocument *doc = (QTextDocument*)text->data1;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
138 QString qstr = QString::fromUtf8(str);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
139 doc->setPlainText(qstr);
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
140 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
141
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
142 char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
143 QTextDocument *doc = (QTextDocument*)text->obj;
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
144 return NULL; // TODO
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
145 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
146
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
147 void ui_textarea_insert(UiText *text, int pos, char *str) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
148 // TODO
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
149 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
150
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
151 void ui_textarea_setposition(UiText *text, int pos) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
152 // TODO
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
153 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
154
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
155 int ui_textarea_position(UiText *text) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
156 return 0; // TODO
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
157 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
158
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
159 void ui_textarea_selection(UiText *text, int *begin, int *end) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
160 // TODO
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
161 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
162
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
163 int ui_textarea_length(UiText *text) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
164 return 0; // TODO
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
165 }
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
166
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
167 void ui_textarea_remove(UiText *text, int begin, int end) {
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
168 // TODO
7273a78e8e5b add textarea (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 524
diff changeset
169 }
523
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
170
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
171 /* ------------------------------ TextField ------------------------------ */
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
172
524
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
173 static UIWIDGET create_textfield(UiObject *obj, UiTextFieldArgs args, bool password, bool frameless) {
523
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
174 UiContainerPrivate *ctn = ui_obj_container(obj);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
175 UI_APPLY_LAYOUT(ctn->layout, args);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
176
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
177 QLineEdit *textfield = new QLineEdit();
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
178 ctn->add(textfield, false);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
179
524
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
180 if(password) {
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
181 textfield->setEchoMode(QLineEdit::Password);
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
182 }
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
183
523
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
184 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
185 if(var) {
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
186 UiString *value = (UiString*)var->value;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
187 if(value->value.ptr) {
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
188 QString str = QString::fromUtf8(value->value.ptr);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
189 textfield->setText(str);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
190 if(value->value.free) {
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
191 value->value.free(value->value.ptr);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
192 }
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
193 value->value.ptr = NULL;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
194 }
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
195
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
196 value->set = ui_textfield_set;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
197 value->get = ui_textfield_get;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
198 value->obj = textfield;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
199 }
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
200
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
201 return textfield;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
202 }
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
203
524
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
204 UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs args) {
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
205 return create_textfield(obj, args, false, false);
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
206 }
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
207
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
208 UIWIDGET ui_frameless_textfield_create(UiObject* obj, UiTextFieldArgs args) {
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
209 return create_textfield(obj, args, false, true);
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
210 }
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
211
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
212 UIWIDGET ui_passwordfield_create(UiObject* obj, UiTextFieldArgs args) {
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
213 return create_textfield(obj, args, true, false);
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
214 }
ce7a198b54b0 implement passwordfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 523
diff changeset
215
523
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
216 char* ui_textfield_get(UiString *str) {
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
217 QLineEdit *textfield = (QLineEdit*)str->obj;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
218 QString qstr = textfield->text();
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
219
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
220 if(str->value.free) {
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
221 str->value.free(str->value.ptr);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
222 }
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
223 QByteArray array = qstr.toUtf8();
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
224 const char *cstr = array.constData();
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
225 str->value.ptr = strdup(cstr);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
226 str->value.free = free;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
227
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
228 return str->value.ptr;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
229 }
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
230
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
231 void ui_textfield_set(UiString *str, const char *value) {
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
232 QLineEdit *textfield = (QLineEdit*)str->obj;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
233 QString qstr = QString::fromUtf8(value);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
234 textfield->setText(qstr);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
235
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
236 if(str->value.free) {
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
237 str->value.free(str->value.ptr);
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
238 }
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
239 str->value.ptr = NULL;
0797c87a2c02 add textfield (QT)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 516
diff changeset
240 }

mercurial