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 style description
89 void SetDescription(const wxString
& descr
) { m_description
= descr
; }
90 const wxString
& GetDescription() const { return m_description
; }
92 /// Sets and gets the name of the style that this style is based on
93 void SetBaseStyle(const wxString
& name
) { m_baseStyle
= name
; }
94 const wxString
& GetBaseStyle() const { return m_baseStyle
; }
97 void SetStyle(const wxRichTextAttr
& style
) { m_style
= style
; }
98 const wxRichTextAttr
& GetStyle() const { return m_style
; }
99 wxRichTextAttr
& GetStyle() { return m_style
; }
103 wxString m_baseStyle
;
104 wxString m_description
;
105 wxRichTextAttr m_style
;
109 * wxRichTextCharacterStyleDefinition class declaration
112 class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition
: public wxRichTextStyleDefinition
114 DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition
)
118 wxRichTextCharacterStyleDefinition(const wxRichTextCharacterStyleDefinition
& def
): wxRichTextStyleDefinition(def
) {}
120 /// Default constructor
121 wxRichTextCharacterStyleDefinition(const wxString
& name
= wxEmptyString
):
122 wxRichTextStyleDefinition(name
) {}
125 virtual ~wxRichTextCharacterStyleDefinition() {}
127 /// Clones the object
128 virtual wxRichTextStyleDefinition
* Clone() const { return new wxRichTextCharacterStyleDefinition(*this); }
134 * wxRichTextParagraphStyleDefinition class declaration
137 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition
: public wxRichTextStyleDefinition
139 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition
)
143 wxRichTextParagraphStyleDefinition(const wxRichTextParagraphStyleDefinition
& def
): wxRichTextStyleDefinition(def
) { m_nextStyle
= def
.m_nextStyle
; }
145 /// Default constructor
146 wxRichTextParagraphStyleDefinition(const wxString
& name
= wxEmptyString
):
147 wxRichTextStyleDefinition(name
) {}
150 virtual ~wxRichTextParagraphStyleDefinition() {}
152 /// Sets and gets the next style
153 void SetNextStyle(const wxString
& name
) { m_nextStyle
= name
; }
154 const wxString
& GetNextStyle() const { return m_nextStyle
; }
157 void Copy(const wxRichTextParagraphStyleDefinition
& def
);
159 /// Assignment operator
160 void operator =(const wxRichTextParagraphStyleDefinition
& def
) { Copy(def
); }
162 /// Equality operator
163 bool operator ==(const wxRichTextParagraphStyleDefinition
& def
) const;
165 /// Clones the object
166 virtual wxRichTextStyleDefinition
* Clone() const { return new wxRichTextParagraphStyleDefinition(*this); }
170 /// The next style to use when adding a paragraph after this style.
171 wxString m_nextStyle
;
175 * wxRichTextListStyleDefinition class declaration
178 class WXDLLIMPEXP_RICHTEXT wxRichTextListStyleDefinition
: public wxRichTextParagraphStyleDefinition
180 DECLARE_DYNAMIC_CLASS(wxRichTextListStyleDefinition
)
184 wxRichTextListStyleDefinition(const wxRichTextListStyleDefinition
& def
): wxRichTextParagraphStyleDefinition(def
) { Init(); Copy(def
); }
186 /// Default constructor
187 wxRichTextListStyleDefinition(const wxString
& name
= wxEmptyString
):
188 wxRichTextParagraphStyleDefinition(name
) { Init(); }
191 virtual ~wxRichTextListStyleDefinition() {}
194 void Copy(const wxRichTextListStyleDefinition
& def
);
196 /// Assignment operator
197 void operator =(const wxRichTextListStyleDefinition
& def
) { Copy(def
); }
199 /// Equality operator
200 bool operator ==(const wxRichTextListStyleDefinition
& def
) const;
202 /// Clones the object
203 virtual wxRichTextStyleDefinition
* Clone() const { return new wxRichTextListStyleDefinition(*this); }
205 /// Sets/gets the attributes for the given level
206 void SetLevelAttributes(int i
, const wxRichTextAttr
& attr
);
207 wxRichTextAttr
* GetLevelAttributes(int i
);
208 const wxRichTextAttr
* GetLevelAttributes(int i
) const;
210 /// Convenience function for setting the major attributes for a list level specification
211 void SetAttributes(int i
, int leftIndent
, int leftSubIndent
, int bulletStyle
, const wxString
& bulletSymbol
= wxEmptyString
);
213 /// Finds the level corresponding to the given indentation
214 int FindLevelForIndent(int indent
) const;
216 /// Combine the base and list style with a paragraph style, using the given indent (from which
217 /// an appropriate level is found)
218 wxRichTextAttr
CombineWithParagraphStyle(int indent
, const wxRichTextAttr
& paraStyle
);
220 /// Combine the base and list style, using the given indent (from which
221 /// an appropriate level is found)
222 wxRichTextAttr
GetCombinedStyle(int indent
);
224 /// Combine the base and list style, using the given level from which
225 /// an appropriate level is found)
226 wxRichTextAttr
GetCombinedStyleForLevel(int level
);
228 /// Gets the number of available levels
229 int GetLevelCount() const { return 10; }
231 /// Is this a numbered list?
232 bool IsNumbered(int i
) const;
236 /// The styles for each level (up to 10)
237 wxRichTextAttr m_levelStyles
[10];
244 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet
: public wxObject
246 DECLARE_CLASS( wxRichTextStyleSheet
)
250 wxRichTextStyleSheet(const wxRichTextStyleSheet
& sheet
)
255 wxRichTextStyleSheet() { Init(); }
256 virtual ~wxRichTextStyleSheet();
262 void Copy(const wxRichTextStyleSheet
& sheet
);
265 void operator=(const wxRichTextStyleSheet
& sheet
) { Copy(sheet
); }
268 bool operator==(const wxRichTextStyleSheet
& sheet
) const;
270 /// Add a definition to the character style list
271 bool AddCharacterStyle(wxRichTextCharacterStyleDefinition
* def
);
273 /// Add a definition to the paragraph style list
274 bool AddParagraphStyle(wxRichTextParagraphStyleDefinition
* def
);
276 /// Add a definition to the list style list
277 bool AddListStyle(wxRichTextListStyleDefinition
* def
);
279 /// Remove a character style
280 bool RemoveCharacterStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_characterStyleDefinitions
, def
, deleteStyle
); }
282 /// Remove a paragraph style
283 bool RemoveParagraphStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_paragraphStyleDefinitions
, def
, deleteStyle
); }
285 /// Remove a list style
286 bool RemoveListStyle(wxRichTextStyleDefinition
* def
, bool deleteStyle
= false) { return RemoveStyle(m_listStyleDefinitions
, def
, deleteStyle
); }
288 /// Find a character definition by name
289 wxRichTextCharacterStyleDefinition
* FindCharacterStyle(const wxString
& name
, bool recurse
= true) const { return (wxRichTextCharacterStyleDefinition
*) FindStyle(m_characterStyleDefinitions
, name
, recurse
); }
291 /// Find a paragraph definition by name
292 wxRichTextParagraphStyleDefinition
* FindParagraphStyle(const wxString
& name
, bool recurse
= true) const { return (wxRichTextParagraphStyleDefinition
*) FindStyle(m_paragraphStyleDefinitions
, name
, recurse
); }
294 /// Find a list definition by name
295 wxRichTextListStyleDefinition
* FindListStyle(const wxString
& name
, bool recurse
= true) const { return (wxRichTextListStyleDefinition
*) FindStyle(m_listStyleDefinitions
, name
, recurse
); }
297 /// Return the number of character styles
298 size_t GetCharacterStyleCount() const { return m_characterStyleDefinitions
.GetCount(); }
300 /// Return the number of paragraph styles
301 size_t GetParagraphStyleCount() const { return m_paragraphStyleDefinitions
.GetCount(); }
303 /// Return the number of list styles
304 size_t GetListStyleCount() const { return m_listStyleDefinitions
.GetCount(); }
306 /// Return the nth character style
307 wxRichTextCharacterStyleDefinition
* GetCharacterStyle(size_t n
) const { return (wxRichTextCharacterStyleDefinition
*) m_characterStyleDefinitions
.Item(n
)->GetData(); }
309 /// Return the nth paragraph style
310 wxRichTextParagraphStyleDefinition
* GetParagraphStyle(size_t n
) const { return (wxRichTextParagraphStyleDefinition
*) m_paragraphStyleDefinitions
.Item(n
)->GetData(); }
312 /// Return the nth list style
313 wxRichTextListStyleDefinition
* GetListStyle(size_t n
) const { return (wxRichTextListStyleDefinition
*) m_listStyleDefinitions
.Item(n
)->GetData(); }
315 /// Delete all styles
318 /// Insert into list of style sheets
319 bool InsertSheet(wxRichTextStyleSheet
* before
);
321 /// Append to list of style sheets
322 bool AppendSheet(wxRichTextStyleSheet
* after
);
324 /// Unlink from the list of style sheets
327 /// Get/set next sheet
328 wxRichTextStyleSheet
* GetNextSheet() const { return m_nextSheet
; }
329 void SetNextSheet(wxRichTextStyleSheet
* sheet
) { m_nextSheet
= sheet
; }
331 /// Get/set previous sheet
332 wxRichTextStyleSheet
* GetPreviousSheet() const { return m_previousSheet
; }
333 void SetPreviousSheet(wxRichTextStyleSheet
* sheet
) { m_previousSheet
= sheet
; }
335 /// Sets and gets the name of the style sheet
336 void SetName(const wxString
& name
) { m_name
= name
; }
337 const wxString
& GetName() const { return m_name
; }
339 /// Sets and gets the style description
340 void SetDescription(const wxString
& descr
) { m_description
= descr
; }
341 const wxString
& GetDescription() const { return m_description
; }
345 /// Add a definition to one of the style lists
346 bool AddStyle(wxList
& list
, wxRichTextStyleDefinition
* def
);
349 bool RemoveStyle(wxList
& list
, wxRichTextStyleDefinition
* def
, bool deleteStyle
);
351 /// Find a definition by name
352 wxRichTextStyleDefinition
* FindStyle(const wxList
& list
, const wxString
& name
, bool recurse
= true) const;
356 wxString m_description
;
359 wxList m_characterStyleDefinitions
;
360 wxList m_paragraphStyleDefinitions
;
361 wxList m_listStyleDefinitions
;
363 wxRichTextStyleSheet
* m_previousSheet
;
364 wxRichTextStyleSheet
* m_nextSheet
;
369 * wxRichTextStyleListBox class declaration
370 * A listbox to display styles.
373 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox
: public wxHtmlListBox
375 DECLARE_CLASS(wxRichTextStyleListBox
)
376 DECLARE_EVENT_TABLE()
379 /// Which type of style definition is currently showing?
380 enum wxRichTextStyleType
382 wxRICHTEXT_STYLE_ALL
,
383 wxRICHTEXT_STYLE_PARAGRAPH
,
384 wxRICHTEXT_STYLE_CHARACTER
,
385 wxRICHTEXT_STYLE_LIST
388 wxRichTextStyleListBox()
392 wxRichTextStyleListBox(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
393 const wxSize
& size
= wxDefaultSize
, long style
= 0);
394 virtual ~wxRichTextStyleListBox();
399 m_richTextCtrl
= NULL
;
400 m_applyOnSelection
= false;
401 m_styleType
= wxRICHTEXT_STYLE_PARAGRAPH
;
402 m_autoSetSelection
= true;
405 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
406 const wxSize
& size
= wxDefaultSize
, long style
= 0);
408 /// Creates a suitable HTML fragment for a definition
409 wxString
CreateHTML(wxRichTextStyleDefinition
* def
) const;
411 /// Associates the control with a style sheet
412 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_styleSheet
= styleSheet
; }
413 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
415 /// Associates the control with a wxRichTextCtrl
416 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_richTextCtrl
= ctrl
; }
417 wxRichTextCtrl
* GetRichTextCtrl() const { return m_richTextCtrl
; }
419 /// Get style for index
420 wxRichTextStyleDefinition
* GetStyle(size_t i
) const ;
422 /// Get index for style name
423 int GetIndexForStyle(const wxString
& name
) const ;
425 /// Set selection for string, returning the index.
426 int SetStyleSelection(const wxString
& name
);
432 void ApplyStyle(int i
);
435 void OnLeftDown(wxMouseEvent
& event
);
437 /// Left double-click
438 void OnLeftDoubleClick(wxMouseEvent
& event
);
440 /// Auto-select from style under caret in idle time
441 void OnIdle(wxIdleEvent
& event
);
443 /// Convert units in tends of a millimetre to device units
444 int ConvertTenthsMMToPixels(wxDC
& dc
, int units
) const;
446 /// Can we set the selection based on the editor caret position?
447 /// Need to override this if being used in a combobox popup
448 virtual bool CanAutoSetSelection() { return m_autoSetSelection
; }
449 virtual void SetAutoSetSelection(bool autoSet
) { m_autoSetSelection
= autoSet
; }
451 /// Set whether the style should be applied as soon as the item is selected (the default)
452 void SetApplyOnSelection(bool applyOnSel
) { m_applyOnSelection
= applyOnSel
; }
453 bool GetApplyOnSelection() const { return m_applyOnSelection
; }
455 /// Set the style type to display
456 void SetStyleType(wxRichTextStyleType styleType
) { m_styleType
= styleType
; UpdateStyles(); }
457 wxRichTextStyleType
GetStyleType() const { return m_styleType
; }
459 /// Helper for listbox and combo control
460 static wxString
GetStyleToShowInIdleTime(wxRichTextCtrl
* ctrl
, wxRichTextStyleType styleType
);
463 /// Returns the HTML for this item
464 virtual wxString
OnGetItem(size_t n
) const;
468 wxRichTextStyleSheet
* m_styleSheet
;
469 wxRichTextCtrl
* m_richTextCtrl
;
470 bool m_applyOnSelection
; // if true, applies style on selection
471 wxRichTextStyleType m_styleType
; // style type to display
472 bool m_autoSetSelection
;
476 * wxRichTextStyleListCtrl class declaration
477 * This is a container for the list control plus a combobox to switch between
481 #define wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR 0x1000
483 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListCtrl
: public wxControl
485 DECLARE_CLASS(wxRichTextStyleListCtrl
)
486 DECLARE_EVENT_TABLE()
491 wxRichTextStyleListCtrl()
496 wxRichTextStyleListCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
497 const wxSize
& size
= wxDefaultSize
, long style
= 0);
500 virtual ~wxRichTextStyleListCtrl();
502 /// Member initialisation
505 m_styleListBox
= NULL
;
506 m_styleChoice
= NULL
;
507 m_dontUpdate
= false;
510 /// Creates the windows
511 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
512 const wxSize
& size
= wxDefaultSize
, long style
= 0);
514 /// Updates the style list box
517 /// Associates the control with a style sheet
518 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
);
519 wxRichTextStyleSheet
* GetStyleSheet() const;
521 /// Associates the control with a wxRichTextCtrl
522 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
);
523 wxRichTextCtrl
* GetRichTextCtrl() const;
525 /// Set/get the style type to display
526 void SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType
);
527 wxRichTextStyleListBox::wxRichTextStyleType
GetStyleType() const;
529 /// Get the choice index for style type
530 int StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType
);
532 /// Get the style type for choice index
533 wxRichTextStyleListBox::wxRichTextStyleType
StyleIndexToType(int i
);
536 wxRichTextStyleListBox
* GetStyleListBox() const { return m_styleListBox
; }
539 wxChoice
* GetStyleChoice() const { return m_styleChoice
; }
541 /// React to style type choice
542 void OnChooseType(wxCommandEvent
& event
);
544 /// Lay out the controls
545 void OnSize(wxSizeEvent
& event
);
549 wxRichTextStyleListBox
* m_styleListBox
;
550 wxChoice
* m_styleChoice
;
557 * Style drop-down for a wxComboCtrl
560 class wxRichTextStyleComboPopup
: public wxRichTextStyleListBox
, public wxComboPopup
565 m_itemHere
= -1; // hot item in list
569 virtual bool Create( wxWindow
* parent
)
571 return wxRichTextStyleListBox::Create(parent
, wxID_ANY
,
572 wxPoint(0,0), wxDefaultSize
,
576 virtual wxWindow
*GetControl() { return this; }
578 virtual void SetStringValue( const wxString
& s
);
580 virtual wxString
GetStringValue() const;
582 /// Can we set the selection based on the editor caret position?
583 // virtual bool CanAutoSetSelection() { return ((m_combo == NULL) || !m_combo->IsPopupShown()); }
584 virtual bool CanAutoSetSelection() { return false; }
587 // Popup event handlers
590 // Mouse hot-tracking
591 void OnMouseMove(wxMouseEvent
& event
);
593 // On mouse left, set the value and close the popup
594 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
));
598 int m_itemHere
; // hot item in popup
602 DECLARE_EVENT_TABLE()
606 * wxRichTextStyleComboCtrl
607 * A combo for applying styles.
610 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleComboCtrl
: public wxComboCtrl
612 DECLARE_CLASS(wxRichTextStyleComboCtrl
)
613 DECLARE_EVENT_TABLE()
616 wxRichTextStyleComboCtrl()
621 wxRichTextStyleComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
622 const wxSize
& size
= wxDefaultSize
, long style
= wxCB_READONLY
)
625 Create(parent
, id
, pos
, size
, style
);
628 virtual ~wxRichTextStyleComboCtrl() {}
635 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
, const wxPoint
& pos
= wxDefaultPosition
,
636 const wxSize
& size
= wxDefaultSize
, long style
= 0);
639 void UpdateStyles() { m_stylePopup
->UpdateStyles(); }
641 /// Associates the control with a style sheet
642 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_stylePopup
->SetStyleSheet(styleSheet
); }
643 wxRichTextStyleSheet
* GetStyleSheet() const { return m_stylePopup
->GetStyleSheet(); }
645 /// Associates the control with a wxRichTextCtrl
646 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_stylePopup
->SetRichTextCtrl(ctrl
); }
647 wxRichTextCtrl
* GetRichTextCtrl() const { return m_stylePopup
->GetRichTextCtrl(); }
649 /// Gets the style popup
650 wxRichTextStyleComboPopup
* GetStylePopup() const { return m_stylePopup
; }
652 /// Auto-select from style under caret in idle time
653 void OnIdle(wxIdleEvent
& event
);
656 wxRichTextStyleComboPopup
* m_stylePopup
;
669 // _WX_RICHTEXTSTYLES_H_