1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextformatdlg.cpp
3 // Purpose: Formatting dialog for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/listbox.h"
23 #include "wx/combobox.h"
24 #include "wx/textctrl.h"
26 #include "wx/stattext.h"
27 #include "wx/statline.h"
28 #include "wx/radiobut.h"
30 #include "wx/bitmap.h"
31 #include "wx/dcclient.h"
33 #include "wx/imaglist.h"
34 #include "wx/checkbox.h"
35 #include "wx/button.h"
40 #include "wx/bookctrl.h"
41 #include "wx/colordlg.h"
42 #include "wx/fontenum.h"
43 #include "wx/settings.h"
44 #include "wx/module.h"
46 #include "wx/richtext/richtextformatdlg.h"
47 #include "wx/richtext/richtextctrl.h"
48 #include "wx/richtext/richtextstyles.h"
50 #include "richtextfontpage.cpp"
51 #include "richtextindentspage.cpp"
52 #include "richtexttabspage.cpp"
53 #include "richtextbulletspage.cpp"
54 #include "richtextstylepage.cpp"
57 #define wxRICHTEXT_USE_TOOLBOOK true
59 #define wxRICHTEXT_USE_TOOLBOOK false
62 IMPLEMENT_CLASS(wxRichTextFormattingDialog
, wxPropertySheetDialog
)
64 BEGIN_EVENT_TABLE(wxRichTextFormattingDialog
, wxPropertySheetDialog
)
65 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxRichTextFormattingDialog::OnTabChanged
)
68 wxRichTextFormattingDialogFactory
* wxRichTextFormattingDialog::ms_FormattingDialogFactory
= NULL
;
70 void wxRichTextFormattingDialog::Init()
73 m_styleDefinition
= NULL
;
77 wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
80 delete m_styleDefinition
;
83 bool wxRichTextFormattingDialog::Create(long flags
, wxWindow
* parent
, const wxString
& title
, wxWindowID id
,
84 const wxPoint
& pos
, const wxSize
& sz
, long style
)
86 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
|wxWS_EX_VALIDATE_RECURSIVELY
);
88 int resizeBorder
= wxRESIZE_BORDER
;
90 GetFormattingDialogFactory()->SetSheetStyle(this);
92 wxPropertySheetDialog::Create(parent
, id
, title
, pos
, sz
,
93 style
| (int)wxPlatform::IfNot(wxWinCE
, resizeBorder
)
96 GetFormattingDialogFactory()->CreateButtons(this);
97 GetFormattingDialogFactory()->CreatePages(flags
, this);
104 /// Get attributes from the given range
105 bool wxRichTextFormattingDialog::GetStyle(wxRichTextCtrl
* ctrl
, const wxRichTextRange
& range
)
107 if (ctrl
->GetBuffer().GetStyleForRange(range
.ToInternal(), m_attributes
))
108 return UpdateDisplay();
113 /// Apply attributes to the given range, only applying if necessary (wxRICHTEXT_SETSTYLE_OPTIMIZE)
114 bool wxRichTextFormattingDialog::ApplyStyle(wxRichTextCtrl
* ctrl
, const wxRichTextRange
& range
, int flags
)
116 return ctrl
->SetStyleEx(range
, m_attributes
, flags
);
119 /// Set the attributes and optionally update the display
120 bool wxRichTextFormattingDialog::SetStyle(const wxTextAttrEx
& style
, bool update
)
122 m_attributes
= style
;
128 /// Set the style definition and optionally update the display
129 bool wxRichTextFormattingDialog::SetStyleDefinition(const wxRichTextStyleDefinition
& styleDef
, wxRichTextStyleSheet
* sheet
, bool update
)
131 m_styleSheet
= sheet
;
133 if (m_styleDefinition
)
134 delete m_styleDefinition
;
135 m_styleDefinition
= styleDef
.Clone();
137 return SetStyle(m_styleDefinition
->GetStyle(), update
);
140 /// Transfers the data and from to the window
141 bool wxRichTextFormattingDialog::TransferDataToWindow()
143 if (m_styleDefinition
)
144 m_attributes
= m_styleDefinition
->GetStyle();
146 if (!wxPropertySheetDialog::TransferDataToWindow())
152 bool wxRichTextFormattingDialog::TransferDataFromWindow()
154 if (!wxPropertySheetDialog::TransferDataFromWindow())
157 if (m_styleDefinition
)
158 m_styleDefinition
->GetStyle() = m_attributes
;
163 /// Update the display
164 bool wxRichTextFormattingDialog::UpdateDisplay()
166 return TransferDataToWindow();
169 /// Apply the styles when a different tab is selected, so the previews are
171 void wxRichTextFormattingDialog::OnTabChanged(wxNotebookEvent
& event
)
173 if (GetBookCtrl() != event
.GetEventObject())
179 int oldPageId
= event
.GetOldSelection();
182 wxWindow
* page
= GetBookCtrl()->GetPage(oldPageId
);
184 page
->TransferDataFromWindow();
187 int pageId
= event
.GetSelection();
190 wxWindow
* page
= GetBookCtrl()->GetPage(pageId
);
192 page
->TransferDataToWindow();
196 void wxRichTextFormattingDialog::SetFormattingDialogFactory(wxRichTextFormattingDialogFactory
* factory
)
198 if (ms_FormattingDialogFactory
)
199 delete ms_FormattingDialogFactory
;
200 ms_FormattingDialogFactory
= factory
;
204 * Factory for formatting dialog
207 /// Create all pages, under the dialog's book control, also calling AddPage
208 bool wxRichTextFormattingDialogFactory::CreatePages(long pages
, wxRichTextFormattingDialog
* dialog
)
210 if (dialog
->GetImageList())
211 dialog
->GetBookCtrl()->SetImageList(dialog
->GetImageList());
213 int availablePageCount
= GetPageIdCount();
215 for (i
= 0; i
< availablePageCount
; i
++)
217 int pageId
= GetPageId(i
);
218 if (pageId
!= -1 && (pages
& pageId
))
221 wxPanel
* panel
= CreatePage(pageId
, title
, dialog
);
222 wxASSERT( panel
!= NULL
);
225 int imageIndex
= GetPageImage(pageId
);
226 dialog
->GetBookCtrl()->AddPage(panel
, title
, false, imageIndex
);
233 /// Create a page, given a page identifier
234 wxPanel
* wxRichTextFormattingDialogFactory::CreatePage(int page
, wxString
& title
, wxRichTextFormattingDialog
* dialog
)
236 if (page
== wxRICHTEXT_FORMAT_STYLE_EDITOR
)
238 wxRichTextStylePage
* page
= new wxRichTextStylePage(dialog
->GetBookCtrl(), wxID_ANY
);
242 else if (page
== wxRICHTEXT_FORMAT_FONT
)
244 wxRichTextFontPage
* page
= new wxRichTextFontPage(dialog
->GetBookCtrl(), wxID_ANY
);
248 else if (page
== wxRICHTEXT_FORMAT_INDENTS_SPACING
)
250 wxRichTextIndentsSpacingPage
* page
= new wxRichTextIndentsSpacingPage(dialog
->GetBookCtrl(), wxID_ANY
);
251 title
= _("Indents && Spacing");
254 else if (page
== wxRICHTEXT_FORMAT_TABS
)
256 wxRichTextTabsPage
* page
= new wxRichTextTabsPage(dialog
->GetBookCtrl(), wxID_ANY
);
260 else if (page
== wxRICHTEXT_FORMAT_BULLETS
)
262 wxRichTextBulletsPage
* page
= new wxRichTextBulletsPage(dialog
->GetBookCtrl(), wxID_ANY
);
263 title
= _("Bullets");
270 /// Enumerate all available page identifiers
271 int wxRichTextFormattingDialogFactory::GetPageId(int i
) const
274 wxRICHTEXT_FORMAT_STYLE_EDITOR
,
275 wxRICHTEXT_FORMAT_FONT
,
276 wxRICHTEXT_FORMAT_INDENTS_SPACING
,
277 wxRICHTEXT_FORMAT_BULLETS
,
278 wxRICHTEXT_FORMAT_TABS
};
286 /// Get the number of available page identifiers
287 int wxRichTextFormattingDialogFactory::GetPageIdCount() const
292 /// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
293 bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog
* dialog
)
295 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
298 int sheetStyle
= wxPROPSHEET_SHRINKTOFIT
;
300 sheetStyle
|= wxPROPSHEET_BUTTONTOOLBOOK
;
302 sheetStyle
|= wxPROPSHEET_TOOLBOOK
;
305 dialog
->SetSheetStyle(sheetStyle
);
306 dialog
->SetSheetInnerBorder(0);
307 dialog
->SetSheetOuterBorder(0);
313 /// Create the main dialog buttons
314 bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog
* dialog
)
316 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
318 // If using a toolbook, also follow Mac style and don't create buttons
319 int flags
= wxOK
|wxCANCEL
;
325 dialog
->CreateButtons(flags
);
331 * Module to initialise and clean up handlers
334 class wxRichTextFormattingDialogModule
: public wxModule
336 DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
)
338 wxRichTextFormattingDialogModule() {}
339 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory
); return true; };
340 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL
); };
343 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
, wxModule
)
346 * Font preview control
349 BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl
, wxWindow
)
350 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint
)
353 void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
357 wxSize size
= GetSize();
358 wxFont font
= GetFont();
363 // Calculate vertical and horizontal centre
366 wxString
text(_("ABCDEFGabcdefg12345"));
368 dc
.GetTextExtent( text
, &w
, &h
);
369 int cx
= wxMax(2, (size
.x
/2) - (w
/2));
370 int cy
= wxMax(2, (size
.y
/2) - (h
/2));
372 dc
.SetTextForeground(GetForegroundColour());
373 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
374 dc
.DrawText(text
, cx
, cy
);
375 dc
.DestroyClippingRegion();
379 // Helper for pages to get the top-level dialog
380 wxRichTextFormattingDialog
* wxRichTextFormattingDialog::GetDialog(wxWindow
* win
)
382 wxWindow
* p
= win
->GetParent();
383 while (p
&& !p
->IsKindOf(CLASSINFO(wxRichTextFormattingDialog
)))
385 wxRichTextFormattingDialog
* dialog
= wxDynamicCast(p
, wxRichTextFormattingDialog
);
390 // Helper for pages to get the attributes
391 wxTextAttrEx
* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow
* win
)
393 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
395 return & dialog
->GetAttributes();
400 // Helper for pages to get the style
401 wxRichTextStyleDefinition
* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow
* win
)
403 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
405 return dialog
->GetStyleDefinition();
411 * A control for displaying a small preview of a colour or bitmap
414 BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl
, wxControl
)
415 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent
)
418 IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl
, wxControl
)
420 wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
):
421 wxControl(parent
, id
, pos
, size
, style
)
423 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
424 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
427 wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
431 void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent
& event
)
433 if (event
.LeftDown())
435 wxWindow
* parent
= GetParent();
436 while (parent
!= NULL
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) && !parent
->IsKindOf(CLASSINFO(wxFrame
)))
437 parent
= parent
->GetParent();
440 data
.SetChooseFull(true);
441 data
.SetColour(m_colour
);
442 wxColourDialog
*dialog
= new wxColourDialog(parent
, &data
);
443 // Crashes on wxMac (no m_peer)
445 dialog
->SetTitle(_("Background colour"));
447 if (dialog
->ShowModal() == wxID_OK
)
449 wxColourData retData
= dialog
->GetColourData();
450 m_colour
= retData
.GetColour();
451 SetBackgroundColour(m_colour
);
456 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
457 GetEventHandler()->ProcessEvent(event
);
464 * wxRichTextFontListBox class declaration
465 * A listbox to display styles.
468 IMPLEMENT_CLASS(wxRichTextFontListBox
, wxHtmlListBox
)
470 BEGIN_EVENT_TABLE(wxRichTextFontListBox
, wxHtmlListBox
)
473 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
474 const wxSize
& size
, long style
)
477 Create(parent
, id
, pos
, size
, style
);
480 bool wxRichTextFontListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
481 const wxSize
& size
, long style
)
483 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
486 wxRichTextFontListBox::~wxRichTextFontListBox()
490 /// Returns the HTML for this item
491 wxString
wxRichTextFontListBox::OnGetItem(size_t n
) const
493 if (m_faceNames
.GetCount() == 0)
494 return wxEmptyString
;
496 wxString str
= CreateHTML(m_faceNames
[n
]);
500 /// Get font name for index
501 wxString
wxRichTextFontListBox::GetFaceName(size_t i
) const
503 return m_faceNames
[i
];
506 /// Set selection for string, returning the index.
507 int wxRichTextFontListBox::SetFaceNameSelection(const wxString
& name
)
509 int i
= m_faceNames
.Index(name
);
515 /// Updates the font list
516 void wxRichTextFontListBox::UpdateFonts()
518 wxFontEnumerator enumerator
;
519 enumerator
.EnumerateFacenames();
520 wxArrayString facenames
= enumerator
.GetFacenames();
521 m_faceNames
= facenames
;
524 SetItemCount(m_faceNames
.GetCount());
529 // Convert a colour to a 6-digit hex string
530 static wxString
ColourToHexString(const wxColour
& col
)
534 hex
+= wxDecToHex(col
.Red());
535 hex
+= wxDecToHex(col
.Green());
536 hex
+= wxDecToHex(col
.Blue());
542 /// Creates a suitable HTML fragment for a definition
543 wxString
wxRichTextFontListBox::CreateHTML(const wxString
& facename
) const
545 wxString str
= wxT("<font");
547 str
<< wxT(" size=\"+2\"");;
549 if (!facename
.IsEmpty() && facename
!= _("(none)"))
550 str
<< wxT(" face=\"") << facename
<< wxT("\"");
552 if (def->GetStyle().GetTextColour().Ok())
553 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
558 bool hasBold
= false;
568 str
<< wxT("</font>");