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