home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / Clock / Sources / Part.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  8.6 KB  |  327 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Part.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef PART_H
  11. #include "Part.h"
  12. #endif
  13.  
  14. #ifndef DEFINES_K
  15. #include "Defines.k"
  16. #endif
  17.  
  18. #ifndef BINDING_K
  19. #include "Binding.k"
  20. #endif
  21.  
  22. #ifndef FRAME_H
  23. #include "Frame.h"
  24. #endif
  25.  
  26. #ifndef CONTENT_H
  27. #include "Content.h"
  28. #endif
  29.  
  30. // ----- Framework Layer -----
  31.  
  32. #ifndef FWPRTITE_H
  33. #include "FWPrtIte.h"
  34. #endif
  35.  
  36. #ifndef FWABOUT_H
  37. #include "FWAbout.h"
  38. #endif
  39.  
  40. #ifndef FWITERS_H
  41. #include "FWIters.h"
  42. #endif
  43.  
  44. #ifndef FWIDLE_H
  45. #include "FWIdle.h"
  46. #endif
  47.  
  48. // ----- OS Layer -----
  49.  
  50. #ifndef FWMENU_H
  51. #include "FWMenu.h"
  52. #endif
  53.  
  54. #ifndef FWODMISC_H
  55. #include "FWODMisc.h"
  56. #endif
  57.  
  58. #ifndef FWEVENT_H
  59. #include "FWEvent.h"
  60. #endif
  61.  
  62. #ifndef FWCFMRES_H
  63. #include "FWCFMRes.h"
  64. #endif
  65.  
  66. // ----- Foundation Layer -----
  67.  
  68. #ifndef FWMEMMGR_H
  69. #include "FWMemMgr.h"
  70. #endif
  71.  
  72. // ----- OpenDoc Includes -----
  73.  
  74. #ifndef SOM_Module_OpenDoc_StdProps_defined
  75. #include <StdProps.xh>
  76. #endif
  77.  
  78. // ----- Macintosh Includes -----
  79.  
  80. #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
  81. #include <Sound.h>
  82. #endif
  83.  
  84. //========================================================================================
  85. // Runtime information
  86. //========================================================================================
  87.  
  88. #ifdef FW_BUILD_MAC
  89. #pragma segment odfclock
  90. #endif
  91.  
  92. //========================================================================================
  93. //    Globals
  94. //========================================================================================
  95.  
  96. //========================================================================================
  97. // Constants
  98. //========================================================================================
  99.  
  100. const short kClockIdleFreq = 15;
  101.  
  102. //========================================================================================
  103. //    Class CClockPart
  104. //========================================================================================
  105.  
  106. FW_DEFINE_AUTO(CClockPart)
  107.     
  108. //----------------------------------------------------------------------------------------
  109. // CClockPart::CClockPart
  110. //----------------------------------------------------------------------------------------
  111. CClockPart::CClockPart(ODPart* odPart) :
  112.     FW_CPart(odPart, FW_gInstance, kPartInfoID),
  113.     fLastTime(FW_CTime::GetCurrentTime()),
  114.     fClockContent(NULL),
  115.     fLockIdle(FALSE),
  116.     fIdler(NULL)
  117. #ifdef FW_BUILD_MAC
  118.     ,fChimeSound(NULL),
  119.     fTickSound(NULL)
  120. #endif
  121. {
  122. }
  123.  
  124. //----------------------------------------------------------------------------------------
  125. // CClockPart::~CClockPart
  126. //----------------------------------------------------------------------------------------
  127. CClockPart::~CClockPart()
  128. {
  129.     delete fIdler;
  130.     
  131. #ifdef FW_BUILD_MAC
  132.     if (fChimeSound)
  133.         FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)fChimeSound);
  134.     fChimeSound = NULL;
  135.  
  136.     if (fTickSound)
  137.         FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)fTickSound);
  138.     fTickSound = NULL;
  139. #endif
  140. }
  141.  
  142. //----------------------------------------------------------------------------------------
  143. // CClockPart::Initialize
  144. //----------------------------------------------------------------------------------------
  145.  
  146. void CClockPart::Initialize(Environment* ev)
  147. {
  148.     FW_CPart::Initialize(ev);
  149.     
  150.     // ----- Register Presentation -----
  151.     fPresentation = RegisterPresentation(ev, "Apple:Presentation:ODFClock", TRUE);
  152.     
  153.     // ----- Initialize my menu -----
  154.     // We instantiate a FW_CToggleItem from a stream so we must 
  155.     // keep it from being deadstripped.
  156.     FW_DO_NOT_DEAD_STRIP(FW_CToggleItem);
  157.     GetMenuBar(ev)->InitializeFromResource(ev, kMenuBar);
  158.  
  159. #ifdef FW_BUILD_MAC    
  160.     FW_CSharedLibraryResourceFile resFile(ev);
  161.     
  162.     // ----- Get default clock sounds
  163.     fChimeSound = ::GetResource(soundListRsrc, kClockChime);
  164.     ::DetachResource(fChimeSound);
  165.     
  166.     fTickSound = ::GetResource(soundListRsrc, kClockTick);
  167.     ::DetachResource(fTickSound);
  168. #endif
  169.  
  170.     // ----- Create the Idler -----
  171.     fIdler = FW_NEW(FW_CIdler, (this, kClockIdleFreq));
  172.     fIdler->RegisterIdle(ev);
  173. }
  174.  
  175. //------------------------------------------------------------------------------
  176. // CClockPart::NewPartContent
  177. //------------------------------------------------------------------------------
  178.  
  179. FW_CContent* CClockPart::NewPartContent(Environment* ev)
  180. {
  181.     fClockContent = FW_NEW(CClockContent, (ev, this));
  182.     return fClockContent;
  183. }
  184.  
  185. //----------------------------------------------------------------------------------------
  186. // CClockPart::NewFrame
  187. //----------------------------------------------------------------------------------------
  188.  
  189. FW_CFrame* CClockPart::NewFrame(Environment* ev, ODFrame* odFrame, FW_CPresentation* presentation, FW_Boolean fromStorage)
  190. {
  191.     FW_UNUSED(fromStorage);
  192.  
  193.     return FW_NEW(CClockFrame, (ev, odFrame, presentation, this, fClockContent));
  194. }
  195.  
  196. //----------------------------------------------------------------------------------------
  197. // CClockPart::DoIdle
  198. //----------------------------------------------------------------------------------------
  199.  
  200. FW_Boolean CClockPart::DoIdle(Environment* ev, const FW_CNullEvent& theNullEvent)
  201. {
  202.     FW_UNUSED(theNullEvent);
  203.  
  204.     if (fLockIdle)
  205.         return TRUE;
  206.         
  207.     FW_CTime currentTime = FW_CTime::GetCurrentTime();
  208.      if (fLastTime != currentTime)
  209.     {
  210.         short frameCount = 0;
  211.         
  212.         // Update all the display frames of the same type
  213.         FW_CPresentationFrameIterator iter(ev, fPresentation);
  214.         for (CClockFrame *clockFrame = (CClockFrame*)iter.First(ev); iter.IsNotComplete(ev); clockFrame = (CClockFrame*)iter.Next(ev))
  215.         {
  216.             if ((!clockFrame->IsInLimbo(ev)))
  217.             {
  218.                 frameCount++;
  219.                 if (clockFrame->GetViewType(ev) == FW_CPart::gViewAsFrameToken)
  220.                     clockFrame->UpdateClock(ev, currentTime);
  221.             }
  222.         }
  223.  
  224.         if (frameCount > 0)
  225.         {
  226.             if (fClockContent->HasTickSound())
  227.                 this->PlayTickSound();
  228.             if (fClockContent->HasChimeSound() && (currentTime.GetMinute() == 0) && (currentTime.GetSecond() == 0))
  229.                 this->PlayChimeSound();
  230.         }
  231.         
  232.         fLastTime = currentTime;
  233.     }
  234.     return TRUE;
  235. }
  236.  
  237. //------------------------------------------------------------------------------
  238. // CClockPart::DoMenu
  239. //------------------------------------------------------------------------------
  240.  
  241. FW_Boolean CClockPart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  242. {
  243.     FW_Boolean menuHandled = TRUE;
  244.     
  245.     switch (theMenuEvent.GetCommandID(ev))
  246.     {
  247.         case kODCommandAbout:
  248.                 ::FW_About(ev, this, kAbout);
  249.                 break;
  250.         case cClockType:
  251.                 {
  252.                     fLockIdle = TRUE;
  253.                     fClockContent->SetClockType(fClockContent->GetClockType() == kAnalogClock ? kDigitalClock : kAnalogClock);
  254.                     
  255.                     fPresentation->ContentUpdated(ev);
  256.                     
  257.                     FW_CPresentationFrameIterator iter(ev, fPresentation);
  258.                     for (CClockFrame* frame = (CClockFrame*)iter.First(ev); iter.IsNotComplete(ev); frame = (CClockFrame*)iter.Next(ev))
  259.                     {
  260.                         frame->ChangeClockType(ev, fClockContent->GetClockType());                
  261.                     }
  262.                     fLockIdle = FALSE;
  263.                     this->Changed(ev);    // document has changed (enable Save menu item)
  264.                 }
  265.                 break;
  266.                 
  267.         case cClockSoundsTick:
  268.                 fClockContent->ToggleTickSound();
  269.                 this->Changed(ev);
  270.                 break;
  271.  
  272.         case cClockSoundsChime:
  273.                 fClockContent->ToggleChimeSound();
  274.                 this->Changed(ev);
  275.                 break;
  276.  
  277.         default:
  278.             menuHandled = FALSE;
  279.     }            
  280.     
  281.     return menuHandled;
  282. }
  283.  
  284. //----------------------------------------------------------------------------------------
  285. // CClockPart::DoAdjustMenus
  286. //----------------------------------------------------------------------------------------
  287.  
  288. FW_Boolean CClockPart::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar, FW_Boolean hasMenuFocus, FW_Boolean isRoot)
  289. {
  290.     if (hasMenuFocus)
  291.     {    
  292.         menuBar->EnableAndToggleCommand(ev, cClockType, TRUE, fClockContent->GetClockType() != kAnalogClock);    
  293.         menuBar->EnableAndCheckCommand(ev, cClockSoundsTick, TRUE, fClockContent->HasTickSound());
  294.         menuBar->EnableAndCheckCommand(ev, cClockSoundsChime, TRUE, fClockContent->HasChimeSound());
  295.     }
  296.         
  297.     return FALSE;
  298. }
  299.  
  300. //----------------------------------------------------------------------------------------
  301. // CClockPart::PlayTickSound
  302. //----------------------------------------------------------------------------------------
  303.  
  304. void CClockPart::PlayTickSound()
  305. {
  306. #ifdef FW_BUILD_MAC
  307.     ::SndPlay(NULL, (SndListHandle)fTickSound, TRUE);
  308. #endif
  309. #ifdef FW_BUILD_WIN
  310.     ::MessageBeep(-1);
  311. #endif
  312. }
  313.  
  314. //----------------------------------------------------------------------------------------
  315. // CClockPart::PlayChimeSound
  316. //----------------------------------------------------------------------------------------
  317.  
  318. void CClockPart::PlayChimeSound()
  319. {
  320. #ifdef FW_BUILD_MAC
  321.     ::SndPlay(NULL, (SndListHandle)fChimeSound, TRUE);
  322. #endif
  323. #ifdef FW_BUILD_WIN
  324.     ::MessageBeep(-1);
  325. #endif
  326. }
  327.