]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxLayout.cpp
bf18ebeae026233ede700263b91e9992448d7528
[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_TEST:
251 {
252 Clear();
253 m_lwin->GetLayoutList().LineBreak();
254 m_lwin->GetLayoutList().Insert("abc");
255 m_lwin->GetLayoutList().LineBreak();
256 m_lwin->GetLayoutList().Insert("def");
257 break;
258 }
259 case ID_HTML:
260 {
261 wxLayoutExportObject *export;
262 wxLayoutList::iterator i = m_lwin->GetLayoutList().begin();
263
264 while((export = wxLayoutExport(m_lwin->GetLayoutList(),
265 i,WXLO_EXPORT_AS_HTML)) != NULL)
266 {
267 if(export->type == WXLO_EXPORT_HTML)
268 cout << *(export->content.text);
269 else
270 cout << "<!--UNKNOWN OBJECT>";
271 delete export;
272 }
273 }
274 break;
275 case ID_TEXT:
276 {
277 wxLayoutExportObject *export;
278 wxLayoutList::iterator i = m_lwin->GetLayoutList().begin();
279
280 while((export = wxLayoutExport(m_lwin->GetLayoutList(),
281 i,WXLO_EXPORT_AS_TEXT)) != NULL)
282 {
283 if(export->type == WXLO_EXPORT_TEXT)
284 cout << *(export->content.text);
285 else
286 cout << "<!--UNKNOWN OBJECT>";
287 delete export;
288 }
289 }
290 break;
291 }
292 };
293
294 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
295 {
296 #ifdef __WXMSW__
297 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
298 #else
299 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
300 #endif
301 wxPrinter printer;
302 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
303 if (! printer.Print(this, &printout, TRUE))
304 wxMessageBox(
305 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
306 "Printing", wxOK);
307 }
308
309 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
310 {
311 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
312
313 wxPostScriptPrinter printer;
314 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
315 printer.Print(this, &printout, TRUE);
316 }
317
318 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
319 {
320 #ifdef __WXMSW__
321 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
322 #else
323 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
324 #endif
325 wxPrintData printData;
326 printData.SetOrientation(orientation);
327
328 // Pass two printout objects: for preview, and possible printing.
329 wxPrintPreview *preview = new wxPrintPreview(new
330 wxLayoutPrintout(
331 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
332 if (!preview->Ok())
333 {
334 delete preview;
335 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
336 return;
337 }
338
339 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
340 frame->Centre(wxBOTH);
341 frame->Initialize();
342 frame->Show(TRUE);
343 }
344
345 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
346 {
347 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
348
349 wxPrintData printData;
350 printData.SetOrientation(orientation);
351
352 // Pass two printout objects: for preview, and possible printing.
353 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
354 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
355 frame->Centre(wxBOTH);
356 frame->Initialize();
357 frame->Show(TRUE);
358 }
359
360 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
361 {
362 #ifdef __WXMSW__
363 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
364 #else
365 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
366 #endif
367 wxPrintData data;
368 data.SetOrientation(orientation);
369
370 #ifdef __WXMSW__
371 wxPrintDialog printerDialog(this, & data);
372 #else
373 wxGenericPrintDialog printerDialog(this, & data);
374 #endif
375 printerDialog.GetPrintData().SetSetupDialog(TRUE);
376 printerDialog.ShowModal();
377
378 orientation = printerDialog.GetPrintData().GetOrientation();
379 }
380
381 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
382 {
383 #ifdef __WXMSW__
384 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
385 #else
386 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
387 #endif
388 wxPageSetupData data;
389 data.SetOrientation(orientation);
390
391 #ifdef __WXMSW__
392 wxPageSetupDialog pageSetupDialog(this, & data);
393 #else
394 wxGenericPageSetupDialog pageSetupDialog(this, & data);
395 #endif
396 pageSetupDialog.ShowModal();
397
398 data = pageSetupDialog.GetPageSetupData();
399 orientation = data.GetOrientation();
400 }
401
402 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
403 {
404 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
405
406 wxPrintData data;
407 data.SetOrientation(orientation);
408
409 wxGenericPrintDialog printerDialog(this, & data);
410 printerDialog.GetPrintData().SetSetupDialog(TRUE);
411 printerDialog.ShowModal();
412
413 orientation = printerDialog.GetPrintData().GetOrientation();
414 }
415
416 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
417 {
418 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
419
420 wxPageSetupData data;
421 data.SetOrientation(orientation);
422
423 wxGenericPageSetupDialog pageSetupDialog(this, & data);
424 pageSetupDialog.ShowModal();
425
426 orientation = pageSetupDialog.GetPageSetupData().GetOrientation();
427 }
428
429
430 //-----------------------------------------------------------------------------
431 // MyApp
432 //-----------------------------------------------------------------------------
433
434 MyApp::MyApp(void) :
435 wxApp( )
436 {
437 };
438
439 bool MyApp::OnInit(void)
440 {
441 wxFrame *frame = new MyFrame();
442 frame->Show( TRUE );
443 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
444 return TRUE;
445 };
446
447
448
449
450