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/filesys.h"
40 #include "wx/fs_mem.h"
44 #include "wx/cshelp.h"
48 #include "../sample.xpm"
51 #include "bitmaps/smiley.xpm"
52 // #include "bitmaps/idea.xpm"
53 #include "bitmaps/zebra.xpm"
55 #include "bitmaps/open.xpm"
56 #include "bitmaps/save.xpm"
57 #include "bitmaps/copy.xpm"
58 #include "bitmaps/cut.xpm"
59 #include "bitmaps/paste.xpm"
60 #include "bitmaps/undo.xpm"
61 #include "bitmaps/redo.xpm"
62 #include "bitmaps/bold.xpm"
63 #include "bitmaps/italic.xpm"
64 #include "bitmaps/underline.xpm"
66 #include "bitmaps/alignleft.xpm"
67 #include "bitmaps/alignright.xpm"
68 #include "bitmaps/centre.xpm"
69 #include "bitmaps/font.xpm"
70 #include "bitmaps/indentless.xpm"
71 #include "bitmaps/indentmore.xpm"
73 #include "wx/richtext/richtextctrl.h"
74 #include "wx/richtext/richtextstyles.h"
75 #include "wx/richtext/richtextxml.h"
76 #include "wx/richtext/richtexthtml.h"
77 #include "wx/richtext/richtextformatdlg.h"
78 #include "wx/richtext/richtextsymboldlg.h"
79 #include "wx/richtext/richtextstyledlg.h"
80 #include "wx/richtext/richtextprint.h"
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // Define a new application type, each program should derive a class from wxApp
91 class MyApp
: public wxApp
94 // override base class virtuals
95 // ----------------------------
97 // this one is called on application startup and is a good place for the app
98 // initialization (doing it here and not in the ctor allows to have an error
99 // return: if OnInit() returns false, the application terminates)
100 virtual bool OnInit();
101 virtual int OnExit();
105 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
106 wxRichTextPrinting
* GetPrinting() const { return m_printing
; }
108 wxRichTextStyleSheet
* m_styleSheet
;
109 wxRichTextPrinting
* m_printing
;
112 // Define a new frame type: this is going to be our main frame
113 class MyFrame
: public wxFrame
117 MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
= wxDefaultPosition
,
118 const wxSize
& size
= wxDefaultSize
, long style
= wxDEFAULT_FRAME_STYLE
);
120 // event handlers (these functions should _not_ be virtual)
121 void OnQuit(wxCommandEvent
& event
);
122 void OnAbout(wxCommandEvent
& event
);
124 void OnOpen(wxCommandEvent
& event
);
125 void OnSave(wxCommandEvent
& event
);
126 void OnSaveAs(wxCommandEvent
& event
);
128 void OnBold(wxCommandEvent
& event
);
129 void OnItalic(wxCommandEvent
& event
);
130 void OnUnderline(wxCommandEvent
& event
);
132 void OnUpdateBold(wxUpdateUIEvent
& event
);
133 void OnUpdateItalic(wxUpdateUIEvent
& event
);
134 void OnUpdateUnderline(wxUpdateUIEvent
& event
);
136 void OnAlignLeft(wxCommandEvent
& event
);
137 void OnAlignCentre(wxCommandEvent
& event
);
138 void OnAlignRight(wxCommandEvent
& event
);
140 void OnUpdateAlignLeft(wxUpdateUIEvent
& event
);
141 void OnUpdateAlignCentre(wxUpdateUIEvent
& event
);
142 void OnUpdateAlignRight(wxUpdateUIEvent
& event
);
144 void OnIndentMore(wxCommandEvent
& event
);
145 void OnIndentLess(wxCommandEvent
& event
);
147 void OnFont(wxCommandEvent
& event
);
148 void OnParagraph(wxCommandEvent
& event
);
149 void OnFormat(wxCommandEvent
& event
);
150 void OnUpdateFormat(wxUpdateUIEvent
& event
);
152 void OnInsertSymbol(wxCommandEvent
& event
);
154 void OnLineSpacingHalf(wxCommandEvent
& event
);
155 void OnLineSpacingDouble(wxCommandEvent
& event
);
156 void OnLineSpacingSingle(wxCommandEvent
& event
);
158 void OnParagraphSpacingMore(wxCommandEvent
& event
);
159 void OnParagraphSpacingLess(wxCommandEvent
& event
);
161 void OnNumberList(wxCommandEvent
& event
);
162 void OnBulletsAndNumbering(wxCommandEvent
& event
);
163 void OnItemizeList(wxCommandEvent
& event
);
164 void OnRenumberList(wxCommandEvent
& event
);
165 void OnPromoteList(wxCommandEvent
& event
);
166 void OnDemoteList(wxCommandEvent
& event
);
167 void OnClearList(wxCommandEvent
& event
);
169 void OnReload(wxCommandEvent
& event
);
171 void OnViewHTML(wxCommandEvent
& event
);
173 void OnSwitchStyleSheets(wxCommandEvent
& event
);
174 void OnManageStyles(wxCommandEvent
& event
);
176 void OnInsertURL(wxCommandEvent
& event
);
177 void OnURL(wxTextUrlEvent
& event
);
178 void OnStyleSheetReplacing(wxRichTextEvent
& event
);
180 void OnPrint(wxCommandEvent
& event
);
181 void OnPreview(wxCommandEvent
& event
);
182 void OnPageSetup(wxCommandEvent
& event
);
186 // Forward command events to the current rich text control, if any
187 bool ProcessEvent(wxEvent
& event
);
190 void WriteInitialText();
193 // any class wishing to process wxWidgets events must use this macro
194 DECLARE_EVENT_TABLE()
196 wxRichTextCtrl
* m_richTextCtrl
;
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 // IDs for the controls and the menu commands
208 ID_About
= wxID_ABOUT
,
210 ID_FORMAT_BOLD
= 100,
222 ID_FORMAT_ALIGN_LEFT
,
223 ID_FORMAT_ALIGN_CENTRE
,
224 ID_FORMAT_ALIGN_RIGHT
,
226 ID_FORMAT_INDENT_MORE
,
227 ID_FORMAT_INDENT_LESS
,
229 ID_FORMAT_PARAGRAPH_SPACING_MORE
,
230 ID_FORMAT_PARAGRAPH_SPACING_LESS
,
232 ID_FORMAT_LINE_SPACING_HALF
,
233 ID_FORMAT_LINE_SPACING_DOUBLE
,
234 ID_FORMAT_LINE_SPACING_SINGLE
,
236 ID_FORMAT_NUMBER_LIST
,
237 ID_FORMAT_BULLETS_AND_NUMBERING
,
238 ID_FORMAT_ITEMIZE_LIST
,
239 ID_FORMAT_RENUMBER_LIST
,
240 ID_FORMAT_PROMOTE_LIST
,
241 ID_FORMAT_DEMOTE_LIST
,
242 ID_FORMAT_CLEAR_LIST
,
245 ID_SWITCH_STYLE_SHEETS
,
253 ID_RICHTEXT_STYLE_LIST
,
254 ID_RICHTEXT_STYLE_COMBO
257 // ----------------------------------------------------------------------------
258 // event tables and other macros for wxWidgets
259 // ----------------------------------------------------------------------------
261 // the event tables connect the wxWidgets events with the functions (event
262 // handlers) which process them. It can be also done at run-time, but for the
263 // simple menu events like this the static method is much simpler.
264 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
265 EVT_MENU(ID_Quit
, MyFrame::OnQuit
)
266 EVT_MENU(ID_About
, MyFrame::OnAbout
)
268 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
269 EVT_MENU(wxID_SAVE
, MyFrame::OnSave
)
270 EVT_MENU(wxID_SAVEAS
, MyFrame::OnSaveAs
)
272 EVT_MENU(ID_FORMAT_BOLD
, MyFrame::OnBold
)
273 EVT_MENU(ID_FORMAT_ITALIC
, MyFrame::OnItalic
)
274 EVT_MENU(ID_FORMAT_UNDERLINE
, MyFrame::OnUnderline
)
276 EVT_UPDATE_UI(ID_FORMAT_BOLD
, MyFrame::OnUpdateBold
)
277 EVT_UPDATE_UI(ID_FORMAT_ITALIC
, MyFrame::OnUpdateItalic
)
278 EVT_UPDATE_UI(ID_FORMAT_UNDERLINE
, MyFrame::OnUpdateUnderline
)
280 EVT_MENU(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnAlignLeft
)
281 EVT_MENU(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnAlignCentre
)
282 EVT_MENU(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnAlignRight
)
284 EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnUpdateAlignLeft
)
285 EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnUpdateAlignCentre
)
286 EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnUpdateAlignRight
)
288 EVT_MENU(ID_FORMAT_FONT
, MyFrame::OnFont
)
289 EVT_MENU(ID_FORMAT_PARAGRAPH
, MyFrame::OnParagraph
)
290 EVT_MENU(ID_FORMAT_CONTENT
, MyFrame::OnFormat
)
291 EVT_UPDATE_UI(ID_FORMAT_CONTENT
, MyFrame::OnUpdateFormat
)
292 EVT_UPDATE_UI(ID_FORMAT_FONT
, MyFrame::OnUpdateFormat
)
293 EVT_UPDATE_UI(ID_FORMAT_PARAGRAPH
, MyFrame::OnUpdateFormat
)
294 EVT_MENU(ID_FORMAT_INDENT_MORE
, MyFrame::OnIndentMore
)
295 EVT_MENU(ID_FORMAT_INDENT_LESS
, MyFrame::OnIndentLess
)
297 EVT_MENU(ID_FORMAT_LINE_SPACING_HALF
, MyFrame::OnLineSpacingHalf
)
298 EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE
, MyFrame::OnLineSpacingSingle
)
299 EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE
, MyFrame::OnLineSpacingDouble
)
301 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE
, MyFrame::OnParagraphSpacingMore
)
302 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS
, MyFrame::OnParagraphSpacingLess
)
304 EVT_MENU(ID_RELOAD
, MyFrame::OnReload
)
306 EVT_MENU(ID_INSERT_SYMBOL
, MyFrame::OnInsertSymbol
)
307 EVT_MENU(ID_INSERT_URL
, MyFrame::OnInsertURL
)
309 EVT_MENU(ID_FORMAT_NUMBER_LIST
, MyFrame::OnNumberList
)
310 EVT_MENU(ID_FORMAT_BULLETS_AND_NUMBERING
, MyFrame::OnBulletsAndNumbering
)
311 EVT_MENU(ID_FORMAT_ITEMIZE_LIST
, MyFrame::OnItemizeList
)
312 EVT_MENU(ID_FORMAT_RENUMBER_LIST
, MyFrame::OnRenumberList
)
313 EVT_MENU(ID_FORMAT_PROMOTE_LIST
, MyFrame::OnPromoteList
)
314 EVT_MENU(ID_FORMAT_DEMOTE_LIST
, MyFrame::OnDemoteList
)
315 EVT_MENU(ID_FORMAT_CLEAR_LIST
, MyFrame::OnClearList
)
317 EVT_MENU(ID_VIEW_HTML
, MyFrame::OnViewHTML
)
318 EVT_MENU(ID_SWITCH_STYLE_SHEETS
, MyFrame::OnSwitchStyleSheets
)
319 EVT_MENU(ID_MANAGE_STYLES
, MyFrame::OnManageStyles
)
321 EVT_MENU(ID_PRINT
, MyFrame::OnPrint
)
322 EVT_MENU(ID_PREVIEW
, MyFrame::OnPreview
)
323 EVT_MENU(ID_PAGE_SETUP
, MyFrame::OnPageSetup
)
325 EVT_TEXT_URL(wxID_ANY
, MyFrame::OnURL
)
326 EVT_RICHTEXT_STYLESHEET_REPLACING(wxID_ANY
, MyFrame::OnStyleSheetReplacing
)
329 // Create a new application object: this macro will allow wxWidgets to create
330 // the application object during program execution (it's better than using a
331 // static object for many reasons) and also implements the accessor function
332 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
336 // ============================================================================
338 // ============================================================================
340 // ----------------------------------------------------------------------------
341 // the application class
342 // ----------------------------------------------------------------------------
344 // 'Main program' equivalent: the program execution "starts" here
347 if ( !wxApp::OnInit() )
351 wxHelpProvider::Set(new wxSimpleHelpProvider
);
354 m_styleSheet
= new wxRichTextStyleSheet
;
355 m_printing
= new wxRichTextPrinting(wxT("Test Document"));
357 m_printing
->SetFooterText(wxT("@TITLE@"), wxRICHTEXT_PAGE_ALL
, wxRICHTEXT_PAGE_CENTRE
);
358 m_printing
->SetFooterText(wxT("Page @PAGENUM@"), wxRICHTEXT_PAGE_ALL
, wxRICHTEXT_PAGE_RIGHT
);
362 // Add extra handlers (plain text is automatically added)
363 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler
);
364 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler
);
366 // Add image handlers
368 wxImage::AddHandler( new wxPNGHandler
);
372 wxImage::AddHandler( new wxJPEGHandler
);
376 wxImage::AddHandler( new wxGIFHandler
);
380 wxFileSystem::AddHandler( new wxMemoryFSHandler
);
383 // create the main application window
384 MyFrame
*frame
= new MyFrame(wxT("wxRichTextCtrl Sample"), wxID_ANY
, wxDefaultPosition
, wxSize(700, 600));
386 m_printing
->SetParentWindow(frame
);
388 // and show it (the frames, unlike simple controls, are not shown when
389 // created initially)
392 // success: wxApp::OnRun() will be called which will enter the main message
393 // loop and the application will run. If we returned false here, the
394 // application would exit immediately.
406 void MyApp::CreateStyles()
410 wxFont
romanFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
411 wxFont
swissFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
);
413 wxRichTextParagraphStyleDefinition
* normalPara
= new wxRichTextParagraphStyleDefinition(wxT("Normal"));
414 wxRichTextAttr normalAttr
;
415 normalAttr
.SetFontFaceName(romanFont
.GetFaceName());
416 normalAttr
.SetFontSize(12);
417 // Let's set all attributes for this style
418 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
|
419 wxTEXT_ATTR_PARA_SPACING_BEFORE
|wxTEXT_ATTR_PARA_SPACING_AFTER
|wxTEXT_ATTR_LINE_SPACING
|
420 wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
);
421 normalPara
->SetStyle(normalAttr
);
423 m_styleSheet
->AddParagraphStyle(normalPara
);
425 wxRichTextParagraphStyleDefinition
* indentedPara
= new wxRichTextParagraphStyleDefinition(wxT("Indented"));
426 wxRichTextAttr indentedAttr
;
427 indentedAttr
.SetFontFaceName(romanFont
.GetFaceName());
428 indentedAttr
.SetFontSize(12);
429 indentedAttr
.SetLeftIndent(100, 0);
430 // We only want to affect indentation
431 indentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
432 indentedPara
->SetStyle(indentedAttr
);
434 m_styleSheet
->AddParagraphStyle(indentedPara
);
436 wxRichTextParagraphStyleDefinition
* indentedPara2
= new wxRichTextParagraphStyleDefinition(wxT("Red Bold Indented"));
437 wxRichTextAttr indentedAttr2
;
438 indentedAttr2
.SetFontFaceName(romanFont
.GetFaceName());
439 indentedAttr2
.SetFontSize(12);
440 indentedAttr2
.SetFontWeight(wxFONTWEIGHT_BOLD
);
441 indentedAttr2
.SetTextColour(*wxRED
);
442 indentedAttr2
.SetFontSize(12);
443 indentedAttr2
.SetLeftIndent(100, 0);
444 // We want to affect indentation, font and text colour
445 indentedAttr2
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
|wxTEXT_ATTR_FONT
|wxTEXT_ATTR_TEXT_COLOUR
);
446 indentedPara2
->SetStyle(indentedAttr2
);
448 m_styleSheet
->AddParagraphStyle(indentedPara2
);
450 wxRichTextParagraphStyleDefinition
* flIndentedPara
= new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
451 wxRichTextAttr flIndentedAttr
;
452 flIndentedAttr
.SetFontFaceName(swissFont
.GetFaceName());
453 flIndentedAttr
.SetFontSize(12);
454 flIndentedAttr
.SetLeftIndent(100, -100);
455 // We only want to affect indentation
456 flIndentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
457 flIndentedPara
->SetStyle(flIndentedAttr
);
459 m_styleSheet
->AddParagraphStyle(flIndentedPara
);
463 wxRichTextCharacterStyleDefinition
* boldDef
= new wxRichTextCharacterStyleDefinition(wxT("Bold"));
464 wxRichTextAttr boldAttr
;
465 boldAttr
.SetFontFaceName(romanFont
.GetFaceName());
466 boldAttr
.SetFontSize(12);
467 boldAttr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
468 // We only want to affect boldness
469 boldAttr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
470 boldDef
->SetStyle(boldAttr
);
472 m_styleSheet
->AddCharacterStyle(boldDef
);
474 wxRichTextCharacterStyleDefinition
* italicDef
= new wxRichTextCharacterStyleDefinition(wxT("Italic"));
475 wxRichTextAttr italicAttr
;
476 italicAttr
.SetFontFaceName(romanFont
.GetFaceName());
477 italicAttr
.SetFontSize(12);
478 italicAttr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
479 // We only want to affect italics
480 italicAttr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
481 italicDef
->SetStyle(italicAttr
);
483 m_styleSheet
->AddCharacterStyle(italicDef
);
485 wxRichTextCharacterStyleDefinition
* redDef
= new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
486 wxRichTextAttr redAttr
;
487 redAttr
.SetFontFaceName(romanFont
.GetFaceName());
488 redAttr
.SetFontSize(12);
489 redAttr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
490 redAttr
.SetTextColour(*wxRED
);
491 // We only want to affect colour, weight and face
492 redAttr
.SetFlags(wxTEXT_ATTR_FONT_FACE
|wxTEXT_ATTR_FONT_WEIGHT
|wxTEXT_ATTR_TEXT_COLOUR
);
493 redDef
->SetStyle(redAttr
);
495 m_styleSheet
->AddCharacterStyle(redDef
);
497 wxRichTextListStyleDefinition
* bulletList
= new wxRichTextListStyleDefinition(wxT("Bullet List 1"));
499 for (i
= 0; i
< 10; i
++)
503 bulletText
= wxT("standard/circle");
505 bulletText
= wxT("standard/square");
507 bulletText
= wxT("standard/circle");
509 bulletText
= wxT("standard/square");
511 bulletText
= wxT("standard/circle");
513 bulletList
->SetAttributes(i
, (i
+1)*60, 60, wxTEXT_ATTR_BULLET_STYLE_STANDARD
, bulletText
);
516 m_styleSheet
->AddListStyle(bulletList
);
518 wxRichTextListStyleDefinition
* numberedList
= new wxRichTextListStyleDefinition(wxT("Numbered List 1"));
519 for (i
= 0; i
< 10; i
++)
523 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
;
525 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
;
527 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
;
529 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
;
531 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
;
533 numberStyle
|= wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT
;
535 numberedList
->SetAttributes(i
, (i
+1)*60, 60, numberStyle
);
538 m_styleSheet
->AddListStyle(numberedList
);
540 wxRichTextListStyleDefinition
* outlineList
= new wxRichTextListStyleDefinition(wxT("Outline List 1"));
541 for (i
= 0; i
< 10; i
++)
545 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_OUTLINE
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
;
547 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
;
549 outlineList
->SetAttributes(i
, (i
+1)*120, 120, numberStyle
);
552 m_styleSheet
->AddListStyle(outlineList
);
555 // ----------------------------------------------------------------------------
557 // ----------------------------------------------------------------------------
560 MyFrame::MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
,
561 const wxSize
& size
, long style
)
562 : wxFrame(NULL
, id
, title
, pos
, size
, style
)
564 // set the frame icon
565 SetIcon(wxICON(sample
));
568 wxMenu
*fileMenu
= new wxMenu
;
570 // the "About" item should be in the help menu
571 wxMenu
*helpMenu
= new wxMenu
;
572 helpMenu
->Append(ID_About
, wxT("&About...\tF1"), wxT("Show about dialog"));
574 fileMenu
->Append(wxID_OPEN
, wxT("&Open\tCtrl+O"), wxT("Open a file"));
575 fileMenu
->Append(wxID_SAVE
, wxT("&Save\tCtrl+S"), wxT("Save a file"));
576 fileMenu
->Append(wxID_SAVEAS
, wxT("&Save As...\tF12"), wxT("Save to a new file"));
577 fileMenu
->AppendSeparator();
578 fileMenu
->Append(ID_RELOAD
, wxT("&Reload Text\tF2"), wxT("Reload the initial text"));
579 fileMenu
->AppendSeparator();
580 fileMenu
->Append(ID_PAGE_SETUP
, wxT("Page Set&up..."), wxT("Page setup"));
581 fileMenu
->Append(ID_PRINT
, wxT("&Print...\tCtrl+P"), wxT("Print"));
582 fileMenu
->Append(ID_PREVIEW
, wxT("Print Pre&view"), wxT("Print preview"));
583 fileMenu
->AppendSeparator();
584 fileMenu
->Append(ID_VIEW_HTML
, wxT("&View as HTML"), wxT("View HTML"));
585 fileMenu
->AppendSeparator();
586 fileMenu
->Append(ID_Quit
, wxT("E&xit\tAlt+X"), wxT("Quit this program"));
588 wxMenu
* editMenu
= new wxMenu
;
589 editMenu
->Append(wxID_UNDO
, _("&Undo\tCtrl+Z"));
590 editMenu
->Append(wxID_REDO
, _("&Redo\tCtrl+Y"));
591 editMenu
->AppendSeparator();
592 editMenu
->Append(wxID_CUT
, _("Cu&t\tCtrl+X"));
593 editMenu
->Append(wxID_COPY
, _("&Copy\tCtrl+C"));
594 editMenu
->Append(wxID_PASTE
, _("&Paste\tCtrl+V"));
596 editMenu
->AppendSeparator();
597 editMenu
->Append(wxID_SELECTALL
, _("Select A&ll\tCtrl+A"));
599 editMenu
->AppendSeparator();
600 editMenu
->Append(wxID_FIND
, _("&Find...\tCtrl+F"));
601 editMenu
->Append(stID_FIND_REPLACE
, _("&Replace...\tCtrl+R"));
604 wxMenu
* formatMenu
= new wxMenu
;
605 formatMenu
->AppendCheckItem(ID_FORMAT_BOLD
, _("&Bold\tCtrl+B"));
606 formatMenu
->AppendCheckItem(ID_FORMAT_ITALIC
, _("&Italic\tCtrl+I"));
607 formatMenu
->AppendCheckItem(ID_FORMAT_UNDERLINE
, _("&Underline\tCtrl+U"));
608 formatMenu
->AppendSeparator();
609 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_LEFT
, _("L&eft Align"));
610 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT
, _("&Right Align"));
611 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE
, _("&Centre"));
612 formatMenu
->AppendSeparator();
613 formatMenu
->Append(ID_FORMAT_INDENT_MORE
, _("Indent &More"));
614 formatMenu
->Append(ID_FORMAT_INDENT_LESS
, _("Indent &Less"));
615 formatMenu
->AppendSeparator();
616 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE
, _("Increase Paragraph &Spacing"));
617 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS
, _("Decrease &Paragraph Spacing"));
618 formatMenu
->AppendSeparator();
619 formatMenu
->Append(ID_FORMAT_LINE_SPACING_SINGLE
, _("Normal Line Spacing"));
620 formatMenu
->Append(ID_FORMAT_LINE_SPACING_HALF
, _("1.5 Line Spacing"));
621 formatMenu
->Append(ID_FORMAT_LINE_SPACING_DOUBLE
, _("Double Line Spacing"));
622 formatMenu
->AppendSeparator();
623 formatMenu
->Append(ID_FORMAT_FONT
, _("&Font..."));
624 formatMenu
->Append(ID_FORMAT_PARAGRAPH
, _("&Paragraph..."));
625 formatMenu
->Append(ID_FORMAT_CONTENT
, _("Font and Pa&ragraph...\tShift+Ctrl+F"));
626 formatMenu
->AppendSeparator();
627 formatMenu
->Append(ID_SWITCH_STYLE_SHEETS
, _("&Switch Style Sheets"));
628 formatMenu
->Append(ID_MANAGE_STYLES
, _("&Manage Styles"));
630 wxMenu
* listsMenu
= new wxMenu
;
631 listsMenu
->Append(ID_FORMAT_BULLETS_AND_NUMBERING
, _("Bullets and &Numbering..."));
632 listsMenu
->AppendSeparator();
633 listsMenu
->Append(ID_FORMAT_NUMBER_LIST
, _("Number List"));
634 listsMenu
->Append(ID_FORMAT_ITEMIZE_LIST
, _("Itemize List"));
635 listsMenu
->Append(ID_FORMAT_RENUMBER_LIST
, _("Renumber List"));
636 listsMenu
->Append(ID_FORMAT_PROMOTE_LIST
, _("Promote List Items"));
637 listsMenu
->Append(ID_FORMAT_DEMOTE_LIST
, _("Demote List Items"));
638 listsMenu
->Append(ID_FORMAT_CLEAR_LIST
, _("Clear List Formatting"));
640 wxMenu
* insertMenu
= new wxMenu
;
641 insertMenu
->Append(ID_INSERT_SYMBOL
, _("&Symbol...\tCtrl+I"));
642 insertMenu
->Append(ID_INSERT_URL
, _("&URL..."));
644 // now append the freshly created menu to the menu bar...
645 wxMenuBar
*menuBar
= new wxMenuBar();
646 menuBar
->Append(fileMenu
, wxT("&File"));
647 menuBar
->Append(editMenu
, wxT("&Edit"));
648 menuBar
->Append(formatMenu
, wxT("F&ormat"));
649 menuBar
->Append(listsMenu
, wxT("&Lists"));
650 menuBar
->Append(insertMenu
, wxT("&Insert"));
651 menuBar
->Append(helpMenu
, wxT("&Help"));
653 // ... and attach this menu bar to the frame
656 // create a status bar just for fun (by default with 1 pane only)
657 // but don't create it on limited screen space (WinCE)
658 bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
664 SetStatusText(wxT("Welcome to wxRichTextCtrl!"));
668 wxToolBar
* toolBar
= CreateToolBar();
670 toolBar
->AddTool(wxID_OPEN
, wxEmptyString
, wxBitmap(open_xpm
), _("Open"));
671 toolBar
->AddTool(wxID_SAVEAS
, wxEmptyString
, wxBitmap(save_xpm
), _("Save"));
672 toolBar
->AddSeparator();
673 toolBar
->AddTool(wxID_CUT
, wxEmptyString
, wxBitmap(cut_xpm
), _("Cut"));
674 toolBar
->AddTool(wxID_COPY
, wxEmptyString
, wxBitmap(copy_xpm
), _("Copy"));
675 toolBar
->AddTool(wxID_PASTE
, wxEmptyString
, wxBitmap(paste_xpm
), _("Paste"));
676 toolBar
->AddSeparator();
677 toolBar
->AddTool(wxID_UNDO
, wxEmptyString
, wxBitmap(undo_xpm
), _("Undo"));
678 toolBar
->AddTool(wxID_REDO
, wxEmptyString
, wxBitmap(redo_xpm
), _("Redo"));
679 toolBar
->AddSeparator();
680 toolBar
->AddCheckTool(ID_FORMAT_BOLD
, wxEmptyString
, wxBitmap(bold_xpm
), wxNullBitmap
, _("Bold"));
681 toolBar
->AddCheckTool(ID_FORMAT_ITALIC
, wxEmptyString
, wxBitmap(italic_xpm
), wxNullBitmap
, _("Italic"));
682 toolBar
->AddCheckTool(ID_FORMAT_UNDERLINE
, wxEmptyString
, wxBitmap(underline_xpm
), wxNullBitmap
, _("Underline"));
683 toolBar
->AddSeparator();
684 toolBar
->AddCheckTool(ID_FORMAT_ALIGN_LEFT
, wxEmptyString
, wxBitmap(alignleft_xpm
), wxNullBitmap
, _("Align Left"));
685 toolBar
->AddCheckTool(ID_FORMAT_ALIGN_CENTRE
, wxEmptyString
, wxBitmap(centre_xpm
), wxNullBitmap
, _("Centre"));
686 toolBar
->AddCheckTool(ID_FORMAT_ALIGN_RIGHT
, wxEmptyString
, wxBitmap(alignright_xpm
), wxNullBitmap
, _("Align Right"));
687 toolBar
->AddSeparator();
688 toolBar
->AddTool(ID_FORMAT_INDENT_LESS
, wxEmptyString
, wxBitmap(indentless_xpm
), _("Indent Less"));
689 toolBar
->AddTool(ID_FORMAT_INDENT_MORE
, wxEmptyString
, wxBitmap(indentmore_xpm
), _("Indent More"));
690 toolBar
->AddSeparator();
691 toolBar
->AddTool(ID_FORMAT_FONT
, wxEmptyString
, wxBitmap(font_xpm
), _("Font"));
693 wxRichTextStyleComboCtrl
* combo
= new wxRichTextStyleComboCtrl(toolBar
, ID_RICHTEXT_STYLE_COMBO
, wxDefaultPosition
, wxSize(200, -1));
694 toolBar
->AddControl(combo
);
698 wxSplitterWindow
* splitter
= new wxSplitterWindow(this, wxID_ANY
, wxDefaultPosition
, GetClientSize(), wxSP_LIVE_UPDATE
);
700 wxFont textFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
701 wxFont boldFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxBOLD
);
702 wxFont italicFont
= wxFont(12, wxROMAN
, wxITALIC
, wxNORMAL
);
704 m_richTextCtrl
= new wxRichTextCtrl(splitter
, ID_RICHTEXT_CTRL
, wxEmptyString
, wxDefaultPosition
, wxSize(200, 200), wxVSCROLL
|wxHSCROLL
|wxWANTS_CHARS
);
705 wxFont
font(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
707 m_richTextCtrl
->SetFont(font
);
709 m_richTextCtrl
->SetStyleSheet(wxGetApp().GetStyleSheet());
711 combo
->SetStyleSheet(wxGetApp().GetStyleSheet());
712 combo
->SetRichTextCtrl(m_richTextCtrl
);
713 combo
->UpdateStyles();
715 wxRichTextStyleListCtrl
* styleListCtrl
= new wxRichTextStyleListCtrl(splitter
, ID_RICHTEXT_STYLE_LIST
);
717 wxSize display
= wxGetDisplaySize();
718 if ( is_pda
&& ( display
.GetWidth() < display
.GetHeight() ) )
720 splitter
->SplitHorizontally(m_richTextCtrl
, styleListCtrl
);
724 splitter
->SplitVertically(m_richTextCtrl
, styleListCtrl
, 500);
727 splitter
->UpdateSize();
729 styleListCtrl
->SetStyleSheet(wxGetApp().GetStyleSheet());
730 styleListCtrl
->SetRichTextCtrl(m_richTextCtrl
);
731 styleListCtrl
->UpdateStyles();
737 void MyFrame::WriteInitialText()
739 wxRichTextCtrl
& r
= *m_richTextCtrl
;
741 r
.SetDefaultStyle(wxRichTextAttr());
743 r
.BeginSuppressUndo();
745 r
.BeginParagraphSpacing(0, 20);
747 r
.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE
);
752 wxString lineBreak
= (wxChar
) 29;
754 r
.WriteText(wxString(wxT("Welcome to wxRichTextCtrl, a wxWidgets control")) + lineBreak
+ wxT("for editing and presenting styled text and images\n"));
758 r
.WriteText(wxT("by Julian Smart"));
764 r
.WriteImage(wxBitmap(zebra_xpm
));
771 r
.WriteText(wxT("What can you do with this thing? "));
773 r
.WriteImage(wxBitmap(smiley_xpm
));
774 r
.WriteText(wxT(" Well, you can change text "));
776 r
.BeginTextColour(wxColour(255, 0, 0));
777 r
.WriteText(wxT("colour, like this red bit."));
780 wxRichTextAttr backgroundColourAttr
;
781 backgroundColourAttr
.SetBackgroundColour(*wxGREEN
);
782 backgroundColourAttr
.SetTextColour(wxColour(0, 0, 255));
783 r
.BeginStyle(backgroundColourAttr
);
784 r
.WriteText(wxT(" And this blue on green bit."));
787 r
.WriteText(wxT(" Naturally you can make things "));
789 r
.WriteText(wxT("bold "));
792 r
.WriteText(wxT("or italic "));
795 r
.WriteText(wxT("or underlined."));
799 r
.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
802 r
.WriteText(wxT(" Next we'll show an indented paragraph."));
806 r
.BeginLeftIndent(60);
807 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."));
812 r
.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
816 r
.BeginLeftIndent(100, -40);
818 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."));
823 r
.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
826 r
.BeginNumberedBullet(1, 100, 60);
827 r
.WriteText(wxT("This is my first item. Note that wxRichTextCtrl can apply numbering and bullets automatically based on list styles, but this list is formatted explicitly by setting indents."));
829 r
.EndNumberedBullet();
831 r
.BeginNumberedBullet(2, 100, 60);
832 r
.WriteText(wxT("This is my second item."));
834 r
.EndNumberedBullet();
836 r
.WriteText(wxT("The following paragraph is right-indented:"));
839 r
.BeginRightIndent(200);
841 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."));
846 r
.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
849 r
.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT
);
850 r
.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF
);
851 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."));
862 attr
.SetFlags(wxTEXT_ATTR_TABS
);
864 r
.SetDefaultStyle(attr
);
866 r
.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
869 r
.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
872 r
.BeginSymbolBullet(wxT('*'), 100, 60);
873 r
.WriteText(wxT("Compatibility with wxTextCtrl API"));
877 r
.BeginSymbolBullet(wxT('*'), 100, 60);
878 r
.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
882 r
.BeginSymbolBullet(wxT('*'), 100, 60);
883 r
.WriteText(wxT("XML loading and saving"));
887 r
.BeginSymbolBullet(wxT('*'), 100, 60);
888 r
.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
892 r
.BeginSymbolBullet(wxT('*'), 100, 60);
893 r
.WriteText(wxT("Clipboard copy and paste"));
897 r
.BeginSymbolBullet(wxT('*'), 100, 60);
898 r
.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
902 r
.BeginSymbolBullet(wxT('*'), 100, 60);
903 r
.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
907 // Make a style suitable for showing a URL
908 wxRichTextAttr urlStyle
;
909 urlStyle
.SetTextColour(*wxBLUE
);
910 urlStyle
.SetFontUnderlined(true);
912 r
.WriteText(wxT("wxRichTextCtrl can also display URLs, such as this one: "));
913 r
.BeginStyle(urlStyle
);
914 r
.BeginURL(wxT("http://www.wxwidgets.org"));
915 r
.WriteText(wxT("The wxWidgets Web Site"));
918 r
.WriteText(wxT(". Click on the URL to generate an event."));
922 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!\n"));
924 r
.EndParagraphSpacing();
931 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
933 // true is to force the frame to close
937 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
940 msg
.Printf( wxT("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
941 wxMessageBox(msg
, wxT("About wxRichTextCtrl Sample"), wxOK
| wxICON_INFORMATION
, this);
944 // Forward command events to the current rich text control, if any
945 bool MyFrame::ProcessEvent(wxEvent
& event
)
947 if (event
.IsCommandEvent() && !event
.IsKindOf(CLASSINFO(wxChildFocusEvent
)))
949 // Problem: we can get infinite recursion because the events
950 // climb back up to this frame, and repeat.
951 // Assume that command events don't cause another command event
952 // to be called, so we can rely on inCommand not being overwritten
954 static int s_eventType
= 0;
955 static wxWindowID s_id
= 0;
957 if (s_id
!= event
.GetId() && s_eventType
!= event
.GetEventType())
959 s_eventType
= event
.GetEventType();
960 s_id
= event
.GetId();
962 wxWindow
* focusWin
= wxFindFocusDescendant(this);
963 if (focusWin
&& focusWin
->GetEventHandler()->ProcessEvent(event
))
980 return wxFrame::ProcessEvent(event
);
983 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
987 wxArrayInt fileTypes
;
989 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes
);
992 filter
+= wxT("All files (*.*)|*.*");
994 wxFileDialog
dialog(this,
995 _("Choose a filename"),
1001 if (dialog
.ShowModal() == wxID_OK
)
1003 wxString path
= dialog
.GetPath();
1007 int filterIndex
= dialog
.GetFilterIndex();
1008 int fileType
= (filterIndex
< (int) fileTypes
.GetCount())
1009 ? fileTypes
[filterIndex
]
1010 : wxRICHTEXT_TYPE_TEXT
;
1011 m_richTextCtrl
->LoadFile(path
, fileType
);
1016 void MyFrame::OnSave(wxCommandEvent
& event
)
1018 if (m_richTextCtrl
->GetFilename().empty())
1023 m_richTextCtrl
->SaveFile();
1026 void MyFrame::OnSaveAs(wxCommandEvent
& WXUNUSED(event
))
1028 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, true);
1032 wxFileDialog
dialog(this,
1033 _("Choose a filename"),
1039 if (dialog
.ShowModal() == wxID_OK
)
1041 wxString path
= dialog
.GetPath();
1045 m_richTextCtrl
->SaveFile(path
);
1050 void MyFrame::OnBold(wxCommandEvent
& WXUNUSED(event
))
1052 m_richTextCtrl
->ApplyBoldToSelection();
1055 void MyFrame::OnItalic(wxCommandEvent
& WXUNUSED(event
))
1057 m_richTextCtrl
->ApplyItalicToSelection();
1060 void MyFrame::OnUnderline(wxCommandEvent
& WXUNUSED(event
))
1062 m_richTextCtrl
->ApplyUnderlineToSelection();
1066 void MyFrame::OnUpdateBold(wxUpdateUIEvent
& event
)
1068 event
.Check(m_richTextCtrl
->IsSelectionBold());
1071 void MyFrame::OnUpdateItalic(wxUpdateUIEvent
& event
)
1073 event
.Check(m_richTextCtrl
->IsSelectionItalics());
1076 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent
& event
)
1078 event
.Check(m_richTextCtrl
->IsSelectionUnderlined());
1081 void MyFrame::OnAlignLeft(wxCommandEvent
& WXUNUSED(event
))
1083 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT
);
1086 void MyFrame::OnAlignCentre(wxCommandEvent
& WXUNUSED(event
))
1088 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE
);
1091 void MyFrame::OnAlignRight(wxCommandEvent
& WXUNUSED(event
))
1093 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT
);
1096 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent
& event
)
1098 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT
));
1101 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent
& event
)
1103 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE
));
1106 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent
& event
)
1108 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT
));
1111 void MyFrame::OnFont(wxCommandEvent
& WXUNUSED(event
))
1113 wxRichTextRange range
;
1114 if (m_richTextCtrl
->HasSelection())
1115 range
= m_richTextCtrl
->GetSelectionRange();
1117 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
1119 int pages
= wxRICHTEXT_FORMAT_FONT
;
1121 wxRichTextFormattingDialog
formatDlg(pages
, this);
1122 formatDlg
.GetStyle(m_richTextCtrl
, range
);
1124 if (formatDlg
.ShowModal() == wxID_OK
)
1126 formatDlg
.ApplyStyle(m_richTextCtrl
, range
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
1129 // Old method using wxFontDialog
1131 if (!m_richTextCtrl
->HasSelection())
1134 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
1135 wxFontData fontData
;
1138 attr
.SetFlags(wxTEXT_ATTR_FONT
);
1140 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1141 fontData
.SetInitialFont(attr
.GetFont());
1143 wxFontDialog
dialog(this, fontData
);
1144 if (dialog
.ShowModal() == wxID_OK
)
1146 fontData
= dialog
.GetFontData();
1147 attr
.SetFlags(wxTEXT_ATTR_FONT
);
1148 attr
.SetFont(fontData
.GetChosenFont());
1149 if (attr
.GetFont().Ok())
1151 m_richTextCtrl
->SetStyle(range
, attr
);
1157 void MyFrame::OnParagraph(wxCommandEvent
& WXUNUSED(event
))
1159 wxRichTextRange range
;
1160 if (m_richTextCtrl
->HasSelection())
1161 range
= m_richTextCtrl
->GetSelectionRange();
1163 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
1165 int pages
= wxRICHTEXT_FORMAT_INDENTS_SPACING
|wxRICHTEXT_FORMAT_TABS
|wxRICHTEXT_FORMAT_BULLETS
;
1167 wxRichTextFormattingDialog
formatDlg(pages
, this);
1168 formatDlg
.GetStyle(m_richTextCtrl
, range
);
1170 if (formatDlg
.ShowModal() == wxID_OK
)
1172 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
1176 void MyFrame::OnFormat(wxCommandEvent
& WXUNUSED(event
))
1178 wxRichTextRange range
;
1179 if (m_richTextCtrl
->HasSelection())
1180 range
= m_richTextCtrl
->GetSelectionRange();
1182 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
1184 int pages
= wxRICHTEXT_FORMAT_FONT
|wxRICHTEXT_FORMAT_INDENTS_SPACING
|wxRICHTEXT_FORMAT_TABS
|wxRICHTEXT_FORMAT_BULLETS
;
1186 wxRichTextFormattingDialog
formatDlg(pages
, this);
1187 formatDlg
.GetStyle(m_richTextCtrl
, range
);
1189 if (formatDlg
.ShowModal() == wxID_OK
)
1191 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
1195 void MyFrame::OnUpdateFormat(wxUpdateUIEvent
& event
)
1197 event
.Enable(m_richTextCtrl
->HasSelection());
1200 void MyFrame::OnIndentMore(wxCommandEvent
& WXUNUSED(event
))
1202 wxRichTextAttr attr
;
1203 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1205 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1207 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1208 if (m_richTextCtrl
->HasSelection())
1209 range
= m_richTextCtrl
->GetSelectionRange();
1211 attr
.SetLeftIndent(attr
.GetLeftIndent() + 100);
1213 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1214 m_richTextCtrl
->SetStyle(range
, attr
);
1218 void MyFrame::OnIndentLess(wxCommandEvent
& WXUNUSED(event
))
1220 wxRichTextAttr attr
;
1221 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1223 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1225 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1226 if (m_richTextCtrl
->HasSelection())
1227 range
= m_richTextCtrl
->GetSelectionRange();
1229 if (attr
.GetLeftIndent() > 0)
1231 attr
.SetLeftIndent(wxMax(0, attr
.GetLeftIndent() - 100));
1233 m_richTextCtrl
->SetStyle(range
, attr
);
1238 void MyFrame::OnLineSpacingHalf(wxCommandEvent
& WXUNUSED(event
))
1240 wxRichTextAttr attr
;
1241 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1243 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1245 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1246 if (m_richTextCtrl
->HasSelection())
1247 range
= m_richTextCtrl
->GetSelectionRange();
1249 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1250 attr
.SetLineSpacing(15);
1252 m_richTextCtrl
->SetStyle(range
, attr
);
1256 void MyFrame::OnLineSpacingDouble(wxCommandEvent
& WXUNUSED(event
))
1258 wxRichTextAttr attr
;
1259 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1261 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1263 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1264 if (m_richTextCtrl
->HasSelection())
1265 range
= m_richTextCtrl
->GetSelectionRange();
1267 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1268 attr
.SetLineSpacing(20);
1270 m_richTextCtrl
->SetStyle(range
, attr
);
1274 void MyFrame::OnLineSpacingSingle(wxCommandEvent
& WXUNUSED(event
))
1276 wxRichTextAttr attr
;
1277 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1279 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1281 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1282 if (m_richTextCtrl
->HasSelection())
1283 range
= m_richTextCtrl
->GetSelectionRange();
1285 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1286 attr
.SetLineSpacing(0); // Can also use 10
1288 m_richTextCtrl
->SetStyle(range
, attr
);
1292 void MyFrame::OnParagraphSpacingMore(wxCommandEvent
& WXUNUSED(event
))
1294 wxRichTextAttr attr
;
1295 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1297 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1299 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1300 if (m_richTextCtrl
->HasSelection())
1301 range
= m_richTextCtrl
->GetSelectionRange();
1303 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() + 20);
1305 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1306 m_richTextCtrl
->SetStyle(range
, attr
);
1310 void MyFrame::OnParagraphSpacingLess(wxCommandEvent
& WXUNUSED(event
))
1312 wxRichTextAttr attr
;
1313 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1315 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1317 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1318 if (m_richTextCtrl
->HasSelection())
1319 range
= m_richTextCtrl
->GetSelectionRange();
1321 if (attr
.GetParagraphSpacingAfter() >= 20)
1323 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() - 20);
1325 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1326 m_richTextCtrl
->SetStyle(range
, attr
);
1331 void MyFrame::OnReload(wxCommandEvent
& WXUNUSED(event
))
1333 m_richTextCtrl
->Clear();
1337 void MyFrame::OnViewHTML(wxCommandEvent
& WXUNUSED(event
))
1339 wxDialog
dialog(this, wxID_ANY
, _("HTML"), wxDefaultPosition
, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE
);
1341 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
1342 dialog
.SetSizer(boxSizer
);
1344 wxHtmlWindow
* win
= new wxHtmlWindow(& dialog
, wxID_ANY
, wxDefaultPosition
, wxSize(500, 400), wxSUNKEN_BORDER
);
1345 boxSizer
->Add(win
, 1, wxALL
, 5);
1347 wxButton
* cancelButton
= new wxButton(& dialog
, wxID_CANCEL
, wxT("&Close"));
1348 boxSizer
->Add(cancelButton
, 0, wxALL
|wxCENTRE
, 5);
1351 wxStringOutputStream
strStream(& text
);
1353 wxRichTextHTMLHandler htmlHandler
;
1354 htmlHandler
.SetFlags(wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY
);
1356 wxArrayInt fontSizeMapping
;
1357 fontSizeMapping
.Add(7);
1358 fontSizeMapping
.Add(9);
1359 fontSizeMapping
.Add(11);
1360 fontSizeMapping
.Add(12);
1361 fontSizeMapping
.Add(14);
1362 fontSizeMapping
.Add(22);
1363 fontSizeMapping
.Add(100);
1365 htmlHandler
.SetFontSizeMapping(fontSizeMapping
);
1367 if (htmlHandler
.SaveFile(& m_richTextCtrl
->GetBuffer(), strStream
))
1372 boxSizer
->Fit(& dialog
);
1376 // Now delete the temporary in-memory images
1377 htmlHandler
.DeleteTemporaryImages();
1380 // Demonstrates how you can change the style sheets and have the changes
1381 // reflected in the control content without wiping out character formatting.
1383 void MyFrame::OnSwitchStyleSheets(wxCommandEvent
& WXUNUSED(event
))
1385 static wxRichTextStyleSheet
* gs_AlternateStyleSheet
= NULL
;
1387 wxRichTextStyleListCtrl
*styleList
= (wxRichTextStyleListCtrl
*) FindWindow(ID_RICHTEXT_STYLE_LIST
);
1388 wxRichTextStyleComboCtrl
* styleCombo
= (wxRichTextStyleComboCtrl
*) FindWindow(ID_RICHTEXT_STYLE_COMBO
);
1390 wxRichTextStyleSheet
* sheet
= m_richTextCtrl
->GetStyleSheet();
1392 // One-time creation of an alternate style sheet
1393 if (!gs_AlternateStyleSheet
)
1395 gs_AlternateStyleSheet
= new wxRichTextStyleSheet(*sheet
);
1397 // Make some modifications
1398 for (int i
= 0; i
< (int) gs_AlternateStyleSheet
->GetParagraphStyleCount(); i
++)
1400 wxRichTextParagraphStyleDefinition
* def
= gs_AlternateStyleSheet
->GetParagraphStyle(i
);
1402 if (def
->GetStyle().HasTextColour())
1403 def
->GetStyle().SetTextColour(*wxBLUE
);
1405 if (def
->GetStyle().HasAlignment())
1407 if (def
->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1408 def
->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1409 else if (def
->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1410 def
->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1412 if (def
->GetStyle().HasLeftIndent())
1414 def
->GetStyle().SetLeftIndent(def
->GetStyle().GetLeftIndent() * 2);
1420 wxRichTextStyleSheet
* tmp
= gs_AlternateStyleSheet
;
1421 gs_AlternateStyleSheet
= sheet
;
1424 m_richTextCtrl
->SetStyleSheet(sheet
);
1425 m_richTextCtrl
->ApplyStyleSheet(sheet
); // Makes the control reflect the new style definitions
1427 styleList
->SetStyleSheet(sheet
);
1428 styleList
->UpdateStyles();
1430 styleCombo
->SetStyleSheet(sheet
);
1431 styleCombo
->UpdateStyles();
1434 void MyFrame::OnManageStyles(wxCommandEvent
& WXUNUSED(event
))
1436 wxRichTextStyleSheet
* sheet
= m_richTextCtrl
->GetStyleSheet();
1438 int flags
= wxRICHTEXT_ORGANISER_CREATE_STYLES
|wxRICHTEXT_ORGANISER_EDIT_STYLES
;
1440 wxRichTextStyleOrganiserDialog
dlg(flags
, sheet
, NULL
, this, wxID_ANY
, _("Style Manager"));
1444 void MyFrame::OnInsertSymbol(wxCommandEvent
& WXUNUSED(event
))
1446 wxRichTextAttr attr
;
1447 attr
.SetFlags(wxTEXT_ATTR_FONT
);
1448 m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
);
1450 wxString currentFontName
;
1451 if (attr
.HasFont() && attr
.GetFont().Ok())
1452 currentFontName
= attr
.GetFont().GetFaceName();
1454 // Don't set the initial font in the dialog (so the user is choosing
1455 // 'normal text', i.e. the current font) but do tell the dialog
1456 // what 'normal text' is.
1458 wxSymbolPickerDialog
dlg(wxT("*"), wxEmptyString
, currentFontName
, this);
1460 if (dlg
.ShowModal() == wxID_OK
)
1462 if (dlg
.HasSelection())
1464 long insertionPoint
= m_richTextCtrl
->GetInsertionPoint();
1466 m_richTextCtrl
->WriteText(dlg
.GetSymbol());
1468 if (!dlg
.UseNormalFont())
1470 wxFont
font(attr
.GetFont());
1471 font
.SetFaceName(dlg
.GetFontName());
1473 m_richTextCtrl
->SetStyle(insertionPoint
, insertionPoint
+1, attr
);
1479 void MyFrame::OnNumberList(wxCommandEvent
& WXUNUSED(event
))
1481 if (m_richTextCtrl
->HasSelection())
1483 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
1484 m_richTextCtrl
->SetListStyle(range
, wxT("Numbered List 1"), wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_RENUMBER
);
1488 void MyFrame::OnBulletsAndNumbering(wxCommandEvent
& WXUNUSED(event
))
1490 wxRichTextStyleSheet
* sheet
= m_richTextCtrl
->GetStyleSheet();
1492 int flags
= wxRICHTEXT_ORGANISER_BROWSE_NUMBERING
;
1494 wxRichTextStyleOrganiserDialog
dlg(flags
, sheet
, m_richTextCtrl
, this, wxID_ANY
, _("Bullets and Numbering"));
1495 if (dlg
.ShowModal() == wxID_OK
)
1497 if (dlg
.GetSelectedStyleDefinition())
1502 void MyFrame::OnItemizeList(wxCommandEvent
& WXUNUSED(event
))
1504 if (m_richTextCtrl
->HasSelection())
1506 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
1507 m_richTextCtrl
->SetListStyle(range
, wxT("Bullet List 1"));
1511 void MyFrame::OnRenumberList(wxCommandEvent
& WXUNUSED(event
))
1513 if (m_richTextCtrl
->HasSelection())
1515 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
1516 m_richTextCtrl
->NumberList(range
, NULL
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_RENUMBER
);
1520 void MyFrame::OnPromoteList(wxCommandEvent
& WXUNUSED(event
))
1522 if (m_richTextCtrl
->HasSelection())
1524 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
1525 m_richTextCtrl
->PromoteList(1, range
, NULL
);
1529 void MyFrame::OnDemoteList(wxCommandEvent
& WXUNUSED(event
))
1531 if (m_richTextCtrl
->HasSelection())
1533 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
1534 m_richTextCtrl
->PromoteList(-1, range
, NULL
);
1538 void MyFrame::OnClearList(wxCommandEvent
& WXUNUSED(event
))
1540 if (m_richTextCtrl
->HasSelection())
1542 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
1543 m_richTextCtrl
->ClearListStyle(range
);
1547 void MyFrame::OnInsertURL(wxCommandEvent
& WXUNUSED(event
))
1549 wxString url
= wxGetTextFromUser(_("URL:"), _("Insert URL"));
1552 // Make a style suitable for showing a URL
1553 wxRichTextAttr urlStyle
;
1554 urlStyle
.SetTextColour(*wxBLUE
);
1555 urlStyle
.SetFontUnderlined(true);
1557 m_richTextCtrl
->BeginStyle(urlStyle
);
1558 m_richTextCtrl
->BeginURL(url
);
1559 m_richTextCtrl
->WriteText(url
);
1560 m_richTextCtrl
->EndURL();
1561 m_richTextCtrl
->EndStyle();
1565 void MyFrame::OnURL(wxTextUrlEvent
& event
)
1567 wxMessageBox(event
.GetString());
1570 // Veto style sheet replace events when loading from XML, since we want
1571 // to keep the original style sheet.
1572 void MyFrame::OnStyleSheetReplacing(wxRichTextEvent
& event
)
1577 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1579 wxGetApp().GetPrinting()->PrintBuffer(m_richTextCtrl
->GetBuffer());
1582 void MyFrame::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1584 wxGetApp().GetPrinting()->PreviewBuffer(m_richTextCtrl
->GetBuffer());
1587 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
1589 wxGetApp().GetPrinting()->PageSetup();