]> git.saurik.com Git - wxWidgets.git/blob - samples/richtext/richtext.cpp
glibc's vswprintf doesn't nul terminate on truncation.
[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 #ifndef __WXMSW__
39 #include "../sample.xpm"
40 #endif
41
42 #include "bitmaps/smiley.xpm"
43 // #include "bitmaps/idea.xpm"
44 #include "bitmaps/zebra.xpm"
45
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"
56
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"
63
64 #include "wx/richtext/richtextctrl.h"
65 #include "wx/richtext/richtextstyles.h"
66 #include "wx/richtext/richtextxml.h"
67 #include "wx/richtext/richtexthtml.h"
68
69 // ----------------------------------------------------------------------------
70 // resources
71 // ----------------------------------------------------------------------------
72
73 // ----------------------------------------------------------------------------
74 // private classes
75 // ----------------------------------------------------------------------------
76
77 // Define a new application type, each program should derive a class from wxApp
78 class MyApp : public wxApp
79 {
80 public:
81 // override base class virtuals
82 // ----------------------------
83
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();
88 virtual int OnExit();
89
90 void CreateStyles();
91
92 wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
93
94 wxRichTextStyleSheet* m_styleSheet;
95 };
96
97 // Define a new frame type: this is going to be our main frame
98 class MyFrame : public wxFrame
99 {
100 public:
101 // ctor(s)
102 MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
103 const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE);
104
105 // event handlers (these functions should _not_ be virtual)
106 void OnQuit(wxCommandEvent& event);
107 void OnAbout(wxCommandEvent& event);
108
109 void OnOpen(wxCommandEvent& event);
110 void OnSave(wxCommandEvent& event);
111 void OnSaveAs(wxCommandEvent& event);
112
113 void OnBold(wxCommandEvent& event);
114 void OnItalic(wxCommandEvent& event);
115 void OnUnderline(wxCommandEvent& event);
116
117 void OnUpdateBold(wxUpdateUIEvent& event);
118 void OnUpdateItalic(wxUpdateUIEvent& event);
119 void OnUpdateUnderline(wxUpdateUIEvent& event);
120
121 void OnAlignLeft(wxCommandEvent& event);
122 void OnAlignCentre(wxCommandEvent& event);
123 void OnAlignRight(wxCommandEvent& event);
124
125 void OnUpdateAlignLeft(wxUpdateUIEvent& event);
126 void OnUpdateAlignCentre(wxUpdateUIEvent& event);
127 void OnUpdateAlignRight(wxUpdateUIEvent& event);
128
129 void OnFont(wxCommandEvent& event);
130 void OnIndentMore(wxCommandEvent& event);
131 void OnIndentLess(wxCommandEvent& event);
132
133 void OnLineSpacingHalf(wxCommandEvent& event);
134 void OnLineSpacingDouble(wxCommandEvent& event);
135 void OnLineSpacingSingle(wxCommandEvent& event);
136
137 void OnParagraphSpacingMore(wxCommandEvent& event);
138 void OnParagraphSpacingLess(wxCommandEvent& event);
139
140 void OnViewHTML(wxCommandEvent& event);
141
142 // Forward command events to the current rich text control, if any
143 bool ProcessEvent(wxEvent& event);
144
145 private:
146 // any class wishing to process wxWidgets events must use this macro
147 DECLARE_EVENT_TABLE()
148
149 wxRichTextCtrl* m_richTextCtrl;
150 };
151
152 // ----------------------------------------------------------------------------
153 // constants
154 // ----------------------------------------------------------------------------
155
156 // IDs for the controls and the menu commands
157 enum
158 {
159 // menu items
160 ID_Quit = wxID_EXIT,
161 ID_About = wxID_ABOUT,
162
163 ID_FORMAT_BOLD = 100,
164 ID_FORMAT_ITALIC,
165 ID_FORMAT_UNDERLINE,
166 ID_FORMAT_FONT,
167
168 ID_FORMAT_ALIGN_LEFT,
169 ID_FORMAT_ALIGN_CENTRE,
170 ID_FORMAT_ALIGN_RIGHT,
171
172 ID_FORMAT_INDENT_MORE,
173 ID_FORMAT_INDENT_LESS,
174
175 ID_FORMAT_PARAGRAPH_SPACING_MORE,
176 ID_FORMAT_PARAGRAPH_SPACING_LESS,
177
178 ID_FORMAT_LINE_SPACING_HALF,
179 ID_FORMAT_LINE_SPACING_DOUBLE,
180 ID_FORMAT_LINE_SPACING_SINGLE,
181
182 ID_VIEW_HTML
183 };
184
185 // ----------------------------------------------------------------------------
186 // event tables and other macros for wxWidgets
187 // ----------------------------------------------------------------------------
188
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)
195
196 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
197 EVT_MENU(wxID_SAVE, MyFrame::OnSave)
198 EVT_MENU(wxID_SAVEAS, MyFrame::OnSaveAs)
199
200 EVT_MENU(ID_FORMAT_BOLD, MyFrame::OnBold)
201 EVT_MENU(ID_FORMAT_ITALIC, MyFrame::OnItalic)
202 EVT_MENU(ID_FORMAT_UNDERLINE, MyFrame::OnUnderline)
203
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)
207
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)
211
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)
215
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)
219
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)
223
224 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE, MyFrame::OnParagraphSpacingMore)
225 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS, MyFrame::OnParagraphSpacingLess)
226
227 EVT_MENU(ID_VIEW_HTML, MyFrame::OnViewHTML)
228 END_EVENT_TABLE()
229
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
234 // not wxApp)
235 IMPLEMENT_APP(MyApp)
236
237 // ============================================================================
238 // implementation
239 // ============================================================================
240
241 // ----------------------------------------------------------------------------
242 // the application class
243 // ----------------------------------------------------------------------------
244
245 // 'Main program' equivalent: the program execution "starts" here
246 bool MyApp::OnInit()
247 {
248 m_styleSheet = new wxRichTextStyleSheet;
249
250 CreateStyles();
251
252 // Add extra handlers (plain text is automatically added)
253 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler);
254 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler);
255
256 // Add image handlers
257 #if wxUSE_LIBPNG
258 wxImage::AddHandler( new wxPNGHandler );
259 #endif
260
261 #if wxUSE_LIBJPEG
262 wxImage::AddHandler( new wxJPEGHandler );
263 #endif
264
265 #if wxUSE_GIF
266 wxImage::AddHandler( new wxGIFHandler );
267 #endif
268
269 // create the main application window
270 MyFrame *frame = new MyFrame(_T("wxRichTextCtrl Sample"), wxID_ANY, wxDefaultPosition, wxSize(600, 500));
271
272 // and show it (the frames, unlike simple controls, are not shown when
273 // created initially)
274 frame->Show(true);
275
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.
279 return true;
280 }
281
282 int MyApp::OnExit()
283 {
284 delete m_styleSheet;
285 return 0;
286 }
287
288 void MyApp::CreateStyles()
289 {
290 // Paragraph styles
291
292 wxFont romanFont(12, wxROMAN, wxNORMAL, wxNORMAL);
293 wxFont swissFont(12, wxSWISS, wxNORMAL, wxNORMAL);
294
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);
304
305 m_styleSheet->AddParagraphStyle(normalPara);
306
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);
315
316 m_styleSheet->AddParagraphStyle(indentedPara);
317
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);
329
330 m_styleSheet->AddParagraphStyle(indentedPara2);
331
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);
340
341 m_styleSheet->AddParagraphStyle(flIndentedPara);
342
343 // Character styles
344
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);
353
354 m_styleSheet->AddCharacterStyle(boldDef);
355
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);
364
365 m_styleSheet->AddCharacterStyle(italicDef);
366
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);
376
377 m_styleSheet->AddCharacterStyle(redDef);
378 }
379
380 // ----------------------------------------------------------------------------
381 // main frame
382 // ----------------------------------------------------------------------------
383
384 // frame constructor
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)
388 {
389 // set the frame icon
390 SetIcon(wxICON(sample));
391
392 // create a menu bar
393 wxMenu *fileMenu = new wxMenu;
394
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"));
398
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"));
406
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"));
414
415 editMenu->Append(wxID_CLEAR, _("&Delete\tDel"));
416
417 editMenu->AppendSeparator();
418 editMenu->Append(wxID_SELECTALL, _("Select A&ll\tCtrl+A"));
419 #if 0
420 editMenu->AppendSeparator();
421 editMenu->Append(wxID_FIND, _("&Find...\tCtrl+F"));
422 editMenu->Append(stID_FIND_REPLACE, _("&Replace...\tCtrl+R"));
423 #endif
424
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..."));
445
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"));
452
453 // ... and attach this menu bar to the frame
454 SetMenuBar(menuBar);
455
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;
459
460 #if wxUSE_STATUSBAR
461 if ( !is_pda )
462 {
463 CreateStatusBar(2);
464 SetStatusText(_T("Welcome to wxRichTextCtrl!"));
465 }
466 #endif
467
468 wxToolBar* toolBar = CreateToolBar();
469
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"));
492
493 toolBar->Realize();
494
495 wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, GetClientSize(), wxSP_NO_XP_THEME|wxSP_3D|wxSP_LIVE_UPDATE);
496
497 wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
498 wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
499 wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL);
500
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);
503
504 m_richTextCtrl->SetFont(font);
505
506 wxRichTextStyleListBox* styleListBox = new wxRichTextStyleListBox(splitter, wxID_ANY);
507
508 wxSize display = wxGetDisplaySize();
509 if ( is_pda && ( display.GetWidth() < display.GetHeight() ) )
510 {
511 splitter->SplitHorizontally(m_richTextCtrl, styleListBox);
512 }
513 else
514 {
515 splitter->SplitVertically(m_richTextCtrl, styleListBox, 400);
516 }
517
518 splitter->UpdateSize();
519
520 styleListBox->SetStyleSheet(wxGetApp().GetStyleSheet());
521 styleListBox->SetRichTextCtrl(m_richTextCtrl);
522 styleListBox->UpdateStyles();
523
524 wxRichTextCtrl& r = *m_richTextCtrl;
525
526 r.BeginSuppressUndo();
527
528 r.BeginParagraphSpacing(0, 20);
529
530 r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
531 r.BeginBold();
532
533 r.BeginFontSize(14);
534 r.WriteText(wxT("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images"));
535 r.EndFontSize();
536 r.Newline();
537
538 r.BeginItalic();
539 r.WriteText(wxT("by Julian Smart"));
540 r.EndItalic();
541
542 r.EndBold();
543
544 r.Newline();
545 r.WriteImage(wxBitmap(zebra_xpm));
546
547 r.EndAlignment();
548
549 r.Newline();
550 r.Newline();
551
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 "));
555
556 r.BeginTextColour(wxColour(255, 0, 0));
557 r.WriteText(wxT("colour, like this red bit."));
558 r.EndTextColour();
559
560 r.BeginTextColour(wxColour(0, 0, 255));
561 r.WriteText(wxT(" And this blue bit."));
562 r.EndTextColour();
563
564 r.WriteText(wxT(" Naturally you can make things "));
565 r.BeginBold();
566 r.WriteText(wxT("bold "));
567 r.EndBold();
568 r.BeginItalic();
569 r.WriteText(wxT("or italic "));
570 r.EndItalic();
571 r.BeginUnderline();
572 r.WriteText(wxT("or underlined."));
573 r.EndUnderline();
574
575 r.BeginFontSize(14);
576 r.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
577 r.EndFontSize();
578
579 r.WriteText(wxT(" Next we'll show an indented paragraph."));
580
581 r.BeginLeftIndent(60);
582 r.Newline();
583
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."));
585 r.EndLeftIndent();
586
587 r.Newline();
588
589 r.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
590
591 r.BeginLeftIndent(100, -40);
592 r.Newline();
593
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."));
595 r.EndLeftIndent();
596
597 r.Newline();
598
599 r.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
600
601 r.BeginNumberedBullet(1, 100, 60);
602 r.Newline();
603
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();
606
607 r.BeginNumberedBullet(2, 100, 60);
608 r.Newline();
609
610 r.WriteText(wxT("This is my second item."));
611 r.EndNumberedBullet();
612
613 r.Newline();
614
615 r.WriteText(wxT("The following paragraph is right-indented:"));
616
617 r.BeginRightIndent(200);
618 r.Newline();
619
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."));
621 r.EndRightIndent();
622
623 r.Newline();
624
625 r.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
626
627 r.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT);
628 r.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF);
629 r.Newline();
630
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."));
632 r.EndLineSpacing();
633 r.EndAlignment();
634
635 wxArrayInt tabs;
636 tabs.Add(400);
637 tabs.Add(600);
638 tabs.Add(800);
639 tabs.Add(1000);
640 wxTextAttrEx attr;
641 attr.SetFlags(wxTEXT_ATTR_TABS);
642 attr.SetTabs(tabs);
643 r.SetDefaultStyle(attr);
644
645 r.Newline();
646 r.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
647
648 r.Newline();
649 r.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
650
651 r.BeginSymbolBullet(wxT('*'), 100, 60);
652 r.Newline();
653 r.WriteText(wxT("Compatibility with wxTextCtrl API"));
654 r.EndSymbolBullet();
655
656 r.BeginSymbolBullet(wxT('*'), 100, 60);
657 r.Newline();
658 r.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
659 r.EndSymbolBullet();
660
661 r.BeginSymbolBullet(wxT('*'), 100, 60);
662 r.Newline();
663 r.WriteText(wxT("XML loading and saving"));
664 r.EndSymbolBullet();
665
666 r.BeginSymbolBullet(wxT('*'), 100, 60);
667 r.Newline();
668 r.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
669 r.EndSymbolBullet();
670
671 r.BeginSymbolBullet(wxT('*'), 100, 60);
672 r.Newline();
673 r.WriteText(wxT("Clipboard copy and paste"));
674 r.EndSymbolBullet();
675
676 r.BeginSymbolBullet(wxT('*'), 100, 60);
677 r.Newline();
678 r.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
679 r.EndSymbolBullet();
680
681 r.BeginSymbolBullet(wxT('*'), 100, 60);
682 r.Newline();
683 r.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
684 r.EndSymbolBullet();
685
686 r.Newline();
687
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!"));
689
690 r.EndParagraphSpacing();
691
692 r.EndSuppressUndo();
693 }
694
695
696 // event handlers
697
698 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
699 {
700 // true is to force the frame to close
701 Close(true);
702 }
703
704 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
705 {
706 wxString msg;
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);
709 }
710
711 // Forward command events to the current rich text control, if any
712 bool MyFrame::ProcessEvent(wxEvent& event)
713 {
714 if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent)))
715 {
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
720
721 static int s_eventType = 0;
722 static wxWindowID s_id = 0;
723
724 if (s_id != event.GetId() && s_eventType != event.GetEventType())
725 {
726 s_eventType = event.GetEventType();
727 s_id = event.GetId();
728
729 wxWindow* focusWin = wxFindFocusDescendant(this);
730 if (focusWin && focusWin->ProcessEvent(event))
731 {
732 //s_command = NULL;
733 s_eventType = 0;
734 s_id = 0;
735 return true;
736 }
737
738 s_eventType = 0;
739 s_id = 0;
740 }
741 else
742 {
743 return false;
744 }
745 }
746
747 return wxFrame::ProcessEvent(event);
748 }
749
750 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
751 {
752 wxString path;
753 wxString filename;
754 wxArrayInt fileTypes;
755
756 wxString filter = wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes);
757 if (!filter.empty())
758 filter += wxT("|");
759 filter += wxT("All files (*.*)|*.*");
760
761 wxFileDialog dialog(this,
762 _("Choose a filename"),
763 path,
764 filename,
765 filter,
766 wxFD_OPEN);
767
768 if (dialog.ShowModal() == wxID_OK)
769 {
770 wxString path = dialog.GetPath();
771
772 if (!path.empty())
773 {
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);
779 }
780 }
781 }
782
783 void MyFrame::OnSave(wxCommandEvent& event)
784 {
785 if (m_richTextCtrl->GetFilename().empty())
786 {
787 OnSaveAs(event);
788 return;
789 }
790 m_richTextCtrl->SaveFile();
791 }
792
793 void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
794 {
795 wxString filter = wxRichTextBuffer::GetExtWildcard(false, true);
796 wxString path;
797 wxString filename;
798
799 wxFileDialog dialog(this,
800 _("Choose a filename"),
801 path,
802 filename,
803 filter,
804 wxFD_SAVE);
805
806 if (dialog.ShowModal() == wxID_OK)
807 {
808 wxString path = dialog.GetPath();
809
810 if (!path.empty())
811 {
812 m_richTextCtrl->SaveFile(path);
813 }
814 }
815 }
816
817 void MyFrame::OnBold(wxCommandEvent& WXUNUSED(event))
818 {
819 m_richTextCtrl->ApplyBoldToSelection();
820 }
821
822 void MyFrame::OnItalic(wxCommandEvent& WXUNUSED(event))
823 {
824 m_richTextCtrl->ApplyItalicToSelection();
825 }
826
827 void MyFrame::OnUnderline(wxCommandEvent& WXUNUSED(event))
828 {
829 m_richTextCtrl->ApplyUnderlineToSelection();
830 }
831
832
833 void MyFrame::OnUpdateBold(wxUpdateUIEvent& event)
834 {
835 event.Check(m_richTextCtrl->IsSelectionBold());
836 }
837
838 void MyFrame::OnUpdateItalic(wxUpdateUIEvent& event)
839 {
840 event.Check(m_richTextCtrl->IsSelectionItalics());
841 }
842
843 void MyFrame::OnUpdateUnderline(wxUpdateUIEvent& event)
844 {
845 event.Check(m_richTextCtrl->IsSelectionUnderlined());
846 }
847
848 void MyFrame::OnAlignLeft(wxCommandEvent& WXUNUSED(event))
849 {
850 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT);
851 }
852
853 void MyFrame::OnAlignCentre(wxCommandEvent& WXUNUSED(event))
854 {
855 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE);
856 }
857
858 void MyFrame::OnAlignRight(wxCommandEvent& WXUNUSED(event))
859 {
860 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT);
861 }
862
863 void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent& event)
864 {
865 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT));
866 }
867
868 void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent& event)
869 {
870 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE));
871 }
872
873 void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent& event)
874 {
875 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT));
876 }
877
878 void MyFrame::OnFont(wxCommandEvent& WXUNUSED(event))
879 {
880 if (!m_richTextCtrl->HasSelection())
881 return;
882
883 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
884 wxFontData fontData;
885
886 wxTextAttrEx attr;
887 attr.SetFlags(wxTEXT_ATTR_FONT);
888
889 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
890 fontData.SetInitialFont(attr.GetFont());
891
892 wxFontDialog dialog(this, fontData);
893 if (dialog.ShowModal() == wxID_OK)
894 {
895 fontData = dialog.GetFontData();
896 attr.SetFlags(wxTEXT_ATTR_FONT);
897 attr.SetFont(fontData.GetChosenFont());
898 if (attr.GetFont().Ok())
899 {
900 m_richTextCtrl->SetStyle(range, attr);
901 }
902 }
903 }
904
905 void MyFrame::OnIndentMore(wxCommandEvent& WXUNUSED(event))
906 {
907 wxTextAttrEx attr;
908 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
909
910 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
911 {
912 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
913 if (m_richTextCtrl->HasSelection())
914 range = m_richTextCtrl->GetSelectionRange();
915
916 wxFontData fontData;
917 attr.SetLeftIndent(attr.GetLeftIndent() + 100);
918
919 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
920 m_richTextCtrl->SetStyle(range, attr);
921 }
922 }
923
924 void MyFrame::OnIndentLess(wxCommandEvent& WXUNUSED(event))
925 {
926 wxTextAttrEx attr;
927 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
928
929 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
930 {
931 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
932 if (m_richTextCtrl->HasSelection())
933 range = m_richTextCtrl->GetSelectionRange();
934
935 if (attr.GetLeftIndent() >= 100)
936 {
937 wxFontData fontData;
938 attr.SetLeftIndent(attr.GetLeftIndent() - 100);
939
940 m_richTextCtrl->SetStyle(range, attr);
941 }
942 }
943 }
944
945 void MyFrame::OnLineSpacingHalf(wxCommandEvent& WXUNUSED(event))
946 {
947 wxTextAttrEx attr;
948 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
949
950 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
951 {
952 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
953 if (m_richTextCtrl->HasSelection())
954 range = m_richTextCtrl->GetSelectionRange();
955
956 wxFontData fontData;
957 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
958 attr.SetLineSpacing(15);
959
960 m_richTextCtrl->SetStyle(range, attr);
961 }
962 }
963
964 void MyFrame::OnLineSpacingDouble(wxCommandEvent& WXUNUSED(event))
965 {
966 wxTextAttrEx attr;
967 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
968
969 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
970 {
971 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
972 if (m_richTextCtrl->HasSelection())
973 range = m_richTextCtrl->GetSelectionRange();
974
975 wxFontData fontData;
976 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
977 attr.SetLineSpacing(20);
978
979 m_richTextCtrl->SetStyle(range, attr);
980 }
981 }
982
983 void MyFrame::OnLineSpacingSingle(wxCommandEvent& WXUNUSED(event))
984 {
985 wxTextAttrEx attr;
986 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
987
988 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
989 {
990 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
991 if (m_richTextCtrl->HasSelection())
992 range = m_richTextCtrl->GetSelectionRange();
993
994 wxFontData fontData;
995 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
996 attr.SetLineSpacing(0); // Can also use 10
997
998 m_richTextCtrl->SetStyle(range, attr);
999 }
1000 }
1001
1002 void MyFrame::OnParagraphSpacingMore(wxCommandEvent& WXUNUSED(event))
1003 {
1004 wxTextAttrEx attr;
1005 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1006
1007 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1008 {
1009 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1010 if (m_richTextCtrl->HasSelection())
1011 range = m_richTextCtrl->GetSelectionRange();
1012
1013 wxFontData fontData;
1014 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() + 20);
1015
1016 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1017 m_richTextCtrl->SetStyle(range, attr);
1018 }
1019 }
1020
1021 void MyFrame::OnParagraphSpacingLess(wxCommandEvent& WXUNUSED(event))
1022 {
1023 wxTextAttrEx attr;
1024 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1025
1026 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1027 {
1028 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1029 if (m_richTextCtrl->HasSelection())
1030 range = m_richTextCtrl->GetSelectionRange();
1031
1032 if (attr.GetParagraphSpacingAfter() >= 20)
1033 {
1034 wxFontData fontData;
1035 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() - 20);
1036
1037 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1038 m_richTextCtrl->SetStyle(range, attr);
1039 }
1040 }
1041 }
1042
1043 void MyFrame::OnViewHTML(wxCommandEvent& WXUNUSED(event))
1044 {
1045 wxDialog dialog(this, wxID_ANY, _("HTML"), wxDefaultPosition, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE);
1046
1047 wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
1048 dialog.SetSizer(boxSizer);
1049
1050 wxHtmlWindow* win = new wxHtmlWindow(& dialog, wxID_ANY, wxDefaultPosition, wxSize(500, 400), wxSUNKEN_BORDER);
1051 boxSizer->Add(win, 1, wxALL, 5);
1052
1053 wxButton* cancelButton = new wxButton(& dialog, wxID_CANCEL, wxT("&Close"));
1054 boxSizer->Add(cancelButton, 0, wxALL|wxCENTRE, 5);
1055
1056 wxString text;
1057 wxStringOutputStream strStream(& text);
1058
1059 wxRichTextHTMLHandler htmlHandler;
1060 if (htmlHandler.SaveFile(& m_richTextCtrl->GetBuffer(), strStream))
1061 {
1062 win->SetPage(text);
1063 }
1064
1065 boxSizer->Fit(& dialog);
1066
1067 dialog.ShowModal();
1068 }