fixed VC warning about unreachable code
[wxWidgets.git] / src / richtext / richtextbuffer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextbuffer.cpp
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_RICHTEXT
20
21 #include "wx/richtext/richtextbuffer.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/dc.h"
25 #include "wx/intl.h"
26 #include "wx/log.h"
27 #include "wx/dataobj.h"
28 #include "wx/module.h"
29 #endif
30
31 #include "wx/filename.h"
32 #include "wx/clipbrd.h"
33 #include "wx/wfstream.h"
34 #include "wx/mstream.h"
35 #include "wx/sstream.h"
36 #include "wx/textfile.h"
37
38 #include "wx/richtext/richtextctrl.h"
39 #include "wx/richtext/richtextstyles.h"
40
41 #include "wx/listimpl.cpp"
42
43 WX_DEFINE_LIST(wxRichTextObjectList)
44 WX_DEFINE_LIST(wxRichTextLineList)
45
46 /*!
47 * wxRichTextObject
48 * This is the base for drawable objects.
49 */
50
51 IMPLEMENT_CLASS(wxRichTextObject, wxObject)
52
53 wxRichTextObject::wxRichTextObject(wxRichTextObject* parent)
54 {
55 m_dirty = false;
56 m_refCount = 1;
57 m_parent = parent;
58 m_leftMargin = 0;
59 m_rightMargin = 0;
60 m_topMargin = 0;
61 m_bottomMargin = 0;
62 m_descent = 0;
63 }
64
65 wxRichTextObject::~wxRichTextObject()
66 {
67 }
68
69 void wxRichTextObject::Dereference()
70 {
71 m_refCount --;
72 if (m_refCount <= 0)
73 delete this;
74 }
75
76 /// Copy
77 void wxRichTextObject::Copy(const wxRichTextObject& obj)
78 {
79 m_size = obj.m_size;
80 m_pos = obj.m_pos;
81 m_dirty = obj.m_dirty;
82 m_range = obj.m_range;
83 m_attributes = obj.m_attributes;
84 m_descent = obj.m_descent;
85 /*
86 if (!m_attributes.GetFont().Ok())
87 wxLogDebug(wxT("No font!"));
88 if (!obj.m_attributes.GetFont().Ok())
89 wxLogDebug(wxT("Parent has no font!"));
90 */
91 }
92
93 void wxRichTextObject::SetMargins(int margin)
94 {
95 m_leftMargin = m_rightMargin = m_topMargin = m_bottomMargin = margin;
96 }
97
98 void wxRichTextObject::SetMargins(int leftMargin, int rightMargin, int topMargin, int bottomMargin)
99 {
100 m_leftMargin = leftMargin;
101 m_rightMargin = rightMargin;
102 m_topMargin = topMargin;
103 m_bottomMargin = bottomMargin;
104 }
105
106 // Convert units in tends of a millimetre to device units
107 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC& dc, int units)
108 {
109 int ppi = dc.GetPPI().x;
110
111 // There are ppi pixels in 254.1 "1/10 mm"
112
113 double pixels = ((double) units * (double)ppi) / 254.1;
114
115 return (int) pixels;
116 }
117
118 /// Dump to output stream for debugging
119 void wxRichTextObject::Dump(wxTextOutputStream& stream)
120 {
121 stream << GetClassInfo()->GetClassName() << wxT("\n");
122 stream << wxString::Format(wxT("Size: %d,%d. Position: %d,%d, Range: %ld,%ld"), m_size.x, m_size.y, m_pos.x, m_pos.y, m_range.GetStart(), m_range.GetEnd()) << wxT("\n");
123 stream << wxString::Format(wxT("Text colour: %d,%d,%d."), (int) m_attributes.GetTextColour().Red(), (int) m_attributes.GetTextColour().Green(), (int) m_attributes.GetTextColour().Blue()) << wxT("\n");
124 }
125
126
127 /*!
128 * wxRichTextCompositeObject
129 * This is the base for drawable objects.
130 */
131
132 IMPLEMENT_CLASS(wxRichTextCompositeObject, wxRichTextObject)
133
134 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject* parent):
135 wxRichTextObject(parent)
136 {
137 }
138
139 wxRichTextCompositeObject::~wxRichTextCompositeObject()
140 {
141 DeleteChildren();
142 }
143
144 /// Get the nth child
145 wxRichTextObject* wxRichTextCompositeObject::GetChild(size_t n) const
146 {
147 wxASSERT ( n < m_children.GetCount() );
148
149 return m_children.Item(n)->GetData();
150 }
151
152 /// Append a child, returning the position
153 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject* child)
154 {
155 m_children.Append(child);
156 child->SetParent(this);
157 return m_children.GetCount() - 1;
158 }
159
160 /// Insert the child in front of the given object, or at the beginning
161 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject* child, wxRichTextObject* inFrontOf)
162 {
163 if (inFrontOf)
164 {
165 wxRichTextObjectList::compatibility_iterator node = m_children.Find(inFrontOf);
166 m_children.Insert(node, child);
167 }
168 else
169 m_children.Insert(child);
170 child->SetParent(this);
171
172 return true;
173 }
174
175 /// Delete the child
176 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject* child, bool deleteChild)
177 {
178 wxRichTextObjectList::compatibility_iterator node = m_children.Find(child);
179 if (node)
180 {
181 wxRichTextObject* obj = node->GetData();
182 m_children.Erase(node);
183 if (deleteChild)
184 delete obj;
185
186 return true;
187 }
188 return false;
189 }
190
191 /// Delete all children
192 bool wxRichTextCompositeObject::DeleteChildren()
193 {
194 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
195 while (node)
196 {
197 wxRichTextObjectList::compatibility_iterator oldNode = node;
198
199 wxRichTextObject* child = node->GetData();
200 child->Dereference(); // Only delete if reference count is zero
201
202 node = node->GetNext();
203 m_children.Erase(oldNode);
204 }
205
206 return true;
207 }
208
209 /// Get the child count
210 size_t wxRichTextCompositeObject::GetChildCount() const
211 {
212 return m_children.GetCount();
213 }
214
215 /// Copy
216 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject& obj)
217 {
218 wxRichTextObject::Copy(obj);
219
220 DeleteChildren();
221
222 wxRichTextObjectList::compatibility_iterator node = obj.m_children.GetFirst();
223 while (node)
224 {
225 wxRichTextObject* child = node->GetData();
226 wxRichTextObject* newChild = child->Clone();
227 newChild->SetParent(this);
228 m_children.Append(newChild);
229
230 node = node->GetNext();
231 }
232 }
233
234 /// Hit-testing: returns a flag indicating hit test details, plus
235 /// information about position
236 int wxRichTextCompositeObject::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition)
237 {
238 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
239 while (node)
240 {
241 wxRichTextObject* child = node->GetData();
242
243 int ret = child->HitTest(dc, pt, textPosition);
244 if (ret != wxRICHTEXT_HITTEST_NONE)
245 return ret;
246
247 node = node->GetNext();
248 }
249
250 return wxRICHTEXT_HITTEST_NONE;
251 }
252
253 /// Finds the absolute position and row height for the given character position
254 bool wxRichTextCompositeObject::FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart)
255 {
256 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
257 while (node)
258 {
259 wxRichTextObject* child = node->GetData();
260
261 if (child->FindPosition(dc, index, pt, height, forceLineStart))
262 return true;
263
264 node = node->GetNext();
265 }
266
267 return false;
268 }
269
270 /// Calculate range
271 void wxRichTextCompositeObject::CalculateRange(long start, long& end)
272 {
273 long current = start;
274 long lastEnd = current;
275
276 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
277 while (node)
278 {
279 wxRichTextObject* child = node->GetData();
280 long childEnd = 0;
281
282 child->CalculateRange(current, childEnd);
283 lastEnd = childEnd;
284
285 current = childEnd + 1;
286
287 node = node->GetNext();
288 }
289
290 end = lastEnd;
291
292 // An object with no children has zero length
293 if (m_children.GetCount() == 0)
294 end --;
295
296 m_range.SetRange(start, end);
297 }
298
299 /// Delete range from layout.
300 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange& range)
301 {
302 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
303
304 while (node)
305 {
306 wxRichTextObject* obj = (wxRichTextObject*) node->GetData();
307 wxRichTextObjectList::compatibility_iterator next = node->GetNext();
308
309 // Delete the range in each paragraph
310
311 // When a chunk has been deleted, internally the content does not
312 // now match the ranges.
313 // However, so long as deletion is not done on the same object twice this is OK.
314 // If you may delete content from the same object twice, recalculate
315 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
316 // adjust the range you're deleting accordingly.
317
318 if (!obj->GetRange().IsOutside(range))
319 {
320 obj->DeleteRange(range);
321
322 // Delete an empty object, or paragraph within this range.
323 if (obj->IsEmpty() ||
324 (range.GetStart() <= obj->GetRange().GetStart() && range.GetEnd() >= obj->GetRange().GetEnd()))
325 {
326 // An empty paragraph has length 1, so won't be deleted unless the
327 // whole range is deleted.
328 RemoveChild(obj, true);
329 }
330 }
331
332 node = next;
333 }
334
335 return true;
336 }
337
338 /// Get any text in this object for the given range
339 wxString wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange& range) const
340 {
341 wxString text;
342 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
343 while (node)
344 {
345 wxRichTextObject* child = node->GetData();
346 wxRichTextRange childRange = range;
347 if (!child->GetRange().IsOutside(range))
348 {
349 childRange.LimitTo(child->GetRange());
350
351 wxString childText = child->GetTextForRange(childRange);
352
353 text += childText;
354 }
355 node = node->GetNext();
356 }
357
358 return text;
359 }
360
361 /// Recursively merge all pieces that can be merged.
362 bool wxRichTextCompositeObject::Defragment()
363 {
364 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
365 while (node)
366 {
367 wxRichTextObject* child = node->GetData();
368 wxRichTextCompositeObject* composite = wxDynamicCast(child, wxRichTextCompositeObject);
369 if (composite)
370 composite->Defragment();
371
372 if (node->GetNext())
373 {
374 wxRichTextObject* nextChild = node->GetNext()->GetData();
375 if (child->CanMerge(nextChild) && child->Merge(nextChild))
376 {
377 nextChild->Dereference();
378 m_children.Erase(node->GetNext());
379
380 // Don't set node -- we'll see if we can merge again with the next
381 // child.
382 }
383 else
384 node = node->GetNext();
385 }
386 else
387 node = node->GetNext();
388 }
389
390 return true;
391 }
392
393 /// Dump to output stream for debugging
394 void wxRichTextCompositeObject::Dump(wxTextOutputStream& stream)
395 {
396 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
397 while (node)
398 {
399 wxRichTextObject* child = node->GetData();
400 child->Dump(stream);
401 node = node->GetNext();
402 }
403 }
404
405
406 /*!
407 * wxRichTextBox
408 * This defines a 2D space to lay out objects
409 */
410
411 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox, wxRichTextCompositeObject)
412
413 wxRichTextBox::wxRichTextBox(wxRichTextObject* parent):
414 wxRichTextCompositeObject(parent)
415 {
416 }
417
418 /// Draw the item
419 bool wxRichTextBox::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& WXUNUSED(rect), int descent, int style)
420 {
421 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
422 while (node)
423 {
424 wxRichTextObject* child = node->GetData();
425
426 wxRect childRect = wxRect(child->GetPosition(), child->GetCachedSize());
427 child->Draw(dc, range, selectionRange, childRect, descent, style);
428
429 node = node->GetNext();
430 }
431 return true;
432 }
433
434 /// Lay the item out
435 bool wxRichTextBox::Layout(wxDC& dc, const wxRect& rect, int style)
436 {
437 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
438 while (node)
439 {
440 wxRichTextObject* child = node->GetData();
441 child->Layout(dc, rect, style);
442
443 node = node->GetNext();
444 }
445 m_dirty = false;
446 return true;
447 }
448
449 /// Get/set the size for the given range. Assume only has one child.
450 bool wxRichTextBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const
451 {
452 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
453 if (node)
454 {
455 wxRichTextObject* child = node->GetData();
456 return child->GetRangeSize(range, size, descent, dc, flags, position);
457 }
458 else
459 return false;
460 }
461
462 /// Copy
463 void wxRichTextBox::Copy(const wxRichTextBox& obj)
464 {
465 wxRichTextCompositeObject::Copy(obj);
466 }
467
468
469 /*!
470 * wxRichTextParagraphLayoutBox
471 * This box knows how to lay out paragraphs.
472 */
473
474 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox, wxRichTextBox)
475
476 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject* parent):
477 wxRichTextBox(parent)
478 {
479 Init();
480 }
481
482 /// Initialize the object.
483 void wxRichTextParagraphLayoutBox::Init()
484 {
485 m_ctrl = NULL;
486
487 // For now, assume is the only box and has no initial size.
488 m_range = wxRichTextRange(0, -1);
489
490 m_invalidRange.SetRange(-1, -1);
491 m_leftMargin = 4;
492 m_rightMargin = 4;
493 m_topMargin = 4;
494 m_bottomMargin = 4;
495 m_partialParagraph = false;
496 }
497
498 /// Draw the item
499 bool wxRichTextParagraphLayoutBox::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style)
500 {
501 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
502 while (node)
503 {
504 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
505 wxASSERT (child != NULL);
506
507 if (child && !child->GetRange().IsOutside(range))
508 {
509 wxRect childRect(child->GetPosition(), child->GetCachedSize());
510
511 if (childRect.GetTop() > rect.GetBottom() || childRect.GetBottom() < rect.GetTop())
512 {
513 // Skip
514 }
515 else
516 child->Draw(dc, child->GetRange(), selectionRange, childRect, descent, style);
517 }
518
519 node = node->GetNext();
520 }
521 return true;
522 }
523
524 /// Lay the item out
525 bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int style)
526 {
527 wxRect availableSpace;
528 bool formatRect = (style & wxRICHTEXT_LAYOUT_SPECIFIED_RECT) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
529
530 // If only laying out a specific area, the passed rect has a different meaning:
531 // the visible part of the buffer.
532 if (formatRect)
533 {
534 availableSpace = wxRect(0 + m_leftMargin,
535 0 + m_topMargin,
536 rect.width - m_leftMargin - m_rightMargin,
537 rect.height);
538
539 // Invalidate the part of the buffer from the first visible line
540 // to the end. If other parts of the buffer are currently invalid,
541 // then they too will be taken into account if they are above
542 // the visible point.
543 long startPos = 0;
544 wxRichTextLine* line = GetLineAtYPosition(rect.y);
545 if (line)
546 startPos = line->GetAbsoluteRange().GetStart();
547
548 Invalidate(wxRichTextRange(startPos, GetRange().GetEnd()));
549 }
550 else
551 availableSpace = wxRect(rect.x + m_leftMargin,
552 rect.y + m_topMargin,
553 rect.width - m_leftMargin - m_rightMargin,
554 rect.height - m_topMargin - m_bottomMargin);
555
556 int maxWidth = 0;
557
558 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
559
560 bool layoutAll = true;
561
562 // Get invalid range, rounding to paragraph start/end.
563 wxRichTextRange invalidRange = GetInvalidRange(true);
564
565 if (invalidRange == wxRICHTEXT_NONE && !formatRect)
566 return true;
567
568 if (invalidRange == wxRICHTEXT_ALL)
569 layoutAll = true;
570 else // If we know what range is affected, start laying out from that point on.
571 if (invalidRange.GetStart() > GetRange().GetStart())
572 {
573 wxRichTextParagraph* firstParagraph = GetParagraphAtPosition(invalidRange.GetStart());
574 if (firstParagraph)
575 {
576 wxRichTextObjectList::compatibility_iterator firstNode = m_children.Find(firstParagraph);
577 wxRichTextObjectList::compatibility_iterator previousNode;
578 if ( firstNode )
579 previousNode = firstNode->GetPrevious();
580 if (firstNode && previousNode)
581 {
582 wxRichTextParagraph* previousParagraph = wxDynamicCast(previousNode->GetData(), wxRichTextParagraph);
583 availableSpace.y = previousParagraph->GetPosition().y + previousParagraph->GetCachedSize().y;
584
585 // Now we're going to start iterating from the first affected paragraph.
586 node = firstNode;
587
588 layoutAll = false;
589 }
590 }
591 }
592
593 // A way to force speedy rest-of-buffer layout (the 'else' below)
594 bool forceQuickLayout = false;
595
596 while (node)
597 {
598 // Assume this box only contains paragraphs
599
600 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
601 wxCHECK_MSG( child, false, _T("Unknown object in layout") );
602
603 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
604 if ( !forceQuickLayout &&
605 (layoutAll ||
606 child->GetLines().IsEmpty() ||
607 !child->GetRange().IsOutside(invalidRange)) )
608 {
609 child->Layout(dc, availableSpace, style);
610
611 // Layout must set the cached size
612 availableSpace.y += child->GetCachedSize().y;
613 maxWidth = wxMax(maxWidth, child->GetCachedSize().x);
614
615 // If we're just formatting the visible part of the buffer,
616 // and we're now past the bottom of the window, start quick
617 // layout.
618 if (formatRect && child->GetPosition().y > rect.GetBottom())
619 forceQuickLayout = true;
620 }
621 else
622 {
623 // We're outside the immediately affected range, so now let's just
624 // move everything up or down. This assumes that all the children have previously
625 // been laid out and have wrapped line lists associated with them.
626 // TODO: check all paragraphs before the affected range.
627
628 int inc = availableSpace.y - child->GetPosition().y;
629
630 while (node)
631 {
632 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
633 if (child)
634 {
635 if (child->GetLines().GetCount() == 0)
636 child->Layout(dc, availableSpace, style);
637 else
638 child->SetPosition(wxPoint(child->GetPosition().x, child->GetPosition().y + inc));
639
640 availableSpace.y += child->GetCachedSize().y;
641 maxWidth = wxMax(maxWidth, child->GetCachedSize().x);
642 }
643
644 node = node->GetNext();
645 }
646 break;
647 }
648
649 node = node->GetNext();
650 }
651
652 SetCachedSize(wxSize(maxWidth, availableSpace.y));
653
654 m_dirty = false;
655 m_invalidRange = wxRICHTEXT_NONE;
656
657 return true;
658 }
659
660 /// Copy
661 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox& obj)
662 {
663 wxRichTextBox::Copy(obj);
664
665 m_partialParagraph = obj.m_partialParagraph;
666 }
667
668 /// Get/set the size for the given range.
669 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const
670 {
671 wxSize sz;
672
673 wxRichTextObjectList::compatibility_iterator startPara = wxRichTextObjectList::compatibility_iterator();
674 wxRichTextObjectList::compatibility_iterator endPara = wxRichTextObjectList::compatibility_iterator();
675
676 // First find the first paragraph whose starting position is within the range.
677 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
678 while (node)
679 {
680 // child is a paragraph
681 wxRichTextObject* child = node->GetData();
682 const wxRichTextRange& r = child->GetRange();
683
684 if (r.GetStart() <= range.GetStart() && r.GetEnd() >= range.GetStart())
685 {
686 startPara = node;
687 break;
688 }
689
690 node = node->GetNext();
691 }
692
693 // Next find the last paragraph containing part of the range
694 node = m_children.GetFirst();
695 while (node)
696 {
697 // child is a paragraph
698 wxRichTextObject* child = node->GetData();
699 const wxRichTextRange& r = child->GetRange();
700
701 if (r.GetStart() <= range.GetEnd() && r.GetEnd() >= range.GetEnd())
702 {
703 endPara = node;
704 break;
705 }
706
707 node = node->GetNext();
708 }
709
710 if (!startPara || !endPara)
711 return false;
712
713 // Now we can add up the sizes
714 for (node = startPara; node ; node = node->GetNext())
715 {
716 // child is a paragraph
717 wxRichTextObject* child = node->GetData();
718 const wxRichTextRange& childRange = child->GetRange();
719 wxRichTextRange rangeToFind = range;
720 rangeToFind.LimitTo(childRange);
721
722 wxSize childSize;
723
724 int childDescent = 0;
725 child->GetRangeSize(rangeToFind, childSize, childDescent, dc, flags, position);
726
727 descent = wxMax(childDescent, descent);
728
729 sz.x = wxMax(sz.x, childSize.x);
730 sz.y += childSize.y;
731
732 if (node == endPara)
733 break;
734 }
735
736 size = sz;
737
738 return true;
739 }
740
741 /// Get the paragraph at the given position
742 wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos, bool caretPosition) const
743 {
744 if (caretPosition)
745 pos ++;
746
747 // First find the first paragraph whose starting position is within the range.
748 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
749 while (node)
750 {
751 // child is a paragraph
752 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
753 wxASSERT (child != NULL);
754
755 // Return first child in buffer if position is -1
756 // if (pos == -1)
757 // return child;
758
759 if (child->GetRange().Contains(pos))
760 return child;
761
762 node = node->GetNext();
763 }
764 return NULL;
765 }
766
767 /// Get the line at the given position
768 wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos, bool caretPosition) const
769 {
770 if (caretPosition)
771 pos ++;
772
773 // First find the first paragraph whose starting position is within the range.
774 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
775 while (node)
776 {
777 // child is a paragraph
778 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
779 wxASSERT (child != NULL);
780
781 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
782 while (node2)
783 {
784 wxRichTextLine* line = node2->GetData();
785
786 wxRichTextRange range = line->GetAbsoluteRange();
787
788 if (range.Contains(pos) ||
789
790 // If the position is end-of-paragraph, then return the last line of
791 // of the paragraph.
792 (range.GetEnd() == child->GetRange().GetEnd()-1) && (pos == child->GetRange().GetEnd()))
793 return line;
794
795 node2 = node2->GetNext();
796 }
797
798 node = node->GetNext();
799 }
800
801 int lineCount = GetLineCount();
802 if (lineCount > 0)
803 return GetLineForVisibleLineNumber(lineCount-1);
804 else
805 return NULL;
806 }
807
808 /// Get the line at the given y pixel position, or the last line.
809 wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y) const
810 {
811 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
812 while (node)
813 {
814 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
815 wxASSERT (child != NULL);
816
817 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
818 while (node2)
819 {
820 wxRichTextLine* line = node2->GetData();
821
822 wxRect rect(line->GetRect());
823
824 if (y <= rect.GetBottom())
825 return line;
826
827 node2 = node2->GetNext();
828 }
829
830 node = node->GetNext();
831 }
832
833 // Return last line
834 int lineCount = GetLineCount();
835 if (lineCount > 0)
836 return GetLineForVisibleLineNumber(lineCount-1);
837 else
838 return NULL;
839 }
840
841 /// Get the number of visible lines
842 int wxRichTextParagraphLayoutBox::GetLineCount() const
843 {
844 int count = 0;
845
846 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
847 while (node)
848 {
849 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
850 wxASSERT (child != NULL);
851
852 count += child->GetLines().GetCount();
853 node = node->GetNext();
854 }
855 return count;
856 }
857
858
859 /// Get the paragraph for a given line
860 wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine* line) const
861 {
862 return GetParagraphAtPosition(line->GetAbsoluteRange().GetStart());
863 }
864
865 /// Get the line size at the given position
866 wxSize wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos, bool caretPosition) const
867 {
868 wxRichTextLine* line = GetLineAtPosition(pos, caretPosition);
869 if (line)
870 {
871 return line->GetSize();
872 }
873 else
874 return wxSize(0, 0);
875 }
876
877
878 /// Convenience function to add a paragraph of text
879 wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraph(const wxString& text, wxTextAttrEx* paraStyle)
880 {
881 #if wxRICHTEXT_USE_DYNAMIC_STYLES
882 // Don't use the base style, just the default style, and the base style will
883 // be combined at display time
884 wxTextAttrEx style(GetDefaultStyle());
885 #else
886 wxTextAttrEx style(GetAttributes());
887
888 // Apply default style. If the style has no attributes set,
889 // then the attributes will remain the 'basic style' (i.e. the
890 // layout box's style).
891 wxRichTextApplyStyle(style, GetDefaultStyle());
892 #endif
893 wxRichTextParagraph* para = new wxRichTextParagraph(text, this, & style);
894 if (paraStyle)
895 para->SetAttributes(*paraStyle);
896
897 AppendChild(para);
898
899 UpdateRanges();
900 SetDirty(true);
901
902 return para->GetRange();
903 }
904
905 /// Adds multiple paragraphs, based on newlines.
906 wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text, wxTextAttrEx* paraStyle)
907 {
908 #if wxRICHTEXT_USE_DYNAMIC_STYLES
909 // Don't use the base style, just the default style, and the base style will
910 // be combined at display time
911 wxTextAttrEx style(GetDefaultStyle());
912 #else
913 wxTextAttrEx style(GetAttributes());
914
915 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
916 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
917
918 // Apply default style. If the style has no attributes set,
919 // then the attributes will remain the 'basic style' (i.e. the
920 // layout box's style).
921 wxRichTextApplyStyle(style, GetDefaultStyle());
922
923 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
924 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
925 #endif
926
927 wxRichTextParagraph* firstPara = NULL;
928 wxRichTextParagraph* lastPara = NULL;
929
930 wxRichTextRange range(-1, -1);
931
932 size_t i = 0;
933 size_t len = text.length();
934 wxString line;
935 wxRichTextParagraph* para = new wxRichTextParagraph(wxT(""), this, & style);
936 if (paraStyle)
937 para->SetAttributes(*paraStyle);
938
939 AppendChild(para);
940
941 firstPara = para;
942 lastPara = para;
943
944 while (i < len)
945 {
946 wxChar ch = text[i];
947 if (ch == wxT('\n') || ch == wxT('\r'))
948 {
949 wxRichTextPlainText* plainText = (wxRichTextPlainText*) para->GetChildren().GetFirst()->GetData();
950 plainText->SetText(line);
951
952 para = new wxRichTextParagraph(wxT(""), this, & style);
953 if (paraStyle)
954 para->SetAttributes(*paraStyle);
955
956 AppendChild(para);
957
958 //if (!firstPara)
959 // firstPara = para;
960
961 lastPara = para;
962 line = wxEmptyString;
963 }
964 else
965 line += ch;
966
967 i ++;
968 }
969
970 if (!line.empty())
971 {
972 wxRichTextPlainText* plainText = (wxRichTextPlainText*) para->GetChildren().GetFirst()->GetData();
973 plainText->SetText(line);
974 }
975
976 /*
977 if (firstPara)
978 range.SetStart(firstPara->GetRange().GetStart());
979 else if (lastPara)
980 range.SetStart(lastPara->GetRange().GetStart());
981
982 if (lastPara)
983 range.SetEnd(lastPara->GetRange().GetEnd());
984 else if (firstPara)
985 range.SetEnd(firstPara->GetRange().GetEnd());
986 */
987
988 UpdateRanges();
989
990 SetDirty(false);
991
992 return wxRichTextRange(firstPara->GetRange().GetStart(), lastPara->GetRange().GetEnd());
993 }
994
995 /// Convenience function to add an image
996 wxRichTextRange wxRichTextParagraphLayoutBox::AddImage(const wxImage& image, wxTextAttrEx* paraStyle)
997 {
998 #if wxRICHTEXT_USE_DYNAMIC_STYLES
999 // Don't use the base style, just the default style, and the base style will
1000 // be combined at display time
1001 wxTextAttrEx style(GetDefaultStyle());
1002 #else
1003 wxTextAttrEx style(GetAttributes());
1004
1005 // Apply default style. If the style has no attributes set,
1006 // then the attributes will remain the 'basic style' (i.e. the
1007 // layout box's style).
1008 wxRichTextApplyStyle(style, GetDefaultStyle());
1009 #endif
1010
1011 wxRichTextParagraph* para = new wxRichTextParagraph(this, & style);
1012 AppendChild(para);
1013 para->AppendChild(new wxRichTextImage(image, this));
1014
1015 if (paraStyle)
1016 para->SetAttributes(*paraStyle);
1017
1018 UpdateRanges();
1019 SetDirty(true);
1020
1021 return para->GetRange();
1022 }
1023
1024
1025 /// Insert fragment into this box at the given position. If partialParagraph is true,
1026 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1027 /// marker.
1028 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
1029 /// to the data (if it has a default style, anyway).
1030
1031 bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParagraphLayoutBox& fragment)
1032 {
1033 SetDirty(true);
1034
1035 // First, find the first paragraph whose starting position is within the range.
1036 wxRichTextParagraph* para = GetParagraphAtPosition(position);
1037 if (para)
1038 {
1039 wxRichTextObjectList::compatibility_iterator node = m_children.Find(para);
1040
1041 // Now split at this position, returning the object to insert the new
1042 // ones in front of.
1043 wxRichTextObject* nextObject = para->SplitAt(position);
1044
1045 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1046 // text, for example, so let's optimize.
1047
1048 if (fragment.GetPartialParagraph() && fragment.GetChildren().GetCount() == 1)
1049 {
1050 // Add the first para to this para...
1051 wxRichTextObjectList::compatibility_iterator firstParaNode = fragment.GetChildren().GetFirst();
1052 if (!firstParaNode)
1053 return false;
1054
1055 // Iterate through the fragment paragraph inserting the content into this paragraph.
1056 wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph);
1057 wxASSERT (firstPara != NULL);
1058
1059 wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst();
1060 while (objectNode)
1061 {
1062 wxRichTextObject* newObj = objectNode->GetData()->Clone();
1063
1064 if (!nextObject)
1065 {
1066 // Append
1067 para->AppendChild(newObj);
1068 }
1069 else
1070 {
1071 // Insert before nextObject
1072 para->InsertChild(newObj, nextObject);
1073 }
1074
1075 objectNode = objectNode->GetNext();
1076 }
1077
1078 return true;
1079 }
1080 else
1081 {
1082 // Procedure for inserting a fragment consisting of a number of
1083 // paragraphs:
1084 //
1085 // 1. Remove and save the content that's after the insertion point, for adding
1086 // back once we've added the fragment.
1087 // 2. Add the content from the first fragment paragraph to the current
1088 // paragraph.
1089 // 3. Add remaining fragment paragraphs after the current paragraph.
1090 // 4. Add back the saved content from the first paragraph. If partialParagraph
1091 // is true, add it to the last paragraph added and not a new one.
1092
1093 // 1. Remove and save objects after split point.
1094 wxList savedObjects;
1095 if (nextObject)
1096 para->MoveToList(nextObject, savedObjects);
1097
1098 // 2. Add the content from the 1st fragment paragraph.
1099 wxRichTextObjectList::compatibility_iterator firstParaNode = fragment.GetChildren().GetFirst();
1100 if (!firstParaNode)
1101 return false;
1102
1103 wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph);
1104 wxASSERT(firstPara != NULL);
1105
1106 wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst();
1107 while (objectNode)
1108 {
1109 wxRichTextObject* newObj = objectNode->GetData()->Clone();
1110
1111 // Append
1112 para->AppendChild(newObj);
1113
1114 objectNode = objectNode->GetNext();
1115 }
1116
1117 // 3. Add remaining fragment paragraphs after the current paragraph.
1118 wxRichTextObjectList::compatibility_iterator nextParagraphNode = node->GetNext();
1119 wxRichTextObject* nextParagraph = NULL;
1120 if (nextParagraphNode)
1121 nextParagraph = nextParagraphNode->GetData();
1122
1123 wxRichTextObjectList::compatibility_iterator i = fragment.GetChildren().GetFirst()->GetNext();
1124 wxRichTextParagraph* finalPara = para;
1125
1126 // If there was only one paragraph, we need to insert a new one.
1127 if (!i)
1128 {
1129 finalPara = new wxRichTextParagraph;
1130
1131 // TODO: These attributes should come from the subsequent paragraph
1132 // when originally deleted, since the subsequent para takes on
1133 // the previous para's attributes.
1134 finalPara->SetAttributes(firstPara->GetAttributes());
1135
1136 if (nextParagraph)
1137 InsertChild(finalPara, nextParagraph);
1138 else
1139 AppendChild(finalPara);
1140 }
1141 else while (i)
1142 {
1143 wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph);
1144 wxASSERT( para != NULL );
1145
1146 finalPara = (wxRichTextParagraph*) para->Clone();
1147
1148 if (nextParagraph)
1149 InsertChild(finalPara, nextParagraph);
1150 else
1151 AppendChild(finalPara);
1152
1153 i = i->GetNext();
1154 }
1155
1156 // 4. Add back the remaining content.
1157 if (finalPara)
1158 {
1159 finalPara->MoveFromList(savedObjects);
1160
1161 // Ensure there's at least one object
1162 if (finalPara->GetChildCount() == 0)
1163 {
1164 wxRichTextPlainText* text = new wxRichTextPlainText(wxEmptyString);
1165 #if !wxRICHTEXT_USE_DYNAMIC_STYLES
1166 text->SetAttributes(finalPara->GetAttributes());
1167 #endif
1168
1169 finalPara->AppendChild(text);
1170 }
1171 }
1172
1173 return true;
1174 }
1175 }
1176 else
1177 {
1178 // Append
1179 wxRichTextObjectList::compatibility_iterator i = fragment.GetChildren().GetFirst();
1180 while (i)
1181 {
1182 wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph);
1183 wxASSERT( para != NULL );
1184
1185 AppendChild(para->Clone());
1186
1187 i = i->GetNext();
1188 }
1189
1190 return true;
1191 }
1192 }
1193
1194 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1195 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1196 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange& range, wxRichTextParagraphLayoutBox& fragment)
1197 {
1198 wxRichTextObjectList::compatibility_iterator i = GetChildren().GetFirst();
1199 while (i)
1200 {
1201 wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph);
1202 wxASSERT( para != NULL );
1203
1204 if (!para->GetRange().IsOutside(range))
1205 {
1206 fragment.AppendChild(para->Clone());
1207 }
1208 i = i->GetNext();
1209 }
1210
1211 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1212 if (!fragment.IsEmpty())
1213 {
1214 wxRichTextRange topTailRange(range);
1215
1216 wxRichTextParagraph* firstPara = wxDynamicCast(fragment.GetChildren().GetFirst()->GetData(), wxRichTextParagraph);
1217 wxASSERT( firstPara != NULL );
1218
1219 // Chop off the start of the paragraph
1220 if (topTailRange.GetStart() > firstPara->GetRange().GetStart())
1221 {
1222 wxRichTextRange r(firstPara->GetRange().GetStart(), topTailRange.GetStart()-1);
1223 firstPara->DeleteRange(r);
1224
1225 // Make sure the numbering is correct
1226 long end;
1227 fragment.CalculateRange(firstPara->GetRange().GetStart(), end);
1228
1229 // Now, we've deleted some positions, so adjust the range
1230 // accordingly.
1231 topTailRange.SetEnd(topTailRange.GetEnd() - r.GetLength());
1232 }
1233
1234 wxRichTextParagraph* lastPara = wxDynamicCast(fragment.GetChildren().GetLast()->GetData(), wxRichTextParagraph);
1235 wxASSERT( lastPara != NULL );
1236
1237 if (topTailRange.GetEnd() < (lastPara->GetRange().GetEnd()-1))
1238 {
1239 wxRichTextRange r(topTailRange.GetEnd()+1, lastPara->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1240 lastPara->DeleteRange(r);
1241
1242 // Make sure the numbering is correct
1243 long end;
1244 fragment.CalculateRange(firstPara->GetRange().GetStart(), end);
1245
1246 // We only have part of a paragraph at the end
1247 fragment.SetPartialParagraph(true);
1248 }
1249 else
1250 {
1251 if (topTailRange.GetEnd() == (lastPara->GetRange().GetEnd() - 1))
1252 // We have a partial paragraph (don't save last new paragraph marker)
1253 fragment.SetPartialParagraph(true);
1254 else
1255 // We have a complete paragraph
1256 fragment.SetPartialParagraph(false);
1257 }
1258 }
1259
1260 return true;
1261 }
1262
1263 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1264 /// starting from zero at the start of the buffer.
1265 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos, bool caretPosition, bool startOfLine) const
1266 {
1267 if (caretPosition)
1268 pos ++;
1269
1270 int lineCount = 0;
1271
1272 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1273 while (node)
1274 {
1275 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1276 wxASSERT( child != NULL );
1277
1278 if (child->GetRange().Contains(pos))
1279 {
1280 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
1281 while (node2)
1282 {
1283 wxRichTextLine* line = node2->GetData();
1284 wxRichTextRange lineRange = line->GetAbsoluteRange();
1285
1286 if (lineRange.Contains(pos))
1287 {
1288 // If the caret is displayed at the end of the previous wrapped line,
1289 // we want to return the line it's _displayed_ at (not the actual line
1290 // containing the position).
1291 if (lineRange.GetStart() == pos && !startOfLine && child->GetRange().GetStart() != pos)
1292 return lineCount - 1;
1293 else
1294 return lineCount;
1295 }
1296
1297 lineCount ++;
1298
1299 node2 = node2->GetNext();
1300 }
1301 // If we didn't find it in the lines, it must be
1302 // the last position of the paragraph. So return the last line.
1303 return lineCount-1;
1304 }
1305 else
1306 lineCount += child->GetLines().GetCount();
1307
1308 node = node->GetNext();
1309 }
1310
1311 // Not found
1312 return -1;
1313 }
1314
1315 /// Given a line number, get the corresponding wxRichTextLine object.
1316 wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber) const
1317 {
1318 int lineCount = 0;
1319
1320 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1321 while (node)
1322 {
1323 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1324 wxASSERT(child != NULL);
1325
1326 if (lineNumber < (int) (child->GetLines().GetCount() + lineCount))
1327 {
1328 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
1329 while (node2)
1330 {
1331 wxRichTextLine* line = node2->GetData();
1332
1333 if (lineCount == lineNumber)
1334 return line;
1335
1336 lineCount ++;
1337
1338 node2 = node2->GetNext();
1339 }
1340 }
1341 else
1342 lineCount += child->GetLines().GetCount();
1343
1344 node = node->GetNext();
1345 }
1346
1347 // Didn't find it
1348 return NULL;
1349 }
1350
1351 /// Delete range from layout.
1352 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange& range)
1353 {
1354 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1355
1356 while (node)
1357 {
1358 wxRichTextParagraph* obj = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1359 wxASSERT (obj != NULL);
1360
1361 wxRichTextObjectList::compatibility_iterator next = node->GetNext();
1362
1363 // Delete the range in each paragraph
1364
1365 if (!obj->GetRange().IsOutside(range))
1366 {
1367 // Deletes the content of this object within the given range
1368 obj->DeleteRange(range);
1369
1370 // If the whole paragraph is within the range to delete,
1371 // delete the whole thing.
1372 if (range.GetStart() <= obj->GetRange().GetStart() && range.GetEnd() >= obj->GetRange().GetEnd())
1373 {
1374 // Delete the whole object
1375 RemoveChild(obj, true);
1376 }
1377 // If the range includes the paragraph end, we need to join this
1378 // and the next paragraph.
1379 else if (range.Contains(obj->GetRange().GetEnd()))
1380 {
1381 // We need to move the objects from the next paragraph
1382 // to this paragraph
1383
1384 if (next)
1385 {
1386 wxRichTextParagraph* nextParagraph = wxDynamicCast(next->GetData(), wxRichTextParagraph);
1387 next = next->GetNext();
1388 if (nextParagraph)
1389 {
1390 // Delete the stuff we need to delete
1391 nextParagraph->DeleteRange(range);
1392
1393 // Move the objects to the previous para
1394 wxRichTextObjectList::compatibility_iterator node1 = nextParagraph->GetChildren().GetFirst();
1395
1396 while (node1)
1397 {
1398 wxRichTextObject* obj1 = node1->GetData();
1399
1400 // If the object is empty, optimise it out
1401 if (obj1->IsEmpty())
1402 {
1403 delete obj1;
1404 }
1405 else
1406 {
1407 obj->AppendChild(obj1);
1408 }
1409
1410 wxRichTextObjectList::compatibility_iterator next1 = node1->GetNext();
1411 nextParagraph->GetChildren().Erase(node1);
1412
1413 node1 = next1;
1414 }
1415
1416 // Delete the paragraph
1417 RemoveChild(nextParagraph, true);
1418
1419 }
1420 }
1421
1422 }
1423 }
1424
1425 node = next;
1426 }
1427
1428 return true;
1429 }
1430
1431 /// Get any text in this object for the given range
1432 wxString wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange& range) const
1433 {
1434 int lineCount = 0;
1435 wxString text;
1436 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1437 while (node)
1438 {
1439 wxRichTextObject* child = node->GetData();
1440 if (!child->GetRange().IsOutside(range))
1441 {
1442 // if (lineCount > 0)
1443 // text += wxT("\n");
1444 wxRichTextRange childRange = range;
1445 childRange.LimitTo(child->GetRange());
1446
1447 wxString childText = child->GetTextForRange(childRange);
1448
1449 text += childText;
1450
1451 if (childRange.GetEnd() == child->GetRange().GetEnd())
1452 text += wxT("\n");
1453
1454 lineCount ++;
1455 }
1456 node = node->GetNext();
1457 }
1458
1459 return text;
1460 }
1461
1462 /// Get all the text
1463 wxString wxRichTextParagraphLayoutBox::GetText() const
1464 {
1465 return GetTextForRange(GetRange());
1466 }
1467
1468 /// Get the paragraph by number
1469 wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber) const
1470 {
1471 if ((size_t) paragraphNumber >= GetChildCount())
1472 return NULL;
1473
1474 return (wxRichTextParagraph*) GetChild((size_t) paragraphNumber);
1475 }
1476
1477 /// Get the length of the paragraph
1478 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber) const
1479 {
1480 wxRichTextParagraph* para = GetParagraphAtLine(paragraphNumber);
1481 if (para)
1482 return para->GetRange().GetLength() - 1; // don't include newline
1483 else
1484 return 0;
1485 }
1486
1487 /// Get the text of the paragraph
1488 wxString wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber) const
1489 {
1490 wxRichTextParagraph* para = GetParagraphAtLine(paragraphNumber);
1491 if (para)
1492 return para->GetTextForRange(para->GetRange());
1493 else
1494 return wxEmptyString;
1495 }
1496
1497 /// Convert zero-based line column and paragraph number to a position.
1498 long wxRichTextParagraphLayoutBox::XYToPosition(long x, long y) const
1499 {
1500 wxRichTextParagraph* para = GetParagraphAtLine(y);
1501 if (para)
1502 {
1503 return para->GetRange().GetStart() + x;
1504 }
1505 else
1506 return -1;
1507 }
1508
1509 /// Convert zero-based position to line column and paragraph number
1510 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos, long* x, long* y) const
1511 {
1512 wxRichTextParagraph* para = GetParagraphAtPosition(pos);
1513 if (para)
1514 {
1515 int count = 0;
1516 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1517 while (node)
1518 {
1519 wxRichTextObject* child = node->GetData();
1520 if (child == para)
1521 break;
1522 count ++;
1523 node = node->GetNext();
1524 }
1525
1526 *y = count;
1527 *x = pos - para->GetRange().GetStart();
1528
1529 return true;
1530 }
1531 else
1532 return false;
1533 }
1534
1535 /// Get the leaf object in a paragraph at this position.
1536 /// Given a line number, get the corresponding wxRichTextLine object.
1537 wxRichTextObject* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position) const
1538 {
1539 wxRichTextParagraph* para = GetParagraphAtPosition(position);
1540 if (para)
1541 {
1542 wxRichTextObjectList::compatibility_iterator node = para->GetChildren().GetFirst();
1543
1544 while (node)
1545 {
1546 wxRichTextObject* child = node->GetData();
1547 if (child->GetRange().Contains(position))
1548 return child;
1549
1550 node = node->GetNext();
1551 }
1552 if (position == para->GetRange().GetEnd() && para->GetChildCount() > 0)
1553 return para->GetChildren().GetLast()->GetData();
1554 }
1555 return NULL;
1556 }
1557
1558 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1559 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style, bool withUndo)
1560 {
1561 bool characterStyle = false;
1562 bool paragraphStyle = false;
1563
1564 if (style.IsCharacterStyle())
1565 characterStyle = true;
1566 if (style.IsParagraphStyle())
1567 paragraphStyle = true;
1568
1569 // If we are associated with a control, make undoable; otherwise, apply immediately
1570 // to the data.
1571
1572 bool haveControl = (GetRichTextCtrl() != NULL);
1573
1574 wxRichTextAction* action = NULL;
1575
1576 if (haveControl && withUndo)
1577 {
1578 action = new wxRichTextAction(NULL, _("Change Style"), wxRICHTEXT_CHANGE_STYLE, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1579 action->SetRange(range);
1580 action->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1581 }
1582
1583 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1584 while (node)
1585 {
1586 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1587 wxASSERT (para != NULL);
1588
1589 if (para && para->GetChildCount() > 0)
1590 {
1591 // Stop searching if we're beyond the range of interest
1592 if (para->GetRange().GetStart() > range.GetEnd())
1593 break;
1594
1595 if (!para->GetRange().IsOutside(range))
1596 {
1597 // We'll be using a copy of the paragraph to make style changes,
1598 // not updating the buffer directly.
1599 wxRichTextParagraph* newPara wxDUMMY_INITIALIZE(NULL);
1600
1601 if (haveControl && withUndo)
1602 {
1603 newPara = new wxRichTextParagraph(*para);
1604 action->GetNewParagraphs().AppendChild(newPara);
1605
1606 // Also store the old ones for Undo
1607 action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para));
1608 }
1609 else
1610 newPara = para;
1611
1612 if (paragraphStyle)
1613 wxRichTextApplyStyle(newPara->GetAttributes(), style);
1614
1615 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1616 // If applying paragraph styles dynamically, don't change the text objects' attributes
1617 // since they will computed as needed. Only apply the character styling if it's _only_
1618 // character styling. This policy is subject to change and might be put under user control.
1619
1620 if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1621 #else
1622 if (characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1623 #endif
1624 {
1625 wxRichTextRange childRange(range);
1626 childRange.LimitTo(newPara->GetRange());
1627
1628 // Find the starting position and if necessary split it so
1629 // we can start applying a different style.
1630 // TODO: check that the style actually changes or is different
1631 // from style outside of range
1632 wxRichTextObject* firstObject wxDUMMY_INITIALIZE(NULL);
1633 wxRichTextObject* lastObject wxDUMMY_INITIALIZE(NULL);
1634
1635 if (childRange.GetStart() == newPara->GetRange().GetStart())
1636 firstObject = newPara->GetChildren().GetFirst()->GetData();
1637 else
1638 firstObject = newPara->SplitAt(range.GetStart());
1639
1640 // Increment by 1 because we're apply the style one _after_ the split point
1641 long splitPoint = childRange.GetEnd();
1642 if (splitPoint != newPara->GetRange().GetEnd())
1643 splitPoint ++;
1644
1645 // Find last object
1646 if (splitPoint == newPara->GetRange().GetEnd() || splitPoint == (newPara->GetRange().GetEnd() - 1))
1647 lastObject = newPara->GetChildren().GetLast()->GetData();
1648 else
1649 // lastObject is set as a side-effect of splitting. It's
1650 // returned as the object before the new object.
1651 (void) newPara->SplitAt(splitPoint, & lastObject);
1652
1653 wxASSERT(firstObject != NULL);
1654 wxASSERT(lastObject != NULL);
1655
1656 if (!firstObject || !lastObject)
1657 continue;
1658
1659 wxRichTextObjectList::compatibility_iterator firstNode = newPara->GetChildren().Find(firstObject);
1660 wxRichTextObjectList::compatibility_iterator lastNode = newPara->GetChildren().Find(lastObject);
1661
1662 wxASSERT(firstNode);
1663 wxASSERT(lastNode);
1664
1665 wxRichTextObjectList::compatibility_iterator node2 = firstNode;
1666
1667 while (node2)
1668 {
1669 wxRichTextObject* child = node2->GetData();
1670
1671 wxRichTextApplyStyle(child->GetAttributes(), style);
1672 if (node2 == lastNode)
1673 break;
1674
1675 node2 = node2->GetNext();
1676 }
1677 }
1678 }
1679 }
1680
1681 node = node->GetNext();
1682 }
1683
1684 // Do action, or delay it until end of batch.
1685 if (haveControl && withUndo)
1686 GetRichTextCtrl()->GetBuffer().SubmitAction(action);
1687
1688 return true;
1689 }
1690
1691 /// Set text attributes
1692 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const wxTextAttrEx& style, bool withUndo)
1693 {
1694 wxRichTextAttr richStyle = style;
1695 return SetStyle(range, richStyle, withUndo);
1696 }
1697
1698 /// Get the text attributes for this position.
1699 bool wxRichTextParagraphLayoutBox::GetStyle(long position, wxTextAttrEx& style)
1700 {
1701 return DoGetStyle(position, style, true);
1702 }
1703
1704 /// Get the text attributes for this position.
1705 bool wxRichTextParagraphLayoutBox::GetStyle(long position, wxRichTextAttr& style)
1706 {
1707 wxTextAttrEx textAttrEx(style);
1708 if (GetStyle(position, textAttrEx))
1709 {
1710 style = textAttrEx;
1711 return true;
1712 }
1713 else
1714 return false;
1715 }
1716
1717 /// Get the content (uncombined) attributes for this position.
1718 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position, wxTextAttrEx& style)
1719 {
1720 return DoGetStyle(position, style, false);
1721 }
1722
1723 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position, wxRichTextAttr& style)
1724 {
1725 wxTextAttrEx textAttrEx(style);
1726 if (GetUncombinedStyle(position, textAttrEx))
1727 {
1728 style = textAttrEx;
1729 return true;
1730 }
1731 else
1732 return false;
1733 }
1734
1735 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1736 /// context attributes.
1737 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position, wxTextAttrEx& style, bool combineStyles)
1738 {
1739 wxRichTextObject* obj wxDUMMY_INITIALIZE(NULL);
1740
1741 if (style.IsParagraphStyle())
1742 {
1743 obj = GetParagraphAtPosition(position);
1744 if (obj)
1745 {
1746 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1747 if (combineStyles)
1748 {
1749 // Start with the base style
1750 style = GetAttributes();
1751
1752 // Apply the paragraph style
1753 wxRichTextApplyStyle(style, obj->GetAttributes());
1754 }
1755 else
1756 style = obj->GetAttributes();
1757 #else
1758 style = obj->GetAttributes();
1759 #endif
1760 return true;
1761 }
1762 }
1763 else
1764 {
1765 obj = GetLeafObjectAtPosition(position);
1766 if (obj)
1767 {
1768 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1769 if (combineStyles)
1770 {
1771 wxRichTextParagraph* para = wxDynamicCast(obj->GetParent(), wxRichTextParagraph);
1772 style = para ? para->GetCombinedAttributes(obj->GetAttributes()) : obj->GetAttributes();
1773 }
1774 else
1775 style = obj->GetAttributes();
1776 #else
1777 style = obj->GetAttributes();
1778 #endif
1779 return true;
1780 }
1781 }
1782 return false;
1783 }
1784
1785 /// Set default style
1786 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx& style)
1787 {
1788 // I don't think the default style should be combined with the previous
1789 // default style.
1790 m_defaultAttributes = style;
1791
1792 #if 0
1793 // keep the old attributes if the new style doesn't specify them unless the
1794 // new style is empty - then reset m_defaultStyle (as there is no other way
1795 // to do it)
1796 if ( style.IsDefault() )
1797 m_defaultAttributes = style;
1798 else
1799 m_defaultAttributes = wxTextAttrEx::CombineEx(style, m_defaultAttributes, NULL);
1800 #endif
1801 return true;
1802 }
1803
1804 /// Test if this whole range has character attributes of the specified kind. If any
1805 /// of the attributes are different within the range, the test fails. You
1806 /// can use this to implement, for example, bold button updating. style must have
1807 /// flags indicating which attributes are of interest.
1808 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const
1809 {
1810 int foundCount = 0;
1811 int matchingCount = 0;
1812
1813 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1814 while (node)
1815 {
1816 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1817 wxASSERT (para != NULL);
1818
1819 if (para)
1820 {
1821 // Stop searching if we're beyond the range of interest
1822 if (para->GetRange().GetStart() > range.GetEnd())
1823 return foundCount == matchingCount;
1824
1825 if (!para->GetRange().IsOutside(range))
1826 {
1827 wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst();
1828
1829 while (node2)
1830 {
1831 wxRichTextObject* child = node2->GetData();
1832 if (!child->GetRange().IsOutside(range) && child->IsKindOf(CLASSINFO(wxRichTextPlainText)))
1833 {
1834 foundCount ++;
1835 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1836 wxTextAttrEx textAttr = para->GetCombinedAttributes(child->GetAttributes());
1837 #else
1838 const wxTextAttrEx& textAttr = child->GetAttributes();
1839 #endif
1840 if (wxTextAttrEqPartial(textAttr, style, style.GetFlags()))
1841 matchingCount ++;
1842 }
1843
1844 node2 = node2->GetNext();
1845 }
1846 }
1847 }
1848
1849 node = node->GetNext();
1850 }
1851
1852 return foundCount == matchingCount;
1853 }
1854
1855 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const
1856 {
1857 wxRichTextAttr richStyle = style;
1858 return HasCharacterAttributes(range, richStyle);
1859 }
1860
1861 /// Test if this whole range has paragraph attributes of the specified kind. If any
1862 /// of the attributes are different within the range, the test fails. You
1863 /// can use this to implement, for example, centering button updating. style must have
1864 /// flags indicating which attributes are of interest.
1865 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const
1866 {
1867 int foundCount = 0;
1868 int matchingCount = 0;
1869
1870 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1871 while (node)
1872 {
1873 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1874 wxASSERT (para != NULL);
1875
1876 if (para)
1877 {
1878 // Stop searching if we're beyond the range of interest
1879 if (para->GetRange().GetStart() > range.GetEnd())
1880 return foundCount == matchingCount;
1881
1882 if (!para->GetRange().IsOutside(range))
1883 {
1884 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1885 wxTextAttrEx textAttr = GetAttributes();
1886 // Apply the paragraph style
1887 wxRichTextApplyStyle(textAttr, para->GetAttributes());
1888
1889 #else
1890 const wxTextAttrEx& textAttr = para->GetAttributes();
1891 #endif
1892 foundCount ++;
1893 if (wxTextAttrEqPartial(textAttr, style, style.GetFlags()))
1894 matchingCount ++;
1895 }
1896 }
1897
1898 node = node->GetNext();
1899 }
1900 return foundCount == matchingCount;
1901 }
1902
1903 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const
1904 {
1905 wxRichTextAttr richStyle = style;
1906 return HasParagraphAttributes(range, richStyle);
1907 }
1908
1909 void wxRichTextParagraphLayoutBox::Clear()
1910 {
1911 DeleteChildren();
1912 }
1913
1914 void wxRichTextParagraphLayoutBox::Reset()
1915 {
1916 Clear();
1917
1918 AddParagraph(wxEmptyString);
1919 }
1920
1921 /// Invalidate the buffer. With no argument, invalidates whole buffer.
1922 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange& invalidRange)
1923 {
1924 SetDirty(true);
1925
1926 if (invalidRange == wxRICHTEXT_ALL)
1927 {
1928 m_invalidRange = wxRICHTEXT_ALL;
1929 return;
1930 }
1931
1932 // Already invalidating everything
1933 if (m_invalidRange == wxRICHTEXT_ALL)
1934 return;
1935
1936 if ((invalidRange.GetStart() < m_invalidRange.GetStart()) || m_invalidRange.GetStart() == -1)
1937 m_invalidRange.SetStart(invalidRange.GetStart());
1938 if (invalidRange.GetEnd() > m_invalidRange.GetEnd())
1939 m_invalidRange.SetEnd(invalidRange.GetEnd());
1940 }
1941
1942 /// Get invalid range, rounding to entire paragraphs if argument is true.
1943 wxRichTextRange wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs) const
1944 {
1945 if (m_invalidRange == wxRICHTEXT_ALL || m_invalidRange == wxRICHTEXT_NONE)
1946 return m_invalidRange;
1947
1948 wxRichTextRange range = m_invalidRange;
1949
1950 if (wholeParagraphs)
1951 {
1952 wxRichTextParagraph* para1 = GetParagraphAtPosition(range.GetStart());
1953 wxRichTextParagraph* para2 = GetParagraphAtPosition(range.GetEnd());
1954 if (para1)
1955 range.SetStart(para1->GetRange().GetStart());
1956 if (para2)
1957 range.SetEnd(para2->GetRange().GetEnd());
1958 }
1959 return range;
1960 }
1961
1962 /// Apply the style sheet to the buffer, for example if the styles have changed.
1963 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
1964 {
1965 wxASSERT(styleSheet != NULL);
1966 if (!styleSheet)
1967 return false;
1968
1969 int foundCount = 0;
1970
1971 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1972 while (node)
1973 {
1974 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1975 wxASSERT (para != NULL);
1976
1977 if (para)
1978 {
1979 if (!para->GetAttributes().GetParagraphStyleName().IsEmpty())
1980 {
1981 wxRichTextParagraphStyleDefinition* def = styleSheet->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
1982 if (def)
1983 {
1984 para->GetAttributes() = def->GetStyle();
1985 foundCount ++;
1986 }
1987 }
1988 }
1989
1990 node = node->GetNext();
1991 }
1992 return foundCount != 0;
1993 }
1994
1995 /*!
1996 * wxRichTextParagraph
1997 * This object represents a single paragraph (or in a straight text editor, a line).
1998 */
1999
2000 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph, wxRichTextBox)
2001
2002 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject* parent, wxTextAttrEx* style):
2003 wxRichTextBox(parent)
2004 {
2005 if (parent && !style)
2006 SetAttributes(parent->GetAttributes());
2007 if (style)
2008 SetAttributes(*style);
2009 }
2010
2011 wxRichTextParagraph::wxRichTextParagraph(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* style):
2012 wxRichTextBox(parent)
2013 {
2014 if (parent && !style)
2015 SetAttributes(parent->GetAttributes());
2016 if (style)
2017 SetAttributes(*style);
2018
2019 AppendChild(new wxRichTextPlainText(text, this));
2020 }
2021
2022 wxRichTextParagraph::~wxRichTextParagraph()
2023 {
2024 ClearLines();
2025 }
2026
2027 /// Draw the item
2028 bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& WXUNUSED(range), const wxRichTextRange& selectionRange, const wxRect& WXUNUSED(rect), int WXUNUSED(descent), int style)
2029 {
2030 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2031 wxTextAttrEx attr = GetCombinedAttributes();
2032 #else
2033 const wxTextAttrEx& attr = GetAttributes();
2034 #endif
2035
2036 // Draw the bullet, if any
2037 if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
2038 {
2039 if (attr.GetLeftSubIndent() != 0)
2040 {
2041 int spaceBeforePara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingBefore());
2042 // int spaceAfterPara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingAfter());
2043 int leftIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftIndent());
2044 // int leftSubIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftSubIndent());
2045 // int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent());
2046
2047 if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP)
2048 {
2049 // TODO
2050 }
2051 else
2052 {
2053 wxString bulletText = GetBulletText();
2054 if (!bulletText.empty())
2055 {
2056 if (attr.GetFont().Ok())
2057 dc.SetFont(attr.GetFont());
2058
2059 if (attr.GetTextColour().Ok())
2060 dc.SetTextForeground(attr.GetTextColour());
2061
2062 dc.SetBackgroundMode(wxTRANSPARENT);
2063
2064 // Get line height from first line, if any
2065 wxRichTextLine* line = m_cachedLines.GetFirst() ? (wxRichTextLine* ) m_cachedLines.GetFirst()->GetData() : (wxRichTextLine*) NULL;
2066
2067 wxPoint linePos;
2068 int lineHeight wxDUMMY_INITIALIZE(0);
2069 if (line)
2070 {
2071 lineHeight = line->GetSize().y;
2072 linePos = line->GetPosition() + GetPosition();
2073 }
2074 else
2075 {
2076 lineHeight = dc.GetCharHeight();
2077 linePos = GetPosition();
2078 linePos.y += spaceBeforePara;
2079 }
2080
2081 int charHeight = dc.GetCharHeight();
2082
2083 int x = GetPosition().x + leftIndent;
2084 int y = linePos.y + (lineHeight - charHeight);
2085
2086 dc.DrawText(bulletText, x, y);
2087 }
2088 }
2089 }
2090 }
2091
2092 // Draw the range for each line, one object at a time.
2093
2094 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
2095 while (node)
2096 {
2097 wxRichTextLine* line = node->GetData();
2098 wxRichTextRange lineRange = line->GetAbsoluteRange();
2099
2100 int maxDescent = line->GetDescent();
2101
2102 // Lines are specified relative to the paragraph
2103
2104 wxPoint linePosition = line->GetPosition() + GetPosition();
2105 wxPoint objectPosition = linePosition;
2106
2107 // Loop through objects until we get to the one within range
2108 wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst();
2109 while (node2)
2110 {
2111 wxRichTextObject* child = node2->GetData();
2112 if (!child->GetRange().IsOutside(lineRange))
2113 {
2114 // Draw this part of the line at the correct position
2115 wxRichTextRange objectRange(child->GetRange());
2116 objectRange.LimitTo(lineRange);
2117
2118 wxSize objectSize;
2119 int descent = 0;
2120 child->GetRangeSize(objectRange, objectSize, descent, dc, wxRICHTEXT_UNFORMATTED, objectPosition);
2121
2122 // Use the child object's width, but the whole line's height
2123 wxRect childRect(objectPosition, wxSize(objectSize.x, line->GetSize().y));
2124 child->Draw(dc, objectRange, selectionRange, childRect, maxDescent, style);
2125
2126 objectPosition.x += objectSize.x;
2127 }
2128 else if (child->GetRange().GetStart() > lineRange.GetEnd())
2129 // Can break out of inner loop now since we've passed this line's range
2130 break;
2131
2132 node2 = node2->GetNext();
2133 }
2134
2135 node = node->GetNext();
2136 }
2137
2138 return true;
2139 }
2140
2141 /// Lay the item out
2142 bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style)
2143 {
2144 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2145 wxTextAttrEx attr = GetCombinedAttributes();
2146 #else
2147 const wxTextAttrEx& attr = GetAttributes();
2148 #endif
2149
2150 // ClearLines();
2151
2152 // Increase the size of the paragraph due to spacing
2153 int spaceBeforePara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingBefore());
2154 int spaceAfterPara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingAfter());
2155 int leftIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftIndent());
2156 int leftSubIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftSubIndent());
2157 int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent());
2158
2159 int lineSpacing = 0;
2160
2161 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
2162 if (attr.GetLineSpacing() > 10 && attr.GetFont().Ok())
2163 {
2164 dc.SetFont(attr.GetFont());
2165 lineSpacing = (ConvertTenthsMMToPixels(dc, dc.GetCharHeight()) * attr.GetLineSpacing())/10;
2166 }
2167
2168 // Available space for text on each line differs.
2169 int availableTextSpaceFirstLine = rect.GetWidth() - leftIndent - rightIndent;
2170
2171 // Bullets start the text at the same position as subsequent lines
2172 if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
2173 availableTextSpaceFirstLine -= leftSubIndent;
2174
2175 int availableTextSpaceSubsequentLines = rect.GetWidth() - leftIndent - rightIndent - leftSubIndent;
2176
2177 // Start position for each line relative to the paragraph
2178 int startPositionFirstLine = leftIndent;
2179 int startPositionSubsequentLines = leftIndent + leftSubIndent;
2180
2181 // If we have a bullet in this paragraph, the start position for the first line's text
2182 // is actually leftIndent + leftSubIndent.
2183 if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
2184 startPositionFirstLine = startPositionSubsequentLines;
2185
2186 //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH);
2187 //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT);
2188
2189 long lastEndPos = GetRange().GetStart()-1;
2190 long lastCompletedEndPos = lastEndPos;
2191
2192 int currentWidth = 0;
2193 SetPosition(rect.GetPosition());
2194
2195 wxPoint currentPosition(0, spaceBeforePara); // We will calculate lines relative to paragraph
2196 int lineHeight = 0;
2197 int maxWidth = 0;
2198 int maxDescent = 0;
2199
2200 int lineCount = 0;
2201
2202 // Split up lines
2203
2204 // We may need to go back to a previous child, in which case create the new line,
2205 // find the child corresponding to the start position of the string, and
2206 // continue.
2207
2208 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2209 while (node)
2210 {
2211 wxRichTextObject* child = node->GetData();
2212
2213 // If this is e.g. a composite text box, it will need to be laid out itself.
2214 // But if just a text fragment or image, for example, this will
2215 // do nothing. NB: won't we need to set the position after layout?
2216 // since for example if position is dependent on vertical line size, we
2217 // can't tell the position until the size is determined. So possibly introduce
2218 // another layout phase.
2219
2220 child->Layout(dc, rect, style);
2221
2222 // Available width depends on whether we're on the first or subsequent lines
2223 int availableSpaceForText = (lineCount == 0 ? availableTextSpaceFirstLine : availableTextSpaceSubsequentLines);
2224
2225 currentPosition.x = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines);
2226
2227 // We may only be looking at part of a child, if we searched back for wrapping
2228 // and found a suitable point some way into the child. So get the size for the fragment
2229 // if necessary.
2230
2231 wxSize childSize;
2232 int childDescent = 0;
2233 if (lastEndPos == child->GetRange().GetStart() - 1)
2234 {
2235 childSize = child->GetCachedSize();
2236 childDescent = child->GetDescent();
2237 }
2238 else
2239 GetRangeSize(wxRichTextRange(lastEndPos+1, child->GetRange().GetEnd()), childSize, childDescent, dc, wxRICHTEXT_UNFORMATTED,rect.GetPosition());
2240
2241 if (childSize.x + currentWidth > availableSpaceForText)
2242 {
2243 long wrapPosition = 0;
2244
2245 // Find a place to wrap. This may walk back to previous children,
2246 // for example if a word spans several objects.
2247 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos+1, child->GetRange().GetEnd()), dc, availableSpaceForText, wrapPosition))
2248 {
2249 // If the function failed, just cut it off at the end of this child.
2250 wrapPosition = child->GetRange().GetEnd();
2251 }
2252
2253 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
2254 if (wrapPosition <= lastCompletedEndPos)
2255 wrapPosition = wxMax(lastCompletedEndPos+1,child->GetRange().GetEnd());
2256
2257 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
2258
2259 // Let's find the actual size of the current line now
2260 wxSize actualSize;
2261 wxRichTextRange actualRange(lastCompletedEndPos+1, wrapPosition);
2262 GetRangeSize(actualRange, actualSize, childDescent, dc, wxRICHTEXT_UNFORMATTED);
2263 currentWidth = actualSize.x;
2264 lineHeight = wxMax(lineHeight, actualSize.y);
2265 maxDescent = wxMax(childDescent, maxDescent);
2266
2267 // Add a new line
2268 wxRichTextLine* line = AllocateLine(lineCount);
2269
2270 // Set relative range so we won't have to change line ranges when paragraphs are moved
2271 line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart()));
2272 line->SetPosition(currentPosition);
2273 line->SetSize(wxSize(currentWidth, lineHeight));
2274 line->SetDescent(maxDescent);
2275
2276 // Now move down a line. TODO: add margins, spacing
2277 currentPosition.y += lineHeight;
2278 currentPosition.y += lineSpacing;
2279 currentWidth = 0;
2280 maxDescent = 0;
2281 maxWidth = wxMax(maxWidth, currentWidth);
2282
2283 lineCount ++;
2284
2285 // TODO: account for zero-length objects, such as fields
2286 wxASSERT(wrapPosition > lastCompletedEndPos);
2287
2288 lastEndPos = wrapPosition;
2289 lastCompletedEndPos = lastEndPos;
2290
2291 lineHeight = 0;
2292
2293 // May need to set the node back to a previous one, due to searching back in wrapping
2294 wxRichTextObject* childAfterWrapPosition = FindObjectAtPosition(wrapPosition+1);
2295 if (childAfterWrapPosition)
2296 node = m_children.Find(childAfterWrapPosition);
2297 else
2298 node = node->GetNext();
2299 }
2300 else
2301 {
2302 // We still fit, so don't add a line, and keep going
2303 currentWidth += childSize.x;
2304 lineHeight = wxMax(lineHeight, childSize.y);
2305 maxDescent = wxMax(childDescent, maxDescent);
2306
2307 maxWidth = wxMax(maxWidth, currentWidth);
2308 lastEndPos = child->GetRange().GetEnd();
2309
2310 node = node->GetNext();
2311 }
2312 }
2313
2314 // Add the last line - it's the current pos -> last para pos
2315 // Substract -1 because the last position is always the end-paragraph position.
2316 if (lastCompletedEndPos <= GetRange().GetEnd()-1)
2317 {
2318 currentPosition.x = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines);
2319
2320 wxRichTextLine* line = AllocateLine(lineCount);
2321
2322 wxRichTextRange actualRange(lastCompletedEndPos+1, GetRange().GetEnd()-1);
2323
2324 // Set relative range so we won't have to change line ranges when paragraphs are moved
2325 line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart()));
2326
2327 line->SetPosition(currentPosition);
2328
2329 if (lineHeight == 0)
2330 {
2331 if (attr.GetFont().Ok())
2332 dc.SetFont(attr.GetFont());
2333 lineHeight = dc.GetCharHeight();
2334 }
2335 if (maxDescent == 0)
2336 {
2337 int w, h;
2338 dc.GetTextExtent(wxT("X"), & w, &h, & maxDescent);
2339 }
2340
2341 line->SetSize(wxSize(currentWidth, lineHeight));
2342 line->SetDescent(maxDescent);
2343 currentPosition.y += lineHeight;
2344 currentPosition.y += lineSpacing;
2345 lineCount ++;
2346 }
2347
2348 // Remove remaining unused line objects, if any
2349 ClearUnusedLines(lineCount);
2350
2351 // Apply styles to wrapped lines
2352 ApplyParagraphStyle(attr, rect);
2353
2354 SetCachedSize(wxSize(maxWidth, currentPosition.y + spaceBeforePara + spaceAfterPara));
2355
2356 m_dirty = false;
2357
2358 return true;
2359 }
2360
2361 /// Apply paragraph styles, such as centering, to wrapped lines
2362 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx& attr, const wxRect& rect)
2363 {
2364 if (!attr.HasAlignment())
2365 return;
2366
2367 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
2368 while (node)
2369 {
2370 wxRichTextLine* line = node->GetData();
2371
2372 wxPoint pos = line->GetPosition();
2373 wxSize size = line->GetSize();
2374
2375 // centering, right-justification
2376 if (attr.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
2377 {
2378 pos.x = (rect.GetWidth() - size.x)/2 + pos.x;
2379 line->SetPosition(pos);
2380 }
2381 else if (attr.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
2382 {
2383 pos.x = rect.GetRight() - size.x;
2384 line->SetPosition(pos);
2385 }
2386
2387 node = node->GetNext();
2388 }
2389 }
2390
2391 /// Insert text at the given position
2392 bool wxRichTextParagraph::InsertText(long pos, const wxString& text)
2393 {
2394 wxRichTextObject* childToUse = NULL;
2395 wxRichTextObjectList::compatibility_iterator nodeToUse = wxRichTextObjectList::compatibility_iterator();
2396
2397 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2398 while (node)
2399 {
2400 wxRichTextObject* child = node->GetData();
2401 if (child->GetRange().Contains(pos) && child->GetRange().GetLength() > 0)
2402 {
2403 childToUse = child;
2404 nodeToUse = node;
2405 break;
2406 }
2407
2408 node = node->GetNext();
2409 }
2410
2411 if (childToUse)
2412 {
2413 wxRichTextPlainText* textObject = wxDynamicCast(childToUse, wxRichTextPlainText);
2414 if (textObject)
2415 {
2416 int posInString = pos - textObject->GetRange().GetStart();
2417
2418 wxString newText = textObject->GetText().Mid(0, posInString) +
2419 text + textObject->GetText().Mid(posInString);
2420 textObject->SetText(newText);
2421
2422 int textLength = text.length();
2423
2424 textObject->SetRange(wxRichTextRange(textObject->GetRange().GetStart(),
2425 textObject->GetRange().GetEnd() + textLength));
2426
2427 // Increment the end range of subsequent fragments in this paragraph.
2428 // We'll set the paragraph range itself at a higher level.
2429
2430 wxRichTextObjectList::compatibility_iterator node = nodeToUse->GetNext();
2431 while (node)
2432 {
2433 wxRichTextObject* child = node->GetData();
2434 child->SetRange(wxRichTextRange(textObject->GetRange().GetStart() + textLength,
2435 textObject->GetRange().GetEnd() + textLength));
2436
2437 node = node->GetNext();
2438 }
2439
2440 return true;
2441 }
2442 else
2443 {
2444 // TODO: if not a text object, insert at closest position, e.g. in front of it
2445 }
2446 }
2447 else
2448 {
2449 // Add at end.
2450 // Don't pass parent initially to suppress auto-setting of parent range.
2451 // We'll do that at a higher level.
2452 wxRichTextPlainText* textObject = new wxRichTextPlainText(text, this);
2453
2454 AppendChild(textObject);
2455 return true;
2456 }
2457
2458 return false;
2459 }
2460
2461 void wxRichTextParagraph::Copy(const wxRichTextParagraph& obj)
2462 {
2463 wxRichTextBox::Copy(obj);
2464 }
2465
2466 /// Clear the cached lines
2467 void wxRichTextParagraph::ClearLines()
2468 {
2469 WX_CLEAR_LIST(wxRichTextLineList, m_cachedLines);
2470 }
2471
2472 /// Get/set the object size for the given range. Returns false if the range
2473 /// is invalid for this object.
2474 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const
2475 {
2476 if (!range.IsWithin(GetRange()))
2477 return false;
2478
2479 if (flags & wxRICHTEXT_UNFORMATTED)
2480 {
2481 // Just use unformatted data, assume no line breaks
2482 // TODO: take into account line breaks
2483
2484 wxSize sz;
2485
2486 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2487 while (node)
2488 {
2489 wxRichTextObject* child = node->GetData();
2490 if (!child->GetRange().IsOutside(range))
2491 {
2492 wxSize childSize;
2493
2494 wxRichTextRange rangeToUse = range;
2495 rangeToUse.LimitTo(child->GetRange());
2496 int childDescent = 0;
2497
2498 if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, position))
2499 {
2500 sz.y = wxMax(sz.y, childSize.y);
2501 sz.x += childSize.x;
2502 descent = wxMax(descent, childDescent);
2503 }
2504 }
2505
2506 node = node->GetNext();
2507 }
2508 size = sz;
2509 }
2510 else
2511 {
2512 // Use formatted data, with line breaks
2513 wxSize sz;
2514
2515 // We're going to loop through each line, and then for each line,
2516 // call GetRangeSize for the fragment that comprises that line.
2517 // Only we have to do that multiple times within the line, because
2518 // the line may be broken into pieces. For now ignore line break commands
2519 // (so we can assume that getting the unformatted size for a fragment
2520 // within a line is the actual size)
2521
2522 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
2523 while (node)
2524 {
2525 wxRichTextLine* line = node->GetData();
2526 wxRichTextRange lineRange = line->GetAbsoluteRange();
2527 if (!lineRange.IsOutside(range))
2528 {
2529 wxSize lineSize;
2530
2531 wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst();
2532 while (node2)
2533 {
2534 wxRichTextObject* child = node2->GetData();
2535
2536 if (!child->GetRange().IsOutside(lineRange))
2537 {
2538 wxRichTextRange rangeToUse = lineRange;
2539 rangeToUse.LimitTo(child->GetRange());
2540
2541 wxSize childSize;
2542 int childDescent = 0;
2543 if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, position))
2544 {
2545 lineSize.y = wxMax(lineSize.y, childSize.y);
2546 lineSize.x += childSize.x;
2547 }
2548 descent = wxMax(descent, childDescent);
2549 }
2550
2551 node2 = node2->GetNext();
2552 }
2553
2554 // Increase size by a line (TODO: paragraph spacing)
2555 sz.y += lineSize.y;
2556 sz.x = wxMax(sz.x, lineSize.x);
2557 }
2558 node = node->GetNext();
2559 }
2560 size = sz;
2561 }
2562 return true;
2563 }
2564
2565 /// Finds the absolute position and row height for the given character position
2566 bool wxRichTextParagraph::FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart)
2567 {
2568 if (index == -1)
2569 {
2570 wxRichTextLine* line = ((wxRichTextParagraphLayoutBox*)GetParent())->GetLineAtPosition(0);
2571 if (line)
2572 *height = line->GetSize().y;
2573 else
2574 *height = dc.GetCharHeight();
2575
2576 // -1 means 'the start of the buffer'.
2577 pt = GetPosition();
2578 if (line)
2579 pt = pt + line->GetPosition();
2580
2581 return true;
2582 }
2583
2584 // The final position in a paragraph is taken to mean the position
2585 // at the start of the next paragraph.
2586 if (index == GetRange().GetEnd())
2587 {
2588 wxRichTextParagraphLayoutBox* parent = wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox);
2589 wxASSERT( parent != NULL );
2590
2591 // Find the height at the next paragraph, if any
2592 wxRichTextLine* line = parent->GetLineAtPosition(index + 1);
2593 if (line)
2594 {
2595 *height = line->GetSize().y;
2596 pt = line->GetAbsolutePosition();
2597 }
2598 else
2599 {
2600 *height = dc.GetCharHeight();
2601 int indent = ConvertTenthsMMToPixels(dc, m_attributes.GetLeftIndent());
2602 pt = wxPoint(indent, GetCachedSize().y);
2603 }
2604
2605 return true;
2606 }
2607
2608 if (index < GetRange().GetStart() || index > GetRange().GetEnd())
2609 return false;
2610
2611 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
2612 while (node)
2613 {
2614 wxRichTextLine* line = node->GetData();
2615 wxRichTextRange lineRange = line->GetAbsoluteRange();
2616 if (index >= lineRange.GetStart() && index <= lineRange.GetEnd())
2617 {
2618 // If this is the last point in the line, and we're forcing the
2619 // returned value to be the start of the next line, do the required
2620 // thing.
2621 if (index == lineRange.GetEnd() && forceLineStart)
2622 {
2623 if (node->GetNext())
2624 {
2625 wxRichTextLine* nextLine = node->GetNext()->GetData();
2626 *height = nextLine->GetSize().y;
2627 pt = nextLine->GetAbsolutePosition();
2628 return true;
2629 }
2630 }
2631
2632 pt.y = line->GetPosition().y + GetPosition().y;
2633
2634 wxRichTextRange r(lineRange.GetStart(), index);
2635 wxSize rangeSize;
2636 int descent = 0;
2637
2638 // We find the size of the line up to this point,
2639 // then we can add this size to the line start position and
2640 // paragraph start position to find the actual position.
2641
2642 if (GetRangeSize(r, rangeSize, descent, dc, wxRICHTEXT_UNFORMATTED, line->GetPosition()+ GetPosition()))
2643 {
2644 pt.x = line->GetPosition().x + GetPosition().x + rangeSize.x;
2645 *height = line->GetSize().y;
2646
2647 return true;
2648 }
2649
2650 }
2651
2652 node = node->GetNext();
2653 }
2654
2655 return false;
2656 }
2657
2658 /// Hit-testing: returns a flag indicating hit test details, plus
2659 /// information about position
2660 int wxRichTextParagraph::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition)
2661 {
2662 wxPoint paraPos = GetPosition();
2663
2664 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
2665 while (node)
2666 {
2667 wxRichTextLine* line = node->GetData();
2668 wxPoint linePos = paraPos + line->GetPosition();
2669 wxSize lineSize = line->GetSize();
2670 wxRichTextRange lineRange = line->GetAbsoluteRange();
2671
2672 if (pt.y >= linePos.y && pt.y <= linePos.y + lineSize.y)
2673 {
2674 if (pt.x < linePos.x)
2675 {
2676 textPosition = lineRange.GetStart();
2677 return wxRICHTEXT_HITTEST_BEFORE;
2678 }
2679 else if (pt.x >= (linePos.x + lineSize.x))
2680 {
2681 textPosition = lineRange.GetEnd();
2682 return wxRICHTEXT_HITTEST_AFTER;
2683 }
2684 else
2685 {
2686 long i;
2687 int lastX = linePos.x;
2688 for (i = lineRange.GetStart(); i <= lineRange.GetEnd(); i++)
2689 {
2690 wxSize childSize;
2691 int descent = 0;
2692
2693 wxRichTextRange rangeToUse(lineRange.GetStart(), i);
2694
2695 GetRangeSize(rangeToUse, childSize, descent, dc, wxRICHTEXT_UNFORMATTED, linePos);
2696
2697 int nextX = childSize.x + linePos.x;
2698
2699 if (pt.x >= lastX && pt.x <= nextX)
2700 {
2701 textPosition = i;
2702
2703 // So now we know it's between i-1 and i.
2704 // Let's see if we can be more precise about
2705 // which side of the position it's on.
2706
2707 int midPoint = (nextX - lastX)/2 + lastX;
2708 if (pt.x >= midPoint)
2709 return wxRICHTEXT_HITTEST_AFTER;
2710 else
2711 return wxRICHTEXT_HITTEST_BEFORE;
2712 }
2713 else
2714 {
2715 lastX = nextX;
2716 }
2717 }
2718 }
2719 }
2720
2721 node = node->GetNext();
2722 }
2723
2724 return wxRICHTEXT_HITTEST_NONE;
2725 }
2726
2727 /// Split an object at this position if necessary, and return
2728 /// the previous object, or NULL if inserting at beginning.
2729 wxRichTextObject* wxRichTextParagraph::SplitAt(long pos, wxRichTextObject** previousObject)
2730 {
2731 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2732 while (node)
2733 {
2734 wxRichTextObject* child = node->GetData();
2735
2736 if (pos == child->GetRange().GetStart())
2737 {
2738 if (previousObject)
2739 {
2740 if (node->GetPrevious())
2741 *previousObject = node->GetPrevious()->GetData();
2742 else
2743 *previousObject = NULL;
2744 }
2745
2746 return child;
2747 }
2748
2749 if (child->GetRange().Contains(pos))
2750 {
2751 // This should create a new object, transferring part of
2752 // the content to the old object and the rest to the new object.
2753 wxRichTextObject* newObject = child->DoSplit(pos);
2754
2755 // If we couldn't split this object, just insert in front of it.
2756 if (!newObject)
2757 {
2758 // Maybe this is an empty string, try the next one
2759 // return child;
2760 }
2761 else
2762 {
2763 // Insert the new object after 'child'
2764 if (node->GetNext())
2765 m_children.Insert(node->GetNext(), newObject);
2766 else
2767 m_children.Append(newObject);
2768 newObject->SetParent(this);
2769
2770 if (previousObject)
2771 *previousObject = child;
2772
2773 return newObject;
2774 }
2775 }
2776
2777 node = node->GetNext();
2778 }
2779 if (previousObject)
2780 *previousObject = NULL;
2781 return NULL;
2782 }
2783
2784 /// Move content to a list from obj on
2785 void wxRichTextParagraph::MoveToList(wxRichTextObject* obj, wxList& list)
2786 {
2787 wxRichTextObjectList::compatibility_iterator node = m_children.Find(obj);
2788 while (node)
2789 {
2790 wxRichTextObject* child = node->GetData();
2791 list.Append(child);
2792
2793 wxRichTextObjectList::compatibility_iterator oldNode = node;
2794
2795 node = node->GetNext();
2796
2797 m_children.DeleteNode(oldNode);
2798 }
2799 }
2800
2801 /// Add content back from list
2802 void wxRichTextParagraph::MoveFromList(wxList& list)
2803 {
2804 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
2805 {
2806 AppendChild((wxRichTextObject*) node->GetData());
2807 }
2808 }
2809
2810 /// Calculate range
2811 void wxRichTextParagraph::CalculateRange(long start, long& end)
2812 {
2813 wxRichTextCompositeObject::CalculateRange(start, end);
2814
2815 // Add one for end of paragraph
2816 end ++;
2817
2818 m_range.SetRange(start, end);
2819 }
2820
2821 /// Find the object at the given position
2822 wxRichTextObject* wxRichTextParagraph::FindObjectAtPosition(long position)
2823 {
2824 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2825 while (node)
2826 {
2827 wxRichTextObject* obj = node->GetData();
2828 if (obj->GetRange().Contains(position))
2829 return obj;
2830
2831 node = node->GetNext();
2832 }
2833 return NULL;
2834 }
2835
2836 /// Get the plain text searching from the start or end of the range.
2837 /// The resulting string may be shorter than the range given.
2838 bool wxRichTextParagraph::GetContiguousPlainText(wxString& text, const wxRichTextRange& range, bool fromStart)
2839 {
2840 text = wxEmptyString;
2841
2842 if (fromStart)
2843 {
2844 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2845 while (node)
2846 {
2847 wxRichTextObject* obj = node->GetData();
2848 if (!obj->GetRange().IsOutside(range))
2849 {
2850 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
2851 if (textObj)
2852 {
2853 text += textObj->GetTextForRange(range);
2854 }
2855 else
2856 return true;
2857 }
2858
2859 node = node->GetNext();
2860 }
2861 }
2862 else
2863 {
2864 wxRichTextObjectList::compatibility_iterator node = m_children.GetLast();
2865 while (node)
2866 {
2867 wxRichTextObject* obj = node->GetData();
2868 if (!obj->GetRange().IsOutside(range))
2869 {
2870 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
2871 if (textObj)
2872 {
2873 text = textObj->GetTextForRange(range) + text;
2874 }
2875 else
2876 return true;
2877 }
2878
2879 node = node->GetPrevious();
2880 }
2881 }
2882
2883 return true;
2884 }
2885
2886 /// Find a suitable wrap position.
2887 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& dc, int availableSpace, long& wrapPosition)
2888 {
2889 // Find the first position where the line exceeds the available space.
2890 wxSize sz;
2891 long i;
2892 long breakPosition = range.GetEnd();
2893 for (i = range.GetStart(); i <= range.GetEnd(); i++)
2894 {
2895 int descent = 0;
2896 GetRangeSize(wxRichTextRange(range.GetStart(), i), sz, descent, dc, wxRICHTEXT_UNFORMATTED);
2897
2898 if (sz.x > availableSpace)
2899 {
2900 breakPosition = i-1;
2901 break;
2902 }
2903 }
2904
2905 // Now we know the last position on the line.
2906 // Let's try to find a word break.
2907
2908 wxString plainText;
2909 if (GetContiguousPlainText(plainText, wxRichTextRange(range.GetStart(), breakPosition), false))
2910 {
2911 int spacePos = plainText.Find(wxT(' '), true);
2912 if (spacePos != wxNOT_FOUND)
2913 {
2914 int positionsFromEndOfString = plainText.length() - spacePos - 1;
2915 breakPosition = breakPosition - positionsFromEndOfString;
2916 }
2917 }
2918
2919 wrapPosition = breakPosition;
2920
2921 return true;
2922 }
2923
2924 /// Get the bullet text for this paragraph.
2925 wxString wxRichTextParagraph::GetBulletText()
2926 {
2927 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE ||
2928 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP))
2929 return wxEmptyString;
2930
2931 int number = GetAttributes().GetBulletNumber();
2932
2933 wxString text;
2934 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC)
2935 {
2936 text.Printf(wxT("%d"), number);
2937 }
2938 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER)
2939 {
2940 // TODO: Unicode, and also check if number > 26
2941 text.Printf(wxT("%c"), (wxChar) (number+64));
2942 }
2943 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER)
2944 {
2945 // TODO: Unicode, and also check if number > 26
2946 text.Printf(wxT("%c"), (wxChar) (number+96));
2947 }
2948 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER)
2949 {
2950 // TODO: convert from number to roman numeral
2951 if (number == 1)
2952 text = wxT("I");
2953 else if (number == 2)
2954 text = wxT("II");
2955 else if (number == 3)
2956 text = wxT("III");
2957 else if (number == 4)
2958 text = wxT("IV");
2959 else
2960 text = wxT("TODO");
2961 }
2962 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER)
2963 {
2964 // TODO: convert from number to roman numeral
2965 if (number == 1)
2966 text = wxT("i");
2967 else if (number == 2)
2968 text = wxT("ii");
2969 else if (number == 3)
2970 text = wxT("iii");
2971 else if (number == 4)
2972 text = wxT("iv");
2973 else
2974 text = wxT("TODO");
2975 }
2976 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL)
2977 {
2978 text = GetAttributes().GetBulletSymbol();
2979 }
2980
2981 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES)
2982 {
2983 text = wxT("(") + text + wxT(")");
2984 }
2985 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD)
2986 {
2987 text += wxT(".");
2988 }
2989
2990 return text;
2991 }
2992
2993 /// Allocate or reuse a line object
2994 wxRichTextLine* wxRichTextParagraph::AllocateLine(int pos)
2995 {
2996 if (pos < (int) m_cachedLines.GetCount())
2997 {
2998 wxRichTextLine* line = m_cachedLines.Item(pos)->GetData();
2999 line->Init(this);
3000 return line;
3001 }
3002 else
3003 {
3004 wxRichTextLine* line = new wxRichTextLine(this);
3005 m_cachedLines.Append(line);
3006 return line;
3007 }
3008 }
3009
3010 /// Clear remaining unused line objects, if any
3011 bool wxRichTextParagraph::ClearUnusedLines(int lineCount)
3012 {
3013 int cachedLineCount = m_cachedLines.GetCount();
3014 if ((int) cachedLineCount > lineCount)
3015 {
3016 for (int i = 0; i < (int) (cachedLineCount - lineCount); i ++)
3017 {
3018 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetLast();
3019 wxRichTextLine* line = node->GetData();
3020 m_cachedLines.Erase(node);
3021 delete line;
3022 }
3023 }
3024 return true;
3025 }
3026
3027 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3028 /// retrieve the actual style.
3029 wxTextAttrEx wxRichTextParagraph::GetCombinedAttributes(const wxTextAttr& contentStyle) const
3030 {
3031 wxTextAttrEx attr;
3032 wxRichTextBuffer* buf = wxDynamicCast(GetParent(), wxRichTextBuffer);
3033 if (buf)
3034 {
3035 attr = buf->GetBasicStyle();
3036 wxRichTextApplyStyle(attr, GetAttributes());
3037 }
3038 else
3039 attr = GetAttributes();
3040
3041 wxRichTextApplyStyle(attr, contentStyle);
3042 return attr;
3043 }
3044
3045 /// Get combined attributes of the base style and paragraph style.
3046 wxTextAttrEx wxRichTextParagraph::GetCombinedAttributes() const
3047 {
3048 wxTextAttrEx attr;
3049 wxRichTextBuffer* buf = wxDynamicCast(GetParent(), wxRichTextBuffer);
3050 if (buf)
3051 {
3052 attr = buf->GetBasicStyle();
3053 wxRichTextApplyStyle(attr, GetAttributes());
3054 }
3055 else
3056 attr = GetAttributes();
3057
3058 return attr;
3059 }
3060
3061 /*!
3062 * wxRichTextLine
3063 * This object represents a line in a paragraph, and stores
3064 * offsets from the start of the paragraph representing the
3065 * start and end positions of the line.
3066 */
3067
3068 wxRichTextLine::wxRichTextLine(wxRichTextParagraph* parent)
3069 {
3070 Init(parent);
3071 }
3072
3073 /// Initialisation
3074 void wxRichTextLine::Init(wxRichTextParagraph* parent)
3075 {
3076 m_parent = parent;
3077 m_range.SetRange(-1, -1);
3078 m_pos = wxPoint(0, 0);
3079 m_size = wxSize(0, 0);
3080 m_descent = 0;
3081 }
3082
3083 /// Copy
3084 void wxRichTextLine::Copy(const wxRichTextLine& obj)
3085 {
3086 m_range = obj.m_range;
3087 }
3088
3089 /// Get the absolute object position
3090 wxPoint wxRichTextLine::GetAbsolutePosition() const
3091 {
3092 return m_parent->GetPosition() + m_pos;
3093 }
3094
3095 /// Get the absolute range
3096 wxRichTextRange wxRichTextLine::GetAbsoluteRange() const
3097 {
3098 wxRichTextRange range(m_range.GetStart() + m_parent->GetRange().GetStart(), 0);
3099 range.SetEnd(range.GetStart() + m_range.GetLength()-1);
3100 return range;
3101 }
3102
3103 /*!
3104 * wxRichTextPlainText
3105 * This object represents a single piece of text.
3106 */
3107
3108 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText, wxRichTextObject)
3109
3110 wxRichTextPlainText::wxRichTextPlainText(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* style):
3111 wxRichTextObject(parent)
3112 {
3113 if (parent && !style)
3114 SetAttributes(parent->GetAttributes());
3115 if (style)
3116 SetAttributes(*style);
3117
3118 m_text = text;
3119 }
3120
3121 /// Draw the item
3122 bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int WXUNUSED(style))
3123 {
3124 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3125 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
3126 wxASSERT (para != NULL);
3127
3128 wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
3129 #else
3130 wxTextAttrEx textAttr(GetAttributes());
3131 #endif
3132
3133 int offset = GetRange().GetStart();
3134
3135 long len = range.GetLength();
3136 wxString stringChunk = m_text.Mid(range.GetStart() - offset, (size_t) len);
3137
3138 int charHeight = dc.GetCharHeight();
3139
3140 int x = rect.x;
3141 int y = rect.y + (rect.height - charHeight - (descent - m_descent));
3142
3143 // Test for the optimized situations where all is selected, or none
3144 // is selected.
3145
3146 if (textAttr.GetFont().Ok())
3147 dc.SetFont(textAttr.GetFont());
3148
3149 // (a) All selected.
3150 if (selectionRange.GetStart() <= range.GetStart() && selectionRange.GetEnd() >= range.GetEnd())
3151 {
3152 DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, true);
3153 }
3154 // (b) None selected.
3155 else if (selectionRange.GetEnd() < range.GetStart() || selectionRange.GetStart() > range.GetEnd())
3156 {
3157 // Draw all unselected
3158 DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, false);
3159 }
3160 else
3161 {
3162 // (c) Part selected, part not
3163 // Let's draw unselected chunk, selected chunk, then unselected chunk.
3164
3165 dc.SetBackgroundMode(wxTRANSPARENT);
3166
3167 // 1. Initial unselected chunk, if any, up until start of selection.
3168 if (selectionRange.GetStart() > range.GetStart() && selectionRange.GetStart() <= range.GetEnd())
3169 {
3170 int r1 = range.GetStart();
3171 int s1 = selectionRange.GetStart()-1;
3172 int fragmentLen = s1 - r1 + 1;
3173 if (fragmentLen < 0)
3174 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1 - offset), (int)fragmentLen);
3175 wxString stringFragment = m_text.Mid(r1 - offset, fragmentLen);
3176
3177 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
3178 }
3179
3180 // 2. Selected chunk, if any.
3181 if (selectionRange.GetEnd() >= range.GetStart())
3182 {
3183 int s1 = wxMax(selectionRange.GetStart(), range.GetStart());
3184 int s2 = wxMin(selectionRange.GetEnd(), range.GetEnd());
3185
3186 int fragmentLen = s2 - s1 + 1;
3187 if (fragmentLen < 0)
3188 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1 - offset), (int)fragmentLen);
3189 wxString stringFragment = m_text.Mid(s1 - offset, fragmentLen);
3190
3191 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, true);
3192 }
3193
3194 // 3. Remaining unselected chunk, if any
3195 if (selectionRange.GetEnd() < range.GetEnd())
3196 {
3197 int s2 = wxMin(selectionRange.GetEnd()+1, range.GetEnd());
3198 int r2 = range.GetEnd();
3199
3200 int fragmentLen = r2 - s2 + 1;
3201 if (fragmentLen < 0)
3202 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2 - offset), (int)fragmentLen);
3203 wxString stringFragment = m_text.Mid(s2 - offset, fragmentLen);
3204
3205 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
3206 }
3207 }
3208
3209 return true;
3210 }
3211
3212 bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect,wxString& str, wxCoord& x, wxCoord& y, bool selected)
3213 {
3214 wxArrayInt tab_array = attr.GetTabs();
3215 if (tab_array.IsEmpty())
3216 {
3217 // create a default tab list at 10 mm each.
3218 for (int i = 0; i < 20; ++i)
3219 {
3220 tab_array.Add(i*100);
3221 }
3222 }
3223 int map_mode = dc.GetMapMode();
3224 dc.SetMapMode(wxMM_LOMETRIC );
3225 int num_tabs = tab_array.GetCount();
3226 for (int i = 0; i < num_tabs; ++i)
3227 {
3228 tab_array[i] = dc.LogicalToDeviceXRel(tab_array[i]);
3229 }
3230
3231 dc.SetMapMode(map_mode );
3232 int next_tab_pos = -1;
3233 int tab_pos = -1;
3234 wxCoord w, h;
3235
3236 if(selected)
3237 {
3238 dc.SetBrush(*wxBLACK_BRUSH);
3239 dc.SetPen(*wxBLACK_PEN);
3240 dc.SetTextForeground(*wxWHITE);
3241 dc.SetBackgroundMode(wxTRANSPARENT);
3242 }
3243 else
3244 {
3245 dc.SetTextForeground(attr.GetTextColour());
3246 dc.SetBackgroundMode(wxTRANSPARENT);
3247 }
3248
3249 while (str.Find(wxT('\t')) >= 0)
3250 {
3251 // the string has a tab
3252 // break up the string at the Tab
3253 wxString stringChunk = str.BeforeFirst(wxT('\t'));
3254 str = str.AfterFirst(wxT('\t'));
3255 dc.GetTextExtent(stringChunk, & w, & h);
3256 tab_pos = x + w;
3257 bool not_found = true;
3258 for (int i = 0; i < num_tabs && not_found; ++i)
3259 {
3260 next_tab_pos = tab_array.Item(i);
3261 if (next_tab_pos > tab_pos)
3262 {
3263 not_found = false;
3264 if (selected)
3265 {
3266 w = next_tab_pos - x;
3267 wxRect selRect(x, rect.y, w, rect.GetHeight());
3268 dc.DrawRectangle(selRect);
3269 }
3270 dc.DrawText(stringChunk, x, y);
3271 x = next_tab_pos;
3272 }
3273 }
3274 }
3275
3276 dc.GetTextExtent(str, & w, & h);
3277 if (selected)
3278 {
3279 wxRect selRect(x, rect.y, w, rect.GetHeight());
3280 dc.DrawRectangle(selRect);
3281 }
3282 dc.DrawText(str, x, y);
3283 x += w;
3284 return true;
3285
3286 }
3287
3288 /// Lay the item out
3289 bool wxRichTextPlainText::Layout(wxDC& dc, const wxRect& WXUNUSED(rect), int WXUNUSED(style))
3290 {
3291 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3292 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
3293 wxASSERT (para != NULL);
3294
3295 wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
3296 #else
3297 wxTextAttrEx textAttr(GetAttributes());
3298 #endif
3299
3300 if (textAttr.GetFont().Ok())
3301 dc.SetFont(textAttr.GetFont());
3302
3303 wxCoord w, h;
3304 dc.GetTextExtent(m_text, & w, & h, & m_descent);
3305 m_size = wxSize(w, dc.GetCharHeight());
3306
3307 return true;
3308 }
3309
3310 /// Copy
3311 void wxRichTextPlainText::Copy(const wxRichTextPlainText& obj)
3312 {
3313 wxRichTextObject::Copy(obj);
3314
3315 m_text = obj.m_text;
3316 }
3317
3318 /// Get/set the object size for the given range. Returns false if the range
3319 /// is invalid for this object.
3320 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int WXUNUSED(flags), wxPoint position) const
3321 {
3322 if (!range.IsWithin(GetRange()))
3323 return false;
3324
3325 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3326 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
3327 wxASSERT (para != NULL);
3328
3329 wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
3330 #else
3331 wxTextAttrEx textAttr(GetAttributes());
3332 #endif
3333
3334 // Always assume unformatted text, since at this level we have no knowledge
3335 // of line breaks - and we don't need it, since we'll calculate size within
3336 // formatted text by doing it in chunks according to the line ranges
3337
3338 if (textAttr.GetFont().Ok())
3339 dc.SetFont(textAttr.GetFont());
3340
3341 int startPos = range.GetStart() - GetRange().GetStart();
3342 long len = range.GetLength();
3343 wxString stringChunk = m_text.Mid(startPos, (size_t) len);
3344 wxCoord w, h;
3345 int width = 0;
3346 if (stringChunk.Find(wxT('\t')) >= 0)
3347 {
3348 // the string has a tab
3349 wxArrayInt tab_array = textAttr.GetTabs();
3350 if (tab_array.IsEmpty())
3351 {
3352 // create a default tab list at 10 mm each.
3353 for (int i = 0; i < 20; ++i)
3354 {
3355 tab_array.Add(i*100);
3356 }
3357 }
3358
3359 int map_mode = dc.GetMapMode();
3360 dc.SetMapMode(wxMM_LOMETRIC );
3361 int num_tabs = tab_array.GetCount();
3362
3363 for (int i = 0; i < num_tabs; ++i)
3364 {
3365 tab_array[i] = dc.LogicalToDeviceXRel(tab_array[i]);
3366 }
3367 dc.SetMapMode(map_mode );
3368 int next_tab_pos = -1;
3369
3370 while (stringChunk.Find(wxT('\t')) >= 0)
3371 {
3372 // the string has a tab
3373 // break up the string at the Tab
3374 wxString stringFragment = stringChunk.BeforeFirst(wxT('\t'));
3375 stringChunk = stringChunk.AfterFirst(wxT('\t'));
3376 dc.GetTextExtent(stringFragment, & w, & h);
3377 width += w;
3378 int absolute_width = width + position.x;
3379 bool not_found = true;
3380 for (int i = 0; i < num_tabs && not_found; ++i)
3381 {
3382 next_tab_pos = tab_array.Item(i);
3383 if (next_tab_pos > absolute_width)
3384 {
3385 not_found = false;
3386 width = next_tab_pos - position.x;
3387 }
3388 }
3389 }
3390 }
3391 dc.GetTextExtent(stringChunk, & w, & h, & descent);
3392 width += w;
3393 size = wxSize(width, dc.GetCharHeight());
3394
3395 return true;
3396 }
3397
3398 /// Do a split, returning an object containing the second part, and setting
3399 /// the first part in 'this'.
3400 wxRichTextObject* wxRichTextPlainText::DoSplit(long pos)
3401 {
3402 int index = pos - GetRange().GetStart();
3403 if (index < 0 || index >= (int) m_text.length())
3404 return NULL;
3405
3406 wxString firstPart = m_text.Mid(0, index);
3407 wxString secondPart = m_text.Mid(index);
3408
3409 m_text = firstPart;
3410
3411 wxRichTextPlainText* newObject = new wxRichTextPlainText(secondPart);
3412 newObject->SetAttributes(GetAttributes());
3413
3414 newObject->SetRange(wxRichTextRange(pos, GetRange().GetEnd()));
3415 GetRange().SetEnd(pos-1);
3416
3417 return newObject;
3418 }
3419
3420 /// Calculate range
3421 void wxRichTextPlainText::CalculateRange(long start, long& end)
3422 {
3423 end = start + m_text.length() - 1;
3424 m_range.SetRange(start, end);
3425 }
3426
3427 /// Delete range
3428 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange& range)
3429 {
3430 wxRichTextRange r = range;
3431
3432 r.LimitTo(GetRange());
3433
3434 if (r.GetStart() == GetRange().GetStart() && r.GetEnd() == GetRange().GetEnd())
3435 {
3436 m_text.Empty();
3437 return true;
3438 }
3439
3440 long startIndex = r.GetStart() - GetRange().GetStart();
3441 long len = r.GetLength();
3442
3443 m_text = m_text.Mid(0, startIndex) + m_text.Mid(startIndex+len);
3444 return true;
3445 }
3446
3447 /// Get text for the given range.
3448 wxString wxRichTextPlainText::GetTextForRange(const wxRichTextRange& range) const
3449 {
3450 wxRichTextRange r = range;
3451
3452 r.LimitTo(GetRange());
3453
3454 long startIndex = r.GetStart() - GetRange().GetStart();
3455 long len = r.GetLength();
3456
3457 return m_text.Mid(startIndex, len);
3458 }
3459
3460 /// Returns true if this object can merge itself with the given one.
3461 bool wxRichTextPlainText::CanMerge(wxRichTextObject* object) const
3462 {
3463 return object->GetClassInfo() == CLASSINFO(wxRichTextPlainText) &&
3464 (m_text.empty() || wxTextAttrEq(GetAttributes(), object->GetAttributes()));
3465 }
3466
3467 /// Returns true if this object merged itself with the given one.
3468 /// The calling code will then delete the given object.
3469 bool wxRichTextPlainText::Merge(wxRichTextObject* object)
3470 {
3471 wxRichTextPlainText* textObject = wxDynamicCast(object, wxRichTextPlainText);
3472 wxASSERT( textObject != NULL );
3473
3474 if (textObject)
3475 {
3476 m_text += textObject->GetText();
3477 return true;
3478 }
3479 else
3480 return false;
3481 }
3482
3483 /// Dump to output stream for debugging
3484 void wxRichTextPlainText::Dump(wxTextOutputStream& stream)
3485 {
3486 wxRichTextObject::Dump(stream);
3487 stream << m_text << wxT("\n");
3488 }
3489
3490 /*!
3491 * wxRichTextBuffer
3492 * This is a kind of box, used to represent the whole buffer
3493 */
3494
3495 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer, wxRichTextParagraphLayoutBox)
3496
3497 wxList wxRichTextBuffer::sm_handlers;
3498
3499 /// Initialisation
3500 void wxRichTextBuffer::Init()
3501 {
3502 m_commandProcessor = new wxCommandProcessor;
3503 m_styleSheet = NULL;
3504 m_modified = false;
3505 m_batchedCommandDepth = 0;
3506 m_batchedCommand = NULL;
3507 m_suppressUndo = 0;
3508 }
3509
3510 /// Initialisation
3511 wxRichTextBuffer::~wxRichTextBuffer()
3512 {
3513 delete m_commandProcessor;
3514 delete m_batchedCommand;
3515
3516 ClearStyleStack();
3517 }
3518
3519 void wxRichTextBuffer::Clear()
3520 {
3521 DeleteChildren();
3522 GetCommandProcessor()->ClearCommands();
3523 Modify(false);
3524 Invalidate(wxRICHTEXT_ALL);
3525 }
3526
3527 void wxRichTextBuffer::Reset()
3528 {
3529 DeleteChildren();
3530 AddParagraph(wxEmptyString);
3531 GetCommandProcessor()->ClearCommands();
3532 Modify(false);
3533 Invalidate(wxRICHTEXT_ALL);
3534 }
3535
3536 void wxRichTextBuffer::Copy(const wxRichTextBuffer& obj)
3537 {
3538 wxRichTextParagraphLayoutBox::Copy(obj);
3539
3540 m_styleSheet = obj.m_styleSheet;
3541 m_modified = obj.m_modified;
3542 m_batchedCommandDepth = obj.m_batchedCommandDepth;
3543 m_batchedCommand = obj.m_batchedCommand;
3544 m_suppressUndo = obj.m_suppressUndo;
3545 }
3546
3547 /// Submit command to insert paragraphs
3548 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos, const wxRichTextParagraphLayoutBox& paragraphs, wxRichTextCtrl* ctrl, int flags)
3549 {
3550 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
3551
3552 wxTextAttrEx* p = NULL;
3553 wxTextAttrEx paraAttr;
3554 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
3555 {
3556 paraAttr = GetStyleForNewParagraph(pos);
3557 if (!paraAttr.IsDefault())
3558 p = & paraAttr;
3559 }
3560
3561 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3562 wxTextAttrEx attr(GetDefaultStyle());
3563 #else
3564 wxTextAttrEx attr(GetBasicStyle());
3565 wxRichTextApplyStyle(attr, GetDefaultStyle());
3566 #endif
3567
3568 action->GetNewParagraphs() = paragraphs;
3569 action->SetPosition(pos);
3570
3571 // Set the range we'll need to delete in Undo
3572 action->SetRange(wxRichTextRange(pos, pos + paragraphs.GetRange().GetEnd() - 1));
3573
3574 SubmitAction(action);
3575
3576 return true;
3577 }
3578
3579 /// Submit command to insert the given text
3580 bool wxRichTextBuffer::InsertTextWithUndo(long pos, const wxString& text, wxRichTextCtrl* ctrl, int flags)
3581 {
3582 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
3583
3584 wxTextAttrEx* p = NULL;
3585 wxTextAttrEx paraAttr;
3586 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
3587 {
3588 paraAttr = GetStyleForNewParagraph(pos);
3589 if (!paraAttr.IsDefault())
3590 p = & paraAttr;
3591 }
3592
3593 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3594 wxTextAttrEx attr(GetDefaultStyle());
3595 #else
3596 wxTextAttrEx attr(GetBasicStyle());
3597 wxRichTextApplyStyle(attr, GetDefaultStyle());
3598 #endif
3599
3600 action->GetNewParagraphs().AddParagraphs(text, p);
3601
3602 int length = action->GetNewParagraphs().GetRange().GetLength();
3603
3604 if (text.length() > 0 && text.Last() != wxT('\n'))
3605 {
3606 // Don't count the newline when undoing
3607 length --;
3608 action->GetNewParagraphs().SetPartialParagraph(true);
3609 }
3610
3611 action->SetPosition(pos);
3612
3613 // Set the range we'll need to delete in Undo
3614 action->SetRange(wxRichTextRange(pos, pos + length - 1));
3615
3616 SubmitAction(action);
3617
3618 return true;
3619 }
3620
3621 /// Submit command to insert the given text
3622 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int flags)
3623 {
3624 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
3625
3626 wxTextAttrEx* p = NULL;
3627 wxTextAttrEx paraAttr;
3628 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
3629 {
3630 paraAttr = GetStyleForNewParagraph(pos);
3631 if (!paraAttr.IsDefault())
3632 p = & paraAttr;
3633 }
3634
3635 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3636 wxTextAttrEx attr(GetDefaultStyle());
3637 #else
3638 wxTextAttrEx attr(GetBasicStyle());
3639 wxRichTextApplyStyle(attr, GetDefaultStyle());
3640 #endif
3641
3642 wxRichTextParagraph* newPara = new wxRichTextParagraph(wxEmptyString, this, & attr);
3643 action->GetNewParagraphs().AppendChild(newPara);
3644 action->GetNewParagraphs().UpdateRanges();
3645 action->GetNewParagraphs().SetPartialParagraph(false);
3646 action->SetPosition(pos);
3647
3648 if (p)
3649 newPara->SetAttributes(*p);
3650
3651 // Set the range we'll need to delete in Undo
3652 action->SetRange(wxRichTextRange(pos, pos));
3653
3654 SubmitAction(action);
3655
3656 return true;
3657 }
3658
3659 /// Submit command to insert the given image
3660 bool wxRichTextBuffer::InsertImageWithUndo(long pos, const wxRichTextImageBlock& imageBlock, wxRichTextCtrl* ctrl, int flags)
3661 {
3662 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, ctrl, false);
3663
3664 wxTextAttrEx* p = NULL;
3665 wxTextAttrEx paraAttr;
3666 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
3667 {
3668 paraAttr = GetStyleForNewParagraph(pos);
3669 if (!paraAttr.IsDefault())
3670 p = & paraAttr;
3671 }
3672
3673 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3674 wxTextAttrEx attr(GetDefaultStyle());
3675 #else
3676 wxTextAttrEx attr(GetBasicStyle());
3677 wxRichTextApplyStyle(attr, GetDefaultStyle());
3678 #endif
3679
3680 wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr);
3681 if (p)
3682 newPara->SetAttributes(*p);
3683
3684 wxRichTextImage* imageObject = new wxRichTextImage(imageBlock, newPara);
3685 newPara->AppendChild(imageObject);
3686 action->GetNewParagraphs().AppendChild(newPara);
3687 action->GetNewParagraphs().UpdateRanges();
3688
3689 action->GetNewParagraphs().SetPartialParagraph(true);
3690
3691 action->SetPosition(pos);
3692
3693 // Set the range we'll need to delete in Undo
3694 action->SetRange(wxRichTextRange(pos, pos));
3695
3696 SubmitAction(action);
3697
3698 return true;
3699 }
3700
3701 /// Get the style that is appropriate for a new paragraph at this position.
3702 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
3703 /// style.
3704 wxRichTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPosition) const
3705 {
3706 wxRichTextParagraph* para = GetParagraphAtPosition(pos, caretPosition);
3707 if (para)
3708 {
3709 if (!para->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
3710 {
3711 wxRichTextParagraphStyleDefinition* paraDef = GetStyleSheet()->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
3712 if (paraDef && !paraDef->GetNextStyle().IsEmpty())
3713 {
3714 wxRichTextParagraphStyleDefinition* nextParaDef = GetStyleSheet()->FindParagraphStyle(paraDef->GetNextStyle());
3715 if (nextParaDef)
3716 return nextParaDef->GetStyle();
3717 }
3718 }
3719 wxRichTextAttr attr(para->GetAttributes());
3720 int flags = attr.GetFlags();
3721
3722 // Eliminate character styles
3723 flags &= ( (~ wxTEXT_ATTR_FONT) |
3724 (~ wxTEXT_ATTR_TEXT_COLOUR) |
3725 (~ wxTEXT_ATTR_BACKGROUND_COLOUR) );
3726 attr.SetFlags(flags);
3727
3728 return attr;
3729 }
3730 else
3731 return wxRichTextAttr();
3732 }
3733
3734 /// Submit command to delete this range
3735 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange& range, long initialCaretPosition, long WXUNUSED(newCaretPositon), wxRichTextCtrl* ctrl)
3736 {
3737 wxRichTextAction* action = new wxRichTextAction(NULL, _("Delete"), wxRICHTEXT_DELETE, this, ctrl);
3738
3739 action->SetPosition(initialCaretPosition);
3740
3741 // Set the range to delete
3742 action->SetRange(range);
3743
3744 // Copy the fragment that we'll need to restore in Undo
3745 CopyFragment(range, action->GetOldParagraphs());
3746
3747 // Special case: if there is only one (non-partial) paragraph,
3748 // we must save the *next* paragraph's style, because that
3749 // is the style we must apply when inserting the content back
3750 // when undoing the delete. (This is because we're merging the
3751 // paragraph with the previous paragraph and throwing away
3752 // the style, and we need to restore it.)
3753 if (!action->GetOldParagraphs().GetPartialParagraph() && action->GetOldParagraphs().GetChildCount() == 1)
3754 {
3755 wxRichTextParagraph* lastPara = GetParagraphAtPosition(range.GetStart());
3756 if (lastPara)
3757 {
3758 wxRichTextParagraph* nextPara = GetParagraphAtPosition(range.GetEnd()+1);
3759 if (nextPara)
3760 {
3761 wxRichTextParagraph* para = (wxRichTextParagraph*) action->GetOldParagraphs().GetChild(0);
3762 para->SetAttributes(nextPara->GetAttributes());
3763 }
3764 }
3765 }
3766
3767 SubmitAction(action);
3768
3769 return true;
3770 }
3771
3772 /// Collapse undo/redo commands
3773 bool wxRichTextBuffer::BeginBatchUndo(const wxString& cmdName)
3774 {
3775 if (m_batchedCommandDepth == 0)
3776 {
3777 wxASSERT(m_batchedCommand == NULL);
3778 if (m_batchedCommand)
3779 {
3780 GetCommandProcessor()->Submit(m_batchedCommand);
3781 }
3782 m_batchedCommand = new wxRichTextCommand(cmdName);
3783 }
3784
3785 m_batchedCommandDepth ++;
3786
3787 return true;
3788 }
3789
3790 /// Collapse undo/redo commands
3791 bool wxRichTextBuffer::EndBatchUndo()
3792 {
3793 m_batchedCommandDepth --;
3794
3795 wxASSERT(m_batchedCommandDepth >= 0);
3796 wxASSERT(m_batchedCommand != NULL);
3797
3798 if (m_batchedCommandDepth == 0)
3799 {
3800 GetCommandProcessor()->Submit(m_batchedCommand);
3801 m_batchedCommand = NULL;
3802 }
3803
3804 return true;
3805 }
3806
3807 /// Submit immediately, or delay according to whether collapsing is on
3808 bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action)
3809 {
3810 if (BatchingUndo() && m_batchedCommand && !SuppressingUndo())
3811 m_batchedCommand->AddAction(action);
3812 else
3813 {
3814 wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName());
3815 cmd->AddAction(action);
3816
3817 // Only store it if we're not suppressing undo.
3818 return GetCommandProcessor()->Submit(cmd, !SuppressingUndo());
3819 }
3820
3821 return true;
3822 }
3823
3824 /// Begin suppressing undo/redo commands.
3825 bool wxRichTextBuffer::BeginSuppressUndo()
3826 {
3827 m_suppressUndo ++;
3828
3829 return true;
3830 }
3831
3832 /// End suppressing undo/redo commands.
3833 bool wxRichTextBuffer::EndSuppressUndo()
3834 {
3835 m_suppressUndo --;
3836
3837 return true;
3838 }
3839
3840 /// Begin using a style
3841 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx& style)
3842 {
3843 wxTextAttrEx newStyle(GetDefaultStyle());
3844
3845 // Save the old default style
3846 m_attributeStack.Append((wxObject*) new wxTextAttrEx(GetDefaultStyle()));
3847
3848 wxRichTextApplyStyle(newStyle, style);
3849 newStyle.SetFlags(style.GetFlags()|newStyle.GetFlags());
3850
3851 SetDefaultStyle(newStyle);
3852
3853 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
3854
3855 return true;
3856 }
3857
3858 /// End the style
3859 bool wxRichTextBuffer::EndStyle()
3860 {
3861 if (!m_attributeStack.GetFirst())
3862 {
3863 wxLogDebug(_("Too many EndStyle calls!"));
3864 return false;
3865 }
3866
3867 wxList::compatibility_iterator node = m_attributeStack.GetLast();
3868 wxTextAttrEx* attr = (wxTextAttrEx*)node->GetData();
3869 m_attributeStack.Erase(node);
3870
3871 SetDefaultStyle(*attr);
3872
3873 delete attr;
3874 return true;
3875 }
3876
3877 /// End all styles
3878 bool wxRichTextBuffer::EndAllStyles()
3879 {
3880 while (m_attributeStack.GetCount() != 0)
3881 EndStyle();
3882 return true;
3883 }
3884
3885 /// Clear the style stack
3886 void wxRichTextBuffer::ClearStyleStack()
3887 {
3888 for (wxList::compatibility_iterator node = m_attributeStack.GetFirst(); node; node = node->GetNext())
3889 delete (wxTextAttrEx*) node->GetData();
3890 m_attributeStack.Clear();
3891 }
3892
3893 /// Begin using bold
3894 bool wxRichTextBuffer::BeginBold()
3895 {
3896 wxFont font(GetBasicStyle().GetFont());
3897 font.SetWeight(wxBOLD);
3898
3899 wxTextAttrEx attr;
3900 attr.SetFont(font,wxTEXT_ATTR_FONT_WEIGHT);
3901
3902 return BeginStyle(attr);
3903 }
3904
3905 /// Begin using italic
3906 bool wxRichTextBuffer::BeginItalic()
3907 {
3908 wxFont font(GetBasicStyle().GetFont());
3909 font.SetStyle(wxITALIC);
3910
3911 wxTextAttrEx attr;
3912 attr.SetFont(font, wxTEXT_ATTR_FONT_ITALIC);
3913
3914 return BeginStyle(attr);
3915 }
3916
3917 /// Begin using underline
3918 bool wxRichTextBuffer::BeginUnderline()
3919 {
3920 wxFont font(GetBasicStyle().GetFont());
3921 font.SetUnderlined(true);
3922
3923 wxTextAttrEx attr;
3924 attr.SetFont(font, wxTEXT_ATTR_FONT_UNDERLINE);
3925
3926 return BeginStyle(attr);
3927 }
3928
3929 /// Begin using point size
3930 bool wxRichTextBuffer::BeginFontSize(int pointSize)
3931 {
3932 wxFont font(GetBasicStyle().GetFont());
3933 font.SetPointSize(pointSize);
3934
3935 wxTextAttrEx attr;
3936 attr.SetFont(font, wxTEXT_ATTR_FONT_SIZE);
3937
3938 return BeginStyle(attr);
3939 }
3940
3941 /// Begin using this font
3942 bool wxRichTextBuffer::BeginFont(const wxFont& font)
3943 {
3944 wxTextAttrEx attr;
3945 attr.SetFlags(wxTEXT_ATTR_FONT);
3946 attr.SetFont(font);
3947
3948 return BeginStyle(attr);
3949 }
3950
3951 /// Begin using this colour
3952 bool wxRichTextBuffer::BeginTextColour(const wxColour& colour)
3953 {
3954 wxTextAttrEx attr;
3955 attr.SetFlags(wxTEXT_ATTR_TEXT_COLOUR);
3956 attr.SetTextColour(colour);
3957
3958 return BeginStyle(attr);
3959 }
3960
3961 /// Begin using alignment
3962 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment)
3963 {
3964 wxTextAttrEx attr;
3965 attr.SetFlags(wxTEXT_ATTR_ALIGNMENT);
3966 attr.SetAlignment(alignment);
3967
3968 return BeginStyle(attr);
3969 }
3970
3971 /// Begin left indent
3972 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent, int leftSubIndent)
3973 {
3974 wxTextAttrEx attr;
3975 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
3976 attr.SetLeftIndent(leftIndent, leftSubIndent);
3977
3978 return BeginStyle(attr);
3979 }
3980
3981 /// Begin right indent
3982 bool wxRichTextBuffer::BeginRightIndent(int rightIndent)
3983 {
3984 wxTextAttrEx attr;
3985 attr.SetFlags(wxTEXT_ATTR_RIGHT_INDENT);
3986 attr.SetRightIndent(rightIndent);
3987
3988 return BeginStyle(attr);
3989 }
3990
3991 /// Begin paragraph spacing
3992 bool wxRichTextBuffer::BeginParagraphSpacing(int before, int after)
3993 {
3994 long flags = 0;
3995 if (before != 0)
3996 flags |= wxTEXT_ATTR_PARA_SPACING_BEFORE;
3997 if (after != 0)
3998 flags |= wxTEXT_ATTR_PARA_SPACING_AFTER;
3999
4000 wxTextAttrEx attr;
4001 attr.SetFlags(flags);
4002 attr.SetParagraphSpacingBefore(before);
4003 attr.SetParagraphSpacingAfter(after);
4004
4005 return BeginStyle(attr);
4006 }
4007
4008 /// Begin line spacing
4009 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing)
4010 {
4011 wxTextAttrEx attr;
4012 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
4013 attr.SetLineSpacing(lineSpacing);
4014
4015 return BeginStyle(attr);
4016 }
4017
4018 /// Begin numbered bullet
4019 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber, int leftIndent, int leftSubIndent, int bulletStyle)
4020 {
4021 wxTextAttrEx attr;
4022 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER|wxTEXT_ATTR_LEFT_INDENT);
4023 attr.SetBulletStyle(bulletStyle);
4024 attr.SetBulletNumber(bulletNumber);
4025 attr.SetLeftIndent(leftIndent, leftSubIndent);
4026
4027 return BeginStyle(attr);
4028 }
4029
4030 /// Begin symbol bullet
4031 bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol, int leftIndent, int leftSubIndent, int bulletStyle)
4032 {
4033 wxTextAttrEx attr;
4034 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_SYMBOL|wxTEXT_ATTR_LEFT_INDENT);
4035 attr.SetBulletStyle(bulletStyle);
4036 attr.SetLeftIndent(leftIndent, leftSubIndent);
4037 attr.SetBulletSymbol(symbol);
4038
4039 return BeginStyle(attr);
4040 }
4041
4042 /// Begin named character style
4043 bool wxRichTextBuffer::BeginCharacterStyle(const wxString& characterStyle)
4044 {
4045 if (GetStyleSheet())
4046 {
4047 wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle);
4048 if (def)
4049 {
4050 wxTextAttrEx attr;
4051 def->GetStyle().CopyTo(attr);
4052 return BeginStyle(attr);
4053 }
4054 }
4055 return false;
4056 }
4057
4058 /// Begin named paragraph style
4059 bool wxRichTextBuffer::BeginParagraphStyle(const wxString& paragraphStyle)
4060 {
4061 if (GetStyleSheet())
4062 {
4063 wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(paragraphStyle);
4064 if (def)
4065 {
4066 wxTextAttrEx attr;
4067 def->GetStyle().CopyTo(attr);
4068 return BeginStyle(attr);
4069 }
4070 }
4071 return false;
4072 }
4073
4074 /// Adds a handler to the end
4075 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler *handler)
4076 {
4077 sm_handlers.Append(handler);
4078 }
4079
4080 /// Inserts a handler at the front
4081 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler *handler)
4082 {
4083 sm_handlers.Insert( handler );
4084 }
4085
4086 /// Removes a handler
4087 bool wxRichTextBuffer::RemoveHandler(const wxString& name)
4088 {
4089 wxRichTextFileHandler *handler = FindHandler(name);
4090 if (handler)
4091 {
4092 sm_handlers.DeleteObject(handler);
4093 delete handler;
4094 return true;
4095 }
4096 else
4097 return false;
4098 }
4099
4100 /// Finds a handler by filename or, if supplied, type
4101 wxRichTextFileHandler *wxRichTextBuffer::FindHandlerFilenameOrType(const wxString& filename, int imageType)
4102 {
4103 if (imageType != wxRICHTEXT_TYPE_ANY)
4104 return FindHandler(imageType);
4105 else if (!filename.IsEmpty())
4106 {
4107 wxString path, file, ext;
4108 wxSplitPath(filename, & path, & file, & ext);
4109 return FindHandler(ext, imageType);
4110 }
4111 else
4112 return NULL;
4113 }
4114
4115
4116 /// Finds a handler by name
4117 wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& name)
4118 {
4119 wxList::compatibility_iterator node = sm_handlers.GetFirst();
4120 while (node)
4121 {
4122 wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData();
4123 if (handler->GetName().Lower() == name.Lower()) return handler;
4124
4125 node = node->GetNext();
4126 }
4127 return NULL;
4128 }
4129
4130 /// Finds a handler by extension and type
4131 wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& extension, int type)
4132 {
4133 wxList::compatibility_iterator node = sm_handlers.GetFirst();
4134 while (node)
4135 {
4136 wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData();
4137 if ( handler->GetExtension().Lower() == extension.Lower() &&
4138 (type == wxRICHTEXT_TYPE_ANY || handler->GetType() == type) )
4139 return handler;
4140 node = node->GetNext();
4141 }
4142 return 0;
4143 }
4144
4145 /// Finds a handler by type
4146 wxRichTextFileHandler* wxRichTextBuffer::FindHandler(int type)
4147 {
4148 wxList::compatibility_iterator node = sm_handlers.GetFirst();
4149 while (node)
4150 {
4151 wxRichTextFileHandler *handler = (wxRichTextFileHandler *)node->GetData();
4152 if (handler->GetType() == type) return handler;
4153 node = node->GetNext();
4154 }
4155 return NULL;
4156 }
4157
4158 void wxRichTextBuffer::InitStandardHandlers()
4159 {
4160 if (!FindHandler(wxRICHTEXT_TYPE_TEXT))
4161 AddHandler(new wxRichTextPlainTextHandler);
4162 }
4163
4164 void wxRichTextBuffer::CleanUpHandlers()
4165 {
4166 wxList::compatibility_iterator node = sm_handlers.GetFirst();
4167 while (node)
4168 {
4169 wxRichTextFileHandler* handler = (wxRichTextFileHandler*)node->GetData();
4170 wxList::compatibility_iterator next = node->GetNext();
4171 delete handler;
4172 node = next;
4173 }
4174
4175 sm_handlers.Clear();
4176 }
4177
4178 wxString wxRichTextBuffer::GetExtWildcard(bool combine, bool save, wxArrayInt* types)
4179 {
4180 if (types)
4181 types->Clear();
4182
4183 wxString wildcard;
4184
4185 wxList::compatibility_iterator node = GetHandlers().GetFirst();
4186 int count = 0;
4187 while (node)
4188 {
4189 wxRichTextFileHandler* handler = (wxRichTextFileHandler*) node->GetData();
4190 if (handler->IsVisible() && ((save && handler->CanSave()) || !save && handler->CanLoad()))
4191 {
4192 if (combine)
4193 {
4194 if (count > 0)
4195 wildcard += wxT(";");
4196 wildcard += wxT("*.") + handler->GetExtension();
4197 }
4198 else
4199 {
4200 if (count > 0)
4201 wildcard += wxT("|");
4202 wildcard += handler->GetName();
4203 wildcard += wxT(" ");
4204 wildcard += _("files");
4205 wildcard += wxT(" (*.");
4206 wildcard += handler->GetExtension();
4207 wildcard += wxT(")|*.");
4208 wildcard += handler->GetExtension();
4209 if (types)
4210 types->Add(handler->GetType());
4211 }
4212 count ++;
4213 }
4214
4215 node = node->GetNext();
4216 }
4217
4218 if (combine)
4219 wildcard = wxT("(") + wildcard + wxT(")|") + wildcard;
4220 return wildcard;
4221 }
4222
4223 /// Load a file
4224 bool wxRichTextBuffer::LoadFile(const wxString& filename, int type)
4225 {
4226 wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type);
4227 if (handler)
4228 {
4229 SetDefaultStyle(wxTextAttrEx());
4230
4231 bool success = handler->LoadFile(this, filename);
4232 Invalidate(wxRICHTEXT_ALL);
4233 return success;
4234 }
4235 else
4236 return false;
4237 }
4238
4239 /// Save a file
4240 bool wxRichTextBuffer::SaveFile(const wxString& filename, int type)
4241 {
4242 wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type);
4243 if (handler)
4244 return handler->SaveFile(this, filename);
4245 else
4246 return false;
4247 }
4248
4249 /// Load from a stream
4250 bool wxRichTextBuffer::LoadFile(wxInputStream& stream, int type)
4251 {
4252 wxRichTextFileHandler* handler = FindHandler(type);
4253 if (handler)
4254 {
4255 SetDefaultStyle(wxTextAttrEx());
4256 bool success = handler->LoadFile(this, stream);
4257 Invalidate(wxRICHTEXT_ALL);
4258 return success;
4259 }
4260 else
4261 return false;
4262 }
4263
4264 /// Save to a stream
4265 bool wxRichTextBuffer::SaveFile(wxOutputStream& stream, int type)
4266 {
4267 wxRichTextFileHandler* handler = FindHandler(type);
4268 if (handler)
4269 return handler->SaveFile(this, stream);
4270 else
4271 return false;
4272 }
4273
4274 /// Copy the range to the clipboard
4275 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange& range)
4276 {
4277 bool success = false;
4278 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
4279
4280 if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open())
4281 {
4282 wxTheClipboard->Clear();
4283
4284 // Add composite object
4285
4286 wxDataObjectComposite* compositeObject = new wxDataObjectComposite();
4287
4288 {
4289 wxString text = GetTextForRange(range);
4290
4291 #ifdef __WXMSW__
4292 text = wxTextFile::Translate(text, wxTextFileType_Dos);
4293 #endif
4294
4295 compositeObject->Add(new wxTextDataObject(text), false /* not preferred */);
4296 }
4297
4298 // Add rich text buffer data object. This needs the XML handler to be present.
4299
4300 if (FindHandler(wxRICHTEXT_TYPE_XML))
4301 {
4302 wxRichTextBuffer* richTextBuf = new wxRichTextBuffer;
4303 CopyFragment(range, *richTextBuf);
4304
4305 compositeObject->Add(new wxRichTextBufferDataObject(richTextBuf), true /* preferred */);
4306 }
4307
4308 if (wxTheClipboard->SetData(compositeObject))
4309 success = true;
4310
4311 wxTheClipboard->Close();
4312 }
4313
4314 #else
4315 wxUnusedVar(range);
4316 #endif
4317 return success;
4318 }
4319
4320 /// Paste the clipboard content to the buffer
4321 bool wxRichTextBuffer::PasteFromClipboard(long position)
4322 {
4323 bool success = false;
4324 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
4325 if (CanPasteFromClipboard())
4326 {
4327 if (wxTheClipboard->Open())
4328 {
4329 if (wxTheClipboard->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
4330 {
4331 wxRichTextBufferDataObject data;
4332 wxTheClipboard->GetData(data);
4333 wxRichTextBuffer* richTextBuffer = data.GetRichTextBuffer();
4334 if (richTextBuffer)
4335 {
4336 InsertParagraphsWithUndo(position+1, *richTextBuffer, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
4337 delete richTextBuffer;
4338 }
4339 }
4340 else if (wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_UNICODETEXT))
4341 {
4342 wxTextDataObject data;
4343 wxTheClipboard->GetData(data);
4344 wxString text(data.GetText());
4345 text.Replace(_T("\r\n"), _T("\n"));
4346
4347 InsertTextWithUndo(position+1, text, GetRichTextCtrl());
4348
4349 success = true;
4350 }
4351 else if (wxTheClipboard->IsSupported(wxDF_BITMAP))
4352 {
4353 wxBitmapDataObject data;
4354 wxTheClipboard->GetData(data);
4355 wxBitmap bitmap(data.GetBitmap());
4356 wxImage image(bitmap.ConvertToImage());
4357
4358 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, GetRichTextCtrl(), false);
4359
4360 action->GetNewParagraphs().AddImage(image);
4361
4362 if (action->GetNewParagraphs().GetChildCount() == 1)
4363 action->GetNewParagraphs().SetPartialParagraph(true);
4364
4365 action->SetPosition(position);
4366
4367 // Set the range we'll need to delete in Undo
4368 action->SetRange(wxRichTextRange(position, position));
4369
4370 SubmitAction(action);
4371
4372 success = true;
4373 }
4374 wxTheClipboard->Close();
4375 }
4376 }
4377 #else
4378 wxUnusedVar(position);
4379 #endif
4380 return success;
4381 }
4382
4383 /// Can we paste from the clipboard?
4384 bool wxRichTextBuffer::CanPasteFromClipboard() const
4385 {
4386 bool canPaste = false;
4387 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
4388 if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open())
4389 {
4390 if (wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_UNICODETEXT) ||
4391 wxTheClipboard->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
4392 wxTheClipboard->IsSupported(wxDF_BITMAP))
4393 {
4394 canPaste = true;
4395 }
4396 wxTheClipboard->Close();
4397 }
4398 #endif
4399 return canPaste;
4400 }
4401
4402 /// Dumps contents of buffer for debugging purposes
4403 void wxRichTextBuffer::Dump()
4404 {
4405 wxString text;
4406 {
4407 wxStringOutputStream stream(& text);
4408 wxTextOutputStream textStream(stream);
4409 Dump(textStream);
4410 }
4411
4412 wxLogDebug(text);
4413 }
4414
4415
4416 /*
4417 * Module to initialise and clean up handlers
4418 */
4419
4420 class wxRichTextModule: public wxModule
4421 {
4422 DECLARE_DYNAMIC_CLASS(wxRichTextModule)
4423 public:
4424 wxRichTextModule() {}
4425 bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; };
4426 void OnExit() { wxRichTextBuffer::CleanUpHandlers(); };
4427 };
4428
4429 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule, wxModule)
4430
4431
4432 /*!
4433 * Commands for undo/redo
4434 *
4435 */
4436
4437 wxRichTextCommand::wxRichTextCommand(const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
4438 wxRichTextCtrl* ctrl, bool ignoreFirstTime): wxCommand(true, name)
4439 {
4440 /* wxRichTextAction* action = */ new wxRichTextAction(this, name, id, buffer, ctrl, ignoreFirstTime);
4441 }
4442
4443 wxRichTextCommand::wxRichTextCommand(const wxString& name): wxCommand(true, name)
4444 {
4445 }
4446
4447 wxRichTextCommand::~wxRichTextCommand()
4448 {
4449 ClearActions();
4450 }
4451
4452 void wxRichTextCommand::AddAction(wxRichTextAction* action)
4453 {
4454 if (!m_actions.Member(action))
4455 m_actions.Append(action);
4456 }
4457
4458 bool wxRichTextCommand::Do()
4459 {
4460 for (wxList::compatibility_iterator node = m_actions.GetFirst(); node; node = node->GetNext())
4461 {
4462 wxRichTextAction* action = (wxRichTextAction*) node->GetData();
4463 action->Do();
4464 }
4465
4466 return true;
4467 }
4468
4469 bool wxRichTextCommand::Undo()
4470 {
4471 for (wxList::compatibility_iterator node = m_actions.GetLast(); node; node = node->GetPrevious())
4472 {
4473 wxRichTextAction* action = (wxRichTextAction*) node->GetData();
4474 action->Undo();
4475 }
4476
4477 return true;
4478 }
4479
4480 void wxRichTextCommand::ClearActions()
4481 {
4482 WX_CLEAR_LIST(wxList, m_actions);
4483 }
4484
4485 /*!
4486 * Individual action
4487 *
4488 */
4489
4490 wxRichTextAction::wxRichTextAction(wxRichTextCommand* cmd, const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
4491 wxRichTextCtrl* ctrl, bool ignoreFirstTime)
4492 {
4493 m_buffer = buffer;
4494 m_ignoreThis = ignoreFirstTime;
4495 m_cmdId = id;
4496 m_position = -1;
4497 m_ctrl = ctrl;
4498 m_name = name;
4499 m_newParagraphs.SetDefaultStyle(buffer->GetDefaultStyle());
4500 m_newParagraphs.SetBasicStyle(buffer->GetBasicStyle());
4501 if (cmd)
4502 cmd->AddAction(this);
4503 }
4504
4505 wxRichTextAction::~wxRichTextAction()
4506 {
4507 }
4508
4509 bool wxRichTextAction::Do()
4510 {
4511 m_buffer->Modify(true);
4512
4513 switch (m_cmdId)
4514 {
4515 case wxRICHTEXT_INSERT:
4516 {
4517 m_buffer->InsertFragment(GetPosition(), m_newParagraphs);
4518 m_buffer->UpdateRanges();
4519 m_buffer->Invalidate(GetRange());
4520
4521 long newCaretPosition = GetPosition() + m_newParagraphs.GetRange().GetLength();
4522
4523 // Character position to caret position
4524 newCaretPosition --;
4525
4526 // Don't take into account the last newline
4527 if (m_newParagraphs.GetPartialParagraph())
4528 newCaretPosition --;
4529
4530 newCaretPosition = wxMin(newCaretPosition, (m_buffer->GetRange().GetEnd()-1));
4531
4532 UpdateAppearance(newCaretPosition, true /* send update event */);
4533
4534 break;
4535 }
4536 case wxRICHTEXT_DELETE:
4537 {
4538 m_buffer->DeleteRange(GetRange());
4539 m_buffer->UpdateRanges();
4540 m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4541
4542 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
4543
4544 break;
4545 }
4546 case wxRICHTEXT_CHANGE_STYLE:
4547 {
4548 ApplyParagraphs(GetNewParagraphs());
4549 m_buffer->Invalidate(GetRange());
4550
4551 UpdateAppearance(GetPosition());
4552
4553 break;
4554 }
4555 default:
4556 break;
4557 }
4558
4559 return true;
4560 }
4561
4562 bool wxRichTextAction::Undo()
4563 {
4564 m_buffer->Modify(true);
4565
4566 switch (m_cmdId)
4567 {
4568 case wxRICHTEXT_INSERT:
4569 {
4570 m_buffer->DeleteRange(GetRange());
4571 m_buffer->UpdateRanges();
4572 m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
4573
4574 long newCaretPosition = GetPosition() - 1;
4575 // if (m_newParagraphs.GetPartialParagraph())
4576 // newCaretPosition --;
4577
4578 UpdateAppearance(newCaretPosition, true /* send update event */);
4579
4580 break;
4581 }
4582 case wxRICHTEXT_DELETE:
4583 {
4584 m_buffer->InsertFragment(GetRange().GetStart(), m_oldParagraphs);
4585 m_buffer->UpdateRanges();
4586 m_buffer->Invalidate(GetRange());
4587
4588 UpdateAppearance(GetPosition(), true /* send update event */);
4589
4590 break;
4591 }
4592 case wxRICHTEXT_CHANGE_STYLE:
4593 {
4594 ApplyParagraphs(GetOldParagraphs());
4595 m_buffer->Invalidate(GetRange());
4596
4597 UpdateAppearance(GetPosition());
4598
4599 break;
4600 }
4601 default:
4602 break;
4603 }
4604
4605 return true;
4606 }
4607
4608 /// Update the control appearance
4609 void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent)
4610 {
4611 if (m_ctrl)
4612 {
4613 m_ctrl->SetCaretPosition(caretPosition);
4614 if (!m_ctrl->IsFrozen())
4615 {
4616 m_ctrl->LayoutContent();
4617 m_ctrl->PositionCaret();
4618 m_ctrl->Refresh(false);
4619
4620 if (sendUpdateEvent)
4621 m_ctrl->SendUpdateEvent();
4622 }
4623 }
4624 }
4625
4626 /// Replace the buffer paragraphs with the new ones.
4627 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox& fragment)
4628 {
4629 wxRichTextObjectList::compatibility_iterator node = fragment.GetChildren().GetFirst();
4630 while (node)
4631 {
4632 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
4633 wxASSERT (para != NULL);
4634
4635 // We'll replace the existing paragraph by finding the paragraph at this position,
4636 // delete its node data, and setting a copy as the new node data.
4637 // TODO: make more efficient by simply swapping old and new paragraph objects.
4638
4639 wxRichTextParagraph* existingPara = m_buffer->GetParagraphAtPosition(para->GetRange().GetStart());
4640 if (existingPara)
4641 {
4642 wxRichTextObjectList::compatibility_iterator bufferParaNode = m_buffer->GetChildren().Find(existingPara);
4643 if (bufferParaNode)
4644 {
4645 wxRichTextParagraph* newPara = new wxRichTextParagraph(*para);
4646 newPara->SetParent(m_buffer);
4647
4648 bufferParaNode->SetData(newPara);
4649
4650 delete existingPara;
4651 }
4652 }
4653
4654 node = node->GetNext();
4655 }
4656 }
4657
4658
4659 /*!
4660 * wxRichTextRange
4661 * This stores beginning and end positions for a range of data.
4662 */
4663
4664 /// Limit this range to be within 'range'
4665 bool wxRichTextRange::LimitTo(const wxRichTextRange& range)
4666 {
4667 if (m_start < range.m_start)
4668 m_start = range.m_start;
4669
4670 if (m_end > range.m_end)
4671 m_end = range.m_end;
4672
4673 return true;
4674 }
4675
4676 /*!
4677 * wxRichTextImage implementation
4678 * This object represents an image.
4679 */
4680
4681 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage, wxRichTextObject)
4682
4683 wxRichTextImage::wxRichTextImage(const wxImage& image, wxRichTextObject* parent):
4684 wxRichTextObject(parent)
4685 {
4686 m_image = image;
4687 }
4688
4689 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent):
4690 wxRichTextObject(parent)
4691 {
4692 m_imageBlock = imageBlock;
4693 m_imageBlock.Load(m_image);
4694 }
4695
4696 /// Load wxImage from the block
4697 bool wxRichTextImage::LoadFromBlock()
4698 {
4699 m_imageBlock.Load(m_image);
4700 return m_imageBlock.Ok();
4701 }
4702
4703 /// Make block from the wxImage
4704 bool wxRichTextImage::MakeBlock()
4705 {
4706 if (m_imageBlock.GetImageType() == wxBITMAP_TYPE_ANY || m_imageBlock.GetImageType() == -1)
4707 m_imageBlock.SetImageType(wxBITMAP_TYPE_PNG);
4708
4709 m_imageBlock.MakeImageBlock(m_image, m_imageBlock.GetImageType());
4710 return m_imageBlock.Ok();
4711 }
4712
4713
4714 /// Draw the item
4715 bool wxRichTextImage::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int WXUNUSED(descent), int WXUNUSED(style))
4716 {
4717 if (!m_image.Ok() && m_imageBlock.Ok())
4718 LoadFromBlock();
4719
4720 if (!m_image.Ok())
4721 return false;
4722
4723 if (m_image.Ok() && !m_bitmap.Ok())
4724 m_bitmap = wxBitmap(m_image);
4725
4726 int y = rect.y + (rect.height - m_image.GetHeight());
4727
4728 if (m_bitmap.Ok())
4729 dc.DrawBitmap(m_bitmap, rect.x, y, true);
4730
4731 if (selectionRange.Contains(range.GetStart()))
4732 {
4733 dc.SetBrush(*wxBLACK_BRUSH);
4734 dc.SetPen(*wxBLACK_PEN);
4735 dc.SetLogicalFunction(wxINVERT);
4736 dc.DrawRectangle(rect);
4737 dc.SetLogicalFunction(wxCOPY);
4738 }
4739
4740 return true;
4741 }
4742
4743 /// Lay the item out
4744 bool wxRichTextImage::Layout(wxDC& WXUNUSED(dc), const wxRect& rect, int WXUNUSED(style))
4745 {
4746 if (!m_image.Ok())
4747 LoadFromBlock();
4748
4749 if (m_image.Ok())
4750 {
4751 SetCachedSize(wxSize(m_image.GetWidth(), m_image.GetHeight()));
4752 SetPosition(rect.GetPosition());
4753 }
4754
4755 return true;
4756 }
4757
4758 /// Get/set the object size for the given range. Returns false if the range
4759 /// is invalid for this object.
4760 bool wxRichTextImage::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& WXUNUSED(descent), wxDC& WXUNUSED(dc), int WXUNUSED(flags), wxPoint WXUNUSED(position)) const
4761 {
4762 if (!range.IsWithin(GetRange()))
4763 return false;
4764
4765 if (!m_image.Ok())
4766 return false;
4767
4768 size.x = m_image.GetWidth();
4769 size.y = m_image.GetHeight();
4770
4771 return true;
4772 }
4773
4774 /// Copy
4775 void wxRichTextImage::Copy(const wxRichTextImage& obj)
4776 {
4777 m_image = obj.m_image;
4778 m_imageBlock = obj.m_imageBlock;
4779 }
4780
4781 /*!
4782 * Utilities
4783 *
4784 */
4785
4786 /// Compare two attribute objects
4787 bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2)
4788 {
4789 return (
4790 attr1.GetTextColour() == attr2.GetTextColour() &&
4791 attr1.GetBackgroundColour() == attr2.GetBackgroundColour() &&
4792 attr1.GetFont() == attr2.GetFont() &&
4793 attr1.GetAlignment() == attr2.GetAlignment() &&
4794 attr1.GetLeftIndent() == attr2.GetLeftIndent() &&
4795 attr1.GetRightIndent() == attr2.GetRightIndent() &&
4796 attr1.GetLeftSubIndent() == attr2.GetLeftSubIndent() &&
4797 attr1.GetTabs().GetCount() == attr2.GetTabs().GetCount() && // heuristic
4798 attr1.GetLineSpacing() == attr2.GetLineSpacing() &&
4799 attr1.GetParagraphSpacingAfter() == attr2.GetParagraphSpacingAfter() &&
4800 attr1.GetParagraphSpacingBefore() == attr2.GetParagraphSpacingBefore() &&
4801 attr1.GetBulletStyle() == attr2.GetBulletStyle() &&
4802 attr1.GetBulletNumber() == attr2.GetBulletNumber() &&
4803 attr1.GetBulletSymbol() == attr2.GetBulletSymbol() &&
4804 attr1.GetCharacterStyleName() == attr2.GetCharacterStyleName() &&
4805 attr1.GetParagraphStyleName() == attr2.GetParagraphStyleName());
4806 }
4807
4808 bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2)
4809 {
4810 return (
4811 attr1.GetTextColour() == attr2.GetTextColour() &&
4812 attr1.GetBackgroundColour() == attr2.GetBackgroundColour() &&
4813 attr1.GetFont().GetPointSize() == attr2.GetFontSize() &&
4814 attr1.GetFont().GetStyle() == attr2.GetFontStyle() &&
4815 attr1.GetFont().GetWeight() == attr2.GetFontWeight() &&
4816 attr1.GetFont().GetFaceName() == attr2.GetFontFaceName() &&
4817 attr1.GetFont().GetUnderlined() == attr2.GetFontUnderlined() &&
4818 attr1.GetAlignment() == attr2.GetAlignment() &&
4819 attr1.GetLeftIndent() == attr2.GetLeftIndent() &&
4820 attr1.GetRightIndent() == attr2.GetRightIndent() &&
4821 attr1.GetLeftSubIndent() == attr2.GetLeftSubIndent() &&
4822 attr1.GetTabs().GetCount() == attr2.GetTabs().GetCount() && // heuristic
4823 attr1.GetLineSpacing() == attr2.GetLineSpacing() &&
4824 attr1.GetParagraphSpacingAfter() == attr2.GetParagraphSpacingAfter() &&
4825 attr1.GetParagraphSpacingBefore() == attr2.GetParagraphSpacingBefore() &&
4826 attr1.GetBulletStyle() == attr2.GetBulletStyle() &&
4827 attr1.GetBulletNumber() == attr2.GetBulletNumber() &&
4828 attr1.GetBulletSymbol() == attr2.GetBulletSymbol() &&
4829 attr1.GetCharacterStyleName() == attr2.GetCharacterStyleName() &&
4830 attr1.GetParagraphStyleName() == attr2.GetParagraphStyleName());
4831 }
4832
4833 /// Compare two attribute objects, but take into account the flags
4834 /// specifying attributes of interest.
4835 bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2, int flags)
4836 {
4837 if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && attr1.GetTextColour() != attr2.GetTextColour())
4838 return false;
4839
4840 if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && attr1.GetBackgroundColour() != attr2.GetBackgroundColour())
4841 return false;
4842
4843 if ((flags & wxTEXT_ATTR_FONT_FACE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
4844 attr1.GetFont().GetFaceName() != attr2.GetFont().GetFaceName())
4845 return false;
4846
4847 if ((flags & wxTEXT_ATTR_FONT_SIZE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
4848 attr1.GetFont().GetPointSize() != attr2.GetFont().GetPointSize())
4849 return false;
4850
4851 if ((flags & wxTEXT_ATTR_FONT_WEIGHT) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
4852 attr1.GetFont().GetWeight() != attr2.GetFont().GetWeight())
4853 return false;
4854
4855 if ((flags & wxTEXT_ATTR_FONT_ITALIC) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
4856 attr1.GetFont().GetStyle() != attr2.GetFont().GetStyle())
4857 return false;
4858
4859 if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
4860 attr1.GetFont().GetUnderlined() != attr2.GetFont().GetUnderlined())
4861 return false;
4862
4863 if ((flags & wxTEXT_ATTR_ALIGNMENT) && attr1.GetAlignment() != attr2.GetAlignment())
4864 return false;
4865
4866 if ((flags & wxTEXT_ATTR_LEFT_INDENT) &&
4867 ((attr1.GetLeftIndent() != attr2.GetLeftIndent()) || (attr1.GetLeftSubIndent() != attr2.GetLeftSubIndent())))
4868 return false;
4869
4870 if ((flags & wxTEXT_ATTR_RIGHT_INDENT) &&
4871 (attr1.GetRightIndent() != attr2.GetRightIndent()))
4872 return false;
4873
4874 if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) &&
4875 (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter()))
4876 return false;
4877
4878 if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) &&
4879 (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore()))
4880 return false;
4881
4882 if ((flags & wxTEXT_ATTR_LINE_SPACING) &&
4883 (attr1.GetLineSpacing() != attr2.GetLineSpacing()))
4884 return false;
4885
4886 if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) &&
4887 (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName()))
4888 return false;
4889
4890 if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) &&
4891 (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName()))
4892 return false;
4893
4894 if ((flags & wxTEXT_ATTR_BULLET_STYLE) &&
4895 (attr1.GetBulletStyle() != attr2.GetBulletStyle()))
4896 return false;
4897
4898 if ((flags & wxTEXT_ATTR_BULLET_NUMBER) &&
4899 (attr1.GetBulletNumber() != attr2.GetBulletNumber()))
4900 return false;
4901
4902 if ((flags & wxTEXT_ATTR_BULLET_SYMBOL) &&
4903 (attr1.GetBulletSymbol() != attr2.GetBulletSymbol()))
4904 return false;
4905
4906 /* TODO
4907 if ((flags & wxTEXT_ATTR_TABS) &&
4908 return false;
4909 */
4910
4911 return true;
4912 }
4913
4914 bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2, int flags)
4915 {
4916 if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && attr1.GetTextColour() != attr2.GetTextColour())
4917 return false;
4918
4919 if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && attr1.GetBackgroundColour() != attr2.GetBackgroundColour())
4920 return false;
4921
4922 if ((flags & (wxTEXT_ATTR_FONT)) && !attr1.GetFont().Ok())
4923 return false;
4924
4925 if ((flags & wxTEXT_ATTR_FONT_FACE) && attr1.GetFont().Ok() &&
4926 attr1.GetFont().GetFaceName() != attr2.GetFontFaceName())
4927 return false;
4928
4929 if ((flags & wxTEXT_ATTR_FONT_SIZE) && attr1.GetFont().Ok() &&
4930 attr1.GetFont().GetPointSize() != attr2.GetFontSize())
4931 return false;
4932
4933 if ((flags & wxTEXT_ATTR_FONT_WEIGHT) && attr1.GetFont().Ok() &&
4934 attr1.GetFont().GetWeight() != attr2.GetFontWeight())
4935 return false;
4936
4937 if ((flags & wxTEXT_ATTR_FONT_ITALIC) && attr1.GetFont().Ok() &&
4938 attr1.GetFont().GetStyle() != attr2.GetFontStyle())
4939 return false;
4940
4941 if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) && attr1.GetFont().Ok() &&
4942 attr1.GetFont().GetUnderlined() != attr2.GetFontUnderlined())
4943 return false;
4944
4945 if ((flags & wxTEXT_ATTR_ALIGNMENT) && attr1.GetAlignment() != attr2.GetAlignment())
4946 return false;
4947
4948 if ((flags & wxTEXT_ATTR_LEFT_INDENT) &&
4949 ((attr1.GetLeftIndent() != attr2.GetLeftIndent()) || (attr1.GetLeftSubIndent() != attr2.GetLeftSubIndent())))
4950 return false;
4951
4952 if ((flags & wxTEXT_ATTR_RIGHT_INDENT) &&
4953 (attr1.GetRightIndent() != attr2.GetRightIndent()))
4954 return false;
4955
4956 if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) &&
4957 (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter()))
4958 return false;
4959
4960 if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) &&
4961 (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore()))
4962 return false;
4963
4964 if ((flags & wxTEXT_ATTR_LINE_SPACING) &&
4965 (attr1.GetLineSpacing() != attr2.GetLineSpacing()))
4966 return false;
4967
4968 if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) &&
4969 (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName()))
4970 return false;
4971
4972 if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) &&
4973 (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName()))
4974 return false;
4975
4976 if ((flags & wxTEXT_ATTR_BULLET_STYLE) &&
4977 (attr1.GetBulletStyle() != attr2.GetBulletStyle()))
4978 return false;
4979
4980 if ((flags & wxTEXT_ATTR_BULLET_NUMBER) &&
4981 (attr1.GetBulletNumber() != attr2.GetBulletNumber()))
4982 return false;
4983
4984 if ((flags & wxTEXT_ATTR_BULLET_SYMBOL) &&
4985 (attr1.GetBulletSymbol() != attr2.GetBulletSymbol()))
4986 return false;
4987
4988 /* TODO
4989 if ((flags & wxTEXT_ATTR_TABS) &&
4990 return false;
4991 */
4992
4993 return true;
4994 }
4995
4996
4997 /// Apply one style to another
4998 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxTextAttrEx& style)
4999 {
5000 // Whole font
5001 if (style.GetFont().Ok() && ((style.GetFlags() & (wxTEXT_ATTR_FONT)) == (wxTEXT_ATTR_FONT)))
5002 destStyle.SetFont(style.GetFont());
5003 else if (style.GetFont().Ok())
5004 {
5005 wxFont font = destStyle.GetFont();
5006
5007 if (style.GetFlags() & wxTEXT_ATTR_FONT_FACE)
5008 {
5009 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_FACE);
5010 font.SetFaceName(style.GetFont().GetFaceName());
5011 }
5012
5013 if (style.GetFlags() & wxTEXT_ATTR_FONT_SIZE)
5014 {
5015 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_SIZE);
5016 font.SetPointSize(style.GetFont().GetPointSize());
5017 }
5018
5019 if (style.GetFlags() & wxTEXT_ATTR_FONT_ITALIC)
5020 {
5021 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_ITALIC);
5022 font.SetStyle(style.GetFont().GetStyle());
5023 }
5024
5025 if (style.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT)
5026 {
5027 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT);
5028 font.SetWeight(style.GetFont().GetWeight());
5029 }
5030
5031 if (style.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE)
5032 {
5033 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE);
5034 font.SetUnderlined(style.GetFont().GetUnderlined());
5035 }
5036
5037 if (font != destStyle.GetFont())
5038 {
5039 int oldFlags = destStyle.GetFlags();
5040
5041 destStyle.SetFont(font);
5042
5043 destStyle.SetFlags(oldFlags);
5044 }
5045 }
5046
5047 if ( style.GetTextColour().Ok() && style.HasTextColour())
5048 destStyle.SetTextColour(style.GetTextColour());
5049
5050 if ( style.GetBackgroundColour().Ok() && style.HasBackgroundColour())
5051 destStyle.SetBackgroundColour(style.GetBackgroundColour());
5052
5053 if (style.HasAlignment())
5054 destStyle.SetAlignment(style.GetAlignment());
5055
5056 if (style.HasTabs())
5057 destStyle.SetTabs(style.GetTabs());
5058
5059 if (style.HasLeftIndent())
5060 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
5061
5062 if (style.HasRightIndent())
5063 destStyle.SetRightIndent(style.GetRightIndent());
5064
5065 if (style.HasParagraphSpacingAfter())
5066 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
5067
5068 if (style.HasParagraphSpacingBefore())
5069 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
5070
5071 if (style.HasLineSpacing())
5072 destStyle.SetLineSpacing(style.GetLineSpacing());
5073
5074 if (style.HasCharacterStyleName())
5075 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
5076
5077 if (style.HasParagraphStyleName())
5078 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
5079
5080 if (style.HasBulletStyle())
5081 {
5082 destStyle.SetBulletStyle(style.GetBulletStyle());
5083 destStyle.SetBulletSymbol(style.GetBulletSymbol());
5084 }
5085
5086 if (style.HasBulletNumber())
5087 destStyle.SetBulletNumber(style.GetBulletNumber());
5088
5089 return true;
5090 }
5091
5092 bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxTextAttrEx& style)
5093 {
5094 wxTextAttrEx destStyle2;
5095 destStyle.CopyTo(destStyle2);
5096 wxRichTextApplyStyle(destStyle2, style);
5097 destStyle = destStyle2;
5098 return true;
5099 }
5100
5101 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style)
5102 {
5103 // Whole font. Avoiding setting individual attributes if possible, since
5104 // it recreates the font each time.
5105 if ((style.GetFlags() & (wxTEXT_ATTR_FONT)) == (wxTEXT_ATTR_FONT))
5106 {
5107 destStyle.SetFont(wxFont(style.GetFontSize(), destStyle.GetFont().Ok() ? destStyle.GetFont().GetFamily() : wxDEFAULT,
5108 style.GetFontStyle(), style.GetFontWeight(), style.GetFontUnderlined(), style.GetFontFaceName()));
5109 }
5110 else if (style.GetFlags() & (wxTEXT_ATTR_FONT))
5111 {
5112 wxFont font = destStyle.GetFont();
5113
5114 if (style.GetFlags() & wxTEXT_ATTR_FONT_FACE)
5115 {
5116 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_FACE);
5117 font.SetFaceName(style.GetFontFaceName());
5118 }
5119
5120 if (style.GetFlags() & wxTEXT_ATTR_FONT_SIZE)
5121 {
5122 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_SIZE);
5123 font.SetPointSize(style.GetFontSize());
5124 }
5125
5126 if (style.GetFlags() & wxTEXT_ATTR_FONT_ITALIC)
5127 {
5128 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_ITALIC);
5129 font.SetStyle(style.GetFontStyle());
5130 }
5131
5132 if (style.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT)
5133 {
5134 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT);
5135 font.SetWeight(style.GetFontWeight());
5136 }
5137
5138 if (style.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE)
5139 {
5140 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE);
5141 font.SetUnderlined(style.GetFontUnderlined());
5142 }
5143
5144 if (font != destStyle.GetFont())
5145 {
5146 int oldFlags = destStyle.GetFlags();
5147
5148 destStyle.SetFont(font);
5149
5150 destStyle.SetFlags(oldFlags);
5151 }
5152 }
5153
5154 if ( style.GetTextColour().Ok() && style.HasTextColour())
5155 destStyle.SetTextColour(style.GetTextColour());
5156
5157 if ( style.GetBackgroundColour().Ok() && style.HasBackgroundColour())
5158 destStyle.SetBackgroundColour(style.GetBackgroundColour());
5159
5160 if (style.HasAlignment())
5161 destStyle.SetAlignment(style.GetAlignment());
5162
5163 if (style.HasTabs())
5164 destStyle.SetTabs(style.GetTabs());
5165
5166 if (style.HasLeftIndent())
5167 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
5168
5169 if (style.HasRightIndent())
5170 destStyle.SetRightIndent(style.GetRightIndent());
5171
5172 if (style.HasParagraphSpacingAfter())
5173 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
5174
5175 if (style.HasParagraphSpacingBefore())
5176 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
5177
5178 if (style.HasLineSpacing())
5179 destStyle.SetLineSpacing(style.GetLineSpacing());
5180
5181 if (style.HasCharacterStyleName())
5182 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
5183
5184 if (style.HasParagraphStyleName())
5185 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
5186
5187 if (style.HasBulletStyle())
5188 {
5189 destStyle.SetBulletStyle(style.GetBulletStyle());
5190 destStyle.SetBulletSymbol(style.GetBulletSymbol());
5191 }
5192
5193 if (style.HasBulletNumber())
5194 destStyle.SetBulletNumber(style.GetBulletNumber());
5195
5196 return true;
5197 }
5198
5199
5200 /*!
5201 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
5202 * efficient way to query styles.
5203 */
5204
5205 // ctors
5206 wxRichTextAttr::wxRichTextAttr(const wxColour& colText,
5207 const wxColour& colBack,
5208 wxTextAttrAlignment alignment): m_textAlignment(alignment), m_colText(colText), m_colBack(colBack)
5209 {
5210 Init();
5211
5212 if (m_colText.Ok()) m_flags |= wxTEXT_ATTR_TEXT_COLOUR;
5213 if (m_colBack.Ok()) m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR;
5214 if (alignment != wxTEXT_ALIGNMENT_DEFAULT)
5215 m_flags |= wxTEXT_ATTR_ALIGNMENT;
5216 }
5217
5218 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx& attr)
5219 {
5220 Init();
5221
5222 (*this) = attr;
5223 }
5224
5225 // operations
5226 void wxRichTextAttr::Init()
5227 {
5228 m_textAlignment = wxTEXT_ALIGNMENT_DEFAULT;
5229 m_flags = 0;
5230 m_leftIndent = 0;
5231 m_leftSubIndent = 0;
5232 m_rightIndent = 0;
5233
5234 m_fontSize = 12;
5235 m_fontStyle = wxNORMAL;
5236 m_fontWeight = wxNORMAL;
5237 m_fontUnderlined = false;
5238
5239 m_paragraphSpacingAfter = 0;
5240 m_paragraphSpacingBefore = 0;
5241 m_lineSpacing = 0;
5242 m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
5243 m_bulletNumber = 0;
5244 m_bulletSymbol = wxT('*');
5245 }
5246
5247 // operators
5248 void wxRichTextAttr::operator= (const wxRichTextAttr& attr)
5249 {
5250 m_colText = attr.m_colText;
5251 m_colBack = attr.m_colBack;
5252 m_textAlignment = attr.m_textAlignment;
5253 m_leftIndent = attr.m_leftIndent;
5254 m_leftSubIndent = attr.m_leftSubIndent;
5255 m_rightIndent = attr.m_rightIndent;
5256 m_tabs = attr.m_tabs;
5257 m_flags = attr.m_flags;
5258
5259 m_fontSize = attr.m_fontSize;
5260 m_fontStyle = attr.m_fontStyle;
5261 m_fontWeight = attr.m_fontWeight;
5262 m_fontUnderlined = attr.m_fontUnderlined;
5263 m_fontFaceName = attr.m_fontFaceName;
5264
5265 m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
5266 m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
5267 m_lineSpacing = attr.m_lineSpacing;
5268 m_characterStyleName = attr.m_characterStyleName;
5269 m_paragraphStyleName = attr.m_paragraphStyleName;
5270 m_bulletStyle = attr.m_bulletStyle;
5271 m_bulletNumber = attr.m_bulletNumber;
5272 m_bulletSymbol = attr.m_bulletSymbol;
5273 }
5274
5275 // operators
5276 void wxRichTextAttr::operator= (const wxTextAttrEx& attr)
5277 {
5278 m_colText = attr.GetTextColour();
5279 m_colBack = attr.GetBackgroundColour();
5280 m_textAlignment = attr.GetAlignment();
5281 m_leftIndent = attr.GetLeftIndent();
5282 m_leftSubIndent = attr.GetLeftSubIndent();
5283 m_rightIndent = attr.GetRightIndent();
5284 m_tabs = attr.GetTabs();
5285 m_flags = attr.GetFlags();
5286
5287 m_paragraphSpacingAfter = attr.GetParagraphSpacingAfter();
5288 m_paragraphSpacingBefore = attr.GetParagraphSpacingBefore();
5289 m_lineSpacing = attr.GetLineSpacing();
5290 m_characterStyleName = attr.GetCharacterStyleName();
5291 m_paragraphStyleName = attr.GetParagraphStyleName();
5292
5293 if (attr.GetFont().Ok())
5294 GetFontAttributes(attr.GetFont());
5295 }
5296
5297 // Making a wxTextAttrEx object.
5298 wxRichTextAttr::operator wxTextAttrEx () const
5299 {
5300 wxTextAttrEx attr;
5301 CopyTo(attr);
5302 return attr;
5303 }
5304
5305 // Equality test
5306 bool wxRichTextAttr::operator== (const wxRichTextAttr& attr) const
5307 {
5308 return GetFlags() == attr.GetFlags() &&
5309
5310 GetTextColour() == attr.GetTextColour() &&
5311 GetBackgroundColour() == attr.GetBackgroundColour() &&
5312
5313 GetAlignment() == attr.GetAlignment() &&
5314 GetLeftIndent() == attr.GetLeftIndent() &&
5315 GetLeftSubIndent() == attr.GetLeftSubIndent() &&
5316 GetRightIndent() == attr.GetRightIndent() &&
5317 //GetTabs() == attr.GetTabs() &&
5318
5319 GetParagraphSpacingAfter() == attr.GetParagraphSpacingAfter() &&
5320 GetParagraphSpacingBefore() == attr.GetParagraphSpacingBefore() &&
5321 GetLineSpacing() == attr.GetLineSpacing() &&
5322 GetCharacterStyleName() == attr.GetCharacterStyleName() &&
5323 GetParagraphStyleName() == attr.GetParagraphStyleName() &&
5324
5325 m_fontSize == attr.m_fontSize &&
5326 m_fontStyle == attr.m_fontStyle &&
5327 m_fontWeight == attr.m_fontWeight &&
5328 m_fontUnderlined == attr.m_fontUnderlined &&
5329 m_fontFaceName == attr.m_fontFaceName;
5330 }
5331
5332 // Copy to a wxTextAttr
5333 void wxRichTextAttr::CopyTo(wxTextAttrEx& attr) const
5334 {
5335 attr.SetTextColour(GetTextColour());
5336 attr.SetBackgroundColour(GetBackgroundColour());
5337 attr.SetAlignment(GetAlignment());
5338 attr.SetTabs(GetTabs());
5339 attr.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
5340 attr.SetRightIndent(GetRightIndent());
5341 attr.SetFont(CreateFont());
5342
5343 attr.SetParagraphSpacingAfter(m_paragraphSpacingAfter);
5344 attr.SetParagraphSpacingBefore(m_paragraphSpacingBefore);
5345 attr.SetLineSpacing(m_lineSpacing);
5346 attr.SetBulletStyle(m_bulletStyle);
5347 attr.SetBulletNumber(m_bulletNumber);
5348 attr.SetBulletSymbol(m_bulletSymbol);
5349 attr.SetCharacterStyleName(m_characterStyleName);
5350 attr.SetParagraphStyleName(m_paragraphStyleName);
5351
5352 attr.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
5353 }
5354
5355 // Create font from font attributes.
5356 wxFont wxRichTextAttr::CreateFont() const
5357 {
5358 wxFont font(m_fontSize, wxDEFAULT, m_fontStyle, m_fontWeight, m_fontUnderlined, m_fontFaceName);
5359 #ifdef __WXMAC__
5360 font.SetNoAntiAliasing(true);
5361 #endif
5362 return font;
5363 }
5364
5365 // Get attributes from font.
5366 bool wxRichTextAttr::GetFontAttributes(const wxFont& font)
5367 {
5368 if (!font.Ok())
5369 return false;
5370
5371 m_fontSize = font.GetPointSize();
5372 m_fontStyle = font.GetStyle();
5373 m_fontWeight = font.GetWeight();
5374 m_fontUnderlined = font.GetUnderlined();
5375 m_fontFaceName = font.GetFaceName();
5376
5377 return true;
5378 }
5379
5380 wxRichTextAttr wxRichTextAttr::Combine(const wxRichTextAttr& attr,
5381 const wxRichTextAttr& attrDef,
5382 const wxTextCtrlBase *text)
5383 {
5384 wxColour colFg = attr.GetTextColour();
5385 if ( !colFg.Ok() )
5386 {
5387 colFg = attrDef.GetTextColour();
5388
5389 if ( text && !colFg.Ok() )
5390 colFg = text->GetForegroundColour();
5391 }
5392
5393 wxColour colBg = attr.GetBackgroundColour();
5394 if ( !colBg.Ok() )
5395 {
5396 colBg = attrDef.GetBackgroundColour();
5397
5398 if ( text && !colBg.Ok() )
5399 colBg = text->GetBackgroundColour();
5400 }
5401
5402 wxRichTextAttr newAttr(colFg, colBg);
5403
5404 if (attr.HasWeight())
5405 newAttr.SetFontWeight(attr.GetFontWeight());
5406
5407 if (attr.HasSize())
5408 newAttr.SetFontSize(attr.GetFontSize());
5409
5410 if (attr.HasItalic())
5411 newAttr.SetFontStyle(attr.GetFontStyle());
5412
5413 if (attr.HasUnderlined())
5414 newAttr.SetFontUnderlined(attr.GetFontUnderlined());
5415
5416 if (attr.HasFaceName())
5417 newAttr.SetFontFaceName(attr.GetFontFaceName());
5418
5419 if (attr.HasAlignment())
5420 newAttr.SetAlignment(attr.GetAlignment());
5421 else if (attrDef.HasAlignment())
5422 newAttr.SetAlignment(attrDef.GetAlignment());
5423
5424 if (attr.HasTabs())
5425 newAttr.SetTabs(attr.GetTabs());
5426 else if (attrDef.HasTabs())
5427 newAttr.SetTabs(attrDef.GetTabs());
5428
5429 if (attr.HasLeftIndent())
5430 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
5431 else if (attrDef.HasLeftIndent())
5432 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
5433
5434 if (attr.HasRightIndent())
5435 newAttr.SetRightIndent(attr.GetRightIndent());
5436 else if (attrDef.HasRightIndent())
5437 newAttr.SetRightIndent(attrDef.GetRightIndent());
5438
5439 // NEW ATTRIBUTES
5440
5441 if (attr.HasParagraphSpacingAfter())
5442 newAttr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter());
5443
5444 if (attr.HasParagraphSpacingBefore())
5445 newAttr.SetParagraphSpacingBefore(attr.GetParagraphSpacingBefore());
5446
5447 if (attr.HasLineSpacing())
5448 newAttr.SetLineSpacing(attr.GetLineSpacing());
5449
5450 if (attr.HasCharacterStyleName())
5451 newAttr.SetCharacterStyleName(attr.GetCharacterStyleName());
5452
5453 if (attr.HasParagraphStyleName())
5454 newAttr.SetParagraphStyleName(attr.GetParagraphStyleName());
5455
5456 if (attr.HasBulletStyle())
5457 newAttr.SetBulletStyle(attr.GetBulletStyle());
5458
5459 if (attr.HasBulletNumber())
5460 newAttr.SetBulletNumber(attr.GetBulletNumber());
5461
5462 if (attr.HasBulletSymbol())
5463 newAttr.SetBulletSymbol(attr.GetBulletSymbol());
5464
5465 return newAttr;
5466 }
5467
5468 /*!
5469 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
5470 */
5471
5472 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx& attr): wxTextAttr(attr)
5473 {
5474 m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
5475 m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
5476 m_lineSpacing = attr.m_lineSpacing;
5477 m_paragraphStyleName = attr.m_paragraphStyleName;
5478 m_characterStyleName = attr.m_characterStyleName;
5479 m_bulletStyle = attr.m_bulletStyle;
5480 m_bulletNumber = attr.m_bulletNumber;
5481 m_bulletSymbol = attr.m_bulletSymbol;
5482 }
5483
5484 // Initialise this object.
5485 void wxTextAttrEx::Init()
5486 {
5487 m_paragraphSpacingAfter = 0;
5488 m_paragraphSpacingBefore = 0;
5489 m_lineSpacing = 0;
5490 m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
5491 m_bulletNumber = 0;
5492 m_bulletSymbol = 0;
5493 m_bulletSymbol = wxT('*');
5494 }
5495
5496 // Assignment from a wxTextAttrEx object
5497 void wxTextAttrEx::operator= (const wxTextAttrEx& attr)
5498 {
5499 wxTextAttr::operator= (attr);
5500
5501 m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
5502 m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
5503 m_lineSpacing = attr.m_lineSpacing;
5504 m_characterStyleName = attr.m_characterStyleName;
5505 m_paragraphStyleName = attr.m_paragraphStyleName;
5506 m_bulletStyle = attr.m_bulletStyle;
5507 m_bulletNumber = attr.m_bulletNumber;
5508 m_bulletSymbol = attr.m_bulletSymbol;
5509 }
5510
5511 // Assignment from a wxTextAttr object.
5512 void wxTextAttrEx::operator= (const wxTextAttr& attr)
5513 {
5514 wxTextAttr::operator= (attr);
5515 }
5516
5517 wxTextAttrEx wxTextAttrEx::CombineEx(const wxTextAttrEx& attr,
5518 const wxTextAttrEx& attrDef,
5519 const wxTextCtrlBase *text)
5520 {
5521 wxTextAttrEx newAttr;
5522
5523 // If attr specifies the complete font, just use that font, overriding all
5524 // default font attributes.
5525 if ((attr.GetFlags() & wxTEXT_ATTR_FONT) == wxTEXT_ATTR_FONT)
5526 newAttr.SetFont(attr.GetFont());
5527 else
5528 {
5529 // First find the basic, default font
5530 long flags = 0;
5531
5532 wxFont font;
5533 if (attrDef.HasFont())
5534 {
5535 flags = (attrDef.GetFlags() & wxTEXT_ATTR_FONT);
5536 font = attrDef.GetFont();
5537 }
5538 else
5539 {
5540 if (text)
5541 font = text->GetFont();
5542
5543 // We leave flags at 0 because no font attributes have been specified yet
5544 }
5545 if (!font.Ok())
5546 font = *wxNORMAL_FONT;
5547
5548 // Otherwise, if there are font attributes in attr, apply them
5549 if (attr.HasFont())
5550 {
5551 if (attr.HasSize())
5552 {
5553 flags |= wxTEXT_ATTR_FONT_SIZE;
5554 font.SetPointSize(attr.GetFont().GetPointSize());
5555 }
5556 if (attr.HasItalic())
5557 {
5558 flags |= wxTEXT_ATTR_FONT_ITALIC;;
5559 font.SetStyle(attr.GetFont().GetStyle());
5560 }
5561 if (attr.HasWeight())
5562 {
5563 flags |= wxTEXT_ATTR_FONT_WEIGHT;
5564 font.SetWeight(attr.GetFont().GetWeight());
5565 }
5566 if (attr.HasFaceName())
5567 {
5568 flags |= wxTEXT_ATTR_FONT_FACE;
5569 font.SetFaceName(attr.GetFont().GetFaceName());
5570 }
5571 if (attr.HasUnderlined())
5572 {
5573 flags |= wxTEXT_ATTR_FONT_UNDERLINE;
5574 font.SetUnderlined(attr.GetFont().GetUnderlined());
5575 }
5576 newAttr.SetFont(font);
5577 newAttr.SetFlags(newAttr.GetFlags()|flags);
5578 }
5579 }
5580
5581 // TODO: should really check we are specifying these in the flags,
5582 // before setting them, as per above; or we will set them willy-nilly.
5583 // However, we should also check whether this is the intention
5584 // as per wxTextAttr::Combine, i.e. always to have valid colours
5585 // in the style.
5586 wxColour colFg = attr.GetTextColour();
5587 if ( !colFg.Ok() )
5588 {
5589 colFg = attrDef.GetTextColour();
5590
5591 if ( text && !colFg.Ok() )
5592 colFg = text->GetForegroundColour();
5593 }
5594
5595 wxColour colBg = attr.GetBackgroundColour();
5596 if ( !colBg.Ok() )
5597 {
5598 colBg = attrDef.GetBackgroundColour();
5599
5600 if ( text && !colBg.Ok() )
5601 colBg = text->GetBackgroundColour();
5602 }
5603
5604 newAttr.SetTextColour(colFg);
5605 newAttr.SetBackgroundColour(colBg);
5606
5607 if (attr.HasAlignment())
5608 newAttr.SetAlignment(attr.GetAlignment());
5609 else if (attrDef.HasAlignment())
5610 newAttr.SetAlignment(attrDef.GetAlignment());
5611
5612 if (attr.HasTabs())
5613 newAttr.SetTabs(attr.GetTabs());
5614 else if (attrDef.HasTabs())
5615 newAttr.SetTabs(attrDef.GetTabs());
5616
5617 if (attr.HasLeftIndent())
5618 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
5619 else if (attrDef.HasLeftIndent())
5620 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
5621
5622 if (attr.HasRightIndent())
5623 newAttr.SetRightIndent(attr.GetRightIndent());
5624 else if (attrDef.HasRightIndent())
5625 newAttr.SetRightIndent(attrDef.GetRightIndent());
5626
5627 // NEW ATTRIBUTES
5628
5629 if (attr.HasParagraphSpacingAfter())
5630 newAttr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter());
5631
5632 if (attr.HasParagraphSpacingBefore())
5633 newAttr.SetParagraphSpacingBefore(attr.GetParagraphSpacingBefore());
5634
5635 if (attr.HasLineSpacing())
5636 newAttr.SetLineSpacing(attr.GetLineSpacing());
5637
5638 if (attr.HasCharacterStyleName())
5639 newAttr.SetCharacterStyleName(attr.GetCharacterStyleName());
5640
5641 if (attr.HasParagraphStyleName())
5642 newAttr.SetParagraphStyleName(attr.GetParagraphStyleName());
5643
5644 if (attr.HasBulletStyle())
5645 newAttr.SetBulletStyle(attr.GetBulletStyle());
5646
5647 if (attr.HasBulletNumber())
5648 newAttr.SetBulletNumber(attr.GetBulletNumber());
5649
5650 if (attr.HasBulletSymbol())
5651 newAttr.SetBulletSymbol(attr.GetBulletSymbol());
5652
5653 return newAttr;
5654 }
5655
5656
5657 /*!
5658 * wxRichTextFileHandler
5659 * Base class for file handlers
5660 */
5661
5662 IMPLEMENT_CLASS(wxRichTextFileHandler, wxObject)
5663
5664 #if wxUSE_STREAMS
5665 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer *buffer, const wxString& filename)
5666 {
5667 wxFFileInputStream stream(filename);
5668 if (stream.Ok())
5669 return LoadFile(buffer, stream);
5670
5671 return false;
5672 }
5673
5674 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& filename)
5675 {
5676 wxFFileOutputStream stream(filename);
5677 if (stream.Ok())
5678 return SaveFile(buffer, stream);
5679
5680 return false;
5681 }
5682 #endif // wxUSE_STREAMS
5683
5684 /// Can we handle this filename (if using files)? By default, checks the extension.
5685 bool wxRichTextFileHandler::CanHandle(const wxString& filename) const
5686 {
5687 wxString path, file, ext;
5688 wxSplitPath(filename, & path, & file, & ext);
5689
5690 return (ext.Lower() == GetExtension());
5691 }
5692
5693 /*!
5694 * wxRichTextTextHandler
5695 * Plain text handler
5696 */
5697
5698 IMPLEMENT_CLASS(wxRichTextPlainTextHandler, wxRichTextFileHandler)
5699
5700 #if wxUSE_STREAMS
5701 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
5702 {
5703 if (!stream.IsOk())
5704 return false;
5705
5706 wxString str;
5707 int lastCh = 0;
5708
5709 while (!stream.Eof())
5710 {
5711 int ch = stream.GetC();
5712
5713 if (!stream.Eof())
5714 {
5715 if (ch == 10 && lastCh != 13)
5716 str += wxT('\n');
5717
5718 if (ch > 0 && ch != 10)
5719 str += wxChar(ch);
5720
5721 lastCh = ch;
5722 }
5723 }
5724
5725 buffer->Clear();
5726 buffer->AddParagraphs(str);
5727 buffer->UpdateRanges();
5728
5729 return true;
5730
5731 }
5732
5733 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
5734 {
5735 if (!stream.IsOk())
5736 return false;
5737
5738 wxString text = buffer->GetText();
5739 wxCharBuffer buf = text.ToAscii();
5740
5741 stream.Write((const char*) buf, text.length());
5742 return true;
5743 }
5744 #endif // wxUSE_STREAMS
5745
5746 /*
5747 * Stores information about an image, in binary in-memory form
5748 */
5749
5750 wxRichTextImageBlock::wxRichTextImageBlock()
5751 {
5752 Init();
5753 }
5754
5755 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock& block):wxObject()
5756 {
5757 Init();
5758 Copy(block);
5759 }
5760
5761 wxRichTextImageBlock::~wxRichTextImageBlock()
5762 {
5763 if (m_data)
5764 {
5765 delete[] m_data;
5766 m_data = NULL;
5767 }
5768 }
5769
5770 void wxRichTextImageBlock::Init()
5771 {
5772 m_data = NULL;
5773 m_dataSize = 0;
5774 m_imageType = -1;
5775 }
5776
5777 void wxRichTextImageBlock::Clear()
5778 {
5779 delete[] m_data;
5780 m_data = NULL;
5781 m_dataSize = 0;
5782 m_imageType = -1;
5783 }
5784
5785
5786 // Load the original image into a memory block.
5787 // If the image is not a JPEG, we must convert it into a JPEG
5788 // to conserve space.
5789 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
5790 // load the image a 2nd time.
5791
5792 bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, int imageType, wxImage& image, bool convertToJPEG)
5793 {
5794 m_imageType = imageType;
5795
5796 wxString filenameToRead(filename);
5797 bool removeFile = false;
5798
5799 if (imageType == -1)
5800 return false; // Could not determine image type
5801
5802 if ((imageType != wxBITMAP_TYPE_JPEG) && convertToJPEG)
5803 {
5804 wxString tempFile;
5805 bool success = wxGetTempFileName(_("image"), tempFile) ;
5806
5807 wxASSERT(success);
5808
5809 wxUnusedVar(success);
5810
5811 image.SaveFile(tempFile, wxBITMAP_TYPE_JPEG);
5812 filenameToRead = tempFile;
5813 removeFile = true;
5814
5815 m_imageType = wxBITMAP_TYPE_JPEG;
5816 }
5817 wxFile file;
5818 if (!file.Open(filenameToRead))
5819 return false;
5820
5821 m_dataSize = (size_t) file.Length();
5822 file.Close();
5823
5824 if (m_data)
5825 delete[] m_data;
5826 m_data = ReadBlock(filenameToRead, m_dataSize);
5827
5828 if (removeFile)
5829 wxRemoveFile(filenameToRead);
5830
5831 return (m_data != NULL);
5832 }
5833
5834 // Make an image block from the wxImage in the given
5835 // format.
5836 bool wxRichTextImageBlock::MakeImageBlock(wxImage& image, int imageType, int quality)
5837 {
5838 m_imageType = imageType;
5839 image.SetOption(wxT("quality"), quality);
5840
5841 if (imageType == -1)
5842 return false; // Could not determine image type
5843
5844 wxString tempFile;
5845 bool success = wxGetTempFileName(_("image"), tempFile) ;
5846
5847 wxASSERT(success);
5848 wxUnusedVar(success);
5849
5850 if (!image.SaveFile(tempFile, m_imageType))
5851 {
5852 if (wxFileExists(tempFile))
5853 wxRemoveFile(tempFile);
5854 return false;
5855 }
5856
5857 wxFile file;
5858 if (!file.Open(tempFile))
5859 return false;
5860
5861 m_dataSize = (size_t) file.Length();
5862 file.Close();
5863
5864 if (m_data)
5865 delete[] m_data;
5866 m_data = ReadBlock(tempFile, m_dataSize);
5867
5868 wxRemoveFile(tempFile);
5869
5870 return (m_data != NULL);
5871 }
5872
5873
5874 // Write to a file
5875 bool wxRichTextImageBlock::Write(const wxString& filename)
5876 {
5877 return WriteBlock(filename, m_data, m_dataSize);
5878 }
5879
5880 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock& block)
5881 {
5882 m_imageType = block.m_imageType;
5883 if (m_data)
5884 {
5885 delete[] m_data;
5886 m_data = NULL;
5887 }
5888 m_dataSize = block.m_dataSize;
5889 if (m_dataSize == 0)
5890 return;
5891
5892 m_data = new unsigned char[m_dataSize];
5893 unsigned int i;
5894 for (i = 0; i < m_dataSize; i++)
5895 m_data[i] = block.m_data[i];
5896 }
5897
5898 //// Operators
5899 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock& block)
5900 {
5901 Copy(block);
5902 }
5903
5904 // Load a wxImage from the block
5905 bool wxRichTextImageBlock::Load(wxImage& image)
5906 {
5907 if (!m_data)
5908 return false;
5909
5910 // Read in the image.
5911 #if wxUSE_STREAMS
5912 wxMemoryInputStream mstream(m_data, m_dataSize);
5913 bool success = image.LoadFile(mstream, GetImageType());
5914 #else
5915 wxString tempFile;
5916 bool success = wxGetTempFileName(_("image"), tempFile) ;
5917 wxASSERT(success);
5918
5919 if (!WriteBlock(tempFile, m_data, m_dataSize))
5920 {
5921 return false;
5922 }
5923 success = image.LoadFile(tempFile, GetImageType());
5924 wxRemoveFile(tempFile);
5925 #endif
5926
5927 return success;
5928 }
5929
5930 // Write data in hex to a stream
5931 bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream)
5932 {
5933 wxString hex;
5934 int i;
5935 for (i = 0; i < (int) m_dataSize; i++)
5936 {
5937 hex = wxDecToHex(m_data[i]);
5938 wxCharBuffer buf = hex.ToAscii();
5939
5940 stream.Write((const char*) buf, hex.length());
5941 }
5942
5943 return true;
5944 }
5945
5946 // Read data in hex from a stream
5947 bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, int imageType)
5948 {
5949 int dataSize = length/2;
5950
5951 if (m_data)
5952 delete[] m_data;
5953
5954 wxString str(wxT(" "));
5955 m_data = new unsigned char[dataSize];
5956 int i;
5957 for (i = 0; i < dataSize; i ++)
5958 {
5959 str[0] = stream.GetC();
5960 str[1] = stream.GetC();
5961
5962 m_data[i] = (unsigned char)wxHexToDec(str);
5963 }
5964
5965 m_dataSize = dataSize;
5966 m_imageType = imageType;
5967
5968 return true;
5969 }
5970
5971 // Allocate and read from stream as a block of memory
5972 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream& stream, size_t size)
5973 {
5974 unsigned char* block = new unsigned char[size];
5975 if (!block)
5976 return NULL;
5977
5978 stream.Read(block, size);
5979
5980 return block;
5981 }
5982
5983 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString& filename, size_t size)
5984 {
5985 wxFileInputStream stream(filename);
5986 if (!stream.Ok())
5987 return NULL;
5988
5989 return ReadBlock(stream, size);
5990 }
5991
5992 // Write memory block to stream
5993 bool wxRichTextImageBlock::WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size)
5994 {
5995 stream.Write((void*) block, size);
5996 return stream.IsOk();
5997
5998 }
5999
6000 // Write memory block to file
6001 bool wxRichTextImageBlock::WriteBlock(const wxString& filename, unsigned char* block, size_t size)
6002 {
6003 wxFileOutputStream outStream(filename);
6004 if (!outStream.Ok())
6005 return false;
6006
6007 return WriteBlock(outStream, block, size);
6008 }
6009
6010 #if wxUSE_DATAOBJ
6011
6012 /*!
6013 * The data object for a wxRichTextBuffer
6014 */
6015
6016 const wxChar *wxRichTextBufferDataObject::ms_richTextBufferFormatId = wxT("wxShape");
6017
6018 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer* richTextBuffer)
6019 {
6020 m_richTextBuffer = richTextBuffer;
6021
6022 // this string should uniquely identify our format, but is otherwise
6023 // arbitrary
6024 m_formatRichTextBuffer.SetId(GetRichTextBufferFormatId());
6025
6026 SetFormat(m_formatRichTextBuffer);
6027 }
6028
6029 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
6030 {
6031 delete m_richTextBuffer;
6032 }
6033
6034 // after a call to this function, the richTextBuffer is owned by the caller and it
6035 // is responsible for deleting it!
6036 wxRichTextBuffer* wxRichTextBufferDataObject::GetRichTextBuffer()
6037 {
6038 wxRichTextBuffer* richTextBuffer = m_richTextBuffer;
6039 m_richTextBuffer = NULL;
6040
6041 return richTextBuffer;
6042 }
6043
6044 wxDataFormat wxRichTextBufferDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
6045 {
6046 return m_formatRichTextBuffer;
6047 }
6048
6049 size_t wxRichTextBufferDataObject::GetDataSize() const
6050 {
6051 if (!m_richTextBuffer)
6052 return 0;
6053
6054 wxString bufXML;
6055
6056 {
6057 wxStringOutputStream stream(& bufXML);
6058 if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
6059 {
6060 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
6061 return 0;
6062 }
6063 }
6064
6065 #if wxUSE_UNICODE
6066 wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
6067 return strlen(buffer) + 1;
6068 #else
6069 return bufXML.Length()+1;
6070 #endif
6071 }
6072
6073 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf) const
6074 {
6075 if (!pBuf || !m_richTextBuffer)
6076 return false;
6077
6078 wxString bufXML;
6079
6080 {
6081 wxStringOutputStream stream(& bufXML);
6082 if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
6083 {
6084 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
6085 return 0;
6086 }
6087 }
6088
6089 #if wxUSE_UNICODE
6090 wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
6091 size_t len = strlen(buffer);
6092 memcpy((char*) pBuf, (const char*) buffer, len);
6093 ((char*) pBuf)[len] = 0;
6094 #else
6095 size_t len = bufXML.Length();
6096 memcpy((char*) pBuf, (const char*) bufXML.c_str(), len);
6097 ((char*) pBuf)[len] = 0;
6098 #endif
6099
6100 return true;
6101 }
6102
6103 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len), const void *buf)
6104 {
6105 delete m_richTextBuffer;
6106 m_richTextBuffer = NULL;
6107
6108 wxString bufXML((const char*) buf, wxConvUTF8);
6109
6110 m_richTextBuffer = new wxRichTextBuffer;
6111
6112 wxStringInputStream stream(bufXML);
6113 if (!m_richTextBuffer->LoadFile(stream, wxRICHTEXT_TYPE_XML))
6114 {
6115 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
6116
6117 delete m_richTextBuffer;
6118 m_richTextBuffer = NULL;
6119
6120 return false;
6121 }
6122 return true;
6123 }
6124
6125 #endif
6126 // wxUSE_DATAOBJ
6127
6128 #endif
6129 // wxUSE_RICHTEXT