Switched antialiasing off since GetTextExtent doesn't work properly
[wxWidgets.git] / samples / richtext / richtext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richedit.cpp
3 // Purpose: wxWidgets rich text editor sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2005-10-02
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/fontdlg.h"
34 #include "wx/splitter.h"
35 #include "wx/sstream.h"
36 #include "wx/html/htmlwin.h"
37
38 #include "bitmaps/sample.xpm"
39 #include "bitmaps/smiley.xpm"
40 // #include "bitmaps/idea.xpm"
41 #include "bitmaps/zebra.xpm"
42
43 #include "bitmaps/open.xpm"
44 #include "bitmaps/save.xpm"
45 #include "bitmaps/copy.xpm"
46 #include "bitmaps/cut.xpm"
47 #include "bitmaps/paste.xpm"
48 #include "bitmaps/undo.xpm"
49 #include "bitmaps/redo.xpm"
50 #include "bitmaps/bold.xpm"
51 #include "bitmaps/italic.xpm"
52 #include "bitmaps/underline.xpm"
53
54 #include "bitmaps/alignleft.xpm"
55 #include "bitmaps/alignright.xpm"
56 #include "bitmaps/centre.xpm"
57 #include "bitmaps/font.xpm"
58 #include "bitmaps/indentless.xpm"
59 #include "bitmaps/indentmore.xpm"
60
61 #include "wx/richtext/richtextctrl.h"
62 #include "wx/richtext/richtextstyles.h"
63 #include "wx/richtext/richtextxml.h"
64
65 // ----------------------------------------------------------------------------
66 // resources
67 // ----------------------------------------------------------------------------
68
69 // ----------------------------------------------------------------------------
70 // private classes
71 // ----------------------------------------------------------------------------
72
73 // Define a new application type, each program should derive a class from wxApp
74 class MyApp : public wxApp
75 {
76 public:
77 // override base class virtuals
78 // ----------------------------
79
80 // this one is called on application startup and is a good place for the app
81 // initialization (doing it here and not in the ctor allows to have an error
82 // return: if OnInit() returns false, the application terminates)
83 virtual bool OnInit();
84 virtual int OnExit();
85
86 void CreateStyles();
87
88 wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
89
90 wxRichTextStyleSheet* m_styleSheet;
91 };
92
93 // Define a new frame type: this is going to be our main frame
94 class MyFrame : public wxFrame
95 {
96 public:
97 // ctor(s)
98 MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
99 const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE);
100
101 // event handlers (these functions should _not_ be virtual)
102 void OnQuit(wxCommandEvent& event);
103 void OnAbout(wxCommandEvent& event);
104
105 void OnOpen(wxCommandEvent& event);
106 void OnSave(wxCommandEvent& event);
107 void OnSaveAs(wxCommandEvent& event);
108
109 void OnBold(wxCommandEvent& event);
110 void OnItalic(wxCommandEvent& event);
111 void OnUnderline(wxCommandEvent& event);
112
113 void OnUpdateBold(wxUpdateUIEvent& event);
114 void OnUpdateItalic(wxUpdateUIEvent& event);
115 void OnUpdateUnderline(wxUpdateUIEvent& event);
116
117 void OnAlignLeft(wxCommandEvent& event);
118 void OnAlignCentre(wxCommandEvent& event);
119 void OnAlignRight(wxCommandEvent& event);
120
121 void OnUpdateAlignLeft(wxUpdateUIEvent& event);
122 void OnUpdateAlignCentre(wxUpdateUIEvent& event);
123 void OnUpdateAlignRight(wxUpdateUIEvent& event);
124
125 void OnFont(wxCommandEvent& event);
126 void OnIndentMore(wxCommandEvent& event);
127 void OnIndentLess(wxCommandEvent& event);
128
129 void OnLineSpacingHalf(wxCommandEvent& event);
130 void OnLineSpacingDouble(wxCommandEvent& event);
131 void OnLineSpacingSingle(wxCommandEvent& event);
132
133 void OnParagraphSpacingMore(wxCommandEvent& event);
134 void OnParagraphSpacingLess(wxCommandEvent& event);
135
136 void OnViewHTML(wxCommandEvent& event);
137
138 // Forward command events to the current rich text control, if any
139 bool ProcessEvent(wxEvent& event);
140
141 private:
142 // any class wishing to process wxWidgets events must use this macro
143 DECLARE_EVENT_TABLE()
144
145 wxRichTextCtrl* m_richTextCtrl;
146 };
147
148 // ----------------------------------------------------------------------------
149 // constants
150 // ----------------------------------------------------------------------------
151
152 // IDs for the controls and the menu commands
153 enum
154 {
155 // menu items
156 ID_Quit = wxID_EXIT,
157 ID_About = wxID_ABOUT,
158
159 ID_FORMAT_BOLD = 100,
160 ID_FORMAT_ITALIC,
161 ID_FORMAT_UNDERLINE,
162 ID_FORMAT_FONT,
163
164 ID_FORMAT_ALIGN_LEFT,
165 ID_FORMAT_ALIGN_CENTRE,
166 ID_FORMAT_ALIGN_RIGHT,
167
168 ID_FORMAT_INDENT_MORE,
169 ID_FORMAT_INDENT_LESS,
170
171 ID_FORMAT_PARAGRAPH_SPACING_MORE,
172 ID_FORMAT_PARAGRAPH_SPACING_LESS,
173
174 ID_FORMAT_LINE_SPACING_HALF,
175 ID_FORMAT_LINE_SPACING_DOUBLE,
176 ID_FORMAT_LINE_SPACING_SINGLE,
177
178 ID_VIEW_HTML
179 };
180
181 // ----------------------------------------------------------------------------
182 // event tables and other macros for wxWidgets
183 // ----------------------------------------------------------------------------
184
185 // the event tables connect the wxWidgets events with the functions (event
186 // handlers) which process them. It can be also done at run-time, but for the
187 // simple menu events like this the static method is much simpler.
188 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
189 EVT_MENU(ID_Quit, MyFrame::OnQuit)
190 EVT_MENU(ID_About, MyFrame::OnAbout)
191
192 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
193 EVT_MENU(wxID_SAVE, MyFrame::OnSave)
194 EVT_MENU(wxID_SAVEAS, MyFrame::OnSaveAs)
195
196 EVT_MENU(ID_FORMAT_BOLD, MyFrame::OnBold)
197 EVT_MENU(ID_FORMAT_ITALIC, MyFrame::OnItalic)
198 EVT_MENU(ID_FORMAT_UNDERLINE, MyFrame::OnUnderline)
199
200 EVT_UPDATE_UI(ID_FORMAT_BOLD, MyFrame::OnUpdateBold)
201 EVT_UPDATE_UI(ID_FORMAT_ITALIC, MyFrame::OnUpdateItalic)
202 EVT_UPDATE_UI(ID_FORMAT_UNDERLINE, MyFrame::OnUpdateUnderline)
203
204 EVT_MENU(ID_FORMAT_ALIGN_LEFT, MyFrame::OnAlignLeft)
205 EVT_MENU(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnAlignCentre)
206 EVT_MENU(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnAlignRight)
207
208 EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT, MyFrame::OnUpdateAlignLeft)
209 EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnUpdateAlignCentre)
210 EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnUpdateAlignRight)
211
212 EVT_MENU(ID_FORMAT_FONT, MyFrame::OnFont)
213 EVT_MENU(ID_FORMAT_INDENT_MORE, MyFrame::OnIndentMore)
214 EVT_MENU(ID_FORMAT_INDENT_LESS, MyFrame::OnIndentLess)
215
216 EVT_MENU(ID_FORMAT_LINE_SPACING_HALF, MyFrame::OnLineSpacingHalf)
217 EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE, MyFrame::OnLineSpacingSingle)
218 EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE, MyFrame::OnLineSpacingDouble)
219
220 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE, MyFrame::OnParagraphSpacingMore)
221 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS, MyFrame::OnParagraphSpacingLess)
222
223 EVT_MENU(ID_VIEW_HTML, MyFrame::OnViewHTML)
224 END_EVENT_TABLE()
225
226 // Create a new application object: this macro will allow wxWidgets to create
227 // the application object during program execution (it's better than using a
228 // static object for many reasons) and also implements the accessor function
229 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
230 // not wxApp)
231 IMPLEMENT_APP(MyApp)
232
233 // ============================================================================
234 // implementation
235 // ============================================================================
236
237 // ----------------------------------------------------------------------------
238 // the application class
239 // ----------------------------------------------------------------------------
240
241 // 'Main program' equivalent: the program execution "starts" here
242 bool MyApp::OnInit()
243 {
244 m_styleSheet = new wxRichTextStyleSheet;
245
246 CreateStyles();
247
248 // Add extra handlers (plain text is automatically added)
249 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler);
250 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler);
251
252 // Add image handlers
253 #if wxUSE_LIBPNG
254 wxImage::AddHandler( new wxPNGHandler );
255 #endif
256
257 #if wxUSE_LIBJPEG
258 wxImage::AddHandler( new wxJPEGHandler );
259 #endif
260
261 #if wxUSE_GIF
262 wxImage::AddHandler( new wxGIFHandler );
263 #endif
264
265 // create the main application window
266 MyFrame *frame = new MyFrame(_T("wxRichTextCtrl Sample"), wxID_ANY, wxDefaultPosition, wxSize(600, 500));
267
268 // and show it (the frames, unlike simple controls, are not shown when
269 // created initially)
270 frame->Show(true);
271
272 // success: wxApp::OnRun() will be called which will enter the main message
273 // loop and the application will run. If we returned false here, the
274 // application would exit immediately.
275 return true;
276 }
277
278 int MyApp::OnExit()
279 {
280 delete m_styleSheet;
281 return 0;
282 }
283
284 void MyApp::CreateStyles()
285 {
286 // Paragraph styles
287
288 wxFont romanFont(12, wxROMAN, wxNORMAL, wxNORMAL);
289 wxFont swissFont(12, wxSWISS, wxNORMAL, wxNORMAL);
290
291 wxRichTextParagraphStyleDefinition* normalPara = new wxRichTextParagraphStyleDefinition(wxT("Normal"));
292 wxRichTextAttr normalAttr;
293 normalAttr.SetFontFaceName(romanFont.GetFaceName());
294 normalAttr.SetFontSize(12);
295 // Let's set all attributes for this style
296 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|
297 wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|
298 wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER);
299 normalPara->SetStyle(normalAttr);
300
301 m_styleSheet->AddParagraphStyle(normalPara);
302
303 wxRichTextParagraphStyleDefinition* indentedPara = new wxRichTextParagraphStyleDefinition(wxT("Indented"));
304 wxRichTextAttr indentedAttr;
305 indentedAttr.SetFontFaceName(romanFont.GetFaceName());
306 indentedAttr.SetFontSize(12);
307 indentedAttr.SetLeftIndent(100, 0);
308 // We only want to affect indentation
309 indentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
310 indentedPara->SetStyle(indentedAttr);
311
312 m_styleSheet->AddParagraphStyle(indentedPara);
313
314 wxRichTextParagraphStyleDefinition* flIndentedPara = new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
315 wxRichTextAttr flIndentedAttr;
316 flIndentedAttr.SetFontFaceName(swissFont.GetFaceName());
317 flIndentedAttr.SetFontSize(12);
318 flIndentedAttr.SetLeftIndent(100, -100);
319 // We only want to affect indentation
320 flIndentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
321 flIndentedPara->SetStyle(flIndentedAttr);
322
323 m_styleSheet->AddParagraphStyle(flIndentedPara);
324
325 // Character styles
326
327 wxRichTextCharacterStyleDefinition* boldDef = new wxRichTextCharacterStyleDefinition(wxT("Bold"));
328 wxRichTextAttr boldAttr;
329 boldAttr.SetFontFaceName(romanFont.GetFaceName());
330 boldAttr.SetFontSize(12);
331 boldAttr.SetFontWeight(wxBOLD);
332 // We only want to affect boldness
333 boldAttr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
334 boldDef->SetStyle(boldAttr);
335
336 m_styleSheet->AddCharacterStyle(boldDef);
337
338 wxRichTextCharacterStyleDefinition* italicDef = new wxRichTextCharacterStyleDefinition(wxT("Italic"));
339 wxRichTextAttr italicAttr;
340 italicAttr.SetFontFaceName(romanFont.GetFaceName());
341 italicAttr.SetFontSize(12);
342 italicAttr.SetFontStyle(wxITALIC);
343 // We only want to affect italics
344 italicAttr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
345 italicDef->SetStyle(italicAttr);
346
347 m_styleSheet->AddCharacterStyle(italicDef);
348
349 wxRichTextCharacterStyleDefinition* redDef = new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
350 wxRichTextAttr redAttr;
351 redAttr.SetFontFaceName(romanFont.GetFaceName());
352 redAttr.SetFontSize(12);
353 redAttr.SetFontWeight(wxBOLD);
354 redAttr.SetTextColour(*wxRED);
355 // We only want to affect colour, weight and face
356 redAttr.SetFlags(wxTEXT_ATTR_FONT_FACE|wxTEXT_ATTR_FONT_WEIGHT|wxTEXT_ATTR_TEXT_COLOUR);
357 redDef->SetStyle(redAttr);
358
359 m_styleSheet->AddCharacterStyle(redDef);
360 }
361
362 // ----------------------------------------------------------------------------
363 // main frame
364 // ----------------------------------------------------------------------------
365
366 // frame constructor
367 MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
368 const wxSize& size, long style)
369 : wxFrame(NULL, id, title, pos, size, style)
370 {
371 // set the frame icon
372 SetIcon(sample_xpm);
373
374 // create a menu bar
375 wxMenu *fileMenu = new wxMenu;
376
377 // the "About" item should be in the help menu
378 wxMenu *helpMenu = new wxMenu;
379 helpMenu->Append(ID_About, _T("&About...\tF1"), _T("Show about dialog"));
380
381 fileMenu->Append(wxID_OPEN, _T("&Open\tCtrl+O"), _T("Open a file"));
382 fileMenu->Append(wxID_SAVE, _T("&Save\tCtrl+S"), _T("Save a file"));
383 fileMenu->Append(wxID_SAVEAS, _T("&Save As...\tF12"), _T("Save to a new file"));
384 fileMenu->AppendSeparator();
385 fileMenu->Append(ID_VIEW_HTML, _T("&View as HTML"), _T("View HTML"));
386 fileMenu->AppendSeparator();
387 fileMenu->Append(ID_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
388
389 wxMenu* editMenu = new wxMenu;
390 editMenu->Append(wxID_UNDO, _("&Undo\tCtrl+Z"));
391 editMenu->Append(wxID_REDO, _("&Redo\tCtrl+Y"));
392 editMenu->AppendSeparator();
393 editMenu->Append(wxID_CUT, _("Cu&t\tCtrl+X"));
394 editMenu->Append(wxID_COPY, _("&Copy\tCtrl+C"));
395 editMenu->Append(wxID_PASTE, _("&Paste\tCtrl+V"));
396
397 editMenu->Append(wxID_CLEAR, _("&Delete\tDel"));
398
399 editMenu->AppendSeparator();
400 editMenu->Append(wxID_SELECTALL, _("Select A&ll\tCtrl+A"));
401 #if 0
402 editMenu->AppendSeparator();
403 editMenu->Append(wxID_FIND, _("&Find...\tCtrl+F"));
404 editMenu->Append(stID_FIND_REPLACE, _("&Replace...\tCtrl+R"));
405 #endif
406
407 wxMenu* formatMenu = new wxMenu;
408 formatMenu->AppendCheckItem(ID_FORMAT_BOLD, _("&Bold\tCtrl+B"));
409 formatMenu->AppendCheckItem(ID_FORMAT_ITALIC, _("&Italic\tCtrl+I"));
410 formatMenu->AppendCheckItem(ID_FORMAT_UNDERLINE, _("&Underline\tCtrl+U"));
411 formatMenu->AppendSeparator();
412 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_LEFT, _("L&eft Align"));
413 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT, _("&Right Align"));
414 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE, _("&Centre"));
415 formatMenu->AppendSeparator();
416 formatMenu->Append(ID_FORMAT_INDENT_MORE, _("I&ndent &More"));
417 formatMenu->Append(ID_FORMAT_INDENT_LESS, _("Indent &Less"));
418 formatMenu->AppendSeparator();
419 formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE, _("Increase Paragraph &Spacing"));
420 formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS, _("Decrease &Paragraph Spacing"));
421 formatMenu->AppendSeparator();
422 formatMenu->Append(ID_FORMAT_LINE_SPACING_SINGLE, _("Normal Line Spacing"));
423 formatMenu->Append(ID_FORMAT_LINE_SPACING_HALF, _("1.5 Line Spacing"));
424 formatMenu->Append(ID_FORMAT_LINE_SPACING_DOUBLE, _("Double Line Spacing"));
425 formatMenu->AppendSeparator();
426 formatMenu->Append(ID_FORMAT_FONT, _("&Font..."));
427
428 // now append the freshly created menu to the menu bar...
429 wxMenuBar *menuBar = new wxMenuBar();
430 menuBar->Append(fileMenu, _T("&File"));
431 menuBar->Append(editMenu, _T("&Edit"));
432 menuBar->Append(formatMenu, _T("F&ormat"));
433 menuBar->Append(helpMenu, _T("&Help"));
434
435 // ... and attach this menu bar to the frame
436 SetMenuBar(menuBar);
437
438 // create a status bar just for fun (by default with 1 pane only)
439 CreateStatusBar(2);
440 SetStatusText(_T("Welcome to wxRichTextCtrl!"));
441
442 wxToolBar* toolBar = CreateToolBar();
443
444 toolBar->AddTool(wxID_OPEN, wxBitmap(open_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Open"));
445 toolBar->AddTool(wxID_SAVEAS, wxBitmap(save_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Save"));
446 toolBar->AddSeparator();
447 toolBar->AddTool(wxID_CUT, wxBitmap(cut_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Cut"));
448 toolBar->AddTool(wxID_COPY, wxBitmap(copy_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Copy"));
449 toolBar->AddTool(wxID_PASTE, wxBitmap(paste_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Paste"));
450 toolBar->AddSeparator();
451 toolBar->AddTool(wxID_UNDO, wxBitmap(undo_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Undo"));
452 toolBar->AddTool(wxID_REDO, wxBitmap(redo_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Redo"));
453 toolBar->AddSeparator();
454 toolBar->AddTool(ID_FORMAT_BOLD, wxBitmap(bold_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Bold"));
455 toolBar->AddTool(ID_FORMAT_ITALIC, wxBitmap(italic_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Italic"));
456 toolBar->AddTool(ID_FORMAT_UNDERLINE, wxBitmap(underline_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Underline"));
457 toolBar->AddSeparator();
458 toolBar->AddTool(ID_FORMAT_ALIGN_LEFT, wxBitmap(alignleft_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Align Left"));
459 toolBar->AddTool(ID_FORMAT_ALIGN_CENTRE, wxBitmap(centre_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Centre"));
460 toolBar->AddTool(ID_FORMAT_ALIGN_RIGHT, wxBitmap(alignright_xpm), wxNullBitmap, TRUE, -1, -1, (wxObject *) NULL, _("Align Right"));
461 toolBar->AddSeparator();
462 toolBar->AddTool(ID_FORMAT_INDENT_LESS, wxBitmap(indentless_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Indent Less"));
463 toolBar->AddTool(ID_FORMAT_INDENT_MORE, wxBitmap(indentmore_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Indent More"));
464 toolBar->AddSeparator();
465 toolBar->AddTool(ID_FORMAT_FONT, wxBitmap(font_xpm), wxNullBitmap, FALSE, -1, -1, (wxObject *) NULL, _("Font"));
466
467 toolBar->Realize();
468
469 wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxSize(100, 100), wxSP_NO_XP_THEME|wxSP_3D|wxSP_LIVE_UPDATE);
470
471
472 wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
473 wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
474 wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL);
475
476 m_richTextCtrl = new wxRichTextCtrl(splitter, wxID_ANY, wxDefaultPosition, wxSize(200, 200), wxVSCROLL|wxHSCROLL|wxNO_BORDER);
477 wxFont font(12, wxROMAN, wxNORMAL, wxNORMAL);
478
479 #ifdef __WXMAC__
480 font.SetNoAntiAliasing(true);
481 #endif
482 m_richTextCtrl->SetFont(font);
483
484 wxRichTextStyleListBox* styleListBox = new wxRichTextStyleListBox(splitter, wxID_ANY);
485 splitter->SplitVertically(m_richTextCtrl, styleListBox, 400);
486
487 styleListBox->SetStyleSheet(wxGetApp().GetStyleSheet());
488 styleListBox->SetRichTextCtrl(m_richTextCtrl);
489 styleListBox->UpdateStyles();
490
491 wxRichTextCtrl& r = *m_richTextCtrl;
492
493 r.BeginSuppressUndo();
494
495 r.BeginParagraphSpacing(0, 20);
496
497 r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
498 r.BeginBold();
499
500 r.BeginFontSize(14);
501 r.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
502 r.EndFontSize();
503 r.Newline();
504
505 r.BeginItalic();
506 r.WriteText(wxT("by Julian Smart"));
507 r.EndItalic();
508
509 r.EndBold();
510
511 r.Newline();
512 r.WriteImage(wxBitmap(zebra_xpm));
513
514 r.EndAlignment();
515
516 r.Newline();
517 r.Newline();
518
519 r.WriteText(wxT("What can you do with this thing? "));
520 r.WriteImage(wxBitmap(smiley_xpm));
521 r.WriteText(wxT(" Well, you can change text "));
522
523 r.BeginTextColour(wxColour(255, 0, 0));
524 r.WriteText(wxT("colour, like this red bit."));
525 r.EndTextColour();
526
527 r.BeginTextColour(wxColour(0, 0, 255));
528 r.WriteText(wxT(" And this blue bit."));
529 r.EndTextColour();
530
531 r.WriteText(wxT(" Naturally you can make things "));
532 r.BeginBold();
533 r.WriteText(wxT("bold "));
534 r.EndBold();
535 r.BeginItalic();
536 r.WriteText(wxT("or italic "));
537 r.EndItalic();
538 r.BeginUnderline();
539 r.WriteText(wxT("or underlined."));
540 r.EndUnderline();
541
542 r.BeginFontSize(14);
543 r.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
544 r.EndFontSize();
545
546 r.WriteText(wxT(" Next we'll show an indented paragraph."));
547
548 r.BeginLeftIndent(60);
549 r.Newline();
550
551 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."));
552 r.EndLeftIndent();
553
554 r.Newline();
555
556 r.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
557
558 r.BeginLeftIndent(100, -40);
559 r.Newline();
560
561 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."));
562 r.EndLeftIndent();
563
564 r.Newline();
565
566 r.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
567
568 r.BeginNumberedBullet(1, 100, 60);
569 r.Newline();
570
571 r.WriteText(wxT("This is my first item. Note that wxRichTextCtrl doesn't automatically do numbering, but this will be added later."));
572 r.EndNumberedBullet();
573
574 r.BeginNumberedBullet(2, 100, 60);
575 r.Newline();
576
577 r.WriteText(wxT("This is my second item."));
578 r.EndNumberedBullet();
579
580 r.Newline();
581
582 r.WriteText(wxT("The following paragraph is right-indented:"));
583
584 r.BeginRightIndent(200);
585 r.Newline();
586
587 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."));
588 r.EndRightIndent();
589
590 r.Newline();
591
592 r.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
593
594 r.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT);
595 r.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF);
596 r.Newline();
597
598 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.EndLineSpacing();
600 r.EndAlignment();
601
602 r.Newline();
603 r.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
604
605 r.BeginSymbolBullet(wxT('*'), 100, 60);
606 r.Newline();
607 r.WriteText(wxT("Compatibility with wxTextCtrl API"));
608 r.EndSymbolBullet();
609
610 r.BeginSymbolBullet(wxT('*'), 100, 60);
611 r.Newline();
612 r.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
613 r.EndSymbolBullet();
614
615 r.BeginSymbolBullet(wxT('*'), 100, 60);
616 r.Newline();
617 r.WriteText(wxT("XML loading and saving"));
618 r.EndSymbolBullet();
619
620 r.BeginSymbolBullet(wxT('*'), 100, 60);
621 r.Newline();
622 r.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
623 r.EndSymbolBullet();
624
625 r.BeginSymbolBullet(wxT('*'), 100, 60);
626 r.Newline();
627 r.WriteText(wxT("Clipboard copy and paste"));
628 r.EndSymbolBullet();
629
630 r.BeginSymbolBullet(wxT('*'), 100, 60);
631 r.Newline();
632 r.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
633 r.EndSymbolBullet();
634
635 r.BeginSymbolBullet(wxT('*'), 100, 60);
636 r.Newline();
637 r.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
638 r.EndSymbolBullet();
639
640 r.Newline();
641
642 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!"));
643
644 r.EndParagraphSpacing();
645
646 r.EndSuppressUndo();
647 }
648
649
650 // event handlers
651
652 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
653 {
654 // true is to force the frame to close
655 Close(true);
656 }
657
658 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
659 {
660 wxString msg;
661 msg.Printf( _T("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
662 wxMessageBox(msg, _T("About wxRichTextCtrl Sample"), wxOK | wxICON_INFORMATION, this);
663 }
664
665 // Forward command events to the current rich text control, if any
666 bool MyFrame::ProcessEvent(wxEvent& event)
667 {
668 if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent)))
669 {
670 // Problem: we can get infinite recursion because the events
671 // climb back up to this frame, and repeat.
672 // Assume that command events don't cause another command event
673 // to be called, so we can rely on inCommand not being overwritten
674
675 static int s_eventType = 0;
676 static wxWindowID s_id = 0;
677
678 if (s_id != event.GetId() && s_eventType != event.GetEventType())
679 {
680 s_eventType = event.GetEventType();
681 s_id = event.GetId();
682
683 wxWindow* focusWin = wxFindFocusDescendant(this);
684 if (focusWin && focusWin->ProcessEvent(event))
685 {
686 //s_command = NULL;
687 s_eventType = 0;
688 s_id = 0;
689 return TRUE;
690 }
691
692 s_eventType = 0;
693 s_id = 0;
694 }
695 else
696 {
697 return FALSE;
698 }
699 }
700
701 return wxFrame::ProcessEvent(event);
702 }
703
704 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
705 {
706 wxString path = wxEmptyString;
707 wxString filename = wxEmptyString;
708 wxArrayInt fileTypes;
709
710 wxString filter = wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes);
711 if (!filter.IsEmpty())
712 filter += wxT("|");
713 filter += wxT("All files (*.*)|*.*");
714
715 wxFileDialog dialog(this,
716 _("Choose a filename"),
717 path,
718 filename,
719 filter,
720 wxOPEN);
721
722 if (dialog.ShowModal() == wxID_OK)
723 {
724 wxString path = dialog.GetPath();
725
726 if (!path.IsEmpty())
727 {
728 int fileType = 0;
729 int filterIndex = dialog.GetFilterIndex();
730 if (filterIndex < (int) fileTypes.GetCount())
731 fileType = fileTypes[filterIndex];
732 else
733 fileType = wxRICHTEXT_TYPE_TEXT;
734 m_richTextCtrl->LoadFile(path, fileType);
735 }
736 }
737 }
738
739 void MyFrame::OnSave(wxCommandEvent& event)
740 {
741 if (m_richTextCtrl->GetFilename().IsEmpty())
742 {
743 OnSaveAs(event);
744 return;
745 }
746 m_richTextCtrl->SaveFile();
747 }
748
749 void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
750 {
751 wxString filter = wxRichTextBuffer::GetExtWildcard(false, true);
752 wxString path = wxEmptyString;
753 wxString filename = wxEmptyString;
754
755 wxFileDialog dialog(this,
756 _("Choose a filename"),
757 path,
758 filename,
759 filter,
760 wxSAVE);
761
762 if (dialog.ShowModal() == wxID_OK)
763 {
764 wxString path = dialog.GetPath();
765
766 if (!path.IsEmpty())
767 {
768 m_richTextCtrl->SaveFile(path);
769 }
770 }
771 }
772
773 void MyFrame::OnBold(wxCommandEvent& WXUNUSED(event))
774 {
775 m_richTextCtrl->ApplyBoldToSelection();
776 }
777
778 void MyFrame::OnItalic(wxCommandEvent& WXUNUSED(event))
779 {
780 m_richTextCtrl->ApplyItalicToSelection();
781 }
782
783 void MyFrame::OnUnderline(wxCommandEvent& WXUNUSED(event))
784 {
785 m_richTextCtrl->ApplyUnderlineToSelection();
786 }
787
788
789 void MyFrame::OnUpdateBold(wxUpdateUIEvent& event)
790 {
791 event.Check(m_richTextCtrl->IsSelectionBold());
792 }
793
794 void MyFrame::OnUpdateItalic(wxUpdateUIEvent& event)
795 {
796 event.Check(m_richTextCtrl->IsSelectionItalics());
797 }
798
799 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent& event)
800 {
801 event.Check(m_richTextCtrl->IsSelectionUnderlined());
802 }
803
804 void MyFrame::OnAlignLeft(wxCommandEvent& WXUNUSED(event))
805 {
806 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT);
807 }
808
809 void MyFrame::OnAlignCentre(wxCommandEvent& WXUNUSED(event))
810 {
811 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE);
812 }
813
814 void MyFrame::OnAlignRight(wxCommandEvent& WXUNUSED(event))
815 {
816 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT);
817 }
818
819 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent& event)
820 {
821 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT));
822 }
823
824 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent& event)
825 {
826 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE));
827 }
828
829 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent& event)
830 {
831 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT));
832 }
833
834 void MyFrame::OnFont(wxCommandEvent& WXUNUSED(event))
835 {
836 if (!m_richTextCtrl->HasSelection())
837 return;
838
839 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
840 wxFontData fontData;
841
842 wxTextAttrEx attr;
843 attr.SetFlags(wxTEXT_ATTR_FONT);
844
845 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
846 fontData.SetInitialFont(attr.GetFont());
847
848 wxFontDialog dialog(this, fontData);
849 if (dialog.ShowModal() == wxID_OK)
850 {
851 fontData = dialog.GetFontData();
852 attr.SetFlags(wxTEXT_ATTR_FONT);
853 attr.SetFont(fontData.GetChosenFont());
854 if (attr.GetFont().Ok())
855 {
856 m_richTextCtrl->SetStyle(range, attr);
857 }
858 }
859 }
860
861 void MyFrame::OnIndentMore(wxCommandEvent& WXUNUSED(event))
862 {
863 wxTextAttrEx attr;
864 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
865
866 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
867 {
868 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
869 if (m_richTextCtrl->HasSelection())
870 range = m_richTextCtrl->GetSelectionRange();
871
872 wxFontData fontData;
873 attr.SetLeftIndent(attr.GetLeftIndent() + 100);
874
875 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
876 m_richTextCtrl->SetStyle(range, attr);
877 }
878 }
879
880 void MyFrame::OnIndentLess(wxCommandEvent& WXUNUSED(event))
881 {
882 wxTextAttrEx attr;
883 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
884
885 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
886 {
887 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
888 if (m_richTextCtrl->HasSelection())
889 range = m_richTextCtrl->GetSelectionRange();
890
891 if (attr.GetLeftIndent() >= 100)
892 {
893 wxFontData fontData;
894 attr.SetLeftIndent(attr.GetLeftIndent() - 100);
895
896 m_richTextCtrl->SetStyle(range, attr);
897 }
898 }
899 }
900
901 void MyFrame::OnLineSpacingHalf(wxCommandEvent& WXUNUSED(event))
902 {
903 wxTextAttrEx attr;
904 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
905
906 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
907 {
908 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
909 if (m_richTextCtrl->HasSelection())
910 range = m_richTextCtrl->GetSelectionRange();
911
912 wxFontData fontData;
913 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
914 attr.SetLineSpacing(15);
915
916 m_richTextCtrl->SetStyle(range, attr);
917 }
918 }
919
920 void MyFrame::OnLineSpacingDouble(wxCommandEvent& WXUNUSED(event))
921 {
922 wxTextAttrEx attr;
923 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
924
925 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
926 {
927 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
928 if (m_richTextCtrl->HasSelection())
929 range = m_richTextCtrl->GetSelectionRange();
930
931 wxFontData fontData;
932 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
933 attr.SetLineSpacing(20);
934
935 m_richTextCtrl->SetStyle(range, attr);
936 }
937 }
938
939 void MyFrame::OnLineSpacingSingle(wxCommandEvent& WXUNUSED(event))
940 {
941 wxTextAttrEx attr;
942 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
943
944 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
945 {
946 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
947 if (m_richTextCtrl->HasSelection())
948 range = m_richTextCtrl->GetSelectionRange();
949
950 wxFontData fontData;
951 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
952 attr.SetLineSpacing(0); // Can also use 10
953
954 m_richTextCtrl->SetStyle(range, attr);
955 }
956 }
957
958 void MyFrame::OnParagraphSpacingMore(wxCommandEvent& WXUNUSED(event))
959 {
960 wxTextAttrEx attr;
961 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
962
963 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
964 {
965 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
966 if (m_richTextCtrl->HasSelection())
967 range = m_richTextCtrl->GetSelectionRange();
968
969 wxFontData fontData;
970 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() + 20);
971
972 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
973 m_richTextCtrl->SetStyle(range, attr);
974 }
975 }
976
977 void MyFrame::OnParagraphSpacingLess(wxCommandEvent& WXUNUSED(event))
978 {
979 wxTextAttrEx attr;
980 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
981
982 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
983 {
984 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
985 if (m_richTextCtrl->HasSelection())
986 range = m_richTextCtrl->GetSelectionRange();
987
988 if (attr.GetParagraphSpacingAfter() >= 20)
989 {
990 wxFontData fontData;
991 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() - 20);
992
993 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
994 m_richTextCtrl->SetStyle(range, attr);
995 }
996 }
997 }
998
999 void MyFrame::OnViewHTML(wxCommandEvent& WXUNUSED(event))
1000 {
1001 wxDialog dialog(this, wxID_ANY, _("HTML"), wxDefaultPosition, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE);
1002
1003 wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
1004 dialog.SetSizer(boxSizer);
1005
1006 wxHtmlWindow* win = new wxHtmlWindow(& dialog, wxID_ANY, wxDefaultPosition, wxSize(500, 400), wxSUNKEN_BORDER);
1007 boxSizer->Add(win, 1, wxALL, 5);
1008
1009 wxButton* cancelButton = new wxButton(& dialog, wxID_CANCEL, wxT("&Close"));
1010 boxSizer->Add(cancelButton, 0, wxALL|wxCENTRE, 5);
1011
1012 wxString text;
1013 wxStringOutputStream strStream(& text);
1014
1015 wxRichTextHTMLHandler htmlHandler;
1016 if (htmlHandler.SaveFile(& m_richTextCtrl->GetBuffer(), strStream))
1017 {
1018 win->SetPage(text);
1019 }
1020
1021 boxSizer->Fit(& dialog);
1022
1023 dialog.ShowModal();
1024 }
1025