home *** CD-ROM | disk | FTP | other *** search
- /*
- * The size of the little squares in the board representation is determined from
- * the size of the board userItem rectangle, which should be
- * (i) square
- * (ii) have a side length of maxSquares * (some integer) + 1
- *
- * This does not use the color manager used to manage the main window.
- * That needs to be fixed if the color model is ever done correctly.
- */
-
- # include "TransSkel.h"
-
- # include "Boxes.h"
-
- # define normalHilite 0
- # define dimHilite 255
-
- typedef enum /* Board Size dialog item numbers */
- {
- iOK = 1, /* OK button */
- iCancel, /* Cancel button */
- iBoard, /* board sizing user item */
- iTitle, /* static text */
- iOutline /* user item for outlining OK button */
- };
-
- static Rect boardRect;
- static short sqSize;
- static short curCols, curRows;
- static short state[maxSquares][maxSquares];
-
-
-
- /*
- * Set the state of the squares in the board representation and redraw any
- * square that has changed state. All squares are drawn if drawAnyway is
- * true, whether they change state or not; this is necessary to cause the
- * board to be drawn correctly the first time.
- */
-
- static void
- SetBoardState (short cols, short rows, Boolean drawAnyway)
- {
- short h, v, oldState, newState;
- short hPos, vPos;
- Rect r;
-
- curCols = cols;
- curRows = rows;
- if (curCols < 0) /* clip values to 0..maxSquares */
- curCols = 0;
- if (curCols > maxSquares)
- curCols = maxSquares;
- if (curRows < 0)
- curRows = 0;
- if (curRows > maxSquares)
- curRows = maxSquares;
- for (h = 0; h < maxSquares; h++)
- {
- for (v = 0; v < maxSquares; v++)
- {
- oldState = state[h][v];
- state[h][v] = newState = (h < curCols & v < curRows);
- /* redraw if always draw or if state has changed */
- if (drawAnyway || oldState != newState)
- {
- hPos = boardRect.left + 1 + h * sqSize;
- vPos = boardRect.top + 1 + v * sqSize;
- SetRect (&r, hPos + 1, vPos + 1, hPos + sqSize - 2, vPos + sqSize - 2);
- if (newState)
- PaintRect (&r);
- else
- EraseRect (&r);
- }
- }
- }
- }
-
-
- /*
- * Filter function for board dialog. Checks for hits in the board
- * size rectangle and follow the mouse as long as it's held down.
- */
-
- static pascal Boolean
- BoardFilter (DialogPtr dlog, EventRecord *evt, short *item)
- {
- Point pt;
- short cols, rows;
- char c;
- short hilite;
-
- if (evt->what == keyDown)
- {
- c = evt->message & charCodeMask;
- if (c == '\r' || c == 3 /* enter */)
- {
- if (curCols * curRows > 0) /* ignore if no squares selected */
- {
- SkelFlashButton (SkelGetDlogCtl (dlog, iOK));
- *item = 1;
- return (true);
- }
- SysBeep (1);
- }
- }
- else if (evt->what == mouseDown)
- {
- pt = evt->where;
- GlobalToLocal (&pt);
- if (PtInRect (pt, &boardRect))
- {
- for (;;)
- {
- cols = ((pt.h - boardRect.left - 1) / sqSize) + 1;
- rows = ((pt.v - boardRect.top - 1) / sqSize) + 1;
- if (cols != curCols || rows != curRows)
- SetBoardState (cols, rows, false);
- if (!StillDown ())
- break;
- GetMouse (&pt);
- }
-
- /*
- * Dim OK button and button outline if board is empty. Don't
- * redraw unless button state actually changes.
- */
-
- hilite = (curCols * curRows > 0 ? normalHilite : dimHilite);
- if (SkelSetDlogCtlHilite (dlog, iOK, hilite))
- SkelDrawButtonOutline (SkelGetDlogCtl (dlog, iOK));
- }
- }
- return (false);
- }
-
-
- static pascal void
- DrawBoardItem (DialogPtr dlog, short item)
- {
- Rect r;
-
- SkelGetDlogRect (dlog, item, &r);
- FrameRect (&r);
- DrawGrid (maxSquares + 1, maxSquares + 1, r.left, r.top, sqSize, sqSize);
- SetBoardState (curCols, curRows, true);
- }
-
-
- void
- BoardDialog (void)
- {
- ModalFilterProcPtr filter;
- DialogPtr dlog;
- GrafPtr savePort;
- short item = 0;
-
- dlog = GetNewDialog (boardDlog, nil, (WindowPtr) -1L);
- if (dlog == (DialogPtr) nil)
- {
- SysBeep (1);
- return;
- }
-
- SkelPositionWindow (dlog, skelPositionOnParentDevice, horizRatio, vertRatio);
-
- SetCursor (&arrow);
- GetPort (&savePort);
- SetPort (dlog);
-
- /* get board rect and set draw proc for board userItem */
- SkelGetDlogRect (dlog, iBoard, &boardRect);
- SkelSetDlogProc (dlog, iBoard, DrawBoardItem);
-
-
- /* install OK button outliner */
- SkelSetDlogButtonOutliner (dlog, iOutline);
-
- /* these must be set before dialog is drawn in order */
- /* that the board be drawn and hilited correctly */
- sqSize = (boardRect.right - boardRect.left - 1 ) / maxSquares;
- GetBoardSize (&curCols, &curRows);
-
- ShowWindow (dlog);
-
- for (;;)
- {
- /* 2nd arg to SkelDlogFilter is false since BoardFilter() handles cr/lf */
- filter = SkelDlogFilter (BoardFilter, false);
- SkelDlogCancelItem (iCancel);
- ModalDialog (filter, &item);
- SkelRmveDlogFilter ();
- if (item == iOK || item == iCancel)
- break;
- }
-
- SetPort (savePort);
- DisposeDialog (dlog);
- if (item == iOK)
- {
- SetBoardSize (curCols, curRows);
- NewGame ();
- }
- }
-