Debian Codegraph @

texk/xdvik/gui/print-dialog.c

raw
1 /*
2 * Copyright (c) 2002-2004 Paul Vojta and the xdvik development team
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 SU: Adapted from non-k xdvi's popups.c and added saving functionality.
25 */
26
27 #include "xdvi-config.h"
28 #include <string.h>
29 #include "xdvi.h"
30 #include "my-vsnprintf.h"
31 #include <ctype.h>
32
33 #include "print-dialog.h"
34 #include "print-internal.h"
35 #include "print-log.h"
36 #include "print-internal.h"
37 #include "events.h"
38 #include "dvi-init.h"
39 #include "string-utils.h"
40 #include "util.h"
41 #include "x_util.h"
42 #include "message-window.h"
43 #include "pagesel.h"
44 #include "my-snprintf.h"
45 #include "sfSelFile.h"
46 #include "dvisel.h" /* for select_marked_pages() */
47 #include "xlwradio.h"
48 #include "statusline.h"
49 #include "search-dialog.h"
50 #include "search-internal.h"
51 #include "special.h"
52
53 /* Xlib and Xutil are already included */
54
55
56 #include <X11/Xatom.h>
57 #include <X11/StringDefs.h>
58
59 #ifdef MOTIF
60 # include <Xm/BulletinB.h>
61 # include <Xm/DialogS.h>
62 # include <Xm/PanedW.h>
63 # include <Xm/MessageB.h>
64 # include <Xm/LabelG.h>
65 # include <Xm/Form.h>
66 # include <Xm/Frame.h>
67 # include <Xm/ToggleBG.h>
68 # include <Xm/Text.h>
69 # include <Xm/TextF.h>
70 # include <Xm/PushB.h>
71 # include <Xm/Protocols.h>
72 # include <Xm/AtomMgr.h>
73 #define XTranslations XmNtranslations
74 #else /* MOTIF */
75 # include <X11/Shell.h>
76 # include <X11/Xaw/Paned.h>
77 # include <X11/Xaw/Box.h>
78 # include <X11/Xaw/Form.h>
79 # include <X11/Xaw/Label.h>
80 # include <X11/Xaw/Command.h>
81 # include <X11/Xaw/Toggle.h>
82 # include <X11/Xaw/AsciiText.h>
83 # include <X11/Xaw/SimpleMenu.h>
84 # include <X11/Xaw/MenuButton.h>
85 # include <X11/Xaw/Sme.h>
86 # include <X11/Xaw/SmeBSB.h>
87 #if 0
88 #include "xaw_bitmaps.h"
89 static Pixmap menu_double_arrow_bitmap;
90 #endif
91 #define XTranslations XtNtranslations
92 #endif /* MOTIF */
93
94 #if HAVE_XKB_BELL_EXT
95 # include <X11/XKBlib.h>
96 # define XBell(dpy, percent) XkbBell(dpy, mane.win, percent, (Atom) None)
97 #endif
98
99 struct output_format_mapping {
100 const char *fmt_string;
101 outputFormatT fmt;
102 const char *extension;
103 } output_format_mapping[] = {
104 { "Postscript", FMT_PS , ".ps" },
105 { "PDF", FMT_PS2PDF, ".pdf" },
106 { "DVI", FMT_DVI, ".dvi" },
107 { "Plain Text (ISO_8859-1)",FMT_ISO_8859_1, ".txt" },
108 { "Plain Text (UTF-8)", FMT_UTF8, ".txt" },
109 { NULL, FMT_NONE, NULL }
110 };
111
112 /* Widget names that are used in callbacks ... */
113 #define Xdvi_SAVE_SHELL_NAME "save_popup"
114 #define Xdvi_PRINT_SHELL_NAME "print_popup"
115
116 #define Xdvi_PAGE_RANGE_FORM_NAME "page_range_form"
117
118 #define Xdvi_PAGES_ALL_RADIO_NAME "pages_all"
119 #define Xdvi_PAGES_MARKED_RADIO_NAME "pages_marked"
120 #define Xdvi_PAGES_RANGE_RADIO_NAME "pages_range"
121
122 #define Xdvi_PAGES_RANGE_FROM_LABEL_NAME "range_from_lab"
123 #define Xdvi_PAGES_RANGE_FROM_TEXT_NAME "range_from_text"
124 #define Xdvi_PAGES_RANGE_TO_LABEL_NAME "range_to_lab"
125 #define Xdvi_PAGES_RANGE_TO_TEXT_NAME "range_to_text"
126 #define Xdvi_PAGES_RANGE_OF_LABEL_NAME "range_of_lab"
127
128 #define Xdvi_TO_PRINTER_NAME "to_printer"
129 #define Xdvi_TO_PRINTER_TEXT "printer_text"
130 #define Xdvi_TO_FILE_NAME "to_file"
131 #define Xdvi_TO_FILE_TEXT "file_text"
132 #define Xdvi_BROWSE_BUTTON "file_button"
133
134 #define Xdvi_DVIPS_OPTIONS_NAME "dvips_options_name"
135 #define Xdvi_DVIPS_OPTIONS_TEXT "dvips_options_text"
136 #define Xdvi_FORMAT_SELECTION_BUTTON_NAME "format_selection_button"
137
138 /* number of items in `format' pulldown menu */
139 #define FORMAT_SEL_CNT 5
140
141 static void print_check_page_values(struct save_or_print_info *info);
142 static void print_check_dvi_file(struct save_or_print_info *info);
143 static void print_check_target_file(XtPointer info);
144
145 #ifndef MOTIF
146 static void xaw_print_save_act_go(Widget, XEvent *, String *, Cardinal *);
147 #endif /* not MOTIF */
148
149 static void wm_cancel(Widget, XEvent *, String *, Cardinal *);
150
151 static XtActionsRec print_actions[] = {
152 #ifndef MOTIF
153 {"xaw_print_or_save", xaw_print_save_act_go },
154 #endif
155 {"WM_cancel", wm_cancel },
156 };
157
158 /*
159 ============================================================
160 generic utility functions
161 ============================================================
162 */
163
164
165 /* return basename of `old_name' with `old_ext' replaced by `new_ext', in fresh memory */
166 static char *
167 get_new_file_name(const char *old_name, const char *old_ext, const char *new_ext)
168 {
169 /* old_name contains the normalized DVI file name, with path and extension */
170 char *basename, *new_name;
171 size_t len;
172
173 basename = strrchr(old_name, DIR_SEPARATOR);
174
175 if (basename != NULL) {
176 basename++;
177 new_name = xmalloc(strlen(basename) + strlen(new_ext) + 1);
178 new_name = strcpy(new_name, basename);
179 }
180 else {
181 new_name = xmalloc(strlen(old_name) + strlen(new_ext) + 1);
182 new_name = strcpy(new_name, old_name);
183 }
184
185 len = strlen(new_name);
186 if (old_ext == NULL) {
187 strcpy(new_name + len, new_ext);
188 }
189 else {
190 strcpy(new_name + len - strlen(old_ext), new_ext);
191 }
192 return new_name;
193 }
194
195 /*
196 * Translate `non-standard' paper sizes for xdvi into options suitable
197 * for dvips. We don't do this always, since it would override the papersize
198 * specials inserted by e.g.
199 * \usepackage[dvips]{geometry}
200 * which is the preferred way to specify the papersize. If the papersize
201 * has been explicitly set by such a special, this function returns NULL.
202 * Else it returns the value of the `paper' resource, without the trailing
203 * `r', and sets the `landscape' argument to True if a trailing `r' was
204 * present.
205 *
206 * Note that we don't check/warn about whether the current paper
207 * resource is valid for dvips; dvips will ignore invalid paper sizes.
208 */
209 static char *
210 get_dvips_papersize(Boolean *landscape)
211 {
212 char *papersize;
213 if (have_papersize_special())
214 return NULL;
215
216 papersize = xstrdup(resource.paper);
217 /* fprintf(stderr, "PAPERSIZE: |%s|\n", papersize); */
218 *landscape = False;
219 if (papersize[strlen(papersize) - 1] == 'r') { /* landscape size */
220 papersize[strlen(papersize) - 1] = '\0';
221 *landscape = True;
222 }
223 TRACE_GUI((stderr, "Using dvips arguments: `%s', %s", papersize, *landscape ? "landscape" : "portrait"));
224 return papersize;
225 }
226
227 static void
228 update_dvips_options_sensitivity(int idx, Widget top)
229 {
230 Widget dvips_label;
231
232 #if MOTIF
233 Widget dvips_options;
234
235 if (get_widget_by_name(&dvips_label, top, Xdvi_DVIPS_OPTIONS_NAME, True) &&
236 get_widget_by_name(&dvips_options, top, Xdvi_DVIPS_OPTIONS_TEXT, True)) {
237 if (output_format_mapping[idx].fmt == FMT_PS ||
238 output_format_mapping[idx].fmt == FMT_PS2PDF) {
239 XtSetSensitive(dvips_label, True);
240 XtSetSensitive(dvips_options, True);
241 }
242 else {
243 XtSetSensitive(dvips_label, False);
244 XtSetSensitive(dvips_options, False);
245 }
246 }
247 #else
248 if (get_widget_by_name(&dvips_label, top, Xdvi_DVIPS_OPTIONS_NAME, True)) {
249 XtSetSensitive(XtParent(dvips_label),
250 output_format_mapping[idx].fmt == FMT_PS ||
251 output_format_mapping[idx].fmt == FMT_PS2PDF);
252 }
253 #endif
254 }
255
256 /* callbacks */
257
258 static void
259 cb_select_format(Widget w, XtPointer client_data, XtPointer call_data)
260 {
261 struct save_or_print_info *info = NULL;
262 Widget file_text;
263 char *filename;
264 ptrdiff_t i;
265
266 #ifndef MOTIF
267 Widget button;
268 char *old_label, *new_label;
269 char tmp_label[1024];
270 #endif
271
272 UNUSED(call_data);
273
274 #ifdef MOTIF
275 i = (ptrdiff_t) client_data;
276 XtVaGetValues(XtParent(w), XmNuserData, &info, NULL);
277 ASSERT(info != NULL, "Expected struct save_or_print_info * in XmNuserData of button!");
278
279 if (get_widget_by_name(&file_text, info->shell, Xdvi_TO_FILE_TEXT, True)) {
280 XtVaGetValues(file_text, XmNvalue, &filename, NULL);
281 if (strrchr(filename, '.') != NULL) {
282 char buf[1024];
283 replace_extension(filename, output_format_mapping[i].extension, buf, sizeof buf);
284 XtVaSetValues(file_text, XmNvalue, buf, XmNcursorPosition, strlen(buf), NULL);
285 }
286 }
287 #else /* MOTIF */
288 info = (struct save_or_print_info *)client_data;
289
290 i = 0;
291 if (get_widget_by_name(&file_text, info->shell, Xdvi_TO_FILE_TEXT, True)
292 && get_widget_by_name(&button, info->shell, Xdvi_FORMAT_SELECTION_BUTTON_NAME, True)) {
293
294 XtVaGetValues(file_text, XtNstring, &filename, NULL);
295
296 /* swap title strings */
297 XtVaGetValues(button, XtNlabel, &old_label, NULL);
298 XtVaGetValues(w, XtNlabel, &new_label, NULL);
299 strncpy(tmp_label, old_label, sizeof tmp_label);
300
301 /* update the filename with new format */
302 for (i = 0; output_format_mapping[i].fmt_string != NULL; i++) {
303 if (strcmp(new_label, output_format_mapping[i].fmt_string) == 0) {
304 char buf[1024];
305 replace_extension(filename, output_format_mapping[i].extension, buf, sizeof buf);
306 XtVaSetValues(file_text, XtNstring, buf, NULL);
307 XawTextSetInsertionPoint(file_text, strlen(buf));
308
309 break;
310 }
311 }
312 /* format not found, complain: */
313 if (output_format_mapping[i].fmt_string == NULL) {
314 popup_message(info->shell,
315 MSG_WARN,
316 REPORT_XDVI_BUG_TEMPLATE,
317 "Unrecognized string in format selector: |%s|", new_label);
318 }
319 XtVaSetValues(button, XtNlabel, new_label, NULL);
320 XtVaSetValues(w, XtNlabel, tmp_label, NULL);
321 }
322 #endif /* MOTIF */
323
324 update_dvips_options_sensitivity(i, info->shell);
325
326 info->fmt = resource.default_saving_format = output_format_mapping[i].fmt;
327 }
328
329 static void
330 cb_popdown(Widget w, XtPointer client_data, XtPointer call_data)
331 {
332 struct save_or_print_info *info = (struct save_or_print_info *)client_data;
333 UNUSED(w);
334 UNUSED(call_data);
335
336 ASSERT(info != NULL, "client_data in cb_popdown musn't be NULL!");
337 if (info->message_popup != 0) {
338 kill_message_window(info->message_popup);
339 }
340 /* fprintf(stderr, "Popping down shell: %p\n", (void *)info->shell); */
341 XtPopdown(info->shell);
342 }
343
344 static void
345 cb_print_or_save(Widget w, XtPointer client_data, XtPointer call_data)
346 {
347 struct save_or_print_info *info = (struct save_or_print_info *)client_data;
348 UNUSED(w);
349 UNUSED(call_data);
350
351 print_check_page_values(info);
352 }
353
354 /* access from outside the module */
355 void
356 cancel_saving(struct save_or_print_info *info)
357 {
358 cb_popdown(NULL, info, NULL);
359 }
360
361
362 static void
363 wm_cancel(Widget w, XEvent *event, String *params, Cardinal *num_params)
364 {
365 struct save_or_print_info *info = NULL;
366 void *ptr;
367
368 UNUSED(w);
369 UNUSED(event);
370
371 ASSERT(*num_params > 0, "params in wm_cancel must be > 0!");
372 ASSERT(*params != NULL, "params in wm_cancel mustn't be NULL!");
373
374 TRACE_GUI((stderr, "Pointer string value: |%s|", *params));
375 sscanf(*params, "%p", &ptr);
376 info = (struct save_or_print_info *)ptr;
377
378 cb_popdown(NULL, info, NULL);
379 }
380
381 /*
382 * Callbacks to enable the corresponding radio button if the user clicks on a text field to
383 * edit it. This is more user-friendly than making the text field insensitive
384 * until the radio button has been clicked. There are 2 separate callbacks, one for
385 * printing vs. saving (only used when actually printing), and one for the page range.
386 */
387 static void
388 text_print_vs_save_callback(Widget widget, XtPointer closure, XEvent *ev, Boolean *cont)
389 {
390 struct save_or_print_info *info = (struct save_or_print_info *)closure;
391 Widget to_printer; /* used to check where we have been called from */
392 Widget radio_printer, radio_file;
393
394 UNUSED(ev);
395 UNUSED(cont);
396
397 if (get_widget_by_name(&to_printer, info->shell, Xdvi_TO_PRINTER_TEXT, True)
398 && get_widget_by_name(&radio_printer, info->shell, Xdvi_TO_PRINTER_NAME, True)
399 && get_widget_by_name(&radio_file, info->shell, Xdvi_TO_FILE_NAME, True)) {
400 #ifdef MOTIF
401 XmToggleButtonGadgetSetState(radio_printer, widget == to_printer, True);
402 XmToggleButtonGadgetSetState(radio_file, widget != to_printer, True);
403 #else
404 XawToggleSetCurrent(radio_printer, widget == to_printer ? cast_int_to_XtPointer(TO_PRINTER) : cast_int_to_XtPointer(TO_FILE));
405 #endif
406 }
407 }
408
409 /* This case is slightly simpler, since the callback only needs to enable
410 * the radio button for the page range, never disable it.
411 */
412 static void
413 text_page_range_callback(Widget widget, XtPointer closure, XEvent *ev, Boolean *cont)
414 {
415 struct save_or_print_info *info = (struct save_or_print_info *)closure;
416 Widget radio_all, radio_selected, radio_range;
417
418 UNUSED(ev);
419 UNUSED(cont);
420 UNUSED(widget);
421
422 if (get_widget_by_name(&radio_all, info->shell, Xdvi_PAGES_ALL_RADIO_NAME, True)
423 && get_widget_by_name(&radio_selected, info->shell, Xdvi_PAGES_MARKED_RADIO_NAME, True)
424 && get_widget_by_name(&radio_range, info->shell, Xdvi_PAGES_RANGE_RADIO_NAME, True)) {
425 #ifdef MOTIF
426 XmToggleButtonGadgetSetState(radio_all, False, True);
427 XmToggleButtonGadgetSetState(radio_selected, False, True);
428 XmToggleButtonGadgetSetState(radio_range, True, True);
429 #else
430 XawToggleSetCurrent(radio_all, cast_int_to_XtPointer(PAGE_RANGE));
431 #endif
432 }
433 }
434
435 #ifdef MOTIF
436
437 /* Motif type callbacks for the above. */
438 static void
439 cb_text_print_vs_save(Widget w, XtPointer client_data, XtPointer call_data)
440 {
441 UNUSED(call_data);
442 /* fprintf(stderr, "************ cb_text_print_vs_save!\n"); */
443 text_print_vs_save_callback(w, client_data, NULL, NULL);
444 }
445
446 static void
447 cb_text_page_range(Widget w, XtPointer client_data, XtPointer call_data)
448 {
449 UNUSED(call_data);
450 text_page_range_callback(w, client_data, NULL, NULL);
451 }
452
453 #else /* MOTIF */
454
455 static void
456 xaw_print_save_act_go(Widget w, XEvent *event, String *params, Cardinal *num_params)
457 {
458 struct save_or_print_info *info = NULL;
459 void *ptr;
460
461 UNUSED(w);
462 UNUSED(event);
463
464 ASSERT(*num_params > 0, "params in xaw_print_save_act_go must be > 0!");
465 ASSERT(*params != NULL, "params in xaw_print_save_act_go mustn't be NULL!");
466
467 TRACE_GUI((stderr, "Pointer string value: |%s|", *params));
468 sscanf(*params, "%p", &ptr);
469 info = (struct save_or_print_info *)ptr;
470
471 print_check_page_values(info);
472 }
473
474 #endif /* MOTIF */
475
476 static void
477 cb_print_vs_save(Widget w, XtPointer client_data, XtPointer call_data)
478 {
479 struct save_or_print_info *info = (struct save_or_print_info *)client_data;
480 Widget radio_printer, radio_file;
481 /* Widget file_button; */
482
483 UNUSED(call_data);
484
485 if (get_widget_by_name(&radio_printer, info->shell, Xdvi_TO_PRINTER_NAME, True)
486 && get_widget_by_name(&radio_file, info->shell, Xdvi_TO_FILE_NAME, True)) {
487 #ifdef MOTIF
488 Widget text;
489
490 if (radio_printer == w) {
491 XmToggleButtonGadgetSetState(radio_printer, True, False);
492 XmToggleButtonGadgetSetState(radio_file, False, False);
493 if (get_widget_by_name(&text, info->shell, Xdvi_TO_PRINTER_TEXT, True))
494 XmProcessTraversal(text, XmTRAVERSE_CURRENT);
495 }
496 else {
497 XmToggleButtonGadgetSetState(radio_printer, False, False);
498 XmToggleButtonGadgetSetState(radio_file, True, False);
499 if (get_widget_by_name(&text, info->shell, Xdvi_TO_FILE_TEXT, True))
500 XmProcessTraversal(text, XmTRAVERSE_CURRENT);
501 }
502 #endif
503 if (radio_printer == w)
504 info->print_target = TO_PRINTER;
505 else
506 info->print_target = TO_FILE;
507 }
508 }
509
510 static void
511 cb_range(Widget w, XtPointer client_data, XtPointer call_data)
512 {
513 struct save_or_print_info *info = (struct save_or_print_info *)client_data;
514 Widget radio_all, radio_selected, radio_range;
515
516 UNUSED(call_data);
517 #ifndef MOTIF
518 UNUSED(w);
519 #endif
520
521 if (get_widget_by_name(&radio_all, info->shell, Xdvi_PAGES_ALL_RADIO_NAME, True)
522 && get_widget_by_name(&radio_selected, info->shell, Xdvi_PAGES_MARKED_RADIO_NAME, True)
523 && get_widget_by_name(&radio_range, info->shell, Xdvi_PAGES_RANGE_RADIO_NAME, True)) {
524
525 #ifdef MOTIF
526 XmToggleButtonGadgetSetState(radio_all, radio_all == w, False);
527 XmToggleButtonGadgetSetState(radio_selected, radio_selected == w, False);
528 XmToggleButtonGadgetSetState(radio_range, radio_range == w, False);
529 #endif
530 }
531
532 /* update_page_range_sensitivity(info->shell, radio_range == w); */
533 }
534
535 static void
536 set_filename_callback(const char *fname, void *data)
537 {
538 /* fprintf(stderr, "Filename: |%s|; widget: %p\n", fname, data); */
539 if (fname != NULL) {
540 Widget w = (Widget)data;
541 #ifdef MOTIF
542 XtVaSetValues(w, XmNvalue, fname, XmNcursorPosition, strlen(fname), NULL);
543 #else
544 XtVaSetValues(w, XtNstring, fname, NULL);
545 XawTextSetInsertionPoint(w, strlen(fname));
546 #endif
547 }
548 }
549
550
551 static void
552 cb_browse(Widget w, XtPointer client_data, XtPointer call_data)
553 {
554 struct save_or_print_info *info = (struct save_or_print_info *)client_data;
555 printOrSaveActionT act = info->act;
556 Widget file_text;
557 /* static so that we can pass its address */
558 static struct filesel_callback cb[2] = {
559 { NULL, NULL,
560 "xdvik: Save to file", "Save to file:", "OK", "Cancel",
561 NULL, NULL, False, False, NULL, NULL },
562 { NULL, NULL,
563 "xdvik: Print to file", "Print to file:", "OK", "Cancel",
564 NULL, "*.ps", False, False, NULL, NULL }
565 };
566
567 UNUSED(call_data);
568
569 /* switch on `to file' radio button */
570 if (act == FILE_PRINT)
571 text_print_vs_save_callback(w, (XtPointer)info, NULL, NULL);
572
573 if (!get_widget_by_name(&file_text, info->shell, Xdvi_TO_FILE_TEXT, True))
574 return;
575
576 if (act == FILE_SAVE) {
577 if (resource.default_saving_format == FMT_PS)
578 cb[0].filemask = "*.ps";
579 else if (resource.default_saving_format == FMT_PS2PDF)
580 cb[0].filemask = "*.pdf";
581 else if (resource.default_saving_format == FMT_DVI)
582 cb[0].filemask = "*.dvi";
583 else
584 cb[0].filemask = "*.txt";
585 cb[0].func_ptr = set_filename_callback;
586 cb[0].data = file_text;
587 cb[0].browse_fname = xt_strdup(globals.cwd);
588
589 /* fprintf(stderr, "==== text widget save: %p; shell: %p\n", (void *)file_text, (void *)info->shell); */
590 if (cb[0].shell == NULL)
591 cb[0].shell = XsraSelFile(info->shell, &cb[0]);
592 XsraSelFilePopup(&cb[0]);
593 /* fprintf(stderr, "done saving: return widget = %p\n", ret_widget); */
594 }
595 else {
596 cb[1].func_ptr = set_filename_callback;
597 cb[1].data = file_text;
598 cb[1].init_path = globals.cwd;
599
600 /* fprintf(stderr, "==== text widget print: %p; shell: %p\n", (void *)file_text, (void *)info->shell); */
601 /* if (!get_widget_by_name(&parent, globals.widgets.top_level, Xdvi_PRINT_SHELL_NAME, True)) */
602 /* parent = globals.widgets.top_level; */
603 if (cb[1].shell == NULL)
604 cb[1].shell = XsraSelFile(info->shell, &cb[1]);
605 XsraSelFilePopup(&cb[1]);
606 }
607 }
608
609 /* create a dialog for printing OR saving */
610 #ifdef MOTIF
611
612 static void
613 motif_create_dialog(struct save_or_print_info *info)
614 {
615 printOrSaveActionT act = info->act;
616 Atom WM_DELETE_WINDOW;
617 char *ptr;
618 char ofstring[1024];
619
620 Widget form, pane, box;
621 Widget print_or_save_frame, destination_form;
622 Widget dvips_options_label;
623 Widget pages_frame, pages_form;
624 Widget print_to_file_text, to_file_radio_or_label;
625 Widget dvips_options_text;
626 Widget print_to_file_button;
627 Widget range_marked_radio, range_from_to_radio, to_printer_radio = NULL,
628 print_to_printer_text = NULL, format_selection_option = NULL,
629 print_or_save_range_all_radio,
630 page_from_text, to_label, page_to_text, of_label, ok_button, cancel_button;
631 Widget from_label;
632 XmString str;
633 XmString format_selection_texts[FORMAT_SEL_CNT];
634 XtTranslations xlats;
635
636 XtAddActions(print_actions, XtNumber(print_actions));
637
638 ptr = get_string_va("#override \n<Key>osfCancel:WM_cancel(%p)", info);
639 xlats = XtParseTranslationTable(ptr);
640 free(ptr);
641
642 if (globals.pageno_correct == 1)
643 sprintf(ofstring, "of %d physical pages", total_pages);
644 else
645 sprintf(ofstring, "of %d to %d pages", globals.pageno_correct, total_pages + globals.pageno_correct - 1);
646
647 info->shell = XtVaCreatePopupShell(act == FILE_SAVE ? "save_popup" : "print_popup",
648 xmDialogShellWidgetClass, globals.widgets.top_level,
649 XmNtitle, act == FILE_SAVE ? "xdvik: Save File" : "xdvik: Print File",
650 XmNallowShellResize, True,
651 XmNdeleteResponse, XmDO_NOTHING, /* we'll take care of that ourselves */
652 XmNmappedWhenManaged, False, /* so that we can center it first */
653 NULL);
654
655 /* make the window manager destroy action just pop down the dialog */
656 WM_DELETE_WINDOW = XmInternAtom(XtDisplay(info->shell), "WM_DELETE_WINDOW", False);
657
658 XmAddWMProtocolCallback(info->shell, WM_DELETE_WINDOW, cb_popdown, (XtPointer)info);
659
660
661 pane = XtVaCreateWidget(act == FILE_SAVE ? "save_paned" : "print_paned",
662 xmPanedWindowWidgetClass, info->shell,
663 /* make sashes invisible */
664 XmNsashWidth, 1,
665 XmNsashHeight, 1,
666 /* turn separator off, since it gives visual noise with the frames */
667 XmNseparatorOn, False,
668 NULL);
669
670
671 form = XtVaCreateWidget("form", xmFormWidgetClass,
672 pane,
673 XmNhorizontalSpacing, DDIST_MAJOR,
674 XmNverticalSpacing, DDIST_MAJOR,
675 XmNautoUnmanage, False,
676 XTranslations, xlats,
677 NULL);
678
679 if (act == FILE_PRINT) {
680 /* First frame: print to printer or file */
681
682 print_or_save_frame = XtVaCreateWidget("print_to_frame", xmFrameWidgetClass, form,
683 XmNmarginWidth, DDIST,
684 XmNmarginHeight, DDIST,
685 XmNtopAttachment, XmATTACH_FORM,
686 XmNleftAttachment, XmATTACH_FORM,
687 XmNrightAttachment, XmATTACH_FORM,
688 XTranslations, xlats,
689 NULL);
690
691 str = XmStringCreateLocalized("Print to:");
692 XtVaCreateManagedWidget("title", xmLabelGadgetClass,
693 print_or_save_frame,
694 XmNchildType, XmFRAME_TITLE_CHILD,
695 XmNlabelString, str,
696 NULL);
697 XmStringFree(str);
698
699 destination_form = XtVaCreateWidget("destination_form", xmFormWidgetClass, print_or_save_frame,
700 XmNhorizontalSpacing, DDIST,
701 XmNverticalSpacing, DDIST,
702 XmNtopAttachment, XmATTACH_FORM,
703 XmNleftAttachment, XmATTACH_FORM,
704 XmNrightAttachment, XmATTACH_FORM,
705 XTranslations, xlats,
706 NULL);
707
708 str = XmStringCreateLocalized("Printer:");
709 to_printer_radio = XtVaCreateManagedWidget(Xdvi_TO_PRINTER_NAME, xmToggleButtonGadgetClass, destination_form,
710 XmNlabelString, str,
711 XmNindicatorType, XmONE_OF_MANY,
712 XmNset, resource.default_printing_target == TO_PRINTER,
713 XmNtopAttachment, XmATTACH_FORM,
714 XmNtopOffset, 0,
715 XmNleftAttachment, XmATTACH_FORM,
716 XmNleftOffset, 0,
717 NULL);
718 XmStringFree(str);
719 XtAddCallback(to_printer_radio, XmNvalueChangedCallback, cb_print_vs_save, (XtPointer)info);
720
721 XtOverrideTranslations(to_printer_radio, xlats);
722
723 print_to_printer_text = XtVaCreateManagedWidget(Xdvi_TO_PRINTER_TEXT, xmTextFieldWidgetClass, destination_form,
724 XmNtopAttachment, XmATTACH_FORM,
725 XmNtopOffset, 0,
726 XmNleftAttachment, XmATTACH_WIDGET,
727 XmNrightAttachment, XmATTACH_FORM,
728 XmNleftWidget, to_printer_radio,
729 /* XmNsensitive, resource.default_printing_target == TO_PRINTER, */
730 XTranslations, xlats,
731 NULL);
732 XtAddCallback(print_to_printer_text, XmNactivateCallback, cb_print_or_save, (XtPointer)info);
733 XtAddCallback(print_to_printer_text, XmNfocusCallback, cb_text_print_vs_save, (XtPointer)info);
734
735 adjust_heights(to_printer_radio, print_to_printer_text, NULL);
736
737 str = XmStringCreateLocalized("PS File:");
738 to_file_radio_or_label = XtVaCreateManagedWidget(Xdvi_TO_FILE_NAME, xmToggleButtonGadgetClass, destination_form,
739 XmNlabelString, str,
740 XmNindicatorType, XmONE_OF_MANY,
741 XmNtopAttachment, XmATTACH_WIDGET,
742 XmNtopWidget, print_to_printer_text,
743 XmNleftAttachment, XmATTACH_FORM,
744 XmNset, resource.default_printing_target == TO_FILE,
745 XmNleftOffset, 0,
746 NULL);
747 XmStringFree(str);
748 XtAddCallback(to_file_radio_or_label, XmNvalueChangedCallback, cb_print_vs_save, (XtPointer)info);
749
750 str = XmStringCreateLocalized("Browse ...");
751 print_to_file_button = XtVaCreateManagedWidget(Xdvi_BROWSE_BUTTON, xmPushButtonWidgetClass, destination_form,
752 XmNlabelString, str,
753 /* XmNsensitive, resource.default_printing_target == TO_FILE, */
754 XmNtopAttachment, XmATTACH_WIDGET,
755 XmNtopWidget, print_to_printer_text,
756 XmNrightAttachment, XmATTACH_FORM,
757 NULL);
758 XmStringFree(str);
759
760 XtOverrideTranslations(to_file_radio_or_label, xlats);
761 XtOverrideTranslations(print_to_file_button, xlats);
762
763 print_to_file_text = XtVaCreateManagedWidget(Xdvi_TO_FILE_TEXT, xmTextFieldWidgetClass, destination_form,
764 /* XmNsensitive, resource.default_printing_target == TO_FILE, */
765 XmNtopAttachment, XmATTACH_WIDGET,
766 XmNtopWidget, print_to_printer_text,
767 XmNrightAttachment, XmATTACH_WIDGET,
768 XmNrightWidget, print_to_file_button,
769 XmNleftAttachment, XmATTACH_WIDGET,
770 XmNleftWidget, to_file_radio_or_label,
771 XTranslations, xlats,
772 NULL);
773
774 adjust_heights(to_file_radio_or_label, print_to_file_button, print_to_file_text, NULL);
775
776 XtAddCallback(print_to_file_text, XmNactivateCallback, cb_print_or_save, (XtPointer)info);
777 XtAddCallback(print_to_file_text, XmNfocusCallback, cb_text_print_vs_save, (XtPointer)info);
778 XtAddCallback(print_to_file_button, XmNactivateCallback, cb_browse, (XtPointer)info);
779
780 XtManageChild(destination_form);
781 XtManageChild(print_or_save_frame);
782
783 /* initial value for printer name */
784 XtVaSetValues(print_to_printer_text,
785 XmNvalue, info->printer_options,
786 XmNcursorPosition, strlen(info->printer_options),
787 NULL);
788 }
789 else { /* saving, not printing */
790 print_or_save_frame = XtVaCreateWidget("save_as_frame", xmFrameWidgetClass, form,
791 XmNmarginWidth, DDIST,
792 XmNmarginHeight, DDIST,
793 XmNtopAttachment, XmATTACH_FORM,
794 XmNleftAttachment, XmATTACH_FORM,
795 XmNrightAttachment, XmATTACH_FORM,
796 NULL);
797
798 str = XmStringCreateLocalized("Save as:");
799 XtVaCreateManagedWidget("title", xmLabelGadgetClass,
800 print_or_save_frame,
801 XmNchildType, XmFRAME_TITLE_CHILD,
802 XmNlabelString, str,
803 NULL);
804 XmStringFree(str);
805
806 destination_form = XtVaCreateWidget("destination_form", xmFormWidgetClass, print_or_save_frame,
807 XmNhorizontalSpacing, DDIST,
808 XmNverticalSpacing, DDIST,
809 XmNtopAttachment, XmATTACH_FORM,
810 XmNleftAttachment, XmATTACH_FORM,
811 XmNrightAttachment, XmATTACH_FORM,
812 XTranslations, xlats,
813 NULL);
814 /* fprintf(stderr, "CHILD: 0x%x\n", (long)destination_form); */
815 str = XmStringCreateLocalized("Format:");
816 format_selection_texts[0] = XmStringCreateLocalized((char *)output_format_mapping[0].fmt_string);
817 format_selection_texts[1] = XmStringCreateLocalized((char *)output_format_mapping[1].fmt_string);
818 format_selection_texts[2] = XmStringCreateLocalized((char *)output_format_mapping[2].fmt_string);
819 format_selection_texts[3] = XmStringCreateLocalized((char *)output_format_mapping[3].fmt_string);
820 format_selection_texts[4] = XmStringCreateLocalized((char *)output_format_mapping[4].fmt_string);
821 format_selection_option
822 = XmVaCreateSimpleOptionMenu(destination_form, "format_selection_option",
823 str, 'F',
824 resource.default_saving_format, /*initial menu selection*/
825 cb_select_format, /* callback */
826 XmVaPUSHBUTTON, format_selection_texts[0], 'P', NULL, NULL,
827 XmVaPUSHBUTTON, format_selection_texts[1], 'F', NULL, NULL,
828 XmVaPUSHBUTTON, format_selection_texts[2], 'D', NULL, NULL,
829 XmVaPUSHBUTTON, format_selection_texts[3], 'T', NULL, NULL,
830 XmVaPUSHBUTTON, format_selection_texts[4], 'U', NULL, NULL,
831 XmNuserData, (XtPointer)info,
832 NULL);
833
834 XmStringFree(str);
835 XmStringFree(format_selection_texts[0]);
836 XmStringFree(format_selection_texts[1]);
837 XmStringFree(format_selection_texts[2]);
838 XmStringFree(format_selection_texts[3]);
839 XmStringFree(format_selection_texts[4]);
840
841 str = XmStringCreateLocalized("File name:");
842 to_file_radio_or_label = XtVaCreateManagedWidget(Xdvi_TO_FILE_NAME, xmLabelGadgetClass, destination_form,
843 XmNtopAttachment, XmATTACH_WIDGET,
844 XmNtopWidget, format_selection_option,
845 XmNleftAttachment, XmATTACH_FORM,
846 /* XmNchildType, XmFRAME_TITLE_CHILD, */
847 XmNlabelString, str,
848 NULL);
849 XmStringFree(str);
850 str = XmStringCreateLocalized("Browse ...");
851 print_to_file_button = XtVaCreateManagedWidget(Xdvi_BROWSE_BUTTON, xmPushButtonWidgetClass, destination_form,
852 XmNlabelString, str,
853 XmNtopAttachment, XmATTACH_WIDGET,
854 XmNtopWidget, format_selection_option,
855 XmNrightAttachment, XmATTACH_FORM,
856 XTranslations, xlats,
857 NULL);
858 print_to_file_text = XtVaCreateManagedWidget(Xdvi_TO_FILE_TEXT, xmTextFieldWidgetClass, destination_form,
859 XmNtopAttachment, XmATTACH_WIDGET,
860 XmNtopWidget, format_selection_option,
861 XmNleftAttachment, XmATTACH_WIDGET,
862 XmNleftWidget, to_file_radio_or_label,
863 XmNrightAttachment, XmATTACH_WIDGET,
864 XmNrightWidget, print_to_file_button,
865 XTranslations, xlats,
866 NULL);
867
868 adjust_heights(to_file_radio_or_label, print_to_file_button, print_to_file_text, NULL);
869 XtAddCallback(print_to_file_button, XmNactivateCallback, cb_browse, (XtPointer)info);
870 }
871
872 /* initial value for filename */
873 XtVaSetValues(print_to_file_text,
874 XmNvalue, info->finfo->out_file,
875 XmNcursorPosition, strlen(info->finfo->out_file),
876 NULL);
877
878 { /* align left edges of widgets */
879 Dimension w1, w2;
880
881 XtVaGetValues(to_file_radio_or_label, XmNwidth, &w2, NULL);
882 if (act == FILE_PRINT)
883 XtVaGetValues(to_printer_radio, XmNwidth, &w1, NULL);
884 else {
885 Widget w;
886 if (get_widget_by_name(&w, format_selection_option, "OptionLabel", True)) {
887 XtVaGetValues(w, XmNwidth, &w1, NULL);
888 }
889 else {
890 w1 = w2;
891 }
892 }
893
894 if (w1 > w2) {
895 Dimension offset = DDIST;
896 offset += w1 - w2;
897 XtVaSetValues(print_to_file_text, XmNleftOffset, offset, NULL);
898 }
899 else if (w2 > w1) {
900 Dimension offset = DDIST;
901 offset += w2 - w1;
902 if (act == FILE_PRINT)
903 XtVaSetValues(print_to_printer_text, XmNleftOffset, offset, NULL);
904 else {
905 Widget w;
906 if (get_widget_by_name(&w, format_selection_option, "OptionLabel", True)) {
907 /* FIXME: Setting width would be cleaner, but it doesn't work ...?
908 Dimension curr_w;
909 XtVaGetValues(w, XmNwidth, &curr_w, NULL);
910 XtVaSetValues(w, XmNwidth, curr_w + offset, NULL);
911 */
912 /* fprintf(stderr, "Setting width from %d to: %d\n", curr_w, curr_w + offset); */
913 XtVaSetValues(w,
914 XmNmarginRight, offset,
915 XmNalignment, XmALIGNMENT_BEGINNING,
916 NULL);
917 }
918
919 }
920 }
921 }
922
923 if (act == FILE_SAVE) {
924 XtManageChild(format_selection_option);
925 XtManageChild(destination_form);
926 XtManageChild(print_or_save_frame);
927 }
928
929 /* additional dvips options */
930 str = XmStringCreateLocalized("Dvips Options:");
931 dvips_options_label = XtVaCreateManagedWidget(Xdvi_DVIPS_OPTIONS_NAME, xmLabelGadgetClass, form,
932 XmNlabelString, str,
933 XmNtopAttachment, XmATTACH_WIDGET,
934 XmNtopWidget, print_or_save_frame,
935 XmNleftAttachment, XmATTACH_FORM,
936 NULL);
937 XmStringFree(str);
938
939 dvips_options_text = XtVaCreateManagedWidget(Xdvi_DVIPS_OPTIONS_TEXT, xmTextFieldWidgetClass, form,
940 XmNtopAttachment, XmATTACH_WIDGET,
941 XmNtopWidget, print_or_save_frame,
942 XmNleftAttachment, XmATTACH_WIDGET,
943 XmNrightAttachment, XmATTACH_FORM,
944 XmNleftWidget, dvips_options_label,
945 XmNcolumns, 20,
946 XTranslations, xlats,
947 NULL);
948 XtAddCallback(dvips_options_text, XmNactivateCallback, cb_print_or_save, (XtPointer)info);
949 adjust_heights(dvips_options_label, dvips_options_text, NULL);
950
951 /* initial value for dvips options */
952 XtVaSetValues(dvips_options_text,
953 XmNvalue, info->dvips_options,
954 XmNcursorPosition, strlen(info->dvips_options),
955 NULL);
956
957 /* page selection */
958 pages_frame = XtVaCreateWidget(act == FILE_PRINT ? "print_page_frame" : "save_page_frame", xmFrameWidgetClass, form,
959 XmNmarginWidth, DDIST,
960 XmNmarginHeight, DDIST,
961 XmNresizable, True,
962 XmNtopAttachment, XmATTACH_WIDGET,
963 XmNtopWidget, dvips_options_text,
964 XmNleftAttachment, XmATTACH_FORM,
965 XmNrightAttachment, XmATTACH_FORM,
966 XTranslations, xlats,
967 NULL);
968
969 str = XmStringCreateLocalized("Pages:");
970 XtVaCreateManagedWidget("title", xmLabelGadgetClass, pages_frame,
971 XmNchildType, XmFRAME_TITLE_CHILD,
972 XmNlabelString, str,
973 NULL);
974 XmStringFree(str);
975
976 pages_form = XtVaCreateWidget("pages_form", xmFormWidgetClass, pages_frame,
977 XmNhorizontalSpacing, DDIST,
978 XmNverticalSpacing, DDIST,
979 XmNresizable, True,
980 XTranslations, xlats,
981 NULL);
982
983 str = XmStringCreateLocalized("All");
984 print_or_save_range_all_radio = XtVaCreateManagedWidget(Xdvi_PAGES_ALL_RADIO_NAME, xmToggleButtonGadgetClass, pages_form,
985 XmNlabelString, str,
986 XmNindicatorType, XmONE_OF_MANY,
987 XmNset, True,
988 XmNtopAttachment, XmATTACH_FORM,
989 XmNtopOffset, 0,
990 XmNleftAttachment, XmATTACH_FORM,
991 XmNleftOffset, 0,
992 XTranslations, xlats,
993 NULL);
994 XmStringFree(str);
995 XtAddCallback(print_or_save_range_all_radio, XmNvalueChangedCallback, cb_range, (XtPointer)info);
996
997 str = XmStringCreateLocalized("Marked");
998 range_marked_radio = XtVaCreateManagedWidget(Xdvi_PAGES_MARKED_RADIO_NAME, xmToggleButtonGadgetClass, pages_form,
999 XmNlabelString, str,
1000 XmNindicatorType, XmONE_OF_MANY,
1001 XmNtopAttachment, XmATTACH_WIDGET,
1002 XmNsensitive, pageinfo_have_marked_pages(),
1003 XmNtopWidget, print_or_save_range_all_radio,
1004 XmNleftAttachment, XmATTACH_FORM,
1005 XmNleftOffset, 0,
1006 XTranslations, xlats,
1007 NULL);
1008 XmStringFree(str);
1009 XtAddCallback(range_marked_radio, XmNvalueChangedCallback, cb_range, (XtPointer)info);
1010
1011 str = XmStringCreateLocalized("Range:");
1012 range_from_to_radio = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_RADIO_NAME, xmToggleButtonGadgetClass, pages_form,
1013 XmNlabelString, str,
1014 XmNindicatorType, XmONE_OF_MANY,
1015 XmNtopAttachment, XmATTACH_WIDGET,
1016 XmNtopWidget, range_marked_radio,
1017 XmNleftAttachment, XmATTACH_FORM,
1018 XmNleftOffset, 0,
1019 XTranslations, xlats,
1020 NULL);
1021 XmStringFree(str);
1022
1023 XtAddCallback(range_from_to_radio, XmNvalueChangedCallback, cb_range, (XtPointer)info);
1024
1025 str = XmStringCreateLocalized("From");
1026 from_label = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_FROM_LABEL_NAME, xmLabelGadgetClass, pages_form,
1027 XmNlabelString, str,
1028 /* XmNsensitive, info->page_selection == PAGE_RANGE, */
1029 XmNtopAttachment, XmATTACH_WIDGET,
1030 XmNtopWidget, range_marked_radio,
1031 XmNleftAttachment, XmATTACH_WIDGET,
1032 XmNleftWidget, range_from_to_radio,
1033 NULL);
1034 XmStringFree(str);
1035
1036 page_from_text = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_FROM_TEXT_NAME, xmTextFieldWidgetClass, pages_form,
1037 XmNcolumns, 5,
1038 /* XmNsensitive, info->page_selection == PAGE_RANGE, */
1039 XmNtopAttachment, XmATTACH_WIDGET,
1040 XmNtopWidget, range_marked_radio,
1041 XmNleftAttachment, XmATTACH_WIDGET,
1042 XmNleftWidget, from_label,
1043 XTranslations, xlats,
1044 NULL);
1045 XtAddCallback(page_from_text, XmNactivateCallback, cb_print_or_save, (XtPointer)info);
1046 XtAddCallback(page_from_text, XmNfocusCallback, cb_text_page_range, (XtPointer)info);
1047
1048 str = XmStringCreateLocalized("to");
1049 to_label = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_TO_LABEL_NAME, xmLabelGadgetClass, pages_form,
1050 XmNlabelString, str,
1051 /* XmNsensitive, info->page_selection == PAGE_RANGE, */
1052 XmNtopAttachment, XmATTACH_WIDGET,
1053 XmNtopWidget, range_marked_radio,
1054 XmNleftAttachment, XmATTACH_WIDGET,
1055 XmNleftWidget, page_from_text,
1056 NULL);
1057 XmStringFree(str);
1058
1059 page_to_text = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_TO_TEXT_NAME, xmTextFieldWidgetClass, pages_form,
1060 XmNcolumns, 5,
1061 /* XmNsensitive, info->page_selection == PAGE_RANGE, */
1062 XmNtopAttachment, XmATTACH_WIDGET,
1063 XmNtopWidget, range_marked_radio,
1064 XmNleftAttachment, XmATTACH_WIDGET,
1065 XmNleftWidget, to_label,
1066 XTranslations, xlats,
1067 NULL);
1068 XtAddCallback(page_to_text, XmNactivateCallback, cb_print_or_save, (XtPointer)info);
1069 XtAddCallback(page_to_text, XmNfocusCallback, cb_text_page_range, (XtPointer)info);
1070
1071 str = XmStringCreateLocalized(ofstring);
1072 of_label = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_OF_LABEL_NAME, xmLabelGadgetClass, pages_form,
1073 XmNlabelString, str,
1074 /* XmNsensitive, info->page_selection == PAGE_RANGE, */
1075 XmNresizable, True,
1076 XmNtopAttachment, XmATTACH_WIDGET,
1077 XmNtopWidget, range_marked_radio,
1078 XmNleftAttachment, XmATTACH_WIDGET,
1079 XmNleftWidget, page_to_text,
1080 NULL);
1081 XmStringFree(str);
1082
1083 adjust_heights(range_from_to_radio,
1084 from_label, page_from_text,
1085 to_label, page_to_text,
1086 of_label,
1087 NULL);
1088
1089 XtManageChild(pages_form);
1090 XtManageChild(pages_frame);
1091
1092 /* box for the OK/Cancel button */
1093 box = XtVaCreateManagedWidget("box", xmFormWidgetClass, pane,
1094 XmNskipAdjust, True, /* don't resize this area */
1095 XTranslations, xlats,
1096 NULL);
1097
1098 str = XmStringCreateLocalized(act == FILE_PRINT ? "Print" : "Save");
1099
1100 ok_button = XtVaCreateManagedWidget(act == FILE_PRINT ? "print_button" : "save_button", xmPushButtonWidgetClass, box,
1101 XmNlabelString, str,
1102 XmNshowAsDefault, True,
1103 XmNdefaultButtonShadowThickness, 1,
1104 XmNtopAttachment, XmATTACH_FORM,
1105 XmNbottomAttachment, XmATTACH_FORM,
1106 XmNleftAttachment, XmATTACH_FORM,
1107 /* to mimick appearance of native dialog buttons: */
1108 XmNmarginWidth, 6,
1109 XmNmarginHeight, 4,
1110 XmNtopOffset, 10,
1111 XmNbottomOffset, 10,
1112 XmNleftOffset, 10,
1113 NULL);
1114 XmStringFree(str);
1115
1116 XtAddCallback(ok_button, XmNactivateCallback, cb_print_or_save, (XtPointer)info);
1117 XtOverrideTranslations(ok_button, XtParseTranslationTable("<Key>Return:ArmAndActivate()"));
1118
1119 str = XmStringCreateLocalized("Cancel");
1120 cancel_button = XtVaCreateManagedWidget("cancel", xmPushButtonWidgetClass, box,
1121 XmNlabelString, str,
1122 XmNdefaultButtonShadowThickness, 1,
1123 XmNtopAttachment, XmATTACH_FORM,
1124 XmNbottomAttachment, XmATTACH_FORM,
1125 XmNrightAttachment, XmATTACH_FORM,
1126 /* to mimick appearance of native dialog buttons: */
1127 XmNmarginWidth, 6,
1128 XmNmarginHeight, 4,
1129 XmNtopOffset, 10,
1130 XmNbottomOffset, 10,
1131 XmNrightOffset, 10,
1132 NULL);
1133 XmStringFree(str);
1134
1135 XtOverrideTranslations(print_or_save_range_all_radio, xlats);
1136 XtOverrideTranslations(range_marked_radio, xlats);
1137 XtOverrideTranslations(range_from_to_radio, xlats);
1138 XtOverrideTranslations(ok_button, xlats);
1139 XtOverrideTranslations(cancel_button, xlats);
1140
1141 XtAddCallback(cancel_button, XmNactivateCallback, cb_popdown, (XtPointer)info);
1142
1143 { /* make the buttons the same size: */
1144 Dimension w1, w2;
1145 XtVaGetValues(ok_button, XtNwidth, &w1, NULL);
1146 XtVaGetValues(cancel_button, XtNwidth, &w2, NULL);
1147 if (w1 < w2)
1148 XtVaSetValues(ok_button, XtNwidth, w2, NULL);
1149 else if (w2 > w1)
1150 XtVaSetValues(cancel_button, XtNwidth, w1, NULL);
1151 }
1152
1153 if (pageinfo_have_marked_pages()) {
1154 XmToggleButtonGadgetSetState(range_marked_radio, True, True);
1155 }
1156 else if (info->page_selection == PAGE_MARKED) {
1157 XmToggleButtonGadgetSetState(print_or_save_range_all_radio, True, True);
1158 }
1159
1160 XtManageChild(form);
1161 XtManageChild(pane);
1162 center_window(info->shell, globals.widgets.top_level);
1163 XtMapWidget(info->shell);
1164 XmProcessTraversal(ok_button, XmTRAVERSE_CURRENT);
1165
1166 /* if (act == FILE_PRINT) { */
1167 /* if (resource.default_printing_target == TO_PRINTER) */
1168 /* XmProcessTraversal(print_to_printer_text, XmTRAVERSE_CURRENT); */
1169 /* else */
1170 /* XmProcessTraversal(print_to_file_text, XmTRAVERSE_CURRENT); */
1171 /* } */
1172 }
1173
1174 #else /* MOTIF */
1175
1176 static void
1177 xaw_create_dialog(struct save_or_print_info *info)
1178 {
1179 printOrSaveActionT act = info->act;
1180 Atom WM_DELETE_WINDOW;
1181 char *ptr;
1182 char ofstring[1024];
1183
1184 Widget form, paned, box;
1185 Widget dummy_label_form, dummy_pages_form; /* dummy forms to get indentation consistent */
1186 Widget save_to_file_form;
1187 Widget print_to_printer_form, print_to_file_form, dvips_options_form;
1188 Widget dvips_options_label;
1189 Widget page_range_form;
1190 Widget range_marked_form;
1191 Widget range_from_to_form;
1192 Widget print_to_file_text, to_file_radio_or_label;
1193 Widget dvips_options_text;
1194 Widget print_to_file_button;
1195 Widget format_selection_menu[FORMAT_SEL_CNT];
1196 Widget format_selection_label = NULL, format_selection_button = NULL, format_selection_popup;
1197 Widget range_marked_radio, range_from_to_radio, to_printer_radio = NULL, print_to_printer_text = NULL,
1198 print_or_save_range_all_radio,
1199 page_from_text, to_label, page_to_text, of_label, ok_button, cancel_button;
1200
1201 XtTranslations xlats, xlats2, wm_translations;
1202 XtAccelerators accels2;
1203 int ddist;
1204
1205 static Dimension w_avg = 220;
1206 Dimension w_curr;
1207
1208 /* handy abbrevs */
1209 #define HORIZONTAL_RESIZING_NO XtNleft, XtChainLeft, XtNright, XtChainLeft
1210 #define HORIZONTAL_RESIZING_YES XtNleft, XtChainLeft, XtNright, XtChainRight
1211 #define VERTICAL_RESIZING_NO XtNtop, XtChainTop, XtNbottom, XtChainTop
1212
1213 XtAddActions(print_actions, XtNumber(print_actions));
1214
1215 if (globals.pageno_correct == 1)
1216 sprintf(ofstring, "of %d physical pages", total_pages);
1217 else
1218 sprintf(ofstring, "of %d to %d pages", globals.pageno_correct, total_pages + globals.pageno_correct - 1);
1219
1220 ptr = get_string_va("<Message>WM_PROTOCOLS: WM_cancel(%p)\n"
1221 "<Key>Escape:WM_cancel(%p)\n"
1222 "<Key>q:WM_cancel(%p)",
1223 info, info, info);
1224 wm_translations = XtParseTranslationTable(ptr);
1225 free(ptr);
1226
1227 info->shell = XtVaCreatePopupShell(act == FILE_SAVE ? Xdvi_SAVE_SHELL_NAME : Xdvi_PRINT_SHELL_NAME,
1228 transientShellWidgetClass, globals.widgets.top_level,
1229 XtNtitle, act == FILE_SAVE ? "xdvik: Save File" : "xdvik: Print File",
1230 XtNmappedWhenManaged, False,
1231 XtNtransientFor, globals.widgets.top_level,
1232 XtNallowShellResize, True,
1233 XtNtranslations, wm_translations,
1234 NULL);
1235
1236 WM_DELETE_WINDOW = XInternAtom(XtDisplay(info->shell), "WM_DELETE_WINDOW", False);
1237
1238 paned = XtVaCreateManagedWidget("paned", panedWidgetClass, info->shell, NULL);
1239
1240 form = XtVaCreateManagedWidget("save_or_print_form", formWidgetClass, paned,
1241 XtNallowResize, True,
1242 NULL);
1243 XtVaGetValues(form, XtNdefaultDistance, &ddist, NULL);
1244 /* fprintf(stderr, "form1: %ld\n", (long)form); */
1245
1246 xlats = XtParseTranslationTable("<EnterWindow>:highlight(Always)\n"
1247 "<LeaveWindow>:unhighlight()\n"
1248 "<Btn1Down>,<Btn1Up>:set()notify()");
1249
1250 ptr = get_string_va("<Key>Return:xaw_print_or_save(%p)", info);
1251 xlats2 = XtParseTranslationTable(ptr);
1252 free(ptr);
1253
1254 if (act == FILE_PRINT) { /* selection `to printer/to file' */
1255 /* FIXME: set real sizes here */
1256 /* Dimension w_curr; */
1257 dummy_label_form = XtVaCreateManagedWidget("dummy_print_to_label_form", formWidgetClass, form,
1258 XtNresizable, True,
1259 XtNborderWidth, 0,
1260 HORIZONTAL_RESIZING_YES,
1261 NULL);
1262 XtVaCreateManagedWidget("print_to", labelWidgetClass, dummy_label_form,
1263 XtNlabel, "Print to: ",
1264 XtNborderWidth, 0,
1265 HORIZONTAL_RESIZING_NO,
1266 NULL);
1267
1268 print_to_printer_form = XtVaCreateManagedWidget("print_to_printer_form", formWidgetClass, form,
1269 XtNresizable, True,
1270 XtNborderWidth, 0,
1271 XtNvertDistance, 0,
1272 XtNfromVert, dummy_label_form,
1273 HORIZONTAL_RESIZING_YES,
1274 XtNallowResize, True,
1275 NULL);
1276
1277 accels2 = XtParseAcceleratorTable("<Btn1Down>,<Btn1Up>:set()notify()");
1278
1279 to_printer_radio = XtVaCreateManagedWidget(Xdvi_TO_PRINTER_NAME,
1280 #ifdef XAW
1281 radioWidgetClass,
1282 #else
1283 toggleWidgetClass,
1284 #endif
1285 print_to_printer_form,
1286 XtNlabel, "Printer: ",
1287 XtNborderWidth, 0,
1288 XtNhighlightThickness, 1,
1289 XtNradioData, TO_PRINTER,
1290 XtNstate, resource.default_printing_target == TO_PRINTER,
1291 XtNtranslations, xlats,
1292 XtNaccelerators, accels2,
1293 HORIZONTAL_RESIZING_NO,
1294 VERTICAL_RESIZING_NO,
1295 NULL);
1296
1297 XtAddCallback(to_printer_radio, XtNcallback, cb_print_vs_save, (XtPointer)info);
1298 XtInstallAccelerators(print_to_printer_form, to_printer_radio);
1299
1300 print_to_printer_text = XtVaCreateManagedWidget(Xdvi_TO_PRINTER_TEXT, asciiTextWidgetClass, print_to_printer_form,
1301 XtNwidth, w_avg,
1302 XtNdataCompression, False,
1303 XtNeditType, XawtextEdit,
1304 /* XtNresize, XawtextResizeWidth, */
1305 XtNfromHoriz, to_printer_radio,
1306 HORIZONTAL_RESIZING_YES,
1307 VERTICAL_RESIZING_NO,
1308 /* XtNscrollHorizontal, XawtextScrollAlways, */
1309 NULL);
1310 XtOverrideTranslations(print_to_printer_text, xlats2);
1311 XtAddEventHandler(print_to_printer_text, KeyPressMask | ButtonPressMask, False,
1312 text_print_vs_save_callback, (XtPointer)info);
1313
1314 adjust_vertically(to_printer_radio, print_to_printer_text, ddist);
1315
1316 print_to_file_form = XtVaCreateManagedWidget("print_to_file_form", formWidgetClass, form,
1317 XtNborderWidth, 0,
1318 XtNvertDistance, -ddist,
1319 XtNfromVert, print_to_printer_form,
1320 HORIZONTAL_RESIZING_YES,
1321 NULL);
1322
1323 to_file_radio_or_label = XtVaCreateManagedWidget(Xdvi_TO_FILE_NAME,
1324 #ifdef XAW
1325 radioWidgetClass,
1326 #else
1327 toggleWidgetClass,
1328 #endif
1329 print_to_file_form,
1330 XtNhighlightThickness, 1,
1331 XtNborderWidth, 0,
1332 XtNlabel, "PS File: ",
1333 XtNstate, resource.default_printing_target == TO_FILE,
1334 XtNradioGroup, to_printer_radio,
1335 XtNradioData, TO_FILE,
1336 XtNtranslations, xlats,
1337 XtNaccelerators, accels2,
1338 HORIZONTAL_RESIZING_NO,
1339 VERTICAL_RESIZING_NO,
1340 NULL);
1341 XtAddCallback(to_file_radio_or_label, XtNcallback, cb_print_vs_save, (XtPointer)info);
1342 XtInstallAccelerators(print_to_file_form, to_file_radio_or_label);
1343
1344 print_to_file_text = XtVaCreateManagedWidget(Xdvi_TO_FILE_TEXT, asciiTextWidgetClass, print_to_file_form,
1345 XtNwidth, w_avg,
1346 XtNdataCompression, False,
1347 XtNeditType, XawtextEdit,
1348 XtNfromHoriz, to_file_radio_or_label,
1349 HORIZONTAL_RESIZING_YES,
1350 VERTICAL_RESIZING_NO,
1351 NULL);
1352 XtOverrideTranslations(print_to_file_text, xlats2);
1353 XtAddEventHandler(print_to_file_text, KeyPressMask | ButtonPressMask, False,
1354 text_print_vs_save_callback, (XtPointer)info);
1355
1356 /* initial value for printer name */
1357 XtVaSetValues(print_to_printer_text, XtNstring, info->printer_options, NULL);
1358 XawTextSetInsertionPoint(print_to_printer_text, strlen(info->printer_options));
1359 }
1360 else { /* not printing, but saving */
1361 int i;
1362 Dimension max_entry_width = 0;
1363 dummy_label_form = XtVaCreateManagedWidget("dummy_save_as_form", formWidgetClass, form,
1364 XtNresizable, True,
1365 XtNborderWidth, 0,
1366 HORIZONTAL_RESIZING_YES,
1367 NULL);
1368 XtVaCreateManagedWidget("save_as", labelWidgetClass, dummy_label_form,
1369 XtNlabel, "Save as: ",
1370 XtNborderWidth, 0,
1371 HORIZONTAL_RESIZING_NO,
1372 NULL);
1373 save_to_file_form = XtVaCreateManagedWidget("save_to_file_form", formWidgetClass, form,
1374 XtNborderWidth, 0,
1375 XtNfromVert, dummy_label_form,
1376 HORIZONTAL_RESIZING_YES,
1377 VERTICAL_RESIZING_NO,
1378 NULL);
1379 /* fprintf(stderr, "formr1: %ld\n", (long)save_to_file_form); */
1380 #if 0
1381 menu_double_arrow_bitmap
1382 = XCreateBitmapFromData(XtDisplay(globals.widgets.top_level),
1383 RootWindowOfScreen(XtScreen(globals.widgets.top_level)),
1384 (char *)menu_double_arrow_bits, MENU_DOUBLE_ARROW_W, MENU_DOUBLE_ARROW_H);
1385 #endif
1386 format_selection_label = XtVaCreateManagedWidget("format_selection_label", labelWidgetClass, save_to_file_form,
1387 XtNlabel, "Format: ",
1388 XtNborderWidth, 0,
1389 HORIZONTAL_RESIZING_NO,
1390 VERTICAL_RESIZING_NO,
1391 NULL);
1392 format_selection_button = XtVaCreateManagedWidget(Xdvi_FORMAT_SELECTION_BUTTON_NAME, menuButtonWidgetClass, save_to_file_form,
1393 XtNmenuName, "format_selection_popup",
1394 XtNlabel, output_format_mapping[resource.default_saving_format].fmt_string,
1395 XtNjustify, XtJustifyLeft,
1396 XtNborderWidth, resource.btn_border_width,
1397 XtNfromHoriz, format_selection_label,
1398 #if 0
1399 XtNhighlightThickness, 0,
1400 #endif
1401 NULL);
1402 #if 0 /* Hack for arrow - menuButtonWidget cannot have a XtNrightBitmap!!! */
1403 format_selection_b1 = XtVaCreateManagedWidget("format_selection_b1", commandWidgetClass, save_to_file_form,
1404 XtNborderWidth, resource.btn_border_width,
1405 XtNfromHoriz, format_selection_button,
1406 XtNhorizDistance, -1,
1407 XtNhighlightThickness, 0,
1408 XtNbitmap, menu_double_arrow_bitmap,
1409 NULL);
1410 adjust_heights(format_selection_button, format_selection_b1, NULL);
1411 #endif
1412 format_selection_popup = XtCreatePopupShell("format_selection_popup", simpleMenuWidgetClass, format_selection_button,
1413 /* globals.widgets.top_level, */
1414 NULL, 0);
1415 #if 0
1416 XtAddCallback(format_selection_b1, XtNcallback, xaw_popup_menu_cb, format_selection_popup);
1417 #endif
1418 for (i = 1; i < FORMAT_SEL_CNT; i++) {
1419 char name[1024];
1420 Dimension curr_entry_width;
1421 SNPRINTF(name, sizeof name, "format_selection_pulldown_%d", i);
1422 format_selection_menu[i] = XtVaCreateManagedWidget(name, smeBSBObjectClass, format_selection_popup,
1423 XtNjustify, XtJustifyLeft,
1424 XtNlabel,
1425 i == resource.default_saving_format
1426 ? output_format_mapping[0].fmt_string
1427 : output_format_mapping[i].fmt_string,
1428 NULL);
1429 XtVaGetValues(format_selection_menu[i], XtNwidth, &curr_entry_width, NULL);
1430 if (curr_entry_width > max_entry_width)
1431 max_entry_width = curr_entry_width;
1432 XtAddCallback(format_selection_menu[i], XtNcallback, cb_select_format, (XtPointer)info);
1433 }
1434 XtVaSetValues(format_selection_button, XtNwidth, max_entry_width, NULL);
1435
1436 print_to_file_form = XtVaCreateManagedWidget("to_file_form", formWidgetClass, form,
1437 XtNborderWidth, 0,
1438 XtNfromVert, save_to_file_form,
1439 XtNvertDistance, -ddist,
1440 HORIZONTAL_RESIZING_YES,
1441 VERTICAL_RESIZING_NO,
1442 NULL);
1443 to_file_radio_or_label = XtVaCreateManagedWidget(Xdvi_TO_FILE_NAME, labelWidgetClass, print_to_file_form,
1444 XtNlabel, "File name: ",
1445 XtNborderWidth, 0,
1446 HORIZONTAL_RESIZING_NO,
1447 VERTICAL_RESIZING_NO,
1448 NULL);
1449 print_to_file_text = XtVaCreateManagedWidget(Xdvi_TO_FILE_TEXT, asciiTextWidgetClass, print_to_file_form,
1450 XtNwidth, w_avg,
1451 XtNdataCompression, False,
1452 XtNeditType, XawtextEdit,
1453 XtNfromHoriz, to_file_radio_or_label,
1454 HORIZONTAL_RESIZING_YES,
1455 VERTICAL_RESIZING_NO,
1456 NULL);
1457 XtOverrideTranslations(print_to_file_text, xlats2);
1458
1459
1460 { /* align left edges of format selection pulldown and filename field */
1461 Dimension w1, w2, w_max;
1462 XtVaGetValues(format_selection_label, XtNwidth, &w1, NULL);
1463 XtVaGetValues(to_file_radio_or_label, XtNwidth, &w2, NULL);
1464 w_max = MAX(w1, w2);
1465 XtVaSetValues(format_selection_button, XtNhorizDistance, ddist + (w_max - w1), NULL);
1466 XtVaSetValues(print_to_file_text, XtNhorizDistance, ddist + (w_max - w2), NULL);
1467 }
1468 }
1469
1470 /* initial value for filename */
1471 XtVaSetValues(print_to_file_text, XtNstring, info->finfo->out_file, NULL);
1472 XawTextSetInsertionPoint(print_to_file_text, strlen(info->finfo->out_file));
1473
1474 #define DIST_1 8 /* FIXME */
1475 print_to_file_button = XtVaCreateManagedWidget(Xdvi_BROWSE_BUTTON, commandWidgetClass, print_to_file_form,
1476 XtNlabel, "Browse ...",
1477 XtNfromHoriz, print_to_file_text,
1478 XtNhorizDistance, DIST_1,
1479 /* XtNsensitive, act == FILE_PRINT ? resource.default_printing_target == TO_FILE : True, */
1480 /* attach to right of form, no resizing: */
1481 XtNleft, XtChainRight, XtNright, XtChainRight,
1482 VERTICAL_RESIZING_NO,
1483 NULL);
1484
1485 XtVaGetValues(print_to_file_button, XtNwidth, &w_curr, NULL);
1486 XtVaSetValues(print_to_file_text, XtNwidth, w_avg - w_curr - DIST_1 - 2, NULL);
1487 #undef DIST_1
1488
1489 adjust_vertically(to_file_radio_or_label, print_to_file_text, ddist + 5);
1490 adjust_vertically(print_to_file_text, to_file_radio_or_label, ddist + 5);
1491 adjust_vertically(print_to_file_button, to_file_radio_or_label, ddist + 5);
1492
1493 XtAddCallback(print_to_file_button, XtNcallback, cb_browse, (XtPointer)info);
1494
1495 /* other dvips options */
1496 dvips_options_form = XtVaCreateManagedWidget("dvips_options_form", formWidgetClass, form,
1497 XtNborderWidth, 0,
1498 XtNfromVert, print_to_file_form,
1499 XtNvertDistance, 3 * ddist,
1500 HORIZONTAL_RESIZING_YES,
1501 NULL);
1502
1503 dvips_options_label = XtVaCreateManagedWidget(Xdvi_DVIPS_OPTIONS_NAME, labelWidgetClass, dvips_options_form,
1504 XtNlabel, "Dvips Options:",
1505 XtNborderWidth, 0,
1506 HORIZONTAL_RESIZING_NO,
1507 VERTICAL_RESIZING_NO,
1508 NULL);
1509
1510 dvips_options_text = XtVaCreateManagedWidget(Xdvi_DVIPS_OPTIONS_TEXT, asciiTextWidgetClass, dvips_options_form,
1511 XtNwidth, w_avg,
1512 XtNdataCompression, False,
1513 XtNeditType, XawtextEdit,
1514 XtNfromHoriz, dvips_options_label,
1515 HORIZONTAL_RESIZING_YES,
1516 VERTICAL_RESIZING_NO,
1517 NULL);
1518 XtOverrideTranslations(dvips_options_text, xlats2);
1519
1520 { /* align left edges of input fields */
1521 Dimension w1, w2, w3, w_max;
1522
1523 if (act == FILE_PRINT)
1524 XtVaGetValues(to_printer_radio, XtNwidth, &w1, NULL);
1525 else
1526 XtVaGetValues(format_selection_label, XtNwidth, &w1, NULL);
1527
1528 XtVaGetValues(to_file_radio_or_label, XtNwidth, &w2, NULL);
1529 XtVaGetValues(dvips_options_label, XtNwidth, &w3, NULL);
1530
1531 w_max = MAX(MAX(w1, w2), w3);
1532
1533 if (act == FILE_PRINT)
1534 XtVaSetValues(print_to_printer_text, XtNhorizDistance, ddist + (w_max - w1), NULL);
1535 else
1536 XtVaSetValues(format_selection_button, XtNhorizDistance, ddist + (w_max - w1), NULL);
1537
1538 XtVaSetValues(print_to_file_text, XtNhorizDistance, ddist + (w_max - w2), NULL);
1539 XtVaSetValues(dvips_options_text, XtNhorizDistance, ddist + (w_max - w3), NULL);
1540 }
1541
1542 /* initial value for dvips options */
1543 XtVaSetValues(dvips_options_text, XtNstring, info->dvips_options, NULL);
1544 XawTextSetInsertionPoint(dvips_options_text, strlen(info->dvips_options));
1545
1546 /* page selection */
1547 dummy_pages_form = XtVaCreateManagedWidget("dummy_pages_form", formWidgetClass, form,
1548 XtNfromVert, dvips_options_form,
1549 XtNresizable, True,
1550 XtNborderWidth, 0,
1551 XtNvertDistance, 3 * ddist,
1552 HORIZONTAL_RESIZING_NO,
1553 NULL);
1554
1555 XtVaCreateManagedWidget("range_lab", labelWidgetClass, dummy_pages_form,
1556 XtNlabel, "Pages:",
1557 XtNborderWidth, 0,
1558 HORIZONTAL_RESIZING_NO,
1559 NULL);
1560
1561 page_range_form = XtVaCreateManagedWidget(Xdvi_PAGE_RANGE_FORM_NAME, formWidgetClass, form,
1562 XtNborderWidth, 0,
1563 XtNfromVert, dummy_pages_form,
1564 XtNvertDistance, 0,
1565 HORIZONTAL_RESIZING_NO,
1566 NULL);
1567
1568 accels2 = XtParseAcceleratorTable("<Btn1Down>,<Btn1Up>:set()notify()");
1569 print_or_save_range_all_radio = XtVaCreateManagedWidget(Xdvi_PAGES_ALL_RADIO_NAME,
1570 #ifdef XAW
1571 radioWidgetClass,
1572 #else
1573 toggleWidgetClass,
1574 #endif
1575 page_range_form,
1576 XtNlabel, "All ",
1577 XtNborderWidth, 0,
1578 XtNhighlightThickness, 1,
1579 XtNradioData, PAGE_ALL,
1580 /* enable this button by default */
1581 XtNstate, True,
1582 XtNtranslations, xlats,
1583 XtNaccelerators, accels2,
1584 HORIZONTAL_RESIZING_NO,
1585 VERTICAL_RESIZING_NO,
1586 NULL);
1587 XtAddCallback(print_or_save_range_all_radio, XtNcallback, cb_range, (XtPointer)info);
1588 XtInstallAccelerators(page_range_form, print_or_save_range_all_radio);
1589
1590 range_marked_form = XtVaCreateManagedWidget("range_marked_form", formWidgetClass, form,
1591 XtNborderWidth, 0,
1592 XtNfromVert, page_range_form,
1593 XtNvertDistance, 0,
1594 HORIZONTAL_RESIZING_NO,
1595 NULL);
1596
1597 range_marked_radio = XtVaCreateManagedWidget(Xdvi_PAGES_MARKED_RADIO_NAME,
1598 #ifdef XAW
1599 radioWidgetClass,
1600 #else
1601 toggleWidgetClass,
1602 #endif
1603
1604 range_marked_form,
1605 XtNlabel, "Marked ",
1606 XtNsensitive, pageinfo_have_marked_pages(),
1607 XtNborderWidth, 0,
1608 XtNhighlightThickness, 1,
1609 XtNradioGroup, print_or_save_range_all_radio,
1610 XtNradioData, PAGE_MARKED,
1611 XtNtranslations, xlats,
1612 XtNaccelerators, accels2,
1613 HORIZONTAL_RESIZING_NO,
1614 VERTICAL_RESIZING_NO,
1615 NULL);
1616 XtAddCallback(range_marked_radio, XtNcallback, cb_range, (XtPointer)info);
1617 XtInstallAccelerators(range_marked_form, range_marked_radio);
1618
1619 range_from_to_form = XtVaCreateManagedWidget("range_from_to_form", formWidgetClass, form,
1620 XtNborderWidth, 0,
1621 XtNfromVert, range_marked_form,
1622 XtNvertDistance, 0,
1623 XtNresizable, True,
1624 /* HORIZONTAL_RESIZING_NO, */
1625 NULL);
1626
1627 range_from_to_radio = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_RADIO_NAME,
1628 #ifdef XAW
1629 radioWidgetClass,
1630 #else
1631 toggleWidgetClass,
1632 #endif
1633 range_from_to_form,
1634 XtNlabel, "From: ",
1635 XtNborderWidth, 0,
1636 XtNhighlightThickness, 1,
1637 XtNradioGroup, print_or_save_range_all_radio,
1638 XtNradioData, PAGE_RANGE,
1639 XtNtranslations, xlats,
1640 XtNaccelerators, accels2,
1641 HORIZONTAL_RESIZING_NO,
1642 VERTICAL_RESIZING_NO,
1643 NULL);
1644 XtAddCallback(range_from_to_radio, XtNcallback, cb_range, (XtPointer)info);
1645 XtInstallAccelerators(range_from_to_form, range_from_to_radio);
1646
1647 page_from_text = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_FROM_TEXT_NAME, asciiTextWidgetClass, range_from_to_form,
1648 XtNdataCompression, False,
1649 XtNeditType, XawtextEdit,
1650 XtNwidth, 50,
1651 XtNfromHoriz, range_from_to_radio,
1652 HORIZONTAL_RESIZING_NO,
1653 VERTICAL_RESIZING_NO,
1654 NULL);
1655 XtOverrideTranslations(page_from_text, xlats2);
1656 XtAddEventHandler(page_from_text, KeyPressMask | ButtonPressMask, False,
1657 text_page_range_callback, (XtPointer)info);
1658
1659 to_label = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_TO_LABEL_NAME, labelWidgetClass, range_from_to_form,
1660 XtNlabel, "to: ",
1661 XtNborderWidth, 0,
1662 XtNfromHoriz, page_from_text,
1663 /* XtNsensitive, info->page_selection == PAGE_RANGE, */
1664 HORIZONTAL_RESIZING_NO,
1665 VERTICAL_RESIZING_NO,
1666 NULL);
1667
1668 page_to_text = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_TO_TEXT_NAME, asciiTextWidgetClass, range_from_to_form,
1669 XtNdataCompression, False,
1670 XtNeditType, XawtextEdit,
1671 XtNwidth, 50,
1672 XtNfromHoriz, to_label,
1673 HORIZONTAL_RESIZING_NO,
1674 VERTICAL_RESIZING_NO,
1675 NULL);
1676 XtOverrideTranslations(page_to_text, xlats2);
1677 XtAddEventHandler(page_to_text, KeyPressMask | ButtonPressMask, False,
1678 text_page_range_callback, (XtPointer)info);
1679
1680 of_label = XtVaCreateManagedWidget(Xdvi_PAGES_RANGE_OF_LABEL_NAME, labelWidgetClass, range_from_to_form,
1681 XtNlabel, ofstring,
1682 XtNborderWidth, 0,
1683 /* XtNsensitive, info->page_selection == PAGE_RANGE, */
1684 XtNresizable, True,
1685 XtNfromHoriz, page_to_text,
1686 /* HORIZONTAL_RESIZING_NO, */
1687 XtNleft, XtChainLeft,
1688 VERTICAL_RESIZING_NO,
1689 NULL);
1690
1691 adjust_vertically(range_from_to_radio, page_from_text, ddist);
1692 adjust_vertically(to_label, page_from_text, ddist);
1693 adjust_vertically(of_label, page_from_text, ddist);
1694
1695 /* box for the OK/Cancel button */
1696 box = XtVaCreateManagedWidget("box", formWidgetClass, paned,
1697 /* resizing by user isn't needed */
1698 XtNshowGrip, False,
1699 XtNdefaultDistance, 6, /* some padding */
1700 /* resizing the window shouldn't influence this box,
1701 * but only the pane widget
1702 */
1703 XtNskipAdjust, True,
1704 XtNaccelerators, G_accels_cr,
1705 NULL);
1706
1707 ok_button = XtVaCreateManagedWidget(act == FILE_PRINT ? "print_button" : "save_button", commandWidgetClass, box,
1708 XtNlabel, act == FILE_PRINT ? "Print" : "Save",
1709 XtNaccelerators, G_accels_cr,
1710 XtNtop, XtChainTop,
1711 XtNbottom, XtChainBottom,
1712 HORIZONTAL_RESIZING_NO,
1713 NULL);
1714 XtAddCallback(ok_button, XtNcallback, cb_print_or_save, (XtPointer)info);
1715
1716 XtInstallAccelerators(form, ok_button);
1717 if (act == FILE_PRINT)
1718 XtInstallAccelerators(print_to_printer_text, ok_button);
1719 XtInstallAccelerators(print_to_file_text, ok_button);
1720
1721 cancel_button = XtVaCreateManagedWidget("cancel", commandWidgetClass, box,
1722 XtNlabel, "Cancel",
1723 XtNfromHoriz, ok_button,
1724 XtNbottom, XtChainBottom,
1725 XtNjustify, XtJustifyRight,
1726 XtNleft, XtChainRight,
1727 XtNright, XtChainRight,
1728 NULL);
1729 XtAddCallback(cancel_button, XtNcallback, cb_popdown, (XtPointer)info);
1730
1731 XtManageChild(info->shell);
1732 center_window(info->shell, globals.widgets.top_level);
1733 XtMapWidget(info->shell);
1734 XSetWMProtocols(XtDisplay(info->shell), XtWindow(info->shell), &WM_DELETE_WINDOW, 1);
1735
1736 if (pageinfo_have_marked_pages()) {
1737 XawToggleSetCurrent(print_or_save_range_all_radio, cast_int_to_XtPointer(PAGE_MARKED));
1738 }
1739 else if (info->page_selection == PAGE_MARKED) {
1740 XawToggleSetCurrent(print_or_save_range_all_radio, cast_int_to_XtPointer(PAGE_ALL));
1741 }
1742 #undef HORIZONTAL_RESIZING_NO
1743 #undef HORIZONTAL_RESIZING_YES
1744 #undef VERTICAL_RESIZING_NO
1745 }
1746
1747 #endif /* MOTIF */
1748
1749 static void
1750 get_initial_values(struct save_or_print_info *info)
1751 {
1752 const char *extension = NULL;
1753 char *tmp = NULL;
1754
1755 info->finfo->tmp_dvi_file = NULL;
1756 info->finfo->tmp_ps_file = NULL;
1757 info->finfo->tmp_dvi_fp = NULL;
1758 /* info->finfo->out_fp = NULL; */
1759
1760 info->message_popup = NULL;
1761
1762 info->dvips_options = info->printer_options = NULL;
1763
1764 /*
1765 * dvips options
1766 */
1767 info->dvips_options = xstrdup(resource.dvips_options_str ? resource.dvips_options_str : "");
1768
1769 /* add -t options if needed */
1770 if (info->dvips_options == NULL || info->dvips_options[0] == '\0' || strstr(info->dvips_options, "-t ") == NULL) {
1771 Boolean landscape = False;
1772 char *paper = get_dvips_papersize(&landscape);
1773 if (paper != NULL) {
1774 if (info->dvips_options == NULL)
1775 info->dvips_options = xstrdup("-t ");
1776 else
1777 info->dvips_options = xstrcat(info->dvips_options, " -t ");
1778
1779 info->dvips_options = xstrcat(info->dvips_options, paper);
1780 free(paper);
1781 if (landscape) {
1782 info->dvips_options = xstrcat(info->dvips_options, " -t landscape");
1783 }
1784 }
1785 }
1786
1787 /*
1788 * printer options
1789 */
1790 if (resource.dvips_printer_str != NULL)
1791 info->printer_options = xstrdup(resource.dvips_printer_str);
1792 else {
1793 /* initialize with default. We add the $PRINTER environment variable
1794 * since dvips otherwise doesn't use it. */
1795 char *printer = getenv("PRINTER");
1796 info->printer_options = xstrdup("lpr");
1797 if (printer != NULL) {
1798 info->printer_options = xstrcat(info->printer_options, " -P");
1799 info->printer_options = xstrcat(info->printer_options, printer);
1800 }
1801 }
1802
1803 info->print_target = resource.default_printing_target;
1804 info->fmt = resource.default_saving_format;
1805
1806 if (pageinfo_have_marked_pages()) {
1807 info->page_selection = PAGE_MARKED;
1808 }
1809 else {
1810 info->page_selection = PAGE_ALL;
1811 }
1812
1813 if (info->act == FILE_SAVE)
1814 extension = output_format_mapping[resource.default_saving_format].extension;
1815 else
1816 extension = ".ps";
1817
1818 tmp = get_new_file_name(globals.dvi_name,
1819 get_extension(globals.dvi_name),
1820 extension);
1821 info->finfo->out_file = xmalloc(strlen(globals.cwd) + strlen(tmp) + 2); /* 1 extra for dir separator */
1822 sprintf(info->finfo->out_file, "%s/%s", globals.cwd, tmp);
1823 free(tmp);
1824 TRACE_FILES((stderr, "OUTFILE: |%s|\n", info->finfo->out_file));
1825 }
1826
1827 static int
1828 textfield_to_int(Widget w)
1829 {
1830 char *s, *p;
1831 int pageno;
1832 #ifndef MOTIF
1833 XtVaGetValues(w, XtNstring, &s, NULL);
1834 #else /* MOTIF */
1835 s = XmTextFieldGetString(w);
1836 #endif
1837 p = s;
1838 if (*p == '-')
1839 ++p;
1840 if (!isdigit((int)*p)) {
1841 return 0;
1842 }
1843 do {
1844 ++p;
1845 } while (isdigit((int)*p));
1846 if (*p != '\0')
1847 return 0;
1848
1849 pageno = atoi(s) - globals.pageno_correct;
1850 #ifdef MOTIF
1851 XtFree(s);
1852 #endif
1853 return pageno;
1854 }
1855
1856 /*
1857 Return a copy of the text value of Widget w in malloc'ed memory,
1858 or NULL if the text is empty.
1859 */
1860
1861 static char *
1862 widgets_get_textfield(Widget w)
1863 {
1864 char *ptr = NULL;
1865 XtVaGetValues(w,
1866 #if MOTIF
1867 XmNvalue,
1868 #else
1869 XtNstring,
1870 #endif
1871 &ptr, NULL);
1872
1873 if (ptr == NULL || *ptr == '\0')
1874 return NULL;
1875 else
1876 return xstrdup(ptr);
1877 }
1878
1879 static char *
1880 widgets_get_textfield_by_name(const struct save_or_print_info *info, const char *widget_name)
1881 {
1882 Widget w;
1883 if (get_widget_by_name(&w, info->shell, widget_name, True)) {
1884 return widgets_get_textfield(w);
1885 }
1886 return NULL;
1887 }
1888
1889 static pageRadioT
1890 widgets_get_page_selection(struct save_or_print_info *info)
1891 {
1892 Widget radio_all;
1893 #ifdef MOTIF
1894 Widget radio_marked;
1895 #endif
1896 if (get_widget_by_name(&radio_all, info->shell, Xdvi_PAGES_ALL_RADIO_NAME, True)
1897 #ifdef MOTIF
1898 && get_widget_by_name(&radio_marked, info->shell, Xdvi_PAGES_MARKED_RADIO_NAME, True)
1899 #endif
1900 ) {
1901 #ifdef MOTIF
1902 if (XmToggleButtonGadgetGetState(radio_all))
1903 return PAGE_ALL;
1904 else if (XmToggleButtonGadgetGetState(radio_marked))
1905 return PAGE_MARKED;
1906 else
1907 return PAGE_RANGE;
1908 #else
1909 return (pageRadioT)XawToggleGetCurrent(radio_all);
1910 #endif
1911 }
1912 return NO_PAGE_VAL; /* dummy */
1913 }
1914
1915 static int
1916 widgets_get_from_page(struct save_or_print_info *info)
1917 {
1918 Widget w;
1919 if (get_widget_by_name(&w, info->shell, Xdvi_PAGES_RANGE_FROM_TEXT_NAME, True)) {
1920 return textfield_to_int(w);
1921 }
1922 return 0;
1923 }
1924
1925 static int
1926 widgets_get_to_page(struct save_or_print_info *info)
1927 {
1928 Widget w;
1929 if (get_widget_by_name(&w, info->shell, Xdvi_PAGES_RANGE_TO_TEXT_NAME, True)) {
1930 return textfield_to_int(w);
1931 }
1932 return 0;
1933 }
1934
1935 static void
1936 set_check_page_range(struct save_or_print_info *info, Boolean check)
1937 {
1938 Widget text_from, text_to;
1939
1940 if (get_widget_by_name(&text_from, info->shell, Xdvi_PAGES_RANGE_FROM_TEXT_NAME, True)
1941 && get_widget_by_name(&text_to, info->shell, Xdvi_PAGES_RANGE_TO_TEXT_NAME, True)) {
1942
1943 char page_string[LENGTH_OF_INT];
1944
1945 if (check) { /* check validity of current values */
1946 int curr_from = textfield_to_int(text_from);
1947 int curr_to = textfield_to_int(text_to);
1948
1949 if (curr_from < total_pages && curr_to < total_pages) /* values are OK */
1950 return;
1951 }
1952
1953 sprintf(page_string, "%d", current_page + globals.pageno_correct);
1954
1955 #ifdef MOTIF
1956 XtVaSetValues(text_from, XmNvalue, page_string, XmNcursorPosition, strlen(page_string), NULL);
1957 XtVaSetValues(text_to, XmNvalue, page_string, XmNcursorPosition, strlen(page_string), NULL);
1958 #else
1959 XtVaSetValues(text_from, XtNstring, page_string, NULL);
1960 XtVaSetValues(text_to, XtNstring, page_string, NULL);
1961 XawTextSetInsertionPoint(text_from, strlen(page_string));
1962 XawTextSetInsertionPoint(text_to, strlen(page_string));
1963 #endif
1964 }
1965 }
1966
1967 static void
1968 popdown_dialog_and_print_or_save(XtPointer myinfo)
1969 {
1970 struct save_or_print_info *info = (struct save_or_print_info *)myinfo;
1971
1972 cb_popdown(NULL, info, NULL);
1973
1974 if (globals.dvi_file.bak_fp == NULL) { /* shouldn't happen */
1975 info->message_popup = popup_message(globals.widgets.top_level,
1976 MSG_ERR, NULL,
1977 "No active DVI file");
1978 return;
1979 }
1980
1981 ASSERT(info->finfo->in_fp != NULL, "DVI input FILE * must have been set!");
1982
1983 store_preference(NULL, "dvipsOptionsString", "%s", info->dvips_options ? info->dvips_options : "");
1984 if (info->act == FILE_PRINT) {
1985 store_preference(NULL, "defaultPrintingTarget", "%d", info->print_target);
1986 store_preference(NULL, "dvipsPrinterString", "%s", info->printer_options ? info->printer_options : "");
1987 internal_print(info);
1988 }
1989 else {
1990 store_preference(NULL, "defaultSavingFormat", "%d", info->fmt);
1991 internal_save(info);
1992 }
1993 }
1994
1995 /* Clean up after user has aborted printing/saving */
1996 static void
1997 do_cleanup(XtPointer arg)
1998 {
1999 struct save_or_print_info *info = (struct save_or_print_info *)arg;
2000
2001 cancel_saving(info);
2002 if (info->finfo->tmp_dvi_file != NULL) {
2003 unlink(info->finfo->tmp_dvi_file);
2004 info->finfo->tmp_dvi_file = NULL;
2005 }
2006 if (info->finfo->tmp_ps_file != NULL) {
2007 unlink(info->finfo->tmp_ps_file);
2008 info->finfo->tmp_ps_file = NULL;
2009 }
2010 }
2011
2012
2013 /*
2014 * First round of sanity checks for page range etc.
2015 */
2016 static void
2017 print_check_page_values(struct save_or_print_info *info)
2018 {
2019 struct select_pages_info *pinfo = info->pinfo;
2020
2021 /* get current dvips and printer options */
2022 free(info->dvips_options);
2023 free(info->printer_options);
2024 info->dvips_options = info->printer_options = NULL;
2025
2026 info->dvips_options = widgets_get_textfield_by_name(info, Xdvi_DVIPS_OPTIONS_TEXT);
2027 if (info->act == FILE_PRINT)
2028 info->printer_options = widgets_get_textfield_by_name(info, Xdvi_TO_PRINTER_TEXT);
2029 info->page_selection = widgets_get_page_selection(info);
2030
2031 /* fprintf(stderr, "page selection: %d\n", info->page_selection); */
2032
2033 if (info->page_selection == PAGE_ALL) {
2034 /* fprintf(stderr, "ALL!!!\n"); */
2035 pinfo->callback = NULL;
2036 }
2037 else if (info->page_selection == PAGE_MARKED) {
2038 /* fprintf(stderr, "MARKED!!!\n"); */
2039 pinfo->callback = check_marked;
2040 }
2041 else { /* PAGE_RANGE */
2042 /* fprintf(stderr, "RANGE!!!\n"); */
2043 pinfo->from = widgets_get_from_page(info);
2044 pinfo->to = widgets_get_to_page(info);
2045 pinfo->callback = check_pagerange;
2046 if (pinfo->from + 1 < 1) {
2047 info->message_popup =
2048 popup_message(info->shell,
2049 MSG_ERR,
2050 "Please specify a valid page range.",
2051 "Invalid page range: start (%d) < 1.",
2052 pinfo->from + 1);
2053 return;
2054 }
2055 else if (pinfo->from > pinfo->to) {
2056 info->message_popup =
2057 popup_message(info->shell,
2058 MSG_ERR,
2059 "Please specify a valid page range.",
2060 "Invalid page range: start (%d) > end (%d).",
2061 pinfo->from + 1, pinfo->to + 1);
2062 return;
2063 }
2064 else if (pinfo->to + 1 > total_pages) {
2065 info->message_popup =
2066 popup_message(info->shell,
2067 MSG_ERR,
2068 "Please specify a valid page range.",
2069 "Invalid page range: end (%d) > total_pages (%d).",
2070 pinfo->to + 1, total_pages);
2071 return;
2072 }
2073 }
2074
2075 print_check_dvi_file(info);
2076 }
2077
2078 /*
2079 Second round of sanity checks.
2080 */
2081 static void
2082 print_check_dvi_file(struct save_or_print_info *info)
2083 {
2084 FILE *fin = NULL;
2085
2086 /* Return if there are active popups; otherwise, the print process
2087 may get messed up */
2088 if (raise_message_windows()) {
2089 XBell(DISP, 0);
2090 #if 0
2091 popup_message(info->shell,
2092 MSG_WARN, NULL,
2093 "Please close other open message windows first!");
2094 #endif
2095 return;
2096 }
2097
2098 /* If we want to print/save the marked pages, or save to a DVI file,
2099 we need to create a temporary DVI file containing the selected or all pages. */
2100 if (info->page_selection == PAGE_MARKED || info->fmt == FMT_DVI) {
2101 int tmp_fd;
2102 free(info->finfo->tmp_dvi_file);
2103 info->finfo->tmp_dvi_file = NULL;
2104 tmp_fd = xdvi_temp_fd(&(info->finfo->tmp_dvi_file)); /* this allocates info->finfo->tmp_dvi_file */
2105
2106 if (tmp_fd == -1) {
2107 info->message_popup =
2108 popup_message(info->shell,
2109 MSG_ERR, NULL,
2110 "Couldn't create temporary DVI file for printing: %s", strerror(errno));
2111 return;
2112 }
2113 /* don't use XFOPEN here, since we don't want to treat an error in opening the file as fatal. */
2114 /* `b' in mode won't hurt ... */
2115 if ((info->finfo->tmp_dvi_fp = try_fdopen(tmp_fd, "wb+")) == NULL) { /* failure */
2116 info->message_popup =
2117 popup_message(info->shell,
2118 MSG_ERR,
2119 "Xdvi needs to create a temporary file containing the "
2120 "currently marked pages, but creating that file failed. "
2121 "Try to fix the cause of the problem, or choose "
2122 "\"Print selected pages\" as a workaround.",
2123 "Could not open temp file for writing: %s.\n",
2124 strerror(errno));
2125 return;
2126 }
2127 }
2128 /* We try to use the original DVI file unless it's corrupted, in which case
2129 we ask the user if they want to save the cached copy instead */
2130 if ((fin = fopen(globals.dvi_name, "rb")) == NULL
2131 || !find_postamble(fin, &(info->pinfo->errflag))) {
2132 Boolean could_not_open = False;
2133 if (fin == NULL)
2134 could_not_open = True;
2135 if (fin)
2136 fclose(fin);
2137
2138 /* if we can't use the temporary file as source for our copy, this is a fatal error */
2139 if (!resource.use_temp_fp) {
2140 info->message_popup =
2141 popup_message(info->shell,
2142 MSG_ERR, NULL,
2143 "Could not copy DVI file %s: The file %s",
2144 globals.dvi_name, could_not_open ? "doesn't exist any more" : "seems to be corrupted");
2145 return;
2146 }
2147
2148 /* else, we'll try to use the cached copy of the DVI file */
2149 if ((fin = fopen(get_tmp_dvi_name(), "rb")) == NULL) {
2150 info->message_popup =
2151 popup_message(info->shell,
2152 MSG_ERR, NULL,
2153 "Something's very wrong here - opening of both the "
2154 "original DVI file and the cached copy failed!");
2155 return;
2156 }
2157
2158 if (!find_postamble(fin, &(info->pinfo->errflag))) {
2159 info->message_popup =
2160 popup_message(info->shell,
2161 MSG_ERR, NULL,
2162 "Shouldn't happen: Cached copy of the DVI file seems corrupted!");
2163 return;
2164 }
2165 info->finfo->in_fp = fin;
2166
2167 /* tell user about it */
2168 info->message_popup =
2169 choice_dialog(info->shell,
2170 MSG_QUESTION, NULL,
2171 #ifndef MOTIF
2172 NULL, /* TODO: binding for RET? */
2173 #endif /* MOTIF */
2174 NULL, NULL, /* no pre_callbacks */
2175 info->act == FILE_PRINT ? "Print Copy" : "Save Copy",
2176 print_check_target_file, (XtPointer)info,
2177 "Cancel", do_cleanup, (XtPointer)info,
2178 "The DVI file %s %s. Do you want to %s a cached copy of the file?",
2179 globals.dvi_name,
2180 could_not_open ? "doesn't exist any more" : "seems to be corrupted",
2181 info->act == FILE_PRINT ? "print" : "save");
2182 }
2183 else { /* original DVI file is OK, copy it */
2184 info->finfo->in_fp = fin;
2185 print_check_target_file((XtPointer)info);
2186 }
2187 }
2188
2189 static void
2190 print_check_target_file(XtPointer myinfo)
2191 {
2192 struct save_or_print_info *info = (struct save_or_print_info *)myinfo;
2193 char *filename = widgets_get_textfield_by_name(info, Xdvi_TO_FILE_TEXT);
2194 char *expanded_filename;
2195
2196 /* Return if there are active popups; otherwise, the print process
2197 will be messed up. */
2198 if (raise_message_windows()) {
2199 XBell(DISP, 0);
2200 return;
2201 }
2202
2203 /* if we're just printing to printer, go ahead */
2204 if (!(info->act == FILE_SAVE || (info->act == FILE_PRINT && info->print_target == TO_FILE))) {
2205 popdown_dialog_and_print_or_save(info);
2206 return;
2207 }
2208
2209 /* expand and canonicalize filename */
2210 if (filename == NULL) {
2211 info->message_popup =
2212 popup_message(info->shell,
2213 MSG_WARN,
2214 NULL,
2215 "No filename specified!");
2216 return;
2217 }
2218 expanded_filename = expand_homedir(filename); /* this allocates expanded_filename */
2219
2220 if (expanded_filename == NULL) {
2221 info->message_popup =
2222 popup_message(info->shell,
2223 MSG_WARN,
2224 "Please specify either a filename or a full path, without using \"~\" or \"~user\".",
2225 "Couldn't expand filename \"%s\" to a full path.",
2226 filename);
2227 free(filename);
2228 return;
2229 }
2230 free(filename);
2231 free(info->finfo->out_file);
2232 info->finfo->out_file = expanded_filename;
2233
2234 if (info->act == FILE_SAVE && info->fmt == FMT_DVI) { /* canonicalize filename */
2235 char *tmp = expand_filename_append_dvi(info->finfo->out_file, USE_CWD_PATH, False); /* this mallocs tmp */
2236 if (strcmp(tmp, globals.dvi_name) == 0) {
2237 info->message_popup =
2238 popup_message(info->shell,
2239 MSG_ERR,
2240 NULL,
2241 "Cannot overwrite the current DVI file (%s). "
2242 "Please choose a different file name.",
2243 info->finfo->out_file);
2244 free(tmp);
2245 free(info->finfo->out_file);
2246 info->finfo->out_file = NULL;
2247 return;
2248 }
2249 free(info->finfo->out_file);
2250 info->finfo->out_file = tmp;
2251 }
2252 if (info->act == FILE_SAVE || (info->act == FILE_PRINT && info->print_target == TO_FILE)) {
2253 /* check whether to clobber existing file */
2254 struct stat statbuf;
2255 if (stat(info->finfo->out_file, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
2256 info->message_popup =
2257 choice_dialog(info->shell,
2258 MSG_QUESTION, NULL,
2259 #ifndef MOTIF
2260 NULL, /* TODO: binding for RET */
2261 #endif
2262 NULL, NULL, /* no pre_callbacks */
2263 "Replace", popdown_dialog_and_print_or_save, (XtPointer)info,
2264 /*
2265 do nothing if user selects `cancel'; this will return to the
2266 printing dialog, since user hasn't changed their mind about
2267 printing, but would probably just like to choose another filename
2268 */
2269 "Cancel", NULL, (XtPointer)NULL,
2270 "The file %s already exists.\nDo you want to replace it?",
2271 info->finfo->out_file);
2272 return;
2273 }
2274 else {
2275 popdown_dialog_and_print_or_save(info);
2276 }
2277 }
2278 else {
2279 popdown_dialog_and_print_or_save(info);
2280 }
2281 }
2282
2283 static void
2284 notify_dialog(Widget w, pageRadioT *value_bak)
2285 {
2286 Widget radio_all, radio_marked;
2287 #ifdef MOTIF
2288 Widget radio_range;
2289 #endif
2290 Boolean have_marked = pageinfo_have_marked_pages();
2291 pageRadioT value_curr;
2292
2293 /* fprintf(stderr, "have_marked: %d\n", have_marked); */
2294
2295 if (get_widget_by_name(&radio_all, w, Xdvi_PAGES_ALL_RADIO_NAME, True)
2296 && get_widget_by_name(&radio_marked, w, Xdvi_PAGES_MARKED_RADIO_NAME, True)
2297 #ifdef MOTIF
2298 && get_widget_by_name(&radio_range, w, Xdvi_PAGES_RANGE_RADIO_NAME, True)
2299 #endif
2300 ) {
2301
2302 /* get the current value */
2303 #ifdef MOTIF
2304 if (XmToggleButtonGadgetGetState(radio_all))
2305 value_curr = PAGE_ALL;
2306 else if (XmToggleButtonGadgetGetState(radio_marked))
2307 value_curr = PAGE_MARKED;
2308 else
2309 value_curr = PAGE_RANGE;
2310 #else
2311 value_curr = (pageRadioT)XawToggleGetCurrent(radio_all);
2312 #endif
2313
2314 /* fprintf(stderr, "current value: %d\n", value_curr); */
2315
2316 if (value_curr == PAGE_MARKED && have_marked)
2317 return;
2318 if (value_curr != PAGE_MARKED && !have_marked) {
2319 XtVaSetValues(radio_marked,
2320 #ifdef MOTIF
2321 XmNsensitive,
2322 #else
2323 XtNsensitive,
2324 #endif
2325 False, NULL);
2326 return;
2327 }
2328
2329 /* otherwise, if we have marked pages now, we want to switch from
2330 the current value to `marked', saving the current value in *value_bak. */
2331
2332 /* XXX FIXME: There's a bug with OpenMotif 2.1 if the following order is used:
2333 - without marked pages, open dialog
2334 - mark a page; button `marked' is selected
2335 - select button `all'
2336 - select button `range'
2337 - unmark page; `marked' becomes insensitive
2338 - mark page, `marked' becomes sensitive and is selected, `range' is unselected
2339 - up to here, everything is as expected, but if you now select `range',
2340 it doesn't work, no button changes its state. As a workaround, if you select
2341 `marked' again and then `range', it works. Strangely, the callback when unsetting
2342 `range' is invoked correctly, so the button should be in the correct internal state ...
2343 I think this is an OpenMotif bug.
2344 */
2345 if (have_marked) {
2346 *value_bak = value_curr;
2347 #ifdef MOTIF
2348 if (value_curr == PAGE_ALL) {
2349 XmToggleButtonGadgetSetState(radio_all, False, True);
2350 }
2351 else {
2352 XmToggleButtonGadgetSetState(radio_range, False, True);
2353 }
2354 XmToggleButtonGadgetSetState(radio_marked, True, True);
2355 XtVaSetValues(radio_marked, XmNsensitive, True, NULL);
2356 XmProcessTraversal(radio_marked, XmTRAVERSE_CURRENT);
2357 #else
2358 XawToggleSetCurrent(radio_all, cast_int_to_XtPointer(PAGE_MARKED));
2359 XtVaSetValues(radio_marked, XtNsensitive, True, NULL);
2360 #endif
2361 }
2362 else {
2363 /* no marked pages now; switch back to the value from *value_bak. */
2364 /* fprintf(stderr, "switching back to button %d\n", *value_bak); */
2365 #ifdef MOTIF
2366 if (*value_bak == PAGE_ALL) {
2367 XmToggleButtonGadgetSetState(radio_all, True, True);
2368 XmProcessTraversal(radio_all, XmTRAVERSE_CURRENT);
2369 }
2370 else {
2371 XmToggleButtonGadgetSetState(radio_range, True, True);
2372 XmProcessTraversal(radio_range, XmTRAVERSE_CURRENT);
2373 }
2374 XmToggleButtonGadgetSetState(radio_marked, False, True);
2375 XtVaSetValues(radio_marked, XmNsensitive, False, NULL);
2376 #else
2377 XawToggleSetCurrent(radio_all, (XtPointer)*value_bak);
2378 XtVaSetValues(radio_marked, XtNsensitive, *value_bak == PAGE_MARKED, NULL);
2379 #endif
2380 }
2381 }
2382 }
2383
2384 /* access from pagesel.c if user marks or unmarks a page in the page list */
2385 void
2386 notify_print_dialog_have_marked(void)
2387 {
2388 Widget print_shell, save_shell;
2389 static pageRadioT save_value_bak = PAGE_ALL;
2390 static pageRadioT print_value_bak = PAGE_ALL;
2391
2392 /* notify *both* windows, save and print, but only if they already exist */
2393 if (get_widget_by_name(&print_shell, globals.widgets.top_level, Xdvi_PRINT_SHELL_NAME, False))
2394 notify_dialog(print_shell, &save_value_bak);
2395
2396 if (get_widget_by_name(&save_shell, globals.widgets.top_level, Xdvi_SAVE_SHELL_NAME, False))
2397 notify_dialog(save_shell, &print_value_bak);
2398 }
2399
2400 /* main entry point: pop up the dialog */
2401 void
2402 save_or_print_callback(struct save_or_print_info *info)
2403 {
2404 if (info->shell != NULL && window_is_mapped(XtWindow(info->shell), DISP)) {
2405 XBell(DISP, 0);
2406 XRaiseWindow(DISP, XtWindow(info->shell));
2407 return;
2408 }
2409
2410 if (info->act == FILE_PRINT && printlog_raise_active(info)) {
2411 XBell(DISP, 0);
2412 return;
2413 }
2414
2415 if (globals.dvi_file.bak_fp == NULL) {
2416 popup_message(globals.widgets.top_level,
2417 MSG_ERR,
2418 NULL,
2419 "Empty or incomplete DVI file. "
2420 "Please select a new DVI file via File -> Open.");
2421 return;
2422 }
2423
2424 if (info->shell == NULL) { /* first time */
2425 get_initial_values(info);
2426 #ifdef MOTIF
2427 motif_create_dialog(info);
2428 #else
2429 xaw_create_dialog(info);
2430 #endif
2431 /* fprintf(stderr, "SHELL IS: %p\n", (void *)info->shell); */
2432 set_check_page_range(info, False); /* set initial values */
2433
2434 if (info->act == FILE_SAVE)
2435 update_dvips_options_sensitivity(resource.default_saving_format, info->shell);
2436 }
2437 else {
2438 set_check_page_range(info, False); /* was: True, use False to reset values to current page */
2439 }
2440
2441 /* map the dialog */
2442 XtPopup(info->shell, XtGrabNone);
2443 }
2444