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