]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxLayout.cpp
441580e3a58f8d664ac98e7aeeeda041b72c915c
[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_ADD_SAMPLE = 1, 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_WRAP, ID_NOWRAP,
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
70 file_menu->Append(ID_PRINT, "&Print...", "Print");
71 file_menu->Append(ID_PRINT_SETUP, "Print &Setup...","Setup printer properties");
72 file_menu->Append(ID_PAGE_SETUP, "Page Set&up...", "Page setup");
73 file_menu->Append(ID_PREVIEW, "Print Pre&view", "Preview");
74 #ifdef __WXMSW__
75 file_menu->AppendSeparator();
76 file_menu->Append(ID_PRINT_PS, "Print PostScript...", "Print (PostScript)");
77 file_menu->Append(ID_PRINT_SETUP_PS, "Print Setup PostScript...", "Setup printer properties (PostScript)");
78 file_menu->Append(ID_PAGE_SETUP_PS, "Page Setup PostScript...", "Page setup (PostScript)");
79 file_menu->Append(ID_PREVIEW_PS, "Print Preview PostScript", "Preview (PostScript)");
80 #endif
81 file_menu->AppendSeparator();
82 file_menu->Append(ID_WRAP, "Wrap mode", "Activate wrapping at pixel 200.");
83 file_menu->Append(ID_NOWRAP, "No-wrap mode", "Deactivate wrapping.");
84
85 file_menu->AppendSeparator();
86 file_menu->Append( ID_TEXT, "Export Text");
87 file_menu->Append( ID_HTML, "Export HTML");
88 file_menu->Append( ID_QUIT, "Exit");
89
90 wxMenuBar *menu_bar = new wxMenuBar();
91 menu_bar->Append(file_menu, "File" );
92
93 #ifndef __WXMSW__
94 menu_bar->Show( TRUE );
95 #endif // MSW
96
97 SetMenuBar( menu_bar );
98
99 m_lwin = new wxLayoutWindow(this);
100 m_lwin->SetMouseTracking(true);
101 m_lwin->SetEditable(true);
102 m_lwin->SetWrapMargin(40);
103 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
104 m_lwin->SetFocus();
105 };
106
107 void
108 MyFrame::AddSampleText(wxLayoutList *llist)
109 {
110 llist->SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
111 llist->Insert("--");
112 llist->LineBreak();
113
114 llist->SetFont(wxROMAN);
115 llist->Insert("The quick brown fox jumps over the lazy dog.");
116 llist->LineBreak();
117 llist->Insert("Hello ");
118 llist->Insert(new wxLayoutObjectIcon(new wxICON(Micon)));
119 llist->SetFontWeight(wxBOLD);
120 llist->Insert("World! ");
121 llist->SetFontWeight(wxNORMAL);
122 llist->Insert("The quick brown fox jumps...");
123 llist->LineBreak();
124
125 llist->Insert("over the lazy dog.");
126 llist->SetFont(-1,-1,-1,-1,true);
127 llist->Insert("underlined");
128 llist->SetFont(-1,-1,-1,-1,false);
129 llist->SetFont(wxROMAN);
130 llist->Insert("This is ");
131 llist->SetFont(-1,-1,-1,wxBOLD); llist->Insert("BOLD "); llist->SetFont(-1,-1,-1,wxNORMAL);
132 llist->Insert("and ");
133 llist->SetFont(-1,-1,wxITALIC);
134 llist->Insert("italics ");
135 llist->SetFont(-1,-1,wxNORMAL);
136 llist->LineBreak();
137
138 llist->Insert("and ");
139 llist->SetFont(-1,-1,wxSLANT);
140 llist->Insert("slanted");
141 llist->SetFont(-1,-1,wxNORMAL);
142 llist->Insert(" text.");
143 llist->LineBreak();
144 llist->Insert("and ");
145 llist->SetFont(-1,-1,-1,-1,-1,"blue");
146 llist->Insert("blue");
147 llist->SetFont(-1,-1,-1,-1,-1,"black");
148 llist->Insert(" and ");
149 llist->SetFont(-1,-1,-1,-1,-1,"green","black");
150 llist->Insert("green on black");
151 llist->SetFont(-1,-1,-1,-1,-1,"black","white");
152 llist->Insert(" text.");
153 llist->LineBreak();
154
155 llist->SetFont(-1,-1,wxSLANT);
156 llist->Insert("Slanted");
157 llist->SetFont(-1,-1,wxNORMAL);
158 llist->Insert(" and normal text and ");
159 llist->SetFont(-1,-1,wxSLANT);
160 llist->Insert("slanted");
161 llist->SetFont(-1,-1,wxNORMAL);
162 llist->Insert(" again.");
163 llist->LineBreak();
164
165 // add some more text for testing:
166 llist->Insert("And here the source for the test program:");
167 llist->LineBreak();
168 llist->SetFont(wxTELETYPE,16);
169 char buffer[1024];
170 FILE *in = fopen("wxLayout.cpp","r");
171 if(in)
172 {
173 for(;;)
174 {
175 fgets(buffer,1024,in);
176 if(feof(in))
177 break;
178 llist->Insert(buffer);
179 llist->LineBreak();
180 }
181 }
182 llist->MoveCursorTo(wxPoint(0,0));
183 m_lwin->SetDirty();
184 m_lwin->Refresh();
185 }
186
187 void
188 MyFrame::Clear(void)
189 {
190 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
191 }
192
193
194 void MyFrame::OnCommand( wxCommandEvent &event )
195 {
196 switch (event.GetId())
197 {
198 case ID_QUIT:
199 Close( TRUE );
200 break;
201 case ID_PRINT:
202 {
203 wxPrinter printer;
204 wxLayoutPrintout printout(m_lwin->GetLayoutList(),_("M: Printout"));
205 if (! printer.Print(this, &printout, TRUE))
206 wxMessageBox(
207 _("There was a problem with printing the message:\n"
208 "perhaps your current printer is not set up correctly?"),
209 _("Printing"), wxOK);
210 }
211 break;
212 case ID_NOWRAP:
213 case ID_WRAP:
214 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
215 break;
216 case ID_ADD_SAMPLE:
217 AddSampleText(m_lwin->GetLayoutList());
218 break;
219 case ID_CLEAR:
220 Clear();
221 break;
222 case ID_CLICK:
223 cerr << "Received click event." << endl;
224 break;
225 case ID_HTML:
226 {
227 wxLayoutExportObject *export;
228 wxLayoutExportStatus status(m_lwin->GetLayoutList());
229
230 while((export = wxLayoutExport( &status,
231 WXLO_EXPORT_AS_HTML)) != NULL)
232 {
233 if(export->type == WXLO_EXPORT_HTML)
234 cout << *(export->content.text);
235 else
236 cout << "<!--UNKNOWN OBJECT>";
237 delete export;
238 }
239 }
240 break;
241 case ID_TEXT:
242 {
243 wxLayoutExportObject *export;
244 wxLayoutExportStatus status(m_lwin->GetLayoutList());
245
246 while((export = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
247 {
248 if(export->type == WXLO_EXPORT_TEXT)
249 cout << *(export->content.text);
250 else
251 cout << "<!--UNKNOWN OBJECT>";
252 delete export;
253 }
254 }
255 break;
256 }
257 };
258
259 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
260 {
261 #ifdef __WXMSW__
262 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
263 #else
264 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
265 #endif
266 wxPrinter printer;
267 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"Printout from wxLayout");
268 if (! printer.Print(this, &printout, TRUE))
269 wxMessageBox(
270 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
271 "Printing", wxOK);
272 }
273
274 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
275 {
276 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
277
278 #ifdef OS_UNIX
279 wxPostScriptPrinter printer;
280 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
281 printer.Print(this, &printout, TRUE);
282 #endif
283 }
284
285 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
286 {
287 #ifdef __WXMSW__
288 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
289 #else
290 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
291 #endif
292 wxPrintData printData;
293 printData.SetOrientation(orientation);
294
295 // Pass two printout objects: for preview, and possible printing.
296 wxPrintPreview *preview = new wxPrintPreview(new
297 wxLayoutPrintout(
298 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
299 if (!preview->Ok())
300 {
301 delete preview;
302 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
303 return;
304 }
305
306 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
307 frame->Centre(wxBOTH);
308 frame->Initialize();
309 frame->Show(TRUE);
310 }
311
312 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
313 {
314 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
315
316 wxPrintData printData;
317 printData.SetOrientation(orientation);
318
319 // Pass two printout objects: for preview, and possible printing.
320 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
321 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
322 frame->Centre(wxBOTH);
323 frame->Initialize();
324 frame->Show(TRUE);
325 }
326
327 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
328 {
329 #ifdef __WXMSW__
330 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
331 #else
332 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
333 #endif
334 wxPrintData data;
335 data.SetOrientation(orientation);
336
337 #ifdef __WXMSW__
338 wxPrintDialog printerDialog(this, & data);
339 #else
340 wxGenericPrintDialog printerDialog(this, & data);
341 #endif
342 printerDialog.GetPrintData().SetSetupDialog(TRUE);
343 printerDialog.ShowModal();
344
345 orientation = printerDialog.GetPrintData().GetOrientation();
346 }
347
348 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
349 {
350 #ifdef __WXMSW__
351 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
352 #else
353 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
354 #endif
355 wxPageSetupData data;
356 data.SetOrientation(orientation);
357
358 #ifdef __WXMSW__
359 wxPageSetupDialog pageSetupDialog(this, & data);
360 #else
361 wxGenericPageSetupDialog pageSetupDialog(this, & data);
362 #endif
363 pageSetupDialog.ShowModal();
364
365 data = pageSetupDialog.GetPageSetupData();
366 orientation = data.GetOrientation();
367 }
368
369 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
370 {
371 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
372
373 wxPrintData data;
374 data.SetOrientation(orientation);
375
376 #ifdef __WXMSW__
377 wxPrintDialog printerDialog(this, & data);
378 #else
379 wxGenericPrintDialog printerDialog(this, & data);
380 #endif
381 printerDialog.GetPrintData().SetSetupDialog(TRUE);
382 printerDialog.ShowModal();
383
384 orientation = printerDialog.GetPrintData().GetOrientation();
385 }
386
387 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
388 {
389 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
390
391 wxPageSetupData data;
392 data.SetOrientation(orientation);
393
394 #ifdef __WXMSW__
395 wxPageSetupDialog pageSetupDialog(this, & data);
396 #else
397 wxGenericPageSetupDialog pageSetupDialog(this, & data);
398 #endif
399 pageSetupDialog.ShowModal();
400
401 orientation = pageSetupDialog.GetPageSetupData().GetOrientation();
402 }
403
404
405 //-----------------------------------------------------------------------------
406 // MyApp
407 //-----------------------------------------------------------------------------
408
409 MyApp::MyApp(void) :
410 wxApp( )
411 {
412 };
413
414 bool MyApp::OnInit(void)
415 {
416 wxFrame *frame = new MyFrame();
417 frame->Show( TRUE );
418 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
419 return TRUE;
420 };
421
422
423
424
425