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
); }
66 /// The name of the style.
67 void SetName(const wxString
& name
) { m_name
= name
; }
68 const wxString
& GetName() const { return m_name
; }
70 /// The name of the style that this style is based on.
71 void SetBaseStyle(const wxString
& name
) { m_baseStyle
= name
; }
72 const wxString
& GetBaseStyle() const { return m_baseStyle
; }
75 void SetStyle(const wxRichTextAttr
& style
) { m_style
= style
; }
76 const wxRichTextAttr
& GetStyle() const { return m_style
; }
77 wxRichTextAttr
& GetStyle() { return m_style
; }
82 wxRichTextAttr m_style
;
86 * wxRichTextCharacterStyleDefinition class declaration
89 class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition
: public wxRichTextStyleDefinition
91 DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition
)
96 wxRichTextCharacterStyleDefinition(const wxRichTextCharacterStyleDefinition
& def
): wxRichTextStyleDefinition(def
) {}
97 wxRichTextCharacterStyleDefinition(const wxString
& name
= wxEmptyString
):
98 wxRichTextStyleDefinition(name
) {}
99 virtual ~wxRichTextCharacterStyleDefinition() {}
105 * wxRichTextParagraphStyleDefinition class declaration
108 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition
: public wxRichTextStyleDefinition
110 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition
)
115 wxRichTextParagraphStyleDefinition(const wxRichTextParagraphStyleDefinition
& def
): wxRichTextStyleDefinition(def
) { m_nextStyle
= def
.m_nextStyle
; }
116 wxRichTextParagraphStyleDefinition(const wxString
& name
= wxEmptyString
):
117 wxRichTextStyleDefinition(name
) {}
118 virtual ~wxRichTextParagraphStyleDefinition() {}
121 void SetNextStyle(const wxString
& name
) { m_nextStyle
= name
; }
122 const wxString
& GetNextStyle() const { return m_nextStyle
; }
124 void Copy(const wxRichTextParagraphStyleDefinition
& def
);
125 void operator =(const wxRichTextParagraphStyleDefinition
& def
) { Copy(def
); }
126 bool operator ==(const wxRichTextParagraphStyleDefinition
& def
) const;
130 /// The next style to use when adding a paragraph after this style.
131 wxString m_nextStyle
;
138 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet
: public wxObject
140 DECLARE_CLASS( wxRichTextStyleSheet
)
144 wxRichTextStyleSheet(const wxRichTextStyleSheet
& sheet
)
149 wxRichTextStyleSheet() { Init(); }
150 virtual ~wxRichTextStyleSheet() { DeleteStyles(); }
156 void Copy(const wxRichTextStyleSheet
& sheet
);
159 void operator=(const wxRichTextStyleSheet
& sheet
) { Copy(sheet
); }
162 bool operator==(const wxRichTextStyleSheet
& sheet
) const;
164 /// Add a definition to the character style list
165 bool AddCharacterStyle(wxRichTextCharacterStyleDefinition
* def
);
167 /// Add a definition to the paragraph style list
168 bool AddParagraphStyle(wxRichTextParagraphStyleDefinition
* def
);
170 /// Remove a character style
171 bool RemoveCharacterStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_characterStyleDefinitions
, def
, deleteStyle
); }
173 /// Remove a paragraph style
174 bool RemoveParagraphStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_paragraphStyleDefinitions
, def
, deleteStyle
); }
176 /// Find a character definition by name
177 wxRichTextCharacterStyleDefinition
* FindCharacterStyle(const wxString
& name
) const { return (wxRichTextCharacterStyleDefinition
*) FindStyle(m_characterStyleDefinitions
, name
); }
179 /// Find a paragraph definition by name
180 wxRichTextParagraphStyleDefinition
* FindParagraphStyle(const wxString
& name
) const { return (wxRichTextParagraphStyleDefinition
*) FindStyle(m_paragraphStyleDefinitions
, name
); }
182 /// Return the number of character styes.
183 size_t GetCharacterStyleCount() const { return m_characterStyleDefinitions
.GetCount(); }
185 /// Return the number of paragraph styes.
186 size_t GetParagraphStyleCount() const { return m_paragraphStyleDefinitions
.GetCount(); }
188 /// Return the nth character style
189 wxRichTextCharacterStyleDefinition
* GetCharacterStyle(size_t n
) const { return (wxRichTextCharacterStyleDefinition
*) m_characterStyleDefinitions
.Item(n
)->GetData(); }
191 /// Return the nth paragraph style
192 wxRichTextParagraphStyleDefinition
* GetParagraphStyle(size_t n
) const { return (wxRichTextParagraphStyleDefinition
*) m_paragraphStyleDefinitions
.Item(n
)->GetData(); }
194 /// Delete all styles
199 /// Add a definition to one of the style lists
200 bool AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
);
203 bool RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
);
205 /// Find a definition by name
206 wxRichTextStyleDefinition
* FindStyle(const wxList
& list
, const wxString
& name
) const;
210 wxList m_characterStyleDefinitions
;
211 wxList m_paragraphStyleDefinitions
;
216 * wxRichTextStyleListBox class declaration
217 * A listbox to display styles.
220 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox
: public wxHtmlListBox
222 DECLARE_CLASS(wxRichTextStyleListBox
)
223 DECLARE_EVENT_TABLE()
226 wxRichTextStyleListBox()
230 wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
231 const wxSize
& size
= wxDefaultSize
, long style
= 0);
232 virtual ~wxRichTextStyleListBox();
237 m_richTextCtrl
= NULL
;
240 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
241 const wxSize
& size
= wxDefaultSize
, long style
= 0);
243 /// Creates a suitable HTML fragment for a definition
244 wxString
CreateHTML(wxRichTextStyleDefinition
* def
) const;
246 /// Associates the control with a style manager
247 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_styleSheet
= styleSheet
; }
248 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
250 /// Associates the control with a wxRichTextCtrl
251 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_richTextCtrl
= ctrl
; }
252 wxRichTextCtrl
* GetRichTextCtrl() const { return m_richTextCtrl
; }
254 /// Get style for index
255 wxRichTextStyleDefinition
* GetStyle(size_t i
) const ;
257 /// Get index for style name
258 int GetIndexForStyle(const wxString
& name
) const ;
260 /// Set selection for string, returning the index.
261 int SetStyleSelection(const wxString
& name
);
267 void DoSelection(int i
);
269 /// React to selection
270 void OnSelect(wxCommandEvent
& event
);
273 void OnLeftDown(wxMouseEvent
& event
);
275 /// Auto-select from style under caret in idle time
276 void OnIdle(wxIdleEvent
& event
);
279 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
280 virtual wxColour
GetSelectedTextBgColour(const wxColour
& colBg
) const;
283 /// Convert units in tends of a millimetre to device units
284 int ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const;
286 /// Can we set the selection based on the editor caret position?
287 /// Need to override this if being used in a combobox popup
288 virtual bool CanAutoSetSelection() { return true; }
291 /// Returns the HTML for this item
292 virtual wxString
OnGetItem(size_t n
) const;
296 wxRichTextStyleSheet
* m_styleSheet
;
297 wxRichTextCtrl
* m_richTextCtrl
;
303 * Style drop-down for a wxComboCtrl
306 class wxRichTextStyleComboPopup
: public wxRichTextStyleListBox
, public wxComboPopup
311 m_itemHere
= -1; // hot item in list
315 virtual bool Create( wxWindow
* parent
)
317 return wxRichTextStyleListBox::Create(parent
, wxID_ANY
,
318 wxPoint(0,0), wxDefaultSize
,
322 virtual wxWindow
*GetControl() { return this; }
324 virtual void SetStringValue( const wxString
& s
);
326 virtual wxString
GetStringValue() const;
328 /// Can we set the selection based on the editor caret position?
329 // virtual bool CanAutoSetSelection() { return ((m_combo == NULL) || !m_combo->IsPopupShown()); }
330 virtual bool CanAutoSetSelection() { return false; }
333 // Popup event handlers
336 // Mouse hot-tracking
337 void OnMouseMove(wxMouseEvent
& event
);
339 // On mouse left, set the value and close the popup
340 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
));
344 int m_itemHere
; // hot item in popup
348 DECLARE_EVENT_TABLE()
352 * wxRichTextStyleComboCtrl
353 * A combo for applying styles.
356 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleComboCtrl
: public wxComboCtrl
358 DECLARE_CLASS(wxRichTextStyleComboCtrl
)
359 DECLARE_EVENT_TABLE()
362 wxRichTextStyleComboCtrl()
367 wxRichTextStyleComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
368 const wxSize
& size
= wxDefaultSize
, long style
= wxCB_READONLY
)
371 Create(parent
, id
, pos
, size
, style
);
374 virtual ~wxRichTextStyleComboCtrl() {}
381 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
382 const wxSize
& size
= wxDefaultSize
, long style
= 0);
385 void UpdateStyles() { m_stylePopup
->UpdateStyles(); }
387 /// Associates the control with a style manager
388 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_stylePopup
->SetStyleSheet(styleSheet
); }
389 wxRichTextStyleSheet
* GetStyleSheet() const { return m_stylePopup
->GetStyleSheet(); }
391 /// Associates the control with a wxRichTextCtrl
392 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_stylePopup
->SetRichTextCtrl(ctrl
); }
393 wxRichTextCtrl
* GetRichTextCtrl() const { return m_stylePopup
->GetRichTextCtrl(); }
395 /// Gets the style popup
396 wxRichTextStyleComboPopup
* GetStylePopup() const { return m_stylePopup
; }
398 /// Auto-select from style under caret in idle time
399 void OnIdle(wxIdleEvent
& event
);
402 wxRichTextStyleComboPopup
* m_stylePopup
;
415 // _WX_RICHTEXTSTYLES_H_