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