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