From 3d4850bdf39e729494aef3af4f0878ce01a504ab Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Fri, 3 Mar 2017 21:06:06 +0100 Subject: [PATCH] Use size of drawing area as a size of saved images in drawing sample Determine actual size of drawing area and use it (instead of virtual size of the window) as a dimension of the images to be saved. Thanks to this clipping, produced images contain only results of drawing operations and not lot of empty background space. --- samples/drawing/drawing.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index f6fb924b41..1d1d3f09da 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -128,6 +128,7 @@ public: #endif // wxUSE_GRAPHICS_CONTEXT void UseBuffer(bool use) { m_useBuffer = use; Refresh(); } void ShowBoundingBox(bool show) { m_showBBox = show; Refresh(); } + void GetDrawingSize(int* width, int* height) const; void Draw(wxDC& dc); @@ -175,6 +176,8 @@ private: #endif bool m_useBuffer; bool m_showBBox; + wxCoord m_sizeX; + wxCoord m_sizeY; wxDECLARE_EVENT_TABLE(); }; @@ -518,6 +521,8 @@ MyCanvas::MyCanvas(MyFrame *parent) #endif m_useBuffer = false; m_showBBox = false; + m_sizeX = 0; + m_sizeY = 0; } void MyCanvas::DrawTestBrushes(wxDC& dc) @@ -1710,6 +1715,15 @@ void MyCanvas::DrawRegionsHelper(wxDC& dc, wxCoord x, bool firstTime) } } +void MyCanvas::GetDrawingSize(int* width, int* height) const +{ + if ( width ) + *width = m_sizeX; + + if ( height ) + *height = m_sizeY; +} + void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event)) { if ( m_useBuffer ) @@ -1760,7 +1774,12 @@ void MyCanvas::Draw(wxDC& pdc) wxDC &dc = pdc ; #endif - PrepareDC(dc); + // Adjust scrolled contents for screen drawing operations only. + if ( wxDynamicCast(&pdc, wxBufferedPaintDC) || + wxDynamicCast(&pdc, wxPaintDC) ) + { + PrepareDC(dc); + } m_owner->PrepareDC(dc); @@ -1870,6 +1889,16 @@ void MyCanvas::Draw(wxDC& pdc) dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.DrawRectangle(dc.MinX(), dc.MinY(), dc.MaxX()-dc.MinX()+1, dc.MaxY()-dc.MinY()+1); } + + // Adjust drawing area dimensions only if screen drawing is invoked. + if ( wxDynamicCast(&pdc, wxBufferedPaintDC) || + wxDynamicCast(&pdc, wxPaintDC) ) + { + wxCoord x0, y0; + dc.GetDeviceOrigin(&x0, &y0); + m_sizeX = dc.LogicalToDeviceX(dc.MaxX()) - x0 + 1; + m_sizeY = dc.LogicalToDeviceY(dc.MaxY()) - y0 + 1; + } } void MyCanvas::OnMouseMove(wxMouseEvent &event) @@ -2207,7 +2236,7 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event)) if (dlg.ShowModal() == wxID_OK) { int width, height; - m_canvas->GetVirtualSize(&width, &height); + m_canvas->GetDrawingSize(&width, &height); #if wxUSE_SVG wxString ext = dlg.GetPath().AfterLast('.').Lower(); if (ext.Lower() == wxT("svg"))