58 (XtCallbackProc)ui_text_selection_callback, |
58 (XtCallbackProc)ui_text_selection_callback, |
59 uitext); |
59 uitext); |
60 |
60 |
61 // bind value |
61 // bind value |
62 if(value) { |
62 if(value) { |
63 if(value->value) { |
63 if(value->value.ptr) { |
64 XmTextSetString(text_area, value->value); |
64 XmTextSetString(text_area, value->value.ptr); |
65 XtFree(value->value); |
65 value->value.free(value->value.ptr); |
66 } |
66 } |
67 |
67 |
68 value->set = ui_textarea_set; |
68 value->set = ui_textarea_set; |
69 value->get = ui_textarea_get; |
69 value->get = ui_textarea_get; |
70 value->getsubstr = ui_textarea_getsubstr; |
70 value->getsubstr = ui_textarea_getsubstr; |
71 value->insert = ui_textarea_insert; |
71 value->insert = ui_textarea_insert; |
72 value->setposition = ui_textarea_setposition; |
72 value->setposition = ui_textarea_setposition; |
73 value->position = ui_textarea_position; |
73 value->position = ui_textarea_position; |
74 value->selection = ui_textarea_selection; |
74 value->selection = ui_textarea_selection; |
75 value->length = ui_textarea_length; |
75 value->length = ui_textarea_length; |
76 value->value = NULL; |
76 value->value.ptr = NULL; |
77 value->obj = text_area; |
77 value->obj = text_area; |
78 |
78 |
79 if(!value->undomgr) { |
79 if(!value->undomgr) { |
80 value->undomgr = ui_create_undomgr(); |
80 value->undomgr = ui_create_undomgr(); |
81 } |
81 } |
89 |
89 |
90 return text_area; |
90 return text_area; |
91 } |
91 } |
92 |
92 |
93 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) { |
93 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) { |
94 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_TEXT); |
94 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_TEXT); |
95 if(var) { |
95 if(var) { |
96 UiText *value = var->value; |
96 UiText *value = var->value; |
97 return ui_textarea(obj, value); |
97 return ui_textarea(obj, value); |
98 } else { |
98 } else { |
99 // TODO: error |
99 // TODO: error |
100 } |
100 } |
101 return NULL; |
101 return NULL; |
102 } |
102 } |
103 |
103 |
104 char* ui_textarea_get(UiText *text) { |
104 char* ui_textarea_get(UiText *text) { |
105 if(text->value) { |
105 if(text->value.ptr) { |
106 XtFree(text->value); |
106 text->value.free(text->value.ptr); |
107 } |
107 } |
108 char *str = XmTextGetString(text->obj); |
108 char *str = XmTextGetString(text->obj); |
109 text->value = str; |
109 text->value.ptr = str; |
|
110 text->value.free = (ui_freefunc)XtFree; |
110 return str; |
111 return str; |
111 } |
112 } |
112 |
113 |
113 void ui_textarea_set(UiText *text, char *str) { |
114 void ui_textarea_set(UiText *text, char *str) { |
114 if(text->value) { |
115 if(text->value.ptr) { |
115 XtFree(text->value); |
116 text->value.free(text->value.ptr); |
116 } |
117 } |
117 text->value = NULL; |
118 text->value.ptr = NULL; |
118 XmTextSetString(text->obj, str); |
119 XmTextSetString(text->obj, str); |
119 } |
120 } |
120 |
121 |
121 char* ui_textarea_getsubstr(UiText *text, int begin, int end) { |
122 char* ui_textarea_getsubstr(UiText *text, int begin, int end) { |
122 if(text->value) { |
123 if(text->value.ptr) { |
123 XtFree(text->value); |
124 text->value.free(text->value.ptr); |
124 } |
125 } |
125 int length = end - begin; |
126 int length = end - begin; |
126 char *str = XtMalloc(length + 1); |
127 char *str = XtMalloc(length + 1); |
127 XmTextGetSubstring(text->obj, begin, length, length + 1, str); |
128 XmTextGetSubstring(text->obj, begin, length, length + 1, str); |
128 text->value = str; |
129 text->value.ptr = str; |
|
130 text->value.free = (ui_freefunc)XtFree; |
129 return str; |
131 return str; |
130 } |
132 } |
131 |
133 |
132 void ui_textarea_insert(UiText *text, int pos, char *str) { |
134 void ui_textarea_insert(UiText *text, int pos, char *str) { |
133 if(text->value) { |
135 if(text->value.ptr) { |
134 XtFree(text->value); |
136 text->value.free(text->value.ptr); |
135 } |
137 } |
136 text->value = NULL; |
138 text->value.ptr = NULL; |
137 XmTextInsert(text->obj, pos, str); |
139 XmTextInsert(text->obj, pos, str); |
138 } |
140 } |
139 |
141 |
140 void ui_textarea_setposition(UiText *text, int pos) { |
142 void ui_textarea_setposition(UiText *text, int pos) { |
141 XmTextSetInsertionPosition(text->obj, pos); |
143 XmTextSetInsertionPosition(text->obj, pos); |
160 |
162 |
161 void ui_text_set(UiText *text, char *str) { |
163 void ui_text_set(UiText *text, char *str) { |
162 if(text->set) { |
164 if(text->set) { |
163 text->set(text, str); |
165 text->set(text, str); |
164 } else { |
166 } else { |
165 if(text->value) { |
167 if(text->value.ptr) { |
166 XtFree(text->value); |
168 text->value.free(text->value.ptr); |
167 } |
169 } |
168 text->value = XtNewString(str); |
170 text->value.ptr = XtNewString(str); |
|
171 text->value.free = (ui_freefunc)XtFree; |
169 } |
172 } |
170 } |
173 } |
171 |
174 |
172 char* ui_text_get(UiText *text) { |
175 char* ui_text_get(UiText *text) { |
173 if(text->get) { |
176 if(text->get) { |
174 return text->get(text); |
177 return text->get(text); |
175 } else { |
178 } else { |
176 return text->value; |
179 return text->value.ptr; |
177 } |
180 } |
178 } |
181 } |
179 |
182 |
180 |
183 |
181 UiUndoMgr* ui_create_undomgr() { |
184 UiUndoMgr* ui_create_undomgr() { |
380 ct->add(ct, textfield); |
383 ct->add(ct, textfield); |
381 XtManageChild(textfield); |
384 XtManageChild(textfield); |
382 |
385 |
383 // bind value |
386 // bind value |
384 if(value) { |
387 if(value) { |
385 if(value->value) { |
388 if(value->value.ptr) { |
386 XmTextSetString(textfield, value->value); |
389 XmTextSetString(textfield, value->value.ptr); |
387 XtFree(value->value); |
390 value->value.free(value->value.ptr); |
388 } |
391 } |
389 |
392 |
390 value->set = ui_textfield_set; |
393 value->set = ui_textfield_set; |
391 value->get = ui_textfield_get; |
394 value->get = ui_textfield_get; |
392 value->value = NULL; |
395 value->value.ptr = NULL; |
393 value->obj = textfield; |
396 value->obj = textfield; |
394 } |
397 } |
395 |
398 |
396 return textfield; |
399 return textfield; |
397 } |
400 } |
398 |
401 |
399 static UIWIDGET create_textfield_nv(UiObject *obj, int width, UiBool frameless, UiBool password, char *varname) { |
402 static UIWIDGET create_textfield_nv(UiObject *obj, int width, UiBool frameless, UiBool password, char *varname) { |
400 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_STRING); |
403 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_STRING); |
401 if(var) { |
404 if(var) { |
402 UiString *value = var->value; |
405 UiString *value = var->value; |
403 return ui_textfield(obj, value); |
406 return ui_textfield(obj, value); |
404 } else { |
407 } else { |
405 // TODO: error |
408 // TODO: error |
447 return create_textfield_nv(obj, width, FALSE, TRUE, varname); |
450 return create_textfield_nv(obj, width, FALSE, TRUE, varname); |
448 } |
451 } |
449 |
452 |
450 |
453 |
451 char* ui_textfield_get(UiString *str) { |
454 char* ui_textfield_get(UiString *str) { |
452 if(str->value) { |
455 if(str->value.ptr) { |
453 XtFree(str->value); |
456 str->value.free(str->value.ptr); |
454 } |
457 } |
455 char *value = XmTextGetString(str->obj); |
458 char *value = XmTextGetString(str->obj); |
456 str->value = value; |
459 str->value.ptr = value; |
|
460 str->value.free = (ui_freefunc)XtFree; |
457 return value; |
461 return value; |
458 } |
462 } |
459 |
463 |
460 void ui_textfield_set(UiString *str, char *value) { |
464 void ui_textfield_set(UiString *str, char *value) { |
461 if(str->value) { |
465 if(str->value.ptr) { |
462 XtFree(str->value); |
466 str->value.free(str->value.ptr); |
463 } |
467 } |
464 str->value = NULL; |
468 str->value.ptr = NULL; |
465 XmTextSetString(str->obj, value); |
469 XmTextSetString(str->obj, value); |
466 } |
470 } |
467 |
471 |