/* $Id: highscores.c,v 1.4 2002/03/02 21:02:21 sverrehu Exp $ */ /************************************************************************** * * FILE highscores.c * MODULE OF Card game. * * DESCRIPTION The highscore list window. * * WRITTEN BY Sverre H. Huseby * **************************************************************************/ #include #include #include #include #include #include #include #include #include #include "win.h" #include "score.h" #include "highscores.h" /************************************************************************** * * * P R I V A T E D A T A * * * **************************************************************************/ static Widget highscoresWidget, highscoresParent, textWidget; static int isPoppedUp = 0; static char *scoreText = NULL; /************************************************************************** * * * P R I V A T E F U N C T I O N S * * * **************************************************************************/ static void callbackOk(Widget w, XtPointer clientData, XtPointer callData) { scoreSetHigscoresChangedCallback(NULL); XtPopdown(highscoresWidget); XtSetSensitive(highscoresButton, 1); isPoppedUp = 0; } static void appendLine(const char *s) { int len, oldlen; len = strlen(s); oldlen = scoreText ? strlen(scoreText) : 0; scoreText = xrealloc(scoreText, oldlen + len + 1); if (!oldlen) *scoreText = '\0'; strcat(scoreText, s); } static void collectScores(void) { int n, thisPlayerIndex; char *s; if (scoreText) { free(scoreText); scoreText = NULL; } appendLine(" "); appendLine(scoreGetHeadStr()); appendLine("\n"); appendLine(" "); appendLine(scoreGetHeadSepStr()); appendLine("\n"); n = 0; thisPlayerIndex = scoreGetThisPlayerIndex(); while ((s = scoreGetEntryStr(n)) != NULL) { if (n == thisPlayerIndex) appendLine("=> "); else appendLine(" "); appendLine(s); if (n == thisPlayerIndex) appendLine(" <="); else appendLine(" "); appendLine("\n"); ++n; } } static void highscoresChanged(void) { collectScores(); XtVaSetValues(textWidget, XtNstring, scoreText, NULL); } /************************************************************************** * * * P U B L I C F U N C T I O N S * * * **************************************************************************/ void highscoresInit(Widget parent) { Widget form, button; highscoresParent = parent; highscoresWidget = XtVaCreatePopupShell("highscoreWindow", topLevelShellWidgetClass, parent, XtNtitle, "Sol Highscores", XtNallowShellResize, 1, NULL); form = XtVaCreateManagedWidget("highscoresFrom", formWidgetClass, highscoresWidget, NULL); textWidget = XtVaCreateManagedWidget("highscores", asciiTextWidgetClass, form, XtNwidth, 66, XtNheight, 124, XtNdisplayCaret, 0, XtNscrollVertical, XawtextScrollWhenNeeded, XtNresizable, 1, XtNresize, XawtextResizeWidth, NULL); button = XtVaCreateManagedWidget("ok", commandWidgetClass, form, XtNlabel, "I don't care about highscores", XtNfromVert, textWidget, NULL); XtAddCallback(button, XtNcallback, callbackOk, 0); } void highscoresFinish(void) { free(scoreText); } void highscoresPopup(void) { if (isPoppedUp) return; XtSetSensitive(highscoresButton, 0); isPoppedUp = 1; collectScores(); XtVaSetValues(textWidget, XtNstring, scoreText, NULL); XtPopup(highscoresWidget, XtGrabNone); scoreSetHigscoresChangedCallback(highscoresChanged); }