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