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