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
)
38 IMPLEMENT_CLASS(wxRichTextBoxStyleDefinition
, wxRichTextStyleDefinition
)
44 void wxRichTextStyleDefinition::Copy(const wxRichTextStyleDefinition
& def
)
47 m_baseStyle
= def
.m_baseStyle
;
48 m_style
= def
.m_style
;
49 m_description
= def
.m_description
;
52 bool wxRichTextStyleDefinition::Eq(const wxRichTextStyleDefinition
& def
) const
54 return (m_name
== def
.m_name
&& m_baseStyle
== def
.m_baseStyle
&& m_style
== def
.m_style
);
57 /// Gets the style combined with the base style
58 wxRichTextAttr
wxRichTextStyleDefinition::GetStyleMergedWithBase(const wxRichTextStyleSheet
* sheet
) const
60 if (m_baseStyle
.IsEmpty())
63 bool isParaStyle
= IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
));
64 bool isCharStyle
= IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition
));
65 bool isListStyle
= IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
));
66 bool isBoxStyle
= IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
));
68 // Collect the styles, detecting loops
69 wxArrayString styleNames
;
71 const wxRichTextStyleDefinition
* def
= this;
74 styles
.Insert((wxObject
*) def
);
75 styleNames
.Add(def
->GetName());
77 wxString baseStyleName
= def
->GetBaseStyle();
78 if (!baseStyleName
.IsEmpty() && styleNames
.Index(baseStyleName
) == wxNOT_FOUND
)
81 def
= sheet
->FindParagraphStyle(baseStyleName
);
83 def
= sheet
->FindCharacterStyle(baseStyleName
);
85 def
= sheet
->FindListStyle(baseStyleName
);
87 def
= sheet
->FindBoxStyle(baseStyleName
);
89 def
= sheet
->FindStyle(baseStyleName
);
96 wxList::compatibility_iterator node
= styles
.GetFirst();
99 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
100 attr
.Apply(def
->GetStyle(), NULL
);
101 node
= node
->GetNext();
108 * Paragraph style definition
111 void wxRichTextParagraphStyleDefinition::Copy(const wxRichTextParagraphStyleDefinition
& def
)
113 wxRichTextStyleDefinition::Copy(def
);
115 m_nextStyle
= def
.m_nextStyle
;
118 bool wxRichTextParagraphStyleDefinition::operator ==(const wxRichTextParagraphStyleDefinition
& def
) const
120 return (Eq(def
) && m_nextStyle
== def
.m_nextStyle
);
124 * Box style definition
127 void wxRichTextBoxStyleDefinition::Copy(const wxRichTextBoxStyleDefinition
& def
)
129 wxRichTextStyleDefinition::Copy(def
);
132 bool wxRichTextBoxStyleDefinition::operator ==(const wxRichTextBoxStyleDefinition
& def
) const
138 * List style definition
141 void wxRichTextListStyleDefinition::Copy(const wxRichTextListStyleDefinition
& def
)
143 wxRichTextParagraphStyleDefinition::Copy(def
);
146 for (i
= 0; i
< 10; i
++)
147 m_levelStyles
[i
] = def
.m_levelStyles
[i
];
150 bool wxRichTextListStyleDefinition::operator ==(const wxRichTextListStyleDefinition
& def
) const
155 for (i
= 0; i
< 10; i
++)
156 if (!(m_levelStyles
[i
] == def
.m_levelStyles
[i
]))
162 /// Sets/gets the attributes for the given level
163 void wxRichTextListStyleDefinition::SetLevelAttributes(int i
, const wxRichTextAttr
& attr
)
165 wxASSERT( (i
>= 0 && i
< 10) );
166 if (i
>= 0 && i
< 10)
167 m_levelStyles
[i
] = attr
;
170 const wxRichTextAttr
* wxRichTextListStyleDefinition::GetLevelAttributes(int i
) const
172 wxASSERT( (i
>= 0 && i
< 10) );
173 if (i
>= 0 && i
< 10)
174 return & m_levelStyles
[i
];
179 wxRichTextAttr
* wxRichTextListStyleDefinition::GetLevelAttributes(int i
)
181 wxASSERT( (i
>= 0 && i
< 10) );
182 if (i
>= 0 && i
< 10)
183 return & m_levelStyles
[i
];
188 /// Convenience function for setting the major attributes for a list level specification
189 void wxRichTextListStyleDefinition::SetAttributes(int i
, int leftIndent
, int leftSubIndent
, int bulletStyle
, const wxString
& bulletSymbol
)
191 wxASSERT( (i
>= 0 && i
< 10) );
192 if (i
>= 0 && i
< 10)
196 attr
.SetBulletStyle(bulletStyle
);
197 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
199 if (!bulletSymbol
.IsEmpty())
201 if (bulletStyle
& wxTEXT_ATTR_BULLET_STYLE_SYMBOL
)
202 attr
.SetBulletText(bulletSymbol
);
204 attr
.SetBulletName(bulletSymbol
);
207 m_levelStyles
[i
] = attr
;
211 /// Finds the level corresponding to the given indentation
212 int wxRichTextListStyleDefinition::FindLevelForIndent(int indent
) const
215 for (i
= 0; i
< 10; i
++)
217 if (indent
< m_levelStyles
[i
].GetLeftIndent())
228 /// Combine the list style with a paragraph style, using the given indent (from which
229 /// an appropriate level is found)
230 wxRichTextAttr
wxRichTextListStyleDefinition::CombineWithParagraphStyle(int indent
, const wxRichTextAttr
& paraStyle
, wxRichTextStyleSheet
* styleSheet
)
232 int listLevel
= FindLevelForIndent(indent
);
234 wxRichTextAttr
attr(*GetLevelAttributes(listLevel
));
235 int oldLeftIndent
= attr
.GetLeftIndent();
236 int oldLeftSubIndent
= attr
.GetLeftSubIndent();
238 // First apply the overall paragraph style, if any
240 attr
.Apply(GetStyleMergedWithBase(styleSheet
));
242 attr
.Apply(GetStyle());
244 // Then apply paragraph style, e.g. from paragraph style definition
245 attr
.Apply(paraStyle
);
247 // We override the indents according to the list definition
248 attr
.SetLeftIndent(oldLeftIndent
, oldLeftSubIndent
);
253 /// Combine the base and list style, using the given indent (from which
254 /// an appropriate level is found)
255 wxRichTextAttr
wxRichTextListStyleDefinition::GetCombinedStyle(int indent
, wxRichTextStyleSheet
* styleSheet
)
257 int listLevel
= FindLevelForIndent(indent
);
258 return GetCombinedStyleForLevel(listLevel
, styleSheet
);
261 /// Combine the base and list style, using the given indent (from which
262 /// an appropriate level is found)
263 wxRichTextAttr
wxRichTextListStyleDefinition::GetCombinedStyleForLevel(int listLevel
, wxRichTextStyleSheet
* styleSheet
)
265 wxRichTextAttr
attr(*GetLevelAttributes(listLevel
));
266 int oldLeftIndent
= attr
.GetLeftIndent();
267 int oldLeftSubIndent
= attr
.GetLeftSubIndent();
269 // Apply the overall paragraph style, if any
271 attr
.Apply(GetStyleMergedWithBase(styleSheet
));
273 attr
.Apply(GetStyle());
275 // We override the indents according to the list definition
276 attr
.SetLeftIndent(oldLeftIndent
, oldLeftSubIndent
);
281 /// Is this a numbered list?
282 bool wxRichTextListStyleDefinition::IsNumbered(int i
) const
284 return (0 != (GetLevelAttributes(i
)->GetFlags() &
285 (wxTEXT_ATTR_BULLET_STYLE_ARABIC
|wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER
|wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER
|
286 wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER
|wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER
)));
293 IMPLEMENT_CLASS(wxRichTextStyleSheet
, wxObject
)
295 wxRichTextStyleSheet::~wxRichTextStyleSheet()
300 m_nextSheet
->m_previousSheet
= m_previousSheet
;
303 m_previousSheet
->m_nextSheet
= m_nextSheet
;
305 m_previousSheet
= NULL
;
310 void wxRichTextStyleSheet::Init()
312 m_previousSheet
= NULL
;
316 /// Add a definition to one of the style lists
317 bool wxRichTextStyleSheet::AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
)
325 bool wxRichTextStyleSheet::RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
)
327 wxList::compatibility_iterator node
= list
.Find(def
);
330 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
341 bool wxRichTextStyleSheet::RemoveStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
)
343 if (RemoveParagraphStyle(def
, deleteStyle
))
345 if (RemoveCharacterStyle(def
, deleteStyle
))
347 if (RemoveListStyle(def
, deleteStyle
))
349 if (RemoveBoxStyle(def
, deleteStyle
))
354 /// Find a definition by name
355 wxRichTextStyleDefinition
* wxRichTextStyleSheet::FindStyle(const wxList
& list
, const wxString
& name
, bool recurse
) const
357 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
359 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
360 if (def
->GetName() == name
)
364 if (m_nextSheet
&& recurse
)
365 return m_nextSheet
->FindStyle(list
, name
, recurse
);
370 /// Delete all styles
371 void wxRichTextStyleSheet::DeleteStyles()
373 WX_CLEAR_LIST(wxList
, m_characterStyleDefinitions
);
374 WX_CLEAR_LIST(wxList
, m_paragraphStyleDefinitions
);
375 WX_CLEAR_LIST(wxList
, m_listStyleDefinitions
);
376 WX_CLEAR_LIST(wxList
, m_boxStyleDefinitions
);
379 /// Insert into list of style sheets
380 bool wxRichTextStyleSheet::InsertSheet(wxRichTextStyleSheet
* before
)
382 m_previousSheet
= before
->m_previousSheet
;
383 m_nextSheet
= before
;
385 before
->m_previousSheet
= this;
389 /// Append to list of style sheets
390 bool wxRichTextStyleSheet::AppendSheet(wxRichTextStyleSheet
* after
)
392 wxRichTextStyleSheet
* last
= after
;
393 while (last
&& last
->m_nextSheet
)
395 last
= last
->m_nextSheet
;
400 m_previousSheet
= last
;
401 last
->m_nextSheet
= this;
409 /// Unlink from the list of style sheets
410 void wxRichTextStyleSheet::Unlink()
413 m_previousSheet
->m_nextSheet
= m_nextSheet
;
415 m_nextSheet
->m_previousSheet
= m_previousSheet
;
417 m_previousSheet
= NULL
;
421 /// Add a definition to the character style list
422 bool wxRichTextStyleSheet::AddCharacterStyle(wxRichTextCharacterStyleDefinition
* def
)
424 def
->GetStyle().SetCharacterStyleName(def
->GetName());
425 return AddStyle(m_characterStyleDefinitions
, def
);
428 /// Add a definition to the paragraph style list
429 bool wxRichTextStyleSheet::AddParagraphStyle(wxRichTextParagraphStyleDefinition
* def
)
431 def
->GetStyle().SetParagraphStyleName(def
->GetName());
432 return AddStyle(m_paragraphStyleDefinitions
, def
);
435 /// Add a definition to the list style list
436 bool wxRichTextStyleSheet::AddListStyle(wxRichTextListStyleDefinition
* def
)
438 def
->GetStyle().SetListStyleName(def
->GetName());
439 return AddStyle(m_listStyleDefinitions
, def
);
442 /// Add a definition to the box style list
443 bool wxRichTextStyleSheet::AddBoxStyle(wxRichTextBoxStyleDefinition
* def
)
445 def
->GetStyle().GetTextBoxAttr().SetBoxStyleName(def
->GetName());
446 return AddStyle(m_boxStyleDefinitions
, def
);
449 /// Add a definition to the appropriate style list
450 bool wxRichTextStyleSheet::AddStyle(wxRichTextStyleDefinition
* def
)
452 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
454 return AddListStyle(listDef
);
456 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
458 return AddParagraphStyle(paraDef
);
460 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
462 return AddCharacterStyle(charDef
);
464 wxRichTextBoxStyleDefinition
* boxDef
= wxDynamicCast(def
, wxRichTextBoxStyleDefinition
);
466 return AddBoxStyle(boxDef
);
471 /// Find any definition by name
472 wxRichTextStyleDefinition
* wxRichTextStyleSheet::FindStyle(const wxString
& name
, bool recurse
) const
474 wxRichTextListStyleDefinition
* listDef
= FindListStyle(name
, recurse
);
478 wxRichTextParagraphStyleDefinition
* paraDef
= FindParagraphStyle(name
, recurse
);
482 wxRichTextCharacterStyleDefinition
* charDef
= FindCharacterStyle(name
, recurse
);
486 wxRichTextBoxStyleDefinition
* boxDef
= FindBoxStyle(name
, recurse
);
494 void wxRichTextStyleSheet::Copy(const wxRichTextStyleSheet
& sheet
)
498 wxList::compatibility_iterator node
;
500 for (node
= sheet
.m_characterStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
502 wxRichTextCharacterStyleDefinition
* def
= (wxRichTextCharacterStyleDefinition
*) node
->GetData();
503 AddCharacterStyle(new wxRichTextCharacterStyleDefinition(*def
));
506 for (node
= sheet
.m_paragraphStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
508 wxRichTextParagraphStyleDefinition
* def
= (wxRichTextParagraphStyleDefinition
*) node
->GetData();
509 AddParagraphStyle(new wxRichTextParagraphStyleDefinition(*def
));
512 for (node
= sheet
.m_listStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
514 wxRichTextListStyleDefinition
* def
= (wxRichTextListStyleDefinition
*) node
->GetData();
515 AddListStyle(new wxRichTextListStyleDefinition(*def
));
518 for (node
= sheet
.m_boxStyleDefinitions
.GetFirst(); node
; node
= node
->GetNext())
520 wxRichTextBoxStyleDefinition
* def
= (wxRichTextBoxStyleDefinition
*) node
->GetData();
521 AddBoxStyle(new wxRichTextBoxStyleDefinition(*def
));
524 SetName(sheet
.GetName());
525 SetDescription(sheet
.GetDescription());
529 bool wxRichTextStyleSheet::operator==(const wxRichTextStyleSheet
& WXUNUSED(sheet
)) const
538 // Functions for dealing with clashing names for different kinds of style.
539 // Returns "P", "C", "L" or "B" (paragraph, character, list or box) for
540 // style name | type.
541 static wxString
wxGetRichTextStyleType(const wxString
& style
)
543 return style
.AfterLast(wxT('|'));
546 static wxString
wxGetRichTextStyle(const wxString
& style
)
548 return style
.BeforeLast(wxT('|'));
553 * wxRichTextStyleListBox: a listbox to display styles.
556 IMPLEMENT_CLASS(wxRichTextStyleListBox
, wxHtmlListBox
)
558 BEGIN_EVENT_TABLE(wxRichTextStyleListBox
, wxHtmlListBox
)
559 EVT_LEFT_DOWN(wxRichTextStyleListBox::OnLeftDown
)
560 EVT_LEFT_DCLICK(wxRichTextStyleListBox::OnLeftDoubleClick
)
561 EVT_IDLE(wxRichTextStyleListBox::OnIdle
)
564 wxRichTextStyleListBox::wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
565 const wxSize
& size
, long style
)
568 Create(parent
, id
, pos
, size
, style
);
571 bool wxRichTextStyleListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
572 const wxSize
& size
, long style
)
574 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
577 wxRichTextStyleListBox::~wxRichTextStyleListBox()
581 /// Returns the HTML for this item
582 wxString
wxRichTextStyleListBox::OnGetItem(size_t n
) const
584 if (!GetStyleSheet())
585 return wxEmptyString
;
587 wxRichTextStyleDefinition
* def
= GetStyle(n
);
589 return CreateHTML(def
);
591 return wxEmptyString
;
594 // Get style for index
595 wxRichTextStyleDefinition
* wxRichTextStyleListBox::GetStyle(size_t i
) const
597 if (!GetStyleSheet())
600 if (i
>= m_styleNames
.GetCount() /* || i < 0 */ )
603 wxString styleType
= wxGetRichTextStyleType(m_styleNames
[i
]);
604 wxString style
= wxGetRichTextStyle(m_styleNames
[i
]);
605 if (styleType
== wxT("P"))
606 return GetStyleSheet()->FindParagraphStyle(style
);
607 else if (styleType
== wxT("C"))
608 return GetStyleSheet()->FindCharacterStyle(style
);
609 else if (styleType
== wxT("L"))
610 return GetStyleSheet()->FindListStyle(style
);
611 else if (styleType
== wxT("B"))
612 return GetStyleSheet()->FindBoxStyle(style
);
614 return GetStyleSheet()->FindStyle(style
);
618 void wxRichTextStyleListBox::UpdateStyles()
622 int oldSel
= GetSelection();
624 SetSelection(wxNOT_FOUND
);
626 m_styleNames
.Clear();
629 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
|| GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH
)
631 for (i
= 0; i
< GetStyleSheet()->GetParagraphStyleCount(); i
++)
632 m_styleNames
.Add(GetStyleSheet()->GetParagraphStyle(i
)->GetName() + wxT("|P"));
634 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
|| GetStyleType() == wxRICHTEXT_STYLE_CHARACTER
)
636 for (i
= 0; i
< GetStyleSheet()->GetCharacterStyleCount(); i
++)
637 m_styleNames
.Add(GetStyleSheet()->GetCharacterStyle(i
)->GetName() + wxT("|C"));
639 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
|| GetStyleType() == wxRICHTEXT_STYLE_LIST
)
641 for (i
= 0; i
< GetStyleSheet()->GetListStyleCount(); i
++)
642 m_styleNames
.Add(GetStyleSheet()->GetListStyle(i
)->GetName() + wxT("|L"));
644 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
|| GetStyleType() == wxRICHTEXT_STYLE_BOX
)
646 for (i
= 0; i
< GetStyleSheet()->GetBoxStyleCount(); i
++)
647 m_styleNames
.Add(GetStyleSheet()->GetBoxStyle(i
)->GetName() + wxT("|B"));
651 SetItemCount(m_styleNames
.GetCount());
656 if (oldSel
>= 0 && oldSel
< (int) GetItemCount())
658 else if (GetItemCount() > 0)
663 SetSelection(newSel
);
669 // Get index for style name
670 int wxRichTextStyleListBox::GetIndexForStyle(const wxString
& name
) const
673 if (GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH
)
675 else if (GetStyleType() == wxRICHTEXT_STYLE_CHARACTER
)
677 else if (GetStyleType() == wxRICHTEXT_STYLE_LIST
)
679 else if (GetStyleType() == wxRICHTEXT_STYLE_BOX
)
683 if (m_styleNames
.Index(s
+ wxT("|P")) != wxNOT_FOUND
)
685 else if (m_styleNames
.Index(s
+ wxT("|C")) != wxNOT_FOUND
)
687 else if (m_styleNames
.Index(s
+ wxT("|L")) != wxNOT_FOUND
)
689 else if (m_styleNames
.Index(s
+ wxT("|B")) != wxNOT_FOUND
)
692 return m_styleNames
.Index(s
);
695 /// Set selection for string
696 int wxRichTextStyleListBox::SetStyleSelection(const wxString
& name
)
698 int i
= GetIndexForStyle(name
);
704 // Convert a colour to a 6-digit hex string
705 static wxString
ColourToHexString(const wxColour
& col
)
709 hex
+= wxDecToHex(col
.Red());
710 hex
+= wxDecToHex(col
.Green());
711 hex
+= wxDecToHex(col
.Blue());
716 /// Creates a suitable HTML fragment for a definition
717 wxString
wxRichTextStyleListBox::CreateHTML(wxRichTextStyleDefinition
* def
) const
719 // TODO: indicate list format for list style types
723 bool isCentred
= false;
725 wxRichTextAttr
attr(def
->GetStyleMergedWithBase(GetStyleSheet()));
727 if (attr
.HasAlignment() && attr
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
731 str
<< wxT("<center>");
734 str
<< wxT("<table><tr>");
736 if (attr
.GetLeftIndent() > 0)
738 wxClientDC
dc((wxWindow
*) this);
740 str
<< wxT("<td width=") << wxMin(50, (ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent())/2)) << wxT("></td>");
744 str
<< wxT("<td nowrap align=\"center\">");
746 str
<< wxT("<td nowrap>");
754 // Guess a standard font size
757 // First see if we have a default/normal style to base the size on
758 wxString
normalTranslated(_("normal"));
759 wxString
defaultTranslated(_("default"));
761 for (i
= 0; i
< GetStyleSheet()->GetParagraphStyleCount(); i
++)
763 wxRichTextStyleDefinition
* d
= GetStyleSheet()->GetParagraphStyle(i
);
764 wxString name
= d
->GetName().Lower();
765 if (name
.Find(wxT("normal")) != wxNOT_FOUND
|| name
.Find(normalTranslated
) != wxNOT_FOUND
||
766 name
.Find(wxT("default")) != wxNOT_FOUND
|| name
.Find(defaultTranslated
) != wxNOT_FOUND
)
768 wxRichTextAttr
attr2(d
->GetStyleMergedWithBase(GetStyleSheet()));
769 if (attr2
.HasFontSize())
771 stdFontSize
= attr2
.GetFontSize();
777 if (stdFontSize
== 0)
779 // Look at sizes up to 20 points, and see which is the most common
782 for (i
= 0; i
<= maxSize
; i
++)
784 for (i
= 0; i
< m_styleNames
.GetCount(); i
++)
786 wxRichTextStyleDefinition
* d
= GetStyle(i
);
789 wxRichTextAttr
attr2(d
->GetStyleMergedWithBase(GetStyleSheet()));
790 if (attr2
.HasFontSize())
792 if (attr2
.GetFontSize() <= (int) maxSize
)
793 sizes
[attr2
.GetFontSize()] ++;
797 int mostCommonSize
= 0;
798 for (i
= 0; i
<= maxSize
; i
++)
800 if (sizes
[i
] > mostCommonSize
)
803 if (mostCommonSize
> 0)
804 stdFontSize
= mostCommonSize
;
807 if (stdFontSize
== 0)
810 int thisFontSize
= ((attr
.GetFlags() & wxTEXT_ATTR_FONT_SIZE
) != 0) ? attr
.GetFontSize() : stdFontSize
;
812 if (thisFontSize
< stdFontSize
)
814 else if (thisFontSize
> stdFontSize
)
819 str
<< wxT(" size=") << size
;
821 if (!attr
.GetFontFaceName().IsEmpty())
822 str
<< wxT(" face=\"") << attr
.GetFontFaceName() << wxT("\"");
824 if (attr
.GetTextColour().IsOk())
825 str
<< wxT(" color=\"#") << ColourToHexString(attr
.GetTextColour()) << wxT("\"");
829 bool hasBold
= false;
830 bool hasItalic
= false;
831 bool hasUnderline
= false;
833 if (attr
.GetFontWeight() == wxBOLD
)
835 if (attr
.GetFontStyle() == wxITALIC
)
837 if (attr
.GetFontUnderlined())
847 str
+= def
->GetName();
857 str
<< wxT("</centre>");
859 str
<< wxT("</font>");
861 str
<< wxT("</td></tr></table>");
864 str
<< wxT("</center>");
869 // Convert units in tends of a millimetre to device units
870 int wxRichTextStyleListBox::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const
872 int ppi
= dc
.GetPPI().x
;
874 // There are ppi pixels in 254.1 "1/10 mm"
876 double pixels
= ((double) units
* (double)ppi
) / 254.1;
881 void wxRichTextStyleListBox::OnLeftDown(wxMouseEvent
& event
)
883 wxVListBox::OnLeftDown(event
);
885 int item
= VirtualHitTest(event
.GetPosition().y
);
886 if (item
!= wxNOT_FOUND
&& GetApplyOnSelection())
890 void wxRichTextStyleListBox::OnLeftDoubleClick(wxMouseEvent
& event
)
892 wxVListBox::OnLeftDown(event
);
894 int item
= VirtualHitTest(event
.GetPosition().y
);
895 if (item
!= wxNOT_FOUND
&& !GetApplyOnSelection())
899 /// Helper for listbox and combo control
900 wxString
wxRichTextStyleListBox::GetStyleToShowInIdleTime(wxRichTextCtrl
* ctrl
, wxRichTextStyleType styleType
)
902 int adjustedCaretPos
= ctrl
->GetAdjustedCaretPosition(ctrl
->GetCaretPosition());
907 ctrl
->GetStyle(adjustedCaretPos
, attr
);
909 // Take into account current default style just chosen by user
910 if (ctrl
->IsDefaultStyleShowing())
912 wxRichTextApplyStyle(attr
, ctrl
->GetDefaultStyleEx());
914 if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_CHARACTER
) &&
915 !attr
.GetCharacterStyleName().IsEmpty())
916 styleName
= attr
.GetCharacterStyleName();
917 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_PARAGRAPH
) &&
918 !attr
.GetParagraphStyleName().IsEmpty())
919 styleName
= attr
.GetParagraphStyleName();
920 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_LIST
) &&
921 !attr
.GetListStyleName().IsEmpty())
922 styleName
= attr
.GetListStyleName();
923 // TODO: when we have a concept of focused object (text box), we'll
924 // use the paragraph style name of the focused object as the frame style name.
926 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_BOX
) &&
927 !attr
.GetBoxStyleName().IsEmpty())
928 styleName
= attr
.GetBoxStyleName();
931 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_CHARACTER
) &&
932 !attr
.GetCharacterStyleName().IsEmpty())
934 styleName
= attr
.GetCharacterStyleName();
936 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_PARAGRAPH
) &&
937 !attr
.GetParagraphStyleName().IsEmpty())
939 styleName
= attr
.GetParagraphStyleName();
941 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_LIST
) &&
942 !attr
.GetListStyleName().IsEmpty())
944 styleName
= attr
.GetListStyleName();
950 /// Auto-select from style under caret in idle time
951 void wxRichTextStyleListBox::OnIdle(wxIdleEvent
& event
)
953 if (CanAutoSetSelection() && GetRichTextCtrl() && IsShownOnScreen() && wxWindow::FindFocus() != this)
955 wxString styleName
= GetStyleToShowInIdleTime(GetRichTextCtrl(), GetStyleType());
957 int sel
= GetSelection();
958 if (!styleName
.IsEmpty())
960 // Don't do the selection if it's already set
961 if (sel
== GetIndexForStyle(styleName
))
964 SetStyleSelection(styleName
);
973 void wxRichTextStyleListBox::ApplyStyle(int item
)
975 if ( item
!= wxNOT_FOUND
)
977 wxRichTextStyleDefinition
* def
= GetStyle(item
);
978 if (def
&& GetRichTextCtrl())
980 GetRichTextCtrl()->ApplyStyle(def
);
981 GetRichTextCtrl()->SetFocus();
987 * wxRichTextStyleListCtrl class: manages a listbox and a choice control to
988 * switch shown style types
991 IMPLEMENT_CLASS(wxRichTextStyleListCtrl
, wxControl
)
993 BEGIN_EVENT_TABLE(wxRichTextStyleListCtrl
, wxControl
)
994 EVT_CHOICE(wxID_ANY
, wxRichTextStyleListCtrl::OnChooseType
)
995 EVT_SIZE(wxRichTextStyleListCtrl::OnSize
)
998 wxRichTextStyleListCtrl::wxRichTextStyleListCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
999 const wxSize
& size
, long style
)
1002 Create(parent
, id
, pos
, size
, style
);
1005 bool wxRichTextStyleListCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
1006 const wxSize
& size
, long style
)
1008 if ((style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
1009 style
|= wxBORDER_THEME
;
1011 wxControl::Create(parent
, id
, pos
, size
, style
);
1013 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1014 if (size
!= wxDefaultSize
)
1015 SetInitialSize(size
);
1017 bool showSelector
= ((style
& wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR
) == 0);
1019 wxBorder listBoxStyle
;
1021 listBoxStyle
= wxBORDER_THEME
;
1023 listBoxStyle
= wxBORDER_NONE
;
1025 m_styleListBox
= new wxRichTextStyleListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, listBoxStyle
);
1027 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
1031 wxArrayString choices
;
1032 choices
.Add(_("All styles"));
1033 choices
.Add(_("Paragraph styles"));
1034 choices
.Add(_("Character styles"));
1035 choices
.Add(_("List styles"));
1036 choices
.Add(_("Box styles"));
1038 m_styleChoice
= new wxChoice(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, choices
);
1040 boxSizer
->Add(m_styleListBox
, 1, wxALL
|wxEXPAND
, 5);
1041 boxSizer
->Add(m_styleChoice
, 0, wxLEFT
|wxRIGHT
|wxBOTTOM
|wxEXPAND
, 5);
1045 boxSizer
->Add(m_styleListBox
, 1, wxALL
|wxEXPAND
, 0);
1051 m_dontUpdate
= true;
1055 int i
= StyleTypeToIndex(m_styleListBox
->GetStyleType());
1056 m_styleChoice
->SetSelection(i
);
1059 m_dontUpdate
= false;
1064 wxRichTextStyleListCtrl::~wxRichTextStyleListCtrl()
1069 /// React to style type choice
1070 void wxRichTextStyleListCtrl::OnChooseType(wxCommandEvent
& event
)
1072 if (event
.GetEventObject() != m_styleChoice
)
1079 wxRichTextStyleListBox::wxRichTextStyleType styleType
= StyleIndexToType(event
.GetSelection());
1080 m_styleListBox
->SetSelection(-1);
1081 m_styleListBox
->SetStyleType(styleType
);
1085 /// Lay out the controls
1086 void wxRichTextStyleListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1088 if (GetAutoLayout())
1092 /// Get the choice index for style type
1093 int wxRichTextStyleListCtrl::StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType
)
1095 if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
)
1099 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH
)
1103 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
)
1107 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST
)
1111 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_BOX
)
1118 /// Get the style type for choice index
1119 wxRichTextStyleListBox::wxRichTextStyleType
wxRichTextStyleListCtrl::StyleIndexToType(int i
)
1122 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH
;
1124 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
;
1126 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST
;
1128 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_BOX
;
1130 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
;
1133 /// Associates the control with a style manager
1134 void wxRichTextStyleListCtrl::SetStyleSheet(wxRichTextStyleSheet
* styleSheet
)
1137 m_styleListBox
->SetStyleSheet(styleSheet
);
1140 wxRichTextStyleSheet
* wxRichTextStyleListCtrl::GetStyleSheet() const
1143 return m_styleListBox
->GetStyleSheet();
1148 /// Associates the control with a wxRichTextCtrl
1149 void wxRichTextStyleListCtrl::SetRichTextCtrl(wxRichTextCtrl
* ctrl
)
1152 m_styleListBox
->SetRichTextCtrl(ctrl
);
1155 wxRichTextCtrl
* wxRichTextStyleListCtrl::GetRichTextCtrl() const
1158 return m_styleListBox
->GetRichTextCtrl();
1163 /// Set/get the style type to display
1164 void wxRichTextStyleListCtrl::SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType
)
1166 if ( !m_styleListBox
)
1169 m_styleListBox
->SetStyleType(styleType
);
1171 m_dontUpdate
= true;
1175 int i
= StyleTypeToIndex(m_styleListBox
->GetStyleType());
1176 m_styleChoice
->SetSelection(i
);
1179 m_dontUpdate
= false;
1182 wxRichTextStyleListBox::wxRichTextStyleType
wxRichTextStyleListCtrl::GetStyleType() const
1185 return m_styleListBox
->GetStyleType();
1187 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
;
1190 /// Updates the style list box
1191 void wxRichTextStyleListCtrl::UpdateStyles()
1194 m_styleListBox
->UpdateStyles();
1200 * Style drop-down for a wxComboCtrl
1204 BEGIN_EVENT_TABLE(wxRichTextStyleComboPopup
, wxRichTextStyleListBox
)
1205 EVT_MOTION(wxRichTextStyleComboPopup::OnMouseMove
)
1206 EVT_LEFT_DOWN(wxRichTextStyleComboPopup::OnMouseClick
)
1209 bool wxRichTextStyleComboPopup::Create( wxWindow
* parent
)
1211 int borderStyle
= GetDefaultBorder();
1212 if (borderStyle
== wxBORDER_SUNKEN
|| borderStyle
== wxBORDER_NONE
)
1213 borderStyle
= wxBORDER_THEME
;
1215 return wxRichTextStyleListBox::Create(parent
, wxID_ANY
,
1216 wxPoint(0,0), wxDefaultSize
,
1220 void wxRichTextStyleComboPopup::SetStringValue( const wxString
& s
)
1222 m_value
= SetStyleSelection(s
);
1225 wxString
wxRichTextStyleComboPopup::GetStringValue() const
1230 wxRichTextStyleDefinition
* def
= GetStyle(sel
);
1232 return def
->GetName();
1234 return wxEmptyString
;
1238 // Popup event handlers
1241 // Mouse hot-tracking
1242 void wxRichTextStyleComboPopup::OnMouseMove(wxMouseEvent
& event
)
1244 // Move selection to cursor if it is inside the popup
1246 int itemHere
= wxRichTextStyleListBox::VirtualHitTest(event
.GetPosition().y
);
1247 if ( itemHere
>= 0 )
1249 wxRichTextStyleListBox::SetSelection(itemHere
);
1250 m_itemHere
= itemHere
;
1255 // On mouse left, set the value and close the popup
1256 void wxRichTextStyleComboPopup::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
1258 if (m_itemHere
>= 0)
1259 m_value
= m_itemHere
;
1261 // Ordering is important, so we don't dismiss this popup accidentally
1262 // by setting the focus elsewhere e.g. in ApplyStyle
1265 if (m_itemHere
>= 0)
1266 wxRichTextStyleListBox::ApplyStyle(m_itemHere
);
1270 * wxRichTextStyleComboCtrl
1271 * A combo for applying styles.
1274 IMPLEMENT_CLASS(wxRichTextStyleComboCtrl
, wxComboCtrl
)
1276 BEGIN_EVENT_TABLE(wxRichTextStyleComboCtrl
, wxComboCtrl
)
1277 EVT_IDLE(wxRichTextStyleComboCtrl::OnIdle
)
1280 bool wxRichTextStyleComboCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
1281 const wxSize
& size
, long style
)
1283 if (!wxComboCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, style
))
1286 SetPopupMaxHeight(400);
1288 m_stylePopup
= new wxRichTextStyleComboPopup
;
1290 SetPopupControl(m_stylePopup
);
1295 /// Auto-select from style under caret in idle time
1297 // TODO: must be able to show italic, bold, combinations
1298 // in style box. Do we have a concept of automatic, temporary
1299 // styles that are added whenever we wish to show a style
1300 // that doesn't exist already? E.g. "Bold, Italic, Underline".
1301 // Word seems to generate these things on the fly.
1302 // If there's a named style already, it uses e.g. Heading1 + Bold, Italic
1303 // If you unembolden text in a style that has bold, it uses the
1305 // TODO: order styles alphabetically. This means indexes can change,
1306 // so need a different way to specify selections, i.e. by name.
1308 void wxRichTextStyleComboCtrl::OnIdle(wxIdleEvent
& event
)
1312 if ( !m_stylePopup
)
1315 wxRichTextCtrl
* const richtext
= GetRichTextCtrl();
1319 if ( !IsPopupShown() && IsShownOnScreen() && wxWindow::FindFocus() != this )
1321 wxString styleName
=
1322 wxRichTextStyleListBox::GetStyleToShowInIdleTime(richtext
, m_stylePopup
->GetStyleType());
1324 wxString currentValue
= GetValue();
1325 if (!styleName
.IsEmpty())
1327 // Don't do the selection if it's already set
1328 if (currentValue
== styleName
)
1331 SetValue(styleName
);
1333 else if (!currentValue
.IsEmpty())
1334 SetValue(wxEmptyString
);