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