]> git.saurik.com Git - wxWidgets.git/blob - include/wx/richtext/richtextbuffer.h
Added rich text buffer clipboard support.
[wxWidgets.git] / include / wx / richtext / richtextbuffer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/richtext/richtextbuffer.h
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2005-09-30
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_RICHTEXTBUFFER_H_
13 #define _WX_RICHTEXTBUFFER_H_
14
15 /*
16
17 Data structures
18 ===============
19
20 Data is represented by a hierarchy of objects, all derived from
21 wxRichTextObject.
22
23 The top of the hierarchy is the buffer, a kind of wxRichTextParagraphLayoutBox.
24 These boxes will allow flexible placement of text boxes on a page, but
25 for now there will be a single box representing the document,
26 and this box will a wxRichTextParagraphLayoutBox which contains further
27 wxRichTextParagraph objects, each of which can include text and images.
28
29 Each object maintains a range (start and end position) measured
30 from the start of the main parent box.
31 A paragraph object knows its range, and a text fragment knows its range
32 too. So, a character or image in a page has a position relative to the
33 start of the document, and a character in an embedded text box has
34 a position relative to that text box. For now, we will not be dealing with
35 embedded objects but it's something to bear in mind for later.
36
37 Layout
38 ======
39
40 When Layout is called on an object, it is given a size which the object
41 must limit itself to, or one or more flexible directions (vertical
42 or horizontal). So for example a centered paragraph is given the page
43 width to play with (minus any margins), but can extend indefinitely
44 in the vertical direction. The implementation of Layout can then
45 cache the calculated size and position within the parent.
46
47 Note that position and size should be calculated separately, because
48 for example inserting a paragraph may result in the following paragraphs
49 moving down, but not changing in size.
50
51 Need to determine how objects specify their position. Absolute coordinates,
52 or relative to last object? May be hard to determine that. So should probably
53 be in absolute coordinates, in which case we'll need a Move virtual function
54 that allows quick movement of all elements without layout.
55
56 Let's work through a simple example of layout. Say we're laying out
57 a document with the buffer as the top box, with a wxRichTextParagraphLayoutBox
58 inside that that consists of wxRichTextParagraph objects.
59
60 We're in a mode whereby changes of window size change the width of the
61 page (useful for text editors, as opposed to word processors). The
62 window width is 600.
63
64 We pass (600, -1) to the top-level Layout (i.e. restrict size in horizontal
65 direction only). The wxRichTextBuffer box doesn't currently have
66 well-defined layout behaviour so we simply assume it has one child
67 that fills its parent (later we can define sizer-like box layout behaviour).
68 So it passes the same dimensions to the child, which is a wxRichTextParagraphLayoutBox.
69 This then looks at each child in turn (wxRichTextParagraph) and determines
70 the size the paragraph will take up, setting the cached size, and
71 splitting the paragraph into lines.
72
73 When the layout for one paragraph returns, the next paragraph is
74 fed the position of the previous, so it can position itself.
75
76 Each time Layout is called, the cached list of lines for each paragraph
77 is recreated, since it can change for example if the parent object width
78 changes.
79
80 Reporting size
81 ==============
82
83 Each object can report its size for a given range. It's important that
84 it can report a partial size, so that wrapping can be implemented,
85 hit test calculations performed, etc. So GetRangeSize must be implemented
86 for each object.
87
88 */
89
90 /*!
91 * Includes
92 */
93
94 #include "wx/defs.h"
95
96 #if wxUSE_RICHTEXT
97
98 #include "wx/list.h"
99 #include "wx/textctrl.h"
100 #include "wx/bitmap.h"
101 #include "wx/image.h"
102 #include "wx/cmdproc.h"
103 #include "wx/txtstrm.h"
104
105 #if wxUSE_DATAOBJ
106 #include "wx/dataobj.h"
107 #endif
108
109 // Experimental dynamic styles to avoid user-specific character styles from being
110 // overwritten by paragraph styles.
111 #define wxRICHTEXT_USE_DYNAMIC_STYLES 1
112
113 /*!
114 * File types
115 */
116
117 #define wxRICHTEXT_TYPE_ANY 0
118 #define wxRICHTEXT_TYPE_TEXT 1
119 #define wxRICHTEXT_TYPE_XML 2
120 #define wxRICHTEXT_TYPE_HTML 3
121 #define wxRICHTEXT_TYPE_RTF 4
122 #define wxRICHTEXT_TYPE_PDF 5
123
124 /*!
125 * Forward declarations
126 */
127
128 class WXDLLIMPEXP_RICHTEXT wxRichTextCtrl;
129 class WXDLLIMPEXP_RICHTEXT wxRichTextObject;
130 class WXDLLIMPEXP_RICHTEXT wxRichTextCacheObject;
131 class WXDLLIMPEXP_RICHTEXT wxRichTextObjectList;
132 class WXDLLIMPEXP_RICHTEXT wxRichTextLine;
133 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraph;
134 class WXDLLIMPEXP_RICHTEXT wxRichTextFileHandler;
135 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet;
136 class WXDLLIMPEXP_RICHTEXT wxTextAttrEx;
137
138 /*!
139 * Flags determining the available space, passed to Layout
140 */
141
142 #define wxRICHTEXT_FIXED_WIDTH 0x01
143 #define wxRICHTEXT_FIXED_HEIGHT 0x02
144 #define wxRICHTEXT_VARIABLE_WIDTH 0x04
145 #define wxRICHTEXT_VARIABLE_HEIGHT 0x08
146
147 // Only lay out the part of the buffer that lies within
148 // the rect passed to Layout.
149 #define wxRICHTEXT_LAYOUT_SPECIFIED_RECT 0x10
150
151 /*!
152 * Flags returned from hit-testing
153 */
154
155 // The point was not on this object
156 #define wxRICHTEXT_HITTEST_NONE 0x01
157 // The point was before the position returned from HitTest
158 #define wxRICHTEXT_HITTEST_BEFORE 0x02
159 // The point was after the position returned from HitTest
160 #define wxRICHTEXT_HITTEST_AFTER 0x04
161 // The point was on the position returned from HitTest
162 #define wxRICHTEXT_HITTEST_ON 0x08
163
164 /*!
165 * Flags for GetRangeSize
166 */
167
168 #define wxRICHTEXT_FORMATTED 0x01
169 #define wxRICHTEXT_UNFORMATTED 0x02
170
171 /*!
172 * Flags for text insertion
173 */
174
175 #define wxRICHTEXT_INSERT_NONE 0x00
176 #define wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE 0x01
177
178 /*!
179 * Extra formatting flags not in wxTextAttr
180 */
181
182 #define wxTEXT_ATTR_PARA_SPACING_AFTER 0x00000800
183 #define wxTEXT_ATTR_PARA_SPACING_BEFORE 0x00001000
184 #define wxTEXT_ATTR_LINE_SPACING 0x00002000
185 #define wxTEXT_ATTR_CHARACTER_STYLE_NAME 0x00004000
186 #define wxTEXT_ATTR_PARAGRAPH_STYLE_NAME 0x00008000
187 #define wxTEXT_ATTR_BULLET_STYLE 0x00010000
188 #define wxTEXT_ATTR_BULLET_NUMBER 0x00020000
189 #define wxTEXT_ATTR_BULLET_SYMBOL 0x00040000
190
191 /*!
192 * Styles for wxTextAttrEx::SetBulletStyle
193 */
194
195 #define wxTEXT_ATTR_BULLET_STYLE_NONE 0x0000
196 #define wxTEXT_ATTR_BULLET_STYLE_ARABIC 0x0001
197 #define wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER 0x0002
198 #define wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER 0x0004
199 #define wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER 0x0008
200 #define wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER 0x0010
201 #define wxTEXT_ATTR_BULLET_STYLE_SYMBOL 0x0020
202 #define wxTEXT_ATTR_BULLET_STYLE_BITMAP 0x0040
203 #define wxTEXT_ATTR_BULLET_STYLE_PARENTHESES 0x0080
204 #define wxTEXT_ATTR_BULLET_STYLE_PERIOD 0x0100
205
206 /*!
207 * Line spacing values
208 */
209
210 #define wxTEXT_ATTR_LINE_SPACING_NORMAL 10
211 #define wxTEXT_ATTR_LINE_SPACING_HALF 15
212 #define wxTEXT_ATTR_LINE_SPACING_TWICE 20
213
214 /*!
215 * wxRichTextRange class declaration
216 * This stores beginning and end positions for a range of data.
217 */
218
219 class WXDLLIMPEXP_RICHTEXT wxRichTextRange
220 {
221 public:
222 // Constructors
223
224 wxRichTextRange() { m_start = 0; m_end = 0; }
225 wxRichTextRange(long start, long end) { m_start = start; m_end = end; }
226 wxRichTextRange(const wxRichTextRange& range) { m_start = range.m_start; m_end = range.m_end; }
227 ~wxRichTextRange() {}
228
229 void operator =(const wxRichTextRange& range) { m_start = range.m_start; m_end = range.m_end; }
230 bool operator ==(const wxRichTextRange& range) const { return (m_start == range.m_start && m_end == range.m_end); }
231 bool operator !=(const wxRichTextRange& range) const { return (m_start != range.m_start && m_end != range.m_end); }
232 wxRichTextRange operator -(const wxRichTextRange& range) const { return wxRichTextRange(m_start - range.m_start, m_end - range.m_end); }
233 wxRichTextRange operator +(const wxRichTextRange& range) const { return wxRichTextRange(m_start + range.m_start, m_end + range.m_end); }
234
235 void SetRange(long start, long end) { m_start = start; m_end = end; }
236
237 void SetStart(long start) { m_start = start; }
238 long GetStart() const { return m_start; }
239
240 void SetEnd(long end) { m_end = end; }
241 long GetEnd() const { return m_end; }
242
243 /// Returns true if this range is completely outside 'range'
244 bool IsOutside(const wxRichTextRange& range) const { return range.m_start > m_end || range.m_end < m_start; }
245
246 /// Returns true if this range is completely within 'range'
247 bool IsWithin(const wxRichTextRange& range) const { return m_start >= range.m_start && m_end <= range.m_end; }
248
249 /// Returns true if the given position is within this range. Allow
250 /// for the possibility of an empty range - assume the position
251 /// is within this empty range. NO, I think we should not match with an empty range.
252 // bool Contains(long pos) const { return pos >= m_start && (pos <= m_end || GetLength() == 0); }
253 bool Contains(long pos) const { return pos >= m_start && pos <= m_end ; }
254
255 /// Limit this range to be within 'range'
256 bool LimitTo(const wxRichTextRange& range) ;
257
258 /// Gets the length of the range
259 long GetLength() const { return m_end - m_start + 1; }
260
261 /// Swaps the start and end
262 void Swap() { long tmp = m_start; m_start = m_end; m_end = tmp; }
263
264 /// Convert to internal form: (n, n) is the range of a single character.
265 wxRichTextRange ToInternal() const { return wxRichTextRange(m_start, m_end-1); }
266
267 /// Convert from internal to public API form: (n, n+1) is the range of a single character.
268 wxRichTextRange FromInternal() const { return wxRichTextRange(m_start, m_end+1); }
269
270 protected:
271 long m_start;
272 long m_end;
273 };
274
275 #define wxRICHTEXT_ALL wxRichTextRange(-2, -2)
276 #define wxRICHTEXT_NONE wxRichTextRange(-1, -1)
277
278 /*!
279 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
280 */
281
282 class WXDLLIMPEXP_RICHTEXT wxTextAttrEx: public wxTextAttr
283 {
284 public:
285 // ctors
286 wxTextAttrEx(const wxTextAttrEx& attr);
287 wxTextAttrEx(const wxTextAttr& attr) { Init(); (*this) = attr; }
288 wxTextAttrEx() { Init(); }
289
290 // Initialise this object.
291 void Init();
292
293 // Assignment from a wxTextAttrEx object
294 void operator= (const wxTextAttrEx& attr);
295
296 // Assignment from a wxTextAttr object.
297 void operator= (const wxTextAttr& attr);
298
299 // setters
300 void SetCharacterStyleName(const wxString& name) { m_characterStyleName = name; SetFlags(GetFlags() | wxTEXT_ATTR_CHARACTER_STYLE_NAME); }
301 void SetParagraphStyleName(const wxString& name) { m_paragraphStyleName = name; SetFlags(GetFlags() | wxTEXT_ATTR_PARAGRAPH_STYLE_NAME); }
302 void SetParagraphSpacingAfter(int spacing) { m_paragraphSpacingAfter = spacing; SetFlags(GetFlags() | wxTEXT_ATTR_PARA_SPACING_AFTER); }
303 void SetParagraphSpacingBefore(int spacing) { m_paragraphSpacingBefore = spacing; SetFlags(GetFlags() | wxTEXT_ATTR_PARA_SPACING_BEFORE); }
304 void SetLineSpacing(int spacing) { m_lineSpacing = spacing; SetFlags(GetFlags() | wxTEXT_ATTR_LINE_SPACING); }
305 void SetBulletStyle(int style) { m_bulletStyle = style; SetFlags(GetFlags() | wxTEXT_ATTR_BULLET_STYLE); }
306 void SetBulletNumber(int n) { m_bulletNumber = n; SetFlags(GetFlags() | wxTEXT_ATTR_BULLET_NUMBER); }
307 void SetBulletSymbol(wxChar symbol) { m_bulletSymbol = symbol; SetFlags(GetFlags() | wxTEXT_ATTR_BULLET_SYMBOL); }
308
309 const wxString& GetCharacterStyleName() const { return m_characterStyleName; }
310 const wxString& GetParagraphStyleName() const { return m_paragraphStyleName; }
311 int GetParagraphSpacingAfter() const { return m_paragraphSpacingAfter; }
312 int GetParagraphSpacingBefore() const { return m_paragraphSpacingBefore; }
313 int GetLineSpacing() const { return m_lineSpacing; }
314 int GetBulletStyle() const { return m_bulletStyle; }
315 int GetBulletNumber() const { return m_bulletNumber; }
316 wxChar GetBulletSymbol() const { return m_bulletSymbol; }
317
318 bool HasWeight() const { return (GetFlags() & wxTEXT_ATTR_FONT_WEIGHT) != 0; }
319 bool HasSize() const { return (GetFlags() & wxTEXT_ATTR_FONT_SIZE) != 0; }
320 bool HasItalic() const { return (GetFlags() & wxTEXT_ATTR_FONT_ITALIC) != 0; }
321 bool HasUnderlined() const { return (GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE) != 0; }
322 bool HasFaceName() const { return (GetFlags() & wxTEXT_ATTR_FONT_FACE) != 0; }
323
324 bool HasParagraphSpacingAfter() const { return HasFlag(wxTEXT_ATTR_PARA_SPACING_AFTER); }
325 bool HasParagraphSpacingBefore() const { return HasFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE); }
326 bool HasLineSpacing() const { return HasFlag(wxTEXT_ATTR_LINE_SPACING); }
327 bool HasCharacterStyleName() const { return HasFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME) || !m_characterStyleName.IsEmpty(); }
328 bool HasParagraphStyleName() const { return HasFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) || !m_paragraphStyleName.IsEmpty(); }
329 bool HasBulletStyle() const { return HasFlag(wxTEXT_ATTR_BULLET_STYLE); }
330 bool HasBulletNumber() const { return HasFlag(wxTEXT_ATTR_BULLET_NUMBER); }
331 bool HasBulletSymbol() const { return HasFlag(wxTEXT_ATTR_BULLET_SYMBOL); }
332
333 // Is this a character style?
334 bool IsCharacterStyle() const { return (0 != (GetFlags() & (wxTEXT_ATTR_FONT | wxTEXT_ATTR_BACKGROUND_COLOUR | wxTEXT_ATTR_TEXT_COLOUR))); }
335 bool IsParagraphStyle() const { return (0 != (GetFlags() & (wxTEXT_ATTR_ALIGNMENT|wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_TABS|
336 wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|
337 wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER))); }
338
339 // returns false if we have any attributes set, true otherwise
340 bool IsDefault() const
341 {
342 return !HasTextColour() && !HasBackgroundColour() && !HasFont() && !HasAlignment() &&
343 !HasTabs() && !HasLeftIndent() && !HasRightIndent() &&
344 !HasParagraphSpacingAfter() && !HasParagraphSpacingBefore() && !HasLineSpacing() &&
345 !HasCharacterStyleName() && !HasParagraphStyleName() && !HasBulletNumber() && !HasBulletStyle() && !HasBulletSymbol();
346 }
347
348 // return the attribute having the valid font and colours: it uses the
349 // attributes set in attr and falls back first to attrDefault and then to
350 // the text control font/colours for those attributes which are not set
351 static wxTextAttrEx CombineEx(const wxTextAttrEx& attr,
352 const wxTextAttrEx& attrDef,
353 const wxTextCtrlBase *text);
354
355 private:
356 // Paragraph styles
357 int m_paragraphSpacingAfter;
358 int m_paragraphSpacingBefore;
359 int m_lineSpacing;
360 int m_bulletStyle;
361 int m_bulletNumber;
362 wxChar m_bulletSymbol;
363
364 // Character style
365 wxString m_characterStyleName;
366
367 // Paragraph style
368 wxString m_paragraphStyleName;
369 };
370
371 /*!
372 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
373 * efficient way to query styles.
374 */
375
376 class WXDLLIMPEXP_RICHTEXT wxRichTextAttr
377 {
378 public:
379 // ctors
380 wxRichTextAttr(const wxTextAttrEx& attr);
381 wxRichTextAttr() { Init(); }
382 wxRichTextAttr(const wxColour& colText,
383 const wxColour& colBack = wxNullColour,
384 wxTextAttrAlignment alignment = wxTEXT_ALIGNMENT_DEFAULT);
385
386 // Initialise this object.
387 void Init();
388
389 // Assignment from a wxRichTextAttr object.
390 void operator= (const wxRichTextAttr& attr);
391
392 // Assignment from a wxTextAttrEx object.
393 void operator= (const wxTextAttrEx& attr);
394
395 // Equality test
396 bool operator== (const wxRichTextAttr& attr) const;
397
398 // Making a wxTextAttrEx object.
399 operator wxTextAttrEx () const ;
400
401 // Copy to a wxTextAttr
402 void CopyTo(wxTextAttrEx& attr) const;
403
404 // Create font from font attributes.
405 wxFont CreateFont() const;
406
407 // Get attributes from font.
408 bool GetFontAttributes(const wxFont& font);
409
410 // setters
411 void SetTextColour(const wxColour& colText) { m_colText = colText; m_flags |= wxTEXT_ATTR_TEXT_COLOUR; }
412 void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR; }
413 void SetAlignment(wxTextAttrAlignment alignment) { m_textAlignment = alignment; m_flags |= wxTEXT_ATTR_ALIGNMENT; }
414 void SetTabs(const wxArrayInt& tabs) { m_tabs = tabs; m_flags |= wxTEXT_ATTR_TABS; }
415 void SetLeftIndent(int indent, int subIndent = 0) { m_leftIndent = indent; m_leftSubIndent = subIndent; m_flags |= wxTEXT_ATTR_LEFT_INDENT; }
416 void SetRightIndent(int indent) { m_rightIndent = indent; m_flags |= wxTEXT_ATTR_RIGHT_INDENT; }
417
418 void SetFontSize(int pointSize) { m_fontSize = pointSize; m_flags |= wxTEXT_ATTR_FONT_SIZE; }
419 void SetFontStyle(int fontStyle) { m_fontStyle = fontStyle; m_flags |= wxTEXT_ATTR_FONT_ITALIC; }
420 void SetFontWeight(int fontWeight) { m_fontWeight = fontWeight; m_flags |= wxTEXT_ATTR_FONT_WEIGHT; }
421 void SetFontFaceName(const wxString& faceName) { m_fontFaceName = faceName; m_flags |= wxTEXT_ATTR_FONT_FACE; }
422 void SetFontUnderlined(bool underlined) { m_fontUnderlined = underlined; m_flags |= wxTEXT_ATTR_FONT_UNDERLINE; }
423
424 void SetFlags(long flags) { m_flags = flags; }
425
426 void SetCharacterStyleName(const wxString& name) { m_characterStyleName = name; m_flags |= wxTEXT_ATTR_CHARACTER_STYLE_NAME; }
427 void SetParagraphStyleName(const wxString& name) { m_paragraphStyleName = name; m_flags |= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME; }
428 void SetParagraphSpacingAfter(int spacing) { m_paragraphSpacingAfter = spacing; m_flags |= wxTEXT_ATTR_PARA_SPACING_AFTER; }
429 void SetParagraphSpacingBefore(int spacing) { m_paragraphSpacingBefore = spacing; m_flags |= wxTEXT_ATTR_PARA_SPACING_BEFORE; }
430 void SetLineSpacing(int spacing) { m_lineSpacing = spacing; m_flags |= wxTEXT_ATTR_LINE_SPACING; }
431 void SetBulletStyle(int style) { m_bulletStyle = style; m_flags |= wxTEXT_ATTR_BULLET_STYLE; }
432 void SetBulletNumber(int n) { m_bulletNumber = n; m_flags |= wxTEXT_ATTR_BULLET_NUMBER; }
433 void SetBulletSymbol(wxChar symbol) { m_bulletSymbol = symbol; m_flags |= wxTEXT_ATTR_BULLET_NUMBER; }
434
435 const wxColour& GetTextColour() const { return m_colText; }
436 const wxColour& GetBackgroundColour() const { return m_colBack; }
437 wxTextAttrAlignment GetAlignment() const { return m_textAlignment; }
438 const wxArrayInt& GetTabs() const { return m_tabs; }
439 long GetLeftIndent() const { return m_leftIndent; }
440 long GetLeftSubIndent() const { return m_leftSubIndent; }
441 long GetRightIndent() const { return m_rightIndent; }
442 long GetFlags() const { return m_flags; }
443
444 int GetFontSize() const { return m_fontSize; }
445 int GetFontStyle() const { return m_fontStyle; }
446 int GetFontWeight() const { return m_fontWeight; }
447 bool GetFontUnderlined() const { return m_fontUnderlined; }
448 const wxString& GetFontFaceName() const { return m_fontFaceName; }
449
450 const wxString& GetCharacterStyleName() const { return m_characterStyleName; }
451 const wxString& GetParagraphStyleName() const { return m_paragraphStyleName; }
452 int GetParagraphSpacingAfter() const { return m_paragraphSpacingAfter; }
453 int GetParagraphSpacingBefore() const { return m_paragraphSpacingBefore; }
454 int GetLineSpacing() const { return m_lineSpacing; }
455 int GetBulletStyle() const { return m_bulletStyle; }
456 int GetBulletNumber() const { return m_bulletNumber; }
457 wxChar GetBulletSymbol() const { return m_bulletSymbol; }
458
459 // accessors
460 bool HasTextColour() const { return m_colText.Ok() && HasFlag(wxTEXT_ATTR_TEXT_COLOUR) ; }
461 bool HasBackgroundColour() const { return m_colBack.Ok() && HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR) ; }
462 bool HasAlignment() const { return (m_textAlignment != wxTEXT_ALIGNMENT_DEFAULT) || ((m_flags & wxTEXT_ATTR_ALIGNMENT) != 0) ; }
463 bool HasTabs() const { return (m_flags & wxTEXT_ATTR_TABS) != 0 ; }
464 bool HasLeftIndent() const { return (m_flags & wxTEXT_ATTR_LEFT_INDENT) != 0 ; }
465 bool HasRightIndent() const { return (m_flags & wxTEXT_ATTR_RIGHT_INDENT) != 0 ; }
466 bool HasWeight() const { return (m_flags & wxTEXT_ATTR_FONT_WEIGHT) != 0; }
467 bool HasSize() const { return (m_flags & wxTEXT_ATTR_FONT_SIZE) != 0; }
468 bool HasItalic() const { return (m_flags & wxTEXT_ATTR_FONT_ITALIC) != 0; }
469 bool HasUnderlined() const { return (m_flags & wxTEXT_ATTR_FONT_UNDERLINE) != 0; }
470 bool HasFaceName() const { return (m_flags & wxTEXT_ATTR_FONT_FACE) != 0; }
471 bool HasFont() const { return (m_flags & (wxTEXT_ATTR_FONT)) != 0; }
472
473 bool HasParagraphSpacingAfter() const { return (m_flags & wxTEXT_ATTR_PARA_SPACING_AFTER) != 0; }
474 bool HasParagraphSpacingBefore() const { return (m_flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) != 0; }
475 bool HasLineSpacing() const { return (m_flags & wxTEXT_ATTR_LINE_SPACING) != 0; }
476 bool HasCharacterStyleName() const { return (m_flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) != 0 || !m_characterStyleName.IsEmpty(); }
477 bool HasParagraphStyleName() const { return (m_flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) != 0 || !m_paragraphStyleName.IsEmpty(); }
478 bool HasBulletStyle() const { return (m_flags & wxTEXT_ATTR_BULLET_STYLE) != 0; }
479 bool HasBulletNumber() const { return (m_flags & wxTEXT_ATTR_BULLET_NUMBER) != 0; }
480 bool HasBulletSymbol() const { return (m_flags & wxTEXT_ATTR_BULLET_SYMBOL) != 0; }
481
482 bool HasFlag(long flag) const { return (m_flags & flag) != 0; }
483
484 // Is this a character style?
485 bool IsCharacterStyle() const { return (0 != (GetFlags() & (wxTEXT_ATTR_FONT | wxTEXT_ATTR_BACKGROUND_COLOUR | wxTEXT_ATTR_TEXT_COLOUR))); }
486 bool IsParagraphStyle() const { return (0 != (GetFlags() & (wxTEXT_ATTR_ALIGNMENT|wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_TABS|
487 wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|
488 wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER))); }
489
490 // returns false if we have any attributes set, true otherwise
491 bool IsDefault() const
492 {
493 return !HasTextColour() && !HasBackgroundColour() && !HasFont() && !HasAlignment() &&
494 !HasTabs() && !HasLeftIndent() && !HasRightIndent() &&
495 !HasParagraphSpacingAfter() && !HasParagraphSpacingBefore() && !HasLineSpacing() &&
496 !HasCharacterStyleName() && !HasParagraphStyleName() && !HasBulletNumber() && !HasBulletStyle() && !HasBulletSymbol();
497 }
498
499 // return the attribute having the valid font and colours: it uses the
500 // attributes set in attr and falls back first to attrDefault and then to
501 // the text control font/colours for those attributes which are not set
502 static wxRichTextAttr Combine(const wxRichTextAttr& attr,
503 const wxRichTextAttr& attrDef,
504 const wxTextCtrlBase *text);
505 private:
506 long m_flags;
507
508 // Paragraph styles
509 wxArrayInt m_tabs; // array of int: tab stops in 1/10 mm
510 int m_leftIndent; // left indent in 1/10 mm
511 int m_leftSubIndent; // left indent for all but the first
512 // line in a paragraph relative to the
513 // first line, in 1/10 mm
514 int m_rightIndent; // right indent in 1/10 mm
515 wxTextAttrAlignment m_textAlignment;
516
517 int m_paragraphSpacingAfter;
518 int m_paragraphSpacingBefore;
519 int m_lineSpacing;
520 int m_bulletStyle;
521 int m_bulletNumber;
522 wxChar m_bulletSymbol;
523
524 // Character styles
525 wxColour m_colText,
526 m_colBack;
527 int m_fontSize;
528 int m_fontStyle;
529 int m_fontWeight;
530 bool m_fontUnderlined;
531 wxString m_fontFaceName;
532
533 // Character style
534 wxString m_characterStyleName;
535
536 // Paragraph style
537 wxString m_paragraphStyleName;
538 };
539
540 #define wxTEXT_ATTR_CHARACTER (wxTEXT_ATTR_FONT | wxTEXT_ATTR_BACKGROUND_COLOUR | wxTEXT_ATTR_TEXT_COLOUR)
541
542 #define wxTEXT_ATTR_PARAGRAPH (wxTEXT_ATTR_ALIGNMENT|wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_TABS|\
543 wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|\
544 wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER|wxTEXT_ATTR_BULLET_SYMBOL)
545
546 #define wxTEXT_ATTR_ALL (wxTEXT_ATTR_CHARACTER|wxTEXT_ATTR_PARAGRAPH)
547
548 /*!
549 * wxRichTextObject class declaration
550 * This is the base for drawable objects.
551 */
552
553 class WXDLLIMPEXP_RICHTEXT wxRichTextObject: public wxObject
554 {
555 DECLARE_CLASS(wxRichTextObject)
556 public:
557 // Constructors
558
559 wxRichTextObject(wxRichTextObject* parent = NULL);
560 virtual ~wxRichTextObject();
561
562 // Overrideables
563
564 /// Draw the item, within the given range. Some objects may ignore the range (for
565 /// example paragraphs) while others must obey it (lines, to implement wrapping)
566 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style) = 0;
567
568 /// Lay the item out at the specified position with the given size constraint.
569 /// Layout must set the cached size.
570 virtual bool Layout(wxDC& dc, const wxRect& rect, int style) = 0;
571
572 /// Hit-testing: returns a flag indicating hit test details, plus
573 /// information about position
574 virtual int HitTest(wxDC& WXUNUSED(dc), const wxPoint& WXUNUSED(pt), long& WXUNUSED(textPosition)) { return false; }
575
576 /// Finds the absolute position and row height for the given character position
577 virtual bool FindPosition(wxDC& WXUNUSED(dc), long WXUNUSED(index), wxPoint& WXUNUSED(pt), int* WXUNUSED(height), bool WXUNUSED(forceLineStart)) { return false; }
578
579 /// Get the best size, i.e. the ideal starting size for this object irrespective
580 /// of available space. For a short text string, it will be the size that exactly encloses
581 /// the text. For a longer string, it might use the parent width for example.
582 virtual wxSize GetBestSize() const { return m_size; }
583
584 /// Get the object size for the given range. Returns false if the range
585 /// is invalid for this object.
586 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const = 0;
587
588 /// Do a split, returning an object containing the second part, and setting
589 /// the first part in 'this'.
590 virtual wxRichTextObject* DoSplit(long WXUNUSED(pos)) { return NULL; }
591
592 /// Calculate range. By default, guess that the object is 1 unit long.
593 virtual void CalculateRange(long start, long& end) { end = start ; m_range.SetRange(start, end); }
594
595 /// Delete range
596 virtual bool DeleteRange(const wxRichTextRange& WXUNUSED(range)) { return false; }
597
598 /// Returns true if the object is empty
599 virtual bool IsEmpty() const { return false; }
600
601 /// Get any text in this object for the given range
602 virtual wxString GetTextForRange(const wxRichTextRange& WXUNUSED(range)) const { return wxEmptyString; }
603
604 /// Returns true if this object can merge itself with the given one.
605 virtual bool CanMerge(wxRichTextObject* WXUNUSED(object)) const { return false; }
606
607 /// Returns true if this object merged itself with the given one.
608 /// The calling code will then delete the given object.
609 virtual bool Merge(wxRichTextObject* WXUNUSED(object)) { return false; }
610
611 /// Dump to output stream for debugging
612 virtual void Dump(wxTextOutputStream& stream);
613
614 // Accessors
615
616 /// Get/set the cached object size as calculated by Layout.
617 virtual wxSize GetCachedSize() const { return m_size; }
618 virtual void SetCachedSize(const wxSize& sz) { m_size = sz; }
619
620 /// Get/set the object position
621 virtual wxPoint GetPosition() const { return m_pos; }
622 virtual void SetPosition(const wxPoint& pos) { m_pos = pos; }
623
624 /// Get the rectangle enclosing the object
625 virtual wxRect GetRect() const { return wxRect(GetPosition(), GetCachedSize()); }
626
627 /// Set the range
628 void SetRange(const wxRichTextRange& range) { m_range = range; }
629
630 /// Get the range
631 const wxRichTextRange& GetRange() const { return m_range; }
632 wxRichTextRange& GetRange() { return m_range; }
633
634 /// Get/set dirty flag (whether the object needs Layout to be called)
635 virtual bool GetDirty() const { return m_dirty; }
636 virtual void SetDirty(bool dirty) { m_dirty = dirty; }
637
638 /// Is this composite?
639 virtual bool IsComposite() const { return false; }
640
641 /// Get/set the parent.
642 virtual wxRichTextObject* GetParent() const { return m_parent; }
643 virtual void SetParent(wxRichTextObject* parent) { m_parent = parent; }
644
645 /// Set the margin around the object
646 virtual void SetMargins(int margin);
647 virtual void SetMargins(int leftMargin, int rightMargin, int topMargin, int bottomMargin);
648 virtual int GetLeftMargin() const { return m_leftMargin; }
649 virtual int GetRightMargin() const { return m_rightMargin; }
650 virtual int GetTopMargin() const { return m_topMargin; }
651 virtual int GetBottomMargin() const { return m_bottomMargin; }
652
653 /// Set attributes object
654 void SetAttributes(const wxTextAttrEx& attr) { m_attributes = attr; }
655 const wxTextAttrEx& GetAttributes() const { return m_attributes; }
656 wxTextAttrEx& GetAttributes() { return m_attributes; }
657
658 /// Set/get stored descent
659 void SetDescent(int descent) { m_descent = descent; }
660 int GetDescent() const { return m_descent; }
661
662 // Operations
663
664 /// Clone the object
665 virtual wxRichTextObject* Clone() const { return NULL; }
666
667 /// Copy
668 void Copy(const wxRichTextObject& obj);
669
670 /// Reference-counting allows us to use the same object in multiple
671 /// lists (not yet used)
672 void Reference() { m_refCount ++; }
673 void Dereference();
674
675 /// Convert units in tends of a millimetre to device units
676 int ConvertTenthsMMToPixels(wxDC& dc, int units);
677
678 protected:
679 wxSize m_size;
680 wxPoint m_pos;
681 int m_descent; // Descent for this object (if any)
682 bool m_dirty;
683 int m_refCount;
684 wxRichTextObject* m_parent;
685
686 /// The range of this object (start position to end position)
687 wxRichTextRange m_range;
688
689 /// Margins
690 int m_leftMargin;
691 int m_rightMargin;
692 int m_topMargin;
693 int m_bottomMargin;
694
695 /// Attributes
696 wxTextAttrEx m_attributes;
697 };
698
699 WX_DECLARE_LIST_WITH_DECL( wxRichTextObject, wxRichTextObjectList, class WXDLLIMPEXP_RICHTEXT );
700
701 /*!
702 * wxRichTextCompositeObject class declaration
703 * Objects of this class can contain other objects.
704 */
705
706 class WXDLLIMPEXP_RICHTEXT wxRichTextCompositeObject: public wxRichTextObject
707 {
708 DECLARE_CLASS(wxRichTextCompositeObject)
709 public:
710 // Constructors
711
712 wxRichTextCompositeObject(wxRichTextObject* parent = NULL);
713 virtual ~wxRichTextCompositeObject();
714
715 // Overrideables
716
717 /// Hit-testing: returns a flag indicating hit test details, plus
718 /// information about position
719 virtual int HitTest(wxDC& dc, const wxPoint& pt, long& textPosition);
720
721 /// Finds the absolute position and row height for the given character position
722 virtual bool FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart);
723
724 /// Calculate range
725 virtual void CalculateRange(long start, long& end);
726
727 /// Delete range
728 virtual bool DeleteRange(const wxRichTextRange& range);
729
730 /// Get any text in this object for the given range
731 virtual wxString GetTextForRange(const wxRichTextRange& range) const;
732
733 /// Dump to output stream for debugging
734 virtual void Dump(wxTextOutputStream& stream);
735
736 // Accessors
737
738 /// Get the children
739 wxRichTextObjectList& GetChildren() { return m_children; }
740 const wxRichTextObjectList& GetChildren() const { return m_children; }
741
742 /// Get the child count
743 size_t GetChildCount() const ;
744
745 /// Get the nth child
746 wxRichTextObject* GetChild(size_t n) const ;
747
748 /// Get/set dirty flag
749 virtual bool GetDirty() const { return m_dirty; }
750 virtual void SetDirty(bool dirty) { m_dirty = dirty; }
751
752 /// Is this composite?
753 virtual bool IsComposite() const { return true; }
754
755 /// Returns true if the buffer is empty
756 virtual bool IsEmpty() const { return GetChildCount() == 0; }
757
758 // Operations
759
760 /// Copy
761 void Copy(const wxRichTextCompositeObject& obj);
762
763 /// Assignment
764 void operator= (const wxRichTextCompositeObject& obj) { Copy(obj); }
765
766 /// Append a child, returning the position
767 size_t AppendChild(wxRichTextObject* child) ;
768
769 /// Insert the child in front of the given object, or at the beginning
770 bool InsertChild(wxRichTextObject* child, wxRichTextObject* inFrontOf) ;
771
772 /// Delete the child
773 bool RemoveChild(wxRichTextObject* child, bool deleteChild = false) ;
774
775 /// Delete all children
776 bool DeleteChildren() ;
777
778 /// Recursively merge all pieces that can be merged.
779 bool Defragment();
780
781 protected:
782 wxRichTextObjectList m_children;
783 };
784
785 /*!
786 * wxRichTextBox class declaration
787 * This defines a 2D space to lay out objects
788 */
789
790 class WXDLLIMPEXP_RICHTEXT wxRichTextBox: public wxRichTextCompositeObject
791 {
792 DECLARE_DYNAMIC_CLASS(wxRichTextBox)
793 public:
794 // Constructors
795
796 wxRichTextBox(wxRichTextObject* parent = NULL);
797 wxRichTextBox(const wxRichTextBox& obj): wxRichTextCompositeObject() { Copy(obj); }
798
799 // Overrideables
800
801 /// Draw the item
802 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
803
804 /// Lay the item out
805 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
806
807 /// Get/set the object size for the given range. Returns false if the range
808 /// is invalid for this object.
809 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const;
810
811 // Accessors
812
813 // Operations
814
815 /// Clone
816 virtual wxRichTextObject* Clone() const { return new wxRichTextBox(*this); }
817
818 /// Copy
819 void Copy(const wxRichTextBox& obj);
820
821 protected:
822 };
823
824 /*!
825 * wxRichTextParagraphBox class declaration
826 * This box knows how to lay out paragraphs.
827 */
828
829 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphLayoutBox: public wxRichTextBox
830 {
831 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox)
832 public:
833 // Constructors
834
835 wxRichTextParagraphLayoutBox(wxRichTextObject* parent = NULL);
836 wxRichTextParagraphLayoutBox(const wxRichTextParagraphLayoutBox& obj): wxRichTextBox() { Init(); Copy(obj); }
837
838 // Overrideables
839
840 /// Draw the item
841 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
842
843 /// Lay the item out
844 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
845
846 /// Get/set the object size for the given range. Returns false if the range
847 /// is invalid for this object.
848 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const;
849
850 /// Delete range
851 virtual bool DeleteRange(const wxRichTextRange& range);
852
853 /// Get any text in this object for the given range
854 virtual wxString GetTextForRange(const wxRichTextRange& range) const;
855
856 // Accessors
857
858 /// Associate a control with the buffer, for operations that for example require refreshing the window.
859 void SetRichTextCtrl(wxRichTextCtrl* ctrl) { m_ctrl = ctrl; }
860
861 /// Get the associated control.
862 wxRichTextCtrl* GetRichTextCtrl() const { return m_ctrl; }
863
864 /// Get/set whether the last paragraph is partial or complete
865 void SetPartialParagraph(bool partialPara) { m_partialParagraph = partialPara; }
866 bool GetPartialParagraph() const { return m_partialParagraph; }
867
868 // Operations
869
870 /// Initialize the object.
871 void Init();
872
873 /// Clear all children
874 virtual void Clear();
875
876 /// Clear and initialize with one blank paragraph
877 virtual void Reset();
878
879 /// Convenience function to add a paragraph of text
880 virtual wxRichTextRange AddParagraph(const wxString& text, wxTextAttrEx* paraStyle = NULL);
881
882 /// Convenience function to add an image
883 virtual wxRichTextRange AddImage(const wxImage& image, wxTextAttrEx* paraStyle = NULL);
884
885 /// Adds multiple paragraphs, based on newlines.
886 virtual wxRichTextRange AddParagraphs(const wxString& text, wxTextAttrEx* paraStyle = NULL);
887
888 /// Get the line at the given position. If caretPosition is true, the position is
889 /// a caret position, which is normally a smaller number.
890 virtual wxRichTextLine* GetLineAtPosition(long pos, bool caretPosition = false) const;
891
892 /// Get the line at the given y pixel position, or the last line.
893 virtual wxRichTextLine* GetLineAtYPosition(int y) const;
894
895 /// Get the paragraph at the given character or caret position
896 virtual wxRichTextParagraph* GetParagraphAtPosition(long pos, bool caretPosition = false) const;
897
898 /// Get the line size at the given position
899 virtual wxSize GetLineSizeAtPosition(long pos, bool caretPosition = false) const;
900
901 /// Given a position, get the number of the visible line (potentially many to a paragraph),
902 /// starting from zero at the start of the buffer. We also have to pass a bool (startOfLine)
903 /// that indicates whether the caret is being shown at the end of the previous line or at the start
904 /// of the next, since the caret can be shown at 2 visible positions for the same underlying
905 /// position.
906 virtual long GetVisibleLineNumber(long pos, bool caretPosition = false, bool startOfLine = false) const;
907
908 /// Given a line number, get the corresponding wxRichTextLine object.
909 virtual wxRichTextLine* GetLineForVisibleLineNumber(long lineNumber) const;
910
911 /// Get the leaf object in a paragraph at this position.
912 /// Given a line number, get the corresponding wxRichTextLine object.
913 virtual wxRichTextObject* GetLeafObjectAtPosition(long position) const;
914
915 /// Get the paragraph by number
916 virtual wxRichTextParagraph* GetParagraphAtLine(long paragraphNumber) const;
917
918 /// Get the paragraph for a given line
919 virtual wxRichTextParagraph* GetParagraphForLine(wxRichTextLine* line) const;
920
921 /// Get the length of the paragraph
922 virtual int GetParagraphLength(long paragraphNumber) const;
923
924 /// Get the number of paragraphs
925 virtual int GetParagraphCount() const { return GetChildCount(); }
926
927 /// Get the number of visible lines
928 virtual int GetLineCount() const;
929
930 /// Get the text of the paragraph
931 virtual wxString GetParagraphText(long paragraphNumber) const;
932
933 /// Convert zero-based line column and paragraph number to a position.
934 virtual long XYToPosition(long x, long y) const;
935
936 /// Convert zero-based position to line column and paragraph number
937 virtual bool PositionToXY(long pos, long* x, long* y) const;
938
939 /// Set text attributes: character and/or paragraph styles.
940 virtual bool SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style, bool withUndo = true);
941 virtual bool SetStyle(const wxRichTextRange& range, const wxTextAttrEx& style, bool withUndo = true);
942
943 /// Get the conbined text attributes for this position.
944 virtual bool GetStyle(long position, wxTextAttrEx& style);
945 virtual bool GetStyle(long position, wxRichTextAttr& style);
946
947 /// Get the content (uncombined) attributes for this position.
948 virtual bool GetUncombinedStyle(long position, wxTextAttrEx& style);
949 virtual bool GetUncombinedStyle(long position, wxRichTextAttr& style);
950
951 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
952 /// context attributes.
953 virtual bool DoGetStyle(long position, wxTextAttrEx& style, bool combineStyles = true);
954
955 /// Test if this whole range has character attributes of the specified kind. If any
956 /// of the attributes are different within the range, the test fails. You
957 /// can use this to implement, for example, bold button updating. style must have
958 /// flags indicating which attributes are of interest.
959 virtual bool HasCharacterAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const;
960 virtual bool HasCharacterAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const;
961
962 /// Test if this whole range has paragraph attributes of the specified kind. If any
963 /// of the attributes are different within the range, the test fails. You
964 /// can use this to implement, for example, centering button updating. style must have
965 /// flags indicating which attributes are of interest.
966 virtual bool HasParagraphAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const;
967 virtual bool HasParagraphAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const;
968
969 /// Clone
970 virtual wxRichTextObject* Clone() const { return new wxRichTextParagraphLayoutBox(*this); }
971
972 /// Insert fragment into this box at the given position. If partialParagraph is true,
973 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
974 /// marker.
975 virtual bool InsertFragment(long position, wxRichTextParagraphLayoutBox& fragment);
976
977 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
978 virtual bool CopyFragment(const wxRichTextRange& range, wxRichTextParagraphLayoutBox& fragment);
979
980 /// Apply the style sheet to the buffer, for example if the styles have changed.
981 virtual bool ApplyStyleSheet(wxRichTextStyleSheet* styleSheet);
982
983 /// Copy
984 void Copy(const wxRichTextParagraphLayoutBox& obj);
985
986 /// Assignment
987 void operator= (const wxRichTextParagraphLayoutBox& obj) { Copy(obj); }
988
989 /// Calculate ranges
990 virtual void UpdateRanges() { long end; CalculateRange(0, end); }
991
992 /// Get all the text
993 virtual wxString GetText() const;
994
995 /// Set default style for new content. Setting it to a default attribute
996 /// makes new content take on the 'basic' style.
997 virtual bool SetDefaultStyle(const wxTextAttrEx& style);
998
999 /// Get default style
1000 virtual const wxTextAttrEx& GetDefaultStyle() const { return m_defaultAttributes; }
1001
1002 /// Set basic (overall) style
1003 virtual void SetBasicStyle(const wxTextAttrEx& style) { m_attributes = style; }
1004 virtual void SetBasicStyle(const wxRichTextAttr& style) { style.CopyTo(m_attributes); }
1005
1006 /// Get basic (overall) style
1007 virtual const wxTextAttrEx& GetBasicStyle() const { return m_attributes; }
1008
1009 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1010 void Invalidate(const wxRichTextRange& invalidRange = wxRICHTEXT_ALL);
1011
1012 /// Get invalid range, rounding to entire paragraphs if argument is true.
1013 wxRichTextRange GetInvalidRange(bool wholeParagraphs = false) const;
1014
1015 protected:
1016 wxRichTextCtrl* m_ctrl;
1017 wxTextAttrEx m_defaultAttributes;
1018
1019 /// The invalidated range that will need full layout
1020 wxRichTextRange m_invalidRange;
1021
1022 // Is the last paragraph partial or complete?
1023 bool m_partialParagraph;
1024 };
1025
1026 /*!
1027 * wxRichTextLine class declaration
1028 * This object represents a line in a paragraph, and stores
1029 * offsets from the start of the paragraph representing the
1030 * start and end positions of the line.
1031 */
1032
1033 class WXDLLIMPEXP_RICHTEXT wxRichTextLine
1034 {
1035 public:
1036 // Constructors
1037
1038 wxRichTextLine(wxRichTextParagraph* parent);
1039 wxRichTextLine(const wxRichTextLine& obj) { Init( NULL); Copy(obj); }
1040 virtual ~wxRichTextLine() {}
1041
1042 // Overrideables
1043
1044 // Accessors
1045
1046 /// Set the range
1047 void SetRange(const wxRichTextRange& range) { m_range = range; }
1048 void SetRange(long from, long to) { m_range = wxRichTextRange(from, to); }
1049
1050 /// Get the parent paragraph
1051 wxRichTextParagraph* GetParent() { return m_parent; }
1052
1053 /// Get the range
1054 const wxRichTextRange& GetRange() const { return m_range; }
1055 wxRichTextRange& GetRange() { return m_range; }
1056
1057 /// Get the absolute range
1058 wxRichTextRange GetAbsoluteRange() const;
1059
1060 /// Get/set the line size as calculated by Layout.
1061 virtual wxSize GetSize() const { return m_size; }
1062 virtual void SetSize(const wxSize& sz) { m_size = sz; }
1063
1064 /// Get/set the object position relative to the parent
1065 virtual wxPoint GetPosition() const { return m_pos; }
1066 virtual void SetPosition(const wxPoint& pos) { m_pos = pos; }
1067
1068 /// Get the absolute object position
1069 virtual wxPoint GetAbsolutePosition() const;
1070
1071 /// Get the rectangle enclosing the line
1072 virtual wxRect GetRect() const { return wxRect(GetAbsolutePosition(), GetSize()); }
1073
1074 /// Set/get stored descent
1075 void SetDescent(int descent) { m_descent = descent; }
1076 int GetDescent() const { return m_descent; }
1077
1078 // Operations
1079
1080 /// Initialisation
1081 void Init(wxRichTextParagraph* parent);
1082
1083 /// Copy
1084 void Copy(const wxRichTextLine& obj);
1085
1086 /// Clone
1087 virtual wxRichTextLine* Clone() const { return new wxRichTextLine(*this); }
1088
1089 protected:
1090
1091 /// The range of the line (start position to end position)
1092 /// This is relative to the parent paragraph.
1093 wxRichTextRange m_range;
1094
1095 /// Size and position measured relative to top of paragraph
1096 wxPoint m_pos;
1097 wxSize m_size;
1098
1099 /// Maximum descent for this line (location of text baseline)
1100 int m_descent;
1101
1102 // The parent object
1103 wxRichTextParagraph* m_parent;
1104 };
1105
1106 WX_DECLARE_LIST_WITH_DECL( wxRichTextLine, wxRichTextLineList , class WXDLLIMPEXP_RICHTEXT );
1107
1108 /*!
1109 * wxRichTextParagraph class declaration
1110 * This object represents a single paragraph (or in a straight text editor, a line).
1111 */
1112
1113 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraph: public wxRichTextBox
1114 {
1115 DECLARE_DYNAMIC_CLASS(wxRichTextParagraph)
1116 public:
1117 // Constructors
1118
1119 wxRichTextParagraph(wxRichTextObject* parent = NULL, wxTextAttrEx* style = NULL);
1120 wxRichTextParagraph(const wxString& text, wxRichTextObject* parent = NULL, wxTextAttrEx* style = NULL);
1121 virtual ~wxRichTextParagraph();
1122 wxRichTextParagraph(const wxRichTextParagraph& obj): wxRichTextBox() { Copy(obj); }
1123
1124 // Overrideables
1125
1126 /// Draw the item
1127 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
1128
1129 /// Lay the item out
1130 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
1131
1132 /// Get/set the object size for the given range. Returns false if the range
1133 /// is invalid for this object.
1134 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const;
1135
1136 /// Finds the absolute position and row height for the given character position
1137 virtual bool FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart);
1138
1139 /// Hit-testing: returns a flag indicating hit test details, plus
1140 /// information about position
1141 virtual int HitTest(wxDC& dc, const wxPoint& pt, long& textPosition);
1142
1143 /// Calculate range
1144 virtual void CalculateRange(long start, long& end);
1145
1146 // Accessors
1147
1148 /// Get the cached lines
1149 wxRichTextLineList& GetLines() { return m_cachedLines; }
1150
1151 // Operations
1152
1153 /// Copy
1154 void Copy(const wxRichTextParagraph& obj);
1155
1156 /// Clone
1157 virtual wxRichTextObject* Clone() const { return new wxRichTextParagraph(*this); }
1158
1159 /// Clear the cached lines
1160 void ClearLines();
1161
1162 // Implementation
1163
1164 /// Apply paragraph styles such as centering to the wrapped lines
1165 virtual void ApplyParagraphStyle(const wxTextAttrEx& attr, const wxRect& rect);
1166
1167 /// Insert text at the given position
1168 virtual bool InsertText(long pos, const wxString& text);
1169
1170 /// Split an object at this position if necessary, and return
1171 /// the previous object, or NULL if inserting at beginning.
1172 virtual wxRichTextObject* SplitAt(long pos, wxRichTextObject** previousObject = NULL);
1173
1174 /// Move content to a list from this point
1175 virtual void MoveToList(wxRichTextObject* obj, wxList& list);
1176
1177 /// Add content back from list
1178 virtual void MoveFromList(wxList& list);
1179
1180 /// Get the plain text searching from the start or end of the range.
1181 /// The resulting string may be shorter than the range given.
1182 bool GetContiguousPlainText(wxString& text, const wxRichTextRange& range, bool fromStart = true);
1183
1184 /// Find a suitable wrap position. wrapPosition is the last position in the line to the left
1185 /// of the split.
1186 bool FindWrapPosition(const wxRichTextRange& range, wxDC& dc, int availableSpace, long& wrapPosition);
1187
1188 /// Find the object at the given position
1189 wxRichTextObject* FindObjectAtPosition(long position);
1190
1191 /// Get the bullet text for this paragraph.
1192 wxString GetBulletText();
1193
1194 /// Allocate or reuse a line object
1195 wxRichTextLine* AllocateLine(int pos);
1196
1197 /// Clear remaining unused line objects, if any
1198 bool ClearUnusedLines(int lineCount);
1199
1200 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
1201 /// retrieve the actual style.
1202 wxTextAttrEx GetCombinedAttributes(const wxTextAttr& contentStyle) const;
1203
1204 /// Get combined attributes of the base style and paragraph style.
1205 wxTextAttrEx GetCombinedAttributes() const;
1206
1207 protected:
1208 /// The lines that make up the wrapped paragraph
1209 wxRichTextLineList m_cachedLines;
1210 };
1211
1212 /*!
1213 * wxRichTextPlainText class declaration
1214 * This object represents a single piece of text.
1215 */
1216
1217 class WXDLLIMPEXP_RICHTEXT wxRichTextPlainText: public wxRichTextObject
1218 {
1219 DECLARE_DYNAMIC_CLASS(wxRichTextPlainText)
1220 public:
1221 // Constructors
1222
1223 wxRichTextPlainText(const wxString& text = wxEmptyString, wxRichTextObject* parent = NULL, wxTextAttrEx* style = NULL);
1224 wxRichTextPlainText(const wxRichTextPlainText& obj): wxRichTextObject() { Copy(obj); }
1225
1226 // Overrideables
1227
1228 /// Draw the item
1229 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
1230
1231 /// Lay the item out
1232 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
1233
1234 /// Get/set the object size for the given range. Returns false if the range
1235 /// is invalid for this object.
1236 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position/* = wxPoint(0,0)*/) const;
1237
1238 /// Get any text in this object for the given range
1239 virtual wxString GetTextForRange(const wxRichTextRange& range) const;
1240
1241 /// Do a split, returning an object containing the second part, and setting
1242 /// the first part in 'this'.
1243 virtual wxRichTextObject* DoSplit(long pos);
1244
1245 /// Calculate range
1246 virtual void CalculateRange(long start, long& end);
1247
1248 /// Delete range
1249 virtual bool DeleteRange(const wxRichTextRange& range);
1250
1251 /// Returns true if the object is empty
1252 virtual bool IsEmpty() const { return m_text.empty(); }
1253
1254 /// Returns true if this object can merge itself with the given one.
1255 virtual bool CanMerge(wxRichTextObject* object) const;
1256
1257 /// Returns true if this object merged itself with the given one.
1258 /// The calling code will then delete the given object.
1259 virtual bool Merge(wxRichTextObject* object);
1260
1261 /// Dump to output stream for debugging
1262 virtual void Dump(wxTextOutputStream& stream);
1263
1264 // Accessors
1265
1266 /// Get the text
1267 const wxString& GetText() const { return m_text; }
1268
1269 /// Set the text
1270 void SetText(const wxString& text) { m_text = text; }
1271
1272 // Operations
1273
1274 /// Copy
1275 void Copy(const wxRichTextPlainText& obj);
1276
1277 /// Clone
1278 virtual wxRichTextObject* Clone() const { return new wxRichTextPlainText(*this); }
1279 private:
1280 bool DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect, wxString& str, wxCoord& x, wxCoord& y, bool selected);
1281
1282 protected:
1283 wxString m_text;
1284 };
1285
1286 /*!
1287 * wxRichTextImageBlock stores information about an image, in binary in-memory form
1288 */
1289
1290 class WXDLLIMPEXP_BASE wxDataInputStream;
1291 class WXDLLIMPEXP_BASE wxDataOutputStream;
1292
1293 class WXDLLIMPEXP_RICHTEXT wxRichTextImageBlock: public wxObject
1294 {
1295 public:
1296 wxRichTextImageBlock();
1297 wxRichTextImageBlock(const wxRichTextImageBlock& block);
1298 virtual ~wxRichTextImageBlock();
1299
1300 void Init();
1301 void Clear();
1302
1303 // Load the original image into a memory block.
1304 // If the image is not a JPEG, we must convert it into a JPEG
1305 // to conserve space.
1306 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
1307 // load the image a 2nd time.
1308 virtual bool MakeImageBlock(const wxString& filename, int imageType, wxImage& image, bool convertToJPEG = true);
1309
1310 // Make an image block from the wxImage in the given
1311 // format.
1312 virtual bool MakeImageBlock(wxImage& image, int imageType, int quality = 80);
1313
1314 // Write to a file
1315 bool Write(const wxString& filename);
1316
1317 // Write data in hex to a stream
1318 bool WriteHex(wxOutputStream& stream);
1319
1320 // Read data in hex from a stream
1321 bool ReadHex(wxInputStream& stream, int length, int imageType);
1322
1323 // Copy from 'block'
1324 void Copy(const wxRichTextImageBlock& block);
1325
1326 // Load a wxImage from the block
1327 bool Load(wxImage& image);
1328
1329 //// Operators
1330 void operator=(const wxRichTextImageBlock& block);
1331
1332 //// Accessors
1333
1334 unsigned char* GetData() const { return m_data; }
1335 size_t GetDataSize() const { return m_dataSize; }
1336 int GetImageType() const { return m_imageType; }
1337
1338 void SetData(unsigned char* image) { m_data = image; }
1339 void SetDataSize(size_t size) { m_dataSize = size; }
1340 void SetImageType(int imageType) { m_imageType = imageType; }
1341
1342 bool Ok() const { return GetData() != NULL; }
1343
1344 /// Implementation
1345
1346 /// Allocate and read from stream as a block of memory
1347 static unsigned char* ReadBlock(wxInputStream& stream, size_t size);
1348 static unsigned char* ReadBlock(const wxString& filename, size_t size);
1349
1350 // Write memory block to stream
1351 static bool WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size);
1352
1353 // Write memory block to file
1354 static bool WriteBlock(const wxString& filename, unsigned char* block, size_t size);
1355
1356 protected:
1357 // Size in bytes of the image stored.
1358 // This is in the raw, original form such as a JPEG file.
1359 unsigned char* m_data;
1360 size_t m_dataSize;
1361 int m_imageType; // wxWin type id
1362 };
1363
1364
1365 /*!
1366 * wxRichTextImage class declaration
1367 * This object represents an image.
1368 */
1369
1370 class WXDLLIMPEXP_RICHTEXT wxRichTextImage: public wxRichTextObject
1371 {
1372 DECLARE_DYNAMIC_CLASS(wxRichTextImage)
1373 public:
1374 // Constructors
1375
1376 wxRichTextImage(wxRichTextObject* parent = NULL): wxRichTextObject(parent) { }
1377 wxRichTextImage(const wxImage& image, wxRichTextObject* parent = NULL);
1378 wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent = NULL);
1379 wxRichTextImage(const wxRichTextImage& obj): wxRichTextObject() { Copy(obj); }
1380
1381 // Overrideables
1382
1383 /// Draw the item
1384 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
1385
1386 /// Lay the item out
1387 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
1388
1389 /// Get the object size for the given range. Returns false if the range
1390 /// is invalid for this object.
1391 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const;
1392
1393 /// Returns true if the object is empty
1394 virtual bool IsEmpty() const { return !m_image.Ok(); }
1395
1396 // Accessors
1397
1398 /// Get the image
1399 const wxImage& GetImage() const { return m_image; }
1400
1401 /// Set the image
1402 void SetImage(const wxImage& image) { m_image = image; }
1403
1404 /// Get the image block containing the raw data
1405 wxRichTextImageBlock& GetImageBlock() { return m_imageBlock; }
1406
1407 // Operations
1408
1409 /// Copy
1410 void Copy(const wxRichTextImage& obj);
1411
1412 /// Clone
1413 virtual wxRichTextObject* Clone() const { return new wxRichTextImage(*this); }
1414
1415 /// Load wxImage from the block
1416 virtual bool LoadFromBlock();
1417
1418 /// Make block from the wxImage
1419 virtual bool MakeBlock();
1420
1421 protected:
1422 // TODO: reduce the multiple representations of data
1423 wxImage m_image;
1424 wxBitmap m_bitmap;
1425 wxRichTextImageBlock m_imageBlock;
1426 };
1427
1428
1429 /*!
1430 * wxRichTextBuffer class declaration
1431 * This is a kind of box, used to represent the whole buffer
1432 */
1433
1434 class WXDLLIMPEXP_RICHTEXT wxRichTextCommand;
1435 class WXDLLIMPEXP_RICHTEXT wxRichTextAction;
1436
1437 class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer: public wxRichTextParagraphLayoutBox
1438 {
1439 DECLARE_DYNAMIC_CLASS(wxRichTextBuffer)
1440 public:
1441 // Constructors
1442
1443 wxRichTextBuffer() { Init(); }
1444 wxRichTextBuffer(const wxRichTextBuffer& obj): wxRichTextParagraphLayoutBox() { Init(); Copy(obj); }
1445 virtual ~wxRichTextBuffer() ;
1446
1447 // Accessors
1448
1449 /// Gets the command processor
1450 wxCommandProcessor* GetCommandProcessor() const { return m_commandProcessor; }
1451
1452 /// Set style sheet, if any.
1453 void SetStyleSheet(wxRichTextStyleSheet* styleSheet) { m_styleSheet = styleSheet; }
1454 wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
1455
1456 // Operations
1457
1458 /// Initialisation
1459 void Init();
1460
1461 /// Clears the buffer and resets the command processor
1462 virtual void Clear();
1463
1464 /// The same as Clear, and adds an empty paragraph.
1465 virtual void Reset();
1466
1467 /// Load a file
1468 virtual bool LoadFile(const wxString& filename, int type = wxRICHTEXT_TYPE_ANY);
1469
1470 /// Save a file
1471 virtual bool SaveFile(const wxString& filename, int type = wxRICHTEXT_TYPE_ANY);
1472
1473 /// Load from a stream
1474 virtual bool LoadFile(wxInputStream& stream, int type = wxRICHTEXT_TYPE_ANY);
1475
1476 /// Save to a stream
1477 virtual bool SaveFile(wxOutputStream& stream, int type = wxRICHTEXT_TYPE_ANY);
1478
1479 /// Convenience function to add a paragraph of text
1480 virtual wxRichTextRange AddParagraph(const wxString& text, wxTextAttrEx* paraStyle = NULL) { Modify(); return wxRichTextParagraphLayoutBox::AddParagraph(text, paraStyle); }
1481
1482 /// Begin collapsing undo/redo commands. Note that this may not work properly
1483 /// if combining commands that delete or insert content, changing ranges for
1484 /// subsequent actions.
1485 virtual bool BeginBatchUndo(const wxString& cmdName);
1486
1487 /// End collapsing undo/redo commands
1488 virtual bool EndBatchUndo();
1489
1490 /// Collapsing commands?
1491 virtual bool BatchingUndo() const { return m_batchedCommandDepth > 0; }
1492
1493 /// Submit immediately, or delay according to whether collapsing is on
1494 virtual bool SubmitAction(wxRichTextAction* action);
1495
1496 /// Get collapsed command
1497 virtual wxRichTextCommand* GetBatchedCommand() const { return m_batchedCommand; }
1498
1499 /// Begin suppressing undo/redo commands. The way undo is suppressed may be implemented
1500 /// differently by each command. If not dealt with by a command implementation, then
1501 /// it will be implemented automatically by not storing the command in the undo history
1502 /// when the action is submitted to the command processor.
1503 virtual bool BeginSuppressUndo();
1504
1505 /// End suppressing undo/redo commands.
1506 virtual bool EndSuppressUndo();
1507
1508 /// Collapsing commands?
1509 virtual bool SuppressingUndo() const { return m_suppressUndo > 0; }
1510
1511 /// Copy the range to the clipboard
1512 virtual bool CopyToClipboard(const wxRichTextRange& range);
1513
1514 /// Paste the clipboard content to the buffer
1515 virtual bool PasteFromClipboard(long position);
1516
1517 /// Can we paste from the clipboard?
1518 virtual bool CanPasteFromClipboard() const;
1519
1520 /// Begin using a style
1521 virtual bool BeginStyle(const wxTextAttrEx& style);
1522
1523 /// End the style
1524 virtual bool EndStyle();
1525
1526 /// End all styles
1527 virtual bool EndAllStyles();
1528
1529 /// Clear the style stack
1530 virtual void ClearStyleStack();
1531
1532 /// Get the size of the style stack, for example to check correct nesting
1533 virtual size_t GetStyleStackSize() const { return m_attributeStack.GetCount(); }
1534
1535 /// Begin using bold
1536 bool BeginBold();
1537
1538 /// End using bold
1539 bool EndBold() { return EndStyle(); }
1540
1541 /// Begin using italic
1542 bool BeginItalic();
1543
1544 /// End using italic
1545 bool EndItalic() { return EndStyle(); }
1546
1547 /// Begin using underline
1548 bool BeginUnderline();
1549
1550 /// End using underline
1551 bool EndUnderline() { return EndStyle(); }
1552
1553 /// Begin using point size
1554 bool BeginFontSize(int pointSize);
1555
1556 /// End using point size
1557 bool EndFontSize() { return EndStyle(); }
1558
1559 /// Begin using this font
1560 bool BeginFont(const wxFont& font);
1561
1562 /// End using a font
1563 bool EndFont() { return EndStyle(); }
1564
1565 /// Begin using this colour
1566 bool BeginTextColour(const wxColour& colour);
1567
1568 /// End using a colour
1569 bool EndTextColour() { return EndStyle(); }
1570
1571 /// Begin using alignment
1572 bool BeginAlignment(wxTextAttrAlignment alignment);
1573
1574 /// End alignment
1575 bool EndAlignment() { return EndStyle(); }
1576
1577 /// Begin left indent
1578 bool BeginLeftIndent(int leftIndent, int leftSubIndent = 0);
1579
1580 /// End left indent
1581 bool EndLeftIndent() { return EndStyle(); }
1582
1583 /// Begin right indent
1584 bool BeginRightIndent(int rightIndent);
1585
1586 /// End right indent
1587 bool EndRightIndent() { return EndStyle(); }
1588
1589 /// Begin paragraph spacing
1590 bool BeginParagraphSpacing(int before, int after);
1591
1592 /// End paragraph spacing
1593 bool EndParagraphSpacing() { return EndStyle(); }
1594
1595 /// Begin line spacing
1596 bool BeginLineSpacing(int lineSpacing);
1597
1598 /// End line spacing
1599 bool EndLineSpacing() { return EndStyle(); }
1600
1601 /// Begin numbered bullet
1602 bool BeginNumberedBullet(int bulletNumber, int leftIndent, int leftSubIndent, int bulletStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD);
1603
1604 /// End numbered bullet
1605 bool EndNumberedBullet() { return EndStyle(); }
1606
1607 /// Begin symbol bullet
1608 bool BeginSymbolBullet(wxChar symbol, int leftIndent, int leftSubIndent, int bulletStyle = wxTEXT_ATTR_BULLET_STYLE_SYMBOL);
1609
1610 /// End symbol bullet
1611 bool EndSymbolBullet() { return EndStyle(); }
1612
1613 /// Begin named character style
1614 bool BeginCharacterStyle(const wxString& characterStyle);
1615
1616 /// End named character style
1617 bool EndCharacterStyle() { return EndStyle(); }
1618
1619 /// Begin named paragraph style
1620 bool BeginParagraphStyle(const wxString& paragraphStyle);
1621
1622 /// End named character style
1623 bool EndParagraphStyle() { return EndStyle(); }
1624
1625 // Implementation
1626
1627 /// Copy
1628 void Copy(const wxRichTextBuffer& obj);
1629
1630 /// Clone
1631 virtual wxRichTextObject* Clone() const { return new wxRichTextBuffer(*this); }
1632
1633 /// Submit command to insert paragraphs
1634 bool InsertParagraphsWithUndo(long pos, const wxRichTextParagraphLayoutBox& paragraphs, wxRichTextCtrl* ctrl, int flags = 0);
1635
1636 /// Submit command to insert the given text
1637 bool InsertTextWithUndo(long pos, const wxString& text, wxRichTextCtrl* ctrl, int flags = 0);
1638
1639 /// Submit command to insert a newline
1640 bool InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int flags = 0);
1641
1642 /// Submit command to insert the given image
1643 bool InsertImageWithUndo(long pos, const wxRichTextImageBlock& imageBlock, wxRichTextCtrl* ctrl, int flags = 0);
1644
1645 /// Submit command to delete this range
1646 bool DeleteRangeWithUndo(const wxRichTextRange& range, long initialCaretPosition, long newCaretPositon, wxRichTextCtrl* ctrl);
1647
1648 /// Mark modified
1649 void Modify(bool modify = true) { m_modified = modify; }
1650 bool IsModified() const { return m_modified; }
1651
1652 /// Get the style that is appropriate for a new paragraph at this position.
1653 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
1654 /// style.
1655 wxRichTextAttr GetStyleForNewParagraph(long pos, bool caretPosition = false) const;
1656
1657 /// Dumps contents of buffer for debugging purposes
1658 virtual void Dump();
1659 virtual void Dump(wxTextOutputStream& stream) { wxRichTextParagraphLayoutBox::Dump(stream); }
1660
1661 /// Returns the file handlers
1662 static wxList& GetHandlers() { return sm_handlers; }
1663
1664 /// Adds a handler to the end
1665 static void AddHandler(wxRichTextFileHandler *handler);
1666
1667 /// Inserts a handler at the front
1668 static void InsertHandler(wxRichTextFileHandler *handler);
1669
1670 /// Removes a handler
1671 static bool RemoveHandler(const wxString& name);
1672
1673 /// Finds a handler by name
1674 static wxRichTextFileHandler *FindHandler(const wxString& name);
1675
1676 /// Finds a handler by extension and type
1677 static wxRichTextFileHandler *FindHandler(const wxString& extension, int imageType);
1678
1679 /// Finds a handler by filename or, if supplied, type
1680 static wxRichTextFileHandler *FindHandlerFilenameOrType(const wxString& filename, int imageType);
1681
1682 /// Finds a handler by type
1683 static wxRichTextFileHandler *FindHandler(int imageType);
1684
1685 /// Gets a wildcard incorporating all visible handlers. If 'types' is present,
1686 /// will be filled with the file type corresponding to each filter. This can be
1687 /// used to determine the type to pass to LoadFile given a selected filter.
1688 static wxString GetExtWildcard(bool combine = false, bool save = false, wxArrayInt* types = NULL);
1689
1690 /// Clean up handlers
1691 static void CleanUpHandlers();
1692
1693 /// Initialise the standard handlers
1694 static void InitStandardHandlers();
1695
1696 protected:
1697
1698 /// Command processor
1699 wxCommandProcessor* m_commandProcessor;
1700
1701 /// Has been modified?
1702 bool m_modified;
1703
1704 /// Collapsed command stack
1705 int m_batchedCommandDepth;
1706
1707 /// Name for collapsed command
1708 wxString m_batchedCommandsName;
1709
1710 /// Current collapsed command accumulating actions
1711 wxRichTextCommand* m_batchedCommand;
1712
1713 /// Whether to suppress undo
1714 int m_suppressUndo;
1715
1716 /// Style sheet, if any
1717 wxRichTextStyleSheet* m_styleSheet;
1718
1719 /// Stack of attributes for convenience functions
1720 wxList m_attributeStack;
1721
1722 /// File handlers
1723 static wxList sm_handlers;
1724 };
1725
1726 /*!
1727 * The command identifiers
1728 *
1729 */
1730
1731 enum wxRichTextCommandId
1732 {
1733 wxRICHTEXT_INSERT,
1734 wxRICHTEXT_DELETE,
1735 wxRICHTEXT_CHANGE_STYLE
1736 };
1737
1738 /*!
1739 * Command classes for undo/redo
1740 *
1741 */
1742
1743 class WXDLLIMPEXP_RICHTEXT wxRichTextAction;
1744 class WXDLLIMPEXP_RICHTEXT wxRichTextCommand: public wxCommand
1745 {
1746 public:
1747 // Ctor for one action
1748 wxRichTextCommand(const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
1749 wxRichTextCtrl* ctrl, bool ignoreFirstTime = false);
1750
1751 // Ctor for multiple actions
1752 wxRichTextCommand(const wxString& name);
1753
1754 virtual ~wxRichTextCommand();
1755
1756 bool Do();
1757 bool Undo();
1758
1759 void AddAction(wxRichTextAction* action);
1760 void ClearActions();
1761
1762 wxList& GetActions() { return m_actions; }
1763
1764 protected:
1765
1766 wxList m_actions;
1767 };
1768
1769 /*!
1770 * wxRichTextAction class declaration
1771 * There can be more than one action in a command.
1772 */
1773
1774 class WXDLLIMPEXP_RICHTEXT wxRichTextAction: public wxObject
1775 {
1776 public:
1777 wxRichTextAction(wxRichTextCommand* cmd, const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
1778 wxRichTextCtrl* ctrl, bool ignoreFirstTime = false);
1779
1780 virtual ~wxRichTextAction();
1781
1782 bool Do();
1783 bool Undo();
1784
1785 /// Update the control appearance
1786 void UpdateAppearance(long caretPosition, bool sendUpdateEvent = false);
1787
1788 /// Replace the buffer paragraphs with the given fragment.
1789 void ApplyParagraphs(const wxRichTextParagraphLayoutBox& fragment);
1790
1791 /// Get the fragments
1792 wxRichTextParagraphLayoutBox& GetNewParagraphs() { return m_newParagraphs; }
1793 wxRichTextParagraphLayoutBox& GetOldParagraphs() { return m_oldParagraphs; }
1794
1795 /// Set/get the position used for e.g. insertion
1796 void SetPosition(long pos) { m_position = pos; }
1797 long GetPosition() const { return m_position; }
1798
1799 /// Set/get the range for e.g. deletion
1800 void SetRange(const wxRichTextRange& range) { m_range = range; }
1801 const wxRichTextRange& GetRange() const { return m_range; }
1802
1803 /// Get name
1804 const wxString& GetName() const { return m_name; }
1805
1806 protected:
1807 // Action name
1808 wxString m_name;
1809
1810 // Buffer
1811 wxRichTextBuffer* m_buffer;
1812
1813 // Control
1814 wxRichTextCtrl* m_ctrl;
1815
1816 // Stores the new paragraphs
1817 wxRichTextParagraphLayoutBox m_newParagraphs;
1818
1819 // Stores the old paragraphs
1820 wxRichTextParagraphLayoutBox m_oldParagraphs;
1821
1822 // The affected range
1823 wxRichTextRange m_range;
1824
1825 // The insertion point for this command
1826 long m_position;
1827
1828 // Ignore 1st 'Do' operation because we already did it
1829 bool m_ignoreThis;
1830
1831 // The command identifier
1832 wxRichTextCommandId m_cmdId;
1833 };
1834
1835 /*!
1836 * wxRichTextFileHandler
1837 * Base class for file handlers
1838 */
1839
1840 class WXDLLIMPEXP_RICHTEXT wxRichTextFileHandler: public wxObject
1841 {
1842 DECLARE_CLASS(wxRichTextFileHandler)
1843 public:
1844 wxRichTextFileHandler(const wxString& name = wxEmptyString, const wxString& ext = wxEmptyString, int type = 0)
1845 : m_name(name), m_extension(ext), m_type(type), m_visible(true)
1846 { }
1847
1848 #if wxUSE_STREAMS
1849 bool LoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
1850 { return DoLoadFile(buffer, stream); }
1851 bool SaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
1852 { return DoSaveFile(buffer, stream); }
1853 #endif
1854
1855 bool LoadFile(wxRichTextBuffer *buffer, const wxString& filename);
1856 bool SaveFile(wxRichTextBuffer *buffer, const wxString& filename);
1857
1858 /// Can we handle this filename (if using files)? By default, checks the extension.
1859 virtual bool CanHandle(const wxString& filename) const;
1860
1861 /// Can we save using this handler?
1862 virtual bool CanSave() const { return false; }
1863
1864 /// Can we load using this handler?
1865 virtual bool CanLoad() const { return false; }
1866
1867 /// Should this handler be visible to the user?
1868 virtual bool IsVisible() const { return m_visible; }
1869 virtual void SetVisible(bool visible) { m_visible = visible; }
1870
1871 /// The name of the nandler
1872 void SetName(const wxString& name) { m_name = name; }
1873 wxString GetName() const { return m_name; }
1874
1875 /// The default extension to recognise
1876 void SetExtension(const wxString& ext) { m_extension = ext; }
1877 wxString GetExtension() const { return m_extension; }
1878
1879 /// The handler type
1880 void SetType(int type) { m_type = type; }
1881 int GetType() const { return m_type; }
1882
1883 /// Encoding to use when saving a file. If empty, a suitable encoding is chosen
1884 void SetEncoding(const wxString& encoding) { m_encoding = encoding; }
1885 const wxString& GetEncoding() const { return m_encoding; }
1886
1887 protected:
1888
1889 #if wxUSE_STREAMS
1890 virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream) = 0;
1891 virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) = 0;
1892 #endif
1893
1894 wxString m_name;
1895 wxString m_encoding;
1896 wxString m_extension;
1897 int m_type;
1898 bool m_visible;
1899 };
1900
1901 /*!
1902 * wxRichTextPlainTextHandler
1903 * Plain text handler
1904 */
1905
1906 class WXDLLIMPEXP_RICHTEXT wxRichTextPlainTextHandler: public wxRichTextFileHandler
1907 {
1908 DECLARE_CLASS(wxRichTextPlainTextHandler)
1909 public:
1910 wxRichTextPlainTextHandler(const wxString& name = wxT("Text"), const wxString& ext = wxT("txt"), int type = wxRICHTEXT_TYPE_TEXT)
1911 : wxRichTextFileHandler(name, ext, type)
1912 { }
1913
1914 /// Can we save using this handler?
1915 virtual bool CanSave() const { return true; }
1916
1917 /// Can we load using this handler?
1918 virtual bool CanLoad() const { return true; }
1919
1920 protected:
1921
1922 #if wxUSE_STREAMS
1923 virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream);
1924 virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream);
1925 #endif
1926
1927 };
1928
1929 #if wxUSE_DATAOBJ
1930
1931 /*!
1932 * The data object for a wxRichTextBuffer
1933 */
1934
1935 class wxRichTextBufferDataObject: public wxDataObjectSimple
1936 {
1937 public:
1938 // ctor doesn't copy the pointer, so it shouldn't go away while this object
1939 // is alive
1940 wxRichTextBufferDataObject(wxRichTextBuffer* richTextBuffer = (wxRichTextBuffer*) NULL);
1941 virtual ~wxRichTextBufferDataObject();
1942
1943 // after a call to this function, the buffer is owned by the caller and it
1944 // is responsible for deleting it
1945 wxRichTextBuffer* GetRichTextBuffer();
1946
1947 // Returns the id for the new data format
1948 static const wxChar* GetRichTextBufferFormatId() { return ms_richTextBufferFormatId; }
1949
1950 // base class pure virtuals
1951
1952 virtual wxDataFormat GetPreferredFormat(Direction dir) const;
1953 virtual size_t GetDataSize() const;
1954 virtual bool GetDataHere(void *pBuf) const;
1955 virtual bool SetData(size_t len, const void *buf);
1956
1957 // prevent warnings
1958
1959 virtual size_t GetDataSize(const wxDataFormat&) const { return GetDataSize(); }
1960 virtual bool GetDataHere(const wxDataFormat&, void *buf) const { return GetDataHere(buf); }
1961 virtual bool SetData(const wxDataFormat&, size_t len, const void *buf) { return SetData(len, buf); }
1962
1963 private:
1964 wxDataFormat m_formatRichTextBuffer; // our custom format
1965 wxRichTextBuffer* m_richTextBuffer; // our data
1966 static const wxChar* ms_richTextBufferFormatId; // our format id
1967 };
1968
1969 #endif
1970
1971 /*!
1972 * Utilities
1973 *
1974 */
1975
1976 inline bool wxRichTextHasStyle(int flags, int style)
1977 {
1978 return ((flags & style) == style);
1979 }
1980
1981 /// Compare two attribute objects
1982 bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2);
1983 bool wxTextAttrEq(const wxTextAttr& attr1, const wxRichTextAttr& attr2);
1984
1985 /// Compare two attribute objects, but take into account the flags
1986 /// specifying attributes of interest.
1987 bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2, int flags);
1988 bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2, int flags);
1989
1990 /// Apply one style to another
1991 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxTextAttrEx& style);
1992 bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxTextAttrEx& style);
1993 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style);
1994
1995 #endif
1996 // wxUSE_RICHTEXT
1997
1998 #endif
1999 // _WX_RICHTEXTBUFFER_H_