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 OnNumberList(wxCommandEvent
& event
);
153 void OnItemizeList(wxCommandEvent
& event
);
154 void OnRenumberList(wxCommandEvent
& event
);
155 void OnPromoteList(wxCommandEvent
& event
);
156 void OnDemoteList(wxCommandEvent
& event
);
157 void OnClearList(wxCommandEvent
& event
);
159 void OnViewHTML(wxCommandEvent
& event
);
161 void OnSwitchStyleSheets(wxCommandEvent
& event
);
163 // Forward command events to the current rich text control, if any
164 bool ProcessEvent(wxEvent
& event
);
167 // any class wishing to process wxWidgets events must use this macro
168 DECLARE_EVENT_TABLE()
170 wxRichTextCtrl
* m_richTextCtrl
;
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 // IDs for the controls and the menu commands
182 ID_About
= wxID_ABOUT
,
184 ID_FORMAT_BOLD
= 100,
193 ID_FORMAT_ALIGN_LEFT
,
194 ID_FORMAT_ALIGN_CENTRE
,
195 ID_FORMAT_ALIGN_RIGHT
,
197 ID_FORMAT_INDENT_MORE
,
198 ID_FORMAT_INDENT_LESS
,
200 ID_FORMAT_PARAGRAPH_SPACING_MORE
,
201 ID_FORMAT_PARAGRAPH_SPACING_LESS
,
203 ID_FORMAT_LINE_SPACING_HALF
,
204 ID_FORMAT_LINE_SPACING_DOUBLE
,
205 ID_FORMAT_LINE_SPACING_SINGLE
,
207 ID_FORMAT_NUMBER_LIST
,
208 ID_FORMAT_ITEMIZE_LIST
,
209 ID_FORMAT_RENUMBER_LIST
,
210 ID_FORMAT_PROMOTE_LIST
,
211 ID_FORMAT_DEMOTE_LIST
,
212 ID_FORMAT_CLEAR_LIST
,
215 ID_SWITCH_STYLE_SHEETS
,
218 ID_RICHTEXT_STYLE_LIST
,
219 ID_RICHTEXT_STYLE_COMBO
222 // ----------------------------------------------------------------------------
223 // event tables and other macros for wxWidgets
224 // ----------------------------------------------------------------------------
226 // the event tables connect the wxWidgets events with the functions (event
227 // handlers) which process them. It can be also done at run-time, but for the
228 // simple menu events like this the static method is much simpler.
229 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
230 EVT_MENU(ID_Quit
, MyFrame::OnQuit
)
231 EVT_MENU(ID_About
, MyFrame::OnAbout
)
233 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
234 EVT_MENU(wxID_SAVE
, MyFrame::OnSave
)
235 EVT_MENU(wxID_SAVEAS
, MyFrame::OnSaveAs
)
237 EVT_MENU(ID_FORMAT_BOLD
, MyFrame::OnBold
)
238 EVT_MENU(ID_FORMAT_ITALIC
, MyFrame::OnItalic
)
239 EVT_MENU(ID_FORMAT_UNDERLINE
, MyFrame::OnUnderline
)
241 EVT_UPDATE_UI(ID_FORMAT_BOLD
, MyFrame::OnUpdateBold
)
242 EVT_UPDATE_UI(ID_FORMAT_ITALIC
, MyFrame::OnUpdateItalic
)
243 EVT_UPDATE_UI(ID_FORMAT_UNDERLINE
, MyFrame::OnUpdateUnderline
)
245 EVT_MENU(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnAlignLeft
)
246 EVT_MENU(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnAlignCentre
)
247 EVT_MENU(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnAlignRight
)
249 EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnUpdateAlignLeft
)
250 EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnUpdateAlignCentre
)
251 EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnUpdateAlignRight
)
253 EVT_MENU(ID_FORMAT_FONT
, MyFrame::OnFont
)
254 EVT_MENU(ID_FORMAT_PARAGRAPH
, MyFrame::OnParagraph
)
255 EVT_MENU(ID_FORMAT_CONTENT
, MyFrame::OnFormat
)
256 EVT_UPDATE_UI(ID_FORMAT_CONTENT
, MyFrame::OnUpdateFormat
)
257 EVT_UPDATE_UI(ID_FORMAT_FONT
, MyFrame::OnUpdateFormat
)
258 EVT_UPDATE_UI(ID_FORMAT_PARAGRAPH
, MyFrame::OnUpdateFormat
)
259 EVT_MENU(ID_FORMAT_INDENT_MORE
, MyFrame::OnIndentMore
)
260 EVT_MENU(ID_FORMAT_INDENT_LESS
, MyFrame::OnIndentLess
)
262 EVT_MENU(ID_FORMAT_LINE_SPACING_HALF
, MyFrame::OnLineSpacingHalf
)
263 EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE
, MyFrame::OnLineSpacingSingle
)
264 EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE
, MyFrame::OnLineSpacingDouble
)
266 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE
, MyFrame::OnParagraphSpacingMore
)
267 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS
, MyFrame::OnParagraphSpacingLess
)
269 EVT_MENU(ID_INSERT_SYMBOL
, MyFrame::OnInsertSymbol
)
271 EVT_MENU(ID_FORMAT_NUMBER_LIST
, MyFrame::OnNumberList
)
272 EVT_MENU(ID_FORMAT_ITEMIZE_LIST
, MyFrame::OnItemizeList
)
273 EVT_MENU(ID_FORMAT_RENUMBER_LIST
, MyFrame::OnRenumberList
)
274 EVT_MENU(ID_FORMAT_PROMOTE_LIST
, MyFrame::OnPromoteList
)
275 EVT_MENU(ID_FORMAT_DEMOTE_LIST
, MyFrame::OnDemoteList
)
276 EVT_MENU(ID_FORMAT_CLEAR_LIST
, MyFrame::OnClearList
)
278 EVT_MENU(ID_VIEW_HTML
, MyFrame::OnViewHTML
)
279 EVT_MENU(ID_SWITCH_STYLE_SHEETS
, MyFrame::OnSwitchStyleSheets
)
282 // Create a new application object: this macro will allow wxWidgets to create
283 // the application object during program execution (it's better than using a
284 // static object for many reasons) and also implements the accessor function
285 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
289 // ============================================================================
291 // ============================================================================
293 // ----------------------------------------------------------------------------
294 // the application class
295 // ----------------------------------------------------------------------------
297 // 'Main program' equivalent: the program execution "starts" here
301 wxHelpProvider::Set(new wxSimpleHelpProvider
);
304 m_styleSheet
= new wxRichTextStyleSheet
;
308 // Add extra handlers (plain text is automatically added)
309 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler
);
310 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler
);
312 // Add image handlers
314 wxImage::AddHandler( new wxPNGHandler
);
318 wxImage::AddHandler( new wxJPEGHandler
);
322 wxImage::AddHandler( new wxGIFHandler
);
325 // create the main application window
326 MyFrame
*frame
= new MyFrame(_T("wxRichTextCtrl Sample"), wxID_ANY
, wxDefaultPosition
, wxSize(700, 600));
328 // and show it (the frames, unlike simple controls, are not shown when
329 // created initially)
332 // success: wxApp::OnRun() will be called which will enter the main message
333 // loop and the application will run. If we returned false here, the
334 // application would exit immediately.
344 void MyApp::CreateStyles()
348 wxFont
romanFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
349 wxFont
swissFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
);
351 wxRichTextParagraphStyleDefinition
* normalPara
= new wxRichTextParagraphStyleDefinition(wxT("Normal"));
352 wxRichTextAttr normalAttr
;
353 normalAttr
.SetFontFaceName(romanFont
.GetFaceName());
354 normalAttr
.SetFontSize(12);
355 // Let's set all attributes for this style
356 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
|
357 wxTEXT_ATTR_PARA_SPACING_BEFORE
|wxTEXT_ATTR_PARA_SPACING_AFTER
|wxTEXT_ATTR_LINE_SPACING
|
358 wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
);
359 normalPara
->SetStyle(normalAttr
);
361 m_styleSheet
->AddParagraphStyle(normalPara
);
363 wxRichTextParagraphStyleDefinition
* indentedPara
= new wxRichTextParagraphStyleDefinition(wxT("Indented"));
364 wxRichTextAttr indentedAttr
;
365 indentedAttr
.SetFontFaceName(romanFont
.GetFaceName());
366 indentedAttr
.SetFontSize(12);
367 indentedAttr
.SetLeftIndent(100, 0);
368 // We only want to affect indentation
369 indentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
370 indentedPara
->SetStyle(indentedAttr
);
372 m_styleSheet
->AddParagraphStyle(indentedPara
);
374 wxRichTextParagraphStyleDefinition
* indentedPara2
= new wxRichTextParagraphStyleDefinition(wxT("Red Bold Indented"));
375 wxRichTextAttr indentedAttr2
;
376 indentedAttr2
.SetFontFaceName(romanFont
.GetFaceName());
377 indentedAttr2
.SetFontSize(12);
378 indentedAttr2
.SetFontWeight(wxBOLD
);
379 indentedAttr2
.SetTextColour(*wxRED
);
380 indentedAttr2
.SetFontSize(12);
381 indentedAttr2
.SetLeftIndent(100, 0);
382 // We want to affect indentation, font and text colour
383 indentedAttr2
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
|wxTEXT_ATTR_FONT
|wxTEXT_ATTR_TEXT_COLOUR
);
384 indentedPara2
->SetStyle(indentedAttr2
);
386 m_styleSheet
->AddParagraphStyle(indentedPara2
);
388 wxRichTextParagraphStyleDefinition
* flIndentedPara
= new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
389 wxRichTextAttr flIndentedAttr
;
390 flIndentedAttr
.SetFontFaceName(swissFont
.GetFaceName());
391 flIndentedAttr
.SetFontSize(12);
392 flIndentedAttr
.SetLeftIndent(100, -100);
393 // We only want to affect indentation
394 flIndentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
395 flIndentedPara
->SetStyle(flIndentedAttr
);
397 m_styleSheet
->AddParagraphStyle(flIndentedPara
);
401 wxRichTextCharacterStyleDefinition
* boldDef
= new wxRichTextCharacterStyleDefinition(wxT("Bold"));
402 wxRichTextAttr boldAttr
;
403 boldAttr
.SetFontFaceName(romanFont
.GetFaceName());
404 boldAttr
.SetFontSize(12);
405 boldAttr
.SetFontWeight(wxBOLD
);
406 // We only want to affect boldness
407 boldAttr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
408 boldDef
->SetStyle(boldAttr
);
410 m_styleSheet
->AddCharacterStyle(boldDef
);
412 wxRichTextCharacterStyleDefinition
* italicDef
= new wxRichTextCharacterStyleDefinition(wxT("Italic"));
413 wxRichTextAttr italicAttr
;
414 italicAttr
.SetFontFaceName(romanFont
.GetFaceName());
415 italicAttr
.SetFontSize(12);
416 italicAttr
.SetFontStyle(wxITALIC
);
417 // We only want to affect italics
418 italicAttr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
419 italicDef
->SetStyle(italicAttr
);
421 m_styleSheet
->AddCharacterStyle(italicDef
);
423 wxRichTextCharacterStyleDefinition
* redDef
= new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
424 wxRichTextAttr redAttr
;
425 redAttr
.SetFontFaceName(romanFont
.GetFaceName());
426 redAttr
.SetFontSize(12);
427 redAttr
.SetFontWeight(wxBOLD
);
428 redAttr
.SetTextColour(*wxRED
);
429 // We only want to affect colour, weight and face
430 redAttr
.SetFlags(wxTEXT_ATTR_FONT_FACE
|wxTEXT_ATTR_FONT_WEIGHT
|wxTEXT_ATTR_TEXT_COLOUR
);
431 redDef
->SetStyle(redAttr
);
433 m_styleSheet
->AddCharacterStyle(redDef
);
435 wxRichTextListStyleDefinition
* bulletList
= new wxRichTextListStyleDefinition(wxT("Bullet List 1"));
437 for (i
= 0; i
< 10; i
++)
439 wxString bulletSymbol
;
441 bulletSymbol
= wxT("*");
443 bulletSymbol
= wxT("-");
445 bulletSymbol
= wxT("*");
447 bulletSymbol
= wxT("-");
449 bulletSymbol
= wxT("*");
451 bulletList
->SetAttributes(i
, (i
+1)*60, 60, wxTEXT_ATTR_BULLET_STYLE_SYMBOL
, bulletSymbol
);
454 m_styleSheet
->AddListStyle(bulletList
);
456 wxRichTextListStyleDefinition
* numberedList
= new wxRichTextListStyleDefinition(wxT("Numbered List 1"));
457 for (i
= 0; i
< 10; i
++)
461 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
;
463 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
;
465 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
;
467 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES
;
469 numberStyle
= wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_PERIOD
;
471 numberedList
->SetAttributes(i
, (i
+1)*60, 60, numberStyle
);
474 m_styleSheet
->AddListStyle(numberedList
);
477 // ----------------------------------------------------------------------------
479 // ----------------------------------------------------------------------------
482 MyFrame::MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
,
483 const wxSize
& size
, long style
)
484 : wxFrame(NULL
, id
, title
, pos
, size
, style
)
486 // set the frame icon
487 SetIcon(wxICON(sample
));
490 wxMenu
*fileMenu
= new wxMenu
;
492 // the "About" item should be in the help menu
493 wxMenu
*helpMenu
= new wxMenu
;
494 helpMenu
->Append(ID_About
, _T("&About...\tF1"), _T("Show about dialog"));
496 fileMenu
->Append(wxID_OPEN
, _T("&Open\tCtrl+O"), _T("Open a file"));
497 fileMenu
->Append(wxID_SAVE
, _T("&Save\tCtrl+S"), _T("Save a file"));
498 fileMenu
->Append(wxID_SAVEAS
, _T("&Save As...\tF12"), _T("Save to a new file"));
499 fileMenu
->AppendSeparator();
500 fileMenu
->Append(ID_VIEW_HTML
, _T("&View as HTML"), _T("View HTML"));
501 fileMenu
->AppendSeparator();
502 fileMenu
->Append(ID_Quit
, _T("E&xit\tAlt+X"), _T("Quit this program"));
504 wxMenu
* editMenu
= new wxMenu
;
505 editMenu
->Append(wxID_UNDO
, _("&Undo\tCtrl+Z"));
506 editMenu
->Append(wxID_REDO
, _("&Redo\tCtrl+Y"));
507 editMenu
->AppendSeparator();
508 editMenu
->Append(wxID_CUT
, _("Cu&t\tCtrl+X"));
509 editMenu
->Append(wxID_COPY
, _("&Copy\tCtrl+C"));
510 editMenu
->Append(wxID_PASTE
, _("&Paste\tCtrl+V"));
512 editMenu
->Append(wxID_CLEAR
, _("&Delete\tDel"));
514 editMenu
->AppendSeparator();
515 editMenu
->Append(wxID_SELECTALL
, _("Select A&ll\tCtrl+A"));
517 editMenu
->AppendSeparator();
518 editMenu
->Append(wxID_FIND
, _("&Find...\tCtrl+F"));
519 editMenu
->Append(stID_FIND_REPLACE
, _("&Replace...\tCtrl+R"));
522 wxMenu
* formatMenu
= new wxMenu
;
523 formatMenu
->AppendCheckItem(ID_FORMAT_BOLD
, _("&Bold\tCtrl+B"));
524 formatMenu
->AppendCheckItem(ID_FORMAT_ITALIC
, _("&Italic\tCtrl+I"));
525 formatMenu
->AppendCheckItem(ID_FORMAT_UNDERLINE
, _("&Underline\tCtrl+U"));
526 formatMenu
->AppendSeparator();
527 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_LEFT
, _("L&eft Align"));
528 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT
, _("&Right Align"));
529 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE
, _("&Centre"));
530 formatMenu
->AppendSeparator();
531 formatMenu
->Append(ID_FORMAT_INDENT_MORE
, _("Indent &More"));
532 formatMenu
->Append(ID_FORMAT_INDENT_LESS
, _("Indent &Less"));
533 formatMenu
->AppendSeparator();
534 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE
, _("Increase Paragraph &Spacing"));
535 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS
, _("Decrease &Paragraph Spacing"));
536 formatMenu
->AppendSeparator();
537 formatMenu
->Append(ID_FORMAT_LINE_SPACING_SINGLE
, _("Normal Line Spacing"));
538 formatMenu
->Append(ID_FORMAT_LINE_SPACING_HALF
, _("1.5 Line Spacing"));
539 formatMenu
->Append(ID_FORMAT_LINE_SPACING_DOUBLE
, _("Double Line Spacing"));
540 formatMenu
->AppendSeparator();
541 formatMenu
->Append(ID_FORMAT_FONT
, _("&Font..."));
542 formatMenu
->Append(ID_FORMAT_PARAGRAPH
, _("&Paragraph..."));
543 formatMenu
->Append(ID_FORMAT_CONTENT
, _("Font and Pa&ragraph...\tShift+Ctrl+F"));
544 formatMenu
->AppendSeparator();
545 formatMenu
->Append(ID_FORMAT_NUMBER_LIST
, _("Number List"));
546 formatMenu
->Append(ID_FORMAT_ITEMIZE_LIST
, _("Itemize List"));
547 formatMenu
->Append(ID_FORMAT_RENUMBER_LIST
, _("Renumber List"));
548 formatMenu
->Append(ID_FORMAT_PROMOTE_LIST
, _("Promote List Items"));
549 formatMenu
->Append(ID_FORMAT_DEMOTE_LIST
, _("Demote List Items"));
550 formatMenu
->Append(ID_FORMAT_CLEAR_LIST
, _("Clear List Formatting"));
551 formatMenu
->AppendSeparator();
552 formatMenu
->Append(ID_SWITCH_STYLE_SHEETS
, _("&Switch Style Sheets"));
554 wxMenu
* insertMenu
= new wxMenu
;
555 insertMenu
->Append(ID_INSERT_SYMBOL
, _("&Symbol...\tCtrl+I"));
557 // now append the freshly created menu to the menu bar...
558 wxMenuBar
*menuBar
= new wxMenuBar();
559 menuBar
->Append(fileMenu
, _T("&File"));
560 menuBar
->Append(editMenu
, _T("&Edit"));
561 menuBar
->Append(formatMenu
, _T("F&ormat"));
562 menuBar
->Append(insertMenu
, _T("&Insert"));
563 menuBar
->Append(helpMenu
, _T("&Help"));
565 // ... and attach this menu bar to the frame
568 // create a status bar just for fun (by default with 1 pane only)
569 // but don't create it on limited screen space (WinCE)
570 bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
576 SetStatusText(_T("Welcome to wxRichTextCtrl!"));
580 wxToolBar
* toolBar
= CreateToolBar();
582 toolBar
->AddTool(wxID_OPEN
, wxBitmap(open_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Open"));
583 toolBar
->AddTool(wxID_SAVEAS
, wxBitmap(save_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Save"));
584 toolBar
->AddSeparator();
585 toolBar
->AddTool(wxID_CUT
, wxBitmap(cut_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Cut"));
586 toolBar
->AddTool(wxID_COPY
, wxBitmap(copy_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Copy"));
587 toolBar
->AddTool(wxID_PASTE
, wxBitmap(paste_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Paste"));
588 toolBar
->AddSeparator();
589 toolBar
->AddTool(wxID_UNDO
, wxBitmap(undo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Undo"));
590 toolBar
->AddTool(wxID_REDO
, wxBitmap(redo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Redo"));
591 toolBar
->AddSeparator();
592 toolBar
->AddTool(ID_FORMAT_BOLD
, wxBitmap(bold_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Bold"));
593 toolBar
->AddTool(ID_FORMAT_ITALIC
, wxBitmap(italic_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Italic"));
594 toolBar
->AddTool(ID_FORMAT_UNDERLINE
, wxBitmap(underline_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Underline"));
595 toolBar
->AddSeparator();
596 toolBar
->AddTool(ID_FORMAT_ALIGN_LEFT
, wxBitmap(alignleft_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Left"));
597 toolBar
->AddTool(ID_FORMAT_ALIGN_CENTRE
, wxBitmap(centre_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Centre"));
598 toolBar
->AddTool(ID_FORMAT_ALIGN_RIGHT
, wxBitmap(alignright_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Right"));
599 toolBar
->AddSeparator();
600 toolBar
->AddTool(ID_FORMAT_INDENT_LESS
, wxBitmap(indentless_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent Less"));
601 toolBar
->AddTool(ID_FORMAT_INDENT_MORE
, wxBitmap(indentmore_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent More"));
602 toolBar
->AddSeparator();
603 toolBar
->AddTool(ID_FORMAT_FONT
, wxBitmap(font_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Font"));
605 wxRichTextStyleComboCtrl
* combo
= new wxRichTextStyleComboCtrl(toolBar
, ID_RICHTEXT_STYLE_COMBO
, wxDefaultPosition
, wxSize(200, -1));
606 toolBar
->AddControl(combo
);
610 wxSplitterWindow
* splitter
= new wxSplitterWindow(this, wxID_ANY
, wxDefaultPosition
, GetClientSize(), wxSP_NO_XP_THEME
|wxSP_3D
|wxSP_LIVE_UPDATE
);
612 wxFont textFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
613 wxFont boldFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxBOLD
);
614 wxFont italicFont
= wxFont(12, wxROMAN
, wxITALIC
, wxNORMAL
);
616 m_richTextCtrl
= new wxRichTextCtrl(splitter
, ID_RICHTEXT_CTRL
, wxEmptyString
, wxDefaultPosition
, wxSize(200, 200), wxVSCROLL
|wxHSCROLL
|wxNO_BORDER
|wxWANTS_CHARS
);
617 wxFont
font(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
619 m_richTextCtrl
->SetFont(font
);
621 m_richTextCtrl
->SetStyleSheet(wxGetApp().GetStyleSheet());
623 combo
->SetStyleSheet(wxGetApp().GetStyleSheet());
624 combo
->SetRichTextCtrl(m_richTextCtrl
);
625 combo
->UpdateStyles();
627 wxRichTextStyleListCtrl
* styleListCtrl
= new wxRichTextStyleListCtrl(splitter
, ID_RICHTEXT_STYLE_LIST
);
629 wxSize display
= wxGetDisplaySize();
630 if ( is_pda
&& ( display
.GetWidth() < display
.GetHeight() ) )
632 splitter
->SplitHorizontally(m_richTextCtrl
, styleListCtrl
);
636 splitter
->SplitVertically(m_richTextCtrl
, styleListCtrl
, 500);
639 splitter
->UpdateSize();
641 styleListCtrl
->SetStyleSheet(wxGetApp().GetStyleSheet());
642 styleListCtrl
->SetRichTextCtrl(m_richTextCtrl
);
643 styleListCtrl
->UpdateStyles();
645 wxRichTextCtrl
& r
= *m_richTextCtrl
;
647 r
.BeginSuppressUndo();
649 r
.BeginParagraphSpacing(0, 20);
651 r
.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE
);
655 r
.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
660 r
.WriteText(wxT("by Julian Smart"));
666 r
.WriteImage(wxBitmap(zebra_xpm
));
673 r
.WriteText(wxT("What can you do with this thing? "));
674 r
.WriteImage(wxBitmap(smiley_xpm
));
675 r
.WriteText(wxT(" Well, you can change text "));
677 r
.BeginTextColour(wxColour(255, 0, 0));
678 r
.WriteText(wxT("colour, like this red bit."));
681 r
.BeginTextColour(wxColour(0, 0, 255));
682 r
.WriteText(wxT(" And this blue bit."));
685 r
.WriteText(wxT(" Naturally you can make things "));
687 r
.WriteText(wxT("bold "));
690 r
.WriteText(wxT("or italic "));
693 r
.WriteText(wxT("or underlined."));
697 r
.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
700 r
.WriteText(wxT(" Next we'll show an indented paragraph."));
702 r
.BeginLeftIndent(60);
705 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."));
710 r
.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
712 r
.BeginLeftIndent(100, -40);
715 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."));
720 r
.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
722 r
.BeginNumberedBullet(1, 100, 60);
725 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."));
726 r
.EndNumberedBullet();
728 r
.BeginNumberedBullet(2, 100, 60);
731 r
.WriteText(wxT("This is my second item."));
732 r
.EndNumberedBullet();
736 r
.WriteText(wxT("The following paragraph is right-indented:"));
738 r
.BeginRightIndent(200);
741 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."));
746 r
.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
748 r
.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT
);
749 r
.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF
);
752 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."));
762 attr
.SetFlags(wxTEXT_ATTR_TABS
);
764 r
.SetDefaultStyle(attr
);
767 r
.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
770 r
.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
772 r
.BeginSymbolBullet(wxT('*'), 100, 60);
774 r
.WriteText(wxT("Compatibility with wxTextCtrl API"));
777 r
.BeginSymbolBullet(wxT('*'), 100, 60);
779 r
.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
782 r
.BeginSymbolBullet(wxT('*'), 100, 60);
784 r
.WriteText(wxT("XML loading and saving"));
787 r
.BeginSymbolBullet(wxT('*'), 100, 60);
789 r
.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
792 r
.BeginSymbolBullet(wxT('*'), 100, 60);
794 r
.WriteText(wxT("Clipboard copy and paste"));
797 r
.BeginSymbolBullet(wxT('*'), 100, 60);
799 r
.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
802 r
.BeginSymbolBullet(wxT('*'), 100, 60);
804 r
.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
809 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!"));
811 r
.EndParagraphSpacing();
819 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
821 // true is to force the frame to close
825 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
828 msg
.Printf( _T("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
829 wxMessageBox(msg
, _T("About wxRichTextCtrl Sample"), wxOK
| wxICON_INFORMATION
, this);
832 // Forward command events to the current rich text control, if any
833 bool MyFrame::ProcessEvent(wxEvent
& event
)
835 if (event
.IsCommandEvent() && !event
.IsKindOf(CLASSINFO(wxChildFocusEvent
)))
837 // Problem: we can get infinite recursion because the events
838 // climb back up to this frame, and repeat.
839 // Assume that command events don't cause another command event
840 // to be called, so we can rely on inCommand not being overwritten
842 static int s_eventType
= 0;
843 static wxWindowID s_id
= 0;
845 if (s_id
!= event
.GetId() && s_eventType
!= event
.GetEventType())
847 s_eventType
= event
.GetEventType();
848 s_id
= event
.GetId();
850 wxWindow
* focusWin
= wxFindFocusDescendant(this);
851 if (focusWin
&& focusWin
->ProcessEvent(event
))
868 return wxFrame::ProcessEvent(event
);
871 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
875 wxArrayInt fileTypes
;
877 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes
);
880 filter
+= wxT("All files (*.*)|*.*");
882 wxFileDialog
dialog(this,
883 _("Choose a filename"),
889 if (dialog
.ShowModal() == wxID_OK
)
891 wxString path
= dialog
.GetPath();
895 int filterIndex
= dialog
.GetFilterIndex();
896 int fileType
= (filterIndex
< (int) fileTypes
.GetCount())
897 ? fileTypes
[filterIndex
]
898 : wxRICHTEXT_TYPE_TEXT
;
899 m_richTextCtrl
->LoadFile(path
, fileType
);
904 void MyFrame::OnSave(wxCommandEvent
& event
)
906 if (m_richTextCtrl
->GetFilename().empty())
911 m_richTextCtrl
->SaveFile();
914 void MyFrame::OnSaveAs(wxCommandEvent
& WXUNUSED(event
))
916 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, true);
920 wxFileDialog
dialog(this,
921 _("Choose a filename"),
927 if (dialog
.ShowModal() == wxID_OK
)
929 wxString path
= dialog
.GetPath();
933 m_richTextCtrl
->SaveFile(path
);
938 void MyFrame::OnBold(wxCommandEvent
& WXUNUSED(event
))
940 m_richTextCtrl
->ApplyBoldToSelection();
943 void MyFrame::OnItalic(wxCommandEvent
& WXUNUSED(event
))
945 m_richTextCtrl
->ApplyItalicToSelection();
948 void MyFrame::OnUnderline(wxCommandEvent
& WXUNUSED(event
))
950 m_richTextCtrl
->ApplyUnderlineToSelection();
954 void MyFrame::OnUpdateBold(wxUpdateUIEvent
& event
)
956 event
.Check(m_richTextCtrl
->IsSelectionBold());
959 void MyFrame::OnUpdateItalic(wxUpdateUIEvent
& event
)
961 event
.Check(m_richTextCtrl
->IsSelectionItalics());
964 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent
& event
)
966 event
.Check(m_richTextCtrl
->IsSelectionUnderlined());
969 void MyFrame::OnAlignLeft(wxCommandEvent
& WXUNUSED(event
))
971 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT
);
974 void MyFrame::OnAlignCentre(wxCommandEvent
& WXUNUSED(event
))
976 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE
);
979 void MyFrame::OnAlignRight(wxCommandEvent
& WXUNUSED(event
))
981 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT
);
984 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent
& event
)
986 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT
));
989 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent
& event
)
991 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE
));
994 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent
& event
)
996 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT
));
999 void MyFrame::OnFont(wxCommandEvent
& WXUNUSED(event
))
1001 wxRichTextRange range
;
1002 if (m_richTextCtrl
->HasSelection())
1003 range
= m_richTextCtrl
->GetSelectionRange();
1005 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
1007 int pages
= wxRICHTEXT_FORMAT_FONT
;
1009 wxRichTextFormattingDialog
formatDlg(pages
, this);
1010 formatDlg
.GetStyle(m_richTextCtrl
, range
);
1012 if (formatDlg
.ShowModal() == wxID_OK
)
1014 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
1017 // Old method using wxFontDialog
1019 if (!m_richTextCtrl
->HasSelection())
1022 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
1023 wxFontData fontData
;
1026 attr
.SetFlags(wxTEXT_ATTR_FONT
);
1028 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1029 fontData
.SetInitialFont(attr
.GetFont());
1031 wxFontDialog
dialog(this, fontData
);
1032 if (dialog
.ShowModal() == wxID_OK
)
1034 fontData
= dialog
.GetFontData();
1035 attr
.SetFlags(wxTEXT_ATTR_FONT
);
1036 attr
.SetFont(fontData
.GetChosenFont());
1037 if (attr
.GetFont().Ok())
1039 m_richTextCtrl
->SetStyle(range
, attr
);
1045 void MyFrame::OnParagraph(wxCommandEvent
& WXUNUSED(event
))
1047 wxRichTextRange range
;
1048 if (m_richTextCtrl
->HasSelection())
1049 range
= m_richTextCtrl
->GetSelectionRange();
1051 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
1053 int pages
= wxRICHTEXT_FORMAT_INDENTS_SPACING
|wxRICHTEXT_FORMAT_TABS
|wxRICHTEXT_FORMAT_BULLETS
;
1055 wxRichTextFormattingDialog
formatDlg(pages
, this);
1056 formatDlg
.GetStyle(m_richTextCtrl
, range
);
1058 if (formatDlg
.ShowModal() == wxID_OK
)
1060 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
1064 void MyFrame::OnFormat(wxCommandEvent
& WXUNUSED(event
))
1066 wxRichTextRange range
;
1067 if (m_richTextCtrl
->HasSelection())
1068 range
= m_richTextCtrl
->GetSelectionRange();
1070 range
= wxRichTextRange(0, m_richTextCtrl
->GetLastPosition()+1);
1072 int pages
= wxRICHTEXT_FORMAT_FONT
|wxRICHTEXT_FORMAT_INDENTS_SPACING
|wxRICHTEXT_FORMAT_TABS
|wxRICHTEXT_FORMAT_BULLETS
;
1074 wxRichTextFormattingDialog
formatDlg(pages
, this);
1075 formatDlg
.GetStyle(m_richTextCtrl
, range
);
1077 if (formatDlg
.ShowModal() == wxID_OK
)
1079 formatDlg
.ApplyStyle(m_richTextCtrl
, range
);
1083 void MyFrame::OnUpdateFormat(wxUpdateUIEvent
& event
)
1085 event
.Enable(m_richTextCtrl
->HasSelection());
1088 void MyFrame::OnIndentMore(wxCommandEvent
& WXUNUSED(event
))
1091 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1093 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1095 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1096 if (m_richTextCtrl
->HasSelection())
1097 range
= m_richTextCtrl
->GetSelectionRange();
1099 wxFontData fontData
;
1100 attr
.SetLeftIndent(attr
.GetLeftIndent() + 100);
1102 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1103 m_richTextCtrl
->SetStyle(range
, attr
);
1107 void MyFrame::OnIndentLess(wxCommandEvent
& WXUNUSED(event
))
1110 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
1112 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1114 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1115 if (m_richTextCtrl
->HasSelection())
1116 range
= m_richTextCtrl
->GetSelectionRange();
1118 if (attr
.GetLeftIndent() >= 100)
1120 wxFontData fontData
;
1121 attr
.SetLeftIndent(attr
.GetLeftIndent() - 100);
1123 m_richTextCtrl
->SetStyle(range
, attr
);
1128 void MyFrame::OnLineSpacingHalf(wxCommandEvent
& WXUNUSED(event
))
1131 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1133 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1135 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1136 if (m_richTextCtrl
->HasSelection())
1137 range
= m_richTextCtrl
->GetSelectionRange();
1139 wxFontData fontData
;
1140 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1141 attr
.SetLineSpacing(15);
1143 m_richTextCtrl
->SetStyle(range
, attr
);
1147 void MyFrame::OnLineSpacingDouble(wxCommandEvent
& WXUNUSED(event
))
1150 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1152 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1154 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1155 if (m_richTextCtrl
->HasSelection())
1156 range
= m_richTextCtrl
->GetSelectionRange();
1158 wxFontData fontData
;
1159 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1160 attr
.SetLineSpacing(20);
1162 m_richTextCtrl
->SetStyle(range
, attr
);
1166 void MyFrame::OnLineSpacingSingle(wxCommandEvent
& WXUNUSED(event
))
1169 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1171 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1173 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1174 if (m_richTextCtrl
->HasSelection())
1175 range
= m_richTextCtrl
->GetSelectionRange();
1177 wxFontData fontData
;
1178 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
1179 attr
.SetLineSpacing(0); // Can also use 10
1181 m_richTextCtrl
->SetStyle(range
, attr
);
1185 void MyFrame::OnParagraphSpacingMore(wxCommandEvent
& WXUNUSED(event
))
1188 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1190 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1192 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1193 if (m_richTextCtrl
->HasSelection())
1194 range
= m_richTextCtrl
->GetSelectionRange();
1196 wxFontData fontData
;
1197 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() + 20);
1199 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1200 m_richTextCtrl
->SetStyle(range
, attr
);
1204 void MyFrame::OnParagraphSpacingLess(wxCommandEvent
& WXUNUSED(event
))
1207 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1209 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1211 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1212 if (m_richTextCtrl
->HasSelection())
1213 range
= m_richTextCtrl
->GetSelectionRange();
1215 if (attr
.GetParagraphSpacingAfter() >= 20)
1217 wxFontData fontData
;
1218 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() - 20);
1220 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1221 m_richTextCtrl
->SetStyle(range
, attr
);
1226 void MyFrame::OnViewHTML(wxCommandEvent
& WXUNUSED(event
))
1228 wxDialog
dialog(this, wxID_ANY
, _("HTML"), wxDefaultPosition
, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE
);
1230 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
1231 dialog
.SetSizer(boxSizer
);
1233 wxHtmlWindow
* win
= new wxHtmlWindow(& dialog
, wxID_ANY
, wxDefaultPosition
, wxSize(500, 400), wxSUNKEN_BORDER
);
1234 boxSizer
->Add(win
, 1, wxALL
, 5);
1236 wxButton
* cancelButton
= new wxButton(& dialog
, wxID_CANCEL
, wxT("&Close"));
1237 boxSizer
->Add(cancelButton
, 0, wxALL
|wxCENTRE
, 5);
1240 wxStringOutputStream
strStream(& text
);
1242 wxRichTextHTMLHandler htmlHandler
;
1243 if (htmlHandler
.SaveFile(& m_richTextCtrl
->GetBuffer(), strStream
))
1248 boxSizer
->Fit(& dialog
);
1253 // Demonstrates how you can change the style sheets and have the changes
1254 // reflected in the control content without wiping out character formatting.
1256 void MyFrame::OnSwitchStyleSheets(wxCommandEvent
& WXUNUSED(event
))
1258 static wxRichTextStyleSheet
* gs_AlternateStyleSheet
= NULL
;
1260 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1261 wxRichTextStyleListBox
* styleList
= (wxRichTextStyleListBox
*) FindWindow(ID_RICHTEXT_STYLE_LIST
);
1262 wxRichTextStyleComboCtrl
* styleCombo
= (wxRichTextStyleComboCtrl
*) FindWindow(ID_RICHTEXT_STYLE_COMBO
);
1264 wxRichTextStyleSheet
* sheet
= ctrl
->GetStyleSheet();
1266 // One-time creation of an alternate style sheet
1267 if (!gs_AlternateStyleSheet
)
1269 gs_AlternateStyleSheet
= new wxRichTextStyleSheet(*sheet
);
1271 // Make some modifications
1272 for (int i
= 0; i
< (int) gs_AlternateStyleSheet
->GetParagraphStyleCount(); i
++)
1274 wxRichTextParagraphStyleDefinition
* def
= gs_AlternateStyleSheet
->GetParagraphStyle(i
);
1276 if (def
->GetStyle().HasTextColour())
1277 def
->GetStyle().SetTextColour(*wxBLUE
);
1279 if (def
->GetStyle().HasAlignment())
1281 if (def
->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1282 def
->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1283 else if (def
->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1284 def
->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1286 if (def
->GetStyle().HasLeftIndent())
1288 def
->GetStyle().SetLeftIndent(def
->GetStyle().GetLeftIndent() * 2);
1294 wxRichTextStyleSheet
* tmp
= gs_AlternateStyleSheet
;
1295 gs_AlternateStyleSheet
= sheet
;
1298 ctrl
->SetStyleSheet(sheet
);
1299 ctrl
->ApplyStyleSheet(sheet
); // Makes the control reflect the new style definitions
1301 styleList
->SetStyleSheet(sheet
);
1302 styleList
->UpdateStyles();
1304 styleCombo
->SetStyleSheet(sheet
);
1305 styleCombo
->UpdateStyles();
1308 void MyFrame::OnInsertSymbol(wxCommandEvent
& WXUNUSED(event
))
1310 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1313 attr
.SetFlags(wxTEXT_ATTR_FONT
);
1314 ctrl
->GetStyle(ctrl
->GetInsertionPoint(), attr
);
1316 wxString currentFontName
;
1317 if (attr
.HasFont() && attr
.GetFont().Ok())
1318 currentFontName
= attr
.GetFont().GetFaceName();
1320 // Don't set the initial font in the dialog (so the user is choosing
1321 // 'normal text', i.e. the current font) but do tell the dialog
1322 // what 'normal text' is.
1324 wxSymbolPickerDialog
dlg(wxT("*"), wxEmptyString
, currentFontName
, this);
1326 if (dlg
.ShowModal() == wxID_OK
)
1328 if (dlg
.HasSelection())
1330 long insertionPoint
= ctrl
->GetInsertionPoint();
1332 ctrl
->WriteText(dlg
.GetSymbol());
1334 if (!dlg
.UseNormalFont())
1336 wxFont
font(attr
.GetFont());
1337 font
.SetFaceName(dlg
.GetFontName());
1339 ctrl
->SetStyle(insertionPoint
, insertionPoint
+1, attr
);
1345 void MyFrame::OnNumberList(wxCommandEvent
& WXUNUSED(event
))
1347 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1349 if (ctrl
->HasSelection())
1351 wxRichTextRange range
= ctrl
->GetSelectionRange();
1352 ctrl
->SetListStyle(range
, wxT("Numbered List 1"), wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_RENUMBER
);
1356 void MyFrame::OnItemizeList(wxCommandEvent
& WXUNUSED(event
))
1358 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1360 if (ctrl
->HasSelection())
1362 wxRichTextRange range
= ctrl
->GetSelectionRange();
1363 ctrl
->SetListStyle(range
, wxT("Bullet List 1"));
1367 void MyFrame::OnRenumberList(wxCommandEvent
& WXUNUSED(event
))
1369 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1371 if (ctrl
->HasSelection())
1373 wxRichTextRange range
= ctrl
->GetSelectionRange();
1374 ctrl
->NumberList(range
, NULL
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_RENUMBER
);
1378 void MyFrame::OnPromoteList(wxCommandEvent
& WXUNUSED(event
))
1380 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1382 if (ctrl
->HasSelection())
1384 wxRichTextRange range
= ctrl
->GetSelectionRange();
1385 ctrl
->PromoteList(1, range
, NULL
);
1389 void MyFrame::OnDemoteList(wxCommandEvent
& WXUNUSED(event
))
1391 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1393 if (ctrl
->HasSelection())
1395 wxRichTextRange range
= ctrl
->GetSelectionRange();
1396 ctrl
->PromoteList(-1, range
, NULL
);
1400 void MyFrame::OnClearList(wxCommandEvent
& WXUNUSED(event
))
1402 wxRichTextCtrl
* ctrl
= (wxRichTextCtrl
*) FindWindow(ID_RICHTEXT_CTRL
);
1404 if (ctrl
->HasSelection())
1406 wxRichTextRange range
= ctrl
->GetSelectionRange();
1407 ctrl
->ClearListStyle(range
);