1 /* xscreensaver, Copyright (c) 1991-1998 Jamie Zawinski <jwz@netscape.com>
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
16 #include <X11/Intrinsic.h>
18 #include "xscreensaver.h"
19 #include "resources.h"
22 #define MAX(a,b) ((a)>(b)?(a):(b))
25 draw_shaded_rectangle (Display *dpy, Window window,
27 int width, int height,
29 unsigned long top_color,
30 unsigned long bottom_color)
35 if (thickness == 0) return;
37 gcv.foreground = top_color;
38 gc1 = XCreateGC (dpy, window, GCForeground, &gcv);
39 gcv.foreground = bottom_color;
40 gc2 = XCreateGC (dpy, window, GCForeground, &gcv);
44 points [1].x = x + width;
46 points [2].x = x + width - thickness;
47 points [2].y = y + thickness;
49 points [3].y = y + thickness;
50 XFillPolygon (dpy, window, gc1, points, 4, Convex, CoordModeOrigin);
53 points [0].y = y + thickness;
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);
62 points [0].x = x + width;
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);
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);
88 string_width (XFontStruct *font, char *s)
90 int direction, ascent, descent;
92 XTextExtents (font, s, strlen(s), &direction, &ascent, &descent, &overall);
98 send_self_clientmessage (saver_info *si, Atom command)
100 Display *dpy = si->dpy;
101 Window window = si->default_screen->screensaver_window;
103 event.xany.type = ClientMessage;
104 event.xclient.display = si->dpy;
105 event.xclient.window = window;
106 event.xclient.message_type = XA_SCREENSAVER;
107 event.xclient.format = 32;
108 memset (&event.xclient.data, 0, sizeof(event.xclient.data));
109 event.xclient.data.l[0] = (long) command;
110 if (! XSendEvent (dpy, window, False, 0L, &event))
111 fprintf (stderr, "%s: XSendEvent(dpy, 0x%x ...) failed.\n",
112 progname, (unsigned int) window);
117 get_help (saver_info *si)
119 saver_preferences *p = &si->prefs;
121 if (!p->help_url || !*p->help_url)
122 fprintf (stderr, "%s: no Help URL has been specified.\n", blurb());
123 else if (!p->load_url_command || !*p->load_url_command)
124 fprintf (stderr, "%s: no URL-loading command has been specified.\n",
128 char *buf = (char *) malloc (strlen(p->load_url_command) +
129 (strlen(p->help_url) * 2) + 10);
130 sprintf (buf, p->load_url_command, p->help_url, p->help_url);
135 static void update_splash_window (saver_info *si);
136 static void draw_splash_window (saver_info *si);
137 static void destroy_splash_window (saver_info *si);
138 static void unsplash_timer (XtPointer closure, XtIntervalId *id);
141 struct splash_dialog_data {
154 XFontStruct *heading_font;
155 XFontStruct *body_font;
156 XFontStruct *button_font;
160 Pixel button_foreground;
161 Pixel button_background;
162 Pixel logo_foreground;
163 Pixel logo_background;
167 Dimension logo_width;
168 Dimension logo_height;
169 Dimension internal_border;
170 Dimension shadow_width;
172 Dimension button_width, button_height;
173 Dimension demo_button_x, demo_button_y;
174 Dimension prefs_button_x, prefs_button_y;
175 Dimension help_button_x, help_button_y;
182 make_splash_dialog (saver_info *si)
185 XSetWindowAttributes attrs;
186 unsigned long attrmask = 0;
187 splash_dialog_data *sp;
188 Screen *screen = si->default_screen->screen;
189 Colormap cmap = DefaultColormapOfScreen (screen);
194 if (si->prefs.splash_duration <= 0)
197 sp = (splash_dialog_data *) calloc (1, sizeof(*sp));
199 sp->heading_label = get_string_resource ("splash.heading.label",
200 "Dialog.Label.Label");
201 sp->body_label = get_string_resource ("splash.body.label",
202 "Dialog.Label.Label");
203 sp->body2_label = get_string_resource ("splash.body2.label",
204 "Dialog.Label.Label");
205 sp->demo_label = get_string_resource ("splash.demo.label",
206 "Dialog.Button.Label");
207 sp->prefs_label = get_string_resource ("splash.prefs.label",
208 "Dialog.Button.Label");
209 sp->help_label = get_string_resource ("splash.help.label",
210 "Dialog.Button.Label");
212 if (!sp->heading_label)
213 sp->heading_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
215 sp->body_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
216 if (!sp->body2_label)
217 sp->body2_label = strdup("ERROR: REESOURCES NOT INSTALLED CORRECTLY");
218 if (!sp->demo_label) sp->demo_label = strdup("ERROR");
219 if (!sp->prefs_label) sp->prefs_label = strdup("ERROR");
220 if (!sp->help_label) sp->help_label = strdup("ERROR");
222 /* Put the version number in the label. */
224 char *s = (char *) malloc (strlen(sp->heading_label) + 20);
225 sprintf(s, sp->heading_label, si->version);
226 free (sp->heading_label);
227 sp->heading_label = s;
230 f = get_string_resource ("splash.headingFont", "Dialog.Font");
231 sp->heading_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
232 if (!sp->heading_font) sp->heading_font = XLoadQueryFont (si->dpy, "fixed");
235 f = get_string_resource("splash.bodyFont", "Dialog.Font");
236 sp->body_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
237 if (!sp->body_font) sp->body_font = XLoadQueryFont (si->dpy, "fixed");
240 f = get_string_resource("splash.buttonFont", "Dialog.Font");
241 sp->button_font = XLoadQueryFont (si->dpy, (f ? f : "fixed"));
242 if (!sp->button_font) sp->button_font = XLoadQueryFont (si->dpy, "fixed");
245 sp->foreground = get_pixel_resource ("splash.foreground",
248 sp->background = get_pixel_resource ("splash.background",
252 if (sp->foreground == sp->background)
254 /* Make sure the error messages show up. */
255 sp->foreground = BlackPixelOfScreen (screen);
256 sp->background = WhitePixelOfScreen (screen);
259 sp->button_foreground = get_pixel_resource ("splash.Button.foreground",
260 "Dialog.Button.Foreground",
262 sp->button_background = get_pixel_resource ("splash.Button.background",
263 "Dialog.Button.Background",
265 sp->logo_foreground = get_pixel_resource ("splash.logo.foreground",
266 "Dialog.Logo.Foreground",
268 sp->logo_background = get_pixel_resource ("splash.logo.background",
269 "Dialog.Logo.Background",
271 sp->shadow_top = get_pixel_resource ("splash.topShadowColor",
274 sp->shadow_bottom = get_pixel_resource ("splash.bottomShadowColor",
278 sp->logo_width = get_integer_resource ("splash.logo.width",
279 "Dialog.Logo.Width");
280 sp->logo_height = get_integer_resource ("splash.logo.height",
281 "Dialog.Logo.Height");
282 sp->internal_border = get_integer_resource ("splash.internalBorderWidth",
283 "Dialog.InternalBorderWidth");
284 sp->shadow_width = get_integer_resource ("splash.shadowThickness",
285 "Dialog.ShadowThickness");
287 if (sp->logo_width == 0) sp->logo_width = 150;
288 if (sp->logo_height == 0) sp->logo_height = 150;
289 if (sp->internal_border == 0) sp->internal_border = 15;
290 if (sp->shadow_width == 0) sp->shadow_width = 4;
293 int direction, ascent, descent;
299 /* Measure the heading_label. */
300 XTextExtents (sp->heading_font,
301 sp->heading_label, strlen(sp->heading_label),
302 &direction, &ascent, &descent, &overall);
303 if (overall.width > sp->width) sp->width = overall.width;
304 sp->height += ascent + descent;
306 /* Measure the body_label. */
307 XTextExtents (sp->body_font,
308 sp->body_label, strlen(sp->body_label),
309 &direction, &ascent, &descent, &overall);
310 if (overall.width > sp->width) sp->width = overall.width;
311 sp->height += ascent + descent;
313 /* Measure the body2_label. */
314 XTextExtents (sp->body_font,
315 sp->body2_label, strlen(sp->body2_label),
316 &direction, &ascent, &descent, &overall);
317 if (overall.width > sp->width) sp->width = overall.width;
318 sp->height += ascent + descent;
321 Dimension w2 = 0, w3 = 0, w4 = 0;
322 Dimension h2 = 0, h3 = 0, h4 = 0;
324 /* Measure the Demo button. */
325 XTextExtents (sp->button_font,
326 sp->demo_label, strlen(sp->demo_label),
327 &direction, &ascent, &descent, &overall);
329 h2 = ascent + descent;
331 /* Measure the Prefs button. */
332 XTextExtents (sp->button_font,
333 sp->prefs_label, strlen(sp->prefs_label),
334 &direction, &ascent, &descent, &overall);
336 h3 = ascent + descent;
338 /* Measure the Help button. */
339 XTextExtents (sp->button_font,
340 sp->help_label, strlen(sp->help_label),
341 &direction, &ascent, &descent, &overall);
343 h4 = ascent + descent;
345 w2 = MAX(w2, w3); w2 = MAX(w2, w4);
346 h2 = MAX(h2, h3); h2 = MAX(h2, h4);
348 w2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
349 h2 += ((ascent + descent) / 2) + (sp->shadow_width * 2);
351 sp->button_width = w2;
352 sp->button_height = h2;
356 w2 += ((ascent + descent) * 2); /* for space between buttons */
358 if (w2 > sp->width) sp->width = w2;
362 sp->width += (sp->internal_border * 2);
363 sp->height += (sp->internal_border * 3);
365 if (sp->logo_height > sp->height)
366 sp->height = sp->logo_height;
367 else if (sp->height > sp->logo_height)
368 sp->logo_height = sp->height;
370 sp->logo_width = sp->logo_height;
372 sp->width += sp->logo_width;
375 attrmask |= CWOverrideRedirect; attrs.override_redirect = True;
376 attrmask |= CWEventMask;
377 attrs.event_mask = (ExposureMask | ButtonPressMask | ButtonReleaseMask);
380 Dimension w = WidthOfScreen(screen);
381 Dimension h = HeightOfScreen(screen);
382 if (si->prefs.debug_p) w /= 2;
383 x = ((w + sp->width) / 2) - sp->width;
384 y = ((h + sp->height) / 2) - sp->height;
389 bw = get_integer_resource ("splash.borderWidth", "Dialog.BorderWidth");
392 XCreateWindow (si->dpy,
393 RootWindowOfScreen(screen),
394 x, y, sp->width, sp->height, bw,
395 DefaultDepthOfScreen (screen), InputOutput,
396 DefaultVisualOfScreen(screen),
398 XSetWindowBackground (si->dpy, si->splash_dialog, sp->background);
400 XMapRaised (si->dpy, si->splash_dialog);
401 XSync (si->dpy, False);
405 sp->timer = XtAppAddTimeOut (si->app, si->prefs.splash_duration,
406 unsplash_timer, (XtPointer) si);
408 draw_splash_window (si);
409 XSync (si->dpy, False);
413 draw_splash_window (saver_info *si)
415 splash_dialog_data *sp = si->sp_data;
418 int hspacing, vspacing, height;
419 int x1, x2, x3, y1, y2;
422 height = (sp->heading_font->ascent + sp->heading_font->descent +
423 sp->body_font->ascent + sp->body_font->descent +
424 sp->body_font->ascent + sp->body_font->descent +
425 sp->button_font->ascent + sp->button_font->descent);
426 vspacing = ((sp->height
427 - (4 * sp->shadow_width)
428 - (2 * sp->internal_border)
430 if (vspacing < 0) vspacing = 0;
431 if (vspacing > (sp->heading_font->ascent * 2))
432 vspacing = (sp->heading_font->ascent * 2);
434 gcv.foreground = sp->foreground;
435 gc1 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
436 gc2 = XCreateGC (si->dpy, si->splash_dialog, GCForeground, &gcv);
438 x3 = sp->width - (sp->shadow_width * 2);
439 y1 = sp->internal_border;
443 XSetFont (si->dpy, gc1, sp->heading_font->fid);
444 sw = string_width (sp->heading_font, sp->heading_label);
445 x2 = (x1 + ((x3 - x1 - sw) / 2));
446 y1 += sp->heading_font->ascent;
447 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
448 sp->heading_label, strlen(sp->heading_label));
449 y1 += sp->heading_font->descent;
451 /* text below top heading
453 XSetFont (si->dpy, gc1, sp->body_font->fid);
454 y1 += vspacing + sp->body_font->ascent;
455 sw = string_width (sp->body_font, sp->body_label);
456 x2 = (x1 + ((x3 - x1 - sw) / 2));
457 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
458 sp->body_label, strlen(sp->body_label));
459 y1 += sp->body_font->descent;
461 y1 += sp->body_font->ascent;
462 sw = string_width (sp->body_font, sp->body2_label);
463 x2 = (x1 + ((x3 - x1 - sw) / 2));
464 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y1,
465 sp->body2_label, strlen(sp->body2_label));
466 y1 += sp->body_font->descent;
470 XSetForeground (si->dpy, gc1, sp->button_foreground);
471 XSetForeground (si->dpy, gc2, sp->button_background);
473 /* y1 += (vspacing * 2);*/
474 y1 = sp->height - sp->internal_border - sp->button_height;
476 x1 += sp->internal_border;
477 y2 = (y1 + ((sp->button_height -
478 (sp->button_font->ascent + sp->button_font->descent))
480 + sp->button_font->ascent);
481 hspacing = ((sp->width - x1 - (sp->shadow_width * 2) -
482 sp->internal_border - (sp->button_width * 3))
485 x2 = x1 + ((sp->button_width - string_width(sp->button_font, sp->demo_label))
487 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
488 sp->button_width, sp->button_height);
489 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
490 sp->demo_label, strlen(sp->demo_label));
491 sp->demo_button_x = x1;
492 sp->demo_button_y = y1;
494 x1 += hspacing + sp->button_width;
495 x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->prefs_label))
497 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
498 sp->button_width, sp->button_height);
499 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
500 sp->prefs_label, strlen(sp->prefs_label));
501 sp->prefs_button_x = x1;
502 sp->prefs_button_y = y1;
504 x1 += hspacing + sp->button_width;
505 x2 = x1 + ((sp->button_width - string_width(sp->button_font,sp->help_label))
507 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1,
508 sp->button_width, sp->button_height);
509 XDrawString (si->dpy, si->splash_dialog, gc1, x2, y2,
510 sp->help_label, strlen(sp->help_label));
511 sp->help_button_x = x1;
512 sp->help_button_y = y1;
517 XSetForeground (si->dpy, gc1, sp->logo_foreground);
518 XSetForeground (si->dpy, gc2, sp->logo_background);
520 x1 = sp->shadow_width * 3;
521 y1 = sp->shadow_width * 3;
522 x2 = sp->logo_width - (sp->shadow_width * 6);
523 y2 = sp->logo_height - (sp->shadow_width * 6);
525 XFillRectangle (si->dpy, si->splash_dialog, gc2, x1, y1, x2, y2);
526 skull (si->dpy, si->splash_dialog, gc1, gc2,
527 x1 + sp->shadow_width, y1 + sp->shadow_width,
528 x2 - (sp->shadow_width * 2), y2 - (sp->shadow_width * 2));
531 /* The shadow around the logo
533 draw_shaded_rectangle (si->dpy, si->splash_dialog,
534 sp->shadow_width * 2,
535 sp->shadow_width * 2,
536 sp->logo_width - (sp->shadow_width * 4),
537 sp->logo_height - (sp->shadow_width * 4),
539 sp->shadow_bottom, sp->shadow_top);
541 /* The shadow around the whole window
543 draw_shaded_rectangle (si->dpy, si->splash_dialog,
544 0, 0, sp->width, sp->height, sp->shadow_width,
545 sp->shadow_top, sp->shadow_bottom);
547 XFreeGC (si->dpy, gc1);
548 XFreeGC (si->dpy, gc2);
550 update_splash_window (si);
555 update_splash_window (saver_info *si)
557 splash_dialog_data *sp = si->sp_data;
560 pressed = sp->pressed;
562 /* The shadows around the buttons
564 draw_shaded_rectangle (si->dpy, si->splash_dialog,
565 sp->demo_button_x, sp->demo_button_y,
566 sp->button_width, sp->button_height, sp->shadow_width,
567 (pressed == 1 ? sp->shadow_bottom : sp->shadow_top),
568 (pressed == 1 ? sp->shadow_top : sp->shadow_bottom));
569 draw_shaded_rectangle (si->dpy, si->splash_dialog,
570 sp->prefs_button_x, sp->prefs_button_y,
571 sp->button_width, sp->button_height, sp->shadow_width,
572 (pressed == 2 ? sp->shadow_bottom : sp->shadow_top),
573 (pressed == 2 ? sp->shadow_top : sp->shadow_bottom));
574 draw_shaded_rectangle (si->dpy, si->splash_dialog,
575 sp->help_button_x, sp->help_button_y,
576 sp->button_width, sp->button_height, sp->shadow_width,
577 (pressed == 3 ? sp->shadow_bottom : sp->shadow_top),
578 (pressed == 3 ? sp->shadow_top : sp->shadow_bottom));
582 destroy_splash_window (saver_info *si)
584 splash_dialog_data *sp = si->sp_data;
585 Screen *screen = si->default_screen->screen;
586 Colormap cmap = DefaultColormapOfScreen (screen);
587 Pixel black = BlackPixelOfScreen (screen);
588 Pixel white = WhitePixelOfScreen (screen);
591 XtRemoveTimeOut (sp->timer);
593 if (si->splash_dialog)
595 XDestroyWindow (si->dpy, si->splash_dialog);
596 si->splash_dialog = 0;
599 if (sp->heading_label) free (sp->heading_label);
600 if (sp->body_label) free (sp->body_label);
601 if (sp->demo_label) free (sp->demo_label);
602 if (sp->prefs_label) free (sp->prefs_label);
603 if (sp->help_label) free (sp->help_label);
605 if (sp->heading_font) XFreeFont (si->dpy, sp->heading_font);
606 if (sp->body_font) XFreeFont (si->dpy, sp->body_font);
607 if (sp->button_font) XFreeFont (si->dpy, sp->button_font);
609 if (sp->foreground != black && sp->foreground != white)
610 XFreeColors (si->dpy, cmap, &sp->foreground, 1, 0L);
611 if (sp->background != black && sp->background != white)
612 XFreeColors (si->dpy, cmap, &sp->background, 1, 0L);
613 if (sp->button_foreground != black && sp->button_foreground != white)
614 XFreeColors (si->dpy, cmap, &sp->button_foreground, 1, 0L);
615 if (sp->button_background != black && sp->button_background != white)
616 XFreeColors (si->dpy, cmap, &sp->button_background, 1, 0L);
617 if (sp->logo_foreground != black && sp->logo_foreground != white)
618 XFreeColors (si->dpy, cmap, &sp->logo_foreground, 1, 0L);
619 if (sp->logo_background != black && sp->logo_background != white)
620 XFreeColors (si->dpy, cmap, &sp->logo_background, 1, 0L);
621 if (sp->shadow_top != black && sp->shadow_top != white)
622 XFreeColors (si->dpy, cmap, &sp->shadow_top, 1, 0L);
623 if (sp->shadow_bottom != black && sp->shadow_bottom != white)
624 XFreeColors (si->dpy, cmap, &sp->shadow_bottom, 1, 0L);
626 memset (sp, 0, sizeof(*sp));
633 handle_splash_event (saver_info *si, XEvent *event)
635 splash_dialog_data *sp = si->sp_data;
639 switch (event->xany.type)
642 draw_splash_window (si);
645 case ButtonPress: case ButtonRelease:
647 if (event->xbutton.x >= sp->demo_button_x &&
648 event->xbutton.x < sp->demo_button_x + sp->button_width &&
649 event->xbutton.y >= sp->demo_button_y &&
650 event->xbutton.y < sp->demo_button_y + sp->button_height)
653 else if (event->xbutton.x >= sp->prefs_button_x &&
654 event->xbutton.x < sp->prefs_button_x + sp->button_width &&
655 event->xbutton.y >= sp->prefs_button_y &&
656 event->xbutton.y < sp->prefs_button_y + sp->button_height)
659 else if (event->xbutton.x >= sp->help_button_x &&
660 event->xbutton.x < sp->help_button_x + sp->button_width &&
661 event->xbutton.y >= sp->help_button_y &&
662 event->xbutton.y < sp->help_button_y + sp->button_height)
665 if (event->xany.type == ButtonPress)
668 update_splash_window (si);
670 XBell (si->dpy, False);
672 else if (event->xany.type == ButtonRelease)
674 if (which && sp->pressed == which)
676 destroy_splash_window (si);
679 case 1: send_self_clientmessage (si, XA_DEMO); break;
680 case 2: send_self_clientmessage (si, XA_PREFS); break;
681 case 3: get_help (si); break;
686 update_splash_window (si);
696 unsplash_timer (XtPointer closure, XtIntervalId *id)
698 saver_info *si = (saver_info *) closure;
699 if (si && si->sp_data)
700 destroy_splash_window (si);