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