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