]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxLayout.cpp
89fe57e094628b82dc029ea6bda16997a25d86c5
[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, "wxLayout",
74 wxPoint(880,100), wxSize(256,256) )
75 {
76 CreateStatusBar( 2 );
77
78 SetStatusText( "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, "&Print...", "Print");
84 file_menu->Append(ID_PRINT_SETUP, "Print &Setup...","Setup printer properties");
85 file_menu->Append(ID_PAGE_SETUP, "Page Set&up...", "Page setup");
86 file_menu->Append(ID_PREVIEW, "Print Pre&view", "Preview");
87 #ifdef __WXMSW__
88 file_menu->AppendSeparator();
89 file_menu->Append(ID_PRINT_PS, "Print PostScript...", "Print (PostScript)");
90 file_menu->Append(ID_PRINT_SETUP_PS, "Print Setup PostScript...", "Setup printer properties (PostScript)");
91 file_menu->Append(ID_PAGE_SETUP_PS, "Page Setup PostScript...", "Page setup (PostScript)");
92 file_menu->Append(ID_PREVIEW_PS, "Print Preview PostScript", "Preview (PostScript)");
93 #endif
94 file_menu->AppendSeparator();
95 file_menu->Append( ID_TEXT, "Export &Text");
96 file_menu->Append( ID_HTML, "Export &HTML");
97 file_menu->Append( ID_QUIT, "E&xit");
98 menu_bar->Append(file_menu, "&File" );
99
100 wxMenu *edit_menu = new wxMenu;
101 edit_menu->Append( ID_CLEAR, "C&lear");
102 edit_menu->Append( ID_ADD_SAMPLE, "&Example");
103 edit_menu->Append( ID_LONG_TEST, "Add &many lines");
104 edit_menu->AppendSeparator();
105 edit_menu->Append( ID_LINEBREAKS_TEST, "Add &several lines");
106 edit_menu->Append( ID_URL_TEST, "Insert an &URL");
107 edit_menu->AppendSeparator();
108 edit_menu->Append(ID_WRAP, "&Wrap mode", "Activate wrapping at pixel 200.");
109 edit_menu->Append(ID_NOWRAP, "&No-wrap mode", "Deactivate wrapping.");
110 edit_menu->AppendSeparator();
111 edit_menu->Append(ID_COPY, "&Copy", "Copy text to clipboard.");
112 edit_menu->Append(ID_CUT, "Cu&t", "Cut text to clipboard.");
113 edit_menu->Append(ID_PASTE,"&Paste", "Paste text from clipboard.");
114 #ifdef __WXGTK__
115 edit_menu->Append(ID_COPY_PRIMARY, "C&opy primary", "Copy text to primary selecton.");
116 edit_menu->Append(ID_PASTE_PRIMARY,"&Paste primary", "Paste text from primary selection.");
117 #endif
118 edit_menu->Append(ID_FIND, "&Find", "Find text.");
119 menu_bar->Append(edit_menu, "&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,"blue");
165 llist->Insert("blue");
166 llist->SetFont(-1,-1,-1,-1,-1,"black");
167 llist->Insert("The quick brown fox jumps over the lazy dog.");
168 llist->LineBreak();
169
170 llist->SetFont(wxROMAN,16,wxNORMAL,wxNORMAL, false);
171 llist->Insert("--");
172 llist->LineBreak();
173
174 llist->SetFont(wxROMAN);
175 llist->Insert("The quick brown fox jumps over the lazy dog.");
176 llist->LineBreak();
177 llist->Insert("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("World! ");
185 llist->SetFontWeight(wxNORMAL);
186 llist->Insert("The quick brown fox jumps...");
187 llist->LineBreak();
188 llist->Insert("over the lazy dog.");
189 llist->SetFont(-1,-1,-1,-1,true);
190 llist->Insert("underlined");
191 llist->SetFont(-1,-1,-1,-1,false);
192 llist->SetFont(wxROMAN);
193 llist->Insert("This is ");
194 llist->SetFont(-1,-1,-1,wxBOLD); llist->Insert("BOLD "); llist->SetFont(-1,-1,-1,wxNORMAL);
195 llist->Insert("and ");
196 llist->SetFont(-1,-1,wxITALIC);
197 llist->Insert("italics ");
198 llist->SetFont(-1,-1,wxNORMAL);
199 llist->LineBreak();
200 llist->Insert("and ");
201 llist->SetFont(-1,-1,wxSLANT);
202 llist->Insert("slanted");
203 llist->SetFont(-1,-1,wxNORMAL);
204 llist->Insert(" text.");
205 llist->LineBreak();
206 llist->Insert("and ");
207 llist->SetFont(-1,-1,-1,-1,-1,"blue");
208 llist->Insert("blue");
209 llist->SetFont(-1,-1,-1,-1,-1,"black");
210 llist->Insert(" and ");
211 llist->SetFont(-1,-1,-1,-1,-1,"green","black");
212 llist->Insert("green on black");
213 llist->SetFont(-1,-1,-1,-1,-1,"black","white");
214 llist->Insert(" text.");
215 llist->LineBreak();
216
217 llist->SetFont(-1,-1,wxSLANT);
218 llist->Insert("Slanted");
219 llist->SetFont(-1,-1,wxNORMAL);
220 llist->Insert(" and normal text and ");
221 llist->SetFont(-1,-1,wxSLANT);
222 llist->Insert("slanted");
223 llist->SetFont(-1,-1,wxNORMAL);
224 llist->Insert(" again.");
225 llist->LineBreak();
226
227 // add some more text for testing:
228 llist->Insert("And here the source for the test program:");
229 llist->LineBreak();
230 llist->SetFont(wxTELETYPE,16);
231 llist->Insert("And here the source for the test program:");
232 llist->LineBreak();
233 llist->Insert("And here the source for the test program:");
234 llist->LineBreak();
235 llist->Insert("And here the source for the test program:");
236
237 char buffer[1024];
238 FILE *in = fopen("wxLayout.cpp","r");
239 if(in)
240 {
241 for(;;)
242 {
243 fgets(buffer,1024,in);
244 if(feof(in))
245 break;
246 wxLayoutImportText(llist, buffer);
247 }
248 }
249
250 llist->MoveCursorTo(wxPoint(0,0));
251 m_lwin->SetDirty();
252 m_lwin->Refresh();
253 }
254
255 void
256 MyFrame::Clear(void)
257 {
258 wxColour colBg(0, 0, 0);
259
260 m_lwin->Clear(wxROMAN,16,wxNORMAL,wxNORMAL, false, wxRED, &colBg);
261 }
262
263
264 void MyFrame::OnCommand( wxCommandEvent &event )
265 {
266 switch (event.GetId())
267 {
268 case ID_QUIT:
269 Close( TRUE );
270 break;
271 case ID_PRINT:
272 {
273 wxPrinter printer;
274 wxLayoutPrintout printout(m_lwin->GetLayoutList(),_("M: Printout"));
275 if (! printer.Print(this, &printout, TRUE))
276 wxMessageBox(
277 _("There was a problem with printing the message:\n"
278 "perhaps your current printer is not set up correctly?"),
279 _("Printing"), wxOK);
280 }
281 break;
282 case ID_NOWRAP:
283 case ID_WRAP:
284 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
285 break;
286 case ID_ADD_SAMPLE:
287 AddSampleText(m_lwin->GetLayoutList());
288 break;
289 case ID_CLEAR:
290 Clear();
291 break;
292 case ID_CLICK:
293 cerr << "Received click event." << endl;
294 break;
295 case ID_PASTE:
296 m_lwin->Paste(TRUE);
297 m_lwin->Refresh(FALSE);
298 break;
299 #ifdef __WXGTK__
300 case ID_PASTE_PRIMARY:
301 // text only from primary:
302 m_lwin->Paste(FALSE, TRUE);
303 m_lwin->Refresh(FALSE);
304 break;
305 case ID_COPY_PRIMARY:
306 // copy text-only to primary selection:
307 m_lwin->Copy(FALSE,FALSE,TRUE);
308 m_lwin->Refresh(FALSE);
309 break;
310 #endif
311 case ID_COPY:
312 m_lwin->Copy(TRUE,TRUE,FALSE);
313 m_lwin->Refresh(FALSE);
314 break;
315 case ID_CUT:
316 m_lwin->Cut();
317 m_lwin->Refresh(FALSE);
318 break;
319 #ifdef M_BASEDIR
320 case ID_FIND:
321 m_lwin->Find("void");
322 m_lwin->Refresh(FALSE);
323 break;
324 #endif
325 case ID_HTML:
326 {
327 wxLayoutExportObject *export0;
328 wxLayoutExportStatus status(m_lwin->GetLayoutList());
329
330 cout << "<HTML>" << endl;
331 while((export0 = wxLayoutExport( &status,
332 WXLO_EXPORT_AS_HTML)) != NULL)
333 {
334 if(export0->type == WXLO_EXPORT_HTML)
335 cout << *(export0->content.text);
336 else
337 ; // ignore itcout << "<!--UNKNOWN OBJECT>";
338 delete export0;
339 }
340 }
341 break;
342 case ID_TEXT:
343 {
344 wxLayoutExportObject *export0;
345 wxLayoutExportStatus status(m_lwin->GetLayoutList());
346
347 while((export0 = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
348 {
349 if(export0->type == WXLO_EXPORT_TEXT)
350 cout << *(export0->content.text);
351 else
352 cout << "<!--UNKNOWN OBJECT>";
353 delete export0;
354 }
355 }
356 break;
357 case ID_LONG_TEST:
358 {
359 wxString line;
360 wxLayoutList *llist = m_lwin->GetLayoutList();
361 for(int i = 1; i < 300; i++)
362 {
363 line.Printf("This is line number %d.", i);
364 llist->Insert(line);
365 llist->LineBreak();
366 }
367 llist->MoveCursorTo(wxPoint(0,0));
368 m_lwin->SetDirty();
369 m_lwin->Refresh();
370 break;
371 }
372
373 case ID_LINEBREAKS_TEST:
374 wxLayoutImportText(m_lwin->GetLayoutList(),
375 "This is a text\n"
376 "with embedded line\n"
377 "breaks.\n");
378 m_lwin->SetDirty();
379 m_lwin->Refresh();
380 break;
381
382 case ID_URL_TEST:
383 // VZ: this doesn't work, of course, but I think it should -
384 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
385 // (instead of doing it manually like M does now)
386 m_lwin->GetLayoutList()->Insert("http://www.wxwindows.org/");
387 }
388 };
389
390 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
391 {
392 #ifdef __WXMSW__
393 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
394 #else
395 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
396 #endif
397 wxPrinter printer;
398 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"Printout from wxLayout");
399 if (! printer.Print(this, &printout, TRUE))
400 wxMessageBox(
401 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
402 "Printing", wxOK);
403 }
404
405 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
406 {
407 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
408
409 #ifdef OS_UNIX
410 wxPostScriptPrinter printer;
411 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
412 printer.Print(this, &printout, TRUE);
413 #endif
414 }
415
416 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
417 {
418 #ifdef __WXMSW__
419 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
420 #else
421 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
422 #endif
423 wxPrintData printData;
424
425 // Pass two printout objects: for preview, and possible printing.
426 wxPrintPreview *preview = new wxPrintPreview(new
427 wxLayoutPrintout(
428 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
429 if (!preview->Ok())
430 {
431 delete preview;
432 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
433 return;
434 }
435
436 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
437 frame->Centre(wxBOTH);
438 frame->Initialize();
439 frame->Show(TRUE);
440 }
441
442 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
443 {
444 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
445
446 wxPrintData printData;
447
448 // Pass two printout objects: for preview, and possible printing.
449 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
450 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
451 frame->Centre(wxBOTH);
452 frame->Initialize();
453 frame->Show(TRUE);
454 }
455
456 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
457 {
458 #ifdef OS_WIN
459 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
460 #else
461 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
462 #endif
463 wxPrintDialog printerDialog(this, & m_PrintData);
464 printerDialog.ShowModal();
465 }
466
467 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
468 {
469 #ifdef __WXMSW__
470 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
471 #else
472 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
473 #endif
474 wxPageSetupData data;
475
476 #if defined(__WXMSW__) || defined(__WXMAC__)
477 wxPageSetupDialog pageSetupDialog(this, & data);
478 #else
479 wxGenericPageSetupDialog pageSetupDialog(this, & data);
480 #endif
481 pageSetupDialog.ShowModal();
482
483 data = pageSetupDialog.GetPageSetupData();
484 }
485
486 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
487 {
488 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
489
490 wxPrintData data;
491
492 #if defined(__WXMSW__) || defined(__WXMAC__)
493 wxPrintDialog printerDialog(this, & data);
494 #else
495 wxGenericPrintDialog printerDialog(this, & data);
496 #endif
497 printerDialog.ShowModal();
498 }
499
500 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
501 {
502 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
503
504 wxPageSetupData data;
505 #if defined(__WXMSW__) || defined(__WXMAC__)
506 wxPageSetupDialog pageSetupDialog(this, & data);
507 #else
508 wxGenericPageSetupDialog pageSetupDialog(this, & data);
509 #endif
510 pageSetupDialog.ShowModal();
511 }
512
513
514 //-----------------------------------------------------------------------------
515 // MyApp
516 //-----------------------------------------------------------------------------
517
518 MyApp::MyApp(void) :
519 wxApp( )
520 {
521 };
522
523 bool MyApp::OnInit(void)
524 {
525 wxFrame *frame = new MyFrame();
526 wxInitAllImageHandlers();
527 frame->Show( TRUE );
528 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
529 return TRUE;
530 };
531
532
533
534
535