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