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