NCBI C++ ToolKit
table_listctrl.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_WIDGETS_WX___TABLE_LIST_CTRL__HPP
2 #define GUI_WIDGETS_WX___TABLE_LIST_CTRL__HPP
3 
4 /* $Id: table_listctrl.hpp 39666 2017-10-25 16:01:13Z katargir $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. `This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Yury Voronov
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbistd.hpp>
36 
37 #include <corelib/ncbitime.hpp>
38 
43 #include <gui/utils/command.hpp>
44 //#include <gui/widgets/wx/context_menu_utils.hpp>
45 
47 
48 #include <wx/wx.h>
49 #include <wx/listctrl.h>
50 #include <wx/imaglist.h>
51 #include <wx/menu.h>
52 #include <wx/timer.h>
53 #include <wx/tipwin.h>
54 
55 //#define ___NO_ORDER_FUNCTIONS_YET
56 
57 
59 
61 {
62 public:
63  wxListItem m_Item;
64  int m_ModelIx;
65  int m_Width;
66 
67  bool m_Visible;
70  bool m_Sortable;
71  //int m_Order;
72 
75 
76 public:
77  CTableListColumn( int aModelIndex );
78 
80  {
81  m_Item.m_mask = column.m_Item.m_mask;
82  m_Item.m_itemId = column.m_Item.m_itemId;
83  m_Item.m_col = column.m_Item.m_col;
84  m_Item.m_state = column.m_Item.m_state;
85  m_Item.m_stateMask = column.m_Item.m_stateMask;
86  m_Item.m_text = column.m_Item.m_text;
87  m_Item.m_image = column.m_Item.m_image;
88  m_Item.m_data = column.m_Item.m_data;
89  m_Item.m_format = column.m_Item.m_format;
90  m_Item.m_width = column.m_Item.m_width;
91 
92  m_ModelIx = column.m_ModelIx;
93  m_Width = column.m_Width;
94  m_Visible = column.m_Visible;
95  m_HidingAllowed = column.m_HidingAllowed;
96  m_Resizable = column.m_Resizable;
97  m_Sortable = column.m_Sortable;
98 
99  m_Sorter = column.m_Sorter;
100  m_Formatter = column.m_Formatter;
101 
102  return *this;
103  }
104 };
105 
106 ///////////////////////////////////////////////////////////////////////////////
107 /// CwxTableListCtrl - generic wxListCtrl-based Table Control
108 ///
109 /// Do not use InsertColumn() and DeleteColumn() as far
110 /// as it ruins all the internal work with columns
111 
113  : public wxListView
114  , public IwxTableModelListener
115  , public CEventHandler
116 {
117 public:
119  {
122  eDescSort
123  };
124 
126  {
128  };
129 
130  typedef pair<int,ESortedState> TSortedCol;
131 
132 
133 public:
135 
137  wxWindow* parent,
138  wxWindowID id = wxID_ANY,
139  const wxPoint& pos = wxDefaultPosition,
140  const wxSize& size = wxDefaultSize,
141  long style = wxLC_REPORT | wxLC_VIRTUAL,
142  const wxValidator& validator = wxDefaultValidator,
143  const wxString& name = wxListCtrlNameStr
144  );
145 
146  CwxTableListCtrl( wxWindow* parent, wxWindowID id, IwxTableModel* aData );
147  virtual ~CwxTableListCtrl();
148 
149 public:
150  IwxTableModel* GetModel() const;
151  void SetModel( IwxTableModel* data_model, bool own = false );
152  void AssignModel( IwxTableModel* data_model );
153  void RemoveModel();
154 
155  wxVariant GetValueFromModelAt( int row, int col ) const;
156  const wxString GetTypeFromModelAt( int row, int col ) const;
157 
158  wxVariant GetValueVisibleAt( int row, int col ) const;
159  const wxString GetTypeVisibleAt( int row, int col ) const;
160 
161  void EnableHeader( bool flag );
162 
163  void SaveTableSettings(CRegistryWriteView& view, bool saveSorting = true) const;
164  void LoadTableSettings(const CRegistryReadView& view, bool byName = false);
165 
166  void ExportTableControlToCSV();
167 
168 #ifndef wxHAS_LISTCTRL_COLUMN_ORDER
169  // Gets the column order from its index or index from its order
170  int GetColumnOrder( int col ) const;
171  int GetColumnIndexFromOrder( int order ) const;
172 
173  // Gets the column order for all columns
174  wxArrayInt GetColumnsOrder() const;
175 
176  // Sets the column order for all columns
177  bool SetColumnsOrder( const wxArrayInt& orders );
178 #endif
179 
180  bool SetColumnOrder( int col, int order );
181 
182  int GetNumColumnsVisible() const;
183  int GetNumColumnsHidden() const;
184 
185  int ColumnVisibleToIndex( int col ) const;
186  int ColumnIndexToVisible( int col ) const;
187 
188  int ColumnHiddenToIndex( int col ) const;
189  int ColumnIndexToHidden( int col ) const;
190 
191  int ColumnIndexToModel( int col ) const;
192  int ColumnModelToIndex( int col ) const;
193 
194  int ColumnVisibleToModel( int col ) const;
195  int ColumnModelToVisible( int col ) const;
196 
197  bool IsColumnVisibleByIndex( int col ) const;
198 
199  wxString GetColumnNameByModel( int col ) const;
200  wxString GetColumnNameByIndex( int col ) const;
201  wxString GetColumnNameVisible( int col ) const;
202  wxString GetColumnNameHidden( int col ) const;
203 
204  wxString GetColumnTitleByModel( int col ) const;
205  wxString GetColumnTitleByIndex( int col ) const;
206  wxString GetColumnTitleVisible( int col ) const;
207  wxString GetColumnTitleHidden( int col ) const;
208 
209  int GetModelIndexByName( wxString name ) const;
210  int GetColumnIndexByName( wxString name ) const;
211  int GetColumnVisibleByName( wxString name ) const;
212  int GetColumnHiddenByName( wxString name ) const;
213 
214  wxListColumnFormat GetColumnAlign( int col_ix ) const; // by index
215  void SetColumnAlign( int col_ix, wxListColumnFormat aAlign ); // by index
216  //! get/set all other column features
217 
218  void AddColumn( CTableListColumn& aColumn );
219  void AddColumn( int model_ix, bool hidden = false );
220 
221  void RemoveColumn( int col_ix, bool hide = false );
222  void MoveColumn( int col_ix, int aTargetColIx );
223 
224  void HideColumn( int col_ix ){ RemoveColumn( col_ix, true ); }
225  void ShowColumn( int col_ix );
226 
227  void RemoveAllColumns();
228 
229  void CreateAllColumnsFromModel();
230 
231  int GetNumRowsVisible() const { return (int)m_VisibleRows.size(); }
232 
233  int RowVisibleToData( int aRow ) const;
234  int RowDataToVisible( int aRow ) const;
235 
236  /// @name Methods concerned with filtering
237  /// @{
238 
239  /// Filter is not owned by Table control.You should manage it deletion by yourself.
240  const IRowFilter* GetFilter() const;
241  void SetFilter( IRowFilter* filter );
242  IRowFilter* RemoveFilter();
243  void ApplyFiltering( bool preserve_state = true );
244  /// @}
245 
246  /// @name Methods concerned with sorting
247  /// @{
248  // Sorter is not owned by Table control. You should manage it deletion by yourself.
249  CIRef<IRowSorter> GetSorter() const;
250  void SetSorter( CIRef<IRowSorter> sorter );
251  CIRef<IRowSorter> RemoveSorter();
252 
253  // Sorters-by-column and default sorters ARE owned by Table control.
254  // Do not delete them by yourself.
255 
256  CIRef<IRowSorter> GetColumnSorterByIndex( int col_ix ) const;
257  void SetSorterByIndex( int col );
258  void AddSorterIndex( size_t col, ESortedState state ){
259  _ASSERT( col < m_Columns.size() );
260 
261  m_SortedCols.push_back( TSortedCol( (int)col, state ) );
262  }
263  void SetSorterByColumns();
264 
265  ESortedState GetSortedState() const { return m_Sorted; }
266  void SetSortedState( ESortedState sorted );
267  //int GetSortedCol() const { return m_SortedCols.empty() ? -1 : m_SortedCols[0]; }
268  const vector<TSortedCol>& GetSortedCols() const { return m_SortedCols; }
269 
270  void UpdateSortIcons();
271  void ClearSortIcons();
272 
273  void InvalidateSorting() { m_SortInvalid = true; }
274  void ResetSorting() { m_SortedCols.clear(); m_Sorted = eNoSort; InvalidateSorting(); }
275  void ApplySorting( bool preserve_state = true );
276  void ShiftSorting( int col = -1 );
277 
278  IwxVariantSorter* GetDefaultSorter( wxString aType ) const;
279  void SetDefaultSorter( wxString aType, IwxVariantSorter* aSorter );
280  void CreateDefaultSorters();
281  /// @}
282 
283  /// @name Focus
284  /// @{
285  int GetFocusRow() const;
286  void SetFocusRow( int row );
287  int GetFocusColumn() const;
288  void SetFocusColumn( int WXUNUSED(col) ) {}
289 
290  void OnFocusChanged( wxListEvent& event );
291  /// @}
292 
293  /// @name Selection
294  /// @{
295  bool IsCellSelectedAt( int row, int WXUNUSED(col) ) const;
296  long GetLastSelected() const;
297 
298  void SelectAll();
299  void ClearSelection();
300 
301  wxArrayInt GetDataRowsSelected() const;
302  void GetDataRowsSelected( vector<int>& rows ) const;
303  void SetDataRowsSelected( const vector<int>& rows );
304  /// @}
305 
306  /// @name Methods concerned with editing
307  /// When editing starts, two instances are installed inside the table, an editor
308  /// and an editing widget.
309  /// @{
310  //bool IsCellEditableAt(int row, int col ) const;
311  //bool EditCellAt(int row, int col, int anEventCode = FL_NO_EVENT );
312 
313  //bool IsEditing() const { return m_EditingWg != (Fl_Widget*)NULL && m_EditingWg -> parent() != NULL; }
314  //int GetEditingRow() const { return IsEditing() ? GetFocusRow() : -1; }
315  //int GetEditingColumn() const { return IsEditing() ? GetFocusColumn() : -1; }
316  /// @}
317 
318  /// @name ITCEListener interface implementation
319  ///
320  /// @{
321  /// To be called by editor when editing session is cancelled by user
322  /// or otherwise
323  /* Done through events
324  virtual void EditingCancelled();
325  /// To be called by editor when editing session is finished by user
326  /// or otherwise
327  virtual void EditingFinished();
328  */
329  /// @}
330 
331  /* Of no use for ListCtrl
332  int GetRowVisibleAtY(int aY ) const;
333  int GetTotalRowHeight() const;
334  int GetNumRowsViewed() const;
335  */
336 
337  // CRect GetCellRect(int aRow, int aCol, bool aWithSpacing ) const;
338 
339  /* Use EnsureVisible( item ) instead
340  virtual void ScrollRectToVisible( const CRect& aRect );
341  void ScrollRowsToVisible(int aRow, int bRow = -1 );
342  void ScrollSelectionToVisible();
343  */
344 
345  /*
346  CAnyRef<ITableCellRenderer> GetDefaultRenderer( const type_info& aType ) const;
347  void SetDefaultRenderer( const type_info& aType, CAnyRef<ITableCellRenderer> aRenderer );
348  ///
349  CAnyRef<ITableCellEditor> GetDefaultEditor( const type_info& aType ) const;
350  void SetDefaultEditor( const type_info& aType, CAnyRef<ITableCellEditor> aEditor );
351  */
352 
353  //CEventProc& GetEventProcessor() { return m_Semantics; }
354 
355  /// @name ITableModel::ITMListener interface implementation
356  /// @{
357  virtual void TableChanged( const CwxTableModelNotice& aNotice );
358  /// @}
359 
360  /// @name ISelectionModel::ISMListener interface implementation
361  /// @{
362  /* Done through events
363  virtual void SelectionChanged( const ISelectionModel::CSMNotice& aNotice );
364  */
365  /// @}
366 
367  // Of no use in wxWidgets; stayed here for ease of porting
368  //bool GetAutoResizeMode() const { return false; }
369  //void SetAutoResizeMode( bool WXUNUSED(aMode) ){ }
370 
371  /// /////////// Exterior settings
372 
373  bool ShowHorzLines() const;
374  void SetShowHorzLines( bool show );
375  bool ShowVertLines() const { return (GetWindowStyleFlag() & wxLC_VRULES) != 0; }
376  void SetShowVertLines( bool show ){ SetSingleStyle( wxLC_VRULES, show ); }
377 
378  void SetShowGrid( bool aShow ){ SetShowHorzLines( aShow ); SetShowVertLines( aShow ); }
379 
380  /// @name Various color settings
381  /// @{
382 
383  wxColor GetForegroundColor() const { return GetTextColour(); }
384  void SetForegroundColor( wxColor aColor ){ SetTextColour( aColor ); }
385 
386  #ifdef __WXMAC__
387  // bug in wxWidgets GetBackgroundColour() define on Mac (missing const)
388  wxColor GetBackgroundColor() { return GetBackgroundColour(); }
389  #else
390  wxColor GetBackgroundColor() const { return GetBackgroundColour(); }
391  #endif
392  void SetBackgroundColor( wxColor aColor ){ SetBackgroundColour( aColor ); }
393 
394  wxColor GetSecondBackgroundColor() const { return m_2ndBackColor; }
395  void SetSecondBackgroundColor( wxColor aColor ){ m_2ndBackColor = aColor; }
396  /// @}
397 
398  bool ShouldPropagateContextMenu() const { return mf_Propagate; }
399  void SetContextMenuEventPropagation( bool flag ){ mf_Propagate = flag; }
400 
401  virtual void AppendMenuItems( wxMenu& aMenu );
402 
403 protected:
404  void OnSelectionChanged( wxListEvent& event );
405  virtual void x_OnSelectionChanged( bool on, int start_row, int end_row = -1 );
406 
407  /// ////////////// Header management
408 
409  void OnColumnClick( wxListEvent &anEvt );
410  void OnColumnRightClick( wxListEvent &anEvt );
411  void OnColumnBeginDrag( wxListEvent &anEvt );
412  void OnColumnEndDrag( wxListEvent &anEvt );
413 
414  void OnKeyDown( wxKeyEvent &anEvt );
415 
416  void OnSelectAll( wxCommandEvent &anEvt );
417 
418  void OnContextMenu( wxContextMenuEvent& anEvent );
419 
420  void OnCopy( wxCommandEvent &anEvt );
421  void OnCopyUpdate( wxUpdateUIEvent &anEvt );
422  virtual void x_OnCopy();
423 
424  void OnDebugEvent( wxListEvent &anEvt );
425  void OnMouseMove( wxMouseEvent &anEvt );
426  void OnLeftDown( wxMouseEvent &anEvt );
427 
428  void OnTimer(wxTimerEvent& event);
429 
430  void OnPaint( wxPaintEvent& anEvt );
431 
432 protected:
433  void InternalOnPopupMenu( wxCommandEvent& event );
434  int DoGetPopupMenuSelectionFromUser( wxMenu& menu, const wxPoint& pos );
435  int x_FindRow(const wxPoint &pt) const;
436  int x_FindColumn(const wxPoint &pt) const;
437  void x_ShowTooltip(bool show);
438 
439  int ShowHeaderContextMenu( const wxPoint& pos );
440 
441  struct SViewState {
444  vector<int> m_Selected;
445 
446  SViewState() : m_FocusModelRow( -1 ), m_TopModelRow( -1 ) {}
447  };
448  void x_SaveViewState( SViewState* state );
449  void x_RestoreViewState( SViewState* state );
450 
451 
452 private:
453  void x_Init();
454 
455  wxListColumnFormat GetAlignByType( wxString aColType );
456 
457  /// @name wxListCtrl virtual mode implementation
458  /// @{
459  virtual wxString OnGetItemText( long aRow, long aCol ) const;
460  virtual int OnGetItemImage( long aRow ) const;
461  virtual int OnGetItemColumnImage( long aRow, long aCol ) const;
462  virtual wxListItemAttr* OnGetItemAttr( long aRow ) const;
463  /// @}
464 
465 private:
466  mutable wxImageList m_ImageList;
467  mutable bool mf_ImageListInstalled;
468 
472 
473  WX_DECLARE_STRING_HASH_MAP( int, TAliasMap );
474  mutable TAliasMap m_AliasToIndex;
475 
476  wxColor m_2ndBackColor;
477 
478  vector<int> m_VisibleRows;
479 
480  vector<CTableListColumn> m_Columns;
482 
484  //FIX-WX: wxListItem have a private assignment operator
485 
488 
493 
494  vector<TSortedCol> m_SortedCols;
495  //int m_SortedCol;
496 
498  mutable TSorterMap m_DefaultSorters;
499 
501 
502  /// Last column user clicked on (tooltips are only on selected/clicked itmes)
504  /// Last row user clicked on
506  /// Test to put in tooltip
507  wxString m_ToolTipText;
508  /// Start/stop x value of cell to which tooltip belongs
509  mutable int m_TipStartX;
510  mutable int m_TipEndX;
511 
512  // ToolTip members
513  wxTimer m_Timer;
514 
515  DECLARE_DYNAMIC_CLASS(CwxTableListCtrl)
516  DECLARE_NO_COPY_CLASS(CwxTableListCtrl)
517  DECLARE_EVENT_TABLE()
518 };
519 
521 
522 #endif // GUI_WIDGETS_WX___TABLE_LIST_CTRL__HPP
CEventHandler.
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
CIRef< IwxStringFormat > m_Formatter
CIRef< IRowSorter > m_Sorter
CTableListColumn & operator=(const CTableListColumn &column)
CTableListColumn(int aModelIndex)
CwxTableListCtrl - generic wxListCtrl-based Table Control.
void SetContextMenuEventPropagation(bool flag)
wxString m_ToolTipText
Test to put in tooltip.
void SetShowVertLines(bool show)
const vector< TSortedCol > & GetSortedCols() const
wxColor GetBackgroundColor() const
int m_TipStartX
Start/stop x value of cell to which tooltip belongs.
void SetFocusColumn(int WXUNUSED(col))
WX_DECLARE_STRING_HASH_MAP(IwxVariantSorter *, TSorterMap)
TAliasMap m_AliasToIndex
WX_DECLARE_STRING_HASH_MAP(int, TAliasMap)
wxColor GetSecondBackgroundColor() const
CFixGenericListCtrl m_FixGenericListCtrl
void SetShowGrid(bool aShow)
void SetSecondBackgroundColor(wxColor aColor)
IwxTableModel * m_DataModel
const wxString GetTypeFromModelAt(int row, int col) const
TSorterMap m_DefaultSorters
vector< CTableListColumn > m_Columns
void SetForegroundColor(wxColor aColor)
bool ShowVertLines() const
int m_CurrentCol
Last column user clicked on (tooltips are only on selected/clicked itmes)
ESortedState GetSortedState() const
void AddSorterIndex(size_t col, ESortedState state)
IRowFilter * m_Filter
void SetBackgroundColor(wxColor aColor)
pair< int, ESortedState > TSortedCol
wxImageList m_ImageList
int m_CurrentRow
Last row user clicked on.
vector< int > m_VisibleRows
void HideColumn(int col_ix)
ESortedState m_Sorted
CIRef< IRowSorter > m_Sorter
bool ShouldPropagateContextMenu() const
vector< TSortedCol > m_SortedCols
int GetNumRowsVisible() const
wxColor GetForegroundColor() const
wxVariant GetValueFromModelAt(int row, int col) const
CwxTableModelNotice.
Definition: table_model.hpp:86
IwxTableModelListener.
virtual void TableChanged(const CwxTableModelNotice &aNotice)=0
GUI command routing and handling framework.
Include a standard set of the NCBI C++ Toolkit most basic headers.
static const char * column
Definition: stats.c:23
@ eBaseCmdLast
Definition: command.hpp:111
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_GUIWIDGETS_WX_EXPORT
Definition: gui_export.h:543
const struct ncbi::grid::netcache::search::fields::SIZE size
Defines: CTimeFormat - storage class for time format.
static static static wxID_ANY
#define row(bind, expected)
Definition: string_bind.c:73
#define _ASSERT
@ eCmdExportToCSV
Modified on Fri Sep 20 14:57:06 2024 by modify_doxy.py rev. 669887