home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / hpp.z / WDATATRG.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-12  |  17.3 KB  |  642 lines

  1. //
  2. // wdatatrg.hpp
  3. //
  4.  
  5. #ifndef _WDATATRG_HPP_INCLUDED
  6. #define _WDATATRG_HPP_INCLUDED
  7.  
  8. //-----------------------------------------------------------------------
  9. //
  10. // Interfaces used to implement the binding of objects to data sources.
  11. //
  12. // These interfaces are meant to be embedded in other objects.  All
  13. // methods are virtual so that specializations can be created.
  14. //
  15. //-----------------------------------------------------------------------
  16.  
  17. #ifndef _WDATAOBJ_HPP_INCLUDED
  18. #include "wdataobj.hpp"
  19. #endif
  20.  
  21. // Define the basic type of a data target (single or multi/table valued).
  22.  
  23. enum WDataTargetType {
  24.     WDTTCell,
  25.     WDTTColumn,         // Multi-value
  26.     WDTTTable,          // Multi-value
  27.     WDTTLookup,
  28.     WDTTNavigator
  29. };
  30.  
  31. #define WDTTIsMultiValued(T)    ( (T)==WDTTTable || (T)==WDTTColumn )
  32. #define WDTTIsTable(T)          ( (T)==WDTTTable )
  33.  
  34. //
  35. // WDataTarget -- An interface exposed by a bound object.  This interface
  36. //                is what the data source uses to communicate with the
  37. //                object.  The default implementation is to call events
  38. //                on a given event object.
  39. //
  40.  
  41. class WCMCLASS WDataTarget : public WObject {
  42.  
  43.     private:
  44.         WDataTarget( const WDataTarget & );
  45.         WDataTarget& operator=( const WDataTarget & );
  46.  
  47.     public:
  48.  
  49.         WDataTarget( WEventGenerator *eventTarget, WDataTargetType type );
  50.  
  51.         ~WDataTarget();
  52.  
  53.         /*********************************************************
  54.          * Properties
  55.          *********************************************************/
  56.  
  57.         // DataChanged
  58.         //
  59.         //    The target sets this value when the data has changed.
  60.         //    The source uses this to determine if it should invoke
  61.         //    the DataRequest method.
  62.  
  63.         virtual WBool GetDataChanged() const;
  64.         virtual WBool SetDataChanged( WBool changed );
  65.  
  66.         // DataColumns
  67.         //
  68.         //    A string that defines the column or columns that
  69.         //    the target is interested in.
  70.  
  71.         virtual WString GetDataColumns() const;
  72.         virtual WBool   SetDataColumns( const WString & cols );
  73.  
  74.         // DataSource
  75.         //
  76.         //    Setting this registers the target with a WDataSource.
  77.  
  78.         virtual WDataSource *GetDataSource() const;
  79.         virtual WBool        SetDataSource( WDataSource *source );
  80.  
  81.         // Type
  82.         //
  83.         //    What type of target this is.
  84.  
  85.         WDataTargetType GetType() const;
  86.  
  87.         /*********************************************************
  88.          * Methods (called by the data source)
  89.          *********************************************************/
  90.  
  91.         // DataAvailable
  92.         //
  93.         //    The data source calls this method when it has data
  94.         //    available for the target (such as when the current
  95.         //    row changes).  The default implementation is to simply
  96.         //    call the DataAvailable event on the event object.
  97.  
  98.         virtual WBool DataAvailable( WDataAvailableEventData *event );
  99.  
  100.         // DataClose
  101.         //
  102.         //    The data source calls this method when its data set
  103.         //    is no longer available.
  104.  
  105.         virtual WBool DataClose( WDataCloseEventData *event );
  106.  
  107.         // DataOpen
  108.         //
  109.         //    The data source calls this method when a new set of
  110.         //    data is available.  The target can prepare itself
  111.         //    and can also make calls back on the source to bind
  112.         //    columns, etc.
  113.  
  114.         virtual WBool DataOpen( WDataOpenEventData *event );
  115.  
  116.         // DataRequest
  117.         //
  118.         //    The data source calls this method when it wishes
  119.         //    the target to send changed data back to the source.
  120.         //    The default implementation is to simply call the
  121.         //    DataRequest event on the event object.
  122.  
  123.         virtual WBool DataRequest( WDataRequestEventData *event );
  124.  
  125.         /*********************************************************
  126.          * Static functions
  127.          *********************************************************/
  128.  
  129.         // ColumnsToIndexes
  130.         //
  131.         //    Parse the given columns string and save the indexes in the given
  132.         //    array, which is assumed to be at least numberToParse elements
  133.         //    large.
  134.  
  135.         static WBool ColumnsToIndexes( WDataSource * dataSource,
  136.                                        const WString & columns,
  137.                                        WShort * indexes,
  138.                                        WShort numberToParse );
  139.  
  140.         // ColumnsToIndexesEx
  141.         //
  142.         //    Parse given columns and save the indexes in an allocated array.
  143.         //    Return a pointer to the array and the number of columns parsed.
  144.  
  145.         static WBool ColumnsToIndexesEx( WDataSource * dataSource,
  146.                                          const WString & columns,
  147.                                          WShort ** indexes,
  148.                                          WShort & numberParsed );
  149.  
  150.     protected:
  151.     
  152.         WBool            _dataChanged;
  153.         WDataSource     *_source;
  154.         WEventGenerator *_eventTarget;
  155.         WString          _columns;
  156.         WDataTargetType  _type;
  157. };
  158.  
  159. //
  160. // WDataTargetCell
  161. //
  162. //     A specialization of WDataTarget for single-valued objects.
  163.  
  164. class WCMCLASS WDataTargetCell : public WDataTarget {
  165.  
  166.     public:
  167.  
  168.         WDataTargetCell( WEventGenerator *eventTarget );
  169.  
  170.         ~WDataTargetCell();
  171.  
  172.         /*********************************************************
  173.          * Properties
  174.          *********************************************************/
  175.  
  176.         // Index
  177.  
  178.         WShort GetIndex() const;
  179.  
  180.         // Nullable
  181.  
  182.         WBool GetNullable() const;
  183.  
  184.         // ReadOnly
  185.  
  186.         WBool GetReadOnly() const;
  187.  
  188.         // SourceValue
  189.  
  190.         WDataValue GetSourceValue() const;
  191.         WBool      SetSourceValue( const WDataValue & value );
  192.  
  193.         /*********************************************************
  194.          * Overrides
  195.          *********************************************************/
  196.  
  197.         virtual WBool SetDataColumns( const WString & cols );
  198.  
  199.     protected:
  200.  
  201.         // ParseColumns
  202.  
  203.         WBool ParseColumns( WDataSource *src ) const;
  204.  
  205.         WShort  _index;
  206.         WBool   _tried;
  207. };
  208.  
  209. //
  210. // WDataTargetBoolCell
  211. //
  212.  
  213. class WCMCLASS WDataTargetBoolCell : public WDataTargetCell {
  214.  
  215.     public:
  216.  
  217.         WDataTargetBoolCell( WEventGenerator *eventTarget );
  218.  
  219.         ~WDataTargetBoolCell();
  220.  
  221.         /*********************************************************
  222.          * Properties
  223.          *********************************************************/
  224.  
  225.         // DataChecked
  226.  
  227.         WBool   GetDataChecked() const;
  228.         WBool   SetDataChecked( WBool checked );
  229.  
  230.         // DataIndeterminate
  231.         //
  232.         //  TRUE if the current data value matches neither the checked nor
  233.         //  the unchecked value.
  234.  
  235.         WBool   GetDataIndeterminate() const;
  236.  
  237.         // DataValueChecked
  238.  
  239.         WString GetDataValueChecked() const;
  240.         WBool   SetDataValueChecked( const WString & str );
  241.  
  242.         // DataValueUnchecked
  243.  
  244.         WString GetDataValueUnchecked() const;
  245.         WBool   SetDataValueUnchecked( const WString & str );
  246.  
  247.     protected:
  248.  
  249.         WString _checked;
  250.         WString _unchecked;
  251. };
  252.  
  253. //
  254. // WDataTargetColumn
  255. //
  256. //     A specialization of WDataTarget for multiple-valued objects which display
  257. //     one column - ListBox.
  258. //     This does not support updates to the database.
  259. //
  260.  
  261. class WCMCLASS WDataTargetColumn : public WDataTarget {
  262.  
  263.     public:
  264.  
  265.         WDataTargetColumn( WEventGenerator *eventTarget );
  266.  
  267.         ~WDataTargetColumn();
  268.  
  269.         /*********************************************************
  270.          * Properties
  271.          *********************************************************/
  272.  
  273.         // DataTrackRow
  274.  
  275.         WBool GetDataTrackRow() const;
  276.         WBool SetDataTrackRow( WBool trackRow );
  277.  
  278.         // SourceValues
  279.  
  280.         WBool GetSourceValues( WDataValue *value, WDataValue *data ) const;
  281.  
  282.         /*********************************************************
  283.          * Methods
  284.          *********************************************************/
  285.  
  286.         // BindToSource
  287.  
  288.         WBool BindToSource() const;
  289.  
  290.         /*********************************************************
  291.          * Overrides
  292.          *********************************************************/
  293.  
  294.         WBool SetDataColumns( const WString & cols );
  295.  
  296.     protected:
  297.  
  298.         // ParseColumns
  299.  
  300.         WBool ParseColumns() const;
  301.  
  302.         WBool           _trackRow;
  303.         WShort          _valueIndex;
  304.         WShort          _dataIndex;
  305. };
  306.  
  307. //
  308. // WDataTargetTable
  309. //
  310.  
  311. class WCMCLASS WDataTargetTable : public WDataTarget {
  312.  
  313.     public:
  314.  
  315.         WDataTargetTable( WEventGenerator *eventTarget );
  316.  
  317.         ~WDataTargetTable();
  318.  
  319.         /*********************************************************
  320.          * Properties
  321.          *********************************************************/
  322.  
  323.         // BottomRow
  324.  
  325.         WLong GetBottomRow() const;
  326.         WBool SetBottomRow( WLong bottomRow );
  327.  
  328.         // Column
  329.  
  330.         const WDataColumn & GetColumn( WShort index ) const;
  331.  
  332.         // ColumnChanged
  333.  
  334.         WBool GetColumnChanged( WShort index ) const;
  335.         WBool SetColumnChanged( WShort index, WBool changed );
  336.  
  337.         // ColumnCount
  338.  
  339.         WShort GetColumnCount() const;
  340.  
  341.         // ColumnName
  342.  
  343.         WString GetColumnName( WShort index ) const;
  344.  
  345.         // DataTrackRow
  346.  
  347.         WBool GetDataTrackRow() const;
  348.         WBool SetDataTrackRow( WBool trackRow );
  349.  
  350.         // DataGuardRows
  351.  
  352.         WLong GetDataGuardRows() const;
  353.         WBool SetDataGuardRows( WLong guardRows );
  354.  
  355.         // DataKeptRows
  356.  
  357.         WLong GetDataKeptRows() const;
  358.         WBool SetDataKeptRows( WLong keptRows );
  359.  
  360.         // SourceValue
  361.  
  362.         WDataValue GetSourceValue( WShort index ) const;
  363.         WBool SetSourceValue( WShort index, const WDataValue & value );
  364.  
  365.         // TopRow
  366.  
  367.         WLong GetTopRow() const;
  368.         WBool SetTopRow( WLong topRow );
  369.  
  370.         /*********************************************************
  371.          * Methods
  372.          *********************************************************/
  373.  
  374.         // BindToSource
  375.  
  376.         WBool BindToSource();
  377.  
  378.         // FetchGuardRows
  379.         //
  380.         //  Returns the number of guard rows fetched.
  381.  
  382.         WLong FetchGuardRows( WLong checkRow, WBool restoreCurrentRow );
  383.  
  384.         /*********************************************************
  385.          * Overrides
  386.          *********************************************************/
  387.  
  388.         virtual WBool SetDataColumns( const WString & cols );
  389.  
  390.         virtual WBool DataAvailable( WDataAvailableEventData *ev );
  391.  
  392.     protected:
  393.  
  394.         /*********************************************************
  395.          * Internal
  396.          *********************************************************/
  397.  
  398.         // ParseColumns
  399.  
  400.         WBool ParseColumns() const;
  401.  
  402.         // SendGuardRowEvent
  403.  
  404.         WBool SendGuardRowEvent( WDataSource *src, WLong row, WLong count,
  405.                                  WULong action, WBool top ) const;
  406.  
  407.         /*********************************************************
  408.          * Data Members
  409.          *********************************************************/
  410.  
  411.         WBool           _trackRow;
  412.         WLong           _guardRows;
  413.         WLong           _keptRows;
  414.         WShort *        _indexes;
  415.         WShort          _columnCount;
  416.         WLong           _topRow;
  417.         WLong           _bottomRow;
  418.         WLong           _addedRows;
  419.         WLong           _deletedRows;
  420.         WBool           _atTop;
  421.         WBool           _atBottom;
  422.         WBool *         _changed;
  423. };
  424.  
  425. //
  426. // WDataLookup -- A specialization of WDataTarget to support lookup-type
  427. //                bound objects.
  428. //                Calls the DataLookupItem event on the bound object.
  429. //                Does not call the DataOpen/Available/Request/Close events.
  430. //
  431.  
  432. class WCMCLASS WDataLookup : public WDataTarget {
  433.  
  434.     public:
  435.  
  436.         WDataLookup( WEventGenerator *eventTarget );
  437.  
  438.         ~WDataLookup();
  439.  
  440.         /*********************************************************
  441.          * Properties
  442.          *********************************************************/
  443.  
  444.         // HasDisplayColumn
  445.         //
  446.         //  TRUE if a display column was specified, FALSE if only a value
  447.         //  column was specified
  448.  
  449.         virtual WBool GetHasDisplayColumn() const;
  450.  
  451.         /*********************************************************
  452.          * Methods
  453.          *********************************************************/
  454.  
  455.         // FetchLookupItems
  456.         //
  457.         //  Calls the WDataLookupItem event for each data item.
  458.         //  This can be called directly by the target (the data source will be
  459.         //  opened and closed automatically) and will otherwise be called
  460.         //  internally when the specified data source is opened.
  461.  
  462.         virtual WBool FetchLookupItems();
  463.  
  464.         /*********************************************************
  465.          * Overrides
  466.          *********************************************************/
  467.  
  468.         virtual WBool GetDataChanged() const;
  469.         virtual WBool SetDataChanged( WBool );
  470.  
  471.         virtual WBool SetDataColumns( const WString & cols );
  472.  
  473.         virtual WBool DataAvailable( WDataAvailableEventData * );
  474.  
  475.         virtual WBool DataClose( WDataCloseEventData * );
  476.  
  477.         virtual WBool DataOpen( WDataOpenEventData *event );
  478.  
  479.         virtual WBool DataRequest( WDataRequestEventData * );
  480.  
  481.         /*********************************************************
  482.          * Internal
  483.          *********************************************************/
  484.  
  485.     protected:
  486.  
  487.         // ParseColumns
  488.  
  489.         WBool ParseColumns() const;
  490.  
  491.         // SendLookupItemEvent
  492.  
  493.         WBool SendLookupItemEvent( WDataSource *src, WDataValue *value,
  494.                                    WDataValue *displayValue,
  495.                                    WULong action ) const;
  496.  
  497.         // DoFetch
  498.         
  499.         WBool DoFetch( WDataSource * src, WBool weOpened ) const;
  500.  
  501.         /*********************************************************
  502.          * Data Members
  503.          *********************************************************/
  504.  
  505.         WShort          _valueIndex;
  506.         WShort          _displayIndex;
  507.         WBool           _ignoreDataOpen;
  508. };
  509.  
  510. //
  511. // Events triggered by the data source
  512. //
  513. //  These events originate in the data source.
  514. //
  515.  
  516. //
  517. // Reasons for data available/request
  518. //
  519.  
  520. enum {
  521.     WDataReasonNone,
  522.     WDataReasonMove,
  523.     WDataReasonUpdate,
  524.     WDataReasonDelete,
  525.     WDataReasonPrepareForAdd,
  526.     WDataReasonAdd,
  527.     WDataReasonFill,
  528.     WDataReasonCancelUpdate,
  529.     WDataReasonClear,
  530.     WDataReasonEnable,
  531.     WDataReasonDisable
  532. };
  533.  
  534. //
  535. // Suggested actions for data available/request
  536. //
  537.  
  538. enum {
  539.     WDataActionNone,
  540.     WDataActionRefreshRow,      // moved to a new row
  541.     WDataActionFillBegin,       // a fill is beginning
  542.     WDataActionFillEnd,         // a fill is ending
  543.     WDataActionClear,           // clear the control
  544.     WDataActionRenumber,        // multi-value, recount internal row numbers
  545.     WDataActionNewState,        // sent to navigators
  546.     WDataActionEnable,
  547.     WDataActionDisable
  548. };
  549.  
  550. //
  551. // WDataOpenEventData
  552. //
  553.  
  554. struct WDataOpenEventData : public WEventData {
  555.     WDataSource      *dataSource;
  556.     WBool             isRequery;
  557. };
  558.  
  559. //
  560. // WDataAvailableEventData
  561. //
  562.  
  563. struct WDataAvailableEventData : public WEventData {
  564.     WDataSource      *dataSource;
  565.     WLong             currentRow;
  566.     WULong            reason;
  567.     WULong            suggestedAction;
  568. };
  569.  
  570. //
  571. // WDataRequestEventData
  572. //
  573.  
  574. struct WDataRequestEventData : public WEventData {
  575.     WDataSource      *dataSource;
  576.     WLong             currentRow;
  577.     WULong            reason;
  578.     WULong            suggestedAction;
  579. };
  580.  
  581. //
  582. // WDataCloseEventData
  583. //
  584.  
  585. struct WDataCloseEventData : public WEventData {
  586.     WDataSource *dataSource;
  587.     WBool        isRequery;
  588. };
  589.  
  590. //
  591. // Events triggered by the data target
  592. //
  593. //  These events originate in the data target.
  594. //
  595.  
  596. //
  597. // GuardRowEvent actions
  598. //
  599.  
  600. enum {
  601.     WDTGRActionNone,
  602.     WDTGRActionGroupBegin,      // start of guard row group
  603.     WDTGRActionGroupEnd,        // end of guard row group
  604.     WDTGRActionAddRow,          // add a row (top or bottom)
  605. };
  606.  
  607. //
  608. // WDataGuardRowEventData
  609. //
  610.  
  611. struct WDataGuardRowEventData : public WEventData {
  612.     WDataSource *       dataSource;
  613.     WLong               currentRow;
  614.     WLong               count;      // Starts at 0, for current fill
  615.     WULong              action;
  616.     WBool               top;        // TRUE if action refers to top row
  617. };
  618.  
  619. //
  620. // LookupItemEvent actions
  621. //
  622.  
  623. enum {
  624.     WDTLIActionNone,
  625.     WDTLIActionFillBegin,
  626.     WDTLIActionFillEnd,
  627.     WDTLIActionAddRow,
  628. };
  629.  
  630. //
  631. // WDataLookupItemEventData
  632. //
  633.  
  634. struct WDataLookupItemEventData : public WEventData {
  635.     WDataSource *       dataSource;
  636.     WDataValue *        value;
  637.     WDataValue *        displayValue;
  638.     WULong              action;
  639. };
  640.  
  641. #endif
  642.