wxGrid printing - wxGridPrintout

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
Ugly!
Earned some good credits
Earned some good credits
Posts: 113
Joined: Mon May 09, 2005 5:11 am
Location: Argentina - BS AS

wxGrid printing - wxGridPrintout

Post by Ugly! » Wed Nov 29, 2006 1:16 am

Hello wxusers, here I leave you something I've been doing for work: a wxPrintout derived class to use with wxGrid.

How to use:

Code: Select all

        
        wxGridPrintout* printOut=new wxGridPrintout(grid,wxGP_DEFAULT);
	wxGridPrintout* printOutForPrinting=new wxGridPrintout(grid,wxGP_DEFAULT);

	printOut->SetUserScale(0.75); //set the scale you want to use, default 1.0
	printOut->AddColBrake(15); //at col 15 a new page will begin
	printOutForPrinting->SetUserScale(0.75);
	printOutForPrinting->AddColBrake(15);
	

	wxPrintData* prnData=new wxPrintData();
	prnData->SetPaperId(wxPAPER_LEGAL);
	prnData->SetQuality(wxPRINT_QUALITY_DRAFT);
	prnData->SetOrientation(wxLANDSCAPE);
	wxPrintDialogData printDialogData(*prnData);

        if(preview){
		wxPrintPreview *previewDlg = new wxPrintPreview(
                             printOut,printOutForPrinting,
                             prnData);
		if (!previewDlg->Ok())
		{
	        delete previewDlg;
			wxMessageBox(wxT("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), wxT("Previewing"), wxOK);
			return;
		}
	
	    wxPreviewFrame *frame = new wxPreviewFrame(previewDlg, this,
								wxT("Demo Print Preview"));
		frame->Centre(wxBOTH);
		frame->Initialize();
		frame->Show(true);
	}
	else{
		wxPrintDialog printDialog(this,&printDialogData);
		if(printDialog.ShowModal()==wxID_CANCEL)
			return;
		printDialogData=printDialog.GetPrintDialogData();
	
	
		wxPrinter printer(&printDialogData);
		if(!printer.Print(this,printOut,false)){
			if(wxPrinter::GetLastError()==wxPRINTER_ERROR)
				wxMessageBox(wxT("Printer error"),wxT("Printer..."),wxID_OK);
			else
				wxMessageBox(wxT("Printing canceled"),wxT("Printer"),wxID_OK);
		}
		else
			(*prnData) = printer.GetPrintDialogData().GetPrintData();
		
		delete printOut;
		delete printOutForPrinting;
	}
	delete prnData;

Hope you find this useful. Ideas, comments and bugs report are welcome.

Regards, Mat
Just a newbie - Too many interests, not too many time.

Windows XP SP2
Kubuntu GNU/Linux - Feisty
wxActiveRecordGenerator (aka wxARG) maintainer
Find it at wxCode

ippo
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Mar 10, 2006 3:05 pm

Post by ippo » Sat Feb 03, 2007 3:30 pm

Great code.
But :
1- using VC 2005 you need to cast to (wxBrush) the *wxBLACK_BRUSH.
2- In the latest labels (row) seem to disappear part of the label
3- No support for ctrl or cell merge ( i'm looking for a simple solution)

regards

ippo
VC Express 2005 WinXp
gcc MacOS x Linux

ippo
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Mar 10, 2006 3:05 pm

Post by ippo » Sat Feb 03, 2007 3:31 pm

Great code.
But :
1- using VC 2005 you need to cast to (wxBrush) the *wxBLACK_BRUSH.
2- In the latest labels (row) seem to disappear part of the label
3- No support for ctrl or cell merge ( i'm looking for a simple solution)

regards

ippo
VC Express 2005 WinXp
gcc MacOS x Linux

Post Reply