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