1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/richtext/richtext.cpp
3 // Purpose: wxWidgets rich text editor sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
33 #include "wx/fontdlg.h"
34 #include "wx/splitter.h"
35 #include "wx/sstream.h"
36 #include "wx/html/htmlwin.h"
38 #include "bitmaps/sample.xpm"
39 #include "bitmaps/smiley.xpm"
40 // #include "bitmaps/idea.xpm"
41 #include "bitmaps/zebra.xpm"
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"
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"
61 #include "wx/richtext/richtextctrl.h"
62 #include "wx/richtext/richtextstyles.h"
63 #include "wx/richtext/richtextxml.h"
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // Define a new application type, each program should derive a class from wxApp
74 class MyApp
: public wxApp
77 // override base class virtuals
78 // ----------------------------
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();
88 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
90 wxRichTextStyleSheet
* m_styleSheet
;
93 // Define a new frame type: this is going to be our main frame
94 class MyFrame
: public wxFrame
98 MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
= wxDefaultPosition
,
99 const wxSize
& size
= wxDefaultSize
, long style
= wxDEFAULT_FRAME_STYLE
);
101 // event handlers (these functions should _not_ be virtual)
102 void OnQuit(wxCommandEvent
& event
);
103 void OnAbout(wxCommandEvent
& event
);
105 void OnOpen(wxCommandEvent
& event
);
106 void OnSave(wxCommandEvent
& event
);
107 void OnSaveAs(wxCommandEvent
& event
);
109 void OnBold(wxCommandEvent
& event
);
110 void OnItalic(wxCommandEvent
& event
);
111 void OnUnderline(wxCommandEvent
& event
);
113 void OnUpdateBold(wxUpdateUIEvent
& event
);
114 void OnUpdateItalic(wxUpdateUIEvent
& event
);
115 void OnUpdateUnderline(wxUpdateUIEvent
& event
);
117 void OnAlignLeft(wxCommandEvent
& event
);
118 void OnAlignCentre(wxCommandEvent
& event
);
119 void OnAlignRight(wxCommandEvent
& event
);
121 void OnUpdateAlignLeft(wxUpdateUIEvent
& event
);
122 void OnUpdateAlignCentre(wxUpdateUIEvent
& event
);
123 void OnUpdateAlignRight(wxUpdateUIEvent
& event
);
125 void OnFont(wxCommandEvent
& event
);
126 void OnIndentMore(wxCommandEvent
& event
);
127 void OnIndentLess(wxCommandEvent
& event
);
129 void OnLineSpacingHalf(wxCommandEvent
& event
);
130 void OnLineSpacingDouble(wxCommandEvent
& event
);
131 void OnLineSpacingSingle(wxCommandEvent
& event
);
133 void OnParagraphSpacingMore(wxCommandEvent
& event
);
134 void OnParagraphSpacingLess(wxCommandEvent
& event
);
136 void OnViewHTML(wxCommandEvent
& event
);
138 // Forward command events to the current rich text control, if any
139 bool ProcessEvent(wxEvent
& event
);
142 // any class wishing to process wxWidgets events must use this macro
143 DECLARE_EVENT_TABLE()
145 wxRichTextCtrl
* m_richTextCtrl
;
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 // IDs for the controls and the menu commands
157 ID_About
= wxID_ABOUT
,
159 ID_FORMAT_BOLD
= 100,
164 ID_FORMAT_ALIGN_LEFT
,
165 ID_FORMAT_ALIGN_CENTRE
,
166 ID_FORMAT_ALIGN_RIGHT
,
168 ID_FORMAT_INDENT_MORE
,
169 ID_FORMAT_INDENT_LESS
,
171 ID_FORMAT_PARAGRAPH_SPACING_MORE
,
172 ID_FORMAT_PARAGRAPH_SPACING_LESS
,
174 ID_FORMAT_LINE_SPACING_HALF
,
175 ID_FORMAT_LINE_SPACING_DOUBLE
,
176 ID_FORMAT_LINE_SPACING_SINGLE
,
181 // ----------------------------------------------------------------------------
182 // event tables and other macros for wxWidgets
183 // ----------------------------------------------------------------------------
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
)
192 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
193 EVT_MENU(wxID_SAVE
, MyFrame::OnSave
)
194 EVT_MENU(wxID_SAVEAS
, MyFrame::OnSaveAs
)
196 EVT_MENU(ID_FORMAT_BOLD
, MyFrame::OnBold
)
197 EVT_MENU(ID_FORMAT_ITALIC
, MyFrame::OnItalic
)
198 EVT_MENU(ID_FORMAT_UNDERLINE
, MyFrame::OnUnderline
)
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
)
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
)
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
)
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
)
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
)
220 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE
, MyFrame::OnParagraphSpacingMore
)
221 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS
, MyFrame::OnParagraphSpacingLess
)
223 EVT_MENU(ID_VIEW_HTML
, MyFrame::OnViewHTML
)
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
233 // ============================================================================
235 // ============================================================================
237 // ----------------------------------------------------------------------------
238 // the application class
239 // ----------------------------------------------------------------------------
241 // 'Main program' equivalent: the program execution "starts" here
244 m_styleSheet
= new wxRichTextStyleSheet
;
248 // Add extra handlers (plain text is automatically added)
249 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler
);
250 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler
);
252 // Add image handlers
254 wxImage::AddHandler( new wxPNGHandler
);
258 wxImage::AddHandler( new wxJPEGHandler
);
262 wxImage::AddHandler( new wxGIFHandler
);
265 // create the main application window
266 MyFrame
*frame
= new MyFrame(_T("wxRichTextCtrl Sample"), wxID_ANY
, wxDefaultPosition
, wxSize(600, 500));
268 // and show it (the frames, unlike simple controls, are not shown when
269 // created initially)
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.
284 void MyApp::CreateStyles()
288 wxFont
romanFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
289 wxFont
swissFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
);
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
);
301 m_styleSheet
->AddParagraphStyle(normalPara
);
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
);
312 m_styleSheet
->AddParagraphStyle(indentedPara
);
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
);
323 m_styleSheet
->AddParagraphStyle(flIndentedPara
);
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
);
336 m_styleSheet
->AddCharacterStyle(boldDef
);
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
);
347 m_styleSheet
->AddCharacterStyle(italicDef
);
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
);
359 m_styleSheet
->AddCharacterStyle(redDef
);
362 // ----------------------------------------------------------------------------
364 // ----------------------------------------------------------------------------
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
)
371 // set the frame icon
375 wxMenu
*fileMenu
= new wxMenu
;
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"));
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"));
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"));
397 editMenu
->Append(wxID_CLEAR
, _("&Delete\tDel"));
399 editMenu
->AppendSeparator();
400 editMenu
->Append(wxID_SELECTALL
, _("Select A&ll\tCtrl+A"));
402 editMenu
->AppendSeparator();
403 editMenu
->Append(wxID_FIND
, _("&Find...\tCtrl+F"));
404 editMenu
->Append(stID_FIND_REPLACE
, _("&Replace...\tCtrl+R"));
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
, _("Indent &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..."));
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"));
435 // ... and attach this menu bar to the frame
438 // create a status bar just for fun (by default with 1 pane only)
441 SetStatusText(_T("Welcome to wxRichTextCtrl!"));
444 wxToolBar
* toolBar
= CreateToolBar();
446 toolBar
->AddTool(wxID_OPEN
, wxBitmap(open_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Open"));
447 toolBar
->AddTool(wxID_SAVEAS
, wxBitmap(save_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Save"));
448 toolBar
->AddSeparator();
449 toolBar
->AddTool(wxID_CUT
, wxBitmap(cut_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Cut"));
450 toolBar
->AddTool(wxID_COPY
, wxBitmap(copy_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Copy"));
451 toolBar
->AddTool(wxID_PASTE
, wxBitmap(paste_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Paste"));
452 toolBar
->AddSeparator();
453 toolBar
->AddTool(wxID_UNDO
, wxBitmap(undo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Undo"));
454 toolBar
->AddTool(wxID_REDO
, wxBitmap(redo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Redo"));
455 toolBar
->AddSeparator();
456 toolBar
->AddTool(ID_FORMAT_BOLD
, wxBitmap(bold_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Bold"));
457 toolBar
->AddTool(ID_FORMAT_ITALIC
, wxBitmap(italic_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Italic"));
458 toolBar
->AddTool(ID_FORMAT_UNDERLINE
, wxBitmap(underline_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Underline"));
459 toolBar
->AddSeparator();
460 toolBar
->AddTool(ID_FORMAT_ALIGN_LEFT
, wxBitmap(alignleft_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Left"));
461 toolBar
->AddTool(ID_FORMAT_ALIGN_CENTRE
, wxBitmap(centre_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Centre"));
462 toolBar
->AddTool(ID_FORMAT_ALIGN_RIGHT
, wxBitmap(alignright_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Right"));
463 toolBar
->AddSeparator();
464 toolBar
->AddTool(ID_FORMAT_INDENT_LESS
, wxBitmap(indentless_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent Less"));
465 toolBar
->AddTool(ID_FORMAT_INDENT_MORE
, wxBitmap(indentmore_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent More"));
466 toolBar
->AddSeparator();
467 toolBar
->AddTool(ID_FORMAT_FONT
, wxBitmap(font_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Font"));
471 wxSplitterWindow
* splitter
= new wxSplitterWindow(this, wxID_ANY
, wxDefaultPosition
, GetClientSize(), wxSP_NO_XP_THEME
|wxSP_3D
|wxSP_LIVE_UPDATE
);
473 wxFont textFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
474 wxFont boldFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxBOLD
);
475 wxFont italicFont
= wxFont(12, wxROMAN
, wxITALIC
, wxNORMAL
);
477 m_richTextCtrl
= new wxRichTextCtrl(splitter
, wxID_ANY
, wxDefaultPosition
, wxSize(200, 200), wxVSCROLL
|wxHSCROLL
|wxNO_BORDER
);
478 wxFont
font(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
481 font
.SetNoAntiAliasing(true);
483 m_richTextCtrl
->SetFont(font
);
485 wxRichTextStyleListBox
* styleListBox
= new wxRichTextStyleListBox(splitter
, wxID_ANY
);
486 splitter
->SplitVertically(m_richTextCtrl
, styleListBox
, 400);
488 splitter
->UpdateSize();
490 styleListBox
->SetStyleSheet(wxGetApp().GetStyleSheet());
491 styleListBox
->SetRichTextCtrl(m_richTextCtrl
);
492 styleListBox
->UpdateStyles();
494 wxRichTextCtrl
& r
= *m_richTextCtrl
;
496 r
.BeginSuppressUndo();
498 r
.BeginParagraphSpacing(0, 20);
500 r
.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE
);
504 r
.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
509 r
.WriteText(wxT("by Julian Smart"));
515 r
.WriteImage(wxBitmap(zebra_xpm
));
522 r
.WriteText(wxT("What can you do with this thing? "));
523 r
.WriteImage(wxBitmap(smiley_xpm
));
524 r
.WriteText(wxT(" Well, you can change text "));
526 r
.BeginTextColour(wxColour(255, 0, 0));
527 r
.WriteText(wxT("colour, like this red bit."));
530 r
.BeginTextColour(wxColour(0, 0, 255));
531 r
.WriteText(wxT(" And this blue bit."));
534 r
.WriteText(wxT(" Naturally you can make things "));
536 r
.WriteText(wxT("bold "));
539 r
.WriteText(wxT("or italic "));
542 r
.WriteText(wxT("or underlined."));
546 r
.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
549 r
.WriteText(wxT(" Next we'll show an indented paragraph."));
551 r
.BeginLeftIndent(60);
554 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."));
559 r
.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
561 r
.BeginLeftIndent(100, -40);
564 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."));
569 r
.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
571 r
.BeginNumberedBullet(1, 100, 60);
574 r
.WriteText(wxT("This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later."));
575 r
.EndNumberedBullet();
577 r
.BeginNumberedBullet(2, 100, 60);
580 r
.WriteText(wxT("This is my second item."));
581 r
.EndNumberedBullet();
585 r
.WriteText(wxT("The following paragraph is right-indented:"));
587 r
.BeginRightIndent(200);
590 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."));
595 r
.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
597 r
.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT
);
598 r
.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF
);
601 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
.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
608 r
.BeginSymbolBullet(wxT('*'), 100, 60);
610 r
.WriteText(wxT("Compatibility with wxTextCtrl API"));
613 r
.BeginSymbolBullet(wxT('*'), 100, 60);
615 r
.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
618 r
.BeginSymbolBullet(wxT('*'), 100, 60);
620 r
.WriteText(wxT("XML loading and saving"));
623 r
.BeginSymbolBullet(wxT('*'), 100, 60);
625 r
.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
628 r
.BeginSymbolBullet(wxT('*'), 100, 60);
630 r
.WriteText(wxT("Clipboard copy and paste"));
633 r
.BeginSymbolBullet(wxT('*'), 100, 60);
635 r
.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
638 r
.BeginSymbolBullet(wxT('*'), 100, 60);
640 r
.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
645 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!"));
647 r
.EndParagraphSpacing();
655 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
657 // true is to force the frame to close
661 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
664 msg
.Printf( _T("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
665 wxMessageBox(msg
, _T("About wxRichTextCtrl Sample"), wxOK
| wxICON_INFORMATION
, this);
668 // Forward command events to the current rich text control, if any
669 bool MyFrame::ProcessEvent(wxEvent
& event
)
671 if (event
.IsCommandEvent() && !event
.IsKindOf(CLASSINFO(wxChildFocusEvent
)))
673 // Problem: we can get infinite recursion because the events
674 // climb back up to this frame, and repeat.
675 // Assume that command events don't cause another command event
676 // to be called, so we can rely on inCommand not being overwritten
678 static int s_eventType
= 0;
679 static wxWindowID s_id
= 0;
681 if (s_id
!= event
.GetId() && s_eventType
!= event
.GetEventType())
683 s_eventType
= event
.GetEventType();
684 s_id
= event
.GetId();
686 wxWindow
* focusWin
= wxFindFocusDescendant(this);
687 if (focusWin
&& focusWin
->ProcessEvent(event
))
704 return wxFrame::ProcessEvent(event
);
707 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
711 wxArrayInt fileTypes
;
713 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes
);
716 filter
+= wxT("All files (*.*)|*.*");
718 wxFileDialog
dialog(this,
719 _("Choose a filename"),
725 if (dialog
.ShowModal() == wxID_OK
)
727 wxString path
= dialog
.GetPath();
731 int filterIndex
= dialog
.GetFilterIndex();
732 int fileType
= (filterIndex
< (int) fileTypes
.GetCount())
733 ? fileTypes
[filterIndex
]
734 : wxRICHTEXT_TYPE_TEXT
;
735 m_richTextCtrl
->LoadFile(path
, fileType
);
740 void MyFrame::OnSave(wxCommandEvent
& event
)
742 if (m_richTextCtrl
->GetFilename().empty())
747 m_richTextCtrl
->SaveFile();
750 void MyFrame::OnSaveAs(wxCommandEvent
& WXUNUSED(event
))
752 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, true);
756 wxFileDialog
dialog(this,
757 _("Choose a filename"),
763 if (dialog
.ShowModal() == wxID_OK
)
765 wxString path
= dialog
.GetPath();
769 m_richTextCtrl
->SaveFile(path
);
774 void MyFrame::OnBold(wxCommandEvent
& WXUNUSED(event
))
776 m_richTextCtrl
->ApplyBoldToSelection();
779 void MyFrame::OnItalic(wxCommandEvent
& WXUNUSED(event
))
781 m_richTextCtrl
->ApplyItalicToSelection();
784 void MyFrame::OnUnderline(wxCommandEvent
& WXUNUSED(event
))
786 m_richTextCtrl
->ApplyUnderlineToSelection();
790 void MyFrame::OnUpdateBold(wxUpdateUIEvent
& event
)
792 event
.Check(m_richTextCtrl
->IsSelectionBold());
795 void MyFrame::OnUpdateItalic(wxUpdateUIEvent
& event
)
797 event
.Check(m_richTextCtrl
->IsSelectionItalics());
800 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent
& event
)
802 event
.Check(m_richTextCtrl
->IsSelectionUnderlined());
805 void MyFrame::OnAlignLeft(wxCommandEvent
& WXUNUSED(event
))
807 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT
);
810 void MyFrame::OnAlignCentre(wxCommandEvent
& WXUNUSED(event
))
812 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE
);
815 void MyFrame::OnAlignRight(wxCommandEvent
& WXUNUSED(event
))
817 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT
);
820 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent
& event
)
822 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT
));
825 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent
& event
)
827 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE
));
830 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent
& event
)
832 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT
));
835 void MyFrame::OnFont(wxCommandEvent
& WXUNUSED(event
))
837 if (!m_richTextCtrl
->HasSelection())
840 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
844 attr
.SetFlags(wxTEXT_ATTR_FONT
);
846 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
847 fontData
.SetInitialFont(attr
.GetFont());
849 wxFontDialog
dialog(this, fontData
);
850 if (dialog
.ShowModal() == wxID_OK
)
852 fontData
= dialog
.GetFontData();
853 attr
.SetFlags(wxTEXT_ATTR_FONT
);
854 attr
.SetFont(fontData
.GetChosenFont());
855 if (attr
.GetFont().Ok())
857 m_richTextCtrl
->SetStyle(range
, attr
);
862 void MyFrame::OnIndentMore(wxCommandEvent
& WXUNUSED(event
))
865 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
867 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
869 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
870 if (m_richTextCtrl
->HasSelection())
871 range
= m_richTextCtrl
->GetSelectionRange();
874 attr
.SetLeftIndent(attr
.GetLeftIndent() + 100);
876 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
877 m_richTextCtrl
->SetStyle(range
, attr
);
881 void MyFrame::OnIndentLess(wxCommandEvent
& WXUNUSED(event
))
884 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
886 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
888 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
889 if (m_richTextCtrl
->HasSelection())
890 range
= m_richTextCtrl
->GetSelectionRange();
892 if (attr
.GetLeftIndent() >= 100)
895 attr
.SetLeftIndent(attr
.GetLeftIndent() - 100);
897 m_richTextCtrl
->SetStyle(range
, attr
);
902 void MyFrame::OnLineSpacingHalf(wxCommandEvent
& WXUNUSED(event
))
905 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
907 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
909 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
910 if (m_richTextCtrl
->HasSelection())
911 range
= m_richTextCtrl
->GetSelectionRange();
914 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
915 attr
.SetLineSpacing(15);
917 m_richTextCtrl
->SetStyle(range
, attr
);
921 void MyFrame::OnLineSpacingDouble(wxCommandEvent
& WXUNUSED(event
))
924 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
926 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
928 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
929 if (m_richTextCtrl
->HasSelection())
930 range
= m_richTextCtrl
->GetSelectionRange();
933 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
934 attr
.SetLineSpacing(20);
936 m_richTextCtrl
->SetStyle(range
, attr
);
940 void MyFrame::OnLineSpacingSingle(wxCommandEvent
& WXUNUSED(event
))
943 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
945 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
947 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
948 if (m_richTextCtrl
->HasSelection())
949 range
= m_richTextCtrl
->GetSelectionRange();
952 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
953 attr
.SetLineSpacing(0); // Can also use 10
955 m_richTextCtrl
->SetStyle(range
, attr
);
959 void MyFrame::OnParagraphSpacingMore(wxCommandEvent
& WXUNUSED(event
))
962 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
964 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
966 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
967 if (m_richTextCtrl
->HasSelection())
968 range
= m_richTextCtrl
->GetSelectionRange();
971 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() + 20);
973 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
974 m_richTextCtrl
->SetStyle(range
, attr
);
978 void MyFrame::OnParagraphSpacingLess(wxCommandEvent
& WXUNUSED(event
))
981 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
983 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
985 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
986 if (m_richTextCtrl
->HasSelection())
987 range
= m_richTextCtrl
->GetSelectionRange();
989 if (attr
.GetParagraphSpacingAfter() >= 20)
992 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() - 20);
994 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
995 m_richTextCtrl
->SetStyle(range
, attr
);
1000 void MyFrame::OnViewHTML(wxCommandEvent
& WXUNUSED(event
))
1002 wxDialog
dialog(this, wxID_ANY
, _("HTML"), wxDefaultPosition
, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE
);
1004 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
1005 dialog
.SetSizer(boxSizer
);
1007 wxHtmlWindow
* win
= new wxHtmlWindow(& dialog
, wxID_ANY
, wxDefaultPosition
, wxSize(500, 400), wxSUNKEN_BORDER
);
1008 boxSizer
->Add(win
, 1, wxALL
, 5);
1010 wxButton
* cancelButton
= new wxButton(& dialog
, wxID_CANCEL
, wxT("&Close"));
1011 boxSizer
->Add(cancelButton
, 0, wxALL
|wxCENTRE
, 5);
1014 wxStringOutputStream
strStream(& text
);
1016 wxRichTextHTMLHandler htmlHandler
;
1017 if (htmlHandler
.SaveFile(& m_richTextCtrl
->GetBuffer(), strStream
))
1022 boxSizer
->Fit(& dialog
);