home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / hpp.z / WLISTBOX.HPP < prev    next >
C/C++ Source or Header  |  1996-12-03  |  17KB  |  479 lines

  1. /*************************************************************************
  2.  *
  3.  * WListBox -- Wrapper for the Windows 95 Listbox control.
  4.  *
  5.  *   Events:
  6.  *
  7.  *       BeginOwnerDraw -- Called before the first DrawItem.
  8.  *
  9.  *       DataAvailable --
  10.  *
  11.  *       DataClose --
  12.  *
  13.  *       DataOpen --
  14.  *
  15.  *       DataRequest --
  16.  *
  17.  *       DoubleClick -- When a listbox item has been double-clicked
  18.  *
  19.  *       DrawItem -- Called for owner-draw.
  20.  *
  21.  *       EndOwnerDraw -- Called after last DrawItem.
  22.  *
  23.  *       Select -- When a listbox item has been selected
  24.  *
  25.  *************************************************************************/
  26.  
  27. #ifndef _WLISTBOX_HPP_INCLUDED
  28. #define _WLISTBOX_HPP_INCLUDED
  29.  
  30. #ifndef _WNO_PRAGMA_PUSH
  31. #pragma pack(push,8);
  32. #pragma enum int;
  33. #endif
  34.  
  35. #ifndef _WCONTROL_HPP_INCLUDED
  36. #  include "wcontrol.hpp"
  37. #endif
  38. #ifndef _WSTRING_HPP_INCLUDED
  39. #  include "wstring.hpp"
  40. #endif
  41. #ifndef _WDATATRG_HPP_INCLUDED
  42. #  include "wdatatrg.hpp"
  43. #endif
  44. #ifndef _WLOCALE_HPP_INCLUDED
  45. #  include "wlocale.hpp"
  46. #endif
  47. #ifndef _WDRAWITM_HPP_INCLUDED
  48. #  include "wdrawitm.hpp"
  49. #endif
  50.  
  51. class WDataSource;
  52.  
  53. //
  54. // ListBox styles
  55. //
  56.  
  57. #define WLBSDefault           ((WStyle)0x50A00141L) // WS_VISIBLE|LBS_HASSTRINGS|LBS_NOTIFY|WS_BORDER|WS_VSCROLL|WS_CHILD|LBS_NOINTEGRALHEIGHT
  58.  
  59. #define WLBSSingleSel         ((WStyle)0x00000000L) // WS_OVERLAPPED
  60. #define WLBSNotify            ((WStyle)0x00000001L) // LBS_NOTIFY
  61. #define WLBSSort              ((WStyle)0x00000002L) // LBS_SORT
  62. #define WLBSNoRedraw          ((WStyle)0x00000004L) // LBS_NOREDRAW
  63. #define WLBSMultipleSel       ((WStyle)0x00000008L) // LBS_MULTIPLESEL
  64. #define WLBSOwnerDrawFixed    ((WStyle)0x00000010L) // LBS_OWNERDRAWFIXED
  65. #define WLBSOwnerDrawVariable ((WStyle)0x00000020L) // LBS_OWNERDRAWVARIABLE
  66. #define WLBSHasStrings        ((WStyle)0x00000040L) // LBS_HASSTRINGS
  67. #define WLBSUseTabStops       ((WStyle)0x00000080L) // LBS_USETABSTOPS
  68. #define WLBSNoIntegralHeight  ((WStyle)0x00000100L) // LBS_NOINTEGRALHEIGHT
  69. #define WLBSMultiColumn       ((WStyle)0x00000200L) // LBS_MULTICOLUMN
  70. #define WLBSWantKeyboardInput ((WStyle)0x00000400L) // LBS_WANTKEYBOARDINPUT
  71. #define WLBSExtendedSel       ((WStyle)0x00000800L) // LBS_EXTENDEDSEL
  72. #define WLBSDisableNoScroll   ((WStyle)0x00001000L) // LBS_DISABLENOSCROLL
  73. #define WLBSNoData            ((WStyle)0x00002000L) // LBS_NODATA
  74. #define WLBSNoSel             ((WStyle)0x00004000L) // LBS_NOSEL
  75.  
  76. #define WLBSSorted            WLBSSort
  77. #define WLBSMultipleSelection WLBSMultipleSel
  78. #define WLBSNoSelection       WLBSNoSel
  79.  
  80.  
  81. class WCMCLASS WListBox : public WControl {
  82.     WDeclareSubclass( WListBox, WControl );
  83.  
  84.     public:
  85.         /**************************************************************
  86.          * Constructors and destructors
  87.          **************************************************************/
  88.  
  89.         WListBox();
  90.  
  91.         ~WListBox();
  92.  
  93.         /**************************************************************
  94.          * Properties
  95.          **************************************************************/
  96.  
  97.         // ColumnWidth
  98.         //
  99.         //     Use to set the width in pixels of the columns in a
  100.         //     multi-column listbox.  A value of -1 means no width
  101.         //     has been set.
  102.  
  103.         WInt  GetColumnWidth() const;
  104.         WBool SetColumnWidth( WInt width );
  105.  
  106.         // Count
  107.         //
  108.         //     Returns the number of items in the list box, or negative
  109.         //     if an error occurs.
  110.  
  111.         WInt GetCount() const;
  112.  
  113.         // DataBindAsLookup
  114.         //
  115.         //     DataTarget property
  116.  
  117.         WBool GetDataBindAsLookup() const;
  118.         WBool SetDataBindAsLookup( WBool bindAsLookup );
  119.  
  120.         // DataColumns
  121.         //
  122.         //     DataTarget property
  123.  
  124.         WString GetDataColumns() const;
  125.         WBool   SetDataColumns( const WString & cols );
  126.  
  127.         // DataLookupColumns
  128.         //
  129.         //     DataTarget property
  130.  
  131.         WString GetDataLookupColumns() const;
  132.         WBool   SetDataLookupColumns( const WString & cols );
  133.  
  134.         // DataLookupSource
  135.         //
  136.         //     DataTarget property
  137.  
  138.         WDataSource *GetDataLookupSource() const;
  139.         WBool        SetDataLookupSource( WDataSource *source );
  140.  
  141.         // DataSource
  142.         //
  143.         //     DataTarget property
  144.  
  145.         WDataSource *GetDataSource() const;
  146.         WBool        SetDataSource( WDataSource *source );
  147.  
  148.         // DataTrackRow
  149.         //
  150.         //     DataTarget method
  151.  
  152.         WBool GetDataTrackRow() const;
  153.         WBool SetDataTrackRow( WBool trackRow );
  154.  
  155.         // HorizontalExtent
  156.         //
  157.         //     The width, in pixels, by which a listbox can be scrolled
  158.         //     horizontally (the scrollable width).  If the value is set
  159.         //     to -1, the horizontal extent will be calculated automatically
  160.         //     on a one-time only basis based on the strings currently in
  161.         //     the listbox.
  162.  
  163.         WBool SetHorizontalExtent( WInt horizontalExtent );
  164.         WInt  GetHorizontalExtent() const;
  165.  
  166.         // FDXDataSource
  167.  
  168.         WBool SetFDXDataSource( WStringArray * fdxDataSource );
  169.         WBool SetFDXDataSource( WIntArray * _fdxDataSourceIntArray );
  170.         WBool SetFDXDataSource( WInt * fdxDataSource );
  171.         WBool SetFDXDataSource( WBool * fdxDataSource );
  172.  
  173.         // Selected
  174.         //
  175.         //     For single-select listboxes, GetSelected() returns the index
  176.         //     of the currently-selected item, or -1 if no selection,
  177.         //     and SetSelected sets the current selection.  SetSelected
  178.         //     of -1 clears the selection.
  179.         //
  180.         //     For multi-select listboxes, use GetSelected( WInt ) to
  181.         //     determine if a specific index is selected.  Use 
  182.         //     SetSelected to select or deselect an item, or use index
  183.         //     -1 to select or deselect all items.
  184.         //     Note: The multi-select versions of GetSelected and
  185.         //           SetSelected are Item Properties, not Properties.
  186.  
  187.         WInt  GetSelected() const;
  188.         WBool SetSelected( WInt index );
  189.  
  190.         // SelectedCount
  191.         //
  192.         //     Returns the number of items selected in a multi-selection
  193.         //     listbox, or -1 if an error occurs.
  194.  
  195.         WInt GetSelectedCount() const;
  196.  
  197.         // SelectedList
  198.         //
  199.         //     An array of indices of selected items.  If no items
  200.         //     are selected, the array size is zero.
  201.  
  202.         WIntArray GetSelectedList() const;
  203.  
  204.         // Sort
  205.         //
  206.         //     Get/set the sort mode.
  207.  
  208.         WBool GetSort() const;
  209.  
  210.         WBool SetSort( WBool sort );
  211.         // Locale (for type of sort)
  212.         //
  213.         //
  214.         WBool           SetLocale( WLocaleID locID );
  215.         WLocaleID       GetLocale();
  216.  
  217.         // TopIndex
  218.         //
  219.         //     Get/set the top index.
  220.  
  221.         WInt GetTopIndex() const;
  222.         WBool SetTopIndex( WInt index );
  223.  
  224.         /**************************************************************
  225.          * Methods
  226.          **************************************************************/
  227.  
  228.         // Add
  229.         //
  230.         //     Adds an item.  If itemNumber is set to >= 0, item is
  231.         //     inserted at the given location.  If the list is sorted
  232.         //     and itemNumber is set to -1, inserts in correct spot;
  233.         //     otherwise adds to the end.  Can also set optional
  234.         //     per-item data.
  235.         //     Returns the index of the item, or a negative value in
  236.         //     case of error.
  237.  
  238.         WInt Add( const WChar * string, WInt itemNumber=-1,
  239.                   void * itemUserData=NULL, WBool addInSortedOrder=TRUE );
  240.  
  241.         // DataAddLookupItem
  242.         //
  243.         //     Allows the user to manually fill the list with lookup items.
  244.         //     This is an alternative method to setting the DataLookupSource
  245.         //     and DataLookupColumns properties.
  246.  
  247.         WInt DataAddLookupItem( const WDataValue & value,
  248.                                 const WChar * string = NULL,
  249.                                 WInt itemNumber=-1, void * itemUserData=NULL,
  250.                                 WBool addInSortedOrder=TRUE );
  251.  
  252.         // DataFetchLookupItems
  253.         //
  254.         //      Forces the lookup items to be fetched from the database
  255.         //      (according to the DataLookupSource and DataLookupColumns
  256.         //      properties).
  257.         //      This is normally done when either data source is opened...
  258.  
  259.         WBool DataFetchLookupItems();
  260.  
  261.         // Delete
  262.         //
  263.         //     Deletes the specified item.  If an item's data is set,
  264.         //     invokes the WDeleteItemEvent event first.  Returns the count
  265.         //     of items remaining in the list, or negative if error occurs.
  266.  
  267.         WInt Delete( WInt itemNumber );
  268.  
  269.         // DeleteAll
  270.  
  271.         WBool DeleteAll();
  272.  
  273.         // Find
  274.         //
  275.         //     Finds the first string that matches a given prefix.  The
  276.         //     search starts immediately AFTER the given index (use -1
  277.         //     to search from the beginning) and wraps when the bottom
  278.         //     is reached.  The search is not case-sensitive.  If exact
  279.         //     is specified, the string must match exactly, otherwise
  280.         //     a prefix match is done.  Returns the index of the match
  281.         //     or negative on error.
  282.  
  283.         WInt Find( const WChar * match, WInt startAfter=-1,
  284.                    WBool exact=FALSE ) const;
  285.  
  286.         // MakeDraggable
  287.  
  288.         WBool MakeDraggable();
  289.  
  290.         // Reset
  291.         //
  292.         //     Reset (clear) the list box.
  293.  
  294.         void Reset();
  295.  
  296.         // Retrieve
  297.         //
  298.         //     Return the index of the listbox item closest to a given point
  299.  
  300.         WLong Retrieve( const WPoint & pt ) const;
  301.  
  302.         // Select
  303.         //
  304.         //     Find and select an item.  Searches for the first item whose
  305.         //     prefix matches (case-insensitive) the given string.  Returns
  306.         //     the index of the new selected item, or negative if an error
  307.         //     occurs.  If startAfter is -1, search starts at first item.
  308.  
  309.         WInt Select( const WChar * match, WInt startAfter=-1 );
  310.  
  311.         /**************************************************************
  312.          * Item Properties
  313.          **************************************************************/
  314.  
  315.         // DataLookupValue
  316.         //
  317.         //     Get the lookup value associated with the given list item.
  318.  
  319.         WDataValue GetDataLookupValue( WInt itemNumber ) const;
  320.  
  321.         // Height
  322.         //
  323.         //     Get/set height of listbox items. For get, negative value
  324.         //     is returned to indicate error.
  325.         // NOTE: Low item heights lead to particularily ugly results.
  326.  
  327.         WInt GetHeight( WInt itemNumber ) const;
  328.         WBool SetHeight( WInt itemNumber, WInt height );
  329.  
  330.         // Text
  331.         //
  332.         //     Get the text of a specific item in the list.  First two
  333.         //     versions return a WString, which is set to a null string
  334.         //     if an error occurs.  Last version copies it into a buffer
  335.         //     and returns the length, or a negative value.
  336.  
  337.         WString GetText( WInt itemNumber ) const;
  338.  
  339.         // Selected
  340.         //
  341.         //     c.f. Selected Property
  342.  
  343.         WInt  GetSelected( WInt whichItem ) const;
  344.         WBool SetSelected( WInt index, WBool selected );
  345.  
  346.         // TextLength
  347.         //
  348.         //     Get the length of the text of an item in the list (without
  349.         //     the null character).  A negative value is returned in case
  350.         //     of error.
  351.  
  352.         WInt GetTextLength( WInt itemNumber ) const;
  353.  
  354.         // UserData
  355.         //
  356.         //     Get/set per-item data.  When getting data, a value of
  357.         //     ((void *)-1) for the itemUserData indicates an error.
  358.  
  359.         virtual WBool SetUserData( WInt itemNumber, void * itemUserData );
  360.         virtual void * GetUserData( WInt itemNumber ) const;
  361.  
  362.         /**************************************************************
  363.          * Overrides
  364.          **************************************************************/
  365.  
  366.         virtual WColor GetBackColor( WBool getResultingColor=TRUE ) const;
  367.  
  368.         virtual size_t GetTextLength() const;
  369.  
  370.         virtual WBool WantsDragMessage( WULong whichButton ) const;
  371.  
  372.         virtual WBool FillDragEventData( WDragEventData & event ) const;
  373.  
  374.         virtual WBool ProcessCommand( WUInt id, WNotify code,
  375.                                       WNotifyInfo info, WLong & returns );
  376.  
  377.         virtual WBool CloneWindow( WStyle newStyle, WStyle newExStyle,
  378.                                    void * data=NULL );
  379.  
  380.         virtual WBool ProcessMessage( const WMessage & msg,
  381.                                       WLong & returns );
  382.  
  383.         virtual WBool SetText( const WString & str );
  384.  
  385.         virtual WString GetText() const;
  386.  
  387.         virtual WBool SetUserData( void * userData );
  388.  
  389.         virtual void * GetUserData() const;
  390.  
  391.         virtual WBool SetHeight( const WInt & height, WBool update=TRUE );
  392.  
  393.         virtual WInt GetHeight() const;
  394.  
  395.         virtual WStyle GetDefaultStyle() const;
  396.  
  397.         virtual WStyle GetDefaultExtendedStyle() const;
  398.  
  399.         virtual const WChar * InitializeClass();
  400.  
  401.         virtual WBool FDXIn();
  402.  
  403.         virtual WBool FDXOut();
  404.  
  405.         /**************************************************************
  406.          * Event Handlers
  407.          **************************************************************/
  408.  
  409.         // Event handlers for the DataTarget
  410.  
  411.         WBool DataOpenHandler( WListBox *, WDataOpenEventData *ev );
  412.         WBool DataCloseHandler( WListBox *, WDataCloseEventData *ev );
  413.         WBool DataAvailableHandler( WListBox *, WDataAvailableEventData *ev );
  414.         WBool DataRequestHandler( WListBox *, WDataRequestEventData *ev );
  415.         WBool DeleteItemHandler( WListBox *, WDeleteItemEventData *ev );
  416.         WBool DestroyEventHandler( WListBox *, WEventData * );
  417.         WBool DataLookupItemHandler( WListBox *, WDataLookupItemEventData *ev );
  418.         WBool DragEnterHandler( WListBox *, WDragEventData * ev );
  419.         WBool DragOverHandler( WListBox *, WDragEventData * ev );
  420.         WBool DragDropHandler( WListBox *, WDragEventData * ev );
  421.         WBool DragEndHandler( WListBox *, WDragEventData * ev );
  422.  
  423.         /**************************************************************
  424.          * Internal
  425.          **************************************************************/
  426.  
  427.     protected:
  428.  
  429.         WBool AllocDTColumn();
  430.         WBool AllocLookup();
  431.  
  432.         void * GetRealUserData( WInt itemNumber ) const;
  433.         WBool  SetRealUserData( WInt itemNumber, void * itemUserData );
  434.  
  435.         WBool IndirectUserData( WBool indirect );
  436.  
  437.         WBool DoLookupDataAvailable( WDataAvailableEventData *ev );
  438.         WBool DoColumnDataAvailable( WDataAvailableEventData *ev );
  439.  
  440.         void RenumberDataRows();
  441.         WBool HandleRefreshRow( WULong reason, WLong row, WInt count );
  442.         WBool AddDataRow( WLong row, WInt itemNumber = -1 );
  443.         WInt FindDataRow( WLong row, WInt itemCount = -1 );
  444.  
  445.         void RemoveLookupItems();
  446.  
  447.         /**************************************************************
  448.          * Data members
  449.          **************************************************************/
  450.  
  451.     private:
  452.  
  453.         WInt                    _columnWidth;
  454.         WDataTargetColumn *     _dataTargetColumn;
  455.         WBool                   _indirectUserData;
  456.         WBool                   _dataFilling;
  457.         WBool                   _wasPainting;
  458.         WDataLookup *           _dataLookup;
  459.         WDataTargetCell *       _dataTargetCell;
  460.         WBool                   _bindAsLookup;
  461.         WBool                   _ignoreDataAvailableMove;
  462.         WInt                    _initialValue;
  463.         WBool                   _wasEnabled;
  464.         WInt                    _addRow;
  465.         WStringArray *          _fdxDataSourceStringArray;
  466.         WIntArray *             _fdxDataSourceIntArray;
  467.         WInt *                  _fdxDataSourceInt;
  468.         WBool *                 _fdxDataSourceBool;
  469.         WInt                    _itemToMove;
  470.         WBool                   _draggable;
  471. };
  472.  
  473. #ifndef _WNO_PRAGMA_PUSH
  474. #pragma enum pop;
  475. #pragma pack(pop);
  476. #endif
  477.  
  478. #endif // _WLISTBOX_HPP_INCLUDED
  479.