]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxLayout.cpp
reversed order of configurations to make the IDE happy about the default one
[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 "Micon.xpm"
30
31
32 //-----------------------------------------------------------------------------
33 // main program
34 //-----------------------------------------------------------------------------
35
36 IMPLEMENT_APP(MyApp)
37
38 //-----------------------------------------------------------------------------
39 // MyFrame
40 //-----------------------------------------------------------------------------
41
42 enum ids
43 {
44 ID_ADD_SAMPLE = 1, ID_CLEAR, ID_PRINT,
45 ID_PRINT_SETUP, ID_PAGE_SETUP, ID_PREVIEW, ID_PRINT_PS,
46 ID_PRINT_SETUP_PS, ID_PAGE_SETUP_PS,ID_PREVIEW_PS,
47 ID_WRAP, ID_NOWRAP, ID_PASTE, ID_COPY, ID_CUT,
48 ID_COPY_PRIMARY, ID_PASTE_PRIMARY,
49 ID_FIND,
50 ID_WXLAYOUT_DEBUG, ID_QUIT, ID_CLICK, ID_HTML, ID_TEXT,
51 ID_TEST, ID_LINEBREAKS_TEST, ID_LONG_TEST, ID_URL_TEST
52 };
53
54
55 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
56
57 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
58 EVT_MENU(ID_PRINT, MyFrame::OnPrint)
59 EVT_MENU(ID_PREVIEW, MyFrame::OnPrintPreview)
60 EVT_MENU(ID_PRINT_SETUP, MyFrame::OnPrintSetup)
61 EVT_MENU(ID_PAGE_SETUP, MyFrame::OnPageSetup)
62 EVT_MENU(ID_PRINT_PS, MyFrame::OnPrintPS)
63 EVT_MENU(ID_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
64 EVT_MENU(ID_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS)
65 EVT_MENU(ID_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
66 EVT_MENU (-1, MyFrame::OnCommand)
67 EVT_COMMAND (-1,-1, MyFrame::OnCommand)
68 EVT_CHAR ( wxLayoutWindow::OnChar )
69 END_EVENT_TABLE()
70
71
72 MyFrame::MyFrame(void) :
73 wxFrame( (wxFrame *) NULL, -1, _T("wxLayout"),
74 wxPoint(880,100), wxSize(256,256) )
75 {
76 CreateStatusBar( 2 );
77
78 SetStatusText( _T("wxLayout by Karsten Ballüder.") );
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 m_lwin->SetStatusBar(GetStatusBar(), 0, 1);
129 m_lwin->SetMouseTracking(true);
130 m_lwin->SetEditable(true);
131 m_lwin->SetWrapMargin(40);
132 m_lwin->SetFocus();
133
134 // JACS: under MSW, the window doesn't show the caret initially,
135 // and the following line I added doesn't help either.
136 // going to another window and then back again fixes it.
137 // m_lwin->OnSetFocus(wxFocusEvent());
138
139 Clear();
140
141 #if 0
142 // create and set the background bitmap (this will result in a lattice)
143 static const int sizeBmp = 10;
144 wxBitmap *bitmap = new wxBitmap(sizeBmp, sizeBmp);
145 wxMemoryDC dcMem;
146 dcMem.SelectObject( *bitmap );
147 dcMem.SetBackground( *wxWHITE_BRUSH );
148 dcMem.Clear();
149
150 dcMem.SetPen( *wxGREEN_PEN );
151 dcMem.DrawLine(sizeBmp/2, 0, sizeBmp/2, sizeBmp);
152 dcMem.DrawLine(0, sizeBmp/2, sizeBmp, sizeBmp/2);
153
154 dcMem.SelectObject( wxNullBitmap );
155
156 m_lwin->SetBackgroundBitmap(bitmap);
157 #endif // 0
158 };
159
160 void
161 MyFrame::AddSampleText(wxLayoutList *llist)
162 {
163 llist->Clear(wxSWISS,16,wxNORMAL,wxNORMAL, false);
164 llist->SetFont(-1,-1,-1,-1,-1,_T("blue"));
165 llist->Insert(_T("blue"));
166 llist->SetFont(-1,-1,-1,-1,-1,_T("black"));
167 llist->Insert(_T("The quick brown fox jumps over the lazy dog."));
168 llist->LineBreak();
169
170 llist->SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
171 llist->Insert(_T("--"));
172 llist->LineBreak();
173
174 llist->SetFont(wxROMAN);
175 llist->Insert(_T("The quick brown fox jumps over the lazy dog."));
176 llist->LineBreak();
177 llist->Insert(_T("Hello "));
178 #if wxICON_IS_BITMAP
179 llist->Insert(new wxLayoutObjectIcon(new wxICON(Micon)));
180 #else
181 llist->Insert(new wxLayoutObjectIcon(new wxBitmap (wxICON(Micon))));
182 #endif
183 llist->SetFontWeight(wxBOLD);
184 llist->Insert(_T("World! "));
185 llist->SetFontWeight(wxNORMAL);
186 llist->Insert(_T("The quick brown fox jumps..."));
187 llist->LineBreak();
188 llist->Insert(_T("over the lazy dog."));
189 llist->SetFont(-1,-1,-1,-1,true);
190 llist->Insert(_T("underlined"));
191 llist->SetFont(-1,-1,-1,-1,false);
192 llist->SetFont(wxROMAN);
193 llist->Insert(_T("This is "));
194 llist->SetFont(-1,-1,-1,wxBOLD); llist->Insert(_T("BOLD ")); llist->SetFont(-1,-1,-1,wxNORMAL);
195 llist->Insert(_T("and "));
196 llist->SetFont(-1,-1,wxITALIC);
197 llist->Insert(_T("italics "));
198 llist->SetFont(-1,-1,wxNORMAL);
199 llist->LineBreak();
200 llist->Insert(_T("and "));
201 llist->SetFont(-1,-1,wxSLANT);
202 llist->Insert(_T("slanted"));
203 llist->SetFont(-1,-1,wxNORMAL);
204 llist->Insert(_T(" text."));
205 llist->LineBreak();
206 llist->Insert(_T("and "));
207 llist->SetFont(-1,-1,-1,-1,-1,_T("blue"));
208 llist->Insert(_T("blue"));
209 llist->SetFont(-1,-1,-1,-1,-1,_T("black"));
210 llist->Insert(_T(" and "));
211 llist->SetFont(-1,-1,-1,-1,-1,_T("green"),_T("black"));
212 llist->Insert(_T("green on black"));
213 llist->SetFont(-1,-1,-1,-1,-1,_T("black"),_T("white"));
214 llist->Insert(_T(" text."));
215 llist->LineBreak();
216
217 llist->SetFont(-1,-1,wxSLANT);
218 llist->Insert(_T("Slanted"));
219 llist->SetFont(-1,-1,wxNORMAL);
220 llist->Insert(_T(" and normal text and "));
221 llist->SetFont(-1,-1,wxSLANT);
222 llist->Insert(_T("slanted"));
223 llist->SetFont(-1,-1,wxNORMAL);
224 llist->Insert(_T(" again."));
225 llist->LineBreak();
226
227 // add some more text for testing:
228 llist->Insert(_T("And here the source for the test program:"));
229 llist->LineBreak();
230 llist->SetFont(wxTELETYPE,16);
231 llist->Insert(_T("And here the source for the test program:"));
232 llist->LineBreak();
233 llist->Insert(_T("And here the source for the test program:"));
234 llist->LineBreak();
235 llist->Insert(_T("And here the source for the test program:"));
236
237 wxFile file( _T("wxLayout.cpp") );
238 if ( file.IsOpened() )
239 {
240 off_t len = file.Length();
241 wxChar *data = (wxChar *)malloc(2*len);
242 if ( file.Read(data, len) == len )
243 {
244 wxLayoutImportText(llist, data);
245 }
246 free(data);
247 }
248
249 llist->MoveCursorTo(wxPoint(0,0));
250 m_lwin->SetDirty();
251 m_lwin->Refresh();
252 }
253
254 void
255 MyFrame::Clear(void)
256 {
257 wxColour colBg(0, 0, 0);
258
259 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false, wxRED, &colBg);
260 }
261
262
263 void MyFrame::OnCommand( wxCommandEvent &event )
264 {
265 switch (event.GetId())
266 {
267 case ID_QUIT:
268 Close( TRUE );
269 break;
270 case ID_PRINT:
271 {
272 wxPrinter printer;
273 wxLayoutPrintout printout(m_lwin->GetLayoutList(),_("M: Printout"));
274 if (! printer.Print(this, &printout, TRUE))
275 {
276 // Had to remove the split up strings that used to be below, and
277 // put them into one long strong. Otherwise MSVC would give an
278 // error "C2308: concatenating mismatched wide strings" when
279 // building a Unicode version.
280 wxMessageBox
281 (
282 _("There was a problem with printing the message:\nperhaps your current printer is not set up correctly?"),
283 _("Printing"), wxOK
284 );
285 }
286 break;
287 }
288
289 case ID_NOWRAP:
290 case ID_WRAP:
291 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
292 break;
293 case ID_ADD_SAMPLE:
294 AddSampleText(m_lwin->GetLayoutList());
295 break;
296 case ID_CLEAR:
297 Clear();
298 break;
299 case ID_CLICK:
300 cerr << "Received click event." << endl;
301 break;
302 case ID_PASTE:
303 m_lwin->Paste(TRUE);
304 m_lwin->Refresh(FALSE);
305 break;
306 #ifdef __WXGTK__
307 case ID_PASTE_PRIMARY:
308 // text only from primary:
309 m_lwin->Paste(FALSE, TRUE);
310 m_lwin->Refresh(FALSE);
311 break;
312 case ID_COPY_PRIMARY:
313 // copy text-only to primary selection:
314 m_lwin->Copy(FALSE,FALSE,TRUE);
315 m_lwin->Refresh(FALSE);
316 break;
317 #endif
318 case ID_COPY:
319 m_lwin->Copy(TRUE,TRUE,FALSE);
320 m_lwin->Refresh(FALSE);
321 break;
322 case ID_CUT:
323 m_lwin->Cut();
324 m_lwin->Refresh(FALSE);
325 break;
326 #ifdef M_BASEDIR
327 case ID_FIND:
328 m_lwin->Find("void");
329 m_lwin->Refresh(FALSE);
330 break;
331 #endif
332 case ID_HTML:
333 {
334 wxLayoutExportObject *export0;
335 wxLayoutExportStatus status(m_lwin->GetLayoutList());
336
337 cout << "<HTML>" << endl;
338 while((export0 = wxLayoutExport( &status,
339 WXLO_EXPORT_AS_HTML)) != NULL)
340 {
341 if(export0->type == WXLO_EXPORT_HTML)
342 cout << *(export0->content.text);
343 else
344 ; // ignore itcout << "<!--UNKNOWN OBJECT>";
345
346 delete export0;
347 }
348 break;
349 }
350
351 case ID_TEXT:
352 {
353 wxLayoutExportObject *export0;
354 wxLayoutExportStatus status(m_lwin->GetLayoutList());
355
356 while((export0 = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
357 {
358 if(export0->type == WXLO_EXPORT_TEXT)
359 cout << *(export0->content.text);
360 else
361 cout << "<!--UNKNOWN OBJECT>";
362
363 delete export0;
364 }
365
366 break;
367 }
368
369
370 case ID_LONG_TEST:
371 {
372 wxString line;
373 wxLayoutList *llist = m_lwin->GetLayoutList();
374 for(int i = 1; i < 300; i++)
375 {
376 line.Printf(wxT("This is line number %d."), i);
377 llist->Insert(line);
378 llist->LineBreak();
379 }
380
381 llist->MoveCursorTo(wxPoint(0,0));
382 m_lwin->SetDirty();
383 m_lwin->Refresh();
384 break;
385 }
386
387 case ID_LINEBREAKS_TEST:
388 wxLayoutImportText
389 (
390 m_lwin->GetLayoutList(),
391 wxT("This is a text\nwith embedded line\nbreaks.\n")
392 );
393
394 m_lwin->SetDirty();
395 m_lwin->Refresh();
396 break;
397
398 case ID_URL_TEST:
399 // VZ: this doesn't work, of course, but I think it should -
400 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
401 // (instead of doing it manually like M does now)
402 m_lwin->GetLayoutList()->Insert(_T("http://www.wxwindows.org/"));
403 }
404 };
405
406 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
407 {
408 #ifdef __WXMSW__
409 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
410 #else
411 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
412 #endif
413 wxPrinter printer;
414 wxLayoutPrintout printout( m_lwin->GetLayoutList(), _T("Printout from wxLayout"));
415 if (! printer.Print(this, &printout, TRUE))
416 wxMessageBox(
417 _T("There was a problem printing.\nPerhaps your current printer is not set correctly?"),
418 _T("Printing"), wxOK);
419 }
420
421 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
422 {
423 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
424
425 #ifdef OS_UNIX
426 wxPostScriptPrinter printer;
427 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
428 printer.Print(this, &printout, TRUE);
429 #endif
430 }
431
432 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
433 {
434 #ifdef __WXMSW__
435 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
436 #else
437 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
438 #endif
439 wxPrintData printData;
440
441 // Pass two printout objects: for preview, and possible printing.
442 wxPrintPreview *preview = new wxPrintPreview(new
443 wxLayoutPrintout(
444 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
445 if (!preview->Ok())
446 {
447 delete preview;
448 wxMessageBox(_T("There was a problem previewing.\nPerhaps your current printer is not set correctly?"), _T("Previewing"), wxOK);
449 return;
450 }
451
452 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
453 frame->Centre(wxBOTH);
454 frame->Initialize();
455 frame->Show(TRUE);
456 }
457
458 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
459 {
460 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
461
462 wxPrintData printData;
463
464 // Pass two printout objects: for preview, and possible printing.
465 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
466 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, _T("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
467 frame->Centre(wxBOTH);
468 frame->Initialize();
469 frame->Show(TRUE);
470 }
471
472 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
473 {
474 #ifdef OS_WIN
475 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
476 #else
477 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
478 #endif
479 wxPrintDialog printerDialog(this, & m_PrintData);
480 printerDialog.ShowModal();
481 }
482
483 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
484 {
485 #ifdef __WXMSW__
486 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
487 #else
488 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
489 #endif
490 wxPageSetupData data;
491
492 #if defined(__WXMSW__) || defined(__WXMAC__)
493 wxPageSetupDialog pageSetupDialog(this, & data);
494 #else
495 wxGenericPageSetupDialog pageSetupDialog(this, & data);
496 #endif
497 pageSetupDialog.ShowModal();
498
499 data = pageSetupDialog.GetPageSetupData();
500 }
501
502 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
503 {
504 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
505
506 wxPrintData data;
507
508 #if defined(__WXMSW__) || defined(__WXMAC__)
509 wxPrintDialog printerDialog(this, & data);
510 #else
511 wxGenericPrintDialog printerDialog(this, & data);
512 #endif
513 printerDialog.ShowModal();
514 }
515
516 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
517 {
518 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
519
520 wxPageSetupData data;
521 #if defined(__WXMSW__) || defined(__WXMAC__)
522 wxPageSetupDialog pageSetupDialog(this, & data);
523 #else
524 wxGenericPageSetupDialog pageSetupDialog(this, & data);
525 #endif
526 pageSetupDialog.ShowModal();
527 }
528
529
530 //-----------------------------------------------------------------------------
531 // MyApp
532 //-----------------------------------------------------------------------------
533
534 MyApp::MyApp(void) :
535 wxApp( )
536 {
537 };
538
539 bool MyApp::OnInit(void)
540 {
541 wxFrame *frame = new MyFrame();
542 wxInitAllImageHandlers();
543 frame->Show( TRUE );
544 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
545 return TRUE;
546 };
547
548
549
550
551