]> git.saurik.com Git - wxWidgets.git/blame - samples/richtext/richtext.cpp
Make a couple virtuals protected so they can be overridden.
[wxWidgets.git] / samples / richtext / richtext.cpp
CommitLineData
5d7836c4 1/////////////////////////////////////////////////////////////////////////////
dbf38e88 2// Name: samples/richtext/richtext.cpp
5d7836c4
JS
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"
bec80f4f 37#include "wx/stopwatch.h"
b4e415df 38#include "wx/sysopt.h"
5d7836c4 39
720cc10a
JS
40#if wxUSE_FILESYSTEM
41#include "wx/filesys.h"
42#include "wx/fs_mem.h"
43#endif
44
2569ebc2
JS
45#if wxUSE_HELP
46#include "wx/cshelp.h"
47#endif
48
e7092398 49#ifndef wxHAS_IMAGES_IN_RESOURCES
1226b3b3
WS
50 #include "../sample.xpm"
51#endif
52
5d7836c4
JS
53#include "bitmaps/smiley.xpm"
54// #include "bitmaps/idea.xpm"
55#include "bitmaps/zebra.xpm"
56
57#include "bitmaps/open.xpm"
58#include "bitmaps/save.xpm"
59#include "bitmaps/copy.xpm"
60#include "bitmaps/cut.xpm"
61#include "bitmaps/paste.xpm"
62#include "bitmaps/undo.xpm"
63#include "bitmaps/redo.xpm"
64#include "bitmaps/bold.xpm"
65#include "bitmaps/italic.xpm"
66#include "bitmaps/underline.xpm"
67
68#include "bitmaps/alignleft.xpm"
69#include "bitmaps/alignright.xpm"
70#include "bitmaps/centre.xpm"
71#include "bitmaps/font.xpm"
72#include "bitmaps/indentless.xpm"
73#include "bitmaps/indentmore.xpm"
74
011b3dcb
JS
75#include "wx/richtext/richtextctrl.h"
76#include "wx/richtext/richtextstyles.h"
77#include "wx/richtext/richtextxml.h"
b71e9aa4 78#include "wx/richtext/richtexthtml.h"
8c9c49a1
JS
79#include "wx/richtext/richtextformatdlg.h"
80#include "wx/richtext/richtextsymboldlg.h"
e4723634 81#include "wx/richtext/richtextstyledlg.h"
8881d9f0 82#include "wx/richtext/richtextprint.h"
cdaed652 83#include "wx/richtext/richtextimagedlg.h"
5d7836c4 84
32423dd8
JS
85// A custom field type
86class wxRichTextFieldTypePropertiesTest: public wxRichTextFieldTypeStandard
87{
88public:
89 wxRichTextFieldTypePropertiesTest(const wxString& name, const wxString& label, int displayStyle = wxRICHTEXT_FIELD_STYLE_RECTANGLE):
90 wxRichTextFieldTypeStandard(name, label, displayStyle)
91 {
92 }
93 wxRichTextFieldTypePropertiesTest(const wxString& name, const wxBitmap& bitmap, int displayStyle = wxRICHTEXT_FIELD_STYLE_RECTANGLE):
94 wxRichTextFieldTypeStandard(name, bitmap, displayStyle)
95 {
96 }
97
98 virtual bool CanEditProperties(wxRichTextField* WXUNUSED(obj)) const { return true; }
99 virtual bool EditProperties(wxRichTextField* WXUNUSED(obj), wxWindow* WXUNUSED(parent), wxRichTextBuffer* WXUNUSED(buffer))
100 {
101 wxString label = GetLabel();
102 wxMessageBox(wxString::Format(wxT("Editing %s"), label.c_str()));
103 return true;
104 }
105
106 virtual wxString GetPropertiesMenuLabel(wxRichTextField* WXUNUSED(obj)) const
107 {
108 return GetLabel();
109 }
110};
111
112// A custom composite field type
113class wxRichTextFieldTypeCompositeTest: public wxRichTextFieldTypePropertiesTest
114{
115public:
116 wxRichTextFieldTypeCompositeTest(const wxString& name, const wxString& label):
117 wxRichTextFieldTypePropertiesTest(name, label, wxRICHTEXT_FIELD_STYLE_COMPOSITE)
118 {
119 }
120
121 virtual bool UpdateField(wxRichTextBuffer* buffer, wxRichTextField* obj)
122 {
123 if (buffer)
124 {
125 wxRichTextAttr attr(buffer->GetAttributes());
126 attr.GetTextBoxAttr().Reset();
127 attr.SetParagraphSpacingAfter(0);
128 attr.SetLineSpacing(10);
129 obj->SetAttributes(attr);
130 }
131 obj->GetChildren().Clear();
132 wxRichTextParagraph* para = new wxRichTextParagraph;
133 wxRichTextPlainText* text = new wxRichTextPlainText(GetLabel());
134 para->AppendChild(text);
135 obj->AppendChild(para);
136 return true;
137 }
138};
139
5d7836c4
JS
140// ----------------------------------------------------------------------------
141// resources
142// ----------------------------------------------------------------------------
143
144// ----------------------------------------------------------------------------
145// private classes
146// ----------------------------------------------------------------------------
147
cc2aecde
JS
148// Define a new application type, each program should derive a class from wxApp
149class MyRichTextCtrl: public wxRichTextCtrl
150{
151public:
152 MyRichTextCtrl( wxWindow* parent, wxWindowID id = -1, const wxString& value = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
153 long style = wxRE_MULTILINE, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxTextCtrlNameStr):
154 wxRichTextCtrl(parent, id, value, pos, size, style, validator, name)
155 {
156 m_lockId = 0;
157 m_locked = false;
158 }
159
160 void SetLockId(long id) { m_lockId = id; }
161 long GetLockId() const { return m_lockId; }
162
163 void BeginLock() { m_lockId ++; m_locked = true; }
164 void EndLock() { m_locked = false; }
165 bool IsLocked() const { return m_locked; }
166
167 static void SetEnhancedDrawingHandler();
168
169 /**
170 Prepares the content just before insertion (or after buffer reset). Called by the same function in wxRichTextBuffer.
171 Currently is only called if undo mode is on.
172 */
173 virtual void PrepareContent(wxRichTextParagraphLayoutBox& container);
174
175 /**
176 Can we delete this range?
177 Sends an event to the control.
178 */
179 virtual bool CanDeleteRange(wxRichTextParagraphLayoutBox& container, const wxRichTextRange& range) const;
180
181 /**
182 Can we insert content at this position?
183 Sends an event to the control.
184 */
185 virtual bool CanInsertContent(wxRichTextParagraphLayoutBox& container, long pos) const;
186
187 long m_lockId;
188 bool m_locked;
189};
190
5d7836c4
JS
191// Define a new application type, each program should derive a class from wxApp
192class MyApp : public wxApp
193{
194public:
195 // override base class virtuals
196 // ----------------------------
197
198 // this one is called on application startup and is a good place for the app
199 // initialization (doing it here and not in the ctor allows to have an error
200 // return: if OnInit() returns false, the application terminates)
201 virtual bool OnInit();
202 virtual int OnExit();
203
204 void CreateStyles();
205
206 wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
8881d9f0 207 wxRichTextPrinting* GetPrinting() const { return m_printing; }
5d7836c4 208
8881d9f0
JS
209 wxRichTextStyleSheet* m_styleSheet;
210 wxRichTextPrinting* m_printing;
5d7836c4
JS
211};
212
213// Define a new frame type: this is going to be our main frame
214class MyFrame : public wxFrame
215{
216public:
217 // ctor(s)
218 MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
219 const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE);
220
221 // event handlers (these functions should _not_ be virtual)
222 void OnQuit(wxCommandEvent& event);
223 void OnAbout(wxCommandEvent& event);
224
225 void OnOpen(wxCommandEvent& event);
226 void OnSave(wxCommandEvent& event);
227 void OnSaveAs(wxCommandEvent& event);
228
229 void OnBold(wxCommandEvent& event);
230 void OnItalic(wxCommandEvent& event);
231 void OnUnderline(wxCommandEvent& event);
232
75936ec6
JS
233 void OnStrikethrough(wxCommandEvent& event);
234 void OnSuperscript(wxCommandEvent& event);
235 void OnSubscript(wxCommandEvent& event);
236
5d7836c4
JS
237 void OnUpdateBold(wxUpdateUIEvent& event);
238 void OnUpdateItalic(wxUpdateUIEvent& event);
239 void OnUpdateUnderline(wxUpdateUIEvent& event);
75936ec6
JS
240 void OnUpdateStrikethrough(wxUpdateUIEvent& event);
241 void OnUpdateSuperscript(wxUpdateUIEvent& event);
242 void OnUpdateSubscript(wxUpdateUIEvent& event);
5d7836c4
JS
243
244 void OnAlignLeft(wxCommandEvent& event);
245 void OnAlignCentre(wxCommandEvent& event);
246 void OnAlignRight(wxCommandEvent& event);
247
248 void OnUpdateAlignLeft(wxUpdateUIEvent& event);
249 void OnUpdateAlignCentre(wxUpdateUIEvent& event);
250 void OnUpdateAlignRight(wxUpdateUIEvent& event);
251
5d7836c4
JS
252 void OnIndentMore(wxCommandEvent& event);
253 void OnIndentLess(wxCommandEvent& event);
254
8c9c49a1 255 void OnFont(wxCommandEvent& event);
cdaed652
VZ
256 void OnImage(wxCommandEvent& event);
257 void OnUpdateImage(wxUpdateUIEvent& event);
8c9c49a1
JS
258 void OnParagraph(wxCommandEvent& event);
259 void OnFormat(wxCommandEvent& event);
260 void OnUpdateFormat(wxUpdateUIEvent& event);
261
262 void OnInsertSymbol(wxCommandEvent& event);
263
5d7836c4
JS
264 void OnLineSpacingHalf(wxCommandEvent& event);
265 void OnLineSpacingDouble(wxCommandEvent& event);
266 void OnLineSpacingSingle(wxCommandEvent& event);
267
268 void OnParagraphSpacingMore(wxCommandEvent& event);
269 void OnParagraphSpacingLess(wxCommandEvent& event);
270
082cfe55 271 void OnNumberList(wxCommandEvent& event);
e4723634 272 void OnBulletsAndNumbering(wxCommandEvent& event);
082cfe55
JS
273 void OnItemizeList(wxCommandEvent& event);
274 void OnRenumberList(wxCommandEvent& event);
275 void OnPromoteList(wxCommandEvent& event);
276 void OnDemoteList(wxCommandEvent& event);
277 void OnClearList(wxCommandEvent& event);
278
99404ab0
JS
279 void OnReload(wxCommandEvent& event);
280
5d7836c4
JS
281 void OnViewHTML(wxCommandEvent& event);
282
fe5aa22c 283 void OnSwitchStyleSheets(wxCommandEvent& event);
e4723634 284 void OnManageStyles(wxCommandEvent& event);
fe5aa22c 285
720cc10a
JS
286 void OnInsertURL(wxCommandEvent& event);
287 void OnURL(wxTextUrlEvent& event);
288 void OnStyleSheetReplacing(wxRichTextEvent& event);
289
8881d9f0
JS
290 void OnPrint(wxCommandEvent& event);
291 void OnPreview(wxCommandEvent& event);
292 void OnPageSetup(wxCommandEvent& event);
293
cdaed652 294 void OnInsertImage(wxCommandEvent& event);
32423dd8
JS
295
296 void OnSetFontScale(wxCommandEvent& event);
297 void OnSetDimensionScale(wxCommandEvent& event);
004867db
FM
298protected:
299
5d7836c4
JS
300 // Forward command events to the current rich text control, if any
301 bool ProcessEvent(wxEvent& event);
302
99404ab0
JS
303 // Write text
304 void WriteInitialText();
305
5d7836c4
JS
306private:
307 // any class wishing to process wxWidgets events must use this macro
308 DECLARE_EVENT_TABLE()
309
cc2aecde 310 MyRichTextCtrl* m_richTextCtrl;
5d7836c4
JS
311};
312
313// ----------------------------------------------------------------------------
314// constants
315// ----------------------------------------------------------------------------
316
317// IDs for the controls and the menu commands
318enum
319{
320 // menu items
321 ID_Quit = wxID_EXIT,
322 ID_About = wxID_ABOUT,
323
324 ID_FORMAT_BOLD = 100,
325 ID_FORMAT_ITALIC,
326 ID_FORMAT_UNDERLINE,
75936ec6
JS
327 ID_FORMAT_STRIKETHROUGH,
328 ID_FORMAT_SUPERSCRIPT,
329 ID_FORMAT_SUBSCRIPT,
5d7836c4 330 ID_FORMAT_FONT,
cdaed652 331 ID_FORMAT_IMAGE,
8c9c49a1
JS
332 ID_FORMAT_PARAGRAPH,
333 ID_FORMAT_CONTENT,
334
99404ab0
JS
335 ID_RELOAD,
336
8c9c49a1 337 ID_INSERT_SYMBOL,
720cc10a 338 ID_INSERT_URL,
cdaed652 339 ID_INSERT_IMAGE,
5d7836c4
JS
340
341 ID_FORMAT_ALIGN_LEFT,
342 ID_FORMAT_ALIGN_CENTRE,
343 ID_FORMAT_ALIGN_RIGHT,
344
345 ID_FORMAT_INDENT_MORE,
346 ID_FORMAT_INDENT_LESS,
347
348 ID_FORMAT_PARAGRAPH_SPACING_MORE,
349 ID_FORMAT_PARAGRAPH_SPACING_LESS,
350
351 ID_FORMAT_LINE_SPACING_HALF,
352 ID_FORMAT_LINE_SPACING_DOUBLE,
353 ID_FORMAT_LINE_SPACING_SINGLE,
354
082cfe55 355 ID_FORMAT_NUMBER_LIST,
e4723634 356 ID_FORMAT_BULLETS_AND_NUMBERING,
082cfe55
JS
357 ID_FORMAT_ITEMIZE_LIST,
358 ID_FORMAT_RENUMBER_LIST,
359 ID_FORMAT_PROMOTE_LIST,
360 ID_FORMAT_DEMOTE_LIST,
361 ID_FORMAT_CLEAR_LIST,
362
32423dd8
JS
363 ID_SET_FONT_SCALE,
364 ID_SET_DIMENSION_SCALE,
365
fe5aa22c
JS
366 ID_VIEW_HTML,
367 ID_SWITCH_STYLE_SHEETS,
e4723634 368 ID_MANAGE_STYLES,
fe5aa22c 369
8881d9f0
JS
370 ID_PRINT,
371 ID_PREVIEW,
372 ID_PAGE_SETUP,
373
fe5aa22c
JS
374 ID_RICHTEXT_CTRL,
375 ID_RICHTEXT_STYLE_LIST,
376 ID_RICHTEXT_STYLE_COMBO
5d7836c4
JS
377};
378
379// ----------------------------------------------------------------------------
380// event tables and other macros for wxWidgets
381// ----------------------------------------------------------------------------
382
383// the event tables connect the wxWidgets events with the functions (event
384// handlers) which process them. It can be also done at run-time, but for the
385// simple menu events like this the static method is much simpler.
386BEGIN_EVENT_TABLE(MyFrame, wxFrame)
387 EVT_MENU(ID_Quit, MyFrame::OnQuit)
388 EVT_MENU(ID_About, MyFrame::OnAbout)
389
390 EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
391 EVT_MENU(wxID_SAVE, MyFrame::OnSave)
392 EVT_MENU(wxID_SAVEAS, MyFrame::OnSaveAs)
393
394 EVT_MENU(ID_FORMAT_BOLD, MyFrame::OnBold)
395 EVT_MENU(ID_FORMAT_ITALIC, MyFrame::OnItalic)
396 EVT_MENU(ID_FORMAT_UNDERLINE, MyFrame::OnUnderline)
397
75936ec6
JS
398 EVT_MENU(ID_FORMAT_STRIKETHROUGH, MyFrame::OnStrikethrough)
399 EVT_MENU(ID_FORMAT_SUPERSCRIPT, MyFrame::OnSuperscript)
400 EVT_MENU(ID_FORMAT_SUBSCRIPT, MyFrame::OnSubscript)
401
5d7836c4
JS
402 EVT_UPDATE_UI(ID_FORMAT_BOLD, MyFrame::OnUpdateBold)
403 EVT_UPDATE_UI(ID_FORMAT_ITALIC, MyFrame::OnUpdateItalic)
404 EVT_UPDATE_UI(ID_FORMAT_UNDERLINE, MyFrame::OnUpdateUnderline)
405
75936ec6
JS
406 EVT_UPDATE_UI(ID_FORMAT_STRIKETHROUGH, MyFrame::OnUpdateStrikethrough)
407 EVT_UPDATE_UI(ID_FORMAT_SUPERSCRIPT, MyFrame::OnUpdateSuperscript)
408 EVT_UPDATE_UI(ID_FORMAT_SUBSCRIPT, MyFrame::OnUpdateSubscript)
409
5d7836c4
JS
410 EVT_MENU(ID_FORMAT_ALIGN_LEFT, MyFrame::OnAlignLeft)
411 EVT_MENU(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnAlignCentre)
412 EVT_MENU(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnAlignRight)
413
414 EVT_UPDATE_UI(ID_FORMAT_ALIGN_LEFT, MyFrame::OnUpdateAlignLeft)
415 EVT_UPDATE_UI(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnUpdateAlignCentre)
416 EVT_UPDATE_UI(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnUpdateAlignRight)
417
418 EVT_MENU(ID_FORMAT_FONT, MyFrame::OnFont)
cdaed652 419 EVT_MENU(ID_FORMAT_IMAGE, MyFrame::OnImage)
8c9c49a1
JS
420 EVT_MENU(ID_FORMAT_PARAGRAPH, MyFrame::OnParagraph)
421 EVT_MENU(ID_FORMAT_CONTENT, MyFrame::OnFormat)
422 EVT_UPDATE_UI(ID_FORMAT_CONTENT, MyFrame::OnUpdateFormat)
423 EVT_UPDATE_UI(ID_FORMAT_FONT, MyFrame::OnUpdateFormat)
cdaed652 424 EVT_UPDATE_UI(ID_FORMAT_IMAGE, MyFrame::OnUpdateImage)
8c9c49a1 425 EVT_UPDATE_UI(ID_FORMAT_PARAGRAPH, MyFrame::OnUpdateFormat)
5d7836c4
JS
426 EVT_MENU(ID_FORMAT_INDENT_MORE, MyFrame::OnIndentMore)
427 EVT_MENU(ID_FORMAT_INDENT_LESS, MyFrame::OnIndentLess)
428
429 EVT_MENU(ID_FORMAT_LINE_SPACING_HALF, MyFrame::OnLineSpacingHalf)
430 EVT_MENU(ID_FORMAT_LINE_SPACING_SINGLE, MyFrame::OnLineSpacingSingle)
431 EVT_MENU(ID_FORMAT_LINE_SPACING_DOUBLE, MyFrame::OnLineSpacingDouble)
432
433 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_MORE, MyFrame::OnParagraphSpacingMore)
434 EVT_MENU(ID_FORMAT_PARAGRAPH_SPACING_LESS, MyFrame::OnParagraphSpacingLess)
435
99404ab0
JS
436 EVT_MENU(ID_RELOAD, MyFrame::OnReload)
437
8c9c49a1 438 EVT_MENU(ID_INSERT_SYMBOL, MyFrame::OnInsertSymbol)
720cc10a 439 EVT_MENU(ID_INSERT_URL, MyFrame::OnInsertURL)
cdaed652 440 EVT_MENU(ID_INSERT_IMAGE, MyFrame::OnInsertImage)
8c9c49a1 441
082cfe55 442 EVT_MENU(ID_FORMAT_NUMBER_LIST, MyFrame::OnNumberList)
e4723634 443 EVT_MENU(ID_FORMAT_BULLETS_AND_NUMBERING, MyFrame::OnBulletsAndNumbering)
082cfe55
JS
444 EVT_MENU(ID_FORMAT_ITEMIZE_LIST, MyFrame::OnItemizeList)
445 EVT_MENU(ID_FORMAT_RENUMBER_LIST, MyFrame::OnRenumberList)
446 EVT_MENU(ID_FORMAT_PROMOTE_LIST, MyFrame::OnPromoteList)
447 EVT_MENU(ID_FORMAT_DEMOTE_LIST, MyFrame::OnDemoteList)
448 EVT_MENU(ID_FORMAT_CLEAR_LIST, MyFrame::OnClearList)
449
5d7836c4 450 EVT_MENU(ID_VIEW_HTML, MyFrame::OnViewHTML)
fe5aa22c 451 EVT_MENU(ID_SWITCH_STYLE_SHEETS, MyFrame::OnSwitchStyleSheets)
e4723634 452 EVT_MENU(ID_MANAGE_STYLES, MyFrame::OnManageStyles)
720cc10a 453
8881d9f0
JS
454 EVT_MENU(ID_PRINT, MyFrame::OnPrint)
455 EVT_MENU(ID_PREVIEW, MyFrame::OnPreview)
456 EVT_MENU(ID_PAGE_SETUP, MyFrame::OnPageSetup)
457
720cc10a
JS
458 EVT_TEXT_URL(wxID_ANY, MyFrame::OnURL)
459 EVT_RICHTEXT_STYLESHEET_REPLACING(wxID_ANY, MyFrame::OnStyleSheetReplacing)
32423dd8
JS
460
461 EVT_MENU(ID_SET_FONT_SCALE, MyFrame::OnSetFontScale)
462 EVT_MENU(ID_SET_DIMENSION_SCALE, MyFrame::OnSetDimensionScale)
5d7836c4
JS
463END_EVENT_TABLE()
464
465// Create a new application object: this macro will allow wxWidgets to create
466// the application object during program execution (it's better than using a
467// static object for many reasons) and also implements the accessor function
468// wxGetApp() which will return the reference of the right type (i.e. MyApp and
469// not wxApp)
470IMPLEMENT_APP(MyApp)
471
472// ============================================================================
473// implementation
474// ============================================================================
475
476// ----------------------------------------------------------------------------
477// the application class
478// ----------------------------------------------------------------------------
479
480// 'Main program' equivalent: the program execution "starts" here
481bool MyApp::OnInit()
482{
45e6e6f8
VZ
483 if ( !wxApp::OnInit() )
484 return false;
485
2569ebc2
JS
486#if wxUSE_HELP
487 wxHelpProvider::Set(new wxSimpleHelpProvider);
488#endif
489
5d7836c4 490 m_styleSheet = new wxRichTextStyleSheet;
8881d9f0
JS
491 m_printing = new wxRichTextPrinting(wxT("Test Document"));
492
493 m_printing->SetFooterText(wxT("@TITLE@"), wxRICHTEXT_PAGE_ALL, wxRICHTEXT_PAGE_CENTRE);
494 m_printing->SetFooterText(wxT("Page @PAGENUM@"), wxRICHTEXT_PAGE_ALL, wxRICHTEXT_PAGE_RIGHT);
5d7836c4
JS
495
496 CreateStyles();
497
cc2aecde
JS
498 MyRichTextCtrl::SetEnhancedDrawingHandler();
499
5d7836c4
JS
500 // Add extra handlers (plain text is automatically added)
501 wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler);
502 wxRichTextBuffer::AddHandler(new wxRichTextHTMLHandler);
503
32423dd8
JS
504 // Add field types
505
506 wxRichTextBuffer::AddFieldType(new wxRichTextFieldTypePropertiesTest(wxT("rectangle"), wxT("RECTANGLE"), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_RECTANGLE));
507
508 wxRichTextFieldTypeStandard* s1 = new wxRichTextFieldTypeStandard(wxT("begin-section"), wxT("SECTION"), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_START_TAG);
509 s1->SetBackgroundColour(*wxBLUE);
510
511 wxRichTextFieldTypeStandard* s2 = new wxRichTextFieldTypeStandard(wxT("end-section"), wxT("SECTION"), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_END_TAG);
512 s2->SetBackgroundColour(*wxBLUE);
513
514 wxRichTextFieldTypeStandard* s3 = new wxRichTextFieldTypeStandard(wxT("bitmap"), wxBitmap(paste_xpm), wxRichTextFieldTypeStandard::wxRICHTEXT_FIELD_STYLE_NO_BORDER);
515
516 wxRichTextBuffer::AddFieldType(s1);
517 wxRichTextBuffer::AddFieldType(s2);
518 wxRichTextBuffer::AddFieldType(s3);
519
520 wxRichTextFieldTypeCompositeTest* s4 = new wxRichTextFieldTypeCompositeTest(wxT("composite"), wxT("This is a field value"));
521 wxRichTextBuffer::AddFieldType(s4);
522
5d7836c4
JS
523 // Add image handlers
524#if wxUSE_LIBPNG
525 wxImage::AddHandler( new wxPNGHandler );
526#endif
9a173d48 527
5d7836c4
JS
528#if wxUSE_LIBJPEG
529 wxImage::AddHandler( new wxJPEGHandler );
530#endif
531
532#if wxUSE_GIF
533 wxImage::AddHandler( new wxGIFHandler );
534#endif
535
720cc10a
JS
536#if wxUSE_FILESYSTEM
537 wxFileSystem::AddHandler( new wxMemoryFSHandler );
538#endif
539
5d7836c4 540 // create the main application window
9a83f860 541 MyFrame *frame = new MyFrame(wxT("wxRichTextCtrl Sample"), wxID_ANY, wxDefaultPosition, wxSize(700, 600));
5d7836c4 542
8881d9f0
JS
543 m_printing->SetParentWindow(frame);
544
5d7836c4
JS
545 // and show it (the frames, unlike simple controls, are not shown when
546 // created initially)
547 frame->Show(true);
548
549 // success: wxApp::OnRun() will be called which will enter the main message
550 // loop and the application will run. If we returned false here, the
551 // application would exit immediately.
552 return true;
553}
554
555int MyApp::OnExit()
556{
8881d9f0 557 delete m_printing;
5d7836c4 558 delete m_styleSheet;
8881d9f0 559
5d7836c4
JS
560 return 0;
561}
562
563void MyApp::CreateStyles()
564{
565 // Paragraph styles
566
567 wxFont romanFont(12, wxROMAN, wxNORMAL, wxNORMAL);
568 wxFont swissFont(12, wxSWISS, wxNORMAL, wxNORMAL);
569
570 wxRichTextParagraphStyleDefinition* normalPara = new wxRichTextParagraphStyleDefinition(wxT("Normal"));
571 wxRichTextAttr normalAttr;
572 normalAttr.SetFontFaceName(romanFont.GetFaceName());
573 normalAttr.SetFontSize(12);
574 // Let's set all attributes for this style
575 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|
576 wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|
577 wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER);
578 normalPara->SetStyle(normalAttr);
9a173d48 579
5d7836c4
JS
580 m_styleSheet->AddParagraphStyle(normalPara);
581
582 wxRichTextParagraphStyleDefinition* indentedPara = new wxRichTextParagraphStyleDefinition(wxT("Indented"));
583 wxRichTextAttr indentedAttr;
584 indentedAttr.SetFontFaceName(romanFont.GetFaceName());
585 indentedAttr.SetFontSize(12);
586 indentedAttr.SetLeftIndent(100, 0);
587 // We only want to affect indentation
588 indentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
589 indentedPara->SetStyle(indentedAttr);
9a173d48 590
5d7836c4
JS
591 m_styleSheet->AddParagraphStyle(indentedPara);
592
27e20452
JS
593 wxRichTextParagraphStyleDefinition* indentedPara2 = new wxRichTextParagraphStyleDefinition(wxT("Red Bold Indented"));
594 wxRichTextAttr indentedAttr2;
595 indentedAttr2.SetFontFaceName(romanFont.GetFaceName());
596 indentedAttr2.SetFontSize(12);
00621a0d 597 indentedAttr2.SetFontWeight(wxFONTWEIGHT_BOLD);
27e20452
JS
598 indentedAttr2.SetTextColour(*wxRED);
599 indentedAttr2.SetFontSize(12);
600 indentedAttr2.SetLeftIndent(100, 0);
601 // We want to affect indentation, font and text colour
602 indentedAttr2.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_FONT|wxTEXT_ATTR_TEXT_COLOUR);
603 indentedPara2->SetStyle(indentedAttr2);
604
605 m_styleSheet->AddParagraphStyle(indentedPara2);
606
5d7836c4
JS
607 wxRichTextParagraphStyleDefinition* flIndentedPara = new wxRichTextParagraphStyleDefinition(wxT("First Line Indented"));
608 wxRichTextAttr flIndentedAttr;
609 flIndentedAttr.SetFontFaceName(swissFont.GetFaceName());
610 flIndentedAttr.SetFontSize(12);
611 flIndentedAttr.SetLeftIndent(100, -100);
612 // We only want to affect indentation
613 flIndentedAttr.SetFlags(wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT);
614 flIndentedPara->SetStyle(flIndentedAttr);
9a173d48 615
5d7836c4
JS
616 m_styleSheet->AddParagraphStyle(flIndentedPara);
617
618 // Character styles
619
620 wxRichTextCharacterStyleDefinition* boldDef = new wxRichTextCharacterStyleDefinition(wxT("Bold"));
621 wxRichTextAttr boldAttr;
622 boldAttr.SetFontFaceName(romanFont.GetFaceName());
623 boldAttr.SetFontSize(12);
00621a0d 624 boldAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
5d7836c4
JS
625 // We only want to affect boldness
626 boldAttr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
627 boldDef->SetStyle(boldAttr);
9a173d48 628
5d7836c4
JS
629 m_styleSheet->AddCharacterStyle(boldDef);
630
631 wxRichTextCharacterStyleDefinition* italicDef = new wxRichTextCharacterStyleDefinition(wxT("Italic"));
632 wxRichTextAttr italicAttr;
633 italicAttr.SetFontFaceName(romanFont.GetFaceName());
634 italicAttr.SetFontSize(12);
00621a0d 635 italicAttr.SetFontStyle(wxFONTSTYLE_ITALIC);
5d7836c4
JS
636 // We only want to affect italics
637 italicAttr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
638 italicDef->SetStyle(italicAttr);
9a173d48 639
5d7836c4
JS
640 m_styleSheet->AddCharacterStyle(italicDef);
641
642 wxRichTextCharacterStyleDefinition* redDef = new wxRichTextCharacterStyleDefinition(wxT("Red Bold"));
643 wxRichTextAttr redAttr;
644 redAttr.SetFontFaceName(romanFont.GetFaceName());
645 redAttr.SetFontSize(12);
00621a0d 646 redAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
5d7836c4
JS
647 redAttr.SetTextColour(*wxRED);
648 // We only want to affect colour, weight and face
649 redAttr.SetFlags(wxTEXT_ATTR_FONT_FACE|wxTEXT_ATTR_FONT_WEIGHT|wxTEXT_ATTR_TEXT_COLOUR);
650 redDef->SetStyle(redAttr);
9a173d48 651
5d7836c4 652 m_styleSheet->AddCharacterStyle(redDef);
082cfe55
JS
653
654 wxRichTextListStyleDefinition* bulletList = new wxRichTextListStyleDefinition(wxT("Bullet List 1"));
655 int i;
656 for (i = 0; i < 10; i++)
657 {
720cc10a 658 wxString bulletText;
082cfe55 659 if (i == 0)
720cc10a 660 bulletText = wxT("standard/circle");
082cfe55 661 else if (i == 1)
720cc10a 662 bulletText = wxT("standard/square");
082cfe55 663 else if (i == 2)
720cc10a 664 bulletText = wxT("standard/circle");
082cfe55 665 else if (i == 3)
720cc10a 666 bulletText = wxT("standard/square");
082cfe55 667 else
720cc10a 668 bulletText = wxT("standard/circle");
082cfe55 669
720cc10a 670 bulletList->SetAttributes(i, (i+1)*60, 60, wxTEXT_ATTR_BULLET_STYLE_STANDARD, bulletText);
082cfe55
JS
671 }
672
673 m_styleSheet->AddListStyle(bulletList);
674
675 wxRichTextListStyleDefinition* numberedList = new wxRichTextListStyleDefinition(wxT("Numbered List 1"));
676 for (i = 0; i < 10; i++)
677 {
678 long numberStyle;
679 if (i == 0)
680 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
681 else if (i == 1)
682 numberStyle = wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES;
683 else if (i == 2)
684 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES;
685 else if (i == 3)
686 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER|wxTEXT_ATTR_BULLET_STYLE_PARENTHESES;
687 else
688 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
689
720cc10a
JS
690 numberStyle |= wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT;
691
082cfe55
JS
692 numberedList->SetAttributes(i, (i+1)*60, 60, numberStyle);
693 }
694
695 m_styleSheet->AddListStyle(numberedList);
720cc10a
JS
696
697 wxRichTextListStyleDefinition* outlineList = new wxRichTextListStyleDefinition(wxT("Outline List 1"));
698 for (i = 0; i < 10; i++)
699 {
700 long numberStyle;
701 if (i < 4)
702 numberStyle = wxTEXT_ATTR_BULLET_STYLE_OUTLINE|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
703 else
704 numberStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD;
705
706 outlineList->SetAttributes(i, (i+1)*120, 120, numberStyle);
707 }
708
709 m_styleSheet->AddListStyle(outlineList);
5d7836c4
JS
710}
711
712// ----------------------------------------------------------------------------
713// main frame
714// ----------------------------------------------------------------------------
715
716// frame constructor
717MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
718 const wxSize& size, long style)
719 : wxFrame(NULL, id, title, pos, size, style)
720{
b4e415df
JS
721#ifdef __WXMAC__
722 SetWindowVariant(wxWINDOW_VARIANT_SMALL);
723#endif
724
5d7836c4 725 // set the frame icon
1226b3b3 726 SetIcon(wxICON(sample));
5d7836c4
JS
727
728 // create a menu bar
729 wxMenu *fileMenu = new wxMenu;
730
731 // the "About" item should be in the help menu
732 wxMenu *helpMenu = new wxMenu;
2d143b66 733 helpMenu->Append(ID_About, wxT("&About\tF1"), wxT("Show about dialog"));
5d7836c4 734
9a83f860
VZ
735 fileMenu->Append(wxID_OPEN, wxT("&Open\tCtrl+O"), wxT("Open a file"));
736 fileMenu->Append(wxID_SAVE, wxT("&Save\tCtrl+S"), wxT("Save a file"));
737 fileMenu->Append(wxID_SAVEAS, wxT("&Save As...\tF12"), wxT("Save to a new file"));
5d7836c4 738 fileMenu->AppendSeparator();
9a83f860 739 fileMenu->Append(ID_RELOAD, wxT("&Reload Text\tF2"), wxT("Reload the initial text"));
99404ab0 740 fileMenu->AppendSeparator();
9a83f860
VZ
741 fileMenu->Append(ID_PAGE_SETUP, wxT("Page Set&up..."), wxT("Page setup"));
742 fileMenu->Append(ID_PRINT, wxT("&Print...\tCtrl+P"), wxT("Print"));
743 fileMenu->Append(ID_PREVIEW, wxT("Print Pre&view"), wxT("Print preview"));
8881d9f0 744 fileMenu->AppendSeparator();
9a83f860 745 fileMenu->Append(ID_VIEW_HTML, wxT("&View as HTML"), wxT("View HTML"));
5d7836c4 746 fileMenu->AppendSeparator();
9a83f860 747 fileMenu->Append(ID_Quit, wxT("E&xit\tAlt+X"), wxT("Quit this program"));
5d7836c4
JS
748
749 wxMenu* editMenu = new wxMenu;
750 editMenu->Append(wxID_UNDO, _("&Undo\tCtrl+Z"));
751 editMenu->Append(wxID_REDO, _("&Redo\tCtrl+Y"));
752 editMenu->AppendSeparator();
753 editMenu->Append(wxID_CUT, _("Cu&t\tCtrl+X"));
754 editMenu->Append(wxID_COPY, _("&Copy\tCtrl+C"));
755 editMenu->Append(wxID_PASTE, _("&Paste\tCtrl+V"));
756
5d7836c4
JS
757 editMenu->AppendSeparator();
758 editMenu->Append(wxID_SELECTALL, _("Select A&ll\tCtrl+A"));
5d7836c4 759 editMenu->AppendSeparator();
32423dd8
JS
760 editMenu->Append(ID_SET_FONT_SCALE, _("Set &Text Scale..."));
761 editMenu->Append(ID_SET_DIMENSION_SCALE, _("Set &Dimension Scale..."));
5d7836c4
JS
762
763 wxMenu* formatMenu = new wxMenu;
764 formatMenu->AppendCheckItem(ID_FORMAT_BOLD, _("&Bold\tCtrl+B"));
765 formatMenu->AppendCheckItem(ID_FORMAT_ITALIC, _("&Italic\tCtrl+I"));
766 formatMenu->AppendCheckItem(ID_FORMAT_UNDERLINE, _("&Underline\tCtrl+U"));
767 formatMenu->AppendSeparator();
75936ec6
JS
768 formatMenu->AppendCheckItem(ID_FORMAT_STRIKETHROUGH, _("Stri&kethrough"));
769 formatMenu->AppendCheckItem(ID_FORMAT_SUPERSCRIPT, _("Superscrip&t"));
770 formatMenu->AppendCheckItem(ID_FORMAT_SUBSCRIPT, _("Subscrip&t"));
771 formatMenu->AppendSeparator();
5d7836c4
JS
772 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_LEFT, _("L&eft Align"));
773 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT, _("&Right Align"));
774 formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE, _("&Centre"));
775 formatMenu->AppendSeparator();
c4cd20cf 776 formatMenu->Append(ID_FORMAT_INDENT_MORE, _("Indent &More"));
5d7836c4
JS
777 formatMenu->Append(ID_FORMAT_INDENT_LESS, _("Indent &Less"));
778 formatMenu->AppendSeparator();
779 formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_MORE, _("Increase Paragraph &Spacing"));
780 formatMenu->Append(ID_FORMAT_PARAGRAPH_SPACING_LESS, _("Decrease &Paragraph Spacing"));
781 formatMenu->AppendSeparator();
782 formatMenu->Append(ID_FORMAT_LINE_SPACING_SINGLE, _("Normal Line Spacing"));
783 formatMenu->Append(ID_FORMAT_LINE_SPACING_HALF, _("1.5 Line Spacing"));
784 formatMenu->Append(ID_FORMAT_LINE_SPACING_DOUBLE, _("Double Line Spacing"));
785 formatMenu->AppendSeparator();
786 formatMenu->Append(ID_FORMAT_FONT, _("&Font..."));
cdaed652 787 formatMenu->Append(ID_FORMAT_IMAGE, _("Image Property"));
8c9c49a1
JS
788 formatMenu->Append(ID_FORMAT_PARAGRAPH, _("&Paragraph..."));
789 formatMenu->Append(ID_FORMAT_CONTENT, _("Font and Pa&ragraph...\tShift+Ctrl+F"));
fe5aa22c
JS
790 formatMenu->AppendSeparator();
791 formatMenu->Append(ID_SWITCH_STYLE_SHEETS, _("&Switch Style Sheets"));
e4723634
JS
792 formatMenu->Append(ID_MANAGE_STYLES, _("&Manage Styles"));
793
794 wxMenu* listsMenu = new wxMenu;
795 listsMenu->Append(ID_FORMAT_BULLETS_AND_NUMBERING, _("Bullets and &Numbering..."));
796 listsMenu->AppendSeparator();
797 listsMenu->Append(ID_FORMAT_NUMBER_LIST, _("Number List"));
798 listsMenu->Append(ID_FORMAT_ITEMIZE_LIST, _("Itemize List"));
799 listsMenu->Append(ID_FORMAT_RENUMBER_LIST, _("Renumber List"));
800 listsMenu->Append(ID_FORMAT_PROMOTE_LIST, _("Promote List Items"));
801 listsMenu->Append(ID_FORMAT_DEMOTE_LIST, _("Demote List Items"));
802 listsMenu->Append(ID_FORMAT_CLEAR_LIST, _("Clear List Formatting"));
5d7836c4 803
8c9c49a1
JS
804 wxMenu* insertMenu = new wxMenu;
805 insertMenu->Append(ID_INSERT_SYMBOL, _("&Symbol...\tCtrl+I"));
720cc10a 806 insertMenu->Append(ID_INSERT_URL, _("&URL..."));
cdaed652 807 insertMenu->Append(ID_INSERT_IMAGE, _("&Image..."));
8c9c49a1 808
5d7836c4
JS
809 // now append the freshly created menu to the menu bar...
810 wxMenuBar *menuBar = new wxMenuBar();
9a83f860
VZ
811 menuBar->Append(fileMenu, wxT("&File"));
812 menuBar->Append(editMenu, wxT("&Edit"));
813 menuBar->Append(formatMenu, wxT("F&ormat"));
814 menuBar->Append(listsMenu, wxT("&Lists"));
815 menuBar->Append(insertMenu, wxT("&Insert"));
816 menuBar->Append(helpMenu, wxT("&Help"));
5d7836c4
JS
817
818 // ... and attach this menu bar to the frame
819 SetMenuBar(menuBar);
820
821 // create a status bar just for fun (by default with 1 pane only)
4753c7ce
WS
822 // but don't create it on limited screen space (WinCE)
823 bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA;
824
9a173d48 825#if wxUSE_STATUSBAR
4753c7ce
WS
826 if ( !is_pda )
827 {
828 CreateStatusBar(2);
9a83f860 829 SetStatusText(wxT("Welcome to wxRichTextCtrl!"));
4753c7ce 830 }
9a173d48 831#endif
5d7836c4 832
b4e415df
JS
833 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
834 SetSizer(sizer);
835
836 // On Mac, don't create a 'native' wxToolBar because small bitmaps are not supported by native
837 // toolbars. On Mac, a non-native, small-bitmap toolbar doesn't show unless it is explicitly
838 // managed, hence the use of sizers. In a real application, use larger icons for the main
839 // toolbar to avoid the need for this workaround. Or, use the toolbar in a container window
840 // as part of a more complex hierarchy, and the toolbar will automatically be non-native.
841
842 wxSystemOptions::SetOption(wxT("mac.toolbar.no-native"), 1);
843
844 wxToolBar* toolBar = new wxToolBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
845 wxNO_BORDER|wxTB_FLAT|wxTB_NODIVIDER|wxTB_NOALIGN);
846
847 sizer->Add(toolBar, 0, wxEXPAND);
5d7836c4 848
bbe28fbb
PC
849 toolBar->AddTool(wxID_OPEN, wxEmptyString, wxBitmap(open_xpm), _("Open"));
850 toolBar->AddTool(wxID_SAVEAS, wxEmptyString, wxBitmap(save_xpm), _("Save"));
5d7836c4 851 toolBar->AddSeparator();
bbe28fbb
PC
852 toolBar->AddTool(wxID_CUT, wxEmptyString, wxBitmap(cut_xpm), _("Cut"));
853 toolBar->AddTool(wxID_COPY, wxEmptyString, wxBitmap(copy_xpm), _("Copy"));
854 toolBar->AddTool(wxID_PASTE, wxEmptyString, wxBitmap(paste_xpm), _("Paste"));
5d7836c4 855 toolBar->AddSeparator();
bbe28fbb
PC
856 toolBar->AddTool(wxID_UNDO, wxEmptyString, wxBitmap(undo_xpm), _("Undo"));
857 toolBar->AddTool(wxID_REDO, wxEmptyString, wxBitmap(redo_xpm), _("Redo"));
5d7836c4 858 toolBar->AddSeparator();
bbe28fbb
PC
859 toolBar->AddCheckTool(ID_FORMAT_BOLD, wxEmptyString, wxBitmap(bold_xpm), wxNullBitmap, _("Bold"));
860 toolBar->AddCheckTool(ID_FORMAT_ITALIC, wxEmptyString, wxBitmap(italic_xpm), wxNullBitmap, _("Italic"));
861 toolBar->AddCheckTool(ID_FORMAT_UNDERLINE, wxEmptyString, wxBitmap(underline_xpm), wxNullBitmap, _("Underline"));
5d7836c4 862 toolBar->AddSeparator();
bbe28fbb
PC
863 toolBar->AddCheckTool(ID_FORMAT_ALIGN_LEFT, wxEmptyString, wxBitmap(alignleft_xpm), wxNullBitmap, _("Align Left"));
864 toolBar->AddCheckTool(ID_FORMAT_ALIGN_CENTRE, wxEmptyString, wxBitmap(centre_xpm), wxNullBitmap, _("Centre"));
865 toolBar->AddCheckTool(ID_FORMAT_ALIGN_RIGHT, wxEmptyString, wxBitmap(alignright_xpm), wxNullBitmap, _("Align Right"));
5d7836c4 866 toolBar->AddSeparator();
bbe28fbb
PC
867 toolBar->AddTool(ID_FORMAT_INDENT_LESS, wxEmptyString, wxBitmap(indentless_xpm), _("Indent Less"));
868 toolBar->AddTool(ID_FORMAT_INDENT_MORE, wxEmptyString, wxBitmap(indentmore_xpm), _("Indent More"));
5d7836c4 869 toolBar->AddSeparator();
bbe28fbb 870 toolBar->AddTool(ID_FORMAT_FONT, wxEmptyString, wxBitmap(font_xpm), _("Font"));
b4e415df 871 toolBar->AddSeparator();
5d7836c4 872
b4e415df 873 wxRichTextStyleComboCtrl* combo = new wxRichTextStyleComboCtrl(toolBar, ID_RICHTEXT_STYLE_COMBO, wxDefaultPosition, wxSize(160, -1), wxCB_READONLY);
e637208a
JS
874 toolBar->AddControl(combo);
875
5d7836c4
JS
876 toolBar->Realize();
877
b4e415df
JS
878 wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxSize(100,100), wxSP_LIVE_UPDATE);
879 sizer->Add(splitter, 1, wxEXPAND);
5d7836c4
JS
880
881 wxFont textFont = wxFont(12, wxROMAN, wxNORMAL, wxNORMAL);
882 wxFont boldFont = wxFont(12, wxROMAN, wxNORMAL, wxBOLD);
883 wxFont italicFont = wxFont(12, wxROMAN, wxITALIC, wxNORMAL);
884
cc2aecde 885 m_richTextCtrl = new MyRichTextCtrl(splitter, ID_RICHTEXT_CTRL, wxEmptyString, wxDefaultPosition, wxSize(200, 200), wxVSCROLL|wxHSCROLL|wxWANTS_CHARS);
32423dd8
JS
886 wxASSERT(!m_richTextCtrl->GetBuffer().GetAttributes().HasFontPixelSize());
887
ff2baa25
JS
888 wxFont font(12, wxROMAN, wxNORMAL, wxNORMAL);
889
ff2baa25 890 m_richTextCtrl->SetFont(font);
5d7836c4 891
32423dd8
JS
892 wxASSERT(!m_richTextCtrl->GetBuffer().GetAttributes().HasFontPixelSize());
893
603f702b
JS
894 m_richTextCtrl->SetMargins(10, 10);
895
fe5aa22c
JS
896 m_richTextCtrl->SetStyleSheet(wxGetApp().GetStyleSheet());
897
e637208a
JS
898 combo->SetStyleSheet(wxGetApp().GetStyleSheet());
899 combo->SetRichTextCtrl(m_richTextCtrl);
900 combo->UpdateStyles();
901
082cfe55 902 wxRichTextStyleListCtrl* styleListCtrl = new wxRichTextStyleListCtrl(splitter, ID_RICHTEXT_STYLE_LIST);
4753c7ce
WS
903
904 wxSize display = wxGetDisplaySize();
905 if ( is_pda && ( display.GetWidth() < display.GetHeight() ) )
906 {
082cfe55 907 splitter->SplitHorizontally(m_richTextCtrl, styleListCtrl);
4753c7ce
WS
908 }
909 else
910 {
082cfe55 911 splitter->SplitVertically(m_richTextCtrl, styleListCtrl, 500);
4753c7ce 912 }
5d7836c4 913
b4e415df
JS
914 Layout();
915
c59f6793
JS
916 splitter->UpdateSize();
917
082cfe55
JS
918 styleListCtrl->SetStyleSheet(wxGetApp().GetStyleSheet());
919 styleListCtrl->SetRichTextCtrl(m_richTextCtrl);
920 styleListCtrl->UpdateStyles();
5d7836c4 921
99404ab0
JS
922 WriteInitialText();
923}
924
925// Write text
926void MyFrame::WriteInitialText()
927{
cc2aecde 928 MyRichTextCtrl& r = *m_richTextCtrl;
5d7836c4 929
99404ab0
JS
930 r.SetDefaultStyle(wxRichTextAttr());
931
b4e415df 932 r.Freeze();
cc2aecde 933
5d7836c4
JS
934 r.BeginSuppressUndo();
935
936 r.BeginParagraphSpacing(0, 20);
937
938 r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
939 r.BeginBold();
940
941 r.BeginFontSize(14);
c3e54dee 942
50f65288 943 wxString lineBreak = (wxChar) 29;
c3e54dee 944
99404ab0 945 r.WriteText(wxString(wxT("Welcome to wxRichTextCtrl, a wxWidgets control")) + lineBreak + wxT("for editing and presenting styled text and images\n"));
5d7836c4 946 r.EndFontSize();
5d7836c4
JS
947
948 r.BeginItalic();
949 r.WriteText(wxT("by Julian Smart"));
950 r.EndItalic();
951
952 r.EndBold();
5d7836c4 953 r.Newline();
42688aea 954
5d7836c4
JS
955 r.WriteImage(wxBitmap(zebra_xpm));
956
5d7836c4
JS
957 r.Newline();
958 r.Newline();
959
99404ab0
JS
960 r.EndAlignment();
961
32423dd8
JS
962#if 0
963 r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
964 r.WriteText(wxString(wxT("This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side.")));
965 r.Newline();
966 r.EndAlignment();
967#endif
968
cdaed652 969 r.BeginAlignment(wxTEXT_ALIGNMENT_LEFT);
24777478
JS
970 wxRichTextAttr imageAttr;
971 imageAttr.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_LEFT);
cdaed652 972 r.WriteText(wxString(wxT("This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side.")));
24777478 973 r.WriteImage(wxBitmap(zebra_xpm), wxBITMAP_TYPE_PNG, imageAttr);
75936ec6 974
24777478
JS
975 imageAttr.GetTextBoxAttr().GetTop().SetValue(200);
976 imageAttr.GetTextBoxAttr().GetTop().SetUnits(wxTEXT_ATTR_UNITS_PIXELS);
977 imageAttr.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_RIGHT);
978 r.WriteImage(wxBitmap(zebra_xpm), wxBITMAP_TYPE_PNG, imageAttr);
5ad9ae3a 979 r.WriteText(wxString(wxT("This is a simple test for a floating right image test. The zebra image should be placed at the right side of the current buffer and all the text should flow around it at the left side. This is a simple test for a floating left image test. The zebra image should be placed at the right side of the current buffer and all the text should flow around it at the left side. This is a simple test for a floating left image test. The zebra image should be placed at the right side of the current buffer and all the text should flow around it at the left side.")));
cdaed652
VZ
980 r.EndAlignment();
981 r.Newline();
982
5d7836c4 983 r.WriteText(wxT("What can you do with this thing? "));
42688aea 984
5d7836c4
JS
985 r.WriteImage(wxBitmap(smiley_xpm));
986 r.WriteText(wxT(" Well, you can change text "));
987
988 r.BeginTextColour(wxColour(255, 0, 0));
989 r.WriteText(wxT("colour, like this red bit."));
990 r.EndTextColour();
991
3e541562
JS
992 wxRichTextAttr backgroundColourAttr;
993 backgroundColourAttr.SetBackgroundColour(*wxGREEN);
994 backgroundColourAttr.SetTextColour(wxColour(0, 0, 255));
995 r.BeginStyle(backgroundColourAttr);
996 r.WriteText(wxT(" And this blue on green bit."));
997 r.EndStyle();
5d7836c4
JS
998
999 r.WriteText(wxT(" Naturally you can make things "));
1000 r.BeginBold();
1001 r.WriteText(wxT("bold "));
1002 r.EndBold();
1003 r.BeginItalic();
1004 r.WriteText(wxT("or italic "));
1005 r.EndItalic();
1006 r.BeginUnderline();
1007 r.WriteText(wxT("or underlined."));
1008 r.EndUnderline();
1009
1010 r.BeginFontSize(14);
1011 r.WriteText(wxT(" Different font sizes on the same line is allowed, too."));
1012 r.EndFontSize();
1013
1014 r.WriteText(wxT(" Next we'll show an indented paragraph."));
1015
5d7836c4
JS
1016 r.Newline();
1017
99404ab0 1018 r.BeginLeftIndent(60);
5d7836c4 1019 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."));
5d7836c4
JS
1020 r.Newline();
1021
99404ab0
JS
1022 r.EndLeftIndent();
1023
5d7836c4
JS
1024 r.WriteText(wxT("Next, we'll show a first-line indent, achieved using BeginLeftIndent(100, -40)."));
1025
5d7836c4
JS
1026 r.Newline();
1027
99404ab0 1028 r.BeginLeftIndent(100, -40);
5d7836c4 1029
99404ab0 1030 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."));
5d7836c4
JS
1031 r.Newline();
1032
99404ab0 1033 r.EndLeftIndent();
5d7836c4 1034
99404ab0 1035 r.WriteText(wxT("Numbered bullets are possible, again using subindents:"));
5d7836c4
JS
1036 r.Newline();
1037
99404ab0 1038 r.BeginNumberedBullet(1, 100, 60);
082cfe55 1039 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."));
99404ab0 1040 r.Newline();
5d7836c4
JS
1041 r.EndNumberedBullet();
1042
1043 r.BeginNumberedBullet(2, 100, 60);
5d7836c4 1044 r.WriteText(wxT("This is my second item."));
5d7836c4 1045 r.Newline();
99404ab0 1046 r.EndNumberedBullet();
5d7836c4
JS
1047
1048 r.WriteText(wxT("The following paragraph is right-indented:"));
99404ab0 1049 r.Newline();
5d7836c4
JS
1050
1051 r.BeginRightIndent(200);
5d7836c4
JS
1052
1053 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."));
5d7836c4
JS
1054 r.Newline();
1055
99404ab0
JS
1056 r.EndRightIndent();
1057
5d7836c4 1058 r.WriteText(wxT("The following paragraph is right-aligned with 1.5 line spacing:"));
99404ab0 1059 r.Newline();
5d7836c4
JS
1060
1061 r.BeginAlignment(wxTEXT_ALIGNMENT_RIGHT);
1062 r.BeginLineSpacing(wxTEXT_ATTR_LINE_SPACING_HALF);
5d7836c4 1063 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."));
99404ab0 1064 r.Newline();
5d7836c4
JS
1065 r.EndLineSpacing();
1066 r.EndAlignment();
1067
7f0d9d71
JS
1068 wxArrayInt tabs;
1069 tabs.Add(400);
1070 tabs.Add(600);
1071 tabs.Add(800);
1072 tabs.Add(1000);
24777478 1073 wxRichTextAttr attr;
7f0d9d71
JS
1074 attr.SetFlags(wxTEXT_ATTR_TABS);
1075 attr.SetTabs(tabs);
1076 r.SetDefaultStyle(attr);
4753c7ce 1077
7f0d9d71 1078 r.WriteText(wxT("This line contains tabs:\tFirst tab\tSecond tab\tThird tab"));
5d7836c4 1079 r.Newline();
99404ab0 1080
5d7836c4 1081 r.WriteText(wxT("Other notable features of wxRichTextCtrl include:"));
99404ab0 1082 r.Newline();
5d7836c4
JS
1083
1084 r.BeginSymbolBullet(wxT('*'), 100, 60);
5d7836c4 1085 r.WriteText(wxT("Compatibility with wxTextCtrl API"));
99404ab0 1086 r.Newline();
5d7836c4
JS
1087 r.EndSymbolBullet();
1088
1089 r.BeginSymbolBullet(wxT('*'), 100, 60);
5d7836c4 1090 r.WriteText(wxT("Easy stack-based BeginXXX()...EndXXX() style setting in addition to SetStyle()"));
99404ab0 1091 r.Newline();
5d7836c4
JS
1092 r.EndSymbolBullet();
1093
1094 r.BeginSymbolBullet(wxT('*'), 100, 60);
5d7836c4 1095 r.WriteText(wxT("XML loading and saving"));
99404ab0 1096 r.Newline();
5d7836c4
JS
1097 r.EndSymbolBullet();
1098
1099 r.BeginSymbolBullet(wxT('*'), 100, 60);
5d7836c4 1100 r.WriteText(wxT("Undo/Redo, with batching option and Undo suppressing"));
99404ab0 1101 r.Newline();
5d7836c4
JS
1102 r.EndSymbolBullet();
1103
1104 r.BeginSymbolBullet(wxT('*'), 100, 60);
5d7836c4 1105 r.WriteText(wxT("Clipboard copy and paste"));
99404ab0 1106 r.Newline();
5d7836c4
JS
1107 r.EndSymbolBullet();
1108
1109 r.BeginSymbolBullet(wxT('*'), 100, 60);
5d7836c4 1110 r.WriteText(wxT("wxRichTextStyleSheet with named character and paragraph styles, and control for applying named styles"));
99404ab0 1111 r.Newline();
5d7836c4
JS
1112 r.EndSymbolBullet();
1113
1114 r.BeginSymbolBullet(wxT('*'), 100, 60);
5d7836c4 1115 r.WriteText(wxT("A design that can easily be extended to other content types, ultimately with text boxes, tables, controls, and so on"));
5d7836c4 1116 r.Newline();
99404ab0 1117 r.EndSymbolBullet();
5d7836c4 1118
720cc10a
JS
1119 // Make a style suitable for showing a URL
1120 wxRichTextAttr urlStyle;
1121 urlStyle.SetTextColour(*wxBLUE);
1122 urlStyle.SetFontUnderlined(true);
1123
1124 r.WriteText(wxT("wxRichTextCtrl can also display URLs, such as this one: "));
1125 r.BeginStyle(urlStyle);
1126 r.BeginURL(wxT("http://www.wxwidgets.org"));
1127 r.WriteText(wxT("The wxWidgets Web Site"));
1128 r.EndURL();
1129 r.EndStyle();
1130 r.WriteText(wxT(". Click on the URL to generate an event."));
1131
1132 r.Newline();
1133
99404ab0 1134 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!\n"));
5d7836c4
JS
1135
1136 r.EndParagraphSpacing();
603f702b 1137
32423dd8 1138#if 1
603f702b
JS
1139 {
1140 // Add a text box
1141
1142 r.Newline();
75936ec6 1143
603f702b
JS
1144 wxRichTextAttr attr;
1145 attr.GetTextBoxAttr().GetMargins().GetLeft().SetValue(20, wxTEXT_ATTR_UNITS_PIXELS);
1146 attr.GetTextBoxAttr().GetMargins().GetTop().SetValue(20, wxTEXT_ATTR_UNITS_PIXELS);
1147 attr.GetTextBoxAttr().GetMargins().GetRight().SetValue(20, wxTEXT_ATTR_UNITS_PIXELS);
1148 attr.GetTextBoxAttr().GetMargins().GetBottom().SetValue(20, wxTEXT_ATTR_UNITS_PIXELS);
5d7836c4 1149
603f702b
JS
1150 attr.GetTextBoxAttr().GetBorder().SetColour(*wxBLACK);
1151 attr.GetTextBoxAttr().GetBorder().SetWidth(1, wxTEXT_ATTR_UNITS_PIXELS);
1152 attr.GetTextBoxAttr().GetBorder().SetStyle(wxTEXT_BOX_ATTR_BORDER_SOLID);
1153
1154 wxRichTextBox* textBox = r.WriteTextBox(attr);
1155 r.SetFocusObject(textBox);
1156
1157 r.WriteText(wxT("This is a text box. Just testing! Once more unto the breach, dear friends, once more..."));
1158
1159 r.SetFocusObject(NULL); // Set the focus back to the main buffer
1160 r.SetInsertionPointEnd();
1161 }
1162#endif
1163#if 1
1164 {
1165 // Add a table
1166
1167 r.Newline();
75936ec6 1168
603f702b
JS
1169 wxRichTextAttr attr;
1170 attr.GetTextBoxAttr().GetMargins().GetLeft().SetValue(5, wxTEXT_ATTR_UNITS_PIXELS);
1171 attr.GetTextBoxAttr().GetMargins().GetTop().SetValue(5, wxTEXT_ATTR_UNITS_PIXELS);
1172 attr.GetTextBoxAttr().GetMargins().GetRight().SetValue(5, wxTEXT_ATTR_UNITS_PIXELS);
1173 attr.GetTextBoxAttr().GetMargins().GetBottom().SetValue(5, wxTEXT_ATTR_UNITS_PIXELS);
1174 attr.GetTextBoxAttr().GetPadding() = attr.GetTextBoxAttr().GetMargins();
1175
1176 attr.GetTextBoxAttr().GetBorder().SetColour(*wxBLACK);
1177 attr.GetTextBoxAttr().GetBorder().SetWidth(1, wxTEXT_ATTR_UNITS_PIXELS);
1178 attr.GetTextBoxAttr().GetBorder().SetStyle(wxTEXT_BOX_ATTR_BORDER_SOLID);
1179
1180 wxRichTextAttr cellAttr = attr;
1181 cellAttr.GetTextBoxAttr().GetWidth().SetValue(200, wxTEXT_ATTR_UNITS_PIXELS);
1182 cellAttr.GetTextBoxAttr().GetHeight().SetValue(150, wxTEXT_ATTR_UNITS_PIXELS);
1183
1184 wxRichTextTable* table = r.WriteTable(3, 2, attr, cellAttr);
1185 int i, j;
1186 for (j = 0; j < table->GetRowCount(); j++)
1187 {
1188 for (i = 0; i < table->GetColumnCount(); i++)
1189 {
1190 wxString msg = wxString::Format(wxT("This is cell %d, %d"), (j+1), (i+1));
1191 r.SetFocusObject(table->GetCell(j, i));
1192 r.WriteText(msg);
1193 }
1194 }
1195 r.SetFocusObject(NULL); // Set the focus back to the main buffer
1196 r.SetInsertionPointEnd();
1197 }
1198#endif
75936ec6 1199
32423dd8
JS
1200 r.Newline();
1201
1202 wxRichTextProperties properties;
1203 r.WriteText(wxT("This is a rectangle field: "));
1204 r.WriteField(wxT("rectangle"), properties);
1205 r.WriteText(wxT(" and a begin section field: "));
1206 r.WriteField(wxT("begin-section"), properties);
1207 r.WriteText(wxT("This is text between the two tags."));
1208 r.WriteField(wxT("end-section"), properties);
1209 r.WriteText(wxT(" Now a bitmap. "));
1210 r.WriteField(wxT("bitmap"), properties);
1211 r.WriteText(wxT(" Before we go, here's a composite field: ***"));
1212 wxRichTextField* field = r.WriteField(wxT("composite"), properties);
1213 field->UpdateField(& r.GetBuffer()); // Creates the composite value (sort of a text box)
1214 r.WriteText(wxT("*** End of composite field."));
1215
1216 r.Newline();
5d7836c4 1217 r.EndSuppressUndo();
b4e415df
JS
1218
1219 // Add some locked content first - needs Undo to be enabled
1220 {
1221 r.BeginLock();
1222 r.WriteText(wxString(wxT("This is a locked object.")));
1223 r.EndLock();
1224
1225 r.WriteText(wxString(wxT(" This is unlocked text. ")));
1226
1227 r.BeginLock();
1228 r.WriteText(wxString(wxT("More locked content.")));
1229 r.EndLock();
1230 r.Newline();
1231
1232 // Flush the Undo buffer
1233 r.GetCommandProcessor()->ClearCommands();
1234 }
1235
1236 r.Thaw();
5d7836c4
JS
1237}
1238
5d7836c4
JS
1239// event handlers
1240
1241void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
1242{
1243 // true is to force the frame to close
1244 Close(true);
1245}
1246
1247void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
1248{
1249 wxString msg;
9a83f860
VZ
1250 msg.Printf( wxT("This is a demo for wxRichTextCtrl, a control for editing styled text.\n(c) Julian Smart, 2005"));
1251 wxMessageBox(msg, wxT("About wxRichTextCtrl Sample"), wxOK | wxICON_INFORMATION, this);
5d7836c4
JS
1252}
1253
1254// Forward command events to the current rich text control, if any
1255bool MyFrame::ProcessEvent(wxEvent& event)
1256{
1257 if (event.IsCommandEvent() && !event.IsKindOf(CLASSINFO(wxChildFocusEvent)))
1258 {
1259 // Problem: we can get infinite recursion because the events
1260 // climb back up to this frame, and repeat.
1261 // Assume that command events don't cause another command event
1262 // to be called, so we can rely on inCommand not being overwritten
1263
1264 static int s_eventType = 0;
1265 static wxWindowID s_id = 0;
1266
1267 if (s_id != event.GetId() && s_eventType != event.GetEventType())
1268 {
1269 s_eventType = event.GetEventType();
1270 s_id = event.GetId();
9a173d48 1271
5d7836c4 1272 wxWindow* focusWin = wxFindFocusDescendant(this);
004867db 1273 if (focusWin && focusWin->GetEventHandler()->ProcessEvent(event))
5d7836c4
JS
1274 {
1275 //s_command = NULL;
1276 s_eventType = 0;
1277 s_id = 0;
9a173d48 1278 return true;
5d7836c4
JS
1279 }
1280
1281 s_eventType = 0;
1282 s_id = 0;
1283 }
1284 else
1285 {
9a173d48 1286 return false;
5d7836c4
JS
1287 }
1288 }
1289
1290 return wxFrame::ProcessEvent(event);
1291}
1292
011b3dcb 1293void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
5d7836c4 1294{
9a173d48
WS
1295 wxString path;
1296 wxString filename;
1e967276
JS
1297 wxArrayInt fileTypes;
1298
1299 wxString filter = wxRichTextBuffer::GetExtWildcard(false, false, & fileTypes);
9a173d48 1300 if (!filter.empty())
5d7836c4
JS
1301 filter += wxT("|");
1302 filter += wxT("All files (*.*)|*.*");
1303
5d7836c4
JS
1304 wxFileDialog dialog(this,
1305 _("Choose a filename"),
1306 path,
1307 filename,
1308 filter,
ff3e84ff 1309 wxFD_OPEN);
5d7836c4
JS
1310
1311 if (dialog.ShowModal() == wxID_OK)
1312 {
1313 wxString path = dialog.GetPath();
9a173d48
WS
1314
1315 if (!path.empty())
5d7836c4 1316 {
1e967276 1317 int filterIndex = dialog.GetFilterIndex();
dbf38e88
WS
1318 int fileType = (filterIndex < (int) fileTypes.GetCount())
1319 ? fileTypes[filterIndex]
1320 : wxRICHTEXT_TYPE_TEXT;
1e967276 1321 m_richTextCtrl->LoadFile(path, fileType);
5d7836c4
JS
1322 }
1323 }
1324}
1325
1326void MyFrame::OnSave(wxCommandEvent& event)
1327{
9a173d48 1328 if (m_richTextCtrl->GetFilename().empty())
5d7836c4
JS
1329 {
1330 OnSaveAs(event);
1331 return;
1332 }
1333 m_richTextCtrl->SaveFile();
1334}
1335
011b3dcb 1336void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
5d7836c4
JS
1337{
1338 wxString filter = wxRichTextBuffer::GetExtWildcard(false, true);
9a173d48
WS
1339 wxString path;
1340 wxString filename;
5d7836c4
JS
1341
1342 wxFileDialog dialog(this,
1343 _("Choose a filename"),
1344 path,
1345 filename,
1346 filter,
ff3e84ff 1347 wxFD_SAVE);
5d7836c4
JS
1348
1349 if (dialog.ShowModal() == wxID_OK)
1350 {
1351 wxString path = dialog.GetPath();
9a173d48
WS
1352
1353 if (!path.empty())
5d7836c4 1354 {
bec80f4f
JS
1355 wxBusyCursor busy;
1356 wxStopWatch stopwatch;
1357
5d7836c4 1358 m_richTextCtrl->SaveFile(path);
bec80f4f
JS
1359
1360 long t = stopwatch.Time();
1361 wxLogDebug(wxT("Saving took %ldms"), t);
1362 wxMessageBox(wxString::Format(wxT("Saving took %ldms"), t));
5d7836c4
JS
1363 }
1364 }
1365}
1366
011b3dcb 1367void MyFrame::OnBold(wxCommandEvent& WXUNUSED(event))
5d7836c4
JS
1368{
1369 m_richTextCtrl->ApplyBoldToSelection();
1370}
1371
011b3dcb 1372void MyFrame::OnItalic(wxCommandEvent& WXUNUSED(event))
5d7836c4
JS
1373{
1374 m_richTextCtrl->ApplyItalicToSelection();
1375}
1376
011b3dcb 1377void MyFrame::OnUnderline(wxCommandEvent& WXUNUSED(event))
5d7836c4
JS
1378{
1379 m_richTextCtrl->ApplyUnderlineToSelection();
1380}
1381
75936ec6
JS
1382void MyFrame::OnStrikethrough(wxCommandEvent& WXUNUSED(event))
1383{
1384 m_richTextCtrl->ApplyTextEffectToSelection(wxTEXT_ATTR_EFFECT_STRIKETHROUGH);
1385}
1386
1387void MyFrame::OnSuperscript(wxCommandEvent& WXUNUSED(event))
1388{
1389 m_richTextCtrl->ApplyTextEffectToSelection(wxTEXT_ATTR_EFFECT_SUPERSCRIPT);
1390}
1391
1392void MyFrame::OnSubscript(wxCommandEvent& WXUNUSED(event))
1393{
1394 m_richTextCtrl->ApplyTextEffectToSelection(wxTEXT_ATTR_EFFECT_SUBSCRIPT);
1395}
1396
5d7836c4
JS
1397
1398void MyFrame::OnUpdateBold(wxUpdateUIEvent& event)
1399{
1400 event.Check(m_richTextCtrl->IsSelectionBold());
1401}
1402
1403void MyFrame::OnUpdateItalic(wxUpdateUIEvent& event)
1404{
1405 event.Check(m_richTextCtrl->IsSelectionItalics());
1406}
1407
1408void MyFrame::OnUpdateUnderline(wxUpdateUIEvent& event)
1409{
1410 event.Check(m_richTextCtrl->IsSelectionUnderlined());
1411}
1412
75936ec6
JS
1413void MyFrame::OnUpdateStrikethrough(wxUpdateUIEvent& event)
1414{
1415 event.Check(m_richTextCtrl->DoesSelectionHaveTextEffectFlag(wxTEXT_ATTR_EFFECT_STRIKETHROUGH));
1416}
1417
1418void MyFrame::OnUpdateSuperscript(wxUpdateUIEvent& event)
1419{
1420 event.Check(m_richTextCtrl->DoesSelectionHaveTextEffectFlag(wxTEXT_ATTR_EFFECT_SUPERSCRIPT));
1421}
1422
1423void MyFrame::OnUpdateSubscript(wxUpdateUIEvent& event)
1424{
1425 event.Check(m_richTextCtrl->DoesSelectionHaveTextEffectFlag(wxTEXT_ATTR_EFFECT_SUBSCRIPT));
1426}
1427
011b3dcb 1428void MyFrame::OnAlignLeft(wxCommandEvent& WXUNUSED(event))
5d7836c4
JS
1429{
1430 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT);
1431}
1432
011b3dcb 1433void MyFrame::OnAlignCentre(wxCommandEvent& WXUNUSED(event))
5d7836c4
JS
1434{
1435 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_CENTRE);
1436}
1437
011b3dcb 1438void MyFrame::OnAlignRight(wxCommandEvent& WXUNUSED(event))
5d7836c4
JS
1439{
1440 m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_RIGHT);
1441}
1442
1443void MyFrame::OnUpdateAlignLeft(wxUpdateUIEvent& event)
1444{
1445 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_LEFT));
1446}
1447
1448void MyFrame::OnUpdateAlignCentre(wxUpdateUIEvent& event)
1449{
1450 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_CENTRE));
1451}
1452
1453void MyFrame::OnUpdateAlignRight(wxUpdateUIEvent& event)
1454{
1455 event.Check(m_richTextCtrl->IsSelectionAligned(wxTEXT_ALIGNMENT_RIGHT));
1456}
1457
011b3dcb 1458void MyFrame::OnFont(wxCommandEvent& WXUNUSED(event))
5d7836c4 1459{
8c9c49a1
JS
1460 wxRichTextRange range;
1461 if (m_richTextCtrl->HasSelection())
1462 range = m_richTextCtrl->GetSelectionRange();
1463 else
1464 range = wxRichTextRange(0, m_richTextCtrl->GetLastPosition()+1);
1465
1466 int pages = wxRICHTEXT_FORMAT_FONT;
1467
1468 wxRichTextFormattingDialog formatDlg(pages, this);
32423dd8 1469 formatDlg.SetOptions(wxRichTextFormattingDialog::Option_AllowPixelFontSize);
8c9c49a1
JS
1470 formatDlg.GetStyle(m_richTextCtrl, range);
1471
1472 if (formatDlg.ShowModal() == wxID_OK)
1473 {
42688aea 1474 formatDlg.ApplyStyle(m_richTextCtrl, range, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
8c9c49a1 1475 }
8c9c49a1
JS
1476}
1477
cdaed652
VZ
1478void MyFrame::OnImage(wxCommandEvent& WXUNUSED(event))
1479{
1480 wxRichTextRange range;
1481 wxASSERT(m_richTextCtrl->HasSelection());
1482
1483 range = m_richTextCtrl->GetSelectionRange();
1484 wxASSERT(range.ToInternal().GetLength() == 1);
1485
603f702b 1486 wxRichTextImage* image = wxDynamicCast(m_richTextCtrl->GetFocusObject()->GetLeafObjectAtPosition(range.GetStart()), wxRichTextImage);
cdaed652
VZ
1487 if (image)
1488 {
603f702b 1489 wxRichTextObjectPropertiesDialog imageDlg(image, this);
cdaed652
VZ
1490
1491 if (imageDlg.ShowModal() == wxID_OK)
1492 {
603f702b 1493 imageDlg.ApplyStyle(m_richTextCtrl);
cdaed652
VZ
1494 }
1495 }
1496}
1497
8c9c49a1
JS
1498void MyFrame::OnParagraph(wxCommandEvent& WXUNUSED(event))
1499{
1500 wxRichTextRange range;
1501 if (m_richTextCtrl->HasSelection())
1502 range = m_richTextCtrl->GetSelectionRange();
1503 else
1504 range = wxRichTextRange(0, m_richTextCtrl->GetLastPosition()+1);
1505
1506 int pages = wxRICHTEXT_FORMAT_INDENTS_SPACING|wxRICHTEXT_FORMAT_TABS|wxRICHTEXT_FORMAT_BULLETS;
1507
1508 wxRichTextFormattingDialog formatDlg(pages, this);
1509 formatDlg.GetStyle(m_richTextCtrl, range);
1510
1511 if (formatDlg.ShowModal() == wxID_OK)
1512 {
1513 formatDlg.ApplyStyle(m_richTextCtrl, range);
1514 }
1515}
1516
1517void MyFrame::OnFormat(wxCommandEvent& WXUNUSED(event))
1518{
1519 wxRichTextRange range;
1520 if (m_richTextCtrl->HasSelection())
1521 range = m_richTextCtrl->GetSelectionRange();
1522 else
1523 range = wxRichTextRange(0, m_richTextCtrl->GetLastPosition()+1);
1524
1525 int pages = wxRICHTEXT_FORMAT_FONT|wxRICHTEXT_FORMAT_INDENTS_SPACING|wxRICHTEXT_FORMAT_TABS|wxRICHTEXT_FORMAT_BULLETS;
1526
1527 wxRichTextFormattingDialog formatDlg(pages, this);
1528 formatDlg.GetStyle(m_richTextCtrl, range);
1529
1530 if (formatDlg.ShowModal() == wxID_OK)
1531 {
1532 formatDlg.ApplyStyle(m_richTextCtrl, range);
1533 }
1534}
1535
1536void MyFrame::OnUpdateFormat(wxUpdateUIEvent& event)
1537{
1538 event.Enable(m_richTextCtrl->HasSelection());
5d7836c4
JS
1539}
1540
cdaed652
VZ
1541void MyFrame::OnUpdateImage(wxUpdateUIEvent& event)
1542{
1543 wxRichTextRange range;
1544 wxRichTextObject *obj;
1545
1546 range = m_richTextCtrl->GetSelectionRange();
1547 if (range.ToInternal().GetLength() == 1)
1548 {
603f702b 1549 obj = m_richTextCtrl->GetFocusObject()->GetLeafObjectAtPosition(range.GetStart());
cdaed652
VZ
1550 if (obj && obj->IsKindOf(CLASSINFO(wxRichTextImage)))
1551 {
1552 event.Enable(true);
1553 return;
1554 }
1555 }
1556
1557 event.Enable(false);
1558}
1559
011b3dcb 1560void MyFrame::OnIndentMore(wxCommandEvent& WXUNUSED(event))
5d7836c4 1561{
44cc96a8 1562 wxRichTextAttr attr;
5d7836c4
JS
1563 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
1564
1565 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1566 {
1567 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1568 if (m_richTextCtrl->HasSelection())
1569 range = m_richTextCtrl->GetSelectionRange();
1570
5d7836c4
JS
1571 attr.SetLeftIndent(attr.GetLeftIndent() + 100);
1572
1573 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
1574 m_richTextCtrl->SetStyle(range, attr);
1575 }
1576}
1577
011b3dcb 1578void MyFrame::OnIndentLess(wxCommandEvent& WXUNUSED(event))
5d7836c4 1579{
44cc96a8 1580 wxRichTextAttr attr;
5d7836c4
JS
1581 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
1582
1583 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1584 {
1585 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1586 if (m_richTextCtrl->HasSelection())
1587 range = m_richTextCtrl->GetSelectionRange();
1588
c3e54dee 1589 if (attr.GetLeftIndent() > 0)
5d7836c4 1590 {
c3e54dee 1591 attr.SetLeftIndent(wxMax(0, attr.GetLeftIndent() - 100));
9a173d48 1592
5d7836c4
JS
1593 m_richTextCtrl->SetStyle(range, attr);
1594 }
1595 }
1596}
1597
011b3dcb 1598void MyFrame::OnLineSpacingHalf(wxCommandEvent& WXUNUSED(event))
5d7836c4 1599{
44cc96a8 1600 wxRichTextAttr attr;
5d7836c4
JS
1601 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1602
1603 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1604 {
1605 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1606 if (m_richTextCtrl->HasSelection())
1607 range = m_richTextCtrl->GetSelectionRange();
1608
5d7836c4
JS
1609 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1610 attr.SetLineSpacing(15);
9a173d48 1611
5d7836c4
JS
1612 m_richTextCtrl->SetStyle(range, attr);
1613 }
1614}
1615
011b3dcb 1616void MyFrame::OnLineSpacingDouble(wxCommandEvent& WXUNUSED(event))
5d7836c4 1617{
44cc96a8 1618 wxRichTextAttr attr;
5d7836c4
JS
1619 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1620
1621 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1622 {
1623 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1624 if (m_richTextCtrl->HasSelection())
1625 range = m_richTextCtrl->GetSelectionRange();
1626
5d7836c4
JS
1627 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1628 attr.SetLineSpacing(20);
9a173d48 1629
5d7836c4
JS
1630 m_richTextCtrl->SetStyle(range, attr);
1631 }
1632}
1633
011b3dcb 1634void MyFrame::OnLineSpacingSingle(wxCommandEvent& WXUNUSED(event))
5d7836c4 1635{
44cc96a8 1636 wxRichTextAttr attr;
5d7836c4
JS
1637 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1638
1639 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1640 {
1641 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1642 if (m_richTextCtrl->HasSelection())
1643 range = m_richTextCtrl->GetSelectionRange();
1644
5d7836c4
JS
1645 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
1646 attr.SetLineSpacing(0); // Can also use 10
9a173d48 1647
5d7836c4
JS
1648 m_richTextCtrl->SetStyle(range, attr);
1649 }
1650}
1651
011b3dcb 1652void MyFrame::OnParagraphSpacingMore(wxCommandEvent& WXUNUSED(event))
5d7836c4 1653{
44cc96a8 1654 wxRichTextAttr attr;
5d7836c4
JS
1655 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1656
1657 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1658 {
1659 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1660 if (m_richTextCtrl->HasSelection())
1661 range = m_richTextCtrl->GetSelectionRange();
1662
5d7836c4
JS
1663 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() + 20);
1664
1665 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1666 m_richTextCtrl->SetStyle(range, attr);
1667 }
1668}
1669
011b3dcb 1670void MyFrame::OnParagraphSpacingLess(wxCommandEvent& WXUNUSED(event))
5d7836c4 1671{
44cc96a8 1672 wxRichTextAttr attr;
5d7836c4
JS
1673 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1674
1675 if (m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr))
1676 {
1677 wxRichTextRange range(m_richTextCtrl->GetInsertionPoint(), m_richTextCtrl->GetInsertionPoint());
1678 if (m_richTextCtrl->HasSelection())
1679 range = m_richTextCtrl->GetSelectionRange();
1680
1681 if (attr.GetParagraphSpacingAfter() >= 20)
1682 {
5d7836c4 1683 attr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter() - 20);
9a173d48 1684
5d7836c4
JS
1685 attr.SetFlags(wxTEXT_ATTR_PARA_SPACING_AFTER);
1686 m_richTextCtrl->SetStyle(range, attr);
1687 }
1688 }
1689}
1690
99404ab0
JS
1691void MyFrame::OnReload(wxCommandEvent& WXUNUSED(event))
1692{
1693 m_richTextCtrl->Clear();
1694 WriteInitialText();
1695}
1696
011b3dcb 1697void MyFrame::OnViewHTML(wxCommandEvent& WXUNUSED(event))
5d7836c4
JS
1698{
1699 wxDialog dialog(this, wxID_ANY, _("HTML"), wxDefaultPosition, wxSize(500, 400), wxDEFAULT_DIALOG_STYLE);
1700
1701 wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
1702 dialog.SetSizer(boxSizer);
1703
1704 wxHtmlWindow* win = new wxHtmlWindow(& dialog, wxID_ANY, wxDefaultPosition, wxSize(500, 400), wxSUNKEN_BORDER);
1705 boxSizer->Add(win, 1, wxALL, 5);
1706
1707 wxButton* cancelButton = new wxButton(& dialog, wxID_CANCEL, wxT("&Close"));
1708 boxSizer->Add(cancelButton, 0, wxALL|wxCENTRE, 5);
1709
1710 wxString text;
1711 wxStringOutputStream strStream(& text);
1712
1713 wxRichTextHTMLHandler htmlHandler;
720cc10a 1714 htmlHandler.SetFlags(wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY);
c3e54dee 1715
50f65288
WS
1716 wxArrayInt fontSizeMapping;
1717 fontSizeMapping.Add(7);
1718 fontSizeMapping.Add(9);
1719 fontSizeMapping.Add(11);
1720 fontSizeMapping.Add(12);
1721 fontSizeMapping.Add(14);
1722 fontSizeMapping.Add(22);
1723 fontSizeMapping.Add(100);
c3e54dee 1724
50f65288 1725 htmlHandler.SetFontSizeMapping(fontSizeMapping);
720cc10a 1726
5d7836c4
JS
1727 if (htmlHandler.SaveFile(& m_richTextCtrl->GetBuffer(), strStream))
1728 {
1729 win->SetPage(text);
1730 }
1731
1732 boxSizer->Fit(& dialog);
1733
1734 dialog.ShowModal();
720cc10a
JS
1735
1736 // Now delete the temporary in-memory images
1737 htmlHandler.DeleteTemporaryImages();
5d7836c4 1738}
fe5aa22c
JS
1739
1740// Demonstrates how you can change the style sheets and have the changes
1741// reflected in the control content without wiping out character formatting.
1742
1743void MyFrame::OnSwitchStyleSheets(wxCommandEvent& WXUNUSED(event))
1744{
1745 static wxRichTextStyleSheet* gs_AlternateStyleSheet = NULL;
1746
e4723634 1747 wxRichTextStyleListCtrl *styleList = (wxRichTextStyleListCtrl*) FindWindow(ID_RICHTEXT_STYLE_LIST);
fe5aa22c
JS
1748 wxRichTextStyleComboCtrl* styleCombo = (wxRichTextStyleComboCtrl*) FindWindow(ID_RICHTEXT_STYLE_COMBO);
1749
8881d9f0 1750 wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
fe5aa22c
JS
1751
1752 // One-time creation of an alternate style sheet
1753 if (!gs_AlternateStyleSheet)
1754 {
1755 gs_AlternateStyleSheet = new wxRichTextStyleSheet(*sheet);
1756
1757 // Make some modifications
1758 for (int i = 0; i < (int) gs_AlternateStyleSheet->GetParagraphStyleCount(); i++)
1759 {
1760 wxRichTextParagraphStyleDefinition* def = gs_AlternateStyleSheet->GetParagraphStyle(i);
1761
1762 if (def->GetStyle().HasTextColour())
1763 def->GetStyle().SetTextColour(*wxBLUE);
1764
1765 if (def->GetStyle().HasAlignment())
1766 {
1767 if (def->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
1768 def->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
1769 else if (def->GetStyle().GetAlignment() == wxTEXT_ALIGNMENT_LEFT)
1770 def->GetStyle().SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
1771 }
1772 if (def->GetStyle().HasLeftIndent())
1773 {
1774 def->GetStyle().SetLeftIndent(def->GetStyle().GetLeftIndent() * 2);
1775 }
1776 }
1777 }
1778
1779 // Switch sheets
1780 wxRichTextStyleSheet* tmp = gs_AlternateStyleSheet;
1781 gs_AlternateStyleSheet = sheet;
1782 sheet = tmp;
1783
8881d9f0
JS
1784 m_richTextCtrl->SetStyleSheet(sheet);
1785 m_richTextCtrl->ApplyStyleSheet(sheet); // Makes the control reflect the new style definitions
fe5aa22c
JS
1786
1787 styleList->SetStyleSheet(sheet);
1788 styleList->UpdateStyles();
1789
1790 styleCombo->SetStyleSheet(sheet);
1791 styleCombo->UpdateStyles();
1792}
8c9c49a1 1793
e4723634
JS
1794void MyFrame::OnManageStyles(wxCommandEvent& WXUNUSED(event))
1795{
8881d9f0 1796 wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
e4723634
JS
1797
1798 int flags = wxRICHTEXT_ORGANISER_CREATE_STYLES|wxRICHTEXT_ORGANISER_EDIT_STYLES;
1799
1800 wxRichTextStyleOrganiserDialog dlg(flags, sheet, NULL, this, wxID_ANY, _("Style Manager"));
1801 dlg.ShowModal();
1802}
1803
8c9c49a1
JS
1804void MyFrame::OnInsertSymbol(wxCommandEvent& WXUNUSED(event))
1805{
44cc96a8 1806 wxRichTextAttr attr;
8c9c49a1 1807 attr.SetFlags(wxTEXT_ATTR_FONT);
8881d9f0 1808 m_richTextCtrl->GetStyle(m_richTextCtrl->GetInsertionPoint(), attr);
8c9c49a1
JS
1809
1810 wxString currentFontName;
a1b806b9 1811 if (attr.HasFont() && attr.GetFont().IsOk())
8c9c49a1
JS
1812 currentFontName = attr.GetFont().GetFaceName();
1813
1814 // Don't set the initial font in the dialog (so the user is choosing
1815 // 'normal text', i.e. the current font) but do tell the dialog
1816 // what 'normal text' is.
1817
1818 wxSymbolPickerDialog dlg(wxT("*"), wxEmptyString, currentFontName, this);
1819
1820 if (dlg.ShowModal() == wxID_OK)
1821 {
1822 if (dlg.HasSelection())
1823 {
8881d9f0 1824 long insertionPoint = m_richTextCtrl->GetInsertionPoint();
8c9c49a1 1825
8881d9f0 1826 m_richTextCtrl->WriteText(dlg.GetSymbol());
8c9c49a1
JS
1827
1828 if (!dlg.UseNormalFont())
1829 {
1830 wxFont font(attr.GetFont());
23c386ba 1831 font.SetFaceName(dlg.GetFontName());
8c9c49a1 1832 attr.SetFont(font);
8881d9f0 1833 m_richTextCtrl->SetStyle(insertionPoint, insertionPoint+1, attr);
8c9c49a1
JS
1834 }
1835 }
1836 }
1837}
082cfe55
JS
1838
1839void MyFrame::OnNumberList(wxCommandEvent& WXUNUSED(event))
1840{
8881d9f0 1841 if (m_richTextCtrl->HasSelection())
082cfe55 1842 {
8881d9f0
JS
1843 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1844 m_richTextCtrl->SetListStyle(range, wxT("Numbered List 1"), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RENUMBER);
082cfe55
JS
1845 }
1846}
1847
e4723634
JS
1848void MyFrame::OnBulletsAndNumbering(wxCommandEvent& WXUNUSED(event))
1849{
8881d9f0 1850 wxRichTextStyleSheet* sheet = m_richTextCtrl->GetStyleSheet();
e4723634
JS
1851
1852 int flags = wxRICHTEXT_ORGANISER_BROWSE_NUMBERING;
1853
8881d9f0 1854 wxRichTextStyleOrganiserDialog dlg(flags, sheet, m_richTextCtrl, this, wxID_ANY, _("Bullets and Numbering"));
e4723634
JS
1855 if (dlg.ShowModal() == wxID_OK)
1856 {
1857 if (dlg.GetSelectedStyleDefinition())
1858 dlg.ApplyStyle();
1859 }
1860}
1861
082cfe55
JS
1862void MyFrame::OnItemizeList(wxCommandEvent& WXUNUSED(event))
1863{
8881d9f0 1864 if (m_richTextCtrl->HasSelection())
082cfe55 1865 {
8881d9f0
JS
1866 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1867 m_richTextCtrl->SetListStyle(range, wxT("Bullet List 1"));
082cfe55
JS
1868 }
1869}
1870
1871void MyFrame::OnRenumberList(wxCommandEvent& WXUNUSED(event))
1872{
8881d9f0 1873 if (m_richTextCtrl->HasSelection())
082cfe55 1874 {
8881d9f0
JS
1875 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1876 m_richTextCtrl->NumberList(range, NULL, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RENUMBER);
082cfe55
JS
1877 }
1878}
1879
1880void MyFrame::OnPromoteList(wxCommandEvent& WXUNUSED(event))
1881{
8881d9f0 1882 if (m_richTextCtrl->HasSelection())
082cfe55 1883 {
8881d9f0
JS
1884 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1885 m_richTextCtrl->PromoteList(1, range, NULL);
082cfe55
JS
1886 }
1887}
1888
1889void MyFrame::OnDemoteList(wxCommandEvent& WXUNUSED(event))
1890{
8881d9f0 1891 if (m_richTextCtrl->HasSelection())
082cfe55 1892 {
8881d9f0
JS
1893 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1894 m_richTextCtrl->PromoteList(-1, range, NULL);
082cfe55
JS
1895 }
1896}
1897
1898void MyFrame::OnClearList(wxCommandEvent& WXUNUSED(event))
1899{
8881d9f0 1900 if (m_richTextCtrl->HasSelection())
082cfe55 1901 {
8881d9f0
JS
1902 wxRichTextRange range = m_richTextCtrl->GetSelectionRange();
1903 m_richTextCtrl->ClearListStyle(range);
082cfe55
JS
1904 }
1905}
1906
720cc10a
JS
1907void MyFrame::OnInsertURL(wxCommandEvent& WXUNUSED(event))
1908{
1909 wxString url = wxGetTextFromUser(_("URL:"), _("Insert URL"));
1910 if (!url.IsEmpty())
1911 {
720cc10a
JS
1912 // Make a style suitable for showing a URL
1913 wxRichTextAttr urlStyle;
1914 urlStyle.SetTextColour(*wxBLUE);
1915 urlStyle.SetFontUnderlined(true);
c3e54dee 1916
8881d9f0
JS
1917 m_richTextCtrl->BeginStyle(urlStyle);
1918 m_richTextCtrl->BeginURL(url);
1919 m_richTextCtrl->WriteText(url);
1920 m_richTextCtrl->EndURL();
1921 m_richTextCtrl->EndStyle();
720cc10a
JS
1922 }
1923}
1924
cdaed652
VZ
1925void MyFrame::OnInsertImage(wxCommandEvent& WXUNUSED(event))
1926{
1927 wxFileDialog dialog(this, _("Choose an image"), "", "", "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png");
1928 if (dialog.ShowModal() == wxID_OK)
1929 {
1930 wxString path = dialog.GetPath();
1931 m_richTextCtrl->WriteImage(path, wxBITMAP_TYPE_ANY);
1932 }
1933}
1934
720cc10a
JS
1935void MyFrame::OnURL(wxTextUrlEvent& event)
1936{
1937 wxMessageBox(event.GetString());
1938}
1939
1940// Veto style sheet replace events when loading from XML, since we want
1941// to keep the original style sheet.
1942void MyFrame::OnStyleSheetReplacing(wxRichTextEvent& event)
1943{
1944 event.Veto();
1945}
8881d9f0
JS
1946
1947void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
1948{
1949 wxGetApp().GetPrinting()->PrintBuffer(m_richTextCtrl->GetBuffer());
1950}
1951
1952void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event))
1953{
1954 wxGetApp().GetPrinting()->PreviewBuffer(m_richTextCtrl->GetBuffer());
1955}
1956
1957void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
1958{
24777478
JS
1959 wxDialog dialog(this, wxID_ANY, wxT("Testing"), wxPoint(10, 10), wxSize(400, 300), wxDEFAULT_DIALOG_STYLE);
1960
1961 wxNotebook* nb = new wxNotebook(& dialog, wxID_ANY, wxPoint(5, 5), wxSize(300, 250));
1962 wxPanel* panel = new wxPanel(nb, wxID_ANY, wxDefaultPosition, wxDefaultSize);
1963 wxPanel* panel2 = new wxPanel(nb, wxID_ANY, wxDefaultPosition, wxDefaultSize);
1964
d275b15d 1965 new wxRichTextCtrl(panel, wxID_ANY, wxEmptyString, wxPoint(5, 5), wxSize(200, 150), wxVSCROLL|wxTE_READONLY);
24777478
JS
1966 nb->AddPage(panel, wxT("Page 1"));
1967
d275b15d 1968 new wxRichTextCtrl(panel2, wxID_ANY, wxEmptyString, wxPoint(5, 5), wxSize(200, 150), wxVSCROLL|wxTE_READONLY);
24777478
JS
1969 nb->AddPage(panel2, wxT("Page 2"));
1970
d275b15d 1971 new wxButton(& dialog, wxID_OK, wxT("OK"), wxPoint(5, 180));
24777478
JS
1972
1973 dialog.ShowModal();
1974
1975// wxGetApp().GetPrinting()->PageSetup();
8881d9f0 1976}
cc2aecde 1977
32423dd8
JS
1978void MyFrame::OnSetFontScale(wxCommandEvent& WXUNUSED(event))
1979{
1980 wxString value = wxString::Format(wxT("%g"), m_richTextCtrl->GetFontScale());
1981 wxString text = wxGetTextFromUser(wxT("Enter a text scale factor:"), wxT("Text Scale Factor"), value, wxGetTopLevelParent(this));
1982 if (!text.IsEmpty() && value != text)
1983 {
1984 double scale = 1.0;
1985 wxSscanf(text, wxT("%lf"), & scale);
1986 m_richTextCtrl->SetFontScale(scale, true);
1987 }
1988}
1989
1990void MyFrame::OnSetDimensionScale(wxCommandEvent& WXUNUSED(event))
1991{
1992 wxString value = wxString::Format(wxT("%g"), m_richTextCtrl->GetDimensionScale());
1993 wxString text = wxGetTextFromUser(wxT("Enter a dimension scale factor:"), wxT("Dimension Scale Factor"), value, wxGetTopLevelParent(this));
1994 if (!text.IsEmpty() && value != text)
1995 {
1996 double scale = 1.0;
1997 wxSscanf(text, wxT("%lf"), & scale);
1998 m_richTextCtrl->SetDimensionScale(scale, true);
1999 }
2000}
2001
cc2aecde
JS
2002void MyRichTextCtrl::PrepareContent(wxRichTextParagraphLayoutBox& container)
2003{
2004 if (IsLocked())
2005 {
2006 // Lock all content that's about to be added to the control
2007 wxRichTextObjectList::compatibility_iterator node = container.GetChildren().GetFirst();
2008 while (node)
2009 {
2010 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
2011 if (para)
2012 {
2013 wxRichTextObjectList::compatibility_iterator childNode = para->GetChildren().GetFirst();
2014 while (childNode)
2015 {
2016 wxRichTextObject* obj = childNode->GetData();
2017 obj->GetProperties().SetProperty(wxT("Lock"), m_lockId);
2018
2019 childNode = childNode->GetNext();
2020 }
2021 }
2022 node = node->GetNext();
2023 }
2024 }
2025}
2026
2027bool MyRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox& container, const wxRichTextRange& range) const
2028{
2029 long i;
2030 for (i = range.GetStart(); i < range.GetEnd(); i++)
2031 {
2032 wxRichTextObject* obj = container.GetLeafObjectAtPosition(i);
2033 if (obj && obj->GetProperties().HasProperty(wxT("Lock")))
2034 {
2035 return false;
2036 }
2037 }
2038 return true;
2039}
2040
2041bool MyRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox& container, long pos) const
2042{
2043 wxRichTextObject* child1 = container.GetLeafObjectAtPosition(pos);
2044 wxRichTextObject* child2 = container.GetLeafObjectAtPosition(pos-1);
2045
2046 long lock1 = -1, lock2 = -1;
2047
2048 if (child1 && child1->GetProperties().HasProperty(wxT("Lock")))
2049 lock1 = child1->GetProperties().GetPropertyLong(wxT("Lock"));
2050 if (child2 && child2->GetProperties().HasProperty(wxT("Lock")))
2051 lock2 = child2->GetProperties().GetPropertyLong(wxT("Lock"));
2052
2053 if (lock1 != -1 && lock1 == lock2)
2054 return false;
2055
2056 // Don't allow insertion before a locked object if it's at the beginning of the buffer.
2057 if (pos == 0 && lock1 != -1)
2058 return false;
2059
2060 return true;
2061}
2062
2063
2064class wxRichTextEnhancedDrawingHandler: public wxRichTextDrawingHandler
2065{
2066public:
2067 wxRichTextEnhancedDrawingHandler()
2068 {
2069 SetName(wxT("enhanceddrawing"));
2070 m_lockBackgroundColour = wxColour(220, 220, 220);
2071 }
2072
2073 /**
2074 Returns @true if this object has virtual attributes that we can provide.
2075 */
2076 virtual bool HasVirtualAttributes(wxRichTextObject* obj) const;
2077
2078 /**
2079 Provides virtual attributes that we can provide.
2080 */
2081 virtual bool GetVirtualAttributes(wxRichTextAttr& attr, wxRichTextObject* obj) const;
2082
2083 wxColour m_lockBackgroundColour;
2084};
2085
2086bool wxRichTextEnhancedDrawingHandler::HasVirtualAttributes(wxRichTextObject* obj) const
2087{
2088 return obj->GetProperties().HasProperty(wxT("Lock"));
2089}
2090
2091bool wxRichTextEnhancedDrawingHandler::GetVirtualAttributes(wxRichTextAttr& attr, wxRichTextObject* obj) const
2092{
2093 if (obj->GetProperties().HasProperty(wxT("Lock")))
2094 {
2095 attr.SetBackgroundColour(m_lockBackgroundColour);
2096 return true;
2097 }
2098 return false;
2099}
2100
2101void MyRichTextCtrl::SetEnhancedDrawingHandler()
2102{
2103 wxRichTextBuffer::AddDrawingHandler(new wxRichTextEnhancedDrawingHandler);
2104}