+// ============================================================================
+// TabularGrid: grid used for display of tabular data
+// ============================================================================
+
+class TabularGridTable : public wxGridTableBase
+{
+public:
+ enum
+ {
+ COL_NAME,
+ COL_EXT,
+ COL_SIZE,
+ COL_DATE,
+ COL_MAX
+ };
+
+ enum
+ {
+ ROW_MAX = 3
+ };
+
+ TabularGridTable() { m_sortOrder = NULL; }
+
+ virtual int GetNumberRows() { return ROW_MAX; }
+ virtual int GetNumberCols() { return COL_MAX; }
+
+ virtual wxString GetValue(int row, int col)
+ {
+ if ( m_sortOrder )
+ row = m_sortOrder[row];
+
+ switch ( col )
+ {
+ case COL_NAME:
+ case COL_EXT:
+ return GetNameOrExt(row, col);
+
+ case COL_SIZE:
+ return wxString::Format("%lu", GetSize(row));
+
+ case COL_DATE:
+ return GetDate(row).FormatDate();
+
+ case COL_MAX:
+ default:
+ wxFAIL_MSG( "unknown column" );
+ }
+
+ return wxString();
+ }
+
+ virtual void SetValue(int, int, const wxString&)
+ {
+ wxFAIL_MSG( "shouldn't be called" );
+ }
+
+ virtual wxString GetColLabelValue(int col)
+ {
+ // notice that column parameter here always refers to the internal
+ // column index, independently of its position on the screen
+ static const char *labels[] = { "Name", "Extension", "Size", "Date" };
+ wxCOMPILE_TIME_ASSERT( WXSIZEOF(labels) == COL_MAX, LabelsMismatch );
+
+ return labels[col];
+ }
+
+ virtual void SetColLabelValue(int, const wxString&)
+ {
+ wxFAIL_MSG( "shouldn't be called" );
+ }
+
+ void Sort(int col, bool ascending)
+ {
+ // we hardcode all sorting orders for simplicity here
+ static int sortOrders[COL_MAX][2][ROW_MAX] =
+ {
+ // descending ascending
+ { { 2, 1, 0 }, { 0, 1, 2 } },
+ { { 2, 1, 0 }, { 0, 1, 2 } },
+ { { 2, 1, 0 }, { 0, 1, 2 } },
+ { { 1, 0, 2 }, { 2, 0, 1 } },
+ };
+
+ m_sortOrder = col == wxNOT_FOUND ? NULL : sortOrders[col][ascending];
+ }
+
+private:
+ wxString GetNameOrExt(int row, int col) const
+ {
+ static const char *
+ names[] = { "autoexec.bat", "boot.ini", "io.sys" };
+ wxCOMPILE_TIME_ASSERT( WXSIZEOF(names) == ROW_MAX, NamesMismatch );
+
+ const wxString s(names[row]);
+ return col == COL_NAME ? s.BeforeFirst('.') : s.AfterLast('.');
+ }
+
+ unsigned long GetSize(int row) const
+ {
+ static const unsigned long
+ sizes[] = { 412, 604, 40774 };
+ wxCOMPILE_TIME_ASSERT( WXSIZEOF(sizes) == ROW_MAX, SizesMismatch );
+
+ return sizes[row];
+ }
+
+ wxDateTime GetDate(int row) const
+ {
+ static const char *
+ dates[] = { "2004-04-17", "2006-05-27", "1994-05-31" };
+ wxCOMPILE_TIME_ASSERT( WXSIZEOF(dates) == ROW_MAX, DatesMismatch );
+
+ wxDateTime dt;
+ dt.ParseISODate(dates[row]);
+ return dt;
+ }
+
+ int *m_sortOrder;
+};
+
+// specialized text control for column indexes entry
+class ColIndexEntry : public wxTextCtrl
+{
+public:
+ ColIndexEntry(wxWindow *parent)
+ : wxTextCtrl(parent, wxID_ANY, "")
+ {
+ SetValidator(wxTextValidator(wxFILTER_NUMERIC));
+ }
+
+ int GetCol()
+ {
+ unsigned long col;
+ if ( !GetValue().ToULong(&col) || col > TabularGridTable::COL_MAX )
+ {
+ SetFocus();
+ return -1;
+ }
+
+ return col;
+ }
+
+protected:
+ virtual wxSize DoGetBestSize() const
+ {
+ wxSize size = wxTextCtrl::DoGetBestSize();
+ size.x = 3*GetCharWidth();
+ return size;
+ }
+};
+
+class TabularGridFrame : public wxFrame
+{
+public:
+ TabularGridFrame();
+
+private:
+ enum // control ids
+ {
+ Id_Check_UseNativeHeader,
+ Id_Check_DrawNativeLabels,
+ Id_Check_ShowRowLabels,
+ Id_Check_EnableColMove
+ };
+
+ // event handlers
+
+ void OnToggleUseNativeHeader(wxCommandEvent&)
+ {
+ m_grid->UseNativeColHeader(m_chkUseNative->IsChecked());
+ }
+
+ void OnUpdateDrawNativeLabelsUI(wxUpdateUIEvent& event)
+ {
+ // we don't draw labels at all, native or otherwise, if we use the
+ // native header control
+ event.Enable( !m_chkUseNative->GetValue() );
+ }
+
+ void OnToggleDrawNativeLabels(wxCommandEvent&)
+ {
+ m_grid->SetUseNativeColLabels(m_chkDrawNative->IsChecked());
+ }
+
+ void OnToggleShowRowLabels(wxCommandEvent&)
+ {
+ m_grid->SetRowLabelSize(m_chkShowRowLabels->IsChecked()
+ ? wxGRID_AUTOSIZE
+ : 0);
+ }
+
+ void OnToggleColMove(wxCommandEvent&)
+ {
+ m_grid->EnableDragColMove(m_chkEnableColMove->IsChecked());
+ }
+
+ void OnShowHideColumn(wxCommandEvent& event)
+ {
+ int col = m_txtColShowHide->GetCol();
+ if ( col != -1 )
+ {
+ m_grid->SetColSize(col,
+ event.GetId() == wxID_ADD ? wxGRID_AUTOSIZE : 0);
+
+ UpdateOrderAndVisibility();
+ }
+ }
+
+ void OnMoveColumn(wxCommandEvent&)
+ {
+ int col = m_txtColIndex->GetCol();
+ int pos = m_txtColPos->GetCol();
+ if ( col == -1 || pos == -1 )
+ return;
+
+ m_grid->SetColPos(col, pos);
+
+ UpdateOrderAndVisibility();
+ }
+
+ void OnResetColumnOrder(wxCommandEvent&)
+ {
+ m_grid->ResetColPos();
+
+ UpdateOrderAndVisibility();
+ }
+
+ void OnGridColSort(wxGridEvent& event)
+ {
+ const int col = event.GetCol();
+ m_table->Sort(col, !(m_grid->IsSortingBy(col) &&
+ m_grid->IsSortOrderAscending()));
+ }
+
+ void OnGridColMove(wxGridEvent& event)
+ {
+ // can't update it yet as the order hasn't been changed, so do it a bit
+ // later
+ m_shouldUpdateOrder = true;
+
+ event.Skip();
+ }
+
+ void OnGridColSize(wxGridSizeEvent& event)
+ {
+ // we only catch this event to react to the user showing or hiding this
+ // column using the header control menu and not because we're
+ // interested in column resizing
+ UpdateOrderAndVisibility();
+
+ event.Skip();
+ }
+
+ void OnIdle(wxIdleEvent& event)
+ {
+ if ( m_shouldUpdateOrder )
+ {
+ m_shouldUpdateOrder = false;
+ UpdateOrderAndVisibility();
+ }
+
+ event.Skip();
+ }
+
+ void UpdateOrderAndVisibility()
+ {
+ wxString s;
+ for ( int pos = 0; pos < TabularGridTable::COL_MAX; pos++ )
+ {
+ const int col = m_grid->GetColAt(pos);
+ const bool isHidden = m_grid->GetColSize(col) == 0;
+
+ if ( isHidden )
+ s << '[';
+ s << col;
+ if ( isHidden )
+ s << ']';
+
+ s << ' ';
+ }
+
+ m_statOrder->SetLabel(s);
+ }
+
+ // controls
+ wxGrid *m_grid;
+ TabularGridTable *m_table;
+ wxCheckBox *m_chkUseNative,
+ *m_chkDrawNative,
+ *m_chkShowRowLabels,
+ *m_chkEnableColMove;
+
+ ColIndexEntry *m_txtColIndex,
+ *m_txtColPos,
+ *m_txtColShowHide;
+
+ wxStaticText *m_statOrder;
+
+ // fla for EVT_IDLE handler
+ bool m_shouldUpdateOrder;
+
+ wxDECLARE_NO_COPY_CLASS(TabularGridFrame);
+ DECLARE_EVENT_TABLE()
+};
+
+BEGIN_EVENT_TABLE(TabularGridFrame, wxFrame)
+ EVT_CHECKBOX(Id_Check_UseNativeHeader,
+ TabularGridFrame::OnToggleUseNativeHeader)
+ EVT_CHECKBOX(Id_Check_DrawNativeLabels,
+ TabularGridFrame::OnToggleDrawNativeLabels)
+ EVT_CHECKBOX(Id_Check_ShowRowLabels,
+ TabularGridFrame::OnToggleShowRowLabels)
+ EVT_CHECKBOX(Id_Check_EnableColMove,
+ TabularGridFrame::OnToggleColMove)
+
+ EVT_UPDATE_UI(Id_Check_DrawNativeLabels,
+ TabularGridFrame::OnUpdateDrawNativeLabelsUI)
+
+ EVT_BUTTON(wxID_APPLY, TabularGridFrame::OnMoveColumn)
+ EVT_BUTTON(wxID_RESET, TabularGridFrame::OnResetColumnOrder)
+ EVT_BUTTON(wxID_ADD, TabularGridFrame::OnShowHideColumn)
+ EVT_BUTTON(wxID_DELETE, TabularGridFrame::OnShowHideColumn)
+
+ EVT_GRID_COL_SORT(TabularGridFrame::OnGridColSort)
+ EVT_GRID_COL_MOVE(TabularGridFrame::OnGridColMove)
+ EVT_GRID_COL_SIZE(TabularGridFrame::OnGridColSize)
+
+ EVT_IDLE(TabularGridFrame::OnIdle)
+END_EVENT_TABLE()
+
+TabularGridFrame::TabularGridFrame()
+ : wxFrame(NULL, wxID_ANY, "Tabular table")
+{
+ m_shouldUpdateOrder = false;
+
+ wxPanel * const panel = new wxPanel(this);
+
+ // create and initialize the grid with the specified data
+ m_table = new TabularGridTable;
+ m_grid = new wxGrid(panel, wxID_ANY,
+ wxDefaultPosition, wxDefaultSize,
+ wxBORDER_STATIC | wxWANTS_CHARS);
+ m_grid->SetTable(m_table, true, wxGrid::wxGridSelectRows);
+
+ m_grid->EnableDragColMove();
+ m_grid->UseNativeColHeader();
+ m_grid->HideRowLabels();
+
+ // add it and the other controls to the frame
+ wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL);
+ sizerTop->Add(m_grid, wxSizerFlags(1).Expand().Border());
+
+ wxSizer * const sizerControls = new wxBoxSizer(wxHORIZONTAL);
+
+ wxSizer * const sizerStyles = new wxBoxSizer(wxVERTICAL);
+ m_chkUseNative = new wxCheckBox(panel, Id_Check_UseNativeHeader,
+ "&Use native header");
+ m_chkUseNative->SetValue(true);
+ sizerStyles->Add(m_chkUseNative, wxSizerFlags().Border());
+
+ m_chkDrawNative = new wxCheckBox(panel, Id_Check_DrawNativeLabels,
+ "&Draw native column labels");
+ sizerStyles->Add(m_chkDrawNative, wxSizerFlags().Border());
+
+ m_chkShowRowLabels = new wxCheckBox(panel, Id_Check_ShowRowLabels,
+ "Show &row labels");
+ sizerStyles->Add(m_chkShowRowLabels, wxSizerFlags().Border());
+
+ m_chkEnableColMove = new wxCheckBox(panel, Id_Check_EnableColMove,
+ "Allow column re&ordering");
+ m_chkEnableColMove->SetValue(true);
+ sizerStyles->Add(m_chkEnableColMove, wxSizerFlags().Border());
+ sizerControls->Add(sizerStyles);
+
+ sizerControls->AddSpacer(10);