]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxLayout.cpp
SetPath() is now implemented in dirdlg.cpp
[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 {
277 // Had to remove the split up strings that used to be below, and
278 // put them into one long strong. Otherwise MSVC would give an
279 // error "C2308: concatenating mismatched wide strings" when
280 // building a Unicode version.
281 wxMessageBox
282 (
283 _("There was a problem with printing the message:\nperhaps your current printer is not set up correctly?"),
284 _("Printing"), wxOK
285 );
286 }
287 break;
288 }
289
290 case ID_NOWRAP:
291 case ID_WRAP:
292 m_lwin->SetWrapMargin(event.GetId() == ID_NOWRAP ? 0 : 40);
293 break;
294 case ID_ADD_SAMPLE:
295 AddSampleText(m_lwin->GetLayoutList());
296 break;
297 case ID_CLEAR:
298 Clear();
299 break;
300 case ID_CLICK:
301 cerr << "Received click event." << endl;
302 break;
303 case ID_PASTE:
304 m_lwin->Paste(TRUE);
305 m_lwin->Refresh(FALSE);
306 break;
307 #ifdef __WXGTK__
308 case ID_PASTE_PRIMARY:
309 // text only from primary:
310 m_lwin->Paste(FALSE, TRUE);
311 m_lwin->Refresh(FALSE);
312 break;
313 case ID_COPY_PRIMARY:
314 // copy text-only to primary selection:
315 m_lwin->Copy(FALSE,FALSE,TRUE);
316 m_lwin->Refresh(FALSE);
317 break;
318 #endif
319 case ID_COPY:
320 m_lwin->Copy(TRUE,TRUE,FALSE);
321 m_lwin->Refresh(FALSE);
322 break;
323 case ID_CUT:
324 m_lwin->Cut();
325 m_lwin->Refresh(FALSE);
326 break;
327 #ifdef M_BASEDIR
328 case ID_FIND:
329 m_lwin->Find("void");
330 m_lwin->Refresh(FALSE);
331 break;
332 #endif
333 case ID_HTML:
334 {
335 wxLayoutExportObject *export0;
336 wxLayoutExportStatus status(m_lwin->GetLayoutList());
337
338 cout << "<HTML>" << endl;
339 while((export0 = wxLayoutExport( &status,
340 WXLO_EXPORT_AS_HTML)) != NULL)
341 {
342 if(export0->type == WXLO_EXPORT_HTML)
343 cout << *(export0->content.text);
344 else
345 ; // ignore itcout << "<!--UNKNOWN OBJECT>";
346
347 delete export0;
348 }
349 break;
350 }
351
352 case ID_TEXT:
353 {
354 wxLayoutExportObject *export0;
355 wxLayoutExportStatus status(m_lwin->GetLayoutList());
356
357 while((export0 = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
358 {
359 if(export0->type == WXLO_EXPORT_TEXT)
360 cout << *(export0->content.text);
361 else
362 cout << "<!--UNKNOWN OBJECT>";
363
364 delete export0;
365 }
366
367 break;
368 }
369
370
371 case ID_LONG_TEST:
372 {
373 wxString line;
374 wxLayoutList *llist = m_lwin->GetLayoutList();
375 for(int i = 1; i < 300; i++)
376 {
377 line.Printf(wxT("This is line number %d."), i);
378 llist->Insert(line);
379 llist->LineBreak();
380 }
381
382 llist->MoveCursorTo(wxPoint(0,0));
383 m_lwin->SetDirty();
384 m_lwin->Refresh();
385 break;
386 }
387
388 case ID_LINEBREAKS_TEST:
389 wxLayoutImportText
390 (
391 m_lwin->GetLayoutList(),
392 wxT("This is a text\nwith embedded line\nbreaks.\n")
393 );
394
395 m_lwin->SetDirty();
396 m_lwin->Refresh();
397 break;
398
399 case ID_URL_TEST:
400 // VZ: this doesn't work, of course, but I think it should -
401 // wxLayoutWindow should have a flag m_highlightUrls and do it itself
402 // (instead of doing it manually like M does now)
403 m_lwin->GetLayoutList()->Insert("http://www.wxwindows.org/");
404 }
405 };
406
407 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
408 {
409 #ifdef __WXMSW__
410 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
411 #else
412 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
413 #endif
414 wxPrinter printer;
415 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"Printout from wxLayout");
416 if (! printer.Print(this, &printout, TRUE))
417 wxMessageBox(
418 "There was a problem printing.\nPerhaps your current printer is not set correctly?",
419 "Printing", wxOK);
420 }
421
422 void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
423 {
424 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
425
426 #ifdef OS_UNIX
427 wxPostScriptPrinter printer;
428 wxLayoutPrintout printout( m_lwin->GetLayoutList(),"My printout");
429 printer.Print(this, &printout, TRUE);
430 #endif
431 }
432
433 void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
434 {
435 #ifdef __WXMSW__
436 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
437 #else
438 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
439 #endif
440 wxPrintData printData;
441
442 // Pass two printout objects: for preview, and possible printing.
443 wxPrintPreview *preview = new wxPrintPreview(new
444 wxLayoutPrintout(
445 m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
446 if (!preview->Ok())
447 {
448 delete preview;
449 wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK);
450 return;
451 }
452
453 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
454 frame->Centre(wxBOTH);
455 frame->Initialize();
456 frame->Show(TRUE);
457 }
458
459 void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
460 {
461 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
462
463 wxPrintData printData;
464
465 // Pass two printout objects: for preview, and possible printing.
466 wxPrintPreview *preview = new wxPrintPreview(new wxLayoutPrintout( m_lwin->GetLayoutList()), new wxLayoutPrintout( m_lwin->GetLayoutList()), & printData);
467 wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650));
468 frame->Centre(wxBOTH);
469 frame->Initialize();
470 frame->Show(TRUE);
471 }
472
473 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
474 {
475 #ifdef OS_WIN
476 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
477 #else
478 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
479 #endif
480 wxPrintDialog printerDialog(this, & m_PrintData);
481 printerDialog.ShowModal();
482 }
483
484 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
485 {
486 #ifdef __WXMSW__
487 wxGetApp().SetPrintMode(wxPRINT_WINDOWS);
488 #else
489 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
490 #endif
491 wxPageSetupData data;
492
493 #if defined(__WXMSW__) || defined(__WXMAC__)
494 wxPageSetupDialog pageSetupDialog(this, & data);
495 #else
496 wxGenericPageSetupDialog pageSetupDialog(this, & data);
497 #endif
498 pageSetupDialog.ShowModal();
499
500 data = pageSetupDialog.GetPageSetupData();
501 }
502
503 void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event))
504 {
505 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
506
507 wxPrintData data;
508
509 #if defined(__WXMSW__) || defined(__WXMAC__)
510 wxPrintDialog printerDialog(this, & data);
511 #else
512 wxGenericPrintDialog printerDialog(this, & data);
513 #endif
514 printerDialog.ShowModal();
515 }
516
517 void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
518 {
519 wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT);
520
521 wxPageSetupData data;
522 #if defined(__WXMSW__) || defined(__WXMAC__)
523 wxPageSetupDialog pageSetupDialog(this, & data);
524 #else
525 wxGenericPageSetupDialog pageSetupDialog(this, & data);
526 #endif
527 pageSetupDialog.ShowModal();
528 }
529
530
531 //-----------------------------------------------------------------------------
532 // MyApp
533 //-----------------------------------------------------------------------------
534
535 MyApp::MyApp(void) :
536 wxApp( )
537 {
538 };
539
540 bool MyApp::OnInit(void)
541 {
542 wxFrame *frame = new MyFrame();
543 wxInitAllImageHandlers();
544 frame->Show( TRUE );
545 // wxSetAFMPath("/usr/local/src/wxWindows/misc/afm/");
546 return TRUE;
547 };
548
549
550
551
552