]> git.saurik.com Git - wxWidgets.git/blame - include/wx/richtext/richtextstyles.h
Fix part of [ 1570325 ] wxAnimationCtrl for wxAdv library
[wxWidgets.git] / include / wx / richtext / richtextstyles.h
CommitLineData
5d7836c4 1/////////////////////////////////////////////////////////////////////////////
b3298d1d 2// Name: wx/richtext/richtextstyles.h
5d7836c4
JS
3// Purpose: Style management for wxRichTextCtrl
4// Author: Julian Smart
e637208a 5// Modified by:
5d7836c4 6// Created: 2005-09-30
e637208a 7// RCS-ID:
5d7836c4
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_RICHTEXTSTYLES_H_
13#define _WX_RICHTEXTSTYLES_H_
14
15/*!
16 * Includes
17 */
18
b3298d1d 19#include "wx/defs.h"
5d7836c4
JS
20
21#if wxUSE_RICHTEXT
22
b3298d1d
WS
23#include "wx/richtext/richtextbuffer.h"
24
5d7836c4
JS
25#if wxUSE_HTML
26#include "wx/htmllbox.h"
27#endif
28
e637208a
JS
29#if wxUSE_COMBOCTRL
30#include "wx/combo.h"
31#endif
32
5d7836c4
JS
33/*!
34 * Forward declarations
35 */
36
3b2cb431
JS
37class WXDLLIMPEXP_RICHTEXT wxRichTextCtrl;
38class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer;
5d7836c4
JS
39
40/*!
41 * wxRichTextStyleDefinition class declaration
42 * A base class for paragraph and character styles.
43 */
44
3b2cb431 45class WXDLLIMPEXP_RICHTEXT wxRichTextStyleDefinition: public wxObject
5d7836c4
JS
46{
47 DECLARE_CLASS(wxRichTextStyleDefinition)
48public:
49
50// Constructors
51
b3298d1d
WS
52 wxRichTextStyleDefinition(const wxRichTextStyleDefinition& def)
53 : wxObject()
54 {
55 Copy(def);
56 }
5d7836c4 57 wxRichTextStyleDefinition(const wxString& name = wxEmptyString) { Init(); m_name = name; }
d3c7fc99 58 virtual ~wxRichTextStyleDefinition() {}
5d7836c4
JS
59
60 void Init() {}
fe5aa22c
JS
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); }
86015e55 65 virtual wxRichTextStyleDefinition* Clone() const = 0;
5d7836c4
JS
66
67 /// The name of the style.
68 void SetName(const wxString& name) { m_name = name; }
69 const wxString& GetName() const { return m_name; }
70
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; }
74
75 /// The style.
76 void SetStyle(const wxRichTextAttr& style) { m_style = style; }
77 const wxRichTextAttr& GetStyle() const { return m_style; }
78 wxRichTextAttr& GetStyle() { return m_style; }
79
80protected:
81 wxString m_name;
82 wxString m_baseStyle;
83 wxRichTextAttr m_style;
84};
85
86/*!
87 * wxRichTextCharacterStyleDefinition class declaration
88 */
89
3b2cb431 90class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition: public wxRichTextStyleDefinition
5d7836c4
JS
91{
92 DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition)
93public:
94
95// Constructors
96
fe5aa22c 97 wxRichTextCharacterStyleDefinition(const wxRichTextCharacterStyleDefinition& def): wxRichTextStyleDefinition(def) {}
5d7836c4
JS
98 wxRichTextCharacterStyleDefinition(const wxString& name = wxEmptyString):
99 wxRichTextStyleDefinition(name) {}
d3c7fc99 100 virtual ~wxRichTextCharacterStyleDefinition() {}
5d7836c4 101
86015e55
JS
102 virtual wxRichTextStyleDefinition* Clone() const { return new wxRichTextCharacterStyleDefinition(*this); }
103
5d7836c4
JS
104protected:
105};
106
107/*!
108 * wxRichTextParagraphStyleDefinition class declaration
109 */
110
3b2cb431 111class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition: public wxRichTextStyleDefinition
5d7836c4
JS
112{
113 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition)
114public:
115
116// Constructors
117
fe5aa22c 118 wxRichTextParagraphStyleDefinition(const wxRichTextParagraphStyleDefinition& def): wxRichTextStyleDefinition(def) { m_nextStyle = def.m_nextStyle; }
5d7836c4
JS
119 wxRichTextParagraphStyleDefinition(const wxString& name = wxEmptyString):
120 wxRichTextStyleDefinition(name) {}
d3c7fc99 121 virtual ~wxRichTextParagraphStyleDefinition() {}
5d7836c4
JS
122
123 /// The next style.
124 void SetNextStyle(const wxString& name) { m_nextStyle = name; }
125 const wxString& GetNextStyle() const { return m_nextStyle; }
126
fe5aa22c
JS
127 void Copy(const wxRichTextParagraphStyleDefinition& def);
128 void operator =(const wxRichTextParagraphStyleDefinition& def) { Copy(def); }
129 bool operator ==(const wxRichTextParagraphStyleDefinition& def) const;
130
86015e55
JS
131 virtual wxRichTextStyleDefinition* Clone() const { return new wxRichTextParagraphStyleDefinition(*this); }
132
5d7836c4
JS
133protected:
134
135 /// The next style to use when adding a paragraph after this style.
136 wxString m_nextStyle;
137};
138
139/*!
140 * The style sheet
141 */
142
3b2cb431 143class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet: public wxObject
5d7836c4
JS
144{
145 DECLARE_CLASS( wxRichTextStyleSheet )
146
147public:
148 /// Constructors
b3298d1d
WS
149 wxRichTextStyleSheet(const wxRichTextStyleSheet& sheet)
150 : wxObject()
151 {
152 Copy(sheet);
153 }
5d7836c4 154 wxRichTextStyleSheet() { Init(); }
d3c7fc99 155 virtual ~wxRichTextStyleSheet() { DeleteStyles(); }
5d7836c4
JS
156
157 /// Initialisation
158 void Init();
159
fe5aa22c
JS
160 /// Copy
161 void Copy(const wxRichTextStyleSheet& sheet);
162
163 /// Assignment
164 void operator=(const wxRichTextStyleSheet& sheet) { Copy(sheet); }
165
166 /// Equality
167 bool operator==(const wxRichTextStyleSheet& sheet) const;
168
5d7836c4 169 /// Add a definition to the character style list
fe5aa22c 170 bool AddCharacterStyle(wxRichTextCharacterStyleDefinition* def);
5d7836c4
JS
171
172 /// Add a definition to the paragraph style list
fe5aa22c 173 bool AddParagraphStyle(wxRichTextParagraphStyleDefinition* def);
5d7836c4
JS
174
175 /// Remove a character style
176 bool RemoveCharacterStyle(wxRichTextStyleDefinition* def, bool deleteStyle = false) { return RemoveStyle(m_characterStyleDefinitions, def, deleteStyle); }
177
178 /// Remove a paragraph style
fe5aa22c 179 bool RemoveParagraphStyle(wxRichTextStyleDefinition* def, bool deleteStyle = false) { return RemoveStyle(m_paragraphStyleDefinitions, def, deleteStyle); }
5d7836c4
JS
180
181 /// Find a character definition by name
182 wxRichTextCharacterStyleDefinition* FindCharacterStyle(const wxString& name) const { return (wxRichTextCharacterStyleDefinition*) FindStyle(m_characterStyleDefinitions, name); }
183
184 /// Find a paragraph definition by name
fe5aa22c 185 wxRichTextParagraphStyleDefinition* FindParagraphStyle(const wxString& name) const { return (wxRichTextParagraphStyleDefinition*) FindStyle(m_paragraphStyleDefinitions, name); }
5d7836c4
JS
186
187 /// Return the number of character styes.
188 size_t GetCharacterStyleCount() const { return m_characterStyleDefinitions.GetCount(); }
189
190 /// Return the number of paragraph styes.
191 size_t GetParagraphStyleCount() const { return m_paragraphStyleDefinitions.GetCount(); }
192
193 /// Return the nth character style
194 wxRichTextCharacterStyleDefinition* GetCharacterStyle(size_t n) const { return (wxRichTextCharacterStyleDefinition*) m_characterStyleDefinitions.Item(n)->GetData(); }
195
196 /// Return the nth paragraph style
197 wxRichTextParagraphStyleDefinition* GetParagraphStyle(size_t n) const { return (wxRichTextParagraphStyleDefinition*) m_paragraphStyleDefinitions.Item(n)->GetData(); }
198
199 /// Delete all styles
200 void DeleteStyles();
201
202/// Implementation
203
204 /// Add a definition to one of the style lists
205 bool AddStyle(wxList& list, wxRichTextStyleDefinition* def);
206
207 /// Remove a style
208 bool RemoveStyle(wxList& list, wxRichTextStyleDefinition* def, bool deleteStyle);
209
210 /// Find a definition by name
211 wxRichTextStyleDefinition* FindStyle(const wxList& list, const wxString& name) const;
212
213protected:
214
215 wxList m_characterStyleDefinitions;
216 wxList m_paragraphStyleDefinitions;
217};
218
219#if wxUSE_HTML
220/*!
221 * wxRichTextStyleListBox class declaration
222 * A listbox to display styles.
223 */
224
3b2cb431 225class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox: public wxHtmlListBox
5d7836c4
JS
226{
227 DECLARE_CLASS(wxRichTextStyleListBox)
228 DECLARE_EVENT_TABLE()
229
230public:
e637208a
JS
231 wxRichTextStyleListBox()
232 {
233 Init();
234 }
5d7836c4
JS
235 wxRichTextStyleListBox(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
236 const wxSize& size = wxDefaultSize, long style = 0);
d3c7fc99 237 virtual ~wxRichTextStyleListBox();
5d7836c4 238
e637208a
JS
239 void Init()
240 {
241 m_styleSheet = NULL;
242 m_richTextCtrl = NULL;
86015e55 243 m_applyOnSelection = true;
e637208a
JS
244 }
245
246 bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
247 const wxSize& size = wxDefaultSize, long style = 0);
248
5d7836c4
JS
249 /// Creates a suitable HTML fragment for a definition
250 wxString CreateHTML(wxRichTextStyleDefinition* def) const;
251
252 /// Associates the control with a style manager
253 void SetStyleSheet(wxRichTextStyleSheet* styleSheet) { m_styleSheet = styleSheet; }
254 wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
255
256 /// Associates the control with a wxRichTextCtrl
257 void SetRichTextCtrl(wxRichTextCtrl* ctrl) { m_richTextCtrl = ctrl; }
258 wxRichTextCtrl* GetRichTextCtrl() const { return m_richTextCtrl; }
259
e637208a 260 /// Get style for index
5d7836c4
JS
261 wxRichTextStyleDefinition* GetStyle(size_t i) const ;
262
e637208a
JS
263 /// Get index for style name
264 int GetIndexForStyle(const wxString& name) const ;
265
266 /// Set selection for string, returning the index.
267 int SetStyleSelection(const wxString& name);
268
5d7836c4
JS
269 /// Updates the list
270 void UpdateStyles();
271
86015e55
JS
272 /// Apply the style
273 void ApplyStyle(int i);
e637208a 274
5d7836c4
JS
275 /// React to selection
276 void OnSelect(wxCommandEvent& event);
277
278 /// Left click
279 void OnLeftDown(wxMouseEvent& event);
280
e637208a
JS
281 /// Auto-select from style under caret in idle time
282 void OnIdle(wxIdleEvent& event);
283
5d7836c4
JS
284#if 0
285 virtual wxColour GetSelectedTextColour(const wxColour& colFg) const;
286 virtual wxColour GetSelectedTextBgColour(const wxColour& colBg) const;
287#endif
288
e637208a 289 /// Convert units in tends of a millimetre to device units
5d7836c4
JS
290 int ConvertTenthsMMToPixels(wxDC& dc, int units) const;
291
e637208a
JS
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; }
295
86015e55
JS
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; }
299
6f02a879
VZ
300protected:
301 /// Returns the HTML for this item
302 virtual wxString OnGetItem(size_t n) const;
303
5d7836c4
JS
304private:
305
306 wxRichTextStyleSheet* m_styleSheet;
307 wxRichTextCtrl* m_richTextCtrl;
86015e55 308 bool m_applyOnSelection; // if true, applies style on selection
5d7836c4 309};
e637208a
JS
310
311#if wxUSE_COMBOCTRL
312
313/*!
314 * Style drop-down for a wxComboCtrl
315 */
316
317class wxRichTextStyleComboPopup : public wxRichTextStyleListBox, public wxComboPopup
318{
319public:
320 virtual void Init()
321 {
322 m_itemHere = -1; // hot item in list
323 m_value = -1;
324 }
325
326 virtual bool Create( wxWindow* parent )
327 {
328 return wxRichTextStyleListBox::Create(parent, wxID_ANY,
329 wxPoint(0,0), wxDefaultSize,
330 wxSIMPLE_BORDER);
331 }
332
333 virtual wxWindow *GetControl() { return this; }
334
335 virtual void SetStringValue( const wxString& s );
336
337 virtual wxString GetStringValue() const;
338
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; }
342
343 //
344 // Popup event handlers
345 //
346
347 // Mouse hot-tracking
348 void OnMouseMove(wxMouseEvent& event);
349
350 // On mouse left, set the value and close the popup
351 void OnMouseClick(wxMouseEvent& WXUNUSED(event));
352
353protected:
354
355 int m_itemHere; // hot item in popup
356 int m_value;
357
358private:
359 DECLARE_EVENT_TABLE()
360};
361
362/*!
363 * wxRichTextStyleComboCtrl
364 * A combo for applying styles.
365 */
366
367class WXDLLIMPEXP_RICHTEXT wxRichTextStyleComboCtrl: public wxComboCtrl
368{
369 DECLARE_CLASS(wxRichTextStyleComboCtrl)
370 DECLARE_EVENT_TABLE()
371
372public:
373 wxRichTextStyleComboCtrl()
374 {
375 Init();
376 }
377
378 wxRichTextStyleComboCtrl(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
379 const wxSize& size = wxDefaultSize, long style = wxCB_READONLY)
380 {
381 Init();
382 Create(parent, id, pos, size, style);
383 }
384
385 virtual ~wxRichTextStyleComboCtrl() {}
386
387 void Init()
388 {
389 m_stylePopup = NULL;
390 }
391
392 bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
393 const wxSize& size = wxDefaultSize, long style = 0);
394
395 /// Updates the list
396 void UpdateStyles() { m_stylePopup->UpdateStyles(); }
397
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(); }
401
402 /// Associates the control with a wxRichTextCtrl
403 void SetRichTextCtrl(wxRichTextCtrl* ctrl) { m_stylePopup->SetRichTextCtrl(ctrl); }
404 wxRichTextCtrl* GetRichTextCtrl() const { return m_stylePopup->GetRichTextCtrl(); }
405
406 /// Gets the style popup
407 wxRichTextStyleComboPopup* GetStylePopup() const { return m_stylePopup; }
408
409 /// Auto-select from style under caret in idle time
410 void OnIdle(wxIdleEvent& event);
411
412protected:
413 wxRichTextStyleComboPopup* m_stylePopup;
414};
415
416#endif
417 // wxUSE_COMBOCTRL
418
5d7836c4 419#endif
e637208a 420 // wxUSE_HTML
5d7836c4
JS
421
422#endif
423 // wxUSE_RICHTEXT
424
425#endif
426 // _WX_RICHTEXTSTYLES_H_