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