First cut at printing support for wxRichTextCtrl
[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 tenths of a millimetre to device units
107 int wxRichTextObject::ConvertTenthsMMToPixels(wxDC& dc, int units)
108 {
109 int p = ConvertTenthsMMToPixels(dc.GetPPI().x, units);
110
111 // Unscale
112 wxRichTextBuffer* buffer = GetBuffer();
113 if (buffer)
114 p = (int) ((double)p / buffer->GetScale());
115 return p;
116 }
117
118 // Convert units in tenths of a millimetre to device units
119 int wxRichTextObject::ConvertTenthsMMToPixels(int ppi, int units)
120 {
121 // There are ppi pixels in 254.1 "1/10 mm"
122
123 double pixels = ((double) units * (double)ppi) / 254.1;
124
125 return (int) pixels;
126 }
127
128 /// Dump to output stream for debugging
129 void wxRichTextObject::Dump(wxTextOutputStream& stream)
130 {
131 stream << GetClassInfo()->GetClassName() << wxT("\n");
132 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");
133 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");
134 }
135
136 /// Gets the containing buffer
137 wxRichTextBuffer* wxRichTextObject::GetBuffer() const
138 {
139 const wxRichTextObject* obj = this;
140 while (obj && !obj->IsKindOf(CLASSINFO(wxRichTextBuffer)))
141 obj = obj->GetParent();
142 return wxDynamicCast(obj, wxRichTextBuffer);
143 }
144
145 /*!
146 * wxRichTextCompositeObject
147 * This is the base for drawable objects.
148 */
149
150 IMPLEMENT_CLASS(wxRichTextCompositeObject, wxRichTextObject)
151
152 wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject* parent):
153 wxRichTextObject(parent)
154 {
155 }
156
157 wxRichTextCompositeObject::~wxRichTextCompositeObject()
158 {
159 DeleteChildren();
160 }
161
162 /// Get the nth child
163 wxRichTextObject* wxRichTextCompositeObject::GetChild(size_t n) const
164 {
165 wxASSERT ( n < m_children.GetCount() );
166
167 return m_children.Item(n)->GetData();
168 }
169
170 /// Append a child, returning the position
171 size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject* child)
172 {
173 m_children.Append(child);
174 child->SetParent(this);
175 return m_children.GetCount() - 1;
176 }
177
178 /// Insert the child in front of the given object, or at the beginning
179 bool wxRichTextCompositeObject::InsertChild(wxRichTextObject* child, wxRichTextObject* inFrontOf)
180 {
181 if (inFrontOf)
182 {
183 wxRichTextObjectList::compatibility_iterator node = m_children.Find(inFrontOf);
184 m_children.Insert(node, child);
185 }
186 else
187 m_children.Insert(child);
188 child->SetParent(this);
189
190 return true;
191 }
192
193 /// Delete the child
194 bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject* child, bool deleteChild)
195 {
196 wxRichTextObjectList::compatibility_iterator node = m_children.Find(child);
197 if (node)
198 {
199 wxRichTextObject* obj = node->GetData();
200 m_children.Erase(node);
201 if (deleteChild)
202 delete obj;
203
204 return true;
205 }
206 return false;
207 }
208
209 /// Delete all children
210 bool wxRichTextCompositeObject::DeleteChildren()
211 {
212 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
213 while (node)
214 {
215 wxRichTextObjectList::compatibility_iterator oldNode = node;
216
217 wxRichTextObject* child = node->GetData();
218 child->Dereference(); // Only delete if reference count is zero
219
220 node = node->GetNext();
221 m_children.Erase(oldNode);
222 }
223
224 return true;
225 }
226
227 /// Get the child count
228 size_t wxRichTextCompositeObject::GetChildCount() const
229 {
230 return m_children.GetCount();
231 }
232
233 /// Copy
234 void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject& obj)
235 {
236 wxRichTextObject::Copy(obj);
237
238 DeleteChildren();
239
240 wxRichTextObjectList::compatibility_iterator node = obj.m_children.GetFirst();
241 while (node)
242 {
243 wxRichTextObject* child = node->GetData();
244 wxRichTextObject* newChild = child->Clone();
245 newChild->SetParent(this);
246 m_children.Append(newChild);
247
248 node = node->GetNext();
249 }
250 }
251
252 /// Hit-testing: returns a flag indicating hit test details, plus
253 /// information about position
254 int wxRichTextCompositeObject::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition)
255 {
256 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
257 while (node)
258 {
259 wxRichTextObject* child = node->GetData();
260
261 int ret = child->HitTest(dc, pt, textPosition);
262 if (ret != wxRICHTEXT_HITTEST_NONE)
263 return ret;
264
265 node = node->GetNext();
266 }
267
268 return wxRICHTEXT_HITTEST_NONE;
269 }
270
271 /// Finds the absolute position and row height for the given character position
272 bool wxRichTextCompositeObject::FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart)
273 {
274 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
275 while (node)
276 {
277 wxRichTextObject* child = node->GetData();
278
279 if (child->FindPosition(dc, index, pt, height, forceLineStart))
280 return true;
281
282 node = node->GetNext();
283 }
284
285 return false;
286 }
287
288 /// Calculate range
289 void wxRichTextCompositeObject::CalculateRange(long start, long& end)
290 {
291 long current = start;
292 long lastEnd = current;
293
294 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
295 while (node)
296 {
297 wxRichTextObject* child = node->GetData();
298 long childEnd = 0;
299
300 child->CalculateRange(current, childEnd);
301 lastEnd = childEnd;
302
303 current = childEnd + 1;
304
305 node = node->GetNext();
306 }
307
308 end = lastEnd;
309
310 // An object with no children has zero length
311 if (m_children.GetCount() == 0)
312 end --;
313
314 m_range.SetRange(start, end);
315 }
316
317 /// Delete range from layout.
318 bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange& range)
319 {
320 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
321
322 while (node)
323 {
324 wxRichTextObject* obj = (wxRichTextObject*) node->GetData();
325 wxRichTextObjectList::compatibility_iterator next = node->GetNext();
326
327 // Delete the range in each paragraph
328
329 // When a chunk has been deleted, internally the content does not
330 // now match the ranges.
331 // However, so long as deletion is not done on the same object twice this is OK.
332 // If you may delete content from the same object twice, recalculate
333 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
334 // adjust the range you're deleting accordingly.
335
336 if (!obj->GetRange().IsOutside(range))
337 {
338 obj->DeleteRange(range);
339
340 // Delete an empty object, or paragraph within this range.
341 if (obj->IsEmpty() ||
342 (range.GetStart() <= obj->GetRange().GetStart() && range.GetEnd() >= obj->GetRange().GetEnd()))
343 {
344 // An empty paragraph has length 1, so won't be deleted unless the
345 // whole range is deleted.
346 RemoveChild(obj, true);
347 }
348 }
349
350 node = next;
351 }
352
353 return true;
354 }
355
356 /// Get any text in this object for the given range
357 wxString wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange& range) const
358 {
359 wxString text;
360 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
361 while (node)
362 {
363 wxRichTextObject* child = node->GetData();
364 wxRichTextRange childRange = range;
365 if (!child->GetRange().IsOutside(range))
366 {
367 childRange.LimitTo(child->GetRange());
368
369 wxString childText = child->GetTextForRange(childRange);
370
371 text += childText;
372 }
373 node = node->GetNext();
374 }
375
376 return text;
377 }
378
379 /// Recursively merge all pieces that can be merged.
380 bool wxRichTextCompositeObject::Defragment()
381 {
382 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
383 while (node)
384 {
385 wxRichTextObject* child = node->GetData();
386 wxRichTextCompositeObject* composite = wxDynamicCast(child, wxRichTextCompositeObject);
387 if (composite)
388 composite->Defragment();
389
390 if (node->GetNext())
391 {
392 wxRichTextObject* nextChild = node->GetNext()->GetData();
393 if (child->CanMerge(nextChild) && child->Merge(nextChild))
394 {
395 nextChild->Dereference();
396 m_children.Erase(node->GetNext());
397
398 // Don't set node -- we'll see if we can merge again with the next
399 // child.
400 }
401 else
402 node = node->GetNext();
403 }
404 else
405 node = node->GetNext();
406 }
407
408 return true;
409 }
410
411 /// Dump to output stream for debugging
412 void wxRichTextCompositeObject::Dump(wxTextOutputStream& stream)
413 {
414 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
415 while (node)
416 {
417 wxRichTextObject* child = node->GetData();
418 child->Dump(stream);
419 node = node->GetNext();
420 }
421 }
422
423
424 /*!
425 * wxRichTextBox
426 * This defines a 2D space to lay out objects
427 */
428
429 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox, wxRichTextCompositeObject)
430
431 wxRichTextBox::wxRichTextBox(wxRichTextObject* parent):
432 wxRichTextCompositeObject(parent)
433 {
434 }
435
436 /// Draw the item
437 bool wxRichTextBox::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& WXUNUSED(rect), int descent, int style)
438 {
439 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
440 while (node)
441 {
442 wxRichTextObject* child = node->GetData();
443
444 wxRect childRect = wxRect(child->GetPosition(), child->GetCachedSize());
445 child->Draw(dc, range, selectionRange, childRect, descent, style);
446
447 node = node->GetNext();
448 }
449 return true;
450 }
451
452 /// Lay the item out
453 bool wxRichTextBox::Layout(wxDC& dc, const wxRect& rect, int style)
454 {
455 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
456 while (node)
457 {
458 wxRichTextObject* child = node->GetData();
459 child->Layout(dc, rect, style);
460
461 node = node->GetNext();
462 }
463 m_dirty = false;
464 return true;
465 }
466
467 /// Get/set the size for the given range. Assume only has one child.
468 bool wxRichTextBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const
469 {
470 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
471 if (node)
472 {
473 wxRichTextObject* child = node->GetData();
474 return child->GetRangeSize(range, size, descent, dc, flags, position);
475 }
476 else
477 return false;
478 }
479
480 /// Copy
481 void wxRichTextBox::Copy(const wxRichTextBox& obj)
482 {
483 wxRichTextCompositeObject::Copy(obj);
484 }
485
486
487 /*!
488 * wxRichTextParagraphLayoutBox
489 * This box knows how to lay out paragraphs.
490 */
491
492 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox, wxRichTextBox)
493
494 wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject* parent):
495 wxRichTextBox(parent)
496 {
497 Init();
498 }
499
500 /// Initialize the object.
501 void wxRichTextParagraphLayoutBox::Init()
502 {
503 m_ctrl = NULL;
504
505 // For now, assume is the only box and has no initial size.
506 m_range = wxRichTextRange(0, -1);
507
508 m_invalidRange.SetRange(-1, -1);
509 m_leftMargin = 4;
510 m_rightMargin = 4;
511 m_topMargin = 4;
512 m_bottomMargin = 4;
513 m_partialParagraph = false;
514 }
515
516 /// Draw the item
517 bool wxRichTextParagraphLayoutBox::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style)
518 {
519 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
520 while (node)
521 {
522 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
523 wxASSERT (child != NULL);
524
525 if (child && !child->GetRange().IsOutside(range))
526 {
527 wxRect childRect(child->GetPosition(), child->GetCachedSize());
528
529 if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) == 0) && childRect.GetTop() > rect.GetBottom() || childRect.GetBottom() < rect.GetTop())
530 {
531 // Skip
532 }
533 else
534 child->Draw(dc, child->GetRange(), selectionRange, childRect, descent, style);
535 }
536
537 node = node->GetNext();
538 }
539 return true;
540 }
541
542 /// Lay the item out
543 bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int style)
544 {
545 wxRect availableSpace;
546 bool formatRect = (style & wxRICHTEXT_LAYOUT_SPECIFIED_RECT) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
547
548 // If only laying out a specific area, the passed rect has a different meaning:
549 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
550 // so that during a size, only the visible part will be relaid out, or
551 // it would take too long causing flicker. As an approximation, we assume that
552 // everything up to the start of the visible area is laid out correctly.
553 if (formatRect)
554 {
555 availableSpace = wxRect(0 + m_leftMargin,
556 0 + m_topMargin,
557 rect.width - m_leftMargin - m_rightMargin,
558 rect.height);
559
560 // Invalidate the part of the buffer from the first visible line
561 // to the end. If other parts of the buffer are currently invalid,
562 // then they too will be taken into account if they are above
563 // the visible point.
564 long startPos = 0;
565 wxRichTextLine* line = GetLineAtYPosition(rect.y);
566 if (line)
567 startPos = line->GetAbsoluteRange().GetStart();
568
569 Invalidate(wxRichTextRange(startPos, GetRange().GetEnd()));
570 }
571 else
572 availableSpace = wxRect(rect.x + m_leftMargin,
573 rect.y + m_topMargin,
574 rect.width - m_leftMargin - m_rightMargin,
575 rect.height - m_topMargin - m_bottomMargin);
576
577 int maxWidth = 0;
578
579 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
580
581 bool layoutAll = true;
582
583 // Get invalid range, rounding to paragraph start/end.
584 wxRichTextRange invalidRange = GetInvalidRange(true);
585
586 if (invalidRange == wxRICHTEXT_NONE && !formatRect)
587 return true;
588
589 if (invalidRange == wxRICHTEXT_ALL)
590 layoutAll = true;
591 else // If we know what range is affected, start laying out from that point on.
592 if (invalidRange.GetStart() > GetRange().GetStart())
593 {
594 wxRichTextParagraph* firstParagraph = GetParagraphAtPosition(invalidRange.GetStart());
595 if (firstParagraph)
596 {
597 wxRichTextObjectList::compatibility_iterator firstNode = m_children.Find(firstParagraph);
598 wxRichTextObjectList::compatibility_iterator previousNode;
599 if ( firstNode )
600 previousNode = firstNode->GetPrevious();
601 if (firstNode && previousNode)
602 {
603 wxRichTextParagraph* previousParagraph = wxDynamicCast(previousNode->GetData(), wxRichTextParagraph);
604 availableSpace.y = previousParagraph->GetPosition().y + previousParagraph->GetCachedSize().y;
605
606 // Now we're going to start iterating from the first affected paragraph.
607 node = firstNode;
608
609 layoutAll = false;
610 }
611 }
612 }
613
614 // A way to force speedy rest-of-buffer layout (the 'else' below)
615 bool forceQuickLayout = false;
616
617 while (node)
618 {
619 // Assume this box only contains paragraphs
620
621 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
622 wxCHECK_MSG( child, false, _T("Unknown object in layout") );
623
624 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
625 if ( !forceQuickLayout &&
626 (layoutAll ||
627 child->GetLines().IsEmpty() ||
628 !child->GetRange().IsOutside(invalidRange)) )
629 {
630 child->Layout(dc, availableSpace, style);
631
632 // Layout must set the cached size
633 availableSpace.y += child->GetCachedSize().y;
634 maxWidth = wxMax(maxWidth, child->GetCachedSize().x);
635
636 // If we're just formatting the visible part of the buffer,
637 // and we're now past the bottom of the window, start quick
638 // layout.
639 if (formatRect && child->GetPosition().y > rect.GetBottom())
640 forceQuickLayout = true;
641 }
642 else
643 {
644 // We're outside the immediately affected range, so now let's just
645 // move everything up or down. This assumes that all the children have previously
646 // been laid out and have wrapped line lists associated with them.
647 // TODO: check all paragraphs before the affected range.
648
649 int inc = availableSpace.y - child->GetPosition().y;
650
651 while (node)
652 {
653 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
654 if (child)
655 {
656 if (child->GetLines().GetCount() == 0)
657 child->Layout(dc, availableSpace, style);
658 else
659 child->SetPosition(wxPoint(child->GetPosition().x, child->GetPosition().y + inc));
660
661 availableSpace.y += child->GetCachedSize().y;
662 maxWidth = wxMax(maxWidth, child->GetCachedSize().x);
663 }
664
665 node = node->GetNext();
666 }
667 break;
668 }
669
670 node = node->GetNext();
671 }
672
673 SetCachedSize(wxSize(maxWidth, availableSpace.y));
674
675 m_dirty = false;
676 m_invalidRange = wxRICHTEXT_NONE;
677
678 return true;
679 }
680
681 /// Copy
682 void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox& obj)
683 {
684 wxRichTextBox::Copy(obj);
685
686 m_partialParagraph = obj.m_partialParagraph;
687 }
688
689 /// Get/set the size for the given range.
690 bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const
691 {
692 wxSize sz;
693
694 wxRichTextObjectList::compatibility_iterator startPara = wxRichTextObjectList::compatibility_iterator();
695 wxRichTextObjectList::compatibility_iterator endPara = wxRichTextObjectList::compatibility_iterator();
696
697 // First find the first paragraph whose starting position is within the range.
698 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
699 while (node)
700 {
701 // child is a paragraph
702 wxRichTextObject* child = node->GetData();
703 const wxRichTextRange& r = child->GetRange();
704
705 if (r.GetStart() <= range.GetStart() && r.GetEnd() >= range.GetStart())
706 {
707 startPara = node;
708 break;
709 }
710
711 node = node->GetNext();
712 }
713
714 // Next find the last paragraph containing part of the range
715 node = m_children.GetFirst();
716 while (node)
717 {
718 // child is a paragraph
719 wxRichTextObject* child = node->GetData();
720 const wxRichTextRange& r = child->GetRange();
721
722 if (r.GetStart() <= range.GetEnd() && r.GetEnd() >= range.GetEnd())
723 {
724 endPara = node;
725 break;
726 }
727
728 node = node->GetNext();
729 }
730
731 if (!startPara || !endPara)
732 return false;
733
734 // Now we can add up the sizes
735 for (node = startPara; node ; node = node->GetNext())
736 {
737 // child is a paragraph
738 wxRichTextObject* child = node->GetData();
739 const wxRichTextRange& childRange = child->GetRange();
740 wxRichTextRange rangeToFind = range;
741 rangeToFind.LimitTo(childRange);
742
743 wxSize childSize;
744
745 int childDescent = 0;
746 child->GetRangeSize(rangeToFind, childSize, childDescent, dc, flags, position);
747
748 descent = wxMax(childDescent, descent);
749
750 sz.x = wxMax(sz.x, childSize.x);
751 sz.y += childSize.y;
752
753 if (node == endPara)
754 break;
755 }
756
757 size = sz;
758
759 return true;
760 }
761
762 /// Get the paragraph at the given position
763 wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos, bool caretPosition) const
764 {
765 if (caretPosition)
766 pos ++;
767
768 // First find the first paragraph whose starting position is within the range.
769 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
770 while (node)
771 {
772 // child is a paragraph
773 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
774 wxASSERT (child != NULL);
775
776 // Return first child in buffer if position is -1
777 // if (pos == -1)
778 // return child;
779
780 if (child->GetRange().Contains(pos))
781 return child;
782
783 node = node->GetNext();
784 }
785 return NULL;
786 }
787
788 /// Get the line at the given position
789 wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos, bool caretPosition) const
790 {
791 if (caretPosition)
792 pos ++;
793
794 // First find the first paragraph whose starting position is within the range.
795 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
796 while (node)
797 {
798 // child is a paragraph
799 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
800 wxASSERT (child != NULL);
801
802 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
803 while (node2)
804 {
805 wxRichTextLine* line = node2->GetData();
806
807 wxRichTextRange range = line->GetAbsoluteRange();
808
809 if (range.Contains(pos) ||
810
811 // If the position is end-of-paragraph, then return the last line of
812 // of the paragraph.
813 (range.GetEnd() == child->GetRange().GetEnd()-1) && (pos == child->GetRange().GetEnd()))
814 return line;
815
816 node2 = node2->GetNext();
817 }
818
819 node = node->GetNext();
820 }
821
822 int lineCount = GetLineCount();
823 if (lineCount > 0)
824 return GetLineForVisibleLineNumber(lineCount-1);
825 else
826 return NULL;
827 }
828
829 /// Get the line at the given y pixel position, or the last line.
830 wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y) const
831 {
832 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
833 while (node)
834 {
835 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
836 wxASSERT (child != NULL);
837
838 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
839 while (node2)
840 {
841 wxRichTextLine* line = node2->GetData();
842
843 wxRect rect(line->GetRect());
844
845 if (y <= rect.GetBottom())
846 return line;
847
848 node2 = node2->GetNext();
849 }
850
851 node = node->GetNext();
852 }
853
854 // Return last line
855 int lineCount = GetLineCount();
856 if (lineCount > 0)
857 return GetLineForVisibleLineNumber(lineCount-1);
858 else
859 return NULL;
860 }
861
862 /// Get the number of visible lines
863 int wxRichTextParagraphLayoutBox::GetLineCount() const
864 {
865 int count = 0;
866
867 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
868 while (node)
869 {
870 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
871 wxASSERT (child != NULL);
872
873 count += child->GetLines().GetCount();
874 node = node->GetNext();
875 }
876 return count;
877 }
878
879
880 /// Get the paragraph for a given line
881 wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine* line) const
882 {
883 return GetParagraphAtPosition(line->GetAbsoluteRange().GetStart());
884 }
885
886 /// Get the line size at the given position
887 wxSize wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos, bool caretPosition) const
888 {
889 wxRichTextLine* line = GetLineAtPosition(pos, caretPosition);
890 if (line)
891 {
892 return line->GetSize();
893 }
894 else
895 return wxSize(0, 0);
896 }
897
898
899 /// Convenience function to add a paragraph of text
900 wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraph(const wxString& text, wxTextAttrEx* paraStyle)
901 {
902 #if wxRICHTEXT_USE_DYNAMIC_STYLES
903 // Don't use the base style, just the default style, and the base style will
904 // be combined at display time
905 wxTextAttrEx style(GetDefaultStyle());
906 #else
907 wxTextAttrEx style(GetAttributes());
908
909 // Apply default style. If the style has no attributes set,
910 // then the attributes will remain the 'basic style' (i.e. the
911 // layout box's style).
912 wxRichTextApplyStyle(style, GetDefaultStyle());
913 #endif
914 wxRichTextParagraph* para = new wxRichTextParagraph(text, this, & style);
915 if (paraStyle)
916 para->SetAttributes(*paraStyle);
917
918 AppendChild(para);
919
920 UpdateRanges();
921 SetDirty(true);
922
923 return para->GetRange();
924 }
925
926 /// Adds multiple paragraphs, based on newlines.
927 wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text, wxTextAttrEx* paraStyle)
928 {
929 #if wxRICHTEXT_USE_DYNAMIC_STYLES
930 // Don't use the base style, just the default style, and the base style will
931 // be combined at display time
932 wxTextAttrEx style(GetDefaultStyle());
933 #else
934 wxTextAttrEx style(GetAttributes());
935
936 //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName());
937 //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize());
938
939 // Apply default style. If the style has no attributes set,
940 // then the attributes will remain the 'basic style' (i.e. the
941 // layout box's style).
942 wxRichTextApplyStyle(style, GetDefaultStyle());
943
944 //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName());
945 //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize());
946 #endif
947
948 wxRichTextParagraph* firstPara = NULL;
949 wxRichTextParagraph* lastPara = NULL;
950
951 wxRichTextRange range(-1, -1);
952
953 size_t i = 0;
954 size_t len = text.length();
955 wxString line;
956 wxRichTextParagraph* para = new wxRichTextParagraph(wxEmptyString, this, & style);
957 if (paraStyle)
958 para->SetAttributes(*paraStyle);
959
960 AppendChild(para);
961
962 firstPara = para;
963 lastPara = para;
964
965 while (i < len)
966 {
967 wxChar ch = text[i];
968 if (ch == wxT('\n') || ch == wxT('\r'))
969 {
970 wxRichTextPlainText* plainText = (wxRichTextPlainText*) para->GetChildren().GetFirst()->GetData();
971 plainText->SetText(line);
972
973 para = new wxRichTextParagraph(wxEmptyString, this, & style);
974 if (paraStyle)
975 para->SetAttributes(*paraStyle);
976
977 AppendChild(para);
978
979 //if (!firstPara)
980 // firstPara = para;
981
982 lastPara = para;
983 line = wxEmptyString;
984 }
985 else
986 line += ch;
987
988 i ++;
989 }
990
991 if (!line.empty())
992 {
993 wxRichTextPlainText* plainText = (wxRichTextPlainText*) para->GetChildren().GetFirst()->GetData();
994 plainText->SetText(line);
995 }
996
997 /*
998 if (firstPara)
999 range.SetStart(firstPara->GetRange().GetStart());
1000 else if (lastPara)
1001 range.SetStart(lastPara->GetRange().GetStart());
1002
1003 if (lastPara)
1004 range.SetEnd(lastPara->GetRange().GetEnd());
1005 else if (firstPara)
1006 range.SetEnd(firstPara->GetRange().GetEnd());
1007 */
1008
1009 UpdateRanges();
1010
1011 SetDirty(false);
1012
1013 return wxRichTextRange(firstPara->GetRange().GetStart(), lastPara->GetRange().GetEnd());
1014 }
1015
1016 /// Convenience function to add an image
1017 wxRichTextRange wxRichTextParagraphLayoutBox::AddImage(const wxImage& image, wxTextAttrEx* paraStyle)
1018 {
1019 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1020 // Don't use the base style, just the default style, and the base style will
1021 // be combined at display time
1022 wxTextAttrEx style(GetDefaultStyle());
1023 #else
1024 wxTextAttrEx style(GetAttributes());
1025
1026 // Apply default style. If the style has no attributes set,
1027 // then the attributes will remain the 'basic style' (i.e. the
1028 // layout box's style).
1029 wxRichTextApplyStyle(style, GetDefaultStyle());
1030 #endif
1031
1032 wxRichTextParagraph* para = new wxRichTextParagraph(this, & style);
1033 AppendChild(para);
1034 para->AppendChild(new wxRichTextImage(image, this));
1035
1036 if (paraStyle)
1037 para->SetAttributes(*paraStyle);
1038
1039 UpdateRanges();
1040 SetDirty(true);
1041
1042 return para->GetRange();
1043 }
1044
1045
1046 /// Insert fragment into this box at the given position. If partialParagraph is true,
1047 /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
1048 /// marker.
1049 /// TODO: if fragment is inserted inside styled fragment, must apply that style to
1050 /// to the data (if it has a default style, anyway).
1051
1052 bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParagraphLayoutBox& fragment)
1053 {
1054 SetDirty(true);
1055
1056 // First, find the first paragraph whose starting position is within the range.
1057 wxRichTextParagraph* para = GetParagraphAtPosition(position);
1058 if (para)
1059 {
1060 wxRichTextObjectList::compatibility_iterator node = m_children.Find(para);
1061
1062 // Now split at this position, returning the object to insert the new
1063 // ones in front of.
1064 wxRichTextObject* nextObject = para->SplitAt(position);
1065
1066 // Special case: partial paragraph, just one paragraph. Might be a small amount of
1067 // text, for example, so let's optimize.
1068
1069 if (fragment.GetPartialParagraph() && fragment.GetChildren().GetCount() == 1)
1070 {
1071 // Add the first para to this para...
1072 wxRichTextObjectList::compatibility_iterator firstParaNode = fragment.GetChildren().GetFirst();
1073 if (!firstParaNode)
1074 return false;
1075
1076 // Iterate through the fragment paragraph inserting the content into this paragraph.
1077 wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph);
1078 wxASSERT (firstPara != NULL);
1079
1080 wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst();
1081 while (objectNode)
1082 {
1083 wxRichTextObject* newObj = objectNode->GetData()->Clone();
1084
1085 if (!nextObject)
1086 {
1087 // Append
1088 para->AppendChild(newObj);
1089 }
1090 else
1091 {
1092 // Insert before nextObject
1093 para->InsertChild(newObj, nextObject);
1094 }
1095
1096 objectNode = objectNode->GetNext();
1097 }
1098
1099 return true;
1100 }
1101 else
1102 {
1103 // Procedure for inserting a fragment consisting of a number of
1104 // paragraphs:
1105 //
1106 // 1. Remove and save the content that's after the insertion point, for adding
1107 // back once we've added the fragment.
1108 // 2. Add the content from the first fragment paragraph to the current
1109 // paragraph.
1110 // 3. Add remaining fragment paragraphs after the current paragraph.
1111 // 4. Add back the saved content from the first paragraph. If partialParagraph
1112 // is true, add it to the last paragraph added and not a new one.
1113
1114 // 1. Remove and save objects after split point.
1115 wxList savedObjects;
1116 if (nextObject)
1117 para->MoveToList(nextObject, savedObjects);
1118
1119 // 2. Add the content from the 1st fragment paragraph.
1120 wxRichTextObjectList::compatibility_iterator firstParaNode = fragment.GetChildren().GetFirst();
1121 if (!firstParaNode)
1122 return false;
1123
1124 wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph);
1125 wxASSERT(firstPara != NULL);
1126
1127 wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst();
1128 while (objectNode)
1129 {
1130 wxRichTextObject* newObj = objectNode->GetData()->Clone();
1131
1132 // Append
1133 para->AppendChild(newObj);
1134
1135 objectNode = objectNode->GetNext();
1136 }
1137
1138 // 3. Add remaining fragment paragraphs after the current paragraph.
1139 wxRichTextObjectList::compatibility_iterator nextParagraphNode = node->GetNext();
1140 wxRichTextObject* nextParagraph = NULL;
1141 if (nextParagraphNode)
1142 nextParagraph = nextParagraphNode->GetData();
1143
1144 wxRichTextObjectList::compatibility_iterator i = fragment.GetChildren().GetFirst()->GetNext();
1145 wxRichTextParagraph* finalPara = para;
1146
1147 // If there was only one paragraph, we need to insert a new one.
1148 if (!i)
1149 {
1150 finalPara = new wxRichTextParagraph;
1151
1152 // TODO: These attributes should come from the subsequent paragraph
1153 // when originally deleted, since the subsequent para takes on
1154 // the previous para's attributes.
1155 finalPara->SetAttributes(firstPara->GetAttributes());
1156
1157 if (nextParagraph)
1158 InsertChild(finalPara, nextParagraph);
1159 else
1160 AppendChild(finalPara);
1161 }
1162 else while (i)
1163 {
1164 wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph);
1165 wxASSERT( para != NULL );
1166
1167 finalPara = (wxRichTextParagraph*) para->Clone();
1168
1169 if (nextParagraph)
1170 InsertChild(finalPara, nextParagraph);
1171 else
1172 AppendChild(finalPara);
1173
1174 i = i->GetNext();
1175 }
1176
1177 // 4. Add back the remaining content.
1178 if (finalPara)
1179 {
1180 finalPara->MoveFromList(savedObjects);
1181
1182 // Ensure there's at least one object
1183 if (finalPara->GetChildCount() == 0)
1184 {
1185 wxRichTextPlainText* text = new wxRichTextPlainText(wxEmptyString);
1186 #if !wxRICHTEXT_USE_DYNAMIC_STYLES
1187 text->SetAttributes(finalPara->GetAttributes());
1188 #endif
1189
1190 finalPara->AppendChild(text);
1191 }
1192 }
1193
1194 return true;
1195 }
1196 }
1197 else
1198 {
1199 // Append
1200 wxRichTextObjectList::compatibility_iterator i = fragment.GetChildren().GetFirst();
1201 while (i)
1202 {
1203 wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph);
1204 wxASSERT( para != NULL );
1205
1206 AppendChild(para->Clone());
1207
1208 i = i->GetNext();
1209 }
1210
1211 return true;
1212 }
1213 }
1214
1215 /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
1216 /// If there was an incomplete paragraph at the end, partialParagraph is set to true.
1217 bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange& range, wxRichTextParagraphLayoutBox& fragment)
1218 {
1219 wxRichTextObjectList::compatibility_iterator i = GetChildren().GetFirst();
1220 while (i)
1221 {
1222 wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph);
1223 wxASSERT( para != NULL );
1224
1225 if (!para->GetRange().IsOutside(range))
1226 {
1227 fragment.AppendChild(para->Clone());
1228 }
1229 i = i->GetNext();
1230 }
1231
1232 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
1233 if (!fragment.IsEmpty())
1234 {
1235 wxRichTextRange topTailRange(range);
1236
1237 wxRichTextParagraph* firstPara = wxDynamicCast(fragment.GetChildren().GetFirst()->GetData(), wxRichTextParagraph);
1238 wxASSERT( firstPara != NULL );
1239
1240 // Chop off the start of the paragraph
1241 if (topTailRange.GetStart() > firstPara->GetRange().GetStart())
1242 {
1243 wxRichTextRange r(firstPara->GetRange().GetStart(), topTailRange.GetStart()-1);
1244 firstPara->DeleteRange(r);
1245
1246 // Make sure the numbering is correct
1247 long end;
1248 fragment.CalculateRange(firstPara->GetRange().GetStart(), end);
1249
1250 // Now, we've deleted some positions, so adjust the range
1251 // accordingly.
1252 topTailRange.SetEnd(topTailRange.GetEnd() - r.GetLength());
1253 }
1254
1255 wxRichTextParagraph* lastPara = wxDynamicCast(fragment.GetChildren().GetLast()->GetData(), wxRichTextParagraph);
1256 wxASSERT( lastPara != NULL );
1257
1258 if (topTailRange.GetEnd() < (lastPara->GetRange().GetEnd()-1))
1259 {
1260 wxRichTextRange r(topTailRange.GetEnd()+1, lastPara->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */
1261 lastPara->DeleteRange(r);
1262
1263 // Make sure the numbering is correct
1264 long end;
1265 fragment.CalculateRange(firstPara->GetRange().GetStart(), end);
1266
1267 // We only have part of a paragraph at the end
1268 fragment.SetPartialParagraph(true);
1269 }
1270 else
1271 {
1272 if (topTailRange.GetEnd() == (lastPara->GetRange().GetEnd() - 1))
1273 // We have a partial paragraph (don't save last new paragraph marker)
1274 fragment.SetPartialParagraph(true);
1275 else
1276 // We have a complete paragraph
1277 fragment.SetPartialParagraph(false);
1278 }
1279 }
1280
1281 return true;
1282 }
1283
1284 /// Given a position, get the number of the visible line (potentially many to a paragraph),
1285 /// starting from zero at the start of the buffer.
1286 long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos, bool caretPosition, bool startOfLine) const
1287 {
1288 if (caretPosition)
1289 pos ++;
1290
1291 int lineCount = 0;
1292
1293 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1294 while (node)
1295 {
1296 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1297 wxASSERT( child != NULL );
1298
1299 if (child->GetRange().Contains(pos))
1300 {
1301 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
1302 while (node2)
1303 {
1304 wxRichTextLine* line = node2->GetData();
1305 wxRichTextRange lineRange = line->GetAbsoluteRange();
1306
1307 if (lineRange.Contains(pos))
1308 {
1309 // If the caret is displayed at the end of the previous wrapped line,
1310 // we want to return the line it's _displayed_ at (not the actual line
1311 // containing the position).
1312 if (lineRange.GetStart() == pos && !startOfLine && child->GetRange().GetStart() != pos)
1313 return lineCount - 1;
1314 else
1315 return lineCount;
1316 }
1317
1318 lineCount ++;
1319
1320 node2 = node2->GetNext();
1321 }
1322 // If we didn't find it in the lines, it must be
1323 // the last position of the paragraph. So return the last line.
1324 return lineCount-1;
1325 }
1326 else
1327 lineCount += child->GetLines().GetCount();
1328
1329 node = node->GetNext();
1330 }
1331
1332 // Not found
1333 return -1;
1334 }
1335
1336 /// Given a line number, get the corresponding wxRichTextLine object.
1337 wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber) const
1338 {
1339 int lineCount = 0;
1340
1341 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1342 while (node)
1343 {
1344 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1345 wxASSERT(child != NULL);
1346
1347 if (lineNumber < (int) (child->GetLines().GetCount() + lineCount))
1348 {
1349 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
1350 while (node2)
1351 {
1352 wxRichTextLine* line = node2->GetData();
1353
1354 if (lineCount == lineNumber)
1355 return line;
1356
1357 lineCount ++;
1358
1359 node2 = node2->GetNext();
1360 }
1361 }
1362 else
1363 lineCount += child->GetLines().GetCount();
1364
1365 node = node->GetNext();
1366 }
1367
1368 // Didn't find it
1369 return NULL;
1370 }
1371
1372 /// Delete range from layout.
1373 bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange& range)
1374 {
1375 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1376
1377 while (node)
1378 {
1379 wxRichTextParagraph* obj = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1380 wxASSERT (obj != NULL);
1381
1382 wxRichTextObjectList::compatibility_iterator next = node->GetNext();
1383
1384 // Delete the range in each paragraph
1385
1386 if (!obj->GetRange().IsOutside(range))
1387 {
1388 // Deletes the content of this object within the given range
1389 obj->DeleteRange(range);
1390
1391 // If the whole paragraph is within the range to delete,
1392 // delete the whole thing.
1393 if (range.GetStart() <= obj->GetRange().GetStart() && range.GetEnd() >= obj->GetRange().GetEnd())
1394 {
1395 // Delete the whole object
1396 RemoveChild(obj, true);
1397 }
1398 // If the range includes the paragraph end, we need to join this
1399 // and the next paragraph.
1400 else if (range.Contains(obj->GetRange().GetEnd()))
1401 {
1402 // We need to move the objects from the next paragraph
1403 // to this paragraph
1404
1405 if (next)
1406 {
1407 wxRichTextParagraph* nextParagraph = wxDynamicCast(next->GetData(), wxRichTextParagraph);
1408 next = next->GetNext();
1409 if (nextParagraph)
1410 {
1411 // Delete the stuff we need to delete
1412 nextParagraph->DeleteRange(range);
1413
1414 // Move the objects to the previous para
1415 wxRichTextObjectList::compatibility_iterator node1 = nextParagraph->GetChildren().GetFirst();
1416
1417 while (node1)
1418 {
1419 wxRichTextObject* obj1 = node1->GetData();
1420
1421 // If the object is empty, optimise it out
1422 if (obj1->IsEmpty())
1423 {
1424 delete obj1;
1425 }
1426 else
1427 {
1428 obj->AppendChild(obj1);
1429 }
1430
1431 wxRichTextObjectList::compatibility_iterator next1 = node1->GetNext();
1432 nextParagraph->GetChildren().Erase(node1);
1433
1434 node1 = next1;
1435 }
1436
1437 // Delete the paragraph
1438 RemoveChild(nextParagraph, true);
1439
1440 }
1441 }
1442
1443 }
1444 }
1445
1446 node = next;
1447 }
1448
1449 return true;
1450 }
1451
1452 /// Get any text in this object for the given range
1453 wxString wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange& range) const
1454 {
1455 int lineCount = 0;
1456 wxString text;
1457 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1458 while (node)
1459 {
1460 wxRichTextObject* child = node->GetData();
1461 if (!child->GetRange().IsOutside(range))
1462 {
1463 // if (lineCount > 0)
1464 // text += wxT("\n");
1465 wxRichTextRange childRange = range;
1466 childRange.LimitTo(child->GetRange());
1467
1468 wxString childText = child->GetTextForRange(childRange);
1469
1470 text += childText;
1471
1472 if (childRange.GetEnd() == child->GetRange().GetEnd())
1473 text += wxT("\n");
1474
1475 lineCount ++;
1476 }
1477 node = node->GetNext();
1478 }
1479
1480 return text;
1481 }
1482
1483 /// Get all the text
1484 wxString wxRichTextParagraphLayoutBox::GetText() const
1485 {
1486 return GetTextForRange(GetRange());
1487 }
1488
1489 /// Get the paragraph by number
1490 wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber) const
1491 {
1492 if ((size_t) paragraphNumber >= GetChildCount())
1493 return NULL;
1494
1495 return (wxRichTextParagraph*) GetChild((size_t) paragraphNumber);
1496 }
1497
1498 /// Get the length of the paragraph
1499 int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber) const
1500 {
1501 wxRichTextParagraph* para = GetParagraphAtLine(paragraphNumber);
1502 if (para)
1503 return para->GetRange().GetLength() - 1; // don't include newline
1504 else
1505 return 0;
1506 }
1507
1508 /// Get the text of the paragraph
1509 wxString wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber) const
1510 {
1511 wxRichTextParagraph* para = GetParagraphAtLine(paragraphNumber);
1512 if (para)
1513 return para->GetTextForRange(para->GetRange());
1514 else
1515 return wxEmptyString;
1516 }
1517
1518 /// Convert zero-based line column and paragraph number to a position.
1519 long wxRichTextParagraphLayoutBox::XYToPosition(long x, long y) const
1520 {
1521 wxRichTextParagraph* para = GetParagraphAtLine(y);
1522 if (para)
1523 {
1524 return para->GetRange().GetStart() + x;
1525 }
1526 else
1527 return -1;
1528 }
1529
1530 /// Convert zero-based position to line column and paragraph number
1531 bool wxRichTextParagraphLayoutBox::PositionToXY(long pos, long* x, long* y) const
1532 {
1533 wxRichTextParagraph* para = GetParagraphAtPosition(pos);
1534 if (para)
1535 {
1536 int count = 0;
1537 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1538 while (node)
1539 {
1540 wxRichTextObject* child = node->GetData();
1541 if (child == para)
1542 break;
1543 count ++;
1544 node = node->GetNext();
1545 }
1546
1547 *y = count;
1548 *x = pos - para->GetRange().GetStart();
1549
1550 return true;
1551 }
1552 else
1553 return false;
1554 }
1555
1556 /// Get the leaf object in a paragraph at this position.
1557 /// Given a line number, get the corresponding wxRichTextLine object.
1558 wxRichTextObject* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position) const
1559 {
1560 wxRichTextParagraph* para = GetParagraphAtPosition(position);
1561 if (para)
1562 {
1563 wxRichTextObjectList::compatibility_iterator node = para->GetChildren().GetFirst();
1564
1565 while (node)
1566 {
1567 wxRichTextObject* child = node->GetData();
1568 if (child->GetRange().Contains(position))
1569 return child;
1570
1571 node = node->GetNext();
1572 }
1573 if (position == para->GetRange().GetEnd() && para->GetChildCount() > 0)
1574 return para->GetChildren().GetLast()->GetData();
1575 }
1576 return NULL;
1577 }
1578
1579 /// Set character or paragraph text attributes: apply character styles only to immediate text nodes
1580 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style, int flags)
1581 {
1582 bool characterStyle = false;
1583 bool paragraphStyle = false;
1584
1585 if (style.IsCharacterStyle())
1586 characterStyle = true;
1587 if (style.IsParagraphStyle())
1588 paragraphStyle = true;
1589
1590 bool withUndo = ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0);
1591 bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
1592 bool parasOnly = ((flags & wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY) != 0);
1593 bool charactersOnly = ((flags & wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY) != 0);
1594
1595 // Limit the attributes to be set to the content to only character attributes.
1596 wxRichTextAttr characterAttributes(style);
1597 characterAttributes.SetFlags(characterAttributes.GetFlags() & (wxTEXT_ATTR_CHARACTER));
1598
1599 // If we are associated with a control, make undoable; otherwise, apply immediately
1600 // to the data.
1601
1602 bool haveControl = (GetRichTextCtrl() != NULL);
1603
1604 wxRichTextAction* action = NULL;
1605
1606 if (haveControl && withUndo)
1607 {
1608 action = new wxRichTextAction(NULL, _("Change Style"), wxRICHTEXT_CHANGE_STYLE, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
1609 action->SetRange(range);
1610 action->SetPosition(GetRichTextCtrl()->GetCaretPosition());
1611 }
1612
1613 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1614 while (node)
1615 {
1616 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1617 wxASSERT (para != NULL);
1618
1619 if (para && para->GetChildCount() > 0)
1620 {
1621 // Stop searching if we're beyond the range of interest
1622 if (para->GetRange().GetStart() > range.GetEnd())
1623 break;
1624
1625 if (!para->GetRange().IsOutside(range))
1626 {
1627 // We'll be using a copy of the paragraph to make style changes,
1628 // not updating the buffer directly.
1629 wxRichTextParagraph* newPara wxDUMMY_INITIALIZE(NULL);
1630
1631 if (haveControl && withUndo)
1632 {
1633 newPara = new wxRichTextParagraph(*para);
1634 action->GetNewParagraphs().AppendChild(newPara);
1635
1636 // Also store the old ones for Undo
1637 action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para));
1638 }
1639 else
1640 newPara = para;
1641
1642 if (paragraphStyle && !charactersOnly)
1643 {
1644 if (applyMinimal)
1645 {
1646 // Only apply attributes that will make a difference to the combined
1647 // style as seen on the display
1648 wxRichTextAttr combinedAttr(para->GetCombinedAttributes());
1649 wxRichTextApplyStyle(newPara->GetAttributes(), style, & combinedAttr);
1650 }
1651 else
1652 wxRichTextApplyStyle(newPara->GetAttributes(), style);
1653 }
1654
1655 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1656 // If applying paragraph styles dynamically, don't change the text objects' attributes
1657 // since they will computed as needed. Only apply the character styling if it's _only_
1658 // character styling. This policy is subject to change and might be put under user control.
1659
1660 // Hm. we might well be applying a mix of paragraph and character styles, in which
1661 // case we _do_ want to apply character styles regardless of what para styles are set.
1662 // But if we're applying a paragraph style, which has some character attributes, but
1663 // we only want the paragraphs to hold this character style, then we _don't_ want to
1664 // apply the character style. So we need to be able to choose.
1665
1666 // if (!paragraphStyle && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1667 if (!parasOnly && characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1668 #else
1669 if (characterStyle && range.GetStart() != newPara->GetRange().GetEnd())
1670 #endif
1671 {
1672 wxRichTextRange childRange(range);
1673 childRange.LimitTo(newPara->GetRange());
1674
1675 // Find the starting position and if necessary split it so
1676 // we can start applying a different style.
1677 // TODO: check that the style actually changes or is different
1678 // from style outside of range
1679 wxRichTextObject* firstObject wxDUMMY_INITIALIZE(NULL);
1680 wxRichTextObject* lastObject wxDUMMY_INITIALIZE(NULL);
1681
1682 if (childRange.GetStart() == newPara->GetRange().GetStart())
1683 firstObject = newPara->GetChildren().GetFirst()->GetData();
1684 else
1685 firstObject = newPara->SplitAt(range.GetStart());
1686
1687 // Increment by 1 because we're apply the style one _after_ the split point
1688 long splitPoint = childRange.GetEnd();
1689 if (splitPoint != newPara->GetRange().GetEnd())
1690 splitPoint ++;
1691
1692 // Find last object
1693 if (splitPoint == newPara->GetRange().GetEnd() || splitPoint == (newPara->GetRange().GetEnd() - 1))
1694 lastObject = newPara->GetChildren().GetLast()->GetData();
1695 else
1696 // lastObject is set as a side-effect of splitting. It's
1697 // returned as the object before the new object.
1698 (void) newPara->SplitAt(splitPoint, & lastObject);
1699
1700 wxASSERT(firstObject != NULL);
1701 wxASSERT(lastObject != NULL);
1702
1703 if (!firstObject || !lastObject)
1704 continue;
1705
1706 wxRichTextObjectList::compatibility_iterator firstNode = newPara->GetChildren().Find(firstObject);
1707 wxRichTextObjectList::compatibility_iterator lastNode = newPara->GetChildren().Find(lastObject);
1708
1709 wxASSERT(firstNode);
1710 wxASSERT(lastNode);
1711
1712 wxRichTextObjectList::compatibility_iterator node2 = firstNode;
1713
1714 while (node2)
1715 {
1716 wxRichTextObject* child = node2->GetData();
1717
1718 if (applyMinimal)
1719 {
1720 // Only apply attributes that will make a difference to the combined
1721 // style as seen on the display
1722 wxRichTextAttr combinedAttr(newPara->GetCombinedAttributes(child->GetAttributes()));
1723 wxRichTextApplyStyle(child->GetAttributes(), characterAttributes, & combinedAttr);
1724 }
1725 else
1726 wxRichTextApplyStyle(child->GetAttributes(), characterAttributes);
1727
1728 if (node2 == lastNode)
1729 break;
1730
1731 node2 = node2->GetNext();
1732 }
1733 }
1734 }
1735 }
1736
1737 node = node->GetNext();
1738 }
1739
1740 // Do action, or delay it until end of batch.
1741 if (haveControl && withUndo)
1742 GetRichTextCtrl()->GetBuffer().SubmitAction(action);
1743
1744 return true;
1745 }
1746
1747 /// Set text attributes
1748 bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const wxTextAttrEx& style, int flags)
1749 {
1750 wxRichTextAttr richStyle = style;
1751 return SetStyle(range, richStyle, flags);
1752 }
1753
1754 /// Get the text attributes for this position.
1755 bool wxRichTextParagraphLayoutBox::GetStyle(long position, wxTextAttrEx& style)
1756 {
1757 return DoGetStyle(position, style, true);
1758 }
1759
1760 /// Get the text attributes for this position.
1761 bool wxRichTextParagraphLayoutBox::GetStyle(long position, wxRichTextAttr& style)
1762 {
1763 wxTextAttrEx textAttrEx(style);
1764 if (GetStyle(position, textAttrEx))
1765 {
1766 style = textAttrEx;
1767 return true;
1768 }
1769 else
1770 return false;
1771 }
1772
1773 /// Get the content (uncombined) attributes for this position.
1774 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position, wxTextAttrEx& style)
1775 {
1776 return DoGetStyle(position, style, false);
1777 }
1778
1779 bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position, wxRichTextAttr& style)
1780 {
1781 wxTextAttrEx textAttrEx(style);
1782 if (GetUncombinedStyle(position, textAttrEx))
1783 {
1784 style = textAttrEx;
1785 return true;
1786 }
1787 else
1788 return false;
1789 }
1790
1791 /// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
1792 /// context attributes.
1793 bool wxRichTextParagraphLayoutBox::DoGetStyle(long position, wxTextAttrEx& style, bool combineStyles)
1794 {
1795 wxRichTextObject* obj wxDUMMY_INITIALIZE(NULL);
1796
1797 if (style.IsParagraphStyle())
1798 {
1799 obj = GetParagraphAtPosition(position);
1800 if (obj)
1801 {
1802 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1803 if (combineStyles)
1804 {
1805 // Start with the base style
1806 style = GetAttributes();
1807
1808 // Apply the paragraph style
1809 wxRichTextApplyStyle(style, obj->GetAttributes());
1810 }
1811 else
1812 style = obj->GetAttributes();
1813 #else
1814 style = obj->GetAttributes();
1815 #endif
1816 return true;
1817 }
1818 }
1819 else
1820 {
1821 obj = GetLeafObjectAtPosition(position);
1822 if (obj)
1823 {
1824 #if wxRICHTEXT_USE_DYNAMIC_STYLES
1825 if (combineStyles)
1826 {
1827 wxRichTextParagraph* para = wxDynamicCast(obj->GetParent(), wxRichTextParagraph);
1828 style = para ? para->GetCombinedAttributes(obj->GetAttributes()) : obj->GetAttributes();
1829 }
1830 else
1831 style = obj->GetAttributes();
1832 #else
1833 style = obj->GetAttributes();
1834 #endif
1835 return true;
1836 }
1837 }
1838 return false;
1839 }
1840
1841 static bool wxHasStyle(long flags, long style)
1842 {
1843 return (flags & style) != 0;
1844 }
1845
1846 /// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
1847 /// content.
1848 bool wxRichTextParagraphLayoutBox::CollectStyle(wxTextAttrEx& currentStyle, const wxTextAttrEx& style, long& multipleStyleAttributes)
1849 {
1850 if (style.HasFont())
1851 {
1852 if (style.HasSize() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_SIZE))
1853 {
1854 if (currentStyle.GetFont().Ok() && currentStyle.HasSize())
1855 {
1856 if (currentStyle.GetFont().GetPointSize() != style.GetFont().GetPointSize())
1857 {
1858 // Clash of style - mark as such
1859 multipleStyleAttributes |= wxTEXT_ATTR_FONT_SIZE;
1860 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_FONT_SIZE);
1861 }
1862 }
1863 else
1864 {
1865 if (!currentStyle.GetFont().Ok())
1866 wxSetFontPreservingStyles(currentStyle, *wxNORMAL_FONT);
1867 wxFont font(currentStyle.GetFont());
1868 font.SetPointSize(style.GetFont().GetPointSize());
1869
1870 wxSetFontPreservingStyles(currentStyle, font);
1871 currentStyle.SetFlags(currentStyle.GetFlags() | wxTEXT_ATTR_FONT_SIZE);
1872 }
1873 }
1874
1875 if (style.HasItalic() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_ITALIC))
1876 {
1877 if (currentStyle.GetFont().Ok() && currentStyle.HasItalic())
1878 {
1879 if (currentStyle.GetFont().GetStyle() != style.GetFont().GetStyle())
1880 {
1881 // Clash of style - mark as such
1882 multipleStyleAttributes |= wxTEXT_ATTR_FONT_ITALIC;
1883 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_FONT_ITALIC);
1884 }
1885 }
1886 else
1887 {
1888 if (!currentStyle.GetFont().Ok())
1889 wxSetFontPreservingStyles(currentStyle, *wxNORMAL_FONT);
1890 wxFont font(currentStyle.GetFont());
1891 font.SetStyle(style.GetFont().GetStyle());
1892 wxSetFontPreservingStyles(currentStyle, font);
1893 currentStyle.SetFlags(currentStyle.GetFlags() | wxTEXT_ATTR_FONT_ITALIC);
1894 }
1895 }
1896
1897 if (style.HasWeight() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_WEIGHT))
1898 {
1899 if (currentStyle.GetFont().Ok() && currentStyle.HasWeight())
1900 {
1901 if (currentStyle.GetFont().GetWeight() != style.GetFont().GetWeight())
1902 {
1903 // Clash of style - mark as such
1904 multipleStyleAttributes |= wxTEXT_ATTR_FONT_WEIGHT;
1905 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_FONT_WEIGHT);
1906 }
1907 }
1908 else
1909 {
1910 if (!currentStyle.GetFont().Ok())
1911 wxSetFontPreservingStyles(currentStyle, *wxNORMAL_FONT);
1912 wxFont font(currentStyle.GetFont());
1913 font.SetWeight(style.GetFont().GetWeight());
1914 wxSetFontPreservingStyles(currentStyle, font);
1915 currentStyle.SetFlags(currentStyle.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT);
1916 }
1917 }
1918
1919 if (style.HasFaceName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_FACE))
1920 {
1921 if (currentStyle.GetFont().Ok() && currentStyle.HasFaceName())
1922 {
1923 wxString faceName1(currentStyle.GetFont().GetFaceName());
1924 wxString faceName2(style.GetFont().GetFaceName());
1925
1926 if (faceName1 != faceName2)
1927 {
1928 // Clash of style - mark as such
1929 multipleStyleAttributes |= wxTEXT_ATTR_FONT_FACE;
1930 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_FONT_FACE);
1931 }
1932 }
1933 else
1934 {
1935 if (!currentStyle.GetFont().Ok())
1936 wxSetFontPreservingStyles(currentStyle, *wxNORMAL_FONT);
1937 wxFont font(currentStyle.GetFont());
1938 font.SetFaceName(style.GetFont().GetFaceName());
1939 wxSetFontPreservingStyles(currentStyle, font);
1940 currentStyle.SetFlags(currentStyle.GetFlags() | wxTEXT_ATTR_FONT_FACE);
1941 }
1942 }
1943
1944 if (style.HasUnderlined() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_FONT_UNDERLINE))
1945 {
1946 if (currentStyle.GetFont().Ok() && currentStyle.HasUnderlined())
1947 {
1948 if (currentStyle.GetFont().GetUnderlined() != style.GetFont().GetUnderlined())
1949 {
1950 // Clash of style - mark as such
1951 multipleStyleAttributes |= wxTEXT_ATTR_FONT_UNDERLINE;
1952 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_FONT_UNDERLINE);
1953 }
1954 }
1955 else
1956 {
1957 if (!currentStyle.GetFont().Ok())
1958 wxSetFontPreservingStyles(currentStyle, *wxNORMAL_FONT);
1959 wxFont font(currentStyle.GetFont());
1960 font.SetUnderlined(style.GetFont().GetUnderlined());
1961 wxSetFontPreservingStyles(currentStyle, font);
1962 currentStyle.SetFlags(currentStyle.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE);
1963 }
1964 }
1965 }
1966
1967 if (style.HasTextColour() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_TEXT_COLOUR))
1968 {
1969 if (currentStyle.HasTextColour())
1970 {
1971 if (currentStyle.GetTextColour() != style.GetTextColour())
1972 {
1973 // Clash of style - mark as such
1974 multipleStyleAttributes |= wxTEXT_ATTR_TEXT_COLOUR;
1975 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_TEXT_COLOUR);
1976 }
1977 }
1978 else
1979 currentStyle.SetTextColour(style.GetTextColour());
1980 }
1981
1982 if (style.HasBackgroundColour() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BACKGROUND_COLOUR))
1983 {
1984 if (currentStyle.HasBackgroundColour())
1985 {
1986 if (currentStyle.GetBackgroundColour() != style.GetBackgroundColour())
1987 {
1988 // Clash of style - mark as such
1989 multipleStyleAttributes |= wxTEXT_ATTR_BACKGROUND_COLOUR;
1990 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR);
1991 }
1992 }
1993 else
1994 currentStyle.SetBackgroundColour(style.GetBackgroundColour());
1995 }
1996
1997 if (style.HasAlignment() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_ALIGNMENT))
1998 {
1999 if (currentStyle.HasAlignment())
2000 {
2001 if (currentStyle.GetAlignment() != style.GetAlignment())
2002 {
2003 // Clash of style - mark as such
2004 multipleStyleAttributes |= wxTEXT_ATTR_ALIGNMENT;
2005 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_ALIGNMENT);
2006 }
2007 }
2008 else
2009 currentStyle.SetAlignment(style.GetAlignment());
2010 }
2011
2012 if (style.HasTabs() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_TABS))
2013 {
2014 if (currentStyle.HasTabs())
2015 {
2016 if (!wxRichTextTabsEq(currentStyle.GetTabs(), style.GetTabs()))
2017 {
2018 // Clash of style - mark as such
2019 multipleStyleAttributes |= wxTEXT_ATTR_TABS;
2020 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_TABS);
2021 }
2022 }
2023 else
2024 currentStyle.SetTabs(style.GetTabs());
2025 }
2026
2027 if (style.HasLeftIndent() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_LEFT_INDENT))
2028 {
2029 if (currentStyle.HasLeftIndent())
2030 {
2031 if (currentStyle.GetLeftIndent() != style.GetLeftIndent() || currentStyle.GetLeftSubIndent() != style.GetLeftSubIndent())
2032 {
2033 // Clash of style - mark as such
2034 multipleStyleAttributes |= wxTEXT_ATTR_LEFT_INDENT;
2035 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT);
2036 }
2037 }
2038 else
2039 currentStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
2040 }
2041
2042 if (style.HasRightIndent() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_RIGHT_INDENT))
2043 {
2044 if (currentStyle.HasRightIndent())
2045 {
2046 if (currentStyle.GetRightIndent() != style.GetRightIndent())
2047 {
2048 // Clash of style - mark as such
2049 multipleStyleAttributes |= wxTEXT_ATTR_RIGHT_INDENT;
2050 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_RIGHT_INDENT);
2051 }
2052 }
2053 else
2054 currentStyle.SetRightIndent(style.GetRightIndent());
2055 }
2056
2057 if (style.HasParagraphSpacingAfter() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_PARA_SPACING_AFTER))
2058 {
2059 if (currentStyle.HasParagraphSpacingAfter())
2060 {
2061 if (currentStyle.HasParagraphSpacingAfter() != style.HasParagraphSpacingAfter())
2062 {
2063 // Clash of style - mark as such
2064 multipleStyleAttributes |= wxTEXT_ATTR_PARA_SPACING_AFTER;
2065 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_AFTER);
2066 }
2067 }
2068 else
2069 currentStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
2070 }
2071
2072 if (style.HasParagraphSpacingBefore() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_PARA_SPACING_BEFORE))
2073 {
2074 if (currentStyle.HasParagraphSpacingBefore())
2075 {
2076 if (currentStyle.HasParagraphSpacingBefore() != style.HasParagraphSpacingBefore())
2077 {
2078 // Clash of style - mark as such
2079 multipleStyleAttributes |= wxTEXT_ATTR_PARA_SPACING_BEFORE;
2080 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_PARA_SPACING_BEFORE);
2081 }
2082 }
2083 else
2084 currentStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
2085 }
2086
2087 if (style.HasLineSpacing() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_LINE_SPACING))
2088 {
2089 if (currentStyle.HasLineSpacing())
2090 {
2091 if (currentStyle.HasLineSpacing() != style.HasLineSpacing())
2092 {
2093 // Clash of style - mark as such
2094 multipleStyleAttributes |= wxTEXT_ATTR_LINE_SPACING;
2095 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_LINE_SPACING);
2096 }
2097 }
2098 else
2099 currentStyle.SetLineSpacing(style.GetLineSpacing());
2100 }
2101
2102 if (style.HasCharacterStyleName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_CHARACTER_STYLE_NAME))
2103 {
2104 if (currentStyle.HasCharacterStyleName())
2105 {
2106 if (currentStyle.HasCharacterStyleName() != style.HasCharacterStyleName())
2107 {
2108 // Clash of style - mark as such
2109 multipleStyleAttributes |= wxTEXT_ATTR_CHARACTER_STYLE_NAME;
2110 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_CHARACTER_STYLE_NAME);
2111 }
2112 }
2113 else
2114 currentStyle.SetCharacterStyleName(style.GetCharacterStyleName());
2115 }
2116
2117 if (style.HasParagraphStyleName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME))
2118 {
2119 if (currentStyle.HasParagraphStyleName())
2120 {
2121 if (currentStyle.HasParagraphStyleName() != style.HasParagraphStyleName())
2122 {
2123 // Clash of style - mark as such
2124 multipleStyleAttributes |= wxTEXT_ATTR_PARAGRAPH_STYLE_NAME;
2125 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_PARAGRAPH_STYLE_NAME);
2126 }
2127 }
2128 else
2129 currentStyle.SetParagraphStyleName(style.GetParagraphStyleName());
2130 }
2131
2132 if (style.HasListStyleName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_LIST_STYLE_NAME))
2133 {
2134 if (currentStyle.HasListStyleName())
2135 {
2136 if (currentStyle.HasListStyleName() != style.HasListStyleName())
2137 {
2138 // Clash of style - mark as such
2139 multipleStyleAttributes |= wxTEXT_ATTR_LIST_STYLE_NAME;
2140 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_LIST_STYLE_NAME);
2141 }
2142 }
2143 else
2144 currentStyle.SetListStyleName(style.GetListStyleName());
2145 }
2146
2147 if (style.HasBulletStyle() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BULLET_STYLE))
2148 {
2149 if (currentStyle.HasBulletStyle())
2150 {
2151 if (currentStyle.HasBulletStyle() != style.HasBulletStyle())
2152 {
2153 // Clash of style - mark as such
2154 multipleStyleAttributes |= wxTEXT_ATTR_BULLET_STYLE;
2155 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_BULLET_STYLE);
2156 }
2157 }
2158 else
2159 currentStyle.SetBulletStyle(style.GetBulletStyle());
2160 }
2161
2162 if (style.HasBulletNumber() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BULLET_NUMBER))
2163 {
2164 if (currentStyle.HasBulletNumber())
2165 {
2166 if (currentStyle.HasBulletNumber() != style.HasBulletNumber())
2167 {
2168 // Clash of style - mark as such
2169 multipleStyleAttributes |= wxTEXT_ATTR_BULLET_NUMBER;
2170 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_BULLET_NUMBER);
2171 }
2172 }
2173 else
2174 currentStyle.SetBulletNumber(style.GetBulletNumber());
2175 }
2176
2177 if (style.HasBulletText() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BULLET_TEXT))
2178 {
2179 if (currentStyle.HasBulletText())
2180 {
2181 if (currentStyle.HasBulletText() != style.HasBulletText())
2182 {
2183 // Clash of style - mark as such
2184 multipleStyleAttributes |= wxTEXT_ATTR_BULLET_TEXT;
2185 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_BULLET_TEXT);
2186 }
2187 }
2188 else
2189 {
2190 currentStyle.SetBulletText(style.GetBulletText());
2191 currentStyle.SetBulletFont(style.GetBulletFont());
2192 }
2193 }
2194
2195 if (style.HasBulletName() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_BULLET_NAME))
2196 {
2197 if (currentStyle.HasBulletName())
2198 {
2199 if (currentStyle.HasBulletName() != style.HasBulletName())
2200 {
2201 // Clash of style - mark as such
2202 multipleStyleAttributes |= wxTEXT_ATTR_BULLET_NAME;
2203 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_BULLET_NAME);
2204 }
2205 }
2206 else
2207 {
2208 currentStyle.SetBulletName(style.GetBulletName());
2209 }
2210 }
2211
2212 if (style.HasURL() && !wxHasStyle(multipleStyleAttributes, wxTEXT_ATTR_URL))
2213 {
2214 if (currentStyle.HasURL())
2215 {
2216 if (currentStyle.HasURL() != style.HasURL())
2217 {
2218 // Clash of style - mark as such
2219 multipleStyleAttributes |= wxTEXT_ATTR_URL;
2220 currentStyle.SetFlags(currentStyle.GetFlags() & ~wxTEXT_ATTR_URL);
2221 }
2222 }
2223 else
2224 {
2225 currentStyle.SetURL(style.GetURL());
2226 }
2227 }
2228
2229 return true;
2230 }
2231
2232 /// Get the combined style for a range - if any attribute is different within the range,
2233 /// that attribute is not present within the flags.
2234 /// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
2235 /// nested.
2236 bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range, wxTextAttrEx& style)
2237 {
2238 style = wxTextAttrEx();
2239
2240 // The attributes that aren't valid because of multiple styles within the range
2241 long multipleStyleAttributes = 0;
2242
2243 wxRichTextObjectList::compatibility_iterator node = GetChildren().GetFirst();
2244 while (node)
2245 {
2246 wxRichTextParagraph* para = (wxRichTextParagraph*) node->GetData();
2247 if (!(para->GetRange().GetStart() > range.GetEnd() || para->GetRange().GetEnd() < range.GetStart()))
2248 {
2249 if (para->GetChildren().GetCount() == 0)
2250 {
2251 wxTextAttrEx paraStyle = para->GetCombinedAttributes();
2252
2253 CollectStyle(style, paraStyle, multipleStyleAttributes);
2254 }
2255 else
2256 {
2257 wxRichTextRange paraRange(para->GetRange());
2258 paraRange.LimitTo(range);
2259
2260 // First collect paragraph attributes only
2261 wxTextAttrEx paraStyle = para->GetCombinedAttributes();
2262 paraStyle.SetFlags(paraStyle.GetFlags() & wxTEXT_ATTR_PARAGRAPH);
2263 CollectStyle(style, paraStyle, multipleStyleAttributes);
2264
2265 wxRichTextObjectList::compatibility_iterator childNode = para->GetChildren().GetFirst();
2266
2267 while (childNode)
2268 {
2269 wxRichTextObject* child = childNode->GetData();
2270 if (!(child->GetRange().GetStart() > range.GetEnd() || child->GetRange().GetEnd() < range.GetStart()))
2271 {
2272 wxTextAttrEx childStyle = para->GetCombinedAttributes(child->GetAttributes());
2273
2274 // Now collect character attributes only
2275 childStyle.SetFlags(childStyle.GetFlags() & wxTEXT_ATTR_CHARACTER);
2276
2277 CollectStyle(style, childStyle, multipleStyleAttributes);
2278 }
2279
2280 childNode = childNode->GetNext();
2281 }
2282 }
2283 }
2284 node = node->GetNext();
2285 }
2286 return true;
2287 }
2288
2289 /// Set default style
2290 bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx& style)
2291 {
2292 // I don't think the default style should be combined with the previous
2293 // default style.
2294 m_defaultAttributes = style;
2295
2296 #if 0
2297 // keep the old attributes if the new style doesn't specify them unless the
2298 // new style is empty - then reset m_defaultStyle (as there is no other way
2299 // to do it)
2300 if ( style.IsDefault() )
2301 m_defaultAttributes = style;
2302 else
2303 m_defaultAttributes = wxTextAttrEx::CombineEx(style, m_defaultAttributes, NULL);
2304 #endif
2305 return true;
2306 }
2307
2308 /// Test if this whole range has character attributes of the specified kind. If any
2309 /// of the attributes are different within the range, the test fails. You
2310 /// can use this to implement, for example, bold button updating. style must have
2311 /// flags indicating which attributes are of interest.
2312 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const
2313 {
2314 int foundCount = 0;
2315 int matchingCount = 0;
2316
2317 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2318 while (node)
2319 {
2320 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
2321 wxASSERT (para != NULL);
2322
2323 if (para)
2324 {
2325 // Stop searching if we're beyond the range of interest
2326 if (para->GetRange().GetStart() > range.GetEnd())
2327 return foundCount == matchingCount;
2328
2329 if (!para->GetRange().IsOutside(range))
2330 {
2331 wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst();
2332
2333 while (node2)
2334 {
2335 wxRichTextObject* child = node2->GetData();
2336 if (!child->GetRange().IsOutside(range) && child->IsKindOf(CLASSINFO(wxRichTextPlainText)))
2337 {
2338 foundCount ++;
2339 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2340 wxTextAttrEx textAttr = para->GetCombinedAttributes(child->GetAttributes());
2341 #else
2342 const wxTextAttrEx& textAttr = child->GetAttributes();
2343 #endif
2344 if (wxTextAttrEqPartial(textAttr, style, style.GetFlags()))
2345 matchingCount ++;
2346 }
2347
2348 node2 = node2->GetNext();
2349 }
2350 }
2351 }
2352
2353 node = node->GetNext();
2354 }
2355
2356 return foundCount == matchingCount;
2357 }
2358
2359 bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const
2360 {
2361 wxRichTextAttr richStyle = style;
2362 return HasCharacterAttributes(range, richStyle);
2363 }
2364
2365 /// Test if this whole range has paragraph attributes of the specified kind. If any
2366 /// of the attributes are different within the range, the test fails. You
2367 /// can use this to implement, for example, centering button updating. style must have
2368 /// flags indicating which attributes are of interest.
2369 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const
2370 {
2371 int foundCount = 0;
2372 int matchingCount = 0;
2373
2374 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2375 while (node)
2376 {
2377 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
2378 wxASSERT (para != NULL);
2379
2380 if (para)
2381 {
2382 // Stop searching if we're beyond the range of interest
2383 if (para->GetRange().GetStart() > range.GetEnd())
2384 return foundCount == matchingCount;
2385
2386 if (!para->GetRange().IsOutside(range))
2387 {
2388 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2389 wxTextAttrEx textAttr = GetAttributes();
2390 // Apply the paragraph style
2391 wxRichTextApplyStyle(textAttr, para->GetAttributes());
2392
2393 #else
2394 const wxTextAttrEx& textAttr = para->GetAttributes();
2395 #endif
2396 foundCount ++;
2397 if (wxTextAttrEqPartial(textAttr, style, style.GetFlags()))
2398 matchingCount ++;
2399 }
2400 }
2401
2402 node = node->GetNext();
2403 }
2404 return foundCount == matchingCount;
2405 }
2406
2407 bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const
2408 {
2409 wxRichTextAttr richStyle = style;
2410 return HasParagraphAttributes(range, richStyle);
2411 }
2412
2413 void wxRichTextParagraphLayoutBox::Clear()
2414 {
2415 DeleteChildren();
2416 }
2417
2418 void wxRichTextParagraphLayoutBox::Reset()
2419 {
2420 Clear();
2421
2422 AddParagraph(wxEmptyString);
2423 }
2424
2425 /// Invalidate the buffer. With no argument, invalidates whole buffer.
2426 void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange& invalidRange)
2427 {
2428 SetDirty(true);
2429
2430 if (invalidRange == wxRICHTEXT_ALL)
2431 {
2432 m_invalidRange = wxRICHTEXT_ALL;
2433 return;
2434 }
2435
2436 // Already invalidating everything
2437 if (m_invalidRange == wxRICHTEXT_ALL)
2438 return;
2439
2440 if ((invalidRange.GetStart() < m_invalidRange.GetStart()) || m_invalidRange.GetStart() == -1)
2441 m_invalidRange.SetStart(invalidRange.GetStart());
2442 if (invalidRange.GetEnd() > m_invalidRange.GetEnd())
2443 m_invalidRange.SetEnd(invalidRange.GetEnd());
2444 }
2445
2446 /// Get invalid range, rounding to entire paragraphs if argument is true.
2447 wxRichTextRange wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs) const
2448 {
2449 if (m_invalidRange == wxRICHTEXT_ALL || m_invalidRange == wxRICHTEXT_NONE)
2450 return m_invalidRange;
2451
2452 wxRichTextRange range = m_invalidRange;
2453
2454 if (wholeParagraphs)
2455 {
2456 wxRichTextParagraph* para1 = GetParagraphAtPosition(range.GetStart());
2457 wxRichTextParagraph* para2 = GetParagraphAtPosition(range.GetEnd());
2458 if (para1)
2459 range.SetStart(para1->GetRange().GetStart());
2460 if (para2)
2461 range.SetEnd(para2->GetRange().GetEnd());
2462 }
2463 return range;
2464 }
2465
2466 /// Apply the style sheet to the buffer, for example if the styles have changed.
2467 bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
2468 {
2469 wxASSERT(styleSheet != NULL);
2470 if (!styleSheet)
2471 return false;
2472
2473 int foundCount = 0;
2474
2475 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2476 while (node)
2477 {
2478 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
2479 wxASSERT (para != NULL);
2480
2481 if (para)
2482 {
2483 // Combine paragraph and list styles. If there is a list style in the original attributes,
2484 // the current indentation overrides anything else and is used to find the item indentation.
2485 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
2486 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
2487 // exception as above).
2488 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
2489 // So when changing a list style interactively, could retrieve level based on current style, then
2490 // set appropriate indent and apply new style.
2491
2492 if (!para->GetAttributes().GetParagraphStyleName().IsEmpty() && !para->GetAttributes().GetListStyleName().IsEmpty())
2493 {
2494 int currentIndent = para->GetAttributes().GetLeftIndent();
2495
2496 wxRichTextParagraphStyleDefinition* paraDef = styleSheet->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
2497 wxRichTextListStyleDefinition* listDef = styleSheet->FindListStyle(para->GetAttributes().GetListStyleName());
2498 if (paraDef && !listDef)
2499 {
2500 para->GetAttributes() = paraDef->GetStyle();
2501 foundCount ++;
2502 }
2503 else if (listDef && !paraDef)
2504 {
2505 // Set overall style defined for the list style definition
2506 para->GetAttributes() = listDef->GetStyle();
2507
2508 // Apply the style for this level
2509 wxRichTextApplyStyle(para->GetAttributes(), * listDef->GetLevelAttributes(listDef->FindLevelForIndent(currentIndent)));
2510 foundCount ++;
2511 }
2512 else if (listDef && paraDef)
2513 {
2514 // Combines overall list style, style for level, and paragraph style
2515 para->GetAttributes() = listDef->CombineWithParagraphStyle(currentIndent, paraDef->GetStyle());
2516 foundCount ++;
2517 }
2518 }
2519 else if (para->GetAttributes().GetParagraphStyleName().IsEmpty() && !para->GetAttributes().GetListStyleName().IsEmpty())
2520 {
2521 int currentIndent = para->GetAttributes().GetLeftIndent();
2522
2523 wxRichTextListStyleDefinition* listDef = styleSheet->FindListStyle(para->GetAttributes().GetListStyleName());
2524
2525 // Overall list definition style
2526 para->GetAttributes() = listDef->GetStyle();
2527
2528 // Style for this level
2529 wxRichTextApplyStyle(para->GetAttributes(), * listDef->GetLevelAttributes(listDef->FindLevelForIndent(currentIndent)));
2530
2531 foundCount ++;
2532 }
2533 else if (!para->GetAttributes().GetParagraphStyleName().IsEmpty() && para->GetAttributes().GetListStyleName().IsEmpty())
2534 {
2535 wxRichTextParagraphStyleDefinition* def = styleSheet->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
2536 if (def)
2537 {
2538 para->GetAttributes() = def->GetStyle();
2539 foundCount ++;
2540 }
2541 }
2542 }
2543
2544 node = node->GetNext();
2545 }
2546 return foundCount != 0;
2547 }
2548
2549 /// Set list style
2550 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
2551 {
2552 bool withUndo = ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0);
2553 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2554 bool specifyLevel = ((flags & wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL) != 0);
2555 bool renumber = ((flags & wxRICHTEXT_SETSTYLE_RENUMBER) != 0);
2556
2557 // Current number, if numbering
2558 int n = startFrom;
2559
2560 wxASSERT (!specifyLevel || (specifyLevel && (specifiedLevel >= 0)));
2561
2562 // If we are associated with a control, make undoable; otherwise, apply immediately
2563 // to the data.
2564
2565 bool haveControl = (GetRichTextCtrl() != NULL);
2566
2567 wxRichTextAction* action = NULL;
2568
2569 if (haveControl && withUndo)
2570 {
2571 action = new wxRichTextAction(NULL, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2572 action->SetRange(range);
2573 action->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2574 }
2575
2576 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2577 while (node)
2578 {
2579 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
2580 wxASSERT (para != NULL);
2581
2582 if (para && para->GetChildCount() > 0)
2583 {
2584 // Stop searching if we're beyond the range of interest
2585 if (para->GetRange().GetStart() > range.GetEnd())
2586 break;
2587
2588 if (!para->GetRange().IsOutside(range))
2589 {
2590 // We'll be using a copy of the paragraph to make style changes,
2591 // not updating the buffer directly.
2592 wxRichTextParagraph* newPara wxDUMMY_INITIALIZE(NULL);
2593
2594 if (haveControl && withUndo)
2595 {
2596 newPara = new wxRichTextParagraph(*para);
2597 action->GetNewParagraphs().AppendChild(newPara);
2598
2599 // Also store the old ones for Undo
2600 action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para));
2601 }
2602 else
2603 newPara = para;
2604
2605 if (def)
2606 {
2607 int thisIndent = newPara->GetAttributes().GetLeftIndent();
2608 int thisLevel = specifyLevel ? specifiedLevel : def->FindLevelForIndent(thisIndent);
2609
2610 // How is numbering going to work?
2611 // If we are renumbering, or numbering for the first time, we need to keep
2612 // track of the number for each level. But we might be simply applying a different
2613 // list style.
2614 // In Word, applying a style to several paragraphs, even if at different levels,
2615 // reverts the level back to the same one. So we could do the same here.
2616 // Renumbering will need to be done when we promote/demote a paragraph.
2617
2618 // Apply the overall list style, and item style for this level
2619 wxTextAttrEx listStyle(def->GetCombinedStyleForLevel(thisLevel));
2620 wxRichTextApplyStyle(newPara->GetAttributes(), listStyle);
2621
2622 // Now we need to do numbering
2623 if (renumber)
2624 {
2625 newPara->GetAttributes().SetBulletNumber(n);
2626 }
2627
2628 n ++;
2629 }
2630 else if (!newPara->GetAttributes().GetListStyleName().IsEmpty())
2631 {
2632 // if def is NULL, remove list style, applying any associated paragraph style
2633 // to restore the attributes
2634
2635 newPara->GetAttributes().SetListStyleName(wxEmptyString);
2636 newPara->GetAttributes().SetLeftIndent(0, 0);
2637 newPara->GetAttributes().SetBulletText(wxEmptyString);
2638
2639 // Eliminate the main list-related attributes
2640 newPara->GetAttributes().SetFlags(newPara->GetAttributes().GetFlags() & ~wxTEXT_ATTR_LEFT_INDENT & ~wxTEXT_ATTR_BULLET_STYLE & ~wxTEXT_ATTR_BULLET_NUMBER & ~wxTEXT_ATTR_BULLET_TEXT & wxTEXT_ATTR_LIST_STYLE_NAME);
2641
2642 wxRichTextStyleSheet* styleSheet = GetStyleSheet();
2643 if (styleSheet && !newPara->GetAttributes().GetParagraphStyleName().IsEmpty())
2644 {
2645 wxRichTextParagraphStyleDefinition* def = styleSheet->FindParagraphStyle(newPara->GetAttributes().GetParagraphStyleName());
2646 if (def)
2647 {
2648 newPara->GetAttributes() = def->GetStyle();
2649 }
2650 }
2651 }
2652 }
2653 }
2654
2655 node = node->GetNext();
2656 }
2657
2658 // Do action, or delay it until end of batch.
2659 if (haveControl && withUndo)
2660 GetRichTextCtrl()->GetBuffer().SubmitAction(action);
2661
2662 return true;
2663 }
2664
2665 bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
2666 {
2667 if (GetStyleSheet())
2668 {
2669 wxRichTextListStyleDefinition* def = GetStyleSheet()->FindListStyle(defName);
2670 if (def)
2671 return SetListStyle(range, def, flags, startFrom, specifiedLevel);
2672 }
2673 return false;
2674 }
2675
2676 /// Clear list for given range
2677 bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange& range, int flags)
2678 {
2679 return SetListStyle(range, NULL, flags);
2680 }
2681
2682 /// Number/renumber any list elements in the given range
2683 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
2684 {
2685 return DoNumberList(range, range, 0, def, flags, startFrom, specifiedLevel);
2686 }
2687
2688 /// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
2689 bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange& range, const wxRichTextRange& promotionRange, int promoteBy,
2690 wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
2691 {
2692 bool withUndo = ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0);
2693 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
2694 bool specifyLevel = ((flags & wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL) != 0);
2695
2696 bool renumber = ((flags & wxRICHTEXT_SETSTYLE_RENUMBER) != 0);
2697
2698 // Max number of levels
2699 const int maxLevels = 10;
2700
2701 // The level we're looking at now
2702 int currentLevel = -1;
2703
2704 // The item number for each level
2705 int levels[maxLevels];
2706 int i;
2707
2708 // Reset all numbering
2709 for (i = 0; i < maxLevels; i++)
2710 {
2711 if (startFrom != -1)
2712 levels[i] = startFrom-1;
2713 else if (renumber) // start again
2714 levels[i] = 0;
2715 else
2716 levels[i] = -1; // start from the number we found, if any
2717 }
2718
2719 wxASSERT(!specifyLevel || (specifyLevel && (specifiedLevel >= 0)));
2720
2721 // If we are associated with a control, make undoable; otherwise, apply immediately
2722 // to the data.
2723
2724 bool haveControl = (GetRichTextCtrl() != NULL);
2725
2726 wxRichTextAction* action = NULL;
2727
2728 if (haveControl && withUndo)
2729 {
2730 action = new wxRichTextAction(NULL, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl());
2731 action->SetRange(range);
2732 action->SetPosition(GetRichTextCtrl()->GetCaretPosition());
2733 }
2734
2735 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2736 while (node)
2737 {
2738 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
2739 wxASSERT (para != NULL);
2740
2741 if (para && para->GetChildCount() > 0)
2742 {
2743 // Stop searching if we're beyond the range of interest
2744 if (para->GetRange().GetStart() > range.GetEnd())
2745 break;
2746
2747 if (!para->GetRange().IsOutside(range))
2748 {
2749 // We'll be using a copy of the paragraph to make style changes,
2750 // not updating the buffer directly.
2751 wxRichTextParagraph* newPara wxDUMMY_INITIALIZE(NULL);
2752
2753 if (haveControl && withUndo)
2754 {
2755 newPara = new wxRichTextParagraph(*para);
2756 action->GetNewParagraphs().AppendChild(newPara);
2757
2758 // Also store the old ones for Undo
2759 action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para));
2760 }
2761 else
2762 newPara = para;
2763
2764 wxRichTextListStyleDefinition* defToUse = def;
2765 if (!defToUse)
2766 {
2767 wxRichTextStyleSheet* sheet = GetStyleSheet();
2768
2769 if (sheet && !newPara->GetAttributes().GetListStyleName().IsEmpty())
2770 defToUse = sheet->FindListStyle(newPara->GetAttributes().GetListStyleName());
2771 }
2772
2773 if (defToUse)
2774 {
2775 int thisIndent = newPara->GetAttributes().GetLeftIndent();
2776 int thisLevel = defToUse->FindLevelForIndent(thisIndent);
2777
2778 // If we've specified a level to apply to all, change the level.
2779 if (specifiedLevel != -1)
2780 thisLevel = specifiedLevel;
2781
2782 // Do promotion if specified
2783 if ((promoteBy != 0) && !para->GetRange().IsOutside(promotionRange))
2784 {
2785 thisLevel = thisLevel - promoteBy;
2786 if (thisLevel < 0)
2787 thisLevel = 0;
2788 if (thisLevel > 9)
2789 thisLevel = 9;
2790 }
2791
2792 // Apply the overall list style, and item style for this level
2793 wxTextAttrEx listStyle(defToUse->GetCombinedStyleForLevel(thisLevel));
2794 wxRichTextApplyStyle(newPara->GetAttributes(), listStyle);
2795
2796 // OK, we've (re)applied the style, now let's get the numbering right.
2797
2798 if (currentLevel == -1)
2799 currentLevel = thisLevel;
2800
2801 // Same level as before, do nothing except increment level's number afterwards
2802 if (currentLevel == thisLevel)
2803 {
2804 }
2805 // A deeper level: start renumbering all levels after current level
2806 else if (thisLevel > currentLevel)
2807 {
2808 for (i = currentLevel+1; i <= thisLevel; i++)
2809 {
2810 levels[i] = 0;
2811 }
2812 currentLevel = thisLevel;
2813 }
2814 else if (thisLevel < currentLevel)
2815 {
2816 currentLevel = thisLevel;
2817 }
2818
2819 // Use the current numbering if -1 and we have a bullet number already
2820 if (levels[currentLevel] == -1)
2821 {
2822 if (newPara->GetAttributes().HasBulletNumber())
2823 levels[currentLevel] = newPara->GetAttributes().GetBulletNumber();
2824 else
2825 levels[currentLevel] = 1;
2826 }
2827 else
2828 {
2829 levels[currentLevel] ++;
2830 }
2831
2832 newPara->GetAttributes().SetBulletNumber(levels[currentLevel]);
2833
2834 // Create the bullet text if an outline list
2835 if (listStyle.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE)
2836 {
2837 wxString text;
2838 for (i = 0; i <= currentLevel; i++)
2839 {
2840 if (!text.IsEmpty())
2841 text += wxT(".");
2842 text += wxString::Format(wxT("%d"), levels[i]);
2843 }
2844 newPara->GetAttributes().SetBulletText(text);
2845 }
2846 }
2847 }
2848 }
2849
2850 node = node->GetNext();
2851 }
2852
2853 // Do action, or delay it until end of batch.
2854 if (haveControl && withUndo)
2855 GetRichTextCtrl()->GetBuffer().SubmitAction(action);
2856
2857 return true;
2858 }
2859
2860 bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
2861 {
2862 if (GetStyleSheet())
2863 {
2864 wxRichTextListStyleDefinition* def = NULL;
2865 if (!defName.IsEmpty())
2866 def = GetStyleSheet()->FindListStyle(defName);
2867 return NumberList(range, def, flags, startFrom, specifiedLevel);
2868 }
2869 return false;
2870 }
2871
2872 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
2873 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel)
2874 {
2875 // TODO
2876 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
2877 // to NumberList with a flag indicating promotion is required within one of the ranges.
2878 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
2879 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
2880 // We start renumbering from the para after that different para we found. We specify that the numbering of that
2881 // list position will start from 1.
2882 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
2883 // We can end the renumbering at this point.
2884
2885 // For now, only renumber within the promotion range.
2886
2887 return DoNumberList(range, range, promoteBy, def, flags, 1, specifiedLevel);
2888 }
2889
2890 bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel)
2891 {
2892 if (GetStyleSheet())
2893 {
2894 wxRichTextListStyleDefinition* def = NULL;
2895 if (!defName.IsEmpty())
2896 def = GetStyleSheet()->FindListStyle(defName);
2897 return PromoteList(promoteBy, range, def, flags, specifiedLevel);
2898 }
2899 return false;
2900 }
2901
2902 /// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
2903 /// position of the paragraph that it had to start looking from.
2904 bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph* previousParagraph, wxRichTextAttr& attr) const
2905 {
2906 #if 0
2907 wxRichTextObjectList::compatibility_iterator node = m_children.Find(previousParagraph);
2908
2909 if (!node)
2910 return false;
2911 #endif
2912
2913 if (!previousParagraph->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE) || previousParagraph->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE)
2914 return false;
2915
2916 wxRichTextStyleSheet* sheet = GetStyleSheet();
2917 if (sheet && !previousParagraph->GetAttributes().GetListStyleName().IsEmpty())
2918 {
2919 wxRichTextListStyleDefinition* def = sheet->FindListStyle(previousParagraph->GetAttributes().GetListStyleName());
2920 if (def)
2921 {
2922 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
2923 // int thisLevel = def->FindLevelForIndent(thisIndent);
2924
2925 bool isOutline = (previousParagraph->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE) != 0;
2926
2927 attr.SetFlags(previousParagraph->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER|wxTEXT_ATTR_BULLET_TEXT|wxTEXT_ATTR_BULLET_NAME));
2928 if (previousParagraph->GetAttributes().HasBulletName())
2929 attr.SetBulletName(previousParagraph->GetAttributes().GetBulletName());
2930 attr.SetBulletStyle(previousParagraph->GetAttributes().GetBulletStyle());
2931 attr.SetListStyleName(previousParagraph->GetAttributes().GetListStyleName());
2932
2933 int nextNumber = previousParagraph->GetAttributes().GetBulletNumber() + 1;
2934 attr.SetBulletNumber(nextNumber);
2935
2936 if (isOutline)
2937 {
2938 wxString text = previousParagraph->GetAttributes().GetBulletText();
2939 if (!text.IsEmpty())
2940 {
2941 int pos = text.Find(wxT('.'), true);
2942 if (pos != wxNOT_FOUND)
2943 {
2944 text = text.Mid(0, text.Length() - pos - 1);
2945 }
2946 else
2947 text = wxEmptyString;
2948 if (!text.IsEmpty())
2949 text += wxT(".");
2950 text += wxString::Format(wxT("%d"), nextNumber);
2951 attr.SetBulletText(text);
2952 }
2953 }
2954
2955 return true;
2956 }
2957 else
2958 return false;
2959 }
2960 else
2961 return false;
2962 }
2963
2964 /*!
2965 * wxRichTextParagraph
2966 * This object represents a single paragraph (or in a straight text editor, a line).
2967 */
2968
2969 IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph, wxRichTextBox)
2970
2971 wxArrayInt wxRichTextParagraph::sm_defaultTabs;
2972
2973 wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject* parent, wxTextAttrEx* style):
2974 wxRichTextBox(parent)
2975 {
2976 if (parent && !style)
2977 SetAttributes(parent->GetAttributes());
2978 if (style)
2979 SetAttributes(*style);
2980 }
2981
2982 wxRichTextParagraph::wxRichTextParagraph(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* style):
2983 wxRichTextBox(parent)
2984 {
2985 if (parent && !style)
2986 SetAttributes(parent->GetAttributes());
2987 if (style)
2988 SetAttributes(*style);
2989
2990 AppendChild(new wxRichTextPlainText(text, this));
2991 }
2992
2993 wxRichTextParagraph::~wxRichTextParagraph()
2994 {
2995 ClearLines();
2996 }
2997
2998 /// Draw the item
2999 bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& WXUNUSED(range), const wxRichTextRange& selectionRange, const wxRect& WXUNUSED(rect), int WXUNUSED(descent), int style)
3000 {
3001 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3002 wxTextAttrEx attr = GetCombinedAttributes();
3003 #else
3004 const wxTextAttrEx& attr = GetAttributes();
3005 #endif
3006
3007 // Draw the bullet, if any
3008 if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
3009 {
3010 if (attr.GetLeftSubIndent() != 0)
3011 {
3012 int spaceBeforePara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingBefore());
3013 int leftIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftIndent());
3014
3015 wxTextAttrEx bulletAttr(GetCombinedAttributes());
3016
3017 // Get line height from first line, if any
3018 wxRichTextLine* line = m_cachedLines.GetFirst() ? (wxRichTextLine* ) m_cachedLines.GetFirst()->GetData() : (wxRichTextLine*) NULL;
3019
3020 wxPoint linePos;
3021 int lineHeight wxDUMMY_INITIALIZE(0);
3022 if (line)
3023 {
3024 lineHeight = line->GetSize().y;
3025 linePos = line->GetPosition() + GetPosition();
3026 }
3027 else
3028 {
3029 wxFont font;
3030 if (bulletAttr.GetFont().Ok())
3031 font = bulletAttr.GetFont();
3032 else
3033 font = (*wxNORMAL_FONT);
3034
3035 dc.SetFont(font);
3036
3037 lineHeight = dc.GetCharHeight();
3038 linePos = GetPosition();
3039 linePos.y += spaceBeforePara;
3040 }
3041
3042 wxRect bulletRect(GetPosition().x + leftIndent, linePos.y, linePos.x - (GetPosition().x + leftIndent), lineHeight);
3043
3044 if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP)
3045 {
3046 if (wxRichTextBuffer::GetRenderer())
3047 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc, bulletAttr, bulletRect);
3048 }
3049 else if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD)
3050 {
3051 if (wxRichTextBuffer::GetRenderer())
3052 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc, bulletAttr, bulletRect);
3053 }
3054 else
3055 {
3056 wxString bulletText = GetBulletText();
3057
3058 if (!bulletText.empty() && wxRichTextBuffer::GetRenderer())
3059 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc, bulletAttr, bulletRect, bulletText);
3060 }
3061 }
3062 }
3063
3064 // Draw the range for each line, one object at a time.
3065
3066 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
3067 while (node)
3068 {
3069 wxRichTextLine* line = node->GetData();
3070 wxRichTextRange lineRange = line->GetAbsoluteRange();
3071
3072 int maxDescent = line->GetDescent();
3073
3074 // Lines are specified relative to the paragraph
3075
3076 wxPoint linePosition = line->GetPosition() + GetPosition();
3077 wxPoint objectPosition = linePosition;
3078
3079 // Loop through objects until we get to the one within range
3080 wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst();
3081 while (node2)
3082 {
3083 wxRichTextObject* child = node2->GetData();
3084 if (!child->GetRange().IsOutside(lineRange))
3085 {
3086 // Draw this part of the line at the correct position
3087 wxRichTextRange objectRange(child->GetRange());
3088 objectRange.LimitTo(lineRange);
3089
3090 wxSize objectSize;
3091 int descent = 0;
3092 child->GetRangeSize(objectRange, objectSize, descent, dc, wxRICHTEXT_UNFORMATTED, objectPosition);
3093
3094 // Use the child object's width, but the whole line's height
3095 wxRect childRect(objectPosition, wxSize(objectSize.x, line->GetSize().y));
3096 child->Draw(dc, objectRange, selectionRange, childRect, maxDescent, style);
3097
3098 objectPosition.x += objectSize.x;
3099 }
3100 else if (child->GetRange().GetStart() > lineRange.GetEnd())
3101 // Can break out of inner loop now since we've passed this line's range
3102 break;
3103
3104 node2 = node2->GetNext();
3105 }
3106
3107 node = node->GetNext();
3108 }
3109
3110 return true;
3111 }
3112
3113 /// Lay the item out
3114 bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style)
3115 {
3116 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3117 wxTextAttrEx attr = GetCombinedAttributes();
3118 #else
3119 const wxTextAttrEx& attr = GetAttributes();
3120 #endif
3121
3122 // ClearLines();
3123
3124 // Increase the size of the paragraph due to spacing
3125 int spaceBeforePara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingBefore());
3126 int spaceAfterPara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingAfter());
3127 int leftIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftIndent());
3128 int leftSubIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftSubIndent());
3129 int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent());
3130
3131 int lineSpacing = 0;
3132
3133 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3134 if (attr.GetLineSpacing() > 10 && attr.GetFont().Ok())
3135 {
3136 dc.SetFont(attr.GetFont());
3137 lineSpacing = (ConvertTenthsMMToPixels(dc, dc.GetCharHeight()) * attr.GetLineSpacing())/10;
3138 }
3139
3140 // Available space for text on each line differs.
3141 int availableTextSpaceFirstLine = rect.GetWidth() - leftIndent - rightIndent;
3142
3143 // Bullets start the text at the same position as subsequent lines
3144 if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
3145 availableTextSpaceFirstLine -= leftSubIndent;
3146
3147 int availableTextSpaceSubsequentLines = rect.GetWidth() - leftIndent - rightIndent - leftSubIndent;
3148
3149 // Start position for each line relative to the paragraph
3150 int startPositionFirstLine = leftIndent;
3151 int startPositionSubsequentLines = leftIndent + leftSubIndent;
3152
3153 // If we have a bullet in this paragraph, the start position for the first line's text
3154 // is actually leftIndent + leftSubIndent.
3155 if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
3156 startPositionFirstLine = startPositionSubsequentLines;
3157
3158 long lastEndPos = GetRange().GetStart()-1;
3159 long lastCompletedEndPos = lastEndPos;
3160
3161 int currentWidth = 0;
3162 SetPosition(rect.GetPosition());
3163
3164 wxPoint currentPosition(0, spaceBeforePara); // We will calculate lines relative to paragraph
3165 int lineHeight = 0;
3166 int maxWidth = 0;
3167 int maxDescent = 0;
3168
3169 int lineCount = 0;
3170
3171 // Split up lines
3172
3173 // We may need to go back to a previous child, in which case create the new line,
3174 // find the child corresponding to the start position of the string, and
3175 // continue.
3176
3177 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3178 while (node)
3179 {
3180 wxRichTextObject* child = node->GetData();
3181
3182 // If this is e.g. a composite text box, it will need to be laid out itself.
3183 // But if just a text fragment or image, for example, this will
3184 // do nothing. NB: won't we need to set the position after layout?
3185 // since for example if position is dependent on vertical line size, we
3186 // can't tell the position until the size is determined. So possibly introduce
3187 // another layout phase.
3188
3189 child->Layout(dc, rect, style);
3190
3191 // Available width depends on whether we're on the first or subsequent lines
3192 int availableSpaceForText = (lineCount == 0 ? availableTextSpaceFirstLine : availableTextSpaceSubsequentLines);
3193
3194 currentPosition.x = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines);
3195
3196 // We may only be looking at part of a child, if we searched back for wrapping
3197 // and found a suitable point some way into the child. So get the size for the fragment
3198 // if necessary.
3199
3200 wxSize childSize;
3201 int childDescent = 0;
3202 if (lastEndPos == child->GetRange().GetStart() - 1)
3203 {
3204 childSize = child->GetCachedSize();
3205 childDescent = child->GetDescent();
3206 }
3207 else
3208 GetRangeSize(wxRichTextRange(lastEndPos+1, child->GetRange().GetEnd()), childSize, childDescent, dc, wxRICHTEXT_UNFORMATTED,rect.GetPosition());
3209
3210 if (childSize.x + currentWidth > availableSpaceForText)
3211 {
3212 long wrapPosition = 0;
3213
3214 // Find a place to wrap. This may walk back to previous children,
3215 // for example if a word spans several objects.
3216 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos+1, child->GetRange().GetEnd()), dc, availableSpaceForText, wrapPosition))
3217 {
3218 // If the function failed, just cut it off at the end of this child.
3219 wrapPosition = child->GetRange().GetEnd();
3220 }
3221
3222 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3223 if (wrapPosition <= lastCompletedEndPos)
3224 wrapPosition = wxMax(lastCompletedEndPos+1,child->GetRange().GetEnd());
3225
3226 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3227
3228 // Let's find the actual size of the current line now
3229 wxSize actualSize;
3230 wxRichTextRange actualRange(lastCompletedEndPos+1, wrapPosition);
3231 GetRangeSize(actualRange, actualSize, childDescent, dc, wxRICHTEXT_UNFORMATTED);
3232 currentWidth = actualSize.x;
3233 lineHeight = wxMax(lineHeight, actualSize.y);
3234 maxDescent = wxMax(childDescent, maxDescent);
3235
3236 // Add a new line
3237 wxRichTextLine* line = AllocateLine(lineCount);
3238
3239 // Set relative range so we won't have to change line ranges when paragraphs are moved
3240 line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart()));
3241 line->SetPosition(currentPosition);
3242 line->SetSize(wxSize(currentWidth, lineHeight));
3243 line->SetDescent(maxDescent);
3244
3245 // Now move down a line. TODO: add margins, spacing
3246 currentPosition.y += lineHeight;
3247 currentPosition.y += lineSpacing;
3248 currentWidth = 0;
3249 maxDescent = 0;
3250 maxWidth = wxMax(maxWidth, currentWidth);
3251
3252 lineCount ++;
3253
3254 // TODO: account for zero-length objects, such as fields
3255 wxASSERT(wrapPosition > lastCompletedEndPos);
3256
3257 lastEndPos = wrapPosition;
3258 lastCompletedEndPos = lastEndPos;
3259
3260 lineHeight = 0;
3261
3262 // May need to set the node back to a previous one, due to searching back in wrapping
3263 wxRichTextObject* childAfterWrapPosition = FindObjectAtPosition(wrapPosition+1);
3264 if (childAfterWrapPosition)
3265 node = m_children.Find(childAfterWrapPosition);
3266 else
3267 node = node->GetNext();
3268 }
3269 else
3270 {
3271 // We still fit, so don't add a line, and keep going
3272 currentWidth += childSize.x;
3273 lineHeight = wxMax(lineHeight, childSize.y);
3274 maxDescent = wxMax(childDescent, maxDescent);
3275
3276 maxWidth = wxMax(maxWidth, currentWidth);
3277 lastEndPos = child->GetRange().GetEnd();
3278
3279 node = node->GetNext();
3280 }
3281 }
3282
3283 // Add the last line - it's the current pos -> last para pos
3284 // Substract -1 because the last position is always the end-paragraph position.
3285 if (lastCompletedEndPos <= GetRange().GetEnd()-1)
3286 {
3287 currentPosition.x = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines);
3288
3289 wxRichTextLine* line = AllocateLine(lineCount);
3290
3291 wxRichTextRange actualRange(lastCompletedEndPos+1, GetRange().GetEnd()-1);
3292
3293 // Set relative range so we won't have to change line ranges when paragraphs are moved
3294 line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart()));
3295
3296 line->SetPosition(currentPosition);
3297
3298 if (lineHeight == 0)
3299 {
3300 if (attr.GetFont().Ok())
3301 dc.SetFont(attr.GetFont());
3302 lineHeight = dc.GetCharHeight();
3303 }
3304 if (maxDescent == 0)
3305 {
3306 int w, h;
3307 dc.GetTextExtent(wxT("X"), & w, &h, & maxDescent);
3308 }
3309
3310 line->SetSize(wxSize(currentWidth, lineHeight));
3311 line->SetDescent(maxDescent);
3312 currentPosition.y += lineHeight;
3313 currentPosition.y += lineSpacing;
3314 lineCount ++;
3315 }
3316
3317 // Remove remaining unused line objects, if any
3318 ClearUnusedLines(lineCount);
3319
3320 // Apply styles to wrapped lines
3321 ApplyParagraphStyle(attr, rect);
3322
3323 SetCachedSize(wxSize(maxWidth, currentPosition.y + spaceBeforePara + spaceAfterPara));
3324
3325 m_dirty = false;
3326
3327 return true;
3328 }
3329
3330 /// Apply paragraph styles, such as centering, to wrapped lines
3331 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx& attr, const wxRect& rect)
3332 {
3333 if (!attr.HasAlignment())
3334 return;
3335
3336 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
3337 while (node)
3338 {
3339 wxRichTextLine* line = node->GetData();
3340
3341 wxPoint pos = line->GetPosition();
3342 wxSize size = line->GetSize();
3343
3344 // centering, right-justification
3345 if (attr.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
3346 {
3347 pos.x = (rect.GetWidth() - size.x)/2 + pos.x;
3348 line->SetPosition(pos);
3349 }
3350 else if (attr.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
3351 {
3352 pos.x = pos.x + rect.GetWidth() - size.x;
3353 line->SetPosition(pos);
3354 }
3355
3356 node = node->GetNext();
3357 }
3358 }
3359
3360 /// Insert text at the given position
3361 bool wxRichTextParagraph::InsertText(long pos, const wxString& text)
3362 {
3363 wxRichTextObject* childToUse = NULL;
3364 wxRichTextObjectList::compatibility_iterator nodeToUse = wxRichTextObjectList::compatibility_iterator();
3365
3366 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3367 while (node)
3368 {
3369 wxRichTextObject* child = node->GetData();
3370 if (child->GetRange().Contains(pos) && child->GetRange().GetLength() > 0)
3371 {
3372 childToUse = child;
3373 nodeToUse = node;
3374 break;
3375 }
3376
3377 node = node->GetNext();
3378 }
3379
3380 if (childToUse)
3381 {
3382 wxRichTextPlainText* textObject = wxDynamicCast(childToUse, wxRichTextPlainText);
3383 if (textObject)
3384 {
3385 int posInString = pos - textObject->GetRange().GetStart();
3386
3387 wxString newText = textObject->GetText().Mid(0, posInString) +
3388 text + textObject->GetText().Mid(posInString);
3389 textObject->SetText(newText);
3390
3391 int textLength = text.length();
3392
3393 textObject->SetRange(wxRichTextRange(textObject->GetRange().GetStart(),
3394 textObject->GetRange().GetEnd() + textLength));
3395
3396 // Increment the end range of subsequent fragments in this paragraph.
3397 // We'll set the paragraph range itself at a higher level.
3398
3399 wxRichTextObjectList::compatibility_iterator node = nodeToUse->GetNext();
3400 while (node)
3401 {
3402 wxRichTextObject* child = node->GetData();
3403 child->SetRange(wxRichTextRange(textObject->GetRange().GetStart() + textLength,
3404 textObject->GetRange().GetEnd() + textLength));
3405
3406 node = node->GetNext();
3407 }
3408
3409 return true;
3410 }
3411 else
3412 {
3413 // TODO: if not a text object, insert at closest position, e.g. in front of it
3414 }
3415 }
3416 else
3417 {
3418 // Add at end.
3419 // Don't pass parent initially to suppress auto-setting of parent range.
3420 // We'll do that at a higher level.
3421 wxRichTextPlainText* textObject = new wxRichTextPlainText(text, this);
3422
3423 AppendChild(textObject);
3424 return true;
3425 }
3426
3427 return false;
3428 }
3429
3430 void wxRichTextParagraph::Copy(const wxRichTextParagraph& obj)
3431 {
3432 wxRichTextBox::Copy(obj);
3433 }
3434
3435 /// Clear the cached lines
3436 void wxRichTextParagraph::ClearLines()
3437 {
3438 WX_CLEAR_LIST(wxRichTextLineList, m_cachedLines);
3439 }
3440
3441 /// Get/set the object size for the given range. Returns false if the range
3442 /// is invalid for this object.
3443 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const
3444 {
3445 if (!range.IsWithin(GetRange()))
3446 return false;
3447
3448 if (flags & wxRICHTEXT_UNFORMATTED)
3449 {
3450 // Just use unformatted data, assume no line breaks
3451 // TODO: take into account line breaks
3452
3453 wxSize sz;
3454
3455 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3456 while (node)
3457 {
3458 wxRichTextObject* child = node->GetData();
3459 if (!child->GetRange().IsOutside(range))
3460 {
3461 wxSize childSize;
3462
3463 wxRichTextRange rangeToUse = range;
3464 rangeToUse.LimitTo(child->GetRange());
3465 int childDescent = 0;
3466
3467 if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, position))
3468 {
3469 sz.y = wxMax(sz.y, childSize.y);
3470 sz.x += childSize.x;
3471 descent = wxMax(descent, childDescent);
3472 }
3473 }
3474
3475 node = node->GetNext();
3476 }
3477 size = sz;
3478 }
3479 else
3480 {
3481 // Use formatted data, with line breaks
3482 wxSize sz;
3483
3484 // We're going to loop through each line, and then for each line,
3485 // call GetRangeSize for the fragment that comprises that line.
3486 // Only we have to do that multiple times within the line, because
3487 // the line may be broken into pieces. For now ignore line break commands
3488 // (so we can assume that getting the unformatted size for a fragment
3489 // within a line is the actual size)
3490
3491 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
3492 while (node)
3493 {
3494 wxRichTextLine* line = node->GetData();
3495 wxRichTextRange lineRange = line->GetAbsoluteRange();
3496 if (!lineRange.IsOutside(range))
3497 {
3498 wxSize lineSize;
3499
3500 wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst();
3501 while (node2)
3502 {
3503 wxRichTextObject* child = node2->GetData();
3504
3505 if (!child->GetRange().IsOutside(lineRange))
3506 {
3507 wxRichTextRange rangeToUse = lineRange;
3508 rangeToUse.LimitTo(child->GetRange());
3509
3510 wxSize childSize;
3511 int childDescent = 0;
3512 if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, position))
3513 {
3514 lineSize.y = wxMax(lineSize.y, childSize.y);
3515 lineSize.x += childSize.x;
3516 }
3517 descent = wxMax(descent, childDescent);
3518 }
3519
3520 node2 = node2->GetNext();
3521 }
3522
3523 // Increase size by a line (TODO: paragraph spacing)
3524 sz.y += lineSize.y;
3525 sz.x = wxMax(sz.x, lineSize.x);
3526 }
3527 node = node->GetNext();
3528 }
3529 size = sz;
3530 }
3531 return true;
3532 }
3533
3534 /// Finds the absolute position and row height for the given character position
3535 bool wxRichTextParagraph::FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart)
3536 {
3537 if (index == -1)
3538 {
3539 wxRichTextLine* line = ((wxRichTextParagraphLayoutBox*)GetParent())->GetLineAtPosition(0);
3540 if (line)
3541 *height = line->GetSize().y;
3542 else
3543 *height = dc.GetCharHeight();
3544
3545 // -1 means 'the start of the buffer'.
3546 pt = GetPosition();
3547 if (line)
3548 pt = pt + line->GetPosition();
3549
3550 return true;
3551 }
3552
3553 // The final position in a paragraph is taken to mean the position
3554 // at the start of the next paragraph.
3555 if (index == GetRange().GetEnd())
3556 {
3557 wxRichTextParagraphLayoutBox* parent = wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox);
3558 wxASSERT( parent != NULL );
3559
3560 // Find the height at the next paragraph, if any
3561 wxRichTextLine* line = parent->GetLineAtPosition(index + 1);
3562 if (line)
3563 {
3564 *height = line->GetSize().y;
3565 pt = line->GetAbsolutePosition();
3566 }
3567 else
3568 {
3569 *height = dc.GetCharHeight();
3570 int indent = ConvertTenthsMMToPixels(dc, m_attributes.GetLeftIndent());
3571 pt = wxPoint(indent, GetCachedSize().y);
3572 }
3573
3574 return true;
3575 }
3576
3577 if (index < GetRange().GetStart() || index > GetRange().GetEnd())
3578 return false;
3579
3580 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
3581 while (node)
3582 {
3583 wxRichTextLine* line = node->GetData();
3584 wxRichTextRange lineRange = line->GetAbsoluteRange();
3585 if (index >= lineRange.GetStart() && index <= lineRange.GetEnd())
3586 {
3587 // If this is the last point in the line, and we're forcing the
3588 // returned value to be the start of the next line, do the required
3589 // thing.
3590 if (index == lineRange.GetEnd() && forceLineStart)
3591 {
3592 if (node->GetNext())
3593 {
3594 wxRichTextLine* nextLine = node->GetNext()->GetData();
3595 *height = nextLine->GetSize().y;
3596 pt = nextLine->GetAbsolutePosition();
3597 return true;
3598 }
3599 }
3600
3601 pt.y = line->GetPosition().y + GetPosition().y;
3602
3603 wxRichTextRange r(lineRange.GetStart(), index);
3604 wxSize rangeSize;
3605 int descent = 0;
3606
3607 // We find the size of the line up to this point,
3608 // then we can add this size to the line start position and
3609 // paragraph start position to find the actual position.
3610
3611 if (GetRangeSize(r, rangeSize, descent, dc, wxRICHTEXT_UNFORMATTED, line->GetPosition()+ GetPosition()))
3612 {
3613 pt.x = line->GetPosition().x + GetPosition().x + rangeSize.x;
3614 *height = line->GetSize().y;
3615
3616 return true;
3617 }
3618
3619 }
3620
3621 node = node->GetNext();
3622 }
3623
3624 return false;
3625 }
3626
3627 /// Hit-testing: returns a flag indicating hit test details, plus
3628 /// information about position
3629 int wxRichTextParagraph::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition)
3630 {
3631 wxPoint paraPos = GetPosition();
3632
3633 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
3634 while (node)
3635 {
3636 wxRichTextLine* line = node->GetData();
3637 wxPoint linePos = paraPos + line->GetPosition();
3638 wxSize lineSize = line->GetSize();
3639 wxRichTextRange lineRange = line->GetAbsoluteRange();
3640
3641 if (pt.y >= linePos.y && pt.y <= linePos.y + lineSize.y)
3642 {
3643 if (pt.x < linePos.x)
3644 {
3645 textPosition = lineRange.GetStart();
3646 return wxRICHTEXT_HITTEST_BEFORE;
3647 }
3648 else if (pt.x >= (linePos.x + lineSize.x))
3649 {
3650 textPosition = lineRange.GetEnd();
3651 return wxRICHTEXT_HITTEST_AFTER;
3652 }
3653 else
3654 {
3655 long i;
3656 int lastX = linePos.x;
3657 for (i = lineRange.GetStart(); i <= lineRange.GetEnd(); i++)
3658 {
3659 wxSize childSize;
3660 int descent = 0;
3661
3662 wxRichTextRange rangeToUse(lineRange.GetStart(), i);
3663
3664 GetRangeSize(rangeToUse, childSize, descent, dc, wxRICHTEXT_UNFORMATTED, linePos);
3665
3666 int nextX = childSize.x + linePos.x;
3667
3668 if (pt.x >= lastX && pt.x <= nextX)
3669 {
3670 textPosition = i;
3671
3672 // So now we know it's between i-1 and i.
3673 // Let's see if we can be more precise about
3674 // which side of the position it's on.
3675
3676 int midPoint = (nextX - lastX)/2 + lastX;
3677 if (pt.x >= midPoint)
3678 return wxRICHTEXT_HITTEST_AFTER;
3679 else
3680 return wxRICHTEXT_HITTEST_BEFORE;
3681 }
3682 else
3683 {
3684 lastX = nextX;
3685 }
3686 }
3687 }
3688 }
3689
3690 node = node->GetNext();
3691 }
3692
3693 return wxRICHTEXT_HITTEST_NONE;
3694 }
3695
3696 /// Split an object at this position if necessary, and return
3697 /// the previous object, or NULL if inserting at beginning.
3698 wxRichTextObject* wxRichTextParagraph::SplitAt(long pos, wxRichTextObject** previousObject)
3699 {
3700 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3701 while (node)
3702 {
3703 wxRichTextObject* child = node->GetData();
3704
3705 if (pos == child->GetRange().GetStart())
3706 {
3707 if (previousObject)
3708 {
3709 if (node->GetPrevious())
3710 *previousObject = node->GetPrevious()->GetData();
3711 else
3712 *previousObject = NULL;
3713 }
3714
3715 return child;
3716 }
3717
3718 if (child->GetRange().Contains(pos))
3719 {
3720 // This should create a new object, transferring part of
3721 // the content to the old object and the rest to the new object.
3722 wxRichTextObject* newObject = child->DoSplit(pos);
3723
3724 // If we couldn't split this object, just insert in front of it.
3725 if (!newObject)
3726 {
3727 // Maybe this is an empty string, try the next one
3728 // return child;
3729 }
3730 else
3731 {
3732 // Insert the new object after 'child'
3733 if (node->GetNext())
3734 m_children.Insert(node->GetNext(), newObject);
3735 else
3736 m_children.Append(newObject);
3737 newObject->SetParent(this);
3738
3739 if (previousObject)
3740 *previousObject = child;
3741
3742 return newObject;
3743 }
3744 }
3745
3746 node = node->GetNext();
3747 }
3748 if (previousObject)
3749 *previousObject = NULL;
3750 return NULL;
3751 }
3752
3753 /// Move content to a list from obj on
3754 void wxRichTextParagraph::MoveToList(wxRichTextObject* obj, wxList& list)
3755 {
3756 wxRichTextObjectList::compatibility_iterator node = m_children.Find(obj);
3757 while (node)
3758 {
3759 wxRichTextObject* child = node->GetData();
3760 list.Append(child);
3761
3762 wxRichTextObjectList::compatibility_iterator oldNode = node;
3763
3764 node = node->GetNext();
3765
3766 m_children.DeleteNode(oldNode);
3767 }
3768 }
3769
3770 /// Add content back from list
3771 void wxRichTextParagraph::MoveFromList(wxList& list)
3772 {
3773 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
3774 {
3775 AppendChild((wxRichTextObject*) node->GetData());
3776 }
3777 }
3778
3779 /// Calculate range
3780 void wxRichTextParagraph::CalculateRange(long start, long& end)
3781 {
3782 wxRichTextCompositeObject::CalculateRange(start, end);
3783
3784 // Add one for end of paragraph
3785 end ++;
3786
3787 m_range.SetRange(start, end);
3788 }
3789
3790 /// Find the object at the given position
3791 wxRichTextObject* wxRichTextParagraph::FindObjectAtPosition(long position)
3792 {
3793 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3794 while (node)
3795 {
3796 wxRichTextObject* obj = node->GetData();
3797 if (obj->GetRange().Contains(position))
3798 return obj;
3799
3800 node = node->GetNext();
3801 }
3802 return NULL;
3803 }
3804
3805 /// Get the plain text searching from the start or end of the range.
3806 /// The resulting string may be shorter than the range given.
3807 bool wxRichTextParagraph::GetContiguousPlainText(wxString& text, const wxRichTextRange& range, bool fromStart)
3808 {
3809 text = wxEmptyString;
3810
3811 if (fromStart)
3812 {
3813 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3814 while (node)
3815 {
3816 wxRichTextObject* obj = node->GetData();
3817 if (!obj->GetRange().IsOutside(range))
3818 {
3819 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
3820 if (textObj)
3821 {
3822 text += textObj->GetTextForRange(range);
3823 }
3824 else
3825 return true;
3826 }
3827
3828 node = node->GetNext();
3829 }
3830 }
3831 else
3832 {
3833 wxRichTextObjectList::compatibility_iterator node = m_children.GetLast();
3834 while (node)
3835 {
3836 wxRichTextObject* obj = node->GetData();
3837 if (!obj->GetRange().IsOutside(range))
3838 {
3839 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
3840 if (textObj)
3841 {
3842 text = textObj->GetTextForRange(range) + text;
3843 }
3844 else
3845 return true;
3846 }
3847
3848 node = node->GetPrevious();
3849 }
3850 }
3851
3852 return true;
3853 }
3854
3855 /// Find a suitable wrap position.
3856 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& dc, int availableSpace, long& wrapPosition)
3857 {
3858 // Find the first position where the line exceeds the available space.
3859 wxSize sz;
3860 long i;
3861 long breakPosition = range.GetEnd();
3862 for (i = range.GetStart(); i <= range.GetEnd(); i++)
3863 {
3864 int descent = 0;
3865 GetRangeSize(wxRichTextRange(range.GetStart(), i), sz, descent, dc, wxRICHTEXT_UNFORMATTED);
3866
3867 if (sz.x > availableSpace)
3868 {
3869 breakPosition = i-1;
3870 break;
3871 }
3872 }
3873
3874 // Now we know the last position on the line.
3875 // Let's try to find a word break.
3876
3877 wxString plainText;
3878 if (GetContiguousPlainText(plainText, wxRichTextRange(range.GetStart(), breakPosition), false))
3879 {
3880 int spacePos = plainText.Find(wxT(' '), true);
3881 if (spacePos != wxNOT_FOUND)
3882 {
3883 int positionsFromEndOfString = plainText.length() - spacePos - 1;
3884 breakPosition = breakPosition - positionsFromEndOfString;
3885 }
3886 }
3887
3888 wrapPosition = breakPosition;
3889
3890 return true;
3891 }
3892
3893 /// Get the bullet text for this paragraph.
3894 wxString wxRichTextParagraph::GetBulletText()
3895 {
3896 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE ||
3897 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP))
3898 return wxEmptyString;
3899
3900 int number = GetAttributes().GetBulletNumber();
3901
3902 wxString text;
3903 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE))
3904 {
3905 text.Printf(wxT("%d"), number);
3906 }
3907 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER)
3908 {
3909 // TODO: Unicode, and also check if number > 26
3910 text.Printf(wxT("%c"), (wxChar) (number+64));
3911 }
3912 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER)
3913 {
3914 // TODO: Unicode, and also check if number > 26
3915 text.Printf(wxT("%c"), (wxChar) (number+96));
3916 }
3917 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER)
3918 {
3919 text = wxRichTextDecimalToRoman(number);
3920 }
3921 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER)
3922 {
3923 text = wxRichTextDecimalToRoman(number);
3924 text.MakeLower();
3925 }
3926 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL)
3927 {
3928 text = GetAttributes().GetBulletText();
3929 }
3930
3931 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE)
3932 {
3933 // The outline style relies on the text being computed statically,
3934 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3935 // should be stored in the attributes; if not, just use the number for this
3936 // level, as previously computed.
3937 if (!GetAttributes().GetBulletText().IsEmpty())
3938 text = GetAttributes().GetBulletText();
3939 }
3940
3941 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES)
3942 {
3943 text = wxT("(") + text + wxT(")");
3944 }
3945 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS)
3946 {
3947 text = text + wxT(")");
3948 }
3949
3950 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD)
3951 {
3952 text += wxT(".");
3953 }
3954
3955 return text;
3956 }
3957
3958 /// Allocate or reuse a line object
3959 wxRichTextLine* wxRichTextParagraph::AllocateLine(int pos)
3960 {
3961 if (pos < (int) m_cachedLines.GetCount())
3962 {
3963 wxRichTextLine* line = m_cachedLines.Item(pos)->GetData();
3964 line->Init(this);
3965 return line;
3966 }
3967 else
3968 {
3969 wxRichTextLine* line = new wxRichTextLine(this);
3970 m_cachedLines.Append(line);
3971 return line;
3972 }
3973 }
3974
3975 /// Clear remaining unused line objects, if any
3976 bool wxRichTextParagraph::ClearUnusedLines(int lineCount)
3977 {
3978 int cachedLineCount = m_cachedLines.GetCount();
3979 if ((int) cachedLineCount > lineCount)
3980 {
3981 for (int i = 0; i < (int) (cachedLineCount - lineCount); i ++)
3982 {
3983 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetLast();
3984 wxRichTextLine* line = node->GetData();
3985 m_cachedLines.Erase(node);
3986 delete line;
3987 }
3988 }
3989 return true;
3990 }
3991
3992 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3993 /// retrieve the actual style.
3994 wxTextAttrEx wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx& contentStyle) const
3995 {
3996 wxTextAttrEx attr;
3997 wxRichTextBuffer* buf = wxDynamicCast(GetParent(), wxRichTextBuffer);
3998 if (buf)
3999 {
4000 attr = buf->GetBasicStyle();
4001 wxRichTextApplyStyle(attr, GetAttributes());
4002 }
4003 else
4004 attr = GetAttributes();
4005
4006 wxRichTextApplyStyle(attr, contentStyle);
4007 return attr;
4008 }
4009
4010 /// Get combined attributes of the base style and paragraph style.
4011 wxTextAttrEx wxRichTextParagraph::GetCombinedAttributes() const
4012 {
4013 wxTextAttrEx attr;
4014 wxRichTextBuffer* buf = wxDynamicCast(GetParent(), wxRichTextBuffer);
4015 if (buf)
4016 {
4017 attr = buf->GetBasicStyle();
4018 wxRichTextApplyStyle(attr, GetAttributes());
4019 }
4020 else
4021 attr = GetAttributes();
4022
4023 return attr;
4024 }
4025
4026 /// Create default tabstop array
4027 void wxRichTextParagraph::InitDefaultTabs()
4028 {
4029 // create a default tab list at 10 mm each.
4030 for (int i = 0; i < 20; ++i)
4031 {
4032 sm_defaultTabs.Add(i*100);
4033 }
4034 }
4035
4036 /// Clear default tabstop array
4037 void wxRichTextParagraph::ClearDefaultTabs()
4038 {
4039 sm_defaultTabs.Clear();
4040 }
4041
4042
4043 /*!
4044 * wxRichTextLine
4045 * This object represents a line in a paragraph, and stores
4046 * offsets from the start of the paragraph representing the
4047 * start and end positions of the line.
4048 */
4049
4050 wxRichTextLine::wxRichTextLine(wxRichTextParagraph* parent)
4051 {
4052 Init(parent);
4053 }
4054
4055 /// Initialisation
4056 void wxRichTextLine::Init(wxRichTextParagraph* parent)
4057 {
4058 m_parent = parent;
4059 m_range.SetRange(-1, -1);
4060 m_pos = wxPoint(0, 0);
4061 m_size = wxSize(0, 0);
4062 m_descent = 0;
4063 }
4064
4065 /// Copy
4066 void wxRichTextLine::Copy(const wxRichTextLine& obj)
4067 {
4068 m_range = obj.m_range;
4069 }
4070
4071 /// Get the absolute object position
4072 wxPoint wxRichTextLine::GetAbsolutePosition() const
4073 {
4074 return m_parent->GetPosition() + m_pos;
4075 }
4076
4077 /// Get the absolute range
4078 wxRichTextRange wxRichTextLine::GetAbsoluteRange() const
4079 {
4080 wxRichTextRange range(m_range.GetStart() + m_parent->GetRange().GetStart(), 0);
4081 range.SetEnd(range.GetStart() + m_range.GetLength()-1);
4082 return range;
4083 }
4084
4085 /*!
4086 * wxRichTextPlainText
4087 * This object represents a single piece of text.
4088 */
4089
4090 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText, wxRichTextObject)
4091
4092 wxRichTextPlainText::wxRichTextPlainText(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* style):
4093 wxRichTextObject(parent)
4094 {
4095 if (parent && !style)
4096 SetAttributes(parent->GetAttributes());
4097 if (style)
4098 SetAttributes(*style);
4099
4100 m_text = text;
4101 }
4102
4103 #define USE_KERNING_FIX 1
4104
4105 /// Draw the item
4106 bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int WXUNUSED(style))
4107 {
4108 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4109 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
4110 wxASSERT (para != NULL);
4111
4112 wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4113 #else
4114 wxTextAttrEx textAttr(GetAttributes());
4115 #endif
4116
4117 int offset = GetRange().GetStart();
4118
4119 long len = range.GetLength();
4120 wxString stringChunk = m_text.Mid(range.GetStart() - offset, (size_t) len);
4121
4122 int charHeight = dc.GetCharHeight();
4123
4124 int x = rect.x;
4125 int y = rect.y + (rect.height - charHeight - (descent - m_descent));
4126
4127 // Test for the optimized situations where all is selected, or none
4128 // is selected.
4129
4130 if (textAttr.GetFont().Ok())
4131 dc.SetFont(textAttr.GetFont());
4132
4133 // (a) All selected.
4134 if (selectionRange.GetStart() <= range.GetStart() && selectionRange.GetEnd() >= range.GetEnd())
4135 {
4136 DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, true);
4137 }
4138 // (b) None selected.
4139 else if (selectionRange.GetEnd() < range.GetStart() || selectionRange.GetStart() > range.GetEnd())
4140 {
4141 // Draw all unselected
4142 DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, false);
4143 }
4144 else
4145 {
4146 // (c) Part selected, part not
4147 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4148
4149 dc.SetBackgroundMode(wxTRANSPARENT);
4150
4151 // 1. Initial unselected chunk, if any, up until start of selection.
4152 if (selectionRange.GetStart() > range.GetStart() && selectionRange.GetStart() <= range.GetEnd())
4153 {
4154 int r1 = range.GetStart();
4155 int s1 = selectionRange.GetStart()-1;
4156 int fragmentLen = s1 - r1 + 1;
4157 if (fragmentLen < 0)
4158 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1 - offset), (int)fragmentLen);
4159 wxString stringFragment = m_text.Mid(r1 - offset, fragmentLen);
4160
4161 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
4162
4163 #if USE_KERNING_FIX
4164 if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
4165 {
4166 // Compensate for kerning difference
4167 wxString stringFragment2(m_text.Mid(r1 - offset, fragmentLen+1));
4168 wxString stringFragment3(m_text.Mid(r1 - offset + fragmentLen, 1));
4169
4170 wxCoord w1, h1, w2, h2, w3, h3;
4171 dc.GetTextExtent(stringFragment, & w1, & h1);
4172 dc.GetTextExtent(stringFragment2, & w2, & h2);
4173 dc.GetTextExtent(stringFragment3, & w3, & h3);
4174
4175 int kerningDiff = (w1 + w3) - w2;
4176 x = x - kerningDiff;
4177 }
4178 #endif
4179 }
4180
4181 // 2. Selected chunk, if any.
4182 if (selectionRange.GetEnd() >= range.GetStart())
4183 {
4184 int s1 = wxMax(selectionRange.GetStart(), range.GetStart());
4185 int s2 = wxMin(selectionRange.GetEnd(), range.GetEnd());
4186
4187 int fragmentLen = s2 - s1 + 1;
4188 if (fragmentLen < 0)
4189 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1 - offset), (int)fragmentLen);
4190 wxString stringFragment = m_text.Mid(s1 - offset, fragmentLen);
4191
4192 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, true);
4193
4194 #if USE_KERNING_FIX
4195 if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
4196 {
4197 // Compensate for kerning difference
4198 wxString stringFragment2(m_text.Mid(s1 - offset, fragmentLen+1));
4199 wxString stringFragment3(m_text.Mid(s1 - offset + fragmentLen, 1));
4200
4201 wxCoord w1, h1, w2, h2, w3, h3;
4202 dc.GetTextExtent(stringFragment, & w1, & h1);
4203 dc.GetTextExtent(stringFragment2, & w2, & h2);
4204 dc.GetTextExtent(stringFragment3, & w3, & h3);
4205
4206 int kerningDiff = (w1 + w3) - w2;
4207 x = x - kerningDiff;
4208 }
4209 #endif
4210 }
4211
4212 // 3. Remaining unselected chunk, if any
4213 if (selectionRange.GetEnd() < range.GetEnd())
4214 {
4215 int s2 = wxMin(selectionRange.GetEnd()+1, range.GetEnd());
4216 int r2 = range.GetEnd();
4217
4218 int fragmentLen = r2 - s2 + 1;
4219 if (fragmentLen < 0)
4220 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2 - offset), (int)fragmentLen);
4221 wxString stringFragment = m_text.Mid(s2 - offset, fragmentLen);
4222
4223 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
4224 }
4225 }
4226
4227 return true;
4228 }
4229
4230 bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect,wxString& str, wxCoord& x, wxCoord& y, bool selected)
4231 {
4232 bool hasTabs = (str.Find(wxT('\t')) != wxNOT_FOUND);
4233
4234 wxArrayInt tabArray;
4235 int tabCount;
4236 if (hasTabs)
4237 {
4238 if (attr.GetTabs().IsEmpty())
4239 tabArray = wxRichTextParagraph::GetDefaultTabs();
4240 else
4241 tabArray = attr.GetTabs();
4242 tabCount = tabArray.GetCount();
4243
4244 for (int i = 0; i < tabCount; ++i)
4245 {
4246 int pos = tabArray[i];
4247 pos = ConvertTenthsMMToPixels(dc, pos);
4248 tabArray[i] = pos;
4249 }
4250 }
4251 else
4252 tabCount = 0;
4253
4254 int nextTabPos = -1;
4255 int tabPos = -1;
4256 wxCoord w, h;
4257
4258 if (selected)
4259 {
4260 dc.SetBrush(*wxBLACK_BRUSH);
4261 dc.SetPen(*wxBLACK_PEN);
4262 dc.SetTextForeground(*wxWHITE);
4263 dc.SetBackgroundMode(wxTRANSPARENT);
4264 }
4265 else
4266 {
4267 dc.SetTextForeground(attr.GetTextColour());
4268 dc.SetBackgroundMode(wxTRANSPARENT);
4269 }
4270
4271 while (hasTabs)
4272 {
4273 // the string has a tab
4274 // break up the string at the Tab
4275 wxString stringChunk = str.BeforeFirst(wxT('\t'));
4276 str = str.AfterFirst(wxT('\t'));
4277 dc.GetTextExtent(stringChunk, & w, & h);
4278 tabPos = x + w;
4279 bool not_found = true;
4280 for (int i = 0; i < tabCount && not_found; ++i)
4281 {
4282 nextTabPos = tabArray.Item(i);
4283 if (nextTabPos > tabPos)
4284 {
4285 not_found = false;
4286 if (selected)
4287 {
4288 w = nextTabPos - x;
4289 wxRect selRect(x, rect.y, w, rect.GetHeight());
4290 dc.DrawRectangle(selRect);
4291 }
4292 dc.DrawText(stringChunk, x, y);
4293 x = nextTabPos;
4294 }
4295 }
4296 hasTabs = (str.Find(wxT('\t')) != wxNOT_FOUND);
4297 }
4298
4299 if (!str.IsEmpty())
4300 {
4301 dc.GetTextExtent(str, & w, & h);
4302 if (selected)
4303 {
4304 wxRect selRect(x, rect.y, w, rect.GetHeight());
4305 dc.DrawRectangle(selRect);
4306 }
4307 dc.DrawText(str, x, y);
4308 x += w;
4309 }
4310 return true;
4311
4312 }
4313
4314 /// Lay the item out
4315 bool wxRichTextPlainText::Layout(wxDC& dc, const wxRect& WXUNUSED(rect), int WXUNUSED(style))
4316 {
4317 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4318 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
4319 wxASSERT (para != NULL);
4320
4321 wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4322 #else
4323 wxTextAttrEx textAttr(GetAttributes());
4324 #endif
4325
4326 if (textAttr.GetFont().Ok())
4327 dc.SetFont(textAttr.GetFont());
4328
4329 wxCoord w, h;
4330 dc.GetTextExtent(m_text, & w, & h, & m_descent);
4331 m_size = wxSize(w, dc.GetCharHeight());
4332
4333 return true;
4334 }
4335
4336 /// Copy
4337 void wxRichTextPlainText::Copy(const wxRichTextPlainText& obj)
4338 {
4339 wxRichTextObject::Copy(obj);
4340
4341 m_text = obj.m_text;
4342 }
4343
4344 /// Get/set the object size for the given range. Returns false if the range
4345 /// is invalid for this object.
4346 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int WXUNUSED(flags), wxPoint position) const
4347 {
4348 if (!range.IsWithin(GetRange()))
4349 return false;
4350
4351 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4352 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
4353 wxASSERT (para != NULL);
4354
4355 wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4356 #else
4357 wxTextAttrEx textAttr(GetAttributes());
4358 #endif
4359
4360 // Always assume unformatted text, since at this level we have no knowledge
4361 // of line breaks - and we don't need it, since we'll calculate size within
4362 // formatted text by doing it in chunks according to the line ranges
4363
4364 if (textAttr.GetFont().Ok())
4365 dc.SetFont(textAttr.GetFont());
4366
4367 int startPos = range.GetStart() - GetRange().GetStart();
4368 long len = range.GetLength();
4369 wxString stringChunk = m_text.Mid(startPos, (size_t) len);
4370 wxCoord w, h;
4371 int width = 0;
4372 if (stringChunk.Find(wxT('\t')) != wxNOT_FOUND)
4373 {
4374 // the string has a tab
4375 wxArrayInt tabArray;
4376 if (textAttr.GetTabs().IsEmpty())
4377 tabArray = wxRichTextParagraph::GetDefaultTabs();
4378 else
4379 tabArray = textAttr.GetTabs();
4380
4381 int tabCount = tabArray.GetCount();
4382
4383 for (int i = 0; i < tabCount; ++i)
4384 {
4385 int pos = tabArray[i];
4386 pos = ((wxRichTextPlainText*) this)->ConvertTenthsMMToPixels(dc, pos);
4387 tabArray[i] = pos;
4388 }
4389
4390 int nextTabPos = -1;
4391
4392 while (stringChunk.Find(wxT('\t')) >= 0)
4393 {
4394 // the string has a tab
4395 // break up the string at the Tab
4396 wxString stringFragment = stringChunk.BeforeFirst(wxT('\t'));
4397 stringChunk = stringChunk.AfterFirst(wxT('\t'));
4398 dc.GetTextExtent(stringFragment, & w, & h);
4399 width += w;
4400 int absoluteWidth = width + position.x;
4401 bool notFound = true;
4402 for (int i = 0; i < tabCount && notFound; ++i)
4403 {
4404 nextTabPos = tabArray.Item(i);
4405 if (nextTabPos > absoluteWidth)
4406 {
4407 notFound = false;
4408 width = nextTabPos - position.x;
4409 }
4410 }
4411 }
4412 }
4413 dc.GetTextExtent(stringChunk, & w, & h, & descent);
4414 width += w;
4415 size = wxSize(width, dc.GetCharHeight());
4416
4417 return true;
4418 }
4419
4420 /// Do a split, returning an object containing the second part, and setting
4421 /// the first part in 'this'.
4422 wxRichTextObject* wxRichTextPlainText::DoSplit(long pos)
4423 {
4424 int index = pos - GetRange().GetStart();
4425 if (index < 0 || index >= (int) m_text.length())
4426 return NULL;
4427
4428 wxString firstPart = m_text.Mid(0, index);
4429 wxString secondPart = m_text.Mid(index);
4430
4431 m_text = firstPart;
4432
4433 wxRichTextPlainText* newObject = new wxRichTextPlainText(secondPart);
4434 newObject->SetAttributes(GetAttributes());
4435
4436 newObject->SetRange(wxRichTextRange(pos, GetRange().GetEnd()));
4437 GetRange().SetEnd(pos-1);
4438
4439 return newObject;
4440 }
4441
4442 /// Calculate range
4443 void wxRichTextPlainText::CalculateRange(long start, long& end)
4444 {
4445 end = start + m_text.length() - 1;
4446 m_range.SetRange(start, end);
4447 }
4448
4449 /// Delete range
4450 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange& range)
4451 {
4452 wxRichTextRange r = range;
4453
4454 r.LimitTo(GetRange());
4455
4456 if (r.GetStart() == GetRange().GetStart() && r.GetEnd() == GetRange().GetEnd())
4457 {
4458 m_text.Empty();
4459 return true;
4460 }
4461
4462 long startIndex = r.GetStart() - GetRange().GetStart();
4463 long len = r.GetLength();
4464
4465 m_text = m_text.Mid(0, startIndex) + m_text.Mid(startIndex+len);
4466 return true;
4467 }
4468
4469 /// Get text for the given range.
4470 wxString wxRichTextPlainText::GetTextForRange(const wxRichTextRange& range) const
4471 {
4472 wxRichTextRange r = range;
4473
4474 r.LimitTo(GetRange());
4475
4476 long startIndex = r.GetStart() - GetRange().GetStart();
4477 long len = r.GetLength();
4478
4479 return m_text.Mid(startIndex, len);
4480 }
4481
4482 /// Returns true if this object can merge itself with the given one.
4483 bool wxRichTextPlainText::CanMerge(wxRichTextObject* object) const
4484 {
4485 return object->GetClassInfo() == CLASSINFO(wxRichTextPlainText) &&
4486 (m_text.empty() || wxTextAttrEq(GetAttributes(), object->GetAttributes()));
4487 }
4488
4489 /// Returns true if this object merged itself with the given one.
4490 /// The calling code will then delete the given object.
4491 bool wxRichTextPlainText::Merge(wxRichTextObject* object)
4492 {
4493 wxRichTextPlainText* textObject = wxDynamicCast(object, wxRichTextPlainText);
4494 wxASSERT( textObject != NULL );
4495
4496 if (textObject)
4497 {
4498 m_text += textObject->GetText();
4499 return true;
4500 }
4501 else
4502 return false;
4503 }
4504
4505 /// Dump to output stream for debugging
4506 void wxRichTextPlainText::Dump(wxTextOutputStream& stream)
4507 {
4508 wxRichTextObject::Dump(stream);
4509 stream << m_text << wxT("\n");
4510 }
4511
4512 /*!
4513 * wxRichTextBuffer
4514 * This is a kind of box, used to represent the whole buffer
4515 */
4516
4517 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer, wxRichTextParagraphLayoutBox)
4518
4519 wxList wxRichTextBuffer::sm_handlers;
4520 wxRichTextRenderer* wxRichTextBuffer::sm_renderer = NULL;
4521 int wxRichTextBuffer::sm_bulletRightMargin = 20;
4522 float wxRichTextBuffer::sm_bulletProportion = (float) 0.3;
4523
4524 /// Initialisation
4525 void wxRichTextBuffer::Init()
4526 {
4527 m_commandProcessor = new wxCommandProcessor;
4528 m_styleSheet = NULL;
4529 m_modified = false;
4530 m_batchedCommandDepth = 0;
4531 m_batchedCommand = NULL;
4532 m_suppressUndo = 0;
4533 m_handlerFlags = 0;
4534 m_scale = 1.0;
4535 }
4536
4537 /// Initialisation
4538 wxRichTextBuffer::~wxRichTextBuffer()
4539 {
4540 delete m_commandProcessor;
4541 delete m_batchedCommand;
4542
4543 ClearStyleStack();
4544 ClearEventHandlers();
4545 }
4546
4547 void wxRichTextBuffer::Clear()
4548 {
4549 DeleteChildren();
4550 GetCommandProcessor()->ClearCommands();
4551 Modify(false);
4552 Invalidate(wxRICHTEXT_ALL);
4553 }
4554
4555 void wxRichTextBuffer::Reset()
4556 {
4557 DeleteChildren();
4558 AddParagraph(wxEmptyString);
4559 GetCommandProcessor()->ClearCommands();
4560 Modify(false);
4561 Invalidate(wxRICHTEXT_ALL);
4562 }
4563
4564 void wxRichTextBuffer::Copy(const wxRichTextBuffer& obj)
4565 {
4566 wxRichTextParagraphLayoutBox::Copy(obj);
4567
4568 m_styleSheet = obj.m_styleSheet;
4569 m_modified = obj.m_modified;
4570 m_batchedCommandDepth = obj.m_batchedCommandDepth;
4571 m_batchedCommand = obj.m_batchedCommand;
4572 m_suppressUndo = obj.m_suppressUndo;
4573 }
4574
4575 /// Push style sheet to top of stack
4576 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet* styleSheet)
4577 {
4578 if (m_styleSheet)
4579 styleSheet->InsertSheet(m_styleSheet);
4580
4581 SetStyleSheet(styleSheet);
4582
4583 return true;
4584 }
4585
4586 /// Pop style sheet from top of stack
4587 wxRichTextStyleSheet* wxRichTextBuffer::PopStyleSheet()
4588 {
4589 if (m_styleSheet)
4590 {
4591 wxRichTextStyleSheet* oldSheet = m_styleSheet;
4592 m_styleSheet = oldSheet->GetNextSheet();
4593 oldSheet->Unlink();
4594
4595 return oldSheet;
4596 }
4597 else
4598 return NULL;
4599 }
4600
4601 /// Submit command to insert paragraphs
4602 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos, const wxRichTextParagraphLayoutBox& paragraphs, wxRichTextCtrl* ctrl, int flags)
4603 {
4604 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
4605
4606 wxTextAttrEx* p = NULL;
4607 wxTextAttrEx paraAttr;
4608 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
4609 {
4610 paraAttr = GetStyleForNewParagraph(pos);
4611 if (!paraAttr.IsDefault())
4612 p = & paraAttr;
4613 }
4614
4615 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4616 wxTextAttrEx attr(GetDefaultStyle());
4617 #else
4618 wxTextAttrEx attr(GetBasicStyle());
4619 wxRichTextApplyStyle(attr, GetDefaultStyle());
4620 #endif
4621
4622 action->GetNewParagraphs() = paragraphs;
4623
4624 if (p)
4625 {
4626 wxRichTextObjectList::compatibility_iterator node = m_children.GetLast();
4627 while (node)
4628 {
4629 wxRichTextParagraph* obj = (wxRichTextParagraph*) node->GetData();
4630 obj->SetAttributes(*p);
4631 node = node->GetPrevious();
4632 }
4633 }
4634
4635 action->SetPosition(pos);
4636
4637 // Set the range we'll need to delete in Undo
4638 action->SetRange(wxRichTextRange(pos, pos + paragraphs.GetRange().GetEnd() - 1));
4639
4640 SubmitAction(action);
4641
4642 return true;
4643 }
4644
4645 /// Submit command to insert the given text
4646 bool wxRichTextBuffer::InsertTextWithUndo(long pos, const wxString& text, wxRichTextCtrl* ctrl, int flags)
4647 {
4648 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
4649
4650 wxTextAttrEx* p = NULL;
4651 wxTextAttrEx paraAttr;
4652 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
4653 {
4654 paraAttr = GetStyleForNewParagraph(pos);
4655 if (!paraAttr.IsDefault())
4656 p = & paraAttr;
4657 }
4658
4659 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4660 wxTextAttrEx attr(GetDefaultStyle());
4661 #else
4662 wxTextAttrEx attr(GetBasicStyle());
4663 wxRichTextApplyStyle(attr, GetDefaultStyle());
4664 #endif
4665
4666 action->GetNewParagraphs().AddParagraphs(text, p);
4667
4668 int length = action->GetNewParagraphs().GetRange().GetLength();
4669
4670 if (text.length() > 0 && text.Last() != wxT('\n'))
4671 {
4672 // Don't count the newline when undoing
4673 length --;
4674 action->GetNewParagraphs().SetPartialParagraph(true);
4675 }
4676
4677 action->SetPosition(pos);
4678
4679 // Set the range we'll need to delete in Undo
4680 action->SetRange(wxRichTextRange(pos, pos + length - 1));
4681
4682 SubmitAction(action);
4683
4684 return true;
4685 }
4686
4687 /// Submit command to insert the given text
4688 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int flags)
4689 {
4690 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
4691
4692 wxTextAttrEx* p = NULL;
4693 wxTextAttrEx paraAttr;
4694 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
4695 {
4696 paraAttr = GetStyleForNewParagraph(pos);
4697 if (!paraAttr.IsDefault())
4698 p = & paraAttr;
4699 }
4700
4701 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4702 wxTextAttrEx attr(GetDefaultStyle());
4703 #else
4704 wxTextAttrEx attr(GetBasicStyle());
4705 wxRichTextApplyStyle(attr, GetDefaultStyle());
4706 #endif
4707
4708 wxRichTextParagraph* newPara = new wxRichTextParagraph(wxEmptyString, this, & attr);
4709 action->GetNewParagraphs().AppendChild(newPara);
4710 action->GetNewParagraphs().UpdateRanges();
4711 action->GetNewParagraphs().SetPartialParagraph(false);
4712 action->SetPosition(pos);
4713
4714 if (p)
4715 newPara->SetAttributes(*p);
4716
4717 // Set the range we'll need to delete in Undo
4718 action->SetRange(wxRichTextRange(pos, pos));
4719
4720 SubmitAction(action);
4721
4722 return true;
4723 }
4724
4725 /// Submit command to insert the given image
4726 bool wxRichTextBuffer::InsertImageWithUndo(long pos, const wxRichTextImageBlock& imageBlock, wxRichTextCtrl* ctrl, int flags)
4727 {
4728 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, ctrl, false);
4729
4730 wxTextAttrEx* p = NULL;
4731 wxTextAttrEx paraAttr;
4732 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
4733 {
4734 paraAttr = GetStyleForNewParagraph(pos);
4735 if (!paraAttr.IsDefault())
4736 p = & paraAttr;
4737 }
4738
4739 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4740 wxTextAttrEx attr(GetDefaultStyle());
4741 #else
4742 wxTextAttrEx attr(GetBasicStyle());
4743 wxRichTextApplyStyle(attr, GetDefaultStyle());
4744 #endif
4745
4746 wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr);
4747 if (p)
4748 newPara->SetAttributes(*p);
4749
4750 wxRichTextImage* imageObject = new wxRichTextImage(imageBlock, newPara);
4751 newPara->AppendChild(imageObject);
4752 action->GetNewParagraphs().AppendChild(newPara);
4753 action->GetNewParagraphs().UpdateRanges();
4754
4755 action->GetNewParagraphs().SetPartialParagraph(true);
4756
4757 action->SetPosition(pos);
4758
4759 // Set the range we'll need to delete in Undo
4760 action->SetRange(wxRichTextRange(pos, pos));
4761
4762 SubmitAction(action);
4763
4764 return true;
4765 }
4766
4767 /// Get the style that is appropriate for a new paragraph at this position.
4768 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4769 /// style.
4770 wxRichTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPosition) const
4771 {
4772 wxRichTextParagraph* para = GetParagraphAtPosition(pos, caretPosition);
4773 if (para)
4774 {
4775 wxRichTextAttr attr;
4776 bool foundAttributes = false;
4777
4778 // Look for a matching paragraph style
4779 if (!para->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4780 {
4781 wxRichTextParagraphStyleDefinition* paraDef = GetStyleSheet()->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
4782 if (paraDef)
4783 {
4784 if (!paraDef->GetNextStyle().IsEmpty())
4785 {
4786 wxRichTextParagraphStyleDefinition* nextParaDef = GetStyleSheet()->FindParagraphStyle(paraDef->GetNextStyle());
4787 if (nextParaDef)
4788 {
4789 foundAttributes = true;
4790 attr = nextParaDef->GetStyle();
4791 }
4792 }
4793
4794 // If we didn't find the 'next style', use this style instead.
4795 if (!foundAttributes)
4796 {
4797 foundAttributes = true;
4798 attr = paraDef->GetStyle();
4799 }
4800 }
4801 }
4802 if (!foundAttributes)
4803 {
4804 attr = para->GetAttributes();
4805 int flags = attr.GetFlags();
4806
4807 // Eliminate character styles
4808 flags &= ( (~ wxTEXT_ATTR_FONT) |
4809 (~ wxTEXT_ATTR_TEXT_COLOUR) |
4810 (~ wxTEXT_ATTR_BACKGROUND_COLOUR) );
4811 attr.SetFlags(flags);
4812 }
4813
4814 // Now see if we need to number the paragraph.
4815 if (attr.HasBulletStyle())
4816 {
4817 wxRichTextAttr numberingAttr;
4818 if (FindNextParagraphNumber(para, numberingAttr))
4819 wxRichTextApplyStyle(attr, numberingAttr);
4820 }
4821
4822 return attr;
4823 }
4824 else
4825 return wxRichTextAttr();
4826 }
4827
4828 /// Submit command to delete this range
4829 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange& range, long initialCaretPosition, long WXUNUSED(newCaretPositon), wxRichTextCtrl* ctrl)
4830 {
4831 wxRichTextAction* action = new wxRichTextAction(NULL, _("Delete"), wxRICHTEXT_DELETE, this, ctrl);
4832
4833 action->SetPosition(initialCaretPosition);
4834
4835 // Set the range to delete
4836 action->SetRange(range);
4837
4838 // Copy the fragment that we'll need to restore in Undo
4839 CopyFragment(range, action->GetOldParagraphs());
4840
4841 // Special case: if there is only one (non-partial) paragraph,
4842 // we must save the *next* paragraph's style, because that
4843 // is the style we must apply when inserting the content back
4844 // when undoing the delete. (This is because we're merging the
4845 // paragraph with the previous paragraph and throwing away
4846 // the style, and we need to restore it.)
4847 if (!action->GetOldParagraphs().GetPartialParagraph() && action->GetOldParagraphs().GetChildCount() == 1)
4848 {
4849 wxRichTextParagraph* lastPara = GetParagraphAtPosition(range.GetStart());
4850 if (lastPara)
4851 {
4852 wxRichTextParagraph* nextPara = GetParagraphAtPosition(range.GetEnd()+1);
4853 if (nextPara)
4854 {
4855 wxRichTextParagraph* para = (wxRichTextParagraph*) action->GetOldParagraphs().GetChild(0);
4856 para->SetAttributes(nextPara->GetAttributes());
4857 }
4858 }
4859 }
4860
4861 SubmitAction(action);
4862
4863 return true;
4864 }
4865
4866 /// Collapse undo/redo commands
4867 bool wxRichTextBuffer::BeginBatchUndo(const wxString& cmdName)
4868 {
4869 if (m_batchedCommandDepth == 0)
4870 {
4871 wxASSERT(m_batchedCommand == NULL);
4872 if (m_batchedCommand)
4873 {
4874 GetCommandProcessor()->Submit(m_batchedCommand);
4875 }
4876 m_batchedCommand = new wxRichTextCommand(cmdName);
4877 }
4878
4879 m_batchedCommandDepth ++;
4880
4881 return true;
4882 }
4883
4884 /// Collapse undo/redo commands
4885 bool wxRichTextBuffer::EndBatchUndo()
4886 {
4887 m_batchedCommandDepth --;
4888
4889 wxASSERT(m_batchedCommandDepth >= 0);
4890 wxASSERT(m_batchedCommand != NULL);
4891
4892 if (m_batchedCommandDepth == 0)
4893 {
4894 GetCommandProcessor()->Submit(m_batchedCommand);
4895 m_batchedCommand = NULL;
4896 }
4897
4898 return true;
4899 }
4900
4901 /// Submit immediately, or delay according to whether collapsing is on
4902 bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action)
4903 {
4904 if (BatchingUndo() && m_batchedCommand && !SuppressingUndo())
4905 m_batchedCommand->AddAction(action);
4906 else
4907 {
4908 wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName());
4909 cmd->AddAction(action);
4910
4911 // Only store it if we're not suppressing undo.
4912 return GetCommandProcessor()->Submit(cmd, !SuppressingUndo());
4913 }
4914
4915 return true;
4916 }
4917
4918 /// Begin suppressing undo/redo commands.
4919 bool wxRichTextBuffer::BeginSuppressUndo()
4920 {
4921 m_suppressUndo ++;
4922
4923 return true;
4924 }
4925
4926 /// End suppressing undo/redo commands.
4927 bool wxRichTextBuffer::EndSuppressUndo()
4928 {
4929 m_suppressUndo --;
4930
4931 return true;
4932 }
4933
4934 /// Begin using a style
4935 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx& style)
4936 {
4937 wxTextAttrEx newStyle(GetDefaultStyle());
4938
4939 // Save the old default style
4940 m_attributeStack.Append((wxObject*) new wxTextAttrEx(GetDefaultStyle()));
4941
4942 wxRichTextApplyStyle(newStyle, style);
4943 newStyle.SetFlags(style.GetFlags()|newStyle.GetFlags());
4944
4945 SetDefaultStyle(newStyle);
4946
4947 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4948
4949 return true;
4950 }
4951
4952 /// End the style
4953 bool wxRichTextBuffer::EndStyle()
4954 {
4955 if (!m_attributeStack.GetFirst())
4956 {
4957 wxLogDebug(_("Too many EndStyle calls!"));
4958 return false;
4959 }
4960
4961 wxList::compatibility_iterator node = m_attributeStack.GetLast();
4962 wxTextAttrEx* attr = (wxTextAttrEx*)node->GetData();
4963 m_attributeStack.Erase(node);
4964
4965 SetDefaultStyle(*attr);
4966
4967 delete attr;
4968 return true;
4969 }
4970
4971 /// End all styles
4972 bool wxRichTextBuffer::EndAllStyles()
4973 {
4974 while (m_attributeStack.GetCount() != 0)
4975 EndStyle();
4976 return true;
4977 }
4978
4979 /// Clear the style stack
4980 void wxRichTextBuffer::ClearStyleStack()
4981 {
4982 for (wxList::compatibility_iterator node = m_attributeStack.GetFirst(); node; node = node->GetNext())
4983 delete (wxTextAttrEx*) node->GetData();
4984 m_attributeStack.Clear();
4985 }
4986
4987 /// Begin using bold
4988 bool wxRichTextBuffer::BeginBold()
4989 {
4990 wxFont font(GetBasicStyle().GetFont());
4991 font.SetWeight(wxBOLD);
4992
4993 wxTextAttrEx attr;
4994 attr.SetFont(font,wxTEXT_ATTR_FONT_WEIGHT);
4995
4996 return BeginStyle(attr);
4997 }
4998
4999 /// Begin using italic
5000 bool wxRichTextBuffer::BeginItalic()
5001 {
5002 wxFont font(GetBasicStyle().GetFont());
5003 font.SetStyle(wxITALIC);
5004
5005 wxTextAttrEx attr;
5006 attr.SetFont(font, wxTEXT_ATTR_FONT_ITALIC);
5007
5008 return BeginStyle(attr);
5009 }
5010
5011 /// Begin using underline
5012 bool wxRichTextBuffer::BeginUnderline()
5013 {
5014 wxFont font(GetBasicStyle().GetFont());
5015 font.SetUnderlined(true);
5016
5017 wxTextAttrEx attr;
5018 attr.SetFont(font, wxTEXT_ATTR_FONT_UNDERLINE);
5019
5020 return BeginStyle(attr);
5021 }
5022
5023 /// Begin using point size
5024 bool wxRichTextBuffer::BeginFontSize(int pointSize)
5025 {
5026 wxFont font(GetBasicStyle().GetFont());
5027 font.SetPointSize(pointSize);
5028
5029 wxTextAttrEx attr;
5030 attr.SetFont(font, wxTEXT_ATTR_FONT_SIZE);
5031
5032 return BeginStyle(attr);
5033 }
5034
5035 /// Begin using this font
5036 bool wxRichTextBuffer::BeginFont(const wxFont& font)
5037 {
5038 wxTextAttrEx attr;
5039 attr.SetFlags(wxTEXT_ATTR_FONT);
5040 attr.SetFont(font);
5041
5042 return BeginStyle(attr);
5043 }
5044
5045 /// Begin using this colour
5046 bool wxRichTextBuffer::BeginTextColour(const wxColour& colour)
5047 {
5048 wxTextAttrEx attr;
5049 attr.SetFlags(wxTEXT_ATTR_TEXT_COLOUR);
5050 attr.SetTextColour(colour);
5051
5052 return BeginStyle(attr);
5053 }
5054
5055 /// Begin using alignment
5056 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment)
5057 {
5058 wxTextAttrEx attr;
5059 attr.SetFlags(wxTEXT_ATTR_ALIGNMENT);
5060 attr.SetAlignment(alignment);
5061
5062 return BeginStyle(attr);
5063 }
5064
5065 /// Begin left indent
5066 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent, int leftSubIndent)
5067 {
5068 wxTextAttrEx attr;
5069 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
5070 attr.SetLeftIndent(leftIndent, leftSubIndent);
5071
5072 return BeginStyle(attr);
5073 }
5074
5075 /// Begin right indent
5076 bool wxRichTextBuffer::BeginRightIndent(int rightIndent)
5077 {
5078 wxTextAttrEx attr;
5079 attr.SetFlags(wxTEXT_ATTR_RIGHT_INDENT);
5080 attr.SetRightIndent(rightIndent);
5081
5082 return BeginStyle(attr);
5083 }
5084
5085 /// Begin paragraph spacing
5086 bool wxRichTextBuffer::BeginParagraphSpacing(int before, int after)
5087 {
5088 long flags = 0;
5089 if (before != 0)
5090 flags |= wxTEXT_ATTR_PARA_SPACING_BEFORE;
5091 if (after != 0)
5092 flags |= wxTEXT_ATTR_PARA_SPACING_AFTER;
5093
5094 wxTextAttrEx attr;
5095 attr.SetFlags(flags);
5096 attr.SetParagraphSpacingBefore(before);
5097 attr.SetParagraphSpacingAfter(after);
5098
5099 return BeginStyle(attr);
5100 }
5101
5102 /// Begin line spacing
5103 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing)
5104 {
5105 wxTextAttrEx attr;
5106 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
5107 attr.SetLineSpacing(lineSpacing);
5108
5109 return BeginStyle(attr);
5110 }
5111
5112 /// Begin numbered bullet
5113 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber, int leftIndent, int leftSubIndent, int bulletStyle)
5114 {
5115 wxTextAttrEx attr;
5116 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_LEFT_INDENT);
5117 attr.SetBulletStyle(bulletStyle);
5118 attr.SetBulletNumber(bulletNumber);
5119 attr.SetLeftIndent(leftIndent, leftSubIndent);
5120
5121 return BeginStyle(attr);
5122 }
5123
5124 /// Begin symbol bullet
5125 bool wxRichTextBuffer::BeginSymbolBullet(const wxString& symbol, int leftIndent, int leftSubIndent, int bulletStyle)
5126 {
5127 wxTextAttrEx attr;
5128 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_LEFT_INDENT);
5129 attr.SetBulletStyle(bulletStyle);
5130 attr.SetLeftIndent(leftIndent, leftSubIndent);
5131 attr.SetBulletText(symbol);
5132
5133 return BeginStyle(attr);
5134 }
5135
5136 /// Begin standard bullet
5137 bool wxRichTextBuffer::BeginStandardBullet(const wxString& bulletName, int leftIndent, int leftSubIndent, int bulletStyle)
5138 {
5139 wxTextAttrEx attr;
5140 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_LEFT_INDENT);
5141 attr.SetBulletStyle(bulletStyle);
5142 attr.SetLeftIndent(leftIndent, leftSubIndent);
5143 attr.SetBulletName(bulletName);
5144
5145 return BeginStyle(attr);
5146 }
5147
5148 /// Begin named character style
5149 bool wxRichTextBuffer::BeginCharacterStyle(const wxString& characterStyle)
5150 {
5151 if (GetStyleSheet())
5152 {
5153 wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle);
5154 if (def)
5155 {
5156 wxTextAttrEx attr;
5157 def->GetStyle().CopyTo(attr);
5158 return BeginStyle(attr);
5159 }
5160 }
5161 return false;
5162 }
5163
5164 /// Begin named paragraph style
5165 bool wxRichTextBuffer::BeginParagraphStyle(const wxString& paragraphStyle)
5166 {
5167 if (GetStyleSheet())
5168 {
5169 wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(paragraphStyle);
5170 if (def)
5171 {
5172 wxTextAttrEx attr;
5173 def->GetStyle().CopyTo(attr);
5174 return BeginStyle(attr);
5175 }
5176 }
5177 return false;
5178 }
5179
5180 /// Begin named list style
5181 bool wxRichTextBuffer::BeginListStyle(const wxString& listStyle, int level, int number)
5182 {
5183 if (GetStyleSheet())
5184 {
5185 wxRichTextListStyleDefinition* def = GetStyleSheet()->FindListStyle(listStyle);
5186 if (def)
5187 {
5188 wxTextAttrEx attr(def->GetCombinedStyleForLevel(level));
5189
5190 attr.SetBulletNumber(number);
5191
5192 return BeginStyle(attr);
5193 }
5194 }
5195 return false;
5196 }
5197
5198 /// Begin URL
5199 bool wxRichTextBuffer::BeginURL(const wxString& url, const wxString& characterStyle)
5200 {
5201 wxTextAttrEx attr;
5202
5203 if (!characterStyle.IsEmpty() && GetStyleSheet())
5204 {
5205 wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle);
5206 if (def)
5207 {
5208 def->GetStyle().CopyTo(attr);
5209 }
5210 }
5211 attr.SetURL(url);
5212
5213 return BeginStyle(attr);
5214 }
5215
5216 /// Adds a handler to the end
5217 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler *handler)
5218 {
5219 sm_handlers.Append(handler);
5220 }
5221
5222 /// Inserts a handler at the front
5223 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler *handler)
5224 {
5225 sm_handlers.Insert( handler );
5226 }
5227
5228 /// Removes a handler
5229 bool wxRichTextBuffer::RemoveHandler(const wxString& name)
5230 {
5231 wxRichTextFileHandler *handler = FindHandler(name);
5232 if (handler)
5233 {
5234 sm_handlers.DeleteObject(handler);
5235 delete handler;
5236 return true;
5237 }
5238 else
5239 return false;
5240 }
5241
5242 /// Finds a handler by filename or, if supplied, type
5243 wxRichTextFileHandler *wxRichTextBuffer::FindHandlerFilenameOrType(const wxString& filename, int imageType)
5244 {
5245 if (imageType != wxRICHTEXT_TYPE_ANY)
5246 return FindHandler(imageType);
5247 else if (!filename.IsEmpty())
5248 {
5249 wxString path, file, ext;
5250 wxSplitPath(filename, & path, & file, & ext);
5251 return FindHandler(ext, imageType);
5252 }
5253 else
5254 return NULL;
5255 }
5256
5257
5258 /// Finds a handler by name
5259 wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& name)
5260 {
5261 wxList::compatibility_iterator node = sm_handlers.GetFirst();
5262 while (node)
5263 {
5264 wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData();
5265 if (handler->GetName().Lower() == name.Lower()) return handler;
5266
5267 node = node->GetNext();
5268 }
5269 return NULL;
5270 }
5271
5272 /// Finds a handler by extension and type
5273 wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& extension, int type)
5274 {
5275 wxList::compatibility_iterator node = sm_handlers.GetFirst();
5276 while (node)
5277 {
5278 wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData();
5279 if ( handler->GetExtension().Lower() == extension.Lower() &&
5280 (type == wxRICHTEXT_TYPE_ANY || handler->GetType() == type) )
5281 return handler;
5282 node = node->GetNext();
5283 }
5284 return 0;
5285 }
5286
5287 /// Finds a handler by type
5288 wxRichTextFileHandler* wxRichTextBuffer::FindHandler(int type)
5289 {
5290 wxList::compatibility_iterator node = sm_handlers.GetFirst();
5291 while (node)
5292 {
5293 wxRichTextFileHandler *handler = (wxRichTextFileHandler *)node->GetData();
5294 if (handler->GetType() == type) return handler;
5295 node = node->GetNext();
5296 }
5297 return NULL;
5298 }
5299
5300 void wxRichTextBuffer::InitStandardHandlers()
5301 {
5302 if (!FindHandler(wxRICHTEXT_TYPE_TEXT))
5303 AddHandler(new wxRichTextPlainTextHandler);
5304 }
5305
5306 void wxRichTextBuffer::CleanUpHandlers()
5307 {
5308 wxList::compatibility_iterator node = sm_handlers.GetFirst();
5309 while (node)
5310 {
5311 wxRichTextFileHandler* handler = (wxRichTextFileHandler*)node->GetData();
5312 wxList::compatibility_iterator next = node->GetNext();
5313 delete handler;
5314 node = next;
5315 }
5316
5317 sm_handlers.Clear();
5318 }
5319
5320 wxString wxRichTextBuffer::GetExtWildcard(bool combine, bool save, wxArrayInt* types)
5321 {
5322 if (types)
5323 types->Clear();
5324
5325 wxString wildcard;
5326
5327 wxList::compatibility_iterator node = GetHandlers().GetFirst();
5328 int count = 0;
5329 while (node)
5330 {
5331 wxRichTextFileHandler* handler = (wxRichTextFileHandler*) node->GetData();
5332 if (handler->IsVisible() && ((save && handler->CanSave()) || !save && handler->CanLoad()))
5333 {
5334 if (combine)
5335 {
5336 if (count > 0)
5337 wildcard += wxT(";");
5338 wildcard += wxT("*.") + handler->GetExtension();
5339 }
5340 else
5341 {
5342 if (count > 0)
5343 wildcard += wxT("|");
5344 wildcard += handler->GetName();
5345 wildcard += wxT(" ");
5346 wildcard += _("files");
5347 wildcard += wxT(" (*.");
5348 wildcard += handler->GetExtension();
5349 wildcard += wxT(")|*.");
5350 wildcard += handler->GetExtension();
5351 if (types)
5352 types->Add(handler->GetType());
5353 }
5354 count ++;
5355 }
5356
5357 node = node->GetNext();
5358 }
5359
5360 if (combine)
5361 wildcard = wxT("(") + wildcard + wxT(")|") + wildcard;
5362 return wildcard;
5363 }
5364
5365 /// Load a file
5366 bool wxRichTextBuffer::LoadFile(const wxString& filename, int type)
5367 {
5368 wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type);
5369 if (handler)
5370 {
5371 SetDefaultStyle(wxTextAttrEx());
5372 handler->SetFlags(GetHandlerFlags());
5373 bool success = handler->LoadFile(this, filename);
5374 Invalidate(wxRICHTEXT_ALL);
5375 return success;
5376 }
5377 else
5378 return false;
5379 }
5380
5381 /// Save a file
5382 bool wxRichTextBuffer::SaveFile(const wxString& filename, int type)
5383 {
5384 wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type);
5385 if (handler)
5386 {
5387 handler->SetFlags(GetHandlerFlags());
5388 return handler->SaveFile(this, filename);
5389 }
5390 else
5391 return false;
5392 }
5393
5394 /// Load from a stream
5395 bool wxRichTextBuffer::LoadFile(wxInputStream& stream, int type)
5396 {
5397 wxRichTextFileHandler* handler = FindHandler(type);
5398 if (handler)
5399 {
5400 SetDefaultStyle(wxTextAttrEx());
5401 handler->SetFlags(GetHandlerFlags());
5402 bool success = handler->LoadFile(this, stream);
5403 Invalidate(wxRICHTEXT_ALL);
5404 return success;
5405 }
5406 else
5407 return false;
5408 }
5409
5410 /// Save to a stream
5411 bool wxRichTextBuffer::SaveFile(wxOutputStream& stream, int type)
5412 {
5413 wxRichTextFileHandler* handler = FindHandler(type);
5414 if (handler)
5415 {
5416 handler->SetFlags(GetHandlerFlags());
5417 return handler->SaveFile(this, stream);
5418 }
5419 else
5420 return false;
5421 }
5422
5423 /// Copy the range to the clipboard
5424 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange& range)
5425 {
5426 bool success = false;
5427 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5428
5429 if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open())
5430 {
5431 wxTheClipboard->Clear();
5432
5433 // Add composite object
5434
5435 wxDataObjectComposite* compositeObject = new wxDataObjectComposite();
5436
5437 {
5438 wxString text = GetTextForRange(range);
5439
5440 #ifdef __WXMSW__
5441 text = wxTextFile::Translate(text, wxTextFileType_Dos);
5442 #endif
5443
5444 compositeObject->Add(new wxTextDataObject(text), false /* not preferred */);
5445 }
5446
5447 // Add rich text buffer data object. This needs the XML handler to be present.
5448
5449 if (FindHandler(wxRICHTEXT_TYPE_XML))
5450 {
5451 wxRichTextBuffer* richTextBuf = new wxRichTextBuffer;
5452 CopyFragment(range, *richTextBuf);
5453
5454 compositeObject->Add(new wxRichTextBufferDataObject(richTextBuf), true /* preferred */);
5455 }
5456
5457 if (wxTheClipboard->SetData(compositeObject))
5458 success = true;
5459
5460 wxTheClipboard->Close();
5461 }
5462
5463 #else
5464 wxUnusedVar(range);
5465 #endif
5466 return success;
5467 }
5468
5469 /// Paste the clipboard content to the buffer
5470 bool wxRichTextBuffer::PasteFromClipboard(long position)
5471 {
5472 bool success = false;
5473 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5474 if (CanPasteFromClipboard())
5475 {
5476 if (wxTheClipboard->Open())
5477 {
5478 if (wxTheClipboard->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5479 {
5480 wxRichTextBufferDataObject data;
5481 wxTheClipboard->GetData(data);
5482 wxRichTextBuffer* richTextBuffer = data.GetRichTextBuffer();
5483 if (richTextBuffer)
5484 {
5485 InsertParagraphsWithUndo(position+1, *richTextBuffer, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
5486 delete richTextBuffer;
5487 }
5488 }
5489 else if (wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_UNICODETEXT))
5490 {
5491 wxTextDataObject data;
5492 wxTheClipboard->GetData(data);
5493 wxString text(data.GetText());
5494 text.Replace(_T("\r\n"), _T("\n"));
5495
5496 InsertTextWithUndo(position+1, text, GetRichTextCtrl());
5497
5498 success = true;
5499 }
5500 else if (wxTheClipboard->IsSupported(wxDF_BITMAP))
5501 {
5502 wxBitmapDataObject data;
5503 wxTheClipboard->GetData(data);
5504 wxBitmap bitmap(data.GetBitmap());
5505 wxImage image(bitmap.ConvertToImage());
5506
5507 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, GetRichTextCtrl(), false);
5508
5509 action->GetNewParagraphs().AddImage(image);
5510
5511 if (action->GetNewParagraphs().GetChildCount() == 1)
5512 action->GetNewParagraphs().SetPartialParagraph(true);
5513
5514 action->SetPosition(position);
5515
5516 // Set the range we'll need to delete in Undo
5517 action->SetRange(wxRichTextRange(position, position));
5518
5519 SubmitAction(action);
5520
5521 success = true;
5522 }
5523 wxTheClipboard->Close();
5524 }
5525 }
5526 #else
5527 wxUnusedVar(position);
5528 #endif
5529 return success;
5530 }
5531
5532 /// Can we paste from the clipboard?
5533 bool wxRichTextBuffer::CanPasteFromClipboard() const
5534 {
5535 bool canPaste = false;
5536 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5537 if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open())
5538 {
5539 if (wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_UNICODETEXT) ||
5540 wxTheClipboard->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5541 wxTheClipboard->IsSupported(wxDF_BITMAP))
5542 {
5543 canPaste = true;
5544 }
5545 wxTheClipboard->Close();
5546 }
5547 #endif
5548 return canPaste;
5549 }
5550
5551 /// Dumps contents of buffer for debugging purposes
5552 void wxRichTextBuffer::Dump()
5553 {
5554 wxString text;
5555 {
5556 wxStringOutputStream stream(& text);
5557 wxTextOutputStream textStream(stream);
5558 Dump(textStream);
5559 }
5560
5561 wxLogDebug(text);
5562 }
5563
5564 /// Add an event handler
5565 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler* handler)
5566 {
5567 m_eventHandlers.Append(handler);
5568 return true;
5569 }
5570
5571 /// Remove an event handler
5572 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler* handler, bool deleteHandler)
5573 {
5574 wxList::compatibility_iterator node = m_eventHandlers.Find(handler);
5575 if (node)
5576 {
5577 m_eventHandlers.Erase(node);
5578 if (deleteHandler)
5579 delete handler;
5580
5581 return true;
5582 }
5583 else
5584 return false;
5585 }
5586
5587 /// Clear event handlers
5588 void wxRichTextBuffer::ClearEventHandlers()
5589 {
5590 m_eventHandlers.Clear();
5591 }
5592
5593 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5594 /// otherwise will stop at the first successful one.
5595 bool wxRichTextBuffer::SendEvent(wxEvent& event, bool sendToAll)
5596 {
5597 bool success = false;
5598 for (wxList::compatibility_iterator node = m_eventHandlers.GetFirst(); node; node = node->GetNext())
5599 {
5600 wxEvtHandler* handler = (wxEvtHandler*) node->GetData();
5601 if (handler->ProcessEvent(event))
5602 {
5603 success = true;
5604 if (!sendToAll)
5605 return true;
5606 }
5607 }
5608 return success;
5609 }
5610
5611 /// Set style sheet and notify of the change
5612 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet* sheet)
5613 {
5614 wxRichTextStyleSheet* oldSheet = GetStyleSheet();
5615
5616 wxWindowID id = wxID_ANY;
5617 if (GetRichTextCtrl())
5618 id = GetRichTextCtrl()->GetId();
5619
5620 wxRichTextEvent event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, id);
5621 event.SetEventObject(GetRichTextCtrl());
5622 event.SetOldStyleSheet(oldSheet);
5623 event.SetNewStyleSheet(sheet);
5624 event.Allow();
5625
5626 if (SendEvent(event) && !event.IsAllowed())
5627 {
5628 if (sheet != oldSheet)
5629 delete sheet;
5630
5631 return false;
5632 }
5633
5634 if (oldSheet && oldSheet != sheet)
5635 delete oldSheet;
5636
5637 SetStyleSheet(sheet);
5638
5639 event.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED);
5640 event.SetOldStyleSheet(NULL);
5641 event.Allow();
5642
5643 return SendEvent(event);
5644 }
5645
5646 /// Set renderer, deleting old one
5647 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer* renderer)
5648 {
5649 if (sm_renderer)
5650 delete sm_renderer;
5651 sm_renderer = renderer;
5652 }
5653
5654 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& bulletAttr, const wxRect& rect)
5655 {
5656 if (bulletAttr.GetTextColour().Ok())
5657 {
5658 dc.SetPen(wxPen(bulletAttr.GetTextColour()));
5659 dc.SetBrush(wxBrush(bulletAttr.GetTextColour()));
5660 }
5661 else
5662 {
5663 dc.SetPen(*wxBLACK_PEN);
5664 dc.SetBrush(*wxBLACK_BRUSH);
5665 }
5666
5667 wxFont font;
5668 if (bulletAttr.GetFont().Ok())
5669 font = bulletAttr.GetFont();
5670 else
5671 font = (*wxNORMAL_FONT);
5672
5673 dc.SetFont(font);
5674
5675 int charHeight = dc.GetCharHeight();
5676
5677 int bulletWidth = (int) (((float) charHeight) * wxRichTextBuffer::GetBulletProportion());
5678 int bulletHeight = bulletWidth;
5679
5680 int x = rect.x;
5681
5682 // Calculate the top position of the character (as opposed to the whole line height)
5683 int y = rect.y + (rect.height - charHeight);
5684
5685 // Calculate where the bullet should be positioned
5686 y = y + (charHeight+1)/2 - (bulletHeight+1)/2;
5687
5688 // The margin between a bullet and text.
5689 int margin = paragraph->ConvertTenthsMMToPixels(dc, wxRichTextBuffer::GetBulletRightMargin());
5690
5691 if (bulletAttr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT)
5692 x = rect.x + rect.width - bulletWidth - margin;
5693 else if (bulletAttr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE)
5694 x = x + (rect.width)/2 - bulletWidth/2;
5695
5696 if (bulletAttr.GetBulletName() == wxT("standard/square"))
5697 {
5698 dc.DrawRectangle(x, y, bulletWidth, bulletHeight);
5699 }
5700 else if (bulletAttr.GetBulletName() == wxT("standard/diamond"))
5701 {
5702 wxPoint pts[5];
5703 pts[0].x = x; pts[0].y = y + bulletHeight/2;
5704 pts[1].x = x + bulletWidth/2; pts[1].y = y;
5705 pts[2].x = x + bulletWidth; pts[2].y = y + bulletHeight/2;
5706 pts[3].x = x + bulletWidth/2; pts[3].y = y + bulletHeight;
5707
5708 dc.DrawPolygon(4, pts);
5709 }
5710 else if (bulletAttr.GetBulletName() == wxT("standard/triangle"))
5711 {
5712 wxPoint pts[3];
5713 pts[0].x = x; pts[0].y = y;
5714 pts[1].x = x + bulletWidth; pts[1].y = y + bulletHeight/2;
5715 pts[2].x = x; pts[2].y = y + bulletHeight;
5716
5717 dc.DrawPolygon(3, pts);
5718 }
5719 else // "standard/circle", and catch-all
5720 {
5721 dc.DrawEllipse(x, y, bulletWidth, bulletHeight);
5722 }
5723
5724 return true;
5725 }
5726
5727 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect, const wxString& text)
5728 {
5729 if (!text.empty())
5730 {
5731 wxFont font;
5732 if ((attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL) && !attr.GetBulletFont().IsEmpty() && attr.GetFont().Ok())
5733 {
5734 font = (*wxTheFontList->FindOrCreateFont(attr.GetFont().GetPointSize(), attr.GetFont().GetFamily(),
5735 attr.GetFont().GetStyle(), attr.GetFont().GetWeight(), attr.GetFont().GetUnderlined(),
5736 attr.GetBulletFont()));
5737 }
5738 else if (attr.GetFont().Ok())
5739 font = attr.GetFont();
5740 else
5741 font = (*wxNORMAL_FONT);
5742
5743 dc.SetFont(font);
5744
5745 if (attr.GetTextColour().Ok())
5746 dc.SetTextForeground(attr.GetTextColour());
5747
5748 dc.SetBackgroundMode(wxTRANSPARENT);
5749
5750 int charHeight = dc.GetCharHeight();
5751 wxCoord tw, th;
5752 dc.GetTextExtent(text, & tw, & th);
5753
5754 int x = rect.x;
5755
5756 // Calculate the top position of the character (as opposed to the whole line height)
5757 int y = rect.y + (rect.height - charHeight);
5758
5759 // The margin between a bullet and text.
5760 int margin = paragraph->ConvertTenthsMMToPixels(dc, wxRichTextBuffer::GetBulletRightMargin());
5761
5762 if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT)
5763 x = (rect.x + rect.width) - tw - margin;
5764 else if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE)
5765 x = x + (rect.width)/2 - tw/2;
5766
5767 dc.DrawText(text, x, y);
5768
5769 return true;
5770 }
5771 else
5772 return false;
5773 }
5774
5775 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph* WXUNUSED(paragraph), wxDC& WXUNUSED(dc), const wxTextAttrEx& WXUNUSED(attr), const wxRect& WXUNUSED(rect))
5776 {
5777 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5778 // with the buffer. The store will allow retrieval from memory, disk or other means.
5779 return false;
5780 }
5781
5782 /// Enumerate the standard bullet names currently supported
5783 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString& bulletNames)
5784 {
5785 bulletNames.Add(wxT("standard/circle"));
5786 bulletNames.Add(wxT("standard/square"));
5787 bulletNames.Add(wxT("standard/diamond"));
5788 bulletNames.Add(wxT("standard/triangle"));
5789
5790 return true;
5791 }
5792
5793 /*
5794 * Module to initialise and clean up handlers
5795 */
5796
5797 class wxRichTextModule: public wxModule
5798 {
5799 DECLARE_DYNAMIC_CLASS(wxRichTextModule)
5800 public:
5801 wxRichTextModule() {}
5802 bool OnInit()
5803 {
5804 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer);
5805 wxRichTextBuffer::InitStandardHandlers();
5806 wxRichTextParagraph::InitDefaultTabs();
5807 return true;
5808 };
5809 void OnExit()
5810 {
5811 wxRichTextBuffer::CleanUpHandlers();
5812 wxRichTextDecimalToRoman(-1);
5813 wxRichTextParagraph::ClearDefaultTabs();
5814 wxRichTextCtrl::ClearAvailableFontNames();
5815 wxRichTextBuffer::SetRenderer(NULL);
5816 };
5817 };
5818
5819 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule, wxModule)
5820
5821
5822 // If the richtext lib is dynamically loaded after the app has already started
5823 // (such as from wxPython) then the built-in module system will not init this
5824 // module. Provide this function to do it manually.
5825 void wxRichTextModuleInit()
5826 {
5827 wxModule* module = new wxRichTextModule;
5828 module->Init();
5829 wxModule::RegisterModule(module);
5830 }
5831
5832
5833 /*!
5834 * Commands for undo/redo
5835 *
5836 */
5837
5838 wxRichTextCommand::wxRichTextCommand(const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
5839 wxRichTextCtrl* ctrl, bool ignoreFirstTime): wxCommand(true, name)
5840 {
5841 /* wxRichTextAction* action = */ new wxRichTextAction(this, name, id, buffer, ctrl, ignoreFirstTime);
5842 }
5843
5844 wxRichTextCommand::wxRichTextCommand(const wxString& name): wxCommand(true, name)
5845 {
5846 }
5847
5848 wxRichTextCommand::~wxRichTextCommand()
5849 {
5850 ClearActions();
5851 }
5852
5853 void wxRichTextCommand::AddAction(wxRichTextAction* action)
5854 {
5855 if (!m_actions.Member(action))
5856 m_actions.Append(action);
5857 }
5858
5859 bool wxRichTextCommand::Do()
5860 {
5861 for (wxList::compatibility_iterator node = m_actions.GetFirst(); node; node = node->GetNext())
5862 {
5863 wxRichTextAction* action = (wxRichTextAction*) node->GetData();
5864 action->Do();
5865 }
5866
5867 return true;
5868 }
5869
5870 bool wxRichTextCommand::Undo()
5871 {
5872 for (wxList::compatibility_iterator node = m_actions.GetLast(); node; node = node->GetPrevious())
5873 {
5874 wxRichTextAction* action = (wxRichTextAction*) node->GetData();
5875 action->Undo();
5876 }
5877
5878 return true;
5879 }
5880
5881 void wxRichTextCommand::ClearActions()
5882 {
5883 WX_CLEAR_LIST(wxList, m_actions);
5884 }
5885
5886 /*!
5887 * Individual action
5888 *
5889 */
5890
5891 wxRichTextAction::wxRichTextAction(wxRichTextCommand* cmd, const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
5892 wxRichTextCtrl* ctrl, bool ignoreFirstTime)
5893 {
5894 m_buffer = buffer;
5895 m_ignoreThis = ignoreFirstTime;
5896 m_cmdId = id;
5897 m_position = -1;
5898 m_ctrl = ctrl;
5899 m_name = name;
5900 m_newParagraphs.SetDefaultStyle(buffer->GetDefaultStyle());
5901 m_newParagraphs.SetBasicStyle(buffer->GetBasicStyle());
5902 if (cmd)
5903 cmd->AddAction(this);
5904 }
5905
5906 wxRichTextAction::~wxRichTextAction()
5907 {
5908 }
5909
5910 bool wxRichTextAction::Do()
5911 {
5912 m_buffer->Modify(true);
5913
5914 switch (m_cmdId)
5915 {
5916 case wxRICHTEXT_INSERT:
5917 {
5918 m_buffer->InsertFragment(GetPosition(), m_newParagraphs);
5919 m_buffer->UpdateRanges();
5920 m_buffer->Invalidate(GetRange());
5921
5922 long newCaretPosition = GetPosition() + m_newParagraphs.GetRange().GetLength();
5923
5924 // Character position to caret position
5925 newCaretPosition --;
5926
5927 // Don't take into account the last newline
5928 if (m_newParagraphs.GetPartialParagraph())
5929 newCaretPosition --;
5930
5931 newCaretPosition = wxMin(newCaretPosition, (m_buffer->GetRange().GetEnd()-1));
5932
5933 UpdateAppearance(newCaretPosition, true /* send update event */);
5934
5935 break;
5936 }
5937 case wxRICHTEXT_DELETE:
5938 {
5939 m_buffer->DeleteRange(GetRange());
5940 m_buffer->UpdateRanges();
5941 m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5942
5943 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
5944
5945 break;
5946 }
5947 case wxRICHTEXT_CHANGE_STYLE:
5948 {
5949 ApplyParagraphs(GetNewParagraphs());
5950 m_buffer->Invalidate(GetRange());
5951
5952 UpdateAppearance(GetPosition());
5953
5954 break;
5955 }
5956 default:
5957 break;
5958 }
5959
5960 return true;
5961 }
5962
5963 bool wxRichTextAction::Undo()
5964 {
5965 m_buffer->Modify(true);
5966
5967 switch (m_cmdId)
5968 {
5969 case wxRICHTEXT_INSERT:
5970 {
5971 m_buffer->DeleteRange(GetRange());
5972 m_buffer->UpdateRanges();
5973 m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5974
5975 long newCaretPosition = GetPosition() - 1;
5976 // if (m_newParagraphs.GetPartialParagraph())
5977 // newCaretPosition --;
5978
5979 UpdateAppearance(newCaretPosition, true /* send update event */);
5980
5981 break;
5982 }
5983 case wxRICHTEXT_DELETE:
5984 {
5985 m_buffer->InsertFragment(GetRange().GetStart(), m_oldParagraphs);
5986 m_buffer->UpdateRanges();
5987 m_buffer->Invalidate(GetRange());
5988
5989 UpdateAppearance(GetPosition(), true /* send update event */);
5990
5991 break;
5992 }
5993 case wxRICHTEXT_CHANGE_STYLE:
5994 {
5995 ApplyParagraphs(GetOldParagraphs());
5996 m_buffer->Invalidate(GetRange());
5997
5998 UpdateAppearance(GetPosition());
5999
6000 break;
6001 }
6002 default:
6003 break;
6004 }
6005
6006 return true;
6007 }
6008
6009 /// Update the control appearance
6010 void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent)
6011 {
6012 if (m_ctrl)
6013 {
6014 m_ctrl->SetCaretPosition(caretPosition);
6015 if (!m_ctrl->IsFrozen())
6016 {
6017 m_ctrl->LayoutContent();
6018 m_ctrl->PositionCaret();
6019 m_ctrl->Refresh(false);
6020
6021 if (sendUpdateEvent)
6022 m_ctrl->SendTextUpdatedEvent();
6023 }
6024 }
6025 }
6026
6027 /// Replace the buffer paragraphs with the new ones.
6028 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox& fragment)
6029 {
6030 wxRichTextObjectList::compatibility_iterator node = fragment.GetChildren().GetFirst();
6031 while (node)
6032 {
6033 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
6034 wxASSERT (para != NULL);
6035
6036 // We'll replace the existing paragraph by finding the paragraph at this position,
6037 // delete its node data, and setting a copy as the new node data.
6038 // TODO: make more efficient by simply swapping old and new paragraph objects.
6039
6040 wxRichTextParagraph* existingPara = m_buffer->GetParagraphAtPosition(para->GetRange().GetStart());
6041 if (existingPara)
6042 {
6043 wxRichTextObjectList::compatibility_iterator bufferParaNode = m_buffer->GetChildren().Find(existingPara);
6044 if (bufferParaNode)
6045 {
6046 wxRichTextParagraph* newPara = new wxRichTextParagraph(*para);
6047 newPara->SetParent(m_buffer);
6048
6049 bufferParaNode->SetData(newPara);
6050
6051 delete existingPara;
6052 }
6053 }
6054
6055 node = node->GetNext();
6056 }
6057 }
6058
6059
6060 /*!
6061 * wxRichTextRange
6062 * This stores beginning and end positions for a range of data.
6063 */
6064
6065 /// Limit this range to be within 'range'
6066 bool wxRichTextRange::LimitTo(const wxRichTextRange& range)
6067 {
6068 if (m_start < range.m_start)
6069 m_start = range.m_start;
6070
6071 if (m_end > range.m_end)
6072 m_end = range.m_end;
6073
6074 return true;
6075 }
6076
6077 /*!
6078 * wxRichTextImage implementation
6079 * This object represents an image.
6080 */
6081
6082 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage, wxRichTextObject)
6083
6084 wxRichTextImage::wxRichTextImage(const wxImage& image, wxRichTextObject* parent):
6085 wxRichTextObject(parent)
6086 {
6087 m_image = image;
6088 }
6089
6090 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent):
6091 wxRichTextObject(parent)
6092 {
6093 m_imageBlock = imageBlock;
6094 m_imageBlock.Load(m_image);
6095 }
6096
6097 /// Load wxImage from the block
6098 bool wxRichTextImage::LoadFromBlock()
6099 {
6100 m_imageBlock.Load(m_image);
6101 return m_imageBlock.Ok();
6102 }
6103
6104 /// Make block from the wxImage
6105 bool wxRichTextImage::MakeBlock()
6106 {
6107 if (m_imageBlock.GetImageType() == wxBITMAP_TYPE_ANY || m_imageBlock.GetImageType() == -1)
6108 m_imageBlock.SetImageType(wxBITMAP_TYPE_PNG);
6109
6110 m_imageBlock.MakeImageBlock(m_image, m_imageBlock.GetImageType());
6111 return m_imageBlock.Ok();
6112 }
6113
6114
6115 /// Draw the item
6116 bool wxRichTextImage::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int WXUNUSED(descent), int WXUNUSED(style))
6117 {
6118 if (!m_image.Ok() && m_imageBlock.Ok())
6119 LoadFromBlock();
6120
6121 if (!m_image.Ok())
6122 return false;
6123
6124 if (m_image.Ok() && !m_bitmap.Ok())
6125 m_bitmap = wxBitmap(m_image);
6126
6127 int y = rect.y + (rect.height - m_image.GetHeight());
6128
6129 if (m_bitmap.Ok())
6130 dc.DrawBitmap(m_bitmap, rect.x, y, true);
6131
6132 if (selectionRange.Contains(range.GetStart()))
6133 {
6134 dc.SetBrush(*wxBLACK_BRUSH);
6135 dc.SetPen(*wxBLACK_PEN);
6136 dc.SetLogicalFunction(wxINVERT);
6137 dc.DrawRectangle(rect);
6138 dc.SetLogicalFunction(wxCOPY);
6139 }
6140
6141 return true;
6142 }
6143
6144 /// Lay the item out
6145 bool wxRichTextImage::Layout(wxDC& WXUNUSED(dc), const wxRect& rect, int WXUNUSED(style))
6146 {
6147 if (!m_image.Ok())
6148 LoadFromBlock();
6149
6150 if (m_image.Ok())
6151 {
6152 SetCachedSize(wxSize(m_image.GetWidth(), m_image.GetHeight()));
6153 SetPosition(rect.GetPosition());
6154 }
6155
6156 return true;
6157 }
6158
6159 /// Get/set the object size for the given range. Returns false if the range
6160 /// is invalid for this object.
6161 bool wxRichTextImage::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& WXUNUSED(descent), wxDC& WXUNUSED(dc), int WXUNUSED(flags), wxPoint WXUNUSED(position)) const
6162 {
6163 if (!range.IsWithin(GetRange()))
6164 return false;
6165
6166 if (!m_image.Ok())
6167 return false;
6168
6169 size.x = m_image.GetWidth();
6170 size.y = m_image.GetHeight();
6171
6172 return true;
6173 }
6174
6175 /// Copy
6176 void wxRichTextImage::Copy(const wxRichTextImage& obj)
6177 {
6178 wxRichTextObject::Copy(obj);
6179
6180 m_image = obj.m_image;
6181 m_imageBlock = obj.m_imageBlock;
6182 }
6183
6184 /*!
6185 * Utilities
6186 *
6187 */
6188
6189 /// Compare two attribute objects
6190 bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2)
6191 {
6192 return (attr1 == attr2);
6193 }
6194
6195 bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2)
6196 {
6197 return (
6198 attr1.GetTextColour() == attr2.GetTextColour() &&
6199 attr1.GetBackgroundColour() == attr2.GetBackgroundColour() &&
6200 attr1.GetFont().GetPointSize() == attr2.GetFontSize() &&
6201 attr1.GetFont().GetStyle() == attr2.GetFontStyle() &&
6202 attr1.GetFont().GetWeight() == attr2.GetFontWeight() &&
6203 attr1.GetFont().GetFaceName() == attr2.GetFontFaceName() &&
6204 attr1.GetFont().GetUnderlined() == attr2.GetFontUnderlined() &&
6205 attr1.GetAlignment() == attr2.GetAlignment() &&
6206 attr1.GetLeftIndent() == attr2.GetLeftIndent() &&
6207 attr1.GetRightIndent() == attr2.GetRightIndent() &&
6208 attr1.GetLeftSubIndent() == attr2.GetLeftSubIndent() &&
6209 wxRichTextTabsEq(attr1.GetTabs(), attr2.GetTabs()) &&
6210 attr1.GetLineSpacing() == attr2.GetLineSpacing() &&
6211 attr1.GetParagraphSpacingAfter() == attr2.GetParagraphSpacingAfter() &&
6212 attr1.GetParagraphSpacingBefore() == attr2.GetParagraphSpacingBefore() &&
6213 attr1.GetBulletStyle() == attr2.GetBulletStyle() &&
6214 attr1.GetBulletNumber() == attr2.GetBulletNumber() &&
6215 attr1.GetBulletText() == attr2.GetBulletText() &&
6216 attr1.GetBulletName() == attr2.GetBulletName() &&
6217 attr1.GetBulletFont() == attr2.GetBulletFont() &&
6218 attr1.GetCharacterStyleName() == attr2.GetCharacterStyleName() &&
6219 attr1.GetParagraphStyleName() == attr2.GetParagraphStyleName() &&
6220 attr1.GetListStyleName() == attr2.GetListStyleName());
6221 }
6222
6223 /// Compare two attribute objects, but take into account the flags
6224 /// specifying attributes of interest.
6225 bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2, int flags)
6226 {
6227 if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && attr1.GetTextColour() != attr2.GetTextColour())
6228 return false;
6229
6230 if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && attr1.GetBackgroundColour() != attr2.GetBackgroundColour())
6231 return false;
6232
6233 if ((flags & wxTEXT_ATTR_FONT_FACE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6234 attr1.GetFont().GetFaceName() != attr2.GetFont().GetFaceName())
6235 return false;
6236
6237 if ((flags & wxTEXT_ATTR_FONT_SIZE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6238 attr1.GetFont().GetPointSize() != attr2.GetFont().GetPointSize())
6239 return false;
6240
6241 if ((flags & wxTEXT_ATTR_FONT_WEIGHT) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6242 attr1.GetFont().GetWeight() != attr2.GetFont().GetWeight())
6243 return false;
6244
6245 if ((flags & wxTEXT_ATTR_FONT_ITALIC) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6246 attr1.GetFont().GetStyle() != attr2.GetFont().GetStyle())
6247 return false;
6248
6249 if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6250 attr1.GetFont().GetUnderlined() != attr2.GetFont().GetUnderlined())
6251 return false;
6252
6253 if ((flags & wxTEXT_ATTR_ALIGNMENT) && attr1.GetAlignment() != attr2.GetAlignment())
6254 return false;
6255
6256 if ((flags & wxTEXT_ATTR_LEFT_INDENT) &&
6257 ((attr1.GetLeftIndent() != attr2.GetLeftIndent()) || (attr1.GetLeftSubIndent() != attr2.GetLeftSubIndent())))
6258 return false;
6259
6260 if ((flags & wxTEXT_ATTR_RIGHT_INDENT) &&
6261 (attr1.GetRightIndent() != attr2.GetRightIndent()))
6262 return false;
6263
6264 if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) &&
6265 (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter()))
6266 return false;
6267
6268 if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) &&
6269 (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore()))
6270 return false;
6271
6272 if ((flags & wxTEXT_ATTR_LINE_SPACING) &&
6273 (attr1.GetLineSpacing() != attr2.GetLineSpacing()))
6274 return false;
6275
6276 if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) &&
6277 (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName()))
6278 return false;
6279
6280 if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) &&
6281 (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName()))
6282 return false;
6283
6284 if ((flags & wxTEXT_ATTR_LIST_STYLE_NAME) &&
6285 (attr1.GetListStyleName() != attr2.GetListStyleName()))
6286 return false;
6287
6288 if ((flags & wxTEXT_ATTR_BULLET_STYLE) &&
6289 (attr1.GetBulletStyle() != attr2.GetBulletStyle()))
6290 return false;
6291
6292 if ((flags & wxTEXT_ATTR_BULLET_NUMBER) &&
6293 (attr1.GetBulletNumber() != attr2.GetBulletNumber()))
6294 return false;
6295
6296 if ((flags & wxTEXT_ATTR_BULLET_TEXT) &&
6297 (attr1.GetBulletText() != attr2.GetBulletText()) &&
6298 (attr1.GetBulletFont() != attr2.GetBulletFont()))
6299 return false;
6300
6301 if ((flags & wxTEXT_ATTR_BULLET_NAME) &&
6302 (attr1.GetBulletName() != attr2.GetBulletName()))
6303 return false;
6304
6305 if ((flags & wxTEXT_ATTR_TABS) &&
6306 !wxRichTextTabsEq(attr1.GetTabs(), attr2.GetTabs()))
6307 return false;
6308
6309 return true;
6310 }
6311
6312 bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2, int flags)
6313 {
6314 if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && attr1.GetTextColour() != attr2.GetTextColour())
6315 return false;
6316
6317 if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && attr1.GetBackgroundColour() != attr2.GetBackgroundColour())
6318 return false;
6319
6320 if ((flags & (wxTEXT_ATTR_FONT)) && !attr1.GetFont().Ok())
6321 return false;
6322
6323 if ((flags & wxTEXT_ATTR_FONT_FACE) && attr1.GetFont().Ok() &&
6324 attr1.GetFont().GetFaceName() != attr2.GetFontFaceName())
6325 return false;
6326
6327 if ((flags & wxTEXT_ATTR_FONT_SIZE) && attr1.GetFont().Ok() &&
6328 attr1.GetFont().GetPointSize() != attr2.GetFontSize())
6329 return false;
6330
6331 if ((flags & wxTEXT_ATTR_FONT_WEIGHT) && attr1.GetFont().Ok() &&
6332 attr1.GetFont().GetWeight() != attr2.GetFontWeight())
6333 return false;
6334
6335 if ((flags & wxTEXT_ATTR_FONT_ITALIC) && attr1.GetFont().Ok() &&
6336 attr1.GetFont().GetStyle() != attr2.GetFontStyle())
6337 return false;
6338
6339 if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) && attr1.GetFont().Ok() &&
6340 attr1.GetFont().GetUnderlined() != attr2.GetFontUnderlined())
6341 return false;
6342
6343 if ((flags & wxTEXT_ATTR_ALIGNMENT) && attr1.GetAlignment() != attr2.GetAlignment())
6344 return false;
6345
6346 if ((flags & wxTEXT_ATTR_LEFT_INDENT) &&
6347 ((attr1.GetLeftIndent() != attr2.GetLeftIndent()) || (attr1.GetLeftSubIndent() != attr2.GetLeftSubIndent())))
6348 return false;
6349
6350 if ((flags & wxTEXT_ATTR_RIGHT_INDENT) &&
6351 (attr1.GetRightIndent() != attr2.GetRightIndent()))
6352 return false;
6353
6354 if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) &&
6355 (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter()))
6356 return false;
6357
6358 if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) &&
6359 (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore()))
6360 return false;
6361
6362 if ((flags & wxTEXT_ATTR_LINE_SPACING) &&
6363 (attr1.GetLineSpacing() != attr2.GetLineSpacing()))
6364 return false;
6365
6366 if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) &&
6367 (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName()))
6368 return false;
6369
6370 if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) &&
6371 (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName()))
6372 return false;
6373
6374 if ((flags & wxTEXT_ATTR_LIST_STYLE_NAME) &&
6375 (attr1.GetListStyleName() != attr2.GetListStyleName()))
6376 return false;
6377
6378 if ((flags & wxTEXT_ATTR_BULLET_STYLE) &&
6379 (attr1.GetBulletStyle() != attr2.GetBulletStyle()))
6380 return false;
6381
6382 if ((flags & wxTEXT_ATTR_BULLET_NUMBER) &&
6383 (attr1.GetBulletNumber() != attr2.GetBulletNumber()))
6384 return false;
6385
6386 if ((flags & wxTEXT_ATTR_BULLET_TEXT) &&
6387 (attr1.GetBulletText() != attr2.GetBulletText()) &&
6388 (attr1.GetBulletFont() != attr2.GetBulletFont()))
6389 return false;
6390
6391 if ((flags & wxTEXT_ATTR_BULLET_NAME) &&
6392 (attr1.GetBulletName() != attr2.GetBulletName()))
6393 return false;
6394
6395 if ((flags & wxTEXT_ATTR_TABS) &&
6396 !wxRichTextTabsEq(attr1.GetTabs(), attr2.GetTabs()))
6397 return false;
6398
6399 return true;
6400 }
6401
6402 /// Compare tabs
6403 bool wxRichTextTabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
6404 {
6405 if (tabs1.GetCount() != tabs2.GetCount())
6406 return false;
6407
6408 size_t i;
6409 for (i = 0; i < tabs1.GetCount(); i++)
6410 {
6411 if (tabs1[i] != tabs2[i])
6412 return false;
6413 }
6414 return true;
6415 }
6416
6417
6418 /// Apply one style to another
6419 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxTextAttrEx& style)
6420 {
6421 // Whole font
6422 if (style.GetFont().Ok() && ((style.GetFlags() & (wxTEXT_ATTR_FONT)) == (wxTEXT_ATTR_FONT)))
6423 destStyle.SetFont(style.GetFont());
6424 else if (style.GetFont().Ok())
6425 {
6426 wxFont font = destStyle.GetFont();
6427
6428 if (style.GetFlags() & wxTEXT_ATTR_FONT_FACE)
6429 {
6430 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_FACE);
6431 font.SetFaceName(style.GetFont().GetFaceName());
6432 }
6433
6434 if (style.GetFlags() & wxTEXT_ATTR_FONT_SIZE)
6435 {
6436 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_SIZE);
6437 font.SetPointSize(style.GetFont().GetPointSize());
6438 }
6439
6440 if (style.GetFlags() & wxTEXT_ATTR_FONT_ITALIC)
6441 {
6442 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_ITALIC);
6443 font.SetStyle(style.GetFont().GetStyle());
6444 }
6445
6446 if (style.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT)
6447 {
6448 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT);
6449 font.SetWeight(style.GetFont().GetWeight());
6450 }
6451
6452 if (style.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE)
6453 {
6454 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE);
6455 font.SetUnderlined(style.GetFont().GetUnderlined());
6456 }
6457
6458 if (font != destStyle.GetFont())
6459 {
6460 int oldFlags = destStyle.GetFlags();
6461
6462 destStyle.SetFont(font);
6463
6464 destStyle.SetFlags(oldFlags);
6465 }
6466 }
6467
6468 if ( style.GetTextColour().Ok() && style.HasTextColour())
6469 destStyle.SetTextColour(style.GetTextColour());
6470
6471 if ( style.GetBackgroundColour().Ok() && style.HasBackgroundColour())
6472 destStyle.SetBackgroundColour(style.GetBackgroundColour());
6473
6474 if (style.HasAlignment())
6475 destStyle.SetAlignment(style.GetAlignment());
6476
6477 if (style.HasTabs())
6478 destStyle.SetTabs(style.GetTabs());
6479
6480 if (style.HasLeftIndent())
6481 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
6482
6483 if (style.HasRightIndent())
6484 destStyle.SetRightIndent(style.GetRightIndent());
6485
6486 if (style.HasParagraphSpacingAfter())
6487 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
6488
6489 if (style.HasParagraphSpacingBefore())
6490 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
6491
6492 if (style.HasLineSpacing())
6493 destStyle.SetLineSpacing(style.GetLineSpacing());
6494
6495 if (style.HasCharacterStyleName())
6496 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
6497
6498 if (style.HasParagraphStyleName())
6499 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
6500
6501 if (style.HasListStyleName())
6502 destStyle.SetListStyleName(style.GetListStyleName());
6503
6504 if (style.HasBulletStyle())
6505 destStyle.SetBulletStyle(style.GetBulletStyle());
6506
6507 if (style.HasBulletText())
6508 {
6509 destStyle.SetBulletText(style.GetBulletText());
6510 destStyle.SetBulletFont(style.GetBulletFont());
6511 }
6512
6513 if (style.HasBulletName())
6514 destStyle.SetBulletName(style.GetBulletName());
6515
6516 if (style.HasBulletNumber())
6517 destStyle.SetBulletNumber(style.GetBulletNumber());
6518
6519 if (style.HasURL())
6520 destStyle.SetURL(style.GetURL());
6521
6522 return true;
6523 }
6524
6525 bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxTextAttrEx& style)
6526 {
6527 wxTextAttrEx destStyle2;
6528 destStyle.CopyTo(destStyle2);
6529 wxRichTextApplyStyle(destStyle2, style);
6530 destStyle = destStyle2;
6531 return true;
6532 }
6533
6534 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style, wxRichTextAttr* compareWith)
6535 {
6536 // Whole font. Avoiding setting individual attributes if possible, since
6537 // it recreates the font each time.
6538 if (((style.GetFlags() & (wxTEXT_ATTR_FONT)) == (wxTEXT_ATTR_FONT)) && !compareWith)
6539 {
6540 destStyle.SetFont(wxFont(style.GetFontSize(), destStyle.GetFont().Ok() ? destStyle.GetFont().GetFamily() : wxDEFAULT,
6541 style.GetFontStyle(), style.GetFontWeight(), style.GetFontUnderlined(), style.GetFontFaceName()));
6542 }
6543 else if (style.GetFlags() & (wxTEXT_ATTR_FONT))
6544 {
6545 wxFont font = destStyle.GetFont();
6546
6547 if (style.GetFlags() & wxTEXT_ATTR_FONT_FACE)
6548 {
6549 if (compareWith && compareWith->HasFaceName() && compareWith->GetFontFaceName() == style.GetFontFaceName())
6550 {
6551 // The same as currently displayed, so don't set
6552 }
6553 else
6554 {
6555 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_FACE);
6556 font.SetFaceName(style.GetFontFaceName());
6557 }
6558 }
6559
6560 if (style.GetFlags() & wxTEXT_ATTR_FONT_SIZE)
6561 {
6562 if (compareWith && compareWith->HasSize() && compareWith->GetFontSize() == style.GetFontSize())
6563 {
6564 // The same as currently displayed, so don't set
6565 }
6566 else
6567 {
6568 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_SIZE);
6569 font.SetPointSize(style.GetFontSize());
6570 }
6571 }
6572
6573 if (style.GetFlags() & wxTEXT_ATTR_FONT_ITALIC)
6574 {
6575 if (compareWith && compareWith->HasItalic() && compareWith->GetFontStyle() == style.GetFontStyle())
6576 {
6577 // The same as currently displayed, so don't set
6578 }
6579 else
6580 {
6581 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_ITALIC);
6582 font.SetStyle(style.GetFontStyle());
6583 }
6584 }
6585
6586 if (style.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT)
6587 {
6588 if (compareWith && compareWith->HasWeight() && compareWith->GetFontWeight() == style.GetFontWeight())
6589 {
6590 // The same as currently displayed, so don't set
6591 }
6592 else
6593 {
6594 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT);
6595 font.SetWeight(style.GetFontWeight());
6596 }
6597 }
6598
6599 if (style.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE)
6600 {
6601 if (compareWith && compareWith->HasUnderlined() && compareWith->GetFontUnderlined() == style.GetFontUnderlined())
6602 {
6603 // The same as currently displayed, so don't set
6604 }
6605 else
6606 {
6607 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE);
6608 font.SetUnderlined(style.GetFontUnderlined());
6609 }
6610 }
6611
6612 if (font != destStyle.GetFont())
6613 {
6614 int oldFlags = destStyle.GetFlags();
6615
6616 destStyle.SetFont(font);
6617
6618 destStyle.SetFlags(oldFlags);
6619 }
6620 }
6621
6622 if (style.GetTextColour().Ok() && style.HasTextColour())
6623 {
6624 if (!(compareWith && compareWith->HasTextColour() && compareWith->GetTextColour() == style.GetTextColour()))
6625 destStyle.SetTextColour(style.GetTextColour());
6626 }
6627
6628 if (style.GetBackgroundColour().Ok() && style.HasBackgroundColour())
6629 {
6630 if (!(compareWith && compareWith->HasBackgroundColour() && compareWith->GetBackgroundColour() == style.GetBackgroundColour()))
6631 destStyle.SetBackgroundColour(style.GetBackgroundColour());
6632 }
6633
6634 if (style.HasAlignment())
6635 {
6636 if (!(compareWith && compareWith->HasAlignment() && compareWith->GetAlignment() == style.GetAlignment()))
6637 destStyle.SetAlignment(style.GetAlignment());
6638 }
6639
6640 if (style.HasTabs())
6641 {
6642 if (!(compareWith && compareWith->HasTabs() && wxRichTextTabsEq(compareWith->GetTabs(), style.GetTabs())))
6643 destStyle.SetTabs(style.GetTabs());
6644 }
6645
6646 if (style.HasLeftIndent())
6647 {
6648 if (!(compareWith && compareWith->HasLeftIndent() && compareWith->GetLeftIndent() == style.GetLeftIndent()
6649 && compareWith->GetLeftSubIndent() == style.GetLeftSubIndent()))
6650 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
6651 }
6652
6653 if (style.HasRightIndent())
6654 {
6655 if (!(compareWith && compareWith->HasRightIndent() && compareWith->GetRightIndent() == style.GetRightIndent()))
6656 destStyle.SetRightIndent(style.GetRightIndent());
6657 }
6658
6659 if (style.HasParagraphSpacingAfter())
6660 {
6661 if (!(compareWith && compareWith->HasParagraphSpacingAfter() && compareWith->GetParagraphSpacingAfter() == style.GetParagraphSpacingAfter()))
6662 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
6663 }
6664
6665 if (style.HasParagraphSpacingBefore())
6666 {
6667 if (!(compareWith && compareWith->HasParagraphSpacingBefore() && compareWith->GetParagraphSpacingBefore() == style.GetParagraphSpacingBefore()))
6668 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
6669 }
6670
6671 if (style.HasLineSpacing())
6672 {
6673 if (!(compareWith && compareWith->HasLineSpacing() && compareWith->GetLineSpacing() == style.GetLineSpacing()))
6674 destStyle.SetLineSpacing(style.GetLineSpacing());
6675 }
6676
6677 if (style.HasCharacterStyleName())
6678 {
6679 if (!(compareWith && compareWith->HasCharacterStyleName() && compareWith->GetCharacterStyleName() == style.GetCharacterStyleName()))
6680 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
6681 }
6682
6683 if (style.HasParagraphStyleName())
6684 {
6685 if (!(compareWith && compareWith->HasParagraphStyleName() && compareWith->GetParagraphStyleName() == style.GetParagraphStyleName()))
6686 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
6687 }
6688
6689 if (style.HasListStyleName())
6690 {
6691 if (!(compareWith && compareWith->HasListStyleName() && compareWith->GetListStyleName() == style.GetListStyleName()))
6692 destStyle.SetListStyleName(style.GetListStyleName());
6693 }
6694
6695 if (style.HasBulletStyle())
6696 {
6697 if (!(compareWith && compareWith->HasBulletStyle() && compareWith->GetBulletStyle() == style.GetBulletStyle()))
6698 destStyle.SetBulletStyle(style.GetBulletStyle());
6699 }
6700
6701 if (style.HasBulletText())
6702 {
6703 if (!(compareWith && compareWith->HasBulletText() && compareWith->GetBulletText() == style.GetBulletText()))
6704 {
6705 destStyle.SetBulletText(style.GetBulletText());
6706 destStyle.SetBulletFont(style.GetBulletFont());
6707 }
6708 }
6709
6710 if (style.HasBulletNumber())
6711 {
6712 if (!(compareWith && compareWith->HasBulletNumber() && compareWith->GetBulletNumber() == style.GetBulletNumber()))
6713 destStyle.SetBulletNumber(style.GetBulletNumber());
6714 }
6715
6716 if (style.HasBulletName())
6717 {
6718 if (!(compareWith && compareWith->HasBulletName() && compareWith->GetBulletName() == style.GetBulletName()))
6719 destStyle.SetBulletName(style.GetBulletName());
6720 }
6721
6722 if (style.HasURL())
6723 {
6724 if (!(compareWith && compareWith->HasURL() && compareWith->GetURL() == style.GetURL()))
6725 destStyle.SetURL(style.GetURL());
6726 }
6727
6728 return true;
6729 }
6730
6731 void wxSetFontPreservingStyles(wxTextAttr& attr, const wxFont& font)
6732 {
6733 long flags = attr.GetFlags();
6734 attr.SetFont(font);
6735 attr.SetFlags(flags);
6736 }
6737
6738 /// Convert a decimal to Roman numerals
6739 wxString wxRichTextDecimalToRoman(long n)
6740 {
6741 static wxArrayInt decimalNumbers;
6742 static wxArrayString romanNumbers;
6743
6744 // Clean up arrays
6745 if (n == -1)
6746 {
6747 decimalNumbers.Clear();
6748 romanNumbers.Clear();
6749 return wxEmptyString;
6750 }
6751
6752 if (decimalNumbers.GetCount() == 0)
6753 {
6754 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6755
6756 wxRichTextAddDecRom(1000, wxT("M"));
6757 wxRichTextAddDecRom(900, wxT("CM"));
6758 wxRichTextAddDecRom(500, wxT("D"));
6759 wxRichTextAddDecRom(400, wxT("CD"));
6760 wxRichTextAddDecRom(100, wxT("C"));
6761 wxRichTextAddDecRom(90, wxT("XC"));
6762 wxRichTextAddDecRom(50, wxT("L"));
6763 wxRichTextAddDecRom(40, wxT("XL"));
6764 wxRichTextAddDecRom(10, wxT("X"));
6765 wxRichTextAddDecRom(9, wxT("IX"));
6766 wxRichTextAddDecRom(5, wxT("V"));
6767 wxRichTextAddDecRom(4, wxT("IV"));
6768 wxRichTextAddDecRom(1, wxT("I"));
6769 }
6770
6771 int i = 0;
6772 wxString roman;
6773
6774 while (n > 0 && i < 13)
6775 {
6776 if (n >= decimalNumbers[i])
6777 {
6778 n -= decimalNumbers[i];
6779 roman += romanNumbers[i];
6780 }
6781 else
6782 {
6783 i ++;
6784 }
6785 }
6786 if (roman.IsEmpty())
6787 roman = wxT("0");
6788 return roman;
6789 }
6790
6791 /*!
6792 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
6793 * efficient way to query styles.
6794 */
6795
6796 // ctors
6797 wxRichTextAttr::wxRichTextAttr(const wxColour& colText,
6798 const wxColour& colBack,
6799 wxTextAttrAlignment alignment): m_textAlignment(alignment), m_colText(colText), m_colBack(colBack)
6800 {
6801 Init();
6802
6803 if (m_colText.Ok()) m_flags |= wxTEXT_ATTR_TEXT_COLOUR;
6804 if (m_colBack.Ok()) m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR;
6805 if (alignment != wxTEXT_ALIGNMENT_DEFAULT)
6806 m_flags |= wxTEXT_ATTR_ALIGNMENT;
6807 }
6808
6809 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx& attr)
6810 {
6811 Init();
6812
6813 (*this) = attr;
6814 }
6815
6816 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr& attr)
6817 {
6818 Copy(attr);
6819 }
6820
6821 // operations
6822 void wxRichTextAttr::Init()
6823 {
6824 m_textAlignment = wxTEXT_ALIGNMENT_DEFAULT;
6825 m_flags = 0;
6826 m_leftIndent = 0;
6827 m_leftSubIndent = 0;
6828 m_rightIndent = 0;
6829
6830 m_fontSize = 12;
6831 m_fontStyle = wxNORMAL;
6832 m_fontWeight = wxNORMAL;
6833 m_fontUnderlined = false;
6834
6835 m_paragraphSpacingAfter = 0;
6836 m_paragraphSpacingBefore = 0;
6837 m_lineSpacing = 0;
6838 m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
6839 m_bulletNumber = 0;
6840 }
6841
6842 // Copy
6843 void wxRichTextAttr::Copy(const wxRichTextAttr& attr)
6844 {
6845 m_colText = attr.m_colText;
6846 m_colBack = attr.m_colBack;
6847 m_textAlignment = attr.m_textAlignment;
6848 m_leftIndent = attr.m_leftIndent;
6849 m_leftSubIndent = attr.m_leftSubIndent;
6850 m_rightIndent = attr.m_rightIndent;
6851 m_tabs = attr.m_tabs;
6852 m_flags = attr.m_flags;
6853
6854 m_fontSize = attr.m_fontSize;
6855 m_fontStyle = attr.m_fontStyle;
6856 m_fontWeight = attr.m_fontWeight;
6857 m_fontUnderlined = attr.m_fontUnderlined;
6858 m_fontFaceName = attr.m_fontFaceName;
6859
6860 m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
6861 m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
6862 m_lineSpacing = attr.m_lineSpacing;
6863 m_characterStyleName = attr.m_characterStyleName;
6864 m_paragraphStyleName = attr.m_paragraphStyleName;
6865 m_listStyleName = attr.m_listStyleName;
6866 m_bulletStyle = attr.m_bulletStyle;
6867 m_bulletNumber = attr.m_bulletNumber;
6868 m_bulletText = attr.m_bulletText;
6869 m_bulletFont = attr.m_bulletFont;
6870 m_bulletName = attr.m_bulletName;
6871
6872 m_urlTarget = attr.m_urlTarget;
6873 }
6874
6875 // operators
6876 void wxRichTextAttr::operator= (const wxRichTextAttr& attr)
6877 {
6878 Copy(attr);
6879 }
6880
6881 // operators
6882 void wxRichTextAttr::operator= (const wxTextAttrEx& attr)
6883 {
6884 m_colText = attr.GetTextColour();
6885 m_colBack = attr.GetBackgroundColour();
6886 m_textAlignment = attr.GetAlignment();
6887 m_leftIndent = attr.GetLeftIndent();
6888 m_leftSubIndent = attr.GetLeftSubIndent();
6889 m_rightIndent = attr.GetRightIndent();
6890 m_tabs = attr.GetTabs();
6891 m_flags = attr.GetFlags();
6892
6893 m_paragraphSpacingAfter = attr.GetParagraphSpacingAfter();
6894 m_paragraphSpacingBefore = attr.GetParagraphSpacingBefore();
6895 m_lineSpacing = attr.GetLineSpacing();
6896 m_characterStyleName = attr.GetCharacterStyleName();
6897 m_paragraphStyleName = attr.GetParagraphStyleName();
6898 m_listStyleName = attr.GetListStyleName();
6899 m_bulletStyle = attr.GetBulletStyle();
6900 m_bulletNumber = attr.GetBulletNumber();
6901 m_bulletText = attr.GetBulletText();
6902 m_bulletName = attr.GetBulletName();
6903 m_bulletFont = attr.GetBulletFont();
6904
6905 m_urlTarget = attr.GetURL();
6906
6907 if (attr.GetFont().Ok())
6908 GetFontAttributes(attr.GetFont());
6909 }
6910
6911 // Making a wxTextAttrEx object.
6912 wxRichTextAttr::operator wxTextAttrEx () const
6913 {
6914 wxTextAttrEx attr;
6915 CopyTo(attr);
6916 return attr;
6917 }
6918
6919 // Equality test
6920 bool wxRichTextAttr::operator== (const wxRichTextAttr& attr) const
6921 {
6922 return GetFlags() == attr.GetFlags() &&
6923
6924 GetTextColour() == attr.GetTextColour() &&
6925 GetBackgroundColour() == attr.GetBackgroundColour() &&
6926
6927 GetAlignment() == attr.GetAlignment() &&
6928 GetLeftIndent() == attr.GetLeftIndent() &&
6929 GetLeftSubIndent() == attr.GetLeftSubIndent() &&
6930 GetRightIndent() == attr.GetRightIndent() &&
6931 wxRichTextTabsEq(GetTabs(), attr.GetTabs()) &&
6932
6933 GetParagraphSpacingAfter() == attr.GetParagraphSpacingAfter() &&
6934 GetParagraphSpacingBefore() == attr.GetParagraphSpacingBefore() &&
6935 GetLineSpacing() == attr.GetLineSpacing() &&
6936 GetCharacterStyleName() == attr.GetCharacterStyleName() &&
6937 GetParagraphStyleName() == attr.GetParagraphStyleName() &&
6938 GetListStyleName() == attr.GetListStyleName() &&
6939
6940 GetBulletStyle() == attr.GetBulletStyle() &&
6941 GetBulletText() == attr.GetBulletText() &&
6942 GetBulletNumber() == attr.GetBulletNumber() &&
6943 GetBulletFont() == attr.GetBulletFont() &&
6944 GetBulletName() == attr.GetBulletName() &&
6945
6946 m_fontSize == attr.m_fontSize &&
6947 m_fontStyle == attr.m_fontStyle &&
6948 m_fontWeight == attr.m_fontWeight &&
6949 m_fontUnderlined == attr.m_fontUnderlined &&
6950 m_fontFaceName == attr.m_fontFaceName &&
6951
6952 m_urlTarget == attr.m_urlTarget;
6953 }
6954
6955 // Copy to a wxTextAttr
6956 void wxRichTextAttr::CopyTo(wxTextAttrEx& attr) const
6957 {
6958 attr.SetTextColour(GetTextColour());
6959 attr.SetBackgroundColour(GetBackgroundColour());
6960 attr.SetAlignment(GetAlignment());
6961 attr.SetTabs(GetTabs());
6962 attr.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
6963 attr.SetRightIndent(GetRightIndent());
6964 attr.SetFont(CreateFont());
6965
6966 attr.SetParagraphSpacingAfter(m_paragraphSpacingAfter);
6967 attr.SetParagraphSpacingBefore(m_paragraphSpacingBefore);
6968 attr.SetLineSpacing(m_lineSpacing);
6969 attr.SetBulletStyle(m_bulletStyle);
6970 attr.SetBulletNumber(m_bulletNumber);
6971 attr.SetBulletText(m_bulletText);
6972 attr.SetBulletName(m_bulletName);
6973 attr.SetBulletFont(m_bulletFont);
6974 attr.SetCharacterStyleName(m_characterStyleName);
6975 attr.SetParagraphStyleName(m_paragraphStyleName);
6976 attr.SetListStyleName(m_listStyleName);
6977
6978 attr.SetURL(m_urlTarget);
6979
6980 attr.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
6981 }
6982
6983 // Create font from font attributes.
6984 wxFont wxRichTextAttr::CreateFont() const
6985 {
6986 wxFont font(m_fontSize, wxDEFAULT, m_fontStyle, m_fontWeight, m_fontUnderlined, m_fontFaceName);
6987 #ifdef __WXMAC__
6988 font.SetNoAntiAliasing(true);
6989 #endif
6990 return font;
6991 }
6992
6993 // Get attributes from font.
6994 bool wxRichTextAttr::GetFontAttributes(const wxFont& font)
6995 {
6996 if (!font.Ok())
6997 return false;
6998
6999 m_fontSize = font.GetPointSize();
7000 m_fontStyle = font.GetStyle();
7001 m_fontWeight = font.GetWeight();
7002 m_fontUnderlined = font.GetUnderlined();
7003 m_fontFaceName = font.GetFaceName();
7004
7005 return true;
7006 }
7007
7008 wxRichTextAttr wxRichTextAttr::Combine(const wxRichTextAttr& attr,
7009 const wxRichTextAttr& attrDef,
7010 const wxTextCtrlBase *text)
7011 {
7012 wxColour colFg = attr.GetTextColour();
7013 if ( !colFg.Ok() )
7014 {
7015 colFg = attrDef.GetTextColour();
7016
7017 if ( text && !colFg.Ok() )
7018 colFg = text->GetForegroundColour();
7019 }
7020
7021 wxColour colBg = attr.GetBackgroundColour();
7022 if ( !colBg.Ok() )
7023 {
7024 colBg = attrDef.GetBackgroundColour();
7025
7026 if ( text && !colBg.Ok() )
7027 colBg = text->GetBackgroundColour();
7028 }
7029
7030 wxRichTextAttr newAttr(colFg, colBg);
7031
7032 if (attr.HasWeight())
7033 newAttr.SetFontWeight(attr.GetFontWeight());
7034
7035 if (attr.HasSize())
7036 newAttr.SetFontSize(attr.GetFontSize());
7037
7038 if (attr.HasItalic())
7039 newAttr.SetFontStyle(attr.GetFontStyle());
7040
7041 if (attr.HasUnderlined())
7042 newAttr.SetFontUnderlined(attr.GetFontUnderlined());
7043
7044 if (attr.HasFaceName())
7045 newAttr.SetFontFaceName(attr.GetFontFaceName());
7046
7047 if (attr.HasAlignment())
7048 newAttr.SetAlignment(attr.GetAlignment());
7049 else if (attrDef.HasAlignment())
7050 newAttr.SetAlignment(attrDef.GetAlignment());
7051
7052 if (attr.HasTabs())
7053 newAttr.SetTabs(attr.GetTabs());
7054 else if (attrDef.HasTabs())
7055 newAttr.SetTabs(attrDef.GetTabs());
7056
7057 if (attr.HasLeftIndent())
7058 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
7059 else if (attrDef.HasLeftIndent())
7060 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
7061
7062 if (attr.HasRightIndent())
7063 newAttr.SetRightIndent(attr.GetRightIndent());
7064 else if (attrDef.HasRightIndent())
7065 newAttr.SetRightIndent(attrDef.GetRightIndent());
7066
7067 // NEW ATTRIBUTES
7068
7069 if (attr.HasParagraphSpacingAfter())
7070 newAttr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter());
7071
7072 if (attr.HasParagraphSpacingBefore())
7073 newAttr.SetParagraphSpacingBefore(attr.GetParagraphSpacingBefore());
7074
7075 if (attr.HasLineSpacing())
7076 newAttr.SetLineSpacing(attr.GetLineSpacing());
7077
7078 if (attr.HasCharacterStyleName())
7079 newAttr.SetCharacterStyleName(attr.GetCharacterStyleName());
7080
7081 if (attr.HasParagraphStyleName())
7082 newAttr.SetParagraphStyleName(attr.GetParagraphStyleName());
7083
7084 if (attr.HasListStyleName())
7085 newAttr.SetListStyleName(attr.GetListStyleName());
7086
7087 if (attr.HasBulletStyle())
7088 newAttr.SetBulletStyle(attr.GetBulletStyle());
7089
7090 if (attr.HasBulletNumber())
7091 newAttr.SetBulletNumber(attr.GetBulletNumber());
7092
7093 if (attr.HasBulletName())
7094 newAttr.SetBulletName(attr.GetBulletName());
7095
7096 if (attr.HasBulletText())
7097 {
7098 newAttr.SetBulletText(attr.GetBulletText());
7099 newAttr.SetBulletFont(attr.GetBulletFont());
7100 }
7101
7102 if (attr.HasURL())
7103 newAttr.SetURL(attr.GetURL());
7104
7105 return newAttr;
7106 }
7107
7108 /*!
7109 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7110 */
7111
7112 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx& attr)
7113 {
7114 Copy(attr);
7115 }
7116
7117 // Initialise this object.
7118 void wxTextAttrEx::Init()
7119 {
7120 m_paragraphSpacingAfter = 0;
7121 m_paragraphSpacingBefore = 0;
7122 m_lineSpacing = 0;
7123 m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
7124 m_bulletNumber = 0;
7125 }
7126
7127 // Copy
7128 void wxTextAttrEx::Copy(const wxTextAttrEx& attr)
7129 {
7130 wxTextAttr::operator= (attr);
7131
7132 m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
7133 m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
7134 m_lineSpacing = attr.m_lineSpacing;
7135 m_characterStyleName = attr.m_characterStyleName;
7136 m_paragraphStyleName = attr.m_paragraphStyleName;
7137 m_listStyleName = attr.m_listStyleName;
7138 m_bulletStyle = attr.m_bulletStyle;
7139 m_bulletNumber = attr.m_bulletNumber;
7140 m_bulletText = attr.m_bulletText;
7141 m_bulletFont = attr.m_bulletFont;
7142 m_bulletName = attr.m_bulletName;
7143 m_urlTarget = attr.m_urlTarget;
7144 }
7145
7146 // Assignment from a wxTextAttrEx object
7147 void wxTextAttrEx::operator= (const wxTextAttrEx& attr)
7148 {
7149 Copy(attr);
7150 }
7151
7152 // Assignment from a wxTextAttr object.
7153 void wxTextAttrEx::operator= (const wxTextAttr& attr)
7154 {
7155 wxTextAttr::operator= (attr);
7156 }
7157
7158 // Equality test
7159 bool wxTextAttrEx::operator== (const wxTextAttrEx& attr) const
7160 {
7161 return (
7162 GetTextColour() == attr.GetTextColour() &&
7163 GetBackgroundColour() == attr.GetBackgroundColour() &&
7164 GetFont() == attr.GetFont() &&
7165 GetAlignment() == attr.GetAlignment() &&
7166 GetLeftIndent() == attr.GetLeftIndent() &&
7167 GetRightIndent() == attr.GetRightIndent() &&
7168 GetLeftSubIndent() == attr.GetLeftSubIndent() &&
7169 wxRichTextTabsEq(GetTabs(), attr.GetTabs()) &&
7170 GetLineSpacing() == attr.GetLineSpacing() &&
7171 GetParagraphSpacingAfter() == attr.GetParagraphSpacingAfter() &&
7172 GetParagraphSpacingBefore() == attr.GetParagraphSpacingBefore() &&
7173 GetBulletStyle() == attr.GetBulletStyle() &&
7174 GetBulletNumber() == attr.GetBulletNumber() &&
7175 GetBulletText() == attr.GetBulletText() &&
7176 GetBulletName() == attr.GetBulletName() &&
7177 GetBulletFont() == attr.GetBulletFont() &&
7178 GetCharacterStyleName() == attr.GetCharacterStyleName() &&
7179 GetParagraphStyleName() == attr.GetParagraphStyleName() &&
7180 GetListStyleName() == attr.GetListStyleName() &&
7181 GetURL() == attr.GetURL());
7182 }
7183
7184 wxTextAttrEx wxTextAttrEx::CombineEx(const wxTextAttrEx& attr,
7185 const wxTextAttrEx& attrDef,
7186 const wxTextCtrlBase *text)
7187 {
7188 wxTextAttrEx newAttr;
7189
7190 // If attr specifies the complete font, just use that font, overriding all
7191 // default font attributes.
7192 if ((attr.GetFlags() & wxTEXT_ATTR_FONT) == wxTEXT_ATTR_FONT)
7193 newAttr.SetFont(attr.GetFont());
7194 else
7195 {
7196 // First find the basic, default font
7197 long flags = 0;
7198
7199 wxFont font;
7200 if (attrDef.HasFont())
7201 {
7202 flags = (attrDef.GetFlags() & wxTEXT_ATTR_FONT);
7203 font = attrDef.GetFont();
7204 }
7205 else
7206 {
7207 if (text)
7208 font = text->GetFont();
7209
7210 // We leave flags at 0 because no font attributes have been specified yet
7211 }
7212 if (!font.Ok())
7213 font = *wxNORMAL_FONT;
7214
7215 // Otherwise, if there are font attributes in attr, apply them
7216 if (attr.GetFlags() & wxTEXT_ATTR_FONT)
7217 {
7218 if (attr.HasSize())
7219 {
7220 flags |= wxTEXT_ATTR_FONT_SIZE;
7221 font.SetPointSize(attr.GetFont().GetPointSize());
7222 }
7223 if (attr.HasItalic())
7224 {
7225 flags |= wxTEXT_ATTR_FONT_ITALIC;;
7226 font.SetStyle(attr.GetFont().GetStyle());
7227 }
7228 if (attr.HasWeight())
7229 {
7230 flags |= wxTEXT_ATTR_FONT_WEIGHT;
7231 font.SetWeight(attr.GetFont().GetWeight());
7232 }
7233 if (attr.HasFaceName())
7234 {
7235 flags |= wxTEXT_ATTR_FONT_FACE;
7236 font.SetFaceName(attr.GetFont().GetFaceName());
7237 }
7238 if (attr.HasUnderlined())
7239 {
7240 flags |= wxTEXT_ATTR_FONT_UNDERLINE;
7241 font.SetUnderlined(attr.GetFont().GetUnderlined());
7242 }
7243 newAttr.SetFont(font);
7244 newAttr.SetFlags(newAttr.GetFlags()|flags);
7245 }
7246 }
7247
7248 // TODO: should really check we are specifying these in the flags,
7249 // before setting them, as per above; or we will set them willy-nilly.
7250 // However, we should also check whether this is the intention
7251 // as per wxTextAttr::Combine, i.e. always to have valid colours
7252 // in the style.
7253 wxColour colFg = attr.GetTextColour();
7254 if ( !colFg.Ok() )
7255 {
7256 colFg = attrDef.GetTextColour();
7257
7258 if ( text && !colFg.Ok() )
7259 colFg = text->GetForegroundColour();
7260 }
7261
7262 wxColour colBg = attr.GetBackgroundColour();
7263 if ( !colBg.Ok() )
7264 {
7265 colBg = attrDef.GetBackgroundColour();
7266
7267 if ( text && !colBg.Ok() )
7268 colBg = text->GetBackgroundColour();
7269 }
7270
7271 newAttr.SetTextColour(colFg);
7272 newAttr.SetBackgroundColour(colBg);
7273
7274 if (attr.HasAlignment())
7275 newAttr.SetAlignment(attr.GetAlignment());
7276 else if (attrDef.HasAlignment())
7277 newAttr.SetAlignment(attrDef.GetAlignment());
7278
7279 if (attr.HasTabs())
7280 newAttr.SetTabs(attr.GetTabs());
7281 else if (attrDef.HasTabs())
7282 newAttr.SetTabs(attrDef.GetTabs());
7283
7284 if (attr.HasLeftIndent())
7285 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
7286 else if (attrDef.HasLeftIndent())
7287 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
7288
7289 if (attr.HasRightIndent())
7290 newAttr.SetRightIndent(attr.GetRightIndent());
7291 else if (attrDef.HasRightIndent())
7292 newAttr.SetRightIndent(attrDef.GetRightIndent());
7293
7294 // NEW ATTRIBUTES
7295
7296 if (attr.HasParagraphSpacingAfter())
7297 newAttr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter());
7298
7299 if (attr.HasParagraphSpacingBefore())
7300 newAttr.SetParagraphSpacingBefore(attr.GetParagraphSpacingBefore());
7301
7302 if (attr.HasLineSpacing())
7303 newAttr.SetLineSpacing(attr.GetLineSpacing());
7304
7305 if (attr.HasCharacterStyleName())
7306 newAttr.SetCharacterStyleName(attr.GetCharacterStyleName());
7307
7308 if (attr.HasParagraphStyleName())
7309 newAttr.SetParagraphStyleName(attr.GetParagraphStyleName());
7310
7311 if (attr.HasListStyleName())
7312 newAttr.SetListStyleName(attr.GetListStyleName());
7313
7314 if (attr.HasBulletStyle())
7315 newAttr.SetBulletStyle(attr.GetBulletStyle());
7316
7317 if (attr.HasBulletNumber())
7318 newAttr.SetBulletNumber(attr.GetBulletNumber());
7319
7320 if (attr.HasBulletName())
7321 newAttr.SetBulletName(attr.GetBulletName());
7322
7323 if (attr.HasBulletText())
7324 {
7325 newAttr.SetBulletText(attr.GetBulletText());
7326 newAttr.SetBulletFont(attr.GetBulletFont());
7327 }
7328
7329 if (attr.HasURL())
7330 newAttr.SetURL(attr.GetURL());
7331
7332 return newAttr;
7333 }
7334
7335
7336 /*!
7337 * wxRichTextFileHandler
7338 * Base class for file handlers
7339 */
7340
7341 IMPLEMENT_CLASS(wxRichTextFileHandler, wxObject)
7342
7343 #if wxUSE_STREAMS
7344 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer *buffer, const wxString& filename)
7345 {
7346 wxFFileInputStream stream(filename);
7347 if (stream.Ok())
7348 return LoadFile(buffer, stream);
7349
7350 return false;
7351 }
7352
7353 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& filename)
7354 {
7355 wxFFileOutputStream stream(filename);
7356 if (stream.Ok())
7357 return SaveFile(buffer, stream);
7358
7359 return false;
7360 }
7361 #endif // wxUSE_STREAMS
7362
7363 /// Can we handle this filename (if using files)? By default, checks the extension.
7364 bool wxRichTextFileHandler::CanHandle(const wxString& filename) const
7365 {
7366 wxString path, file, ext;
7367 wxSplitPath(filename, & path, & file, & ext);
7368
7369 return (ext.Lower() == GetExtension());
7370 }
7371
7372 /*!
7373 * wxRichTextTextHandler
7374 * Plain text handler
7375 */
7376
7377 IMPLEMENT_CLASS(wxRichTextPlainTextHandler, wxRichTextFileHandler)
7378
7379 #if wxUSE_STREAMS
7380 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
7381 {
7382 if (!stream.IsOk())
7383 return false;
7384
7385 wxString str;
7386 int lastCh = 0;
7387
7388 while (!stream.Eof())
7389 {
7390 int ch = stream.GetC();
7391
7392 if (!stream.Eof())
7393 {
7394 if (ch == 10 && lastCh != 13)
7395 str += wxT('\n');
7396
7397 if (ch > 0 && ch != 10)
7398 str += wxChar(ch);
7399
7400 lastCh = ch;
7401 }
7402 }
7403
7404 buffer->Clear();
7405 buffer->AddParagraphs(str);
7406 buffer->UpdateRanges();
7407
7408 return true;
7409
7410 }
7411
7412 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
7413 {
7414 if (!stream.IsOk())
7415 return false;
7416
7417 wxString text = buffer->GetText();
7418 wxCharBuffer buf = text.ToAscii();
7419
7420 stream.Write((const char*) buf, text.length());
7421 return true;
7422 }
7423 #endif // wxUSE_STREAMS
7424
7425 /*
7426 * Stores information about an image, in binary in-memory form
7427 */
7428
7429 wxRichTextImageBlock::wxRichTextImageBlock()
7430 {
7431 Init();
7432 }
7433
7434 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock& block):wxObject()
7435 {
7436 Init();
7437 Copy(block);
7438 }
7439
7440 wxRichTextImageBlock::~wxRichTextImageBlock()
7441 {
7442 if (m_data)
7443 {
7444 delete[] m_data;
7445 m_data = NULL;
7446 }
7447 }
7448
7449 void wxRichTextImageBlock::Init()
7450 {
7451 m_data = NULL;
7452 m_dataSize = 0;
7453 m_imageType = -1;
7454 }
7455
7456 void wxRichTextImageBlock::Clear()
7457 {
7458 delete[] m_data;
7459 m_data = NULL;
7460 m_dataSize = 0;
7461 m_imageType = -1;
7462 }
7463
7464
7465 // Load the original image into a memory block.
7466 // If the image is not a JPEG, we must convert it into a JPEG
7467 // to conserve space.
7468 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7469 // load the image a 2nd time.
7470
7471 bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, int imageType, wxImage& image, bool convertToJPEG)
7472 {
7473 m_imageType = imageType;
7474
7475 wxString filenameToRead(filename);
7476 bool removeFile = false;
7477
7478 if (imageType == -1)
7479 return false; // Could not determine image type
7480
7481 if ((imageType != wxBITMAP_TYPE_JPEG) && convertToJPEG)
7482 {
7483 wxString tempFile;
7484 bool success = wxGetTempFileName(_("image"), tempFile) ;
7485
7486 wxASSERT(success);
7487
7488 wxUnusedVar(success);
7489
7490 image.SaveFile(tempFile, wxBITMAP_TYPE_JPEG);
7491 filenameToRead = tempFile;
7492 removeFile = true;
7493
7494 m_imageType = wxBITMAP_TYPE_JPEG;
7495 }
7496 wxFile file;
7497 if (!file.Open(filenameToRead))
7498 return false;
7499
7500 m_dataSize = (size_t) file.Length();
7501 file.Close();
7502
7503 if (m_data)
7504 delete[] m_data;
7505 m_data = ReadBlock(filenameToRead, m_dataSize);
7506
7507 if (removeFile)
7508 wxRemoveFile(filenameToRead);
7509
7510 return (m_data != NULL);
7511 }
7512
7513 // Make an image block from the wxImage in the given
7514 // format.
7515 bool wxRichTextImageBlock::MakeImageBlock(wxImage& image, int imageType, int quality)
7516 {
7517 m_imageType = imageType;
7518 image.SetOption(wxT("quality"), quality);
7519
7520 if (imageType == -1)
7521 return false; // Could not determine image type
7522
7523 wxString tempFile;
7524 bool success = wxGetTempFileName(_("image"), tempFile) ;
7525
7526 wxASSERT(success);
7527 wxUnusedVar(success);
7528
7529 if (!image.SaveFile(tempFile, m_imageType))
7530 {
7531 if (wxFileExists(tempFile))
7532 wxRemoveFile(tempFile);
7533 return false;
7534 }
7535
7536 wxFile file;
7537 if (!file.Open(tempFile))
7538 return false;
7539
7540 m_dataSize = (size_t) file.Length();
7541 file.Close();
7542
7543 if (m_data)
7544 delete[] m_data;
7545 m_data = ReadBlock(tempFile, m_dataSize);
7546
7547 wxRemoveFile(tempFile);
7548
7549 return (m_data != NULL);
7550 }
7551
7552
7553 // Write to a file
7554 bool wxRichTextImageBlock::Write(const wxString& filename)
7555 {
7556 return WriteBlock(filename, m_data, m_dataSize);
7557 }
7558
7559 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock& block)
7560 {
7561 m_imageType = block.m_imageType;
7562 if (m_data)
7563 {
7564 delete[] m_data;
7565 m_data = NULL;
7566 }
7567 m_dataSize = block.m_dataSize;
7568 if (m_dataSize == 0)
7569 return;
7570
7571 m_data = new unsigned char[m_dataSize];
7572 unsigned int i;
7573 for (i = 0; i < m_dataSize; i++)
7574 m_data[i] = block.m_data[i];
7575 }
7576
7577 //// Operators
7578 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock& block)
7579 {
7580 Copy(block);
7581 }
7582
7583 // Load a wxImage from the block
7584 bool wxRichTextImageBlock::Load(wxImage& image)
7585 {
7586 if (!m_data)
7587 return false;
7588
7589 // Read in the image.
7590 #if wxUSE_STREAMS
7591 wxMemoryInputStream mstream(m_data, m_dataSize);
7592 bool success = image.LoadFile(mstream, GetImageType());
7593 #else
7594 wxString tempFile;
7595 bool success = wxGetTempFileName(_("image"), tempFile) ;
7596 wxASSERT(success);
7597
7598 if (!WriteBlock(tempFile, m_data, m_dataSize))
7599 {
7600 return false;
7601 }
7602 success = image.LoadFile(tempFile, GetImageType());
7603 wxRemoveFile(tempFile);
7604 #endif
7605
7606 return success;
7607 }
7608
7609 // Write data in hex to a stream
7610 bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream)
7611 {
7612 wxString hex;
7613 int i;
7614 for (i = 0; i < (int) m_dataSize; i++)
7615 {
7616 hex = wxDecToHex(m_data[i]);
7617 wxCharBuffer buf = hex.ToAscii();
7618
7619 stream.Write((const char*) buf, hex.length());
7620 }
7621
7622 return true;
7623 }
7624
7625 // Read data in hex from a stream
7626 bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, int imageType)
7627 {
7628 int dataSize = length/2;
7629
7630 if (m_data)
7631 delete[] m_data;
7632
7633 wxString str(wxT(" "));
7634 m_data = new unsigned char[dataSize];
7635 int i;
7636 for (i = 0; i < dataSize; i ++)
7637 {
7638 str[0] = stream.GetC();
7639 str[1] = stream.GetC();
7640
7641 m_data[i] = (unsigned char)wxHexToDec(str);
7642 }
7643
7644 m_dataSize = dataSize;
7645 m_imageType = imageType;
7646
7647 return true;
7648 }
7649
7650 // Allocate and read from stream as a block of memory
7651 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream& stream, size_t size)
7652 {
7653 unsigned char* block = new unsigned char[size];
7654 if (!block)
7655 return NULL;
7656
7657 stream.Read(block, size);
7658
7659 return block;
7660 }
7661
7662 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString& filename, size_t size)
7663 {
7664 wxFileInputStream stream(filename);
7665 if (!stream.Ok())
7666 return NULL;
7667
7668 return ReadBlock(stream, size);
7669 }
7670
7671 // Write memory block to stream
7672 bool wxRichTextImageBlock::WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size)
7673 {
7674 stream.Write((void*) block, size);
7675 return stream.IsOk();
7676
7677 }
7678
7679 // Write memory block to file
7680 bool wxRichTextImageBlock::WriteBlock(const wxString& filename, unsigned char* block, size_t size)
7681 {
7682 wxFileOutputStream outStream(filename);
7683 if (!outStream.Ok())
7684 return false;
7685
7686 return WriteBlock(outStream, block, size);
7687 }
7688
7689 // Gets the extension for the block's type
7690 wxString wxRichTextImageBlock::GetExtension() const
7691 {
7692 wxImageHandler* handler = wxImage::FindHandler(GetImageType());
7693 if (handler)
7694 return handler->GetExtension();
7695 else
7696 return wxEmptyString;
7697 }
7698
7699 #if wxUSE_DATAOBJ
7700
7701 /*!
7702 * The data object for a wxRichTextBuffer
7703 */
7704
7705 const wxChar *wxRichTextBufferDataObject::ms_richTextBufferFormatId = wxT("wxShape");
7706
7707 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer* richTextBuffer)
7708 {
7709 m_richTextBuffer = richTextBuffer;
7710
7711 // this string should uniquely identify our format, but is otherwise
7712 // arbitrary
7713 m_formatRichTextBuffer.SetId(GetRichTextBufferFormatId());
7714
7715 SetFormat(m_formatRichTextBuffer);
7716 }
7717
7718 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7719 {
7720 delete m_richTextBuffer;
7721 }
7722
7723 // after a call to this function, the richTextBuffer is owned by the caller and it
7724 // is responsible for deleting it!
7725 wxRichTextBuffer* wxRichTextBufferDataObject::GetRichTextBuffer()
7726 {
7727 wxRichTextBuffer* richTextBuffer = m_richTextBuffer;
7728 m_richTextBuffer = NULL;
7729
7730 return richTextBuffer;
7731 }
7732
7733 wxDataFormat wxRichTextBufferDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
7734 {
7735 return m_formatRichTextBuffer;
7736 }
7737
7738 size_t wxRichTextBufferDataObject::GetDataSize() const
7739 {
7740 if (!m_richTextBuffer)
7741 return 0;
7742
7743 wxString bufXML;
7744
7745 {
7746 wxStringOutputStream stream(& bufXML);
7747 if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
7748 {
7749 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7750 return 0;
7751 }
7752 }
7753
7754 #if wxUSE_UNICODE
7755 wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
7756 return strlen(buffer) + 1;
7757 #else
7758 return bufXML.Length()+1;
7759 #endif
7760 }
7761
7762 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf) const
7763 {
7764 if (!pBuf || !m_richTextBuffer)
7765 return false;
7766
7767 wxString bufXML;
7768
7769 {
7770 wxStringOutputStream stream(& bufXML);
7771 if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
7772 {
7773 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7774 return 0;
7775 }
7776 }
7777
7778 #if wxUSE_UNICODE
7779 wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
7780 size_t len = strlen(buffer);
7781 memcpy((char*) pBuf, (const char*) buffer, len);
7782 ((char*) pBuf)[len] = 0;
7783 #else
7784 size_t len = bufXML.Length();
7785 memcpy((char*) pBuf, (const char*) bufXML.c_str(), len);
7786 ((char*) pBuf)[len] = 0;
7787 #endif
7788
7789 return true;
7790 }
7791
7792 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len), const void *buf)
7793 {
7794 delete m_richTextBuffer;
7795 m_richTextBuffer = NULL;
7796
7797 wxString bufXML((const char*) buf, wxConvUTF8);
7798
7799 m_richTextBuffer = new wxRichTextBuffer;
7800
7801 wxStringInputStream stream(bufXML);
7802 if (!m_richTextBuffer->LoadFile(stream, wxRICHTEXT_TYPE_XML))
7803 {
7804 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7805
7806 delete m_richTextBuffer;
7807 m_richTextBuffer = NULL;
7808
7809 return false;
7810 }
7811 return true;
7812 }
7813
7814 #endif
7815 // wxUSE_DATAOBJ
7816
7817 #endif
7818 // wxUSE_RICHTEXT