Debian Codegraph @

texk/xdvik/gui/xm_toolbar.c

raw
1 /*
2 * Copyright (c) 2001 Marcin Dalecki
3 * Copyright (c) 2002-2004 the xdvik development team
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25 /*
26 * Tool bar implementation for the Motif widget set.
27 */
28
29 #include "xdvi-config.h"
30 #include "xdvi.h"
31
32 #include "pagesel.h"
33 #include "help-window.h"
34 #include "message-window.h"
35 #include "kpathsea/tex-file.h"
36 #include "kpathsea/expand.h"
37 #include "statusline.h"
38 #include "dvi-init.h"
39 #include "events.h"
40 #include "dvi-draw.h"
41 #include "xm_menu.h"
42 #include "xm_toolbar.h"
43 #include "util.h"
44 #include "x_util.h"
45 #include "Tip.h"
46
47 #include <stdlib.h>
48
49 #include "xdvi-config.h"
50 #include "c-openmx.h"
51
52 /* default toolbar pixmap file */
53 #include "pixmaps/toolbar.xpm"
54
55 #ifdef MOTIF
56 #include <X11/Intrinsic.h>
57 #include <X11/StringDefs.h>
58 #include <X11/Shell.h>
59
60 #include <Xm/Xm.h>
61 #include <Xm/RowColumn.h>
62 #include <Xm/Frame.h>
63 #include <Xm/PushB.h>
64 #include <Xm/Form.h>
65 #include <Xm/Separator.h>
66 #include <Xm/CascadeB.h>
67 #include <Xm/MainW.h>
68 #include <Xm/DialogS.h>
69 #include <Xm/MessageB.h>
70 #include <Xm/Label.h>
71
72 #if defined(HAVE_X11_XPM_H)
73 # include <X11/xpm.h>
74 #elif defined(HAVE_XPM_H)
75 # include <xpm.h>
76 #elif defined(HAVE_XM_XPMP_H)
77 # include <Xm/XpmP.h>
78 #endif
79
80 #include <sys/stat.h>
81
82 /*
83 * The following needs to be keept in sync with the actual pixmap sizes in
84 * tools.xpm. When editing the pixmaps, take care that the symbolic names
85 * are not messed up (e.g. it works with "pixmap" from ftp.x11.org, but
86 * not with xv).
87 */
88
89 #define PIXMAP_WIDTH 18
90 #define PIXMAP_HEIGHT 18
91 /* #define PIXMAP_COUNT 13 */
92
93
94 static Widget tool_bar_frame;
95
96 #define SEP_CHAR ':' /* character separating entries in translations lines */
97
98 void
99 toggle_scrollbars(void)
100 {
101 Widget x_bar = XtNameToWidget(globals.widgets.main_window, "HorScrollBar");
102 Widget y_bar = XtNameToWidget(globals.widgets.main_window, "VertScrollBar");
103
104 if ((resource.expert_mode & XPRT_SHOW_SCROLLBARS) == 0) {
105 XtUnmanageChild(x_bar);
106 XtUnmanageChild(y_bar);
107 }
108 else {
109 XtManageChild(x_bar);
110 XtManageChild(y_bar);
111 }
112
113 set_menu(&resource.expert_mode, Act_set_expert_mode, check_resource_expert);
114 }
115
116 void
117 toggle_toolbar(void)
118 {
119 #if !HAVE_XPM
120 statusline_error(STATUS_LONG,
121 "Compiled without XPM support; no toolbar available.");
122 #else
123 if (resource.toolbar_unusable) {
124 statusline_error(STATUS_LONG,
125 "Toolbar pixmap file not found; toolbar is disabled.");
126 return;
127 }
128
129 if ((resource.expert_mode & XPRT_SHOW_TOOLBAR) == 0)
130 XtUnmanageChild(tool_bar_frame);
131 else
132 XtManageChild(tool_bar_frame);
133
134 set_menu(&resource.expert_mode, Act_set_expert_mode, check_resource_expert);
135
136 #endif /* not HAVE_XPM */
137 }
138
139 typedef enum { TB_BUTTON, TB_SEPARATOR } toolbarButtonT;
140
141 /* global array of button infos */
142 static struct toolbar_button_info {
143 Widget button;
144 toolbarButtonT type;
145 char *tip;
146 } *toolbar_buttons = NULL;
147
148 #if HAVE_XPM /* remainder of file: toolbar is disabled without XPM support */
149
150 /* to save current values to */
151 static Pixel m_background = 0, m_top_shadow_color = 0, m_bottom_shadow_color = 0;
152
153 /* to save resource value of XmNshadowThickness before overriding it if
154 toolbar_buttons_raised is false; it will be used to raise the button on enter:
155 */
156 static int m_shadow_thickness;
157 extern Boolean get_int_arg(String *param, Cardinal *num_params, int *res);
158
159 /* indexes for named colors */
160 enum {
161 BACKGROUND,
162 FOREGROUND,
163 BOTTOM_SHADOW,
164 TOP_SHADOW,
165 HIGHLIGHT
166 };
167
168 /* save info about buttons that need to change state */
169 static struct state_buttons_info {
170 Widget back_button; /* insensitive when current page is last page */
171 Widget zoom_in_button; /* insensitive when shrinkfactor is 1 */
172 Widget forward_button; /* insensitive when current page is first page */
173 Widget hyperref_back_button; /* insensitive when at end of href history */
174 Widget hyperref_forward_button; /* insensitive when at begin of href history */
175 } m_button_info;
176
177 static XtCallbackRec command_call[] = {
178 { handle_command, NULL },
179 { NULL, NULL },
180 };
181
182 static void
183 set_button_sensitivity(Widget w, Boolean sensitive)
184 {
185 if (w == NULL) { /* if button hadn't been initialized properly */
186 return;
187 }
188 XtVaSetValues(w, XmNsensitive, sensitive, NULL);
189 /* also remove `raised' property on mouse over */
190 if (!sensitive && !resource.toolbar_buttons_raised) {
191 Dimension x, y, wd, ht;
192 Pixel foreground;
193 static GC backgroundGC = NULL;
194 if (!XtIsRealized(w))
195 return;
196 XtVaGetValues(w,
197 XmNx, &x,
198 XmNy, &y,
199 XmNwidth, &wd,
200 XmNheight, &ht,
201 NULL);
202 if (backgroundGC == NULL) {
203 XtVaGetValues(w, XmNforeground, &foreground, NULL);
204 backgroundGC = set_or_make_gc(NULL, GXcopy, m_background, foreground);
205 }
206 XtVaSetValues(w,
207 XmNtopShadowColor, m_background,
208 XmNbottomShadowColor, m_background,
209 NULL);
210 #if 0
211 fprintf(stderr, "drawing on %d,%d\n", x, y);
212 XFillRectangle(XtDisplay(w), XtWindow(w), globals.gc.high, /* backgroundGC, */
213 x, y, wd, ht);
214 #endif
215 }
216 XSync(DISP, False);
217 }
218
219 void
220 tb_check_navigation_sensitivity(int pageno)
221 {
222 if (!XtIsRealized(globals.widgets.top_level) || (resource.expert_mode & XPRT_SHOW_TOOLBAR) == 0)
223 return;
224
225 set_button_sensitivity(m_button_info.forward_button, pageno == total_pages - 1 ? False : True);
226 set_button_sensitivity(m_button_info.back_button, pageno == 0 ? False : True);
227 }
228
229 void
230 tb_set_htex_back_sensitivity(Boolean sensitive)
231 {
232 if (!XtIsRealized(globals.widgets.top_level) || (resource.expert_mode & XPRT_SHOW_TOOLBAR) == 0)
233 return;
234 set_button_sensitivity(m_button_info.hyperref_back_button, sensitive);
235 }
236
237 void
238 tb_set_htex_forward_sensitivity(Boolean sensitive)
239 {
240 if (!XtIsRealized(globals.widgets.top_level) || (resource.expert_mode & XPRT_SHOW_TOOLBAR) == 0)
241 return;
242
243 set_button_sensitivity(m_button_info.hyperref_forward_button, sensitive);
244 }
245
246 void
247 tb_set_pagehist_back_sensitivity(Boolean sensitive)
248 {
249 if (!XtIsRealized(globals.widgets.top_level) || (resource.expert_mode & XPRT_SHOW_TOOLBAR) == 0)
250 return;
251 set_button_sensitivity(m_button_info.hyperref_back_button, sensitive);
252 }
253
254 void
255 tb_set_pagehist_forward_sensitivity(Boolean sensitive)
256 {
257 if (!XtIsRealized(globals.widgets.top_level) || (resource.expert_mode & XPRT_SHOW_TOOLBAR) == 0)
258 return;
259
260 set_button_sensitivity(m_button_info.hyperref_forward_button, sensitive);
261 }
262
263 void
264 tb_set_zoom_sensitivity(Boolean sensitive)
265 {
266 if (!XtIsRealized(globals.widgets.top_level) || (resource.expert_mode & XPRT_SHOW_TOOLBAR) == 0)
267 return;
268
269 set_button_sensitivity(m_button_info.zoom_in_button, sensitive);
270 }
271
272 #if 0
273 static void
274 search_callback(Widget w,
275 XtPointer client_data,
276 XmAnyCallbackStruct *call_data)
277 {
278 UNUSED(w);
279 UNUSED(client_data);
280 UNUSED(call_data);
281
282 popup_message(globals.widgets.top_level,
283 MSG_ERR, NULL, "Sorry, not yet implemented");
284 }
285 #endif /* 0 */
286
287
288
289 /*
290 * If successful, this extracts a square pixmap from resource.toolbar_pixmap_file
291 * and returns True.
292 * In this case, the pixmap `sen' will contain the actual
293 * pixmap and `insen' will contain a drawn pixmap for the insensitive state of
294 * the button. We are emulating a shaded state derived from the monochrome 'm'
295 * color attributes of the XPM.
296 *
297 * If unsuccessful, it returns False, and the Pixmap pointers are undefined.
298 */
299
300 static Boolean
301 create_pixmap(Widget parent, int iconidx, Pixmap *sen, Pixmap *insen)
302 {
303 static Boolean first_time = True;
304 static Window rootWindow;
305 static XpmColorSymbol color[5] = {
306 {"none", "none", 0},
307 {"iconColor1", NULL, 0},
308 {"bottomShadowColor", NULL, 0},
309 {"topShadowColor", NULL, 0},
310 {"selectColor", NULL, 0}
311 };
312 static Pixmap tools_map;
313 static Pixmap tools_mask;
314 static Pixmap shade_map;
315 static Pixmap shade_mask;
316
317 int status = 0;
318 XpmAttributes attr;
319 Pixmap map;
320 Pixmap mask;
321 static String pixmap_file_path = NULL; /* note: never free()d */
322
323 ASSERT(iconidx >= 0, "index must be positive");
324
325 if (first_time) {
326 /* FIXME: We use a dummy window here to get the correct depth/visual for the
327 pixmap creation (cant use globals.widgets.top_level since it's not realized
328 yet and realizing it now would result in wrong dimensions) ... */
329 Widget dummy = XtVaAppCreateShell("xdvi", "Xdvi",
330 transientShellWidgetClass, DISP,
331 XtNdepth, G_depth,
332 XtNvisual, G_visual,
333 XtNcolormap, G_colormap,
334 XtNmappedWhenManaged, False,
335 NULL);
336 XtRealizeWidget(dummy); /* note: doesn't pop it up */
337 rootWindow = XtWindow(dummy);
338 ASSERT(rootWindow != 0, "");
339 XtVaGetValues(parent,
340 XmNbackground, &color[BACKGROUND].pixel,
341 XmNforeground, &color[FOREGROUND].pixel,
342 XmNbottomShadowColor, &color[BOTTOM_SHADOW].pixel,
343 XmNtopShadowColor, &color[TOP_SHADOW].pixel,
344 XmNhighlightColor, &color[HIGHLIGHT].pixel,
345 NULL);
346 /* try to locate the XPM file with the toolbar pixmaps */
347 pixmap_file_path = XtResolvePathname(DISP,
348 "pixmaps",
349 resource.toolbar_pixmap_file,
350 (String)NULL, /* suffix */
351 (String)NULL, /* use default path */
352 (Substitution)NULL, /* substitutions */
353 0, /* number of substitutions */
354 (XtFilePredicate)NULL); /* return True iff file exists */
355
356 TRACE_GUI((stderr, "pixmap file search via XtResolvePathname: %s => %s",
357 resource.toolbar_pixmap_file, pixmap_file_path ? (char*)pixmap_file_path : "<NULL>"));
358 if (pixmap_file_path == NULL) {
359 pixmap_file_path = (String)kpse_find_file(resource.toolbar_pixmap_file,
360 kpse_program_text_format,
361 0);
362 TRACE_GUI((stderr,
363 "pixmap file search via kpse_find_file: %s => %s",
364 resource.toolbar_pixmap_file,
365 pixmap_file_path ? (char*)pixmap_file_path : "<NULL>"));
366 if (pixmap_file_path == NULL) {
367 TRACE_GUI((stderr, "No installed toolbar pixmap found, using built-in pixmap."));
368 }
369 }
370 }
371
372 /* Setup the color subsititution table */
373 attr.valuemask = XpmColorSymbols | XpmCloseness | XpmColormap | XpmDepth | XpmVisual;
374 attr.closeness = 65535; /* accuracy isn't crucial */
375 attr.colorsymbols = color;
376 attr.numsymbols = XtNumber(color);
377 attr.visual = G_visual;
378 attr.colormap = G_colormap;
379 attr.depth = G_depth;
380
381 /* Create the "sensitive" pixmap */
382 if (!tools_map) {
383 if (pixmap_file_path != NULL) {
384 status = XpmReadFileToPixmap(XtDisplay(globals.widgets.top_level), rootWindow,
385 pixmap_file_path, &tools_map, &tools_mask, &attr);
386 }
387 else {
388 status = XpmCreatePixmapFromData(XtDisplay(globals.widgets.top_level), rootWindow,
389 (char **)toolbar_xpm, &tools_map, &tools_mask, &attr);
390 }
391 }
392 else
393 status = XpmSuccess;
394
395 map = tools_map;
396 mask = tools_mask;
397
398 if (status == XpmSuccess) {
399 static Pixmap tmp_mask;
400 static GC gc;
401
402 if (first_time) {
403 tmp_mask = XCreatePixmap(XtDisplay(globals.widgets.top_level), rootWindow, PIXMAP_WIDTH, PIXMAP_HEIGHT, 1);
404 gc = XCreateGC(XtDisplay(globals.widgets.top_level), tmp_mask, 0, NULL);
405 }
406 XCopyArea(XtDisplay(globals.widgets.top_level),
407 mask, tmp_mask, gc, iconidx * PIXMAP_WIDTH, 0, PIXMAP_WIDTH, PIXMAP_HEIGHT, 0, 0);
408
409 mask = tmp_mask;
410 }
411 else { /* something went wrong */
412 popup_message(globals.widgets.top_level,
413 MSG_ERR,
414 "Something's wrong with your XPM file - "
415 "try to load it into an image editor and fix the problem.",
416 "Xpm error: %s - switching toolbar off.",
417 XpmGetErrorString(status));
418 sen = insen = NULL;
419 resource.expert_mode ^= XPRT_SHOW_TOOLBAR;
420 return False;
421 }
422
423 XpmFreeAttributes(&attr);
424
425 if (map != 0) {
426 static GC back_gc, bots_gc;
427
428 if (first_time) {
429 XGCValues gcvalues;
430
431 gcvalues.foreground = color[BACKGROUND].pixel;
432 back_gc = XCreateGC(XtDisplay(globals.widgets.top_level), rootWindow, GCForeground, &gcvalues);
433
434 gcvalues.foreground = color[BOTTOM_SHADOW].pixel;
435 bots_gc = XCreateGC(XtDisplay(globals.widgets.top_level), rootWindow, GCForeground, &gcvalues);
436 }
437
438 /* Need to create new Pixmaps with the mask applied. */
439 XSetClipMask(XtDisplay(globals.widgets.top_level), bots_gc, mask);
440
441 /* Create the "sensitive" pixmap. */
442 *sen = XCreatePixmap(XtDisplay(globals.widgets.top_level), rootWindow, PIXMAP_WIDTH, PIXMAP_HEIGHT,
443 G_depth);
444 XFillRectangle(XtDisplay(globals.widgets.top_level), *sen, back_gc, 0, 0, PIXMAP_WIDTH, PIXMAP_HEIGHT);
445 if (iconidx != -1)
446 XCopyArea(XtDisplay(globals.widgets.top_level), map, *sen, bots_gc,
447 iconidx * PIXMAP_WIDTH, 0, PIXMAP_WIDTH, PIXMAP_HEIGHT, 0, 0);
448 else
449 XCopyArea(XtDisplay(globals.widgets.top_level), map, *sen, bots_gc,
450 0, 0, PIXMAP_WIDTH, PIXMAP_HEIGHT, 0, 0);
451
452 if (iconidx == -1)
453 XFreePixmap(XtDisplay(globals.widgets.top_level), map);
454
455 /* Create the "insensitive" pixmap. */
456 if (insen != NULL) {
457 Pixmap map;
458 Pixmap mask;
459
460 attr.valuemask = XpmColorSymbols | XpmCloseness | XpmColorKey | XpmColormap | XpmDepth | XpmVisual;
461 attr.closeness = 65535; /* accuracy isn't crucial */
462 attr.colorsymbols = color;
463 attr.numsymbols = XtNumber(color);
464 attr.color_key = XPM_MONO;
465 attr.visual = G_visual;
466 attr.colormap = G_colormap;
467 attr.depth = G_depth;
468
469
470 if (!shade_map) {
471 if (pixmap_file_path != NULL) {
472 status = XpmReadFileToPixmap(XtDisplay(globals.widgets.top_level), rootWindow,
473 pixmap_file_path, &shade_map, &shade_mask, &attr);
474 }
475 else {
476 status = XpmCreatePixmapFromData(XtDisplay(globals.widgets.top_level), rootWindow,
477 (char **)toolbar_xpm, &shade_map, &shade_mask, &attr);
478 }
479 }
480 else
481 status = XpmSuccess;
482
483 map = shade_map;
484 mask = shade_mask;
485
486 if (status == XpmSuccess) {
487 static Pixmap tmp_mask;
488 static GC gc;
489
490 if (first_time) {
491 tmp_mask = XCreatePixmap(XtDisplay(globals.widgets.top_level), rootWindow,
492 PIXMAP_WIDTH, PIXMAP_HEIGHT, 1);
493 gc = XCreateGC(XtDisplay(globals.widgets.top_level), tmp_mask, 0, NULL);
494 }
495
496 XCopyArea(XtDisplay(globals.widgets.top_level), mask, tmp_mask, gc,
497 iconidx * PIXMAP_WIDTH, 0,
498 PIXMAP_WIDTH, PIXMAP_HEIGHT,
499 0, 0);
500
501 mask = tmp_mask;
502 }
503 else { /* something went wrong */
504 popup_message(globals.widgets.top_level,
505 MSG_ERR,
506 "Something's wrong with your XPM file - "
507 "try to load it into an image editor and fix the problem.",
508 "Xpm error: %s - switching toolbar off.",
509 XpmGetErrorString(status));
510 sen = insen = NULL;
511 resource.expert_mode ^= XPRT_SHOW_TOOLBAR;
512 return False;
513 }
514
515 if (mask != 0) {
516 static GC tops_gc;
517
518 if (first_time) {
519 XGCValues gcvalues;
520
521 gcvalues.foreground = color[TOP_SHADOW].pixel;
522 tops_gc = XCreateGC(XtDisplay(globals.widgets.top_level), rootWindow, GCForeground, &gcvalues);
523 }
524
525 /* Need to create new Pixmaps with the mask applied. */
526 XSetClipMask(XtDisplay(globals.widgets.top_level), bots_gc, mask);
527 XSetClipMask(XtDisplay(globals.widgets.top_level), tops_gc, mask);
528 XSetClipOrigin(XtDisplay(globals.widgets.top_level), tops_gc, 1, 1);
529
530 *insen = XCreatePixmap(XtDisplay(globals.widgets.top_level), rootWindow, PIXMAP_WIDTH, PIXMAP_HEIGHT,
531 G_depth);
532
533 XFillRectangle(XtDisplay(globals.widgets.top_level), *insen, back_gc, 0, 0,
534 PIXMAP_WIDTH, PIXMAP_HEIGHT);
535 XFillRectangle(XtDisplay(globals.widgets.top_level), *insen, tops_gc, 1, 1,
536 PIXMAP_WIDTH - 1, PIXMAP_HEIGHT - 1);
537 XFillRectangle(XtDisplay(globals.widgets.top_level), *insen, bots_gc, 0, 0, PIXMAP_WIDTH, PIXMAP_HEIGHT);
538
539 if (iconidx == -1)
540 XFreePixmap(XtDisplay(globals.widgets.top_level), map);
541 }
542
543 XpmFreeAttributes(&attr);
544 }
545 }
546
547 first_time = False;
548 return True;
549 }
550
551 /*
552 * If successful, this returns True, and sets *button to a button with
553 * a square pixmap on it (which is cut out form `index' position in
554 * resource.toolbar_pixmap_file). If unsuccessful, it returns false,
555 * and *button is undefined.
556 */
557 static Boolean
558 create_toolbar_button(Widget parent, Widget *button,
559 const Pixmap *image_sens, const Pixmap *image_insens)
560 {
561 Boolean sensitive = True; /* dummy */
562 *button = XtVaCreateManagedWidget("button", xmPushButtonWidgetClass, parent,
563 XmNhighlightOnEnter, True,
564 XmNlabelPixmap, *image_sens,
565 XmNlabelInsensitivePixmap, *image_insens,
566 XmNsensitive, sensitive,
567 XmNlabelType, XmPIXMAP,
568 NULL);
569 XtVaGetValues(*button, XmNshadowThickness, &m_shadow_thickness, NULL);
570 if (!resource.toolbar_buttons_raised) {
571 if (m_background == 0) { /* initialize values */
572 XtVaGetValues(*button,
573 /* should we rather get background of parent widget? */
574 XmNbackground, &m_background,
575 XmNtopShadowColor, &m_top_shadow_color,
576 XmNbottomShadowColor, &m_bottom_shadow_color,
577 NULL);
578 }
579 /* remove shadows, setting them later when mouse enters button */
580 XtVaSetValues(*button,
581 XmNtopShadowColor, m_background,
582 XmNbottomShadowColor, m_background,
583 NULL);
584 }
585 return True;
586 }
587
588 static void
589 create_toolbar_separator(Widget parent, Widget *separator, int width)
590 {
591 *separator = XtVaCreateManagedWidget("toolbarSeparator",
592 xmSeparatorWidgetClass, parent,
593 XmNseparatorType, XmNO_LINE,
594 XmNminWidth, width,
595 XmNwidth, width,
596 XmNorientation, XmVERTICAL,
597 XmNleftAttachment, XmATTACH_WIDGET,
598 XmNrightAttachment, XmATTACH_WIDGET,
599 XmNtopAttachment, XmATTACH_FORM,
600 XmNbottomAttachment, XmATTACH_FORM,
601 NULL);
602 }
603
604 /* taken from shadow_trick in WlToolBar.c, mgv-3.1.5:
605 Get a raised behaviour for armed buttons, by explicitly setting
606 a shadow on entry and removing it on leave.
607 */
608 static void
609 enter_leave(Widget w, XtPointer closure, XEvent *event, Boolean *cont)
610 {
611 char *tooltip = (char *)closure;
612 XCrossingEvent *ev = (XCrossingEvent *)event;
613 static Boolean entered = False; /* to skip double leaves */
614
615 UNUSED(cont);
616
617 if (ev->type == EnterNotify) {
618 if (!resource.toolbar_buttons_raised) {
619 /* draw shadows */
620 XtVaSetValues(w,
621 XmNtopShadowColor, m_top_shadow_color,
622 XmNbottomShadowColor, m_bottom_shadow_color,
623 NULL);
624 }
625 entered = True;
626 if (resource.tooltips_in_statusline) {
627 statusline_info(STATUS_SHORT, tooltip);
628 }
629 }
630 else if (ev->type == LeaveNotify && !resource.toolbar_buttons_raised) {
631 /* remove shadows */
632 if (!entered)
633 return;
634
635 XtVaSetValues(w,
636 XmNtopShadowColor, m_background,
637 XmNbottomShadowColor, m_background,
638 NULL);
639 entered = False;
640 }
641 }
642
643 /*
644 save info about special buttons in m_button_info
645 (see definition of that for details)
646 */
647 static void
648 button_info_save(struct xdvi_action *action, Widget w)
649 {
650 if (action->proc == Act_back_page
651 && action->num_params > 0 && strcmp(action->params[0], "1") == 0) {
652 m_button_info.back_button = w;
653 }
654 else if (action->proc == Act_forward_page
655 && action->num_params > 0 && strcmp(action->params[0], "1") == 0) {
656 m_button_info.forward_button = w;
657 }
658 else if (action->proc == Act_pagehistory_back) {
659 set_button_sensitivity(w, False);
660 m_button_info.hyperref_back_button = w;
661 }
662 else if (action->proc == Act_pagehistory_forward) {
663 set_button_sensitivity(w, False);
664 m_button_info.hyperref_forward_button = w;
665 }
666 else if (action->proc == Act_set_shrink_factor
667 && action->num_params > 0 && action->params[0][0] == '+') {
668 m_button_info.zoom_in_button = w;
669 }
670 }
671
672 #endif /* HAVE_XPM */
673
674 /*
675 * Create a toolbar with buttons, return toolbar widget.
676 */
677 Widget
678 create_toolbar(Widget parent, Widget menu_bar)
679 {
680 #if HAVE_XPM
681 size_t alloc_len = 0, n;
682 size_t alloc_step = 16;
683 const char *c_ptr, *e_ptr;
684 #endif /* HAVE_XPM */
685 Widget tool_bar;
686 resource.toolbar_unusable = False;
687
688 tool_bar_frame = XtVaCreateWidget("toolBarFrame",
689 xmFrameWidgetClass, parent,
690 XmNshadowType, XmSHADOW_OUT,
691 XmNleftAttachment, XmATTACH_FORM,
692 XmNrightAttachment, XmATTACH_FORM,
693 XmNtopAttachment, XmATTACH_WIDGET,
694 XmNtopWidget, menu_bar,
695 NULL);
696
697 tool_bar = XtVaCreateManagedWidget("toolBar",
698 xmRowColumnWidgetClass, tool_bar_frame,
699 XmNchildType, XmFRAME_WORKAREA_CHILD,
700 XmNrowColumnType, XmWORK_AREA,
701 XmNorientation, XmHORIZONTAL,
702 XmNtraversalOn, False,
703 XmNisHomogeneous, False,
704 XmNpacking, XmPACK_TIGHT,
705 XmNspacing, 0, /* override to use SEPARATOR(n) instead */
706 XmNadjustLast, True,
707 NULL);
708
709 #if HAVE_XPM
710 /* parse toolbar_translations, create the widgets and assign the actions */
711 for (n = 0, c_ptr = resource.toolbar_translations;
712 c_ptr != NULL && *c_ptr != '\0';
713 c_ptr = e_ptr, n++) {
714 char **line_items = NULL;
715 int extra_space;
716 size_t len, curr, item_count = 0;
717
718 if ((e_ptr = strchr(c_ptr, '\n')) == NULL
719 /* ... and in case last line doesn't end with \n ... */
720 && (e_ptr = strchr(c_ptr, '\0')) == NULL) {
721 break;
722 }
723
724 if (e_ptr == c_ptr) {
725 XDVI_WARNING((stderr, "Skipping empty line in toolbarTranslations resource."));
726 e_ptr++;
727 continue;
728 }
729 len = e_ptr - c_ptr;
730 TRACE_GUI((stderr, "LEN %lu: |%.*s|", (unsigned long)len, (int)len, c_ptr));
731
732 line_items = split_line(c_ptr, SEP_CHAR, 0, len, &item_count);
733
734 if (globals.debug & DBG_GUI) {
735 int k;
736 for (k = 0; line_items[k] != NULL; k++) {
737 fprintf(stderr, "ITEM %d of %lu: |%s|\n", k, (unsigned long)item_count, line_items[k]);
738 }
739 }
740 while (alloc_len <= n + 1) {
741 alloc_len += alloc_step;
742 toolbar_buttons = xrealloc(toolbar_buttons, alloc_len * sizeof *toolbar_buttons);
743 }
744
745 if (item_count == 1 && sscanf(line_items[0], "SPACER(%d)", &extra_space) == 1) {
746 TRACE_GUI((stderr, "creating spacer of witdh %d at %lu", extra_space, (unsigned long)n));
747 create_toolbar_separator(tool_bar, &(toolbar_buttons[n].button), extra_space);
748 toolbar_buttons[n].type = TB_SEPARATOR;
749 }
750 else if (item_count == 4) {
751 Pixmap sens, insens;
752 int idx = strtoul(line_items[0], (char **)NULL, 10);
753 struct xdvi_action *action;
754
755 sens = insens = 0; /* make compiler happy ... */
756
757 TRACE_GUI((stderr, "creating pixmap at %d", idx));
758 if (!create_pixmap(tool_bar, idx, &sens, &insens)) {
759 free(toolbar_buttons);
760 toolbar_buttons = NULL;
761 break;
762 }
763 TRACE_GUI((stderr, "creating button %ld", (unsigned long)n));
764 if (!create_toolbar_button(tool_bar, &(toolbar_buttons[n].button), &sens, &insens)) {
765 free(toolbar_buttons);
766 toolbar_buttons = NULL;
767 break;
768 }
769 toolbar_buttons[n].type = TB_BUTTON;
770
771 if (compile_action(line_items[3], &action)) {
772 char *long_tooltip = xstrdup(line_items[1]);
773 toolbar_buttons[n].tip = xstrdup(line_items[2]);
774 /* char *short_tooltip = xstrdup(line_items[2]); */
775 command_call[0].closure = (XtPointer) action;
776
777 /*
778 eventually save this widget in list of `special' buttons
779 that need to toggle between sensitive/insensitive
780 */
781 button_info_save(action, toolbar_buttons[n].button);
782
783 XtVaSetValues(toolbar_buttons[n].button, XmNactivateCallback, (XtArgVal)command_call, NULL);
784 XtAddEventHandler(toolbar_buttons[n].button,
785 EnterWindowMask | LeaveWindowMask,
786 False,
787 enter_leave,
788 long_tooltip);
789 }
790 else {
791 XDVI_WARNING((stderr, "Invalid action \"%s\" in toolbarTranslations resource:\n\"%.*s\"",
792 line_items[3], (int)len, c_ptr));
793 }
794 }
795 else {
796 XDVI_WARNING((stderr, "Skipping malformed line \"%.*s\" in toolbarTranslations resource "
797 "(%lu instead of 4 items).",
798 (int)len, c_ptr, (unsigned long)item_count));
799 toolbar_buttons[n].button = 0;
800 }
801
802 for (curr = 0; curr < item_count; curr++) {
803 free(line_items[curr]);
804 }
805 free(line_items);
806 line_items = NULL;
807
808 if (*e_ptr != '\0')
809 e_ptr++;
810 }
811 #else
812 if ((resource.expert_mode & XPRT_SHOW_SCROLLBARS) != 0) {
813 XDVI_WARNING((stderr, "This version has been compiled without XPM support. "
814 "Disabling the toolbar, which needs XPM."));
815 }
816 #endif /* HAVE_XPM */
817
818 if (toolbar_buttons == NULL) {
819 resource.toolbar_unusable = True;
820 resource.expert_mode ^= XPRT_SHOW_TOOLBAR;
821 }
822 else {
823 #if HAVE_XPM
824 toolbar_buttons[n].button = 0; /* terminate info */
825 #endif
826 }
827 return tool_bar;
828 }
829
830 /*
831 * Add the tips to the toolbar. This has to be done after the toolbar has been
832 * realized.
833 */
834 void
835 create_tips(Widget toplevel)
836 {
837 #if HAVE_XPM
838 Widget tip_shell;
839
840 int i;
841
842 if (resource.toolbar_unusable) /* don't create tips in this case */
843 return;
844
845 tip_shell = XtVaCreatePopupShell("tipShell", tipWidgetClass,
846 toplevel, XtNwidth, 1, XtNheight, 1,
847 NULL);
848
849 for (i = 0; toolbar_buttons[i].button != 0; i++) {
850 if (toolbar_buttons[i].type == TB_BUTTON) {
851 TipAddWidget(tip_shell, toolbar_buttons[i].button, toolbar_buttons[i].tip);
852 }
853 }
854 #else
855 UNUSED(toplevel);
856 #endif /* HAVE_XPM */
857 }
858
859
860 #else /* MOTIF */
861 /* silence "empty compilation unit" and "`toolbar_xpm' defined but not used" warnings */
862 static void bar(void); static void foo(void) { UNUSED(toolbar_xpm); bar(); } static void bar(void) { foo(); }
863 #endif /* MOTIF */