Pagination fix
[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, range, 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& 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
3085 if (!child->GetRange().IsOutside(lineRange) && !lineRange.IsOutside(range))
3086 {
3087 // Draw this part of the line at the correct position
3088 wxRichTextRange objectRange(child->GetRange());
3089 objectRange.LimitTo(lineRange);
3090
3091 wxSize objectSize;
3092 int descent = 0;
3093 child->GetRangeSize(objectRange, objectSize, descent, dc, wxRICHTEXT_UNFORMATTED, objectPosition);
3094
3095 // Use the child object's width, but the whole line's height
3096 wxRect childRect(objectPosition, wxSize(objectSize.x, line->GetSize().y));
3097 child->Draw(dc, objectRange, selectionRange, childRect, maxDescent, style);
3098
3099 objectPosition.x += objectSize.x;
3100 }
3101 else if (child->GetRange().GetStart() > lineRange.GetEnd())
3102 // Can break out of inner loop now since we've passed this line's range
3103 break;
3104
3105 node2 = node2->GetNext();
3106 }
3107
3108 node = node->GetNext();
3109 }
3110
3111 return true;
3112 }
3113
3114 /// Lay the item out
3115 bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style)
3116 {
3117 #if wxRICHTEXT_USE_DYNAMIC_STYLES
3118 wxTextAttrEx attr = GetCombinedAttributes();
3119 #else
3120 const wxTextAttrEx& attr = GetAttributes();
3121 #endif
3122
3123 // ClearLines();
3124
3125 // Increase the size of the paragraph due to spacing
3126 int spaceBeforePara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingBefore());
3127 int spaceAfterPara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingAfter());
3128 int leftIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftIndent());
3129 int leftSubIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftSubIndent());
3130 int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent());
3131
3132 int lineSpacing = 0;
3133
3134 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
3135 if (attr.GetLineSpacing() > 10 && attr.GetFont().Ok())
3136 {
3137 dc.SetFont(attr.GetFont());
3138 lineSpacing = (ConvertTenthsMMToPixels(dc, dc.GetCharHeight()) * attr.GetLineSpacing())/10;
3139 }
3140
3141 // Available space for text on each line differs.
3142 int availableTextSpaceFirstLine = rect.GetWidth() - leftIndent - rightIndent;
3143
3144 // Bullets start the text at the same position as subsequent lines
3145 if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
3146 availableTextSpaceFirstLine -= leftSubIndent;
3147
3148 int availableTextSpaceSubsequentLines = rect.GetWidth() - leftIndent - rightIndent - leftSubIndent;
3149
3150 // Start position for each line relative to the paragraph
3151 int startPositionFirstLine = leftIndent;
3152 int startPositionSubsequentLines = leftIndent + leftSubIndent;
3153
3154 // If we have a bullet in this paragraph, the start position for the first line's text
3155 // is actually leftIndent + leftSubIndent.
3156 if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
3157 startPositionFirstLine = startPositionSubsequentLines;
3158
3159 long lastEndPos = GetRange().GetStart()-1;
3160 long lastCompletedEndPos = lastEndPos;
3161
3162 int currentWidth = 0;
3163 SetPosition(rect.GetPosition());
3164
3165 wxPoint currentPosition(0, spaceBeforePara); // We will calculate lines relative to paragraph
3166 int lineHeight = 0;
3167 int maxWidth = 0;
3168 int maxDescent = 0;
3169
3170 int lineCount = 0;
3171
3172 // Split up lines
3173
3174 // We may need to go back to a previous child, in which case create the new line,
3175 // find the child corresponding to the start position of the string, and
3176 // continue.
3177
3178 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3179 while (node)
3180 {
3181 wxRichTextObject* child = node->GetData();
3182
3183 // If this is e.g. a composite text box, it will need to be laid out itself.
3184 // But if just a text fragment or image, for example, this will
3185 // do nothing. NB: won't we need to set the position after layout?
3186 // since for example if position is dependent on vertical line size, we
3187 // can't tell the position until the size is determined. So possibly introduce
3188 // another layout phase.
3189
3190 child->Layout(dc, rect, style);
3191
3192 // Available width depends on whether we're on the first or subsequent lines
3193 int availableSpaceForText = (lineCount == 0 ? availableTextSpaceFirstLine : availableTextSpaceSubsequentLines);
3194
3195 currentPosition.x = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines);
3196
3197 // We may only be looking at part of a child, if we searched back for wrapping
3198 // and found a suitable point some way into the child. So get the size for the fragment
3199 // if necessary.
3200
3201 wxSize childSize;
3202 int childDescent = 0;
3203 if (lastEndPos == child->GetRange().GetStart() - 1)
3204 {
3205 childSize = child->GetCachedSize();
3206 childDescent = child->GetDescent();
3207 }
3208 else
3209 GetRangeSize(wxRichTextRange(lastEndPos+1, child->GetRange().GetEnd()), childSize, childDescent, dc, wxRICHTEXT_UNFORMATTED,rect.GetPosition());
3210
3211 if (childSize.x + currentWidth > availableSpaceForText)
3212 {
3213 long wrapPosition = 0;
3214
3215 // Find a place to wrap. This may walk back to previous children,
3216 // for example if a word spans several objects.
3217 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos+1, child->GetRange().GetEnd()), dc, availableSpaceForText, wrapPosition))
3218 {
3219 // If the function failed, just cut it off at the end of this child.
3220 wrapPosition = child->GetRange().GetEnd();
3221 }
3222
3223 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
3224 if (wrapPosition <= lastCompletedEndPos)
3225 wrapPosition = wxMax(lastCompletedEndPos+1,child->GetRange().GetEnd());
3226
3227 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
3228
3229 // Let's find the actual size of the current line now
3230 wxSize actualSize;
3231 wxRichTextRange actualRange(lastCompletedEndPos+1, wrapPosition);
3232 GetRangeSize(actualRange, actualSize, childDescent, dc, wxRICHTEXT_UNFORMATTED);
3233 currentWidth = actualSize.x;
3234 lineHeight = wxMax(lineHeight, actualSize.y);
3235 maxDescent = wxMax(childDescent, maxDescent);
3236
3237 // Add a new line
3238 wxRichTextLine* line = AllocateLine(lineCount);
3239
3240 // Set relative range so we won't have to change line ranges when paragraphs are moved
3241 line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart()));
3242 line->SetPosition(currentPosition);
3243 line->SetSize(wxSize(currentWidth, lineHeight));
3244 line->SetDescent(maxDescent);
3245
3246 // Now move down a line. TODO: add margins, spacing
3247 currentPosition.y += lineHeight;
3248 currentPosition.y += lineSpacing;
3249 currentWidth = 0;
3250 maxDescent = 0;
3251 maxWidth = wxMax(maxWidth, currentWidth);
3252
3253 lineCount ++;
3254
3255 // TODO: account for zero-length objects, such as fields
3256 wxASSERT(wrapPosition > lastCompletedEndPos);
3257
3258 lastEndPos = wrapPosition;
3259 lastCompletedEndPos = lastEndPos;
3260
3261 lineHeight = 0;
3262
3263 // May need to set the node back to a previous one, due to searching back in wrapping
3264 wxRichTextObject* childAfterWrapPosition = FindObjectAtPosition(wrapPosition+1);
3265 if (childAfterWrapPosition)
3266 node = m_children.Find(childAfterWrapPosition);
3267 else
3268 node = node->GetNext();
3269 }
3270 else
3271 {
3272 // We still fit, so don't add a line, and keep going
3273 currentWidth += childSize.x;
3274 lineHeight = wxMax(lineHeight, childSize.y);
3275 maxDescent = wxMax(childDescent, maxDescent);
3276
3277 maxWidth = wxMax(maxWidth, currentWidth);
3278 lastEndPos = child->GetRange().GetEnd();
3279
3280 node = node->GetNext();
3281 }
3282 }
3283
3284 // Add the last line - it's the current pos -> last para pos
3285 // Substract -1 because the last position is always the end-paragraph position.
3286 if (lastCompletedEndPos <= GetRange().GetEnd()-1)
3287 {
3288 currentPosition.x = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines);
3289
3290 wxRichTextLine* line = AllocateLine(lineCount);
3291
3292 wxRichTextRange actualRange(lastCompletedEndPos+1, GetRange().GetEnd()-1);
3293
3294 // Set relative range so we won't have to change line ranges when paragraphs are moved
3295 line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart()));
3296
3297 line->SetPosition(currentPosition);
3298
3299 if (lineHeight == 0)
3300 {
3301 if (attr.GetFont().Ok())
3302 dc.SetFont(attr.GetFont());
3303 lineHeight = dc.GetCharHeight();
3304 }
3305 if (maxDescent == 0)
3306 {
3307 int w, h;
3308 dc.GetTextExtent(wxT("X"), & w, &h, & maxDescent);
3309 }
3310
3311 line->SetSize(wxSize(currentWidth, lineHeight));
3312 line->SetDescent(maxDescent);
3313 currentPosition.y += lineHeight;
3314 currentPosition.y += lineSpacing;
3315 lineCount ++;
3316 }
3317
3318 // Remove remaining unused line objects, if any
3319 ClearUnusedLines(lineCount);
3320
3321 // Apply styles to wrapped lines
3322 ApplyParagraphStyle(attr, rect);
3323
3324 SetCachedSize(wxSize(maxWidth, currentPosition.y + spaceBeforePara + spaceAfterPara));
3325
3326 m_dirty = false;
3327
3328 return true;
3329 }
3330
3331 /// Apply paragraph styles, such as centering, to wrapped lines
3332 void wxRichTextParagraph::ApplyParagraphStyle(const wxTextAttrEx& attr, const wxRect& rect)
3333 {
3334 if (!attr.HasAlignment())
3335 return;
3336
3337 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
3338 while (node)
3339 {
3340 wxRichTextLine* line = node->GetData();
3341
3342 wxPoint pos = line->GetPosition();
3343 wxSize size = line->GetSize();
3344
3345 // centering, right-justification
3346 if (attr.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
3347 {
3348 pos.x = (rect.GetWidth() - size.x)/2 + pos.x;
3349 line->SetPosition(pos);
3350 }
3351 else if (attr.HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
3352 {
3353 pos.x = pos.x + rect.GetWidth() - size.x;
3354 line->SetPosition(pos);
3355 }
3356
3357 node = node->GetNext();
3358 }
3359 }
3360
3361 /// Insert text at the given position
3362 bool wxRichTextParagraph::InsertText(long pos, const wxString& text)
3363 {
3364 wxRichTextObject* childToUse = NULL;
3365 wxRichTextObjectList::compatibility_iterator nodeToUse = wxRichTextObjectList::compatibility_iterator();
3366
3367 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3368 while (node)
3369 {
3370 wxRichTextObject* child = node->GetData();
3371 if (child->GetRange().Contains(pos) && child->GetRange().GetLength() > 0)
3372 {
3373 childToUse = child;
3374 nodeToUse = node;
3375 break;
3376 }
3377
3378 node = node->GetNext();
3379 }
3380
3381 if (childToUse)
3382 {
3383 wxRichTextPlainText* textObject = wxDynamicCast(childToUse, wxRichTextPlainText);
3384 if (textObject)
3385 {
3386 int posInString = pos - textObject->GetRange().GetStart();
3387
3388 wxString newText = textObject->GetText().Mid(0, posInString) +
3389 text + textObject->GetText().Mid(posInString);
3390 textObject->SetText(newText);
3391
3392 int textLength = text.length();
3393
3394 textObject->SetRange(wxRichTextRange(textObject->GetRange().GetStart(),
3395 textObject->GetRange().GetEnd() + textLength));
3396
3397 // Increment the end range of subsequent fragments in this paragraph.
3398 // We'll set the paragraph range itself at a higher level.
3399
3400 wxRichTextObjectList::compatibility_iterator node = nodeToUse->GetNext();
3401 while (node)
3402 {
3403 wxRichTextObject* child = node->GetData();
3404 child->SetRange(wxRichTextRange(textObject->GetRange().GetStart() + textLength,
3405 textObject->GetRange().GetEnd() + textLength));
3406
3407 node = node->GetNext();
3408 }
3409
3410 return true;
3411 }
3412 else
3413 {
3414 // TODO: if not a text object, insert at closest position, e.g. in front of it
3415 }
3416 }
3417 else
3418 {
3419 // Add at end.
3420 // Don't pass parent initially to suppress auto-setting of parent range.
3421 // We'll do that at a higher level.
3422 wxRichTextPlainText* textObject = new wxRichTextPlainText(text, this);
3423
3424 AppendChild(textObject);
3425 return true;
3426 }
3427
3428 return false;
3429 }
3430
3431 void wxRichTextParagraph::Copy(const wxRichTextParagraph& obj)
3432 {
3433 wxRichTextBox::Copy(obj);
3434 }
3435
3436 /// Clear the cached lines
3437 void wxRichTextParagraph::ClearLines()
3438 {
3439 WX_CLEAR_LIST(wxRichTextLineList, m_cachedLines);
3440 }
3441
3442 /// Get/set the object size for the given range. Returns false if the range
3443 /// is invalid for this object.
3444 bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const
3445 {
3446 if (!range.IsWithin(GetRange()))
3447 return false;
3448
3449 if (flags & wxRICHTEXT_UNFORMATTED)
3450 {
3451 // Just use unformatted data, assume no line breaks
3452 // TODO: take into account line breaks
3453
3454 wxSize sz;
3455
3456 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3457 while (node)
3458 {
3459 wxRichTextObject* child = node->GetData();
3460 if (!child->GetRange().IsOutside(range))
3461 {
3462 wxSize childSize;
3463
3464 wxRichTextRange rangeToUse = range;
3465 rangeToUse.LimitTo(child->GetRange());
3466 int childDescent = 0;
3467
3468 if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, position))
3469 {
3470 sz.y = wxMax(sz.y, childSize.y);
3471 sz.x += childSize.x;
3472 descent = wxMax(descent, childDescent);
3473 }
3474 }
3475
3476 node = node->GetNext();
3477 }
3478 size = sz;
3479 }
3480 else
3481 {
3482 // Use formatted data, with line breaks
3483 wxSize sz;
3484
3485 // We're going to loop through each line, and then for each line,
3486 // call GetRangeSize for the fragment that comprises that line.
3487 // Only we have to do that multiple times within the line, because
3488 // the line may be broken into pieces. For now ignore line break commands
3489 // (so we can assume that getting the unformatted size for a fragment
3490 // within a line is the actual size)
3491
3492 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
3493 while (node)
3494 {
3495 wxRichTextLine* line = node->GetData();
3496 wxRichTextRange lineRange = line->GetAbsoluteRange();
3497 if (!lineRange.IsOutside(range))
3498 {
3499 wxSize lineSize;
3500
3501 wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst();
3502 while (node2)
3503 {
3504 wxRichTextObject* child = node2->GetData();
3505
3506 if (!child->GetRange().IsOutside(lineRange))
3507 {
3508 wxRichTextRange rangeToUse = lineRange;
3509 rangeToUse.LimitTo(child->GetRange());
3510
3511 wxSize childSize;
3512 int childDescent = 0;
3513 if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags, position))
3514 {
3515 lineSize.y = wxMax(lineSize.y, childSize.y);
3516 lineSize.x += childSize.x;
3517 }
3518 descent = wxMax(descent, childDescent);
3519 }
3520
3521 node2 = node2->GetNext();
3522 }
3523
3524 // Increase size by a line (TODO: paragraph spacing)
3525 sz.y += lineSize.y;
3526 sz.x = wxMax(sz.x, lineSize.x);
3527 }
3528 node = node->GetNext();
3529 }
3530 size = sz;
3531 }
3532 return true;
3533 }
3534
3535 /// Finds the absolute position and row height for the given character position
3536 bool wxRichTextParagraph::FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart)
3537 {
3538 if (index == -1)
3539 {
3540 wxRichTextLine* line = ((wxRichTextParagraphLayoutBox*)GetParent())->GetLineAtPosition(0);
3541 if (line)
3542 *height = line->GetSize().y;
3543 else
3544 *height = dc.GetCharHeight();
3545
3546 // -1 means 'the start of the buffer'.
3547 pt = GetPosition();
3548 if (line)
3549 pt = pt + line->GetPosition();
3550
3551 return true;
3552 }
3553
3554 // The final position in a paragraph is taken to mean the position
3555 // at the start of the next paragraph.
3556 if (index == GetRange().GetEnd())
3557 {
3558 wxRichTextParagraphLayoutBox* parent = wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox);
3559 wxASSERT( parent != NULL );
3560
3561 // Find the height at the next paragraph, if any
3562 wxRichTextLine* line = parent->GetLineAtPosition(index + 1);
3563 if (line)
3564 {
3565 *height = line->GetSize().y;
3566 pt = line->GetAbsolutePosition();
3567 }
3568 else
3569 {
3570 *height = dc.GetCharHeight();
3571 int indent = ConvertTenthsMMToPixels(dc, m_attributes.GetLeftIndent());
3572 pt = wxPoint(indent, GetCachedSize().y);
3573 }
3574
3575 return true;
3576 }
3577
3578 if (index < GetRange().GetStart() || index > GetRange().GetEnd())
3579 return false;
3580
3581 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
3582 while (node)
3583 {
3584 wxRichTextLine* line = node->GetData();
3585 wxRichTextRange lineRange = line->GetAbsoluteRange();
3586 if (index >= lineRange.GetStart() && index <= lineRange.GetEnd())
3587 {
3588 // If this is the last point in the line, and we're forcing the
3589 // returned value to be the start of the next line, do the required
3590 // thing.
3591 if (index == lineRange.GetEnd() && forceLineStart)
3592 {
3593 if (node->GetNext())
3594 {
3595 wxRichTextLine* nextLine = node->GetNext()->GetData();
3596 *height = nextLine->GetSize().y;
3597 pt = nextLine->GetAbsolutePosition();
3598 return true;
3599 }
3600 }
3601
3602 pt.y = line->GetPosition().y + GetPosition().y;
3603
3604 wxRichTextRange r(lineRange.GetStart(), index);
3605 wxSize rangeSize;
3606 int descent = 0;
3607
3608 // We find the size of the line up to this point,
3609 // then we can add this size to the line start position and
3610 // paragraph start position to find the actual position.
3611
3612 if (GetRangeSize(r, rangeSize, descent, dc, wxRICHTEXT_UNFORMATTED, line->GetPosition()+ GetPosition()))
3613 {
3614 pt.x = line->GetPosition().x + GetPosition().x + rangeSize.x;
3615 *height = line->GetSize().y;
3616
3617 return true;
3618 }
3619
3620 }
3621
3622 node = node->GetNext();
3623 }
3624
3625 return false;
3626 }
3627
3628 /// Hit-testing: returns a flag indicating hit test details, plus
3629 /// information about position
3630 int wxRichTextParagraph::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition)
3631 {
3632 wxPoint paraPos = GetPosition();
3633
3634 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
3635 while (node)
3636 {
3637 wxRichTextLine* line = node->GetData();
3638 wxPoint linePos = paraPos + line->GetPosition();
3639 wxSize lineSize = line->GetSize();
3640 wxRichTextRange lineRange = line->GetAbsoluteRange();
3641
3642 if (pt.y >= linePos.y && pt.y <= linePos.y + lineSize.y)
3643 {
3644 if (pt.x < linePos.x)
3645 {
3646 textPosition = lineRange.GetStart();
3647 return wxRICHTEXT_HITTEST_BEFORE;
3648 }
3649 else if (pt.x >= (linePos.x + lineSize.x))
3650 {
3651 textPosition = lineRange.GetEnd();
3652 return wxRICHTEXT_HITTEST_AFTER;
3653 }
3654 else
3655 {
3656 long i;
3657 int lastX = linePos.x;
3658 for (i = lineRange.GetStart(); i <= lineRange.GetEnd(); i++)
3659 {
3660 wxSize childSize;
3661 int descent = 0;
3662
3663 wxRichTextRange rangeToUse(lineRange.GetStart(), i);
3664
3665 GetRangeSize(rangeToUse, childSize, descent, dc, wxRICHTEXT_UNFORMATTED, linePos);
3666
3667 int nextX = childSize.x + linePos.x;
3668
3669 if (pt.x >= lastX && pt.x <= nextX)
3670 {
3671 textPosition = i;
3672
3673 // So now we know it's between i-1 and i.
3674 // Let's see if we can be more precise about
3675 // which side of the position it's on.
3676
3677 int midPoint = (nextX - lastX)/2 + lastX;
3678 if (pt.x >= midPoint)
3679 return wxRICHTEXT_HITTEST_AFTER;
3680 else
3681 return wxRICHTEXT_HITTEST_BEFORE;
3682 }
3683 else
3684 {
3685 lastX = nextX;
3686 }
3687 }
3688 }
3689 }
3690
3691 node = node->GetNext();
3692 }
3693
3694 return wxRICHTEXT_HITTEST_NONE;
3695 }
3696
3697 /// Split an object at this position if necessary, and return
3698 /// the previous object, or NULL if inserting at beginning.
3699 wxRichTextObject* wxRichTextParagraph::SplitAt(long pos, wxRichTextObject** previousObject)
3700 {
3701 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3702 while (node)
3703 {
3704 wxRichTextObject* child = node->GetData();
3705
3706 if (pos == child->GetRange().GetStart())
3707 {
3708 if (previousObject)
3709 {
3710 if (node->GetPrevious())
3711 *previousObject = node->GetPrevious()->GetData();
3712 else
3713 *previousObject = NULL;
3714 }
3715
3716 return child;
3717 }
3718
3719 if (child->GetRange().Contains(pos))
3720 {
3721 // This should create a new object, transferring part of
3722 // the content to the old object and the rest to the new object.
3723 wxRichTextObject* newObject = child->DoSplit(pos);
3724
3725 // If we couldn't split this object, just insert in front of it.
3726 if (!newObject)
3727 {
3728 // Maybe this is an empty string, try the next one
3729 // return child;
3730 }
3731 else
3732 {
3733 // Insert the new object after 'child'
3734 if (node->GetNext())
3735 m_children.Insert(node->GetNext(), newObject);
3736 else
3737 m_children.Append(newObject);
3738 newObject->SetParent(this);
3739
3740 if (previousObject)
3741 *previousObject = child;
3742
3743 return newObject;
3744 }
3745 }
3746
3747 node = node->GetNext();
3748 }
3749 if (previousObject)
3750 *previousObject = NULL;
3751 return NULL;
3752 }
3753
3754 /// Move content to a list from obj on
3755 void wxRichTextParagraph::MoveToList(wxRichTextObject* obj, wxList& list)
3756 {
3757 wxRichTextObjectList::compatibility_iterator node = m_children.Find(obj);
3758 while (node)
3759 {
3760 wxRichTextObject* child = node->GetData();
3761 list.Append(child);
3762
3763 wxRichTextObjectList::compatibility_iterator oldNode = node;
3764
3765 node = node->GetNext();
3766
3767 m_children.DeleteNode(oldNode);
3768 }
3769 }
3770
3771 /// Add content back from list
3772 void wxRichTextParagraph::MoveFromList(wxList& list)
3773 {
3774 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
3775 {
3776 AppendChild((wxRichTextObject*) node->GetData());
3777 }
3778 }
3779
3780 /// Calculate range
3781 void wxRichTextParagraph::CalculateRange(long start, long& end)
3782 {
3783 wxRichTextCompositeObject::CalculateRange(start, end);
3784
3785 // Add one for end of paragraph
3786 end ++;
3787
3788 m_range.SetRange(start, end);
3789 }
3790
3791 /// Find the object at the given position
3792 wxRichTextObject* wxRichTextParagraph::FindObjectAtPosition(long position)
3793 {
3794 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3795 while (node)
3796 {
3797 wxRichTextObject* obj = node->GetData();
3798 if (obj->GetRange().Contains(position))
3799 return obj;
3800
3801 node = node->GetNext();
3802 }
3803 return NULL;
3804 }
3805
3806 /// Get the plain text searching from the start or end of the range.
3807 /// The resulting string may be shorter than the range given.
3808 bool wxRichTextParagraph::GetContiguousPlainText(wxString& text, const wxRichTextRange& range, bool fromStart)
3809 {
3810 text = wxEmptyString;
3811
3812 if (fromStart)
3813 {
3814 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3815 while (node)
3816 {
3817 wxRichTextObject* obj = node->GetData();
3818 if (!obj->GetRange().IsOutside(range))
3819 {
3820 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
3821 if (textObj)
3822 {
3823 text += textObj->GetTextForRange(range);
3824 }
3825 else
3826 return true;
3827 }
3828
3829 node = node->GetNext();
3830 }
3831 }
3832 else
3833 {
3834 wxRichTextObjectList::compatibility_iterator node = m_children.GetLast();
3835 while (node)
3836 {
3837 wxRichTextObject* obj = node->GetData();
3838 if (!obj->GetRange().IsOutside(range))
3839 {
3840 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
3841 if (textObj)
3842 {
3843 text = textObj->GetTextForRange(range) + text;
3844 }
3845 else
3846 return true;
3847 }
3848
3849 node = node->GetPrevious();
3850 }
3851 }
3852
3853 return true;
3854 }
3855
3856 /// Find a suitable wrap position.
3857 bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& dc, int availableSpace, long& wrapPosition)
3858 {
3859 // Find the first position where the line exceeds the available space.
3860 wxSize sz;
3861 long i;
3862 long breakPosition = range.GetEnd();
3863 for (i = range.GetStart(); i <= range.GetEnd(); i++)
3864 {
3865 int descent = 0;
3866 GetRangeSize(wxRichTextRange(range.GetStart(), i), sz, descent, dc, wxRICHTEXT_UNFORMATTED);
3867
3868 if (sz.x > availableSpace)
3869 {
3870 breakPosition = i-1;
3871 break;
3872 }
3873 }
3874
3875 // Now we know the last position on the line.
3876 // Let's try to find a word break.
3877
3878 wxString plainText;
3879 if (GetContiguousPlainText(plainText, wxRichTextRange(range.GetStart(), breakPosition), false))
3880 {
3881 int spacePos = plainText.Find(wxT(' '), true);
3882 if (spacePos != wxNOT_FOUND)
3883 {
3884 int positionsFromEndOfString = plainText.length() - spacePos - 1;
3885 breakPosition = breakPosition - positionsFromEndOfString;
3886 }
3887 }
3888
3889 wrapPosition = breakPosition;
3890
3891 return true;
3892 }
3893
3894 /// Get the bullet text for this paragraph.
3895 wxString wxRichTextParagraph::GetBulletText()
3896 {
3897 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE ||
3898 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP))
3899 return wxEmptyString;
3900
3901 int number = GetAttributes().GetBulletNumber();
3902
3903 wxString text;
3904 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE))
3905 {
3906 text.Printf(wxT("%d"), number);
3907 }
3908 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER)
3909 {
3910 // TODO: Unicode, and also check if number > 26
3911 text.Printf(wxT("%c"), (wxChar) (number+64));
3912 }
3913 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER)
3914 {
3915 // TODO: Unicode, and also check if number > 26
3916 text.Printf(wxT("%c"), (wxChar) (number+96));
3917 }
3918 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER)
3919 {
3920 text = wxRichTextDecimalToRoman(number);
3921 }
3922 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER)
3923 {
3924 text = wxRichTextDecimalToRoman(number);
3925 text.MakeLower();
3926 }
3927 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL)
3928 {
3929 text = GetAttributes().GetBulletText();
3930 }
3931
3932 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE)
3933 {
3934 // The outline style relies on the text being computed statically,
3935 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
3936 // should be stored in the attributes; if not, just use the number for this
3937 // level, as previously computed.
3938 if (!GetAttributes().GetBulletText().IsEmpty())
3939 text = GetAttributes().GetBulletText();
3940 }
3941
3942 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES)
3943 {
3944 text = wxT("(") + text + wxT(")");
3945 }
3946 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS)
3947 {
3948 text = text + wxT(")");
3949 }
3950
3951 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD)
3952 {
3953 text += wxT(".");
3954 }
3955
3956 return text;
3957 }
3958
3959 /// Allocate or reuse a line object
3960 wxRichTextLine* wxRichTextParagraph::AllocateLine(int pos)
3961 {
3962 if (pos < (int) m_cachedLines.GetCount())
3963 {
3964 wxRichTextLine* line = m_cachedLines.Item(pos)->GetData();
3965 line->Init(this);
3966 return line;
3967 }
3968 else
3969 {
3970 wxRichTextLine* line = new wxRichTextLine(this);
3971 m_cachedLines.Append(line);
3972 return line;
3973 }
3974 }
3975
3976 /// Clear remaining unused line objects, if any
3977 bool wxRichTextParagraph::ClearUnusedLines(int lineCount)
3978 {
3979 int cachedLineCount = m_cachedLines.GetCount();
3980 if ((int) cachedLineCount > lineCount)
3981 {
3982 for (int i = 0; i < (int) (cachedLineCount - lineCount); i ++)
3983 {
3984 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetLast();
3985 wxRichTextLine* line = node->GetData();
3986 m_cachedLines.Erase(node);
3987 delete line;
3988 }
3989 }
3990 return true;
3991 }
3992
3993 /// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
3994 /// retrieve the actual style.
3995 wxTextAttrEx wxRichTextParagraph::GetCombinedAttributes(const wxTextAttrEx& contentStyle) const
3996 {
3997 wxTextAttrEx attr;
3998 wxRichTextBuffer* buf = wxDynamicCast(GetParent(), wxRichTextBuffer);
3999 if (buf)
4000 {
4001 attr = buf->GetBasicStyle();
4002 wxRichTextApplyStyle(attr, GetAttributes());
4003 }
4004 else
4005 attr = GetAttributes();
4006
4007 wxRichTextApplyStyle(attr, contentStyle);
4008 return attr;
4009 }
4010
4011 /// Get combined attributes of the base style and paragraph style.
4012 wxTextAttrEx wxRichTextParagraph::GetCombinedAttributes() const
4013 {
4014 wxTextAttrEx attr;
4015 wxRichTextBuffer* buf = wxDynamicCast(GetParent(), wxRichTextBuffer);
4016 if (buf)
4017 {
4018 attr = buf->GetBasicStyle();
4019 wxRichTextApplyStyle(attr, GetAttributes());
4020 }
4021 else
4022 attr = GetAttributes();
4023
4024 return attr;
4025 }
4026
4027 /// Create default tabstop array
4028 void wxRichTextParagraph::InitDefaultTabs()
4029 {
4030 // create a default tab list at 10 mm each.
4031 for (int i = 0; i < 20; ++i)
4032 {
4033 sm_defaultTabs.Add(i*100);
4034 }
4035 }
4036
4037 /// Clear default tabstop array
4038 void wxRichTextParagraph::ClearDefaultTabs()
4039 {
4040 sm_defaultTabs.Clear();
4041 }
4042
4043
4044 /*!
4045 * wxRichTextLine
4046 * This object represents a line in a paragraph, and stores
4047 * offsets from the start of the paragraph representing the
4048 * start and end positions of the line.
4049 */
4050
4051 wxRichTextLine::wxRichTextLine(wxRichTextParagraph* parent)
4052 {
4053 Init(parent);
4054 }
4055
4056 /// Initialisation
4057 void wxRichTextLine::Init(wxRichTextParagraph* parent)
4058 {
4059 m_parent = parent;
4060 m_range.SetRange(-1, -1);
4061 m_pos = wxPoint(0, 0);
4062 m_size = wxSize(0, 0);
4063 m_descent = 0;
4064 }
4065
4066 /// Copy
4067 void wxRichTextLine::Copy(const wxRichTextLine& obj)
4068 {
4069 m_range = obj.m_range;
4070 }
4071
4072 /// Get the absolute object position
4073 wxPoint wxRichTextLine::GetAbsolutePosition() const
4074 {
4075 return m_parent->GetPosition() + m_pos;
4076 }
4077
4078 /// Get the absolute range
4079 wxRichTextRange wxRichTextLine::GetAbsoluteRange() const
4080 {
4081 wxRichTextRange range(m_range.GetStart() + m_parent->GetRange().GetStart(), 0);
4082 range.SetEnd(range.GetStart() + m_range.GetLength()-1);
4083 return range;
4084 }
4085
4086 /*!
4087 * wxRichTextPlainText
4088 * This object represents a single piece of text.
4089 */
4090
4091 IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText, wxRichTextObject)
4092
4093 wxRichTextPlainText::wxRichTextPlainText(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* style):
4094 wxRichTextObject(parent)
4095 {
4096 if (parent && !style)
4097 SetAttributes(parent->GetAttributes());
4098 if (style)
4099 SetAttributes(*style);
4100
4101 m_text = text;
4102 }
4103
4104 #define USE_KERNING_FIX 1
4105
4106 /// Draw the item
4107 bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int WXUNUSED(style))
4108 {
4109 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4110 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
4111 wxASSERT (para != NULL);
4112
4113 wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4114 #else
4115 wxTextAttrEx textAttr(GetAttributes());
4116 #endif
4117
4118 int offset = GetRange().GetStart();
4119
4120 long len = range.GetLength();
4121 wxString stringChunk = m_text.Mid(range.GetStart() - offset, (size_t) len);
4122
4123 int charHeight = dc.GetCharHeight();
4124
4125 int x = rect.x;
4126 int y = rect.y + (rect.height - charHeight - (descent - m_descent));
4127
4128 // Test for the optimized situations where all is selected, or none
4129 // is selected.
4130
4131 if (textAttr.GetFont().Ok())
4132 dc.SetFont(textAttr.GetFont());
4133
4134 // (a) All selected.
4135 if (selectionRange.GetStart() <= range.GetStart() && selectionRange.GetEnd() >= range.GetEnd())
4136 {
4137 DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, true);
4138 }
4139 // (b) None selected.
4140 else if (selectionRange.GetEnd() < range.GetStart() || selectionRange.GetStart() > range.GetEnd())
4141 {
4142 // Draw all unselected
4143 DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, false);
4144 }
4145 else
4146 {
4147 // (c) Part selected, part not
4148 // Let's draw unselected chunk, selected chunk, then unselected chunk.
4149
4150 dc.SetBackgroundMode(wxTRANSPARENT);
4151
4152 // 1. Initial unselected chunk, if any, up until start of selection.
4153 if (selectionRange.GetStart() > range.GetStart() && selectionRange.GetStart() <= range.GetEnd())
4154 {
4155 int r1 = range.GetStart();
4156 int s1 = selectionRange.GetStart()-1;
4157 int fragmentLen = s1 - r1 + 1;
4158 if (fragmentLen < 0)
4159 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1 - offset), (int)fragmentLen);
4160 wxString stringFragment = m_text.Mid(r1 - offset, fragmentLen);
4161
4162 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
4163
4164 #if USE_KERNING_FIX
4165 if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
4166 {
4167 // Compensate for kerning difference
4168 wxString stringFragment2(m_text.Mid(r1 - offset, fragmentLen+1));
4169 wxString stringFragment3(m_text.Mid(r1 - offset + fragmentLen, 1));
4170
4171 wxCoord w1, h1, w2, h2, w3, h3;
4172 dc.GetTextExtent(stringFragment, & w1, & h1);
4173 dc.GetTextExtent(stringFragment2, & w2, & h2);
4174 dc.GetTextExtent(stringFragment3, & w3, & h3);
4175
4176 int kerningDiff = (w1 + w3) - w2;
4177 x = x - kerningDiff;
4178 }
4179 #endif
4180 }
4181
4182 // 2. Selected chunk, if any.
4183 if (selectionRange.GetEnd() >= range.GetStart())
4184 {
4185 int s1 = wxMax(selectionRange.GetStart(), range.GetStart());
4186 int s2 = wxMin(selectionRange.GetEnd(), range.GetEnd());
4187
4188 int fragmentLen = s2 - s1 + 1;
4189 if (fragmentLen < 0)
4190 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1 - offset), (int)fragmentLen);
4191 wxString stringFragment = m_text.Mid(s1 - offset, fragmentLen);
4192
4193 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, true);
4194
4195 #if USE_KERNING_FIX
4196 if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
4197 {
4198 // Compensate for kerning difference
4199 wxString stringFragment2(m_text.Mid(s1 - offset, fragmentLen+1));
4200 wxString stringFragment3(m_text.Mid(s1 - offset + fragmentLen, 1));
4201
4202 wxCoord w1, h1, w2, h2, w3, h3;
4203 dc.GetTextExtent(stringFragment, & w1, & h1);
4204 dc.GetTextExtent(stringFragment2, & w2, & h2);
4205 dc.GetTextExtent(stringFragment3, & w3, & h3);
4206
4207 int kerningDiff = (w1 + w3) - w2;
4208 x = x - kerningDiff;
4209 }
4210 #endif
4211 }
4212
4213 // 3. Remaining unselected chunk, if any
4214 if (selectionRange.GetEnd() < range.GetEnd())
4215 {
4216 int s2 = wxMin(selectionRange.GetEnd()+1, range.GetEnd());
4217 int r2 = range.GetEnd();
4218
4219 int fragmentLen = r2 - s2 + 1;
4220 if (fragmentLen < 0)
4221 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2 - offset), (int)fragmentLen);
4222 wxString stringFragment = m_text.Mid(s2 - offset, fragmentLen);
4223
4224 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
4225 }
4226 }
4227
4228 return true;
4229 }
4230
4231 bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect,wxString& str, wxCoord& x, wxCoord& y, bool selected)
4232 {
4233 bool hasTabs = (str.Find(wxT('\t')) != wxNOT_FOUND);
4234
4235 wxArrayInt tabArray;
4236 int tabCount;
4237 if (hasTabs)
4238 {
4239 if (attr.GetTabs().IsEmpty())
4240 tabArray = wxRichTextParagraph::GetDefaultTabs();
4241 else
4242 tabArray = attr.GetTabs();
4243 tabCount = tabArray.GetCount();
4244
4245 for (int i = 0; i < tabCount; ++i)
4246 {
4247 int pos = tabArray[i];
4248 pos = ConvertTenthsMMToPixels(dc, pos);
4249 tabArray[i] = pos;
4250 }
4251 }
4252 else
4253 tabCount = 0;
4254
4255 int nextTabPos = -1;
4256 int tabPos = -1;
4257 wxCoord w, h;
4258
4259 if (selected)
4260 {
4261 dc.SetBrush(*wxBLACK_BRUSH);
4262 dc.SetPen(*wxBLACK_PEN);
4263 dc.SetTextForeground(*wxWHITE);
4264 dc.SetBackgroundMode(wxTRANSPARENT);
4265 }
4266 else
4267 {
4268 dc.SetTextForeground(attr.GetTextColour());
4269 dc.SetBackgroundMode(wxTRANSPARENT);
4270 }
4271
4272 while (hasTabs)
4273 {
4274 // the string has a tab
4275 // break up the string at the Tab
4276 wxString stringChunk = str.BeforeFirst(wxT('\t'));
4277 str = str.AfterFirst(wxT('\t'));
4278 dc.GetTextExtent(stringChunk, & w, & h);
4279 tabPos = x + w;
4280 bool not_found = true;
4281 for (int i = 0; i < tabCount && not_found; ++i)
4282 {
4283 nextTabPos = tabArray.Item(i);
4284 if (nextTabPos > tabPos)
4285 {
4286 not_found = false;
4287 if (selected)
4288 {
4289 w = nextTabPos - x;
4290 wxRect selRect(x, rect.y, w, rect.GetHeight());
4291 dc.DrawRectangle(selRect);
4292 }
4293 dc.DrawText(stringChunk, x, y);
4294 x = nextTabPos;
4295 }
4296 }
4297 hasTabs = (str.Find(wxT('\t')) != wxNOT_FOUND);
4298 }
4299
4300 if (!str.IsEmpty())
4301 {
4302 dc.GetTextExtent(str, & w, & h);
4303 if (selected)
4304 {
4305 wxRect selRect(x, rect.y, w, rect.GetHeight());
4306 dc.DrawRectangle(selRect);
4307 }
4308 dc.DrawText(str, x, y);
4309 x += w;
4310 }
4311 return true;
4312
4313 }
4314
4315 /// Lay the item out
4316 bool wxRichTextPlainText::Layout(wxDC& dc, const wxRect& WXUNUSED(rect), int WXUNUSED(style))
4317 {
4318 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4319 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
4320 wxASSERT (para != NULL);
4321
4322 wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4323 #else
4324 wxTextAttrEx textAttr(GetAttributes());
4325 #endif
4326
4327 if (textAttr.GetFont().Ok())
4328 dc.SetFont(textAttr.GetFont());
4329
4330 wxCoord w, h;
4331 dc.GetTextExtent(m_text, & w, & h, & m_descent);
4332 m_size = wxSize(w, dc.GetCharHeight());
4333
4334 return true;
4335 }
4336
4337 /// Copy
4338 void wxRichTextPlainText::Copy(const wxRichTextPlainText& obj)
4339 {
4340 wxRichTextObject::Copy(obj);
4341
4342 m_text = obj.m_text;
4343 }
4344
4345 /// Get/set the object size for the given range. Returns false if the range
4346 /// is invalid for this object.
4347 bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int WXUNUSED(flags), wxPoint position) const
4348 {
4349 if (!range.IsWithin(GetRange()))
4350 return false;
4351
4352 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4353 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
4354 wxASSERT (para != NULL);
4355
4356 wxTextAttrEx textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
4357 #else
4358 wxTextAttrEx textAttr(GetAttributes());
4359 #endif
4360
4361 // Always assume unformatted text, since at this level we have no knowledge
4362 // of line breaks - and we don't need it, since we'll calculate size within
4363 // formatted text by doing it in chunks according to the line ranges
4364
4365 if (textAttr.GetFont().Ok())
4366 dc.SetFont(textAttr.GetFont());
4367
4368 int startPos = range.GetStart() - GetRange().GetStart();
4369 long len = range.GetLength();
4370 wxString stringChunk = m_text.Mid(startPos, (size_t) len);
4371 wxCoord w, h;
4372 int width = 0;
4373 if (stringChunk.Find(wxT('\t')) != wxNOT_FOUND)
4374 {
4375 // the string has a tab
4376 wxArrayInt tabArray;
4377 if (textAttr.GetTabs().IsEmpty())
4378 tabArray = wxRichTextParagraph::GetDefaultTabs();
4379 else
4380 tabArray = textAttr.GetTabs();
4381
4382 int tabCount = tabArray.GetCount();
4383
4384 for (int i = 0; i < tabCount; ++i)
4385 {
4386 int pos = tabArray[i];
4387 pos = ((wxRichTextPlainText*) this)->ConvertTenthsMMToPixels(dc, pos);
4388 tabArray[i] = pos;
4389 }
4390
4391 int nextTabPos = -1;
4392
4393 while (stringChunk.Find(wxT('\t')) >= 0)
4394 {
4395 // the string has a tab
4396 // break up the string at the Tab
4397 wxString stringFragment = stringChunk.BeforeFirst(wxT('\t'));
4398 stringChunk = stringChunk.AfterFirst(wxT('\t'));
4399 dc.GetTextExtent(stringFragment, & w, & h);
4400 width += w;
4401 int absoluteWidth = width + position.x;
4402 bool notFound = true;
4403 for (int i = 0; i < tabCount && notFound; ++i)
4404 {
4405 nextTabPos = tabArray.Item(i);
4406 if (nextTabPos > absoluteWidth)
4407 {
4408 notFound = false;
4409 width = nextTabPos - position.x;
4410 }
4411 }
4412 }
4413 }
4414 dc.GetTextExtent(stringChunk, & w, & h, & descent);
4415 width += w;
4416 size = wxSize(width, dc.GetCharHeight());
4417
4418 return true;
4419 }
4420
4421 /// Do a split, returning an object containing the second part, and setting
4422 /// the first part in 'this'.
4423 wxRichTextObject* wxRichTextPlainText::DoSplit(long pos)
4424 {
4425 int index = pos - GetRange().GetStart();
4426 if (index < 0 || index >= (int) m_text.length())
4427 return NULL;
4428
4429 wxString firstPart = m_text.Mid(0, index);
4430 wxString secondPart = m_text.Mid(index);
4431
4432 m_text = firstPart;
4433
4434 wxRichTextPlainText* newObject = new wxRichTextPlainText(secondPart);
4435 newObject->SetAttributes(GetAttributes());
4436
4437 newObject->SetRange(wxRichTextRange(pos, GetRange().GetEnd()));
4438 GetRange().SetEnd(pos-1);
4439
4440 return newObject;
4441 }
4442
4443 /// Calculate range
4444 void wxRichTextPlainText::CalculateRange(long start, long& end)
4445 {
4446 end = start + m_text.length() - 1;
4447 m_range.SetRange(start, end);
4448 }
4449
4450 /// Delete range
4451 bool wxRichTextPlainText::DeleteRange(const wxRichTextRange& range)
4452 {
4453 wxRichTextRange r = range;
4454
4455 r.LimitTo(GetRange());
4456
4457 if (r.GetStart() == GetRange().GetStart() && r.GetEnd() == GetRange().GetEnd())
4458 {
4459 m_text.Empty();
4460 return true;
4461 }
4462
4463 long startIndex = r.GetStart() - GetRange().GetStart();
4464 long len = r.GetLength();
4465
4466 m_text = m_text.Mid(0, startIndex) + m_text.Mid(startIndex+len);
4467 return true;
4468 }
4469
4470 /// Get text for the given range.
4471 wxString wxRichTextPlainText::GetTextForRange(const wxRichTextRange& range) const
4472 {
4473 wxRichTextRange r = range;
4474
4475 r.LimitTo(GetRange());
4476
4477 long startIndex = r.GetStart() - GetRange().GetStart();
4478 long len = r.GetLength();
4479
4480 return m_text.Mid(startIndex, len);
4481 }
4482
4483 /// Returns true if this object can merge itself with the given one.
4484 bool wxRichTextPlainText::CanMerge(wxRichTextObject* object) const
4485 {
4486 return object->GetClassInfo() == CLASSINFO(wxRichTextPlainText) &&
4487 (m_text.empty() || wxTextAttrEq(GetAttributes(), object->GetAttributes()));
4488 }
4489
4490 /// Returns true if this object merged itself with the given one.
4491 /// The calling code will then delete the given object.
4492 bool wxRichTextPlainText::Merge(wxRichTextObject* object)
4493 {
4494 wxRichTextPlainText* textObject = wxDynamicCast(object, wxRichTextPlainText);
4495 wxASSERT( textObject != NULL );
4496
4497 if (textObject)
4498 {
4499 m_text += textObject->GetText();
4500 return true;
4501 }
4502 else
4503 return false;
4504 }
4505
4506 /// Dump to output stream for debugging
4507 void wxRichTextPlainText::Dump(wxTextOutputStream& stream)
4508 {
4509 wxRichTextObject::Dump(stream);
4510 stream << m_text << wxT("\n");
4511 }
4512
4513 /*!
4514 * wxRichTextBuffer
4515 * This is a kind of box, used to represent the whole buffer
4516 */
4517
4518 IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer, wxRichTextParagraphLayoutBox)
4519
4520 wxList wxRichTextBuffer::sm_handlers;
4521 wxRichTextRenderer* wxRichTextBuffer::sm_renderer = NULL;
4522 int wxRichTextBuffer::sm_bulletRightMargin = 20;
4523 float wxRichTextBuffer::sm_bulletProportion = (float) 0.3;
4524
4525 /// Initialisation
4526 void wxRichTextBuffer::Init()
4527 {
4528 m_commandProcessor = new wxCommandProcessor;
4529 m_styleSheet = NULL;
4530 m_modified = false;
4531 m_batchedCommandDepth = 0;
4532 m_batchedCommand = NULL;
4533 m_suppressUndo = 0;
4534 m_handlerFlags = 0;
4535 m_scale = 1.0;
4536 }
4537
4538 /// Initialisation
4539 wxRichTextBuffer::~wxRichTextBuffer()
4540 {
4541 delete m_commandProcessor;
4542 delete m_batchedCommand;
4543
4544 ClearStyleStack();
4545 ClearEventHandlers();
4546 }
4547
4548 void wxRichTextBuffer::Clear()
4549 {
4550 DeleteChildren();
4551 GetCommandProcessor()->ClearCommands();
4552 Modify(false);
4553 Invalidate(wxRICHTEXT_ALL);
4554 }
4555
4556 void wxRichTextBuffer::Reset()
4557 {
4558 DeleteChildren();
4559 AddParagraph(wxEmptyString);
4560 GetCommandProcessor()->ClearCommands();
4561 Modify(false);
4562 Invalidate(wxRICHTEXT_ALL);
4563 }
4564
4565 void wxRichTextBuffer::Copy(const wxRichTextBuffer& obj)
4566 {
4567 wxRichTextParagraphLayoutBox::Copy(obj);
4568
4569 m_styleSheet = obj.m_styleSheet;
4570 m_modified = obj.m_modified;
4571 m_batchedCommandDepth = obj.m_batchedCommandDepth;
4572 m_batchedCommand = obj.m_batchedCommand;
4573 m_suppressUndo = obj.m_suppressUndo;
4574 }
4575
4576 /// Push style sheet to top of stack
4577 bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet* styleSheet)
4578 {
4579 if (m_styleSheet)
4580 styleSheet->InsertSheet(m_styleSheet);
4581
4582 SetStyleSheet(styleSheet);
4583
4584 return true;
4585 }
4586
4587 /// Pop style sheet from top of stack
4588 wxRichTextStyleSheet* wxRichTextBuffer::PopStyleSheet()
4589 {
4590 if (m_styleSheet)
4591 {
4592 wxRichTextStyleSheet* oldSheet = m_styleSheet;
4593 m_styleSheet = oldSheet->GetNextSheet();
4594 oldSheet->Unlink();
4595
4596 return oldSheet;
4597 }
4598 else
4599 return NULL;
4600 }
4601
4602 /// Submit command to insert paragraphs
4603 bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos, const wxRichTextParagraphLayoutBox& paragraphs, wxRichTextCtrl* ctrl, int flags)
4604 {
4605 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
4606
4607 wxTextAttrEx* p = NULL;
4608 wxTextAttrEx paraAttr;
4609 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
4610 {
4611 paraAttr = GetStyleForNewParagraph(pos);
4612 if (!paraAttr.IsDefault())
4613 p = & paraAttr;
4614 }
4615
4616 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4617 wxTextAttrEx attr(GetDefaultStyle());
4618 #else
4619 wxTextAttrEx attr(GetBasicStyle());
4620 wxRichTextApplyStyle(attr, GetDefaultStyle());
4621 #endif
4622
4623 action->GetNewParagraphs() = paragraphs;
4624
4625 if (p)
4626 {
4627 wxRichTextObjectList::compatibility_iterator node = m_children.GetLast();
4628 while (node)
4629 {
4630 wxRichTextParagraph* obj = (wxRichTextParagraph*) node->GetData();
4631 obj->SetAttributes(*p);
4632 node = node->GetPrevious();
4633 }
4634 }
4635
4636 action->SetPosition(pos);
4637
4638 // Set the range we'll need to delete in Undo
4639 action->SetRange(wxRichTextRange(pos, pos + paragraphs.GetRange().GetEnd() - 1));
4640
4641 SubmitAction(action);
4642
4643 return true;
4644 }
4645
4646 /// Submit command to insert the given text
4647 bool wxRichTextBuffer::InsertTextWithUndo(long pos, const wxString& text, wxRichTextCtrl* ctrl, int flags)
4648 {
4649 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
4650
4651 wxTextAttrEx* p = NULL;
4652 wxTextAttrEx paraAttr;
4653 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
4654 {
4655 paraAttr = GetStyleForNewParagraph(pos);
4656 if (!paraAttr.IsDefault())
4657 p = & paraAttr;
4658 }
4659
4660 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4661 wxTextAttrEx attr(GetDefaultStyle());
4662 #else
4663 wxTextAttrEx attr(GetBasicStyle());
4664 wxRichTextApplyStyle(attr, GetDefaultStyle());
4665 #endif
4666
4667 action->GetNewParagraphs().AddParagraphs(text, p);
4668
4669 int length = action->GetNewParagraphs().GetRange().GetLength();
4670
4671 if (text.length() > 0 && text.Last() != wxT('\n'))
4672 {
4673 // Don't count the newline when undoing
4674 length --;
4675 action->GetNewParagraphs().SetPartialParagraph(true);
4676 }
4677
4678 action->SetPosition(pos);
4679
4680 // Set the range we'll need to delete in Undo
4681 action->SetRange(wxRichTextRange(pos, pos + length - 1));
4682
4683 SubmitAction(action);
4684
4685 return true;
4686 }
4687
4688 /// Submit command to insert the given text
4689 bool wxRichTextBuffer::InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int flags)
4690 {
4691 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false);
4692
4693 wxTextAttrEx* p = NULL;
4694 wxTextAttrEx paraAttr;
4695 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
4696 {
4697 paraAttr = GetStyleForNewParagraph(pos);
4698 if (!paraAttr.IsDefault())
4699 p = & paraAttr;
4700 }
4701
4702 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4703 wxTextAttrEx attr(GetDefaultStyle());
4704 #else
4705 wxTextAttrEx attr(GetBasicStyle());
4706 wxRichTextApplyStyle(attr, GetDefaultStyle());
4707 #endif
4708
4709 wxRichTextParagraph* newPara = new wxRichTextParagraph(wxEmptyString, this, & attr);
4710 action->GetNewParagraphs().AppendChild(newPara);
4711 action->GetNewParagraphs().UpdateRanges();
4712 action->GetNewParagraphs().SetPartialParagraph(false);
4713 action->SetPosition(pos);
4714
4715 if (p)
4716 newPara->SetAttributes(*p);
4717
4718 // Set the range we'll need to delete in Undo
4719 action->SetRange(wxRichTextRange(pos, pos));
4720
4721 SubmitAction(action);
4722
4723 return true;
4724 }
4725
4726 /// Submit command to insert the given image
4727 bool wxRichTextBuffer::InsertImageWithUndo(long pos, const wxRichTextImageBlock& imageBlock, wxRichTextCtrl* ctrl, int flags)
4728 {
4729 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, ctrl, false);
4730
4731 wxTextAttrEx* p = NULL;
4732 wxTextAttrEx paraAttr;
4733 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
4734 {
4735 paraAttr = GetStyleForNewParagraph(pos);
4736 if (!paraAttr.IsDefault())
4737 p = & paraAttr;
4738 }
4739
4740 #if wxRICHTEXT_USE_DYNAMIC_STYLES
4741 wxTextAttrEx attr(GetDefaultStyle());
4742 #else
4743 wxTextAttrEx attr(GetBasicStyle());
4744 wxRichTextApplyStyle(attr, GetDefaultStyle());
4745 #endif
4746
4747 wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr);
4748 if (p)
4749 newPara->SetAttributes(*p);
4750
4751 wxRichTextImage* imageObject = new wxRichTextImage(imageBlock, newPara);
4752 newPara->AppendChild(imageObject);
4753 action->GetNewParagraphs().AppendChild(newPara);
4754 action->GetNewParagraphs().UpdateRanges();
4755
4756 action->GetNewParagraphs().SetPartialParagraph(true);
4757
4758 action->SetPosition(pos);
4759
4760 // Set the range we'll need to delete in Undo
4761 action->SetRange(wxRichTextRange(pos, pos));
4762
4763 SubmitAction(action);
4764
4765 return true;
4766 }
4767
4768 /// Get the style that is appropriate for a new paragraph at this position.
4769 /// If the previous paragraph has a paragraph style name, look up the next-paragraph
4770 /// style.
4771 wxRichTextAttr wxRichTextBuffer::GetStyleForNewParagraph(long pos, bool caretPosition) const
4772 {
4773 wxRichTextParagraph* para = GetParagraphAtPosition(pos, caretPosition);
4774 if (para)
4775 {
4776 wxRichTextAttr attr;
4777 bool foundAttributes = false;
4778
4779 // Look for a matching paragraph style
4780 if (!para->GetAttributes().GetParagraphStyleName().IsEmpty() && GetStyleSheet())
4781 {
4782 wxRichTextParagraphStyleDefinition* paraDef = GetStyleSheet()->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
4783 if (paraDef)
4784 {
4785 if (!paraDef->GetNextStyle().IsEmpty())
4786 {
4787 wxRichTextParagraphStyleDefinition* nextParaDef = GetStyleSheet()->FindParagraphStyle(paraDef->GetNextStyle());
4788 if (nextParaDef)
4789 {
4790 foundAttributes = true;
4791 attr = nextParaDef->GetStyle();
4792 }
4793 }
4794
4795 // If we didn't find the 'next style', use this style instead.
4796 if (!foundAttributes)
4797 {
4798 foundAttributes = true;
4799 attr = paraDef->GetStyle();
4800 }
4801 }
4802 }
4803 if (!foundAttributes)
4804 {
4805 attr = para->GetAttributes();
4806 int flags = attr.GetFlags();
4807
4808 // Eliminate character styles
4809 flags &= ( (~ wxTEXT_ATTR_FONT) |
4810 (~ wxTEXT_ATTR_TEXT_COLOUR) |
4811 (~ wxTEXT_ATTR_BACKGROUND_COLOUR) );
4812 attr.SetFlags(flags);
4813 }
4814
4815 // Now see if we need to number the paragraph.
4816 if (attr.HasBulletStyle())
4817 {
4818 wxRichTextAttr numberingAttr;
4819 if (FindNextParagraphNumber(para, numberingAttr))
4820 wxRichTextApplyStyle(attr, numberingAttr);
4821 }
4822
4823 return attr;
4824 }
4825 else
4826 return wxRichTextAttr();
4827 }
4828
4829 /// Submit command to delete this range
4830 bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange& range, long initialCaretPosition, long WXUNUSED(newCaretPositon), wxRichTextCtrl* ctrl)
4831 {
4832 wxRichTextAction* action = new wxRichTextAction(NULL, _("Delete"), wxRICHTEXT_DELETE, this, ctrl);
4833
4834 action->SetPosition(initialCaretPosition);
4835
4836 // Set the range to delete
4837 action->SetRange(range);
4838
4839 // Copy the fragment that we'll need to restore in Undo
4840 CopyFragment(range, action->GetOldParagraphs());
4841
4842 // Special case: if there is only one (non-partial) paragraph,
4843 // we must save the *next* paragraph's style, because that
4844 // is the style we must apply when inserting the content back
4845 // when undoing the delete. (This is because we're merging the
4846 // paragraph with the previous paragraph and throwing away
4847 // the style, and we need to restore it.)
4848 if (!action->GetOldParagraphs().GetPartialParagraph() && action->GetOldParagraphs().GetChildCount() == 1)
4849 {
4850 wxRichTextParagraph* lastPara = GetParagraphAtPosition(range.GetStart());
4851 if (lastPara)
4852 {
4853 wxRichTextParagraph* nextPara = GetParagraphAtPosition(range.GetEnd()+1);
4854 if (nextPara)
4855 {
4856 wxRichTextParagraph* para = (wxRichTextParagraph*) action->GetOldParagraphs().GetChild(0);
4857 para->SetAttributes(nextPara->GetAttributes());
4858 }
4859 }
4860 }
4861
4862 SubmitAction(action);
4863
4864 return true;
4865 }
4866
4867 /// Collapse undo/redo commands
4868 bool wxRichTextBuffer::BeginBatchUndo(const wxString& cmdName)
4869 {
4870 if (m_batchedCommandDepth == 0)
4871 {
4872 wxASSERT(m_batchedCommand == NULL);
4873 if (m_batchedCommand)
4874 {
4875 GetCommandProcessor()->Submit(m_batchedCommand);
4876 }
4877 m_batchedCommand = new wxRichTextCommand(cmdName);
4878 }
4879
4880 m_batchedCommandDepth ++;
4881
4882 return true;
4883 }
4884
4885 /// Collapse undo/redo commands
4886 bool wxRichTextBuffer::EndBatchUndo()
4887 {
4888 m_batchedCommandDepth --;
4889
4890 wxASSERT(m_batchedCommandDepth >= 0);
4891 wxASSERT(m_batchedCommand != NULL);
4892
4893 if (m_batchedCommandDepth == 0)
4894 {
4895 GetCommandProcessor()->Submit(m_batchedCommand);
4896 m_batchedCommand = NULL;
4897 }
4898
4899 return true;
4900 }
4901
4902 /// Submit immediately, or delay according to whether collapsing is on
4903 bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action)
4904 {
4905 if (BatchingUndo() && m_batchedCommand && !SuppressingUndo())
4906 m_batchedCommand->AddAction(action);
4907 else
4908 {
4909 wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName());
4910 cmd->AddAction(action);
4911
4912 // Only store it if we're not suppressing undo.
4913 return GetCommandProcessor()->Submit(cmd, !SuppressingUndo());
4914 }
4915
4916 return true;
4917 }
4918
4919 /// Begin suppressing undo/redo commands.
4920 bool wxRichTextBuffer::BeginSuppressUndo()
4921 {
4922 m_suppressUndo ++;
4923
4924 return true;
4925 }
4926
4927 /// End suppressing undo/redo commands.
4928 bool wxRichTextBuffer::EndSuppressUndo()
4929 {
4930 m_suppressUndo --;
4931
4932 return true;
4933 }
4934
4935 /// Begin using a style
4936 bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx& style)
4937 {
4938 wxTextAttrEx newStyle(GetDefaultStyle());
4939
4940 // Save the old default style
4941 m_attributeStack.Append((wxObject*) new wxTextAttrEx(GetDefaultStyle()));
4942
4943 wxRichTextApplyStyle(newStyle, style);
4944 newStyle.SetFlags(style.GetFlags()|newStyle.GetFlags());
4945
4946 SetDefaultStyle(newStyle);
4947
4948 // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize());
4949
4950 return true;
4951 }
4952
4953 /// End the style
4954 bool wxRichTextBuffer::EndStyle()
4955 {
4956 if (!m_attributeStack.GetFirst())
4957 {
4958 wxLogDebug(_("Too many EndStyle calls!"));
4959 return false;
4960 }
4961
4962 wxList::compatibility_iterator node = m_attributeStack.GetLast();
4963 wxTextAttrEx* attr = (wxTextAttrEx*)node->GetData();
4964 m_attributeStack.Erase(node);
4965
4966 SetDefaultStyle(*attr);
4967
4968 delete attr;
4969 return true;
4970 }
4971
4972 /// End all styles
4973 bool wxRichTextBuffer::EndAllStyles()
4974 {
4975 while (m_attributeStack.GetCount() != 0)
4976 EndStyle();
4977 return true;
4978 }
4979
4980 /// Clear the style stack
4981 void wxRichTextBuffer::ClearStyleStack()
4982 {
4983 for (wxList::compatibility_iterator node = m_attributeStack.GetFirst(); node; node = node->GetNext())
4984 delete (wxTextAttrEx*) node->GetData();
4985 m_attributeStack.Clear();
4986 }
4987
4988 /// Begin using bold
4989 bool wxRichTextBuffer::BeginBold()
4990 {
4991 wxFont font(GetBasicStyle().GetFont());
4992 font.SetWeight(wxBOLD);
4993
4994 wxTextAttrEx attr;
4995 attr.SetFont(font,wxTEXT_ATTR_FONT_WEIGHT);
4996
4997 return BeginStyle(attr);
4998 }
4999
5000 /// Begin using italic
5001 bool wxRichTextBuffer::BeginItalic()
5002 {
5003 wxFont font(GetBasicStyle().GetFont());
5004 font.SetStyle(wxITALIC);
5005
5006 wxTextAttrEx attr;
5007 attr.SetFont(font, wxTEXT_ATTR_FONT_ITALIC);
5008
5009 return BeginStyle(attr);
5010 }
5011
5012 /// Begin using underline
5013 bool wxRichTextBuffer::BeginUnderline()
5014 {
5015 wxFont font(GetBasicStyle().GetFont());
5016 font.SetUnderlined(true);
5017
5018 wxTextAttrEx attr;
5019 attr.SetFont(font, wxTEXT_ATTR_FONT_UNDERLINE);
5020
5021 return BeginStyle(attr);
5022 }
5023
5024 /// Begin using point size
5025 bool wxRichTextBuffer::BeginFontSize(int pointSize)
5026 {
5027 wxFont font(GetBasicStyle().GetFont());
5028 font.SetPointSize(pointSize);
5029
5030 wxTextAttrEx attr;
5031 attr.SetFont(font, wxTEXT_ATTR_FONT_SIZE);
5032
5033 return BeginStyle(attr);
5034 }
5035
5036 /// Begin using this font
5037 bool wxRichTextBuffer::BeginFont(const wxFont& font)
5038 {
5039 wxTextAttrEx attr;
5040 attr.SetFlags(wxTEXT_ATTR_FONT);
5041 attr.SetFont(font);
5042
5043 return BeginStyle(attr);
5044 }
5045
5046 /// Begin using this colour
5047 bool wxRichTextBuffer::BeginTextColour(const wxColour& colour)
5048 {
5049 wxTextAttrEx attr;
5050 attr.SetFlags(wxTEXT_ATTR_TEXT_COLOUR);
5051 attr.SetTextColour(colour);
5052
5053 return BeginStyle(attr);
5054 }
5055
5056 /// Begin using alignment
5057 bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment)
5058 {
5059 wxTextAttrEx attr;
5060 attr.SetFlags(wxTEXT_ATTR_ALIGNMENT);
5061 attr.SetAlignment(alignment);
5062
5063 return BeginStyle(attr);
5064 }
5065
5066 /// Begin left indent
5067 bool wxRichTextBuffer::BeginLeftIndent(int leftIndent, int leftSubIndent)
5068 {
5069 wxTextAttrEx attr;
5070 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
5071 attr.SetLeftIndent(leftIndent, leftSubIndent);
5072
5073 return BeginStyle(attr);
5074 }
5075
5076 /// Begin right indent
5077 bool wxRichTextBuffer::BeginRightIndent(int rightIndent)
5078 {
5079 wxTextAttrEx attr;
5080 attr.SetFlags(wxTEXT_ATTR_RIGHT_INDENT);
5081 attr.SetRightIndent(rightIndent);
5082
5083 return BeginStyle(attr);
5084 }
5085
5086 /// Begin paragraph spacing
5087 bool wxRichTextBuffer::BeginParagraphSpacing(int before, int after)
5088 {
5089 long flags = 0;
5090 if (before != 0)
5091 flags |= wxTEXT_ATTR_PARA_SPACING_BEFORE;
5092 if (after != 0)
5093 flags |= wxTEXT_ATTR_PARA_SPACING_AFTER;
5094
5095 wxTextAttrEx attr;
5096 attr.SetFlags(flags);
5097 attr.SetParagraphSpacingBefore(before);
5098 attr.SetParagraphSpacingAfter(after);
5099
5100 return BeginStyle(attr);
5101 }
5102
5103 /// Begin line spacing
5104 bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing)
5105 {
5106 wxTextAttrEx attr;
5107 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
5108 attr.SetLineSpacing(lineSpacing);
5109
5110 return BeginStyle(attr);
5111 }
5112
5113 /// Begin numbered bullet
5114 bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber, int leftIndent, int leftSubIndent, int bulletStyle)
5115 {
5116 wxTextAttrEx attr;
5117 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_LEFT_INDENT);
5118 attr.SetBulletStyle(bulletStyle);
5119 attr.SetBulletNumber(bulletNumber);
5120 attr.SetLeftIndent(leftIndent, leftSubIndent);
5121
5122 return BeginStyle(attr);
5123 }
5124
5125 /// Begin symbol bullet
5126 bool wxRichTextBuffer::BeginSymbolBullet(const wxString& symbol, int leftIndent, int leftSubIndent, int bulletStyle)
5127 {
5128 wxTextAttrEx attr;
5129 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_LEFT_INDENT);
5130 attr.SetBulletStyle(bulletStyle);
5131 attr.SetLeftIndent(leftIndent, leftSubIndent);
5132 attr.SetBulletText(symbol);
5133
5134 return BeginStyle(attr);
5135 }
5136
5137 /// Begin standard bullet
5138 bool wxRichTextBuffer::BeginStandardBullet(const wxString& bulletName, int leftIndent, int leftSubIndent, int bulletStyle)
5139 {
5140 wxTextAttrEx attr;
5141 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_LEFT_INDENT);
5142 attr.SetBulletStyle(bulletStyle);
5143 attr.SetLeftIndent(leftIndent, leftSubIndent);
5144 attr.SetBulletName(bulletName);
5145
5146 return BeginStyle(attr);
5147 }
5148
5149 /// Begin named character style
5150 bool wxRichTextBuffer::BeginCharacterStyle(const wxString& characterStyle)
5151 {
5152 if (GetStyleSheet())
5153 {
5154 wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle);
5155 if (def)
5156 {
5157 wxTextAttrEx attr;
5158 def->GetStyle().CopyTo(attr);
5159 return BeginStyle(attr);
5160 }
5161 }
5162 return false;
5163 }
5164
5165 /// Begin named paragraph style
5166 bool wxRichTextBuffer::BeginParagraphStyle(const wxString& paragraphStyle)
5167 {
5168 if (GetStyleSheet())
5169 {
5170 wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(paragraphStyle);
5171 if (def)
5172 {
5173 wxTextAttrEx attr;
5174 def->GetStyle().CopyTo(attr);
5175 return BeginStyle(attr);
5176 }
5177 }
5178 return false;
5179 }
5180
5181 /// Begin named list style
5182 bool wxRichTextBuffer::BeginListStyle(const wxString& listStyle, int level, int number)
5183 {
5184 if (GetStyleSheet())
5185 {
5186 wxRichTextListStyleDefinition* def = GetStyleSheet()->FindListStyle(listStyle);
5187 if (def)
5188 {
5189 wxTextAttrEx attr(def->GetCombinedStyleForLevel(level));
5190
5191 attr.SetBulletNumber(number);
5192
5193 return BeginStyle(attr);
5194 }
5195 }
5196 return false;
5197 }
5198
5199 /// Begin URL
5200 bool wxRichTextBuffer::BeginURL(const wxString& url, const wxString& characterStyle)
5201 {
5202 wxTextAttrEx attr;
5203
5204 if (!characterStyle.IsEmpty() && GetStyleSheet())
5205 {
5206 wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle);
5207 if (def)
5208 {
5209 def->GetStyle().CopyTo(attr);
5210 }
5211 }
5212 attr.SetURL(url);
5213
5214 return BeginStyle(attr);
5215 }
5216
5217 /// Adds a handler to the end
5218 void wxRichTextBuffer::AddHandler(wxRichTextFileHandler *handler)
5219 {
5220 sm_handlers.Append(handler);
5221 }
5222
5223 /// Inserts a handler at the front
5224 void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler *handler)
5225 {
5226 sm_handlers.Insert( handler );
5227 }
5228
5229 /// Removes a handler
5230 bool wxRichTextBuffer::RemoveHandler(const wxString& name)
5231 {
5232 wxRichTextFileHandler *handler = FindHandler(name);
5233 if (handler)
5234 {
5235 sm_handlers.DeleteObject(handler);
5236 delete handler;
5237 return true;
5238 }
5239 else
5240 return false;
5241 }
5242
5243 /// Finds a handler by filename or, if supplied, type
5244 wxRichTextFileHandler *wxRichTextBuffer::FindHandlerFilenameOrType(const wxString& filename, int imageType)
5245 {
5246 if (imageType != wxRICHTEXT_TYPE_ANY)
5247 return FindHandler(imageType);
5248 else if (!filename.IsEmpty())
5249 {
5250 wxString path, file, ext;
5251 wxSplitPath(filename, & path, & file, & ext);
5252 return FindHandler(ext, imageType);
5253 }
5254 else
5255 return NULL;
5256 }
5257
5258
5259 /// Finds a handler by name
5260 wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& name)
5261 {
5262 wxList::compatibility_iterator node = sm_handlers.GetFirst();
5263 while (node)
5264 {
5265 wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData();
5266 if (handler->GetName().Lower() == name.Lower()) return handler;
5267
5268 node = node->GetNext();
5269 }
5270 return NULL;
5271 }
5272
5273 /// Finds a handler by extension and type
5274 wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& extension, int type)
5275 {
5276 wxList::compatibility_iterator node = sm_handlers.GetFirst();
5277 while (node)
5278 {
5279 wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData();
5280 if ( handler->GetExtension().Lower() == extension.Lower() &&
5281 (type == wxRICHTEXT_TYPE_ANY || handler->GetType() == type) )
5282 return handler;
5283 node = node->GetNext();
5284 }
5285 return 0;
5286 }
5287
5288 /// Finds a handler by type
5289 wxRichTextFileHandler* wxRichTextBuffer::FindHandler(int type)
5290 {
5291 wxList::compatibility_iterator node = sm_handlers.GetFirst();
5292 while (node)
5293 {
5294 wxRichTextFileHandler *handler = (wxRichTextFileHandler *)node->GetData();
5295 if (handler->GetType() == type) return handler;
5296 node = node->GetNext();
5297 }
5298 return NULL;
5299 }
5300
5301 void wxRichTextBuffer::InitStandardHandlers()
5302 {
5303 if (!FindHandler(wxRICHTEXT_TYPE_TEXT))
5304 AddHandler(new wxRichTextPlainTextHandler);
5305 }
5306
5307 void wxRichTextBuffer::CleanUpHandlers()
5308 {
5309 wxList::compatibility_iterator node = sm_handlers.GetFirst();
5310 while (node)
5311 {
5312 wxRichTextFileHandler* handler = (wxRichTextFileHandler*)node->GetData();
5313 wxList::compatibility_iterator next = node->GetNext();
5314 delete handler;
5315 node = next;
5316 }
5317
5318 sm_handlers.Clear();
5319 }
5320
5321 wxString wxRichTextBuffer::GetExtWildcard(bool combine, bool save, wxArrayInt* types)
5322 {
5323 if (types)
5324 types->Clear();
5325
5326 wxString wildcard;
5327
5328 wxList::compatibility_iterator node = GetHandlers().GetFirst();
5329 int count = 0;
5330 while (node)
5331 {
5332 wxRichTextFileHandler* handler = (wxRichTextFileHandler*) node->GetData();
5333 if (handler->IsVisible() && ((save && handler->CanSave()) || !save && handler->CanLoad()))
5334 {
5335 if (combine)
5336 {
5337 if (count > 0)
5338 wildcard += wxT(";");
5339 wildcard += wxT("*.") + handler->GetExtension();
5340 }
5341 else
5342 {
5343 if (count > 0)
5344 wildcard += wxT("|");
5345 wildcard += handler->GetName();
5346 wildcard += wxT(" ");
5347 wildcard += _("files");
5348 wildcard += wxT(" (*.");
5349 wildcard += handler->GetExtension();
5350 wildcard += wxT(")|*.");
5351 wildcard += handler->GetExtension();
5352 if (types)
5353 types->Add(handler->GetType());
5354 }
5355 count ++;
5356 }
5357
5358 node = node->GetNext();
5359 }
5360
5361 if (combine)
5362 wildcard = wxT("(") + wildcard + wxT(")|") + wildcard;
5363 return wildcard;
5364 }
5365
5366 /// Load a file
5367 bool wxRichTextBuffer::LoadFile(const wxString& filename, int type)
5368 {
5369 wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type);
5370 if (handler)
5371 {
5372 SetDefaultStyle(wxTextAttrEx());
5373 handler->SetFlags(GetHandlerFlags());
5374 bool success = handler->LoadFile(this, filename);
5375 Invalidate(wxRICHTEXT_ALL);
5376 return success;
5377 }
5378 else
5379 return false;
5380 }
5381
5382 /// Save a file
5383 bool wxRichTextBuffer::SaveFile(const wxString& filename, int type)
5384 {
5385 wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type);
5386 if (handler)
5387 {
5388 handler->SetFlags(GetHandlerFlags());
5389 return handler->SaveFile(this, filename);
5390 }
5391 else
5392 return false;
5393 }
5394
5395 /// Load from a stream
5396 bool wxRichTextBuffer::LoadFile(wxInputStream& stream, int type)
5397 {
5398 wxRichTextFileHandler* handler = FindHandler(type);
5399 if (handler)
5400 {
5401 SetDefaultStyle(wxTextAttrEx());
5402 handler->SetFlags(GetHandlerFlags());
5403 bool success = handler->LoadFile(this, stream);
5404 Invalidate(wxRICHTEXT_ALL);
5405 return success;
5406 }
5407 else
5408 return false;
5409 }
5410
5411 /// Save to a stream
5412 bool wxRichTextBuffer::SaveFile(wxOutputStream& stream, int type)
5413 {
5414 wxRichTextFileHandler* handler = FindHandler(type);
5415 if (handler)
5416 {
5417 handler->SetFlags(GetHandlerFlags());
5418 return handler->SaveFile(this, stream);
5419 }
5420 else
5421 return false;
5422 }
5423
5424 /// Copy the range to the clipboard
5425 bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange& range)
5426 {
5427 bool success = false;
5428 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5429
5430 if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open())
5431 {
5432 wxTheClipboard->Clear();
5433
5434 // Add composite object
5435
5436 wxDataObjectComposite* compositeObject = new wxDataObjectComposite();
5437
5438 {
5439 wxString text = GetTextForRange(range);
5440
5441 #ifdef __WXMSW__
5442 text = wxTextFile::Translate(text, wxTextFileType_Dos);
5443 #endif
5444
5445 compositeObject->Add(new wxTextDataObject(text), false /* not preferred */);
5446 }
5447
5448 // Add rich text buffer data object. This needs the XML handler to be present.
5449
5450 if (FindHandler(wxRICHTEXT_TYPE_XML))
5451 {
5452 wxRichTextBuffer* richTextBuf = new wxRichTextBuffer;
5453 CopyFragment(range, *richTextBuf);
5454
5455 compositeObject->Add(new wxRichTextBufferDataObject(richTextBuf), true /* preferred */);
5456 }
5457
5458 if (wxTheClipboard->SetData(compositeObject))
5459 success = true;
5460
5461 wxTheClipboard->Close();
5462 }
5463
5464 #else
5465 wxUnusedVar(range);
5466 #endif
5467 return success;
5468 }
5469
5470 /// Paste the clipboard content to the buffer
5471 bool wxRichTextBuffer::PasteFromClipboard(long position)
5472 {
5473 bool success = false;
5474 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5475 if (CanPasteFromClipboard())
5476 {
5477 if (wxTheClipboard->Open())
5478 {
5479 if (wxTheClipboard->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
5480 {
5481 wxRichTextBufferDataObject data;
5482 wxTheClipboard->GetData(data);
5483 wxRichTextBuffer* richTextBuffer = data.GetRichTextBuffer();
5484 if (richTextBuffer)
5485 {
5486 InsertParagraphsWithUndo(position+1, *richTextBuffer, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
5487 delete richTextBuffer;
5488 }
5489 }
5490 else if (wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_UNICODETEXT))
5491 {
5492 wxTextDataObject data;
5493 wxTheClipboard->GetData(data);
5494 wxString text(data.GetText());
5495 text.Replace(_T("\r\n"), _T("\n"));
5496
5497 InsertTextWithUndo(position+1, text, GetRichTextCtrl());
5498
5499 success = true;
5500 }
5501 else if (wxTheClipboard->IsSupported(wxDF_BITMAP))
5502 {
5503 wxBitmapDataObject data;
5504 wxTheClipboard->GetData(data);
5505 wxBitmap bitmap(data.GetBitmap());
5506 wxImage image(bitmap.ConvertToImage());
5507
5508 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, GetRichTextCtrl(), false);
5509
5510 action->GetNewParagraphs().AddImage(image);
5511
5512 if (action->GetNewParagraphs().GetChildCount() == 1)
5513 action->GetNewParagraphs().SetPartialParagraph(true);
5514
5515 action->SetPosition(position);
5516
5517 // Set the range we'll need to delete in Undo
5518 action->SetRange(wxRichTextRange(position, position));
5519
5520 SubmitAction(action);
5521
5522 success = true;
5523 }
5524 wxTheClipboard->Close();
5525 }
5526 }
5527 #else
5528 wxUnusedVar(position);
5529 #endif
5530 return success;
5531 }
5532
5533 /// Can we paste from the clipboard?
5534 bool wxRichTextBuffer::CanPasteFromClipboard() const
5535 {
5536 bool canPaste = false;
5537 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5538 if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open())
5539 {
5540 if (wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_UNICODETEXT) ||
5541 wxTheClipboard->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
5542 wxTheClipboard->IsSupported(wxDF_BITMAP))
5543 {
5544 canPaste = true;
5545 }
5546 wxTheClipboard->Close();
5547 }
5548 #endif
5549 return canPaste;
5550 }
5551
5552 /// Dumps contents of buffer for debugging purposes
5553 void wxRichTextBuffer::Dump()
5554 {
5555 wxString text;
5556 {
5557 wxStringOutputStream stream(& text);
5558 wxTextOutputStream textStream(stream);
5559 Dump(textStream);
5560 }
5561
5562 wxLogDebug(text);
5563 }
5564
5565 /// Add an event handler
5566 bool wxRichTextBuffer::AddEventHandler(wxEvtHandler* handler)
5567 {
5568 m_eventHandlers.Append(handler);
5569 return true;
5570 }
5571
5572 /// Remove an event handler
5573 bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler* handler, bool deleteHandler)
5574 {
5575 wxList::compatibility_iterator node = m_eventHandlers.Find(handler);
5576 if (node)
5577 {
5578 m_eventHandlers.Erase(node);
5579 if (deleteHandler)
5580 delete handler;
5581
5582 return true;
5583 }
5584 else
5585 return false;
5586 }
5587
5588 /// Clear event handlers
5589 void wxRichTextBuffer::ClearEventHandlers()
5590 {
5591 m_eventHandlers.Clear();
5592 }
5593
5594 /// Send event to event handlers. If sendToAll is true, will send to all event handlers,
5595 /// otherwise will stop at the first successful one.
5596 bool wxRichTextBuffer::SendEvent(wxEvent& event, bool sendToAll)
5597 {
5598 bool success = false;
5599 for (wxList::compatibility_iterator node = m_eventHandlers.GetFirst(); node; node = node->GetNext())
5600 {
5601 wxEvtHandler* handler = (wxEvtHandler*) node->GetData();
5602 if (handler->ProcessEvent(event))
5603 {
5604 success = true;
5605 if (!sendToAll)
5606 return true;
5607 }
5608 }
5609 return success;
5610 }
5611
5612 /// Set style sheet and notify of the change
5613 bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet* sheet)
5614 {
5615 wxRichTextStyleSheet* oldSheet = GetStyleSheet();
5616
5617 wxWindowID id = wxID_ANY;
5618 if (GetRichTextCtrl())
5619 id = GetRichTextCtrl()->GetId();
5620
5621 wxRichTextEvent event(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, id);
5622 event.SetEventObject(GetRichTextCtrl());
5623 event.SetOldStyleSheet(oldSheet);
5624 event.SetNewStyleSheet(sheet);
5625 event.Allow();
5626
5627 if (SendEvent(event) && !event.IsAllowed())
5628 {
5629 if (sheet != oldSheet)
5630 delete sheet;
5631
5632 return false;
5633 }
5634
5635 if (oldSheet && oldSheet != sheet)
5636 delete oldSheet;
5637
5638 SetStyleSheet(sheet);
5639
5640 event.SetEventType(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED);
5641 event.SetOldStyleSheet(NULL);
5642 event.Allow();
5643
5644 return SendEvent(event);
5645 }
5646
5647 /// Set renderer, deleting old one
5648 void wxRichTextBuffer::SetRenderer(wxRichTextRenderer* renderer)
5649 {
5650 if (sm_renderer)
5651 delete sm_renderer;
5652 sm_renderer = renderer;
5653 }
5654
5655 bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& bulletAttr, const wxRect& rect)
5656 {
5657 if (bulletAttr.GetTextColour().Ok())
5658 {
5659 dc.SetPen(wxPen(bulletAttr.GetTextColour()));
5660 dc.SetBrush(wxBrush(bulletAttr.GetTextColour()));
5661 }
5662 else
5663 {
5664 dc.SetPen(*wxBLACK_PEN);
5665 dc.SetBrush(*wxBLACK_BRUSH);
5666 }
5667
5668 wxFont font;
5669 if (bulletAttr.GetFont().Ok())
5670 font = bulletAttr.GetFont();
5671 else
5672 font = (*wxNORMAL_FONT);
5673
5674 dc.SetFont(font);
5675
5676 int charHeight = dc.GetCharHeight();
5677
5678 int bulletWidth = (int) (((float) charHeight) * wxRichTextBuffer::GetBulletProportion());
5679 int bulletHeight = bulletWidth;
5680
5681 int x = rect.x;
5682
5683 // Calculate the top position of the character (as opposed to the whole line height)
5684 int y = rect.y + (rect.height - charHeight);
5685
5686 // Calculate where the bullet should be positioned
5687 y = y + (charHeight+1)/2 - (bulletHeight+1)/2;
5688
5689 // The margin between a bullet and text.
5690 int margin = paragraph->ConvertTenthsMMToPixels(dc, wxRichTextBuffer::GetBulletRightMargin());
5691
5692 if (bulletAttr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT)
5693 x = rect.x + rect.width - bulletWidth - margin;
5694 else if (bulletAttr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE)
5695 x = x + (rect.width)/2 - bulletWidth/2;
5696
5697 if (bulletAttr.GetBulletName() == wxT("standard/square"))
5698 {
5699 dc.DrawRectangle(x, y, bulletWidth, bulletHeight);
5700 }
5701 else if (bulletAttr.GetBulletName() == wxT("standard/diamond"))
5702 {
5703 wxPoint pts[5];
5704 pts[0].x = x; pts[0].y = y + bulletHeight/2;
5705 pts[1].x = x + bulletWidth/2; pts[1].y = y;
5706 pts[2].x = x + bulletWidth; pts[2].y = y + bulletHeight/2;
5707 pts[3].x = x + bulletWidth/2; pts[3].y = y + bulletHeight;
5708
5709 dc.DrawPolygon(4, pts);
5710 }
5711 else if (bulletAttr.GetBulletName() == wxT("standard/triangle"))
5712 {
5713 wxPoint pts[3];
5714 pts[0].x = x; pts[0].y = y;
5715 pts[1].x = x + bulletWidth; pts[1].y = y + bulletHeight/2;
5716 pts[2].x = x; pts[2].y = y + bulletHeight;
5717
5718 dc.DrawPolygon(3, pts);
5719 }
5720 else // "standard/circle", and catch-all
5721 {
5722 dc.DrawEllipse(x, y, bulletWidth, bulletHeight);
5723 }
5724
5725 return true;
5726 }
5727
5728 bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxTextAttrEx& attr, const wxRect& rect, const wxString& text)
5729 {
5730 if (!text.empty())
5731 {
5732 wxFont font;
5733 if ((attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL) && !attr.GetBulletFont().IsEmpty() && attr.GetFont().Ok())
5734 {
5735 font = (*wxTheFontList->FindOrCreateFont(attr.GetFont().GetPointSize(), attr.GetFont().GetFamily(),
5736 attr.GetFont().GetStyle(), attr.GetFont().GetWeight(), attr.GetFont().GetUnderlined(),
5737 attr.GetBulletFont()));
5738 }
5739 else if (attr.GetFont().Ok())
5740 font = attr.GetFont();
5741 else
5742 font = (*wxNORMAL_FONT);
5743
5744 dc.SetFont(font);
5745
5746 if (attr.GetTextColour().Ok())
5747 dc.SetTextForeground(attr.GetTextColour());
5748
5749 dc.SetBackgroundMode(wxTRANSPARENT);
5750
5751 int charHeight = dc.GetCharHeight();
5752 wxCoord tw, th;
5753 dc.GetTextExtent(text, & tw, & th);
5754
5755 int x = rect.x;
5756
5757 // Calculate the top position of the character (as opposed to the whole line height)
5758 int y = rect.y + (rect.height - charHeight);
5759
5760 // The margin between a bullet and text.
5761 int margin = paragraph->ConvertTenthsMMToPixels(dc, wxRichTextBuffer::GetBulletRightMargin());
5762
5763 if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT)
5764 x = (rect.x + rect.width) - tw - margin;
5765 else if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE)
5766 x = x + (rect.width)/2 - tw/2;
5767
5768 dc.DrawText(text, x, y);
5769
5770 return true;
5771 }
5772 else
5773 return false;
5774 }
5775
5776 bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph* WXUNUSED(paragraph), wxDC& WXUNUSED(dc), const wxTextAttrEx& WXUNUSED(attr), const wxRect& WXUNUSED(rect))
5777 {
5778 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
5779 // with the buffer. The store will allow retrieval from memory, disk or other means.
5780 return false;
5781 }
5782
5783 /// Enumerate the standard bullet names currently supported
5784 bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString& bulletNames)
5785 {
5786 bulletNames.Add(wxT("standard/circle"));
5787 bulletNames.Add(wxT("standard/square"));
5788 bulletNames.Add(wxT("standard/diamond"));
5789 bulletNames.Add(wxT("standard/triangle"));
5790
5791 return true;
5792 }
5793
5794 /*
5795 * Module to initialise and clean up handlers
5796 */
5797
5798 class wxRichTextModule: public wxModule
5799 {
5800 DECLARE_DYNAMIC_CLASS(wxRichTextModule)
5801 public:
5802 wxRichTextModule() {}
5803 bool OnInit()
5804 {
5805 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer);
5806 wxRichTextBuffer::InitStandardHandlers();
5807 wxRichTextParagraph::InitDefaultTabs();
5808 return true;
5809 };
5810 void OnExit()
5811 {
5812 wxRichTextBuffer::CleanUpHandlers();
5813 wxRichTextDecimalToRoman(-1);
5814 wxRichTextParagraph::ClearDefaultTabs();
5815 wxRichTextCtrl::ClearAvailableFontNames();
5816 wxRichTextBuffer::SetRenderer(NULL);
5817 };
5818 };
5819
5820 IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule, wxModule)
5821
5822
5823 // If the richtext lib is dynamically loaded after the app has already started
5824 // (such as from wxPython) then the built-in module system will not init this
5825 // module. Provide this function to do it manually.
5826 void wxRichTextModuleInit()
5827 {
5828 wxModule* module = new wxRichTextModule;
5829 module->Init();
5830 wxModule::RegisterModule(module);
5831 }
5832
5833
5834 /*!
5835 * Commands for undo/redo
5836 *
5837 */
5838
5839 wxRichTextCommand::wxRichTextCommand(const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
5840 wxRichTextCtrl* ctrl, bool ignoreFirstTime): wxCommand(true, name)
5841 {
5842 /* wxRichTextAction* action = */ new wxRichTextAction(this, name, id, buffer, ctrl, ignoreFirstTime);
5843 }
5844
5845 wxRichTextCommand::wxRichTextCommand(const wxString& name): wxCommand(true, name)
5846 {
5847 }
5848
5849 wxRichTextCommand::~wxRichTextCommand()
5850 {
5851 ClearActions();
5852 }
5853
5854 void wxRichTextCommand::AddAction(wxRichTextAction* action)
5855 {
5856 if (!m_actions.Member(action))
5857 m_actions.Append(action);
5858 }
5859
5860 bool wxRichTextCommand::Do()
5861 {
5862 for (wxList::compatibility_iterator node = m_actions.GetFirst(); node; node = node->GetNext())
5863 {
5864 wxRichTextAction* action = (wxRichTextAction*) node->GetData();
5865 action->Do();
5866 }
5867
5868 return true;
5869 }
5870
5871 bool wxRichTextCommand::Undo()
5872 {
5873 for (wxList::compatibility_iterator node = m_actions.GetLast(); node; node = node->GetPrevious())
5874 {
5875 wxRichTextAction* action = (wxRichTextAction*) node->GetData();
5876 action->Undo();
5877 }
5878
5879 return true;
5880 }
5881
5882 void wxRichTextCommand::ClearActions()
5883 {
5884 WX_CLEAR_LIST(wxList, m_actions);
5885 }
5886
5887 /*!
5888 * Individual action
5889 *
5890 */
5891
5892 wxRichTextAction::wxRichTextAction(wxRichTextCommand* cmd, const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
5893 wxRichTextCtrl* ctrl, bool ignoreFirstTime)
5894 {
5895 m_buffer = buffer;
5896 m_ignoreThis = ignoreFirstTime;
5897 m_cmdId = id;
5898 m_position = -1;
5899 m_ctrl = ctrl;
5900 m_name = name;
5901 m_newParagraphs.SetDefaultStyle(buffer->GetDefaultStyle());
5902 m_newParagraphs.SetBasicStyle(buffer->GetBasicStyle());
5903 if (cmd)
5904 cmd->AddAction(this);
5905 }
5906
5907 wxRichTextAction::~wxRichTextAction()
5908 {
5909 }
5910
5911 bool wxRichTextAction::Do()
5912 {
5913 m_buffer->Modify(true);
5914
5915 switch (m_cmdId)
5916 {
5917 case wxRICHTEXT_INSERT:
5918 {
5919 m_buffer->InsertFragment(GetPosition(), m_newParagraphs);
5920 m_buffer->UpdateRanges();
5921 m_buffer->Invalidate(GetRange());
5922
5923 long newCaretPosition = GetPosition() + m_newParagraphs.GetRange().GetLength();
5924
5925 // Character position to caret position
5926 newCaretPosition --;
5927
5928 // Don't take into account the last newline
5929 if (m_newParagraphs.GetPartialParagraph())
5930 newCaretPosition --;
5931
5932 newCaretPosition = wxMin(newCaretPosition, (m_buffer->GetRange().GetEnd()-1));
5933
5934 UpdateAppearance(newCaretPosition, true /* send update event */);
5935
5936 break;
5937 }
5938 case wxRICHTEXT_DELETE:
5939 {
5940 m_buffer->DeleteRange(GetRange());
5941 m_buffer->UpdateRanges();
5942 m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5943
5944 UpdateAppearance(GetRange().GetStart()-1, true /* send update event */);
5945
5946 break;
5947 }
5948 case wxRICHTEXT_CHANGE_STYLE:
5949 {
5950 ApplyParagraphs(GetNewParagraphs());
5951 m_buffer->Invalidate(GetRange());
5952
5953 UpdateAppearance(GetPosition());
5954
5955 break;
5956 }
5957 default:
5958 break;
5959 }
5960
5961 return true;
5962 }
5963
5964 bool wxRichTextAction::Undo()
5965 {
5966 m_buffer->Modify(true);
5967
5968 switch (m_cmdId)
5969 {
5970 case wxRICHTEXT_INSERT:
5971 {
5972 m_buffer->DeleteRange(GetRange());
5973 m_buffer->UpdateRanges();
5974 m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5975
5976 long newCaretPosition = GetPosition() - 1;
5977 // if (m_newParagraphs.GetPartialParagraph())
5978 // newCaretPosition --;
5979
5980 UpdateAppearance(newCaretPosition, true /* send update event */);
5981
5982 break;
5983 }
5984 case wxRICHTEXT_DELETE:
5985 {
5986 m_buffer->InsertFragment(GetRange().GetStart(), m_oldParagraphs);
5987 m_buffer->UpdateRanges();
5988 m_buffer->Invalidate(GetRange());
5989
5990 UpdateAppearance(GetPosition(), true /* send update event */);
5991
5992 break;
5993 }
5994 case wxRICHTEXT_CHANGE_STYLE:
5995 {
5996 ApplyParagraphs(GetOldParagraphs());
5997 m_buffer->Invalidate(GetRange());
5998
5999 UpdateAppearance(GetPosition());
6000
6001 break;
6002 }
6003 default:
6004 break;
6005 }
6006
6007 return true;
6008 }
6009
6010 /// Update the control appearance
6011 void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent)
6012 {
6013 if (m_ctrl)
6014 {
6015 m_ctrl->SetCaretPosition(caretPosition);
6016 if (!m_ctrl->IsFrozen())
6017 {
6018 m_ctrl->LayoutContent();
6019 m_ctrl->PositionCaret();
6020 m_ctrl->Refresh(false);
6021
6022 if (sendUpdateEvent)
6023 m_ctrl->SendTextUpdatedEvent();
6024 }
6025 }
6026 }
6027
6028 /// Replace the buffer paragraphs with the new ones.
6029 void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox& fragment)
6030 {
6031 wxRichTextObjectList::compatibility_iterator node = fragment.GetChildren().GetFirst();
6032 while (node)
6033 {
6034 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
6035 wxASSERT (para != NULL);
6036
6037 // We'll replace the existing paragraph by finding the paragraph at this position,
6038 // delete its node data, and setting a copy as the new node data.
6039 // TODO: make more efficient by simply swapping old and new paragraph objects.
6040
6041 wxRichTextParagraph* existingPara = m_buffer->GetParagraphAtPosition(para->GetRange().GetStart());
6042 if (existingPara)
6043 {
6044 wxRichTextObjectList::compatibility_iterator bufferParaNode = m_buffer->GetChildren().Find(existingPara);
6045 if (bufferParaNode)
6046 {
6047 wxRichTextParagraph* newPara = new wxRichTextParagraph(*para);
6048 newPara->SetParent(m_buffer);
6049
6050 bufferParaNode->SetData(newPara);
6051
6052 delete existingPara;
6053 }
6054 }
6055
6056 node = node->GetNext();
6057 }
6058 }
6059
6060
6061 /*!
6062 * wxRichTextRange
6063 * This stores beginning and end positions for a range of data.
6064 */
6065
6066 /// Limit this range to be within 'range'
6067 bool wxRichTextRange::LimitTo(const wxRichTextRange& range)
6068 {
6069 if (m_start < range.m_start)
6070 m_start = range.m_start;
6071
6072 if (m_end > range.m_end)
6073 m_end = range.m_end;
6074
6075 return true;
6076 }
6077
6078 /*!
6079 * wxRichTextImage implementation
6080 * This object represents an image.
6081 */
6082
6083 IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage, wxRichTextObject)
6084
6085 wxRichTextImage::wxRichTextImage(const wxImage& image, wxRichTextObject* parent):
6086 wxRichTextObject(parent)
6087 {
6088 m_image = image;
6089 }
6090
6091 wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent):
6092 wxRichTextObject(parent)
6093 {
6094 m_imageBlock = imageBlock;
6095 m_imageBlock.Load(m_image);
6096 }
6097
6098 /// Load wxImage from the block
6099 bool wxRichTextImage::LoadFromBlock()
6100 {
6101 m_imageBlock.Load(m_image);
6102 return m_imageBlock.Ok();
6103 }
6104
6105 /// Make block from the wxImage
6106 bool wxRichTextImage::MakeBlock()
6107 {
6108 if (m_imageBlock.GetImageType() == wxBITMAP_TYPE_ANY || m_imageBlock.GetImageType() == -1)
6109 m_imageBlock.SetImageType(wxBITMAP_TYPE_PNG);
6110
6111 m_imageBlock.MakeImageBlock(m_image, m_imageBlock.GetImageType());
6112 return m_imageBlock.Ok();
6113 }
6114
6115
6116 /// Draw the item
6117 bool wxRichTextImage::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int WXUNUSED(descent), int WXUNUSED(style))
6118 {
6119 if (!m_image.Ok() && m_imageBlock.Ok())
6120 LoadFromBlock();
6121
6122 if (!m_image.Ok())
6123 return false;
6124
6125 if (m_image.Ok() && !m_bitmap.Ok())
6126 m_bitmap = wxBitmap(m_image);
6127
6128 int y = rect.y + (rect.height - m_image.GetHeight());
6129
6130 if (m_bitmap.Ok())
6131 dc.DrawBitmap(m_bitmap, rect.x, y, true);
6132
6133 if (selectionRange.Contains(range.GetStart()))
6134 {
6135 dc.SetBrush(*wxBLACK_BRUSH);
6136 dc.SetPen(*wxBLACK_PEN);
6137 dc.SetLogicalFunction(wxINVERT);
6138 dc.DrawRectangle(rect);
6139 dc.SetLogicalFunction(wxCOPY);
6140 }
6141
6142 return true;
6143 }
6144
6145 /// Lay the item out
6146 bool wxRichTextImage::Layout(wxDC& WXUNUSED(dc), const wxRect& rect, int WXUNUSED(style))
6147 {
6148 if (!m_image.Ok())
6149 LoadFromBlock();
6150
6151 if (m_image.Ok())
6152 {
6153 SetCachedSize(wxSize(m_image.GetWidth(), m_image.GetHeight()));
6154 SetPosition(rect.GetPosition());
6155 }
6156
6157 return true;
6158 }
6159
6160 /// Get/set the object size for the given range. Returns false if the range
6161 /// is invalid for this object.
6162 bool wxRichTextImage::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& WXUNUSED(descent), wxDC& WXUNUSED(dc), int WXUNUSED(flags), wxPoint WXUNUSED(position)) const
6163 {
6164 if (!range.IsWithin(GetRange()))
6165 return false;
6166
6167 if (!m_image.Ok())
6168 return false;
6169
6170 size.x = m_image.GetWidth();
6171 size.y = m_image.GetHeight();
6172
6173 return true;
6174 }
6175
6176 /// Copy
6177 void wxRichTextImage::Copy(const wxRichTextImage& obj)
6178 {
6179 wxRichTextObject::Copy(obj);
6180
6181 m_image = obj.m_image;
6182 m_imageBlock = obj.m_imageBlock;
6183 }
6184
6185 /*!
6186 * Utilities
6187 *
6188 */
6189
6190 /// Compare two attribute objects
6191 bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2)
6192 {
6193 return (attr1 == attr2);
6194 }
6195
6196 bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2)
6197 {
6198 return (
6199 attr1.GetTextColour() == attr2.GetTextColour() &&
6200 attr1.GetBackgroundColour() == attr2.GetBackgroundColour() &&
6201 attr1.GetFont().GetPointSize() == attr2.GetFontSize() &&
6202 attr1.GetFont().GetStyle() == attr2.GetFontStyle() &&
6203 attr1.GetFont().GetWeight() == attr2.GetFontWeight() &&
6204 attr1.GetFont().GetFaceName() == attr2.GetFontFaceName() &&
6205 attr1.GetFont().GetUnderlined() == attr2.GetFontUnderlined() &&
6206 attr1.GetAlignment() == attr2.GetAlignment() &&
6207 attr1.GetLeftIndent() == attr2.GetLeftIndent() &&
6208 attr1.GetRightIndent() == attr2.GetRightIndent() &&
6209 attr1.GetLeftSubIndent() == attr2.GetLeftSubIndent() &&
6210 wxRichTextTabsEq(attr1.GetTabs(), attr2.GetTabs()) &&
6211 attr1.GetLineSpacing() == attr2.GetLineSpacing() &&
6212 attr1.GetParagraphSpacingAfter() == attr2.GetParagraphSpacingAfter() &&
6213 attr1.GetParagraphSpacingBefore() == attr2.GetParagraphSpacingBefore() &&
6214 attr1.GetBulletStyle() == attr2.GetBulletStyle() &&
6215 attr1.GetBulletNumber() == attr2.GetBulletNumber() &&
6216 attr1.GetBulletText() == attr2.GetBulletText() &&
6217 attr1.GetBulletName() == attr2.GetBulletName() &&
6218 attr1.GetBulletFont() == attr2.GetBulletFont() &&
6219 attr1.GetCharacterStyleName() == attr2.GetCharacterStyleName() &&
6220 attr1.GetParagraphStyleName() == attr2.GetParagraphStyleName() &&
6221 attr1.GetListStyleName() == attr2.GetListStyleName());
6222 }
6223
6224 /// Compare two attribute objects, but take into account the flags
6225 /// specifying attributes of interest.
6226 bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2, int flags)
6227 {
6228 if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && attr1.GetTextColour() != attr2.GetTextColour())
6229 return false;
6230
6231 if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && attr1.GetBackgroundColour() != attr2.GetBackgroundColour())
6232 return false;
6233
6234 if ((flags & wxTEXT_ATTR_FONT_FACE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6235 attr1.GetFont().GetFaceName() != attr2.GetFont().GetFaceName())
6236 return false;
6237
6238 if ((flags & wxTEXT_ATTR_FONT_SIZE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6239 attr1.GetFont().GetPointSize() != attr2.GetFont().GetPointSize())
6240 return false;
6241
6242 if ((flags & wxTEXT_ATTR_FONT_WEIGHT) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6243 attr1.GetFont().GetWeight() != attr2.GetFont().GetWeight())
6244 return false;
6245
6246 if ((flags & wxTEXT_ATTR_FONT_ITALIC) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6247 attr1.GetFont().GetStyle() != attr2.GetFont().GetStyle())
6248 return false;
6249
6250 if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() &&
6251 attr1.GetFont().GetUnderlined() != attr2.GetFont().GetUnderlined())
6252 return false;
6253
6254 if ((flags & wxTEXT_ATTR_ALIGNMENT) && attr1.GetAlignment() != attr2.GetAlignment())
6255 return false;
6256
6257 if ((flags & wxTEXT_ATTR_LEFT_INDENT) &&
6258 ((attr1.GetLeftIndent() != attr2.GetLeftIndent()) || (attr1.GetLeftSubIndent() != attr2.GetLeftSubIndent())))
6259 return false;
6260
6261 if ((flags & wxTEXT_ATTR_RIGHT_INDENT) &&
6262 (attr1.GetRightIndent() != attr2.GetRightIndent()))
6263 return false;
6264
6265 if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) &&
6266 (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter()))
6267 return false;
6268
6269 if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) &&
6270 (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore()))
6271 return false;
6272
6273 if ((flags & wxTEXT_ATTR_LINE_SPACING) &&
6274 (attr1.GetLineSpacing() != attr2.GetLineSpacing()))
6275 return false;
6276
6277 if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) &&
6278 (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName()))
6279 return false;
6280
6281 if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) &&
6282 (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName()))
6283 return false;
6284
6285 if ((flags & wxTEXT_ATTR_LIST_STYLE_NAME) &&
6286 (attr1.GetListStyleName() != attr2.GetListStyleName()))
6287 return false;
6288
6289 if ((flags & wxTEXT_ATTR_BULLET_STYLE) &&
6290 (attr1.GetBulletStyle() != attr2.GetBulletStyle()))
6291 return false;
6292
6293 if ((flags & wxTEXT_ATTR_BULLET_NUMBER) &&
6294 (attr1.GetBulletNumber() != attr2.GetBulletNumber()))
6295 return false;
6296
6297 if ((flags & wxTEXT_ATTR_BULLET_TEXT) &&
6298 (attr1.GetBulletText() != attr2.GetBulletText()) &&
6299 (attr1.GetBulletFont() != attr2.GetBulletFont()))
6300 return false;
6301
6302 if ((flags & wxTEXT_ATTR_BULLET_NAME) &&
6303 (attr1.GetBulletName() != attr2.GetBulletName()))
6304 return false;
6305
6306 if ((flags & wxTEXT_ATTR_TABS) &&
6307 !wxRichTextTabsEq(attr1.GetTabs(), attr2.GetTabs()))
6308 return false;
6309
6310 return true;
6311 }
6312
6313 bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2, int flags)
6314 {
6315 if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && attr1.GetTextColour() != attr2.GetTextColour())
6316 return false;
6317
6318 if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && attr1.GetBackgroundColour() != attr2.GetBackgroundColour())
6319 return false;
6320
6321 if ((flags & (wxTEXT_ATTR_FONT)) && !attr1.GetFont().Ok())
6322 return false;
6323
6324 if ((flags & wxTEXT_ATTR_FONT_FACE) && attr1.GetFont().Ok() &&
6325 attr1.GetFont().GetFaceName() != attr2.GetFontFaceName())
6326 return false;
6327
6328 if ((flags & wxTEXT_ATTR_FONT_SIZE) && attr1.GetFont().Ok() &&
6329 attr1.GetFont().GetPointSize() != attr2.GetFontSize())
6330 return false;
6331
6332 if ((flags & wxTEXT_ATTR_FONT_WEIGHT) && attr1.GetFont().Ok() &&
6333 attr1.GetFont().GetWeight() != attr2.GetFontWeight())
6334 return false;
6335
6336 if ((flags & wxTEXT_ATTR_FONT_ITALIC) && attr1.GetFont().Ok() &&
6337 attr1.GetFont().GetStyle() != attr2.GetFontStyle())
6338 return false;
6339
6340 if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) && attr1.GetFont().Ok() &&
6341 attr1.GetFont().GetUnderlined() != attr2.GetFontUnderlined())
6342 return false;
6343
6344 if ((flags & wxTEXT_ATTR_ALIGNMENT) && attr1.GetAlignment() != attr2.GetAlignment())
6345 return false;
6346
6347 if ((flags & wxTEXT_ATTR_LEFT_INDENT) &&
6348 ((attr1.GetLeftIndent() != attr2.GetLeftIndent()) || (attr1.GetLeftSubIndent() != attr2.GetLeftSubIndent())))
6349 return false;
6350
6351 if ((flags & wxTEXT_ATTR_RIGHT_INDENT) &&
6352 (attr1.GetRightIndent() != attr2.GetRightIndent()))
6353 return false;
6354
6355 if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) &&
6356 (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter()))
6357 return false;
6358
6359 if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) &&
6360 (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore()))
6361 return false;
6362
6363 if ((flags & wxTEXT_ATTR_LINE_SPACING) &&
6364 (attr1.GetLineSpacing() != attr2.GetLineSpacing()))
6365 return false;
6366
6367 if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) &&
6368 (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName()))
6369 return false;
6370
6371 if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) &&
6372 (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName()))
6373 return false;
6374
6375 if ((flags & wxTEXT_ATTR_LIST_STYLE_NAME) &&
6376 (attr1.GetListStyleName() != attr2.GetListStyleName()))
6377 return false;
6378
6379 if ((flags & wxTEXT_ATTR_BULLET_STYLE) &&
6380 (attr1.GetBulletStyle() != attr2.GetBulletStyle()))
6381 return false;
6382
6383 if ((flags & wxTEXT_ATTR_BULLET_NUMBER) &&
6384 (attr1.GetBulletNumber() != attr2.GetBulletNumber()))
6385 return false;
6386
6387 if ((flags & wxTEXT_ATTR_BULLET_TEXT) &&
6388 (attr1.GetBulletText() != attr2.GetBulletText()) &&
6389 (attr1.GetBulletFont() != attr2.GetBulletFont()))
6390 return false;
6391
6392 if ((flags & wxTEXT_ATTR_BULLET_NAME) &&
6393 (attr1.GetBulletName() != attr2.GetBulletName()))
6394 return false;
6395
6396 if ((flags & wxTEXT_ATTR_TABS) &&
6397 !wxRichTextTabsEq(attr1.GetTabs(), attr2.GetTabs()))
6398 return false;
6399
6400 return true;
6401 }
6402
6403 /// Compare tabs
6404 bool wxRichTextTabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
6405 {
6406 if (tabs1.GetCount() != tabs2.GetCount())
6407 return false;
6408
6409 size_t i;
6410 for (i = 0; i < tabs1.GetCount(); i++)
6411 {
6412 if (tabs1[i] != tabs2[i])
6413 return false;
6414 }
6415 return true;
6416 }
6417
6418
6419 /// Apply one style to another
6420 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxTextAttrEx& style)
6421 {
6422 // Whole font
6423 if (style.GetFont().Ok() && ((style.GetFlags() & (wxTEXT_ATTR_FONT)) == (wxTEXT_ATTR_FONT)))
6424 destStyle.SetFont(style.GetFont());
6425 else if (style.GetFont().Ok())
6426 {
6427 wxFont font = destStyle.GetFont();
6428
6429 if (style.GetFlags() & wxTEXT_ATTR_FONT_FACE)
6430 {
6431 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_FACE);
6432 font.SetFaceName(style.GetFont().GetFaceName());
6433 }
6434
6435 if (style.GetFlags() & wxTEXT_ATTR_FONT_SIZE)
6436 {
6437 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_SIZE);
6438 font.SetPointSize(style.GetFont().GetPointSize());
6439 }
6440
6441 if (style.GetFlags() & wxTEXT_ATTR_FONT_ITALIC)
6442 {
6443 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_ITALIC);
6444 font.SetStyle(style.GetFont().GetStyle());
6445 }
6446
6447 if (style.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT)
6448 {
6449 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT);
6450 font.SetWeight(style.GetFont().GetWeight());
6451 }
6452
6453 if (style.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE)
6454 {
6455 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE);
6456 font.SetUnderlined(style.GetFont().GetUnderlined());
6457 }
6458
6459 if (font != destStyle.GetFont())
6460 {
6461 int oldFlags = destStyle.GetFlags();
6462
6463 destStyle.SetFont(font);
6464
6465 destStyle.SetFlags(oldFlags);
6466 }
6467 }
6468
6469 if ( style.GetTextColour().Ok() && style.HasTextColour())
6470 destStyle.SetTextColour(style.GetTextColour());
6471
6472 if ( style.GetBackgroundColour().Ok() && style.HasBackgroundColour())
6473 destStyle.SetBackgroundColour(style.GetBackgroundColour());
6474
6475 if (style.HasAlignment())
6476 destStyle.SetAlignment(style.GetAlignment());
6477
6478 if (style.HasTabs())
6479 destStyle.SetTabs(style.GetTabs());
6480
6481 if (style.HasLeftIndent())
6482 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
6483
6484 if (style.HasRightIndent())
6485 destStyle.SetRightIndent(style.GetRightIndent());
6486
6487 if (style.HasParagraphSpacingAfter())
6488 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
6489
6490 if (style.HasParagraphSpacingBefore())
6491 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
6492
6493 if (style.HasLineSpacing())
6494 destStyle.SetLineSpacing(style.GetLineSpacing());
6495
6496 if (style.HasCharacterStyleName())
6497 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
6498
6499 if (style.HasParagraphStyleName())
6500 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
6501
6502 if (style.HasListStyleName())
6503 destStyle.SetListStyleName(style.GetListStyleName());
6504
6505 if (style.HasBulletStyle())
6506 destStyle.SetBulletStyle(style.GetBulletStyle());
6507
6508 if (style.HasBulletText())
6509 {
6510 destStyle.SetBulletText(style.GetBulletText());
6511 destStyle.SetBulletFont(style.GetBulletFont());
6512 }
6513
6514 if (style.HasBulletName())
6515 destStyle.SetBulletName(style.GetBulletName());
6516
6517 if (style.HasBulletNumber())
6518 destStyle.SetBulletNumber(style.GetBulletNumber());
6519
6520 if (style.HasURL())
6521 destStyle.SetURL(style.GetURL());
6522
6523 return true;
6524 }
6525
6526 bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxTextAttrEx& style)
6527 {
6528 wxTextAttrEx destStyle2;
6529 destStyle.CopyTo(destStyle2);
6530 wxRichTextApplyStyle(destStyle2, style);
6531 destStyle = destStyle2;
6532 return true;
6533 }
6534
6535 bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style, wxRichTextAttr* compareWith)
6536 {
6537 // Whole font. Avoiding setting individual attributes if possible, since
6538 // it recreates the font each time.
6539 if (((style.GetFlags() & (wxTEXT_ATTR_FONT)) == (wxTEXT_ATTR_FONT)) && !compareWith)
6540 {
6541 destStyle.SetFont(wxFont(style.GetFontSize(), destStyle.GetFont().Ok() ? destStyle.GetFont().GetFamily() : wxDEFAULT,
6542 style.GetFontStyle(), style.GetFontWeight(), style.GetFontUnderlined(), style.GetFontFaceName()));
6543 }
6544 else if (style.GetFlags() & (wxTEXT_ATTR_FONT))
6545 {
6546 wxFont font = destStyle.GetFont();
6547
6548 if (style.GetFlags() & wxTEXT_ATTR_FONT_FACE)
6549 {
6550 if (compareWith && compareWith->HasFaceName() && compareWith->GetFontFaceName() == style.GetFontFaceName())
6551 {
6552 // The same as currently displayed, so don't set
6553 }
6554 else
6555 {
6556 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_FACE);
6557 font.SetFaceName(style.GetFontFaceName());
6558 }
6559 }
6560
6561 if (style.GetFlags() & wxTEXT_ATTR_FONT_SIZE)
6562 {
6563 if (compareWith && compareWith->HasSize() && compareWith->GetFontSize() == style.GetFontSize())
6564 {
6565 // The same as currently displayed, so don't set
6566 }
6567 else
6568 {
6569 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_SIZE);
6570 font.SetPointSize(style.GetFontSize());
6571 }
6572 }
6573
6574 if (style.GetFlags() & wxTEXT_ATTR_FONT_ITALIC)
6575 {
6576 if (compareWith && compareWith->HasItalic() && compareWith->GetFontStyle() == style.GetFontStyle())
6577 {
6578 // The same as currently displayed, so don't set
6579 }
6580 else
6581 {
6582 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_ITALIC);
6583 font.SetStyle(style.GetFontStyle());
6584 }
6585 }
6586
6587 if (style.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT)
6588 {
6589 if (compareWith && compareWith->HasWeight() && compareWith->GetFontWeight() == style.GetFontWeight())
6590 {
6591 // The same as currently displayed, so don't set
6592 }
6593 else
6594 {
6595 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_WEIGHT);
6596 font.SetWeight(style.GetFontWeight());
6597 }
6598 }
6599
6600 if (style.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE)
6601 {
6602 if (compareWith && compareWith->HasUnderlined() && compareWith->GetFontUnderlined() == style.GetFontUnderlined())
6603 {
6604 // The same as currently displayed, so don't set
6605 }
6606 else
6607 {
6608 destStyle.SetFlags(destStyle.GetFlags() | wxTEXT_ATTR_FONT_UNDERLINE);
6609 font.SetUnderlined(style.GetFontUnderlined());
6610 }
6611 }
6612
6613 if (font != destStyle.GetFont())
6614 {
6615 int oldFlags = destStyle.GetFlags();
6616
6617 destStyle.SetFont(font);
6618
6619 destStyle.SetFlags(oldFlags);
6620 }
6621 }
6622
6623 if (style.GetTextColour().Ok() && style.HasTextColour())
6624 {
6625 if (!(compareWith && compareWith->HasTextColour() && compareWith->GetTextColour() == style.GetTextColour()))
6626 destStyle.SetTextColour(style.GetTextColour());
6627 }
6628
6629 if (style.GetBackgroundColour().Ok() && style.HasBackgroundColour())
6630 {
6631 if (!(compareWith && compareWith->HasBackgroundColour() && compareWith->GetBackgroundColour() == style.GetBackgroundColour()))
6632 destStyle.SetBackgroundColour(style.GetBackgroundColour());
6633 }
6634
6635 if (style.HasAlignment())
6636 {
6637 if (!(compareWith && compareWith->HasAlignment() && compareWith->GetAlignment() == style.GetAlignment()))
6638 destStyle.SetAlignment(style.GetAlignment());
6639 }
6640
6641 if (style.HasTabs())
6642 {
6643 if (!(compareWith && compareWith->HasTabs() && wxRichTextTabsEq(compareWith->GetTabs(), style.GetTabs())))
6644 destStyle.SetTabs(style.GetTabs());
6645 }
6646
6647 if (style.HasLeftIndent())
6648 {
6649 if (!(compareWith && compareWith->HasLeftIndent() && compareWith->GetLeftIndent() == style.GetLeftIndent()
6650 && compareWith->GetLeftSubIndent() == style.GetLeftSubIndent()))
6651 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
6652 }
6653
6654 if (style.HasRightIndent())
6655 {
6656 if (!(compareWith && compareWith->HasRightIndent() && compareWith->GetRightIndent() == style.GetRightIndent()))
6657 destStyle.SetRightIndent(style.GetRightIndent());
6658 }
6659
6660 if (style.HasParagraphSpacingAfter())
6661 {
6662 if (!(compareWith && compareWith->HasParagraphSpacingAfter() && compareWith->GetParagraphSpacingAfter() == style.GetParagraphSpacingAfter()))
6663 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
6664 }
6665
6666 if (style.HasParagraphSpacingBefore())
6667 {
6668 if (!(compareWith && compareWith->HasParagraphSpacingBefore() && compareWith->GetParagraphSpacingBefore() == style.GetParagraphSpacingBefore()))
6669 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
6670 }
6671
6672 if (style.HasLineSpacing())
6673 {
6674 if (!(compareWith && compareWith->HasLineSpacing() && compareWith->GetLineSpacing() == style.GetLineSpacing()))
6675 destStyle.SetLineSpacing(style.GetLineSpacing());
6676 }
6677
6678 if (style.HasCharacterStyleName())
6679 {
6680 if (!(compareWith && compareWith->HasCharacterStyleName() && compareWith->GetCharacterStyleName() == style.GetCharacterStyleName()))
6681 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
6682 }
6683
6684 if (style.HasParagraphStyleName())
6685 {
6686 if (!(compareWith && compareWith->HasParagraphStyleName() && compareWith->GetParagraphStyleName() == style.GetParagraphStyleName()))
6687 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
6688 }
6689
6690 if (style.HasListStyleName())
6691 {
6692 if (!(compareWith && compareWith->HasListStyleName() && compareWith->GetListStyleName() == style.GetListStyleName()))
6693 destStyle.SetListStyleName(style.GetListStyleName());
6694 }
6695
6696 if (style.HasBulletStyle())
6697 {
6698 if (!(compareWith && compareWith->HasBulletStyle() && compareWith->GetBulletStyle() == style.GetBulletStyle()))
6699 destStyle.SetBulletStyle(style.GetBulletStyle());
6700 }
6701
6702 if (style.HasBulletText())
6703 {
6704 if (!(compareWith && compareWith->HasBulletText() && compareWith->GetBulletText() == style.GetBulletText()))
6705 {
6706 destStyle.SetBulletText(style.GetBulletText());
6707 destStyle.SetBulletFont(style.GetBulletFont());
6708 }
6709 }
6710
6711 if (style.HasBulletNumber())
6712 {
6713 if (!(compareWith && compareWith->HasBulletNumber() && compareWith->GetBulletNumber() == style.GetBulletNumber()))
6714 destStyle.SetBulletNumber(style.GetBulletNumber());
6715 }
6716
6717 if (style.HasBulletName())
6718 {
6719 if (!(compareWith && compareWith->HasBulletName() && compareWith->GetBulletName() == style.GetBulletName()))
6720 destStyle.SetBulletName(style.GetBulletName());
6721 }
6722
6723 if (style.HasURL())
6724 {
6725 if (!(compareWith && compareWith->HasURL() && compareWith->GetURL() == style.GetURL()))
6726 destStyle.SetURL(style.GetURL());
6727 }
6728
6729 return true;
6730 }
6731
6732 void wxSetFontPreservingStyles(wxTextAttr& attr, const wxFont& font)
6733 {
6734 long flags = attr.GetFlags();
6735 attr.SetFont(font);
6736 attr.SetFlags(flags);
6737 }
6738
6739 /// Convert a decimal to Roman numerals
6740 wxString wxRichTextDecimalToRoman(long n)
6741 {
6742 static wxArrayInt decimalNumbers;
6743 static wxArrayString romanNumbers;
6744
6745 // Clean up arrays
6746 if (n == -1)
6747 {
6748 decimalNumbers.Clear();
6749 romanNumbers.Clear();
6750 return wxEmptyString;
6751 }
6752
6753 if (decimalNumbers.GetCount() == 0)
6754 {
6755 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
6756
6757 wxRichTextAddDecRom(1000, wxT("M"));
6758 wxRichTextAddDecRom(900, wxT("CM"));
6759 wxRichTextAddDecRom(500, wxT("D"));
6760 wxRichTextAddDecRom(400, wxT("CD"));
6761 wxRichTextAddDecRom(100, wxT("C"));
6762 wxRichTextAddDecRom(90, wxT("XC"));
6763 wxRichTextAddDecRom(50, wxT("L"));
6764 wxRichTextAddDecRom(40, wxT("XL"));
6765 wxRichTextAddDecRom(10, wxT("X"));
6766 wxRichTextAddDecRom(9, wxT("IX"));
6767 wxRichTextAddDecRom(5, wxT("V"));
6768 wxRichTextAddDecRom(4, wxT("IV"));
6769 wxRichTextAddDecRom(1, wxT("I"));
6770 }
6771
6772 int i = 0;
6773 wxString roman;
6774
6775 while (n > 0 && i < 13)
6776 {
6777 if (n >= decimalNumbers[i])
6778 {
6779 n -= decimalNumbers[i];
6780 roman += romanNumbers[i];
6781 }
6782 else
6783 {
6784 i ++;
6785 }
6786 }
6787 if (roman.IsEmpty())
6788 roman = wxT("0");
6789 return roman;
6790 }
6791
6792 /*!
6793 * wxRichTextAttr stores attributes without a wxFont object, so is a much more
6794 * efficient way to query styles.
6795 */
6796
6797 // ctors
6798 wxRichTextAttr::wxRichTextAttr(const wxColour& colText,
6799 const wxColour& colBack,
6800 wxTextAttrAlignment alignment): m_textAlignment(alignment), m_colText(colText), m_colBack(colBack)
6801 {
6802 Init();
6803
6804 if (m_colText.Ok()) m_flags |= wxTEXT_ATTR_TEXT_COLOUR;
6805 if (m_colBack.Ok()) m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR;
6806 if (alignment != wxTEXT_ALIGNMENT_DEFAULT)
6807 m_flags |= wxTEXT_ATTR_ALIGNMENT;
6808 }
6809
6810 wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx& attr)
6811 {
6812 Init();
6813
6814 (*this) = attr;
6815 }
6816
6817 wxRichTextAttr::wxRichTextAttr(const wxRichTextAttr& attr)
6818 {
6819 Copy(attr);
6820 }
6821
6822 // operations
6823 void wxRichTextAttr::Init()
6824 {
6825 m_textAlignment = wxTEXT_ALIGNMENT_DEFAULT;
6826 m_flags = 0;
6827 m_leftIndent = 0;
6828 m_leftSubIndent = 0;
6829 m_rightIndent = 0;
6830
6831 m_fontSize = 12;
6832 m_fontStyle = wxNORMAL;
6833 m_fontWeight = wxNORMAL;
6834 m_fontUnderlined = false;
6835
6836 m_paragraphSpacingAfter = 0;
6837 m_paragraphSpacingBefore = 0;
6838 m_lineSpacing = 0;
6839 m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
6840 m_bulletNumber = 0;
6841 }
6842
6843 // Copy
6844 void wxRichTextAttr::Copy(const wxRichTextAttr& attr)
6845 {
6846 m_colText = attr.m_colText;
6847 m_colBack = attr.m_colBack;
6848 m_textAlignment = attr.m_textAlignment;
6849 m_leftIndent = attr.m_leftIndent;
6850 m_leftSubIndent = attr.m_leftSubIndent;
6851 m_rightIndent = attr.m_rightIndent;
6852 m_tabs = attr.m_tabs;
6853 m_flags = attr.m_flags;
6854
6855 m_fontSize = attr.m_fontSize;
6856 m_fontStyle = attr.m_fontStyle;
6857 m_fontWeight = attr.m_fontWeight;
6858 m_fontUnderlined = attr.m_fontUnderlined;
6859 m_fontFaceName = attr.m_fontFaceName;
6860
6861 m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
6862 m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
6863 m_lineSpacing = attr.m_lineSpacing;
6864 m_characterStyleName = attr.m_characterStyleName;
6865 m_paragraphStyleName = attr.m_paragraphStyleName;
6866 m_listStyleName = attr.m_listStyleName;
6867 m_bulletStyle = attr.m_bulletStyle;
6868 m_bulletNumber = attr.m_bulletNumber;
6869 m_bulletText = attr.m_bulletText;
6870 m_bulletFont = attr.m_bulletFont;
6871 m_bulletName = attr.m_bulletName;
6872
6873 m_urlTarget = attr.m_urlTarget;
6874 }
6875
6876 // operators
6877 void wxRichTextAttr::operator= (const wxRichTextAttr& attr)
6878 {
6879 Copy(attr);
6880 }
6881
6882 // operators
6883 void wxRichTextAttr::operator= (const wxTextAttrEx& attr)
6884 {
6885 m_colText = attr.GetTextColour();
6886 m_colBack = attr.GetBackgroundColour();
6887 m_textAlignment = attr.GetAlignment();
6888 m_leftIndent = attr.GetLeftIndent();
6889 m_leftSubIndent = attr.GetLeftSubIndent();
6890 m_rightIndent = attr.GetRightIndent();
6891 m_tabs = attr.GetTabs();
6892 m_flags = attr.GetFlags();
6893
6894 m_paragraphSpacingAfter = attr.GetParagraphSpacingAfter();
6895 m_paragraphSpacingBefore = attr.GetParagraphSpacingBefore();
6896 m_lineSpacing = attr.GetLineSpacing();
6897 m_characterStyleName = attr.GetCharacterStyleName();
6898 m_paragraphStyleName = attr.GetParagraphStyleName();
6899 m_listStyleName = attr.GetListStyleName();
6900 m_bulletStyle = attr.GetBulletStyle();
6901 m_bulletNumber = attr.GetBulletNumber();
6902 m_bulletText = attr.GetBulletText();
6903 m_bulletName = attr.GetBulletName();
6904 m_bulletFont = attr.GetBulletFont();
6905
6906 m_urlTarget = attr.GetURL();
6907
6908 if (attr.GetFont().Ok())
6909 GetFontAttributes(attr.GetFont());
6910 }
6911
6912 // Making a wxTextAttrEx object.
6913 wxRichTextAttr::operator wxTextAttrEx () const
6914 {
6915 wxTextAttrEx attr;
6916 CopyTo(attr);
6917 return attr;
6918 }
6919
6920 // Equality test
6921 bool wxRichTextAttr::operator== (const wxRichTextAttr& attr) const
6922 {
6923 return GetFlags() == attr.GetFlags() &&
6924
6925 GetTextColour() == attr.GetTextColour() &&
6926 GetBackgroundColour() == attr.GetBackgroundColour() &&
6927
6928 GetAlignment() == attr.GetAlignment() &&
6929 GetLeftIndent() == attr.GetLeftIndent() &&
6930 GetLeftSubIndent() == attr.GetLeftSubIndent() &&
6931 GetRightIndent() == attr.GetRightIndent() &&
6932 wxRichTextTabsEq(GetTabs(), attr.GetTabs()) &&
6933
6934 GetParagraphSpacingAfter() == attr.GetParagraphSpacingAfter() &&
6935 GetParagraphSpacingBefore() == attr.GetParagraphSpacingBefore() &&
6936 GetLineSpacing() == attr.GetLineSpacing() &&
6937 GetCharacterStyleName() == attr.GetCharacterStyleName() &&
6938 GetParagraphStyleName() == attr.GetParagraphStyleName() &&
6939 GetListStyleName() == attr.GetListStyleName() &&
6940
6941 GetBulletStyle() == attr.GetBulletStyle() &&
6942 GetBulletText() == attr.GetBulletText() &&
6943 GetBulletNumber() == attr.GetBulletNumber() &&
6944 GetBulletFont() == attr.GetBulletFont() &&
6945 GetBulletName() == attr.GetBulletName() &&
6946
6947 m_fontSize == attr.m_fontSize &&
6948 m_fontStyle == attr.m_fontStyle &&
6949 m_fontWeight == attr.m_fontWeight &&
6950 m_fontUnderlined == attr.m_fontUnderlined &&
6951 m_fontFaceName == attr.m_fontFaceName &&
6952
6953 m_urlTarget == attr.m_urlTarget;
6954 }
6955
6956 // Copy to a wxTextAttr
6957 void wxRichTextAttr::CopyTo(wxTextAttrEx& attr) const
6958 {
6959 attr.SetTextColour(GetTextColour());
6960 attr.SetBackgroundColour(GetBackgroundColour());
6961 attr.SetAlignment(GetAlignment());
6962 attr.SetTabs(GetTabs());
6963 attr.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent());
6964 attr.SetRightIndent(GetRightIndent());
6965 attr.SetFont(CreateFont());
6966
6967 attr.SetParagraphSpacingAfter(m_paragraphSpacingAfter);
6968 attr.SetParagraphSpacingBefore(m_paragraphSpacingBefore);
6969 attr.SetLineSpacing(m_lineSpacing);
6970 attr.SetBulletStyle(m_bulletStyle);
6971 attr.SetBulletNumber(m_bulletNumber);
6972 attr.SetBulletText(m_bulletText);
6973 attr.SetBulletName(m_bulletName);
6974 attr.SetBulletFont(m_bulletFont);
6975 attr.SetCharacterStyleName(m_characterStyleName);
6976 attr.SetParagraphStyleName(m_paragraphStyleName);
6977 attr.SetListStyleName(m_listStyleName);
6978
6979 attr.SetURL(m_urlTarget);
6980
6981 attr.SetFlags(GetFlags()); // Important: set after SetFont and others, since they set flags
6982 }
6983
6984 // Create font from font attributes.
6985 wxFont wxRichTextAttr::CreateFont() const
6986 {
6987 wxFont font(m_fontSize, wxDEFAULT, m_fontStyle, m_fontWeight, m_fontUnderlined, m_fontFaceName);
6988 #ifdef __WXMAC__
6989 font.SetNoAntiAliasing(true);
6990 #endif
6991 return font;
6992 }
6993
6994 // Get attributes from font.
6995 bool wxRichTextAttr::GetFontAttributes(const wxFont& font)
6996 {
6997 if (!font.Ok())
6998 return false;
6999
7000 m_fontSize = font.GetPointSize();
7001 m_fontStyle = font.GetStyle();
7002 m_fontWeight = font.GetWeight();
7003 m_fontUnderlined = font.GetUnderlined();
7004 m_fontFaceName = font.GetFaceName();
7005
7006 return true;
7007 }
7008
7009 wxRichTextAttr wxRichTextAttr::Combine(const wxRichTextAttr& attr,
7010 const wxRichTextAttr& attrDef,
7011 const wxTextCtrlBase *text)
7012 {
7013 wxColour colFg = attr.GetTextColour();
7014 if ( !colFg.Ok() )
7015 {
7016 colFg = attrDef.GetTextColour();
7017
7018 if ( text && !colFg.Ok() )
7019 colFg = text->GetForegroundColour();
7020 }
7021
7022 wxColour colBg = attr.GetBackgroundColour();
7023 if ( !colBg.Ok() )
7024 {
7025 colBg = attrDef.GetBackgroundColour();
7026
7027 if ( text && !colBg.Ok() )
7028 colBg = text->GetBackgroundColour();
7029 }
7030
7031 wxRichTextAttr newAttr(colFg, colBg);
7032
7033 if (attr.HasWeight())
7034 newAttr.SetFontWeight(attr.GetFontWeight());
7035
7036 if (attr.HasSize())
7037 newAttr.SetFontSize(attr.GetFontSize());
7038
7039 if (attr.HasItalic())
7040 newAttr.SetFontStyle(attr.GetFontStyle());
7041
7042 if (attr.HasUnderlined())
7043 newAttr.SetFontUnderlined(attr.GetFontUnderlined());
7044
7045 if (attr.HasFaceName())
7046 newAttr.SetFontFaceName(attr.GetFontFaceName());
7047
7048 if (attr.HasAlignment())
7049 newAttr.SetAlignment(attr.GetAlignment());
7050 else if (attrDef.HasAlignment())
7051 newAttr.SetAlignment(attrDef.GetAlignment());
7052
7053 if (attr.HasTabs())
7054 newAttr.SetTabs(attr.GetTabs());
7055 else if (attrDef.HasTabs())
7056 newAttr.SetTabs(attrDef.GetTabs());
7057
7058 if (attr.HasLeftIndent())
7059 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
7060 else if (attrDef.HasLeftIndent())
7061 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
7062
7063 if (attr.HasRightIndent())
7064 newAttr.SetRightIndent(attr.GetRightIndent());
7065 else if (attrDef.HasRightIndent())
7066 newAttr.SetRightIndent(attrDef.GetRightIndent());
7067
7068 // NEW ATTRIBUTES
7069
7070 if (attr.HasParagraphSpacingAfter())
7071 newAttr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter());
7072
7073 if (attr.HasParagraphSpacingBefore())
7074 newAttr.SetParagraphSpacingBefore(attr.GetParagraphSpacingBefore());
7075
7076 if (attr.HasLineSpacing())
7077 newAttr.SetLineSpacing(attr.GetLineSpacing());
7078
7079 if (attr.HasCharacterStyleName())
7080 newAttr.SetCharacterStyleName(attr.GetCharacterStyleName());
7081
7082 if (attr.HasParagraphStyleName())
7083 newAttr.SetParagraphStyleName(attr.GetParagraphStyleName());
7084
7085 if (attr.HasListStyleName())
7086 newAttr.SetListStyleName(attr.GetListStyleName());
7087
7088 if (attr.HasBulletStyle())
7089 newAttr.SetBulletStyle(attr.GetBulletStyle());
7090
7091 if (attr.HasBulletNumber())
7092 newAttr.SetBulletNumber(attr.GetBulletNumber());
7093
7094 if (attr.HasBulletName())
7095 newAttr.SetBulletName(attr.GetBulletName());
7096
7097 if (attr.HasBulletText())
7098 {
7099 newAttr.SetBulletText(attr.GetBulletText());
7100 newAttr.SetBulletFont(attr.GetBulletFont());
7101 }
7102
7103 if (attr.HasURL())
7104 newAttr.SetURL(attr.GetURL());
7105
7106 return newAttr;
7107 }
7108
7109 /*!
7110 * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
7111 */
7112
7113 wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx& attr)
7114 {
7115 Copy(attr);
7116 }
7117
7118 // Initialise this object.
7119 void wxTextAttrEx::Init()
7120 {
7121 m_paragraphSpacingAfter = 0;
7122 m_paragraphSpacingBefore = 0;
7123 m_lineSpacing = 0;
7124 m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
7125 m_bulletNumber = 0;
7126 }
7127
7128 // Copy
7129 void wxTextAttrEx::Copy(const wxTextAttrEx& attr)
7130 {
7131 wxTextAttr::operator= (attr);
7132
7133 m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
7134 m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
7135 m_lineSpacing = attr.m_lineSpacing;
7136 m_characterStyleName = attr.m_characterStyleName;
7137 m_paragraphStyleName = attr.m_paragraphStyleName;
7138 m_listStyleName = attr.m_listStyleName;
7139 m_bulletStyle = attr.m_bulletStyle;
7140 m_bulletNumber = attr.m_bulletNumber;
7141 m_bulletText = attr.m_bulletText;
7142 m_bulletFont = attr.m_bulletFont;
7143 m_bulletName = attr.m_bulletName;
7144 m_urlTarget = attr.m_urlTarget;
7145 }
7146
7147 // Assignment from a wxTextAttrEx object
7148 void wxTextAttrEx::operator= (const wxTextAttrEx& attr)
7149 {
7150 Copy(attr);
7151 }
7152
7153 // Assignment from a wxTextAttr object.
7154 void wxTextAttrEx::operator= (const wxTextAttr& attr)
7155 {
7156 wxTextAttr::operator= (attr);
7157 }
7158
7159 // Equality test
7160 bool wxTextAttrEx::operator== (const wxTextAttrEx& attr) const
7161 {
7162 return (
7163 GetTextColour() == attr.GetTextColour() &&
7164 GetBackgroundColour() == attr.GetBackgroundColour() &&
7165 GetFont() == attr.GetFont() &&
7166 GetAlignment() == attr.GetAlignment() &&
7167 GetLeftIndent() == attr.GetLeftIndent() &&
7168 GetRightIndent() == attr.GetRightIndent() &&
7169 GetLeftSubIndent() == attr.GetLeftSubIndent() &&
7170 wxRichTextTabsEq(GetTabs(), attr.GetTabs()) &&
7171 GetLineSpacing() == attr.GetLineSpacing() &&
7172 GetParagraphSpacingAfter() == attr.GetParagraphSpacingAfter() &&
7173 GetParagraphSpacingBefore() == attr.GetParagraphSpacingBefore() &&
7174 GetBulletStyle() == attr.GetBulletStyle() &&
7175 GetBulletNumber() == attr.GetBulletNumber() &&
7176 GetBulletText() == attr.GetBulletText() &&
7177 GetBulletName() == attr.GetBulletName() &&
7178 GetBulletFont() == attr.GetBulletFont() &&
7179 GetCharacterStyleName() == attr.GetCharacterStyleName() &&
7180 GetParagraphStyleName() == attr.GetParagraphStyleName() &&
7181 GetListStyleName() == attr.GetListStyleName() &&
7182 GetURL() == attr.GetURL());
7183 }
7184
7185 wxTextAttrEx wxTextAttrEx::CombineEx(const wxTextAttrEx& attr,
7186 const wxTextAttrEx& attrDef,
7187 const wxTextCtrlBase *text)
7188 {
7189 wxTextAttrEx newAttr;
7190
7191 // If attr specifies the complete font, just use that font, overriding all
7192 // default font attributes.
7193 if ((attr.GetFlags() & wxTEXT_ATTR_FONT) == wxTEXT_ATTR_FONT)
7194 newAttr.SetFont(attr.GetFont());
7195 else
7196 {
7197 // First find the basic, default font
7198 long flags = 0;
7199
7200 wxFont font;
7201 if (attrDef.HasFont())
7202 {
7203 flags = (attrDef.GetFlags() & wxTEXT_ATTR_FONT);
7204 font = attrDef.GetFont();
7205 }
7206 else
7207 {
7208 if (text)
7209 font = text->GetFont();
7210
7211 // We leave flags at 0 because no font attributes have been specified yet
7212 }
7213 if (!font.Ok())
7214 font = *wxNORMAL_FONT;
7215
7216 // Otherwise, if there are font attributes in attr, apply them
7217 if (attr.GetFlags() & wxTEXT_ATTR_FONT)
7218 {
7219 if (attr.HasSize())
7220 {
7221 flags |= wxTEXT_ATTR_FONT_SIZE;
7222 font.SetPointSize(attr.GetFont().GetPointSize());
7223 }
7224 if (attr.HasItalic())
7225 {
7226 flags |= wxTEXT_ATTR_FONT_ITALIC;;
7227 font.SetStyle(attr.GetFont().GetStyle());
7228 }
7229 if (attr.HasWeight())
7230 {
7231 flags |= wxTEXT_ATTR_FONT_WEIGHT;
7232 font.SetWeight(attr.GetFont().GetWeight());
7233 }
7234 if (attr.HasFaceName())
7235 {
7236 flags |= wxTEXT_ATTR_FONT_FACE;
7237 font.SetFaceName(attr.GetFont().GetFaceName());
7238 }
7239 if (attr.HasUnderlined())
7240 {
7241 flags |= wxTEXT_ATTR_FONT_UNDERLINE;
7242 font.SetUnderlined(attr.GetFont().GetUnderlined());
7243 }
7244 newAttr.SetFont(font);
7245 newAttr.SetFlags(newAttr.GetFlags()|flags);
7246 }
7247 }
7248
7249 // TODO: should really check we are specifying these in the flags,
7250 // before setting them, as per above; or we will set them willy-nilly.
7251 // However, we should also check whether this is the intention
7252 // as per wxTextAttr::Combine, i.e. always to have valid colours
7253 // in the style.
7254 wxColour colFg = attr.GetTextColour();
7255 if ( !colFg.Ok() )
7256 {
7257 colFg = attrDef.GetTextColour();
7258
7259 if ( text && !colFg.Ok() )
7260 colFg = text->GetForegroundColour();
7261 }
7262
7263 wxColour colBg = attr.GetBackgroundColour();
7264 if ( !colBg.Ok() )
7265 {
7266 colBg = attrDef.GetBackgroundColour();
7267
7268 if ( text && !colBg.Ok() )
7269 colBg = text->GetBackgroundColour();
7270 }
7271
7272 newAttr.SetTextColour(colFg);
7273 newAttr.SetBackgroundColour(colBg);
7274
7275 if (attr.HasAlignment())
7276 newAttr.SetAlignment(attr.GetAlignment());
7277 else if (attrDef.HasAlignment())
7278 newAttr.SetAlignment(attrDef.GetAlignment());
7279
7280 if (attr.HasTabs())
7281 newAttr.SetTabs(attr.GetTabs());
7282 else if (attrDef.HasTabs())
7283 newAttr.SetTabs(attrDef.GetTabs());
7284
7285 if (attr.HasLeftIndent())
7286 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
7287 else if (attrDef.HasLeftIndent())
7288 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
7289
7290 if (attr.HasRightIndent())
7291 newAttr.SetRightIndent(attr.GetRightIndent());
7292 else if (attrDef.HasRightIndent())
7293 newAttr.SetRightIndent(attrDef.GetRightIndent());
7294
7295 // NEW ATTRIBUTES
7296
7297 if (attr.HasParagraphSpacingAfter())
7298 newAttr.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter());
7299
7300 if (attr.HasParagraphSpacingBefore())
7301 newAttr.SetParagraphSpacingBefore(attr.GetParagraphSpacingBefore());
7302
7303 if (attr.HasLineSpacing())
7304 newAttr.SetLineSpacing(attr.GetLineSpacing());
7305
7306 if (attr.HasCharacterStyleName())
7307 newAttr.SetCharacterStyleName(attr.GetCharacterStyleName());
7308
7309 if (attr.HasParagraphStyleName())
7310 newAttr.SetParagraphStyleName(attr.GetParagraphStyleName());
7311
7312 if (attr.HasListStyleName())
7313 newAttr.SetListStyleName(attr.GetListStyleName());
7314
7315 if (attr.HasBulletStyle())
7316 newAttr.SetBulletStyle(attr.GetBulletStyle());
7317
7318 if (attr.HasBulletNumber())
7319 newAttr.SetBulletNumber(attr.GetBulletNumber());
7320
7321 if (attr.HasBulletName())
7322 newAttr.SetBulletName(attr.GetBulletName());
7323
7324 if (attr.HasBulletText())
7325 {
7326 newAttr.SetBulletText(attr.GetBulletText());
7327 newAttr.SetBulletFont(attr.GetBulletFont());
7328 }
7329
7330 if (attr.HasURL())
7331 newAttr.SetURL(attr.GetURL());
7332
7333 return newAttr;
7334 }
7335
7336
7337 /*!
7338 * wxRichTextFileHandler
7339 * Base class for file handlers
7340 */
7341
7342 IMPLEMENT_CLASS(wxRichTextFileHandler, wxObject)
7343
7344 #if wxUSE_STREAMS
7345 bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer *buffer, const wxString& filename)
7346 {
7347 wxFFileInputStream stream(filename);
7348 if (stream.Ok())
7349 return LoadFile(buffer, stream);
7350
7351 return false;
7352 }
7353
7354 bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& filename)
7355 {
7356 wxFFileOutputStream stream(filename);
7357 if (stream.Ok())
7358 return SaveFile(buffer, stream);
7359
7360 return false;
7361 }
7362 #endif // wxUSE_STREAMS
7363
7364 /// Can we handle this filename (if using files)? By default, checks the extension.
7365 bool wxRichTextFileHandler::CanHandle(const wxString& filename) const
7366 {
7367 wxString path, file, ext;
7368 wxSplitPath(filename, & path, & file, & ext);
7369
7370 return (ext.Lower() == GetExtension());
7371 }
7372
7373 /*!
7374 * wxRichTextTextHandler
7375 * Plain text handler
7376 */
7377
7378 IMPLEMENT_CLASS(wxRichTextPlainTextHandler, wxRichTextFileHandler)
7379
7380 #if wxUSE_STREAMS
7381 bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
7382 {
7383 if (!stream.IsOk())
7384 return false;
7385
7386 wxString str;
7387 int lastCh = 0;
7388
7389 while (!stream.Eof())
7390 {
7391 int ch = stream.GetC();
7392
7393 if (!stream.Eof())
7394 {
7395 if (ch == 10 && lastCh != 13)
7396 str += wxT('\n');
7397
7398 if (ch > 0 && ch != 10)
7399 str += wxChar(ch);
7400
7401 lastCh = ch;
7402 }
7403 }
7404
7405 buffer->Clear();
7406 buffer->AddParagraphs(str);
7407 buffer->UpdateRanges();
7408
7409 return true;
7410
7411 }
7412
7413 bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
7414 {
7415 if (!stream.IsOk())
7416 return false;
7417
7418 wxString text = buffer->GetText();
7419 wxCharBuffer buf = text.ToAscii();
7420
7421 stream.Write((const char*) buf, text.length());
7422 return true;
7423 }
7424 #endif // wxUSE_STREAMS
7425
7426 /*
7427 * Stores information about an image, in binary in-memory form
7428 */
7429
7430 wxRichTextImageBlock::wxRichTextImageBlock()
7431 {
7432 Init();
7433 }
7434
7435 wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock& block):wxObject()
7436 {
7437 Init();
7438 Copy(block);
7439 }
7440
7441 wxRichTextImageBlock::~wxRichTextImageBlock()
7442 {
7443 if (m_data)
7444 {
7445 delete[] m_data;
7446 m_data = NULL;
7447 }
7448 }
7449
7450 void wxRichTextImageBlock::Init()
7451 {
7452 m_data = NULL;
7453 m_dataSize = 0;
7454 m_imageType = -1;
7455 }
7456
7457 void wxRichTextImageBlock::Clear()
7458 {
7459 delete[] m_data;
7460 m_data = NULL;
7461 m_dataSize = 0;
7462 m_imageType = -1;
7463 }
7464
7465
7466 // Load the original image into a memory block.
7467 // If the image is not a JPEG, we must convert it into a JPEG
7468 // to conserve space.
7469 // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
7470 // load the image a 2nd time.
7471
7472 bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, int imageType, wxImage& image, bool convertToJPEG)
7473 {
7474 m_imageType = imageType;
7475
7476 wxString filenameToRead(filename);
7477 bool removeFile = false;
7478
7479 if (imageType == -1)
7480 return false; // Could not determine image type
7481
7482 if ((imageType != wxBITMAP_TYPE_JPEG) && convertToJPEG)
7483 {
7484 wxString tempFile;
7485 bool success = wxGetTempFileName(_("image"), tempFile) ;
7486
7487 wxASSERT(success);
7488
7489 wxUnusedVar(success);
7490
7491 image.SaveFile(tempFile, wxBITMAP_TYPE_JPEG);
7492 filenameToRead = tempFile;
7493 removeFile = true;
7494
7495 m_imageType = wxBITMAP_TYPE_JPEG;
7496 }
7497 wxFile file;
7498 if (!file.Open(filenameToRead))
7499 return false;
7500
7501 m_dataSize = (size_t) file.Length();
7502 file.Close();
7503
7504 if (m_data)
7505 delete[] m_data;
7506 m_data = ReadBlock(filenameToRead, m_dataSize);
7507
7508 if (removeFile)
7509 wxRemoveFile(filenameToRead);
7510
7511 return (m_data != NULL);
7512 }
7513
7514 // Make an image block from the wxImage in the given
7515 // format.
7516 bool wxRichTextImageBlock::MakeImageBlock(wxImage& image, int imageType, int quality)
7517 {
7518 m_imageType = imageType;
7519 image.SetOption(wxT("quality"), quality);
7520
7521 if (imageType == -1)
7522 return false; // Could not determine image type
7523
7524 wxString tempFile;
7525 bool success = wxGetTempFileName(_("image"), tempFile) ;
7526
7527 wxASSERT(success);
7528 wxUnusedVar(success);
7529
7530 if (!image.SaveFile(tempFile, m_imageType))
7531 {
7532 if (wxFileExists(tempFile))
7533 wxRemoveFile(tempFile);
7534 return false;
7535 }
7536
7537 wxFile file;
7538 if (!file.Open(tempFile))
7539 return false;
7540
7541 m_dataSize = (size_t) file.Length();
7542 file.Close();
7543
7544 if (m_data)
7545 delete[] m_data;
7546 m_data = ReadBlock(tempFile, m_dataSize);
7547
7548 wxRemoveFile(tempFile);
7549
7550 return (m_data != NULL);
7551 }
7552
7553
7554 // Write to a file
7555 bool wxRichTextImageBlock::Write(const wxString& filename)
7556 {
7557 return WriteBlock(filename, m_data, m_dataSize);
7558 }
7559
7560 void wxRichTextImageBlock::Copy(const wxRichTextImageBlock& block)
7561 {
7562 m_imageType = block.m_imageType;
7563 if (m_data)
7564 {
7565 delete[] m_data;
7566 m_data = NULL;
7567 }
7568 m_dataSize = block.m_dataSize;
7569 if (m_dataSize == 0)
7570 return;
7571
7572 m_data = new unsigned char[m_dataSize];
7573 unsigned int i;
7574 for (i = 0; i < m_dataSize; i++)
7575 m_data[i] = block.m_data[i];
7576 }
7577
7578 //// Operators
7579 void wxRichTextImageBlock::operator=(const wxRichTextImageBlock& block)
7580 {
7581 Copy(block);
7582 }
7583
7584 // Load a wxImage from the block
7585 bool wxRichTextImageBlock::Load(wxImage& image)
7586 {
7587 if (!m_data)
7588 return false;
7589
7590 // Read in the image.
7591 #if wxUSE_STREAMS
7592 wxMemoryInputStream mstream(m_data, m_dataSize);
7593 bool success = image.LoadFile(mstream, GetImageType());
7594 #else
7595 wxString tempFile;
7596 bool success = wxGetTempFileName(_("image"), tempFile) ;
7597 wxASSERT(success);
7598
7599 if (!WriteBlock(tempFile, m_data, m_dataSize))
7600 {
7601 return false;
7602 }
7603 success = image.LoadFile(tempFile, GetImageType());
7604 wxRemoveFile(tempFile);
7605 #endif
7606
7607 return success;
7608 }
7609
7610 // Write data in hex to a stream
7611 bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream)
7612 {
7613 wxString hex;
7614 int i;
7615 for (i = 0; i < (int) m_dataSize; i++)
7616 {
7617 hex = wxDecToHex(m_data[i]);
7618 wxCharBuffer buf = hex.ToAscii();
7619
7620 stream.Write((const char*) buf, hex.length());
7621 }
7622
7623 return true;
7624 }
7625
7626 // Read data in hex from a stream
7627 bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, int imageType)
7628 {
7629 int dataSize = length/2;
7630
7631 if (m_data)
7632 delete[] m_data;
7633
7634 wxString str(wxT(" "));
7635 m_data = new unsigned char[dataSize];
7636 int i;
7637 for (i = 0; i < dataSize; i ++)
7638 {
7639 str[0] = stream.GetC();
7640 str[1] = stream.GetC();
7641
7642 m_data[i] = (unsigned char)wxHexToDec(str);
7643 }
7644
7645 m_dataSize = dataSize;
7646 m_imageType = imageType;
7647
7648 return true;
7649 }
7650
7651 // Allocate and read from stream as a block of memory
7652 unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream& stream, size_t size)
7653 {
7654 unsigned char* block = new unsigned char[size];
7655 if (!block)
7656 return NULL;
7657
7658 stream.Read(block, size);
7659
7660 return block;
7661 }
7662
7663 unsigned char* wxRichTextImageBlock::ReadBlock(const wxString& filename, size_t size)
7664 {
7665 wxFileInputStream stream(filename);
7666 if (!stream.Ok())
7667 return NULL;
7668
7669 return ReadBlock(stream, size);
7670 }
7671
7672 // Write memory block to stream
7673 bool wxRichTextImageBlock::WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size)
7674 {
7675 stream.Write((void*) block, size);
7676 return stream.IsOk();
7677
7678 }
7679
7680 // Write memory block to file
7681 bool wxRichTextImageBlock::WriteBlock(const wxString& filename, unsigned char* block, size_t size)
7682 {
7683 wxFileOutputStream outStream(filename);
7684 if (!outStream.Ok())
7685 return false;
7686
7687 return WriteBlock(outStream, block, size);
7688 }
7689
7690 // Gets the extension for the block's type
7691 wxString wxRichTextImageBlock::GetExtension() const
7692 {
7693 wxImageHandler* handler = wxImage::FindHandler(GetImageType());
7694 if (handler)
7695 return handler->GetExtension();
7696 else
7697 return wxEmptyString;
7698 }
7699
7700 #if wxUSE_DATAOBJ
7701
7702 /*!
7703 * The data object for a wxRichTextBuffer
7704 */
7705
7706 const wxChar *wxRichTextBufferDataObject::ms_richTextBufferFormatId = wxT("wxShape");
7707
7708 wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer* richTextBuffer)
7709 {
7710 m_richTextBuffer = richTextBuffer;
7711
7712 // this string should uniquely identify our format, but is otherwise
7713 // arbitrary
7714 m_formatRichTextBuffer.SetId(GetRichTextBufferFormatId());
7715
7716 SetFormat(m_formatRichTextBuffer);
7717 }
7718
7719 wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
7720 {
7721 delete m_richTextBuffer;
7722 }
7723
7724 // after a call to this function, the richTextBuffer is owned by the caller and it
7725 // is responsible for deleting it!
7726 wxRichTextBuffer* wxRichTextBufferDataObject::GetRichTextBuffer()
7727 {
7728 wxRichTextBuffer* richTextBuffer = m_richTextBuffer;
7729 m_richTextBuffer = NULL;
7730
7731 return richTextBuffer;
7732 }
7733
7734 wxDataFormat wxRichTextBufferDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
7735 {
7736 return m_formatRichTextBuffer;
7737 }
7738
7739 size_t wxRichTextBufferDataObject::GetDataSize() const
7740 {
7741 if (!m_richTextBuffer)
7742 return 0;
7743
7744 wxString bufXML;
7745
7746 {
7747 wxStringOutputStream stream(& bufXML);
7748 if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
7749 {
7750 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7751 return 0;
7752 }
7753 }
7754
7755 #if wxUSE_UNICODE
7756 wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
7757 return strlen(buffer) + 1;
7758 #else
7759 return bufXML.Length()+1;
7760 #endif
7761 }
7762
7763 bool wxRichTextBufferDataObject::GetDataHere(void *pBuf) const
7764 {
7765 if (!pBuf || !m_richTextBuffer)
7766 return false;
7767
7768 wxString bufXML;
7769
7770 {
7771 wxStringOutputStream stream(& bufXML);
7772 if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
7773 {
7774 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
7775 return 0;
7776 }
7777 }
7778
7779 #if wxUSE_UNICODE
7780 wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
7781 size_t len = strlen(buffer);
7782 memcpy((char*) pBuf, (const char*) buffer, len);
7783 ((char*) pBuf)[len] = 0;
7784 #else
7785 size_t len = bufXML.Length();
7786 memcpy((char*) pBuf, (const char*) bufXML.c_str(), len);
7787 ((char*) pBuf)[len] = 0;
7788 #endif
7789
7790 return true;
7791 }
7792
7793 bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len), const void *buf)
7794 {
7795 delete m_richTextBuffer;
7796 m_richTextBuffer = NULL;
7797
7798 wxString bufXML((const char*) buf, wxConvUTF8);
7799
7800 m_richTextBuffer = new wxRichTextBuffer;
7801
7802 wxStringInputStream stream(bufXML);
7803 if (!m_richTextBuffer->LoadFile(stream, wxRICHTEXT_TYPE_XML))
7804 {
7805 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
7806
7807 delete m_richTextBuffer;
7808 m_richTextBuffer = NULL;
7809
7810 return false;
7811 }
7812 return true;
7813 }
7814
7815 #endif
7816 // wxUSE_DATAOBJ
7817
7818 #endif
7819 // wxUSE_RICHTEXT