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