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"
50 #include "../../src/richtext/richtextfontpage.cpp"
51 #include "../../src/richtext/richtextindentspage.cpp"
52 #include "../../src/richtext/richtexttabspage.cpp"
53 #include "../../src/richtext/richtextbulletspage.cpp"
54 #include "../../src/richtext/richtextstylepage.cpp"
56 #include "richtextfontpage.cpp"
57 #include "richtextindentspage.cpp"
58 #include "richtexttabspage.cpp"
59 #include "richtextbulletspage.cpp"
60 #include "richtextstylepage.cpp"
63 #if 0 // def __WXMAC__
64 #define wxRICHTEXT_USE_TOOLBOOK true
66 #define wxRICHTEXT_USE_TOOLBOOK false
69 IMPLEMENT_CLASS(wxRichTextFormattingDialog
, wxPropertySheetDialog
)
71 BEGIN_EVENT_TABLE(wxRichTextFormattingDialog
, wxPropertySheetDialog
)
72 EVT_BOOKCTRL_PAGE_CHANGED(wxID_ANY
, wxRichTextFormattingDialog::OnTabChanged
)
75 wxRichTextFormattingDialogFactory
* wxRichTextFormattingDialog::ms_FormattingDialogFactory
= NULL
;
77 void wxRichTextFormattingDialog::Init()
80 m_styleDefinition
= NULL
;
84 wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
87 delete m_styleDefinition
;
90 bool wxRichTextFormattingDialog::Create(long flags
, wxWindow
* parent
, const wxString
& title
, wxWindowID id
,
91 const wxPoint
& pos
, const wxSize
& sz
, long style
)
93 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
|wxWS_EX_VALIDATE_RECURSIVELY
);
95 int resizeBorder
= wxRESIZE_BORDER
;
97 GetFormattingDialogFactory()->SetSheetStyle(this);
99 wxPropertySheetDialog::Create(parent
, id
, title
, pos
, sz
,
100 style
| (int)wxPlatform::IfNot(wxOS_WINDOWS_CE
, resizeBorder
)
103 GetFormattingDialogFactory()->CreateButtons(this);
104 GetFormattingDialogFactory()->CreatePages(flags
, this);
111 /// Get attributes from the given range
112 bool wxRichTextFormattingDialog::GetStyle(wxRichTextCtrl
* ctrl
, const wxRichTextRange
& range
)
114 if (ctrl
->GetBuffer().GetStyleForRange(range
.ToInternal(), m_attributes
))
115 return UpdateDisplay();
120 /// Apply attributes to the given range, only applying if necessary (wxRICHTEXT_SETSTYLE_OPTIMIZE)
121 bool wxRichTextFormattingDialog::ApplyStyle(wxRichTextCtrl
* ctrl
, const wxRichTextRange
& range
, int flags
)
123 return ctrl
->SetStyleEx(range
, m_attributes
, flags
);
126 /// Set the attributes and optionally update the display
127 bool wxRichTextFormattingDialog::SetStyle(const wxTextAttrEx
& style
, bool update
)
129 m_attributes
= style
;
135 /// Set the style definition and optionally update the display
136 bool wxRichTextFormattingDialog::SetStyleDefinition(const wxRichTextStyleDefinition
& styleDef
, wxRichTextStyleSheet
* sheet
, bool update
)
138 m_styleSheet
= sheet
;
140 if (m_styleDefinition
)
141 delete m_styleDefinition
;
142 m_styleDefinition
= styleDef
.Clone();
144 return SetStyle(m_styleDefinition
->GetStyle(), update
);
147 /// Transfers the data and from to the window
148 bool wxRichTextFormattingDialog::TransferDataToWindow()
150 if (m_styleDefinition
)
151 m_attributes
= m_styleDefinition
->GetStyle();
153 if (!wxPropertySheetDialog::TransferDataToWindow())
159 bool wxRichTextFormattingDialog::TransferDataFromWindow()
161 if (!wxPropertySheetDialog::TransferDataFromWindow())
164 if (m_styleDefinition
)
165 m_styleDefinition
->GetStyle() = m_attributes
;
170 /// Update the display
171 bool wxRichTextFormattingDialog::UpdateDisplay()
173 return TransferDataToWindow();
176 /// Apply the styles when a different tab is selected, so the previews are
178 void wxRichTextFormattingDialog::OnTabChanged(wxBookCtrlEvent
& event
)
180 if (GetBookCtrl() != event
.GetEventObject())
186 int oldPageId
= event
.GetOldSelection();
189 wxWindow
* page
= GetBookCtrl()->GetPage(oldPageId
);
191 page
->TransferDataFromWindow();
194 int pageId
= event
.GetSelection();
197 wxWindow
* page
= GetBookCtrl()->GetPage(pageId
);
199 page
->TransferDataToWindow();
203 /// Respond to help command
204 void wxRichTextFormattingDialog::OnHelp(wxCommandEvent
& event
)
206 int selPage
= GetBookCtrl()->GetSelection();
207 if (selPage
!= wxNOT_FOUND
)
209 int pageId
= m_pageIds
[selPage
];
210 if (!GetFormattingDialogFactory()->ShowHelp(pageId
, this))
215 void wxRichTextFormattingDialog::SetFormattingDialogFactory(wxRichTextFormattingDialogFactory
* factory
)
217 if (ms_FormattingDialogFactory
)
218 delete ms_FormattingDialogFactory
;
219 ms_FormattingDialogFactory
= factory
;
223 * Factory for formatting dialog
226 /// Create all pages, under the dialog's book control, also calling AddPage
227 bool wxRichTextFormattingDialogFactory::CreatePages(long pages
, wxRichTextFormattingDialog
* dialog
)
229 if (dialog
->GetImageList())
230 dialog
->GetBookCtrl()->SetImageList(dialog
->GetImageList());
232 int availablePageCount
= GetPageIdCount();
234 bool selected
= false;
235 for (i
= 0; i
< availablePageCount
; i
++)
237 int pageId
= GetPageId(i
);
238 if (pageId
!= -1 && (pages
& pageId
))
241 wxPanel
* panel
= CreatePage(pageId
, title
, dialog
);
242 wxASSERT( panel
!= NULL
);
245 int imageIndex
= GetPageImage(pageId
);
246 dialog
->GetBookCtrl()->AddPage(panel
, title
, !selected
, imageIndex
);
249 dialog
->AddPageId(pageId
);
257 /// Create a page, given a page identifier
258 wxPanel
* wxRichTextFormattingDialogFactory::CreatePage(int page
, wxString
& title
, wxRichTextFormattingDialog
* dialog
)
260 if (page
== wxRICHTEXT_FORMAT_STYLE_EDITOR
)
262 wxRichTextStylePage
* page
= new wxRichTextStylePage(dialog
->GetBookCtrl(), wxID_ANY
);
266 else if (page
== wxRICHTEXT_FORMAT_FONT
)
268 wxRichTextFontPage
* page
= new wxRichTextFontPage(dialog
->GetBookCtrl(), wxID_ANY
);
272 else if (page
== wxRICHTEXT_FORMAT_INDENTS_SPACING
)
274 wxRichTextIndentsSpacingPage
* page
= new wxRichTextIndentsSpacingPage(dialog
->GetBookCtrl(), wxID_ANY
);
275 title
= _("Indents && Spacing");
278 else if (page
== wxRICHTEXT_FORMAT_TABS
)
280 wxRichTextTabsPage
* page
= new wxRichTextTabsPage(dialog
->GetBookCtrl(), wxID_ANY
);
284 else if (page
== wxRICHTEXT_FORMAT_BULLETS
)
286 wxRichTextBulletsPage
* page
= new wxRichTextBulletsPage(dialog
->GetBookCtrl(), wxID_ANY
);
287 title
= _("Bullets");
294 /// Enumerate all available page identifiers
295 int wxRichTextFormattingDialogFactory::GetPageId(int i
) const
298 wxRICHTEXT_FORMAT_STYLE_EDITOR
,
299 wxRICHTEXT_FORMAT_FONT
,
300 wxRICHTEXT_FORMAT_INDENTS_SPACING
,
301 wxRICHTEXT_FORMAT_BULLETS
,
302 wxRICHTEXT_FORMAT_TABS
};
310 /// Get the number of available page identifiers
311 int wxRichTextFormattingDialogFactory::GetPageIdCount() const
316 /// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
317 bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog
* dialog
)
319 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
322 int sheetStyle
= wxPROPSHEET_SHRINKTOFIT
;
324 sheetStyle
|= wxPROPSHEET_BUTTONTOOLBOOK
;
326 sheetStyle
|= wxPROPSHEET_TOOLBOOK
;
329 dialog
->SetSheetStyle(sheetStyle
);
330 dialog
->SetSheetInnerBorder(0);
331 dialog
->SetSheetOuterBorder(0);
337 /// Create the main dialog buttons
338 bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog
* dialog
)
340 bool useToolBook
= wxRICHTEXT_USE_TOOLBOOK
;
342 // If using a toolbook, also follow Mac style and don't create buttons
343 int flags
= wxOK
|wxCANCEL
;
349 dialog
->CreateButtons(flags
);
355 * Module to initialise and clean up handlers
358 class wxRichTextFormattingDialogModule
: public wxModule
360 DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
)
362 wxRichTextFormattingDialogModule() {}
363 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory
); return true; };
364 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL
); };
367 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule
, wxModule
)
370 * Font preview control
373 BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl
, wxWindow
)
374 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint
)
377 void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
381 wxSize size
= GetSize();
382 wxFont font
= GetFont();
387 // Calculate vertical and horizontal centre
390 wxString
text(_("ABCDEFGabcdefg12345"));
392 dc
.GetTextExtent( text
, &w
, &h
);
393 int cx
= wxMax(2, (size
.x
/2) - (w
/2));
394 int cy
= wxMax(2, (size
.y
/2) - (h
/2));
396 dc
.SetTextForeground(GetForegroundColour());
397 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
398 dc
.DrawText(text
, cx
, cy
);
399 dc
.DestroyClippingRegion();
403 // Helper for pages to get the top-level dialog
404 wxRichTextFormattingDialog
* wxRichTextFormattingDialog::GetDialog(wxWindow
* win
)
406 wxWindow
* p
= win
->GetParent();
407 while (p
&& !p
->IsKindOf(CLASSINFO(wxRichTextFormattingDialog
)))
409 wxRichTextFormattingDialog
* dialog
= wxDynamicCast(p
, wxRichTextFormattingDialog
);
414 // Helper for pages to get the attributes
415 wxTextAttrEx
* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow
* win
)
417 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
419 return & dialog
->GetAttributes();
424 // Helper for pages to get the style
425 wxRichTextStyleDefinition
* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow
* win
)
427 wxRichTextFormattingDialog
* dialog
= GetDialog(win
);
429 return dialog
->GetStyleDefinition();
435 * A control for displaying a small preview of a colour or bitmap
438 BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl
, wxControl
)
439 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent
)
442 IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl
, wxControl
)
444 wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
):
445 wxControl(parent
, id
, pos
, size
, style
)
447 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
448 SetBackgroundStyle(wxBG_STYLE_COLOUR
);
451 wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
455 void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent
& event
)
457 if (event
.LeftDown())
459 wxWindow
* parent
= GetParent();
460 while (parent
!= NULL
&& !parent
->IsKindOf(CLASSINFO(wxDialog
)) && !parent
->IsKindOf(CLASSINFO(wxFrame
)))
461 parent
= parent
->GetParent();
464 data
.SetChooseFull(true);
465 data
.SetColour(m_colour
);
467 wxColourDialog
*dialog
= new wxColourDialog(parent
, &data
);
468 // Crashes on wxMac (no m_peer)
470 dialog
->SetTitle(_("Background colour"));
472 if (dialog
->ShowModal() == wxID_OK
)
474 wxColourData retData
= dialog
->GetColourData();
475 m_colour
= retData
.GetColour();
476 SetBackgroundColour(m_colour
);
479 #endif // wxUSE_COLOURDLG
482 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
483 GetEventHandler()->ProcessEvent(event
);
490 * wxRichTextFontListBox class declaration
491 * A listbox to display styles.
494 IMPLEMENT_CLASS(wxRichTextFontListBox
, wxHtmlListBox
)
496 BEGIN_EVENT_TABLE(wxRichTextFontListBox
, wxHtmlListBox
)
499 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
500 const wxSize
& size
, long style
)
503 Create(parent
, id
, pos
, size
, style
);
506 bool wxRichTextFontListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
507 const wxSize
& size
, long style
)
509 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
512 wxRichTextFontListBox::~wxRichTextFontListBox()
516 /// Returns the HTML for this item
517 wxString
wxRichTextFontListBox::OnGetItem(size_t n
) const
519 if (m_faceNames
.GetCount() == 0)
520 return wxEmptyString
;
522 wxString str
= CreateHTML(m_faceNames
[n
]);
526 /// Get font name for index
527 wxString
wxRichTextFontListBox::GetFaceName(size_t i
) const
529 return m_faceNames
[i
];
532 /// Set selection for string, returning the index.
533 int wxRichTextFontListBox::SetFaceNameSelection(const wxString
& name
)
535 int i
= m_faceNames
.Index(name
);
541 /// Updates the font list
542 void wxRichTextFontListBox::UpdateFonts()
544 wxFontEnumerator enumerator
;
545 enumerator
.EnumerateFacenames();
546 wxArrayString facenames
= enumerator
.GetFacenames();
547 m_faceNames
= facenames
;
550 SetItemCount(m_faceNames
.GetCount());
555 // Convert a colour to a 6-digit hex string
556 static wxString
ColourToHexString(const wxColour
& col
)
560 hex
+= wxDecToHex(col
.Red());
561 hex
+= wxDecToHex(col
.Green());
562 hex
+= wxDecToHex(col
.Blue());
568 /// Creates a suitable HTML fragment for a definition
569 wxString
wxRichTextFontListBox::CreateHTML(const wxString
& facename
) const
571 wxString str
= wxT("<font");
573 str
<< wxT(" size=\"+2\"");;
575 if (!facename
.IsEmpty() && facename
!= _("(none)"))
576 str
<< wxT(" face=\"") << facename
<< wxT("\"");
578 if (def->GetStyle().GetTextColour().Ok())
579 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
584 bool hasBold
= false;
594 str
<< wxT("</font>");