home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / Kibitz / GoToMove.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-06  |  4.6 KB  |  195 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        gotomove.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __PACKAGES__
  21. #include <Packages.h>
  22. #endif
  23.  
  24. #ifndef __RESOURCES__
  25. #include <Resources.h>
  26. #endif
  27.  
  28. #ifndef __TOOLUTILS__
  29. #include <ToolUtils.h>
  30. #endif
  31.  
  32. #ifndef __UTILITIES__
  33. #include <Utilities.h>
  34. #endif
  35.  
  36.  
  37.  
  38. /*****************************************************************************/
  39.  
  40.  
  41.  
  42. #define kGoToOK        1
  43. #define kGoToEquivs    2
  44. #define kGoToCancel    3
  45. #define kStatText1    4
  46. #define kWhichMove    5
  47. #define kWhiteMove    6
  48. #define kBlackMove    7
  49.  
  50. #define kLeftArrow    28
  51. #define kRightArrow    29
  52. #define kUpArrow    30
  53. #define kDownArrow    31
  54.  
  55. static short    gOption;
  56.  
  57. static pascal Boolean    GoToFilter(DialogPtr dlg, EventRecord *event, short *item);
  58.  
  59.  
  60.  
  61. /*****************************************************************************/
  62. /*****************************************************************************/
  63.  
  64.  
  65.  
  66. #pragma segment GoTo
  67. void    DoGoToMove(FileRecHndl frHndl)
  68. {
  69.     WindowPtr        oldPort, window;
  70.     DialogPtr        dialog;
  71.     short            item, userItemType, startColor, halfMoveNum, moveNum;
  72.     short            gameIndex, whosMove, i;
  73.     Handle            itemHndl;
  74.     Rect            userItemRect;
  75.     TEHandle        te;
  76.     Str255            str;
  77.     TheDocPtr        docPtr;
  78.     static ModalFilterUPP goToFilterUPP = nil;
  79.  
  80.     oldPort = SetFilePort(frHndl);
  81.  
  82.     window = (*frHndl)->fileState.window;
  83.     dialog = GetCenteredDialog(rGoToMove, nil, window, (WindowPtr)-1L);
  84.     if (dialog) {
  85.  
  86.         startColor  = (*frHndl)->doc.startColor;
  87.         halfMoveNum = (*frHndl)->doc.gameIndex + startColor;
  88.         moveNum     = (halfMoveNum >> 1);
  89.  
  90.         item = kWhiteMove + (halfMoveNum - 2 * moveNum);
  91.         GetDialogItem(dialog, item, &userItemType, &itemHndl, &userItemRect);
  92.         SetControlValue((ControlHandle)itemHndl, true);
  93.  
  94.         te = ((DialogPeek)dialog)->textH;
  95.         pcpydec(str, moveNum + 1);
  96.         TESetText(str + 1, *str, te);
  97.         TESetSelect(0, *str, te);
  98.  
  99.         for (;;) {
  100.  
  101.             SetPort(dialog);
  102.             if (!goToFilterUPP)
  103.                 goToFilterUPP = NewModalFilterProc (GoToFilter);
  104.             ModalDialog(goToFilterUPP, &item);
  105.  
  106.             if (item == kGoToOK) {
  107.                 GetDialogItem(dialog, kBlackMove, &userItemType, &itemHndl, &userItemRect);
  108.                 whosMove = GetControlValue((ControlHandle)itemHndl);
  109.                 BlockMove(*((*te)->hText), str + 1, *str = (*te)->teLength);
  110.                 break;
  111.             }
  112.  
  113.             if (item == kGoToCancel) break;
  114.  
  115.             if (item >= kWhiteMove) {
  116.                 GetDialogItem(dialog, item, &userItemType, &itemHndl, &userItemRect);
  117.                 SetControlValue((ControlHandle)itemHndl, true);
  118.                 item = (kWhiteMove + kBlackMove) - item;
  119.                 GetDialogItem(dialog, item, &userItemType, &itemHndl, &userItemRect);
  120.                 SetControlValue((ControlHandle)itemHndl, false);
  121.             }
  122.         }
  123.  
  124.         DisposeDialog(dialog);
  125.  
  126.         if (item == kGoToOK) {
  127.             moveNum   = p2num(str, 10, nil) - 1;
  128.             gameIndex = ( 2 * moveNum) + whosMove - startColor;
  129.             if (gOption) {
  130.                 moveNum = gameIndex = 0;
  131.                 docPtr  = &((*frHndl)->doc);
  132.                 if (docPtr->timeLeft[0] != -1)
  133.                     for (i = 0; i < 2; ++i)
  134.                         docPtr->displayTime[i] = docPtr->timeLeft[i] = docPtr->defaultTime[i];
  135.             }
  136.             if ((moveNum >= 0) && (gameIndex >= 0)) {
  137.                 SetFilePort(frHndl);
  138.                 RepositionBoard(frHndl, gameIndex, true);
  139.                 ImageDocument(frHndl, true);
  140.                 AdjustGameSlider(frHndl);
  141.                 if ((*frHndl)->doc.twoPlayer) SendGame(frHndl, kResync, nil);
  142.                 SendMssg(frHndl, kTimeMssg);
  143.                 (*frHndl)->doc.timerRefTick = TickCount();
  144.                 DrawTime(frHndl);
  145.             }
  146.         }
  147.     }
  148.  
  149.     SetPort(oldPort);
  150. }
  151.  
  152.  
  153.  
  154. /*****************************************************************************/
  155.  
  156.  
  157.  
  158. #pragma segment Config
  159. pascal Boolean    GoToFilter(DialogPtr dlg, EventRecord *event, short *item)
  160. {
  161.     TEHandle                te;
  162.     char                    key;
  163.     Boolean                    arrowKey;
  164.     static unsigned long    lastIdle;
  165.  
  166.     gOption = (event->modifiers) & optionKey;
  167.  
  168.     if (KeyEquivFilter(dlg, event, item)) return(true);
  169.  
  170.     *item = 0;
  171.  
  172.     switch (event->what) {
  173.         case keyDown:
  174.         case autoKey:
  175.             te       = ((DialogPeek)dlg)->textH;
  176.             key      = event->message & charCodeMask;
  177.             arrowKey = ((key >= kLeftArrow) && (key <= kDownArrow));
  178.             if ((arrowKey) || (key == 8)) return(false);    /* These are always okay. */
  179.             if ((key < '0') || (key > '9')) return(true);    /* Don't accept non-digits. */
  180.             if ((*te)->teLength >= 3) return(true);            /* Don't accept more than 3 chars. */
  181.             break;
  182.     }
  183.  
  184.     if (lastIdle + 30 < TickCount()) {
  185.         lastIdle = TickCount();
  186.         DoIdleTasks(false);
  187.         SetPort(dlg);
  188.     }
  189.  
  190.     return(false);
  191. }
  192.  
  193.  
  194.  
  195.