26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include <stdio.h> |
29 #include <stdio.h> |
30 #include <stdlib.h> |
30 #include <stdlib.h> |
|
31 #include <unistd.h> |
31 |
32 |
32 #include "text.h" |
33 #include "text.h" |
33 #include "container.h" |
34 #include "container.h" |
34 |
35 |
35 |
36 #include <cx/string.h> |
36 UIWIDGET ui_textarea_var(UiObject *obj, UiVar *var) { |
37 |
37 UiContainer *ct = uic_get_current_container(obj); |
38 |
|
39 |
|
40 /* ------------------------------ Text Field ------------------------------ */ |
|
41 |
|
42 static UIWIDGET create_textfield(UiObject *obj, UiTextFieldArgs args, int frameless, int password) { |
|
43 Arg xargs[16]; |
38 int n = 0; |
44 int n = 0; |
39 Arg args[16]; |
45 |
40 |
|
41 //XtSetArg(args[n], XmNeditable, TRUE); |
|
42 //n++; |
|
43 XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT); |
|
44 n++; |
|
45 |
|
46 Widget parent = ct->prepare(ct, args, &n, TRUE); |
|
47 Widget text_area = XmCreateScrolledText(parent, "text_area", args, n); |
|
48 ct->add(ct, XtParent(text_area)); |
|
49 XtManageChild(text_area); |
|
50 |
|
51 UiTextArea *uitext = ucx_mempool_malloc( |
|
52 obj->ctx->mempool, |
|
53 sizeof(UiTextArea)); |
|
54 uitext->ctx = obj->ctx; |
|
55 uitext->last_selection_state = 0; |
|
56 XtAddCallback( |
|
57 text_area, |
|
58 XmNmotionVerifyCallback, |
|
59 (XtCallbackProc)ui_text_selection_callback, |
|
60 uitext); |
|
61 |
|
62 // bind value |
|
63 if(var->value) { |
|
64 UiText *value = var->value; |
|
65 if(value->value.ptr) { |
|
66 XmTextSetString(text_area, value->value.ptr); |
|
67 value->value.free(value->value.ptr); |
|
68 } |
|
69 |
|
70 value->set = ui_textarea_set; |
|
71 value->get = ui_textarea_get; |
|
72 value->getsubstr = ui_textarea_getsubstr; |
|
73 value->insert = ui_textarea_insert; |
|
74 value->setposition = ui_textarea_setposition; |
|
75 value->position = ui_textarea_position; |
|
76 value->selection = ui_textarea_selection; |
|
77 value->length = ui_textarea_length; |
|
78 value->value.ptr = NULL; |
|
79 value->obj = text_area; |
|
80 |
|
81 if(!value->undomgr) { |
|
82 value->undomgr = ui_create_undomgr(); |
|
83 } |
|
84 |
|
85 XtAddCallback( |
|
86 text_area, |
|
87 XmNmodifyVerifyCallback, |
|
88 (XtCallbackProc)ui_text_modify_callback, |
|
89 var); |
|
90 } |
|
91 |
|
92 return text_area; |
|
93 } |
|
94 |
|
95 UIWIDGET ui_textarea(UiObject *obj, UiText *value) { |
|
96 UiVar *var = malloc(sizeof(UiVar)); |
|
97 var->value = value; |
|
98 var->type = UI_VAR_SPECIAL; |
|
99 return ui_textarea_var(obj, var); |
|
100 } |
|
101 |
|
102 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname) { |
|
103 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_TEXT); |
|
104 if(var) { |
|
105 return ui_textarea_var(obj, var); |
|
106 } else { |
|
107 // TODO: error |
|
108 } |
|
109 return NULL; |
|
110 } |
|
111 |
|
112 char* ui_textarea_get(UiText *text) { |
|
113 if(text->value.ptr) { |
|
114 text->value.free(text->value.ptr); |
|
115 } |
|
116 char *str = XmTextGetString(text->obj); |
|
117 text->value.ptr = str; |
|
118 text->value.free = (ui_freefunc)XtFree; |
|
119 return str; |
|
120 } |
|
121 |
|
122 void ui_textarea_set(UiText *text, char *str) { |
|
123 XmTextSetString(text->obj, str); |
|
124 if(text->value.ptr) { |
|
125 text->value.free(text->value.ptr); |
|
126 } |
|
127 text->value.ptr = NULL; |
|
128 } |
|
129 |
|
130 char* ui_textarea_getsubstr(UiText *text, int begin, int end) { |
|
131 if(text->value.ptr) { |
|
132 text->value.free(text->value.ptr); |
|
133 } |
|
134 int length = end - begin; |
|
135 char *str = XtMalloc(length + 1); |
|
136 XmTextGetSubstring(text->obj, begin, length, length + 1, str); |
|
137 text->value.ptr = str; |
|
138 text->value.free = (ui_freefunc)XtFree; |
|
139 return str; |
|
140 } |
|
141 |
|
142 void ui_textarea_insert(UiText *text, int pos, char *str) { |
|
143 text->value.ptr = NULL; |
|
144 XmTextInsert(text->obj, pos, str); |
|
145 if(text->value.ptr) { |
|
146 text->value.free(text->value.ptr); |
|
147 } |
|
148 } |
|
149 |
|
150 void ui_textarea_setposition(UiText *text, int pos) { |
|
151 XmTextSetInsertionPosition(text->obj, pos); |
|
152 } |
|
153 |
|
154 int ui_textarea_position(UiText *text) { |
|
155 long begin; |
|
156 long end; |
|
157 XmTextGetSelectionPosition(text->obj, &begin, &end); |
|
158 text->pos = begin; |
|
159 return text->pos; |
|
160 } |
|
161 |
|
162 void ui_textarea_selection(UiText *text, int *begin, int *end) { |
|
163 XmTextGetSelectionPosition(text->obj, (long*)begin, (long*)end); |
|
164 } |
|
165 |
|
166 int ui_textarea_length(UiText *text) { |
|
167 return (int)XmTextGetLastPosition(text->obj); |
|
168 } |
|
169 |
|
170 |
|
171 void ui_text_set(UiText *text, char *str) { |
|
172 if(text->set) { |
|
173 text->set(text, str); |
|
174 } else { |
|
175 if(text->value.ptr) { |
|
176 text->value.free(text->value.ptr); |
|
177 } |
|
178 text->value.ptr = XtNewString(str); |
|
179 text->value.free = (ui_freefunc)XtFree; |
|
180 } |
|
181 } |
|
182 |
|
183 char* ui_text_get(UiText *text) { |
|
184 if(text->get) { |
|
185 return text->get(text); |
|
186 } else { |
|
187 return text->value.ptr; |
|
188 } |
|
189 } |
|
190 |
|
191 |
|
192 UiUndoMgr* ui_create_undomgr() { |
|
193 UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr)); |
|
194 mgr->begin = NULL; |
|
195 mgr->cur = NULL; |
|
196 mgr->length = 0; |
|
197 mgr->event = 1; |
|
198 return mgr; |
|
199 } |
|
200 |
|
201 void ui_text_selection_callback( |
|
202 Widget widget, |
|
203 UiTextArea *textarea, |
|
204 XtPointer data) |
|
205 { |
|
206 long left = 0; |
|
207 long right = 0; |
|
208 XmTextGetSelectionPosition(widget, &left, &right); |
|
209 int sel = left < right ? 1 : 0; |
|
210 if(sel != textarea->last_selection_state) { |
|
211 if(sel) { |
|
212 ui_set_group(textarea->ctx, UI_GROUP_SELECTION); |
|
213 } else { |
|
214 ui_unset_group(textarea->ctx, UI_GROUP_SELECTION); |
|
215 } |
|
216 } |
|
217 textarea->last_selection_state = sel; |
|
218 } |
|
219 |
|
220 void ui_text_modify_callback(Widget widget, UiVar *var, XtPointer data) { |
|
221 UiText *value = var->value; |
|
222 if(!value->obj) { |
|
223 // TODO: bug, fix |
|
224 return; |
|
225 } |
|
226 if(!value->undomgr) { |
|
227 value->undomgr = ui_create_undomgr(); |
|
228 } |
|
229 |
|
230 XmTextVerifyCallbackStruct *txv = (XmTextVerifyCallbackStruct*)data; |
|
231 int type = txv->text->length > 0 ? UI_TEXTBUF_INSERT : UI_TEXTBUF_DELETE; |
|
232 UiUndoMgr *mgr = value->undomgr; |
|
233 if(!mgr->event) { |
|
234 return; |
|
235 } |
|
236 |
|
237 char *text = txv->text->ptr; |
|
238 int length = txv->text->length; |
|
239 |
|
240 if(mgr->cur) { |
|
241 UcxList *elm = mgr->cur->next; |
|
242 if(elm) { |
|
243 mgr->cur->next = NULL; |
|
244 while(elm) { |
|
245 elm->prev = NULL; |
|
246 UcxList *next = elm->next; |
|
247 ui_free_textbuf_op(elm->data); |
|
248 free(elm); |
|
249 elm = next; |
|
250 } |
|
251 } |
|
252 |
|
253 if(type == UI_TEXTBUF_INSERT) { |
|
254 UiTextBufOp *last_op = mgr->cur->data; |
|
255 if( |
|
256 last_op->type == UI_TEXTBUF_INSERT && |
|
257 ui_check_insertstr(last_op->text, last_op->len, text, length) == 0) |
|
258 { |
|
259 // append text to last op |
|
260 int ln = last_op->len; |
|
261 char *newtext = malloc(ln + length + 1); |
|
262 memcpy(newtext, last_op->text, ln); |
|
263 memcpy(newtext+ln, text, length); |
|
264 newtext[ln+length] = '\0'; |
|
265 |
|
266 last_op->text = newtext; |
|
267 last_op->len = ln + length; |
|
268 last_op->end += length; |
|
269 |
|
270 return; |
|
271 } |
|
272 } |
|
273 } |
|
274 |
|
275 char *str; |
|
276 if(type == UI_TEXTBUF_INSERT) { |
|
277 str = malloc(length + 1); |
|
278 memcpy(str, text, length); |
|
279 str[length] = 0; |
|
280 } else { |
|
281 length = txv->endPos - txv->startPos; |
|
282 str = malloc(length + 1); |
|
283 XmTextGetSubstring(value->obj, txv->startPos, length, length+1, str); |
|
284 } |
|
285 |
|
286 UiTextBufOp *op = malloc(sizeof(UiTextBufOp)); |
|
287 op->type = type; |
|
288 op->start = txv->startPos; |
|
289 op->end = txv->endPos + 1; |
|
290 op->len = length; |
|
291 op->text = str; |
|
292 |
|
293 UcxList *elm = ucx_list_append(NULL, op); |
|
294 mgr->cur = elm; |
|
295 mgr->begin = ucx_list_concat(mgr->begin, elm); |
|
296 } |
|
297 |
|
298 int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen) { |
|
299 // return 1 if oldstr + newstr are one word |
|
300 |
|
301 int has_space = 0; |
|
302 for(int i=0;i<oldlen;i++) { |
|
303 if(oldstr[i] < 33) { |
|
304 has_space = 1; |
|
305 break; |
|
306 } |
|
307 } |
|
308 |
|
309 for(int i=0;i<newlen;i++) { |
|
310 if(has_space && newstr[i] > 32) { |
|
311 return 1; |
|
312 } |
|
313 } |
|
314 |
|
315 return 0; |
|
316 } |
|
317 |
|
318 void ui_free_textbuf_op(UiTextBufOp *op) { |
|
319 if(op->text) { |
|
320 free(op->text); |
|
321 } |
|
322 free(op); |
|
323 } |
|
324 |
|
325 |
|
326 void ui_text_undo(UiText *value) { |
|
327 UiUndoMgr *mgr = value->undomgr; |
|
328 |
|
329 if(mgr->cur) { |
|
330 UiTextBufOp *op = mgr->cur->data; |
|
331 mgr->event = 0; |
|
332 switch(op->type) { |
|
333 case UI_TEXTBUF_INSERT: { |
|
334 XmTextReplace(value->obj, op->start, op->end, ""); |
|
335 break; |
|
336 } |
|
337 case UI_TEXTBUF_DELETE: { |
|
338 XmTextInsert(value->obj, op->start, op->text); |
|
339 break; |
|
340 } |
|
341 } |
|
342 mgr->event = 1; |
|
343 mgr->cur = mgr->cur->prev; |
|
344 } |
|
345 } |
|
346 |
|
347 void ui_text_redo(UiText *value) { |
|
348 UiUndoMgr *mgr = value->undomgr; |
|
349 |
|
350 UcxList *elm = NULL; |
|
351 if(mgr->cur) { |
|
352 if(mgr->cur->next) { |
|
353 elm = mgr->cur->next; |
|
354 } |
|
355 } else if(mgr->begin) { |
|
356 elm = mgr->begin; |
|
357 } |
|
358 |
|
359 if(elm) { |
|
360 UiTextBufOp *op = elm->data; |
|
361 mgr->event = 0; |
|
362 switch(op->type) { |
|
363 case UI_TEXTBUF_INSERT: { |
|
364 XmTextInsert(value->obj, op->start, op->text); |
|
365 break; |
|
366 } |
|
367 case UI_TEXTBUF_DELETE: { |
|
368 XmTextReplace(value->obj, op->start, op->end, ""); |
|
369 break; |
|
370 } |
|
371 } |
|
372 mgr->event = 1; |
|
373 mgr->cur = elm; |
|
374 } |
|
375 } |
|
376 |
|
377 |
|
378 /* ------------------------- textfield ------------------------- */ |
|
379 |
|
380 static UIWIDGET create_textfield(UiObject *obj, int width, UiBool frameless, UiBool password, UiString *value) { |
|
381 UiContainer *ct = uic_get_current_container(obj); |
|
382 int n = 0; |
|
383 Arg args[16]; |
|
384 XtSetArg(args[n], XmNeditMode, XmSINGLE_LINE_EDIT); |
|
385 n++; |
|
386 if(width > 0) { |
|
387 XtSetArg(args[n], XmNcolumns, width / 2 + 1); |
|
388 n++; |
|
389 } |
|
390 if(frameless) { |
46 if(frameless) { |
391 XtSetArg(args[n], XmNshadowThickness, 0); |
47 XtSetArg(xargs[n], XmNshadowThickness, 0); |
392 n++; |
48 n++; |
393 } |
49 } |
394 if(password) { |
50 if(password) { |
395 // TODO |
51 // TODO |
396 } |
52 } |
397 |
53 |
398 Widget parent = ct->prepare(ct, args, &n, FALSE); |
54 UiContainerPrivate *ctn = ui_obj_container(obj); |
399 Widget textfield = XmCreateText(parent, "text_field", args, n); |
55 UI_APPLY_LAYOUT(ctn->layout, args); |
400 ct->add(ct, textfield); |
56 |
|
57 Widget parent = ctn->prepare(ctn, xargs, &n); |
|
58 char *name = args.name ? (char*)args.name : "textfield"; |
|
59 Widget textfield = XmCreateTextField(parent, name, xargs, n); |
401 XtManageChild(textfield); |
60 XtManageChild(textfield); |
402 |
61 |
403 // bind value |
62 ui_set_widget_groups(obj->ctx, textfield, args.groups); |
404 if(value) { |
63 |
|
64 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING); |
|
65 if(var) { |
|
66 UiString *value = (UiString*)var->value; |
|
67 value->obj = textfield; |
|
68 value->get = ui_textfield_get; |
|
69 value->set = ui_textfield_set; |
|
70 |
405 if(value->value.ptr) { |
71 if(value->value.ptr) { |
406 XmTextSetString(textfield, value->value.ptr); |
72 ui_textfield_set(value, value->value.ptr); |
407 value->value.free(value->value.ptr); |
73 } |
408 } |
|
409 |
|
410 value->set = ui_textfield_set; |
|
411 value->get = ui_textfield_get; |
|
412 value->value.ptr = NULL; |
|
413 value->obj = textfield; |
|
414 } |
74 } |
415 |
75 |
416 return textfield; |
76 return textfield; |
417 } |
77 } |
418 |
78 |
419 static UIWIDGET create_textfield_nv(UiObject *obj, int width, UiBool frameless, UiBool password, char *varname) { |
79 UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs args) { |
420 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_STRING); |
80 return create_textfield(obj, args, FALSE, FALSE); |
421 if(var) { |
81 } |
422 UiString *value = var->value; |
82 |
423 return ui_textfield(obj, value); |
83 UIWIDGET ui_frameless_textfield_create(UiObject* obj, UiTextFieldArgs args) { |
424 } else { |
84 return create_textfield(obj, args, TRUE, FALSE); |
425 // TODO: error |
85 } |
426 } |
86 |
427 return NULL; |
87 UIWIDGET ui_passwordfield_create(UiObject* obj, UiTextFieldArgs args) { |
428 } |
88 return create_textfield(obj, args, FALSE, FALSE); |
429 |
89 } |
430 UIWIDGET ui_textfield(UiObject *obj, UiString *value) { |
|
431 return create_textfield(obj, 0, FALSE, FALSE, value); |
|
432 } |
|
433 |
|
434 UIWIDGET ui_textfield_nv(UiObject *obj, char *varname) { |
|
435 return create_textfield_nv(obj, 0, FALSE, FALSE, varname); |
|
436 } |
|
437 |
|
438 UIWIDGET ui_textfield_w(UiObject *obj, int width, UiString *value) { |
|
439 return create_textfield(obj, width, FALSE, FALSE, value); |
|
440 } |
|
441 |
|
442 UIWIDGET ui_textfield_wnv(UiObject *obj, int width, char *varname) { |
|
443 return create_textfield_nv(obj, width, FALSE, FALSE, varname); |
|
444 } |
|
445 |
|
446 UIWIDGET ui_frameless_textfield(UiObject *obj, UiString *value) { |
|
447 return create_textfield(obj, 0, TRUE, FALSE, value); |
|
448 } |
|
449 |
|
450 UIWIDGET ui_frameless_textfield_nv(UiObject *obj, char *varname) { |
|
451 return create_textfield_nv(obj, 0, TRUE, FALSE, varname); |
|
452 } |
|
453 |
|
454 UIWIDGET ui_passwordfield(UiObject *obj, UiString *value) { |
|
455 return create_textfield(obj, 0, FALSE, TRUE, value); |
|
456 } |
|
457 |
|
458 UIWIDGET ui_passwordfield_nv(UiObject *obj, char *varname) { |
|
459 return create_textfield_nv(obj, 0, FALSE, TRUE, varname); |
|
460 } |
|
461 |
|
462 UIWIDGET ui_passwordfield_w(UiObject *obj, int width, UiString *value) { |
|
463 return create_textfield(obj, width, FALSE, TRUE, value); |
|
464 } |
|
465 |
|
466 UIWIDGET ui_passwordfield_wnv(UiObject *obj, int width, char *varname) { |
|
467 return create_textfield_nv(obj, width, FALSE, TRUE, varname); |
|
468 } |
|
469 |
|
470 |
90 |
471 char* ui_textfield_get(UiString *str) { |
91 char* ui_textfield_get(UiString *str) { |
472 if(str->value.ptr) { |
92 str->value.free(str->value.ptr); |
473 str->value.free(str->value.ptr); |
93 char *value = XmTextFieldGetString(str->obj); |
474 } |
|
475 char *value = XmTextGetString(str->obj); |
|
476 str->value.ptr = value; |
94 str->value.ptr = value; |
477 str->value.free = (ui_freefunc)XtFree; |
95 str->value.free = (ui_freefunc)XtFree; |
478 return value; |
96 return value; |
479 } |
97 } |
480 |
98 |
481 void ui_textfield_set(UiString *str, char *value) { |
99 void ui_textfield_set(UiString *str, const char *value) { |
482 XmTextSetString(str->obj, value); |
100 XmTextFieldSetString(str->obj, (void*)value); |
483 if(str->value.ptr) { |
|
484 str->value.free(str->value.ptr); |
|
485 } |
|
486 str->value.ptr = NULL; |
101 str->value.ptr = NULL; |
487 } |
102 str->value.free(str->value.ptr); |
488 |
103 } |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 /* -------------------- path bar -------------------- */ |
|
110 |
|
111 #define XNECreateText(parent,name,args,count) XmCreateTextField(parent,name,args,count) |
|
112 #define XNETextSetString(widget,value) XmTextFieldSetString(widget,value) |
|
113 #define XNETextGetString(widget) XmTextFieldGetString(widget) |
|
114 #define XNETextGetLastPosition(widget) XmTextFieldGetLastPosition(widget) |
|
115 #define XNETextSetInsertionPosition(widget, i) XmTextFieldSetInsertionPosition(widget, i) |
|
116 #define XNETextSetSelection(w, f, l, t) XmTextFieldSetSelection(w, f, l, t) |
|
117 |
|
118 typedef void(*updatedir_callback)(void*,char*,int); |
|
119 |
|
120 typedef struct PathBar { |
|
121 Widget widget; |
|
122 Widget textfield; |
|
123 |
|
124 Widget focus_widget; |
|
125 |
|
126 Widget left; |
|
127 Widget right; |
|
128 Dimension lw; |
|
129 Dimension rw; |
|
130 |
|
131 int shift; |
|
132 |
|
133 UiPathElm *current_pathelms; |
|
134 Widget *pathSegments; |
|
135 size_t numSegments; |
|
136 size_t segmentAlloc; |
|
137 |
|
138 char *path; |
|
139 int selection; |
|
140 Boolean input; |
|
141 |
|
142 int focus; |
|
143 |
|
144 updatedir_callback updateDir; |
|
145 void *updateDirData; |
|
146 |
|
147 ui_pathelm_func getpathelm; |
|
148 void *getpathelmdata; |
|
149 } PathBar; |
|
150 |
|
151 void PathBarSetPath(PathBar *bar, const char *path); |
|
152 |
|
153 void pathbar_resize(Widget w, PathBar *p, XtPointer d) |
|
154 { |
|
155 Dimension width, height; |
|
156 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, NULL); |
|
157 |
|
158 Dimension *segW = (void*)XtCalloc(p->numSegments, sizeof(Dimension)); |
|
159 |
|
160 Dimension maxHeight = 0; |
|
161 |
|
162 /* get width/height from all widgets */ |
|
163 Dimension pathWidth = 0; |
|
164 for(int i=0;i<p->numSegments;i++) { |
|
165 Dimension segWidth; |
|
166 Dimension segHeight; |
|
167 XtVaGetValues(p->pathSegments[i], XmNwidth, &segWidth, XmNheight, &segHeight, NULL); |
|
168 segW[i] = segWidth; |
|
169 pathWidth += segWidth; |
|
170 if(segHeight > maxHeight) { |
|
171 maxHeight = segHeight; |
|
172 } |
|
173 } |
|
174 Dimension tfHeight; |
|
175 XtVaGetValues(p->textfield, XmNheight, &tfHeight, NULL); |
|
176 if(tfHeight > maxHeight) { |
|
177 maxHeight = tfHeight; |
|
178 } |
|
179 |
|
180 Boolean arrows = False; |
|
181 if(pathWidth + 10 > width) { |
|
182 arrows = True; |
|
183 pathWidth += p->lw + p->rw; |
|
184 } |
|
185 |
|
186 /* calc max visible widgets */ |
|
187 int start = 0; |
|
188 if(arrows) { |
|
189 Dimension vis = p->lw+p->rw; |
|
190 for(int i=p->numSegments;i>0;i--) { |
|
191 Dimension segWidth = segW[i-1]; |
|
192 if(vis + segWidth + 10 > width) { |
|
193 start = i; |
|
194 arrows = True; |
|
195 break; |
|
196 } |
|
197 vis += segWidth; |
|
198 } |
|
199 } else { |
|
200 p->shift = 0; |
|
201 } |
|
202 |
|
203 int leftShift = 0; |
|
204 if(p->shift < 0) { |
|
205 if(start + p->shift < 0) { |
|
206 leftShift = start; |
|
207 start = 0; |
|
208 p->shift = -leftShift; |
|
209 } else { |
|
210 leftShift = -p->shift; /* negative shift */ |
|
211 start += p->shift; |
|
212 } |
|
213 } |
|
214 |
|
215 int x = 0; |
|
216 if(arrows) { |
|
217 XtManageChild(p->left); |
|
218 XtManageChild(p->right); |
|
219 x = p->lw; |
|
220 } else { |
|
221 XtUnmanageChild(p->left); |
|
222 XtUnmanageChild(p->right); |
|
223 } |
|
224 |
|
225 for(int i=0;i<p->numSegments;i++) { |
|
226 if(i >= start && i < p->numSegments - leftShift && !p->input) { |
|
227 XtVaSetValues(p->pathSegments[i], XmNx, x, XmNy, 0, XmNheight, maxHeight, NULL); |
|
228 x += segW[i]; |
|
229 XtManageChild(p->pathSegments[i]); |
|
230 } else { |
|
231 XtUnmanageChild(p->pathSegments[i]); |
|
232 } |
|
233 } |
|
234 |
|
235 if(arrows) { |
|
236 XtVaSetValues(p->left, XmNx, 0, XmNy, 0, XmNheight, maxHeight, NULL); |
|
237 XtVaSetValues(p->right, XmNx, x, XmNy, 0, XmNheight, maxHeight, NULL); |
|
238 } |
|
239 |
|
240 free(segW); |
|
241 |
|
242 Dimension rw, rh; |
|
243 XtMakeResizeRequest(w, width, maxHeight, &rw, &rh); |
|
244 |
|
245 XtVaSetValues(p->textfield, XmNwidth, rw, XmNheight, rh, NULL); |
|
246 } |
|
247 |
|
248 static void pathbarActivateTF(PathBar *p) |
|
249 { |
|
250 XtUnmanageChild(p->left); |
|
251 XtUnmanageChild(p->right); |
|
252 XNETextSetSelection(p->textfield, 0, XNETextGetLastPosition(p->textfield), 0); |
|
253 XtManageChild(p->textfield); |
|
254 p->input = 1; |
|
255 |
|
256 XmProcessTraversal(p->textfield, XmTRAVERSE_CURRENT); |
|
257 |
|
258 pathbar_resize(p->widget, p, NULL); |
|
259 } |
|
260 |
|
261 void PathBarActivateTextfield(PathBar *p) |
|
262 { |
|
263 p->focus = 1; |
|
264 pathbarActivateTF(p); |
|
265 } |
|
266 |
|
267 void pathbar_input(Widget w, PathBar *p, XtPointer c) |
|
268 { |
|
269 XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct*)c; |
|
270 XEvent *xevent = cbs->event; |
|
271 |
|
272 if (cbs->reason == XmCR_INPUT) { |
|
273 if (xevent->xany.type == ButtonPress) { |
|
274 p->focus = 0; |
|
275 pathbarActivateTF(p); |
|
276 } |
|
277 } |
|
278 } |
|
279 |
|
280 void pathbar_losingfocus(Widget w, PathBar *p, XtPointer c) |
|
281 { |
|
282 if(--p->focus < 0) { |
|
283 p->input = False; |
|
284 XtUnmanageChild(p->textfield); |
|
285 } |
|
286 } |
|
287 |
|
288 static cxmutstr concat_path_s(cxstring base, cxstring path) { |
|
289 if(!path.ptr) { |
|
290 path = CX_STR(""); |
|
291 } |
|
292 |
|
293 int add_separator = 0; |
|
294 if(base.length != 0 && base.ptr[base.length-1] == '/') { |
|
295 if(path.ptr[0] == '/') { |
|
296 base.length--; |
|
297 } |
|
298 } else { |
|
299 if(path.length == 0 || path.ptr[0] != '/') { |
|
300 add_separator = 1; |
|
301 } |
|
302 } |
|
303 |
|
304 cxmutstr url; |
|
305 if(add_separator) { |
|
306 url = cx_strcat(3, base, CX_STR("/"), path); |
|
307 } else { |
|
308 url = cx_strcat(2, base, path); |
|
309 } |
|
310 |
|
311 return url; |
|
312 } |
|
313 |
|
314 static char* ConcatPath(const char *path1, const char *path2) { |
|
315 return concat_path_s(cx_str(path1), cx_str(path2)).ptr; |
|
316 } |
|
317 |
|
318 void pathbar_pathinput(Widget w, PathBar *p, XtPointer d) |
|
319 { |
|
320 char *newpath = XNETextGetString(p->textfield); |
|
321 if(newpath) { |
|
322 if(newpath[0] == '~') { |
|
323 char *p = newpath+1; |
|
324 char *home = getenv("HOME"); |
|
325 char *cp = ConcatPath(home, p); |
|
326 XtFree(newpath); |
|
327 newpath = cp; |
|
328 } else if(newpath[0] != '/') { |
|
329 char curdir[2048]; |
|
330 curdir[0] = 0; |
|
331 getcwd(curdir, 2048); |
|
332 char *cp = ConcatPath(curdir, newpath); |
|
333 XtFree(newpath); |
|
334 newpath = cp; |
|
335 } |
|
336 |
|
337 /* update path */ |
|
338 PathBarSetPath(p, newpath); |
|
339 if(p->updateDir) { |
|
340 p->updateDir(p->updateDirData, newpath, -1); |
|
341 } |
|
342 XtFree(newpath); |
|
343 |
|
344 /* hide textfield and show path as buttons */ |
|
345 XtUnmanageChild(p->textfield); |
|
346 pathbar_resize(p->widget, p, NULL); |
|
347 |
|
348 if(p->focus_widget) { |
|
349 XmProcessTraversal(p->focus_widget, XmTRAVERSE_CURRENT); |
|
350 } |
|
351 } |
|
352 } |
|
353 |
|
354 void pathbar_shift_left(Widget w, PathBar *p, XtPointer d) |
|
355 { |
|
356 p->shift--; |
|
357 pathbar_resize(p->widget, p, NULL); |
|
358 } |
|
359 |
|
360 void pathbar_shift_right(Widget w, PathBar *p, XtPointer d) |
|
361 { |
|
362 if(p->shift < 0) { |
|
363 p->shift++; |
|
364 } |
|
365 pathbar_resize(p->widget, p, NULL); |
|
366 } |
|
367 |
|
368 static void pathTextEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) { |
|
369 PathBar *pb = data; |
|
370 if(event->type == KeyReleaseMask) { |
|
371 if(event->xkey.keycode == 9) { |
|
372 XtUnmanageChild(pb->textfield); |
|
373 pathbar_resize(pb->widget, pb, NULL); |
|
374 *dispatch = False; |
|
375 } else if(event->xkey.keycode == 36) { |
|
376 pathbar_pathinput(pb->textfield, pb, NULL); |
|
377 *dispatch = False; |
|
378 } |
|
379 } |
|
380 } |
|
381 |
|
382 PathBar* CreatePathBar(Widget parent, ArgList args, int n) |
|
383 { |
|
384 PathBar *bar = (PathBar*)XtMalloc(sizeof(PathBar)); |
|
385 bar->path = NULL; |
|
386 bar->updateDir = NULL; |
|
387 bar->updateDirData = NULL; |
|
388 |
|
389 bar->focus_widget = NULL; |
|
390 |
|
391 bar->getpathelm = NULL; |
|
392 bar->getpathelmdata = NULL; |
|
393 bar->current_pathelms = NULL; |
|
394 |
|
395 bar->shift = 0; |
|
396 |
|
397 XtSetArg(args[n], XmNmarginWidth, 0); n++; |
|
398 XtSetArg(args[n], XmNmarginHeight, 0); n++; |
|
399 bar->widget = XmCreateDrawingArea(parent, "pathbar", args, n); |
|
400 XtAddCallback( |
|
401 bar->widget, |
|
402 XmNresizeCallback, |
|
403 (XtCallbackProc)pathbar_resize, |
|
404 bar); |
|
405 XtAddCallback( |
|
406 bar->widget, |
|
407 XmNinputCallback, |
|
408 (XtCallbackProc)pathbar_input, |
|
409 bar); |
|
410 |
|
411 Arg a[4]; |
|
412 XtSetArg(a[0], XmNshadowThickness, 0); |
|
413 XtSetArg(a[1], XmNx, 0); |
|
414 XtSetArg(a[2], XmNy, 0); |
|
415 bar->textfield = XNECreateText(bar->widget, "pbtext", a, 3); |
|
416 bar->input = 0; |
|
417 XtAddCallback( |
|
418 bar->textfield, |
|
419 XmNlosingFocusCallback, |
|
420 (XtCallbackProc)pathbar_losingfocus, |
|
421 bar); |
|
422 XtAddCallback(bar->textfield, XmNactivateCallback, |
|
423 (XtCallbackProc)pathbar_pathinput, bar); |
|
424 XtAddEventHandler(bar->textfield, KeyPressMask | KeyReleaseMask, FALSE, pathTextEH, bar); |
|
425 |
|
426 XtSetArg(a[0], XmNarrowDirection, XmARROW_LEFT); |
|
427 bar->left = XmCreateArrowButton(bar->widget, "pbbutton", a, 1); |
|
428 XtSetArg(a[0], XmNarrowDirection, XmARROW_RIGHT); |
|
429 bar->right = XmCreateArrowButton(bar->widget, "pbbutton", a, 1); |
|
430 XtAddCallback( |
|
431 bar->left, |
|
432 XmNactivateCallback, |
|
433 (XtCallbackProc)pathbar_shift_left, |
|
434 bar); |
|
435 XtAddCallback( |
|
436 bar->right, |
|
437 XmNactivateCallback, |
|
438 (XtCallbackProc)pathbar_shift_right, |
|
439 bar); |
|
440 |
|
441 Pixel bg; |
|
442 XtVaGetValues(bar->textfield, XmNbackground, &bg, NULL); |
|
443 XtVaSetValues(bar->widget, XmNbackground, bg, NULL); |
|
444 |
|
445 XtManageChild(bar->left); |
|
446 XtManageChild(bar->right); |
|
447 |
|
448 XtVaGetValues(bar->left, XmNwidth, &bar->lw, NULL); |
|
449 XtVaGetValues(bar->right, XmNwidth, &bar->rw, NULL); |
|
450 |
|
451 bar->segmentAlloc = 16; |
|
452 bar->numSegments = 0; |
|
453 bar->pathSegments = (Widget*)XtCalloc(16, sizeof(Widget)); |
|
454 |
|
455 bar->selection = 0; |
|
456 |
|
457 return bar; |
|
458 } |
|
459 |
|
460 void PathBarChangeDir(Widget w, PathBar *bar, XtPointer c) |
|
461 { |
|
462 XmToggleButtonSetState(bar->pathSegments[bar->selection], False, False); |
|
463 |
|
464 int i; |
|
465 for(i=0;i<bar->numSegments;i++) { |
|
466 if(bar->pathSegments[i] == w) { |
|
467 bar->selection = i; |
|
468 XmToggleButtonSetState(w, True, False); |
|
469 break; |
|
470 } |
|
471 } |
|
472 |
|
473 UiPathElm elm = bar->current_pathelms[i]; |
|
474 cxmutstr name = cx_strdup(cx_strn(elm.name, elm.name_len)); |
|
475 if(bar->updateDir) { |
|
476 bar->updateDir(bar->updateDirData, name.ptr, i); |
|
477 } |
|
478 free(name.ptr); |
|
479 } |
|
480 |
|
481 static void ui_pathelm_destroy(UiPathElm *elms, size_t nelm) { |
|
482 for(int i=0;i<nelm;i++) { |
|
483 free(elms[i].name); |
|
484 free(elms[i].path); |
|
485 } |
|
486 free(elms); |
|
487 } |
|
488 |
|
489 void PathBarSetPath(PathBar *bar, const char *path) |
|
490 { |
|
491 if(bar->path) { |
|
492 free(bar->path); |
|
493 } |
|
494 bar->path = strdup(path); |
|
495 |
|
496 for(int i=0;i<bar->numSegments;i++) { |
|
497 XtDestroyWidget(bar->pathSegments[i]); |
|
498 } |
|
499 XtUnmanageChild(bar->textfield); |
|
500 XtManageChild(bar->left); |
|
501 XtManageChild(bar->right); |
|
502 bar->input = False; |
|
503 |
|
504 Arg args[4]; |
|
505 XmString str; |
|
506 |
|
507 bar->numSegments = 0; |
|
508 |
|
509 ui_pathelm_destroy(bar->current_pathelms, bar->numSegments); |
|
510 size_t nelm = 0; |
|
511 UiPathElm* path_elm = bar->getpathelm(bar->path, strlen(bar->path), &nelm, bar->getpathelmdata); |
|
512 if (!path_elm) { |
|
513 return; |
|
514 } |
|
515 bar->current_pathelms = path_elm; |
|
516 bar->numSegments = nelm; |
|
517 bar->pathSegments = realloc(bar->pathSegments, nelm * sizeof(Widget*)); |
|
518 |
|
519 for(int i=0;i<nelm;i++) { |
|
520 UiPathElm elm = path_elm[i]; |
|
521 |
|
522 cxmutstr name = cx_strdup(cx_strn(elm.name, elm.name_len)); |
|
523 str = XmStringCreateLocalized(elm.name); |
|
524 free(name.ptr); |
|
525 |
|
526 XtSetArg(args[0], XmNlabelString, str); |
|
527 XtSetArg(args[1], XmNfillOnSelect, True); |
|
528 XtSetArg(args[2], XmNindicatorOn, False); |
|
529 Widget button = XmCreateToggleButton(bar->widget, "pbbutton", args, 3); |
|
530 XtAddCallback( |
|
531 button, |
|
532 XmNvalueChangedCallback, |
|
533 (XtCallbackProc)PathBarChangeDir, |
|
534 bar); |
|
535 XmStringFree(str); |
|
536 |
|
537 bar->pathSegments[i] = button; |
|
538 } |
|
539 |
|
540 bar->selection = bar->numSegments-1; |
|
541 XmToggleButtonSetState(bar->pathSegments[bar->selection], True, False); |
|
542 |
|
543 XNETextSetString(bar->textfield, (char*)path); |
|
544 XNETextSetInsertionPosition(bar->textfield, XNETextGetLastPosition(bar->textfield)); |
|
545 |
|
546 pathbar_resize(bar->widget, bar, NULL); |
|
547 } |
|
548 |
|
549 void PathBarDestroy(PathBar *pathbar) { |
|
550 if(pathbar->path) { |
|
551 XtFree(pathbar->path); |
|
552 } |
|
553 XtFree((void*)pathbar->pathSegments); |
|
554 XtFree((void*)pathbar); |
|
555 } |
|
556 |
|
557 |
|
558 /* ---------------------------- Path Text Field ---------------------------- */ |
|
559 |
|
560 static void destroy_pathbar(Widget w, XtPointer *data, XtPointer d) { |
|
561 PathBar *pathbar = (PathBar*)data; |
|
562 // TODO: check if there is somonething missing |
|
563 XtFree((void*)pathbar->pathSegments); |
|
564 XtFree((void*)pathbar); |
|
565 } |
|
566 |
|
567 // TODO: move to common |
|
568 static UiPathElm* default_pathelm_func(const char* full_path, size_t len, size_t* ret_nelm, void* data) { |
|
569 cxstring *pathelms; |
|
570 size_t nelm = cx_strsplit_a(cxDefaultAllocator, cx_strn(full_path, len), CX_STR("/"), 4096, &pathelms); |
|
571 |
|
572 if (nelm == 0) { |
|
573 *ret_nelm = 0; |
|
574 return NULL; |
|
575 } |
|
576 |
|
577 UiPathElm* elms = (UiPathElm*)calloc(nelm, sizeof(UiPathElm)); |
|
578 size_t n = nelm; |
|
579 int j = 0; |
|
580 for (int i = 0; i < nelm; i++) { |
|
581 cxstring c = pathelms[i]; |
|
582 if (c.length == 0) { |
|
583 if (i == 0) { |
|
584 c.length = 1; |
|
585 } |
|
586 else { |
|
587 n--; |
|
588 continue; |
|
589 } |
|
590 } |
|
591 |
|
592 cxmutstr m = cx_strdup(c); |
|
593 elms[j].name = m.ptr; |
|
594 elms[j].name_len = m.length; |
|
595 |
|
596 size_t elm_path_len = c.ptr + c.length - full_path; |
|
597 cxmutstr elm_path = cx_strdup(cx_strn(full_path, elm_path_len)); |
|
598 elms[j].path = elm_path.ptr; |
|
599 elms[j].path_len = elm_path.length; |
|
600 |
|
601 j++; |
|
602 } |
|
603 *ret_nelm = n; |
|
604 |
|
605 return elms; |
|
606 } |
|
607 |
|
608 static void pathbar_activate(void *data, char *path, int index) { |
|
609 UiEventData *event = data; |
|
610 UiEvent evt; |
|
611 evt.obj = event->obj; |
|
612 evt.window = evt.obj->window; |
|
613 evt.document = evt.obj->ctx->document; |
|
614 evt.eventdata = path; |
|
615 evt.intval = index; |
|
616 event->callback(&evt, event->userdata); |
|
617 } |
|
618 |
|
619 UIWIDGET ui_path_textfield_create(UiObject* obj, UiPathTextFieldArgs args) { |
|
620 Arg xargs[16]; |
|
621 int n = 0; |
|
622 |
|
623 UiContainerPrivate *ctn = ui_obj_container(obj); |
|
624 UI_APPLY_LAYOUT(ctn->layout, args); |
|
625 |
|
626 Widget parent = ctn->prepare(ctn, xargs, &n); |
|
627 // TODO: name |
|
628 |
|
629 |
|
630 PathBar *pathbar = CreatePathBar(parent, xargs, n); |
|
631 if(!args.getpathelm) { |
|
632 pathbar->getpathelm= default_pathelm_func; |
|
633 } else { |
|
634 pathbar->getpathelm = args.getpathelm; |
|
635 pathbar->getpathelmdata = args.getpathelmdata; |
|
636 } |
|
637 |
|
638 |
|
639 XtManageChild(pathbar->widget); |
|
640 ctn->add(ctn, pathbar->widget); |
|
641 |
|
642 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING); |
|
643 if (var) { |
|
644 UiString* value = (UiString*)var->value; |
|
645 value->obj = pathbar; |
|
646 value->get = ui_path_textfield_get; |
|
647 value->set = ui_path_textfield_set; |
|
648 |
|
649 if(value->value.ptr) { |
|
650 char *str = strdup(value->value.ptr); |
|
651 ui_string_set(value, str); |
|
652 free(str); |
|
653 } |
|
654 } |
|
655 |
|
656 if(args.onactivate) { |
|
657 UiEventData *eventdata = malloc(sizeof(UiEventData)); |
|
658 eventdata->callback = args.onactivate; |
|
659 eventdata->userdata = args.onactivatedata; |
|
660 eventdata->obj = obj; |
|
661 eventdata->value = 0; |
|
662 |
|
663 pathbar->updateDir = pathbar_activate; |
|
664 pathbar->updateDirData = eventdata; |
|
665 |
|
666 XtAddCallback( |
|
667 pathbar->widget, |
|
668 XmNdestroyCallback, |
|
669 (XtCallbackProc)ui_destroy_eventdata, |
|
670 eventdata); |
|
671 } |
|
672 |
|
673 XtAddCallback( |
|
674 pathbar->widget, |
|
675 XmNdestroyCallback, |
|
676 (XtCallbackProc)destroy_pathbar, |
|
677 pathbar); |
|
678 |
|
679 return pathbar->widget; |
|
680 } |
|
681 |
|
682 char* ui_path_textfield_get(UiString *str) { |
|
683 PathBar *pathbar = str->obj; |
|
684 str->value.free(str->value.ptr); |
|
685 char *value = XmTextFieldGetString(pathbar->textfield); |
|
686 str->value.ptr = value; |
|
687 str->value.free = (ui_freefunc)XtFree; |
|
688 return value; |
|
689 } |
|
690 |
|
691 void ui_path_textfield_set(UiString *str, const char *value) { |
|
692 PathBarSetPath(str->obj, value); |
|
693 } |