home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Games / Bolo 0.99.6 / More information / Sample Code / Std Autopilot / BF_Utils.c < prev    next >
Encoding:
Text File  |  1995-04-28  |  7.6 KB  |  274 lines  |  [TEXT/KAHL]

  1. // Bolo code (C) Stuart Cheshire <cheshire@cs.stanford.edu> 1987-1995.
  2. // All rights reserved. This code is owned by Stuart Cheshire and is donated
  3. // free of charge for non-commercial use. You may not use this code in any
  4. // product sold for commercial profit, except shareware priced at $25 or less.
  5.  
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8.  
  9. #include <Types.h>
  10. #include <Resources.h>
  11. #include <GestaltEqu.h>
  12. #include <Notification.h>
  13. #include <TextUtils.h>
  14. #include <Traps.h>
  15.  
  16. #include "BrainFrame.h"
  17. #include "BF_ResourceIDs.h"
  18. #include "BF_Main.h"
  19. #include "BF_Globals.h"
  20. #include "BF_Events.h"
  21. #include "BF_Utils.h"
  22.  
  23. export void PrepareForAlert(void)
  24.     {
  25.     if (!frontapp)
  26.         {
  27.         NMRec nmr;
  28.         nmr.qType   = nmType;            // qType for Notification Manager
  29.         nmr.nmMark  = 1;                // Yes, mark this application in the menu
  30.         nmr.nmIcon  = MyIconSuite;        // Display this icon in the menu bar
  31.         nmr.nmSound = (Handle)-1;        // Standard system beep
  32.         nmr.nmStr   = "\pBrainFrame needs your attention. "
  33.             "Please select “BrainFrame” on the Application menu "
  34.             "to bring the BrainFrame application program to the front";
  35.         nmr.nmResp  = NULL;
  36.         NMInstall(&nmr);
  37.         notification_posted = TRUE;        // make sure we don't try another alert
  38.         while (!frontapp) handle_next_event(NULL, NULL);
  39.         notification_posted = FALSE;
  40.         NMRemove(&nmr);
  41.         }
  42.     SetCursor(&qd.arrow);
  43.     }
  44.  
  45. export short MyCautionAlert(short id)
  46.     { PrepareForAlert(); return(CautionAlert(id, NULL)); }
  47.  
  48. export short MyStopAlert(short id)
  49.     { PrepareForAlert(); return(StopAlert(id, NULL)); }
  50.  
  51. export short GeneralStopAlert(OSErr error, u_char *explanation)
  52.     {
  53.     u_char str[32];
  54.     NumToString(error, str);
  55.     ParamText(str, explanation, NULL, NULL);
  56.     return(MyStopAlert(alert_generalerror));
  57.     }
  58.  
  59. export void HiliteControl_by_item(DialogPtr dlog, short item, short hiliteState)
  60.     {
  61.     short    di_type;
  62.     Handle    di_handle;
  63.     Rect    di_box;
  64.     GetDItem(dlog, item, &di_type, &di_handle, &di_box);
  65.     HiliteControl((ControlHandle)di_handle, hiliteState);
  66.     }
  67.  
  68. export void RedrawControl_by_item(DialogPtr dlog, short item)
  69.     {
  70.     short    di_type;
  71.     Handle    di_handle;
  72.     Rect    di_box;
  73.     GetDItem(dlog, item, &di_type, &di_handle, &di_box);
  74.     InvalRect(&di_box);
  75.     }
  76.  
  77. export void SetCtlValue_by_item(DialogPtr dlog, short item, short theValue)
  78.     {
  79.     short    di_type;
  80.     Handle    di_handle;
  81.     Rect    di_box;
  82.     GetDItem(dlog, item, &di_type, &di_handle, &di_box);
  83.     SetCtlValue((ControlHandle)di_handle, theValue);
  84.     }
  85.  
  86. export short GetCtlValue_by_item(DialogPtr dlog, short item)
  87.     {
  88.     short    di_type;
  89.     Handle    di_handle;
  90.     Rect    di_box;
  91.     GetDItem(dlog, item, &di_type, &di_handle, &di_box);
  92.     return(GetCtlValue((ControlHandle)di_handle));
  93.     }
  94.  
  95. export void SetIText_by_item(DialogPtr dlog, short item, StringPtr theString)
  96.     {
  97.     short    di_type;
  98.     Handle    di_handle;
  99.     Rect    di_box;
  100.     GetDItem(dlog, item, &di_type, &di_handle, &di_box);
  101.     SetIText(di_handle, theString);
  102.     }
  103.  
  104. export void GetIText_by_item(DialogPtr dlog, short item, StringPtr theString)
  105.     {
  106.     short    di_type;
  107.     Handle    di_handle;
  108.     Rect    di_box;
  109.     GetDItem(dlog, item, &di_type, &di_handle, &di_box);
  110.     GetIText(di_handle, theString);
  111.     }
  112.  
  113. local pascal void OutlineOK(DialogPtr dlg, short item)
  114.     {
  115.     short    di_type;
  116.     Handle    di_handle;
  117.     Rect    di_box;
  118.     item;    // Unused
  119.     PenSize(3,3);
  120.     GetDItem(dlg, 1, &di_type, &di_handle, &di_box);
  121.     InsetRect(&di_box,-4,-4);
  122.     FrameRoundRect(&di_box,16,16);
  123.     PenNormal();
  124.     }
  125.  
  126. export void set_OutlineOK(DialogPtr d, short item)
  127.     {
  128.     short    di_type;
  129.     Handle    di_handle;
  130.     Rect    di_box;
  131.     GetDItem(d, item, &di_type, &di_handle, &di_box);
  132.     SetDItem(d, item,  di_type, (Handle)OutlineOK, &di_box);
  133.     }
  134.  
  135. /*************************************************************************/
  136.  
  137. local pascal Boolean StandardFilterProc(DialogPtr dlog, EventRecord *event, short *itemHit)
  138.     {
  139.     char key = event->message & charCodeMask;
  140.     dlog;    // Unused
  141.     
  142.     // Only do special processing for keyboard events
  143.     if (event->what != keyDown && event->what != autoKey) return false;
  144.     
  145.     if (key==3 || key==13) { *itemHit = 1; return(true); }
  146.     if (key==27) { *itemHit = 2; return(true); }
  147.     
  148.     return false;
  149.     }
  150.  
  151. export void MovableModalDialog(ModalFilterProcPtr FilterProc, short *item)
  152.     {
  153.     if (quitting) return;
  154.     if (!FilterProc) FilterProc = StandardFilterProc;
  155.     while (!handle_next_event(FilterProc, item)) continue;
  156.     }
  157.  
  158. typedef struct
  159.     {
  160.     short maxitem;    // number of items in list minus one
  161.     char data[];
  162.     } **DITLHndl;
  163.  
  164. typedef struct { short pos; MenuHandle handle; } MenuBarEntry;
  165. typedef struct { short size; short width; MenuBarEntry menu[1]; } MenuBar;
  166. typedef struct MenuStates MenuStates;
  167. typedef struct MenuStates { MenuStates **next; long state[]; };
  168. local MenuStates **MRestoreState = NULL;
  169.  
  170. export DialogPtr OpenMovableModalDialog(short dialogid)
  171.     {
  172.     int i;
  173.     MenuBar **mbar   = (MenuBar **)GetMenuBar();
  174.     short num_menus  = mbar && *mbar ? (*mbar)->size / sizeof(MenuBarEntry) : 0;
  175.     MenuStates **m   = (MenuStates **)
  176.                         NewHandle(sizeof(MenuStates) + sizeof(long) * num_menus);
  177.     DialogPtr dlg    = GetNewDialog(dialogid, NULL, (WindowPtr)(-1));
  178.     DITLHndl ditl    = (DITLHndl)GetResource('DITL', dialogid);
  179.     long editsetting = 0;
  180.     
  181.     if (!mbar || !m || !dlg || !ditl)
  182.         {
  183.         if (mbar) DisposHandle((Handle)mbar);
  184.         if (m   ) DisposHandle((Handle)m);
  185.         if (dlg ) DisposDialog(dlg);
  186.         if (ditl) ReleaseResource((Handle)ditl);
  187.         return(NULL);
  188.         }
  189.  
  190.     (*m)->next = MRestoreState;
  191.     MRestoreState = m;
  192.     // Stash a copy of the current menu settings
  193.     for (i=1; i<num_menus; i++)
  194.         (*m)->state[i] = (*((*mbar)->menu[i].handle))->enableFlags;
  195.  
  196.     // Check if this dialog box has any editText fields
  197.     for (i=0; i<=(*ditl)->maxitem; i++)
  198.         {
  199.         short    di_type;
  200.         Handle    di_handle;
  201.         Rect    di_box;
  202.         GetDItem(dlg, i+1, &di_type, &di_handle, &di_box);
  203.         if (di_type == editText) { editsetting = 1+2+8+16+32+64; break; }
  204.         }
  205.     
  206.     // Set the new menu states: File menu, Edit Menu, and the rest
  207.     (*((*mbar)->menu[1].handle))->enableFlags = 0;
  208.     (*((*mbar)->menu[2].handle))->enableFlags = editsetting;
  209.     for (i=3; i<num_menus; i++) (*((*mbar)->menu[i].handle))->enableFlags = 0;
  210.     HiliteMenu(0);        // zero means unhilite all hilited menus
  211.     DrawMenuBar();        // redraw the menu bar
  212.     DisposeHandle((Handle)mbar);
  213.     ReleaseResource((Handle)ditl);
  214.     return(dlg);
  215.     }
  216.  
  217. export void CloseMovableModalDialog(DialogPtr modaldialog)
  218.     {
  219.     int i;
  220.     MenuBar **mbar   = (MenuBar **)GetMenuBar();
  221.     short num_menus  = mbar && *mbar ? (*mbar)->size / sizeof(MenuBarEntry) : 0;
  222.     MenuStates **m = MRestoreState;
  223.     MRestoreState = (*MRestoreState)->next;
  224.     for (i=1; i<num_menus; i++)
  225.         (*((*mbar)->menu[i].handle))->enableFlags = (*m)->state[i];
  226.     DrawMenuBar();
  227.     DisposeHandle((Handle)mbar);
  228.     DisposHandle((Handle)m);
  229.     DisposDialog(modaldialog);
  230.     }
  231.  
  232. /*************************************************************************/
  233.  
  234. export Boolean TrapAvailable(u_long trap)
  235.     {
  236.     TrapType tType = (trap & 0x800 ? ToolTrap : OSTrap);
  237.     if (trap & 0x800)                // if it is a ToolBox Trap
  238.         {
  239.         u_long n = 0x400;    // number of toolbox traps
  240.         if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  241.             n = 0x200;
  242.         if ((trap &= 0x7FF) >= n) trap = _Unimplemented;
  243.         }
  244.     return(NGetTrapAddress(trap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  245.     }
  246.  
  247. export void fatal(char *format, ...)
  248.     {
  249.     unsigned char buffer[256];
  250.     va_list ptr;
  251.     va_start(ptr,format);
  252.     buffer[0] = vsprintf((char *)buffer+1, format, ptr);
  253.     va_end(ptr);
  254.     DebugStr(buffer);
  255.     }
  256.  
  257. /*************************************************************************/
  258.  
  259. export short c_compare_structs(register u_short size, void *a, void *b)
  260.     {
  261.     register BYTE *x = (BYTE *)a;
  262.     register BYTE *y = (BYTE *)b;
  263.     while (size-->0) if(*x-*y) return(*x-*y); else { x++; y++; }
  264.     return(0);
  265.     }
  266.  
  267. export short c_copy_struct(register u_short size, void *src, void *dst)
  268.     {
  269.     register BYTE *x = (BYTE *)src;
  270.     register BYTE *y = (BYTE *)dst;
  271.     while (size-->0) *y++ = *x++;
  272.     return(0);
  273.     }
  274.