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