http://www.jwz.org/xscreensaver/xscreensaver-5.13.tar.gz
[xscreensaver] / driver / splash.c
1 /* xscreensaver, Copyright (c) 1991-2008 Jamie Zawinski <jwz@netscape.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #include <X11/Intrinsic.h>
17
18 #include "xscreensaver.h"
19 #include "resources.h"
20
21 #undef MAX
22 #define MAX(a,b) ((a)>(b)?(a):(b))
23
24 void
25 draw_shaded_rectangle (Display *dpy, Window window,
26                        int x, int y,
27                        int width, int height,
28                        int thickness,
29                        unsigned long top_color,
30                        unsigned long bottom_color)
31 {
32   XPoint points[4];
33   XGCValues gcv;
34   GC gc1, gc2;
35   if (thickness == 0) return;
36
37   gcv.foreground = top_color;
38   gc1 = XCreateGC (dpy, window, GCForeground, &gcv);
39   gcv.foreground = bottom_color;
40   gc2 = XCreateGC (dpy, window, GCForeground, &gcv);
41
42   points [0].x = x;
43   points [0].y = y;
44   points [1].x = x + width;
45   points [1].y = y;
46   points [2].x = x + width - thickness;
47   points [2].y = y + thickness;
48   points [3].x = x;
49   points [3].y = y + thickness;
50   XFillPolygon (dpy, window, gc1, points, 4, Convex, CoordModeOrigin);
51
52   points [0].x = x;
53   points [0].y = y + thickness;
54   points [1].x = x;
55   points [1].y = y + height;
56   points [2].x = x + thickness;
57   points [2].y = y + height - thickness;
58   points [3].x = x + thickness;
59   points [3].y = y + thickness;
60   XFillPolygon (dpy, window, gc1, points, 4, Convex, CoordModeOrigin);
61
62   points [0].x = x + width;
63   points [0].y = y;
64   points [1].x = x + width - thickness;
65   points [1].y = y + thickness;
66   points [2].x = x + width - thickness;
67   points [2].y = y + height - thickness;
68   points [3].x = x + width;
69   points [3].y = y + height - thickness;
70   XFillPolygon (dpy, window, gc2, points, 4, Convex, CoordModeOrigin);
71
72   points [0].x = x;
73   points [0].y = y + height;
74   points [1].x = x + width;
75   points [1].y = y + height;
76   points [2].x = x + width;
77   points [2].y = y + height - thickness;
78   points [3].x = x + thickness;
79   points [3].y = y + height - thickness;
80   XFillPolygon (dpy, window, gc2, points, 4, Convex, CoordModeOrigin);
81
82   XFreeGC (dpy, gc1);
83   XFreeGC (dpy, gc2);
84 }
85
86
87 int
88 string_width (XFontStruct *font, char *s)
89 {
90   return XTextWidth(font, s, strlen(s));
91 }
92
93
94 static void update_splash_window (saver_info *si);
95 static void draw_splash_window (saver_info *si);
96 static void destroy_splash_window (saver_info *si);
97 static void unsplash_timer (XtPointer closure, XtIntervalId *id);
98
99 static void do_demo (saver_screen_info *ssi);
100 #ifdef PREFS_BUTTON
101 static void do_prefs (saver_screen_info *ssi);
102 #endif /* PREFS_BUTTON */
103 static void do_help (saver_screen_info *ssi);
104
105
106 struct splash_dialog_data {
107
108   saver_screen_info *prompt_screen;
109   XtIntervalId timer;
110
111   Dimension width;
112   Dimension height;
113
114   char *heading_label;
115   char *body_label;
116   char *body2_label;
117   char *demo_label;
118 #ifdef PREFS_BUTTON
119   char *prefs_label;
120 #endif /* PREFS_BUTTON */
121   char *help_label;
122
123   XFontStruct *heading_font;
124   XFontStruct *body_font;
125   XFontStruct *button_font;
126
127   Pixel foreground;
128   Pixel background;
129   Pixel border;
130   Pixel button_foreground;
131   Pixel button_background;
132   Pixel shadow_top;
133   Pixel shadow_bottom;
134
135   Dimension logo_width;
136   Dimension logo_height;
137   Dimension internal_border;
138   Dimension shadow_width;
139
140   Dimension button_width, button_height;
141   Dimension demo_button_x, demo_button_y;
142 #ifdef PREFS_BUTTON
143   Dimension prefs_button_x, prefs_button_y;
144 #endif /* PREFS_BUTTON */
145   Dimension help_button_x, help_button_y;
146
147   Pixmap logo_pixmap;
148   Pixmap logo_clipmask;
149   int logo_npixels;
150   unsigned long *logo_pixels;
151
152   int pressed;
153 };
154
155
156 void
157 make_splash_dialog (saver_info *si)
158 {
159   int x, y, bw;
160   XSetWindowAttributes attrs;
161   unsigned long attrmask = 0;
162   splash_dialog_data *sp;
163   saver_screen_info *ssi;
164   Colormap cmap;
165   char *f;
166
167   if (si->sp_data)
168     return;
169   if (!si->prefs.splash_p ||
170       si->prefs.splash_duration <= 0)
171     return;
172
173   ssi = &si->screens[mouse_screen (si)];
174
175   if (!ssi || !ssi->screen)
176     return;    /* WTF?  Trying to splash while no screens connected? */
177
178   cmap = DefaultColormapOfScreen (ssi->screen);
179
180   sp = (splash_dialog_data *) calloc (1, sizeof(*sp));
181   sp->prompt_screen = ssi;
182
183   sp->heading_label = get_string_resource (si->dpy,
184                                            "splash.heading.label",
185                                            "Dialog.Label.Label");
186   sp->body_label = get_string_resource (si->dpy,
187                                         "splash.body.label",
188                                         "Dialog.Label.Label");
189   sp->body2_label = get_string_resource (si->dpy,
190                                          "splash.body2.label",
191                                          "Dialog.Label.Label");
192   sp->demo_label = get_string_resource (si->dpy,
193                                         "splash.demo.label",
194                                         "Dialog.Button.Label");
195 #ifdef PREFS_BUTTON
196   sp->prefs_label = get_string_resource (si->dpy,
197                                          "splash.prefs.label",
198                                         "Dialog.Button.Label");
199 #endif /* PREFS_BUTTON */
200   sp->help_label = get_string_resource (si->dpy,
201                                         "splash.help.label",
202                                         "Dialog.Button.Label");
203
204   if (!sp->heading_label)
205     sp->heading_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
206   if (!sp->body_label)
207     sp->body_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
208   if (!sp->body2_label)
209     sp->body2_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
210   if (!sp->demo_label) sp->demo_label = strdup("ERROR");
211 #ifdef PREFS_BUTTON
212   if (!sp->prefs_label) sp->prefs_label = strdup("ERROR");
213 #endif /* PREFS_BUTTON */
214   if (!sp->help_label) sp->help_label = strdup("ERROR");
215
216   /* Put the version number in the label. */
217   {
218     char *s = (char *) malloc (strlen(sp->heading_label) + 20);
219     sprintf(s, sp->heading_label, si->version);
220     free (sp->heading_label);
221     sp->heading_label = s;
222   }
223
224   f = get_string_resource (si->dpy, "splash.headingFont", "Dialog.Font");
225   sp->heading_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
226   if (!sp->heading_font) sp->heading_font = XLoadQueryFont (si->dpy, "fixed");
227   if (f) free (f);
228
229   f = get_string_resource(si->dpy, "splash.bodyFont", "Dialog.Font");
230   sp->body_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
231   if (!sp->body_font) sp->body_font = XLoadQueryFont (si->dpy, "fixed");
232   if (f) free (f);
233
234   f = get_string_resource(si->dpy, "splash.buttonFont", "Dialog.Font");
235   sp->button_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
236   if (!sp->button_font) sp->button_font = XLoadQueryFont (si->dpy, "fixed");
237   if (f) free (f);
238
239   sp->foreground = get_pixel_resource (si->dpy, cmap,
240                                        "splash.foreground",
241                                        "Dialog.Foreground");
242   sp->background = get_pixel_resource (si->dpy, cmap, 
243                                        "splash.background",
244                                        "Dialog.Background");
245   sp->border = get_pixel_resource (si->dpy, cmap, 
246                                        "splash.borderColor",
247                                        "Dialog.borderColor");
248
249   if (sp->foreground == sp->background)
250     {
251       /* Make sure the error messages show up. */
252       sp->foreground = BlackPixelOfScreen (ssi->screen);
253       sp->background = WhitePixelOfScreen (ssi->screen);
254     }
255
256   sp->button_foreground = get_pixel_resource (si->dpy, cmap,
257                                               "splash.Button.foreground",
258                                               "Dialog.Button.Foreground");
259   sp->button_background = get_pixel_resource (si->dpy, cmap,
260                                               "splash.Button.background",
261                                               "Dialog.Button.Background");
262   sp->shadow_top = get_pixel_resource (si->dpy, cmap,
263                                        "splash.topShadowColor",
264                                        "Dialog.Foreground");
265   sp->shadow_bottom = get_pixel_resource (si->dpy, cmap,
266                                           "splash.bottomShadowColor",
267                                           "Dialog.Background");
268
269   sp->logo_width = get_integer_resource (si->dpy, 
270                                          "splash.logo.width",
271                                          "Dialog.Logo.Width");
272   sp->logo_height = get_integer_resource (si->dpy, 
273                                           "splash.logo.height",
274                                           "Dialog.Logo.Height");
275   sp->internal_border = get_integer_resource (si->dpy, 
276                                               "splash.internalBorderWidth",
277                                               "Dialog.InternalBorderWidth");
278   sp->shadow_width = get_integer_resource (si->dpy, 
279                                            "splash.shadowThickness",
280                                            "Dialog.ShadowThickness");
281
282   if (sp->logo_width == 0)  sp->logo_width = 150;
283   if (sp->logo_height == 0) sp->logo_height = 150;
284   if (sp->internal_border == 0) sp->internal_border = 15;
285   if (sp->shadow_width == 0) sp->shadow_width = 4;
286
287   {
288     int direction, ascent, descent;
289     XCharStruct overall;
290
291     sp->width = 0;
292     sp->height = 0;
293
294     /* Measure the heading_label. */
295     XTextExtents (sp->heading_font,
296                   sp->heading_label, strlen(sp->heading_label),
297                   &direction, &ascent, &descent, &overall);
298     if (overall.width > sp->width) sp->width = overall.width;
299     sp->height += ascent + descent;
300
301     /* Measure the body_label. */
302     XTextExtents (sp->body_font,
303                   sp->body_label, strlen(sp->body_label),
304                   &direction, &ascent, &descent, &overall);
305     if (overall.width > sp->width) sp->width = overall.width;
306     sp->height += ascent + descent;
307
308     /* Measure the body2_label. */
309     XTextExtents (sp->body_font,
310                   sp->body2_label, strlen(sp->body2_label),
311                   &direction, &ascent, &descent, &overall);
312     if (overall.width > sp->width) sp->width = overall.width;
313     sp->height += ascent + descent;
314
315     {
316       Dimension w2 = 0, w3 = 0, w4 = 0;
317       Dimension h2 = 0, h3 = 0, h4 = 0;
318
319       /* Measure the Demo button. */
320       XTextExtents (sp->button_font,
321                     sp->demo_label, strlen(sp->demo_label),
322                     &direction, &ascent, &descent, &overall);
323       w2 = overall.width;
324       h2 = ascent + descent;
325
326 #ifdef PREFS_BUTTON
327       /* Measure the Prefs button. */
328       XTextExtents (sp->button_font,
329                     sp->prefs_label, strlen(sp->prefs_label),
330                     &direction, &ascent, &descent, &overall);
331       w3 = overall.width;
332       h3 = ascent + descent;
333 #else  /* !PREFS_BUTTON */
334       w3 = 0;
335       h3 = 0;
336 #endif /* !PREFS_BUTTON */
337
338       /* Measure the Help button. */
339       XTextExtents (sp->button_font,
340                     sp->help_label, strlen(sp->help_label),
341                     &direction, &ascent, &descent, &overall);
342       w4 = overall.width;
343       h4 = ascent + descent;
344
345       w2 = MAX(w2, w3); w2 = MAX(w2, w4);
346       h2 = MAX(h2, h3); h2 = MAX(h2, h4);
347
348       /* Add some horizontal padding inside the buttons. */
349       w2 += ascent;
350
351       w2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
352       h2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
353
354       sp->button_width = w2;
355       sp->button_height = h2;
356
357 #ifdef PREFS_BUTTON
358       w2 *= 3;
359 #else  /* !PREFS_BUTTON */
360       w2 *= 2;
361 #endif /* !PREFS_BUTTON */
362
363       w2 += ((ascent + descent) * 2);  /* for space between buttons */
364
365       if (w2 > sp->width) sp->width = w2;
366       sp->height += h2;
367     }
368
369     sp->width  += (sp->internal_border * 2);
370     sp->height += (sp->internal_border * 3);
371
372     if (sp->logo_height > sp->height)
373       sp->height = sp->logo_height;
374     else if (sp->height > sp->logo_height)
375       sp->logo_height = sp->height;
376
377     sp->logo_width = sp->logo_height;
378
379     sp->width += sp->logo_width;
380   }
381
382   attrmask |= CWOverrideRedirect; attrs.override_redirect = True;
383   attrmask |= CWEventMask;
384   attrs.event_mask = (ExposureMask | ButtonPressMask | ButtonReleaseMask);
385
386   {
387     int sx = 0, sy = 0, w, h;
388     int mouse_x = 0, mouse_y = 0;
389
390     {
391       Window pointer_root, pointer_child;
392       int root_x, root_y, win_x, win_y;
393       unsigned int mask;
394       if (XQueryPointer (si->dpy,
395                          RootWindowOfScreen (ssi->screen),
396                          &pointer_root, &pointer_child,
397                          &root_x, &root_y, &win_x, &win_y, &mask))
398         {
399           mouse_x = root_x;
400           mouse_y = root_y;
401         }
402     }
403
404     x = ssi->x;
405     y = ssi->y;
406     w = ssi->width;
407     h = ssi->height;
408     if (si->prefs.debug_p) w /= 2;
409     x = sx + (((w + sp->width)  / 2) - sp->width);
410     y = sy + (((h + sp->height) / 2) - sp->height);
411     if (x < sx) x = sx;
412     if (y < sy) y = sy;
413   }
414
415   bw = get_integer_resource (si->dpy, 
416                              "splash.borderWidth",
417                              "Dialog.BorderWidth");
418
419   si->splash_dialog =
420     XCreateWindow (si->dpy,
421                    RootWindowOfScreen(ssi->screen),
422                    x, y, sp->width, sp->height, bw,
423                    DefaultDepthOfScreen (ssi->screen), InputOutput,
424                    DefaultVisualOfScreen(ssi->screen),
425                    attrmask, &attrs);
426   XSetWindowBackground (si->dpy, si->splash_dialog, sp->background);
427   XSetWindowBorder (si->dpy, si->splash_dialog, sp->border);
428
429
430   sp->logo_pixmap = xscreensaver_logo (ssi->screen, 
431                                        /* same visual as si->splash_dialog */
432                                        DefaultVisualOfScreen (ssi->screen),
433                                        si->splash_dialog, cmap,
434                                        sp->background, 
435                                        &sp->logo_pixels, &sp->logo_npixels,
436                                        &sp->logo_clipmask, True);
437
438   XMapRaised (si->dpy, si->splash_dialog);
439   XSync (si->dpy, False);
440
441   si->sp_data = sp;
442
443   sp->timer = XtAppAddTimeOut (si->app, si->prefs.splash_duration,
444                                unsplash_timer, (XtPointer) si);
445
446   draw_splash_window (si);
447   XSync (si->dpy, False);
448 }
449
450
451 static void
452 draw_splash_window (saver_info *si)
453 {
454   splash_dialog_data *sp = si->sp_data;
455   XGCValues gcv;
456   GC gc1, gc2;
457   int hspacing, vspacing, height;
458   int x1, x2, x3, y1, y2;
459   int sw;
460
461 #ifdef PREFS_BUTTON
462   int nbuttons = 3;
463 #else  /* !PREFS_BUTTON */
464   int nbuttons = 2;
465 #endif /* !PREFS_BUTTON */
466
467   height = (sp->heading_font->ascent + sp->heading_font->descent +
468             sp->body_font->ascent + sp->body_font->descent +
469             sp->body_font->ascent + sp->body_font->descent +
470             sp->button_font->ascent + sp->button_font->descent);
471   vspacing = ((sp->height
472                - (4 * sp->shadow_width)
473                - (2 * sp->internal_border)
474                - height) / 5);
475   if (vspacing < 0) vspacing = 0;
476   if (vspacing > (sp->heading_font->ascent * 2))
477     vspacing = (sp->heading_font->ascent * 2);
478             
479   gcv.foreground = sp->foreground;
480   gc1 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
481   gc2 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
482   x1 = sp->logo_width;
483   x3 = sp->width - (sp->shadow_width * 2);
484   y1 = sp->internal_border;
485
486   /* top heading
487    */
488   XSetFont (si->dpy, gc1, sp->heading_font->fid);
489   sw = string_width (sp->heading_font, sp->heading_label);
490   x2 = (x1 + ((x3 - x1 - sw) / 2));
491   y1 += sp->heading_font->ascent;
492   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
493                sp->heading_label, strlen(sp->heading_label));
494   y1 += sp->heading_font->descent;
495
496   /* text below top heading
497    */
498   XSetFont (si->dpy, gc1, sp->body_font->fid);
499   y1 += vspacing + sp->body_font->ascent;
500   sw = string_width (sp->body_font, sp->body_label);
501   x2 = (x1 + ((x3 - x1 - sw) / 2));
502   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
503                sp->body_label, strlen(sp->body_label));
504   y1 += sp->body_font->descent;
505
506   y1 += sp->body_font->ascent;
507   sw = string_width (sp->body_font, sp->body2_label);
508   x2 = (x1 + ((x3 - x1 - sw) / 2));
509   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
510                sp->body2_label, strlen(sp->body2_label));
511   y1 += sp->body_font->descent;
512
513   /* The buttons
514    */
515   XSetForeground (si->dpy, gc1, sp->button_foreground);
516   XSetForeground (si->dpy, gc2, sp->button_background);
517
518 /*  y1 += (vspacing * 2);*/
519   y1 = sp->height - sp->internal_border - sp->button_height;
520
521   x1 += sp->internal_border;
522   y2 = (y1 + ((sp->button_height -
523                (sp->button_font->ascent + sp->button_font->descent))
524               / 2)
525         + sp->button_font->ascent);
526   hspacing = ((sp->width - x1 - (sp->shadow_width * 2) -
527                sp->internal_border - (sp->button_width * nbuttons))
528               / 2);
529
530   x2 = x1 + ((sp->button_width - string_width(sp->button_font, sp->demo_label))
531              / 2);
532   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
533                   sp->button_width, sp->button_height);
534   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
535                sp->demo_label, strlen(sp->demo_label));
536   sp->demo_button_x = x1;
537   sp->demo_button_y = y1;
538   
539 #ifdef PREFS_BUTTON
540   x1 += hspacing + sp->button_width;
541   x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->prefs_label))
542              / 2);
543   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
544                   sp->button_width, sp->button_height);
545   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
546                sp->prefs_label, strlen(sp->prefs_label));
547   sp->prefs_button_x = x1;
548   sp->prefs_button_y = y1;
549 #endif /* PREFS_BUTTON */
550
551 #ifdef PREFS_BUTTON
552   x1 += hspacing + sp->button_width;
553 #else  /* !PREFS_BUTTON */
554   x1 = (sp->width - sp->button_width -
555         sp->internal_border - (sp->shadow_width * 2));
556 #endif /* !PREFS_BUTTON */
557
558   x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->help_label))
559              / 2);
560   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
561                   sp->button_width, sp->button_height);
562   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
563                sp->help_label, strlen(sp->help_label));
564   sp->help_button_x = x1;
565   sp->help_button_y = y1;
566
567
568   /* The logo
569    */
570   x1 = sp->shadow_width * 6;
571   y1 = sp->shadow_width * 6;
572   x2 = sp->logo_width - (sp->shadow_width * 12);
573   y2 = sp->logo_height - (sp->shadow_width * 12);
574
575   if (sp->logo_pixmap)
576     {
577       Window root;
578       int x, y;
579       unsigned int w, h, bw, d;
580       XGetGeometry (si->dpy, sp->logo_pixmap, &root, &x, &y, &w, &h, &bw, &d);
581       XSetForeground (si->dpy, gc1, sp->foreground);
582       XSetBackground (si->dpy, gc1, sp->background);
583       XSetClipMask (si->dpy, gc1, sp->logo_clipmask);
584       XSetClipOrigin (si->dpy, gc1, x1 + ((x2 - (int)w) /2), y1 + ((y2 - (int)h) / 2));
585       if (d == 1)
586         XCopyPlane (si->dpy, sp->logo_pixmap, si->splash_dialog, gc1,
587                     0, 0, w, h,
588                     x1 + ((x2 - (int)w) / 2),
589                     y1 + ((y2 - (int)h) / 2),
590                     1);
591       else
592         XCopyArea (si->dpy, sp->logo_pixmap, si->splash_dialog, gc1,
593                    0, 0, w, h,
594                    x1 + ((x2 - (int)w) / 2),
595                    y1 + ((y2 - (int)h) / 2));
596     }
597
598   /* Solid border inside the logo box. */
599 #if 0
600   XSetForeground (si->dpy, gc1, sp->foreground);
601   XDrawRectangle (si->dpy, si->splash_dialog, gc1, x1, y1, x2-1, y2-1);
602 #endif
603
604   /* The shadow around the logo
605    */
606   draw_shaded_rectangle (si->dpy, si->splash_dialog,
607                          sp->shadow_width * 4,
608                          sp->shadow_width * 4,
609                          sp->logo_width - (sp->shadow_width * 8),
610                          sp->logo_height - (sp->shadow_width * 8),
611                          sp->shadow_width,
612                          sp->shadow_bottom, sp->shadow_top);
613
614   /* The shadow around the whole window
615    */
616   draw_shaded_rectangle (si->dpy, si->splash_dialog,
617                          0, 0, sp->width, sp->height, sp->shadow_width,
618                          sp->shadow_top, sp->shadow_bottom);
619
620   XFreeGC (si->dpy, gc1);
621   XFreeGC (si->dpy, gc2);
622
623   update_splash_window (si);
624 }
625
626
627 static void
628 update_splash_window (saver_info *si)
629 {
630   splash_dialog_data *sp = si->sp_data;
631   int pressed;
632   if (!sp) return;
633   pressed = sp->pressed;
634
635   /* The shadows around the buttons
636    */
637   draw_shaded_rectangle (si->dpy, si->splash_dialog,
638                          sp->demo_button_x, sp->demo_button_y,
639                          sp->button_width, sp->button_height, sp->shadow_width,
640                          (pressed == 1 ? sp->shadow_bottom : sp->shadow_top),
641                          (pressed == 1 ? sp->shadow_top : sp->shadow_bottom));
642 #ifdef PREFS_BUTTON
643   draw_shaded_rectangle (si->dpy, si->splash_dialog,
644                          sp->prefs_button_x, sp->prefs_button_y,
645                          sp->button_width, sp->button_height, sp->shadow_width,
646                          (pressed == 2 ? sp->shadow_bottom : sp->shadow_top),
647                          (pressed == 2 ? sp->shadow_top : sp->shadow_bottom));
648 #endif /* PREFS_BUTTON */
649   draw_shaded_rectangle (si->dpy, si->splash_dialog,
650                          sp->help_button_x, sp->help_button_y,
651                          sp->button_width, sp->button_height, sp->shadow_width,
652                          (pressed == 3 ? sp->shadow_bottom : sp->shadow_top),
653                          (pressed == 3 ? sp->shadow_top : sp->shadow_bottom));
654 }
655
656 static void
657 destroy_splash_window (saver_info *si)
658 {
659   splash_dialog_data *sp = si->sp_data;
660   saver_screen_info *ssi = sp->prompt_screen;
661   Colormap cmap = DefaultColormapOfScreen (ssi->screen);
662   Pixel black = BlackPixelOfScreen (ssi->screen);
663   Pixel white = WhitePixelOfScreen (ssi->screen);
664
665   if (sp->timer)
666     XtRemoveTimeOut (sp->timer);
667
668   if (si->splash_dialog)
669     {
670       XDestroyWindow (si->dpy, si->splash_dialog);
671       si->splash_dialog = 0;
672     }
673   
674   if (sp->heading_label) free (sp->heading_label);
675   if (sp->body_label)    free (sp->body_label);
676   if (sp->body2_label)   free (sp->body2_label);
677   if (sp->demo_label)    free (sp->demo_label);
678 #ifdef PREFS_BUTTON
679   if (sp->prefs_label)   free (sp->prefs_label);
680 #endif /* PREFS_BUTTON */
681   if (sp->help_label)    free (sp->help_label);
682
683   if (sp->heading_font) XFreeFont (si->dpy, sp->heading_font);
684   if (sp->body_font)    XFreeFont (si->dpy, sp->body_font);
685   if (sp->button_font)  XFreeFont (si->dpy, sp->button_font);
686
687   if (sp->foreground != black && sp->foreground != white)
688     XFreeColors (si->dpy, cmap, &sp->foreground, 1, 0L);
689   if (sp->background != black && sp->background != white)
690     XFreeColors (si->dpy, cmap, &sp->background, 1, 0L);
691   if (sp->button_foreground != black && sp->button_foreground != white)
692     XFreeColors (si->dpy, cmap, &sp->button_foreground, 1, 0L);
693   if (sp->button_background != black && sp->button_background != white)
694     XFreeColors (si->dpy, cmap, &sp->button_background, 1, 0L);
695   if (sp->shadow_top != black && sp->shadow_top != white)
696     XFreeColors (si->dpy, cmap, &sp->shadow_top, 1, 0L);
697   if (sp->shadow_bottom != black && sp->shadow_bottom != white)
698     XFreeColors (si->dpy, cmap, &sp->shadow_bottom, 1, 0L);
699
700   if (sp->logo_pixmap)
701     XFreePixmap (si->dpy, sp->logo_pixmap);
702   if (sp->logo_clipmask)
703     XFreePixmap (si->dpy, sp->logo_clipmask);
704   if (sp->logo_pixels)
705     {
706       if (sp->logo_npixels)
707         XFreeColors (si->dpy, cmap, sp->logo_pixels, sp->logo_npixels, 0L);
708       free (sp->logo_pixels);
709       sp->logo_pixels = 0;
710       sp->logo_npixels = 0;
711     }
712
713   memset (sp, 0, sizeof(*sp));
714   free (sp);
715   si->sp_data = 0;
716 }
717
718 void
719 handle_splash_event (saver_info *si, XEvent *event)
720 {
721   splash_dialog_data *sp = si->sp_data;
722   saver_screen_info *ssi = sp->prompt_screen;
723   int which = 0;
724   if (!sp) return;
725
726   switch (event->xany.type)
727     {
728     case Expose:
729       draw_splash_window (si);
730       break;
731
732     case ButtonPress: case ButtonRelease:
733
734       if (event->xbutton.x >= sp->demo_button_x &&
735           event->xbutton.x < sp->demo_button_x + sp->button_width &&
736           event->xbutton.y >= sp->demo_button_y &&
737           event->xbutton.y < sp->demo_button_y + sp->button_height)
738         which = 1;
739
740 #ifdef PREFS_BUTTON
741       else if (event->xbutton.x >= sp->prefs_button_x &&
742                event->xbutton.x < sp->prefs_button_x + sp->button_width &&
743                event->xbutton.y >= sp->prefs_button_y &&
744                event->xbutton.y < sp->prefs_button_y + sp->button_height)
745         which = 2;
746 #endif /* PREFS_BUTTON */
747
748       else if (event->xbutton.x >= sp->help_button_x &&
749                event->xbutton.x < sp->help_button_x + sp->button_width &&
750                event->xbutton.y >= sp->help_button_y &&
751                event->xbutton.y < sp->help_button_y + sp->button_height)
752         which = 3;
753
754       if (event->xany.type == ButtonPress)
755         {
756           sp->pressed = which;
757           update_splash_window (si);
758           if (which == 0)
759             XBell (si->dpy, False);
760         }
761       else if (event->xany.type == ButtonRelease)
762         {
763           if (which && sp->pressed == which)
764             {
765               destroy_splash_window (si);
766               sp = si->sp_data;
767               switch (which)
768                 {
769                 case 1: do_demo (ssi); break;
770 #ifdef PREFS_BUTTON
771                 case 2: do_prefs (ssi); break;
772 #endif /* PREFS_BUTTON */
773                 case 3: do_help (ssi); break;
774                 default: abort();
775                 }
776             }
777           else if (which == 0 && sp->pressed == 0)
778             {
779               /* click and release on the window but not in a button:
780                  treat that as "dismiss the splash dialog." */
781               destroy_splash_window (si);
782               sp = si->sp_data;
783             }
784           if (sp) sp->pressed = 0;
785           update_splash_window (si);
786         }
787       break;
788
789     default:
790       break;
791     }
792 }
793
794 static void
795 unsplash_timer (XtPointer closure, XtIntervalId *id)
796 {
797   saver_info *si = (saver_info *) closure;
798   if (si && si->sp_data)
799     destroy_splash_window (si);
800 }
801
802 \f
803 /* Button callbacks */
804
805 #ifdef VMS
806 # define pid_t int
807 # define fork  vfork
808 #endif /* VMS */
809
810
811 static void
812 do_demo (saver_screen_info *ssi)
813 {
814   saver_info *si = ssi->global;
815   saver_preferences *p = &si->prefs;
816   const char *cmd = p->demo_command;
817
818   if (cmd && *cmd)
819     fork_and_exec (ssi, cmd);
820   else
821     fprintf (stderr, "%s: no demo-mode command has been specified.\n",
822              blurb());
823 }
824
825 #ifdef PREFS_BUTTON
826 static void
827 do_prefs (saver_screen_info *ssi)
828 {
829   saver_info *si = ssi->global;
830   saver_preferences *p = &si->prefs;
831   const char *cmd = p->prefs_command;
832
833   if (command && *command)
834     fork_and_exec (ssi, cmd);
835   else
836     fprintf (stderr, "%s: no preferences command has been specified.\n",
837              blurb());
838 }
839 #endif /* PREFS_BUTTON */
840
841 static void
842 do_help (saver_screen_info *ssi)
843 {
844   saver_info *si = ssi->global;
845   saver_preferences *p = &si->prefs;
846   char *help_command = 0;
847
848   if (!p->load_url_command || !*p->load_url_command)
849     {
850       fprintf (stderr, "%s: no URL command has been specified.\n", blurb());
851       return;
852     }
853   if (!p->help_url || !*p->help_url)
854     {
855       fprintf (stderr, "%s: no Help URL has been specified.\n", blurb());
856       return;
857     }
858
859   help_command = (char *) malloc (strlen (p->load_url_command) +
860                                   (strlen (p->help_url) * 4) + 10);
861   sprintf (help_command, p->load_url_command,
862            p->help_url, p->help_url, p->help_url, p->help_url);
863
864   fork_and_exec (ssi, help_command);
865   free (help_command);
866 }