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