home *** CD-ROM | disk | FTP | other *** search
-
- /* File PixExample.h
- PixMap manipulation routines, example program.
- Copyright (c) 1996, 1997 by John Montbriand. All Rights Reserved.
- Permission hereby granted for public use.
- Distribute freely in areas where the laws of copyright apply.
- USE AT YOUR OWN RISK.
- DO NOT DISTRIBUTE MODIFIED COPIES.
- Comments/questions/postcards* to the author at the address:
- John Montbriand
- P.O. Box. 1133
- Saskatoon Saskatchewan Canada
- S7K 3N2
- or by email at:
- tinyjohn@sk.sympatico.ca
- *if you mail a postcard, then I will provide you with technical support
- regarding questions you may have about this file.
- */
-
- #include "CSApp.h"
- #include "PixMap.h"
- #include <Devices.h>
- #include <Scrap.h>
- #include <String.h>
- #include <Strings.h>
- #include <StdLib.h>
-
-
- /* menu constants */
- #define kMenuBar 128
- #define mApple 128
- #define mFile 129
- #define iRight 1
- #define iLeft 2
- #define iVertical 3
- #define iHorizontal 4
- #define iAngle 5
- #define iDraw 6
- #define iQuit 8
- #define mEdit 130
-
-
- /* various resource id numbers */
- #define kNoMemory 128
- #define kWindowID 128
- #define kAngleBox 129
- #define kAboutBox 130
- #define kDefaultPict 128
-
-
- /* some ascii keys */
- #define ETX ((char) 3) /* end of text, ENTER KEY */
- #define CR ((char) 13) /* carriage return, RETURN KEY */
-
-
- /* globals */
- Boolean gRunning = true;
- WindowPtr gWind = NULL;
- PixMapHandle gThePix = NULL, gDispPix = NULL;
- MenuHandle gEditMenu = NULL;
-
-
- /* drawing the pixmap in the window */
-
- static Rect* CenterRectangle(Rect* target, Rect* theBox) {
- Rect dr = *target, wr = *theBox, r;
- short left, top;
- left = ((dr.right + dr.left) - (wr.right-wr.left)) / 2;
- top = ((dr.bottom + dr.top) - (wr.bottom-wr.top)) / 2;
- SetRect(&r, left, top, left+(wr.right-wr.left), top+(wr.bottom-wr.top));
- *theBox = r;
- return theBox;
- }
-
- /* called whenever the window needs to be re-drawn */
- static void MyUpdateProc(WindowPtr window) {
- PixMapHandle pix;
- Rect box;
- if ((pix = gDispPix) == NULL) pix = gThePix;
- box = (**pix).bounds;
- SetPort(gWind);
- BeginUpdate(gWind);
- EraseRect(&gWind->portRect);
- CenterRectangle(&gWind->portRect, &box);
- PlotPixMap(pix, box.left, box.top, srcCopy);
- EndUpdate(gWind);
- }
-
-
- /* routines for managing the globals, here so the Edit menu's
- Undo command can be implemented */
- static PixMapHandle GetCurrentPixMap(void) {
- if (gDispPix != NULL) return gDispPix; else return gThePix;
- }
-
- static void SetNextPixMap(PixMapHandle nextpix) {
- if (gDispPix != NULL) {
- KillPixMap(gThePix);
- gThePix = gDispPix;
- gDispPix = nextpix;
- } else if (gThePix == NULL) {
- gThePix = nextpix;
- } else gDispPix = nextpix;
- SetPort(gWind);
- InvalRect(&gWind->portRect);
- }
-
-
- /* edit commands for the main window */
- static void MyWindowEditFunc(WindowPtr window, short editcmd) {
- switch (editcmd) {
- case iUndo:
- { PixMapHandle temp;
- temp = gDispPix;
- gDispPix = gThePix;
- gThePix = temp;
- SetPort(gWind);
- InvalRect(&gWind->portRect);
- }
- break;
- case iCut:
- MyWindowEditFunc(window, iCopy);
- MyWindowEditFunc(window, iClear);
- break;
- case iCopy:
- { PixMapHandle pix;
- PicHandle pic;
- pix = GetCurrentPixMap();
- if ((pic = PixMapToPICT(pix)) != NULL) {
- ZeroScrap();
- HLock((Handle) pic);
- PutScrap(GetHandleSize((Handle) pic), 'PICT', (Ptr) (*pic));
- } else Alert(kNoMemory, NULL);
- }
- break;
- case iPaste:
- { Handle pic;
- long offset;
- PixMapHandle nextpix;
- pic = NewHandle(0);
- if (GetScrap(pic, 'PICT', &offset) > sizeof(Picture)) {
- if ((nextpix = PICTToPixMap((PicHandle) pic, NULL)) != NULL) {
- SetNextPixMap(nextpix);
- } else Alert(kNoMemory, NULL);
- }
- DisposeHandle(pic);
- }
- break;
- case iClear:
- { PixMapHandle nextpix;
- if ((nextpix = PICTToPixMap(GetPicture(kDefaultPict), NULL)) != NULL) {
- SetNextPixMap(nextpix);
- } else Alert(kNoMemory, NULL);
- }
- break;
- }
- }
-
-
- /* dialog handling commands */
- short gLastItemHit = 0;
- static void MyDialogEventFunc(DialogPtr dialog, EventRecord *ev) {
- if (ev->what == keyDown) {
- char ch;
- ch = (ev->message & charCodeMask);
- if (ch == CR || ch == ETX) {
- ev->what = nullEvent;
- gLastItemHit = ok;
- } else if (strchr("0123456789-.", ch) == NULL) {
- ev->what = nullEvent;
- SysBeep(1);
- }
- }
- }
-
- static void MyDialogHitProc(DialogPtr dialog, short itemhit, short modifiers) {
- gLastItemHit = itemhit;
- }
-
-
- /* moveable modal dialog for retrieving a specific angle */
- static Boolean GetAngle(float *the_angle) {
- DialogPtr the_dialog;
- WindowPtr mmSave;
- short itemt;
- Handle itemh;
- Rect itemb;
- Str255 itemtext;
- gLastItemHit = 0;
- the_dialog = GetNewDialog(kAngleBox, NULL, (WindowPtr) (-1));
- mmSave = SetMMWindow(the_dialog);
- SelectDialogItemText(the_dialog, 3, 0, 255);
- while (gRunning && (gLastItemHit != ok && gLastItemHit != cancel))
- RunCSApp(GetCaretTime());
- if (gLastItemHit == ok) {
- GetDialogItem(the_dialog, 3, &itemt, &itemh, &itemb);
- GetDialogItemText(itemh, itemtext);
- *the_angle = atof(p2cstr(itemtext));
- }
- SetMMWindow(mmSave);
- DisposeDialog(the_dialog);
- return (gLastItemHit == ok);
- }
-
-
-
- /* menu handling routine. */
-
- static void MyMenuProc(short menu, short item, short modifiers, MenuHandle the_menu) {
- switch (menu) {
- case mApple:
- if (item == 1) {
- DialogPtr the_dialog;
- WindowPtr mmSave;
- gLastItemHit = 0;
- the_dialog = GetNewDialog(kAboutBox, NULL, (WindowPtr) (-1));
- mmSave = SetMMWindow(the_dialog);
- while (gRunning && (gLastItemHit != ok)) RunCSApp(GetCaretTime());
- SetMMWindow(mmSave);
- DisposeDialog(the_dialog);
- gLastItemHit = 0;
- } else if (item > 2) {
- Str255 deskAccName;
- GetMenuItemText(the_menu, item, deskAccName);
- OpenDeskAcc(deskAccName);
- }
- break;
-
- case mFile:
- { short x, y;
- float the_angle;
- PixMapHandle nextpix, oldpix;
- switch (item) {
- case iRight:
- oldpix = GetCurrentPixMap();
- if ((nextpix = RotatePixRight(oldpix)) != NULL) {
- SetNextPixMap(nextpix);
- } else Alert(kNoMemory, NULL);
- break;
-
- case iLeft:
- oldpix = GetCurrentPixMap();
- if ((nextpix = RotatePixLeft(oldpix)) != NULL) {
- SetNextPixMap(nextpix);
- } else Alert(kNoMemory, NULL);
- break;
-
- case iVertical:
- oldpix = GetCurrentPixMap();
- if ((nextpix = FlipPixVertical(oldpix)) != NULL) {
- SetNextPixMap(nextpix);
- } else Alert(kNoMemory, NULL);
- break;
-
- case iHorizontal:
- oldpix = GetCurrentPixMap();
- if ((nextpix = FlipPixHorizontal(oldpix)) != NULL) {
- SetNextPixMap(nextpix);
- } else Alert(kNoMemory, NULL);
- break;
-
- case iAngle:
- if (GetAngle(&the_angle)) {
- oldpix = GetCurrentPixMap();
- x = ((**oldpix).bounds.right + (**oldpix).bounds.left) / 2;
- y = ((**oldpix).bounds.bottom + (**oldpix).bounds.top) / 2;
- if ((nextpix = RotatePixMap(oldpix, x, y, the_angle)) != NULL) {
- SetNextPixMap(nextpix);
- } else Alert(kNoMemory, NULL);
- }
- break;
-
- case iDraw:
- { Rect r;
- long i, n;
- PixMapPort* pxmp;
- CTabHandle clut;
- RGBColor the_color;
- oldpix = GetCurrentPixMap();
- if ((nextpix = DuplicatePixMap(oldpix)) != NULL) {
- r = (**nextpix).bounds;
- clut = (**nextpix).pmTable;
- if (r.right > r.bottom) n = r.bottom; else n = r.right;
- WithPixMap(nextpix, pxmp) {
- for (i=0; i<n/10; i++) {
- the_color = (**clut).ctTable[i].rgb;
- RGBForeColor(&the_color);
- FrameRect(&r);
- InsetRect(&r, 10, 10);
- }
- }
- SetNextPixMap(nextpix);
- } else Alert(kNoMemory, NULL);
- }
- break;
- case iQuit:
- gRunning = false;
- break;
- }
- }
- break;
- case mEdit:
- CallEditCommand(item);
- break;
- }
- HiliteMenu(0);
- }
-
- static void SetMenuItemEnable(MenuHandle theMenu, short theItem, Boolean enabled) {
- if (enabled) EnableItem(theMenu, theItem); else DisableItem(theMenu, theItem);
- }
-
-
- /* menu reset routine, called before menu selections so we can enable
- or disable items. */
- static void MyMenuReset(void) {
- long offset, count;
- if (gWind == FrontWindow()) {
- SetMenuItemEnable(gEditMenu, iPaste,
- (GetScrap(NULL, 'PICT', &offset) > sizeof(Picture)));
- SetMenuItemEnable(gEditMenu, iUndo, (gDispPix != NULL));
- } else {
- count = GetScrap(NULL, 'TEXT', &offset);
- SetMenuItemEnable(gEditMenu, iPaste, (count > 0 && count < 5));
- SetMenuItemEnable(gEditMenu, iUndo, false);
- }
- }
-
-
- /* moveable modal hook. we disable the file menu whenever a moveable
- modal dialog is up */
- static void MyMMHook(Boolean mmactive) {
- SetMenuItemEnable(GetMHandle(mFile), 0, !mmactive);
- DrawMenuBar();
- }
-
-
- /* main program */
- int main(void) {
- if (OpenCSApp(2, 4) != noErr) return 0;
-
- SetMenuFunctions(kMenuBar, MyMenuReset, MyMenuProc);
- AppendResMenu(GetMHandle(mApple), 'DRVR');
- gEditMenu = GetMHandle(mEdit);
- SetWindowRedraw(NULL, MyUpdateProc);
- SetWindowInteraction(NULL, MyWindowEditFunc);
- SetDialogFunctions(MyDialogEventFunc, MyDialogHitProc, NULL);
- SetMMHookProc(MyMMHook);
- gWind = GetNewCWindow(kWindowID, NULL, (WindowPtr) (-1));
- /* clear the window */
- MyWindowEditFunc(gWind, iClear);
-
- while (gRunning) RunCSApp(GetCaretTime());
-
- bail:
- CloseCSApp();
- return 0;
- }
-