]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxLayout.cpp
Fixed flat style for toolbars under XP, Windows Classic style
[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 Ballüder.") );
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 =
187 #if wxICON_IS_BITMAP
188 new wxIcon(Micon_xpm)
189 #else
190 new wxBitmap (wxIcon(Micon_xpm))
191 #endif
192
193 ;
194
195 llist->Insert(new wxLayoutObjectIcon(icon));
196 llist->SetFontWeight(wxBOLD);
197 llist->Insert(_T("World! "));
198 llist->SetFontWeight(wxNORMAL);
199 llist->Insert(_T("The quick brown fox jumps..."));
200 llist->LineBreak();
201
202 llist->Insert(_T("over the lazy dog."));
203 llist->SetFont(-1,-1,-1,-1,true);
204 llist->Insert(_T("underlined"));
205 llist->SetFont(-1,-1,-1,-1,false);
206 llist->SetFont(wxROMAN);
207 llist->Insert(_T("This is "));
208 llist->SetFont(-1,-1,-1,wxBOLD);
209 llist->Insert(_T("BOLD "));
210 llist->SetFont(-1,-1,-1,wxNORMAL);
211 llist->Insert(_T("and "));
212 llist->SetFont(-1,-1,wxITALIC);
213 llist->Insert(_T("italics "));
214 llist->SetFont(-1,-1,wxNORMAL);
215 llist->LineBreak();
216
217 llist->Insert(_T("and "));
218 llist->SetFont(-1,-1,wxSLANT);
219 llist->Insert(_T("slanted"));
220 llist->SetFont(-1,-1,wxNORMAL);
221 llist->Insert(_T(" text."));
222 llist->LineBreak();
223
224 llist->Insert(_T("and "));
225 llist->SetFont(-1,-1,-1,-1,-1,_T("blue"));
226 llist->Insert(_T("blue"));
227 llist->SetFont(-1,-1,-1,-1,-1,_T("black"));
228 llist->Insert(_T(" and "));
229 llist->SetFont(-1,-1,-1,-1,-1,_T("green"),_T("black"));
230 llist->Insert(_T("green on black"));
231 llist->SetFont(-1,-1,-1,-1,-1,_T("black"),_T("white"));
232 llist->Insert(_T(" text."));
233 llist->LineBreak();
234
235 llist->SetFont(-1,-1,wxSLANT);
236 llist->Insert(_T("Slanted"));
237 llist->SetFont(-1,-1,wxNORMAL);
238 llist->Insert(_T(" and normal text and "));
239 llist->SetFont(-1,-1,wxSLANT);
240 llist->Insert(_T("slanted"));
241 llist->SetFont(-1,-1,wxNORMAL);
242 llist->Insert(_T(" again."));
243 llist->LineBreak();
244
245 // add some more text for testing:
246 llist->Insert(_T("And here the source for the test program:"));
247 llist->LineBreak();
248
249 llist->SetFont(wxTELETYPE,16);
250 llist->Insert(_T("And here the source for the test program:"));
251 llist->LineBreak();
252
253 wxTextFile file(_T("wxLayout.cpp"));
254 if ( file.Open() )
255 {
256 for ( wxString s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
257 {
258 wxString line;
259 llist->Insert(line.Format(_T("%6u: %s"),file.GetCurrentLine()+1,s.c_str()));
260 llist->LineBreak();
261 }
262 }
263
264 llist->MoveCursorTo(wxPoint(0,0));
265 m_lwin->SetDirty();
266 m_lwin->Refresh();
267 }
268
269 void MyFrame::Clear()
270 {
271 wxColour colBg(0, 0, 0);
272
273 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false, wxRED, &colBg);
274 }
275
276
277 void MyFrame::OnCommand( wxCommandEvent &event )
278 {
279 switch (event.GetId())
280 {
281 case ID_QUIT:
282 Close(true);
283 break;
284 case ID_PRINT:
285 {
286 wxPrinter printer;
287 wxLayoutPrintout printout(m_lwin->GetLayoutList(),_("M: Printout"));
288 if (! printer.Print(this, &printout, true))
289 {
290 // Had to remove the split up strings that used to be below, and
291 // put them into one long strong. Otherwise MSVC would give an
292 // error "C2308: concatenating mismatched wide strings" when
293 // building a Unicode version.
294 wxMessageBox
295 (
296 _("There was a problem with printing the message:\nperhaps your current printer is not set up correctly?"),
297 _("Printing"), wxOK
298 );
299 }
300 break;
301 }
302
303 case ID_NOWRAP:
304 case ID_WRAP:
305 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
306 break;
307 case ID_ADD_SAMPLE:
308 AddSampleText(m_lwin->GetLayoutList());
309 break;
310 case ID_CLEAR:
311 Clear();
312 break;
313 case ID_CLICK:
314 wxLogError( _T("Received click event.") );
315 break;
316 case ID_PASTE:
317 m_lwin->Paste(true);
318 m_lwin->Refresh(false);
319 break;
320 #ifdef __WXGTK__
321 case ID_PASTE_PRIMARY:
322 // text only from primary:
323 m_lwin->Paste(false, true);
324 m_lwin->Refresh(false);
325 break;
326 case ID_COPY_PRIMARY:
327 // copy text-only to primary selection:
328 m_lwin->Copy(false, false, true);
329 m_lwin->Refresh(false);
330 break;
331 #endif
332 case ID_COPY:
333 m_lwin->Copy(true, true, false);
334 m_lwin->Refresh(false);
335 break;
336 case ID_CUT:
337 m_lwin->Cut();
338 m_lwin->Refresh(false);
339 break;
340 #ifdef M_BASEDIR
341 case ID_FIND:
342 m_lwin->Find("void");
343 m_lwin->Refresh(false);
344 break;
345 #endif
346 case ID_HTML:
347 {
348 wxFileDialog
349 HTML_dialog( this,
350 _T("Save As HTML..."),
351 wxEmptyString,
352 wxEmptyString,
353 _T("HTML file (*.html)|*.html|Text file (*.txt)|*.txt|Any file (*)|*"),
354 wxSAVE|wxOVERWRITE_PROMPT
355 );
356 if (HTML_dialog.ShowModal() == wxID_OK)
357 {
358 wxFFileOutputStream output( HTML_dialog.GetPath() );
359 wxTextOutputStream textout( output );
360
361 wxLayoutExportObject *export0;
362 wxString object;
363 wxLayoutExportStatus status(m_lwin->GetLayoutList());
364 while((export0 = wxLayoutExport( &status, WXLO_EXPORT_AS_HTML)) != NULL)
365 {
366 if(export0->type == WXLO_EXPORT_HTML)
367 object = *(export0->content.text);
368 else
369 ; // ignore "<!--UNKNOWN OBJECT>";
370 delete export0;
371 textout << object;
372 }
373 }
374 break;
375 }
376
377 case ID_TEXT:
378 {
379 wxFileDialog
380 TEXT_dialog( this,
381 _T("Save As TXT..."),
382 wxEmptyString,
383 wxEmptyString,
384 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
385 wxSAVE|wxOVERWRITE_PROMPT
386 );
387 if (TEXT_dialog.ShowModal() == wxID_OK)
388 {
389 wxFFileOutputStream output( TEXT_dialog.GetPath() );
390 wxTextOutputStream textout( output );
391
392 wxLayoutExportObject *export0;
393 wxString object;
394 wxLayoutExportStatus status(m_lwin->GetLayoutList());
395 while((export0 = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
396 {
397 if(export0->type == WXLO_EXPORT_TEXT)
398 object = *(export0->content.text);
399 else
400 object = _T("<!--UNKNOWN OBJECT>");
401 delete export0;
402 textout << object;
403 }
404 }
405 break;
406 }
407
408 case ID_LONG_TEST:
409 {
410 wxString line;
411 wxLayoutList *llist = m_lwin->GetLayoutList();
412 for(int i = 1; i < 300; i++)
413 {
414 line.Printf(wxT("This is line number %d."), i);
415 llist->Insert(line);
416 llist->LineBreak();
417 }
418
419 llist->MoveCursorTo(wxPoint(0,0));
420 m_lwin->SetDirty();
421 m_lwin->Refresh();
422 break;
423 }
424
425 case ID_LINEBREAKS_TEST:
426 wxLayoutImportText
427 (
428 m_lwin->GetLayoutList(),
429 wxT("This is a text\nwith embedded line\nbreaks.\n")
430 );
431
432 m_lwin->SetDirty();
433 m_lwin->Refresh();
434 break;
435
436 case ID_URL_TEST:
437 // VZ: this doesn't work, of course, but I think it should -
438 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
439 // (instead of doing it manually like M does now)
440 m_lwin->GetLayoutList()->Insert(_T("http://www.wxwidgets.org/"));
441 m_lwin->Refresh();
442 }
443 };
444
445 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
446 {
447 #ifdef __WXMSW__
448 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
449 #else
450 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
451 #endif
452 wxPrinter printer;
453 wxLayoutPrintout printout( m_lwin->GetLayoutList(), _T("Printout from wxLayout"));
454 if (! printer.Print(this, &printout, true))
455 wxMessageBox(
456 _T("There was a problem printing.\nPerhaps your current printer is not set correctly?"),
457 _T("Printing"), wxOK);
458 }
459
460 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
461 {
462 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
463
464 #ifdef OS_UNIX
465 wxPostScriptPrinter printer;
466 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
467 printer.Print(this, &printout, true);
468 #endif
469 }
470
471 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
472 {
473 #ifdef __WXMSW__
474 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
475 #else
476 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
477 #endif
478 wxPrintData printData;
479
480 // Pass two printout objects: for preview, and possible printing.
481 wxPrintPreview *preview = new wxPrintPreview(new
482 wxLayoutPrintout(
483 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
484 if (!preview->Ok())
485 {
486 delete preview;
487 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK);
488 return;
489 }
490
491 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
492 frame->Centre(wxBOTH);
493 frame->Initialize();
494 frame->Show(true);
495 }
496
497 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
498 {
499 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
500
501 wxPrintData printData;
502
503 // Pass two printout objects: for preview, and possible printing.
504 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
505 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
506 frame->Centre(wxBOTH);
507 frame->Initialize();
508 frame->Show(true);
509 }
510
511 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
512 {
513 #ifdef OS_WIN
514 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
515 #else
516 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
517 #endif
518 wxPrintDialog printerDialog(this, & m_PrintData);
519 printerDialog.ShowModal();
520 }
521
522 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
523 {
524 #ifdef __WXMSW__
525 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
526 #else
527 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
528 #endif
529 wxPageSetupData data;
530
531 #if defined(__WXMSW__) || defined(__WXMAC__)
532 wxPageSetupDialog pageSetupDialog(this, & data);
533 #else
534 wxGenericPageSetupDialog pageSetupDialog(this, & data);
535 #endif
536 pageSetupDialog.ShowModal();
537
538 data = pageSetupDialog.GetPageSetupData();
539 }
540
541 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
542 {
543 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
544
545 wxPrintData data;
546
547 #if defined(__WXMSW__) || defined(__WXMAC__)
548 wxPrintDialog printerDialog(this, & data);
549 #else
550 wxGenericPrintDialog printerDialog(this, & data);
551 #endif
552 printerDialog.ShowModal();
553 }
554
555 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
556 {
557 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
558
559 wxPageSetupData data;
560 #if defined(__WXMSW__) || defined(__WXMAC__)
561 wxPageSetupDialog pageSetupDialog(this, & data);
562 #else
563 wxGenericPageSetupDialog pageSetupDialog(this, & data);
564 #endif
565 pageSetupDialog.ShowModal();
566 }
567
568
569 //-----------------------------------------------------------------------------
570 // MyApp
571 //-----------------------------------------------------------------------------
572
573 MyApp::MyApp() :
574 wxApp( )
575 {
576 };
577
578 bool MyApp::OnInit()
579 {
580 wxFrame *frame = new MyFrame();
581 wxInitAllImageHandlers();
582 frame->Show( true );
583 // wxSetAFMPath("/usr/local/src/wxWidgets/misc/afm/");
584 return true;
585 };
586
587
588
589
590