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