From 8665960c5567d33d18528653def60887b8c6848e Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sat, 23 Apr 2022 18:09:50 +0200 Subject: [PATCH] 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. --- samples/drawing/drawing.cpp | 58 ++++++++++++++++++----------------- samples/printing/printing.cpp | 37 +++++++++++----------- samples/printing/printing.h | 4 +-- 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index 366c408e1a..b190c1e6c8 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -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(); diff --git a/samples/printing/printing.cpp b/samples/printing/printing.cpp index d6c7bfa221..8d8b4a361d 100644 --- a/samples/printing/printing.cpp +++ b/samples/printing/printing.cpp @@ -110,10 +110,7 @@ bool MyApp::OnInit(void) // Create the main frame window // ---------------------------- - MyFrame* frame = new MyFrame((wxFrame *) NULL, "wxWidgets Printing Demo", - wxDefaultPosition, wxDefaultSize); - frame->SetSize(frame->FromDIP(wxSize(400, 400))); - frame->Centre(wxBOTH); + MyFrame* frame = new MyFrame("wxWidgets Printing Demo"); frame->Show(); return true; @@ -142,15 +139,15 @@ void MyApp::Draw(wxDC&dc) dc.SetPen(*wxBLACK_PEN); dc.SetBrush(*wxLIGHT_GREY_BRUSH); - dc.DrawRectangle(dc.FromDIP(0), dc.FromDIP(0), dc.FromDIP(230), dc.FromDIP(350)); - dc.DrawLine(dc.FromDIP(0), dc.FromDIP(0), dc.FromDIP(229), dc.FromDIP(349)); - dc.DrawLine(dc.FromDIP(229), dc.FromDIP(0), dc.FromDIP(0), dc.FromDIP(349)); + dc.DrawRectangle(0, 0, dc.FromDIP(230), dc.FromDIP(350)); + dc.DrawLine(0, 0, dc.FromDIP(229), dc.FromDIP(349)); + dc.DrawLine(dc.FromDIP(229), 0, 0, dc.FromDIP(349)); dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.SetBrush(*wxCYAN_BRUSH); dc.SetPen(*wxRED_PEN); - dc.DrawRoundedRectangle(dc.FromDIP(0), dc.FromDIP(20), dc.FromDIP(200), dc.FromDIP(80), 20); + dc.DrawRoundedRectangle(0, dc.FromDIP(20), dc.FromDIP(200), dc.FromDIP(80), 20); dc.DrawText( "Rectangle 200 by 80", dc.FromDIP(40), dc.FromDIP(40)); @@ -169,10 +166,10 @@ void MyApp::Draw(wxDC&dc) #endif wxPoint points[5]; - points[0].x = dc.FromDIP(0); - points[0].y = dc.FromDIP(0); + points[0].x = 0; + points[0].y = 0; points[1].x = dc.FromDIP(20); - points[1].y = dc.FromDIP(0); + points[1].y = 0; points[2].x = dc.FromDIP(20); points[2].y = dc.FromDIP(20); points[3].x = dc.FromDIP(10); @@ -277,8 +274,8 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) MyFrame::OnPreviewFrameModalityKind) wxEND_EVENT_TABLE() -MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const wxSize&size) - : wxFrame(frame, wxID_ANY, title, pos, size) +MyFrame::MyFrame(const wxString& title) + : wxFrame(NULL, wxID_ANY, title) { m_canvas = NULL; m_previewModality = wxPreviewFrame_AppModal; @@ -343,11 +340,13 @@ MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const // create the canvas // ----------------- - m_canvas = new MyCanvas(this, wxPoint(0, 0), FromDIP(wxSize(100, 100)), - wxRETAINED|wxHSCROLL|wxVSCROLL); + m_canvas = new MyCanvas(this, wxRETAINED | wxHSCROLL | wxVSCROLL); // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction m_canvas->SetScrollbars(20, 20, 50, 50); + + SetSize(FromDIP(wxSize(400, 400))); + Centre(wxBOTH); } void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) @@ -392,7 +391,7 @@ void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event)) } wxPreviewFrame *frame = - new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), FromDIP(wxSize(600, 650))); + new wxPreviewFrame(preview, this, "Demo Print Preview", wxDefaultPosition, FromDIP(wxSize(600, 700))); frame->Centre(wxBOTH); frame->InitializeWithModality(m_previewModality); frame->Show(); @@ -427,7 +426,7 @@ void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event)) wxPrintDialogData printDialogData(* g_printData); wxPrintPreview *preview = new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData); wxPreviewFrame *frame = - new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), FromDIP(wxSize(600, 650))); + new wxPreviewFrame(preview, this, "Demo Print Preview", wxDefaultPosition, FromDIP(wxSize(600, 700))); frame->Centre(wxBOTH); frame->Initialize(); frame->Show(); @@ -491,8 +490,8 @@ wxBEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) // EVT_PAINT(MyCanvas::OnPaint) wxEND_EVENT_TABLE() -MyCanvas::MyCanvas(wxFrame *frame, const wxPoint&pos, const wxSize&size, long style) - : wxScrolledWindow(frame, wxID_ANY, pos, size, style) +MyCanvas::MyCanvas(wxFrame *frame, long style) + : wxScrolledWindow(frame, wxID_ANY, wxDefaultPosition, wxDefaultSize, style) { SetBackgroundColour(*wxWHITE); } diff --git a/samples/printing/printing.h b/samples/printing/printing.h index 7d04618673..46747cc2bc 100644 --- a/samples/printing/printing.h +++ b/samples/printing/printing.h @@ -40,7 +40,7 @@ class MyCanvas; class MyFrame: public wxFrame { public: - MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size); + MyFrame(const wxString& title); void OnAngleUp(wxCommandEvent& event); void OnAngleDown(wxCommandEvent& event); @@ -73,7 +73,7 @@ private: class MyCanvas: public wxScrolledWindow { public: - MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style = wxRETAINED); + MyCanvas(wxFrame *frame, long style); //void OnPaint(wxPaintEvent& evt); virtual void OnDraw(wxDC& dc) wxOVERRIDE;