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