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