Simplify printing and drawing sample constructors

Don't use FromDIP for 0.
Include DIP in drawing sample variable and function names to make it clear these sizes are in DIP.
Show both logical and DIP coordinates in statusbar.
This commit is contained in:
Maarten Bent 2022-04-23 18:09:50 +02:00
parent bb91534cb3
commit 8665960c55
3 changed files with 50 additions and 49 deletions

View file

@ -125,7 +125,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;
wxSize GetDIPDrawingSize() const;
void Draw(wxDC& dc);
@ -175,8 +175,7 @@ private:
#endif
bool m_useBuffer;
bool m_showBBox;
wxCoord m_sizeX;
wxCoord m_sizeY;
wxSize m_sizeDIP;
wxDECLARE_EVENT_TABLE();
};
@ -187,7 +186,7 @@ class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
MyFrame(const wxString& title);
// event handlers (these functions should _not_ be virtual)
void OnQuit(wxCommandEvent& event);
@ -461,8 +460,7 @@ bool MyApp::OnInit()
#endif
// Create the main application window
MyFrame *frame = new MyFrame("Drawing sample",
wxDefaultPosition, wxSize(550, 840));
MyFrame *frame = new MyFrame("Drawing sample");
// Show it
frame->Show(true);
@ -522,8 +520,7 @@ MyCanvas::MyCanvas(MyFrame *parent)
#endif
m_useBuffer = false;
m_showBBox = false;
m_sizeX = 0;
m_sizeY = 0;
m_sizeDIP = wxSize(0, 0);
}
void MyCanvas::DrawTestBrushes(wxDC& dc)
@ -587,7 +584,7 @@ void MyCanvas::DrawTestPoly(wxDC& dc)
"one entirely hatched", dc.FromDIP(10), dc.FromDIP(30));
dc.DrawText("The third star only has a hatched outline", dc.FromDIP(10), dc.FromDIP(50));
dc.DrawPolygon(WXSIZEOF(star), star, dc.FromDIP(0), dc.FromDIP(30));
dc.DrawPolygon(WXSIZEOF(star), star, 0, dc.FromDIP(30));
dc.DrawPolygon(WXSIZEOF(star), star, dc.FromDIP(160), dc.FromDIP(30), wxWINDING_RULE);
wxBrush brushHatchGreen(*wxGREEN, wxBRUSHSTYLE_FDIAGONAL_HATCH);
@ -1808,13 +1805,9 @@ void MyCanvas::DrawRegionsHelper(wxDC& dc, wxCoord x, bool firstTime)
}
}
void MyCanvas::GetDrawingSize(int* width, int* height) const
wxSize MyCanvas::GetDIPDrawingSize() const
{
if ( width )
*width = m_sizeX;
if ( height )
*height = m_sizeY;
return m_sizeDIP;
}
void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
@ -1895,8 +1888,8 @@ void MyCanvas::Draw(wxDC& pdc)
if ( m_clip )
{
int clipSize = dc.FromDIP(100);
dc.SetClippingRegion(clipSize, clipSize, clipSize, clipSize);
dc.SetClippingRegion(wxPoint(dc.FromDIP(100), dc.FromDIP(100)),
wxSize(dc.FromDIP(100), dc.FromDIP(100)));
}
dc.Clear();
@ -2000,8 +1993,8 @@ void MyCanvas::Draw(wxDC& pdc)
{
wxCoord x0, y0;
dc.GetDeviceOrigin(&x0, &y0);
m_sizeX = dc.ToDIP(dc.LogicalToDeviceX(dc.MaxX()) - x0) + 1;
m_sizeY = dc.ToDIP(dc.LogicalToDeviceY(dc.MaxY()) - y0) + 1;
m_sizeDIP.x = dc.ToDIP(dc.LogicalToDeviceX(dc.MaxX()) - x0) + 1;
m_sizeDIP.y = dc.ToDIP(dc.LogicalToDeviceY(dc.MaxY()) - y0) + 1;
}
}
@ -2013,9 +2006,12 @@ void MyCanvas::OnMouseMove(wxMouseEvent &event)
PrepareDC(dc);
m_owner->PrepareDC(dc);
wxPoint pos = dc.ToDIP(dc.DeviceToLogical(event.GetPosition()));
wxPoint pos = dc.DeviceToLogical(event.GetPosition());
wxPoint dipPos = dc.ToDIP(pos);
wxString str;
str.Printf( "Current mouse position: %d,%d", pos.x, pos.y );
str.Printf( "Mouse position: %d,%d", pos.x, pos.y );
if ( pos != dipPos )
str += wxString::Format("; DIP position: %d,%d", dipPos.x, dipPos.y);
m_owner->SetStatusText( str );
}
@ -2231,8 +2227,8 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
wxEND_EVENT_TABLE()
// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
// set the frame icon
SetIcon(wxICON(sample));
@ -2403,6 +2399,9 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
m_canvas = new MyCanvas( this );
m_canvas->SetScrollbars( 10, 10, 100, 240 );
SetSize(FromDIP(wxSize(800, 700)));
Center(wxBOTH);
}
// event handlers
@ -2468,8 +2467,7 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event))
wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (dlg.ShowModal() == wxID_OK)
{
int width, height;
m_canvas->GetDrawingSize(&width, &height);
wxSize canvasSize = m_canvas->GetDIPDrawingSize();
wxFileName fn(dlg.GetPath());
wxString ext = fn.GetExt().Lower();
#if wxUSE_SVG
@ -2484,7 +2482,11 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event))
wxGraphicsRenderer* tempRenderer = m_canvas->GetRenderer();
m_canvas->UseGraphicRenderer(NULL);
#endif
wxSVGFileDC svgdc(dlg.GetPath(), width, height, 72, "Drawing sample");
wxSVGFileDC svgdc(dlg.GetPath(),
canvasSize.GetWidth(),
canvasSize.GetHeight(),
72,
"Drawing sample");
svgdc.SetBitmapHandler(new wxSVGBitmapEmbedHandler());
m_canvas->Draw(svgdc);
#if wxUSE_GRAPHICS_CONTEXT
@ -2518,7 +2520,7 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event))
// Change the scale temporarily to fit the drawing into the page.
int w, h;
psdc.GetSize(&w, &h);
double sc = wxMin((double)w / width, (double)h / height);
double sc = wxMin((double)w / canvasSize.GetWidth(), (double)h / canvasSize.GetHeight());
m_xUserScale *= sc;
m_yUserScale *= sc;
psdc.StartDoc("Drawing sample");
@ -2539,7 +2541,7 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event))
#endif // wxUSE_POSTSCRIPT
{
wxBitmap bmp;
bmp.CreateWithDIPSize(wxSize(width, height), GetDPIScaleFactor());
bmp.CreateWithDIPSize(canvasSize, GetDPIScaleFactor());
wxMemoryDC mdc(bmp);
mdc.SetBackground(*wxWHITE_BRUSH);
mdc.Clear();