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