Added a customisable formatting dialog for wxRichTextCtrl. Control the
[wxWidgets.git] / src / richtext / richtextformatdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextformatdlg.cpp
3 // Purpose: Formatting dialog for wxRichTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2006-10-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/defs.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/listbox.h"
23 #include "wx/combobox.h"
24 #include "wx/textctrl.h"
25 #include "wx/sizer.h"
26 #include "wx/stattext.h"
27 #include "wx/statline.h"
28 #include "wx/radiobut.h"
29 #include "wx/icon.h"
30 #include "wx/bitmap.h"
31 #include "wx/dcclient.h"
32 #include "wx/frame.h"
33 #include "wx/imaglist.h"
34 #include "wx/checkbox.h"
35 #include "wx/button.h"
36 #endif // WX_PRECOMP
37
38 #if wxUSE_RICHTEXT
39
40 #include "wx/bookctrl.h"
41 #include "wx/colordlg.h"
42 #include "wx/fontenum.h"
43 #include "wx/settings.h"
44 #include "wx/module.h"
45
46 #include "wx/richtext/richtextformatdlg.h"
47 #include "wx/richtext/richtextctrl.h"
48 #include "wx/richtext/richtextstyles.h"
49
50 #include "richtextfontpage.cpp"
51 #include "richtextindentspage.cpp"
52 #include "richtexttabspage.cpp"
53 #include "richtextbulletspage.cpp"
54 #include "richtextstylepage.cpp"
55
56 #ifdef __WXMAC__
57 #define wxRICHTEXT_USE_TOOLBOOK true
58 #else
59 #define wxRICHTEXT_USE_TOOLBOOK false
60 #endif
61
62 IMPLEMENT_CLASS(wxRichTextFormattingDialog, wxPropertySheetDialog)
63
64 BEGIN_EVENT_TABLE(wxRichTextFormattingDialog, wxPropertySheetDialog)
65 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxRichTextFormattingDialog::OnTabChanged)
66 END_EVENT_TABLE()
67
68 wxRichTextFormattingDialogFactory* wxRichTextFormattingDialog::ms_FormattingDialogFactory = NULL;
69
70 void wxRichTextFormattingDialog::Init()
71 {
72 m_imageList = NULL;
73 m_styleDefinition = NULL;
74 m_styleSheet = NULL;
75 }
76
77 wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
78 {
79 delete m_imageList;
80 delete m_styleDefinition;
81 }
82
83 bool wxRichTextFormattingDialog::Create(long flags, wxWindow* parent, const wxString& title, wxWindowID id,
84 const wxPoint& pos, const wxSize& sz, long style)
85 {
86 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP|wxWS_EX_VALIDATE_RECURSIVELY);
87
88 int resizeBorder = wxRESIZE_BORDER;
89
90 GetFormattingDialogFactory()->SetSheetStyle(this);
91
92 wxPropertySheetDialog::Create(parent, id, title, pos, sz,
93 style | (int)wxPlatform::IfNot(wxWinCE, resizeBorder)
94 );
95
96 GetFormattingDialogFactory()->CreateButtons(this);
97 GetFormattingDialogFactory()->CreatePages(flags, this);
98
99 LayoutDialog();
100
101 return true;
102 }
103
104 /// Get attributes from the given range
105 bool wxRichTextFormattingDialog::GetStyle(wxRichTextCtrl* ctrl, const wxRichTextRange& range)
106 {
107 if (ctrl->GetBuffer().GetStyleForRange(range.ToInternal(), m_attributes))
108 return UpdateDisplay();
109 else
110 return false;
111 }
112
113 /// Apply attributes to the given range, only applying if necessary (wxRICHTEXT_SETSTYLE_OPTIMIZE)
114 bool wxRichTextFormattingDialog::ApplyStyle(wxRichTextCtrl* ctrl, const wxRichTextRange& range, int flags)
115 {
116 return ctrl->SetStyleEx(range, m_attributes, flags);
117 }
118
119 /// Set the attributes and optionally update the display
120 bool wxRichTextFormattingDialog::SetStyle(const wxTextAttrEx& style, bool update)
121 {
122 m_attributes = style;
123 if (update)
124 UpdateDisplay();
125 return true;
126 }
127
128 /// Set the style definition and optionally update the display
129 bool wxRichTextFormattingDialog::SetStyleDefinition(const wxRichTextStyleDefinition& styleDef, wxRichTextStyleSheet* sheet, bool update)
130 {
131 m_styleSheet = sheet;
132
133 if (m_styleDefinition)
134 delete m_styleDefinition;
135 m_styleDefinition = styleDef.Clone();
136
137 return SetStyle(m_styleDefinition->GetStyle(), update);
138 }
139
140 /// Transfers the data and from to the window
141 bool wxRichTextFormattingDialog::TransferDataToWindow()
142 {
143 if (m_styleDefinition)
144 m_attributes = m_styleDefinition->GetStyle();
145
146 if (!wxPropertySheetDialog::TransferDataToWindow())
147 return false;
148
149 return true;
150 }
151
152 bool wxRichTextFormattingDialog::TransferDataFromWindow()
153 {
154 if (!wxPropertySheetDialog::TransferDataFromWindow())
155 return false;
156
157 if (m_styleDefinition)
158 m_styleDefinition->GetStyle() = m_attributes;
159
160 return true;
161 }
162
163 /// Update the display
164 bool wxRichTextFormattingDialog::UpdateDisplay()
165 {
166 return TransferDataToWindow();
167 }
168
169 /// Apply the styles when a different tab is selected, so the previews are
170 /// up to date
171 void wxRichTextFormattingDialog::OnTabChanged(wxNotebookEvent& event)
172 {
173 if (GetBookCtrl() != event.GetEventObject())
174 {
175 event.Skip();
176 return;
177 }
178
179 int oldPageId = event.GetOldSelection();
180 if (oldPageId != -1)
181 {
182 wxWindow* page = GetBookCtrl()->GetPage(oldPageId);
183 if (page)
184 page->TransferDataFromWindow();
185 }
186
187 int pageId = event.GetSelection();
188 if (pageId != -1)
189 {
190 wxWindow* page = GetBookCtrl()->GetPage(pageId);
191 if (page)
192 page->TransferDataToWindow();
193 }
194 }
195
196 void wxRichTextFormattingDialog::SetFormattingDialogFactory(wxRichTextFormattingDialogFactory* factory)
197 {
198 if (ms_FormattingDialogFactory)
199 delete ms_FormattingDialogFactory;
200 ms_FormattingDialogFactory = factory;
201 }
202
203 /*!
204 * Factory for formatting dialog
205 */
206
207 /// Create all pages, under the dialog's book control, also calling AddPage
208 bool wxRichTextFormattingDialogFactory::CreatePages(long pages, wxRichTextFormattingDialog* dialog)
209 {
210 if (dialog->GetImageList())
211 dialog->GetBookCtrl()->SetImageList(dialog->GetImageList());
212
213 int availablePageCount = GetPageIdCount();
214 int i;
215 for (i = 0; i < availablePageCount; i ++)
216 {
217 int pageId = GetPageId(i);
218 if (pageId != -1 && (pages & pageId))
219 {
220 wxString title;
221 wxPanel* panel = CreatePage(pageId, title, dialog);
222 wxASSERT( panel != NULL );
223 if (panel)
224 {
225 int imageIndex = GetPageImage(pageId);
226 dialog->GetBookCtrl()->AddPage(panel, title, false, imageIndex);
227 }
228 }
229 }
230 return true;
231 }
232
233 /// Create a page, given a page identifier
234 wxPanel* wxRichTextFormattingDialogFactory::CreatePage(int page, wxString& title, wxRichTextFormattingDialog* dialog)
235 {
236 if (page == wxRICHTEXT_FORMAT_STYLE_EDITOR)
237 {
238 wxRichTextStylePage* page = new wxRichTextStylePage(dialog->GetBookCtrl(), wxID_ANY);
239 title = _("Style");
240 return page;
241 }
242 else if (page == wxRICHTEXT_FORMAT_FONT)
243 {
244 wxRichTextFontPage* page = new wxRichTextFontPage(dialog->GetBookCtrl(), wxID_ANY);
245 title = _("Font");
246 return page;
247 }
248 else if (page == wxRICHTEXT_FORMAT_INDENTS_SPACING)
249 {
250 wxRichTextIndentsSpacingPage* page = new wxRichTextIndentsSpacingPage(dialog->GetBookCtrl(), wxID_ANY);
251 title = _("Indents && Spacing");
252 return page;
253 }
254 else if (page == wxRICHTEXT_FORMAT_TABS)
255 {
256 wxRichTextTabsPage* page = new wxRichTextTabsPage(dialog->GetBookCtrl(), wxID_ANY);
257 title = _("Tabs");
258 return page;
259 }
260 else if (page == wxRICHTEXT_FORMAT_BULLETS)
261 {
262 wxRichTextBulletsPage* page = new wxRichTextBulletsPage(dialog->GetBookCtrl(), wxID_ANY);
263 title = _("Bullets");
264 return page;
265 }
266 else
267 return NULL;
268 }
269
270 /// Enumerate all available page identifiers
271 int wxRichTextFormattingDialogFactory::GetPageId(int i) const
272 {
273 int pages[] = {
274 wxRICHTEXT_FORMAT_STYLE_EDITOR,
275 wxRICHTEXT_FORMAT_FONT,
276 wxRICHTEXT_FORMAT_INDENTS_SPACING,
277 wxRICHTEXT_FORMAT_BULLETS,
278 wxRICHTEXT_FORMAT_TABS };
279
280 if (i < 0 || i > 4)
281 return -1;
282
283 return pages[i];
284 }
285
286 /// Get the number of available page identifiers
287 int wxRichTextFormattingDialogFactory::GetPageIdCount() const
288 {
289 return 5;
290 }
291
292 /// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
293 bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog* dialog)
294 {
295 bool useToolBook = wxRICHTEXT_USE_TOOLBOOK;
296 if (useToolBook)
297 {
298 int sheetStyle = wxPROPSHEET_SHRINKTOFIT;
299 #ifdef __WXMAC__
300 sheetStyle |= wxPROPSHEET_BUTTONTOOLBOOK;
301 #else
302 sheetStyle |= wxPROPSHEET_TOOLBOOK;
303 #endif
304
305 dialog->SetSheetStyle(sheetStyle);
306 dialog->SetSheetInnerBorder(0);
307 dialog->SetSheetOuterBorder(0);
308 }
309
310 return true;
311 }
312
313 /// Create the main dialog buttons
314 bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog* dialog)
315 {
316 bool useToolBook = wxRICHTEXT_USE_TOOLBOOK;
317
318 // If using a toolbook, also follow Mac style and don't create buttons
319 int flags = wxOK|wxCANCEL;
320 #ifndef __WXWINCE__
321 flags |= wxHELP;
322 #endif
323
324 if (!useToolBook)
325 dialog->CreateButtons(flags);
326
327 return true;
328 }
329
330 /*
331 * Module to initialise and clean up handlers
332 */
333
334 class wxRichTextFormattingDialogModule: public wxModule
335 {
336 DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule)
337 public:
338 wxRichTextFormattingDialogModule() {}
339 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory); return true; };
340 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL); };
341 };
342
343 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule, wxModule)
344
345 /*
346 * Font preview control
347 */
348
349 BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl, wxWindow)
350 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint)
351 END_EVENT_TABLE()
352
353 void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
354 {
355 wxPaintDC dc(this);
356
357 wxSize size = GetSize();
358 wxFont font = GetFont();
359
360 if ( font.Ok() )
361 {
362 dc.SetFont(font);
363 // Calculate vertical and horizontal centre
364 long w = 0, h = 0;
365
366 wxString text(_("ABCDEFGabcdefg12345"));
367
368 dc.GetTextExtent( text, &w, &h);
369 int cx = wxMax(2, (size.x/2) - (w/2));
370 int cy = wxMax(2, (size.y/2) - (h/2));
371
372 dc.SetTextForeground(GetForegroundColour());
373 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
374 dc.DrawText(text, cx, cy);
375 dc.DestroyClippingRegion();
376 }
377 }
378
379 // Helper for pages to get the top-level dialog
380 wxRichTextFormattingDialog* wxRichTextFormattingDialog::GetDialog(wxWindow* win)
381 {
382 wxWindow* p = win->GetParent();
383 while (p && !p->IsKindOf(CLASSINFO(wxRichTextFormattingDialog)))
384 p = p->GetParent();
385 wxRichTextFormattingDialog* dialog = wxDynamicCast(p, wxRichTextFormattingDialog);
386 return dialog;
387 }
388
389
390 // Helper for pages to get the attributes
391 wxTextAttrEx* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow* win)
392 {
393 wxRichTextFormattingDialog* dialog = GetDialog(win);
394 if (dialog)
395 return & dialog->GetAttributes();
396 else
397 return NULL;
398 }
399
400 // Helper for pages to get the style
401 wxRichTextStyleDefinition* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow* win)
402 {
403 wxRichTextFormattingDialog* dialog = GetDialog(win);
404 if (dialog)
405 return dialog->GetStyleDefinition();
406 else
407 return NULL;
408 }
409
410 /*
411 * A control for displaying a small preview of a colour or bitmap
412 */
413
414 BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl, wxControl)
415 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent)
416 END_EVENT_TABLE()
417
418 IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl, wxControl)
419
420 wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style):
421 wxControl(parent, id, pos, size, style)
422 {
423 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
424 SetBackgroundStyle(wxBG_STYLE_COLOUR);
425 }
426
427 wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
428 {
429 }
430
431 void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent& event)
432 {
433 if (event.LeftDown())
434 {
435 wxWindow* parent = GetParent();
436 while (parent != NULL && !parent->IsKindOf(CLASSINFO(wxDialog)) && !parent->IsKindOf(CLASSINFO(wxFrame)))
437 parent = parent->GetParent();
438
439 wxColourData data;
440 data.SetChooseFull(true);
441 data.SetColour(m_colour);
442 wxColourDialog *dialog = new wxColourDialog(parent, &data);
443 // Crashes on wxMac (no m_peer)
444 #ifndef __WXMAC__
445 dialog->SetTitle(_("Background colour"));
446 #endif
447 if (dialog->ShowModal() == wxID_OK)
448 {
449 wxColourData retData = dialog->GetColourData();
450 m_colour = retData.GetColour();
451 SetBackgroundColour(m_colour);
452 }
453 dialog->Destroy();
454 Refresh();
455
456 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
457 GetEventHandler()->ProcessEvent(event);
458 }
459 }
460
461 #if wxUSE_HTML
462
463 /*!
464 * wxRichTextFontListBox class declaration
465 * A listbox to display styles.
466 */
467
468 IMPLEMENT_CLASS(wxRichTextFontListBox, wxHtmlListBox)
469
470 BEGIN_EVENT_TABLE(wxRichTextFontListBox, wxHtmlListBox)
471 END_EVENT_TABLE()
472
473 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos,
474 const wxSize& size, long style)
475 {
476 Init();
477 Create(parent, id, pos, size, style);
478 }
479
480 bool wxRichTextFontListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
481 const wxSize& size, long style)
482 {
483 return wxHtmlListBox::Create(parent, id, pos, size, style);
484 }
485
486 wxRichTextFontListBox::~wxRichTextFontListBox()
487 {
488 }
489
490 /// Returns the HTML for this item
491 wxString wxRichTextFontListBox::OnGetItem(size_t n) const
492 {
493 if (m_faceNames.GetCount() == 0)
494 return wxEmptyString;
495
496 wxString str = CreateHTML(m_faceNames[n]);
497 return str;
498 }
499
500 /// Get font name for index
501 wxString wxRichTextFontListBox::GetFaceName(size_t i) const
502 {
503 return m_faceNames[i];
504 }
505
506 /// Set selection for string, returning the index.
507 int wxRichTextFontListBox::SetFaceNameSelection(const wxString& name)
508 {
509 int i = m_faceNames.Index(name);
510 SetSelection(i);
511
512 return i;
513 }
514
515 /// Updates the font list
516 void wxRichTextFontListBox::UpdateFonts()
517 {
518 wxFontEnumerator enumerator;
519 enumerator.EnumerateFacenames();
520 wxArrayString facenames = enumerator.GetFacenames();
521 m_faceNames = facenames;
522 m_faceNames.Sort();
523
524 SetItemCount(m_faceNames.GetCount());
525 Refresh();
526 }
527
528 #if 0
529 // Convert a colour to a 6-digit hex string
530 static wxString ColourToHexString(const wxColour& col)
531 {
532 wxString hex;
533
534 hex += wxDecToHex(col.Red());
535 hex += wxDecToHex(col.Green());
536 hex += wxDecToHex(col.Blue());
537
538 return hex;
539 }
540 #endif
541
542 /// Creates a suitable HTML fragment for a definition
543 wxString wxRichTextFontListBox::CreateHTML(const wxString& facename) const
544 {
545 wxString str = wxT("<font");
546
547 str << wxT(" size=\"+2\"");;
548
549 if (!facename.IsEmpty() && facename != _("(none)"))
550 str << wxT(" face=\"") << facename << wxT("\"");
551 /*
552 if (def->GetStyle().GetTextColour().Ok())
553 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
554 */
555
556 str << wxT(">");
557
558 bool hasBold = false;
559
560 if (hasBold)
561 str << wxT("<b>");
562
563 str += facename;
564
565 if (hasBold)
566 str << wxT("</b>");
567
568 str << wxT("</font>");
569
570 return str;
571 }
572
573 #endif
574 // wxUSE_HTML
575
576
577 #endif
578 // wxUSE_RICHTEXT
579