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