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