home *** CD-ROM | disk | FTP | other *** search
- #define DISABLE_LOCAL_CALLTRACE 1 // Set to 1 to disable Call Traces for this file.
- #define DISABLE_LOCAL_DEBUG 0 // Set to 1 to disable all debugging for this file.
- #include "DebugUtils.h"
-
- #include <Dialogs.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "Menu.h"
- #include "QDUtils.h"
- #include "Window.h"
-
-
-
-
-
- enum
- {
- kOKButtonID = 1,
- kCancelButtonID = 2
- };
-
-
-
-
-
- extern MenuManager *gMenuManager;
- extern WindowManager *gWindowManager;
-
-
-
-
-
- BaseWindowManager::BaseWindowManager(void)
- {
- fSuspended = false;
- }
-
-
-
-
-
- BaseWindowObject *BaseWindowManager::GetWindowObject(WindowPtr window)
- {
- BaseWindowObject *obj;
-
-
- obj = fWindowList.GetFirst();
- while(obj && (obj->fWindowID != window))
- obj = obj->next;
-
- return obj;
- }
-
-
-
-
-
- void BaseWindowManager::DoAddWindow(Window *window)
- {
- BaseWindowObject *obj;
-
-
- obj = new BaseWindowObject;
- if (obj != NULL)
- {
- obj->fWindowID = window->fWindow;
- obj->fWindowObject = window;
- fWindowList.Append(obj);
- gMenuManager->DoWindowNotice(window,true);
- }
- }
-
-
-
-
-
- void BaseWindowManager::DoDeleteWindow(Window *window)
- {
- BaseWindowObject *obj;
-
-
- obj = GetWindowObject(window->fWindow);
- if (obj != NULL)
- {
- gMenuManager->DoWindowNotice(window,false);
- fWindowList.Delete(obj);
- delete obj;
- }
- }
-
-
-
-
-
- void BaseWindowManager::DoClick(Point where,UInt32 modifiers,WindowPtr window,SInt32 part)
- {
- Window *win = DoGetWindow(window);
- Window *top;
-
-
- // Don't let any other window have input or be able
- // to layer switch if a modal dialog is the topmost
- top = DoGetFrontWindow();
- if ((win != top) && ((top->fFlags & (kDialog | kModal)) == (kDialog | kModal)))
- return;
-
- if (win != NULL)
- win->DoClick(where,modifiers,part);
- }
-
-
-
-
-
- void BaseWindowManager::DoIdleTime(EventRecord *event,Point mouse,UInt32 modifiers)
- {
- BaseWindowObject *obj;
- WindowPtr window;
- Window *win;
- SInt32 part;
-
-
- if (!fSuspended)
- {
- win = NULL;
- part = FindWindow(mouse,&window);
- if (part == inContent)
- {
- win = DoGetWindow(window);
- if ((win != NULL) && (win->fFlags & kActive))
- win->DoUpdateCursor(mouse,modifiers);
- else
- win = NULL;
- }
-
- if (win == NULL)
- SetCursor(&qd.arrow);
- }
-
- obj = fWindowList.GetFirst();
- while(obj != NULL)
- {
- obj->fWindowObject->DoIdleTime(event,mouse,modifiers);
- obj = obj->next;
- }
- }
-
-
-
-
-
- void BaseWindowManager::DoActivation(WindowPtr window,Boolean isActivating)
- {
- Window *win;
-
-
- win = DoGetWindow(window);
- if (win != NULL)
- {
- win->DoSetActivationState(isActivating);
- gMenuManager->DoWindowActivation(win,isActivating);
- }
- }
-
-
-
-
-
- void BaseWindowManager::DoSuspendResume(EventRecord *event,Boolean isSuspend)
- {
- BaseWindowObject *obj;
-
-
- obj = fWindowList.GetFirst();
- while(obj != NULL)
- {
- obj->fWindowObject->DoSetSuspensionState(event,isSuspend);
- obj = obj->next;
- }
-
- fSuspended = isSuspend;
- }
-
-
-
-
-
- Window *BaseWindowManager::DoGetFrontWindow(void)
- {
- WindowPtr window;
- Window *win;
-
-
- // Floating windows are system windows
- // do this actually is correct
- window = FrontWindow();
- while(window != NULL)
- {
- if (((WindowPeek)window)->visible)
- {
- win = DoGetWindow(window);
- if (win != NULL)
- return win;
- }
-
- window = (WindowPtr)((WindowPeek)window)->nextWindow;
- }
-
- return NULL;
- }
-
-
-
-
-
- Window *BaseWindowManager::DoGetNextWindow(Window *window)
- {
- WindowPtr next;
-
-
- next = (WindowPtr)((WindowPeek)window->fWindow)->nextWindow;
- while(next != NULL)
- {
- window = DoGetWindow(next);
- if ((window != NULL) && ((WindowPeek)window)->visible)
- return window;
-
- next = (WindowPtr)((WindowPeek)next)->nextWindow;
- }
-
- return NULL;
- }
-
-
-
-
-
- Window *BaseWindowManager::DoGetWindow(WindowPtr window)
- {
- BaseWindowObject *obj;
-
-
- obj = GetWindowObject(window);
- return obj ? obj->fWindowObject : NULL;
- }
-
-
-
-
-
- // Do nothing constructor varient
- BaseWindow::BaseWindow(void)
- {
-
- }
-
-
-
-
-
- // Resource based window constructor varient
- BaseWindow::BaseWindow(UInt32 windowID)
- {
- fWindow = ::GetNewCWindow(windowID,NULL,(WindowPtr)-1L);
- if (fWindow != NULL)
- {
- fFlags = 0L;
- gWindowManager->DoAddWindow(this);
- }
- }
-
-
-
-
-
- // Runtime based window constructor varient
- BaseWindow::BaseWindow(Boolean isFloatingWindow,short procID,Boolean goAwayFlag)
- {
- Rect bounds;
-
-
- SetRect(&bounds,32,48,160,144);
- if (isFloatingWindow)
- {
- dprintf(kDConPrefix "What, you think we do everything?\n");
- }
- else
- {
- fWindow = NewCWindow( NULL,
- &bounds,
- "\p",
- false,
- procID,
- (WindowPtr)(-1L),
- goAwayFlag,
- 0L);
-
- if (fWindow != NULL)
- {
- fFlags = 0L;
- gWindowManager->DoAddWindow(this);
- }
- }
- }
-
-
-
-
-
- BaseWindow::~BaseWindow(void)
- {
- if (fWindow != NULL)
- {
- gWindowManager->DoDeleteWindow(this);
- if (fFlags & kFloater)
- {
-
- }
- else
- DisposeWindow(fWindow);
- fWindow = NULL;
- }
- }
-
-
-
-
-
- Boolean BaseWindow::DoGetParam(OSType param,SInt32 *value)
- {
- return HandleGetParam(param,value);
- }
-
-
-
-
-
- Boolean BaseWindow::DoSetParam(OSType param,SInt32 value)
- {
- QDContext context(fWindow);
-
-
- return HandleSetParam(param,value);
- }
-
-
-
-
-
- void BaseWindow::DoDialogEvent(EventRecord *event)
- {
- DialogPtr dialog;
- short item;
-
-
- // Were really not a dialog...but
- // lets do the right thing anyway
- DialogSelect(event,&dialog,&item);
- }
-
-
-
-
-
- void BaseWindow::DoClose(void)
- {
- QDContext context(fWindow);
-
-
- HandleClose();
- }
-
-
-
-
-
- void BaseWindow::DoKey(UInt32 key,UInt32 modifiers)
- {
- QDContext context(fWindow);
-
-
- HandleKey(key,modifiers);
- }
-
-
-
-
-
- void BaseWindow::DoClick(Point where,UInt32 modifiers,SInt32 part)
- {
- QDContext context(fWindow);
-
-
- switch(part)
- {
- case inContent:
- if (!(fFlags & (kActive | kFloater)))
- SelectWindow(fWindow);
- else
- {
- GlobalToLocal(&where);
- HandleClick(where,modifiers);
- }
- break;
-
- case inDrag:
- HandleDrag(where);
- break;
-
- case inGrow:
- HandleGrow(where);
- break;
-
- case inGoAway:
- if (TrackGoAway(fWindow,where))
- HandleClose();
- break;
-
- case inZoomIn:
- if (TrackBox(fWindow,where,part))
- HandleZoomIn();
- break;
-
- case inZoomOut:
- if (TrackBox(fWindow,where,part))
- HandleZoomOut();
- break;
- }
- }
-
-
-
-
-
- void BaseWindow::DoUpdate(void)
- {
- QDContext context(fWindow);
-
-
- BeginUpdate((GrafPtr)fWindow);
- HandleDraw();
- EndUpdate((GrafPtr)fWindow);
- }
-
-
-
-
-
- void BaseWindow::DoUpdateCursor(Point mouse,UInt32 modifiers)
- {
- QDContext context(fWindow);
-
-
- GlobalToLocal(&mouse);
- HandleCursorUpdate(mouse,modifiers);
- }
-
-
-
-
-
- void BaseWindow::DoIdleTime(EventRecord *event,Point mouse,UInt32 modifiers)
- {
- QDContext context(fWindow);
-
-
- GlobalToLocal(&mouse);
- HandleIdleTime(mouse,modifiers);
-
- if ((fFlags & kDialog) && IsDialogEvent(event))
- DoDialogEvent(event);
- }
-
-
-
-
-
- void BaseWindow::DoSetActivationState(Boolean isActive)
- {
- QDContext context(fWindow);
-
-
- if (isActive)
- {
- fFlags |= kActive;
- HandleActivate();
- }
- else
- {
- fFlags &= ~kActive;
- HandleDeactivate();
- }
- }
-
-
-
-
-
- void BaseWindow::DoSetSuspensionState(EventRecord *event,Boolean isSuspended)
- {
- if (fFlags & kFloater)
- {
- if (isSuspended)
- {
- if (((WindowPeek)fWindow)->visible)
- {
- fFlags |= kSuspended;
- ::HideWindow(fWindow);
- }
- }
- else
- {
- if (fFlags & kSuspended)
- {
- fFlags &= ~kSuspended;
- ::ShowWindow(fWindow);
- }
- }
- }
- else
- {
- if (isSuspended)
- {
- if (fFlags & kActive)
- {
- fFlags |= kSuspended;
- DoSetActivationState(false);
- }
- }
- else
- {
- if (fFlags & kSuspended)
- {
- fFlags &= ~kSuspended;
- DoSetActivationState(true);
- }
- }
- }
- }
-
-
-
-
-
- Boolean BaseWindow::HandleGetParam(OSType param,SInt32 *value)
- {
- switch(param)
- {
- case 'type':
- *value = 'base';
- return true;
- break;
-
- default:
- return false;
- break;
- }
- }
-
-
-
-
-
- Boolean BaseWindow::HandleSetParam(OSType param,SInt32 value)
- {
- switch(param)
- {
- default:
- return false;
- break;
- }
- }
-
-
-
-
-
- void BaseWindow::HandleClose(void)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleZoomIn(void)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleZoomOut(void)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleDrag(Point start)
- {
- Point oldPos,newPos;
-
-
- oldPos.h = fWindow->portRect.left;
- oldPos.v = fWindow->portRect.top;
- LocalToGlobal(&oldPos);
-
- DragWindow(fWindow,start,&qd.screenBits.bounds);
-
- newPos.h = fWindow->portRect.left;
- newPos.v = fWindow->portRect.top;
- LocalToGlobal(&newPos);
-
- if ((oldPos.h != newPos.h) || (oldPos.v != newPos.v))
- HandleMove(newPos);
- }
-
-
-
-
-
- void BaseWindow::HandleMove(Point where)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleGrow(Point start)
- {
- Rect limits;
- UInt32 dimensions;
-
-
- limits.top = 64;
- limits.left = 64;
- limits.right = qd.screenBits.bounds.right;
- limits.bottom = qd.screenBits.bounds.bottom;
-
- if (0 != (dimensions = GrowWindow(fWindow,start,&limits)))
- HandleResize((dimensions >> 16),(dimensions & 0xFFFF));
- }
-
-
-
-
-
- void BaseWindow::HandleResize(UInt32 height,UInt32 width)
- {
- SizeWindow(fWindow,width,height,false);
- InvalRect(&fWindow->portRect);
- }
-
-
-
-
-
- void BaseWindow::HandleKey(UInt32 key,UInt32 modifiers)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleClick(Point where,UInt32 modifiers)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleActivate(void)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleDeactivate(void)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleCursorUpdate(Point mouse,UInt32 modifiers)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleIdleTime(Point mouse,UInt32 modifiers)
- {
-
- }
-
-
-
-
-
- void BaseWindow::HandleDraw(void)
- {
-
- }
-
-
-
-
-
- BaseDialog::BaseDialog(UInt32 dialogID,Boolean isModal)
- {
- fDialog = ::GetNewDialog(dialogID,NULL,(WindowPtr)-1L);
- if (fDialog != NULL)
- {
- fFlags = isModal ? (kDialog | kModal) : kDialog;
- fWindow = (WindowPtr)fDialog;
- gWindowManager->DoAddWindow(this);
- SelectWindow(fWindow);
- }
- }
-
-
-
-
-
- BaseDialog::~BaseDialog(void)
- {
- if (fDialog != NULL)
- {
- gWindowManager->DoDeleteWindow(this);
- DisposeDialog(fDialog);
- fDialog = NULL;
- fWindow = NULL;
- }
- }
-
-
-
-
-
- void BaseDialog::DoDialogEvent(EventRecord *event)
- {
- QDContext context(fWindow);
-
-
- // We don't do any special preprocessing of
- // events (yet) so just pass it straight thru
- HandleDialogEvent(event);
- }
-
-
-
-
-
- void BaseDialog::HandleDialogEvent(EventRecord *event)
- {
- DialogPtr dialog;
- short item;
-
-
- if (HandleDialogEventFilter(event))
- {
- if (DialogSelect(event,&dialog,&item))
- HandleDialogItemhit(item);
-
- // We have to check here to see if we've been deleted
- // because HandleDialogItemhit might have been a hit to
- // the OK or Cancel button.
- if (fDialog != NULL)
- {
- // Automagically handle the hiliting
- // and updating of the OK button
- switch(event->what)
- {
- case keyDown:
- case keyUp:
- case mouseDown:
- case mouseUp:
- // Only query the OK button state if something
- // happened that could have possibly changed it
- SetOKState(HandleOKButtonHiliteQuery());
- break;
-
- case updateEvt:
- DrawThickOutline(kOKButtonID);
- break;
- }
- }
- }
- }
-
-
-
-
-
- Boolean BaseDialog::HandleDialogEventFilter(EventRecord *event)
- {
- ControlHandle control;
- short type;
- Rect box;
-
-
- // Filter out command/option/control keys
- if (event->what == keyDown && (event->modifiers & (cmdKey | optionKey | controlKey)))
- return false;
-
- switch(event->what)
- {
- case keyDown:
- case autoKey:
- switch(charCodeMask & event->message)
- {
- case 0x0D: // RETURN
- GetDialogItem(fDialog,kOKButtonID,&type,(Handle*)&control,&box);
- if ((type == 4) && (control[0]->contrlHilite == 0))
- {
- AnimateButtonPress(kOKButtonID);
- HandleDialogItemhit(kOKButtonID);
- return false;
- }
- break;
-
- case 0x1B: // ESC
- GetDialogItem(fDialog,kCancelButtonID,&type,(Handle*)&control,&box);
- if ((type == 4) && (control[0]->contrlHilite == 0))
- {
- AnimateButtonPress(kCancelButtonID);
- HandleDialogItemhit(kCancelButtonID);
- return false;
- }
- break;
- }
- break;
- }
-
- // Continue processing event
- return true;
- }
-
-
-
-
-
- void BaseDialog::HandleDialogItemhit(short item)
- {
- // Default behavior for OK and Cancel
- // button hits are to dismiss the dialog
- switch(item)
- {
- case kOKButtonID:
- case kCancelButtonID:
- delete this;
- break;
- }
- }
-
-
-
-
-
- Boolean BaseDialog::HandleOKButtonHiliteQuery(void)
- {
- // Default behavior is to have a
- // hilited and active OK button
- return true;
- }
-
-
-
-
-
- void BaseDialog::DrawThickOutline(short item)
- {
- QDContext context(fWindow);
- ControlHandle control;
- short type;
- Rect box;
-
-
- // Draw a thick border around an item (default button outline)
- GetDialogItem(fDialog,item,&type,(Handle*)&control,&box);
- if (type == 4)
- {
- InsetRect(&box,-4,-4);
-
- if (control[0]->contrlHilite == 255)
- PenPat(&qd.gray);
-
- PenSize(3,3);
- FrameRoundRect(&box,16,16);
- }
- }
-
-
-
-
-
- void BaseDialog::SetOKState(Boolean isActive)
- {
- ControlHandle control;
- short type;
- Rect box;
-
-
- // (De)Hilite the OK button to show its (in)active state
- GetDialogItem(fDialog,kOKButtonID,&type,(Handle*)&control,&box);
- if (type == 4)
- {
- if (isActive)
- {
- if (control[0]->contrlHilite == 255)
- {
- HiliteControl(control,0);
- DrawThickOutline(kOKButtonID);
- }
- }
- else
- {
- if (control[0]->contrlHilite != 255)
- {
- HiliteControl(control,255);
- DrawThickOutline(kOKButtonID);
- }
- }
- }
- }
-
-
-
-
-
- void BaseDialog::AnimateButtonPress(short item)
- {
- ControlHandle control;
- UInt32 ticks;
- short type;
- Rect box;
-
-
- // Make it appear to the user that a button was pressed
- GetDialogItem(fDialog,item,&type,(Handle*)&control,&box);
- if ((type == 4) && (control[0]->contrlHilite == 0))
- {
- HiliteControl(control,kControlButtonPart);
- Delay(10,&ticks);
- HiliteControl(control,0);
- }
- }
-
-
-
-
-
- void BaseDialog::GetItemText(short item,char *text)
- {
- Handle data;
- short type;
- Rect rect;
- Str255 pstr;
-
-
- // Get the text of a static text item or an
- // edit text item and return it as a c string
- GetDialogItem(fDialog,item,&type,&data,&rect);
- if ((type == statText) || (type == editText))
- GetDialogItemText(data,pstr);
- else
- pstr[0] = '\0';
-
- pstr[1 + pstr[0]] = '\0';
- strcpy(text,(char*)&pstr[1]);
- }
-
-
-
-
-
- int BaseDialog::GetItemTextAsDecimal(short item)
- {
- char text[256];
-
-
- // Get the text of a static text item or an edit
- // text item and return it as a decimal value
- GetItemText(item,text);
- return atoi(text);
- }
-
-
-
-
-
- void BaseDialog::SetItemText(short item,char *text)
- {
- Handle data;
- short type;
- Rect rect;
- Str255 pstr;
-
-
- // Set the text of a static text item
- // or an edit text item from a c string
- GetDialogItem(fDialog,item,&type,&data,&rect);
- if ((type == statText) || (type == editText))
- {
- pstr[0] = strlen(text);
- strcpy((char*)&pstr[1],text);
- SetDialogItemText(data,pstr);
- }
- }
-
-
-
-
-
- void BaseDialog::SetItemTextf(short item,char *format,...)
- {
- va_list args;
- char text[256];
-
-
- // Set the text of a static text item or an edit
- // text item from a printf formatted specification
- va_start(args,format);
- vsprintf(text,format,args);
- va_end(args);
-
- SetItemText(item,text);
- }
-
-
-
-
-
- void BaseDialog::AppendItemText(short item,char *text)
- {
- char cur[256];
-
-
- // Append text to the end of a static text
- // item or an edit text item from a c string
- GetItemText(item,cur);
- strcat(cur,text);
- SetItemText(item,cur);
- }
-
-
-
-
-
- void BaseDialog::AppendItemTextf(short item,char *format,...)
- {
- va_list args;
- char cur[256],text[256];
-
-
- // Append text to the end of a static text item or an
- // edit text item from a printf formatted specification
- va_start(args,format);
- vsprintf(text,format,args);
- va_end(args);
-
- GetItemText(item,cur);
- strcat(cur,text);
- SetItemText(item,cur);
- }
-