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_NOTEBOOK_PAGE_CHANGED(-1, 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(wxNotebookEvent
& 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 void wxRichTextFormattingDialog::SetFormattingDialogFactory(wxRichTextFormattingDialogFactory
* factory
)
197 if (ms_FormattingDialogFactory
)
198 delete ms_FormattingDialogFactory
;
199 ms_FormattingDialogFactory
= factory
;
203 * Factory for formatting dialog
206 /// Create all pages, under the dialog's book control, also calling AddPage
207 bool wxRichTextFormattingDialogFactory::CreatePages(long pages
, wxRichTextFormattingDialog
* dialog
)
209 if (dialog
->GetImageList())
210 dialog
->GetBookCtrl()->SetImageList(dialog
->GetImageList());
212 int availablePageCount
= GetPageIdCount();
214 bool selected
= false;
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
, !selected
, imageIndex
);
235 /// Create a page, given a page identifier
236 wxPanel
* wxRichTextFormattingDialogFactory::CreatePage(int page
, wxString
& title
, wxRichTextFormattingDialog
* dialog
)
238 if (page
== wxRICHTEXT_FORMAT_STYLE_EDITOR
)
240 wxRichTextStylePage
* page
= new wxRichTextStylePage(dialog
->GetBookCtrl(), wxID_ANY
);
244 else if (page
== wxRICHTEXT_FORMAT_FONT
)
246 wxRichTextFontPage
* page
= new wxRichTextFontPage(dialog
->GetBookCtrl(), wxID_ANY
);
250 else if (page
== wxRICHTEXT_FORMAT_INDENTS_SPACING
)
252 wxRichTextIndentsSpacingPage
* page
= new wxRichTextIndentsSpacingPage(dialog
->GetBookCtrl(), wxID_ANY
);
253 title
= _("Indents && Spacing");
256 else if (page
== wxRICHTEXT_FORMAT_TABS
)
258 wxRichTextTabsPage
* page
= new wxRichTextTabsPage(dialog
->GetBookCtrl(), wxID_ANY
);
262 else if (page
== wxRICHTEXT_FORMAT_BULLETS
)
264 wxRichTextBulletsPage
* page
= new wxRichTextBulletsPage(dialog
->GetBookCtrl(), wxID_ANY
);
265 title
= _("Bullets");
272 /// Enumerate all available page identifiers
273 int wxRichTextFormattingDialogFactory::GetPageId(int i
) const
276 wxRICHTEXT_FORMAT_STYLE_EDITOR
,
277 wxRICHTEXT_FORMAT_FONT
,
278 wxRICHTEXT_FORMAT_INDENTS_SPACING
,
279 wxRICHTEXT_FORMAT_BULLETS
,
280 wxRICHTEXT_FORMAT_TABS
};
288 /// Get the number of available page identifiers
289 int wxRichTextFormattingDialogFactory::GetPageIdCount() const
294 /// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
295 bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog
* dialog
)
297 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
300 int sheetStyle
= wxPROPSHEET_SHRINKTOFIT
;
302 sheetStyle
|= wxPROPSHEET_BUTTONTOOLBOOK
;
304 sheetStyle
|= wxPROPSHEET_TOOLBOOK
;
307 dialog
->SetSheetStyle(sheetStyle
);
308 dialog
->SetSheetInnerBorder(0);
309 dialog
->SetSheetOuterBorder(0);
315 /// Create the main dialog buttons
316 bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog
* dialog
)
318 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
320 // If using a toolbook, also follow Mac style and don't create buttons
321 int flags
= wxOK
|wxCANCEL
;
327 dialog
->CreateButtons(flags
);
333 * Module to initialise and clean up handlers
336 class wxRichTextFormattingDialogModule
: public wxModule
338 DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
)
340 wxRichTextFormattingDialogModule() {}
341 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory
); return true; };
342 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL
); };
345 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
, wxModule
)
348 * Font preview control
351 BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl
, wxWindow
)
352 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint
)
355 void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
359 wxSize size
= GetSize();
360 wxFont font
= GetFont();
365 // Calculate vertical and horizontal centre
368 wxString
text(_("ABCDEFGabcdefg12345"));
370 dc
.GetTextExtent( text
, &w
, &h
);
371 int cx
= wxMax(2, (size
.x
/2) - (w
/2));
372 int cy
= wxMax(2, (size
.y
/2) - (h
/2));
374 dc
.SetTextForeground(GetForegroundColour());
375 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
376 dc
.DrawText(text
, cx
, cy
);
377 dc
.DestroyClippingRegion();
381 // Helper for pages to get the top-level dialog
382 wxRichTextFormattingDialog
* wxRichTextFormattingDialog::GetDialog(wxWindow
* win
)
384 wxWindow
* p
= win
->GetParent();
385 while (p
&& !p
->IsKindOf(CLASSINFO(wxRichTextFormattingDialog
)))
387 wxRichTextFormattingDialog
* dialog
= wxDynamicCast(p
, wxRichTextFormattingDialog
);
392 // Helper for pages to get the attributes
393 wxTextAttrEx
* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow
* win
)
395 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
397 return & dialog
->GetAttributes();
402 // Helper for pages to get the style
403 wxRichTextStyleDefinition
* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow
* win
)
405 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
407 return dialog
->GetStyleDefinition();
413 * A control for displaying a small preview of a colour or bitmap
416 BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl
, wxControl
)
417 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent
)
420 IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl
, wxControl
)
422 wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
):
423 wxControl(parent
, id
, pos
, size
, style
)
425 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
426 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
429 wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
433 void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent
& event
)
435 if (event
.LeftDown())
437 wxWindow
* parent
= GetParent();
438 while (parent
!= NULL
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) && !parent
->IsKindOf(CLASSINFO(wxFrame
)))
439 parent
= parent
->GetParent();
442 data
.SetChooseFull(true);
443 data
.SetColour(m_colour
);
444 wxColourDialog
*dialog
= new wxColourDialog(parent
, &data
);
445 // Crashes on wxMac (no m_peer)
447 dialog
->SetTitle(_("Background colour"));
449 if (dialog
->ShowModal() == wxID_OK
)
451 wxColourData retData
= dialog
->GetColourData();
452 m_colour
= retData
.GetColour();
453 SetBackgroundColour(m_colour
);
458 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
459 GetEventHandler()->ProcessEvent(event
);
466 * wxRichTextFontListBox class declaration
467 * A listbox to display styles.
470 IMPLEMENT_CLASS(wxRichTextFontListBox
, wxHtmlListBox
)
472 BEGIN_EVENT_TABLE(wxRichTextFontListBox
, wxHtmlListBox
)
475 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
476 const wxSize
& size
, long style
)
479 Create(parent
, id
, pos
, size
, style
);
482 bool wxRichTextFontListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
483 const wxSize
& size
, long style
)
485 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
488 wxRichTextFontListBox::~wxRichTextFontListBox()
492 /// Returns the HTML for this item
493 wxString
wxRichTextFontListBox::OnGetItem(size_t n
) const
495 if (m_faceNames
.GetCount() == 0)
496 return wxEmptyString
;
498 wxString str
= CreateHTML(m_faceNames
[n
]);
502 /// Get font name for index
503 wxString
wxRichTextFontListBox::GetFaceName(size_t i
) const
505 return m_faceNames
[i
];
508 /// Set selection for string, returning the index.
509 int wxRichTextFontListBox::SetFaceNameSelection(const wxString
& name
)
511 int i
= m_faceNames
.Index(name
);
517 /// Updates the font list
518 void wxRichTextFontListBox::UpdateFonts()
520 wxFontEnumerator enumerator
;
521 enumerator
.EnumerateFacenames();
522 wxArrayString facenames
= enumerator
.GetFacenames();
523 m_faceNames
= facenames
;
526 SetItemCount(m_faceNames
.GetCount());
531 // Convert a colour to a 6-digit hex string
532 static wxString
ColourToHexString(const wxColour
& col
)
536 hex
+= wxDecToHex(col
.Red());
537 hex
+= wxDecToHex(col
.Green());
538 hex
+= wxDecToHex(col
.Blue());
544 /// Creates a suitable HTML fragment for a definition
545 wxString
wxRichTextFontListBox::CreateHTML(const wxString
& facename
) const
547 wxString str
= wxT("<font");
549 str
<< wxT(" size=\"+2\"");;
551 if (!facename
.IsEmpty() && facename
!= _("(none)"))
552 str
<< wxT(" face=\"") << facename
<< wxT("\"");
554 if (def->GetStyle().GetTextColour().Ok())
555 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
560 bool hasBold
= false;
570 str
<< wxT("</font>");