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