Debian Codegraph @

texk/xdvik/gui/xm_prefs_helpers.c

raw
1 /*
2 * Copyright (c) 2004 Stefan Ulrich
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
24 /*
25 * Panel 3 (Helper Applications) for xdvik preferences dialog.
26 */
27
28 #include "xdvi-config.h"
29 #include "xdvi.h"
30
31 #include "x_util.h"
32 #include "xm_colorsel.h"
33 #include "topic-window.h"
34 #include "util.h"
35 #include "string-utils.h"
36 #include "string_list.h"
37 #include "message-window.h"
38 #include "events.h"
39 #include "xm_menu.h"
40
41 #include "xm_prefsP.h"
42 #include "xm_prefs.h"
43 #include "xm_prefs_helpers.h"
44
45 #ifdef MOTIF /* entire file */
46
47 #include <X11/Xatom.h>
48
49 #include <Xm/Xm.h>
50 #include <Xm/Protocols.h>
51 #include <Xm/DialogS.h>
52 #include <Xm/LabelG.h>
53 #include <Xm/PushB.h>
54 #include <Xm/Form.h>
55 #include <Xm/Frame.h>
56 #include <Xm/PanedW.h>
57 #include <Xm/DrawnB.h>
58 #include <Xm/RowColumn.h>
59 #include <Xm/Scale.h>
60 #include <Xm/ToggleBG.h>
61 #include <Xm/PushBG.h>
62 #include <Xm/Text.h>
63 #include <Xm/TextF.h>
64 #include <Xm/FileSB.h>
65 #include <Xm/SelectioB.h>
66
67 struct choice_dialog_info {
68 struct topic_info *tinfo;
69 /* struct prefs_choice *prefs; */
70 Widget combo_box;
71 Widget message_popup;
72 };
73
74 static char **m_browser_list = NULL;
75 static char **m_editor_list = NULL;
76
77 #if USE_COMBOBOX
78 #include <Xm/ComboBox.h>
79 #include <Xm/List.h>
80 #else
81 #include <Xm/CascadeBG.h>
82 #endif
83
84 static void select_browser_cb(Widget w, XtPointer client_data, XtPointer call_data);
85 static void select_editor_cb(Widget w, XtPointer client_data, XtPointer call_data);
86
87 /*
88 * User clicked on `Help' in browser text input prompt
89 */
90 static void
91 help_browser_dialog_cb(Widget w, XtPointer client_data, XtPointer call_data)
92 {
93 Widget popup = popup_message(XtParent(w),
94 MSG_HELP,
95 NULL,
96 "The browser is used by xdvi to browse remote documents. "
97 "Please enter the name of the browser (i.e. the executable program) "
98 "you want to use. The browser command may optionally contain a string "
99 "`%%s' which is replaced by the current URL. If no `%%s' is present, "
100 "the URL argument is simply appended to the command.");
101 UNUSED(client_data);
102 UNUSED(call_data);
103
104 TRACE_GUI((stderr, "setting user data to %p", (void *)popup));
105 XtVaSetValues(w, XmNuserData, popup, NULL);
106 }
107
108 /*
109 * User clicked on `Help' in editor text input prompt
110 */
111 static void
112 help_editor_dialog_cb(Widget w, XtPointer client_data, XtPointer call_data)
113 {
114 Widget popup = popup_message(XtParent(w),
115 MSG_HELP,
116 NULL,
117 "The editor is used by reverse search to open the TeX source for a DVI file "
118 "(see `Help' -> `Source Specials' for details)."
119 "Please enter the name of the editor executable you want to use. "
120 "The editor command may optionally contain two format strings: "
121 "`%%l' is replaced by the line number in the TeX file, and `%%f' by the file name. "
122 "(If the format strings are not present, they are appended implicitly.)");
123 UNUSED(client_data);
124 UNUSED(call_data);
125
126 TRACE_GUI((stderr, "setting user data to %p", (void *)popup));
127 XtVaSetValues(w, XmNuserData, popup, NULL);
128 }
129
130 /*
131 * User cancelled browser or editor text input prompt
132 */
133 static void
134 destroy_dialog_cb(Widget w, XtPointer client_data, XtPointer call_data)
135 {
136 /* Widget browser_combo = (Widget)client_data; */
137 struct choice_dialog_info *info = (struct choice_dialog_info *)client_data;
138 Widget browser_combo = NULL;
139 Widget help_popup;
140 #if USE_COMBOBOX
141 Widget browser_list_w;
142 #endif
143 UNUSED(call_data);
144
145 XtVaGetValues(w, XmNuserData, &help_popup, NULL);
146 TRACE_GUI((stderr, "GOT help_popup: %p", (void *)help_popup));
147
148 if (info != NULL)
149 browser_combo = info->combo_box;
150
151 if (browser_combo != NULL) {
152 #if USE_COMBOBOX
153 /* Need to unselect the `Other...' entry from the list; to do this,
154 we select the index from XmNuserData if it's >= 0, or the first item. */
155 ptrdiff_t idx;
156 XtPointer p;
157 XtVaGetValues(browser_combo,
158 XmNlist, &browser_list_w,
159 XmNuserData, &p,
160 NULL);
161 idx = (ptrdiff_t) p;
162 if (browser_list_w == 0)
163 XDVI_ERROR((stderr, "couldn't get list component of combo box!\n"));
164 else {
165 if (idx < 0)
166 idx = 0;
167 XmListSelectPos(browser_list_w, idx + 1, True);
168 }
169 #else
170 Widget parent;
171 if ((parent = XtParent(w)) != NULL) {
172 Widget rowcol, child;
173 if (strcmp(XtName(parent), Xdvi_BROWSER_POPUP) == 0) {
174 if (get_widget_by_name(&rowcol, globals.widgets.top_level, Xdvi_BROWSER_COMBO_NAME, True)) {
175 XtVaGetValues(rowcol, XmNuserData, &child, NULL);
176 XtVaSetValues(rowcol, XmNmenuHistory, child, NULL);
177 }
178 }
179 else if (strcmp(XtName(parent), Xdvi_EDITOR_POPUP) == 0) {
180 if (get_widget_by_name(&rowcol, globals.widgets.top_level, Xdvi_EDITOR_COMBO_NAME, True)) {
181 XtVaGetValues(rowcol, XmNuserData, &child, NULL);
182 XtVaSetValues(rowcol, XmNmenuHistory, child, NULL);
183 }
184 }
185 else {
186 XDVI_WARNING((stderr, "unexpected widget name `%s' in destroy_dialog_cb", XtName(parent)));
187 }
188 }
189 #endif
190 }
191
192 /*
193 * Also popdown dependent help window if it exists before destroying the widget;
194 * otherwise, Motif may even crash in _XmIsFastSubclass() when the help window is moved!
195 *
196 * Since `help_popup' is the top-level xmDialogShellWidget, we need to get its
197 * xmMessageBoxWidget child (by name ...). Don't report an error if it doesn't
198 * exist in case the help window has already been closed.
199 */
200 if (help_popup != NULL) {
201 static Widget message;
202 if (get_widget_by_name(&message, help_popup, Xdvi_MESSAGE_DIALOG_NAME, False))
203 XtCallCallbacks(message, XmNokCallback, NULL);
204 }
205
206 if (info != NULL) {
207 struct prefs_choice *prefs = (struct prefs_choice *)info->tinfo->data;
208 remove_from_deplist(prefs, w);
209 free(info);
210 }
211
212 /* destroy the parent of this dialog (the shell) */
213 XtDestroyWidget(XtParent(w));
214 }
215
216 static void
217 h_get_input_wrapper(const char *listbox_name,
218 Widget w, XtPointer client_data, XtPointer call_data)
219 {
220 /* Widget combobox = (Widget)client_data; */
221 struct choice_dialog_info *info = (struct choice_dialog_info *)client_data;
222 struct topic_info *tinfo = info->tinfo;
223 struct prefs_choice *prefs = (struct prefs_choice *)tinfo->data;
224 Widget combobox, child;
225 /* following need to be allocated since we want to set resource.xyz to it */
226 static char *browser_choice= NULL, *editor_choice = NULL;
227 #if !USE_COMBOBOX
228 XtCallbackProc select_cb = NULL;
229 #endif
230 XmSelectionBoxCallbackStruct *cbs = (XmSelectionBoxCallbackStruct *)call_data;
231 static char *ptr = NULL;
232 char *tmp_list;
233
234 if (call_data == NULL) /* widget already being destroyed? */
235 return;
236
237 ASSERT(info != NULL, "client_data in h_get_input_wrapper mustn't be NULL!");
238 combobox = info->combo_box;
239 child = info->message_popup;
240 XtVaGetValues(w, XmNuserData, &child, NULL);
241 TRACE_GUI((stderr, "GOT child: %p", (void *)child));
242
243 if (ptr)
244 XtFree((XtPointer)ptr);
245 ptr = NULL;
246 XmStringGetLtoR(cbs->value, G_charset, &ptr);
247 if (strlen(ptr) == 0 || is_spaces_only(ptr)) {
248 popup_message(XtParent(w),
249 MSG_ERR,
250 NULL,
251 "Empty input string");
252 return;
253 }
254
255 if (strcmp(listbox_name, Xdvi_BROWSER_COMBO_NAME) == 0) {
256 #if 0
257 int i;
258 #endif
259
260 free(browser_choice);
261 browser_choice = xstrdup(ptr);
262 resource.browser = browser_choice;
263
264 #if 0
265 for (i = 0; m_browser_list[i] != NULL; i++) {
266 fprintf(stderr, "list %d: |%s|\n", i, m_browser_list[i]);
267 }
268 #endif
269 m_browser_list = string_list_prepend(m_browser_list, ptr);
270 #if 0
271 for (i = 0; m_browser_list[i] != NULL; i++) {
272 fprintf(stderr, "NEW list %d: |%s|\n", i, m_browser_list[i]);
273 }
274 #endif
275 tmp_list = string_list_to_str(m_browser_list, "\n");
276 #if 0
277 fprintf(stderr, "TMP LIST: |%s|\n", tmp_list);
278 #endif /* 0 */
279 store_preference(&(prefs->db), "prefsBrowserList", "%s", tmp_list);
280 free(tmp_list);
281
282 store_preference(&(prefs->db), "wwwBrowser", "%s", ptr);
283 #if !USE_COMBOBOX
284 select_cb = select_browser_cb;
285 #endif
286 }
287 else if (strcmp(listbox_name, Xdvi_EDITOR_COMBO_NAME) == 0) {
288 free(editor_choice);
289 editor_choice = xstrdup(ptr);
290 resource.editor = editor_choice;
291
292 m_editor_list = string_list_prepend(m_editor_list, ptr);
293 tmp_list = string_list_to_str(m_editor_list, "\n");
294 store_preference(&(prefs->db), "prefsEditorList", "%s", tmp_list);
295 free(tmp_list);
296
297 store_preference(&(prefs->db), "editor", "%s", ptr);
298 #if !USE_COMBOBOX
299 select_cb = select_editor_cb;
300 #endif
301 }
302 else
303 XDVI_WARNING((stderr, "Unexpected name in h_get_input_wrapper: `%s'", listbox_name));
304
305 #if USE_COMBOBOX
306 UNUSED(listbox_name);
307 /* add user input to the combo box list, and make it current */
308 XmComboBoxAddItem(combobox, cbs->value, 1, True);
309 XmComboBoxSelectItem(combobox, cbs->value);
310 { /* if more than 9 items, add scrollbar */
311 size_t n;
312 XtVaGetValues(combobox, XmNitemCount, &n, NULL);
313 XtVaSetValues(combobox, XmNvisibleItemCount, n > 10 ? 10 : n, NULL);
314 }
315 #else
316 {
317 Widget new_menu, parent, grandparent, rowcol;
318 if ((parent = XtParent(w)) != NULL && (grandparent = XtParent(parent)) != NULL) {
319 /* add new item to front of list ... */
320 new_menu = XtVaCreateManagedWidget(ptr, xmPushButtonGadgetClass, grandparent,
321 XmNpositionIndex, 0,
322 XmNuserData, tinfo,
323 NULL);
324 if (select_cb != NULL) {
325 XtAddCallback(new_menu, XmNactivateCallback, select_cb, grandparent);
326 }
327 /* ... and make it current! */
328 if (get_widget_by_name(&rowcol, globals.widgets.top_level, listbox_name, True)) {
329 XtVaSetValues(rowcol, XmNmenuHistory, new_menu, NULL);
330 }
331 }
332 }
333 #endif
334
335 /* as above: popdown help window */
336 if (child != NULL) {
337 Widget message;
338 if (get_widget_by_name(&message, child, Xdvi_MESSAGE_DIALOG_NAME, False))
339 XtCallCallbacks(message, XmNokCallback, NULL);
340 }
341
342 remove_from_deplist(prefs, w);
343 free(info);
344
345 /* destroy the parent of this dialog (the shell) */
346 XtDestroyWidget(XtParent(w));
347 }
348
349 static void
350 h_selector(const char *prompt_name,
351 const char *title_str, const char *label_str,
352 XtCallbackProc ok_cb,
353 XtCallbackProc destroy_cb,
354 XtCallbackProc help_cb,
355 Widget w, XtPointer client_data, XtPointer call_data)
356 {
357 struct topic_info *tinfo = NULL;
358 struct prefs_choice *prefs = NULL;
359 struct choice_dialog_info *info = NULL;
360 char *choice;
361 /* following need to be allocated since we want to set resource.xyz to it */
362 static char *browser_choice= NULL, *editor_choice = NULL;
363
364 #if USE_COMBOBOX
365 XmComboBoxCallbackStruct *cb;
366 #else
367 XmString str;
368 UNUSED(client_data);
369 UNUSED(call_data);
370 #endif
371
372 #if USE_COMBOBOX
373 tinfo = (struct topic_info *)client_data;
374 prefs = (struct prefs_choice *)tinfo->data;
375 #endif
376 info = xmalloc(sizeof *info);
377 #if USE_COMBOBOX
378 cb = (XmComboBoxCallbackStruct *)call_data;
379
380 if (cb->event == NULL) /* only browsing, no selection */
381 return;
382
383 choice = (char *)XmStringUnparse(cb->item_or_text, XmFONTLIST_DEFAULT_TAG,
384 XmCHARSET_TEXT, XmCHARSET_TEXT,
385 NULL, 0,
386 XmOUTPUT_ALL);
387 #else
388 XtVaGetValues(w, XmNlabelString, &str, NULL);
389 XmStringGetLtoR(str, G_charset, &choice);
390
391 XtVaGetValues(w, XmNuserData, &tinfo, NULL);
392 ASSERT(tinfo != NULL, "XmNuserData in callback musn't be NULL!");
393 prefs = (struct prefs_choice *)tinfo->data;
394 #endif
395
396 if (strcmp(choice, Xdvi_ADD_COMMAND_STR) == 0) {
397 Widget prompt_widget;
398 Arg args[8];
399 int n = 0;
400 XmString title = XmStringCreateLocalized((char *)title_str);
401 XmString label = XmStringCreateLocalized((char *)label_str);
402
403 XtSetArg(args[n], XmNselectionLabelString, label); n++;
404 XtSetArg(args[n], XmNautoUnmanage, False); n++;
405 XtSetArg(args[n], XmNdialogTitle, title); n++;
406 XtSetArg(args[n], XmNuserData, NULL); n++;
407 prompt_widget = XmCreatePromptDialog(
408 #if USE_COMBOBOX
409 w,
410 #else
411 XtParent(w),
412 #endif
413 (char *)prompt_name, args, n);
414 /* XmStringFree(label); */
415
416 add_to_deplist(prefs, prompt_widget);
417
418 info->tinfo = tinfo;
419 info->combo_box = w;
420 info->message_popup = NULL;
421
422 /* Note: w is the browser_combo widget */
423 XtAddCallback(prompt_widget, XmNokCallback, ok_cb, (XtPointer)info);
424 XtAddCallback(prompt_widget, XmNcancelCallback, destroy_cb, (XtPointer)info);
425 XtAddCallback(prompt_widget, XmNhelpCallback, help_cb, (XtPointer)info);
426
427 XtManageChild(prompt_widget);
428 }
429 else { /* normal item */
430 #if USE_COMBOBOX
431 int i;
432 #endif
433
434 if (strcmp(prompt_name, Xdvi_BROWSER_POPUP_NAME) == 0) {
435 #if !USE_COMBOBOX
436 Widget rowcol;
437 if (get_widget_by_name(&rowcol, globals.widgets.top_level, Xdvi_BROWSER_COMBO_NAME, True)) {
438 XtVaSetValues(rowcol, XmNuserData, w, NULL);
439 }
440 #endif
441 free(browser_choice);
442 browser_choice = xstrdup(choice);
443 resource.browser = browser_choice;
444 store_preference(&(prefs->db), "wwwBrowser", "%s", browser_choice);
445 }
446 else if (strcmp(prompt_name, Xdvi_EDITOR_POPUP_NAME) == 0) {
447 #if !USE_COMBOBOX
448 Widget rowcol;
449 if (get_widget_by_name(&rowcol, globals.widgets.top_level, Xdvi_EDITOR_COMBO_NAME, True)) {
450 XtVaSetValues(rowcol, XmNuserData, w, NULL);
451 }
452 #endif
453 free(editor_choice);
454 editor_choice = xstrdup(choice);
455 resource.editor = editor_choice;
456 store_preference(&(prefs->db), "editor", "%s", editor_choice);
457 }
458 else
459 XDVI_ERROR((stderr, "Unknown category `%s' in h_selector()!", prompt_name));
460
461
462 #if USE_COMBOBOX
463 /* update the currently selected value */
464 XtVaGetValues(w, XmNselectedPosition, &i, NULL);
465 XtVaSetValues(w, XmNuserData, cast_int_to_XtPointer(i), NULL);
466 #endif
467 }
468 #if USE_COMBOBOX
469 XtFree(choice);
470 #endif
471 }
472
473 /*
474 * User OK'ed browser text input prompt
475 */
476 static void
477 get_browser_text_cb(Widget w, XtPointer client_data, XtPointer call_data)
478 {
479 if (XtIsRealized(w)) {
480 h_get_input_wrapper(Xdvi_BROWSER_COMBO_NAME, w, client_data, call_data);
481 }
482 }
483
484
485 /*
486 * User OK'ed editor text input prompt
487 */
488 static void
489 get_editor_text_cb(Widget w, XtPointer client_data, XtPointer call_data)
490 {
491 if (XtIsRealized(w)) {
492 h_get_input_wrapper(Xdvi_EDITOR_COMBO_NAME, w, client_data, call_data);
493 }
494 }
495
496
497 /*
498 * User selected an item from browser combo box pulldown list
499 */
500 static void
501 select_browser_cb(Widget w, XtPointer client_data, XtPointer call_data)
502 {
503 h_selector(Xdvi_BROWSER_POPUP_NAME,
504 "xdvik: Add Browser Command",
505 "Browser Command (optional `%s' is replaced by URL): ",
506 get_browser_text_cb, destroy_dialog_cb, help_browser_dialog_cb,
507 w, client_data, call_data);
508 }
509
510 /*
511 * User selected an item from editor combo box pulldown list
512 */
513 static void
514 select_editor_cb(Widget w, XtPointer client_data, XtPointer call_data)
515 {
516 UNUSED(client_data);
517 h_selector(Xdvi_EDITOR_POPUP_NAME,
518 "xdvik: Add Editor Command",
519 "Editor Command (optional: `%l' = line number, `%f' = file name): ",
520 get_editor_text_cb, destroy_dialog_cb, help_editor_dialog_cb,
521 w, client_data, call_data);
522 }
523
524 void
525 update_preferences_helpers(void)
526 {
527 Widget shell, browser_cascade, editor_cascade;
528
529 if (get_widget_by_name(&shell, globals.widgets.top_level, Xdvi_PREFS_DIALOG_NAME, False)
530 && get_widget_by_name(&browser_cascade, shell, Xdvi_BROWSER_COMBO_NAME, True)
531 && get_widget_by_name(&editor_cascade, shell, Xdvi_EDITOR_COMBO_NAME, True)) {
532
533 #if USE_COMBOBOX
534
535 Widget browser_list_w, editor_list_w;
536 XmString str;
537 int i;
538 Boolean found = False;
539
540 XtVaGetValues(browser_cascade, XmNlist, &browser_list_w, NULL);
541 XtVaGetValues(editor_cascade, XmNlist, &editor_list_w, NULL);
542
543 for (i = 0; m_browser_list[i] != NULL; i++) {
544 if (globals.curr_browser != NULL) { /* if this is set, ignore browser setting */
545 if (strcmp(globals.curr_browser, m_browser_list[i]) == 0) {
546 found = True;
547 break;
548 }
549 }
550 else if (resource.browser != NULL) {
551 if (strcmp(resource.browser, m_browser_list[i]) == 0) {
552 found = True;
553 break;
554 }
555 }
556 }
557 if (!found)
558 i = 0;
559 str = XmStringCreateLtoR((char *)m_browser_list[i], "UNMARKED");
560 XmComboBoxSelectItem(browser_cascade, str);
561 XmStringFree(str);
562
563 for (i = 0; m_editor_list[i] != NULL; i++) {
564 if (globals.curr_editor != NULL) { /* if this is set, ignore editor setting */
565 if (strcmp(globals.curr_editor, m_editor_list[i]) == 0) {
566 found = True;
567 break;
568 }
569 }
570 else if (resource.editor != NULL) {
571 if (strcmp(resource.editor, m_editor_list[i]) == 0) {
572 found = True;
573 break;
574 }
575 }
576 }
577 if (!found)
578 i = 0;
579 str = XmStringCreateLtoR((char *)m_editor_list[i], "UNMARKED");
580 XmComboBoxSelectItem(editor_cascade, str);
581 XmStringFree(str);
582 /* XmListSetPos(editor_list_w, i); */
583 /* fprintf(stderr, "setting editor list index %d, %s\n", */
584 /* i, editor_list[i-1]); */
585
586
587 /* if (XmListGetSelectedPos(browser_list_w, &browser_items, &browser_cnt) */
588 /* && XmListGetSelectedPos(editor_list_w, &editor_items, &editor_cnt)) { */
589 /* if (browser_cnt > 0 && editor_cnt > 0) { */
590 /* int browser_idx = browser_items[0] - 1; */
591 /* int editor_idx = editor_items[0] - 1; */
592 /* fprintf(stderr, "selected: %s, %s\n", */
593 /* browser_list[browser_idx], */
594 /* editor_list[editor_idx]); */
595 /* } */
596 /* else { */
597 /* XDVI_WARNING((stderr, "Shouldn't happen: No items selected in browser list?")); */
598 /* return; */
599 /* } */
600 /* XtFree((XtPointer)browser_items); */
601 /* XtFree((XtPointer)editor_items); */
602 /* } */
603 #else
604 Widget browser_menu, editor_menu;
605 if (get_widget_by_name(&browser_menu, shell, Xdvi_BROWSER_MENU_NAME, True)
606 && get_widget_by_name(&editor_menu, shell, Xdvi_EDITOR_MENU_NAME, True)) {
607 int i, num_buttons;
608 WidgetList buttons;
609
610 XtVaGetValues(browser_menu,
611 XmNnumChildren, &num_buttons,
612 XmNchildren, &buttons,
613 NULL);
614 for (i = 0; i < num_buttons; i++) {
615 XmString str;
616 char *ptr;
617
618 XtVaGetValues(buttons[i], XmNlabelString, &str, NULL);
619 XmStringGetLtoR(str, G_charset, &ptr);
620 /* fprintf(stderr, "kid %d: %s\n", i, ptr); */
621 if (globals.curr_browser != NULL) { /* if this is set, ignore browser setting */
622 if (strcmp(globals.curr_browser, ptr) == 0) {
623 break;
624 }
625 }
626 else if (resource.browser != NULL) {
627 if (strcmp(resource.browser, ptr) == 0) {
628 break;
629 }
630 }
631 }
632 if (i >= num_buttons) /* not found */
633 i = 0;
634 XtVaSetValues(browser_cascade, XmNmenuHistory, buttons[i], NULL);
635
636 /* same for editor */
637 XtVaGetValues(editor_menu,
638 XmNnumChildren, &num_buttons,
639 XmNchildren, &buttons, NULL);
640 for (i = 0; i < num_buttons; i++) {
641 XmString str;
642 char *ptr;
643
644 XtVaGetValues(buttons[i], XmNlabelString, &str, NULL);
645 XmStringGetLtoR(str, G_charset, &ptr);
646 /* fprintf(stderr, "kid %d: %s\n", i, ptr); */
647 if (globals.curr_editor != NULL) { /* if this is set, ignore editor setting */
648 if (strcmp(globals.curr_editor, ptr) == 0) {
649 break;
650 }
651 }
652 else if (resource.editor != NULL) {
653 if (strcmp(resource.editor, ptr) == 0) {
654 break;
655 }
656 }
657 }
658 if (i >= num_buttons) /* not found */
659 i = 0;
660 XtVaSetValues(editor_cascade, XmNmenuHistory, buttons[i], NULL);
661
662 }
663 #endif
664 /* for browser/editor, try:
665
666 Widget menu;
667 int num_buttons;
668 WidgetList buttons;
669
670 XtVaGetValues( simple_option_widget, XmNsubMenuId, &menu, NULL);
671
672 XtVaGetValues( menu, XmNnumChildren, &num_buttons,
673 XmNchildren, &buttons, NULL ) ;
674
675 */
676 }
677 }
678
679 static Widget
680 h_create_command(const char *name,
681 const char *menu_name,
682 const char *label,
683 const char *curr_value,
684 Widget parent, Widget top,
685 char **command_list,
686 XtCallbackProc select_cb,
687 struct topic_info *tinfo)
688 {
689 const char *ptr = NULL;
690 size_t i;
691
692 #if USE_COMBOBOX
693 XmStringTable str_list;
694 Widget text_label, combo_box;
695 size_t k, num;
696 int curr_index = -1;
697 #else
698 Widget menu, cascade, item;
699 XmString str;
700 Arg args[8];
701 int n;
702 #endif
703
704 /* check if we need to add the default resource name */
705 ptr = curr_value;
706
707 /* don't add it if it's already in our list */
708 for (i = 0; ptr != NULL && command_list[i] != NULL; i++) {
709 if (strcmp(command_list[i], ptr) == 0) {
710 ptr = NULL;
711 }
712 }
713 #if USE_COMBOBOX
714 UNUSED(menu_name);
715 text_label = XmCreateLabelGadget(parent, (char *)label, NULL, 0);
716 XtVaSetValues(text_label,
717 XmNtopAttachment, XmATTACH_WIDGET,
718 XmNtopWidget, top,
719 XmNleftAttachment, XmATTACH_FORM,
720 /* XmNrightAttachment, XmATTACH_FORM, */
721 NULL);
722
723 /* count elements in command_list */
724 for (num = 0; command_list[num] != NULL; num++) { ; }
725
726 if (ptr != NULL)
727 num++;
728
729 str_list = (XmStringTable)XtMalloc(num * sizeof(XmString *));
730
731 i = 0;
732 if (ptr != NULL)
733 str_list[i++] = XmStringCreateLtoR((char *)ptr, "UNMARKED");
734
735 for (k = 0; i < num; i++, k++) {
736 if (curr_value != NULL && strcmp(command_list[k], curr_value) == 0) {
737 curr_index = i;
738 }
739 if (strcmp(command_list[k], Xdvi_ADD_COMMAND_STR) == 0)
740 str_list[i] = XmStringCreateLtoR((char *)command_list[k], "MARKED");
741 else
742 str_list[i] = XmStringCreateLtoR((char *)command_list[k], "UNMARKED");
743 }
744 combo_box = XtVaCreateWidget(name, xmComboBoxWidgetClass,
745 parent,
746 XmNtopAttachment, XmATTACH_WIDGET,
747 XmNtopWidget, top, /* if top == NULL, this uses XmATTACH_FORM */
748 XmNleftAttachment, XmATTACH_WIDGET,
749 XmNleftWidget, text_label,
750 XmNrightAttachment, XmATTACH_FORM,
751 XmNcomboBoxType, XmDROP_DOWN_LIST,
752 XmNitems, str_list,
753 XmNitemCount, num,
754 XmNvisibleItemCount, num > 10 ? 10 : num,
755 XmNuserData, cast_int_to_XtPointer(curr_index),
756 XmNarrowSize, Xdvi_COMBO_BOX_ARROW_SIZE,
757 NULL);
758 if (top != NULL) /* FIXME: This assumes we only have 2 items ... */
759 XtVaSetValues(combo_box,
760 XmNbottomAttachment, XmATTACH_FORM,
761 NULL);
762
763 /* make resource setting current value */
764 if (curr_index >= 0) {
765 XmComboBoxSelectItem(combo_box, str_list[curr_index]);
766 }
767
768 for (i = 0; i < num; i++)
769 XmStringFree(str_list[i]);
770 XtFree((XtPointer)str_list);
771
772 XtAddCallback(combo_box, XmNselectionCallback, select_cb, (XtPointer)tinfo);
773
774 /*
775 * workaround for pointer grabbing bug (see xm_menu.c). We need to use
776 * the popdownCallback of the internal `GrabShell' child of the combo box.
777 */
778 {
779 Widget grab_shell;
780 if (get_widget_by_name(&grab_shell, combo_box, "GrabShell", True))
781 XtAddCallback(grab_shell, XtNpopdownCallback, popdown_callback, NULL);
782 }
783 adjust_heights(text_label, combo_box, NULL);
784
785 XtManageChild(text_label);
786 XtManageChild(combo_box);
787
788 return combo_box;
789 #else
790 menu = XmCreatePulldownMenu(parent, (char *)menu_name, NULL, 0);
791 /* XtVaSetValues(menu, */
792 /* XmNtopAttachment, XmATTACH_WIDGET, */
793 /* XmNtopWidget, top, */
794 /* XmNleftAttachment, XmATTACH_FORM, */
795 /* XmNrightAttachment, XmATTACH_FORM, */
796 /* NULL); */
797
798 str = XmStringCreateLocalized((char *)label);
799 n = 0;
800 XtSetArg(args[n], XmNsubMenuId, menu); n++;
801 XtSetArg(args[n], XmNlabelString, str); n++;
802 XtSetArg(args[n], XmNuserData, NULL); n++;
803 cascade = XmCreateOptionMenu(parent, (char *)name, args, n);
804 if (top)
805 XtVaSetValues(cascade,
806 XmNtopAttachment, XmATTACH_WIDGET,
807 XmNtopWidget, top,
808 XmNleftAttachment, XmATTACH_FORM,
809 NULL);
810 else
811 XtVaSetValues(cascade,
812 XmNtopAttachment, XmATTACH_FORM,
813 XmNleftAttachment, XmATTACH_FORM,
814 NULL);
815 XmStringFree(str);
816
817 if (ptr != NULL) {
818 item = XtVaCreateManagedWidget(ptr, xmPushButtonGadgetClass, menu,
819 XmNuserData, tinfo,
820 NULL);
821 XtAddCallback(item, XmNactivateCallback, select_cb, menu);
822 }
823
824 for (i = 0; command_list[i] != NULL; i++) {
825 item = XtVaCreateManagedWidget(command_list[i], xmPushButtonGadgetClass, menu,
826 XmNuserData, tinfo,
827 NULL);
828 XtAddCallback(item, XmNactivateCallback, select_cb, menu);
829 }
830 XtManageChild(cascade);
831 return cascade;
832 #endif
833 }
834
835 Widget
836 prefs_helpers(struct topic_info *tinfo)
837 {
838 /* struct prefs_choice *prefs = (struct prefs_choice *)info->data; */
839 Widget form, /* frame, */ form1;
840 Widget browser_command;
841
842 form = XmCreateForm(tinfo->right_form, "helpers_form", NULL, 0);
843
844 /* frame = XmCreateFrame(form, "commands_frame", NULL, 0); */
845
846 form1 = XtVaCreateWidget("commands_form", xmFormWidgetClass,
847 form,
848 XmNverticalSpacing, 10,
849 XmNhorizontalSpacing, 0,
850 NULL);
851
852 if (m_browser_list == NULL)
853 m_browser_list = get_separated_list(resource.prefs_browser_list, "\n", False);
854
855 if (m_editor_list == NULL)
856 m_editor_list = get_separated_list(resource.prefs_editor_list, "\n", False);
857
858 browser_command = h_create_command(Xdvi_BROWSER_COMBO_NAME,
859 Xdvi_BROWSER_MENU_NAME,
860 "Web Browser: ",
861 resource.browser,
862 form1, NULL,
863 m_browser_list,
864 select_browser_cb, tinfo);
865
866 h_create_command(Xdvi_EDITOR_COMBO_NAME,
867 Xdvi_EDITOR_MENU_NAME,
868 "Editor for Source Specials: ",
869 resource.editor,
870 form1, browser_command,
871 m_editor_list,
872 select_editor_cb, tinfo);
873
874 /* #if PS_GS */
875 /* Widget b2 = XmCreateLabelGadget(rowcol, "[x] Use Ghostscript to interpret PS specials", NULL, 0); */
876
877 /* TODO: don't need this??? Similar: ps2pdf, dvips conversion?? */
878 /* Widget b3 = XmCreateLabelGadget(rowcol, "Path to Ghostscript: ____________", NULL, 0); */
879
880 /* #endif */
881 /* Widget b4 = XmCreateLabelGadget(rowcol, "Editor for source specials: <pulldown> [Other ...]", NULL, 0); */
882 /* XtManageChild(b1); */
883 /* XtManageChild(b2); */
884 /* XtManageChild(b3); */
885 /* XtManageChild(b4); */
886 /* XtManageChild(rowcol); */
887
888 XtManageChild(form1);
889 /* XtManageChild(frame); */
890
891 return form;
892 }
893
894 #else
895 /* silence `empty compilation unit' warnings */
896 static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); }
897 #endif /* MOTIF */
898