]> git.saurik.com Git - wxWidgets.git/blob - src/richtext/richtextformatdlg.cpp
avoid setting initial position if it was not specified, broken in r70734
[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 checkBox->SetValue(false);
602 valueCtrl->SetValue(wxT("0"));
603 unitsCtrl->SetSelection(0);
604 #if 0
605 dim.SetValue(0);
606 dim.SetUnits(wxTEXT_ATTR_UNITS_PIXELS);
607 #endif
608 }
609 else
610 {
611 checkBox->SetValue(true);
612 if (dim.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
613 {
614 unitsIdx = 1;
615 float value = float(dim.GetValue()) / 100.0;
616 valueCtrl->SetValue(wxString::Format(wxT("%.2f"), value));
617 }
618 else if (dim.GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
619 {
620 unitsIdx = 2;
621 valueCtrl->SetValue(wxString::Format(wxT("%d"), (int) dim.GetValue()));
622 }
623 else
624 {
625 unitsIdx = 0;
626 valueCtrl->SetValue(wxString::Format(wxT("%d"), (int) dim.GetValue()));
627 }
628
629 unitsCtrl->SetSelection(unitsIdx);
630 }
631 }
632
633 void wxRichTextFormattingDialog::GetDimensionValue(wxTextAttrDimension& dim, wxTextCtrl* valueCtrl, wxComboBox* unitsCtrl, wxCheckBox* checkBox)
634 {
635 if (!checkBox->GetValue())
636 {
637 dim.Reset();
638 }
639 else
640 {
641 if (unitsCtrl->GetSelection() == 1)
642 dim.SetUnits(wxTEXT_ATTR_UNITS_TENTHS_MM);
643 else if (unitsCtrl->GetSelection() == 2)
644 dim.SetUnits(wxTEXT_ATTR_UNITS_PERCENTAGE);
645 else
646 dim.SetUnits(wxTEXT_ATTR_UNITS_PIXELS);
647
648 int value = 0;
649 if (ConvertFromString(valueCtrl->GetValue(), value, dim.GetUnits()))
650 dim.SetValue(value);
651 }
652 }
653
654 bool wxRichTextFormattingDialog::ConvertFromString(const wxString& string, int& ret, int scale)
655 {
656 const wxChar* chars = string.GetData();
657 int remain = 2;
658 bool dot = false;
659 ret = 0;
660
661 for (unsigned int i = 0; i < string.Len() && remain; i++)
662 {
663 if (!(chars[i] >= wxT('0') && chars[i] <= wxT('9')) && !(scale == wxTEXT_ATTR_UNITS_TENTHS_MM && chars[i] == wxT('.')))
664 return false;
665
666 if (chars[i] == wxT('.'))
667 {
668 dot = true;
669 continue;
670 }
671
672 if (dot)
673 remain--;
674
675 ret = ret * 10 + chars[i] - wxT('0');
676 }
677
678 while (remain-- > 0 && scale == wxTEXT_ATTR_UNITS_TENTHS_MM)
679 ret *= 10;
680
681 return true;
682 }
683
684 /*
685 * A control for displaying a small preview of a colour or bitmap
686 */
687
688 BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl, wxControl)
689 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent)
690 END_EVENT_TABLE()
691
692 IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl, wxControl)
693
694 wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
695 {
696 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
697 style |= wxBORDER_THEME;
698
699 wxControl::Create(parent, id, pos, size, style);
700
701 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
702 }
703
704 wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
705 {
706 }
707
708 void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent& event)
709 {
710 if (event.LeftDown())
711 {
712 wxWindow* parent = GetParent();
713 while (parent != NULL && !wxDynamicCast(parent, wxDialog) && !wxDynamicCast(parent, wxFrame))
714 parent = parent->GetParent();
715
716 wxColourData data;
717 data.SetChooseFull(true);
718 data.SetColour(m_colour);
719 #if wxUSE_COLOURDLG
720 wxColourDialog *dialog = new wxColourDialog(parent, &data);
721 // Crashes on wxMac (no m_peer)
722 #ifndef __WXMAC__
723 dialog->SetTitle(_("Colour"));
724 #endif
725 if (dialog->ShowModal() == wxID_OK)
726 {
727 wxColourData retData = dialog->GetColourData();
728 m_colour = retData.GetColour();
729 SetBackgroundColour(m_colour);
730 }
731 dialog->Destroy();
732 #endif // wxUSE_COLOURDLG
733 Refresh();
734
735 wxCommandEvent event(wxEVT_BUTTON, GetId());
736 GetEventHandler()->ProcessEvent(event);
737 }
738 }
739
740 #if wxUSE_HTML
741
742 /*!
743 * wxRichTextFontListBox class declaration
744 * A listbox to display styles.
745 */
746
747 IMPLEMENT_CLASS(wxRichTextFontListBox, wxHtmlListBox)
748
749 BEGIN_EVENT_TABLE(wxRichTextFontListBox, wxHtmlListBox)
750 END_EVENT_TABLE()
751
752 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos,
753 const wxSize& size, long style)
754 {
755 Init();
756 Create(parent, id, pos, size, style);
757 }
758
759 bool wxRichTextFontListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
760 const wxSize& size, long style)
761 {
762 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
763 style |= wxBORDER_THEME;
764
765 return wxHtmlListBox::Create(parent, id, pos, size, style);
766 }
767
768 wxRichTextFontListBox::~wxRichTextFontListBox()
769 {
770 }
771
772 /// Returns the HTML for this item
773 wxString wxRichTextFontListBox::OnGetItem(size_t n) const
774 {
775 if (m_faceNames.GetCount() == 0)
776 return wxEmptyString;
777
778 wxString str = CreateHTML(m_faceNames[n]);
779 return str;
780 }
781
782 /// Get font name for index
783 wxString wxRichTextFontListBox::GetFaceName(size_t i) const
784 {
785 return m_faceNames[i];
786 }
787
788 /// Set selection for string, returning the index.
789 int wxRichTextFontListBox::SetFaceNameSelection(const wxString& name)
790 {
791 int i = m_faceNames.Index(name);
792 SetSelection(i);
793
794 return i;
795 }
796
797 /// Updates the font list
798 void wxRichTextFontListBox::UpdateFonts()
799 {
800 wxArrayString facenames = wxRichTextCtrl::GetAvailableFontNames();
801 m_faceNames = facenames;
802 m_faceNames.Sort();
803
804 SetItemCount(m_faceNames.GetCount());
805 Refresh();
806 }
807
808 #if 0
809 // Convert a colour to a 6-digit hex string
810 static wxString ColourToHexString(const wxColour& col)
811 {
812 wxString hex;
813
814 hex += wxDecToHex(col.Red());
815 hex += wxDecToHex(col.Green());
816 hex += wxDecToHex(col.Blue());
817
818 return hex;
819 }
820 #endif
821
822 /// Creates a suitable HTML fragment for a definition
823 wxString wxRichTextFontListBox::CreateHTML(const wxString& facename) const
824 {
825 wxString str = wxT("<font");
826
827 str << wxT(" size=\"+2\"");;
828
829 if (!facename.IsEmpty() && facename != _("(none)"))
830 str << wxT(" face=\"") << facename << wxT("\"");
831 /*
832 if (def->GetStyle().GetTextColour().IsOk())
833 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
834 */
835
836 str << wxT(">");
837
838 bool hasBold = false;
839
840 if (hasBold)
841 str << wxT("<b>");
842
843 str += facename;
844
845 if (hasBold)
846 str << wxT("</b>");
847
848 str << wxT("</font>");
849
850 return str;
851 }
852
853 #endif
854 // wxUSE_HTML
855
856
857 #endif
858 // wxUSE_RICHTEXT