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