protect gs_allThreads with a mutex (modified patch 1518719)
[wxWidgets.git] / samples / richtext / richtext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/richtext/richtext.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 #if wxUSE_FILESYSTEM
39 #include "wx/filesys.h"
40 #include "wx/fs_mem.h"
41 #endif
42
43 #if wxUSE_HELP
44 #include "wx/cshelp.h"
45 #endif
46
47 #ifndef __WXMSW__
48 #include "../sample.xpm"
49 #endif
50
51 #include "bitmaps/smiley.xpm"
52 // #include "bitmaps/idea.xpm"
53 #include "bitmaps/zebra.xpm"
54
55 #include "bitmaps/open.xpm"
56 #include "bitmaps/save.xpm"
57 #include "bitmaps/copy.xpm"
58 #include "bitmaps/cut.xpm"
59 #include "bitmaps/paste.xpm"
60 #include "bitmaps/undo.xpm"
61 #include "bitmaps/redo.xpm"
62 #include "bitmaps/bold.xpm"
63 #include "bitmaps/italic.xpm"
64 #include "bitmaps/underline.xpm"
65
66 #include "bitmaps/alignleft.xpm"
67 #include "bitmaps/alignright.xpm"
68 #include "bitmaps/centre.xpm"
69 #include "bitmaps/font.xpm"
70 #include "bitmaps/indentless.xpm"
71 #include "bitmaps/indentmore.xpm"
72
73 #include "wx/richtext/richtextctrl.h"
74 #include "wx/richtext/richtextstyles.h"
75 #include "wx/richtext/richtextxml.h"
76 #include "wx/richtext/richtexthtml.h"
77 #include "wx/richtext/richtextformatdlg.h"
78 #include "wx/richtext/richtextsymboldlg.h"
79 #include "wx/richtext/richtextstyledlg.h"
80 #include "wx/richtext/richtextprint.h"
81
82 // ----------------------------------------------------------------------------
83 // resources
84 // ----------------------------------------------------------------------------
85
86 // ----------------------------------------------------------------------------
87 // private classes
88 // ----------------------------------------------------------------------------
89
90 // Define a new application type, each program should derive a class from wxApp
91 class MyApp : public wxApp
92 {
93 public:
94 // override base class virtuals
95 // ----------------------------
96
97 // this one is called on application startup and is a good place for the app
98 // initialization (doing it here and not in the ctor allows to have an error
99 // return: if OnInit() returns false, the application terminates)
100 virtual bool OnInit();
101 virtual int OnExit();
102
103 void CreateStyles();
104
105 wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
106 wxRichTextPrinting* GetPrinting() const { return m_printing; }
107
108 wxRichTextStyleSheet* m_styleSheet;
109 wxRichTextPrinting* m_printing;
110 };
111
112 // Define a new frame type: this is going to be our main frame
113 class MyFrame : public wxFrame
114 {
115 public:
116 // ctor(s)
117 MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
118 const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE);
119
120 // event handlers (these functions should _not_ be virtual)
121 void OnQuit(wxCommandEvent& event);
122 void OnAbout(wxCommandEvent& event);
123
124 void OnOpen(wxCommandEvent& event);
125 void OnSave(wxCommandEvent& event);
126 void OnSaveAs(wxCommandEvent& event);
127
128 void OnBold(wxCommandEvent& event);
129 void OnItalic(wxCommandEvent& event);
130 void OnUnderline(wxCommandEvent& event);
131
132 void OnUpdateBold(wxUpdateUIEvent& event);
133 void OnUpdateItalic(wxUpdateUIEvent& event);
134 void OnUpdateUnderline(wxUpdateUIEvent& event);
135
136 void OnAlignLeft(wxCommandEvent& event);
137 void OnAlignCentre(wxCommandEvent& event);
138 void OnAlignRight(wxCommandEvent& event);
139
140 void OnUpdateAlignLeft(wxUpdateUIEvent& event);
141 void OnUpdateAlignCentre(wxUpdateUIEvent& event);
142 void OnUpdateAlignRight(wxUpdateUIEvent& event);
143
144 void OnIndentMore(wxCommandEvent& event);
145 void OnIndentLess(wxCommandEvent& event);
146
147 void OnFont(wxCommandEvent& event);
148 void OnParagraph(wxCommandEvent& event);
149 void OnFormat(wxCommandEvent& event);
150 void OnUpdateFormat(wxUpdateUIEvent& event);
151
152 void OnInsertSymbol(wxCommandEvent& event);
153
154 void OnLineSpacingHalf(wxCommandEvent& event);
155 void OnLineSpacingDouble(wxCommandEvent& event);
156 void OnLineSpacingSingle(wxCommandEvent& event);
157
158 void OnParagraphSpacingMore(wxCommandEvent& event);
159 void OnParagraphSpacingLess(wxCommandEvent& event);
160
161 void OnNumberList(wxCommandEvent& event);
162 void OnBulletsAndNumbering(wxCommandEvent& event);
163 void OnItemizeList(wxCommandEvent& event);
164 void OnRenumberList(wxCommandEvent& event);
165 void OnPromoteList(wxCommandEvent& event);
166 void OnDemoteList(wxCommandEvent& event);
167 void OnClearList(wxCommandEvent& event);
168
169 void OnViewHTML(wxCommandEvent& event);
170
171 void OnSwitchStyleSheets(wxCommandEvent& event);
172 void OnManageStyles(wxCommandEvent& event);
173
174 void OnInsertURL(wxCommandEvent& event);
175 void OnURL(wxTextUrlEvent& event);
176 void OnStyleSheetReplacing(wxRichTextEvent& event);
177
178 void OnPrint(wxCommandEvent& event);
179 void OnPreview(wxCommandEvent& event);
180 void OnPageSetup(wxCommandEvent& event);
181
182 // Forward command events to the current rich text control, if any
183 bool ProcessEvent(wxEvent& event);
184
185 private:
186 // any class wishing to process wxWidgets events must use this macro
187 DECLARE_EVENT_TABLE()
188
189 wxRichTextCtrl* m_richTextCtrl;
190 };
191
192 // ----------------------------------------------------------------------------
193 // constants
194 // ----------------------------------------------------------------------------
195
196 // IDs for the controls and the menu commands
197 enum
198 {
199 // menu items
200 ID_Quit = wxID_EXIT,
201 ID_About = wxID_ABOUT,
202
203 ID_FORMAT_BOLD = 100,
204 ID_FORMAT_ITALIC,
205 ID_FORMAT_UNDERLINE,
206 ID_FORMAT_FONT,
207 ID_FORMAT_PARAGRAPH,
208 ID_FORMAT_CONTENT,
209
210 ID_INSERT_SYMBOL,
211 ID_INSERT_URL,
212
213 ID_FORMAT_ALIGN_LEFT,
214 ID_FORMAT_ALIGN_CENTRE,
215 ID_FORMAT_ALIGN_RIGHT,
216
217 ID_FORMAT_INDENT_MORE,
218 ID_FORMAT_INDENT_LESS,
219
220 ID_FORMAT_PARAGRAPH_SPACING_MORE,
221 ID_FORMAT_PARAGRAPH_SPACING_LESS,
222
223 ID_FORMAT_LINE_SPACING_HALF,
224 ID_FORMAT_LINE_SPACING_DOUBLE,
225 ID_FORMAT_LINE_SPACING_SINGLE,
226
227 ID_FORMAT_NUMBER_LIST,
228 ID_FORMAT_BULLETS_AND_NUMBERING,
229 ID_FORMAT_ITEMIZE_LIST,
230 ID_FORMAT_RENUMBER_LIST,
231 ID_FORMAT_PROMOTE_LIST,
232 ID_FORMAT_DEMOTE_LIST,
233 ID_FORMAT_CLEAR_LIST,
234
235 ID_VIEW_HTML,
236 ID_SWITCH_STYLE_SHEETS,
237 ID_MANAGE_STYLES,
238
239 ID_PRINT,
240 ID_PREVIEW,
241 ID_PAGE_SETUP,
242
243 ID_RICHTEXT_CTRL,
244 ID_RICHTEXT_STYLE_LIST,
245 ID_RICHTEXT_STYLE_COMBO
246 };
247
248 // ----------------------------------------------------------------------------
249 // event tables and other macros for wxWidgets
250 // ----------------------------------------------------------------------------
251
252 // the event tables connect the wxWidgets events with the functions (event
253 // handlers) which process them. It can be also done at run-time, but for the
254 // simple menu events like this the static method is much simpler.
255 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
256 EVT_MENU(ID_Quit, MyFrame::OnQuit)
257 EVT_MENU(ID_About, MyFrame::OnAbout)
258
259 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
260 EVT_MENU(wxID_SAVE, MyFrame::OnSave)
261 EVT_MENU(wxID_SAVEAS, MyFrame::OnSaveAs)
262
263 EVT_MENU(ID_FORMAT_BOLD, MyFrame::OnBold)
264 EVT_MENU(ID_FORMAT_ITALIC, MyFrame::OnItalic)
265 EVT_MENU(ID_FORMAT_UNDERLINE, MyFrame::OnUnderline)
266
267 EVT_UPDATE_UI(ID_FORMAT_BOLD, MyFrame::OnUpdateBold)
268 EVT_UPDATE_UI(ID_FORMAT_ITALIC, MyFrame::OnUpdateItalic)
269 EVT_UPDATE_UI(ID_FORMAT_UNDERLINE, MyFrame::OnUpdateUnderline)
270
271 EVT_MENU(ID_FORMAT_ALIGN_LEFT, MyFrame::OnAlignLeft)
272 EVT_MENU(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnAlignCentre)
273 EVT_MENU(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnAlignRight)
274
275 EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT, MyFrame::OnUpdateAlignLeft)
276 EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnUpdateAlignCentre)
277 EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnUpdateAlignRight)
278
279 EVT_MENU(ID_FORMAT_FONT, MyFrame::OnFont)
280 EVT_MENU(ID_FORMAT_PARAGRAPH, MyFrame::OnParagraph)
281 EVT_MENU(ID_FORMAT_CONTENT, MyFrame::OnFormat)
282 EVT_UPDATE_UI(ID_FORMAT_CONTENT, MyFrame::OnUpdateFormat)
283 EVT_UPDATE_UI(ID_FORMAT_FONT, MyFrame::OnUpdateFormat)
284 EVT_UPDATE_UI(ID_FORMAT_PARAGRAPH, MyFrame::OnUpdateFormat)
285 EVT_MENU(ID_FORMAT_INDENT_MORE, MyFrame::OnIndentMore)
286 EVT_MENU(ID_FORMAT_INDENT_LESS, MyFrame::OnIndentLess)
287
288 EVT_MENU(ID_FORMAT_LINE_SPACING_HALF, MyFrame::OnLineSpacingHalf)
289 EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE, MyFrame::OnLineSpacingSingle)
290 EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE, MyFrame::OnLineSpacingDouble)
291
292 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE, MyFrame::OnParagraphSpacingMore)
293 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS, MyFrame::OnParagraphSpacingLess)
294
295 EVT_MENU(ID_INSERT_SYMBOL, MyFrame::OnInsertSymbol)
296 EVT_MENU(ID_INSERT_URL, MyFrame::OnInsertURL)
297
298 EVT_MENU(ID_FORMAT_NUMBER_LIST, MyFrame::OnNumberList)
299 EVT_MENU(ID_FORMAT_BULLETS_AND_NUMBERING, MyFrame::OnBulletsAndNumbering)
300 EVT_MENU(ID_FORMAT_ITEMIZE_LIST, MyFrame::OnItemizeList)
301 EVT_MENU(ID_FORMAT_RENUMBER_LIST, MyFrame::OnRenumberList)
302 EVT_MENU(ID_FORMAT_PROMOTE_LIST, MyFrame::OnPromoteList)
303 EVT_MENU(ID_FORMAT_DEMOTE_LIST, MyFrame::OnDemoteList)
304 EVT_MENU(ID_FORMAT_CLEAR_LIST, MyFrame::OnClearList)
305
306 EVT_MENU(ID_VIEW_HTML, MyFrame::OnViewHTML)
307 EVT_MENU(ID_SWITCH_STYLE_SHEETS, MyFrame::OnSwitchStyleSheets)
308 EVT_MENU(ID_MANAGE_STYLES, MyFrame::OnManageStyles)
309
310 EVT_MENU(ID_PRINT, MyFrame::OnPrint)
311 EVT_MENU(ID_PREVIEW, MyFrame::OnPreview)
312 EVT_MENU(ID_PAGE_SETUP, MyFrame::OnPageSetup)
313
314 EVT_TEXT_URL(wxID_ANY, MyFrame::OnURL)
315 EVT_RICHTEXT_STYLESHEET_REPLACING(wxID_ANY, MyFrame::OnStyleSheetReplacing)
316 END_EVENT_TABLE()
317
318 // Create a new application object: this macro will allow wxWidgets to create
319 // the application object during program execution (it's better than using a
320 // static object for many reasons) and also implements the accessor function
321 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
322 // not wxApp)
323 IMPLEMENT_APP(MyApp)
324
325 // ============================================================================
326 // implementation
327 // ============================================================================
328
329 // ----------------------------------------------------------------------------
330 // the application class
331 // ----------------------------------------------------------------------------
332
333 // 'Main program' equivalent: the program execution "starts" here
334 bool MyApp::OnInit()
335 {
336 #if wxUSE_HELP
337 wxHelpProvider::Set(new wxSimpleHelpProvider);
338 #endif
339
340 m_styleSheet = new wxRichTextStyleSheet;
341 m_printing = new wxRichTextPrinting(wxT("Test Document"));
342
343 m_printing->SetFooterText(wxT("@TITLE@"), wxRICHTEXT_PAGE_ALL, wxRICHTEXT_PAGE_CENTRE);
344 m_printing->SetFooterText(wxT("Page @PAGENUM@"), wxRICHTEXT_PAGE_ALL, wxRICHTEXT_PAGE_RIGHT);
345
346 CreateStyles();
347
348 // Add extra handlers (plain text is automatically added)
349 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler);
350 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler);
351
352 // Add image handlers
353 #if wxUSE_LIBPNG
354 wxImage::AddHandler( new wxPNGHandler );
355 #endif
356
357 #if wxUSE_LIBJPEG
358 wxImage::AddHandler( new wxJPEGHandler );
359 #endif
360
361 #if wxUSE_GIF
362 wxImage::AddHandler( new wxGIFHandler );
363 #endif
364
365 #if wxUSE_FILESYSTEM
366 wxFileSystem::AddHandler( new wxMemoryFSHandler );
367 #endif
368
369 // create the main application window
370 MyFrame *frame = new MyFrame(_T("wxRichTextCtrl Sample"), wxID_ANY, wxDefaultPosition, wxSize(700, 600));
371
372 m_printing->SetParentWindow(frame);
373
374 // and show it (the frames, unlike simple controls, are not shown when
375 // created initially)
376 frame->Show(true);
377
378 // success: wxApp::OnRun() will be called which will enter the main message
379 // loop and the application will run. If we returned false here, the
380 // application would exit immediately.
381 return true;
382 }
383
384 int MyApp::OnExit()
385 {
386 delete m_printing;
387 delete m_styleSheet;
388
389 return 0;
390 }
391
392 void MyApp::CreateStyles()
393 {
394 // Paragraph styles
395
396 wxFont romanFont(12, wxROMAN, wxNORMAL, wxNORMAL);
397 wxFont swissFont(12, wxSWISS, wxNORMAL, wxNORMAL);
398
399 wxRichTextParagraphStyleDefinition* normalPara = new wxRichTextParagraphStyleDefinition(wxT("Normal"));
400 wxRichTextAttr normalAttr;
401 normalAttr.SetFontFaceName(romanFont.GetFaceName());
402 normalAttr.SetFontSize(12);
403 // Let's set all attributes for this style
404 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|
405 wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|
406 wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER);
407 normalPara->SetStyle(normalAttr);
408
409 m_styleSheet->AddParagraphStyle(normalPara);
410
411 wxRichTextParagraphStyleDefinition* indentedPara = new wxRichTextParagraphStyleDefinition(wxT("Indented"));
412 wxRichTextAttr indentedAttr;
413 indentedAttr.SetFontFaceName(romanFont.GetFaceName());
414 indentedAttr.SetFontSize(12);
415 indentedAttr.SetLeftIndent(100, 0);
416 // We only want to affect indentation
417 indentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
418 indentedPara->SetStyle(indentedAttr);
419
420 m_styleSheet->AddParagraphStyle(indentedPara);
421
422 wxRichTextParagraphStyleDefinition* indentedPara2 = new wxRichTextParagraphStyleDefinition(wxT("Red Bold Indented"));
423 wxRichTextAttr indentedAttr2;
424 indentedAttr2.SetFontFaceName(romanFont.GetFaceName());
425 indentedAttr2.SetFontSize(12);
426 indentedAttr2.SetFontWeight(wxBOLD);
427 indentedAttr2.SetTextColour(*wxRED);
428 indentedAttr2.SetFontSize(12);
429 indentedAttr2.SetLeftIndent(100, 0);
430 // We want to affect indentation, font and text colour
431 indentedAttr2.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_FONT|wxTEXT_ATTR_TEXT_COLOUR);
432 indentedPara2->SetStyle(indentedAttr2);
433
434 m_styleSheet->AddParagraphStyle(indentedPara2);
435
436 wxRichTextParagraphStyleDefinition* flIndentedPara = new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
437 wxRichTextAttr flIndentedAttr;
438 flIndentedAttr.SetFontFaceName(swissFont.GetFaceName());
439 flIndentedAttr.SetFontSize(12);
440 flIndentedAttr.SetLeftIndent(100, -100);
441 // We only want to affect indentation
442 flIndentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
443 flIndentedPara->SetStyle(flIndentedAttr);
444
445 m_styleSheet->AddParagraphStyle(flIndentedPara);
446
447 // Character styles
448
449 wxRichTextCharacterStyleDefinition* boldDef = new wxRichTextCharacterStyleDefinition(wxT("Bold"));
450 wxRichTextAttr boldAttr;
451 boldAttr.SetFontFaceName(romanFont.GetFaceName());
452 boldAttr.SetFontSize(12);
453 boldAttr.SetFontWeight(wxBOLD);
454 // We only want to affect boldness
455 boldAttr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
456 boldDef->SetStyle(boldAttr);
457
458 m_styleSheet->AddCharacterStyle(boldDef);
459
460 wxRichTextCharacterStyleDefinition* italicDef = new wxRichTextCharacterStyleDefinition(wxT("Italic"));
461 wxRichTextAttr italicAttr;
462 italicAttr.SetFontFaceName(romanFont.GetFaceName());
463 italicAttr.SetFontSize(12);
464 italicAttr.SetFontStyle(wxITALIC);
465 // We only want to affect italics
466 italicAttr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
467 italicDef->SetStyle(italicAttr);
468
469 m_styleSheet->AddCharacterStyle(italicDef);
470
471 wxRichTextCharacterStyleDefinition* redDef = new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
472 wxRichTextAttr redAttr;
473 redAttr.SetFontFaceName(romanFont.GetFaceName());
474 redAttr.SetFontSize(12);
475 redAttr.SetFontWeight(wxBOLD);
476 redAttr.SetTextColour(*wxRED);
477 // We only want to affect colour, weight and face
478 redAttr.SetFlags(wxTEXT_ATTR_FONT_FACE|wxTEXT_ATTR_FONT_WEIGHT|wxTEXT_ATTR_TEXT_COLOUR);
479 redDef->SetStyle(redAttr);
480
481 m_styleSheet->AddCharacterStyle(redDef);
482
483 wxRichTextListStyleDefinition* bulletList = new wxRichTextListStyleDefinition(wxT("Bullet List 1"));
484 int i;
485 for (i = 0; i < 10; i++)
486 {
487 wxString bulletText;
488 if (i == 0)
489 bulletText = wxT("standard/circle");
490 else if (i == 1)
491 bulletText = wxT("standard/square");
492 else if (i == 2)
493 bulletText = wxT("standard/circle");
494 else if (i == 3)
495 bulletText = wxT("standard/square");
496 else
497 bulletText = wxT("standard/circle");
498
499 bulletList->SetAttributes(i, (i+1)*60, 60, wxTEXT_ATTR_BULLET_STYLE_STANDARD, bulletText);
500 }
501
502 m_styleSheet->AddListStyle(bulletList);
503
504 wxRichTextListStyleDefinition* numberedList = new wxRichTextListStyleDefinition(wxT("Numbered List 1"));
505 for (i = 0; i < 10; i++)
506 {
507 long numberStyle;
508 if (i == 0)
509 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
510 else if (i == 1)
511 numberStyle = wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES;
512 else if (i == 2)
513 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES;
514 else if (i == 3)
515 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES;
516 else
517 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
518
519 numberStyle |= wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT;
520
521 numberedList->SetAttributes(i, (i+1)*60, 60, numberStyle);
522 }
523
524 m_styleSheet->AddListStyle(numberedList);
525
526 wxRichTextListStyleDefinition* outlineList = new wxRichTextListStyleDefinition(wxT("Outline List 1"));
527 for (i = 0; i < 10; i++)
528 {
529 long numberStyle;
530 if (i < 4)
531 numberStyle = wxTEXT_ATTR_BULLET_STYLE_OUTLINE|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
532 else
533 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
534
535 outlineList->SetAttributes(i, (i+1)*120, 120, numberStyle);
536 }
537
538 m_styleSheet->AddListStyle(outlineList);
539 }
540
541 // ----------------------------------------------------------------------------
542 // main frame
543 // ----------------------------------------------------------------------------
544
545 // frame constructor
546 MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
547 const wxSize& size, long style)
548 : wxFrame(NULL, id, title, pos, size, style)
549 {
550 // set the frame icon
551 SetIcon(wxICON(sample));
552
553 // create a menu bar
554 wxMenu *fileMenu = new wxMenu;
555
556 // the "About" item should be in the help menu
557 wxMenu *helpMenu = new wxMenu;
558 helpMenu->Append(ID_About, _T("&About...\tF1"), _T("Show about dialog"));
559
560 fileMenu->Append(wxID_OPEN, _T("&Open\tCtrl+O"), _T("Open a file"));
561 fileMenu->Append(wxID_SAVE, _T("&Save\tCtrl+S"), _T("Save a file"));
562 fileMenu->Append(wxID_SAVEAS, _T("&Save As...\tF12"), _T("Save to a new file"));
563 fileMenu->AppendSeparator();
564 fileMenu->Append(ID_PAGE_SETUP, _T("Page Set&up..."), _T("Page setup"));
565 fileMenu->Append(ID_PRINT, _T("&Print...\tCtrl+P"), _T("Print"));
566 fileMenu->Append(ID_PREVIEW, _T("Print Pre&view"), _T("Print preview"));
567 fileMenu->AppendSeparator();
568 fileMenu->Append(ID_VIEW_HTML, _T("&View as HTML"), _T("View HTML"));
569 fileMenu->AppendSeparator();
570 fileMenu->Append(ID_Quit, _T("E&xit\tAlt+X"), _T("Quit this program"));
571
572 wxMenu* editMenu = new wxMenu;
573 editMenu->Append(wxID_UNDO, _("&Undo\tCtrl+Z"));
574 editMenu->Append(wxID_REDO, _("&Redo\tCtrl+Y"));
575 editMenu->AppendSeparator();
576 editMenu->Append(wxID_CUT, _("Cu&t\tCtrl+X"));
577 editMenu->Append(wxID_COPY, _("&Copy\tCtrl+C"));
578 editMenu->Append(wxID_PASTE, _("&Paste\tCtrl+V"));
579
580 editMenu->AppendSeparator();
581 editMenu->Append(wxID_SELECTALL, _("Select A&ll\tCtrl+A"));
582 #if 0
583 editMenu->AppendSeparator();
584 editMenu->Append(wxID_FIND, _("&Find...\tCtrl+F"));
585 editMenu->Append(stID_FIND_REPLACE, _("&Replace...\tCtrl+R"));
586 #endif
587
588 wxMenu* formatMenu = new wxMenu;
589 formatMenu->AppendCheckItem(ID_FORMAT_BOLD, _("&Bold\tCtrl+B"));
590 formatMenu->AppendCheckItem(ID_FORMAT_ITALIC, _("&Italic\tCtrl+I"));
591 formatMenu->AppendCheckItem(ID_FORMAT_UNDERLINE, _("&Underline\tCtrl+U"));
592 formatMenu->AppendSeparator();
593 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_LEFT, _("L&eft Align"));
594 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT, _("&Right Align"));
595 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE, _("&Centre"));
596 formatMenu->AppendSeparator();
597 formatMenu->Append(ID_FORMAT_INDENT_MORE, _("Indent &More"));
598 formatMenu->Append(ID_FORMAT_INDENT_LESS, _("Indent &Less"));
599 formatMenu->AppendSeparator();
600 formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE, _("Increase Paragraph &Spacing"));
601 formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS, _("Decrease &Paragraph Spacing"));
602 formatMenu->AppendSeparator();
603 formatMenu->Append(ID_FORMAT_LINE_SPACING_SINGLE, _("Normal Line Spacing"));
604 formatMenu->Append(ID_FORMAT_LINE_SPACING_HALF, _("1.5 Line Spacing"));
605 formatMenu->Append(ID_FORMAT_LINE_SPACING_DOUBLE, _("Double Line Spacing"));
606 formatMenu->AppendSeparator();
607 formatMenu->Append(ID_FORMAT_FONT, _("&Font..."));
608 formatMenu->Append(ID_FORMAT_PARAGRAPH, _("&Paragraph..."));
609 formatMenu->Append(ID_FORMAT_CONTENT, _("Font and Pa&ragraph...\tShift+Ctrl+F"));
610 formatMenu->AppendSeparator();
611 formatMenu->Append(ID_SWITCH_STYLE_SHEETS, _("&Switch Style Sheets"));
612 formatMenu->Append(ID_MANAGE_STYLES, _("&Manage Styles"));
613
614 wxMenu* listsMenu = new wxMenu;
615 listsMenu->Append(ID_FORMAT_BULLETS_AND_NUMBERING, _("Bullets and &Numbering..."));
616 listsMenu->AppendSeparator();
617 listsMenu->Append(ID_FORMAT_NUMBER_LIST, _("Number List"));
618 listsMenu->Append(ID_FORMAT_ITEMIZE_LIST, _("Itemize List"));
619 listsMenu->Append(ID_FORMAT_RENUMBER_LIST, _("Renumber List"));
620 listsMenu->Append(ID_FORMAT_PROMOTE_LIST, _("Promote List Items"));
621 listsMenu->Append(ID_FORMAT_DEMOTE_LIST, _("Demote List Items"));
622 listsMenu->Append(ID_FORMAT_CLEAR_LIST, _("Clear List Formatting"));
623
624 wxMenu* insertMenu = new wxMenu;
625 insertMenu->Append(ID_INSERT_SYMBOL, _("&Symbol...\tCtrl+I"));
626 insertMenu->Append(ID_INSERT_URL, _("&URL..."));
627
628 // now append the freshly created menu to the menu bar...
629 wxMenuBar *menuBar = new wxMenuBar();
630 menuBar->Append(fileMenu, _T("&File"));
631 menuBar->Append(editMenu, _T("&Edit"));
632 menuBar->Append(formatMenu, _T("F&ormat"));
633 menuBar->Append(listsMenu, _T("&Lists"));
634 menuBar->Append(insertMenu, _T("&Insert"));
635 menuBar->Append(helpMenu, _T("&Help"));
636
637 // ... and attach this menu bar to the frame
638 SetMenuBar(menuBar);
639
640 // create a status bar just for fun (by default with 1 pane only)
641 // but don't create it on limited screen space (WinCE)
642 bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA;
643
644 #if wxUSE_STATUSBAR
645 if ( !is_pda )
646 {
647 CreateStatusBar(2);
648 SetStatusText(_T("Welcome to wxRichTextCtrl!"));
649 }
650 #endif
651
652 wxToolBar* toolBar = CreateToolBar();
653
654 toolBar->AddTool(wxID_OPEN, wxBitmap(open_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Open"));
655 toolBar->AddTool(wxID_SAVEAS, wxBitmap(save_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Save"));
656 toolBar->AddSeparator();
657 toolBar->AddTool(wxID_CUT, wxBitmap(cut_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Cut"));
658 toolBar->AddTool(wxID_COPY, wxBitmap(copy_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Copy"));
659 toolBar->AddTool(wxID_PASTE, wxBitmap(paste_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Paste"));
660 toolBar->AddSeparator();
661 toolBar->AddTool(wxID_UNDO, wxBitmap(undo_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Undo"));
662 toolBar->AddTool(wxID_REDO, wxBitmap(redo_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Redo"));
663 toolBar->AddSeparator();
664 toolBar->AddTool(ID_FORMAT_BOLD, wxBitmap(bold_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Bold"));
665 toolBar->AddTool(ID_FORMAT_ITALIC, wxBitmap(italic_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Italic"));
666 toolBar->AddTool(ID_FORMAT_UNDERLINE, wxBitmap(underline_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Underline"));
667 toolBar->AddSeparator();
668 toolBar->AddTool(ID_FORMAT_ALIGN_LEFT, wxBitmap(alignleft_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Align Left"));
669 toolBar->AddTool(ID_FORMAT_ALIGN_CENTRE, wxBitmap(centre_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Centre"));
670 toolBar->AddTool(ID_FORMAT_ALIGN_RIGHT, wxBitmap(alignright_xpm), wxNullBitmap, true, -1, -1, (wxObject *) NULL, _("Align Right"));
671 toolBar->AddSeparator();
672 toolBar->AddTool(ID_FORMAT_INDENT_LESS, wxBitmap(indentless_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Indent Less"));
673 toolBar->AddTool(ID_FORMAT_INDENT_MORE, wxBitmap(indentmore_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Indent More"));
674 toolBar->AddSeparator();
675 toolBar->AddTool(ID_FORMAT_FONT, wxBitmap(font_xpm), wxNullBitmap, false, -1, -1, (wxObject *) NULL, _("Font"));
676
677 wxRichTextStyleComboCtrl* combo = new wxRichTextStyleComboCtrl(toolBar, ID_RICHTEXT_STYLE_COMBO, wxDefaultPosition, wxSize(200, -1));
678 toolBar->AddControl(combo);
679
680 toolBar->Realize();
681
682 wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, GetClientSize(), wxSP_NO_XP_THEME|wxSP_3D|wxSP_LIVE_UPDATE);
683
684 wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
685 wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
686 wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL);
687
688 m_richTextCtrl = new wxRichTextCtrl(splitter, ID_RICHTEXT_CTRL, wxEmptyString, wxDefaultPosition, wxSize(200, 200), wxVSCROLL|wxHSCROLL|wxNO_BORDER|wxWANTS_CHARS);
689 wxFont font(12, wxROMAN, wxNORMAL, wxNORMAL);
690
691 m_richTextCtrl->SetFont(font);
692
693 m_richTextCtrl->SetStyleSheet(wxGetApp().GetStyleSheet());
694
695 combo->SetStyleSheet(wxGetApp().GetStyleSheet());
696 combo->SetRichTextCtrl(m_richTextCtrl);
697 combo->UpdateStyles();
698
699 wxRichTextStyleListCtrl* styleListCtrl = new wxRichTextStyleListCtrl(splitter, ID_RICHTEXT_STYLE_LIST);
700
701 wxSize display = wxGetDisplaySize();
702 if ( is_pda && ( display.GetWidth() < display.GetHeight() ) )
703 {
704 splitter->SplitHorizontally(m_richTextCtrl, styleListCtrl);
705 }
706 else
707 {
708 splitter->SplitVertically(m_richTextCtrl, styleListCtrl, 500);
709 }
710
711 splitter->UpdateSize();
712
713 styleListCtrl->SetStyleSheet(wxGetApp().GetStyleSheet());
714 styleListCtrl->SetRichTextCtrl(m_richTextCtrl);
715 styleListCtrl->UpdateStyles();
716
717 wxRichTextCtrl& r = *m_richTextCtrl;
718
719 r.BeginSuppressUndo();
720
721 r.BeginParagraphSpacing(0, 20);
722
723 r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
724 r.BeginBold();
725
726 r.BeginFontSize(14);
727 r.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control"));
728 r.LineBreak();
729 r.WriteText(wxT("for editing and presenting styled text and images"));
730 r.EndFontSize();
731 r.Newline();
732
733 r.BeginItalic();
734 r.WriteText(wxT("by Julian Smart"));
735 r.EndItalic();
736
737 r.EndBold();
738 r.Newline();
739
740 r.WriteImage(wxBitmap(zebra_xpm));
741
742 r.EndAlignment();
743
744 r.Newline();
745 r.Newline();
746
747 r.WriteText(wxT("What can you do with this thing? "));
748
749 r.WriteImage(wxBitmap(smiley_xpm));
750 r.WriteText(wxT(" Well, you can change text "));
751
752 r.BeginTextColour(wxColour(255, 0, 0));
753 r.WriteText(wxT("colour, like this red bit."));
754 r.EndTextColour();
755
756 r.BeginTextColour(wxColour(0, 0, 255));
757 r.WriteText(wxT(" And this blue bit."));
758 r.EndTextColour();
759
760 r.WriteText(wxT(" Naturally you can make things "));
761 r.BeginBold();
762 r.WriteText(wxT("bold "));
763 r.EndBold();
764 r.BeginItalic();
765 r.WriteText(wxT("or italic "));
766 r.EndItalic();
767 r.BeginUnderline();
768 r.WriteText(wxT("or underlined."));
769 r.EndUnderline();
770
771 r.BeginFontSize(14);
772 r.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
773 r.EndFontSize();
774
775 r.WriteText(wxT(" Next we'll show an indented paragraph."));
776
777 r.BeginLeftIndent(60);
778 r.Newline();
779
780 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."));
781 r.EndLeftIndent();
782
783 r.Newline();
784
785 r.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
786
787 r.BeginLeftIndent(100, -40);
788 r.Newline();
789
790 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."));
791 r.EndLeftIndent();
792
793 r.Newline();
794
795 r.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
796
797 r.BeginNumberedBullet(1, 100, 60);
798 r.Newline();
799
800 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."));
801 r.EndNumberedBullet();
802
803 r.BeginNumberedBullet(2, 100, 60);
804 r.Newline();
805
806 r.WriteText(wxT("This is my second item."));
807 r.EndNumberedBullet();
808
809 r.Newline();
810
811 r.WriteText(wxT("The following paragraph is right-indented:"));
812
813 r.BeginRightIndent(200);
814 r.Newline();
815
816 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."));
817 r.EndRightIndent();
818
819 r.Newline();
820
821 r.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
822
823 r.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT);
824 r.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF);
825 r.Newline();
826
827 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."));
828 r.EndLineSpacing();
829 r.EndAlignment();
830
831 wxArrayInt tabs;
832 tabs.Add(400);
833 tabs.Add(600);
834 tabs.Add(800);
835 tabs.Add(1000);
836 wxTextAttrEx attr;
837 attr.SetFlags(wxTEXT_ATTR_TABS);
838 attr.SetTabs(tabs);
839 r.SetDefaultStyle(attr);
840
841 r.Newline();
842 r.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
843
844 r.Newline();
845 r.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
846
847 r.BeginSymbolBullet(wxT('*'), 100, 60);
848 r.Newline();
849 r.WriteText(wxT("Compatibility with wxTextCtrl API"));
850 r.EndSymbolBullet();
851
852 r.BeginSymbolBullet(wxT('*'), 100, 60);
853 r.Newline();
854 r.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
855 r.EndSymbolBullet();
856
857 r.BeginSymbolBullet(wxT('*'), 100, 60);
858 r.Newline();
859 r.WriteText(wxT("XML loading and saving"));
860 r.EndSymbolBullet();
861
862 r.BeginSymbolBullet(wxT('*'), 100, 60);
863 r.Newline();
864 r.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
865 r.EndSymbolBullet();
866
867 r.BeginSymbolBullet(wxT('*'), 100, 60);
868 r.Newline();
869 r.WriteText(wxT("Clipboard copy and paste"));
870 r.EndSymbolBullet();
871
872 r.BeginSymbolBullet(wxT('*'), 100, 60);
873 r.Newline();
874 r.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
875 r.EndSymbolBullet();
876
877 r.BeginSymbolBullet(wxT('*'), 100, 60);
878 r.Newline();
879 r.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
880 r.EndSymbolBullet();
881
882 r.Newline();
883
884 // Make a style suitable for showing a URL
885 wxRichTextAttr urlStyle;
886 urlStyle.SetTextColour(*wxBLUE);
887 urlStyle.SetFontUnderlined(true);
888
889 r.WriteText(wxT("wxRichTextCtrl can also display URLs, such as this one: "));
890 r.BeginStyle(urlStyle);
891 r.BeginURL(wxT("http://www.wxwidgets.org"));
892 r.WriteText(wxT("The wxWidgets Web Site"));
893 r.EndURL();
894 r.EndStyle();
895 r.WriteText(wxT(". Click on the URL to generate an event."));
896
897 r.Newline();
898
899 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!"));
900
901 r.EndParagraphSpacing();
902
903 r.EndSuppressUndo();
904 }
905
906
907 // event handlers
908
909 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
910 {
911 // true is to force the frame to close
912 Close(true);
913 }
914
915 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
916 {
917 wxString msg;
918 msg.Printf( _T("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
919 wxMessageBox(msg, _T("About wxRichTextCtrl Sample"), wxOK | wxICON_INFORMATION, this);
920 }
921
922 // Forward command events to the current rich text control, if any
923 bool MyFrame::ProcessEvent(wxEvent& event)
924 {
925 if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent)))
926 {
927 // Problem: we can get infinite recursion because the events
928 // climb back up to this frame, and repeat.
929 // Assume that command events don't cause another command event
930 // to be called, so we can rely on inCommand not being overwritten
931
932 static int s_eventType = 0;
933 static wxWindowID s_id = 0;
934
935 if (s_id != event.GetId() && s_eventType != event.GetEventType())
936 {
937 s_eventType = event.GetEventType();
938 s_id = event.GetId();
939
940 wxWindow* focusWin = wxFindFocusDescendant(this);
941 if (focusWin && focusWin->ProcessEvent(event))
942 {
943 //s_command = NULL;
944 s_eventType = 0;
945 s_id = 0;
946 return true;
947 }
948
949 s_eventType = 0;
950 s_id = 0;
951 }
952 else
953 {
954 return false;
955 }
956 }
957
958 return wxFrame::ProcessEvent(event);
959 }
960
961 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
962 {
963 wxString path;
964 wxString filename;
965 wxArrayInt fileTypes;
966
967 wxString filter = wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes);
968 if (!filter.empty())
969 filter += wxT("|");
970 filter += wxT("All files (*.*)|*.*");
971
972 wxFileDialog dialog(this,
973 _("Choose a filename"),
974 path,
975 filename,
976 filter,
977 wxFD_OPEN);
978
979 if (dialog.ShowModal() == wxID_OK)
980 {
981 wxString path = dialog.GetPath();
982
983 if (!path.empty())
984 {
985 int filterIndex = dialog.GetFilterIndex();
986 int fileType = (filterIndex < (int) fileTypes.GetCount())
987 ? fileTypes[filterIndex]
988 : wxRICHTEXT_TYPE_TEXT;
989 m_richTextCtrl->LoadFile(path, fileType);
990 }
991 }
992 }
993
994 void MyFrame::OnSave(wxCommandEvent& event)
995 {
996 if (m_richTextCtrl->GetFilename().empty())
997 {
998 OnSaveAs(event);
999 return;
1000 }
1001 m_richTextCtrl->SaveFile();
1002 }
1003
1004 void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
1005 {
1006 wxString filter = wxRichTextBuffer::GetExtWildcard(false, true);
1007 wxString path;
1008 wxString filename;
1009
1010 wxFileDialog dialog(this,
1011 _("Choose a filename"),
1012 path,
1013 filename,
1014 filter,
1015 wxFD_SAVE);
1016
1017 if (dialog.ShowModal() == wxID_OK)
1018 {
1019 wxString path = dialog.GetPath();
1020
1021 if (!path.empty())
1022 {
1023 m_richTextCtrl->SaveFile(path);
1024 }
1025 }
1026 }
1027
1028 void MyFrame::OnBold(wxCommandEvent& WXUNUSED(event))
1029 {
1030 m_richTextCtrl->ApplyBoldToSelection();
1031 }
1032
1033 void MyFrame::OnItalic(wxCommandEvent& WXUNUSED(event))
1034 {
1035 m_richTextCtrl->ApplyItalicToSelection();
1036 }
1037
1038 void MyFrame::OnUnderline(wxCommandEvent& WXUNUSED(event))
1039 {
1040 m_richTextCtrl->ApplyUnderlineToSelection();
1041 }
1042
1043
1044 void MyFrame::OnUpdateBold(wxUpdateUIEvent& event)
1045 {
1046 event.Check(m_richTextCtrl->IsSelectionBold());
1047 }
1048
1049 void MyFrame::OnUpdateItalic(wxUpdateUIEvent& event)
1050 {
1051 event.Check(m_richTextCtrl->IsSelectionItalics());
1052 }
1053
1054 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent& event)
1055 {
1056 event.Check(m_richTextCtrl->IsSelectionUnderlined());
1057 }
1058
1059 void MyFrame::OnAlignLeft(wxCommandEvent& WXUNUSED(event))
1060 {
1061 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT);
1062 }
1063
1064 void MyFrame::OnAlignCentre(wxCommandEvent& WXUNUSED(event))
1065 {
1066 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE);
1067 }
1068
1069 void MyFrame::OnAlignRight(wxCommandEvent& WXUNUSED(event))
1070 {
1071 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT);
1072 }
1073
1074 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent& event)
1075 {
1076 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT));
1077 }
1078
1079 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent& event)
1080 {
1081 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE));
1082 }
1083
1084 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent& event)
1085 {
1086 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT));
1087 }
1088
1089 void MyFrame::OnFont(wxCommandEvent& WXUNUSED(event))
1090 {
1091 wxRichTextRange range;
1092 if (m_richTextCtrl->HasSelection())
1093 range = m_richTextCtrl->GetSelectionRange();
1094 else
1095 range = wxRichTextRange(0, m_richTextCtrl->GetLastPosition()+1);
1096
1097 int pages = wxRICHTEXT_FORMAT_FONT;
1098
1099 wxRichTextFormattingDialog formatDlg(pages, this);
1100 formatDlg.GetStyle(m_richTextCtrl, range);
1101
1102 if (formatDlg.ShowModal() == wxID_OK)
1103 {
1104 formatDlg.ApplyStyle(m_richTextCtrl, range, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
1105 }
1106
1107 // Old method using wxFontDialog
1108 #if 0
1109 if (!m_richTextCtrl->HasSelection())
1110 return;
1111
1112 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1113 wxFontData fontData;
1114
1115 wxTextAttrEx attr;
1116 attr.SetFlags(wxTEXT_ATTR_FONT);
1117
1118 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1119 fontData.SetInitialFont(attr.GetFont());
1120
1121 wxFontDialog dialog(this, fontData);
1122 if (dialog.ShowModal() == wxID_OK)
1123 {
1124 fontData = dialog.GetFontData();
1125 attr.SetFlags(wxTEXT_ATTR_FONT);
1126 attr.SetFont(fontData.GetChosenFont());
1127 if (attr.GetFont().Ok())
1128 {
1129 m_richTextCtrl->SetStyle(range, attr);
1130 }
1131 }
1132 #endif
1133 }
1134
1135 void MyFrame::OnParagraph(wxCommandEvent& WXUNUSED(event))
1136 {
1137 wxRichTextRange range;
1138 if (m_richTextCtrl->HasSelection())
1139 range = m_richTextCtrl->GetSelectionRange();
1140 else
1141 range = wxRichTextRange(0, m_richTextCtrl->GetLastPosition()+1);
1142
1143 int pages = wxRICHTEXT_FORMAT_INDENTS_SPACING|wxRICHTEXT_FORMAT_TABS|wxRICHTEXT_FORMAT_BULLETS;
1144
1145 wxRichTextFormattingDialog formatDlg(pages, this);
1146 formatDlg.GetStyle(m_richTextCtrl, range);
1147
1148 if (formatDlg.ShowModal() == wxID_OK)
1149 {
1150 formatDlg.ApplyStyle(m_richTextCtrl, range);
1151 }
1152 }
1153
1154 void MyFrame::OnFormat(wxCommandEvent& WXUNUSED(event))
1155 {
1156 wxRichTextRange range;
1157 if (m_richTextCtrl->HasSelection())
1158 range = m_richTextCtrl->GetSelectionRange();
1159 else
1160 range = wxRichTextRange(0, m_richTextCtrl->GetLastPosition()+1);
1161
1162 int pages = wxRICHTEXT_FORMAT_FONT|wxRICHTEXT_FORMAT_INDENTS_SPACING|wxRICHTEXT_FORMAT_TABS|wxRICHTEXT_FORMAT_BULLETS;
1163
1164 wxRichTextFormattingDialog formatDlg(pages, this);
1165 formatDlg.GetStyle(m_richTextCtrl, range);
1166
1167 if (formatDlg.ShowModal() == wxID_OK)
1168 {
1169 formatDlg.ApplyStyle(m_richTextCtrl, range);
1170 }
1171 }
1172
1173 void MyFrame::OnUpdateFormat(wxUpdateUIEvent& event)
1174 {
1175 event.Enable(m_richTextCtrl->HasSelection());
1176 }
1177
1178 void MyFrame::OnIndentMore(wxCommandEvent& WXUNUSED(event))
1179 {
1180 wxTextAttrEx attr;
1181 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
1182
1183 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1184 {
1185 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1186 if (m_richTextCtrl->HasSelection())
1187 range = m_richTextCtrl->GetSelectionRange();
1188
1189 wxFontData fontData;
1190 attr.SetLeftIndent(attr.GetLeftIndent() + 100);
1191
1192 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
1193 m_richTextCtrl->SetStyle(range, attr);
1194 }
1195 }
1196
1197 void MyFrame::OnIndentLess(wxCommandEvent& WXUNUSED(event))
1198 {
1199 wxTextAttrEx attr;
1200 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
1201
1202 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1203 {
1204 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1205 if (m_richTextCtrl->HasSelection())
1206 range = m_richTextCtrl->GetSelectionRange();
1207
1208 if (attr.GetLeftIndent() >= 100)
1209 {
1210 wxFontData fontData;
1211 attr.SetLeftIndent(attr.GetLeftIndent() - 100);
1212
1213 m_richTextCtrl->SetStyle(range, attr);
1214 }
1215 }
1216 }
1217
1218 void MyFrame::OnLineSpacingHalf(wxCommandEvent& WXUNUSED(event))
1219 {
1220 wxTextAttrEx attr;
1221 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1222
1223 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1224 {
1225 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1226 if (m_richTextCtrl->HasSelection())
1227 range = m_richTextCtrl->GetSelectionRange();
1228
1229 wxFontData fontData;
1230 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1231 attr.SetLineSpacing(15);
1232
1233 m_richTextCtrl->SetStyle(range, attr);
1234 }
1235 }
1236
1237 void MyFrame::OnLineSpacingDouble(wxCommandEvent& WXUNUSED(event))
1238 {
1239 wxTextAttrEx attr;
1240 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1241
1242 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1243 {
1244 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1245 if (m_richTextCtrl->HasSelection())
1246 range = m_richTextCtrl->GetSelectionRange();
1247
1248 wxFontData fontData;
1249 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1250 attr.SetLineSpacing(20);
1251
1252 m_richTextCtrl->SetStyle(range, attr);
1253 }
1254 }
1255
1256 void MyFrame::OnLineSpacingSingle(wxCommandEvent& WXUNUSED(event))
1257 {
1258 wxTextAttrEx attr;
1259 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1260
1261 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1262 {
1263 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1264 if (m_richTextCtrl->HasSelection())
1265 range = m_richTextCtrl->GetSelectionRange();
1266
1267 wxFontData fontData;
1268 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1269 attr.SetLineSpacing(0); // Can also use 10
1270
1271 m_richTextCtrl->SetStyle(range, attr);
1272 }
1273 }
1274
1275 void MyFrame::OnParagraphSpacingMore(wxCommandEvent& WXUNUSED(event))
1276 {
1277 wxTextAttrEx attr;
1278 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1279
1280 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1281 {
1282 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1283 if (m_richTextCtrl->HasSelection())
1284 range = m_richTextCtrl->GetSelectionRange();
1285
1286 wxFontData fontData;
1287 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() + 20);
1288
1289 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1290 m_richTextCtrl->SetStyle(range, attr);
1291 }
1292 }
1293
1294 void MyFrame::OnParagraphSpacingLess(wxCommandEvent& WXUNUSED(event))
1295 {
1296 wxTextAttrEx attr;
1297 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1298
1299 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1300 {
1301 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1302 if (m_richTextCtrl->HasSelection())
1303 range = m_richTextCtrl->GetSelectionRange();
1304
1305 if (attr.GetParagraphSpacingAfter() >= 20)
1306 {
1307 wxFontData fontData;
1308 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() - 20);
1309
1310 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1311 m_richTextCtrl->SetStyle(range, attr);
1312 }
1313 }
1314 }
1315
1316 void MyFrame::OnViewHTML(wxCommandEvent& WXUNUSED(event))
1317 {
1318 wxDialog dialog(this, wxID_ANY, _("HTML"), wxDefaultPosition, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE);
1319
1320 wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
1321 dialog.SetSizer(boxSizer);
1322
1323 wxHtmlWindow* win = new wxHtmlWindow(& dialog, wxID_ANY, wxDefaultPosition, wxSize(500, 400), wxSUNKEN_BORDER);
1324 boxSizer->Add(win, 1, wxALL, 5);
1325
1326 wxButton* cancelButton = new wxButton(& dialog, wxID_CANCEL, wxT("&Close"));
1327 boxSizer->Add(cancelButton, 0, wxALL|wxCENTRE, 5);
1328
1329 wxString text;
1330 wxStringOutputStream strStream(& text);
1331
1332 wxRichTextHTMLHandler htmlHandler;
1333 htmlHandler.SetFlags(wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY);
1334
1335 if (htmlHandler.SaveFile(& m_richTextCtrl->GetBuffer(), strStream))
1336 {
1337 win->SetPage(text);
1338 }
1339
1340 boxSizer->Fit(& dialog);
1341
1342 dialog.ShowModal();
1343
1344 // Now delete the temporary in-memory images
1345 htmlHandler.DeleteTemporaryImages();
1346 }
1347
1348 // Demonstrates how you can change the style sheets and have the changes
1349 // reflected in the control content without wiping out character formatting.
1350
1351 void MyFrame::OnSwitchStyleSheets(wxCommandEvent& WXUNUSED(event))
1352 {
1353 static wxRichTextStyleSheet* gs_AlternateStyleSheet = NULL;
1354
1355 wxRichTextStyleListCtrl *styleList = (wxRichTextStyleListCtrl*) FindWindow(ID_RICHTEXT_STYLE_LIST);
1356 wxRichTextStyleComboCtrl* styleCombo = (wxRichTextStyleComboCtrl*) FindWindow(ID_RICHTEXT_STYLE_COMBO);
1357
1358 wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
1359
1360 // One-time creation of an alternate style sheet
1361 if (!gs_AlternateStyleSheet)
1362 {
1363 gs_AlternateStyleSheet = new wxRichTextStyleSheet(*sheet);
1364
1365 // Make some modifications
1366 for (int i = 0; i < (int) gs_AlternateStyleSheet->GetParagraphStyleCount(); i++)
1367 {
1368 wxRichTextParagraphStyleDefinition* def = gs_AlternateStyleSheet->GetParagraphStyle(i);
1369
1370 if (def->GetStyle().HasTextColour())
1371 def->GetStyle().SetTextColour(*wxBLUE);
1372
1373 if (def->GetStyle().HasAlignment())
1374 {
1375 if (def->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
1376 def->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
1377 else if (def->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_LEFT)
1378 def->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
1379 }
1380 if (def->GetStyle().HasLeftIndent())
1381 {
1382 def->GetStyle().SetLeftIndent(def->GetStyle().GetLeftIndent() * 2);
1383 }
1384 }
1385 }
1386
1387 // Switch sheets
1388 wxRichTextStyleSheet* tmp = gs_AlternateStyleSheet;
1389 gs_AlternateStyleSheet = sheet;
1390 sheet = tmp;
1391
1392 m_richTextCtrl->SetStyleSheet(sheet);
1393 m_richTextCtrl->ApplyStyleSheet(sheet); // Makes the control reflect the new style definitions
1394
1395 styleList->SetStyleSheet(sheet);
1396 styleList->UpdateStyles();
1397
1398 styleCombo->SetStyleSheet(sheet);
1399 styleCombo->UpdateStyles();
1400 }
1401
1402 void MyFrame::OnManageStyles(wxCommandEvent& WXUNUSED(event))
1403 {
1404 wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
1405
1406 int flags = wxRICHTEXT_ORGANISER_CREATE_STYLES|wxRICHTEXT_ORGANISER_EDIT_STYLES;
1407
1408 wxRichTextStyleOrganiserDialog dlg(flags, sheet, NULL, this, wxID_ANY, _("Style Manager"));
1409 dlg.ShowModal();
1410 }
1411
1412 void MyFrame::OnInsertSymbol(wxCommandEvent& WXUNUSED(event))
1413 {
1414 wxTextAttrEx attr;
1415 attr.SetFlags(wxTEXT_ATTR_FONT);
1416 m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr);
1417
1418 wxString currentFontName;
1419 if (attr.HasFont() && attr.GetFont().Ok())
1420 currentFontName = attr.GetFont().GetFaceName();
1421
1422 // Don't set the initial font in the dialog (so the user is choosing
1423 // 'normal text', i.e. the current font) but do tell the dialog
1424 // what 'normal text' is.
1425
1426 wxSymbolPickerDialog dlg(wxT("*"), wxEmptyString, currentFontName, this);
1427
1428 if (dlg.ShowModal() == wxID_OK)
1429 {
1430 if (dlg.HasSelection())
1431 {
1432 long insertionPoint = m_richTextCtrl->GetInsertionPoint();
1433
1434 m_richTextCtrl->WriteText(dlg.GetSymbol());
1435
1436 if (!dlg.UseNormalFont())
1437 {
1438 wxFont font(attr.GetFont());
1439 font.SetFaceName(dlg.GetFontName());
1440 attr.SetFont(font);
1441 m_richTextCtrl->SetStyle(insertionPoint, insertionPoint+1, attr);
1442 }
1443 }
1444 }
1445 }
1446
1447 void MyFrame::OnNumberList(wxCommandEvent& WXUNUSED(event))
1448 {
1449 if (m_richTextCtrl->HasSelection())
1450 {
1451 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1452 m_richTextCtrl->SetListStyle(range, wxT("Numbered List 1"), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RENUMBER);
1453 }
1454 }
1455
1456 void MyFrame::OnBulletsAndNumbering(wxCommandEvent& WXUNUSED(event))
1457 {
1458 wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
1459
1460 int flags = wxRICHTEXT_ORGANISER_BROWSE_NUMBERING;
1461
1462 wxRichTextStyleOrganiserDialog dlg(flags, sheet, m_richTextCtrl, this, wxID_ANY, _("Bullets and Numbering"));
1463 if (dlg.ShowModal() == wxID_OK)
1464 {
1465 if (dlg.GetSelectedStyleDefinition())
1466 dlg.ApplyStyle();
1467 }
1468 }
1469
1470 void MyFrame::OnItemizeList(wxCommandEvent& WXUNUSED(event))
1471 {
1472 if (m_richTextCtrl->HasSelection())
1473 {
1474 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1475 m_richTextCtrl->SetListStyle(range, wxT("Bullet List 1"));
1476 }
1477 }
1478
1479 void MyFrame::OnRenumberList(wxCommandEvent& WXUNUSED(event))
1480 {
1481 if (m_richTextCtrl->HasSelection())
1482 {
1483 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1484 m_richTextCtrl->NumberList(range, NULL, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RENUMBER);
1485 }
1486 }
1487
1488 void MyFrame::OnPromoteList(wxCommandEvent& WXUNUSED(event))
1489 {
1490 if (m_richTextCtrl->HasSelection())
1491 {
1492 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1493 m_richTextCtrl->PromoteList(1, range, NULL);
1494 }
1495 }
1496
1497 void MyFrame::OnDemoteList(wxCommandEvent& WXUNUSED(event))
1498 {
1499 if (m_richTextCtrl->HasSelection())
1500 {
1501 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1502 m_richTextCtrl->PromoteList(-1, range, NULL);
1503 }
1504 }
1505
1506 void MyFrame::OnClearList(wxCommandEvent& WXUNUSED(event))
1507 {
1508 if (m_richTextCtrl->HasSelection())
1509 {
1510 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1511 m_richTextCtrl->ClearListStyle(range);
1512 }
1513 }
1514
1515 void MyFrame::OnInsertURL(wxCommandEvent& WXUNUSED(event))
1516 {
1517 wxString url = wxGetTextFromUser(_("URL:"), _("Insert URL"));
1518 if (!url.IsEmpty())
1519 {
1520 // Make a style suitable for showing a URL
1521 wxRichTextAttr urlStyle;
1522 urlStyle.SetTextColour(*wxBLUE);
1523 urlStyle.SetFontUnderlined(true);
1524
1525 m_richTextCtrl->BeginStyle(urlStyle);
1526 m_richTextCtrl->BeginURL(url);
1527 m_richTextCtrl->WriteText(url);
1528 m_richTextCtrl->EndURL();
1529 m_richTextCtrl->EndStyle();
1530 }
1531 }
1532
1533 void MyFrame::OnURL(wxTextUrlEvent& event)
1534 {
1535 wxMessageBox(event.GetString());
1536 }
1537
1538 // Veto style sheet replace events when loading from XML, since we want
1539 // to keep the original style sheet.
1540 void MyFrame::OnStyleSheetReplacing(wxRichTextEvent& event)
1541 {
1542 event.Veto();
1543 }
1544
1545 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
1546 {
1547 wxGetApp().GetPrinting()->PrintBuffer(m_richTextCtrl->GetBuffer());
1548 }
1549
1550 void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event))
1551 {
1552 wxGetApp().GetPrinting()->PreviewBuffer(m_richTextCtrl->GetBuffer());
1553 }
1554
1555 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
1556 {
1557 wxGetApp().GetPrinting()->PageSetup();
1558 }