If I read the question correctly, then you seem to have several text controls. If you wish to have all the data in a single text control, then pass the text control as a parameter to the printout.
Sample code:
Code: Select all
/*
* WxToolButton2Click
*/
void PrintingFromTextCtrlFrm::OnPrintPreview(wxCommandEvent& event)
{
// Pass two printout objects: for preview, and possible printing.
wxPrintDialogData printDialogData(* g_printData);
wxPrintPreview *preview = new wxPrintPreview(new TextCtrlPrintout(WxMemo1), new TextCtrlPrintout(WxMemo1), & printDialogData );
// wxPrintPreview *preview = new wxPrintPreview(new EditPrint(WxMemo1), new EditPrint(WxMemo1), & printDialogData );
if (!preview->Ok())
{
delete preview;
wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK);
return;
}
wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
frame->Centre(wxBOTH);
frame->Initialize();
frame->Show();
}
Note that this uses a TextCtrlPrintout. This is sample code, which needs working on to get it to do everything you wqant, but it might point you in the right direction.
Code: Select all
#ifdef __GNUG__
#pragma implementation
#endif
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "TxtCtlPrn.h" // Printout for wxTextCtrl
//int orientation = wxPORTRAIT;
int LeftMargin = 20; // Default for 1" left margin
int RightMargin = 20; // Default for 1" right margin
int TopMargin = 20; // Default for 1" top margin
int BottomMargin = 20; // Default for 1" bottom margin
// NOTE: Adjust the max_lines according to the wxFONT size
int max_lines = 53; // Maximum number of lines per page
bool TextCtrlPrintout::OnPrintPage(int page)
{
wxDC *dc = GetDC();
if (dc)
{
DrawTextPage(dc, page, LeftMargin, RightMargin, TopMargin, BottomMargin);
return TRUE;
}
else
return FALSE;
}
bool TextCtrlPrintout::HasPage(int pageNum)
{
// Calculate the number of pages in this document
int minPage, maxPage, selPageFrom, selPageTo;
GetPageInfo(&minPage, &maxPage, &selPageFrom, &selPageTo);
return (pageNum == minPage) || (pageNum <= maxPage);
}
bool TextCtrlPrintout::OnBeginDocument(int startPage, int endPage)
{
if (!wxPrintout::OnBeginDocument(startPage, endPage))
return FALSE;
return TRUE;
// return wxPrintout::OnBeginDocument(startPage, endPage);
}
void TextCtrlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom,
int *selPageTo)
{
int lines = m_textctrl->GetNumberOfLines();
int first_page = 1; // Always starting on page one
int last_page = 1; // Default is the first page
*minPage = first_page; // Always starting on page one
*selPageFrom = first_page; // Always starting on page one
// Scale the pages by the number of lines
for(;;)
{
if(lines <= max_lines)
break;
lines = lines - max_lines;
last_page++; // Increment the page count
}
*selPageTo = last_page;
*maxPage = last_page;
}
void TextCtrlPrintout::DrawTextPage(wxDC *dc)
// This version of DrawTextPage() dumps the entire contents
// of the text frame into the device context.
{
// Make sure the DC font is set or the program will crash in motif
dc->SetFont(textFont);
wxString buf; // Maximum characters per line
int lines = m_textctrl->GetNumberOfLines();
wxCoord w, h; // Text width and height
wxCoord x, y; // Device Context Coordinates
x = y = w = h = 0;
dc->SetDeviceOrigin(x, y);
for(int reps = 0; reps < lines; reps++)
{
buf = m_textctrl->GetLineText(reps);
// Strip off Carriage Returns and Line Feeds
unsigned len = 0;
for(unsigned i = 0; (buf[i] != '\r') && (buf[i] != '\n'); i++)
len++;
char *s = new char[len];
s[len] = '\0';
memmove(s, buf, len);
// Draw text into the Device Context and increment y postion
dc->DrawText(s, x , y);
dc->GetTextExtent(s, &w, &h);
y = y + h;
}
}
void TextCtrlPrintout::DrawTextPage(wxDC *dc, int page)
// This version of DrawTextPage() writes the contents of the
// text frame page by page into the device context.
{
// Make sure the DC font is set or the program will crash in motif
dc->SetFont(textFont);
wxString buf; // Maximum characters per line
wxCoord w, h; // Text width and height
wxCoord x, y; // Device Context Coordinates
x = y = w = h = 0;
dc->SetDeviceOrigin(x, y);
int end = page * max_lines; // Last line to write
int start = end - max_lines; // First line to write
int total_lines = m_textctrl->GetNumberOfLines();
for(int reps = start; reps < end; reps++)
{
if(reps > total_lines)
break; // Do not go past the end of text
buf = m_textctrl->GetLineText(reps);
// Strip off Carriage Returns and Line Feeds
unsigned len = 0;
for(unsigned i = 0; (buf[i] != '\r') && (buf[i] != '\n'); i++)
len++;
char *s = new char[len];
s[len] = '\0';
memmove(s, buf, len);
// Draw text into the Device Context and increment y postion
dc->DrawText(s, x , y);
dc->GetTextExtent(s, &w, &h);
y = y + h;
}
}
void TextCtrlPrintout::DrawTextPage(wxDC *dc, int page,
int leftMargin, int rightMargin,
int topMargin, int bottomMargin)
// This version of DrawTextPage() writes the contents of the
// text frame page by page into the device context with margins.
{
// Make sure the DC font is set or the program will crash in motif
dc->SetFont(textFont);
// Get the logical pixels per inch of screen and printer
int ppiScreenX, ppiScreenY;
GetPPIScreen(&ppiScreenX, &ppiScreenY);
int ppiPrinterX, ppiPrinterY;
GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
// Scale the DC so that the printout roughly represents the
// the screen scaling.
float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
// Check real page size in case it is reduced by print preview
int pageWidth, pageHeight;
wxCoord w, h;
dc->GetSize(&w, &h);
GetPageSizePixels(&pageWidth, &pageHeight);
// Do not change if printer pageWidth == current DC width
float overallScale = scale * (float)(w/(float)pageWidth);
dc->SetUserScale(overallScale, overallScale);
// Calculate conversion factor for converting millimetres into
// logical units. There are approx. 25.1 mm to the inch. There
// are ppi device units to the inch. Therefore 1 mm corresponds
// to ppi/25.1 device units.
float logUnitsFactor = (float)(ppiPrinterX/(scale*25.1));
int pageWidthMM, pageHeightMM;
GetPageSizeMM(&pageWidthMM, &pageHeightMM);
float leftMarginLogical = (float)(logUnitsFactor*leftMargin);
float topMarginLogical = (float)(logUnitsFactor*topMargin);
float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin));
float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin));
wxString buf; // Maximum characters per line
int end = page * max_lines; // Last line to write
int start = end - max_lines; // First line to write
int total_lines = m_textctrl->GetNumberOfLines();
wxCoord textW, textH; // Text width and height
wxCoord xpos = (wxCoord)leftMarginLogical;
wxCoord ypos = (wxCoord)topMarginLogical;
for(int reps = start; reps < end; reps++)
{
if(reps > total_lines)
break; // Do not go past the end of text
buf = m_textctrl->GetLineText(reps);
// Strip off Carriage Returns and Line Feeds
unsigned len = 0;
for(unsigned i = 0; (buf[i] != '\r') && (buf[i] != '\n'); i++)
len++;
char *s = new char[len];
s[len] = '\0';
memmove(s, buf, len);
// Draw text into the Device Context and increment ypos postion
dc->DrawText(s, xpos , ypos);
dc->GetTextExtent(s, &textW, &textH);
ypos = ypos + textH;
}
}
Code: Select all
#ifndef __TXTCTRLPRN_h__
#define __TXTCTRLPRN_h__
#if defined(__GNUG__) && !defined(__APPLE__)
#pragma interface
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#include <wx/frame.h>
#else
#include <wx/wxprec.h>
#endif
#include <wx/print.h>
#include <wx/textctrl.h>
// Printer default values
class TextCtrlPrintout: public wxPrintout
{
public:
// TextCtrlPrintout(wxTextCtrl *textctrl, wxChar *title = "My printout"): wxPrintout(title)
TextCtrlPrintout(wxTextCtrl *textctrl, wxChar *title = _T("My printout")): wxPrintout(title)
{
m_textctrl = textctrl;
m_printed = 0;
// textFont = wxFont(12, wxSWISS, wxNORMAL, wxNORMAL);
textFont.Create(12, wxSWISS, wxNORMAL, wxNORMAL);
}
~TextCtrlPrintout()
{
}
public:
bool OnPrintPage(int page);
bool HasPage(int page);
bool OnBeginDocument(int startPage, int endPage);
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom,
int *selPageTo);
void DrawTextPage(wxDC *dc); // Write entire contents of text frame
void DrawTextPage(wxDC *dc, int page); // Write page by page
// Write page by page with left, right, top and bottom margins
void DrawTextPage(wxDC *dc, int page, int leftMargin, int rightMargin, int topMargin, int bottomMargin);
private:
wxTextCtrl *m_textctrl;
int m_printed;
wxFont textFont;
};
#endif
Failing that, you may want to create a report of your results, and perhaps use wxReportWriter.