home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / HCmdButtonAttachment 1.1.1 / HCmdButtonAttachment.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-20  |  14.9 KB  |  485 lines  |  [TEXT/CWIE]

  1. /*******************************************************************************\
  2. |                                                                                |
  3. |    HCmdButtonAttachment.h         ©1997 John C. Daub.  All rights reserved.        |
  4. |                                                                                |
  5. |    See the file "HCmdButtonAttachment README" for full details, instructions,    |
  6. |    changes, licensing agreement, etc.  Due to the important information        |
  7. |    included in that file, if you did not receive a copy of it, please contact    |
  8. |    the author for a copy immediately, before using this code.                    |
  9. |                                                                                |
  10. |    John C. Daub                        <mailto:hsoi@eden.com>                    |
  11. |    <http://www.eden.com/~hsoi/>        <http://www.eden.com/~hsoi/prog.html>    |
  12. |                                                                                |
  13. \*******************************************************************************/
  14.  
  15. #ifndef _H_HCmdButtonAttachment
  16. #define _H_HCmdButtonAttachment
  17. #pragma once
  18.  
  19. #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__)
  20.     #pragma import on
  21. #endif
  22.  
  23. #include <PP_Prefix.h>
  24. #include <PP_Messages.h>
  25. #include <LAttachment.h>
  26. #include <LPeriodical.h>
  27. #include <LString.h>
  28. #include <LStream.h>
  29. #include <LControl.h>
  30.  
  31. #ifndef __TYPES__
  32. #include <Types.h>
  33. #endif
  34.  
  35.  
  36.  
  37. class HCmdButtonAttachment : public LAttachment,
  38.                                 public LPeriodical {
  39.  
  40. public:
  41.  
  42.             enum { class_ID = 'HCmd' };
  43.         
  44.     #if ( __PowerPlant__ < 0x01608000 ) // version 1.6/CW11
  45.         static    HCmdButtonAttachment*    CreateFromStream( LStream *inStream );
  46.     #endif
  47.         
  48.                                     HCmdButtonAttachment(     PaneIDT        inPaneID,
  49.                                                             Uchar        inSpecifiedKey = 0,
  50.                                                             Uint32        inDelay = 0,
  51.                                                             Boolean        inDrawShortcut = true,
  52.                                                             MessageT    inMessage = msg_KeyPress,
  53.                                                             Boolean        inExecuteHost = true );
  54.                                     HCmdButtonAttachment( LStream        *inStream );
  55.         virtual                        ~HCmdButtonAttachment();
  56.  
  57.         virtual    void                SpendTime( const EventRecord &inMacEvent);    
  58.  
  59.         virtual    Boolean                IsCommandKeyPressed();    
  60.         virtual    void                MungeTitle( Uchar inHotKey );
  61.         virtual    Uchar                FindHotKey( Boolean inForceUpper = true );
  62.  
  63.  
  64. protected:
  65.                 
  66.         virtual void                ExecuteSelf(MessageT inMessage, void *ioParam);
  67.         
  68.         virtual    LControl*            GetControl() const;
  69.         virtual    void                SetControl( LControl *inControl );
  70.         
  71.         virtual    PaneIDT                GetControlPaneID() const;
  72.         virtual    void                SetControlPaneID( const PaneIDT inControlPaneID );
  73.         
  74.         virtual    Boolean                GetDrawShortcut() const;
  75.         virtual    void                SetDrawShortcut( const Boolean inDrawShortcut );
  76.         
  77.         virtual    Boolean                GetUseSpecifiedKey() const;
  78.         virtual    Boolean                GetUseFirstTitleChar() const;
  79.         virtual    void                SetUseSpecifiedKey( const Boolean inUseSpecifiedKey );
  80.         virtual    void                SetUseFirstTitleChar( const Boolean inUseFirstTitleChar );
  81.  
  82.         virtual    Uchar                GetHotKey() const;
  83.         virtual    void                SetHotKey( const Uchar inHotKey );
  84.         
  85.         virtual    Boolean                GetShowKeyEquiv() const;
  86.         virtual    void                SetShowKeyEquiv( const Boolean inShowKeyEquiv );
  87.         
  88.         virtual    void                GetOriginalTitle( Str255 outOriginalTitle );
  89.         virtual    void                SetOriginalTitle( ConstStr255Param inTitle );
  90.         
  91.         virtual    void                GetMungedTitle( Str255 outMungedTitle );
  92.         virtual    void                SetMungedTitle( ConstStr255Param inTitle );
  93.         
  94.         virtual    Uint32                GetWhenToDraw() const;
  95.         virtual    void                SetWhenToDraw( const Uint32 inWhenToDraw );
  96.         virtual    void                SetWhenToDraw();
  97.         
  98.         virtual    Uint32                GetDelay() const;
  99.         virtual    void                SetDelay( const Uint32 inDelay );
  100.     
  101. private:
  102.  
  103.                 void                InitCmdButtonAttachment();
  104.             
  105.         // defensive programming.  No copy constructor, no assignment operator
  106.         // and no default constructor (since LAttachment doesn't have them)
  107.         
  108.                                     HCmdButtonAttachment();
  109.                                     HCmdButtonAttachment( const HCmdButtonAttachment &inOriginal);
  110.             HCmdButtonAttachment&    operator=( const HCmdButtonAttachment &inOriginal );
  111.  
  112.                 LControl            *mControl;            // pointer to our control
  113.                 PaneIDT                mControlID;            // pane ID of the control
  114.                 Boolean                mDrawShortcut;        // draw the shortcut or not
  115.                 Boolean                mUseSpecifiedKey;    // true = use specified key
  116.                                                         // false = use first char of descriptor
  117.  
  118.                 Uchar                mSpecifiedKey;        // user selected hot key
  119.                 Boolean                mFlipped;            // showing the cmd equivs or not?
  120.  
  121.                 LStr255                mOriginalTitle;        // original control title
  122.                 LStr255                mMungedTitle;        // modified control title
  123.             
  124.                 Uint32                mWhenToDraw;        // buffers our count for delay drawing
  125.                 Uint32                mDelay;                // how many ticks to wait before we
  126.                                                         // display the cmd equiv
  127.             
  128.  
  129. };    
  130.  
  131.  
  132. //=============================================================================
  133. //        ••• Inlines
  134. //=============================================================================
  135.  
  136. //=============================================================================
  137. //    • GetControl                            [protected, virtual]
  138. //=============================================================================
  139. //    Return a pointer to the LControl on which we function
  140.  
  141. inline
  142. LControl*
  143. HCmdButtonAttachment::GetControl() const
  144. {
  145.     return mControl;
  146. }
  147.  
  148.  
  149. //=============================================================================
  150. //    • SetControl                            [protected, virtual]
  151. //=============================================================================
  152. //    Set the control on which we function. nil can be a valid value
  153.  
  154. inline
  155. void
  156. HCmdButtonAttachment::SetControl(
  157.     LControl *inControl )
  158. {
  159.     mControl = inControl;
  160. }
  161.  
  162.  
  163. //=============================================================================
  164. //    • GetControlPaneID                        [protected, virtual]
  165. //=============================================================================
  166. //    Return the PaneIDT of our control
  167.  
  168. inline
  169. PaneIDT
  170. HCmdButtonAttachment::GetControlPaneID() const
  171. {
  172.     // even if we don't have a control (mControl == nil), we should still
  173.     // have a PaneIDT for it since all ctors require this be specified
  174.     
  175.     return mControlID;
  176. }
  177.  
  178.  
  179. //=============================================================================
  180. //    • SetControlPaneID                        [protected, virtual]
  181. //=============================================================================
  182. //    Set the PaneIDT of our control. Doesn't load the control, create it, get
  183. //     pointer to it. Just establishes the PaneIDT within the object.
  184.  
  185. inline
  186. void
  187. HCmdButtonAttachment::SetControlPaneID(
  188.     const PaneIDT inControlPaneID )
  189. {
  190.     mControlID = inControlPaneID;
  191. }
  192.  
  193.  
  194. //=============================================================================
  195. //    • GetDrawShortcut                        [protected, virtual]
  196. //=============================================================================
  197. //    Return true if we will be drawing the shortcut in the control's title
  198.  
  199. inline
  200. Boolean
  201. HCmdButtonAttachment::GetDrawShortcut() const
  202. {
  203.     return mDrawShortcut;
  204. }
  205.  
  206.  
  207. //=============================================================================
  208. //    • SetDrawShortcut                        [protected, virtual]
  209. //=============================================================================
  210. //    Set if we will be drawing the shortcut in the control's title or not
  211.  
  212. inline
  213. void
  214. HCmdButtonAttachment::SetDrawShortcut(
  215.     const Boolean inDrawShortcut )
  216. {
  217.     mDrawShortcut = inDrawShortcut;
  218. }
  219.  
  220.  
  221. //=============================================================================
  222. //    • GetUseSpecifiedKey                    [protected, virtual]
  223. //=============================================================================
  224. //    Returns true if we are to use the user-specified key. False, then we
  225. //    would utilize the first character of the control's descriptor (title).
  226.  
  227. inline
  228. Boolean
  229. HCmdButtonAttachment::GetUseSpecifiedKey() const
  230. {
  231.     return mUseSpecifiedKey;
  232. }
  233.  
  234. // As a convenience, we'll offer the converse
  235.  
  236. inline
  237. Boolean
  238. HCmdButtonAttachment::GetUseFirstTitleChar() const
  239. {
  240.     return !mUseSpecifiedKey;
  241. }
  242.  
  243.  
  244. //=============================================================================
  245. //    • SetUseSpecifiedKey                    [protected, virtual]
  246. //=============================================================================
  247. //    Set if we use a user specified key (true) or to use the first character
  248. //    of the control's descriptor (false)
  249.  
  250. inline
  251. void
  252. HCmdButtonAttachment::SetUseSpecifiedKey(
  253.     const Boolean inUseSpecifiedKey )
  254. {
  255.     mUseSpecifiedKey = inUseSpecifiedKey;
  256. }
  257.  
  258. // As a convenience, we'll offer the converse
  259.  
  260. inline
  261. void
  262. HCmdButtonAttachment::SetUseFirstTitleChar(
  263.     const Boolean inUseFirstTitleChar )
  264. {
  265.     mUseSpecifiedKey = !inUseFirstTitleChar;
  266. }
  267.  
  268.  
  269. //=============================================================================
  270. //    • GetHotKey                                [protected, virtual]
  271. //=============================================================================
  272. //    Return our hot-key. Even if it's not being used (i.e. they're using the
  273. //    first char of the descriptor), we'll still return something (zero should
  274. //    mean that we're not using a hot-key).
  275.  
  276. inline
  277. Uchar
  278. HCmdButtonAttachment::GetHotKey() const
  279. {
  280.     return mSpecifiedKey;
  281. }
  282.  
  283.  
  284. //=============================================================================
  285. //    • SetHotKey                                [protected, virtual]
  286. //=============================================================================
  287. //    Establish our (user specified) hot-key: e.g. "N" for "New", "O" for "Open".
  288. //    We'll assume if you specify something, that you want to use it, so we'll
  289. //    set data accordingly. However if you wish to "clear" this and use the
  290. //    first character of the descriptor, then pass zero.
  291.  
  292. inline
  293. void
  294. HCmdButtonAttachment::SetHotKey(
  295.     const Uchar inHotKey )
  296. {
  297.     mSpecifiedKey = inHotKey;
  298.     
  299.     SetUseSpecifiedKey( (Boolean)inHotKey );
  300. }
  301.  
  302.  
  303. //=============================================================================
  304. //    • GetShowKeyEquiv                        [protected, virtual]
  305. //=============================================================================
  306. //    Return true if we will display the keyboard shortcut, false if not
  307.  
  308. inline
  309. Boolean
  310. HCmdButtonAttachment::GetShowKeyEquiv() const
  311. {
  312.     return mFlipped;
  313. }
  314.  
  315.  
  316. //=============================================================================
  317. //    • SetShowKeyEquiv                        [protected, virtual]
  318. //=============================================================================
  319. //    Pass true if you wish to display the keyboard shortcut with the control.
  320. //    Be careful however... the way we display the shortcut is to modify the
  321. //    control's descriptor. If the control doesn't have a descriptor (e.g. icon
  322. //    button), then we could be in trouble. You can still use HCmdButtonAttachment
  323. //    for non-descriptor-having controls, just can't print the key equiv.
  324.  
  325. inline
  326. void
  327. HCmdButtonAttachment::SetShowKeyEquiv(
  328.     const Boolean inShowKeyEquiv )
  329. {
  330.     mFlipped = inShowKeyEquiv;
  331. }
  332.  
  333.  
  334. //=============================================================================
  335. //    • GetOriginalTitle                        [protected, virtual]
  336. //=============================================================================
  337. //    Return the original, unmagled title (should be the descriptor of the control)
  338.  
  339. inline
  340. void
  341. HCmdButtonAttachment::GetOriginalTitle(
  342.     Str255 outOriginalTitle )
  343. {
  344.     LString::CopyPStr(mOriginalTitle, outOriginalTitle );
  345. }
  346.  
  347.  
  348. //=============================================================================
  349. //    • SetOriginalTitle                        [protected, virtual]
  350. //=============================================================================
  351. //    Set the original title (i.e. title displayed when modifier keys are not
  352. //    depressed) to the given Pascal-style string.
  353.  
  354. inline
  355. void
  356. HCmdButtonAttachment::SetOriginalTitle(
  357.     ConstStr255Param inTitle )
  358. {
  359.     mOriginalTitle = inTitle;
  360. }
  361.  
  362.  
  363. //=============================================================================
  364. //    • GetMungedTitle                        [protected, virtual]
  365. //=============================================================================
  366. //    Return the munged title (title with the cmd-key shortcut added to it)
  367.  
  368. inline
  369. void
  370. HCmdButtonAttachment::GetMungedTitle(
  371.     Str255 outMungedTitle )
  372. {
  373.     LString::CopyPStr(mMungedTitle, outMungedTitle);
  374. }
  375.  
  376.  
  377. //=============================================================================
  378. //    • SetMungedTitle                        [protected, virtual]
  379. //=============================================================================
  380. //    Set the munged title (i.e. control's title + modifier keys)
  381.  
  382. inline
  383. void
  384. HCmdButtonAttachment::SetMungedTitle(
  385.     ConstStr255Param inTitle )
  386. {
  387.     mMungedTitle = inTitle;
  388. }
  389.  
  390.  
  391. //=============================================================================
  392. //    The following four methods: Get/SetWhenToDraw, Get/SetDelay are related
  393. //    in purpose. HCmdButtonAttachment allows one to specify if the control
  394. //    should display the keyboard shortcut or not (nice for letting your users
  395. //    know it's there as well as what it is). But as a cosmetic addition, one
  396. //    can delay the time until this shortcut is displayed... i.e. wait a second
  397. //    and the show it (can help to reduce flicker and such for power users that
  398. //    just bounce everywhere quickly,  or just make you look cool ;-)
  399. //
  400. //    So these 4 methods help in this regard. WhenToDraw establishes when we
  401. //    actually are to draw, in a value compared to ::TickCount(). When
  402. //    ::TickCount() is greater than or equal to our "WhenToDraw" value, then
  403. //    we shall display the keyboard shortcut. The Delay is how many ticks we
  404. //    are to wait from time of key depression until we display the shortcut.
  405. //    And the modifier key must be held down for that total length of time...
  406. //    letting go of the key resets the counter.
  407. //=============================================================================
  408.  
  409. //=============================================================================
  410. //    • GetDelay                                [protected, virtual]
  411. //=============================================================================
  412. //    Return a number (in ticks) that represents how much time we wait before
  413. //    we display the keyboard shortcut (after the modifier key is depressed and
  414. //    held down)
  415.  
  416. inline
  417. Uint32
  418. HCmdButtonAttachment::GetDelay() const
  419. {
  420.     return mDelay;
  421. }
  422.  
  423.  
  424. //=============================================================================
  425. //    • SetDelay                                [protected, virtual]
  426. //=============================================================================
  427. //    Establish the amount of time (in ticks) that we are to wait before drawing
  428. //    the keyboard shortcut
  429.  
  430. inline
  431. void
  432. HCmdButtonAttachment::SetDelay(
  433.     const Uint32 inDelay )
  434. {
  435.     mDelay = inDelay;
  436. }
  437.  
  438.  
  439. //=============================================================================
  440. //    • GetWhenToDraw                            [protected, virtual]
  441. //=============================================================================
  442. //    Return a number (in ticks, and compared against ::TickCount()) that
  443. //    says when we are to draw/display the key shortcut.
  444.  
  445. inline
  446. Uint32
  447. HCmdButtonAttachment::GetWhenToDraw() const
  448. {
  449.     return mWhenToDraw;
  450. }
  451.  
  452.  
  453. //=============================================================================
  454. //    • SetWhenToDraw                            [protected, virtual]
  455. //=============================================================================
  456. //    Set the time (in ticks) to draw at.
  457.  
  458. inline
  459. void
  460. HCmdButtonAttachment::SetWhenToDraw(
  461.     const Uint32 inWhenToDraw )
  462. {
  463.     mWhenToDraw = inWhenToDraw;
  464. }
  465.  
  466. //    Often when to draw will be just a function of ::TickCount() + GetDelay()
  467. //    so we'll provide a no-parameter variant of this routine that does just
  468. //    that (saves you a step)
  469.  
  470. inline
  471. void
  472. HCmdButtonAttachment::SetWhenToDraw()
  473. {
  474.     mWhenToDraw = ::TickCount() + GetDelay();
  475. }
  476.  
  477.  
  478.  
  479.  
  480.  
  481. #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__)
  482.     #pragma import reset
  483. #endif
  484.  
  485. #endif // #ifndef _H_HCmdButtonAttachment