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"
39 #include "wx/cshelp.h"
43 #include "../sample.xpm"
46 #include "bitmaps/smiley.xpm"
47 // #include "bitmaps/idea.xpm"
48 #include "bitmaps/zebra.xpm"
50 #include "bitmaps/open.xpm"
51 #include "bitmaps/save.xpm"
52 #include "bitmaps/copy.xpm"
53 #include "bitmaps/cut.xpm"
54 #include "bitmaps/paste.xpm"
55 #include "bitmaps/undo.xpm"
56 #include "bitmaps/redo.xpm"
57 #include "bitmaps/bold.xpm"
58 #include "bitmaps/italic.xpm"
59 #include "bitmaps/underline.xpm"
61 #include "bitmaps/alignleft.xpm"
62 #include "bitmaps/alignright.xpm"
63 #include "bitmaps/centre.xpm"
64 #include "bitmaps/font.xpm"
65 #include "bitmaps/indentless.xpm"
66 #include "bitmaps/indentmore.xpm"
68 #include "wx/richtext/richtextctrl.h"
69 #include "wx/richtext/richtextstyles.h"
70 #include "wx/richtext/richtextxml.h"
71 #include "wx/richtext/richtexthtml.h"
72 #include "wx/richtext/richtextformatdlg.h"
73 #include "wx/richtext/richtextsymboldlg.h"
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // Define a new application type, each program should derive a class from wxApp
84 class MyApp
: public wxApp
87 // override base class virtuals
88 // ----------------------------
90 // this one is called on application startup and is a good place for the app
91 // initialization (doing it here and not in the ctor allows to have an error
92 // return: if OnInit() returns false, the application terminates)
93 virtual bool OnInit();
98 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
100 wxRichTextStyleSheet
* m_styleSheet
;
103 // Define a new frame type: this is going to be our main frame
104 class MyFrame
: public wxFrame
108 MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
= wxDefaultPosition
,
109 const wxSize
& size
= wxDefaultSize
, long style
= wxDEFAULT_FRAME_STYLE
);
111 // event handlers (these functions should _not_ be virtual)
112 void OnQuit(wxCommandEvent
& event
);
113 void OnAbout(wxCommandEvent
& event
);
115 void OnOpen(wxCommandEvent
& event
);
116 void OnSave(wxCommandEvent
& event
);
117 void OnSaveAs(wxCommandEvent
& event
);
119 void OnBold(wxCommandEvent
& event
);
120 void OnItalic(wxCommandEvent
& event
);
121 void OnUnderline(wxCommandEvent
& event
);
123 void OnUpdateBold(wxUpdateUIEvent
& event
);
124 void OnUpdateItalic(wxUpdateUIEvent
& event
);
125 void OnUpdateUnderline(wxUpdateUIEvent
& event
);
127 void OnAlignLeft(wxCommandEvent
& event
);
128 void OnAlignCentre(wxCommandEvent
& event
);
129 void OnAlignRight(wxCommandEvent
& event
);
131 void OnUpdateAlignLeft(wxUpdateUIEvent
& event
);
132 void OnUpdateAlignCentre(wxUpdateUIEvent
& event
);
133 void OnUpdateAlignRight(wxUpdateUIEvent
& event
);
135 void OnIndentMore(wxCommandEvent
& event
);
136 void OnIndentLess(wxCommandEvent
& event
);
138 void OnFont(wxCommandEvent
& event
);
139 void OnParagraph(wxCommandEvent
& event
);
140 void OnFormat(wxCommandEvent
& event
);
141 void OnUpdateFormat(wxUpdateUIEvent
& event
);
143 void OnInsertSymbol(wxCommandEvent
& event
);
145 void OnLineSpacingHalf(wxCommandEvent
& event
);
146 void OnLineSpacingDouble(wxCommandEvent
& event
);
147 void OnLineSpacingSingle(wxCommandEvent
& event
);
149 void OnParagraphSpacingMore(wxCommandEvent
& event
);
150 void OnParagraphSpacingLess(wxCommandEvent
& event
);
152 void OnViewHTML(wxCommandEvent
& event
);
154 void OnSwitchStyleSheets(wxCommandEvent
& event
);
156 // Forward command events to the current rich text control, if any
157 bool ProcessEvent(wxEvent
& event
);
160 // any class wishing to process wxWidgets events must use this macro
161 DECLARE_EVENT_TABLE()
163 wxRichTextCtrl
* m_richTextCtrl
;
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 // IDs for the controls and the menu commands
175 ID_About
= wxID_ABOUT
,
177 ID_FORMAT_BOLD
= 100,
186 ID_FORMAT_ALIGN_LEFT
,
187 ID_FORMAT_ALIGN_CENTRE
,
188 ID_FORMAT_ALIGN_RIGHT
,
190 ID_FORMAT_INDENT_MORE
,
191 ID_FORMAT_INDENT_LESS
,
193 ID_FORMAT_PARAGRAPH_SPACING_MORE
,
194 ID_FORMAT_PARAGRAPH_SPACING_LESS
,
196 ID_FORMAT_LINE_SPACING_HALF
,
197 ID_FORMAT_LINE_SPACING_DOUBLE
,
198 ID_FORMAT_LINE_SPACING_SINGLE
,
201 ID_SWITCH_STYLE_SHEETS
,
204 ID_RICHTEXT_STYLE_LIST
,
205 ID_RICHTEXT_STYLE_COMBO
208 // ----------------------------------------------------------------------------
209 // event tables and other macros for wxWidgets
210 // ----------------------------------------------------------------------------
212 // the event tables connect the wxWidgets events with the functions (event
213 // handlers) which process them. It can be also done at run-time, but for the
214 // simple menu events like this the static method is much simpler.
215 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
216 EVT_MENU(ID_Quit
, MyFrame::OnQuit
)
217 EVT_MENU(ID_About
, MyFrame::OnAbout
)
219 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
220 EVT_MENU(wxID_SAVE
, MyFrame::OnSave
)
221 EVT_MENU(wxID_SAVEAS
, MyFrame::OnSaveAs
)
223 EVT_MENU(ID_FORMAT_BOLD
, MyFrame::OnBold
)
224 EVT_MENU(ID_FORMAT_ITALIC
, MyFrame::OnItalic
)
225 EVT_MENU(ID_FORMAT_UNDERLINE
, MyFrame::OnUnderline
)
227 EVT_UPDATE_UI(ID_FORMAT_BOLD
, MyFrame::OnUpdateBold
)
228 EVT_UPDATE_UI(ID_FORMAT_ITALIC
, MyFrame::OnUpdateItalic
)
229 EVT_UPDATE_UI(ID_FORMAT_UNDERLINE
, MyFrame::OnUpdateUnderline
)
231 EVT_MENU(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnAlignLeft
)
232 EVT_MENU(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnAlignCentre
)
233 EVT_MENU(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnAlignRight
)
235 EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnUpdateAlignLeft
)
236 EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnUpdateAlignCentre
)
237 EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnUpdateAlignRight
)
239 EVT_MENU(ID_FORMAT_FONT
, MyFrame::OnFont
)
240 EVT_MENU(ID_FORMAT_PARAGRAPH
, MyFrame::OnParagraph
)
241 EVT_MENU(ID_FORMAT_CONTENT
, MyFrame::OnFormat
)
242 EVT_UPDATE_UI(ID_FORMAT_CONTENT
, MyFrame::OnUpdateFormat
)
243 EVT_UPDATE_UI(ID_FORMAT_FONT
, MyFrame::OnUpdateFormat
)
244 EVT_UPDATE_UI(ID_FORMAT_PARAGRAPH
, MyFrame::OnUpdateFormat
)
245 EVT_MENU(ID_FORMAT_INDENT_MORE
, MyFrame::OnIndentMore
)
246 EVT_MENU(ID_FORMAT_INDENT_LESS
, MyFrame::OnIndentLess
)
248 EVT_MENU(ID_FORMAT_LINE_SPACING_HALF
, MyFrame::OnLineSpacingHalf
)
249 EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE
, MyFrame::OnLineSpacingSingle
)
250 EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE
, MyFrame::OnLineSpacingDouble
)
252 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE
, MyFrame::OnParagraphSpacingMore
)
253 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS
, MyFrame::OnParagraphSpacingLess
)
255 EVT_MENU(ID_INSERT_SYMBOL
, MyFrame::OnInsertSymbol
)
257 EVT_MENU(ID_VIEW_HTML
, MyFrame::OnViewHTML
)
258 EVT_MENU(ID_SWITCH_STYLE_SHEETS
, MyFrame::OnSwitchStyleSheets
)
261 // Create a new application object: this macro will allow wxWidgets to create
262 // the application object during program execution (it's better than using a
263 // static object for many reasons) and also implements the accessor function
264 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
268 // ============================================================================
270 // ============================================================================
272 // ----------------------------------------------------------------------------
273 // the application class
274 // ----------------------------------------------------------------------------
276 // 'Main program' equivalent: the program execution "starts" here
280 wxHelpProvider::Set(new wxSimpleHelpProvider
);
283 m_styleSheet
= new wxRichTextStyleSheet
;
287 // Add extra handlers (plain text is automatically added)
288 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler
);
289 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler
);
291 // Add image handlers
293 wxImage::AddHandler( new wxPNGHandler
);
297 wxImage::AddHandler( new wxJPEGHandler
);
301 wxImage::AddHandler( new wxGIFHandler
);
304 // create the main application window
305 MyFrame
*frame
= new MyFrame(_T("wxRichTextCtrl Sample"), wxID_ANY
, wxDefaultPosition
, wxSize(700, 600));
307 // and show it (the frames, unlike simple controls, are not shown when
308 // created initially)
311 // success: wxApp::OnRun() will be called which will enter the main message
312 // loop and the application will run. If we returned false here, the
313 // application would exit immediately.
323 void MyApp::CreateStyles()
327 wxFont
romanFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
328 wxFont
swissFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
);
330 wxRichTextParagraphStyleDefinition
* normalPara
= new wxRichTextParagraphStyleDefinition(wxT("Normal"));
331 wxRichTextAttr normalAttr
;
332 normalAttr
.SetFontFaceName(romanFont
.GetFaceName());
333 normalAttr
.SetFontSize(12);
334 // Let's set all attributes for this style
335 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
|
336 wxTEXT_ATTR_PARA_SPACING_BEFORE
|wxTEXT_ATTR_PARA_SPACING_AFTER
|wxTEXT_ATTR_LINE_SPACING
|
337 wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
);
338 normalPara
->SetStyle(normalAttr
);
340 m_styleSheet
->AddParagraphStyle(normalPara
);
342 wxRichTextParagraphStyleDefinition
* indentedPara
= new wxRichTextParagraphStyleDefinition(wxT("Indented"));
343 wxRichTextAttr indentedAttr
;
344 indentedAttr
.SetFontFaceName(romanFont
.GetFaceName());
345 indentedAttr
.SetFontSize(12);
346 indentedAttr
.SetLeftIndent(100, 0);
347 // We only want to affect indentation
348 indentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
349 indentedPara
->SetStyle(indentedAttr
);
351 m_styleSheet
->AddParagraphStyle(indentedPara
);
353 wxRichTextParagraphStyleDefinition
* indentedPara2
= new wxRichTextParagraphStyleDefinition(wxT("Red Bold Indented"));
354 wxRichTextAttr indentedAttr2
;
355 indentedAttr2
.SetFontFaceName(romanFont
.GetFaceName());
356 indentedAttr2
.SetFontSize(12);
357 indentedAttr2
.SetFontWeight(wxBOLD
);
358 indentedAttr2
.SetTextColour(*wxRED
);
359 indentedAttr2
.SetFontSize(12);
360 indentedAttr2
.SetLeftIndent(100, 0);
361 // We want to affect indentation, font and text colour
362 indentedAttr2
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
|wxTEXT_ATTR_FONT
|wxTEXT_ATTR_TEXT_COLOUR
);
363 indentedPara2
->SetStyle(indentedAttr2
);
365 m_styleSheet
->AddParagraphStyle(indentedPara2
);
367 wxRichTextParagraphStyleDefinition
* flIndentedPara
= new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
368 wxRichTextAttr flIndentedAttr
;
369 flIndentedAttr
.SetFontFaceName(swissFont
.GetFaceName());
370 flIndentedAttr
.SetFontSize(12);
371 flIndentedAttr
.SetLeftIndent(100, -100);
372 // We only want to affect indentation
373 flIndentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
374 flIndentedPara
->SetStyle(flIndentedAttr
);
376 m_styleSheet
->AddParagraphStyle(flIndentedPara
);
380 wxRichTextCharacterStyleDefinition
* boldDef
= new wxRichTextCharacterStyleDefinition(wxT("Bold"));
381 wxRichTextAttr boldAttr
;
382 boldAttr
.SetFontFaceName(romanFont
.GetFaceName());
383 boldAttr
.SetFontSize(12);
384 boldAttr
.SetFontWeight(wxBOLD
);
385 // We only want to affect boldness
386 boldAttr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
387 boldDef
->SetStyle(boldAttr
);
389 m_styleSheet
->AddCharacterStyle(boldDef
);
391 wxRichTextCharacterStyleDefinition
* italicDef
= new wxRichTextCharacterStyleDefinition(wxT("Italic"));
392 wxRichTextAttr italicAttr
;
393 italicAttr
.SetFontFaceName(romanFont
.GetFaceName());
394 italicAttr
.SetFontSize(12);
395 italicAttr
.SetFontStyle(wxITALIC
);
396 // We only want to affect italics
397 italicAttr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
398 italicDef
->SetStyle(italicAttr
);
400 m_styleSheet
->AddCharacterStyle(italicDef
);
402 wxRichTextCharacterStyleDefinition
* redDef
= new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
403 wxRichTextAttr redAttr
;
404 redAttr
.SetFontFaceName(romanFont
.GetFaceName());
405 redAttr
.SetFontSize(12);
406 redAttr
.SetFontWeight(wxBOLD
);
407 redAttr
.SetTextColour(*wxRED
);
408 // We only want to affect colour, weight and face
409 redAttr
.SetFlags(wxTEXT_ATTR_FONT_FACE
|wxTEXT_ATTR_FONT_WEIGHT
|wxTEXT_ATTR_TEXT_COLOUR
);
410 redDef
->SetStyle(redAttr
);
412 m_styleSheet
->AddCharacterStyle(redDef
);
415 // ----------------------------------------------------------------------------
417 // ----------------------------------------------------------------------------
420 MyFrame::MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
,
421 const wxSize
& size
, long style
)
422 : wxFrame(NULL
, id
, title
, pos
, size
, style
)
424 // set the frame icon
425 SetIcon(wxICON(sample
));
428 wxMenu
*fileMenu
= new wxMenu
;
430 // the "About" item should be in the help menu
431 wxMenu
*helpMenu
= new wxMenu
;
432 helpMenu
->Append(ID_About
, _T("&About...\tF1"), _T("Show about dialog"));
434 fileMenu
->Append(wxID_OPEN
, _T("&Open\tCtrl+O"), _T("Open a file"));
435 fileMenu
->Append(wxID_SAVE
, _T("&Save\tCtrl+S"), _T("Save a file"));
436 fileMenu
->Append(wxID_SAVEAS
, _T("&Save As...\tF12"), _T("Save to a new file"));
437 fileMenu
->AppendSeparator();
438 fileMenu
->Append(ID_VIEW_HTML
, _T("&View as HTML"), _T("View HTML"));
439 fileMenu
->AppendSeparator();
440 fileMenu
->Append(ID_Quit
, _T("E&xit\tAlt+X"), _T("Quit this program"));
442 wxMenu
* editMenu
= new wxMenu
;
443 editMenu
->Append(wxID_UNDO
, _("&Undo\tCtrl+Z"));
444 editMenu
->Append(wxID_REDO
, _("&Redo\tCtrl+Y"));
445 editMenu
->AppendSeparator();
446 editMenu
->Append(wxID_CUT
, _("Cu&t\tCtrl+X"));
447 editMenu
->Append(wxID_COPY
, _("&Copy\tCtrl+C"));
448 editMenu
->Append(wxID_PASTE
, _("&Paste\tCtrl+V"));
450 editMenu
->Append(wxID_CLEAR
, _("&Delete\tDel"));
452 editMenu
->AppendSeparator();
453 editMenu
->Append(wxID_SELECTALL
, _("Select A&ll\tCtrl+A"));
455 editMenu
->AppendSeparator();
456 editMenu
->Append(wxID_FIND
, _("&Find...\tCtrl+F"));
457 editMenu
->Append(stID_FIND_REPLACE
, _("&Replace...\tCtrl+R"));
460 wxMenu
* formatMenu
= new wxMenu
;
461 formatMenu
->AppendCheckItem(ID_FORMAT_BOLD
, _("&Bold\tCtrl+B"));
462 formatMenu
->AppendCheckItem(ID_FORMAT_ITALIC
, _("&Italic\tCtrl+I"));
463 formatMenu
->AppendCheckItem(ID_FORMAT_UNDERLINE
, _("&Underline\tCtrl+U"));
464 formatMenu
->AppendSeparator();
465 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_LEFT
, _("L&eft Align"));
466 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT
, _("&Right Align"));
467 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE
, _("&Centre"));
468 formatMenu
->AppendSeparator();
469 formatMenu
->Append(ID_FORMAT_INDENT_MORE
, _("Indent &More"));
470 formatMenu
->Append(ID_FORMAT_INDENT_LESS
, _("Indent &Less"));
471 formatMenu
->AppendSeparator();
472 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE
, _("Increase Paragraph &Spacing"));
473 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS
, _("Decrease &Paragraph Spacing"));
474 formatMenu
->AppendSeparator();
475 formatMenu
->Append(ID_FORMAT_LINE_SPACING_SINGLE
, _("Normal Line Spacing"));
476 formatMenu
->Append(ID_FORMAT_LINE_SPACING_HALF
, _("1.5 Line Spacing"));
477 formatMenu
->Append(ID_FORMAT_LINE_SPACING_DOUBLE
, _("Double Line Spacing"));
478 formatMenu
->AppendSeparator();
479 formatMenu
->Append(ID_FORMAT_FONT
, _("&Font..."));
480 formatMenu
->Append(ID_FORMAT_PARAGRAPH
, _("&Paragraph..."));
481 formatMenu
->Append(ID_FORMAT_CONTENT
, _("Font and Pa&ragraph...\tShift+Ctrl+F"));
482 formatMenu
->AppendSeparator();
483 formatMenu
->Append(ID_SWITCH_STYLE_SHEETS
, _("&Switch Style Sheets"));
485 wxMenu
* insertMenu
= new wxMenu
;
486 insertMenu
->Append(ID_INSERT_SYMBOL
, _("&Symbol...\tCtrl+I"));
488 // now append the freshly created menu to the menu bar...
489 wxMenuBar
*menuBar
= new wxMenuBar();
490 menuBar
->Append(fileMenu
, _T("&File"));
491 menuBar
->Append(editMenu
, _T("&Edit"));
492 menuBar
->Append(formatMenu
, _T("F&ormat"));
493 menuBar
->Append(insertMenu
, _T("&Insert"));
494 menuBar
->Append(helpMenu
, _T("&Help"));
496 // ... and attach this menu bar to the frame
499 // create a status bar just for fun (by default with 1 pane only)
500 // but don't create it on limited screen space (WinCE)
501 bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
507 SetStatusText(_T("Welcome to wxRichTextCtrl!"));
511 wxToolBar
* toolBar
= CreateToolBar();
513 toolBar
->AddTool(wxID_OPEN
, wxBitmap(open_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Open"));
514 toolBar
->AddTool(wxID_SAVEAS
, wxBitmap(save_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Save"));
515 toolBar
->AddSeparator();
516 toolBar
->AddTool(wxID_CUT
, wxBitmap(cut_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Cut"));
517 toolBar
->AddTool(wxID_COPY
, wxBitmap(copy_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Copy"));
518 toolBar
->AddTool(wxID_PASTE
, wxBitmap(paste_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Paste"));
519 toolBar
->AddSeparator();
520 toolBar
->AddTool(wxID_UNDO
, wxBitmap(undo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Undo"));
521 toolBar
->AddTool(wxID_REDO
, wxBitmap(redo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Redo"));
522 toolBar
->AddSeparator();
523 toolBar
->AddTool(ID_FORMAT_BOLD
, wxBitmap(bold_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Bold"));
524 toolBar
->AddTool(ID_FORMAT_ITALIC
, wxBitmap(italic_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Italic"));
525 toolBar
->AddTool(ID_FORMAT_UNDERLINE
, wxBitmap(underline_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Underline"));
526 toolBar
->AddSeparator();
527 toolBar
->AddTool(ID_FORMAT_ALIGN_LEFT
, wxBitmap(alignleft_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Left"));
528 toolBar
->AddTool(ID_FORMAT_ALIGN_CENTRE
, wxBitmap(centre_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Centre"));
529 toolBar
->AddTool(ID_FORMAT_ALIGN_RIGHT
, wxBitmap(alignright_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Right"));
530 toolBar
->AddSeparator();
531 toolBar
->AddTool(ID_FORMAT_INDENT_LESS
, wxBitmap(indentless_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent Less"));
532 toolBar
->AddTool(ID_FORMAT_INDENT_MORE
, wxBitmap(indentmore_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent More"));
533 toolBar
->AddSeparator();
534 toolBar
->AddTool(ID_FORMAT_FONT
, wxBitmap(font_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Font"));
536 wxRichTextStyleComboCtrl
* combo
= new wxRichTextStyleComboCtrl(toolBar
, ID_RICHTEXT_STYLE_COMBO
, wxDefaultPosition
, wxSize(200, -1));
537 toolBar
->AddControl(combo
);
541 wxSplitterWindow
* splitter
= new wxSplitterWindow(this, wxID_ANY
, wxDefaultPosition
, GetClientSize(), wxSP_NO_XP_THEME
|wxSP_3D
|wxSP_LIVE_UPDATE
);
543 wxFont textFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
544 wxFont boldFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxBOLD
);
545 wxFont italicFont
= wxFont(12, wxROMAN
, wxITALIC
, wxNORMAL
);
547 m_richTextCtrl
= new wxRichTextCtrl(splitter
, ID_RICHTEXT_CTRL
, wxEmptyString
, wxDefaultPosition
, wxSize(200, 200), wxVSCROLL
|wxHSCROLL
|wxNO_BORDER
|wxWANTS_CHARS
);
548 wxFont
font(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
550 m_richTextCtrl
->SetFont(font
);
552 m_richTextCtrl
->SetStyleSheet(wxGetApp().GetStyleSheet());
554 combo
->SetStyleSheet(wxGetApp().GetStyleSheet());
555 combo
->SetRichTextCtrl(m_richTextCtrl
);
556 combo
->UpdateStyles();
558 wxRichTextStyleListBox
* styleListBox
= new wxRichTextStyleListBox(splitter
, ID_RICHTEXT_STYLE_LIST
);
560 wxSize display
= wxGetDisplaySize();
561 if ( is_pda
&& ( display
.GetWidth() < display
.GetHeight() ) )
563 splitter
->SplitHorizontally(m_richTextCtrl
, styleListBox
);
567 splitter
->SplitVertically(m_richTextCtrl
, styleListBox
, 500);
570 splitter
->UpdateSize();
572 styleListBox
->SetStyleSheet(wxGetApp().GetStyleSheet());
573 styleListBox
->SetRichTextCtrl(m_richTextCtrl
);
574 styleListBox
->UpdateStyles();
576 wxRichTextCtrl
& r
= *m_richTextCtrl
;
579 r
.WriteText(wxT("One\nTwo\nThree\n"));
582 for (i
= 0; i
< 3; i
++)
585 wxRichTextParagraph
* para
= r
.GetBuffer().GetParagraphAtLine(i
);
588 wxLogDebug(wxT("Range for paragraph %d: %d -> %d"), i
, para
->GetRange().GetStart(), para
->GetRange().GetEnd());
594 r
.BeginSuppressUndo();
596 r
.BeginParagraphSpacing(0, 20);
598 r
.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE
);
602 r
.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
607 r
.WriteText(wxT("by Julian Smart"));
613 r
.WriteImage(wxBitmap(zebra_xpm
));
620 r
.WriteText(wxT("What can you do with this thing? "));
621 r
.WriteImage(wxBitmap(smiley_xpm
));
622 r
.WriteText(wxT(" Well, you can change text "));
624 r
.BeginTextColour(wxColour(255, 0, 0));
625 r
.WriteText(wxT("colour, like this red bit."));
628 r
.BeginTextColour(wxColour(0, 0, 255));
629 r
.WriteText(wxT(" And this blue bit."));
632 r
.WriteText(wxT(" Naturally you can make things "));
634 r
.WriteText(wxT("bold "));
637 r
.WriteText(wxT("or italic "));
640 r
.WriteText(wxT("or underlined."));
644 r
.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
647 r
.WriteText(wxT(" Next we'll show an indented paragraph."));
649 r
.BeginLeftIndent(60);
652 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."));
657 r
.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
659 r
.BeginLeftIndent(100, -40);
662 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."));
667 r
.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
669 r
.BeginNumberedBullet(1, 100, 60);
672 r
.WriteText(wxT("This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later."));
673 r
.EndNumberedBullet();
675 r
.BeginNumberedBullet(2, 100, 60);
678 r
.WriteText(wxT("This is my second item."));
679 r
.EndNumberedBullet();
683 r
.WriteText(wxT("The following paragraph is right-indented:"));
685 r
.BeginRightIndent(200);
688 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."));
693 r
.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
695 r
.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT
);
696 r
.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF
);
699 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."));
709 attr
.SetFlags(wxTEXT_ATTR_TABS
);
711 r
.SetDefaultStyle(attr
);
714 r
.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
717 r
.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
719 r
.BeginSymbolBullet(wxT('*'), 100, 60);
721 r
.WriteText(wxT("Compatibility with wxTextCtrl API"));
724 r
.BeginSymbolBullet(wxT('*'), 100, 60);
726 r
.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
729 r
.BeginSymbolBullet(wxT('*'), 100, 60);
731 r
.WriteText(wxT("XML loading and saving"));
734 r
.BeginSymbolBullet(wxT('*'), 100, 60);
736 r
.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
739 r
.BeginSymbolBullet(wxT('*'), 100, 60);
741 r
.WriteText(wxT("Clipboard copy and paste"));
744 r
.BeginSymbolBullet(wxT('*'), 100, 60);
746 r
.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
749 r
.BeginSymbolBullet(wxT('*'), 100, 60);
751 r
.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
756 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!"));
758 r
.EndParagraphSpacing();
767 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
769 // true is to force the frame to close
773 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
776 msg
.Printf( _T("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
777 wxMessageBox(msg
, _T("About wxRichTextCtrl Sample"), wxOK
| wxICON_INFORMATION
, this);
780 // Forward command events to the current rich text control, if any
781 bool MyFrame::ProcessEvent(wxEvent
& event
)
783 if (event
.IsCommandEvent() && !event
.IsKindOf(CLASSINFO(wxChildFocusEvent
)))
785 // Problem: we can get infinite recursion because the events
786 // climb back up to this frame, and repeat.
787 // Assume that command events don't cause another command event
788 // to be called, so we can rely on inCommand not being overwritten
790 static int s_eventType
= 0;
791 static wxWindowID s_id
= 0;
793 if (s_id
!= event
.GetId() && s_eventType
!= event
.GetEventType())
795 s_eventType
= event
.GetEventType();
796 s_id
= event
.GetId();
798 wxWindow
* focusWin
= wxFindFocusDescendant(this);
799 if (focusWin
&& focusWin
->ProcessEvent(event
))
816 return wxFrame::ProcessEvent(event
);
819 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
823 wxArrayInt fileTypes
;
825 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes
);
828 filter
+= wxT("All files (*.*)|*.*");
830 wxFileDialog
dialog(this,
831 _("Choose a filename"),
837 if (dialog
.ShowModal() == wxID_OK
)
839 wxString path
= dialog
.GetPath();
843 int filterIndex
= dialog
.GetFilterIndex();
844 int fileType
= (filterIndex
< (int) fileTypes
.GetCount())
845 ? fileTypes
[filterIndex
]
846 : wxRICHTEXT_TYPE_TEXT
;
847 m_richTextCtrl
->LoadFile(path
, fileType
);
852 void MyFrame::OnSave(wxCommandEvent
& event
)
854 if (m_richTextCtrl
->GetFilename().empty())
859 m_richTextCtrl
->SaveFile();
862 void MyFrame::OnSaveAs(wxCommandEvent
& WXUNUSED(event
))
864 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, true);
868 wxFileDialog
dialog(this,
869 _("Choose a filename"),
875 if (dialog
.ShowModal() == wxID_OK
)
877 wxString path
= dialog
.GetPath();
881 m_richTextCtrl
->SaveFile(path
);
886 void MyFrame::OnBold(wxCommandEvent
& WXUNUSED(event
))
888 m_richTextCtrl
->ApplyBoldToSelection();
891 void MyFrame::OnItalic(wxCommandEvent
& WXUNUSED(event
))
893 m_richTextCtrl
->ApplyItalicToSelection();
896 void MyFrame::OnUnderline(wxCommandEvent
& WXUNUSED(event
))
898 m_richTextCtrl
->ApplyUnderlineToSelection();
902 void MyFrame::OnUpdateBold(wxUpdateUIEvent
& event
)
904 event
.Check(m_richTextCtrl
->IsSelectionBold());
907 void MyFrame::OnUpdateItalic(wxUpdateUIEvent
& event
)
909 event
.Check(m_richTextCtrl
->IsSelectionItalics());
912 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent
& event
)
914 event
.Check(m_richTextCtrl
->IsSelectionUnderlined());
917 void MyFrame::OnAlignLeft(wxCommandEvent
& WXUNUSED(event
))
919 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT
);
922 void MyFrame::OnAlignCentre(wxCommandEvent
& WXUNUSED(event
))
924 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE
);
927 void MyFrame::OnAlignRight(wxCommandEvent
& WXUNUSED(event
))
929 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT
);
932 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent
& event
)
934 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT
));
937 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent
& event
)
939 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE
));
942 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent
& event
)
944 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT
));
947 void MyFrame::OnFont(wxCommandEvent
& WXUNUSED(event
))
949 wxRichTextRange range
;
950 if (m_richTextCtrl
->HasSelection())
951 range
= m_richTextCtrl
->GetSelectionRange();
953 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
955 int pages
= wxRICHTEXT_FORMAT_FONT
;
957 wxRichTextFormattingDialog
formatDlg(pages
, this);
958 formatDlg
.GetStyle(m_richTextCtrl
, range
);
960 if (formatDlg
.ShowModal() == wxID_OK
)
962 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
965 // Old method using wxFontDialog
967 if (!m_richTextCtrl
->HasSelection())
970 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
974 attr
.SetFlags(wxTEXT_ATTR_FONT
);
976 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
977 fontData
.SetInitialFont(attr
.GetFont());
979 wxFontDialog
dialog(this, fontData
);
980 if (dialog
.ShowModal() == wxID_OK
)
982 fontData
= dialog
.GetFontData();
983 attr
.SetFlags(wxTEXT_ATTR_FONT
);
984 attr
.SetFont(fontData
.GetChosenFont());
985 if (attr
.GetFont().Ok())
987 m_richTextCtrl
->SetStyle(range
, attr
);
993 void MyFrame::OnParagraph(wxCommandEvent
& WXUNUSED(event
))
995 wxRichTextRange range
;
996 if (m_richTextCtrl
->HasSelection())
997 range
= m_richTextCtrl
->GetSelectionRange();
999 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
1001 int pages
= wxRICHTEXT_FORMAT_INDENTS_SPACING
|wxRICHTEXT_FORMAT_TABS
|wxRICHTEXT_FORMAT_BULLETS
;
1003 wxRichTextFormattingDialog
formatDlg(pages
, this);
1004 formatDlg
.GetStyle(m_richTextCtrl
, range
);
1006 if (formatDlg
.ShowModal() == wxID_OK
)
1008 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
1012 void MyFrame::OnFormat(wxCommandEvent
& WXUNUSED(event
))
1014 wxRichTextRange range
;
1015 if (m_richTextCtrl
->HasSelection())
1016 range
= m_richTextCtrl
->GetSelectionRange();
1018 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
1020 int pages
= wxRICHTEXT_FORMAT_FONT
|wxRICHTEXT_FORMAT_INDENTS_SPACING
|wxRICHTEXT_FORMAT_TABS
|wxRICHTEXT_FORMAT_BULLETS
;
1022 wxRichTextFormattingDialog
formatDlg(pages
, this);
1023 formatDlg
.GetStyle(m_richTextCtrl
, range
);
1025 if (formatDlg
.ShowModal() == wxID_OK
)
1027 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
1031 void MyFrame::OnUpdateFormat(wxUpdateUIEvent
& event
)
1033 event
.Enable(m_richTextCtrl
->HasSelection());
1036 void MyFrame::OnIndentMore(wxCommandEvent
& WXUNUSED(event
))
1039 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1041 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1043 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1044 if (m_richTextCtrl
->HasSelection())
1045 range
= m_richTextCtrl
->GetSelectionRange();
1047 wxFontData fontData
;
1048 attr
.SetLeftIndent(attr
.GetLeftIndent() + 100);
1050 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1051 m_richTextCtrl
->SetStyle(range
, attr
);
1055 void MyFrame::OnIndentLess(wxCommandEvent
& WXUNUSED(event
))
1058 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1060 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1062 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1063 if (m_richTextCtrl
->HasSelection())
1064 range
= m_richTextCtrl
->GetSelectionRange();
1066 if (attr
.GetLeftIndent() >= 100)
1068 wxFontData fontData
;
1069 attr
.SetLeftIndent(attr
.GetLeftIndent() - 100);
1071 m_richTextCtrl
->SetStyle(range
, attr
);
1076 void MyFrame::OnLineSpacingHalf(wxCommandEvent
& WXUNUSED(event
))
1079 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1081 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1083 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1084 if (m_richTextCtrl
->HasSelection())
1085 range
= m_richTextCtrl
->GetSelectionRange();
1087 wxFontData fontData
;
1088 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1089 attr
.SetLineSpacing(15);
1091 m_richTextCtrl
->SetStyle(range
, attr
);
1095 void MyFrame::OnLineSpacingDouble(wxCommandEvent
& WXUNUSED(event
))
1098 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1100 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1102 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1103 if (m_richTextCtrl
->HasSelection())
1104 range
= m_richTextCtrl
->GetSelectionRange();
1106 wxFontData fontData
;
1107 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1108 attr
.SetLineSpacing(20);
1110 m_richTextCtrl
->SetStyle(range
, attr
);
1114 void MyFrame::OnLineSpacingSingle(wxCommandEvent
& WXUNUSED(event
))
1117 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1119 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1121 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1122 if (m_richTextCtrl
->HasSelection())
1123 range
= m_richTextCtrl
->GetSelectionRange();
1125 wxFontData fontData
;
1126 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1127 attr
.SetLineSpacing(0); // Can also use 10
1129 m_richTextCtrl
->SetStyle(range
, attr
);
1133 void MyFrame::OnParagraphSpacingMore(wxCommandEvent
& WXUNUSED(event
))
1136 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1138 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1140 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1141 if (m_richTextCtrl
->HasSelection())
1142 range
= m_richTextCtrl
->GetSelectionRange();
1144 wxFontData fontData
;
1145 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() + 20);
1147 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1148 m_richTextCtrl
->SetStyle(range
, attr
);
1152 void MyFrame::OnParagraphSpacingLess(wxCommandEvent
& WXUNUSED(event
))
1155 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1157 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1159 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1160 if (m_richTextCtrl
->HasSelection())
1161 range
= m_richTextCtrl
->GetSelectionRange();
1163 if (attr
.GetParagraphSpacingAfter() >= 20)
1165 wxFontData fontData
;
1166 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() - 20);
1168 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1169 m_richTextCtrl
->SetStyle(range
, attr
);
1174 void MyFrame::OnViewHTML(wxCommandEvent
& WXUNUSED(event
))
1176 wxDialog
dialog(this, wxID_ANY
, _("HTML"), wxDefaultPosition
, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE
);
1178 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
1179 dialog
.SetSizer(boxSizer
);
1181 wxHtmlWindow
* win
= new wxHtmlWindow(& dialog
, wxID_ANY
, wxDefaultPosition
, wxSize(500, 400), wxSUNKEN_BORDER
);
1182 boxSizer
->Add(win
, 1, wxALL
, 5);
1184 wxButton
* cancelButton
= new wxButton(& dialog
, wxID_CANCEL
, wxT("&Close"));
1185 boxSizer
->Add(cancelButton
, 0, wxALL
|wxCENTRE
, 5);
1188 wxStringOutputStream
strStream(& text
);
1190 wxRichTextHTMLHandler htmlHandler
;
1191 if (htmlHandler
.SaveFile(& m_richTextCtrl
->GetBuffer(), strStream
))
1196 boxSizer
->Fit(& dialog
);
1201 // Demonstrates how you can change the style sheets and have the changes
1202 // reflected in the control content without wiping out character formatting.
1204 void MyFrame::OnSwitchStyleSheets(wxCommandEvent
& WXUNUSED(event
))
1206 static wxRichTextStyleSheet
* gs_AlternateStyleSheet
= NULL
;
1208 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1209 wxRichTextStyleListBox
* styleList
= (wxRichTextStyleListBox
*) FindWindow(ID_RICHTEXT_STYLE_LIST
);
1210 wxRichTextStyleComboCtrl
* styleCombo
= (wxRichTextStyleComboCtrl
*) FindWindow(ID_RICHTEXT_STYLE_COMBO
);
1212 wxRichTextStyleSheet
* sheet
= ctrl
->GetStyleSheet();
1214 // One-time creation of an alternate style sheet
1215 if (!gs_AlternateStyleSheet
)
1217 gs_AlternateStyleSheet
= new wxRichTextStyleSheet(*sheet
);
1219 // Make some modifications
1220 for (int i
= 0; i
< (int) gs_AlternateStyleSheet
->GetParagraphStyleCount(); i
++)
1222 wxRichTextParagraphStyleDefinition
* def
= gs_AlternateStyleSheet
->GetParagraphStyle(i
);
1224 if (def
->GetStyle().HasTextColour())
1225 def
->GetStyle().SetTextColour(*wxBLUE
);
1227 if (def
->GetStyle().HasAlignment())
1229 if (def
->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1230 def
->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1231 else if (def
->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1232 def
->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1234 if (def
->GetStyle().HasLeftIndent())
1236 def
->GetStyle().SetLeftIndent(def
->GetStyle().GetLeftIndent() * 2);
1242 wxRichTextStyleSheet
* tmp
= gs_AlternateStyleSheet
;
1243 gs_AlternateStyleSheet
= sheet
;
1246 ctrl
->SetStyleSheet(sheet
);
1247 ctrl
->ApplyStyleSheet(sheet
); // Makes the control reflect the new style definitions
1249 styleList
->SetStyleSheet(sheet
);
1250 styleList
->UpdateStyles();
1252 styleCombo
->SetStyleSheet(sheet
);
1253 styleCombo
->UpdateStyles();
1256 void MyFrame::OnInsertSymbol(wxCommandEvent
& WXUNUSED(event
))
1258 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1261 attr
.SetFlags(wxTEXT_ATTR_FONT
);
1262 ctrl
->GetStyle(ctrl
->GetInsertionPoint(), attr
);
1264 wxString currentFontName
;
1265 if (attr
.HasFont() && attr
.GetFont().Ok())
1266 currentFontName
= attr
.GetFont().GetFaceName();
1268 // Don't set the initial font in the dialog (so the user is choosing
1269 // 'normal text', i.e. the current font) but do tell the dialog
1270 // what 'normal text' is.
1272 wxSymbolPickerDialog
dlg(wxT("*"), wxEmptyString
, currentFontName
, this);
1274 if (dlg
.ShowModal() == wxID_OK
)
1276 if (dlg
.HasSelection())
1278 long insertionPoint
= ctrl
->GetInsertionPoint();
1280 ctrl
->WriteText(dlg
.GetSymbol());
1282 if (!dlg
.UseNormalFont())
1284 wxFont
font(attr
.GetFont());
1285 font
.SetFaceName(dlg
.GetNormalTextFontName());
1287 ctrl
->SetStyle(insertionPoint
, insertionPoint
+1, attr
);