Debian Codegraph @

texk/xdvik/gui/xm_prefs.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 * Preferences dialog for xdvik.
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 "message-window.h"
35 #include "util.h"
36 #include "events.h"
37
38 #include "xm_prefsP.h"
39 #include "xm_prefs.h"
40
41 #include "xm_prefs_appearance.h"
42 #include "xm_prefs_fonts.h"
43 #include "xm_prefs_helpers.h"
44 #include "xm_prefs_page.h"
45 #include "xm_prefs_scroll.h"
46
47 /* for reverting preferences, we need access to more prototypes ... */
48 #include "dvi-draw.h"
49 #include "search-internal.h"
50 #include "statusline.h"
51 #include "xm_toolbar.h"
52 #include "xm_menu.h"
53 #include "pagesel.h"
54
55 #ifdef MOTIF /* entire file */
56
57 #include <X11/Xatom.h>
58
59 #include <Xm/Xm.h>
60 #include <Xm/Protocols.h>
61 #include <Xm/DialogS.h>
62 #include <Xm/Form.h>
63
64 #define SCROLLING_DONE 0
65
66 #define NUM_PREFS_TOPICS 16 /* should be ample ... */
67
68 /* hmm, should maybe move these into xm_prefsP.c ... */
69 void
70 h_attach_below(Widget x, Widget y) {
71 if (y == NULL) {
72 /* no top widget, attach to form - could just pass NULL as XmNtopWidget, but lesstif warns in that case ... */
73 XtVaSetValues(x,
74 XmNtopAttachment, XmATTACH_FORM,
75 XmNleftAttachment, XmATTACH_FORM,
76 XmNrightAttachment, XmATTACH_FORM,
77 NULL);
78 }
79 else {
80 XtVaSetValues(x,
81 XmNtopAttachment, XmATTACH_WIDGET,
82 XmNtopWidget, y,
83 XmNleftAttachment, XmATTACH_FORM,
84 XmNrightAttachment, XmATTACH_FORM,
85 NULL);
86 }
87 }
88
89
90 static void
91 initialize_items(struct topic_info *info)
92 {
93 int i = 0;
94
95 info->items[i].widget = prefs_appearance(info);
96 info->items[i].topic = xstrdup("Appearance");
97 info->items[i].title = xstrdup("Change the appearance of xdvi");
98
99 i++;
100 info->items[i].widget = prefs_fonts_and_colors(info);
101 info->items[i].topic = xstrdup("Fonts and Colors");
102 info->items[i].title = xstrdup("Display of fonts and colors in DVI files");
103
104 i++;
105 info->items[i].widget = prefs_paper(info);
106 info->items[i].topic = xstrdup("Page Size");
107 info->items[i].title = xstrdup("Customize the default window and page size");
108
109 #if SCROLLING_DONE /* not finished yet */
110 i++;
111 info->items[i].widget = prefs_scrolling(info);
112 info->items[i].topic = xstrdup("Scrolling");
113 info->items[i].title = xstrdup("Customize the scrolling behaviour when switching pages");
114 #endif
115
116 i++;
117 info->items[i].widget = prefs_helpers(info);
118 #ifdef LESSTIF_VERSION
119 /* compensate for width computation bug by adding extra whitespace at end */
120 info->items[i].topic = xstrdup("Helper Applications ");
121 #else
122 info->items[i].topic = xstrdup("Helper Applications");
123 #endif
124 info->items[i].title = xstrdup("External programs used by xdvi");
125
126 /* terminate */
127 i++;
128 info->items[i].widget = 0;
129 info->items[i].topic = info->items[i].title = NULL;
130
131 ASSERT(i < NUM_PREFS_TOPICS, "NUM_PREFS_TOPICS too small!");
132 }
133
134 void
135 remove_from_deplist(struct prefs_choice *prefs, Widget w)
136 {
137 size_t i;
138 Boolean found = False;
139
140 /* locate this widget */
141 for (i = 0; i < prefs->depwin_cnt; i++) {
142 if (prefs->depwin[i] == w) {
143 found = True;
144 break;
145 }
146 }
147
148 if (found) {
149 /* move later widgets down */
150 for (; i < prefs->depwin_cnt - 1; i++) {
151 prefs->depwin[i] = prefs->depwin[i + 1];
152 }
153 prefs->depwin_cnt--;
154 }
155
156 #if 0
157 fprintf(stderr, "deplist after removal:\n");
158 for (i = 0; i < prefs->depwin_cnt; i++) {
159 fprintf(stderr, "%d: %p\n", i, prefs->depwin[i]);
160 }
161 #endif
162 }
163
164 static void
165 update_button(Widget button, Pixel pix)
166 {
167 struct color_button_info *cinfo;
168 static XmDrawnButtonCallbackStruct cbs;
169
170 XtVaGetValues(button, XmNuserData, &cinfo, NULL);
171 cinfo->pixel = pix;
172 cbs.reason = XmCR_EXPOSE;
173 XtCallCallbacks(button, XmNexposeCallback, &cbs);
174 }
175
176 static void
177 update_button_by_name(Pixel pix, const char *name)
178 {
179 static Widget pref_shell = 0;
180 Widget button;
181
182 if (pref_shell == 0
183 && !get_widget_by_name(&pref_shell, globals.widgets.top_level, Xdvi_PREFS_DIALOG_NAME, True))
184 return;
185
186 if (get_widget_by_name(&button, pref_shell, name, True)) {
187 update_button(button, pix);
188 }
189 }
190
191 static Boolean
192 revert_colors(Pixel fg, Pixel bg, Pixel hl)
193 {
194 XColor color_data[2];
195 Boolean need_redraw = False;
196 Widget button;
197 Pixel visited, unvisited;
198
199 color_data[0].pixel = resource.fore_Pixel;
200 color_data[1].pixel = resource.back_Pixel;
201
202 str_to_pixel(globals.widgets.top_level, resource.visited_link_color, &visited);
203 str_to_pixel(globals.widgets.top_level, resource.link_color, &unvisited);
204
205 XQueryColors(DISP, G_colormap, color_data, 2);
206
207 if (fg != resource.fore_Pixel) {
208 XGCValues values;
209
210 fg_initial.r = color_data[0].red;
211 fg_initial.g = color_data[0].green;
212 fg_initial.b = color_data[0].blue;
213
214 values.foreground = resource.rule_pixel = resource.fore_Pixel;
215 XChangeGC(DISP, globals.gc.ruler, GCForeground, &values);
216
217 scanned_page = scanned_page_color = scanned_page_reset;
218
219 update_button_by_name(resource.fore_Pixel, Xdvi_FG_COLOR_BTN);
220
221 need_redraw = True;
222 }
223 if (bg != resource.back_Pixel) {
224 bg_initial.r = color_data[1].red;
225 bg_initial.g = color_data[1].green;
226 bg_initial.b = color_data[1].blue;
227
228 scanned_page = scanned_page_color = scanned_page_reset;
229
230 update_button_by_name(resource.back_Pixel, Xdvi_BG_COLOR_BTN);
231
232 need_redraw = True;
233 }
234 if (hl != resource.hl_Pixel) {
235 XGCValues values;
236 values.foreground = resource.hl_Pixel;
237 XChangeGC(DISP, globals.gc.high, GCForeground, &values);
238 /* hack to update match GC: fake change in inverted property, redraw
239 so that GC is cleared, then change inverted property back */
240 resource.match_highlight_inverted = !resource.match_highlight_inverted;
241 search_draw_inverted_regions();
242 resource.match_highlight_inverted = !resource.match_highlight_inverted;
243
244 update_button_by_name(resource.hl_Pixel, Xdvi_HL_COLOR_BTN);
245
246 need_redraw = True;
247 }
248 /* NOTE: pixels for hyperlinks are not stored anywhere; just update always: */
249 if (get_widget_by_name(&button, globals.widgets.top_level, Xdvi_VISITED_LINKS_BTN, True)) {
250 update_button(button, visited);
251 h_update_hyperlinks(button, visited); /* this already triggers a redraw */
252 }
253 if (get_widget_by_name(&button, globals.widgets.top_level, Xdvi_UNVISITED_LINKS_BTN, True)) {
254 update_button(button, unvisited);
255 h_update_hyperlinks(button, unvisited); /* this already triggers a redraw */
256 }
257 return need_redraw;
258 }
259
260 static void
261 revert_resources(void)
262 {
263 Pixel curr_fg = resource.fore_Pixel;
264 Pixel curr_bg = resource.back_Pixel;
265 Pixel curr_hl = resource.hl_Pixel;
266 Boolean need_redraw = False;
267
268 /* save some old values */
269 int save_shrink = resource.shrinkfactor;
270
271 load_app_resources(True);
272
273 /* revert from saved values */
274 resource.use_color = globals.curr_use_color;
275 resource.gamma = globals.curr_gamma;
276 resource.paper = globals.curr_paper;
277 resource.shrinkfactor = save_shrink;
278 do_set_shrinkfactor(resource.shrinkfactor, True);
279
280 update_preferences_darkness();
281
282 /* revert expert mode */
283 if (resource.expert)
284 resource.expert_mode = XPRT_SHOW_NONE;
285 update_expert_mode();
286
287 toggle_statusline();
288 #ifndef MOTIF
289 if (!BROKEN_RECONFIG)
290 toggle_scrollbars();
291 #else
292 toggle_scrollbars();
293 #endif
294
295 #ifdef MOTIF
296 toggle_pagelist();
297 toggle_toolbar();
298 toggle_menubar();
299 #else
300 toggle_buttons();
301 #endif
302
303 /* reset tooltips_wait_period, similar to TipAddWidget() in Tip.c */
304 resource.tooltips_wait_period = resource.tooltips_wait_period_bak;
305 if (resource.tooltips_wait_period < 0) {
306 resource.show_tooltips = False;
307 }
308 else if (!resource.show_tooltips) {
309 if (resource.tooltips_wait_period == 0)
310 resource.tooltips_wait_period = -1;
311 else
312 resource.tooltips_wait_period = -resource.tooltips_wait_period;
313 }
314
315 update_preferences_expert();
316 update_preferences_tooltips();
317 update_preferences_search();
318
319 update_preferences_color();
320 update_preferences_hyperlinks();
321
322 update_preferences_windowsize();
323 update_preferences_shrink();
324 update_preferences_paper();
325
326 update_preferences_helpers();
327
328 need_redraw |= revert_colors(curr_fg, curr_bg, curr_hl);
329
330 /* if (need_redraw) */
331 /* just reload it always, there's too many exceptions ...
332 redraw isn't always sufficient if file has colors. */
333 globals.ev.flags |= EV_RELOAD;
334
335 }
336
337 /* add widget w to list of dependent windows */
338 void
339 add_to_deplist(struct prefs_choice *prefs, Widget w)
340 {
341 #if 0
342 size_t i;
343 #endif
344 prefs->depwin = xrealloc(prefs->depwin,
345 (prefs->depwin_cnt + 1) * sizeof *(prefs->depwin));
346 prefs->depwin[prefs->depwin_cnt] = w;
347 prefs->depwin_cnt++;
348
349 #if 0
350 fprintf(stderr, "deplist after adding:\n");
351 for (i = 0; i < prefs->depwin_cnt; i++) {
352 fprintf(stderr, "%lu: %p\n", (unsigned long)i, prefs->depwin[i]);
353 }
354 #endif
355 }
356
357 static void
358 reread_prefs_cb(Window w)
359 {
360 if (w != XtWindow(globals.widgets.top_level)) {
361 XChangeProperty(DISP, w,
362 atom_reread_prefs(), atom_reread_prefs(), 8,
363 PropModeReplace,
364 /* dummy values, since all the other instance needs to do is
365 reread the ~/.xdvirc.tmp file */
366 (unsigned char *)"Y", 1);
367 }
368 }
369
370 static void
371 apply_prefs_cb(XtPointer arg)
372 {
373 struct topic_info *info = (struct topic_info *)arg;
374 struct prefs_choice *prefs = (struct prefs_choice *)info->data;
375 size_t i;
376 Widget colorsel;
377
378 if (get_widget_by_name(&colorsel, globals.widgets.top_level, Xdvi_COLOR_DIALOG_NAME, False)) {
379 XtPopdown(XtParent(colorsel));
380 }
381
382 /* pop down dependent windows */
383 TRACE_GUI((stderr, "window count: %lu\n", (unsigned long)prefs->depwin_cnt));
384 for (i = 0; i < prefs->depwin_cnt; i++) {
385 TRACE_GUI((stderr, "popping down %lu: %p", (unsigned long)i, (void *)(prefs->depwin[i])));
386 if (XtIsRealized(prefs->depwin[i])) {
387 XtCallCallbacks(prefs->depwin[i], XmNcancelCallback, NULL);
388 XSync(DISP, True); /* wait for server to catch up */
389 if (XtIsRealized(prefs->depwin[i])) {
390 TRACE_GUI((stderr, "calling XmNokCallback of %lu: %p", (unsigned long)i, (void *)(prefs->depwin[i])));
391 XtCallCallbacks(prefs->depwin[i], XmNokCallback, NULL);
392 }
393 }
394 }
395 free(prefs->depwin);
396 prefs->depwin = NULL;
397 prefs->depwin_cnt = 0;
398
399 if (prefs->db == NULL) /* callback invoked multiple times? */
400 return;
401
402 merge_into_user_db(prefs->db); /* this destroys prefs->db */
403 prefs->db = NULL;
404
405 /* remember some current values */
406 free(globals.curr_paper);
407 if (resource.paper != NULL)
408 globals.curr_paper = xstrdup(resource.paper);
409
410 free(globals.curr_editor);
411 if (resource.editor != NULL)
412 globals.curr_editor = xstrdup(resource.editor);
413
414 free(globals.curr_browser);
415 if (resource.browser != NULL)
416 globals.curr_browser = xstrdup(resource.browser);
417
418 /* fprintf(stderr, "set curr_browser to: |%s|\n", globals.curr_browser); */
419 /* fprintf(stderr, "set curr_editor to: |%s|\n", globals.curr_editor); */
420
421 if (get_xdvi_window_id(False, NULL) && save_user_preferences(False)) {
422 /* if other instances of xdvi are running, make them reread the
423 changed preferences by writing them to ~/.xdvirc.tmp and having
424 them read that file; otherwise they would overwrite the file if
425 user quits them after the current instance.
426 */
427 get_xdvi_window_id(False, reread_prefs_cb);
428 }
429 }
430
431 static void
432 revert_prefs_cb(XtPointer arg)
433 {
434 struct topic_info *info = (struct topic_info *)arg;
435 struct prefs_choice *prefs = (struct prefs_choice *)info->data;
436 size_t i;
437
438 /* pop down dependent windows */
439 TRACE_GUI((stderr, "window count: %lu", (unsigned long)prefs->depwin_cnt));
440 for (i = 0; i < prefs->depwin_cnt; i++) {
441 TRACE_GUI((stderr, "popping down %lu: %p", (unsigned long)i, (void *)(prefs->depwin[i])));
442 if (XtIsRealized(prefs->depwin[i])) {
443 XtCallCallbacks(prefs->depwin[i], XmNcancelCallback, NULL);
444 XSync(DISP, True); /* wait for server to catch up */
445 if (XtIsRealized(prefs->depwin[i])) {
446 TRACE_GUI((stderr, "calling XmNokCallback of %lu: %p", (unsigned long)i, (void *)(prefs->depwin[i])));
447 XtCallCallbacks(prefs->depwin[i], XmNokCallback, NULL);
448 }
449 }
450 }
451 free(prefs->depwin);
452 prefs->depwin = NULL;
453 prefs->depwin_cnt = 0;
454
455 if (prefs->db == NULL) { /* callback invoked multiple times, or prefs not changed */
456 return;
457 }
458
459 revert_resources();
460
461 XrmDestroyDatabase(prefs->db);
462 prefs->db = NULL;
463 }
464
465 static void
466 save_prefs_exit(XtPointer arg)
467 {
468 struct topic_info *info = (struct topic_info *)arg;
469 apply_prefs_cb(info);
470 XtPopdown(info->shell);
471 XSync(DISP, False);
472 xdvi_exit(EXIT_SUCCESS);
473 }
474
475 static void
476 no_save_prefs_exit(XtPointer arg)
477 {
478 struct topic_info *info = (struct topic_info *)arg;
479 revert_prefs_cb(info);
480 XtPopdown(info->shell);
481 XSync(DISP, False);
482 xdvi_exit(EXIT_SUCCESS);
483 }
484
485 static void
486 close_prefs_exit(Widget w, XtPointer arg)
487 {
488 struct topic_info *info = (struct topic_info *)arg;
489 struct prefs_choice *prefs = (struct prefs_choice *)info->data;
490 /* Widget dialog; */
491 /* if (get_widget_by_name(&dialog, w, Xdvi_MESSAGE_DIALOG_NAME, True)) { */
492 /* fprintf(stderr, "!!!!!!!!! removing window from deplist!\n"); */
493 /* remove_from_deplist(prefs, dialog); */
494 /* } */
495
496 remove_from_deplist(prefs, w);
497
498 }
499
500 Boolean
501 preferences_changed(void)
502 {
503 Widget prefs_shell = 0, topic_pane = 0;
504
505 if (get_widget_by_name(&prefs_shell, globals.widgets.top_level, "preferences_window", False)
506 && get_widget_by_name(&topic_pane, prefs_shell, "topic_pane", False)) {
507 struct topic_info *info = NULL;
508 struct prefs_choice *prefs = NULL;
509
510 XtVaGetValues(topic_pane, XmNuserData, &info, NULL);
511 if (info == NULL) {
512 return False;
513 }
514 prefs = (struct prefs_choice *)info->data;
515 if (prefs->db != NULL) { /* prefs changed */
516
517 Widget popup = choice3_dialog(prefs_shell,
518 MSG_QUESTION, NULL,
519 #ifndef MOTIF
520 NULL,
521 #endif
522 close_prefs_exit, info, /* pre_callbacks */
523 "Save and Exit", save_prefs_exit, info,
524 "Exit, don't Save", no_save_prefs_exit, info,
525 "Cancel", NULL, NULL,
526 "Preferences have been changed, but not saved yet. "
527 "Save them now?");
528 add_to_deplist(prefs, popup);
529 return True;
530 }
531 }
532
533 return False;
534 }
535
536 void
537 popup_preferences_dialog(Widget parent, int arg)
538 {
539 static Widget preferences_shell = 0;
540 static struct topic_info info;
541 static struct topic_item items[NUM_PREFS_TOPICS];
542 static struct prefs_choice *prefs = NULL;
543
544 if (preferences_shell == 0) { /* called 1st time; create widget */
545 info.ok_callback = apply_prefs_cb;
546 info.cancel_callback = revert_prefs_cb;
547 info.items = items;
548 /* info.items_size = NUM_PREFS_TOPICS; */
549
550 prefs = xmalloc(sizeof *prefs);
551 prefs->depwin_cnt = 0;
552 prefs->depwin = NULL;
553 /* prefs->orig = orig_prefs; */
554 /* apply_prefs_cb/revert_prefs_cb are responsible for copying
555 the changed preferences into the current preferences as
556 appropriate, and free()ing prefs.changed */
557 /* prefs->changed = xmalloc(sizeof *(prefs->changed)); */
558 /* copy_resources(orig_prefs, prefs->changed); */
559 prefs->db = NULL; /* XrmGetStringDatabase(""); */
560 info.data = prefs;
561
562 preferences_shell = create_topic_window(parent,
563 "xdvik: Preferences",
564 Xdvi_PREFS_DIALOG_NAME,
565 &info,
566 initialize_items,
567 "OK", "Cancel");
568 info.shell = preferences_shell;
569 center_window(preferences_shell, parent);
570 select_topic(&info, 0);
571 }
572
573 if (arg >= 0)
574 select_topic(&info, arg);
575
576 XtPopup(preferences_shell, XtGrabNone);
577
578 if (resource.no_init_file) {
579 popup_message(preferences_shell,
580 MSG_WARN,
581 NULL,
582 "You specified the resource `noInitFile' or the `-q' command-line option. "
583 "Any preferences that you set in this dialog will be lost when you exit xdvi.");
584 }
585 }
586
587 #else
588 /* silence `empty compilation unit' warnings */
589 static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); }
590 #endif /* MOTIF */