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 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
);
445 wxColourDialog
*dialog
= new wxColourDialog(parent
, &data
);
446 // Crashes on wxMac (no m_peer)
448 dialog
->SetTitle(_("Background colour"));
450 if (dialog
->ShowModal() == wxID_OK
)
452 wxColourData retData
= dialog
->GetColourData();
453 m_colour
= retData
.GetColour();
454 SetBackgroundColour(m_colour
);
457 #endif // wxUSE_COLOURDLG
460 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
461 GetEventHandler()->ProcessEvent(event
);
468 * wxRichTextFontListBox class declaration
469 * A listbox to display styles.
472 IMPLEMENT_CLASS(wxRichTextFontListBox
, wxHtmlListBox
)
474 BEGIN_EVENT_TABLE(wxRichTextFontListBox
, wxHtmlListBox
)
477 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
478 const wxSize
& size
, long style
)
481 Create(parent
, id
, pos
, size
, style
);
484 bool wxRichTextFontListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
485 const wxSize
& size
, long style
)
487 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
490 wxRichTextFontListBox::~wxRichTextFontListBox()
494 /// Returns the HTML for this item
495 wxString
wxRichTextFontListBox::OnGetItem(size_t n
) const
497 if (m_faceNames
.GetCount() == 0)
498 return wxEmptyString
;
500 wxString str
= CreateHTML(m_faceNames
[n
]);
504 /// Get font name for index
505 wxString
wxRichTextFontListBox::GetFaceName(size_t i
) const
507 return m_faceNames
[i
];
510 /// Set selection for string, returning the index.
511 int wxRichTextFontListBox::SetFaceNameSelection(const wxString
& name
)
513 int i
= m_faceNames
.Index(name
);
519 /// Updates the font list
520 void wxRichTextFontListBox::UpdateFonts()
522 wxFontEnumerator enumerator
;
523 enumerator
.EnumerateFacenames();
524 wxArrayString facenames
= enumerator
.GetFacenames();
525 m_faceNames
= facenames
;
528 SetItemCount(m_faceNames
.GetCount());
533 // Convert a colour to a 6-digit hex string
534 static wxString
ColourToHexString(const wxColour
& col
)
538 hex
+= wxDecToHex(col
.Red());
539 hex
+= wxDecToHex(col
.Green());
540 hex
+= wxDecToHex(col
.Blue());
546 /// Creates a suitable HTML fragment for a definition
547 wxString
wxRichTextFontListBox::CreateHTML(const wxString
& facename
) const
549 wxString str
= wxT("<font");
551 str
<< wxT(" size=\"+2\"");;
553 if (!facename
.IsEmpty() && facename
!= _("(none)"))
554 str
<< wxT(" face=\"") << facename
<< wxT("\"");
556 if (def->GetStyle().GetTextColour().Ok())
557 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
562 bool hasBold
= false;
572 str
<< wxT("</font>");