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