home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-04-28 | 7.6 KB | 274 lines | [TEXT/KAHL] |
- // Bolo code (C) Stuart Cheshire <cheshire@cs.stanford.edu> 1987-1995.
- // All rights reserved. This code is owned by Stuart Cheshire and is donated
- // free of charge for non-commercial use. You may not use this code in any
- // product sold for commercial profit, except shareware priced at $25 or less.
-
- #include <stdarg.h>
- #include <stdio.h>
-
- #include <Types.h>
- #include <Resources.h>
- #include <GestaltEqu.h>
- #include <Notification.h>
- #include <TextUtils.h>
- #include <Traps.h>
-
- #include "BrainFrame.h"
- #include "BF_ResourceIDs.h"
- #include "BF_Main.h"
- #include "BF_Globals.h"
- #include "BF_Events.h"
- #include "BF_Utils.h"
-
- export void PrepareForAlert(void)
- {
- if (!frontapp)
- {
- NMRec nmr;
- nmr.qType = nmType; // qType for Notification Manager
- nmr.nmMark = 1; // Yes, mark this application in the menu
- nmr.nmIcon = MyIconSuite; // Display this icon in the menu bar
- nmr.nmSound = (Handle)-1; // Standard system beep
- nmr.nmStr = "\pBrainFrame needs your attention. "
- "Please select “BrainFrame” on the Application menu "
- "to bring the BrainFrame application program to the front";
- nmr.nmResp = NULL;
- NMInstall(&nmr);
- notification_posted = TRUE; // make sure we don't try another alert
- while (!frontapp) handle_next_event(NULL, NULL);
- notification_posted = FALSE;
- NMRemove(&nmr);
- }
- SetCursor(&qd.arrow);
- }
-
- export short MyCautionAlert(short id)
- { PrepareForAlert(); return(CautionAlert(id, NULL)); }
-
- export short MyStopAlert(short id)
- { PrepareForAlert(); return(StopAlert(id, NULL)); }
-
- export short GeneralStopAlert(OSErr error, u_char *explanation)
- {
- u_char str[32];
- NumToString(error, str);
- ParamText(str, explanation, NULL, NULL);
- return(MyStopAlert(alert_generalerror));
- }
-
- export void HiliteControl_by_item(DialogPtr dlog, short item, short hiliteState)
- {
- short di_type;
- Handle di_handle;
- Rect di_box;
- GetDItem(dlog, item, &di_type, &di_handle, &di_box);
- HiliteControl((ControlHandle)di_handle, hiliteState);
- }
-
- export void RedrawControl_by_item(DialogPtr dlog, short item)
- {
- short di_type;
- Handle di_handle;
- Rect di_box;
- GetDItem(dlog, item, &di_type, &di_handle, &di_box);
- InvalRect(&di_box);
- }
-
- export void SetCtlValue_by_item(DialogPtr dlog, short item, short theValue)
- {
- short di_type;
- Handle di_handle;
- Rect di_box;
- GetDItem(dlog, item, &di_type, &di_handle, &di_box);
- SetCtlValue((ControlHandle)di_handle, theValue);
- }
-
- export short GetCtlValue_by_item(DialogPtr dlog, short item)
- {
- short di_type;
- Handle di_handle;
- Rect di_box;
- GetDItem(dlog, item, &di_type, &di_handle, &di_box);
- return(GetCtlValue((ControlHandle)di_handle));
- }
-
- export void SetIText_by_item(DialogPtr dlog, short item, StringPtr theString)
- {
- short di_type;
- Handle di_handle;
- Rect di_box;
- GetDItem(dlog, item, &di_type, &di_handle, &di_box);
- SetIText(di_handle, theString);
- }
-
- export void GetIText_by_item(DialogPtr dlog, short item, StringPtr theString)
- {
- short di_type;
- Handle di_handle;
- Rect di_box;
- GetDItem(dlog, item, &di_type, &di_handle, &di_box);
- GetIText(di_handle, theString);
- }
-
- local pascal void OutlineOK(DialogPtr dlg, short item)
- {
- short di_type;
- Handle di_handle;
- Rect di_box;
- item; // Unused
- PenSize(3,3);
- GetDItem(dlg, 1, &di_type, &di_handle, &di_box);
- InsetRect(&di_box,-4,-4);
- FrameRoundRect(&di_box,16,16);
- PenNormal();
- }
-
- export void set_OutlineOK(DialogPtr d, short item)
- {
- short di_type;
- Handle di_handle;
- Rect di_box;
- GetDItem(d, item, &di_type, &di_handle, &di_box);
- SetDItem(d, item, di_type, (Handle)OutlineOK, &di_box);
- }
-
- /*************************************************************************/
-
- local pascal Boolean StandardFilterProc(DialogPtr dlog, EventRecord *event, short *itemHit)
- {
- char key = event->message & charCodeMask;
- dlog; // Unused
-
- // Only do special processing for keyboard events
- if (event->what != keyDown && event->what != autoKey) return false;
-
- if (key==3 || key==13) { *itemHit = 1; return(true); }
- if (key==27) { *itemHit = 2; return(true); }
-
- return false;
- }
-
- export void MovableModalDialog(ModalFilterProcPtr FilterProc, short *item)
- {
- if (quitting) return;
- if (!FilterProc) FilterProc = StandardFilterProc;
- while (!handle_next_event(FilterProc, item)) continue;
- }
-
- typedef struct
- {
- short maxitem; // number of items in list minus one
- char data[];
- } **DITLHndl;
-
- typedef struct { short pos; MenuHandle handle; } MenuBarEntry;
- typedef struct { short size; short width; MenuBarEntry menu[1]; } MenuBar;
- typedef struct MenuStates MenuStates;
- typedef struct MenuStates { MenuStates **next; long state[]; };
- local MenuStates **MRestoreState = NULL;
-
- export DialogPtr OpenMovableModalDialog(short dialogid)
- {
- int i;
- MenuBar **mbar = (MenuBar **)GetMenuBar();
- short num_menus = mbar && *mbar ? (*mbar)->size / sizeof(MenuBarEntry) : 0;
- MenuStates **m = (MenuStates **)
- NewHandle(sizeof(MenuStates) + sizeof(long) * num_menus);
- DialogPtr dlg = GetNewDialog(dialogid, NULL, (WindowPtr)(-1));
- DITLHndl ditl = (DITLHndl)GetResource('DITL', dialogid);
- long editsetting = 0;
-
- if (!mbar || !m || !dlg || !ditl)
- {
- if (mbar) DisposHandle((Handle)mbar);
- if (m ) DisposHandle((Handle)m);
- if (dlg ) DisposDialog(dlg);
- if (ditl) ReleaseResource((Handle)ditl);
- return(NULL);
- }
-
- (*m)->next = MRestoreState;
- MRestoreState = m;
- // Stash a copy of the current menu settings
- for (i=1; i<num_menus; i++)
- (*m)->state[i] = (*((*mbar)->menu[i].handle))->enableFlags;
-
- // Check if this dialog box has any editText fields
- for (i=0; i<=(*ditl)->maxitem; i++)
- {
- short di_type;
- Handle di_handle;
- Rect di_box;
- GetDItem(dlg, i+1, &di_type, &di_handle, &di_box);
- if (di_type == editText) { editsetting = 1+2+8+16+32+64; break; }
- }
-
- // Set the new menu states: File menu, Edit Menu, and the rest
- (*((*mbar)->menu[1].handle))->enableFlags = 0;
- (*((*mbar)->menu[2].handle))->enableFlags = editsetting;
- for (i=3; i<num_menus; i++) (*((*mbar)->menu[i].handle))->enableFlags = 0;
- HiliteMenu(0); // zero means unhilite all hilited menus
- DrawMenuBar(); // redraw the menu bar
- DisposeHandle((Handle)mbar);
- ReleaseResource((Handle)ditl);
- return(dlg);
- }
-
- export void CloseMovableModalDialog(DialogPtr modaldialog)
- {
- int i;
- MenuBar **mbar = (MenuBar **)GetMenuBar();
- short num_menus = mbar && *mbar ? (*mbar)->size / sizeof(MenuBarEntry) : 0;
- MenuStates **m = MRestoreState;
- MRestoreState = (*MRestoreState)->next;
- for (i=1; i<num_menus; i++)
- (*((*mbar)->menu[i].handle))->enableFlags = (*m)->state[i];
- DrawMenuBar();
- DisposeHandle((Handle)mbar);
- DisposHandle((Handle)m);
- DisposDialog(modaldialog);
- }
-
- /*************************************************************************/
-
- export Boolean TrapAvailable(u_long trap)
- {
- TrapType tType = (trap & 0x800 ? ToolTrap : OSTrap);
- if (trap & 0x800) // if it is a ToolBox Trap
- {
- u_long n = 0x400; // number of toolbox traps
- if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
- n = 0x200;
- if ((trap &= 0x7FF) >= n) trap = _Unimplemented;
- }
- return(NGetTrapAddress(trap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
- }
-
- export void fatal(char *format, ...)
- {
- unsigned char buffer[256];
- va_list ptr;
- va_start(ptr,format);
- buffer[0] = vsprintf((char *)buffer+1, format, ptr);
- va_end(ptr);
- DebugStr(buffer);
- }
-
- /*************************************************************************/
-
- export short c_compare_structs(register u_short size, void *a, void *b)
- {
- register BYTE *x = (BYTE *)a;
- register BYTE *y = (BYTE *)b;
- while (size-->0) if(*x-*y) return(*x-*y); else { x++; y++; }
- return(0);
- }
-
- export short c_copy_struct(register u_short size, void *src, void *dst)
- {
- register BYTE *x = (BYTE *)src;
- register BYTE *y = (BYTE *)dst;
- while (size-->0) *y++ = *x++;
- return(0);
- }
-