Added extra hit test style for more accurate reporting
[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 is a single box representing the document, and this box is
26 a wxRichTextParagraphLayoutBox which contains further wxRichTextParagraph
27 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 Note that internally, a range (5,5) represents a range of one character.
38 In the public wx[Rich]TextCtrl API, this would be passed to e.g. SetSelection
39 as (5,6). A paragraph with one character might have an internal range of (0, 1)
40 since the end of the paragraph takes up one position.
41
42 Layout
43 ======
44
45 When Layout is called on an object, it is given a size which the object
46 must limit itself to, or one or more flexible directions (vertical
47 or horizontal). So for example a centered paragraph is given the page
48 width to play with (minus any margins), but can extend indefinitely
49 in the vertical direction. The implementation of Layout can then
50 cache the calculated size and position within the parent.
51
52 */
53
54 /*!
55 * Includes
56 */
57
58 #include "wx/defs.h"
59
60 #if wxUSE_RICHTEXT
61
62 #include "wx/list.h"
63 #include "wx/textctrl.h"
64 #include "wx/bitmap.h"
65 #include "wx/image.h"
66 #include "wx/cmdproc.h"
67 #include "wx/txtstrm.h"
68
69 #if wxUSE_DATAOBJ
70 #include "wx/dataobj.h"
71 #endif
72
73 /*!
74 * Special characters
75 */
76
77 extern WXDLLIMPEXP_RICHTEXT const wxChar wxRichTextLineBreakChar;
78
79 /*!
80 * File types
81 */
82
83 #define wxRICHTEXT_TYPE_ANY 0
84 #define wxRICHTEXT_TYPE_TEXT 1
85 #define wxRICHTEXT_TYPE_XML 2
86 #define wxRICHTEXT_TYPE_HTML 3
87 #define wxRICHTEXT_TYPE_RTF 4
88 #define wxRICHTEXT_TYPE_PDF 5
89
90 /*!
91 * Forward declarations
92 */
93
94 class WXDLLIMPEXP_RICHTEXT wxRichTextCtrl;
95 class WXDLLIMPEXP_RICHTEXT wxRichTextObject;
96 class WXDLLIMPEXP_RICHTEXT wxRichTextCacheObject;
97 class WXDLLIMPEXP_RICHTEXT wxRichTextObjectList;
98 class WXDLLIMPEXP_RICHTEXT wxRichTextLine;
99 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraph;
100 class WXDLLIMPEXP_RICHTEXT wxRichTextFileHandler;
101 class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet;
102 class WXDLLIMPEXP_RICHTEXT wxTextAttrEx;
103 class WXDLLIMPEXP_RICHTEXT wxRichTextListStyleDefinition;
104 class WXDLLIMPEXP_RICHTEXT wxRichTextEvent;
105 class WXDLLIMPEXP_RICHTEXT wxRichTextRenderer;
106 class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer;
107
108 /*!
109 * Flags determining the available space, passed to Layout
110 */
111
112 #define wxRICHTEXT_FIXED_WIDTH 0x01
113 #define wxRICHTEXT_FIXED_HEIGHT 0x02
114 #define wxRICHTEXT_VARIABLE_WIDTH 0x04
115 #define wxRICHTEXT_VARIABLE_HEIGHT 0x08
116
117 // Only lay out the part of the buffer that lies within
118 // the rect passed to Layout.
119 #define wxRICHTEXT_LAYOUT_SPECIFIED_RECT 0x10
120
121 /*!
122 * Flags to pass to Draw
123 */
124
125 // Ignore paragraph cache optimization, e.g. for printing purposes
126 // where one line may be drawn higher (on the next page) compared
127 // with the previous line
128 #define wxRICHTEXT_DRAW_IGNORE_CACHE 0x01
129
130 /*!
131 * Flags returned from hit-testing
132 */
133
134 // The point was not on this object
135 #define wxRICHTEXT_HITTEST_NONE 0x01
136 // The point was before the position returned from HitTest
137 #define wxRICHTEXT_HITTEST_BEFORE 0x02
138 // The point was after the position returned from HitTest
139 #define wxRICHTEXT_HITTEST_AFTER 0x04
140 // The point was on the position returned from HitTest
141 #define wxRICHTEXT_HITTEST_ON 0x08
142 // The point was on space outside content
143 #define wxRICHTEXT_HITTEST_OUTSIDE 0x10
144
145 /*!
146 * Flags for GetRangeSize
147 */
148
149 #define wxRICHTEXT_FORMATTED 0x01
150 #define wxRICHTEXT_UNFORMATTED 0x02
151
152 /*!
153 * Flags for SetStyle/SetListStyle
154 */
155
156 #define wxRICHTEXT_SETSTYLE_NONE 0x00
157
158 // Specifies that this operation should be undoable
159 #define wxRICHTEXT_SETSTYLE_WITH_UNDO 0x01
160
161 // Specifies that the style should not be applied if the
162 // combined style at this point is already the style in question.
163 #define wxRICHTEXT_SETSTYLE_OPTIMIZE 0x02
164
165 // Specifies that the style should only be applied to paragraphs,
166 // and not the content. This allows content styling to be
167 // preserved independently from that of e.g. a named paragraph style.
168 #define wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY 0x04
169
170 // Specifies that the style should only be applied to characters,
171 // and not the paragraph. This allows content styling to be
172 // preserved independently from that of e.g. a named paragraph style.
173 #define wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY 0x08
174
175 // For SetListStyle only: specifies starting from the given number, otherwise
176 // deduces number from existing attributes
177 #define wxRICHTEXT_SETSTYLE_RENUMBER 0x10
178
179 // For SetListStyle only: specifies the list level for all paragraphs, otherwise
180 // the current indentation will be used
181 #define wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL 0x20
182
183 // Resets the existing style before applying the new style
184 #define wxRICHTEXT_SETSTYLE_RESET 0x40
185
186 /*!
187 * Flags for text insertion
188 */
189
190 #define wxRICHTEXT_INSERT_NONE 0x00
191 #define wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE 0x01
192
193 /*!
194 * Extra formatting flags not in wxTextAttr
195 */
196
197 #define wxTEXT_ATTR_PARA_SPACING_AFTER 0x00000800
198 #define wxTEXT_ATTR_PARA_SPACING_BEFORE 0x00001000
199 #define wxTEXT_ATTR_LINE_SPACING 0x00002000
200 #define wxTEXT_ATTR_CHARACTER_STYLE_NAME 0x00004000
201 #define wxTEXT_ATTR_PARAGRAPH_STYLE_NAME 0x00008000
202 #define wxTEXT_ATTR_LIST_STYLE_NAME 0x00010000
203 #define wxTEXT_ATTR_BULLET_STYLE 0x00020000
204 #define wxTEXT_ATTR_BULLET_NUMBER 0x00040000
205 #define wxTEXT_ATTR_BULLET_TEXT 0x00080000
206 #define wxTEXT_ATTR_BULLET_NAME 0x00100000
207 #define wxTEXT_ATTR_URL 0x00200000
208 #define wxTEXT_ATTR_PAGE_BREAK 0x00400000
209 #define wxTEXT_ATTR_EFFECTS 0x00800000
210 #define wxTEXT_ATTR_OUTLINE_LEVEL 0x01000000
211
212 /*!
213 * Styles for wxTextAttrEx::SetBulletStyle
214 */
215
216 #define wxTEXT_ATTR_BULLET_STYLE_NONE 0x00000000
217 #define wxTEXT_ATTR_BULLET_STYLE_ARABIC 0x00000001
218 #define wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER 0x00000002
219 #define wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER 0x00000004
220 #define wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER 0x00000008
221 #define wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER 0x00000010
222 #define wxTEXT_ATTR_BULLET_STYLE_SYMBOL 0x00000020
223 #define wxTEXT_ATTR_BULLET_STYLE_BITMAP 0x00000040
224 #define wxTEXT_ATTR_BULLET_STYLE_PARENTHESES 0x00000080
225 #define wxTEXT_ATTR_BULLET_STYLE_PERIOD 0x00000100
226 #define wxTEXT_ATTR_BULLET_STYLE_STANDARD 0x00000200
227 #define wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS 0x00000400
228 #define wxTEXT_ATTR_BULLET_STYLE_OUTLINE 0x00000800
229
230 #define wxTEXT_ATTR_BULLET_STYLE_ALIGN_LEFT 0x00000000
231 #define wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT 0x00001000
232 #define wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE 0x00002000
233
234 /*!
235 * Styles for wxTextAttrEx::SetTextEffects
236 */
237
238 #define wxTEXT_ATTR_EFFECT_NONE 0x00000000
239 #define wxTEXT_ATTR_EFFECT_CAPITALS 0x00000001
240 #define wxTEXT_ATTR_EFFECT_SMALL_CAPITALS 0x00000002
241 #define wxTEXT_ATTR_EFFECT_STRIKETHROUGH 0x00000004
242 #define wxTEXT_ATTR_EFFECT_DOUBLE_STRIKETHROUGH 0x00000008
243 #define wxTEXT_ATTR_EFFECT_SHADOW 0x00000010
244 #define wxTEXT_ATTR_EFFECT_EMBOSS 0x00000020
245 #define wxTEXT_ATTR_EFFECT_OUTLINE 0x00000040
246 #define wxTEXT_ATTR_EFFECT_ENGRAVE 0x00000080
247 #define wxTEXT_ATTR_EFFECT_SUPERSCRIPT 0x00000100
248 #define wxTEXT_ATTR_EFFECT_SUBSCRIPT 0x00000200
249
250 /*!
251 * Line spacing values
252 */
253
254 #define wxTEXT_ATTR_LINE_SPACING_NORMAL 10
255 #define wxTEXT_ATTR_LINE_SPACING_HALF 15
256 #define wxTEXT_ATTR_LINE_SPACING_TWICE 20
257
258 /*!
259 * Character and paragraph combined styles
260 */
261
262 #define wxTEXT_ATTR_CHARACTER (wxTEXT_ATTR_FONT|wxTEXT_ATTR_EFFECTS|wxTEXT_ATTR_BACKGROUND_COLOUR|wxTEXT_ATTR_TEXT_COLOUR|wxTEXT_ATTR_CHARACTER_STYLE_NAME|wxTEXT_ATTR_URL)
263
264 #define wxTEXT_ATTR_PARAGRAPH (wxTEXT_ATTR_ALIGNMENT|wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_TABS|\
265 wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|\
266 wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER|wxTEXT_ATTR_BULLET_TEXT|wxTEXT_ATTR_BULLET_NAME|\
267 wxTEXT_ATTR_PARAGRAPH_STYLE_NAME|wxTEXT_ATTR_LIST_STYLE_NAME|wxTEXT_ATTR_OUTLINE_LEVEL)
268
269 #define wxTEXT_ATTR_ALL (wxTEXT_ATTR_CHARACTER|wxTEXT_ATTR_PARAGRAPH)
270
271 /*!
272 * wxRichTextRange class declaration
273 * This stores beginning and end positions for a range of data.
274 */
275
276 class WXDLLIMPEXP_RICHTEXT wxRichTextRange
277 {
278 public:
279 // Constructors
280
281 wxRichTextRange() { m_start = 0; m_end = 0; }
282 wxRichTextRange(long start, long end) { m_start = start; m_end = end; }
283 wxRichTextRange(const wxRichTextRange& range) { m_start = range.m_start; m_end = range.m_end; }
284 ~wxRichTextRange() {}
285
286 void operator =(const wxRichTextRange& range) { m_start = range.m_start; m_end = range.m_end; }
287 bool operator ==(const wxRichTextRange& range) const { return (m_start == range.m_start && m_end == range.m_end); }
288 bool operator !=(const wxRichTextRange& range) const { return (m_start != range.m_start && m_end != range.m_end); }
289 wxRichTextRange operator -(const wxRichTextRange& range) const { return wxRichTextRange(m_start - range.m_start, m_end - range.m_end); }
290 wxRichTextRange operator +(const wxRichTextRange& range) const { return wxRichTextRange(m_start + range.m_start, m_end + range.m_end); }
291
292 void SetRange(long start, long end) { m_start = start; m_end = end; }
293
294 void SetStart(long start) { m_start = start; }
295 long GetStart() const { return m_start; }
296
297 void SetEnd(long end) { m_end = end; }
298 long GetEnd() const { return m_end; }
299
300 /// Returns true if this range is completely outside 'range'
301 bool IsOutside(const wxRichTextRange& range) const { return range.m_start > m_end || range.m_end < m_start; }
302
303 /// Returns true if this range is completely within 'range'
304 bool IsWithin(const wxRichTextRange& range) const { return m_start >= range.m_start && m_end <= range.m_end; }
305
306 /// Returns true if the given position is within this range. Allow
307 /// for the possibility of an empty range - assume the position
308 /// is within this empty range. NO, I think we should not match with an empty range.
309 // bool Contains(long pos) const { return pos >= m_start && (pos <= m_end || GetLength() == 0); }
310 bool Contains(long pos) const { return pos >= m_start && pos <= m_end ; }
311
312 /// Limit this range to be within 'range'
313 bool LimitTo(const wxRichTextRange& range) ;
314
315 /// Gets the length of the range
316 long GetLength() const { return m_end - m_start + 1; }
317
318 /// Swaps the start and end
319 void Swap() { long tmp = m_start; m_start = m_end; m_end = tmp; }
320
321 /// Convert to internal form: (n, n) is the range of a single character.
322 wxRichTextRange ToInternal() const { return wxRichTextRange(m_start, m_end-1); }
323
324 /// Convert from internal to public API form: (n, n+1) is the range of a single character.
325 wxRichTextRange FromInternal() const { return wxRichTextRange(m_start, m_end+1); }
326
327 protected:
328 long m_start;
329 long m_end;
330 };
331
332 #define wxRICHTEXT_ALL wxRichTextRange(-2, -2)
333 #define wxRICHTEXT_NONE wxRichTextRange(-1, -1)
334
335 /*!
336 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
337 */
338
339 class WXDLLIMPEXP_RICHTEXT wxTextAttrEx: public wxTextAttr
340 {
341 public:
342 // ctors
343 wxTextAttrEx(const wxTextAttrEx& attr);
344 wxTextAttrEx(const wxTextAttr& attr) { Init(); (*this) = attr; }
345 wxTextAttrEx() { Init(); }
346
347 // Initialise this object
348 void Init();
349
350 // Copy
351 void Copy(const wxTextAttrEx& attr);
352
353 // Assignment from a wxTextAttrEx object
354 void operator= (const wxTextAttrEx& attr);
355
356 // Assignment from a wxTextAttr object
357 void operator= (const wxTextAttr& attr);
358
359 // Equality test
360 bool operator== (const wxTextAttrEx& attr) const;
361
362 // setters
363 void SetCharacterStyleName(const wxString& name) { m_characterStyleName = name; SetFlags(GetFlags() | wxTEXT_ATTR_CHARACTER_STYLE_NAME); }
364 void SetParagraphStyleName(const wxString& name) { m_paragraphStyleName = name; SetFlags(GetFlags() | wxTEXT_ATTR_PARAGRAPH_STYLE_NAME); }
365 void SetListStyleName(const wxString& name) { m_listStyleName = name; SetFlags(GetFlags() | wxTEXT_ATTR_LIST_STYLE_NAME); }
366 void SetParagraphSpacingAfter(int spacing) { m_paragraphSpacingAfter = spacing; SetFlags(GetFlags() | wxTEXT_ATTR_PARA_SPACING_AFTER); }
367 void SetParagraphSpacingBefore(int spacing) { m_paragraphSpacingBefore = spacing; SetFlags(GetFlags() | wxTEXT_ATTR_PARA_SPACING_BEFORE); }
368 void SetLineSpacing(int spacing) { m_lineSpacing = spacing; SetFlags(GetFlags() | wxTEXT_ATTR_LINE_SPACING); }
369 void SetBulletStyle(int style) { m_bulletStyle = style; SetFlags(GetFlags() | wxTEXT_ATTR_BULLET_STYLE); }
370 void SetBulletNumber(int n) { m_bulletNumber = n; SetFlags(GetFlags() | wxTEXT_ATTR_BULLET_NUMBER); }
371 void SetBulletText(const wxString& text) { m_bulletText = text; SetFlags(GetFlags() | wxTEXT_ATTR_BULLET_TEXT); }
372 void SetBulletName(const wxString& name) { m_bulletName = name; SetFlags(GetFlags() | wxTEXT_ATTR_BULLET_NAME); }
373 void SetBulletFont(const wxString& bulletFont) { m_bulletFont = bulletFont; }
374 void SetURL(const wxString& url) { m_urlTarget = url; SetFlags(GetFlags() | wxTEXT_ATTR_URL); }
375 void SetPageBreak(bool pageBreak = true) { SetFlags(pageBreak ? (GetFlags() | wxTEXT_ATTR_PAGE_BREAK) : (GetFlags() & ~wxTEXT_ATTR_PAGE_BREAK)); }
376 void SetTextEffects(int effects) { m_textEffects = effects; SetFlags(GetFlags() | wxTEXT_ATTR_EFFECTS); }
377 void SetTextEffectFlags(int effects) { m_textEffectFlags = effects; }
378 void SetOutlineLevel(int level) { m_outlineLevel = level; SetFlags(GetFlags() | wxTEXT_ATTR_OUTLINE_LEVEL); }
379
380 const wxString& GetCharacterStyleName() const { return m_characterStyleName; }
381 const wxString& GetParagraphStyleName() const { return m_paragraphStyleName; }
382 const wxString& GetListStyleName() const { return m_listStyleName; }
383 int GetParagraphSpacingAfter() const { return m_paragraphSpacingAfter; }
384 int GetParagraphSpacingBefore() const { return m_paragraphSpacingBefore; }
385 int GetLineSpacing() const { return m_lineSpacing; }
386 int GetBulletStyle() const { return m_bulletStyle; }
387 int GetBulletNumber() const { return m_bulletNumber; }
388 const wxString& GetBulletText() const { return m_bulletText; }
389 const wxString& GetBulletName() const { return m_bulletName; }
390 const wxString& GetBulletFont() const { return m_bulletFont; }
391 const wxString& GetURL() const { return m_urlTarget; }
392 int GetTextEffects() const { return m_textEffects; }
393 int GetTextEffectFlags() const { return m_textEffectFlags; }
394 int GetOutlineLevel() const { return m_outlineLevel; }
395
396 bool HasFontWeight() const { return (GetFlags() & wxTEXT_ATTR_FONT_WEIGHT) != 0; }
397 bool HasFontSize() const { return (GetFlags() & wxTEXT_ATTR_FONT_SIZE) != 0; }
398 bool HasFontItalic() const { return (GetFlags() & wxTEXT_ATTR_FONT_ITALIC) != 0; }
399 bool HasFontUnderlined() const { return (GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE) != 0; }
400 bool HasFontFaceName() const { return (GetFlags() & wxTEXT_ATTR_FONT_FACE) != 0; }
401
402 bool HasParagraphSpacingAfter() const { return HasFlag(wxTEXT_ATTR_PARA_SPACING_AFTER); }
403 bool HasParagraphSpacingBefore() const { return HasFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE); }
404 bool HasLineSpacing() const { return HasFlag(wxTEXT_ATTR_LINE_SPACING); }
405 bool HasCharacterStyleName() const { return HasFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME) && !m_characterStyleName.IsEmpty(); }
406 bool HasParagraphStyleName() const { return HasFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) && !m_paragraphStyleName.IsEmpty(); }
407 bool HasListStyleName() const { return HasFlag(wxTEXT_ATTR_LIST_STYLE_NAME) || !m_listStyleName.IsEmpty(); }
408 bool HasBulletStyle() const { return HasFlag(wxTEXT_ATTR_BULLET_STYLE); }
409 bool HasBulletNumber() const { return HasFlag(wxTEXT_ATTR_BULLET_NUMBER); }
410 bool HasBulletText() const { return HasFlag(wxTEXT_ATTR_BULLET_TEXT); }
411 bool HasBulletName() const { return HasFlag(wxTEXT_ATTR_BULLET_NAME); }
412 bool HasURL() const { return HasFlag(wxTEXT_ATTR_URL); }
413 bool HasPageBreak() const { return HasFlag(wxTEXT_ATTR_PAGE_BREAK); }
414 bool HasTextEffects() const { return HasFlag(wxTEXT_ATTR_EFFECTS); }
415 bool HasTextEffect(int effect) const { return HasFlag(wxTEXT_ATTR_EFFECTS) && ((GetTextEffectFlags() & effect) != 0); }
416 bool HasOutlineLevel() const { return HasFlag(wxTEXT_ATTR_OUTLINE_LEVEL); }
417
418 // Is this a character style?
419 bool IsCharacterStyle() const { return (0 != (GetFlags() & wxTEXT_ATTR_CHARACTER)); }
420 bool IsParagraphStyle() const { return (0 != (GetFlags() & wxTEXT_ATTR_PARAGRAPH)); }
421
422 // returns false if we have any attributes set, true otherwise
423 bool IsDefault() const
424 {
425 return (GetFlags() == 0);
426 }
427
428 // return the attribute having the valid font and colours: it uses the
429 // attributes set in attr and falls back first to attrDefault and then to
430 // the text control font/colours for those attributes which are not set
431 static wxTextAttrEx CombineEx(const wxTextAttrEx& attr,
432 const wxTextAttrEx& attrDef,
433 const wxTextCtrlBase *text);
434
435 private:
436 // Paragraph styles
437 int m_paragraphSpacingAfter;
438 int m_paragraphSpacingBefore;
439 int m_lineSpacing;
440 int m_bulletStyle;
441 int m_bulletNumber;
442 int m_textEffects;
443 int m_textEffectFlags;
444 int m_outlineLevel;
445 wxString m_bulletText;
446 wxString m_bulletFont;
447 wxString m_bulletName;
448 wxString m_urlTarget;
449
450 // Character style
451 wxString m_characterStyleName;
452
453 // Paragraph style
454 wxString m_paragraphStyleName;
455
456 // List style
457 wxString m_listStyleName;
458 };
459
460 /*!
461 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
462 * efficient way to query styles.
463 */
464
465 class WXDLLIMPEXP_RICHTEXT wxRichTextAttr
466 {
467 public:
468 // ctors
469 wxRichTextAttr(const wxTextAttrEx& attr);
470 wxRichTextAttr(const wxRichTextAttr& attr);
471 wxRichTextAttr() { Init(); }
472 wxRichTextAttr(const wxColour& colText,
473 const wxColour& colBack = wxNullColour,
474 wxTextAttrAlignment alignment = wxTEXT_ALIGNMENT_DEFAULT);
475
476 // Initialise this object.
477 void Init();
478
479 // Copy
480 void Copy(const wxRichTextAttr& attr);
481
482 // Assignment from a wxRichTextAttr object.
483 void operator= (const wxRichTextAttr& attr);
484
485 // Assignment from a wxTextAttrEx object.
486 void operator= (const wxTextAttrEx& attr);
487
488 // Equality test
489 bool operator== (const wxRichTextAttr& attr) const;
490
491 // Making a wxTextAttrEx object.
492 operator wxTextAttrEx () const ;
493
494 // Create font from font attributes.
495 wxFont CreateFont() const;
496
497 // Get attributes from font.
498 bool GetFontAttributes(const wxFont& font);
499
500 // setters
501 void SetTextColour(const wxColour& colText) { m_colText = colText; m_flags |= wxTEXT_ATTR_TEXT_COLOUR; }
502 void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR; }
503 void SetAlignment(wxTextAttrAlignment alignment) { m_textAlignment = alignment; m_flags |= wxTEXT_ATTR_ALIGNMENT; }
504 void SetTabs(const wxArrayInt& tabs) { m_tabs = tabs; m_flags |= wxTEXT_ATTR_TABS; }
505 void SetLeftIndent(int indent, int subIndent = 0) { m_leftIndent = indent; m_leftSubIndent = subIndent; m_flags |= wxTEXT_ATTR_LEFT_INDENT; }
506 void SetRightIndent(int indent) { m_rightIndent = indent; m_flags |= wxTEXT_ATTR_RIGHT_INDENT; }
507
508 void SetFontSize(int pointSize) { m_fontSize = pointSize; m_flags |= wxTEXT_ATTR_FONT_SIZE; }
509 void SetFontStyle(int fontStyle) { m_fontStyle = fontStyle; m_flags |= wxTEXT_ATTR_FONT_ITALIC; }
510 void SetFontWeight(int fontWeight) { m_fontWeight = fontWeight; m_flags |= wxTEXT_ATTR_FONT_WEIGHT; }
511 void SetFontFaceName(const wxString& faceName) { m_fontFaceName = faceName; m_flags |= wxTEXT_ATTR_FONT_FACE; }
512 void SetFontUnderlined(bool underlined) { m_fontUnderlined = underlined; m_flags |= wxTEXT_ATTR_FONT_UNDERLINE; }
513
514 void SetFlags(long flags) { m_flags = flags; }
515
516 void SetCharacterStyleName(const wxString& name) { m_characterStyleName = name; m_flags |= wxTEXT_ATTR_CHARACTER_STYLE_NAME; }
517 void SetParagraphStyleName(const wxString& name) { m_paragraphStyleName = name; m_flags |= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME; }
518 void SetListStyleName(const wxString& name) { m_listStyleName = name; SetFlags(GetFlags() | wxTEXT_ATTR_LIST_STYLE_NAME); }
519 void SetParagraphSpacingAfter(int spacing) { m_paragraphSpacingAfter = spacing; m_flags |= wxTEXT_ATTR_PARA_SPACING_AFTER; }
520 void SetParagraphSpacingBefore(int spacing) { m_paragraphSpacingBefore = spacing; m_flags |= wxTEXT_ATTR_PARA_SPACING_BEFORE; }
521 void SetLineSpacing(int spacing) { m_lineSpacing = spacing; m_flags |= wxTEXT_ATTR_LINE_SPACING; }
522 void SetBulletStyle(int style) { m_bulletStyle = style; m_flags |= wxTEXT_ATTR_BULLET_STYLE; }
523 void SetBulletNumber(int n) { m_bulletNumber = n; m_flags |= wxTEXT_ATTR_BULLET_NUMBER; }
524 void SetBulletText(const wxString& text) { m_bulletText = text; m_flags |= wxTEXT_ATTR_BULLET_TEXT; }
525 void SetBulletFont(const wxString& bulletFont) { m_bulletFont = bulletFont; }
526 void SetBulletName(const wxString& name) { m_bulletName = name; m_flags |= wxTEXT_ATTR_BULLET_NAME; }
527 void SetURL(const wxString& url) { m_urlTarget = url; m_flags |= wxTEXT_ATTR_URL; }
528 void SetPageBreak(bool pageBreak = true) { SetFlags(pageBreak ? (GetFlags() | wxTEXT_ATTR_PAGE_BREAK) : (GetFlags() & ~wxTEXT_ATTR_PAGE_BREAK)); }
529 void SetTextEffects(int effects) { m_textEffects = effects; SetFlags(GetFlags() | wxTEXT_ATTR_EFFECTS); }
530 void SetTextEffectFlags(int effects) { m_textEffectFlags = effects; }
531 void SetOutlineLevel(int level) { m_outlineLevel = level; SetFlags(GetFlags() | wxTEXT_ATTR_OUTLINE_LEVEL); }
532
533 const wxColour& GetTextColour() const { return m_colText; }
534 const wxColour& GetBackgroundColour() const { return m_colBack; }
535 wxTextAttrAlignment GetAlignment() const { return m_textAlignment; }
536 const wxArrayInt& GetTabs() const { return m_tabs; }
537 long GetLeftIndent() const { return m_leftIndent; }
538 long GetLeftSubIndent() const { return m_leftSubIndent; }
539 long GetRightIndent() const { return m_rightIndent; }
540 long GetFlags() const { return m_flags; }
541
542 int GetFontSize() const { return m_fontSize; }
543 int GetFontStyle() const { return m_fontStyle; }
544 int GetFontWeight() const { return m_fontWeight; }
545 bool GetFontUnderlined() const { return m_fontUnderlined; }
546 const wxString& GetFontFaceName() const { return m_fontFaceName; }
547
548 const wxString& GetCharacterStyleName() const { return m_characterStyleName; }
549 const wxString& GetParagraphStyleName() const { return m_paragraphStyleName; }
550 const wxString& GetListStyleName() const { return m_listStyleName; }
551 int GetParagraphSpacingAfter() const { return m_paragraphSpacingAfter; }
552 int GetParagraphSpacingBefore() const { return m_paragraphSpacingBefore; }
553 int GetLineSpacing() const { return m_lineSpacing; }
554 int GetBulletStyle() const { return m_bulletStyle; }
555 int GetBulletNumber() const { return m_bulletNumber; }
556 const wxString& GetBulletText() const { return m_bulletText; }
557 const wxString& GetBulletFont() const { return m_bulletFont; }
558 const wxString& GetBulletName() const { return m_bulletName; }
559 const wxString& GetURL() const { return m_urlTarget; }
560 int GetTextEffects() const { return m_textEffects; }
561 int GetTextEffectFlags() const { return m_textEffectFlags; }
562 int GetOutlineLevel() const { return m_outlineLevel; }
563
564 // accessors
565 bool HasTextColour() const { return m_colText.Ok() && HasFlag(wxTEXT_ATTR_TEXT_COLOUR) ; }
566 bool HasBackgroundColour() const { return m_colBack.Ok() && HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR) ; }
567 bool HasAlignment() const { return (m_textAlignment != wxTEXT_ALIGNMENT_DEFAULT) || ((m_flags & wxTEXT_ATTR_ALIGNMENT) != 0) ; }
568 bool HasTabs() const { return (m_flags & wxTEXT_ATTR_TABS) != 0 ; }
569 bool HasLeftIndent() const { return (m_flags & wxTEXT_ATTR_LEFT_INDENT) != 0 ; }
570 bool HasRightIndent() const { return (m_flags & wxTEXT_ATTR_RIGHT_INDENT) != 0 ; }
571 bool HasFontWeight() const { return (m_flags & wxTEXT_ATTR_FONT_WEIGHT) != 0; }
572 bool HasFontSize() const { return (m_flags & wxTEXT_ATTR_FONT_SIZE) != 0; }
573 bool HasFontItalic() const { return (m_flags & wxTEXT_ATTR_FONT_ITALIC) != 0; }
574 bool HasFontUnderlined() const { return (m_flags & wxTEXT_ATTR_FONT_UNDERLINE) != 0; }
575 bool HasFontFaceName() const { return (m_flags & wxTEXT_ATTR_FONT_FACE) != 0; }
576 bool HasFont() const { return (m_flags & (wxTEXT_ATTR_FONT)) != 0; }
577
578 bool HasParagraphSpacingAfter() const { return (m_flags & wxTEXT_ATTR_PARA_SPACING_AFTER) != 0; }
579 bool HasParagraphSpacingBefore() const { return (m_flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) != 0; }
580 bool HasLineSpacing() const { return (m_flags & wxTEXT_ATTR_LINE_SPACING) != 0; }
581 bool HasCharacterStyleName() const { return (m_flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) != 0 && !m_characterStyleName.IsEmpty(); }
582 bool HasParagraphStyleName() const { return (m_flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) != 0 && !m_paragraphStyleName.IsEmpty(); }
583 bool HasListStyleName() const { return HasFlag(wxTEXT_ATTR_LIST_STYLE_NAME) || !m_listStyleName.IsEmpty(); }
584 bool HasBulletStyle() const { return (m_flags & wxTEXT_ATTR_BULLET_STYLE) != 0; }
585 bool HasBulletNumber() const { return (m_flags & wxTEXT_ATTR_BULLET_NUMBER) != 0; }
586 bool HasBulletText() const { return (m_flags & wxTEXT_ATTR_BULLET_TEXT) != 0; }
587 bool HasBulletName() const { return (m_flags & wxTEXT_ATTR_BULLET_NAME) != 0; }
588 bool HasURL() const { return HasFlag(wxTEXT_ATTR_URL); }
589 bool HasPageBreak() const { return HasFlag(wxTEXT_ATTR_PAGE_BREAK); }
590 bool HasTextEffects() const { return HasFlag(wxTEXT_ATTR_EFFECTS); }
591 bool HasTextEffect(int effect) const { return HasFlag(wxTEXT_ATTR_EFFECTS) && ((GetTextEffectFlags() & effect) != 0); }
592 bool HasOutlineLevel() const { return HasFlag(wxTEXT_ATTR_OUTLINE_LEVEL); }
593
594 bool HasFlag(long flag) const { return (m_flags & flag) != 0; }
595
596 // Is this a character style?
597 bool IsCharacterStyle() const { return (0 != (GetFlags() & wxTEXT_ATTR_CHARACTER)); }
598 bool IsParagraphStyle() const { return (0 != (GetFlags() & wxTEXT_ATTR_PARAGRAPH)); }
599
600 // returns false if we have any attributes set, true otherwise
601 bool IsDefault() const
602 {
603 return GetFlags() == 0;
604 }
605
606 // Merges the given attributes. Does not affect 'this'. If compareWith
607 // is non-NULL, then it will be used to mask out those attributes that are the same in style
608 // and compareWith, for situations where we don't want to explicitly set inherited attributes.
609 bool Apply(const wxRichTextAttr& style, const wxRichTextAttr* compareWith = NULL);
610
611 // Merges the given attributes and returns the result. Does not affect 'this'. If compareWith
612 // is non-NULL, then it will be used to mask out those attributes that are the same in style
613 // and compareWith, for situations where we don't want to explicitly set inherited attributes.
614 wxRichTextAttr Combine(const wxRichTextAttr& style, const wxRichTextAttr* compareWith = NULL) const;
615
616 private:
617 long m_flags;
618
619 // Paragraph styles
620 wxArrayInt m_tabs; // array of int: tab stops in 1/10 mm
621 int m_leftIndent; // left indent in 1/10 mm
622 int m_leftSubIndent; // left indent for all but the first
623 // line in a paragraph relative to the
624 // first line, in 1/10 mm
625 int m_rightIndent; // right indent in 1/10 mm
626 wxTextAttrAlignment m_textAlignment;
627
628 int m_paragraphSpacingAfter;
629 int m_paragraphSpacingBefore;
630 int m_lineSpacing;
631 int m_bulletStyle;
632 int m_bulletNumber;
633 int m_textEffects;
634 int m_textEffectFlags;
635 int m_outlineLevel;
636 wxString m_bulletText;
637 wxString m_bulletFont;
638 wxString m_bulletName;
639 wxString m_urlTarget;
640
641 // Character styles
642 wxColour m_colText,
643 m_colBack;
644 int m_fontSize;
645 int m_fontStyle;
646 int m_fontWeight;
647 bool m_fontUnderlined;
648 wxString m_fontFaceName;
649
650 // Character style
651 wxString m_characterStyleName;
652
653 // Paragraph style
654 wxString m_paragraphStyleName;
655
656 // List style
657 wxString m_listStyleName;
658 };
659
660 /*!
661 * wxRichTextObject class declaration
662 * This is the base for drawable objects.
663 */
664
665 class WXDLLIMPEXP_RICHTEXT wxRichTextObject: public wxObject
666 {
667 DECLARE_CLASS(wxRichTextObject)
668 public:
669 // Constructors
670
671 wxRichTextObject(wxRichTextObject* parent = NULL);
672 virtual ~wxRichTextObject();
673
674 // Overrideables
675
676 /// Draw the item, within the given range. Some objects may ignore the range (for
677 /// example paragraphs) while others must obey it (lines, to implement wrapping)
678 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style) = 0;
679
680 /// Lay the item out at the specified position with the given size constraint.
681 /// Layout must set the cached size.
682 virtual bool Layout(wxDC& dc, const wxRect& rect, int style) = 0;
683
684 /// Hit-testing: returns a flag indicating hit test details, plus
685 /// information about position
686 virtual int HitTest(wxDC& WXUNUSED(dc), const wxPoint& WXUNUSED(pt), long& WXUNUSED(textPosition)) { return false; }
687
688 /// Finds the absolute position and row height for the given character position
689 virtual bool FindPosition(wxDC& WXUNUSED(dc), long WXUNUSED(index), wxPoint& WXUNUSED(pt), int* WXUNUSED(height), bool WXUNUSED(forceLineStart)) { return false; }
690
691 /// Get the best size, i.e. the ideal starting size for this object irrespective
692 /// of available space. For a short text string, it will be the size that exactly encloses
693 /// the text. For a longer string, it might use the parent width for example.
694 virtual wxSize GetBestSize() const { return m_size; }
695
696 /// Get the object size for the given range. Returns false if the range
697 /// is invalid for this object.
698 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const = 0;
699
700 /// Do a split, returning an object containing the second part, and setting
701 /// the first part in 'this'.
702 virtual wxRichTextObject* DoSplit(long WXUNUSED(pos)) { return NULL; }
703
704 /// Calculate range. By default, guess that the object is 1 unit long.
705 virtual void CalculateRange(long start, long& end) { end = start ; m_range.SetRange(start, end); }
706
707 /// Delete range
708 virtual bool DeleteRange(const wxRichTextRange& WXUNUSED(range)) { return false; }
709
710 /// Returns true if the object is empty
711 virtual bool IsEmpty() const { return false; }
712
713 /// Get any text in this object for the given range
714 virtual wxString GetTextForRange(const wxRichTextRange& WXUNUSED(range)) const { return wxEmptyString; }
715
716 /// Returns true if this object can merge itself with the given one.
717 virtual bool CanMerge(wxRichTextObject* WXUNUSED(object)) const { return false; }
718
719 /// Returns true if this object merged itself with the given one.
720 /// The calling code will then delete the given object.
721 virtual bool Merge(wxRichTextObject* WXUNUSED(object)) { return false; }
722
723 /// Dump to output stream for debugging
724 virtual void Dump(wxTextOutputStream& stream);
725
726 // Accessors
727
728 /// Get/set the cached object size as calculated by Layout.
729 virtual wxSize GetCachedSize() const { return m_size; }
730 virtual void SetCachedSize(const wxSize& sz) { m_size = sz; }
731
732 /// Get/set the object position
733 virtual wxPoint GetPosition() const { return m_pos; }
734 virtual void SetPosition(const wxPoint& pos) { m_pos = pos; }
735
736 /// Get the rectangle enclosing the object
737 virtual wxRect GetRect() const { return wxRect(GetPosition(), GetCachedSize()); }
738
739 /// Set the range
740 void SetRange(const wxRichTextRange& range) { m_range = range; }
741
742 /// Get the range
743 const wxRichTextRange& GetRange() const { return m_range; }
744 wxRichTextRange& GetRange() { return m_range; }
745
746 /// Get/set dirty flag (whether the object needs Layout to be called)
747 virtual bool GetDirty() const { return m_dirty; }
748 virtual void SetDirty(bool dirty) { m_dirty = dirty; }
749
750 /// Is this composite?
751 virtual bool IsComposite() const { return false; }
752
753 /// Get/set the parent.
754 virtual wxRichTextObject* GetParent() const { return m_parent; }
755 virtual void SetParent(wxRichTextObject* parent) { m_parent = parent; }
756
757 /// Set the margin around the object
758 virtual void SetMargins(int margin);
759 virtual void SetMargins(int leftMargin, int rightMargin, int topMargin, int bottomMargin);
760 virtual int GetLeftMargin() const { return m_leftMargin; }
761 virtual int GetRightMargin() const { return m_rightMargin; }
762 virtual int GetTopMargin() const { return m_topMargin; }
763 virtual int GetBottomMargin() const { return m_bottomMargin; }
764
765 /// Set attributes object
766 void SetAttributes(const wxTextAttrEx& attr) { m_attributes = attr; }
767 const wxTextAttrEx& GetAttributes() const { return m_attributes; }
768 wxTextAttrEx& GetAttributes() { return m_attributes; }
769
770 /// Set/get stored descent
771 void SetDescent(int descent) { m_descent = descent; }
772 int GetDescent() const { return m_descent; }
773
774 /// Gets the containing buffer
775 wxRichTextBuffer* GetBuffer() const;
776
777 // Operations
778
779 /// Clone the object
780 virtual wxRichTextObject* Clone() const { return NULL; }
781
782 /// Copy
783 void Copy(const wxRichTextObject& obj);
784
785 /// Reference-counting allows us to use the same object in multiple
786 /// lists (not yet used)
787 void Reference() { m_refCount ++; }
788 void Dereference();
789
790 /// Convert units in tenths of a millimetre to device units
791 int ConvertTenthsMMToPixels(wxDC& dc, int units);
792 static int ConvertTenthsMMToPixels(int ppi, int units);
793
794 protected:
795 wxSize m_size;
796 wxPoint m_pos;
797 int m_descent; // Descent for this object (if any)
798 bool m_dirty;
799 int m_refCount;
800 wxRichTextObject* m_parent;
801
802 /// The range of this object (start position to end position)
803 wxRichTextRange m_range;
804
805 /// Margins
806 int m_leftMargin;
807 int m_rightMargin;
808 int m_topMargin;
809 int m_bottomMargin;
810
811 /// Attributes
812 wxTextAttrEx m_attributes;
813 };
814
815 WX_DECLARE_LIST_WITH_DECL( wxRichTextObject, wxRichTextObjectList, class WXDLLIMPEXP_RICHTEXT );
816
817 /*!
818 * wxRichTextCompositeObject class declaration
819 * Objects of this class can contain other objects.
820 */
821
822 class WXDLLIMPEXP_RICHTEXT wxRichTextCompositeObject: public wxRichTextObject
823 {
824 DECLARE_CLASS(wxRichTextCompositeObject)
825 public:
826 // Constructors
827
828 wxRichTextCompositeObject(wxRichTextObject* parent = NULL);
829 virtual ~wxRichTextCompositeObject();
830
831 // Overrideables
832
833 /// Hit-testing: returns a flag indicating hit test details, plus
834 /// information about position
835 virtual int HitTest(wxDC& dc, const wxPoint& pt, long& textPosition);
836
837 /// Finds the absolute position and row height for the given character position
838 virtual bool FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart);
839
840 /// Calculate range
841 virtual void CalculateRange(long start, long& end);
842
843 /// Delete range
844 virtual bool DeleteRange(const wxRichTextRange& range);
845
846 /// Get any text in this object for the given range
847 virtual wxString GetTextForRange(const wxRichTextRange& range) const;
848
849 /// Dump to output stream for debugging
850 virtual void Dump(wxTextOutputStream& stream);
851
852 // Accessors
853
854 /// Get the children
855 wxRichTextObjectList& GetChildren() { return m_children; }
856 const wxRichTextObjectList& GetChildren() const { return m_children; }
857
858 /// Get the child count
859 size_t GetChildCount() const ;
860
861 /// Get the nth child
862 wxRichTextObject* GetChild(size_t n) const ;
863
864 /// Get/set dirty flag
865 virtual bool GetDirty() const { return m_dirty; }
866 virtual void SetDirty(bool dirty) { m_dirty = dirty; }
867
868 /// Is this composite?
869 virtual bool IsComposite() const { return true; }
870
871 /// Returns true if the buffer is empty
872 virtual bool IsEmpty() const { return GetChildCount() == 0; }
873
874 // Operations
875
876 /// Copy
877 void Copy(const wxRichTextCompositeObject& obj);
878
879 /// Assignment
880 void operator= (const wxRichTextCompositeObject& obj) { Copy(obj); }
881
882 /// Append a child, returning the position
883 size_t AppendChild(wxRichTextObject* child) ;
884
885 /// Insert the child in front of the given object, or at the beginning
886 bool InsertChild(wxRichTextObject* child, wxRichTextObject* inFrontOf) ;
887
888 /// Delete the child
889 bool RemoveChild(wxRichTextObject* child, bool deleteChild = false) ;
890
891 /// Delete all children
892 bool DeleteChildren() ;
893
894 /// Recursively merge all pieces that can be merged.
895 bool Defragment();
896
897 protected:
898 wxRichTextObjectList m_children;
899 };
900
901 /*!
902 * wxRichTextBox class declaration
903 * This defines a 2D space to lay out objects
904 */
905
906 class WXDLLIMPEXP_RICHTEXT wxRichTextBox: public wxRichTextCompositeObject
907 {
908 DECLARE_DYNAMIC_CLASS(wxRichTextBox)
909 public:
910 // Constructors
911
912 wxRichTextBox(wxRichTextObject* parent = NULL);
913 wxRichTextBox(const wxRichTextBox& obj): wxRichTextCompositeObject() { Copy(obj); }
914
915 // Overrideables
916
917 /// Draw the item
918 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
919
920 /// Lay the item out
921 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
922
923 /// Get/set the object size for the given range. Returns false if the range
924 /// is invalid for this object.
925 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const;
926
927 // Accessors
928
929 // Operations
930
931 /// Clone
932 virtual wxRichTextObject* Clone() const { return new wxRichTextBox(*this); }
933
934 /// Copy
935 void Copy(const wxRichTextBox& obj);
936
937 protected:
938 };
939
940 /*!
941 * wxRichTextParagraphBox class declaration
942 * This box knows how to lay out paragraphs.
943 */
944
945 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraphLayoutBox: public wxRichTextBox
946 {
947 DECLARE_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox)
948 public:
949 // Constructors
950
951 wxRichTextParagraphLayoutBox(wxRichTextObject* parent = NULL);
952 wxRichTextParagraphLayoutBox(const wxRichTextParagraphLayoutBox& obj): wxRichTextBox() { Init(); Copy(obj); }
953
954 // Overrideables
955
956 /// Draw the item
957 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
958
959 /// Lay the item out
960 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
961
962 /// Get/set the object size for the given range. Returns false if the range
963 /// is invalid for this object.
964 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const;
965
966 /// Delete range
967 virtual bool DeleteRange(const wxRichTextRange& range);
968
969 /// Get any text in this object for the given range
970 virtual wxString GetTextForRange(const wxRichTextRange& range) const;
971
972 // Accessors
973
974 /// Associate a control with the buffer, for operations that for example require refreshing the window.
975 void SetRichTextCtrl(wxRichTextCtrl* ctrl) { m_ctrl = ctrl; }
976
977 /// Get the associated control.
978 wxRichTextCtrl* GetRichTextCtrl() const { return m_ctrl; }
979
980 /// Get/set whether the last paragraph is partial or complete
981 void SetPartialParagraph(bool partialPara) { m_partialParagraph = partialPara; }
982 bool GetPartialParagraph() const { return m_partialParagraph; }
983
984 /// If this is a buffer, returns the current style sheet. The base layout box
985 /// class doesn't have an associated style sheet.
986 virtual wxRichTextStyleSheet* GetStyleSheet() const { return NULL; }
987
988 // Operations
989
990 /// Initialize the object.
991 void Init();
992
993 /// Clear all children
994 virtual void Clear();
995
996 /// Clear and initialize with one blank paragraph
997 virtual void Reset();
998
999 /// Convenience function to add a paragraph of text
1000 virtual wxRichTextRange AddParagraph(const wxString& text, wxTextAttrEx* paraStyle = NULL);
1001
1002 /// Convenience function to add an image
1003 virtual wxRichTextRange AddImage(const wxImage& image, wxTextAttrEx* paraStyle = NULL);
1004
1005 /// Adds multiple paragraphs, based on newlines.
1006 virtual wxRichTextRange AddParagraphs(const wxString& text, wxTextAttrEx* paraStyle = NULL);
1007
1008 /// Get the line at the given position. If caretPosition is true, the position is
1009 /// a caret position, which is normally a smaller number.
1010 virtual wxRichTextLine* GetLineAtPosition(long pos, bool caretPosition = false) const;
1011
1012 /// Get the line at the given y pixel position, or the last line.
1013 virtual wxRichTextLine* GetLineAtYPosition(int y) const;
1014
1015 /// Get the paragraph at the given character or caret position
1016 virtual wxRichTextParagraph* GetParagraphAtPosition(long pos, bool caretPosition = false) const;
1017
1018 /// Get the line size at the given position
1019 virtual wxSize GetLineSizeAtPosition(long pos, bool caretPosition = false) const;
1020
1021 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1022 /// starting from zero at the start of the buffer. We also have to pass a bool (startOfLine)
1023 /// that indicates whether the caret is being shown at the end of the previous line or at the start
1024 /// of the next, since the caret can be shown at 2 visible positions for the same underlying
1025 /// position.
1026 virtual long GetVisibleLineNumber(long pos, bool caretPosition = false, bool startOfLine = false) const;
1027
1028 /// Given a line number, get the corresponding wxRichTextLine object.
1029 virtual wxRichTextLine* GetLineForVisibleLineNumber(long lineNumber) const;
1030
1031 /// Get the leaf object in a paragraph at this position.
1032 /// Given a line number, get the corresponding wxRichTextLine object.
1033 virtual wxRichTextObject* GetLeafObjectAtPosition(long position) const;
1034
1035 /// Get the paragraph by number
1036 virtual wxRichTextParagraph* GetParagraphAtLine(long paragraphNumber) const;
1037
1038 /// Get the paragraph for a given line
1039 virtual wxRichTextParagraph* GetParagraphForLine(wxRichTextLine* line) const;
1040
1041 /// Get the length of the paragraph
1042 virtual int GetParagraphLength(long paragraphNumber) const;
1043
1044 /// Get the number of paragraphs
1045 virtual int GetParagraphCount() const { return GetChildCount(); }
1046
1047 /// Get the number of visible lines
1048 virtual int GetLineCount() const;
1049
1050 /// Get the text of the paragraph
1051 virtual wxString GetParagraphText(long paragraphNumber) const;
1052
1053 /// Convert zero-based line column and paragraph number to a position.
1054 virtual long XYToPosition(long x, long y) const;
1055
1056 /// Convert zero-based position to line column and paragraph number
1057 virtual bool PositionToXY(long pos, long* x, long* y) const;
1058
1059 /// Set text attributes: character and/or paragraph styles.
1060 virtual bool SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO);
1061 virtual bool SetStyle(const wxRichTextRange& range, const wxTextAttrEx& style, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO);
1062
1063 /// Get the conbined text attributes for this position.
1064 virtual bool GetStyle(long position, wxTextAttrEx& style);
1065 virtual bool GetStyle(long position, wxRichTextAttr& style);
1066
1067 /// Get the content (uncombined) attributes for this position.
1068 virtual bool GetUncombinedStyle(long position, wxTextAttrEx& style);
1069 virtual bool GetUncombinedStyle(long position, wxRichTextAttr& style);
1070
1071 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1072 /// context attributes.
1073 virtual bool DoGetStyle(long position, wxTextAttrEx& style, bool combineStyles = true);
1074
1075 /// Get the combined style for a range - if any attribute is different within the range,
1076 /// that attribute is not present within the flags
1077 virtual bool GetStyleForRange(const wxRichTextRange& range, wxTextAttrEx& style);
1078
1079 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1080 /// content.
1081 bool CollectStyle(wxTextAttrEx& currentStyle, const wxTextAttrEx& style, long& multipleStyleAttributes, int& multipleTextEffectAttributes);
1082
1083 /// Set list style
1084 virtual bool SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO, int startFrom = 1, int specifiedLevel = -1);
1085 virtual bool SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO, int startFrom = 1, int specifiedLevel = -1);
1086
1087 /// Clear list for given range
1088 virtual bool ClearListStyle(const wxRichTextRange& range, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO);
1089
1090 /// Number/renumber any list elements in the given range.
1091 /// def/defName can be NULL/empty to indicate that the existing list style should be used.
1092 virtual bool NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def = NULL, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO, int startFrom = 1, int specifiedLevel = -1);
1093 virtual bool NumberList(const wxRichTextRange& range, const wxString& defName, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO, int startFrom = 1, int specifiedLevel = -1);
1094
1095 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
1096 /// def/defName can be NULL/empty to indicate that the existing list style should be used.
1097 virtual bool PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def = NULL, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO, int specifiedLevel = -1);
1098 virtual bool PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO, int specifiedLevel = -1);
1099
1100 /// Helper for NumberList and PromoteList, that does renumbering and promotion simultaneously
1101 /// def/defName can be NULL/empty to indicate that the existing list style should be used.
1102 virtual bool DoNumberList(const wxRichTextRange& range, const wxRichTextRange& promotionRange, int promoteBy, wxRichTextListStyleDefinition* def, int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO, int startFrom = 1, int specifiedLevel = -1);
1103
1104 /// Fills in the attributes for numbering a paragraph after previousParagraph.
1105 virtual bool FindNextParagraphNumber(wxRichTextParagraph* previousParagraph, wxRichTextAttr& attr) const;
1106
1107 /// Test if this whole range has character attributes of the specified kind. If any
1108 /// of the attributes are different within the range, the test fails. You
1109 /// can use this to implement, for example, bold button updating. style must have
1110 /// flags indicating which attributes are of interest.
1111 virtual bool HasCharacterAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const;
1112 virtual bool HasCharacterAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const;
1113
1114 /// Test if this whole range has paragraph attributes of the specified kind. If any
1115 /// of the attributes are different within the range, the test fails. You
1116 /// can use this to implement, for example, centering button updating. style must have
1117 /// flags indicating which attributes are of interest.
1118 virtual bool HasParagraphAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const;
1119 virtual bool HasParagraphAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const;
1120
1121 /// Clone
1122 virtual wxRichTextObject* Clone() const { return new wxRichTextParagraphLayoutBox(*this); }
1123
1124 /// Insert fragment into this box at the given position. If partialParagraph is true,
1125 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1126 /// marker.
1127 virtual bool InsertFragment(long position, wxRichTextParagraphLayoutBox& fragment);
1128
1129 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1130 virtual bool CopyFragment(const wxRichTextRange& range, wxRichTextParagraphLayoutBox& fragment);
1131
1132 /// Apply the style sheet to the buffer, for example if the styles have changed.
1133 virtual bool ApplyStyleSheet(wxRichTextStyleSheet* styleSheet);
1134
1135 /// Copy
1136 void Copy(const wxRichTextParagraphLayoutBox& obj);
1137
1138 /// Assignment
1139 void operator= (const wxRichTextParagraphLayoutBox& obj) { Copy(obj); }
1140
1141 /// Calculate ranges
1142 virtual void UpdateRanges() { long end; CalculateRange(0, end); }
1143
1144 /// Get all the text
1145 virtual wxString GetText() const;
1146
1147 /// Set default style for new content. Setting it to a default attribute
1148 /// makes new content take on the 'basic' style.
1149 virtual bool SetDefaultStyle(const wxTextAttrEx& style);
1150
1151 /// Get default style
1152 virtual const wxTextAttrEx& GetDefaultStyle() const { return m_defaultAttributes; }
1153
1154 /// Set basic (overall) style
1155 virtual void SetBasicStyle(const wxTextAttrEx& style) { m_attributes = style; }
1156 virtual void SetBasicStyle(const wxRichTextAttr& style) { m_attributes = style; }
1157
1158 /// Get basic (overall) style
1159 virtual const wxTextAttrEx& GetBasicStyle() const { return m_attributes; }
1160
1161 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1162 void Invalidate(const wxRichTextRange& invalidRange = wxRICHTEXT_ALL);
1163
1164 /// Get invalid range, rounding to entire paragraphs if argument is true.
1165 wxRichTextRange GetInvalidRange(bool wholeParagraphs = false) const;
1166
1167 protected:
1168 wxRichTextCtrl* m_ctrl;
1169 wxTextAttrEx m_defaultAttributes;
1170
1171 /// The invalidated range that will need full layout
1172 wxRichTextRange m_invalidRange;
1173
1174 // Is the last paragraph partial or complete?
1175 bool m_partialParagraph;
1176 };
1177
1178 /*!
1179 * wxRichTextLine class declaration
1180 * This object represents a line in a paragraph, and stores
1181 * offsets from the start of the paragraph representing the
1182 * start and end positions of the line.
1183 */
1184
1185 class WXDLLIMPEXP_RICHTEXT wxRichTextLine
1186 {
1187 public:
1188 // Constructors
1189
1190 wxRichTextLine(wxRichTextParagraph* parent);
1191 wxRichTextLine(const wxRichTextLine& obj) { Init( NULL); Copy(obj); }
1192 virtual ~wxRichTextLine() {}
1193
1194 // Overrideables
1195
1196 // Accessors
1197
1198 /// Set the range
1199 void SetRange(const wxRichTextRange& range) { m_range = range; }
1200 void SetRange(long from, long to) { m_range = wxRichTextRange(from, to); }
1201
1202 /// Get the parent paragraph
1203 wxRichTextParagraph* GetParent() { return m_parent; }
1204
1205 /// Get the range
1206 const wxRichTextRange& GetRange() const { return m_range; }
1207 wxRichTextRange& GetRange() { return m_range; }
1208
1209 /// Get the absolute range
1210 wxRichTextRange GetAbsoluteRange() const;
1211
1212 /// Get/set the line size as calculated by Layout.
1213 virtual wxSize GetSize() const { return m_size; }
1214 virtual void SetSize(const wxSize& sz) { m_size = sz; }
1215
1216 /// Get/set the object position relative to the parent
1217 virtual wxPoint GetPosition() const { return m_pos; }
1218 virtual void SetPosition(const wxPoint& pos) { m_pos = pos; }
1219
1220 /// Get the absolute object position
1221 virtual wxPoint GetAbsolutePosition() const;
1222
1223 /// Get the rectangle enclosing the line
1224 virtual wxRect GetRect() const { return wxRect(GetAbsolutePosition(), GetSize()); }
1225
1226 /// Set/get stored descent
1227 void SetDescent(int descent) { m_descent = descent; }
1228 int GetDescent() const { return m_descent; }
1229
1230 // Operations
1231
1232 /// Initialisation
1233 void Init(wxRichTextParagraph* parent);
1234
1235 /// Copy
1236 void Copy(const wxRichTextLine& obj);
1237
1238 /// Clone
1239 virtual wxRichTextLine* Clone() const { return new wxRichTextLine(*this); }
1240
1241 protected:
1242
1243 /// The range of the line (start position to end position)
1244 /// This is relative to the parent paragraph.
1245 wxRichTextRange m_range;
1246
1247 /// Size and position measured relative to top of paragraph
1248 wxPoint m_pos;
1249 wxSize m_size;
1250
1251 /// Maximum descent for this line (location of text baseline)
1252 int m_descent;
1253
1254 // The parent object
1255 wxRichTextParagraph* m_parent;
1256 };
1257
1258 WX_DECLARE_LIST_WITH_DECL( wxRichTextLine, wxRichTextLineList , class WXDLLIMPEXP_RICHTEXT );
1259
1260 /*!
1261 * wxRichTextParagraph class declaration
1262 * This object represents a single paragraph (or in a straight text editor, a line).
1263 */
1264
1265 class WXDLLIMPEXP_RICHTEXT wxRichTextParagraph: public wxRichTextBox
1266 {
1267 DECLARE_DYNAMIC_CLASS(wxRichTextParagraph)
1268 public:
1269 // Constructors
1270
1271 wxRichTextParagraph(wxRichTextObject* parent = NULL, wxTextAttrEx* style = NULL);
1272 wxRichTextParagraph(const wxString& text, wxRichTextObject* parent = NULL, wxTextAttrEx* paraStyle = NULL, wxTextAttrEx* charStyle = NULL);
1273 virtual ~wxRichTextParagraph();
1274 wxRichTextParagraph(const wxRichTextParagraph& obj): wxRichTextBox() { Copy(obj); }
1275
1276 // Overrideables
1277
1278 /// Draw the item
1279 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
1280
1281 /// Lay the item out
1282 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
1283
1284 /// Get/set the object size for the given range. Returns false if the range
1285 /// is invalid for this object.
1286 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const;
1287
1288 /// Finds the absolute position and row height for the given character position
1289 virtual bool FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart);
1290
1291 /// Hit-testing: returns a flag indicating hit test details, plus
1292 /// information about position
1293 virtual int HitTest(wxDC& dc, const wxPoint& pt, long& textPosition);
1294
1295 /// Calculate range
1296 virtual void CalculateRange(long start, long& end);
1297
1298 // Accessors
1299
1300 /// Get the cached lines
1301 wxRichTextLineList& GetLines() { return m_cachedLines; }
1302
1303 // Operations
1304
1305 /// Copy
1306 void Copy(const wxRichTextParagraph& obj);
1307
1308 /// Clone
1309 virtual wxRichTextObject* Clone() const { return new wxRichTextParagraph(*this); }
1310
1311 /// Clear the cached lines
1312 void ClearLines();
1313
1314 // Implementation
1315
1316 /// Apply paragraph styles such as centering to the wrapped lines
1317 virtual void ApplyParagraphStyle(const wxTextAttrEx& attr, const wxRect& rect);
1318
1319 /// Insert text at the given position
1320 virtual bool InsertText(long pos, const wxString& text);
1321
1322 /// Split an object at this position if necessary, and return
1323 /// the previous object, or NULL if inserting at beginning.
1324 virtual wxRichTextObject* SplitAt(long pos, wxRichTextObject** previousObject = NULL);
1325
1326 /// Move content to a list from this point
1327 virtual void MoveToList(wxRichTextObject* obj, wxList& list);
1328
1329 /// Add content back from list
1330 virtual void MoveFromList(wxList& list);
1331
1332 /// Get the plain text searching from the start or end of the range.
1333 /// The resulting string may be shorter than the range given.
1334 bool GetContiguousPlainText(wxString& text, const wxRichTextRange& range, bool fromStart = true);
1335
1336 /// Find a suitable wrap position. wrapPosition is the last position in the line to the left
1337 /// of the split.
1338 bool FindWrapPosition(const wxRichTextRange& range, wxDC& dc, int availableSpace, long& wrapPosition);
1339
1340 /// Find the object at the given position
1341 wxRichTextObject* FindObjectAtPosition(long position);
1342
1343 /// Get the bullet text for this paragraph.
1344 wxString GetBulletText();
1345
1346 /// Allocate or reuse a line object
1347 wxRichTextLine* AllocateLine(int pos);
1348
1349 /// Clear remaining unused line objects, if any
1350 bool ClearUnusedLines(int lineCount);
1351
1352 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
1353 /// retrieve the actual style.
1354 wxTextAttrEx GetCombinedAttributes(const wxTextAttrEx& contentStyle) const;
1355
1356 /// Get combined attributes of the base style and paragraph style.
1357 wxTextAttrEx GetCombinedAttributes() const;
1358
1359 /// Get the first position from pos that has a line break character.
1360 long GetFirstLineBreakPosition(long pos);
1361
1362 /// Create default tabstop array
1363 static void InitDefaultTabs();
1364
1365 /// Clear default tabstop array
1366 static void ClearDefaultTabs();
1367
1368 /// Get default tabstop array
1369 static const wxArrayInt& GetDefaultTabs() { return sm_defaultTabs; }
1370
1371 protected:
1372 /// The lines that make up the wrapped paragraph
1373 wxRichTextLineList m_cachedLines;
1374
1375 /// Default tabstops
1376 static wxArrayInt sm_defaultTabs;
1377 };
1378
1379 /*!
1380 * wxRichTextPlainText class declaration
1381 * This object represents a single piece of text.
1382 */
1383
1384 class WXDLLIMPEXP_RICHTEXT wxRichTextPlainText: public wxRichTextObject
1385 {
1386 DECLARE_DYNAMIC_CLASS(wxRichTextPlainText)
1387 public:
1388 // Constructors
1389
1390 wxRichTextPlainText(const wxString& text = wxEmptyString, wxRichTextObject* parent = NULL, wxTextAttrEx* style = NULL);
1391 wxRichTextPlainText(const wxRichTextPlainText& obj): wxRichTextObject() { Copy(obj); }
1392
1393 // Overrideables
1394
1395 /// Draw the item
1396 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
1397
1398 /// Lay the item out
1399 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
1400
1401 /// Get/set the object size for the given range. Returns false if the range
1402 /// is invalid for this object.
1403 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position/* = wxPoint(0,0)*/) const;
1404
1405 /// Get any text in this object for the given range
1406 virtual wxString GetTextForRange(const wxRichTextRange& range) const;
1407
1408 /// Do a split, returning an object containing the second part, and setting
1409 /// the first part in 'this'.
1410 virtual wxRichTextObject* DoSplit(long pos);
1411
1412 /// Calculate range
1413 virtual void CalculateRange(long start, long& end);
1414
1415 /// Delete range
1416 virtual bool DeleteRange(const wxRichTextRange& range);
1417
1418 /// Returns true if the object is empty
1419 virtual bool IsEmpty() const { return m_text.empty(); }
1420
1421 /// Returns true if this object can merge itself with the given one.
1422 virtual bool CanMerge(wxRichTextObject* object) const;
1423
1424 /// Returns true if this object merged itself with the given one.
1425 /// The calling code will then delete the given object.
1426 virtual bool Merge(wxRichTextObject* object);
1427
1428 /// Dump to output stream for debugging
1429 virtual void Dump(wxTextOutputStream& stream);
1430
1431 /// Get the first position from pos that has a line break character.
1432 long GetFirstLineBreakPosition(long pos);
1433
1434 // Accessors
1435
1436 /// Get the text
1437 const wxString& GetText() const { return m_text; }
1438
1439 /// Set the text
1440 void SetText(const wxString& text) { m_text = text; }
1441
1442 // Operations
1443
1444 /// Copy
1445 void Copy(const wxRichTextPlainText& obj);
1446
1447 /// Clone
1448 virtual wxRichTextObject* Clone() const { return new wxRichTextPlainText(*this); }
1449 private:
1450 bool DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect, wxString& str, wxCoord& x, wxCoord& y, bool selected);
1451
1452 protected:
1453 wxString m_text;
1454 };
1455
1456 /*!
1457 * wxRichTextImageBlock stores information about an image, in binary in-memory form
1458 */
1459
1460 class WXDLLIMPEXP_BASE wxDataInputStream;
1461 class WXDLLIMPEXP_BASE wxDataOutputStream;
1462
1463 class WXDLLIMPEXP_RICHTEXT wxRichTextImageBlock: public wxObject
1464 {
1465 public:
1466 wxRichTextImageBlock();
1467 wxRichTextImageBlock(const wxRichTextImageBlock& block);
1468 virtual ~wxRichTextImageBlock();
1469
1470 void Init();
1471 void Clear();
1472
1473 // Load the original image into a memory block.
1474 // If the image is not a JPEG, we must convert it into a JPEG
1475 // to conserve space.
1476 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
1477 // load the image a 2nd time.
1478 virtual bool MakeImageBlock(const wxString& filename, int imageType, wxImage& image, bool convertToJPEG = true);
1479
1480 // Make an image block from the wxImage in the given
1481 // format.
1482 virtual bool MakeImageBlock(wxImage& image, int imageType, int quality = 80);
1483
1484 // Write to a file
1485 bool Write(const wxString& filename);
1486
1487 // Write data in hex to a stream
1488 bool WriteHex(wxOutputStream& stream);
1489
1490 // Read data in hex from a stream
1491 bool ReadHex(wxInputStream& stream, int length, int imageType);
1492
1493 // Copy from 'block'
1494 void Copy(const wxRichTextImageBlock& block);
1495
1496 // Load a wxImage from the block
1497 bool Load(wxImage& image);
1498
1499 //// Operators
1500 void operator=(const wxRichTextImageBlock& block);
1501
1502 //// Accessors
1503
1504 unsigned char* GetData() const { return m_data; }
1505 size_t GetDataSize() const { return m_dataSize; }
1506 int GetImageType() const { return m_imageType; }
1507
1508 void SetData(unsigned char* image) { m_data = image; }
1509 void SetDataSize(size_t size) { m_dataSize = size; }
1510 void SetImageType(int imageType) { m_imageType = imageType; }
1511
1512 bool Ok() const { return IsOk(); }
1513 bool IsOk() const { return GetData() != NULL; }
1514
1515 // Gets the extension for the block's type
1516 wxString GetExtension() const;
1517
1518 /// Implementation
1519
1520 // Allocate and read from stream as a block of memory
1521 static unsigned char* ReadBlock(wxInputStream& stream, size_t size);
1522 static unsigned char* ReadBlock(const wxString& filename, size_t size);
1523
1524 // Write memory block to stream
1525 static bool WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size);
1526
1527 // Write memory block to file
1528 static bool WriteBlock(const wxString& filename, unsigned char* block, size_t size);
1529
1530 protected:
1531 // Size in bytes of the image stored.
1532 // This is in the raw, original form such as a JPEG file.
1533 unsigned char* m_data;
1534 size_t m_dataSize;
1535 int m_imageType; // wxWin type id
1536 };
1537
1538
1539 /*!
1540 * wxRichTextImage class declaration
1541 * This object represents an image.
1542 */
1543
1544 class WXDLLIMPEXP_RICHTEXT wxRichTextImage: public wxRichTextObject
1545 {
1546 DECLARE_DYNAMIC_CLASS(wxRichTextImage)
1547 public:
1548 // Constructors
1549
1550 wxRichTextImage(wxRichTextObject* parent = NULL): wxRichTextObject(parent) { }
1551 wxRichTextImage(const wxImage& image, wxRichTextObject* parent = NULL, wxTextAttrEx* charStyle = NULL);
1552 wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent = NULL, wxTextAttrEx* charStyle = NULL);
1553 wxRichTextImage(const wxRichTextImage& obj): wxRichTextObject() { Copy(obj); }
1554
1555 // Overrideables
1556
1557 /// Draw the item
1558 virtual bool Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style);
1559
1560 /// Lay the item out
1561 virtual bool Layout(wxDC& dc, const wxRect& rect, int style);
1562
1563 /// Get the object size for the given range. Returns false if the range
1564 /// is invalid for this object.
1565 virtual bool GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position = wxPoint(0,0)) const;
1566
1567 /// Returns true if the object is empty
1568 virtual bool IsEmpty() const { return !m_image.Ok(); }
1569
1570 // Accessors
1571
1572 /// Get the image
1573 const wxImage& GetImage() const { return m_image; }
1574
1575 /// Set the image
1576 void SetImage(const wxImage& image) { m_image = image; }
1577
1578 /// Get the image block containing the raw data
1579 wxRichTextImageBlock& GetImageBlock() { return m_imageBlock; }
1580
1581 // Operations
1582
1583 /// Copy
1584 void Copy(const wxRichTextImage& obj);
1585
1586 /// Clone
1587 virtual wxRichTextObject* Clone() const { return new wxRichTextImage(*this); }
1588
1589 /// Load wxImage from the block
1590 virtual bool LoadFromBlock();
1591
1592 /// Make block from the wxImage
1593 virtual bool MakeBlock();
1594
1595 protected:
1596 // TODO: reduce the multiple representations of data
1597 wxImage m_image;
1598 wxBitmap m_bitmap;
1599 wxRichTextImageBlock m_imageBlock;
1600 };
1601
1602
1603 /*!
1604 * wxRichTextBuffer class declaration
1605 * This is a kind of box, used to represent the whole buffer
1606 */
1607
1608 class WXDLLIMPEXP_RICHTEXT wxRichTextCommand;
1609 class WXDLLIMPEXP_RICHTEXT wxRichTextAction;
1610
1611 class WXDLLIMPEXP_RICHTEXT wxRichTextBuffer: public wxRichTextParagraphLayoutBox
1612 {
1613 DECLARE_DYNAMIC_CLASS(wxRichTextBuffer)
1614 public:
1615 // Constructors
1616
1617 wxRichTextBuffer() { Init(); }
1618 wxRichTextBuffer(const wxRichTextBuffer& obj): wxRichTextParagraphLayoutBox() { Init(); Copy(obj); }
1619 virtual ~wxRichTextBuffer() ;
1620
1621 // Accessors
1622
1623 /// Gets the command processor
1624 wxCommandProcessor* GetCommandProcessor() const { return m_commandProcessor; }
1625
1626 /// Set style sheet, if any.
1627 void SetStyleSheet(wxRichTextStyleSheet* styleSheet) { m_styleSheet = styleSheet; }
1628 virtual wxRichTextStyleSheet* GetStyleSheet() const { return m_styleSheet; }
1629
1630 /// Set style sheet and notify of the change
1631 bool SetStyleSheetAndNotify(wxRichTextStyleSheet* sheet);
1632
1633 /// Push style sheet to top of stack
1634 bool PushStyleSheet(wxRichTextStyleSheet* styleSheet);
1635
1636 /// Pop style sheet from top of stack
1637 wxRichTextStyleSheet* PopStyleSheet();
1638
1639 // Operations
1640
1641 /// Initialisation
1642 void Init();
1643
1644 /// Clears the buffer, adds an empty paragraph, and clears the command processor.
1645 virtual void ResetAndClearCommands();
1646
1647 /// Load a file
1648 virtual bool LoadFile(const wxString& filename, int type = wxRICHTEXT_TYPE_ANY);
1649
1650 /// Save a file
1651 virtual bool SaveFile(const wxString& filename, int type = wxRICHTEXT_TYPE_ANY);
1652
1653 /// Load from a stream
1654 virtual bool LoadFile(wxInputStream& stream, int type = wxRICHTEXT_TYPE_ANY);
1655
1656 /// Save to a stream
1657 virtual bool SaveFile(wxOutputStream& stream, int type = wxRICHTEXT_TYPE_ANY);
1658
1659 /// Set the handler flags, controlling loading and saving
1660 void SetHandlerFlags(int flags) { m_handlerFlags = flags; }
1661
1662 /// Get the handler flags, controlling loading and saving
1663 int GetHandlerFlags() const { return m_handlerFlags; }
1664
1665 /// Convenience function to add a paragraph of text
1666 virtual wxRichTextRange AddParagraph(const wxString& text, wxTextAttrEx* paraStyle = NULL) { Modify(); return wxRichTextParagraphLayoutBox::AddParagraph(text, paraStyle); }
1667
1668 /// Begin collapsing undo/redo commands. Note that this may not work properly
1669 /// if combining commands that delete or insert content, changing ranges for
1670 /// subsequent actions.
1671 virtual bool BeginBatchUndo(const wxString& cmdName);
1672
1673 /// End collapsing undo/redo commands
1674 virtual bool EndBatchUndo();
1675
1676 /// Collapsing commands?
1677 virtual bool BatchingUndo() const { return m_batchedCommandDepth > 0; }
1678
1679 /// Submit immediately, or delay according to whether collapsing is on
1680 virtual bool SubmitAction(wxRichTextAction* action);
1681
1682 /// Get collapsed command
1683 virtual wxRichTextCommand* GetBatchedCommand() const { return m_batchedCommand; }
1684
1685 /// Begin suppressing undo/redo commands. The way undo is suppressed may be implemented
1686 /// differently by each command. If not dealt with by a command implementation, then
1687 /// it will be implemented automatically by not storing the command in the undo history
1688 /// when the action is submitted to the command processor.
1689 virtual bool BeginSuppressUndo();
1690
1691 /// End suppressing undo/redo commands.
1692 virtual bool EndSuppressUndo();
1693
1694 /// Collapsing commands?
1695 virtual bool SuppressingUndo() const { return m_suppressUndo > 0; }
1696
1697 /// Copy the range to the clipboard
1698 virtual bool CopyToClipboard(const wxRichTextRange& range);
1699
1700 /// Paste the clipboard content to the buffer
1701 virtual bool PasteFromClipboard(long position);
1702
1703 /// Can we paste from the clipboard?
1704 virtual bool CanPasteFromClipboard() const;
1705
1706 /// Begin using a style
1707 virtual bool BeginStyle(const wxTextAttrEx& style);
1708
1709 /// End the style
1710 virtual bool EndStyle();
1711
1712 /// End all styles
1713 virtual bool EndAllStyles();
1714
1715 /// Clear the style stack
1716 virtual void ClearStyleStack();
1717
1718 /// Get the size of the style stack, for example to check correct nesting
1719 virtual size_t GetStyleStackSize() const { return m_attributeStack.GetCount(); }
1720
1721 /// Begin using bold
1722 bool BeginBold();
1723
1724 /// End using bold
1725 bool EndBold() { return EndStyle(); }
1726
1727 /// Begin using italic
1728 bool BeginItalic();
1729
1730 /// End using italic
1731 bool EndItalic() { return EndStyle(); }
1732
1733 /// Begin using underline
1734 bool BeginUnderline();
1735
1736 /// End using underline
1737 bool EndUnderline() { return EndStyle(); }
1738
1739 /// Begin using point size
1740 bool BeginFontSize(int pointSize);
1741
1742 /// End using point size
1743 bool EndFontSize() { return EndStyle(); }
1744
1745 /// Begin using this font
1746 bool BeginFont(const wxFont& font);
1747
1748 /// End using a font
1749 bool EndFont() { return EndStyle(); }
1750
1751 /// Begin using this colour
1752 bool BeginTextColour(const wxColour& colour);
1753
1754 /// End using a colour
1755 bool EndTextColour() { return EndStyle(); }
1756
1757 /// Begin using alignment
1758 bool BeginAlignment(wxTextAttrAlignment alignment);
1759
1760 /// End alignment
1761 bool EndAlignment() { return EndStyle(); }
1762
1763 /// Begin left indent
1764 bool BeginLeftIndent(int leftIndent, int leftSubIndent = 0);
1765
1766 /// End left indent
1767 bool EndLeftIndent() { return EndStyle(); }
1768
1769 /// Begin right indent
1770 bool BeginRightIndent(int rightIndent);
1771
1772 /// End right indent
1773 bool EndRightIndent() { return EndStyle(); }
1774
1775 /// Begin paragraph spacing
1776 bool BeginParagraphSpacing(int before, int after);
1777
1778 /// End paragraph spacing
1779 bool EndParagraphSpacing() { return EndStyle(); }
1780
1781 /// Begin line spacing
1782 bool BeginLineSpacing(int lineSpacing);
1783
1784 /// End line spacing
1785 bool EndLineSpacing() { return EndStyle(); }
1786
1787 /// Begin numbered bullet
1788 bool BeginNumberedBullet(int bulletNumber, int leftIndent, int leftSubIndent, int bulletStyle = wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_PERIOD);
1789
1790 /// End numbered bullet
1791 bool EndNumberedBullet() { return EndStyle(); }
1792
1793 /// Begin symbol bullet
1794 bool BeginSymbolBullet(const wxString& symbol, int leftIndent, int leftSubIndent, int bulletStyle = wxTEXT_ATTR_BULLET_STYLE_SYMBOL);
1795
1796 /// End symbol bullet
1797 bool EndSymbolBullet() { return EndStyle(); }
1798
1799 /// Begin standard bullet
1800 bool BeginStandardBullet(const wxString& bulletName, int leftIndent, int leftSubIndent, int bulletStyle = wxTEXT_ATTR_BULLET_STYLE_STANDARD);
1801
1802 /// End standard bullet
1803 bool EndStandardBullet() { return EndStyle(); }
1804
1805 /// Begin named character style
1806 bool BeginCharacterStyle(const wxString& characterStyle);
1807
1808 /// End named character style
1809 bool EndCharacterStyle() { return EndStyle(); }
1810
1811 /// Begin named paragraph style
1812 bool BeginParagraphStyle(const wxString& paragraphStyle);
1813
1814 /// End named character style
1815 bool EndParagraphStyle() { return EndStyle(); }
1816
1817 /// Begin named list style
1818 bool BeginListStyle(const wxString& listStyle, int level = 1, int number = 1);
1819
1820 /// End named character style
1821 bool EndListStyle() { return EndStyle(); }
1822
1823 /// Begin URL
1824 bool BeginURL(const wxString& url, const wxString& characterStyle = wxEmptyString);
1825
1826 /// End URL
1827 bool EndURL() { return EndStyle(); }
1828
1829 // Event handling
1830
1831 /// Add an event handler
1832 bool AddEventHandler(wxEvtHandler* handler);
1833
1834 /// Remove an event handler
1835 bool RemoveEventHandler(wxEvtHandler* handler, bool deleteHandler = false);
1836
1837 /// Clear event handlers
1838 void ClearEventHandlers();
1839
1840 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
1841 /// otherwise will stop at the first successful one.
1842 bool SendEvent(wxEvent& event, bool sendToAll = true);
1843
1844 // Implementation
1845
1846 /// Copy
1847 void Copy(const wxRichTextBuffer& obj);
1848
1849 /// Clone
1850 virtual wxRichTextObject* Clone() const { return new wxRichTextBuffer(*this); }
1851
1852 /// Submit command to insert paragraphs
1853 bool InsertParagraphsWithUndo(long pos, const wxRichTextParagraphLayoutBox& paragraphs, wxRichTextCtrl* ctrl, int flags = 0);
1854
1855 /// Submit command to insert the given text
1856 bool InsertTextWithUndo(long pos, const wxString& text, wxRichTextCtrl* ctrl, int flags = 0);
1857
1858 /// Submit command to insert a newline
1859 bool InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int flags = 0);
1860
1861 /// Submit command to insert the given image
1862 bool InsertImageWithUndo(long pos, const wxRichTextImageBlock& imageBlock, wxRichTextCtrl* ctrl, int flags = 0);
1863
1864 /// Submit command to delete this range
1865 bool DeleteRangeWithUndo(const wxRichTextRange& range, wxRichTextCtrl* ctrl);
1866
1867 /// Mark modified
1868 void Modify(bool modify = true) { m_modified = modify; }
1869 bool IsModified() const { return m_modified; }
1870
1871 /// Get the style that is appropriate for a new paragraph at this position.
1872 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
1873 /// style.
1874 wxRichTextAttr GetStyleForNewParagraph(long pos, bool caretPosition = false, bool lookUpNewParaStyle=false) const;
1875
1876 /// Dumps contents of buffer for debugging purposes
1877 virtual void Dump();
1878 virtual void Dump(wxTextOutputStream& stream) { wxRichTextParagraphLayoutBox::Dump(stream); }
1879
1880 /// Returns the file handlers
1881 static wxList& GetHandlers() { return sm_handlers; }
1882
1883 /// Adds a handler to the end
1884 static void AddHandler(wxRichTextFileHandler *handler);
1885
1886 /// Inserts a handler at the front
1887 static void InsertHandler(wxRichTextFileHandler *handler);
1888
1889 /// Removes a handler
1890 static bool RemoveHandler(const wxString& name);
1891
1892 /// Finds a handler by name
1893 static wxRichTextFileHandler *FindHandler(const wxString& name);
1894
1895 /// Finds a handler by extension and type
1896 static wxRichTextFileHandler *FindHandler(const wxString& extension, int imageType);
1897
1898 /// Finds a handler by filename or, if supplied, type
1899 static wxRichTextFileHandler *FindHandlerFilenameOrType(const wxString& filename, int imageType);
1900
1901 /// Finds a handler by type
1902 static wxRichTextFileHandler *FindHandler(int imageType);
1903
1904 /// Gets a wildcard incorporating all visible handlers. If 'types' is present,
1905 /// will be filled with the file type corresponding to each filter. This can be
1906 /// used to determine the type to pass to LoadFile given a selected filter.
1907 static wxString GetExtWildcard(bool combine = false, bool save = false, wxArrayInt* types = NULL);
1908
1909 /// Clean up handlers
1910 static void CleanUpHandlers();
1911
1912 /// Initialise the standard handlers
1913 static void InitStandardHandlers();
1914
1915 /// Get renderer
1916 static wxRichTextRenderer* GetRenderer() { return sm_renderer; }
1917
1918 /// Set renderer, deleting old one
1919 static void SetRenderer(wxRichTextRenderer* renderer);
1920
1921 /// Minimum margin between bullet and paragraph in 10ths of a mm
1922 static int GetBulletRightMargin() { return sm_bulletRightMargin; }
1923 static void SetBulletRightMargin(int margin) { sm_bulletRightMargin = margin; }
1924
1925 /// Factor to multiply by character height to get a reasonable bullet size
1926 static float GetBulletProportion() { return sm_bulletProportion; }
1927 static void SetBulletProportion(float prop) { sm_bulletProportion = prop; }
1928
1929 /// Scale factor for calculating dimensions
1930 double GetScale() const { return m_scale; }
1931 void SetScale(double scale) { m_scale = scale; }
1932
1933 protected:
1934
1935 /// Command processor
1936 wxCommandProcessor* m_commandProcessor;
1937
1938 /// Has been modified?
1939 bool m_modified;
1940
1941 /// Collapsed command stack
1942 int m_batchedCommandDepth;
1943
1944 /// Name for collapsed command
1945 wxString m_batchedCommandsName;
1946
1947 /// Current collapsed command accumulating actions
1948 wxRichTextCommand* m_batchedCommand;
1949
1950 /// Whether to suppress undo
1951 int m_suppressUndo;
1952
1953 /// Style sheet, if any
1954 wxRichTextStyleSheet* m_styleSheet;
1955
1956 /// List of event handlers that will be notified of events
1957 wxList m_eventHandlers;
1958
1959 /// Stack of attributes for convenience functions
1960 wxList m_attributeStack;
1961
1962 /// Flags to be passed to handlers
1963 int m_handlerFlags;
1964
1965 /// File handlers
1966 static wxList sm_handlers;
1967
1968 /// Renderer
1969 static wxRichTextRenderer* sm_renderer;
1970
1971 /// Minimum margin between bullet and paragraph in 10ths of a mm
1972 static int sm_bulletRightMargin;
1973
1974 /// Factor to multiply by character height to get a reasonable bullet size
1975 static float sm_bulletProportion;
1976
1977 /// Scaling factor in use: needed to calculate correct dimensions when printing
1978 double m_scale;
1979 };
1980
1981 /*!
1982 * The command identifiers
1983 *
1984 */
1985
1986 enum wxRichTextCommandId
1987 {
1988 wxRICHTEXT_INSERT,
1989 wxRICHTEXT_DELETE,
1990 wxRICHTEXT_CHANGE_STYLE
1991 };
1992
1993 /*!
1994 * Command classes for undo/redo
1995 *
1996 */
1997
1998 class WXDLLIMPEXP_RICHTEXT wxRichTextAction;
1999 class WXDLLIMPEXP_RICHTEXT wxRichTextCommand: public wxCommand
2000 {
2001 public:
2002 // Ctor for one action
2003 wxRichTextCommand(const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
2004 wxRichTextCtrl* ctrl, bool ignoreFirstTime = false);
2005
2006 // Ctor for multiple actions
2007 wxRichTextCommand(const wxString& name);
2008
2009 virtual ~wxRichTextCommand();
2010
2011 bool Do();
2012 bool Undo();
2013
2014 void AddAction(wxRichTextAction* action);
2015 void ClearActions();
2016
2017 wxList& GetActions() { return m_actions; }
2018
2019 protected:
2020
2021 wxList m_actions;
2022 };
2023
2024 /*!
2025 * wxRichTextAction class declaration
2026 * There can be more than one action in a command.
2027 */
2028
2029 class WXDLLIMPEXP_RICHTEXT wxRichTextAction: public wxObject
2030 {
2031 public:
2032 wxRichTextAction(wxRichTextCommand* cmd, const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
2033 wxRichTextCtrl* ctrl, bool ignoreFirstTime = false);
2034
2035 virtual ~wxRichTextAction();
2036
2037 bool Do();
2038 bool Undo();
2039
2040 /// Update the control appearance
2041 void UpdateAppearance(long caretPosition, bool sendUpdateEvent = false,
2042 wxArrayInt* optimizationLineCharPositions = NULL, wxArrayInt* optimizationLineYPositions = NULL);
2043
2044 /// Replace the buffer paragraphs with the given fragment.
2045 void ApplyParagraphs(const wxRichTextParagraphLayoutBox& fragment);
2046
2047 /// Get the fragments
2048 wxRichTextParagraphLayoutBox& GetNewParagraphs() { return m_newParagraphs; }
2049 wxRichTextParagraphLayoutBox& GetOldParagraphs() { return m_oldParagraphs; }
2050
2051 /// Set/get the position used for e.g. insertion
2052 void SetPosition(long pos) { m_position = pos; }
2053 long GetPosition() const { return m_position; }
2054
2055 /// Set/get the range for e.g. deletion
2056 void SetRange(const wxRichTextRange& range) { m_range = range; }
2057 const wxRichTextRange& GetRange() const { return m_range; }
2058
2059 /// Get name
2060 const wxString& GetName() const { return m_name; }
2061
2062 protected:
2063 // Action name
2064 wxString m_name;
2065
2066 // Buffer
2067 wxRichTextBuffer* m_buffer;
2068
2069 // Control
2070 wxRichTextCtrl* m_ctrl;
2071
2072 // Stores the new paragraphs
2073 wxRichTextParagraphLayoutBox m_newParagraphs;
2074
2075 // Stores the old paragraphs
2076 wxRichTextParagraphLayoutBox m_oldParagraphs;
2077
2078 // The affected range
2079 wxRichTextRange m_range;
2080
2081 // The insertion point for this command
2082 long m_position;
2083
2084 // Ignore 1st 'Do' operation because we already did it
2085 bool m_ignoreThis;
2086
2087 // The command identifier
2088 wxRichTextCommandId m_cmdId;
2089 };
2090
2091 /*!
2092 * Handler flags
2093 */
2094
2095 // Include style sheet when loading and saving
2096 #define wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET 0x0001
2097
2098 // Save images to memory file system in HTML handler
2099 #define wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY 0x0010
2100
2101 // Save images to files in HTML handler
2102 #define wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_FILES 0x0020
2103
2104 // Save images as inline base64 data in HTML handler
2105 #define wxRICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64 0x0040
2106
2107 // Don't write header and footer (or BODY), so we can include the fragment
2108 // in a larger document
2109 #define wxRICHTEXT_HANDLER_NO_HEADER_FOOTER 0x0080
2110
2111 /*!
2112 * wxRichTextFileHandler
2113 * Base class for file handlers
2114 */
2115
2116 class WXDLLIMPEXP_RICHTEXT wxRichTextFileHandler: public wxObject
2117 {
2118 DECLARE_CLASS(wxRichTextFileHandler)
2119 public:
2120 wxRichTextFileHandler(const wxString& name = wxEmptyString, const wxString& ext = wxEmptyString, int type = 0)
2121 : m_name(name), m_extension(ext), m_type(type), m_flags(0), m_visible(true)
2122 { }
2123
2124 #if wxUSE_STREAMS
2125 bool LoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
2126 { return DoLoadFile(buffer, stream); }
2127 bool SaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
2128 { return DoSaveFile(buffer, stream); }
2129 #endif
2130
2131 bool LoadFile(wxRichTextBuffer *buffer, const wxString& filename);
2132 bool SaveFile(wxRichTextBuffer *buffer, const wxString& filename);
2133
2134 /// Can we handle this filename (if using files)? By default, checks the extension.
2135 virtual bool CanHandle(const wxString& filename) const;
2136
2137 /// Can we save using this handler?
2138 virtual bool CanSave() const { return false; }
2139
2140 /// Can we load using this handler?
2141 virtual bool CanLoad() const { return false; }
2142
2143 /// Should this handler be visible to the user?
2144 virtual bool IsVisible() const { return m_visible; }
2145 virtual void SetVisible(bool visible) { m_visible = visible; }
2146
2147 /// The name of the nandler
2148 void SetName(const wxString& name) { m_name = name; }
2149 wxString GetName() const { return m_name; }
2150
2151 /// The default extension to recognise
2152 void SetExtension(const wxString& ext) { m_extension = ext; }
2153 wxString GetExtension() const { return m_extension; }
2154
2155 /// The handler type
2156 void SetType(int type) { m_type = type; }
2157 int GetType() const { return m_type; }
2158
2159 /// Flags controlling how loading and saving is done
2160 void SetFlags(int flags) { m_flags = flags; }
2161 int GetFlags() const { return m_flags; }
2162
2163 /// Encoding to use when saving a file. If empty, a suitable encoding is chosen
2164 void SetEncoding(const wxString& encoding) { m_encoding = encoding; }
2165 const wxString& GetEncoding() const { return m_encoding; }
2166
2167 protected:
2168
2169 #if wxUSE_STREAMS
2170 virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream) = 0;
2171 virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) = 0;
2172 #endif
2173
2174 wxString m_name;
2175 wxString m_encoding;
2176 wxString m_extension;
2177 int m_type;
2178 int m_flags;
2179 bool m_visible;
2180 };
2181
2182 /*!
2183 * wxRichTextPlainTextHandler
2184 * Plain text handler
2185 */
2186
2187 class WXDLLIMPEXP_RICHTEXT wxRichTextPlainTextHandler: public wxRichTextFileHandler
2188 {
2189 DECLARE_CLASS(wxRichTextPlainTextHandler)
2190 public:
2191 wxRichTextPlainTextHandler(const wxString& name = wxT("Text"), const wxString& ext = wxT("txt"), int type = wxRICHTEXT_TYPE_TEXT)
2192 : wxRichTextFileHandler(name, ext, type)
2193 { }
2194
2195 /// Can we save using this handler?
2196 virtual bool CanSave() const { return true; }
2197
2198 /// Can we load using this handler?
2199 virtual bool CanLoad() const { return true; }
2200
2201 protected:
2202
2203 #if wxUSE_STREAMS
2204 virtual bool DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream);
2205 virtual bool DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream);
2206 #endif
2207
2208 };
2209
2210 #if wxUSE_DATAOBJ
2211
2212 /*!
2213 * The data object for a wxRichTextBuffer
2214 */
2215
2216 class WXDLLIMPEXP_RICHTEXT wxRichTextBufferDataObject: public wxDataObjectSimple
2217 {
2218 public:
2219 // ctor doesn't copy the pointer, so it shouldn't go away while this object
2220 // is alive
2221 wxRichTextBufferDataObject(wxRichTextBuffer* richTextBuffer = (wxRichTextBuffer*) NULL);
2222 virtual ~wxRichTextBufferDataObject();
2223
2224 // after a call to this function, the buffer is owned by the caller and it
2225 // is responsible for deleting it
2226 wxRichTextBuffer* GetRichTextBuffer();
2227
2228 // Returns the id for the new data format
2229 static const wxChar* GetRichTextBufferFormatId() { return ms_richTextBufferFormatId; }
2230
2231 // base class pure virtuals
2232
2233 virtual wxDataFormat GetPreferredFormat(Direction dir) const;
2234 virtual size_t GetDataSize() const;
2235 virtual bool GetDataHere(void *pBuf) const;
2236 virtual bool SetData(size_t len, const void *buf);
2237
2238 // prevent warnings
2239
2240 virtual size_t GetDataSize(const wxDataFormat&) const { return GetDataSize(); }
2241 virtual bool GetDataHere(const wxDataFormat&, void *buf) const { return GetDataHere(buf); }
2242 virtual bool SetData(const wxDataFormat&, size_t len, const void *buf) { return SetData(len, buf); }
2243
2244 private:
2245 wxDataFormat m_formatRichTextBuffer; // our custom format
2246 wxRichTextBuffer* m_richTextBuffer; // our data
2247 static const wxChar* ms_richTextBufferFormatId; // our format id
2248 };
2249
2250 #endif
2251
2252 /*!
2253 * wxRichTextRenderer isolates common drawing functionality
2254 */
2255
2256 class WXDLLIMPEXP_RICHTEXT wxRichTextRenderer: public wxObject
2257 {
2258 public:
2259 wxRichTextRenderer() {}
2260 virtual ~wxRichTextRenderer() {}
2261
2262 /// Draw a standard bullet, as specified by the value of GetBulletName
2263 virtual bool DrawStandardBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect) = 0;
2264
2265 /// Draw a bullet that can be described by text, such as numbered or symbol bullets
2266 virtual bool DrawTextBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect, const wxString& text) = 0;
2267
2268 /// Draw a bitmap bullet, where the bullet bitmap is specified by the value of GetBulletName
2269 virtual bool DrawBitmapBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect) = 0;
2270
2271 /// Enumerate the standard bullet names currently supported
2272 virtual bool EnumerateStandardBulletNames(wxArrayString& bulletNames) = 0;
2273 };
2274
2275 /*!
2276 * wxRichTextStdRenderer: standard renderer
2277 */
2278
2279 class WXDLLIMPEXP_RICHTEXT wxRichTextStdRenderer: public wxRichTextRenderer
2280 {
2281 public:
2282 wxRichTextStdRenderer() {}
2283
2284 /// Draw a standard bullet, as specified by the value of GetBulletName
2285 virtual bool DrawStandardBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect);
2286
2287 /// Draw a bullet that can be described by text, such as numbered or symbol bullets
2288 virtual bool DrawTextBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect, const wxString& text);
2289
2290 /// Draw a bitmap bullet, where the bullet bitmap is specified by the value of GetBulletName
2291 virtual bool DrawBitmapBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect);
2292
2293 /// Enumerate the standard bullet names currently supported
2294 virtual bool EnumerateStandardBulletNames(wxArrayString& bulletNames);
2295 };
2296
2297 /*!
2298 * Utilities
2299 *
2300 */
2301
2302 inline bool wxRichTextHasStyle(int flags, int style)
2303 {
2304 return ((flags & style) == style);
2305 }
2306
2307 /// Compare two attribute objects
2308 WXDLLIMPEXP_RICHTEXT bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2);
2309 WXDLLIMPEXP_RICHTEXT bool wxTextAttrEq(const wxTextAttr& attr1, const wxRichTextAttr& attr2);
2310
2311 /// Compare two attribute objects, but take into account the flags
2312 /// specifying attributes of interest.
2313 WXDLLIMPEXP_RICHTEXT bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2, int flags);
2314 WXDLLIMPEXP_RICHTEXT bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2, int flags);
2315
2316 /// Apply one style to another
2317 WXDLLIMPEXP_RICHTEXT bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxTextAttrEx& style);
2318 WXDLLIMPEXP_RICHTEXT bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxTextAttrEx& style);
2319 WXDLLIMPEXP_RICHTEXT bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style, wxRichTextAttr* compareWith = NULL);
2320 WXDLLIMPEXP_RICHTEXT bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxRichTextAttr& style, wxRichTextAttr* compareWith = NULL);
2321
2322 /// Combine two bitlists
2323 WXDLLIMPEXP_RICHTEXT bool wxRichTextCombineBitlists(int& valueA, int valueB, int& flagsA, int flagsB);
2324
2325 /// Compare two bitlists
2326 WXDLLIMPEXP_RICHTEXT bool wxRichTextBitlistsEqPartial(int valueA, int valueB, int flags);
2327
2328 /// Split into paragraph and character styles
2329 WXDLLIMPEXP_RICHTEXT bool wxRichTextSplitParaCharStyles(const wxTextAttrEx& style, wxTextAttrEx& parStyle, wxTextAttrEx& charStyle);
2330
2331 /// Compare tabs
2332 WXDLLIMPEXP_RICHTEXT bool wxRichTextTabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2);
2333
2334 /// Set the font without changing the font attributes
2335 WXDLLIMPEXP_RICHTEXT void wxSetFontPreservingStyles(wxTextAttr& attr, const wxFont& font);
2336
2337 /// Convert a decimal to Roman numerals
2338 WXDLLIMPEXP_RICHTEXT wxString wxRichTextDecimalToRoman(long n);
2339
2340 WXDLLIMPEXP_RICHTEXT void wxRichTextModuleInit();
2341
2342 #endif
2343 // wxUSE_RICHTEXT
2344
2345 #endif
2346 // _WX_RICHTEXTBUFFER_H_
2347