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