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