added printing
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@529 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
parent
3b8b23873a
commit
3ebece2bda
@ -30,18 +30,41 @@ IMPLEMENT_APP(MyApp)
|
||||
// MyFrame
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
enum ids{ ID_EDIT = 1, ID_ADD_SAMPLE, ID_CLEAR, ID_PRINT, ID_DPRINT,
|
||||
enum ids{ ID_EDIT = 1, ID_ADD_SAMPLE, ID_CLEAR, ID_PRINT,
|
||||
ID_PRINT_SETUP, ID_PAGE_SETUP, ID_PREVIEW, ID_PRINT_PS,
|
||||
ID_PRINT_SETUP_PS, ID_PAGE_SETUP_PS,ID_PREVIEW_PS,
|
||||
ID_DPRINT,
|
||||
ID_WXLAYOUT_DEBUG, ID_QUIT, ID_CLICK, ID_HTML, ID_TEXT, ID_TEST };
|
||||
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
|
||||
|
||||
BEGIN_EVENT_TABLE(MyFrame,wxFrame)
|
||||
EVT_MENU (-1, MyFrame::OnCommand)
|
||||
EVT_COMMAND (-1,-1, MyFrame::OnCommand)
|
||||
EVT_CHAR ( wxLayoutWindow::OnChar )
|
||||
EVT_MENU(ID_PRINT, MyFrame::OnPrint)
|
||||
EVT_MENU(ID_PREVIEW, MyFrame::OnPrintPreview)
|
||||
EVT_MENU(ID_PRINT_SETUP, MyFrame::OnPrintSetup)
|
||||
EVT_MENU(ID_PAGE_SETUP, MyFrame::OnPageSetup)
|
||||
EVT_MENU(ID_PRINT_PS, MyFrame::OnPrintPS)
|
||||
EVT_MENU(ID_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
|
||||
EVT_MENU(ID_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
|
||||
EVT_MENU(ID_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
|
||||
EVT_MENU (-1, MyFrame::OnCommand)
|
||||
EVT_COMMAND (-1,-1, MyFrame::OnCommand)
|
||||
EVT_CHAR ( wxLayoutWindow::OnChar )
|
||||
END_EVENT_TABLE()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int orientation = wxPORTRAIT;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
MyFrame::MyFrame(void) :
|
||||
wxFrame( NULL, -1, "wxLayout", wxPoint(20,20), wxSize(600,360) )
|
||||
{
|
||||
@ -54,7 +77,20 @@ IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
|
||||
file_menu->Append( ID_ADD_SAMPLE, "Example");
|
||||
file_menu->Append( ID_EDIT, "Edit");
|
||||
file_menu->Append( ID_WXLAYOUT_DEBUG, "Debug");
|
||||
file_menu->Append( ID_PRINT, "Print");
|
||||
|
||||
file_menu->Append(ID_PRINT, "&Print...", "Print");
|
||||
file_menu->Append(ID_PRINT_SETUP, "Print &Setup...","Setup printer properties");
|
||||
file_menu->Append(ID_PAGE_SETUP, "Page Set&up...", "Page setup");
|
||||
file_menu->Append(ID_PREVIEW, "Print Pre&view", "Preview");
|
||||
#ifdef __WXMSW__
|
||||
file_menu->AppendSeparator();
|
||||
file_menu->Append(ID_PRINT_PS, "Print PostScript...", "Print (PostScript)");
|
||||
file_menu->Append(ID_PRINT_SETUP_PS, "Print Setup PostScript...", "Setup printer properties (PostScript)");
|
||||
file_menu->Append(ID_PAGE_SETUP_PS, "Page Setup PostScript...", "Page setup (PostScript)");
|
||||
file_menu->Append(ID_PREVIEW_PS, "Print Preview PostScript", "Preview (PostScript)");
|
||||
#endif
|
||||
file_menu->AppendSeparator();
|
||||
|
||||
file_menu->Append( ID_DPRINT, "Direct Print");
|
||||
file_menu->Append( ID_TEXT, "Export Text");
|
||||
file_menu->Append( ID_HTML, "Export HTML");
|
||||
@ -262,6 +298,137 @@ void MyFrame::OnCommand( wxCommandEvent &event )
|
||||
}
|
||||
};
|
||||
|
||||
void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
#ifdef __WXMSW__
|
||||
wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
|
||||
#else
|
||||
wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
|
||||
#endif
|
||||
wxPrinter printer;
|
||||
wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
|
||||
if (!printer.Print(this, &printout, TRUE))
|
||||
wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK);
|
||||
}
|
||||
|
||||
void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
|
||||
|
||||
wxPostScriptPrinter printer;
|
||||
wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
|
||||
printer.Print(this, &printout, TRUE);
|
||||
}
|
||||
|
||||
void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
#ifdef __WXMSW__
|
||||
wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
|
||||
#else
|
||||
wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
|
||||
#endif
|
||||
wxPrintData printData;
|
||||
printData.SetOrientation(orientation);
|
||||
|
||||
// Pass two printout objects: for preview, and possible printing.
|
||||
wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
|
||||
if (!preview->Ok())
|
||||
{
|
||||
delete preview;
|
||||
wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
|
||||
return;
|
||||
}
|
||||
|
||||
wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
|
||||
frame->Centre(wxBOTH);
|
||||
frame->Initialize();
|
||||
frame->Show(TRUE);
|
||||
}
|
||||
|
||||
void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
|
||||
|
||||
wxPrintData printData;
|
||||
printData.SetOrientation(orientation);
|
||||
|
||||
// Pass two printout objects: for preview, and possible printing.
|
||||
wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
|
||||
wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
|
||||
frame->Centre(wxBOTH);
|
||||
frame->Initialize();
|
||||
frame->Show(TRUE);
|
||||
}
|
||||
|
||||
void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
#ifdef __WXMSW__
|
||||
wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
|
||||
#else
|
||||
wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
|
||||
#endif
|
||||
wxPrintData data;
|
||||
data.SetOrientation(orientation);
|
||||
|
||||
#ifdef __WXMSW__
|
||||
wxPrintDialog printerDialog(this, & data);
|
||||
#else
|
||||
wxGenericPrintDialog printerDialog(this, & data);
|
||||
#endif
|
||||
printerDialog.GetPrintData().SetSetupDialog(TRUE);
|
||||
printerDialog.ShowModal();
|
||||
|
||||
orientation = printerDialog.GetPrintData().GetOrientation();
|
||||
}
|
||||
|
||||
void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
#ifdef __WXMSW__
|
||||
wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
|
||||
#else
|
||||
wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
|
||||
#endif
|
||||
wxPageSetupData data;
|
||||
data.SetOrientation(orientation);
|
||||
|
||||
#ifdef __WXMSW__
|
||||
wxPageSetupDialog pageSetupDialog(this, & data);
|
||||
#else
|
||||
wxGenericPageSetupDialog pageSetupDialog(this, & data);
|
||||
#endif
|
||||
pageSetupDialog.ShowModal();
|
||||
|
||||
data = pageSetupDialog.GetPageSetupData();
|
||||
orientation = data.GetOrientation();
|
||||
}
|
||||
|
||||
void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
|
||||
|
||||
wxPrintData data;
|
||||
data.SetOrientation(orientation);
|
||||
|
||||
wxGenericPrintDialog printerDialog(this, & data);
|
||||
printerDialog.GetPrintData().SetSetupDialog(TRUE);
|
||||
printerDialog.ShowModal();
|
||||
|
||||
orientation = printerDialog.GetPrintData().GetOrientation();
|
||||
}
|
||||
|
||||
void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
|
||||
|
||||
wxPageSetupData data;
|
||||
data.SetOrientation(orientation);
|
||||
|
||||
wxGenericPageSetupDialog pageSetupDialog(this, & data);
|
||||
pageSetupDialog.ShowModal();
|
||||
|
||||
orientation = pageSetupDialog.GetPageSetupData().GetOrientation();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// MyApp
|
||||
|
@ -35,6 +35,16 @@ class MyFrame: public wxFrame
|
||||
void AddSampleText(wxLayoutList &llist);
|
||||
void Clear(void);
|
||||
void OnCommand( wxCommandEvent &event );
|
||||
|
||||
void OnPrint(wxCommandEvent& event);
|
||||
void OnPrintPreview(wxCommandEvent& event);
|
||||
void OnPrintSetup(wxCommandEvent& event);
|
||||
void OnPageSetup(wxCommandEvent& event);
|
||||
void OnPrintPS(wxCommandEvent& event);
|
||||
void OnPrintPreviewPS(wxCommandEvent& event);
|
||||
void OnPrintSetupPS(wxCommandEvent& event);
|
||||
void OnPageSetupPS(wxCommandEvent& event);
|
||||
|
||||
DECLARE_EVENT_TABLE()
|
||||
|
||||
private:
|
||||
|
@ -46,7 +46,7 @@ static const char *g_aTypeStrings[] =
|
||||
};
|
||||
|
||||
# define wxLayoutDebug wxLogDebug
|
||||
# define WXL_VAR(x) wxLogDebug(#x " = ", x)
|
||||
# define WXL_VAR(x) cerr << #x " = " << x ;
|
||||
# define WXL_DBG_POINT(p) wxLogDebug(#p ": (%d, %d)", p.x, p.y)
|
||||
# define WXL_TRACE(f) wxLogDebug(#f ": ")
|
||||
# define TypeString(t) g_aTypeStrings[t]
|
||||
@ -236,6 +236,7 @@ wxLayoutList::SetFont(int family, int size, int style, int weight,
|
||||
void
|
||||
wxLayoutList::SetFont(int family, int size, int style, int weight,
|
||||
int underline, char const *fg, char const *bg)
|
||||
|
||||
{
|
||||
wxColour const
|
||||
* cfg = NULL,
|
||||
@ -255,14 +256,15 @@ void
|
||||
wxLayoutList::GetSize(CoordType *max_x, CoordType *max_y,
|
||||
CoordType *lineHeight)
|
||||
{
|
||||
wxASSERT(max_x); wxASSERT(max_y); wxASSERT(lineHeight);
|
||||
*max_x = m_MaxX;
|
||||
*max_y = m_MaxY;
|
||||
*lineHeight = m_LineHeight;
|
||||
|
||||
if(max_x) *max_x = m_MaxX;
|
||||
if(max_y) *max_y = m_MaxY;
|
||||
if(lineHeight) *lineHeight = m_LineHeight;
|
||||
}
|
||||
|
||||
wxLayoutObjectBase *
|
||||
wxLayoutList::Draw(wxDC &dc, bool findObject, wxPoint const &findCoords)
|
||||
wxLayoutList::Draw(wxDC &dc, bool findObject, wxPoint const
|
||||
&findCoords, int pageNo, bool reallyDraw)
|
||||
{
|
||||
wxLayoutObjectList::iterator i;
|
||||
|
||||
@ -310,19 +312,16 @@ wxLayoutList::Draw(wxDC &dc, bool findObject, wxPoint const &findCoords)
|
||||
int top, bottom, left, right;
|
||||
} margins;
|
||||
|
||||
if(
|
||||
#ifdef __WXMSW__
|
||||
dc.IsKindOf(CLASSINFO(wxPrinterDC)) ||
|
||||
#endif
|
||||
dc.IsKindOf(CLASSINFO(wxPostScriptDC)))
|
||||
int currentPage = 1;
|
||||
|
||||
if(pageNo > 0)
|
||||
{
|
||||
dc.GetSize(&pageWidth, &pageHeight);
|
||||
dc.StartDoc(_("Printing..."));
|
||||
dc.StartPage();
|
||||
margins.top = (1*pageHeight)/10; // 10%
|
||||
margins.bottom = (9*pageHeight)/10; // 90%
|
||||
margins.left = (1*pageWidth)/10;
|
||||
margins.right = (9*pageWidth)/10;
|
||||
WXL_VAR(pageHeight);
|
||||
margins.top = 0; //(1*pageHeight)/10; // 10%
|
||||
margins.bottom = pageHeight;// (9*pageHeight)/10; // 90%
|
||||
margins.left = 0; //(1*pageWidth)/10;
|
||||
margins.right = pageWidth; //(9*pageWidth)/10;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -330,7 +329,7 @@ wxLayoutList::Draw(wxDC &dc, bool findObject, wxPoint const &findCoords)
|
||||
margins.right = -1;
|
||||
margins.bottom = -1;
|
||||
}
|
||||
position.y = margins.right;
|
||||
position.y = margins.top;
|
||||
position.x = margins.left;
|
||||
|
||||
// if the cursorobject is a cmd, we need to find the first
|
||||
@ -365,8 +364,14 @@ wxLayoutList::Draw(wxDC &dc, bool findObject, wxPoint const &findCoords)
|
||||
type = (*i)->GetType();
|
||||
|
||||
// to initialise sizes of objects, we need to call Draw
|
||||
(*i)->Draw(dc, position, baseLine, draw);
|
||||
|
||||
if(draw && (pageNo == -1 || pageNo == currentPage))
|
||||
{
|
||||
(*i)->Draw(dc, position, baseLine, draw);
|
||||
#ifdef WXLAYOUT_DEBUG
|
||||
if(i == begin())
|
||||
wxLogDebug("first position = (%d,%d)",(int) position.x, (int)position.y);
|
||||
#endif
|
||||
}
|
||||
// update coordinates for next object:
|
||||
size = (*i)->GetSize(&objBaseLine);
|
||||
if(findObject && draw) // we need to look for an object
|
||||
@ -448,17 +453,19 @@ wxLayoutList::Draw(wxDC &dc, bool findObject, wxPoint const &findCoords)
|
||||
{
|
||||
// if the this line needs to go onto a new page, we need
|
||||
// to change pages before drawing it:
|
||||
if(margins.bottom != -1 && position.y > margins.bottom)
|
||||
if(pageNo > 0 && position.y > margins.bottom)
|
||||
{
|
||||
dc.EndPage();
|
||||
currentPage++;
|
||||
position_HeadOfLine.y = margins.top;
|
||||
dc.StartPage();
|
||||
}
|
||||
// do this line again, this time drawing it
|
||||
position = position_HeadOfLine;
|
||||
draw = true;
|
||||
i = headOfLine;
|
||||
continue;
|
||||
if(reallyDraw && (pageNo == -1 || pageNo == currentPage))
|
||||
{
|
||||
// do this line again, this time drawing it
|
||||
position = position_HeadOfLine;
|
||||
draw = true;
|
||||
i = headOfLine;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else // we have drawn a line, so continue calculating next one
|
||||
draw = false;
|
||||
@ -478,7 +485,6 @@ wxLayoutList::Draw(wxDC &dc, bool findObject, wxPoint const &findCoords)
|
||||
}
|
||||
i++;
|
||||
}
|
||||
dc.EndDoc();
|
||||
// draw the cursor
|
||||
if(m_Editable)
|
||||
{
|
||||
@ -997,3 +1003,74 @@ wxLayoutList::Clear(int family, int size, int style, int weight,
|
||||
m_FontWeight,m_FontUnderline,
|
||||
m_ColourFG, m_ColourBG);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************** printing stuff ********************/
|
||||
|
||||
bool wxLayoutPrintout::OnPrintPage(int page)
|
||||
{
|
||||
wxDC *dc = GetDC();
|
||||
if (dc)
|
||||
{
|
||||
m_llist->Draw(*dc,false,wxPoint(0,0),page);
|
||||
return TRUE;
|
||||
}
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
bool wxLayoutPrintout::OnBeginDocument(int startPage, int endPage)
|
||||
{
|
||||
if (!wxPrintout::OnBeginDocument(startPage, endPage))
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void wxLayoutPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
|
||||
{
|
||||
|
||||
// This code doesn't work, because we don't have a DC yet.
|
||||
// How on earth are we supposed to calculate the number of pages then?
|
||||
|
||||
*minPage = 1;
|
||||
*maxPage = 32000;
|
||||
|
||||
*selPageFrom = 1;
|
||||
*selPageTo = 1;
|
||||
|
||||
#if 0
|
||||
CoordType height;
|
||||
int pageWidth, pageHeight;
|
||||
|
||||
wxDC *dc = GetDC();
|
||||
wxASSERT(dc);
|
||||
|
||||
dc->GetSize(&pageWidth, &pageHeight);
|
||||
// don't draw but just recalculate sizes:
|
||||
m_llist->Draw(*dc,false,wxPoint(0,0),-1,false);
|
||||
m_llist->GetSize(NULL,&height,NULL);
|
||||
|
||||
*minPage = 1;
|
||||
*maxPage = height/pageHeight+1;
|
||||
|
||||
*selPageFrom = 1;
|
||||
*selPageTo = *maxPage;
|
||||
m_maxPage = *maxPage;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
bool wxLayoutPrintout::HasPage(int pageNum)
|
||||
{
|
||||
return true;
|
||||
// return m_maxPage >= pageNum;
|
||||
}
|
||||
|
||||
|
||||
wxLayoutPrintout *
|
||||
wxLayoutList::MakePrintout(wxString const &name)
|
||||
{
|
||||
return new wxLayoutPrintout(*this,name);
|
||||
}
|
||||
|
@ -15,6 +15,10 @@
|
||||
#include "kbList.h"
|
||||
|
||||
#include "wx/wx.h"
|
||||
#include "wx/print.h"
|
||||
#include "wx/printdlg.h"
|
||||
#include "wx/generic/printps.h"
|
||||
#include "wx/generic/prntdlgg.h"
|
||||
|
||||
// skip the following defines if embedded in M application
|
||||
#ifdef M_BASEDIR
|
||||
@ -189,6 +193,8 @@ public:
|
||||
};
|
||||
|
||||
|
||||
class wxLayoutPrintout;
|
||||
|
||||
/**
|
||||
This class provides a high level abstraction to the wxFText
|
||||
classes.
|
||||
@ -229,10 +235,14 @@ public:
|
||||
@param findObject if true, return the object occupying the
|
||||
position specified by coords
|
||||
@param coords position where to find the object
|
||||
@pageNo if > 0, print only that page of a document (for
|
||||
printing)
|
||||
@reallyDraw set this to false if you don't want to draw but just calculate the coordinates
|
||||
@return if findObject == true, the object or NULL
|
||||
*/
|
||||
wxLayoutObjectBase *Draw(wxDC &dc, bool findObject = false,
|
||||
wxPoint const &coords = wxPoint(0,0));
|
||||
wxPoint const &coords = wxPoint(0,0),
|
||||
int pageNo = -1, bool reallyDraw = true);
|
||||
|
||||
#ifdef WXLAYOUT_DEBUG
|
||||
void Debug(void);
|
||||
@ -244,6 +254,7 @@ public:
|
||||
void GetSize(CoordType *max_x, CoordType *max_y,
|
||||
CoordType *lineHeight);
|
||||
|
||||
|
||||
/**@name Functionality for editing */
|
||||
//@{
|
||||
/// set list editable or read only
|
||||
@ -269,6 +280,8 @@ public:
|
||||
// only used to decide whether we are before or after linebreak
|
||||
CoordType GetLineLength(wxLayoutObjectList::iterator i,
|
||||
CoordType offs = 0);
|
||||
wxLayoutPrintout *MakePrintout(wxString const &name);
|
||||
|
||||
//@}
|
||||
protected:
|
||||
/// font parameters:
|
||||
@ -307,4 +320,19 @@ protected:
|
||||
|
||||
};
|
||||
|
||||
class wxLayoutPrintout: public wxPrintout
|
||||
{
|
||||
public:
|
||||
wxLayoutPrintout(wxLayoutList &llist, wxString const & title = "My printout"):wxPrintout(title)
|
||||
{ m_llist = &llist; m_maxPage = 0; }
|
||||
bool OnPrintPage(int page);
|
||||
bool HasPage(int page);
|
||||
bool OnBeginDocument(int startPage, int endPage);
|
||||
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int
|
||||
*selPageTo);
|
||||
private:
|
||||
wxLayoutList *m_llist;
|
||||
int m_maxPage;
|
||||
};
|
||||
|
||||
#endif // WXLLIST_H
|
||||
|
Loading…
Reference in New Issue
Block a user