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