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