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"
24 #include "wx/dcclient.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/richtextctrl.h"
34 IMPLEMENT_CLASS(wxRichTextStyleDefinition
, wxObject
)
35 IMPLEMENT_CLASS(wxRichTextCharacterStyleDefinition
, wxRichTextStyleDefinition
)
36 IMPLEMENT_CLASS(wxRichTextParagraphStyleDefinition
, wxRichTextStyleDefinition
)
42 IMPLEMENT_CLASS(wxRichTextStyleSheet
, wxObject
)
45 void wxRichTextStyleSheet::Init()
49 /// Add a definition to one of the style lists
50 bool wxRichTextStyleSheet::AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
)
58 bool wxRichTextStyleSheet::RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
)
60 wxList::compatibility_iterator node
= list
.Find(def
);
63 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
73 /// Find a definition by name
74 wxRichTextStyleDefinition
* wxRichTextStyleSheet::FindStyle(const wxList
& list
, const wxString
& name
) const
76 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
78 wxRichTextStyleDefinition
* def
= (wxRichTextStyleDefinition
*) node
->GetData();
79 if (def
->GetName().Lower() == name
.Lower())
86 void wxRichTextStyleSheet::DeleteStyles()
88 WX_CLEAR_LIST(wxList
, m_characterStyleDefinitions
);
89 WX_CLEAR_LIST(wxList
, m_paragraphStyleDefinitions
);
94 * wxRichTextStyleListBox class declaration
95 * A listbox to display styles.
98 IMPLEMENT_CLASS(wxRichTextStyleListBox
, wxHtmlListBox
)
100 BEGIN_EVENT_TABLE(wxRichTextStyleListBox
, wxHtmlListBox
)
101 EVT_LISTBOX(wxID_ANY
, wxRichTextStyleListBox::OnSelect
)
102 EVT_LEFT_DOWN(wxRichTextStyleListBox::OnLeftDown
)
105 wxRichTextStyleListBox::wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pos
,
106 const wxSize
& size
, long style
): wxHtmlListBox(parent
, id
, pos
, size
, style
)
109 m_richTextCtrl
= NULL
;
112 wxRichTextStyleListBox::~wxRichTextStyleListBox()
116 /// Returns the HTML for this item
117 wxString
wxRichTextStyleListBox::OnGetItem(size_t n
) const
119 if (!GetStyleSheet())
120 return wxEmptyString
;
122 // First paragraph styles, then character
123 if (n
< GetStyleSheet()->GetParagraphStyleCount())
125 wxRichTextParagraphStyleDefinition
* def
= GetStyleSheet()->GetParagraphStyle(n
);
127 wxString str
= CreateHTML(def
);
131 if ((n
- GetStyleSheet()->GetParagraphStyleCount()) < GetStyleSheet()->GetCharacterStyleCount())
133 wxRichTextCharacterStyleDefinition
* def
= GetStyleSheet()->GetCharacterStyle(n
- GetStyleSheet()->GetParagraphStyleCount());
135 wxString str
= CreateHTML(def
);
138 return wxEmptyString
;
141 // Get style for index
142 wxRichTextStyleDefinition
* wxRichTextStyleListBox::GetStyle(size_t i
) const
144 if (!GetStyleSheet())
147 // First paragraph styles, then character
148 if (i
< GetStyleSheet()->GetParagraphStyleCount())
149 return GetStyleSheet()->GetParagraphStyle(i
);
151 if ((i
- GetStyleSheet()->GetParagraphStyleCount()) < GetStyleSheet()->GetCharacterStyleCount())
152 return GetStyleSheet()->GetCharacterStyle(i
- GetStyleSheet()->GetParagraphStyleCount());
158 void wxRichTextStyleListBox::UpdateStyles()
162 SetItemCount(GetStyleSheet()->GetParagraphStyleCount()+GetStyleSheet()->GetCharacterStyleCount());
167 // Convert a colour to a 6-digit hex string
168 static wxString
ColourToHexString(const wxColour
& col
)
172 hex
+= wxDecToHex(col
.Red());
173 hex
+= wxDecToHex(col
.Green());
174 hex
+= wxDecToHex(col
.Blue());
179 /// Creates a suitable HTML fragment for a definition
180 wxString
wxRichTextStyleListBox::CreateHTML(wxRichTextStyleDefinition
* def
) const
182 wxString
str(wxT("<table><tr>"));
184 if (def
->GetStyle().GetLeftIndent() > 0)
186 wxClientDC
dc((wxWindow
*) this);
188 str
<< wxT("<td width=") << ConvertTenthsMMToPixels(dc
, def
->GetStyle().GetLeftIndent()) << wxT("></td>");
191 str
<< wxT("<td nowrap>");
195 // Standard size is 12, say
196 size
+= 12 - def
->GetStyle().GetFontSize();
200 str
<< wxT(" size=") << size
;
202 if (!def
->GetStyle().GetFontFaceName().IsEmpty())
203 str
<< wxT(" face=\"") << def
->GetStyle().GetFontFaceName() << wxT("\"");
205 if (def
->GetStyle().GetTextColour().Ok())
206 str
<< wxT(" color=\"#") << ColourToHexString(def
->GetStyle().GetTextColour()) << wxT("\"");
210 bool hasBold
= false;
211 bool hasItalic
= false;
212 bool hasUnderline
= false;
214 if (def
->GetStyle().GetFontWeight() == wxBOLD
)
216 if (def
->GetStyle().GetFontStyle() == wxITALIC
)
218 if (def
->GetStyle().GetFontUnderlined())
228 str
+= def
->GetName();
237 str
<< wxT("</font>");
239 str
+= wxT("</td></tr></table>");
243 // Convert units in tends of a millimetre to device units
244 int wxRichTextStyleListBox::ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const
246 int ppi
= dc
.GetPPI().x
;
248 // There are ppi pixels in 254.1 "1/10 mm"
250 double pixels
= ((double) units
* (double)ppi
) / 254.1;
255 /// React to selection
256 void wxRichTextStyleListBox::OnSelect(wxCommandEvent
& WXUNUSED(event
))
259 wxRichTextStyleDefinition
* def
= GetStyle(event
.GetSelection());
262 wxMessageBox(def
->GetName());
267 void wxRichTextStyleListBox::OnLeftDown(wxMouseEvent
& event
)
269 wxVListBox::OnLeftDown(event
);
271 int item
= HitTest(event
.GetPosition());
273 if ( item
!= wxNOT_FOUND
)
275 wxRichTextStyleDefinition
* def
= GetStyle(item
);
276 if (def
&& GetRichTextCtrl())
278 wxRichTextRange
range(m_richTextCtrl
->GetInsertionPoint(), m_richTextCtrl
->GetInsertionPoint());
280 // Flags are defined within each definition, so only certain
281 // attributes are applied.
282 wxRichTextAttr
attr(def
->GetStyle());
284 if (m_richTextCtrl
->HasSelection())
285 m_richTextCtrl
->SetStyle(m_richTextCtrl
->GetSelectionRange(), attr
);
287 m_richTextCtrl
->SetDefaultStyle(attr
);
289 m_richTextCtrl
->SetFocus();
295 wxColour
wxRichTextStyleListBox::GetSelectedTextColour(const wxColour
& colFg
) const
300 wxColour
wxRichTextStyleListBox::GetSelectedTextBgColour(const wxColour
& colBg
) const