]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxLayout.cpp
more fixes, printing works quite fine now
[wxWidgets.git] / user / wxLayout / wxLayout.cpp
1 /*
2 * Program: wxLayout
3 *
4 * Author: Karsten Ballüder
5 *
6 * Copyright: (C) 1998, Karsten Ballüder <Ballueder@usa.net>
7 *
8 */
9
10 #ifdef __GNUG__
11 #pragma implementation "wxLayout.h"
12 #endif
13
14 #include "wxLayout.h"
15 #include "wx/textfile.h"
16
17
18 #include "Micon.xpm"
19
20 // for testing only:
21 #include <stdio.h>
22
23 //-----------------------------------------------------------------------------
24 // main program
25 //-----------------------------------------------------------------------------
26
27 IMPLEMENT_APP(MyApp)
28
29 //-----------------------------------------------------------------------------
30 // MyFrame
31 //-----------------------------------------------------------------------------
32
33 enum ids{ ID_EDIT = 1, ID_ADD_SAMPLE, ID_CLEAR, ID_PRINT,
34 ID_PRINT_SETUP, ID_PAGE_SETUP, ID_PREVIEW, ID_PRINT_PS,
35 ID_PRINT_SETUP_PS, ID_PAGE_SETUP_PS,ID_PREVIEW_PS,
36 ID_DPRINT,
37 ID_WXLAYOUT_DEBUG, ID_QUIT, ID_CLICK, ID_HTML, ID_TEXT, ID_TEST };
38
39
40 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
41
42 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
43 EVT_MENU(ID_PRINT, MyFrame::OnPrint)
44 EVT_MENU(ID_PREVIEW, MyFrame::OnPrintPreview)
45 EVT_MENU(ID_PRINT_SETUP, MyFrame::OnPrintSetup)
46 EVT_MENU(ID_PAGE_SETUP, MyFrame::OnPageSetup)
47 EVT_MENU(ID_PRINT_PS, MyFrame::OnPrintPS)
48 EVT_MENU(ID_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
49 EVT_MENU(ID_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
50 EVT_MENU(ID_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
51 EVT_MENU (-1, MyFrame::OnCommand)
52 EVT_COMMAND (-1,-1, MyFrame::OnCommand)
53 EVT_CHAR ( wxLayoutWindow::OnChar )
54 END_EVENT_TABLE()
55
56
57 int orientation = wxPORTRAIT;
58
59 MyFrame::MyFrame(void) :
60 wxFrame( (wxFrame *) NULL, -1, (char *) "wxLayout", wxPoint(20,20), wxSize(600,360) )
61 {
62 CreateStatusBar( 1 );
63
64 SetStatusText( "wxLayout by Karsten Ballüder." );
65
66 wxMenu *file_menu = new wxMenu;
67 file_menu->Append( ID_CLEAR, "Clear");
68 file_menu->Append( ID_ADD_SAMPLE, "Example");
69 // file_menu->Append( ID_EDIT, "Edit");
70 // file_menu->Append( ID_WXLAYOUT_DEBUG, "Debug");
71
72 file_menu->Append(ID_PRINT, "&Print...", "Print");
73 file_menu->Append(ID_PRINT_SETUP, "Print &Setup...","Setup printer properties");
74 file_menu->Append(ID_PAGE_SETUP, "Page Set&up...", "Page setup");
75 file_menu->Append(ID_PREVIEW, "Print Pre&view", "Preview");
76 #ifdef __WXMSW__
77 file_menu->AppendSeparator();
78 file_menu->Append(ID_PRINT_PS, "Print PostScript...", "Print (PostScript)");
79 file_menu->Append(ID_PRINT_SETUP_PS, "Print Setup PostScript...", "Setup printer properties (PostScript)");
80 file_menu->Append(ID_PAGE_SETUP_PS, "Page Setup PostScript...", "Page setup (PostScript)");
81 file_menu->Append(ID_PREVIEW_PS, "Print Preview PostScript", "Preview (PostScript)");
82 #endif
83 file_menu->AppendSeparator();
84
85 // file_menu->Append( ID_DPRINT, "Direct Print");
86 file_menu->Append( ID_TEXT, "Export Text");
87 file_menu->Append( ID_HTML, "Export HTML");
88 // file_menu->Append( ID_TEST, "Test");
89 file_menu->Append( ID_QUIT, "Exit");
90
91 wxMenuBar *menu_bar = new wxMenuBar();
92 menu_bar->Append(file_menu, "File" );
93 menu_bar->Show( TRUE );
94
95 SetMenuBar( menu_bar );
96
97 m_lwin = new wxLayoutWindow(this);
98 m_lwin->SetMouseTracking(true);
99 m_lwin->GetLayoutList().SetEditable(true);
100 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
101 m_lwin->SetFocus();
102 };
103
104 void
105 MyFrame::AddSampleText(wxLayoutList &llist)
106 {
107 llist.SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
108 llist.Insert("--");
109 llist.LineBreak();
110
111 llist.SetFont(wxROMAN);
112 llist.Insert("The quick brown fox jumps over the lazy dog.");
113 llist.LineBreak();
114 llist.Insert("Hello ");
115 llist.Insert(new wxLayoutObjectIcon(new wxIcon(Micon_xpm,-1,-1)));
116 llist.LineBreak();
117 llist.SetFontWeight(wxBOLD);
118 llist.Insert("World! ");
119 llist.SetFontWeight(wxNORMAL);
120 llist.Insert("The quick brown fox jumps...");
121 llist.LineBreak();
122
123 llist.Insert("over the lazy dog.");
124 llist.SetFont(-1,-1,-1,-1,true);
125 llist.Insert("underlined");
126 llist.SetFont(-1,-1,-1,-1,false);
127 llist.SetFont(wxROMAN);
128 llist.Insert("This is ");
129 llist.SetFont(-1,-1,-1,wxBOLD); llist.Insert("BOLD "); llist.SetFont(-1,-1,-1,wxNORMAL);
130 llist.Insert("and ");
131 llist.SetFont(-1,-1,wxITALIC);
132 llist.Insert("italics ");
133 llist.SetFont(-1,-1,wxNORMAL);
134 llist.LineBreak();
135
136 llist.Insert("and ");
137 llist.SetFont(-1,-1,wxSLANT);
138 llist.Insert("slanted");
139 llist.SetFont(-1,-1,wxNORMAL);
140 llist.Insert(" text.");
141 llist.LineBreak();
142
143 llist.Insert("and ");
144 llist.SetFont(-1,-1,-1,-1,-1,"blue");
145 llist.Insert("blue");
146 llist.SetFont(-1,-1,-1,-1,-1,"black");
147 llist.Insert(" and ");
148 llist.SetFont(-1,-1,-1,-1,-1,"green","black");
149 llist.Insert("green on black");
150 llist.SetFont(-1,-1,-1,-1,-1,"black","white");
151 llist.Insert(" text.");
152 llist.LineBreak();
153
154 llist.SetFont(-1,-1,wxSLANT);
155 llist.Insert("Slanted");
156 llist.SetFont(-1,-1,wxNORMAL);
157 llist.Insert(" and normal text and ");
158 llist.SetFont(-1,-1,wxSLANT);
159 llist.Insert("slanted");
160 llist.SetFont(-1,-1,wxNORMAL);
161 llist.Insert(" again.");
162 llist.LineBreak();
163
164 // add some more text for testing:
165 llist.Insert("And here the source for the test program:");
166 llist.LineBreak();
167 llist.SetFont(wxTELETYPE,16);
168 char buffer[1024];
169 FILE *in = fopen("wxLayout.cpp","r");
170 if(in)
171 {
172 for(;;)
173 {
174 fgets(buffer,1024,in);
175 if(feof(in))
176 break;
177 llist.Insert(buffer);
178 llist.LineBreak();
179 }
180 }
181
182 m_lwin->Refresh();
183 m_lwin->UpdateScrollbars();
184 llist.SetEditable();
185 llist.SetCursor(wxPoint(0,0));
186 }
187
188 void
189 MyFrame::Clear(void)
190 {
191 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
192 m_lwin->UpdateScrollbars();
193 }
194
195 /* test the editing */
196 void MyFrame::Edit(void)
197 {
198 wxLayoutList & llist = m_lwin->GetLayoutList();
199 //m_lwin->SetEventId(ID_CLICK);
200
201 llist.MoveCursor(0);
202 llist.MoveCursor(5);
203 llist.MoveCursor(0,2);
204 llist.Delete(2);
205 llist.MoveCursor(2);
206 llist.Insert("not");
207 llist.LineBreak();
208 m_lwin->Refresh();
209 }
210
211 void MyFrame::OnCommand( wxCommandEvent &event )
212 {
213 cerr << "id:" << event.GetId() << endl;
214 switch (event.GetId())
215 {
216 case ID_QUIT:
217 Close( TRUE );
218 break;
219 case ID_PRINT:
220 m_lwin->Print();
221 break;
222 case ID_DPRINT:
223 {
224 wxLayoutList llist;
225 AddSampleText(llist);
226 wxPostScriptDC dc("layout.ps",true,this);
227 if (dc.Ok() && dc.StartDoc((char *)_("Printing message...")))
228 {
229 //dc.SetUserScale(1.0, 1.0);
230 llist.Draw(dc); //,false,wxPoint(0,0),true);
231 dc.EndDoc();
232 }
233 }
234 break;
235 case ID_EDIT:
236 Edit();
237 break;
238 case ID_ADD_SAMPLE:
239 AddSampleText(m_lwin->GetLayoutList());
240 break;
241 case ID_CLEAR:
242 Clear();
243 break;
244 case ID_WXLAYOUT_DEBUG:
245 m_lwin->GetLayoutList().Debug();
246 break;
247 case ID_CLICK:
248 cerr << "Received click event." << endl;
249 break;
250 case ID_HTML:
251 {
252 wxLayoutExportObject *export;
253 wxLayoutList::iterator i = m_lwin->GetLayoutList().begin();
254
255 while((export = wxLayoutExport(m_lwin->GetLayoutList(),
256 i,WXLO_EXPORT_AS_HTML)) != NULL)
257 {
258 if(export->type == WXLO_EXPORT_HTML)
259 cout << *(export->content.text);
260 else
261 cout << "<!--UNKNOWN OBJECT>";
262 delete export;
263 }
264 }
265 break;
266 case ID_TEXT:
267 {
268 wxLayoutExportObject *export;
269 wxLayoutList::iterator i = m_lwin->GetLayoutList().begin();
270
271 while((export = wxLayoutExport(m_lwin->GetLayoutList(),
272 i,WXLO_EXPORT_AS_TEXT)) != NULL)
273 {
274 if(export->type == WXLO_EXPORT_TEXT)
275 cout << *(export->content.text);
276 else
277 cout << "<!--UNKNOWN OBJECT>";
278 delete export;
279 }
280 }
281 break;
282 }
283 };
284
285 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
286 {
287 #ifdef __WXMSW__
288 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
289 #else
290 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
291 #endif
292 wxPrinter printer;
293 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"Printout from wxLayout");
294 if (! printer.Print(this, &printout, TRUE))
295 wxMessageBox(
296 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
297 "Printing", wxOK);
298 }
299
300 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
301 {
302 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
303
304 wxPostScriptPrinter printer;
305 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
306 printer.Print(this, &printout, TRUE);
307 }
308
309 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
310 {
311 #ifdef __WXMSW__
312 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
313 #else
314 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
315 #endif
316 wxPrintData printData;
317 printData.SetOrientation(orientation);
318
319 // Pass two printout objects: for preview, and possible printing.
320 wxPrintPreview *preview = new wxPrintPreview(new
321 wxLayoutPrintout(
322 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
323 if (!preview->Ok())
324 {
325 delete preview;
326 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
327 return;
328 }
329
330 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
331 frame->Centre(wxBOTH);
332 frame->Initialize();
333 frame->Show(TRUE);
334 }
335
336 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
337 {
338 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
339
340 wxPrintData printData;
341 printData.SetOrientation(orientation);
342
343 // Pass two printout objects: for preview, and possible printing.
344 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
345 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
346 frame->Centre(wxBOTH);
347 frame->Initialize();
348 frame->Show(TRUE);
349 }
350
351 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
352 {
353 #ifdef __WXMSW__
354 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
355 #else
356 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
357 #endif
358 wxPrintData data;
359 data.SetOrientation(orientation);
360
361 #ifdef __WXMSW__
362 wxPrintDialog printerDialog(this, & data);
363 #else
364 wxGenericPrintDialog printerDialog(this, & data);
365 #endif
366 printerDialog.GetPrintData().SetSetupDialog(TRUE);
367 printerDialog.ShowModal();
368
369 orientation = printerDialog.GetPrintData().GetOrientation();
370 }
371
372 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
373 {
374 #ifdef __WXMSW__
375 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
376 #else
377 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
378 #endif
379 wxPageSetupData data;
380 data.SetOrientation(orientation);
381
382 #ifdef __WXMSW__
383 wxPageSetupDialog pageSetupDialog(this, & data);
384 #else
385 wxGenericPageSetupDialog pageSetupDialog(this, & data);
386 #endif
387 pageSetupDialog.ShowModal();
388
389 data = pageSetupDialog.GetPageSetupData();
390 orientation = data.GetOrientation();
391 }
392
393 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
394 {
395 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
396
397 wxPrintData data;
398 data.SetOrientation(orientation);
399
400 wxGenericPrintDialog printerDialog(this, & data);
401 printerDialog.GetPrintData().SetSetupDialog(TRUE);
402 printerDialog.ShowModal();
403
404 orientation = printerDialog.GetPrintData().GetOrientation();
405 }
406
407 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
408 {
409 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
410
411 wxPageSetupData data;
412 data.SetOrientation(orientation);
413
414 wxGenericPageSetupDialog pageSetupDialog(this, & data);
415 pageSetupDialog.ShowModal();
416
417 orientation = pageSetupDialog.GetPageSetupData().GetOrientation();
418 }
419
420
421 //-----------------------------------------------------------------------------
422 // MyApp
423 //-----------------------------------------------------------------------------
424
425 MyApp::MyApp(void) :
426 wxApp( )
427 {
428 };
429
430 bool MyApp::OnInit(void)
431 {
432 wxFrame *frame = new MyFrame();
433 frame->Show( TRUE );
434 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
435 return TRUE;
436 };
437
438
439
440
441