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"
33 #include "wx/choice.h"
36 * Forward declarations
39 class WXDLLIMPEXP_RICHTEXT wxRichTextCtrl
;
40 class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer
;
43 * wxRichTextStyleDefinition class declaration
44 * A base class for paragraph and character styles.
47 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleDefinition
: public wxObject
49 DECLARE_CLASS(wxRichTextStyleDefinition
)
53 wxRichTextStyleDefinition(const wxRichTextStyleDefinition
& def
)
60 /// Default constructor
61 wxRichTextStyleDefinition(const wxString
& name
= wxEmptyString
) { Init(); m_name
= name
; }
64 virtual ~wxRichTextStyleDefinition() {}
66 /// Initialises members
70 void Copy(const wxRichTextStyleDefinition
& def
);
73 bool Eq(const wxRichTextStyleDefinition
& def
) const;
75 /// Assignment operator
76 void operator =(const wxRichTextStyleDefinition
& def
) { Copy(def
); }
79 bool operator ==(const wxRichTextStyleDefinition
& def
) const { return Eq(def
); }
81 /// Override to clone the object
82 virtual wxRichTextStyleDefinition
* Clone() const = 0;
84 /// Sets and gets the name of the style
85 void SetName(const wxString
& name
) { m_name
= name
; }
86 const wxString
& GetName() const { return m_name
; }
88 /// Sets and gets the name of the style that this style is based on
89 void SetBaseStyle(const wxString
& name
) { m_baseStyle
= name
; }
90 const wxString
& GetBaseStyle() const { return m_baseStyle
; }
93 void SetStyle(const wxRichTextAttr
& style
) { m_style
= style
; }
94 const wxRichTextAttr
& GetStyle() const { return m_style
; }
95 wxRichTextAttr
& GetStyle() { return m_style
; }
100 wxRichTextAttr m_style
;
104 * wxRichTextCharacterStyleDefinition class declaration
107 class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition
: public wxRichTextStyleDefinition
109 DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition
)
113 wxRichTextCharacterStyleDefinition(const wxRichTextCharacterStyleDefinition
& def
): wxRichTextStyleDefinition(def
) {}
115 /// Default constructor
116 wxRichTextCharacterStyleDefinition(const wxString
& name
= wxEmptyString
):
117 wxRichTextStyleDefinition(name
) {}
120 virtual ~wxRichTextCharacterStyleDefinition() {}
122 /// Clones the object
123 virtual wxRichTextStyleDefinition
* Clone() const { return new wxRichTextCharacterStyleDefinition(*this); }
129 * wxRichTextParagraphStyleDefinition class declaration
132 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition
: public wxRichTextStyleDefinition
134 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition
)
138 wxRichTextParagraphStyleDefinition(const wxRichTextParagraphStyleDefinition
& def
): wxRichTextStyleDefinition(def
) { m_nextStyle
= def
.m_nextStyle
; }
140 /// Default constructor
141 wxRichTextParagraphStyleDefinition(const wxString
& name
= wxEmptyString
):
142 wxRichTextStyleDefinition(name
) {}
145 virtual ~wxRichTextParagraphStyleDefinition() {}
147 /// Sets and gets the next style
148 void SetNextStyle(const wxString
& name
) { m_nextStyle
= name
; }
149 const wxString
& GetNextStyle() const { return m_nextStyle
; }
152 void Copy(const wxRichTextParagraphStyleDefinition
& def
);
154 /// Assignment operator
155 void operator =(const wxRichTextParagraphStyleDefinition
& def
) { Copy(def
); }
157 /// Equality operator
158 bool operator ==(const wxRichTextParagraphStyleDefinition
& def
) const;
160 /// Clones the object
161 virtual wxRichTextStyleDefinition
* Clone() const { return new wxRichTextParagraphStyleDefinition(*this); }
165 /// The next style to use when adding a paragraph after this style.
166 wxString m_nextStyle
;
170 * wxRichTextListStyleDefinition class declaration
173 class WXDLLIMPEXP_RICHTEXT wxRichTextListStyleDefinition
: public wxRichTextParagraphStyleDefinition
175 DECLARE_DYNAMIC_CLASS(wxRichTextListStyleDefinition
)
179 wxRichTextListStyleDefinition(const wxRichTextListStyleDefinition
& def
): wxRichTextParagraphStyleDefinition(def
) { Init(); Copy(def
); }
181 /// Default constructor
182 wxRichTextListStyleDefinition(const wxString
& name
= wxEmptyString
):
183 wxRichTextParagraphStyleDefinition(name
) { Init(); }
186 virtual ~wxRichTextListStyleDefinition() {}
189 void Copy(const wxRichTextListStyleDefinition
& def
);
191 /// Assignment operator
192 void operator =(const wxRichTextListStyleDefinition
& def
) { Copy(def
); }
194 /// Equality operator
195 bool operator ==(const wxRichTextListStyleDefinition
& def
) const;
197 /// Clones the object
198 virtual wxRichTextStyleDefinition
* Clone() const { return new wxRichTextListStyleDefinition(*this); }
200 /// Sets/gets the attributes for the given level
201 void SetLevelAttributes(int i
, const wxRichTextAttr
& attr
);
202 wxRichTextAttr
* GetLevelAttributes(int i
);
203 const wxRichTextAttr
* GetLevelAttributes(int i
) const;
205 /// Convenience function for setting the major attributes for a list level specification
206 void SetAttributes(int i
, int leftIndent
, int leftSubIndent
, int bulletStyle
, const wxString
& bulletSymbol
= wxEmptyString
);
208 /// Finds the level corresponding to the given indentation
209 int FindLevelForIndent(int indent
) const;
211 /// Combine the base and list style with a paragraph style, using the given indent (from which
212 /// an appropriate level is found)
213 wxRichTextAttr
CombineWithParagraphStyle(int indent
, const wxRichTextAttr
& paraStyle
);
215 /// Combine the base and list style, using the given indent (from which
216 /// an appropriate level is found)
217 wxRichTextAttr
GetCombinedStyle(int indent
);
219 /// Combine the base and list style, using the given level from which
220 /// an appropriate level is found)
221 wxRichTextAttr
GetCombinedStyleForLevel(int level
);
223 /// Gets the number of available levels
224 int GetLevelCount() const { return 10; }
226 /// Is this a numbered list?
227 bool IsNumbered(int i
) const;
231 /// The styles for each level (up to 10)
232 wxRichTextAttr m_levelStyles
[10];
239 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet
: public wxObject
241 DECLARE_CLASS( wxRichTextStyleSheet
)
245 wxRichTextStyleSheet(const wxRichTextStyleSheet
& sheet
)
250 wxRichTextStyleSheet() { Init(); }
251 virtual ~wxRichTextStyleSheet();
257 void Copy(const wxRichTextStyleSheet
& sheet
);
260 void operator=(const wxRichTextStyleSheet
& sheet
) { Copy(sheet
); }
263 bool operator==(const wxRichTextStyleSheet
& sheet
) const;
265 /// Add a definition to the character style list
266 bool AddCharacterStyle(wxRichTextCharacterStyleDefinition
* def
);
268 /// Add a definition to the paragraph style list
269 bool AddParagraphStyle(wxRichTextParagraphStyleDefinition
* def
);
271 /// Add a definition to the list style list
272 bool AddListStyle(wxRichTextListStyleDefinition
* def
);
274 /// Remove a character style
275 bool RemoveCharacterStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_characterStyleDefinitions
, def
, deleteStyle
); }
277 /// Remove a paragraph style
278 bool RemoveParagraphStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_paragraphStyleDefinitions
, def
, deleteStyle
); }
280 /// Remove a list style
281 bool RemoveListStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_listStyleDefinitions
, def
, deleteStyle
); }
283 /// Find a character definition by name
284 wxRichTextCharacterStyleDefinition
* FindCharacterStyle(const wxString
& name
, bool recurse
= true) const { return (wxRichTextCharacterStyleDefinition
*) FindStyle(m_characterStyleDefinitions
, name
, recurse
); }
286 /// Find a paragraph definition by name
287 wxRichTextParagraphStyleDefinition
* FindParagraphStyle(const wxString
& name
, bool recurse
= true) const { return (wxRichTextParagraphStyleDefinition
*) FindStyle(m_paragraphStyleDefinitions
, name
, recurse
); }
289 /// Find a list definition by name
290 wxRichTextListStyleDefinition
* FindListStyle(const wxString
& name
, bool recurse
= true) const { return (wxRichTextListStyleDefinition
*) FindStyle(m_listStyleDefinitions
, name
, recurse
); }
292 /// Return the number of character styles
293 size_t GetCharacterStyleCount() const { return m_characterStyleDefinitions
.GetCount(); }
295 /// Return the number of paragraph styles
296 size_t GetParagraphStyleCount() const { return m_paragraphStyleDefinitions
.GetCount(); }
298 /// Return the number of list styles
299 size_t GetListStyleCount() const { return m_listStyleDefinitions
.GetCount(); }
301 /// Return the nth character style
302 wxRichTextCharacterStyleDefinition
* GetCharacterStyle(size_t n
) const { return (wxRichTextCharacterStyleDefinition
*) m_characterStyleDefinitions
.Item(n
)->GetData(); }
304 /// Return the nth paragraph style
305 wxRichTextParagraphStyleDefinition
* GetParagraphStyle(size_t n
) const { return (wxRichTextParagraphStyleDefinition
*) m_paragraphStyleDefinitions
.Item(n
)->GetData(); }
307 /// Return the nth list style
308 wxRichTextListStyleDefinition
* GetListStyle(size_t n
) const { return (wxRichTextListStyleDefinition
*) m_listStyleDefinitions
.Item(n
)->GetData(); }
310 /// Delete all styles
313 /// Insert into list of style sheets
314 bool InsertSheet(wxRichTextStyleSheet
* before
);
316 /// Append to list of style sheets
317 bool AppendSheet(wxRichTextStyleSheet
* after
);
319 /// Unlink from the list of style sheets
322 /// Get/set next sheet
323 wxRichTextStyleSheet
* GetNextSheet() const { return m_nextSheet
; }
324 void SetNextSheet(wxRichTextStyleSheet
* sheet
) { m_nextSheet
= sheet
; }
326 /// Get/set previous sheet
327 wxRichTextStyleSheet
* GetPreviousSheet() const { return m_previousSheet
; }
328 void SetPreviousSheet(wxRichTextStyleSheet
* sheet
) { m_previousSheet
= sheet
; }
332 /// Add a definition to one of the style lists
333 bool AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
);
336 bool RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
);
338 /// Find a definition by name
339 wxRichTextStyleDefinition
* FindStyle(const wxList
& list
, const wxString
& name
, bool recurse
= true) const;
343 wxList m_characterStyleDefinitions
;
344 wxList m_paragraphStyleDefinitions
;
345 wxList m_listStyleDefinitions
;
347 wxRichTextStyleSheet
* m_previousSheet
;
348 wxRichTextStyleSheet
* m_nextSheet
;
353 * wxRichTextStyleListBox class declaration
354 * A listbox to display styles.
357 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox
: public wxHtmlListBox
359 DECLARE_CLASS(wxRichTextStyleListBox
)
360 DECLARE_EVENT_TABLE()
363 /// Which type of style definition is currently showing?
364 enum wxRichTextStyleType
366 wxRICHTEXT_STYLE_ALL
,
367 wxRICHTEXT_STYLE_PARAGRAPH
,
368 wxRICHTEXT_STYLE_CHARACTER
,
369 wxRICHTEXT_STYLE_LIST
372 wxRichTextStyleListBox()
376 wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
377 const wxSize
& size
= wxDefaultSize
, long style
= 0);
378 virtual ~wxRichTextStyleListBox();
383 m_richTextCtrl
= NULL
;
384 m_applyOnSelection
= false;
385 m_styleType
= wxRICHTEXT_STYLE_PARAGRAPH
;
386 m_autoSetSelection
= true;
389 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
390 const wxSize
& size
= wxDefaultSize
, long style
= 0);
392 /// Creates a suitable HTML fragment for a definition
393 wxString
CreateHTML(wxRichTextStyleDefinition
* def
) const;
395 /// Associates the control with a style manager
396 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_styleSheet
= styleSheet
; }
397 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
399 /// Associates the control with a wxRichTextCtrl
400 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_richTextCtrl
= ctrl
; }
401 wxRichTextCtrl
* GetRichTextCtrl() const { return m_richTextCtrl
; }
403 /// Get style for index
404 wxRichTextStyleDefinition
* GetStyle(size_t i
) const ;
406 /// Get index for style name
407 int GetIndexForStyle(const wxString
& name
) const ;
409 /// Set selection for string, returning the index.
410 int SetStyleSelection(const wxString
& name
);
416 void ApplyStyle(int i
);
419 void OnLeftDown(wxMouseEvent
& event
);
421 /// Left double-click
422 void OnLeftDoubleClick(wxMouseEvent
& event
);
424 /// Auto-select from style under caret in idle time
425 void OnIdle(wxIdleEvent
& event
);
427 /// Convert units in tends of a millimetre to device units
428 int ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const;
430 /// Can we set the selection based on the editor caret position?
431 /// Need to override this if being used in a combobox popup
432 virtual bool CanAutoSetSelection() { return m_autoSetSelection
; }
433 virtual void SetAutoSetSelection(bool autoSet
) { m_autoSetSelection
= autoSet
; }
435 /// Set whether the style should be applied as soon as the item is selected (the default)
436 void SetApplyOnSelection(bool applyOnSel
) { m_applyOnSelection
= applyOnSel
; }
437 bool GetApplyOnSelection() const { return m_applyOnSelection
; }
439 /// Set the style type to display
440 void SetStyleType(wxRichTextStyleType styleType
) { m_styleType
= styleType
; UpdateStyles(); }
441 wxRichTextStyleType
GetStyleType() const { return m_styleType
; }
443 /// Helper for listbox and combo control
444 static wxString
GetStyleToShowInIdleTime(wxRichTextCtrl
* ctrl
, wxRichTextStyleType styleType
);
447 /// Returns the HTML for this item
448 virtual wxString
OnGetItem(size_t n
) const;
452 wxRichTextStyleSheet
* m_styleSheet
;
453 wxRichTextCtrl
* m_richTextCtrl
;
454 bool m_applyOnSelection
; // if true, applies style on selection
455 wxRichTextStyleType m_styleType
; // style type to display
456 bool m_autoSetSelection
;
460 * wxRichTextStyleListCtrl class declaration
461 * This is a container for the list control plus a combobox to switch between
465 #define wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR 0x1000
467 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListCtrl
: public wxControl
469 DECLARE_CLASS(wxRichTextStyleListCtrl
)
470 DECLARE_EVENT_TABLE()
475 wxRichTextStyleListCtrl()
480 wxRichTextStyleListCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
481 const wxSize
& size
= wxDefaultSize
, long style
= 0);
484 virtual ~wxRichTextStyleListCtrl();
486 /// Member initialisation
489 m_styleListBox
= NULL
;
490 m_styleChoice
= NULL
;
491 m_dontUpdate
= false;
494 /// Creates the windows
495 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
496 const wxSize
& size
= wxDefaultSize
, long style
= 0);
498 /// Updates the style list box
501 /// Associates the control with a style manager
502 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
);
503 wxRichTextStyleSheet
* GetStyleSheet() const;
505 /// Associates the control with a wxRichTextCtrl
506 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
);
507 wxRichTextCtrl
* GetRichTextCtrl() const;
509 /// Set/get the style type to display
510 void SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType
);
511 wxRichTextStyleListBox::wxRichTextStyleType
GetStyleType() const;
513 /// Get the choice index for style type
514 int StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType
);
516 /// Get the style type for choice index
517 wxRichTextStyleListBox::wxRichTextStyleType
StyleIndexToType(int i
);
520 wxRichTextStyleListBox
* GetStyleListBox() const { return m_styleListBox
; }
523 wxChoice
* GetStyleChoice() const { return m_styleChoice
; }
525 /// React to style type choice
526 void OnChooseType(wxCommandEvent
& event
);
528 /// Lay out the controls
529 void OnSize(wxSizeEvent
& event
);
533 wxRichTextStyleListBox
* m_styleListBox
;
534 wxChoice
* m_styleChoice
;
541 * Style drop-down for a wxComboCtrl
544 class wxRichTextStyleComboPopup
: public wxRichTextStyleListBox
, public wxComboPopup
549 m_itemHere
= -1; // hot item in list
553 virtual bool Create( wxWindow
* parent
)
555 return wxRichTextStyleListBox::Create(parent
, wxID_ANY
,
556 wxPoint(0,0), wxDefaultSize
,
560 virtual wxWindow
*GetControl() { return this; }
562 virtual void SetStringValue( const wxString
& s
);
564 virtual wxString
GetStringValue() const;
566 /// Can we set the selection based on the editor caret position?
567 // virtual bool CanAutoSetSelection() { return ((m_combo == NULL) || !m_combo->IsPopupShown()); }
568 virtual bool CanAutoSetSelection() { return false; }
571 // Popup event handlers
574 // Mouse hot-tracking
575 void OnMouseMove(wxMouseEvent
& event
);
577 // On mouse left, set the value and close the popup
578 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
));
582 int m_itemHere
; // hot item in popup
586 DECLARE_EVENT_TABLE()
590 * wxRichTextStyleComboCtrl
591 * A combo for applying styles.
594 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleComboCtrl
: public wxComboCtrl
596 DECLARE_CLASS(wxRichTextStyleComboCtrl
)
597 DECLARE_EVENT_TABLE()
600 wxRichTextStyleComboCtrl()
605 wxRichTextStyleComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
606 const wxSize
& size
= wxDefaultSize
, long style
= wxCB_READONLY
)
609 Create(parent
, id
, pos
, size
, style
);
612 virtual ~wxRichTextStyleComboCtrl() {}
619 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
620 const wxSize
& size
= wxDefaultSize
, long style
= 0);
623 void UpdateStyles() { m_stylePopup
->UpdateStyles(); }
625 /// Associates the control with a style manager
626 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_stylePopup
->SetStyleSheet(styleSheet
); }
627 wxRichTextStyleSheet
* GetStyleSheet() const { return m_stylePopup
->GetStyleSheet(); }
629 /// Associates the control with a wxRichTextCtrl
630 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_stylePopup
->SetRichTextCtrl(ctrl
); }
631 wxRichTextCtrl
* GetRichTextCtrl() const { return m_stylePopup
->GetRichTextCtrl(); }
633 /// Gets the style popup
634 wxRichTextStyleComboPopup
* GetStylePopup() const { return m_stylePopup
; }
636 /// Auto-select from style under caret in idle time
637 void OnIdle(wxIdleEvent
& event
);
640 wxRichTextStyleComboPopup
* m_stylePopup
;
653 // _WX_RICHTEXTSTYLES_H_