1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
27 #include "wx/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/wfstream.h"
30 #include "wx/module.h"
32 #include "wx/richtext/richtextstyles.h"
33 #include "wx/richtext/richtextctrl.h"
35 IMPLEMENT_CLASS(wxRichTextStyleDefinition
, wxObject
)
36 IMPLEMENT_CLASS(wxRichTextCharacterStyleDefinition
, wxRichTextStyleDefinition
)
37 IMPLEMENT_CLASS(wxRichTextParagraphStyleDefinition
, wxRichTextStyleDefinition
)
43 IMPLEMENT_CLASS(wxRichTextStyleSheet
, wxObject
)
46 void wxRichTextStyleSheet::Init()
50 /// Add a definition to one of the style lists
51 bool wxRichTextStyleSheet::AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
)
59 bool wxRichTextStyleSheet::RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
)
61 wxList::compatibility_iterator node
= list
.Find(def
);
64 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
74 /// Find a definition by name
75 wxRichTextStyleDefinition
* wxRichTextStyleSheet::FindStyle(const wxList
& list
, const wxString
& name
) const
77 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
79 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
80 if (def
->GetName().Lower() == name
.Lower())
87 void wxRichTextStyleSheet::DeleteStyles()
89 WX_CLEAR_LIST(wxList
, m_characterStyleDefinitions
);
90 WX_CLEAR_LIST(wxList
, m_paragraphStyleDefinitions
);
95 * wxRichTextStyleListBox class declaration
96 * A listbox to display styles.
99 IMPLEMENT_CLASS(wxRichTextStyleListBox
, wxHtmlListBox
)
101 BEGIN_EVENT_TABLE(wxRichTextStyleListBox
, wxHtmlListBox
)
102 EVT_LISTBOX(wxID_ANY
, wxRichTextStyleListBox::OnSelect
)
103 EVT_LEFT_DOWN(wxRichTextStyleListBox::OnLeftDown
)
106 wxRichTextStyleListBox::wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
107 const wxSize
& size
, long style
): wxHtmlListBox(parent
, id
, pos
, size
, style
)
110 m_richTextCtrl
= NULL
;
113 wxRichTextStyleListBox::~wxRichTextStyleListBox()
117 /// Returns the HTML for this item
118 wxString
wxRichTextStyleListBox::OnGetItem(size_t n
) const
120 if (!GetStyleSheet())
121 return wxEmptyString
;
123 // First paragraph styles, then character
124 if (n
< GetStyleSheet()->GetParagraphStyleCount())
126 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->GetParagraphStyle(n
);
128 wxString str
= CreateHTML(def
);
132 if ((n
- GetStyleSheet()->GetParagraphStyleCount()) < GetStyleSheet()->GetCharacterStyleCount())
134 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->GetCharacterStyle(n
- GetStyleSheet()->GetParagraphStyleCount());
136 wxString str
= CreateHTML(def
);
139 return wxEmptyString
;
142 // Get style for index
143 wxRichTextStyleDefinition
* wxRichTextStyleListBox::GetStyle(size_t i
) const
145 if (!GetStyleSheet())
148 // First paragraph styles, then character
149 if (i
< GetStyleSheet()->GetParagraphStyleCount())
150 return GetStyleSheet()->GetParagraphStyle(i
);
152 if ((i
- GetStyleSheet()->GetParagraphStyleCount()) < GetStyleSheet()->GetCharacterStyleCount())
153 return GetStyleSheet()->GetCharacterStyle(i
- GetStyleSheet()->GetParagraphStyleCount());
159 void wxRichTextStyleListBox::UpdateStyles()
163 SetItemCount(GetStyleSheet()->GetParagraphStyleCount()+GetStyleSheet()->GetCharacterStyleCount());
168 // Convert a colour to a 6-digit hex string
169 static wxString
ColourToHexString(const wxColour
& col
)
173 hex
+= wxDecToHex(col
.Red());
174 hex
+= wxDecToHex(col
.Green());
175 hex
+= wxDecToHex(col
.Blue());
180 /// Creates a suitable HTML fragment for a definition
181 wxString
wxRichTextStyleListBox::CreateHTML(wxRichTextStyleDefinition
* def
) const
183 wxString
str(wxT("<table><tr>"));
185 if (def
->GetStyle().GetLeftIndent() > 0)
187 wxClientDC
dc((wxWindow
*) this);
189 str
<< wxT("<td width=") << ConvertTenthsMMToPixels(dc
, def
->GetStyle().GetLeftIndent()) << wxT("></td>");
192 str
<< wxT("<td nowrap>");
196 // Standard size is 12, say
197 size
+= 12 - def
->GetStyle().GetFontSize();
201 str
<< wxT(" size=") << size
;
203 if (!def
->GetStyle().GetFontFaceName().IsEmpty())
204 str
<< wxT(" face=\"") << def
->GetStyle().GetFontFaceName() << wxT("\"");
206 if (def
->GetStyle().GetTextColour().Ok())
207 str
<< wxT(" color=\"#") << ColourToHexString(def
->GetStyle().GetTextColour()) << wxT("\"");
211 bool hasBold
= false;
212 bool hasItalic
= false;
213 bool hasUnderline
= false;
215 if (def
->GetStyle().GetFontWeight() == wxBOLD
)
217 if (def
->GetStyle().GetFontStyle() == wxITALIC
)
219 if (def
->GetStyle().GetFontUnderlined())
229 str
+= def
->GetName();
238 str
<< wxT("</font>");
240 str
+= wxT("</td></tr></table>");
244 // Convert units in tends of a millimetre to device units
245 int wxRichTextStyleListBox::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const
247 int ppi
= dc
.GetPPI().x
;
249 // There are ppi pixels in 254.1 "1/10 mm"
251 double pixels
= ((double) units
* (double)ppi
) / 254.1;
256 /// React to selection
257 void wxRichTextStyleListBox::OnSelect(wxCommandEvent
& WXUNUSED(event
))
260 wxRichTextStyleDefinition
* def
= GetStyle(event
.GetSelection());
263 wxMessageBox(def
->GetName());
268 void wxRichTextStyleListBox::OnLeftDown(wxMouseEvent
& event
)
270 wxVListBox::OnLeftDown(event
);
272 int item
= HitTest(event
.GetPosition());
274 if ( item
!= wxNOT_FOUND
)
276 wxRichTextStyleDefinition
* def
= GetStyle(item
);
277 if (def
&& GetRichTextCtrl())
279 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
281 // Flags are defined within each definition, so only certain
282 // attributes are applied.
283 wxRichTextAttr
attr(def
->GetStyle());
285 if (m_richTextCtrl
->HasSelection())
286 m_richTextCtrl
->SetStyle(m_richTextCtrl
->GetSelectionRange(), attr
);
288 m_richTextCtrl
->SetDefaultStyle(attr
);
290 m_richTextCtrl
->SetFocus();
296 wxColour
wxRichTextStyleListBox::GetSelectedTextColour(const wxColour
& colFg
) const
301 wxColour
wxRichTextStyleListBox::GetSelectedTextBgColour(const wxColour
& colBg
) const