summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvik/gui/xm_prefs_helpers.c
blob: 49b19c406a41d3ff4f62654cbb3bbc987e857eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/*
 * Copyright (c) 2004 Stefan Ulrich
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

/*
 * Panel 3 (Helper Applications) for xdvik preferences dialog.
 */

#include "xdvi-config.h"
#include "xdvi.h"

#include "x_util.h"
#include "xm_colorsel.h"
#include "topic-window.h"
#include "util.h"
#include "string-utils.h"
#include "string_list.h"
#include "message-window.h"
#include "events.h"
#include "xm_menu.h"

#include "xm_prefsP.h"
#include "xm_prefs.h"
#include "xm_prefs_helpers.h"

#ifdef MOTIF /* entire file */

#include <X11/Xatom.h>

#include <Xm/Xm.h>
#include <Xm/Protocols.h>
#include <Xm/DialogS.h>
#include <Xm/LabelG.h>
#include <Xm/PushB.h>
#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <Xm/PanedW.h>
#include <Xm/DrawnB.h>
#include <Xm/RowColumn.h>
#include <Xm/Scale.h>
#include <Xm/ToggleBG.h>
#include <Xm/PushBG.h>
#include <Xm/Text.h>
#include <Xm/TextF.h>
#include <Xm/FileSB.h>
#include <Xm/SelectioB.h>

struct choice_dialog_info {
    struct topic_info *tinfo;
    /*     struct prefs_choice *prefs; */
    Widget combo_box;
    Widget message_popup;
};

static char **m_browser_list = NULL;
static char **m_editor_list = NULL;

#if USE_COMBOBOX
#include <Xm/ComboBox.h>
#include <Xm/List.h>
#else
#include <Xm/CascadeBG.h>
#endif

static void select_browser_cb(Widget w, XtPointer client_data, XtPointer call_data);
static void select_editor_cb(Widget w, XtPointer client_data, XtPointer call_data);

/*
 * User clicked on `Help' in browser text input prompt
 */
static void
help_browser_dialog_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
    Widget popup = popup_message(XtParent(w),
				 MSG_HELP,
				 NULL,
				 "The browser is used by xdvi to browse remote documents. "
				 "Please enter the name of the browser (i.e. the executable program) "
				 "you want to use. The browser command may optionally contain a string "
				 "`%%s' which is replaced by the current URL. If no `%%s' is present, "
				 "the URL argument is simply appended to the command.");
    UNUSED(client_data);
    UNUSED(call_data);

    TRACE_GUI((stderr, "setting user data to %p", (void *)popup));
    XtVaSetValues(w, XmNuserData, popup, NULL);
}

/*
 * User clicked on `Help' in editor text input prompt
 */
static void
help_editor_dialog_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
    Widget popup = popup_message(XtParent(w),
				 MSG_HELP,
				 NULL,
				 "The editor is used by reverse search to open the TeX source for a DVI file "
				 "(see `Help' -> `Source Specials' for details)."
				 "Please enter the name of the editor executable you want to use. "
				 "The editor command may optionally contain two format strings: "
				 "`%%l' is replaced by the line number in the TeX file, and `%%f' by the file name. "
				 "(If the format strings are not present, they are appended implicitly.)");
    UNUSED(client_data);
    UNUSED(call_data);

    TRACE_GUI((stderr, "setting user data to %p", (void *)popup));
    XtVaSetValues(w, XmNuserData, popup, NULL);
}

/*
 * User cancelled browser or editor text input prompt
 */
static void
destroy_dialog_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
    /*     Widget browser_combo = (Widget)client_data; */
    struct choice_dialog_info *info = (struct choice_dialog_info *)client_data;
    Widget browser_combo = NULL;
    Widget help_popup;
#if USE_COMBOBOX
    Widget browser_list_w;
#endif
    UNUSED(call_data);

    XtVaGetValues(w, XmNuserData, &help_popup, NULL);
    TRACE_GUI((stderr, "GOT help_popup: %p", (void *)help_popup));

    if (info != NULL)
	browser_combo = info->combo_box;
    
    if (browser_combo != NULL) {
#if USE_COMBOBOX
	/* Need to unselect the `Other...' entry from the list; to do this,
	   we select the index from XmNuserData if it's >= 0, or the first item. */
	int idx;
	XtPointer p;
	XtVaGetValues(browser_combo,
		      XmNlist, &browser_list_w,
		      XmNuserData, &p,
		      NULL);
	idx = (int)p;
	if (browser_list_w == 0)
	    XDVI_ERROR((stderr, "couldn't get list component of combo box!\n"));
	else {
	    if (idx < 0)
		idx = 0;
	    XmListSelectPos(browser_list_w, idx + 1, True);
	}
#else
	Widget parent;
	if ((parent = XtParent(w)) != NULL) {
	    Widget rowcol, child;
	    if (strcmp(XtName(parent), Xdvi_BROWSER_POPUP) == 0) {
		if (get_widget_by_name(&rowcol, globals.widgets.top_level, Xdvi_BROWSER_COMBO_NAME, True)) {
		    XtVaGetValues(rowcol, XmNuserData, &child, NULL);
		    XtVaSetValues(rowcol, XmNmenuHistory, child, NULL);
		}
	    }
	    else if (strcmp(XtName(parent), Xdvi_EDITOR_POPUP) == 0) {
		if (get_widget_by_name(&rowcol, globals.widgets.top_level, Xdvi_EDITOR_COMBO_NAME, True)) {
		    XtVaGetValues(rowcol, XmNuserData, &child, NULL);
		    XtVaSetValues(rowcol, XmNmenuHistory, child, NULL);
		}
	    }
	    else {
		XDVI_WARNING((stderr, "unexpected widget name `%s' in destroy_dialog_cb", XtName(parent)));
	    }
	}
#endif
    }

    /*
     * Also popdown dependent help window if it exists before destroying the widget;
     * otherwise, Motif may even crash in _XmIsFastSubclass() when the help window is moved!
     * 
     * Since `help_popup' is the top-level xmDialogShellWidget, we need to get its
     * xmMessageBoxWidget child (by name ...). Don't report an error if it doesn't
     * exist in case the help window has already been closed.
     */
    if (help_popup != NULL) {
	static Widget message;
	if (get_widget_by_name(&message, help_popup, Xdvi_MESSAGE_DIALOG_NAME, False))
	    XtCallCallbacks(message, XmNokCallback, NULL);
    }

    if (info != NULL) {
	struct prefs_choice *prefs = (struct prefs_choice *)info->tinfo->data;
	remove_from_deplist(prefs, w);
	free(info);
    }
    
    /* destroy the parent of this dialog (the shell) */
    XtDestroyWidget(XtParent(w));
}

static void
h_get_input_wrapper(const char *listbox_name,
		    Widget w, XtPointer client_data, XtPointer call_data)
{
    /*     Widget combobox = (Widget)client_data; */
    struct choice_dialog_info *info = (struct choice_dialog_info *)client_data;
    struct topic_info *tinfo = info->tinfo;
    struct prefs_choice *prefs = (struct prefs_choice *)tinfo->data;
    Widget combobox, child;
    /* following need to be allocated since we want to set resource.xyz to it */
    static char *browser_choice= NULL, *editor_choice = NULL;
#if !USE_COMBOBOX
    XtCallbackProc select_cb = NULL;
#endif
    XmSelectionBoxCallbackStruct *cbs = (XmSelectionBoxCallbackStruct *)call_data;
    static char *ptr = NULL;
    char *tmp_list;

    if (call_data == NULL) /* widget already being destroyed? */
	return;

    ASSERT(info != NULL, "client_data in h_get_input_wrapper mustn't be NULL!");
    combobox = info->combo_box;
    child = info->message_popup;
    XtVaGetValues(w, XmNuserData, &child, NULL);
    TRACE_GUI((stderr, "GOT child: %p", (void *)child));

    if (ptr)
	XtFree((XtPointer)ptr);
    ptr = NULL;
    XmStringGetLtoR(cbs->value, G_charset, &ptr);
    if (strlen(ptr) == 0 || is_spaces_only(ptr)) {
	popup_message(XtParent(w),
		      MSG_ERR,
		      NULL,
		      "Empty input string");
	return;
    }

    if (strcmp(listbox_name, Xdvi_BROWSER_COMBO_NAME) == 0) {
#if 0
	int i;
#endif
	
	free(browser_choice);
	browser_choice = xstrdup(ptr);
	resource.browser = browser_choice;

#if 0
	for (i = 0; m_browser_list[i] != NULL; i++) {
	    fprintf(stderr, "list %d: |%s|\n", i, m_browser_list[i]);
	}
#endif
	m_browser_list = string_list_prepend(m_browser_list, ptr);
#if 0
	for (i = 0; m_browser_list[i] != NULL; i++) {
	    fprintf(stderr, "NEW list %d: |%s|\n", i, m_browser_list[i]);
	}
#endif
	tmp_list = string_list_to_str(m_browser_list, "\n");
#if 0
	fprintf(stderr, "TMP LIST: |%s|\n", tmp_list);
#endif /* 0 */
	store_preference(&(prefs->db), "prefsBrowserList", "%s", tmp_list);
	free(tmp_list);
	
	store_preference(&(prefs->db), "wwwBrowser", "%s", ptr);
#if !USE_COMBOBOX
	select_cb = select_browser_cb;
#endif
    }
    else if (strcmp(listbox_name, Xdvi_EDITOR_COMBO_NAME) == 0) {
	free(editor_choice);
	editor_choice = xstrdup(ptr);
	resource.editor = editor_choice;

	m_editor_list = string_list_prepend(m_editor_list, ptr);
	tmp_list = string_list_to_str(m_editor_list, "\n");
	store_preference(&(prefs->db), "prefsEditorList", "%s", tmp_list);
	free(tmp_list);
	
	store_preference(&(prefs->db), "editor", "%s", ptr);
#if !USE_COMBOBOX
	select_cb = select_editor_cb;
#endif
    }
    else
	XDVI_WARNING((stderr, "Unexpected name in h_get_input_wrapper: `%s'", listbox_name));

#if USE_COMBOBOX
    UNUSED(listbox_name);
    /* add user input to the combo box list, and make it current */
    XmComboBoxAddItem(combobox, cbs->value, 1, True);
    XmComboBoxSelectItem(combobox, cbs->value);
    {   /* if more than 9 items, add scrollbar */
	size_t n;
	XtVaGetValues(combobox, XmNitemCount, &n, NULL);
	XtVaSetValues(combobox, XmNvisibleItemCount, n > 10 ? 10 : n, NULL);
    }
#else
    {
	Widget new_menu, parent, grandparent, rowcol;
	if ((parent = XtParent(w)) != NULL && (grandparent = XtParent(parent)) != NULL) {
	    /* add new item to front of list ... */
	    new_menu = XtVaCreateManagedWidget(ptr, xmPushButtonGadgetClass, grandparent,
					       XmNpositionIndex, 0,
					       XmNuserData, tinfo,
					       NULL);
	    if (select_cb != NULL) {
		XtAddCallback(new_menu, XmNactivateCallback, select_cb, grandparent);
	    }
	    /* ... and make it current! */
	    if (get_widget_by_name(&rowcol, globals.widgets.top_level, listbox_name, True)) {
		XtVaSetValues(rowcol, XmNmenuHistory, new_menu, NULL);
	    }
	}
    }
#endif

    /* as above: popdown help window */
    if (child != NULL) {
	Widget message;
	if (get_widget_by_name(&message, child, Xdvi_MESSAGE_DIALOG_NAME, False))
	    XtCallCallbacks(message, XmNokCallback, NULL);
    }

    remove_from_deplist(prefs, w);
    free(info);
    
    /* destroy the parent of this dialog (the shell) */
    XtDestroyWidget(XtParent(w));
}

static void
h_selector(const char *prompt_name,
	   const char *title_str, const char *label_str,
	   XtCallbackProc ok_cb, 
	   XtCallbackProc destroy_cb, 
	   XtCallbackProc help_cb, 
	   Widget w, XtPointer client_data, XtPointer call_data)
{
    struct topic_info *tinfo = NULL;
    struct prefs_choice *prefs = NULL;
    struct choice_dialog_info *info = NULL;
    char *choice;
    /* following need to be allocated since we want to set resource.xyz to it */
    static char *browser_choice= NULL, *editor_choice = NULL;

#if USE_COMBOBOX
    XmComboBoxCallbackStruct *cb;
#else
    XmString str;
    UNUSED(client_data);
    UNUSED(call_data);
#endif
    
#if USE_COMBOBOX
    tinfo = (struct topic_info *)client_data;
    prefs = (struct prefs_choice *)tinfo->data;
#endif
    info = xmalloc(sizeof *info);
#if USE_COMBOBOX
    cb = (XmComboBoxCallbackStruct *)call_data;

    if (cb->event == NULL) /* only browsing, no selection */
	return;
    
    choice = (char *)XmStringUnparse(cb->item_or_text, XmFONTLIST_DEFAULT_TAG,
				     XmCHARSET_TEXT, XmCHARSET_TEXT,
				     NULL, 0,
				     XmOUTPUT_ALL);
#else
    XtVaGetValues(w, XmNlabelString, &str, NULL);
    XmStringGetLtoR(str, G_charset, &choice);
    
    XtVaGetValues(w, XmNuserData, &tinfo, NULL);
    ASSERT(tinfo != NULL, "XmNuserData in callback musn't be NULL!");
    prefs = (struct prefs_choice *)tinfo->data;
#endif

    if (strcmp(choice, Xdvi_ADD_COMMAND_STR) == 0) {
	Widget prompt_widget;
	Arg args[8];
	int n = 0;
	XmString title = XmStringCreateLocalized((char *)title_str);
	XmString label = XmStringCreateLocalized((char *)label_str);
	
	XtSetArg(args[n], XmNselectionLabelString, label); n++;
	XtSetArg(args[n], XmNautoUnmanage, False); n++;
	XtSetArg(args[n], XmNdialogTitle, title); n++;
	XtSetArg(args[n], XmNuserData, NULL); n++;
	prompt_widget = XmCreatePromptDialog(
#if USE_COMBOBOX
					     w,
#else
					     XtParent(w),
#endif
					     (char *)prompt_name, args, n);
	/* 	XmStringFree(label); */
	
	add_to_deplist(prefs, prompt_widget);
	
	info->tinfo = tinfo;
	info->combo_box = w;
	info->message_popup = NULL;
	
	/* Note: w is the browser_combo widget */
	XtAddCallback(prompt_widget, XmNokCallback, ok_cb, (XtPointer)info);
	XtAddCallback(prompt_widget, XmNcancelCallback, destroy_cb, (XtPointer)info);
	XtAddCallback(prompt_widget, XmNhelpCallback, help_cb, (XtPointer)info);
	
	XtManageChild(prompt_widget);
    }
    else { /* normal item */
#if USE_COMBOBOX
	int i;
#endif

	if (strcmp(prompt_name, Xdvi_BROWSER_POPUP_NAME) == 0) {
#if !USE_COMBOBOX
	    Widget rowcol;
	    if (get_widget_by_name(&rowcol, globals.widgets.top_level, Xdvi_BROWSER_COMBO_NAME, True)) {
		XtVaSetValues(rowcol, XmNuserData, w, NULL);
	    }
#endif
	    free(browser_choice);
	    browser_choice = xstrdup(choice);
	    resource.browser = browser_choice;
	    store_preference(&(prefs->db), "wwwBrowser", "%s", browser_choice);
	}
	else if (strcmp(prompt_name, Xdvi_EDITOR_POPUP_NAME) == 0) {
#if !USE_COMBOBOX
	    Widget rowcol;
	    if (get_widget_by_name(&rowcol, globals.widgets.top_level, Xdvi_EDITOR_COMBO_NAME, True)) {
		XtVaSetValues(rowcol, XmNuserData, w, NULL);
	    }
#endif
	    free(editor_choice);
	    editor_choice = xstrdup(choice);
	    resource.editor = editor_choice;
	    store_preference(&(prefs->db), "editor", "%s", editor_choice);
	}
	else
	    XDVI_ERROR((stderr, "Unknown category `%s' in h_selector()!", prompt_name));


#if USE_COMBOBOX
	/* update the currently selected value */
	XtVaGetValues(w, XmNselectedPosition, &i, NULL);
	XtVaSetValues(w, XmNuserData, cast_int_to_XtPointer(i), NULL);
#endif
    }
#if USE_COMBOBOX
    XtFree(choice);
#endif
}

/*
 * User OK'ed browser text input prompt
 */
static void
get_browser_text_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
    if (XtIsRealized(w)) {
	h_get_input_wrapper(Xdvi_BROWSER_COMBO_NAME, w, client_data, call_data);
    }
}


/*
 * User OK'ed editor text input prompt
 */
static void
get_editor_text_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
    if (XtIsRealized(w)) {
	h_get_input_wrapper(Xdvi_EDITOR_COMBO_NAME, w, client_data, call_data);
    }
}


/*
 * User selected an item from browser combo box pulldown list
 */
static void
select_browser_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
    h_selector(Xdvi_BROWSER_POPUP_NAME,
	       "xdvik: Add Browser Command",
	       "Browser Command (optional `%s' is replaced by URL): ",
	       get_browser_text_cb, destroy_dialog_cb, help_browser_dialog_cb,
	       w, client_data, call_data);
}

/*
 * User selected an item from editor combo box pulldown list
 */
static void
select_editor_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
    UNUSED(client_data);
    h_selector(Xdvi_EDITOR_POPUP_NAME,
	       "xdvik: Add Editor Command",
	       "Editor Command (optional: `%l' = line number, `%f' = file name): ",
	       get_editor_text_cb, destroy_dialog_cb, help_editor_dialog_cb,
	       w, client_data, call_data);
}

void
update_preferences_helpers(void)
{
    Widget shell, browser_cascade, editor_cascade;

    if (get_widget_by_name(&shell, globals.widgets.top_level, Xdvi_PREFS_DIALOG_NAME, False)
	&& get_widget_by_name(&browser_cascade, shell, Xdvi_BROWSER_COMBO_NAME, True)
	&& get_widget_by_name(&editor_cascade, shell, Xdvi_EDITOR_COMBO_NAME, True)) {

#if USE_COMBOBOX

	Widget browser_list_w, editor_list_w;
	XmString str;
	int i;
	Boolean found = False;
	
	XtVaGetValues(browser_cascade, XmNlist, &browser_list_w, NULL);
	XtVaGetValues(editor_cascade, XmNlist, &editor_list_w, NULL);

	for (i = 0; m_browser_list[i] != NULL; i++) {
	    if (globals.curr_browser != NULL) { /* if this is set, ignore browser setting */
		if (strcmp(globals.curr_browser, m_browser_list[i]) == 0) {
		    found = True;
		    break;
		}
	    }
	    else if (resource.browser != NULL) {
		if (strcmp(resource.browser, m_browser_list[i]) == 0) {
		    found = True;
		    break;
		}
	    }
	}
	if (!found)
	    i = 0;
	str = XmStringCreateLtoR((char *)m_browser_list[i], "UNMARKED");
	XmComboBoxSelectItem(browser_cascade, str);
	XmStringFree(str);
	
	for (i = 0; m_editor_list[i] != NULL; i++) {
	    if (globals.curr_editor != NULL) { /* if this is set, ignore editor setting */
		if (strcmp(globals.curr_editor, m_editor_list[i]) == 0) {
		    found = True;
		    break;
		}
	    }
	    else if (resource.editor != NULL) {
		if (strcmp(resource.editor, m_editor_list[i]) == 0) {
		    found = True;
		    break;
		}
	    }
	}
	if (!found)
	    i = 0;
	str = XmStringCreateLtoR((char *)m_editor_list[i], "UNMARKED");
	XmComboBoxSelectItem(editor_cascade, str);
	XmStringFree(str);
	/* 	XmListSetPos(editor_list_w, i); */
	/* 	fprintf(stderr, "setting editor list index %d, %s\n", */
	/* 		i, editor_list[i-1]); */

	
	/* 	if (XmListGetSelectedPos(browser_list_w, &browser_items, &browser_cnt) */
	/* 	    && XmListGetSelectedPos(editor_list_w, &editor_items, &editor_cnt)) { */
	/* 	    if (browser_cnt > 0 && editor_cnt > 0) { */
	/* 		int browser_idx = browser_items[0] - 1; */
	/* 		int editor_idx = editor_items[0] - 1; */
	/* 		fprintf(stderr, "selected: %s, %s\n", */
	/* 			browser_list[browser_idx], */
	/* 			editor_list[editor_idx]); */
	/* 	    } */
	/* 	    else { */
	/* 		XDVI_WARNING((stderr, "Shouldn't happen: No items selected in browser list?")); */
	/* 		return; */
	/* 	    } */
	/* 	    XtFree((XtPointer)browser_items); */
	/* 	    XtFree((XtPointer)editor_items); */
	/* 	} */
#else
	Widget browser_menu, editor_menu;
	if (get_widget_by_name(&browser_menu, shell, Xdvi_BROWSER_MENU_NAME, True)
	    && get_widget_by_name(&editor_menu, shell, Xdvi_EDITOR_MENU_NAME, True)) {
	    int i, num_buttons;
	    WidgetList buttons;
	    
	    XtVaGetValues(browser_menu,
			  XmNnumChildren, &num_buttons,
			  XmNchildren, &buttons,
			  NULL);
	    for (i = 0; i < num_buttons; i++) {
		XmString str;
		char *ptr;
		
		XtVaGetValues(buttons[i], XmNlabelString, &str, NULL);
		XmStringGetLtoR(str, G_charset, &ptr);
		/* 		fprintf(stderr, "kid %d: %s\n", i, ptr); */
		if (globals.curr_browser != NULL) { /* if this is set, ignore browser setting */
		    if (strcmp(globals.curr_browser, ptr) == 0) {
			break;
		    }
		}
		else if (resource.browser != NULL) {
		    if (strcmp(resource.browser, ptr) == 0) {
			break;
		    }
		}
	    }
	    if (i >= num_buttons) /* not found */
		i = 0;
	    XtVaSetValues(browser_cascade, XmNmenuHistory, buttons[i], NULL);

	    /* same for editor */
	    XtVaGetValues(editor_menu,
			  XmNnumChildren, &num_buttons,
			  XmNchildren, &buttons, NULL);
	    for (i = 0; i < num_buttons; i++) {
		XmString str;
		char *ptr;
		
		XtVaGetValues(buttons[i], XmNlabelString, &str, NULL);
		XmStringGetLtoR(str, G_charset, &ptr);
		/* 		fprintf(stderr, "kid %d: %s\n", i, ptr); */
		if (globals.curr_editor != NULL) { /* if this is set, ignore editor setting */
		    if (strcmp(globals.curr_editor, ptr) == 0) {
			break;
		    }
		}
		else if (resource.editor != NULL) {
		    if (strcmp(resource.editor, ptr) == 0) {
			break;
		    }
		}
	    }
	    if (i >= num_buttons) /* not found */
		i = 0;
	    XtVaSetValues(editor_cascade, XmNmenuHistory, buttons[i], NULL);

	}
#endif
        /* for browser/editor, try:

	Widget      menu;
	int         num_buttons;
	WidgetList  buttons;
       
	XtVaGetValues( simple_option_widget, XmNsubMenuId, &menu, NULL);
       
	XtVaGetValues( menu, XmNnumChildren, &num_buttons,
	XmNchildren, &buttons, NULL ) ;
	
	*/
    }
}

static Widget
h_create_command(const char *name,
		 const char *menu_name,
		 const char *label,
		 const char *curr_value,
		 Widget parent, Widget top,
		 char **command_list,
		 XtCallbackProc select_cb,
		 struct topic_info *tinfo)
{
    const char *ptr = NULL;
    size_t i;

#if USE_COMBOBOX
    XmStringTable str_list;
    Widget text_label, combo_box;
    size_t k, num;
    int curr_index = -1;
#else
    Widget menu, cascade, item;
    XmString str;
    Arg args[8];
    int n;
#endif
    
    /* check if we need to add the default resource name */
    ptr = curr_value;

    /* don't add it if it's already in our list */
    for (i = 0; ptr != NULL && command_list[i] != NULL; i++) {
	if (strcmp(command_list[i], ptr) == 0) {
	    ptr = NULL;
	}
    }
#if USE_COMBOBOX
    UNUSED(menu_name);
    text_label = XmCreateLabelGadget(parent, (char *)label, NULL, 0);
    XtVaSetValues(text_label,
		  XmNtopAttachment, XmATTACH_WIDGET,
		  XmNtopWidget, top,
		  XmNleftAttachment, XmATTACH_FORM,
		  /* 		  XmNrightAttachment, XmATTACH_FORM, */
		  NULL);

    /* count elements in command_list */
    for (num = 0; command_list[num] != NULL; num++) { ; }

    if (ptr != NULL)
	num++;
	
    str_list = (XmStringTable)XtMalloc(num * sizeof(XmString *));

    i = 0;
    if (ptr != NULL)
	str_list[i++] = XmStringCreateLtoR((char *)ptr, "UNMARKED");
    
    for (k = 0; i < num; i++, k++) {
	if (curr_value != NULL && strcmp(command_list[k], curr_value) == 0) {
	    curr_index = i;
	}
	if (strcmp(command_list[k], Xdvi_ADD_COMMAND_STR) == 0)
	    str_list[i] = XmStringCreateLtoR((char *)command_list[k], "MARKED");
	else
	    str_list[i] = XmStringCreateLtoR((char *)command_list[k], "UNMARKED");
    }
    combo_box = XtVaCreateWidget(name, xmComboBoxWidgetClass,
				 parent,
				 XmNtopAttachment, XmATTACH_WIDGET,
				 XmNtopWidget, top, /* if top == NULL, this uses XmATTACH_FORM */
				 XmNleftAttachment, XmATTACH_WIDGET,
				 XmNleftWidget, text_label,
				 XmNrightAttachment, XmATTACH_FORM,
				 XmNcomboBoxType, XmDROP_DOWN_LIST,
				 XmNitems, str_list,
				 XmNitemCount, num,
				 XmNvisibleItemCount, num > 10 ? 10 : num,
				 XmNuserData, cast_int_to_XtPointer(curr_index),
				 XmNarrowSize, Xdvi_COMBO_BOX_ARROW_SIZE,
				 NULL);
    if (top != NULL) /* FIXME: This assumes we only have 2 items ... */
	XtVaSetValues(combo_box,
		      XmNbottomAttachment, XmATTACH_FORM,
		      NULL);
    
    /* make resource setting current value */
    if (curr_index >= 0) {
	XmComboBoxSelectItem(combo_box, str_list[curr_index]);
    }
	
    for (i = 0; i < num; i++)
	XmStringFree(str_list[i]);
    XtFree((XtPointer)str_list);

    XtAddCallback(combo_box, XmNselectionCallback, select_cb, (XtPointer)tinfo); 
    
    /*
     * workaround for pointer grabbing bug (see xm_menu.c). We need to use
     * the popdownCallback of the internal `GrabShell' child of the combo box.
     */
    {
	Widget grab_shell;
	if (get_widget_by_name(&grab_shell, combo_box, "GrabShell", True))
	    XtAddCallback(grab_shell, XtNpopdownCallback, popdown_callback, NULL);
    }
    adjust_heights(text_label, combo_box, NULL);

    XtManageChild(text_label);
    XtManageChild(combo_box);

    return combo_box;
#else
    menu = XmCreatePulldownMenu(parent, (char *)menu_name, NULL, 0);
    /*     XtVaSetValues(menu, */
    /* 		  XmNtopAttachment, XmATTACH_WIDGET, */
    /* 		  XmNtopWidget, top, */
    /* 		  XmNleftAttachment, XmATTACH_FORM, */
    /* 		  XmNrightAttachment, XmATTACH_FORM, */
    /* 		  NULL); */

    str = XmStringCreateLocalized((char *)label);
    n = 0;
    XtSetArg(args[n], XmNsubMenuId, menu); n++;
    XtSetArg(args[n], XmNlabelString, str); n++;
    XtSetArg(args[n], XmNuserData, NULL); n++;
    cascade = XmCreateOptionMenu(parent, (char *)name, args, n);
    if (top)
	XtVaSetValues(cascade,
		      XmNtopAttachment, XmATTACH_WIDGET,
		      XmNtopWidget, top,
		      XmNleftAttachment, XmATTACH_FORM,
		      NULL);
    else
	XtVaSetValues(cascade,
		      XmNtopAttachment, XmATTACH_FORM,
		      XmNleftAttachment, XmATTACH_FORM,
		      NULL);
    XmStringFree(str);

    if (ptr != NULL) {
	item = XtVaCreateManagedWidget(ptr, xmPushButtonGadgetClass, menu,
				       XmNuserData, tinfo,
				       NULL);
	XtAddCallback(item, XmNactivateCallback, select_cb, menu); 
    }
    
    for (i = 0; command_list[i] != NULL; i++) {
	item = XtVaCreateManagedWidget(command_list[i], xmPushButtonGadgetClass, menu,
				       XmNuserData, tinfo,
				       NULL);
	XtAddCallback(item, XmNactivateCallback, select_cb, menu); 
    }
    XtManageChild(cascade);
    return cascade;
#endif
}

Widget
prefs_helpers(struct topic_info *tinfo)
{
    /*     struct prefs_choice *prefs = (struct prefs_choice *)info->data; */
    Widget form, /* frame, */ form1;
    Widget browser_command, editor_command;

    form = XmCreateForm(tinfo->right_form, "helpers_form", NULL, 0);

    /*     frame = XmCreateFrame(form, "commands_frame", NULL, 0); */

    form1 = XtVaCreateWidget("commands_form", xmFormWidgetClass,
			     form,
			     XmNverticalSpacing, 10,
			     XmNhorizontalSpacing, 0,
			     NULL);

    if (m_browser_list == NULL)
	m_browser_list = get_separated_list(resource.prefs_browser_list, "\n", False);
    
    if (m_editor_list == NULL)
	m_editor_list = get_separated_list(resource.prefs_editor_list, "\n", False);
    
    browser_command = h_create_command(Xdvi_BROWSER_COMBO_NAME,
				       Xdvi_BROWSER_MENU_NAME,
				       "Web Browser: ",
				       resource.browser,
				       form1, NULL,
				       m_browser_list,
				       select_browser_cb, tinfo);
    
    editor_command = h_create_command(Xdvi_EDITOR_COMBO_NAME,
				      Xdvi_EDITOR_MENU_NAME,
				      "Editor for Source Specials: ",
				      resource.editor,
				      form1, browser_command,
				      m_editor_list,
				      select_editor_cb, tinfo);
    
    /* #if PS_GS */
    /*     Widget b2 = XmCreateLabelGadget(rowcol, "[x] Use Ghostscript to interpret PS specials", NULL, 0); */

    /* TODO: don't need this??? Similar: ps2pdf, dvips conversion?? */
    /*     Widget b3 = XmCreateLabelGadget(rowcol, "Path to Ghostscript: ____________", NULL, 0); */
    
    /* #endif */
    /*     Widget b4 = XmCreateLabelGadget(rowcol, "Editor for source specials: <pulldown> [Other ...]", NULL, 0); */
    /*     XtManageChild(b1); */
    /*     XtManageChild(b2); */
    /*     XtManageChild(b3); */
    /*     XtManageChild(b4); */
    /*     XtManageChild(rowcol); */

    XtManageChild(form1);
    /*     XtManageChild(frame); */

    return form;
}

#else
/* silence `empty compilation unit' warnings */
static void bar(void); static void foo() { bar(); } static void bar(void) { foo(); }
#endif /* MOTIF */