Implement support for printing only selected pages in wxMSW
While the user could select to print only the selection in the dialog, there was no way for the program to specify which pages were selected, making this choice impossible to handle. Add new IsPageSelected() function which is now used to query which pages are selected and should actually be printed. Update the printing sample to show how this function is used.
This commit is contained in:
parent
0331057aba
commit
e93570f681
6 changed files with 76 additions and 7 deletions
|
|
@ -356,9 +356,20 @@ void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
|
|||
void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
wxPrintDialogData printDialogData(* g_printData);
|
||||
printDialogData.EnableSelection(true);
|
||||
printDialogData.EnablePageNumbers(true);
|
||||
printDialogData.SetMinPage(1);
|
||||
printDialogData.SetMaxPage(2);
|
||||
printDialogData.SetFromPage(1);
|
||||
printDialogData.SetToPage(2);
|
||||
printDialogData.SetAllPages(true);
|
||||
|
||||
wxPrinter printer(&printDialogData);
|
||||
MyPrintout printout(this, "My printout");
|
||||
|
||||
// wxPrinter copies printDialogData internally, so we have to pass this
|
||||
// instance in order to evaluate users inputs.
|
||||
MyPrintout printout(this, &printer.GetPrintDialogData(), "My printout");
|
||||
|
||||
if (!printer.Print(this, &printout, true /*prompt*/))
|
||||
{
|
||||
if (wxPrinter::GetLastError() == wxPRINTER_ERROR)
|
||||
|
|
@ -381,7 +392,7 @@ void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
|
|||
// Pass two printout objects: for preview, and possible printing.
|
||||
wxPrintDialogData printDialogData(* g_printData);
|
||||
wxPrintPreview *preview =
|
||||
new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData);
|
||||
new wxPrintPreview(new MyPrintout(this, &printDialogData), new MyPrintout(this, &printDialogData), &printDialogData);
|
||||
if (!preview->IsOk())
|
||||
{
|
||||
delete preview;
|
||||
|
|
@ -412,7 +423,7 @@ void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
|
|||
wxPrintDialogData printDialogData(* g_printData);
|
||||
|
||||
wxPostScriptPrinter printer(&printDialogData);
|
||||
MyPrintout printout(this, "My printout");
|
||||
MyPrintout printout(this, &printer.GetPrintDialogData(), "My printout");
|
||||
printer.Print(this, &printout, true/*prompt*/);
|
||||
|
||||
(*g_printData) = printer.GetPrintDialogData().GetPrintData();
|
||||
|
|
@ -422,7 +433,7 @@ void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
|
|||
{
|
||||
// Pass two printout objects: for preview, and possible printing.
|
||||
wxPrintDialogData printDialogData(* g_printData);
|
||||
wxPrintPreview *preview = new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData);
|
||||
wxPrintPreview *preview = new wxPrintPreview(new MyPrintout(this, &printDialogData), new MyPrintout(this, &printDialogData), &printDialogData);
|
||||
wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview");
|
||||
frame->Initialize();
|
||||
frame->Centre(wxBOTH);
|
||||
|
|
@ -541,6 +552,16 @@ void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *
|
|||
*maxPage = 2;
|
||||
*selPageFrom = 1;
|
||||
*selPageTo = 2;
|
||||
|
||||
if (m_printDlgData->GetSelection())
|
||||
{
|
||||
// if the user wants to print the selection, we could set the range via
|
||||
// selPageFrom and selPageTo, but if the pages are not consecutive, we
|
||||
// set selPageFrom and selPageTo to the maximum range and we use
|
||||
// IsPageSelected() to tell the printing system which page is selected.
|
||||
|
||||
// in our example below, only page 2 is selected.
|
||||
}
|
||||
}
|
||||
|
||||
bool MyPrintout::HasPage(int pageNum)
|
||||
|
|
@ -548,6 +569,12 @@ bool MyPrintout::HasPage(int pageNum)
|
|||
return (pageNum == 1 || pageNum == 2);
|
||||
}
|
||||
|
||||
bool MyPrintout::IsPageSelected(int pageNum)
|
||||
{
|
||||
// to demonstrate selection, we just simulate selection of page 2
|
||||
return pageNum == 2;
|
||||
}
|
||||
|
||||
void MyPrintout::DrawPageOne()
|
||||
{
|
||||
// You might use THIS code if you were scaling graphics of known size to fit
|
||||
|
|
|
|||
|
|
@ -86,13 +86,20 @@ private:
|
|||
class MyPrintout: public wxPrintout
|
||||
{
|
||||
public:
|
||||
MyPrintout(MyFrame* frame, const wxString &title = "My printout")
|
||||
: wxPrintout(title) { m_frame=frame; }
|
||||
MyPrintout(MyFrame* frame,
|
||||
wxPrintDialogData* printDlgData,
|
||||
const wxString& title = "My printout")
|
||||
: wxPrintout(title)
|
||||
{
|
||||
m_frame = frame;
|
||||
m_printDlgData = printDlgData;
|
||||
}
|
||||
|
||||
virtual bool OnPrintPage(int page) override;
|
||||
virtual bool HasPage(int page) override;
|
||||
virtual bool OnBeginDocument(int startPage, int endPage) override;
|
||||
virtual void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override;
|
||||
virtual bool IsPageSelected(int pageNum) override;
|
||||
|
||||
void DrawPageOne();
|
||||
void DrawPageTwo();
|
||||
|
|
@ -102,6 +109,7 @@ public:
|
|||
|
||||
private:
|
||||
MyFrame *m_frame;
|
||||
wxPrintDialogData* m_printDlgData;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue