1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/richtext/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_
23 #include "wx/richtext/richtextbuffer.h"
26 #include "wx/htmllbox.h"
34 * Forward declarations
37 class WXDLLIMPEXP_RICHTEXT wxRichTextCtrl
;
38 class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer
;
41 * wxRichTextStyleDefinition class declaration
42 * A base class for paragraph and character styles.
45 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleDefinition
: public wxObject
47 DECLARE_CLASS(wxRichTextStyleDefinition
)
52 wxRichTextStyleDefinition(const wxRichTextStyleDefinition
& def
)
57 wxRichTextStyleDefinition(const wxString
& name
= wxEmptyString
) { Init(); m_name
= name
; }
58 virtual ~wxRichTextStyleDefinition() {}
61 void Copy(const wxRichTextStyleDefinition
& def
);
62 bool Eq(const wxRichTextStyleDefinition
& def
) const;
63 void operator =(const wxRichTextStyleDefinition
& def
) { Copy(def
); }
64 bool operator ==(const wxRichTextStyleDefinition
& def
) const { return Eq(def
); }
65 virtual wxRichTextStyleDefinition
* Clone() const = 0;
67 /// The name of the style.
68 void SetName(const wxString
& name
) { m_name
= name
; }
69 const wxString
& GetName() const { return m_name
; }
71 /// The name of the style that this style is based on.
72 void SetBaseStyle(const wxString
& name
) { m_baseStyle
= name
; }
73 const wxString
& GetBaseStyle() const { return m_baseStyle
; }
76 void SetStyle(const wxRichTextAttr
& style
) { m_style
= style
; }
77 const wxRichTextAttr
& GetStyle() const { return m_style
; }
78 wxRichTextAttr
& GetStyle() { return m_style
; }
83 wxRichTextAttr m_style
;
87 * wxRichTextCharacterStyleDefinition class declaration
90 class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition
: public wxRichTextStyleDefinition
92 DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition
)
97 wxRichTextCharacterStyleDefinition(const wxRichTextCharacterStyleDefinition
& def
): wxRichTextStyleDefinition(def
) {}
98 wxRichTextCharacterStyleDefinition(const wxString
& name
= wxEmptyString
):
99 wxRichTextStyleDefinition(name
) {}
100 virtual ~wxRichTextCharacterStyleDefinition() {}
102 virtual wxRichTextStyleDefinition
* Clone() const { return new wxRichTextCharacterStyleDefinition(*this); }
108 * wxRichTextParagraphStyleDefinition class declaration
111 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition
: public wxRichTextStyleDefinition
113 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition
)
118 wxRichTextParagraphStyleDefinition(const wxRichTextParagraphStyleDefinition
& def
): wxRichTextStyleDefinition(def
) { m_nextStyle
= def
.m_nextStyle
; }
119 wxRichTextParagraphStyleDefinition(const wxString
& name
= wxEmptyString
):
120 wxRichTextStyleDefinition(name
) {}
121 virtual ~wxRichTextParagraphStyleDefinition() {}
124 void SetNextStyle(const wxString
& name
) { m_nextStyle
= name
; }
125 const wxString
& GetNextStyle() const { return m_nextStyle
; }
127 void Copy(const wxRichTextParagraphStyleDefinition
& def
);
128 void operator =(const wxRichTextParagraphStyleDefinition
& def
) { Copy(def
); }
129 bool operator ==(const wxRichTextParagraphStyleDefinition
& def
) const;
131 virtual wxRichTextStyleDefinition
* Clone() const { return new wxRichTextParagraphStyleDefinition(*this); }
135 /// The next style to use when adding a paragraph after this style.
136 wxString m_nextStyle
;
143 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet
: public wxObject
145 DECLARE_CLASS( wxRichTextStyleSheet
)
149 wxRichTextStyleSheet(const wxRichTextStyleSheet
& sheet
)
154 wxRichTextStyleSheet() { Init(); }
155 virtual ~wxRichTextStyleSheet() { DeleteStyles(); }
161 void Copy(const wxRichTextStyleSheet
& sheet
);
164 void operator=(const wxRichTextStyleSheet
& sheet
) { Copy(sheet
); }
167 bool operator==(const wxRichTextStyleSheet
& sheet
) const;
169 /// Add a definition to the character style list
170 bool AddCharacterStyle(wxRichTextCharacterStyleDefinition
* def
);
172 /// Add a definition to the paragraph style list
173 bool AddParagraphStyle(wxRichTextParagraphStyleDefinition
* def
);
175 /// Remove a character style
176 bool RemoveCharacterStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_characterStyleDefinitions
, def
, deleteStyle
); }
178 /// Remove a paragraph style
179 bool RemoveParagraphStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_paragraphStyleDefinitions
, def
, deleteStyle
); }
181 /// Find a character definition by name
182 wxRichTextCharacterStyleDefinition
* FindCharacterStyle(const wxString
& name
) const { return (wxRichTextCharacterStyleDefinition
*) FindStyle(m_characterStyleDefinitions
, name
); }
184 /// Find a paragraph definition by name
185 wxRichTextParagraphStyleDefinition
* FindParagraphStyle(const wxString
& name
) const { return (wxRichTextParagraphStyleDefinition
*) FindStyle(m_paragraphStyleDefinitions
, name
); }
187 /// Return the number of character styes.
188 size_t GetCharacterStyleCount() const { return m_characterStyleDefinitions
.GetCount(); }
190 /// Return the number of paragraph styes.
191 size_t GetParagraphStyleCount() const { return m_paragraphStyleDefinitions
.GetCount(); }
193 /// Return the nth character style
194 wxRichTextCharacterStyleDefinition
* GetCharacterStyle(size_t n
) const { return (wxRichTextCharacterStyleDefinition
*) m_characterStyleDefinitions
.Item(n
)->GetData(); }
196 /// Return the nth paragraph style
197 wxRichTextParagraphStyleDefinition
* GetParagraphStyle(size_t n
) const { return (wxRichTextParagraphStyleDefinition
*) m_paragraphStyleDefinitions
.Item(n
)->GetData(); }
199 /// Delete all styles
204 /// Add a definition to one of the style lists
205 bool AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
);
208 bool RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
);
210 /// Find a definition by name
211 wxRichTextStyleDefinition
* FindStyle(const wxList
& list
, const wxString
& name
) const;
215 wxList m_characterStyleDefinitions
;
216 wxList m_paragraphStyleDefinitions
;
221 * wxRichTextStyleListBox class declaration
222 * A listbox to display styles.
225 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox
: public wxHtmlListBox
227 DECLARE_CLASS(wxRichTextStyleListBox
)
228 DECLARE_EVENT_TABLE()
231 wxRichTextStyleListBox()
235 wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
236 const wxSize
& size
= wxDefaultSize
, long style
= 0);
237 virtual ~wxRichTextStyleListBox();
242 m_richTextCtrl
= NULL
;
243 m_applyOnSelection
= true;
246 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
247 const wxSize
& size
= wxDefaultSize
, long style
= 0);
249 /// Creates a suitable HTML fragment for a definition
250 wxString
CreateHTML(wxRichTextStyleDefinition
* def
) const;
252 /// Associates the control with a style manager
253 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_styleSheet
= styleSheet
; }
254 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
256 /// Associates the control with a wxRichTextCtrl
257 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_richTextCtrl
= ctrl
; }
258 wxRichTextCtrl
* GetRichTextCtrl() const { return m_richTextCtrl
; }
260 /// Get style for index
261 wxRichTextStyleDefinition
* GetStyle(size_t i
) const ;
263 /// Get index for style name
264 int GetIndexForStyle(const wxString
& name
) const ;
266 /// Set selection for string, returning the index.
267 int SetStyleSelection(const wxString
& name
);
273 void ApplyStyle(int i
);
275 /// React to selection
276 void OnSelect(wxCommandEvent
& event
);
279 void OnLeftDown(wxMouseEvent
& event
);
281 /// Auto-select from style under caret in idle time
282 void OnIdle(wxIdleEvent
& event
);
285 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
286 virtual wxColour
GetSelectedTextBgColour(const wxColour
& colBg
) const;
289 /// Convert units in tends of a millimetre to device units
290 int ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const;
292 /// Can we set the selection based on the editor caret position?
293 /// Need to override this if being used in a combobox popup
294 virtual bool CanAutoSetSelection() { return true; }
296 /// Set whether the style should be applied as soon as the item is selected (the default)
297 void SetApplyOnSelection(bool applyOnSel
) { m_applyOnSelection
= applyOnSel
; }
298 bool GetApplyOnSelection() const { return m_applyOnSelection
; }
301 /// Returns the HTML for this item
302 virtual wxString
OnGetItem(size_t n
) const;
306 wxRichTextStyleSheet
* m_styleSheet
;
307 wxRichTextCtrl
* m_richTextCtrl
;
308 bool m_applyOnSelection
; // if true, applies style on selection
314 * Style drop-down for a wxComboCtrl
317 class wxRichTextStyleComboPopup
: public wxRichTextStyleListBox
, public wxComboPopup
322 m_itemHere
= -1; // hot item in list
326 virtual bool Create( wxWindow
* parent
)
328 return wxRichTextStyleListBox::Create(parent
, wxID_ANY
,
329 wxPoint(0,0), wxDefaultSize
,
333 virtual wxWindow
*GetControl() { return this; }
335 virtual void SetStringValue( const wxString
& s
);
337 virtual wxString
GetStringValue() const;
339 /// Can we set the selection based on the editor caret position?
340 // virtual bool CanAutoSetSelection() { return ((m_combo == NULL) || !m_combo->IsPopupShown()); }
341 virtual bool CanAutoSetSelection() { return false; }
344 // Popup event handlers
347 // Mouse hot-tracking
348 void OnMouseMove(wxMouseEvent
& event
);
350 // On mouse left, set the value and close the popup
351 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
));
355 int m_itemHere
; // hot item in popup
359 DECLARE_EVENT_TABLE()
363 * wxRichTextStyleComboCtrl
364 * A combo for applying styles.
367 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleComboCtrl
: public wxComboCtrl
369 DECLARE_CLASS(wxRichTextStyleComboCtrl
)
370 DECLARE_EVENT_TABLE()
373 wxRichTextStyleComboCtrl()
378 wxRichTextStyleComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
379 const wxSize
& size
= wxDefaultSize
, long style
= wxCB_READONLY
)
382 Create(parent
, id
, pos
, size
, style
);
385 virtual ~wxRichTextStyleComboCtrl() {}
392 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
393 const wxSize
& size
= wxDefaultSize
, long style
= 0);
396 void UpdateStyles() { m_stylePopup
->UpdateStyles(); }
398 /// Associates the control with a style manager
399 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_stylePopup
->SetStyleSheet(styleSheet
); }
400 wxRichTextStyleSheet
* GetStyleSheet() const { return m_stylePopup
->GetStyleSheet(); }
402 /// Associates the control with a wxRichTextCtrl
403 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_stylePopup
->SetRichTextCtrl(ctrl
); }
404 wxRichTextCtrl
* GetRichTextCtrl() const { return m_stylePopup
->GetRichTextCtrl(); }
406 /// Gets the style popup
407 wxRichTextStyleComboPopup
* GetStylePopup() const { return m_stylePopup
; }
409 /// Auto-select from style under caret in idle time
410 void OnIdle(wxIdleEvent
& event
);
413 wxRichTextStyleComboPopup
* m_stylePopup
;
426 // _WX_RICHTEXTSTYLES_H_