home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / ISPINBT.HPP < prev    next >
C/C++ Source or Header  |  1993-10-22  |  27KB  |  504 lines

  1. #ifndef _ISPINBT_
  2.   #define _ISPINBT_
  3. /*******************************************************************************
  4. * FILE NAME: ispinbt.hpp                                                       *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the class(es):                                              *
  8. *     ISpinButton - This class creates and manages the spin button control     *
  9. *                   window.                                                    *
  10. *                                                                              *
  11. * COPYRIGHT:                                                                   *
  12. *   Licensed Materials - Property of IBM                                       *
  13. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  14. *   All Rights Reserved                                                        *
  15. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  16. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  17. *                                                                              *
  18. *******************************************************************************/
  19.  
  20. #ifndef _ICONTROL_
  21.   #include <icontrol.hpp>
  22. #endif
  23. #ifndef _IRECT_
  24.   #include <irect.hpp>
  25. #endif
  26. #ifndef _ISTRING_
  27.   #include <istring.hpp>
  28. #endif
  29.  
  30. class IColor;
  31. class IRange;
  32. class IResourceId;
  33.  
  34. /*----------------------------------------------------------------------------*/
  35. /* Align classes on four byte boundary.                                       */
  36. /*----------------------------------------------------------------------------*/
  37. #pragma pack(4)
  38.  
  39. /*----------------------------------------------------------------------------*/
  40. /* Turn off warning for compiler generated copy/assignment                    */
  41. /*----------------------------------------------------------------------------*/
  42. #pragma info(nocpy)
  43.  
  44. class ISpinButton : public IControl {
  45. typedef IControl 
  46.   Inherited;
  47. /*******************************************************************************
  48. * The ISpinButton class creates and manages the spin button control window.    *
  49. *******************************************************************************/
  50. public :
  51. /*----------------------------- Style ------------------------------------------
  52. | The valid spin button styles are:                                            |
  53. |   classDefaultStyle - The original default style for this class, which is    |
  54. |                       master | leftAlign | allCharacters | IWindow::visible. |
  55. |   master            - Specifies that the spin button component consists of:  |
  56. |                        - At least one single line entry field (SLE), or      |
  57. |                          spin field.                                         |
  58. |                        - Two arrows, the Up arrow and the Down arrow.        |
  59. |                       When a spin button contains more than one spin field,  |
  60. |                       the master component contains the spin arrows.  If     |
  61. |                       the component contains only one spin field, it must    |
  62. |                       be a master.                                           |
  63. |   servant           - Allows you to create a multiple field spin button by   |
  64. |                       spinning servants from the master.  This style         |
  65. |                       requires the setMaster function be called to define    |
  66. |                       the master spin field.                                 |
  67. |   leftAlign         - Makes the text left-justified.                         |
  68. |   centerAlign       - Centers the text.                                      |
  69. |   rightAlign        - Makes the text right-justified.                        |
  70. |   allCharacters     - Permits any character to be typed in the spin field.   |
  71. |   numericOnly       - Specifies that only digits between 0 and 9 and the     |
  72. |                       minus sign (-) can be typed in the spin field.         |
  73. |   noBorder          - Suppresses the drawing of a border.                    |
  74. |   readOnly          - Prevents entering input into the spin field.           |
  75. |   fastSpin          - Enables the spin button to increase the spin speed     |
  76. |                       with time.  The speed doubles every two seconds.       |
  77. |   padWithZeros      - Pads the displayed number with zeros at the front.     |
  78. |                                                                              |
  79. | The following functions provide a means of getting and setting the default   |
  80. | style for this class:                                                        |
  81. |   defaultStyle    - Returns the current default style.  This is the same as  |
  82. |                     classDefaultStyle unless setDefaultStyle has been        |
  83. |                     called.                                                  |
  84. |   setDefaultStyle - Sets the default style for all subsequent spin buttons.  |
  85. -------------------------------------------------------------------------------*/
  86. INESTEDBITFLAGCLASSDEF2(Style, ISpinButton, IWindow, IControl);
  87.  
  88. static const Style
  89.   classDefaultStyle,
  90.   master,
  91.   servant,
  92.   allCharacters,
  93.   numericOnly,
  94.   readOnly,
  95.   leftAlign,
  96.   centerAlign,
  97.   rightAlign,
  98.   noBorder,
  99.   fastSpin,
  100.   padWithZeros;
  101.  
  102. static Style
  103.    defaultStyle();
  104. static void
  105.    setDefaultStyle(const Style& style);
  106.  
  107. /*------------------------ Constructors ----------------------------------------
  108. | You can construct instances of this class in the following ways:             |
  109. |    - From a control ID, parent and owner windows, rectangle, and style.      |
  110. |      This creates the specified spin button control and an object for it.    |
  111. |    - From the ID of a spin button control on a dialog window.  This creates  |
  112. |      the object for the specified spin button control.                       |
  113. |    - From the window handle of an existing spin button control.  This        |
  114. |      creates the object for the specified spin button control.               |
  115. ------------------------------------------------------------------------------*/
  116.    ISpinButton ( unsigned long    id,
  117.                  IWindow*         parent,
  118.                  IWindow*         owner,
  119.                  const IRectangle &initial = IRectangle(),
  120.                  const Style      &style   = defaultStyle() );
  121.  
  122.    ISpinButton ( unsigned long id,
  123.                  IWindow*      parentDialog );
  124.  
  125.    ISpinButton ( const IWindowHandle& handle );
  126.  
  127. virtual
  128.   ~ISpinButton ( );
  129. /*---------------------------- Enumerator --------------------------------------
  130. | The following enumerations provide a means of setting the style of           |
  131. | instances of this class:                                                     |
  132. |   Type      - Enumeration used to define the types of values that can be     |
  133. |               entered in the spin button field.  The valid values are:       |
  134. |                 alphanumeric - Specifies the spin button field contains a    |
  135. |                                list of text.                                 |
  136. |                 numeric      - Specifies the spin button field contains a    |
  137. |                                range of numbers.                             |
  138. |                                                                              |
  139. |   Alignment - Enumeration used to align the text in the spin field.  The     |
  140. |               valid values are:                                              |
  141. |                 left   - Left-justifies the text in the spin field.          |
  142. |                 center - Centers the text in the spin field.                 |
  143. |                 right  - Right-justifies the text in the spin field.         |
  144. ------------------------------------------------------------------------------*/
  145. enum Type {
  146.   alphanumeric,
  147.   numeric
  148. };
  149.  
  150. enum Alignment {
  151.   left,
  152.   right,
  153.   center
  154. };
  155.  
  156. /*---------------------------- Style Functions ---------------------------------
  157. | The following functions provide a means to change the style of instances     |
  158. | of this class:                                                               |
  159. |   setReadOnly   - Disables input in the spin field.                          |
  160. |   setFastSpin   - Causes the spin button to increase the spin speed with     |
  161. |                   time.  The speed doubles every two seconds until the       |
  162. |                   upper or lower range is reached.  At this point, the       |
  163. |                   speed resets to the original speed and again doubles       |
  164. |                   every two seconds.  This member function should not be     |
  165. |                   invoked on a master spin button that has servants spun     |
  166. |                   from it.                                                   |
  167. |   setInputType  - Changes the permitted input type for the spin field.       |
  168. |                   NOTE: This function does not work in OS/2 Version 2.0.     |
  169. |   setAlignment  - Changes the text alignment in the spin field.              |
  170. |   enableBorder  - Draws a border.                                            |
  171. |   disableBorder - Suppresses the drawing of a border.                        |
  172. ------------------------------------------------------------------------------*/
  173. ISpinButton
  174.   &setReadOnly   ( Boolean            readOnly = true ),
  175.   &setFastSpin   ( Boolean            fast     = true ),
  176.   &setInputType  ( Type               aValue   = alphanumeric ),
  177.   &setAlignment  ( Alignment          aValue   = left ),
  178.   &enableBorder  ( Boolean            enable   = true ),
  179.   &disableBorder ( );
  180.  
  181. /*-------------------------- Style Testing -------------------------------------
  182. | The following functions provides a mean to test the style of instances of    |
  183. | this class:                                                                  |
  184. |   isMaster         - Returns true if the spin button is a master.            |
  185. |   isServant        - Returns true if the spin button is a servant.           |
  186. |   isReadOnly       - Returns true if the user is not permitted to type in    |
  187. |                      the spin field.                                         |
  188. |   isFastSpin       - Returns true if the spin speed is doubled every two     |
  189. |                      seconds.                                                |
  190. |   isBorder         - Returns true if the spin button has a border.           |
  191. |   alignment        - Returns the text alignment in the spin field.           |
  192. |   inputTypeAllowed - Returns the data type permitted in the spin field.      |
  193. ------------------------------------------------------------------------------*/
  194. Boolean
  195.   isMaster   ( ) const,
  196.   isServant  ( ) const, 
  197.   isReadOnly ( ) const, 
  198.   isFastSpin ( ) const, 
  199.   isBorder   ( ) const;
  200.  
  201. Alignment 
  202.   alignment  ( ) const;
  203.  
  204. Type
  205.   inputTypeAllowed ( ) const;
  206.  
  207. /*---------------------- ColorArea Enumerator ----------------------------------
  208. |   ColorArea - Enumeration that is used to replace the color of a             |
  209. |               particular region of the spin field.  The valid values are:    |
  210. |                 foreground          - Sets the color of the foreground text. |
  211. |                 disabledForeground  - Sets the foreground color for          |
  212. |                                       disabled text.                         |
  213. |                 highlightForeground - Sets the foreground color for          |
  214. |                                       selected text.                         |
  215. |                 border              - Sets the color of the border that      |
  216. |                                       surrounds the spin field, if any.      |
  217. ------------------------------------------------------------------------------*/
  218. enum ColorArea {
  219.   foreground,
  220.   disabledForeground,
  221.   highlightForeground,
  222.   border };
  223.  
  224. /*------------------------- Set Limit & Spin -----------------------------------
  225. | The following functions provide a means to manage the spin field of          |
  226. | instances of this class:                                                     |
  227. |   setMaster - Defines a servant spin button's master.  The spin button must  |
  228. |               be a servant, and the spin button passed as the master         |
  229. |               argument must be a master.                                     |
  230. |   setColor  - Changes the color of the specified spin field area.            |
  231. |   setLimit  - Sets the number of characters permitted in the spin field.     |
  232. |               The original value at construction is 255.                     |
  233. |               NOTE: Calling this function with a limit that does not allow   |
  234. |                     all numbers in the spin button range to be displayed     |
  235. |                     leads to erratic results when the button is spun.  For   |
  236. |                     example, if the range is 1 to 100 and the limit is set   |
  237. |                     to 2, the button will spin up to 99 and wrap to 10       |
  238. |                     instead of 1.  Spinning down, the button wraps from      |
  239. |                     1 to 10.  This is an OS/2 Version 2.0 problem.           |
  240. |   spinDown  - Spins the button down the specified number of times.           |
  241. |   spinUp    - Spins the button up the specified number of times.             |
  242. |   color     - Returns the color of the specific spin field area.             |
  243. |   isValid   - Returns true if the contents of the spin field are within the  |
  244. |               number range or matches one of the text values.  If the        |
  245. |               updateContents argument is set to true, the spin field will    |
  246. |               be updated as follows:                                         |
  247. |               - If the spin button contains a number range and the contents  |
  248. |                 of the spin field are out-of-range, the spin field is set    |
  249. |                 to the last valid value.                                     |
  250. |               - If the spin button contains text values and the contents of  |
  251. |                 the spin field matches a text value, but the case is         |
  252. |                 different, the contents of the spin field are set to match   |
  253. |                 the case of the spin button text.  If the values do not      |
  254. |                 match, the spin field is set to the last valid value.        |
  255. ------------------------------------------------------------------------------*/
  256. ISpinButton
  257.   &setMaster ( const ISpinButton& master ),
  258.   &setColor  ( ColorArea     value,
  259.                const IColor& color ),
  260.   &setLimit  ( unsigned long aNumber ),
  261.   &spinDown  ( unsigned long spinBy  = 1 ),
  262.   &spinUp    ( unsigned long spinBy  = 1 );
  263.  
  264. IColor
  265.   color ( ColorArea value ) const;
  266.  
  267. Boolean
  268.   isValid ( Boolean updateContents = false ) const;
  269.  
  270. /*------------------------- Number Range Operations ----------------------------
  271. | The following functions provides a means to manage the number range of       |
  272. | instances of this class.  These functions only apply to spin buttons that    |
  273. | have a number range:                                                         |
  274. |   setCurrent - Sets the spin field to the specified value.  This function    |
  275. |                is the same as spinTo.                                        |
  276. |   spinTo     - Sets the spin field to the specified value.  This function    |
  277. |                is the same as setCurrent.                                    |
  278. |   setRange   - Sets the lower and upper range of the spin button numbers.    |
  279. |                If the override argument is set to true and the current       |
  280. |                value in the spin field is outside the new range, the         |
  281. |                current value in the spin field will not change.              |
  282. |   range      - Returns the lower and upper range of the spin button.         |
  283. |                NOTE: This function causes a trap in OS/2 Version 2.0.        |
  284. |   value      - Returns the current value displayed in the spin field.        |
  285. |   setValue   - Sets the displayed value of the spin field, regardless of     |
  286. |                the validity of the number.                                   |
  287. ------------------------------------------------------------------------------*/
  288. ISpinButton
  289.   &setCurrent ( long aValue ),
  290.   &spinTo     ( long aValue ),
  291.   &setRange   ( const IRange& aNewRange,
  292.                 Boolean       override = false );
  293.  
  294. IRange
  295.   range ( ) const;
  296.  
  297. long
  298.   value ( ) const;
  299.  
  300. ISpinButton
  301.  &setValue ( long aValue );
  302.  
  303. /*-------------------- Text Query, Validate & Remove ---------------------------
  304. | The following functions provide the means to query the text contents of      |
  305. | the spin field and remove the contents of instances of this class.           |
  306. | These functions only apply to spin buttons with text:                        |
  307. |   text      - Returns the displayed contents of the spin field.              |
  308. |   setText   - Sets the displayed contents of the spin field, regardless of   |
  309. |               the validity of the text.  This does not alter the contents    |
  310. |               of the spin button array.                                      |
  311. |   removeAll - Removes all text in the spin button.                           |
  312. ------------------------------------------------------------------------------*/
  313. IString
  314.   text ( ) const;
  315.  
  316. ISpinButton
  317.   &setText    ( const char*        string ),
  318.   &setText    ( const IResourceId& residText ),
  319.   &removeAll  ( );
  320.  
  321. class Cursor : public IVBase {
  322. /*******************************************************************************
  323. * The ISpinButton::Cursor class creates and manages the cursor for a spin      *
  324. * button that has text.  Each cursor maintains a copy of the spin button text  *
  325. * on construction.  It is not necessary to use a cursor in a spin button that  *
  326. * has a number range.                                                          *
  327. *******************************************************************************/
  328. public:
  329. /*------------------ Constructor/Destructor ---------------------------------
  330. |  The only way to construct instances of this class is with an instance    |
  331. |  of a spin button.  The spin button text is copied into the cursor.       |
  332. ---------------------------------------------------------------------------*/
  333.   Cursor  ( const ISpinButton& spinButton );
  334.  
  335. virtual
  336.   ~Cursor ( );
  337.  
  338. /*------------------------ Cursor Movement ------------------------
  339. | The following functions provides the means to change the cursor |
  340. | position:                                                       |
  341. |   setToFirst    - Points to the first item in the text list.    |
  342. |   setToNext     - Points to the next item in the text list.  If |
  343. |                   there is none, it invalidates the cursor.     |
  344. |   setToPrevious - Points to the previous item in the text list. |
  345. |                   If there is none, it invalidates the cursor.  |
  346. |   setToLast     - Points to the last item in the text list.     |
  347. -----------------------------------------------------------------*/
  348. Boolean
  349.   setToFirst    ( ),
  350.   setToNext     ( ),
  351.   setToPrevious ( ),
  352.   setToLast     ( );
  353.  
  354. /*--------------- Cursor Validation -------------------------------
  355. | The following functions provide a means of checking the         |
  356. | validity, and invalidating the cursor:                          |
  357. |   isValid    - Queries whether this cursor points to a valid    |
  358. |                item.                                            |
  359. |   invalidate - Flags this cursor as invalid.                    |
  360. +----------------------------------------------------------------*/
  361. Boolean
  362.   isValid    ( ) const;
  363. void
  364.   invalidate ( );
  365.  
  366. private:  /*------------------- PRIVATE ------------------------*/
  367. friend class
  368.   ISpinButton;
  369. long
  370.   lCurrent;
  371. unsigned long
  372.   ulCurCount;
  373. const ISpinButton
  374.  &rSpBut;
  375. }; 
  376.  
  377. /*-------------------------- Text-List Operations ------------------------------
  378. | The following functions provides the means to manage the text array of       |
  379. | instances of this class.  These functions only apply to spin buttons that    |
  380. | have the allCharacters style.                                                |
  381. |                                                                              |
  382. |   elementAt   - Returns the string at the cursor position.                   |
  383. |   refreshText - Sets the local list of text in the instance to the real      |
  384. |                 spin button.  This function provides the ability to add,     |
  385. |                 remove, and replace multiple text entries before applying    |
  386. |                 the changes to the spin button.  This is recommended when    |
  387. |                 adding a large quantity of text.  Otherwise, if the          |
  388. |                 immediateUpdate argument is set to true on add, remove, and  |
  389. |                 replace functions, the spin button will be updated           |
  390. |                 immediately.                                                 |
  391. |   removeAt    - Removes the item at the cursor position and sets the cursor  |
  392. |                 to the item that precedes the removed item.                  |
  393. |   replaceAt   - Replaces the string at the cursor position.                  |
  394. |   add         - Adds a new item at the cursor location by pushing all the    |
  395. |                 following items after the cursor.                            |
  396. |   addAsFirst  - Adds the item as the first item and sets the cursor on it.   |
  397. |                 This function allows you to add an item to an empty text     |
  398. |                 ISpinButton object.  The only other way to add an item to    |
  399. |                 an empty text ISpinButton object is to use the addAsLast     |
  400. |                 function.                                                    |
  401. |   addAsLast   - Adds the item as the last item and sets the cursor on it.    |
  402. |                 This function allows you to add an item to an empty text     |
  403. |                 ISpinButton object.  The only other way to add an item to    |
  404. |                 an empty text ISpinButton object is to use the addAsFirst    |
  405. |                 function.                                                    |
  406. |   addAsNext   - Adds the item as the next item and sets the cursor on it.    |
  407. |   setCurrent  - Sets the spin field to the cursor position and calls the     |
  408. |                 refreshText member function to update the spin button.       |
  409. |                 This function is the same as spinTo.                         |
  410. |   spinTo      - Sets the spin field to the cursor position and calls the     |
  411. |                 refreshText member function to update the spin button.       |
  412. |                 This function is the same as setCurrent.                     |
  413. ------------------------------------------------------------------------------*/
  414. IString
  415.   elementAt   ( const Cursor& cursor ) const;
  416.  
  417. ISpinButton
  418.   &refreshText ( ),
  419.   &removeAt    ( Cursor&            cursor,
  420.                  Boolean            immediateUpdate = false ),
  421.   &replaceAt   ( const char*        newString,
  422.                  Cursor&            cursor,
  423.                  Boolean            immediateUpdate = false ),
  424.   &replaceAt   ( const IResourceId& resid,
  425.                  Cursor&            cursor,
  426.                  Boolean            immediateUpdate = false ),
  427.   &add         ( const char*        string,
  428.                  Cursor&            cursor,
  429.                  Boolean            immediateUpdate = false ),
  430.   &add         ( const IResourceId& resid,
  431.                  Cursor&            cursor,
  432.                  Boolean            immediateUpdate = false ),
  433.   &addAsNext   ( const char*        string,
  434.                  Cursor&            cursor,
  435.                  Boolean            immediateUpdate = false ),
  436.   &addAsNext   ( const IResourceId& resid,
  437.                  Cursor&            cursor,
  438.                  Boolean            immediateUpdate = false ),
  439.   &addAsFirst  ( const char*        string,
  440.                  Cursor&            object,
  441.                  Boolean            immediateUpdate = false ),
  442.   &addAsFirst  ( const IResourceId& resid,
  443.                  Cursor&            object,
  444.                  Boolean            immediateUpdate = false ),
  445.   &addAsLast   ( const char*        string,
  446.                  Cursor&            object,
  447.                  Boolean            immediateUpdate = false ),
  448.   &addAsLast   ( const IResourceId& resid,
  449.                  Cursor&            object,
  450.                  Boolean            immediateUpdate = false ),
  451.   &setCurrent  ( const Cursor&      cursor ),
  452.   &spinTo      ( const Cursor&      cursor );
  453.  
  454. /*--------------------------------- Override -----------------------------------
  455. | This function is overriden to ensure the control is redrawn correctly:       |
  456. |   enable - Enables the window to accept keyboard and mouse input.            |
  457. ------------------------------------------------------------------------------*/
  458. virtual ISpinButton
  459.  &enable ( Boolean enableControl = true );
  460.  
  461. protected:
  462. /*----------------------------- Layout Size ------------------------------------
  463. | calcMinimumSize - Returns the recommended minimum size of this spin button   |
  464. |                   control.  The size is based on the font and text limit.    |
  465. ------------------------------------------------------------------------------*/
  466. virtual ISize
  467.   calcMinimumSize() const;
  468.  
  469. private:
  470. /*--------------------------------- PRIVATE ----------------------------------*/
  471.   ISpinButton(const ISpinButton&);
  472.   ISpinButton& operator=(const ISpinButton&);
  473. unsigned long
  474.   limit ( ) const;
  475.  
  476. friend class
  477.   ISpinButton::Cursor;
  478. static Style
  479.   currentDefaultStyle;
  480. Boolean
  481.   bDelayedItems;
  482. IString
  483.   spinText;
  484. unsigned long
  485.   findStringIndex(const Cursor & cursor) const,
  486.   lCount,
  487.   textLimit;
  488. char** pList;
  489. };
  490.  
  491. INESTEDBITFLAGCLASSFUNCS( Style, ISpinButton);
  492.  
  493. #ifndef I_NO_INLINES
  494.   #include <ispinbt.inl>
  495. #endif
  496.  
  497. /*----------------------------------------------------------------------------*/
  498. /* Resume compiler default packing and warning messages.                      */
  499. /*----------------------------------------------------------------------------*/
  500. #pragma pack()
  501. #pragma info(restore)
  502.  
  503. #endif // _ISPINBT_
  504.