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