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
This commit is contained in:
Vadim Zeitlin 2013-04-05 21:11:59 +00:00
parent 8a47731406
commit 6f58f3d7e0
6 changed files with 37 additions and 5 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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;

View File

@ -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 )
{

View File

@ -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& );

View File

@ -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: