1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/richtext/richtext.cpp
3 // Purpose: wxWidgets rich text editor sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
33 #include "wx/fontdlg.h"
34 #include "wx/splitter.h"
35 #include "wx/sstream.h"
36 #include "wx/html/htmlwin.h"
39 #include "../sample.xpm"
42 #include "bitmaps/smiley.xpm"
43 // #include "bitmaps/idea.xpm"
44 #include "bitmaps/zebra.xpm"
46 #include "bitmaps/open.xpm"
47 #include "bitmaps/save.xpm"
48 #include "bitmaps/copy.xpm"
49 #include "bitmaps/cut.xpm"
50 #include "bitmaps/paste.xpm"
51 #include "bitmaps/undo.xpm"
52 #include "bitmaps/redo.xpm"
53 #include "bitmaps/bold.xpm"
54 #include "bitmaps/italic.xpm"
55 #include "bitmaps/underline.xpm"
57 #include "bitmaps/alignleft.xpm"
58 #include "bitmaps/alignright.xpm"
59 #include "bitmaps/centre.xpm"
60 #include "bitmaps/font.xpm"
61 #include "bitmaps/indentless.xpm"
62 #include "bitmaps/indentmore.xpm"
64 #include "wx/richtext/richtextctrl.h"
65 #include "wx/richtext/richtextstyles.h"
66 #include "wx/richtext/richtextxml.h"
67 #include "wx/richtext/richtexthtml.h"
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 // Define a new application type, each program should derive a class from wxApp
78 class MyApp
: public wxApp
81 // override base class virtuals
82 // ----------------------------
84 // this one is called on application startup and is a good place for the app
85 // initialization (doing it here and not in the ctor allows to have an error
86 // return: if OnInit() returns false, the application terminates)
87 virtual bool OnInit();
92 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
94 wxRichTextStyleSheet
* m_styleSheet
;
97 // Define a new frame type: this is going to be our main frame
98 class MyFrame
: public wxFrame
102 MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
= wxDefaultPosition
,
103 const wxSize
& size
= wxDefaultSize
, long style
= wxDEFAULT_FRAME_STYLE
);
105 // event handlers (these functions should _not_ be virtual)
106 void OnQuit(wxCommandEvent
& event
);
107 void OnAbout(wxCommandEvent
& event
);
109 void OnOpen(wxCommandEvent
& event
);
110 void OnSave(wxCommandEvent
& event
);
111 void OnSaveAs(wxCommandEvent
& event
);
113 void OnBold(wxCommandEvent
& event
);
114 void OnItalic(wxCommandEvent
& event
);
115 void OnUnderline(wxCommandEvent
& event
);
117 void OnUpdateBold(wxUpdateUIEvent
& event
);
118 void OnUpdateItalic(wxUpdateUIEvent
& event
);
119 void OnUpdateUnderline(wxUpdateUIEvent
& event
);
121 void OnAlignLeft(wxCommandEvent
& event
);
122 void OnAlignCentre(wxCommandEvent
& event
);
123 void OnAlignRight(wxCommandEvent
& event
);
125 void OnUpdateAlignLeft(wxUpdateUIEvent
& event
);
126 void OnUpdateAlignCentre(wxUpdateUIEvent
& event
);
127 void OnUpdateAlignRight(wxUpdateUIEvent
& event
);
129 void OnFont(wxCommandEvent
& event
);
130 void OnIndentMore(wxCommandEvent
& event
);
131 void OnIndentLess(wxCommandEvent
& event
);
133 void OnLineSpacingHalf(wxCommandEvent
& event
);
134 void OnLineSpacingDouble(wxCommandEvent
& event
);
135 void OnLineSpacingSingle(wxCommandEvent
& event
);
137 void OnParagraphSpacingMore(wxCommandEvent
& event
);
138 void OnParagraphSpacingLess(wxCommandEvent
& event
);
140 void OnViewHTML(wxCommandEvent
& event
);
142 // Forward command events to the current rich text control, if any
143 bool ProcessEvent(wxEvent
& event
);
146 // any class wishing to process wxWidgets events must use this macro
147 DECLARE_EVENT_TABLE()
149 wxRichTextCtrl
* m_richTextCtrl
;
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 // IDs for the controls and the menu commands
161 ID_About
= wxID_ABOUT
,
163 ID_FORMAT_BOLD
= 100,
168 ID_FORMAT_ALIGN_LEFT
,
169 ID_FORMAT_ALIGN_CENTRE
,
170 ID_FORMAT_ALIGN_RIGHT
,
172 ID_FORMAT_INDENT_MORE
,
173 ID_FORMAT_INDENT_LESS
,
175 ID_FORMAT_PARAGRAPH_SPACING_MORE
,
176 ID_FORMAT_PARAGRAPH_SPACING_LESS
,
178 ID_FORMAT_LINE_SPACING_HALF
,
179 ID_FORMAT_LINE_SPACING_DOUBLE
,
180 ID_FORMAT_LINE_SPACING_SINGLE
,
185 // ----------------------------------------------------------------------------
186 // event tables and other macros for wxWidgets
187 // ----------------------------------------------------------------------------
189 // the event tables connect the wxWidgets events with the functions (event
190 // handlers) which process them. It can be also done at run-time, but for the
191 // simple menu events like this the static method is much simpler.
192 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
193 EVT_MENU(ID_Quit
, MyFrame::OnQuit
)
194 EVT_MENU(ID_About
, MyFrame::OnAbout
)
196 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
197 EVT_MENU(wxID_SAVE
, MyFrame::OnSave
)
198 EVT_MENU(wxID_SAVEAS
, MyFrame::OnSaveAs
)
200 EVT_MENU(ID_FORMAT_BOLD
, MyFrame::OnBold
)
201 EVT_MENU(ID_FORMAT_ITALIC
, MyFrame::OnItalic
)
202 EVT_MENU(ID_FORMAT_UNDERLINE
, MyFrame::OnUnderline
)
204 EVT_UPDATE_UI(ID_FORMAT_BOLD
, MyFrame::OnUpdateBold
)
205 EVT_UPDATE_UI(ID_FORMAT_ITALIC
, MyFrame::OnUpdateItalic
)
206 EVT_UPDATE_UI(ID_FORMAT_UNDERLINE
, MyFrame::OnUpdateUnderline
)
208 EVT_MENU(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnAlignLeft
)
209 EVT_MENU(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnAlignCentre
)
210 EVT_MENU(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnAlignRight
)
212 EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT
, MyFrame::OnUpdateAlignLeft
)
213 EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE
, MyFrame::OnUpdateAlignCentre
)
214 EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT
, MyFrame::OnUpdateAlignRight
)
216 EVT_MENU(ID_FORMAT_FONT
, MyFrame::OnFont
)
217 EVT_MENU(ID_FORMAT_INDENT_MORE
, MyFrame::OnIndentMore
)
218 EVT_MENU(ID_FORMAT_INDENT_LESS
, MyFrame::OnIndentLess
)
220 EVT_MENU(ID_FORMAT_LINE_SPACING_HALF
, MyFrame::OnLineSpacingHalf
)
221 EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE
, MyFrame::OnLineSpacingSingle
)
222 EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE
, MyFrame::OnLineSpacingDouble
)
224 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE
, MyFrame::OnParagraphSpacingMore
)
225 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS
, MyFrame::OnParagraphSpacingLess
)
227 EVT_MENU(ID_VIEW_HTML
, MyFrame::OnViewHTML
)
230 // Create a new application object: this macro will allow wxWidgets to create
231 // the application object during program execution (it's better than using a
232 // static object for many reasons) and also implements the accessor function
233 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
237 // ============================================================================
239 // ============================================================================
241 // ----------------------------------------------------------------------------
242 // the application class
243 // ----------------------------------------------------------------------------
245 // 'Main program' equivalent: the program execution "starts" here
248 m_styleSheet
= new wxRichTextStyleSheet
;
252 // Add extra handlers (plain text is automatically added)
253 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler
);
254 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler
);
256 // Add image handlers
258 wxImage::AddHandler( new wxPNGHandler
);
262 wxImage::AddHandler( new wxJPEGHandler
);
266 wxImage::AddHandler( new wxGIFHandler
);
269 // create the main application window
270 MyFrame
*frame
= new MyFrame(_T("wxRichTextCtrl Sample"), wxID_ANY
, wxDefaultPosition
, wxSize(600, 500));
272 // and show it (the frames, unlike simple controls, are not shown when
273 // created initially)
276 // success: wxApp::OnRun() will be called which will enter the main message
277 // loop and the application will run. If we returned false here, the
278 // application would exit immediately.
288 void MyApp::CreateStyles()
292 wxFont
romanFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
293 wxFont
swissFont(12, wxSWISS
, wxNORMAL
, wxNORMAL
);
295 wxRichTextParagraphStyleDefinition
* normalPara
= new wxRichTextParagraphStyleDefinition(wxT("Normal"));
296 wxRichTextAttr normalAttr
;
297 normalAttr
.SetFontFaceName(romanFont
.GetFaceName());
298 normalAttr
.SetFontSize(12);
299 // Let's set all attributes for this style
300 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
|
301 wxTEXT_ATTR_PARA_SPACING_BEFORE
|wxTEXT_ATTR_PARA_SPACING_AFTER
|wxTEXT_ATTR_LINE_SPACING
|
302 wxTEXT_ATTR_BULLET_STYLE
|wxTEXT_ATTR_BULLET_NUMBER
);
303 normalPara
->SetStyle(normalAttr
);
305 m_styleSheet
->AddParagraphStyle(normalPara
);
307 wxRichTextParagraphStyleDefinition
* indentedPara
= new wxRichTextParagraphStyleDefinition(wxT("Indented"));
308 wxRichTextAttr indentedAttr
;
309 indentedAttr
.SetFontFaceName(romanFont
.GetFaceName());
310 indentedAttr
.SetFontSize(12);
311 indentedAttr
.SetLeftIndent(100, 0);
312 // We only want to affect indentation
313 indentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
314 indentedPara
->SetStyle(indentedAttr
);
316 m_styleSheet
->AddParagraphStyle(indentedPara
);
318 wxRichTextParagraphStyleDefinition
* indentedPara2
= new wxRichTextParagraphStyleDefinition(wxT("Red Bold Indented"));
319 wxRichTextAttr indentedAttr2
;
320 indentedAttr2
.SetFontFaceName(romanFont
.GetFaceName());
321 indentedAttr2
.SetFontSize(12);
322 indentedAttr2
.SetFontWeight(wxBOLD
);
323 indentedAttr2
.SetTextColour(*wxRED
);
324 indentedAttr2
.SetFontSize(12);
325 indentedAttr2
.SetLeftIndent(100, 0);
326 // We want to affect indentation, font and text colour
327 indentedAttr2
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
|wxTEXT_ATTR_FONT
|wxTEXT_ATTR_TEXT_COLOUR
);
328 indentedPara2
->SetStyle(indentedAttr2
);
330 m_styleSheet
->AddParagraphStyle(indentedPara2
);
332 wxRichTextParagraphStyleDefinition
* flIndentedPara
= new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
333 wxRichTextAttr flIndentedAttr
;
334 flIndentedAttr
.SetFontFaceName(swissFont
.GetFaceName());
335 flIndentedAttr
.SetFontSize(12);
336 flIndentedAttr
.SetLeftIndent(100, -100);
337 // We only want to affect indentation
338 flIndentedAttr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
|wxTEXT_ATTR_RIGHT_INDENT
);
339 flIndentedPara
->SetStyle(flIndentedAttr
);
341 m_styleSheet
->AddParagraphStyle(flIndentedPara
);
345 wxRichTextCharacterStyleDefinition
* boldDef
= new wxRichTextCharacterStyleDefinition(wxT("Bold"));
346 wxRichTextAttr boldAttr
;
347 boldAttr
.SetFontFaceName(romanFont
.GetFaceName());
348 boldAttr
.SetFontSize(12);
349 boldAttr
.SetFontWeight(wxBOLD
);
350 // We only want to affect boldness
351 boldAttr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
352 boldDef
->SetStyle(boldAttr
);
354 m_styleSheet
->AddCharacterStyle(boldDef
);
356 wxRichTextCharacterStyleDefinition
* italicDef
= new wxRichTextCharacterStyleDefinition(wxT("Italic"));
357 wxRichTextAttr italicAttr
;
358 italicAttr
.SetFontFaceName(romanFont
.GetFaceName());
359 italicAttr
.SetFontSize(12);
360 italicAttr
.SetFontStyle(wxITALIC
);
361 // We only want to affect italics
362 italicAttr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
363 italicDef
->SetStyle(italicAttr
);
365 m_styleSheet
->AddCharacterStyle(italicDef
);
367 wxRichTextCharacterStyleDefinition
* redDef
= new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
368 wxRichTextAttr redAttr
;
369 redAttr
.SetFontFaceName(romanFont
.GetFaceName());
370 redAttr
.SetFontSize(12);
371 redAttr
.SetFontWeight(wxBOLD
);
372 redAttr
.SetTextColour(*wxRED
);
373 // We only want to affect colour, weight and face
374 redAttr
.SetFlags(wxTEXT_ATTR_FONT_FACE
|wxTEXT_ATTR_FONT_WEIGHT
|wxTEXT_ATTR_TEXT_COLOUR
);
375 redDef
->SetStyle(redAttr
);
377 m_styleSheet
->AddCharacterStyle(redDef
);
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
385 MyFrame::MyFrame(const wxString
& title
, wxWindowID id
, const wxPoint
& pos
,
386 const wxSize
& size
, long style
)
387 : wxFrame(NULL
, id
, title
, pos
, size
, style
)
389 // set the frame icon
390 SetIcon(wxICON(sample
));
393 wxMenu
*fileMenu
= new wxMenu
;
395 // the "About" item should be in the help menu
396 wxMenu
*helpMenu
= new wxMenu
;
397 helpMenu
->Append(ID_About
, _T("&About...\tF1"), _T("Show about dialog"));
399 fileMenu
->Append(wxID_OPEN
, _T("&Open\tCtrl+O"), _T("Open a file"));
400 fileMenu
->Append(wxID_SAVE
, _T("&Save\tCtrl+S"), _T("Save a file"));
401 fileMenu
->Append(wxID_SAVEAS
, _T("&Save As...\tF12"), _T("Save to a new file"));
402 fileMenu
->AppendSeparator();
403 fileMenu
->Append(ID_VIEW_HTML
, _T("&View as HTML"), _T("View HTML"));
404 fileMenu
->AppendSeparator();
405 fileMenu
->Append(ID_Quit
, _T("E&xit\tAlt+X"), _T("Quit this program"));
407 wxMenu
* editMenu
= new wxMenu
;
408 editMenu
->Append(wxID_UNDO
, _("&Undo\tCtrl+Z"));
409 editMenu
->Append(wxID_REDO
, _("&Redo\tCtrl+Y"));
410 editMenu
->AppendSeparator();
411 editMenu
->Append(wxID_CUT
, _("Cu&t\tCtrl+X"));
412 editMenu
->Append(wxID_COPY
, _("&Copy\tCtrl+C"));
413 editMenu
->Append(wxID_PASTE
, _("&Paste\tCtrl+V"));
415 editMenu
->Append(wxID_CLEAR
, _("&Delete\tDel"));
417 editMenu
->AppendSeparator();
418 editMenu
->Append(wxID_SELECTALL
, _("Select A&ll\tCtrl+A"));
420 editMenu
->AppendSeparator();
421 editMenu
->Append(wxID_FIND
, _("&Find...\tCtrl+F"));
422 editMenu
->Append(stID_FIND_REPLACE
, _("&Replace...\tCtrl+R"));
425 wxMenu
* formatMenu
= new wxMenu
;
426 formatMenu
->AppendCheckItem(ID_FORMAT_BOLD
, _("&Bold\tCtrl+B"));
427 formatMenu
->AppendCheckItem(ID_FORMAT_ITALIC
, _("&Italic\tCtrl+I"));
428 formatMenu
->AppendCheckItem(ID_FORMAT_UNDERLINE
, _("&Underline\tCtrl+U"));
429 formatMenu
->AppendSeparator();
430 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_LEFT
, _("L&eft Align"));
431 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT
, _("&Right Align"));
432 formatMenu
->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE
, _("&Centre"));
433 formatMenu
->AppendSeparator();
434 formatMenu
->Append(ID_FORMAT_INDENT_MORE
, _("Indent &More"));
435 formatMenu
->Append(ID_FORMAT_INDENT_LESS
, _("Indent &Less"));
436 formatMenu
->AppendSeparator();
437 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE
, _("Increase Paragraph &Spacing"));
438 formatMenu
->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS
, _("Decrease &Paragraph Spacing"));
439 formatMenu
->AppendSeparator();
440 formatMenu
->Append(ID_FORMAT_LINE_SPACING_SINGLE
, _("Normal Line Spacing"));
441 formatMenu
->Append(ID_FORMAT_LINE_SPACING_HALF
, _("1.5 Line Spacing"));
442 formatMenu
->Append(ID_FORMAT_LINE_SPACING_DOUBLE
, _("Double Line Spacing"));
443 formatMenu
->AppendSeparator();
444 formatMenu
->Append(ID_FORMAT_FONT
, _("&Font..."));
446 // now append the freshly created menu to the menu bar...
447 wxMenuBar
*menuBar
= new wxMenuBar();
448 menuBar
->Append(fileMenu
, _T("&File"));
449 menuBar
->Append(editMenu
, _T("&Edit"));
450 menuBar
->Append(formatMenu
, _T("F&ormat"));
451 menuBar
->Append(helpMenu
, _T("&Help"));
453 // ... and attach this menu bar to the frame
456 // create a status bar just for fun (by default with 1 pane only)
457 // but don't create it on limited screen space (WinCE)
458 bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
464 SetStatusText(_T("Welcome to wxRichTextCtrl!"));
468 wxToolBar
* toolBar
= CreateToolBar();
470 toolBar
->AddTool(wxID_OPEN
, wxBitmap(open_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Open"));
471 toolBar
->AddTool(wxID_SAVEAS
, wxBitmap(save_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Save"));
472 toolBar
->AddSeparator();
473 toolBar
->AddTool(wxID_CUT
, wxBitmap(cut_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Cut"));
474 toolBar
->AddTool(wxID_COPY
, wxBitmap(copy_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Copy"));
475 toolBar
->AddTool(wxID_PASTE
, wxBitmap(paste_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Paste"));
476 toolBar
->AddSeparator();
477 toolBar
->AddTool(wxID_UNDO
, wxBitmap(undo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Undo"));
478 toolBar
->AddTool(wxID_REDO
, wxBitmap(redo_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Redo"));
479 toolBar
->AddSeparator();
480 toolBar
->AddTool(ID_FORMAT_BOLD
, wxBitmap(bold_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Bold"));
481 toolBar
->AddTool(ID_FORMAT_ITALIC
, wxBitmap(italic_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Italic"));
482 toolBar
->AddTool(ID_FORMAT_UNDERLINE
, wxBitmap(underline_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Underline"));
483 toolBar
->AddSeparator();
484 toolBar
->AddTool(ID_FORMAT_ALIGN_LEFT
, wxBitmap(alignleft_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Left"));
485 toolBar
->AddTool(ID_FORMAT_ALIGN_CENTRE
, wxBitmap(centre_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Centre"));
486 toolBar
->AddTool(ID_FORMAT_ALIGN_RIGHT
, wxBitmap(alignright_xpm
), wxNullBitmap
, true, -1, -1, (wxObject
*) NULL
, _("Align Right"));
487 toolBar
->AddSeparator();
488 toolBar
->AddTool(ID_FORMAT_INDENT_LESS
, wxBitmap(indentless_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent Less"));
489 toolBar
->AddTool(ID_FORMAT_INDENT_MORE
, wxBitmap(indentmore_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Indent More"));
490 toolBar
->AddSeparator();
491 toolBar
->AddTool(ID_FORMAT_FONT
, wxBitmap(font_xpm
), wxNullBitmap
, false, -1, -1, (wxObject
*) NULL
, _("Font"));
495 wxSplitterWindow
* splitter
= new wxSplitterWindow(this, wxID_ANY
, wxDefaultPosition
, GetClientSize(), wxSP_NO_XP_THEME
|wxSP_3D
|wxSP_LIVE_UPDATE
);
497 wxFont textFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
498 wxFont boldFont
= wxFont(12, wxROMAN
, wxNORMAL
, wxBOLD
);
499 wxFont italicFont
= wxFont(12, wxROMAN
, wxITALIC
, wxNORMAL
);
501 m_richTextCtrl
= new wxRichTextCtrl(splitter
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxSize(200, 200), wxVSCROLL
|wxHSCROLL
|wxNO_BORDER
|wxWANTS_CHARS
);
502 wxFont
font(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
504 m_richTextCtrl
->SetFont(font
);
506 wxRichTextStyleListBox
* styleListBox
= new wxRichTextStyleListBox(splitter
, wxID_ANY
);
508 wxSize display
= wxGetDisplaySize();
509 if ( is_pda
&& ( display
.GetWidth() < display
.GetHeight() ) )
511 splitter
->SplitHorizontally(m_richTextCtrl
, styleListBox
);
515 splitter
->SplitVertically(m_richTextCtrl
, styleListBox
, 400);
518 splitter
->UpdateSize();
520 styleListBox
->SetStyleSheet(wxGetApp().GetStyleSheet());
521 styleListBox
->SetRichTextCtrl(m_richTextCtrl
);
522 styleListBox
->UpdateStyles();
524 wxRichTextCtrl
& r
= *m_richTextCtrl
;
526 r
.BeginSuppressUndo();
528 r
.BeginParagraphSpacing(0, 20);
530 r
.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE
);
534 r
.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
539 r
.WriteText(wxT("by Julian Smart"));
545 r
.WriteImage(wxBitmap(zebra_xpm
));
552 r
.WriteText(wxT("What can you do with this thing? "));
553 r
.WriteImage(wxBitmap(smiley_xpm
));
554 r
.WriteText(wxT(" Well, you can change text "));
556 r
.BeginTextColour(wxColour(255, 0, 0));
557 r
.WriteText(wxT("colour, like this red bit."));
560 r
.BeginTextColour(wxColour(0, 0, 255));
561 r
.WriteText(wxT(" And this blue bit."));
564 r
.WriteText(wxT(" Naturally you can make things "));
566 r
.WriteText(wxT("bold "));
569 r
.WriteText(wxT("or italic "));
572 r
.WriteText(wxT("or underlined."));
576 r
.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
579 r
.WriteText(wxT(" Next we'll show an indented paragraph."));
581 r
.BeginLeftIndent(60);
584 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."));
589 r
.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
591 r
.BeginLeftIndent(100, -40);
594 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."));
599 r
.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
601 r
.BeginNumberedBullet(1, 100, 60);
604 r
.WriteText(wxT("This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later."));
605 r
.EndNumberedBullet();
607 r
.BeginNumberedBullet(2, 100, 60);
610 r
.WriteText(wxT("This is my second item."));
611 r
.EndNumberedBullet();
615 r
.WriteText(wxT("The following paragraph is right-indented:"));
617 r
.BeginRightIndent(200);
620 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."));
625 r
.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
627 r
.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT
);
628 r
.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF
);
631 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."));
641 attr
.SetFlags(wxTEXT_ATTR_TABS
);
643 r
.SetDefaultStyle(attr
);
646 r
.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
649 r
.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
651 r
.BeginSymbolBullet(wxT('*'), 100, 60);
653 r
.WriteText(wxT("Compatibility with wxTextCtrl API"));
656 r
.BeginSymbolBullet(wxT('*'), 100, 60);
658 r
.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
661 r
.BeginSymbolBullet(wxT('*'), 100, 60);
663 r
.WriteText(wxT("XML loading and saving"));
666 r
.BeginSymbolBullet(wxT('*'), 100, 60);
668 r
.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
671 r
.BeginSymbolBullet(wxT('*'), 100, 60);
673 r
.WriteText(wxT("Clipboard copy and paste"));
676 r
.BeginSymbolBullet(wxT('*'), 100, 60);
678 r
.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
681 r
.BeginSymbolBullet(wxT('*'), 100, 60);
683 r
.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
688 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!"));
690 r
.EndParagraphSpacing();
698 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
700 // true is to force the frame to close
704 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
707 msg
.Printf( _T("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
708 wxMessageBox(msg
, _T("About wxRichTextCtrl Sample"), wxOK
| wxICON_INFORMATION
, this);
711 // Forward command events to the current rich text control, if any
712 bool MyFrame::ProcessEvent(wxEvent
& event
)
714 if (event
.IsCommandEvent() && !event
.IsKindOf(CLASSINFO(wxChildFocusEvent
)))
716 // Problem: we can get infinite recursion because the events
717 // climb back up to this frame, and repeat.
718 // Assume that command events don't cause another command event
719 // to be called, so we can rely on inCommand not being overwritten
721 static int s_eventType
= 0;
722 static wxWindowID s_id
= 0;
724 if (s_id
!= event
.GetId() && s_eventType
!= event
.GetEventType())
726 s_eventType
= event
.GetEventType();
727 s_id
= event
.GetId();
729 wxWindow
* focusWin
= wxFindFocusDescendant(this);
730 if (focusWin
&& focusWin
->ProcessEvent(event
))
747 return wxFrame::ProcessEvent(event
);
750 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
754 wxArrayInt fileTypes
;
756 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes
);
759 filter
+= wxT("All files (*.*)|*.*");
761 wxFileDialog
dialog(this,
762 _("Choose a filename"),
768 if (dialog
.ShowModal() == wxID_OK
)
770 wxString path
= dialog
.GetPath();
774 int filterIndex
= dialog
.GetFilterIndex();
775 int fileType
= (filterIndex
< (int) fileTypes
.GetCount())
776 ? fileTypes
[filterIndex
]
777 : wxRICHTEXT_TYPE_TEXT
;
778 m_richTextCtrl
->LoadFile(path
, fileType
);
783 void MyFrame::OnSave(wxCommandEvent
& event
)
785 if (m_richTextCtrl
->GetFilename().empty())
790 m_richTextCtrl
->SaveFile();
793 void MyFrame::OnSaveAs(wxCommandEvent
& WXUNUSED(event
))
795 wxString filter
= wxRichTextBuffer::GetExtWildcard(false, true);
799 wxFileDialog
dialog(this,
800 _("Choose a filename"),
806 if (dialog
.ShowModal() == wxID_OK
)
808 wxString path
= dialog
.GetPath();
812 m_richTextCtrl
->SaveFile(path
);
817 void MyFrame::OnBold(wxCommandEvent
& WXUNUSED(event
))
819 m_richTextCtrl
->ApplyBoldToSelection();
822 void MyFrame::OnItalic(wxCommandEvent
& WXUNUSED(event
))
824 m_richTextCtrl
->ApplyItalicToSelection();
827 void MyFrame::OnUnderline(wxCommandEvent
& WXUNUSED(event
))
829 m_richTextCtrl
->ApplyUnderlineToSelection();
833 void MyFrame::OnUpdateBold(wxUpdateUIEvent
& event
)
835 event
.Check(m_richTextCtrl
->IsSelectionBold());
838 void MyFrame::OnUpdateItalic(wxUpdateUIEvent
& event
)
840 event
.Check(m_richTextCtrl
->IsSelectionItalics());
843 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent
& event
)
845 event
.Check(m_richTextCtrl
->IsSelectionUnderlined());
848 void MyFrame::OnAlignLeft(wxCommandEvent
& WXUNUSED(event
))
850 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT
);
853 void MyFrame::OnAlignCentre(wxCommandEvent
& WXUNUSED(event
))
855 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE
);
858 void MyFrame::OnAlignRight(wxCommandEvent
& WXUNUSED(event
))
860 m_richTextCtrl
->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT
);
863 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent
& event
)
865 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT
));
868 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent
& event
)
870 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE
));
873 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent
& event
)
875 event
.Check(m_richTextCtrl
->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT
));
878 void MyFrame::OnFont(wxCommandEvent
& WXUNUSED(event
))
880 if (!m_richTextCtrl
->HasSelection())
883 wxRichTextRange range
= m_richTextCtrl
->GetSelectionRange();
887 attr
.SetFlags(wxTEXT_ATTR_FONT
);
889 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
890 fontData
.SetInitialFont(attr
.GetFont());
892 wxFontDialog
dialog(this, fontData
);
893 if (dialog
.ShowModal() == wxID_OK
)
895 fontData
= dialog
.GetFontData();
896 attr
.SetFlags(wxTEXT_ATTR_FONT
);
897 attr
.SetFont(fontData
.GetChosenFont());
898 if (attr
.GetFont().Ok())
900 m_richTextCtrl
->SetStyle(range
, attr
);
905 void MyFrame::OnIndentMore(wxCommandEvent
& WXUNUSED(event
))
908 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
910 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
912 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
913 if (m_richTextCtrl
->HasSelection())
914 range
= m_richTextCtrl
->GetSelectionRange();
917 attr
.SetLeftIndent(attr
.GetLeftIndent() + 100);
919 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
920 m_richTextCtrl
->SetStyle(range
, attr
);
924 void MyFrame::OnIndentLess(wxCommandEvent
& WXUNUSED(event
))
927 attr
.SetFlags(wxTEXT_ATTR_LEFT_INDENT
);
929 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
931 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
932 if (m_richTextCtrl
->HasSelection())
933 range
= m_richTextCtrl
->GetSelectionRange();
935 if (attr
.GetLeftIndent() >= 100)
938 attr
.SetLeftIndent(attr
.GetLeftIndent() - 100);
940 m_richTextCtrl
->SetStyle(range
, attr
);
945 void MyFrame::OnLineSpacingHalf(wxCommandEvent
& WXUNUSED(event
))
948 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
950 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
952 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
953 if (m_richTextCtrl
->HasSelection())
954 range
= m_richTextCtrl
->GetSelectionRange();
957 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
958 attr
.SetLineSpacing(15);
960 m_richTextCtrl
->SetStyle(range
, attr
);
964 void MyFrame::OnLineSpacingDouble(wxCommandEvent
& WXUNUSED(event
))
967 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
969 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
971 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
972 if (m_richTextCtrl
->HasSelection())
973 range
= m_richTextCtrl
->GetSelectionRange();
976 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
977 attr
.SetLineSpacing(20);
979 m_richTextCtrl
->SetStyle(range
, attr
);
983 void MyFrame::OnLineSpacingSingle(wxCommandEvent
& WXUNUSED(event
))
986 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
988 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
990 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
991 if (m_richTextCtrl
->HasSelection())
992 range
= m_richTextCtrl
->GetSelectionRange();
995 attr
.SetFlags(wxTEXT_ATTR_LINE_SPACING
);
996 attr
.SetLineSpacing(0); // Can also use 10
998 m_richTextCtrl
->SetStyle(range
, attr
);
1002 void MyFrame::OnParagraphSpacingMore(wxCommandEvent
& WXUNUSED(event
))
1005 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1007 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1009 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1010 if (m_richTextCtrl
->HasSelection())
1011 range
= m_richTextCtrl
->GetSelectionRange();
1013 wxFontData fontData
;
1014 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() + 20);
1016 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1017 m_richTextCtrl
->SetStyle(range
, attr
);
1021 void MyFrame::OnParagraphSpacingLess(wxCommandEvent
& WXUNUSED(event
))
1024 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1026 if (m_richTextCtrl
->GetStyle(m_richTextCtrl
->GetInsertionPoint(), attr
))
1028 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
1029 if (m_richTextCtrl
->HasSelection())
1030 range
= m_richTextCtrl
->GetSelectionRange();
1032 if (attr
.GetParagraphSpacingAfter() >= 20)
1034 wxFontData fontData
;
1035 attr
.SetParagraphSpacingAfter(attr
.GetParagraphSpacingAfter() - 20);
1037 attr
.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER
);
1038 m_richTextCtrl
->SetStyle(range
, attr
);
1043 void MyFrame::OnViewHTML(wxCommandEvent
& WXUNUSED(event
))
1045 wxDialog
dialog(this, wxID_ANY
, _("HTML"), wxDefaultPosition
, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE
);
1047 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
1048 dialog
.SetSizer(boxSizer
);
1050 wxHtmlWindow
* win
= new wxHtmlWindow(& dialog
, wxID_ANY
, wxDefaultPosition
, wxSize(500, 400), wxSUNKEN_BORDER
);
1051 boxSizer
->Add(win
, 1, wxALL
, 5);
1053 wxButton
* cancelButton
= new wxButton(& dialog
, wxID_CANCEL
, wxT("&Close"));
1054 boxSizer
->Add(cancelButton
, 0, wxALL
|wxCENTRE
, 5);
1057 wxStringOutputStream
strStream(& text
);
1059 wxRichTextHTMLHandler htmlHandler
;
1060 if (htmlHandler
.SaveFile(& m_richTextCtrl
->GetBuffer(), strStream
))
1065 boxSizer
->Fit(& dialog
);