]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxLayout.cpp
Trace module initialization and cleanup.
[wxWidgets.git] / samples / richedit / 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 #include "wx/wxprec.h"
11 #ifdef __BORLANDC__
12 # pragma hdrstop
13 #endif
14
15 #include "wxLayout.h"
16 #include "wx/textfile.h"
17 #include "wx/image.h"
18
19 #if wxUSE_IOSTREAMH
20 #include <iostream.h>
21 #else
22 #include <iostream>
23 #endif
24
25 #include "wx/wfstream.h"
26 #include "wx/txtstrm.h"
27
28 #include "Micon.xpm"
29
30
31 //-----------------------------------------------------------------------------
32 // main program
33 //-----------------------------------------------------------------------------
34
35 IMPLEMENT_APP(MyApp)
36
37 //-----------------------------------------------------------------------------
38 // MyFrame
39 //-----------------------------------------------------------------------------
40
41 enum ids
42 {
43 ID_ADD_SAMPLE = 1, ID_CLEAR, ID_PRINT,
44 ID_PRINT_SETUP, ID_PAGE_SETUP, ID_PREVIEW, ID_PRINT_PS,
45 ID_PRINT_SETUP_PS, ID_PAGE_SETUP_PS,ID_PREVIEW_PS,
46 ID_WRAP, ID_NOWRAP, ID_PASTE, ID_COPY, ID_CUT,
47 ID_COPY_PRIMARY, ID_PASTE_PRIMARY,
48 ID_FIND,
49 ID_WXLAYOUT_DEBUG, ID_QUIT, ID_CLICK, ID_HTML, ID_TEXT,
50 ID_TEST, ID_LINEBREAKS_TEST, ID_LONG_TEST, ID_URL_TEST
51 };
52
53
54 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
55
56 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
57 EVT_MENU(ID_PRINT, MyFrame::OnPrint)
58 EVT_MENU(ID_PREVIEW, MyFrame::OnPrintPreview)
59 EVT_MENU(ID_PRINT_SETUP, MyFrame::OnPrintSetup)
60 EVT_MENU(ID_PAGE_SETUP, MyFrame::OnPageSetup)
61 EVT_MENU(ID_PRINT_PS, MyFrame::OnPrintPS)
62 EVT_MENU(ID_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
63 EVT_MENU(ID_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
64 EVT_MENU(ID_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
65 EVT_MENU (wxID_ANY, MyFrame::OnCommand)
66 EVT_COMMAND (wxID_ANY,wxID_ANY, MyFrame::OnCommand)
67 EVT_CHAR ( wxLayoutWindow::OnChar )
68 END_EVENT_TABLE()
69
70
71 MyFrame::MyFrame() :
72 wxFrame( (wxFrame *) NULL, wxID_ANY, _T("wxLayout"),
73 wxDefaultPosition, wxDefaultSize )
74 {
75 #if wxUSE_STATUSBAR
76 CreateStatusBar( 2 );
77 SetStatusText( _T("wxLayout by Karsten Ballueder.") );
78 #endif // wxUSE_STATUSBAR
79
80 wxMenuBar *menu_bar = new wxMenuBar();
81
82 wxMenu *file_menu = new wxMenu;
83 file_menu->Append(ID_PRINT, _T("&Print..."), _T("Print"));
84 file_menu->Append(ID_PRINT_SETUP, _T("Print &Setup..."),_T("Setup printer properties"));
85 file_menu->Append(ID_PAGE_SETUP, _T("Page Set&up..."), _T("Page setup"));
86 file_menu->Append(ID_PREVIEW, _T("Print Pre&view"), _T("Preview"));
87 #ifdef __WXMSW__
88 file_menu->AppendSeparator();
89 file_menu->Append(ID_PRINT_PS, _T("Print PostScript..."), _T("Print (PostScript)"));
90 file_menu->Append(ID_PRINT_SETUP_PS, _T("Print Setup PostScript..."), _T("Setup printer properties (PostScript)"));
91 file_menu->Append(ID_PAGE_SETUP_PS, _T("Page Setup PostScript..."), _T("Page setup (PostScript)"));
92 file_menu->Append(ID_PREVIEW_PS, _T("Print Preview PostScript"), _T("Preview (PostScript)"));
93 #endif
94 file_menu->AppendSeparator();
95 file_menu->Append( ID_TEXT, _T("Export &Text"));
96 file_menu->Append( ID_HTML, _T("Export &HTML"));
97 file_menu->Append( ID_QUIT, _T("E&xit"));
98 menu_bar->Append(file_menu, _T("&File"));
99
100 wxMenu *edit_menu = new wxMenu;
101 edit_menu->Append( ID_CLEAR, _T("C&lear"));
102 edit_menu->Append( ID_ADD_SAMPLE, _T("&Example"));
103 edit_menu->Append( ID_LONG_TEST, _T("Add &many lines"));
104 edit_menu->AppendSeparator();
105 edit_menu->Append( ID_LINEBREAKS_TEST, _T("Add &several lines"));
106 edit_menu->Append( ID_URL_TEST, _T("Insert an &URL"));
107 edit_menu->AppendSeparator();
108 edit_menu->Append(ID_WRAP, _T("&Wrap mode"), _T("Activate wrapping at pixel 200."));
109 edit_menu->Append(ID_NOWRAP, _T("&No-wrap mode"), _T("Deactivate wrapping."));
110 edit_menu->AppendSeparator();
111 edit_menu->Append(ID_COPY, _T("&Copy"), _T("Copy text to clipboard."));
112 edit_menu->Append(ID_CUT, _T("Cu&t"), _T("Cut text to clipboard."));
113 edit_menu->Append(ID_PASTE,_T("&Paste"), _T("Paste text from clipboard."));
114 #ifdef __WXGTK__
115 edit_menu->Append(ID_COPY_PRIMARY, _T("C&opy primary"), _T("Copy text to primary selecton."));
116 edit_menu->Append(ID_PASTE_PRIMARY, _T("&Paste primary"), _T("Paste text from primary selection."));
117 #endif
118 edit_menu->Append(ID_FIND, _T("&Find"), _T("Find text."));
119 menu_bar->Append(edit_menu, _T("&Edit") );
120
121 #ifndef __WXMSW__
122 menu_bar->Show(true);
123 #endif // MSW
124
125 SetMenuBar( menu_bar );
126
127 m_lwin = new wxLayoutWindow(this);
128 #if wxUSE_STATUSBAR
129 m_lwin->SetStatusBar(GetStatusBar(), 0, 1);
130 #endif // wxUSE_STATUSBAR
131 m_lwin->SetMouseTracking(true);
132 m_lwin->SetEditable(true);
133 m_lwin->SetWrapMargin(40);
134 m_lwin->SetFocus();
135
136 // JACS: under MSW, the window doesn't show the caret initially,
137 // and the following line I added doesn't help either.
138 // going to another window and then back again fixes it.
139 // m_lwin->OnSetFocus(wxFocusEvent());
140
141 Clear();
142
143 #if 0
144 // create and set the background bitmap (this will result in a lattice)
145 static const int sizeBmp = 10;
146 wxBitmap *bitmap = new wxBitmap(sizeBmp, sizeBmp);
147 wxMemoryDC dcMem;
148 dcMem.SelectObject( *bitmap );
149 dcMem.SetBackground( *wxWHITE_BRUSH );
150 dcMem.Clear();
151
152 dcMem.SetPen( *wxGREEN_PEN );
153 dcMem.DrawLine(sizeBmp/2, 0, sizeBmp/2, sizeBmp);
154 dcMem.DrawLine(0, sizeBmp/2, sizeBmp, sizeBmp/2);
155
156 dcMem.SelectObject( wxNullBitmap );
157
158 m_lwin->SetBackgroundBitmap(bitmap);
159 #endif // 0
160 };
161
162 void MyFrame::AddSampleText(wxLayoutList *llist)
163 {
164 llist->Clear(wxSWISS,16,wxNORMAL,wxNORMAL, false);
165 llist->SetFont(-1,-1,-1,-1,-1,_T("blue"));
166 llist->Insert(_T("blue"));
167 llist->LineBreak();
168
169 llist->SetFont(-1,-1,-1,-1,-1,_T("black"));
170 llist->Insert(_T("The quick brown fox jumps over the lazy dog."));
171 llist->LineBreak();
172
173 llist->SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
174 llist->Insert(_T("--"));
175 llist->LineBreak();
176
177 llist->SetFont(wxROMAN);
178 llist->Insert(_T("The quick brown fox jumps over the lazy dog."));
179 llist->LineBreak();
180
181 llist->Insert(_T("Hello "));
182 wxBitmap *icon = new wxBitmap (wxIcon(Micon_xpm));
183
184 llist->Insert(new wxLayoutObjectIcon(icon));
185 llist->SetFontWeight(wxBOLD);
186 llist->Insert(_T("World! "));
187 llist->SetFontWeight(wxNORMAL);
188 llist->Insert(_T("The quick brown fox jumps..."));
189 llist->LineBreak();
190
191 llist->Insert(_T("over the lazy dog."));
192 llist->SetFont(-1,-1,-1,-1,true);
193 llist->Insert(_T("underlined"));
194 llist->SetFont(-1,-1,-1,-1,false);
195 llist->SetFont(wxROMAN);
196 llist->Insert(_T("This is "));
197 llist->SetFont(-1,-1,-1,wxBOLD);
198 llist->Insert(_T("BOLD "));
199 llist->SetFont(-1,-1,-1,wxNORMAL);
200 llist->Insert(_T("and "));
201 llist->SetFont(-1,-1,wxITALIC);
202 llist->Insert(_T("italics "));
203 llist->SetFont(-1,-1,wxNORMAL);
204 llist->LineBreak();
205
206 llist->Insert(_T("and "));
207 llist->SetFont(-1,-1,wxSLANT);
208 llist->Insert(_T("slanted"));
209 llist->SetFont(-1,-1,wxNORMAL);
210 llist->Insert(_T(" text."));
211 llist->LineBreak();
212
213 llist->Insert(_T("and "));
214 llist->SetFont(-1,-1,-1,-1,-1,_T("blue"));
215 llist->Insert(_T("blue"));
216 llist->SetFont(-1,-1,-1,-1,-1,_T("black"));
217 llist->Insert(_T(" and "));
218 llist->SetFont(-1,-1,-1,-1,-1,_T("green"),_T("black"));
219 llist->Insert(_T("green on black"));
220 llist->SetFont(-1,-1,-1,-1,-1,_T("black"),_T("white"));
221 llist->Insert(_T(" text."));
222 llist->LineBreak();
223
224 llist->SetFont(-1,-1,wxSLANT);
225 llist->Insert(_T("Slanted"));
226 llist->SetFont(-1,-1,wxNORMAL);
227 llist->Insert(_T(" and normal text and "));
228 llist->SetFont(-1,-1,wxSLANT);
229 llist->Insert(_T("slanted"));
230 llist->SetFont(-1,-1,wxNORMAL);
231 llist->Insert(_T(" again."));
232 llist->LineBreak();
233
234 // add some more text for testing:
235 llist->Insert(_T("And here the source for the test program:"));
236 llist->LineBreak();
237
238 llist->SetFont(wxTELETYPE,16);
239 llist->Insert(_T("And here the source for the test program:"));
240 llist->LineBreak();
241
242 wxTextFile file(_T("wxLayout.cpp"));
243 if ( file.Open() )
244 {
245 for ( wxString s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
246 {
247 wxString line;
248 llist->Insert(line.Format(_T("%6u: %s"),file.GetCurrentLine()+1,s.c_str()));
249 llist->LineBreak();
250 }
251 }
252
253 llist->MoveCursorTo(wxPoint(0,0));
254 m_lwin->SetDirty();
255 m_lwin->Refresh();
256 }
257
258 void MyFrame::Clear()
259 {
260 wxColour colBg(0, 0, 0);
261
262 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false, wxRED, &colBg);
263 }
264
265
266 void MyFrame::OnCommand( wxCommandEvent &event )
267 {
268 switch (event.GetId())
269 {
270 case ID_QUIT:
271 Close(true);
272 break;
273 case ID_PRINT:
274 {
275 wxPrinter printer;
276 wxLayoutPrintout printout(m_lwin->GetLayoutList(),_("M: Printout"));
277 if (! printer.Print(this, &printout, true))
278 {
279 // Had to remove the split up strings that used to be below, and
280 // put them into one long strong. Otherwise MSVC would give an
281 // error "C2308: concatenating mismatched wide strings" when
282 // building a Unicode version.
283 wxMessageBox
284 (
285 _("There was a problem with printing the message:\nperhaps your current printer is not set up correctly?"),
286 _("Printing"), wxOK
287 );
288 }
289 break;
290 }
291
292 case ID_NOWRAP:
293 case ID_WRAP:
294 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
295 break;
296 case ID_ADD_SAMPLE:
297 AddSampleText(m_lwin->GetLayoutList());
298 break;
299 case ID_CLEAR:
300 Clear();
301 break;
302 case ID_CLICK:
303 wxLogError( _T("Received click event.") );
304 break;
305 case ID_PASTE:
306 m_lwin->Paste(true);
307 m_lwin->Refresh(false);
308 break;
309 #ifdef __WXGTK__
310 case ID_PASTE_PRIMARY:
311 // text only from primary:
312 m_lwin->Paste(false, true);
313 m_lwin->Refresh(false);
314 break;
315 case ID_COPY_PRIMARY:
316 // copy text-only to primary selection:
317 m_lwin->Copy(false, false, true);
318 m_lwin->Refresh(false);
319 break;
320 #endif
321 case ID_COPY:
322 m_lwin->Copy(true, true, false);
323 m_lwin->Refresh(false);
324 break;
325 case ID_CUT:
326 m_lwin->Cut();
327 m_lwin->Refresh(false);
328 break;
329 #ifdef M_BASEDIR
330 case ID_FIND:
331 m_lwin->Find("void");
332 m_lwin->Refresh(false);
333 break;
334 #endif
335 case ID_HTML:
336 {
337 wxFileDialog
338 HTML_dialog( this,
339 _T("Save As HTML..."),
340 wxEmptyString,
341 wxEmptyString,
342 _T("HTML file (*.html)|*.html|Text file (*.txt)|*.txt|Any file (*)|*"),
343 wxSAVE|wxOVERWRITE_PROMPT
344 );
345 if (HTML_dialog.ShowModal() == wxID_OK)
346 {
347 wxFFileOutputStream output( HTML_dialog.GetPath() );
348 wxTextOutputStream textout( output );
349
350 wxLayoutExportObject *export0;
351 wxString object;
352 wxLayoutExportStatus status(m_lwin->GetLayoutList());
353 while((export0 = wxLayoutExport( &status, WXLO_EXPORT_AS_HTML)) != NULL)
354 {
355 if(export0->type == WXLO_EXPORT_HTML)
356 object = *(export0->content.text);
357 else
358 ; // ignore "<!--UNKNOWN OBJECT>";
359 delete export0;
360 textout << object;
361 }
362 }
363 break;
364 }
365
366 case ID_TEXT:
367 {
368 wxFileDialog
369 TEXT_dialog( this,
370 _T("Save As TXT..."),
371 wxEmptyString,
372 wxEmptyString,
373 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
374 wxSAVE|wxOVERWRITE_PROMPT
375 );
376 if (TEXT_dialog.ShowModal() == wxID_OK)
377 {
378 wxFFileOutputStream output( TEXT_dialog.GetPath() );
379 wxTextOutputStream textout( output );
380
381 wxLayoutExportObject *export0;
382 wxString object;
383 wxLayoutExportStatus status(m_lwin->GetLayoutList());
384 while((export0 = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
385 {
386 if(export0->type == WXLO_EXPORT_TEXT)
387 object = *(export0->content.text);
388 else
389 object = _T("<!--UNKNOWN OBJECT>");
390 delete export0;
391 textout << object;
392 }
393 }
394 break;
395 }
396
397 case ID_LONG_TEST:
398 {
399 wxString line;
400 wxLayoutList *llist = m_lwin->GetLayoutList();
401 for(int i = 1; i < 300; i++)
402 {
403 line.Printf(wxT("This is line number %d."), i);
404 llist->Insert(line);
405 llist->LineBreak();
406 }
407
408 llist->MoveCursorTo(wxPoint(0,0));
409 m_lwin->SetDirty();
410 m_lwin->Refresh();
411 break;
412 }
413
414 case ID_LINEBREAKS_TEST:
415 wxLayoutImportText
416 (
417 m_lwin->GetLayoutList(),
418 wxT("This is a text\nwith embedded line\nbreaks.\n")
419 );
420
421 m_lwin->SetDirty();
422 m_lwin->Refresh();
423 break;
424
425 case ID_URL_TEST:
426 // VZ: this doesn't work, of course, but I think it should -
427 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
428 // (instead of doing it manually like M does now)
429 m_lwin->GetLayoutList()->Insert(_T("http://www.wxwidgets.org/"));
430 m_lwin->Refresh();
431 }
432 };
433
434 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
435 {
436 #ifdef __WXMSW__
437 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
438 #else
439 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
440 #endif
441 wxPrinter printer;
442 wxLayoutPrintout printout( m_lwin->GetLayoutList(), _T("Printout from wxLayout"));
443 if (! printer.Print(this, &printout, true))
444 wxMessageBox(
445 _T("There was a problem printing.\nPerhaps your current printer is not set correctly?"),
446 _T("Printing"), wxOK);
447 }
448
449 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
450 {
451 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
452
453 #ifdef OS_UNIX
454 wxPostScriptPrinter printer;
455 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
456 printer.Print(this, &printout, true);
457 #endif
458 }
459
460 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
461 {
462 #ifdef __WXMSW__
463 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
464 #else
465 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
466 #endif
467 wxPrintData printData;
468
469 // Pass two printout objects: for preview, and possible printing.
470 wxPrintPreview *preview = new wxPrintPreview(new
471 wxLayoutPrintout(
472 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
473 if (!preview->Ok())
474 {
475 delete preview;
476 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK);
477 return;
478 }
479
480 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
481 frame->Centre(wxBOTH);
482 frame->Initialize();
483 frame->Show(true);
484 }
485
486 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
487 {
488 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
489
490 wxPrintData printData;
491
492 // Pass two printout objects: for preview, and possible printing.
493 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
494 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
495 frame->Centre(wxBOTH);
496 frame->Initialize();
497 frame->Show(true);
498 }
499
500 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
501 {
502 #ifdef OS_WIN
503 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
504 #else
505 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
506 #endif
507 wxPrintDialog printerDialog(this, & m_PrintData);
508 printerDialog.ShowModal();
509 }
510
511 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
512 {
513 #ifdef __WXMSW__
514 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
515 #else
516 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
517 #endif
518 wxPageSetupDialogData data;
519
520 #if defined(__WXMSW__) || defined(__WXMAC__)
521 wxPageSetupDialog pageSetupDialog(this, & data);
522 #else
523 wxGenericPageSetupDialog pageSetupDialog(this, & data);
524 #endif
525 pageSetupDialog.ShowModal();
526
527 data = pageSetupDialog.GetPageSetupDialogData();
528 }
529
530 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
531 {
532 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
533
534 wxPrintData data;
535
536 #if defined(__WXMSW__) || defined(__WXMAC__)
537 wxPrintDialog printerDialog(this, & data);
538 #else
539 wxGenericPrintDialog printerDialog(this, & data);
540 #endif
541 printerDialog.ShowModal();
542 }
543
544 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
545 {
546 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
547
548 wxPageSetupData data;
549 #if defined(__WXMSW__) || defined(__WXMAC__)
550 wxPageSetupDialog pageSetupDialog(this, & data);
551 #else
552 wxGenericPageSetupDialog pageSetupDialog(this, & data);
553 #endif
554 pageSetupDialog.ShowModal();
555 }
556
557
558 //-----------------------------------------------------------------------------
559 // MyApp
560 //-----------------------------------------------------------------------------
561
562 MyApp::MyApp() :
563 wxApp( )
564 {
565 };
566
567 bool MyApp::OnInit()
568 {
569 wxFrame *frame = new MyFrame();
570 wxInitAllImageHandlers();
571 frame->Show( true );
572 // wxSetAFMPath("/usr/local/src/wxWidgets/misc/afm/");
573 return true;
574 };
575
576
577
578
579