X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/009c72169f3dc90dbec89fdfe2956065aa35377f..54e18afcca6b75bfaf9dd1f12f4a255df8ae089d:/samples/grid/griddemo.cpp diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index ea97cc9bc6..6e682f597b 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -33,6 +33,7 @@ #include "wx/aboutdlg.h" #include "wx/grid.h" +#include "wx/headerctrl.h" #include "wx/generic/gridctrl.h" #include "griddemo.h" @@ -132,6 +133,7 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame ) EVT_GRID_COL_SIZE( GridFrame::OnColSize ) EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell ) EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected ) + EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging ) EVT_GRID_CELL_CHANGE( GridFrame::OnCellValueChanged ) EVT_GRID_CELL_BEGIN_DRAG( GridFrame::OnCellBeginDrag ) @@ -1058,13 +1060,36 @@ void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev ) ev.Skip(); } +void GridFrame::OnCellValueChanging( wxGridEvent& ev ) +{ + int row = ev.GetRow(), + col = ev.GetCol(); + + wxLogMessage("Value of cell at (%d, %d): about to change " + "from \"%s\" to \"%s\"", + row, col, + grid->GetCellValue(row, col), ev.GetString()); + + // test how vetoing works + if ( ev.GetString() == "42" ) + { + wxLogMessage("Vetoing the change."); + ev.Veto(); + return; + } + + ev.Skip(); +} + void GridFrame::OnCellValueChanged( wxGridEvent& ev ) { int row = ev.GetRow(), col = ev.GetCol(); - wxLogMessage(_T("Value changed for cell at row %d, col %d: now \"%s\""), - row, col, grid->GetCellValue(row, col).c_str()); + wxLogMessage("Value of cell at (%d, %d) changed and is now \"%s\" " + "(was \"%s\")", + row, col, + grid->GetCellValue(row, col), ev.GetString()); ev.Skip(); } @@ -1744,7 +1769,12 @@ private: { int col = m_txtColShowHide->GetCol(); if ( col != -1 ) - m_grid->SetColSize(col, event.GetId() == wxID_ADD ? -1 : 0); + { + m_grid->SetColSize(col, + event.GetId() == wxID_ADD ? wxGRID_AUTOSIZE : 0); + + UpdateOrderAndVisibility(); + } } void OnMoveColumn(wxCommandEvent&) @@ -1756,7 +1786,14 @@ private: m_grid->SetColPos(col, pos); - UpdateOrder(); + UpdateOrderAndVisibility(); + } + + void OnResetColumnOrder(wxCommandEvent&) + { + m_grid->ResetColPos(); + + UpdateOrderAndVisibility(); } void OnGridColSort(wxGridEvent& event) @@ -1775,22 +1812,43 @@ private: 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; - UpdateOrder(); + UpdateOrderAndVisibility(); } event.Skip(); } - void UpdateOrder() + void UpdateOrderAndVisibility() { wxString s; for ( int pos = 0; pos < TabularGridTable::COL_MAX; pos++ ) - s << m_grid->GetColAt(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); } @@ -1830,11 +1888,13 @@ BEGIN_EVENT_TABLE(TabularGridFrame, wxFrame) 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() @@ -1903,8 +1963,9 @@ TabularGridFrame::TabularGridFrame() wxSizer * const sizerShowCols = new wxBoxSizer(wxHORIZONTAL); sizerShowCols->Add(new wxStaticText(panel, wxID_ANY, "Current order:"), flagsHorz); - m_statOrder = new wxStaticText(panel, wxID_ANY, ""); + m_statOrder = new wxStaticText(panel, wxID_ANY, "<<< default >>>"); sizerShowCols->Add(m_statOrder, flagsHorz); + sizerShowCols->Add(new wxButton(panel, wxID_RESET, "&Reset order")); sizerColumns->Add(sizerShowCols, wxSizerFlags().Expand().Border(wxTOP)); wxSizer * const sizerShowHide = new wxBoxSizer(wxHORIZONTAL); @@ -1932,4 +1993,3 @@ void GridFrame::OnTabularTable(wxCommandEvent&) { new TabularGridFrame; } -