home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / Registers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-25  |  7.9 KB  |  317 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993-1997 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "FabWList.h"
  14. #include    "Independents.h"
  15. #include    "SimResIDs.h"
  16.  
  17. #include    "Disasm.h"
  18. #include    "DoEditDialog.h"
  19. #include    "Globals.h"
  20. #include    "Main.h"
  21. #include    "Registers.h"
  22. #include    "DragManSim.h"
  23. #include    "SimUtils.h"
  24.  
  25. #if defined(FabSystem7orlater)
  26.  
  27. enum {
  28. kREGFONT = 128
  29. };
  30.  
  31.  
  32. /* CLRRECT: makes a Rect an empty Rect */
  33. #define    CLRRECT(r)    (*(long *)&r = 0, *(long *)((Ptr)&r + 2) = 0)
  34.  
  35. static ControlHandle    Ctrl_Base;
  36. static short    regLineHeight;
  37. static short    regDistFromTop;
  38. static short    regVertSepLine;
  39. static short    oldChoice;
  40.  
  41. static void Do_A_Button(ControlHandle theControl);
  42.  
  43.  
  44. #pragma segment Init
  45. /* Init_Registers: initializes the Registers window */
  46.  
  47. OSErr Init_Registers(void)
  48. {
  49. Str255    tempS;
  50. FMetricRec    theMetrics;
  51. Rect    tempRect;
  52. FabWindowPtr    thefabw;
  53. WindowPtr    w;
  54. Handle    myStrHandle;
  55. RgnHandle    theRgn;
  56. Fixed    tempFixed;
  57. short    fontNumber, fontSize;
  58. short    theWidth, i;
  59. short    maxLength = 0;
  60. OSErr    err = appMemFullErr;
  61.  
  62. gWPtr_Registers = w = GetNewWindow(kWIND_Registers, nil, (WindowPtr)-1L);
  63. if (w) {
  64.     SetPortWindowPort(w);
  65.     if (gPrefs.remembWind)
  66.         if (IsOnScreenWeak(gPrefs.RegsTopLeft))
  67.             MoveWindow(w, gPrefs.RegsTopLeft.h, gPrefs.RegsTopLeft.v, false);
  68.     (void)WantThisHandleSafe(myStrHandle = Get1Resource('REGF', kREGFONT));
  69.     if (myStrHandle && GetFontNumber((StringPtr)(*myStrHandle + 2), &fontNumber)) {
  70.         fontSize = *(short *)*myStrHandle;
  71.         }
  72.     else {
  73.         fontNumber = monaco;
  74.         fontSize = 9;
  75.         }
  76.     if (myStrHandle)
  77.         ReleaseResource(myStrHandle);
  78.  
  79.     TextFont(fontNumber);
  80.     TextSize(fontSize);
  81.     if (Ctrl_Base = GetNewControl(kCNTL_RegsBase, w)) {
  82.         FontMetrics(&theMetrics);
  83.         tempFixed = theMetrics.descent + theMetrics.ascent + theMetrics.leading;
  84.  
  85.         tempFixed = mySwap(tempFixed);
  86.  
  87.         regLineHeight = (short)tempFixed;
  88.         
  89.         regDistFromTop = *(short *)&theMetrics.ascent + kREG_HORZSEPLINE;
  90.         HLock(myStrHandle = Get1Resource('STR#', kREG_NAMES));
  91.         for(i = 0; i < kDISP_REGS; i++) {
  92.             if (maxLength < (theWidth = StringWidth(GetPtrIndHString(myStrHandle, i))))
  93.                 maxLength = theWidth;
  94.             }
  95.         HUnlock(myStrHandle);
  96.         tempFixed = maxLength;
  97.         regVertSepLine = maxLength + kDIST_FROMLEFT + kDIST_FROMNAME;
  98.         
  99.         tempFixed = mySwap(tempFixed);
  100.         
  101.         maxLength = 0;
  102.             {
  103.             register Byte *myString;
  104.             register Byte    j, rememberedCh, stopAt;
  105.             SignedByte    savedState;
  106.         
  107.             savedState = WantThisHandleSafe(myStrHandle = (Handle)GetString(kSTR_BINALLOWED));
  108.             myString = (Byte *)*myStrHandle;
  109.             for (stopAt = *myString++, j = 0; j < stopAt; j++)
  110.                 if (maxLength < (theWidth = CharWidth(*myString++))) {
  111.                     maxLength = theWidth;
  112.                     rememberedCh = *(myString - 1);
  113.                     }
  114.             HSetState(myStrHandle, savedState);
  115.             for (myString = (Byte *)&tempS, j = 0; j <= 15; j++)
  116.                 *myString++ = rememberedCh;
  117.             maxLength = TextWidth(&tempS, 0, 16);
  118.             }
  119.         
  120.         tempFixed += ((long)maxLength << 16) +
  121.             ((const long)(kDIST_FROMLEFT + kDIST_FROMNAME + kDIST_FROMVERTSEP)<< 16);
  122.         tempFixed = mySwap(tempFixed);
  123.         SizeWindow(w, (short)tempFixed + 2,
  124.                     REG_TOP + regLineHeight * kDISP_REGS, false);
  125.         SetControlValue(Ctrl_Base, gPrefs.remembWind ? gPrefs.RegsBase : kPOP_HEX);
  126.         oldChoice = GetControlValue(Ctrl_Base);
  127.  
  128.     //    InitFabWindow((FabWindowPtr)w);
  129.         thefabw = AddWindowToList(w);
  130.         SetUpdate(thefabw, Update_Registers);
  131.         SetGoAway(thefabw, CloseRegisters);
  132.         SetContent(thefabw, Do_Registers);
  133.         SetGetDragRect(thefabw, getDragRectRegs);
  134.  
  135.         theRgn = NewRgn();
  136.         tempRect = (*Ctrl_Base)->contrlRect;
  137.         RectRgn(theRgn, &tempRect);
  138.         InstallRgnHandler(thefabw, theRgn, nil, nil,
  139.                         toMenu(kBalloons_Regs, kBRegs_Base), 0, 0);
  140.  
  141.         tempRect.top = REG_TOP;
  142.         tempRect.left = PRCT_L(w);
  143.         tempRect.bottom = REG_TOP + regLineHeight;
  144.         tempRect.right = PRCT_R(w);
  145.         for (i = kBRegs_PC; i <= kBRegs_Lo8; i++) {
  146.             theRgn = NewRgn();
  147.             RectRgn(theRgn, &tempRect);
  148.             InstallRgnHandler(thefabw, theRgn, nil, nil,
  149.                             toMenu(kBalloons_Regs, i), 0, 0);
  150.             tempRect.top = tempRect.bottom;
  151.             tempRect.bottom += regLineHeight;
  152.             }
  153. // no resizable objects for this window
  154.     //    ResizeObjects(w);
  155.     
  156.         RecalcGlobalCoords(thefabw);
  157.         if (gDragManagerActive)
  158.             (void) MyInstallHWindow(w);
  159.  
  160.         err = noErr;
  161.         }
  162.     }
  163. //SetFractEnable(false);
  164. return err;
  165. }
  166.  
  167.  
  168. #pragma segment Main
  169.  
  170. short GetRegistersBase(void)
  171. {
  172. return GetControlValue(Ctrl_Base);
  173. }
  174.  
  175. void Update_Registers(WindowPtr w, short)
  176. {
  177. Str255    sTemp;
  178. register Handle    tempH;
  179. register short i,j;
  180.  
  181. EraseRect(&w->portRect);
  182. HLockHi(tempH = Get1Resource('STR#', kREG_NAMES));
  183. for(i = 0, j = regDistFromTop; i <= kDISP_REGS - 1; i++, j += regLineHeight) {
  184.     MoveTo(REG_LEFT + kDIST_FROMLEFT, j);
  185.     DrawString((ConstStr255Param)GetPtrIndHString(tempH, i));
  186.     MoveTo(REG_LEFT + regVertSepLine + kDIST_FROMVERTSEP + 1, j);
  187.     FromNumToString(sTemp, gRegs[i], oldChoice);
  188.     DrawString(sTemp);
  189.     }
  190. HUnlock(tempH);
  191. /*  Draw the horizontal line  */
  192. MoveTo(REG_LEFT, kREG_HORZSEPLINE);
  193. LineTo(REG_RIGHT, kREG_HORZSEPLINE);
  194. /*  Draw the vertical line  */
  195. MoveTo(regVertSepLine, REG_TOP);
  196. LineTo(regVertSepLine, REG_BOTTOM - 1);
  197. UpdateControls(w, w->visRgn);
  198. }
  199.  
  200. void Do_Registers(WindowPtr w, EventRecord *theEvent)
  201. {
  202. Rect    tempRect, oldRect, shiftingRect;
  203. Point    myPt;
  204. ControlHandle    theControl;
  205. register short    code;
  206. register Boolean    inbigrect;
  207.  
  208. myPt = theEvent->where;
  209. GlobalToLocal(&myPt);
  210. if (FindControl(myPt, w, &theControl) != 0) {
  211.     if (TrackControl(theControl, myPt, (ControlActionUPP)-1L) == kControlLabelPart)
  212.         if ((code = GetControlValue(Ctrl_Base)) != oldChoice) {
  213.             oldChoice = code;
  214.             tempRect.top = REG_TOP;
  215.             tempRect.left = REG_LEFT + regVertSepLine + 1;
  216.             tempRect.bottom = REG_BOTTOM;
  217.             tempRect.right = REG_RIGHT;
  218.             InvalRect(&tempRect);
  219.             }
  220.     }
  221. else {
  222.     tempRect.top = REG_TOP;
  223.     tempRect.left = REG_LEFT;
  224.     tempRect.bottom = REG_BOTTOM;
  225.     tempRect.right = REG_RIGHT;
  226.     if (PtInRect(myPt, &tempRect)){
  227.         CLRRECT(oldRect);
  228.         do {
  229.             GetMouse(&myPt);
  230.             if (!PtInRect(myPt, &oldRect)) {
  231.                 if (inbigrect = PtInRect(myPt, &tempRect)) {
  232.                     shiftingRect.top = REG_TOP;
  233.                     shiftingRect.left = REG_LEFT;
  234.                     shiftingRect.bottom = REG_TOP + regLineHeight;
  235.                     shiftingRect.right = REG_RIGHT;
  236.                     for(code = kREG_PC;
  237.                         !(PtInRect(myPt, &shiftingRect)&&(code <= kREG_LOW8));
  238.                         code++, shiftingRect.top += regLineHeight,
  239.                         shiftingRect.bottom += regLineHeight)
  240.                             ;
  241.                     InvertRect(&oldRect);
  242.                     InvertRect(&shiftingRect);
  243.                     oldRect = shiftingRect;
  244.                     }
  245.                 else {
  246.                     InvertRect(&oldRect);
  247.                     CLRRECT(oldRect);
  248.                     }
  249.                 }
  250.             }
  251.         while( StillDown() );
  252.         if (inbigrect) {
  253.             InvertRect(&oldRect);
  254.             if(DoEditDialog(code, kREG_EXTNAMES, oldChoice)) {
  255.                 oldRect.left += regVertSepLine+1;
  256.                 InvalRect(&oldRect);
  257.                 if (code == kREG_PC)
  258.                     InvalDisasm();
  259.                 }
  260.             UnloadSeg(DoEditDialog);
  261.             }
  262.         }
  263.     }
  264. }
  265.  
  266. /* ChangedRegister: to tell us that a register has changed its value */
  267.  
  268. void ChangedRegister(short whichreg)
  269. {
  270. Rect    tempRect;
  271. GrafPtr    savePort;
  272.  
  273. GetPort(&savePort);
  274. SetPort(gWPtr_Registers);
  275. tempRect.top = REG_TOP + (regLineHeight * whichreg);
  276. tempRect.left = REG_LEFT + regVertSepLine+1;
  277. tempRect.bottom = tempRect.top + regLineHeight;
  278. tempRect.right = REG_RIGHT;
  279. InvalRect(&tempRect);
  280. if (whichreg == kREG_PC)
  281.     InvalDisasm();
  282. SetPort(savePort);
  283. }
  284.  
  285. /* ChangedAllRegisters: to tell us that _all_ registers have changed */
  286.  
  287. void ChangedAllRegisters(void)
  288. {
  289. Rect    tempRect;
  290. GrafPtr    savePort;
  291.  
  292. GetPort(&savePort);
  293. SetPortWindowPort(gWPtr_Registers);
  294. tempRect.top = REG_TOP;
  295. tempRect.left = REG_LEFT + regVertSepLine+1;
  296. tempRect.bottom = tempRect.top + (regLineHeight * kREG_ZERO);
  297. tempRect.right = REG_RIGHT;
  298. InvalRect(&tempRect);
  299. InvalDisasm();
  300. SetPort(savePort);
  301. }
  302.  
  303. /* procedure called when closing the Registers window */
  304.  
  305. void CloseRegisters(WindowPtr w)
  306. {
  307. DoCloseWindow(w, kMItem_Registers);
  308. }
  309.  
  310. void getDragRectRegs(WindowPtr w, RectPtr r)
  311. {
  312. *r = w->portRect;
  313. }
  314.  
  315. #endif
  316.  
  317.