SVN:(VZ)[71577] Add wxGrid::Render() for drawing the grid to any wxDC.

36 views
Skip to first unread message

nor...@wxsite.net

unread,
May 27, 2012, 9:00:19 AM5/27/12
to wx-commi...@googlegroups.com
Revision
71577
Author
VZ
Date
2012-05-27 06:00:19 -0700 (Sun, 27 May 2012)

Log Message

Add wxGrid::Render() for drawing the grid to any wxDC.

In particular, this allows to print the grid contents easily.

Closes 14294.

Modified Paths

Diff

Modified: wxWidgets/trunk/docs/changes.txt (71576 => 71577)


--- wxWidgets/trunk/docs/changes.txt	2012-05-27 13:00:13 UTC (rev 71576)
+++ wxWidgets/trunk/docs/changes.txt	2012-05-27 13:00:19 UTC (rev 71577)
@@ -526,6 +526,7 @@
 
 All (GUI):
 
+- Add wxGrid::Render() for printing wxGrid (John Roberts).
 - Added strike-through support to wxFont (Igor Korot).
 - Add support for horizontal mouse wheel events to MSW and GTK (Lauri Nurmi).
 - Fix infinite loop in wxHtmlEasyPrinting when trying to page break images

Modified: wxWidgets/trunk/include/wx/generic/grid.h (71576 => 71577)


--- wxWidgets/trunk/include/wx/generic/grid.h	2012-05-27 13:00:13 UTC (rev 71576)
+++ wxWidgets/trunk/include/wx/generic/grid.h	2012-05-27 13:00:19 UTC (rev 71577)
@@ -65,6 +65,20 @@
     wxGRID_ROW
 };
 
+// Flags used with wxGrid::Render() to select parts of the grid to draw.
+enum wxGridRenderStyle
+{
+    wxGRID_DRAW_ROWS_HEADER = 0x001,
+    wxGRID_DRAW_COLS_HEADER = 0x002,
+    wxGRID_DRAW_CELL_LINES = 0x004,
+    wxGRID_DRAW_BOX_RECT = 0x008,
+    wxGRID_DRAW_SELECTION = 0x010,
+    wxGRID_DRAW_DEFAULT = wxGRID_DRAW_ROWS_HEADER |
+                          wxGRID_DRAW_COLS_HEADER |
+                          wxGRID_DRAW_CELL_LINES |
+                          wxGRID_DRAW_BOX_RECT
+};
+
 // ----------------------------------------------------------------------------
 // forward declarations
 // ----------------------------------------------------------------------------
@@ -1030,6 +1044,14 @@
                             int verticalAlignment = wxALIGN_TOP,
                             int textOrientation = wxHORIZONTAL ) const;
 
+    // ------ grid render function for printing
+    //
+    void Render( wxDC& dc,
+                 const wxPoint& pos = wxDefaultPosition,
+                 const wxSize& size = wxDefaultSize,
+                 const wxGridCellCoords& topLeft = wxGridCellCoords(-1, -1),
+                 const wxGridCellCoords& bottomRight = wxGridCellCoords(-1, -1),
+                 int style = wxGRID_DRAW_DEFAULT );
 
     // Split a string containing newline characters into an array of
     // strings and return the number of lines
@@ -2253,7 +2275,11 @@
     bool DoCanResizeLine(int line, const wxGridFixedIndicesSet *setFixed) const;
 
 
+    // Helper of Render(): set the scale to draw the cells at the right size.
+    void SetRenderScale( wxDC& dc, const wxPoint& pos, const wxSize& size,
+                         int gridWidth, int gridHeight );
 
+
     // these sets contain the indices of fixed, i.e. non-resizable
     // interactively, grid rows or columns and are NULL if there are no fixed
     // elements (which is the default)

Modified: wxWidgets/trunk/interface/wx/grid.h (71576 => 71577)


--- wxWidgets/trunk/interface/wx/grid.h	2012-05-27 13:00:13 UTC (rev 71576)
+++ wxWidgets/trunk/interface/wx/grid.h	2012-05-27 13:00:19 UTC (rev 71577)
@@ -1851,6 +1851,50 @@
     };
 
     /**
+        Rendering styles supported by wxGrid::Render() method.
+
+        @since 2.9.4
+     */
+    enum wxGridRenderStyle
+    {
+        /// Draw grid row header labels.
+        wxGRID_DRAW_ROWS_HEADER = 0x001,
+
+        /// Draw grid column header labels.
+        wxGRID_DRAW_COLS_HEADER = 0x002,
+
+        /// Draw grid cell border lines.
+        wxGRID_DRAW_CELL_LINES = 0x004,
+
+        /**
+            Draw a bounding rectangle around the rendered cell area.
+
+            Useful where row or column headers are not drawn or where there is
+            multi row or column cell clipping and therefore no cell border at
+            the rendered outer boundary.
+        */
+        wxGRID_DRAW_BOX_RECT = 0x008,
+
+        /**
+            Draw the grid cell selection highlight if a selection is present.
+
+            At present the highlight colour drawn depends on whether the grid
+            window loses focus before drawing begins.
+        */
+        wxGRID_DRAW_SELECTION = 0x010,
+
+        /**
+            The default render style.
+
+            Includes all except wxGRID_DRAW_SELECTION.
+         */
+        wxGRID_DRAW_DEFAULT = wxGRID_DRAW_ROWS_HEADER |
+                              wxGRID_DRAW_COLS_HEADER |
+                              wxGRID_DRAW_CELL_LINES |
+                              wxGRID_DRAW_BOX_RECT
+    };
+
+    /**
         @name Constructors and Initialization
      */
     //@{
@@ -4003,6 +4047,39 @@
     void RefreshAttr(int row, int col);
 
     /**
+        Draws part or all of a wxGrid on a wxDC for printing or display.
+
+        Pagination can be accomplished by using sequential Render() calls
+        with appropriate values in wxGridCellCoords topLeft and bottomRight.
+
+        @param dc
+            The wxDC to be drawn on.
+        @param pos
+            The position on the wxDC where rendering should begin. If not
+            specified drawing will begin at the wxDC MaxX() and MaxY().
+        @param size
+            The size of the area on the wxDC that the rendered wxGrid should
+            occupy. If not specified the drawing will be scaled to fit the
+            available dc width or height. The wxGrid's aspect ratio is
+            maintained whether or not size is specified.
+        @param topLeft
+            The top left cell of the block to be drawn. Defaults to ( 0, 0 ).
+        @param bottomRight
+            The bottom right cell of the block to be drawn. Defaults to row and
+            column counts.
+        @param style
+            A combination of values from wxGridRenderStyle.
+
+        @since 2.9.4
+     */
+    void Render( wxDC& dc,
+                 const wxPoint& pos = wxDefaultPosition,
+                 const wxSize& size = wxDefaultSize,
+                 const wxGridCellCoords& topLeft = wxGridCellCoords( -1, -1 ),
+                 const wxGridCellCoords& bottomRight = wxGridCellCoords( -1, -1 ),
+                 int style = wxGRID_DRAW_DEFAULT );
+
+    /**
         Sets the cell attributes for all cells in the specified column.
 
         For more information about controlling grid cell attributes see the

Modified: wxWidgets/trunk/samples/grid/griddemo.cpp (71576 => 71577)


--- wxWidgets/trunk/samples/grid/griddemo.cpp	2012-05-27 13:00:13 UTC (rev 71576)
+++ wxWidgets/trunk/samples/grid/griddemo.cpp	2012-05-27 13:00:19 UTC (rev 71577)
@@ -212,6 +212,9 @@
     EVT_MENU( ID_SET_HIGHLIGHT_WIDTH, GridFrame::OnSetHighlightWidth)
     EVT_MENU( ID_SET_RO_HIGHLIGHT_WIDTH, GridFrame::OnSetROHighlightWidth)
 
+    EVT_MENU( wxID_PRINT, GridFrame::OnGridRender )
+    EVT_MENU( ID_RENDER_COORDS, GridFrame::OnGridRender )
+
     EVT_GRID_LABEL_LEFT_CLICK( GridFrame::OnLabelLeftClick )
     EVT_GRID_CELL_LEFT_CLICK( GridFrame::OnCellLeftClick )
     EVT_GRID_ROW_SIZE( GridFrame::OnRowSize )
@@ -239,6 +242,38 @@
     fileMenu->Append( ID_BUGS_TABLE, wxT("&Bugs table test\tCtrl-B"));
     fileMenu->Append( ID_TABULAR_TABLE, wxT("&Tabular table test\tCtrl-T"));
     fileMenu->AppendSeparator();
+
+    wxMenu* setupMenu = new wxMenu;
+    wxMenuItem* item;
+    item = setupMenu->AppendCheckItem( ID_RENDER_ROW_LABEL,
+                                       "Render row labels" );
+    item->Check();
+    item = setupMenu->AppendCheckItem( ID_RENDER_COL_LABEL,
+                                       "Render column labels" );
+    item->Check();
+    item = setupMenu->AppendCheckItem( ID_RENDER_GRID_LINES,
+                                       "Render grid cell lines" );
+    item->Check();
+    item = setupMenu->AppendCheckItem( ID_RENDER_GRID_BORDER,
+                                       "Render border" );
+    item->Check();
+    item = setupMenu->AppendCheckItem( ID_RENDER_SELECT_HLIGHT,
+                                       "Render selection highlight" );
+    setupMenu->AppendSeparator();
+    setupMenu->AppendCheckItem( ID_RENDER_LOMETRIC,
+                                       "Use LOMETRIC mapping mode" );
+    setupMenu->AppendCheckItem( ID_RENDER_DEFAULT_SIZE,
+                                       "Use wxDefaultSize" );
+    setupMenu->AppendCheckItem( ID_RENDER_MARGIN,
+                                "Logical 50 unit margin" );
+    setupMenu->AppendCheckItem( ID_RENDER_ZOOM,
+                                "Zoom 125%" );
+
+    fileMenu->AppendSubMenu( setupMenu, "Render setup" );
+    fileMenu->Append( wxID_PRINT, "Render" );
+    fileMenu->Append( ID_RENDER_COORDS, "Render G5:P30" );
+
+    fileMenu->AppendSeparator();
     fileMenu->Append( wxID_EXIT, wxT("E&xit\tAlt-X") );
 
     wxMenu *viewMenu = new wxMenu;
@@ -2068,3 +2103,158 @@
 {
     new TabularGridFrame;
 }
+
+// Example using wxGrid::Render
+// Displays a preset selection or, if it exists, a selection block
+// Draws the selection to a wxBitmap and displays the bitmap
+void GridFrame::OnGridRender( wxCommandEvent& event )
+{
+    int styleRender = 0, i;
+    bool useLometric = false, defSize = false;
+    double zoom = 1;
+    wxSize sizeMargin( 0, 0 );
+    wxPoint pointOrigin( 0, 0 );
+
+    wxMenu* menu = GetMenuBar()->GetMenu( 0 );
+    wxMenuItem* menuItem = menu->FindItem( ID_RENDER_ROW_LABEL );
+    menu = menuItem->GetMenu();
+
+    if ( menu->FindItem( ID_RENDER_ROW_LABEL )->IsChecked() )
+        styleRender |= wxGRID_DRAW_ROWS_HEADER;
+    if ( menu->FindItem( ID_RENDER_COL_LABEL )->IsChecked() )
+        styleRender |= wxGRID_DRAW_COLS_HEADER;
+    if ( menu->FindItem( ID_RENDER_GRID_LINES )->IsChecked() )
+        styleRender |= wxGRID_DRAW_CELL_LINES;
+    if ( menu->FindItem( ID_RENDER_GRID_BORDER )->IsChecked() )
+        styleRender |= wxGRID_DRAW_BOX_RECT;
+    if ( menu->FindItem( ID_RENDER_SELECT_HLIGHT )->IsChecked() )
+        styleRender |= wxGRID_DRAW_SELECTION;
+    if ( menu->FindItem( ID_RENDER_LOMETRIC )->IsChecked() )
+        useLometric = true;
+    if ( menu->FindItem( ID_RENDER_MARGIN )->IsChecked() )
+    {
+        pointOrigin.x += 50;
+        pointOrigin.y += 50;
+        sizeMargin.IncBy( 50 );
+    }
+    if ( menu->FindItem( ID_RENDER_ZOOM )->IsChecked() )
+        zoom = 1.25;
+    if ( menu->FindItem( ID_RENDER_DEFAULT_SIZE )->IsChecked() )
+        defSize = true;
+
+    // init render area coords with a default row and col selection
+    wxGridCellCoords topLeft( 0, 0 ), bottomRight( 8, 6 );
+    // check whether we are printing a block selection
+    // other selection types not catered for here
+    if ( event.GetId() == ID_RENDER_COORDS )
+    {
+        topLeft.SetCol( 6 );
+        topLeft.SetRow( 4 );
+        bottomRight.SetCol( 15 );
+        bottomRight.SetRow( 29 );
+    }
+    else if ( grid->IsSelection() && grid->GetSelectionBlockTopLeft().Count() )
+    {
+        wxGridCellCoordsArray cells = grid->GetSelectionBlockTopLeft();
+        if ( grid->GetSelectionBlockBottomRight().Count() )
+        {
+            cells.Add( grid->GetSelectionBlockBottomRight()[ 0 ] );
+            topLeft.Set( cells[ 0 ].GetRow(),
+                            cells[ 0 ].GetCol() );
+            bottomRight.Set( cells[ 1 ].GetRow(),
+                                cells[ 1 ].GetCol() );
+        }
+    }
+
+    // sum col widths
+    wxSize sizeRender( 0, 0 );
+    wxGridSizesInfo sizeinfo = grid->GetColSizes();
@@ Diff output truncated at 10240 characters. @@
Reply all
Reply to author
Forward
0 new messages