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"
21 #include "wx/richtext/richtextformatdlg.h"
24 #include "wx/listbox.h"
25 #include "wx/combobox.h"
26 #include "wx/textctrl.h"
28 #include "wx/stattext.h"
29 #include "wx/statline.h"
30 #include "wx/radiobut.h"
32 #include "wx/bitmap.h"
33 #include "wx/dcclient.h"
35 #include "wx/checkbox.h"
36 #include "wx/button.h"
39 #include "wx/bookctrl.h"
40 #include "wx/colordlg.h"
41 #include "wx/fontenum.h"
42 #include "wx/settings.h"
43 #include "wx/module.h"
44 #include "wx/imaglist.h"
46 #include "wx/richtext/richtextctrl.h"
47 #include "wx/richtext/richtextstyles.h"
49 #include "richtextfontpage.cpp"
50 #include "richtextindentspage.cpp"
51 #include "richtexttabspage.cpp"
52 #include "richtextbulletspage.cpp"
53 #include "richtextstylepage.cpp"
55 #if 0 // def __WXMAC__
56 #define wxRICHTEXT_USE_TOOLBOOK true
58 #define wxRICHTEXT_USE_TOOLBOOK false
61 IMPLEMENT_CLASS(wxRichTextFormattingDialog
, wxPropertySheetDialog
)
63 BEGIN_EVENT_TABLE(wxRichTextFormattingDialog
, wxPropertySheetDialog
)
64 EVT_BOOKCTRL_PAGE_CHANGED(wxID_ANY
, wxRichTextFormattingDialog::OnTabChanged
)
67 wxRichTextFormattingDialogFactory
* wxRichTextFormattingDialog::ms_FormattingDialogFactory
= NULL
;
69 void wxRichTextFormattingDialog::Init()
72 m_styleDefinition
= NULL
;
76 wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
79 delete m_styleDefinition
;
82 bool wxRichTextFormattingDialog::Create(long flags
, wxWindow
* parent
, const wxString
& title
, wxWindowID id
,
83 const wxPoint
& pos
, const wxSize
& sz
, long style
)
85 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
|wxWS_EX_VALIDATE_RECURSIVELY
);
87 int resizeBorder
= wxRESIZE_BORDER
;
89 GetFormattingDialogFactory()->SetSheetStyle(this);
91 wxPropertySheetDialog::Create(parent
, id
, title
, pos
, sz
,
92 style
| (int)wxPlatform::IfNot(wxOS_WINDOWS_CE
, resizeBorder
)
95 GetFormattingDialogFactory()->CreateButtons(this);
96 GetFormattingDialogFactory()->CreatePages(flags
, this);
103 /// Get attributes from the given range
104 bool wxRichTextFormattingDialog::GetStyle(wxRichTextCtrl
* ctrl
, const wxRichTextRange
& range
)
106 if (ctrl
->GetBuffer().GetStyleForRange(range
.ToInternal(), m_attributes
))
107 return UpdateDisplay();
112 /// Apply attributes to the given range, only applying if necessary (wxRICHTEXT_SETSTYLE_OPTIMIZE)
113 bool wxRichTextFormattingDialog::ApplyStyle(wxRichTextCtrl
* ctrl
, const wxRichTextRange
& range
, int flags
)
115 return ctrl
->SetStyleEx(range
, m_attributes
, flags
);
118 /// Set the attributes and optionally update the display
119 bool wxRichTextFormattingDialog::SetStyle(const wxTextAttrEx
& style
, bool update
)
121 m_attributes
= style
;
127 /// Set the style definition and optionally update the display
128 bool wxRichTextFormattingDialog::SetStyleDefinition(const wxRichTextStyleDefinition
& styleDef
, wxRichTextStyleSheet
* sheet
, bool update
)
130 m_styleSheet
= sheet
;
132 if (m_styleDefinition
)
133 delete m_styleDefinition
;
134 m_styleDefinition
= styleDef
.Clone();
136 return SetStyle(m_styleDefinition
->GetStyle(), update
);
139 /// Transfers the data and from to the window
140 bool wxRichTextFormattingDialog::TransferDataToWindow()
142 if (m_styleDefinition
)
143 m_attributes
= m_styleDefinition
->GetStyle();
145 if (!wxPropertySheetDialog::TransferDataToWindow())
151 bool wxRichTextFormattingDialog::TransferDataFromWindow()
153 if (!wxPropertySheetDialog::TransferDataFromWindow())
156 if (m_styleDefinition
)
157 m_styleDefinition
->GetStyle() = m_attributes
;
162 /// Update the display
163 bool wxRichTextFormattingDialog::UpdateDisplay()
165 return TransferDataToWindow();
168 /// Apply the styles when a different tab is selected, so the previews are
170 void wxRichTextFormattingDialog::OnTabChanged(wxBookCtrlEvent
& event
)
172 if (GetBookCtrl() != event
.GetEventObject())
178 int oldPageId
= event
.GetOldSelection();
181 wxWindow
* page
= GetBookCtrl()->GetPage(oldPageId
);
183 page
->TransferDataFromWindow();
186 int pageId
= event
.GetSelection();
189 wxWindow
* page
= GetBookCtrl()->GetPage(pageId
);
191 page
->TransferDataToWindow();
195 /// Respond to help command
196 void wxRichTextFormattingDialog::OnHelp(wxCommandEvent
& event
)
198 int selPage
= GetBookCtrl()->GetSelection();
199 if (selPage
!= wxNOT_FOUND
)
201 int pageId
= m_pageIds
[selPage
];
202 if (!GetFormattingDialogFactory()->ShowHelp(pageId
, this))
207 void wxRichTextFormattingDialog::SetFormattingDialogFactory(wxRichTextFormattingDialogFactory
* factory
)
209 if (ms_FormattingDialogFactory
)
210 delete ms_FormattingDialogFactory
;
211 ms_FormattingDialogFactory
= factory
;
215 * Factory for formatting dialog
218 /// Create all pages, under the dialog's book control, also calling AddPage
219 bool wxRichTextFormattingDialogFactory::CreatePages(long pages
, wxRichTextFormattingDialog
* dialog
)
221 if (dialog
->GetImageList())
222 dialog
->GetBookCtrl()->SetImageList(dialog
->GetImageList());
224 int availablePageCount
= GetPageIdCount();
226 bool selected
= false;
227 for (i
= 0; i
< availablePageCount
; i
++)
229 int pageId
= GetPageId(i
);
230 if (pageId
!= -1 && (pages
& pageId
))
233 wxPanel
* panel
= CreatePage(pageId
, title
, dialog
);
234 wxASSERT( panel
!= NULL
);
237 int imageIndex
= GetPageImage(pageId
);
238 dialog
->GetBookCtrl()->AddPage(panel
, title
, !selected
, imageIndex
);
241 dialog
->AddPageId(pageId
);
249 /// Create a page, given a page identifier
250 wxPanel
* wxRichTextFormattingDialogFactory::CreatePage(int page
, wxString
& title
, wxRichTextFormattingDialog
* dialog
)
252 if (page
== wxRICHTEXT_FORMAT_STYLE_EDITOR
)
254 wxRichTextStylePage
* page
= new wxRichTextStylePage(dialog
->GetBookCtrl(), wxID_ANY
);
258 else if (page
== wxRICHTEXT_FORMAT_FONT
)
260 wxRichTextFontPage
* page
= new wxRichTextFontPage(dialog
->GetBookCtrl(), wxID_ANY
);
264 else if (page
== wxRICHTEXT_FORMAT_INDENTS_SPACING
)
266 wxRichTextIndentsSpacingPage
* page
= new wxRichTextIndentsSpacingPage(dialog
->GetBookCtrl(), wxID_ANY
);
267 title
= _("Indents && Spacing");
270 else if (page
== wxRICHTEXT_FORMAT_TABS
)
272 wxRichTextTabsPage
* page
= new wxRichTextTabsPage(dialog
->GetBookCtrl(), wxID_ANY
);
276 else if (page
== wxRICHTEXT_FORMAT_BULLETS
)
278 wxRichTextBulletsPage
* page
= new wxRichTextBulletsPage(dialog
->GetBookCtrl(), wxID_ANY
);
279 title
= _("Bullets");
286 /// Enumerate all available page identifiers
287 int wxRichTextFormattingDialogFactory::GetPageId(int i
) const
290 wxRICHTEXT_FORMAT_STYLE_EDITOR
,
291 wxRICHTEXT_FORMAT_FONT
,
292 wxRICHTEXT_FORMAT_INDENTS_SPACING
,
293 wxRICHTEXT_FORMAT_BULLETS
,
294 wxRICHTEXT_FORMAT_TABS
};
302 /// Get the number of available page identifiers
303 int wxRichTextFormattingDialogFactory::GetPageIdCount() const
308 /// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
309 bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog
* dialog
)
311 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
314 int sheetStyle
= wxPROPSHEET_SHRINKTOFIT
;
316 sheetStyle
|= wxPROPSHEET_BUTTONTOOLBOOK
;
318 sheetStyle
|= wxPROPSHEET_TOOLBOOK
;
321 dialog
->SetSheetStyle(sheetStyle
);
322 dialog
->SetSheetInnerBorder(0);
323 dialog
->SetSheetOuterBorder(0);
329 /// Create the main dialog buttons
330 bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog
* dialog
)
332 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
334 // If using a toolbook, also follow Mac style and don't create buttons
335 int flags
= wxOK
|wxCANCEL
;
341 dialog
->CreateButtons(flags
);
347 * Module to initialise and clean up handlers
350 class wxRichTextFormattingDialogModule
: public wxModule
352 DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
)
354 wxRichTextFormattingDialogModule() {}
355 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory
); return true; };
356 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL
); };
359 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
, wxModule
)
362 * Font preview control
365 BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl
, wxWindow
)
366 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint
)
369 void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
373 wxSize size
= GetSize();
374 wxFont font
= GetFont();
379 // Calculate vertical and horizontal centre
382 wxString
text(_("ABCDEFGabcdefg12345"));
384 dc
.GetTextExtent( text
, &w
, &h
);
385 int cx
= wxMax(2, (size
.x
/2) - (w
/2));
386 int cy
= wxMax(2, (size
.y
/2) - (h
/2));
388 dc
.SetTextForeground(GetForegroundColour());
389 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
390 dc
.DrawText(text
, cx
, cy
);
391 dc
.DestroyClippingRegion();
395 // Helper for pages to get the top-level dialog
396 wxRichTextFormattingDialog
* wxRichTextFormattingDialog::GetDialog(wxWindow
* win
)
398 wxWindow
* p
= win
->GetParent();
399 while (p
&& !p
->IsKindOf(CLASSINFO(wxRichTextFormattingDialog
)))
401 wxRichTextFormattingDialog
* dialog
= wxDynamicCast(p
, wxRichTextFormattingDialog
);
406 // Helper for pages to get the attributes
407 wxTextAttrEx
* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow
* win
)
409 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
411 return & dialog
->GetAttributes();
416 // Helper for pages to get the style
417 wxRichTextStyleDefinition
* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow
* win
)
419 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
421 return dialog
->GetStyleDefinition();
427 * A control for displaying a small preview of a colour or bitmap
430 BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl
, wxControl
)
431 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent
)
434 IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl
, wxControl
)
436 wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
):
437 wxControl(parent
, id
, pos
, size
, style
)
439 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
440 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
443 wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
447 void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent
& event
)
449 if (event
.LeftDown())
451 wxWindow
* parent
= GetParent();
452 while (parent
!= NULL
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) && !parent
->IsKindOf(CLASSINFO(wxFrame
)))
453 parent
= parent
->GetParent();
456 data
.SetChooseFull(true);
457 data
.SetColour(m_colour
);
459 wxColourDialog
*dialog
= new wxColourDialog(parent
, &data
);
460 // Crashes on wxMac (no m_peer)
462 dialog
->SetTitle(_("Background colour"));
464 if (dialog
->ShowModal() == wxID_OK
)
466 wxColourData retData
= dialog
->GetColourData();
467 m_colour
= retData
.GetColour();
468 SetBackgroundColour(m_colour
);
471 #endif // wxUSE_COLOURDLG
474 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
475 GetEventHandler()->ProcessEvent(event
);
482 * wxRichTextFontListBox class declaration
483 * A listbox to display styles.
486 IMPLEMENT_CLASS(wxRichTextFontListBox
, wxHtmlListBox
)
488 BEGIN_EVENT_TABLE(wxRichTextFontListBox
, wxHtmlListBox
)
491 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
492 const wxSize
& size
, long style
)
495 Create(parent
, id
, pos
, size
, style
);
498 bool wxRichTextFontListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
499 const wxSize
& size
, long style
)
501 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
504 wxRichTextFontListBox::~wxRichTextFontListBox()
508 /// Returns the HTML for this item
509 wxString
wxRichTextFontListBox::OnGetItem(size_t n
) const
511 if (m_faceNames
.GetCount() == 0)
512 return wxEmptyString
;
514 wxString str
= CreateHTML(m_faceNames
[n
]);
518 /// Get font name for index
519 wxString
wxRichTextFontListBox::GetFaceName(size_t i
) const
521 return m_faceNames
[i
];
524 /// Set selection for string, returning the index.
525 int wxRichTextFontListBox::SetFaceNameSelection(const wxString
& name
)
527 int i
= m_faceNames
.Index(name
);
533 /// Updates the font list
534 void wxRichTextFontListBox::UpdateFonts()
536 wxFontEnumerator enumerator
;
537 enumerator
.EnumerateFacenames();
538 wxArrayString facenames
= enumerator
.GetFacenames();
539 m_faceNames
= facenames
;
542 SetItemCount(m_faceNames
.GetCount());
547 // Convert a colour to a 6-digit hex string
548 static wxString
ColourToHexString(const wxColour
& col
)
552 hex
+= wxDecToHex(col
.Red());
553 hex
+= wxDecToHex(col
.Green());
554 hex
+= wxDecToHex(col
.Blue());
560 /// Creates a suitable HTML fragment for a definition
561 wxString
wxRichTextFontListBox::CreateHTML(const wxString
& facename
) const
563 wxString str
= wxT("<font");
565 str
<< wxT(" size=\"+2\"");;
567 if (!facename
.IsEmpty() && facename
!= _("(none)"))
568 str
<< wxT(" face=\"") << facename
<< wxT("\"");
570 if (def->GetStyle().GetTextColour().Ok())
571 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
576 bool hasBold
= false;
586 str
<< wxT("</font>");