home *** CD-ROM | disk | FTP | other *** search
- /*
- * wind.c - WindDemo: a demo of a custom Window Definition.
- *
- * How to build this program:
- * 1) In the "iconproj" project, create the CODE resource, "icon.wdef".
- * 2) Run RMaker on the file "wind.r".
- * 4) In the "windproj" project, Build the Application, WindDemo.
- *
- *
- * The interesting parts of this program are:
- *
- * 1) In doevent(), when growing a window (just before calling SizeWindow()),
- * it forces small windows to be the size of an icon;
- *
- * 2) In drawwindow(), any small window gets an icon drawn it in
- * instead of its normal stuff.
- *
- * The rest of this program is just boilerplate
- * (and not very pretty code anyway!)
- */
-
- #include <MacTypes.h>
- #include <Quickdraw.h>
- #include <MemoryMgr.h>
- #include <WindowMgr.h>
- #include <DialogMgr.h>
- #include <MenuMgr.h>
- #include <EventMgr.h>
- #include <DeskMgr.h>
- #include <ControlMgr.h>
- #include <TextEdit.h>
- #include <PackageMgr.h>
- #include <ToolboxUtil.h>
- #include <pascal.h>
-
- /*
- * SMALLWIDTH,
- * SMALLHEIGHT - largest dimensions of a collapsed-state window.
- * Any window larger than this (in both dimensions) is a standard window.
- *
- * These numbers come from the iconwind WDEF.
- */
-
- #define SMALLWIDTH 63
- #define SMALLHEIGHT 15
-
- /*
- * ICONSIZE - the width/height of an icon.
- * This program forces any collapsed window to the size of an icon.
- */
-
- #define ICONSIZE 32
-
- #define APPLEMENU 1 /* Menu ID for Apple menu */
- #define FILEMENU 2 /* ...File menu */
- #define LASTMENU 2 /* Number of menus */
-
- #define DLG_ABOUT 256 /* ResID of the About... dialog */
-
- /*
- * *FILE - items in the FILE menu
- */
-
- #define NEWFILE 1 /* create a new window */
- #define QUITFILE 2 /* quit the program */
-
- MenuHandle mymenus[LASTMENU + 1]; /* menus[1..LASTMENU] */
-
- #define maxStackSize 8192 /* max size of our stack */
-
- /*
- * screenport - used to keep the current port from ever dangling.
- * This is a port for the whole screen.
- */
-
- GrafPtr screenport;
-
- #define NUM_WINDOWS 3 /* number of window's in the program */
- WindowPtr windoc[NUM_WINDOWS]; /* the program's windows */
-
- #define WINDOWID 260 /* Resource ID of my first window */
- #define ICONID 260 /* Resource ID of the icon to plot */
-
- /*
- * main() - run the program.
- */
-
- main()
- {
- int widx; /* index into wrecord[] */
- long *stackbaseptr; /* points to current stack base */
- int i;
-
- /*
- * Initialize our memory,
- * then init the Toolbox managers (order is important).
- */
-
- stackbaseptr = (long *) 0x908;
- SetApplLimit((Ptr) (*stackbaseptr - maxStackSize));
- MaxApplZone();
- MoreMasters();
- MoreMasters();
-
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs((ProcPtr) 0);
- DILoad();
- SetEventMask(everyEvent - keyUpMask);
- InitCursor();
- GetWMgrPort(&screenport);
- SetPort(screenport);
-
- /*
- * Setup our menus.
- */
-
- for (i = 1; i <= LASTMENU; i++) {
- mymenus[i] = GetMenu(i);
- InsertMenu(mymenus[i], 0);
- }
- DrawMenuBar();
-
- /*
- * Create our first window.
- */
-
- makewindow();
-
- while (doevent())
- ;
- InitCursor();
- SetEventMask(everyEvent - keyUpMask);
- }
-
- /*
- * makewindow() - create a new window.
- */
-
- makewindow()
- {
- static WindowRecord wrecord[NUM_WINDOWS]; /* Storage for window records. */
- int widx;
-
- for (widx = 0; widx < NUM_WINDOWS && windoc[widx]; ++widx)
- ;
- if (widx < NUM_WINDOWS) {
- windoc[widx] = GetNewWindow(WINDOWID + widx, &wrecord[widx], (long) -1);
- }
- }
-
- /*
- * unmakewindow() - destroy (close) a window.
- */
-
- unmakewindow(windp)
- WindowPtr windp; /* the window to destroy */
- {
- int widx;
-
- for (widx = 0; widx < NUM_WINDOWS && windoc[widx] != windp; ++widx)
- ;
- if (widx < NUM_WINDOWS) {
- CloseWindow(windp);
- windoc[widx] = (WindowPtr) 0;
- }
- }
-
- /*
- * doevent() - one loop through the main event handler
- */
-
- int /* "do another event" */
- doevent()
- {
- GrafPtr saveport;
- EventRecord myevent; /* the event to handle */
- WindowPtr whichwindow; /* Points to window of MouseDown */
- char whichchar; /* the character typed */
- long menucommand; /* a menu command to do (menu + item) */
- int windowcode; /* What mouse was in when event posted */
- Rect dragrect; /* Bounds of window dragging */
- Rect limits; /* limits to growth */
- long newdims; /* new width & height of the window */
- int newwidth, newheight; /* newdims split into width and height */
- int userdone; /* True when user wants to exit program */
-
- userdone = FALSE;
- SystemTask(); /* give the system a moment of time */
-
- if (!GetNextEvent(everyEvent, &myevent)) {
- /* We're idle. */
- } else if ((myevent.modifiers & cmdKey) &&
- (myevent.what == keyDown || myevent.what == autoKey)) {
-
- /*
- * It's a command-key keystroke.
- * (command keystrokes are handled here since dialogs can't)
- */
-
- whichchar = (char) (myevent.message & charCodeMask);
- menucommand = MenuKey(whichchar);
- userdone = docommand(menucommand);
- } else if (IsDialogEvent(&myevent)) {
- /* here's where we'd handle modeless dialog events */
- } else {
- switch (myevent.what) {
- case updateEvt: /* update the appropriate window */
- BeginUpdate((WindowPtr)(myevent.message));
- GetPort(&saveport);
- SetPort((WindowPtr)(myevent.message));
-
- drawwindow((WindowPtr)(myevent.message));
-
- SetPort(saveport);
- EndUpdate((WindowPtr)(myevent.message));
- break;
- case activateEvt: /* activate or deactivate window */
- if (myevent.modifiers & 1) {
- SetPort((WindowPtr) myevent.message);
- InvalRect(&thePort->portRect);
- } else {
- InvalRect(&thePort->portRect);
- SetPort(screenport);
- }
- break;
- case mouseDown: /* a mouse click someplace */
- windowcode = FindWindow(myevent.where, &whichwindow);
-
- switch (windowcode) {
- case inSysWindow: /* a DA's window */
- SystemClick(&myevent, whichwindow);
- break;
- case inMenuBar: /* a menu item */
- menucommand = MenuSelect(myevent.where);
- userdone = docommand(menucommand);
- break;
- case inDrag: /* Title Bar */
-
- /*
- * Drag the window around.
- *
- * If the clicked window wasn't selected
- * (and this wasn't a command-click), select it.
- */
-
- if (whichwindow != FrontWindow() &&
- (myevent.modifiers & cmdKey) == 0) {
- SelectWindow(whichwindow);
- }
-
- dragrect.left = 4;
- dragrect.top = 24;
- dragrect.right = screenBits.bounds.right - 4;
- dragrect.bottom = screenBits.bounds.bottom - 4;
-
- DragWindow(whichwindow, myevent.where, &dragrect);
- break;
- case inGoAway: /* Close box */
- if (TrackGoAway(whichwindow, myevent.where)) {
- unmakewindow(whichwindow);
- }
- break;
- case inZoomIn:
- case inZoomOut: /* Zoom box */
- if (TrackBox(whichwindow, myevent.where, windowcode)) {
- EraseRect(&whichwindow->portRect);
- ZoomWindow(whichwindow, windowcode, FALSE);
- }
- break;
- case inGrow: /* in the grow region */
- limits.top = 15;
- limits.left = 15;
- limits.bottom =
- screenport->portRect.bottom - screenport->portRect.top;
- limits.right =
- screenport->portRect.right - screenport->portRect.left;
-
- limits.right++; /* (to compensate for a toolbox bug) */
- limits.bottom++; /* (ditto) */
-
- newdims = GrowWindow(whichwindow, myevent.where, &limits);
- if (newdims != 0L) {
-
- /**********
- * Here's one of the two interesting parts of this program:
- *
- * If the new window size is small, it is forced to be
- * the size of an icon.
- **********/
-
- newwidth = LoWord(newdims);
- newheight = HiWord(newdims);
-
- if (newwidth <= SMALLWIDTH || newheight <= SMALLHEIGHT) {
- newwidth = ICONSIZE;
- newheight = ICONSIZE;
- }
- SizeWindow(whichwindow, newwidth, newheight, -1);
- InvalRect(&thePort->portRect);
- }
- break;
- case inContent: /* Somewhere in the window */
- if (whichwindow != FrontWindow()) {
- /* the window isn't active - just activate it */
- SelectWindow(whichwindow);
- } else {
-
- /*
- * The window is active,
- * interpret the mouse-down in this window's context.
- */
-
- GlobalToLocal(&myevent.where);
- }
- }
- break;
- }
- }
- return(!userdone);
- }
-
- /*
- * docommand() - handle a command given through a menu selection.
- */
-
- int /* "the command was 'quit' */
- docommand(mresult)
- long mresult; /* the command to execute (a menu item) */
- {
- int themenu; /* menu the mouse chose */
- int theitem; /* item in that menu */
- int returns; /* value to be returned */
- short itemhit;
- DialogPtr reportptr;
-
- returns = FALSE;
- themenu = HiWord(mresult);
- theitem = LoWord(mresult);
-
- switch (themenu) {
- case APPLEMENU: /* Tell about the program */
- reportptr = GetNewDialog(DLG_ABOUT, (char *) 0, (long) -1);
- ModalDialog((ProcPtr) 0, &itemhit);
- DisposDialog(reportptr);
- break;
-
- case FILEMENU:
- switch (theitem) {
- case NEWFILE: /* create a new window (if we can) */
- makewindow();
- break;
- case QUITFILE: /* quit the program */
- returns = TRUE;
- break;
- }
- break;
- }
- HiliteMenu(0);
- return(returns);
- }
-
- /*
- * drawwindow() - draw the given window's contents into the current port.
- */
-
- drawwindow(windp)
- WindowPtr windp;
- {
- int widx;
- Handle iconh; /* an icon to draw */
- Rect tmprect; /* a temporary rectangle */
- RgnHandle oldclip; /* original clip region to restore */
-
- for (widx = 0; widx < NUM_WINDOWS && windp != windoc[widx]; ++widx)
- ;
- if (widx >= NUM_WINDOWS) return;
-
- EraseRgn(thePort->visRgn);
-
-
- /**********
- * Here's the other interesting part of this program:
- *
- * If the window is small, an icon is drawn in it
- * (rather than its normal stuff).
- **********/
-
- if (thePort->portRect.right - thePort->portRect.left <= SMALLWIDTH ||
- thePort->portRect.bottom - thePort->portRect.top <= SMALLHEIGHT) {
-
- /*
- * It's a collapsed window -- draw our icon in it.
- */
-
- tmprect.left = 0;
- tmprect.top = 0;
- tmprect.right = tmprect.left + 32;
- tmprect.bottom = tmprect.top + 32;
- iconh = GetIcon(ICONID);
- PlotIcon(&tmprect, iconh);
-
- } else {
-
- /*
- * It's a standard window -- draw something interesting
- * excluding the scrollbar area.
- */
-
- tmprect = thePort->portRect;
- tmprect.right -= 15;
- tmprect.bottom -= 15;
- oldclip = NewRgn();
- if (oldclip) {
- GetClip(oldclip);
- RectRgn(thePort->clipRgn, &tmprect);
- SectRgn(oldclip, thePort->clipRgn, thePort->clipRgn);
- }
- TextBox("The WDEF acts like a standard window until it shrinks \
- past a certain point.", 75L, &tmprect, teJustLeft);
- if (oldclip) {
- SetClip(oldclip);
- DisposeRgn(oldclip);
- }
- DrawGrowIcon(windp);
- }
- }
-