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