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*); |
|
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 Widget *pathSegments; |
|
134 size_t numSegments; |
|
135 size_t segmentAlloc; |
|
136 |
|
137 char *path; |
|
138 int selection; |
|
139 Boolean input; |
|
140 |
|
141 int focus; |
|
142 |
|
143 updatedir_callback updateDir; |
|
144 void *updateDirData; |
|
145 } PathBar; |
|
146 |
|
147 void PathBarSetPath(PathBar *bar, const char *path); |
|
148 |
|
149 void pathbar_resize(Widget w, PathBar *p, XtPointer d) |
|
150 { |
|
151 Dimension width, height; |
|
152 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, NULL); |
|
153 |
|
154 Dimension *segW = (void*)XtCalloc(p->numSegments, sizeof(Dimension)); |
|
155 |
|
156 Dimension maxHeight = 0; |
|
157 |
|
158 /* get width/height from all widgets */ |
|
159 Dimension pathWidth = 0; |
|
160 for(int i=0;i<p->numSegments;i++) { |
|
161 Dimension segWidth; |
|
162 Dimension segHeight; |
|
163 XtVaGetValues(p->pathSegments[i], XmNwidth, &segWidth, XmNheight, &segHeight, NULL); |
|
164 segW[i] = segWidth; |
|
165 pathWidth += segWidth; |
|
166 if(segHeight > maxHeight) { |
|
167 maxHeight = segHeight; |
|
168 } |
|
169 } |
|
170 Dimension tfHeight; |
|
171 XtVaGetValues(p->textfield, XmNheight, &tfHeight, NULL); |
|
172 if(tfHeight > maxHeight) { |
|
173 maxHeight = tfHeight; |
|
174 } |
|
175 |
|
176 Boolean arrows = False; |
|
177 if(pathWidth + 10 > width) { |
|
178 arrows = True; |
|
179 pathWidth += p->lw + p->rw; |
|
180 } |
|
181 |
|
182 /* calc max visible widgets */ |
|
183 int start = 0; |
|
184 if(arrows) { |
|
185 Dimension vis = p->lw+p->rw; |
|
186 for(int i=p->numSegments;i>0;i--) { |
|
187 Dimension segWidth = segW[i-1]; |
|
188 if(vis + segWidth + 10 > width) { |
|
189 start = i; |
|
190 arrows = True; |
|
191 break; |
|
192 } |
|
193 vis += segWidth; |
|
194 } |
|
195 } else { |
|
196 p->shift = 0; |
|
197 } |
|
198 |
|
199 int leftShift = 0; |
|
200 if(p->shift < 0) { |
|
201 if(start + p->shift < 0) { |
|
202 leftShift = start; |
|
203 start = 0; |
|
204 p->shift = -leftShift; |
|
205 } else { |
|
206 leftShift = -p->shift; /* negative shift */ |
|
207 start += p->shift; |
|
208 } |
|
209 } |
|
210 |
|
211 int x = 0; |
|
212 if(arrows) { |
|
213 XtManageChild(p->left); |
|
214 XtManageChild(p->right); |
|
215 x = p->lw; |
|
216 } else { |
|
217 XtUnmanageChild(p->left); |
|
218 XtUnmanageChild(p->right); |
|
219 } |
|
220 |
|
221 for(int i=0;i<p->numSegments;i++) { |
|
222 if(i >= start && i < p->numSegments - leftShift && !p->input) { |
|
223 XtVaSetValues(p->pathSegments[i], XmNx, x, XmNy, 0, XmNheight, maxHeight, NULL); |
|
224 x += segW[i]; |
|
225 XtManageChild(p->pathSegments[i]); |
|
226 } else { |
|
227 XtUnmanageChild(p->pathSegments[i]); |
|
228 } |
|
229 } |
|
230 |
|
231 if(arrows) { |
|
232 XtVaSetValues(p->left, XmNx, 0, XmNy, 0, XmNheight, maxHeight, NULL); |
|
233 XtVaSetValues(p->right, XmNx, x, XmNy, 0, XmNheight, maxHeight, NULL); |
|
234 } |
|
235 |
|
236 free(segW); |
|
237 |
|
238 Dimension rw, rh; |
|
239 XtMakeResizeRequest(w, width, maxHeight, &rw, &rh); |
|
240 |
|
241 XtVaSetValues(p->textfield, XmNwidth, rw, XmNheight, rh, NULL); |
|
242 } |
|
243 |
|
244 static void pathbarActivateTF(PathBar *p) |
|
245 { |
|
246 XtUnmanageChild(p->left); |
|
247 XtUnmanageChild(p->right); |
|
248 XNETextSetSelection(p->textfield, 0, XNETextGetLastPosition(p->textfield), 0); |
|
249 XtManageChild(p->textfield); |
|
250 p->input = 1; |
|
251 |
|
252 XmProcessTraversal(p->textfield, XmTRAVERSE_CURRENT); |
|
253 |
|
254 pathbar_resize(p->widget, p, NULL); |
|
255 } |
|
256 |
|
257 void PathBarActivateTextfield(PathBar *p) |
|
258 { |
|
259 p->focus = 1; |
|
260 pathbarActivateTF(p); |
|
261 } |
|
262 |
|
263 void pathbar_input(Widget w, PathBar *p, XtPointer c) |
|
264 { |
|
265 XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct*)c; |
|
266 XEvent *xevent = cbs->event; |
|
267 |
|
268 if (cbs->reason == XmCR_INPUT) { |
|
269 if (xevent->xany.type == ButtonPress) { |
|
270 p->focus = 0; |
|
271 pathbarActivateTF(p); |
|
272 } |
|
273 } |
|
274 } |
|
275 |
|
276 void pathbar_losingfocus(Widget w, PathBar *p, XtPointer c) |
|
277 { |
|
278 if(--p->focus < 0) { |
|
279 p->input = False; |
|
280 XtUnmanageChild(p->textfield); |
|
281 } |
|
282 } |
|
283 |
|
284 static cxmutstr concat_path_s(cxstring base, cxstring path) { |
|
285 if(!path.ptr) { |
|
286 path = CX_STR(""); |
|
287 } |
|
288 |
|
289 int add_separator = 0; |
|
290 if(base.length != 0 && base.ptr[base.length-1] == '/') { |
|
291 if(path.ptr[0] == '/') { |
|
292 base.length--; |
|
293 } |
|
294 } else { |
|
295 if(path.length == 0 || path.ptr[0] != '/') { |
|
296 add_separator = 1; |
|
297 } |
|
298 } |
|
299 |
|
300 cxmutstr url; |
|
301 if(add_separator) { |
|
302 url = cx_strcat(3, base, CX_STR("/"), path); |
|
303 } else { |
|
304 url = cx_strcat(2, base, path); |
|
305 } |
|
306 |
|
307 return url; |
|
308 } |
|
309 |
|
310 static char* ConcatPath(const char *path1, const char *path2) { |
|
311 return concat_path_s(cx_str(path1), cx_str(path2)).ptr; |
|
312 } |
|
313 |
|
314 void pathbar_pathinput(Widget w, PathBar *p, XtPointer d) |
|
315 { |
|
316 char *newpath = XNETextGetString(p->textfield); |
|
317 if(newpath) { |
|
318 if(newpath[0] == '~') { |
|
319 char *p = newpath+1; |
|
320 char *home = getenv("HOME"); |
|
321 char *cp = ConcatPath(home, p); |
|
322 XtFree(newpath); |
|
323 newpath = cp; |
|
324 } else if(newpath[0] != '/') { |
|
325 char curdir[2048]; |
|
326 curdir[0] = 0; |
|
327 getcwd(curdir, 2048); |
|
328 char *cp = ConcatPath(curdir, newpath); |
|
329 XtFree(newpath); |
|
330 newpath = cp; |
|
331 } |
|
332 |
|
333 /* update path */ |
|
334 PathBarSetPath(p, newpath); |
|
335 if(p->updateDir) { |
|
336 p->updateDir(p->updateDirData, newpath); |
|
337 } |
|
338 XtFree(newpath); |
|
339 |
|
340 /* hide textfield and show path as buttons */ |
|
341 XtUnmanageChild(p->textfield); |
|
342 pathbar_resize(p->widget, p, NULL); |
|
343 |
|
344 if(p->focus_widget) { |
|
345 XmProcessTraversal(p->focus_widget, XmTRAVERSE_CURRENT); |
|
346 } |
|
347 } |
|
348 } |
|
349 |
|
350 void pathbar_shift_left(Widget w, PathBar *p, XtPointer d) |
|
351 { |
|
352 p->shift--; |
|
353 pathbar_resize(p->widget, p, NULL); |
|
354 } |
|
355 |
|
356 void pathbar_shift_right(Widget w, PathBar *p, XtPointer d) |
|
357 { |
|
358 if(p->shift < 0) { |
|
359 p->shift++; |
|
360 } |
|
361 pathbar_resize(p->widget, p, NULL); |
|
362 } |
|
363 |
|
364 static void pathTextEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) { |
|
365 PathBar *pb = data; |
|
366 if(event->type == KeyReleaseMask) { |
|
367 if(event->xkey.keycode == 9) { |
|
368 XtUnmanageChild(pb->textfield); |
|
369 pathbar_resize(pb->widget, pb, NULL); |
|
370 *dispatch = False; |
|
371 } else if(event->xkey.keycode == 36) { |
|
372 pathbar_pathinput(pb->textfield, pb, NULL); |
|
373 *dispatch = False; |
|
374 } |
|
375 } |
|
376 } |
|
377 |
|
378 PathBar* CreatePathBar(Widget parent, ArgList args, int n) |
|
379 { |
|
380 PathBar *bar = (PathBar*)XtMalloc(sizeof(PathBar)); |
|
381 bar->path = NULL; |
|
382 bar->updateDir = NULL; |
|
383 bar->updateDirData = NULL; |
|
384 |
|
385 bar->focus_widget = NULL; |
|
386 |
|
387 bar->shift = 0; |
|
388 |
|
389 XtSetArg(args[n], XmNmarginWidth, 0); n++; |
|
390 XtSetArg(args[n], XmNmarginHeight, 0); n++; |
|
391 bar->widget = XmCreateDrawingArea(parent, "pathbar", args, n); |
|
392 XtAddCallback( |
|
393 bar->widget, |
|
394 XmNresizeCallback, |
|
395 (XtCallbackProc)pathbar_resize, |
|
396 bar); |
|
397 XtAddCallback( |
|
398 bar->widget, |
|
399 XmNinputCallback, |
|
400 (XtCallbackProc)pathbar_input, |
|
401 bar); |
|
402 |
|
403 Arg a[4]; |
|
404 XtSetArg(a[0], XmNshadowThickness, 0); |
|
405 XtSetArg(a[1], XmNx, 0); |
|
406 XtSetArg(a[2], XmNy, 0); |
|
407 bar->textfield = XNECreateText(bar->widget, "pbtext", a, 3); |
|
408 bar->input = 0; |
|
409 XtAddCallback( |
|
410 bar->textfield, |
|
411 XmNlosingFocusCallback, |
|
412 (XtCallbackProc)pathbar_losingfocus, |
|
413 bar); |
|
414 XtAddCallback(bar->textfield, XmNactivateCallback, |
|
415 (XtCallbackProc)pathbar_pathinput, bar); |
|
416 XtAddEventHandler(bar->textfield, KeyPressMask | KeyReleaseMask, FALSE, pathTextEH, bar); |
|
417 |
|
418 XtSetArg(a[0], XmNarrowDirection, XmARROW_LEFT); |
|
419 bar->left = XmCreateArrowButton(bar->widget, "pbbutton", a, 1); |
|
420 XtSetArg(a[0], XmNarrowDirection, XmARROW_RIGHT); |
|
421 bar->right = XmCreateArrowButton(bar->widget, "pbbutton", a, 1); |
|
422 XtAddCallback( |
|
423 bar->left, |
|
424 XmNactivateCallback, |
|
425 (XtCallbackProc)pathbar_shift_left, |
|
426 bar); |
|
427 XtAddCallback( |
|
428 bar->right, |
|
429 XmNactivateCallback, |
|
430 (XtCallbackProc)pathbar_shift_right, |
|
431 bar); |
|
432 |
|
433 Pixel bg; |
|
434 XtVaGetValues(bar->textfield, XmNbackground, &bg, NULL); |
|
435 XtVaSetValues(bar->widget, XmNbackground, bg, NULL); |
|
436 |
|
437 XtManageChild(bar->left); |
|
438 XtManageChild(bar->right); |
|
439 |
|
440 XtVaGetValues(bar->left, XmNwidth, &bar->lw, NULL); |
|
441 XtVaGetValues(bar->right, XmNwidth, &bar->rw, NULL); |
|
442 |
|
443 bar->segmentAlloc = 16; |
|
444 bar->numSegments = 0; |
|
445 bar->pathSegments = (Widget*)XtCalloc(16, sizeof(Widget)); |
|
446 |
|
447 bar->selection = 0; |
|
448 |
|
449 return bar; |
|
450 } |
|
451 |
|
452 void PathBarChangeDir(Widget w, PathBar *bar, XtPointer c) |
|
453 { |
|
454 XmToggleButtonSetState(bar->pathSegments[bar->selection], False, False); |
|
455 |
|
456 for(int i=0;i<bar->numSegments;i++) { |
|
457 if(bar->pathSegments[i] == w) { |
|
458 bar->selection = i; |
|
459 XmToggleButtonSetState(w, True, False); |
|
460 break; |
|
461 } |
|
462 } |
|
463 |
|
464 int plen = strlen(bar->path); |
|
465 int countSeg = 0; |
|
466 for(int i=0;i<=plen;i++) { |
|
467 char c = bar->path[i]; |
|
468 if(c == '/' || c == '\0') { |
|
469 if(countSeg == bar->selection) { |
|
470 char *dir = XtMalloc(i+2); |
|
471 memcpy(dir, bar->path, i+1); |
|
472 dir[i+1] = '\0'; |
|
473 if(bar->updateDir) { |
|
474 bar->updateDir(bar->updateDirData, dir); |
|
475 } |
|
476 free(dir); |
|
477 } |
|
478 countSeg++; |
|
479 } |
|
480 } |
|
481 } |
|
482 |
|
483 void PathBarSetPath(PathBar *bar, const char *path) |
|
484 { |
|
485 if(bar->path) { |
|
486 free(bar->path); |
|
487 } |
|
488 bar->path = strdup(path); |
|
489 |
|
490 for(int i=0;i<bar->numSegments;i++) { |
|
491 XtDestroyWidget(bar->pathSegments[i]); |
|
492 } |
|
493 XtUnmanageChild(bar->textfield); |
|
494 XtManageChild(bar->left); |
|
495 XtManageChild(bar->right); |
|
496 bar->input = False; |
|
497 |
|
498 Arg args[4]; |
|
499 XmString str; |
|
500 |
|
501 bar->numSegments = 0; |
|
502 |
|
503 int i=0; |
|
504 if(path[0] == '/') { |
|
505 str = XmStringCreateLocalized("/"); |
|
506 XtSetArg(args[0], XmNlabelString, str); |
|
507 XtSetArg(args[1], XmNfillOnSelect, True); |
|
508 XtSetArg(args[2], XmNindicatorOn, False); |
|
509 bar->pathSegments[0] = XmCreateToggleButton( |
|
510 bar->widget, "pbbutton", args, 3); |
|
511 XtAddCallback( |
|
512 bar->pathSegments[0], |
|
513 XmNvalueChangedCallback, |
|
514 (XtCallbackProc)PathBarChangeDir, |
|
515 bar); |
|
516 XmStringFree(str); |
|
517 bar->numSegments++; |
|
518 i++; |
|
519 } |
|
520 |
|
521 int len = strlen(path); |
|
522 int begin = i; |
|
523 for(;i<=len;i++) { |
|
524 char c = path[i]; |
|
525 if((c == '/' || c == '\0') && i > begin) { |
|
526 char *segStr = XtMalloc(i - begin + 1); |
|
527 memcpy(segStr, path+begin, i-begin); |
|
528 segStr[i-begin] = '\0'; |
|
529 begin = i+1; |
|
530 |
|
531 str = XmStringCreateLocalized(segStr); |
|
532 XtFree(segStr); |
|
533 XtSetArg(args[0], XmNlabelString, str); |
|
534 XtSetArg(args[1], XmNfillOnSelect, True); |
|
535 XtSetArg(args[2], XmNindicatorOn, False); |
|
536 Widget button = XmCreateToggleButton(bar->widget, "pbbutton", args, 3); |
|
537 XtAddCallback( |
|
538 button, |
|
539 XmNvalueChangedCallback, |
|
540 (XtCallbackProc)PathBarChangeDir, |
|
541 bar); |
|
542 XmStringFree(str); |
|
543 |
|
544 if(bar->numSegments >= bar->segmentAlloc) { |
|
545 bar->segmentAlloc += 8; |
|
546 bar->pathSegments = realloc(bar->pathSegments, bar->segmentAlloc * sizeof(Widget)); |
|
547 } |
|
548 |
|
549 bar->pathSegments[bar->numSegments++] = button; |
|
550 } |
|
551 } |
|
552 |
|
553 bar->selection = bar->numSegments-1; |
|
554 XmToggleButtonSetState(bar->pathSegments[bar->selection], True, False); |
|
555 |
|
556 XNETextSetString(bar->textfield, (char*)path); |
|
557 XNETextSetInsertionPosition(bar->textfield, XNETextGetLastPosition(bar->textfield)); |
|
558 |
|
559 pathbar_resize(bar->widget, bar, NULL); |
|
560 } |
|
561 |
|
562 void PathBarDestroy(PathBar *pathbar) { |
|
563 if(pathbar->path) { |
|
564 XtFree(pathbar->path); |
|
565 } |
|
566 XtFree((void*)pathbar->pathSegments); |
|
567 XtFree((void*)pathbar); |
|
568 } |
|
569 |
|
570 |
|
571 /* ---------------------------- Path Text Field ---------------------------- */ |
|
572 |
|
573 static void destroy_pathbar(Widget w, XtPointer *data, XtPointer d) { |
|
574 PathBar *pathbar = (PathBar*)data; |
|
575 // TODO: check if there is somonething missing |
|
576 XtFree((void*)pathbar->pathSegments); |
|
577 XtFree((void*)pathbar); |
|
578 } |
|
579 |
|
580 UIWIDGET ui_path_textfield_create(UiObject* obj, UiPathTextFieldArgs args) { |
|
581 Arg xargs[16]; |
|
582 int n = 0; |
|
583 |
|
584 UiContainerPrivate *ctn = ui_obj_container(obj); |
|
585 UI_APPLY_LAYOUT(ctn->layout, args); |
|
586 |
|
587 Widget parent = ctn->prepare(ctn, xargs, &n); |
|
588 // TODO: name |
|
589 |
|
590 PathBar *pathbar = CreatePathBar(parent, xargs, n); |
|
591 XtManageChild(pathbar->widget); |
|
592 ctn->add(ctn, pathbar->widget); |
|
593 |
|
594 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING); |
|
595 if (var) { |
|
596 UiString* value = (UiString*)var->value; |
|
597 value->obj = pathbar; |
|
598 value->get = ui_path_textfield_get; |
|
599 value->set = ui_path_textfield_set; |
|
600 |
|
601 if(value->value.ptr) { |
|
602 char *str = strdup(value->value.ptr); |
|
603 ui_string_set(value, str); |
|
604 free(str); |
|
605 } |
|
606 } |
|
607 |
|
608 XtAddCallback( |
|
609 pathbar->widget, |
|
610 XmNdestroyCallback, |
|
611 (XtCallbackProc)destroy_pathbar, |
|
612 pathbar); |
|
613 |
|
614 return pathbar->widget; |
|
615 } |
|
616 |
|
617 char* ui_path_textfield_get(UiString *str) { |
|
618 |
|
619 } |
|
620 |
|
621 void ui_path_textfield_set(UiString *str, const char *value) { |
|
622 PathBarSetPath(str->obj, value); |
|
623 } |