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/checkbox.h"
34 #include "wx/button.h"
37 #include "wx/bookctrl.h"
38 #include "wx/colordlg.h"
39 #include "wx/fontenum.h"
40 #include "wx/settings.h"
41 #include "wx/module.h"
42 #include "wx/imaglist.h"
44 #include "wx/richtext/richtextformatdlg.h"
45 #include "wx/richtext/richtextctrl.h"
46 #include "wx/richtext/richtextstyles.h"
48 #include "richtextfontpage.cpp"
49 #include "richtextindentspage.cpp"
50 #include "richtexttabspage.cpp"
51 #include "richtextbulletspage.cpp"
52 #include "richtextstylepage.cpp"
55 #define wxRICHTEXT_USE_TOOLBOOK true
57 #define wxRICHTEXT_USE_TOOLBOOK false
60 IMPLEMENT_CLASS(wxRichTextFormattingDialog
, wxPropertySheetDialog
)
62 BEGIN_EVENT_TABLE(wxRichTextFormattingDialog
, wxPropertySheetDialog
)
63 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxRichTextFormattingDialog::OnTabChanged
)
66 wxRichTextFormattingDialogFactory
* wxRichTextFormattingDialog::ms_FormattingDialogFactory
= NULL
;
68 void wxRichTextFormattingDialog::Init()
71 m_styleDefinition
= NULL
;
75 wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
78 delete m_styleDefinition
;
81 bool wxRichTextFormattingDialog::Create(long flags
, wxWindow
* parent
, const wxString
& title
, wxWindowID id
,
82 const wxPoint
& pos
, const wxSize
& sz
, long style
)
84 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
|wxWS_EX_VALIDATE_RECURSIVELY
);
86 int resizeBorder
= wxRESIZE_BORDER
;
88 GetFormattingDialogFactory()->SetSheetStyle(this);
90 wxPropertySheetDialog::Create(parent
, id
, title
, pos
, sz
,
91 style
| (int)wxPlatform::IfNot(wxWinCE
, resizeBorder
)
94 GetFormattingDialogFactory()->CreateButtons(this);
95 GetFormattingDialogFactory()->CreatePages(flags
, this);
102 /// Get attributes from the given range
103 bool wxRichTextFormattingDialog::GetStyle(wxRichTextCtrl
* ctrl
, const wxRichTextRange
& range
)
105 if (ctrl
->GetBuffer().GetStyleForRange(range
.ToInternal(), m_attributes
))
106 return UpdateDisplay();
111 /// Apply attributes to the given range, only applying if necessary (wxRICHTEXT_SETSTYLE_OPTIMIZE)
112 bool wxRichTextFormattingDialog::ApplyStyle(wxRichTextCtrl
* ctrl
, const wxRichTextRange
& range
, int flags
)
114 return ctrl
->SetStyleEx(range
, m_attributes
, flags
);
117 /// Set the attributes and optionally update the display
118 bool wxRichTextFormattingDialog::SetStyle(const wxTextAttrEx
& style
, bool update
)
120 m_attributes
= style
;
126 /// Set the style definition and optionally update the display
127 bool wxRichTextFormattingDialog::SetStyleDefinition(const wxRichTextStyleDefinition
& styleDef
, wxRichTextStyleSheet
* sheet
, bool update
)
129 m_styleSheet
= sheet
;
131 if (m_styleDefinition
)
132 delete m_styleDefinition
;
133 m_styleDefinition
= styleDef
.Clone();
135 return SetStyle(m_styleDefinition
->GetStyle(), update
);
138 /// Transfers the data and from to the window
139 bool wxRichTextFormattingDialog::TransferDataToWindow()
141 if (m_styleDefinition
)
142 m_attributes
= m_styleDefinition
->GetStyle();
144 if (!wxPropertySheetDialog::TransferDataToWindow())
150 bool wxRichTextFormattingDialog::TransferDataFromWindow()
152 if (!wxPropertySheetDialog::TransferDataFromWindow())
155 if (m_styleDefinition
)
156 m_styleDefinition
->GetStyle() = m_attributes
;
161 /// Update the display
162 bool wxRichTextFormattingDialog::UpdateDisplay()
164 return TransferDataToWindow();
167 /// Apply the styles when a different tab is selected, so the previews are
169 void wxRichTextFormattingDialog::OnTabChanged(wxNotebookEvent
& event
)
171 if (GetBookCtrl() != event
.GetEventObject())
177 int oldPageId
= event
.GetOldSelection();
180 wxWindow
* page
= GetBookCtrl()->GetPage(oldPageId
);
182 page
->TransferDataFromWindow();
185 int pageId
= event
.GetSelection();
188 wxWindow
* page
= GetBookCtrl()->GetPage(pageId
);
190 page
->TransferDataToWindow();
194 void wxRichTextFormattingDialog::SetFormattingDialogFactory(wxRichTextFormattingDialogFactory
* factory
)
196 if (ms_FormattingDialogFactory
)
197 delete ms_FormattingDialogFactory
;
198 ms_FormattingDialogFactory
= factory
;
202 * Factory for formatting dialog
205 /// Create all pages, under the dialog's book control, also calling AddPage
206 bool wxRichTextFormattingDialogFactory::CreatePages(long pages
, wxRichTextFormattingDialog
* dialog
)
208 if (dialog
->GetImageList())
209 dialog
->GetBookCtrl()->SetImageList(dialog
->GetImageList());
211 int availablePageCount
= GetPageIdCount();
213 for (i
= 0; i
< availablePageCount
; i
++)
215 int pageId
= GetPageId(i
);
216 if (pageId
!= -1 && (pages
& pageId
))
219 wxPanel
* panel
= CreatePage(pageId
, title
, dialog
);
220 wxASSERT( panel
!= NULL
);
223 int imageIndex
= GetPageImage(pageId
);
224 dialog
->GetBookCtrl()->AddPage(panel
, title
, false, imageIndex
);
231 /// Create a page, given a page identifier
232 wxPanel
* wxRichTextFormattingDialogFactory::CreatePage(int page
, wxString
& title
, wxRichTextFormattingDialog
* dialog
)
234 if (page
== wxRICHTEXT_FORMAT_STYLE_EDITOR
)
236 wxRichTextStylePage
* page
= new wxRichTextStylePage(dialog
->GetBookCtrl(), wxID_ANY
);
240 else if (page
== wxRICHTEXT_FORMAT_FONT
)
242 wxRichTextFontPage
* page
= new wxRichTextFontPage(dialog
->GetBookCtrl(), wxID_ANY
);
246 else if (page
== wxRICHTEXT_FORMAT_INDENTS_SPACING
)
248 wxRichTextIndentsSpacingPage
* page
= new wxRichTextIndentsSpacingPage(dialog
->GetBookCtrl(), wxID_ANY
);
249 title
= _("Indents && Spacing");
252 else if (page
== wxRICHTEXT_FORMAT_TABS
)
254 wxRichTextTabsPage
* page
= new wxRichTextTabsPage(dialog
->GetBookCtrl(), wxID_ANY
);
258 else if (page
== wxRICHTEXT_FORMAT_BULLETS
)
260 wxRichTextBulletsPage
* page
= new wxRichTextBulletsPage(dialog
->GetBookCtrl(), wxID_ANY
);
261 title
= _("Bullets");
268 /// Enumerate all available page identifiers
269 int wxRichTextFormattingDialogFactory::GetPageId(int i
) const
272 wxRICHTEXT_FORMAT_STYLE_EDITOR
,
273 wxRICHTEXT_FORMAT_FONT
,
274 wxRICHTEXT_FORMAT_INDENTS_SPACING
,
275 wxRICHTEXT_FORMAT_BULLETS
,
276 wxRICHTEXT_FORMAT_TABS
};
284 /// Get the number of available page identifiers
285 int wxRichTextFormattingDialogFactory::GetPageIdCount() const
290 /// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
291 bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog
* dialog
)
293 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
296 int sheetStyle
= wxPROPSHEET_SHRINKTOFIT
;
298 sheetStyle
|= wxPROPSHEET_BUTTONTOOLBOOK
;
300 sheetStyle
|= wxPROPSHEET_TOOLBOOK
;
303 dialog
->SetSheetStyle(sheetStyle
);
304 dialog
->SetSheetInnerBorder(0);
305 dialog
->SetSheetOuterBorder(0);
311 /// Create the main dialog buttons
312 bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog
* dialog
)
314 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
316 // If using a toolbook, also follow Mac style and don't create buttons
317 int flags
= wxOK
|wxCANCEL
;
323 dialog
->CreateButtons(flags
);
329 * Module to initialise and clean up handlers
332 class wxRichTextFormattingDialogModule
: public wxModule
334 DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
)
336 wxRichTextFormattingDialogModule() {}
337 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory
); return true; };
338 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL
); };
341 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
, wxModule
)
344 * Font preview control
347 BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl
, wxWindow
)
348 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint
)
351 void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
355 wxSize size
= GetSize();
356 wxFont font
= GetFont();
361 // Calculate vertical and horizontal centre
364 wxString
text(_("ABCDEFGabcdefg12345"));
366 dc
.GetTextExtent( text
, &w
, &h
);
367 int cx
= wxMax(2, (size
.x
/2) - (w
/2));
368 int cy
= wxMax(2, (size
.y
/2) - (h
/2));
370 dc
.SetTextForeground(GetForegroundColour());
371 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
372 dc
.DrawText(text
, cx
, cy
);
373 dc
.DestroyClippingRegion();
377 // Helper for pages to get the top-level dialog
378 wxRichTextFormattingDialog
* wxRichTextFormattingDialog::GetDialog(wxWindow
* win
)
380 wxWindow
* p
= win
->GetParent();
381 while (p
&& !p
->IsKindOf(CLASSINFO(wxRichTextFormattingDialog
)))
383 wxRichTextFormattingDialog
* dialog
= wxDynamicCast(p
, wxRichTextFormattingDialog
);
388 // Helper for pages to get the attributes
389 wxTextAttrEx
* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow
* win
)
391 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
393 return & dialog
->GetAttributes();
398 // Helper for pages to get the style
399 wxRichTextStyleDefinition
* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow
* win
)
401 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
403 return dialog
->GetStyleDefinition();
409 * A control for displaying a small preview of a colour or bitmap
412 BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl
, wxControl
)
413 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent
)
416 IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl
, wxControl
)
418 wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
):
419 wxControl(parent
, id
, pos
, size
, style
)
421 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
422 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
425 wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
429 void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent
& event
)
431 if (event
.LeftDown())
433 wxWindow
* parent
= GetParent();
434 while (parent
!= NULL
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) && !parent
->IsKindOf(CLASSINFO(wxFrame
)))
435 parent
= parent
->GetParent();
438 data
.SetChooseFull(true);
439 data
.SetColour(m_colour
);
440 wxColourDialog
*dialog
= new wxColourDialog(parent
, &data
);
441 // Crashes on wxMac (no m_peer)
443 dialog
->SetTitle(_("Background colour"));
445 if (dialog
->ShowModal() == wxID_OK
)
447 wxColourData retData
= dialog
->GetColourData();
448 m_colour
= retData
.GetColour();
449 SetBackgroundColour(m_colour
);
454 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
455 GetEventHandler()->ProcessEvent(event
);
462 * wxRichTextFontListBox class declaration
463 * A listbox to display styles.
466 IMPLEMENT_CLASS(wxRichTextFontListBox
, wxHtmlListBox
)
468 BEGIN_EVENT_TABLE(wxRichTextFontListBox
, wxHtmlListBox
)
471 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
472 const wxSize
& size
, long style
)
475 Create(parent
, id
, pos
, size
, style
);
478 bool wxRichTextFontListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
479 const wxSize
& size
, long style
)
481 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
484 wxRichTextFontListBox::~wxRichTextFontListBox()
488 /// Returns the HTML for this item
489 wxString
wxRichTextFontListBox::OnGetItem(size_t n
) const
491 if (m_faceNames
.GetCount() == 0)
492 return wxEmptyString
;
494 wxString str
= CreateHTML(m_faceNames
[n
]);
498 /// Get font name for index
499 wxString
wxRichTextFontListBox::GetFaceName(size_t i
) const
501 return m_faceNames
[i
];
504 /// Set selection for string, returning the index.
505 int wxRichTextFontListBox::SetFaceNameSelection(const wxString
& name
)
507 int i
= m_faceNames
.Index(name
);
513 /// Updates the font list
514 void wxRichTextFontListBox::UpdateFonts()
516 wxFontEnumerator enumerator
;
517 enumerator
.EnumerateFacenames();
518 wxArrayString facenames
= enumerator
.GetFacenames();
519 m_faceNames
= facenames
;
522 SetItemCount(m_faceNames
.GetCount());
527 // Convert a colour to a 6-digit hex string
528 static wxString
ColourToHexString(const wxColour
& col
)
532 hex
+= wxDecToHex(col
.Red());
533 hex
+= wxDecToHex(col
.Green());
534 hex
+= wxDecToHex(col
.Blue());
540 /// Creates a suitable HTML fragment for a definition
541 wxString
wxRichTextFontListBox::CreateHTML(const wxString
& facename
) const
543 wxString str
= wxT("<font");
545 str
<< wxT(" size=\"+2\"");;
547 if (!facename
.IsEmpty() && facename
!= _("(none)"))
548 str
<< wxT(" face=\"") << facename
<< wxT("\"");
550 if (def->GetStyle().GetTextColour().Ok())
551 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
556 bool hasBold
= false;
566 str
<< wxT("</font>");