-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrichtextbuffer.cpp
More file actions
9757 lines (8107 loc) · 323 KB
/
richtextbuffer.cpp
File metadata and controls
9757 lines (8107 loc) · 323 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////
// Name: src/richtext/richtextbuffer.cpp
// Purpose: Buffer for wxRichTextCtrl
// Author: Julian Smart
// Modified by:
// Created: 2005-09-30
// RCS-ID: $Id: richtextbuffer.cpp 67111 2011-03-02 20:55:16Z JS $
// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_RICHTEXT
#include "wx/richtext/richtextbuffer.h"
#ifndef WX_PRECOMP
#include "wx/dc.h"
#include "wx/intl.h"
#include "wx/log.h"
#include "wx/dataobj.h"
#include "wx/module.h"
#endif
#include "wx/settings.h"
#include "wx/filename.h"
#include "wx/clipbrd.h"
#include "wx/wfstream.h"
#include "wx/mstream.h"
#include "wx/sstream.h"
#include "wx/textfile.h"
#include "wx/richtext/richtextctrl.h"
#include "wx/richtext/richtextstyles.h"
#include "wx/listimpl.cpp"
WX_DEFINE_LIST(wxRichTextObjectList)
WX_DEFINE_LIST(wxRichTextLineList)
// Switch off if the platform doesn't like it for some reason
#define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
const wxChar wxRichTextLineBreakChar = (wxChar) 29;
// Use GetPartialTextExtents for platforms that support it natively
#define wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS 1
// Use global array for binary compatibility; in 2.9+ it will be done more elegantly
#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
wxArrayInt g_GlobalPartialTextExtents;
bool g_UseGlobalPartialTextExtents = false;
#endif
// Helpers for efficiency
inline void wxCheckSetFont(wxDC& dc, const wxFont& font)
{
const wxFont& font1 = dc.GetFont();
if (font1.IsOk() && font.IsOk())
{
if (font1.GetPointSize() == font.GetPointSize() &&
font1.GetFamily() == font.GetFamily() &&
font1.GetStyle() == font.GetStyle() &&
font1.GetWeight() == font.GetWeight() &&
font1.GetUnderlined() == font.GetUnderlined() &&
font1.GetFaceName() == font.GetFaceName())
return;
}
dc.SetFont(font);
}
inline void wxCheckSetPen(wxDC& dc, const wxPen& pen)
{
const wxPen& pen1 = dc.GetPen();
if (pen1.IsOk() && pen.IsOk())
{
if (pen1.GetWidth() == pen.GetWidth() &&
pen1.GetStyle() == pen.GetStyle() &&
pen1.GetColour() == pen.GetColour())
return;
}
dc.SetPen(pen);
}
inline void wxCheckSetBrush(wxDC& dc, const wxBrush& brush)
{
const wxBrush& brush1 = dc.GetBrush();
if (brush1.IsOk() && brush.IsOk())
{
if (brush1.GetStyle() == brush.GetStyle() &&
brush1.GetColour() == brush.GetColour())
return;
}
dc.SetBrush(brush);
}
// Functions that set properties without using the accessors
inline void wxFontSetPointSize(wxFont& font, int pointSize)
{
if (font.Ok() && font.GetPointSize() != pointSize)
{
wxFont tempFont(pointSize, font.GetFamily(), font.GetStyle(), font.GetWeight(), font.GetUnderlined(), font.GetFaceName(), font.GetEncoding());
font = tempFont;
}
}
inline void wxFontSetStyle(wxFont& font, int fontStyle)
{
if (font.Ok() && font.GetStyle() != fontStyle)
{
wxFont tempFont(font.GetPointSize(), font.GetFamily(), fontStyle, font.GetWeight(), font.GetUnderlined(), font.GetFaceName(), font.GetEncoding());
font = tempFont;
}
}
inline void wxFontSetWeight(wxFont& font, int fontWeight)
{
if (font.Ok() && font.GetWeight() != fontWeight)
{
wxFont tempFont(font.GetPointSize(), font.GetFamily(), font.GetStyle(), fontWeight, font.GetUnderlined(), font.GetFaceName(), font.GetEncoding());
font = tempFont;
}
}
inline void wxFontSetUnderlined(wxFont& font, bool underlined)
{
if (font.Ok() && font.GetUnderlined() != underlined)
{
wxFont tempFont(font.GetPointSize(), font.GetFamily(), font.GetStyle(), font.GetWeight(), underlined, font.GetFaceName(), font.GetEncoding());
font = tempFont;
}
}
inline void wxFontSetFaceName(wxFont& font, const wxString& faceName)
{
if (font.Ok() && font.GetFaceName() != faceName)
{
wxFont tempFont(font.GetPointSize(), font.GetFamily(), font.GetStyle(), font.GetWeight(), font.GetUnderlined(), faceName, font.GetEncoding());
font = tempFont;
}
}
/*!
* wxRichTextObject
* This is the base for drawable objects.
*/
IMPLEMENT_CLASS(wxRichTextObject, wxObject)
wxRichTextObject::wxRichTextObject(wxRichTextObject* parent)
{
m_dirty = false;
m_refCount = 1;
m_parent = parent;
m_leftMargin = 0;
m_rightMargin = 0;
m_topMargin = 0;
m_bottomMargin = 0;
m_descent = 0;
}
wxRichTextObject::~wxRichTextObject()
{
}
void wxRichTextObject::Dereference()
{
m_refCount --;
if (m_refCount <= 0)
delete this;
}
/// Copy
void wxRichTextObject::Copy(const wxRichTextObject& obj)
{
m_size = obj.m_size;
m_pos = obj.m_pos;
m_dirty = obj.m_dirty;
m_range = obj.m_range;
m_attributes = obj.m_attributes;
m_descent = obj.m_descent;
}
void wxRichTextObject::SetMargins(int margin)
{
m_leftMargin = m_rightMargin = m_topMargin = m_bottomMargin = margin;
}
void wxRichTextObject::SetMargins(int leftMargin, int rightMargin, int topMargin, int bottomMargin)
{
m_leftMargin = leftMargin;
m_rightMargin = rightMargin;
m_topMargin = topMargin;
m_bottomMargin = bottomMargin;
}
// Convert units in tenths of a millimetre to device units
int wxRichTextObject::ConvertTenthsMMToPixels(wxDC& dc, int units)
{
int p = ConvertTenthsMMToPixels(dc.GetPPI().x, units);
// Unscale
wxRichTextBuffer* buffer = GetBuffer();
if (buffer)
p = (int) ((double)p / buffer->GetScale());
return p;
}
// Convert units in tenths of a millimetre to device units
int wxRichTextObject::ConvertTenthsMMToPixels(int ppi, int units)
{
// There are ppi pixels in 254.1 "1/10 mm"
double pixels = ((double) units * (double)ppi) / 254.1;
return (int) pixels;
}
/// Dump to output stream for debugging
void wxRichTextObject::Dump(wxTextOutputStream& stream)
{
stream << GetClassInfo()->GetClassName() << wxT("\n");
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");
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");
}
/// Gets the containing buffer
wxRichTextBuffer* wxRichTextObject::GetBuffer() const
{
const wxRichTextObject* obj = this;
while (obj && !obj->IsKindOf(CLASSINFO(wxRichTextBuffer)))
obj = obj->GetParent();
return wxDynamicCast(obj, wxRichTextBuffer);
}
/*!
* wxRichTextCompositeObject
* This is the base for drawable objects.
*/
IMPLEMENT_CLASS(wxRichTextCompositeObject, wxRichTextObject)
wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject* parent):
wxRichTextObject(parent)
{
}
wxRichTextCompositeObject::~wxRichTextCompositeObject()
{
DeleteChildren();
}
/// Get the nth child
wxRichTextObject* wxRichTextCompositeObject::GetChild(size_t n) const
{
wxASSERT ( n < m_children.GetCount() );
return m_children.Item(n)->GetData();
}
/// Append a child, returning the position
size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject* child)
{
m_children.Append(child);
child->SetParent(this);
return m_children.GetCount() - 1;
}
/// Insert the child in front of the given object, or at the beginning
bool wxRichTextCompositeObject::InsertChild(wxRichTextObject* child, wxRichTextObject* inFrontOf)
{
if (inFrontOf)
{
wxRichTextObjectList::compatibility_iterator node = m_children.Find(inFrontOf);
m_children.Insert(node, child);
}
else
m_children.Insert(child);
child->SetParent(this);
return true;
}
/// Delete the child
bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject* child, bool deleteChild)
{
wxRichTextObjectList::compatibility_iterator node = m_children.Find(child);
if (node)
{
wxRichTextObject* obj = node->GetData();
m_children.Erase(node);
if (deleteChild)
delete obj;
return true;
}
return false;
}
/// Delete all children
bool wxRichTextCompositeObject::DeleteChildren()
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObjectList::compatibility_iterator oldNode = node;
wxRichTextObject* child = node->GetData();
child->Dereference(); // Only delete if reference count is zero
node = node->GetNext();
m_children.Erase(oldNode);
}
return true;
}
/// Get the child count
size_t wxRichTextCompositeObject::GetChildCount() const
{
return m_children.GetCount();
}
/// Copy
void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject& obj)
{
wxRichTextObject::Copy(obj);
DeleteChildren();
wxRichTextObjectList::compatibility_iterator node = obj.m_children.GetFirst();
while (node)
{
wxRichTextObject* child = node->GetData();
wxRichTextObject* newChild = child->Clone();
newChild->SetParent(this);
m_children.Append(newChild);
node = node->GetNext();
}
}
/// Hit-testing: returns a flag indicating hit test details, plus
/// information about position
int wxRichTextCompositeObject::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition)
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* child = node->GetData();
int ret = child->HitTest(dc, pt, textPosition);
if (ret != wxRICHTEXT_HITTEST_NONE)
return ret;
node = node->GetNext();
}
textPosition = GetRange().GetEnd()-1;
return wxRICHTEXT_HITTEST_AFTER|wxRICHTEXT_HITTEST_OUTSIDE;
}
/// Finds the absolute position and row height for the given character position
bool wxRichTextCompositeObject::FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart)
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* child = node->GetData();
if (child->FindPosition(dc, index, pt, height, forceLineStart))
return true;
node = node->GetNext();
}
return false;
}
/// Calculate range
void wxRichTextCompositeObject::CalculateRange(long start, long& end)
{
long current = start;
long lastEnd = current;
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* child = node->GetData();
long childEnd = 0;
child->CalculateRange(current, childEnd);
lastEnd = childEnd;
current = childEnd + 1;
node = node->GetNext();
}
end = lastEnd;
// An object with no children has zero length
if (m_children.GetCount() == 0)
end --;
m_range.SetRange(start, end);
}
/// Delete range from layout.
bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange& range)
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* obj = (wxRichTextObject*) node->GetData();
wxRichTextObjectList::compatibility_iterator next = node->GetNext();
// Delete the range in each paragraph
// When a chunk has been deleted, internally the content does not
// now match the ranges.
// However, so long as deletion is not done on the same object twice this is OK.
// If you may delete content from the same object twice, recalculate
// the ranges inbetween DeleteRange calls by calling CalculateRanges, and
// adjust the range you're deleting accordingly.
if (!obj->GetRange().IsOutside(range))
{
obj->DeleteRange(range);
// Delete an empty object, or paragraph within this range.
if (obj->IsEmpty() ||
(range.GetStart() <= obj->GetRange().GetStart() && range.GetEnd() >= obj->GetRange().GetEnd()))
{
// An empty paragraph has length 1, so won't be deleted unless the
// whole range is deleted.
RemoveChild(obj, true);
}
}
node = next;
}
return true;
}
/// Get any text in this object for the given range
wxString wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange& range) const
{
wxString text;
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* child = node->GetData();
wxRichTextRange childRange = range;
if (!child->GetRange().IsOutside(range))
{
childRange.LimitTo(child->GetRange());
wxString childText = child->GetTextForRange(childRange);
text += childText;
}
node = node->GetNext();
}
return text;
}
/// Recursively merge all pieces that can be merged.
bool wxRichTextCompositeObject::Defragment()
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* child = node->GetData();
wxRichTextCompositeObject* composite = wxDynamicCast(child, wxRichTextCompositeObject);
if (composite)
composite->Defragment();
if (node->GetNext())
{
wxRichTextObject* nextChild = node->GetNext()->GetData();
if (child->CanMerge(nextChild) && child->Merge(nextChild))
{
nextChild->Dereference();
m_children.Erase(node->GetNext());
// Don't set node -- we'll see if we can merge again with the next
// child.
}
else
{
node = node->GetNext();
}
}
else
node = node->GetNext();
}
return true;
}
/// Dump to output stream for debugging
void wxRichTextCompositeObject::Dump(wxTextOutputStream& stream)
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* child = node->GetData();
child->Dump(stream);
node = node->GetNext();
}
}
/*!
* wxRichTextBox
* This defines a 2D space to lay out objects
*/
IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox, wxRichTextCompositeObject)
wxRichTextBox::wxRichTextBox(wxRichTextObject* parent):
wxRichTextCompositeObject(parent)
{
}
/// Draw the item
bool wxRichTextBox::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& WXUNUSED(rect), int descent, int style)
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* child = node->GetData();
wxRect childRect = wxRect(child->GetPosition(), child->GetCachedSize());
child->Draw(dc, range, selectionRange, childRect, descent, style);
node = node->GetNext();
}
return true;
}
/// Lay the item out
bool wxRichTextBox::Layout(wxDC& dc, const wxRect& rect, int style)
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* child = node->GetData();
child->Layout(dc, rect, style);
node = node->GetNext();
}
m_dirty = false;
return true;
}
/// Get/set the size for the given range. Assume only has one child.
bool wxRichTextBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
if (node)
{
wxRichTextObject* child = node->GetData();
return child->GetRangeSize(range, size, descent, dc, flags, position);
}
else
return false;
}
/// Copy
void wxRichTextBox::Copy(const wxRichTextBox& obj)
{
wxRichTextCompositeObject::Copy(obj);
}
/*!
* wxRichTextParagraphLayoutBox
* This box knows how to lay out paragraphs.
*/
IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox, wxRichTextBox)
wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject* parent):
wxRichTextBox(parent)
{
Init();
}
/// Initialize the object.
void wxRichTextParagraphLayoutBox::Init()
{
m_ctrl = NULL;
// For now, assume is the only box and has no initial size.
m_range = wxRichTextRange(0, -1);
m_invalidRange.SetRange(-1, -1);
m_leftMargin = 4;
m_rightMargin = 4;
m_topMargin = 4;
m_bottomMargin = 4;
m_partialParagraph = false;
}
/// Draw the item
bool wxRichTextParagraphLayoutBox::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style)
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
wxASSERT (child != NULL);
if (child && !child->GetRange().IsOutside(range))
{
wxRect childRect(child->GetPosition(), child->GetCachedSize());
if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) == 0) && childRect.GetTop() > rect.GetBottom())
{
// Stop drawing
break;
}
else if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) == 0) && childRect.GetBottom() < rect.GetTop())
{
// Skip
}
else
child->Draw(dc, range, selectionRange, rect, descent, style);
}
node = node->GetNext();
}
return true;
}
/// Lay the item out
bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int style)
{
wxRect availableSpace;
bool formatRect = (style & wxRICHTEXT_LAYOUT_SPECIFIED_RECT) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
// If only laying out a specific area, the passed rect has a different meaning:
// the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
// so that during a size, only the visible part will be relaid out, or
// it would take too long causing flicker. As an approximation, we assume that
// everything up to the start of the visible area is laid out correctly.
if (formatRect)
{
availableSpace = wxRect(0 + m_leftMargin,
0 + m_topMargin,
rect.width - m_leftMargin - m_rightMargin,
rect.height);
// Invalidate the part of the buffer from the first visible line
// to the end. If other parts of the buffer are currently invalid,
// then they too will be taken into account if they are above
// the visible point.
long startPos = 0;
wxRichTextLine* line = GetLineAtYPosition(rect.y);
if (line)
startPos = line->GetAbsoluteRange().GetStart();
Invalidate(wxRichTextRange(startPos, GetRange().GetEnd()));
}
else
availableSpace = wxRect(rect.x + m_leftMargin,
rect.y + m_topMargin,
rect.width - m_leftMargin - m_rightMargin,
rect.height - m_topMargin - m_bottomMargin);
int maxWidth = 0;
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
bool layoutAll = true;
// Get invalid range, rounding to paragraph start/end.
wxRichTextRange invalidRange = GetInvalidRange(true);
if (invalidRange == wxRICHTEXT_NONE && !formatRect)
return true;
if (invalidRange == wxRICHTEXT_ALL)
layoutAll = true;
else // If we know what range is affected, start laying out from that point on.
if (invalidRange.GetStart() >= GetRange().GetStart())
{
wxRichTextParagraph* firstParagraph = GetParagraphAtPosition(invalidRange.GetStart());
if (firstParagraph)
{
wxRichTextObjectList::compatibility_iterator firstNode = m_children.Find(firstParagraph);
wxRichTextObjectList::compatibility_iterator previousNode;
if ( firstNode )
previousNode = firstNode->GetPrevious();
if (firstNode)
{
if (previousNode)
{
wxRichTextParagraph* previousParagraph = wxDynamicCast(previousNode->GetData(), wxRichTextParagraph);
availableSpace.y = previousParagraph->GetPosition().y + previousParagraph->GetCachedSize().y;
}
// Now we're going to start iterating from the first affected paragraph.
node = firstNode;
layoutAll = false;
}
}
}
// A way to force speedy rest-of-buffer layout (the 'else' below)
bool forceQuickLayout = false;
while (node)
{
// Assume this box only contains paragraphs
wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
wxCHECK_MSG( child, false, _T("Unknown object in layout") );
// TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
if ( !forceQuickLayout &&
(layoutAll ||
child->GetLines().IsEmpty() ||
!child->GetRange().IsOutside(invalidRange)) )
{
child->Layout(dc, availableSpace, style);
// Layout must set the cached size
availableSpace.y += child->GetCachedSize().y;
maxWidth = wxMax(maxWidth, child->GetCachedSize().x);
// If we're just formatting the visible part of the buffer,
// and we're now past the bottom of the window, start quick
// layout.
if (formatRect && child->GetPosition().y > rect.GetBottom())
forceQuickLayout = true;
}
else
{
// We're outside the immediately affected range, so now let's just
// move everything up or down. This assumes that all the children have previously
// been laid out and have wrapped line lists associated with them.
// TODO: check all paragraphs before the affected range.
int inc = availableSpace.y - child->GetPosition().y;
while (node)
{
wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
if (child)
{
if (child->GetLines().GetCount() == 0)
child->Layout(dc, availableSpace, style);
else
child->SetPosition(wxPoint(child->GetPosition().x, child->GetPosition().y + inc));
availableSpace.y += child->GetCachedSize().y;
maxWidth = wxMax(maxWidth, child->GetCachedSize().x);
}
node = node->GetNext();
}
break;
}
node = node->GetNext();
}
SetCachedSize(wxSize(maxWidth, availableSpace.y));
m_dirty = false;
m_invalidRange = wxRICHTEXT_NONE;
return true;
}
/// Copy
void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox& obj)
{
wxRichTextBox::Copy(obj);
m_partialParagraph = obj.m_partialParagraph;
m_defaultAttributes = obj.m_defaultAttributes;
}
/// Get/set the size for the given range.
bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags, wxPoint position) const
{
wxSize sz;
wxRichTextObjectList::compatibility_iterator startPara = wxRichTextObjectList::compatibility_iterator();
wxRichTextObjectList::compatibility_iterator endPara = wxRichTextObjectList::compatibility_iterator();
// First find the first paragraph whose starting position is within the range.
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
// child is a paragraph
wxRichTextObject* child = node->GetData();
const wxRichTextRange& r = child->GetRange();
if (r.GetStart() <= range.GetStart() && r.GetEnd() >= range.GetStart())
{
startPara = node;
break;
}
node = node->GetNext();
}
// Next find the last paragraph containing part of the range
node = m_children.GetFirst();
while (node)
{
// child is a paragraph
wxRichTextObject* child = node->GetData();
const wxRichTextRange& r = child->GetRange();
if (r.GetStart() <= range.GetEnd() && r.GetEnd() >= range.GetEnd())
{
endPara = node;
break;
}
node = node->GetNext();
}
if (!startPara || !endPara)
return false;
// Now we can add up the sizes
for (node = startPara; node ; node = node->GetNext())
{
// child is a paragraph
wxRichTextObject* child = node->GetData();
const wxRichTextRange& childRange = child->GetRange();
wxRichTextRange rangeToFind = range;
rangeToFind.LimitTo(childRange);
wxSize childSize;
int childDescent = 0;
child->GetRangeSize(rangeToFind, childSize, childDescent, dc, flags, position);
descent = wxMax(childDescent, descent);
sz.x = wxMax(sz.x, childSize.x);
sz.y += childSize.y;
if (node == endPara)
break;
}
size = sz;
return true;
}
/// Get the paragraph at the given position
wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos, bool caretPosition) const
{
if (caretPosition)
pos ++;
// First find the first paragraph whose starting position is within the range.
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
// child is a paragraph
wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
wxASSERT (child != NULL);
// Return first child in buffer if position is -1
// if (pos == -1)
// return child;
if (child->GetRange().Contains(pos))
return child;
node = node->GetNext();
}
return NULL;
}
/// Get the line at the given position
wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos, bool caretPosition) const
{
if (caretPosition)
pos ++;
// First find the first paragraph whose starting position is within the range.
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextObject* obj = (wxRichTextObject*) node->GetData();
if (obj->GetRange().Contains(pos))
{
// child is a paragraph
wxRichTextParagraph* child = wxDynamicCast(obj, wxRichTextParagraph);
wxASSERT (child != NULL);
wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
while (node2)
{
wxRichTextLine* line = node2->GetData();
wxRichTextRange range = line->GetAbsoluteRange();
if (range.Contains(pos) ||
// If the position is end-of-paragraph, then return the last line of
// of the paragraph.
((range.GetEnd() == child->GetRange().GetEnd()-1) && (pos == child->GetRange().GetEnd())))
return line;
node2 = node2->GetNext();
}
}
node = node->GetNext();
}
int lineCount = GetLineCount();
if (lineCount > 0)
return GetLineForVisibleLineNumber(lineCount-1);
else
return NULL;
}
/// Get the line at the given y pixel position, or the last line.
wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y) const
{
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
wxASSERT (child != NULL);
wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
while (node2)
{
wxRichTextLine* line = node2->GetData();
wxRect rect(line->GetRect());
if (y <= rect.GetBottom())
return line;
node2 = node2->GetNext();
}
node = node->GetNext();
}
// Return last line
int lineCount = GetLineCount();
if (lineCount > 0)
return GetLineForVisibleLineNumber(lineCount-1);
else
return NULL;
}
/// Get the number of visible lines
int wxRichTextParagraphLayoutBox::GetLineCount() const
{
int count = 0;
wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
while (node)
{
wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
wxASSERT (child != NULL);
count += child->GetLines().GetCount();
node = node->GetNext();
}
return count;
}
/// Get the paragraph for a given line
wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine* line) const
{
return GetParagraphAtPosition(line->GetAbsoluteRange().GetStart());
}