home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Clock / Sources / ClockPar.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  9.7 KB  |  331 lines  |  [TEXT/MPS ]

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