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 wxRichTextAttr
& attr
)
99 wxASSERT( (i
>= 0 && i
< 10) );
100 if (i
>= 0 && i
< 10)
101 m_levelStyles
[i
] = attr
;
104 const wxRichTextAttr
* wxRichTextListStyleDefinition::GetLevelAttributes(int i
) const
106 wxASSERT( (i
>= 0 && i
< 10) );
107 if (i
>= 0 && i
< 10)
108 return & m_levelStyles
[i
];
113 wxRichTextAttr
* 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())
135 if (bulletStyle
& wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
136 attr
.SetBulletText(bulletSymbol
);
138 attr
.SetBulletName(bulletSymbol
);
141 m_levelStyles
[i
] = attr
;
145 /// Finds the level corresponding to the given indentation
146 int wxRichTextListStyleDefinition::FindLevelForIndent(int indent
) const
149 for (i
= 0; i
< 10; i
++)
151 if (indent
< m_levelStyles
[i
].GetLeftIndent())
162 /// Combine the list style with a paragraph style, using the given indent (from which
163 /// an appropriate level is found)
164 wxRichTextAttr
wxRichTextListStyleDefinition::CombineWithParagraphStyle(int indent
, const wxRichTextAttr
& paraStyle
)
166 int listLevel
= FindLevelForIndent(indent
);
168 wxRichTextAttr
attr(*GetLevelAttributes(listLevel
));
169 int oldLeftIndent
= attr
.GetLeftIndent();
170 int oldLeftSubIndent
= attr
.GetLeftSubIndent();
172 // First apply the overall paragraph style, if any
173 wxRichTextApplyStyle(attr
, GetStyle());
175 // Then apply paragraph style, e.g. from paragraph style definition
176 wxRichTextApplyStyle(attr
, paraStyle
);
178 // We override the indents according to the list definition
179 attr
.SetLeftIndent(oldLeftIndent
, oldLeftSubIndent
);
184 /// Combine the base and list style, using the given indent (from which
185 /// an appropriate level is found)
186 wxRichTextAttr
wxRichTextListStyleDefinition::GetCombinedStyle(int indent
)
188 int listLevel
= FindLevelForIndent(indent
);
189 return GetCombinedStyleForLevel(listLevel
);
192 /// Combine the base and list style, using the given indent (from which
193 /// an appropriate level is found)
194 wxRichTextAttr
wxRichTextListStyleDefinition::GetCombinedStyleForLevel(int listLevel
)
196 wxRichTextAttr
attr(*GetLevelAttributes(listLevel
));
197 int oldLeftIndent
= attr
.GetLeftIndent();
198 int oldLeftSubIndent
= attr
.GetLeftSubIndent();
200 // Apply the overall paragraph style, if any
201 wxRichTextApplyStyle(attr
, GetStyle());
203 // We override the indents according to the list definition
204 attr
.SetLeftIndent(oldLeftIndent
, oldLeftSubIndent
);
209 /// Is this a numbered list?
210 bool wxRichTextListStyleDefinition::IsNumbered(int i
) const
212 return (0 != (GetLevelAttributes(i
)->GetFlags() &
213 (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
|wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
|
214 wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
|wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)));
221 IMPLEMENT_CLASS(wxRichTextStyleSheet
, wxObject
)
223 wxRichTextStyleSheet::~wxRichTextStyleSheet()
228 m_nextSheet
->m_previousSheet
= m_previousSheet
;
231 m_previousSheet
->m_nextSheet
= m_nextSheet
;
233 m_previousSheet
= NULL
;
238 void wxRichTextStyleSheet::Init()
240 m_previousSheet
= NULL
;
244 /// Add a definition to one of the style lists
245 bool wxRichTextStyleSheet::AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
)
253 bool wxRichTextStyleSheet::RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
)
255 wxList::compatibility_iterator node
= list
.Find(def
);
258 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
268 /// Find a definition by name
269 wxRichTextStyleDefinition
* wxRichTextStyleSheet::FindStyle(const wxList
& list
, const wxString
& name
, bool recurse
) const
271 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
273 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
274 if (def
->GetName().Lower() == name
.Lower())
278 if (m_nextSheet
&& recurse
)
279 return m_nextSheet
->FindStyle(list
, name
, recurse
);
284 /// Delete all styles
285 void wxRichTextStyleSheet::DeleteStyles()
287 WX_CLEAR_LIST(wxList
, m_characterStyleDefinitions
);
288 WX_CLEAR_LIST(wxList
, m_paragraphStyleDefinitions
);
289 WX_CLEAR_LIST(wxList
, m_listStyleDefinitions
);
292 /// Insert into list of style sheets
293 bool wxRichTextStyleSheet::InsertSheet(wxRichTextStyleSheet
* before
)
295 m_previousSheet
= before
->m_previousSheet
;
296 m_nextSheet
= before
;
298 before
->m_previousSheet
= this;
302 /// Append to list of style sheets
303 bool wxRichTextStyleSheet::AppendSheet(wxRichTextStyleSheet
* after
)
305 wxRichTextStyleSheet
* last
= after
;
306 while (last
&& last
->m_nextSheet
)
308 last
= last
->m_nextSheet
;
313 m_previousSheet
= last
;
314 last
->m_nextSheet
= this;
322 /// Unlink from the list of style sheets
323 void wxRichTextStyleSheet::Unlink()
326 m_previousSheet
->m_nextSheet
= m_nextSheet
;
328 m_nextSheet
->m_previousSheet
= m_previousSheet
;
330 m_previousSheet
= NULL
;
334 /// Add a definition to the character style list
335 bool wxRichTextStyleSheet::AddCharacterStyle(wxRichTextCharacterStyleDefinition
* def
)
337 def
->GetStyle().SetCharacterStyleName(def
->GetName());
338 return AddStyle(m_characterStyleDefinitions
, def
);
341 /// Add a definition to the paragraph style list
342 bool wxRichTextStyleSheet::AddParagraphStyle(wxRichTextParagraphStyleDefinition
* def
)
344 def
->GetStyle().SetParagraphStyleName(def
->GetName());
345 return AddStyle(m_paragraphStyleDefinitions
, def
);
348 /// Add a definition to the list style list
349 bool wxRichTextStyleSheet::AddListStyle(wxRichTextListStyleDefinition
* def
)
351 def
->GetStyle().SetListStyleName(def
->GetName());
352 return AddStyle(m_listStyleDefinitions
, def
);
356 void wxRichTextStyleSheet::Copy(const wxRichTextStyleSheet
& sheet
)
360 wxList::compatibility_iterator node
;
362 for (node
= sheet
.m_characterStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
364 wxRichTextCharacterStyleDefinition
* def
= (wxRichTextCharacterStyleDefinition
*) node
->GetData();
365 AddCharacterStyle(new wxRichTextCharacterStyleDefinition(*def
));
368 for (node
= sheet
.m_paragraphStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
370 wxRichTextParagraphStyleDefinition
* def
= (wxRichTextParagraphStyleDefinition
*) node
->GetData();
371 AddParagraphStyle(new wxRichTextParagraphStyleDefinition(*def
));
374 for (node
= sheet
.m_listStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
376 wxRichTextListStyleDefinition
* def
= (wxRichTextListStyleDefinition
*) node
->GetData();
377 AddListStyle(new wxRichTextListStyleDefinition(*def
));
382 bool wxRichTextStyleSheet::operator==(const wxRichTextStyleSheet
& WXUNUSED(sheet
)) const
391 * wxRichTextStyleListBox: a listbox to display styles.
394 IMPLEMENT_CLASS(wxRichTextStyleListBox
, wxHtmlListBox
)
396 BEGIN_EVENT_TABLE(wxRichTextStyleListBox
, wxHtmlListBox
)
397 EVT_LEFT_DOWN(wxRichTextStyleListBox::OnLeftDown
)
398 EVT_LEFT_DCLICK(wxRichTextStyleListBox::OnLeftDoubleClick
)
399 EVT_IDLE(wxRichTextStyleListBox::OnIdle
)
402 wxRichTextStyleListBox::wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
403 const wxSize
& size
, long style
)
406 Create(parent
, id
, pos
, size
, style
);
409 bool wxRichTextStyleListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
410 const wxSize
& size
, long style
)
412 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
415 wxRichTextStyleListBox::~wxRichTextStyleListBox()
419 /// Returns the HTML for this item
420 wxString
wxRichTextStyleListBox::OnGetItem(size_t n
) const
422 if (!GetStyleSheet())
423 return wxEmptyString
;
425 wxRichTextStyleDefinition
* def
= GetStyle(n
);
427 return CreateHTML(def
);
429 return wxEmptyString
;
432 // Get style for index
433 wxRichTextStyleDefinition
* wxRichTextStyleListBox::GetStyle(size_t i
) const
435 if (!GetStyleSheet())
438 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
)
440 // First paragraph styles, then character, then list
441 if (i
< GetStyleSheet()->GetParagraphStyleCount())
442 return GetStyleSheet()->GetParagraphStyle(i
);
444 if ((i
- GetStyleSheet()->GetParagraphStyleCount()) < GetStyleSheet()->GetCharacterStyleCount())
445 return GetStyleSheet()->GetCharacterStyle(i
- GetStyleSheet()->GetParagraphStyleCount());
447 if ((i
- GetStyleSheet()->GetParagraphStyleCount() - GetStyleSheet()->GetCharacterStyleCount()) < GetStyleSheet()->GetListStyleCount())
448 return GetStyleSheet()->GetListStyle(i
- GetStyleSheet()->GetParagraphStyleCount() - GetStyleSheet()->GetCharacterStyleCount());
450 else if ((GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH
) && (i
< GetStyleSheet()->GetParagraphStyleCount()))
452 return GetStyleSheet()->GetParagraphStyle(i
);
454 else if ((GetStyleType() == wxRICHTEXT_STYLE_CHARACTER
) && (i
< GetStyleSheet()->GetCharacterStyleCount()))
456 return GetStyleSheet()->GetCharacterStyle(i
);
458 else if ((GetStyleType() == wxRICHTEXT_STYLE_LIST
) && (i
< GetStyleSheet()->GetListStyleCount()))
460 return GetStyleSheet()->GetListStyle(i
);
467 void wxRichTextStyleListBox::UpdateStyles()
471 SetSelection(wxNOT_FOUND
);
473 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
)
474 SetItemCount(GetStyleSheet()->GetParagraphStyleCount()+GetStyleSheet()->GetCharacterStyleCount()+GetStyleSheet()->GetListStyleCount());
475 else if (GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH
)
476 SetItemCount(GetStyleSheet()->GetParagraphStyleCount());
477 else if (GetStyleType() == wxRICHTEXT_STYLE_CHARACTER
)
478 SetItemCount(GetStyleSheet()->GetCharacterStyleCount());
479 else if (GetStyleType() == wxRICHTEXT_STYLE_LIST
)
480 SetItemCount(GetStyleSheet()->GetListStyleCount());
484 if (GetItemCount() > 0)
492 // Get index for style name
493 int wxRichTextStyleListBox::GetIndexForStyle(const wxString
& name
) const
497 int count
= GetItemCount();
500 for (i
= 0; i
< (int) count
; i
++)
502 wxRichTextStyleDefinition
* def
= GetStyle(i
);
503 if (def
->GetName() == name
)
510 /// Set selection for string
511 int wxRichTextStyleListBox::SetStyleSelection(const wxString
& name
)
513 int i
= GetIndexForStyle(name
);
519 // Convert a colour to a 6-digit hex string
520 static wxString
ColourToHexString(const wxColour
& col
)
524 hex
+= wxDecToHex(col
.Red());
525 hex
+= wxDecToHex(col
.Green());
526 hex
+= wxDecToHex(col
.Blue());
531 /// Creates a suitable HTML fragment for a definition
532 wxString
wxRichTextStyleListBox::CreateHTML(wxRichTextStyleDefinition
* def
) const
534 // TODO: indicate list format for list style types
536 wxString
str(wxT("<table><tr>"));
538 if (def
->GetStyle().GetLeftIndent() > 0)
540 wxClientDC
dc((wxWindow
*) this);
542 str
<< wxT("<td width=") << (ConvertTenthsMMToPixels(dc
, def
->GetStyle().GetLeftIndent())/2) << wxT("></td>");
545 str
<< wxT("<td nowrap>");
553 int stdFontSize
= 12;
554 int thisFontSize
= ((def
->GetStyle().GetFlags() & wxTEXT_ATTR_FONT_SIZE
) != 0) ? def
->GetStyle().GetFontSize() : stdFontSize
;
556 if (thisFontSize
< stdFontSize
)
558 else if (thisFontSize
> stdFontSize
)
563 str
<< wxT(" size=") << size
;
565 if (!def
->GetStyle().GetFontFaceName().IsEmpty())
566 str
<< wxT(" face=\"") << def
->GetStyle().GetFontFaceName() << wxT("\"");
568 if (def
->GetStyle().GetTextColour().Ok())
569 str
<< wxT(" color=\"#") << ColourToHexString(def
->GetStyle().GetTextColour()) << wxT("\"");
573 bool hasBold
= false;
574 bool hasItalic
= false;
575 bool hasUnderline
= false;
577 if (def
->GetStyle().GetFontWeight() == wxBOLD
)
579 if (def
->GetStyle().GetFontStyle() == wxITALIC
)
581 if (def
->GetStyle().GetFontUnderlined())
591 str
+= def
->GetName();
600 str
<< wxT("</font>");
602 str
+= wxT("</td></tr></table>");
606 // Convert units in tends of a millimetre to device units
607 int wxRichTextStyleListBox::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const
609 int ppi
= dc
.GetPPI().x
;
611 // There are ppi pixels in 254.1 "1/10 mm"
613 double pixels
= ((double) units
* (double)ppi
) / 254.1;
618 void wxRichTextStyleListBox::OnLeftDown(wxMouseEvent
& event
)
620 wxVListBox::OnLeftDown(event
);
622 int item
= HitTest(event
.GetPosition());
623 if (item
!= wxNOT_FOUND
&& GetApplyOnSelection())
627 void wxRichTextStyleListBox::OnLeftDoubleClick(wxMouseEvent
& event
)
629 wxVListBox::OnLeftDown(event
);
631 int item
= HitTest(event
.GetPosition());
632 if (item
!= wxNOT_FOUND
&& !GetApplyOnSelection())
636 /// Helper for listbox and combo control
637 wxString
wxRichTextStyleListBox::GetStyleToShowInIdleTime(wxRichTextCtrl
* ctrl
, wxRichTextStyleType styleType
)
639 int adjustedCaretPos
= ctrl
->GetAdjustedCaretPosition(ctrl
->GetCaretPosition());
641 wxRichTextParagraph
* para
= ctrl
->GetBuffer().GetParagraphAtPosition(adjustedCaretPos
);
642 wxRichTextObject
* obj
= ctrl
->GetBuffer().GetLeafObjectAtPosition(adjustedCaretPos
);
646 // Take into account current default style just chosen by user
647 if (ctrl
->IsDefaultStyleShowing())
649 if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_CHARACTER
) &&
650 !ctrl
->GetDefaultStyleEx().GetCharacterStyleName().IsEmpty())
651 styleName
= ctrl
->GetDefaultStyleEx().GetCharacterStyleName();
652 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_PARAGRAPH
) &&
653 !ctrl
->GetDefaultStyleEx().GetParagraphStyleName().IsEmpty())
654 styleName
= ctrl
->GetDefaultStyleEx().GetParagraphStyleName();
655 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_LIST
) &&
656 !ctrl
->GetDefaultStyleEx().GetListStyleName().IsEmpty())
657 styleName
= ctrl
->GetDefaultStyleEx().GetListStyleName();
659 else if (obj
&& (styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_CHARACTER
) &&
660 !obj
->GetAttributes().GetCharacterStyleName().IsEmpty())
662 styleName
= obj
->GetAttributes().GetCharacterStyleName();
664 else if (para
&& (styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_PARAGRAPH
) &&
665 !para
->GetAttributes().GetParagraphStyleName().IsEmpty())
667 styleName
= para
->GetAttributes().GetParagraphStyleName();
669 else if (para
&& (styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_LIST
) &&
670 !para
->GetAttributes().GetListStyleName().IsEmpty())
672 styleName
= para
->GetAttributes().GetListStyleName();
678 /// Auto-select from style under caret in idle time
679 void wxRichTextStyleListBox::OnIdle(wxIdleEvent
& event
)
681 if (CanAutoSetSelection() && GetRichTextCtrl() && wxWindow::FindFocus() != this)
683 wxString styleName
= GetStyleToShowInIdleTime(GetRichTextCtrl(), GetStyleType());
685 int sel
= GetSelection();
686 if (!styleName
.IsEmpty())
688 // Don't do the selection if it's already set
689 if (sel
== GetIndexForStyle(styleName
))
692 SetStyleSelection(styleName
);
701 void wxRichTextStyleListBox::ApplyStyle(int item
)
703 if ( item
!= wxNOT_FOUND
)
705 wxRichTextStyleDefinition
* def
= GetStyle(item
);
706 if (def
&& GetRichTextCtrl())
708 GetRichTextCtrl()->ApplyStyle(def
);
709 GetRichTextCtrl()->SetFocus();
715 * wxRichTextStyleListCtrl class: manages a listbox and a choice control to
716 * switch shown style types
719 IMPLEMENT_CLASS(wxRichTextStyleListCtrl
, wxControl
)
721 BEGIN_EVENT_TABLE(wxRichTextStyleListCtrl
, wxControl
)
722 EVT_CHOICE(wxID_ANY
, wxRichTextStyleListCtrl::OnChooseType
)
723 EVT_SIZE(wxRichTextStyleListCtrl::OnSize
)
726 wxRichTextStyleListCtrl::wxRichTextStyleListCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
727 const wxSize
& size
, long style
)
730 Create(parent
, id
, pos
, size
, style
);
733 bool wxRichTextStyleListCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
734 const wxSize
& size
, long style
)
736 wxControl::Create(parent
, id
, pos
, size
, style
);
738 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
739 if (size
!= wxDefaultSize
)
740 SetInitialSize(size
);
742 bool showSelector
= ((style
& wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR
) == 0);
744 m_styleListBox
= new wxRichTextStyleListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, showSelector
? wxSIMPLE_BORDER
: wxNO_BORDER
);
746 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
750 wxArrayString choices
;
751 choices
.Add(_("All styles"));
752 choices
.Add(_("Paragraph styles"));
753 choices
.Add(_("Character styles"));
754 choices
.Add(_("List styles"));
756 m_styleChoice
= new wxChoice(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, choices
);
758 boxSizer
->Add(m_styleListBox
, 1, wxALL
|wxEXPAND
, 5);
759 boxSizer
->Add(m_styleChoice
, 0, wxALL
|wxEXPAND
, 5);
763 boxSizer
->Add(m_styleListBox
, 1, wxALL
|wxEXPAND
, 0);
773 int i
= StyleTypeToIndex(m_styleListBox
->GetStyleType());
774 m_styleChoice
->SetSelection(i
);
777 m_dontUpdate
= false;
782 wxRichTextStyleListCtrl::~wxRichTextStyleListCtrl()
787 /// React to style type choice
788 void wxRichTextStyleListCtrl::OnChooseType(wxCommandEvent
& event
)
790 if (event
.GetEventObject() != m_styleChoice
)
797 wxRichTextStyleListBox::wxRichTextStyleType styleType
= StyleIndexToType(event
.GetSelection());
798 m_styleListBox
->SetStyleType(styleType
);
802 /// Lay out the controls
803 void wxRichTextStyleListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
809 /// Get the choice index for style type
810 int wxRichTextStyleListCtrl::StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType
)
812 if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
)
816 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH
)
820 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
)
824 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST
)
831 /// Get the style type for choice index
832 wxRichTextStyleListBox::wxRichTextStyleType
wxRichTextStyleListCtrl::StyleIndexToType(int i
)
835 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH
;
837 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
;
839 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST
;
841 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
;
844 /// Associates the control with a style manager
845 void wxRichTextStyleListCtrl::SetStyleSheet(wxRichTextStyleSheet
* styleSheet
)
848 m_styleListBox
->SetStyleSheet(styleSheet
);
851 wxRichTextStyleSheet
* wxRichTextStyleListCtrl::GetStyleSheet() const
854 return m_styleListBox
->GetStyleSheet();
859 /// Associates the control with a wxRichTextCtrl
860 void wxRichTextStyleListCtrl::SetRichTextCtrl(wxRichTextCtrl
* ctrl
)
863 m_styleListBox
->SetRichTextCtrl(ctrl
);
866 wxRichTextCtrl
* wxRichTextStyleListCtrl::GetRichTextCtrl() const
869 return m_styleListBox
->GetRichTextCtrl();
874 /// Set/get the style type to display
875 void wxRichTextStyleListCtrl::SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType
)
878 m_styleListBox
->SetStyleType(styleType
);
884 int i
= StyleTypeToIndex(m_styleListBox
->GetStyleType());
885 m_styleChoice
->SetSelection(i
);
888 m_dontUpdate
= false;
891 wxRichTextStyleListBox::wxRichTextStyleType
wxRichTextStyleListCtrl::GetStyleType() const
894 return m_styleListBox
->GetStyleType();
896 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
;
899 /// Updates the style list box
900 void wxRichTextStyleListCtrl::UpdateStyles()
903 m_styleListBox
->UpdateStyles();
909 * Style drop-down for a wxComboCtrl
913 BEGIN_EVENT_TABLE(wxRichTextStyleComboPopup
, wxRichTextStyleListBox
)
914 EVT_MOTION(wxRichTextStyleComboPopup::OnMouseMove
)
915 EVT_LEFT_DOWN(wxRichTextStyleComboPopup::OnMouseClick
)
918 void wxRichTextStyleComboPopup::SetStringValue( const wxString
& s
)
920 m_value
= SetStyleSelection(s
);
923 wxString
wxRichTextStyleComboPopup::GetStringValue() const
928 wxRichTextStyleDefinition
* def
= GetStyle(sel
);
930 return def
->GetName();
932 return wxEmptyString
;
936 // Popup event handlers
939 // Mouse hot-tracking
940 void wxRichTextStyleComboPopup::OnMouseMove(wxMouseEvent
& event
)
942 // Move selection to cursor if it is inside the popup
944 int itemHere
= wxRichTextStyleListBox::HitTest(event
.GetPosition());
947 wxRichTextStyleListBox::SetSelection(itemHere
);
948 m_itemHere
= itemHere
;
953 // On mouse left, set the value and close the popup
954 void wxRichTextStyleComboPopup::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
957 m_value
= m_itemHere
;
959 // Ordering is important, so we don't dismiss this popup accidentally
960 // by setting the focus elsewhere e.g. in ApplyStyle
964 wxRichTextStyleListBox::ApplyStyle(m_itemHere
);
968 * wxRichTextStyleComboCtrl
969 * A combo for applying styles.
972 IMPLEMENT_CLASS(wxRichTextStyleComboCtrl
, wxComboCtrl
)
974 BEGIN_EVENT_TABLE(wxRichTextStyleComboCtrl
, wxComboCtrl
)
975 EVT_IDLE(wxRichTextStyleComboCtrl::OnIdle
)
978 bool wxRichTextStyleComboCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
979 const wxSize
& size
, long style
)
981 if (!wxComboCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, style
))
984 SetPopupMaxHeight(400);
986 m_stylePopup
= new wxRichTextStyleComboPopup
;
988 SetPopupControl(m_stylePopup
);
993 /// Auto-select from style under caret in idle time
995 // TODO: must be able to show italic, bold, combinations
996 // in style box. Do we have a concept of automatic, temporary
997 // styles that are added whenever we wish to show a style
998 // that doesn't exist already? E.g. "Bold, Italic, Underline".
999 // Word seems to generate these things on the fly.
1000 // If there's a named style already, it uses e.g. Heading1 + Bold, Italic
1001 // If you unembolden text in a style that has bold, it uses the
1003 // TODO: order styles alphabetically. This means indexes can change,
1004 // so need a different way to specify selections, i.e. by name.
1006 void wxRichTextStyleComboCtrl::OnIdle(wxIdleEvent
& event
)
1008 if (GetRichTextCtrl() && !IsPopupShown() && m_stylePopup
&& wxWindow::FindFocus() != this)
1010 wxString styleName
= wxRichTextStyleListBox::GetStyleToShowInIdleTime(GetRichTextCtrl(), m_stylePopup
->GetStyleType());
1012 wxString currentValue
= GetValue();
1013 if (!styleName
.IsEmpty())
1015 // Don't do the selection if it's already set
1016 if (currentValue
== styleName
)
1019 SetValue(styleName
);
1021 else if (!currentValue
.IsEmpty())
1022 SetValue(wxEmptyString
);