]> git.saurik.com Git - wxWidgets.git/blob - include/wx/richtext/richtextstyles.h
Return type change
[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 virtual wxRichTextStyleDefinition* Clone() const = 0;
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
80 protected:
81 wxString m_name;
82 wxString m_baseStyle;
83 wxRichTextAttr m_style;
84 };
85
86 /*!
87 * wxRichTextCharacterStyleDefinition class declaration
88 */
89
90 class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition: public wxRichTextStyleDefinition
91 {
92 DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition)
93 public:
94
95 // Constructors
96
97 wxRichTextCharacterStyleDefinition(const wxRichTextCharacterStyleDefinition& def): wxRichTextStyleDefinition(def) {}
98 wxRichTextCharacterStyleDefinition(const wxString& name = wxEmptyString):
99 wxRichTextStyleDefinition(name) {}
100 virtual ~wxRichTextCharacterStyleDefinition() {}
101
102 virtual wxRichTextStyleDefinition* Clone() const { return new wxRichTextCharacterStyleDefinition(*this); }
103
104 protected:
105 };
106
107 /*!
108 * wxRichTextParagraphStyleDefinition class declaration
109 */
110
111 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition: public wxRichTextStyleDefinition
112 {
113 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition)
114 public:
115
116 // Constructors
117
118 wxRichTextParagraphStyleDefinition(const wxRichTextParagraphStyleDefinition& def): wxRichTextStyleDefinition(def) { m_nextStyle = def.m_nextStyle; }
119 wxRichTextParagraphStyleDefinition(const wxString& name = wxEmptyString):
120 wxRichTextStyleDefinition(name) {}
121 virtual ~wxRichTextParagraphStyleDefinition() {}
122
123 /// The next style.
124 void SetNextStyle(const wxString& name) { m_nextStyle = name; }
125 const wxString& GetNextStyle() const { return m_nextStyle; }
126
127 void Copy(const wxRichTextParagraphStyleDefinition& def);
128 void operator =(const wxRichTextParagraphStyleDefinition& def) { Copy(def); }
129 bool operator ==(const wxRichTextParagraphStyleDefinition& def) const;
130
131 virtual wxRichTextStyleDefinition* Clone() const { return new wxRichTextParagraphStyleDefinition(*this); }
132
133 protected:
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
143 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet: public wxObject
144 {
145 DECLARE_CLASS( wxRichTextStyleSheet )
146
147 public:
148 /// Constructors
149 wxRichTextStyleSheet(const wxRichTextStyleSheet& sheet)
150 : wxObject()
151 {
152 Copy(sheet);
153 }
154 wxRichTextStyleSheet() { Init(); }
155 virtual ~wxRichTextStyleSheet() { DeleteStyles(); }
156
157 /// Initialisation
158 void Init();
159
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
169 /// Add a definition to the character style list
170 bool AddCharacterStyle(wxRichTextCharacterStyleDefinition* def);
171
172 /// Add a definition to the paragraph style list
173 bool AddParagraphStyle(wxRichTextParagraphStyleDefinition* def);
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
179 bool RemoveParagraphStyle(wxRichTextStyleDefinition* def, bool deleteStyle = false) { return RemoveStyle(m_paragraphStyleDefinitions, def, deleteStyle); }
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
185 wxRichTextParagraphStyleDefinition* FindParagraphStyle(const wxString& name) const { return (wxRichTextParagraphStyleDefinition*) FindStyle(m_paragraphStyleDefinitions, name); }
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
213 protected:
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
225 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox: public wxHtmlListBox
226 {
227 DECLARE_CLASS(wxRichTextStyleListBox)
228 DECLARE_EVENT_TABLE()
229
230 public:
231 wxRichTextStyleListBox()
232 {
233 Init();
234 }
235 wxRichTextStyleListBox(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
236 const wxSize& size = wxDefaultSize, long style = 0);
237 virtual ~wxRichTextStyleListBox();
238
239 void Init()
240 {
241 m_styleSheet = NULL;
242 m_richTextCtrl = NULL;
243 m_applyOnSelection = true;
244 }
245
246 bool Create(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
247 const wxSize& size = wxDefaultSize, long style = 0);
248
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
260 /// Get style for index
261 wxRichTextStyleDefinition* GetStyle(size_t i) const ;
262
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
269 /// Updates the list
270 void UpdateStyles();
271
272 /// Apply the style
273 void ApplyStyle(int i);
274
275 /// React to selection
276 void OnSelect(wxCommandEvent& event);
277
278 /// Left click
279 void OnLeftDown(wxMouseEvent& event);
280
281 /// Auto-select from style under caret in idle time
282 void OnIdle(wxIdleEvent& event);
283
284 #if 0
285 virtual wxColour GetSelectedTextColour(const wxColour& colFg) const;
286 virtual wxColour GetSelectedTextBgColour(const wxColour& colBg) const;
287 #endif
288
289 /// Convert units in tends of a millimetre to device units
290 int ConvertTenthsMMToPixels(wxDC& dc, int units) const;
291
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
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
300 protected:
301 /// Returns the HTML for this item
302 virtual wxString OnGetItem(size_t n) const;
303
304 private:
305
306 wxRichTextStyleSheet* m_styleSheet;
307 wxRichTextCtrl* m_richTextCtrl;
308 bool m_applyOnSelection; // if true, applies style on selection
309 };
310
311 #if wxUSE_COMBOCTRL
312
313 /*!
314 * Style drop-down for a wxComboCtrl
315 */
316
317 class wxRichTextStyleComboPopup : public wxRichTextStyleListBox, public wxComboPopup
318 {
319 public:
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
353 protected:
354
355 int m_itemHere; // hot item in popup
356 int m_value;
357
358 private:
359 DECLARE_EVENT_TABLE()
360 };
361
362 /*!
363 * wxRichTextStyleComboCtrl
364 * A combo for applying styles.
365 */
366
367 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleComboCtrl: public wxComboCtrl
368 {
369 DECLARE_CLASS(wxRichTextStyleComboCtrl)
370 DECLARE_EVENT_TABLE()
371
372 public:
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
412 protected:
413 wxRichTextStyleComboPopup* m_stylePopup;
414 };
415
416 #endif
417 // wxUSE_COMBOCTRL
418
419 #endif
420 // wxUSE_HTML
421
422 #endif
423 // wxUSE_RICHTEXT
424
425 #endif
426 // _WX_RICHTEXTSTYLES_H_