]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextbuffer.cpp
Return NULL from wxWindow::GetCapture() when the capture is being lost.
[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
5d7836c4
JS
7// Copyright: (c) Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
2be72ac2 10
5d7836c4
JS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
61399247 15 #pragma hdrstop
5d7836c4
JS
16#endif
17
b01ca8b6
JS
18#if wxUSE_RICHTEXT
19
20#include "wx/richtext/richtextbuffer.h"
21
5d7836c4 22#ifndef WX_PRECOMP
61399247
WS
23 #include "wx/dc.h"
24 #include "wx/intl.h"
7947a48a 25 #include "wx/log.h"
28f92d74 26 #include "wx/dataobj.h"
02761f6c 27 #include "wx/module.h"
5d7836c4
JS
28#endif
29
0ec6da02 30#include "wx/settings.h"
5d7836c4
JS
31#include "wx/filename.h"
32#include "wx/clipbrd.h"
33#include "wx/wfstream.h"
5d7836c4
JS
34#include "wx/mstream.h"
35#include "wx/sstream.h"
0ca07313 36#include "wx/textfile.h"
44cc96a8 37#include "wx/hashmap.h"
cdaed652 38#include "wx/dynarray.h"
5d7836c4 39
5d7836c4
JS
40#include "wx/richtext/richtextctrl.h"
41#include "wx/richtext/richtextstyles.h"
cdaed652 42#include "wx/richtext/richtextimagedlg.h"
603f702b 43#include "wx/richtext/richtextsizepage.h"
1aca9fcd 44#include "wx/richtext/richtextxml.h"
5d7836c4
JS
45
46#include "wx/listimpl.cpp"
bec80f4f 47#include "wx/arrimpl.cpp"
5d7836c4 48
412e0d47
DS
49WX_DEFINE_LIST(wxRichTextObjectList)
50WX_DEFINE_LIST(wxRichTextLineList)
5d7836c4 51
ea160b2e
JS
52// Switch off if the platform doesn't like it for some reason
53#define wxRICHTEXT_USE_OPTIMIZED_DRAWING 1
54
31778480
JS
55// Use GetPartialTextExtents for platforms that support it natively
56#define wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS 1
57
ff76711f
JS
58const wxChar wxRichTextLineBreakChar = (wxChar) 29;
59
cdaed652
VZ
60// Helper classes for floating layout
61struct wxRichTextFloatRectMap
62{
63 wxRichTextFloatRectMap(int sY, int eY, int w, wxRichTextObject* obj)
64 {
65 startY = sY;
66 endY = eY;
67 width = w;
68 anchor = obj;
69 }
70
71 int startY, endY;
72 int width;
73 wxRichTextObject* anchor;
74};
75
76WX_DEFINE_SORTED_ARRAY(wxRichTextFloatRectMap*, wxRichTextFloatRectMapArray);
77
78int wxRichTextFloatRectMapCmp(wxRichTextFloatRectMap* r1, wxRichTextFloatRectMap* r2)
79{
80 return r1->startY - r2->startY;
81}
82
83class wxRichTextFloatCollector
84{
85public:
603f702b 86 wxRichTextFloatCollector(const wxRect& availableRect);
cdaed652
VZ
87 ~wxRichTextFloatCollector();
88
89 // Collect the floating objects info in the given paragraph
90 void CollectFloat(wxRichTextParagraph* para);
91 void CollectFloat(wxRichTextParagraph* para, wxRichTextObject* floating);
92
93 // Return the last paragraph we collected
94 wxRichTextParagraph* LastParagraph();
95
96 // Given the start y position and the height of the line,
97 // find out how wide the line can be
98 wxRect GetAvailableRect(int startY, int endY);
99
100 // Given a floating box, find its fit position
101 int GetFitPosition(int direction, int start, int height) const;
102 int GetFitPosition(const wxRichTextFloatRectMapArray& array, int start, int height) const;
103
104 // Find the last y position
105 int GetLastRectBottom();
106
107 // Draw the floats inside a rect
8db2e3ef 108 void Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int style);
cdaed652
VZ
109
110 // HitTest the floats
8db2e3ef 111 int HitTest(wxDC& dc, wxRichTextDrawingContext& context, const wxPoint& pt, long& textPosition, wxRichTextObject** obj, int flags);
603f702b
JS
112
113 // Get floating object count
114 int GetFloatingObjectCount() const { return m_left.GetCount() + m_right.GetCount(); }
115
116 // Get floating objects
117 bool GetFloatingObjects(wxRichTextObjectList& objects) const;
cdaed652 118
07d4142f
JS
119 // Delete a float
120 bool DeleteFloat(wxRichTextObject* obj);
121
122 // Do we have this float already?
123 bool HasFloat(wxRichTextObject* obj);
124
125 bool HasFloats() const { return m_left.GetCount() >0 || m_right.GetCount() > 0; }
126
cdaed652
VZ
127 static int SearchAdjacentRect(const wxRichTextFloatRectMapArray& array, int point);
128
129 static int GetWidthFromFloatRect(const wxRichTextFloatRectMapArray& array, int index, int startY, int endY);
ce00f59b 130
cdaed652
VZ
131 static void FreeFloatRectMapArray(wxRichTextFloatRectMapArray& array);
132
8db2e3ef 133 static void DrawFloat(const wxRichTextFloatRectMapArray& array, wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int style);
cdaed652 134
8db2e3ef 135 static int HitTestFloat(const wxRichTextFloatRectMapArray& array, wxDC& dc, wxRichTextDrawingContext& context, const wxPoint& pt, long& textPosition, wxRichTextObject** obj, int flags);
ce00f59b 136
cdaed652
VZ
137private:
138 wxRichTextFloatRectMapArray m_left;
139 wxRichTextFloatRectMapArray m_right;
603f702b
JS
140 //int m_width;
141 wxRect m_availableRect;
cdaed652
VZ
142 wxRichTextParagraph* m_para;
143};
144
07d4142f
JS
145// Delete a float
146bool wxRichTextFloatCollector::DeleteFloat(wxRichTextObject* obj)
147{
148 size_t i;
149 for (i = 0; i < m_left.GetCount(); i++)
150 {
151 if (m_left[i]->anchor == obj)
152 {
153 m_left.RemoveAt(i);
154 return true;
155 }
156 }
157 for (i = 0; i < m_right.GetCount(); i++)
158 {
159 if (m_right[i]->anchor == obj)
160 {
161 m_right.RemoveAt(i);
162 return true;
163 }
164 }
165 return false;
166}
167
168// Do we have this float already?
169bool wxRichTextFloatCollector::HasFloat(wxRichTextObject* obj)
170{
171 size_t i;
172 for (i = 0; i < m_left.GetCount(); i++)
173 {
174 if (m_left[i]->anchor == obj)
175 {
176 return true;
177 }
178 }
179 for (i = 0; i < m_right.GetCount(); i++)
180 {
181 if (m_right[i]->anchor == obj)
182 {
183 return true;
184 }
185 }
186 return false;
187}
188
603f702b
JS
189// Get floating objects
190bool wxRichTextFloatCollector::GetFloatingObjects(wxRichTextObjectList& objects) const
191{
192 size_t i;
193 for (i = 0; i < m_left.GetCount(); i++)
194 objects.Append(m_left[i]->anchor);
195 for (i = 0; i < m_right.GetCount(); i++)
196 objects.Append(m_right[i]->anchor);
197 return true;
198}
199
200
cdaed652
VZ
201/*
202 * Binary search helper function
203 * The argument point is the Y coordinate, and this fuction
204 * always return the floating rect that contain this coordinate
205 * or under this coordinate.
206 */
207int wxRichTextFloatCollector::SearchAdjacentRect(const wxRichTextFloatRectMapArray& array, int point)
208{
209 int end = array.GetCount() - 1;
210 int start = 0;
211 int ret = 0;
212
213 wxASSERT(end >= 0);
214
215 while (true)
216 {
217 if (start > end)
218 {
219 break;
220 }
221
222 int mid = (start + end) / 2;
223 if (array[mid]->startY <= point && array[mid]->endY >= point)
224 return mid;
225 else if (array[mid]->startY > point)
226 {
227 end = mid - 1;
228 ret = mid;
229 }
230 else if (array[mid]->endY < point)
231 {
232 start = mid + 1;
233 ret = start;
234 }
235 }
236
237 return ret;
238}
239
240int wxRichTextFloatCollector::GetWidthFromFloatRect(const wxRichTextFloatRectMapArray& array, int index, int startY, int endY)
241{
242 int ret = 0;
243 int len = array.GetCount();
244
245 wxASSERT(index >= 0 && index < len);
246
247 if (array[index]->startY < startY && array[index]->endY > startY)
248 ret = ret < array[index]->width ? array[index]->width : ret;
249 while (index < len && array[index]->startY <= endY)
250 {
251 ret = ret < array[index]->width ? array[index]->width : ret;
252 index++;
253 }
254
255 return ret;
256}
257
603f702b 258wxRichTextFloatCollector::wxRichTextFloatCollector(const wxRect& rect) : m_left(wxRichTextFloatRectMapCmp), m_right(wxRichTextFloatRectMapCmp)
cdaed652 259{
603f702b 260 m_availableRect = rect;
cdaed652
VZ
261 m_para = NULL;
262}
263
264void wxRichTextFloatCollector::FreeFloatRectMapArray(wxRichTextFloatRectMapArray& array)
265{
266 int len = array.GetCount();
267 for (int i = 0; i < len; i++)
268 delete array[i];
269}
270
271wxRichTextFloatCollector::~wxRichTextFloatCollector()
272{
273 FreeFloatRectMapArray(m_left);
274 FreeFloatRectMapArray(m_right);
275}
276
277int wxRichTextFloatCollector::GetFitPosition(const wxRichTextFloatRectMapArray& array, int start, int height) const
278{
279 if (array.GetCount() == 0)
280 return start;
281
603f702b 282 int i = SearchAdjacentRect(array, start);
cdaed652 283 int last = start;
603f702b 284 while (i < (int) array.GetCount())
cdaed652
VZ
285 {
286 if (array[i]->startY - last >= height)
287 return last + 1;
288 last = array[i]->endY;
289 i++;
290 }
291
292 return last + 1;
293}
294
295int wxRichTextFloatCollector::GetFitPosition(int direction, int start, int height) const
296{
24777478 297 if (direction == wxTEXT_BOX_ATTR_FLOAT_LEFT)
cdaed652 298 return GetFitPosition(m_left, start, height);
24777478 299 else if (direction == wxTEXT_BOX_ATTR_FLOAT_RIGHT)
cdaed652
VZ
300 return GetFitPosition(m_right, start, height);
301 else
302 {
303 wxASSERT("Never should be here");
304 return start;
305 }
306}
307
603f702b
JS
308// Adds a floating image to the float collector.
309// The actual positioning is done by wxRichTextParagraph::LayoutFloat.
cdaed652
VZ
310void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph* para, wxRichTextObject* floating)
311{
603f702b 312 int direction = floating->GetFloatDirection();
24777478 313
603f702b
JS
314 wxPoint pos = floating->GetPosition();
315 wxSize size = floating->GetCachedSize();
316 wxRichTextFloatRectMap *map = new wxRichTextFloatRectMap(pos.y, pos.y + size.y, size.x, floating);
317 switch (direction)
318 {
319 case wxTEXT_BOX_ATTR_FLOAT_NONE:
320 delete map;
321 break;
322 case wxTEXT_BOX_ATTR_FLOAT_LEFT:
323 // Just a not-enough simple assertion
324 wxASSERT (m_left.Index(map) == wxNOT_FOUND);
325 m_left.Add(map);
326 break;
327 case wxTEXT_BOX_ATTR_FLOAT_RIGHT:
328 wxASSERT (m_right.Index(map) == wxNOT_FOUND);
329 m_right.Add(map);
330 break;
331 default:
332 delete map;
333 wxASSERT("Unrecognised float attribute.");
334 }
cdaed652 335
603f702b 336 m_para = para;
cdaed652
VZ
337}
338
339void wxRichTextFloatCollector::CollectFloat(wxRichTextParagraph* para)
340{
341 wxRichTextObjectList::compatibility_iterator node = para->GetChildren().GetFirst();
342 while (node)
343 {
344 wxRichTextObject* floating = node->GetData();
ce00f59b 345
cdaed652
VZ
346 if (floating->IsFloating())
347 {
bec80f4f 348 CollectFloat(para, floating);
cdaed652 349 }
ce00f59b 350
cdaed652
VZ
351 node = node->GetNext();
352 }
353
354 m_para = para;
355}
356
357wxRichTextParagraph* wxRichTextFloatCollector::LastParagraph()
358{
359 return m_para;
360}
361
362wxRect wxRichTextFloatCollector::GetAvailableRect(int startY, int endY)
363{
364 int widthLeft = 0, widthRight = 0;
365 if (m_left.GetCount() != 0)
366 {
603f702b
JS
367 int i = SearchAdjacentRect(m_left, startY);
368 if (i < (int) m_left.GetCount())
cdaed652
VZ
369 widthLeft = GetWidthFromFloatRect(m_left, i, startY, endY);
370 }
371 if (m_right.GetCount() != 0)
372 {
603f702b
JS
373 int j = SearchAdjacentRect(m_right, startY);
374 if (j < (int) m_right.GetCount())
cdaed652
VZ
375 widthRight = GetWidthFromFloatRect(m_right, j, startY, endY);
376 }
377
603f702b
JS
378 // TODO: actually we want to use the actual image positions to find the
379 // available remaining space, since the image might not be right up against
380 // the left or right edge of the container.
381 return wxRect(widthLeft + m_availableRect.x, 0, m_availableRect.width - widthLeft - widthRight, 0);
cdaed652
VZ
382}
383
384int wxRichTextFloatCollector::GetLastRectBottom()
385{
386 int ret = 0;
387 int len = m_left.GetCount();
388 if (len) {
389 ret = ret > m_left[len-1]->endY ? ret : m_left[len-1]->endY;
390 }
391 len = m_right.GetCount();
392 if (len) {
393 ret = ret > m_right[len-1]->endY ? ret : m_right[len-1]->endY;
394 }
395
396 return ret;
397}
398
8db2e3ef 399void wxRichTextFloatCollector::DrawFloat(const wxRichTextFloatRectMapArray& array, wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& WXUNUSED(range), const wxRichTextSelection& selection, const wxRect& rect, int descent, int style)
cdaed652
VZ
400{
401 int start = rect.y;
402 int end = rect.y + rect.height;
603f702b 403 int i, j;
cdaed652 404 i = SearchAdjacentRect(array, start);
603f702b 405 if (i < 0 || i >= (int) array.GetCount())
cdaed652
VZ
406 return;
407 j = SearchAdjacentRect(array, end);
603f702b 408 if (j < 0 || j >= (int) array.GetCount())
cdaed652
VZ
409 j = array.GetCount() - 1;
410 while (i <= j)
411 {
412 wxRichTextObject* obj = array[i]->anchor;
413 wxRichTextRange r = obj->GetRange();
8db2e3ef 414 obj->Draw(dc, context, r, selection, wxRect(obj->GetPosition(), obj->GetCachedSize()), descent, style);
cdaed652
VZ
415 i++;
416 }
417}
ecb5fbf1 418
8db2e3ef 419void wxRichTextFloatCollector::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int style)
cdaed652
VZ
420{
421 if (m_left.GetCount() > 0)
8db2e3ef 422 DrawFloat(m_left, dc, context, range, selection, rect, descent, style);
cdaed652 423 if (m_right.GetCount() > 0)
8db2e3ef 424 DrawFloat(m_right, dc, context, range, selection, rect, descent, style);
cdaed652
VZ
425}
426
8db2e3ef 427int wxRichTextFloatCollector::HitTestFloat(const wxRichTextFloatRectMapArray& array, wxDC& WXUNUSED(dc), wxRichTextDrawingContext& WXUNUSED(context), const wxPoint& pt, long& textPosition, wxRichTextObject** obj, int WXUNUSED(flags))
cdaed652 428{
603f702b 429 int i;
cdaed652
VZ
430 if (array.GetCount() == 0)
431 return wxRICHTEXT_HITTEST_NONE;
432 i = SearchAdjacentRect(array, pt.y);
603f702b 433 if (i < 0 || i >= (int) array.GetCount())
cdaed652 434 return wxRICHTEXT_HITTEST_NONE;
603f702b
JS
435 if (!array[i]->anchor->IsShown())
436 return wxRICHTEXT_HITTEST_NONE;
437
cdaed652
VZ
438 wxPoint point = array[i]->anchor->GetPosition();
439 wxSize size = array[i]->anchor->GetCachedSize();
440 if (point.x <= pt.x && point.x + size.x >= pt.x
441 && point.y <= pt.y && point.y + size.y >= pt.y)
442 {
443 textPosition = array[i]->anchor->GetRange().GetStart();
603f702b 444 * obj = array[i]->anchor;
cdaed652
VZ
445 if (pt.x > (pt.x + pt.x + size.x) / 2)
446 return wxRICHTEXT_HITTEST_BEFORE;
447 else
448 return wxRICHTEXT_HITTEST_AFTER;
449 }
ce00f59b 450
cdaed652
VZ
451 return wxRICHTEXT_HITTEST_NONE;
452}
453
8db2e3ef 454int wxRichTextFloatCollector::HitTest(wxDC& dc, wxRichTextDrawingContext& context, const wxPoint& pt, long& textPosition, wxRichTextObject** obj, int flags)
cdaed652 455{
8db2e3ef 456 int ret = HitTestFloat(m_left, dc, context, pt, textPosition, obj, flags);
cdaed652
VZ
457 if (ret == wxRICHTEXT_HITTEST_NONE)
458 {
8db2e3ef 459 ret = HitTestFloat(m_right, dc, context, pt, textPosition, obj, flags);
cdaed652
VZ
460 }
461 return ret;
462}
463
ce00f59b 464// Helpers for efficiency
ecb5fbf1
JS
465inline void wxCheckSetFont(wxDC& dc, const wxFont& font)
466{
ecb5fbf1
JS
467 dc.SetFont(font);
468}
469
470inline void wxCheckSetPen(wxDC& dc, const wxPen& pen)
471{
472 const wxPen& pen1 = dc.GetPen();
473 if (pen1.IsOk() && pen.IsOk())
474 {
475 if (pen1.GetWidth() == pen.GetWidth() &&
476 pen1.GetStyle() == pen.GetStyle() &&
477 pen1.GetColour() == pen.GetColour())
478 return;
479 }
480 dc.SetPen(pen);
481}
482
483inline void wxCheckSetBrush(wxDC& dc, const wxBrush& brush)
484{
485 const wxBrush& brush1 = dc.GetBrush();
486 if (brush1.IsOk() && brush.IsOk())
487 {
488 if (brush1.GetStyle() == brush.GetStyle() &&
489 brush1.GetColour() == brush.GetColour())
490 return;
491 }
492 dc.SetBrush(brush);
493}
494
5d7836c4
JS
495/*!
496 * wxRichTextObject
497 * This is the base for drawable objects.
498 */
499
500IMPLEMENT_CLASS(wxRichTextObject, wxObject)
501
502wxRichTextObject::wxRichTextObject(wxRichTextObject* parent)
503{
5d7836c4
JS
504 m_refCount = 1;
505 m_parent = parent;
5d7836c4 506 m_descent = 0;
603f702b 507 m_show = true;
5d7836c4
JS
508}
509
510wxRichTextObject::~wxRichTextObject()
511{
512}
513
514void wxRichTextObject::Dereference()
515{
516 m_refCount --;
517 if (m_refCount <= 0)
518 delete this;
519}
520
521/// Copy
522void wxRichTextObject::Copy(const wxRichTextObject& obj)
523{
524 m_size = obj.m_size;
603f702b
JS
525 m_maxSize = obj.m_maxSize;
526 m_minSize = obj.m_minSize;
5d7836c4 527 m_pos = obj.m_pos;
5d7836c4 528 m_range = obj.m_range;
603f702b 529 m_ownRange = obj.m_ownRange;
5d7836c4 530 m_attributes = obj.m_attributes;
bec80f4f 531 m_properties = obj.m_properties;
5d7836c4 532 m_descent = obj.m_descent;
603f702b
JS
533 m_show = obj.m_show;
534}
535
536// Get/set the top-level container of this object.
537wxRichTextParagraphLayoutBox* wxRichTextObject::GetContainer() const
538{
539 const wxRichTextObject* p = this;
540 while (p)
541 {
542 if (p->IsTopLevel())
543 {
544 return wxDynamicCast(p, wxRichTextParagraphLayoutBox);
545 }
546 p = p->GetParent();
547 }
548 return NULL;
5d7836c4
JS
549}
550
551void wxRichTextObject::SetMargins(int margin)
552{
603f702b 553 SetMargins(margin, margin, margin, margin);
5d7836c4
JS
554}
555
556void wxRichTextObject::SetMargins(int leftMargin, int rightMargin, int topMargin, int bottomMargin)
557{
603f702b
JS
558 GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(leftMargin, wxTEXT_ATTR_UNITS_PIXELS);
559 GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(rightMargin, wxTEXT_ATTR_UNITS_PIXELS);
560 GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(topMargin, wxTEXT_ATTR_UNITS_PIXELS);
561 GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(bottomMargin, wxTEXT_ATTR_UNITS_PIXELS);
562}
563
564int wxRichTextObject::GetLeftMargin() const
565{
566 return GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue();
567}
568
569int wxRichTextObject::GetRightMargin() const
570{
571 return GetAttributes().GetTextBoxAttr().GetMargins().GetRight().GetValue();
572}
573
574int wxRichTextObject::GetTopMargin() const
575{
576 return GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue();
577}
578
579int wxRichTextObject::GetBottomMargin() const
580{
581 return GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().GetValue();
582}
583
584// Calculate the available content space in the given rectangle, given the
585// margins, border and padding specified in the object's attributes.
8db2e3ef 586wxRect wxRichTextObject::GetAvailableContentArea(wxDC& dc, wxRichTextDrawingContext& context, const wxRect& outerRect) const
603f702b
JS
587{
588 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
589 marginRect = outerRect;
8db2e3ef
JS
590 wxRichTextAttr attr(GetAttributes());
591 context.ApplyVirtualAttributes(attr, (wxRichTextObject*) this);
592 GetBoxRects(dc, GetBuffer(), attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
593 return contentRect;
594}
595
596// Invalidate the buffer. With no argument, invalidates whole buffer.
597void wxRichTextObject::Invalidate(const wxRichTextRange& invalidRange)
598{
599 if (invalidRange != wxRICHTEXT_NONE)
600 {
23698b12
JS
601 // If this is a floating object, size may not be recalculated
602 // after floats have been collected in an early stage of Layout.
603 // So avoid resetting the cache for floating objects during layout.
e12b91a3 604 if (!IsFloating() || !wxRichTextBuffer::GetFloatingLayoutMode())
23698b12 605 SetCachedSize(wxDefaultSize);
603f702b
JS
606 SetMaxSize(wxDefaultSize);
607 SetMinSize(wxDefaultSize);
608 }
5d7836c4
JS
609}
610
44219ff0 611// Convert units in tenths of a millimetre to device units
cdaed652 612int wxRichTextObject::ConvertTenthsMMToPixels(wxDC& dc, int units) const
5d7836c4 613{
44219ff0 614 // Unscale
bec80f4f
JS
615 double scale = 1.0;
616 if (GetBuffer())
32423dd8 617 scale = GetBuffer()->GetScale() / GetBuffer()->GetDimensionScale();
bec80f4f
JS
618 int p = ConvertTenthsMMToPixels(dc.GetPPI().x, units, scale);
619
44219ff0
JS
620 return p;
621}
622
623// Convert units in tenths of a millimetre to device units
bec80f4f 624int wxRichTextObject::ConvertTenthsMMToPixels(int ppi, int units, double scale)
44219ff0 625{
5d7836c4
JS
626 // There are ppi pixels in 254.1 "1/10 mm"
627
628 double pixels = ((double) units * (double)ppi) / 254.1;
bec80f4f
JS
629 if (scale != 1.0)
630 pixels /= scale;
5d7836c4 631
603f702b
JS
632 // If the result is very small, make it at least one pixel in size.
633 if (pixels == 0 && units > 0)
634 pixels = 1;
635
5d7836c4
JS
636 return (int) pixels;
637}
638
24777478
JS
639// Convert units in pixels to tenths of a millimetre
640int wxRichTextObject::ConvertPixelsToTenthsMM(wxDC& dc, int pixels) const
641{
642 int p = pixels;
bec80f4f
JS
643 double scale = 1.0;
644 if (GetBuffer())
645 scale = GetBuffer()->GetScale();
646
647 return ConvertPixelsToTenthsMM(dc.GetPPI().x, p, scale);
24777478
JS
648}
649
bec80f4f 650int wxRichTextObject::ConvertPixelsToTenthsMM(int ppi, int pixels, double scale)
24777478
JS
651{
652 // There are ppi pixels in 254.1 "1/10 mm"
bec80f4f
JS
653
654 double p = double(pixels);
655
656 if (scale != 1.0)
657 p *= scale;
658
659 int units = int( p * 254.1 / (double) ppi );
24777478
JS
660 return units;
661}
662
bec80f4f 663// Draw the borders and background for the given rectangle and attributes.
603f702b
JS
664// Width and height are taken to be the outer margin size, not the content.
665bool wxRichTextObject::DrawBoxAttributes(wxDC& dc, wxRichTextBuffer* buffer, const wxRichTextAttr& attr, const wxRect& boxRect, int flags)
bec80f4f
JS
666{
667 // Assume boxRect is the area around the content
603f702b
JS
668 wxRect marginRect = boxRect;
669 wxRect contentRect, borderRect, paddingRect, outlineRect;
bec80f4f 670
603f702b 671 GetBoxRects(dc, buffer, attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
bec80f4f
JS
672
673 // Margin is transparent. Draw background from margin.
603f702b 674 if (attr.HasBackgroundColour() || (flags & wxRICHTEXT_DRAW_SELECTED))
bec80f4f 675 {
603f702b
JS
676 wxColour colour;
677 if (flags & wxRICHTEXT_DRAW_SELECTED)
678 {
679 // TODO: get selection colour from control?
680 colour = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
681 }
682 else
683 colour = attr.GetBackgroundColour();
684
685 wxPen pen(colour);
686 wxBrush brush(colour);
bec80f4f
JS
687
688 dc.SetPen(pen);
689 dc.SetBrush(brush);
37e7b783 690 dc.DrawRectangle(borderRect);
bec80f4f
JS
691 }
692
603f702b
JS
693 if (flags & wxRICHTEXT_DRAW_GUIDELINES)
694 {
695 wxRichTextAttr editBorderAttr = attr;
696 // TODO: make guideline colour configurable
697 editBorderAttr.GetTextBoxAttr().GetBorder().SetColour(*wxLIGHT_GREY);
698 editBorderAttr.GetTextBoxAttr().GetBorder().SetWidth(1, wxTEXT_ATTR_UNITS_PIXELS);
699 editBorderAttr.GetTextBoxAttr().GetBorder().SetStyle(wxTEXT_BOX_ATTR_BORDER_SOLID);
700
701 DrawBorder(dc, buffer, editBorderAttr.GetTextBoxAttr().GetBorder(), borderRect, flags);
702 }
703
704 if (attr.GetTextBoxAttr().GetBorder().IsValid())
705 DrawBorder(dc, buffer, attr.GetTextBoxAttr().GetBorder(), borderRect);
bec80f4f 706
603f702b
JS
707 if (attr.GetTextBoxAttr().GetOutline().IsValid())
708 DrawBorder(dc, buffer, attr.GetTextBoxAttr().GetOutline(), outlineRect);
bec80f4f
JS
709
710 return true;
711}
712
713// Draw a border
603f702b 714bool wxRichTextObject::DrawBorder(wxDC& dc, wxRichTextBuffer* buffer, const wxTextAttrBorders& attr, const wxRect& rect, int WXUNUSED(flags))
bec80f4f
JS
715{
716 int borderLeft = 0, borderRight = 0, borderTop = 0, borderBottom = 0;
603f702b 717 wxTextAttrDimensionConverter converter(dc, buffer ? buffer->GetScale() : 1.0);
bec80f4f 718
603f702b 719 if (attr.GetLeft().IsValid() && attr.GetLeft().GetStyle() != wxTEXT_BOX_ATTR_BORDER_NONE)
bec80f4f
JS
720 {
721 borderLeft = converter.GetPixels(attr.GetLeft().GetWidth());
722 wxColour col(attr.GetLeft().GetColour());
723
724 // If pen width is > 1, resorts to a solid rectangle.
725 if (borderLeft == 1)
726 {
727 int penStyle = wxSOLID;
728 if (attr.GetLeft().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED)
729 penStyle = wxDOT;
730 else if (attr.GetLeft().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED)
731 penStyle = wxLONG_DASH;
603f702b 732 wxPen pen(col, 1, penStyle);
bec80f4f
JS
733 dc.SetPen(pen);
734 dc.DrawLine(rect.x, rect.y, rect.x, rect.y + rect.height);
735
736 }
737 else if (borderLeft > 1)
738 {
739 wxPen pen(col);
740 wxBrush brush(col);
741 dc.SetPen(pen);
742 dc.SetBrush(brush);
743 dc.DrawRectangle(rect.x, rect.y, borderLeft, rect.height);
744 }
745 }
746
603f702b 747 if (attr.GetRight().IsValid() && attr.GetRight().GetStyle() != wxTEXT_BOX_ATTR_BORDER_NONE)
bec80f4f
JS
748 {
749 borderRight = converter.GetPixels(attr.GetRight().GetWidth());
750
751 wxColour col(attr.GetRight().GetColour());
752
753 // If pen width is > 1, resorts to a solid rectangle.
754 if (borderRight == 1)
755 {
756 int penStyle = wxSOLID;
757 if (attr.GetRight().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED)
758 penStyle = wxDOT;
759 else if (attr.GetRight().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED)
760 penStyle = wxLONG_DASH;
603f702b 761 wxPen pen(col, 1, penStyle);
bec80f4f 762 dc.SetPen(pen);
603f702b 763 dc.DrawLine(rect.x + rect.width, rect.y, rect.x + rect.width, rect.y + rect.height + 1);
bec80f4f
JS
764
765 }
766 else if (borderRight > 1)
767 {
768 wxPen pen(col);
769 wxBrush brush(col);
770 dc.SetPen(pen);
771 dc.SetBrush(brush);
63af79de 772 dc.DrawRectangle(rect.x + rect.width - borderRight, rect.y, borderRight, rect.height);
bec80f4f
JS
773 }
774 }
775
603f702b 776 if (attr.GetTop().IsValid() && attr.GetTop().GetStyle() != wxTEXT_BOX_ATTR_BORDER_NONE)
bec80f4f
JS
777 {
778 borderTop = converter.GetPixels(attr.GetTop().GetWidth());
779
780 wxColour col(attr.GetTop().GetColour());
781
782 // If pen width is > 1, resorts to a solid rectangle.
783 if (borderTop == 1)
784 {
785 int penStyle = wxSOLID;
786 if (attr.GetTop().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED)
787 penStyle = wxDOT;
788 else if (attr.GetTop().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED)
789 penStyle = wxLONG_DASH;
603f702b 790 wxPen pen(col, 1, penStyle);
bec80f4f
JS
791 dc.SetPen(pen);
792 dc.DrawLine(rect.x, rect.y, rect.x + rect.width, rect.y);
793
794 }
795 else if (borderTop > 1)
796 {
797 wxPen pen(col);
798 wxBrush brush(col);
799 dc.SetPen(pen);
800 dc.SetBrush(brush);
801 dc.DrawRectangle(rect.x, rect.y, rect.width, borderTop);
802 }
803 }
804
603f702b 805 if (attr.GetBottom().IsValid() && attr.GetBottom().GetStyle() != wxTEXT_BOX_ATTR_BORDER_NONE)
bec80f4f
JS
806 {
807 borderBottom = converter.GetPixels(attr.GetBottom().GetWidth());
914a4e23 808 wxColour col(attr.GetBottom().GetColour());
bec80f4f
JS
809
810 // If pen width is > 1, resorts to a solid rectangle.
811 if (borderBottom == 1)
812 {
813 int penStyle = wxSOLID;
814 if (attr.GetBottom().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DOTTED)
815 penStyle = wxDOT;
816 else if (attr.GetBottom().GetStyle() == wxTEXT_BOX_ATTR_BORDER_DASHED)
817 penStyle = wxLONG_DASH;
603f702b 818 wxPen pen(col, 1, penStyle);
bec80f4f
JS
819 dc.SetPen(pen);
820 dc.DrawLine(rect.x, rect.y + rect.height, rect.x + rect.width, rect.y + rect.height);
821
822 }
823 else if (borderBottom > 1)
824 {
825 wxPen pen(col);
826 wxBrush brush(col);
827 dc.SetPen(pen);
828 dc.SetBrush(brush);
63af79de 829 dc.DrawRectangle(rect.x, rect.y + rect.height - borderBottom, rect.width, borderBottom);
bec80f4f
JS
830 }
831 }
832
833 return true;
834}
835
836// Get the various rectangles of the box model in pixels. You can either specify contentRect (inner)
837// or marginRect (outer), and the other must be the default rectangle (no width or height).
838// Note that the outline doesn't affect the position of the rectangle, it's drawn in whatever space
839// is available.
840//
841// | Margin | Border | Padding | CONTENT | Padding | Border | Margin |
842
603f702b 843bool wxRichTextObject::GetBoxRects(wxDC& dc, wxRichTextBuffer* buffer, const wxRichTextAttr& attr, wxRect& marginRect, wxRect& borderRect, wxRect& contentRect, wxRect& paddingRect, wxRect& outlineRect)
bec80f4f
JS
844{
845 int borderLeft = 0, borderRight = 0, borderTop = 0, borderBottom = 0;
846 int outlineLeft = 0, outlineRight = 0, outlineTop = 0, outlineBottom = 0;
847 int paddingLeft = 0, paddingRight = 0, paddingTop = 0, paddingBottom = 0;
848 int marginLeft = 0, marginRight = 0, marginTop = 0, marginBottom = 0;
849
603f702b 850 wxTextAttrDimensionConverter converter(dc, buffer ? buffer->GetScale() : 1.0);
bec80f4f 851
603f702b 852 if (attr.GetTextBoxAttr().GetMargins().GetLeft().IsValid())
bec80f4f 853 marginLeft = converter.GetPixels(attr.GetTextBoxAttr().GetMargins().GetLeft());
603f702b 854 if (attr.GetTextBoxAttr().GetMargins().GetRight().IsValid())
bec80f4f 855 marginRight = converter.GetPixels(attr.GetTextBoxAttr().GetMargins().GetRight());
603f702b 856 if (attr.GetTextBoxAttr().GetMargins().GetTop().IsValid())
bec80f4f 857 marginTop = converter.GetPixels(attr.GetTextBoxAttr().GetMargins().GetTop());
83c6ae8e 858 if (attr.GetTextBoxAttr().GetMargins().GetBottom().IsValid())
bec80f4f
JS
859 marginBottom = converter.GetPixels(attr.GetTextBoxAttr().GetMargins().GetBottom());
860
603f702b 861 if (attr.GetTextBoxAttr().GetBorder().GetLeft().GetWidth().IsValid())
bec80f4f 862 borderLeft = converter.GetPixels(attr.GetTextBoxAttr().GetBorder().GetLeft().GetWidth());
603f702b 863 if (attr.GetTextBoxAttr().GetBorder().GetRight().GetWidth().IsValid())
bec80f4f 864 borderRight = converter.GetPixels(attr.GetTextBoxAttr().GetBorder().GetRight().GetWidth());
603f702b 865 if (attr.GetTextBoxAttr().GetBorder().GetTop().GetWidth().IsValid())
bec80f4f 866 borderTop = converter.GetPixels(attr.GetTextBoxAttr().GetBorder().GetTop().GetWidth());
83c6ae8e 867 if (attr.GetTextBoxAttr().GetBorder().GetBottom().GetWidth().IsValid())
bec80f4f
JS
868 borderBottom = converter.GetPixels(attr.GetTextBoxAttr().GetBorder().GetBottom().GetWidth());
869
603f702b 870 if (attr.GetTextBoxAttr().GetPadding().GetLeft().IsValid())
bec80f4f 871 paddingLeft = converter.GetPixels(attr.GetTextBoxAttr().GetPadding().GetLeft());
603f702b 872 if (attr.GetTextBoxAttr().GetPadding().GetRight().IsValid())
bec80f4f 873 paddingRight = converter.GetPixels(attr.GetTextBoxAttr().GetPadding().GetRight());
603f702b 874 if (attr.GetTextBoxAttr().GetPadding().GetTop().IsValid())
bec80f4f 875 paddingTop = converter.GetPixels(attr.GetTextBoxAttr().GetPadding().GetTop());
603f702b 876 if (attr.GetTextBoxAttr().GetPadding().GetBottom().IsValid())
bec80f4f
JS
877 paddingBottom = converter.GetPixels(attr.GetTextBoxAttr().GetPadding().GetBottom());
878
603f702b 879 if (attr.GetTextBoxAttr().GetOutline().GetLeft().GetWidth().IsValid())
bec80f4f 880 outlineLeft = converter.GetPixels(attr.GetTextBoxAttr().GetOutline().GetLeft().GetWidth());
603f702b 881 if (attr.GetTextBoxAttr().GetOutline().GetRight().GetWidth().IsValid())
bec80f4f 882 outlineRight = converter.GetPixels(attr.GetTextBoxAttr().GetOutline().GetRight().GetWidth());
603f702b 883 if (attr.GetTextBoxAttr().GetOutline().GetTop().GetWidth().IsValid())
bec80f4f 884 outlineTop = converter.GetPixels(attr.GetTextBoxAttr().GetOutline().GetTop().GetWidth());
603f702b 885 if (attr.GetTextBoxAttr().GetOutline().GetBottom().GetWidth().IsValid())
bec80f4f
JS
886 outlineBottom = converter.GetPixels(attr.GetTextBoxAttr().GetOutline().GetBottom().GetWidth());
887
888 int leftTotal = marginLeft + borderLeft + paddingLeft;
889 int rightTotal = marginRight + borderRight + paddingRight;
890 int topTotal = marginTop + borderTop + paddingTop;
891 int bottomTotal = marginBottom + borderBottom + paddingBottom;
892
893 if (marginRect != wxRect())
894 {
895 contentRect.x = marginRect.x + leftTotal;
896 contentRect.y = marginRect.y + topTotal;
897 contentRect.width = marginRect.width - (leftTotal + rightTotal);
898 contentRect.height = marginRect.height - (topTotal + bottomTotal);
899 }
900 else
901 {
902 marginRect.x = contentRect.x - leftTotal;
903 marginRect.y = contentRect.y - topTotal;
904 marginRect.width = contentRect.width + (leftTotal + rightTotal);
905 marginRect.height = contentRect.height + (topTotal + bottomTotal);
906 }
907
908 borderRect.x = marginRect.x + marginLeft;
909 borderRect.y = marginRect.y + marginTop;
910 borderRect.width = marginRect.width - (marginLeft + marginRight);
911 borderRect.height = marginRect.height - (marginTop + marginBottom);
912
913 paddingRect.x = marginRect.x + marginLeft + borderLeft;
914 paddingRect.y = marginRect.y + marginTop + borderTop;
915 paddingRect.width = marginRect.width - (marginLeft + marginRight + borderLeft + borderRight);
916 paddingRect.height = marginRect.height - (marginTop + marginBottom + borderTop + borderBottom);
917
918 // The outline is outside the margin and doesn't influence the overall box position or content size.
919 outlineRect.x = marginRect.x - outlineLeft;
920 outlineRect.y = marginRect.y - outlineTop;
921 outlineRect.width = marginRect.width + (outlineLeft + outlineRight);
922 outlineRect.height = marginRect.height + (outlineTop + outlineBottom);
923
924 return true;
925}
926
603f702b
JS
927// Get the total margin for the object in pixels, taking into account margin, padding and border size
928bool wxRichTextObject::GetTotalMargin(wxDC& dc, wxRichTextBuffer* buffer, const wxRichTextAttr& attr, int& leftMargin, int& rightMargin,
929 int& topMargin, int& bottomMargin)
930{
931 // Assume boxRect is the area around the content
932 wxRect contentRect, marginRect, borderRect, paddingRect, outlineRect;
933 marginRect = wxRect(0, 0, 1000, 1000);
bec80f4f 934
603f702b
JS
935 GetBoxRects(dc, buffer, attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
936
937 leftMargin = contentRect.GetLeft() - marginRect.GetLeft();
938 rightMargin = marginRect.GetRight() - contentRect.GetRight();
939 topMargin = contentRect.GetTop() - marginRect.GetTop();
940 bottomMargin = marginRect.GetBottom() - contentRect.GetBottom();
941
942 return true;
943}
944
945// Returns the rectangle which the child has available to it given restrictions specified in the
946// child attribute, e.g. 50% width of the parent, 400 pixels, x position 20% of the parent, etc.
bb7bbd12
JS
947// availableContainerSpace might be a parent that the cell has to compute its width relative to.
948// E.g. a cell that's 50% of its parent.
949wxRect wxRichTextObject::AdjustAvailableSpace(wxDC& dc, wxRichTextBuffer* buffer, const wxRichTextAttr& WXUNUSED(parentAttr), const wxRichTextAttr& childAttr, const wxRect& availableParentSpace, const wxRect& availableContainerSpace)
603f702b
JS
950{
951 wxRect rect = availableParentSpace;
952 double scale = 1.0;
953 if (buffer)
954 scale = buffer->GetScale();
955
bb7bbd12 956 wxTextAttrDimensionConverter converter(dc, scale, availableContainerSpace.GetSize());
603f702b
JS
957
958 if (childAttr.GetTextBoxAttr().GetWidth().IsValid())
959 rect.width = converter.GetPixels(childAttr.GetTextBoxAttr().GetWidth());
960
961 if (childAttr.GetTextBoxAttr().GetHeight().IsValid())
962 rect.height = converter.GetPixels(childAttr.GetTextBoxAttr().GetHeight());
963
964 // Can specify either left or right for the position (we're assuming we can't
965 // set the left and right edges to effectively set the size. Would we want to do that?)
966 if (childAttr.GetTextBoxAttr().GetPosition().GetLeft().IsValid())
967 {
968 rect.x = rect.x + converter.GetPixels(childAttr.GetTextBoxAttr().GetPosition().GetLeft());
969 }
970 else if (childAttr.GetTextBoxAttr().GetPosition().GetRight().IsValid())
971 {
972 int x = converter.GetPixels(childAttr.GetTextBoxAttr().GetPosition().GetRight());
973 if (childAttr.GetTextBoxAttr().GetPosition().GetRight().GetPosition() == wxTEXT_BOX_ATTR_POSITION_RELATIVE)
bb7bbd12 974 rect.x = availableContainerSpace.x + availableContainerSpace.width - rect.width;
603f702b
JS
975 else
976 rect.x += x;
977 }
978
979 if (childAttr.GetTextBoxAttr().GetPosition().GetTop().IsValid())
980 {
981 rect.y = rect.y + converter.GetPixels(childAttr.GetTextBoxAttr().GetPosition().GetTop());
982 }
983 else if (childAttr.GetTextBoxAttr().GetPosition().GetBottom().IsValid())
984 {
985 int y = converter.GetPixels(childAttr.GetTextBoxAttr().GetPosition().GetBottom());
986 if (childAttr.GetTextBoxAttr().GetPosition().GetBottom().GetPosition() == wxTEXT_BOX_ATTR_POSITION_RELATIVE)
bb7bbd12 987 rect.y = availableContainerSpace.y + availableContainerSpace.height - rect.height;
603f702b
JS
988 else
989 rect.y += y;
990 }
991
bb7bbd12
JS
992 if (rect.GetWidth() > availableParentSpace.GetWidth())
993 rect.SetWidth(availableParentSpace.GetWidth());
994
603f702b
JS
995 return rect;
996}
997
998// Dump to output stream for debugging
5d7836c4
JS
999void wxRichTextObject::Dump(wxTextOutputStream& stream)
1000{
1001 stream << GetClassInfo()->GetClassName() << wxT("\n");
1002 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");
1003 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");
1004}
1005
603f702b 1006// Gets the containing buffer
44219ff0
JS
1007wxRichTextBuffer* wxRichTextObject::GetBuffer() const
1008{
1009 const wxRichTextObject* obj = this;
345c78ca 1010 while (obj && !wxDynamicCast(obj, wxRichTextBuffer))
44219ff0
JS
1011 obj = obj->GetParent();
1012 return wxDynamicCast(obj, wxRichTextBuffer);
1013}
5d7836c4 1014
603f702b
JS
1015// Get the absolute object position, by traversing up the child/parent hierarchy
1016wxPoint wxRichTextObject::GetAbsolutePosition() const
1017{
1018 wxPoint pt = GetPosition();
1019
1020 wxRichTextObject* p = GetParent();
1021 while (p)
1022 {
1023 pt = pt + p->GetPosition();
1024 p = p->GetParent();
1025 }
1026
1027 return pt;
1028}
1029
1030// Hit-testing: returns a flag indicating hit test details, plus
1031// information about position
8db2e3ef 1032int wxRichTextObject::HitTest(wxDC& WXUNUSED(dc), wxRichTextDrawingContext& WXUNUSED(context), const wxPoint& pt, long& textPosition, wxRichTextObject** obj, wxRichTextObject** contextObj, int WXUNUSED(flags))
603f702b
JS
1033{
1034 if (!IsShown())
1035 return wxRICHTEXT_HITTEST_NONE;
1036
1037 wxRect rect = GetRect();
1038 if (pt.x >= rect.x && pt.x < rect.x + rect.width &&
1039 pt.y >= rect.y && pt.y < rect.y + rect.height)
1040 {
1041 *obj = this;
1042 *contextObj = GetParentContainer();
1043 textPosition = GetRange().GetStart();
1044 return wxRICHTEXT_HITTEST_ON;
1045 }
1046 else
1047 return wxRICHTEXT_HITTEST_NONE;
1048}
1049
1050// Lays out the object first with a given amount of space, and then if no width was specified in attr,
1051// lays out the object again using the maximum ('best') size
8db2e3ef 1052bool wxRichTextObject::LayoutToBestSize(wxDC& dc, wxRichTextDrawingContext& context, wxRichTextBuffer* buffer,
bb7bbd12
JS
1053 const wxRichTextAttr& parentAttr, const wxRichTextAttr& attr,
1054 const wxRect& availableParentSpace, const wxRect& availableContainerSpace,
603f702b
JS
1055 int style)
1056{
bb7bbd12 1057 wxRect availableChildRect = AdjustAvailableSpace(dc, buffer, parentAttr, attr, availableParentSpace, availableContainerSpace);
603f702b 1058 wxRect originalAvailableRect = availableChildRect;
8db2e3ef 1059 Layout(dc, context, availableChildRect, availableContainerSpace, style);
603f702b
JS
1060
1061 wxSize maxSize = GetMaxSize();
1062
1063 // Don't ignore if maxSize.x is zero, since we need to redo the paragraph's lines
1064 // on this basis
bb7bbd12 1065 if (!attr.GetTextBoxAttr().GetWidth().IsValid() && maxSize.x < availableChildRect.width)
603f702b
JS
1066 {
1067 // Redo the layout with a fixed, minimum size this time.
1068 Invalidate(wxRICHTEXT_ALL);
1069 wxRichTextAttr newAttr(attr);
1070 newAttr.GetTextBoxAttr().GetWidth().SetValue(maxSize.x, wxTEXT_ATTR_UNITS_PIXELS);
1071 newAttr.GetTextBoxAttr().GetWidth().SetPosition(wxTEXT_BOX_ATTR_POSITION_ABSOLUTE);
1072
bb7bbd12 1073 availableChildRect = AdjustAvailableSpace(dc, buffer, parentAttr, newAttr, availableParentSpace, availableContainerSpace);
603f702b
JS
1074
1075 // If a paragraph, align the whole paragraph.
1076 // Problem with this: if we're limited by a floating object, a line may be centered
1077 // w.r.t. the smaller resulting box rather than the actual available width.
e12b91a3
JS
1078 // FIXME: aligning whole paragraph not compatible with floating objects
1079 if (attr.HasAlignment() && (!wxRichTextBuffer::GetFloatingLayoutMode() || (GetContainer()->GetFloatCollector() && !GetContainer()->GetFloatCollector()->HasFloats())))
603f702b
JS
1080 {
1081 // centering, right-justification
8db2e3ef 1082 if (attr.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
603f702b
JS
1083 {
1084 availableChildRect.x = (originalAvailableRect.GetWidth() - availableChildRect.GetWidth())/2 + availableChildRect.x;
1085 }
8db2e3ef 1086 else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
603f702b
JS
1087 {
1088 availableChildRect.x = availableChildRect.x + originalAvailableRect.GetWidth() - availableChildRect.GetWidth();
1089 }
1090 }
1091
8db2e3ef 1092 Layout(dc, context, availableChildRect, availableContainerSpace, style);
603f702b
JS
1093 }
1094
1095 /*
1096 __________________
1097 | ____________ |
1098 | | | |
1099
1100
1101 */
1102
1103 return true;
1104}
1105
1106// Move the object recursively, by adding the offset from old to new
1107void wxRichTextObject::Move(const wxPoint& pt)
1108{
1109 SetPosition(pt);
1110}
1111
1112
5d7836c4
JS
1113/*!
1114 * wxRichTextCompositeObject
1115 * This is the base for drawable objects.
1116 */
1117
1118IMPLEMENT_CLASS(wxRichTextCompositeObject, wxRichTextObject)
1119
1120wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject* parent):
1121 wxRichTextObject(parent)
1122{
1123}
1124
1125wxRichTextCompositeObject::~wxRichTextCompositeObject()
1126{
1127 DeleteChildren();
1128}
1129
1130/// Get the nth child
1131wxRichTextObject* wxRichTextCompositeObject::GetChild(size_t n) const
1132{
1133 wxASSERT ( n < m_children.GetCount() );
1134
1135 return m_children.Item(n)->GetData();
1136}
1137
1138/// Append a child, returning the position
1139size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject* child)
1140{
1141 m_children.Append(child);
1142 child->SetParent(this);
1143 return m_children.GetCount() - 1;
1144}
1145
1146/// Insert the child in front of the given object, or at the beginning
1147bool wxRichTextCompositeObject::InsertChild(wxRichTextObject* child, wxRichTextObject* inFrontOf)
1148{
1149 if (inFrontOf)
1150 {
1151 wxRichTextObjectList::compatibility_iterator node = m_children.Find(inFrontOf);
1152 m_children.Insert(node, child);
1153 }
1154 else
1155 m_children.Insert(child);
1156 child->SetParent(this);
1157
1158 return true;
1159}
1160
1161/// Delete the child
1162bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject* child, bool deleteChild)
1163{
1164 wxRichTextObjectList::compatibility_iterator node = m_children.Find(child);
1165 if (node)
1166 {
efbf6735
JS
1167 wxRichTextObject* obj = node->GetData();
1168 m_children.Erase(node);
5d7836c4 1169 if (deleteChild)
efbf6735 1170 delete obj;
5d7836c4
JS
1171
1172 return true;
1173 }
1174 return false;
1175}
1176
1177/// Delete all children
1178bool wxRichTextCompositeObject::DeleteChildren()
1179{
1180 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1181 while (node)
1182 {
1183 wxRichTextObjectList::compatibility_iterator oldNode = node;
1184
1185 wxRichTextObject* child = node->GetData();
1186 child->Dereference(); // Only delete if reference count is zero
1187
1188 node = node->GetNext();
efbf6735 1189 m_children.Erase(oldNode);
5d7836c4
JS
1190 }
1191
1192 return true;
1193}
1194
1195/// Get the child count
1196size_t wxRichTextCompositeObject::GetChildCount() const
1197{
1198 return m_children.GetCount();
1199}
1200
1201/// Copy
1202void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject& obj)
1203{
1204 wxRichTextObject::Copy(obj);
1205
1206 DeleteChildren();
1207
1208 wxRichTextObjectList::compatibility_iterator node = obj.m_children.GetFirst();
1209 while (node)
1210 {
1211 wxRichTextObject* child = node->GetData();
fe5aa22c
JS
1212 wxRichTextObject* newChild = child->Clone();
1213 newChild->SetParent(this);
1214 m_children.Append(newChild);
5d7836c4
JS
1215
1216 node = node->GetNext();
1217 }
1218}
1219
1220/// Hit-testing: returns a flag indicating hit test details, plus
1221/// information about position
8db2e3ef 1222int wxRichTextCompositeObject::HitTest(wxDC& dc, wxRichTextDrawingContext& context, const wxPoint& pt, long& textPosition, wxRichTextObject** obj, wxRichTextObject** contextObj, int flags)
5d7836c4 1223{
603f702b
JS
1224 if (!IsShown())
1225 return wxRICHTEXT_HITTEST_NONE;
1226
5d7836c4
JS
1227 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1228 while (node)
1229 {
1230 wxRichTextObject* child = node->GetData();
1231
603f702b
JS
1232 if (child->IsShown() && child->IsTopLevel() && (flags & wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS))
1233 {
1234 // Just check if we hit the overall object
8db2e3ef 1235 int ret = child->wxRichTextObject::HitTest(dc, context, pt, textPosition, obj, contextObj, flags);
603f702b
JS
1236 if (ret != wxRICHTEXT_HITTEST_NONE)
1237 return ret;
1238 }
1239 else if (child->IsShown())
1240 {
8db2e3ef 1241 int ret = child->HitTest(dc, context, pt, textPosition, obj, contextObj, flags);
603f702b
JS
1242 if (ret != wxRICHTEXT_HITTEST_NONE)
1243 return ret;
1244 }
5d7836c4
JS
1245
1246 node = node->GetNext();
1247 }
1248
603f702b 1249 return wxRICHTEXT_HITTEST_NONE;
5d7836c4
JS
1250}
1251
1252/// Finds the absolute position and row height for the given character position
8db2e3ef 1253bool wxRichTextCompositeObject::FindPosition(wxDC& dc, wxRichTextDrawingContext& context, long index, wxPoint& pt, int* height, bool forceLineStart)
5d7836c4
JS
1254{
1255 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1256 while (node)
1257 {
1258 wxRichTextObject* child = node->GetData();
1259
603f702b
JS
1260 // Don't recurse if the child is a top-level object,
1261 // such as a text box, because the character position will no longer
1262 // apply. By definition, a top-level object has its own range of
1263 // character positions.
8db2e3ef 1264 if (!child->IsTopLevel() && child->FindPosition(dc, context, index, pt, height, forceLineStart))
5d7836c4
JS
1265 return true;
1266
1267 node = node->GetNext();
1268 }
1269
1270 return false;
1271}
1272
1273/// Calculate range
1274void wxRichTextCompositeObject::CalculateRange(long start, long& end)
1275{
1276 long current = start;
1277 long lastEnd = current;
1278
603f702b
JS
1279 if (IsTopLevel())
1280 {
1281 current = 0;
1282 lastEnd = 0;
1283 }
1284
5d7836c4
JS
1285 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1286 while (node)
1287 {
1288 wxRichTextObject* child = node->GetData();
1289 long childEnd = 0;
1290
1291 child->CalculateRange(current, childEnd);
1292 lastEnd = childEnd;
1293
1294 current = childEnd + 1;
1295
1296 node = node->GetNext();
1297 }
1298
603f702b
JS
1299 if (IsTopLevel())
1300 {
1301 // A top-level object always has a range of size 1,
1302 // because its children don't count at this level.
1303 end = start;
1304 m_range.SetRange(start, start);
5d7836c4 1305
603f702b
JS
1306 // An object with no children has zero length
1307 if (m_children.GetCount() == 0)
1308 lastEnd --;
1309 m_ownRange.SetRange(0, lastEnd);
1310 }
1311 else
1312 {
1313 end = lastEnd;
5d7836c4 1314
603f702b
JS
1315 // An object with no children has zero length
1316 if (m_children.GetCount() == 0)
1317 end --;
1318
1319 m_range.SetRange(start, end);
1320 }
5d7836c4
JS
1321}
1322
1323/// Delete range from layout.
1324bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange& range)
1325{
1326 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
7fe8059f 1327
5d7836c4
JS
1328 while (node)
1329 {
1330 wxRichTextObject* obj = (wxRichTextObject*) node->GetData();
1331 wxRichTextObjectList::compatibility_iterator next = node->GetNext();
7fe8059f 1332
5d7836c4
JS
1333 // Delete the range in each paragraph
1334
1335 // When a chunk has been deleted, internally the content does not
1336 // now match the ranges.
1337 // However, so long as deletion is not done on the same object twice this is OK.
1338 // If you may delete content from the same object twice, recalculate
1339 // the ranges inbetween DeleteRange calls by calling CalculateRanges, and
1340 // adjust the range you're deleting accordingly.
7fe8059f 1341
5d7836c4
JS
1342 if (!obj->GetRange().IsOutside(range))
1343 {
603f702b
JS
1344 // No need to delete within a top-level object; just removing this object will do fine
1345 if (!obj->IsTopLevel())
1346 obj->DeleteRange(range);
5d7836c4
JS
1347
1348 // Delete an empty object, or paragraph within this range.
1349 if (obj->IsEmpty() ||
1350 (range.GetStart() <= obj->GetRange().GetStart() && range.GetEnd() >= obj->GetRange().GetEnd()))
1351 {
1352 // An empty paragraph has length 1, so won't be deleted unless the
1353 // whole range is deleted.
7fe8059f 1354 RemoveChild(obj, true);
5d7836c4
JS
1355 }
1356 }
7fe8059f 1357
5d7836c4
JS
1358 node = next;
1359 }
7fe8059f 1360
5d7836c4
JS
1361 return true;
1362}
1363
1364/// Get any text in this object for the given range
1365wxString wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange& range) const
1366{
1367 wxString text;
1368 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1369 while (node)
1370 {
1371 wxRichTextObject* child = node->GetData();
1372 wxRichTextRange childRange = range;
1373 if (!child->GetRange().IsOutside(range))
1374 {
1375 childRange.LimitTo(child->GetRange());
7fe8059f 1376
5d7836c4 1377 wxString childText = child->GetTextForRange(childRange);
7fe8059f 1378
5d7836c4
JS
1379 text += childText;
1380 }
1381 node = node->GetNext();
1382 }
1383
1384 return text;
1385}
1386
603f702b
JS
1387/// Get the child object at the given character position
1388wxRichTextObject* wxRichTextCompositeObject::GetChildAtPosition(long pos) const
1389{
1390 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1391 while (node)
1392 {
1393 wxRichTextObject* child = node->GetData();
1394 if (child->GetRange().GetStart() == pos)
1395 return child;
1396 node = node->GetNext();
1397 }
1398 return NULL;
1399}
1400
5d7836c4 1401/// Recursively merge all pieces that can be merged.
f7667b84 1402bool wxRichTextCompositeObject::Defragment(wxRichTextDrawingContext& context, const wxRichTextRange& range)
5d7836c4
JS
1403{
1404 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1405 while (node)
1406 {
1407 wxRichTextObject* child = node->GetData();
5cb0b827 1408 if (range == wxRICHTEXT_ALL || !child->GetRange().IsOutside(range))
5d7836c4 1409 {
109bfc88
JS
1410 wxRichTextCompositeObject* composite = wxDynamicCast(child, wxRichTextCompositeObject);
1411 if (composite)
f7667b84 1412 composite->Defragment(context);
109bfc88 1413
f7667b84
JS
1414 // Optimization: if there are no virtual attributes, we won't need to
1415 // to split objects in order to paint individually attributed chunks.
1416 // So only merge in this case.
1417 if (!context.GetVirtualAttributesEnabled())
5d7836c4 1418 {
f7667b84 1419 if (node->GetNext())
109bfc88 1420 {
f7667b84
JS
1421 wxRichTextObject* nextChild = node->GetNext()->GetData();
1422 if (child->CanMerge(nextChild, context) && child->Merge(nextChild, context))
1423 {
1424 nextChild->Dereference();
1425 m_children.Erase(node->GetNext());
1426 }
1427 else
1428 node = node->GetNext();
109bfc88
JS
1429 }
1430 else
1431 node = node->GetNext();
5d7836c4
JS
1432 }
1433 else
f7667b84
JS
1434 {
1435 // If we might have virtual attributes, we first see if we have to split
1436 // objects so that they may be painted with potential virtual attributes,
1437 // since text objects can only draw or measure with a single attributes object
1438 // at a time.
1439 wxRichTextObject* childAfterSplit = child;
1440 if (child->CanSplit(context))
1441 {
1442 childAfterSplit = child->Split(context);
1443 node = m_children.Find(childAfterSplit);
1444 }
1445
1446 if (node->GetNext())
1447 {
1448 wxRichTextObject* nextChild = node->GetNext()->GetData();
f7667b84
JS
1449
1450 // First split child and nextChild so we have smaller fragments to merge.
1451 // Then Merge only has to test per-object virtual attributes
1452 // because for an object with all the same sub-object attributes,
1453 // then any general virtual attributes should be merged with sub-objects by
1454 // the implementation.
1455
1456 wxRichTextObject* nextChildAfterSplit = nextChild;
1457
1458 if (nextChildAfterSplit->CanSplit(context))
1459 nextChildAfterSplit = nextChild->Split(context);
1460
1461 bool splitNextChild = nextChild != nextChildAfterSplit;
1462
1463 // See if we can merge this new fragment with (perhaps the first part of) the next object.
1464 // Note that we use nextChild because if we had split nextChild, the first object always
1465 // remains (and further parts are appended). However we must use childAfterSplit since
1466 // it's the last part of a possibly split child.
1467
1468 if (childAfterSplit->CanMerge(nextChild, context) && childAfterSplit->Merge(nextChild, context))
1469 {
1470 nextChild->Dereference();
1471 m_children.Erase(node->GetNext());
1472
1473 // Don't set node -- we'll see if we can merge again with the next
1474 // child. UNLESS we split this or the next child, in which case we know we have to
1475 // move on to the end of the next child.
1476 if (splitNextChild)
1477 node = m_children.Find(nextChildAfterSplit);
1478 }
1479 else
1480 {
1481 if (splitNextChild)
1482 node = m_children.Find(nextChildAfterSplit); // start from the last object in the split
1483 else
1484 node = node->GetNext();
1485 }
1486 }
1487 else
1488 node = node->GetNext();
1489 }
5d7836c4
JS
1490 }
1491 else
1492 node = node->GetNext();
1493 }
1494
bec80f4f
JS
1495 // Delete any remaining empty objects, but leave at least one empty object per composite object.
1496 if (GetChildCount() > 1)
5d7836c4 1497 {
bec80f4f
JS
1498 node = m_children.GetFirst();
1499 while (node)
1500 {
1501 wxRichTextObjectList::compatibility_iterator next = node->GetNext();
1502 wxRichTextObject* child = node->GetData();
1503 if (range == wxRICHTEXT_ALL || !child->GetRange().IsOutside(range))
1504 {
1505 if (child->IsEmpty())
1506 {
1507 child->Dereference();
1508 m_children.Erase(node);
1509 }
1510 node = next;
1511 }
1512 else
1513 node = node->GetNext();
1514 }
5d7836c4 1515 }
5d7836c4 1516
5d7836c4
JS
1517 return true;
1518}
1519
bec80f4f
JS
1520/// Dump to output stream for debugging
1521void wxRichTextCompositeObject::Dump(wxTextOutputStream& stream)
5d7836c4
JS
1522{
1523 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1524 while (node)
1525 {
1526 wxRichTextObject* child = node->GetData();
bec80f4f 1527 child->Dump(stream);
5d7836c4
JS
1528 node = node->GetNext();
1529 }
5d7836c4
JS
1530}
1531
603f702b
JS
1532/// Get/set the object size for the given range. Returns false if the range
1533/// is invalid for this object.
914a4e23 1534bool wxRichTextCompositeObject::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, wxRichTextDrawingContext& context, int flags, const wxPoint& position, const wxSize& parentSize, wxArrayInt* partialExtents) const
603f702b
JS
1535{
1536 if (!range.IsWithin(GetRange()))
1537 return false;
5d7836c4 1538
603f702b 1539 wxSize sz;
5d7836c4 1540
603f702b
JS
1541 wxArrayInt childExtents;
1542 wxArrayInt* p;
1543 if (partialExtents)
1544 p = & childExtents;
1545 else
1546 p = NULL;
5d7836c4 1547
603f702b
JS
1548 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1549 while (node)
cdaed652 1550 {
603f702b
JS
1551 wxRichTextObject* child = node->GetData();
1552 if (!child->GetRange().IsOutside(range))
1553 {
1554 // Floating objects have a zero size within the paragraph.
e12b91a3 1555 if (child->IsFloating() && wxRichTextBuffer::GetFloatingLayoutMode())
603f702b
JS
1556 {
1557 if (partialExtents)
1558 {
1559 int lastSize;
1560 if (partialExtents->GetCount() > 0)
1561 lastSize = (*partialExtents)[partialExtents->GetCount()-1];
1562 else
1563 lastSize = 0;
cdaed652 1564
603f702b
JS
1565 partialExtents->Add(0 /* zero size */ + lastSize);
1566 }
1567 }
1568 else
1569 {
1570 wxSize childSize;
5d7836c4 1571
603f702b
JS
1572 wxRichTextRange rangeToUse = range;
1573 rangeToUse.LimitTo(child->GetRange());
1574 if (child->IsTopLevel())
1575 rangeToUse = child->GetOwnRange();
5d7836c4 1576
603f702b 1577 int childDescent = 0;
cdaed652 1578
603f702b
JS
1579 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we're already cached the size,
1580 // but it's only going to be used after caching has taken place.
1581 if ((flags & wxRICHTEXT_HEIGHT_ONLY) && child->GetCachedSize().y != 0)
1582 {
1583 childDescent = child->GetDescent();
1584 childSize = child->GetCachedSize();
bec80f4f 1585
603f702b
JS
1586 sz.y = wxMax(sz.y, childSize.y);
1587 sz.x += childSize.x;
1588 descent = wxMax(descent, childDescent);
1589 }
914a4e23 1590 else if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, context, flags, wxPoint(position.x + sz.x, position.y), parentSize, p))
603f702b
JS
1591 {
1592 sz.y = wxMax(sz.y, childSize.y);
1593 sz.x += childSize.x;
1594 descent = wxMax(descent, childDescent);
bec80f4f 1595
603f702b
JS
1596 if ((flags & wxRICHTEXT_CACHE_SIZE) && (rangeToUse == child->GetRange() || child->IsTopLevel()))
1597 {
1598 child->SetCachedSize(childSize);
1599 child->SetDescent(childDescent);
1600 }
bec80f4f 1601
603f702b
JS
1602 if (partialExtents)
1603 {
1604 int lastSize;
1605 if (partialExtents->GetCount() > 0)
1606 lastSize = (*partialExtents)[partialExtents->GetCount()-1];
1607 else
1608 lastSize = 0;
bec80f4f 1609
603f702b
JS
1610 size_t i;
1611 for (i = 0; i < childExtents.GetCount(); i++)
1612 {
1613 partialExtents->Add(childExtents[i] + lastSize);
1614 }
1615 }
1616 }
1617 }
1618
1619 if (p)
1620 p->Clear();
1621 }
1622
1623 node = node->GetNext();
1624 }
1625 size = sz;
1626 return true;
1627}
1628
1629// Invalidate the buffer. With no argument, invalidates whole buffer.
1630void wxRichTextCompositeObject::Invalidate(const wxRichTextRange& invalidRange)
1631{
1632 wxRichTextObject::Invalidate(invalidRange);
1633
1634 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1635 while (node)
1636 {
1637 wxRichTextObject* child = node->GetData();
1638 if (invalidRange != wxRICHTEXT_ALL && invalidRange != wxRICHTEXT_NONE && child->GetRange().IsOutside(invalidRange))
1639 {
1640 // Skip
1641 }
1642 else if (child->IsTopLevel())
1643 {
e12b91a3 1644 if (wxRichTextBuffer::GetFloatingLayoutMode() && child->IsFloating() && GetBuffer()->GetFloatCollector() && GetBuffer()->GetFloatCollector()->HasFloat(child))
c4168888
JS
1645 {
1646 // Don't invalidate subhierarchy if we've already been laid out
1647 }
603f702b 1648 else
c4168888
JS
1649 {
1650 if (invalidRange == wxRICHTEXT_NONE)
1651 child->Invalidate(wxRICHTEXT_NONE);
1652 else
1653 child->Invalidate(wxRICHTEXT_ALL); // All children must be invalidated if within parent range
1654 }
603f702b
JS
1655 }
1656 else
1657 child->Invalidate(invalidRange);
1658 node = node->GetNext();
1659 }
1660}
1661
1662// Move the object recursively, by adding the offset from old to new
1663void wxRichTextCompositeObject::Move(const wxPoint& pt)
1664{
1665 wxPoint oldPos = GetPosition();
1666 SetPosition(pt);
1667 wxPoint offset = pt - oldPos;
1668
1669 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1670 while (node)
1671 {
1672 wxRichTextObject* child = node->GetData();
1673 wxPoint childPos = child->GetPosition() + offset;
1674 child->Move(childPos);
1675 node = node->GetNext();
1676 }
1677}
1678
1679
1680/*!
1681 * wxRichTextParagraphLayoutBox
1682 * This box knows how to lay out paragraphs.
1683 */
1684
1685IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox, wxRichTextCompositeObject)
1686
1687wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject* parent):
1688 wxRichTextCompositeObject(parent)
1689{
1690 Init();
1691}
1692
1693wxRichTextParagraphLayoutBox::~wxRichTextParagraphLayoutBox()
1694{
1695 if (m_floatCollector)
1696 {
1697 delete m_floatCollector;
1698 m_floatCollector = NULL;
1699 }
1700}
1701
1702/// Initialize the object.
1703void wxRichTextParagraphLayoutBox::Init()
1704{
1705 m_ctrl = NULL;
1706
1707 // For now, assume is the only box and has no initial size.
1708 m_range = wxRichTextRange(0, -1);
1709 m_ownRange = wxRichTextRange(0, -1);
1710
1711 m_invalidRange = wxRICHTEXT_ALL;
1712
603f702b
JS
1713 m_partialParagraph = false;
1714 m_floatCollector = NULL;
1715}
1716
1717void wxRichTextParagraphLayoutBox::Clear()
1718{
1719 DeleteChildren();
1720
1721 if (m_floatCollector)
1722 delete m_floatCollector;
1723 m_floatCollector = NULL;
1724 m_partialParagraph = false;
1725}
1726
1727/// Copy
1728void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox& obj)
1729{
1730 Clear();
1731
1732 wxRichTextCompositeObject::Copy(obj);
1733
1734 m_partialParagraph = obj.m_partialParagraph;
1735 m_defaultAttributes = obj.m_defaultAttributes;
bec80f4f
JS
1736}
1737
07d4142f
JS
1738// Gather information about floating objects; only gather floats for those paragraphs that
1739// will not be formatted again due to optimization, after which floats will be gathered per-paragraph
1740// during layout.
603f702b 1741bool wxRichTextParagraphLayoutBox::UpdateFloatingObjects(const wxRect& availableRect, wxRichTextObject* untilObj)
cdaed652
VZ
1742{
1743 if (m_floatCollector != NULL)
1744 delete m_floatCollector;
603f702b 1745 m_floatCollector = new wxRichTextFloatCollector(availableRect);
cdaed652 1746 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
07d4142f
JS
1747 // Only gather floats up to the point we'll start formatting paragraphs.
1748 while (untilObj && node && node->GetData() != untilObj)
cdaed652
VZ
1749 {
1750 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
1751 wxASSERT (child != NULL);
1752 if (child)
1753 m_floatCollector->CollectFloat(child);
1754 node = node->GetNext();
1755 }
ce00f59b 1756
cdaed652
VZ
1757 return true;
1758}
1759
603f702b
JS
1760// Returns the style sheet associated with the overall buffer.
1761wxRichTextStyleSheet* wxRichTextParagraphLayoutBox::GetStyleSheet() const
1762{
1763 return GetBuffer() ? GetBuffer()->GetStyleSheet() : (wxRichTextStyleSheet*) NULL;
1764}
1765
1766// Get the number of floating objects at this level
1767int wxRichTextParagraphLayoutBox::GetFloatingObjectCount() const
1768{
1769 if (m_floatCollector)
1770 return m_floatCollector->GetFloatingObjectCount();
1771 else
1772 return 0;
1773}
1774
1775// Get a list of floating objects
1776bool wxRichTextParagraphLayoutBox::GetFloatingObjects(wxRichTextObjectList& objects) const
1777{
1778 if (m_floatCollector)
1779 {
1780 return m_floatCollector->GetFloatingObjects(objects);
1781 }
1782 else
1783 return false;
1784}
1785
1786// Calculate ranges
1787void wxRichTextParagraphLayoutBox::UpdateRanges()
1788{
1789 long start = 0;
1790 if (GetParent())
1791 start = GetRange().GetStart();
1792 long end;
1793 CalculateRange(start, end);
1794}
1795
cdaed652 1796// HitTest
8db2e3ef 1797int wxRichTextParagraphLayoutBox::HitTest(wxDC& dc, wxRichTextDrawingContext& context, const wxPoint& pt, long& textPosition, wxRichTextObject** obj, wxRichTextObject** contextObj, int flags)
cdaed652 1798{
603f702b
JS
1799 if (!IsShown())
1800 return wxRICHTEXT_HITTEST_NONE;
1801
cdaed652 1802 int ret = wxRICHTEXT_HITTEST_NONE;
e12b91a3 1803 if (wxRichTextBuffer::GetFloatingLayoutMode() && m_floatCollector && (flags & wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS) == 0)
8db2e3ef 1804 ret = m_floatCollector->HitTest(dc, context, pt, textPosition, obj, flags);
ce00f59b 1805
cdaed652 1806 if (ret == wxRICHTEXT_HITTEST_NONE)
8db2e3ef 1807 return wxRichTextCompositeObject::HitTest(dc, context, pt, textPosition, obj, contextObj, flags);
cdaed652 1808 else
603f702b
JS
1809 {
1810 *contextObj = this;
cdaed652 1811 return ret;
603f702b 1812 }
cdaed652
VZ
1813}
1814
1815/// Draw the floating objects
8db2e3ef 1816void wxRichTextParagraphLayoutBox::DrawFloats(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int style)
cdaed652 1817{
e12b91a3 1818 if (wxRichTextBuffer::GetFloatingLayoutMode() && m_floatCollector)
8db2e3ef 1819 m_floatCollector->Draw(dc, context, range, selection, rect, descent, style);
cdaed652
VZ
1820}
1821
bec80f4f 1822void wxRichTextParagraphLayoutBox::MoveAnchoredObjectToParagraph(wxRichTextParagraph* from, wxRichTextParagraph* to, wxRichTextObject* obj)
cdaed652
VZ
1823{
1824 if (from == to)
1825 return;
1826
1827 from->RemoveChild(obj);
1828 to->AppendChild(obj);
5d7836c4
JS
1829}
1830
1831/// Draw the item
8db2e3ef 1832bool wxRichTextParagraphLayoutBox::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int style)
5d7836c4 1833{
603f702b
JS
1834 if (!IsShown())
1835 return true;
1836
1837 wxRect thisRect(GetPosition(), GetCachedSize());
1838
8db2e3ef
JS
1839 wxRichTextAttr attr(GetAttributes());
1840 context.ApplyVirtualAttributes(attr, this);
1841
603f702b
JS
1842 int flags = style;
1843 if (selection.IsValid() && GetParentContainer() != this && selection.WithinSelection(GetRange().GetStart(), GetParentContainer()))
1844 flags |= wxRICHTEXT_DRAW_SELECTED;
1845
1846 // Don't draw guidelines if at top level
1847 int theseFlags = flags;
1848 if (!GetParent())
1849 theseFlags &= ~wxRICHTEXT_DRAW_GUIDELINES;
8db2e3ef 1850 DrawBoxAttributes(dc, GetBuffer(), attr, thisRect, theseFlags);
603f702b 1851
e12b91a3
JS
1852 if (wxRichTextBuffer::GetFloatingLayoutMode())
1853 DrawFloats(dc, context, range, selection, rect, descent, style);
1854
5d7836c4
JS
1855 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
1856 while (node)
1857 {
603f702b 1858 wxRichTextObject* child = node->GetData();
7fe8059f 1859
5d7836c4
JS
1860 if (child && !child->GetRange().IsOutside(range))
1861 {
1862 wxRect childRect(child->GetPosition(), child->GetCachedSize());
603f702b
JS
1863 wxRichTextRange childRange = range;
1864 if (child->IsTopLevel())
1865 {
1866 childRange = child->GetOwnRange();
1867 }
7fe8059f 1868
ea160b2e
JS
1869 if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) == 0) && childRect.GetTop() > rect.GetBottom())
1870 {
1871 // Stop drawing
1872 break;
1873 }
1874 else if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) == 0) && childRect.GetBottom() < rect.GetTop())
011b3dcb
JS
1875 {
1876 // Skip
1877 }
1878 else
8db2e3ef 1879 child->Draw(dc, context, childRange, selection, rect, descent, style);
5d7836c4
JS
1880 }
1881
1882 node = node->GetNext();
1883 }
1884 return true;
1885}
1886
1887/// Lay the item out
8db2e3ef 1888bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, wxRichTextDrawingContext& context, const wxRect& rect, const wxRect& parentRect, int style)
5d7836c4 1889{
603f702b
JS
1890 SetPosition(rect.GetPosition());
1891
1892 if (!IsShown())
1893 return true;
1894
4d551ad5
JS
1895 wxRect availableSpace;
1896 bool formatRect = (style & wxRICHTEXT_LAYOUT_SPECIFIED_RECT) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
1897
8db2e3ef
JS
1898 wxRichTextAttr attr(GetAttributes());
1899 context.ApplyVirtualAttributes(attr, this);
1900
4d551ad5 1901 // If only laying out a specific area, the passed rect has a different meaning:
44219ff0
JS
1902 // the visible part of the buffer. This is used in wxRichTextCtrl::OnSize,
1903 // so that during a size, only the visible part will be relaid out, or
1904 // it would take too long causing flicker. As an approximation, we assume that
1905 // everything up to the start of the visible area is laid out correctly.
4d551ad5
JS
1906 if (formatRect)
1907 {
603f702b 1908 wxRect rect2(0, 0, rect.width, rect.height);
8db2e3ef 1909 availableSpace = GetAvailableContentArea(dc, context, rect2);
4d551ad5
JS
1910
1911 // Invalidate the part of the buffer from the first visible line
1912 // to the end. If other parts of the buffer are currently invalid,
1913 // then they too will be taken into account if they are above
1914 // the visible point.
1915 long startPos = 0;
1916 wxRichTextLine* line = GetLineAtYPosition(rect.y);
1917 if (line)
1918 startPos = line->GetAbsoluteRange().GetStart();
1919
603f702b 1920 Invalidate(wxRichTextRange(startPos, GetOwnRange().GetEnd()));
4d551ad5
JS
1921 }
1922 else
603f702b 1923 {
8db2e3ef 1924 availableSpace = GetAvailableContentArea(dc, context, rect);
603f702b
JS
1925 }
1926
d157d142
JS
1927 // Fix the width if we're at the top level
1928 if (!GetParent())
1929 attr.GetTextBoxAttr().GetWidth().SetValue(rect.GetWidth(), wxTEXT_ATTR_UNITS_PIXELS);
1930
603f702b 1931 int leftMargin, rightMargin, topMargin, bottomMargin;
8db2e3ef 1932 wxRichTextObject::GetTotalMargin(dc, GetBuffer(), attr, leftMargin, rightMargin,
603f702b 1933 topMargin, bottomMargin);
5d7836c4
JS
1934
1935 int maxWidth = 0;
603f702b
JS
1936 int maxHeight = 0;
1937
1938 // The maximum paragraph maximum width, so we can set the overall maximum width for this object
1939 int maxMaxWidth = 0;
1940
1941 // The maximum paragraph minimum width, so we can set the overall minimum width for this object
1942 int maxMinWidth = 0;
1943
1944 // If we have vertical alignment, we must recalculate everything.
8db2e3ef
JS
1945 bool hasVerticalAlignment = (attr.GetTextBoxAttr().HasVerticalAlignment() &&
1946 (attr.GetTextBoxAttr().GetVerticalAlignment() > wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_TOP));
7fe8059f 1947
5d7836c4 1948 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
39a1c2f2 1949
38113684 1950 bool layoutAll = true;
1e967276 1951
38113684
JS
1952 // Get invalid range, rounding to paragraph start/end.
1953 wxRichTextRange invalidRange = GetInvalidRange(true);
1954
4d551ad5 1955 if (invalidRange == wxRICHTEXT_NONE && !formatRect)
1e967276
JS
1956 return true;
1957
603f702b 1958 if (invalidRange == wxRICHTEXT_ALL || hasVerticalAlignment)
1e967276 1959 layoutAll = true;
38113684 1960 else // If we know what range is affected, start laying out from that point on.
603f702b 1961 if (invalidRange.GetStart() >= GetOwnRange().GetStart())
2c375f42 1962 {
38113684 1963 wxRichTextParagraph* firstParagraph = GetParagraphAtPosition(invalidRange.GetStart());
2c375f42
JS
1964 if (firstParagraph)
1965 {
1966 wxRichTextObjectList::compatibility_iterator firstNode = m_children.Find(firstParagraph);
0cc70962
VZ
1967 wxRichTextObjectList::compatibility_iterator previousNode;
1968 if ( firstNode )
1969 previousNode = firstNode->GetPrevious();
9b4af7b7 1970 if (firstNode)
2c375f42 1971 {
9b4af7b7
JS
1972 if (previousNode)
1973 {
1974 wxRichTextParagraph* previousParagraph = wxDynamicCast(previousNode->GetData(), wxRichTextParagraph);
1975 availableSpace.y = previousParagraph->GetPosition().y + previousParagraph->GetCachedSize().y;
1976 }
7fe8059f 1977
2c375f42
JS
1978 // Now we're going to start iterating from the first affected paragraph.
1979 node = firstNode;
1e967276
JS
1980
1981 layoutAll = false;
2c375f42
JS
1982 }
1983 }
1984 }
1985
07d4142f
JS
1986 // Gather information about only those floating objects that will not be formatted,
1987 // after which floats will be gathered per-paragraph during layout.
e12b91a3
JS
1988 if (wxRichTextBuffer::GetFloatingLayoutMode())
1989 UpdateFloatingObjects(availableSpace, node ? node->GetData() : (wxRichTextObject*) NULL);
cdaed652 1990
4d551ad5
JS
1991 // A way to force speedy rest-of-buffer layout (the 'else' below)
1992 bool forceQuickLayout = false;
39a1c2f2 1993
d3f6b1b5
JS
1994 // First get the size of the paragraphs we won't be laying out
1995 wxRichTextObjectList::compatibility_iterator n = m_children.GetFirst();
1996 while (n && n != node)
1997 {
1998 wxRichTextParagraph* child = wxDynamicCast(n->GetData(), wxRichTextParagraph);
1999 if (child)
2000 {
2001 maxWidth = wxMax(maxWidth, child->GetCachedSize().x);
2002 maxMinWidth = wxMax(maxMinWidth, child->GetMinSize().x);
2003 maxMaxWidth = wxMax(maxMaxWidth, child->GetMaxSize().x);
2004 }
2005 n = n->GetNext();
2006 }
2007
5d7836c4
JS
2008 while (node)
2009 {
2010 // Assume this box only contains paragraphs
2011
2012 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
706465df
JS
2013 // Unsure if this is needed
2014 // wxCHECK_MSG( child, false, wxT("Unknown object in layout") );
7fe8059f 2015
603f702b 2016 if (child && child->IsShown())
2c375f42 2017 {
603f702b
JS
2018 // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines
2019 if ( !forceQuickLayout &&
2020 (layoutAll ||
2021 child->GetLines().IsEmpty() ||
2022 !child->GetRange().IsOutside(invalidRange)) )
2023 {
2024 // Lays out the object first with a given amount of space, and then if no width was specified in attr,
2025 // lays out the object again using the minimum size
8db2e3ef
JS
2026 child->LayoutToBestSize(dc, context, GetBuffer(),
2027 attr, child->GetAttributes(), availableSpace, rect, style&~wxRICHTEXT_LAYOUT_SPECIFIED_RECT);
603f702b
JS
2028
2029 // Layout must set the cached size
2030 availableSpace.y += child->GetCachedSize().y;
2031 maxWidth = wxMax(maxWidth, child->GetCachedSize().x);
2032 maxMinWidth = wxMax(maxMinWidth, child->GetMinSize().x);
2033 maxMaxWidth = wxMax(maxMaxWidth, child->GetMaxSize().x);
2034
2035 // If we're just formatting the visible part of the buffer,
2036 // and we're now past the bottom of the window, and we don't have any
2037 // floating objects (since they may cause wrapping to change for the rest of the
2038 // the buffer), start quick layout.
2039 if (!hasVerticalAlignment && formatRect && child->GetPosition().y > rect.GetBottom() && GetFloatingObjectCount() == 0)
2040 forceQuickLayout = true;
2041 }
2042 else
2043 {
2044 // We're outside the immediately affected range, so now let's just
2045 // move everything up or down. This assumes that all the children have previously
2046 // been laid out and have wrapped line lists associated with them.
2047 // TODO: check all paragraphs before the affected range.
2048
2049 int inc = availableSpace.y - child->GetPosition().y;
2050
2051 while (node)
2052 {
2053 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
2054 if (child)
2055 {
2056 if (child->GetLines().GetCount() == 0)
2057 {
2058 // Lays out the object first with a given amount of space, and then if no width was specified in attr,
2059 // lays out the object again using the minimum size
8db2e3ef
JS
2060 child->LayoutToBestSize(dc, context, GetBuffer(),
2061 attr, child->GetAttributes(), availableSpace, rect, style&~wxRICHTEXT_LAYOUT_SPECIFIED_RECT);
603f702b
JS
2062
2063 //child->Layout(dc, availableChildRect, style);
2064 }
2065 else
2066 child->Move(wxPoint(child->GetPosition().x, child->GetPosition().y + inc));
5d7836c4 2067
603f702b
JS
2068 availableSpace.y += child->GetCachedSize().y;
2069 maxWidth = wxMax(maxWidth, child->GetCachedSize().x);
2070 maxMinWidth = wxMax(maxMinWidth, child->GetMinSize().x);
2071 maxMaxWidth = wxMax(maxMaxWidth, child->GetMaxSize().x);
2072 }
4d551ad5 2073
603f702b
JS
2074 node = node->GetNext();
2075 }
2076 break;
2077 }
2c375f42 2078 }
7fe8059f 2079
603f702b
JS
2080 node = node->GetNext();
2081 }
2082
2083 node = m_children.GetLast();
2084 if (node && node->GetData()->IsShown())
2085 {
2086 wxRichTextObject* child = node->GetData();
603f702b
JS
2087 maxHeight = child->GetPosition().y - (GetPosition().y + topMargin) + child->GetCachedSize().y;
2088 }
2089 else
2090 maxHeight = 0; // topMargin + bottomMargin;
2091
23698b12 2092 // Check the bottom edge of any floating object
e12b91a3 2093 if (wxRichTextBuffer::GetFloatingLayoutMode() && GetFloatCollector() && GetFloatCollector()->HasFloats())
23698b12
JS
2094 {
2095 int bottom = GetFloatCollector()->GetLastRectBottom();
2096 if (bottom > maxHeight)
2097 maxHeight = bottom;
2098 }
2099
8db2e3ef 2100 if (attr.GetTextBoxAttr().GetSize().GetWidth().IsValid())
bb7bbd12 2101 {
8db2e3ef 2102 wxRect r = AdjustAvailableSpace(dc, GetBuffer(), wxRichTextAttr() /* not used */, attr, parentRect, parentRect);
bb7bbd12
JS
2103 int w = r.GetWidth();
2104
2105 // Convert external to content rect
2106 w = w - leftMargin - rightMargin;
2107 maxWidth = wxMax(maxWidth, w);
2108 maxMaxWidth = wxMax(maxMaxWidth, w);
2109 }
32423dd8
JS
2110 else
2111 {
2112 // TODO: Make sure the layout box's position reflects
2113 // the position of the children, but without
2114 // breaking layout of a box within a paragraph.
2115 }
bb7bbd12 2116
603f702b
JS
2117 // TODO: (also in para layout) should set the
2118 // object's size to an absolute one if specified,
2119 // but if not specified, calculate it from content.
2120
2121 // We need to add back the margins etc.
2122 {
2123 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
2124 contentRect = wxRect(wxPoint(0, 0), wxSize(maxWidth, maxHeight));
8db2e3ef 2125 GetBoxRects(dc, GetBuffer(), attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
2126 SetCachedSize(marginRect.GetSize());
2127 }
2128
2129 // The maximum size is the greatest of all maximum widths for all paragraphs.
2130 {
2131 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
2132 contentRect = wxRect(wxPoint(0, 0), wxSize(maxMaxWidth, maxHeight)); // Actually max height is a lie, we can't know it
8db2e3ef 2133 GetBoxRects(dc, GetBuffer(), attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
2134 SetMaxSize(marginRect.GetSize());
2135 }
2136
2137 // The minimum size is the greatest of all minimum widths for all paragraphs.
2138 {
2139 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
2140 contentRect = wxRect(wxPoint(0, 0), wxSize(maxMinWidth, maxHeight)); // Actually max height is a lie, we can't know it
8db2e3ef 2141 GetBoxRects(dc, GetBuffer(), attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
2142 SetMinSize(marginRect.GetSize());
2143 }
2144
8db2e3ef
JS
2145 if (attr.GetTextBoxAttr().HasVerticalAlignment() &&
2146 (attr.GetTextBoxAttr().GetVerticalAlignment() > wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_TOP))
603f702b
JS
2147 {
2148 int yOffset = 0;
2149 int leftOverSpace = availableSpace.height - topMargin - bottomMargin - maxHeight;
2150 if (leftOverSpace > 0)
2151 {
8db2e3ef 2152 if (attr.GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_CENTRE)
603f702b
JS
2153 {
2154 yOffset = (leftOverSpace/2);
2155 }
8db2e3ef 2156 else if (attr.GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_BOTTOM)
603f702b
JS
2157 {
2158 yOffset = leftOverSpace;
2159 }
2160 }
7fe8059f 2161
603f702b
JS
2162 // Move all the children to vertically align the content
2163 // This doesn't take into account floating objects, unfortunately.
2164 if (yOffset != 0)
2165 {
2166 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2c375f42
JS
2167 while (node)
2168 {
2169 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
2170 if (child)
603f702b 2171 child->Move(wxPoint(child->GetPosition().x, child->GetPosition().y + yOffset));
7fe8059f
WS
2172
2173 node = node->GetNext();
2c375f42 2174 }
2c375f42 2175 }
5d7836c4
JS
2176 }
2177
1e967276 2178 m_invalidRange = wxRICHTEXT_NONE;
5d7836c4
JS
2179
2180 return true;
2181}
2182
5d7836c4 2183/// Get/set the size for the given range.
914a4e23 2184bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, wxRichTextDrawingContext& context, int flags, const wxPoint& position, const wxSize& parentSize, wxArrayInt* WXUNUSED(partialExtents)) const
5d7836c4
JS
2185{
2186 wxSize sz;
2187
09f14108
JS
2188 wxRichTextObjectList::compatibility_iterator startPara = wxRichTextObjectList::compatibility_iterator();
2189 wxRichTextObjectList::compatibility_iterator endPara = wxRichTextObjectList::compatibility_iterator();
5d7836c4
JS
2190
2191 // First find the first paragraph whose starting position is within the range.
2192 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2193 while (node)
2194 {
2195 // child is a paragraph
2196 wxRichTextObject* child = node->GetData();
2197 const wxRichTextRange& r = child->GetRange();
2198
2199 if (r.GetStart() <= range.GetStart() && r.GetEnd() >= range.GetStart())
2200 {
2201 startPara = node;
2202 break;
2203 }
2204
2205 node = node->GetNext();
2206 }
2207
2208 // Next find the last paragraph containing part of the range
2209 node = m_children.GetFirst();
2210 while (node)
2211 {
2212 // child is a paragraph
2213 wxRichTextObject* child = node->GetData();
2214 const wxRichTextRange& r = child->GetRange();
2215
2216 if (r.GetStart() <= range.GetEnd() && r.GetEnd() >= range.GetEnd())
2217 {
2218 endPara = node;
2219 break;
2220 }
2221
2222 node = node->GetNext();
2223 }
2224
2225 if (!startPara || !endPara)
2226 return false;
2227
2228 // Now we can add up the sizes
2229 for (node = startPara; node ; node = node->GetNext())
2230 {
2231 // child is a paragraph
2232 wxRichTextObject* child = node->GetData();
2233 const wxRichTextRange& childRange = child->GetRange();
2234 wxRichTextRange rangeToFind = range;
2235 rangeToFind.LimitTo(childRange);
2236
603f702b
JS
2237 if (child->IsTopLevel())
2238 rangeToFind = child->GetOwnRange();
2239
5d7836c4
JS
2240 wxSize childSize;
2241
2242 int childDescent = 0;
914a4e23 2243 child->GetRangeSize(rangeToFind, childSize, childDescent, dc, context, flags, position, parentSize);
5d7836c4
JS
2244
2245 descent = wxMax(childDescent, descent);
2246
2247 sz.x = wxMax(sz.x, childSize.x);
2248 sz.y += childSize.y;
2249
2250 if (node == endPara)
2251 break;
2252 }
2253
2254 size = sz;
2255
2256 return true;
2257}
2258
2259/// Get the paragraph at the given position
2260wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos, bool caretPosition) const
2261{
2262 if (caretPosition)
2263 pos ++;
2264
2265 // First find the first paragraph whose starting position is within the range.
2266 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2267 while (node)
2268 {
2269 // child is a paragraph
2270 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 2271 // wxASSERT (child != NULL);
5d7836c4 2272
603f702b
JS
2273 if (child)
2274 {
2275 // Return first child in buffer if position is -1
2276 // if (pos == -1)
2277 // return child;
5d7836c4 2278
603f702b
JS
2279 if (child->GetRange().Contains(pos))
2280 return child;
2281 }
5d7836c4
JS
2282
2283 node = node->GetNext();
2284 }
2285 return NULL;
2286}
2287
2288/// Get the line at the given position
2289wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos, bool caretPosition) const
2290{
2291 if (caretPosition)
2292 pos ++;
2293
2294 // First find the first paragraph whose starting position is within the range.
2295 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2296 while (node)
2297 {
7051fa41
JS
2298 wxRichTextObject* obj = (wxRichTextObject*) node->GetData();
2299 if (obj->GetRange().Contains(pos))
5d7836c4 2300 {
7051fa41
JS
2301 // child is a paragraph
2302 wxRichTextParagraph* child = wxDynamicCast(obj, wxRichTextParagraph);
603f702b 2303 // wxASSERT (child != NULL);
7051fa41 2304
603f702b 2305 if (child)
7051fa41 2306 {
603f702b
JS
2307 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
2308 while (node2)
2309 {
2310 wxRichTextLine* line = node2->GetData();
5d7836c4 2311
603f702b 2312 wxRichTextRange range = line->GetAbsoluteRange();
1e967276 2313
603f702b 2314 if (range.Contains(pos) ||
5d7836c4 2315
603f702b
JS
2316 // If the position is end-of-paragraph, then return the last line of
2317 // of the paragraph.
2318 ((range.GetEnd() == child->GetRange().GetEnd()-1) && (pos == child->GetRange().GetEnd())))
2319 return line;
5d7836c4 2320
603f702b
JS
2321 node2 = node2->GetNext();
2322 }
7051fa41 2323 }
7fe8059f 2324 }
5d7836c4
JS
2325
2326 node = node->GetNext();
2327 }
2328
2329 int lineCount = GetLineCount();
2330 if (lineCount > 0)
2331 return GetLineForVisibleLineNumber(lineCount-1);
2332 else
2333 return NULL;
2334}
2335
2336/// Get the line at the given y pixel position, or the last line.
2337wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y) const
2338{
2339 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2340 while (node)
2341 {
2342 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 2343 // wxASSERT (child != NULL);
5d7836c4 2344
603f702b 2345 if (child)
5d7836c4 2346 {
603f702b
JS
2347 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
2348 while (node2)
2349 {
2350 wxRichTextLine* line = node2->GetData();
5d7836c4 2351
603f702b 2352 wxRect rect(line->GetRect());
5d7836c4 2353
603f702b
JS
2354 if (y <= rect.GetBottom())
2355 return line;
5d7836c4 2356
603f702b
JS
2357 node2 = node2->GetNext();
2358 }
7fe8059f 2359 }
5d7836c4
JS
2360
2361 node = node->GetNext();
2362 }
2363
2364 // Return last line
2365 int lineCount = GetLineCount();
2366 if (lineCount > 0)
2367 return GetLineForVisibleLineNumber(lineCount-1);
2368 else
2369 return NULL;
2370}
2371
2372/// Get the number of visible lines
2373int wxRichTextParagraphLayoutBox::GetLineCount() const
2374{
2375 int count = 0;
2376
2377 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2378 while (node)
2379 {
2380 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b
JS
2381 // wxASSERT (child != NULL);
2382
2383 if (child)
2384 count += child->GetLines().GetCount();
5d7836c4 2385
5d7836c4
JS
2386 node = node->GetNext();
2387 }
2388 return count;
2389}
2390
2391
2392/// Get the paragraph for a given line
2393wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine* line) const
2394{
1e967276 2395 return GetParagraphAtPosition(line->GetAbsoluteRange().GetStart());
5d7836c4
JS
2396}
2397
2398/// Get the line size at the given position
2399wxSize wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos, bool caretPosition) const
2400{
2401 wxRichTextLine* line = GetLineAtPosition(pos, caretPosition);
2402 if (line)
2403 {
2404 return line->GetSize();
2405 }
2406 else
2407 return wxSize(0, 0);
2408}
2409
2410
2411/// Convenience function to add a paragraph of text
24777478 2412wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraph(const wxString& text, wxRichTextAttr* paraStyle)
5d7836c4 2413{
fe5aa22c 2414 // Don't use the base style, just the default style, and the base style will
4f32b3cf
JS
2415 // be combined at display time.
2416 // Divide into paragraph and character styles.
3e541562 2417
24777478
JS
2418 wxRichTextAttr defaultCharStyle;
2419 wxRichTextAttr defaultParaStyle;
4f32b3cf 2420
5607c890
JS
2421 // If the default style is a named paragraph style, don't apply any character formatting
2422 // to the initial text string.
2423 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
2424 {
2425 wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
2426 if (def)
2427 defaultParaStyle = def->GetStyleMergedWithBase(GetStyleSheet());
2428 }
2429 else
2430 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle);
2431
24777478
JS
2432 wxRichTextAttr* pStyle = paraStyle ? paraStyle : (wxRichTextAttr*) & defaultParaStyle;
2433 wxRichTextAttr* cStyle = & defaultCharStyle;
4f32b3cf
JS
2434
2435 wxRichTextParagraph* para = new wxRichTextParagraph(text, this, pStyle, cStyle);
32423dd8 2436 para->GetAttributes().GetTextBoxAttr().Reset();
5d7836c4
JS
2437
2438 AppendChild(para);
2439
2440 UpdateRanges();
5d7836c4
JS
2441
2442 return para->GetRange();
2443}
2444
2445/// Adds multiple paragraphs, based on newlines.
24777478 2446wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text, wxRichTextAttr* paraStyle)
5d7836c4 2447{
fe5aa22c 2448 // Don't use the base style, just the default style, and the base style will
4f32b3cf
JS
2449 // be combined at display time.
2450 // Divide into paragraph and character styles.
3e541562 2451
24777478
JS
2452 wxRichTextAttr defaultCharStyle;
2453 wxRichTextAttr defaultParaStyle;
5607c890
JS
2454
2455 // If the default style is a named paragraph style, don't apply any character formatting
2456 // to the initial text string.
2457 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
2458 {
2459 wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
2460 if (def)
2461 defaultParaStyle = def->GetStyleMergedWithBase(GetStyleSheet());
2462 }
2463 else
2464 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle);
5d7836c4 2465
24777478
JS
2466 wxRichTextAttr* pStyle = paraStyle ? paraStyle : (wxRichTextAttr*) & defaultParaStyle;
2467 wxRichTextAttr* cStyle = & defaultCharStyle;
4f32b3cf 2468
5d7836c4
JS
2469 wxRichTextParagraph* firstPara = NULL;
2470 wxRichTextParagraph* lastPara = NULL;
2471
2472 wxRichTextRange range(-1, -1);
0ca07313 2473
5d7836c4 2474 size_t i = 0;
28f92d74 2475 size_t len = text.length();
5d7836c4 2476 wxString line;
4f32b3cf 2477 wxRichTextParagraph* para = new wxRichTextParagraph(wxEmptyString, this, pStyle, cStyle);
32423dd8 2478 para->GetAttributes().GetTextBoxAttr().Reset();
0ca07313
JS
2479
2480 AppendChild(para);
2481
2482 firstPara = para;
2483 lastPara = para;
2484
5d7836c4
JS
2485 while (i < len)
2486 {
2487 wxChar ch = text[i];
2488 if (ch == wxT('\n') || ch == wxT('\r'))
2489 {
99404ab0
JS
2490 if (i != (len-1))
2491 {
2492 wxRichTextPlainText* plainText = (wxRichTextPlainText*) para->GetChildren().GetFirst()->GetData();
2493 plainText->SetText(line);
0ca07313 2494
99404ab0 2495 para = new wxRichTextParagraph(wxEmptyString, this, pStyle, cStyle);
32423dd8 2496 para->GetAttributes().GetTextBoxAttr().Reset();
5d7836c4 2497
99404ab0 2498 AppendChild(para);
0ca07313 2499
99404ab0
JS
2500 lastPara = para;
2501 line = wxEmptyString;
2502 }
5d7836c4
JS
2503 }
2504 else
2505 line += ch;
2506
2507 i ++;
2508 }
0ca07313 2509
7fe8059f 2510 if (!line.empty())
5d7836c4 2511 {
0ca07313
JS
2512 wxRichTextPlainText* plainText = (wxRichTextPlainText*) para->GetChildren().GetFirst()->GetData();
2513 plainText->SetText(line);
5d7836c4
JS
2514 }
2515
5d7836c4 2516 UpdateRanges();
0ca07313 2517
0ca07313 2518 return wxRichTextRange(firstPara->GetRange().GetStart(), lastPara->GetRange().GetEnd());
5d7836c4
JS
2519}
2520
2521/// Convenience function to add an image
24777478 2522wxRichTextRange wxRichTextParagraphLayoutBox::AddImage(const wxImage& image, wxRichTextAttr* paraStyle)
5d7836c4 2523{
fe5aa22c 2524 // Don't use the base style, just the default style, and the base style will
4f32b3cf
JS
2525 // be combined at display time.
2526 // Divide into paragraph and character styles.
3e541562 2527
24777478
JS
2528 wxRichTextAttr defaultCharStyle;
2529 wxRichTextAttr defaultParaStyle;
5607c890
JS
2530
2531 // If the default style is a named paragraph style, don't apply any character formatting
2532 // to the initial text string.
2533 if (GetDefaultStyle().HasParagraphStyleName() && GetStyleSheet())
2534 {
2535 wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(GetDefaultStyle().GetParagraphStyleName());
2536 if (def)
2537 defaultParaStyle = def->GetStyleMergedWithBase(GetStyleSheet());
2538 }
2539 else
2540 wxRichTextSplitParaCharStyles(GetDefaultStyle(), defaultParaStyle, defaultCharStyle);
5d7836c4 2541
24777478
JS
2542 wxRichTextAttr* pStyle = paraStyle ? paraStyle : (wxRichTextAttr*) & defaultParaStyle;
2543 wxRichTextAttr* cStyle = & defaultCharStyle;
5d7836c4 2544
4f32b3cf 2545 wxRichTextParagraph* para = new wxRichTextParagraph(this, pStyle);
32423dd8 2546 para->GetAttributes().GetTextBoxAttr().Reset();
4f32b3cf
JS
2547 AppendChild(para);
2548 para->AppendChild(new wxRichTextImage(image, this, cStyle));
fe5aa22c 2549
5d7836c4 2550 UpdateRanges();
5d7836c4
JS
2551
2552 return para->GetRange();
2553}
2554
2555
2556/// Insert fragment into this box at the given position. If partialParagraph is true,
2557/// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph
2558/// marker.
5d7836c4 2559
0ca07313 2560bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParagraphLayoutBox& fragment)
5d7836c4 2561{
5d7836c4
JS
2562 // First, find the first paragraph whose starting position is within the range.
2563 wxRichTextParagraph* para = GetParagraphAtPosition(position);
2564 if (para)
2565 {
24777478 2566 wxRichTextAttr originalAttr = para->GetAttributes();
99404ab0 2567
5d7836c4
JS
2568 wxRichTextObjectList::compatibility_iterator node = m_children.Find(para);
2569
2570 // Now split at this position, returning the object to insert the new
2571 // ones in front of.
2572 wxRichTextObject* nextObject = para->SplitAt(position);
2573
2574 // Special case: partial paragraph, just one paragraph. Might be a small amount of
2575 // text, for example, so let's optimize.
2576
2577 if (fragment.GetPartialParagraph() && fragment.GetChildren().GetCount() == 1)
2578 {
2579 // Add the first para to this para...
2580 wxRichTextObjectList::compatibility_iterator firstParaNode = fragment.GetChildren().GetFirst();
2581 if (!firstParaNode)
2582 return false;
2583
2584 // Iterate through the fragment paragraph inserting the content into this paragraph.
2585 wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph);
2586 wxASSERT (firstPara != NULL);
2587
2588 wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst();
2589 while (objectNode)
2590 {
2591 wxRichTextObject* newObj = objectNode->GetData()->Clone();
7fe8059f 2592
5d7836c4
JS
2593 if (!nextObject)
2594 {
2595 // Append
2596 para->AppendChild(newObj);
2597 }
2598 else
2599 {
2600 // Insert before nextObject
2601 para->InsertChild(newObj, nextObject);
2602 }
7fe8059f 2603
5d7836c4
JS
2604 objectNode = objectNode->GetNext();
2605 }
2606
2607 return true;
2608 }
2609 else
2610 {
2611 // Procedure for inserting a fragment consisting of a number of
2612 // paragraphs:
2613 //
2614 // 1. Remove and save the content that's after the insertion point, for adding
2615 // back once we've added the fragment.
2616 // 2. Add the content from the first fragment paragraph to the current
2617 // paragraph.
2618 // 3. Add remaining fragment paragraphs after the current paragraph.
2619 // 4. Add back the saved content from the first paragraph. If partialParagraph
2620 // is true, add it to the last paragraph added and not a new one.
2621
2622 // 1. Remove and save objects after split point.
2623 wxList savedObjects;
2624 if (nextObject)
2625 para->MoveToList(nextObject, savedObjects);
2626
2627 // 2. Add the content from the 1st fragment paragraph.
2628 wxRichTextObjectList::compatibility_iterator firstParaNode = fragment.GetChildren().GetFirst();
2629 if (!firstParaNode)
2630 return false;
2631
2632 wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph);
2633 wxASSERT(firstPara != NULL);
2634
6c0ea513
JS
2635 if (!(fragment.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE))
2636 para->SetAttributes(firstPara->GetAttributes());
99404ab0
JS
2637
2638 // Save empty paragraph attributes for appending later
2639 // These are character attributes deliberately set for a new paragraph. Without this,
2640 // we couldn't pass default attributes when appending a new paragraph.
24777478 2641 wxRichTextAttr emptyParagraphAttributes;
99404ab0 2642
5d7836c4 2643 wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst();
99404ab0
JS
2644
2645 if (objectNode && firstPara->GetChildren().GetCount() == 1 && objectNode->GetData()->IsEmpty())
2646 emptyParagraphAttributes = objectNode->GetData()->GetAttributes();
2647
5d7836c4
JS
2648 while (objectNode)
2649 {
c025e094 2650 wxRichTextObject* newObj = objectNode->GetData()->Clone();
7fe8059f 2651
c025e094
JS
2652 // Append
2653 para->AppendChild(newObj);
7fe8059f 2654
5d7836c4
JS
2655 objectNode = objectNode->GetNext();
2656 }
2657
2658 // 3. Add remaining fragment paragraphs after the current paragraph.
2659 wxRichTextObjectList::compatibility_iterator nextParagraphNode = node->GetNext();
2660 wxRichTextObject* nextParagraph = NULL;
2661 if (nextParagraphNode)
2662 nextParagraph = nextParagraphNode->GetData();
2663
2664 wxRichTextObjectList::compatibility_iterator i = fragment.GetChildren().GetFirst()->GetNext();
2665 wxRichTextParagraph* finalPara = para;
2666
99404ab0
JS
2667 bool needExtraPara = (!i || !fragment.GetPartialParagraph());
2668
5d7836c4 2669 // If there was only one paragraph, we need to insert a new one.
99404ab0 2670 while (i)
5d7836c4 2671 {
99404ab0
JS
2672 wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph);
2673 wxASSERT( para != NULL );
5d7836c4 2674
99404ab0 2675 finalPara = (wxRichTextParagraph*) para->Clone();
5d7836c4
JS
2676
2677 if (nextParagraph)
2678 InsertChild(finalPara, nextParagraph);
2679 else
7fe8059f 2680 AppendChild(finalPara);
99404ab0
JS
2681
2682 i = i->GetNext();
5d7836c4 2683 }
5d7836c4 2684
99404ab0
JS
2685 // If there was only one paragraph, or we have full paragraphs in our fragment,
2686 // we need to insert a new one.
2687 if (needExtraPara)
2688 {
2689 finalPara = new wxRichTextParagraph;
5d7836c4
JS
2690
2691 if (nextParagraph)
2692 InsertChild(finalPara, nextParagraph);
2693 else
2694 AppendChild(finalPara);
5d7836c4
JS
2695 }
2696
2697 // 4. Add back the remaining content.
2698 if (finalPara)
2699 {
c025e094
JS
2700 if (nextObject)
2701 finalPara->MoveFromList(savedObjects);
5d7836c4
JS
2702
2703 // Ensure there's at least one object
2704 if (finalPara->GetChildCount() == 0)
2705 {
7fe8059f 2706 wxRichTextPlainText* text = new wxRichTextPlainText(wxEmptyString);
99404ab0 2707 text->SetAttributes(emptyParagraphAttributes);
5d7836c4
JS
2708
2709 finalPara->AppendChild(text);
2710 }
2711 }
2712
6c0ea513
JS
2713 if ((fragment.GetAttributes().GetFlags() & wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE) && firstPara)
2714 finalPara->SetAttributes(firstPara->GetAttributes());
2715 else if (finalPara && finalPara != para)
99404ab0
JS
2716 finalPara->SetAttributes(originalAttr);
2717
5d7836c4
JS
2718 return true;
2719 }
2720 }
2721 else
2722 {
2723 // Append
2724 wxRichTextObjectList::compatibility_iterator i = fragment.GetChildren().GetFirst();
2725 while (i)
2726 {
2727 wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph);
2728 wxASSERT( para != NULL );
7fe8059f 2729
5d7836c4 2730 AppendChild(para->Clone());
7fe8059f 2731
5d7836c4
JS
2732 i = i->GetNext();
2733 }
2734
2735 return true;
2736 }
5d7836c4
JS
2737}
2738
2739/// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'.
2740/// If there was an incomplete paragraph at the end, partialParagraph is set to true.
0ca07313 2741bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange& range, wxRichTextParagraphLayoutBox& fragment)
5d7836c4
JS
2742{
2743 wxRichTextObjectList::compatibility_iterator i = GetChildren().GetFirst();
2744 while (i)
2745 {
2746 wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph);
2747 wxASSERT( para != NULL );
2748
2749 if (!para->GetRange().IsOutside(range))
2750 {
2751 fragment.AppendChild(para->Clone());
7fe8059f 2752 }
5d7836c4
JS
2753 i = i->GetNext();
2754 }
2755
2756 // Now top and tail the first and last paragraphs in our new fragment (which might be the same).
2757 if (!fragment.IsEmpty())
2758 {
5d7836c4
JS
2759 wxRichTextParagraph* firstPara = wxDynamicCast(fragment.GetChildren().GetFirst()->GetData(), wxRichTextParagraph);
2760 wxASSERT( firstPara != NULL );
2761
0e190fa2
JS
2762 wxRichTextParagraph* lastPara = wxDynamicCast(fragment.GetChildren().GetLast()->GetData(), wxRichTextParagraph);
2763 wxASSERT( lastPara != NULL );
2764
2765 if (!firstPara || !lastPara)
2766 return false;
2767
2768 bool isFragment = (range.GetEnd() < lastPara->GetRange().GetEnd());
2769
2770 long firstPos = firstPara->GetRange().GetStart();
2771
2772 // Adjust for renumbering from zero
2773 wxRichTextRange topTailRange(range.GetStart() - firstPos, range.GetEnd() - firstPos);
2774
2775 long end;
2776 fragment.CalculateRange(0, end);
2777
5d7836c4 2778 // Chop off the start of the paragraph
0e190fa2 2779 if (topTailRange.GetStart() > 0)
5d7836c4 2780 {
0e190fa2 2781 wxRichTextRange r(0, topTailRange.GetStart()-1);
5d7836c4
JS
2782 firstPara->DeleteRange(r);
2783
2784 // Make sure the numbering is correct
0e190fa2 2785 fragment.CalculateRange(0, end);
5d7836c4
JS
2786
2787 // Now, we've deleted some positions, so adjust the range
2788 // accordingly.
0e190fa2
JS
2789 topTailRange.SetStart(range.GetLength());
2790 topTailRange.SetEnd(fragment.GetOwnRange().GetEnd());
2791 }
2792 else
2793 {
2794 topTailRange.SetStart(range.GetLength());
2795 topTailRange.SetEnd(fragment.GetOwnRange().GetEnd());
5d7836c4
JS
2796 }
2797
61e6149e 2798 if (topTailRange.GetStart() < lastPara->GetRange().GetEnd())
5d7836c4 2799 {
0e190fa2 2800 lastPara->DeleteRange(topTailRange);
5d7836c4
JS
2801
2802 // Make sure the numbering is correct
2803 long end;
0e190fa2 2804 fragment.CalculateRange(0, end);
5d7836c4
JS
2805
2806 // We only have part of a paragraph at the end
2807 fragment.SetPartialParagraph(true);
2808 }
2809 else
2810 {
0e190fa2
JS
2811 // We have a partial paragraph (don't save last new paragraph marker)
2812 // or complete paragraph
2813 fragment.SetPartialParagraph(isFragment);
5d7836c4
JS
2814 }
2815 }
2816
2817 return true;
2818}
2819
2820/// Given a position, get the number of the visible line (potentially many to a paragraph),
2821/// starting from zero at the start of the buffer.
2822long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos, bool caretPosition, bool startOfLine) const
2823{
2824 if (caretPosition)
2825 pos ++;
2826
2827 int lineCount = 0;
2828
2829 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2830 while (node)
2831 {
2832 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 2833 // wxASSERT( child != NULL );
5d7836c4 2834
603f702b 2835 if (child)
5d7836c4 2836 {
603f702b 2837 if (child->GetRange().Contains(pos))
5d7836c4 2838 {
603f702b
JS
2839 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
2840 while (node2)
5d7836c4 2841 {
603f702b
JS
2842 wxRichTextLine* line = node2->GetData();
2843 wxRichTextRange lineRange = line->GetAbsoluteRange();
5d7836c4 2844
603f702b
JS
2845 if (lineRange.Contains(pos) || pos == lineRange.GetStart())
2846 {
2847 // If the caret is displayed at the end of the previous wrapped line,
2848 // we want to return the line it's _displayed_ at (not the actual line
2849 // containing the position).
2850 if (lineRange.GetStart() == pos && !startOfLine && child->GetRange().GetStart() != pos)
2851 return lineCount - 1;
2852 else
2853 return lineCount;
2854 }
7fe8059f 2855
603f702b
JS
2856 lineCount ++;
2857
2858 node2 = node2->GetNext();
2859 }
2860 // If we didn't find it in the lines, it must be
2861 // the last position of the paragraph. So return the last line.
2862 return lineCount-1;
5d7836c4 2863 }
603f702b
JS
2864 else
2865 lineCount += child->GetLines().GetCount();
5d7836c4 2866 }
5d7836c4
JS
2867
2868 node = node->GetNext();
2869 }
2870
2871 // Not found
2872 return -1;
2873}
2874
2875/// Given a line number, get the corresponding wxRichTextLine object.
2876wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber) const
2877{
2878 int lineCount = 0;
2879
2880 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
2881 while (node)
2882 {
2883 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 2884 // wxASSERT(child != NULL);
5d7836c4 2885
603f702b 2886 if (child)
5d7836c4 2887 {
603f702b 2888 if (lineNumber < (int) (child->GetLines().GetCount() + lineCount))
5d7836c4 2889 {
603f702b
JS
2890 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
2891 while (node2)
2892 {
2893 wxRichTextLine* line = node2->GetData();
7fe8059f 2894
603f702b
JS
2895 if (lineCount == lineNumber)
2896 return line;
5d7836c4 2897
603f702b 2898 lineCount ++;
7fe8059f 2899
603f702b
JS
2900 node2 = node2->GetNext();
2901 }
7fe8059f 2902 }
603f702b
JS
2903 else
2904 lineCount += child->GetLines().GetCount();
5d7836c4 2905 }
5d7836c4
JS
2906
2907 node = node->GetNext();
2908 }
2909
2910 // Didn't find it
2911 return NULL;
2912}
2913
2914/// Delete range from layout.
2915bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange& range)
2916{
2917 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
7fe8059f 2918
99404ab0 2919 wxRichTextParagraph* firstPara = NULL;
5d7836c4
JS
2920 while (node)
2921 {
2922 wxRichTextParagraph* obj = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 2923 // wxASSERT (obj != NULL);
5d7836c4
JS
2924
2925 wxRichTextObjectList::compatibility_iterator next = node->GetNext();
7fe8059f 2926
603f702b 2927 if (obj)
5d7836c4 2928 {
603f702b 2929 // Delete the range in each paragraph
99404ab0 2930
603f702b 2931 if (!obj->GetRange().IsOutside(range))
5d7836c4 2932 {
603f702b
JS
2933 // Deletes the content of this object within the given range
2934 obj->DeleteRange(range);
99404ab0 2935
603f702b
JS
2936 wxRichTextRange thisRange = obj->GetRange();
2937 wxRichTextAttr thisAttr = obj->GetAttributes();
5d7836c4 2938
603f702b
JS
2939 // If the whole paragraph is within the range to delete,
2940 // delete the whole thing.
2941 if (range.GetStart() <= thisRange.GetStart() && range.GetEnd() >= thisRange.GetEnd())
5d7836c4 2942 {
603f702b
JS
2943 // Delete the whole object
2944 RemoveChild(obj, true);
2945 obj = NULL;
99404ab0 2946 }
603f702b
JS
2947 else if (!firstPara)
2948 firstPara = obj;
5d7836c4 2949
603f702b
JS
2950 // If the range includes the paragraph end, we need to join this
2951 // and the next paragraph.
2952 if (range.GetEnd() <= thisRange.GetEnd())
6c0ea513 2953 {
603f702b
JS
2954 // We need to move the objects from the next paragraph
2955 // to this paragraph
2956
2957 wxRichTextParagraph* nextParagraph = NULL;
2958 if ((range.GetEnd() < thisRange.GetEnd()) && obj)
2959 nextParagraph = obj;
6c0ea513 2960 else
603f702b
JS
2961 {
2962 // We're ending at the end of the paragraph, so merge the _next_ paragraph.
2963 if (next)
2964 nextParagraph = wxDynamicCast(next->GetData(), wxRichTextParagraph);
2965 }
5d7836c4 2966
603f702b
JS
2967 bool applyFinalParagraphStyle = firstPara && nextParagraph && nextParagraph != firstPara;
2968
2969 wxRichTextAttr nextParaAttr;
2970 if (applyFinalParagraphStyle)
2971 {
2972 // Special case when deleting the end of a paragraph - use _this_ paragraph's style,
2973 // not the next one.
2974 if (range.GetStart() == range.GetEnd() && range.GetStart() == thisRange.GetEnd())
2975 nextParaAttr = thisAttr;
2976 else
2977 nextParaAttr = nextParagraph->GetAttributes();
2978 }
5d7836c4 2979
603f702b 2980 if (firstPara && nextParagraph && firstPara != nextParagraph)
99404ab0 2981 {
603f702b
JS
2982 // Move the objects to the previous para
2983 wxRichTextObjectList::compatibility_iterator node1 = nextParagraph->GetChildren().GetFirst();
5d7836c4 2984
603f702b
JS
2985 while (node1)
2986 {
2987 wxRichTextObject* obj1 = node1->GetData();
5d7836c4 2988
603f702b 2989 firstPara->AppendChild(obj1);
5d7836c4 2990
603f702b
JS
2991 wxRichTextObjectList::compatibility_iterator next1 = node1->GetNext();
2992 nextParagraph->GetChildren().Erase(node1);
99404ab0 2993
603f702b
JS
2994 node1 = next1;
2995 }
5d7836c4 2996
603f702b
JS
2997 // Delete the paragraph
2998 RemoveChild(nextParagraph, true);
2999 }
fa01bfdd 3000
603f702b
JS
3001 // Avoid empty paragraphs
3002 if (firstPara && firstPara->GetChildren().GetCount() == 0)
3003 {
3004 wxRichTextPlainText* text = new wxRichTextPlainText(wxEmptyString);
3005 firstPara->AppendChild(text);
3006 }
99404ab0 3007
603f702b
JS
3008 if (applyFinalParagraphStyle)
3009 firstPara->SetAttributes(nextParaAttr);
3010
3011 return true;
3012 }
5d7836c4
JS
3013 }
3014 }
7fe8059f 3015
5d7836c4
JS
3016 node = next;
3017 }
7fe8059f 3018
5d7836c4
JS
3019 return true;
3020}
3021
3022/// Get any text in this object for the given range
3023wxString wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange& range) const
3024{
3025 int lineCount = 0;
3026 wxString text;
3027 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3028 while (node)
3029 {
3030 wxRichTextObject* child = node->GetData();
3031 if (!child->GetRange().IsOutside(range))
3032 {
5d7836c4
JS
3033 wxRichTextRange childRange = range;
3034 childRange.LimitTo(child->GetRange());
7fe8059f 3035
5d7836c4 3036 wxString childText = child->GetTextForRange(childRange);
7fe8059f 3037
5d7836c4
JS
3038 text += childText;
3039
1a75935d 3040 if ((childRange.GetEnd() == child->GetRange().GetEnd()) && node->GetNext())
fe5aa22c
JS
3041 text += wxT("\n");
3042
5d7836c4
JS
3043 lineCount ++;
3044 }
3045 node = node->GetNext();
3046 }
3047
3048 return text;
3049}
3050
3051/// Get all the text
3052wxString wxRichTextParagraphLayoutBox::GetText() const
3053{
c99f1b0f 3054 return GetTextForRange(GetOwnRange());
5d7836c4
JS
3055}
3056
3057/// Get the paragraph by number
3058wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber) const
3059{
27e20452 3060 if ((size_t) paragraphNumber >= GetChildCount())
5d7836c4
JS
3061 return NULL;
3062
3063 return (wxRichTextParagraph*) GetChild((size_t) paragraphNumber);
3064}
3065
3066/// Get the length of the paragraph
3067int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber) const
3068{
3069 wxRichTextParagraph* para = GetParagraphAtLine(paragraphNumber);
3070 if (para)
3071 return para->GetRange().GetLength() - 1; // don't include newline
3072 else
3073 return 0;
3074}
3075
3076/// Get the text of the paragraph
3077wxString wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber) const
3078{
3079 wxRichTextParagraph* para = GetParagraphAtLine(paragraphNumber);
3080 if (para)
3081 return para->GetTextForRange(para->GetRange());
3082 else
3083 return wxEmptyString;
3084}
3085
3086/// Convert zero-based line column and paragraph number to a position.
3087long wxRichTextParagraphLayoutBox::XYToPosition(long x, long y) const
3088{
3089 wxRichTextParagraph* para = GetParagraphAtLine(y);
3090 if (para)
3091 {
3092 return para->GetRange().GetStart() + x;
3093 }
3094 else
3095 return -1;
3096}
3097
3098/// Convert zero-based position to line column and paragraph number
3099bool wxRichTextParagraphLayoutBox::PositionToXY(long pos, long* x, long* y) const
3100{
3101 wxRichTextParagraph* para = GetParagraphAtPosition(pos);
3102 if (para)
3103 {
3104 int count = 0;
3105 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3106 while (node)
3107 {
3108 wxRichTextObject* child = node->GetData();
3109 if (child == para)
3110 break;
3111 count ++;
3112 node = node->GetNext();
3113 }
3114
3115 *y = count;
3116 *x = pos - para->GetRange().GetStart();
3117
3118 return true;
3119 }
3120 else
3121 return false;
3122}
3123
3124/// Get the leaf object in a paragraph at this position.
3125/// Given a line number, get the corresponding wxRichTextLine object.
3126wxRichTextObject* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position) const
3127{
3128 wxRichTextParagraph* para = GetParagraphAtPosition(position);
3129 if (para)
3130 {
3131 wxRichTextObjectList::compatibility_iterator node = para->GetChildren().GetFirst();
7fe8059f 3132
5d7836c4
JS
3133 while (node)
3134 {
3135 wxRichTextObject* child = node->GetData();
3136 if (child->GetRange().Contains(position))
3137 return child;
7fe8059f 3138
5d7836c4
JS
3139 node = node->GetNext();
3140 }
3141 if (position == para->GetRange().GetEnd() && para->GetChildCount() > 0)
3142 return para->GetChildren().GetLast()->GetData();
3143 }
3144 return NULL;
3145}
3146
3147/// Set character or paragraph text attributes: apply character styles only to immediate text nodes
24777478 3148bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style, int flags)
5d7836c4
JS
3149{
3150 bool characterStyle = false;
3151 bool paragraphStyle = false;
3152
3153 if (style.IsCharacterStyle())
3154 characterStyle = true;
3155 if (style.IsParagraphStyle())
3156 paragraphStyle = true;
3157
603f702b
JS
3158 wxRichTextBuffer* buffer = GetBuffer();
3159
59509217
JS
3160 bool withUndo = ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0);
3161 bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
3162 bool parasOnly = ((flags & wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY) != 0);
3163 bool charactersOnly = ((flags & wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY) != 0);
523d2f14 3164 bool resetExistingStyle = ((flags & wxRICHTEXT_SETSTYLE_RESET) != 0);
aeb6ebe2 3165 bool removeStyle = ((flags & wxRICHTEXT_SETSTYLE_REMOVE) != 0);
523d2f14
JS
3166
3167 // Apply paragraph style first, if any
24777478 3168 wxRichTextAttr wholeStyle(style);
523d2f14 3169
603f702b 3170 if (!removeStyle && wholeStyle.HasParagraphStyleName() && buffer->GetStyleSheet())
523d2f14 3171 {
603f702b 3172 wxRichTextParagraphStyleDefinition* def = buffer->GetStyleSheet()->FindParagraphStyle(wholeStyle.GetParagraphStyleName());
523d2f14 3173 if (def)
603f702b 3174 wxRichTextApplyStyle(wholeStyle, def->GetStyleMergedWithBase(buffer->GetStyleSheet()));
523d2f14 3175 }
59509217
JS
3176
3177 // Limit the attributes to be set to the content to only character attributes.
24777478 3178 wxRichTextAttr characterAttributes(wholeStyle);
59509217
JS
3179 characterAttributes.SetFlags(characterAttributes.GetFlags() & (wxTEXT_ATTR_CHARACTER));
3180
603f702b 3181 if (!removeStyle && characterAttributes.HasCharacterStyleName() && buffer->GetStyleSheet())
523d2f14 3182 {
603f702b 3183 wxRichTextCharacterStyleDefinition* def = buffer->GetStyleSheet()->FindCharacterStyle(characterAttributes.GetCharacterStyleName());
523d2f14 3184 if (def)
603f702b 3185 wxRichTextApplyStyle(characterAttributes, def->GetStyleMergedWithBase(buffer->GetStyleSheet()));
523d2f14
JS
3186 }
3187
5d7836c4
JS
3188 // If we are associated with a control, make undoable; otherwise, apply immediately
3189 // to the data.
3190
603f702b 3191 bool haveControl = (buffer->GetRichTextCtrl() != NULL);
5d7836c4
JS
3192
3193 wxRichTextAction* action = NULL;
7fe8059f 3194
5d7836c4
JS
3195 if (haveControl && withUndo)
3196 {
603f702b 3197 action = new wxRichTextAction(NULL, _("Change Style"), wxRICHTEXT_CHANGE_STYLE, buffer, this, buffer->GetRichTextCtrl());
5d7836c4 3198 action->SetRange(range);
603f702b 3199 action->SetPosition(buffer->GetRichTextCtrl()->GetCaretPosition());
5d7836c4
JS
3200 }
3201
3202 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3203 while (node)
3204 {
3205 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 3206 // wxASSERT (para != NULL);
5d7836c4
JS
3207
3208 if (para && para->GetChildCount() > 0)
3209 {
3210 // Stop searching if we're beyond the range of interest
3211 if (para->GetRange().GetStart() > range.GetEnd())
3212 break;
3213
3214 if (!para->GetRange().IsOutside(range))
3215 {
3216 // We'll be using a copy of the paragraph to make style changes,
3217 // not updating the buffer directly.
4e09ebe8 3218 wxRichTextParagraph* newPara wxDUMMY_INITIALIZE(NULL);
7fe8059f 3219
5d7836c4
JS
3220 if (haveControl && withUndo)
3221 {
3222 newPara = new wxRichTextParagraph(*para);
3223 action->GetNewParagraphs().AppendChild(newPara);
3224
3225 // Also store the old ones for Undo
3226 action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para));
3227 }
3228 else
3229 newPara = para;
41a85215 3230
a7ed48a5
JS
3231 // If we're specifying paragraphs only, then we really mean character formatting
3232 // to be included in the paragraph style
3233 if ((paragraphStyle || parasOnly) && !charactersOnly)
59509217 3234 {
aeb6ebe2
JS
3235 if (removeStyle)
3236 {
3237 // Removes the given style from the paragraph
3238 wxRichTextRemoveStyle(newPara->GetAttributes(), style);
3239 }
3240 else if (resetExistingStyle)
523d2f14
JS
3241 newPara->GetAttributes() = wholeStyle;
3242 else
59509217 3243 {
523d2f14
JS
3244 if (applyMinimal)
3245 {
3246 // Only apply attributes that will make a difference to the combined
3247 // style as seen on the display
603f702b 3248 wxRichTextAttr combinedAttr(para->GetCombinedAttributes(true));
523d2f14
JS
3249 wxRichTextApplyStyle(newPara->GetAttributes(), wholeStyle, & combinedAttr);
3250 }
3251 else
3252 wxRichTextApplyStyle(newPara->GetAttributes(), wholeStyle);
59509217 3253 }
59509217 3254 }
5d7836c4 3255
5912d19e 3256 // When applying paragraph styles dynamically, don't change the text objects' attributes
fe5aa22c
JS
3257 // since they will computed as needed. Only apply the character styling if it's _only_
3258 // character styling. This policy is subject to change and might be put under user control.
3259
59509217
JS
3260 // Hm. we might well be applying a mix of paragraph and character styles, in which
3261 // case we _do_ want to apply character styles regardless of what para styles are set.
3262 // But if we're applying a paragraph style, which has some character attributes, but
3263 // we only want the paragraphs to hold this character style, then we _don't_ want to
3264 // apply the character style. So we need to be able to choose.
3265
f1d800d9 3266 if (!parasOnly && (characterStyle|charactersOnly) && range.GetStart() != newPara->GetRange().GetEnd())
5d7836c4
JS
3267 {
3268 wxRichTextRange childRange(range);
3269 childRange.LimitTo(newPara->GetRange());
7fe8059f 3270
5d7836c4
JS
3271 // Find the starting position and if necessary split it so
3272 // we can start applying a different style.
3273 // TODO: check that the style actually changes or is different
3274 // from style outside of range
4e09ebe8
JS
3275 wxRichTextObject* firstObject wxDUMMY_INITIALIZE(NULL);
3276 wxRichTextObject* lastObject wxDUMMY_INITIALIZE(NULL);
7fe8059f 3277
5d7836c4
JS
3278 if (childRange.GetStart() == newPara->GetRange().GetStart())
3279 firstObject = newPara->GetChildren().GetFirst()->GetData();
3280 else
3281 firstObject = newPara->SplitAt(range.GetStart());
7fe8059f 3282
5d7836c4
JS
3283 // Increment by 1 because we're apply the style one _after_ the split point
3284 long splitPoint = childRange.GetEnd();
3285 if (splitPoint != newPara->GetRange().GetEnd())
3286 splitPoint ++;
7fe8059f 3287
5d7836c4 3288 // Find last object
4b3483e7 3289 if (splitPoint == newPara->GetRange().GetEnd())
5d7836c4
JS
3290 lastObject = newPara->GetChildren().GetLast()->GetData();
3291 else
3292 // lastObject is set as a side-effect of splitting. It's
3293 // returned as the object before the new object.
3294 (void) newPara->SplitAt(splitPoint, & lastObject);
7fe8059f 3295
5d7836c4
JS
3296 wxASSERT(firstObject != NULL);
3297 wxASSERT(lastObject != NULL);
7fe8059f 3298
5d7836c4
JS
3299 if (!firstObject || !lastObject)
3300 continue;
7fe8059f 3301
5d7836c4
JS
3302 wxRichTextObjectList::compatibility_iterator firstNode = newPara->GetChildren().Find(firstObject);
3303 wxRichTextObjectList::compatibility_iterator lastNode = newPara->GetChildren().Find(lastObject);
7fe8059f 3304
4c9847e1
MW
3305 wxASSERT(firstNode);
3306 wxASSERT(lastNode);
7fe8059f 3307
5d7836c4 3308 wxRichTextObjectList::compatibility_iterator node2 = firstNode;
7fe8059f 3309
5d7836c4
JS
3310 while (node2)
3311 {
3312 wxRichTextObject* child = node2->GetData();
7fe8059f 3313
aeb6ebe2
JS
3314 if (removeStyle)
3315 {
3316 // Removes the given style from the paragraph
3317 wxRichTextRemoveStyle(child->GetAttributes(), style);
3318 }
3319 else if (resetExistingStyle)
5fe7fce4
JS
3320 {
3321 // Preserve the URL as it's not really a formatting style but a property of the object
3322 wxString url;
3323 if (child->GetAttributes().HasURL() && !characterAttributes.HasURL())
3324 url = child->GetAttributes().GetURL();
3325
523d2f14 3326 child->GetAttributes() = characterAttributes;
5fe7fce4
JS
3327
3328 if (!url.IsEmpty())
3329 child->GetAttributes().SetURL(url);
3330 }
523d2f14 3331 else
59509217 3332 {
523d2f14
JS
3333 if (applyMinimal)
3334 {
3335 // Only apply attributes that will make a difference to the combined
3336 // style as seen on the display
603f702b 3337 wxRichTextAttr combinedAttr(newPara->GetCombinedAttributes(child->GetAttributes(), true));
523d2f14
JS
3338 wxRichTextApplyStyle(child->GetAttributes(), characterAttributes, & combinedAttr);
3339 }
3340 else
3341 wxRichTextApplyStyle(child->GetAttributes(), characterAttributes);
59509217 3342 }
59509217 3343
5d7836c4
JS
3344 if (node2 == lastNode)
3345 break;
7fe8059f 3346
5d7836c4
JS
3347 node2 = node2->GetNext();
3348 }
3349 }
3350 }
3351 }
3352
3353 node = node->GetNext();
3354 }
3355
3356 // Do action, or delay it until end of batch.
3357 if (haveControl && withUndo)
603f702b 3358 buffer->SubmitAction(action);
5d7836c4
JS
3359
3360 return true;
3361}
3362
603f702b
JS
3363// Just change the attributes for this single object.
3364void wxRichTextParagraphLayoutBox::SetStyle(wxRichTextObject* obj, const wxRichTextAttr& textAttr, int flags)
cdaed652 3365{
603f702b 3366 wxRichTextBuffer* buffer = GetBuffer();
cdaed652 3367 bool withUndo = flags & wxRICHTEXT_SETSTYLE_WITH_UNDO;
603f702b
JS
3368 bool resetExistingStyle = ((flags & wxRICHTEXT_SETSTYLE_RESET) != 0);
3369 bool haveControl = (buffer->GetRichTextCtrl() != NULL);
3370
cdaed652 3371 wxRichTextAction *action = NULL;
603f702b
JS
3372 wxRichTextAttr newAttr = obj->GetAttributes();
3373 if (resetExistingStyle)
3374 newAttr = textAttr;
3375 else
3376 newAttr.Apply(textAttr);
cdaed652
VZ
3377
3378 if (haveControl && withUndo)
3379 {
603f702b
JS
3380 action = new wxRichTextAction(NULL, _("Change Object Style"), wxRICHTEXT_CHANGE_ATTRIBUTES, buffer, obj->GetContainer(), buffer->GetRichTextCtrl());
3381 action->SetRange(obj->GetRange().FromInternal());
3382 action->SetPosition(buffer->GetRichTextCtrl()->GetCaretPosition());
3383 action->MakeObject(obj);
bec80f4f 3384
603f702b 3385 action->GetAttributes() = newAttr;
cdaed652
VZ
3386 }
3387 else
603f702b 3388 obj->GetAttributes() = newAttr;
cdaed652
VZ
3389
3390 if (haveControl && withUndo)
603f702b 3391 buffer->SubmitAction(action);
cdaed652
VZ
3392}
3393
5d7836c4 3394/// Get the text attributes for this position.
24777478 3395bool wxRichTextParagraphLayoutBox::GetStyle(long position, wxRichTextAttr& style)
5d7836c4 3396{
fe5aa22c
JS
3397 return DoGetStyle(position, style, true);
3398}
e191ee87 3399
24777478 3400bool wxRichTextParagraphLayoutBox::GetUncombinedStyle(long position, wxRichTextAttr& style)
fe5aa22c
JS
3401{
3402 return DoGetStyle(position, style, false);
3403}
3404
fe5aa22c
JS
3405/// Implementation helper for GetStyle. If combineStyles is true, combine base, paragraph and
3406/// context attributes.
24777478 3407bool wxRichTextParagraphLayoutBox::DoGetStyle(long position, wxRichTextAttr& style, bool combineStyles)
5d7836c4 3408{
4e09ebe8 3409 wxRichTextObject* obj wxDUMMY_INITIALIZE(NULL);
e191ee87 3410
5d7836c4 3411 if (style.IsParagraphStyle())
fe5aa22c 3412 {
5d7836c4 3413 obj = GetParagraphAtPosition(position);
fe5aa22c
JS
3414 if (obj)
3415 {
fe5aa22c
JS
3416 if (combineStyles)
3417 {
3418 // Start with the base style
3419 style = GetAttributes();
32423dd8 3420 style.GetTextBoxAttr().Reset();
e191ee87 3421
fe5aa22c
JS
3422 // Apply the paragraph style
3423 wxRichTextApplyStyle(style, obj->GetAttributes());
3424 }
3425 else
3426 style = obj->GetAttributes();
5912d19e 3427
fe5aa22c
JS
3428 return true;
3429 }
5d7836c4
JS
3430 }
3431 else
fe5aa22c
JS
3432 {
3433 obj = GetLeafObjectAtPosition(position);
3434 if (obj)
3435 {
fe5aa22c
JS
3436 if (combineStyles)
3437 {
3438 wxRichTextParagraph* para = wxDynamicCast(obj->GetParent(), wxRichTextParagraph);
3439 style = para ? para->GetCombinedAttributes(obj->GetAttributes()) : obj->GetAttributes();
3440 }
3441 else
3442 style = obj->GetAttributes();
5912d19e 3443
fe5aa22c
JS
3444 return true;
3445 }
fe5aa22c
JS
3446 }
3447 return false;
5d7836c4
JS
3448}
3449
59509217
JS
3450static bool wxHasStyle(long flags, long style)
3451{
3452 return (flags & style) != 0;
3453}
3454
3455/// Combines 'style' with 'currentStyle' for the purpose of summarising the attributes of a range of
3456/// content.
24777478
JS
3457bool wxRichTextParagraphLayoutBox::CollectStyle(wxRichTextAttr& currentStyle, const wxRichTextAttr& style, wxRichTextAttr& clashingAttr, wxRichTextAttr& absentAttr)
3458{
3459 currentStyle.CollectCommonAttributes(style, clashingAttr, absentAttr);
3460
3461 return true;
3462}
3463
3464/// Get the combined style for a range - if any attribute is different within the range,
3465/// that attribute is not present within the flags.
3466/// *** Note that this is not recursive, and so assumes that content inside a paragraph is not itself
3467/// nested.
3468bool wxRichTextParagraphLayoutBox::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style)
59509217 3469{
24777478
JS
3470 style = wxRichTextAttr();
3471
c4168888 3472 wxRichTextAttr clashingAttrPara, clashingAttrChar;
24777478 3473 wxRichTextAttr absentAttrPara, absentAttrChar;
d1e5be0e 3474
24777478
JS
3475 wxRichTextObjectList::compatibility_iterator node = GetChildren().GetFirst();
3476 while (node)
59509217 3477 {
603f702b
JS
3478 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
3479 if (para && !(para->GetRange().GetStart() > range.GetEnd() || para->GetRange().GetEnd() < range.GetStart()))
59509217 3480 {
24777478 3481 if (para->GetChildren().GetCount() == 0)
59509217 3482 {
603f702b 3483 wxRichTextAttr paraStyle = para->GetCombinedAttributes(true /* use box attributes */);
59509217 3484
c4168888 3485 CollectStyle(style, paraStyle, clashingAttrPara, absentAttrPara);
59509217
JS
3486 }
3487 else
3488 {
24777478
JS
3489 wxRichTextRange paraRange(para->GetRange());
3490 paraRange.LimitTo(range);
59509217 3491
24777478
JS
3492 // First collect paragraph attributes only
3493 wxRichTextAttr paraStyle = para->GetCombinedAttributes();
3494 paraStyle.SetFlags(paraStyle.GetFlags() & wxTEXT_ATTR_PARAGRAPH);
c4168888 3495 CollectStyle(style, paraStyle, clashingAttrPara, absentAttrPara);
9c4cb611 3496
24777478
JS
3497 wxRichTextObjectList::compatibility_iterator childNode = para->GetChildren().GetFirst();
3498
3499 while (childNode)
59509217 3500 {
24777478
JS
3501 wxRichTextObject* child = childNode->GetData();
3502 if (!(child->GetRange().GetStart() > range.GetEnd() || child->GetRange().GetEnd() < range.GetStart()))
3503 {
603f702b 3504 wxRichTextAttr childStyle = para->GetCombinedAttributes(child->GetAttributes(), true /* include box attributes */);
59509217 3505
24777478
JS
3506 // Now collect character attributes only
3507 childStyle.SetFlags(childStyle.GetFlags() & wxTEXT_ATTR_CHARACTER);
59509217 3508
c4168888 3509 CollectStyle(style, childStyle, clashingAttrChar, absentAttrChar);
24777478 3510 }
59509217 3511
24777478 3512 childNode = childNode->GetNext();
59509217
JS
3513 }
3514 }
59509217 3515 }
24777478 3516 node = node->GetNext();
59509217 3517 }
24777478
JS
3518 return true;
3519}
59509217 3520
24777478
JS
3521/// Set default style
3522bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxRichTextAttr& style)
3523{
3524 m_defaultAttributes = style;
3525 return true;
3526}
59509217 3527
24777478
JS
3528/// Test if this whole range has character attributes of the specified kind. If any
3529/// of the attributes are different within the range, the test fails. You
3530/// can use this to implement, for example, bold button updating. style must have
3531/// flags indicating which attributes are of interest.
3532bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const
3533{
3534 int foundCount = 0;
3535 int matchingCount = 0;
59509217 3536
24777478
JS
3537 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3538 while (node)
59509217 3539 {
24777478 3540 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 3541 // wxASSERT (para != NULL);
59509217 3542
24777478 3543 if (para)
59509217 3544 {
24777478
JS
3545 // Stop searching if we're beyond the range of interest
3546 if (para->GetRange().GetStart() > range.GetEnd())
3547 return foundCount == matchingCount && foundCount != 0;
59509217 3548
24777478 3549 if (!para->GetRange().IsOutside(range))
59509217 3550 {
24777478 3551 wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst();
59509217 3552
24777478
JS
3553 while (node2)
3554 {
3555 wxRichTextObject* child = node2->GetData();
3556 // Allow for empty string if no buffer
3557 wxRichTextRange childRange = child->GetRange();
3558 if (childRange.GetLength() == 0 && GetRange().GetLength() == 1)
3559 childRange.SetEnd(childRange.GetEnd()+1);
59509217 3560
345c78ca 3561 if (!childRange.IsOutside(range) && wxDynamicCast(child, wxRichTextPlainText))
24777478
JS
3562 {
3563 foundCount ++;
3564 wxRichTextAttr textAttr = para->GetCombinedAttributes(child->GetAttributes());
59509217 3565
32423dd8 3566 if (textAttr.EqPartial(style, false /* strong test - attributes must be valid in both objects */))
24777478
JS
3567 matchingCount ++;
3568 }
59509217 3569
24777478
JS
3570 node2 = node2->GetNext();
3571 }
59509217
JS
3572 }
3573 }
59509217 3574
24777478 3575 node = node->GetNext();
59509217
JS
3576 }
3577
24777478
JS
3578 return foundCount == matchingCount && foundCount != 0;
3579}
59509217 3580
24777478
JS
3581/// Test if this whole range has paragraph attributes of the specified kind. If any
3582/// of the attributes are different within the range, the test fails. You
3583/// can use this to implement, for example, centering button updating. style must have
3584/// flags indicating which attributes are of interest.
3585bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const
3586{
3587 int foundCount = 0;
3588 int matchingCount = 0;
3589
3590 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3591 while (node)
38f833b1 3592 {
24777478 3593 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 3594 // wxASSERT (para != NULL);
24777478
JS
3595
3596 if (para)
38f833b1 3597 {
24777478
JS
3598 // Stop searching if we're beyond the range of interest
3599 if (para->GetRange().GetStart() > range.GetEnd())
3600 return foundCount == matchingCount && foundCount != 0;
3601
3602 if (!para->GetRange().IsOutside(range))
38f833b1 3603 {
24777478
JS
3604 wxRichTextAttr textAttr = GetAttributes();
3605 // Apply the paragraph style
3606 wxRichTextApplyStyle(textAttr, para->GetAttributes());
3607
3608 foundCount ++;
32423dd8 3609 if (textAttr.EqPartial(style, false /* strong test */))
24777478 3610 matchingCount ++;
38f833b1
JS
3611 }
3612 }
24777478
JS
3613
3614 node = node->GetNext();
38f833b1 3615 }
24777478
JS
3616 return foundCount == matchingCount && foundCount != 0;
3617}
5d7836c4 3618
cc2aecde
JS
3619void wxRichTextParagraphLayoutBox::PrepareContent(wxRichTextParagraphLayoutBox& container)
3620{
3621 wxRichTextBuffer* buffer = GetBuffer();
3622 if (buffer && buffer->GetRichTextCtrl())
3623 buffer->GetRichTextCtrl()->PrepareContent(container);
3624}
3625
590a0f8b
JS
3626/// Set character or paragraph properties
3627bool wxRichTextParagraphLayoutBox::SetProperties(const wxRichTextRange& range, const wxRichTextProperties& properties, int flags)
3628{
3629 wxRichTextBuffer* buffer = GetBuffer();
3630
3631 bool withUndo = ((flags & wxRICHTEXT_SETPROPERTIES_WITH_UNDO) != 0);
3632 bool parasOnly = ((flags & wxRICHTEXT_SETPROPERTIES_PARAGRAPHS_ONLY) != 0);
3633 bool charactersOnly = ((flags & wxRICHTEXT_SETPROPERTIES_CHARACTERS_ONLY) != 0);
3634 bool resetExistingProperties = ((flags & wxRICHTEXT_SETPROPERTIES_RESET) != 0);
3635 bool removeProperties = ((flags & wxRICHTEXT_SETPROPERTIES_REMOVE) != 0);
3636
3637 // If we are associated with a control, make undoable; otherwise, apply immediately
3638 // to the data.
3639
3640 bool haveControl = (buffer->GetRichTextCtrl() != NULL);
3641
3642 wxRichTextAction* action = NULL;
3643
3644 if (haveControl && withUndo)
3645 {
3646 action = new wxRichTextAction(NULL, _("Change Properties"), wxRICHTEXT_CHANGE_PROPERTIES, buffer, this, buffer->GetRichTextCtrl());
3647 action->SetRange(range);
3648 action->SetPosition(buffer->GetRichTextCtrl()->GetCaretPosition());
3649 }
3650
3651 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3652 while (node)
3653 {
3654 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
3655 // wxASSERT (para != NULL);
3656
3657 if (para && para->GetChildCount() > 0)
3658 {
3659 // Stop searching if we're beyond the range of interest
3660 if (para->GetRange().GetStart() > range.GetEnd())
3661 break;
3662
3663 if (!para->GetRange().IsOutside(range))
3664 {
3665 // We'll be using a copy of the paragraph to make style changes,
3666 // not updating the buffer directly.
3667 wxRichTextParagraph* newPara wxDUMMY_INITIALIZE(NULL);
3668
3669 if (haveControl && withUndo)
3670 {
3671 newPara = new wxRichTextParagraph(*para);
3672 action->GetNewParagraphs().AppendChild(newPara);
3673
3674 // Also store the old ones for Undo
3675 action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para));
3676 }
3677 else
3678 newPara = para;
3679
3680 if (parasOnly)
3681 {
3682 if (removeProperties)
3683 {
3684 // Removes the given style from the paragraph
3685 // TODO
3686 newPara->GetProperties().RemoveProperties(properties);
3687 }
3688 else if (resetExistingProperties)
3689 newPara->GetProperties() = properties;
3690 else
3691 newPara->GetProperties().MergeProperties(properties);
3692 }
3693
3694 // When applying paragraph styles dynamically, don't change the text objects' attributes
3695 // since they will computed as needed. Only apply the character styling if it's _only_
3696 // character styling. This policy is subject to change and might be put under user control.
3697
3698 // Hm. we might well be applying a mix of paragraph and character styles, in which
3699 // case we _do_ want to apply character styles regardless of what para styles are set.
3700 // But if we're applying a paragraph style, which has some character attributes, but
3701 // we only want the paragraphs to hold this character style, then we _don't_ want to
3702 // apply the character style. So we need to be able to choose.
3703
3704 if (!parasOnly && charactersOnly && range.GetStart() != newPara->GetRange().GetEnd())
3705 {
3706 wxRichTextRange childRange(range);
3707 childRange.LimitTo(newPara->GetRange());
3708
3709 // Find the starting position and if necessary split it so
3710 // we can start applying different properties.
3711 // TODO: check that the properties actually change or are different
3712 // from properties outside of range
3713 wxRichTextObject* firstObject wxDUMMY_INITIALIZE(NULL);
3714 wxRichTextObject* lastObject wxDUMMY_INITIALIZE(NULL);
3715
3716 if (childRange.GetStart() == newPara->GetRange().GetStart())
3717 firstObject = newPara->GetChildren().GetFirst()->GetData();
3718 else
3719 firstObject = newPara->SplitAt(range.GetStart());
3720
3721 // Increment by 1 because we're apply the style one _after_ the split point
3722 long splitPoint = childRange.GetEnd();
3723 if (splitPoint != newPara->GetRange().GetEnd())
3724 splitPoint ++;
3725
3726 // Find last object
3727 if (splitPoint == newPara->GetRange().GetEnd())
3728 lastObject = newPara->GetChildren().GetLast()->GetData();
3729 else
3730 // lastObject is set as a side-effect of splitting. It's
3731 // returned as the object before the new object.
3732 (void) newPara->SplitAt(splitPoint, & lastObject);
3733
3734 wxASSERT(firstObject != NULL);
3735 wxASSERT(lastObject != NULL);
3736
3737 if (!firstObject || !lastObject)
3738 continue;
3739
3740 wxRichTextObjectList::compatibility_iterator firstNode = newPara->GetChildren().Find(firstObject);
3741 wxRichTextObjectList::compatibility_iterator lastNode = newPara->GetChildren().Find(lastObject);
3742
3743 wxASSERT(firstNode);
3744 wxASSERT(lastNode);
3745
3746 wxRichTextObjectList::compatibility_iterator node2 = firstNode;
3747
3748 while (node2)
3749 {
3750 wxRichTextObject* child = node2->GetData();
3751
3752 if (removeProperties)
3753 {
3754 // Removes the given properties from the paragraph
3755 child->GetProperties().RemoveProperties(properties);
3756 }
3757 else if (resetExistingProperties)
3758 child->GetProperties() = properties;
3759 else
3760 {
3761 child->GetProperties().MergeProperties(properties);
3762 }
3763
3764 if (node2 == lastNode)
3765 break;
3766
3767 node2 = node2->GetNext();
3768 }
3769 }
3770 }
3771 }
3772
3773 node = node->GetNext();
3774 }
3775
3776 // Do action, or delay it until end of batch.
3777 if (haveControl && withUndo)
3778 buffer->SubmitAction(action);
3779
3780 return true;
3781}
3782
5d7836c4
JS
3783void wxRichTextParagraphLayoutBox::Reset()
3784{
3785 Clear();
3786
603f702b
JS
3787 wxRichTextBuffer* buffer = GetBuffer();
3788 if (buffer && buffer->GetRichTextCtrl())
cd8ba0d9 3789 {
ce7fe42e 3790 wxRichTextEvent event(wxEVT_RICHTEXT_BUFFER_RESET, buffer->GetRichTextCtrl()->GetId());
603f702b
JS
3791 event.SetEventObject(buffer->GetRichTextCtrl());
3792 event.SetContainer(this);
cd8ba0d9
JS
3793
3794 buffer->SendEvent(event, true);
3795 }
3796
7fe8059f 3797 AddParagraph(wxEmptyString);
3e541562 3798
cc2aecde
JS
3799 PrepareContent(*this);
3800
603f702b 3801 InvalidateHierarchy(wxRICHTEXT_ALL);
5d7836c4
JS
3802}
3803
38113684
JS
3804/// Invalidate the buffer. With no argument, invalidates whole buffer.
3805void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange& invalidRange)
3806{
603f702b 3807 wxRichTextCompositeObject::Invalidate(invalidRange);
39a1c2f2 3808
603f702b
JS
3809 DoInvalidate(invalidRange);
3810}
3811
3812// Do the (in)validation for this object only
3813void wxRichTextParagraphLayoutBox::DoInvalidate(const wxRichTextRange& invalidRange)
3814{
1e967276 3815 if (invalidRange == wxRICHTEXT_ALL)
38113684 3816 {
1e967276 3817 m_invalidRange = wxRICHTEXT_ALL;
38113684 3818 }
1e967276 3819 // Already invalidating everything
603f702b
JS
3820 else if (m_invalidRange == wxRICHTEXT_ALL)
3821 {
3822 }
3823 else
3824 {
3825 if ((invalidRange.GetStart() < m_invalidRange.GetStart()) || m_invalidRange.GetStart() == -1)
3826 m_invalidRange.SetStart(invalidRange.GetStart());
3827 if (invalidRange.GetEnd() > m_invalidRange.GetEnd())
3828 m_invalidRange.SetEnd(invalidRange.GetEnd());
3829 }
3830}
39a1c2f2 3831
603f702b
JS
3832// Do the (in)validation both up and down the hierarchy
3833void wxRichTextParagraphLayoutBox::InvalidateHierarchy(const wxRichTextRange& invalidRange)
3834{
3835 Invalidate(invalidRange);
3836
3837 if (invalidRange != wxRICHTEXT_NONE)
3838 {
3839 // Now go up the hierarchy
3840 wxRichTextObject* thisObj = this;
3841 wxRichTextObject* p = GetParent();
3842 while (p)
3843 {
3844 wxRichTextParagraphLayoutBox* l = wxDynamicCast(p, wxRichTextParagraphLayoutBox);
3845 if (l)
3846 l->DoInvalidate(thisObj->GetRange());
3847
3848 thisObj = p;
3849 p = p->GetParent();
3850 }
3851 }
38113684
JS
3852}
3853
3854/// Get invalid range, rounding to entire paragraphs if argument is true.
3855wxRichTextRange wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs) const
3856{
1e967276 3857 if (m_invalidRange == wxRICHTEXT_ALL || m_invalidRange == wxRICHTEXT_NONE)
38113684 3858 return m_invalidRange;
39a1c2f2 3859
38113684 3860 wxRichTextRange range = m_invalidRange;
39a1c2f2 3861
38113684
JS
3862 if (wholeParagraphs)
3863 {
3864 wxRichTextParagraph* para1 = GetParagraphAtPosition(range.GetStart());
38113684
JS
3865 if (para1)
3866 range.SetStart(para1->GetRange().GetStart());
f7667b84
JS
3867
3868 // FIXME: be more intelligent about this. Check if we have floating objects
3869 // before the end of the range. But it's not clear how we can in general
3870 // tell where it's safe to stop laying out.
3871 // Anyway, this code is central to efficiency when laying in floating mode.
3872 if (!wxRichTextBuffer::GetFloatingLayoutMode())
3873 {
3874 wxRichTextParagraph* para2 = GetParagraphAtPosition(range.GetEnd());
3875 if (para2)
3876 range.SetEnd(para2->GetRange().GetEnd());
3877 }
3878 else
3879 // Floating layout means that all children should be laid out,
3880 // because we can't tell how the whole buffer will be affected.
3881 range.SetEnd(GetOwnRange().GetEnd());
38113684
JS
3882 }
3883 return range;
3884}
3885
fe5aa22c
JS
3886/// Apply the style sheet to the buffer, for example if the styles have changed.
3887bool wxRichTextParagraphLayoutBox::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
3888{
3889 wxASSERT(styleSheet != NULL);
3890 if (!styleSheet)
3891 return false;
3892
3893 int foundCount = 0;
3894
44580804
JS
3895 wxRichTextAttr attr(GetBasicStyle());
3896 if (GetBasicStyle().HasParagraphStyleName())
3897 {
3898 wxRichTextParagraphStyleDefinition* paraDef = styleSheet->FindParagraphStyle(GetBasicStyle().GetParagraphStyleName());
3899 if (paraDef)
3900 {
3901 attr.Apply(paraDef->GetStyleMergedWithBase(styleSheet));
3902 SetBasicStyle(attr);
3903 foundCount ++;
3904 }
3905 }
3906
3907 if (GetBasicStyle().HasCharacterStyleName())
3908 {
3909 wxRichTextCharacterStyleDefinition* charDef = styleSheet->FindCharacterStyle(GetBasicStyle().GetCharacterStyleName());
3910 if (charDef)
3911 {
3912 attr.Apply(charDef->GetStyleMergedWithBase(styleSheet));
3913 SetBasicStyle(attr);
3914 foundCount ++;
3915 }
3916 }
3917
fe5aa22c
JS
3918 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
3919 while (node)
3920 {
3921 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 3922 // wxASSERT (para != NULL);
fe5aa22c
JS
3923
3924 if (para)
3925 {
38f833b1
JS
3926 // Combine paragraph and list styles. If there is a list style in the original attributes,
3927 // the current indentation overrides anything else and is used to find the item indentation.
3928 // Also, for applying paragraph styles, consider having 2 modes: (1) we merge with what we have,
3929 // thereby taking into account all user changes, (2) reset the style completely (except for indentation/list
3930 // exception as above).
3931 // Problem: when changing from one list style to another, there's a danger that the level info will get lost.
3932 // So when changing a list style interactively, could retrieve level based on current style, then
3933 // set appropriate indent and apply new style.
41a85215 3934
bbd55ff9
JS
3935 int outline = -1;
3936 int num = -1;
3937 if (para->GetAttributes().HasOutlineLevel())
3938 outline = para->GetAttributes().GetOutlineLevel();
3939 if (para->GetAttributes().HasBulletNumber())
3940 num = para->GetAttributes().GetBulletNumber();
3941
38f833b1
JS
3942 if (!para->GetAttributes().GetParagraphStyleName().IsEmpty() && !para->GetAttributes().GetListStyleName().IsEmpty())
3943 {
3944 int currentIndent = para->GetAttributes().GetLeftIndent();
3945
3946 wxRichTextParagraphStyleDefinition* paraDef = styleSheet->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
3947 wxRichTextListStyleDefinition* listDef = styleSheet->FindListStyle(para->GetAttributes().GetListStyleName());
3948 if (paraDef && !listDef)
3949 {
336d8ae9 3950 para->GetAttributes() = paraDef->GetStyleMergedWithBase(styleSheet);
38f833b1
JS
3951 foundCount ++;
3952 }
3953 else if (listDef && !paraDef)
3954 {
3955 // Set overall style defined for the list style definition
336d8ae9 3956 para->GetAttributes() = listDef->GetStyleMergedWithBase(styleSheet);
38f833b1
JS
3957
3958 // Apply the style for this level
3959 wxRichTextApplyStyle(para->GetAttributes(), * listDef->GetLevelAttributes(listDef->FindLevelForIndent(currentIndent)));
3960 foundCount ++;
3961 }
3962 else if (listDef && paraDef)
3963 {
3964 // Combines overall list style, style for level, and paragraph style
336d8ae9 3965 para->GetAttributes() = listDef->CombineWithParagraphStyle(currentIndent, paraDef->GetStyleMergedWithBase(styleSheet));
38f833b1
JS
3966 foundCount ++;
3967 }
3968 }
3969 else if (para->GetAttributes().GetParagraphStyleName().IsEmpty() && !para->GetAttributes().GetListStyleName().IsEmpty())
3970 {
3971 int currentIndent = para->GetAttributes().GetLeftIndent();
3972
3973 wxRichTextListStyleDefinition* listDef = styleSheet->FindListStyle(para->GetAttributes().GetListStyleName());
3974
41a85215 3975 // Overall list definition style
336d8ae9 3976 para->GetAttributes() = listDef->GetStyleMergedWithBase(styleSheet);
41a85215 3977
38f833b1
JS
3978 // Style for this level
3979 wxRichTextApplyStyle(para->GetAttributes(), * listDef->GetLevelAttributes(listDef->FindLevelForIndent(currentIndent)));
3980
3981 foundCount ++;
3982 }
3983 else if (!para->GetAttributes().GetParagraphStyleName().IsEmpty() && para->GetAttributes().GetListStyleName().IsEmpty())
fe5aa22c
JS
3984 {
3985 wxRichTextParagraphStyleDefinition* def = styleSheet->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
3986 if (def)
3987 {
336d8ae9 3988 para->GetAttributes() = def->GetStyleMergedWithBase(styleSheet);
fe5aa22c
JS
3989 foundCount ++;
3990 }
3991 }
bbd55ff9
JS
3992
3993 if (outline != -1)
3994 para->GetAttributes().SetOutlineLevel(outline);
3995 if (num != -1)
3996 para->GetAttributes().SetBulletNumber(num);
fe5aa22c
JS
3997 }
3998
3999 node = node->GetNext();
4000 }
4001 return foundCount != 0;
4002}
4003
38f833b1
JS
4004/// Set list style
4005bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
4006{
603f702b
JS
4007 wxRichTextBuffer* buffer = GetBuffer();
4008 wxRichTextStyleSheet* styleSheet = buffer->GetStyleSheet();
3e541562 4009
38f833b1
JS
4010 bool withUndo = ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0);
4011 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
4012 bool specifyLevel = ((flags & wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL) != 0);
4013 bool renumber = ((flags & wxRICHTEXT_SETSTYLE_RENUMBER) != 0);
41a85215 4014
38f833b1
JS
4015 // Current number, if numbering
4016 int n = startFrom;
41a85215 4017
38f833b1
JS
4018 wxASSERT (!specifyLevel || (specifyLevel && (specifiedLevel >= 0)));
4019
4020 // If we are associated with a control, make undoable; otherwise, apply immediately
4021 // to the data.
4022
603f702b 4023 bool haveControl = (buffer->GetRichTextCtrl() != NULL);
38f833b1
JS
4024
4025 wxRichTextAction* action = NULL;
4026
4027 if (haveControl && withUndo)
4028 {
603f702b 4029 action = new wxRichTextAction(NULL, _("Change List Style"), wxRICHTEXT_CHANGE_STYLE, buffer, this, buffer->GetRichTextCtrl());
38f833b1 4030 action->SetRange(range);
603f702b 4031 action->SetPosition(buffer->GetRichTextCtrl()->GetCaretPosition());
38f833b1
JS
4032 }
4033
4034 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
4035 while (node)
4036 {
4037 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 4038 // wxASSERT (para != NULL);
38f833b1
JS
4039
4040 if (para && para->GetChildCount() > 0)
4041 {
4042 // Stop searching if we're beyond the range of interest
4043 if (para->GetRange().GetStart() > range.GetEnd())
4044 break;
4045
4046 if (!para->GetRange().IsOutside(range))
4047 {
4048 // We'll be using a copy of the paragraph to make style changes,
4049 // not updating the buffer directly.
4050 wxRichTextParagraph* newPara wxDUMMY_INITIALIZE(NULL);
4051
4052 if (haveControl && withUndo)
4053 {
4054 newPara = new wxRichTextParagraph(*para);
4055 action->GetNewParagraphs().AppendChild(newPara);
4056
4057 // Also store the old ones for Undo
4058 action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para));
4059 }
4060 else
4061 newPara = para;
41a85215 4062
38f833b1
JS
4063 if (def)
4064 {
4065 int thisIndent = newPara->GetAttributes().GetLeftIndent();
4066 int thisLevel = specifyLevel ? specifiedLevel : def->FindLevelForIndent(thisIndent);
41a85215 4067
38f833b1
JS
4068 // How is numbering going to work?
4069 // If we are renumbering, or numbering for the first time, we need to keep
4070 // track of the number for each level. But we might be simply applying a different
4071 // list style.
4072 // In Word, applying a style to several paragraphs, even if at different levels,
4073 // reverts the level back to the same one. So we could do the same here.
4074 // Renumbering will need to be done when we promote/demote a paragraph.
4075
4076 // Apply the overall list style, and item style for this level
24777478 4077 wxRichTextAttr listStyle(def->GetCombinedStyleForLevel(thisLevel, styleSheet));
38f833b1 4078 wxRichTextApplyStyle(newPara->GetAttributes(), listStyle);
41a85215 4079
d2d0adc7 4080 // Now we need to do numbering
4ce3ebd3
JS
4081 // Preserve the existing list item continuation bullet style, if any
4082 if (para->GetAttributes().HasBulletStyle() && (para->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION))
4083 newPara->GetAttributes().SetBulletStyle(newPara->GetAttributes().GetBulletStyle()|wxTEXT_ATTR_BULLET_STYLE_CONTINUATION);
4084 else
38f833b1 4085 {
4ce3ebd3
JS
4086 if (renumber)
4087 {
4088 newPara->GetAttributes().SetBulletNumber(n);
4089 }
41a85215 4090
4ce3ebd3
JS
4091 n ++;
4092 }
38f833b1
JS
4093 }
4094 else if (!newPara->GetAttributes().GetListStyleName().IsEmpty())
4095 {
4096 // if def is NULL, remove list style, applying any associated paragraph style
4097 // to restore the attributes
4098
4099 newPara->GetAttributes().SetListStyleName(wxEmptyString);
4100 newPara->GetAttributes().SetLeftIndent(0, 0);
d2d0adc7 4101 newPara->GetAttributes().SetBulletText(wxEmptyString);
c4168888 4102 newPara->GetAttributes().SetBulletStyle(0);
41a85215 4103
38f833b1 4104 // Eliminate the main list-related attributes
d2d0adc7 4105 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 4106
38f833b1
JS
4107 if (styleSheet && !newPara->GetAttributes().GetParagraphStyleName().IsEmpty())
4108 {
4109 wxRichTextParagraphStyleDefinition* def = styleSheet->FindParagraphStyle(newPara->GetAttributes().GetParagraphStyleName());
4110 if (def)
4111 {
336d8ae9 4112 newPara->GetAttributes() = def->GetStyleMergedWithBase(styleSheet);
38f833b1
JS
4113 }
4114 }
4115 }
4116 }
4117 }
4118
4119 node = node->GetNext();
4120 }
4121
4122 // Do action, or delay it until end of batch.
4123 if (haveControl && withUndo)
603f702b 4124 buffer->SubmitAction(action);
38f833b1
JS
4125
4126 return true;
4127}
4128
4129bool wxRichTextParagraphLayoutBox::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
4130{
603f702b
JS
4131 wxRichTextBuffer* buffer = GetBuffer();
4132 if (buffer && buffer->GetStyleSheet())
38f833b1 4133 {
603f702b 4134 wxRichTextListStyleDefinition* def = buffer->GetStyleSheet()->FindListStyle(defName);
38f833b1
JS
4135 if (def)
4136 return SetListStyle(range, def, flags, startFrom, specifiedLevel);
4137 }
4138 return false;
4139}
4140
4141/// Clear list for given range
4142bool wxRichTextParagraphLayoutBox::ClearListStyle(const wxRichTextRange& range, int flags)
4143{
4144 return SetListStyle(range, NULL, flags);
4145}
4146
4147/// Number/renumber any list elements in the given range
4148bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
4149{
4150 return DoNumberList(range, range, 0, def, flags, startFrom, specifiedLevel);
4151}
4152
4153/// Number/renumber any list elements in the given range. Also do promotion or demotion of items, if specified
4154bool wxRichTextParagraphLayoutBox::DoNumberList(const wxRichTextRange& range, const wxRichTextRange& promotionRange, int promoteBy,
4155 wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
4156{
603f702b
JS
4157 wxRichTextBuffer* buffer = GetBuffer();
4158 wxRichTextStyleSheet* styleSheet = buffer->GetStyleSheet();
336d8ae9 4159
38f833b1
JS
4160 bool withUndo = ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0);
4161 // bool applyMinimal = ((flags & wxRICHTEXT_SETSTYLE_OPTIMIZE) != 0);
4b6a582b 4162#if wxDEBUG_LEVEL
38f833b1 4163 bool specifyLevel = ((flags & wxRICHTEXT_SETSTYLE_SPECIFY_LEVEL) != 0);
3c738608 4164#endif
38f833b1
JS
4165
4166 bool renumber = ((flags & wxRICHTEXT_SETSTYLE_RENUMBER) != 0);
41a85215 4167
38f833b1
JS
4168 // Max number of levels
4169 const int maxLevels = 10;
41a85215 4170
38f833b1
JS
4171 // The level we're looking at now
4172 int currentLevel = -1;
41a85215 4173
38f833b1
JS
4174 // The item number for each level
4175 int levels[maxLevels];
4176 int i;
41a85215 4177
38f833b1
JS
4178 // Reset all numbering
4179 for (i = 0; i < maxLevels; i++)
4180 {
4181 if (startFrom != -1)
d2d0adc7 4182 levels[i] = startFrom-1;
38f833b1 4183 else if (renumber) // start again
d2d0adc7 4184 levels[i] = 0;
38f833b1
JS
4185 else
4186 levels[i] = -1; // start from the number we found, if any
4187 }
41a85215 4188
bb7bbd12 4189#if wxDEBUG_LEVEL
38f833b1 4190 wxASSERT(!specifyLevel || (specifyLevel && (specifiedLevel >= 0)));
bb7bbd12 4191#endif
38f833b1
JS
4192
4193 // If we are associated with a control, make undoable; otherwise, apply immediately
4194 // to the data.
4195
603f702b 4196 bool haveControl = (buffer->GetRichTextCtrl() != NULL);
38f833b1
JS
4197
4198 wxRichTextAction* action = NULL;
4199
4200 if (haveControl && withUndo)
4201 {
603f702b 4202 action = new wxRichTextAction(NULL, _("Renumber List"), wxRICHTEXT_CHANGE_STYLE, buffer, this, buffer->GetRichTextCtrl());
38f833b1 4203 action->SetRange(range);
603f702b 4204 action->SetPosition(buffer->GetRichTextCtrl()->GetCaretPosition());
38f833b1
JS
4205 }
4206
4207 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
4208 while (node)
4209 {
4210 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
603f702b 4211 // wxASSERT (para != NULL);
38f833b1
JS
4212
4213 if (para && para->GetChildCount() > 0)
4214 {
4215 // Stop searching if we're beyond the range of interest
4216 if (para->GetRange().GetStart() > range.GetEnd())
4217 break;
4218
4219 if (!para->GetRange().IsOutside(range))
4220 {
4221 // We'll be using a copy of the paragraph to make style changes,
4222 // not updating the buffer directly.
4223 wxRichTextParagraph* newPara wxDUMMY_INITIALIZE(NULL);
4224
4225 if (haveControl && withUndo)
4226 {
4227 newPara = new wxRichTextParagraph(*para);
4228 action->GetNewParagraphs().AppendChild(newPara);
4229
4230 // Also store the old ones for Undo
4231 action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para));
4232 }
4233 else
4234 newPara = para;
41a85215 4235
38f833b1
JS
4236 wxRichTextListStyleDefinition* defToUse = def;
4237 if (!defToUse)
4238 {
336d8ae9
VZ
4239 if (styleSheet && !newPara->GetAttributes().GetListStyleName().IsEmpty())
4240 defToUse = styleSheet->FindListStyle(newPara->GetAttributes().GetListStyleName());
38f833b1 4241 }
41a85215 4242
38f833b1
JS
4243 if (defToUse)
4244 {
4245 int thisIndent = newPara->GetAttributes().GetLeftIndent();
4246 int thisLevel = defToUse->FindLevelForIndent(thisIndent);
4247
d2d0adc7
JS
4248 // If we've specified a level to apply to all, change the level.
4249 if (specifiedLevel != -1)
38f833b1 4250 thisLevel = specifiedLevel;
41a85215 4251
38f833b1
JS
4252 // Do promotion if specified
4253 if ((promoteBy != 0) && !para->GetRange().IsOutside(promotionRange))
4254 {
4255 thisLevel = thisLevel - promoteBy;
4256 if (thisLevel < 0)
4257 thisLevel = 0;
4258 if (thisLevel > 9)
4259 thisLevel = 9;
4260 }
41a85215 4261
38f833b1 4262 // Apply the overall list style, and item style for this level
24777478 4263 wxRichTextAttr listStyle(defToUse->GetCombinedStyleForLevel(thisLevel, styleSheet));
38f833b1 4264 wxRichTextApplyStyle(newPara->GetAttributes(), listStyle);
41a85215 4265
4ce3ebd3
JS
4266 // Preserve the existing list item continuation bullet style, if any
4267 if (para->GetAttributes().HasBulletStyle() && (para->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION))
4268 newPara->GetAttributes().SetBulletStyle(newPara->GetAttributes().GetBulletStyle()|wxTEXT_ATTR_BULLET_STYLE_CONTINUATION);
4269
38f833b1 4270 // OK, we've (re)applied the style, now let's get the numbering right.
41a85215 4271
38f833b1
JS
4272 if (currentLevel == -1)
4273 currentLevel = thisLevel;
41a85215 4274
38f833b1
JS
4275 // Same level as before, do nothing except increment level's number afterwards
4276 if (currentLevel == thisLevel)
4277 {
4278 }
4279 // A deeper level: start renumbering all levels after current level
4280 else if (thisLevel > currentLevel)
4281 {
4282 for (i = currentLevel+1; i <= thisLevel; i++)
4283 {
d2d0adc7 4284 levels[i] = 0;
38f833b1
JS
4285 }
4286 currentLevel = thisLevel;
4287 }
4288 else if (thisLevel < currentLevel)
4289 {
4290 currentLevel = thisLevel;
41a85215 4291 }
38f833b1
JS
4292
4293 // Use the current numbering if -1 and we have a bullet number already
4294 if (levels[currentLevel] == -1)
4295 {
4296 if (newPara->GetAttributes().HasBulletNumber())
4297 levels[currentLevel] = newPara->GetAttributes().GetBulletNumber();
4298 else
4299 levels[currentLevel] = 1;
4300 }
d2d0adc7
JS
4301 else
4302 {
4ce3ebd3
JS
4303 if (!(para->GetAttributes().HasBulletStyle() && (para->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION)))
4304 levels[currentLevel] ++;
d2d0adc7 4305 }
41a85215 4306
38f833b1
JS
4307 newPara->GetAttributes().SetBulletNumber(levels[currentLevel]);
4308
d2d0adc7
JS
4309 // Create the bullet text if an outline list
4310 if (listStyle.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE)
4311 {
4312 wxString text;
4313 for (i = 0; i <= currentLevel; i++)
4314 {
4315 if (!text.IsEmpty())
4316 text += wxT(".");
4317 text += wxString::Format(wxT("%d"), levels[i]);
4318 }
4319 newPara->GetAttributes().SetBulletText(text);
4320 }
38f833b1
JS
4321 }
4322 }
4323 }
4324
4325 node = node->GetNext();
4326 }
4327
4328 // Do action, or delay it until end of batch.
4329 if (haveControl && withUndo)
603f702b 4330 buffer->SubmitAction(action);
38f833b1
JS
4331
4332 return true;
4333}
4334
4335bool wxRichTextParagraphLayoutBox::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
4336{
603f702b
JS
4337 wxRichTextBuffer* buffer = GetBuffer();
4338 if (buffer->GetStyleSheet())
38f833b1
JS
4339 {
4340 wxRichTextListStyleDefinition* def = NULL;
4341 if (!defName.IsEmpty())
603f702b 4342 def = buffer->GetStyleSheet()->FindListStyle(defName);
38f833b1
JS
4343 return NumberList(range, def, flags, startFrom, specifiedLevel);
4344 }
4345 return false;
4346}
4347
4348/// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4349bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel)
4350{
4351 // TODO
4352 // One strategy is to first work out the range within which renumbering must occur. Then could pass these two ranges
4353 // to NumberList with a flag indicating promotion is required within one of the ranges.
4354 // Find first and last paragraphs in range. Then for first, calculate new indentation and look back until we find
4355 // a paragraph that either has no list style, or has one that is different or whose indentation is less.
4356 // We start renumbering from the para after that different para we found. We specify that the numbering of that
4357 // list position will start from 1.
4358 // Similarly, we look after the last para in the promote range for an indentation that is less (or no list style).
4359 // We can end the renumbering at this point.
41a85215 4360
38f833b1 4361 // For now, only renumber within the promotion range.
41a85215 4362
38f833b1
JS
4363 return DoNumberList(range, range, promoteBy, def, flags, 1, specifiedLevel);
4364}
4365
4366bool wxRichTextParagraphLayoutBox::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel)
4367{
603f702b
JS
4368 wxRichTextBuffer* buffer = GetBuffer();
4369 if (buffer->GetStyleSheet())
38f833b1
JS
4370 {
4371 wxRichTextListStyleDefinition* def = NULL;
4372 if (!defName.IsEmpty())
603f702b 4373 def = buffer->GetStyleSheet()->FindListStyle(defName);
38f833b1
JS
4374 return PromoteList(promoteBy, range, def, flags, specifiedLevel);
4375 }
4376 return false;
4377}
4378
d2d0adc7
JS
4379/// Fills in the attributes for numbering a paragraph after previousParagraph. It also finds the
4380/// position of the paragraph that it had to start looking from.
24777478 4381bool wxRichTextParagraphLayoutBox::FindNextParagraphNumber(wxRichTextParagraph* previousParagraph, wxRichTextAttr& attr) const
d2d0adc7 4382{
c4168888 4383 // TODO: add GetNextChild/GetPreviousChild to composite
4ce3ebd3
JS
4384 // Search for a paragraph that isn't a continuation paragraph (no bullet)
4385 while (previousParagraph && previousParagraph->GetAttributes().HasBulletStyle() && previousParagraph->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION)
4386 {
4387 wxRichTextObjectList::compatibility_iterator node = ((wxRichTextCompositeObject*) previousParagraph->GetParent())->GetChildren().Find(previousParagraph);
4388 if (node)
4389 {
4390 node = node->GetPrevious();
4391 if (node)
4392 previousParagraph = wxDynamicCast(node->GetData(), wxRichTextParagraph);
4393 else
4394 previousParagraph = NULL;
4395 }
4396 else
4397 previousParagraph = NULL;
4398 }
4399
c4168888 4400 if (!previousParagraph || !previousParagraph->GetAttributes().HasFlag(wxTEXT_ATTR_BULLET_STYLE) || previousParagraph->GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE)
d2d0adc7 4401 return false;
3e541562 4402
603f702b
JS
4403 wxRichTextBuffer* buffer = GetBuffer();
4404 wxRichTextStyleSheet* styleSheet = buffer->GetStyleSheet();
336d8ae9 4405 if (styleSheet && !previousParagraph->GetAttributes().GetListStyleName().IsEmpty())
d2d0adc7 4406 {
336d8ae9 4407 wxRichTextListStyleDefinition* def = styleSheet->FindListStyle(previousParagraph->GetAttributes().GetListStyleName());
d2d0adc7
JS
4408 if (def)
4409 {
4410 // int thisIndent = previousParagraph->GetAttributes().GetLeftIndent();
4411 // int thisLevel = def->FindLevelForIndent(thisIndent);
3e541562 4412
d2d0adc7
JS
4413 bool isOutline = (previousParagraph->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE) != 0;
4414
4415 attr.SetFlags(previousParagraph->GetAttributes().GetFlags() & (wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER|wxTEXT_ATTR_BULLET_TEXT|wxTEXT_ATTR_BULLET_NAME));
4416 if (previousParagraph->GetAttributes().HasBulletName())
4417 attr.SetBulletName(previousParagraph->GetAttributes().GetBulletName());
4418 attr.SetBulletStyle(previousParagraph->GetAttributes().GetBulletStyle());
4419 attr.SetListStyleName(previousParagraph->GetAttributes().GetListStyleName());
3e541562 4420
d2d0adc7
JS
4421 int nextNumber = previousParagraph->GetAttributes().GetBulletNumber() + 1;
4422 attr.SetBulletNumber(nextNumber);
3e541562 4423
d2d0adc7
JS
4424 if (isOutline)
4425 {
4426 wxString text = previousParagraph->GetAttributes().GetBulletText();
4427 if (!text.IsEmpty())
4428 {
4429 int pos = text.Find(wxT('.'), true);
4430 if (pos != wxNOT_FOUND)
4431 {
4432 text = text.Mid(0, text.Length() - pos - 1);
4433 }
4434 else
4435 text = wxEmptyString;
4436 if (!text.IsEmpty())
4437 text += wxT(".");
4438 text += wxString::Format(wxT("%d"), nextNumber);
4439 attr.SetBulletText(text);
4440 }
4441 }
3e541562 4442
d2d0adc7
JS
4443 return true;
4444 }
4445 else
4446 return false;
4447 }
4448 else
4449 return false;
4450}
4451
5d7836c4
JS
4452/*!
4453 * wxRichTextParagraph
4454 * This object represents a single paragraph (or in a straight text editor, a line).
4455 */
4456
603f702b 4457IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph, wxRichTextCompositeObject)
5d7836c4 4458
cfa3b256
JS
4459wxArrayInt wxRichTextParagraph::sm_defaultTabs;
4460
24777478 4461wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject* parent, wxRichTextAttr* style):
603f702b 4462 wxRichTextCompositeObject(parent)
5d7836c4 4463{
5d7836c4
JS
4464 if (style)
4465 SetAttributes(*style);
4466}
4467
24777478 4468wxRichTextParagraph::wxRichTextParagraph(const wxString& text, wxRichTextObject* parent, wxRichTextAttr* paraStyle, wxRichTextAttr* charStyle):
603f702b 4469 wxRichTextCompositeObject(parent)
5d7836c4 4470{
4f32b3cf
JS
4471 if (paraStyle)
4472 SetAttributes(*paraStyle);
5d7836c4 4473
4f32b3cf 4474 AppendChild(new wxRichTextPlainText(text, this, charStyle));
5d7836c4
JS
4475}
4476
4477wxRichTextParagraph::~wxRichTextParagraph()
4478{
4479 ClearLines();
4480}
4481
4482/// Draw the item
8db2e3ef 4483bool wxRichTextParagraph::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int WXUNUSED(descent), int style)
5d7836c4 4484{
603f702b
JS
4485 if (!IsShown())
4486 return true;
4487
4488 // Currently we don't merge these attributes with the parent, but we
4489 // should consider whether we should (e.g. if we set a border colour
4490 // for all paragraphs). But generally box attributes are likely to be
4491 // different for different objects.
4492 wxRect paraRect = GetRect();
24777478 4493 wxRichTextAttr attr = GetCombinedAttributes();
8db2e3ef
JS
4494 context.ApplyVirtualAttributes(attr, this);
4495
4496 DrawBoxAttributes(dc, GetBuffer(), attr, paraRect);
fe5aa22c 4497
5d7836c4 4498 // Draw the bullet, if any
4ce3ebd3 4499 if ((attr.GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE) == 0 && (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION) == 0)
5d7836c4 4500 {
fe5aa22c 4501 if (attr.GetLeftSubIndent() != 0)
5d7836c4 4502 {
fe5aa22c 4503 int spaceBeforePara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingBefore());
fe5aa22c 4504 int leftIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftIndent());
5d7836c4 4505
8db2e3ef 4506 wxRichTextAttr bulletAttr(attr);
d2d0adc7 4507
e3eac0ff
JS
4508 // Combine with the font of the first piece of content, if one is specified
4509 if (GetChildren().GetCount() > 0)
4510 {
4511 wxRichTextObject* firstObj = (wxRichTextObject*) GetChildren().GetFirst()->GetData();
cdaed652 4512 if (!firstObj->IsFloatable() && firstObj->GetAttributes().HasFont())
e3eac0ff
JS
4513 {
4514 wxRichTextApplyStyle(bulletAttr, firstObj->GetAttributes());
4515 }
4516 }
4517
d2d0adc7 4518 // Get line height from first line, if any
d3b9f782 4519 wxRichTextLine* line = m_cachedLines.GetFirst() ? (wxRichTextLine* ) m_cachedLines.GetFirst()->GetData() : NULL;
d2d0adc7
JS
4520
4521 wxPoint linePos;
4522 int lineHeight wxDUMMY_INITIALIZE(0);
4523 if (line)
5d7836c4 4524 {
d2d0adc7
JS
4525 lineHeight = line->GetSize().y;
4526 linePos = line->GetPosition() + GetPosition();
5d7836c4 4527 }
d2d0adc7 4528 else
f089713f 4529 {
f089713f 4530 wxFont font;
44cc96a8
JS
4531 if (bulletAttr.HasFont() && GetBuffer())
4532 font = GetBuffer()->GetFontTable().FindFont(bulletAttr);
f089713f
JS
4533 else
4534 font = (*wxNORMAL_FONT);
4535
ecb5fbf1 4536 wxCheckSetFont(dc, font);
f089713f 4537
d2d0adc7
JS
4538 lineHeight = dc.GetCharHeight();
4539 linePos = GetPosition();
4540 linePos.y += spaceBeforePara;
4541 }
f089713f 4542
d2d0adc7 4543 wxRect bulletRect(GetPosition().x + leftIndent, linePos.y, linePos.x - (GetPosition().x + leftIndent), lineHeight);
f089713f 4544
d2d0adc7
JS
4545 if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP)
4546 {
4547 if (wxRichTextBuffer::GetRenderer())
4548 wxRichTextBuffer::GetRenderer()->DrawBitmapBullet(this, dc, bulletAttr, bulletRect);
4549 }
3e541562
JS
4550 else if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_STANDARD)
4551 {
d2d0adc7
JS
4552 if (wxRichTextBuffer::GetRenderer())
4553 wxRichTextBuffer::GetRenderer()->DrawStandardBullet(this, dc, bulletAttr, bulletRect);
f089713f 4554 }
5d7836c4
JS
4555 else
4556 {
4557 wxString bulletText = GetBulletText();
3e541562 4558
d2d0adc7
JS
4559 if (!bulletText.empty() && wxRichTextBuffer::GetRenderer())
4560 wxRichTextBuffer::GetRenderer()->DrawTextBullet(this, dc, bulletAttr, bulletRect, bulletText);
5d7836c4
JS
4561 }
4562 }
4563 }
7fe8059f 4564
5d7836c4
JS
4565 // Draw the range for each line, one object at a time.
4566
4567 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
4568 while (node)
4569 {
4570 wxRichTextLine* line = node->GetData();
1e967276 4571 wxRichTextRange lineRange = line->GetAbsoluteRange();
5d7836c4 4572
5d7836c4
JS
4573 // Lines are specified relative to the paragraph
4574
4575 wxPoint linePosition = line->GetPosition() + GetPosition();
5d7836c4 4576
7051fa41
JS
4577 // Don't draw if off the screen
4578 if (((style & wxRICHTEXT_DRAW_IGNORE_CACHE) != 0) || ((linePosition.y + line->GetSize().y) >= rect.y && linePosition.y <= rect.y + rect.height))
5d7836c4 4579 {
7051fa41
JS
4580 wxPoint objectPosition = linePosition;
4581 int maxDescent = line->GetDescent();
4582
4583 // Loop through objects until we get to the one within range
4584 wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst();
3e541562 4585
7051fa41
JS
4586 int i = 0;
4587 while (node2)
5d7836c4 4588 {
7051fa41 4589 wxRichTextObject* child = node2->GetData();
5d7836c4 4590
e12b91a3 4591 if ((!child->IsFloating() || !wxRichTextBuffer::GetFloatingLayoutMode()) && child->GetRange().GetLength() > 0 && !child->GetRange().IsOutside(lineRange) && !lineRange.IsOutside(range))
2f45f554 4592 {
7051fa41
JS
4593 // Draw this part of the line at the correct position
4594 wxRichTextRange objectRange(child->GetRange());
4595 objectRange.LimitTo(lineRange);
4596
4597 wxSize objectSize;
603f702b 4598 if (child->IsTopLevel())
7051fa41 4599 {
603f702b
JS
4600 objectSize = child->GetCachedSize();
4601 objectRange = child->GetOwnRange();
7051fa41
JS
4602 }
4603 else
7051fa41 4604 {
603f702b
JS
4605#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING && wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4606 if (i < (int) line->GetObjectSizes().GetCount())
4607 {
4608 objectSize.x = line->GetObjectSizes()[(size_t) i];
4609 }
4610 else
4611#endif
4612 {
4613 int descent = 0;
8db2e3ef 4614 child->GetRangeSize(objectRange, objectSize, descent, dc, context, wxRICHTEXT_UNFORMATTED, objectPosition);
603f702b 4615 }
7051fa41 4616 }
5d7836c4 4617
7051fa41
JS
4618 // Use the child object's width, but the whole line's height
4619 wxRect childRect(objectPosition, wxSize(objectSize.x, line->GetSize().y));
8db2e3ef 4620 child->Draw(dc, context, objectRange, selection, childRect, maxDescent, style);
5d7836c4 4621
7051fa41
JS
4622 objectPosition.x += objectSize.x;
4623 i ++;
4624 }
4625 else if (child->GetRange().GetStart() > lineRange.GetEnd())
4626 // Can break out of inner loop now since we've passed this line's range
4627 break;
5d7836c4 4628
7051fa41
JS
4629 node2 = node2->GetNext();
4630 }
5d7836c4
JS
4631 }
4632
4633 node = node->GetNext();
7fe8059f 4634 }
5d7836c4
JS
4635
4636 return true;
4637}
4638
4f3d5bc0
JS
4639// Get the range width using partial extents calculated for the whole paragraph.
4640static int wxRichTextGetRangeWidth(const wxRichTextParagraph& para, const wxRichTextRange& range, const wxArrayInt& partialExtents)
4641{
4642 wxASSERT(partialExtents.GetCount() >= (size_t) range.GetLength());
4643
affbfa1f
JS
4644 if (partialExtents.GetCount() < (size_t) range.GetLength())
4645 return 0;
4646
4f3d5bc0
JS
4647 int leftMostPos = 0;
4648 if (range.GetStart() - para.GetRange().GetStart() > 0)
4649 leftMostPos = partialExtents[range.GetStart() - para.GetRange().GetStart() - 1];
4650
4651 int rightMostPos = partialExtents[range.GetEnd() - para.GetRange().GetStart()];
4652
4653 int w = rightMostPos - leftMostPos;
4654
4655 return w;
4656}
4657
5d7836c4 4658/// Lay the item out
8db2e3ef 4659bool wxRichTextParagraph::Layout(wxDC& dc, wxRichTextDrawingContext& context, const wxRect& rect, const wxRect& parentRect, int style)
5d7836c4 4660{
cdaed652
VZ
4661 // Deal with floating objects firstly before the normal layout
4662 wxRichTextBuffer* buffer = GetBuffer();
4663 wxASSERT(buffer);
e12b91a3 4664
07d4142f 4665 wxRichTextFloatCollector* collector = GetContainer()->GetFloatCollector();
e12b91a3
JS
4666
4667 if (wxRichTextBuffer::GetFloatingLayoutMode())
4668 {
4669 wxASSERT(collector != NULL);
4670 if (collector)
4671 LayoutFloat(dc, context, rect, parentRect, style, collector);
4672 }
cdaed652 4673
24777478 4674 wxRichTextAttr attr = GetCombinedAttributes();
8db2e3ef 4675 context.ApplyVirtualAttributes(attr, this);
fe5aa22c 4676
169adfa9
JS
4677 // ClearLines();
4678
5d7836c4 4679 // Increase the size of the paragraph due to spacing
fe5aa22c
JS
4680 int spaceBeforePara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingBefore());
4681 int spaceAfterPara = ConvertTenthsMMToPixels(dc, attr.GetParagraphSpacingAfter());
4682 int leftIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftIndent());
4683 int leftSubIndent = ConvertTenthsMMToPixels(dc, attr.GetLeftSubIndent());
4684 int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent());
5d7836c4
JS
4685
4686 int lineSpacing = 0;
4687
4688 // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc.
77120d82 4689 if (attr.HasLineSpacing() && attr.GetLineSpacing() > 0 && attr.HasFont())
5d7836c4 4690 {
77120d82
JS
4691 wxFont font(buffer->GetFontTable().FindFont(attr));
4692 if (font.IsOk())
4693 {
4694 wxCheckSetFont(dc, font);
4695 lineSpacing = (int) (double(dc.GetCharHeight()) * (double(attr.GetLineSpacing())/10.0 - 1.0));
4696 }
5d7836c4
JS
4697 }
4698
5d7836c4
JS
4699 // Start position for each line relative to the paragraph
4700 int startPositionFirstLine = leftIndent;
4701 int startPositionSubsequentLines = leftIndent + leftSubIndent;
4702
4703 // If we have a bullet in this paragraph, the start position for the first line's text
4704 // is actually leftIndent + leftSubIndent.
fe5aa22c 4705 if (attr.GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE)
5d7836c4
JS
4706 startPositionFirstLine = startPositionSubsequentLines;
4707
5d7836c4
JS
4708 long lastEndPos = GetRange().GetStart()-1;
4709 long lastCompletedEndPos = lastEndPos;
4710
4711 int currentWidth = 0;
4712 SetPosition(rect.GetPosition());
4713
4714 wxPoint currentPosition(0, spaceBeforePara); // We will calculate lines relative to paragraph
4715 int lineHeight = 0;
4716 int maxWidth = 0;
603f702b 4717 int maxHeight = currentPosition.y;
476a319a 4718 int maxAscent = 0;
5d7836c4 4719 int maxDescent = 0;
5d7836c4 4720 int lineCount = 0;
cdaed652
VZ
4721 int lineAscent = 0;
4722 int lineDescent = 0;
5d7836c4 4723
2f45f554
JS
4724 wxRichTextObjectList::compatibility_iterator node;
4725
4726#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4727 wxUnusedVar(style);
4728 wxArrayInt partialExtents;
4729
4730 wxSize paraSize;
8aab23a1 4731 int paraDescent = 0;
2f45f554
JS
4732
4733 // This calculates the partial text extents
914a4e23 4734 GetRangeSize(GetRange(), paraSize, paraDescent, dc, context, wxRICHTEXT_UNFORMATTED|wxRICHTEXT_CACHE_SIZE, rect.GetPosition(), parentRect.GetSize(), & partialExtents);
2f45f554
JS
4735#else
4736 node = m_children.GetFirst();
ecb5fbf1
JS
4737 while (node)
4738 {
4739 wxRichTextObject* child = node->GetData();
4740
603f702b 4741 //child->SetCachedSize(wxDefaultSize);
8db2e3ef 4742 child->Layout(dc, context, rect, style);
ecb5fbf1
JS
4743
4744 node = node->GetNext();
4745 }
31778480
JS
4746#endif
4747
5d7836c4
JS
4748 // Split up lines
4749
4750 // We may need to go back to a previous child, in which case create the new line,
4751 // find the child corresponding to the start position of the string, and
4752 // continue.
4753
603f702b
JS
4754 wxRect availableRect;
4755
ecb5fbf1 4756 node = m_children.GetFirst();
5d7836c4
JS
4757 while (node)
4758 {
4759 wxRichTextObject* child = node->GetData();
4760
cdaed652 4761 // If floating, ignore. We already laid out floats.
603f702b
JS
4762 // Also ignore if empty object, except if we haven't got any
4763 // size yet.
e12b91a3
JS
4764 if ((child->IsFloating() && wxRichTextBuffer::GetFloatingLayoutMode())
4765 || !child->IsShown() || (child->GetRange().GetLength() == 0 && maxHeight > spaceBeforePara)
603f702b 4766 )
affbfa1f
JS
4767 {
4768 node = node->GetNext();
4769 continue;
4770 }
4771
5d7836c4
JS
4772 // If this is e.g. a composite text box, it will need to be laid out itself.
4773 // But if just a text fragment or image, for example, this will
4774 // do nothing. NB: won't we need to set the position after layout?
4775 // since for example if position is dependent on vertical line size, we
4776 // can't tell the position until the size is determined. So possibly introduce
4777 // another layout phase.
4778
5d7836c4
JS
4779 // We may only be looking at part of a child, if we searched back for wrapping
4780 // and found a suitable point some way into the child. So get the size for the fragment
4781 // if necessary.
3e541562 4782
ff76711f
JS
4783 long nextBreakPos = GetFirstLineBreakPosition(lastEndPos+1);
4784 long lastPosToUse = child->GetRange().GetEnd();
4785 bool lineBreakInThisObject = (nextBreakPos > -1 && nextBreakPos <= child->GetRange().GetEnd());
3e541562 4786
ff76711f
JS
4787 if (lineBreakInThisObject)
4788 lastPosToUse = nextBreakPos;
5d7836c4
JS
4789
4790 wxSize childSize;
4791 int childDescent = 0;
3e541562 4792
603f702b
JS
4793 int startOffset = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines);
4794 availableRect = wxRect(rect.x + startOffset, rect.y + currentPosition.y,
4795 rect.width - startOffset - rightIndent, rect.height);
4796
4797 if (child->IsTopLevel())
4798 {
4799 wxSize oldSize = child->GetCachedSize();
4800
4801 child->Invalidate(wxRICHTEXT_ALL);
4802 child->SetPosition(wxPoint(0, 0));
4803
4804 // Lays out the object first with a given amount of space, and then if no width was specified in attr,
4805 // lays out the object again using the minimum size
4806 // The position will be determined by its location in its line,
4807 // and not by the child's actual position.
8db2e3ef
JS
4808 child->LayoutToBestSize(dc, context, buffer,
4809 attr, child->GetAttributes(), availableRect, parentRect, style);
603f702b
JS
4810
4811 if (oldSize != child->GetCachedSize())
4812 {
4813 partialExtents.Clear();
4814
4815 // Recalculate the partial text extents since the child object changed size
914a4e23 4816 GetRangeSize(GetRange(), paraSize, paraDescent, dc, context, wxRICHTEXT_UNFORMATTED|wxRICHTEXT_CACHE_SIZE, wxPoint(0,0), parentRect.GetSize(), & partialExtents);
603f702b
JS
4817 }
4818 }
4819
4820 // Problem: we need to layout composites here for which we need the available width,
4821 // but we can't get the available width without using the float collector which
4822 // needs to know the object height.
4823
ff76711f 4824 if ((nextBreakPos == -1) && (lastEndPos == child->GetRange().GetStart() - 1)) // i.e. we want to get the whole thing
5d7836c4
JS
4825 {
4826 childSize = child->GetCachedSize();
4827 childDescent = child->GetDescent();
4828 }
4829 else
4f3d5bc0
JS
4830 {
4831#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
4832 // Get height only, then the width using the partial extents
914a4e23 4833 GetRangeSize(wxRichTextRange(lastEndPos+1, lastPosToUse), childSize, childDescent, dc, context, wxRICHTEXT_UNFORMATTED|wxRICHTEXT_HEIGHT_ONLY, wxPoint(0,0), parentRect.GetSize());
4f3d5bc0
JS
4834 childSize.x = wxRichTextGetRangeWidth(*this, wxRichTextRange(lastEndPos+1, lastPosToUse), partialExtents);
4835#else
914a4e23 4836 GetRangeSize(wxRichTextRange(lastEndPos+1, lastPosToUse), childSize, childDescent, dc, context, wxRICHTEXT_UNFORMATTED, rect.GetPosition(), parentRect.GetSize());
4f3d5bc0
JS
4837#endif
4838 }
ff76711f 4839
603f702b
JS
4840 bool doLoop = true;
4841 int loopIterations = 0;
4842
4843 // If there are nested objects that need to lay themselves out, we have to do this in a
4844 // loop because the height of the object may well depend on the available width.
4845 // And because of floating object positioning, the available width depends on the
4846 // height of the object and whether it will clash with the floating objects.
4847 // So, we see whether the available width changes due to the presence of floating images.
4848 // If it does, then we'll use the new restricted width to find the object height again.
4849 // If this causes another restriction in the available width, we'll try again, until
4850 // either we lose patience or the available width settles down.
4851 do
4852 {
4853 loopIterations ++;
4854
4855 wxRect oldAvailableRect = availableRect;
4856
4857 // Available width depends on the floating objects and the line height.
7c9fdebe 4858 // Note: the floating objects may be placed vertically along the two sides of
603f702b
JS
4859 // buffer, so we may have different available line widths with different
4860 // [startY, endY]. So, we can't determine how wide the available
4861 // space is until we know the exact line height.
a70eb13e
JS
4862 if (childDescent == 0)
4863 {
4864 lineHeight = wxMax(lineHeight, childSize.y);
4865 lineDescent = maxDescent;
4866 lineAscent = maxAscent;
4867 }
4868 else
4869 {
4870 lineDescent = wxMax(childDescent, maxDescent);
4871 lineAscent = wxMax(childSize.y-childDescent, maxAscent);
4872 }
4873 lineHeight = wxMax(lineHeight, (lineDescent + lineAscent));
603f702b 4874
e12b91a3 4875 if (wxRichTextBuffer::GetFloatingLayoutMode() && collector)
603f702b 4876 {
e12b91a3
JS
4877 wxRect floatAvailableRect = collector->GetAvailableRect(rect.y + currentPosition.y, rect.y + currentPosition.y + lineHeight);
4878
4879 // Adjust availableRect to the space that is available when taking floating objects into account.
4880
4881 if (floatAvailableRect.x + startOffset > availableRect.x)
4882 {
4883 int newX = floatAvailableRect.x + startOffset;
4884 int newW = availableRect.width - (newX - availableRect.x);
4885 availableRect.x = newX;
4886 availableRect.width = newW;
4887 }
603f702b 4888
e12b91a3
JS
4889 if (floatAvailableRect.width < availableRect.width)
4890 availableRect.width = floatAvailableRect.width;
4891 }
603f702b
JS
4892
4893 currentPosition.x = availableRect.x - rect.x;
4894
4895 if (child->IsTopLevel() && loopIterations <= 20)
4896 {
4897 if (availableRect != oldAvailableRect)
4898 {
4899 wxSize oldSize = child->GetCachedSize();
4900
603f702b
JS
4901 // Lays out the object first with a given amount of space, and then if no width was specified in attr,
4902 // lays out the object again using the minimum size
4903 child->Invalidate(wxRICHTEXT_ALL);
8db2e3ef 4904 child->LayoutToBestSize(dc, context, buffer,
914a4e23 4905 attr, child->GetAttributes(), availableRect, parentRect.GetSize(), style);
603f702b
JS
4906 childSize = child->GetCachedSize();
4907 childDescent = child->GetDescent();
603f702b
JS
4908
4909 if (oldSize != child->GetCachedSize())
4910 {
4911 partialExtents.Clear();
4912
4913 // Recalculate the partial text extents since the child object changed size
914a4e23 4914 GetRangeSize(GetRange(), paraSize, paraDescent, dc, context, wxRICHTEXT_UNFORMATTED|wxRICHTEXT_CACHE_SIZE, wxPoint(0,0), parentRect.GetSize(), & partialExtents);
603f702b 4915 }
cdaed652 4916
603f702b
JS
4917 // Go around the loop finding the available rect for the given floating objects
4918 }
4919 else
4920 doLoop = false;
4921 }
4922 else
4923 doLoop = false;
4924 }
4925 while (doLoop);
cdaed652 4926
20d09da5
JS
4927 if (child->IsTopLevel())
4928 {
4929 // We can move it to the correct position at this point
32423dd8
JS
4930 // TODO: probably need to add margin
4931 child->Move(GetPosition() + wxPoint(currentWidth + (wxMax(leftIndent, leftIndent + leftSubIndent)), currentPosition.y));
20d09da5
JS
4932 }
4933
ff76711f
JS
4934 // Cases:
4935 // 1) There was a line break BEFORE the natural break
4936 // 2) There was a line break AFTER the natural break
603f702b
JS
4937 // 3) It's the last line
4938 // 4) The child still fits (carry on) - 'else' clause
5d7836c4 4939
603f702b
JS
4940 if ((lineBreakInThisObject && (childSize.x + currentWidth <= availableRect.width))
4941 ||
4942 (childSize.x + currentWidth > availableRect.width)
914a4e23 4943#if 0
603f702b
JS
4944 ||
4945 ((childSize.x + currentWidth <= availableRect.width) && !node->GetNext())
914a4e23 4946#endif
603f702b 4947 )
5d7836c4
JS
4948 {
4949 long wrapPosition = 0;
603f702b
JS
4950 if ((childSize.x + currentWidth <= availableRect.width) && !node->GetNext() && !lineBreakInThisObject)
4951 wrapPosition = child->GetRange().GetEnd();
4952 else
5d7836c4
JS
4953
4954 // Find a place to wrap. This may walk back to previous children,
4955 // for example if a word spans several objects.
cdaed652
VZ
4956 // Note: one object must contains only one wxTextAtrr, so the line height will not
4957 // change inside one object. Thus, we can pass the remain line width to the
4958 // FindWrapPosition function.
8db2e3ef 4959 if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos+1, child->GetRange().GetEnd()), dc, context, availableRect.width, wrapPosition, & partialExtents))
5d7836c4
JS
4960 {
4961 // If the function failed, just cut it off at the end of this child.
4962 wrapPosition = child->GetRange().GetEnd();
4963 }
4964
4965 // FindWrapPosition can still return a value that will put us in an endless wrapping loop
4966 if (wrapPosition <= lastCompletedEndPos)
4967 wrapPosition = wxMax(lastCompletedEndPos+1,child->GetRange().GetEnd());
4968
603f702b
JS
4969 // Line end position shouldn't be the same as the end, or greater.
4970 if (wrapPosition >= GetRange().GetEnd())
a8a15de6 4971 wrapPosition = wxMax(0, GetRange().GetEnd()-1);
603f702b 4972
5d7836c4 4973 // wxLogDebug(wxT("Split at %ld"), wrapPosition);
7fe8059f 4974
5d7836c4
JS
4975 // Let's find the actual size of the current line now
4976 wxSize actualSize;
4977 wxRichTextRange actualRange(lastCompletedEndPos+1, wrapPosition);
4f3d5bc0 4978
a70eb13e 4979 childDescent = 0;
4ab8a5e2 4980
4f3d5bc0 4981#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
603f702b
JS
4982 if (!child->IsEmpty())
4983 {
4984 // Get height only, then the width using the partial extents
914a4e23 4985 GetRangeSize(actualRange, actualSize, childDescent, dc, context, wxRICHTEXT_UNFORMATTED|wxRICHTEXT_HEIGHT_ONLY, wxPoint(0,0), parentRect.GetSize());
603f702b
JS
4986 actualSize.x = wxRichTextGetRangeWidth(*this, actualRange, partialExtents);
4987 }
4988 else
4f3d5bc0 4989#endif
914a4e23 4990 GetRangeSize(actualRange, actualSize, childDescent, dc, context, wxRICHTEXT_UNFORMATTED, wxPoint(0,0), parentRect.GetSize());
4f3d5bc0 4991
5d7836c4 4992 currentWidth = actualSize.x;
a70eb13e
JS
4993
4994 // The descent for the whole line at this point, is the correct max descent
4995 maxDescent = childDescent;
4996 // Maximum ascent
4997 maxAscent = actualSize.y-childDescent;
4998
4999 // lineHeight is given by the height for the whole line, since it will
5000 // take into account ascend/descend.
5001 lineHeight = actualSize.y;
7fe8059f 5002
07d4142f 5003 if (lineHeight == 0 && buffer)
603f702b 5004 {
07d4142f 5005 wxFont font(buffer->GetFontTable().FindFont(attr));
603f702b
JS
5006 wxCheckSetFont(dc, font);
5007 lineHeight = dc.GetCharHeight();
5008 }
5009
5010 if (maxDescent == 0)
5011 {
5012 int w, h;
5013 dc.GetTextExtent(wxT("X"), & w, &h, & maxDescent);
5014 }
5015
5d7836c4 5016 // Add a new line
1e967276 5017 wxRichTextLine* line = AllocateLine(lineCount);
39a1c2f2 5018
1e967276
JS
5019 // Set relative range so we won't have to change line ranges when paragraphs are moved
5020 line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart()));
5d7836c4
JS
5021 line->SetPosition(currentPosition);
5022 line->SetSize(wxSize(currentWidth, lineHeight));
5023 line->SetDescent(maxDescent);
5024
603f702b
JS
5025 maxHeight = currentPosition.y + lineHeight;
5026
5d7836c4
JS
5027 // Now move down a line. TODO: add margins, spacing
5028 currentPosition.y += lineHeight;
5029 currentPosition.y += lineSpacing;
5d7836c4 5030 maxDescent = 0;
476a319a 5031 maxAscent = 0;
603f702b
JS
5032 maxWidth = wxMax(maxWidth, currentWidth+startOffset);
5033 currentWidth = 0;
7fe8059f 5034
5d7836c4
JS
5035 lineCount ++;
5036
a70eb13e 5037 // TODO: account for zero-length objects
603f702b 5038 // wxASSERT(wrapPosition > lastCompletedEndPos);
7fe8059f 5039
5d7836c4
JS
5040 lastEndPos = wrapPosition;
5041 lastCompletedEndPos = lastEndPos;
5042
5043 lineHeight = 0;
5044
603f702b
JS
5045 if (wrapPosition < GetRange().GetEnd()-1)
5046 {
5047 // May need to set the node back to a previous one, due to searching back in wrapping
5048 wxRichTextObject* childAfterWrapPosition = FindObjectAtPosition(wrapPosition+1);
5049 if (childAfterWrapPosition)
5050 node = m_children.Find(childAfterWrapPosition);
5051 else
5052 node = node->GetNext();
5053 }
5d7836c4
JS
5054 else
5055 node = node->GetNext();
603f702b
JS
5056
5057 // Apply paragraph styles such as alignment to the wrapped line
5058 ApplyParagraphStyle(line, attr, availableRect, dc);
5d7836c4
JS
5059 }
5060 else
5061 {
5062 // We still fit, so don't add a line, and keep going
5063 currentWidth += childSize.x;
a70eb13e
JS
5064
5065 if (childDescent == 0)
5066 {
5067 // An object with a zero descend value wants to take up the whole
5068 // height regardless of baseline
5069 lineHeight = wxMax(lineHeight, childSize.y);
5070 }
5071 else
5072 {
5073 maxDescent = wxMax(childDescent, maxDescent);
5074 maxAscent = wxMax(childSize.y-childDescent, maxAscent);
5075 }
5076
5077 lineHeight = wxMax(lineHeight, (maxDescent + maxAscent));
5d7836c4 5078
603f702b 5079 maxWidth = wxMax(maxWidth, currentWidth+startOffset);
5d7836c4
JS
5080 lastEndPos = child->GetRange().GetEnd();
5081
5082 node = node->GetNext();
5083 }
5084 }
5085
07d4142f 5086 //wxASSERT(!(lastCompletedEndPos != -1 && lastCompletedEndPos < GetRange().GetEnd()-1));
5d7836c4 5087
914a4e23
JS
5088 // Add the last line - it's the current pos -> last para pos
5089 // Substract -1 because the last position is always the end-paragraph position.
5090 if (lastCompletedEndPos <= GetRange().GetEnd()-1)
5091 {
5092 currentPosition.x = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines);
5093
5094 wxRichTextLine* line = AllocateLine(lineCount);
5095
5096 wxRichTextRange actualRange(lastCompletedEndPos+1, GetRange().GetEnd()-1);
5097
5098 // Set relative range so we won't have to change line ranges when paragraphs are moved
5099 line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart()));
5100
5101 line->SetPosition(currentPosition);
5102
5103 if (lineHeight == 0 && buffer)
5104 {
5105 wxFont font(buffer->GetFontTable().FindFont(attr));
5106 wxCheckSetFont(dc, font);
5107 lineHeight = dc.GetCharHeight();
5108 }
5109
5110 if (maxDescent == 0)
5111 {
5112 int w, h;
5113 dc.GetTextExtent(wxT("X"), & w, &h, & maxDescent);
5114 }
5115
5116 line->SetSize(wxSize(currentWidth, lineHeight));
5117 line->SetDescent(maxDescent);
5118 currentPosition.y += lineHeight;
5119 currentPosition.y += lineSpacing;
5120 lineCount ++;
1bdb8131
JS
5121
5122 // Apply paragraph styles such as alignment to the wrapped line
5123 ApplyParagraphStyle(line, attr, availableRect, dc);
914a4e23
JS
5124 }
5125
1e967276
JS
5126 // Remove remaining unused line objects, if any
5127 ClearUnusedLines(lineCount);
5128
603f702b
JS
5129 // We need to add back the margins etc.
5130 {
5131 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
5132 contentRect = wxRect(wxPoint(0, 0), wxSize(maxWidth, currentPosition.y + spaceAfterPara));
8db2e3ef 5133 GetBoxRects(dc, buffer, attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
5134 SetCachedSize(marginRect.GetSize());
5135 }
5136
5137 // The maximum size is the length of the paragraph stretched out into a line.
5138 // So if there were a single word, or an image, or a fixed-size text box, the object could be shrunk around
5139 // this size. TODO: take into account line breaks.
5140 {
5141 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
031b5b0c 5142 contentRect = wxRect(wxPoint(0, 0), wxSize(paraSize.x + wxMax(leftIndent, leftIndent + leftSubIndent) + rightIndent, currentPosition.y + spaceAfterPara));
8db2e3ef 5143 GetBoxRects(dc, buffer, attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
5144 SetMaxSize(marginRect.GetSize());
5145 }
5146
5147 // Find the greatest minimum size. Currently we only look at non-text objects,
5148 // which isn't ideal but it would be slow to find the maximum word width to
5149 // use as the minimum.
5150 {
5151 int minWidth = 0;
5152 node = m_children.GetFirst();
5153 while (node)
5154 {
5155 wxRichTextObject* child = node->GetData();
5156
5157 // If floating, ignore. We already laid out floats.
5158 // Also ignore if empty object, except if we haven't got any
5159 // size yet.
e12b91a3 5160 if ((!child->IsFloating() || !wxRichTextBuffer::GetFloatingLayoutMode()) && child->GetRange().GetLength() != 0 && !wxDynamicCast(child, wxRichTextPlainText))
603f702b
JS
5161 {
5162 if (child->GetCachedSize().x > minWidth)
5163 minWidth = child->GetMinSize().x;
5164 }
5165 node = node->GetNext();
5166 }
5d7836c4 5167
603f702b
JS
5168 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
5169 contentRect = wxRect(wxPoint(0, 0), wxSize(minWidth, currentPosition.y + spaceAfterPara));
8db2e3ef 5170 GetBoxRects(dc, buffer, attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
5171 SetMinSize(marginRect.GetSize());
5172 }
5d7836c4 5173
2f45f554
JS
5174#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
5175#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
5176 // Use the text extents to calculate the size of each fragment in each line
5177 wxRichTextLineList::compatibility_iterator lineNode = m_cachedLines.GetFirst();
5178 while (lineNode)
5179 {
5180 wxRichTextLine* line = lineNode->GetData();
5181 wxRichTextRange lineRange = line->GetAbsoluteRange();
5182
5183 // Loop through objects until we get to the one within range
5184 wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst();
5185
5186 while (node2)
5187 {
5188 wxRichTextObject* child = node2->GetData();
5189
affbfa1f 5190 if (child->GetRange().GetLength() > 0 && !child->GetRange().IsOutside(lineRange))
2f45f554
JS
5191 {
5192 wxRichTextRange rangeToUse = lineRange;
5193 rangeToUse.LimitTo(child->GetRange());
5194
5195 // Find the size of the child from the text extents, and store in an array
5196 // for drawing later
5197 int left = 0;
5198 if (rangeToUse.GetStart() > GetRange().GetStart())
5199 left = partialExtents[(rangeToUse.GetStart()-1) - GetRange().GetStart()];
5200 int right = partialExtents[rangeToUse.GetEnd() - GetRange().GetStart()];
5201 int sz = right - left;
5202 line->GetObjectSizes().Add(sz);
5203 }
5204 else if (child->GetRange().GetStart() > lineRange.GetEnd())
5205 // Can break out of inner loop now since we've passed this line's range
5206 break;
5207
5208 node2 = node2->GetNext();
5209 }
5210
5211 lineNode = lineNode->GetNext();
5212 }
5213#endif
5214#endif
5215
5d7836c4
JS
5216 return true;
5217}
5218
603f702b
JS
5219/// Apply paragraph styles, such as centering, to wrapped lines
5220/// TODO: take into account box attributes, possibly
5221void wxRichTextParagraph::ApplyParagraphStyle(wxRichTextLine* line, const wxRichTextAttr& attr, const wxRect& rect, wxDC& dc)
5222{
5223 if (!attr.HasAlignment())
5224 return;
5225
5226 wxPoint pos = line->GetPosition();
32423dd8 5227 wxPoint originalPos = pos;
603f702b
JS
5228 wxSize size = line->GetSize();
5229
5230 // centering, right-justification
8db2e3ef 5231 if (attr.HasAlignment() && attr.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
603f702b
JS
5232 {
5233 int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent());
5234 pos.x = (rect.GetWidth() - rightIndent - size.x)/2 + pos.x;
5235 line->SetPosition(pos);
5236 }
8db2e3ef 5237 else if (attr.HasAlignment() && attr.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
603f702b
JS
5238 {
5239 int rightIndent = ConvertTenthsMMToPixels(dc, attr.GetRightIndent());
5240 pos.x = pos.x + rect.GetWidth() - size.x - rightIndent;
5241 line->SetPosition(pos);
5242 }
32423dd8
JS
5243
5244 if (pos != originalPos)
5245 {
5246 wxPoint inc = pos - originalPos;
5247
5248 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
5249
5250 while (node)
5251 {
5252 wxRichTextObject* child = node->GetData();
5253 if (child->IsTopLevel() && !child->GetRange().IsOutside(line->GetAbsoluteRange()))
5254 child->Move(child->GetPosition() + inc);
5255
5256 node = node->GetNext();
5257 }
5258 }
603f702b 5259}
5d7836c4
JS
5260
5261/// Insert text at the given position
5262bool wxRichTextParagraph::InsertText(long pos, const wxString& text)
5263{
5264 wxRichTextObject* childToUse = NULL;
09f14108 5265 wxRichTextObjectList::compatibility_iterator nodeToUse = wxRichTextObjectList::compatibility_iterator();
5d7836c4
JS
5266
5267 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
5268 while (node)
5269 {
5270 wxRichTextObject* child = node->GetData();
5271 if (child->GetRange().Contains(pos) && child->GetRange().GetLength() > 0)
5272 {
5273 childToUse = child;
5274 nodeToUse = node;
5275 break;
5276 }
5277
5278 node = node->GetNext();
5279 }
5280
5281 if (childToUse)
5282 {
5283 wxRichTextPlainText* textObject = wxDynamicCast(childToUse, wxRichTextPlainText);
5284 if (textObject)
5285 {
5286 int posInString = pos - textObject->GetRange().GetStart();
5287
5288 wxString newText = textObject->GetText().Mid(0, posInString) +
5289 text + textObject->GetText().Mid(posInString);
5290 textObject->SetText(newText);
5291
28f92d74 5292 int textLength = text.length();
5d7836c4
JS
5293
5294 textObject->SetRange(wxRichTextRange(textObject->GetRange().GetStart(),
5295 textObject->GetRange().GetEnd() + textLength));
5296
5297 // Increment the end range of subsequent fragments in this paragraph.
5298 // We'll set the paragraph range itself at a higher level.
5299
5300 wxRichTextObjectList::compatibility_iterator node = nodeToUse->GetNext();
5301 while (node)
5302 {
5303 wxRichTextObject* child = node->GetData();
5304 child->SetRange(wxRichTextRange(textObject->GetRange().GetStart() + textLength,
5305 textObject->GetRange().GetEnd() + textLength));
7fe8059f 5306
5d7836c4
JS
5307 node = node->GetNext();
5308 }
5309
5310 return true;
5311 }
5312 else
5313 {
5314 // TODO: if not a text object, insert at closest position, e.g. in front of it
5315 }
5316 }
5317 else
5318 {
5319 // Add at end.
5320 // Don't pass parent initially to suppress auto-setting of parent range.
5321 // We'll do that at a higher level.
5322 wxRichTextPlainText* textObject = new wxRichTextPlainText(text, this);
5323
5324 AppendChild(textObject);
5325 return true;
5326 }
5327
5328 return false;
5329}
5330
5331void wxRichTextParagraph::Copy(const wxRichTextParagraph& obj)
5332{
bec80f4f 5333 wxRichTextCompositeObject::Copy(obj);
5d7836c4
JS
5334}
5335
5336/// Clear the cached lines
5337void wxRichTextParagraph::ClearLines()
5338{
5339 WX_CLEAR_LIST(wxRichTextLineList, m_cachedLines);
5340}
5341
5342/// Get/set the object size for the given range. Returns false if the range
5343/// is invalid for this object.
914a4e23 5344bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, wxRichTextDrawingContext& context, int flags, const wxPoint& position, const wxSize& parentSize, wxArrayInt* partialExtents) const
5d7836c4
JS
5345{
5346 if (!range.IsWithin(GetRange()))
5347 return false;
5348
5349 if (flags & wxRICHTEXT_UNFORMATTED)
5350 {
5351 // Just use unformatted data, assume no line breaks
5d7836c4
JS
5352 wxSize sz;
5353
31778480
JS
5354 wxArrayInt childExtents;
5355 wxArrayInt* p;
5356 if (partialExtents)
5357 p = & childExtents;
5358 else
5359 p = NULL;
5360
a70eb13e
JS
5361 int maxDescent = 0;
5362 int maxAscent = 0;
5363 int maxLineHeight = 0;
5364
5d7836c4
JS
5365 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
5366 while (node)
5367 {
5368 wxRichTextObject* child = node->GetData();
5369 if (!child->GetRange().IsOutside(range))
5370 {
cdaed652 5371 // Floating objects have a zero size within the paragraph.
e12b91a3 5372 if (child->IsFloating() && wxRichTextBuffer::GetFloatingLayoutMode())
cdaed652
VZ
5373 {
5374 if (partialExtents)
5375 {
5376 int lastSize;
5377 if (partialExtents->GetCount() > 0)
5378 lastSize = (*partialExtents)[partialExtents->GetCount()-1];
5379 else
5380 lastSize = 0;
5381
5382 partialExtents->Add(0 /* zero size */ + lastSize);
5383 }
5384 }
5385 else
5386 {
603f702b 5387 wxSize childSize;
4f3d5bc0 5388
603f702b
JS
5389 wxRichTextRange rangeToUse = range;
5390 rangeToUse.LimitTo(child->GetRange());
603f702b 5391 int childDescent = 0;
31778480 5392
7c9fdebe 5393 // At present wxRICHTEXT_HEIGHT_ONLY is only fast if we've already cached the size,
603f702b
JS
5394 // but it's only going to be used after caching has taken place.
5395 if ((flags & wxRICHTEXT_HEIGHT_ONLY) && child->GetCachedSize().y != 0)
2f45f554 5396 {
603f702b
JS
5397 childDescent = child->GetDescent();
5398 childSize = child->GetCachedSize();
2f45f554 5399
a70eb13e
JS
5400 if (childDescent == 0)
5401 {
5402 maxLineHeight = wxMax(maxLineHeight, childSize.y);
5403 }
5404 else
5405 {
5406 maxDescent = wxMax(maxDescent, childDescent);
5407 maxAscent = wxMax(maxAscent, (childSize.y - childDescent));
5408 }
5409
5410 maxLineHeight = wxMax(maxLineHeight, (maxAscent + maxDescent));
5411
5412 sz.y = wxMax(sz.y, maxLineHeight);
603f702b 5413 sz.x += childSize.x;
a70eb13e 5414 descent = maxDescent;
603f702b
JS
5415 }
5416 else if (child->IsTopLevel())
31778480 5417 {
603f702b
JS
5418 childDescent = child->GetDescent();
5419 childSize = child->GetCachedSize();
31778480 5420
a70eb13e
JS
5421 if (childDescent == 0)
5422 {
5423 maxLineHeight = wxMax(maxLineHeight, childSize.y);
5424 }
5425 else
5426 {
5427 maxDescent = wxMax(maxDescent, childDescent);
5428 maxAscent = wxMax(maxAscent, (childSize.y - childDescent));
5429 }
5430
5431 maxLineHeight = wxMax(maxLineHeight, (maxAscent + maxDescent));
5432
5433 sz.y = wxMax(sz.y, maxLineHeight);
603f702b 5434 sz.x += childSize.x;
a70eb13e
JS
5435 descent = maxDescent;
5436
5437 // FIXME: this won't change the original values.
5438 // Should we be calling GetRangeSize above instead of using cached values?
5439#if 0
603f702b 5440 if ((flags & wxRICHTEXT_CACHE_SIZE) && (rangeToUse == child->GetRange()))
31778480 5441 {
603f702b
JS
5442 child->SetCachedSize(childSize);
5443 child->SetDescent(childDescent);
31778480 5444 }
a70eb13e 5445#endif
31778480 5446
603f702b
JS
5447 if (partialExtents)
5448 {
5449 int lastSize;
5450 if (partialExtents->GetCount() > 0)
5451 lastSize = (*partialExtents)[partialExtents->GetCount()-1];
5452 else
5453 lastSize = 0;
5454
5455 partialExtents->Add(childSize.x + lastSize);
5456 }
5457 }
914a4e23 5458 else if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, context, flags, wxPoint(position.x + sz.x, position.y), parentSize, p))
603f702b 5459 {
a70eb13e
JS
5460 if (childDescent == 0)
5461 {
5462 maxLineHeight = wxMax(maxLineHeight, childSize.y);
5463 }
5464 else
5465 {
5466 maxDescent = wxMax(maxDescent, childDescent);
5467 maxAscent = wxMax(maxAscent, (childSize.y - childDescent));
5468 }
5469
5470 maxLineHeight = wxMax(maxLineHeight, (maxAscent + maxDescent));
5471
5472 sz.y = wxMax(sz.y, maxLineHeight);
603f702b 5473 sz.x += childSize.x;
a70eb13e 5474 descent = maxDescent;
603f702b
JS
5475
5476 if ((flags & wxRICHTEXT_CACHE_SIZE) && (rangeToUse == child->GetRange()))
5477 {
5478 child->SetCachedSize(childSize);
5479 child->SetDescent(childDescent);
5480 }
5481
5482 if (partialExtents)
5483 {
5484 int lastSize;
5485 if (partialExtents->GetCount() > 0)
5486 lastSize = (*partialExtents)[partialExtents->GetCount()-1];
5487 else
5488 lastSize = 0;
5489
5490 size_t i;
5491 for (i = 0; i < childExtents.GetCount(); i++)
5492 {
5493 partialExtents->Add(childExtents[i] + lastSize);
5494 }
5495 }
5496 }
5497 }
5498
5499 if (p)
5500 p->Clear();
5d7836c4
JS
5501 }
5502
5503 node = node->GetNext();
5504 }
5505 size = sz;
5506 }
5507 else
5508 {
5509 // Use formatted data, with line breaks
5510 wxSize sz;
5511
5512 // We're going to loop through each line, and then for each line,
5513 // call GetRangeSize for the fragment that comprises that line.
5514 // Only we have to do that multiple times within the line, because
5515 // the line may be broken into pieces. For now ignore line break commands
5516 // (so we can assume that getting the unformatted size for a fragment
5517 // within a line is the actual size)
5518
5519 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
5520 while (node)
5521 {
5522 wxRichTextLine* line = node->GetData();
1e967276
JS
5523 wxRichTextRange lineRange = line->GetAbsoluteRange();
5524 if (!lineRange.IsOutside(range))
5d7836c4 5525 {
a70eb13e
JS
5526 int maxDescent = 0;
5527 int maxAscent = 0;
5528 int maxLineHeight = 0;
5529 int maxLineWidth = 0;
7fe8059f 5530
5d7836c4
JS
5531 wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst();
5532 while (node2)
5533 {
5534 wxRichTextObject* child = node2->GetData();
7fe8059f 5535
e12b91a3 5536 if ((!child->IsFloating() || !wxRichTextBuffer::GetFloatingLayoutMode()) && !child->GetRange().IsOutside(lineRange))
5d7836c4 5537 {
1e967276 5538 wxRichTextRange rangeToUse = lineRange;
5d7836c4 5539 rangeToUse.LimitTo(child->GetRange());
603f702b
JS
5540 if (child->IsTopLevel())
5541 rangeToUse = child->GetOwnRange();
7fe8059f 5542
5d7836c4
JS
5543 wxSize childSize;
5544 int childDescent = 0;
914a4e23 5545 if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, context, flags, wxPoint(position.x + sz.x, position.y), parentSize))
5d7836c4 5546 {
a70eb13e
JS
5547 if (childDescent == 0)
5548 {
5549 // Assume that if descent is zero, this child can occupy the full line height
5550 // and does not need space for the line's maximum descent. So we influence
5551 // the overall max line height only.
5552 maxLineHeight = wxMax(maxLineHeight, childSize.y);
5553 }
5554 else
5555 {
5556 maxAscent = wxMax(maxAscent, (childSize.y - childDescent));
5557 maxDescent = wxMax(maxAscent, childDescent);
5558 }
5559 maxLineHeight = wxMax(maxLineHeight, (maxAscent + maxDescent));
5560 maxLineWidth += childSize.x;
5d7836c4 5561 }
5d7836c4 5562 }
7fe8059f 5563
5d7836c4
JS
5564 node2 = node2->GetNext();
5565 }
5566
a70eb13e
JS
5567 descent = wxMax(descent, maxDescent);
5568
5d7836c4 5569 // Increase size by a line (TODO: paragraph spacing)
a70eb13e
JS
5570 sz.y += maxLineHeight;
5571 sz.x = wxMax(sz.x, maxLineWidth);
5d7836c4
JS
5572 }
5573 node = node->GetNext();
5574 }
5575 size = sz;
5576 }
5577 return true;
5578}
5579
5580/// Finds the absolute position and row height for the given character position
8db2e3ef 5581bool wxRichTextParagraph::FindPosition(wxDC& dc, wxRichTextDrawingContext& context, long index, wxPoint& pt, int* height, bool forceLineStart)
5d7836c4
JS
5582{
5583 if (index == -1)
5584 {
5585 wxRichTextLine* line = ((wxRichTextParagraphLayoutBox*)GetParent())->GetLineAtPosition(0);
5586 if (line)
5587 *height = line->GetSize().y;
5588 else
5589 *height = dc.GetCharHeight();
5590
5591 // -1 means 'the start of the buffer'.
5592 pt = GetPosition();
5593 if (line)
5594 pt = pt + line->GetPosition();
5595
5d7836c4
JS
5596 return true;
5597 }
5598
5599 // The final position in a paragraph is taken to mean the position
5600 // at the start of the next paragraph.
5601 if (index == GetRange().GetEnd())
5602 {
5603 wxRichTextParagraphLayoutBox* parent = wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox);
5604 wxASSERT( parent != NULL );
5605
5606 // Find the height at the next paragraph, if any
5607 wxRichTextLine* line = parent->GetLineAtPosition(index + 1);
5608 if (line)
5609 {
5610 *height = line->GetSize().y;
5611 pt = line->GetAbsolutePosition();
5612 }
5613 else
5614 {
5615 *height = dc.GetCharHeight();
5616 int indent = ConvertTenthsMMToPixels(dc, m_attributes.GetLeftIndent());
5617 pt = wxPoint(indent, GetCachedSize().y);
5618 }
5619
5620 return true;
5621 }
5622
5623 if (index < GetRange().GetStart() || index > GetRange().GetEnd())
5624 return false;
5625
5626 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
5627 while (node)
5628 {
5629 wxRichTextLine* line = node->GetData();
1e967276
JS
5630 wxRichTextRange lineRange = line->GetAbsoluteRange();
5631 if (index >= lineRange.GetStart() && index <= lineRange.GetEnd())
5d7836c4
JS
5632 {
5633 // If this is the last point in the line, and we're forcing the
5634 // returned value to be the start of the next line, do the required
5635 // thing.
1e967276 5636 if (index == lineRange.GetEnd() && forceLineStart)
5d7836c4
JS
5637 {
5638 if (node->GetNext())
5639 {
5640 wxRichTextLine* nextLine = node->GetNext()->GetData();
5641 *height = nextLine->GetSize().y;
5642 pt = nextLine->GetAbsolutePosition();
5643 return true;
5644 }
5645 }
5646
5647 pt.y = line->GetPosition().y + GetPosition().y;
5648
1e967276 5649 wxRichTextRange r(lineRange.GetStart(), index);
5d7836c4
JS
5650 wxSize rangeSize;
5651 int descent = 0;
5652
5653 // We find the size of the line up to this point,
5654 // then we can add this size to the line start position and
5655 // paragraph start position to find the actual position.
5656
8db2e3ef 5657 if (GetRangeSize(r, rangeSize, descent, dc, context, wxRICHTEXT_UNFORMATTED, line->GetPosition()+ GetPosition()))
5d7836c4
JS
5658 {
5659 pt.x = line->GetPosition().x + GetPosition().x + rangeSize.x;
5660 *height = line->GetSize().y;
5661
5662 return true;
5663 }
5664
5665 }
5666
5667 node = node->GetNext();
5668 }
5669
5670 return false;
5671}
5672
5673/// Hit-testing: returns a flag indicating hit test details, plus
5674/// information about position
8db2e3ef 5675int wxRichTextParagraph::HitTest(wxDC& dc, wxRichTextDrawingContext& context, const wxPoint& pt, long& textPosition, wxRichTextObject** obj, wxRichTextObject** contextObj, int flags)
5d7836c4 5676{
603f702b
JS
5677 if (!IsShown())
5678 return wxRICHTEXT_HITTEST_NONE;
5679
5680 // If we're in the top-level container, then we can return
5681 // a suitable hit test code even if the point is outside the container area,
5682 // so that we can position the caret sensibly even if we don't
5683 // click on valid content. If we're not at the top-level, and the point
5684 // is not within this paragraph object, then we don't want to stop more
5685 // precise hit-testing from working prematurely, so return immediately.
5686 // NEW STRATEGY: use the parent boundary to test whether we're in the
5687 // right region, not the paragraph, since the paragraph may be positioned
5688 // some way in from where the user clicks.
5689 {
5690 long tmpPos;
5691 wxRichTextObject* tempObj, *tempContextObj;
8db2e3ef 5692 if (GetParent() && GetParent()->wxRichTextObject::HitTest(dc, context, pt, tmpPos, & tempObj, & tempContextObj, flags) == wxRICHTEXT_HITTEST_NONE)
603f702b
JS
5693 return wxRICHTEXT_HITTEST_NONE;
5694 }
5695
5696 wxRichTextObjectList::compatibility_iterator objNode = m_children.GetFirst();
5697 while (objNode)
5698 {
5699 wxRichTextObject* child = objNode->GetData();
7c9fdebe
JS
5700 // Don't recurse if we have wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS,
5701 // and also, if this seems composite but actually is marked as atomic,
5702 // don't recurse.
5703 if (child->IsTopLevel() && ((flags & wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS) == 0) &&
5704 (! (((flags & wxRICHTEXT_HITTEST_HONOUR_ATOMIC) != 0) && child->IsAtomic())))
603f702b
JS
5705 {
5706 {
8db2e3ef 5707 int hitTest = child->HitTest(dc, context, pt, textPosition, obj, contextObj);
603f702b
JS
5708 if (hitTest != wxRICHTEXT_HITTEST_NONE)
5709 return hitTest;
5710 }
5711 }
5712
5713 objNode = objNode->GetNext();
5714 }
5715
5d7836c4
JS
5716 wxPoint paraPos = GetPosition();
5717
5718 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst();
5719 while (node)
5720 {
5721 wxRichTextLine* line = node->GetData();
5722 wxPoint linePos = paraPos + line->GetPosition();
5723 wxSize lineSize = line->GetSize();
1e967276 5724 wxRichTextRange lineRange = line->GetAbsoluteRange();
5d7836c4 5725
62381daa 5726 if (pt.y <= linePos.y + lineSize.y)
5d7836c4
JS
5727 {
5728 if (pt.x < linePos.x)
5729 {
1e967276 5730 textPosition = lineRange.GetStart();
603f702b
JS
5731 *obj = FindObjectAtPosition(textPosition);
5732 *contextObj = GetContainer();
f262b25c 5733 return wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_OUTSIDE;
5d7836c4
JS
5734 }
5735 else if (pt.x >= (linePos.x + lineSize.x))
5736 {
1e967276 5737 textPosition = lineRange.GetEnd();
603f702b
JS
5738 *obj = FindObjectAtPosition(textPosition);
5739 *contextObj = GetContainer();
f262b25c 5740 return wxRICHTEXT_HITTEST_AFTER|wxRICHTEXT_HITTEST_OUTSIDE;
5d7836c4
JS
5741 }
5742 else
5743 {
2f45f554
JS
5744#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
5745 wxArrayInt partialExtents;
5746
5747 wxSize paraSize;
5748 int paraDescent;
5749
5750 // This calculates the partial text extents
914a4e23 5751 GetRangeSize(lineRange, paraSize, paraDescent, dc, context, wxRICHTEXT_UNFORMATTED, linePos, wxDefaultSize, & partialExtents);
2f45f554
JS
5752
5753 int lastX = linePos.x;
5754 size_t i;
5755 for (i = 0; i < partialExtents.GetCount(); i++)
5756 {
5757 int nextX = partialExtents[i] + linePos.x;
5758
5759 if (pt.x >= lastX && pt.x <= nextX)
5760 {
5761 textPosition = i + lineRange.GetStart(); // minus 1?
5762
603f702b
JS
5763 *obj = FindObjectAtPosition(textPosition);
5764 *contextObj = GetContainer();
5765
2f45f554
JS
5766 // So now we know it's between i-1 and i.
5767 // Let's see if we can be more precise about
5768 // which side of the position it's on.
5769
cdaed652 5770 int midPoint = (nextX + lastX)/2;
2f45f554
JS
5771 if (pt.x >= midPoint)
5772 return wxRICHTEXT_HITTEST_AFTER;
5773 else
5774 return wxRICHTEXT_HITTEST_BEFORE;
5775 }
5776
5777 lastX = nextX;
5778 }
5779#else
5d7836c4
JS
5780 long i;
5781 int lastX = linePos.x;
1e967276 5782 for (i = lineRange.GetStart(); i <= lineRange.GetEnd(); i++)
5d7836c4
JS
5783 {
5784 wxSize childSize;
5785 int descent = 0;
7fe8059f 5786
1e967276 5787 wxRichTextRange rangeToUse(lineRange.GetStart(), i);
7fe8059f 5788
8db2e3ef 5789 GetRangeSize(rangeToUse, childSize, descent, dc, context, wxRICHTEXT_UNFORMATTED, linePos);
5d7836c4
JS
5790
5791 int nextX = childSize.x + linePos.x;
5792
5793 if (pt.x >= lastX && pt.x <= nextX)
5794 {
5795 textPosition = i;
5796
603f702b
JS
5797 *obj = FindObjectAtPosition(textPosition);
5798 *contextObj = GetContainer();
5799
5d7836c4
JS
5800 // So now we know it's between i-1 and i.
5801 // Let's see if we can be more precise about
5802 // which side of the position it's on.
5803
cdaed652 5804 int midPoint = (nextX + lastX)/2;
5d7836c4
JS
5805 if (pt.x >= midPoint)
5806 return wxRICHTEXT_HITTEST_AFTER;
5807 else
5808 return wxRICHTEXT_HITTEST_BEFORE;
5809 }
5810 else
5811 {
5812 lastX = nextX;
5813 }
5814 }
2f45f554 5815#endif
5d7836c4
JS
5816 }
5817 }
7fe8059f 5818
5d7836c4
JS
5819 node = node->GetNext();
5820 }
5821
5822 return wxRICHTEXT_HITTEST_NONE;
5823}
5824
5825/// Split an object at this position if necessary, and return
5826/// the previous object, or NULL if inserting at beginning.
5827wxRichTextObject* wxRichTextParagraph::SplitAt(long pos, wxRichTextObject** previousObject)
5828{
5829 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
5830 while (node)
5831 {
5832 wxRichTextObject* child = node->GetData();
5833
5834 if (pos == child->GetRange().GetStart())
5835 {
5836 if (previousObject)
4d551ad5
JS
5837 {
5838 if (node->GetPrevious())
5839 *previousObject = node->GetPrevious()->GetData();
5840 else
5841 *previousObject = NULL;
5842 }
5d7836c4
JS
5843
5844 return child;
5845 }
5846
5847 if (child->GetRange().Contains(pos))
5848 {
5849 // This should create a new object, transferring part of
5850 // the content to the old object and the rest to the new object.
5851 wxRichTextObject* newObject = child->DoSplit(pos);
5852
5853 // If we couldn't split this object, just insert in front of it.
5854 if (!newObject)
5855 {
5856 // Maybe this is an empty string, try the next one
5857 // return child;
5858 }
5859 else
5860 {
5861 // Insert the new object after 'child'
5862 if (node->GetNext())
5863 m_children.Insert(node->GetNext(), newObject);
5864 else
5865 m_children.Append(newObject);
5866 newObject->SetParent(this);
5867
5868 if (previousObject)
5869 *previousObject = child;
5870
5871 return newObject;
5872 }
5873 }
5874
5875 node = node->GetNext();
5876 }
5877 if (previousObject)
5878 *previousObject = NULL;
5879 return NULL;
5880}
5881
5882/// Move content to a list from obj on
5883void wxRichTextParagraph::MoveToList(wxRichTextObject* obj, wxList& list)
5884{
5885 wxRichTextObjectList::compatibility_iterator node = m_children.Find(obj);
5886 while (node)
5887 {
5888 wxRichTextObject* child = node->GetData();
5889 list.Append(child);
5890
5891 wxRichTextObjectList::compatibility_iterator oldNode = node;
5892
5893 node = node->GetNext();
5894
5895 m_children.DeleteNode(oldNode);
5896 }
5897}
5898
5899/// Add content back from list
5900void wxRichTextParagraph::MoveFromList(wxList& list)
5901{
09f14108 5902 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
5d7836c4
JS
5903 {
5904 AppendChild((wxRichTextObject*) node->GetData());
5905 }
5906}
5907
5908/// Calculate range
5909void wxRichTextParagraph::CalculateRange(long start, long& end)
5910{
5911 wxRichTextCompositeObject::CalculateRange(start, end);
5912
5913 // Add one for end of paragraph
5914 end ++;
5915
5916 m_range.SetRange(start, end);
5917}
5918
5919/// Find the object at the given position
5920wxRichTextObject* wxRichTextParagraph::FindObjectAtPosition(long position)
5921{
5922 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
5923 while (node)
5924 {
5925 wxRichTextObject* obj = node->GetData();
603f702b
JS
5926 if (obj->GetRange().Contains(position) ||
5927 obj->GetRange().GetStart() == position ||
5928 obj->GetRange().GetEnd() == position)
5d7836c4 5929 return obj;
7fe8059f 5930
5d7836c4
JS
5931 node = node->GetNext();
5932 }
5933 return NULL;
5934}
5935
5936/// Get the plain text searching from the start or end of the range.
5937/// The resulting string may be shorter than the range given.
5938bool wxRichTextParagraph::GetContiguousPlainText(wxString& text, const wxRichTextRange& range, bool fromStart)
5939{
5940 text = wxEmptyString;
5941
5942 if (fromStart)
5943 {
5944 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
5945 while (node)
5946 {
5947 wxRichTextObject* obj = node->GetData();
5948 if (!obj->GetRange().IsOutside(range))
5949 {
5950 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
5951 if (textObj)
5952 {
5953 text += textObj->GetTextForRange(range);
5954 }
5955 else
043c0d58
JS
5956 {
5957 text += wxT(" ");
5958 }
5d7836c4
JS
5959 }
5960
5961 node = node->GetNext();
5962 }
5963 }
5964 else
5965 {
5966 wxRichTextObjectList::compatibility_iterator node = m_children.GetLast();
5967 while (node)
5968 {
5969 wxRichTextObject* obj = node->GetData();
5970 if (!obj->GetRange().IsOutside(range))
5971 {
5972 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
5973 if (textObj)
5974 {
5975 text = textObj->GetTextForRange(range) + text;
5976 }
5977 else
043c0d58
JS
5978 {
5979 text = wxT(" ") + text;
5980 }
5d7836c4
JS
5981 }
5982
5983 node = node->GetPrevious();
5984 }
5985 }
5986
5987 return true;
5988}
5989
5990/// Find a suitable wrap position.
8db2e3ef 5991bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& dc, wxRichTextDrawingContext& context, int availableSpace, long& wrapPosition, wxArrayInt* partialExtents)
5d7836c4 5992{
72945e24
JS
5993 if (range.GetLength() <= 0)
5994 return false;
5995
5d7836c4
JS
5996 // Find the first position where the line exceeds the available space.
5997 wxSize sz;
5d7836c4 5998 long breakPosition = range.GetEnd();
ecb5fbf1 5999
31778480
JS
6000#if wxRICHTEXT_USE_PARTIAL_TEXT_EXTENTS
6001 if (partialExtents && partialExtents->GetCount() >= (size_t) (GetRange().GetLength()-1)) // the final position in a paragraph is the newline
5d7836c4 6002 {
31778480 6003 int widthBefore;
5d7836c4 6004
31778480
JS
6005 if (range.GetStart() > GetRange().GetStart())
6006 widthBefore = (*partialExtents)[range.GetStart() - GetRange().GetStart() - 1];
6007 else
6008 widthBefore = 0;
6009
6010 size_t i;
43a0d1e1 6011 for (i = (size_t) range.GetStart(); i <= (size_t) range.GetEnd(); i++)
5d7836c4 6012 {
31778480 6013 int widthFromStartOfThisRange = (*partialExtents)[i - GetRange().GetStart()] - widthBefore;
ecb5fbf1 6014
72945e24 6015 if (widthFromStartOfThisRange > availableSpace)
ecb5fbf1 6016 {
31778480
JS
6017 breakPosition = i-1;
6018 break;
ecb5fbf1 6019 }
5d7836c4 6020 }
31778480
JS
6021 }
6022 else
6023#endif
6024 {
6025 // Binary chop for speed
6026 long minPos = range.GetStart();
6027 long maxPos = range.GetEnd();
6028 while (true)
ecb5fbf1 6029 {
31778480
JS
6030 if (minPos == maxPos)
6031 {
6032 int descent = 0;
8db2e3ef 6033 GetRangeSize(wxRichTextRange(range.GetStart(), minPos), sz, descent, dc, context, wxRICHTEXT_UNFORMATTED);
ecb5fbf1 6034
31778480
JS
6035 if (sz.x > availableSpace)
6036 breakPosition = minPos - 1;
6037 break;
6038 }
6039 else if ((maxPos - minPos) == 1)
ecb5fbf1 6040 {
31778480 6041 int descent = 0;
8db2e3ef 6042 GetRangeSize(wxRichTextRange(range.GetStart(), minPos), sz, descent, dc, context, wxRICHTEXT_UNFORMATTED);
31778480
JS
6043
6044 if (sz.x > availableSpace)
6045 breakPosition = minPos - 1;
6046 else
6047 {
8db2e3ef 6048 GetRangeSize(wxRichTextRange(range.GetStart(), maxPos), sz, descent, dc, context, wxRICHTEXT_UNFORMATTED);
31778480
JS
6049 if (sz.x > availableSpace)
6050 breakPosition = maxPos-1;
6051 }
6052 break;
ecb5fbf1
JS
6053 }
6054 else
6055 {
31778480
JS
6056 long nextPos = minPos + ((maxPos - minPos) / 2);
6057
6058 int descent = 0;
8db2e3ef 6059 GetRangeSize(wxRichTextRange(range.GetStart(), nextPos), sz, descent, dc, context, wxRICHTEXT_UNFORMATTED);
31778480
JS
6060
6061 if (sz.x > availableSpace)
6062 {
6063 maxPos = nextPos;
6064 }
6065 else
6066 {
6067 minPos = nextPos;
6068 }
ecb5fbf1
JS
6069 }
6070 }
5d7836c4
JS
6071 }
6072
6073 // Now we know the last position on the line.
6074 // Let's try to find a word break.
6075
6076 wxString plainText;
6077 if (GetContiguousPlainText(plainText, wxRichTextRange(range.GetStart(), breakPosition), false))
6078 {
ff76711f
JS
6079 int newLinePos = plainText.Find(wxRichTextLineBreakChar);
6080 if (newLinePos != wxNOT_FOUND)
5d7836c4 6081 {
ff76711f
JS
6082 breakPosition = wxMax(0, range.GetStart() + newLinePos);
6083 }
6084 else
6085 {
6086 int spacePos = plainText.Find(wxT(' '), true);
31002e44
JS
6087 int tabPos = plainText.Find(wxT('\t'), true);
6088 int pos = wxMax(spacePos, tabPos);
6089 if (pos != wxNOT_FOUND)
ff76711f 6090 {
31002e44 6091 int positionsFromEndOfString = plainText.length() - pos - 1;
ff76711f
JS
6092 breakPosition = breakPosition - positionsFromEndOfString;
6093 }
5d7836c4
JS
6094 }
6095 }
6096
6097 wrapPosition = breakPosition;
6098
6099 return true;
6100}
6101
6102/// Get the bullet text for this paragraph.
6103wxString wxRichTextParagraph::GetBulletText()
6104{
6105 if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE ||
6106 (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP))
6107 return wxEmptyString;
6108
6109 int number = GetAttributes().GetBulletNumber();
6110
6111 wxString text;
d2d0adc7 6112 if ((GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC) || (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE))
5d7836c4
JS
6113 {
6114 text.Printf(wxT("%d"), number);
6115 }
6116 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER)
6117 {
6118 // TODO: Unicode, and also check if number > 26
6119 text.Printf(wxT("%c"), (wxChar) (number+64));
6120 }
6121 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER)
6122 {
6123 // TODO: Unicode, and also check if number > 26
6124 text.Printf(wxT("%c"), (wxChar) (number+96));
6125 }
6126 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER)
6127 {
59509217 6128 text = wxRichTextDecimalToRoman(number);
5d7836c4
JS
6129 }
6130 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER)
6131 {
59509217
JS
6132 text = wxRichTextDecimalToRoman(number);
6133 text.MakeLower();
5d7836c4
JS
6134 }
6135 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL)
6136 {
d2d0adc7
JS
6137 text = GetAttributes().GetBulletText();
6138 }
3e541562 6139
d2d0adc7
JS
6140 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_OUTLINE)
6141 {
6142 // The outline style relies on the text being computed statically,
6143 // since it depends on other levels points (e.g. 1.2.1.1). So normally the bullet text
6144 // should be stored in the attributes; if not, just use the number for this
6145 // level, as previously computed.
6146 if (!GetAttributes().GetBulletText().IsEmpty())
6147 text = GetAttributes().GetBulletText();
5d7836c4
JS
6148 }
6149
6150 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES)
6151 {
6152 text = wxT("(") + text + wxT(")");
6153 }
d2d0adc7
JS
6154 else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_RIGHT_PARENTHESIS)
6155 {
6156 text = text + wxT(")");
6157 }
6158
5d7836c4
JS
6159 if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD)
6160 {
6161 text += wxT(".");
6162 }
6163
6164 return text;
6165}
6166
1e967276
JS
6167/// Allocate or reuse a line object
6168wxRichTextLine* wxRichTextParagraph::AllocateLine(int pos)
6169{
6170 if (pos < (int) m_cachedLines.GetCount())
6171 {
6172 wxRichTextLine* line = m_cachedLines.Item(pos)->GetData();
6173 line->Init(this);
6174 return line;
6175 }
6176 else
6177 {
6178 wxRichTextLine* line = new wxRichTextLine(this);
6179 m_cachedLines.Append(line);
6180 return line;
6181 }
6182}
6183
6184/// Clear remaining unused line objects, if any
6185bool wxRichTextParagraph::ClearUnusedLines(int lineCount)
6186{
6187 int cachedLineCount = m_cachedLines.GetCount();
6188 if ((int) cachedLineCount > lineCount)
6189 {
6190 for (int i = 0; i < (int) (cachedLineCount - lineCount); i ++)
6191 {
6192 wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetLast();
6193 wxRichTextLine* line = node->GetData();
6194 m_cachedLines.Erase(node);
6195 delete line;
6196 }
6197 }
6198 return true;
6199}
6200
fe5aa22c
JS
6201/// Get combined attributes of the base style, paragraph style and character style. We use this to dynamically
6202/// retrieve the actual style.
603f702b 6203wxRichTextAttr wxRichTextParagraph::GetCombinedAttributes(const wxRichTextAttr& contentStyle, bool includingBoxAttr) const
fe5aa22c 6204{
24777478 6205 wxRichTextAttr attr;
603f702b 6206 wxRichTextParagraphLayoutBox* buf = wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox);
fe5aa22c
JS
6207 if (buf)
6208 {
6209 attr = buf->GetBasicStyle();
603f702b
JS
6210 if (!includingBoxAttr)
6211 {
6212 attr.GetTextBoxAttr().Reset();
6213 // The background colour will be painted by the container, and we don't
6214 // want to unnecessarily overwrite the background when we're drawing text
6215 // because this may erase the guideline (which appears just under the text
6216 // if there's no padding).
6217 attr.SetFlags(attr.GetFlags() & ~wxTEXT_ATTR_BACKGROUND_COLOUR);
6218 }
fe5aa22c
JS
6219 wxRichTextApplyStyle(attr, GetAttributes());
6220 }
6221 else
6222 attr = GetAttributes();
6223
6224 wxRichTextApplyStyle(attr, contentStyle);
6225 return attr;
6226}
6227
6228/// Get combined attributes of the base style and paragraph style.
603f702b 6229wxRichTextAttr wxRichTextParagraph::GetCombinedAttributes(bool includingBoxAttr) const
fe5aa22c 6230{
24777478 6231 wxRichTextAttr attr;
603f702b 6232 wxRichTextParagraphLayoutBox* buf = wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox);
fe5aa22c
JS
6233 if (buf)
6234 {
6235 attr = buf->GetBasicStyle();
603f702b
JS
6236 if (!includingBoxAttr)
6237 attr.GetTextBoxAttr().Reset();
fe5aa22c
JS
6238 wxRichTextApplyStyle(attr, GetAttributes());
6239 }
6240 else
6241 attr = GetAttributes();
6242
6243 return attr;
6244}
5d7836c4 6245
603f702b 6246// Create default tabstop array
cfa3b256
JS
6247void wxRichTextParagraph::InitDefaultTabs()
6248{
6249 // create a default tab list at 10 mm each.
6250 for (int i = 0; i < 20; ++i)
6251 {
6252 sm_defaultTabs.Add(i*100);
6253 }
6254}
6255
603f702b 6256// Clear default tabstop array
cfa3b256
JS
6257void wxRichTextParagraph::ClearDefaultTabs()
6258{
6259 sm_defaultTabs.Clear();
6260}
6261
c4168888 6262void wxRichTextParagraph::LayoutFloat(wxDC& dc, wxRichTextDrawingContext& context, const wxRect& rect, const wxRect& parentRect, int style, wxRichTextFloatCollector* floatCollector)
cdaed652
VZ
6263{
6264 wxRichTextObjectList::compatibility_iterator node = GetChildren().GetFirst();
6265 while (node)
6266 {
bec80f4f 6267 wxRichTextObject* anchored = node->GetData();
07d4142f 6268 if (anchored && anchored->IsFloating() && !floatCollector->HasFloat(anchored))
cdaed652 6269 {
c4168888
JS
6270 int x = 0;
6271 wxRichTextAttr parentAttr(GetAttributes());
6272 context.ApplyVirtualAttributes(parentAttr, this);
6273#if 1
6274 // 27-09-2012
6275 wxRect availableSpace = GetParent()->GetAvailableContentArea(dc, context, rect);
6276
6277 anchored->LayoutToBestSize(dc, context, GetBuffer(),
6278 parentAttr, anchored->GetAttributes(),
6279 parentRect, availableSpace,
6280 style);
6281 wxSize size = anchored->GetCachedSize();
6282#else
cdaed652 6283 wxSize size;
c4168888 6284 int descent = 0;
8db2e3ef 6285 anchored->GetRangeSize(anchored->GetRange(), size, descent, dc, context, style);
c4168888 6286#endif
bec80f4f 6287
24777478 6288 int offsetY = 0;
603f702b 6289 if (anchored->GetAttributes().GetTextBoxAttr().GetTop().IsValid())
24777478
JS
6290 {
6291 offsetY = anchored->GetAttributes().GetTextBoxAttr().GetTop().GetValue();
6292 if (anchored->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
6293 {
6294 offsetY = ConvertTenthsMMToPixels(dc, offsetY);
6295 }
6296 }
bec80f4f 6297
24777478 6298 int pos = floatCollector->GetFitPosition(anchored->GetAttributes().GetTextBoxAttr().GetFloatMode(), rect.y + offsetY, size.y);
ce00f59b 6299
cdaed652 6300 /* Update the offset */
24777478
JS
6301 int newOffsetY = pos - rect.y;
6302 if (newOffsetY != offsetY)
6303 {
6304 if (anchored->GetAttributes().GetTextBoxAttr().GetTop().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
6305 newOffsetY = ConvertPixelsToTenthsMM(dc, newOffsetY);
6306 anchored->GetAttributes().GetTextBoxAttr().GetTop().SetValue(newOffsetY);
6307 }
cdaed652 6308
24777478 6309 if (anchored->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT)
603f702b 6310 x = rect.x;
24777478 6311 else if (anchored->GetAttributes().GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT)
603f702b 6312 x = rect.x + rect.width - size.x;
24777478 6313
c4168888
JS
6314 //anchored->SetPosition(wxPoint(x, pos));
6315 anchored->Move(wxPoint(x, pos)); // should move children
cdaed652
VZ
6316 anchored->SetCachedSize(size);
6317 floatCollector->CollectFloat(this, anchored);
6318 }
6319
6320 node = node->GetNext();
6321 }
6322}
6323
603f702b 6324// Get the first position from pos that has a line break character.
ff76711f
JS
6325long wxRichTextParagraph::GetFirstLineBreakPosition(long pos)
6326{
6327 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
6328 while (node)
6329 {
6330 wxRichTextObject* obj = node->GetData();
6331 if (pos >= obj->GetRange().GetStart() && pos <= obj->GetRange().GetEnd())
6332 {
6333 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
6334 if (textObj)
6335 {
6336 long breakPos = textObj->GetFirstLineBreakPosition(pos);
6337 if (breakPos > -1)
6338 return breakPos;
6339 }
6340 }
6341 node = node->GetNext();
6342 }
6343 return -1;
6344}
cfa3b256 6345
5d7836c4
JS
6346/*!
6347 * wxRichTextLine
6348 * This object represents a line in a paragraph, and stores
6349 * offsets from the start of the paragraph representing the
6350 * start and end positions of the line.
6351 */
6352
6353wxRichTextLine::wxRichTextLine(wxRichTextParagraph* parent)
6354{
1e967276 6355 Init(parent);
5d7836c4
JS
6356}
6357
6358/// Initialisation
1e967276 6359void wxRichTextLine::Init(wxRichTextParagraph* parent)
5d7836c4 6360{
1e967276
JS
6361 m_parent = parent;
6362 m_range.SetRange(-1, -1);
6363 m_pos = wxPoint(0, 0);
6364 m_size = wxSize(0, 0);
5d7836c4 6365 m_descent = 0;
2f45f554
JS
6366#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
6367 m_objectSizes.Clear();
6368#endif
5d7836c4
JS
6369}
6370
6371/// Copy
6372void wxRichTextLine::Copy(const wxRichTextLine& obj)
6373{
6374 m_range = obj.m_range;
2f45f554
JS
6375#if wxRICHTEXT_USE_OPTIMIZED_LINE_DRAWING
6376 m_objectSizes = obj.m_objectSizes;
6377#endif
5d7836c4
JS
6378}
6379
6380/// Get the absolute object position
6381wxPoint wxRichTextLine::GetAbsolutePosition() const
6382{
6383 return m_parent->GetPosition() + m_pos;
6384}
6385
1e967276
JS
6386/// Get the absolute range
6387wxRichTextRange wxRichTextLine::GetAbsoluteRange() const
6388{
6389 wxRichTextRange range(m_range.GetStart() + m_parent->GetRange().GetStart(), 0);
6390 range.SetEnd(range.GetStart() + m_range.GetLength()-1);
6391 return range;
6392}
6393
5d7836c4
JS
6394/*!
6395 * wxRichTextPlainText
6396 * This object represents a single piece of text.
6397 */
6398
6399IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText, wxRichTextObject)
6400
24777478 6401wxRichTextPlainText::wxRichTextPlainText(const wxString& text, wxRichTextObject* parent, wxRichTextAttr* style):
5d7836c4
JS
6402 wxRichTextObject(parent)
6403{
5d7836c4
JS
6404 if (style)
6405 SetAttributes(*style);
6406
6407 m_text = text;
6408}
6409
cfa3b256
JS
6410#define USE_KERNING_FIX 1
6411
4794d69c
JS
6412// If insufficient tabs are defined, this is the tab width used
6413#define WIDTH_FOR_DEFAULT_TABS 50
6414
5d7836c4 6415/// Draw the item
8db2e3ef 6416bool wxRichTextPlainText::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int WXUNUSED(style))
5d7836c4 6417{
fe5aa22c
JS
6418 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
6419 wxASSERT (para != NULL);
6420
603f702b 6421 wxRichTextAttr textAttr(para ? para->GetCombinedAttributes(GetAttributes(), false /* no box attributes */) : GetAttributes());
8db2e3ef 6422 context.ApplyVirtualAttributes(textAttr, this);
603f702b
JS
6423
6424 // Let's make the assumption for now that for content in a paragraph, including
6425 // text, we never have a discontinuous selection. So we only deal with a
6426 // single range.
6427 wxRichTextRange selectionRange;
6428 if (selection.IsValid())
6429 {
6430 wxRichTextRangeArray selectionRanges = selection.GetSelectionForObject(this);
6431 if (selectionRanges.GetCount() > 0)
6432 selectionRange = selectionRanges[0];
6433 else
6434 selectionRange = wxRICHTEXT_NO_SELECTION;
6435 }
6436 else
6437 selectionRange = wxRICHTEXT_NO_SELECTION;
fe5aa22c 6438
5d7836c4
JS
6439 int offset = GetRange().GetStart();
6440
ff76711f 6441 wxString str = m_text;
f7667b84
JS
6442 if (context.HasVirtualText(this))
6443 {
6444 if (!context.GetVirtualText(this, str) || str.Length() != m_text.Length())
6445 str = m_text;
6446 }
6447
6448 // Replace line break characters with spaces
ff76711f
JS
6449 wxString toRemove = wxRichTextLineBreakChar;
6450 str.Replace(toRemove, wxT(" "));
d07f2e19 6451 if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & (wxTEXT_ATTR_EFFECT_CAPITALS|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS)))
c025e094 6452 str.MakeUpper();
3e541562 6453
5d7836c4 6454 long len = range.GetLength();
ff76711f 6455 wxString stringChunk = str.Mid(range.GetStart() - offset, (size_t) len);
5d7836c4 6456
5d7836c4
JS
6457 // Test for the optimized situations where all is selected, or none
6458 // is selected.
6459
30bf7630
JS
6460 wxFont textFont(GetBuffer()->GetFontTable().FindFont(textAttr));
6461 wxCheckSetFont(dc, textFont);
6462 int charHeight = dc.GetCharHeight();
6463
6464 int x, y;
a1b806b9 6465 if ( textFont.IsOk() )
30bf7630 6466 {
d07f2e19
JS
6467 if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SMALL_CAPITALS))
6468 {
6469 textFont.SetPointSize((int) (textFont.GetPointSize()*0.75));
6470 wxCheckSetFont(dc, textFont);
6471 charHeight = dc.GetCharHeight();
6472 }
6473
30bf7630
JS
6474 if ( textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT) )
6475 {
32423dd8
JS
6476 if (textFont.IsUsingSizeInPixels())
6477 {
6478 double size = static_cast<double>(textFont.GetPixelSize().y) / wxSCRIPT_MUL_FACTOR;
4ba36292 6479 textFont.SetPixelSize(wxSize(0, static_cast<int>(size)));
32423dd8
JS
6480 x = rect.x;
6481 y = rect.y;
6482 }
6483 else
6484 {
6485 double size = static_cast<double>(textFont.GetPointSize()) / wxSCRIPT_MUL_FACTOR;
4ba36292 6486 textFont.SetPointSize(static_cast<int>(size));
32423dd8
JS
6487 x = rect.x;
6488 y = rect.y;
6489 }
30bf7630
JS
6490 wxCheckSetFont(dc, textFont);
6491 }
6492 else if ( textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) )
6493 {
32423dd8
JS
6494 if (textFont.IsUsingSizeInPixels())
6495 {
6496 double size = static_cast<double>(textFont.GetPixelSize().y) / wxSCRIPT_MUL_FACTOR;
6497 textFont.SetPixelSize(wxSize(0, static_cast<int>(size)));
6498 x = rect.x;
4ba36292 6499 int sub_height = static_cast<int>(static_cast<double>(charHeight) / wxSCRIPT_MUL_FACTOR);
32423dd8
JS
6500 y = rect.y + (rect.height - sub_height + (descent - m_descent));
6501 }
6502 else
6503 {
6504 double size = static_cast<double>(textFont.GetPointSize()) / wxSCRIPT_MUL_FACTOR;
4ba36292 6505 textFont.SetPointSize(static_cast<int>(size));
32423dd8 6506 x = rect.x;
4ba36292 6507 int sub_height = static_cast<int>(static_cast<double>(charHeight) / wxSCRIPT_MUL_FACTOR);
32423dd8
JS
6508 y = rect.y + (rect.height - sub_height + (descent - m_descent));
6509 }
30bf7630
JS
6510 wxCheckSetFont(dc, textFont);
6511 }
6512 else
6513 {
6514 x = rect.x;
6515 y = rect.y + (rect.height - charHeight - (descent - m_descent));
6516 }
6517 }
6518 else
6519 {
6520 x = rect.x;
6521 y = rect.y + (rect.height - charHeight - (descent - m_descent));
6522 }
5d7836c4 6523
603f702b
JS
6524 // TODO: new selection code
6525
5d7836c4
JS
6526 // (a) All selected.
6527 if (selectionRange.GetStart() <= range.GetStart() && selectionRange.GetEnd() >= range.GetEnd())
ab14c7aa 6528 {
fe5aa22c 6529 DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, true);
5d7836c4
JS
6530 }
6531 // (b) None selected.
6532 else if (selectionRange.GetEnd() < range.GetStart() || selectionRange.GetStart() > range.GetEnd())
6533 {
6534 // Draw all unselected
fe5aa22c 6535 DrawTabbedString(dc, textAttr, rect, stringChunk, x, y, false);
5d7836c4
JS
6536 }
6537 else
6538 {
6539 // (c) Part selected, part not
6540 // Let's draw unselected chunk, selected chunk, then unselected chunk.
6541
04ee05f9 6542 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
7fe8059f 6543
5d7836c4
JS
6544 // 1. Initial unselected chunk, if any, up until start of selection.
6545 if (selectionRange.GetStart() > range.GetStart() && selectionRange.GetStart() <= range.GetEnd())
6546 {
6547 int r1 = range.GetStart();
6548 int s1 = selectionRange.GetStart()-1;
6549 int fragmentLen = s1 - r1 + 1;
6550 if (fragmentLen < 0)
af588446 6551 {
5d7836c4 6552 wxLogDebug(wxT("Mid(%d, %d"), (int)(r1 - offset), (int)fragmentLen);
af588446 6553 }
ff76711f 6554 wxString stringFragment = str.Mid(r1 - offset, fragmentLen);
5d7836c4 6555
fe5aa22c 6556 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
cfa3b256
JS
6557
6558#if USE_KERNING_FIX
6559 if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
6560 {
6561 // Compensate for kerning difference
ff76711f
JS
6562 wxString stringFragment2(str.Mid(r1 - offset, fragmentLen+1));
6563 wxString stringFragment3(str.Mid(r1 - offset + fragmentLen, 1));
41a85215 6564
cfa3b256
JS
6565 wxCoord w1, h1, w2, h2, w3, h3;
6566 dc.GetTextExtent(stringFragment, & w1, & h1);
6567 dc.GetTextExtent(stringFragment2, & w2, & h2);
6568 dc.GetTextExtent(stringFragment3, & w3, & h3);
41a85215 6569
cfa3b256
JS
6570 int kerningDiff = (w1 + w3) - w2;
6571 x = x - kerningDiff;
6572 }
6573#endif
5d7836c4
JS
6574 }
6575
6576 // 2. Selected chunk, if any.
6577 if (selectionRange.GetEnd() >= range.GetStart())
6578 {
6579 int s1 = wxMax(selectionRange.GetStart(), range.GetStart());
6580 int s2 = wxMin(selectionRange.GetEnd(), range.GetEnd());
6581
6582 int fragmentLen = s2 - s1 + 1;
6583 if (fragmentLen < 0)
af588446 6584 {
5d7836c4 6585 wxLogDebug(wxT("Mid(%d, %d"), (int)(s1 - offset), (int)fragmentLen);
af588446 6586 }
ff76711f 6587 wxString stringFragment = str.Mid(s1 - offset, fragmentLen);
5d7836c4 6588
fe5aa22c 6589 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, true);
cfa3b256
JS
6590
6591#if USE_KERNING_FIX
6592 if (stringChunk.Find(wxT("\t")) == wxNOT_FOUND)
6593 {
6594 // Compensate for kerning difference
ff76711f
JS
6595 wxString stringFragment2(str.Mid(s1 - offset, fragmentLen+1));
6596 wxString stringFragment3(str.Mid(s1 - offset + fragmentLen, 1));
41a85215 6597
cfa3b256
JS
6598 wxCoord w1, h1, w2, h2, w3, h3;
6599 dc.GetTextExtent(stringFragment, & w1, & h1);
6600 dc.GetTextExtent(stringFragment2, & w2, & h2);
6601 dc.GetTextExtent(stringFragment3, & w3, & h3);
41a85215 6602
cfa3b256
JS
6603 int kerningDiff = (w1 + w3) - w2;
6604 x = x - kerningDiff;
6605 }
6606#endif
5d7836c4
JS
6607 }
6608
6609 // 3. Remaining unselected chunk, if any
6610 if (selectionRange.GetEnd() < range.GetEnd())
6611 {
6612 int s2 = wxMin(selectionRange.GetEnd()+1, range.GetEnd());
6613 int r2 = range.GetEnd();
6614
6615 int fragmentLen = r2 - s2 + 1;
6616 if (fragmentLen < 0)
af588446 6617 {
5d7836c4 6618 wxLogDebug(wxT("Mid(%d, %d"), (int)(s2 - offset), (int)fragmentLen);
af588446 6619 }
ff76711f 6620 wxString stringFragment = str.Mid(s2 - offset, fragmentLen);
ab14c7aa 6621
fe5aa22c 6622 DrawTabbedString(dc, textAttr, rect, stringFragment, x, y, false);
7fe8059f 6623 }
5d7836c4
JS
6624 }
6625
6626 return true;
6627}
61399247 6628
24777478 6629bool wxRichTextPlainText::DrawTabbedString(wxDC& dc, const wxRichTextAttr& attr, const wxRect& rect,wxString& str, wxCoord& x, wxCoord& y, bool selected)
7f0d9d71 6630{
cfa3b256
JS
6631 bool hasTabs = (str.Find(wxT('\t')) != wxNOT_FOUND);
6632
6633 wxArrayInt tabArray;
6634 int tabCount;
6635 if (hasTabs)
ab14c7aa 6636 {
cfa3b256
JS
6637 if (attr.GetTabs().IsEmpty())
6638 tabArray = wxRichTextParagraph::GetDefaultTabs();
6639 else
6640 tabArray = attr.GetTabs();
6641 tabCount = tabArray.GetCount();
6642
6643 for (int i = 0; i < tabCount; ++i)
ab14c7aa 6644 {
cfa3b256
JS
6645 int pos = tabArray[i];
6646 pos = ConvertTenthsMMToPixels(dc, pos);
6647 tabArray[i] = pos;
7f0d9d71
JS
6648 }
6649 }
cfa3b256
JS
6650 else
6651 tabCount = 0;
ab14c7aa 6652
cfa3b256
JS
6653 int nextTabPos = -1;
6654 int tabPos = -1;
7f0d9d71 6655 wxCoord w, h;
ab14c7aa 6656
cfa3b256 6657 if (selected)
ab14c7aa 6658 {
0ec6da02
JS
6659 wxColour highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
6660 wxColour highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
6661
ecb5fbf1
JS
6662 wxCheckSetBrush(dc, wxBrush(highlightColour));
6663 wxCheckSetPen(dc, wxPen(highlightColour));
0ec6da02 6664 dc.SetTextForeground(highlightTextColour);
04ee05f9 6665 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
7f0d9d71 6666 }
ab14c7aa
JS
6667 else
6668 {
fe5aa22c 6669 dc.SetTextForeground(attr.GetTextColour());
ab14c7aa 6670
f0e9eda2
JS
6671 if (attr.HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR) && attr.GetBackgroundColour().IsOk())
6672 {
04ee05f9 6673 dc.SetBackgroundMode(wxBRUSHSTYLE_SOLID);
f0e9eda2
JS
6674 dc.SetTextBackground(attr.GetBackgroundColour());
6675 }
6676 else
04ee05f9 6677 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
3e541562 6678 }
3e541562 6679
925a662a 6680 wxCoord x_orig = GetParent()->GetPosition().x;
cfa3b256 6681 while (hasTabs)
ab14c7aa
JS
6682 {
6683 // the string has a tab
7f0d9d71
JS
6684 // break up the string at the Tab
6685 wxString stringChunk = str.BeforeFirst(wxT('\t'));
6686 str = str.AfterFirst(wxT('\t'));
6687 dc.GetTextExtent(stringChunk, & w, & h);
cfa3b256 6688 tabPos = x + w;
7f0d9d71 6689 bool not_found = true;
cfa3b256 6690 for (int i = 0; i < tabCount && not_found; ++i)
ab14c7aa 6691 {
015d0446 6692 nextTabPos = tabArray.Item(i) + x_orig;
4794d69c
JS
6693
6694 // Find the next tab position.
6695 // Even if we're at the end of the tab array, we must still draw the chunk.
6696
6697 if (nextTabPos > tabPos || (i == (tabCount - 1)))
ab14c7aa 6698 {
4794d69c
JS
6699 if (nextTabPos <= tabPos)
6700 {
6701 int defaultTabWidth = ConvertTenthsMMToPixels(dc, WIDTH_FOR_DEFAULT_TABS);
6702 nextTabPos = tabPos + defaultTabWidth;
6703 }
6704
7f0d9d71 6705 not_found = false;
ab14c7aa
JS
6706 if (selected)
6707 {
cfa3b256 6708 w = nextTabPos - x;
7f0d9d71 6709 wxRect selRect(x, rect.y, w, rect.GetHeight());
61399247 6710 dc.DrawRectangle(selRect);
7f0d9d71
JS
6711 }
6712 dc.DrawText(stringChunk, x, y);
42688aea
JS
6713
6714 if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH))
6715 {
6716 wxPen oldPen = dc.GetPen();
ecb5fbf1 6717 wxCheckSetPen(dc, wxPen(attr.GetTextColour(), 1));
42688aea 6718 dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5));
ecb5fbf1 6719 wxCheckSetPen(dc, oldPen);
42688aea
JS
6720 }
6721
cfa3b256 6722 x = nextTabPos;
7f0d9d71
JS
6723 }
6724 }
cfa3b256 6725 hasTabs = (str.Find(wxT('\t')) != wxNOT_FOUND);
7f0d9d71 6726 }
61399247 6727
cfa3b256 6728 if (!str.IsEmpty())
ab14c7aa 6729 {
cfa3b256
JS
6730 dc.GetTextExtent(str, & w, & h);
6731 if (selected)
6732 {
6733 wxRect selRect(x, rect.y, w, rect.GetHeight());
6734 dc.DrawRectangle(selRect);
6735 }
6736 dc.DrawText(str, x, y);
42688aea
JS
6737
6738 if (attr.HasTextEffects() && (attr.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH))
6739 {
6740 wxPen oldPen = dc.GetPen();
ecb5fbf1 6741 wxCheckSetPen(dc, wxPen(attr.GetTextColour(), 1));
42688aea 6742 dc.DrawLine(x, (int) (y+(h/2)+0.5), x+w, (int) (y+(h/2)+0.5));
ecb5fbf1 6743 wxCheckSetPen(dc, oldPen);
42688aea
JS
6744 }
6745
cfa3b256 6746 x += w;
7f0d9d71 6747 }
5d7836c4 6748
7c9fdebe 6749 return true;
7f0d9d71 6750}
fe5aa22c 6751
5d7836c4 6752/// Lay the item out
8db2e3ef 6753bool wxRichTextPlainText::Layout(wxDC& dc, wxRichTextDrawingContext& context, const wxRect& WXUNUSED(rect), const wxRect& WXUNUSED(parentRect), int WXUNUSED(style))
5d7836c4 6754{
ecb5fbf1
JS
6755 // Only lay out if we haven't already cached the size
6756 if (m_size.x == -1)
8db2e3ef 6757 GetRangeSize(GetRange(), m_size, m_descent, dc, context, 0, wxPoint(0, 0));
603f702b
JS
6758 m_maxSize = m_size;
6759 // Eventually we want to have a reasonable estimate of minimum size.
6760 m_minSize = wxSize(0, 0);
5d7836c4
JS
6761 return true;
6762}
6763
6764/// Copy
6765void wxRichTextPlainText::Copy(const wxRichTextPlainText& obj)
6766{
6767 wxRichTextObject::Copy(obj);
6768
6769 m_text = obj.m_text;
6770}
6771
6772/// Get/set the object size for the given range. Returns false if the range
6773/// is invalid for this object.
914a4e23 6774bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, wxRichTextDrawingContext& context, int WXUNUSED(flags), const wxPoint& position, const wxSize& WXUNUSED(parentSize), wxArrayInt* partialExtents) const
5d7836c4
JS
6775{
6776 if (!range.IsWithin(GetRange()))
6777 return false;
6778
fe5aa22c
JS
6779 wxRichTextParagraph* para = wxDynamicCast(GetParent(), wxRichTextParagraph);
6780 wxASSERT (para != NULL);
603f702b 6781
925a662a 6782 int relativeX = position.x - GetParent()->GetPosition().x;
fe5aa22c 6783
24777478 6784 wxRichTextAttr textAttr(para ? para->GetCombinedAttributes(GetAttributes()) : GetAttributes());
8db2e3ef 6785 context.ApplyVirtualAttributes(textAttr, (wxRichTextObject*) this);
fe5aa22c 6786
5d7836c4
JS
6787 // Always assume unformatted text, since at this level we have no knowledge
6788 // of line breaks - and we don't need it, since we'll calculate size within
6789 // formatted text by doing it in chunks according to the line ranges
6790
30bf7630 6791 bool bScript(false);
44cc96a8 6792 wxFont font(GetBuffer()->GetFontTable().FindFont(textAttr));
a1b806b9 6793 if (font.IsOk())
30bf7630
JS
6794 {
6795 if ( textAttr.HasTextEffects() && ( (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT)
6796 || (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT) ) )
6797 {
6798 wxFont textFont = font;
32423dd8
JS
6799 if (textFont.IsUsingSizeInPixels())
6800 {
6801 double size = static_cast<double>(textFont.GetPixelSize().y) / wxSCRIPT_MUL_FACTOR;
6802 textFont.SetPixelSize(wxSize(0, static_cast<int>(size)));
6803 }
6804 else
6805 {
6806 double size = static_cast<double>(textFont.GetPointSize()) / wxSCRIPT_MUL_FACTOR;
6807 textFont.SetPointSize(static_cast<int>(size));
6808 }
30bf7630
JS
6809 wxCheckSetFont(dc, textFont);
6810 bScript = true;
6811 }
d07f2e19
JS
6812 else if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SMALL_CAPITALS))
6813 {
6814 wxFont textFont = font;
6815 textFont.SetPointSize((int) (textFont.GetPointSize()*0.75));
6816 wxCheckSetFont(dc, textFont);
6817 bScript = true;
6818 }
30bf7630
JS
6819 else
6820 {
6821 wxCheckSetFont(dc, font);
6822 }
6823 }
5d7836c4 6824
109bfc88 6825 bool haveDescent = false;
5d7836c4
JS
6826 int startPos = range.GetStart() - GetRange().GetStart();
6827 long len = range.GetLength();
3e541562 6828
ff76711f 6829 wxString str(m_text);
f7667b84
JS
6830 if (context.HasVirtualText(this))
6831 {
6832 if (!context.GetVirtualText(this, str) || str.Length() != m_text.Length())
6833 str = m_text;
6834 }
6835
ff76711f
JS
6836 wxString toReplace = wxRichTextLineBreakChar;
6837 str.Replace(toReplace, wxT(" "));
6838
6839 wxString stringChunk = str.Mid(startPos, (size_t) len);
42688aea 6840
d07f2e19 6841 if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & (wxTEXT_ATTR_EFFECT_CAPITALS|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS)))
42688aea
JS
6842 stringChunk.MakeUpper();
6843
5d7836c4 6844 wxCoord w, h;
7f0d9d71 6845 int width = 0;
cfa3b256 6846 if (stringChunk.Find(wxT('\t')) != wxNOT_FOUND)
ab14c7aa
JS
6847 {
6848 // the string has a tab
cfa3b256
JS
6849 wxArrayInt tabArray;
6850 if (textAttr.GetTabs().IsEmpty())
6851 tabArray = wxRichTextParagraph::GetDefaultTabs();
6852 else
6853 tabArray = textAttr.GetTabs();
ab14c7aa 6854
cfa3b256 6855 int tabCount = tabArray.GetCount();
41a85215 6856
cfa3b256 6857 for (int i = 0; i < tabCount; ++i)
61399247 6858 {
cfa3b256
JS
6859 int pos = tabArray[i];
6860 pos = ((wxRichTextPlainText*) this)->ConvertTenthsMMToPixels(dc, pos);
6861 tabArray[i] = pos;
7f0d9d71 6862 }
41a85215 6863
cfa3b256 6864 int nextTabPos = -1;
61399247 6865
ab14c7aa
JS
6866 while (stringChunk.Find(wxT('\t')) >= 0)
6867 {
109bfc88
JS
6868 int absoluteWidth = 0;
6869
ab14c7aa 6870 // the string has a tab
7f0d9d71
JS
6871 // break up the string at the Tab
6872 wxString stringFragment = stringChunk.BeforeFirst(wxT('\t'));
6873 stringChunk = stringChunk.AfterFirst(wxT('\t'));
4794d69c 6874
31778480
JS
6875 if (partialExtents)
6876 {
109bfc88
JS
6877 int oldWidth;
6878 if (partialExtents->GetCount() > 0)
6879 oldWidth = (*partialExtents)[partialExtents->GetCount()-1];
6880 else
6881 oldWidth = 0;
6882
31778480
JS
6883 // Add these partial extents
6884 wxArrayInt p;
6885 dc.GetPartialTextExtents(stringFragment, p);
6886 size_t j;
6887 for (j = 0; j < p.GetCount(); j++)
6888 partialExtents->Add(oldWidth + p[j]);
109bfc88
JS
6889
6890 if (partialExtents->GetCount() > 0)
925a662a 6891 absoluteWidth = (*partialExtents)[(*partialExtents).GetCount()-1] + relativeX;
109bfc88 6892 else
925a662a 6893 absoluteWidth = relativeX;
109bfc88
JS
6894 }
6895 else
6896 {
6897 dc.GetTextExtent(stringFragment, & w, & h);
6898 width += w;
603f702b 6899 absoluteWidth = width + relativeX;
109bfc88 6900 haveDescent = true;
31778480
JS
6901 }
6902
cfa3b256
JS
6903 bool notFound = true;
6904 for (int i = 0; i < tabCount && notFound; ++i)
ab14c7aa 6905 {
cfa3b256 6906 nextTabPos = tabArray.Item(i);
4794d69c
JS
6907
6908 // Find the next tab position.
6909 // Even if we're at the end of the tab array, we must still process the chunk.
6910
6911 if (nextTabPos > absoluteWidth || (i == (tabCount - 1)))
ab14c7aa 6912 {
4794d69c
JS
6913 if (nextTabPos <= absoluteWidth)
6914 {
6915 int defaultTabWidth = ((wxRichTextPlainText*) this)->ConvertTenthsMMToPixels(dc, WIDTH_FOR_DEFAULT_TABS);
6916 nextTabPos = absoluteWidth + defaultTabWidth;
6917 }
6918
cfa3b256 6919 notFound = false;
925a662a 6920 width = nextTabPos - relativeX;
31778480
JS
6921
6922 if (partialExtents)
6923 partialExtents->Add(width);
7f0d9d71
JS
6924 }
6925 }
6926 }
6927 }
30bf7630 6928
31778480
JS
6929 if (!stringChunk.IsEmpty())
6930 {
31778480
JS
6931 if (partialExtents)
6932 {
109bfc88
JS
6933 int oldWidth;
6934 if (partialExtents->GetCount() > 0)
6935 oldWidth = (*partialExtents)[partialExtents->GetCount()-1];
6936 else
6937 oldWidth = 0;
6938
31778480
JS
6939 // Add these partial extents
6940 wxArrayInt p;
6941 dc.GetPartialTextExtents(stringChunk, p);
6942 size_t j;
6943 for (j = 0; j < p.GetCount(); j++)
6944 partialExtents->Add(oldWidth + p[j]);
6945 }
109bfc88
JS
6946 else
6947 {
6948 dc.GetTextExtent(stringChunk, & w, & h, & descent);
6949 width += w;
6950 haveDescent = true;
6951 }
6952 }
6953
6954 if (partialExtents)
6955 {
6956 int charHeight = dc.GetCharHeight();
6957 if ((*partialExtents).GetCount() > 0)
6958 w = (*partialExtents)[partialExtents->GetCount()-1];
6959 else
6960 w = 0;
6961 size = wxSize(w, charHeight);
6962 }
6963 else
6964 {
6965 size = wxSize(width, dc.GetCharHeight());
31778480 6966 }
30bf7630 6967
109bfc88
JS
6968 if (!haveDescent)
6969 dc.GetTextExtent(wxT("X"), & w, & h, & descent);
6970
30bf7630
JS
6971 if ( bScript )
6972 dc.SetFont(font);
6973
5d7836c4
JS
6974 return true;
6975}
6976
6977/// Do a split, returning an object containing the second part, and setting
6978/// the first part in 'this'.
6979wxRichTextObject* wxRichTextPlainText::DoSplit(long pos)
6980{
ff76711f 6981 long index = pos - GetRange().GetStart();
3e541562 6982
28f92d74 6983 if (index < 0 || index >= (int) m_text.length())
5d7836c4
JS
6984 return NULL;
6985
6986 wxString firstPart = m_text.Mid(0, index);
6987 wxString secondPart = m_text.Mid(index);
6988
6989 m_text = firstPart;
6990
6991 wxRichTextPlainText* newObject = new wxRichTextPlainText(secondPart);
6992 newObject->SetAttributes(GetAttributes());
8db2e3ef 6993 newObject->SetProperties(GetProperties());
5d7836c4
JS
6994
6995 newObject->SetRange(wxRichTextRange(pos, GetRange().GetEnd()));
6996 GetRange().SetEnd(pos-1);
3e541562 6997
5d7836c4
JS
6998 return newObject;
6999}
7000
7001/// Calculate range
7002void wxRichTextPlainText::CalculateRange(long start, long& end)
7003{
28f92d74 7004 end = start + m_text.length() - 1;
5d7836c4
JS
7005 m_range.SetRange(start, end);
7006}
7007
7008/// Delete range
7009bool wxRichTextPlainText::DeleteRange(const wxRichTextRange& range)
7010{
7011 wxRichTextRange r = range;
7012
7013 r.LimitTo(GetRange());
7014
7015 if (r.GetStart() == GetRange().GetStart() && r.GetEnd() == GetRange().GetEnd())
7016 {
7017 m_text.Empty();
7018 return true;
7019 }
7020
7021 long startIndex = r.GetStart() - GetRange().GetStart();
7022 long len = r.GetLength();
7023
7024 m_text = m_text.Mid(0, startIndex) + m_text.Mid(startIndex+len);
7025 return true;
7026}
7027
7028/// Get text for the given range.
7029wxString wxRichTextPlainText::GetTextForRange(const wxRichTextRange& range) const
7030{
7031 wxRichTextRange r = range;
7032
7033 r.LimitTo(GetRange());
7034
7035 long startIndex = r.GetStart() - GetRange().GetStart();
7036 long len = r.GetLength();
7037
7038 return m_text.Mid(startIndex, len);
7039}
7040
7041/// Returns true if this object can merge itself with the given one.
f7667b84 7042bool wxRichTextPlainText::CanMerge(wxRichTextObject* object, wxRichTextDrawingContext& context) const
5d7836c4 7043{
f7667b84
JS
7044 // JACS 2013-01-27
7045 if (!context.GetVirtualAttributesEnabled())
7046 {
7047 return object->GetClassInfo() == wxCLASSINFO(wxRichTextPlainText) &&
7048 (m_text.empty() || (wxTextAttrEq(GetAttributes(), object->GetAttributes()) && m_properties == object->GetProperties()));
7049 }
7050 else
7051 {
7052 wxRichTextPlainText* otherObj = wxDynamicCast(object, wxRichTextPlainText);
7053 if (!otherObj || m_text.empty())
7054 return false;
7055
7056 if (!wxTextAttrEq(GetAttributes(), object->GetAttributes()) || !(m_properties == object->GetProperties()))
7057 return false;
7058
7059 // Check if differing virtual attributes makes it impossible to merge
7060 // these strings.
7061
7062 bool hasVirtualAttr1 = context.HasVirtualAttributes((wxRichTextObject*) this);
7063 bool hasVirtualAttr2 = context.HasVirtualAttributes((wxRichTextObject*) object);
7064 if (!hasVirtualAttr1 && !hasVirtualAttr2)
7065 return true;
7066 else if (hasVirtualAttr1 != hasVirtualAttr2)
7067 return false;
7068 else
7069 {
7070 wxRichTextAttr virtualAttr1 = context.GetVirtualAttributes((wxRichTextObject*) this);
7071 wxRichTextAttr virtualAttr2 = context.GetVirtualAttributes((wxRichTextObject*) object);
7072 return virtualAttr1 == virtualAttr2;
7073 }
7074 }
5d7836c4
JS
7075}
7076
7077/// Returns true if this object merged itself with the given one.
7078/// The calling code will then delete the given object.
f7667b84 7079bool wxRichTextPlainText::Merge(wxRichTextObject* object, wxRichTextDrawingContext& WXUNUSED(context))
5d7836c4
JS
7080{
7081 wxRichTextPlainText* textObject = wxDynamicCast(object, wxRichTextPlainText);
7082 wxASSERT( textObject != NULL );
7083
7084 if (textObject)
7085 {
7086 m_text += textObject->GetText();
99404ab0 7087 wxRichTextApplyStyle(m_attributes, textObject->GetAttributes());
5d7836c4
JS
7088 return true;
7089 }
7090 else
7091 return false;
7092}
7093
f7667b84
JS
7094bool wxRichTextPlainText::CanSplit(wxRichTextDrawingContext& context) const
7095{
7096 // If this object has any virtual attributes at all, whether for the whole object
7097 // or individual ones, we should try splitting it by calling Split.
7098 // Must be more than one character in order to be able to split.
7099 return m_text.Length() > 1 && context.HasVirtualAttributes((wxRichTextObject*) this);
7100}
7101
7102wxRichTextObject* wxRichTextPlainText::Split(wxRichTextDrawingContext& context)
7103{
7104 int count = context.GetVirtualSubobjectAttributesCount(this);
7105 if (count > 0 && GetParent())
7106 {
7107 wxRichTextCompositeObject* parent = wxDynamicCast(GetParent(), wxRichTextCompositeObject);
7108 wxRichTextObjectList::compatibility_iterator node = parent->GetChildren().Find(this);
7109 if (node)
7110 {
7111 const wxRichTextAttr emptyAttr;
7112 wxRichTextObjectList::compatibility_iterator next = node->GetNext();
7113
7114 wxArrayInt positions;
7115 wxRichTextAttrArray attributes;
7116 if (context.GetVirtualSubobjectAttributes(this, positions, attributes) && positions.GetCount() > 0)
7117 {
7118 wxASSERT(positions.GetCount() == attributes.GetCount());
7119
7120 // We will gather up runs of text with the same virtual attributes
7121
7122 int len = m_text.Length();
7123 int i = 0;
7124
7125 // runStart and runEnd represent the accumulated run with a consistent attribute
7126 // that hasn't yet been appended
7127 int runStart = -1;
7128 int runEnd = -1;
7129 wxRichTextAttr currentAttr;
7130 wxString text = m_text;
7131 wxRichTextPlainText* lastPlainText = this;
7132
7133 for (i = 0; i < (int) positions.GetCount(); i++)
7134 {
7135 int pos = positions[i];
7136 wxASSERT(pos >= 0 && pos < len);
7137 if (pos >= 0 && pos < len)
7138 {
7139 const wxRichTextAttr& attr = attributes[i];
7140
7141 if (pos == 0)
7142 {
7143 runStart = 0;
7144 currentAttr = attr;
7145 }
7146 // Check if there was a gap from the last known attribute and this.
7147 // In that case, we need to do something with the span of non-attributed text.
7148 else if ((pos-1) > runEnd)
7149 {
7150 if (runEnd == -1)
7151 {
7152 // We hadn't processed anything previously, so the previous run is from the text start
7153 // to just before this position. The current attribute remains empty.
7154 runStart = 0;
7155 runEnd = pos-1;
7156 }
7157 else
7158 {
7159 // If the previous attribute matches the gap's attribute (i.e., no attributes)
7160 // then just extend the run.
7161 if (currentAttr.IsDefault())
7162 {
7163 runEnd = pos-1;
7164 }
7165 else
7166 {
7167 // We need to add an object, or reuse the existing one.
7168 if (runStart == 0)
7169 {
7170 lastPlainText = this;
7171 SetText(text.Mid(runStart, runEnd - runStart + 1));
7172 }
7173 else
7174 {
7175 wxRichTextPlainText* obj = new wxRichTextPlainText;
7176 lastPlainText = obj;
7177 obj->SetAttributes(GetAttributes());
7178 obj->SetProperties(GetProperties());
7179 obj->SetParent(parent);
7180
7181 obj->SetText(text.Mid(runStart, runEnd - runStart + 1));
7182 if (next)
7183 parent->GetChildren().Insert(next, obj);
7184 else
7185 parent->GetChildren().Append(obj);
7186 }
7187
7188 runStart = runEnd+1;
7189 runEnd = pos-1;
7190
7191 currentAttr = emptyAttr;
7192 }
7193 }
7194 }
7195
7196 wxASSERT(runEnd == pos-1);
7197
7198 // Now we only have to deal with the previous run
7199 if (currentAttr == attr)
7200 {
7201 // If we still have the same attributes, then we
7202 // simply increase the run size.
7203 runEnd = pos;
7204 }
7205 else
7206 {
7207 if (runEnd >= 0)
7208 {
7209 // We need to add an object, or reuse the existing one.
7210 if (runStart == 0)
7211 {
7212 lastPlainText = this;
7213 SetText(text.Mid(runStart, runEnd - runStart + 1));
7214 }
7215 else
7216 {
7217 wxRichTextPlainText* obj = new wxRichTextPlainText;
7218 lastPlainText = obj;
7219 obj->SetAttributes(GetAttributes());
7220 obj->SetProperties(GetProperties());
7221 obj->SetParent(parent);
7222
7223 obj->SetText(text.Mid(runStart, runEnd - runStart + 1));
7224 if (next)
7225 parent->GetChildren().Insert(next, obj);
7226 else
7227 parent->GetChildren().Append(obj);
7228 }
7229 }
7230
7231 runStart = pos;
7232 runEnd = pos;
7233
7234 currentAttr = attr;
7235 }
7236 }
7237 }
7238
7239 // We may still have a run to add, and possibly a no-attribute text fragment after that.
7240 // If the whole string was already a single attribute (the run covers the whole string), don't split.
7241 if ((runStart != -1) && !(runStart == 0 && runEnd == (len-1)))
7242 {
7243 // If the current attribute is empty, merge the run with the next fragment
7244 // which by definition (because it's not specified) has empty attributes.
7245 if (currentAttr.IsDefault())
7246 runEnd = (len-1);
7247
7248 if (runEnd < (len-1))
7249 {
7250 // We need to add an object, or reuse the existing one.
7251 if (runStart == 0)
7252 {
7253 lastPlainText = this;
7254 SetText(text.Mid(runStart, runEnd - runStart + 1));
7255 }
7256 else
7257 {
7258 wxRichTextPlainText* obj = new wxRichTextPlainText;
7259 lastPlainText = obj;
7260 obj->SetAttributes(GetAttributes());
7261 obj->SetProperties(GetProperties());
7262 obj->SetParent(parent);
7263
7264 obj->SetText(text.Mid(runStart, runEnd - runStart + 1));
7265 if (next)
7266 parent->GetChildren().Insert(next, obj);
7267 else
7268 parent->GetChildren().Append(obj);
7269 }
7270
7271 runStart = runEnd+1;
7272 runEnd = (len-1);
7273 }
7274
7275 // Now the last, non-attributed fragment at the end, if any
7276 if ((runStart < len) && !(runStart == 0 && runEnd == (len-1)))
7277 {
7278 wxASSERT(runStart != 0);
7279
7280 wxRichTextPlainText* obj = new wxRichTextPlainText;
7281 obj->SetAttributes(GetAttributes());
7282 obj->SetProperties(GetProperties());
7283 obj->SetParent(parent);
7284
7285 obj->SetText(text.Mid(runStart, runEnd - runStart + 1));
7286 if (next)
7287 parent->GetChildren().Insert(next, obj);
7288 else
7289 parent->GetChildren().Append(obj);
7290
7291 lastPlainText = obj;
7292 }
7293 }
7294
7295 return lastPlainText;
7296 }
7297 }
7298 }
7299 return this;
7300}
7301
5d7836c4
JS
7302/// Dump to output stream for debugging
7303void wxRichTextPlainText::Dump(wxTextOutputStream& stream)
7304{
7305 wxRichTextObject::Dump(stream);
7306 stream << m_text << wxT("\n");
7307}
7308
ff76711f
JS
7309/// Get the first position from pos that has a line break character.
7310long wxRichTextPlainText::GetFirstLineBreakPosition(long pos)
7311{
7312 int i;
7313 int len = m_text.length();
7314 int startPos = pos - m_range.GetStart();
7315 for (i = startPos; i < len; i++)
7316 {
7317 wxChar ch = m_text[i];
7318 if (ch == wxRichTextLineBreakChar)
7319 {
7320 return i + m_range.GetStart();
7321 }
7322 }
7323 return -1;
7324}
7325
5d7836c4
JS
7326/*!
7327 * wxRichTextBuffer
7328 * This is a kind of box, used to represent the whole buffer
7329 */
7330
7331IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer, wxRichTextParagraphLayoutBox)
7332
7c9fdebe
JS
7333wxList wxRichTextBuffer::sm_handlers;
7334wxList wxRichTextBuffer::sm_drawingHandlers;
7335wxRichTextFieldTypeHashMap wxRichTextBuffer::sm_fieldTypes;
7336wxRichTextRenderer* wxRichTextBuffer::sm_renderer = NULL;
7337int wxRichTextBuffer::sm_bulletRightMargin = 20;
7338float wxRichTextBuffer::sm_bulletProportion = (float) 0.3;
e12b91a3 7339bool wxRichTextBuffer::sm_floatingLayoutMode = true;
5d7836c4
JS
7340
7341/// Initialisation
7342void wxRichTextBuffer::Init()
7343{
7344 m_commandProcessor = new wxCommandProcessor;
7345 m_styleSheet = NULL;
7346 m_modified = false;
7347 m_batchedCommandDepth = 0;
7348 m_batchedCommand = NULL;
7349 m_suppressUndo = 0;
d2d0adc7 7350 m_handlerFlags = 0;
44219ff0 7351 m_scale = 1.0;
32423dd8
JS
7352 m_dimensionScale = 1.0;
7353 m_fontScale = 1.0;
f819ed5d 7354 SetMargins(4);
5d7836c4
JS
7355}
7356
7357/// Initialisation
7358wxRichTextBuffer::~wxRichTextBuffer()
7359{
7360 delete m_commandProcessor;
7361 delete m_batchedCommand;
7362
7363 ClearStyleStack();
d2d0adc7 7364 ClearEventHandlers();
5d7836c4
JS
7365}
7366
85d8909b 7367void wxRichTextBuffer::ResetAndClearCommands()
5d7836c4 7368{
85d8909b 7369 Reset();
3e541562 7370
5d7836c4 7371 GetCommandProcessor()->ClearCommands();
5d7836c4 7372
5d7836c4 7373 Modify(false);
1e967276 7374 Invalidate(wxRICHTEXT_ALL);
5d7836c4
JS
7375}
7376
0ca07313
JS
7377void wxRichTextBuffer::Copy(const wxRichTextBuffer& obj)
7378{
7379 wxRichTextParagraphLayoutBox::Copy(obj);
7380
7381 m_styleSheet = obj.m_styleSheet;
7382 m_modified = obj.m_modified;
bec80f4f
JS
7383 m_batchedCommandDepth = 0;
7384 if (m_batchedCommand)
7385 delete m_batchedCommand;
7386 m_batchedCommand = NULL;
0ca07313 7387 m_suppressUndo = obj.m_suppressUndo;
603f702b 7388 m_invalidRange = obj.m_invalidRange;
32423dd8
JS
7389 m_dimensionScale = obj.m_dimensionScale;
7390 m_fontScale = obj.m_fontScale;
0ca07313
JS
7391}
7392
38f833b1
JS
7393/// Push style sheet to top of stack
7394bool wxRichTextBuffer::PushStyleSheet(wxRichTextStyleSheet* styleSheet)
7395{
7396 if (m_styleSheet)
7397 styleSheet->InsertSheet(m_styleSheet);
7398
7399 SetStyleSheet(styleSheet);
41a85215 7400
38f833b1
JS
7401 return true;
7402}
7403
7404/// Pop style sheet from top of stack
7405wxRichTextStyleSheet* wxRichTextBuffer::PopStyleSheet()
7406{
7407 if (m_styleSheet)
7408 {
7409 wxRichTextStyleSheet* oldSheet = m_styleSheet;
7410 m_styleSheet = oldSheet->GetNextSheet();
7411 oldSheet->Unlink();
41a85215 7412
38f833b1
JS
7413 return oldSheet;
7414 }
7415 else
7416 return NULL;
7417}
7418
0ca07313
JS
7419/// Submit command to insert paragraphs
7420bool wxRichTextBuffer::InsertParagraphsWithUndo(long pos, const wxRichTextParagraphLayoutBox& paragraphs, wxRichTextCtrl* ctrl, int flags)
7421{
4e63bfb9 7422 return ctrl->GetFocusObject()->InsertParagraphsWithUndo(this, pos, paragraphs, ctrl, flags);
603f702b
JS
7423}
7424
7425/// Submit command to insert paragraphs
4e63bfb9 7426bool wxRichTextParagraphLayoutBox::InsertParagraphsWithUndo(wxRichTextBuffer* buffer, long pos, const wxRichTextParagraphLayoutBox& paragraphs, wxRichTextCtrl* ctrl, int WXUNUSED(flags))
603f702b
JS
7427{
7428 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, buffer, this, ctrl, false);
0ca07313 7429
0ca07313 7430 action->GetNewParagraphs() = paragraphs;
59509217 7431
0ca07313
JS
7432 action->SetPosition(pos);
7433
603f702b 7434 wxRichTextRange range = wxRichTextRange(pos, pos + paragraphs.GetOwnRange().GetEnd() - 1);
99404ab0
JS
7435 if (!paragraphs.GetPartialParagraph())
7436 range.SetEnd(range.GetEnd()+1);
7437
0ca07313 7438 // Set the range we'll need to delete in Undo
99404ab0 7439 action->SetRange(range);
0ca07313 7440
603f702b 7441 buffer->SubmitAction(action);
0ca07313
JS
7442
7443 return true;
7444}
7445
5d7836c4 7446/// Submit command to insert the given text
fe5aa22c 7447bool wxRichTextBuffer::InsertTextWithUndo(long pos, const wxString& text, wxRichTextCtrl* ctrl, int flags)
5d7836c4 7448{
4e63bfb9 7449 return ctrl->GetFocusObject()->InsertTextWithUndo(this, pos, text, ctrl, flags);
603f702b
JS
7450}
7451
7452/// Submit command to insert the given text
4e63bfb9 7453bool wxRichTextParagraphLayoutBox::InsertTextWithUndo(wxRichTextBuffer* buffer, long pos, const wxString& text, wxRichTextCtrl* ctrl, int flags)
603f702b
JS
7454{
7455 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, buffer, this, ctrl, false);
5d7836c4 7456
24777478
JS
7457 wxRichTextAttr* p = NULL;
7458 wxRichTextAttr paraAttr;
fe5aa22c
JS
7459 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
7460 {
7c081bd2 7461 // Get appropriate paragraph style
603f702b 7462 paraAttr = GetStyleForNewParagraph(buffer, pos, false, false);
fe5aa22c
JS
7463 if (!paraAttr.IsDefault())
7464 p = & paraAttr;
7465 }
7466
fe5aa22c 7467 action->GetNewParagraphs().AddParagraphs(text, p);
0ca07313 7468
603f702b 7469 int length = action->GetNewParagraphs().GetOwnRange().GetLength();
0ca07313 7470
6636ef8d 7471 if (!text.empty() && text.Last() != wxT('\n'))
0ca07313
JS
7472 {
7473 // Don't count the newline when undoing
7474 length --;
5d7836c4 7475 action->GetNewParagraphs().SetPartialParagraph(true);
0ca07313 7476 }
6636ef8d 7477 else if (!text.empty() && text.Last() == wxT('\n'))
46ee0e5b 7478 length --;
5d7836c4
JS
7479
7480 action->SetPosition(pos);
7481
7482 // Set the range we'll need to delete in Undo
0ca07313 7483 action->SetRange(wxRichTextRange(pos, pos + length - 1));
7fe8059f 7484
603f702b 7485 buffer->SubmitAction(action);
7fe8059f 7486
5d7836c4
JS
7487 return true;
7488}
7489
7490/// Submit command to insert the given text
fe5aa22c 7491bool wxRichTextBuffer::InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int flags)
5d7836c4 7492{
4e63bfb9 7493 return ctrl->GetFocusObject()->InsertNewlineWithUndo(this, pos, ctrl, flags);
603f702b
JS
7494}
7495
7496/// Submit command to insert the given text
4e63bfb9 7497bool wxRichTextParagraphLayoutBox::InsertNewlineWithUndo(wxRichTextBuffer* buffer, long pos, wxRichTextCtrl* ctrl, int flags)
603f702b
JS
7498{
7499 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, buffer, this, ctrl, false);
5d7836c4 7500
24777478
JS
7501 wxRichTextAttr* p = NULL;
7502 wxRichTextAttr paraAttr;
fe5aa22c
JS
7503 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
7504 {
603f702b 7505 paraAttr = GetStyleForNewParagraph(buffer, pos, false, true /* look for next paragraph style */);
fe5aa22c
JS
7506 if (!paraAttr.IsDefault())
7507 p = & paraAttr;
7508 }
7509
603f702b 7510 wxRichTextAttr attr(buffer->GetDefaultStyle());
32423dd8
JS
7511 // Don't include box attributes such as margins
7512 attr.GetTextBoxAttr().Reset();
7fe8059f
WS
7513
7514 wxRichTextParagraph* newPara = new wxRichTextParagraph(wxEmptyString, this, & attr);
5d7836c4
JS
7515 action->GetNewParagraphs().AppendChild(newPara);
7516 action->GetNewParagraphs().UpdateRanges();
7517 action->GetNewParagraphs().SetPartialParagraph(false);
c025e094
JS
7518 wxRichTextParagraph* para = GetParagraphAtPosition(pos, false);
7519 long pos1 = pos;
7520
6c0ea513
JS
7521 if (p)
7522 newPara->SetAttributes(*p);
7523
c025e094
JS
7524 if (flags & wxRICHTEXT_INSERT_INTERACTIVE)
7525 {
7526 if (para && para->GetRange().GetEnd() == pos)
7527 pos1 ++;
e2d0875a
JS
7528
7529 // Now see if we need to number the paragraph.
6c0ea513 7530 if (newPara->GetAttributes().HasBulletNumber())
e2d0875a
JS
7531 {
7532 wxRichTextAttr numberingAttr;
7533 if (FindNextParagraphNumber(para, numberingAttr))
7534 wxRichTextApplyStyle(newPara->GetAttributes(), (const wxRichTextAttr&) numberingAttr);
7535 }
c025e094
JS
7536 }
7537
5d7836c4
JS
7538 action->SetPosition(pos);
7539
99404ab0 7540 // Use the default character style
603f702b 7541 if (!buffer->GetDefaultStyle().IsDefault() && newPara->GetChildren().GetFirst())
c025e094
JS
7542 {
7543 // Check whether the default style merely reflects the paragraph/basic style,
7544 // in which case don't apply it.
603f702b 7545 wxRichTextAttr defaultStyle(buffer->GetDefaultStyle());
32423dd8 7546 defaultStyle.GetTextBoxAttr().Reset();
24777478 7547 wxRichTextAttr toApply;
c025e094
JS
7548 if (para)
7549 {
603f702b 7550 wxRichTextAttr combinedAttr = para->GetCombinedAttributes(true /* include box attributes */);
24777478 7551 wxRichTextAttr newAttr;
c025e094
JS
7552 // This filters out attributes that are accounted for by the current
7553 // paragraph/basic style
7554 wxRichTextApplyStyle(toApply, defaultStyle, & combinedAttr);
7555 }
7556 else
7557 toApply = defaultStyle;
7558
7559 if (!toApply.IsDefault())
7560 newPara->GetChildren().GetFirst()->GetData()->SetAttributes(toApply);
7561 }
99404ab0 7562
5d7836c4 7563 // Set the range we'll need to delete in Undo
c025e094 7564 action->SetRange(wxRichTextRange(pos1, pos1));
7fe8059f 7565
603f702b 7566 buffer->SubmitAction(action);
7fe8059f 7567
5d7836c4
JS
7568 return true;
7569}
7570
7571/// Submit command to insert the given image
24777478
JS
7572bool wxRichTextBuffer::InsertImageWithUndo(long pos, const wxRichTextImageBlock& imageBlock, wxRichTextCtrl* ctrl, int flags,
7573 const wxRichTextAttr& textAttr)
5d7836c4 7574{
4e63bfb9 7575 return ctrl->GetFocusObject()->InsertImageWithUndo(this, pos, imageBlock, ctrl, flags, textAttr);
603f702b
JS
7576}
7577
7578/// Submit command to insert the given image
4e63bfb9
JS
7579bool wxRichTextParagraphLayoutBox::InsertImageWithUndo(wxRichTextBuffer* buffer, long pos, const wxRichTextImageBlock& imageBlock,
7580 wxRichTextCtrl* ctrl, int flags,
603f702b
JS
7581 const wxRichTextAttr& textAttr)
7582{
7583 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, buffer, this, ctrl, false);
5d7836c4 7584
24777478
JS
7585 wxRichTextAttr* p = NULL;
7586 wxRichTextAttr paraAttr;
fe5aa22c
JS
7587 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
7588 {
603f702b 7589 paraAttr = GetStyleForNewParagraph(buffer, pos);
fe5aa22c
JS
7590 if (!paraAttr.IsDefault())
7591 p = & paraAttr;
7592 }
7593
603f702b 7594 wxRichTextAttr attr(buffer->GetDefaultStyle());
7fe8059f 7595
32423dd8
JS
7596 // Don't include box attributes such as margins
7597 attr.GetTextBoxAttr().Reset();
7598
5d7836c4 7599 wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr);
fe5aa22c
JS
7600 if (p)
7601 newPara->SetAttributes(*p);
7602
5d7836c4
JS
7603 wxRichTextImage* imageObject = new wxRichTextImage(imageBlock, newPara);
7604 newPara->AppendChild(imageObject);
24777478 7605 imageObject->SetAttributes(textAttr);
5d7836c4
JS
7606 action->GetNewParagraphs().AppendChild(newPara);
7607 action->GetNewParagraphs().UpdateRanges();
7608
7609 action->GetNewParagraphs().SetPartialParagraph(true);
7610
7611 action->SetPosition(pos);
7612
7613 // Set the range we'll need to delete in Undo
7614 action->SetRange(wxRichTextRange(pos, pos));
7fe8059f 7615
603f702b 7616 buffer->SubmitAction(action);
7fe8059f 7617
5d7836c4
JS
7618 return true;
7619}
7620
cdaed652 7621// Insert an object with no change of it
603f702b
JS
7622wxRichTextObject* wxRichTextBuffer::InsertObjectWithUndo(long pos, wxRichTextObject *object, wxRichTextCtrl* ctrl, int flags)
7623{
4e63bfb9 7624 return ctrl->GetFocusObject()->InsertObjectWithUndo(this, pos, object, ctrl, flags);
603f702b
JS
7625}
7626
7627// Insert an object with no change of it
4e63bfb9 7628wxRichTextObject* wxRichTextParagraphLayoutBox::InsertObjectWithUndo(wxRichTextBuffer* buffer, long pos, wxRichTextObject *object, wxRichTextCtrl* ctrl, int flags)
cdaed652 7629{
603f702b 7630 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Object"), wxRICHTEXT_INSERT, buffer, this, ctrl, false);
cdaed652 7631
24777478
JS
7632 wxRichTextAttr* p = NULL;
7633 wxRichTextAttr paraAttr;
cdaed652
VZ
7634 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
7635 {
603f702b 7636 paraAttr = GetStyleForNewParagraph(buffer, pos);
cdaed652
VZ
7637 if (!paraAttr.IsDefault())
7638 p = & paraAttr;
7639 }
7640
603f702b 7641 wxRichTextAttr attr(buffer->GetDefaultStyle());
cdaed652 7642
32423dd8
JS
7643 // Don't include box attributes such as margins
7644 attr.GetTextBoxAttr().Reset();
7645
cdaed652
VZ
7646 wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr);
7647 if (p)
7648 newPara->SetAttributes(*p);
7649
7650 newPara->AppendChild(object);
7651 action->GetNewParagraphs().AppendChild(newPara);
7652 action->GetNewParagraphs().UpdateRanges();
7653
7654 action->GetNewParagraphs().SetPartialParagraph(true);
7655
7656 action->SetPosition(pos);
7657
7658 // Set the range we'll need to delete in Undo
7659 action->SetRange(wxRichTextRange(pos, pos));
7660
603f702b 7661 buffer->SubmitAction(action);
cdaed652 7662
603f702b
JS
7663 wxRichTextObject* obj = GetLeafObjectAtPosition(pos);
7664 return obj;
cdaed652 7665}
603f702b 7666
7c9fdebe
JS
7667wxRichTextField* wxRichTextParagraphLayoutBox::InsertFieldWithUndo(wxRichTextBuffer* buffer, long pos, const wxString& fieldType,
7668 const wxRichTextProperties& properties,
7669 wxRichTextCtrl* ctrl, int flags,
7670 const wxRichTextAttr& textAttr)
7671{
7672 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Field"), wxRICHTEXT_INSERT, buffer, this, ctrl, false);
7673
7674 wxRichTextAttr* p = NULL;
7675 wxRichTextAttr paraAttr;
7676 if (flags & wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE)
7677 {
7678 paraAttr = GetStyleForNewParagraph(buffer, pos);
7679 if (!paraAttr.IsDefault())
7680 p = & paraAttr;
7681 }
7682
7683 wxRichTextAttr attr(buffer->GetDefaultStyle());
7684
32423dd8
JS
7685 // Don't include box attributes such as margins
7686 attr.GetTextBoxAttr().Reset();
7687
7c9fdebe
JS
7688 wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr);
7689 if (p)
7690 newPara->SetAttributes(*p);
7691
7692 wxRichTextField* fieldObject = new wxRichTextField();
7693 fieldObject->wxRichTextObject::SetProperties(properties);
7694 fieldObject->SetFieldType(fieldType);
7695 fieldObject->SetAttributes(textAttr);
7696 newPara->AppendChild(fieldObject);
7697 action->GetNewParagraphs().AppendChild(newPara);
7698 action->GetNewParagraphs().UpdateRanges();
7699 action->GetNewParagraphs().SetPartialParagraph(true);
7700 action->SetPosition(pos);
7701
7702 // Set the range we'll need to delete in Undo
7703 action->SetRange(wxRichTextRange(pos, pos));
7704
7705 buffer->SubmitAction(action);
7706
7707 wxRichTextField* obj = wxDynamicCast(GetLeafObjectAtPosition(pos), wxRichTextField);
7708 return obj;
7709}
7710
fe5aa22c
JS
7711/// Get the style that is appropriate for a new paragraph at this position.
7712/// If the previous paragraph has a paragraph style name, look up the next-paragraph
7713/// style.
603f702b 7714wxRichTextAttr wxRichTextParagraphLayoutBox::GetStyleForNewParagraph(wxRichTextBuffer* buffer, long pos, bool caretPosition, bool lookUpNewParaStyle) const
fe5aa22c
JS
7715{
7716 wxRichTextParagraph* para = GetParagraphAtPosition(pos, caretPosition);
7717 if (para)
7718 {
24777478 7719 wxRichTextAttr attr;
d2d0adc7 7720 bool foundAttributes = false;
3e541562 7721
d2d0adc7 7722 // Look for a matching paragraph style
603f702b 7723 if (lookUpNewParaStyle && !para->GetAttributes().GetParagraphStyleName().IsEmpty() && buffer->GetStyleSheet())
fe5aa22c 7724 {
603f702b 7725 wxRichTextParagraphStyleDefinition* paraDef = buffer->GetStyleSheet()->FindParagraphStyle(para->GetAttributes().GetParagraphStyleName());
d2d0adc7 7726 if (paraDef)
fe5aa22c 7727 {
caad0109
JS
7728 // If we're not at the end of the paragraph, then we apply THIS style, and not the designated next style.
7729 if (para->GetRange().GetEnd() == pos && !paraDef->GetNextStyle().IsEmpty())
d2d0adc7 7730 {
603f702b 7731 wxRichTextParagraphStyleDefinition* nextParaDef = buffer->GetStyleSheet()->FindParagraphStyle(paraDef->GetNextStyle());
d2d0adc7
JS
7732 if (nextParaDef)
7733 {
7734 foundAttributes = true;
603f702b 7735 attr = nextParaDef->GetStyleMergedWithBase(buffer->GetStyleSheet());
d2d0adc7
JS
7736 }
7737 }
3e541562 7738
d2d0adc7
JS
7739 // If we didn't find the 'next style', use this style instead.
7740 if (!foundAttributes)
7741 {
7742 foundAttributes = true;
603f702b 7743 attr = paraDef->GetStyleMergedWithBase(buffer->GetStyleSheet());
d2d0adc7 7744 }
fe5aa22c
JS
7745 }
7746 }
e2d0875a
JS
7747
7748 // Also apply list style if present
603f702b 7749 if (lookUpNewParaStyle && !para->GetAttributes().GetListStyleName().IsEmpty() && buffer->GetStyleSheet())
e2d0875a 7750 {
603f702b 7751 wxRichTextListStyleDefinition* listDef = buffer->GetStyleSheet()->FindListStyle(para->GetAttributes().GetListStyleName());
e2d0875a
JS
7752 if (listDef)
7753 {
7754 int thisIndent = para->GetAttributes().GetLeftIndent();
7755 int thisLevel = para->GetAttributes().HasOutlineLevel() ? para->GetAttributes().GetOutlineLevel() : listDef->FindLevelForIndent(thisIndent);
7756
7757 // Apply the overall list style, and item style for this level
603f702b 7758 wxRichTextAttr listStyle(listDef->GetCombinedStyleForLevel(thisLevel, buffer->GetStyleSheet()));
e2d0875a
JS
7759 wxRichTextApplyStyle(attr, listStyle);
7760 attr.SetOutlineLevel(thisLevel);
7761 if (para->GetAttributes().HasBulletNumber())
7762 attr.SetBulletNumber(para->GetAttributes().GetBulletNumber());
7763 }
34b4899d 7764 }
e2d0875a 7765
d2d0adc7
JS
7766 if (!foundAttributes)
7767 {
7768 attr = para->GetAttributes();
7769 int flags = attr.GetFlags();
fe5aa22c 7770
d2d0adc7
JS
7771 // Eliminate character styles
7772 flags &= ( (~ wxTEXT_ATTR_FONT) |
fe5aa22c
JS
7773 (~ wxTEXT_ATTR_TEXT_COLOUR) |
7774 (~ wxTEXT_ATTR_BACKGROUND_COLOUR) );
d2d0adc7
JS
7775 attr.SetFlags(flags);
7776 }
3e541562 7777
fe5aa22c
JS
7778 return attr;
7779 }
7780 else
24777478 7781 return wxRichTextAttr();
fe5aa22c
JS
7782}
7783
5d7836c4 7784/// Submit command to delete this range
12cc29c5 7785bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange& range, wxRichTextCtrl* ctrl)
5d7836c4 7786{
603f702b
JS
7787 return ctrl->GetFocusObject()->DeleteRangeWithUndo(range, ctrl, this);
7788}
7789
7790/// Submit command to delete this range
7791bool wxRichTextParagraphLayoutBox::DeleteRangeWithUndo(const wxRichTextRange& range, wxRichTextCtrl* ctrl, wxRichTextBuffer* buffer)
7792{
7793 wxRichTextAction* action = new wxRichTextAction(NULL, _("Delete"), wxRICHTEXT_DELETE, buffer, this, ctrl);
7fe8059f 7794
12cc29c5 7795 action->SetPosition(ctrl->GetCaretPosition());
5d7836c4
JS
7796
7797 // Set the range to delete
7798 action->SetRange(range);
7fe8059f 7799
5d7836c4
JS
7800 // Copy the fragment that we'll need to restore in Undo
7801 CopyFragment(range, action->GetOldParagraphs());
7802
6c0ea513
JS
7803 // See if we're deleting a paragraph marker, in which case we need to
7804 // make a note not to copy the attributes from the 2nd paragraph to the 1st.
7805 if (range.GetStart() == range.GetEnd())
5d7836c4 7806 {
6c0ea513
JS
7807 wxRichTextParagraph* para = GetParagraphAtPosition(range.GetStart());
7808 if (para && para->GetRange().GetEnd() == range.GetEnd())
5d7836c4 7809 {
6c0ea513
JS
7810 wxRichTextParagraph* nextPara = GetParagraphAtPosition(range.GetStart()+1);
7811 if (nextPara && nextPara != para)
5d7836c4 7812 {
6c0ea513
JS
7813 action->GetOldParagraphs().GetChildren().GetFirst()->GetData()->SetAttributes(nextPara->GetAttributes());
7814 action->GetOldParagraphs().GetAttributes().SetFlags(action->GetOldParagraphs().GetAttributes().GetFlags() | wxTEXT_ATTR_KEEP_FIRST_PARA_STYLE);
5d7836c4
JS
7815 }
7816 }
7817 }
7818
603f702b 7819 buffer->SubmitAction(action);
7fe8059f 7820
5d7836c4
JS
7821 return true;
7822}
7823
7824/// Collapse undo/redo commands
7825bool wxRichTextBuffer::BeginBatchUndo(const wxString& cmdName)
7826{
7827 if (m_batchedCommandDepth == 0)
7828 {
7829 wxASSERT(m_batchedCommand == NULL);
7830 if (m_batchedCommand)
7831 {
0745f364 7832 GetCommandProcessor()->Store(m_batchedCommand);
5d7836c4
JS
7833 }
7834 m_batchedCommand = new wxRichTextCommand(cmdName);
7835 }
7836
7fe8059f 7837 m_batchedCommandDepth ++;
5d7836c4
JS
7838
7839 return true;
7840}
7841
7842/// Collapse undo/redo commands
7843bool wxRichTextBuffer::EndBatchUndo()
7844{
7845 m_batchedCommandDepth --;
7846
7847 wxASSERT(m_batchedCommandDepth >= 0);
7848 wxASSERT(m_batchedCommand != NULL);
7849
7850 if (m_batchedCommandDepth == 0)
7851 {
0745f364 7852 GetCommandProcessor()->Store(m_batchedCommand);
5d7836c4
JS
7853 m_batchedCommand = NULL;
7854 }
7855
7856 return true;
7857}
7858
7859/// Submit immediately, or delay according to whether collapsing is on
7860bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action)
7861{
cc2aecde
JS
7862 if (action && !action->GetNewParagraphs().IsEmpty())
7863 PrepareContent(action->GetNewParagraphs());
7864
5d7836c4 7865 if (BatchingUndo() && m_batchedCommand && !SuppressingUndo())
0745f364
JS
7866 {
7867 wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName());
7868 cmd->AddAction(action);
7869 cmd->Do();
7870 cmd->GetActions().Clear();
7871 delete cmd;
7872
5d7836c4 7873 m_batchedCommand->AddAction(action);
0745f364 7874 }
5d7836c4
JS
7875 else
7876 {
7877 wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName());
7878 cmd->AddAction(action);
7879
7880 // Only store it if we're not suppressing undo.
7881 return GetCommandProcessor()->Submit(cmd, !SuppressingUndo());
7882 }
7883
7884 return true;
7885}
7886
7887/// Begin suppressing undo/redo commands.
7888bool wxRichTextBuffer::BeginSuppressUndo()
7889{
7fe8059f 7890 m_suppressUndo ++;
5d7836c4
JS
7891
7892 return true;
7893}
7894
7895/// End suppressing undo/redo commands.
7896bool wxRichTextBuffer::EndSuppressUndo()
7897{
7fe8059f 7898 m_suppressUndo --;
5d7836c4
JS
7899
7900 return true;
7901}
7902
7903/// Begin using a style
24777478 7904bool wxRichTextBuffer::BeginStyle(const wxRichTextAttr& style)
5d7836c4 7905{
24777478 7906 wxRichTextAttr newStyle(GetDefaultStyle());
32423dd8 7907 newStyle.GetTextBoxAttr().Reset();
5d7836c4
JS
7908
7909 // Save the old default style
32423dd8 7910 m_attributeStack.Append((wxObject*) new wxRichTextAttr(newStyle));
5d7836c4
JS
7911
7912 wxRichTextApplyStyle(newStyle, style);
7913 newStyle.SetFlags(style.GetFlags()|newStyle.GetFlags());
7914
7915 SetDefaultStyle(newStyle);
7916
5d7836c4
JS
7917 return true;
7918}
7919
7920/// End the style
7921bool wxRichTextBuffer::EndStyle()
7922{
63886f6d 7923 if (!m_attributeStack.GetFirst())
5d7836c4
JS
7924 {
7925 wxLogDebug(_("Too many EndStyle calls!"));
7926 return false;
7927 }
7928
09f14108 7929 wxList::compatibility_iterator node = m_attributeStack.GetLast();
24777478 7930 wxRichTextAttr* attr = (wxRichTextAttr*)node->GetData();
9e31a660 7931 m_attributeStack.Erase(node);
5d7836c4
JS
7932
7933 SetDefaultStyle(*attr);
7934
7935 delete attr;
7936 return true;
7937}
7938
7939/// End all styles
7940bool wxRichTextBuffer::EndAllStyles()
7941{
7942 while (m_attributeStack.GetCount() != 0)
7943 EndStyle();
7944 return true;
7945}
7946
7947/// Clear the style stack
7948void wxRichTextBuffer::ClearStyleStack()
7949{
09f14108 7950 for (wxList::compatibility_iterator node = m_attributeStack.GetFirst(); node; node = node->GetNext())
24777478 7951 delete (wxRichTextAttr*) node->GetData();
5d7836c4
JS
7952 m_attributeStack.Clear();
7953}
7954
7955/// Begin using bold
7956bool wxRichTextBuffer::BeginBold()
7957{
24777478 7958 wxRichTextAttr attr;
7d76fbd5 7959 attr.SetFontWeight(wxFONTWEIGHT_BOLD);
7fe8059f 7960
5d7836c4
JS
7961 return BeginStyle(attr);
7962}
7963
7964/// Begin using italic
7965bool wxRichTextBuffer::BeginItalic()
7966{
24777478 7967 wxRichTextAttr attr;
7d76fbd5 7968 attr.SetFontStyle(wxFONTSTYLE_ITALIC);
7fe8059f 7969
5d7836c4
JS
7970 return BeginStyle(attr);
7971}
7972
7973/// Begin using underline
7974bool wxRichTextBuffer::BeginUnderline()
7975{
24777478 7976 wxRichTextAttr attr;
44cc96a8 7977 attr.SetFontUnderlined(true);
7fe8059f 7978
5d7836c4
JS
7979 return BeginStyle(attr);
7980}
7981
7982/// Begin using point size
7983bool wxRichTextBuffer::BeginFontSize(int pointSize)
7984{
24777478 7985 wxRichTextAttr attr;
44cc96a8 7986 attr.SetFontSize(pointSize);
7fe8059f 7987
5d7836c4
JS
7988 return BeginStyle(attr);
7989}
7990
7991/// Begin using this font
7992bool wxRichTextBuffer::BeginFont(const wxFont& font)
7993{
24777478 7994 wxRichTextAttr attr;
5d7836c4 7995 attr.SetFont(font);
7fe8059f 7996
5d7836c4
JS
7997 return BeginStyle(attr);
7998}
7999
8000/// Begin using this colour
8001bool wxRichTextBuffer::BeginTextColour(const wxColour& colour)
8002{
24777478 8003 wxRichTextAttr attr;
5d7836c4
JS
8004 attr.SetFlags(wxTEXT_ATTR_TEXT_COLOUR);
8005 attr.SetTextColour(colour);
7fe8059f 8006
5d7836c4
JS
8007 return BeginStyle(attr);
8008}
8009
8010/// Begin using alignment
8011bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment)
8012{
24777478 8013 wxRichTextAttr attr;
5d7836c4
JS
8014 attr.SetFlags(wxTEXT_ATTR_ALIGNMENT);
8015 attr.SetAlignment(alignment);
7fe8059f 8016
5d7836c4
JS
8017 return BeginStyle(attr);
8018}
8019
8020/// Begin left indent
8021bool wxRichTextBuffer::BeginLeftIndent(int leftIndent, int leftSubIndent)
8022{
24777478 8023 wxRichTextAttr attr;
5d7836c4
JS
8024 attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT);
8025 attr.SetLeftIndent(leftIndent, leftSubIndent);
7fe8059f 8026
5d7836c4
JS
8027 return BeginStyle(attr);
8028}
8029
8030/// Begin right indent
8031bool wxRichTextBuffer::BeginRightIndent(int rightIndent)
8032{
24777478 8033 wxRichTextAttr attr;
5d7836c4
JS
8034 attr.SetFlags(wxTEXT_ATTR_RIGHT_INDENT);
8035 attr.SetRightIndent(rightIndent);
7fe8059f 8036
5d7836c4
JS
8037 return BeginStyle(attr);
8038}
8039
8040/// Begin paragraph spacing
8041bool wxRichTextBuffer::BeginParagraphSpacing(int before, int after)
8042{
8043 long flags = 0;
8044 if (before != 0)
8045 flags |= wxTEXT_ATTR_PARA_SPACING_BEFORE;
8046 if (after != 0)
8047 flags |= wxTEXT_ATTR_PARA_SPACING_AFTER;
8048
24777478 8049 wxRichTextAttr attr;
5d7836c4
JS
8050 attr.SetFlags(flags);
8051 attr.SetParagraphSpacingBefore(before);
8052 attr.SetParagraphSpacingAfter(after);
7fe8059f 8053
5d7836c4
JS
8054 return BeginStyle(attr);
8055}
8056
8057/// Begin line spacing
8058bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing)
8059{
24777478 8060 wxRichTextAttr attr;
5d7836c4
JS
8061 attr.SetFlags(wxTEXT_ATTR_LINE_SPACING);
8062 attr.SetLineSpacing(lineSpacing);
7fe8059f 8063
5d7836c4
JS
8064 return BeginStyle(attr);
8065}
8066
8067/// Begin numbered bullet
8068bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber, int leftIndent, int leftSubIndent, int bulletStyle)
8069{
24777478 8070 wxRichTextAttr attr;
f089713f 8071 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_LEFT_INDENT);
5d7836c4
JS
8072 attr.SetBulletStyle(bulletStyle);
8073 attr.SetBulletNumber(bulletNumber);
8074 attr.SetLeftIndent(leftIndent, leftSubIndent);
7fe8059f 8075
5d7836c4
JS
8076 return BeginStyle(attr);
8077}
8078
8079/// Begin symbol bullet
d2d0adc7 8080bool wxRichTextBuffer::BeginSymbolBullet(const wxString& symbol, int leftIndent, int leftSubIndent, int bulletStyle)
5d7836c4 8081{
24777478 8082 wxRichTextAttr attr;
f089713f 8083 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_LEFT_INDENT);
5d7836c4
JS
8084 attr.SetBulletStyle(bulletStyle);
8085 attr.SetLeftIndent(leftIndent, leftSubIndent);
d2d0adc7 8086 attr.SetBulletText(symbol);
7fe8059f 8087
5d7836c4
JS
8088 return BeginStyle(attr);
8089}
8090
f089713f
JS
8091/// Begin standard bullet
8092bool wxRichTextBuffer::BeginStandardBullet(const wxString& bulletName, int leftIndent, int leftSubIndent, int bulletStyle)
8093{
24777478 8094 wxRichTextAttr attr;
f089713f
JS
8095 attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_LEFT_INDENT);
8096 attr.SetBulletStyle(bulletStyle);
8097 attr.SetLeftIndent(leftIndent, leftSubIndent);
8098 attr.SetBulletName(bulletName);
8099
8100 return BeginStyle(attr);
8101}
8102
5d7836c4
JS
8103/// Begin named character style
8104bool wxRichTextBuffer::BeginCharacterStyle(const wxString& characterStyle)
8105{
8106 if (GetStyleSheet())
8107 {
8108 wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle);
8109 if (def)
8110 {
24777478 8111 wxRichTextAttr attr = def->GetStyleMergedWithBase(GetStyleSheet());
5d7836c4
JS
8112 return BeginStyle(attr);
8113 }
8114 }
8115 return false;
8116}
8117
8118/// Begin named paragraph style
8119bool wxRichTextBuffer::BeginParagraphStyle(const wxString& paragraphStyle)
8120{
8121 if (GetStyleSheet())
8122 {
8123 wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(paragraphStyle);
8124 if (def)
8125 {
24777478 8126 wxRichTextAttr attr = def->GetStyleMergedWithBase(GetStyleSheet());
5d7836c4
JS
8127 return BeginStyle(attr);
8128 }
8129 }
8130 return false;
8131}
8132
f089713f
JS
8133/// Begin named list style
8134bool wxRichTextBuffer::BeginListStyle(const wxString& listStyle, int level, int number)
8135{
8136 if (GetStyleSheet())
8137 {
8138 wxRichTextListStyleDefinition* def = GetStyleSheet()->FindListStyle(listStyle);
8139 if (def)
8140 {
24777478 8141 wxRichTextAttr attr(def->GetCombinedStyleForLevel(level));
f089713f
JS
8142
8143 attr.SetBulletNumber(number);
8144
8145 return BeginStyle(attr);
8146 }
8147 }
8148 return false;
8149}
8150
d2d0adc7
JS
8151/// Begin URL
8152bool wxRichTextBuffer::BeginURL(const wxString& url, const wxString& characterStyle)
8153{
24777478 8154 wxRichTextAttr attr;
d2d0adc7
JS
8155
8156 if (!characterStyle.IsEmpty() && GetStyleSheet())
8157 {
8158 wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle);
8159 if (def)
8160 {
336d8ae9 8161 attr = def->GetStyleMergedWithBase(GetStyleSheet());
d2d0adc7
JS
8162 }
8163 }
8164 attr.SetURL(url);
8165
8166 return BeginStyle(attr);
8167}
8168
5d7836c4
JS
8169/// Adds a handler to the end
8170void wxRichTextBuffer::AddHandler(wxRichTextFileHandler *handler)
8171{
8172 sm_handlers.Append(handler);
8173}
8174
8175/// Inserts a handler at the front
8176void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler *handler)
8177{
8178 sm_handlers.Insert( handler );
8179}
8180
8181/// Removes a handler
8182bool wxRichTextBuffer::RemoveHandler(const wxString& name)
8183{
8184 wxRichTextFileHandler *handler = FindHandler(name);
8185 if (handler)
8186 {
8187 sm_handlers.DeleteObject(handler);
8188 delete handler;
8189 return true;
8190 }
8191 else
8192 return false;
8193}
8194
8195/// Finds a handler by filename or, if supplied, type
d75a69e8
FM
8196wxRichTextFileHandler *wxRichTextBuffer::FindHandlerFilenameOrType(const wxString& filename,
8197 wxRichTextFileType imageType)
5d7836c4
JS
8198{
8199 if (imageType != wxRICHTEXT_TYPE_ANY)
8200 return FindHandler(imageType);
0ca07313 8201 else if (!filename.IsEmpty())
5d7836c4
JS
8202 {
8203 wxString path, file, ext;
a51e601e 8204 wxFileName::SplitPath(filename, & path, & file, & ext);
5d7836c4
JS
8205 return FindHandler(ext, imageType);
8206 }
0ca07313
JS
8207 else
8208 return NULL;
5d7836c4
JS
8209}
8210
8211
8212/// Finds a handler by name
8213wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& name)
8214{
8215 wxList::compatibility_iterator node = sm_handlers.GetFirst();
8216 while (node)
8217 {
8218 wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData();
8219 if (handler->GetName().Lower() == name.Lower()) return handler;
8220
8221 node = node->GetNext();
8222 }
8223 return NULL;
8224}
8225
8226/// Finds a handler by extension and type
d75a69e8 8227wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& extension, wxRichTextFileType type)
5d7836c4
JS
8228{
8229 wxList::compatibility_iterator node = sm_handlers.GetFirst();
8230 while (node)
8231 {
8232 wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData();
8233 if ( handler->GetExtension().Lower() == extension.Lower() &&
8234 (type == wxRICHTEXT_TYPE_ANY || handler->GetType() == type) )
8235 return handler;
8236 node = node->GetNext();
8237 }
8238 return 0;
8239}
8240
8241/// Finds a handler by type
d75a69e8 8242wxRichTextFileHandler* wxRichTextBuffer::FindHandler(wxRichTextFileType type)
5d7836c4
JS
8243{
8244 wxList::compatibility_iterator node = sm_handlers.GetFirst();
8245 while (node)
8246 {
8247 wxRichTextFileHandler *handler = (wxRichTextFileHandler *)node->GetData();
8248 if (handler->GetType() == type) return handler;
8249 node = node->GetNext();
8250 }
8251 return NULL;
8252}
8253
8254void wxRichTextBuffer::InitStandardHandlers()
8255{
8256 if (!FindHandler(wxRICHTEXT_TYPE_TEXT))
8257 AddHandler(new wxRichTextPlainTextHandler);
8258}
8259
8260void wxRichTextBuffer::CleanUpHandlers()
8261{
8262 wxList::compatibility_iterator node = sm_handlers.GetFirst();
8263 while (node)
8264 {
8265 wxRichTextFileHandler* handler = (wxRichTextFileHandler*)node->GetData();
8266 wxList::compatibility_iterator next = node->GetNext();
8267 delete handler;
8268 node = next;
8269 }
8270
8271 sm_handlers.Clear();
8272}
8273
1e967276 8274wxString wxRichTextBuffer::GetExtWildcard(bool combine, bool save, wxArrayInt* types)
5d7836c4 8275{
1e967276
JS
8276 if (types)
8277 types->Clear();
8278
5d7836c4
JS
8279 wxString wildcard;
8280
8281 wxList::compatibility_iterator node = GetHandlers().GetFirst();
8282 int count = 0;
8283 while (node)
8284 {
8285 wxRichTextFileHandler* handler = (wxRichTextFileHandler*) node->GetData();
2a230426 8286 if (handler->IsVisible() && ((save && handler->CanSave()) || (!save && handler->CanLoad())))
5d7836c4
JS
8287 {
8288 if (combine)
8289 {
8290 if (count > 0)
8291 wildcard += wxT(";");
8292 wildcard += wxT("*.") + handler->GetExtension();
8293 }
8294 else
8295 {
8296 if (count > 0)
8297 wildcard += wxT("|");
8298 wildcard += handler->GetName();
8299 wildcard += wxT(" ");
8300 wildcard += _("files");
8301 wildcard += wxT(" (*.");
8302 wildcard += handler->GetExtension();
8303 wildcard += wxT(")|*.");
8304 wildcard += handler->GetExtension();
1e967276
JS
8305 if (types)
8306 types->Add(handler->GetType());
5d7836c4
JS
8307 }
8308 count ++;
8309 }
8310
8311 node = node->GetNext();
8312 }
8313
8314 if (combine)
8315 wildcard = wxT("(") + wildcard + wxT(")|") + wildcard;
8316 return wildcard;
8317}
8318
7e81e3a7 8319#if wxUSE_FFILE && wxUSE_STREAMS
5d7836c4 8320/// Load a file
d75a69e8 8321bool wxRichTextBuffer::LoadFile(const wxString& filename, wxRichTextFileType type)
5d7836c4
JS
8322{
8323 wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type);
8324 if (handler)
1e967276 8325 {
24777478 8326 SetDefaultStyle(wxRichTextAttr());
d2d0adc7 8327 handler->SetFlags(GetHandlerFlags());
1e967276
JS
8328 bool success = handler->LoadFile(this, filename);
8329 Invalidate(wxRICHTEXT_ALL);
8330 return success;
8331 }
5d7836c4
JS
8332 else
8333 return false;
8334}
8335
8336/// Save a file
d75a69e8 8337bool wxRichTextBuffer::SaveFile(const wxString& filename, wxRichTextFileType type)
5d7836c4
JS
8338{
8339 wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type);
8340 if (handler)
d2d0adc7
JS
8341 {
8342 handler->SetFlags(GetHandlerFlags());
5d7836c4 8343 return handler->SaveFile(this, filename);
d2d0adc7 8344 }
5d7836c4
JS
8345 else
8346 return false;
8347}
7e81e3a7 8348#endif // wxUSE_FFILE && wxUSE_STREAMS
5d7836c4 8349
7e81e3a7 8350#if wxUSE_STREAMS
5d7836c4 8351/// Load from a stream
d75a69e8 8352bool wxRichTextBuffer::LoadFile(wxInputStream& stream, wxRichTextFileType type)
5d7836c4
JS
8353{
8354 wxRichTextFileHandler* handler = FindHandler(type);
8355 if (handler)
1e967276 8356 {
24777478 8357 SetDefaultStyle(wxRichTextAttr());
d2d0adc7 8358 handler->SetFlags(GetHandlerFlags());
1e967276
JS
8359 bool success = handler->LoadFile(this, stream);
8360 Invalidate(wxRICHTEXT_ALL);
8361 return success;
8362 }
5d7836c4
JS
8363 else
8364 return false;
8365}
8366
8367/// Save to a stream
d75a69e8 8368bool wxRichTextBuffer::SaveFile(wxOutputStream& stream, wxRichTextFileType type)
5d7836c4
JS
8369{
8370 wxRichTextFileHandler* handler = FindHandler(type);
8371 if (handler)
d2d0adc7
JS
8372 {
8373 handler->SetFlags(GetHandlerFlags());
5d7836c4 8374 return handler->SaveFile(this, stream);
d2d0adc7 8375 }
5d7836c4
JS
8376 else
8377 return false;
8378}
7e81e3a7 8379#endif // wxUSE_STREAMS
5d7836c4
JS
8380
8381/// Copy the range to the clipboard
8382bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange& range)
8383{
8384 bool success = false;
603f702b
JS
8385 wxRichTextParagraphLayoutBox* container = this;
8386 if (GetRichTextCtrl())
8387 container = GetRichTextCtrl()->GetFocusObject();
8388
11ef729d 8389#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
0ca07313 8390
d2142335 8391 if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open())
7fe8059f 8392 {
0ca07313
JS
8393 wxTheClipboard->Clear();
8394
8395 // Add composite object
8396
8397 wxDataObjectComposite* compositeObject = new wxDataObjectComposite();
8398
8399 {
603f702b 8400 wxString text = container->GetTextForRange(range);
0ca07313
JS
8401
8402#ifdef __WXMSW__
8403 text = wxTextFile::Translate(text, wxTextFileType_Dos);
8404#endif
8405
8406 compositeObject->Add(new wxTextDataObject(text), false /* not preferred */);
8407 }
8408
8409 // Add rich text buffer data object. This needs the XML handler to be present.
8410
8411 if (FindHandler(wxRICHTEXT_TYPE_XML))
8412 {
8413 wxRichTextBuffer* richTextBuf = new wxRichTextBuffer;
603f702b 8414 container->CopyFragment(range, *richTextBuf);
0ca07313
JS
8415
8416 compositeObject->Add(new wxRichTextBufferDataObject(richTextBuf), true /* preferred */);
8417 }
8418
8419 if (wxTheClipboard->SetData(compositeObject))
8420 success = true;
8421
5d7836c4
JS
8422 wxTheClipboard->Close();
8423 }
0ca07313 8424
39a1c2f2
WS
8425#else
8426 wxUnusedVar(range);
8427#endif
5d7836c4
JS
8428 return success;
8429}
8430
8431/// Paste the clipboard content to the buffer
8432bool wxRichTextBuffer::PasteFromClipboard(long position)
8433{
8434 bool success = false;
603f702b
JS
8435 wxRichTextParagraphLayoutBox* container = this;
8436 if (GetRichTextCtrl())
8437 container = GetRichTextCtrl()->GetFocusObject();
8438
11ef729d 8439#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
5d7836c4
JS
8440 if (CanPasteFromClipboard())
8441 {
8442 if (wxTheClipboard->Open())
8443 {
0ca07313
JS
8444 if (wxTheClipboard->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())))
8445 {
8446 wxRichTextBufferDataObject data;
8447 wxTheClipboard->GetData(data);
8448 wxRichTextBuffer* richTextBuffer = data.GetRichTextBuffer();
8449 if (richTextBuffer)
8450 {
4e63bfb9 8451 container->InsertParagraphsWithUndo(this, position+1, *richTextBuffer, GetRichTextCtrl(), 0);
62381daa 8452 if (GetRichTextCtrl())
603f702b 8453 GetRichTextCtrl()->ShowPosition(position + richTextBuffer->GetOwnRange().GetEnd());
0ca07313
JS
8454 delete richTextBuffer;
8455 }
8456 }
f7d83f24 8457 else if (wxTheClipboard->IsSupported(wxDF_TEXT)
603f702b
JS
8458 #if wxUSE_UNICODE
8459 || wxTheClipboard->IsSupported(wxDF_UNICODETEXT)
8460 #endif
f7d83f24 8461 )
5d7836c4
JS
8462 {
8463 wxTextDataObject data;
8464 wxTheClipboard->GetData(data);
8465 wxString text(data.GetText());
c21f3211
JS
8466#ifdef __WXMSW__
8467 wxString text2;
8468 text2.Alloc(text.Length()+1);
8469 size_t i;
8470 for (i = 0; i < text.Length(); i++)
8471 {
8472 wxChar ch = text[i];
8473 if (ch != wxT('\r'))
8474 text2 += ch;
8475 }
8476#else
8477 wxString text2 = text;
8478#endif
4e63bfb9 8479 container->InsertTextWithUndo(this, position+1, text2, GetRichTextCtrl(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
7fe8059f 8480
62381daa
JS
8481 if (GetRichTextCtrl())
8482 GetRichTextCtrl()->ShowPosition(position + text2.Length());
8483
5d7836c4
JS
8484 success = true;
8485 }
8486 else if (wxTheClipboard->IsSupported(wxDF_BITMAP))
8487 {
8488 wxBitmapDataObject data;
8489 wxTheClipboard->GetData(data);
8490 wxBitmap bitmap(data.GetBitmap());
8491 wxImage image(bitmap.ConvertToImage());
8492
603f702b 8493 wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, container, GetRichTextCtrl(), false);
7fe8059f 8494
5d7836c4
JS
8495 action->GetNewParagraphs().AddImage(image);
8496
8497 if (action->GetNewParagraphs().GetChildCount() == 1)
8498 action->GetNewParagraphs().SetPartialParagraph(true);
7fe8059f 8499
9c8e10ad 8500 action->SetPosition(position+1);
7fe8059f 8501
5d7836c4 8502 // Set the range we'll need to delete in Undo
9c8e10ad 8503 action->SetRange(wxRichTextRange(position+1, position+1));
7fe8059f 8504
5d7836c4
JS
8505 SubmitAction(action);
8506
8507 success = true;
8508 }
8509 wxTheClipboard->Close();
8510 }
8511 }
39a1c2f2
WS
8512#else
8513 wxUnusedVar(position);
8514#endif
5d7836c4
JS
8515 return success;
8516}
8517
8518/// Can we paste from the clipboard?
8519bool wxRichTextBuffer::CanPasteFromClipboard() const
8520{
7fe8059f 8521 bool canPaste = false;
11ef729d 8522#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ
d2142335 8523 if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open())
5d7836c4 8524 {
f7d83f24
VZ
8525 if (wxTheClipboard->IsSupported(wxDF_TEXT)
8526#if wxUSE_UNICODE
603f702b
JS
8527 || wxTheClipboard->IsSupported(wxDF_UNICODETEXT)
8528#endif
8529 || wxTheClipboard->IsSupported(wxDataFormat(wxRichTextBufferDataObject::GetRichTextBufferFormatId())) ||
8530 wxTheClipboard->IsSupported(wxDF_BITMAP))
5d7836c4 8531 {
7fe8059f 8532 canPaste = true;
5d7836c4
JS
8533 }
8534 wxTheClipboard->Close();
8535 }
39a1c2f2 8536#endif
5d7836c4
JS
8537 return canPaste;
8538}
8539
8540/// Dumps contents of buffer for debugging purposes
8541void wxRichTextBuffer::Dump()
8542{
8543 wxString text;
8544 {
8545 wxStringOutputStream stream(& text);
8546 wxTextOutputStream textStream(stream);
8547 Dump(textStream);
8548 }
8549
8550 wxLogDebug(text);
8551}
8552
d2d0adc7
JS
8553/// Add an event handler
8554bool wxRichTextBuffer::AddEventHandler(wxEvtHandler* handler)
8555{
8556 m_eventHandlers.Append(handler);
8557 return true;
8558}
8559
8560/// Remove an event handler
8561bool wxRichTextBuffer::RemoveEventHandler(wxEvtHandler* handler, bool deleteHandler)
8562{
8563 wxList::compatibility_iterator node = m_eventHandlers.Find(handler);
8564 if (node)
8565 {
8566 m_eventHandlers.Erase(node);
8567 if (deleteHandler)
8568 delete handler;
3e541562 8569
d2d0adc7
JS
8570 return true;
8571 }
8572 else
8573 return false;
8574}
8575
8576/// Clear event handlers
8577void wxRichTextBuffer::ClearEventHandlers()
8578{
8579 m_eventHandlers.Clear();
8580}
8581
8582/// Send event to event handlers. If sendToAll is true, will send to all event handlers,
8583/// otherwise will stop at the first successful one.
8584bool wxRichTextBuffer::SendEvent(wxEvent& event, bool sendToAll)
8585{
8586 bool success = false;
8587 for (wxList::compatibility_iterator node = m_eventHandlers.GetFirst(); node; node = node->GetNext())
8588 {
8589 wxEvtHandler* handler = (wxEvtHandler*) node->GetData();
8590 if (handler->ProcessEvent(event))
8591 {
8592 success = true;
8593 if (!sendToAll)
8594 return true;
8595 }
8596 }
8597 return success;
8598}
8599
8600/// Set style sheet and notify of the change
8601bool wxRichTextBuffer::SetStyleSheetAndNotify(wxRichTextStyleSheet* sheet)
8602{
8603 wxRichTextStyleSheet* oldSheet = GetStyleSheet();
3e541562 8604
616c7cbd 8605 wxWindowID winid = wxID_ANY;
d2d0adc7 8606 if (GetRichTextCtrl())
616c7cbd 8607 winid = GetRichTextCtrl()->GetId();
3e541562 8608
ce7fe42e 8609 wxRichTextEvent event(wxEVT_RICHTEXT_STYLESHEET_REPLACING, winid);
d2d0adc7 8610 event.SetEventObject(GetRichTextCtrl());
603f702b 8611 event.SetContainer(GetRichTextCtrl()->GetFocusObject());
d2d0adc7
JS
8612 event.SetOldStyleSheet(oldSheet);
8613 event.SetNewStyleSheet(sheet);
8614 event.Allow();
3e541562 8615
d2d0adc7
JS
8616 if (SendEvent(event) && !event.IsAllowed())
8617 {
8618 if (sheet != oldSheet)
8619 delete sheet;
8620
8621 return false;
8622 }
8623
8624 if (oldSheet && oldSheet != sheet)
8625 delete oldSheet;
8626
8627 SetStyleSheet(sheet);
8628
ce7fe42e 8629 event.SetEventType(wxEVT_RICHTEXT_STYLESHEET_REPLACED);
d2d0adc7
JS
8630 event.SetOldStyleSheet(NULL);
8631 event.Allow();
8632
8633 return SendEvent(event);
8634}
8635
8636/// Set renderer, deleting old one
8637void wxRichTextBuffer::SetRenderer(wxRichTextRenderer* renderer)
8638{
8639 if (sm_renderer)
8640 delete sm_renderer;
8641 sm_renderer = renderer;
8642}
8643
603f702b
JS
8644/// Hit-testing: returns a flag indicating hit test details, plus
8645/// information about position
8db2e3ef 8646int wxRichTextBuffer::HitTest(wxDC& dc, wxRichTextDrawingContext& context, const wxPoint& pt, long& textPosition, wxRichTextObject** obj, wxRichTextObject** contextObj, int flags)
603f702b 8647{
8db2e3ef 8648 int ret = wxRichTextParagraphLayoutBox::HitTest(dc, context, pt, textPosition, obj, contextObj, flags);
603f702b
JS
8649 if (ret != wxRICHTEXT_HITTEST_NONE)
8650 {
8651 return ret;
8652 }
8653 else
8654 {
8655 textPosition = m_ownRange.GetEnd()-1;
8656 *obj = this;
8657 *contextObj = this;
8658 return wxRICHTEXT_HITTEST_AFTER|wxRICHTEXT_HITTEST_OUTSIDE;
8659 }
8660}
8661
32423dd8
JS
8662void wxRichTextBuffer::SetFontScale(double fontScale)
8663{
8664 m_fontScale = fontScale;
8665 m_fontTable.SetFontScale(fontScale);
8666}
8667
8668void wxRichTextBuffer::SetDimensionScale(double dimScale)
8669{
8670 m_dimensionScale = dimScale;
8671}
8672
24777478 8673bool wxRichTextStdRenderer::DrawStandardBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxRichTextAttr& bulletAttr, const wxRect& rect)
d2d0adc7 8674{
a1b806b9 8675 if (bulletAttr.GetTextColour().IsOk())
d2d0adc7 8676 {
ecb5fbf1
JS
8677 wxCheckSetPen(dc, wxPen(bulletAttr.GetTextColour()));
8678 wxCheckSetBrush(dc, wxBrush(bulletAttr.GetTextColour()));
d2d0adc7
JS
8679 }
8680 else
8681 {
ecb5fbf1
JS
8682 wxCheckSetPen(dc, *wxBLACK_PEN);
8683 wxCheckSetBrush(dc, *wxBLACK_BRUSH);
d2d0adc7
JS
8684 }
8685
8686 wxFont font;
44cc96a8
JS
8687 if (bulletAttr.HasFont())
8688 {
8689 font = paragraph->GetBuffer()->GetFontTable().FindFont(bulletAttr);
8690 }
d2d0adc7
JS
8691 else
8692 font = (*wxNORMAL_FONT);
8693
ecb5fbf1 8694 wxCheckSetFont(dc, font);
d2d0adc7
JS
8695
8696 int charHeight = dc.GetCharHeight();
3e541562 8697
d2d0adc7
JS
8698 int bulletWidth = (int) (((float) charHeight) * wxRichTextBuffer::GetBulletProportion());
8699 int bulletHeight = bulletWidth;
8700
8701 int x = rect.x;
3e541562 8702
d2d0adc7
JS
8703 // Calculate the top position of the character (as opposed to the whole line height)
8704 int y = rect.y + (rect.height - charHeight);
3e541562 8705
d2d0adc7
JS
8706 // Calculate where the bullet should be positioned
8707 y = y + (charHeight+1)/2 - (bulletHeight+1)/2;
3e541562 8708
d2d0adc7 8709 // The margin between a bullet and text.
44219ff0 8710 int margin = paragraph->ConvertTenthsMMToPixels(dc, wxRichTextBuffer::GetBulletRightMargin());
3e541562 8711
d2d0adc7
JS
8712 if (bulletAttr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT)
8713 x = rect.x + rect.width - bulletWidth - margin;
8714 else if (bulletAttr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE)
8715 x = x + (rect.width)/2 - bulletWidth/2;
3e541562 8716
d2d0adc7
JS
8717 if (bulletAttr.GetBulletName() == wxT("standard/square"))
8718 {
8719 dc.DrawRectangle(x, y, bulletWidth, bulletHeight);
8720 }
8721 else if (bulletAttr.GetBulletName() == wxT("standard/diamond"))
8722 {
8723 wxPoint pts[5];
8724 pts[0].x = x; pts[0].y = y + bulletHeight/2;
8725 pts[1].x = x + bulletWidth/2; pts[1].y = y;
8726 pts[2].x = x + bulletWidth; pts[2].y = y + bulletHeight/2;
8727 pts[3].x = x + bulletWidth/2; pts[3].y = y + bulletHeight;
3e541562 8728
d2d0adc7
JS
8729 dc.DrawPolygon(4, pts);
8730 }
8731 else if (bulletAttr.GetBulletName() == wxT("standard/triangle"))
8732 {
8733 wxPoint pts[3];
8734 pts[0].x = x; pts[0].y = y;
8735 pts[1].x = x + bulletWidth; pts[1].y = y + bulletHeight/2;
8736 pts[2].x = x; pts[2].y = y + bulletHeight;
3e541562 8737
d2d0adc7
JS
8738 dc.DrawPolygon(3, pts);
8739 }
603f702b
JS
8740 else if (bulletAttr.GetBulletName() == wxT("standard/circle-outline"))
8741 {
8742 wxCheckSetBrush(dc, *wxTRANSPARENT_BRUSH);
8743 dc.DrawEllipse(x, y, bulletWidth, bulletHeight);
8744 }
d2d0adc7
JS
8745 else // "standard/circle", and catch-all
8746 {
8747 dc.DrawEllipse(x, y, bulletWidth, bulletHeight);
3e541562
JS
8748 }
8749
d2d0adc7
JS
8750 return true;
8751}
8752
24777478 8753bool wxRichTextStdRenderer::DrawTextBullet(wxRichTextParagraph* paragraph, wxDC& dc, const wxRichTextAttr& attr, const wxRect& rect, const wxString& text)
d2d0adc7
JS
8754{
8755 if (!text.empty())
8756 {
8757 wxFont font;
44cc96a8
JS
8758 if ((attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL) && !attr.GetBulletFont().IsEmpty() && attr.HasFont())
8759 {
24777478 8760 wxRichTextAttr fontAttr;
32423dd8
JS
8761 if (attr.HasFontPixelSize())
8762 fontAttr.SetFontPixelSize(attr.GetFontSize());
8763 else
8764 fontAttr.SetFontPointSize(attr.GetFontSize());
44cc96a8
JS
8765 fontAttr.SetFontStyle(attr.GetFontStyle());
8766 fontAttr.SetFontWeight(attr.GetFontWeight());
8767 fontAttr.SetFontUnderlined(attr.GetFontUnderlined());
8768 fontAttr.SetFontFaceName(attr.GetBulletFont());
8769 font = paragraph->GetBuffer()->GetFontTable().FindFont(fontAttr);
8770 }
8771 else if (attr.HasFont())
8772 font = paragraph->GetBuffer()->GetFontTable().FindFont(attr);
d2d0adc7
JS
8773 else
8774 font = (*wxNORMAL_FONT);
8775
ecb5fbf1 8776 wxCheckSetFont(dc, font);
d2d0adc7 8777
a1b806b9 8778 if (attr.GetTextColour().IsOk())
d2d0adc7
JS
8779 dc.SetTextForeground(attr.GetTextColour());
8780
04ee05f9 8781 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
d2d0adc7
JS
8782
8783 int charHeight = dc.GetCharHeight();
8784 wxCoord tw, th;
8785 dc.GetTextExtent(text, & tw, & th);
8786
8787 int x = rect.x;
8788
8789 // Calculate the top position of the character (as opposed to the whole line height)
3e541562 8790 int y = rect.y + (rect.height - charHeight);
d2d0adc7
JS
8791
8792 // The margin between a bullet and text.
44219ff0 8793 int margin = paragraph->ConvertTenthsMMToPixels(dc, wxRichTextBuffer::GetBulletRightMargin());
3e541562 8794
d2d0adc7
JS
8795 if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_RIGHT)
8796 x = (rect.x + rect.width) - tw - margin;
8797 else if (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ALIGN_CENTRE)
8798 x = x + (rect.width)/2 - tw/2;
8799
8800 dc.DrawText(text, x, y);
3e541562 8801
d2d0adc7
JS
8802 return true;
8803 }
8804 else
8805 return false;
8806}
8807
24777478 8808bool wxRichTextStdRenderer::DrawBitmapBullet(wxRichTextParagraph* WXUNUSED(paragraph), wxDC& WXUNUSED(dc), const wxRichTextAttr& WXUNUSED(attr), const wxRect& WXUNUSED(rect))
d2d0adc7
JS
8809{
8810 // Currently unimplemented. The intention is to store bitmaps by name in a media store associated
8811 // with the buffer. The store will allow retrieval from memory, disk or other means.
8812 return false;
8813}
8814
8815/// Enumerate the standard bullet names currently supported
8816bool wxRichTextStdRenderer::EnumerateStandardBulletNames(wxArrayString& bulletNames)
8817{
04529b2a 8818 bulletNames.Add(wxTRANSLATE("standard/circle"));
603f702b 8819 bulletNames.Add(wxTRANSLATE("standard/circle-outline"));
04529b2a
JS
8820 bulletNames.Add(wxTRANSLATE("standard/square"));
8821 bulletNames.Add(wxTRANSLATE("standard/diamond"));
8822 bulletNames.Add(wxTRANSLATE("standard/triangle"));
d2d0adc7
JS
8823
8824 return true;
8825}
5d7836c4 8826
bec80f4f
JS
8827/*!
8828 * wxRichTextBox
8829 */
8830
603f702b 8831IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox, wxRichTextParagraphLayoutBox)
bec80f4f
JS
8832
8833wxRichTextBox::wxRichTextBox(wxRichTextObject* parent):
603f702b 8834 wxRichTextParagraphLayoutBox(parent)
bec80f4f
JS
8835{
8836}
8837
8838/// Draw the item
8db2e3ef 8839bool wxRichTextBox::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int style)
bec80f4f 8840{
603f702b
JS
8841 if (!IsShown())
8842 return true;
5ad9ae3a 8843
603f702b
JS
8844 // TODO: if the active object in the control, draw an indication.
8845 // We need to add the concept of active object, and not just focus object,
8846 // so we can apply commands (properties, delete, ...) to objects such as text boxes and images.
8847 // Ultimately we would like to be able to interactively resize an active object
8848 // using drag handles.
8db2e3ef 8849 return wxRichTextParagraphLayoutBox::Draw(dc, context, range, selection, rect, descent, style);
603f702b 8850}
5ad9ae3a 8851
603f702b
JS
8852/// Copy
8853void wxRichTextBox::Copy(const wxRichTextBox& obj)
8854{
8855 wxRichTextParagraphLayoutBox::Copy(obj);
8856}
8857
8858// Edit properties via a GUI
8859bool wxRichTextBox::EditProperties(wxWindow* parent, wxRichTextBuffer* buffer)
8860{
8861 wxRichTextObjectPropertiesDialog boxDlg(this, wxGetTopLevelParent(parent), wxID_ANY, _("Box Properties"));
8862 boxDlg.SetAttributes(GetAttributes());
8863
8864 if (boxDlg.ShowModal() == wxID_OK)
8865 {
8866 // By passing wxRICHTEXT_SETSTYLE_RESET, indeterminate attributes set by the user will be set as
8867 // indeterminate in the object.
8868 boxDlg.ApplyStyle(buffer->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RESET);
8869 return true;
5ad9ae3a 8870 }
603f702b
JS
8871 else
8872 return false;
bec80f4f
JS
8873}
8874
7c9fdebe
JS
8875/*!
8876 * wxRichTextField
8877 */
8878
8879IMPLEMENT_DYNAMIC_CLASS(wxRichTextField, wxRichTextParagraphLayoutBox)
8880
8881wxRichTextField::wxRichTextField(const wxString& fieldType, wxRichTextObject* parent):
8882 wxRichTextParagraphLayoutBox(parent)
8883{
8884 SetFieldType(fieldType);
8885}
8886
8887/// Draw the item
8888bool wxRichTextField::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int style)
8889{
8890 if (!IsShown())
8891 return true;
8892
8893 wxRichTextFieldType* fieldType = wxRichTextBuffer::FindFieldType(GetFieldType());
8894 if (fieldType && fieldType->Draw(this, dc, context, range, selection, rect, descent, style))
8895 return true;
8896
8897 // Fallback; but don't draw guidelines.
8898 style &= ~wxRICHTEXT_DRAW_GUIDELINES;
8899 return wxRichTextParagraphLayoutBox::Draw(dc, context, range, selection, rect, descent, style);
8900}
8901
8902bool wxRichTextField::Layout(wxDC& dc, wxRichTextDrawingContext& context, const wxRect& rect, const wxRect& parentRect, int style)
8903{
8904 wxRichTextFieldType* fieldType = wxRichTextBuffer::FindFieldType(GetFieldType());
8905 if (fieldType && fieldType->Layout(this, dc, context, rect, parentRect, style))
8906 return true;
8907
8908 // Fallback
8909 return wxRichTextParagraphLayoutBox::Layout(dc, context, rect, parentRect, style);
8910}
8911
914a4e23 8912bool wxRichTextField::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, wxRichTextDrawingContext& context, int flags, const wxPoint& position, const wxSize& parentSize, wxArrayInt* partialExtents) const
7c9fdebe
JS
8913{
8914 wxRichTextFieldType* fieldType = wxRichTextBuffer::FindFieldType(GetFieldType());
8915 if (fieldType)
914a4e23 8916 return fieldType->GetRangeSize((wxRichTextField*) this, range, size, descent, dc, context, flags, position, parentSize, partialExtents);
7c9fdebe 8917
914a4e23 8918 return wxRichTextParagraphLayoutBox::GetRangeSize(range, size, descent, dc, context, flags, position, parentSize, partialExtents);
7c9fdebe
JS
8919}
8920
8921/// Calculate range
8922void wxRichTextField::CalculateRange(long start, long& end)
8923{
8924 if (IsTopLevel())
8925 wxRichTextParagraphLayoutBox::CalculateRange(start, end);
8926 else
8927 wxRichTextObject::CalculateRange(start, end);
8928}
8929
8930/// Copy
8931void wxRichTextField::Copy(const wxRichTextField& obj)
8932{
8933 wxRichTextParagraphLayoutBox::Copy(obj);
8934
32423dd8 8935 UpdateField(GetBuffer());
7c9fdebe
JS
8936}
8937
8938// Edit properties via a GUI
8939bool wxRichTextField::EditProperties(wxWindow* parent, wxRichTextBuffer* buffer)
8940{
8941 wxRichTextFieldType* fieldType = wxRichTextBuffer::FindFieldType(GetFieldType());
8942 if (fieldType)
8943 return fieldType->EditProperties(this, parent, buffer);
8944
8945 return false;
8946}
8947
8948bool wxRichTextField::CanEditProperties() const
8949{
8950 wxRichTextFieldType* fieldType = wxRichTextBuffer::FindFieldType(GetFieldType());
8951 if (fieldType)
8952 return fieldType->CanEditProperties((wxRichTextField*) this);
8953
8954 return false;
8955}
8956
8957wxString wxRichTextField::GetPropertiesMenuLabel() const
8958{
8959 wxRichTextFieldType* fieldType = wxRichTextBuffer::FindFieldType(GetFieldType());
8960 if (fieldType)
8961 return fieldType->GetPropertiesMenuLabel((wxRichTextField*) this);
8962
8963 return wxEmptyString;
8964}
8965
32423dd8 8966bool wxRichTextField::UpdateField(wxRichTextBuffer* buffer)
7c9fdebe
JS
8967{
8968 wxRichTextFieldType* fieldType = wxRichTextBuffer::FindFieldType(GetFieldType());
8969 if (fieldType)
32423dd8 8970 return fieldType->UpdateField(buffer, (wxRichTextField*) this);
7c9fdebe
JS
8971
8972 return false;
8973}
8974
8975bool wxRichTextField::IsTopLevel() const
8976{
8977 wxRichTextFieldType* fieldType = wxRichTextBuffer::FindFieldType(GetFieldType());
8978 if (fieldType)
8979 return fieldType->IsTopLevel((wxRichTextField*) this);
8980
8981 return true;
8982}
8983
8984IMPLEMENT_CLASS(wxRichTextFieldType, wxObject)
8985
8986IMPLEMENT_CLASS(wxRichTextFieldTypeStandard, wxRichTextFieldType)
8987
8988wxRichTextFieldTypeStandard::wxRichTextFieldTypeStandard(const wxString& name, const wxString& label, int displayStyle)
8989{
8990 Init();
8991
8992 SetName(name);
8993 SetLabel(label);
8994 SetDisplayStyle(displayStyle);
8995}
8996
8997wxRichTextFieldTypeStandard::wxRichTextFieldTypeStandard(const wxString& name, const wxBitmap& bitmap, int displayStyle)
8998{
8999 Init();
9000
9001 SetName(name);
9002 SetBitmap(bitmap);
9003 SetDisplayStyle(displayStyle);
9004}
9005
9006void wxRichTextFieldTypeStandard::Init()
9007{
9008 m_displayStyle = wxRICHTEXT_FIELD_STYLE_RECTANGLE;
9009 m_font = wxFont(6, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
9010 m_textColour = *wxWHITE;
9011 m_borderColour = *wxBLACK;
9012 m_backgroundColour = *wxBLACK;
9013 m_verticalPadding = 1;
9014 m_horizontalPadding = 3;
9015 m_horizontalMargin = 2;
9016 m_verticalMargin = 0;
9017}
9018
9019void wxRichTextFieldTypeStandard::Copy(const wxRichTextFieldTypeStandard& field)
9020{
9021 wxRichTextFieldType::Copy(field);
9022
9023 m_label = field.m_label;
9024 m_displayStyle = field.m_displayStyle;
9025 m_font = field.m_font;
9026 m_textColour = field.m_textColour;
9027 m_borderColour = field.m_borderColour;
9028 m_backgroundColour = field.m_backgroundColour;
9029 m_verticalPadding = field.m_verticalPadding;
9030 m_horizontalPadding = field.m_horizontalPadding;
9031 m_horizontalMargin = field.m_horizontalMargin;
9032 m_bitmap = field.m_bitmap;
9033}
9034
9035bool wxRichTextFieldTypeStandard::Draw(wxRichTextField* obj, wxDC& dc, wxRichTextDrawingContext& WXUNUSED(context), const wxRichTextRange& WXUNUSED(range), const wxRichTextSelection& selection, const wxRect& rect, int descent, int WXUNUSED(style))
9036{
9037 if (m_displayStyle == wxRICHTEXT_FIELD_STYLE_COMPOSITE)
9038 return false; // USe default composite drawing
9039 else // if (m_displayStyle == wxRICHTEXT_FIELD_STYLE_RECTANGLE || m_displayStyle == wxRICHTEXT_FIELD_STYLE_NOBORDER)
9040 {
9041 int borderSize = 1;
9042
9043 wxPen borderPen(m_borderColour, 1, wxSOLID);
9044 wxBrush backgroundBrush(m_backgroundColour);
9045 wxColour textColour(m_textColour);
9046
9047 if (selection.WithinSelection(obj->GetRange().GetStart(), obj))
9048 {
9049 wxColour highlightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
9050 wxColour highlightTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
9051
9052 borderPen = wxPen(highlightTextColour, 1, wxSOLID);
9053 backgroundBrush = wxBrush(highlightColour);
9054
9055 wxCheckSetBrush(dc, backgroundBrush);
9056 wxCheckSetPen(dc, wxPen(highlightColour, 1, wxSOLID));
9057 dc.DrawRectangle(rect);
9058 }
9059
9060 if (m_displayStyle != wxRICHTEXT_FIELD_STYLE_NO_BORDER)
9061 borderSize = 0;
9062
9063 // objectRect is the area where the content is drawn, after margins around it have been taken into account
9064 wxRect objectRect = wxRect(wxPoint(rect.x + m_horizontalMargin, rect.y + wxMax(0, rect.height - descent - obj->GetCachedSize().y)),
9065 wxSize(obj->GetCachedSize().x - 2*m_horizontalMargin - borderSize, obj->GetCachedSize().y));
9066
9067 // clientArea is where the text is actually written
9068 wxRect clientArea = objectRect;
9069
9070 if (m_displayStyle == wxRICHTEXT_FIELD_STYLE_RECTANGLE)
9071 {
9072 dc.SetPen(borderPen);
9073 dc.SetBrush(backgroundBrush);
9074 dc.DrawRoundedRectangle(objectRect, 4.0);
9075 }
9076 else if (m_displayStyle == wxRICHTEXT_FIELD_STYLE_START_TAG)
9077 {
9078 int arrowLength = objectRect.height/2;
9079 clientArea.width -= (arrowLength - m_horizontalPadding);
9080
9081 wxPoint pts[5];
9082 pts[0].x = objectRect.x; pts[0].y = objectRect.y;
9083 pts[1].x = objectRect.x + objectRect.width - arrowLength; pts[1].y = objectRect.y;
9084 pts[2].x = objectRect.x + objectRect.width; pts[2].y = objectRect.y + (objectRect.height/2);
9085 pts[3].x = objectRect.x + objectRect.width - arrowLength; pts[3].y = objectRect.y + objectRect.height;
9086 pts[4].x = objectRect.x; pts[4].y = objectRect.y + objectRect.height;
9087 dc.SetPen(borderPen);
9088 dc.SetBrush(backgroundBrush);
9089 dc.DrawPolygon(5, pts);
9090 }
9091 else if (m_displayStyle == wxRICHTEXT_FIELD_STYLE_END_TAG)
9092 {
9093 int arrowLength = objectRect.height/2;
9094 clientArea.width -= (arrowLength - m_horizontalPadding);
9095 clientArea.x += (arrowLength - m_horizontalPadding);
9096
9097 wxPoint pts[5];
9098 pts[0].x = objectRect.x + objectRect.width; pts[0].y = objectRect.y;
9099 pts[1].x = objectRect.x + arrowLength; pts[1].y = objectRect.y;
9100 pts[2].x = objectRect.x; pts[2].y = objectRect.y + (objectRect.height/2);
9101 pts[3].x = objectRect.x + arrowLength; pts[3].y = objectRect.y + objectRect.height;
9102 pts[4].x = objectRect.x + objectRect.width; pts[4].y = objectRect.y + objectRect.height;
9103 dc.SetPen(borderPen);
9104 dc.SetBrush(backgroundBrush);
9105 dc.DrawPolygon(5, pts);
9106 }
9107
9108 if (m_bitmap.IsOk())
9109 {
9110 int x = clientArea.x + (clientArea.width - m_bitmap.GetWidth())/2;
9111 int y = clientArea.y + m_verticalPadding;
9112 dc.DrawBitmap(m_bitmap, x, y, true);
9113
9114 if (selection.WithinSelection(obj->GetRange().GetStart(), obj))
9115 {
9116 wxCheckSetBrush(dc, *wxBLACK_BRUSH);
9117 wxCheckSetPen(dc, *wxBLACK_PEN);
9118 dc.SetLogicalFunction(wxINVERT);
9119 dc.DrawRectangle(wxRect(x, y, m_bitmap.GetWidth(), m_bitmap.GetHeight()));
9120 dc.SetLogicalFunction(wxCOPY);
9121 }
9122 }
9123 else
9124 {
9125 wxString label(m_label);
9126 if (label.IsEmpty())
9127 label = wxT("??");
9128 int w, h, maxDescent;
9129 dc.SetFont(m_font);
9130 dc.GetTextExtent(m_label, & w, &h, & maxDescent);
9131 dc.SetTextForeground(textColour);
9132
9133 int x = clientArea.x + (clientArea.width - w)/2;
9134 int y = clientArea.y + (clientArea.height - (h - maxDescent))/2;
9135 dc.DrawText(m_label, x, y);
9136 }
9137 }
9138
9139 return true;
9140}
9141
9142bool wxRichTextFieldTypeStandard::Layout(wxRichTextField* obj, wxDC& dc, wxRichTextDrawingContext& context, const wxRect& WXUNUSED(rect), const wxRect& WXUNUSED(parentRect), int style)
9143{
9144 if (m_displayStyle == wxRICHTEXT_FIELD_STYLE_COMPOSITE)
9145 return false; // USe default composite layout
9146
9147 wxSize size = GetSize(obj, dc, context, style);
9148 obj->SetCachedSize(size);
9149 obj->SetMinSize(size);
9150 obj->SetMaxSize(size);
9151 return true;
9152}
9153
914a4e23 9154bool wxRichTextFieldTypeStandard::GetRangeSize(wxRichTextField* obj, const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, wxRichTextDrawingContext& context, int flags, const wxPoint& position, const wxSize& parentSize, wxArrayInt* partialExtents) const
7c9fdebe
JS
9155{
9156 if (IsTopLevel(obj))
914a4e23 9157 return obj->wxRichTextParagraphLayoutBox::GetRangeSize(range, size, descent, dc, context, flags, position, parentSize);
7c9fdebe
JS
9158 else
9159 {
9160 wxSize sz = GetSize(obj, dc, context, 0);
9161 if (partialExtents)
9162 {
9163 int lastSize;
9164 if (partialExtents->GetCount() > 0)
9165 lastSize = (*partialExtents)[partialExtents->GetCount()-1];
9166 else
9167 lastSize = 0;
9168 partialExtents->Add(lastSize + sz.x);
9169 }
9170 size = sz;
9171 return true;
9172 }
9173}
9174
9175wxSize wxRichTextFieldTypeStandard::GetSize(wxRichTextField* WXUNUSED(obj), wxDC& dc, wxRichTextDrawingContext& WXUNUSED(context), int WXUNUSED(style)) const
9176{
9177 int borderSize = 1;
9178 int w = 0, h = 0, maxDescent = 0;
9179
9180 wxSize sz;
9181 if (m_bitmap.IsOk())
9182 {
9183 w = m_bitmap.GetWidth();
9184 h = m_bitmap.GetHeight();
9185
9186 sz = wxSize(w + m_horizontalMargin*2, h + m_verticalMargin*2);
9187 }
9188 else
9189 {
9190 wxString label(m_label);
9191 if (label.IsEmpty())
9192 label = wxT("??");
9193 dc.SetFont(m_font);
9194 dc.GetTextExtent(label, & w, &h, & maxDescent);
9195
9196 sz = wxSize(w + m_horizontalPadding*2 + m_horizontalMargin*2, h + m_verticalPadding *2 + m_verticalMargin*2);
9197 }
9198
9199 if (m_displayStyle != wxRICHTEXT_FIELD_STYLE_NO_BORDER)
9200 {
9201 sz.x += borderSize*2;
9202 sz.y += borderSize*2;
9203 }
9204
9205 if (m_displayStyle == wxRICHTEXT_FIELD_STYLE_START_TAG || m_displayStyle == wxRICHTEXT_FIELD_STYLE_END_TAG)
9206 {
9207 // Add space for the arrow
9208 sz.x += (sz.y/2 - m_horizontalPadding);
9209 }
9210
9211 return sz;
9212}
9213
603f702b
JS
9214IMPLEMENT_DYNAMIC_CLASS(wxRichTextCell, wxRichTextBox)
9215
9216wxRichTextCell::wxRichTextCell(wxRichTextObject* parent):
9217 wxRichTextBox(parent)
bec80f4f 9218{
603f702b
JS
9219}
9220
9221/// Draw the item
8db2e3ef 9222bool wxRichTextCell::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int style)
603f702b 9223{
8db2e3ef 9224 return wxRichTextBox::Draw(dc, context, range, selection, rect, descent, style);
603f702b
JS
9225}
9226
9227/// Copy
9228void wxRichTextCell::Copy(const wxRichTextCell& obj)
9229{
9230 wxRichTextBox::Copy(obj);
9231}
9232
9233// Edit properties via a GUI
9234bool wxRichTextCell::EditProperties(wxWindow* parent, wxRichTextBuffer* buffer)
9235{
9236 // We need to gather common attributes for all selected cells.
9237
9238 wxRichTextTable* table = wxDynamicCast(GetParent(), wxRichTextTable);
9239 bool multipleCells = false;
9240 wxRichTextAttr attr;
9241
9242 if (table && buffer && buffer->GetRichTextCtrl() && buffer->GetRichTextCtrl()->GetSelection().IsValid() &&
9243 buffer->GetRichTextCtrl()->GetSelection().GetContainer() == GetParent())
5ad9ae3a 9244 {
603f702b
JS
9245 wxRichTextAttr clashingAttr, absentAttr;
9246 const wxRichTextSelection& sel = buffer->GetRichTextCtrl()->GetSelection();
9247 size_t i;
9248 int selectedCellCount = 0;
9249 for (i = 0; i < sel.GetCount(); i++)
9250 {
9251 const wxRichTextRange& range = sel[i];
9252 wxRichTextCell* cell = table->GetCell(range.GetStart());
9253 if (cell)
9254 {
9255 wxRichTextAttr cellStyle = cell->GetAttributes();
5ad9ae3a 9256
603f702b
JS
9257 CollectStyle(attr, cellStyle, clashingAttr, absentAttr);
9258
9259 selectedCellCount ++;
9260 }
9261 }
9262 multipleCells = selectedCellCount > 1;
5ad9ae3a 9263 }
603f702b
JS
9264 else
9265 {
9266 attr = GetAttributes();
9267 }
9268
9269 wxString caption;
9270 if (multipleCells)
9271 caption = _("Multiple Cell Properties");
9272 else
9273 caption = _("Cell Properties");
9274
9275 wxRichTextObjectPropertiesDialog cellDlg(this, wxGetTopLevelParent(parent), wxID_ANY, caption);
9276 cellDlg.SetAttributes(attr);
9277
80a46597 9278 wxRichTextSizePage* sizePage = wxDynamicCast(cellDlg.FindPage(wxCLASSINFO(wxRichTextSizePage)), wxRichTextSizePage);
603f702b
JS
9279 if (sizePage)
9280 {
9281 // We don't want position and floating controls for a cell.
9282 sizePage->ShowPositionControls(false);
9283 sizePage->ShowFloatingControls(false);
9284 }
9285
9286 if (cellDlg.ShowModal() == wxID_OK)
9287 {
9288 if (multipleCells)
9289 {
9290 const wxRichTextSelection& sel = buffer->GetRichTextCtrl()->GetSelection();
9291 // Apply the style; we interpret indeterminate attributes as 'don't touch this attribute'
9292 // since it may represent clashing attributes across multiple objects.
9293 table->SetCellStyle(sel, attr);
9294 }
9295 else
9296 // For a single object, indeterminate attributes set by the user should be reflected in the
9297 // actual object style, so pass the wxRICHTEXT_SETSTYLE_RESET flag to assign
9298 // the style directly instead of applying (which ignores indeterminate attributes,
9299 // leaving them as they were).
9300 cellDlg.ApplyStyle(buffer->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RESET);
9301 return true;
9302 }
9303 else
9304 return false;
9305}
9306
8a28cd66
JS
9307// The next 2 methods return span values. Note that the default is 1, not 0
9308int wxRichTextCell::GetColspan() const
9309{
9310 int span = 1;
9311 if (GetProperties().HasProperty(wxT("colspan")))
9312 {
9313 span = GetProperties().GetPropertyLong(wxT("colspan"));
9314 }
9315
9316 return span;
9317}
9318
9319int wxRichTextCell::GetRowspan() const
9320{
9321 int span = 1;
9322 if (GetProperties().HasProperty(wxT("rowspan")))
9323 {
9324 span = GetProperties().GetPropertyLong(wxT("rowspan"));
9325 }
9326
9327 return span;
9328}
9329
603f702b
JS
9330WX_DEFINE_OBJARRAY(wxRichTextObjectPtrArrayArray)
9331
9332IMPLEMENT_DYNAMIC_CLASS(wxRichTextTable, wxRichTextBox)
9333
9334wxRichTextTable::wxRichTextTable(wxRichTextObject* parent): wxRichTextBox(parent)
9335{
9336 m_rowCount = 0;
9337 m_colCount = 0;
9338}
5ad9ae3a 9339
603f702b 9340// Draws the object.
8db2e3ef 9341bool wxRichTextTable::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& range, const wxRichTextSelection& selection, const wxRect& rect, int descent, int style)
603f702b 9342{
8db2e3ef 9343 return wxRichTextBox::Draw(dc, context, range, selection, rect, descent, style);
bec80f4f
JS
9344}
9345
603f702b
JS
9346WX_DECLARE_OBJARRAY(wxRect, wxRichTextRectArray);
9347WX_DEFINE_OBJARRAY(wxRichTextRectArray);
9348
8e77fd8b
JS
9349
9350 // Helper function for Layout() that clears the space needed by a cell with rowspan > 1
9351int GetRowspanDisplacement(const wxRichTextTable* table, int row, int col, int paddingX, const wxArrayInt& colWidths)
9352{
9353 // If one or more cells above-left of this one has rowspan > 1, the affected cells below it
9354 // will have been hidden and have width 0. As a result they are ignored by the layout algorithm,
9355 // and all cells to their right are effectively shifted left. As a result there's no hole for
9356 // the spanning cell to fill.
9357 // So search back along the current row for hidden cells. However there's also the annoying issue of a
9358 // rowspanning cell that also has colspam. So we can't rely on the rowspanning cell being directly above
9359 // the first hidden one we come to. We also can't rely on a cell being hidden only by one type of span;
9360 // there's nothing to stop a cell being hidden by colspan, and then again hidden from above by rowspan.
9361 // The answer is to look above each hidden cell in turn, which I think covers all bases.
9362 int deltaX = 0;
9363 for (int prevcol = 0; prevcol < col; ++prevcol)
9364 {
9365 if (!table->GetCell(row, prevcol)->IsShown())
9366 {
9367 // We've found a hidden cell. If it's hidden because of colspan to its left, it's
9368 // already been taken into account; but not if there's a rowspanning cell above
9369 for (int prevrow = row-1; prevrow >= 0; --prevrow)
9370 {
9371 wxRichTextCell* cell = table->GetCell(prevrow, prevcol);
9372 if (cell && cell->IsShown())
9373 {
9374 int rowSpan = cell->GetRowspan();
9375 if (rowSpan > 1 && rowSpan > (row-prevrow))
9376 {
9377 // There is a rowspanning cell above above the hidden one, so we need
9378 // to right-shift the index cell by this column's width. Furthermore,
9379 // if the cell also colspans, we need to shift by all affected columns
9380 for (int colSpan = 0; colSpan < cell->GetColspan(); ++colSpan)
9381 deltaX += (colWidths[prevcol+colSpan] + paddingX);
9382 break;
9383 }
9384 }
9385 }
9386 }
9387 }
9388 return deltaX;
9389}
9390
9391 // Helper function for Layout() that expands any cell with rowspan > 1
9392void ExpandCellsWithRowspan(const wxRichTextTable* table, int paddingY, int& bottomY, wxDC& dc, wxRichTextDrawingContext& context, const wxRect& availableSpace, int style)
9393{
9394 // This is called when the table's cell layout is otherwise complete.
9395 // For any cell with rowspan > 1, expand downwards into the row(s) below.
9396
9397 // Start by finding the current 'y' of the top of each row, plus the bottom of the available area for cells.
9398 // Deduce this from the top of a visible cell in the row below. (If none are visible, the row will be invisible anyway and can be ignored.)
9399 const int rowCount = table->GetRowCount();
9400 const int colCount = table->GetColumnCount();
9401 wxArrayInt rowTops;
9402 rowTops.Add(0, rowCount+1);
5d98e603
VZ
9403 int row;
9404 for (row = 0; row < rowCount; ++row)
8e77fd8b
JS
9405 {
9406 for (int column = 0; column < colCount; ++column)
9407 {
9408 wxRichTextCell* cell = table->GetCell(row, column);
9409 if (cell && cell->IsShown())
9410 {
9411 rowTops[row] = cell->GetPosition().y;
9412 break;
9413 }
9414 }
9415 }
9416 rowTops[rowCount] = bottomY + paddingY; // The table bottom, which was passed to us
9417
9418 bool needsRelay = false;
9419
8e77fd8b
JS
9420 for (row = 0; row < rowCount-1; ++row) // -1 as the bottom row can't rowspan
9421 {
5d98e603 9422 for (int col = 0; col < colCount; ++col)
8e77fd8b
JS
9423 {
9424 wxRichTextCell* cell = table->GetCell(row, col);
9425 if (cell && cell->IsShown())
9426 {
9427 int span = cell->GetRowspan();
9428 if (span > 1)
9429 {
9430 span = wxMin(span, rowCount-row); // Don't try to span below the table!
9431 if (span < 2)
9432 continue;
9433
9434 int availableHeight = rowTops[row+span] - rowTops[row] - paddingY;
9435 wxSize newSize = wxSize(cell->GetCachedSize().GetWidth(), availableHeight);
9436 wxRect availableCellSpace = wxRect(cell->GetPosition(), newSize);
9437 cell->Invalidate(wxRICHTEXT_ALL);
9438 cell->Layout(dc, context, availableCellSpace, availableSpace, style);
9439 // Ensure there's room in the span to display its contents, else it'll overwrite lower rows
9440 int overhang = cell->GetCachedSize().GetHeight() - availableHeight;
9441 cell->SetCachedSize(newSize);
9442
9443 if (overhang > 0)
9444 {
9445 // There are 3 things to get right:
9446 // 1) The easiest is the rows below the span: they need to be downshifted by the overhang, and so does the table bottom itself
9447 // 2) The rows within the span, including the one holding this cell, need to be deepened by their share of the overhang
9448 // e.g. if rowspan == 3, each row should increase in depth by 1/3rd of the overhang.
9449 // 3) The cell with the rowspan shouldn't be touched in 2); its height will be set to the whole span later.
9450 int deltaY = overhang / span;
9451 int spare = overhang % span;
9452
9453 // Each row in the span needs to by deepened by its share of the overhang (give the first row any spare).
9454 // This is achieved by increasing the value stored in the following row's rowTops
9455 for (int spannedRows = 0; spannedRows < span; ++spannedRows)
9456 {
9457 rowTops[row+spannedRows+1] += ((deltaY * (spannedRows+1)) + (spannedRows == 0 ? spare:0));
9458 }
9459
9460 // Any rows below the span need shifting down
9461 for (int rowsBelow = row + span+1; rowsBelow <= rowCount; ++rowsBelow)
9462 {
9463 rowTops[rowsBelow] += overhang;
9464 }
9465
9466 needsRelay = true;
9467 }
9468 }
9469 }
9470 }
9471 }
9472
9473 if (!needsRelay)
9474 return;
9475
9476 // There were overflowing rowspanning cells, so layout yet again to make the increased row depths show
9477 for (row = 0; row < rowCount; ++row)
9478 {
5d98e603 9479 for (int col = 0; col < colCount; ++col)
8e77fd8b
JS
9480 {
9481 wxRichTextCell* cell = table->GetCell(row, col);
9482 if (cell && cell->IsShown())
9483 {
9484 wxPoint position(cell->GetPosition().x, rowTops[row]);
9485
9486 // GetRowspan() will usually return 1, but may be greater
9487 wxSize size(cell->GetCachedSize().GetWidth(), rowTops[row + cell->GetRowspan()] - rowTops[row] - paddingY);
9488
9489 wxRect availableCellSpace = wxRect(position, size);
9490 cell->Invalidate(wxRICHTEXT_ALL);
9491 cell->Layout(dc, context, availableCellSpace, availableSpace, style);
9492 cell->SetCachedSize(size);
9493 }
9494 }
9495
9496 bottomY = rowTops[rowCount] - paddingY;
9497 }
9498}
9499
603f702b
JS
9500// Lays the object out. rect is the space available for layout. Often it will
9501// be the specified overall space for this object, if trying to constrain
9502// layout to a particular size, or it could be the total space available in the
9503// parent. rect is the overall size, so we must subtract margins and padding.
9504// to get the actual available space.
8db2e3ef 9505bool wxRichTextTable::Layout(wxDC& dc, wxRichTextDrawingContext& context, const wxRect& rect, const wxRect& WXUNUSED(parentRect), int style)
bec80f4f 9506{
603f702b
JS
9507 SetPosition(rect.GetPosition());
9508
8a28cd66 9509 // The meaty bit. Calculate sizes of all cells and rows. Try to use
603f702b
JS
9510 // minimum size if within alloted size, then divide up remaining size
9511 // between rows/cols.
9512
9513 double scale = 1.0;
9514 wxRichTextBuffer* buffer = GetBuffer();
9515 if (buffer) scale = buffer->GetScale();
9516
8db2e3ef 9517 wxRect availableSpace = GetAvailableContentArea(dc, context, rect);
603f702b
JS
9518 wxTextAttrDimensionConverter converter(dc, scale, availableSpace.GetSize());
9519
8db2e3ef
JS
9520 wxRichTextAttr attr(GetAttributes());
9521 context.ApplyVirtualAttributes(attr, this);
9522
fc63fb9a 9523 bool tableHasPercentWidth = (attr.GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE);
603f702b
JS
9524 // If we have no fixed table size, and assuming we're not pushed for
9525 // space, then we don't have to try to stretch the table to fit the contents.
fc63fb9a
JS
9526 bool stretchToFitTableWidth = tableHasPercentWidth;
9527
603f702b 9528 int tableWidth = rect.width;
fc63fb9a 9529 if (attr.GetTextBoxAttr().GetWidth().IsValid() && !tableHasPercentWidth)
603f702b 9530 {
8db2e3ef 9531 tableWidth = converter.GetPixels(attr.GetTextBoxAttr().GetWidth());
603f702b
JS
9532
9533 // Fixed table width, so we do want to stretch columns out if necessary.
9534 stretchToFitTableWidth = true;
9535
9536 // Shouldn't be able to exceed the size passed to this function
9537 tableWidth = wxMin(rect.width, tableWidth);
9538 }
9539
9540 // Get internal padding
36307fdf 9541 int paddingLeft = 0, paddingTop = 0;
8db2e3ef
JS
9542 if (attr.GetTextBoxAttr().GetPadding().GetLeft().IsValid())
9543 paddingLeft = converter.GetPixels(attr.GetTextBoxAttr().GetPadding().GetLeft());
9544 if (attr.GetTextBoxAttr().GetPadding().GetTop().IsValid())
9545 paddingTop = converter.GetPixels(attr.GetTextBoxAttr().GetPadding().GetTop());
603f702b
JS
9546
9547 // Assume that left and top padding are also used for inter-cell padding.
9548 int paddingX = paddingLeft;
9549 int paddingY = paddingTop;
9550
9551 int totalLeftMargin = 0, totalRightMargin = 0, totalTopMargin = 0, totalBottomMargin = 0;
8db2e3ef 9552 GetTotalMargin(dc, buffer, attr, totalLeftMargin, totalRightMargin, totalTopMargin, totalBottomMargin);
603f702b
JS
9553
9554 // Internal table width - the area for content
9555 int internalTableWidth = tableWidth - totalLeftMargin - totalRightMargin;
9556
9557 int rowCount = m_cells.GetCount();
9558 if (m_colCount == 0 || rowCount == 0)
9559 {
9560 wxRect overallRect(rect.x, rect.y, totalLeftMargin + totalRightMargin, totalTopMargin + totalBottomMargin);
9561 SetCachedSize(overallRect.GetSize());
9562
9563 // Zero content size
9564 SetMinSize(overallRect.GetSize());
9565 SetMaxSize(GetMinSize());
9566 return true;
9567 }
9568
9569 // The final calculated widths
bb7bbd12
JS
9570 wxArrayInt colWidths;
9571 colWidths.Add(0, m_colCount);
603f702b 9572
bb7bbd12
JS
9573 wxArrayInt absoluteColWidths;
9574 absoluteColWidths.Add(0, m_colCount);
7c9fdebe 9575
bb7bbd12
JS
9576 wxArrayInt percentageColWidths;
9577 percentageColWidths.Add(0, m_colCount);
603f702b
JS
9578 // wxArrayInt percentageColWidthsSpanning(m_colCount);
9579 // These are only relevant when the first column contains spanning information.
9580 // wxArrayInt columnSpans(m_colCount); // Each contains 1 for non-spanning cell, > 1 for spanning cell.
bb7bbd12
JS
9581 wxArrayInt maxColWidths;
9582 maxColWidths.Add(0, m_colCount);
9583 wxArrayInt minColWidths;
9584 minColWidths.Add(0, m_colCount);
603f702b
JS
9585
9586 wxSize tableSize(tableWidth, 0);
9587
9588 int i, j, k;
9589
9590 for (i = 0; i < m_colCount; i++)
9591 {
9592 absoluteColWidths[i] = 0;
9593 // absoluteColWidthsSpanning[i] = 0;
9594 percentageColWidths[i] = -1;
9595 // percentageColWidthsSpanning[i] = -1;
9596 colWidths[i] = 0;
9597 maxColWidths[i] = 0;
9598 minColWidths[i] = 0;
9599 // columnSpans[i] = 1;
9600 }
9601
9602 // (0) Determine which cells are visible according to spans
9603 // 1 2 3 4 5
9604 // __________________
9605 // | | | | | 1
9606 // |------| |----|
9607 // |------| | | 2
9608 // |------| | | 3
9609 // |------------------|
9610 // |__________________| 4
9611
9612 // To calculate cell visibility:
9613 // First find all spanning cells. Build an array of span records with start x, y and end x, y.
9614 // Then for each cell, test whether we're within one of those cells, and unless we're at the start of
9615 // that cell, hide the cell.
9616
9617 // We can also use this array to match the size of spanning cells to the grid. Or just do
9618 // this when we iterate through all cells.
9619
9620 // 0.1: add spanning cells to an array
9621 wxRichTextRectArray rectArray;
9622 for (j = 0; j < m_rowCount; j++)
9623 {
9624 for (i = 0; i < m_colCount; i++)
9625 {
8a28cd66
JS
9626 wxRichTextCell* cell = GetCell(j, i);
9627 int colSpan = cell->GetColspan();
9628 int rowSpan = cell->GetRowspan();
603f702b
JS
9629 if (colSpan > 1 || rowSpan > 1)
9630 {
9631 rectArray.Add(wxRect(i, j, colSpan, rowSpan));
9632 }
9633 }
9634 }
9635 // 0.2: find which cells are subsumed by a spanning cell
9636 for (j = 0; j < m_rowCount; j++)
9637 {
9638 for (i = 0; i < m_colCount; i++)
9639 {
8a28cd66 9640 wxRichTextCell* cell = GetCell(j, i);
603f702b
JS
9641 if (rectArray.GetCount() == 0)
9642 {
9643 cell->Show(true);
9644 }
9645 else
9646 {
8a28cd66
JS
9647 int colSpan = cell->GetColspan();
9648 int rowSpan = cell->GetRowspan();
9649
603f702b
JS
9650 if (colSpan > 1 || rowSpan > 1)
9651 {
9652 // Assume all spanning cells are shown
9653 cell->Show(true);
9654 }
9655 else
9656 {
9657 bool shown = true;
9658 for (k = 0; k < (int) rectArray.GetCount(); k++)
9659 {
9660 if (rectArray[k].Contains(wxPoint(i, j)))
9661 {
9662 shown = false;
9663 break;
9664 }
9665 }
9666 cell->Show(shown);
9667 }
9668 }
9669 }
9670 }
9671
8a28cd66 9672 // Find the first spanned cell in each row that spans the most columns and doesn't
603f702b
JS
9673 // overlap with a spanned cell starting at a previous column position.
9674 // This means we need to keep an array of rects so we can check. However
9675 // it does also mean that some spans simply may not be taken into account
9676 // where there are different spans happening on different rows. In these cases,
9677 // they will simply be as wide as their constituent columns.
9678
9679 // (1) Do an initial layout for all cells to get minimum and maximum size, and get
9680 // the absolute or percentage width of each column.
9681
9682 for (j = 0; j < m_rowCount; j++)
9683 {
9684 // First get the overall margins so we can calculate percentage widths based on
9685 // the available content space for all cells on the row
9686
9687 int overallRowContentMargin = 0;
9688 int visibleCellCount = 0;
9689
9690 for (i = 0; i < m_colCount; i++)
9691 {
9692 wxRichTextBox* cell = GetCell(j, i);
9693 if (cell->IsShown())
9694 {
9695 int cellTotalLeftMargin = 0, cellTotalRightMargin = 0, cellTotalTopMargin = 0, cellTotalBottomMargin = 0;
9696 GetTotalMargin(dc, buffer, cell->GetAttributes(), cellTotalLeftMargin, cellTotalRightMargin, cellTotalTopMargin, cellTotalBottomMargin);
9697
9698 overallRowContentMargin += (cellTotalLeftMargin + cellTotalRightMargin);
9699 visibleCellCount ++;
9700 }
9701 }
9702
9703 // Add in inter-cell padding
9704 overallRowContentMargin += ((visibleCellCount-1) * paddingX);
9705
9706 int rowContentWidth = internalTableWidth - overallRowContentMargin;
9707 wxSize rowTableSize(rowContentWidth, 0);
9708 wxTextAttrDimensionConverter converter(dc, scale, rowTableSize);
9709
9710 for (i = 0; i < m_colCount; i++)
9711 {
8a28cd66 9712 wxRichTextCell* cell = GetCell(j, i);
603f702b
JS
9713 if (cell->IsShown())
9714 {
8a28cd66 9715 int colSpan = cell->GetColspan();
603f702b
JS
9716
9717 // Lay out cell to find min/max widths
9718 cell->Invalidate(wxRICHTEXT_ALL);
8db2e3ef 9719 cell->Layout(dc, context, availableSpace, availableSpace, style);
603f702b
JS
9720
9721 if (colSpan == 1)
9722 {
9723 int absoluteCellWidth = -1;
9724 int percentageCellWidth = -1;
9725
9726 // I think we need to calculate percentages from the internal table size,
9727 // minus the padding between cells which we'll need to calculate from the
9728 // (number of VISIBLE cells - 1)*paddingX. Then percentages that add up to 100%
9729 // will add up to 100%. In CSS, the width specifies the cell's content rect width,
9730 // so if we want to conform to that we'll need to add in the overall cell margins.
9731 // However, this will make it difficult to specify percentages that add up to
9732 // 100% and still fit within the table width.
9733 // Let's say two cells have 50% width. They have 10 pixels of overall margin each.
9734 // The table content rect is 500 pixels and the inter-cell padding is 20 pixels.
9735 // If we're using internal content size for the width, we would calculate the
9736 // the overall cell width for n cells as:
9737 // (500 - 20*(n-1) - overallCellMargin1 - overallCellMargin2 - ...) * percentage / 100
9738 // + thisOverallCellMargin
9739 // = 500 - 20 - 10 - 10) * 0.5 + 10 = 240 pixels overall cell width.
9740 // Adding this back, we get 240 + 240 + 20 = 500 pixels.
9741
9742 if (cell->GetAttributes().GetTextBoxAttr().GetWidth().IsValid())
9743 {
9744 int w = converter.GetPixels(cell->GetAttributes().GetTextBoxAttr().GetWidth());
9745 if (cell->GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
9746 {
9747 percentageCellWidth = w;
9748 }
9749 else
9750 {
9751 absoluteCellWidth = w;
9752 }
9753 // Override absolute width with minimum width if necessary
9754 if (cell->GetMinSize().x > 0 && absoluteCellWidth !=1 && cell->GetMinSize().x > absoluteCellWidth)
9755 absoluteCellWidth = cell->GetMinSize().x;
9756 }
9757
9758 if (absoluteCellWidth != -1)
9759 {
9760 if (absoluteCellWidth > absoluteColWidths[i])
9761 absoluteColWidths[i] = absoluteCellWidth;
9762 }
9763
9764 if (percentageCellWidth != -1)
9765 {
9766 if (percentageCellWidth > percentageColWidths[i])
9767 percentageColWidths[i] = percentageCellWidth;
9768 }
9769
9770 if (colSpan == 1 && cell->GetMinSize().x && cell->GetMinSize().x > minColWidths[i])
9771 minColWidths[i] = cell->GetMinSize().x;
9772 if (colSpan == 1 && cell->GetMaxSize().x && cell->GetMaxSize().x > maxColWidths[i])
9773 maxColWidths[i] = cell->GetMaxSize().x;
9774 }
9775 }
9776 }
9777 }
9778
9779 // (2) Allocate initial column widths from minimum widths, absolute values and proportions
9780 // TODO: simply merge this into (1).
9781 for (i = 0; i < m_colCount; i++)
9782 {
9783 if (absoluteColWidths[i] > 0)
9784 {
9785 colWidths[i] = absoluteColWidths[i];
9786 }
9787 else if (percentageColWidths[i] > 0)
9788 {
9789 colWidths[i] = percentageColWidths[i];
9790
9791 // This is rubbish - we calculated the absolute widths from percentages, so
9792 // we can't do it again here.
9793 //colWidths[i] = (int) (double(percentageColWidths[i]) * double(tableWidth) / 100.0 + 0.5);
9794 }
9795 }
9796
9797 // (3) Process absolute or proportional widths of spanning columns,
9798 // now that we know what our fixed column widths are going to be.
9799 // Spanned cells will try to adjust columns so the span will fit.
9800 // Even existing fixed column widths can be expanded if necessary.
9801 // Actually, currently fixed columns widths aren't adjusted; instead,
9802 // the algorithm favours earlier rows and adjusts unspecified column widths
9803 // the first time only. After that, we can't know whether the column has been
9804 // specified explicitly or not. (We could make a note if necessary.)
9805 for (j = 0; j < m_rowCount; j++)
9806 {
9807 // First get the overall margins so we can calculate percentage widths based on
9808 // the available content space for all cells on the row
9809
9810 int overallRowContentMargin = 0;
9811 int visibleCellCount = 0;
9812
9813 for (i = 0; i < m_colCount; i++)
9814 {
9815 wxRichTextBox* cell = GetCell(j, i);
9816 if (cell->IsShown())
9817 {
9818 int cellTotalLeftMargin = 0, cellTotalRightMargin = 0, cellTotalTopMargin = 0, cellTotalBottomMargin = 0;
9819 GetTotalMargin(dc, buffer, cell->GetAttributes(), cellTotalLeftMargin, cellTotalRightMargin, cellTotalTopMargin, cellTotalBottomMargin);
9820
9821 overallRowContentMargin += (cellTotalLeftMargin + cellTotalRightMargin);
9822 visibleCellCount ++;
9823 }
9824 }
9825
9826 // Add in inter-cell padding
9827 overallRowContentMargin += ((visibleCellCount-1) * paddingX);
9828
9829 int rowContentWidth = internalTableWidth - overallRowContentMargin;
9830 wxSize rowTableSize(rowContentWidth, 0);
9831 wxTextAttrDimensionConverter converter(dc, scale, rowTableSize);
9832
9833 for (i = 0; i < m_colCount; i++)
9834 {
8a28cd66 9835 wxRichTextCell* cell = GetCell(j, i);
603f702b
JS
9836 if (cell->IsShown())
9837 {
8a28cd66 9838 int colSpan = cell->GetColspan();
603f702b
JS
9839 if (colSpan > 1)
9840 {
9841 int spans = wxMin(colSpan, m_colCount - i);
9842 int cellWidth = 0;
9843 if (spans > 0)
9844 {
9845 if (cell->GetAttributes().GetTextBoxAttr().GetWidth().IsValid())
9846 {
9847 cellWidth = converter.GetPixels(cell->GetAttributes().GetTextBoxAttr().GetWidth());
9848 // Override absolute width with minimum width if necessary
9849 if (cell->GetMinSize().x > 0 && cellWidth !=1 && cell->GetMinSize().x > cellWidth)
9850 cellWidth = cell->GetMinSize().x;
9851 }
9852 else
9853 {
9854 // Do we want to do this? It's the only chance we get to
9855 // use the cell's min/max sizes, so we need to work out
9856 // how we're going to balance the unspecified spanning cell
9857 // width with the possibility more-constrained constituent cell widths.
9858 // Say there's a tiny bitmap giving it a max width of 10 pixels. We
9859 // don't want to constraint all the spanned columns to fit into this cell.
9860 // OK, let's say that if any of the constituent columns don't fit,
9861 // then we simply stop constraining the columns; instead, we'll just fit the spanning
9862 // cells to the columns later.
9863 cellWidth = cell->GetMinSize().x;
9864 if (cell->GetMaxSize().x > cellWidth)
9865 cellWidth = cell->GetMaxSize().x;
9866 }
9867
9868 // Subtract the padding between cells
9869 int spanningWidth = cellWidth;
9870 spanningWidth -= paddingX * (spans-1);
9871
9872 if (spanningWidth > 0)
9873 {
9874 // Now share the spanning width between columns within that span
9875 // TODO: take into account min widths of columns within the span
9876 int spanningWidthLeft = spanningWidth;
9877 int stretchColCount = 0;
9878 for (k = i; k < (i+spans); k++)
9879 {
9880 if (colWidths[k] > 0) // absolute or proportional width has been specified
9881 spanningWidthLeft -= colWidths[k];
9882 else
9883 stretchColCount ++;
9884 }
9885 // Now divide what's left between the remaining columns
9886 int colShare = 0;
9887 if (stretchColCount > 0)
9888 colShare = spanningWidthLeft / stretchColCount;
9889 int colShareRemainder = spanningWidthLeft - (colShare * stretchColCount);
9890
9891 // If fixed-width columns are currently too big, then we'll later
9892 // stretch the spanned cell to fit.
9893
9894 if (spanningWidthLeft > 0)
9895 {
9896 for (k = i; k < (i+spans); k++)
9897 {
9898 if (colWidths[k] <= 0) // absolute or proportional width has not been specified
9899 {
9900 int newWidth = colShare;
9901 if (k == (i+spans-1))
9902 newWidth += colShareRemainder; // ensure all pixels are filled
9903 colWidths[k] = newWidth;
9904 }
9905 }
9906 }
9907 }
9908 }
9909 }
9910 }
9911 }
9912 }
9913
9914 // (4) Next, share any remaining space out between columns that have not yet been calculated.
9915 // TODO: take into account min widths of columns within the span
9916 int tableWidthMinusPadding = internalTableWidth - (m_colCount-1)*paddingX;
9917 int widthLeft = tableWidthMinusPadding;
9918 int stretchColCount = 0;
9919 for (i = 0; i < m_colCount; i++)
9920 {
603f702b
JS
9921 // Subtract min width from width left, then
9922 // add the colShare to the min width
9923 if (colWidths[i] > 0) // absolute or proportional width has been specified
9924 widthLeft -= colWidths[i];
9925 else
9926 {
9927 if (minColWidths[i] > 0)
9928 widthLeft -= minColWidths[i];
9929
9930 stretchColCount ++;
9931 }
9932 }
9933
9934 // Now divide what's left between the remaining columns
9935 int colShare = 0;
9936 if (stretchColCount > 0)
9937 colShare = widthLeft / stretchColCount;
9938 int colShareRemainder = widthLeft - (colShare * stretchColCount);
9939
9940 // Check we don't have enough space, in which case shrink all columns, overriding
9941 // any absolute/proportional widths
9942 // TODO: actually we would like to divide up the shrinkage according to size.
9943 // How do we calculate the proportions that will achieve this?
9944 // Could first choose an arbitrary value for stretching cells, and then calculate
9945 // factors to multiply each width by.
9946 // TODO: want to record this fact and pass to an iteration that tries e.g. min widths
9947 if (widthLeft < 0 || (stretchToFitTableWidth && (stretchColCount == 0)))
9948 {
9949 colShare = tableWidthMinusPadding / m_colCount;
9950 colShareRemainder = tableWidthMinusPadding - (colShare * m_colCount);
9951 for (i = 0; i < m_colCount; i++)
9952 {
9953 colWidths[i] = 0;
9954 minColWidths[i] = 0;
9955 }
9956 }
9957
9958 // We have to adjust the columns if either we need to shrink the
9959 // table to fit the parent/table width, or we explicitly set the
9960 // table width and need to stretch out the table.
9961 if (widthLeft < 0 || stretchToFitTableWidth)
9962 {
9963 for (i = 0; i < m_colCount; i++)
9964 {
9965 if (colWidths[i] <= 0) // absolute or proportional width has not been specified
9966 {
9967 if (minColWidths[i] > 0)
9968 colWidths[i] = minColWidths[i] + colShare;
9969 else
9970 colWidths[i] = colShare;
9971 if (i == (m_colCount-1))
9972 colWidths[i] += colShareRemainder; // ensure all pixels are filled
9973 }
9974 }
9975 }
9976
9977 // TODO: if spanned cells have no specified or max width, make them the
9978 // as big as the columns they span. Do this for all spanned cells in all
9979 // rows, of course. Size any spanned cells left over at the end - even if they
9980 // have width > 0, make sure they're limited to the appropriate column edge.
9981
9982
9983/*
9984 Sort out confusion between content width
9985 and overall width later. For now, assume we specify overall width.
9986
9987 So, now we've laid out the table to fit into the given space
9988 and have used specified widths and minimum widths.
9989
9990 Now we need to consider how we will try to take maximum width into account.
9991
9992*/
9993
9994 // (??) TODO: take max width into account
9995
9996 // (6) Lay out all cells again with the current values
9997
9998 int maxRight = 0;
9999 int y = availableSpace.y;
10000 for (j = 0; j < m_rowCount; j++)
10001 {
10002 int x = availableSpace.x; // TODO: take into account centering etc.
10003 int maxCellHeight = 0;
10004 int maxSpecifiedCellHeight = 0;
10005
bb7bbd12
JS
10006 wxArrayInt actualWidths;
10007 actualWidths.Add(0, m_colCount);
603f702b
JS
10008
10009 wxTextAttrDimensionConverter converter(dc, scale);
10010 for (i = 0; i < m_colCount; i++)
10011 {
10012 wxRichTextCell* cell = GetCell(j, i);
10013 if (cell->IsShown())
10014 {
603f702b
JS
10015 // Get max specified cell height
10016 // Don't handle percentages for height
10017 if (cell->GetAttributes().GetTextBoxAttr().GetHeight().IsValid() && cell->GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() != wxTEXT_ATTR_UNITS_PERCENTAGE)
10018 {
10019 int h = converter.GetPixels(cell->GetAttributes().GetTextBoxAttr().GetHeight());
10020 if (h > maxSpecifiedCellHeight)
10021 maxSpecifiedCellHeight = h;
10022 }
10023
10024 if (colWidths[i] > 0) // absolute or proportional width has been specified
10025 {
8a28cd66 10026 int colSpan = cell->GetColspan();
603f702b
JS
10027 wxRect availableCellSpace;
10028
8a28cd66 10029 // Take into account spans
603f702b
JS
10030 if (colSpan > 1)
10031 {
10032 // Calculate the size of this spanning cell from its constituent columns
8a28cd66 10033 int xx = 0;
603f702b 10034 int spans = wxMin(colSpan, m_colCount - i);
8a28cd66 10035 for (k = i; k < (i+spans); k++)
603f702b
JS
10036 {
10037 if (k != i)
10038 xx += paddingX;
10039 xx += colWidths[k];
10040 }
10041 availableCellSpace = wxRect(x, y, xx, -1);
10042 }
10043 else
10044 availableCellSpace = wxRect(x, y, colWidths[i], -1);
10045
10046 // Store actual width so we can force cell to be the appropriate width on the final loop
10047 actualWidths[i] = availableCellSpace.GetWidth();
10048
8e77fd8b
JS
10049 // We now need to shift right by the width of any rowspanning cells above-left of us
10050 int deltaX = GetRowspanDisplacement(this, j, i, paddingX, colWidths);
10051 availableCellSpace.SetX(availableCellSpace.GetX() + deltaX);
10052
603f702b
JS
10053 // Lay out cell
10054 cell->Invalidate(wxRICHTEXT_ALL);
8db2e3ef 10055 cell->Layout(dc, context, availableCellSpace, availableSpace, style);
603f702b
JS
10056
10057 // TODO: use GetCachedSize().x to compute 'natural' size
10058
10059 x += (availableCellSpace.GetWidth() + paddingX);
8e77fd8b 10060 if ((cell->GetCachedSize().y > maxCellHeight) && (cell->GetRowspan() < 2))
603f702b
JS
10061 maxCellHeight = cell->GetCachedSize().y;
10062 }
10063 }
10064 }
10065
10066 maxCellHeight = wxMax(maxCellHeight, maxSpecifiedCellHeight);
10067
10068 for (i = 0; i < m_colCount; i++)
10069 {
10070 wxRichTextCell* cell = GetCell(j, i);
10071 if (cell->IsShown())
10072 {
10073 wxRect availableCellSpace = wxRect(cell->GetPosition(), wxSize(actualWidths[i], maxCellHeight));
10074 // Lay out cell with new height
10075 cell->Invalidate(wxRICHTEXT_ALL);
8db2e3ef 10076 cell->Layout(dc, context, availableCellSpace, availableSpace, style);
603f702b
JS
10077
10078 // Make sure the cell size really is the appropriate size,
10079 // not the calculated box size
10080 cell->SetCachedSize(wxSize(actualWidths[i], maxCellHeight));
10081
10082 maxRight = wxMax(maxRight, cell->GetPosition().x + cell->GetCachedSize().x);
10083 }
10084 }
10085
10086 y += maxCellHeight;
10087 if (j < (m_rowCount-1))
10088 y += paddingY;
10089 }
8e77fd8b
JS
10090
10091 // Finally we need to expand any cell with rowspan > 1. We couldn't earlier; lower rows' heights weren't known
10092 ExpandCellsWithRowspan(this, paddingY, y, dc, context, availableSpace, style);
603f702b
JS
10093
10094 // We need to add back the margins etc.
10095 {
10096 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
10097 contentRect = wxRect(wxPoint(0, 0), wxSize(maxRight - availableSpace.x, y - availableSpace.y));
8db2e3ef 10098 GetBoxRects(dc, GetBuffer(), attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
10099 SetCachedSize(marginRect.GetSize());
10100 }
10101
10102 // TODO: calculate max size
10103 {
10104 SetMaxSize(GetCachedSize());
10105 }
10106
10107 // TODO: calculate min size
10108 {
10109 SetMinSize(GetCachedSize());
10110 }
10111
10112 // TODO: currently we use either a fixed table width or the parent's size.
10113 // We also want to be able to calculate the table width from its content,
10114 // whether using fixed column widths or cell content min/max width.
10115 // Probably need a boolean flag to say whether we need to stretch cells
10116 // to fit the table width, or to simply use min/max cell widths. The
10117 // trouble with this is that if cell widths are not specified, they
10118 // will be tiny; we could use arbitrary defaults but this seems unsatisfactory.
10119 // Anyway, ignoring that problem, we probably need to factor layout into a function
10120 // that can can calculate the maximum unconstrained layout in case table size is
10121 // not specified. Then LayoutToBestSize() can choose to use either parent size to
10122 // constrain Layout(), or the previously-calculated max size to constraint layout.
10123
10124 return true;
10125}
10126
10127// Finds the absolute position and row height for the given character position
8db2e3ef 10128bool wxRichTextTable::FindPosition(wxDC& dc, wxRichTextDrawingContext& context, long index, wxPoint& pt, int* height, bool forceLineStart)
603f702b
JS
10129{
10130 wxRichTextCell* child = GetCell(index+1);
10131 if (child)
10132 {
10133 // Find the position at the start of the child cell, since the table doesn't
10134 // have any caret position of its own.
8db2e3ef 10135 return child->FindPosition(dc, context, -1, pt, height, forceLineStart);
603f702b
JS
10136 }
10137 else
10138 return false;
10139}
10140
10141// Get the cell at the given character position (in the range of the table).
10142wxRichTextCell* wxRichTextTable::GetCell(long pos) const
10143{
10144 int row = 0, col = 0;
10145 if (GetCellRowColumnPosition(pos, row, col))
10146 {
10147 return GetCell(row, col);
10148 }
10149 else
10150 return NULL;
10151}
10152
10153// Get the row/column for a given character position
10154bool wxRichTextTable::GetCellRowColumnPosition(long pos, int& row, int& col) const
10155{
10156 if (m_colCount == 0 || m_rowCount == 0)
10157 return false;
10158
10159 row = (int) (pos / m_colCount);
10160 col = pos - (row * m_colCount);
10161
10162 wxASSERT(row < m_rowCount && col < m_colCount);
10163
10164 if (row < m_rowCount && col < m_colCount)
10165 return true;
10166 else
10167 return false;
10168}
10169
10170// Calculate range, taking row/cell ordering into account instead of relying
10171// on list ordering.
10172void wxRichTextTable::CalculateRange(long start, long& end)
10173{
10174 long current = start;
10175 long lastEnd = current;
10176
10177 if (IsTopLevel())
10178 {
10179 current = 0;
10180 lastEnd = 0;
10181 }
10182
10183 int i, j;
10184 for (i = 0; i < m_rowCount; i++)
10185 {
10186 for (j = 0; j < m_colCount; j++)
10187 {
10188 wxRichTextCell* child = GetCell(i, j);
10189 if (child)
10190 {
10191 long childEnd = 0;
10192
10193 child->CalculateRange(current, childEnd);
10194
10195 lastEnd = childEnd;
10196 current = childEnd + 1;
10197 }
10198 }
10199 }
10200
10201 // A top-level object always has a range of size 1,
10202 // because its children don't count at this level.
10203 end = start;
10204 m_range.SetRange(start, start);
10205
10206 // An object with no children has zero length
10207 if (m_children.GetCount() == 0)
10208 lastEnd --;
10209 m_ownRange.SetRange(0, lastEnd);
10210}
10211
10212// Gets the range size.
914a4e23 10213bool wxRichTextTable::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, wxRichTextDrawingContext& context, int flags, const wxPoint& position, const wxSize& parentSize, wxArrayInt* partialExtents) const
603f702b 10214{
914a4e23 10215 return wxRichTextBox::GetRangeSize(range, size, descent, dc, context, flags, position, parentSize, partialExtents);
603f702b
JS
10216}
10217
10218// Deletes content in the given range.
10219bool wxRichTextTable::DeleteRange(const wxRichTextRange& WXUNUSED(range))
10220{
10221 // TODO: implement deletion of cells
10222 return true;
10223}
10224
10225// Gets any text in this object for the given range.
10226wxString wxRichTextTable::GetTextForRange(const wxRichTextRange& range) const
10227{
10228 return wxRichTextBox::GetTextForRange(range);
10229}
10230
10231// Copies this object.
10232void wxRichTextTable::Copy(const wxRichTextTable& obj)
10233{
10234 wxRichTextBox::Copy(obj);
10235
10236 ClearTable();
10237
10238 m_rowCount = obj.m_rowCount;
10239 m_colCount = obj.m_colCount;
10240
10241 m_cells.Add(wxRichTextObjectPtrArray(), m_rowCount);
10242
10243 int i, j;
10244 for (i = 0; i < m_rowCount; i++)
10245 {
10246 wxRichTextObjectPtrArray& colArray = m_cells[i];
10247 for (j = 0; j < m_colCount; j++)
10248 {
10249 wxRichTextCell* cell = wxDynamicCast(obj.GetCell(i, j)->Clone(), wxRichTextCell);
10250 AppendChild(cell);
10251
10252 colArray.Add(cell);
10253 }
10254 }
10255}
10256
10257void wxRichTextTable::ClearTable()
10258{
10259 m_cells.Clear();
10260 DeleteChildren();
0a6ec346
VZ
10261 m_rowCount = 0;
10262 m_colCount = 0;
603f702b
JS
10263}
10264
10265bool wxRichTextTable::CreateTable(int rows, int cols)
10266{
10267 ClearTable();
10268
10269 m_rowCount = rows;
10270 m_colCount = cols;
10271
10272 m_cells.Add(wxRichTextObjectPtrArray(), rows);
10273
10274 int i, j;
10275 for (i = 0; i < rows; i++)
10276 {
10277 wxRichTextObjectPtrArray& colArray = m_cells[i];
10278 for (j = 0; j < cols; j++)
10279 {
10280 wxRichTextCell* cell = new wxRichTextCell;
10281 AppendChild(cell);
10282 cell->AddParagraph(wxEmptyString);
10283
10284 colArray.Add(cell);
10285 }
10286 }
10287
10288 return true;
10289}
10290
10291wxRichTextCell* wxRichTextTable::GetCell(int row, int col) const
10292{
10293 wxASSERT(row < m_rowCount);
10294 wxASSERT(col < m_colCount);
10295
10296 if (row < m_rowCount && col < m_colCount)
10297 {
10298 wxRichTextObjectPtrArray& colArray = m_cells[row];
10299 wxRichTextObject* obj = colArray[col];
10300 return wxDynamicCast(obj, wxRichTextCell);
10301 }
10302 else
d67faa04 10303 return NULL;
603f702b
JS
10304}
10305
10306// Returns a selection object specifying the selections between start and end character positions.
10307// For example, a table would deduce what cells (of range length 1) are selected when dragging across the table.
10308wxRichTextSelection wxRichTextTable::GetSelection(long start, long end) const
10309{
10310 wxRichTextSelection selection;
10311 selection.SetContainer((wxRichTextTable*) this);
10312
10313 if (start > end)
10314 {
10315 long tmp = end;
10316 end = start;
10317 start = tmp;
10318 }
10319
10320 wxASSERT( start >= 0 && end < (m_colCount * m_rowCount));
10321
10322 if (end >= (m_colCount * m_rowCount))
10323 return selection;
10324
10325 // We need to find the rectangle of cells that is described by the rectangle
10326 // with start, end as the diagonal. Make sure we don't add cells that are
10327 // not currenty visible because they are overlapped by spanning cells.
10328/*
10329 --------------------------
10330 | 0 | 1 | 2 | 3 | 4 |
10331 --------------------------
10332 | 5 | 6 | 7 | 8 | 9 |
10333 --------------------------
10334 | 10 | 11 | 12 | 13 | 14 |
10335 --------------------------
10336 | 15 | 16 | 17 | 18 | 19 |
10337 --------------------------
10338
10339 Let's say we select 6 -> 18.
10340
10341 Left and right edge cols of rectangle are 1 and 3 inclusive. Find least/greatest to find
10342 which is left and which is right.
10343
10344 Top and bottom edge rows are 1 and 3 inclusive. Again, find least/greatest to find top and bottom.
10345
10346 Now go through rows from 1 to 3 and only add cells that are (a) within above column range
10347 and (b) shown.
10348
10349
10350*/
10351
10352 int leftCol = start - m_colCount * int(start/m_colCount);
10353 int rightCol = end - m_colCount * int(end/m_colCount);
10354
10355 int topRow = int(start/m_colCount);
10356 int bottomRow = int(end/m_colCount);
10357
10358 if (leftCol > rightCol)
10359 {
10360 int tmp = rightCol;
10361 rightCol = leftCol;
10362 leftCol = tmp;
10363 }
10364
10365 if (topRow > bottomRow)
10366 {
10367 int tmp = bottomRow;
10368 bottomRow = topRow;
10369 topRow = tmp;
10370 }
10371
10372 int i, j;
10373 for (i = topRow; i <= bottomRow; i++)
10374 {
10375 for (j = leftCol; j <= rightCol; j++)
10376 {
10377 wxRichTextCell* cell = GetCell(i, j);
10378 if (cell && cell->IsShown())
10379 selection.Add(cell->GetRange());
10380 }
10381 }
10382
10383 return selection;
10384}
10385
10386// Sets the attributes for the cells specified by the selection.
10387bool wxRichTextTable::SetCellStyle(const wxRichTextSelection& selection, const wxRichTextAttr& style, int flags)
10388{
10389 if (selection.GetContainer() != this)
10390 return false;
10391
10392 wxRichTextBuffer* buffer = GetBuffer();
10393 bool haveControl = (buffer && buffer->GetRichTextCtrl() != NULL);
10394 bool withUndo = haveControl && ((flags & wxRICHTEXT_SETSTYLE_WITH_UNDO) != 0);
10395
10396 if (withUndo)
10397 buffer->BeginBatchUndo(_("Set Cell Style"));
10398
10399 wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst();
10400 while (node)
10401 {
10402 wxRichTextCell* cell = wxDynamicCast(node->GetData(), wxRichTextCell);
10403 if (cell && selection.WithinSelection(cell->GetRange().GetStart()))
10404 SetStyle(cell, style, flags);
10405 node = node->GetNext();
10406 }
10407
10408 // Do action, or delay it until end of batch.
10409 if (withUndo)
10410 buffer->EndBatchUndo();
10411
10412 return true;
10413}
10414
4c86168d
JS
10415wxPosition wxRichTextTable::GetFocusedCell() const
10416{
10417 wxPosition position(-1, -1);
10418 const wxRichTextObject* focus = GetBuffer()->GetRichTextCtrl()->GetFocusObject();
10419
10420 for (int row = 0; row < GetRowCount(); ++row)
10421 {
10422 for (int col = 0; col < GetColumnCount(); ++col)
10423 {
10424 if (GetCell(row, col) == focus)
10425 {
10426 position.SetRow(row);
10427 position.SetCol(col);
10428 return position;
10429 }
10430 }
10431 }
10432
10433 return position;
10434}
10435
603f702b
JS
10436bool wxRichTextTable::DeleteRows(int startRow, int noRows)
10437{
ef3f0679
JS
10438 wxASSERT((startRow + noRows) <= m_rowCount);
10439 if ((startRow + noRows) > m_rowCount)
603f702b
JS
10440 return false;
10441
4c86168d
JS
10442 wxCHECK_MSG(noRows != m_rowCount, false, "Trying to delete all the cells in a table");
10443
31be8400 10444 wxRichTextBuffer* buffer = GetBuffer();
4c86168d
JS
10445 wxRichTextCtrl* rtc = buffer->GetRichTextCtrl();
10446
10447 wxPosition position = GetFocusedCell();
10448 int focusCol = position.GetCol();
10449 int focusRow = position.GetRow();
10450 if (focusRow >= startRow && focusRow < (startRow+noRows))
10451 {
10452 // Deleting a focused cell causes a segfault later when laying out, due to GetFocusedObject() returning an invalid object
10453 if ((startRow + noRows) < m_rowCount)
10454 {
10455 // There are more rows after the one(s) to be deleted, so set focus in the first of them
10456 rtc->SetFocusObject(GetCell(startRow + noRows, focusCol));
10457 }
10458 else
10459 {
10460 // Otherwise set focus in the preceding row
10461 rtc->SetFocusObject(GetCell(startRow - 1, focusCol));
10462 }
10463 }
10464
31be8400
JS
10465 wxRichTextAction* action = NULL;
10466 wxRichTextTable* clone = NULL;
4c86168d 10467 if (!rtc->SuppressingUndo())
31be8400
JS
10468 {
10469 // Create a clone containing the current state of the table. It will be used to Undo the action
10470 clone = wxStaticCast(this->Clone(), wxRichTextTable);
10471 clone->SetParent(GetParent());
4c86168d 10472 action = new wxRichTextAction(NULL, _("Delete row"), wxRICHTEXT_CHANGE_OBJECT, buffer, this, rtc);
31be8400
JS
10473 action->SetObject(this);
10474 action->SetPosition(GetRange().GetStart());
10475 }
10476
603f702b
JS
10477 int i, j;
10478 for (i = startRow; i < (startRow+noRows); i++)
10479 {
10480 wxRichTextObjectPtrArray& colArray = m_cells[startRow];
10481 for (j = 0; j < (int) colArray.GetCount(); j++)
10482 {
10483 wxRichTextObject* cell = colArray[j];
10484 RemoveChild(cell, true);
10485 }
10486
10487 // Keep deleting at the same position, since we move all
10488 // the others up
10489 m_cells.RemoveAt(startRow);
10490 }
10491
10492 m_rowCount = m_rowCount - noRows;
10493
4c86168d 10494 if (!rtc->SuppressingUndo())
31be8400
JS
10495 {
10496 buffer->SubmitAction(action);
10497 // Finally store the original-state clone; doing so earlier would cause various failures
10498 action->StoreObject(clone);
10499 }
10500
603f702b
JS
10501 return true;
10502}
10503
10504bool wxRichTextTable::DeleteColumns(int startCol, int noCols)
10505{
ef3f0679
JS
10506 wxASSERT((startCol + noCols) <= m_colCount);
10507 if ((startCol + noCols) > m_colCount)
603f702b
JS
10508 return false;
10509
4c86168d
JS
10510 wxCHECK_MSG(noCols != m_colCount, false, "Trying to delete all the cells in a table");
10511
31be8400 10512 wxRichTextBuffer* buffer = GetBuffer();
4c86168d
JS
10513 wxRichTextCtrl* rtc = buffer->GetRichTextCtrl();
10514
10515 wxPosition position = GetFocusedCell();
10516 int focusCol = position.GetCol();
10517 int focusRow = position.GetRow();
10518 if (focusCol >= startCol && focusCol < (startCol+noCols))
10519 {
10520 // Deleting a focused cell causes a segfault later when laying out, due to GetFocusedObject() returning an invalid object
10521 if ((startCol + noCols) < m_colCount)
10522 {
10523 // There are more columns after the one(s) to be deleted, so set focus in the first of them
10524 rtc->SetFocusObject(GetCell(focusRow, startCol + noCols));
10525 }
10526 else
10527 {
10528 // Otherwise set focus in the preceding column
10529 rtc->SetFocusObject(GetCell(focusRow, startCol - 1));
10530 }
10531 }
10532
31be8400
JS
10533 wxRichTextAction* action = NULL;
10534 wxRichTextTable* clone = NULL;
4c86168d 10535 if (!rtc->SuppressingUndo())
31be8400
JS
10536 {
10537 // Create a clone containing the current state of the table. It will be used to Undo the action
10538 clone = wxStaticCast(this->Clone(), wxRichTextTable);
10539 clone->SetParent(GetParent());
4c86168d 10540 action = new wxRichTextAction(NULL, _("Delete column"), wxRICHTEXT_CHANGE_OBJECT, buffer, this, rtc);
31be8400
JS
10541 action->SetObject(this);
10542 action->SetPosition(GetRange().GetStart());
10543 }
10544
603f702b
JS
10545 bool deleteRows = (noCols == m_colCount);
10546
10547 int i, j;
10548 for (i = 0; i < m_rowCount; i++)
10549 {
10550 wxRichTextObjectPtrArray& colArray = m_cells[deleteRows ? 0 : i];
ef3f0679 10551 for (j = 0; j < noCols; j++)
603f702b 10552 {
ef3f0679 10553 wxRichTextObject* cell = colArray[startCol];
603f702b 10554 RemoveChild(cell, true);
ef3f0679 10555 colArray.RemoveAt(startCol);
603f702b
JS
10556 }
10557
10558 if (deleteRows)
10559 m_cells.RemoveAt(0);
10560 }
10561
10562 if (deleteRows)
10563 m_rowCount = 0;
10564 m_colCount = m_colCount - noCols;
10565
4c86168d 10566 if (!rtc->SuppressingUndo())
31be8400
JS
10567 {
10568 buffer->SubmitAction(action);
10569 // Finally store the original-state clone; doing so earlier would cause various failures
10570 action->StoreObject(clone);
10571 }
10572
603f702b
JS
10573 return true;
10574}
10575
10576bool wxRichTextTable::AddRows(int startRow, int noRows, const wxRichTextAttr& attr)
10577{
10578 wxASSERT(startRow <= m_rowCount);
10579 if (startRow > m_rowCount)
10580 return false;
10581
31be8400
JS
10582 wxRichTextBuffer* buffer = GetBuffer();
10583 wxRichTextAction* action = NULL;
10584 wxRichTextTable* clone = NULL;
10585 if (!buffer->GetRichTextCtrl()->SuppressingUndo())
10586 {
10587 // Create a clone containing the current state of the table. It will be used to Undo the action
10588 clone = wxStaticCast(this->Clone(), wxRichTextTable);
10589 clone->SetParent(GetParent());
10590 action = new wxRichTextAction(NULL, _("Add row"), wxRICHTEXT_CHANGE_OBJECT, buffer, this, buffer->GetRichTextCtrl());
10591 action->SetObject(this);
10592 action->SetPosition(GetRange().GetStart());
10593 }
10594
603f702b
JS
10595 int i, j;
10596 for (i = 0; i < noRows; i++)
10597 {
10598 int idx;
10599 if (startRow == m_rowCount)
10600 {
10601 m_cells.Add(wxRichTextObjectPtrArray());
10602 idx = m_cells.GetCount() - 1;
10603 }
10604 else
10605 {
10606 m_cells.Insert(wxRichTextObjectPtrArray(), startRow+i);
10607 idx = startRow+i;
10608 }
10609
10610 wxRichTextObjectPtrArray& colArray = m_cells[idx];
10611 for (j = 0; j < m_colCount; j++)
10612 {
10613 wxRichTextCell* cell = new wxRichTextCell;
10614 cell->GetAttributes() = attr;
10615
10616 AppendChild(cell);
a9fd42cc 10617 cell->AddParagraph(wxEmptyString);
603f702b
JS
10618 colArray.Add(cell);
10619 }
10620 }
10621
10622 m_rowCount = m_rowCount + noRows;
31be8400
JS
10623
10624 if (!buffer->GetRichTextCtrl()->SuppressingUndo())
10625 {
10626 buffer->SubmitAction(action);
10627 // Finally store the original-state clone; doing so earlier would cause various failures
10628 action->StoreObject(clone);
10629 }
10630
603f702b
JS
10631 return true;
10632}
10633
10634bool wxRichTextTable::AddColumns(int startCol, int noCols, const wxRichTextAttr& attr)
10635{
10636 wxASSERT(startCol <= m_colCount);
10637 if (startCol > m_colCount)
10638 return false;
10639
31be8400
JS
10640 wxRichTextBuffer* buffer = GetBuffer();
10641 wxRichTextAction* action = NULL;
10642 wxRichTextTable* clone = NULL;
10643 if (!buffer->GetRichTextCtrl()->SuppressingUndo())
10644 {
10645 // Create a clone containing the current state of the table. It will be used to Undo the action
10646 clone = wxStaticCast(this->Clone(), wxRichTextTable);
10647 clone->SetParent(GetParent());
10648 action = new wxRichTextAction(NULL, _("Add column"), wxRICHTEXT_CHANGE_OBJECT, buffer, this, buffer->GetRichTextCtrl());
10649 action->SetObject(this);
10650 action->SetPosition(GetRange().GetStart());
10651 }
10652
603f702b
JS
10653 int i, j;
10654 for (i = 0; i < m_rowCount; i++)
10655 {
10656 wxRichTextObjectPtrArray& colArray = m_cells[i];
10657 for (j = 0; j < noCols; j++)
10658 {
10659 wxRichTextCell* cell = new wxRichTextCell;
10660 cell->GetAttributes() = attr;
10661
10662 AppendChild(cell);
a9fd42cc 10663 cell->AddParagraph(wxEmptyString);
603f702b
JS
10664
10665 if (startCol == m_colCount)
10666 colArray.Add(cell);
10667 else
10668 colArray.Insert(cell, startCol+j);
10669 }
10670 }
10671
10672 m_colCount = m_colCount + noCols;
10673
31be8400
JS
10674 if (!buffer->GetRichTextCtrl()->SuppressingUndo())
10675 {
10676 buffer->SubmitAction(action);
10677 // Finally store the original-state clone; doing so earlier would cause various failures
10678 action->StoreObject(clone);
10679 }
10680
603f702b 10681 return true;
5ad9ae3a
JS
10682}
10683
603f702b
JS
10684// Edit properties via a GUI
10685bool wxRichTextTable::EditProperties(wxWindow* parent, wxRichTextBuffer* buffer)
5ad9ae3a 10686{
603f702b
JS
10687 wxRichTextObjectPropertiesDialog boxDlg(this, wxGetTopLevelParent(parent), wxID_ANY, _("Table Properties"));
10688 boxDlg.SetAttributes(GetAttributes());
10689
10690 if (boxDlg.ShowModal() == wxID_OK)
5ad9ae3a 10691 {
603f702b
JS
10692 boxDlg.ApplyStyle(buffer->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RESET);
10693 return true;
5ad9ae3a
JS
10694 }
10695 else
10696 return false;
bec80f4f
JS
10697}
10698
5d7836c4
JS
10699/*
10700 * Module to initialise and clean up handlers
10701 */
10702
10703class wxRichTextModule: public wxModule
10704{
10705DECLARE_DYNAMIC_CLASS(wxRichTextModule)
10706public:
10707 wxRichTextModule() {}
cfa3b256
JS
10708 bool OnInit()
10709 {
d2d0adc7 10710 wxRichTextBuffer::SetRenderer(new wxRichTextStdRenderer);
cfa3b256
JS
10711 wxRichTextBuffer::InitStandardHandlers();
10712 wxRichTextParagraph::InitDefaultTabs();
1aca9fcd
JS
10713
10714 wxRichTextXMLHandler::RegisterNodeName(wxT("text"), wxT("wxRichTextPlainText"));
10715 wxRichTextXMLHandler::RegisterNodeName(wxT("symbol"), wxT("wxRichTextPlainText"));
10716 wxRichTextXMLHandler::RegisterNodeName(wxT("image"), wxT("wxRichTextImage"));
10717 wxRichTextXMLHandler::RegisterNodeName(wxT("paragraph"), wxT("wxRichTextParagraph"));
10718 wxRichTextXMLHandler::RegisterNodeName(wxT("paragraphlayout"), wxT("wxRichTextParagraphLayoutBox"));
10719 wxRichTextXMLHandler::RegisterNodeName(wxT("textbox"), wxT("wxRichTextBox"));
10720 wxRichTextXMLHandler::RegisterNodeName(wxT("cell"), wxT("wxRichTextCell"));
10721 wxRichTextXMLHandler::RegisterNodeName(wxT("table"), wxT("wxRichTextTable"));
10722 wxRichTextXMLHandler::RegisterNodeName(wxT("field"), wxT("wxRichTextField"));
10723
cfa3b256 10724 return true;
47b378bd 10725 }
cfa3b256
JS
10726 void OnExit()
10727 {
10728 wxRichTextBuffer::CleanUpHandlers();
8db2e3ef 10729 wxRichTextBuffer::CleanUpDrawingHandlers();
7c9fdebe 10730 wxRichTextBuffer::CleanUpFieldTypes();
1aca9fcd 10731 wxRichTextXMLHandler::ClearNodeToClassMap();
cfa3b256
JS
10732 wxRichTextDecimalToRoman(-1);
10733 wxRichTextParagraph::ClearDefaultTabs();
dadd4f55 10734 wxRichTextCtrl::ClearAvailableFontNames();
d2d0adc7 10735 wxRichTextBuffer::SetRenderer(NULL);
47b378bd 10736 }
5d7836c4
JS
10737};
10738
10739IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule, wxModule)
10740
10741
f1d6804f
RD
10742// If the richtext lib is dynamically loaded after the app has already started
10743// (such as from wxPython) then the built-in module system will not init this
10744// module. Provide this function to do it manually.
10745void wxRichTextModuleInit()
10746{
10747 wxModule* module = new wxRichTextModule;
f1d6804f 10748 wxModule::RegisterModule(module);
58d1949f 10749 wxModule::InitializeModules();
f1d6804f
RD
10750}
10751
10752
5d7836c4
JS
10753/*!
10754 * Commands for undo/redo
10755 *
10756 */
10757
10758wxRichTextCommand::wxRichTextCommand(const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer,
603f702b 10759 wxRichTextParagraphLayoutBox* container, wxRichTextCtrl* ctrl, bool ignoreFirstTime): wxCommand(true, name)
5d7836c4 10760{
603f702b 10761 /* wxRichTextAction* action = */ new wxRichTextAction(this, name, id, buffer, container, ctrl, ignoreFirstTime);
5d7836c4
JS
10762}
10763
7fe8059f 10764wxRichTextCommand::wxRichTextCommand(const wxString& name): wxCommand(true, name)
5d7836c4
JS
10765{
10766}
10767
10768wxRichTextCommand::~wxRichTextCommand()
10769{
10770 ClearActions();
10771}
10772
10773void wxRichTextCommand::AddAction(wxRichTextAction* action)
10774{
10775 if (!m_actions.Member(action))
10776 m_actions.Append(action);
10777}
10778
10779bool wxRichTextCommand::Do()
10780{
09f14108 10781 for (wxList::compatibility_iterator node = m_actions.GetFirst(); node; node = node->GetNext())
5d7836c4
JS
10782 {
10783 wxRichTextAction* action = (wxRichTextAction*) node->GetData();
10784 action->Do();
10785 }
10786
10787 return true;
10788}
10789
10790bool wxRichTextCommand::Undo()
10791{
09f14108 10792 for (wxList::compatibility_iterator node = m_actions.GetLast(); node; node = node->GetPrevious())
5d7836c4
JS
10793 {
10794 wxRichTextAction* action = (wxRichTextAction*) node->GetData();
10795 action->Undo();
10796 }
10797
10798 return true;
10799}
10800
10801void wxRichTextCommand::ClearActions()
10802{
10803 WX_CLEAR_LIST(wxList, m_actions);
10804}
10805
10806/*!
10807 * Individual action
10808 *
10809 */
10810
603f702b
JS
10811wxRichTextAction::wxRichTextAction(wxRichTextCommand* cmd, const wxString& name, wxRichTextCommandId id,
10812 wxRichTextBuffer* buffer, wxRichTextParagraphLayoutBox* container,
10813 wxRichTextCtrl* ctrl, bool ignoreFirstTime)
5d7836c4
JS
10814{
10815 m_buffer = buffer;
603f702b
JS
10816 m_object = NULL;
10817 m_containerAddress.Create(buffer, container);
5d7836c4
JS
10818 m_ignoreThis = ignoreFirstTime;
10819 m_cmdId = id;
10820 m_position = -1;
10821 m_ctrl = ctrl;
10822 m_name = name;
10823 m_newParagraphs.SetDefaultStyle(buffer->GetDefaultStyle());
10824 m_newParagraphs.SetBasicStyle(buffer->GetBasicStyle());
10825 if (cmd)
10826 cmd->AddAction(this);
10827}
10828
10829wxRichTextAction::~wxRichTextAction()
10830{
603f702b
JS
10831 if (m_object)
10832 delete m_object;
10833}
10834
10835// Returns the container that this action refers to, using the container address and top-level buffer.
10836wxRichTextParagraphLayoutBox* wxRichTextAction::GetContainer() const
10837{
10838 wxRichTextParagraphLayoutBox* container = wxDynamicCast(GetContainerAddress().GetObject(m_buffer), wxRichTextParagraphLayoutBox);
10839 return container;
5d7836c4
JS
10840}
10841
603f702b 10842
7051fa41
JS
10843void wxRichTextAction::CalculateRefreshOptimizations(wxArrayInt& optimizationLineCharPositions, wxArrayInt& optimizationLineYPositions)
10844{
10845 // Store a list of line start character and y positions so we can figure out which area
10846 // we need to refresh
10847
10848#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
603f702b
JS
10849 wxRichTextParagraphLayoutBox* container = GetContainer();
10850 wxASSERT(container != NULL);
10851 if (!container)
10852 return;
10853
7051fa41
JS
10854 // NOTE: we're assuming that the buffer is laid out correctly at this point.
10855 // If we had several actions, which only invalidate and leave layout until the
10856 // paint handler is called, then this might not be true. So we may need to switch
10857 // optimisation on only when we're simply adding text and not simultaneously
10858 // deleting a selection, for example. Or, we make sure the buffer is laid out correctly
10859 // first, but of course this means we'll be doing it twice.
603f702b 10860 if (!m_buffer->IsDirty() && m_ctrl) // can only do optimisation if the buffer is already laid out correctly
7051fa41 10861 {
4ba36292
JS
10862 wxSize clientSize = m_ctrl->GetUnscaledSize(m_ctrl->GetClientSize());
10863 wxPoint firstVisiblePt = m_ctrl->GetUnscaledPoint(m_ctrl->GetFirstVisiblePoint());
7051fa41
JS
10864 int lastY = firstVisiblePt.y + clientSize.y;
10865
603f702b
JS
10866 wxRichTextParagraph* para = container->GetParagraphAtPosition(GetRange().GetStart());
10867 wxRichTextObjectList::compatibility_iterator node = container->GetChildren().Find(para);
7051fa41
JS
10868 while (node)
10869 {
10870 wxRichTextParagraph* child = (wxRichTextParagraph*) node->GetData();
10871 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
10872 while (node2)
10873 {
10874 wxRichTextLine* line = node2->GetData();
10875 wxPoint pt = line->GetAbsolutePosition();
10876 wxRichTextRange range = line->GetAbsoluteRange();
10877
10878 if (pt.y > lastY)
10879 {
10880 node2 = wxRichTextLineList::compatibility_iterator();
10881 node = wxRichTextObjectList::compatibility_iterator();
10882 }
10883 else if (range.GetStart() > GetPosition() && pt.y >= firstVisiblePt.y)
10884 {
10885 optimizationLineCharPositions.Add(range.GetStart());
10886 optimizationLineYPositions.Add(pt.y);
10887 }
10888
10889 if (node2)
10890 node2 = node2->GetNext();
10891 }
10892
10893 if (node)
10894 node = node->GetNext();
10895 }
10896 }
10897#endif
10898}
10899
5d7836c4
JS
10900bool wxRichTextAction::Do()
10901{
10902 m_buffer->Modify(true);
10903
603f702b
JS
10904 wxRichTextParagraphLayoutBox* container = GetContainer();
10905 wxASSERT(container != NULL);
10906 if (!container)
10907 return false;
10908
5d7836c4
JS
10909 switch (m_cmdId)
10910 {
10911 case wxRICHTEXT_INSERT:
10912 {
ea160b2e
JS
10913 // Store a list of line start character and y positions so we can figure out which area
10914 // we need to refresh
10915 wxArrayInt optimizationLineCharPositions;
10916 wxArrayInt optimizationLineYPositions;
10917
10918#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
7051fa41 10919 CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions);
ea160b2e
JS
10920#endif
10921
603f702b
JS
10922 container->InsertFragment(GetRange().GetStart(), m_newParagraphs);
10923 container->UpdateRanges();
10924
10925 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
10926 // Layout() would stop prematurely at the top level.
10927 container->InvalidateHierarchy(wxRichTextRange(wxMax(0, GetRange().GetStart()-1), GetRange().GetEnd()));
5d7836c4 10928
603f702b 10929 long newCaretPosition = GetPosition() + m_newParagraphs.GetOwnRange().GetLength();
0ca07313
JS
10930
10931 // Character position to caret position
10932 newCaretPosition --;
10933
10934 // Don't take into account the last newline
5d7836c4
JS
10935 if (m_newParagraphs.GetPartialParagraph())
10936 newCaretPosition --;
46ee0e5b 10937 else
7c081bd2 10938 if (m_newParagraphs.GetChildren().GetCount() > 1)
46ee0e5b
JS
10939 {
10940 wxRichTextObject* p = (wxRichTextObject*) m_newParagraphs.GetChildren().GetLast()->GetData();
10941 if (p->GetRange().GetLength() == 1)
10942 newCaretPosition --;
10943 }
5d7836c4 10944
603f702b 10945 newCaretPosition = wxMin(newCaretPosition, (container->GetOwnRange().GetEnd()-1));
0ca07313 10946
7051fa41 10947 UpdateAppearance(newCaretPosition, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions, true /* do */);
3e541562 10948
5912d19e 10949 wxRichTextEvent cmdEvent(
ce7fe42e 10950 wxEVT_RICHTEXT_CONTENT_INSERTED,
5912d19e
JS
10951 m_ctrl ? m_ctrl->GetId() : -1);
10952 cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
10953 cmdEvent.SetRange(GetRange());
10954 cmdEvent.SetPosition(GetRange().GetStart());
603f702b 10955 cmdEvent.SetContainer(container);
3e541562 10956
5912d19e 10957 m_buffer->SendEvent(cmdEvent);
5d7836c4
JS
10958
10959 break;
10960 }
10961 case wxRICHTEXT_DELETE:
10962 {
7051fa41
JS
10963 wxArrayInt optimizationLineCharPositions;
10964 wxArrayInt optimizationLineYPositions;
10965
10966#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
10967 CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions);
10968#endif
10969
603f702b
JS
10970 container->DeleteRange(GetRange());
10971 container->UpdateRanges();
10972 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
10973 // Layout() would stop prematurely at the top level.
10974 container->InvalidateHierarchy(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5d7836c4 10975
6ccbca24 10976 long caretPos = GetRange().GetStart()-1;
603f702b 10977 if (caretPos >= container->GetOwnRange().GetEnd())
6ccbca24
JS
10978 caretPos --;
10979
7051fa41 10980 UpdateAppearance(caretPos, true /* send update event */, & optimizationLineCharPositions, & optimizationLineYPositions, true /* do */);
5d7836c4 10981
5912d19e 10982 wxRichTextEvent cmdEvent(
ce7fe42e 10983 wxEVT_RICHTEXT_CONTENT_DELETED,
5912d19e
JS
10984 m_ctrl ? m_ctrl->GetId() : -1);
10985 cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
10986 cmdEvent.SetRange(GetRange());
10987 cmdEvent.SetPosition(GetRange().GetStart());
603f702b 10988 cmdEvent.SetContainer(container);
3e541562 10989
5912d19e
JS
10990 m_buffer->SendEvent(cmdEvent);
10991
5d7836c4
JS
10992 break;
10993 }
10994 case wxRICHTEXT_CHANGE_STYLE:
590a0f8b 10995 case wxRICHTEXT_CHANGE_PROPERTIES:
5d7836c4
JS
10996 {
10997 ApplyParagraphs(GetNewParagraphs());
603f702b 10998
c4168888 10999 // Invalidate the whole buffer if there were floating objects
e12b91a3 11000 if (wxRichTextBuffer::GetFloatingLayoutMode() && container->GetFloatingObjectCount() > 0)
c4168888
JS
11001 m_buffer->InvalidateHierarchy(wxRICHTEXT_ALL);
11002 else
11003 {
11004 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
11005 // Layout() would stop prematurely at the top level.
11006 container->InvalidateHierarchy(GetRange());
11007 }
603f702b
JS
11008
11009 UpdateAppearance(GetPosition());
11010
11011 wxRichTextEvent cmdEvent(
ce7fe42e 11012 m_cmdId == wxRICHTEXT_CHANGE_STYLE ? wxEVT_RICHTEXT_STYLE_CHANGED : wxEVT_RICHTEXT_PROPERTIES_CHANGED,
603f702b
JS
11013 m_ctrl ? m_ctrl->GetId() : -1);
11014 cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
11015 cmdEvent.SetRange(GetRange());
11016 cmdEvent.SetPosition(GetRange().GetStart());
11017 cmdEvent.SetContainer(container);
11018
11019 m_buffer->SendEvent(cmdEvent);
11020
11021 break;
11022 }
11023 case wxRICHTEXT_CHANGE_ATTRIBUTES:
11024 {
11025 wxRichTextObject* obj = m_objectAddress.GetObject(m_buffer); // container->GetChildAtPosition(GetRange().GetStart());
11026 if (obj)
11027 {
11028 wxRichTextAttr oldAttr = obj->GetAttributes();
11029 obj->GetAttributes() = m_attributes;
11030 m_attributes = oldAttr;
11031 }
11032
11033 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
11034 // Layout() would stop prematurely at the top level.
c4168888 11035 // Invalidate the whole buffer if there were floating objects
e12b91a3 11036 if (wxRichTextBuffer::GetFloatingLayoutMode() && container->GetFloatingObjectCount() > 0)
c4168888
JS
11037 m_buffer->InvalidateHierarchy(wxRICHTEXT_ALL);
11038 else
11039 container->InvalidateHierarchy(GetRange());
5d7836c4
JS
11040
11041 UpdateAppearance(GetPosition());
11042
5912d19e 11043 wxRichTextEvent cmdEvent(
ce7fe42e 11044 wxEVT_RICHTEXT_STYLE_CHANGED,
5912d19e
JS
11045 m_ctrl ? m_ctrl->GetId() : -1);
11046 cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
11047 cmdEvent.SetRange(GetRange());
11048 cmdEvent.SetPosition(GetRange().GetStart());
603f702b 11049 cmdEvent.SetContainer(container);
3e541562 11050
5912d19e
JS
11051 m_buffer->SendEvent(cmdEvent);
11052
603f702b
JS
11053 break;
11054 }
11055 case wxRICHTEXT_CHANGE_OBJECT:
11056 {
11057 wxRichTextObject* obj = m_objectAddress.GetObject(m_buffer);
31be8400 11058 if (obj && m_object && m_ctrl)
603f702b 11059 {
31be8400
JS
11060 // The plan is to swap the current object with the stored, previous-state, clone
11061 // We can't get 'node' from the containing buffer (as it doesn't directly store objects)
11062 // so use the parent paragraph
11063 wxRichTextParagraph* para = wxDynamicCast(obj->GetParent(), wxRichTextParagraph);
11064 wxCHECK_MSG(para, false, "Invalid parent paragraph");
bf5e92db
VZ
11065
11066 // The stored object, m_object, may have a stale parent paragraph. This would cause
11067 // a crash during layout, so use obj's parent para, which should be the correct one.
11068 // (An alternative would be to return the parent too from m_objectAddress.GetObject(),
11069 // or to set obj's parent there before returning)
11070 m_object->SetParent(para);
11071
31be8400 11072 wxRichTextObjectList::compatibility_iterator node = para->GetChildren().Find(obj);
603f702b
JS
11073 if (node)
11074 {
11075 wxRichTextObject* obj = node->GetData();
11076 node->SetData(m_object);
11077 m_object = obj;
11078 }
11079 }
11080
11081 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
11082 // Layout() would stop prematurely at the top level.
c4168888 11083 // Invalidate the whole buffer if there were floating objects
e12b91a3 11084 if (wxRichTextBuffer::GetFloatingLayoutMode() && container->GetFloatingObjectCount() > 0)
c4168888
JS
11085 m_buffer->InvalidateHierarchy(wxRICHTEXT_ALL);
11086 else
11087 container->InvalidateHierarchy(GetRange());
603f702b
JS
11088
11089 UpdateAppearance(GetPosition());
11090
11091 // TODO: send new kind of modification event
11092
5d7836c4
JS
11093 break;
11094 }
11095 default:
11096 break;
11097 }
11098
11099 return true;
11100}
11101
11102bool wxRichTextAction::Undo()
11103{
11104 m_buffer->Modify(true);
11105
603f702b
JS
11106 wxRichTextParagraphLayoutBox* container = GetContainer();
11107 wxASSERT(container != NULL);
11108 if (!container)
11109 return false;
11110
5d7836c4
JS
11111 switch (m_cmdId)
11112 {
11113 case wxRICHTEXT_INSERT:
11114 {
7051fa41
JS
11115 wxArrayInt optimizationLineCharPositions;
11116 wxArrayInt optimizationLineYPositions;
11117
11118#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
11119 CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions);
11120#endif
11121
603f702b
JS
11122 container->DeleteRange(GetRange());
11123 container->UpdateRanges();
7c9fdebe 11124
603f702b
JS
11125 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
11126 // Layout() would stop prematurely at the top level.
11127 container->InvalidateHierarchy(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart()));
5d7836c4
JS
11128
11129 long newCaretPosition = GetPosition() - 1;
3e541562 11130
7051fa41 11131 UpdateAppearance(newCaretPosition, true, /* send update event */ & optimizationLineCharPositions, & optimizationLineYPositions, false /* undo */);
5d7836c4 11132
5912d19e 11133 wxRichTextEvent cmdEvent(
ce7fe42e 11134 wxEVT_RICHTEXT_CONTENT_DELETED,
5912d19e
JS
11135 m_ctrl ? m_ctrl->GetId() : -1);
11136 cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
11137 cmdEvent.SetRange(GetRange());
11138 cmdEvent.SetPosition(GetRange().GetStart());
603f702b 11139 cmdEvent.SetContainer(container);
3e541562 11140
5912d19e
JS
11141 m_buffer->SendEvent(cmdEvent);
11142
5d7836c4
JS
11143 break;
11144 }
11145 case wxRICHTEXT_DELETE:
11146 {
7051fa41
JS
11147 wxArrayInt optimizationLineCharPositions;
11148 wxArrayInt optimizationLineYPositions;
11149
11150#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
11151 CalculateRefreshOptimizations(optimizationLineCharPositions, optimizationLineYPositions);
11152#endif
11153
603f702b
JS
11154 container->InsertFragment(GetRange().GetStart(), m_oldParagraphs);
11155 container->UpdateRanges();
7c9fdebe 11156
603f702b
JS
11157 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
11158 // Layout() would stop prematurely at the top level.
11159 container->InvalidateHierarchy(GetRange());
5d7836c4 11160
7051fa41 11161 UpdateAppearance(GetPosition(), true, /* send update event */ & optimizationLineCharPositions, & optimizationLineYPositions, false /* undo */);
5d7836c4 11162
5912d19e 11163 wxRichTextEvent cmdEvent(
ce7fe42e 11164 wxEVT_RICHTEXT_CONTENT_INSERTED,
5912d19e
JS
11165 m_ctrl ? m_ctrl->GetId() : -1);
11166 cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
11167 cmdEvent.SetRange(GetRange());
11168 cmdEvent.SetPosition(GetRange().GetStart());
603f702b 11169 cmdEvent.SetContainer(container);
3e541562 11170
5912d19e
JS
11171 m_buffer->SendEvent(cmdEvent);
11172
5d7836c4
JS
11173 break;
11174 }
11175 case wxRICHTEXT_CHANGE_STYLE:
590a0f8b 11176 case wxRICHTEXT_CHANGE_PROPERTIES:
5d7836c4
JS
11177 {
11178 ApplyParagraphs(GetOldParagraphs());
603f702b
JS
11179 // InvalidateHierarchy goes up the hierarchy as well as down, otherwise with a nested object,
11180 // Layout() would stop prematurely at the top level.
11181 container->InvalidateHierarchy(GetRange());
5d7836c4
JS
11182
11183 UpdateAppearance(GetPosition());
11184
5912d19e 11185 wxRichTextEvent cmdEvent(
ce7fe42e 11186 m_cmdId == wxRICHTEXT_CHANGE_STYLE ? wxEVT_RICHTEXT_STYLE_CHANGED : wxEVT_RICHTEXT_PROPERTIES_CHANGED,
5912d19e
JS
11187 m_ctrl ? m_ctrl->GetId() : -1);
11188 cmdEvent.SetEventObject(m_ctrl ? (wxObject*) m_ctrl : (wxObject*) m_buffer);
11189 cmdEvent.SetRange(GetRange());
11190 cmdEvent.SetPosition(GetRange().GetStart());
603f702b 11191 cmdEvent.SetContainer(container);
3e541562 11192
5912d19e
JS
11193 m_buffer->SendEvent(cmdEvent);
11194
5d7836c4
JS
11195 break;
11196 }
603f702b
JS
11197 case wxRICHTEXT_CHANGE_ATTRIBUTES:
11198 case wxRICHTEXT_CHANGE_OBJECT:
11199 {
11200 return Do();
11201 }
5d7836c4
JS
11202 default:
11203 break;
11204 }
11205
11206 return true;
11207}
11208
11209/// Update the control appearance
603f702b 11210void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent, wxArrayInt* optimizationLineCharPositions, wxArrayInt* optimizationLineYPositions, bool isDoCmd)
5d7836c4 11211{
603f702b
JS
11212 wxRichTextParagraphLayoutBox* container = GetContainer();
11213 wxASSERT(container != NULL);
11214 if (!container)
11215 return;
11216
5d7836c4
JS
11217 if (m_ctrl)
11218 {
603f702b 11219 m_ctrl->SetFocusObject(container);
5d7836c4 11220 m_ctrl->SetCaretPosition(caretPosition);
603f702b 11221
5d7836c4
JS
11222 if (!m_ctrl->IsFrozen())
11223 {
603f702b
JS
11224 wxRect containerRect = container->GetRect();
11225
2f36e8dc 11226 m_ctrl->LayoutContent();
5d7836c4 11227
603f702b
JS
11228 // Refresh everything if there were floating objects or the container changed size
11229 // (we can't yet optimize in these cases, since more complex interaction with other content occurs)
e12b91a3 11230 if ((wxRichTextBuffer::GetFloatingLayoutMode() && container->GetFloatingObjectCount() > 0) || (container->GetParent() && containerRect != container->GetRect()))
603f702b
JS
11231 {
11232 m_ctrl->Refresh(false);
11233 }
11234 else
11235
11236#if wxRICHTEXT_USE_OPTIMIZED_DRAWING
11237 // Find refresh rectangle if we are in a position to optimise refresh
11238 if ((m_cmdId == wxRICHTEXT_INSERT || m_cmdId == wxRICHTEXT_DELETE) && optimizationLineCharPositions)
11239 {
11240 size_t i;
11241
4ba36292
JS
11242 wxSize clientSize = m_ctrl->GetUnscaledSize(m_ctrl->GetClientSize());
11243 wxPoint firstVisiblePt = m_ctrl->GetUnscaledPoint(m_ctrl->GetFirstVisiblePoint());
603f702b
JS
11244
11245 // Start/end positions
11246 int firstY = 0;
11247 int lastY = firstVisiblePt.y + clientSize.y;
11248
11249 bool foundEnd = false;
11250
11251 // position offset - how many characters were inserted
11252 int positionOffset = GetRange().GetLength();
11253
11254 // Determine whether this is Do or Undo, and adjust positionOffset accordingly
11255 if ((m_cmdId == wxRICHTEXT_DELETE && isDoCmd) || (m_cmdId == wxRICHTEXT_INSERT && !isDoCmd))
11256 positionOffset = - positionOffset;
11257
11258 // find the first line which is being drawn at the same position as it was
11259 // before. Since we're talking about a simple insertion, we can assume
11260 // that the rest of the window does not need to be redrawn.
93a16a7d 11261 long pos = GetRange().GetStart();
603f702b 11262
93a16a7d 11263 wxRichTextParagraph* para = container->GetParagraphAtPosition(pos, false /* is not caret pos */);
603f702b
JS
11264 // Since we support floating layout, we should redraw the whole para instead of just
11265 // the first line touching the invalid range.
11266 if (para)
11267 {
93a16a7d
JS
11268 // In case something was drawn above the paragraph,
11269 // such as a line break, allow a little extra.
11270 firstY = para->GetPosition().y - 4;
603f702b
JS
11271 }
11272
11273 wxRichTextObjectList::compatibility_iterator node = container->GetChildren().Find(para);
11274 while (node)
11275 {
11276 wxRichTextParagraph* child = (wxRichTextParagraph*) node->GetData();
11277 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
11278 while (node2)
11279 {
11280 wxRichTextLine* line = node2->GetData();
11281 wxPoint pt = line->GetAbsolutePosition();
11282 wxRichTextRange range = line->GetAbsoluteRange();
11283
11284 // we want to find the first line that is in the same position
11285 // as before. This will mean we're at the end of the changed text.
11286
11287 if (pt.y > lastY) // going past the end of the window, no more info
11288 {
11289 node2 = wxRichTextLineList::compatibility_iterator();
11290 node = wxRichTextObjectList::compatibility_iterator();
11291 }
11292 // Detect last line in the buffer
11293 else if (!node2->GetNext() && para->GetRange().Contains(container->GetOwnRange().GetEnd()))
11294 {
11295 // If deleting text, make sure we refresh below as well as above
11296 if (positionOffset >= 0)
11297 {
11298 foundEnd = true;
11299 lastY = pt.y + line->GetSize().y;
11300 }
11301
11302 node2 = wxRichTextLineList::compatibility_iterator();
11303 node = wxRichTextObjectList::compatibility_iterator();
11304
11305 break;
11306 }
11307 else
11308 {
11309 // search for this line being at the same position as before
11310 for (i = 0; i < optimizationLineCharPositions->GetCount(); i++)
11311 {
11312 if (((*optimizationLineCharPositions)[i] + positionOffset == range.GetStart()) &&
11313 ((*optimizationLineYPositions)[i] == pt.y))
11314 {
11315 // Stop, we're now the same as we were
11316 foundEnd = true;
11317
93a16a7d 11318 lastY = pt.y + line->GetSize().y;
603f702b
JS
11319
11320 node2 = wxRichTextLineList::compatibility_iterator();
11321 node = wxRichTextObjectList::compatibility_iterator();
11322
11323 break;
11324 }
11325 }
11326 }
11327
11328 if (node2)
11329 node2 = node2->GetNext();
11330 }
11331
11332 if (node)
11333 node = node->GetNext();
11334 }
11335
11336 firstY = wxMax(firstVisiblePt.y, firstY);
11337 if (!foundEnd)
11338 lastY = firstVisiblePt.y + clientSize.y;
11339
11340 // Convert to device coordinates
4ba36292 11341 wxRect rect(m_ctrl->GetPhysicalPoint(m_ctrl->GetScaledPoint(wxPoint(firstVisiblePt.x, firstY))), m_ctrl->GetScaledSize(wxSize(clientSize.x, lastY - firstY)));
603f702b
JS
11342 m_ctrl->RefreshRect(rect);
11343 }
11344 else
1c13f06e 11345#endif
603f702b
JS
11346 m_ctrl->Refresh(false);
11347
11348 m_ctrl->PositionCaret();
4fe83b93
JS
11349
11350 // This causes styles to persist when doing programmatic
11351 // content creation except when Freeze/Thaw is used, so
11352 // disable this and check for the consequences.
11353 // m_ctrl->SetDefaultStyleToCursorStyle();
603f702b 11354
5d7836c4 11355 if (sendUpdateEvent)
0ec1179b 11356 wxTextCtrl::SendTextUpdatedEvent(m_ctrl);
5d7836c4 11357 }
7fe8059f 11358 }
5d7836c4
JS
11359}
11360
11361/// Replace the buffer paragraphs with the new ones.
0ca07313 11362void wxRichTextAction::ApplyParagraphs(const wxRichTextParagraphLayoutBox& fragment)
5d7836c4 11363{
603f702b
JS
11364 wxRichTextParagraphLayoutBox* container = GetContainer();
11365 wxASSERT(container != NULL);
11366 if (!container)
11367 return;
11368
5d7836c4
JS
11369 wxRichTextObjectList::compatibility_iterator node = fragment.GetChildren().GetFirst();
11370 while (node)
11371 {
11372 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
11373 wxASSERT (para != NULL);
11374
11375 // We'll replace the existing paragraph by finding the paragraph at this position,
11376 // delete its node data, and setting a copy as the new node data.
11377 // TODO: make more efficient by simply swapping old and new paragraph objects.
11378
603f702b 11379 wxRichTextParagraph* existingPara = container->GetParagraphAtPosition(para->GetRange().GetStart());
5d7836c4
JS
11380 if (existingPara)
11381 {
603f702b 11382 wxRichTextObjectList::compatibility_iterator bufferParaNode = container->GetChildren().Find(existingPara);
5d7836c4
JS
11383 if (bufferParaNode)
11384 {
11385 wxRichTextParagraph* newPara = new wxRichTextParagraph(*para);
603f702b 11386 newPara->SetParent(container);
5d7836c4
JS
11387
11388 bufferParaNode->SetData(newPara);
11389
11390 delete existingPara;
11391 }
11392 }
11393
11394 node = node->GetNext();
11395 }
11396}
11397
11398
11399/*!
11400 * wxRichTextRange
11401 * This stores beginning and end positions for a range of data.
11402 */
11403
603f702b
JS
11404WX_DEFINE_OBJARRAY(wxRichTextRangeArray);
11405
5d7836c4
JS
11406/// Limit this range to be within 'range'
11407bool wxRichTextRange::LimitTo(const wxRichTextRange& range)
11408{
11409 if (m_start < range.m_start)
11410 m_start = range.m_start;
11411
11412 if (m_end > range.m_end)
11413 m_end = range.m_end;
11414
11415 return true;
11416}
11417
11418/*!
11419 * wxRichTextImage implementation
11420 * This object represents an image.
11421 */
11422
bec80f4f 11423IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage, wxRichTextObject)
5d7836c4 11424
24777478 11425wxRichTextImage::wxRichTextImage(const wxImage& image, wxRichTextObject* parent, wxRichTextAttr* charStyle):
bec80f4f 11426 wxRichTextObject(parent)
5d7836c4 11427{
23698b12 11428 Init();
cdaed652 11429 m_imageBlock.MakeImageBlockDefaultQuality(image, wxBITMAP_TYPE_PNG);
4f32b3cf
JS
11430 if (charStyle)
11431 SetAttributes(*charStyle);
5d7836c4
JS
11432}
11433
24777478 11434wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent, wxRichTextAttr* charStyle):
bec80f4f 11435 wxRichTextObject(parent)
5d7836c4 11436{
23698b12 11437 Init();
5d7836c4 11438 m_imageBlock = imageBlock;
4f32b3cf
JS
11439 if (charStyle)
11440 SetAttributes(*charStyle);
5d7836c4
JS
11441}
11442
914a4e23
JS
11443wxRichTextImage::~wxRichTextImage()
11444{
11445}
11446
23698b12
JS
11447void wxRichTextImage::Init()
11448{
11449 m_originalImageSize = wxSize(-1, -1);
11450}
11451
cdaed652 11452/// Create a cached image at the required size
914a4e23 11453bool wxRichTextImage::LoadImageCache(wxDC& dc, bool resetCache, const wxSize& parentSize)
5d7836c4 11454{
23698b12
JS
11455 if (!m_imageBlock.IsOk())
11456 return false;
11457
11458 // If we have an original image size, use that to compute the cached bitmap size
11459 // instead of loading the image each time. This way we can avoid loading
11460 // the image so long as the new cached bitmap size hasn't changed.
11461
11462 wxImage image;
2798df59 11463 if (resetCache || m_originalImageSize.GetWidth() <= 0 || m_originalImageSize.GetHeight() <= 0)
cdaed652 11464 {
23698b12 11465 m_imageCache = wxNullBitmap;
ce00f59b 11466
cdaed652
VZ
11467 m_imageBlock.Load(image);
11468 if (!image.IsOk())
11469 return false;
ce00f59b 11470
23698b12
JS
11471 m_originalImageSize = wxSize(image.GetWidth(), image.GetHeight());
11472 }
11473
11474 int width = m_originalImageSize.GetWidth();
11475 int height = m_originalImageSize.GetHeight();
11476
11477 int parentWidth = 0;
11478 int parentHeight = 0;
bec80f4f 11479
23698b12
JS
11480 int maxWidth = -1;
11481 int maxHeight = -1;
11482
914a4e23
JS
11483 wxSize sz = parentSize;
11484 if (sz == wxDefaultSize)
23698b12 11485 {
914a4e23
JS
11486 if (GetParent() && GetParent()->GetParent())
11487 sz = GetParent()->GetParent()->GetCachedSize();
11488 }
ab6b1860 11489
914a4e23
JS
11490 if (sz != wxDefaultSize)
11491 {
11492 wxRichTextBuffer* buffer = GetBuffer();
11493 if (buffer)
11494 {
11495 // Find the actual space available when margin is taken into account
23698b12
JS
11496 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
11497 marginRect = wxRect(0, 0, sz.x, sz.y);
914a4e23
JS
11498 if (GetParent() && GetParent()->GetParent())
11499 {
11500 buffer->GetBoxRects(dc, buffer, GetParent()->GetParent()->GetAttributes(), marginRect, borderRect, contentRect, paddingRect, outlineRect);
11501 sz = contentRect.GetSize();
11502 }
23698b12 11503
914a4e23
JS
11504 // Use a minimum size to stop images becoming very small
11505 parentWidth = wxMax(100, sz.GetWidth());
11506 parentHeight = wxMax(100, sz.GetHeight());
23698b12 11507
914a4e23
JS
11508 if (buffer->GetRichTextCtrl())
11509 // Start with a maximum width of the control size, even if not specified by the content,
11510 // to minimize the amount of picture overlapping the right-hand side
11511 maxWidth = parentWidth;
cdaed652 11512 }
23698b12
JS
11513 }
11514
11515 if (GetAttributes().GetTextBoxAttr().GetWidth().IsValid() && GetAttributes().GetTextBoxAttr().GetWidth().GetValue() > 0)
11516 {
11517 if (parentWidth > 0 && GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
11518 width = (int) ((GetAttributes().GetTextBoxAttr().GetWidth().GetValue() * parentWidth)/100.0);
11519 else if (GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
11520 width = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetWidth().GetValue());
11521 else if (GetAttributes().GetTextBoxAttr().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
11522 width = GetAttributes().GetTextBoxAttr().GetWidth().GetValue();
11523 }
11524
11525 // Limit to max width
11526
11527 if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().IsValid() && GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetValue() > 0)
11528 {
11529 int mw = -1;
11530
11531 if (parentWidth > 0 && GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
11532 mw = (int) ((GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetValue() * parentWidth)/100.0);
11533 else if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
11534 mw = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetValue());
11535 else if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
11536 mw = GetAttributes().GetTextBoxAttr().GetMaxSize().GetWidth().GetValue();
11537
11538 // If we already have a smaller max width due to the constraints of the control size,
11539 // don't use the larger max width.
11540 if (mw != -1 && ((maxWidth == -1) || (mw < maxWidth)))
11541 maxWidth = mw;
11542 }
11543
11544 if (maxWidth > 0 && width > maxWidth)
11545 width = maxWidth;
11546
11547 // Preserve the aspect ratio
11548 if (width != m_originalImageSize.GetWidth())
11549 height = (int) (float(m_originalImageSize.GetHeight()) * (float(width)/float(m_originalImageSize.GetWidth())));
11550
11551 if (GetAttributes().GetTextBoxAttr().GetHeight().IsValid() && GetAttributes().GetTextBoxAttr().GetHeight().GetValue() > 0)
11552 {
11553 if (parentHeight > 0 && GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
11554 height = (int) ((GetAttributes().GetTextBoxAttr().GetHeight().GetValue() * parentHeight)/100.0);
11555 else if (GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
11556 height = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetHeight().GetValue());
11557 else if (GetAttributes().GetTextBoxAttr().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
11558 height = GetAttributes().GetTextBoxAttr().GetHeight().GetValue();
11559
11560 // Preserve the aspect ratio
11561 if (height != m_originalImageSize.GetHeight())
11562 width = (int) (float(m_originalImageSize.GetWidth()) * (float(height)/float(m_originalImageSize.GetHeight())));
11563 }
11564
11565 // Limit to max height
11566
11567 if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().IsValid() && GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetValue() > 0)
11568 {
11569 if (parentHeight > 0 && GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
11570 maxHeight = (int) ((GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetValue() * parentHeight)/100.0);
11571 else if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
11572 maxHeight = ConvertTenthsMMToPixels(dc, GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetValue());
11573 else if (GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
11574 maxHeight = GetAttributes().GetTextBoxAttr().GetMaxSize().GetHeight().GetValue();
11575 }
11576
11577 if (maxHeight > 0 && height > maxHeight)
11578 {
11579 height = maxHeight;
11580
11581 // Preserve the aspect ratio
11582 if (height != m_originalImageSize.GetHeight())
11583 width = (int) (float(m_originalImageSize.GetWidth()) * (float(height)/float(m_originalImageSize.GetHeight())));
11584 }
11585
ab6b1860
JS
11586 // Prevent the use of zero size
11587 width = wxMax(1, width);
11588 height = wxMax(1, height);
11589
23698b12
JS
11590 if (m_imageCache.IsOk() && m_imageCache.GetWidth() == width && m_imageCache.GetHeight() == height)
11591 {
11592 // Do nothing, we didn't need to change the image cache
11593 }
11594 else
11595 {
11596 if (!image.IsOk())
cdaed652 11597 {
23698b12
JS
11598 m_imageBlock.Load(image);
11599 if (!image.IsOk())
11600 return false;
cdaed652 11601 }
5d7836c4 11602
cdaed652
VZ
11603 if (image.GetWidth() == width && image.GetHeight() == height)
11604 m_imageCache = wxBitmap(image);
11605 else
11606 {
11607 // If the original width and height is small, e.g. 400 or below,
11608 // scale up and then down to improve image quality. This can make
11609 // a big difference, with not much performance hit.
11610 int upscaleThreshold = 400;
11611 wxImage img;
11612 if (image.GetWidth() <= upscaleThreshold || image.GetHeight() <= upscaleThreshold)
11613 {
11614 img = image.Scale(image.GetWidth()*2, image.GetHeight()*2);
11615 img.Rescale(width, height, wxIMAGE_QUALITY_HIGH);
11616 }
11617 else
11618 img = image.Scale(width, height, wxIMAGE_QUALITY_HIGH);
11619 m_imageCache = wxBitmap(img);
11620 }
11621 }
ce00f59b 11622
cdaed652 11623 return m_imageCache.IsOk();
5d7836c4
JS
11624}
11625
5d7836c4 11626/// Draw the item
20d09da5 11627bool wxRichTextImage::Draw(wxDC& dc, wxRichTextDrawingContext& context, const wxRichTextRange& WXUNUSED(range), const wxRichTextSelection& selection, const wxRect& rect, int WXUNUSED(descent), int WXUNUSED(style))
5d7836c4 11628{
603f702b
JS
11629 if (!IsShown())
11630 return true;
11631
cdaed652
VZ
11632 // Don't need cached size AFAIK
11633 // wxSize size = GetCachedSize();
11634 if (!LoadImageCache(dc))
5d7836c4 11635 return false;
ce00f59b 11636
8db2e3ef
JS
11637 wxRichTextAttr attr(GetAttributes());
11638 context.ApplyVirtualAttributes(attr, this);
11639
11640 DrawBoxAttributes(dc, GetBuffer(), attr, wxRect(rect.GetPosition(), GetCachedSize()));
603f702b 11641
603f702b
JS
11642 wxSize imageSize(m_imageCache.GetWidth(), m_imageCache.GetHeight());
11643 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
11644 marginRect = rect; // outer rectangle, will calculate contentRect
8db2e3ef 11645 GetBoxRects(dc, GetBuffer(), attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
11646
11647 dc.DrawBitmap(m_imageCache, contentRect.x, contentRect.y, true);
5d7836c4 11648
a70eb13e 11649 if (selection.WithinSelection(GetRange().GetStart(), this))
5d7836c4 11650 {
ecb5fbf1
JS
11651 wxCheckSetBrush(dc, *wxBLACK_BRUSH);
11652 wxCheckSetPen(dc, *wxBLACK_PEN);
5d7836c4 11653 dc.SetLogicalFunction(wxINVERT);
603f702b 11654 dc.DrawRectangle(contentRect);
5d7836c4
JS
11655 dc.SetLogicalFunction(wxCOPY);
11656 }
11657
11658 return true;
11659}
11660
11661/// Lay the item out
8db2e3ef 11662bool wxRichTextImage::Layout(wxDC& dc, wxRichTextDrawingContext& context, const wxRect& rect, const wxRect& WXUNUSED(parentRect), int WXUNUSED(style))
5d7836c4 11663{
cdaed652
VZ
11664 if (!LoadImageCache(dc))
11665 return false;
5d7836c4 11666
603f702b
JS
11667 wxSize imageSize(m_imageCache.GetWidth(), m_imageCache.GetHeight());
11668 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
11669 contentRect = wxRect(wxPoint(0,0), imageSize);
8db2e3ef
JS
11670
11671 wxRichTextAttr attr(GetAttributes());
11672 context.ApplyVirtualAttributes(attr, this);
11673
11674 GetBoxRects(dc, GetBuffer(), attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
11675
11676 wxSize overallSize = marginRect.GetSize();
11677
11678 SetCachedSize(overallSize);
11679 SetMaxSize(overallSize);
11680 SetMinSize(overallSize);
cdaed652 11681 SetPosition(rect.GetPosition());
5d7836c4
JS
11682
11683 return true;
11684}
11685
11686/// Get/set the object size for the given range. Returns false if the range
11687/// is invalid for this object.
914a4e23 11688bool wxRichTextImage::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& WXUNUSED(descent), wxDC& dc, wxRichTextDrawingContext& context, int WXUNUSED(flags), const wxPoint& WXUNUSED(position), const wxSize& parentSize, wxArrayInt* partialExtents) const
5d7836c4
JS
11689{
11690 if (!range.IsWithin(GetRange()))
11691 return false;
11692
914a4e23 11693 if (!((wxRichTextImage*)this)->LoadImageCache(dc, false, parentSize))
31778480 11694 {
cdaed652
VZ
11695 size.x = 0; size.y = 0;
11696 if (partialExtents)
31778480 11697 partialExtents->Add(0);
cdaed652 11698 return false;
31778480 11699 }
ce00f59b 11700
8db2e3ef
JS
11701 wxRichTextAttr attr(GetAttributes());
11702 context.ApplyVirtualAttributes(attr, (wxRichTextObject*) this);
11703
603f702b
JS
11704 wxSize imageSize(m_imageCache.GetWidth(), m_imageCache.GetHeight());
11705 wxRect marginRect, borderRect, contentRect, paddingRect, outlineRect;
11706 contentRect = wxRect(wxPoint(0,0), imageSize);
8db2e3ef 11707 GetBoxRects(dc, GetBuffer(), attr, marginRect, borderRect, contentRect, paddingRect, outlineRect);
603f702b
JS
11708
11709 wxSize overallSize = marginRect.GetSize();
31778480 11710
cdaed652 11711 if (partialExtents)
603f702b 11712 partialExtents->Add(overallSize.x);
5d7836c4 11713
603f702b 11714 size = overallSize;
5d7836c4
JS
11715
11716 return true;
11717}
11718
603f702b
JS
11719// Get the 'natural' size for an object. For an image, it would be the
11720// image size.
11721wxTextAttrSize wxRichTextImage::GetNaturalSize() const
11722{
11723 wxTextAttrSize size;
11724 if (GetImageCache().IsOk())
11725 {
11726 size.SetWidth(GetImageCache().GetWidth(), wxTEXT_ATTR_UNITS_PIXELS);
11727 size.SetHeight(GetImageCache().GetHeight(), wxTEXT_ATTR_UNITS_PIXELS);
11728 }
11729 return size;
11730}
11731
11732
5d7836c4
JS
11733/// Copy
11734void wxRichTextImage::Copy(const wxRichTextImage& obj)
11735{
bec80f4f 11736 wxRichTextObject::Copy(obj);
59509217 11737
5d7836c4 11738 m_imageBlock = obj.m_imageBlock;
23698b12 11739 m_originalImageSize = obj.m_originalImageSize;
5d7836c4
JS
11740}
11741
cdaed652
VZ
11742/// Edit properties via a GUI
11743bool wxRichTextImage::EditProperties(wxWindow* parent, wxRichTextBuffer* buffer)
11744{
603f702b
JS
11745 wxRichTextObjectPropertiesDialog imageDlg(this, wxGetTopLevelParent(parent), wxID_ANY, _("Picture Properties"));
11746 imageDlg.SetAttributes(GetAttributes());
cdaed652
VZ
11747
11748 if (imageDlg.ShowModal() == wxID_OK)
11749 {
603f702b
JS
11750 // By passing wxRICHTEXT_SETSTYLE_RESET, indeterminate attributes set by the user will be set as
11751 // indeterminate in the object.
11752 imageDlg.ApplyStyle(buffer->GetRichTextCtrl(), wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RESET);
cdaed652
VZ
11753 return true;
11754 }
11755 else
11756 return false;
11757}
11758
5d7836c4
JS
11759/*!
11760 * Utilities
11761 *
11762 */
11763
11764/// Compare two attribute objects
24777478 11765bool wxTextAttrEq(const wxRichTextAttr& attr1, const wxRichTextAttr& attr2)
5d7836c4 11766{
38f833b1 11767 return (attr1 == attr2);
5d7836c4
JS
11768}
11769
44cc96a8
JS
11770/// Compare tabs
11771bool wxRichTextTabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
11772{
11773 if (tabs1.GetCount() != tabs2.GetCount())
5d7836c4
JS
11774 return false;
11775
44cc96a8
JS
11776 size_t i;
11777 for (i = 0; i < tabs1.GetCount(); i++)
11778 {
11779 if (tabs1[i] != tabs2[i])
11780 return false;
11781 }
11782 return true;
11783}
5d7836c4 11784
24777478 11785bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxRichTextAttr& style, wxRichTextAttr* compareWith)
44cc96a8
JS
11786{
11787 return destStyle.Apply(style, compareWith);
11788}
5d7836c4 11789
44cc96a8 11790// Remove attributes
24777478 11791bool wxRichTextRemoveStyle(wxRichTextAttr& destStyle, const wxRichTextAttr& style)
44cc96a8 11792{
24777478 11793 return destStyle.RemoveStyle(style);
44cc96a8 11794}
5d7836c4 11795
44cc96a8
JS
11796/// Combine two bitlists, specifying the bits of interest with separate flags.
11797bool wxRichTextCombineBitlists(int& valueA, int valueB, int& flagsA, int flagsB)
11798{
24777478 11799 return wxRichTextAttr::CombineBitlists(valueA, valueB, flagsA, flagsB);
44cc96a8 11800}
5d7836c4 11801
44cc96a8
JS
11802/// Compare two bitlists
11803bool wxRichTextBitlistsEqPartial(int valueA, int valueB, int flags)
11804{
24777478 11805 return wxRichTextAttr::BitlistsEqPartial(valueA, valueB, flags);
44cc96a8 11806}
38f833b1 11807
44cc96a8 11808/// Split into paragraph and character styles
24777478 11809bool wxRichTextSplitParaCharStyles(const wxRichTextAttr& style, wxRichTextAttr& parStyle, wxRichTextAttr& charStyle)
44cc96a8 11810{
24777478 11811 return wxRichTextAttr::SplitParaCharStyles(style, parStyle, charStyle);
44cc96a8 11812}
5d7836c4 11813
44cc96a8
JS
11814/// Convert a decimal to Roman numerals
11815wxString wxRichTextDecimalToRoman(long n)
11816{
11817 static wxArrayInt decimalNumbers;
11818 static wxArrayString romanNumbers;
5d7836c4 11819
44cc96a8
JS
11820 // Clean up arrays
11821 if (n == -1)
11822 {
11823 decimalNumbers.Clear();
11824 romanNumbers.Clear();
11825 return wxEmptyString;
11826 }
5d7836c4 11827
44cc96a8
JS
11828 if (decimalNumbers.GetCount() == 0)
11829 {
11830 #define wxRichTextAddDecRom(n, r) decimalNumbers.Add(n); romanNumbers.Add(r);
59509217 11831
44cc96a8
JS
11832 wxRichTextAddDecRom(1000, wxT("M"));
11833 wxRichTextAddDecRom(900, wxT("CM"));
11834 wxRichTextAddDecRom(500, wxT("D"));
11835 wxRichTextAddDecRom(400, wxT("CD"));
11836 wxRichTextAddDecRom(100, wxT("C"));
11837 wxRichTextAddDecRom(90, wxT("XC"));
11838 wxRichTextAddDecRom(50, wxT("L"));
11839 wxRichTextAddDecRom(40, wxT("XL"));
11840 wxRichTextAddDecRom(10, wxT("X"));
11841 wxRichTextAddDecRom(9, wxT("IX"));
11842 wxRichTextAddDecRom(5, wxT("V"));
11843 wxRichTextAddDecRom(4, wxT("IV"));
11844 wxRichTextAddDecRom(1, wxT("I"));
11845 }
5d7836c4 11846
44cc96a8
JS
11847 int i = 0;
11848 wxString roman;
ea160b2e 11849
44cc96a8 11850 while (n > 0 && i < 13)
42688aea 11851 {
44cc96a8
JS
11852 if (n >= decimalNumbers[i])
11853 {
11854 n -= decimalNumbers[i];
11855 roman += romanNumbers[i];
11856 }
11857 else
11858 {
11859 i ++;
11860 }
42688aea 11861 }
44cc96a8
JS
11862 if (roman.IsEmpty())
11863 roman = wxT("0");
11864 return roman;
11865}
42688aea 11866
44cc96a8
JS
11867/*!
11868 * wxRichTextFileHandler
11869 * Base class for file handlers
11870 */
4d6d8bf4 11871
44cc96a8 11872IMPLEMENT_CLASS(wxRichTextFileHandler, wxObject)
5d7836c4 11873
44cc96a8
JS
11874#if wxUSE_FFILE && wxUSE_STREAMS
11875bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer *buffer, const wxString& filename)
5d7836c4 11876{
44cc96a8 11877 wxFFileInputStream stream(filename);
a1b806b9 11878 if (stream.IsOk())
44cc96a8 11879 return LoadFile(buffer, stream);
5d7836c4 11880
44cc96a8
JS
11881 return false;
11882}
5d7836c4 11883
44cc96a8
JS
11884bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& filename)
11885{
11886 wxFFileOutputStream stream(filename);
a1b806b9 11887 if (stream.IsOk())
44cc96a8 11888 return SaveFile(buffer, stream);
5d7836c4 11889
44cc96a8
JS
11890 return false;
11891}
11892#endif // wxUSE_FFILE && wxUSE_STREAMS
5d7836c4 11893
44cc96a8
JS
11894/// Can we handle this filename (if using files)? By default, checks the extension.
11895bool wxRichTextFileHandler::CanHandle(const wxString& filename) const
11896{
11897 wxString path, file, ext;
a51e601e 11898 wxFileName::SplitPath(filename, & path, & file, & ext);
5d7836c4 11899
44cc96a8
JS
11900 return (ext.Lower() == GetExtension());
11901}
5d7836c4 11902
44cc96a8
JS
11903/*!
11904 * wxRichTextTextHandler
11905 * Plain text handler
11906 */
5d7836c4 11907
44cc96a8 11908IMPLEMENT_CLASS(wxRichTextPlainTextHandler, wxRichTextFileHandler)
5d7836c4 11909
44cc96a8
JS
11910#if wxUSE_STREAMS
11911bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
11912{
11913 if (!stream.IsOk())
797e38dd
JS
11914 return false;
11915
44cc96a8
JS
11916 wxString str;
11917 int lastCh = 0;
5d7836c4 11918
44cc96a8
JS
11919 while (!stream.Eof())
11920 {
11921 int ch = stream.GetC();
5d7836c4 11922
44cc96a8
JS
11923 if (!stream.Eof())
11924 {
11925 if (ch == 10 && lastCh != 13)
11926 str += wxT('\n');
5d7836c4 11927
44cc96a8
JS
11928 if (ch > 0 && ch != 10)
11929 str += wxChar(ch);
5d7836c4 11930
44cc96a8
JS
11931 lastCh = ch;
11932 }
11933 }
5d7836c4 11934
44cc96a8
JS
11935 buffer->ResetAndClearCommands();
11936 buffer->Clear();
11937 buffer->AddParagraphs(str);
11938 buffer->UpdateRanges();
5d7836c4 11939
44cc96a8
JS
11940 return true;
11941}
5d7836c4 11942
44cc96a8
JS
11943bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
11944{
11945 if (!stream.IsOk())
5d7836c4
JS
11946 return false;
11947
44cc96a8 11948 wxString text = buffer->GetText();
38f833b1 11949
44cc96a8
JS
11950 wxString newLine = wxRichTextLineBreakChar;
11951 text.Replace(newLine, wxT("\n"));
5d7836c4 11952
44cc96a8 11953 wxCharBuffer buf = text.ToAscii();
5d7836c4 11954
44cc96a8
JS
11955 stream.Write((const char*) buf, text.length());
11956 return true;
11957}
11958#endif // wxUSE_STREAMS
5d7836c4 11959
44cc96a8
JS
11960/*
11961 * Stores information about an image, in binary in-memory form
11962 */
59509217 11963
44cc96a8
JS
11964wxRichTextImageBlock::wxRichTextImageBlock()
11965{
11966 Init();
11967}
5d7836c4 11968
44cc96a8
JS
11969wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock& block):wxObject()
11970{
11971 Init();
11972 Copy(block);
11973}
ea160b2e 11974
44cc96a8
JS
11975wxRichTextImageBlock::~wxRichTextImageBlock()
11976{
5276b0a5 11977 wxDELETEA(m_data);
5d7836c4
JS
11978}
11979
44cc96a8 11980void wxRichTextImageBlock::Init()
5d7836c4
JS
11981{
11982 m_data = NULL;
11983 m_dataSize = 0;
d75a69e8 11984 m_imageType = wxBITMAP_TYPE_INVALID;
5d7836c4
JS
11985}
11986
11987void wxRichTextImageBlock::Clear()
11988{
5276b0a5 11989 wxDELETEA(m_data);
5d7836c4 11990 m_dataSize = 0;
d75a69e8 11991 m_imageType = wxBITMAP_TYPE_INVALID;
5d7836c4
JS
11992}
11993
11994
11995// Load the original image into a memory block.
11996// If the image is not a JPEG, we must convert it into a JPEG
11997// to conserve space.
11998// If it's not a JPEG we can make use of 'image', already scaled, so we don't have to
11999// load the image a 2nd time.
12000
d75a69e8
FM
12001bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, wxBitmapType imageType,
12002 wxImage& image, bool convertToJPEG)
5d7836c4
JS
12003{
12004 m_imageType = imageType;
12005
12006 wxString filenameToRead(filename);
7fe8059f 12007 bool removeFile = false;
5d7836c4 12008
62891c87 12009 if (imageType == wxBITMAP_TYPE_INVALID)
7fe8059f 12010 return false; // Could not determine image type
5d7836c4
JS
12011
12012 if ((imageType != wxBITMAP_TYPE_JPEG) && convertToJPEG)
12013 {
a51e601e
FM
12014 wxString tempFile =
12015 wxFileName::CreateTempFileName(_("image"));
5d7836c4 12016
a51e601e 12017 wxASSERT(!tempFile.IsEmpty());
5d7836c4
JS
12018
12019 image.SaveFile(tempFile, wxBITMAP_TYPE_JPEG);
12020 filenameToRead = tempFile;
7fe8059f 12021 removeFile = true;
5d7836c4
JS
12022
12023 m_imageType = wxBITMAP_TYPE_JPEG;
12024 }
12025 wxFile file;
12026 if (!file.Open(filenameToRead))
7fe8059f 12027 return false;
5d7836c4
JS
12028
12029 m_dataSize = (size_t) file.Length();
12030 file.Close();
12031
12032 if (m_data)
12033 delete[] m_data;
12034 m_data = ReadBlock(filenameToRead, m_dataSize);
12035
12036 if (removeFile)
12037 wxRemoveFile(filenameToRead);
12038
12039 return (m_data != NULL);
12040}
12041
12042// Make an image block from the wxImage in the given
12043// format.
d75a69e8 12044bool wxRichTextImageBlock::MakeImageBlock(wxImage& image, wxBitmapType imageType, int quality)
5d7836c4 12045{
5d7836c4
JS
12046 image.SetOption(wxT("quality"), quality);
12047
62891c87 12048 if (imageType == wxBITMAP_TYPE_INVALID)
7fe8059f 12049 return false; // Could not determine image type
5d7836c4 12050
cdaed652
VZ
12051 return DoMakeImageBlock(image, imageType);
12052}
12053
12054// Uses a const wxImage for efficiency, but can't set quality (only relevant for JPEG)
12055bool wxRichTextImageBlock::MakeImageBlockDefaultQuality(const wxImage& image, wxBitmapType imageType)
12056{
12057 if (imageType == wxBITMAP_TYPE_INVALID)
12058 return false; // Could not determine image type
ce00f59b 12059
cdaed652
VZ
12060 return DoMakeImageBlock(image, imageType);
12061}
7fe8059f 12062
cdaed652
VZ
12063// Makes the image block
12064bool wxRichTextImageBlock::DoMakeImageBlock(const wxImage& image, wxBitmapType imageType)
12065{
12066 wxMemoryOutputStream memStream;
12067 if (!image.SaveFile(memStream, imageType))
5d7836c4 12068 {
7fe8059f 12069 return false;
5d7836c4 12070 }
ce00f59b 12071
cdaed652
VZ
12072 unsigned char* block = new unsigned char[memStream.GetSize()];
12073 if (!block)
377c1ba4 12074 return false;
ce00f59b 12075
5d7836c4
JS
12076 if (m_data)
12077 delete[] m_data;
cdaed652 12078 m_data = block;
ce00f59b
VZ
12079
12080 m_imageType = imageType;
cdaed652 12081 m_dataSize = memStream.GetSize();
5d7836c4 12082
cdaed652 12083 memStream.CopyTo(m_data, m_dataSize);
5d7836c4
JS
12084
12085 return (m_data != NULL);
12086}
12087
5d7836c4
JS
12088// Write to a file
12089bool wxRichTextImageBlock::Write(const wxString& filename)
12090{
12091 return WriteBlock(filename, m_data, m_dataSize);
12092}
12093
12094void wxRichTextImageBlock::Copy(const wxRichTextImageBlock& block)
12095{
12096 m_imageType = block.m_imageType;
5276b0a5 12097 wxDELETEA(m_data);
5d7836c4
JS
12098 m_dataSize = block.m_dataSize;
12099 if (m_dataSize == 0)
12100 return;
12101
12102 m_data = new unsigned char[m_dataSize];
12103 unsigned int i;
12104 for (i = 0; i < m_dataSize; i++)
12105 m_data[i] = block.m_data[i];
12106}
12107
12108//// Operators
12109void wxRichTextImageBlock::operator=(const wxRichTextImageBlock& block)
12110{
12111 Copy(block);
12112}
12113
12114// Load a wxImage from the block
12115bool wxRichTextImageBlock::Load(wxImage& image)
12116{
12117 if (!m_data)
7fe8059f 12118 return false;
5d7836c4
JS
12119
12120 // Read in the image.
0ca07313 12121#if wxUSE_STREAMS
5d7836c4
JS
12122 wxMemoryInputStream mstream(m_data, m_dataSize);
12123 bool success = image.LoadFile(mstream, GetImageType());
12124#else
a51e601e
FM
12125 wxString tempFile = wxFileName::CreateTempFileName(_("image"));
12126 wxASSERT(!tempFile.IsEmpty());
5d7836c4
JS
12127
12128 if (!WriteBlock(tempFile, m_data, m_dataSize))
12129 {
7fe8059f 12130 return false;
5d7836c4
JS
12131 }
12132 success = image.LoadFile(tempFile, GetImageType());
12133 wxRemoveFile(tempFile);
12134#endif
12135
12136 return success;
12137}
12138
12139// Write data in hex to a stream
12140bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream)
12141{
4dc7ae1a
JS
12142 if (m_dataSize == 0)
12143 return true;
12144
12145 int bufSize = 100000;
a3c12576
JS
12146 if (int(2*m_dataSize) < bufSize)
12147 bufSize = 2*m_dataSize;
4dc7ae1a 12148 char* buf = new char[bufSize+1];
351c0647
JS
12149
12150 int left = m_dataSize;
12151 int n, i, j;
12152 j = 0;
12153 while (left > 0)
5d7836c4 12154 {
351c0647
JS
12155 if (left*2 > bufSize)
12156 {
12157 n = bufSize; left -= (bufSize/2);
12158 }
12159 else
12160 {
12161 n = left*2; left = 0;
12162 }
7fe8059f 12163
351c0647
JS
12164 char* b = buf;
12165 for (i = 0; i < (n/2); i++)
12166 {
f728025e 12167 wxDecToHex(m_data[j], b, b+1);
351c0647
JS
12168 b += 2; j ++;
12169 }
5d7836c4 12170
351c0647
JS
12171 buf[n] = 0;
12172 stream.Write((const char*) buf, n);
12173 }
4dc7ae1a 12174 delete[] buf;
5d7836c4
JS
12175 return true;
12176}
12177
12178// Read data in hex from a stream
d75a69e8 12179bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, wxBitmapType imageType)
5d7836c4
JS
12180{
12181 int dataSize = length/2;
12182
12183 if (m_data)
12184 delete[] m_data;
12185
046fce47
FM
12186 // create a null terminated temporary string:
12187 char str[3];
12188 str[2] = '\0';
12189
5d7836c4
JS
12190 m_data = new unsigned char[dataSize];
12191 int i;
12192 for (i = 0; i < dataSize; i ++)
12193 {
c9f78968
VS
12194 str[0] = (char)stream.GetC();
12195 str[1] = (char)stream.GetC();
5d7836c4 12196
a9465653 12197 m_data[i] = (unsigned char)wxHexToDec(str);
5d7836c4
JS
12198 }
12199
12200 m_dataSize = dataSize;
12201 m_imageType = imageType;
12202
12203 return true;
12204}
12205
5d7836c4
JS
12206// Allocate and read from stream as a block of memory
12207unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream& stream, size_t size)
12208{
12209 unsigned char* block = new unsigned char[size];
12210 if (!block)
12211 return NULL;
12212
12213 stream.Read(block, size);
12214
12215 return block;
12216}
12217
12218unsigned char* wxRichTextImageBlock::ReadBlock(const wxString& filename, size_t size)
12219{
12220 wxFileInputStream stream(filename);
a1b806b9 12221 if (!stream.IsOk())
5d7836c4
JS
12222 return NULL;
12223
12224 return ReadBlock(stream, size);
12225}
12226
12227// Write memory block to stream
12228bool wxRichTextImageBlock::WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size)
12229{
12230 stream.Write((void*) block, size);
12231 return stream.IsOk();
12232
12233}
12234
12235// Write memory block to file
12236bool wxRichTextImageBlock::WriteBlock(const wxString& filename, unsigned char* block, size_t size)
12237{
12238 wxFileOutputStream outStream(filename);
a1b806b9 12239 if (!outStream.IsOk())
7fe8059f 12240 return false;
5d7836c4
JS
12241
12242 return WriteBlock(outStream, block, size);
12243}
12244
d2d0adc7
JS
12245// Gets the extension for the block's type
12246wxString wxRichTextImageBlock::GetExtension() const
12247{
12248 wxImageHandler* handler = wxImage::FindHandler(GetImageType());
12249 if (handler)
12250 return handler->GetExtension();
12251 else
12252 return wxEmptyString;
12253}
12254
0ca07313
JS
12255#if wxUSE_DATAOBJ
12256
12257/*!
12258 * The data object for a wxRichTextBuffer
12259 */
12260
bafc4eac 12261const wxChar *wxRichTextBufferDataObject::ms_richTextBufferFormatId = wxT("wxRichText");
0ca07313
JS
12262
12263wxRichTextBufferDataObject::wxRichTextBufferDataObject(wxRichTextBuffer* richTextBuffer)
12264{
12265 m_richTextBuffer = richTextBuffer;
12266
12267 // this string should uniquely identify our format, but is otherwise
12268 // arbitrary
12269 m_formatRichTextBuffer.SetId(GetRichTextBufferFormatId());
12270
12271 SetFormat(m_formatRichTextBuffer);
12272}
12273
12274wxRichTextBufferDataObject::~wxRichTextBufferDataObject()
12275{
12276 delete m_richTextBuffer;
12277}
12278
12279// after a call to this function, the richTextBuffer is owned by the caller and it
12280// is responsible for deleting it!
12281wxRichTextBuffer* wxRichTextBufferDataObject::GetRichTextBuffer()
12282{
12283 wxRichTextBuffer* richTextBuffer = m_richTextBuffer;
12284 m_richTextBuffer = NULL;
12285
12286 return richTextBuffer;
12287}
12288
12289wxDataFormat wxRichTextBufferDataObject::GetPreferredFormat(Direction WXUNUSED(dir)) const
12290{
12291 return m_formatRichTextBuffer;
12292}
12293
12294size_t wxRichTextBufferDataObject::GetDataSize() const
12295{
12296 if (!m_richTextBuffer)
12297 return 0;
12298
12299 wxString bufXML;
12300
12301 {
12302 wxStringOutputStream stream(& bufXML);
12303 if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
12304 {
12305 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
12306 return 0;
12307 }
12308 }
12309
12310#if wxUSE_UNICODE
12311 wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
12312 return strlen(buffer) + 1;
12313#else
12314 return bufXML.Length()+1;
12315#endif
12316}
12317
12318bool wxRichTextBufferDataObject::GetDataHere(void *pBuf) const
12319{
12320 if (!pBuf || !m_richTextBuffer)
12321 return false;
12322
12323 wxString bufXML;
12324
12325 {
12326 wxStringOutputStream stream(& bufXML);
12327 if (!m_richTextBuffer->SaveFile(stream, wxRICHTEXT_TYPE_XML))
12328 {
12329 wxLogError(wxT("Could not write the buffer to an XML stream.\nYou may have forgotten to add the XML file handler."));
12330 return 0;
12331 }
12332 }
12333
12334#if wxUSE_UNICODE
12335 wxCharBuffer buffer = bufXML.mb_str(wxConvUTF8);
12336 size_t len = strlen(buffer);
12337 memcpy((char*) pBuf, (const char*) buffer, len);
12338 ((char*) pBuf)[len] = 0;
12339#else
12340 size_t len = bufXML.Length();
12341 memcpy((char*) pBuf, (const char*) bufXML.c_str(), len);
12342 ((char*) pBuf)[len] = 0;
12343#endif
12344
12345 return true;
12346}
12347
12348bool wxRichTextBufferDataObject::SetData(size_t WXUNUSED(len), const void *buf)
12349{
5276b0a5 12350 wxDELETE(m_richTextBuffer);
0ca07313
JS
12351
12352 wxString bufXML((const char*) buf, wxConvUTF8);
12353
12354 m_richTextBuffer = new wxRichTextBuffer;
12355
12356 wxStringInputStream stream(bufXML);
12357 if (!m_richTextBuffer->LoadFile(stream, wxRICHTEXT_TYPE_XML))
12358 {
12359 wxLogError(wxT("Could not read the buffer from an XML stream.\nYou may have forgotten to add the XML file handler."));
12360
5276b0a5 12361 wxDELETE(m_richTextBuffer);
0ca07313
JS
12362
12363 return false;
12364 }
12365 return true;
12366}
12367
12368#endif
12369 // wxUSE_DATAOBJ
12370
44cc96a8
JS
12371
12372/*
12373 * wxRichTextFontTable
12374 * Manages quick access to a pool of fonts for rendering rich text
12375 */
12376
d65381ac 12377WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxFont, wxRichTextFontTableHashMap, class WXDLLIMPEXP_RICHTEXT);
44cc96a8
JS
12378
12379class wxRichTextFontTableData: public wxObjectRefData
12380{
12381public:
12382 wxRichTextFontTableData() {}
12383
32423dd8 12384 wxFont FindFont(const wxRichTextAttr& fontSpec, double fontScale);
44cc96a8
JS
12385
12386 wxRichTextFontTableHashMap m_hashMap;
12387};
12388
32423dd8 12389wxFont wxRichTextFontTableData::FindFont(const wxRichTextAttr& fontSpec, double fontScale)
44cc96a8
JS
12390{
12391 wxString facename(fontSpec.GetFontFaceName());
44cc96a8 12392
32423dd8
JS
12393 int fontSize = fontSpec.GetFontSize();
12394 if (fontScale != 1.0)
12395 fontSize = (int) ((double(fontSize) * fontScale) + 0.5);
12396
12397 wxString units;
12398 if (fontSpec.HasFontPixelSize() && !fontSpec.HasFontPointSize())
12399 units = wxT("px");
12400 else
12401 units = wxT("pt");
12402 wxString spec = wxString::Format(wxT("%d-%s-%d-%d-%d-%d-%s-%d"),
12403 fontSize, units.c_str(), fontSpec.GetFontStyle(), fontSpec.GetFontWeight(), (int) fontSpec.GetFontUnderlined(), (int) fontSpec.GetFontStrikethrough(),
12404 facename.c_str(), (int) fontSpec.GetFontEncoding());
12405
12406 wxRichTextFontTableHashMap::iterator entry = m_hashMap.find(spec);
44cc96a8
JS
12407 if ( entry == m_hashMap.end() )
12408 {
32423dd8
JS
12409 if (fontSpec.HasFontPixelSize() && !fontSpec.HasFontPointSize())
12410 {
b42e4b3e 12411 wxFont font(wxSize(0, fontSize), wxFONTFAMILY_DEFAULT, fontSpec.GetFontStyle(), fontSpec.GetFontWeight(), fontSpec.GetFontUnderlined(), facename);
32423dd8
JS
12412 if (fontSpec.HasFontStrikethrough() && fontSpec.GetFontStrikethrough())
12413 font.SetStrikethrough(true);
12414 m_hashMap[spec] = font;
12415 return font;
12416 }
12417 else
12418 {
5a0e33af 12419 wxFont font(fontSize, wxFONTFAMILY_DEFAULT, fontSpec.GetFontStyle(), fontSpec.GetFontWeight(), fontSpec.GetFontUnderlined(), facename.c_str());
32423dd8
JS
12420 if (fontSpec.HasFontStrikethrough() && fontSpec.GetFontStrikethrough())
12421 font.SetStrikethrough(true);
12422
12423 m_hashMap[spec] = font;
12424 return font;
12425 }
44cc96a8
JS
12426 }
12427 else
12428 {
12429 return entry->second;
12430 }
12431}
12432
12433IMPLEMENT_DYNAMIC_CLASS(wxRichTextFontTable, wxObject)
12434
12435wxRichTextFontTable::wxRichTextFontTable()
12436{
12437 m_refData = new wxRichTextFontTableData;
32423dd8 12438 m_fontScale = 1.0;
44cc96a8
JS
12439}
12440
12441wxRichTextFontTable::wxRichTextFontTable(const wxRichTextFontTable& table)
2a230426 12442 : wxObject()
44cc96a8
JS
12443{
12444 (*this) = table;
12445}
12446
12447wxRichTextFontTable::~wxRichTextFontTable()
12448{
12449 UnRef();
12450}
12451
12452bool wxRichTextFontTable::operator == (const wxRichTextFontTable& table) const
12453{
12454 return (m_refData == table.m_refData);
12455}
12456
12457void wxRichTextFontTable::operator= (const wxRichTextFontTable& table)
12458{
12459 Ref(table);
32423dd8 12460 m_fontScale = table.m_fontScale;
44cc96a8
JS
12461}
12462
24777478 12463wxFont wxRichTextFontTable::FindFont(const wxRichTextAttr& fontSpec)
44cc96a8
JS
12464{
12465 wxRichTextFontTableData* data = (wxRichTextFontTableData*) m_refData;
12466 if (data)
32423dd8 12467 return data->FindFont(fontSpec, m_fontScale);
44cc96a8
JS
12468 else
12469 return wxFont();
12470}
12471
12472void wxRichTextFontTable::Clear()
12473{
12474 wxRichTextFontTableData* data = (wxRichTextFontTableData*) m_refData;
12475 if (data)
12476 data->m_hashMap.clear();
12477}
12478
32423dd8
JS
12479void wxRichTextFontTable::SetFontScale(double fontScale)
12480{
12481 if (fontScale != m_fontScale)
12482 Clear();
12483 m_fontScale = fontScale;
12484}
12485
24777478
JS
12486// wxTextBoxAttr
12487
24777478
JS
12488void wxTextBoxAttr::Reset()
12489{
12490 m_flags = 0;
603f702b
JS
12491 m_floatMode = wxTEXT_BOX_ATTR_FLOAT_NONE;
12492 m_clearMode = wxTEXT_BOX_ATTR_CLEAR_NONE;
12493 m_collapseMode = wxTEXT_BOX_ATTR_COLLAPSE_NONE;
12494 m_verticalAlignment = wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_NONE;
2f987d83 12495 m_boxStyleName = wxEmptyString;
bec80f4f 12496
24777478
JS
12497 m_margins.Reset();
12498 m_padding.Reset();
12499 m_position.Reset();
12500
603f702b 12501 m_size.Reset();
303f0be7
JS
12502 m_minSize.Reset();
12503 m_maxSize.Reset();
24777478
JS
12504
12505 m_border.Reset();
12506 m_outline.Reset();
12507}
12508
12509// Equality test
12510bool wxTextBoxAttr::operator== (const wxTextBoxAttr& attr) const
12511{
12512 return (
12513 m_flags == attr.m_flags &&
12514 m_floatMode == attr.m_floatMode &&
12515 m_clearMode == attr.m_clearMode &&
12516 m_collapseMode == attr.m_collapseMode &&
603f702b 12517 m_verticalAlignment == attr.m_verticalAlignment &&
bec80f4f 12518
24777478
JS
12519 m_margins == attr.m_margins &&
12520 m_padding == attr.m_padding &&
12521 m_position == attr.m_position &&
12522
603f702b 12523 m_size == attr.m_size &&
303f0be7
JS
12524 m_minSize == attr.m_minSize &&
12525 m_maxSize == attr.m_maxSize &&
24777478
JS
12526
12527 m_border == attr.m_border &&
2f987d83
JS
12528 m_outline == attr.m_outline &&
12529
12530 m_boxStyleName == attr.m_boxStyleName
24777478
JS
12531 );
12532}
12533
12534// Partial equality test
32423dd8 12535bool wxTextBoxAttr::EqPartial(const wxTextBoxAttr& attr, bool weakTest) const
24777478 12536{
32423dd8
JS
12537 if (!weakTest &&
12538 ((!HasFloatMode() && attr.HasFloatMode()) ||
12539 (!HasClearMode() && attr.HasClearMode()) ||
12540 (!HasCollapseBorders() && attr.HasCollapseBorders()) ||
12541 (!HasVerticalAlignment() && attr.HasVerticalAlignment()) ||
12542 (!HasBoxStyleName() && attr.HasBoxStyleName())))
12543 {
12544 return false;
12545 }
24777478
JS
12546 if (attr.HasFloatMode() && HasFloatMode() && (GetFloatMode() != attr.GetFloatMode()))
12547 return false;
12548
12549 if (attr.HasClearMode() && HasClearMode() && (GetClearMode() != attr.GetClearMode()))
12550 return false;
12551
12552 if (attr.HasCollapseBorders() && HasCollapseBorders() && (attr.GetCollapseBorders() != GetCollapseBorders()))
12553 return false;
12554
603f702b
JS
12555 if (attr.HasVerticalAlignment() && HasVerticalAlignment() && (attr.GetVerticalAlignment() != GetVerticalAlignment()))
12556 return false;
12557
2f987d83
JS
12558 if (attr.HasBoxStyleName() && HasBoxStyleName() && (attr.GetBoxStyleName() != GetBoxStyleName()))
12559 return false;
12560
24777478
JS
12561 // Position
12562
32423dd8 12563 if (!m_position.EqPartial(attr.m_position, weakTest))
24777478
JS
12564 return false;
12565
303f0be7
JS
12566 // Size
12567
32423dd8 12568 if (!m_size.EqPartial(attr.m_size, weakTest))
303f0be7 12569 return false;
32423dd8 12570 if (!m_minSize.EqPartial(attr.m_minSize, weakTest))
303f0be7 12571 return false;
32423dd8 12572 if (!m_maxSize.EqPartial(attr.m_maxSize, weakTest))
303f0be7
JS
12573 return false;
12574
24777478
JS
12575 // Margins
12576
32423dd8 12577 if (!m_margins.EqPartial(attr.m_margins, weakTest))
24777478
JS
12578 return false;
12579
12580 // Padding
12581
32423dd8 12582 if (!m_padding.EqPartial(attr.m_padding, weakTest))
24777478
JS
12583 return false;
12584
12585 // Border
12586
32423dd8 12587 if (!GetBorder().EqPartial(attr.GetBorder(), weakTest))
24777478
JS
12588 return false;
12589
12590 // Outline
12591
32423dd8 12592 if (!GetOutline().EqPartial(attr.GetOutline(), weakTest))
24777478
JS
12593 return false;
12594
12595 return true;
12596}
12597
12598// Merges the given attributes. If compareWith
12599// is non-NULL, then it will be used to mask out those attributes that are the same in style
12600// and compareWith, for situations where we don't want to explicitly set inherited attributes.
12601bool wxTextBoxAttr::Apply(const wxTextBoxAttr& attr, const wxTextBoxAttr* compareWith)
12602{
12603 if (attr.HasFloatMode())
12604 {
12605 if (!(compareWith && compareWith->HasFloatMode() && compareWith->GetFloatMode() == attr.GetFloatMode()))
12606 SetFloatMode(attr.GetFloatMode());
12607 }
12608
12609 if (attr.HasClearMode())
12610 {
12611 if (!(compareWith && compareWith->HasClearMode() && compareWith->GetClearMode() == attr.GetClearMode()))
12612 SetClearMode(attr.GetClearMode());
12613 }
12614
12615 if (attr.HasCollapseBorders())
12616 {
12617 if (!(compareWith && compareWith->HasCollapseBorders() && compareWith->GetCollapseBorders() == attr.GetCollapseBorders()))
603f702b
JS
12618 SetCollapseBorders(attr.GetCollapseBorders());
12619 }
12620
12621 if (attr.HasVerticalAlignment())
12622 {
12623 if (!(compareWith && compareWith->HasVerticalAlignment() && compareWith->GetVerticalAlignment() == attr.GetVerticalAlignment()))
12624 SetVerticalAlignment(attr.GetVerticalAlignment());
24777478 12625 }
bec80f4f 12626
2f987d83
JS
12627 if (attr.HasBoxStyleName())
12628 {
12629 if (!(compareWith && compareWith->HasBoxStyleName() && compareWith->GetBoxStyleName() == attr.GetBoxStyleName()))
12630 SetBoxStyleName(attr.GetBoxStyleName());
12631 }
12632
bec80f4f
JS
12633 m_margins.Apply(attr.m_margins, compareWith ? (& attr.m_margins) : (const wxTextAttrDimensions*) NULL);
12634 m_padding.Apply(attr.m_padding, compareWith ? (& attr.m_padding) : (const wxTextAttrDimensions*) NULL);
12635 m_position.Apply(attr.m_position, compareWith ? (& attr.m_position) : (const wxTextAttrDimensions*) NULL);
24777478 12636
603f702b 12637 m_size.Apply(attr.m_size, compareWith ? (& attr.m_size) : (const wxTextAttrSize*) NULL);
303f0be7
JS
12638 m_minSize.Apply(attr.m_minSize, compareWith ? (& attr.m_minSize) : (const wxTextAttrSize*) NULL);
12639 m_maxSize.Apply(attr.m_maxSize, compareWith ? (& attr.m_maxSize) : (const wxTextAttrSize*) NULL);
24777478 12640
bec80f4f
JS
12641 m_border.Apply(attr.m_border, compareWith ? (& attr.m_border) : (const wxTextAttrBorders*) NULL);
12642 m_outline.Apply(attr.m_outline, compareWith ? (& attr.m_outline) : (const wxTextAttrBorders*) NULL);
24777478
JS
12643
12644 return true;
12645}
12646
12647// Remove specified attributes from this object
12648bool wxTextBoxAttr::RemoveStyle(const wxTextBoxAttr& attr)
12649{
12650 if (attr.HasFloatMode())
12651 RemoveFlag(wxTEXT_BOX_ATTR_FLOAT);
12652
12653 if (attr.HasClearMode())
12654 RemoveFlag(wxTEXT_BOX_ATTR_CLEAR);
12655
12656 if (attr.HasCollapseBorders())
12657 RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS);
12658
603f702b
JS
12659 if (attr.HasVerticalAlignment())
12660 RemoveFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT);
12661
2f987d83
JS
12662 if (attr.HasBoxStyleName())
12663 {
12664 SetBoxStyleName(wxEmptyString);
12665 RemoveFlag(wxTEXT_BOX_ATTR_BOX_STYLE_NAME);
12666 }
12667
24777478
JS
12668 m_margins.RemoveStyle(attr.m_margins);
12669 m_padding.RemoveStyle(attr.m_padding);
12670 m_position.RemoveStyle(attr.m_position);
12671
603f702b 12672 m_size.RemoveStyle(attr.m_size);
303f0be7
JS
12673 m_minSize.RemoveStyle(attr.m_minSize);
12674 m_maxSize.RemoveStyle(attr.m_maxSize);
24777478
JS
12675
12676 m_border.RemoveStyle(attr.m_border);
12677 m_outline.RemoveStyle(attr.m_outline);
12678
12679 return true;
12680}
12681
12682// Collects the attributes that are common to a range of content, building up a note of
12683// which attributes are absent in some objects and which clash in some objects.
12684void wxTextBoxAttr::CollectCommonAttributes(const wxTextBoxAttr& attr, wxTextBoxAttr& clashingAttr, wxTextBoxAttr& absentAttr)
12685{
12686 if (attr.HasFloatMode())
12687 {
12688 if (!clashingAttr.HasFloatMode() && !absentAttr.HasFloatMode())
12689 {
12690 if (HasFloatMode())
12691 {
12692 if (GetFloatMode() != attr.GetFloatMode())
12693 {
12694 clashingAttr.AddFlag(wxTEXT_BOX_ATTR_FLOAT);
12695 RemoveFlag(wxTEXT_BOX_ATTR_FLOAT);
12696 }
12697 }
12698 else
12699 SetFloatMode(attr.GetFloatMode());
12700 }
12701 }
12702 else
12703 absentAttr.AddFlag(wxTEXT_BOX_ATTR_FLOAT);
bec80f4f 12704
24777478
JS
12705 if (attr.HasClearMode())
12706 {
12707 if (!clashingAttr.HasClearMode() && !absentAttr.HasClearMode())
12708 {
12709 if (HasClearMode())
12710 {
12711 if (GetClearMode() != attr.GetClearMode())
12712 {
12713 clashingAttr.AddFlag(wxTEXT_BOX_ATTR_CLEAR);
12714 RemoveFlag(wxTEXT_BOX_ATTR_CLEAR);
12715 }
12716 }
12717 else
12718 SetClearMode(attr.GetClearMode());
12719 }
12720 }
12721 else
12722 absentAttr.AddFlag(wxTEXT_BOX_ATTR_CLEAR);
12723
12724 if (attr.HasCollapseBorders())
12725 {
12726 if (!clashingAttr.HasCollapseBorders() && !absentAttr.HasCollapseBorders())
12727 {
12728 if (HasCollapseBorders())
12729 {
12730 if (GetCollapseBorders() != attr.GetCollapseBorders())
12731 {
12732 clashingAttr.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS);
12733 RemoveFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS);
12734 }
12735 }
12736 else
12737 SetCollapseBorders(attr.GetCollapseBorders());
12738 }
12739 }
12740 else
12741 absentAttr.AddFlag(wxTEXT_BOX_ATTR_COLLAPSE_BORDERS);
bec80f4f 12742
603f702b
JS
12743 if (attr.HasVerticalAlignment())
12744 {
12745 if (!clashingAttr.HasVerticalAlignment() && !absentAttr.HasVerticalAlignment())
12746 {
12747 if (HasVerticalAlignment())
12748 {
12749 if (GetVerticalAlignment() != attr.GetVerticalAlignment())
12750 {
12751 clashingAttr.AddFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT);
12752 RemoveFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT);
12753 }
12754 }
12755 else
12756 SetVerticalAlignment(attr.GetVerticalAlignment());
12757 }
12758 }
12759 else
12760 absentAttr.AddFlag(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT);
12761
2f987d83
JS
12762 if (attr.HasBoxStyleName())
12763 {
12764 if (!clashingAttr.HasBoxStyleName() && !absentAttr.HasBoxStyleName())
12765 {
12766 if (HasBoxStyleName())
12767 {
12768 if (GetBoxStyleName() != attr.GetBoxStyleName())
12769 {
12770 clashingAttr.AddFlag(wxTEXT_BOX_ATTR_BOX_STYLE_NAME);
12771 RemoveFlag(wxTEXT_BOX_ATTR_BOX_STYLE_NAME);
12772 }
12773 }
12774 else
12775 SetBoxStyleName(attr.GetBoxStyleName());
12776 }
12777 }
12778 else
12779 absentAttr.AddFlag(wxTEXT_BOX_ATTR_BOX_STYLE_NAME);
12780
24777478
JS
12781 m_margins.CollectCommonAttributes(attr.m_margins, clashingAttr.m_margins, absentAttr.m_margins);
12782 m_padding.CollectCommonAttributes(attr.m_padding, clashingAttr.m_padding, absentAttr.m_padding);
12783 m_position.CollectCommonAttributes(attr.m_position, clashingAttr.m_position, absentAttr.m_position);
12784
603f702b 12785 m_size.CollectCommonAttributes(attr.m_size, clashingAttr.m_size, absentAttr.m_size);
303f0be7
JS
12786 m_minSize.CollectCommonAttributes(attr.m_minSize, clashingAttr.m_minSize, absentAttr.m_minSize);
12787 m_maxSize.CollectCommonAttributes(attr.m_maxSize, clashingAttr.m_maxSize, absentAttr.m_maxSize);
24777478
JS
12788
12789 m_border.CollectCommonAttributes(attr.m_border, clashingAttr.m_border, absentAttr.m_border);
12790 m_outline.CollectCommonAttributes(attr.m_outline, clashingAttr.m_outline, absentAttr.m_outline);
12791}
12792
eb3d8a33
JS
12793bool wxTextBoxAttr::IsDefault() const
12794{
12795 return GetFlags() == 0 && !m_border.IsValid() && !m_outline.IsValid() &&
303f0be7 12796 !m_size.IsValid() && !m_minSize.IsValid() && !m_maxSize.IsValid() &&
eb3d8a33
JS
12797 !m_position.IsValid() && !m_padding.IsValid() && !m_margins.IsValid();
12798}
12799
24777478
JS
12800// wxRichTextAttr
12801
12802void wxRichTextAttr::Copy(const wxRichTextAttr& attr)
12803{
bec80f4f
JS
12804 wxTextAttr::Copy(attr);
12805
24777478
JS
12806 m_textBoxAttr = attr.m_textBoxAttr;
12807}
12808
12809bool wxRichTextAttr::operator==(const wxRichTextAttr& attr) const
12810{
12811 if (!(wxTextAttr::operator==(attr)))
12812 return false;
bec80f4f 12813
24777478
JS
12814 return (m_textBoxAttr == attr.m_textBoxAttr);
12815}
12816
32423dd8
JS
12817// Partial equality test
12818bool wxRichTextAttr::EqPartial(const wxRichTextAttr& attr, bool weakTest) const
24777478 12819{
32423dd8 12820 if (!(wxTextAttr::EqPartial(attr, weakTest)))
24777478 12821 return false;
bec80f4f 12822
32423dd8 12823 return m_textBoxAttr.EqPartial(attr.m_textBoxAttr, weakTest);
24777478
JS
12824}
12825
12826// Merges the given attributes. If compareWith
12827// is non-NULL, then it will be used to mask out those attributes that are the same in style
12828// and compareWith, for situations where we don't want to explicitly set inherited attributes.
12829bool wxRichTextAttr::Apply(const wxRichTextAttr& style, const wxRichTextAttr* compareWith)
12830{
12831 wxTextAttr::Apply(style, compareWith);
12832
12833 return m_textBoxAttr.Apply(style.m_textBoxAttr, compareWith ? (& compareWith->m_textBoxAttr) : (const wxTextBoxAttr*) NULL);
12834}
12835
12836// Remove specified attributes from this object
12837bool wxRichTextAttr::RemoveStyle(const wxRichTextAttr& attr)
12838{
12839 wxTextAttr::RemoveStyle(*this, attr);
12840
12841 return m_textBoxAttr.RemoveStyle(attr.m_textBoxAttr);
12842}
12843
12844// Collects the attributes that are common to a range of content, building up a note of
12845// which attributes are absent in some objects and which clash in some objects.
12846void wxRichTextAttr::CollectCommonAttributes(const wxRichTextAttr& attr, wxRichTextAttr& clashingAttr, wxRichTextAttr& absentAttr)
12847{
12848 wxTextAttrCollectCommonAttributes(*this, attr, clashingAttr, absentAttr);
bec80f4f 12849
24777478
JS
12850 m_textBoxAttr.CollectCommonAttributes(attr.m_textBoxAttr, clashingAttr.m_textBoxAttr, absentAttr.m_textBoxAttr);
12851}
12852
12853// Partial equality test
32423dd8 12854bool wxTextAttrBorder::EqPartial(const wxTextAttrBorder& border, bool weakTest) const
24777478 12855{
32423dd8
JS
12856 if (!weakTest &&
12857 ((!HasStyle() && border.HasStyle()) ||
12858 (!HasColour() && border.HasColour()) ||
12859 (!HasWidth() && border.HasWidth())))
12860 {
12861 return false;
12862 }
12863
12864 if (border.HasStyle() && HasStyle() && (border.GetStyle() != GetStyle()))
24777478
JS
12865 return false;
12866
32423dd8 12867 if (border.HasColour() && HasColour() && (border.GetColourLong() != GetColourLong()))
24777478
JS
12868 return false;
12869
32423dd8 12870 if (border.HasWidth() && HasWidth() && !(border.GetWidth() == GetWidth()))
24777478
JS
12871 return false;
12872
12873 return true;
12874}
12875
12876// Apply border to 'this', but not if the same as compareWith
bec80f4f 12877bool wxTextAttrBorder::Apply(const wxTextAttrBorder& border, const wxTextAttrBorder* compareWith)
24777478
JS
12878{
12879 if (border.HasStyle())
12880 {
12881 if (!(compareWith && (border.GetStyle() == compareWith->GetStyle())))
12882 SetStyle(border.GetStyle());
12883 }
12884 if (border.HasColour())
12885 {
12886 if (!(compareWith && (border.GetColourLong() == compareWith->GetColourLong())))
12887 SetColour(border.GetColourLong());
12888 }
12889 if (border.HasWidth())
12890 {
12891 if (!(compareWith && (border.GetWidth() == compareWith->GetWidth())))
12892 SetWidth(border.GetWidth());
12893 }
12894
12895 return true;
12896}
12897
12898// Remove specified attributes from this object
bec80f4f 12899bool wxTextAttrBorder::RemoveStyle(const wxTextAttrBorder& attr)
24777478
JS
12900{
12901 if (attr.HasStyle() && HasStyle())
12902 SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_STYLE);
12903 if (attr.HasColour() && HasColour())
12904 SetFlags(GetFlags() & ~wxTEXT_BOX_ATTR_BORDER_COLOUR);
12905 if (attr.HasWidth() && HasWidth())
12906 m_borderWidth.Reset();
12907
12908 return true;
12909}
12910
12911// Collects the attributes that are common to a range of content, building up a note of
12912// which attributes are absent in some objects and which clash in some objects.
bec80f4f 12913void wxTextAttrBorder::CollectCommonAttributes(const wxTextAttrBorder& attr, wxTextAttrBorder& clashingAttr, wxTextAttrBorder& absentAttr)
24777478
JS
12914{
12915 if (attr.HasStyle())
12916 {
12917 if (!clashingAttr.HasStyle() && !absentAttr.HasStyle())
12918 {
12919 if (HasStyle())
12920 {
12921 if (GetStyle() != attr.GetStyle())
12922 {
12923 clashingAttr.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE);
12924 RemoveFlag(wxTEXT_BOX_ATTR_BORDER_STYLE);
12925 }
12926 }
12927 else
12928 SetStyle(attr.GetStyle());
12929 }
12930 }
12931 else
12932 absentAttr.AddFlag(wxTEXT_BOX_ATTR_BORDER_STYLE);
12933
12934 if (attr.HasColour())
12935 {
12936 if (!clashingAttr.HasColour() && !absentAttr.HasColour())
12937 {
12938 if (HasColour())
12939 {
12940 if (GetColour() != attr.GetColour())
12941 {
12942 clashingAttr.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR);
12943 RemoveFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR);
12944 }
12945 }
12946 else
12947 SetColour(attr.GetColourLong());
12948 }
12949 }
12950 else
12951 absentAttr.AddFlag(wxTEXT_BOX_ATTR_BORDER_COLOUR);
bec80f4f 12952
24777478
JS
12953 m_borderWidth.CollectCommonAttributes(attr.m_borderWidth, clashingAttr.m_borderWidth, absentAttr.m_borderWidth);
12954}
12955
12956// Partial equality test
32423dd8 12957bool wxTextAttrBorders::EqPartial(const wxTextAttrBorders& borders, bool weakTest) const
24777478 12958{
32423dd8
JS
12959 return m_left.EqPartial(borders.m_left, weakTest) && m_right.EqPartial(borders.m_right, weakTest) &&
12960 m_top.EqPartial(borders.m_top, weakTest) && m_bottom.EqPartial(borders.m_bottom, weakTest);
24777478
JS
12961}
12962
12963// Apply border to 'this', but not if the same as compareWith
bec80f4f 12964bool wxTextAttrBorders::Apply(const wxTextAttrBorders& borders, const wxTextAttrBorders* compareWith)
24777478 12965{
bec80f4f
JS
12966 m_left.Apply(borders.m_left, compareWith ? (& compareWith->m_left) : (const wxTextAttrBorder*) NULL);
12967 m_right.Apply(borders.m_right, compareWith ? (& compareWith->m_right) : (const wxTextAttrBorder*) NULL);
12968 m_top.Apply(borders.m_top, compareWith ? (& compareWith->m_top) : (const wxTextAttrBorder*) NULL);
12969 m_bottom.Apply(borders.m_bottom, compareWith ? (& compareWith->m_bottom) : (const wxTextAttrBorder*) NULL);
24777478
JS
12970 return true;
12971}
12972
12973// Remove specified attributes from this object
bec80f4f 12974bool wxTextAttrBorders::RemoveStyle(const wxTextAttrBorders& attr)
24777478
JS
12975{
12976 m_left.RemoveStyle(attr.m_left);
12977 m_right.RemoveStyle(attr.m_right);
12978 m_top.RemoveStyle(attr.m_top);
12979 m_bottom.RemoveStyle(attr.m_bottom);
12980 return true;
12981}
12982
12983// Collects the attributes that are common to a range of content, building up a note of
12984// which attributes are absent in some objects and which clash in some objects.
bec80f4f 12985void wxTextAttrBorders::CollectCommonAttributes(const wxTextAttrBorders& attr, wxTextAttrBorders& clashingAttr, wxTextAttrBorders& absentAttr)
24777478
JS
12986{
12987 m_left.CollectCommonAttributes(attr.m_left, clashingAttr.m_left, absentAttr.m_left);
12988 m_right.CollectCommonAttributes(attr.m_right, clashingAttr.m_right, absentAttr.m_right);
12989 m_top.CollectCommonAttributes(attr.m_top, clashingAttr.m_top, absentAttr.m_top);
12990 m_bottom.CollectCommonAttributes(attr.m_bottom, clashingAttr.m_bottom, absentAttr.m_bottom);
12991}
12992
12993// Set style of all borders
bec80f4f 12994void wxTextAttrBorders::SetStyle(int style)
24777478
JS
12995{
12996 m_left.SetStyle(style);
12997 m_right.SetStyle(style);
12998 m_top.SetStyle(style);
12999 m_bottom.SetStyle(style);
13000}
13001
13002// Set colour of all borders
bec80f4f 13003void wxTextAttrBorders::SetColour(unsigned long colour)
24777478
JS
13004{
13005 m_left.SetColour(colour);
13006 m_right.SetColour(colour);
13007 m_top.SetColour(colour);
13008 m_bottom.SetColour(colour);
13009}
13010
bec80f4f 13011void wxTextAttrBorders::SetColour(const wxColour& colour)
24777478
JS
13012{
13013 m_left.SetColour(colour);
13014 m_right.SetColour(colour);
13015 m_top.SetColour(colour);
13016 m_bottom.SetColour(colour);
13017}
13018
13019// Set width of all borders
bec80f4f 13020void wxTextAttrBorders::SetWidth(const wxTextAttrDimension& width)
24777478
JS
13021{
13022 m_left.SetWidth(width);
13023 m_right.SetWidth(width);
13024 m_top.SetWidth(width);
13025 m_bottom.SetWidth(width);
13026}
13027
13028// Partial equality test
32423dd8 13029bool wxTextAttrDimension::EqPartial(const wxTextAttrDimension& dim, bool weakTest) const
24777478 13030{
32423dd8
JS
13031 if (!weakTest && !IsValid() && dim.IsValid())
13032 return false;
13033
603f702b 13034 if (dim.IsValid() && IsValid() && !((*this) == dim))
24777478
JS
13035 return false;
13036 else
13037 return true;
13038}
13039
13040bool wxTextAttrDimension::Apply(const wxTextAttrDimension& dim, const wxTextAttrDimension* compareWith)
13041{
603f702b 13042 if (dim.IsValid())
24777478
JS
13043 {
13044 if (!(compareWith && dim == (*compareWith)))
13045 (*this) = dim;
13046 }
13047
13048 return true;
13049}
13050
13051// Collects the attributes that are common to a range of content, building up a note of
13052// which attributes are absent in some objects and which clash in some objects.
13053void wxTextAttrDimension::CollectCommonAttributes(const wxTextAttrDimension& attr, wxTextAttrDimension& clashingAttr, wxTextAttrDimension& absentAttr)
13054{
603f702b 13055 if (attr.IsValid())
24777478 13056 {
603f702b 13057 if (!clashingAttr.IsValid() && !absentAttr.IsValid())
24777478 13058 {
603f702b 13059 if (IsValid())
24777478
JS
13060 {
13061 if (!((*this) == attr))
13062 {
603f702b
JS
13063 clashingAttr.SetValid(true);
13064 SetValid(false);
24777478
JS
13065 }
13066 }
13067 else
13068 (*this) = attr;
13069 }
13070 }
13071 else
603f702b 13072 absentAttr.SetValid(true);
24777478
JS
13073}
13074
8995db52
JS
13075wxTextAttrDimensionConverter::wxTextAttrDimensionConverter(wxDC& dc, double scale, const wxSize& parentSize)
13076{
13077 m_ppi = dc.GetPPI().x; m_scale = scale; m_parentSize = parentSize;
13078}
13079
13080wxTextAttrDimensionConverter::wxTextAttrDimensionConverter(int ppi, double scale, const wxSize& parentSize)
13081{
13082 m_ppi = ppi; m_scale = scale; m_parentSize = parentSize;
13083}
13084
bec80f4f
JS
13085int wxTextAttrDimensionConverter::ConvertTenthsMMToPixels(int units) const
13086{
13087 return wxRichTextObject::ConvertTenthsMMToPixels(m_ppi, units, m_scale);
13088}
13089
13090int wxTextAttrDimensionConverter::ConvertPixelsToTenthsMM(int pixels) const
13091{
13092 return wxRichTextObject::ConvertPixelsToTenthsMM(m_ppi, pixels, m_scale);
13093}
13094
13095int wxTextAttrDimensionConverter::GetPixels(const wxTextAttrDimension& dim, int direction) const
13096{
13097 if (dim.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
13098 return ConvertTenthsMMToPixels(dim.GetValue());
13099 else if (dim.GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
13100 return dim.GetValue();
13101 else if (dim.GetUnits() == wxTEXT_ATTR_UNITS_PERCENTAGE)
13102 {
13103 wxASSERT(m_parentSize != wxDefaultSize);
13104 if (direction == wxHORIZONTAL)
13105 return (int) (double(m_parentSize.x) * double(dim.GetValue()) / 100.0);
13106 else
13107 return (int) (double(m_parentSize.y) * double(dim.GetValue()) / 100.0);
13108 }
13109 else
13110 {
13111 wxASSERT(false);
13112 return 0;
13113 }
13114}
13115
13116int wxTextAttrDimensionConverter::GetTenthsMM(const wxTextAttrDimension& dim) const
13117{
13118 if (dim.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM)
13119 return dim.GetValue();
13120 else if (dim.GetUnits() == wxTEXT_ATTR_UNITS_PIXELS)
13121 return ConvertPixelsToTenthsMM(dim.GetValue());
13122 else
13123 {
13124 wxASSERT(false);
13125 return 0;
13126 }
13127}
13128
24777478 13129// Partial equality test
32423dd8 13130bool wxTextAttrDimensions::EqPartial(const wxTextAttrDimensions& dims, bool weakTest) const
24777478 13131{
32423dd8 13132 if (!m_left.EqPartial(dims.m_left, weakTest))
24777478
JS
13133 return false;
13134
32423dd8 13135 if (!m_right.EqPartial(dims.m_right, weakTest))
24777478
JS
13136 return false;
13137
32423dd8 13138 if (!m_top.EqPartial(dims.m_top, weakTest))
24777478
JS
13139 return false;
13140
32423dd8 13141 if (!m_bottom.EqPartial(dims.m_bottom, weakTest))
24777478
JS
13142 return false;
13143
13144 return true;
13145}
13146
13147// Apply border to 'this', but not if the same as compareWith
bec80f4f 13148bool wxTextAttrDimensions::Apply(const wxTextAttrDimensions& dims, const wxTextAttrDimensions* compareWith)
24777478
JS
13149{
13150 m_left.Apply(dims.m_left, compareWith ? (& compareWith->m_left) : (const wxTextAttrDimension*) NULL);
13151 m_right.Apply(dims.m_right, compareWith ? (& compareWith->m_right): (const wxTextAttrDimension*) NULL);
13152 m_top.Apply(dims.m_top, compareWith ? (& compareWith->m_top): (const wxTextAttrDimension*) NULL);
13153 m_bottom.Apply(dims.m_bottom, compareWith ? (& compareWith->m_bottom): (const wxTextAttrDimension*) NULL);
13154
13155 return true;
13156}
13157
13158// Remove specified attributes from this object
bec80f4f 13159bool wxTextAttrDimensions::RemoveStyle(const wxTextAttrDimensions& attr)
24777478 13160{
603f702b 13161 if (attr.m_left.IsValid())
24777478 13162 m_left.Reset();
603f702b 13163 if (attr.m_right.IsValid())
24777478 13164 m_right.Reset();
603f702b 13165 if (attr.m_top.IsValid())
24777478 13166 m_top.Reset();
603f702b 13167 if (attr.m_bottom.IsValid())
24777478
JS
13168 m_bottom.Reset();
13169
13170 return true;
13171}
13172
13173// Collects the attributes that are common to a range of content, building up a note of
13174// which attributes are absent in some objects and which clash in some objects.
bec80f4f 13175void wxTextAttrDimensions::CollectCommonAttributes(const wxTextAttrDimensions& attr, wxTextAttrDimensions& clashingAttr, wxTextAttrDimensions& absentAttr)
24777478
JS
13176{
13177 m_left.CollectCommonAttributes(attr.m_left, clashingAttr.m_left, absentAttr.m_left);
13178 m_right.CollectCommonAttributes(attr.m_right, clashingAttr.m_right, absentAttr.m_right);
13179 m_top.CollectCommonAttributes(attr.m_top, clashingAttr.m_top, absentAttr.m_top);
13180 m_bottom.CollectCommonAttributes(attr.m_bottom, clashingAttr.m_bottom, absentAttr.m_bottom);
13181}
13182
603f702b 13183// Partial equality test
32423dd8 13184bool wxTextAttrSize::EqPartial(const wxTextAttrSize& size, bool weakTest) const
603f702b 13185{
32423dd8 13186 if (!m_width.EqPartial(size.m_width, weakTest))
603f702b
JS
13187 return false;
13188
32423dd8 13189 if (!m_height.EqPartial(size.m_height, weakTest))
603f702b
JS
13190 return false;
13191
13192 return true;
13193}
13194
13195// Apply border to 'this', but not if the same as compareWith
13196bool wxTextAttrSize::Apply(const wxTextAttrSize& size, const wxTextAttrSize* compareWith)
13197{
13198 m_width.Apply(size.m_width, compareWith ? (& compareWith->m_width) : (const wxTextAttrDimension*) NULL);
13199 m_height.Apply(size.m_height, compareWith ? (& compareWith->m_height): (const wxTextAttrDimension*) NULL);
13200
13201 return true;
13202}
13203
13204// Remove specified attributes from this object
13205bool wxTextAttrSize::RemoveStyle(const wxTextAttrSize& attr)
13206{
13207 if (attr.m_width.IsValid())
13208 m_width.Reset();
13209 if (attr.m_height.IsValid())
13210 m_height.Reset();
13211
13212 return true;
13213}
13214
13215// Collects the attributes that are common to a range of content, building up a note of
13216// which attributes are absent in some objects and which clash in some objects.
13217void wxTextAttrSize::CollectCommonAttributes(const wxTextAttrSize& attr, wxTextAttrSize& clashingAttr, wxTextAttrSize& absentAttr)
13218{
13219 m_width.CollectCommonAttributes(attr.m_width, clashingAttr.m_width, absentAttr.m_width);
13220 m_height.CollectCommonAttributes(attr.m_height, clashingAttr.m_height, absentAttr.m_height);
13221}
13222
24777478
JS
13223// Collects the attributes that are common to a range of content, building up a note of
13224// which attributes are absent in some objects and which clash in some objects.
13225void wxTextAttrCollectCommonAttributes(wxTextAttr& currentStyle, const wxTextAttr& attr, wxTextAttr& clashingAttr, wxTextAttr& absentAttr)
13226{
13227 absentAttr.SetFlags(absentAttr.GetFlags() | (~attr.GetFlags() & wxTEXT_ATTR_ALL));
13228 absentAttr.SetTextEffectFlags(absentAttr.GetTextEffectFlags() | (~attr.GetTextEffectFlags() & 0xFFFF));
13229
13230 long forbiddenFlags = clashingAttr.GetFlags()|absentAttr.GetFlags();
13231
c4168888
JS
13232 // If different font size units are being used, this is a clash.
13233 if (((attr.GetFlags() & wxTEXT_ATTR_FONT_SIZE) | (currentStyle.GetFlags() & wxTEXT_ATTR_FONT_SIZE)) == wxTEXT_ATTR_FONT_SIZE)
24777478 13234 {
c4168888
JS
13235 currentStyle.SetFontSize(0);
13236 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_SIZE);
13237 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_SIZE);
13238 }
13239 else
13240 {
13241 if (attr.HasFontPointSize() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_POINT_SIZE))
340ef5c5 13242 {
c4168888 13243 if (currentStyle.HasFontPointSize())
24777478 13244 {
c4168888 13245 if (currentStyle.GetFontSize() != attr.GetFontSize())
24777478 13246 {
c4168888
JS
13247 // Clash of attr - mark as such
13248 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_POINT_SIZE);
13249 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_POINT_SIZE);
32423dd8
JS
13250 }
13251 }
c4168888
JS
13252 else
13253 currentStyle.SetFontSize(attr.GetFontSize());
13254 }
13255 else if (!attr.HasFontPointSize() && currentStyle.HasFontPointSize())
13256 {
13257 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_POINT_SIZE);
13258 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_POINT_SIZE);
32423dd8
JS
13259 }
13260
c4168888 13261 if (attr.HasFontPixelSize() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_PIXEL_SIZE))
24777478 13262 {
c4168888 13263 if (currentStyle.HasFontPixelSize())
24777478 13264 {
c4168888 13265 if (currentStyle.GetFontSize() != attr.GetFontSize())
24777478
JS
13266 {
13267 // Clash of attr - mark as such
c4168888
JS
13268 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_PIXEL_SIZE);
13269 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_PIXEL_SIZE);
24777478
JS
13270 }
13271 }
13272 else
c4168888 13273 currentStyle.SetFontPixelSize(attr.GetFontSize());
24777478 13274 }
c4168888
JS
13275 else if (!attr.HasFontPixelSize() && currentStyle.HasFontPixelSize())
13276 {
13277 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_PIXEL_SIZE);
13278 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_PIXEL_SIZE);
13279 }
13280 }
24777478 13281
c4168888
JS
13282 if (attr.HasFontItalic() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_ITALIC))
13283 {
13284 if (currentStyle.HasFontItalic())
24777478 13285 {
c4168888 13286 if (currentStyle.GetFontStyle() != attr.GetFontStyle())
24777478 13287 {
c4168888
JS
13288 // Clash of attr - mark as such
13289 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_ITALIC);
13290 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_ITALIC);
24777478 13291 }
24777478 13292 }
c4168888
JS
13293 else
13294 currentStyle.SetFontStyle(attr.GetFontStyle());
13295 }
13296 else if (!attr.HasFontItalic() && currentStyle.HasFontItalic())
13297 {
13298 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_ITALIC);
13299 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_ITALIC);
13300 }
24777478 13301
c4168888
JS
13302 if (attr.HasFontFamily() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_FAMILY))
13303 {
13304 if (currentStyle.HasFontFamily())
24777478 13305 {
c4168888 13306 if (currentStyle.GetFontFamily() != attr.GetFontFamily())
24777478 13307 {
c4168888
JS
13308 // Clash of attr - mark as such
13309 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_FAMILY);
13310 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_FAMILY);
24777478 13311 }
24777478 13312 }
c4168888
JS
13313 else
13314 currentStyle.SetFontFamily(attr.GetFontFamily());
13315 }
13316 else if (!attr.HasFontFamily() && currentStyle.HasFontFamily())
13317 {
13318 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_FAMILY);
13319 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_FAMILY);
13320 }
24777478 13321
c4168888
JS
13322 if (attr.HasFontWeight() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_WEIGHT))
13323 {
13324 if (currentStyle.HasFontWeight())
24777478 13325 {
c4168888 13326 if (currentStyle.GetFontWeight() != attr.GetFontWeight())
24777478 13327 {
c4168888
JS
13328 // Clash of attr - mark as such
13329 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_WEIGHT);
13330 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_WEIGHT);
13331 }
13332 }
13333 else
13334 currentStyle.SetFontWeight(attr.GetFontWeight());
13335 }
13336 else if (!attr.HasFontWeight() && currentStyle.HasFontWeight())
13337 {
13338 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_WEIGHT);
13339 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_WEIGHT);
13340 }
24777478 13341
c4168888
JS
13342 if (attr.HasFontFaceName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_FACE))
13343 {
13344 if (currentStyle.HasFontFaceName())
13345 {
13346 wxString faceName1(currentStyle.GetFontFaceName());
13347 wxString faceName2(attr.GetFontFaceName());
13348
13349 if (faceName1 != faceName2)
13350 {
13351 // Clash of attr - mark as such
13352 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_FACE);
13353 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_FACE);
24777478 13354 }
24777478 13355 }
c4168888
JS
13356 else
13357 currentStyle.SetFontFaceName(attr.GetFontFaceName());
13358 }
13359 else if (!attr.HasFontFaceName() && currentStyle.HasFontFaceName())
13360 {
13361 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_FACE);
13362 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_FACE);
13363 }
24777478 13364
c4168888
JS
13365 if (attr.HasFontUnderlined() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_UNDERLINE))
13366 {
13367 if (currentStyle.HasFontUnderlined())
24777478 13368 {
c4168888 13369 if (currentStyle.GetFontUnderlined() != attr.GetFontUnderlined())
24777478 13370 {
c4168888
JS
13371 // Clash of attr - mark as such
13372 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_UNDERLINE);
13373 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_UNDERLINE);
24777478 13374 }
24777478 13375 }
c4168888
JS
13376 else
13377 currentStyle.SetFontUnderlined(attr.GetFontUnderlined());
13378 }
13379 else if (!attr.HasFontUnderlined() && currentStyle.HasFontUnderlined())
13380 {
13381 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_UNDERLINE);
13382 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_UNDERLINE);
13383 }
32423dd8 13384
c4168888
JS
13385 if (attr.HasFontStrikethrough() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_FONT_STRIKETHROUGH))
13386 {
13387 if (currentStyle.HasFontStrikethrough())
32423dd8 13388 {
c4168888 13389 if (currentStyle.GetFontStrikethrough() != attr.GetFontStrikethrough())
32423dd8 13390 {
c4168888
JS
13391 // Clash of attr - mark as such
13392 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_STRIKETHROUGH);
13393 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_STRIKETHROUGH);
32423dd8 13394 }
32423dd8 13395 }
c4168888
JS
13396 else
13397 currentStyle.SetFontStrikethrough(attr.GetFontStrikethrough());
13398 }
13399 else if (!attr.HasFontStrikethrough() && currentStyle.HasFontStrikethrough())
13400 {
13401 clashingAttr.AddFlag(wxTEXT_ATTR_FONT_STRIKETHROUGH);
13402 currentStyle.RemoveFlag(wxTEXT_ATTR_FONT_STRIKETHROUGH);
24777478
JS
13403 }
13404
13405 if (attr.HasTextColour() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_TEXT_COLOUR))
13406 {
13407 if (currentStyle.HasTextColour())
13408 {
13409 if (currentStyle.GetTextColour() != attr.GetTextColour())
13410 {
13411 // Clash of attr - mark as such
13412 clashingAttr.AddFlag(wxTEXT_ATTR_TEXT_COLOUR);
13413 currentStyle.RemoveFlag(wxTEXT_ATTR_TEXT_COLOUR);
13414 }
13415 }
13416 else
13417 currentStyle.SetTextColour(attr.GetTextColour());
13418 }
c4168888
JS
13419 else if (!attr.HasTextColour() && currentStyle.HasTextColour())
13420 {
13421 clashingAttr.AddFlag(wxTEXT_ATTR_TEXT_COLOUR);
13422 currentStyle.RemoveFlag(wxTEXT_ATTR_TEXT_COLOUR);
13423 }
24777478
JS
13424
13425 if (attr.HasBackgroundColour() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BACKGROUND_COLOUR))
13426 {
13427 if (currentStyle.HasBackgroundColour())
13428 {
13429 if (currentStyle.GetBackgroundColour() != attr.GetBackgroundColour())
13430 {
13431 // Clash of attr - mark as such
13432 clashingAttr.AddFlag(wxTEXT_ATTR_BACKGROUND_COLOUR);
13433 currentStyle.RemoveFlag(wxTEXT_ATTR_BACKGROUND_COLOUR);
13434 }
13435 }
13436 else
13437 currentStyle.SetBackgroundColour(attr.GetBackgroundColour());
13438 }
c4168888
JS
13439 else if (!attr.HasBackgroundColour() && currentStyle.HasBackgroundColour())
13440 {
13441 clashingAttr.AddFlag(wxTEXT_ATTR_BACKGROUND_COLOUR);
13442 currentStyle.RemoveFlag(wxTEXT_ATTR_BACKGROUND_COLOUR);
13443 }
24777478
JS
13444
13445 if (attr.HasAlignment() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_ALIGNMENT))
13446 {
13447 if (currentStyle.HasAlignment())
13448 {
13449 if (currentStyle.GetAlignment() != attr.GetAlignment())
13450 {
13451 // Clash of attr - mark as such
13452 clashingAttr.AddFlag(wxTEXT_ATTR_ALIGNMENT);
13453 currentStyle.RemoveFlag(wxTEXT_ATTR_ALIGNMENT);
13454 }
13455 }
13456 else
13457 currentStyle.SetAlignment(attr.GetAlignment());
13458 }
c4168888
JS
13459 else if (!attr.HasAlignment() && currentStyle.HasAlignment())
13460 {
13461 clashingAttr.AddFlag(wxTEXT_ATTR_ALIGNMENT);
13462 currentStyle.RemoveFlag(wxTEXT_ATTR_ALIGNMENT);
13463 }
24777478
JS
13464
13465 if (attr.HasTabs() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_TABS))
13466 {
13467 if (currentStyle.HasTabs())
13468 {
13469 if (!wxRichTextTabsEq(currentStyle.GetTabs(), attr.GetTabs()))
13470 {
13471 // Clash of attr - mark as such
13472 clashingAttr.AddFlag(wxTEXT_ATTR_TABS);
13473 currentStyle.RemoveFlag(wxTEXT_ATTR_TABS);
13474 }
13475 }
13476 else
13477 currentStyle.SetTabs(attr.GetTabs());
13478 }
c4168888
JS
13479 else if (!attr.HasTabs() && currentStyle.HasTabs())
13480 {
13481 clashingAttr.AddFlag(wxTEXT_ATTR_TABS);
13482 currentStyle.RemoveFlag(wxTEXT_ATTR_TABS);
13483 }
24777478
JS
13484
13485 if (attr.HasLeftIndent() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_LEFT_INDENT))
13486 {
13487 if (currentStyle.HasLeftIndent())
13488 {
13489 if (currentStyle.GetLeftIndent() != attr.GetLeftIndent() || currentStyle.GetLeftSubIndent() != attr.GetLeftSubIndent())
13490 {
13491 // Clash of attr - mark as such
13492 clashingAttr.AddFlag(wxTEXT_ATTR_LEFT_INDENT);
13493 currentStyle.RemoveFlag(wxTEXT_ATTR_LEFT_INDENT);
13494 }
13495 }
13496 else
13497 currentStyle.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
13498 }
c4168888
JS
13499 else if (!attr.HasLeftIndent() && currentStyle.HasLeftIndent())
13500 {
13501 clashingAttr.AddFlag(wxTEXT_ATTR_LEFT_INDENT);
13502 currentStyle.RemoveFlag(wxTEXT_ATTR_LEFT_INDENT);
13503 }
24777478
JS
13504
13505 if (attr.HasRightIndent() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_RIGHT_INDENT))
13506 {
13507 if (currentStyle.HasRightIndent())
13508 {
13509 if (currentStyle.GetRightIndent() != attr.GetRightIndent())
13510 {
13511 // Clash of attr - mark as such
13512 clashingAttr.AddFlag(wxTEXT_ATTR_RIGHT_INDENT);
13513 currentStyle.RemoveFlag(wxTEXT_ATTR_RIGHT_INDENT);
13514 }
13515 }
13516 else
13517 currentStyle.SetRightIndent(attr.GetRightIndent());
13518 }
c4168888
JS
13519 else if (!attr.HasRightIndent() && currentStyle.HasRightIndent())
13520 {
13521 clashingAttr.AddFlag(wxTEXT_ATTR_RIGHT_INDENT);
13522 currentStyle.RemoveFlag(wxTEXT_ATTR_RIGHT_INDENT);
13523 }
24777478
JS
13524
13525 if (attr.HasParagraphSpacingAfter() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_PARA_SPACING_AFTER))
13526 {
13527 if (currentStyle.HasParagraphSpacingAfter())
13528 {
13529 if (currentStyle.GetParagraphSpacingAfter() != attr.GetParagraphSpacingAfter())
13530 {
13531 // Clash of attr - mark as such
13532 clashingAttr.AddFlag(wxTEXT_ATTR_PARA_SPACING_AFTER);
13533 currentStyle.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_AFTER);
13534 }
13535 }
13536 else
13537 currentStyle.SetParagraphSpacingAfter(attr.GetParagraphSpacingAfter());
13538 }
c4168888
JS
13539 else if (!attr.HasParagraphSpacingAfter() && currentStyle.HasParagraphSpacingAfter())
13540 {
13541 clashingAttr.AddFlag(wxTEXT_ATTR_PARA_SPACING_AFTER);
13542 currentStyle.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_AFTER);
13543 }
24777478
JS
13544
13545 if (attr.HasParagraphSpacingBefore() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_PARA_SPACING_BEFORE))
13546 {
13547 if (currentStyle.HasParagraphSpacingBefore())
13548 {
13549 if (currentStyle.GetParagraphSpacingBefore() != attr.GetParagraphSpacingBefore())
13550 {
13551 // Clash of attr - mark as such
13552 clashingAttr.AddFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE);
13553 currentStyle.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE);
13554 }
13555 }
13556 else
13557 currentStyle.SetParagraphSpacingBefore(attr.GetParagraphSpacingBefore());
13558 }
c4168888
JS
13559 else if (!attr.HasParagraphSpacingBefore() && currentStyle.HasParagraphSpacingBefore())
13560 {
13561 clashingAttr.AddFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE);
13562 currentStyle.RemoveFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE);
13563 }
24777478
JS
13564
13565 if (attr.HasLineSpacing() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_LINE_SPACING))
13566 {
13567 if (currentStyle.HasLineSpacing())
13568 {
13569 if (currentStyle.GetLineSpacing() != attr.GetLineSpacing())
13570 {
13571 // Clash of attr - mark as such
13572 clashingAttr.AddFlag(wxTEXT_ATTR_LINE_SPACING);
13573 currentStyle.RemoveFlag(wxTEXT_ATTR_LINE_SPACING);
13574 }
13575 }
13576 else
13577 currentStyle.SetLineSpacing(attr.GetLineSpacing());
13578 }
c4168888
JS
13579 else if (!attr.HasLineSpacing() && currentStyle.HasLineSpacing())
13580 {
13581 clashingAttr.AddFlag(wxTEXT_ATTR_LINE_SPACING);
13582 currentStyle.RemoveFlag(wxTEXT_ATTR_LINE_SPACING);
13583 }
24777478
JS
13584
13585 if (attr.HasCharacterStyleName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_CHARACTER_STYLE_NAME))
13586 {
13587 if (currentStyle.HasCharacterStyleName())
13588 {
13589 if (currentStyle.GetCharacterStyleName() != attr.GetCharacterStyleName())
13590 {
13591 // Clash of attr - mark as such
13592 clashingAttr.AddFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME);
13593 currentStyle.RemoveFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME);
13594 }
13595 }
13596 else
13597 currentStyle.SetCharacterStyleName(attr.GetCharacterStyleName());
13598 }
c4168888
JS
13599 else if (!attr.HasCharacterStyleName() && currentStyle.HasCharacterStyleName())
13600 {
13601 clashingAttr.AddFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME);
13602 currentStyle.RemoveFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME);
13603 }
24777478
JS
13604
13605 if (attr.HasParagraphStyleName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_PARAGRAPH_STYLE_NAME))
13606 {
13607 if (currentStyle.HasParagraphStyleName())
13608 {
13609 if (currentStyle.GetParagraphStyleName() != attr.GetParagraphStyleName())
13610 {
13611 // Clash of attr - mark as such
13612 clashingAttr.AddFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME);
13613 currentStyle.RemoveFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME);
13614 }
13615 }
13616 else
13617 currentStyle.SetParagraphStyleName(attr.GetParagraphStyleName());
13618 }
c4168888
JS
13619 else if (!attr.HasParagraphStyleName() && currentStyle.HasParagraphStyleName())
13620 {
13621 clashingAttr.AddFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME);
13622 currentStyle.RemoveFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME);
13623 }
24777478
JS
13624
13625 if (attr.HasListStyleName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_LIST_STYLE_NAME))
13626 {
13627 if (currentStyle.HasListStyleName())
13628 {
13629 if (currentStyle.GetListStyleName() != attr.GetListStyleName())
13630 {
13631 // Clash of attr - mark as such
13632 clashingAttr.AddFlag(wxTEXT_ATTR_LIST_STYLE_NAME);
13633 currentStyle.RemoveFlag(wxTEXT_ATTR_LIST_STYLE_NAME);
13634 }
13635 }
13636 else
13637 currentStyle.SetListStyleName(attr.GetListStyleName());
13638 }
c4168888
JS
13639 else if (!attr.HasListStyleName() && currentStyle.HasListStyleName())
13640 {
13641 clashingAttr.AddFlag(wxTEXT_ATTR_LIST_STYLE_NAME);
13642 currentStyle.RemoveFlag(wxTEXT_ATTR_LIST_STYLE_NAME);
13643 }
24777478
JS
13644
13645 if (attr.HasBulletStyle() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BULLET_STYLE))
13646 {
13647 if (currentStyle.HasBulletStyle())
13648 {
13649 if (currentStyle.GetBulletStyle() != attr.GetBulletStyle())
13650 {
13651 // Clash of attr - mark as such
13652 clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_STYLE);
13653 currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_STYLE);
13654 }
13655 }
13656 else
13657 currentStyle.SetBulletStyle(attr.GetBulletStyle());
13658 }
c4168888
JS
13659 else if (!attr.HasBulletStyle() && currentStyle.HasBulletStyle())
13660 {
13661 clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_STYLE);
13662 currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_STYLE);
13663 }
24777478
JS
13664
13665 if (attr.HasBulletNumber() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BULLET_NUMBER))
13666 {
13667 if (currentStyle.HasBulletNumber())
13668 {
13669 if (currentStyle.GetBulletNumber() != attr.GetBulletNumber())
13670 {
13671 // Clash of attr - mark as such
13672 clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_NUMBER);
13673 currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_NUMBER);
13674 }
13675 }
13676 else
13677 currentStyle.SetBulletNumber(attr.GetBulletNumber());
13678 }
c4168888
JS
13679 else if (!attr.HasBulletNumber() && currentStyle.HasBulletNumber())
13680 {
13681 clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_NUMBER);
13682 currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_NUMBER);
13683 }
24777478
JS
13684
13685 if (attr.HasBulletText() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BULLET_TEXT))
13686 {
13687 if (currentStyle.HasBulletText())
13688 {
13689 if (currentStyle.GetBulletText() != attr.GetBulletText())
13690 {
13691 // Clash of attr - mark as such
13692 clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_TEXT);
13693 currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_TEXT);
13694 }
13695 }
13696 else
13697 {
13698 currentStyle.SetBulletText(attr.GetBulletText());
13699 currentStyle.SetBulletFont(attr.GetBulletFont());
13700 }
13701 }
c4168888
JS
13702 else if (!attr.HasBulletText() && currentStyle.HasBulletText())
13703 {
13704 clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_TEXT);
13705 currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_TEXT);
13706 }
24777478
JS
13707
13708 if (attr.HasBulletName() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_BULLET_NAME))
13709 {
13710 if (currentStyle.HasBulletName())
13711 {
13712 if (currentStyle.GetBulletName() != attr.GetBulletName())
13713 {
13714 // Clash of attr - mark as such
13715 clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_NAME);
13716 currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_NAME);
13717 }
13718 }
13719 else
13720 {
13721 currentStyle.SetBulletName(attr.GetBulletName());
13722 }
13723 }
c4168888
JS
13724 else if (!attr.HasBulletName() && currentStyle.HasBulletName())
13725 {
13726 clashingAttr.AddFlag(wxTEXT_ATTR_BULLET_NAME);
13727 currentStyle.RemoveFlag(wxTEXT_ATTR_BULLET_NAME);
13728 }
24777478
JS
13729
13730 if (attr.HasURL() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_URL))
13731 {
13732 if (currentStyle.HasURL())
13733 {
13734 if (currentStyle.GetURL() != attr.GetURL())
13735 {
13736 // Clash of attr - mark as such
13737 clashingAttr.AddFlag(wxTEXT_ATTR_URL);
13738 currentStyle.RemoveFlag(wxTEXT_ATTR_URL);
13739 }
13740 }
13741 else
13742 {
13743 currentStyle.SetURL(attr.GetURL());
13744 }
13745 }
c4168888
JS
13746 else if (!attr.HasURL() && currentStyle.HasURL())
13747 {
13748 clashingAttr.AddFlag(wxTEXT_ATTR_URL);
13749 currentStyle.RemoveFlag(wxTEXT_ATTR_URL);
13750 }
24777478
JS
13751
13752 if (attr.HasTextEffects() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_EFFECTS))
13753 {
13754 if (currentStyle.HasTextEffects())
13755 {
13756 // We need to find the bits in the new attr that are different:
13757 // just look at those bits that are specified by the new attr.
13758
13759 // We need to remove the bits and flags that are not common between current attr
13760 // and new attr. In so doing we need to take account of the styles absent from one or more of the
13761 // previous styles.
13762
13763 int currentRelevantTextEffects = currentStyle.GetTextEffects() & attr.GetTextEffectFlags();
13764 int newRelevantTextEffects = attr.GetTextEffects() & attr.GetTextEffectFlags();
13765
13766 if (currentRelevantTextEffects != newRelevantTextEffects)
13767 {
13768 // Find the text effects that were different, using XOR
13769 int differentEffects = currentRelevantTextEffects ^ newRelevantTextEffects;
13770
13771 // Clash of attr - mark as such
13772 clashingAttr.SetTextEffectFlags(clashingAttr.GetTextEffectFlags() | differentEffects);
13773 currentStyle.SetTextEffectFlags(currentStyle.GetTextEffectFlags() & ~differentEffects);
13774 }
13775 }
13776 else
13777 {
13778 currentStyle.SetTextEffects(attr.GetTextEffects());
13779 currentStyle.SetTextEffectFlags(attr.GetTextEffectFlags());
13780 }
13781
13782 // Mask out the flags and values that cannot be common because they were absent in one or more objecrs
13783 // that we've looked at so far
13784 currentStyle.SetTextEffects(currentStyle.GetTextEffects() & ~absentAttr.GetTextEffectFlags());
13785 currentStyle.SetTextEffectFlags(currentStyle.GetTextEffectFlags() & ~absentAttr.GetTextEffectFlags());
13786
13787 if (currentStyle.GetTextEffectFlags() == 0)
13788 currentStyle.RemoveFlag(wxTEXT_ATTR_EFFECTS);
13789 }
c4168888
JS
13790 else if (!attr.HasTextEffects() && currentStyle.HasTextEffects())
13791 {
13792 clashingAttr.AddFlag(wxTEXT_ATTR_EFFECTS);
13793 currentStyle.RemoveFlag(wxTEXT_ATTR_EFFECTS);
13794 }
24777478
JS
13795
13796 if (attr.HasOutlineLevel() && !wxHasStyle(forbiddenFlags, wxTEXT_ATTR_OUTLINE_LEVEL))
13797 {
13798 if (currentStyle.HasOutlineLevel())
13799 {
13800 if (currentStyle.GetOutlineLevel() != attr.GetOutlineLevel())
13801 {
13802 // Clash of attr - mark as such
13803 clashingAttr.AddFlag(wxTEXT_ATTR_OUTLINE_LEVEL);
13804 currentStyle.RemoveFlag(wxTEXT_ATTR_OUTLINE_LEVEL);
13805 }
13806 }
13807 else
13808 currentStyle.SetOutlineLevel(attr.GetOutlineLevel());
13809 }
c4168888
JS
13810 else if (!attr.HasOutlineLevel() && currentStyle.HasOutlineLevel())
13811 {
13812 clashingAttr.AddFlag(wxTEXT_ATTR_OUTLINE_LEVEL);
13813 currentStyle.RemoveFlag(wxTEXT_ATTR_OUTLINE_LEVEL);
13814 }
24777478
JS
13815}
13816
bec80f4f
JS
13817WX_DEFINE_OBJARRAY(wxRichTextVariantArray);
13818
f7667b84
JS
13819// JACS 2013-01-27
13820WX_DEFINE_OBJARRAY(wxRichTextAttrArray);
13821
bec80f4f
JS
13822IMPLEMENT_DYNAMIC_CLASS(wxRichTextProperties, wxObject)
13823
13824bool wxRichTextProperties::operator==(const wxRichTextProperties& props) const
13825{
13826 if (m_properties.GetCount() != props.GetCount())
13827 return false;
13828
13829 size_t i;
13830 for (i = 0; i < m_properties.GetCount(); i++)
13831 {
13832 const wxVariant& var1 = m_properties[i];
13833 int idx = props.Find(var1.GetName());
13834 if (idx == -1)
13835 return false;
13836 const wxVariant& var2 = props.m_properties[idx];
13837 if (!(var1 == var2))
13838 return false;
13839 }
13840
13841 return true;
13842}
13843
13844wxArrayString wxRichTextProperties::GetPropertyNames() const
13845{
13846 wxArrayString arr;
13847 size_t i;
13848 for (i = 0; i < m_properties.GetCount(); i++)
13849 {
13850 arr.Add(m_properties[i].GetName());
13851 }
13852 return arr;
13853}
13854
13855int wxRichTextProperties::Find(const wxString& name) const
13856{
13857 size_t i;
13858 for (i = 0; i < m_properties.GetCount(); i++)
13859 {
13860 if (m_properties[i].GetName() == name)
13861 return (int) i;
13862 }
13863 return -1;
13864}
13865
590a0f8b
JS
13866bool wxRichTextProperties::Remove(const wxString& name)
13867{
13868 int idx = Find(name);
13869 if (idx != -1)
13870 {
13871 m_properties.RemoveAt(idx);
13872 return true;
13873 }
13874 else
13875 return false;
13876}
13877
bec80f4f
JS
13878wxVariant* wxRichTextProperties::FindOrCreateProperty(const wxString& name)
13879{
13880 int idx = Find(name);
13881 if (idx == wxNOT_FOUND)
13882 SetProperty(name, wxString());
13883 idx = Find(name);
13884 if (idx != wxNOT_FOUND)
13885 {
13886 return & (*this)[idx];
13887 }
13888 else
13889 return NULL;
13890}
13891
13892const wxVariant& wxRichTextProperties::GetProperty(const wxString& name) const
13893{
13894 static const wxVariant nullVariant;
13895 int idx = Find(name);
13896 if (idx != -1)
13897 return m_properties[idx];
13898 else
13899 return nullVariant;
13900}
13901
13902wxString wxRichTextProperties::GetPropertyString(const wxString& name) const
13903{
13904 return GetProperty(name).GetString();
13905}
13906
13907long wxRichTextProperties::GetPropertyLong(const wxString& name) const
13908{
13909 return GetProperty(name).GetLong();
13910}
13911
13912bool wxRichTextProperties::GetPropertyBool(const wxString& name) const
13913{
13914 return GetProperty(name).GetBool();
13915}
13916
13917double wxRichTextProperties::GetPropertyDouble(const wxString& name) const
13918{
13919 return GetProperty(name).GetDouble();
13920}
13921
13922void wxRichTextProperties::SetProperty(const wxVariant& variant)
13923{
13924 wxASSERT(!variant.GetName().IsEmpty());
13925
13926 int idx = Find(variant.GetName());
13927
13928 if (idx == -1)
13929 m_properties.Add(variant);
13930 else
13931 m_properties[idx] = variant;
13932}
13933
13934void wxRichTextProperties::SetProperty(const wxString& name, const wxVariant& variant)
13935{
13936 int idx = Find(name);
13937 wxVariant var(variant);
13938 var.SetName(name);
13939
13940 if (idx == -1)
13941 m_properties.Add(var);
13942 else
13943 m_properties[idx] = var;
13944}
13945
13946void wxRichTextProperties::SetProperty(const wxString& name, const wxString& value)
13947{
13948 SetProperty(name, wxVariant(value, name));
13949}
13950
13951void wxRichTextProperties::SetProperty(const wxString& name, long value)
13952{
13953 SetProperty(name, wxVariant(value, name));
13954}
13955
13956void wxRichTextProperties::SetProperty(const wxString& name, double value)
13957{
13958 SetProperty(name, wxVariant(value, name));
13959}
13960
13961void wxRichTextProperties::SetProperty(const wxString& name, bool value)
13962{
13963 SetProperty(name, wxVariant(value, name));
13964}
24777478 13965
590a0f8b
JS
13966void wxRichTextProperties::RemoveProperties(const wxRichTextProperties& properties)
13967{
13968 size_t i;
13969 for (i = 0; i < properties.GetCount(); i++)
13970 {
13971 wxString name = properties.GetProperties()[i].GetName();
13972 if (HasProperty(name))
13973 Remove(name);
13974 }
13975}
13976
13977void wxRichTextProperties::MergeProperties(const wxRichTextProperties& properties)
13978{
13979 size_t i;
13980 for (i = 0; i < properties.GetCount(); i++)
13981 {
13982 SetProperty(properties.GetProperties()[i]);
13983 }
13984}
13985
603f702b
JS
13986wxRichTextObject* wxRichTextObjectAddress::GetObject(wxRichTextParagraphLayoutBox* topLevelContainer) const
13987{
13988 if (m_address.GetCount() == 0)
13989 return topLevelContainer;
13990
13991 wxRichTextCompositeObject* p = topLevelContainer;
13992 size_t i = 0;
13993 while (p && i < m_address.GetCount())
13994 {
13995 int pos = m_address[i];
13996 wxASSERT(pos >= 0 && pos < (int) p->GetChildren().GetCount());
13997 if (pos < 0 || pos >= (int) p->GetChildren().GetCount())
13998 return NULL;
13999
14000 wxRichTextObject* p1 = p->GetChild(pos);
14001 if (i == (m_address.GetCount()-1))
14002 return p1;
14003
14004 p = wxDynamicCast(p1, wxRichTextCompositeObject);
14005 i ++;
14006 }
14007 return NULL;
14008}
14009
14010bool wxRichTextObjectAddress::Create(wxRichTextParagraphLayoutBox* topLevelContainer, wxRichTextObject* obj)
14011{
14012 m_address.Clear();
14013
14014 if (topLevelContainer == obj)
14015 return true;
14016
14017 wxRichTextObject* o = obj;
14018 while (o)
14019 {
14020 wxRichTextCompositeObject* p = wxDynamicCast(o->GetParent(), wxRichTextCompositeObject);
14021 if (!p)
14022 return false;
14023
14024 int pos = p->GetChildren().IndexOf(o);
14025 if (pos == -1)
14026 return false;
14027
14028 m_address.Insert(pos, 0);
14029
14030 if (p == topLevelContainer)
14031 return true;
14032
14033 o = p;
14034 }
14035 return false;
14036}
14037
14038// Equality test
14039bool wxRichTextSelection::operator==(const wxRichTextSelection& sel) const
14040{
14041 if (m_container != sel.m_container)
14042 return false;
14043 if (m_ranges.GetCount() != sel.m_ranges.GetCount())
14044 return false;
14045 size_t i;
14046 for (i = 0; i < m_ranges.GetCount(); i++)
14047 if (!(m_ranges[i] == sel.m_ranges[i]))
14048 return false;
14049 return true;
14050}
14051
14052// Get the selections appropriate to the specified object, if any; returns wxRICHTEXT_NO_SELECTION if none
14053// or none at the level of the object's container.
14054wxRichTextRangeArray wxRichTextSelection::GetSelectionForObject(wxRichTextObject* obj) const
14055{
14056 if (IsValid())
14057 {
14058 wxRichTextParagraphLayoutBox* container = obj->GetParentContainer();
14059
14060 if (container == m_container)
14061 return m_ranges;
14062
14063 container = obj->GetContainer();
14064 while (container)
14065 {
14066 if (container->GetParent())
14067 {
14068 // If we found that our object's container is within the range of
14069 // a selection higher up, then assume the whole original object
14070 // is also selected.
14071 wxRichTextParagraphLayoutBox* parentContainer = container->GetParentContainer();
14072 if (parentContainer == m_container)
14073 {
14074 if (WithinSelection(container->GetRange().GetStart(), m_ranges))
14075 {
14076 wxRichTextRangeArray ranges;
14077 ranges.Add(obj->GetRange());
14078 return ranges;
14079 }
14080 }
14081
14082 container = parentContainer;
14083 }
14084 else
14085 {
14086 container = NULL;
14087 break;
14088 }
14089 }
14090 }
14091 return wxRichTextRangeArray();
14092}
14093
14094// Is the given position within the selection?
14095bool wxRichTextSelection::WithinSelection(long pos, wxRichTextObject* obj) const
14096{
14097 if (!IsValid())
14098 return false;
14099 else
14100 {
14101 wxRichTextRangeArray selectionRanges = GetSelectionForObject(obj);
14102 return WithinSelection(pos, selectionRanges);
14103 }
14104}
14105
14106// Is the given position within the selection range?
14107bool wxRichTextSelection::WithinSelection(long pos, const wxRichTextRangeArray& ranges)
14108{
14109 size_t i;
14110 for (i = 0; i < ranges.GetCount(); i++)
14111 {
14112 const wxRichTextRange& range = ranges[i];
14113 if (pos >= range.GetStart() && pos <= range.GetEnd())
14114 return true;
14115 }
14116 return false;
14117}
14118
14119// Is the given range completely within the selection range?
14120bool wxRichTextSelection::WithinSelection(const wxRichTextRange& range, const wxRichTextRangeArray& ranges)
14121{
14122 size_t i;
14123 for (i = 0; i < ranges.GetCount(); i++)
14124 {
14125 const wxRichTextRange& eachRange = ranges[i];
14126 if (range.IsWithin(eachRange))
14127 return true;
14128 }
14129 return false;
14130}
14131
8db2e3ef
JS
14132IMPLEMENT_CLASS(wxRichTextDrawingHandler, wxObject)
14133IMPLEMENT_CLASS(wxRichTextDrawingContext, wxObject)
14134
f7667b84
JS
14135wxRichTextDrawingContext::wxRichTextDrawingContext(wxRichTextBuffer* buffer)
14136{
14137 Init();
14138 m_buffer = buffer;
14139 if (m_buffer && m_buffer->GetRichTextCtrl())
14140 EnableVirtualAttributes(m_buffer->GetRichTextCtrl()->GetVirtualAttributesEnabled());
14141}
14142
8db2e3ef
JS
14143bool wxRichTextDrawingContext::HasVirtualAttributes(wxRichTextObject* obj) const
14144{
f7667b84
JS
14145 if (!GetVirtualAttributesEnabled())
14146 return false;
14147
8db2e3ef
JS
14148 wxList::compatibility_iterator node = m_buffer->GetDrawingHandlers().GetFirst();
14149 while (node)
14150 {
14151 wxRichTextDrawingHandler *handler = (wxRichTextDrawingHandler*)node->GetData();
14152 if (handler->HasVirtualAttributes(obj))
14153 return true;
14154
14155 node = node->GetNext();
14156 }
14157 return false;
14158}
14159
14160wxRichTextAttr wxRichTextDrawingContext::GetVirtualAttributes(wxRichTextObject* obj) const
14161{
14162 wxRichTextAttr attr;
f7667b84
JS
14163 if (!GetVirtualAttributesEnabled())
14164 return attr;
14165
8db2e3ef
JS
14166 // We apply all handlers, so we can may combine several different attributes
14167 wxList::compatibility_iterator node = m_buffer->GetDrawingHandlers().GetFirst();
14168 while (node)
14169 {
14170 wxRichTextDrawingHandler *handler = (wxRichTextDrawingHandler*)node->GetData();
14171 if (handler->HasVirtualAttributes(obj))
14172 {
14173 bool success = handler->GetVirtualAttributes(attr, obj);
14174 wxASSERT(success);
aa8f57f4 14175 wxUnusedVar(success);
8db2e3ef
JS
14176 }
14177
14178 node = node->GetNext();
14179 }
14180 return attr;
14181}
14182
14183bool wxRichTextDrawingContext::ApplyVirtualAttributes(wxRichTextAttr& attr, wxRichTextObject* obj) const
14184{
f7667b84
JS
14185 if (!GetVirtualAttributesEnabled())
14186 return false;
14187
8db2e3ef
JS
14188 if (HasVirtualAttributes(obj))
14189 {
14190 wxRichTextAttr a(GetVirtualAttributes(obj));
14191 attr.Apply(a);
14192 return true;
14193 }
14194 else
14195 return false;
14196}
14197
f7667b84
JS
14198int wxRichTextDrawingContext::GetVirtualSubobjectAttributesCount(wxRichTextObject* obj) const
14199{
14200 if (!GetVirtualAttributesEnabled())
14201 return 0;
14202
14203 wxList::compatibility_iterator node = m_buffer->GetDrawingHandlers().GetFirst();
14204 while (node)
14205 {
14206 wxRichTextDrawingHandler *handler = (wxRichTextDrawingHandler*)node->GetData();
14207 int count = handler->GetVirtualSubobjectAttributesCount(obj);
14208 if (count > 0)
14209 return count;
14210
14211 node = node->GetNext();
14212 }
14213 return 0;
14214}
14215
14216int wxRichTextDrawingContext::GetVirtualSubobjectAttributes(wxRichTextObject* obj, wxArrayInt& positions, wxRichTextAttrArray& attributes) const
14217{
14218 if (!GetVirtualAttributesEnabled())
14219 return 0;
14220
14221 wxList::compatibility_iterator node = m_buffer->GetDrawingHandlers().GetFirst();
14222 while (node)
14223 {
14224 wxRichTextDrawingHandler *handler = (wxRichTextDrawingHandler*)node->GetData();
14225 if (handler->GetVirtualSubobjectAttributes(obj, positions, attributes))
14226 return positions.GetCount();
14227
14228 node = node->GetNext();
14229 }
14230 return 0;
14231}
14232
14233bool wxRichTextDrawingContext::HasVirtualText(const wxRichTextPlainText* obj) const
14234{
14235 if (!GetVirtualAttributesEnabled())
14236 return false;
14237
14238 wxList::compatibility_iterator node = m_buffer->GetDrawingHandlers().GetFirst();
14239 while (node)
14240 {
14241 wxRichTextDrawingHandler *handler = (wxRichTextDrawingHandler*)node->GetData();
14242 if (handler->HasVirtualText(obj))
14243 return true;
14244
14245 node = node->GetNext();
14246 }
14247 return false;
14248}
14249
14250bool wxRichTextDrawingContext::GetVirtualText(const wxRichTextPlainText* obj, wxString& text) const
14251{
14252 if (!GetVirtualAttributesEnabled())
14253 return false;
14254
14255 wxList::compatibility_iterator node = m_buffer->GetDrawingHandlers().GetFirst();
14256 while (node)
14257 {
14258 wxRichTextDrawingHandler *handler = (wxRichTextDrawingHandler*)node->GetData();
14259 if (handler->GetVirtualText(obj, text))
14260 return true;
14261
14262 node = node->GetNext();
14263 }
14264 return false;
14265}
14266
8db2e3ef
JS
14267/// Adds a handler to the end
14268void wxRichTextBuffer::AddDrawingHandler(wxRichTextDrawingHandler *handler)
14269{
14270 sm_drawingHandlers.Append(handler);
14271}
14272
14273/// Inserts a handler at the front
14274void wxRichTextBuffer::InsertDrawingHandler(wxRichTextDrawingHandler *handler)
14275{
14276 sm_drawingHandlers.Insert( handler );
14277}
14278
14279/// Removes a handler
14280bool wxRichTextBuffer::RemoveDrawingHandler(const wxString& name)
14281{
14282 wxRichTextDrawingHandler *handler = FindDrawingHandler(name);
14283 if (handler)
14284 {
14285 sm_drawingHandlers.DeleteObject(handler);
14286 delete handler;
14287 return true;
14288 }
14289 else
14290 return false;
14291}
14292
14293wxRichTextDrawingHandler* wxRichTextBuffer::FindDrawingHandler(const wxString& name)
14294{
14295 wxList::compatibility_iterator node = sm_drawingHandlers.GetFirst();
14296 while (node)
14297 {
14298 wxRichTextDrawingHandler *handler = (wxRichTextDrawingHandler*)node->GetData();
14299 if (handler->GetName().Lower() == name.Lower()) return handler;
14300
14301 node = node->GetNext();
14302 }
14303 return NULL;
14304}
14305
14306void wxRichTextBuffer::CleanUpDrawingHandlers()
14307{
14308 wxList::compatibility_iterator node = sm_drawingHandlers.GetFirst();
14309 while (node)
14310 {
14311 wxRichTextDrawingHandler* handler = (wxRichTextDrawingHandler*)node->GetData();
14312 wxList::compatibility_iterator next = node->GetNext();
14313 delete handler;
14314 node = next;
14315 }
14316
14317 sm_drawingHandlers.Clear();
14318}
603f702b 14319
7c9fdebe
JS
14320void wxRichTextBuffer::AddFieldType(wxRichTextFieldType *fieldType)
14321{
14322 sm_fieldTypes[fieldType->GetName()] = fieldType;
14323}
14324
14325bool wxRichTextBuffer::RemoveFieldType(const wxString& name)
14326{
14327 wxRichTextFieldTypeHashMap::iterator it = sm_fieldTypes.find(name);
14328 if (it == sm_fieldTypes.end())
14329 return false;
14330 else
14331 {
14332 wxRichTextFieldType* fieldType = it->second;
14333 sm_fieldTypes.erase(it);
14334 delete fieldType;
14335 return true;
14336 }
14337}
14338
14339wxRichTextFieldType *wxRichTextBuffer::FindFieldType(const wxString& name)
14340{
14341 wxRichTextFieldTypeHashMap::iterator it = sm_fieldTypes.find(name);
14342 if (it == sm_fieldTypes.end())
14343 return NULL;
14344 else
14345 return it->second;
14346}
14347
14348void wxRichTextBuffer::CleanUpFieldTypes()
14349{
14350 wxRichTextFieldTypeHashMap::iterator it;
14351 for( it = sm_fieldTypes.begin(); it != sm_fieldTypes.end(); ++it )
14352 {
14353 wxRichTextFieldType* fieldType = it->second;
14354 delete fieldType;
14355 }
14356
14357 sm_fieldTypes.clear();
14358}
14359
5d7836c4
JS
14360#endif
14361 // wxUSE_RICHTEXT