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