]>
Commit | Line | Data |
---|---|---|
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 |
37 | class WXDLLIMPEXP_RICHTEXT wxRichTextCtrl; |
38 | class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer; | |
5d7836c4 JS |
39 | |
40 | /*! | |
41 | * wxRichTextStyleDefinition class declaration | |
42 | * A base class for paragraph and character styles. | |
43 | */ | |
44 | ||
3b2cb431 | 45 | class WXDLLIMPEXP_RICHTEXT wxRichTextStyleDefinition: public wxObject |
5d7836c4 JS |
46 | { |
47 | DECLARE_CLASS(wxRichTextStyleDefinition) | |
48 | public: | |
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); } | |
5d7836c4 JS |
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 | ||
3b2cb431 | 89 | class WXDLLIMPEXP_RICHTEXT wxRichTextCharacterStyleDefinition: public wxRichTextStyleDefinition |
5d7836c4 JS |
90 | { |
91 | DECLARE_DYNAMIC_CLASS(wxRichTextCharacterStyleDefinition) | |
92 | public: | |
93 | ||
94 | // Constructors | |
95 | ||
fe5aa22c | 96 | wxRichTextCharacterStyleDefinition(const wxRichTextCharacterStyleDefinition& def): wxRichTextStyleDefinition(def) {} |
5d7836c4 JS |
97 | wxRichTextCharacterStyleDefinition(const wxString& name = wxEmptyString): |
98 | wxRichTextStyleDefinition(name) {} | |
d3c7fc99 | 99 | virtual ~wxRichTextCharacterStyleDefinition() {} |
5d7836c4 JS |
100 | |
101 | protected: | |
102 | }; | |
103 | ||
104 | /*! | |
105 | * wxRichTextParagraphStyleDefinition class declaration | |
106 | */ | |
107 | ||
3b2cb431 | 108 | class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphStyleDefinition: public wxRichTextStyleDefinition |
5d7836c4 JS |
109 | { |
110 | DECLARE_DYNAMIC_CLASS(wxRichTextParagraphStyleDefinition) | |
111 | public: | |
112 | ||
113 | // Constructors | |
114 | ||
fe5aa22c | 115 | wxRichTextParagraphStyleDefinition(const wxRichTextParagraphStyleDefinition& def): wxRichTextStyleDefinition(def) { m_nextStyle = def.m_nextStyle; } |
5d7836c4 JS |
116 | wxRichTextParagraphStyleDefinition(const wxString& name = wxEmptyString): |
117 | wxRichTextStyleDefinition(name) {} | |
d3c7fc99 | 118 | virtual ~wxRichTextParagraphStyleDefinition() {} |
5d7836c4 JS |
119 | |
120 | /// The next style. | |
121 | void SetNextStyle(const wxString& name) { m_nextStyle = name; } | |
122 | const wxString& GetNextStyle() const { return m_nextStyle; } | |
123 | ||
fe5aa22c JS |
124 | void Copy(const wxRichTextParagraphStyleDefinition& def); |
125 | void operator =(const wxRichTextParagraphStyleDefinition& def) { Copy(def); } | |
126 | bool operator ==(const wxRichTextParagraphStyleDefinition& def) const; | |
127 | ||
5d7836c4 JS |
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 | ||
3b2cb431 | 138 | class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet: public wxObject |
5d7836c4 JS |
139 | { |
140 | DECLARE_CLASS( wxRichTextStyleSheet ) | |
141 | ||
142 | public: | |
143 | /// Constructors | |
b3298d1d WS |
144 | wxRichTextStyleSheet(const wxRichTextStyleSheet& sheet) |
145 | : wxObject() | |
146 | { | |
147 | Copy(sheet); | |
148 | } | |
5d7836c4 | 149 | wxRichTextStyleSheet() { Init(); } |
d3c7fc99 | 150 | virtual ~wxRichTextStyleSheet() { DeleteStyles(); } |
5d7836c4 JS |
151 | |
152 | /// Initialisation | |
153 | void Init(); | |
154 | ||
fe5aa22c JS |
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 | ||
5d7836c4 | 164 | /// Add a definition to the character style list |
fe5aa22c | 165 | bool AddCharacterStyle(wxRichTextCharacterStyleDefinition* def); |
5d7836c4 JS |
166 | |
167 | /// Add a definition to the paragraph style list | |
fe5aa22c | 168 | bool AddParagraphStyle(wxRichTextParagraphStyleDefinition* def); |
5d7836c4 JS |
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 | |
fe5aa22c | 174 | bool RemoveParagraphStyle(wxRichTextStyleDefinition* def, bool deleteStyle = false) { return RemoveStyle(m_paragraphStyleDefinitions, def, deleteStyle); } |
5d7836c4 JS |
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 | |
fe5aa22c | 180 | wxRichTextParagraphStyleDefinition* FindParagraphStyle(const wxString& name) const { return (wxRichTextParagraphStyleDefinition*) FindStyle(m_paragraphStyleDefinitions, name); } |
5d7836c4 JS |
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 | ||
3b2cb431 | 220 | class WXDLLIMPEXP_RICHTEXT wxRichTextStyleListBox: public wxHtmlListBox |
5d7836c4 JS |
221 | { |
222 | DECLARE_CLASS(wxRichTextStyleListBox) | |
223 | DECLARE_EVENT_TABLE() | |
224 | ||
225 | public: | |
e637208a JS |
226 | wxRichTextStyleListBox() |
227 | { | |
228 | Init(); | |
229 | } | |
5d7836c4 JS |
230 | wxRichTextStyleListBox(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, |
231 | const wxSize& size = wxDefaultSize, long style = 0); | |
d3c7fc99 | 232 | virtual ~wxRichTextStyleListBox(); |
5d7836c4 | 233 | |
e637208a JS |
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 | ||
5d7836c4 JS |
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 | ||
e637208a | 254 | /// Get style for index |
5d7836c4 JS |
255 | wxRichTextStyleDefinition* GetStyle(size_t i) const ; |
256 | ||
e637208a JS |
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 | ||
5d7836c4 JS |
263 | /// Updates the list |
264 | void UpdateStyles(); | |
265 | ||
e637208a JS |
266 | /// Do selection |
267 | void DoSelection(int i); | |
268 | ||
5d7836c4 JS |
269 | /// React to selection |
270 | void OnSelect(wxCommandEvent& event); | |
271 | ||
272 | /// Left click | |
273 | void OnLeftDown(wxMouseEvent& event); | |
274 | ||
e637208a JS |
275 | /// Auto-select from style under caret in idle time |
276 | void OnIdle(wxIdleEvent& event); | |
277 | ||
5d7836c4 JS |
278 | #if 0 |
279 | virtual wxColour GetSelectedTextColour(const wxColour& colFg) const; | |
280 | virtual wxColour GetSelectedTextBgColour(const wxColour& colBg) const; | |
281 | #endif | |
282 | ||
e637208a | 283 | /// Convert units in tends of a millimetre to device units |
5d7836c4 JS |
284 | int ConvertTenthsMMToPixels(wxDC& dc, int units) const; |
285 | ||
e637208a JS |
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 | ||
6f02a879 VZ |
290 | protected: |
291 | /// Returns the HTML for this item | |
292 | virtual wxString OnGetItem(size_t n) const; | |
293 | ||
5d7836c4 JS |
294 | private: |
295 | ||
296 | wxRichTextStyleSheet* m_styleSheet; | |
297 | wxRichTextCtrl* m_richTextCtrl; | |
298 | }; | |
e637208a JS |
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 | ||
5d7836c4 | 408 | #endif |
e637208a | 409 | // wxUSE_HTML |
5d7836c4 JS |
410 | |
411 | #endif | |
412 | // wxUSE_RICHTEXT | |
413 | ||
414 | #endif | |
415 | // _WX_RICHTEXTSTYLES_H_ |