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