home *** CD-ROM | disk | FTP | other *** search
/ Mac Expert 1995 Winter / Mac Expert - Winter 95.iso / Les fichiers / Communications / Divers / DinkClass ƒ / DinkClass / DApplication.c < prev    next >
Encoding:
Text File  |  1992-12-31  |  5.9 KB  |  291 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        DApplication.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Mark Gross
  7.  
  8.     Copyright:    © 1992 by Applied Technical Software, all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <6>    12/31/92    MTG        making the code conditionaly compiled so         that I am
  13.                                     always working with a current         version in either think c
  14.                                     or MPW C++
  15.          <5>    11/14/92    MTG        Bringing the C++ version up to date WRT the ThinkC version.
  16.          <4>     9/20/92    MTG        Bringing the C++version up to date with the THINK C VERSION
  17.          <3>      8/9/92    MTG        merging in changes from the ThinkC version
  18.          <2>      8/8/92    MTG        fixing warnings
  19.  
  20.     To Do:
  21. */
  22.  
  23. // This is the Class definition of the DApplication class
  24. //
  25.  
  26. #include "DApplication.h"
  27. #include "DEventHandler.h"
  28. #include "DDocument.h"
  29.  
  30.  
  31. DApplication::DApplication(void)
  32. {
  33.     fIterator  = new DIterator;
  34.  
  35. }
  36.  
  37. DApplication::~DApplication(void)
  38. {
  39.     delete fIterator;
  40. }
  41.  
  42.  
  43.  
  44. //Set up the Handler lists and set up the members to safe values
  45.  
  46. Boolean    DApplication::InitApp(void)
  47. {
  48.     Handle    menuBar;
  49.     
  50.     fEventHandlers = new DList;
  51.     fDeadHandlers = new DList;
  52.     fEventHandlers->Init();
  53.     fDeadHandlers->Init();
  54.     fSleepVal = SLEEPVAL;
  55.     fDone = FALSE;
  56.  
  57.     fTarget = this;
  58.     fClipData = NULL;
  59.     GetClipFromSystem();
  60.     fInBackground = false;
  61.     fCursorRgn = NewRgn();
  62.     
  63.     menuBar = GetNewMBar(rMenuBarID);
  64.     SetMenuBar(menuBar);
  65.     DisposHandle(menuBar);
  66.     AddResMenu(GetMHandle(rAppleMenu), 'DRVR');
  67.     DrawMenuBar();
  68.  
  69.     return TRUE;     // I know that it never will return fals
  70.                     // but, I think subclasses may need to have
  71.                     // the option of returning FALSE.
  72. }// end of member function InitApp
  73.  
  74.  
  75. // Add all the event handlers to the fDeadHandlers by telling them all to 
  76. // kill them selves then Flush 'em!  This method smoked out a lot of side effect
  77. // problems with the event handler maintenance implemented in DinkClass, Its been
  78. // fixed.  If your interested look at the comments in the 
  79. // DApplication::FlushDeadHandlers method.
  80.  
  81. Boolean DApplication::CleanUp(void)
  82. {
  83.     DEventHandler *nextHandler;
  84.     DIterator *iterator;
  85.         // I'm using an alocated iterator in this function because IT
  86.         // calls functions which result in calls to functions wich use
  87.         // the DApplications fIterator (which messes up its use in THIS
  88.         // function.)
  89.     
  90.     iterator = new DIterator;
  91.     iterator->Init(fEventHandlers);
  92.     
  93.     while( nextHandler = (DEventHandler *)iterator->GetCurrentThenIncrement() )
  94.     {
  95.         nextHandler->KillMeNext();
  96.     }    
  97.  
  98.     FlushDeadHandlers();
  99.     delete iterator;
  100.         
  101.     return fDone;// if user canceles out of closing a window then 
  102.                 // fDone is rest to FALSE within DDocument::WindowClosed.
  103.         
  104. }// end of CleanUp method...
  105.  
  106.  
  107. //
  108. // The following section of code is the deffintions of the fuctions
  109. // used in support of the DDocument handling modle used in this class 
  110. // library. 
  111. //
  112.  
  113. DDocument* DApplication::MakeDDoc(Boolean OpenFromFile )
  114. {
  115.     DDocument *New;
  116.     
  117.     New = new(DDocument);
  118.             // if OpenFromFile == FALSE then it will 
  119.             // not read from any file
  120.     if(New->Init(OpenFromFile)) 
  121.     {
  122.         if ( fTarget = New->MakeWindow(HasColorQD()) )
  123.             ((DWindow *) fTarget)->SetWindowTitle();// everything went ok, so set the title
  124.                 // I'm having the App tell 
  125.                 // the doc to make the window because I don't 
  126.                 // like haveing one init function do too much stuff.  
  127.                 // Its a reliability thing. BTW FALSE is for no colorQD
  128.         else
  129.             fTarget = New; // window not created
  130.     }
  131.     else
  132.     {
  133.         fTarget = this;
  134.         SelectWindow( FrontWindow());
  135.     }
  136.     return New;
  137.     
  138. }//end member function MakeDDoc
  139.  
  140.  
  141.  
  142. //
  143. // DApplication overides of some of the DEventHandler subclass methods
  144. //
  145.  
  146. void DApplication::HandleMenuChoice(short menuID, short menuItem)
  147. {
  148.     short    itemHit;
  149.     Str255    daName;
  150.     short    daRefNum;
  151.  
  152.     if( menuID == rAppleMenu )
  153.     {
  154.         switch (menuItem)
  155.         {
  156.             case iAbout:
  157.                 itemHit = Alert(rAboutIDBox, NULL);
  158.                 break;
  159.             default:
  160.                 GetItem(GetMHandle(rAppleMenu), menuItem, daName);
  161.                 daRefNum = OpenDeskAcc( daName);
  162.                 break;
  163.         }
  164.     }
  165.                 
  166.     if( menuID == rFileMenu)
  167.     {
  168.         switch (menuItem)
  169.         {
  170.             case iNew:
  171.                 MakeDDoc(FALSE);
  172.                 break;
  173.                 
  174.             case iOpen:
  175.                 MakeDDoc(TRUE);
  176.                 break;
  177.                 
  178.             case iQuit:
  179.                 fDone = TRUE;
  180.                 break;
  181.                 
  182.             default:
  183.                 break;
  184.                 
  185.         }// end switch on menuItem
  186.     }// end if fileMenu
  187.     
  188.     inherited::HandleMenuChoice( menuID, menuItem);
  189. }
  190.             
  191.  
  192. void DApplication::SetUpMenues(void)
  193. {
  194.     MenuHandle    menu;
  195.     
  196.     menu = GetMHandle(rFileMenu);
  197.  
  198.     EnableMenuItem( menu, iNew, TRUE);
  199.     //EnableMenuItem( menu, iOpen, TRUE);
  200.     EnableMenuItem( menu, iQuit, TRUE);
  201.     
  202.     inherited::SetUpMenues( );
  203.     
  204. }
  205.  
  206.  
  207.  
  208. // Return the number of types int the typeList and stuff the fMainFileType
  209. // in the first item.  
  210.  
  211. int DApplication::GetFileType(OSType *typeList)
  212. {
  213.     *typeList = fMainFileType;
  214.     return 1;
  215. }
  216.  
  217.  
  218. void DApplication::GiveClipToSystem(void)
  219. {
  220.     long    result, size;
  221.     
  222.     if(fClipData != NULL)
  223.     {
  224.         result = ZeroScrap();
  225.         if(result != noErr)
  226.             return;
  227.         
  228.         size = GetHandleSize(fClipData);
  229.         HLock(fClipData);
  230.         result = PutScrap(size, fClipType, *fClipData);
  231.         HUnlock(fClipData);
  232.     }    
  233. }// end of GiveClipToSystem member fuction
  234.  
  235.  
  236.  
  237. void    DApplication::GetClipFromSystem(void)
  238. {
  239.     long    offset, result;
  240.     Handle     newData;
  241.     
  242.     newData = NewHandle(0); // Make an initial zero leangth handle
  243.                             // it will be sized by GetScrap as needed.
  244.  
  245.     result = GetScrap(newData, fClipType, &offset);
  246.     if (result > 0)
  247.     {
  248.         if(fClipData != NULL)
  249.             DisposHandle(fClipData);
  250.         SetHandleSize(newData, result);
  251.         fClipData = newData;
  252.     }
  253. }// end of GetClipFromSystem member function
  254.  
  255.  
  256. Handle    DApplication::GetClipCopyFromApp(OSType *type)
  257. {
  258.     Handle tempHandle;
  259.     OSErr err;
  260.     
  261.     *type = fClipType;    
  262.     tempHandle = fClipData;
  263.     err = HandToHand(&tempHandle);
  264.     if(err != noErr)
  265.         EnterMBStr("failed on the HandToHand copy");
  266.  
  267.     return tempHandle;
  268. }
  269.  
  270. void    DApplication::GiveDataToApp(Handle data, OSType type)
  271. {
  272.     Handle tempHandle;
  273.     OSErr err;
  274.  
  275.     fClipType = type;
  276.     if (fClipData)
  277.     {
  278.         DisposHandle(fClipData);
  279.     }
  280.     tempHandle = data;
  281.     err = HandToHand(&tempHandle);
  282.     if(err != noErr)
  283.         EnterMBStr("failed on the HandToHand copy");
  284.     
  285.     fClipData = tempHandle;
  286.     
  287. }
  288.  
  289.  
  290.  
  291.