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