ui/motif/text.c

changeset 101
7b3a3130be44
parent 100
d2bd73d28ff1
equal deleted inserted replaced
100:d2bd73d28ff1 101:7b3a3130be44
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
36 #include <cx/string.h>
37
38
39
40 /* ------------------------------ Text Field ------------------------------ */
41
42 static UIWIDGET create_textfield(UiObject *obj, UiTextFieldArgs args, int frameless, int password) {
43 Arg xargs[16];
44 int n = 0;
45
46 if(frameless) {
47 XtSetArg(xargs[n], XmNshadowThickness, 0);
48 n++;
49 }
50 if(password) {
51 // TODO
52 }
53
54 UiContainerPrivate *ctn = ui_obj_container(obj);
55 UI_APPLY_LAYOUT(ctn->layout, args);
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);
60 XtManageChild(textfield);
61
62 ui_set_widget_groups(obj->ctx, textfield, args.groups);
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
71 if(value->value.ptr) {
72 ui_textfield_set(value, value->value.ptr);
73 }
74 }
75
76 return textfield;
77 }
78
79 UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs args) {
80 return create_textfield(obj, args, FALSE, FALSE);
81 }
82
83 UIWIDGET ui_frameless_textfield_create(UiObject* obj, UiTextFieldArgs args) {
84 return create_textfield(obj, args, TRUE, FALSE);
85 }
86
87 UIWIDGET ui_passwordfield_create(UiObject* obj, UiTextFieldArgs args) {
88 return create_textfield(obj, args, FALSE, FALSE);
89 }
90
91 char* ui_textfield_get(UiString *str) {
92 str->value.free(str->value.ptr);
93 char *value = XmTextFieldGetString(str->obj);
94 str->value.ptr = value;
95 str->value.free = (ui_freefunc)XtFree;
96 return value;
97 }
98
99 void ui_textfield_set(UiString *str, const char *value) {
100 XmTextFieldSetString(str->obj, (void*)value);
101 str->value.ptr = NULL;
102 str->value.free(str->value.ptr);
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 }

mercurial