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