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