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