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