1 /* $Id: about.c,v 1.4 2002/03/02 21:02:21 sverrehu Exp $ */
2 /**************************************************************************
3  *
4  *  FILE            about.c
5  *  MODULE OF       Card game.
6  *
7  *  WRITTEN BY      Sverre H. Huseby <shh@thathost.com>
8  *
9  **************************************************************************/
10 
11 #include <X11/Xlib.h>
12 #include <X11/Intrinsic.h>
13 #include <X11/StringDefs.h>
14 #include <X11/Shell.h>
15 #include <X11/Xaw/Command.h>
16 #include <X11/Xaw/AsciiText.h>
17 #include <X11/Xaw/Form.h>
18 #include <X11/xpm.h>
19 
20 #include <shhmsg.h>
21 
22 #include "win.h"
23 #include "game.h"
24 #include "about.h"
25 
26 /**************************************************************************
27  *                                                                        *
28  *                       P R I V A T E    D A T A                         *
29  *                                                                        *
30  **************************************************************************/
31 
32 #include "shapes.xpm"
33 
34 static char *title = "About sol";
35 static char *copyright =
36     "sol " VERSION " - Solitaire card game\n"
37     "\n"
38     "by Sverre H. Huseby, Norway\n"
39     "<shh@thathost.com>\n"
40     "\n"
41     "This program is free:\n"
42     "     Copy it and use it as you wish.\n";
43 static Widget aboutWidget, aboutParent, aboutImage, aboutStatus;
44 static Pixmap aboutPixmap;
45 static int isPoppedUp = 0;
46 
47 
48 
49 /**************************************************************************
50  *                                                                        *
51  *                   P R I V A T E    F U N C T I O N S                   *
52  *                                                                        *
53  **************************************************************************/
54 
55 static void
callbackOk(Widget w,XtPointer clientData,XtPointer callData)56 callbackOk(Widget w, XtPointer clientData, XtPointer callData)
57 {
58     XtPopdown(aboutWidget);
59     XFreePixmap(XtDisplayOfObject(aboutWidget), aboutPixmap);
60     XtSetSensitive(aboutButton, 1);
61     isPoppedUp = 0;
62 }
63 
64 static void
createPixmap(char ** data,Pixmap * pix)65 createPixmap(char **data, Pixmap *pix)
66 {
67     int err;
68     XpmAttributes xa;
69 
70     xa.valuemask = 0;
71     xa.closeness = 40000;
72     xa.valuemask |= XpmCloseness;
73     err = XpmCreatePixmapFromData(XtDisplay(aboutParent),
74 				  XtWindow(aboutParent),
75 				  data, pix, NULL, &xa);
76     if (err == XpmSuccess)
77 	return;
78     else if (err > 0)
79 	msgFatal("Xpm: %s\n", XpmGetErrorString(err));
80 }
81 
82 
83 
84 /**************************************************************************
85  *                                                                        *
86  *                    P U B L I C    F U N C T I O N S                    *
87  *                                                                        *
88  **************************************************************************/
89 
90 void
aboutInit(Widget parent)91 aboutInit(Widget parent)
92 {
93     Widget form, text, button;
94 
95     aboutParent = parent;
96     aboutWidget
97 	= XtVaCreatePopupShell("aboutWindow",
98 			       transientShellWidgetClass, parent,
99 			       XtNtitle, title,
100 			       NULL);
101     form
102 	= XtVaCreateManagedWidget("about",
103 				  formWidgetClass, aboutWidget,
104 				  XtNdefaultDistance, 8,
105 				  NULL);
106     aboutImage
107 	= XtVaCreateManagedWidget("shapes",
108 				  coreWidgetClass, form,
109 				  XtNwidth, 160,
110 				  XtNheight, 100,
111 				  XtNborderWidth, 1,
112 				  NULL);
113     text
114 	= XtVaCreateManagedWidget("copyright",
115 				  asciiTextWidgetClass, form,
116 				  XtNwidth, 250,
117 				  XtNheight, 100,
118 				  XtNborderWidth, 0,
119 				  XtNfromHoriz, aboutImage,
120 				  XtNstring, copyright,
121 				  XtNdisplayCaret, 0,
122 				  XtNsensitive, 0,
123 				  NULL);
124     button
125 	= XtVaCreateManagedWidget("ok",
126 				  commandWidgetClass, form,
127 				  XtNlabel, "OK",
128 				  XtNfromHoriz, text,
129 				  NULL);
130     aboutStatus
131 	= XtVaCreateManagedWidget("status",
132 				  asciiTextWidgetClass, form,
133 				  XtNwidth, 450,
134 				  XtNheight, 24,
135 				  XtNborderWidth, 0,
136 				  XtNfromVert, aboutImage,
137 				  XtNdisplayCaret, 0,
138 				  XtNsensitive, 0,
139 				  XtNwrap, XawtextWrapWord,
140 				  NULL);
141     XtAddCallback(button, XtNcallback, callbackOk, 0);
142 }
143 
144 void
aboutFinish(void)145 aboutFinish(void)
146 {
147 }
148 
149 void
aboutPopup(void)150 aboutPopup(void)
151 {
152     Dimension popWidth, popHeight, parWidth, parHeight;
153     Position x, y;
154     char statusMsg[320], s1[20], s2[20];
155 
156     if (isPoppedUp)
157 	return;
158     XtSetSensitive(aboutButton, 0);
159     isPoppedUp = 1;
160     createPixmap(shapes, &aboutPixmap);
161 
162     if (!gameNumPlayed) {
163 	sprintf(statusMsg,
164 		"You haven't played the game yet. Go on, try it!");
165     } else if (gameNumPlayed == 1) {
166 	sprintf(statusMsg,
167 		"In %s, you have played one game.",
168 		gameTimeToVerboseStr(gameTotalTime));
169     } else {
170 	strcpy(s1, gameCountToStr(gameNumPlayed));
171 	strcpy(s2, gameCountToStr(gameNumSolved));
172 	sprintf(statusMsg,
173 		"In %s, you have played %s games, of which %s are solved.",
174 		gameTimeToVerboseStr(gameTotalTime), s1, s2);
175     }
176     XtVaSetValues(aboutStatus, XtNstring, statusMsg, NULL);
177 
178     /* must temporarily realize the widget to be able to get it's size */
179     XtRealizeWidget(aboutWidget);
180     XtUnrealizeWidget(aboutWidget);
181 
182     XtVaGetValues(aboutWidget,
183 		  XtNwidth, &popWidth, XtNheight, &popHeight, NULL);
184     XtVaGetValues(aboutParent,
185 		  XtNwidth, &parWidth, XtNheight, &parHeight, NULL);
186     XtTranslateCoords(aboutParent,
187 		      (Position) (parWidth - popWidth) / 2,
188 		      (Position) (parHeight - popHeight) / 2,
189 		      &x, &y);
190     XtVaSetValues(aboutWidget,
191 		  XtNx, x, XtNy, y,
192 		  NULL);
193     XtVaSetValues(aboutImage,
194 		  XtNbackgroundPixmap, aboutPixmap,
195 		  NULL);
196     XtPopup(aboutWidget, XtGrabNone);
197 }
198