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 |
36 static QTextDocument* create_doc(UiText *value) { |
|
37 QTextDocument *document = nullptr; |
|
38 if(value->data1) { |
|
39 document = (QTextDocument*)value->data1; |
|
40 } else { |
|
41 document = new QTextDocument(); |
|
42 if(value->value.ptr) { |
|
43 QString str = QString::fromUtf8(value->value.ptr); |
|
44 document->setPlainText(str); |
|
45 } |
|
46 } |
|
47 |
|
48 if(value->value.free) { |
|
49 value->value.free(value->value.ptr); |
|
50 } |
|
51 value->value.ptr = NULL; |
|
52 value->value.free = NULL; |
|
53 |
|
54 return document; |
|
55 } |
|
56 |
|
57 UIWIDGET ui_textarea_create(UiObject *obj, UiTextAreaArgs args) { |
|
58 UiContainerPrivate *ctn = ui_obj_container(obj); |
|
59 UI_APPLY_LAYOUT(ctn->layout, args); |
|
60 |
|
61 QTextEdit *textarea = new QTextEdit(); |
|
62 ctn->add(textarea, true); |
|
63 |
|
64 QTextDocument *document = nullptr; |
|
65 |
|
66 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING); |
|
67 if(var) { |
|
68 UiText *value = (UiText*)var->value; |
|
69 |
|
70 document = 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 |
|
78 value->save = ui_textarea_save; |
|
79 value->restore = ui_textarea_restore; |
|
80 value->destroy = ui_textarea_text_destroy; |
|
81 value->get = ui_textarea_get; |
|
82 value->set = ui_textarea_set; |
|
83 value->getsubstr = ui_textarea_getsubstr; |
|
84 value->insert = ui_textarea_insert; |
|
85 value->setposition = ui_textarea_setposition; |
|
86 value->position = ui_textarea_position; |
|
87 value->selection = ui_textarea_selection; |
|
88 value->length = ui_textarea_length; |
|
89 value->remove = ui_textarea_remove; |
|
90 value->obj = textarea; |
|
91 value->data1 = document; |
|
92 } else { |
|
93 document = new QTextDocument(); |
|
94 } |
|
95 |
|
96 textarea->setDocument(document); |
|
97 |
|
98 return textarea; |
|
99 } |
|
100 |
|
101 void ui_textarea_save(UiText *text) { |
|
102 // NOOP |
|
103 } |
|
104 |
|
105 void ui_textarea_restore(UiText *text) { |
|
106 QTextEdit *textarea = (QTextEdit*)text->obj; |
|
107 QTextDocument *document = create_doc(text); |
|
108 textarea->setDocument(document); |
|
109 } |
|
110 |
|
111 void ui_textarea_text_destroy(UiText *text) { |
|
112 |
|
113 } |
|
114 |
|
115 char* ui_textarea_get(UiText *text) { |
|
116 if(text->value.free) { |
|
117 text->value.free(text->value.ptr); |
|
118 } |
|
119 |
|
120 QTextDocument *doc = (QTextDocument*)text->data1; |
|
121 QString str = doc->toPlainText(); |
|
122 QByteArray array = str.toUtf8(); |
|
123 const char *cstr = array.constData(); |
|
124 |
|
125 text->value.ptr = strdup(cstr); |
|
126 text->value.free = free; |
|
127 return text->value.ptr; |
|
128 } |
|
129 |
|
130 void ui_textarea_set(UiText *text, const char *str) { |
|
131 if(text->value.free) { |
|
132 text->value.free(text->value.ptr); |
|
133 } |
|
134 text->value.ptr = NULL; |
|
135 text->value.free = NULL; |
|
136 |
|
137 QTextDocument *doc = (QTextDocument*)text->data1; |
|
138 QString qstr = QString::fromUtf8(str); |
|
139 doc->setPlainText(qstr); |
|
140 } |
|
141 |
|
142 char* ui_textarea_getsubstr(UiText *text, int begin, int end) { |
|
143 QTextDocument *doc = (QTextDocument*)text->obj; |
|
144 return NULL; // TODO |
|
145 } |
|
146 |
|
147 void ui_textarea_insert(UiText *text, int pos, char *str) { |
|
148 // TODO |
|
149 } |
|
150 |
|
151 void ui_textarea_setposition(UiText *text, int pos) { |
|
152 // TODO |
|
153 } |
|
154 |
|
155 int ui_textarea_position(UiText *text) { |
|
156 return 0; // TODO |
|
157 } |
|
158 |
|
159 void ui_textarea_selection(UiText *text, int *begin, int *end) { |
|
160 // TODO |
|
161 } |
|
162 |
|
163 int ui_textarea_length(UiText *text) { |
|
164 return 0; // TODO |
|
165 } |
|
166 |
|
167 void ui_textarea_remove(UiText *text, int begin, int end) { |
|
168 // TODO |
|
169 } |
37 |
170 |
38 /* ------------------------------ TextField ------------------------------ */ |
171 /* ------------------------------ TextField ------------------------------ */ |
39 |
172 |
40 static UIWIDGET create_textfield(UiObject *obj, UiTextFieldArgs args, bool password, bool frameless) { |
173 static UIWIDGET create_textfield(UiObject *obj, UiTextFieldArgs args, bool password, bool frameless) { |
41 UiContainerPrivate *ctn = ui_obj_container(obj); |
174 UiContainerPrivate *ctn = ui_obj_container(obj); |