1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextstyles.cpp
3 // Purpose: Style management for wxRichTextCtrl
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/richtext/richtextstyles.h"
26 #include "wx/filename.h"
27 #include "wx/clipbrd.h"
28 #include "wx/wfstream.h"
29 #include "wx/settings.h"
31 #include "wx/richtext/richtextctrl.h"
33 IMPLEMENT_CLASS(wxRichTextStyleDefinition
, wxObject
)
34 IMPLEMENT_CLASS(wxRichTextCharacterStyleDefinition
, wxRichTextStyleDefinition
)
35 IMPLEMENT_CLASS(wxRichTextParagraphStyleDefinition
, wxRichTextStyleDefinition
)
36 IMPLEMENT_CLASS(wxRichTextListStyleDefinition
, wxRichTextParagraphStyleDefinition
)
37 IMPLEMENT_CLASS(wxRichTextBoxStyleDefinition
, wxRichTextStyleDefinition
)
43 void wxRichTextStyleDefinition::Copy(const wxRichTextStyleDefinition
& def
)
46 m_baseStyle
= def
.m_baseStyle
;
47 m_style
= def
.m_style
;
48 m_description
= def
.m_description
;
49 m_properties
= def
.m_properties
;
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
&& m_properties
== def
.m_properties
);
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(wxCLASSINFO(wxRichTextParagraphStyleDefinition
));
64 bool isCharStyle
= IsKindOf(wxCLASSINFO(wxRichTextCharacterStyleDefinition
));
65 bool isListStyle
= IsKindOf(wxCLASSINFO(wxRichTextListStyleDefinition
));
66 bool isBoxStyle
= IsKindOf(wxCLASSINFO(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());
526 m_properties
= sheet
.m_properties
;
530 bool wxRichTextStyleSheet::operator==(const wxRichTextStyleSheet
& WXUNUSED(sheet
)) const
539 // Functions for dealing with clashing names for different kinds of style.
540 // Returns "P", "C", "L" or "B" (paragraph, character, list or box) for
541 // style name | type.
542 static wxString
wxGetRichTextStyleType(const wxString
& style
)
544 return style
.AfterLast(wxT('|'));
547 static wxString
wxGetRichTextStyle(const wxString
& style
)
549 return style
.BeforeLast(wxT('|'));
554 * wxRichTextStyleListBox: a listbox to display styles.
557 IMPLEMENT_CLASS(wxRichTextStyleListBox
, wxHtmlListBox
)
559 BEGIN_EVENT_TABLE(wxRichTextStyleListBox
, wxHtmlListBox
)
560 EVT_LEFT_DOWN(wxRichTextStyleListBox::OnLeftDown
)
561 EVT_LEFT_DCLICK(wxRichTextStyleListBox::OnLeftDoubleClick
)
562 EVT_IDLE(wxRichTextStyleListBox::OnIdle
)
565 wxRichTextStyleListBox::wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
566 const wxSize
& size
, long style
)
569 Create(parent
, id
, pos
, size
, style
);
572 bool wxRichTextStyleListBox::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
573 const wxSize
& size
, long style
)
575 return wxHtmlListBox::Create(parent
, id
, pos
, size
, style
);
578 wxRichTextStyleListBox::~wxRichTextStyleListBox()
582 /// Returns the HTML for this item
583 wxString
wxRichTextStyleListBox::OnGetItem(size_t n
) const
585 if (!GetStyleSheet())
586 return wxEmptyString
;
588 wxRichTextStyleDefinition
* def
= GetStyle(n
);
590 return CreateHTML(def
);
592 return wxEmptyString
;
595 // Get style for index
596 wxRichTextStyleDefinition
* wxRichTextStyleListBox::GetStyle(size_t i
) const
598 if (!GetStyleSheet())
601 if (i
>= m_styleNames
.GetCount() /* || i < 0 */ )
604 wxString styleType
= wxGetRichTextStyleType(m_styleNames
[i
]);
605 wxString style
= wxGetRichTextStyle(m_styleNames
[i
]);
606 if (styleType
== wxT("P"))
607 return GetStyleSheet()->FindParagraphStyle(style
);
608 else if (styleType
== wxT("C"))
609 return GetStyleSheet()->FindCharacterStyle(style
);
610 else if (styleType
== wxT("L"))
611 return GetStyleSheet()->FindListStyle(style
);
612 else if (styleType
== wxT("B"))
613 return GetStyleSheet()->FindBoxStyle(style
);
615 return GetStyleSheet()->FindStyle(style
);
619 void wxRichTextStyleListBox::UpdateStyles()
623 int oldSel
= GetSelection();
625 SetSelection(wxNOT_FOUND
);
627 m_styleNames
.Clear();
630 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
|| GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH
)
632 for (i
= 0; i
< GetStyleSheet()->GetParagraphStyleCount(); i
++)
633 m_styleNames
.Add(GetStyleSheet()->GetParagraphStyle(i
)->GetName() + wxT("|P"));
635 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
|| GetStyleType() == wxRICHTEXT_STYLE_CHARACTER
)
637 for (i
= 0; i
< GetStyleSheet()->GetCharacterStyleCount(); i
++)
638 m_styleNames
.Add(GetStyleSheet()->GetCharacterStyle(i
)->GetName() + wxT("|C"));
640 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
|| GetStyleType() == wxRICHTEXT_STYLE_LIST
)
642 for (i
= 0; i
< GetStyleSheet()->GetListStyleCount(); i
++)
643 m_styleNames
.Add(GetStyleSheet()->GetListStyle(i
)->GetName() + wxT("|L"));
645 if (GetStyleType() == wxRICHTEXT_STYLE_ALL
|| GetStyleType() == wxRICHTEXT_STYLE_BOX
)
647 for (i
= 0; i
< GetStyleSheet()->GetBoxStyleCount(); i
++)
648 m_styleNames
.Add(GetStyleSheet()->GetBoxStyle(i
)->GetName() + wxT("|B"));
652 SetItemCount(m_styleNames
.GetCount());
657 if (oldSel
>= 0 && oldSel
< (int) GetItemCount())
659 else if (GetItemCount() > 0)
664 SetSelection(newSel
);
670 m_styleNames
.Clear();
671 SetSelection(wxNOT_FOUND
);
677 // Get index for style name
678 int wxRichTextStyleListBox::GetIndexForStyle(const wxString
& name
) const
681 if (GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH
)
683 else if (GetStyleType() == wxRICHTEXT_STYLE_CHARACTER
)
685 else if (GetStyleType() == wxRICHTEXT_STYLE_LIST
)
687 else if (GetStyleType() == wxRICHTEXT_STYLE_BOX
)
691 if (m_styleNames
.Index(s
+ wxT("|P")) != wxNOT_FOUND
)
693 else if (m_styleNames
.Index(s
+ wxT("|C")) != wxNOT_FOUND
)
695 else if (m_styleNames
.Index(s
+ wxT("|L")) != wxNOT_FOUND
)
697 else if (m_styleNames
.Index(s
+ wxT("|B")) != wxNOT_FOUND
)
700 return m_styleNames
.Index(s
);
703 /// Set selection for string
704 int wxRichTextStyleListBox::SetStyleSelection(const wxString
& name
)
706 int i
= GetIndexForStyle(name
);
716 // Convert a colour to a 6-digit hex string
717 static wxString
ColourToHexString(const wxColour
& col
)
721 hex
+= wxDecToHex(col
.Red());
722 hex
+= wxDecToHex(col
.Green());
723 hex
+= wxDecToHex(col
.Blue());
728 /// Creates a suitable HTML fragment for a definition
729 wxString
wxRichTextStyleListBox::CreateHTML(wxRichTextStyleDefinition
* def
) const
731 // TODO: indicate list format for list style types
735 bool isCentred
= false;
737 wxRichTextAttr
attr(def
->GetStyleMergedWithBase(GetStyleSheet()));
739 if (attr
.HasAlignment() && attr
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
742 str
<< wxT("<html><head></head>");
744 if (attr
.GetBackgroundColour().Ok())
745 str
<< wxT(" bgcolor=\"#") << ColourToHexString(attr
.GetBackgroundColour()) << wxT("\"");
749 str
<< wxT("<center>");
751 str
<< wxT("<table");
752 if (attr
.GetBackgroundColour().Ok())
753 str
<< wxT(" bgcolor=\"#") << ColourToHexString(attr
.GetBackgroundColour()) << wxT("\"");
757 if (attr
.GetLeftIndent() > 0)
759 wxClientDC
dc((wxWindow
*) this);
761 str
<< wxT("<td width=") << wxMin(50, (ConvertTenthsMMToPixels(dc
, attr
.GetLeftIndent())/2)) << wxT("></td>");
765 str
<< wxT("<td nowrap align=\"center\">");
767 str
<< wxT("<td nowrap>");
775 // Guess a standard font size
778 // First see if we have a default/normal style to base the size on
779 wxString
normalTranslated(_("normal"));
780 wxString
defaultTranslated(_("default"));
782 for (i
= 0; i
< GetStyleSheet()->GetParagraphStyleCount(); i
++)
784 wxRichTextStyleDefinition
* d
= GetStyleSheet()->GetParagraphStyle(i
);
785 wxString name
= d
->GetName().Lower();
786 if (name
.Find(wxT("normal")) != wxNOT_FOUND
|| name
.Find(normalTranslated
) != wxNOT_FOUND
||
787 name
.Find(wxT("default")) != wxNOT_FOUND
|| name
.Find(defaultTranslated
) != wxNOT_FOUND
)
789 wxRichTextAttr
attr2(d
->GetStyleMergedWithBase(GetStyleSheet()));
790 if (attr2
.HasFontPointSize())
792 stdFontSize
= attr2
.GetFontSize();
798 if (stdFontSize
== 0)
800 // Look at sizes up to 20 points, and see which is the most common
803 for (i
= 0; i
<= maxSize
; i
++)
805 for (i
= 0; i
< m_styleNames
.GetCount(); i
++)
807 wxRichTextStyleDefinition
* d
= GetStyle(i
);
810 wxRichTextAttr
attr2(d
->GetStyleMergedWithBase(GetStyleSheet()));
811 if (attr2
.HasFontPointSize())
813 if (attr2
.GetFontSize() <= (int) maxSize
)
814 sizes
[attr2
.GetFontSize()] ++;
818 int mostCommonSize
= 0;
819 for (i
= 0; i
<= maxSize
; i
++)
821 if (sizes
[i
] > mostCommonSize
)
824 if (mostCommonSize
> 0)
825 stdFontSize
= mostCommonSize
;
828 if (stdFontSize
== 0)
831 int thisFontSize
= attr
.HasFontPointSize() ? attr
.GetFontSize() : stdFontSize
;
833 if (thisFontSize
< stdFontSize
)
835 else if (thisFontSize
> stdFontSize
)
840 str
<< wxT(" size=") << size
;
842 if (!attr
.GetFontFaceName().IsEmpty())
843 str
<< wxT(" face=\"") << attr
.GetFontFaceName() << wxT("\"");
845 if (attr
.GetTextColour().IsOk())
846 str
<< wxT(" color=\"#") << ColourToHexString(attr
.GetTextColour()) << wxT("\"");
848 if (attr
.GetBackgroundColour().Ok())
849 str
<< wxT(" bgcolor=\"#") << ColourToHexString(attr
.GetBackgroundColour()) << wxT("\"");
853 bool hasBold
= false;
854 bool hasItalic
= false;
855 bool hasUnderline
= false;
857 if (attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
)
859 if (attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
)
861 if (attr
.GetFontUnderlined())
871 str
+= def
->GetName();
881 str
<< wxT("</centre>");
883 str
<< wxT("</font>");
885 str
<< wxT("</td></tr></table>");
888 str
<< wxT("</center>");
890 str
<< wxT("</body></html>");
894 // Convert units in tends of a millimetre to device units
895 int wxRichTextStyleListBox::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const
897 int ppi
= dc
.GetPPI().x
;
899 // There are ppi pixels in 254.1 "1/10 mm"
901 double pixels
= ((double) units
* (double)ppi
) / 254.1;
906 void wxRichTextStyleListBox::OnLeftDown(wxMouseEvent
& event
)
908 wxVListBox::OnLeftDown(event
);
910 int item
= VirtualHitTest(event
.GetPosition().y
);
911 if (item
!= wxNOT_FOUND
&& GetApplyOnSelection())
915 void wxRichTextStyleListBox::OnLeftDoubleClick(wxMouseEvent
& event
)
917 wxVListBox::OnLeftDown(event
);
919 int item
= VirtualHitTest(event
.GetPosition().y
);
920 if (item
!= wxNOT_FOUND
&& !GetApplyOnSelection())
924 /// Helper for listbox and combo control
925 wxString
wxRichTextStyleListBox::GetStyleToShowInIdleTime(wxRichTextCtrl
* ctrl
, wxRichTextStyleType styleType
)
927 int adjustedCaretPos
= ctrl
->GetAdjustedCaretPosition(ctrl
->GetCaretPosition());
932 ctrl
->GetStyle(adjustedCaretPos
, attr
);
934 // Take into account current default style just chosen by user
935 if (ctrl
->IsDefaultStyleShowing())
937 wxRichTextApplyStyle(attr
, ctrl
->GetDefaultStyleEx());
939 if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_CHARACTER
) &&
940 !attr
.GetCharacterStyleName().IsEmpty())
941 styleName
= attr
.GetCharacterStyleName();
942 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_PARAGRAPH
) &&
943 !attr
.GetParagraphStyleName().IsEmpty())
944 styleName
= attr
.GetParagraphStyleName();
945 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_LIST
) &&
946 !attr
.GetListStyleName().IsEmpty())
947 styleName
= attr
.GetListStyleName();
948 // TODO: when we have a concept of focused object (text box), we'll
949 // use the paragraph style name of the focused object as the frame style name.
951 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_BOX
) &&
952 !attr
.GetBoxStyleName().IsEmpty())
953 styleName
= attr
.GetBoxStyleName();
956 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_CHARACTER
) &&
957 !attr
.GetCharacterStyleName().IsEmpty())
959 styleName
= attr
.GetCharacterStyleName();
961 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_PARAGRAPH
) &&
962 !attr
.GetParagraphStyleName().IsEmpty())
964 styleName
= attr
.GetParagraphStyleName();
966 else if ((styleType
== wxRICHTEXT_STYLE_ALL
|| styleType
== wxRICHTEXT_STYLE_LIST
) &&
967 !attr
.GetListStyleName().IsEmpty())
969 styleName
= attr
.GetListStyleName();
975 /// Auto-select from style under caret in idle time
976 void wxRichTextStyleListBox::OnIdle(wxIdleEvent
& event
)
978 if (CanAutoSetSelection() && GetRichTextCtrl() && IsShownOnScreen() && wxWindow::FindFocus() != this)
980 wxString styleName
= GetStyleToShowInIdleTime(GetRichTextCtrl(), GetStyleType());
982 int sel
= GetSelection();
983 if (!styleName
.IsEmpty())
985 // Don't do the selection if it's already set
986 if (sel
== GetIndexForStyle(styleName
))
989 SetStyleSelection(styleName
);
998 void wxRichTextStyleListBox::ApplyStyle(int item
)
1000 if ( item
!= wxNOT_FOUND
)
1002 wxRichTextStyleDefinition
* def
= GetStyle(item
);
1003 if (def
&& GetRichTextCtrl())
1005 GetRichTextCtrl()->ApplyStyle(def
);
1006 GetRichTextCtrl()->SetFocus();
1012 * wxRichTextStyleListCtrl class: manages a listbox and a choice control to
1013 * switch shown style types
1016 IMPLEMENT_CLASS(wxRichTextStyleListCtrl
, wxControl
)
1018 BEGIN_EVENT_TABLE(wxRichTextStyleListCtrl
, wxControl
)
1019 EVT_CHOICE(wxID_ANY
, wxRichTextStyleListCtrl::OnChooseType
)
1020 EVT_SIZE(wxRichTextStyleListCtrl::OnSize
)
1023 wxRichTextStyleListCtrl::wxRichTextStyleListCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
1024 const wxSize
& size
, long style
)
1027 Create(parent
, id
, pos
, size
, style
);
1030 bool wxRichTextStyleListCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
1031 const wxSize
& size
, long style
)
1033 if ((style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
1034 style
|= wxBORDER_THEME
;
1036 wxControl::Create(parent
, id
, pos
, size
, style
);
1038 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1039 if (size
!= wxDefaultSize
)
1040 SetInitialSize(size
);
1042 bool showSelector
= ((style
& wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR
) == 0);
1044 wxBorder listBoxStyle
;
1046 listBoxStyle
= wxBORDER_THEME
;
1048 listBoxStyle
= wxBORDER_NONE
;
1050 m_styleListBox
= new wxRichTextStyleListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, listBoxStyle
);
1052 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
1056 wxArrayString choices
;
1057 choices
.Add(_("All styles"));
1058 choices
.Add(_("Paragraph styles"));
1059 choices
.Add(_("Character styles"));
1060 choices
.Add(_("List styles"));
1061 choices
.Add(_("Box styles"));
1063 m_styleChoice
= new wxChoice(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, choices
);
1065 boxSizer
->Add(m_styleListBox
, 1, wxALL
|wxEXPAND
, 5);
1066 boxSizer
->Add(m_styleChoice
, 0, wxLEFT
|wxRIGHT
|wxBOTTOM
|wxEXPAND
, 5);
1070 boxSizer
->Add(m_styleListBox
, 1, wxALL
|wxEXPAND
, 0);
1076 m_dontUpdate
= true;
1080 int i
= StyleTypeToIndex(m_styleListBox
->GetStyleType());
1081 m_styleChoice
->SetSelection(i
);
1084 m_dontUpdate
= false;
1089 wxRichTextStyleListCtrl::~wxRichTextStyleListCtrl()
1094 /// React to style type choice
1095 void wxRichTextStyleListCtrl::OnChooseType(wxCommandEvent
& event
)
1097 if (event
.GetEventObject() != m_styleChoice
)
1104 wxRichTextStyleListBox::wxRichTextStyleType styleType
= StyleIndexToType(event
.GetSelection());
1105 m_styleListBox
->SetSelection(-1);
1106 m_styleListBox
->SetStyleType(styleType
);
1110 /// Lay out the controls
1111 void wxRichTextStyleListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1113 if (GetAutoLayout())
1117 /// Get the choice index for style type
1118 int wxRichTextStyleListCtrl::StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType
)
1120 if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
)
1124 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH
)
1128 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
)
1132 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST
)
1136 else if (styleType
== wxRichTextStyleListBox::wxRICHTEXT_STYLE_BOX
)
1143 /// Get the style type for choice index
1144 wxRichTextStyleListBox::wxRichTextStyleType
wxRichTextStyleListCtrl::StyleIndexToType(int i
)
1147 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH
;
1149 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER
;
1151 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST
;
1153 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_BOX
;
1155 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
;
1158 /// Associates the control with a style manager
1159 void wxRichTextStyleListCtrl::SetStyleSheet(wxRichTextStyleSheet
* styleSheet
)
1162 m_styleListBox
->SetStyleSheet(styleSheet
);
1165 wxRichTextStyleSheet
* wxRichTextStyleListCtrl::GetStyleSheet() const
1168 return m_styleListBox
->GetStyleSheet();
1173 /// Associates the control with a wxRichTextCtrl
1174 void wxRichTextStyleListCtrl::SetRichTextCtrl(wxRichTextCtrl
* ctrl
)
1177 m_styleListBox
->SetRichTextCtrl(ctrl
);
1180 wxRichTextCtrl
* wxRichTextStyleListCtrl::GetRichTextCtrl() const
1183 return m_styleListBox
->GetRichTextCtrl();
1188 /// Set/get the style type to display
1189 void wxRichTextStyleListCtrl::SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType
)
1191 if ( !m_styleListBox
)
1194 m_styleListBox
->SetStyleType(styleType
);
1196 m_dontUpdate
= true;
1200 int i
= StyleTypeToIndex(m_styleListBox
->GetStyleType());
1201 m_styleChoice
->SetSelection(i
);
1204 m_dontUpdate
= false;
1207 wxRichTextStyleListBox::wxRichTextStyleType
wxRichTextStyleListCtrl::GetStyleType() const
1210 return m_styleListBox
->GetStyleType();
1212 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL
;
1215 /// Updates the style list box
1216 void wxRichTextStyleListCtrl::UpdateStyles()
1219 m_styleListBox
->UpdateStyles();
1225 * Style drop-down for a wxComboCtrl
1229 BEGIN_EVENT_TABLE(wxRichTextStyleComboPopup
, wxRichTextStyleListBox
)
1230 EVT_MOTION(wxRichTextStyleComboPopup::OnMouseMove
)
1231 EVT_LEFT_DOWN(wxRichTextStyleComboPopup::OnMouseClick
)
1234 bool wxRichTextStyleComboPopup::Create( wxWindow
* parent
)
1236 int borderStyle
= GetDefaultBorder();
1237 if (borderStyle
== wxBORDER_SUNKEN
|| borderStyle
== wxBORDER_NONE
)
1238 borderStyle
= wxBORDER_THEME
;
1240 return wxRichTextStyleListBox::Create(parent
, wxID_ANY
,
1241 wxPoint(0,0), wxDefaultSize
,
1245 void wxRichTextStyleComboPopup::SetStringValue( const wxString
& s
)
1247 m_value
= SetStyleSelection(s
);
1250 wxString
wxRichTextStyleComboPopup::GetStringValue() const
1255 wxRichTextStyleDefinition
* def
= GetStyle(sel
);
1257 return def
->GetName();
1259 return wxEmptyString
;
1263 // Popup event handlers
1266 // Mouse hot-tracking
1267 void wxRichTextStyleComboPopup::OnMouseMove(wxMouseEvent
& event
)
1269 // Move selection to cursor if it is inside the popup
1271 int itemHere
= wxRichTextStyleListBox::VirtualHitTest(event
.GetPosition().y
);
1272 if ( itemHere
>= 0 )
1274 wxRichTextStyleListBox::SetSelection(itemHere
);
1275 m_itemHere
= itemHere
;
1280 // On mouse left, set the value and close the popup
1281 void wxRichTextStyleComboPopup::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
1283 if (m_itemHere
>= 0)
1284 m_value
= m_itemHere
;
1286 // Ordering is important, so we don't dismiss this popup accidentally
1287 // by setting the focus elsewhere e.g. in ApplyStyle
1290 if (m_itemHere
>= 0)
1291 wxRichTextStyleListBox::ApplyStyle(m_itemHere
);
1295 * wxRichTextStyleComboCtrl
1296 * A combo for applying styles.
1299 IMPLEMENT_CLASS(wxRichTextStyleComboCtrl
, wxComboCtrl
)
1301 BEGIN_EVENT_TABLE(wxRichTextStyleComboCtrl
, wxComboCtrl
)
1302 EVT_IDLE(wxRichTextStyleComboCtrl::OnIdle
)
1305 bool wxRichTextStyleComboCtrl::Create(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
1306 const wxSize
& size
, long style
)
1308 if (!wxComboCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, style
))
1311 SetPopupMaxHeight(400);
1313 m_stylePopup
= new wxRichTextStyleComboPopup
;
1315 SetPopupControl(m_stylePopup
);
1320 /// Auto-select from style under caret in idle time
1322 // TODO: must be able to show italic, bold, combinations
1323 // in style box. Do we have a concept of automatic, temporary
1324 // styles that are added whenever we wish to show a style
1325 // that doesn't exist already? E.g. "Bold, Italic, Underline".
1326 // Word seems to generate these things on the fly.
1327 // If there's a named style already, it uses e.g. Heading1 + Bold, Italic
1328 // If you unembolden text in a style that has bold, it uses the
1330 // TODO: order styles alphabetically. This means indexes can change,
1331 // so need a different way to specify selections, i.e. by name.
1333 void wxRichTextStyleComboCtrl::OnIdle(wxIdleEvent
& event
)
1337 if ( !m_stylePopup
)
1340 wxRichTextCtrl
* const richtext
= GetRichTextCtrl();
1344 if ( !IsPopupShown() && IsShownOnScreen() && wxWindow::FindFocus() != this )
1346 wxString styleName
=
1347 wxRichTextStyleListBox::GetStyleToShowInIdleTime(richtext
, m_stylePopup
->GetStyleType());
1349 wxString currentValue
= GetValue();
1350 if (!styleName
.IsEmpty())
1352 // Don't do the selection if it's already set
1353 if (currentValue
== styleName
)
1356 SetValue(styleName
);
1358 else if (!currentValue
.IsEmpty())
1359 SetValue(wxEmptyString
);