home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright © 1993-1997 Fabrizio Oddone
- ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
- This source code is distributed as freeware:
- you may copy, exchange, modify this code.
- You may include this code in any kind of application: freeware,
- shareware, or commercial, provided that full credits are given.
- You may not sell or distribute this code for profit.
- */
-
- #include <Limits.h>
-
- #include "UtilsSys7.h"
- #include "Conversions.h"
- #include "FabWList.h"
- #include "Independents.h"
- #include "SimResIDs.h"
-
- #include "Globals.h"
- #include "Disasm.h"
- #include "Dump.h"
- #include "DoEditDialog.h"
- #include "DoMenu.h"
- #include "Main.h"
- #include "Input.h"
- #include "Registers.h"
- #include "Scroll.h"
- #include "TrackThumb.h"
- #include "DragManSim.h"
- #include "SimUtils.h"
-
-
- #if defined(FabSystem7orlater)
-
-
- static ControlRef dumpVScroll;
- static WindowRef curPosW;
- static short dumpLineHeight, dumpFromTop, dumpCWidMax;
-
- static void NoRealTimeActionProc(ControlHandle control, short value);
- static void RealTimeActionProc(ControlHandle control, short value);
- static pascal void DumpActionProc(ControlHandle control, short part);
- static void DrawLine(Ptr start);
- static short TrackDumpObject(RectPtr r, short theLoc);
-
- #pragma segment Init
-
- /* InitDump: initializes the Dump window */
-
- OSErr InitDump(void)
- {
- enum {
- kCharsInOneLine = 0x002D0000
- };
-
- FMetricRec theMetrics;
- FabWindowPtr thefabw;
- Fixed tempFix;
- WindowPtr window;
- OSErr err = appMemFullErr;
-
- gWPtr_Dump = window = GetNewWindow(kWIND_Dump, nil, (WindowPtr)-1L);
- if (window) {
- SetPortWindowPort(window);
- if (gPrefs.remembWind)
- if (IsOnScreenWeak(gPrefs.DumpTopLeft))
- MoveWindow(window, gPrefs.DumpTopLeft.h, gPrefs.DumpTopLeft.v, false);
- if (dumpVScroll = GetNewControl(kCNTL_dmpVScroll, window)) {
- TextFont(monaco);
- TextSize(9);
- TextMode(srcCopy);
- FontMetrics(&theMetrics);
- tempFix = theMetrics.descent + theMetrics.ascent + theMetrics.leading;
- tempFix = mySwap(tempFix);
- dumpLineHeight = (short)tempFix;
- dumpFromTop = *(short *)&theMetrics.ascent;
- dumpCWidMax = *(short *)&theMetrics.widMax;
- tempFix = FixMul(theMetrics.widMax, kCharsInOneLine);
- tempFix = mySwap(tempFix);
- SizeWindow(window, (short)tempFix + kScrollbarWidth + kDIST_FROMLEFT,
- gPrefs.remembWind ? gPrefs.DumpHeight :
- ((PRCT_B(window) - PRCT_T(window)) / dumpLineHeight) * dumpLineHeight,
- false);
- SetupVertScrollBar(window, dumpVScroll);
- SetupDumpCtlMax(dumpVScroll);
- if (gPrefs.remembWind)
- SetControlValue(dumpVScroll, gPrefs.DumpScrollVal);
-
- thefabw = AddWindowToList(window);
- SetActivate(thefabw, Activate_Dump);
- SetUpdate(thefabw, Update_Dump);
- SetGrow(thefabw, Grow_Dump);
- SetGoAway(thefabw, CloseDump);
- SetContent(thefabw, Do_Dump);
- SetGetDragRect(thefabw, getDragRectDump);
-
- InstallRgnHandler(thefabw, NewRgn(), RecalcDump, nil,
- toMenu(kBalloons_Dump, kBDump_Contents), 0, 0);
-
- ResizeObjects(window);
- RecalcGlobalCoords(thefabw);
-
- if (gDragManagerActive)
- (void) MyInstallHWindow(window);
- err = noErr;
- }
- }
- return err;
- } /* InitDump */
-
-
- #pragma segment Main
-
- /*
- GetDumpVScrollValue: accessor */
-
- short GetDumpVScrollValue(void)
- {
- return GetControlValue(dumpVScroll);
- }
-
- /*
- GetDumpLineHeight: accessor */
-
- short GetDumpLineHeight(void)
- {
- return dumpLineHeight;
- }
-
- void ScrollDumpTo(short newPos)
- {
- SetControlValue(dumpVScroll, newPos);
- InvalDump();
- }
-
- void DumpHome(void)
- {
- SetControlValue(dumpVScroll, GetControlMinimum(dumpVScroll));
- InvalDump();
- }
-
- void DumpEnd(void)
- {
- SetControlValue(dumpVScroll, GetControlMaximum(dumpVScroll));
- InvalDump();
- }
-
- void DumpPgUp(void)
- {
- DumpActionProc(dumpVScroll, kControlPageUpPart);
- }
-
- void DumpPgDn(void)
- {
- DumpActionProc(dumpVScroll, kControlPageDownPart);
- }
-
- void Activate_Dump(EventRecord */*evt*/, WindowPtr w, Handle, short, Boolean becomingActive)
- {
- Rect growRect;
-
- /* the growbox needs to be redrawn on activation: */
- growRect = w->portRect;
- /* adjust for the scrollbars */
- growRect.top = growRect.bottom - kScrollbarAdjust + 1;
- growRect.left = growRect.right - kScrollbarAdjust + 1;
- if (becomingActive) {
- InvalRect(&growRect); /* we cannot avoid grow box flicker */
- if ((*dumpVScroll)->contrlVis == 0) {
- ShowControl(dumpVScroll);
- ValidRect(&(*dumpVScroll)->contrlRect);
- }
- }
- else {
- /* the control must be redrawn on deactivation: */
- HideControl(dumpVScroll);
- InvalRect(&growRect);
- }
- } /*Activate*/
-
- void Grow_Dump(WindowPtr w, EventRecord *event)
- {
- Rect tempRect, updateRect;
- long growResult;
-
- tempRect.right = tempRect.left = PRCT_R(w) + 1;
- tempRect.bottom = SHRT_MAX; /* set up limiting values */
- tempRect.top = kMinDocDim + 30;
- updateRect = w->portRect;
- updateRect.top = updateRect.bottom - kScrollbarAdjust;
- /* see if it really changed size */
- if (growResult = GrowWindow(w, event->where, &tempRect)) {
- SizeWindow(w, LoWrd(growResult),
- (HiWrd(growResult) / dumpLineHeight) * dumpLineHeight, true);
- SizeControl(dumpVScroll, kScrollbarWidth, PRCT_B(w) - PRCT_T(w) - 13);
- if (HiWrd(growResult) > updateRect.bottom)
- /* enlarged */
- InvalRect(&updateRect);
- else /* reduced */ {
- updateRect.bottom = PRCT_B(w);
- updateRect.right = PRCT_R(w);
- updateRect.top = updateRect.bottom - kScrollbarAdjust + 1;
- updateRect.left = updateRect.right - kScrollbarAdjust + 1;
- InvalRect(&updateRect);
- }
- ValidRect(&(*dumpVScroll)->contrlRect);
- SetupDumpCtlMax(dumpVScroll);
- }
- }
-
- void Update_Dump(WindowPtr w, short)
- {
- Rect growRect;
- RgnHandle oldClip;
-
- if (EmptyRgn(w->visRgn) == false) { /* draw if updating needs to be done */
- DrawDump(w);
- oldClip = NewRgn();
- GetClip(oldClip);
- growRect = w->portRect;
- growRect.left = growRect.right - kScrollbarAdjust;
- ClipRect(&growRect);
- DrawGrowIcon(w);
- SetClip(oldClip);
- DisposeRgn(oldClip);
- UpdateControls(w, w->visRgn);
- }
- }
-
- void Do_Dump(WindowPtr w, EventRecord *event)
- {
- Rect tempRect;
- GrafPtr savePort;
- ControlHandle control;
- Point mouse;
- unsigned long clickAddr;
- short part, offset, popItem;
-
- mouse = event->where; /* get the click position */
- GlobalToLocal(&mouse);
- /* see if we are in the dump area; if so, we won’t check the controls */
- tempRect = w->portRect;
- tempRect.right -= kScrollbarAdjust;
- if (PtInRect(mouse, &tempRect)) {
- /* handle editing click */
- if ((offset = mouse.h - dumpCWidMax * 6 - kDIST_FROMLEFT) >= 0)
- if ((offset /= dumpCWidMax) % 5 != 4) {
- tempRect.top = (mouse.v /= dumpLineHeight) * dumpLineHeight;
- tempRect.left = ((offset /= 5) * 5) * dumpCWidMax + dumpCWidMax * 6 + kDIST_FROMLEFT;
- tempRect.bottom = tempRect.top + dumpLineHeight;
- tempRect.right = tempRect.left + (dumpCWidMax << 2);
- clickAddr = ((unsigned long)(GetControlValue(dumpVScroll) + mouse.v) << 4)
- + (offset <<= 1);
- if (popItem = TrackDumpObject(&tempRect, *(unsigned short *)(gMMemory + clickAddr))) {
- if (popItem == kD_Disasm) {
- ScrollDisasmTo(clickAddr >> 2);
- DoMenuWindows(kMItem_Disasm);
- }
- else if (event->modifiers & optionKey || popItem == kD_DisasmFrom) {
- ScrollDisasmTo(*(unsigned short *)(gMMemory + clickAddr) >> 1);
- DoMenuWindows(kMItem_Disasm);
- }
- else if (event->modifiers & cmdKey || popItem == kD_DumpFrom) {
- ScrollDumpTo(*(unsigned short *)(gMMemory + clickAddr) >> 3);
- }
- else {
- if (DoEditDump((short *)(gMMemory + clickAddr), clickAddr >> 1)) {
- InvalDump();
- InvalDisasm();
- }
- UnloadSeg(DoEditDump);
- }
- }
- }
- }
- else {
- part = FindControl(mouse, w, &control);
- switch ( part ) {
- case 0: /* do nothing for viewRect case */
- break;
- case kControlIndicatorPart:
- if (gPrefs.NeXTScroll)
- (void)TrackThumb(control, mouse, RealTimeActionProc);
- else {
- GetPort(&savePort);
- curPosW = GetNewWindow(kWIND_CurPos, nil, (WindowPtr)-1L);
- SetPortWindowPort(curPosW);
- TextMode(srcCopy);
- TextFont(monaco);
- TextSize(9);
- SetPort(savePort);
- // Apple would like us not to use GhostWindow, but in this case we avoid
- // plenty of useless activate/deactivate events, so we don’t feel guilty.
-
- savePort = (GrafPtr)LMGetGhostWindow();
- LMSetGhostWindow(curPosW);
- ShowWindow(curPosW);
- (void) TrackThumb(control, mouse, NoRealTimeActionProc);
- DisposeWindow(curPosW);
- LMSetGhostWindow(savePort);
- InvalDump();
- }
- break;
- default: /* clicked in an arrow, so track & scroll */
- {
- ControlActionUPP DumpActionProcUPP = NewControlActionProc(DumpActionProc);
-
- (void) TrackControl(control, mouse, DumpActionProcUPP);
- if (DumpActionProcUPP)
- DisposeRoutineDescriptor(DumpActionProcUPP);
- }
- break;
- }
- }
- }
-
- /* NoRealTimeActionProc: only updates the number in the little window up there */
-
- static void NoRealTimeActionProc(ControlHandle , short value)
- {
- Str15 tempS;
- GrafPtr savePort;
-
- ShortToHexString(value << 3, tempS);
- GetPort(&savePort);
- SetPort(curPosW);
- MoveTo(kDIST_FROMLEFT,12);
- DrawString(tempS);
- SetPort(savePort);
- }
-
- /* RealTimeActionProc: updates all of the window contents while dragging the scroll box */
-
- static void RealTimeActionProc(ControlHandle control, short )
- {
- DrawDump((*control)->contrlOwner);
- }
-
- static pascal void DumpActionProc(ControlHandle control, short part)
- {
- Rect tempRect;
- WindowPtr w;
- Ptr addr;
- short amount, oldAmount, newAmount, vc;
- Boolean doScrollRect = false;
-
- if ( part ) { /* if it was actually in the control */
- w = (*control)->contrlOwner;
- switch ( part ) {
- case kControlUpButtonPart:
- amount = -1;
- doScrollRect = true;
- break;
- case kControlDownButtonPart:
- amount = 1;
- doScrollRect = true;
- break;
- case kControlPageUpPart:
- amount = (PRCT_T(w) - PRCT_B(w)) / dumpLineHeight + 1;
- break;
- case kControlPageDownPart:
- amount = (PRCT_B(w) - PRCT_T(w)) / dumpLineHeight - 1;
- break;
- }
- SetControlValue(control, (oldAmount = GetControlValue(control)) + amount);
- if (doScrollRect && (newAmount = oldAmount - GetControlValue(control))) {
- tempRect = w->portRect;
- tempRect.right -= kScrollbarAdjust;
- VScrollRect(&tempRect, newAmount * dumpLineHeight);
- addr = &gMMemory[(unsigned long)GetControlValue(control) << 4];
- vc = dumpFromTop;
- if (newAmount < 0) {
- vc += PRCT_B(w) - PRCT_T(w) - dumpLineHeight;
- addr += ((PRCT_B(w) - PRCT_T(w)) / dumpLineHeight - 1) << 4;
- }
- MoveTo(PRCT_L(w) + kDIST_FROMLEFT, vc);
- DrawLine(addr);
- }
- else
- DrawDump(w);
- }
- } /* DumpActionProc */
-
- void DrawDump(WindowPtr w)
- {
- Rect tempRect;
- GrafPtr savePort;
- Point tempPoint;
- Ptr addr;
- short j;
-
- GetPort(&savePort);
- SetPort(w);
- tempRect = w->portRect;
- tempRect.bottom += dumpLineHeight;
- addr = &gMMemory[(unsigned long)GetControlValue(dumpVScroll) << 4];
- for(tempPoint.h = kDIST_FROMLEFT, j = dumpFromTop;
- tempPoint.v = j, PtInRect(tempPoint, &tempRect);
- j += dumpLineHeight, addr += 16) {
-
- MoveTo(PRCT_L(w) + kDIST_FROMLEFT, j);
- DrawLine(addr);
- }
- SetPort(savePort);
- }
-
- /* DrawLine: draws a "line" of memory in the Dump window */
-
- static void DrawLine(Ptr start)
- {
- Str63 tempS;
- short *hexPtr, *endPtr;
- Ptr textPtr;
-
- endPtr = (short *)(start + 16);
- textPtr = ShortToHexText(PTR2MEMWORD(start), (Ptr)&tempS);
- *textPtr++ = ':';
- for (hexPtr = (short *)start; hexPtr < endPtr; ) {
- *textPtr++ = ' ';
- textPtr = ShortToHexText(*hexPtr++, textPtr);
- }
- DrawText(&tempS, 0, 45);
- }
-
- /* SetupDumpCtlMax: sets up the CtlMax value of our scroll bar */
-
- void SetupDumpCtlMax(ControlHandle theControl)
- {
- enum {
- kAdjustForPleasantGrow = 3
- };
-
- WindowPtr wind;
- short newmax;
-
- wind = (*theControl)->contrlOwner;
- newmax = 8191 - (PRCT_B(wind) - PRCT_T(wind) - kAdjustForPleasantGrow) / dumpLineHeight;
- if (newmax != GetControlMaximum(theControl)) {
- SetControlMaximum(theControl, newmax);
- InvalDump();
- }
- }
-
- void InvalDump(void)
- {
- Rect tempRect;
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(gWPtr_Dump);
- tempRect = gWPtr_Dump->portRect;
- tempRect.right -= kScrollbarWidth;
- InvalRect(&tempRect);
- SetPort(savePort);
- }
-
- /* TrackDumpObject: like all the TrackThing in the Mac OS, plus the popUp */
-
- short TrackDumpObject(RectPtr r, short theLocation)
- {
- enum {
- kSimpleClick = -1,
- kDelay = 1 * 60 // one second
- };
-
- Str32 itemStr;
- Str15 numStr;
- Point myPt;
- MenuRef tempMenu;
- long timeout = TickCount();
- long chosen = kSimpleClick;
- Boolean inrect;
-
- InvertRect(r);
- inrect = true;
- do {
- Boolean tempB;
-
- GetMouse(&myPt);
- if ((tempB = PtInRect(myPt, r)) != inrect) {
- InvertRect(r);
- inrect = tempB;
- }
- if (inrect == false)
- timeout = TickCount();
- if ((TickCount() - timeout) > GetDblTime()) {
- tempMenu = gPopMenu;
- if (noErr == HandToHand((Handle *)&tempMenu)) {
- GetMenuItemText(tempMenu, kD_DisasmFrom, itemStr);
- ShortToHexString(theLocation, numStr);
- PLstrcat(itemStr, numStr);
- SetMenuItemText(tempMenu, kD_DisasmFrom, itemStr);
- GetMenuItemText(tempMenu, kD_DumpFrom, itemStr);
- PLstrcat(itemStr, numStr);
- SetMenuItemText(tempMenu, kD_DumpFrom, itemStr);
- InsertMenu(tempMenu, hierMenu);
- LocalToGlobal(&myPt);
- chosen = PopUpMenuSelect(tempMenu, myPt.v, myPt.h, kD_Edit);
- DeleteMenu(kRes_Menu_PopDump);
- DisposeMenu(tempMenu);
- }
- }
- }
- while( StillDown() );
- if (inrect)
- InvertRect(r);
- if (chosen == kSimpleClick)
- chosen = inrect ? toMenu(kRes_Menu_PopDump, kD_Edit) : 0L;
- return HiWrd(chosen) ? LoWrd(chosen) : 0;
- }
-
- /* procedure called when closing the Dump window */
-
- void CloseDump(WindowPtr w)
- {
- DoCloseWindow(w, kMItem_Dump);
- }
-
- void RecalcDump(DialogRef w, RgnBalloonCursPtr theObj)
- {
- Rect tempRect;
-
- tempRect = w->portRect;
- tempRect.right -= kScrollbarWidth;
- RectRgn(theObj->zoneLocal, &tempRect);
- }
-
- void getDragRectDump(WindowPtr w, RectPtr r)
- {
- *r = w->portRect;
- r->right -= kScrollbarAdjust;
- }
-
- #endif
-
-