home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <stdio.h>
-
- #include "tk.h"
- #include "tkprivate.h"
- #include "tkmacglobals.h"
-
-
- #if defined(__MWERKS__) || defined(__SC__)
- #include "console.h"
- #endif
-
- #if defined(__MWERKS__)
- #include "SIOUX.h"
- #endif
-
- void (*ExposeFunc)(int, int) = NULL;
- void (*ReshapeFunc)(int, int) = NULL;
- void (*DisplayFunc)(void) = NULL;
- GLenum (*KeyDownFunc)(int, GLenum) = NULL;
- GLenum (*MouseDownFunc)(int, int, GLenum) = NULL;
- GLenum (*MouseUpFunc)(int, int, GLenum) = NULL;
- GLenum (*MouseMoveFunc)(int, int, GLenum) = NULL;
- void (*IdleFunc)(void) = NULL;
-
- Boolean gDone = false;
- Boolean gDrawFlag = true;
-
- void DoMenuCommand(long menuAndItem);
- void DoDrag(WindowPtr myWindow,Point mouseloc);
- void DoContentClick(WindowPtr myWindow, EventRecord *event);
- Boolean IsDAccWindow(WindowPtr myWindow);
- void DoGoAwayBox(WindowPtr myWindow, Point mouseloc);
- void DoCloseWindow(WindowPtr myWindow);
- void DoKeyDown(EventRecord *event);
- void DoDiskEvent(EventRecord *event);
- void DoOSEvent(EventRecord *event);
- void DoUpdate(WindowPtr myWindow);
- void DoZoom(WindowPtr myWindow, Point mouseloc, int part);
- void DoActivate(WindowPtr myWindow, int myModifiers);
- void DoAboutBox(void);
- void DoNextEvent(void);
- void DoMenuCommand(long menuAndItem);
- void DoReshape(int width,int height);
- void DoCallIdle(void);
- void DoMouseDown(EventRecord *event);
- void DoMouseUp(EventRecord *event);
- void DoMenuAdjust(void);
- void DoKeyUp(EventRecord *event);
-
- static void DoGrow(WindowPtr myWindow, Point mouseloc)
- {
- Rect growBounds;
- long newSize;
-
- growBounds = (**GetGrayRgn()).rgnBBox;
- growBounds.right -= growBounds.left;
- growBounds.bottom -= growBounds.top;
- growBounds.left = 20;
- growBounds.top = 20;
-
- newSize = GrowWindow(myWindow,mouseloc,&growBounds);
-
- if(newSize)
- {
- SizeWindow(myWindow, newSize & 0x0000FFFF, newSize >> 16, false);
- SetPort(myWindow);
- DoReshape(LoWord(newSize),HiWord(newSize));
- }
- }
-
- static void DoZoom(WindowPtr myWindow, Point mouseloc, int part)
- {
- short w, h;
-
- if(TrackBox(myWindow, mouseloc, part))
- {
- ZoomWindow(myWindow, part, false);
- w = abs(myWindow->portRect.right - myWindow->portRect.left);
- h = abs(myWindow->portRect.top - myWindow->portRect.bottom);
-
- DoReshape(w, h);
- }
- }
-
- static void DoReshape(int width, int height)
- {
- tkWindow.width = width;
- tkWindow.height = height;
-
- if(!tkWindow.context) return;
-
- aglUpdateContext(aglGetCurrentContext());
-
- if(ReshapeFunc)
- {
- (*ReshapeFunc)(tkWindow.width,tkWindow.height);
- gDrawFlag = true;
- }
- }
-
- static void DoRedraw(void)
- {
- static GLboolean update_grow = GL_TRUE;
-
- if(!tkWindow.context) return;
-
- if(DisplayFunc && gDrawFlag)
- {
- gDrawFlag = false;
- (*DisplayFunc)();
- }
- }
-
- void DoCallIdle(void)
- {
- if(!tkWindow.context) return;
-
- if(IdleFunc)
- {
- (*IdleFunc)();
- gDrawFlag = true;
- }
- }
-
- void DoMouseDown(EventRecord *event)
- {
- int myPart;
- WindowPtr myWindow;
-
- myPart = FindWindow(event->where, &myWindow);
-
- switch(myPart)
- {
- case inMenuBar:
- DrawMenuBar();
- DoMenuCommand(MenuSelect(event->where));
- break;
- case inSysWindow:
- SystemClick(event, myWindow);
- break;
- case inDrag:
- DoDrag(myWindow, event->where);
- break;
- case inGoAway:
- DoGoAwayBox(myWindow, event->where);
- break;
- case inGrow:
- DoGrow(myWindow, event->where);
- break;
- case inZoomIn:
- case inZoomOut:
- DoZoom(myWindow, event->where, myPart);
- break;
- case inContent:
- if (myWindow != FrontWindow())
- {
- SelectWindow(myWindow);
- }
- else
- {
- DoContentClick(myWindow,event);
- }
- break;
- }
- }
-
- void DoMouseUp(EventRecord *event)
- {
- if(MouseUpFunc)
- {
- if(event->modifiers & shiftKey)
- {
- gDrawFlag = (*MouseUpFunc)(event->where.h, event->where.v, TK_RIGHTBUTTON);
- }
- else if(event->modifiers & optionKey)
- {
- gDrawFlag = (*MouseUpFunc)(event->where.h, event->where.v, TK_MIDDLEBUTTON);
- }
- else
- {
- gDrawFlag = (*MouseUpFunc)(event->where.h, event->where.v, TK_LEFTBUTTON);
- }
- }
- }
-
- void DoDrag(WindowPtr myWindow, Point mouseloc)
- {
- Rect dragBounds;
-
- dragBounds = (**GetGrayRgn()).rgnBBox;
- DragWindow(myWindow,mouseloc,&dragBounds);
-
- aglUpdateContext(aglGetCurrentContext());
- }
-
- void DoContentClick(WindowPtr myWindow, EventRecord *event)
- {
- if(tkWindow.fullscreen) return;
-
- SetPort(myWindow);
- GlobalToLocal(&event->where);
-
- if(MouseDownFunc)
- {
- if(event->modifiers & shiftKey)
- {
- gDrawFlag = (*MouseDownFunc)(event->where.h, event->where.v, TK_RIGHTBUTTON);
- }
- else if(event->modifiers & optionKey)
- {
- gDrawFlag = (*MouseDownFunc)(event->where.h, event->where.v, TK_MIDDLEBUTTON);
- }
- else
- {
- gDrawFlag = (*MouseDownFunc)(event->where.h, event->where.v, TK_LEFTBUTTON);
- }
- }
- }
-
- Boolean IsDAccWindow(WindowPtr myWindow)
- {
- if(myWindow == nil)
- {
- return false;
- }
- else
- {
- return(((WindowPeek)myWindow)->windowKind < 0);
- }
- }
-
- void DoGoAwayBox(WindowPtr myWindow, Point mouseloc)
- {
- if(TrackGoAway(myWindow,mouseloc))
- {
- DoCloseWindow(myWindow);
- }
- }
-
- void DoCloseWindow(WindowPtr myWindow)
- {
- if(myWindow)
- {
- if(IsDAccWindow(myWindow))
- {
- CloseDeskAcc(((WindowPeek)myWindow)->windowKind);
- }
- else
- {
- tkQuit();
- }
- }
- }
-
- void DoMenuAdjust(void)
- {
- }
-
- void DoKeyDown(EventRecord *event)
- {
- char myCharCode;
- char myKeyCode;
- int myKey;
- GLenum mask;
-
- myCharCode = (char)BitAnd(event->message,charCodeMask);
-
- if(BitAnd(event->modifiers,cmdKey) != 0)
- {
- DoMenuAdjust();
- DoMenuCommand(MenuKey(myCharCode));
- }
- else
- {
- myKeyCode = event->message & keyCodeMask;
-
- switch(myCharCode)
- {
- case '0': myKey = TK_0; break;
- case '1': myKey = TK_1; break;
- case '2': myKey = TK_2; break;
- case '3': myKey = TK_3; break;
- case '4': myKey = TK_4; break;
- case '5': myKey = TK_5; break;
- case '6': myKey = TK_6; break;
- case '7': myKey = TK_7; break;
- case '8': myKey = TK_8; break;
- case '9': myKey = TK_9; break;
- case 'A': myKey = TK_A; break;
- case 'B': myKey = TK_B; break;
- case 'C': myKey = TK_C; break;
- case 'D': myKey = TK_D; break;
- case 'E': myKey = TK_E; break;
- case 'F': myKey = TK_F; break;
- case 'G': myKey = TK_G; break;
- case 'H': myKey = TK_H; break;
- case 'I': myKey = TK_I; break;
- case 'J': myKey = TK_J; break;
- case 'K': myKey = TK_K; break;
- case 'L': myKey = TK_L; break;
- case 'M': myKey = TK_M; break;
- case 'N': myKey = TK_N; break;
- case 'O': myKey = TK_O; break;
- case 'P': myKey = TK_P; break;
- case 'Q': myKey = TK_Q; break;
- case 'R': myKey = TK_R; break;
- case 'S': myKey = TK_S; break;
- case 'T': myKey = TK_T; break;
- case 'U': myKey = TK_U; break;
- case 'V': myKey = TK_V; break;
- case 'W': myKey = TK_W; break;
- case 'X': myKey = TK_X; break;
- case 'Y': myKey = TK_Y; break;
- case 'Z': myKey = TK_Z; break;
- case 'a': myKey = TK_a; break;
- case 'b': myKey = TK_b; break;
- case 'c': myKey = TK_c; break;
- case 'd': myKey = TK_d; break;
- case 'e': myKey = TK_e; break;
- case 'f': myKey = TK_f; break;
- case 'g': myKey = TK_g; break;
- case 'h': myKey = TK_h; break;
- case 'i': myKey = TK_i; break;
- case 'j': myKey = TK_j; break;
- case 'k': myKey = TK_k; break;
- case 'l': myKey = TK_l; break;
- case 'm': myKey = TK_m; break;
- case 'n': myKey = TK_n; break;
- case 'o': myKey = TK_o; break;
- case 'p': myKey = TK_p; break;
- case 'q': myKey = TK_q; break;
- case 'r': myKey = TK_r; break;
- case 's': myKey = TK_s; break;
- case 't': myKey = TK_t; break;
- case 'u': myKey = TK_u; break;
- case 'v': myKey = TK_v; break;
- case 'w': myKey = TK_w; break;
- case 'x': myKey = TK_x; break;
- case 'y': myKey = TK_y; break;
- case 'z': myKey = TK_z; break;
- case ' ': myKey = TK_SPACE; break;
- case 0x0D: myKey = TK_RETURN; break;
- case 0x1B: myKey = TK_ESCAPE; break;
- case 0x1C: myKey = TK_LEFT; break;
- case 0x1D: myKey = TK_RIGHT; break;
- case 0x1E: myKey = TK_UP; break;
- case 0x1F: myKey = TK_DOWN; break;
- default: myKey = GL_FALSE; break;
- }
-
- if (myKey && KeyDownFunc)
- {
- mask = 0;
-
- if (event->modifiers & controlKey)
- {
- mask |= TK_CONTROL;
- }
-
- if (event->modifiers & shiftKey)
- {
- mask |= TK_SHIFT;
- }
-
- gDrawFlag = true;
- (*KeyDownFunc)(myKey, mask);
-
- return;
- }
- }
- }
-
- static void DoDiskEvent(EventRecord *event)
- {
-
- }
-
- static void DoOSEvent(EventRecord *event)
- {
-
- }
-
- static void DoUpdate(WindowPtr myWindow)
- {
- GrafPtr origPort;
-
- GetPort(&origPort);
- SetPort(myWindow);
-
- BeginUpdate(myWindow);
- EndUpdate(myWindow);
-
- aglUpdateContext(aglGetCurrentContext());
-
- SetPort(origPort);
-
- gDrawFlag = true;
- }
-
- static void DoActivate(WindowPtr myWindow,int myModifiers)
- {
-
- }
-
- static void DoAboutBox(void)
- {
- DialogPtr myDialog;
- short itemHit;
-
- myDialog = GetNewDialog(kAboutDialog, nil, (WindowPtr) -1);
- ModalDialog(nil, &itemHit);
- DisposeDialog(myDialog);
- }
-
- static void DoMenuCommand(long menuAndItem)
- {
- int myMenuNum;
- int myItemNum;
- int myResult;
- Str255 myDAName;
- WindowPtr myWindow;
-
- myMenuNum = HiWord(menuAndItem);
- myItemNum = LoWord(menuAndItem);
-
- GetPort(&myWindow);
-
- switch (myMenuNum)
- {
- case mApple:
- switch (myItemNum)
- {
- case iAbout:
- DoAboutBox();
- break;
- default:
- GetMenuItemText(GetMenuHandle(mApple), myItemNum, myDAName);
- myResult = OpenDeskAcc(myDAName);
- break;
- }
- break;
- case mFile:
- switch (myItemNum)
- {
- case iQuit:
- tkQuit();
- break;
- }
- break;
- }
-
- HiliteMenu(0);
- }
-
- static Boolean DoConsoleEvent(EventRecord *event)
- {
- GLboolean flag = GL_FALSE;
-
- #if defined(__MWERKS__)
- flag = SIOUXHandleOneEvent(event);
- #endif
-
- return flag;
- }
-
- static void DoNextEvent(void)
- {
- Boolean gotEvent;
- EventRecord event;
- static double t0 = -10.0, t1;
-
- gotEvent = WaitNextEvent(everyEvent, &event, 0, nil);
-
- if(gotEvent && !DoConsoleEvent(&event))
- {
- switch(event.what)
- {
- case mouseDown:
- DoMouseDown(&event);
- break;
- case mouseUp:
- DoMouseUp(&event);
- break;
- case keyDown:
- DoKeyDown(&event);
- break;
- case keyUp:
- break;
- case autoKey:
- DoKeyDown(&event);
- break;
- case updateEvt:
- DoUpdate((WindowPtr) event.message);
- break;
- case diskEvt:
- DoDiskEvent(&event);
- break;
- case activateEvt:
- DoActivate((WindowPtr) event.message, event.modifiers);
- break;
- case osEvt:
- DoOSEvent(&event);
- break;
- default:
- DoCallIdle();
- break;
- }
- }
- else
- {
- DoCallIdle();
- }
- }
-
- void tkExec(void)
- {
- gDrawFlag = true;
-
- if(tkWindow.drawable)
- {
- BeginUpdate((GrafPort *) tkWindow.drawable);
- EndUpdate((GrafPort *) tkWindow.drawable);
- }
-
- DoReshape(tkWindow.width,tkWindow.height);
-
- while(!gDone)
- {
- DoNextEvent();
- DoRedraw();
- }
-
- tkCloseWindow();
- }
-
-
- void tkExposeFunc(void (*Func)(int, int))
- {
- ExposeFunc = Func;
- }
-
-
- void tkReshapeFunc(void (*Func)(int, int))
- {
- ReshapeFunc = Func;
- }
-
-
- void tkDisplayFunc(void (*Func)(void))
- {
- DisplayFunc = Func;
- }
-
-
- void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
- {
- KeyDownFunc = Func;
- }
-
- void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
- {
- MouseDownFunc = Func;
- }
-
-
- void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
- {
- MouseUpFunc = Func;
- }
-
-
- void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
- {
- MouseMoveFunc = Func;
- }
-
-
- void tkIdleFunc(void (*Func)(void))
- {
- IdleFunc = Func;
- }
-