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