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