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