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