home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / PP Balloon Help Support / CHelpAttach.cp < prev    next >
Encoding:
Text File  |  1996-08-15  |  6.5 KB  |  226 lines  |  [TEXT/CWIE]

  1. // Source for CHelpAttach class
  2.  
  3. #include "CHelpAttach.h"
  4. #include "CBalloonApp.h"
  5.  
  6. #include <LControl.h>
  7. #include <LPane.h>
  8. #include <LStream.h>
  9.  
  10. #include <UEnvironment.h>
  11.  
  12. // __________________________________________________________________________________________________
  13. // C L A S S __ C H E L P A T T A C H
  14. // __________________________________________________________________________________________________
  15.  
  16. LPane* CHelpAttach::sHelpPane = nil;
  17.  
  18. // C O N S T R U C T I O N / D E S T R U C T I O N  M E T H O D S
  19.  
  20. CHelpAttach* CHelpAttach::CreateHelpAttachStream(LStream *inStream)
  21. {
  22.     return new CHelpAttach(inStream);
  23. }
  24.  
  25. // Default constructor
  26. CHelpAttach::CHelpAttach(short strId, short index)
  27.         : LAttachment(msg_ShowHelp, true)
  28. {
  29.     // Check for availablity of Balloon Help
  30.     mHasBalloonHelp = UEnvironment::HasGestaltAttribute(gestaltHelpMgrAttr,gestaltHelpMgrPresent);
  31.  
  32.     // Cache the resource info
  33.     mStrId = strId;
  34.     mIndex = index;
  35. }
  36.  
  37. CHelpAttach::CHelpAttach(LStream *inStream)
  38.         : LAttachment(inStream)
  39. {
  40.     // Check for availablity of Balloon Help
  41.     mHasBalloonHelp = UEnvironment::HasGestaltAttribute(gestaltHelpMgrAttr,gestaltHelpMgrPresent);
  42.  
  43.     // Always force to our message
  44.     mMessage = msg_ShowHelp;
  45.  
  46.     // Cache the resource info
  47.     inStream->ReadData(&mStrId, sizeof(mStrId));
  48.     inStream->ReadData(&mIndex, sizeof(mIndex));
  49. }
  50.  
  51.  
  52. // O T H E R  M E T H O D S ____________________________________________________________________________
  53.  
  54. // Show help balloon
  55. void CHelpAttach::ExecuteSelf(MessageT inMessage, void *ioParam)
  56. {
  57.     // Only do if help is present and Balloons are on
  58.     if (!mHasBalloonHelp || !::HMGetBalloons())
  59.     {
  60.         sHelpPane = nil;
  61.         return;
  62.     }
  63.     
  64.     // Check for same pane and same balloon
  65.     if (((LPane*) ioParam == sHelpPane) && ::HMIsBalloon() && SameBalloon(ioParam))
  66.         return;
  67.  
  68.     // Remove any old balloon
  69.     if (::HMIsBalloon())
  70.         ::HMRemoveBalloon();
  71.     
  72.     // Fill in Help manager record
  73.     HMMessageRecord    aHelpMsg;
  74.     FillHMRecord(aHelpMsg, ioParam);
  75.     
  76.     // Get hot rect so that balloon help automatically removes the balloon if it
  77.     // moves out of the pane
  78.     Rect hotRect;
  79.     ((LPane*) ioParam)->CalcPortFrameRect(hotRect);
  80.     ((LPane*) ioParam)->PortToGlobalPoint(topLeft(hotRect));
  81.     ((LPane*) ioParam)->PortToGlobalPoint(botRight(hotRect));
  82.     
  83.     // Set tip of balloon to centre of pane
  84.     Point tip = {(hotRect.top + hotRect.bottom)/2,
  85.                     (hotRect.left + hotRect.right)/2};
  86.  
  87.     // Show the balloon
  88.     OSErr err =::HMShowBalloon(&aHelpMsg, tip, &hotRect, nil, 0, 0, kHMRegularWindow);
  89.  
  90.     // If there was an error set the cached pane to nil to force the balloon
  91.     // to be redrawn the next time AdjustCursor is called. This is neccessary because
  92.     // sometimes the mouse is moving too quick for help manager and balloon fails to draw.    
  93.     sHelpPane = (err ? nil : (LPane*) ioParam);
  94. }
  95.  
  96. // Fill in the HMMessageRecord
  97. void CHelpAttach::FillHMRecord(HMMessageRecord    &theHelpMsg, void *ioParam)
  98. {
  99.     // Fill in Help manager record
  100.     theHelpMsg.hmmHelpType = khmmStringRes;
  101.     theHelpMsg.u.hmmStringRes.hmmResID = mStrId;
  102.     theHelpMsg.u.hmmStringRes.hmmIndex = mIndex;
  103.     
  104.     mCurrentIndex = theHelpMsg.u.hmmStringRes.hmmIndex;
  105. }
  106.  
  107. // Will display same balloon?
  108. Boolean CHelpAttach::SameBalloon(void *ioParam)
  109. {
  110.     // Save for compare
  111.     short current = mCurrentIndex;
  112.     
  113.     // Find out what str index will be used now
  114.     HMMessageRecord    aHelpMsg;
  115.     FillHMRecord(aHelpMsg, ioParam);
  116.     
  117.     // Compare
  118.     return (mCurrentIndex == current);
  119. }
  120.  
  121. #pragma mark -
  122.  
  123. // __________________________________________________________________________________________________
  124. // C L A S S __ C H E L P P A N E A T T A C H
  125. // __________________________________________________________________________________________________
  126.  
  127. // C O N S T R U C T I O N / D E S T R U C T I O N  M E T H O D S
  128.  
  129. CHelpPaneAttach* CHelpPaneAttach::CreateHelpPaneAttachStream(LStream *inStream)
  130. {
  131.     return new CHelpPaneAttach(inStream);
  132. }
  133.  
  134. // Default constructor
  135. CHelpPaneAttach::CHelpPaneAttach(short strId, short index_enabled, short index_disabled)
  136.         : CHelpAttach(strId, index_enabled)
  137. {
  138.     // Cache the resource info
  139.     mEnabledIndex = index_enabled;
  140.     mDisabledIndex = index_disabled;
  141. }
  142.  
  143. CHelpPaneAttach::CHelpPaneAttach(LStream *inStream)
  144.         : CHelpAttach(inStream)
  145. {
  146.     // Cache the resource info
  147.     mEnabledIndex = mIndex;
  148.     inStream->ReadData(&mDisabledIndex, sizeof(mDisabledIndex));
  149. }
  150.  
  151.  
  152. // O T H E R  M E T H O D S ____________________________________________________________________________
  153.  
  154. // Fill in the HMMessageRecord
  155. void CHelpPaneAttach::FillHMRecord(HMMessageRecord    &theHelpMsg, void *ioParam)
  156. {
  157.     // Fill in Help manager record
  158.     theHelpMsg.hmmHelpType = khmmStringRes;
  159.     theHelpMsg.u.hmmStringRes.hmmResID = mStrId;
  160.     
  161.     // Select help string based on pane state
  162.     if (((LPane*) ioParam)->IsEnabled())
  163.         theHelpMsg.u.hmmStringRes.hmmIndex = mEnabledIndex;
  164.     else
  165.         theHelpMsg.u.hmmStringRes.hmmIndex = mDisabledIndex;
  166.     
  167.     
  168.     mCurrentIndex = theHelpMsg.u.hmmStringRes.hmmIndex;
  169. }
  170.  
  171. #pragma mark -
  172.  
  173. // __________________________________________________________________________________________________
  174. // C L A S S __ C H E L P P A N E A T T A C H
  175. // __________________________________________________________________________________________________
  176.  
  177. // C O N S T R U C T I O N / D E S T R U C T I O N  M E T H O D S
  178.  
  179. CHelpControlAttach* CHelpControlAttach::CreateHelpControlAttachStream(LStream *inStream)
  180. {
  181.     return new CHelpControlAttach(inStream);
  182. }
  183.  
  184. // Default constructor
  185. CHelpControlAttach::CHelpControlAttach(short strId, short index_enabled_on, short index_enabled_off, short index_disabled)
  186.         : CHelpAttach(strId, index_enabled_on)
  187. {
  188.     // Cache the resource info
  189.     mEnabledOnIndex = index_enabled_on;
  190.     mEnabledOffIndex = index_enabled_off;
  191.     mDisabledIndex = index_disabled;
  192. }
  193.  
  194. CHelpControlAttach::CHelpControlAttach(LStream *inStream)
  195.         : CHelpAttach(inStream)
  196. {
  197.     // Cache the resource info
  198.     mEnabledOnIndex = mIndex;
  199.     inStream->ReadData(&mEnabledOffIndex, sizeof(mEnabledOffIndex));
  200.     inStream->ReadData(&mDisabledIndex, sizeof(mDisabledIndex));
  201. }
  202.  
  203.  
  204. // O T H E R  M E T H O D S ____________________________________________________________________________
  205.  
  206. // Fill in the HMMessageRecord
  207. void CHelpControlAttach::FillHMRecord(HMMessageRecord    &theHelpMsg, void *ioParam)
  208. {
  209.     // Fill in Help manager record
  210.     theHelpMsg.hmmHelpType = khmmStringRes;
  211.     theHelpMsg.u.hmmStringRes.hmmResID = mStrId;
  212.     
  213.     // Select help string based on pane state
  214.     if (((LControl*) ioParam)->IsEnabled())
  215.     {
  216.         if (((LControl*) ioParam)->GetValue())
  217.             theHelpMsg.u.hmmStringRes.hmmIndex = mEnabledOnIndex;
  218.         else
  219.             theHelpMsg.u.hmmStringRes.hmmIndex = mEnabledOffIndex;
  220.     }
  221.     else
  222.         theHelpMsg.u.hmmStringRes.hmmIndex = mDisabledIndex;
  223.     
  224.     
  225.     mCurrentIndex = theHelpMsg.u.hmmStringRes.hmmIndex;
  226. }