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