Added missing RTTI
[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 #if wxUSE_RICHTEXT
20
21 #include "wx/richtext/richtextformatdlg.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/listbox.h"
25 #include "wx/combobox.h"
26 #include "wx/textctrl.h"
27 #include "wx/sizer.h"
28 #include "wx/stattext.h"
29 #include "wx/statline.h"
30 #include "wx/radiobut.h"
31 #include "wx/icon.h"
32 #include "wx/bitmap.h"
33 #include "wx/dcclient.h"
34 #include "wx/frame.h"
35 #include "wx/checkbox.h"
36 #include "wx/button.h"
37 #endif // WX_PRECOMP
38
39 #include "wx/bookctrl.h"
40 #include "wx/colordlg.h"
41 #include "wx/settings.h"
42 #include "wx/module.h"
43 #include "wx/imaglist.h"
44
45 #include "wx/richtext/richtextctrl.h"
46 #include "wx/richtext/richtextstyles.h"
47
48 #ifdef __WXMAC__
49 #include "../../src/richtext/richtextfontpage.cpp"
50 #include "../../src/richtext/richtextindentspage.cpp"
51 #include "../../src/richtext/richtexttabspage.cpp"
52 #include "../../src/richtext/richtextbulletspage.cpp"
53 #include "../../src/richtext/richtextstylepage.cpp"
54 #include "../../src/richtext/richtextliststylepage.cpp"
55 #include "../../src/richtext/richtextsizepage.cpp"
56 #include "../../src/richtext/richtextmarginspage.cpp"
57 #include "../../src/richtext/richtextborderspage.cpp"
58 #include "../../src/richtext/richtextbackgroundpage.cpp"
59 #else
60 #include "richtextfontpage.cpp"
61 #include "richtextindentspage.cpp"
62 #include "richtexttabspage.cpp"
63 #include "richtextbulletspage.cpp"
64 #include "richtextmarginspage.cpp"
65 #include "richtextsizepage.cpp"
66 #include "richtextborderspage.cpp"
67 #include "richtextbackgroundpage.cpp"
68 // Digital Mars can't cope with this much code
69 #ifndef __DMC__
70 #include "richtextliststylepage.cpp"
71 #endif
72 #include "richtextstylepage.cpp"
73 #endif
74
75 #if 0 // def __WXMAC__
76 #define wxRICHTEXT_USE_TOOLBOOK 1
77 #else
78 #define wxRICHTEXT_USE_TOOLBOOK 0
79 #endif
80
81 bool wxRichTextFormattingDialog::sm_showToolTips = false;
82
83 IMPLEMENT_CLASS(wxRichTextDialogPage, wxPanel)
84
85 IMPLEMENT_CLASS(wxRichTextFormattingDialog, wxPropertySheetDialog)
86
87 BEGIN_EVENT_TABLE(wxRichTextFormattingDialog, wxPropertySheetDialog)
88 EVT_BOOKCTRL_PAGE_CHANGED(wxID_ANY, wxRichTextFormattingDialog::OnTabChanged)
89 EVT_BUTTON(wxID_HELP, wxRichTextFormattingDialog::OnHelp)
90 EVT_UPDATE_UI(wxID_HELP, wxRichTextFormattingDialog::OnUpdateHelp)
91 END_EVENT_TABLE()
92
93 IMPLEMENT_HELP_PROVISION(wxRichTextFormattingDialog)
94
95 wxRichTextFormattingDialogFactory* wxRichTextFormattingDialog::ms_FormattingDialogFactory = NULL;
96
97 void wxRichTextFormattingDialog::Init()
98 {
99 m_styleDefinition = NULL;
100 m_styleSheet = NULL;
101 m_object = NULL;
102 m_options = 0;
103 }
104
105 wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
106 {
107 delete m_styleDefinition;
108 }
109
110 bool wxRichTextFormattingDialog::Create(long flags, wxWindow* parent, const wxString& title, wxWindowID id,
111 const wxPoint& pos, const wxSize& sz, long style)
112 {
113 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP|wxWS_EX_VALIDATE_RECURSIVELY);
114 #ifdef __WXMAC__
115 SetWindowVariant(wxWINDOW_VARIANT_SMALL);
116 #endif
117
118 int resizeBorder = wxRESIZE_BORDER;
119
120 GetFormattingDialogFactory()->SetSheetStyle(this);
121
122 wxPropertySheetDialog::Create(parent, id, title, pos, sz,
123 style | (int)wxPlatform::IfNot(wxOS_WINDOWS_CE, resizeBorder)
124 );
125
126 GetFormattingDialogFactory()->CreateButtons(this);
127 GetFormattingDialogFactory()->CreatePages(flags, this);
128
129 LayoutDialog();
130
131 return true;
132 }
133
134 /// Get attributes from the given range
135 bool wxRichTextFormattingDialog::GetStyle(wxRichTextCtrl* ctrl, const wxRichTextRange& range)
136 {
137 if (ctrl->GetFocusObject()->GetStyleForRange(range.ToInternal(), m_attributes))
138 return UpdateDisplay();
139 else
140 return false;
141 }
142
143 /// Apply attributes to the given range, only applying if necessary (wxRICHTEXT_SETSTYLE_OPTIMIZE)
144 bool wxRichTextFormattingDialog::ApplyStyle(wxRichTextCtrl* ctrl, const wxRichTextRange& range, int flags)
145 {
146 return ctrl->SetStyleEx(range, m_attributes, flags);
147 }
148
149 // Apply attributes to the object being edited, if any
150 bool wxRichTextFormattingDialog::ApplyStyle(wxRichTextCtrl* ctrl, int flags)
151 {
152 if (GetObject())
153 {
154 ctrl->SetStyle(GetObject(), m_attributes, flags);
155 return true;
156 }
157 else
158 return false;
159 }
160
161 /// Set the attributes and optionally update the display
162 bool wxRichTextFormattingDialog::SetStyle(const wxRichTextAttr& style, bool update)
163 {
164 m_attributes = style;
165 if (update)
166 UpdateDisplay();
167 return true;
168 }
169
170 /// Set the style definition and optionally update the display
171 bool wxRichTextFormattingDialog::SetStyleDefinition(const wxRichTextStyleDefinition& styleDef, wxRichTextStyleSheet* sheet, bool update)
172 {
173 m_styleSheet = sheet;
174
175 if (m_styleDefinition)
176 delete m_styleDefinition;
177 m_styleDefinition = styleDef.Clone();
178
179 return SetStyle(m_styleDefinition->GetStyle(), update);
180 }
181
182 /// Transfers the data and from to the window
183 bool wxRichTextFormattingDialog::TransferDataToWindow()
184 {
185 if (m_styleDefinition)
186 m_attributes = m_styleDefinition->GetStyle();
187
188 if (!wxPropertySheetDialog::TransferDataToWindow())
189 return false;
190
191 return true;
192 }
193
194 bool wxRichTextFormattingDialog::TransferDataFromWindow()
195 {
196 if (!wxPropertySheetDialog::TransferDataFromWindow())
197 return false;
198
199 if (m_styleDefinition)
200 m_styleDefinition->GetStyle() = m_attributes;
201
202 return true;
203 }
204
205 /// Update the display
206 bool wxRichTextFormattingDialog::UpdateDisplay()
207 {
208 return TransferDataToWindow();
209 }
210
211 /// Apply the styles when a different tab is selected, so the previews are
212 /// up to date
213 void wxRichTextFormattingDialog::OnTabChanged(wxBookCtrlEvent& event)
214 {
215 if (GetBookCtrl() != event.GetEventObject())
216 {
217 event.Skip();
218 return;
219 }
220
221 int oldPageId = event.GetOldSelection();
222 if (oldPageId != -1)
223 {
224 wxWindow* page = GetBookCtrl()->GetPage(oldPageId);
225 if (page)
226 page->TransferDataFromWindow();
227 }
228
229 int pageId = event.GetSelection();
230 if (pageId != -1)
231 {
232 wxWindow* page = GetBookCtrl()->GetPage(pageId);
233 if (page)
234 page->TransferDataToWindow();
235 }
236 }
237
238 /// Respond to help command
239 void wxRichTextFormattingDialog::OnHelp(wxCommandEvent& event)
240 {
241 int selPage = GetBookCtrl()->GetSelection();
242 if (selPage != wxNOT_FOUND)
243 {
244 int pageId = -1;
245 if (selPage < (int) m_pageIds.GetCount())
246 pageId = m_pageIds[selPage];
247 if (!GetFormattingDialogFactory()->ShowHelp(pageId, this))
248 event.Skip();
249 }
250 }
251
252 void wxRichTextFormattingDialog::OnUpdateHelp(wxUpdateUIEvent& event)
253 {
254 event.Enable(true);
255 }
256
257 void wxRichTextFormattingDialog::SetFormattingDialogFactory(wxRichTextFormattingDialogFactory* factory)
258 {
259 if (ms_FormattingDialogFactory)
260 delete ms_FormattingDialogFactory;
261 ms_FormattingDialogFactory = factory;
262 }
263
264 // Find a page by class
265 wxWindow* wxRichTextFormattingDialog::FindPage(wxClassInfo* info) const
266 {
267 size_t i;
268 for (i = 0; i < GetBookCtrl()->GetPageCount(); i++)
269 {
270 wxWindow* w = GetBookCtrl()->GetPage(i);
271 if (w && w->GetClassInfo() == info)
272 return w;
273 }
274 return NULL;
275 }
276
277
278 /*!
279 * Factory for formatting dialog
280 */
281
282 /// Create all pages, under the dialog's book control, also calling AddPage
283 bool wxRichTextFormattingDialogFactory::CreatePages(long pages, wxRichTextFormattingDialog* dialog)
284 {
285 if (dialog->GetImageList())
286 dialog->GetBookCtrl()->SetImageList(dialog->GetImageList());
287
288 int availablePageCount = GetPageIdCount();
289 int i;
290 bool selected = false;
291 for (i = 0; i < availablePageCount; i ++)
292 {
293 int pageId = GetPageId(i);
294 if (pageId != -1 && (pages & pageId))
295 {
296 wxString title;
297 wxPanel* panel = CreatePage(pageId, title, dialog);
298 wxASSERT( panel != NULL );
299 if (panel)
300 {
301 int imageIndex = GetPageImage(pageId);
302 dialog->GetBookCtrl()->AddPage(panel, title, !selected, imageIndex);
303 selected = true;
304
305 dialog->AddPageId(pageId);
306 }
307 }
308 }
309
310 return true;
311 }
312
313 /// Create a page, given a page identifier
314 wxPanel* wxRichTextFormattingDialogFactory::CreatePage(int page, wxString& title, wxRichTextFormattingDialog* dialog)
315 {
316 if (page == wxRICHTEXT_FORMAT_STYLE_EDITOR)
317 {
318 wxRichTextStylePage* page = new wxRichTextStylePage(dialog->GetBookCtrl(), wxID_ANY);
319 title = _("Style");
320 return page;
321 }
322 else if (page == wxRICHTEXT_FORMAT_FONT)
323 {
324 wxRichTextFontPage* page = new wxRichTextFontPage(dialog->GetBookCtrl(), wxID_ANY);
325 title = _("Font");
326 return page;
327 }
328 else if (page == wxRICHTEXT_FORMAT_INDENTS_SPACING)
329 {
330 wxRichTextIndentsSpacingPage* page = new wxRichTextIndentsSpacingPage(dialog->GetBookCtrl(), wxID_ANY);
331 title = _("Indents && Spacing");
332 return page;
333 }
334 else if (page == wxRICHTEXT_FORMAT_TABS)
335 {
336 wxRichTextTabsPage* page = new wxRichTextTabsPage(dialog->GetBookCtrl(), wxID_ANY);
337 title = _("Tabs");
338 return page;
339 }
340 else if (page == wxRICHTEXT_FORMAT_BULLETS)
341 {
342 wxRichTextBulletsPage* page = new wxRichTextBulletsPage(dialog->GetBookCtrl(), wxID_ANY);
343 title = _("Bullets");
344 return page;
345 }
346 #ifndef __DMC__
347 else if (page == wxRICHTEXT_FORMAT_LIST_STYLE)
348 {
349 wxRichTextListStylePage* page = new wxRichTextListStylePage(dialog->GetBookCtrl(), wxID_ANY);
350 title = _("List Style");
351 return page;
352 }
353 #endif
354 else if (page == wxRICHTEXT_FORMAT_SIZE)
355 {
356 wxRichTextSizePage* page = new wxRichTextSizePage(dialog->GetBookCtrl(), wxID_ANY);
357 title = _("Size");
358 return page;
359 }
360 else if (page == wxRICHTEXT_FORMAT_MARGINS)
361 {
362 wxRichTextMarginsPage* page = new wxRichTextMarginsPage(dialog->GetBookCtrl(), wxID_ANY);
363 title = _("Margins");
364 return page;
365 }
366 else if (page == wxRICHTEXT_FORMAT_BORDERS)
367 {
368 wxRichTextBordersPage* page = new wxRichTextBordersPage(dialog->GetBookCtrl(), wxID_ANY);
369 title = _("Borders");
370 return page;
371 }
372 else if (page == wxRICHTEXT_FORMAT_BACKGROUND)
373 {
374 wxRichTextBackgroundPage* page = new wxRichTextBackgroundPage(dialog->GetBookCtrl(), wxID_ANY);
375 title = _("Background");
376 return page;
377 }
378 else
379 return NULL;
380 }
381
382 /// Enumerate all available page identifiers
383 int wxRichTextFormattingDialogFactory::GetPageId(int i) const
384 {
385 int pages[] = {
386 wxRICHTEXT_FORMAT_STYLE_EDITOR,
387 wxRICHTEXT_FORMAT_FONT,
388 wxRICHTEXT_FORMAT_INDENTS_SPACING,
389 wxRICHTEXT_FORMAT_BULLETS,
390 wxRICHTEXT_FORMAT_TABS,
391 wxRICHTEXT_FORMAT_LIST_STYLE,
392 wxRICHTEXT_FORMAT_SIZE,
393 wxRICHTEXT_FORMAT_MARGINS,
394 wxRICHTEXT_FORMAT_BORDERS,
395 wxRICHTEXT_FORMAT_BACKGROUND
396 };
397
398 if (i < 0 || i >= GetPageIdCount())
399 return -1;
400
401 return pages[i];
402 }
403
404 /// Get the number of available page identifiers
405 int wxRichTextFormattingDialogFactory::GetPageIdCount() const
406 {
407 #ifdef __DMC__
408 return 9;
409 #else
410 return 10;
411 #endif
412 }
413
414 /// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
415 bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog* dialog)
416 {
417 #if wxRICHTEXT_USE_TOOLBOOK
418 int sheetStyle = wxPROPSHEET_SHRINKTOFIT;
419 #ifdef __WXMAC__
420 sheetStyle |= wxPROPSHEET_BUTTONTOOLBOOK;
421 #else
422 sheetStyle |= wxPROPSHEET_TOOLBOOK;
423 #endif
424
425 dialog->SetSheetStyle(sheetStyle);
426 dialog->SetSheetInnerBorder(0);
427 dialog->SetSheetOuterBorder(0);
428 #else
429 wxUnusedVar(dialog);
430 #endif // wxRICHTEXT_USE_TOOLBOOK
431
432 return true;
433 }
434
435 /// Create the main dialog buttons
436 bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog* dialog)
437 {
438 int flags = wxOK|wxCANCEL;
439 #ifndef __WXWINCE__
440 if (dialog->GetWindowStyleFlag() & wxRICHTEXT_FORMAT_HELP_BUTTON)
441 flags |= wxHELP;
442 #endif
443
444 // If using a toolbook, also follow Mac style and don't create buttons
445 #if !wxRICHTEXT_USE_TOOLBOOK
446 dialog->CreateButtons(flags);
447 #endif
448
449 return true;
450 }
451
452 // Invoke help for the dialog
453 bool wxRichTextFormattingDialogFactory::ShowHelp(int WXUNUSED(page), wxRichTextFormattingDialog* dialog)
454 {
455 wxRichTextDialogPage* window = NULL;
456 int sel = dialog->GetBookCtrl()->GetSelection();
457 if (sel != -1)
458 window = wxDynamicCast(dialog->GetBookCtrl()->GetPage(sel), wxRichTextDialogPage);
459 if (window && window->GetHelpId() != -1)
460 {
461 if (window->GetUICustomization())
462 return window->GetUICustomization()->ShowHelp(dialog, window->GetHelpId());
463 else if (dialog->GetUICustomization())
464 return dialog->GetUICustomization()->ShowHelp(dialog, window->GetHelpId());
465 else
466 return false;
467 }
468 else if (dialog->GetHelpId() != -1 && dialog->GetUICustomization())
469 return dialog->ShowHelp(dialog);
470 else
471 return false;
472 }
473
474 /*
475 * Module to initialise and clean up handlers
476 */
477
478 class wxRichTextFormattingDialogModule: public wxModule
479 {
480 DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule)
481 public:
482 wxRichTextFormattingDialogModule() {}
483 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory); return true; }
484 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL); }
485 };
486
487 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule, wxModule)
488
489 /*
490 * Font preview control
491 */
492
493 BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl, wxWindow)
494 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint)
495 END_EVENT_TABLE()
496
497 wxRichTextFontPreviewCtrl::wxRichTextFontPreviewCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& sz, long style)
498 {
499 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
500 style |= wxBORDER_THEME;
501
502 wxWindow::Create(parent, id, pos, sz, style);
503
504 SetBackgroundColour(*wxWHITE);
505 m_textEffects = 0;
506 }
507
508 void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
509 {
510 wxPaintDC dc(this);
511
512 wxSize size = GetSize();
513 wxFont font = GetFont();
514
515 if ((GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT) || (GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT))
516 {
517 double size = static_cast<double>(font.GetPointSize()) / wxSCRIPT_MUL_FACTOR;
518 font.SetPointSize( static_cast<int>(size) );
519 }
520
521 if ( font.IsOk() )
522 {
523 dc.SetFont(font);
524 // Calculate vertical and horizontal centre
525 wxCoord w = 0, h = 0;
526
527 wxString text(_("ABCDEFGabcdefg12345"));
528 if (GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS)
529 text.MakeUpper();
530
531 dc.GetTextExtent( text, &w, &h);
532 int cx = wxMax(2, (size.x/2) - (w/2));
533 int cy = wxMax(2, (size.y/2) - (h/2));
534
535 if ( GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT )
536 cy -= h/2;
537 if ( GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT )
538 cy += h/2;
539
540 dc.SetTextForeground(GetForegroundColour());
541 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
542 dc.DrawText(text, cx, cy);
543
544 if (GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH)
545 {
546 dc.SetPen(wxPen(GetForegroundColour(), 1));
547 dc.DrawLine(cx, (int) (cy + h/2 + 0.5), cx + w, (int) (cy + h/2 + 0.5));
548 }
549
550 dc.DestroyClippingRegion();
551 }
552 }
553
554 // Helper for pages to get the top-level dialog
555 wxRichTextFormattingDialog* wxRichTextFormattingDialog::GetDialog(wxWindow* win)
556 {
557 wxWindow* p = win->GetParent();
558 while (p && !wxDynamicCast(p, wxRichTextFormattingDialog))
559 p = p->GetParent();
560 wxRichTextFormattingDialog* dialog = wxDynamicCast(p, wxRichTextFormattingDialog);
561 return dialog;
562 }
563
564 // Helper for pages to get the attributes
565 wxRichTextAttr* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow* win)
566 {
567 wxRichTextFormattingDialog* dialog = GetDialog(win);
568 if (dialog)
569 return & dialog->GetAttributes();
570 else
571 return NULL;
572 }
573
574 #if 0
575 // Helper for pages to get the attributes to reset
576 wxRichTextAttr* wxRichTextFormattingDialog::GetDialogResetAttributes(wxWindow* win)
577 {
578 wxRichTextFormattingDialog* dialog = GetDialog(win);
579 if (dialog)
580 return & dialog->GetResetAttributes();
581 else
582 return NULL;
583 }
584 #endif
585
586 // Helper for pages to get the style
587 wxRichTextStyleDefinition* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow* win)
588 {
589 wxRichTextFormattingDialog* dialog = GetDialog(win);
590 if (dialog)
591 return dialog->GetStyleDefinition();
592 else
593 return NULL;
594 }
595
596 void wxRichTextFormattingDialog::SetDimensionValue(wxTextAttrDimension& dim, wxTextCtrl* valueCtrl, wxComboBox* unitsCtrl, wxCheckBox* checkBox)
597 {
598 int unitsIdx = 0;
599
600 if (!dim.IsValid())
601 {
602 checkBox->SetValue(false);
603 valueCtrl->SetValue(wxT("0"));
604 unitsCtrl->SetSelection(0);
605 #if 0
606 dim.SetValue(0);
607 dim.SetUnits(wxTEXT_ATTR_UNITS_PIXELS);
608 #endif
609 }
610 else
611 {
612 checkBox->SetValue(true);
613 if (dim.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
614 {
615 unitsIdx = 1;
616 float value = float(dim.GetValue()) / 100.0;
617 valueCtrl->SetValue(wxString::Format(wxT("%.2f"), value));
618 }
619 else if (dim.GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
620 {
621 unitsIdx = 2;
622 valueCtrl->SetValue(wxString::Format(wxT("%d"), (int) dim.GetValue()));
623 }
624 else
625 {
626 unitsIdx = 0;
627 valueCtrl->SetValue(wxString::Format(wxT("%d"), (int) dim.GetValue()));
628 }
629
630 unitsCtrl->SetSelection(unitsIdx);
631 }
632 }
633
634 void wxRichTextFormattingDialog::GetDimensionValue(wxTextAttrDimension& dim, wxTextCtrl* valueCtrl, wxComboBox* unitsCtrl, wxCheckBox* checkBox)
635 {
636 if (!checkBox->GetValue())
637 {
638 dim.Reset();
639 }
640 else
641 {
642 if (unitsCtrl->GetSelection() == 1)
643 dim.SetUnits(wxTEXT_ATTR_UNITS_TENTHS_MM);
644 else if (unitsCtrl->GetSelection() == 2)
645 dim.SetUnits(wxTEXT_ATTR_UNITS_PERCENTAGE);
646 else
647 dim.SetUnits(wxTEXT_ATTR_UNITS_PIXELS);
648
649 int value = 0;
650 if (ConvertFromString(valueCtrl->GetValue(), value, dim.GetUnits()))
651 dim.SetValue(value);
652 }
653 }
654
655 bool wxRichTextFormattingDialog::ConvertFromString(const wxString& string, int& ret, int scale)
656 {
657 const wxChar* chars = string.GetData();
658 int remain = 2;
659 bool dot = false;
660 ret = 0;
661
662 for (unsigned int i = 0; i < string.Len() && remain; i++)
663 {
664 if (!(chars[i] >= wxT('0') && chars[i] <= wxT('9')) && !(scale == wxTEXT_ATTR_UNITS_TENTHS_MM && chars[i] == wxT('.')))
665 return false;
666
667 if (chars[i] == wxT('.'))
668 {
669 dot = true;
670 continue;
671 }
672
673 if (dot)
674 remain--;
675
676 ret = ret * 10 + chars[i] - wxT('0');
677 }
678
679 while (remain-- > 0 && scale == wxTEXT_ATTR_UNITS_TENTHS_MM)
680 ret *= 10;
681
682 return true;
683 }
684
685 /*
686 * A control for displaying a small preview of a colour or bitmap
687 */
688
689 BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl, wxControl)
690 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent)
691 END_EVENT_TABLE()
692
693 IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl, wxControl)
694
695 wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
696 {
697 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
698 style |= wxBORDER_THEME;
699
700 wxControl::Create(parent, id, pos, size, style);
701
702 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
703 }
704
705 wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
706 {
707 }
708
709 void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent& event)
710 {
711 if (event.LeftDown())
712 {
713 wxWindow* parent = GetParent();
714 while (parent != NULL && !wxDynamicCast(parent, wxDialog) && !wxDynamicCast(parent, wxFrame))
715 parent = parent->GetParent();
716
717 wxColourData data;
718 data.SetChooseFull(true);
719 data.SetColour(m_colour);
720 #if wxUSE_COLOURDLG
721 wxColourDialog *dialog = new wxColourDialog(parent, &data);
722 // Crashes on wxMac (no m_peer)
723 #ifndef __WXMAC__
724 dialog->SetTitle(_("Colour"));
725 #endif
726 if (dialog->ShowModal() == wxID_OK)
727 {
728 wxColourData retData = dialog->GetColourData();
729 m_colour = retData.GetColour();
730 SetBackgroundColour(m_colour);
731 }
732 dialog->Destroy();
733 #endif // wxUSE_COLOURDLG
734 Refresh();
735
736 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
737 GetEventHandler()->ProcessEvent(event);
738 }
739 }
740
741 #if wxUSE_HTML
742
743 /*!
744 * wxRichTextFontListBox class declaration
745 * A listbox to display styles.
746 */
747
748 IMPLEMENT_CLASS(wxRichTextFontListBox, wxHtmlListBox)
749
750 BEGIN_EVENT_TABLE(wxRichTextFontListBox, wxHtmlListBox)
751 END_EVENT_TABLE()
752
753 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos,
754 const wxSize& size, long style)
755 {
756 Init();
757 Create(parent, id, pos, size, style);
758 }
759
760 bool wxRichTextFontListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
761 const wxSize& size, long style)
762 {
763 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
764 style |= wxBORDER_THEME;
765
766 return wxHtmlListBox::Create(parent, id, pos, size, style);
767 }
768
769 wxRichTextFontListBox::~wxRichTextFontListBox()
770 {
771 }
772
773 /// Returns the HTML for this item
774 wxString wxRichTextFontListBox::OnGetItem(size_t n) const
775 {
776 if (m_faceNames.GetCount() == 0)
777 return wxEmptyString;
778
779 wxString str = CreateHTML(m_faceNames[n]);
780 return str;
781 }
782
783 /// Get font name for index
784 wxString wxRichTextFontListBox::GetFaceName(size_t i) const
785 {
786 return m_faceNames[i];
787 }
788
789 /// Set selection for string, returning the index.
790 int wxRichTextFontListBox::SetFaceNameSelection(const wxString& name)
791 {
792 int i = m_faceNames.Index(name);
793 SetSelection(i);
794
795 return i;
796 }
797
798 /// Updates the font list
799 void wxRichTextFontListBox::UpdateFonts()
800 {
801 wxArrayString facenames = wxRichTextCtrl::GetAvailableFontNames();
802 m_faceNames = facenames;
803 m_faceNames.Sort();
804
805 SetItemCount(m_faceNames.GetCount());
806 Refresh();
807 }
808
809 #if 0
810 // Convert a colour to a 6-digit hex string
811 static wxString ColourToHexString(const wxColour& col)
812 {
813 wxString hex;
814
815 hex += wxDecToHex(col.Red());
816 hex += wxDecToHex(col.Green());
817 hex += wxDecToHex(col.Blue());
818
819 return hex;
820 }
821 #endif
822
823 /// Creates a suitable HTML fragment for a definition
824 wxString wxRichTextFontListBox::CreateHTML(const wxString& facename) const
825 {
826 wxString str = wxT("<font");
827
828 str << wxT(" size=\"+2\"");;
829
830 if (!facename.IsEmpty() && facename != _("(none)"))
831 str << wxT(" face=\"") << facename << wxT("\"");
832 /*
833 if (def->GetStyle().GetTextColour().IsOk())
834 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
835 */
836
837 str << wxT(">");
838
839 bool hasBold = false;
840
841 if (hasBold)
842 str << wxT("<b>");
843
844 str += facename;
845
846 if (hasBold)
847 str << wxT("</b>");
848
849 str << wxT("</font>");
850
851 return str;
852 }
853
854 #endif
855 // wxUSE_HTML
856
857
858 #endif
859 // wxUSE_RICHTEXT