http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.04.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, 
418                                        /* same visual as si->splash_dialog */
419                                        DefaultVisualOfScreen (ssi->screen),
420                                        si->splash_dialog, cmap,
421                                        sp->background, 
422                                        &sp->logo_pixels, &sp->logo_npixels,
423                                        &sp->logo_clipmask, True);
424
425   XMapRaised (si->dpy, si->splash_dialog);
426   XSync (si->dpy, False);
427
428   si->sp_data = sp;
429
430   sp->timer = XtAppAddTimeOut (si->app, si->prefs.splash_duration,
431                                unsplash_timer, (XtPointer) si);
432
433   draw_splash_window (si);
434   XSync (si->dpy, False);
435 }
436
437
438 static void
439 draw_splash_window (saver_info *si)
440 {
441   splash_dialog_data *sp = si->sp_data;
442   XGCValues gcv;
443   GC gc1, gc2;
444   int hspacing, vspacing, height;
445   int x1, x2, x3, y1, y2;
446   int sw;
447
448 #ifdef PREFS_BUTTON
449   int nbuttons = 3;
450 #else  /* !PREFS_BUTTON */
451   int nbuttons = 2;
452 #endif /* !PREFS_BUTTON */
453
454   height = (sp->heading_font->ascent + sp->heading_font->descent +
455             sp->body_font->ascent + sp->body_font->descent +
456             sp->body_font->ascent + sp->body_font->descent +
457             sp->button_font->ascent + sp->button_font->descent);
458   vspacing = ((sp->height
459                - (4 * sp->shadow_width)
460                - (2 * sp->internal_border)
461                - height) / 5);
462   if (vspacing < 0) vspacing = 0;
463   if (vspacing > (sp->heading_font->ascent * 2))
464     vspacing = (sp->heading_font->ascent * 2);
465             
466   gcv.foreground = sp->foreground;
467   gc1 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
468   gc2 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
469   x1 = sp->logo_width;
470   x3 = sp->width - (sp->shadow_width * 2);
471   y1 = sp->internal_border;
472
473   /* top heading
474    */
475   XSetFont (si->dpy, gc1, sp->heading_font->fid);
476   sw = string_width (sp->heading_font, sp->heading_label);
477   x2 = (x1 + ((x3 - x1 - sw) / 2));
478   y1 += sp->heading_font->ascent;
479   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
480                sp->heading_label, strlen(sp->heading_label));
481   y1 += sp->heading_font->descent;
482
483   /* text below top heading
484    */
485   XSetFont (si->dpy, gc1, sp->body_font->fid);
486   y1 += vspacing + sp->body_font->ascent;
487   sw = string_width (sp->body_font, sp->body_label);
488   x2 = (x1 + ((x3 - x1 - sw) / 2));
489   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
490                sp->body_label, strlen(sp->body_label));
491   y1 += sp->body_font->descent;
492
493   y1 += sp->body_font->ascent;
494   sw = string_width (sp->body_font, sp->body2_label);
495   x2 = (x1 + ((x3 - x1 - sw) / 2));
496   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
497                sp->body2_label, strlen(sp->body2_label));
498   y1 += sp->body_font->descent;
499
500   /* The buttons
501    */
502   XSetForeground (si->dpy, gc1, sp->button_foreground);
503   XSetForeground (si->dpy, gc2, sp->button_background);
504
505 /*  y1 += (vspacing * 2);*/
506   y1 = sp->height - sp->internal_border - sp->button_height;
507
508   x1 += sp->internal_border;
509   y2 = (y1 + ((sp->button_height -
510                (sp->button_font->ascent + sp->button_font->descent))
511               / 2)
512         + sp->button_font->ascent);
513   hspacing = ((sp->width - x1 - (sp->shadow_width * 2) -
514                sp->internal_border - (sp->button_width * nbuttons))
515               / 2);
516
517   x2 = x1 + ((sp->button_width - string_width(sp->button_font, sp->demo_label))
518              / 2);
519   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
520                   sp->button_width, sp->button_height);
521   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
522                sp->demo_label, strlen(sp->demo_label));
523   sp->demo_button_x = x1;
524   sp->demo_button_y = y1;
525   
526 #ifdef PREFS_BUTTON
527   x1 += hspacing + sp->button_width;
528   x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->prefs_label))
529              / 2);
530   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
531                   sp->button_width, sp->button_height);
532   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
533                sp->prefs_label, strlen(sp->prefs_label));
534   sp->prefs_button_x = x1;
535   sp->prefs_button_y = y1;
536 #endif /* PREFS_BUTTON */
537
538 #ifdef PREFS_BUTTON
539   x1 += hspacing + sp->button_width;
540 #else  /* !PREFS_BUTTON */
541   x1 = (sp->width - sp->button_width -
542         sp->internal_border - (sp->shadow_width * 2));
543 #endif /* !PREFS_BUTTON */
544
545   x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->help_label))
546              / 2);
547   XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
548                   sp->button_width, sp->button_height);
549   XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
550                sp->help_label, strlen(sp->help_label));
551   sp->help_button_x = x1;
552   sp->help_button_y = y1;
553
554
555   /* The logo
556    */
557   x1 = sp->shadow_width * 6;
558   y1 = sp->shadow_width * 6;
559   x2 = sp->logo_width - (sp->shadow_width * 12);
560   y2 = sp->logo_height - (sp->shadow_width * 12);
561
562   if (sp->logo_pixmap)
563     {
564       Window root;
565       int x, y;
566       unsigned int w, h, bw, d;
567       XGetGeometry (si->dpy, sp->logo_pixmap, &root, &x, &y, &w, &h, &bw, &d);
568       XSetForeground (si->dpy, gc1, sp->foreground);
569       XSetBackground (si->dpy, gc1, sp->background);
570       XSetClipMask (si->dpy, gc1, sp->logo_clipmask);
571       XSetClipOrigin (si->dpy, gc1, x1 + ((x2 - (int)w) /2), y1 + ((y2 - (int)h) / 2));
572       if (d == 1)
573         XCopyPlane (si->dpy, sp->logo_pixmap, si->splash_dialog, gc1,
574                     0, 0, w, h,
575                     x1 + ((x2 - (int)w) / 2),
576                     y1 + ((y2 - (int)h) / 2),
577                     1);
578       else
579         XCopyArea (si->dpy, sp->logo_pixmap, si->splash_dialog, gc1,
580                    0, 0, w, h,
581                    x1 + ((x2 - (int)w) / 2),
582                    y1 + ((y2 - (int)h) / 2));
583     }
584
585   /* Solid border inside the logo box. */
586 #if 0
587   XSetForeground (si->dpy, gc1, sp->foreground);
588   XDrawRectangle (si->dpy, si->splash_dialog, gc1, x1, y1, x2-1, y2-1);
589 #endif
590
591   /* The shadow around the logo
592    */
593   draw_shaded_rectangle (si->dpy, si->splash_dialog,
594                          sp->shadow_width * 4,
595                          sp->shadow_width * 4,
596                          sp->logo_width - (sp->shadow_width * 8),
597                          sp->logo_height - (sp->shadow_width * 8),
598                          sp->shadow_width,
599                          sp->shadow_bottom, sp->shadow_top);
600
601   /* The shadow around the whole window
602    */
603   draw_shaded_rectangle (si->dpy, si->splash_dialog,
604                          0, 0, sp->width, sp->height, sp->shadow_width,
605                          sp->shadow_top, sp->shadow_bottom);
606
607   XFreeGC (si->dpy, gc1);
608   XFreeGC (si->dpy, gc2);
609
610   update_splash_window (si);
611 }
612
613
614 static void
615 update_splash_window (saver_info *si)
616 {
617   splash_dialog_data *sp = si->sp_data;
618   int pressed;
619   if (!sp) return;
620   pressed = sp->pressed;
621
622   /* The shadows around the buttons
623    */
624   draw_shaded_rectangle (si->dpy, si->splash_dialog,
625                          sp->demo_button_x, sp->demo_button_y,
626                          sp->button_width, sp->button_height, sp->shadow_width,
627                          (pressed == 1 ? sp->shadow_bottom : sp->shadow_top),
628                          (pressed == 1 ? sp->shadow_top : sp->shadow_bottom));
629 #ifdef PREFS_BUTTON
630   draw_shaded_rectangle (si->dpy, si->splash_dialog,
631                          sp->prefs_button_x, sp->prefs_button_y,
632                          sp->button_width, sp->button_height, sp->shadow_width,
633                          (pressed == 2 ? sp->shadow_bottom : sp->shadow_top),
634                          (pressed == 2 ? sp->shadow_top : sp->shadow_bottom));
635 #endif /* PREFS_BUTTON */
636   draw_shaded_rectangle (si->dpy, si->splash_dialog,
637                          sp->help_button_x, sp->help_button_y,
638                          sp->button_width, sp->button_height, sp->shadow_width,
639                          (pressed == 3 ? sp->shadow_bottom : sp->shadow_top),
640                          (pressed == 3 ? sp->shadow_top : sp->shadow_bottom));
641 }
642
643 static void
644 destroy_splash_window (saver_info *si)
645 {
646   splash_dialog_data *sp = si->sp_data;
647   saver_screen_info *ssi = sp->prompt_screen;
648   Colormap cmap = DefaultColormapOfScreen (ssi->screen);
649   Pixel black = BlackPixelOfScreen (ssi->screen);
650   Pixel white = WhitePixelOfScreen (ssi->screen);
651
652   if (sp->timer)
653     XtRemoveTimeOut (sp->timer);
654
655   if (si->splash_dialog)
656     {
657       XDestroyWindow (si->dpy, si->splash_dialog);
658       si->splash_dialog = 0;
659     }
660   
661   if (sp->heading_label) free (sp->heading_label);
662   if (sp->body_label)    free (sp->body_label);
663   if (sp->body2_label)   free (sp->body2_label);
664   if (sp->demo_label)    free (sp->demo_label);
665 #ifdef PREFS_BUTTON
666   if (sp->prefs_label)   free (sp->prefs_label);
667 #endif /* PREFS_BUTTON */
668   if (sp->help_label)    free (sp->help_label);
669
670   if (sp->heading_font) XFreeFont (si->dpy, sp->heading_font);
671   if (sp->body_font)    XFreeFont (si->dpy, sp->body_font);
672   if (sp->button_font)  XFreeFont (si->dpy, sp->button_font);
673
674   if (sp->foreground != black && sp->foreground != white)
675     XFreeColors (si->dpy, cmap, &sp->foreground, 1, 0L);
676   if (sp->background != black && sp->background != white)
677     XFreeColors (si->dpy, cmap, &sp->background, 1, 0L);
678   if (sp->button_foreground != black && sp->button_foreground != white)
679     XFreeColors (si->dpy, cmap, &sp->button_foreground, 1, 0L);
680   if (sp->button_background != black && sp->button_background != white)
681     XFreeColors (si->dpy, cmap, &sp->button_background, 1, 0L);
682   if (sp->shadow_top != black && sp->shadow_top != white)
683     XFreeColors (si->dpy, cmap, &sp->shadow_top, 1, 0L);
684   if (sp->shadow_bottom != black && sp->shadow_bottom != white)
685     XFreeColors (si->dpy, cmap, &sp->shadow_bottom, 1, 0L);
686
687   if (sp->logo_pixmap)
688     XFreePixmap (si->dpy, sp->logo_pixmap);
689   if (sp->logo_clipmask)
690     XFreePixmap (si->dpy, sp->logo_clipmask);
691   if (sp->logo_pixels)
692     {
693       if (sp->logo_npixels)
694         XFreeColors (si->dpy, cmap, sp->logo_pixels, sp->logo_npixels, 0L);
695       free (sp->logo_pixels);
696       sp->logo_pixels = 0;
697       sp->logo_npixels = 0;
698     }
699
700   memset (sp, 0, sizeof(*sp));
701   free (sp);
702   si->sp_data = 0;
703 }
704
705 void
706 handle_splash_event (saver_info *si, XEvent *event)
707 {
708   splash_dialog_data *sp = si->sp_data;
709   saver_screen_info *ssi = sp->prompt_screen;
710   int which = 0;
711   if (!sp) return;
712
713   switch (event->xany.type)
714     {
715     case Expose:
716       draw_splash_window (si);
717       break;
718
719     case ButtonPress: case ButtonRelease:
720
721       if (event->xbutton.x >= sp->demo_button_x &&
722           event->xbutton.x < sp->demo_button_x + sp->button_width &&
723           event->xbutton.y >= sp->demo_button_y &&
724           event->xbutton.y < sp->demo_button_y + sp->button_height)
725         which = 1;
726
727 #ifdef PREFS_BUTTON
728       else if (event->xbutton.x >= sp->prefs_button_x &&
729                event->xbutton.x < sp->prefs_button_x + sp->button_width &&
730                event->xbutton.y >= sp->prefs_button_y &&
731                event->xbutton.y < sp->prefs_button_y + sp->button_height)
732         which = 2;
733 #endif /* PREFS_BUTTON */
734
735       else if (event->xbutton.x >= sp->help_button_x &&
736                event->xbutton.x < sp->help_button_x + sp->button_width &&
737                event->xbutton.y >= sp->help_button_y &&
738                event->xbutton.y < sp->help_button_y + sp->button_height)
739         which = 3;
740
741       if (event->xany.type == ButtonPress)
742         {
743           sp->pressed = which;
744           update_splash_window (si);
745           if (which == 0)
746             XBell (si->dpy, False);
747         }
748       else if (event->xany.type == ButtonRelease)
749         {
750           if (which && sp->pressed == which)
751             {
752               destroy_splash_window (si);
753               sp = si->sp_data;
754               switch (which)
755                 {
756                 case 1: do_demo (ssi); break;
757 #ifdef PREFS_BUTTON
758                 case 2: do_prefs (ssi); break;
759 #endif /* PREFS_BUTTON */
760                 case 3: do_help (ssi); break;
761                 default: abort();
762                 }
763             }
764           else if (which == 0 && sp->pressed == 0)
765             {
766               /* click and release on the window but not in a button:
767                  treat that as "dismiss the splash dialog." */
768               destroy_splash_window (si);
769               sp = si->sp_data;
770             }
771           if (sp) sp->pressed = 0;
772           update_splash_window (si);
773         }
774       break;
775
776     default:
777       break;
778     }
779 }
780
781 static void
782 unsplash_timer (XtPointer closure, XtIntervalId *id)
783 {
784   saver_info *si = (saver_info *) closure;
785   if (si && si->sp_data)
786     destroy_splash_window (si);
787 }
788
789 \f
790 /* Button callbacks */
791
792 #ifdef VMS
793 # define pid_t int
794 # define fork  vfork
795 #endif /* VMS */
796
797
798 static void
799 do_demo (saver_screen_info *ssi)
800 {
801   saver_info *si = ssi->global;
802   saver_preferences *p = &si->prefs;
803   const char *cmd = p->demo_command;
804
805   if (cmd && *cmd)
806     fork_and_exec (ssi, cmd);
807   else
808     fprintf (stderr, "%s: no demo-mode command has been specified.\n",
809              blurb());
810 }
811
812 #ifdef PREFS_BUTTON
813 static void
814 do_prefs (saver_screen_info *ssi)
815 {
816   saver_info *si = ssi->global;
817   saver_preferences *p = &si->prefs;
818   const char *cmd = p->prefs_command;
819
820   if (command && *command)
821     fork_and_exec (ssi, cmd);
822   else
823     fprintf (stderr, "%s: no preferences command has been specified.\n",
824              blurb());
825 }
826 #endif /* PREFS_BUTTON */
827
828 static void
829 do_help (saver_screen_info *ssi)
830 {
831   saver_info *si = ssi->global;
832   saver_preferences *p = &si->prefs;
833   char *help_command = 0;
834
835   if (!p->load_url_command || !*p->load_url_command)
836     {
837       fprintf (stderr, "%s: no URL command has been specified.\n", blurb());
838       return;
839     }
840   if (!p->help_url || !*p->help_url)
841     {
842       fprintf (stderr, "%s: no Help URL has been specified.\n", blurb());
843       return;
844     }
845
846   help_command = (char *) malloc (strlen (p->load_url_command) +
847                                   (strlen (p->help_url) * 4) + 10);
848   sprintf (help_command, p->load_url_command,
849            p->help_url, p->help_url, p->help_url, p->help_url);
850
851   fork_and_exec (ssi, help_command);
852   free (help_command);
853 }