]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxLayout.cpp
Changed a few #include <xxx.h> to #include "xxx.h"
[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
127 // JACS: under MSW, the window doesn't show the caret initially,
128 // and the following line I added doesn't help either.
129 // going to another window and then back again fixes it.
130 // m_lwin->OnSetFocus(wxFocusEvent());
131
132 Clear();
133
134 #if 0
135 // create and set the background bitmap (this will result in a lattice)
136 static const int sizeBmp = 10;
137 wxBitmap *bitmap = new wxBitmap(sizeBmp, sizeBmp);
138 wxMemoryDC dcMem;
139 dcMem.SelectObject( *bitmap );
140 dcMem.SetBackground( *wxWHITE_BRUSH );
141 dcMem.Clear();
142
143 dcMem.SetPen( *wxGREEN_PEN );
144 dcMem.DrawLine(sizeBmp/2, 0, sizeBmp/2, sizeBmp);
145 dcMem.DrawLine(0, sizeBmp/2, sizeBmp, sizeBmp/2);
146
147 dcMem.SelectObject( wxNullBitmap );
148
149 m_lwin->SetBackgroundBitmap(bitmap);
150 #endif // 0
151 };
152
153 void
154 MyFrame::AddSampleText(wxLayoutList *llist)
155 {
156 llist->Clear(wxSWISS,16,wxNORMAL,wxNORMAL, false);
157 llist->SetFont(-1,-1,-1,-1,-1,"blue");
158 llist->Insert("blue");
159 llist->SetFont(-1,-1,-1,-1,-1,"black");
160 llist->Insert("The quick brown fox jumps over the lazy dog.");
161 llist->LineBreak();
162
163 llist->SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
164 llist->Insert("--");
165 llist->LineBreak();
166
167 llist->SetFont(wxROMAN);
168 llist->Insert("The quick brown fox jumps over the lazy dog.");
169 llist->LineBreak();
170 llist->Insert("Hello ");
171 llist->Insert(new wxLayoutObjectIcon(new wxICON(Micon)));
172 llist->SetFontWeight(wxBOLD);
173 llist->Insert("World! ");
174 llist->SetFontWeight(wxNORMAL);
175 llist->Insert("The quick brown fox jumps...");
176 llist->LineBreak();
177 llist->Insert("over the lazy dog.");
178 llist->SetFont(-1,-1,-1,-1,true);
179 llist->Insert("underlined");
180 llist->SetFont(-1,-1,-1,-1,false);
181 llist->SetFont(wxROMAN);
182 llist->Insert("This is ");
183 llist->SetFont(-1,-1,-1,wxBOLD); llist->Insert("BOLD "); llist->SetFont(-1,-1,-1,wxNORMAL);
184 llist->Insert("and ");
185 llist->SetFont(-1,-1,wxITALIC);
186 llist->Insert("italics ");
187 llist->SetFont(-1,-1,wxNORMAL);
188 llist->LineBreak();
189 llist->Insert("and ");
190 llist->SetFont(-1,-1,wxSLANT);
191 llist->Insert("slanted");
192 llist->SetFont(-1,-1,wxNORMAL);
193 llist->Insert(" text.");
194 llist->LineBreak();
195 llist->Insert("and ");
196 llist->SetFont(-1,-1,-1,-1,-1,"blue");
197 llist->Insert("blue");
198 llist->SetFont(-1,-1,-1,-1,-1,"black");
199 llist->Insert(" and ");
200 llist->SetFont(-1,-1,-1,-1,-1,"green","black");
201 llist->Insert("green on black");
202 llist->SetFont(-1,-1,-1,-1,-1,"black","white");
203 llist->Insert(" text.");
204 llist->LineBreak();
205
206 llist->SetFont(-1,-1,wxSLANT);
207 llist->Insert("Slanted");
208 llist->SetFont(-1,-1,wxNORMAL);
209 llist->Insert(" and normal text and ");
210 llist->SetFont(-1,-1,wxSLANT);
211 llist->Insert("slanted");
212 llist->SetFont(-1,-1,wxNORMAL);
213 llist->Insert(" again.");
214 llist->LineBreak();
215
216 // add some more text for testing:
217 llist->Insert("And here the source for the test program:");
218 llist->LineBreak();
219 llist->SetFont(wxTELETYPE,16);
220 llist->Insert("And here the source for the test program:");
221 llist->LineBreak();
222 llist->Insert("And here the source for the test program:");
223 llist->LineBreak();
224 llist->Insert("And here the source for the test program:");
225
226 char buffer[1024];
227 FILE *in = fopen("wxLayout.cpp","r");
228 if(in)
229 {
230 for(;;)
231 {
232 fgets(buffer,1024,in);
233 if(feof(in))
234 break;
235 wxLayoutImportText(llist, buffer);
236 }
237 }
238
239 llist->MoveCursorTo(wxPoint(0,0));
240 m_lwin->SetDirty();
241 m_lwin->Refresh();
242 }
243
244 void
245 MyFrame::Clear(void)
246 {
247 wxColour colBg(0, 0, 0);
248
249 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false, wxRED, &colBg);
250 }
251
252
253 void MyFrame::OnCommand( wxCommandEvent &event )
254 {
255 switch (event.GetId())
256 {
257 case ID_QUIT:
258 Close( TRUE );
259 break;
260 case ID_PRINT:
261 {
262 wxPrinter printer;
263 wxLayoutPrintout printout(m_lwin->GetLayoutList(),_("M: Printout"));
264 if (! printer.Print(this, &printout, TRUE))
265 wxMessageBox(
266 _("There was a problem with printing the message:\n"
267 "perhaps your current printer is not set up correctly?"),
268 _("Printing"), wxOK);
269 }
270 break;
271 case ID_NOWRAP:
272 case ID_WRAP:
273 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
274 break;
275 case ID_ADD_SAMPLE:
276 AddSampleText(m_lwin->GetLayoutList());
277 break;
278 case ID_CLEAR:
279 Clear();
280 break;
281 case ID_CLICK:
282 cerr << "Received click event." << endl;
283 break;
284 case ID_PASTE:
285 m_lwin->Paste();
286 m_lwin->Refresh(FALSE);
287 break;
288 #ifdef __WXGTK__
289 case ID_PASTE_PRIMARY:
290 m_lwin->Paste(TRUE);
291 m_lwin->Refresh(FALSE);
292 break;
293 #endif
294 case ID_COPY:
295 m_lwin->Copy();
296 m_lwin->Refresh(FALSE);
297 break;
298 case ID_CUT:
299 m_lwin->Cut();
300 m_lwin->Refresh(FALSE);
301 break;
302 case ID_FIND:
303 m_lwin->Find("void");
304 m_lwin->Refresh(FALSE);
305 break;
306 case ID_HTML:
307 {
308 wxLayoutExportObject *export;
309 wxLayoutExportStatus status(m_lwin->GetLayoutList());
310
311 while((export = wxLayoutExport( &status,
312 WXLO_EXPORT_AS_HTML)) != NULL)
313 {
314 if(export->type == WXLO_EXPORT_HTML)
315 cout << *(export->content.text);
316 else
317 cout << "<!--UNKNOWN OBJECT>";
318 delete export;
319 }
320 }
321 break;
322 case ID_TEXT:
323 {
324 wxLayoutExportObject *export;
325 wxLayoutExportStatus status(m_lwin->GetLayoutList());
326
327 while((export = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
328 {
329 if(export->type == WXLO_EXPORT_TEXT)
330 cout << *(export->content.text);
331 else
332 cout << "<!--UNKNOWN OBJECT>";
333 delete export;
334 }
335 }
336 break;
337 case ID_LONG_TEST:
338 {
339 wxString line;
340 wxLayoutList *llist = m_lwin->GetLayoutList();
341 for(int i = 1; i < 300; i++)
342 {
343 line.Printf("This is line number %d.", i);
344 llist->Insert(line);
345 llist->LineBreak();
346 }
347 llist->MoveCursorTo(wxPoint(0,0));
348 m_lwin->SetDirty();
349 m_lwin->Refresh();
350 break;
351 }
352
353 case ID_LINEBREAKS_TEST:
354 wxLayoutImportText(m_lwin->GetLayoutList(),
355 "This is a text\n"
356 "with embedded line\n"
357 "breaks.\n");
358 m_lwin->SetDirty();
359 m_lwin->Refresh();
360 break;
361
362 case ID_URL_TEST:
363 // VZ: this doesn't work, of course, but I think it should -
364 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
365 // (instead of doing it manually like M does now)
366 m_lwin->GetLayoutList()->Insert("http://www.wxwindows.org/");
367 }
368 };
369
370 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
371 {
372 #ifdef __WXMSW__
373 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
374 #else
375 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
376 #endif
377 wxPrinter printer;
378 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"Printout from wxLayout");
379 if (! printer.Print(this, &printout, TRUE))
380 wxMessageBox(
381 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
382 "Printing", wxOK);
383 }
384
385 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
386 {
387 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
388
389 #ifdef OS_UNIX
390 wxPostScriptPrinter printer;
391 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
392 printer.Print(this, &printout, TRUE);
393 #endif
394 }
395
396 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
397 {
398 #ifdef __WXMSW__
399 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
400 #else
401 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
402 #endif
403 wxPrintData printData;
404
405 // Pass two printout objects: for preview, and possible printing.
406 wxPrintPreview *preview = new wxPrintPreview(new
407 wxLayoutPrintout(
408 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
409 if (!preview->Ok())
410 {
411 delete preview;
412 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
413 return;
414 }
415
416 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
417 frame->Centre(wxBOTH);
418 frame->Initialize();
419 frame->Show(TRUE);
420 }
421
422 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
423 {
424 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
425
426 wxPrintData printData;
427
428 // Pass two printout objects: for preview, and possible printing.
429 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
430 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
431 frame->Centre(wxBOTH);
432 frame->Initialize();
433 frame->Show(TRUE);
434 }
435
436 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
437 {
438 #ifdef OS_WIN
439 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
440 #else
441 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
442 #endif
443 wxPrintDialog printerDialog(this, & m_PrintData);
444 printerDialog.ShowModal();
445 }
446
447 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
448 {
449 #ifdef __WXMSW__
450 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
451 #else
452 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
453 #endif
454 wxPageSetupData data;
455
456 #ifdef __WXMSW__
457 wxPageSetupDialog pageSetupDialog(this, & data);
458 #else
459 wxGenericPageSetupDialog pageSetupDialog(this, & data);
460 #endif
461 pageSetupDialog.ShowModal();
462
463 data = pageSetupDialog.GetPageSetupData();
464 }
465
466 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
467 {
468 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
469
470 wxPrintData data;
471
472 #ifdef __WXMSW__
473 wxPrintDialog printerDialog(this, & data);
474 #else
475 wxGenericPrintDialog printerDialog(this, & data);
476 #endif
477 printerDialog.ShowModal();
478 }
479
480 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
481 {
482 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
483
484 wxPageSetupData data;
485 #ifdef __WXMSW__
486 wxPageSetupDialog pageSetupDialog(this, & data);
487 #else
488 wxGenericPageSetupDialog pageSetupDialog(this, & data);
489 #endif
490 pageSetupDialog.ShowModal();
491 }
492
493
494 //-----------------------------------------------------------------------------
495 // MyApp
496 //-----------------------------------------------------------------------------
497
498 MyApp::MyApp(void) :
499 wxApp( )
500 {
501 };
502
503 bool MyApp::OnInit(void)
504 {
505 wxFrame *frame = new MyFrame();
506 frame->Show( TRUE );
507 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
508 return TRUE;
509 };
510
511
512
513
514