]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxLayout.cpp
504fef1d1f06d676aa6ee6be684b9910aa01a61b
[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, 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 wxPostScriptPrinter printer;
279 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
280 printer.Print(this, &printout, TRUE);
281 }
282
283 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
284 {
285 #ifdef __WXMSW__
286 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
287 #else
288 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
289 #endif
290 wxPrintData printData;
291 printData.SetOrientation(orientation);
292
293 // Pass two printout objects: for preview, and possible printing.
294 wxPrintPreview *preview = new wxPrintPreview(new
295 wxLayoutPrintout(
296 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
297 if (!preview->Ok())
298 {
299 delete preview;
300 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
301 return;
302 }
303
304 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
305 frame->Centre(wxBOTH);
306 frame->Initialize();
307 frame->Show(TRUE);
308 }
309
310 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
311 {
312 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
313
314 wxPrintData printData;
315 printData.SetOrientation(orientation);
316
317 // Pass two printout objects: for preview, and possible printing.
318 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
319 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
320 frame->Centre(wxBOTH);
321 frame->Initialize();
322 frame->Show(TRUE);
323 }
324
325 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
326 {
327 #ifdef __WXMSW__
328 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
329 #else
330 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
331 #endif
332 wxPrintData data;
333 data.SetOrientation(orientation);
334
335 #ifdef __WXMSW__
336 wxPrintDialog printerDialog(this, & data);
337 #else
338 wxGenericPrintDialog printerDialog(this, & data);
339 #endif
340 printerDialog.GetPrintData().SetSetupDialog(TRUE);
341 printerDialog.ShowModal();
342
343 orientation = printerDialog.GetPrintData().GetOrientation();
344 }
345
346 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
347 {
348 #ifdef __WXMSW__
349 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
350 #else
351 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
352 #endif
353 wxPageSetupData data;
354 data.SetOrientation(orientation);
355
356 #ifdef __WXMSW__
357 wxPageSetupDialog pageSetupDialog(this, & data);
358 #else
359 wxGenericPageSetupDialog pageSetupDialog(this, & data);
360 #endif
361 pageSetupDialog.ShowModal();
362
363 data = pageSetupDialog.GetPageSetupData();
364 orientation = data.GetOrientation();
365 }
366
367 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
368 {
369 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
370
371 wxPrintData data;
372 data.SetOrientation(orientation);
373
374 wxGenericPrintDialog printerDialog(this, & data);
375 printerDialog.GetPrintData().SetSetupDialog(TRUE);
376 printerDialog.ShowModal();
377
378 orientation = printerDialog.GetPrintData().GetOrientation();
379 }
380
381 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
382 {
383 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
384
385 wxPageSetupData data;
386 data.SetOrientation(orientation);
387
388 wxGenericPageSetupDialog pageSetupDialog(this, & data);
389 pageSetupDialog.ShowModal();
390
391 orientation = pageSetupDialog.GetPageSetupData().GetOrientation();
392 }
393
394
395 //-----------------------------------------------------------------------------
396 // MyApp
397 //-----------------------------------------------------------------------------
398
399 MyApp::MyApp(void) :
400 wxApp( )
401 {
402 };
403
404 bool MyApp::OnInit(void)
405 {
406 wxFrame *frame = new MyFrame();
407 frame->Show( TRUE );
408 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
409 return TRUE;
410 };
411
412
413
414
415