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