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