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