]> git.saurik.com Git - wxWidgets.git/commitdiff
Allow changing horizontal alignment of numeric cells in wxGrid.
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 27 Nov 2009 01:47:38 +0000 (01:47 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 27 Nov 2009 01:47:38 +0000 (01:47 +0000)
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

docs/changes.txt
include/wx/generic/grid.h
interface/wx/grid.h
samples/grid/griddemo.cpp
src/generic/grid.cpp
src/generic/gridctrl.cpp

index 2c586993cb2a2967d299e2d1c4a824604d2c7a24..dda8f8f2794a815c09091d6d3295302fe5b25358 100644 (file)
@@ -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.
index 32c3f39e53b8e483fb31e672ce35e4cf7c5f99ca..2f1877c215b7bd51600321357c9e8282e96f6c8d 100644 (file)
@@ -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; }
index 27cd239a6e31ec252de77ebe9824c3884f1dc1dd..65ae9e76ced68b5657b83928178d85af6ed0856f 100644 (file)
@@ -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.
     */
index 34518418f6b25589ed151dbd14e0599d9e09a006..89b6030fefaa15f567d42f3d693fb9fb34ec0c9a 100644 (file)
@@ -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[] =
     {
index e0d9dc6c379958234f733e7dc23a7a4385331fc6..04fbd31efef7c11e553c21bac314f01836f87aef 100644 (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
 {
     if ( num_rows )
index e4d2608d211e2376ef06a44c2e7aaf0bf7e14578..fdf63a20ffc758de6e948e07c7737ff4b938ea37 100644 (file)
@@ -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);