1 /* $Id: highscores.c,v 1.4 2002/03/02 21:02:21 sverrehu Exp $ */
2 /**************************************************************************
3  *
4  *  FILE            highscores.c
5  *  MODULE OF       Card game.
6  *
7  *  DESCRIPTION     The highscore list window.
8  *
9  *  WRITTEN BY      Sverre H. Huseby <shh@thathost.com>
10  *
11  **************************************************************************/
12 
13 #include <stdlib.h>
14 
15 #include <X11/Xlib.h>
16 #include <X11/Intrinsic.h>
17 #include <X11/StringDefs.h>
18 #include <X11/Shell.h>
19 #include <X11/Xaw/Command.h>
20 #include <X11/Xaw/AsciiText.h>
21 #include <X11/Xaw/Form.h>
22 
23 #include <xalloc.h>
24 
25 #include "win.h"
26 #include "score.h"
27 #include "highscores.h"
28 
29 /**************************************************************************
30  *                                                                        *
31  *                       P R I V A T E    D A T A                         *
32  *                                                                        *
33  **************************************************************************/
34 
35 static Widget highscoresWidget, highscoresParent, textWidget;
36 static int isPoppedUp = 0;
37 static char *scoreText = NULL;
38 
39 
40 
41 /**************************************************************************
42  *                                                                        *
43  *                   P R I V A T E    F U N C T I O N S                   *
44  *                                                                        *
45  **************************************************************************/
46 
47 static void
callbackOk(Widget w,XtPointer clientData,XtPointer callData)48 callbackOk(Widget w, XtPointer clientData, XtPointer callData)
49 {
50     scoreSetHigscoresChangedCallback(NULL);
51     XtPopdown(highscoresWidget);
52     XtSetSensitive(highscoresButton, 1);
53     isPoppedUp = 0;
54 }
55 
56 static void
appendLine(const char * s)57 appendLine(const char *s)
58 {
59     int len, oldlen;
60 
61     len = strlen(s);
62     oldlen = scoreText ? strlen(scoreText) : 0;
63     scoreText = xrealloc(scoreText, oldlen + len + 1);
64     if (!oldlen)
65 	*scoreText = '\0';
66     strcat(scoreText, s);
67 }
68 
69 static void
collectScores(void)70 collectScores(void)
71 {
72     int  n, thisPlayerIndex;
73     char *s;
74 
75     if (scoreText) {
76 	free(scoreText);
77 	scoreText = NULL;
78     }
79     appendLine("   ");
80     appendLine(scoreGetHeadStr());
81     appendLine("\n");
82     appendLine("   ");
83     appendLine(scoreGetHeadSepStr());
84     appendLine("\n");
85     n = 0;
86     thisPlayerIndex = scoreGetThisPlayerIndex();
87     while ((s = scoreGetEntryStr(n)) != NULL) {
88 	if (n == thisPlayerIndex)
89 	    appendLine("=> ");
90 	else
91 	    appendLine("   ");
92 	appendLine(s);
93 	if (n == thisPlayerIndex)
94 	    appendLine(" <=");
95 	else
96 	    appendLine("   ");
97 	appendLine("\n");
98 	++n;
99     }
100 }
101 
102 static void
highscoresChanged(void)103 highscoresChanged(void)
104 {
105     collectScores();
106     XtVaSetValues(textWidget,
107 		  XtNstring, scoreText,
108 		  NULL);
109 }
110 
111 
112 
113 /**************************************************************************
114  *                                                                        *
115  *                    P U B L I C    F U N C T I O N S                    *
116  *                                                                        *
117  **************************************************************************/
118 
119 void
highscoresInit(Widget parent)120 highscoresInit(Widget parent)
121 {
122     Widget form, button;
123 
124     highscoresParent = parent;
125     highscoresWidget
126 	= XtVaCreatePopupShell("highscoreWindow",
127 			       topLevelShellWidgetClass, parent,
128 			       XtNtitle, "Sol Highscores",
129 			       XtNallowShellResize, 1,
130 			       NULL);
131     form
132 	= XtVaCreateManagedWidget("highscoresFrom",
133 				  formWidgetClass, highscoresWidget,
134 				  NULL);
135     textWidget
136 	= XtVaCreateManagedWidget("highscores",
137 				  asciiTextWidgetClass, form,
138 				  XtNwidth, 66,
139 				  XtNheight, 124,
140 				  XtNdisplayCaret, 0,
141 				  XtNscrollVertical, XawtextScrollWhenNeeded,
142 				  XtNresizable, 1,
143 				  XtNresize, XawtextResizeWidth,
144 				  NULL);
145     button
146 	= XtVaCreateManagedWidget("ok",
147 				  commandWidgetClass, form,
148 				  XtNlabel, "I don't care about highscores",
149 				  XtNfromVert, textWidget,
150 				  NULL);
151     XtAddCallback(button, XtNcallback, callbackOk, 0);
152 }
153 
154 void
highscoresFinish(void)155 highscoresFinish(void)
156 {
157     free(scoreText);
158 }
159 
160 void
highscoresPopup(void)161 highscoresPopup(void)
162 {
163     if (isPoppedUp)
164 	return;
165     XtSetSensitive(highscoresButton, 0);
166     isPoppedUp = 1;
167 
168     collectScores();
169     XtVaSetValues(textWidget, XtNstring, scoreText, NULL);
170     XtPopup(highscoresWidget, XtGrabNone);
171     scoreSetHigscoresChangedCallback(highscoresChanged);
172 }
173