From 6f58f3d7e0ed959dd6a568a70161ec14ec524feb Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 5 Apr 2013 21:11:59 +0000 Subject: [PATCH] Allow to customize wxGrid column auto-sizing. By default the columns are auto-sized to fit just their label, which is fast but not very user-friendly. Allow customizing this behaviour by handling the (new) wxEVT_GRID_COL_AUTO_SIZE event. Closes #15077. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73789 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/generic/grid.h | 8 ++++++-- interface/wx/grid.h | 8 ++++++++ samples/grid/griddemo.cpp | 16 ++++++++++++++++ samples/grid/griddemo.h | 1 + src/generic/grid.cpp | 8 +++++--- 6 files changed, 37 insertions(+), 5 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 23b2862cc2..7789ce6bd9 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -627,6 +627,7 @@ All (GUI): - Added wxBookCtrlBase::FindPage() (troelsk). - Added wxDocument::Activate() (troelsk). - Added wxDocManager::FindDocumentByPath() (troelsk). +- Added wxEVT_GRID_COL_AUTO_SIZE event (Igor Korot). wxGTK: diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index a223c4f569..a763cd1b04 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -2116,8 +2116,9 @@ protected: int SendEvent(const wxEventType evtType, const wxString& s = wxString()) { return SendEvent(evtType, m_currentCellCoords, s); } - // send wxEVT_GRID_{ROW,COL}_SIZE - void SendGridSizeEvent(wxEventType type, + // send wxEVT_GRID_{ROW,COL}_SIZE or wxEVT_GRID_COL_AUTO_SIZE, return true + // if the event was processed, false otherwise + bool SendGridSizeEvent(wxEventType type, int row, int col, const wxMouseEvent& mouseEv); @@ -2605,6 +2606,7 @@ wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridE wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_GRID_LABEL_RIGHT_DCLICK, wxGridEvent ); wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_GRID_ROW_SIZE, wxGridSizeEvent ); wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_GRID_COL_SIZE, wxGridSizeEvent ); +wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_GRID_COL_AUTO_SIZE, wxGridSizeEvent ); wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_GRID_RANGE_SELECT, wxGridRangeSelectEvent ); wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_GRID_CELL_CHANGING, wxGridEvent ); wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_GRID_CELL_CHANGED, wxGridEvent ); @@ -2656,6 +2658,7 @@ typedef void (wxEvtHandler::*wxGridEditorCreatedEventFunction)(wxGridEditorCreat #define EVT_GRID_CMD_LABEL_RIGHT_DCLICK(id, fn) wx__DECLARE_GRIDEVT(LABEL_RIGHT_DCLICK, id, fn) #define EVT_GRID_CMD_ROW_SIZE(id, fn) wx__DECLARE_GRIDSIZEEVT(ROW_SIZE, id, fn) #define EVT_GRID_CMD_COL_SIZE(id, fn) wx__DECLARE_GRIDSIZEEVT(COL_SIZE, id, fn) +#define EVT_GRID_CMD_COL_AUTO_SIZE(id, fn) wx__DECLARE_GRIDSIZEEVT(COL_AUTO_SIZE, id, fn) #define EVT_GRID_CMD_COL_MOVE(id, fn) wx__DECLARE_GRIDEVT(COL_MOVE, id, fn) #define EVT_GRID_CMD_COL_SORT(id, fn) wx__DECLARE_GRIDEVT(COL_SORT, id, fn) #define EVT_GRID_CMD_RANGE_SELECT(id, fn) wx__DECLARE_GRIDRANGESELEVT(RANGE_SELECT, id, fn) @@ -2680,6 +2683,7 @@ typedef void (wxEvtHandler::*wxGridEditorCreatedEventFunction)(wxGridEditorCreat #define EVT_GRID_LABEL_RIGHT_DCLICK(fn) EVT_GRID_CMD_LABEL_RIGHT_DCLICK(wxID_ANY, fn) #define EVT_GRID_ROW_SIZE(fn) EVT_GRID_CMD_ROW_SIZE(wxID_ANY, fn) #define EVT_GRID_COL_SIZE(fn) EVT_GRID_CMD_COL_SIZE(wxID_ANY, fn) +#define EVT_GRID_COL_AUTO_SIZE(fn) EVT_GRID_CMD_COL_AUTO_SIZE(wxID_ANY, fn) #define EVT_GRID_COL_MOVE(fn) EVT_GRID_CMD_COL_MOVE(wxID_ANY, fn) #define EVT_GRID_COL_SORT(fn) EVT_GRID_CMD_COL_SORT(wxID_ANY, fn) #define EVT_GRID_RANGE_SELECT(fn) EVT_GRID_CMD_RANGE_SELECT(wxID_ANY, fn) diff --git a/interface/wx/grid.h b/interface/wx/grid.h index 1503e01e0f..8f4c93c81d 100644 --- a/interface/wx/grid.h +++ b/interface/wx/grid.h @@ -4707,6 +4707,13 @@ public: type. @event{EVT_GRID_COL_SIZE(func)} Same as EVT_GRID_CMD_COL_SIZE() but uses `wxID_ANY` id. + @event{EVT_GRID_COL_AUTO_SIZE(func)} + This event is sent when a column must be resized to its best size, e.g. + when the user double clicks the column divider. The default + implementation simply resizes the column to fit the column label (but + not its contents as this could be too slow for big grids). This macro + corresponds to @c wxEVT_GRID_COL_AUTO_SIZE event type and is new since + wxWidgets 2.9.5. @event{EVT_GRID_ROW_SIZE(func)} Same as EVT_GRID_CMD_ROW_SIZE() but uses `wxID_ANY` id. @endEventTable @@ -4920,6 +4927,7 @@ wxEventType wxEVT_GRID_LABEL_LEFT_DCLICK; wxEventType wxEVT_GRID_LABEL_RIGHT_DCLICK; wxEventType wxEVT_GRID_ROW_SIZE; wxEventType wxEVT_GRID_COL_SIZE; +wxEventType wxEVT_GRID_COL_AUTO_SIZE; wxEventType wxEVT_GRID_RANGE_SELECT; wxEventType wxEVT_GRID_CELL_CHANGING; wxEventType wxEVT_GRID_CELL_CHANGED; diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index f10c12a51c..9674b3ef79 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -226,6 +226,7 @@ BEGIN_EVENT_TABLE( GridFrame, wxFrame ) EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick ) EVT_GRID_ROW_SIZE( GridFrame::OnRowSize ) EVT_GRID_COL_SIZE( GridFrame::OnColSize ) + EVT_GRID_COL_AUTO_SIZE( GridFrame::OnColAutoSize ) EVT_GRID_SELECT_CELL( GridFrame::OnSelectCell ) EVT_GRID_RANGE_SELECT( GridFrame::OnRangeSelected ) EVT_GRID_CELL_CHANGING( GridFrame::OnCellValueChanging ) @@ -1174,6 +1175,21 @@ void GridFrame::OnColSize( wxGridSizeEvent& ev ) ev.Skip(); } +void GridFrame::OnColAutoSize( wxGridSizeEvent &event ) +{ + // Fit even-numbered columns to their contents while using the default + // behaviour for the odd-numbered ones to be able to see the difference. + int col = event.GetRowOrCol(); + if ( col % 2 ) + { + wxLogMessage("Auto-sizing column %d to fit its contents", col); + grid->AutoSizeColumn(col); + } + else + { + event.Skip(); + } +} void GridFrame::OnSelectCell( wxGridEvent& ev ) { diff --git a/samples/grid/griddemo.h b/samples/grid/griddemo.h index 1303018c97..749b06784f 100644 --- a/samples/grid/griddemo.h +++ b/samples/grid/griddemo.h @@ -98,6 +98,7 @@ class GridFrame : public wxFrame void OnCellLeftClick( wxGridEvent& ); void OnRowSize( wxGridSizeEvent& ); void OnColSize( wxGridSizeEvent& ); + void OnColAutoSize( wxGridSizeEvent& ); void OnSelectCell( wxGridEvent& ); void OnRangeSelected( wxGridRangeSelectEvent& ); void OnCellValueChanging( wxGridEvent& ); diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 9c795c276b..6bd231ea0b 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -146,6 +146,7 @@ wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE, wxGridSizeEvent ); wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE, wxGridSizeEvent ); +wxDEFINE_EVENT( wxEVT_GRID_COL_AUTO_SIZE, wxGridSizeEvent ); wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_COL_SORT, wxGridEvent ); wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT, wxGridRangeSelectEvent ); @@ -3720,7 +3721,8 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event ) // adjust column width depending on label text // // TODO: generate RESIZING event, see #10754 - AutoSizeColLabelSize( colEdge ); + if ( !SendGridSizeEvent(wxEVT_GRID_COL_AUTO_SIZE, -1, colEdge, event) ) + AutoSizeColLabelSize( colEdge ); SendGridSizeEvent(wxEVT_GRID_COL_SIZE, -1, colEdge, event); @@ -4642,7 +4644,7 @@ wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend)(size_t), // event generation helpers // ---------------------------------------------------------------------------- -void +bool wxGrid::SendGridSizeEvent(wxEventType type, int row, int col, const wxMouseEvent& mouseEv) @@ -4657,7 +4659,7 @@ wxGrid::SendGridSizeEvent(wxEventType type, mouseEv.GetY() + GetColLabelSize(), mouseEv); - GetEventHandler()->ProcessEvent(gridEvt); + return GetEventHandler()->ProcessEvent(gridEvt); } // Generate a grid event based on a mouse event and return: -- 2.45.2