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