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