1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextstyles.cpp
3 // Purpose: Style management 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/richtextstyles.h"
27 #include "wx/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/wfstream.h"
30 #include "wx/settings.h"
32 #include "wx/richtext/richtextctrl.h"
34 IMPLEMENT_CLASS(wxRichTextStyleDefinition
, wxObject
)
35 IMPLEMENT_CLASS(wxRichTextCharacterStyleDefinition
, wxRichTextStyleDefinition
)
36 IMPLEMENT_CLASS(wxRichTextParagraphStyleDefinition
, wxRichTextStyleDefinition
)
37 IMPLEMENT_CLASS(wxRichTextListStyleDefinition
, wxRichTextParagraphStyleDefinition
)
43 void wxRichTextStyleDefinition::Copy(const wxRichTextStyleDefinition
& def
)
46 m_baseStyle
= def
.m_baseStyle
;
47 m_style
= def
.m_style
;
50 bool wxRichTextStyleDefinition::Eq(const wxRichTextStyleDefinition
& def
) const
52 return (m_name
== def
.m_name
&& m_baseStyle
== def
.m_baseStyle
&& m_style
== def
.m_style
);
56 * Paragraph style definition
59 void wxRichTextParagraphStyleDefinition::Copy(const wxRichTextParagraphStyleDefinition
& def
)
61 wxRichTextStyleDefinition::Copy(def
);
63 m_nextStyle
= def
.m_nextStyle
;
66 bool wxRichTextParagraphStyleDefinition::operator ==(const wxRichTextParagraphStyleDefinition
& def
) const
68 return (Eq(def
) && m_nextStyle
== def
.m_nextStyle
);
72 * List style definition
75 void wxRichTextListStyleDefinition::Copy(const wxRichTextListStyleDefinition
& def
)
77 wxRichTextParagraphStyleDefinition::Copy(def
);
80 for (i
= 0; i
< 10; i
++)
81 m_levelStyles
[i
] = def
.m_levelStyles
[i
];
84 bool wxRichTextListStyleDefinition::operator ==(const wxRichTextListStyleDefinition
& def
) const
89 for (i
= 0; i
< 10; i
++)
90 if (!(m_levelStyles
[i
] == def
.m_levelStyles
[i
]))
96 /// Sets/gets the attributes for the given level
97 void wxRichTextListStyleDefinition::SetLevelAttributes(int i
, const wxTextAttrEx
& attr
)
99 wxASSERT( (i
>= 0 && i
< 10) );
100 if (i
>= 0 && i
< 10)
101 m_levelStyles
[i
] = attr
;
104 const wxTextAttrEx
* wxRichTextListStyleDefinition::GetLevelAttributes(int i
) const
106 wxASSERT( (i
>= 0 && i
< 10) );
107 if (i
>= 0 && i
< 10)
108 return & m_levelStyles
[i
];
113 wxTextAttrEx
* wxRichTextListStyleDefinition::GetLevelAttributes(int i
)
115 wxASSERT( (i
>= 0 && i
< 10) );
116 if (i
>= 0 && i
< 10)
117 return & m_levelStyles
[i
];
122 /// Convenience function for setting the major attributes for a list level specification
123 void wxRichTextListStyleDefinition::SetAttributes(int i
, int leftIndent
, int leftSubIndent
, int bulletStyle
, const wxString
& bulletSymbol
)
125 wxASSERT( (i
>= 0 && i
< 10) );
126 if (i
>= 0 && i
< 10)
130 attr
.SetBulletStyle(bulletStyle
);
131 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
133 if (!bulletSymbol
.IsEmpty())
134 attr
.SetBulletSymbol(bulletSymbol
[0]);
136 m_levelStyles
[i
] = attr
;
140 /// Finds the level corresponding to the given indentation
141 int wxRichTextListStyleDefinition::FindLevelForIndent(int indent
) const
144 for (i
= 0; i
< 10; i
++)
146 if (indent
< m_levelStyles
[i
].GetLeftIndent())
157 /// Combine the list style with a paragraph style, using the given indent (from which
158 /// an appropriate level is found)
159 wxTextAttrEx
wxRichTextListStyleDefinition::CombineWithParagraphStyle(int indent
, const wxTextAttrEx
& paraStyle
)
161 int listLevel
= FindLevelForIndent(indent
);
163 wxTextAttrEx
attr(*GetLevelAttributes(listLevel
));
164 int oldLeftIndent
= attr
.GetLeftIndent();
165 int oldLeftSubIndent
= attr
.GetLeftSubIndent();
167 // First apply the overall paragraph style, if any
168 wxRichTextApplyStyle(attr
, GetStyle());
170 // Then apply paragraph style, e.g. from paragraph style definition
171 wxRichTextApplyStyle(attr
, paraStyle
);
173 // We override the indents according to the list definition
174 attr
.SetLeftIndent(oldLeftIndent
, oldLeftSubIndent
);
179 /// Combine the base and list style, using the given indent (from which
180 /// an appropriate level is found)
181 wxTextAttrEx
wxRichTextListStyleDefinition::GetCombinedStyle(int indent
)
183 int listLevel
= FindLevelForIndent(indent
);
184 return GetCombinedStyleForLevel(listLevel
);
187 /// Combine the base and list style, using the given indent (from which
188 /// an appropriate level is found)
189 wxTextAttrEx
wxRichTextListStyleDefinition::GetCombinedStyleForLevel(int listLevel
)
191 wxTextAttrEx
attr(*GetLevelAttributes(listLevel
));
192 int oldLeftIndent
= attr
.GetLeftIndent();
193 int oldLeftSubIndent
= attr
.GetLeftSubIndent();
195 // Apply the overall paragraph style, if any
196 wxRichTextApplyStyle(attr
, GetStyle());
198 // We override the indents according to the list definition
199 attr
.SetLeftIndent(oldLeftIndent
, oldLeftSubIndent
);
204 /// Is this a numbered list?
205 bool wxRichTextListStyleDefinition::IsNumbered(int i
) const
207 return (0 != (GetLevelAttributes(i
)->GetFlags() &
208 (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
|wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
|
209 wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
|wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)));
216 IMPLEMENT_CLASS(wxRichTextStyleSheet
, wxObject
)
218 wxRichTextStyleSheet::~wxRichTextStyleSheet()
223 m_nextSheet
->m_previousSheet
= m_previousSheet
;
226 m_previousSheet
->m_nextSheet
= m_nextSheet
;
228 m_previousSheet
= NULL
;
233 void wxRichTextStyleSheet::Init()
235 m_previousSheet
= NULL
;
239 /// Add a definition to one of the style lists
240 bool wxRichTextStyleSheet::AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
)
248 bool wxRichTextStyleSheet::RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
)
250 wxList::compatibility_iterator node
= list
.Find(def
);
253 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
263 /// Find a definition by name
264 wxRichTextStyleDefinition
* wxRichTextStyleSheet::FindStyle(const wxList
& list
, const wxString
& name
, bool recurse
) const
266 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
268 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
269 if (def
->GetName().Lower() == name
.Lower())
273 if (m_nextSheet
&& recurse
)
274 return m_nextSheet
->FindStyle(list
, name
, recurse
);
279 /// Delete all styles
280 void wxRichTextStyleSheet::DeleteStyles()
282 WX_CLEAR_LIST(wxList
, m_characterStyleDefinitions
);
283 WX_CLEAR_LIST(wxList
, m_paragraphStyleDefinitions
);
284 WX_CLEAR_LIST(wxList
, m_listStyleDefinitions
);
287 /// Insert into list of style sheets
288 bool wxRichTextStyleSheet::InsertSheet(wxRichTextStyleSheet
* before
)
290 m_previousSheet
= before
->m_previousSheet
;
291 m_nextSheet
= before
;
293 before
->m_previousSheet
= this;
297 /// Append to list of style sheets
298 bool wxRichTextStyleSheet::AppendSheet(wxRichTextStyleSheet
* after
)
300 wxRichTextStyleSheet
* last
= after
;
301 while (last
&& last
->m_nextSheet
)
303 last
= last
->m_nextSheet
;
308 m_previousSheet
= last
;
309 last
->m_nextSheet
= this;
317 /// Unlink from the list of style sheets
318 void wxRichTextStyleSheet::Unlink()
321 m_previousSheet
->m_nextSheet
= m_nextSheet
;
323 m_nextSheet
->m_previousSheet
= m_previousSheet
;
325 m_previousSheet
= NULL
;
329 /// Add a definition to the character style list
330 bool wxRichTextStyleSheet::AddCharacterStyle(wxRichTextCharacterStyleDefinition
* def
)
332 def
->GetStyle().SetCharacterStyleName(def
->GetName());
333 return AddStyle(m_characterStyleDefinitions
, def
);
336 /// Add a definition to the paragraph style list
337 bool wxRichTextStyleSheet::AddParagraphStyle(wxRichTextParagraphStyleDefinition
* def
)
339 def
->GetStyle().SetParagraphStyleName(def
->GetName());
340 return AddStyle(m_paragraphStyleDefinitions
, def
);
343 /// Add a definition to the list style list
344 bool wxRichTextStyleSheet::AddListStyle(wxRichTextListStyleDefinition
* def
)
346 def
->GetStyle().SetListStyleName(def
->GetName());
347 return AddStyle(m_listStyleDefinitions
, def
);
351 void wxRichTextStyleSheet::Copy(const wxRichTextStyleSheet
& sheet
)
355 wxList::compatibility_iterator node
;
357 for (node
= sheet
.m_characterStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
359 wxRichTextCharacterStyleDefinition
* def
= (wxRichTextCharacterStyleDefinition
*) node
->GetData();
360 AddCharacterStyle(new wxRichTextCharacterStyleDefinition(*def
));
363 for (node
= sheet
.m_paragraphStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
365 wxRichTextParagraphStyleDefinition
* def
= (wxRichTextParagraphStyleDefinition
*) node
->GetData();
366 AddParagraphStyle(new wxRichTextParagraphStyleDefinition(*def
));
369 for (node
= sheet
.m_listStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
371 wxRichTextListStyleDefinition
* def
= (wxRichTextListStyleDefinition
*) node
->GetData();
372 AddListStyle(new wxRichTextListStyleDefinition(*def
));
377 bool wxRichTextStyleSheet::operator==(const wxRichTextStyleSheet
& WXUNUSED(sheet
)) const
386 * wxRichTextStyleListBox: a listbox to display styles.
389 IMPLEMENT_CLASS(wxRichTextStyleListBox
, wxHtmlListBox
)
391 BEGIN_EVENT_TABLE(wxRichTextStyleListBox
, wxHtmlListBox
)
392 EVT_LEFT_DOWN(wxRichTextStyleListBox::OnLeftDown
)
393 EVT_LEFT_DCLICK(wxRichTextStyleListBox::OnLeftDoubleClick
)
394 EVT_IDLE(wxRichTextStyleListBox::OnIdle
)
397 wxRichTextStyleListBox::wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
398 const wxSize
& size
, long style
)
401 Create(parent
, id
, pos
, size
, style
);
404 bool wxRichTextStyleListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
405 const wxSize
& size
, long style
)
407 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
410 wxRichTextStyleListBox::~wxRichTextStyleListBox()
414 /// Returns the HTML for this item
415 wxString
wxRichTextStyleListBox::OnGetItem(size_t n
) const
417 if (!GetStyleSheet())
418 return wxEmptyString
;
420 wxRichTextStyleDefinition
* def
= GetStyle(n
);
422 return CreateHTML(def
);
424 return wxEmptyString
;
427 // Get style for index
428 wxRichTextStyleDefinition
* wxRichTextStyleListBox::GetStyle(size_t i
) const
430 if (!GetStyleSheet())
433 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
)
435 // First paragraph styles, then character, then list
436 if (i
< GetStyleSheet()->GetParagraphStyleCount())
437 return GetStyleSheet()->GetParagraphStyle(i
);
439 if ((i
- GetStyleSheet()->GetParagraphStyleCount()) < GetStyleSheet()->GetCharacterStyleCount())
440 return GetStyleSheet()->GetCharacterStyle(i
- GetStyleSheet()->GetParagraphStyleCount());
442 if ((i
- GetStyleSheet()->GetParagraphStyleCount() - GetStyleSheet()->GetCharacterStyleCount()) < GetStyleSheet()->GetListStyleCount())
443 return GetStyleSheet()->GetListStyle(i
- GetStyleSheet()->GetParagraphStyleCount() - GetStyleSheet()->GetCharacterStyleCount());
445 else if ((GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH
) && (i
< GetStyleSheet()->GetParagraphStyleCount()))
447 return GetStyleSheet()->GetParagraphStyle(i
);
449 else if ((GetStyleType() == wxRICHTEXT_STYLE_CHARACTER
) && (i
< GetStyleSheet()->GetCharacterStyleCount()))
451 return GetStyleSheet()->GetCharacterStyle(i
);
453 else if ((GetStyleType() == wxRICHTEXT_STYLE_LIST
) && (i
< GetStyleSheet()->GetListStyleCount()))
455 return GetStyleSheet()->GetListStyle(i
);
462 void wxRichTextStyleListBox::UpdateStyles()
466 SetSelection(wxNOT_FOUND
);
468 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
)
469 SetItemCount(GetStyleSheet()->GetParagraphStyleCount()+GetStyleSheet()->GetCharacterStyleCount()+GetStyleSheet()->GetListStyleCount());
470 else if (GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH
)
471 SetItemCount(GetStyleSheet()->GetParagraphStyleCount());
472 else if (GetStyleType() == wxRICHTEXT_STYLE_CHARACTER
)
473 SetItemCount(GetStyleSheet()->GetCharacterStyleCount());
474 else if (GetStyleType() == wxRICHTEXT_STYLE_LIST
)
475 SetItemCount(GetStyleSheet()->GetListStyleCount());
479 if (GetItemCount() > 0)
487 // Get index for style name
488 int wxRichTextStyleListBox::GetIndexForStyle(const wxString
& name
) const
492 int count
= GetItemCount();
495 for (i
= 0; i
< (int) count
; i
++)
497 wxRichTextStyleDefinition
* def
= GetStyle(i
);
498 if (def
->GetName() == name
)
505 /// Set selection for string
506 int wxRichTextStyleListBox::SetStyleSelection(const wxString
& name
)
508 int i
= GetIndexForStyle(name
);
514 // Convert a colour to a 6-digit hex string
515 static wxString
ColourToHexString(const wxColour
& col
)
519 hex
+= wxDecToHex(col
.Red());
520 hex
+= wxDecToHex(col
.Green());
521 hex
+= wxDecToHex(col
.Blue());
526 /// Creates a suitable HTML fragment for a definition
527 wxString
wxRichTextStyleListBox::CreateHTML(wxRichTextStyleDefinition
* def
) const
529 // TODO: indicate list format for list style types
531 wxString
str(wxT("<table><tr>"));
533 if (def
->GetStyle().GetLeftIndent() > 0)
535 wxClientDC
dc((wxWindow
*) this);
537 str
<< wxT("<td width=") << (ConvertTenthsMMToPixels(dc
, def
->GetStyle().GetLeftIndent())/2) << wxT("></td>");
540 str
<< wxT("<td nowrap>");
548 int stdFontSize
= 12;
549 int thisFontSize
= ((def
->GetStyle().GetFlags() & wxTEXT_ATTR_FONT_SIZE
) != 0) ? def
->GetStyle().GetFontSize() : stdFontSize
;
551 if (thisFontSize
< stdFontSize
)
553 else if (thisFontSize
> stdFontSize
)
558 str
<< wxT(" size=") << size
;
560 if (!def
->GetStyle().GetFontFaceName().IsEmpty())
561 str
<< wxT(" face=\"") << def
->GetStyle().GetFontFaceName() << wxT("\"");
563 if (def
->GetStyle().GetTextColour().Ok())
564 str
<< wxT(" color=\"#") << ColourToHexString(def
->GetStyle().GetTextColour()) << wxT("\"");
568 bool hasBold
= false;
569 bool hasItalic
= false;
570 bool hasUnderline
= false;
572 if (def
->GetStyle().GetFontWeight() == wxBOLD
)
574 if (def
->GetStyle().GetFontStyle() == wxITALIC
)
576 if (def
->GetStyle().GetFontUnderlined())
586 str
+= def
->GetName();
595 str
<< wxT("</font>");
597 str
+= wxT("</td></tr></table>");
601 // Convert units in tends of a millimetre to device units
602 int wxRichTextStyleListBox::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const
604 int ppi
= dc
.GetPPI().x
;
606 // There are ppi pixels in 254.1 "1/10 mm"
608 double pixels
= ((double) units
* (double)ppi
) / 254.1;
613 void wxRichTextStyleListBox::OnLeftDown(wxMouseEvent
& event
)
615 wxVListBox::OnLeftDown(event
);
617 int item
= HitTest(event
.GetPosition());
618 if (item
!= wxNOT_FOUND
&& GetApplyOnSelection())
622 void wxRichTextStyleListBox::OnLeftDoubleClick(wxMouseEvent
& event
)
624 wxVListBox::OnLeftDown(event
);
626 int item
= HitTest(event
.GetPosition());
627 if (item
!= wxNOT_FOUND
&& !GetApplyOnSelection())
631 /// Helper for listbox and combo control
632 wxString
wxRichTextStyleListBox::GetStyleToShowInIdleTime(wxRichTextCtrl
* ctrl
, wxRichTextStyleType styleType
)
634 int adjustedCaretPos
= ctrl
->GetAdjustedCaretPosition(ctrl
->GetCaretPosition());
636 wxRichTextParagraph
* para
= ctrl
->GetBuffer().GetParagraphAtPosition(adjustedCaretPos
);
637 wxRichTextObject
* obj
= ctrl
->GetBuffer().GetLeafObjectAtPosition(adjustedCaretPos
);
641 // Take into account current default style just chosen by user
642 if (ctrl
->IsDefaultStyleShowing())
644 if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_CHARACTER
) &&
645 !ctrl
->GetDefaultStyleEx().GetCharacterStyleName().IsEmpty())
646 styleName
= ctrl
->GetDefaultStyleEx().GetCharacterStyleName();
647 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_PARAGRAPH
) &&
648 !ctrl
->GetDefaultStyleEx().GetParagraphStyleName().IsEmpty())
649 styleName
= ctrl
->GetDefaultStyleEx().GetParagraphStyleName();
650 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_LIST
) &&
651 !ctrl
->GetDefaultStyleEx().GetListStyleName().IsEmpty())
652 styleName
= ctrl
->GetDefaultStyleEx().GetListStyleName();
654 else if (obj
&& (styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_CHARACTER
) &&
655 !obj
->GetAttributes().GetCharacterStyleName().IsEmpty())
657 styleName
= obj
->GetAttributes().GetCharacterStyleName();
659 else if (para
&& (styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_PARAGRAPH
) &&
660 !para
->GetAttributes().GetParagraphStyleName().IsEmpty())
662 styleName
= para
->GetAttributes().GetParagraphStyleName();
664 else if (para
&& (styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_LIST
) &&
665 !para
->GetAttributes().GetListStyleName().IsEmpty())
667 styleName
= para
->GetAttributes().GetListStyleName();
673 /// Auto-select from style under caret in idle time
674 void wxRichTextStyleListBox::OnIdle(wxIdleEvent
& event
)
676 if (CanAutoSetSelection() && GetRichTextCtrl() && wxWindow::FindFocus() != this)
678 wxString styleName
= GetStyleToShowInIdleTime(GetRichTextCtrl(), GetStyleType());
680 int sel
= GetSelection();
681 if (!styleName
.IsEmpty())
683 // Don't do the selection if it's already set
684 if (sel
== GetIndexForStyle(styleName
))
687 SetStyleSelection(styleName
);
696 void wxRichTextStyleListBox::ApplyStyle(int item
)
698 if ( item
!= wxNOT_FOUND
)
700 wxRichTextStyleDefinition
* def
= GetStyle(item
);
701 if (def
&& GetRichTextCtrl())
703 GetRichTextCtrl()->ApplyStyle(def
);
704 GetRichTextCtrl()->SetFocus();
710 * wxRichTextStyleListCtrl class: manages a listbox and a choice control to
711 * switch shown style types
714 IMPLEMENT_CLASS(wxRichTextStyleListCtrl
, wxControl
)
716 BEGIN_EVENT_TABLE(wxRichTextStyleListCtrl
, wxControl
)
717 EVT_CHOICE(wxID_ANY
, wxRichTextStyleListCtrl::OnChooseType
)
718 EVT_SIZE(wxRichTextStyleListCtrl::OnSize
)
721 wxRichTextStyleListCtrl::wxRichTextStyleListCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
722 const wxSize
& size
, long style
)
725 Create(parent
, id
, pos
, size
, style
);
728 bool wxRichTextStyleListCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
729 const wxSize
& size
, long style
)
731 wxControl::Create(parent
, id
, pos
, size
, style
);
733 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
734 if (size
!= wxDefaultSize
)
735 SetBestFittingSize(size
);
737 bool showSelector
= ((style
& wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR
) == 0);
739 m_styleListBox
= new wxRichTextStyleListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, showSelector
? wxSIMPLE_BORDER
: wxNO_BORDER
);
741 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
745 wxArrayString choices
;
746 choices
.Add(_("All styles"));
747 choices
.Add(_("Paragraph styles"));
748 choices
.Add(_("Character styles"));
749 choices
.Add(_("List styles"));
751 m_styleChoice
= new wxChoice(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, choices
);
753 boxSizer
->Add(m_styleListBox
, 1, wxALL
|wxEXPAND
, 5);
754 boxSizer
->Add(m_styleChoice
, 0, wxALL
|wxEXPAND
, 5);
758 boxSizer
->Add(m_styleListBox
, 1, wxALL
|wxEXPAND
, 0);
768 int i
= StyleTypeToIndex(m_styleListBox
->GetStyleType());
769 m_styleChoice
->SetSelection(i
);
772 m_dontUpdate
= false;
777 wxRichTextStyleListCtrl::~wxRichTextStyleListCtrl()
782 /// React to style type choice
783 void wxRichTextStyleListCtrl::OnChooseType(wxCommandEvent
& event
)
785 if (event
.GetEventObject() != m_styleChoice
)
792 wxRichTextStyleListBox::wxRichTextStyleType styleType
= StyleIndexToType(event
.GetSelection());
793 m_styleListBox
->SetStyleType(styleType
);
797 /// Lay out the controls
798 void wxRichTextStyleListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
804 /// Get the choice index for style type
805 int wxRichTextStyleListCtrl::StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType
)
807 if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
)
811 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH
)
815 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
)
819 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST
)
826 /// Get the style type for choice index
827 wxRichTextStyleListBox::wxRichTextStyleType
wxRichTextStyleListCtrl::StyleIndexToType(int i
)
830 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH
;
832 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
;
834 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST
;
836 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
;
839 /// Associates the control with a style manager
840 void wxRichTextStyleListCtrl::SetStyleSheet(wxRichTextStyleSheet
* styleSheet
)
843 m_styleListBox
->SetStyleSheet(styleSheet
);
846 wxRichTextStyleSheet
* wxRichTextStyleListCtrl::GetStyleSheet() const
849 return m_styleListBox
->GetStyleSheet();
854 /// Associates the control with a wxRichTextCtrl
855 void wxRichTextStyleListCtrl::SetRichTextCtrl(wxRichTextCtrl
* ctrl
)
858 m_styleListBox
->SetRichTextCtrl(ctrl
);
861 wxRichTextCtrl
* wxRichTextStyleListCtrl::GetRichTextCtrl() const
864 return m_styleListBox
->GetRichTextCtrl();
869 /// Set/get the style type to display
870 void wxRichTextStyleListCtrl::SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType
)
873 m_styleListBox
->SetStyleType(styleType
);
879 int i
= StyleTypeToIndex(m_styleListBox
->GetStyleType());
880 m_styleChoice
->SetSelection(i
);
883 m_dontUpdate
= false;
886 wxRichTextStyleListBox::wxRichTextStyleType
wxRichTextStyleListCtrl::GetStyleType() const
889 return m_styleListBox
->GetStyleType();
891 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
;
894 /// Updates the style list box
895 void wxRichTextStyleListCtrl::UpdateStyles()
898 m_styleListBox
->UpdateStyles();
904 * Style drop-down for a wxComboCtrl
908 BEGIN_EVENT_TABLE(wxRichTextStyleComboPopup
, wxRichTextStyleListBox
)
909 EVT_MOTION(wxRichTextStyleComboPopup::OnMouseMove
)
910 EVT_LEFT_DOWN(wxRichTextStyleComboPopup::OnMouseClick
)
913 void wxRichTextStyleComboPopup::SetStringValue( const wxString
& s
)
915 m_value
= SetStyleSelection(s
);
918 wxString
wxRichTextStyleComboPopup::GetStringValue() const
923 wxRichTextStyleDefinition
* def
= GetStyle(sel
);
925 return def
->GetName();
927 return wxEmptyString
;
931 // Popup event handlers
934 // Mouse hot-tracking
935 void wxRichTextStyleComboPopup::OnMouseMove(wxMouseEvent
& event
)
937 // Move selection to cursor if it is inside the popup
939 int itemHere
= wxRichTextStyleListBox::HitTest(event
.GetPosition());
942 wxRichTextStyleListBox::SetSelection(itemHere
);
943 m_itemHere
= itemHere
;
948 // On mouse left, set the value and close the popup
949 void wxRichTextStyleComboPopup::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
952 m_value
= m_itemHere
;
954 // Ordering is important, so we don't dismiss this popup accidentally
955 // by setting the focus elsewhere e.g. in ApplyStyle
959 wxRichTextStyleListBox::ApplyStyle(m_itemHere
);
963 * wxRichTextStyleComboCtrl
964 * A combo for applying styles.
967 IMPLEMENT_CLASS(wxRichTextStyleComboCtrl
, wxComboCtrl
)
969 BEGIN_EVENT_TABLE(wxRichTextStyleComboCtrl
, wxComboCtrl
)
970 EVT_IDLE(wxRichTextStyleComboCtrl::OnIdle
)
973 bool wxRichTextStyleComboCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
974 const wxSize
& size
, long style
)
976 if (!wxComboCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, style
))
979 SetPopupMaxHeight(400);
981 m_stylePopup
= new wxRichTextStyleComboPopup
;
983 SetPopupControl(m_stylePopup
);
988 /// Auto-select from style under caret in idle time
990 // TODO: must be able to show italic, bold, combinations
991 // in style box. Do we have a concept of automatic, temporary
992 // styles that are added whenever we wish to show a style
993 // that doesn't exist already? E.g. "Bold, Italic, Underline".
994 // Word seems to generate these things on the fly.
995 // If there's a named style already, it uses e.g. Heading1 + Bold, Italic
996 // If you unembolden text in a style that has bold, it uses the
998 // TODO: order styles alphabetically. This means indexes can change,
999 // so need a different way to specify selections, i.e. by name.
1001 void wxRichTextStyleComboCtrl::OnIdle(wxIdleEvent
& event
)
1003 if (GetRichTextCtrl() && !IsPopupShown() && m_stylePopup
&& wxWindow::FindFocus() != this)
1005 wxString styleName
= wxRichTextStyleListBox::GetStyleToShowInIdleTime(GetRichTextCtrl(), m_stylePopup
->GetStyleType());
1007 wxString currentValue
= GetValue();
1008 if (!styleName
.IsEmpty())
1010 // Don't do the selection if it's already set
1011 if (currentValue
== styleName
)
1014 SetValue(styleName
);
1016 else if (!currentValue
.IsEmpty())
1017 SetValue(wxEmptyString
);