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