1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/richtext/richtextbuffer.h
3 // Purpose: Buffer for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
17 Data is represented by a hierarchy of objects, all derived from
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.
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.
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.
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.
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.
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.
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
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.
70 When the layout for one paragraph returns, the next paragraph is
71 fed the position of the previous, so it can position itself.
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
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
87 #ifndef _WX_RICHTEXTBUFFER_H_
88 #define _WX_RICHTEXTBUFFER_H_
99 #include "wx/cmdproc.h"
100 #include "wx/txtstrm.h"
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
114 * Forward declarations
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
;
129 * Flags determining the available space, passed to Layout
132 #define wxRICHTEXT_FIXED_WIDTH 0x01
133 #define wxRICHTEXT_FIXED_HEIGHT 0x02
134 #define wxRICHTEXT_VARIABLE_WIDTH 0x04
135 #define wxRICHTEXT_VARIABLE_HEIGHT 0x08
138 * Flags returned from hit-testing
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
151 * Flags for GetRangeSize
154 #define wxRICHTEXT_FORMATTED 0x01
155 #define wxRICHTEXT_UNFORMATTED 0x02
158 * Extra formatting flags not in wxTextAttr
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
171 * Styles for wxTextAttrEx::SetBulletStyle
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
186 * Line spacing values
189 #define wxTEXT_ATTR_LINE_SPACING_NORMAL 10
190 #define wxTEXT_ATTR_LINE_SPACING_HALF 15
191 #define wxTEXT_ATTR_LINE_SPACING_TWICE 20
194 * wxRichTextRange class declaration
195 * This stores beginning and end positions for a range of data.
198 class WXDLLIMPEXP_ADV wxRichTextRange
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() {}
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
); }
213 void SetRange(long start
, long end
) { m_start
= start
; m_end
= end
; }
215 void SetStart(long start
) { m_start
= start
; }
216 long GetStart() const { return m_start
; }
218 void SetEnd(long end
) { m_end
= end
; }
219 long GetEnd() const { return m_end
; }
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
; }
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
; }
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
; }
233 /// Limit this range to be within 'range'
234 bool LimitTo(const wxRichTextRange
& range
) ;
236 /// Gets the length of the range
237 long GetLength() const { return m_end
- m_start
+ 1; }
239 /// Swaps the start and end
240 void Swap() { long tmp
= m_start
; m_start
= m_end
; m_end
= tmp
; }
248 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
251 class WXDLLIMPEXP_ADV wxTextAttrEx
: public wxTextAttr
255 wxTextAttrEx(const wxTextAttrEx
& attr
);
256 wxTextAttrEx() { Init(); }
258 // Initialise this object.
261 // Assignment from a wxTextAttrEx object
262 void operator= (const wxTextAttrEx
& attr
);
264 // Assignment from a wxTextAttr object.
265 void operator= (const wxTextAttr
& attr
);
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
; }
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
; }
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
); }
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
))); }
301 // returns false if we have any attributes set, true otherwise
302 bool IsDefault() const
304 return !HasTextColour() && !HasBackgroundColour() && !HasFont() && !HasAlignment() &&
305 !HasTabs() && !HasLeftIndent() && !HasRightIndent() &&
306 !HasParagraphSpacingAfter() && !HasParagraphSpacingBefore() && !HasLineSpacing() &&
307 !HasCharacterStyleName() && !HasParagraphStyleName() && !HasBulletNumber() && !HasBulletStyle() && !HasBulletSymbol();
311 int m_paragraphSpacingAfter
;
312 int m_paragraphSpacingBefore
;
316 wxChar m_bulletSymbol
;
319 wxString m_characterStyleName
;
322 wxString m_paragraphStyleName
;
326 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
327 * efficient way to query styles.
330 class WXDLLIMPEXP_ADV wxRichTextAttr
334 wxRichTextAttr(const wxTextAttrEx
& attr
);
335 wxRichTextAttr() { Init(); }
336 wxRichTextAttr(const wxColour
& colText
,
337 const wxColour
& colBack
= wxNullColour
,
338 wxTextAttrAlignment alignment
= wxTEXT_ALIGNMENT_DEFAULT
);
340 // Initialise this object.
343 // Assignment from a wxRichTextAttr object.
344 void operator= (const wxRichTextAttr
& attr
);
346 // Assignment from a wxTextAttrEx object.
347 void operator= (const wxTextAttrEx
& attr
);
349 // Making a wxTextAttrEx object.
350 operator wxTextAttrEx () const ;
352 // Copy to a wxTextAttr
353 void CopyTo(wxTextAttrEx
& attr
) const;
355 // Create font from font attributes.
356 wxFont
CreateFont() const;
358 // Get attributes from font.
359 bool GetFontAttributes(const wxFont
& font
);
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
; }
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
; }
375 void SetFlags(long flags
) { m_flags
= flags
; }
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
; }
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
; }
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
; }
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
; }
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; }
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; }
433 bool HasFlag(long flag
) const { return (m_flags
& flag
) != 0; }
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
))); }
441 // returns false if we have any attributes set, true otherwise
442 bool IsDefault() const
444 return !HasTextColour() && !HasBackgroundColour() && !HasFont() && !HasAlignment() &&
445 !HasTabs() && !HasLeftIndent() && !HasRightIndent() &&
446 !HasParagraphSpacingAfter() && !HasParagraphSpacingBefore() && !HasLineSpacing() &&
447 !HasCharacterStyleName() && !HasParagraphStyleName() && !HasBulletNumber() && !HasBulletStyle() && !HasBulletSymbol();
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
;
462 int m_paragraphSpacingAfter
;
463 int m_paragraphSpacingBefore
;
467 wxChar m_bulletSymbol
;
475 bool m_fontUnderlined
;
476 wxString m_fontFaceName
;
479 wxString m_characterStyleName
;
482 wxString m_paragraphStyleName
;
485 #define wxTEXT_ATTR_CHARACTER (wxTEXT_ATTR_FONT) | wxTEXT_ATTR_BACKGROUND_COLOUR | wxTEXT_ATTR_TEXT_COLOUR
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
491 #define wxTEXT_ATTR_ALL wxTEXT_ATTR_CHARACTER|wxTEXT_ATTR_PARAGRAPH
494 * wxRichTextObject class declaration
495 * This is the base for drawable objects.
498 class WXDLLIMPEXP_ADV wxRichTextObject
: public wxObject
500 DECLARE_CLASS(wxRichTextObject
)
504 wxRichTextObject(wxRichTextObject
* parent
= NULL
);
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;
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;
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; }
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; }
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
; }
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;
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
; }
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
); }
541 virtual bool DeleteRange(const wxRichTextRange
& WXUNUSED(range
)) { return false; }
543 /// Returns true if the object is empty
544 virtual bool IsEmpty() const { return false; }
546 /// Get any text in this object for the given range
547 virtual wxString
GetTextForRange(const wxRichTextRange
& WXUNUSED(range
)) const { return wxEmptyString
; }
549 /// Returns true if this object can merge itself with the given one.
550 virtual bool CanMerge(wxRichTextObject
* WXUNUSED(object
)) const { return false; }
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; }
556 /// Dump to output stream for debugging
557 virtual void Dump(wxTextOutputStream
& stream
);
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
; }
565 /// Get/set the object position
566 virtual wxPoint
GetPosition() const { return m_pos
; }
567 virtual void SetPosition(const wxPoint
& pos
) { m_pos
= pos
; }
569 /// Get the rectangle enclosing the object
570 virtual wxRect
GetRect() const { return wxRect(GetPosition(), GetCachedSize()); }
573 void SetRange(const wxRichTextRange
& range
) { m_range
= range
; }
576 const wxRichTextRange
& GetRange() const { return m_range
; }
577 wxRichTextRange
& GetRange() { return m_range
; }
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
; }
583 /// Is this composite?
584 virtual bool IsComposite() const { return false; }
586 /// Get/set the parent.
587 virtual wxRichTextObject
* GetParent() const { return m_parent
; }
588 virtual void SetParent(wxRichTextObject
* parent
) { m_parent
= parent
; }
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
; }
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
; }
603 /// Set/get stored descent
604 void SetDescent(int descent
) { m_descent
= descent
; }
605 int GetDescent() const { return m_descent
; }
610 virtual wxRichTextObject
* Clone() const { return NULL
; }
613 void Copy(const wxRichTextObject
& obj
);
615 /// Reference-counting allows us to use the same object in multiple
616 /// lists (not yet used)
617 void Reference() { m_refCount
++; }
620 /// Convert units in tends of a millimetre to device units
621 int ConvertTenthsMMToPixels(wxDC
& dc
, int units
);
626 int m_descent
; // Descent for this object (if any)
629 wxRichTextObject
* m_parent
;
631 /// The range of this object (start position to end position)
632 wxRichTextRange m_range
;
641 wxTextAttrEx m_attributes
;
644 WX_DECLARE_EXPORTED_LIST( wxRichTextObject
, wxRichTextObjectList
);
647 * wxRichTextCompositeObject class declaration
648 * Objects of this class can contain other objects.
651 class WXDLLIMPEXP_ADV wxRichTextCompositeObject
: public wxRichTextObject
653 DECLARE_CLASS(wxRichTextCompositeObject
)
657 wxRichTextCompositeObject(wxRichTextObject
* parent
= NULL
);
658 ~wxRichTextCompositeObject();
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
);
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
);
670 virtual void CalculateRange(long start
, long& end
);
673 virtual bool DeleteRange(const wxRichTextRange
& range
);
675 /// Get any text in this object for the given range
676 virtual wxString
GetTextForRange(const wxRichTextRange
& range
) const;
678 /// Dump to output stream for debugging
679 virtual void Dump(wxTextOutputStream
& stream
);
684 wxRichTextObjectList
& GetChildren() { return m_children
; }
685 const wxRichTextObjectList
& GetChildren() const { return m_children
; }
687 /// Get the child count
688 size_t GetChildCount() const ;
690 /// Get the nth child
691 wxRichTextObject
* GetChild(size_t n
) const ;
693 /// Get/set dirty flag
694 virtual bool GetDirty() const { return m_dirty
; }
695 virtual void SetDirty(bool dirty
) { m_dirty
= dirty
; }
697 /// Is this composite?
698 virtual bool IsComposite() const { return true; }
700 /// Returns true if the buffer is empty
701 virtual bool IsEmpty() const { return GetChildCount() == 0; }
706 void Copy(const wxRichTextCompositeObject
& obj
);
708 /// Append a child, returning the position
709 size_t AppendChild(wxRichTextObject
* child
) ;
711 /// Insert the child in front of the given object, or at the beginning
712 bool InsertChild(wxRichTextObject
* child
, wxRichTextObject
* inFrontOf
) ;
715 bool RemoveChild(wxRichTextObject
* child
, bool deleteChild
= false) ;
717 /// Delete all children
718 bool DeleteChildren() ;
720 /// Recursively merge all pieces that can be merged.
724 wxRichTextObjectList m_children
;
728 * wxRichTextBox class declaration
729 * This defines a 2D space to lay out objects
732 class WXDLLIMPEXP_ADV wxRichTextBox
: public wxRichTextCompositeObject
734 DECLARE_DYNAMIC_CLASS(wxRichTextBox
)
738 wxRichTextBox(wxRichTextObject
* parent
= NULL
);
739 wxRichTextBox(const wxRichTextBox
& obj
): wxRichTextCompositeObject() { Copy(obj
); }
744 virtual bool Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
);
747 virtual bool Layout(wxDC
& dc
, const wxRect
& rect
, int style
);
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;
758 virtual wxRichTextObject
* Clone() const { return new wxRichTextBox(*this); }
761 void Copy(const wxRichTextBox
& obj
);
767 * wxRichTextParagraphBox class declaration
768 * This box knows how to lay out paragraphs.
771 class WXDLLIMPEXP_ADV wxRichTextParagraphLayoutBox
: public wxRichTextBox
773 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox
)
777 wxRichTextParagraphLayoutBox(wxRichTextObject
* parent
= NULL
);
778 wxRichTextParagraphLayoutBox(const wxRichTextParagraphLayoutBox
& obj
):wxRichTextBox() { Init(); Copy(obj
); }
783 virtual bool Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
);
786 virtual bool Layout(wxDC
& dc
, const wxRect
& rect
, int style
);
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;
793 virtual bool DeleteRange(const wxRichTextRange
& range
);
795 /// Get any text in this object for the given range
796 virtual wxString
GetTextForRange(const wxRichTextRange
& range
) const;
800 /// Associate a control with the buffer, for operations that for example require refreshing the window.
801 void SetRichTextCtrl(wxRichTextCtrl
* ctrl
) { m_ctrl
= ctrl
; }
803 /// Get the associated control.
804 wxRichTextCtrl
* GetRichTextCtrl() const { return m_ctrl
; }
808 /// Initialize the object.
811 /// Clear all children
812 virtual void Clear();
814 /// Clear and initialize with one blank paragraph
815 virtual void Reset();
817 /// Convenience function to add a paragraph of text
818 virtual wxRichTextRange
AddParagraph(const wxString
& text
);
820 /// Convenience function to add an image
821 virtual wxRichTextRange
AddImage(const wxImage
& image
);
823 /// Adds multiple paragraphs, based on newlines.
824 virtual wxRichTextRange
AddParagraphs(const wxString
& text
);
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;
830 /// Get the line at the given y pixel position, or the last line.
831 virtual wxRichTextLine
* GetLineAtYPosition(int y
) const;
833 /// Get the paragraph at the given character or caret position
834 virtual wxRichTextParagraph
* GetParagraphAtPosition(long pos
, bool caretPosition
= false) const;
836 /// Get the line size at the given position
837 virtual wxSize
GetLineSizeAtPosition(long pos
, bool caretPosition
= false) const;
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
844 virtual long GetVisibleLineNumber(long pos
, bool caretPosition
= false, bool startOfLine
= false) const;
846 /// Given a line number, get the corresponding wxRichTextLine object.
847 virtual wxRichTextLine
* GetLineForVisibleLineNumber(long lineNumber
) const;
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;
853 /// Get the paragraph by number
854 virtual wxRichTextParagraph
* GetParagraphAtLine(long paragraphNumber
) const;
856 /// Get the paragraph for a given line
857 virtual wxRichTextParagraph
* GetParagraphForLine(wxRichTextLine
* line
) const;
859 /// Get the length of the paragraph
860 virtual int GetParagraphLength(long paragraphNumber
) const;
862 /// Get the number of paragraphs
863 virtual int GetParagraphCount() const { return GetChildCount(); }
865 /// Get the number of visible lines
866 virtual int GetLineCount() const;
868 /// Get the text of the paragraph
869 virtual wxString
GetParagraphText(long paragraphNumber
) const;
871 /// Convert zero-based line column and paragraph number to a position.
872 virtual long XYToPosition(long x
, long y
) const;
874 /// Convert zero-based position to line column and paragraph number
875 virtual bool PositionToXY(long pos
, long* x
, long* y
) const;
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);
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;
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;
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;
900 virtual wxRichTextObject
* Clone() const { return new wxRichTextParagraphLayoutBox(*this); }
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
905 virtual bool InsertFragment(long position
, wxRichTextFragment
& fragment
);
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
);
911 void Copy(const wxRichTextParagraphLayoutBox
& obj
);
914 virtual void UpdateRanges() { long end
; CalculateRange(0, end
); }
917 virtual wxString
GetText() const;
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
);
923 /// Get default style
924 virtual const wxTextAttrEx
& GetDefaultStyle() const { return m_defaultAttributes
; }
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
); }
930 /// Get basic (overall) style
931 virtual const wxTextAttrEx
& GetBasicStyle() const { return m_attributes
; }
933 /// Invalidate the buffer. With no argument, invalidates whole buffer.
934 void Invalidate(const wxRichTextRange
& invalidRange
= wxRichTextRange(-1, -1));
936 /// Get invalid range, rounding to entire paragraphs if argument is true.
937 wxRichTextRange
GetInvalidRange(bool wholeParagraphs
= false) const;
940 wxRichTextCtrl
* m_ctrl
;
941 wxTextAttrEx m_defaultAttributes
;
943 /// The invalidated range that will need full layout
944 wxRichTextRange m_invalidRange
;
948 * wxRichTextFragment class declaration
949 * This is a lind of paragraph layout box used for storing
950 * paragraphs for Undo/Redo, for example.
953 class WXDLLIMPEXP_ADV wxRichTextFragment
: public wxRichTextParagraphLayoutBox
955 DECLARE_DYNAMIC_CLASS(wxRichTextFragment
)
959 wxRichTextFragment() { Init(); }
960 wxRichTextFragment(const wxRichTextFragment
& obj
):wxRichTextParagraphLayoutBox() { Init(); Copy(obj
); }
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
; }
976 void Copy(const wxRichTextFragment
& obj
);
979 virtual wxRichTextObject
* Clone() const { return new wxRichTextFragment(*this); }
983 // Is the last paragraph partial or complete?
984 bool m_partialParagraph
;
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.
994 class WXDLLIMPEXP_ADV wxRichTextLine
999 wxRichTextLine(wxRichTextParagraph
* parent
);
1000 wxRichTextLine(const wxRichTextLine
& obj
) { Init(); Copy(obj
); }
1001 virtual ~wxRichTextLine() {}
1008 void SetRange(const wxRichTextRange
& range
) { m_range
= range
; }
1009 void SetRange(long from
, long to
) { m_range
= wxRichTextRange(from
, to
); }
1011 /// Get the parent paragraph
1012 wxRichTextParagraph
* GetParent() { return m_parent
; }
1015 const wxRichTextRange
& GetRange() const { return m_range
; }
1016 wxRichTextRange
& GetRange() { return m_range
; }
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
; }
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
; }
1026 /// Get the absolute object position
1027 virtual wxPoint
GetAbsolutePosition() const;
1029 /// Get the rectangle enclosing the line
1030 virtual wxRect
GetRect() const { return wxRect(GetAbsolutePosition(), GetSize()); }
1032 /// Set/get stored descent
1033 void SetDescent(int descent
) { m_descent
= descent
; }
1034 int GetDescent() const { return m_descent
; }
1042 void Copy(const wxRichTextLine
& obj
);
1045 virtual wxRichTextLine
* Clone() const { return new wxRichTextLine(*this); }
1049 /// The range of the line (start position to end position)
1050 wxRichTextRange m_range
;
1052 /// Size and position measured relative to top of paragraph
1056 /// Maximum descent for this line (location of text baseline)
1059 // The parent object
1060 wxRichTextParagraph
* m_parent
;
1063 WX_DECLARE_EXPORTED_LIST( wxRichTextLine
, wxRichTextLineList
);
1066 * wxRichTextParagraph class declaration
1067 * This object represents a single paragraph (or in a straight text editor, a line).
1070 class WXDLLIMPEXP_ADV wxRichTextParagraph
: public wxRichTextBox
1072 DECLARE_DYNAMIC_CLASS(wxRichTextParagraph
)
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
); }
1084 virtual bool Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
);
1086 /// Lay the item out
1087 virtual bool Layout(wxDC
& dc
, const wxRect
& rect
, int style
);
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;
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
);
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
);
1101 virtual void CalculateRange(long start
, long& end
);
1105 /// Get the cached lines
1106 wxRichTextLineList
& GetLines() { return m_cachedLines
; }
1111 void Copy(const wxRichTextParagraph
& obj
);
1114 virtual wxRichTextObject
* Clone() const { return new wxRichTextParagraph(*this); }
1116 /// Clear the cached lines
1121 /// Apply paragraph styles such as centering to the wrapped lines
1122 virtual void ApplyParagraphStyle(const wxRect
& rect
);
1124 /// Insert text at the given position
1125 virtual bool InsertText(long pos
, const wxString
& text
);
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
);
1131 /// Move content to a list from this point
1132 virtual void MoveToList(wxRichTextObject
* obj
, wxList
& list
);
1134 /// Add content back from list
1135 virtual void MoveFromList(wxList
& list
);
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);
1141 /// Find a suitable wrap position. wrapPosition is the last position in the line to the left
1143 bool FindWrapPosition(const wxRichTextRange
& range
, wxDC
& dc
, int availableSpace
, long& wrapPosition
);
1145 /// Find the object at the given position
1146 wxRichTextObject
* FindObjectAtPosition(long position
);
1148 /// Get the bullet text for this paragraph.
1149 wxString
GetBulletText();
1152 /// The lines that make up the wrapped paragraph
1153 wxRichTextLineList m_cachedLines
;
1157 * wxRichTextPlainText class declaration
1158 * This object represents a single piece of text.
1161 class WXDLLIMPEXP_ADV wxRichTextPlainText
: public wxRichTextObject
1163 DECLARE_DYNAMIC_CLASS(wxRichTextPlainText
)
1167 wxRichTextPlainText(const wxString
& text
= wxEmptyString
, wxRichTextObject
* parent
= NULL
, wxTextAttrEx
* style
= NULL
);
1168 wxRichTextPlainText(const wxRichTextPlainText
& obj
):wxRichTextObject() { Copy(obj
); }
1173 virtual bool Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
);
1175 /// Lay the item out
1176 virtual bool Layout(wxDC
& dc
, const wxRect
& rect
, int style
);
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;
1182 /// Get any text in this object for the given range
1183 virtual wxString
GetTextForRange(const wxRichTextRange
& range
) const;
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
);
1190 virtual void CalculateRange(long start
, long& end
);
1193 virtual bool DeleteRange(const wxRichTextRange
& range
);
1195 /// Returns true if the object is empty
1196 virtual bool IsEmpty() const { return m_text
.empty(); }
1198 /// Returns true if this object can merge itself with the given one.
1199 virtual bool CanMerge(wxRichTextObject
* object
) const;
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
);
1205 /// Dump to output stream for debugging
1206 virtual void Dump(wxTextOutputStream
& stream
);
1211 const wxString
& GetText() const { return m_text
; }
1214 void SetText(const wxString
& text
) { m_text
= text
; }
1219 void Copy(const wxRichTextPlainText
& obj
);
1222 virtual wxRichTextObject
* Clone() const { return new wxRichTextPlainText(*this); }
1229 * wxRichTextImageBlock stores information about an image, in binary in-memory form
1232 class WXDLLIMPEXP_BASE wxDataInputStream
;
1233 class WXDLLIMPEXP_BASE wxDataOutputStream
;
1235 class WXDLLIMPEXP_ADV wxRichTextImageBlock
: public wxObject
1238 wxRichTextImageBlock();
1239 wxRichTextImageBlock(const wxRichTextImageBlock
& block
);
1240 ~wxRichTextImageBlock();
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);
1252 // Make an image block from the wxImage in the given
1254 virtual bool MakeImageBlock(wxImage
& image
, int imageType
, int quality
= 80);
1257 bool Write(const wxString
& filename
);
1259 // Write data in hex to a stream
1260 bool WriteHex(wxOutputStream
& stream
);
1262 // Read data in hex from a stream
1263 bool ReadHex(wxInputStream
& stream
, int length
, int imageType
);
1265 // Copy from 'block'
1266 void Copy(const wxRichTextImageBlock
& block
);
1268 // Load a wxImage from the block
1269 bool Load(wxImage
& image
);
1272 void operator=(const wxRichTextImageBlock
& block
);
1276 unsigned char* GetData() const { return m_data
; }
1277 size_t GetDataSize() const { return m_dataSize
; }
1278 int GetImageType() const { return m_imageType
; }
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
; }
1284 bool Ok() const { return GetData() != NULL
; }
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
);
1292 // Write memory block to stream
1293 static bool WriteBlock(wxOutputStream
& stream
, unsigned char* block
, size_t size
);
1295 // Write memory block to file
1296 static bool WriteBlock(const wxString
& filename
, unsigned char* block
, size_t size
);
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
;
1303 int m_imageType
; // wxWin type id
1308 * wxRichTextImage class declaration
1309 * This object represents an image.
1312 class WXDLLIMPEXP_ADV wxRichTextImage
: public wxRichTextObject
1314 DECLARE_DYNAMIC_CLASS(wxRichTextImage
)
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
); }
1326 virtual bool Draw(wxDC
& dc
, const wxRichTextRange
& range
, const wxRichTextRange
& selectionRange
, const wxRect
& rect
, int descent
, int style
);
1328 /// Lay the item out
1329 virtual bool Layout(wxDC
& dc
, const wxRect
& rect
, int style
);
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;
1335 /// Returns true if the object is empty
1336 virtual bool IsEmpty() const { return !m_image
.Ok(); }
1341 const wxImage
& GetImage() const { return m_image
; }
1344 void SetImage(const wxImage
& image
) { m_image
= image
; }
1346 /// Get the image block containing the raw data
1347 wxRichTextImageBlock
& GetImageBlock() { return m_imageBlock
; }
1352 void Copy(const wxRichTextImage
& obj
);
1355 virtual wxRichTextObject
* Clone() const { return new wxRichTextImage(*this); }
1357 /// Load wxImage from the block
1358 virtual bool LoadFromBlock();
1360 /// Make block from the wxImage
1361 virtual bool MakeBlock();
1364 // TODO: reduce the multiple representations of data
1367 wxRichTextImageBlock m_imageBlock
;
1372 * wxRichTextBuffer class declaration
1373 * This is a kind of box, used to represent the whole buffer
1376 class WXDLLIMPEXP_ADV wxRichTextCommand
;
1377 class WXDLLIMPEXP_ADV wxRichTextAction
;
1379 class WXDLLIMPEXP_ADV wxRichTextBuffer
: public wxRichTextParagraphLayoutBox
1381 DECLARE_DYNAMIC_CLASS(wxRichTextBuffer
)
1385 wxRichTextBuffer() { Init(); }
1386 wxRichTextBuffer(const wxRichTextBuffer
& obj
):wxRichTextParagraphLayoutBox() { Init(); Copy(obj
); }
1387 ~wxRichTextBuffer() ;
1391 /// Gets the command processor
1392 wxCommandProcessor
* GetCommandProcessor() const { return m_commandProcessor
; }
1394 /// Set style sheet, if any.
1395 void SetStyleSheet(wxRichTextStyleSheet
* styleSheet
) { m_styleSheet
= styleSheet
; }
1396 wxRichTextStyleSheet
* GetStyleSheet() const { return m_styleSheet
; }
1403 /// Clears the buffer and resets the command processor
1404 virtual void Clear();
1406 /// The same as Clear, and adds an empty paragraph.
1407 virtual void Reset();
1410 virtual bool LoadFile(const wxString
& filename
, int type
= wxRICHTEXT_TYPE_ANY
);
1413 virtual bool SaveFile(const wxString
& filename
, int type
= wxRICHTEXT_TYPE_ANY
);
1415 /// Load from a stream
1416 virtual bool LoadFile(wxInputStream
& stream
, int type
= wxRICHTEXT_TYPE_ANY
);
1418 /// Save to a stream
1419 virtual bool SaveFile(wxOutputStream
& stream
, int type
= wxRICHTEXT_TYPE_ANY
);
1421 /// Convenience function to add a paragraph of text
1422 virtual wxRichTextRange
AddParagraph(const wxString
& text
) { Modify(); return wxRichTextParagraphLayoutBox
::AddParagraph(text
); }
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
);
1429 /// End collapsing undo/redo commands
1430 virtual bool EndBatchUndo();
1432 /// Collapsing commands?
1433 virtual bool BatchingUndo() const { return m_batchedCommandDepth
> 0; }
1435 /// Submit immediately, or delay according to whether collapsing is on
1436 virtual bool SubmitAction(wxRichTextAction
* action
);
1438 /// Get collapsed command
1439 virtual wxRichTextCommand
* GetBatchedCommand() const { return m_batchedCommand
; }
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();
1447 /// End suppressing undo/redo commands.
1448 virtual bool EndSuppressUndo();
1450 /// Collapsing commands?
1451 virtual bool SuppressingUndo() const { return m_suppressUndo
> 0; }
1453 /// Copy the range to the clipboard
1454 virtual bool CopyToClipboard(const wxRichTextRange
& range
);
1456 /// Paste the clipboard content to the buffer
1457 virtual bool PasteFromClipboard(long position
);
1459 /// Can we paste from the clipboard?
1460 virtual bool CanPasteFromClipboard() const;
1462 /// Begin using a style
1463 virtual bool BeginStyle(const wxTextAttrEx
& style
);
1466 virtual bool EndStyle();
1469 virtual bool EndAllStyles();
1471 /// Clear the style stack
1472 virtual void ClearStyleStack();
1474 /// Get the size of the style stack, for example to check correct nesting
1475 virtual int GetStyleStackSize() const { return m_attributeStack
.GetCount(); }
1477 /// Begin using bold
1481 bool EndBold() { return EndStyle(); }
1483 /// Begin using italic
1486 /// End using italic
1487 bool EndItalic() { return EndStyle(); }
1489 /// Begin using underline
1490 bool BeginUnderline();
1492 /// End using underline
1493 bool EndUnderline() { return EndStyle(); }
1495 /// Begin using point size
1496 bool BeginFontSize(int pointSize
);
1498 /// End using point size
1499 bool EndFontSize() { return EndStyle(); }
1501 /// Begin using this font
1502 bool BeginFont(const wxFont
& font
);
1504 /// End using a font
1505 bool EndFont() { return EndStyle(); }
1507 /// Begin using this colour
1508 bool BeginTextColour(const wxColour
& colour
);
1510 /// End using a colour
1511 bool EndTextColour() { return EndStyle(); }
1513 /// Begin using alignment
1514 bool BeginAlignment(wxTextAttrAlignment alignment
);
1517 bool EndAlignment() { return EndStyle(); }
1519 /// Begin left indent
1520 bool BeginLeftIndent(int leftIndent
, int leftSubIndent
= 0);
1523 bool EndLeftIndent() { return EndStyle(); }
1525 /// Begin right indent
1526 bool BeginRightIndent(int rightIndent
);
1528 /// End right indent
1529 bool EndRightIndent() { return EndStyle(); }
1531 /// Begin paragraph spacing
1532 bool BeginParagraphSpacing(int before
, int after
);
1534 /// End paragraph spacing
1535 bool EndParagraphSpacing() { return EndStyle(); }
1537 /// Begin line spacing
1538 bool BeginLineSpacing(int lineSpacing
);
1540 /// End line spacing
1541 bool EndLineSpacing() { return EndStyle(); }
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
);
1546 /// End numbered bullet
1547 bool EndNumberedBullet() { return EndStyle(); }
1549 /// Begin symbol bullet
1550 bool BeginSymbolBullet(wxChar symbol
, int leftIndent
, int leftSubIndent
, int bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_SYMBOL
);
1552 /// End symbol bullet
1553 bool EndSymbolBullet() { return EndStyle(); }
1555 /// Begin named character style
1556 bool BeginCharacterStyle(const wxString
& characterStyle
);
1558 /// End named character style
1559 bool EndCharacterStyle() { return EndStyle(); }
1561 /// Begin named paragraph style
1562 bool BeginParagraphStyle(const wxString
& paragraphStyle
);
1564 /// End named character style
1565 bool EndParagraphStyle() { return EndStyle(); }
1570 void Copy(const wxRichTextBuffer
& obj
) { wxRichTextBox
::Copy(obj
); }
1573 virtual wxRichTextObject
* Clone() const { return new wxRichTextBuffer(*this); }
1575 /// Submit command to insert the given text
1576 bool InsertTextWithUndo(long pos
, const wxString
& text
, wxRichTextCtrl
* ctrl
);
1578 /// Submit command to insert a newline
1579 bool InsertNewlineWithUndo(long pos
, wxRichTextCtrl
* ctrl
);
1581 /// Submit command to insert the given image
1582 bool InsertImageWithUndo(long pos
, const wxRichTextImageBlock
& imageBlock
, wxRichTextCtrl
* ctrl
);
1584 /// Submit command to delete this range
1585 bool DeleteRangeWithUndo(const wxRichTextRange
& range
, long initialCaretPosition
, long newCaretPositon
, wxRichTextCtrl
* ctrl
);
1588 void Modify(bool modify
= true) { m_modified
= modify
; }
1589 bool IsModified() const { return m_modified
; }
1591 /// Dumps contents of buffer for debugging purposes
1592 virtual void Dump();
1593 virtual void Dump(wxTextOutputStream
& stream
) { wxRichTextParagraphLayoutBox
::Dump(stream
); }
1595 /// Returns the file handlers
1596 static wxList
& GetHandlers() { return sm_handlers
; }
1598 /// Adds a handler to the end
1599 static void AddHandler(wxRichTextFileHandler
*handler
);
1601 /// Inserts a handler at the front
1602 static void InsertHandler(wxRichTextFileHandler
*handler
);
1604 /// Removes a handler
1605 static bool RemoveHandler(const wxString
& name
);
1607 /// Finds a handler by name
1608 static wxRichTextFileHandler
*FindHandler(const wxString
& name
);
1610 /// Finds a handler by extension and type
1611 static wxRichTextFileHandler
*FindHandler(const wxString
& extension
, int imageType
);
1613 /// Finds a handler by filename or, if supplied, type
1614 static wxRichTextFileHandler
*FindHandlerFilenameOrType(const wxString
& filename
, int imageType
);
1616 /// Finds a handler by type
1617 static wxRichTextFileHandler
*FindHandler(int imageType
);
1619 /// Gets a wildcard incorporating all visible handlers
1620 static wxString
GetExtWildcard(bool combine
= false, bool save
= false);
1622 /// Clean up handlers
1623 static void CleanUpHandlers();
1625 /// Initialise the standard handlers
1626 static void InitStandardHandlers();
1630 /// Command processor
1631 wxCommandProcessor
* m_commandProcessor
;
1633 /// Has been modified?
1636 /// Collapsed command stack
1637 int m_batchedCommandDepth
;
1639 /// Name for collapsed command
1640 wxString m_batchedCommandsName
;
1642 /// Current collapsed command accumulating actions
1643 wxRichTextCommand
* m_batchedCommand
;
1645 /// Whether to suppress undo
1648 /// Style sheet, if any
1649 wxRichTextStyleSheet
* m_styleSheet
;
1651 /// Stack of attributes for convenience functions
1652 wxList m_attributeStack
;
1655 static wxList sm_handlers
;
1659 * The command identifiers
1663 enum wxRichTextCommandId
1667 wxRICHTEXT_CHANGE_STYLE
1671 * Command classes for undo/redo
1675 class WXDLLIMPEXP_ADV wxRichTextAction
;
1676 class WXDLLIMPEXP_ADV wxRichTextCommand
: public wxCommand
1679 // Ctor for one action
1680 wxRichTextCommand(const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
1681 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
= false);
1683 // Ctor for multiple actions
1684 wxRichTextCommand(const wxString
& name
);
1686 ~wxRichTextCommand();
1691 void AddAction(wxRichTextAction
* action
);
1692 void ClearActions();
1694 wxList
& GetActions() { return m_actions
; }
1702 * wxRichTextAction class declaration
1703 * There can be more than one action in a command.
1706 class WXDLLIMPEXP_ADV wxRichTextAction
: public wxObject
1709 wxRichTextAction(wxRichTextCommand
* cmd
, const wxString
& name
, wxRichTextCommandId id
, wxRichTextBuffer
* buffer
,
1710 wxRichTextCtrl
* ctrl
, bool ignoreFirstTime
= false);
1712 ~wxRichTextAction();
1717 /// Update the control appearance
1718 void UpdateAppearance(long caretPosition
, bool sendUpdateEvent
= false);
1720 /// Replace the buffer paragraphs with the given fragment.
1721 void ApplyParagraphs(const wxRichTextFragment
& fragment
);
1723 /// Get the fragments
1724 wxRichTextFragment
& GetNewParagraphs() { return m_newParagraphs
; }
1725 wxRichTextFragment
& GetOldParagraphs() { return m_oldParagraphs
; }
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
; }
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
; }
1736 const wxString
& GetName() const { return m_name
; }
1743 wxRichTextBuffer
* m_buffer
;
1746 wxRichTextCtrl
* m_ctrl
;
1748 // Stores the new paragraphs
1749 wxRichTextFragment m_newParagraphs
;
1751 // Stores the old paragraphs
1752 wxRichTextFragment m_oldParagraphs
;
1754 // The affected range
1755 wxRichTextRange m_range
;
1757 // The insertion point for this command
1760 // Ignore 1st 'Do' operation because we already did it
1763 // The command identifier
1764 wxRichTextCommandId m_cmdId
;
1768 * wxRichTextFileHandler
1769 * Base class for file handlers
1772 class WXDLLIMPEXP_ADV wxRichTextFileHandler
: public wxObject
1774 DECLARE_CLASS(wxRichTextFileHandler
)
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)
1781 bool LoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
1782 { return DoLoadFile(buffer
, stream
); }
1783 bool SaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
1784 { return DoSaveFile(buffer
, stream
); }
1787 bool LoadFile(wxRichTextBuffer
*buffer
, const wxString
& filename
);
1788 bool SaveFile(wxRichTextBuffer
*buffer
, const wxString
& filename
);
1790 /// Can we handle this filename (if using files)? By default, checks the extension.
1791 virtual bool CanHandle(const wxString
& filename
) const;
1793 /// Can we save using this handler?
1794 virtual bool CanSave() const { return false; }
1796 /// Can we load using this handler?
1797 virtual bool CanLoad() const { return false; }
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
; }
1803 void SetName(const wxString
& name
) { m_name
= name
; }
1804 wxString
GetName() const { return m_name
; }
1806 void SetExtension(const wxString
& ext
) { m_extension
= ext
; }
1807 wxString
GetExtension() const { return m_extension
; }
1809 void SetType(int type
) { m_type
= type
; }
1810 int GetType() const { return m_type
; }
1815 virtual bool DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
) = 0;
1816 virtual bool DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
) = 0;
1820 wxString m_extension
;
1826 * wxRichTextPlainTextHandler
1827 * Plain text handler
1830 class WXDLLIMPEXP_ADV wxRichTextPlainTextHandler
: public wxRichTextFileHandler
1832 DECLARE_CLASS(wxRichTextPlainTextHandler
)
1834 wxRichTextPlainTextHandler(const wxString
& name
= wxT("Text"), const wxString
& ext
= wxT("txt"), int type
= wxRICHTEXT_TYPE_TEXT
)
1835 : wxRichTextFileHandler(name
, ext
, type
)
1838 /// Can we save using this handler?
1839 virtual bool CanSave() const { return true; }
1841 /// Can we load using this handler?
1842 virtual bool CanLoad() const { return true; }
1847 virtual bool DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
);
1848 virtual bool DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
);
1858 inline bool wxRichTextHasStyle(int flags
, int style
)
1860 return ((flags
& style
) == style
);
1863 /// Compare two attribute objects
1864 bool wxTextAttrEq(const wxTextAttrEx
& attr1
, const wxTextAttrEx
& attr2
);
1865 bool wxTextAttrEq(const wxTextAttr
& attr1
, const wxRichTextAttr
& attr2
);
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
);
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
);
1881 // _WX_RICHTEXTBUFFER_H_