]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/prntbase.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[wxWidgets.git] / src / common / prntbase.cpp
index 57157aebd21bba92aded78d154039067c4170a2f..e71e729e054e278c55317921d70e99957118388f 100644 (file)
@@ -32,6 +32,7 @@
     #include "wx/layout.h"
     #include "wx/choice.h"
     #include "wx/button.h"
+    #include "wx/bmpbuttn.h"
     #include "wx/settings.h"
     #include "wx/dcmemory.h"
     #include "wx/dcclient.h"
@@ -320,21 +321,9 @@ wxPrinterBase::~wxPrinterBase()
 {
 }
 
-wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout)
+wxPrintAbortDialog *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout)
 {
-    wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing ") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
-
-    wxBoxSizer *button_sizer = new wxBoxSizer( wxVERTICAL );
-    button_sizer->Add( new wxStaticText(dialog, wxID_ANY, _("Please wait while printing\n") + printout->GetTitle() ), 0, wxALL, 10 );
-    button_sizer->Add( new wxButton( dialog, wxID_CANCEL, wxT("Cancel") ), 0, wxALL | wxALIGN_CENTER, 10 );
-
-    dialog->SetAutoLayout( true );
-    dialog->SetSizer( button_sizer );
-
-    button_sizer->Fit(dialog);
-    button_sizer->SetSizeHints (dialog) ;
-
-    return dialog;
+    return new wxPrintAbortDialog(parent, printout->GetTitle());
 }
 
 void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message)
@@ -363,7 +352,7 @@ wxPrinter::~wxPrinter()
     delete m_pimpl;
 }
 
-wxWindow *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout)
+wxPrintAbortDialog *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout)
 {
     return m_pimpl->CreateAbortWindow( parent, printout );
 }
@@ -380,6 +369,19 @@ bool wxPrinter::Setup(wxWindow *parent)
 
 bool wxPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
 {
+    if ( !prompt && m_printDialogData.GetToPage() == 0 )
+    {
+        // If the dialog is not shown, set the pages range to print everything
+        // by default (as otherwise we wouldn't print anything at all which is
+        // certainly not a reasonable default behaviour).
+        int minPage, maxPage, selFrom, selTo;
+        printout->GetPageInfo(&minPage, &maxPage, &selFrom, &selTo);
+
+        wxPrintDialogData& pdd = m_pimpl->GetPrintDialogData();
+        pdd.SetFromPage(minPage);
+        pdd.SetToPage(maxPage);
+    }
+
     return m_pimpl->Print( parent, printout, prompt );
 }
 
@@ -508,11 +510,49 @@ BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
     EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
 END_EVENT_TABLE()
 
+wxPrintAbortDialog::wxPrintAbortDialog(wxWindow *parent,
+                                       const wxString& documentTitle,
+                                       const wxPoint& pos,
+                                       const wxSize& size,
+                                       long style,
+                                       const wxString& name)
+    : wxDialog(parent, wxID_ANY, _("Printing"), pos, size, style, name)
+{
+    wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
+    mainSizer->Add(new wxStaticText(this, wxID_ANY, _("Please wait while printing...")),
+                   wxSizerFlags().Expand().DoubleBorder());
+
+    wxFlexGridSizer *gridSizer = new wxFlexGridSizer(2, wxSize(20, 0));
+    gridSizer->Add(new wxStaticText(this, wxID_ANY, _("Document:")));
+    gridSizer->AddGrowableCol(1);
+    gridSizer->Add(new wxStaticText(this, wxID_ANY, documentTitle));
+    gridSizer->Add(new wxStaticText(this, wxID_ANY, _("Progress:")));
+    m_progress = new wxStaticText(this, wxID_ANY, _("Preparing"));
+    m_progress->SetMinSize(wxSize(250, -1));
+    gridSizer->Add(m_progress);
+    mainSizer->Add(gridSizer, wxSizerFlags().Expand().DoubleBorder(wxLEFT | wxRIGHT));
+
+    mainSizer->Add(CreateStdDialogButtonSizer(wxCANCEL),
+                   wxSizerFlags().Expand().DoubleBorder());
+
+    SetSizerAndFit(mainSizer);
+}
+
+void wxPrintAbortDialog::SetProgress(int currentPage, int totalPages,
+                                     int currentCopy, int totalCopies)
+{
+  wxString text;
+  text.Printf(_("Printing page %d of %d"), currentPage, totalPages);
+  if ( totalCopies > 1 )
+      text += wxString::Format(_(" (copy %d of %d)"), currentCopy, totalCopies);
+  m_progress->SetLabel(text);
+}
+
 void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
 {
+    wxCHECK_RET( wxPrinterBase::sm_abortWindow != NULL, "OnCancel called twice" );
+
     wxPrinterBase::sm_abortIt = true;
-    wxPrinterBase::sm_abortWindow->Show(false);
-    wxPrinterBase::sm_abortWindow->Close(true);
     wxPrinterBase::sm_abortWindow->Destroy();
     wxPrinterBase::sm_abortWindow = NULL;
 }
@@ -985,38 +1025,90 @@ void wxPreviewCanvas::OnMouseWheel(wxMouseEvent& event)
 
 #endif // wxUSE_MOUSEWHEEL
 
+namespace
+{
+
+// This is by the controls in the print preview as the maximal (and hence
+// longest) page number we may have to display.
+enum { MAX_PAGE_NUMBER = 99999 };
+
+} // anonymous namespace
+
+// ----------------------------------------------------------------------------
+// wxPrintPageMaxCtrl
+// ----------------------------------------------------------------------------
+
+// A simple static control showing the maximal number of pages.
+class wxPrintPageMaxCtrl : public wxStaticText
+{
+public:
+    wxPrintPageMaxCtrl(wxWindow *parent)
+        : wxStaticText(
+                        parent,
+                        wxID_ANY,
+                        wxString(),
+                        wxDefaultPosition,
+                        wxSize
+                        (
+                         parent->GetTextExtent(MaxAsString(MAX_PAGE_NUMBER)).x,
+                         wxDefaultCoord
+                        ),
+                        wxST_NO_AUTORESIZE | wxALIGN_CENTRE
+                      )
+    {
+    }
+
+    // Set the maximal page to display once we really know what it is.
+    void SetMaxPage(int maxPage)
+    {
+        SetLabel(MaxAsString(maxPage));
+    }
+
+private:
+    static wxString MaxAsString(int maxPage)
+    {
+        return wxString::Format("/ %d", maxPage);
+    }
+
+    wxDECLARE_NO_COPY_CLASS(wxPrintPageMaxCtrl);
+};
+
 // ----------------------------------------------------------------------------
 // wxPrintPageTextCtrl
 // ----------------------------------------------------------------------------
 
-// This text control contains the page number in the interval specified during
-// its construction. Invalid pages are not accepted and the control contents is
-// validated when it loses focus. Conversely, if the user changes the page to
-// another valid one or presses Enter, OnGotoPage() method of the preview object
-// will be called.
+// This text control contains the page number in the specified interval.
+//
+// Invalid pages are not accepted and the control contents is validated when it
+// loses focus. Conversely, if the user changes the page to another valid one
+// or presses Enter, OnGotoPage() method of the preview object will be called.
 class wxPrintPageTextCtrl : public wxTextCtrl
 {
 public:
-    wxPrintPageTextCtrl(wxPreviewControlBar *preview, int minPage, int maxPage)
+    wxPrintPageTextCtrl(wxPreviewControlBar *preview)
         : wxTextCtrl(preview,
                      wxID_PREVIEW_GOTO,
-                     PageAsString(minPage),
+                     wxString(),
                      wxDefaultPosition,
-                     // We use hardcoded 99999 for the width instead of fitting
-                     // it to the values we can show because the control looks
-                     // uncomfortably narrow if the real page number is just
-                     // one or two digits.
-                     wxSize(preview->GetTextExtent("99999").x, wxDefaultCoord),
+                     // We use hardcoded maximal page number for the width
+                     // instead of fitting it to the values we can show because
+                     // the control looks uncomfortably narrow if the real page
+                     // number is just one or two digits.
+                     wxSize
+                     (
+                      preview->GetTextExtent(PageAsString(MAX_PAGE_NUMBER)).x,
+                      wxDefaultCoord
+                     ),
                      wxTE_PROCESS_ENTER
 #if wxUSE_VALIDATORS
                      , wxTextValidator(wxFILTER_DIGITS)
 #endif // wxUSE_VALIDATORS
                     ),
-          m_preview(preview),
-          m_minPage(minPage),
-          m_maxPage(maxPage)
+          m_preview(preview)
     {
-        m_page = minPage;
+        m_minPage =
+        m_maxPage =
+        m_page = 1;
 
         Connect(wxEVT_KILL_FOCUS,
                 wxFocusEventHandler(wxPrintPageTextCtrl::OnKillFocus));
@@ -1024,6 +1116,17 @@ public:
                 wxCommandEventHandler(wxPrintPageTextCtrl::OnTextEnter));
     }
 
+    // Update the pages range, must be called after OnPreparePrinting() as
+    // these values are not known before.
+    void SetPageInfo(int minPage, int maxPage)
+    {
+        m_minPage = minPage;
+        m_maxPage = maxPage;
+
+        // Show the first page by default.
+        SetPageNumber(minPage);
+    }
+
     // Helpers to conveniently set or get the current page number. Return value
     // is 0 if the current controls contents is invalid.
     void SetPageNumber(int page)
@@ -1094,8 +1197,8 @@ private:
 
     wxPreviewControlBar * const m_preview;
 
-    const int m_minPage,
-              m_maxPage;
+    int m_minPage,
+        m_maxPage;
 
     // This is the last valid page value that we had, we revert to it if an
     // invalid page is entered.
@@ -1141,6 +1244,7 @@ wxPanel(parent, wxID_ANY, pos, size, style, name)
     m_closeButton = NULL;
     m_zoomControl = NULL;
     m_currentPageText = NULL;
+    m_maxPageText = NULL;
     m_buttonFlags = buttons;
 }
 
@@ -1422,18 +1526,11 @@ void wxPreviewControlBar::CreateButtons()
 
     if (m_buttonFlags & wxPREVIEW_GOTO)
     {
-        int minPage, maxPage, pageFrom, pageTo;
-        m_printPreview->GetPrintout()->GetPageInfo(&minPage, &maxPage,
-                                                   &pageFrom, &pageTo);
-
-        m_currentPageText = new wxPrintPageTextCtrl(this, minPage, maxPage);
+        m_currentPageText = new wxPrintPageTextCtrl(this);
         sizer.Add(m_currentPageText);
 
-        wxStaticText *
-            maxPageText = new wxStaticText(this, wxID_ANY,
-                                           wxString::Format("/ %d", maxPage));
-
-        sizer.Add(maxPageText);
+        m_maxPageText = new wxPrintPageMaxCtrl(this);
+        sizer.Add(m_maxPageText);
     }
 
     if (m_buttonFlags & wxPREVIEW_NEXT)
@@ -1475,6 +1572,15 @@ void wxPreviewControlBar::CreateButtons()
     sizer.AddAtEnd(m_closeButton);
 }
 
+void wxPreviewControlBar::SetPageInfo(int minPage, int maxPage)
+{
+    if ( m_currentPageText )
+        m_currentPageText->SetPageInfo(minPage, maxPage);
+
+    if ( m_maxPageText )
+        m_maxPageText->SetMaxPage(maxPage);
+}
+
 void wxPreviewControlBar::SetZoomControl(int zoom)
 {
     if (m_zoomControl)
@@ -1539,6 +1645,7 @@ wxFrame(parent, wxID_ANY, title, pos, size, style, name)
     m_controlBar = NULL;
     m_previewCanvas = NULL;
     m_windowDisabler = NULL;
+    m_modalityKind = wxPreviewFrame_NonModal;
 
     // Give the application icon
 #ifdef __WXMSW__
@@ -1550,14 +1657,6 @@ wxFrame(parent, wxID_ANY, title, pos, size, style, name)
 
 wxPreviewFrame::~wxPreviewFrame()
 {
-}
-
-void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
-{
-    if (m_windowDisabler)
-        delete m_windowDisabler;
-
-    // Need to delete the printout and the print preview
     wxPrintout *printout = m_printPreview->GetPrintout();
     if (printout)
     {
@@ -1568,12 +1667,33 @@ void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
     }
 
     m_previewCanvas->SetPreview(NULL);
-    wxDELETE(m_printPreview);
+    delete m_printPreview;
+}
+
+void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
+{
+    // Reenable any windows we disabled by undoing whatever we did in our
+    // Initialize().
+    switch ( m_modalityKind )
+    {
+        case wxPreviewFrame_AppModal:
+            delete m_windowDisabler;
+            m_windowDisabler = NULL;
+            break;
+
+        case wxPreviewFrame_WindowModal:
+            if ( GetParent() )
+                GetParent()->Enable();
+            break;
+
+        case wxPreviewFrame_NonModal:
+            break;
+    }
 
     Destroy();
 }
 
-void wxPreviewFrame::Initialize()
+void wxPreviewFrame::InitializeWithModality(wxPreviewFrameModalityKind kind)
 {
 #if wxUSE_STATUSBAR
     CreateStatusBar();
@@ -1592,7 +1712,32 @@ void wxPreviewFrame::Initialize()
     SetAutoLayout( true );
     SetSizer( item0 );
 
-    m_windowDisabler = new wxWindowDisabler(this);
+    m_modalityKind = kind;
+    switch ( m_modalityKind )
+    {
+        case wxPreviewFrame_AppModal:
+            // Disable everything.
+            m_windowDisabler = new wxWindowDisabler( this );
+            break;
+
+        case wxPreviewFrame_WindowModal:
+            // Disable our parent if we have one.
+            if ( GetParent() )
+                GetParent()->Disable();
+            break;
+
+        case wxPreviewFrame_NonModal:
+            // Nothing to do, we don't need to disable any windows.
+            break;
+    }
+
+    if ( m_modalityKind != wxPreviewFrame_NonModal )
+    {
+        // Behave like modal dialogs, don't show in taskbar. This implies
+        // removing the minimize box, because minimizing windows without
+        // taskbar entry is confusing.
+        SetWindowStyle((GetWindowStyle() & ~wxMINIMIZE_BOX) | wxFRAME_NO_TASKBAR);
+    }
 
     Layout();
 
@@ -1815,13 +1960,23 @@ bool wxPrintPreviewBase::RenderPageIntoDC(wxDC& dc, int pageNum)
     m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
 
     // Need to delay OnPreparePrinting() until here, so we have enough
-    // information.
+    // information and a wxDC.
     if (!m_printingPrepared)
     {
+        m_printingPrepared = true;
+
         m_previewPrintout->OnPreparePrinting();
         int selFrom, selTo;
         m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
-        m_printingPrepared = true;
+
+        // Update the wxPreviewControlBar page range display.
+        if ( m_previewFrame )
+        {
+            wxPreviewControlBar * const
+                controlBar = ((wxPreviewFrame*)m_previewFrame)->GetControlBar();
+            if ( controlBar )
+                controlBar->SetPageInfo(m_minPage, m_maxPage);
+        }
     }
 
     m_previewPrintout->OnBeginPrinting();
@@ -1867,7 +2022,7 @@ bool wxPrintPreviewBase::RenderPage(int pageNum)
     {
         m_previewBitmap = new wxBitmap(pageRect.width, pageRect.height);
 
-        if (!m_previewBitmap || !m_previewBitmap->Ok())
+        if (!m_previewBitmap || !m_previewBitmap->IsOk())
         {
             InvalidatePreviewBitmap();
             wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
@@ -2088,7 +2243,7 @@ int wxPrintPreview::GetMinPage() const
 
 bool wxPrintPreview::IsOk() const
 {
-    return m_pimpl->Ok();
+    return m_pimpl->IsOk();
 }
 
 void wxPrintPreview::SetOk(bool ok)