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