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