home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / Inspector / Sources / RegisteredObjectsWindow.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  12.1 KB  |  424 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        RegisteredObjectsWindow.cp
  3.  
  4.     Contains:    Implementation of TRegisteredObjectsWindow.  A window that displays a list
  5.                 of TRegisteredObjects.
  6.  
  7.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  8.  
  9. */
  10.  
  11. #ifndef __WINDOWS__
  12. #include <Windows.h>
  13. #endif
  14. #ifndef __EVENTS__
  15. #include <Events.h>
  16. #endif
  17. #ifndef __TOOLUTILS__
  18. #include <ToolUtils.h>
  19. #endif
  20. #ifndef __FONTS__
  21. #include <Fonts.h>
  22. #endif
  23. #ifndef __STRING__
  24. #include <string.h>
  25. #endif
  26. #ifndef __Resources__
  27. #include <Resources.h>
  28. #endif
  29. #ifndef __STRINGS__
  30. #include <Strings.h>
  31. #endif
  32.  
  33. #ifndef __REGISTEREDOBJECTSWINDOW__
  34. #include "RegisteredObjectsWindow.h"
  35. #endif
  36. #ifndef __REGISTEREDOBJECTS__
  37. #include "RegisteredObjects.h"
  38. #endif
  39. #ifndef __INSPECTOR__
  40. #include "Inspector.h"
  41. #endif
  42. #ifndef __LIST__
  43. #include "List.h"
  44. #endif
  45.  
  46. //
  47. // structure for saving the location of a window as a resource
  48. //
  49. struct WindowLocationRec
  50. {
  51.     signed short    left, top;
  52.     signed short    width, height;
  53. };    
  54.  
  55. /**********************************************************************
  56. **  PUBLIC Constructor/Destructor
  57. ***********************************************************************/
  58.  
  59. TRegisteredObjectsWindow::TRegisteredObjectsWindow()
  60. {
  61.     fList                = NULL;
  62.     fRegisteredObjects    = NULL;
  63.     fInspector            = NULL;
  64. }
  65.  
  66. TRegisteredObjectsWindow::TRegisteredObjectsWindow(short resID,
  67.     TRegisteredObjects *theObjects, TInspector* theInspector) : TDocument(resID)
  68. {
  69.     InitRegisteredObjectsWindow(theObjects, theInspector);
  70. }
  71.  
  72. TRegisteredObjectsWindow::~TRegisteredObjectsWindow()
  73. {
  74.     fRegisteredObjects->SetRegisteredObjectsWindow(NULL);    /* we no longer exist */
  75.     HideWindow(fDocWindow);
  76.     delete fList;
  77. }
  78.  
  79. /**********************************************************************
  80. ** PRIVATE InitRegisteredObjectsWindow
  81. ***********************************************************************/
  82.  
  83. void TRegisteredObjectsWindow::InitRegisteredObjectsWindow(TRegisteredObjects *theObjects,
  84.                                                            TInspector* theInspector )
  85. {
  86.     fInspector = theInspector;
  87.     
  88.     GrafPtr savedPort;
  89.     GetPort(&savedPort);
  90.     SetPort(fDocWindow);
  91.     TextFont(monaco);
  92.  
  93.     /* set the windows title */
  94.  
  95.     char theName[256];
  96.     strcpy(&theName[1],theObjects->GetClassName());
  97.     theName[0] = strlen(&theName[1]);
  98.     SetWTitle(GetDocWindow(),StringPtr(theName));
  99.  
  100.     /* setup the List Manager list */
  101.  
  102.     Rect listRect = fDocWindow->portRect;
  103.     listRect.right -= 15;            // scroll bar space
  104.     listRect.bottom -= 15;            // scroll bar space
  105.     listRect.left += kButtonSpace;    // button space
  106.     Rect dataBounds = {0,0,0,1};        // empty 1 column list
  107.     Point cSize = {0,0};                // let list manager decide cell size
  108.     fList = new TList(&listRect,&dataBounds,cSize,0,fDocWindow,
  109.                 true/*drawit*/, true/*hasgrow*/, false/*hScroll*/, true/*vScroll*/);
  110.     fList->SetSelFlags(lOnlyOne);        // only allow one cell to be selected
  111.     (**fList->GetList()).cellSize.h = 16383;
  112.     
  113.     fRegisteredObjects = theObjects;
  114.     theObjects->SetRegisteredObjectsWindow(this);    /* it needs a pointer to us */
  115.     UpdateList();
  116.  
  117.     SetPort(savedPort);
  118. }
  119.  
  120. /**********************************************************************
  121. ** PUBLIC DoContent
  122. ***********************************************************************/
  123.  
  124. void TRegisteredObjectsWindow::DoContent(EventRecord* theEvent)
  125. {
  126.     //Trace("TRegisteredObjectsWindow::DoContent\n");
  127.     Point mouse;
  128.  
  129.     SetPort(fDocWindow);
  130.     mouse = theEvent->where;                // get the click position 
  131.     GlobalToLocal(&mouse);
  132.     Rect listRect = GetListRect();
  133.     listRect.right += 15;                    // 15 for the scrollbars
  134.     listRect.bottom += 15;                    // 15 for the scrollbars
  135.     if (PtInRect(mouse,&listRect)) {        // did we click in list regions?
  136.         if (fList->Click(mouse,theEvent->modifiers)) {    // did we double click?
  137.             Cell theCell = fList->LastClick();            // what cell did we double click in
  138.             //*fRegisteredObjects[theCell.v]->Dump();    // dump the object
  139.             TDynamic* theObj = (*fRegisteredObjects)[theCell.v]; 
  140.             //delete theObj;
  141.         }
  142.     }
  143. }
  144.  
  145. /**********************************************************************
  146. ** PUBLIC DoGrow
  147. ***********************************************************************/
  148.  
  149. void TRegisteredObjectsWindow::DoGrow(EventRecord* theEvent)
  150. {
  151.     Rect sizeRect = {100,175, 32767, 32767};
  152.     Rect oldRect = fDocWindow->portRect;
  153.     long result = GrowWindow(fDocWindow, theEvent->where, &sizeRect);
  154.     if (result)
  155.     {
  156.         short h = LoWord(result);
  157.         short v = HiWord(result);
  158.         SizeWindow(h, v);
  159.     }
  160. }
  161.  
  162. /**********************************************************************
  163. ** PUBLIC DoActivate
  164. ***********************************************************************/
  165.  
  166. void TRegisteredObjectsWindow::DoActivate(Boolean becomingActive)
  167. {
  168.     DrawGrowIcon();
  169.     fList->Activate(becomingActive);
  170. }
  171.  
  172. /**********************************************************************
  173. ** PUBLIC DoUpdate
  174. ***********************************************************************/
  175.  
  176. void TRegisteredObjectsWindow::DoUpdate()
  177. {
  178.     BeginUpdate(fDocWindow);                // this sets up the visRgn 
  179.     if (!EmptyRgn(fDocWindow->visRgn)) {    // draw if updating needs to be done 
  180.         fList->Update(fDocWindow->visRgn);
  181.     }
  182.     DrawGrowIcon();
  183.     MoveTo(fDocWindow->portRect.left + kButtonSpace - 1, fDocWindow->portRect.top);
  184.     LineTo(fDocWindow->portRect.left + kButtonSpace - 1, fDocWindow->portRect.bottom);
  185.     EndUpdate(fDocWindow);
  186. }
  187.  
  188. /**********************************************************************
  189. ** PUBLIC DrawWindow
  190. ***********************************************************************/
  191.  
  192. // Draw the contents of an application window. 
  193.  
  194. void TRegisteredObjectsWindow::DrawWindow()
  195. {
  196.     Rect tRect;
  197.  
  198.     SetPort(fDocWindow);
  199.     tRect = fDocWindow->portRect;
  200.     EraseRect(&tRect);
  201.     InvalRect(&tRect);
  202. }
  203.  
  204. /**********************************************************************
  205. ** PUBLIC UpdateList
  206. ***********************************************************************/
  207.  
  208. void TRegisteredObjectsWindow::UpdateList()
  209. {
  210.     GrafPtr savedPort;
  211.     GetPort(&savedPort);
  212.     SetPort(GrafPtr(GetDocWindow()));
  213.  
  214.     EraseRect(&(**fList->GetList()).rView);
  215.  
  216.     fList->DoDraw(false);        // turn off list drawing
  217.     unsigned short count = (unsigned short)fRegisteredObjects->GetCount();
  218.  
  219. //    fList->DelRow(0,0);            // delete the entire list
  220. //    fList->AddRow(count);    // make space for the cells
  221.  
  222.     //
  223.     // adjust the number of rows in the list
  224.     //
  225.     if (count > fList->GetLastRow())
  226.         fList->AddRow(count - fList->GetLastRow());
  227.     else if (count < fList->GetLastRow())
  228.         fList->  DelRow(fList->GetLastRow() - count, count);
  229.     
  230.     for (int row = 0; row < count; row++) {
  231.         fRegisteredObjects->GetSemaphore()->Grab();    // don't let list change on us
  232.         if (fRegisteredObjects->GetCount() > row) {    // make sure list has index "row"
  233.             char theData[256];
  234.             (*fRegisteredObjects)[row]->GetVerboseName(theData);    // get the text for the new cell
  235.             fRegisteredObjects->GetSemaphore()->Release();    // ok, it can change now
  236.             
  237.             Cell cell = {0,0};                // {row,0} won't work!!!
  238.             cell.v = row;                    // we'll add the cell to the end
  239.             fList->SetCell(theData,strlen(theData),cell);
  240.         }
  241.         else
  242.             fRegisteredObjects->GetSemaphore()->Release();
  243.     }
  244.  
  245.     // force the list to be updated
  246.  
  247.     fList->DoDraw(true);        // turn on list drawing
  248.     InvalRect(&(**fList->GetList()).rView);
  249.     SetPort(savedPort);
  250. }
  251.  
  252. /**********************************************************************
  253. ** PUBLIC GetListRect
  254. ***********************************************************************/
  255.  
  256. Rect TRegisteredObjectsWindow::GetListRect()
  257. {
  258.     return fList->GetListRect();
  259. }
  260.  
  261.  
  262. /**********************************************************************
  263. ** PUBLIC DrawGrowIcon
  264. ***********************************************************************/
  265.  
  266. void TRegisteredObjectsWindow::DrawGrowIcon()
  267. {
  268.     ::DrawGrowIcon(fDocWindow);
  269.     
  270.     /* get rid of extra "grow icon" section in lower left corner */
  271.     Rect r = fDocWindow->portRect;
  272.     r.top = r.bottom - 16;
  273.     r.bottom -=14;
  274.     r.right = r.left + kButtonSpace - 1;
  275.     EraseRect(&r);
  276. }
  277.  
  278. /**********************************************************************
  279. ** PUBLIC MoveWindow
  280. ***********************************************************************/
  281.  
  282. //  Move an application window. 
  283.  
  284. void TRegisteredObjectsWindow::MoveWindow(short hGlobal, short vGlobal)
  285. {
  286.     ::MoveWindow(fDocWindow, hGlobal, vGlobal, false);
  287. }
  288.  
  289. /**********************************************************************
  290. ** PUBLIC SizeWindow
  291. ***********************************************************************/
  292.  
  293. // Set the size of an application window. 
  294.  
  295. void TRegisteredObjectsWindow::SizeWindow(short w, short h)
  296. {
  297.     ::SizeWindow(fDocWindow, w, h, true);
  298.     fList->DoDraw(false);
  299.     fList->Size(w - 15 - kButtonSpace, h - 15);
  300.     fList->DoDraw(true);
  301.     DrawWindow();
  302. }
  303.  
  304. /**********************************************************************
  305. ** PUBLIC HomeWindow
  306. ***********************************************************************/
  307.  
  308. // Move the window to the "home" position so it can definitely be seen. 
  309.  
  310. void TRegisteredObjectsWindow::HomeWindow()
  311. {
  312.     MoveWindow(kWindowHStart, kWindowVStart);
  313.     SizeWindow(kWindowHSizeStart, kWindowVSizeStart);
  314. }
  315.  
  316. /**********************************************************************
  317. ** PUBLIC GetWindowSize
  318. ***********************************************************************/
  319.  
  320. //  Returns size of an application window. 
  321.  
  322. void TRegisteredObjectsWindow::GetWindowSize(short &w, short &h)
  323. {
  324.     w = fDocWindow->portRect.right - fDocWindow->portRect.left;
  325.     h = fDocWindow->portRect.bottom - fDocWindow->portRect.top;
  326. }
  327.  
  328. /**********************************************************************
  329. ** PUBLIC GetWindowLocation
  330. ***********************************************************************/
  331.  
  332. // Returns the location of an application window. 
  333.  
  334. void TRegisteredObjectsWindow::GetWindowLocation(short &hGlobal, short &vGlobal)
  335. {
  336.     hGlobal = - fDocWindow->portBits.bounds.left;
  337.     vGlobal = - fDocWindow->portBits.bounds.top;
  338. }
  339.  
  340. /**********************************************************************
  341. ** PUBLIC SaveWindowLocation
  342. ***********************************************************************/
  343.  
  344. void TRegisteredObjectsWindow::SaveWindowLocation()
  345. {
  346.     //
  347.     // save the window size and location in the prefs file
  348.     //
  349.     short prefsRefNum = fInspector->GetPrefsFileRefNum();
  350.     Handle windowLocationResource = NULL;
  351.     if (prefsRefNum != -1)
  352.         windowLocationResource = NewHandle(sizeof(WindowLocationRec));
  353.     if (windowLocationResource)
  354.     {
  355.         signed short    left, top;
  356.         signed short    width, height;
  357.  
  358.         GetWindowLocation(left, top);
  359.         GetWindowSize(width, height);
  360.         
  361.         HLock(windowLocationResource);
  362.         (**(WindowLocationRec**)windowLocationResource).left    = left;
  363.         (**(WindowLocationRec**)windowLocationResource).top        = top;
  364.         (**(WindowLocationRec**)windowLocationResource).width    = width;
  365.         (**(WindowLocationRec**)windowLocationResource).height    = height;
  366.         
  367.         short savedRefNum = CurResFile();
  368.         UseResFile(prefsRefNum);
  369.         char    className[255];
  370.         strcpy(className,fRegisteredObjects->GetClassName());
  371.         c2pstr(className);
  372.         Handle dupRes = Get1NamedResource('wloc', (ConstStr255Param)className);
  373.         if (dupRes)
  374.         {
  375.             RemoveResource(dupRes);
  376.             DisposeHandle(dupRes);
  377.         }
  378.         AddResource(windowLocationResource, 'wloc', UniqueID('wloc'),
  379.             (ConstStr255Param)className);
  380.         WriteResource(windowLocationResource);
  381.         UseResFile(savedRefNum);
  382.     }
  383. }
  384.  
  385. /**********************************************************************
  386. ** PUBLIC RestoreWindowLocation
  387. ***********************************************************************/
  388.  
  389. Boolean TRegisteredObjectsWindow::RestoreWindowLocation()
  390. {
  391.     Boolean result = false;        // true if relocation was actually done
  392.     short prefsRefNum = fInspector->GetPrefsFileRefNum();
  393.  
  394.     if (prefsRefNum)
  395.     {
  396.         signed short    left, top;
  397.         signed short    width, height;
  398.         
  399.         short savedRefNum = CurResFile();
  400.         UseResFile(prefsRefNum);
  401.         char    className[255];
  402.         strcpy(className,fRegisteredObjects->GetClassName());
  403.         c2pstr(className);
  404.         Handle windowLocationResource = Get1NamedResource('wloc', (ConstStr255Param)className);
  405.         if (windowLocationResource)
  406.         {
  407.             HLock(windowLocationResource);
  408.             left = (**(WindowLocationRec**)windowLocationResource).left;
  409.             top = (**(WindowLocationRec**)windowLocationResource).top;
  410.             width = (**(WindowLocationRec**)windowLocationResource).width;
  411.             height = (**(WindowLocationRec**)windowLocationResource).height;
  412.             ReleaseResource(windowLocationResource);
  413.             
  414.             MoveWindow(left, top);
  415.             SizeWindow(width, height);
  416.             
  417.             result = true;
  418.         }
  419.         UseResFile(savedRefNum);
  420.     }
  421.     
  422.     return result;
  423. }
  424.