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