Efficiency measures; file loading bug fix
[wxWidgets.git] / samples / richtext / richtext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richedit.cpp
3 // Purpose: wxWidgets rich text editor sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2005-10-02
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/fontdlg.h"
34 #include "wx/splitter.h"
35 #include "wx/sstream.h"
36 #include "wx/html/htmlwin.h"
37
38 #include "bitmaps/sample.xpm"
39 #include "bitmaps/smiley.xpm"
40 // #include "bitmaps/idea.xpm"
41 #include "bitmaps/zebra.xpm"
42
43 #include "bitmaps/open.xpm"
44 #include "bitmaps/save.xpm"
45 #include "bitmaps/copy.xpm"
46 #include "bitmaps/cut.xpm"
47 #include "bitmaps/paste.xpm"
48 #include "bitmaps/undo.xpm"
49 #include "bitmaps/redo.xpm"
50 #include "bitmaps/bold.xpm"
51 #include "bitmaps/italic.xpm"
52 #include "bitmaps/underline.xpm"
53
54 #include "bitmaps/alignleft.xpm"
55 #include "bitmaps/alignright.xpm"
56 #include "bitmaps/centre.xpm"
57 #include "bitmaps/font.xpm"
58 #include "bitmaps/indentless.xpm"
59 #include "bitmaps/indentmore.xpm"
60
61 #include "wx/richtext/richtextctrl.h"
62 #include "wx/richtext/richtextstyles.h"
63 #include "wx/richtext/richtextxml.h"
64
65 // ----------------------------------------------------------------------------
66 // resources
67 // ----------------------------------------------------------------------------
68
69 // ----------------------------------------------------------------------------
70 // private classes
71 // ----------------------------------------------------------------------------
72
73 // Define a new application type, each program should derive a class from wxApp
74 class MyApp : public wxApp
75 {
76 public:
77 // override base class virtuals
78 // ----------------------------
79
80 // this one is called on application startup and is a good place for the app
81 // initialization (doing it here and not in the ctor allows to have an error
82 // return: if OnInit() returns false, the application terminates)
83 virtual bool OnInit();
84 virtual int OnExit();
85
86 void CreateStyles();
87
88 wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
89
90 wxRichTextStyleSheet* m_styleSheet;
91 };
92
93 // Define a new frame type: this is going to be our main frame
94 class MyFrame : public wxFrame
95 {
96 public:
97 // ctor(s)
98 MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
99 const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE);
100
101 // event handlers (these functions should _not_ be virtual)
102 void OnQuit(wxCommandEvent& event);
103 void OnAbout(wxCommandEvent& event);
104
105 void OnOpen(wxCommandEvent& event);
106 void OnSave(wxCommandEvent& event);
107 void OnSaveAs(wxCommandEvent& event);
108
109 void OnBold(wxCommandEvent& event);
110 void OnItalic(wxCommandEvent& event);
111 void OnUnderline(wxCommandEvent& event);
112
113 void OnUpdateBold(wxUpdateUIEvent& event);
114 void OnUpdateItalic(wxUpdateUIEvent& event);
115 void OnUpdateUnderline(wxUpdateUIEvent& event);
116
117 void OnAlignLeft(wxCommandEvent& event);
118 void OnAlignCentre(wxCommandEvent& event);
119 void OnAlignRight(wxCommandEvent& event);
120
121 void OnUpdateAlignLeft(wxUpdateUIEvent& event);
122 void OnUpdateAlignCentre(wxUpdateUIEvent& event);
123 void OnUpdateAlignRight(wxUpdateUIEvent& event);
124
125 void OnFont(wxCommandEvent& event);
126 void OnIndentMore(wxCommandEvent& event);
127 void OnIndentLess(wxCommandEvent& event);
128
129 void OnLineSpacingHalf(wxCommandEvent& event);
130 void OnLineSpacingDouble(wxCommandEvent& event);
131 void OnLineSpacingSingle(wxCommandEvent& event);
132
133 void OnParagraphSpacingMore(wxCommandEvent& event);
134 void OnParagraphSpacingLess(wxCommandEvent& event);
135
136 void OnViewHTML(wxCommandEvent& event);
137
138 // Forward command events to the current rich text control, if any
139 bool ProcessEvent(wxEvent& event);
140
141 private:
142 // any class wishing to process wxWidgets events must use this macro
143 DECLARE_EVENT_TABLE()
144
145 wxRichTextCtrl* m_richTextCtrl;
146 };
147
148 // ----------------------------------------------------------------------------
149 // constants
150 // ----------------------------------------------------------------------------
151
152 // IDs for the controls and the menu commands
153 enum
154 {
155 // menu items
156 ID_Quit = wxID_EXIT,
157 ID_About = wxID_ABOUT,
158
159 ID_FORMAT_BOLD = 100,
160 ID_FORMAT_ITALIC,
161 ID_FORMAT_UNDERLINE,
162 ID_FORMAT_FONT,
163
164 ID_FORMAT_ALIGN_LEFT,
165 ID_FORMAT_ALIGN_CENTRE,
166 ID_FORMAT_ALIGN_RIGHT,
167
168 ID_FORMAT_INDENT_MORE,
169 ID_FORMAT_INDENT_LESS,
170
171 ID_FORMAT_PARAGRAPH_SPACING_MORE,
172 ID_FORMAT_PARAGRAPH_SPACING_LESS,
173
174 ID_FORMAT_LINE_SPACING_HALF,
175 ID_FORMAT_LINE_SPACING_DOUBLE,
176 ID_FORMAT_LINE_SPACING_SINGLE,
177
178 ID_VIEW_HTML
179 };
180
181 // ----------------------------------------------------------------------------
182 // event tables and other macros for wxWidgets
183 // ----------------------------------------------------------------------------
184
185 // the event tables connect the wxWidgets events with the functions (event
186 // handlers) which process them. It can be also done at run-time, but for the
187 // simple menu events like this the static method is much simpler.
188 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
189 EVT_MENU(ID_Quit, MyFrame::OnQuit)
190 EVT_MENU(ID_About, MyFrame::OnAbout)
191
192 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
193 EVT_MENU(wxID_SAVE, MyFrame::OnSave)
194 EVT_MENU(wxID_SAVEAS, MyFrame::OnSaveAs)
195
196 EVT_MENU(ID_FORMAT_BOLD, MyFrame::OnBold)
197 EVT_MENU(ID_FORMAT_ITALIC, MyFrame::OnItalic)
198 EVT_MENU(ID_FORMAT_UNDERLINE, MyFrame::OnUnderline)
199
200 EVT_UPDATE_UI(ID_FORMAT_BOLD, MyFrame::OnUpdateBold)
201 EVT_UPDATE_UI(ID_FORMAT_ITALIC, MyFrame::OnUpdateItalic)
202 EVT_UPDATE_UI(ID_FORMAT_UNDERLINE, MyFrame::OnUpdateUnderline)
203
204 EVT_MENU(ID_FORMAT_ALIGN_LEFT, MyFrame::OnAlignLeft)
205 EVT_MENU(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnAlignCentre)
206 EVT_MENU(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnAlignRight)
207
208 EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT, MyFrame::OnUpdateAlignLeft)
209 EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnUpdateAlignCentre)
210 EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnUpdateAlignRight)
211
212 EVT_MENU(ID_FORMAT_FONT, MyFrame::OnFont)
213 EVT_MENU(ID_FORMAT_INDENT_MORE, MyFrame::OnIndentMore)
214 EVT_MENU(ID_FORMAT_INDENT_LESS, MyFrame::OnIndentLess)
215
216 EVT_MENU(ID_FORMAT_LINE_SPACING_HALF, MyFrame::OnLineSpacingHalf)
217 EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE, MyFrame::OnLineSpacingSingle)
218 EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE, MyFrame::OnLineSpacingDouble)
219
220 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE, MyFrame::OnParagraphSpacingMore)
221 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS, MyFrame::OnParagraphSpacingLess)
222
223 EVT_MENU(ID_VIEW_HTML, MyFrame::OnViewHTML)
224 END_EVENT_TABLE()
225
226 // Create a new application object: this macro will allow wxWidgets to create
227 // the application object during program execution (it's better than using a
228 // static object for many reasons) and also implements the accessor function
229 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
230 // not wxApp)
231 IMPLEMENT_APP(MyApp)
232
233 // ============================================================================
234 // implementation
235 // ============================================================================
236
237 // ----------------------------------------------------------------------------
238 // the application class
239 // ----------------------------------------------------------------------------
240
241 // 'Main program' equivalent: the program execution "starts" here
242 bool MyApp::OnInit()
243 {
244 m_styleSheet = new wxRichTextStyleSheet;
245
246 CreateStyles();
247
248 // Add extra handlers (plain text is automatically added)
249 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler);
250 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler);
251
252 // Add image handlers
253 #if wxUSE_LIBPNG
254 wxImage::AddHandler( new wxPNGHandler );
255 #endif
256
257 #if wxUSE_LIBJPEG
258 wxImage::AddHandler( new wxJPEGHandler );
259 #endif
260
261 #if wxUSE_GIF
262 wxImage::AddHandler( new wxGIFHandler );
263 #endif
264
265 // create the main application window
266 MyFrame *frame = new MyFrame(_T("wxRichTextCtrl Sample"), wxID_ANY, wxDefaultPosition, wxSize(600, 500));
267
268 // and show it (the frames, unlike simple controls, are not shown when
269 // created initially)
270 frame->Show(true);
271
272 // success: wxApp::OnRun() will be called which will enter the main message
273 // loop and the application will run. If we returned false here, the
274 // application would exit immediately.
275 return true;
276 }
277
278 int MyApp::OnExit()
279 {
280 delete m_styleSheet;
281 return 0;
282 }
283
284 void MyApp::CreateStyles()
285 {
286 // Paragraph styles
287
288 wxFont romanFont(12, wxROMAN, wxNORMAL, wxNORMAL);
289 wxFont swissFont(12, wxSWISS, wxNORMAL, wxNORMAL);
290
291 wxRichTextParagraphStyleDefinition* normalPara = new wxRichTextParagraphStyleDefinition(wxT("Normal"));
292 wxRichTextAttr normalAttr;
293 normalAttr.SetFontFaceName(romanFont.GetFaceName());
294 normalAttr.SetFontSize(12);
295 // Let's set all attributes for this style
296 normalAttr.SetFlags(wxTEXT_ATTR_FONT | wxTEXT_ATTR_BACKGROUND_COLOUR | wxTEXT_ATTR_TEXT_COLOUR|wxTEXT_ATTR_ALIGNMENT|wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_TABS|
297 wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|
298 wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER);
299 normalPara->SetStyle(normalAttr);
300
301 m_styleSheet->AddParagraphStyle(normalPara);
302
303 wxRichTextParagraphStyleDefinition* indentedPara = new wxRichTextParagraphStyleDefinition(wxT("Indented"));
304 wxRichTextAttr indentedAttr;
305 indentedAttr.SetFontFaceName(romanFont.GetFaceName());
306 indentedAttr.SetFontSize(12);
307 indentedAttr.SetLeftIndent(100, 0);
308 // We only want to affect indentation
309 indentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
310 indentedPara->SetStyle(indentedAttr);
311
312 m_styleSheet->AddParagraphStyle(indentedPara);
313
314 wxRichTextParagraphStyleDefinition* flIndentedPara = new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
315 wxRichTextAttr flIndentedAttr;
316 flIndentedAttr.SetFontFaceName(swissFont.GetFaceName());
317 flIndentedAttr.SetFontSize(12);
318 flIndentedAttr.SetLeftIndent(100, -100);
319 // We only want to affect indentation
320 flIndentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
321 flIndentedPara->SetStyle(flIndentedAttr);
322
323 m_styleSheet->AddParagraphStyle(flIndentedPara);
324
325 // Character styles
326
327 wxRichTextCharacterStyleDefinition* boldDef = new wxRichTextCharacterStyleDefinition(wxT("Bold"));
328 wxRichTextAttr boldAttr;
329 boldAttr.SetFontFaceName(romanFont.GetFaceName());
330 boldAttr.SetFontSize(12);
331 boldAttr.SetFontWeight(wxBOLD);
332 // We only want to affect boldness
333 boldAttr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
334 boldDef->SetStyle(boldAttr);
335
336 m_styleSheet->AddCharacterStyle(boldDef);
337
338 wxRichTextCharacterStyleDefinition* italicDef = new wxRichTextCharacterStyleDefinition(wxT("Italic"));
339 wxRichTextAttr italicAttr;
340 italicAttr.SetFontFaceName(romanFont.GetFaceName());
341 italicAttr.SetFontSize(12);
342 italicAttr.SetFontStyle(wxITALIC);
343 // We only want to affect italics
344 italicAttr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
345 italicDef->SetStyle(italicAttr);
346
347 m_styleSheet->AddCharacterStyle(italicDef);
348
349 wxRichTextCharacterStyleDefinition* redDef = new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
350 wxRichTextAttr redAttr;
351 redAttr.SetFontFaceName(romanFont.GetFaceName());
352 redAttr.SetFontSize(12);
353 redAttr.SetFontWeight(wxBOLD);
354 redAttr.SetTextColour(*wxRED);
355 // We only want to affect colour, weight and face
356 redAttr.SetFlags(wxTEXT_ATTR_FONT_FACE|wxTEXT_ATTR_FONT_WEIGHT|wxTEXT_ATTR_TEXT_COLOUR);
357 redDef->SetStyle(redAttr);
358
359 m_styleSheet->AddCharacterStyle(redDef);
360 }
361
362 // ----------------------------------------------------------------------------
363 // main frame
364 // ----------------------------------------------------------------------------
365
366 // frame constructor
367 MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
368 const wxSize& size, long style)
369 : wxFrame(NULL, id, title, pos, size, style)
370 {
371 // set the frame icon
372 SetIcon(sample_xpm);
373
374 // create a menu bar
375 wxMenu *fileMenu = new wxMenu;
376
377 // the "About" item should be in the help menu
378 wxMenu *helpMenu = new wxMenu;
379 helpMenu->Append(ID_About, _T("&About...\tF1"), _T("Show about dialog"));
380
381 fileMenu->Append(wxID_OPEN, _T("&Open\tCtrl+O"), _T("Open a file"));
382 fileMenu->Append(wxID_SAVE, _T("&Save\tCtrl+S"), _T("Save a file"));
383 fileMenu->Append(wxID_SAVEAS, _T("&Save As...\tF12"), _T("Save to a new file"));
384 fileMenu->AppendSeparator();
385 fileMenu->Append(ID_VIEW_HTML, _T("&View as HTML"), _T("View HTML"));
386 fileMenu->AppendSeparator();
387 fileMenu->Append(ID_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
388
389 wxMenu* editMenu = new wxMenu;
390 editMenu->Append(wxID_UNDO, _("&Undo\tCtrl+Z"));
391 editMenu->Append(wxID_REDO, _("&Redo\tCtrl+Y"));
392 editMenu->AppendSeparator();
393 editMenu->Append(wxID_CUT, _("Cu&t\tCtrl+X"));
394 editMenu->Append(wxID_COPY, _("&Copy\tCtrl+C"));
395 editMenu->Append(wxID_PASTE, _("&Paste\tCtrl+V"));
396
397 editMenu->Append(wxID_CLEAR, _("&Delete\tDel"));
398
399 editMenu->AppendSeparator();
400 editMenu->Append(wxID_SELECTALL, _("Select A&ll\tCtrl+A"));
401 #if 0
402 editMenu->AppendSeparator();
403 editMenu->Append(wxID_FIND, _("&Find...\tCtrl+F"));
404 editMenu->Append(stID_FIND_REPLACE, _("&Replace...\tCtrl+R"));
405 #endif
406
407 wxMenu* formatMenu = new wxMenu;
408 formatMenu->AppendCheckItem(ID_FORMAT_BOLD, _("&Bold\tCtrl+B"));
409 formatMenu->AppendCheckItem(ID_FORMAT_ITALIC, _("&Italic\tCtrl+I"));
410 formatMenu->AppendCheckItem(ID_FORMAT_UNDERLINE, _("&Underline\tCtrl+U"));
411 formatMenu->AppendSeparator();
412 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_LEFT, _("L&eft Align"));
413 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT, _("&Right Align"));
414 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE, _("&Centre"));
415 formatMenu->AppendSeparator();
416 formatMenu->Append(ID_FORMAT_INDENT_MORE, _("I&ndent &More"));
417 formatMenu->Append(ID_FORMAT_INDENT_LESS, _("Indent &Less"));
418 formatMenu->AppendSeparator();
419 formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE, _("Increase Paragraph &Spacing"));
420 formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS, _("Decrease &Paragraph Spacing"));
421 formatMenu->AppendSeparator();
422 formatMenu->Append(ID_FORMAT_LINE_SPACING_SINGLE, _("Normal Line Spacing"));
423 formatMenu->Append(ID_FORMAT_LINE_SPACING_HALF, _("1.5 Line Spacing"));
424 formatMenu->Append(ID_FORMAT_LINE_SPACING_DOUBLE, _("Double Line Spacing"));
425 formatMenu->AppendSeparator();
426 formatMenu->Append(ID_FORMAT_FONT, _("&Font..."));
427
428 // now append the freshly created menu to the menu bar...
429 wxMenuBar *menuBar = new wxMenuBar();
430 menuBar->Append(fileMenu, _T("&File"));
431 menuBar->Append(editMenu, _T("&Edit"));
432 menuBar->Append(formatMenu, _T("F&ormat"));
433 menuBar->Append(helpMenu, _T("&Help"));
434
435 // ... and attach this menu bar to the frame
436 SetMenuBar(menuBar);
437
438 // create a status bar just for fun (by default with 1 pane only)
439 CreateStatusBar(2);
440 SetStatusText(_T("Welcome to wxRichTextCtrl!"));
441
442 wxToolBar* toolBar = CreateToolBar();
443
444 toolBar->AddTool(wxID_OPEN, wxBitmap(open_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Open"));
445 toolBar->AddTool(wxID_SAVEAS, wxBitmap(save_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Save"));
446 toolBar->AddSeparator();
447 toolBar->AddTool(wxID_CUT, wxBitmap(cut_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Cut"));
448 toolBar->AddTool(wxID_COPY, wxBitmap(copy_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Copy"));
449 toolBar->AddTool(wxID_PASTE, wxBitmap(paste_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Paste"));
450 toolBar->AddSeparator();
451 toolBar->AddTool(wxID_UNDO, wxBitmap(undo_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Undo"));
452 toolBar->AddTool(wxID_REDO, wxBitmap(redo_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Redo"));
453 toolBar->AddSeparator();
454 toolBar->AddTool(ID_FORMAT_BOLD, wxBitmap(bold_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Bold"));
455 toolBar->AddTool(ID_FORMAT_ITALIC, wxBitmap(italic_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Italic"));
456 toolBar->AddTool(ID_FORMAT_UNDERLINE, wxBitmap(underline_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Underline"));
457 toolBar->AddSeparator();
458 toolBar->AddTool(ID_FORMAT_ALIGN_LEFT, wxBitmap(alignleft_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Align Left"));
459 toolBar->AddTool(ID_FORMAT_ALIGN_CENTRE, wxBitmap(centre_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Centre"));
460 toolBar->AddTool(ID_FORMAT_ALIGN_RIGHT, wxBitmap(alignright_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Align Right"));
461 toolBar->AddSeparator();
462 toolBar->AddTool(ID_FORMAT_INDENT_LESS, wxBitmap(indentless_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Indent Less"));
463 toolBar->AddTool(ID_FORMAT_INDENT_MORE, wxBitmap(indentmore_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Indent More"));
464 toolBar->AddSeparator();
465 toolBar->AddTool(ID_FORMAT_FONT, wxBitmap(font_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Font"));
466
467 toolBar->Realize();
468
469 wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxSize(100, 100), wxSP_NO_XP_THEME|wxSP_3D|wxSP_LIVE_UPDATE);
470
471
472 wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
473 wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
474 wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL);
475
476 m_richTextCtrl = new wxRichTextCtrl(splitter, wxID_ANY, wxDefaultPosition, wxSize(200, 200), wxVSCROLL|wxHSCROLL|wxNO_BORDER);
477 m_richTextCtrl->SetFont(wxFont(12, wxROMAN, wxNORMAL, wxNORMAL));
478
479 wxRichTextStyleListBox* styleListBox = new wxRichTextStyleListBox(splitter, wxID_ANY);
480 splitter->SplitVertically(m_richTextCtrl, styleListBox, 400);
481
482 styleListBox->SetStyleSheet(wxGetApp().GetStyleSheet());
483 styleListBox->SetRichTextCtrl(m_richTextCtrl);
484 styleListBox->UpdateStyles();
485
486 wxRichTextCtrl& r = *m_richTextCtrl;
487
488 r.BeginSuppressUndo();
489
490 r.BeginParagraphSpacing(0, 20);
491
492 r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
493 r.BeginBold();
494
495 r.BeginFontSize(14);
496 r.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
497 r.EndFontSize();
498 r.Newline();
499
500 r.BeginItalic();
501 r.WriteText(wxT("by Julian Smart"));
502 r.EndItalic();
503
504 r.EndBold();
505
506 r.Newline();
507 r.WriteImage(wxBitmap(zebra_xpm));
508
509 r.EndAlignment();
510
511 r.Newline();
512 r.Newline();
513
514 r.WriteText(wxT("What can you do with this thing? "));
515 r.WriteImage(wxBitmap(smiley_xpm));
516 r.WriteText(wxT(" Well, you can change text "));
517
518 r.BeginTextColour(wxColour(255, 0, 0));
519 r.WriteText(wxT("colour, like this red bit."));
520 r.EndTextColour();
521
522 r.BeginTextColour(wxColour(0, 0, 255));
523 r.WriteText(wxT(" And this blue bit."));
524 r.EndTextColour();
525
526 r.WriteText(wxT(" Naturally you can make things "));
527 r.BeginBold();
528 r.WriteText(wxT("bold "));
529 r.EndBold();
530 r.BeginItalic();
531 r.WriteText(wxT("or italic "));
532 r.EndItalic();
533 r.BeginUnderline();
534 r.WriteText(wxT("or underlined."));
535 r.EndUnderline();
536
537 r.BeginFontSize(14);
538 r.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
539 r.EndFontSize();
540
541 r.WriteText(wxT(" Next we'll show an indented paragraph."));
542
543 r.BeginLeftIndent(60);
544 r.Newline();
545
546 r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable."));
547 r.EndLeftIndent();
548
549 r.Newline();
550
551 r.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
552
553 r.BeginLeftIndent(100, -40);
554 r.Newline();
555
556 r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable."));
557 r.EndLeftIndent();
558
559 r.Newline();
560
561 r.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
562
563 r.BeginNumberedBullet(1, 100, 60);
564 r.Newline();
565
566 r.WriteText(wxT("This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later."));
567 r.EndNumberedBullet();
568
569 r.BeginNumberedBullet(2, 100, 60);
570 r.Newline();
571
572 r.WriteText(wxT("This is my second item."));
573 r.EndNumberedBullet();
574
575 r.Newline();
576
577 r.WriteText(wxT("The following paragraph is right-indented:"));
578
579 r.BeginRightIndent(200);
580 r.Newline();
581
582 r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable."));
583 r.EndRightIndent();
584
585 r.Newline();
586
587 r.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
588
589 r.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT);
590 r.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF);
591 r.Newline();
592
593 r.WriteText(wxT("It was in January, the most down-trodden month of an Edinburgh winter. An attractive woman came into the cafe, which is nothing remarkable."));
594 r.EndLineSpacing();
595 r.EndAlignment();
596
597 r.Newline();
598 r.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
599
600 r.BeginSymbolBullet(wxT('*'), 100, 60);
601 r.Newline();
602 r.WriteText(wxT("Compatibility with wxTextCtrl API"));
603 r.EndSymbolBullet();
604
605 r.BeginSymbolBullet(wxT('*'), 100, 60);
606 r.Newline();
607 r.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
608 r.EndSymbolBullet();
609
610 r.BeginSymbolBullet(wxT('*'), 100, 60);
611 r.Newline();
612 r.WriteText(wxT("XML loading and saving"));
613 r.EndSymbolBullet();
614
615 r.BeginSymbolBullet(wxT('*'), 100, 60);
616 r.Newline();
617 r.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
618 r.EndSymbolBullet();
619
620 r.BeginSymbolBullet(wxT('*'), 100, 60);
621 r.Newline();
622 r.WriteText(wxT("Clipboard copy and paste"));
623 r.EndSymbolBullet();
624
625 r.BeginSymbolBullet(wxT('*'), 100, 60);
626 r.Newline();
627 r.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
628 r.EndSymbolBullet();
629
630 r.BeginSymbolBullet(wxT('*'), 100, 60);
631 r.Newline();
632 r.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
633 r.EndSymbolBullet();
634
635 r.Newline();
636
637 r.WriteText(wxT("Note: this sample content was generated programmatically from within the MyFrame constructor in the demo. The images were loaded from inline XPMs. Enjoy wxRichTextCtrl!"));
638
639 r.EndParagraphSpacing();
640
641 r.EndSuppressUndo();
642 }
643
644
645 // event handlers
646
647 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
648 {
649 // true is to force the frame to close
650 Close(true);
651 }
652
653 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
654 {
655 wxString msg;
656 msg.Printf( _T("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
657 wxMessageBox(msg, _T("About wxRichTextCtrl Sample"), wxOK | wxICON_INFORMATION, this);
658 }
659
660 // Forward command events to the current rich text control, if any
661 bool MyFrame::ProcessEvent(wxEvent& event)
662 {
663 if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent)))
664 {
665 // Problem: we can get infinite recursion because the events
666 // climb back up to this frame, and repeat.
667 // Assume that command events don't cause another command event
668 // to be called, so we can rely on inCommand not being overwritten
669
670 static int s_eventType = 0;
671 static wxWindowID s_id = 0;
672
673 if (s_id != event.GetId() && s_eventType != event.GetEventType())
674 {
675 s_eventType = event.GetEventType();
676 s_id = event.GetId();
677
678 wxWindow* focusWin = wxFindFocusDescendant(this);
679 if (focusWin && focusWin->ProcessEvent(event))
680 {
681 //s_command = NULL;
682 s_eventType = 0;
683 s_id = 0;
684 return TRUE;
685 }
686
687 s_eventType = 0;
688 s_id = 0;
689 }
690 else
691 {
692 return FALSE;
693 }
694 }
695
696 return wxFrame::ProcessEvent(event);
697 }
698
699 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
700 {
701 wxString path = wxEmptyString;
702 wxString filename = wxEmptyString;
703 wxArrayInt fileTypes;
704
705 wxString filter = wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes);
706 if (!filter.IsEmpty())
707 filter += wxT("|");
708 filter += wxT("All files (*.*)|*.*");
709
710 wxFileDialog dialog(this,
711 _("Choose a filename"),
712 path,
713 filename,
714 filter,
715 wxOPEN);
716
717 if (dialog.ShowModal() == wxID_OK)
718 {
719 wxString path = dialog.GetPath();
720
721 if (!path.IsEmpty())
722 {
723 int fileType = 0;
724 int filterIndex = dialog.GetFilterIndex();
725 if (filterIndex < (int) fileTypes.GetCount())
726 fileType = fileTypes[filterIndex];
727 else
728 fileType = wxRICHTEXT_TYPE_TEXT;
729 m_richTextCtrl->LoadFile(path, fileType);
730 }
731 }
732 }
733
734 void MyFrame::OnSave(wxCommandEvent& event)
735 {
736 if (m_richTextCtrl->GetFilename().IsEmpty())
737 {
738 OnSaveAs(event);
739 return;
740 }
741 m_richTextCtrl->SaveFile();
742 }
743
744 void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
745 {
746 wxString filter = wxRichTextBuffer::GetExtWildcard(false, true);
747 wxString path = wxEmptyString;
748 wxString filename = wxEmptyString;
749
750 wxFileDialog dialog(this,
751 _("Choose a filename"),
752 path,
753 filename,
754 filter,
755 wxSAVE);
756
757 if (dialog.ShowModal() == wxID_OK)
758 {
759 wxString path = dialog.GetPath();
760
761 if (!path.IsEmpty())
762 {
763 m_richTextCtrl->SaveFile(path);
764 }
765 }
766 }
767
768 void MyFrame::OnBold(wxCommandEvent& WXUNUSED(event))
769 {
770 m_richTextCtrl->ApplyBoldToSelection();
771 }
772
773 void MyFrame::OnItalic(wxCommandEvent& WXUNUSED(event))
774 {
775 m_richTextCtrl->ApplyItalicToSelection();
776 }
777
778 void MyFrame::OnUnderline(wxCommandEvent& WXUNUSED(event))
779 {
780 m_richTextCtrl->ApplyUnderlineToSelection();
781 }
782
783
784 void MyFrame::OnUpdateBold(wxUpdateUIEvent& event)
785 {
786 event.Check(m_richTextCtrl->IsSelectionBold());
787 }
788
789 void MyFrame::OnUpdateItalic(wxUpdateUIEvent& event)
790 {
791 event.Check(m_richTextCtrl->IsSelectionItalics());
792 }
793
794 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent& event)
795 {
796 event.Check(m_richTextCtrl->IsSelectionUnderlined());
797 }
798
799 void MyFrame::OnAlignLeft(wxCommandEvent& WXUNUSED(event))
800 {
801 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT);
802 }
803
804 void MyFrame::OnAlignCentre(wxCommandEvent& WXUNUSED(event))
805 {
806 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE);
807 }
808
809 void MyFrame::OnAlignRight(wxCommandEvent& WXUNUSED(event))
810 {
811 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT);
812 }
813
814 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent& event)
815 {
816 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT));
817 }
818
819 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent& event)
820 {
821 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE));
822 }
823
824 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent& event)
825 {
826 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT));
827 }
828
829 void MyFrame::OnFont(wxCommandEvent& WXUNUSED(event))
830 {
831 if (!m_richTextCtrl->HasSelection())
832 return;
833
834 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
835 wxFontData fontData;
836
837 wxTextAttrEx attr;
838 attr.SetFlags(wxTEXT_ATTR_FONT);
839
840 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
841 fontData.SetInitialFont(attr.GetFont());
842
843 wxFontDialog dialog(this, fontData);
844 if (dialog.ShowModal() == wxID_OK)
845 {
846 fontData = dialog.GetFontData();
847 attr.SetFlags(wxTEXT_ATTR_FONT);
848 attr.SetFont(fontData.GetChosenFont());
849 if (attr.GetFont().Ok())
850 {
851 m_richTextCtrl->SetStyle(range, attr);
852 }
853 }
854 }
855
856 void MyFrame::OnIndentMore(wxCommandEvent& WXUNUSED(event))
857 {
858 wxTextAttrEx attr;
859 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
860
861 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
862 {
863 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
864 if (m_richTextCtrl->HasSelection())
865 range = m_richTextCtrl->GetSelectionRange();
866
867 wxFontData fontData;
868 attr.SetLeftIndent(attr.GetLeftIndent() + 100);
869
870 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
871 m_richTextCtrl->SetStyle(range, attr);
872 }
873 }
874
875 void MyFrame::OnIndentLess(wxCommandEvent& WXUNUSED(event))
876 {
877 wxTextAttrEx attr;
878 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
879
880 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
881 {
882 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
883 if (m_richTextCtrl->HasSelection())
884 range = m_richTextCtrl->GetSelectionRange();
885
886 if (attr.GetLeftIndent() >= 100)
887 {
888 wxFontData fontData;
889 attr.SetLeftIndent(attr.GetLeftIndent() - 100);
890
891 m_richTextCtrl->SetStyle(range, attr);
892 }
893 }
894 }
895
896 void MyFrame::OnLineSpacingHalf(wxCommandEvent& WXUNUSED(event))
897 {
898 wxTextAttrEx attr;
899 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
900
901 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
902 {
903 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
904 if (m_richTextCtrl->HasSelection())
905 range = m_richTextCtrl->GetSelectionRange();
906
907 wxFontData fontData;
908 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
909 attr.SetLineSpacing(15);
910
911 m_richTextCtrl->SetStyle(range, attr);
912 }
913 }
914
915 void MyFrame::OnLineSpacingDouble(wxCommandEvent& WXUNUSED(event))
916 {
917 wxTextAttrEx attr;
918 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
919
920 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
921 {
922 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
923 if (m_richTextCtrl->HasSelection())
924 range = m_richTextCtrl->GetSelectionRange();
925
926 wxFontData fontData;
927 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
928 attr.SetLineSpacing(20);
929
930 m_richTextCtrl->SetStyle(range, attr);
931 }
932 }
933
934 void MyFrame::OnLineSpacingSingle(wxCommandEvent& WXUNUSED(event))
935 {
936 wxTextAttrEx attr;
937 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
938
939 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
940 {
941 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
942 if (m_richTextCtrl->HasSelection())
943 range = m_richTextCtrl->GetSelectionRange();
944
945 wxFontData fontData;
946 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
947 attr.SetLineSpacing(0); // Can also use 10
948
949 m_richTextCtrl->SetStyle(range, attr);
950 }
951 }
952
953 void MyFrame::OnParagraphSpacingMore(wxCommandEvent& WXUNUSED(event))
954 {
955 wxTextAttrEx attr;
956 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
957
958 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
959 {
960 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
961 if (m_richTextCtrl->HasSelection())
962 range = m_richTextCtrl->GetSelectionRange();
963
964 wxFontData fontData;
965 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() + 20);
966
967 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
968 m_richTextCtrl->SetStyle(range, attr);
969 }
970 }
971
972 void MyFrame::OnParagraphSpacingLess(wxCommandEvent& WXUNUSED(event))
973 {
974 wxTextAttrEx attr;
975 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
976
977 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
978 {
979 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
980 if (m_richTextCtrl->HasSelection())
981 range = m_richTextCtrl->GetSelectionRange();
982
983 if (attr.GetParagraphSpacingAfter() >= 20)
984 {
985 wxFontData fontData;
986 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() - 20);
987
988 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
989 m_richTextCtrl->SetStyle(range, attr);
990 }
991 }
992 }
993
994 void MyFrame::OnViewHTML(wxCommandEvent& WXUNUSED(event))
995 {
996 wxDialog dialog(this, wxID_ANY, _("HTML"), wxDefaultPosition, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE);
997
998 wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
999 dialog.SetSizer(boxSizer);
1000
1001 wxHtmlWindow* win = new wxHtmlWindow(& dialog, wxID_ANY, wxDefaultPosition, wxSize(500, 400), wxSUNKEN_BORDER);
1002 boxSizer->Add(win, 1, wxALL, 5);
1003
1004 wxButton* cancelButton = new wxButton(& dialog, wxID_CANCEL, wxT("&Close"));
1005 boxSizer->Add(cancelButton, 0, wxALL|wxCENTRE, 5);
1006
1007 wxString text;
1008 wxStringOutputStream strStream(& text);
1009
1010 wxRichTextHTMLHandler htmlHandler;
1011 if (htmlHandler.SaveFile(& m_richTextCtrl->GetBuffer(), strStream))
1012 {
1013 win->SetPage(text);
1014 }
1015
1016 boxSizer->Fit(& dialog);
1017
1018 dialog.ShowModal();
1019 }
1020