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