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 "../sample.xpm"
42 #include "bitmaps/smiley.xpm"
43 // #include "bitmaps/idea.xpm"
44 #include "bitmaps/zebra.xpm"
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"
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"
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 #include "wx/richtext/richtextformatdlg.h"
69 #include "wx/richtext/richtextsymboldlg.h"
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // Define a new application type, each program should derive a class from wxApp
80 class MyApp
: public wxApp
83 // override base class virtuals
84 // ----------------------------
86 // this one is called on application startup and is a good place for the app
87 // initialization (doing it here and not in the ctor allows to have an error
88 // return: if OnInit() returns false, the application terminates)
89 virtual bool OnInit();
94 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
96 wxRichTextStyleSheet
* m_styleSheet
;
99 // Define a new frame type: this is going to be our main frame
100 class MyFrame
: public wxFrame
104 MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
= wxDefaultPosition
,
105 const wxSize
& size
= wxDefaultSize
, long style
= wxDEFAULT_FRAME_STYLE
);
107 // event handlers (these functions should _not_ be virtual)
108 void OnQuit(wxCommandEvent
& event
);
109 void OnAbout(wxCommandEvent
& event
);
111 void OnOpen(wxCommandEvent
& event
);
112 void OnSave(wxCommandEvent
& event
);
113 void OnSaveAs(wxCommandEvent
& event
);
115 void OnBold(wxCommandEvent
& event
);
116 void OnItalic(wxCommandEvent
& event
);
117 void OnUnderline(wxCommandEvent
& event
);
119 void OnUpdateBold(wxUpdateUIEvent
& event
);
120 void OnUpdateItalic(wxUpdateUIEvent
& event
);
121 void OnUpdateUnderline(wxUpdateUIEvent
& event
);
123 void OnAlignLeft(wxCommandEvent
& event
);
124 void OnAlignCentre(wxCommandEvent
& event
);
125 void OnAlignRight(wxCommandEvent
& event
);
127 void OnUpdateAlignLeft(wxUpdateUIEvent
& event
);
128 void OnUpdateAlignCentre(wxUpdateUIEvent
& event
);
129 void OnUpdateAlignRight(wxUpdateUIEvent
& event
);
131 void OnIndentMore(wxCommandEvent
& event
);
132 void OnIndentLess(wxCommandEvent
& event
);
134 void OnFont(wxCommandEvent
& event
);
135 void OnParagraph(wxCommandEvent
& event
);
136 void OnFormat(wxCommandEvent
& event
);
137 void OnUpdateFormat(wxUpdateUIEvent
& event
);
139 void OnInsertSymbol(wxCommandEvent
& event
);
141 void OnLineSpacingHalf(wxCommandEvent
& event
);
142 void OnLineSpacingDouble(wxCommandEvent
& event
);
143 void OnLineSpacingSingle(wxCommandEvent
& event
);
145 void OnParagraphSpacingMore(wxCommandEvent
& event
);
146 void OnParagraphSpacingLess(wxCommandEvent
& event
);
148 void OnViewHTML(wxCommandEvent
& event
);
150 void OnSwitchStyleSheets(wxCommandEvent
& event
);
152 // Forward command events to the current rich text control, if any
153 bool ProcessEvent(wxEvent
& event
);
156 // any class wishing to process wxWidgets events must use this macro
157 DECLARE_EVENT_TABLE()
159 wxRichTextCtrl
* m_richTextCtrl
;
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 // IDs for the controls and the menu commands
171 ID_About
= wxID_ABOUT
,
173 ID_FORMAT_BOLD
= 100,
182 ID_FORMAT_ALIGN_LEFT
,
183 ID_FORMAT_ALIGN_CENTRE
,
184 ID_FORMAT_ALIGN_RIGHT
,
186 ID_FORMAT_INDENT_MORE
,
187 ID_FORMAT_INDENT_LESS
,
189 ID_FORMAT_PARAGRAPH_SPACING_MORE
,
190 ID_FORMAT_PARAGRAPH_SPACING_LESS
,
192 ID_FORMAT_LINE_SPACING_HALF
,
193 ID_FORMAT_LINE_SPACING_DOUBLE
,
194 ID_FORMAT_LINE_SPACING_SINGLE
,
197 ID_SWITCH_STYLE_SHEETS
,
200 ID_RICHTEXT_STYLE_LIST
,
201 ID_RICHTEXT_STYLE_COMBO
204 // ----------------------------------------------------------------------------
205 // event tables and other macros for wxWidgets
206 // ----------------------------------------------------------------------------
208 // the event tables connect the wxWidgets events with the functions (event
209 // handlers) which process them. It can be also done at run-time, but for the
210 // simple menu events like this the static method is much simpler.
211 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
212 EVT_MENU(ID_Quit
, MyFrame::OnQuit
)
213 EVT_MENU(ID_About
, MyFrame::OnAbout
)
215 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
216 EVT_MENU(wxID_SAVE
, MyFrame::OnSave
)
217 EVT_MENU(wxID_SAVEAS
, MyFrame::OnSaveAs
)
219 EVT_MENU(ID_FORMAT_BOLD
, MyFrame::OnBold
)
220 EVT_MENU(ID_FORMAT_ITALIC
, MyFrame::OnItalic
)
221 EVT_MENU(ID_FORMAT_UNDERLINE
, MyFrame::OnUnderline
)
223 EVT_UPDATE_UI(ID_FORMAT_BOLD
, MyFrame::OnUpdateBold
)
224 EVT_UPDATE_UI(ID_FORMAT_ITALIC
, MyFrame::OnUpdateItalic
)
225 EVT_UPDATE_UI(ID_FORMAT_UNDERLINE
, MyFrame::OnUpdateUnderline
)
227 EVT_MENU(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnAlignLeft
)
228 EVT_MENU(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnAlignCentre
)
229 EVT_MENU(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnAlignRight
)
231 EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnUpdateAlignLeft
)
232 EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnUpdateAlignCentre
)
233 EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnUpdateAlignRight
)
235 EVT_MENU(ID_FORMAT_FONT
, MyFrame::OnFont
)
236 EVT_MENU(ID_FORMAT_PARAGRAPH
, MyFrame::OnParagraph
)
237 EVT_MENU(ID_FORMAT_CONTENT
, MyFrame::OnFormat
)
238 EVT_UPDATE_UI(ID_FORMAT_CONTENT
, MyFrame::OnUpdateFormat
)
239 EVT_UPDATE_UI(ID_FORMAT_FONT
, MyFrame::OnUpdateFormat
)
240 EVT_UPDATE_UI(ID_FORMAT_PARAGRAPH
, MyFrame::OnUpdateFormat
)
241 EVT_MENU(ID_FORMAT_INDENT_MORE
, MyFrame::OnIndentMore
)
242 EVT_MENU(ID_FORMAT_INDENT_LESS
, MyFrame::OnIndentLess
)
244 EVT_MENU(ID_FORMAT_LINE_SPACING_HALF
, MyFrame::OnLineSpacingHalf
)
245 EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE
, MyFrame::OnLineSpacingSingle
)
246 EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE
, MyFrame::OnLineSpacingDouble
)
248 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE
, MyFrame::OnParagraphSpacingMore
)
249 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS
, MyFrame::OnParagraphSpacingLess
)
251 EVT_MENU(ID_INSERT_SYMBOL
, MyFrame::OnInsertSymbol
)
253 EVT_MENU(ID_VIEW_HTML
, MyFrame::OnViewHTML
)
254 EVT_MENU(ID_SWITCH_STYLE_SHEETS
, MyFrame::OnSwitchStyleSheets
)
257 // Create a new application object: this macro will allow wxWidgets to create
258 // the application object during program execution (it's better than using a
259 // static object for many reasons) and also implements the accessor function
260 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
264 // ============================================================================
266 // ============================================================================
268 // ----------------------------------------------------------------------------
269 // the application class
270 // ----------------------------------------------------------------------------
272 // 'Main program' equivalent: the program execution "starts" here
275 m_styleSheet
= new wxRichTextStyleSheet
;
279 // Add extra handlers (plain text is automatically added)
280 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler
);
281 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler
);
283 // Add image handlers
285 wxImage::AddHandler( new wxPNGHandler
);
289 wxImage::AddHandler( new wxJPEGHandler
);
293 wxImage::AddHandler( new wxGIFHandler
);
296 // create the main application window
297 MyFrame
*frame
= new MyFrame(_T("wxRichTextCtrl Sample"), wxID_ANY
, wxDefaultPosition
, wxSize(700, 600));
299 // and show it (the frames, unlike simple controls, are not shown when
300 // created initially)
303 // success: wxApp::OnRun() will be called which will enter the main message
304 // loop and the application will run. If we returned false here, the
305 // application would exit immediately.
315 void MyApp::CreateStyles()
319 wxFont
romanFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
320 wxFont
swissFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
);
322 wxRichTextParagraphStyleDefinition
* normalPara
= new wxRichTextParagraphStyleDefinition(wxT("Normal"));
323 wxRichTextAttr normalAttr
;
324 normalAttr
.SetFontFaceName(romanFont
.GetFaceName());
325 normalAttr
.SetFontSize(12);
326 // Let's set all attributes for this style
327 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
|
328 wxTEXT_ATTR_PARA_SPACING_BEFORE
|wxTEXT_ATTR_PARA_SPACING_AFTER
|wxTEXT_ATTR_LINE_SPACING
|
329 wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
);
330 normalPara
->SetStyle(normalAttr
);
332 m_styleSheet
->AddParagraphStyle(normalPara
);
334 wxRichTextParagraphStyleDefinition
* indentedPara
= new wxRichTextParagraphStyleDefinition(wxT("Indented"));
335 wxRichTextAttr indentedAttr
;
336 indentedAttr
.SetFontFaceName(romanFont
.GetFaceName());
337 indentedAttr
.SetFontSize(12);
338 indentedAttr
.SetLeftIndent(100, 0);
339 // We only want to affect indentation
340 indentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
341 indentedPara
->SetStyle(indentedAttr
);
343 m_styleSheet
->AddParagraphStyle(indentedPara
);
345 wxRichTextParagraphStyleDefinition
* indentedPara2
= new wxRichTextParagraphStyleDefinition(wxT("Red Bold Indented"));
346 wxRichTextAttr indentedAttr2
;
347 indentedAttr2
.SetFontFaceName(romanFont
.GetFaceName());
348 indentedAttr2
.SetFontSize(12);
349 indentedAttr2
.SetFontWeight(wxBOLD
);
350 indentedAttr2
.SetTextColour(*wxRED
);
351 indentedAttr2
.SetFontSize(12);
352 indentedAttr2
.SetLeftIndent(100, 0);
353 // We want to affect indentation, font and text colour
354 indentedAttr2
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
|wxTEXT_ATTR_FONT
|wxTEXT_ATTR_TEXT_COLOUR
);
355 indentedPara2
->SetStyle(indentedAttr2
);
357 m_styleSheet
->AddParagraphStyle(indentedPara2
);
359 wxRichTextParagraphStyleDefinition
* flIndentedPara
= new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
360 wxRichTextAttr flIndentedAttr
;
361 flIndentedAttr
.SetFontFaceName(swissFont
.GetFaceName());
362 flIndentedAttr
.SetFontSize(12);
363 flIndentedAttr
.SetLeftIndent(100, -100);
364 // We only want to affect indentation
365 flIndentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
366 flIndentedPara
->SetStyle(flIndentedAttr
);
368 m_styleSheet
->AddParagraphStyle(flIndentedPara
);
372 wxRichTextCharacterStyleDefinition
* boldDef
= new wxRichTextCharacterStyleDefinition(wxT("Bold"));
373 wxRichTextAttr boldAttr
;
374 boldAttr
.SetFontFaceName(romanFont
.GetFaceName());
375 boldAttr
.SetFontSize(12);
376 boldAttr
.SetFontWeight(wxBOLD
);
377 // We only want to affect boldness
378 boldAttr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
379 boldDef
->SetStyle(boldAttr
);
381 m_styleSheet
->AddCharacterStyle(boldDef
);
383 wxRichTextCharacterStyleDefinition
* italicDef
= new wxRichTextCharacterStyleDefinition(wxT("Italic"));
384 wxRichTextAttr italicAttr
;
385 italicAttr
.SetFontFaceName(romanFont
.GetFaceName());
386 italicAttr
.SetFontSize(12);
387 italicAttr
.SetFontStyle(wxITALIC
);
388 // We only want to affect italics
389 italicAttr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
390 italicDef
->SetStyle(italicAttr
);
392 m_styleSheet
->AddCharacterStyle(italicDef
);
394 wxRichTextCharacterStyleDefinition
* redDef
= new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
395 wxRichTextAttr redAttr
;
396 redAttr
.SetFontFaceName(romanFont
.GetFaceName());
397 redAttr
.SetFontSize(12);
398 redAttr
.SetFontWeight(wxBOLD
);
399 redAttr
.SetTextColour(*wxRED
);
400 // We only want to affect colour, weight and face
401 redAttr
.SetFlags(wxTEXT_ATTR_FONT_FACE
|wxTEXT_ATTR_FONT_WEIGHT
|wxTEXT_ATTR_TEXT_COLOUR
);
402 redDef
->SetStyle(redAttr
);
404 m_styleSheet
->AddCharacterStyle(redDef
);
407 // ----------------------------------------------------------------------------
409 // ----------------------------------------------------------------------------
412 MyFrame::MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
,
413 const wxSize
& size
, long style
)
414 : wxFrame(NULL
, id
, title
, pos
, size
, style
)
416 // set the frame icon
417 SetIcon(wxICON(sample
));
420 wxMenu
*fileMenu
= new wxMenu
;
422 // the "About" item should be in the help menu
423 wxMenu
*helpMenu
= new wxMenu
;
424 helpMenu
->Append(ID_About
, _T("&About...\tF1"), _T("Show about dialog"));
426 fileMenu
->Append(wxID_OPEN
, _T("&Open\tCtrl+O"), _T("Open a file"));
427 fileMenu
->Append(wxID_SAVE
, _T("&Save\tCtrl+S"), _T("Save a file"));
428 fileMenu
->Append(wxID_SAVEAS
, _T("&Save As...\tF12"), _T("Save to a new file"));
429 fileMenu
->AppendSeparator();
430 fileMenu
->Append(ID_VIEW_HTML
, _T("&View as HTML"), _T("View HTML"));
431 fileMenu
->AppendSeparator();
432 fileMenu
->Append(ID_Quit
, _T("E&xit\tAlt+X"), _T("Quit this program"));
434 wxMenu
* editMenu
= new wxMenu
;
435 editMenu
->Append(wxID_UNDO
, _("&Undo\tCtrl+Z"));
436 editMenu
->Append(wxID_REDO
, _("&Redo\tCtrl+Y"));
437 editMenu
->AppendSeparator();
438 editMenu
->Append(wxID_CUT
, _("Cu&t\tCtrl+X"));
439 editMenu
->Append(wxID_COPY
, _("&Copy\tCtrl+C"));
440 editMenu
->Append(wxID_PASTE
, _("&Paste\tCtrl+V"));
442 editMenu
->Append(wxID_CLEAR
, _("&Delete\tDel"));
444 editMenu
->AppendSeparator();
445 editMenu
->Append(wxID_SELECTALL
, _("Select A&ll\tCtrl+A"));
447 editMenu
->AppendSeparator();
448 editMenu
->Append(wxID_FIND
, _("&Find...\tCtrl+F"));
449 editMenu
->Append(stID_FIND_REPLACE
, _("&Replace...\tCtrl+R"));
452 wxMenu
* formatMenu
= new wxMenu
;
453 formatMenu
->AppendCheckItem(ID_FORMAT_BOLD
, _("&Bold\tCtrl+B"));
454 formatMenu
->AppendCheckItem(ID_FORMAT_ITALIC
, _("&Italic\tCtrl+I"));
455 formatMenu
->AppendCheckItem(ID_FORMAT_UNDERLINE
, _("&Underline\tCtrl+U"));
456 formatMenu
->AppendSeparator();
457 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_LEFT
, _("L&eft Align"));
458 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT
, _("&Right Align"));
459 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE
, _("&Centre"));
460 formatMenu
->AppendSeparator();
461 formatMenu
->Append(ID_FORMAT_INDENT_MORE
, _("Indent &More"));
462 formatMenu
->Append(ID_FORMAT_INDENT_LESS
, _("Indent &Less"));
463 formatMenu
->AppendSeparator();
464 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE
, _("Increase Paragraph &Spacing"));
465 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS
, _("Decrease &Paragraph Spacing"));
466 formatMenu
->AppendSeparator();
467 formatMenu
->Append(ID_FORMAT_LINE_SPACING_SINGLE
, _("Normal Line Spacing"));
468 formatMenu
->Append(ID_FORMAT_LINE_SPACING_HALF
, _("1.5 Line Spacing"));
469 formatMenu
->Append(ID_FORMAT_LINE_SPACING_DOUBLE
, _("Double Line Spacing"));
470 formatMenu
->AppendSeparator();
471 formatMenu
->Append(ID_FORMAT_FONT
, _("&Font..."));
472 formatMenu
->Append(ID_FORMAT_PARAGRAPH
, _("&Paragraph..."));
473 formatMenu
->Append(ID_FORMAT_CONTENT
, _("Font and Pa&ragraph...\tShift+Ctrl+F"));
474 formatMenu
->AppendSeparator();
475 formatMenu
->Append(ID_SWITCH_STYLE_SHEETS
, _("&Switch Style Sheets"));
477 wxMenu
* insertMenu
= new wxMenu
;
478 insertMenu
->Append(ID_INSERT_SYMBOL
, _("&Symbol...\tCtrl+I"));
480 // now append the freshly created menu to the menu bar...
481 wxMenuBar
*menuBar
= new wxMenuBar();
482 menuBar
->Append(fileMenu
, _T("&File"));
483 menuBar
->Append(editMenu
, _T("&Edit"));
484 menuBar
->Append(formatMenu
, _T("F&ormat"));
485 menuBar
->Append(insertMenu
, _T("&Insert"));
486 menuBar
->Append(helpMenu
, _T("&Help"));
488 // ... and attach this menu bar to the frame
491 // create a status bar just for fun (by default with 1 pane only)
492 // but don't create it on limited screen space (WinCE)
493 bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
499 SetStatusText(_T("Welcome to wxRichTextCtrl!"));
503 wxToolBar
* toolBar
= CreateToolBar();
505 toolBar
->AddTool(wxID_OPEN
, wxBitmap(open_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Open"));
506 toolBar
->AddTool(wxID_SAVEAS
, wxBitmap(save_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Save"));
507 toolBar
->AddSeparator();
508 toolBar
->AddTool(wxID_CUT
, wxBitmap(cut_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Cut"));
509 toolBar
->AddTool(wxID_COPY
, wxBitmap(copy_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Copy"));
510 toolBar
->AddTool(wxID_PASTE
, wxBitmap(paste_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Paste"));
511 toolBar
->AddSeparator();
512 toolBar
->AddTool(wxID_UNDO
, wxBitmap(undo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Undo"));
513 toolBar
->AddTool(wxID_REDO
, wxBitmap(redo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Redo"));
514 toolBar
->AddSeparator();
515 toolBar
->AddTool(ID_FORMAT_BOLD
, wxBitmap(bold_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Bold"));
516 toolBar
->AddTool(ID_FORMAT_ITALIC
, wxBitmap(italic_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Italic"));
517 toolBar
->AddTool(ID_FORMAT_UNDERLINE
, wxBitmap(underline_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Underline"));
518 toolBar
->AddSeparator();
519 toolBar
->AddTool(ID_FORMAT_ALIGN_LEFT
, wxBitmap(alignleft_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Left"));
520 toolBar
->AddTool(ID_FORMAT_ALIGN_CENTRE
, wxBitmap(centre_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Centre"));
521 toolBar
->AddTool(ID_FORMAT_ALIGN_RIGHT
, wxBitmap(alignright_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Right"));
522 toolBar
->AddSeparator();
523 toolBar
->AddTool(ID_FORMAT_INDENT_LESS
, wxBitmap(indentless_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent Less"));
524 toolBar
->AddTool(ID_FORMAT_INDENT_MORE
, wxBitmap(indentmore_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent More"));
525 toolBar
->AddSeparator();
526 toolBar
->AddTool(ID_FORMAT_FONT
, wxBitmap(font_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Font"));
528 wxRichTextStyleComboCtrl
* combo
= new wxRichTextStyleComboCtrl(toolBar
, ID_RICHTEXT_STYLE_COMBO
, wxDefaultPosition
, wxSize(200, -1));
529 toolBar
->AddControl(combo
);
533 wxSplitterWindow
* splitter
= new wxSplitterWindow(this, wxID_ANY
, wxDefaultPosition
, GetClientSize(), wxSP_NO_XP_THEME
|wxSP_3D
|wxSP_LIVE_UPDATE
);
535 wxFont textFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
536 wxFont boldFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxBOLD
);
537 wxFont italicFont
= wxFont(12, wxROMAN
, wxITALIC
, wxNORMAL
);
539 m_richTextCtrl
= new wxRichTextCtrl(splitter
, ID_RICHTEXT_CTRL
, wxEmptyString
, wxDefaultPosition
, wxSize(200, 200), wxVSCROLL
|wxHSCROLL
|wxNO_BORDER
|wxWANTS_CHARS
);
540 wxFont
font(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
542 m_richTextCtrl
->SetFont(font
);
544 m_richTextCtrl
->SetStyleSheet(wxGetApp().GetStyleSheet());
546 combo
->SetStyleSheet(wxGetApp().GetStyleSheet());
547 combo
->SetRichTextCtrl(m_richTextCtrl
);
548 combo
->UpdateStyles();
550 wxRichTextStyleListBox
* styleListBox
= new wxRichTextStyleListBox(splitter
, ID_RICHTEXT_STYLE_LIST
);
552 wxSize display
= wxGetDisplaySize();
553 if ( is_pda
&& ( display
.GetWidth() < display
.GetHeight() ) )
555 splitter
->SplitHorizontally(m_richTextCtrl
, styleListBox
);
559 splitter
->SplitVertically(m_richTextCtrl
, styleListBox
, 500);
562 splitter
->UpdateSize();
564 styleListBox
->SetStyleSheet(wxGetApp().GetStyleSheet());
565 styleListBox
->SetRichTextCtrl(m_richTextCtrl
);
566 styleListBox
->UpdateStyles();
568 wxRichTextCtrl
& r
= *m_richTextCtrl
;
571 r
.WriteText(wxT("One\nTwo\nThree\n"));
574 for (i
= 0; i
< 3; i
++)
577 wxRichTextParagraph
* para
= r
.GetBuffer().GetParagraphAtLine(i
);
580 wxLogDebug(wxT("Range for paragraph %d: %d -> %d"), i
, para
->GetRange().GetStart(), para
->GetRange().GetEnd());
586 r
.BeginSuppressUndo();
588 r
.BeginParagraphSpacing(0, 20);
590 r
.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE
);
594 r
.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
599 r
.WriteText(wxT("by Julian Smart"));
605 r
.WriteImage(wxBitmap(zebra_xpm
));
612 r
.WriteText(wxT("What can you do with this thing? "));
613 r
.WriteImage(wxBitmap(smiley_xpm
));
614 r
.WriteText(wxT(" Well, you can change text "));
616 r
.BeginTextColour(wxColour(255, 0, 0));
617 r
.WriteText(wxT("colour, like this red bit."));
620 r
.BeginTextColour(wxColour(0, 0, 255));
621 r
.WriteText(wxT(" And this blue bit."));
624 r
.WriteText(wxT(" Naturally you can make things "));
626 r
.WriteText(wxT("bold "));
629 r
.WriteText(wxT("or italic "));
632 r
.WriteText(wxT("or underlined."));
636 r
.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
639 r
.WriteText(wxT(" Next we'll show an indented paragraph."));
641 r
.BeginLeftIndent(60);
644 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."));
649 r
.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
651 r
.BeginLeftIndent(100, -40);
654 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."));
659 r
.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
661 r
.BeginNumberedBullet(1, 100, 60);
664 r
.WriteText(wxT("This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later."));
665 r
.EndNumberedBullet();
667 r
.BeginNumberedBullet(2, 100, 60);
670 r
.WriteText(wxT("This is my second item."));
671 r
.EndNumberedBullet();
675 r
.WriteText(wxT("The following paragraph is right-indented:"));
677 r
.BeginRightIndent(200);
680 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."));
685 r
.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
687 r
.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT
);
688 r
.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF
);
691 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."));
701 attr
.SetFlags(wxTEXT_ATTR_TABS
);
703 r
.SetDefaultStyle(attr
);
706 r
.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
709 r
.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
711 r
.BeginSymbolBullet(wxT('*'), 100, 60);
713 r
.WriteText(wxT("Compatibility with wxTextCtrl API"));
716 r
.BeginSymbolBullet(wxT('*'), 100, 60);
718 r
.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
721 r
.BeginSymbolBullet(wxT('*'), 100, 60);
723 r
.WriteText(wxT("XML loading and saving"));
726 r
.BeginSymbolBullet(wxT('*'), 100, 60);
728 r
.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
731 r
.BeginSymbolBullet(wxT('*'), 100, 60);
733 r
.WriteText(wxT("Clipboard copy and paste"));
736 r
.BeginSymbolBullet(wxT('*'), 100, 60);
738 r
.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
741 r
.BeginSymbolBullet(wxT('*'), 100, 60);
743 r
.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
748 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!"));
750 r
.EndParagraphSpacing();
759 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
761 // true is to force the frame to close
765 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
768 msg
.Printf( _T("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
769 wxMessageBox(msg
, _T("About wxRichTextCtrl Sample"), wxOK
| wxICON_INFORMATION
, this);
772 // Forward command events to the current rich text control, if any
773 bool MyFrame::ProcessEvent(wxEvent
& event
)
775 if (event
.IsCommandEvent() && !event
.IsKindOf(CLASSINFO(wxChildFocusEvent
)))
777 // Problem: we can get infinite recursion because the events
778 // climb back up to this frame, and repeat.
779 // Assume that command events don't cause another command event
780 // to be called, so we can rely on inCommand not being overwritten
782 static int s_eventType
= 0;
783 static wxWindowID s_id
= 0;
785 if (s_id
!= event
.GetId() && s_eventType
!= event
.GetEventType())
787 s_eventType
= event
.GetEventType();
788 s_id
= event
.GetId();
790 wxWindow
* focusWin
= wxFindFocusDescendant(this);
791 if (focusWin
&& focusWin
->ProcessEvent(event
))
808 return wxFrame::ProcessEvent(event
);
811 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
815 wxArrayInt fileTypes
;
817 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes
);
820 filter
+= wxT("All files (*.*)|*.*");
822 wxFileDialog
dialog(this,
823 _("Choose a filename"),
829 if (dialog
.ShowModal() == wxID_OK
)
831 wxString path
= dialog
.GetPath();
835 int filterIndex
= dialog
.GetFilterIndex();
836 int fileType
= (filterIndex
< (int) fileTypes
.GetCount())
837 ? fileTypes
[filterIndex
]
838 : wxRICHTEXT_TYPE_TEXT
;
839 m_richTextCtrl
->LoadFile(path
, fileType
);
844 void MyFrame::OnSave(wxCommandEvent
& event
)
846 if (m_richTextCtrl
->GetFilename().empty())
851 m_richTextCtrl
->SaveFile();
854 void MyFrame::OnSaveAs(wxCommandEvent
& WXUNUSED(event
))
856 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, true);
860 wxFileDialog
dialog(this,
861 _("Choose a filename"),
867 if (dialog
.ShowModal() == wxID_OK
)
869 wxString path
= dialog
.GetPath();
873 m_richTextCtrl
->SaveFile(path
);
878 void MyFrame::OnBold(wxCommandEvent
& WXUNUSED(event
))
880 m_richTextCtrl
->ApplyBoldToSelection();
883 void MyFrame::OnItalic(wxCommandEvent
& WXUNUSED(event
))
885 m_richTextCtrl
->ApplyItalicToSelection();
888 void MyFrame::OnUnderline(wxCommandEvent
& WXUNUSED(event
))
890 m_richTextCtrl
->ApplyUnderlineToSelection();
894 void MyFrame::OnUpdateBold(wxUpdateUIEvent
& event
)
896 event
.Check(m_richTextCtrl
->IsSelectionBold());
899 void MyFrame::OnUpdateItalic(wxUpdateUIEvent
& event
)
901 event
.Check(m_richTextCtrl
->IsSelectionItalics());
904 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent
& event
)
906 event
.Check(m_richTextCtrl
->IsSelectionUnderlined());
909 void MyFrame::OnAlignLeft(wxCommandEvent
& WXUNUSED(event
))
911 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT
);
914 void MyFrame::OnAlignCentre(wxCommandEvent
& WXUNUSED(event
))
916 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE
);
919 void MyFrame::OnAlignRight(wxCommandEvent
& WXUNUSED(event
))
921 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT
);
924 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent
& event
)
926 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT
));
929 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent
& event
)
931 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE
));
934 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent
& event
)
936 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT
));
939 void MyFrame::OnFont(wxCommandEvent
& WXUNUSED(event
))
941 wxRichTextRange range
;
942 if (m_richTextCtrl
->HasSelection())
943 range
= m_richTextCtrl
->GetSelectionRange();
945 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
947 int pages
= wxRICHTEXT_FORMAT_FONT
;
949 wxRichTextFormattingDialog
formatDlg(pages
, this);
950 formatDlg
.GetStyle(m_richTextCtrl
, range
);
952 if (formatDlg
.ShowModal() == wxID_OK
)
954 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
957 // Old method using wxFontDialog
959 if (!m_richTextCtrl
->HasSelection())
962 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
966 attr
.SetFlags(wxTEXT_ATTR_FONT
);
968 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
969 fontData
.SetInitialFont(attr
.GetFont());
971 wxFontDialog
dialog(this, fontData
);
972 if (dialog
.ShowModal() == wxID_OK
)
974 fontData
= dialog
.GetFontData();
975 attr
.SetFlags(wxTEXT_ATTR_FONT
);
976 attr
.SetFont(fontData
.GetChosenFont());
977 if (attr
.GetFont().Ok())
979 m_richTextCtrl
->SetStyle(range
, attr
);
985 void MyFrame::OnParagraph(wxCommandEvent
& WXUNUSED(event
))
987 wxRichTextRange range
;
988 if (m_richTextCtrl
->HasSelection())
989 range
= m_richTextCtrl
->GetSelectionRange();
991 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
993 int pages
= wxRICHTEXT_FORMAT_INDENTS_SPACING
|wxRICHTEXT_FORMAT_TABS
|wxRICHTEXT_FORMAT_BULLETS
;
995 wxRichTextFormattingDialog
formatDlg(pages
, this);
996 formatDlg
.GetStyle(m_richTextCtrl
, range
);
998 if (formatDlg
.ShowModal() == wxID_OK
)
1000 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
1004 void MyFrame::OnFormat(wxCommandEvent
& WXUNUSED(event
))
1006 wxRichTextRange range
;
1007 if (m_richTextCtrl
->HasSelection())
1008 range
= m_richTextCtrl
->GetSelectionRange();
1010 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
1012 int pages
= wxRICHTEXT_FORMAT_FONT
|wxRICHTEXT_FORMAT_INDENTS_SPACING
|wxRICHTEXT_FORMAT_TABS
|wxRICHTEXT_FORMAT_BULLETS
;
1014 wxRichTextFormattingDialog
formatDlg(pages
, this);
1015 formatDlg
.GetStyle(m_richTextCtrl
, range
);
1017 if (formatDlg
.ShowModal() == wxID_OK
)
1019 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
1023 void MyFrame::OnUpdateFormat(wxUpdateUIEvent
& event
)
1025 event
.Enable(m_richTextCtrl
->HasSelection());
1028 void MyFrame::OnIndentMore(wxCommandEvent
& WXUNUSED(event
))
1031 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1033 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1035 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1036 if (m_richTextCtrl
->HasSelection())
1037 range
= m_richTextCtrl
->GetSelectionRange();
1039 wxFontData fontData
;
1040 attr
.SetLeftIndent(attr
.GetLeftIndent() + 100);
1042 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1043 m_richTextCtrl
->SetStyle(range
, attr
);
1047 void MyFrame::OnIndentLess(wxCommandEvent
& WXUNUSED(event
))
1050 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1052 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1054 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1055 if (m_richTextCtrl
->HasSelection())
1056 range
= m_richTextCtrl
->GetSelectionRange();
1058 if (attr
.GetLeftIndent() >= 100)
1060 wxFontData fontData
;
1061 attr
.SetLeftIndent(attr
.GetLeftIndent() - 100);
1063 m_richTextCtrl
->SetStyle(range
, attr
);
1068 void MyFrame::OnLineSpacingHalf(wxCommandEvent
& WXUNUSED(event
))
1071 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1073 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1075 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1076 if (m_richTextCtrl
->HasSelection())
1077 range
= m_richTextCtrl
->GetSelectionRange();
1079 wxFontData fontData
;
1080 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1081 attr
.SetLineSpacing(15);
1083 m_richTextCtrl
->SetStyle(range
, attr
);
1087 void MyFrame::OnLineSpacingDouble(wxCommandEvent
& WXUNUSED(event
))
1090 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1092 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1094 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1095 if (m_richTextCtrl
->HasSelection())
1096 range
= m_richTextCtrl
->GetSelectionRange();
1098 wxFontData fontData
;
1099 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1100 attr
.SetLineSpacing(20);
1102 m_richTextCtrl
->SetStyle(range
, attr
);
1106 void MyFrame::OnLineSpacingSingle(wxCommandEvent
& WXUNUSED(event
))
1109 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1111 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1113 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1114 if (m_richTextCtrl
->HasSelection())
1115 range
= m_richTextCtrl
->GetSelectionRange();
1117 wxFontData fontData
;
1118 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1119 attr
.SetLineSpacing(0); // Can also use 10
1121 m_richTextCtrl
->SetStyle(range
, attr
);
1125 void MyFrame::OnParagraphSpacingMore(wxCommandEvent
& WXUNUSED(event
))
1128 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1130 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1132 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1133 if (m_richTextCtrl
->HasSelection())
1134 range
= m_richTextCtrl
->GetSelectionRange();
1136 wxFontData fontData
;
1137 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() + 20);
1139 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1140 m_richTextCtrl
->SetStyle(range
, attr
);
1144 void MyFrame::OnParagraphSpacingLess(wxCommandEvent
& WXUNUSED(event
))
1147 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1149 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1151 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1152 if (m_richTextCtrl
->HasSelection())
1153 range
= m_richTextCtrl
->GetSelectionRange();
1155 if (attr
.GetParagraphSpacingAfter() >= 20)
1157 wxFontData fontData
;
1158 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() - 20);
1160 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1161 m_richTextCtrl
->SetStyle(range
, attr
);
1166 void MyFrame::OnViewHTML(wxCommandEvent
& WXUNUSED(event
))
1168 wxDialog
dialog(this, wxID_ANY
, _("HTML"), wxDefaultPosition
, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE
);
1170 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
1171 dialog
.SetSizer(boxSizer
);
1173 wxHtmlWindow
* win
= new wxHtmlWindow(& dialog
, wxID_ANY
, wxDefaultPosition
, wxSize(500, 400), wxSUNKEN_BORDER
);
1174 boxSizer
->Add(win
, 1, wxALL
, 5);
1176 wxButton
* cancelButton
= new wxButton(& dialog
, wxID_CANCEL
, wxT("&Close"));
1177 boxSizer
->Add(cancelButton
, 0, wxALL
|wxCENTRE
, 5);
1180 wxStringOutputStream
strStream(& text
);
1182 wxRichTextHTMLHandler htmlHandler
;
1183 if (htmlHandler
.SaveFile(& m_richTextCtrl
->GetBuffer(), strStream
))
1188 boxSizer
->Fit(& dialog
);
1193 // Demonstrates how you can change the style sheets and have the changes
1194 // reflected in the control content without wiping out character formatting.
1196 void MyFrame::OnSwitchStyleSheets(wxCommandEvent
& WXUNUSED(event
))
1198 static wxRichTextStyleSheet
* gs_AlternateStyleSheet
= NULL
;
1200 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1201 wxRichTextStyleListBox
* styleList
= (wxRichTextStyleListBox
*) FindWindow(ID_RICHTEXT_STYLE_LIST
);
1202 wxRichTextStyleComboCtrl
* styleCombo
= (wxRichTextStyleComboCtrl
*) FindWindow(ID_RICHTEXT_STYLE_COMBO
);
1204 wxRichTextStyleSheet
* sheet
= ctrl
->GetStyleSheet();
1206 // One-time creation of an alternate style sheet
1207 if (!gs_AlternateStyleSheet
)
1209 gs_AlternateStyleSheet
= new wxRichTextStyleSheet(*sheet
);
1211 // Make some modifications
1212 for (int i
= 0; i
< (int) gs_AlternateStyleSheet
->GetParagraphStyleCount(); i
++)
1214 wxRichTextParagraphStyleDefinition
* def
= gs_AlternateStyleSheet
->GetParagraphStyle(i
);
1216 if (def
->GetStyle().HasTextColour())
1217 def
->GetStyle().SetTextColour(*wxBLUE
);
1219 if (def
->GetStyle().HasAlignment())
1221 if (def
->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1222 def
->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1223 else if (def
->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1224 def
->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1226 if (def
->GetStyle().HasLeftIndent())
1228 def
->GetStyle().SetLeftIndent(def
->GetStyle().GetLeftIndent() * 2);
1234 wxRichTextStyleSheet
* tmp
= gs_AlternateStyleSheet
;
1235 gs_AlternateStyleSheet
= sheet
;
1238 ctrl
->SetStyleSheet(sheet
);
1239 ctrl
->ApplyStyleSheet(sheet
); // Makes the control reflect the new style definitions
1241 styleList
->SetStyleSheet(sheet
);
1242 styleList
->UpdateStyles();
1244 styleCombo
->SetStyleSheet(sheet
);
1245 styleCombo
->UpdateStyles();
1248 void MyFrame::OnInsertSymbol(wxCommandEvent
& WXUNUSED(event
))
1250 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1253 attr
.SetFlags(wxTEXT_ATTR_FONT
);
1254 ctrl
->GetStyle(ctrl
->GetInsertionPoint(), attr
);
1256 wxString currentFontName
;
1257 if (attr
.HasFont() && attr
.GetFont().Ok())
1258 currentFontName
= attr
.GetFont().GetFaceName();
1260 // Don't set the initial font in the dialog (so the user is choosing
1261 // 'normal text', i.e. the current font) but do tell the dialog
1262 // what 'normal text' is.
1264 wxSymbolPickerDialog
dlg(wxT("*"), wxEmptyString
, currentFontName
, this);
1266 if (dlg
.ShowModal() == wxID_OK
)
1268 if (dlg
.HasSelection())
1270 long insertionPoint
= ctrl
->GetInsertionPoint();
1272 ctrl
->WriteText(dlg
.GetSymbol());
1274 if (!dlg
.UseNormalFont())
1276 wxFont
font(attr
.GetFont());
1277 font
.SetFaceName(dlg
.GetNormalTextFontName());
1279 ctrl
->SetStyle(insertionPoint
, insertionPoint
+1, attr
);