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