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