home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-01 | 15.7 KB | 763 lines | [TEXT/CWIE] |
- // HexThing
- // version 1.0.1
- // by Ken Long <kenlong@netcom.com>
- // updated for CW7 and THINK 7 on 951201
- #include <stdio.h>
- #include <Math.h>
-
- #define words "\pAnother itty bitty bytes™ public domain source code demo, by kenlong @netcom. com."
- enum {
- appleID = 1,
- fileID,
- editID
- };
-
- enum {
- quitItem = 1
- };
-
- short thing;
-
- MenuHandle appleMenu, fileMenu, editMenu;
- EventRecord mainEvent;
-
- Boolean weBeHued;
-
- WindowPtr hexWindow;
-
- Rect hexOneRect, hexTwoRect, hexTriRect, hexQuaRect,
- hexOneDataRect, hexTwoDataRect, hexTriDataRect, hexQuaDataRect,
- controlWindowRect, decRect, hexRect, bitRect;
- Rect bitOneDataRect, bitTwoDataRect, bitTriDataRect, bitQuaDataRect,
- aboutRect, ctlBoxRect;
-
- ControlHandle hexOne, hexTwo, hexTri, hexQua;
-
- void SetForeColor (short red, short green, short blue);
- void SetBackColor (short red, short green, short blue);
- void ShowScrlValue (ControlHandle theControl);
- void OneTimeStrings ();
- void ControlsUpdate ();
- pascal void Track (ControlHandle Control, short partCode);
- void MakeHex (short thing);
- void MakeBits (short thing);
- void ControlMouse (Point thePoint);
- void EmbossString (Str255 string, short h, short v);
- void EmbossFrame (Rect someRect);
- void ShadowBox (Rect someRect);
- void InitHexThing (void);
- void DrawStuff ();
- void SetUpRects (void);
- void WindowClicks (WindowPtr theWindow, Point where);
- void Updates (void);
- void MenuPicks (long mSelect);
- void MouseClicks (EventRecord *event);
- void Events (void);
- void SetUpMenus (void);
- void SetUpMac (void);
- void main (void);
-
- pascal void TETextBox(const void *text, long length, const Rect *box, short just)
- =(0xA9CE);
-
- //• --------------------------------------------------------------- •//
- DoAbout ()
- {
- long ticks;
-
- EmbossFrame (aboutRect);
- InsetRect (&aboutRect, 1, 1);
- TETextBox(&words [1], words[0], &aboutRect, 0);
- Delay (240L, &ticks);
- SetForeColor(0xeeee, 0xeeee, 0xeeee);
- InsetRect (&aboutRect, -2, -2);
- PaintRect (&aboutRect);
- SetForeColor(0, 0, 0);
- }
-
- //• --------------------------------------------------------------- •//
- void SetForeColor(short red, short green, short blue)
- {
- RGBColor hue;
-
- hue.red = red;
- hue.green = green;
- hue.blue = blue;
-
- RGBForeColor (&hue);
- }
-
- //• --------------------------------------------------------------- •//
- void SetBackColor(short red, short green, short blue)
- {
- RGBColor hue;
-
- hue.red = red;
- hue.green = green;
- hue.blue = blue;
-
- RGBBackColor (&hue);
- }
-
- //• --------------------------------------------------------------- •//
- void ShowScrlValue (ControlHandle theControl)
- {
- Rect aRect;
- Str255 aStr;
- short anInt;
-
- TextFont (helvetica);
- TextSize (9);
-
- aRect = theControl[0]->contrlRect;
- aRect.top = aRect.bottom + 4;
- aRect.bottom = aRect.top + 15;
- aRect.left = aRect.left;
- aRect.right = aRect.right;
-
- FrameRect (&aRect);
- InsetRect (&aRect, 1, 1);
- EraseRect (&aRect);
-
- anInt = GetCtlValue (theControl);
- NumToString (anInt, aStr);
- anInt = StringWidth (aStr) / 2;
- MoveTo (aRect.left + ((aRect.right - aRect.left) / 2 - anInt),
- aRect.top + 10);
- DrawString (aStr);
- TextFont (0);
- TextSize (12);
- }
-
- //• --------------------------------------------------------------- •//
- void OneTimeStrings ()
- {
- char s[64];
- short val1, val2, val3, val4;
- unsigned short decValue;
- Str255 aString;
-
- MoveTo (24, 248);
- DrawString ("\p0x0000");
-
- MoveTo (24, 292);
- DrawString ("\p0000000000000000");
- }
-
- //• --------------------------------------------------------------- •//
- void ControlsUpdate ()
- {
- GrafPtr thePort;
-
- GetPort (&thePort);
- SetPort (hexWindow);
- DrawControls (thePort);
- ShowScrlValue (hexOne);
- ShowScrlValue (hexTwo);
- ShowScrlValue (hexTri);
- ShowScrlValue (hexQua);
-
- OneTimeStrings ();
- }
-
- //• --------------------------------------------------------------- •//
- pascal void Track (ControlHandle Control, short partCode)
- {
- short i, step, val1, val2, val3, val4;
- unsigned short decValue;
- long waited;
- char s[128];
- Str255 aString;
-
- if (partCode == 0)
- return;
-
- switch (partCode)
- {
- case inUpButton:
- step = -1;
- break;
-
- case inDownButton:
- step = 1;
- break;
-
- case inPageUp:
- step = -1;
- break;
-
- case inPageDown:
- step = 1;
- break;
- }
-
- i = GetCtlValue (Control) + step;
-
- if (*Control == *hexOne)
- {
- if (i > 15)
- i = 15;
- if (i < 0)
- i = 0;
-
- SetCtlValue (Control, i);
-
- EraseRect (&hexOneDataRect);
- EraseRect (&bitOneDataRect);
- MoveTo (41, 248);
- thing = GetCtlValue (hexOne);
- MakeHex(thing);
- MoveTo (24, 292);
- MakeBits(thing);
- ShowScrlValue (hexOne);
- }
-
- if (*Control == *hexTwo)
- {
- if (i > 15)
- i = 15;
- if (i < 0)
- i = 0;
-
- SetCtlValue (Control, i);
-
- EraseRect (&hexTwoDataRect);
- EraseRect (&bitTwoDataRect);
- MoveTo (49, 248);
- thing = GetCtlValue (hexTwo);
- MakeHex(thing);
- MoveTo (56, 292);
- MakeBits(thing);
- ShowScrlValue (hexTwo);
- }
-
- if (*Control == *hexTri)
- {
- if (i > 15)
- i = 15;
- if (i < 0)
- i = 0;
-
- SetCtlValue (Control, i);
-
- EraseRect (&hexTriDataRect);
- EraseRect (&bitTriDataRect);
- MoveTo (57, 248);
- thing = GetCtlValue (hexTri);
- MakeHex(thing);
- MoveTo (88, 292);
- MakeBits(thing);
- ShowScrlValue (hexTri);
- }
- if (*Control == *hexQua)
- {
- if (i > 15)
- {
- i = 15;
- return;
- }
- if (i < 0)
- {
- i = 0;
- return;
- }
-
- SetCtlValue (Control, i);
-
- EraseRect (&hexQuaDataRect);
- EraseRect (&bitQuaDataRect);
- MoveTo (65, 248);
- thing = GetCtlValue (hexQua);
- MakeHex(thing);
- MoveTo (120, 292);
- MakeBits(thing);
- ShowScrlValue (hexQua);
- }
- val1 = GetCtlValue (hexOne) * 16 * 16 * 16;
- val2 = GetCtlValue (hexTwo) * 16 * 16;
- val3 = GetCtlValue (hexTri) * 16;
- val4 = GetCtlValue (hexQua);
- decValue = val1 + val2 + val3 + val4;
- NumToString (decValue, aString);
- EraseRect (&decRect);
- MoveTo ((decRect.right-5) - StringWidth (aString), 270);
- DrawString (aString);
- }
-
- //• --------------------------------------------------------------- •//
- void MakeHex (short thing) //• One way to fake it.
- {
- switch (thing)
- {
- case 0:
- DrawString ("\p0");
- break;
-
- case 1:
- DrawString ("\p1");
- break;
-
- case 2:
- DrawString ("\p2");
- break;
-
- case 3:
- DrawString ("\p3");
- break;
-
- case 4:
- DrawString ("\p4");
- break;
-
- case 5:
- DrawString ("\p5");
- break;
-
- case 6:
- DrawString ("\p6");
- break;
-
- case 7:
- DrawString ("\p7");
- break;
-
- case 8:
- DrawString ("\p8");
- break;
-
- case 9:
- DrawString ("\p9");
- break;
-
- case 10:
- DrawString ("\pA");
- break;
-
- case 11:
- DrawString ("\pB");
- break;
-
- case 12:
- DrawString ("\pC");
- break;
-
- case 13:
- DrawString ("\pD");
- break;
-
- case 14:
- DrawString ("\pE");
- break;
-
- case 15:
- DrawString ("\pF");
- break;
- }
- }
-
- //• --------------------------------------------------------------- •//
- void MakeBits (short thing) //• Faking bits the same way.
- {
- switch (thing)
- {
- case 0:
- DrawString ("\p0000");
- break;
-
- case 1:
- DrawString ("\p0001");
- break;
-
- case 2:
- DrawString ("\p0010");
- break;
-
- case 3:
- DrawString ("\p0011");
- break;
-
- case 4:
- DrawString ("\p0100");
- break;
-
- case 5:
- DrawString ("\p0101");
- break;
-
- case 6:
- DrawString ("\p0110");
- break;
-
- case 7:
- DrawString ("\p0111");
- break;
-
- case 8:
- DrawString ("\p1000");
- break;
-
- case 9:
- DrawString ("\p1001");
- break;
-
- case 10:
- DrawString ("\p1010");
- break;
-
- case 11:
- DrawString ("\p1011");
- break;
-
- case 12:
- DrawString ("\p1100");
- break;
-
- case 13:
- DrawString ("\p1101");
- break;
-
- case 14:
- DrawString ("\p1110");
- break;
-
- case 15:
- DrawString ("\p1111");
- break;
- }
- }
-
- //• --------------------------------------------------------------- •//
- void ControlMouse (Point thePoint) //• Leftover from Earthplot.
- {
- ControlHandle theControl;
- short partCode;
- char s[100];
-
- partCode = FindControl (thePoint, hexWindow, &theControl);
-
- if (partCode)
- {
- switch (partCode)
- {
- case inUpButton:
- case inDownButton:
- case inPageUp:
- case inPageDown:
- partCode = TrackControl (theControl, thePoint, Track);
- break;
- case inThumb:
- break;
- }
- }
- }
-
- //• --------------------------------------------------------------- •//
- void EmbossString (Str255 string, short h, short v)
- {
- MoveTo (h - 1, v - 1); //• Passed loc. minus 1.
- SetForeColor (0xffff, 0xffff, 0xffff); //• White.
- DrawString (string); //• First string.
- MoveTo (h + 1, v + 1); //• passed loc. plus 1.
- SetForeColor (0xaaaa, 0xaaaa, 0xaaaa); //• A medium gray.
- DrawString (string); //• Second string.
- MoveTo (h, v); //• Passed location.
- SetForeColor (0, 0, 0); //• Black.
- DrawString (string); //• Final string.
- }
-
- //• --------------------------------------------------------------- •//
- void EmbossFrame (Rect someRect)
- {
- OffsetRect (&someRect, 1, 1);
- SetForeColor (0xaaaa, 0xaaaa, 0xaaaa); //• A medium gray.
- FrameRect (&someRect);
- OffsetRect (&someRect, -2, -2);
- SetForeColor (0xffff, 0xffff, 0xffff); //• White.
- FrameRect (&someRect);
- OffsetRect (&someRect, 1, 1);
- SetForeColor (0, 0, 0); //• Black
- FrameRect (&someRect);
- }
-
- //• --------------------------------------------------------------- •//
- void ShadowBox (Rect someRect) //• Receive rect to be done.
- {
- OffsetRect (&someRect, 2, 2); //• Bop it down, right, 2.
- SetForeColor (40960, 40960, 40960); //• Using 1/3 gray...
- FrameRect (&someRect); //• ...frame it.
- OffsetRect (&someRect, -1, -1); //• Bring it up, left, 1.
- SetForeColor (20480, 20480, 20480); //• Using 2/3 gray...
- FrameRect (&someRect); //• ...frame it.
- OffsetRect (&someRect, -1, -1); //• Bring it up, left, 1 more.
- SetForeColor (0, 0, 0); //• Using 100% gray...
- FrameRect (&someRect); //• ...frame it.
- InsetRect (&someRect, 1, 1); //• Shrink it by 1, all sides.
- EraseRect (&someRect); //• Clean out the in'ards.
- }
-
- //• --------------------------------------------------------------- •//
- void InitHexThing ()
- {
- SetRect (&controlWindowRect, 20, 40, 220, 350);
-
- hexWindow = NewCWindow (nil, &controlWindowRect, "\pitty bitty bytes™",
- true, documentProc, (WindowPtr)-1L, true, 0L);
-
- SetPort (hexWindow);
-
- SetUpRects ();
- DrawStuff ();
- }
-
- //• --------------------------------------------------------------- •//
- void DrawStuff ()
- {
- SetBackColor (0xeeee, 0xeeee, 0xeeee);
- EraseRect (&hexWindow->portRect);
- SetBackColor (0xffff, 0xffff, 0xffff);
-
- hexOne = NewControl(hexWindow, &hexOneRect, "\p", true, 0, 0, 15, 16, 0L);
- hexTwo = NewControl(hexWindow, &hexTwoRect, "\p", true, 0, 0, 15, 16, 0L);
- hexTri = NewControl(hexWindow, &hexTriRect, "\p", true, 0, 0, 15, 16, 0L);
- hexQua = NewControl(hexWindow, &hexQuaRect, "\p", true, 0, 0, 15, 16, 0L);
-
- ShadowBox (hexRect);
- ShadowBox (decRect);
- ShadowBox (bitRect);
- InsetRect (&decRect, 1, 1); //• No wipe rect so must do this.
-
- EmbossFrame (ctlBoxRect);
-
- EmbossString ("\pCtl. Value", hexRect.right + 22, 224);
- EmbossString ("\pHex. Value", hexRect.right + 22, 248);
- EmbossString ("\pDec. Value", hexRect.right + 22, 270);
- EmbossString ("\pBits", bitRect.right + 10, 292);
- // EmbossString ("\p(No Thumb)", 23, 15);
-
- ControlsUpdate ();
- }
-
- //• --------------------------------------------------------------- •//
- void SetUpRects ()
- {
- SetRect (&aboutRect, 110, 20, 190, 190);
-
- SetRect (&ctlBoxRect, 16, 16, 100, 231);
- SetRect (&hexOneRect, 20, 20, 36, 208);
- SetRect (&hexTwoRect, 40, 20, 56, 208);
- SetRect (&hexTriRect, 60, 20, 76, 208);
- SetRect (&hexQuaRect, 80, 20, 96, 208);
-
- SetRect (&hexRect, 20, 235, 80, 252);
- SetRect (&hexOneDataRect, 41, 238, 49, 248);
- SetRect (&hexTwoDataRect, 41 + 8, 238, 49 + 8, 248);
- SetRect (&hexTriDataRect, 41 + 8 + 8, 238, 49 + 8 + 8, 248);
- SetRect (&hexQuaDataRect, 41 + 8 + 8 + 8, 238, 49 + 8 + 8 + 8, 248);
-
- SetRect (&decRect, 20, 256, 70, 274);
-
- SetRect (&bitRect, 20, 278, 156, 296);
- SetRect (&bitOneDataRect, 24, 281, 56, 292);
- SetRect (&bitTwoDataRect, 24 + 32, 281, 56 + 32, 292);
- SetRect (&bitTriDataRect, 24 + 32 + 32, 281, 56 + 32 + 32, 292);
- SetRect (&bitQuaDataRect, 24 + 32 + 32 + 32, 281, 56 + 32 + 32 + 32, 292);
- }
-
- //• --------------------------------------------------------------- •//
- void WindowClicks (WindowPtr theWindow, Point where)
- {
- if (theWindow == hexWindow)
- {
- SelectWindow (hexWindow);
- SetPort (hexWindow);
- GlobalToLocal (&where);
- ControlMouse (where);
- }
- }
-
- //• --------------------------------------------------------------- •//
- void Updates ()
- {
- WindowPtr ActivePort, whichWindow;
- short x, width;
-
- //• Save the current port in 'activeport', set the port to the
- //• window needing updating, redraw the contents of the window,
- //• restore the port to the original 'activeport'.
- GetPort(&ActivePort);
- whichWindow = (WindowPtr) mainEvent.message;
-
- if (whichWindow == hexWindow) //• Control window.
- {
- BeginUpdate (hexWindow);
- SelectWindow (hexWindow);
- SetPort (hexWindow);
- ControlsUpdate ();
- // OneTimeStrings ();
- EndUpdate (hexWindow);
- }
- SetPort (ActivePort);
- }
-
- //• --------------------------------------------------------------- •//
- void MenuPicks (long mSelect)
- {
- short menuID = HiWord(mSelect);
- short menuItem = LoWord(mSelect);
- Str255 name;
- GrafPtr savePort;
- WindowPeek frontWindow;
- switch (menuID)
- {
- case appleID:
- if (menuItem == 1)
- DoAbout ();
- else
- {
- GetPort(&savePort);
- GetItem(appleMenu, menuItem, name);
- OpenDeskAcc(name);
- SetPort(savePort);
- }
- break;
-
- case fileID:
- switch (menuItem)
- {
- case quitItem:
- ExitToShell();
- break;
- }
- break;
-
- case editID:
- if (!SystemEdit(menuItem-1))
- SysBeep(5);
- break;
- }
- }
-
- //• --------------------------------------------------------------- •//
- void MouseClicks (EventRecord *event)
- {
- WindowPtr theWindow;
- Point thePoint;
-
- short windowCode = FindWindow (event->where, &theWindow);
- switch (windowCode)
- {
- case inSysWindow:
- SystemClick (event, theWindow);
- break;
-
- case inMenuBar:
- MenuPicks (MenuSelect (event->where));
- break;
-
- case inDrag:
- DragWindow (theWindow, event->where, &qd.screenBits.bounds);
- break;
-
- case inContent:
- WindowClicks (theWindow, event->where);
- break;
-
- case inGoAway:
- if (theWindow == theWindow &&
- TrackGoAway (theWindow, event->where))
- ExitToShell ();
- break;
- }
- }
-
- void Events(void)
- {
- int ok;
- EventRecord theEvent;
-
- HiliteMenu(0);
- SystemTask (); /* Handle desk accessories */
-
- ok = GetNextEvent (everyEvent, &theEvent);
- if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- MouseClicks(&theEvent);
- break;
-
- case keyDown:
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- MenuPicks(MenuKey((char) (theEvent.message & charCodeMask)));
- }
- break;
-
- case updateEvt:
- BeginUpdate(hexWindow);
- EndUpdate(hexWindow);
- break;
-
- case activateEvt:
- InvalRect(&hexWindow->portRect);
- break;
- }
- }
-
- //• --------------------------------------------------------------- •//
- void SetUpMenus ()
- {
- InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
- InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
- InsertMenu (editMenu = NewMenu (editID, "\pEdit"), 0);
- DrawMenuBar ();
- AppendMenu (appleMenu, "\pNeed I Say More?");
- AddResMenu (appleMenu, 'DRVR');
- AppendMenu (fileMenu, "\pQuit/Q");
- AppendMenu (editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
- }
-
- //• --------------------------------------------------------------- •//
- Boolean CheckForColor ()
- {
- SysEnvRec theMac;
-
- if (theMac.hasColorQD)
- weBeHued = true;
- else
- weBeHued = false;
- }
-
- //• --------------------------------------------------------------- •//
- void SetUpMac ()
- {
- EventRecord myevent;
-
- MaxApplZone ();
- InitGraf (&qd.thePort);
- InitFonts ();
- FlushEvents (everyEvent, 0);
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (0L);
- InitCursor ();
- }
-
- //• --------------------------------------------------------------- •//
- void main ()
- {
- SetUpMac ();
- CheckForColor ();
- if (weBeHued)
- {
- SetUpMenus ();
- InitHexThing ();
-
- for (;;)
- Events (); /* loop 'til Quit selected */
- }
- }
-
-