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