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