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