]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxLayout.cpp
880a1dd6903dc9ee2ef3cb6fdc257eba402b703e
[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->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false);
103 m_lwin->SetFocus();
104 };
105
106 void
107 MyFrame::AddSampleText(wxLayoutList *llist)
108 {
109 llist->SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
110 llist->Insert("--");
111 llist->LineBreak();
112
113 llist->SetFont(wxROMAN);
114 llist->Insert("The quick brown fox jumps over the lazy dog.");
115 llist->LineBreak();
116 llist->Insert("Hello ");
117 llist->Insert(new wxLayoutObjectIcon(new wxICON(Micon)));
118 llist->SetFontWeight(wxBOLD);
119 llist->Insert("World! ");
120 llist->SetFontWeight(wxNORMAL);
121 llist->Insert("The quick brown fox jumps...");
122 llist->LineBreak();
123
124 llist->Insert("over the lazy dog.");
125 llist->SetFont(-1,-1,-1,-1,true);
126 llist->Insert("underlined");
127 llist->SetFont(-1,-1,-1,-1,false);
128 llist->SetFont(wxROMAN);
129 llist->Insert("This is ");
130 llist->SetFont(-1,-1,-1,wxBOLD); llist->Insert("BOLD "); llist->SetFont(-1,-1,-1,wxNORMAL);
131 llist->Insert("and ");
132 llist->SetFont(-1,-1,wxITALIC);
133 llist->Insert("italics ");
134 llist->SetFont(-1,-1,wxNORMAL);
135 llist->LineBreak();
136
137 llist->Insert("and ");
138 llist->SetFont(-1,-1,wxSLANT);
139 llist->Insert("slanted");
140 llist->SetFont(-1,-1,wxNORMAL);
141 llist->Insert(" text.");
142 llist->LineBreak();
143
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->GetLayoutList()->SetWrapMargin(
215 //// event.GetId() == ID_NOWRAP ? -1 : 40);
216 break;
217 case ID_ADD_SAMPLE:
218 AddSampleText(m_lwin->GetLayoutList());
219 break;
220 case ID_CLEAR:
221 Clear();
222 break;
223 case ID_CLICK:
224 cerr << "Received click event." << endl;
225 break;
226 case ID_HTML:
227 {
228 wxLayoutExportObject *export;
229 wxLayoutExportStatus status(m_lwin->GetLayoutList());
230
231 while((export = wxLayoutExport( &status,
232 WXLO_EXPORT_AS_HTML)) != NULL)
233 {
234 if(export->type == WXLO_EXPORT_HTML)
235 cout << *(export->content.text);
236 else
237 cout << "<!--UNKNOWN OBJECT>";
238 delete export;
239 }
240 }
241 break;
242 case ID_TEXT:
243 {
244 wxLayoutExportObject *export;
245 wxLayoutExportStatus status(m_lwin->GetLayoutList());
246
247 while((export = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
248 {
249 if(export->type == WXLO_EXPORT_TEXT)
250 cout << *(export->content.text);
251 else
252 cout << "<!--UNKNOWN OBJECT>";
253 delete export;
254 }
255 }
256 break;
257 }
258 };
259
260 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
261 {
262 #ifdef __WXMSW__
263 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
264 #else
265 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
266 #endif
267 wxPrinter printer;
268 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"Printout from wxLayout");
269 if (! printer.Print(this, &printout, TRUE))
270 wxMessageBox(
271 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
272 "Printing", wxOK);
273 }
274
275 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
276 {
277 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
278
279 #ifdef OS_UNIX
280 wxPostScriptPrinter printer;
281 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
282 printer.Print(this, &printout, TRUE);
283 #endif
284 }
285
286 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
287 {
288 #ifdef __WXMSW__
289 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
290 #else
291 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
292 #endif
293 wxPrintData printData;
294 printData.SetOrientation(orientation);
295
296 // Pass two printout objects: for preview, and possible printing.
297 wxPrintPreview *preview = new wxPrintPreview(new
298 wxLayoutPrintout(
299 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
300 if (!preview->Ok())
301 {
302 delete preview;
303 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
304 return;
305 }
306
307 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
308 frame->Centre(wxBOTH);
309 frame->Initialize();
310 frame->Show(TRUE);
311 }
312
313 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
314 {
315 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
316
317 wxPrintData printData;
318 printData.SetOrientation(orientation);
319
320 // Pass two printout objects: for preview, and possible printing.
321 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
322 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
323 frame->Centre(wxBOTH);
324 frame->Initialize();
325 frame->Show(TRUE);
326 }
327
328 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
329 {
330 #ifdef __WXMSW__
331 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
332 #else
333 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
334 #endif
335 wxPrintData data;
336 data.SetOrientation(orientation);
337
338 #ifdef __WXMSW__
339 wxPrintDialog printerDialog(this, & data);
340 #else
341 wxGenericPrintDialog printerDialog(this, & data);
342 #endif
343 printerDialog.GetPrintData().SetSetupDialog(TRUE);
344 printerDialog.ShowModal();
345
346 orientation = printerDialog.GetPrintData().GetOrientation();
347 }
348
349 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
350 {
351 #ifdef __WXMSW__
352 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
353 #else
354 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
355 #endif
356 wxPageSetupData data;
357 data.SetOrientation(orientation);
358
359 #ifdef __WXMSW__
360 wxPageSetupDialog pageSetupDialog(this, & data);
361 #else
362 wxGenericPageSetupDialog pageSetupDialog(this, & data);
363 #endif
364 pageSetupDialog.ShowModal();
365
366 data = pageSetupDialog.GetPageSetupData();
367 orientation = data.GetOrientation();
368 }
369
370 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
371 {
372 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
373
374 wxPrintData data;
375 data.SetOrientation(orientation);
376
377 #ifdef __WXMSW__
378 wxPrintDialog printerDialog(this, & data);
379 #else
380 wxGenericPrintDialog printerDialog(this, & data);
381 #endif
382 printerDialog.GetPrintData().SetSetupDialog(TRUE);
383 printerDialog.ShowModal();
384
385 orientation = printerDialog.GetPrintData().GetOrientation();
386 }
387
388 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
389 {
390 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
391
392 wxPageSetupData data;
393 data.SetOrientation(orientation);
394
395 #ifdef __WXMSW__
396 wxPageSetupDialog pageSetupDialog(this, & data);
397 #else
398 wxGenericPageSetupDialog pageSetupDialog(this, & data);
399 #endif
400 pageSetupDialog.ShowModal();
401
402 orientation = pageSetupDialog.GetPageSetupData().GetOrientation();
403 }
404
405
406 //-----------------------------------------------------------------------------
407 // MyApp
408 //-----------------------------------------------------------------------------
409
410 MyApp::MyApp(void) :
411 wxApp( )
412 {
413 };
414
415 bool MyApp::OnInit(void)
416 {
417 wxFrame *frame = new MyFrame();
418 frame->Show( TRUE );
419 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
420 return TRUE;
421 };
422
423
424
425
426