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 wxRichTextStyleDefinition
& def
) { Copy(def
); }
51 wxRichTextStyleDefinition(const wxString
& name
= wxEmptyString
) { Init(); m_name
= name
; }
52 virtual ~wxRichTextStyleDefinition() {}
55 void Copy(const wxRichTextStyleDefinition
& def
);
56 bool Eq(const wxRichTextStyleDefinition
& def
) const;
57 void operator =(const wxRichTextStyleDefinition
& def
) { Copy(def
); }
58 bool operator ==(const wxRichTextStyleDefinition
& def
) const { return Eq(def
); }
60 /// The name of the style.
61 void SetName(const wxString
& name
) { m_name
= name
; }
62 const wxString
& GetName() const { return m_name
; }
64 /// The name of the style that this style is based on.
65 void SetBaseStyle(const wxString
& name
) { m_baseStyle
= name
; }
66 const wxString
& GetBaseStyle() const { return m_baseStyle
; }
69 void SetStyle(const wxRichTextAttr
& style
) { m_style
= style
; }
70 const wxRichTextAttr
& GetStyle() const { return m_style
; }
71 wxRichTextAttr
& GetStyle() { return m_style
; }
76 wxRichTextAttr m_style
;
80 * wxRichTextCharacterStyleDefinition class declaration
83 class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition
: public wxRichTextStyleDefinition
85 DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition
)
90 wxRichTextCharacterStyleDefinition(const wxRichTextCharacterStyleDefinition
& def
): wxRichTextStyleDefinition(def
) {}
91 wxRichTextCharacterStyleDefinition(const wxString
& name
= wxEmptyString
):
92 wxRichTextStyleDefinition(name
) {}
93 virtual ~wxRichTextCharacterStyleDefinition() {}
99 * wxRichTextParagraphStyleDefinition class declaration
102 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition
: public wxRichTextStyleDefinition
104 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition
)
109 wxRichTextParagraphStyleDefinition(const wxRichTextParagraphStyleDefinition
& def
): wxRichTextStyleDefinition(def
) { m_nextStyle
= def
.m_nextStyle
; }
110 wxRichTextParagraphStyleDefinition(const wxString
& name
= wxEmptyString
):
111 wxRichTextStyleDefinition(name
) {}
112 virtual ~wxRichTextParagraphStyleDefinition() {}
115 void SetNextStyle(const wxString
& name
) { m_nextStyle
= name
; }
116 const wxString
& GetNextStyle() const { return m_nextStyle
; }
118 void Copy(const wxRichTextParagraphStyleDefinition
& def
);
119 void operator =(const wxRichTextParagraphStyleDefinition
& def
) { Copy(def
); }
120 bool operator ==(const wxRichTextParagraphStyleDefinition
& def
) const;
124 /// The next style to use when adding a paragraph after this style.
125 wxString m_nextStyle
;
132 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet
: public wxObject
134 DECLARE_CLASS( wxRichTextStyleSheet
)
138 wxRichTextStyleSheet(const wxRichTextStyleSheet
& sheet
) { Copy(sheet
); }
139 wxRichTextStyleSheet() { Init(); }
140 virtual ~wxRichTextStyleSheet() { DeleteStyles(); }
146 void Copy(const wxRichTextStyleSheet
& sheet
);
149 void operator=(const wxRichTextStyleSheet
& sheet
) { Copy(sheet
); }
152 bool operator==(const wxRichTextStyleSheet
& sheet
) const;
154 /// Add a definition to the character style list
155 bool AddCharacterStyle(wxRichTextCharacterStyleDefinition
* def
);
157 /// Add a definition to the paragraph style list
158 bool AddParagraphStyle(wxRichTextParagraphStyleDefinition
* def
);
160 /// Remove a character style
161 bool RemoveCharacterStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_characterStyleDefinitions
, def
, deleteStyle
); }
163 /// Remove a paragraph style
164 bool RemoveParagraphStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_paragraphStyleDefinitions
, def
, deleteStyle
); }
166 /// Find a character definition by name
167 wxRichTextCharacterStyleDefinition
* FindCharacterStyle(const wxString
& name
) const { return (wxRichTextCharacterStyleDefinition
*) FindStyle(m_characterStyleDefinitions
, name
); }
169 /// Find a paragraph definition by name
170 wxRichTextParagraphStyleDefinition
* FindParagraphStyle(const wxString
& name
) const { return (wxRichTextParagraphStyleDefinition
*) FindStyle(m_paragraphStyleDefinitions
, name
); }
172 /// Return the number of character styes.
173 size_t GetCharacterStyleCount() const { return m_characterStyleDefinitions
.GetCount(); }
175 /// Return the number of paragraph styes.
176 size_t GetParagraphStyleCount() const { return m_paragraphStyleDefinitions
.GetCount(); }
178 /// Return the nth character style
179 wxRichTextCharacterStyleDefinition
* GetCharacterStyle(size_t n
) const { return (wxRichTextCharacterStyleDefinition
*) m_characterStyleDefinitions
.Item(n
)->GetData(); }
181 /// Return the nth paragraph style
182 wxRichTextParagraphStyleDefinition
* GetParagraphStyle(size_t n
) const { return (wxRichTextParagraphStyleDefinition
*) m_paragraphStyleDefinitions
.Item(n
)->GetData(); }
184 /// Delete all styles
189 /// Add a definition to one of the style lists
190 bool AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
);
193 bool RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
);
195 /// Find a definition by name
196 wxRichTextStyleDefinition
* FindStyle(const wxList
& list
, const wxString
& name
) const;
200 wxList m_characterStyleDefinitions
;
201 wxList m_paragraphStyleDefinitions
;
206 * wxRichTextStyleListBox class declaration
207 * A listbox to display styles.
210 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox
: public wxHtmlListBox
212 DECLARE_CLASS(wxRichTextStyleListBox
)
213 DECLARE_EVENT_TABLE()
216 wxRichTextStyleListBox()
220 wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
221 const wxSize
& size
= wxDefaultSize
, long style
= 0);
222 virtual ~wxRichTextStyleListBox();
227 m_richTextCtrl
= NULL
;
230 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
231 const wxSize
& size
= wxDefaultSize
, long style
= 0);
233 /// Creates a suitable HTML fragment for a definition
234 wxString
CreateHTML(wxRichTextStyleDefinition
* def
) const;
236 /// Associates the control with a style manager
237 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_styleSheet
= styleSheet
; }
238 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
240 /// Associates the control with a wxRichTextCtrl
241 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_richTextCtrl
= ctrl
; }
242 wxRichTextCtrl
* GetRichTextCtrl() const { return m_richTextCtrl
; }
244 /// Get style for index
245 wxRichTextStyleDefinition
* GetStyle(size_t i
) const ;
247 /// Get index for style name
248 int GetIndexForStyle(const wxString
& name
) const ;
250 /// Set selection for string, returning the index.
251 int SetStyleSelection(const wxString
& name
);
257 void DoSelection(int i
);
259 /// React to selection
260 void OnSelect(wxCommandEvent
& event
);
263 void OnLeftDown(wxMouseEvent
& event
);
265 /// Auto-select from style under caret in idle time
266 void OnIdle(wxIdleEvent
& event
);
269 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
270 virtual wxColour
GetSelectedTextBgColour(const wxColour
& colBg
) const;
273 /// Convert units in tends of a millimetre to device units
274 int ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const;
276 /// Can we set the selection based on the editor caret position?
277 /// Need to override this if being used in a combobox popup
278 virtual bool CanAutoSetSelection() { return true; }
281 /// Returns the HTML for this item
282 virtual wxString
OnGetItem(size_t n
) const;
286 wxRichTextStyleSheet
* m_styleSheet
;
287 wxRichTextCtrl
* m_richTextCtrl
;
293 * Style drop-down for a wxComboCtrl
296 class wxRichTextStyleComboPopup
: public wxRichTextStyleListBox
, public wxComboPopup
301 m_itemHere
= -1; // hot item in list
305 virtual bool Create( wxWindow
* parent
)
307 return wxRichTextStyleListBox::Create(parent
, wxID_ANY
,
308 wxPoint(0,0), wxDefaultSize
,
312 virtual wxWindow
*GetControl() { return this; }
314 virtual void SetStringValue( const wxString
& s
);
316 virtual wxString
GetStringValue() const;
318 /// Can we set the selection based on the editor caret position?
319 // virtual bool CanAutoSetSelection() { return ((m_combo == NULL) || !m_combo->IsPopupShown()); }
320 virtual bool CanAutoSetSelection() { return false; }
323 // Popup event handlers
326 // Mouse hot-tracking
327 void OnMouseMove(wxMouseEvent
& event
);
329 // On mouse left, set the value and close the popup
330 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
));
334 int m_itemHere
; // hot item in popup
338 DECLARE_EVENT_TABLE()
342 * wxRichTextStyleComboCtrl
343 * A combo for applying styles.
346 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleComboCtrl
: public wxComboCtrl
348 DECLARE_CLASS(wxRichTextStyleComboCtrl
)
349 DECLARE_EVENT_TABLE()
352 wxRichTextStyleComboCtrl()
357 wxRichTextStyleComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
358 const wxSize
& size
= wxDefaultSize
, long style
= wxCB_READONLY
)
361 Create(parent
, id
, pos
, size
, style
);
364 virtual ~wxRichTextStyleComboCtrl() {}
371 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
372 const wxSize
& size
= wxDefaultSize
, long style
= 0);
375 void UpdateStyles() { m_stylePopup
->UpdateStyles(); }
377 /// Associates the control with a style manager
378 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_stylePopup
->SetStyleSheet(styleSheet
); }
379 wxRichTextStyleSheet
* GetStyleSheet() const { return m_stylePopup
->GetStyleSheet(); }
381 /// Associates the control with a wxRichTextCtrl
382 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_stylePopup
->SetRichTextCtrl(ctrl
); }
383 wxRichTextCtrl
* GetRichTextCtrl() const { return m_stylePopup
->GetRichTextCtrl(); }
385 /// Gets the style popup
386 wxRichTextStyleComboPopup
* GetStylePopup() const { return m_stylePopup
; }
388 /// Auto-select from style under caret in idle time
389 void OnIdle(wxIdleEvent
& event
);
392 wxRichTextStyleComboPopup
* m_stylePopup
;
405 // _WX_RICHTEXTSTYLES_H_