Page Setup Dialog settings not kept

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
ercolesptr
Earned a small fee
Earned a small fee
Posts: 16
Joined: Tue Nov 08, 2022 12:12 pm

Page Setup Dialog settings not kept

Post by ercolesptr »

I have this code in my program and although it shows up the page setup dialog correctly, the settings inputed are not being kept and if I reopen the page setup again I find all settings gone back to default. Furthermore the settings inputed here are not used for the print job. Any Idea what I need to do?

Code: Select all

wxPrintData g_printData;
wxPrintDialogData g_printDialogData;

void Project1Frm::WxButton6Click(wxCommandEvent& event)
{
	wxPageSetupDialogData psdd(g_printData);
    WxPageSetupDialog1->ShowModal();
    psdd.EnablePrinter(true);
    psdd.EnablePaper(true);
    psdd.EnableOrientation(true);
    g_printData = wxPrintData(WxPageSetupDialog1->GetPageSetupDialogData().GetPrintData());
}
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Page Setup Dialog settings not kept

Post by ONEEYEMAN »

Hi,
Did you look at the printing sample?

What is your wx version and platform you test it on?

Thank you.
ercolesptr
Earned a small fee
Earned a small fee
Posts: 16
Joined: Tue Nov 08, 2022 12:12 pm

Re: Page Setup Dialog settings not kept

Post by ercolesptr »

The platform is Windows 10, and wx version is 3.2.1. Here is the rest of the code:

Code: Select all

#include "Project1Frm.h"
#include <wx/msgdlg.h>
#include <wx/wx.h>
#include <wx/print.h>
#include <wx/printdlg.h>
#include <wx/cmndata.h>
#include <iostream>

wxPrintData g_printData;
wxPrintDialogData g_printDialogData;
static const int brush_size = 3;

class MyPrintout : public wxPrintout
{
    public: MyPrintout() : wxPrintout()
    {
        //
    }
    bool OnBeginDocument(int startPage, int endPage)
    {
        return wxPrintout::OnBeginDocument(startPage,endPage);
    }
    void OnBeginPrinting()
    {
        return wxPrintout::OnBeginPrinting();
    }
    void OnEndDocument()
    {
        return wxPrintout::OnEndDocument();
    }
    void GetPageInfo(int *minPage, int *maxPage, int *pageSelFrom, int *pageSelTo)
    {
        *minPage = 1;
        *maxPage = 1;
        *pageSelFrom = 1;
        *pageSelTo = 1;
    }
    bool HasPage(int pageNum)
    {
        if (pageNum <= 2)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    void OnPreparePrinting()
    {
        return wxPrintout::OnPreparePrinting();
    }
    bool OnPrintPage(int pageNum)
    {
        //wxLocale locale(wxLANGUAGE_ENGLISH_MALTA);
        wxDC *ptr = GetDC();
        if (ptr == NULL || !ptr->IsOk())
        {
            return false;
        }
        wxDC& dc = *ptr;
        dc.Clear();
        dc.SetMapMode(wxMM_POINTS);
        dc.SetPen(wxPen(wxColor(0,0,0),brush_size));
        dc.SetBrush(wxBrush(*wxTRANSPARENT_BRUSH));
               		 
      dc.SetFont(wxFont(16,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,wxEmptyString,wxFONTENCODING_DEFAULT));
        dc.SetTextForeground(wxColor(0,0,0));
        dc.DrawText("This is a test",120,30);

        return true;
    }
    void OnEndPrinting()
    {
        return wxPrintout::OnEndPrinting();
    }
};

void Project1Frm::WxButton6Click(wxCommandEvent& event)
{
	wxPageSetupDialogData psdd(g_printData);
    WxPageSetupDialog1->ShowModal();
    psdd.EnablePrinter(true);
    psdd.EnablePaper(true);
    psdd.EnableOrientation(true);
    g_printData = wxPrintData(WxPageSetupDialog1->GetPageSetupDialogData().GetPrintData());
}

/*
 * WxButton7Click
 */
void Project1Frm::WxButton7Click(wxCommandEvent& event)
{
	wxPrintPreview *preview = new wxPrintPreview(new MyPrintout(), new MyPrintout(), &g_printDialogData);
	if (! preview->Ok())
    {
        return;
    }
    wxPreviewFrame *frame = new wxPreviewFrame(preview, this, wxT("Print Preview"), wxPoint(100,100), wxSize(800,600));
    frame->Center(wxBOTH);
    frame->Initialize();
    frame->Show(true);
}

/*
 * WxButton8Click
 */
void Project1Frm::WxButton8Click(wxCommandEvent& event)
{
	wxPrintDialogData g_printDialogData(g_printData);
	g_printDialogData.SetPrintData(g_printData);
	g_printDialogData.SetPrintToFile(false);
	wxPrinter printer;
	MyPrintout printout;

	if (!printer.Print(this, &printout,true))
    {
        return;
    }
    else
    {
        g_printData = wxPrintData(printer.GetPrintDialogData().GetPrintData());
    }
}
ercolesptr
Earned a small fee
Earned a small fee
Posts: 16
Joined: Tue Nov 08, 2022 12:12 pm

Re: Page Setup Dialog settings not kept

Post by ercolesptr »

I found that changing the line:

Code: Select all

wxPrinter printer;
to:

Code: Select all

wxPrinter printer(&g_printDialogData);
solved the problem for real printing, however for preview it is still not having any effect.
ercolesptr
Earned a small fee
Earned a small fee
Posts: 16
Joined: Tue Nov 08, 2022 12:12 pm

Re: Page Setup Dialog settings not kept

Post by ercolesptr »

I found the solution for preview as well. I needed to add this line in the preview function at the beginning:

Code: Select all

wxPrintDialogData g_printDialogData(g_printData);
ercolesptr
Earned a small fee
Earned a small fee
Posts: 16
Joined: Tue Nov 08, 2022 12:12 pm

Re: Page Setup Dialog settings not kept

Post by ercolesptr »

Just for completeness sake and also because I could not find any examples easily here is how the Page setup function should be:

Code: Select all

void Project1Frm::WxButton6Click(wxCommandEvent& event)
{
	wxPageSetupDialogData psdd;
	psdd=wxPageSetupDialogData(g_printData);
	psdd.EnablePrinter(true);
    psdd.EnablePaper(true);
    psdd.EnableMargins(true);
    psdd.EnableHelp(true);
    psdd.EnableOrientation(true);
	wxPageSetupDialog dialog( NULL, &psdd );
    dialog.ShowModal();
    g_printData = wxPrintData(dialog.GetPageSetupDialogData().GetPrintData());
}
Post Reply