Allow changing horizontal alignment of numeric cells in wxGrid.

wxGridCellAttr didn't provide any way to query its alignment attributes
without falling back to the (always defined) default alignment so the code in
wxGridCellNumberRenderer and similar classes simply always used right
alignment,

Add a new wxGridCellAttr::GetNonDefaultAlignment() function which allows to
retrieve the alignment defined in the attribute and use it to use right
alignment by default but allow overriding it.

Add a test to the sample showing a non right-aligned numeric cell.

Incidentally fix a long-standing bug in wxGridCell{DateTime,Enum}Renderers
which used wxRIGHT instead of wxALIGN_RIGHT and so were not aligned properly
even by default.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62728 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-11-27 01:47:38 +00:00
parent ba0185b557
commit cfbc15ee04
6 changed files with 71 additions and 13 deletions

View File

@@ -450,6 +450,7 @@ All (GUI):
- Added wxEditableListBox XRC handler. - Added wxEditableListBox XRC handler.
- Added multiple selection support to wxDirCtrl (Steve Lamerton). - Added multiple selection support to wxDirCtrl (Steve Lamerton).
- wxGrid: add possibility to prevent resizing of individual rows/columns. - 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). - wxHTML: add support for table borders width (Laurent Humbertclaude).
- Added wxMouseEventsManager. - Added wxMouseEventsManager.
- Building OpenGL library is now enabled by default. - Building OpenGL library is now enabled by default.

View File

@@ -449,6 +449,15 @@ public:
const wxColour& GetBackgroundColour() const; const wxColour& GetBackgroundColour() const;
const wxFont& GetFont() const; const wxFont& GetFont() const;
void GetAlignment(int *hAlign, int *vAlign) 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; void GetSize(int *num_rows, int *num_cols) const;
bool GetOverflow() const bool GetOverflow() const
{ return m_overflow != SingleCell; } { return m_overflow != SingleCell; }

View File

@@ -527,7 +527,21 @@ public:
void DecRef(); 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; void GetAlignment(int* hAlign, int* vAlign) const;
@@ -546,6 +560,29 @@ public:
*/ */
const wxFont& GetFont() const; 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. Returns the cell renderer.
*/ */

View File

@@ -456,6 +456,8 @@ GridFrame::GridFrame()
grid->SetCellValue(0, 8, "17"); grid->SetCellValue(0, 8, "17");
grid->SetCellValue(1, 8, "0"); grid->SetCellValue(1, 8, "0");
grid->SetCellValue(2, 8, "-666"); 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[] = const wxString choices[] =
{ {

View File

@@ -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 void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const
{ {
if ( num_rows ) if ( num_rows )

View File

@@ -145,9 +145,9 @@ void wxGridCellDateTimeRenderer::Draw(wxGrid& grid,
SetTextColoursAndFont(grid, attr, dc, isSelected); SetTextColoursAndFont(grid, attr, dc, isSelected);
// draw the text right aligned by default // draw the text right aligned by default
int hAlign, vAlign; int hAlign = wxALIGN_RIGHT,
attr.GetAlignment(&hAlign, &vAlign); vAlign = wxALIGN_INVALID;
hAlign = wxRIGHT; attr.GetNonDefaultAlignment(&hAlign, &vAlign);
wxRect rect = rectCell; wxRect rect = rectCell;
rect.Inflate(-1); rect.Inflate(-1);
@@ -222,9 +222,9 @@ void wxGridCellEnumRenderer::Draw(wxGrid& grid,
SetTextColoursAndFont(grid, attr, dc, isSelected); SetTextColoursAndFont(grid, attr, dc, isSelected);
// draw the text right aligned by default // draw the text right aligned by default
int hAlign, vAlign; int hAlign = wxALIGN_RIGHT,
attr.GetAlignment(&hAlign, &vAlign); vAlign = wxALIGN_INVALID;
hAlign = wxRIGHT; attr.GetNonDefaultAlignment(&hAlign, &vAlign);
wxRect rect = rectCell; wxRect rect = rectCell;
rect.Inflate(-1); rect.Inflate(-1);
@@ -576,9 +576,9 @@ void wxGridCellNumberRenderer::Draw(wxGrid& grid,
SetTextColoursAndFont(grid, attr, dc, isSelected); SetTextColoursAndFont(grid, attr, dc, isSelected);
// draw the text right aligned by default // draw the text right aligned by default
int hAlign, vAlign; int hAlign = wxALIGN_RIGHT,
attr.GetAlignment(&hAlign, &vAlign); vAlign = wxALIGN_INVALID;
hAlign = wxALIGN_RIGHT; attr.GetNonDefaultAlignment(&hAlign, &vAlign);
wxRect rect = rectCell; wxRect rect = rectCell;
rect.Inflate(-1); rect.Inflate(-1);
@@ -679,9 +679,9 @@ void wxGridCellFloatRenderer::Draw(wxGrid& grid,
SetTextColoursAndFont(grid, attr, dc, isSelected); SetTextColoursAndFont(grid, attr, dc, isSelected);
// draw the text right aligned by default // draw the text right aligned by default
int hAlign, vAlign; int hAlign = wxALIGN_RIGHT,
attr.GetAlignment(&hAlign, &vAlign); vAlign = wxALIGN_INVALID;
hAlign = wxALIGN_RIGHT; attr.GetNonDefaultAlignment(&hAlign, &vAlign);
wxRect rect = rectCell; wxRect rect = rectCell;
rect.Inflate(-1); rect.Inflate(-1);