diff --git a/docs/changes.txt b/docs/changes.txt index 2c586993cb..dda8f8f279 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -450,6 +450,7 @@ All (GUI): - Added wxEditableListBox XRC handler. - Added multiple selection support to wxDirCtrl (Steve Lamerton). - wxGrid: add possibility to prevent resizing of individual rows/columns. +- wxGrid: allow changing the horizontal alignment of numeric cells. - wxHTML: add support for table borders width (Laurent Humbertclaude). - Added wxMouseEventsManager. - Building OpenGL library is now enabled by default. diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 32c3f39e53..2f1877c215 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -449,6 +449,15 @@ public: const wxColour& GetBackgroundColour() const; const wxFont& GetFont() const; void GetAlignment(int *hAlign, int *vAlign) const; + + // unlike GetAlignment() which always overwrites its output arguments with + // the alignment values to use, falling back on default alignment if this + // attribute doesn't have any, this function will preserve the values of + // parameters on entry if the corresponding alignment is not set in this + // attribute meaning that they can be initialized to default alignment (and + // also that they must be initialized, unlike with GetAlignment()) + void GetNonDefaultAlignment(int *hAlign, int *vAlign) const; + void GetSize(int *num_rows, int *num_cols) const; bool GetOverflow() const { return m_overflow != SingleCell; } diff --git a/interface/wx/grid.h b/interface/wx/grid.h index 27cd239a6e..65ae9e76ce 100644 --- a/interface/wx/grid.h +++ b/interface/wx/grid.h @@ -527,7 +527,21 @@ public: void DecRef(); /** - See SetAlignment() for the returned values. + Get the alignment to use for the cell with the given attribute. + + If this attribute doesn't specify any alignment, the default attribute + alignment is used (which can be changed using + wxGrid::SetDefaultCellAlignment() but is left and top by default). + + Notice that @a hAlign and @a vAlign values are always overwritten by + this function, use GetNonDefaultAlignment() if this is not desirable. + + @param hAlign + Horizontal alignment is returned here if this argument is non-@NULL. + It is one of wxALIGN_LEFT, wxALIGN_CENTRE or wxALIGN_RIGHT. + @param vAlign + Vertical alignment is returned here if this argument is non-@NULL. + It is one of wxALIGN_TOP, wxALIGN_CENTRE or wxALIGN_BOTTOM. */ void GetAlignment(int* hAlign, int* vAlign) const; @@ -546,6 +560,29 @@ public: */ const wxFont& GetFont() const; + /** + Get the alignment defined by this attribute. + + Unlike GetAlignment() this function only modifies @a hAlign and @a + vAlign if this attribute does define a non-default alignment. This + means that they must be initialized before calling this function and + that their values will be preserved unchanged if they are different + from wxALIGN_INVALID. + + For example, the following fragment can be used to use the cell + alignment if one is defined but right-align its contents by default + (instead of left-aligning it by default) while still using the default + vertical alignment: + @code + int hAlign = wxALIGN_RIGHT, + vAlign = wxALIGN_INVALID; + attr.GetNonDefaultAlignment(&hAlign, &vAlign); + @endcode + + @since 2.9.1 + */ + void GetNonDefaultAlignment(int *hAlign, int *vAlign) const; + /** Returns the cell renderer. */ diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index 34518418f6..89b6030fef 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -456,6 +456,8 @@ GridFrame::GridFrame() grid->SetCellValue(0, 8, "17"); grid->SetCellValue(1, 8, "0"); grid->SetCellValue(2, 8, "-666"); + grid->SetCellAlignment(2, 8, wxALIGN_CENTRE, wxALIGN_TOP); + grid->SetCellValue(2, 9, "<- This numeric cell should be centred"); const wxString choices[] = { diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index e0d9dc6c37..04fbd31efe 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -476,6 +476,15 @@ void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const } } +void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign, int *vAlign) const +{ + if ( hAlign && m_hAlign != wxALIGN_INVALID ) + *hAlign = m_hAlign; + + if ( vAlign && m_vAlign != wxALIGN_INVALID ) + *vAlign = m_vAlign; +} + void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const { if ( num_rows ) diff --git a/src/generic/gridctrl.cpp b/src/generic/gridctrl.cpp index e4d2608d21..fdf63a20ff 100644 --- a/src/generic/gridctrl.cpp +++ b/src/generic/gridctrl.cpp @@ -145,9 +145,9 @@ void wxGridCellDateTimeRenderer::Draw(wxGrid& grid, SetTextColoursAndFont(grid, attr, dc, isSelected); // draw the text right aligned by default - int hAlign, vAlign; - attr.GetAlignment(&hAlign, &vAlign); - hAlign = wxRIGHT; + int hAlign = wxALIGN_RIGHT, + vAlign = wxALIGN_INVALID; + attr.GetNonDefaultAlignment(&hAlign, &vAlign); wxRect rect = rectCell; rect.Inflate(-1); @@ -222,9 +222,9 @@ void wxGridCellEnumRenderer::Draw(wxGrid& grid, SetTextColoursAndFont(grid, attr, dc, isSelected); // draw the text right aligned by default - int hAlign, vAlign; - attr.GetAlignment(&hAlign, &vAlign); - hAlign = wxRIGHT; + int hAlign = wxALIGN_RIGHT, + vAlign = wxALIGN_INVALID; + attr.GetNonDefaultAlignment(&hAlign, &vAlign); wxRect rect = rectCell; rect.Inflate(-1); @@ -576,9 +576,9 @@ void wxGridCellNumberRenderer::Draw(wxGrid& grid, SetTextColoursAndFont(grid, attr, dc, isSelected); // draw the text right aligned by default - int hAlign, vAlign; - attr.GetAlignment(&hAlign, &vAlign); - hAlign = wxALIGN_RIGHT; + int hAlign = wxALIGN_RIGHT, + vAlign = wxALIGN_INVALID; + attr.GetNonDefaultAlignment(&hAlign, &vAlign); wxRect rect = rectCell; rect.Inflate(-1); @@ -679,9 +679,9 @@ void wxGridCellFloatRenderer::Draw(wxGrid& grid, SetTextColoursAndFont(grid, attr, dc, isSelected); // draw the text right aligned by default - int hAlign, vAlign; - attr.GetAlignment(&hAlign, &vAlign); - hAlign = wxALIGN_RIGHT; + int hAlign = wxALIGN_RIGHT, + vAlign = wxALIGN_INVALID; + attr.GetNonDefaultAlignment(&hAlign, &vAlign); wxRect rect = rectCell; rect.Inflate(-1);