home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lstbx3.zip / xlistbox.cpp < prev    next >
Text File  |  1994-08-16  |  22KB  |  466 lines

  1. /*******************************************************************************
  2. * FILE NAME: xlistbox.cpp                                                      *
  3. *                                                                              *
  4. * DESCRIPTION:                                                                 *
  5. *   This file contains the implementation of classes/functions declared        *
  6. *   in xlistbox.hpp.                                                           *
  7. *                                                                              *
  8. *******************************************************************************/
  9. extern "C"
  10. {
  11.   #define INCL_WINLISTBOXES      // LIT_
  12.   #define INCL_WINSYS            // System values and Colors
  13.   #define INCL_WINWINDOWMGR      // WinQueryWindowULong
  14.   #define INCL_DOSMODULEMGR      // DosLoadModule
  15.   #include <os2.h>
  16.   #include "listbox.h"
  17. }
  18.  
  19. #ifndef _ISTRING_
  20.   #include <istring.hpp>
  21. #endif
  22. #ifndef _ITHREAD_
  23.   #include <ithread.hpp>
  24. #endif
  25. #ifndef _IEXCEPT_
  26.   #include <iexcept.hpp>
  27. #endif
  28.  
  29. #ifndef _XLISTBOX_
  30.   #include "xlistbox.hpp"
  31. #endif
  32.  
  33. /***************************************************************/
  34. /* Public list box styles.                                     */
  35. /***************************************************************/
  36. const ListBox32::ExtendedStyle
  37.   ListBox32::checkBox                  = LSXS_CHECKBOX,
  38.   ListBox32::classDefaultExtendedStyle = 0;
  39.  
  40. /***************************************************************/
  41. /* Default style for new objects (initial value)               */
  42. /***************************************************************/
  43. ListBox32::ExtendedStyle ListBox32::currentDefaultExtendedStyle = 0;
  44.  
  45.  
  46. /*------------------------------------------------------------------------------
  47. | ListBox32::ListBox32                                                         |
  48. |                                                                              |
  49. | Construct a ListBox32 on an IWindow.                                         |
  50. ------------------------------------------------------------------------------*/
  51. ListBox32::ListBox32( unsigned long ulId,
  52.                       IWindow *pParent,
  53.                       IWindow *pOwner,
  54.                       const IRectangle &rectInit,
  55.                       const Style &style,
  56.                       const ExtendedStyle &extendedStyle ) :
  57.            IListBox( createListBox( ulId, pParent, pOwner, rectInit,
  58.                                     style, extendedStyle ) ),
  59.                       ulExtendedStyle( extendedStyle.asUnsignedLong() ),
  60.                       bSoundSupported( 0 )
  61. {
  62.   initialize();
  63. }
  64.  
  65. /*------------------------------------------------------------------------------
  66. | ListBox32::ListBox32                                                         |
  67. |                                                                              |
  68. | Construct a ListBox32 on a dialog.                                           |
  69. ------------------------------------------------------------------------------*/
  70. ListBox32::ListBox32( unsigned long ulId, IWindow* parent ) :
  71.            IListBox( validate( WinWindowFromID(parent->handle(), ulId) ) ),
  72.                       bSoundSupported( 0 )
  73. {
  74.   initialize();
  75. }
  76.  
  77. /*------------------------------------------------------------------------------
  78. | ListBox32::ListBox32                                                         |
  79. |                                                                              |
  80. | Wrapper an existing ListBox.                                                 |
  81. ------------------------------------------------------------------------------*/
  82. ListBox32::ListBox32( const IWindowHandle &handle ) :
  83.            IListBox( validate( handle ) ),
  84.                       bSoundSupported( 0 )
  85. {
  86.   initialize();
  87. }
  88.  
  89. /*------------------------------------------------------------------------------
  90. | ListBox32::~ListBox32                                                        |
  91. |                                                                              |
  92. | Default destructor                                                           |
  93. ------------------------------------------------------------------------------*/
  94. ListBox32::~ListBox32()
  95. {
  96. }
  97.  
  98. /*------------------------------------------------------------------------------
  99. | ListBox32::createListBox                                                     |
  100. |                                                                              |
  101. | Static function that creates the 32-Bit list box.                            |
  102. ------------------------------------------------------------------------------*/
  103. IWindowHandle ListBox32::createListBox(unsigned long       ulId,
  104.                                        IWindow             *pParent,
  105.                                        IWindow             *pOwner,
  106.                                        const IRectangle    &rectInit,
  107.                                        const Style         &style,
  108.                                        const ExtendedStyle &extendedStyle)
  109. {
  110.   /***************************************************************/
  111.   /* Assertions on input parms                                   */
  112.   /***************************************************************/
  113.   IASSERTPARM(pParent!=0);
  114.  
  115.   /***************************************************************/
  116.   /* Register the 32-Bit list box class                          */
  117.   /***************************************************************/
  118.   if (!fRegisterListBox( IThread::current().anchorBlock() ))
  119.     ITHROWGUIERROR( "fRegisterListBox" );
  120.  
  121.   /***************************************************************/
  122.   /* Build the control data structure for the 32-Bit list box    */
  123.   /***************************************************************/
  124.   LISTBOXCDATA ctlData;
  125.   memset(&ctlData, 0, sizeof(LISTBOXCDATA));
  126.   ctlData.cb = sizeof(LISTBOXCDATA);
  127.   ctlData.ulVersion = LBV_110;
  128.   ctlData.vdata.lbcd1_1.flExtStyles = extendedStyle.asUnsignedLong();
  129.  
  130.   /***************************************************************/
  131.   /* Create the 32-Bit list box                                  */
  132.   /***************************************************************/
  133.   IWindowHandle hwndLB = WinCreateWindow( pParent->handle(),
  134.                                           "ListBoxWindow",
  135.                                           0,
  136.                                           style.asUnsignedLong(),
  137.                                           rectInit.left(),
  138.                                           rectInit.bottom(),
  139.                                           rectInit.width(),
  140.                                           rectInit.height(),
  141.                                           (pOwner==0) ? IWindowHandle(0) :
  142.                                                         pOwner->handle(),
  143.                                           HWND_TOP,
  144.                                           ulId,
  145.                                           &ctlData,
  146.                                           0 );
  147.   if (hwndLB == 0)
  148.   {
  149.     ITHROWGUIERROR(IString("WinCreateWindow: Id=") +
  150.                    IString(ulId) +
  151.                    IString(" Class=ListBoxWindow"));
  152.   }
  153.  
  154.   return( hwndLB );
  155. }
  156.  
  157. /*------------------------------------------------------------------------------
  158. | ListBox32::validate                                                          |
  159. |                                                                              |
  160. | Static function that validates the window handle.                            |
  161. ------------------------------------------------------------------------------*/
  162. IWindowHandle ListBox32::validate( const IWindowHandle &handle )
  163. {
  164.   /***************************************************************/
  165.   /* Verify the window handle                                    */
  166.   /***************************************************************/
  167.   IASSERTPARM( handle.isValid() );
  168.  
  169.   /***************************************************************/
  170.   /* Verify that the window class is "ListBoxWindow"             */
  171.   /***************************************************************/
  172.   IString className = IString( 0, 16 );
  173.   WinQueryClassName( handle, className.length()+1, className );
  174.   IASSERTSTATE( className == "ListBoxWindow" );
  175.  
  176.   return( handle );
  177. }
  178.  
  179. /*------------------------------------------------------------------------------
  180. | ListBox32::initialize                                                        |
  181. |                                                                              |
  182. | Perform initialization tasks that are common across the ctors.               |
  183. ------------------------------------------------------------------------------*/
  184. void ListBox32::initialize( )
  185. {
  186.   HMODULE hmod;
  187.  
  188.   /***************************************************************/
  189.   /* I know, I know, but this is the easiest way to determine    */
  190.   /* if the sound support is available.                          */
  191.   /***************************************************************/
  192.   if (!DosLoadModule(NULL, 0, "LBSnd", &hmod))
  193.     this->bSoundSupported = true;
  194. }
  195.  
  196. /*------------------------------------------------------------------------------
  197. | ListBox32::check                                                             |
  198. |                                                                              |
  199. | Set an item as checked in the list.                                          |
  200. ------------------------------------------------------------------------------*/
  201. ListBox32& ListBox32::check(unsigned long index, Boolean turnOn)
  202. {
  203.   IEventResult evt = handle().sendEvent(LMX_SETCHECK,
  204.                                         IEventParameter1(index),
  205.                                         IEventParameter2(turnOn));
  206.   if (!(evt.asUnsignedLong()))
  207.      ITHROWGUIERROR("LMX_SETCHECK");
  208.  
  209.   return( *this );
  210. };
  211.  
  212. /*------------------------------------------------------------------------------
  213. | ListBox32::unCheck                                                           |
  214. |                                                                              |
  215. | Set an item as unchecked in the list.                                        |
  216. ------------------------------------------------------------------------------*/
  217. ListBox32& ListBox32::unCheck(unsigned long index)
  218. {
  219.   check(index, false);
  220.   return( *this );
  221. };
  222.  
  223. /*------------------------------------------------------------------------------
  224. | ListBox32::isChecked                                                         |
  225. |                                                                              |
  226. | Query whether given item is checked.                                         |
  227. ------------------------------------------------------------------------------*/
  228. Boolean ListBox32::isChecked(unsigned long index) const
  229. {
  230.   IEventResult evt = handle().sendEvent(LMX_QUERYCHECK,
  231.                                         IEventParameter1(index-1),
  232.                                         IEventParameter2(0));
  233.   if ((evt.asUnsignedLong() != LIT_NONE) && (evt.asUnsignedLong() == index))
  234.     return( true );
  235.    else
  236.     return( false );
  237. }
  238.  
  239. /*------------------------------------------------------------------------------
  240. | ListBox32::checkAll                                                          |
  241. |                                                                              |
  242. | Sequentially check all items in list box.                                    |
  243. ------------------------------------------------------------------------------*/
  244. ListBox32& ListBox32::checkAll()
  245. {
  246.   int i, j;
  247.   for (i = 0, j = count(); i < j; i++)
  248.     check(i);
  249.  
  250.   return( *this );
  251. }
  252.  
  253. /*------------------------------------------------------------------------------
  254. | ListBox32::deselectAll                                                       |
  255. |                                                                              |
  256. | Set all items as unchecked.                                                  |
  257. ------------------------------------------------------------------------------*/
  258. ListBox32& ListBox32::unCheckAll()
  259. {
  260.   IEventResult evt = handle().sendEvent(LMX_SETCHECK,
  261.                                         IEventParameter1(LIT_NONE),
  262.                                         IEventParameter2(FALSE));
  263.   if (!(evt.asUnsignedLong()))
  264.     ITHROWGUIERROR("LMX_SETCHECK");
  265.  
  266.    return( *this );
  267. };
  268.  
  269. /*------------------------------------------------------------------------------
  270. | ListBox32::numberChecked                                                     |
  271. |                                                                              |
  272. | Return the number of checked items in the list.                              |
  273. ------------------------------------------------------------------------------*/
  274. unsigned long ListBox32::numberChecked() const
  275. {
  276.    unsigned long  workSelect, i;
  277.    workSelect = LIT_FIRST;
  278.    i = 0;
  279.  
  280.    for (i = 0; ((workSelect = handle().sendEvent(LMX_QUERYCHECK,
  281.                                                  IEventParameter1(workSelect),
  282.                                                  IEventParameter2(0)))
  283.                  != LIT_NONE); i++) {;}
  284.  
  285.    return( i );
  286. };
  287.  
  288. /*------------------------------------------------------------------------------
  289. | ListBox32::checkedItem                                                       |
  290. |                                                                              |
  291. | Return the index of the first checked item (or LIT_NONE).                    |
  292. ------------------------------------------------------------------------------*/
  293. long ListBox32::checkedItem() const
  294. {
  295.   IEventResult evt = handle().sendEvent(LMX_QUERYCHECK,
  296.                                         IEventParameter1(LIT_FIRST),
  297.                                         IEventParameter2(0));
  298.   return( (long)evt.asUnsignedLong() );
  299. };
  300.  
  301. /*------------------------------------------------------------------------------
  302. | ListBox32::isSound                                                           |
  303. |                                                                              |
  304. | Returns true if sound support is available.                                  |
  305. ------------------------------------------------------------------------------*/
  306. Boolean ListBox32::isSound() const
  307. {
  308.   return( this->bSoundSupported );
  309. }
  310.  
  311. /*------------------------------------------------------------------------------
  312. | ListBox32::soundEvent                                                        |
  313. |                                                                              |
  314. | Returns a wave file name for the event if it is set.                         |
  315. ------------------------------------------------------------------------------*/
  316. IString ListBox32::soundEvent(ClickEvent value) const
  317. {
  318.   /***************************************************************/
  319.   /* Throw an exception if sound is not supported on this system */
  320.   /***************************************************************/
  321.   IASSERTSTATE( isSound() == true );
  322.  
  323.   /***************************************************************/
  324.   /* Get the sound event                                         */
  325.   /***************************************************************/
  326.   unsigned long ulClick = (value == singleClick)  ?  LSND_SINGLECLICK
  327.                                                   :  LSND_DOUBLECLICK;
  328.   IString waveFile = IString( 0, CCHMAXPATH );
  329.  
  330.   IEventResult evt = handle().sendEvent(LMXM_QUERYSOUNDEVENT,
  331.                                         IEventParameter1(ulClick),
  332.                                         IEventParameter2((char *)waveFile));
  333.   if (!evt.asUnsignedLong());
  334.     ITHROWGUIERROR( "LMXM_QUERYSOUNDEVENT" );
  335.  
  336.   return( waveFile );
  337. }
  338.  
  339. /*------------------------------------------------------------------------------
  340. | ListBox32::setSoundEvent                                                     |
  341. |                                                                              |
  342. | Sets a wave file name for the event.                                         |
  343. ------------------------------------------------------------------------------*/
  344. ListBox32& ListBox32::setSoundEvent(ClickEvent value, const char *pszWaveFile)
  345. {
  346.   /***************************************************************/
  347.   /* Throw an exception if sound is not supported on this system */
  348.   /***************************************************************/
  349.   IASSERTSTATE( isSound() == true );
  350.  
  351.   unsigned long ulClick = (value == singleClick)  ?  LSND_SINGLECLICK
  352.                                                   :  LSND_DOUBLECLICK;
  353.  
  354.   /***************************************************************/
  355.   /* Set the sound event                                         */
  356.   /***************************************************************/
  357.   handle().sendEvent(LMXM_SETSOUNDEVENT,
  358.                      IEventParameter1(ulClick),
  359.                      IEventParameter2((char *)pszWaveFile));
  360.   return( *this );
  361. }
  362.  
  363. /*------------------------------------------------------------------------------
  364. | ListBox32::isCheckBox                                                        |
  365. ------------------------------------------------------------------------------*/
  366. Boolean ListBox32::isCheckBox() const
  367. {
  368.   if (extendedStyle() & LSXS_CHECKBOX)
  369.     return( true );
  370.   else
  371.     return( false );
  372. }
  373.  
  374. /*------------------------------------------------------------------------------
  375. | ListBox32::enableCheckBox                                                    |
  376. ------------------------------------------------------------------------------*/
  377. ListBox32& ListBox32::enableCheckBox(Boolean bTurnOn)
  378. {
  379.   unsigned long ulExtStyle = extendedStyle();
  380.   unsigned long ulOldStyle = ulExtStyle;
  381.  
  382.   if (bTurnOn)
  383.     ulExtStyle |= LSXS_CHECKBOX;
  384.   else
  385.     ulExtStyle &= ~LSXS_CHECKBOX;
  386.  
  387.   if (ulOldStyle != ulExtStyle)
  388.   {
  389.     setExtendedStyle(ulExtStyle);
  390.     refresh();
  391.   }
  392.   return( *this );
  393. }
  394.  
  395. /*------------------------------------------------------------------------------
  396. | ListBox32::disableCheckBox                                                   |
  397. ------------------------------------------------------------------------------*/
  398. ListBox32& ListBox32::disableCheckBox()
  399. {
  400.   enableCheckBox(false);
  401.   return( *this );
  402. }
  403.  
  404. /*------------------------------------------------------------------------------
  405. | ListBox32::defaultExtendedStyle                                              |
  406. ------------------------------------------------------------------------------*/
  407. ListBox32::ExtendedStyle ListBox32::defaultExtendedStyle()
  408. {
  409.   return currentDefaultExtendedStyle;
  410. }
  411.  
  412. /*------------------------------------------------------------------------------
  413. | ListBox32::setDefaultExtendedStyle                                           |
  414. ------------------------------------------------------------------------------*/
  415. void ListBox32::setDefaultExtendedStyle(ExtendedStyle extendedStyle)
  416. {
  417.   currentDefaultExtendedStyle = extendedStyle;
  418. }
  419.  
  420. /*------------------------------------------------------------------------------
  421. | ListBox32::calcMinimumSize                                                   |
  422. |                                                                              |
  423. | Returns the recommended minimum size of the 32-Bit list box control.  The    |
  424. | size is based on the text string length of the longest string (if not using  |
  425. | IListBox::horizontalScroll style) and the current font.                      |
  426. ------------------------------------------------------------------------------*/
  427. ISize ListBox32::calcMinimumSize() const
  428. {
  429.   /***************************************************************/
  430.   /* Use default if horizontal scrolling style is set            */
  431.   /***************************************************************/
  432.   if (style() & IListBox::horizontalScroll.asUnsignedLong())
  433.     return( Inherited::calcMinimumSize() );
  434.  
  435.   /***************************************************************/
  436.   /* Calculate the smallest possible size required to display    */
  437.   /* one item in the list box.                                   */
  438.   /***************************************************************/
  439.   IEventResult evt = handle().sendEvent(LMX_CALCSIZE,
  440.                                         IEventParameter1(1),
  441.                                         IEventParameter2(0));
  442.   return( ISize(evt.lowNumber(), evt.highNumber()) );
  443. }
  444.  
  445. /*------------------------------------------------------------------------------
  446. | ListBox32::extendedStyle                                                     |
  447. |                                                                              |
  448. | Returns the extended style ...                                               |
  449. ------------------------------------------------------------------------------*/
  450. unsigned long ListBox32::extendedStyle( ) const
  451. {
  452.   return( this->ulExtendedStyle );
  453. }
  454.  
  455. /*------------------------------------------------------------------------------
  456. | ListBox32::setExtendedStyle                                                  |
  457. |                                                                              |
  458. | Set the extended style ...                                                   |
  459. ------------------------------------------------------------------------------*/
  460. ListBox32& ListBox32::setExtendedStyle( const unsigned long ulExt )
  461. {
  462.   this->ulExtendedStyle = ulExt;
  463.   return( *this );
  464. }
  465.  
  466.