1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtextstyles.h
3 // Purpose: Style management for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_RICHTEXTSTYLES_H_
13 #define _WX_RICHTEXTSTYLES_H_
19 #include "wx/richtext/richtextbuffer.h"
24 #include "wx/htmllbox.h"
32 * Forward declarations
35 class WXDLLIMPEXP_RICHTEXT wxRichTextCtrl
;
36 class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer
;
39 * wxRichTextStyleDefinition class declaration
40 * A base class for paragraph and character styles.
43 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleDefinition
: public wxObject
45 DECLARE_CLASS(wxRichTextStyleDefinition
)
50 wxRichTextStyleDefinition(const wxString
& name
= wxEmptyString
) { Init(); m_name
= name
; }
51 virtual ~wxRichTextStyleDefinition() {}
55 /// The name of the style.
56 void SetName(const wxString
& name
) { m_name
= name
; }
57 const wxString
& GetName() const { return m_name
; }
59 /// The name of the style that this style is based on.
60 void SetBaseStyle(const wxString
& name
) { m_baseStyle
= name
; }
61 const wxString
& GetBaseStyle() const { return m_baseStyle
; }
64 void SetStyle(const wxRichTextAttr
& style
) { m_style
= style
; }
65 const wxRichTextAttr
& GetStyle() const { return m_style
; }
66 wxRichTextAttr
& GetStyle() { return m_style
; }
71 wxRichTextAttr m_style
;
75 * wxRichTextCharacterStyleDefinition class declaration
78 class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition
: public wxRichTextStyleDefinition
80 DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition
)
85 wxRichTextCharacterStyleDefinition(const wxString
& name
= wxEmptyString
):
86 wxRichTextStyleDefinition(name
) {}
87 virtual ~wxRichTextCharacterStyleDefinition() {}
93 * wxRichTextParagraphStyleDefinition class declaration
96 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition
: public wxRichTextStyleDefinition
98 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition
)
103 wxRichTextParagraphStyleDefinition(const wxString
& name
= wxEmptyString
):
104 wxRichTextStyleDefinition(name
) {}
105 virtual ~wxRichTextParagraphStyleDefinition() {}
108 void SetNextStyle(const wxString
& name
) { m_nextStyle
= name
; }
109 const wxString
& GetNextStyle() const { return m_nextStyle
; }
113 /// The next style to use when adding a paragraph after this style.
114 wxString m_nextStyle
;
121 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet
: public wxObject
123 DECLARE_CLASS( wxRichTextStyleSheet
)
127 wxRichTextStyleSheet() { Init(); }
128 virtual ~wxRichTextStyleSheet() { DeleteStyles(); }
133 /// Add a definition to the character style list
134 bool AddCharacterStyle(wxRichTextCharacterStyleDefinition
* def
) { return AddStyle(m_characterStyleDefinitions
, def
); }
136 /// Add a definition to the paragraph style list
137 bool AddParagraphStyle(wxRichTextParagraphStyleDefinition
* def
) { return AddStyle(m_paragraphStyleDefinitions
, def
); }
139 /// Remove a character style
140 bool RemoveCharacterStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_characterStyleDefinitions
, def
, deleteStyle
); }
142 /// Remove a paragraph style
143 bool RemoveParagraphStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_characterStyleDefinitions
, def
, deleteStyle
); }
145 /// Find a character definition by name
146 wxRichTextCharacterStyleDefinition
* FindCharacterStyle(const wxString
& name
) const { return (wxRichTextCharacterStyleDefinition
*) FindStyle(m_characterStyleDefinitions
, name
); }
148 /// Find a paragraph definition by name
149 wxRichTextParagraphStyleDefinition
* FindParagraphStyle(const wxString
& name
) const { return (wxRichTextParagraphStyleDefinition
*) FindStyle(m_characterStyleDefinitions
, name
); }
151 /// Return the number of character styes.
152 size_t GetCharacterStyleCount() const { return m_characterStyleDefinitions
.GetCount(); }
154 /// Return the number of paragraph styes.
155 size_t GetParagraphStyleCount() const { return m_paragraphStyleDefinitions
.GetCount(); }
157 /// Return the nth character style
158 wxRichTextCharacterStyleDefinition
* GetCharacterStyle(size_t n
) const { return (wxRichTextCharacterStyleDefinition
*) m_characterStyleDefinitions
.Item(n
)->GetData(); }
160 /// Return the nth paragraph style
161 wxRichTextParagraphStyleDefinition
* GetParagraphStyle(size_t n
) const { return (wxRichTextParagraphStyleDefinition
*) m_paragraphStyleDefinitions
.Item(n
)->GetData(); }
163 /// Delete all styles
168 /// Add a definition to one of the style lists
169 bool AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
);
172 bool RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
);
174 /// Find a definition by name
175 wxRichTextStyleDefinition
* FindStyle(const wxList
& list
, const wxString
& name
) const;
179 wxList m_characterStyleDefinitions
;
180 wxList m_paragraphStyleDefinitions
;
185 * wxRichTextStyleListBox class declaration
186 * A listbox to display styles.
189 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox
: public wxHtmlListBox
191 DECLARE_CLASS(wxRichTextStyleListBox
)
192 DECLARE_EVENT_TABLE()
195 wxRichTextStyleListBox()
199 wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
200 const wxSize
& size
= wxDefaultSize
, long style
= 0);
201 virtual ~wxRichTextStyleListBox();
206 m_richTextCtrl
= NULL
;
209 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
210 const wxSize
& size
= wxDefaultSize
, long style
= 0);
212 /// Creates a suitable HTML fragment for a definition
213 wxString
CreateHTML(wxRichTextStyleDefinition
* def
) const;
215 /// Associates the control with a style manager
216 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_styleSheet
= styleSheet
; }
217 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
219 /// Associates the control with a wxRichTextCtrl
220 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_richTextCtrl
= ctrl
; }
221 wxRichTextCtrl
* GetRichTextCtrl() const { return m_richTextCtrl
; }
223 /// Get style for index
224 wxRichTextStyleDefinition
* GetStyle(size_t i
) const ;
226 /// Get index for style name
227 int GetIndexForStyle(const wxString
& name
) const ;
229 /// Set selection for string, returning the index.
230 int SetStyleSelection(const wxString
& name
);
236 void DoSelection(int i
);
238 /// React to selection
239 void OnSelect(wxCommandEvent
& event
);
242 void OnLeftDown(wxMouseEvent
& event
);
244 /// Auto-select from style under caret in idle time
245 void OnIdle(wxIdleEvent
& event
);
248 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
249 virtual wxColour
GetSelectedTextBgColour(const wxColour
& colBg
) const;
252 /// Convert units in tends of a millimetre to device units
253 int ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const;
255 /// Can we set the selection based on the editor caret position?
256 /// Need to override this if being used in a combobox popup
257 virtual bool CanAutoSetSelection() { return true; }
260 /// Returns the HTML for this item
261 virtual wxString
OnGetItem(size_t n
) const;
265 wxRichTextStyleSheet
* m_styleSheet
;
266 wxRichTextCtrl
* m_richTextCtrl
;
272 * Style drop-down for a wxComboCtrl
275 class wxRichTextStyleComboPopup
: public wxRichTextStyleListBox
, public wxComboPopup
280 m_itemHere
= -1; // hot item in list
284 virtual bool Create( wxWindow
* parent
)
286 return wxRichTextStyleListBox::Create(parent
, wxID_ANY
,
287 wxPoint(0,0), wxDefaultSize
,
291 virtual wxWindow
*GetControl() { return this; }
293 virtual void SetStringValue( const wxString
& s
);
295 virtual wxString
GetStringValue() const;
297 /// Can we set the selection based on the editor caret position?
298 // virtual bool CanAutoSetSelection() { return ((m_combo == NULL) || !m_combo->IsPopupShown()); }
299 virtual bool CanAutoSetSelection() { return false; }
302 // Popup event handlers
305 // Mouse hot-tracking
306 void OnMouseMove(wxMouseEvent
& event
);
308 // On mouse left, set the value and close the popup
309 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
));
313 int m_itemHere
; // hot item in popup
317 DECLARE_EVENT_TABLE()
321 * wxRichTextStyleComboCtrl
322 * A combo for applying styles.
325 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleComboCtrl
: public wxComboCtrl
327 DECLARE_CLASS(wxRichTextStyleComboCtrl
)
328 DECLARE_EVENT_TABLE()
331 wxRichTextStyleComboCtrl()
336 wxRichTextStyleComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
337 const wxSize
& size
= wxDefaultSize
, long style
= wxCB_READONLY
)
340 Create(parent
, id
, pos
, size
, style
);
343 virtual ~wxRichTextStyleComboCtrl() {}
350 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
351 const wxSize
& size
= wxDefaultSize
, long style
= 0);
354 void UpdateStyles() { m_stylePopup
->UpdateStyles(); }
356 /// Associates the control with a style manager
357 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_stylePopup
->SetStyleSheet(styleSheet
); }
358 wxRichTextStyleSheet
* GetStyleSheet() const { return m_stylePopup
->GetStyleSheet(); }
360 /// Associates the control with a wxRichTextCtrl
361 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_stylePopup
->SetRichTextCtrl(ctrl
); }
362 wxRichTextCtrl
* GetRichTextCtrl() const { return m_stylePopup
->GetRichTextCtrl(); }
364 /// Gets the style popup
365 wxRichTextStyleComboPopup
* GetStylePopup() const { return m_stylePopup
; }
367 /// Auto-select from style under caret in idle time
368 void OnIdle(wxIdleEvent
& event
);
371 wxRichTextStyleComboPopup
* m_stylePopup
;
384 // _WX_RICHTEXTSTYLES_H_