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