| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: 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/wx.h" |
| 25 | #endif |
| 26 | |
| 27 | #include "wx/filename.h" |
| 28 | #include "wx/clipbrd.h" |
| 29 | #include "wx/dataobj.h" |
| 30 | #include "wx/wfstream.h" |
| 31 | #include "wx/module.h" |
| 32 | #include "wx/mstream.h" |
| 33 | #include "wx/sstream.h" |
| 34 | |
| 35 | #include "wx/richtext/richtextctrl.h" |
| 36 | #include "wx/richtext/richtextstyles.h" |
| 37 | |
| 38 | #include "wx/listimpl.cpp" |
| 39 | |
| 40 | WX_DEFINE_LIST(wxRichTextObjectList) |
| 41 | WX_DEFINE_LIST(wxRichTextLineList) |
| 42 | |
| 43 | /*! |
| 44 | * wxRichTextObject |
| 45 | * This is the base for drawable objects. |
| 46 | */ |
| 47 | |
| 48 | IMPLEMENT_CLASS(wxRichTextObject, wxObject) |
| 49 | |
| 50 | wxRichTextObject::wxRichTextObject(wxRichTextObject* parent) |
| 51 | { |
| 52 | m_dirty = false; |
| 53 | m_refCount = 1; |
| 54 | m_parent = parent; |
| 55 | m_leftMargin = 0; |
| 56 | m_rightMargin = 0; |
| 57 | m_topMargin = 0; |
| 58 | m_bottomMargin = 0; |
| 59 | m_descent = 0; |
| 60 | } |
| 61 | |
| 62 | wxRichTextObject::~wxRichTextObject() |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | void wxRichTextObject::Dereference() |
| 67 | { |
| 68 | m_refCount --; |
| 69 | if (m_refCount <= 0) |
| 70 | delete this; |
| 71 | } |
| 72 | |
| 73 | /// Copy |
| 74 | void wxRichTextObject::Copy(const wxRichTextObject& obj) |
| 75 | { |
| 76 | m_size = obj.m_size; |
| 77 | m_pos = obj.m_pos; |
| 78 | m_dirty = obj.m_dirty; |
| 79 | m_range = obj.m_range; |
| 80 | m_attributes = obj.m_attributes; |
| 81 | m_descent = obj.m_descent; |
| 82 | |
| 83 | if (!m_attributes.GetFont().Ok()) |
| 84 | wxLogDebug(wxT("No font!")); |
| 85 | if (!obj.m_attributes.GetFont().Ok()) |
| 86 | wxLogDebug(wxT("Parent has no font!")); |
| 87 | } |
| 88 | |
| 89 | void wxRichTextObject::SetMargins(int margin) |
| 90 | { |
| 91 | m_leftMargin = m_rightMargin = m_topMargin = m_bottomMargin = margin; |
| 92 | } |
| 93 | |
| 94 | void wxRichTextObject::SetMargins(int leftMargin, int rightMargin, int topMargin, int bottomMargin) |
| 95 | { |
| 96 | m_leftMargin = leftMargin; |
| 97 | m_rightMargin = rightMargin; |
| 98 | m_topMargin = topMargin; |
| 99 | m_bottomMargin = bottomMargin; |
| 100 | } |
| 101 | |
| 102 | // Convert units in tends of a millimetre to device units |
| 103 | int wxRichTextObject::ConvertTenthsMMToPixels(wxDC& dc, int units) |
| 104 | { |
| 105 | int ppi = dc.GetPPI().x; |
| 106 | |
| 107 | // There are ppi pixels in 254.1 "1/10 mm" |
| 108 | |
| 109 | double pixels = ((double) units * (double)ppi) / 254.1; |
| 110 | |
| 111 | return (int) pixels; |
| 112 | } |
| 113 | |
| 114 | /// Dump to output stream for debugging |
| 115 | void wxRichTextObject::Dump(wxTextOutputStream& stream) |
| 116 | { |
| 117 | stream << GetClassInfo()->GetClassName() << wxT("\n"); |
| 118 | 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"); |
| 119 | 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"); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /*! |
| 124 | * wxRichTextCompositeObject |
| 125 | * This is the base for drawable objects. |
| 126 | */ |
| 127 | |
| 128 | IMPLEMENT_CLASS(wxRichTextCompositeObject, wxRichTextObject) |
| 129 | |
| 130 | wxRichTextCompositeObject::wxRichTextCompositeObject(wxRichTextObject* parent): |
| 131 | wxRichTextObject(parent) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | wxRichTextCompositeObject::~wxRichTextCompositeObject() |
| 136 | { |
| 137 | DeleteChildren(); |
| 138 | } |
| 139 | |
| 140 | /// Get the nth child |
| 141 | wxRichTextObject* wxRichTextCompositeObject::GetChild(size_t n) const |
| 142 | { |
| 143 | wxASSERT ( n < m_children.GetCount() ); |
| 144 | |
| 145 | return m_children.Item(n)->GetData(); |
| 146 | } |
| 147 | |
| 148 | /// Append a child, returning the position |
| 149 | size_t wxRichTextCompositeObject::AppendChild(wxRichTextObject* child) |
| 150 | { |
| 151 | m_children.Append(child); |
| 152 | child->SetParent(this); |
| 153 | return m_children.GetCount() - 1; |
| 154 | } |
| 155 | |
| 156 | /// Insert the child in front of the given object, or at the beginning |
| 157 | bool wxRichTextCompositeObject::InsertChild(wxRichTextObject* child, wxRichTextObject* inFrontOf) |
| 158 | { |
| 159 | if (inFrontOf) |
| 160 | { |
| 161 | wxRichTextObjectList::compatibility_iterator node = m_children.Find(inFrontOf); |
| 162 | m_children.Insert(node, child); |
| 163 | } |
| 164 | else |
| 165 | m_children.Insert(child); |
| 166 | child->SetParent(this); |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | /// Delete the child |
| 172 | bool wxRichTextCompositeObject::RemoveChild(wxRichTextObject* child, bool deleteChild) |
| 173 | { |
| 174 | wxRichTextObjectList::compatibility_iterator node = m_children.Find(child); |
| 175 | if (node) |
| 176 | { |
| 177 | wxRichTextObject* obj = node->GetData(); |
| 178 | m_children.Erase(node); |
| 179 | if (deleteChild) |
| 180 | delete obj; |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | /// Delete all children |
| 188 | bool wxRichTextCompositeObject::DeleteChildren() |
| 189 | { |
| 190 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 191 | while (node) |
| 192 | { |
| 193 | wxRichTextObjectList::compatibility_iterator oldNode = node; |
| 194 | |
| 195 | wxRichTextObject* child = node->GetData(); |
| 196 | child->Dereference(); // Only delete if reference count is zero |
| 197 | |
| 198 | node = node->GetNext(); |
| 199 | m_children.Erase(oldNode); |
| 200 | } |
| 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | /// Get the child count |
| 206 | size_t wxRichTextCompositeObject::GetChildCount() const |
| 207 | { |
| 208 | return m_children.GetCount(); |
| 209 | } |
| 210 | |
| 211 | /// Copy |
| 212 | void wxRichTextCompositeObject::Copy(const wxRichTextCompositeObject& obj) |
| 213 | { |
| 214 | wxRichTextObject::Copy(obj); |
| 215 | |
| 216 | DeleteChildren(); |
| 217 | |
| 218 | wxRichTextObjectList::compatibility_iterator node = obj.m_children.GetFirst(); |
| 219 | while (node) |
| 220 | { |
| 221 | wxRichTextObject* child = node->GetData(); |
| 222 | m_children.Append(child->Clone()); |
| 223 | |
| 224 | node = node->GetNext(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /// Hit-testing: returns a flag indicating hit test details, plus |
| 229 | /// information about position |
| 230 | int wxRichTextCompositeObject::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition) |
| 231 | { |
| 232 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 233 | while (node) |
| 234 | { |
| 235 | wxRichTextObject* child = node->GetData(); |
| 236 | |
| 237 | int ret = child->HitTest(dc, pt, textPosition); |
| 238 | if (ret != wxRICHTEXT_HITTEST_NONE) |
| 239 | return ret; |
| 240 | |
| 241 | node = node->GetNext(); |
| 242 | } |
| 243 | |
| 244 | return wxRICHTEXT_HITTEST_NONE; |
| 245 | } |
| 246 | |
| 247 | /// Finds the absolute position and row height for the given character position |
| 248 | bool wxRichTextCompositeObject::FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart) |
| 249 | { |
| 250 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 251 | while (node) |
| 252 | { |
| 253 | wxRichTextObject* child = node->GetData(); |
| 254 | |
| 255 | if (child->FindPosition(dc, index, pt, height, forceLineStart)) |
| 256 | return true; |
| 257 | |
| 258 | node = node->GetNext(); |
| 259 | } |
| 260 | |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | /// Calculate range |
| 265 | void wxRichTextCompositeObject::CalculateRange(long start, long& end) |
| 266 | { |
| 267 | long current = start; |
| 268 | long lastEnd = current; |
| 269 | |
| 270 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 271 | while (node) |
| 272 | { |
| 273 | wxRichTextObject* child = node->GetData(); |
| 274 | long childEnd = 0; |
| 275 | |
| 276 | child->CalculateRange(current, childEnd); |
| 277 | lastEnd = childEnd; |
| 278 | |
| 279 | current = childEnd + 1; |
| 280 | |
| 281 | node = node->GetNext(); |
| 282 | } |
| 283 | |
| 284 | end = lastEnd; |
| 285 | |
| 286 | // An object with no children has zero length |
| 287 | if (m_children.GetCount() == 0) |
| 288 | end --; |
| 289 | |
| 290 | m_range.SetRange(start, end); |
| 291 | } |
| 292 | |
| 293 | /// Delete range from layout. |
| 294 | bool wxRichTextCompositeObject::DeleteRange(const wxRichTextRange& range) |
| 295 | { |
| 296 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 297 | |
| 298 | while (node) |
| 299 | { |
| 300 | wxRichTextObject* obj = (wxRichTextObject*) node->GetData(); |
| 301 | wxRichTextObjectList::compatibility_iterator next = node->GetNext(); |
| 302 | |
| 303 | // Delete the range in each paragraph |
| 304 | |
| 305 | // When a chunk has been deleted, internally the content does not |
| 306 | // now match the ranges. |
| 307 | // However, so long as deletion is not done on the same object twice this is OK. |
| 308 | // If you may delete content from the same object twice, recalculate |
| 309 | // the ranges inbetween DeleteRange calls by calling CalculateRanges, and |
| 310 | // adjust the range you're deleting accordingly. |
| 311 | |
| 312 | if (!obj->GetRange().IsOutside(range)) |
| 313 | { |
| 314 | obj->DeleteRange(range); |
| 315 | |
| 316 | // Delete an empty object, or paragraph within this range. |
| 317 | if (obj->IsEmpty() || |
| 318 | (range.GetStart() <= obj->GetRange().GetStart() && range.GetEnd() >= obj->GetRange().GetEnd())) |
| 319 | { |
| 320 | // An empty paragraph has length 1, so won't be deleted unless the |
| 321 | // whole range is deleted. |
| 322 | RemoveChild(obj, true); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | node = next; |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | /// Get any text in this object for the given range |
| 333 | wxString wxRichTextCompositeObject::GetTextForRange(const wxRichTextRange& range) const |
| 334 | { |
| 335 | wxString text; |
| 336 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 337 | while (node) |
| 338 | { |
| 339 | wxRichTextObject* child = node->GetData(); |
| 340 | wxRichTextRange childRange = range; |
| 341 | if (!child->GetRange().IsOutside(range)) |
| 342 | { |
| 343 | childRange.LimitTo(child->GetRange()); |
| 344 | |
| 345 | wxString childText = child->GetTextForRange(childRange); |
| 346 | |
| 347 | text += childText; |
| 348 | } |
| 349 | node = node->GetNext(); |
| 350 | } |
| 351 | |
| 352 | return text; |
| 353 | } |
| 354 | |
| 355 | /// Recursively merge all pieces that can be merged. |
| 356 | bool wxRichTextCompositeObject::Defragment() |
| 357 | { |
| 358 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 359 | while (node) |
| 360 | { |
| 361 | wxRichTextObject* child = node->GetData(); |
| 362 | wxRichTextCompositeObject* composite = wxDynamicCast(child, wxRichTextCompositeObject); |
| 363 | if (composite) |
| 364 | composite->Defragment(); |
| 365 | |
| 366 | if (node->GetNext()) |
| 367 | { |
| 368 | wxRichTextObject* nextChild = node->GetNext()->GetData(); |
| 369 | if (child->CanMerge(nextChild) && child->Merge(nextChild)) |
| 370 | { |
| 371 | nextChild->Dereference(); |
| 372 | m_children.Erase(node->GetNext()); |
| 373 | |
| 374 | // Don't set node -- we'll see if we can merge again with the next |
| 375 | // child. |
| 376 | } |
| 377 | else |
| 378 | node = node->GetNext(); |
| 379 | } |
| 380 | else |
| 381 | node = node->GetNext(); |
| 382 | } |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | /// Dump to output stream for debugging |
| 388 | void wxRichTextCompositeObject::Dump(wxTextOutputStream& stream) |
| 389 | { |
| 390 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 391 | while (node) |
| 392 | { |
| 393 | wxRichTextObject* child = node->GetData(); |
| 394 | child->Dump(stream); |
| 395 | node = node->GetNext(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /*! |
| 401 | * wxRichTextBox |
| 402 | * This defines a 2D space to lay out objects |
| 403 | */ |
| 404 | |
| 405 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextBox, wxRichTextCompositeObject) |
| 406 | |
| 407 | wxRichTextBox::wxRichTextBox(wxRichTextObject* parent): |
| 408 | wxRichTextCompositeObject(parent) |
| 409 | { |
| 410 | } |
| 411 | |
| 412 | /// Draw the item |
| 413 | bool wxRichTextBox::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& WXUNUSED(rect), int descent, int style) |
| 414 | { |
| 415 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 416 | while (node) |
| 417 | { |
| 418 | wxRichTextObject* child = node->GetData(); |
| 419 | |
| 420 | wxRect childRect = wxRect(child->GetPosition(), child->GetCachedSize()); |
| 421 | child->Draw(dc, range, selectionRange, childRect, descent, style); |
| 422 | |
| 423 | node = node->GetNext(); |
| 424 | } |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | /// Lay the item out |
| 429 | bool wxRichTextBox::Layout(wxDC& dc, const wxRect& rect, int style) |
| 430 | { |
| 431 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 432 | while (node) |
| 433 | { |
| 434 | wxRichTextObject* child = node->GetData(); |
| 435 | child->Layout(dc, rect, style); |
| 436 | |
| 437 | node = node->GetNext(); |
| 438 | } |
| 439 | m_dirty = false; |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | /// Get/set the size for the given range. Assume only has one child. |
| 444 | bool wxRichTextBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags) const |
| 445 | { |
| 446 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 447 | if (node) |
| 448 | { |
| 449 | wxRichTextObject* child = node->GetData(); |
| 450 | return child->GetRangeSize(range, size, descent, dc, flags); |
| 451 | } |
| 452 | else |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | /// Copy |
| 457 | void wxRichTextBox::Copy(const wxRichTextBox& obj) |
| 458 | { |
| 459 | wxRichTextCompositeObject::Copy(obj); |
| 460 | } |
| 461 | |
| 462 | |
| 463 | /*! |
| 464 | * wxRichTextParagraphLayoutBox |
| 465 | * This box knows how to lay out paragraphs. |
| 466 | */ |
| 467 | |
| 468 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraphLayoutBox, wxRichTextBox) |
| 469 | |
| 470 | wxRichTextParagraphLayoutBox::wxRichTextParagraphLayoutBox(wxRichTextObject* parent): |
| 471 | wxRichTextBox(parent) |
| 472 | { |
| 473 | Init(); |
| 474 | } |
| 475 | |
| 476 | /// Initialize the object. |
| 477 | void wxRichTextParagraphLayoutBox::Init() |
| 478 | { |
| 479 | m_ctrl = NULL; |
| 480 | |
| 481 | // For now, assume is the only box and has no initial size. |
| 482 | m_range = wxRichTextRange(0, -1); |
| 483 | |
| 484 | m_invalidRange.SetRange(-1, -1); |
| 485 | m_leftMargin = 4; |
| 486 | m_rightMargin = 4; |
| 487 | m_topMargin = 4; |
| 488 | m_bottomMargin = 4; |
| 489 | } |
| 490 | |
| 491 | /// Draw the item |
| 492 | bool wxRichTextParagraphLayoutBox::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int style) |
| 493 | { |
| 494 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 495 | while (node) |
| 496 | { |
| 497 | wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 498 | wxASSERT (child != NULL); |
| 499 | |
| 500 | if (child && !child->GetRange().IsOutside(range)) |
| 501 | { |
| 502 | wxRect childRect(child->GetPosition(), child->GetCachedSize()); |
| 503 | |
| 504 | if (childRect.GetTop() > rect.GetBottom() || childRect.GetBottom() < rect.GetTop()) |
| 505 | { |
| 506 | // Skip |
| 507 | } |
| 508 | else |
| 509 | child->Draw(dc, child->GetRange(), selectionRange, childRect, descent, style); |
| 510 | } |
| 511 | |
| 512 | node = node->GetNext(); |
| 513 | } |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | /// Lay the item out |
| 518 | bool wxRichTextParagraphLayoutBox::Layout(wxDC& dc, const wxRect& rect, int style) |
| 519 | { |
| 520 | wxRect availableSpace; |
| 521 | bool formatRect = (style & wxRICHTEXT_LAYOUT_SPECIFIED_RECT) == wxRICHTEXT_LAYOUT_SPECIFIED_RECT; |
| 522 | |
| 523 | // If only laying out a specific area, the passed rect has a different meaning: |
| 524 | // the visible part of the buffer. |
| 525 | if (formatRect) |
| 526 | { |
| 527 | availableSpace = wxRect(0 + m_leftMargin, |
| 528 | 0 + m_topMargin, |
| 529 | rect.width - m_leftMargin - m_rightMargin, |
| 530 | rect.height); |
| 531 | |
| 532 | // Invalidate the part of the buffer from the first visible line |
| 533 | // to the end. If other parts of the buffer are currently invalid, |
| 534 | // then they too will be taken into account if they are above |
| 535 | // the visible point. |
| 536 | long startPos = 0; |
| 537 | wxRichTextLine* line = GetLineAtYPosition(rect.y); |
| 538 | if (line) |
| 539 | startPos = line->GetAbsoluteRange().GetStart(); |
| 540 | |
| 541 | Invalidate(wxRichTextRange(startPos, GetRange().GetEnd())); |
| 542 | } |
| 543 | else |
| 544 | availableSpace = wxRect(rect.x + m_leftMargin, |
| 545 | rect.y + m_topMargin, |
| 546 | rect.width - m_leftMargin - m_rightMargin, |
| 547 | rect.height - m_topMargin - m_bottomMargin); |
| 548 | |
| 549 | int maxWidth = 0; |
| 550 | |
| 551 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 552 | |
| 553 | bool layoutAll = true; |
| 554 | |
| 555 | // Get invalid range, rounding to paragraph start/end. |
| 556 | wxRichTextRange invalidRange = GetInvalidRange(true); |
| 557 | |
| 558 | if (invalidRange == wxRICHTEXT_NONE && !formatRect) |
| 559 | return true; |
| 560 | |
| 561 | if (invalidRange == wxRICHTEXT_ALL) |
| 562 | layoutAll = true; |
| 563 | else // If we know what range is affected, start laying out from that point on. |
| 564 | if (invalidRange.GetStart() > GetRange().GetStart()) |
| 565 | { |
| 566 | wxRichTextParagraph* firstParagraph = GetParagraphAtPosition(invalidRange.GetStart()); |
| 567 | if (firstParagraph) |
| 568 | { |
| 569 | wxRichTextObjectList::compatibility_iterator firstNode = m_children.Find(firstParagraph); |
| 570 | wxRichTextObjectList::compatibility_iterator previousNode = firstNode ? firstNode->GetPrevious() : wxRichTextObjectList::compatibility_iterator(); |
| 571 | if (firstNode && previousNode) |
| 572 | { |
| 573 | wxRichTextParagraph* previousParagraph = wxDynamicCast(previousNode->GetData(), wxRichTextParagraph); |
| 574 | availableSpace.y = previousParagraph->GetPosition().y + previousParagraph->GetCachedSize().y; |
| 575 | |
| 576 | // Now we're going to start iterating from the first affected paragraph. |
| 577 | node = firstNode; |
| 578 | |
| 579 | layoutAll = false; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // A way to force speedy rest-of-buffer layout (the 'else' below) |
| 585 | bool forceQuickLayout = false; |
| 586 | |
| 587 | while (node) |
| 588 | { |
| 589 | // Assume this box only contains paragraphs |
| 590 | |
| 591 | wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 592 | wxASSERT (child != NULL); |
| 593 | |
| 594 | // TODO: what if the child hasn't been laid out (e.g. involved in Undo) but still has 'old' lines |
| 595 | if (child && !forceQuickLayout && (layoutAll || child->GetLines().GetCount() == 0 || !child->GetRange().IsOutside(invalidRange))) |
| 596 | { |
| 597 | child->Layout(dc, availableSpace, style); |
| 598 | |
| 599 | // Layout must set the cached size |
| 600 | availableSpace.y += child->GetCachedSize().y; |
| 601 | maxWidth = wxMax(maxWidth, child->GetCachedSize().x); |
| 602 | |
| 603 | // If we're just formatting the visible part of the buffer, |
| 604 | // and we're now past the bottom of the window, start quick |
| 605 | // layout. |
| 606 | if (formatRect && child->GetPosition().y > rect.GetBottom()) |
| 607 | forceQuickLayout = true; |
| 608 | } |
| 609 | else |
| 610 | { |
| 611 | // We're outside the immediately affected range, so now let's just |
| 612 | // move everything up or down. This assumes that all the children have previously |
| 613 | // been laid out and have wrapped line lists associated with them. |
| 614 | // TODO: check all paragraphs before the affected range. |
| 615 | |
| 616 | int inc = availableSpace.y - child->GetPosition().y; |
| 617 | |
| 618 | while (node) |
| 619 | { |
| 620 | wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 621 | if (child) |
| 622 | { |
| 623 | if (child->GetLines().GetCount() == 0) |
| 624 | child->Layout(dc, availableSpace, style); |
| 625 | else |
| 626 | child->SetPosition(wxPoint(child->GetPosition().x, child->GetPosition().y + inc)); |
| 627 | |
| 628 | availableSpace.y += child->GetCachedSize().y; |
| 629 | maxWidth = wxMax(maxWidth, child->GetCachedSize().x); |
| 630 | } |
| 631 | |
| 632 | node = node->GetNext(); |
| 633 | } |
| 634 | break; |
| 635 | } |
| 636 | |
| 637 | node = node->GetNext(); |
| 638 | } |
| 639 | |
| 640 | SetCachedSize(wxSize(maxWidth, availableSpace.y)); |
| 641 | |
| 642 | m_dirty = false; |
| 643 | m_invalidRange = wxRICHTEXT_NONE; |
| 644 | |
| 645 | return true; |
| 646 | } |
| 647 | |
| 648 | /// Copy |
| 649 | void wxRichTextParagraphLayoutBox::Copy(const wxRichTextParagraphLayoutBox& obj) |
| 650 | { |
| 651 | wxRichTextBox::Copy(obj); |
| 652 | } |
| 653 | |
| 654 | /// Get/set the size for the given range. |
| 655 | bool wxRichTextParagraphLayoutBox::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags) const |
| 656 | { |
| 657 | wxSize sz; |
| 658 | |
| 659 | wxRichTextObjectList::compatibility_iterator startPara = wxRichTextObjectList::compatibility_iterator(); |
| 660 | wxRichTextObjectList::compatibility_iterator endPara = wxRichTextObjectList::compatibility_iterator(); |
| 661 | |
| 662 | // First find the first paragraph whose starting position is within the range. |
| 663 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 664 | while (node) |
| 665 | { |
| 666 | // child is a paragraph |
| 667 | wxRichTextObject* child = node->GetData(); |
| 668 | const wxRichTextRange& r = child->GetRange(); |
| 669 | |
| 670 | if (r.GetStart() <= range.GetStart() && r.GetEnd() >= range.GetStart()) |
| 671 | { |
| 672 | startPara = node; |
| 673 | break; |
| 674 | } |
| 675 | |
| 676 | node = node->GetNext(); |
| 677 | } |
| 678 | |
| 679 | // Next find the last paragraph containing part of the range |
| 680 | node = m_children.GetFirst(); |
| 681 | while (node) |
| 682 | { |
| 683 | // child is a paragraph |
| 684 | wxRichTextObject* child = node->GetData(); |
| 685 | const wxRichTextRange& r = child->GetRange(); |
| 686 | |
| 687 | if (r.GetStart() <= range.GetEnd() && r.GetEnd() >= range.GetEnd()) |
| 688 | { |
| 689 | endPara = node; |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | node = node->GetNext(); |
| 694 | } |
| 695 | |
| 696 | if (!startPara || !endPara) |
| 697 | return false; |
| 698 | |
| 699 | // Now we can add up the sizes |
| 700 | for (node = startPara; node ; node = node->GetNext()) |
| 701 | { |
| 702 | // child is a paragraph |
| 703 | wxRichTextObject* child = node->GetData(); |
| 704 | const wxRichTextRange& childRange = child->GetRange(); |
| 705 | wxRichTextRange rangeToFind = range; |
| 706 | rangeToFind.LimitTo(childRange); |
| 707 | |
| 708 | wxSize childSize; |
| 709 | |
| 710 | int childDescent = 0; |
| 711 | child->GetRangeSize(rangeToFind, childSize, childDescent, dc, flags); |
| 712 | |
| 713 | descent = wxMax(childDescent, descent); |
| 714 | |
| 715 | sz.x = wxMax(sz.x, childSize.x); |
| 716 | sz.y += childSize.y; |
| 717 | |
| 718 | if (node == endPara) |
| 719 | break; |
| 720 | } |
| 721 | |
| 722 | size = sz; |
| 723 | |
| 724 | return true; |
| 725 | } |
| 726 | |
| 727 | /// Get the paragraph at the given position |
| 728 | wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphAtPosition(long pos, bool caretPosition) const |
| 729 | { |
| 730 | if (caretPosition) |
| 731 | pos ++; |
| 732 | |
| 733 | // First find the first paragraph whose starting position is within the range. |
| 734 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 735 | while (node) |
| 736 | { |
| 737 | // child is a paragraph |
| 738 | wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 739 | wxASSERT (child != NULL); |
| 740 | |
| 741 | // Return first child in buffer if position is -1 |
| 742 | // if (pos == -1) |
| 743 | // return child; |
| 744 | |
| 745 | if (child->GetRange().Contains(pos)) |
| 746 | return child; |
| 747 | |
| 748 | node = node->GetNext(); |
| 749 | } |
| 750 | return NULL; |
| 751 | } |
| 752 | |
| 753 | /// Get the line at the given position |
| 754 | wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtPosition(long pos, bool caretPosition) const |
| 755 | { |
| 756 | if (caretPosition) |
| 757 | pos ++; |
| 758 | |
| 759 | // First find the first paragraph whose starting position is within the range. |
| 760 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 761 | while (node) |
| 762 | { |
| 763 | // child is a paragraph |
| 764 | wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 765 | wxASSERT (child != NULL); |
| 766 | |
| 767 | wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); |
| 768 | while (node2) |
| 769 | { |
| 770 | wxRichTextLine* line = node2->GetData(); |
| 771 | |
| 772 | wxRichTextRange range = line->GetAbsoluteRange(); |
| 773 | |
| 774 | if (range.Contains(pos) || |
| 775 | |
| 776 | // If the position is end-of-paragraph, then return the last line of |
| 777 | // of the paragraph. |
| 778 | (range.GetEnd() == child->GetRange().GetEnd()-1) && (pos == child->GetRange().GetEnd())) |
| 779 | return line; |
| 780 | |
| 781 | node2 = node2->GetNext(); |
| 782 | } |
| 783 | |
| 784 | node = node->GetNext(); |
| 785 | } |
| 786 | |
| 787 | int lineCount = GetLineCount(); |
| 788 | if (lineCount > 0) |
| 789 | return GetLineForVisibleLineNumber(lineCount-1); |
| 790 | else |
| 791 | return NULL; |
| 792 | } |
| 793 | |
| 794 | /// Get the line at the given y pixel position, or the last line. |
| 795 | wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineAtYPosition(int y) const |
| 796 | { |
| 797 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 798 | while (node) |
| 799 | { |
| 800 | wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 801 | wxASSERT (child != NULL); |
| 802 | |
| 803 | wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); |
| 804 | while (node2) |
| 805 | { |
| 806 | wxRichTextLine* line = node2->GetData(); |
| 807 | |
| 808 | wxRect rect(line->GetRect()); |
| 809 | |
| 810 | if (y <= rect.GetBottom()) |
| 811 | return line; |
| 812 | |
| 813 | node2 = node2->GetNext(); |
| 814 | } |
| 815 | |
| 816 | node = node->GetNext(); |
| 817 | } |
| 818 | |
| 819 | // Return last line |
| 820 | int lineCount = GetLineCount(); |
| 821 | if (lineCount > 0) |
| 822 | return GetLineForVisibleLineNumber(lineCount-1); |
| 823 | else |
| 824 | return NULL; |
| 825 | } |
| 826 | |
| 827 | /// Get the number of visible lines |
| 828 | int wxRichTextParagraphLayoutBox::GetLineCount() const |
| 829 | { |
| 830 | int count = 0; |
| 831 | |
| 832 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 833 | while (node) |
| 834 | { |
| 835 | wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 836 | wxASSERT (child != NULL); |
| 837 | |
| 838 | count += child->GetLines().GetCount(); |
| 839 | node = node->GetNext(); |
| 840 | } |
| 841 | return count; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | /// Get the paragraph for a given line |
| 846 | wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphForLine(wxRichTextLine* line) const |
| 847 | { |
| 848 | return GetParagraphAtPosition(line->GetAbsoluteRange().GetStart()); |
| 849 | } |
| 850 | |
| 851 | /// Get the line size at the given position |
| 852 | wxSize wxRichTextParagraphLayoutBox::GetLineSizeAtPosition(long pos, bool caretPosition) const |
| 853 | { |
| 854 | wxRichTextLine* line = GetLineAtPosition(pos, caretPosition); |
| 855 | if (line) |
| 856 | { |
| 857 | return line->GetSize(); |
| 858 | } |
| 859 | else |
| 860 | return wxSize(0, 0); |
| 861 | } |
| 862 | |
| 863 | |
| 864 | /// Convenience function to add a paragraph of text |
| 865 | wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraph(const wxString& text) |
| 866 | { |
| 867 | wxTextAttrEx style(GetAttributes()); |
| 868 | |
| 869 | // Apply default style. If the style has no attributes set, |
| 870 | // then the attributes will remain the 'basic style' (i.e. the |
| 871 | // layout box's style). |
| 872 | wxRichTextApplyStyle(style, GetDefaultStyle()); |
| 873 | |
| 874 | wxRichTextParagraph* para = new wxRichTextParagraph(text, this, & style); |
| 875 | |
| 876 | AppendChild(para); |
| 877 | |
| 878 | UpdateRanges(); |
| 879 | SetDirty(true); |
| 880 | |
| 881 | return para->GetRange(); |
| 882 | } |
| 883 | |
| 884 | /// Adds multiple paragraphs, based on newlines. |
| 885 | wxRichTextRange wxRichTextParagraphLayoutBox::AddParagraphs(const wxString& text) |
| 886 | { |
| 887 | wxTextAttrEx style(GetAttributes()); |
| 888 | //wxLogDebug("Initial style = %s", style.GetFont().GetFaceName()); |
| 889 | //wxLogDebug("Initial size = %d", style.GetFont().GetPointSize()); |
| 890 | |
| 891 | // Apply default style. If the style has no attributes set, |
| 892 | // then the attributes will remain the 'basic style' (i.e. the |
| 893 | // layout box's style). |
| 894 | wxRichTextApplyStyle(style, GetDefaultStyle()); |
| 895 | |
| 896 | //wxLogDebug("Style after applying default style = %s", style.GetFont().GetFaceName()); |
| 897 | //wxLogDebug("Size after applying default style = %d", style.GetFont().GetPointSize()); |
| 898 | |
| 899 | wxRichTextParagraph* firstPara = NULL; |
| 900 | wxRichTextParagraph* lastPara = NULL; |
| 901 | |
| 902 | wxRichTextRange range(-1, -1); |
| 903 | size_t i = 0; |
| 904 | size_t len = text.Length(); |
| 905 | wxString line; |
| 906 | while (i < len) |
| 907 | { |
| 908 | wxChar ch = text[i]; |
| 909 | if (ch == wxT('\n') || ch == wxT('\r')) |
| 910 | { |
| 911 | wxRichTextParagraph* para = new wxRichTextParagraph(line, this, & style); |
| 912 | |
| 913 | AppendChild(para); |
| 914 | if (!firstPara) |
| 915 | firstPara = para; |
| 916 | lastPara = para; |
| 917 | line = wxEmptyString; |
| 918 | } |
| 919 | else |
| 920 | line += ch; |
| 921 | |
| 922 | i ++; |
| 923 | } |
| 924 | if (!line.empty()) |
| 925 | { |
| 926 | lastPara = new wxRichTextParagraph(line, this, & style); |
| 927 | //wxLogDebug("Para Face = %s", lastPara->GetAttributes().GetFont().GetFaceName()); |
| 928 | AppendChild(lastPara); |
| 929 | } |
| 930 | |
| 931 | if (firstPara) |
| 932 | range.SetStart(firstPara->GetRange().GetStart()); |
| 933 | else if (lastPara) |
| 934 | range.SetStart(lastPara->GetRange().GetStart()); |
| 935 | |
| 936 | if (lastPara) |
| 937 | range.SetEnd(lastPara->GetRange().GetEnd()); |
| 938 | else if (firstPara) |
| 939 | range.SetEnd(firstPara->GetRange().GetEnd()); |
| 940 | |
| 941 | UpdateRanges(); |
| 942 | SetDirty(false); |
| 943 | |
| 944 | return GetRange(); |
| 945 | } |
| 946 | |
| 947 | /// Convenience function to add an image |
| 948 | wxRichTextRange wxRichTextParagraphLayoutBox::AddImage(const wxImage& image) |
| 949 | { |
| 950 | wxTextAttrEx style(GetAttributes()); |
| 951 | |
| 952 | // Apply default style. If the style has no attributes set, |
| 953 | // then the attributes will remain the 'basic style' (i.e. the |
| 954 | // layout box's style). |
| 955 | wxRichTextApplyStyle(style, GetDefaultStyle()); |
| 956 | |
| 957 | wxRichTextParagraph* para = new wxRichTextParagraph(this, & style); |
| 958 | AppendChild(para); |
| 959 | para->AppendChild(new wxRichTextImage(image, this)); |
| 960 | |
| 961 | UpdateRanges(); |
| 962 | SetDirty(true); |
| 963 | |
| 964 | return para->GetRange(); |
| 965 | } |
| 966 | |
| 967 | |
| 968 | /// Insert fragment into this box at the given position. If partialParagraph is true, |
| 969 | /// it is assumed that the last (or only) paragraph is just a piece of data with no paragraph |
| 970 | /// marker. |
| 971 | /// TODO: if fragment is inserted inside styled fragment, must apply that style to |
| 972 | /// to the data (if it has a default style, anyway). |
| 973 | |
| 974 | bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextFragment& fragment) |
| 975 | { |
| 976 | SetDirty(true); |
| 977 | |
| 978 | // First, find the first paragraph whose starting position is within the range. |
| 979 | wxRichTextParagraph* para = GetParagraphAtPosition(position); |
| 980 | if (para) |
| 981 | { |
| 982 | wxRichTextObjectList::compatibility_iterator node = m_children.Find(para); |
| 983 | |
| 984 | // Now split at this position, returning the object to insert the new |
| 985 | // ones in front of. |
| 986 | wxRichTextObject* nextObject = para->SplitAt(position); |
| 987 | |
| 988 | // Special case: partial paragraph, just one paragraph. Might be a small amount of |
| 989 | // text, for example, so let's optimize. |
| 990 | |
| 991 | if (fragment.GetPartialParagraph() && fragment.GetChildren().GetCount() == 1) |
| 992 | { |
| 993 | // Add the first para to this para... |
| 994 | wxRichTextObjectList::compatibility_iterator firstParaNode = fragment.GetChildren().GetFirst(); |
| 995 | if (!firstParaNode) |
| 996 | return false; |
| 997 | |
| 998 | // Iterate through the fragment paragraph inserting the content into this paragraph. |
| 999 | wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph); |
| 1000 | wxASSERT (firstPara != NULL); |
| 1001 | |
| 1002 | wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst(); |
| 1003 | while (objectNode) |
| 1004 | { |
| 1005 | wxRichTextObject* newObj = objectNode->GetData()->Clone(); |
| 1006 | |
| 1007 | if (!nextObject) |
| 1008 | { |
| 1009 | // Append |
| 1010 | para->AppendChild(newObj); |
| 1011 | } |
| 1012 | else |
| 1013 | { |
| 1014 | // Insert before nextObject |
| 1015 | para->InsertChild(newObj, nextObject); |
| 1016 | } |
| 1017 | |
| 1018 | objectNode = objectNode->GetNext(); |
| 1019 | } |
| 1020 | |
| 1021 | return true; |
| 1022 | } |
| 1023 | else |
| 1024 | { |
| 1025 | // Procedure for inserting a fragment consisting of a number of |
| 1026 | // paragraphs: |
| 1027 | // |
| 1028 | // 1. Remove and save the content that's after the insertion point, for adding |
| 1029 | // back once we've added the fragment. |
| 1030 | // 2. Add the content from the first fragment paragraph to the current |
| 1031 | // paragraph. |
| 1032 | // 3. Add remaining fragment paragraphs after the current paragraph. |
| 1033 | // 4. Add back the saved content from the first paragraph. If partialParagraph |
| 1034 | // is true, add it to the last paragraph added and not a new one. |
| 1035 | |
| 1036 | // 1. Remove and save objects after split point. |
| 1037 | wxList savedObjects; |
| 1038 | if (nextObject) |
| 1039 | para->MoveToList(nextObject, savedObjects); |
| 1040 | |
| 1041 | // 2. Add the content from the 1st fragment paragraph. |
| 1042 | wxRichTextObjectList::compatibility_iterator firstParaNode = fragment.GetChildren().GetFirst(); |
| 1043 | if (!firstParaNode) |
| 1044 | return false; |
| 1045 | |
| 1046 | wxRichTextParagraph* firstPara = wxDynamicCast(firstParaNode->GetData(), wxRichTextParagraph); |
| 1047 | wxASSERT(firstPara != NULL); |
| 1048 | |
| 1049 | wxRichTextObjectList::compatibility_iterator objectNode = firstPara->GetChildren().GetFirst(); |
| 1050 | while (objectNode) |
| 1051 | { |
| 1052 | wxRichTextObject* newObj = objectNode->GetData()->Clone(); |
| 1053 | |
| 1054 | // Append |
| 1055 | para->AppendChild(newObj); |
| 1056 | |
| 1057 | objectNode = objectNode->GetNext(); |
| 1058 | } |
| 1059 | |
| 1060 | // 3. Add remaining fragment paragraphs after the current paragraph. |
| 1061 | wxRichTextObjectList::compatibility_iterator nextParagraphNode = node->GetNext(); |
| 1062 | wxRichTextObject* nextParagraph = NULL; |
| 1063 | if (nextParagraphNode) |
| 1064 | nextParagraph = nextParagraphNode->GetData(); |
| 1065 | |
| 1066 | wxRichTextObjectList::compatibility_iterator i = fragment.GetChildren().GetFirst()->GetNext(); |
| 1067 | wxRichTextParagraph* finalPara = para; |
| 1068 | |
| 1069 | // If there was only one paragraph, we need to insert a new one. |
| 1070 | if (!i) |
| 1071 | { |
| 1072 | finalPara = new wxRichTextParagraph; |
| 1073 | |
| 1074 | // TODO: These attributes should come from the subsequent paragraph |
| 1075 | // when originally deleted, since the subsequent para takes on |
| 1076 | // the previous para's attributes. |
| 1077 | finalPara->SetAttributes(firstPara->GetAttributes()); |
| 1078 | |
| 1079 | if (nextParagraph) |
| 1080 | InsertChild(finalPara, nextParagraph); |
| 1081 | else |
| 1082 | AppendChild(finalPara); |
| 1083 | } |
| 1084 | else while (i) |
| 1085 | { |
| 1086 | wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph); |
| 1087 | wxASSERT( para != NULL ); |
| 1088 | |
| 1089 | finalPara = (wxRichTextParagraph*) para->Clone(); |
| 1090 | |
| 1091 | if (nextParagraph) |
| 1092 | InsertChild(finalPara, nextParagraph); |
| 1093 | else |
| 1094 | AppendChild(finalPara); |
| 1095 | |
| 1096 | i = i->GetNext(); |
| 1097 | } |
| 1098 | |
| 1099 | // 4. Add back the remaining content. |
| 1100 | if (finalPara) |
| 1101 | { |
| 1102 | finalPara->MoveFromList(savedObjects); |
| 1103 | |
| 1104 | // Ensure there's at least one object |
| 1105 | if (finalPara->GetChildCount() == 0) |
| 1106 | { |
| 1107 | wxRichTextPlainText* text = new wxRichTextPlainText(wxEmptyString); |
| 1108 | text->SetAttributes(finalPara->GetAttributes()); |
| 1109 | |
| 1110 | finalPara->AppendChild(text); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | return true; |
| 1115 | } |
| 1116 | } |
| 1117 | else |
| 1118 | { |
| 1119 | // Append |
| 1120 | wxRichTextObjectList::compatibility_iterator i = fragment.GetChildren().GetFirst(); |
| 1121 | while (i) |
| 1122 | { |
| 1123 | wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph); |
| 1124 | wxASSERT( para != NULL ); |
| 1125 | |
| 1126 | AppendChild(para->Clone()); |
| 1127 | |
| 1128 | i = i->GetNext(); |
| 1129 | } |
| 1130 | |
| 1131 | return true; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | /// Make a copy of the fragment corresponding to the given range, putting it in 'fragment'. |
| 1136 | /// If there was an incomplete paragraph at the end, partialParagraph is set to true. |
| 1137 | bool wxRichTextParagraphLayoutBox::CopyFragment(const wxRichTextRange& range, wxRichTextFragment& fragment) |
| 1138 | { |
| 1139 | wxRichTextObjectList::compatibility_iterator i = GetChildren().GetFirst(); |
| 1140 | while (i) |
| 1141 | { |
| 1142 | wxRichTextParagraph* para = wxDynamicCast(i->GetData(), wxRichTextParagraph); |
| 1143 | wxASSERT( para != NULL ); |
| 1144 | |
| 1145 | if (!para->GetRange().IsOutside(range)) |
| 1146 | { |
| 1147 | fragment.AppendChild(para->Clone()); |
| 1148 | } |
| 1149 | i = i->GetNext(); |
| 1150 | } |
| 1151 | |
| 1152 | // Now top and tail the first and last paragraphs in our new fragment (which might be the same). |
| 1153 | if (!fragment.IsEmpty()) |
| 1154 | { |
| 1155 | wxRichTextRange topTailRange(range); |
| 1156 | |
| 1157 | wxRichTextParagraph* firstPara = wxDynamicCast(fragment.GetChildren().GetFirst()->GetData(), wxRichTextParagraph); |
| 1158 | wxASSERT( firstPara != NULL ); |
| 1159 | |
| 1160 | // Chop off the start of the paragraph |
| 1161 | if (topTailRange.GetStart() > firstPara->GetRange().GetStart()) |
| 1162 | { |
| 1163 | wxRichTextRange r(firstPara->GetRange().GetStart(), topTailRange.GetStart()-1); |
| 1164 | firstPara->DeleteRange(r); |
| 1165 | |
| 1166 | // Make sure the numbering is correct |
| 1167 | long end; |
| 1168 | fragment.CalculateRange(firstPara->GetRange().GetStart(), end); |
| 1169 | |
| 1170 | // Now, we've deleted some positions, so adjust the range |
| 1171 | // accordingly. |
| 1172 | topTailRange.SetEnd(topTailRange.GetEnd() - r.GetLength()); |
| 1173 | } |
| 1174 | |
| 1175 | wxRichTextParagraph* lastPara = wxDynamicCast(fragment.GetChildren().GetLast()->GetData(), wxRichTextParagraph); |
| 1176 | wxASSERT( lastPara != NULL ); |
| 1177 | |
| 1178 | if (topTailRange.GetEnd() < (lastPara->GetRange().GetEnd()-1)) |
| 1179 | { |
| 1180 | wxRichTextRange r(topTailRange.GetEnd()+1, lastPara->GetRange().GetEnd()-1); /* -1 since actual text ends 1 position before end of para marker */ |
| 1181 | lastPara->DeleteRange(r); |
| 1182 | |
| 1183 | // Make sure the numbering is correct |
| 1184 | long end; |
| 1185 | fragment.CalculateRange(firstPara->GetRange().GetStart(), end); |
| 1186 | |
| 1187 | // We only have part of a paragraph at the end |
| 1188 | fragment.SetPartialParagraph(true); |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | if (topTailRange.GetEnd() == (lastPara->GetRange().GetEnd() - 1)) |
| 1193 | // We have a partial paragraph (don't save last new paragraph marker) |
| 1194 | fragment.SetPartialParagraph(true); |
| 1195 | else |
| 1196 | // We have a complete paragraph |
| 1197 | fragment.SetPartialParagraph(false); |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
| 1204 | /// Given a position, get the number of the visible line (potentially many to a paragraph), |
| 1205 | /// starting from zero at the start of the buffer. |
| 1206 | long wxRichTextParagraphLayoutBox::GetVisibleLineNumber(long pos, bool caretPosition, bool startOfLine) const |
| 1207 | { |
| 1208 | if (caretPosition) |
| 1209 | pos ++; |
| 1210 | |
| 1211 | int lineCount = 0; |
| 1212 | |
| 1213 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 1214 | while (node) |
| 1215 | { |
| 1216 | wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 1217 | wxASSERT( child != NULL ); |
| 1218 | |
| 1219 | if (child->GetRange().Contains(pos)) |
| 1220 | { |
| 1221 | wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); |
| 1222 | while (node2) |
| 1223 | { |
| 1224 | wxRichTextLine* line = node2->GetData(); |
| 1225 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 1226 | |
| 1227 | if (lineRange.Contains(pos)) |
| 1228 | { |
| 1229 | // If the caret is displayed at the end of the previous wrapped line, |
| 1230 | // we want to return the line it's _displayed_ at (not the actual line |
| 1231 | // containing the position). |
| 1232 | if (lineRange.GetStart() == pos && !startOfLine && child->GetRange().GetStart() != pos) |
| 1233 | return lineCount - 1; |
| 1234 | else |
| 1235 | return lineCount; |
| 1236 | } |
| 1237 | |
| 1238 | lineCount ++; |
| 1239 | |
| 1240 | node2 = node2->GetNext(); |
| 1241 | } |
| 1242 | // If we didn't find it in the lines, it must be |
| 1243 | // the last position of the paragraph. So return the last line. |
| 1244 | return lineCount-1; |
| 1245 | } |
| 1246 | else |
| 1247 | lineCount += child->GetLines().GetCount(); |
| 1248 | |
| 1249 | node = node->GetNext(); |
| 1250 | } |
| 1251 | |
| 1252 | // Not found |
| 1253 | return -1; |
| 1254 | } |
| 1255 | |
| 1256 | /// Given a line number, get the corresponding wxRichTextLine object. |
| 1257 | wxRichTextLine* wxRichTextParagraphLayoutBox::GetLineForVisibleLineNumber(long lineNumber) const |
| 1258 | { |
| 1259 | int lineCount = 0; |
| 1260 | |
| 1261 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 1262 | while (node) |
| 1263 | { |
| 1264 | wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 1265 | wxASSERT(child != NULL); |
| 1266 | |
| 1267 | if (lineNumber < (int) (child->GetLines().GetCount() + lineCount)) |
| 1268 | { |
| 1269 | wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst(); |
| 1270 | while (node2) |
| 1271 | { |
| 1272 | wxRichTextLine* line = node2->GetData(); |
| 1273 | |
| 1274 | if (lineCount == lineNumber) |
| 1275 | return line; |
| 1276 | |
| 1277 | lineCount ++; |
| 1278 | |
| 1279 | node2 = node2->GetNext(); |
| 1280 | } |
| 1281 | } |
| 1282 | else |
| 1283 | lineCount += child->GetLines().GetCount(); |
| 1284 | |
| 1285 | node = node->GetNext(); |
| 1286 | } |
| 1287 | |
| 1288 | // Didn't find it |
| 1289 | return NULL; |
| 1290 | } |
| 1291 | |
| 1292 | /// Delete range from layout. |
| 1293 | bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange& range) |
| 1294 | { |
| 1295 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 1296 | |
| 1297 | while (node) |
| 1298 | { |
| 1299 | wxRichTextParagraph* obj = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 1300 | wxASSERT (obj != NULL); |
| 1301 | |
| 1302 | wxRichTextObjectList::compatibility_iterator next = node->GetNext(); |
| 1303 | |
| 1304 | // Delete the range in each paragraph |
| 1305 | |
| 1306 | if (!obj->GetRange().IsOutside(range)) |
| 1307 | { |
| 1308 | // Deletes the content of this object within the given range |
| 1309 | obj->DeleteRange(range); |
| 1310 | |
| 1311 | // If the whole paragraph is within the range to delete, |
| 1312 | // delete the whole thing. |
| 1313 | if (range.GetStart() <= obj->GetRange().GetStart() && range.GetEnd() >= obj->GetRange().GetEnd()) |
| 1314 | { |
| 1315 | // Delete the whole object |
| 1316 | RemoveChild(obj, true); |
| 1317 | } |
| 1318 | // If the range includes the paragraph end, we need to join this |
| 1319 | // and the next paragraph. |
| 1320 | else if (range.Contains(obj->GetRange().GetEnd())) |
| 1321 | { |
| 1322 | // We need to move the objects from the next paragraph |
| 1323 | // to this paragraph |
| 1324 | |
| 1325 | if (next) |
| 1326 | { |
| 1327 | wxRichTextParagraph* nextParagraph = wxDynamicCast(next->GetData(), wxRichTextParagraph); |
| 1328 | next = next->GetNext(); |
| 1329 | if (nextParagraph) |
| 1330 | { |
| 1331 | // Delete the stuff we need to delete |
| 1332 | nextParagraph->DeleteRange(range); |
| 1333 | |
| 1334 | // Move the objects to the previous para |
| 1335 | wxRichTextObjectList::compatibility_iterator node1 = nextParagraph->GetChildren().GetFirst(); |
| 1336 | |
| 1337 | while (node1) |
| 1338 | { |
| 1339 | wxRichTextObject* obj1 = node1->GetData(); |
| 1340 | |
| 1341 | // If the object is empty, optimise it out |
| 1342 | if (obj1->IsEmpty()) |
| 1343 | { |
| 1344 | delete obj1; |
| 1345 | } |
| 1346 | else |
| 1347 | { |
| 1348 | obj->AppendChild(obj1); |
| 1349 | } |
| 1350 | |
| 1351 | wxRichTextObjectList::compatibility_iterator next1 = node1->GetNext(); |
| 1352 | nextParagraph->GetChildren().Erase(node1); |
| 1353 | |
| 1354 | node1 = next1; |
| 1355 | } |
| 1356 | |
| 1357 | // Delete the paragraph |
| 1358 | RemoveChild(nextParagraph, true); |
| 1359 | |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | node = next; |
| 1367 | } |
| 1368 | |
| 1369 | return true; |
| 1370 | } |
| 1371 | |
| 1372 | /// Get any text in this object for the given range |
| 1373 | wxString wxRichTextParagraphLayoutBox::GetTextForRange(const wxRichTextRange& range) const |
| 1374 | { |
| 1375 | int lineCount = 0; |
| 1376 | wxString text; |
| 1377 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 1378 | while (node) |
| 1379 | { |
| 1380 | wxRichTextObject* child = node->GetData(); |
| 1381 | if (!child->GetRange().IsOutside(range)) |
| 1382 | { |
| 1383 | if (lineCount > 0) |
| 1384 | text += wxT("\n"); |
| 1385 | wxRichTextRange childRange = range; |
| 1386 | childRange.LimitTo(child->GetRange()); |
| 1387 | |
| 1388 | wxString childText = child->GetTextForRange(childRange); |
| 1389 | |
| 1390 | text += childText; |
| 1391 | |
| 1392 | lineCount ++; |
| 1393 | } |
| 1394 | node = node->GetNext(); |
| 1395 | } |
| 1396 | |
| 1397 | return text; |
| 1398 | } |
| 1399 | |
| 1400 | /// Get all the text |
| 1401 | wxString wxRichTextParagraphLayoutBox::GetText() const |
| 1402 | { |
| 1403 | return GetTextForRange(GetRange()); |
| 1404 | } |
| 1405 | |
| 1406 | /// Get the paragraph by number |
| 1407 | wxRichTextParagraph* wxRichTextParagraphLayoutBox::GetParagraphAtLine(long paragraphNumber) const |
| 1408 | { |
| 1409 | if ((size_t) paragraphNumber <= GetChildCount()) |
| 1410 | return NULL; |
| 1411 | |
| 1412 | return (wxRichTextParagraph*) GetChild((size_t) paragraphNumber); |
| 1413 | } |
| 1414 | |
| 1415 | /// Get the length of the paragraph |
| 1416 | int wxRichTextParagraphLayoutBox::GetParagraphLength(long paragraphNumber) const |
| 1417 | { |
| 1418 | wxRichTextParagraph* para = GetParagraphAtLine(paragraphNumber); |
| 1419 | if (para) |
| 1420 | return para->GetRange().GetLength() - 1; // don't include newline |
| 1421 | else |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | /// Get the text of the paragraph |
| 1426 | wxString wxRichTextParagraphLayoutBox::GetParagraphText(long paragraphNumber) const |
| 1427 | { |
| 1428 | wxRichTextParagraph* para = GetParagraphAtLine(paragraphNumber); |
| 1429 | if (para) |
| 1430 | return para->GetTextForRange(para->GetRange()); |
| 1431 | else |
| 1432 | return wxEmptyString; |
| 1433 | } |
| 1434 | |
| 1435 | /// Convert zero-based line column and paragraph number to a position. |
| 1436 | long wxRichTextParagraphLayoutBox::XYToPosition(long x, long y) const |
| 1437 | { |
| 1438 | wxRichTextParagraph* para = GetParagraphAtLine(y); |
| 1439 | if (para) |
| 1440 | { |
| 1441 | return para->GetRange().GetStart() + x; |
| 1442 | } |
| 1443 | else |
| 1444 | return -1; |
| 1445 | } |
| 1446 | |
| 1447 | /// Convert zero-based position to line column and paragraph number |
| 1448 | bool wxRichTextParagraphLayoutBox::PositionToXY(long pos, long* x, long* y) const |
| 1449 | { |
| 1450 | wxRichTextParagraph* para = GetParagraphAtPosition(pos); |
| 1451 | if (para) |
| 1452 | { |
| 1453 | int count = 0; |
| 1454 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 1455 | while (node) |
| 1456 | { |
| 1457 | wxRichTextObject* child = node->GetData(); |
| 1458 | if (child == para) |
| 1459 | break; |
| 1460 | count ++; |
| 1461 | node = node->GetNext(); |
| 1462 | } |
| 1463 | |
| 1464 | *y = count; |
| 1465 | *x = pos - para->GetRange().GetStart(); |
| 1466 | |
| 1467 | return true; |
| 1468 | } |
| 1469 | else |
| 1470 | return false; |
| 1471 | } |
| 1472 | |
| 1473 | /// Get the leaf object in a paragraph at this position. |
| 1474 | /// Given a line number, get the corresponding wxRichTextLine object. |
| 1475 | wxRichTextObject* wxRichTextParagraphLayoutBox::GetLeafObjectAtPosition(long position) const |
| 1476 | { |
| 1477 | wxRichTextParagraph* para = GetParagraphAtPosition(position); |
| 1478 | if (para) |
| 1479 | { |
| 1480 | wxRichTextObjectList::compatibility_iterator node = para->GetChildren().GetFirst(); |
| 1481 | |
| 1482 | while (node) |
| 1483 | { |
| 1484 | wxRichTextObject* child = node->GetData(); |
| 1485 | if (child->GetRange().Contains(position)) |
| 1486 | return child; |
| 1487 | |
| 1488 | node = node->GetNext(); |
| 1489 | } |
| 1490 | if (position == para->GetRange().GetEnd() && para->GetChildCount() > 0) |
| 1491 | return para->GetChildren().GetLast()->GetData(); |
| 1492 | } |
| 1493 | return NULL; |
| 1494 | } |
| 1495 | |
| 1496 | /// Set character or paragraph text attributes: apply character styles only to immediate text nodes |
| 1497 | bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style, bool withUndo) |
| 1498 | { |
| 1499 | bool characterStyle = false; |
| 1500 | bool paragraphStyle = false; |
| 1501 | |
| 1502 | if (style.IsCharacterStyle()) |
| 1503 | characterStyle = true; |
| 1504 | if (style.IsParagraphStyle()) |
| 1505 | paragraphStyle = true; |
| 1506 | |
| 1507 | // If we are associated with a control, make undoable; otherwise, apply immediately |
| 1508 | // to the data. |
| 1509 | |
| 1510 | bool haveControl = (GetRichTextCtrl() != NULL); |
| 1511 | |
| 1512 | wxRichTextAction* action = NULL; |
| 1513 | |
| 1514 | if (haveControl && withUndo) |
| 1515 | { |
| 1516 | action = new wxRichTextAction(NULL, _("Change Style"), wxRICHTEXT_CHANGE_STYLE, & GetRichTextCtrl()->GetBuffer(), GetRichTextCtrl()); |
| 1517 | action->SetRange(range); |
| 1518 | action->SetPosition(GetRichTextCtrl()->GetCaretPosition()); |
| 1519 | } |
| 1520 | |
| 1521 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 1522 | while (node) |
| 1523 | { |
| 1524 | wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 1525 | wxASSERT (para != NULL); |
| 1526 | |
| 1527 | if (para && para->GetChildCount() > 0) |
| 1528 | { |
| 1529 | // Stop searching if we're beyond the range of interest |
| 1530 | if (para->GetRange().GetStart() > range.GetEnd()) |
| 1531 | break; |
| 1532 | |
| 1533 | if (!para->GetRange().IsOutside(range)) |
| 1534 | { |
| 1535 | // We'll be using a copy of the paragraph to make style changes, |
| 1536 | // not updating the buffer directly. |
| 1537 | wxRichTextParagraph* newPara wxDUMMY_INITIALIZE(NULL); |
| 1538 | |
| 1539 | if (haveControl && withUndo) |
| 1540 | { |
| 1541 | newPara = new wxRichTextParagraph(*para); |
| 1542 | action->GetNewParagraphs().AppendChild(newPara); |
| 1543 | |
| 1544 | // Also store the old ones for Undo |
| 1545 | action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para)); |
| 1546 | } |
| 1547 | else |
| 1548 | newPara = para; |
| 1549 | |
| 1550 | if (paragraphStyle) |
| 1551 | wxRichTextApplyStyle(newPara->GetAttributes(), style); |
| 1552 | |
| 1553 | if (characterStyle && range.GetStart() != newPara->GetRange().GetEnd()) |
| 1554 | { |
| 1555 | wxRichTextRange childRange(range); |
| 1556 | childRange.LimitTo(newPara->GetRange()); |
| 1557 | |
| 1558 | // Find the starting position and if necessary split it so |
| 1559 | // we can start applying a different style. |
| 1560 | // TODO: check that the style actually changes or is different |
| 1561 | // from style outside of range |
| 1562 | wxRichTextObject* firstObject wxDUMMY_INITIALIZE(NULL); |
| 1563 | wxRichTextObject* lastObject wxDUMMY_INITIALIZE(NULL); |
| 1564 | |
| 1565 | if (childRange.GetStart() == newPara->GetRange().GetStart()) |
| 1566 | firstObject = newPara->GetChildren().GetFirst()->GetData(); |
| 1567 | else |
| 1568 | firstObject = newPara->SplitAt(range.GetStart()); |
| 1569 | |
| 1570 | // Increment by 1 because we're apply the style one _after_ the split point |
| 1571 | long splitPoint = childRange.GetEnd(); |
| 1572 | if (splitPoint != newPara->GetRange().GetEnd()) |
| 1573 | splitPoint ++; |
| 1574 | |
| 1575 | // Find last object |
| 1576 | if (splitPoint == newPara->GetRange().GetEnd() || splitPoint == (newPara->GetRange().GetEnd() - 1)) |
| 1577 | lastObject = newPara->GetChildren().GetLast()->GetData(); |
| 1578 | else |
| 1579 | // lastObject is set as a side-effect of splitting. It's |
| 1580 | // returned as the object before the new object. |
| 1581 | (void) newPara->SplitAt(splitPoint, & lastObject); |
| 1582 | |
| 1583 | wxASSERT(firstObject != NULL); |
| 1584 | wxASSERT(lastObject != NULL); |
| 1585 | |
| 1586 | if (!firstObject || !lastObject) |
| 1587 | continue; |
| 1588 | |
| 1589 | wxRichTextObjectList::compatibility_iterator firstNode = newPara->GetChildren().Find(firstObject); |
| 1590 | wxRichTextObjectList::compatibility_iterator lastNode = newPara->GetChildren().Find(lastObject); |
| 1591 | |
| 1592 | wxASSERT(firstNode); |
| 1593 | wxASSERT(lastNode); |
| 1594 | |
| 1595 | wxRichTextObjectList::compatibility_iterator node2 = firstNode; |
| 1596 | |
| 1597 | while (node2) |
| 1598 | { |
| 1599 | wxRichTextObject* child = node2->GetData(); |
| 1600 | |
| 1601 | wxRichTextApplyStyle(child->GetAttributes(), style); |
| 1602 | if (node2 == lastNode) |
| 1603 | break; |
| 1604 | |
| 1605 | node2 = node2->GetNext(); |
| 1606 | } |
| 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | node = node->GetNext(); |
| 1612 | } |
| 1613 | |
| 1614 | // Do action, or delay it until end of batch. |
| 1615 | if (haveControl && withUndo) |
| 1616 | GetRichTextCtrl()->GetBuffer().SubmitAction(action); |
| 1617 | |
| 1618 | return true; |
| 1619 | } |
| 1620 | |
| 1621 | /// Set text attributes |
| 1622 | bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const wxTextAttrEx& style, bool withUndo) |
| 1623 | { |
| 1624 | wxRichTextAttr richStyle = style; |
| 1625 | return SetStyle(range, richStyle, withUndo); |
| 1626 | } |
| 1627 | |
| 1628 | /// Get the text attributes for this position. |
| 1629 | bool wxRichTextParagraphLayoutBox::GetStyle(long position, wxTextAttrEx& style) const |
| 1630 | { |
| 1631 | wxRichTextObject* obj wxDUMMY_INITIALIZE(NULL); |
| 1632 | |
| 1633 | if (style.IsParagraphStyle()) |
| 1634 | obj = GetParagraphAtPosition(position); |
| 1635 | else |
| 1636 | obj = GetLeafObjectAtPosition(position); |
| 1637 | |
| 1638 | if (obj) |
| 1639 | { |
| 1640 | style = obj->GetAttributes(); |
| 1641 | return true; |
| 1642 | } |
| 1643 | else |
| 1644 | return false; |
| 1645 | } |
| 1646 | |
| 1647 | /// Get the text attributes for this position. |
| 1648 | bool wxRichTextParagraphLayoutBox::GetStyle(long position, wxRichTextAttr& style) const |
| 1649 | { |
| 1650 | wxRichTextObject* obj wxDUMMY_INITIALIZE(NULL); |
| 1651 | |
| 1652 | if (style.IsParagraphStyle()) |
| 1653 | obj = GetParagraphAtPosition(position); |
| 1654 | else |
| 1655 | obj = GetLeafObjectAtPosition(position); |
| 1656 | |
| 1657 | if (obj) |
| 1658 | { |
| 1659 | style = obj->GetAttributes(); |
| 1660 | return true; |
| 1661 | } |
| 1662 | else |
| 1663 | return false; |
| 1664 | } |
| 1665 | |
| 1666 | /// Set default style |
| 1667 | bool wxRichTextParagraphLayoutBox::SetDefaultStyle(const wxTextAttrEx& style) |
| 1668 | { |
| 1669 | m_defaultAttributes = style; |
| 1670 | |
| 1671 | return true; |
| 1672 | } |
| 1673 | |
| 1674 | /// Test if this whole range has character attributes of the specified kind. If any |
| 1675 | /// of the attributes are different within the range, the test fails. You |
| 1676 | /// can use this to implement, for example, bold button updating. style must have |
| 1677 | /// flags indicating which attributes are of interest. |
| 1678 | bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const |
| 1679 | { |
| 1680 | int foundCount = 0; |
| 1681 | int matchingCount = 0; |
| 1682 | |
| 1683 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 1684 | while (node) |
| 1685 | { |
| 1686 | wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 1687 | wxASSERT (para != NULL); |
| 1688 | |
| 1689 | if (para) |
| 1690 | { |
| 1691 | // Stop searching if we're beyond the range of interest |
| 1692 | if (para->GetRange().GetStart() > range.GetEnd()) |
| 1693 | return foundCount == matchingCount; |
| 1694 | |
| 1695 | if (!para->GetRange().IsOutside(range)) |
| 1696 | { |
| 1697 | wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst(); |
| 1698 | |
| 1699 | while (node2) |
| 1700 | { |
| 1701 | wxRichTextObject* child = node2->GetData(); |
| 1702 | if (!child->GetRange().IsOutside(range) && child->IsKindOf(CLASSINFO(wxRichTextPlainText))) |
| 1703 | { |
| 1704 | foundCount ++; |
| 1705 | if (wxTextAttrEqPartial(child->GetAttributes(), style, style.GetFlags())) |
| 1706 | matchingCount ++; |
| 1707 | } |
| 1708 | |
| 1709 | node2 = node2->GetNext(); |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | node = node->GetNext(); |
| 1715 | } |
| 1716 | |
| 1717 | return foundCount == matchingCount; |
| 1718 | } |
| 1719 | |
| 1720 | bool wxRichTextParagraphLayoutBox::HasCharacterAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const |
| 1721 | { |
| 1722 | wxRichTextAttr richStyle = style; |
| 1723 | return HasCharacterAttributes(range, richStyle); |
| 1724 | } |
| 1725 | |
| 1726 | /// Test if this whole range has paragraph attributes of the specified kind. If any |
| 1727 | /// of the attributes are different within the range, the test fails. You |
| 1728 | /// can use this to implement, for example, centering button updating. style must have |
| 1729 | /// flags indicating which attributes are of interest. |
| 1730 | bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange& range, const wxRichTextAttr& style) const |
| 1731 | { |
| 1732 | int foundCount = 0; |
| 1733 | int matchingCount = 0; |
| 1734 | |
| 1735 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 1736 | while (node) |
| 1737 | { |
| 1738 | wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 1739 | wxASSERT (para != NULL); |
| 1740 | |
| 1741 | if (para) |
| 1742 | { |
| 1743 | // Stop searching if we're beyond the range of interest |
| 1744 | if (para->GetRange().GetStart() > range.GetEnd()) |
| 1745 | return foundCount == matchingCount; |
| 1746 | |
| 1747 | if (!para->GetRange().IsOutside(range)) |
| 1748 | { |
| 1749 | foundCount ++; |
| 1750 | if (wxTextAttrEqPartial(para->GetAttributes(), style, style.GetFlags())) |
| 1751 | matchingCount ++; |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | node = node->GetNext(); |
| 1756 | } |
| 1757 | return foundCount == matchingCount; |
| 1758 | } |
| 1759 | |
| 1760 | bool wxRichTextParagraphLayoutBox::HasParagraphAttributes(const wxRichTextRange& range, const wxTextAttrEx& style) const |
| 1761 | { |
| 1762 | wxRichTextAttr richStyle = style; |
| 1763 | return HasParagraphAttributes(range, richStyle); |
| 1764 | } |
| 1765 | |
| 1766 | void wxRichTextParagraphLayoutBox::Clear() |
| 1767 | { |
| 1768 | DeleteChildren(); |
| 1769 | } |
| 1770 | |
| 1771 | void wxRichTextParagraphLayoutBox::Reset() |
| 1772 | { |
| 1773 | Clear(); |
| 1774 | |
| 1775 | AddParagraph(wxEmptyString); |
| 1776 | } |
| 1777 | |
| 1778 | /// Invalidate the buffer. With no argument, invalidates whole buffer. |
| 1779 | void wxRichTextParagraphLayoutBox::Invalidate(const wxRichTextRange& invalidRange) |
| 1780 | { |
| 1781 | SetDirty(true); |
| 1782 | |
| 1783 | if (invalidRange == wxRICHTEXT_ALL) |
| 1784 | { |
| 1785 | m_invalidRange = wxRICHTEXT_ALL; |
| 1786 | return; |
| 1787 | } |
| 1788 | |
| 1789 | // Already invalidating everything |
| 1790 | if (m_invalidRange == wxRICHTEXT_ALL) |
| 1791 | return; |
| 1792 | |
| 1793 | if ((invalidRange.GetStart() < m_invalidRange.GetStart()) || m_invalidRange.GetStart() == -1) |
| 1794 | m_invalidRange.SetStart(invalidRange.GetStart()); |
| 1795 | if (invalidRange.GetEnd() > m_invalidRange.GetEnd()) |
| 1796 | m_invalidRange.SetEnd(invalidRange.GetEnd()); |
| 1797 | } |
| 1798 | |
| 1799 | /// Get invalid range, rounding to entire paragraphs if argument is true. |
| 1800 | wxRichTextRange wxRichTextParagraphLayoutBox::GetInvalidRange(bool wholeParagraphs) const |
| 1801 | { |
| 1802 | if (m_invalidRange == wxRICHTEXT_ALL || m_invalidRange == wxRICHTEXT_NONE) |
| 1803 | return m_invalidRange; |
| 1804 | |
| 1805 | wxRichTextRange range = m_invalidRange; |
| 1806 | |
| 1807 | if (wholeParagraphs) |
| 1808 | { |
| 1809 | wxRichTextParagraph* para1 = GetParagraphAtPosition(range.GetStart()); |
| 1810 | wxRichTextParagraph* para2 = GetParagraphAtPosition(range.GetEnd()); |
| 1811 | if (para1) |
| 1812 | range.SetStart(para1->GetRange().GetStart()); |
| 1813 | if (para2) |
| 1814 | range.SetEnd(para2->GetRange().GetEnd()); |
| 1815 | } |
| 1816 | return range; |
| 1817 | } |
| 1818 | |
| 1819 | /*! |
| 1820 | * wxRichTextFragment class declaration |
| 1821 | * This is a lind of paragraph layout box used for storing |
| 1822 | * paragraphs for Undo/Redo, for example. |
| 1823 | */ |
| 1824 | |
| 1825 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextFragment, wxRichTextParagraphLayoutBox) |
| 1826 | |
| 1827 | /// Initialise |
| 1828 | void wxRichTextFragment::Init() |
| 1829 | { |
| 1830 | m_partialParagraph = false; |
| 1831 | } |
| 1832 | |
| 1833 | /// Copy |
| 1834 | void wxRichTextFragment::Copy(const wxRichTextFragment& obj) |
| 1835 | { |
| 1836 | wxRichTextParagraphLayoutBox::Copy(obj); |
| 1837 | |
| 1838 | m_partialParagraph = obj.m_partialParagraph; |
| 1839 | } |
| 1840 | |
| 1841 | /*! |
| 1842 | * wxRichTextParagraph |
| 1843 | * This object represents a single paragraph (or in a straight text editor, a line). |
| 1844 | */ |
| 1845 | |
| 1846 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextParagraph, wxRichTextBox) |
| 1847 | |
| 1848 | wxRichTextParagraph::wxRichTextParagraph(wxRichTextObject* parent, wxTextAttrEx* style): |
| 1849 | wxRichTextBox(parent) |
| 1850 | { |
| 1851 | if (parent && !style) |
| 1852 | SetAttributes(parent->GetAttributes()); |
| 1853 | if (style) |
| 1854 | SetAttributes(*style); |
| 1855 | } |
| 1856 | |
| 1857 | wxRichTextParagraph::wxRichTextParagraph(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* style): |
| 1858 | wxRichTextBox(parent) |
| 1859 | { |
| 1860 | if (parent && !style) |
| 1861 | SetAttributes(parent->GetAttributes()); |
| 1862 | if (style) |
| 1863 | SetAttributes(*style); |
| 1864 | |
| 1865 | AppendChild(new wxRichTextPlainText(text, this)); |
| 1866 | } |
| 1867 | |
| 1868 | wxRichTextParagraph::~wxRichTextParagraph() |
| 1869 | { |
| 1870 | ClearLines(); |
| 1871 | } |
| 1872 | |
| 1873 | /// Draw the item |
| 1874 | bool wxRichTextParagraph::Draw(wxDC& dc, const wxRichTextRange& WXUNUSED(range), const wxRichTextRange& selectionRange, const wxRect& WXUNUSED(rect), int WXUNUSED(descent), int style) |
| 1875 | { |
| 1876 | // Draw the bullet, if any |
| 1877 | if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE) |
| 1878 | { |
| 1879 | if (GetAttributes().GetLeftSubIndent() != 0) |
| 1880 | { |
| 1881 | int spaceBeforePara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingBefore()); |
| 1882 | // int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter()); |
| 1883 | int leftIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftIndent()); |
| 1884 | // int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent()); |
| 1885 | // int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent()); |
| 1886 | |
| 1887 | if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP) |
| 1888 | { |
| 1889 | // TODO |
| 1890 | } |
| 1891 | else |
| 1892 | { |
| 1893 | wxString bulletText = GetBulletText(); |
| 1894 | if (!bulletText.empty()) |
| 1895 | { |
| 1896 | if (GetAttributes().GetFont().Ok()) |
| 1897 | dc.SetFont(GetAttributes().GetFont()); |
| 1898 | |
| 1899 | if (GetAttributes().GetTextColour().Ok()) |
| 1900 | dc.SetTextForeground(GetAttributes().GetTextColour()); |
| 1901 | |
| 1902 | dc.SetBackgroundMode(wxTRANSPARENT); |
| 1903 | |
| 1904 | // Get line height from first line, if any |
| 1905 | wxRichTextLine* line = m_cachedLines.GetFirst() ? (wxRichTextLine* ) m_cachedLines.GetFirst()->GetData() : (wxRichTextLine*) NULL; |
| 1906 | |
| 1907 | wxPoint linePos; |
| 1908 | int lineHeight wxDUMMY_INITIALIZE(0); |
| 1909 | if (line) |
| 1910 | { |
| 1911 | lineHeight = line->GetSize().y; |
| 1912 | linePos = line->GetPosition() + GetPosition(); |
| 1913 | } |
| 1914 | else |
| 1915 | { |
| 1916 | lineHeight = dc.GetCharHeight(); |
| 1917 | linePos = GetPosition(); |
| 1918 | linePos.y += spaceBeforePara; |
| 1919 | } |
| 1920 | |
| 1921 | int charHeight = dc.GetCharHeight(); |
| 1922 | |
| 1923 | int x = GetPosition().x + leftIndent; |
| 1924 | int y = linePos.y + (lineHeight - charHeight); |
| 1925 | |
| 1926 | dc.DrawText(bulletText, x, y); |
| 1927 | } |
| 1928 | } |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | // Draw the range for each line, one object at a time. |
| 1933 | |
| 1934 | wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst(); |
| 1935 | while (node) |
| 1936 | { |
| 1937 | wxRichTextLine* line = node->GetData(); |
| 1938 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 1939 | |
| 1940 | int maxDescent = line->GetDescent(); |
| 1941 | |
| 1942 | // Lines are specified relative to the paragraph |
| 1943 | |
| 1944 | wxPoint linePosition = line->GetPosition() + GetPosition(); |
| 1945 | wxPoint objectPosition = linePosition; |
| 1946 | |
| 1947 | // Loop through objects until we get to the one within range |
| 1948 | wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst(); |
| 1949 | while (node2) |
| 1950 | { |
| 1951 | wxRichTextObject* child = node2->GetData(); |
| 1952 | if (!child->GetRange().IsOutside(lineRange)) |
| 1953 | { |
| 1954 | // Draw this part of the line at the correct position |
| 1955 | wxRichTextRange objectRange(child->GetRange()); |
| 1956 | objectRange.LimitTo(lineRange); |
| 1957 | |
| 1958 | wxSize objectSize; |
| 1959 | int descent = 0; |
| 1960 | child->GetRangeSize(objectRange, objectSize, descent, dc, wxRICHTEXT_UNFORMATTED); |
| 1961 | |
| 1962 | // Use the child object's width, but the whole line's height |
| 1963 | wxRect childRect(objectPosition, wxSize(objectSize.x, line->GetSize().y)); |
| 1964 | child->Draw(dc, objectRange, selectionRange, childRect, maxDescent, style); |
| 1965 | |
| 1966 | objectPosition.x += objectSize.x; |
| 1967 | } |
| 1968 | else if (child->GetRange().GetStart() > lineRange.GetEnd()) |
| 1969 | // Can break out of inner loop now since we've passed this line's range |
| 1970 | break; |
| 1971 | |
| 1972 | node2 = node2->GetNext(); |
| 1973 | } |
| 1974 | |
| 1975 | node = node->GetNext(); |
| 1976 | } |
| 1977 | |
| 1978 | return true; |
| 1979 | } |
| 1980 | |
| 1981 | /// Lay the item out |
| 1982 | bool wxRichTextParagraph::Layout(wxDC& dc, const wxRect& rect, int style) |
| 1983 | { |
| 1984 | // ClearLines(); |
| 1985 | |
| 1986 | // Increase the size of the paragraph due to spacing |
| 1987 | int spaceBeforePara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingBefore()); |
| 1988 | int spaceAfterPara = ConvertTenthsMMToPixels(dc, GetAttributes().GetParagraphSpacingAfter()); |
| 1989 | int leftIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftIndent()); |
| 1990 | int leftSubIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetLeftSubIndent()); |
| 1991 | int rightIndent = ConvertTenthsMMToPixels(dc, GetAttributes().GetRightIndent()); |
| 1992 | |
| 1993 | int lineSpacing = 0; |
| 1994 | |
| 1995 | // Let's assume line spacing of 10 is normal, 15 is 1.5, 20 is 2, etc. |
| 1996 | if (GetAttributes().GetLineSpacing() > 10 && GetAttributes().GetFont().Ok()) |
| 1997 | { |
| 1998 | dc.SetFont(GetAttributes().GetFont()); |
| 1999 | lineSpacing = (ConvertTenthsMMToPixels(dc, dc.GetCharHeight()) * GetAttributes().GetLineSpacing())/10; |
| 2000 | } |
| 2001 | |
| 2002 | // Available space for text on each line differs. |
| 2003 | int availableTextSpaceFirstLine = rect.GetWidth() - leftIndent - rightIndent; |
| 2004 | |
| 2005 | // Bullets start the text at the same position as subsequent lines |
| 2006 | if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE) |
| 2007 | availableTextSpaceFirstLine -= leftSubIndent; |
| 2008 | |
| 2009 | int availableTextSpaceSubsequentLines = rect.GetWidth() - leftIndent - rightIndent - leftSubIndent; |
| 2010 | |
| 2011 | // Start position for each line relative to the paragraph |
| 2012 | int startPositionFirstLine = leftIndent; |
| 2013 | int startPositionSubsequentLines = leftIndent + leftSubIndent; |
| 2014 | |
| 2015 | // If we have a bullet in this paragraph, the start position for the first line's text |
| 2016 | // is actually leftIndent + leftSubIndent. |
| 2017 | if (GetAttributes().GetBulletStyle() != wxTEXT_ATTR_BULLET_STYLE_NONE) |
| 2018 | startPositionFirstLine = startPositionSubsequentLines; |
| 2019 | |
| 2020 | //bool restrictWidth = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_WIDTH); |
| 2021 | //bool restrictHeight = wxRichTextHasStyle(style, wxRICHTEXT_FIXED_HEIGHT); |
| 2022 | |
| 2023 | long lastEndPos = GetRange().GetStart()-1; |
| 2024 | long lastCompletedEndPos = lastEndPos; |
| 2025 | |
| 2026 | int currentWidth = 0; |
| 2027 | SetPosition(rect.GetPosition()); |
| 2028 | |
| 2029 | wxPoint currentPosition(0, spaceBeforePara); // We will calculate lines relative to paragraph |
| 2030 | int lineHeight = 0; |
| 2031 | int maxWidth = 0; |
| 2032 | int maxDescent = 0; |
| 2033 | |
| 2034 | int lineCount = 0; |
| 2035 | |
| 2036 | // Split up lines |
| 2037 | |
| 2038 | // We may need to go back to a previous child, in which case create the new line, |
| 2039 | // find the child corresponding to the start position of the string, and |
| 2040 | // continue. |
| 2041 | |
| 2042 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 2043 | while (node) |
| 2044 | { |
| 2045 | wxRichTextObject* child = node->GetData(); |
| 2046 | |
| 2047 | // If this is e.g. a composite text box, it will need to be laid out itself. |
| 2048 | // But if just a text fragment or image, for example, this will |
| 2049 | // do nothing. NB: won't we need to set the position after layout? |
| 2050 | // since for example if position is dependent on vertical line size, we |
| 2051 | // can't tell the position until the size is determined. So possibly introduce |
| 2052 | // another layout phase. |
| 2053 | |
| 2054 | child->Layout(dc, rect, style); |
| 2055 | |
| 2056 | // Available width depends on whether we're on the first or subsequent lines |
| 2057 | int availableSpaceForText = (lineCount == 0 ? availableTextSpaceFirstLine : availableTextSpaceSubsequentLines); |
| 2058 | |
| 2059 | currentPosition.x = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines); |
| 2060 | |
| 2061 | // We may only be looking at part of a child, if we searched back for wrapping |
| 2062 | // and found a suitable point some way into the child. So get the size for the fragment |
| 2063 | // if necessary. |
| 2064 | |
| 2065 | wxSize childSize; |
| 2066 | int childDescent = 0; |
| 2067 | if (lastEndPos == child->GetRange().GetStart() - 1) |
| 2068 | { |
| 2069 | childSize = child->GetCachedSize(); |
| 2070 | childDescent = child->GetDescent(); |
| 2071 | } |
| 2072 | else |
| 2073 | GetRangeSize(wxRichTextRange(lastEndPos+1, child->GetRange().GetEnd()), childSize, childDescent, dc, wxRICHTEXT_UNFORMATTED); |
| 2074 | |
| 2075 | if (childSize.x + currentWidth > availableSpaceForText) |
| 2076 | { |
| 2077 | long wrapPosition = 0; |
| 2078 | |
| 2079 | // Find a place to wrap. This may walk back to previous children, |
| 2080 | // for example if a word spans several objects. |
| 2081 | if (!FindWrapPosition(wxRichTextRange(lastCompletedEndPos+1, child->GetRange().GetEnd()), dc, availableSpaceForText, wrapPosition)) |
| 2082 | { |
| 2083 | // If the function failed, just cut it off at the end of this child. |
| 2084 | wrapPosition = child->GetRange().GetEnd(); |
| 2085 | } |
| 2086 | |
| 2087 | // FindWrapPosition can still return a value that will put us in an endless wrapping loop |
| 2088 | if (wrapPosition <= lastCompletedEndPos) |
| 2089 | wrapPosition = wxMax(lastCompletedEndPos+1,child->GetRange().GetEnd()); |
| 2090 | |
| 2091 | // wxLogDebug(wxT("Split at %ld"), wrapPosition); |
| 2092 | |
| 2093 | // Let's find the actual size of the current line now |
| 2094 | wxSize actualSize; |
| 2095 | wxRichTextRange actualRange(lastCompletedEndPos+1, wrapPosition); |
| 2096 | GetRangeSize(actualRange, actualSize, childDescent, dc, wxRICHTEXT_UNFORMATTED); |
| 2097 | currentWidth = actualSize.x; |
| 2098 | lineHeight = wxMax(lineHeight, actualSize.y); |
| 2099 | maxDescent = wxMax(childDescent, maxDescent); |
| 2100 | |
| 2101 | // Add a new line |
| 2102 | wxRichTextLine* line = AllocateLine(lineCount); |
| 2103 | |
| 2104 | // Set relative range so we won't have to change line ranges when paragraphs are moved |
| 2105 | line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart())); |
| 2106 | line->SetPosition(currentPosition); |
| 2107 | line->SetSize(wxSize(currentWidth, lineHeight)); |
| 2108 | line->SetDescent(maxDescent); |
| 2109 | |
| 2110 | // Now move down a line. TODO: add margins, spacing |
| 2111 | currentPosition.y += lineHeight; |
| 2112 | currentPosition.y += lineSpacing; |
| 2113 | currentWidth = 0; |
| 2114 | maxDescent = 0; |
| 2115 | maxWidth = wxMax(maxWidth, currentWidth); |
| 2116 | |
| 2117 | lineCount ++; |
| 2118 | |
| 2119 | // TODO: account for zero-length objects, such as fields |
| 2120 | wxASSERT(wrapPosition > lastCompletedEndPos); |
| 2121 | |
| 2122 | lastEndPos = wrapPosition; |
| 2123 | lastCompletedEndPos = lastEndPos; |
| 2124 | |
| 2125 | lineHeight = 0; |
| 2126 | |
| 2127 | // May need to set the node back to a previous one, due to searching back in wrapping |
| 2128 | wxRichTextObject* childAfterWrapPosition = FindObjectAtPosition(wrapPosition+1); |
| 2129 | if (childAfterWrapPosition) |
| 2130 | node = m_children.Find(childAfterWrapPosition); |
| 2131 | else |
| 2132 | node = node->GetNext(); |
| 2133 | } |
| 2134 | else |
| 2135 | { |
| 2136 | // We still fit, so don't add a line, and keep going |
| 2137 | currentWidth += childSize.x; |
| 2138 | lineHeight = wxMax(lineHeight, childSize.y); |
| 2139 | maxDescent = wxMax(childDescent, maxDescent); |
| 2140 | |
| 2141 | maxWidth = wxMax(maxWidth, currentWidth); |
| 2142 | lastEndPos = child->GetRange().GetEnd(); |
| 2143 | |
| 2144 | node = node->GetNext(); |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | // Add the last line - it's the current pos -> last para pos |
| 2149 | // Substract -1 because the last position is always the end-paragraph position. |
| 2150 | if (lastCompletedEndPos <= GetRange().GetEnd()-1) |
| 2151 | { |
| 2152 | currentPosition.x = (lineCount == 0 ? startPositionFirstLine : startPositionSubsequentLines); |
| 2153 | |
| 2154 | wxRichTextLine* line = AllocateLine(lineCount); |
| 2155 | |
| 2156 | wxRichTextRange actualRange(lastCompletedEndPos+1, GetRange().GetEnd()-1); |
| 2157 | |
| 2158 | // Set relative range so we won't have to change line ranges when paragraphs are moved |
| 2159 | line->SetRange(wxRichTextRange(actualRange.GetStart() - GetRange().GetStart(), actualRange.GetEnd() - GetRange().GetStart())); |
| 2160 | |
| 2161 | line->SetPosition(currentPosition); |
| 2162 | |
| 2163 | if (lineHeight == 0) |
| 2164 | { |
| 2165 | if (GetAttributes().GetFont().Ok()) |
| 2166 | dc.SetFont(GetAttributes().GetFont()); |
| 2167 | lineHeight = dc.GetCharHeight(); |
| 2168 | } |
| 2169 | if (maxDescent == 0) |
| 2170 | { |
| 2171 | int w, h; |
| 2172 | dc.GetTextExtent(wxT("X"), & w, &h, & maxDescent); |
| 2173 | } |
| 2174 | |
| 2175 | line->SetSize(wxSize(currentWidth, lineHeight)); |
| 2176 | line->SetDescent(maxDescent); |
| 2177 | currentPosition.y += lineHeight; |
| 2178 | currentPosition.y += lineSpacing; |
| 2179 | lineCount ++; |
| 2180 | } |
| 2181 | |
| 2182 | // Remove remaining unused line objects, if any |
| 2183 | ClearUnusedLines(lineCount); |
| 2184 | |
| 2185 | // Apply styles to wrapped lines |
| 2186 | ApplyParagraphStyle(rect); |
| 2187 | |
| 2188 | SetCachedSize(wxSize(maxWidth, currentPosition.y + spaceBeforePara + spaceAfterPara)); |
| 2189 | |
| 2190 | m_dirty = false; |
| 2191 | |
| 2192 | return true; |
| 2193 | } |
| 2194 | |
| 2195 | /// Apply paragraph styles, such as centering, to wrapped lines |
| 2196 | void wxRichTextParagraph::ApplyParagraphStyle(const wxRect& rect) |
| 2197 | { |
| 2198 | if (!GetAttributes().HasAlignment()) |
| 2199 | return; |
| 2200 | |
| 2201 | wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst(); |
| 2202 | while (node) |
| 2203 | { |
| 2204 | wxRichTextLine* line = node->GetData(); |
| 2205 | |
| 2206 | wxPoint pos = line->GetPosition(); |
| 2207 | wxSize size = line->GetSize(); |
| 2208 | |
| 2209 | // centering, right-justification |
| 2210 | if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_CENTRE) |
| 2211 | { |
| 2212 | pos.x = (rect.GetWidth() - size.x)/2 + pos.x; |
| 2213 | line->SetPosition(pos); |
| 2214 | } |
| 2215 | else if (GetAttributes().HasAlignment() && GetAttributes().GetAlignment() == wxTEXT_ALIGNMENT_RIGHT) |
| 2216 | { |
| 2217 | pos.x = rect.GetRight() - size.x; |
| 2218 | line->SetPosition(pos); |
| 2219 | } |
| 2220 | |
| 2221 | node = node->GetNext(); |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | /// Insert text at the given position |
| 2226 | bool wxRichTextParagraph::InsertText(long pos, const wxString& text) |
| 2227 | { |
| 2228 | wxRichTextObject* childToUse = NULL; |
| 2229 | wxRichTextObjectList::compatibility_iterator nodeToUse = wxRichTextObjectList::compatibility_iterator(); |
| 2230 | |
| 2231 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 2232 | while (node) |
| 2233 | { |
| 2234 | wxRichTextObject* child = node->GetData(); |
| 2235 | if (child->GetRange().Contains(pos) && child->GetRange().GetLength() > 0) |
| 2236 | { |
| 2237 | childToUse = child; |
| 2238 | nodeToUse = node; |
| 2239 | break; |
| 2240 | } |
| 2241 | |
| 2242 | node = node->GetNext(); |
| 2243 | } |
| 2244 | |
| 2245 | if (childToUse) |
| 2246 | { |
| 2247 | wxRichTextPlainText* textObject = wxDynamicCast(childToUse, wxRichTextPlainText); |
| 2248 | if (textObject) |
| 2249 | { |
| 2250 | int posInString = pos - textObject->GetRange().GetStart(); |
| 2251 | |
| 2252 | wxString newText = textObject->GetText().Mid(0, posInString) + |
| 2253 | text + textObject->GetText().Mid(posInString); |
| 2254 | textObject->SetText(newText); |
| 2255 | |
| 2256 | int textLength = text.Length(); |
| 2257 | |
| 2258 | textObject->SetRange(wxRichTextRange(textObject->GetRange().GetStart(), |
| 2259 | textObject->GetRange().GetEnd() + textLength)); |
| 2260 | |
| 2261 | // Increment the end range of subsequent fragments in this paragraph. |
| 2262 | // We'll set the paragraph range itself at a higher level. |
| 2263 | |
| 2264 | wxRichTextObjectList::compatibility_iterator node = nodeToUse->GetNext(); |
| 2265 | while (node) |
| 2266 | { |
| 2267 | wxRichTextObject* child = node->GetData(); |
| 2268 | child->SetRange(wxRichTextRange(textObject->GetRange().GetStart() + textLength, |
| 2269 | textObject->GetRange().GetEnd() + textLength)); |
| 2270 | |
| 2271 | node = node->GetNext(); |
| 2272 | } |
| 2273 | |
| 2274 | return true; |
| 2275 | } |
| 2276 | else |
| 2277 | { |
| 2278 | // TODO: if not a text object, insert at closest position, e.g. in front of it |
| 2279 | } |
| 2280 | } |
| 2281 | else |
| 2282 | { |
| 2283 | // Add at end. |
| 2284 | // Don't pass parent initially to suppress auto-setting of parent range. |
| 2285 | // We'll do that at a higher level. |
| 2286 | wxRichTextPlainText* textObject = new wxRichTextPlainText(text, this); |
| 2287 | |
| 2288 | AppendChild(textObject); |
| 2289 | return true; |
| 2290 | } |
| 2291 | |
| 2292 | return false; |
| 2293 | } |
| 2294 | |
| 2295 | void wxRichTextParagraph::Copy(const wxRichTextParagraph& obj) |
| 2296 | { |
| 2297 | wxRichTextBox::Copy(obj); |
| 2298 | } |
| 2299 | |
| 2300 | /// Clear the cached lines |
| 2301 | void wxRichTextParagraph::ClearLines() |
| 2302 | { |
| 2303 | WX_CLEAR_LIST(wxRichTextLineList, m_cachedLines); |
| 2304 | } |
| 2305 | |
| 2306 | /// Get/set the object size for the given range. Returns false if the range |
| 2307 | /// is invalid for this object. |
| 2308 | bool wxRichTextParagraph::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int flags) const |
| 2309 | { |
| 2310 | if (!range.IsWithin(GetRange())) |
| 2311 | return false; |
| 2312 | |
| 2313 | if (flags & wxRICHTEXT_UNFORMATTED) |
| 2314 | { |
| 2315 | // Just use unformatted data, assume no line breaks |
| 2316 | // TODO: take into account line breaks |
| 2317 | |
| 2318 | wxSize sz; |
| 2319 | |
| 2320 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 2321 | while (node) |
| 2322 | { |
| 2323 | wxRichTextObject* child = node->GetData(); |
| 2324 | if (!child->GetRange().IsOutside(range)) |
| 2325 | { |
| 2326 | wxSize childSize; |
| 2327 | |
| 2328 | wxRichTextRange rangeToUse = range; |
| 2329 | rangeToUse.LimitTo(child->GetRange()); |
| 2330 | int childDescent = 0; |
| 2331 | |
| 2332 | if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags)) |
| 2333 | { |
| 2334 | sz.y = wxMax(sz.y, childSize.y); |
| 2335 | sz.x += childSize.x; |
| 2336 | descent = wxMax(descent, childDescent); |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | node = node->GetNext(); |
| 2341 | } |
| 2342 | size = sz; |
| 2343 | } |
| 2344 | else |
| 2345 | { |
| 2346 | // Use formatted data, with line breaks |
| 2347 | wxSize sz; |
| 2348 | |
| 2349 | // We're going to loop through each line, and then for each line, |
| 2350 | // call GetRangeSize for the fragment that comprises that line. |
| 2351 | // Only we have to do that multiple times within the line, because |
| 2352 | // the line may be broken into pieces. For now ignore line break commands |
| 2353 | // (so we can assume that getting the unformatted size for a fragment |
| 2354 | // within a line is the actual size) |
| 2355 | |
| 2356 | wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst(); |
| 2357 | while (node) |
| 2358 | { |
| 2359 | wxRichTextLine* line = node->GetData(); |
| 2360 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 2361 | if (!lineRange.IsOutside(range)) |
| 2362 | { |
| 2363 | wxSize lineSize; |
| 2364 | |
| 2365 | wxRichTextObjectList::compatibility_iterator node2 = m_children.GetFirst(); |
| 2366 | while (node2) |
| 2367 | { |
| 2368 | wxRichTextObject* child = node2->GetData(); |
| 2369 | |
| 2370 | if (!child->GetRange().IsOutside(lineRange)) |
| 2371 | { |
| 2372 | wxRichTextRange rangeToUse = lineRange; |
| 2373 | rangeToUse.LimitTo(child->GetRange()); |
| 2374 | |
| 2375 | wxSize childSize; |
| 2376 | int childDescent = 0; |
| 2377 | if (child->GetRangeSize(rangeToUse, childSize, childDescent, dc, flags)) |
| 2378 | { |
| 2379 | lineSize.y = wxMax(lineSize.y, childSize.y); |
| 2380 | lineSize.x += childSize.x; |
| 2381 | } |
| 2382 | descent = wxMax(descent, childDescent); |
| 2383 | } |
| 2384 | |
| 2385 | node2 = node2->GetNext(); |
| 2386 | } |
| 2387 | |
| 2388 | // Increase size by a line (TODO: paragraph spacing) |
| 2389 | sz.y += lineSize.y; |
| 2390 | sz.x = wxMax(sz.x, lineSize.x); |
| 2391 | } |
| 2392 | node = node->GetNext(); |
| 2393 | } |
| 2394 | size = sz; |
| 2395 | } |
| 2396 | return true; |
| 2397 | } |
| 2398 | |
| 2399 | /// Finds the absolute position and row height for the given character position |
| 2400 | bool wxRichTextParagraph::FindPosition(wxDC& dc, long index, wxPoint& pt, int* height, bool forceLineStart) |
| 2401 | { |
| 2402 | if (index == -1) |
| 2403 | { |
| 2404 | wxRichTextLine* line = ((wxRichTextParagraphLayoutBox*)GetParent())->GetLineAtPosition(0); |
| 2405 | if (line) |
| 2406 | *height = line->GetSize().y; |
| 2407 | else |
| 2408 | *height = dc.GetCharHeight(); |
| 2409 | |
| 2410 | // -1 means 'the start of the buffer'. |
| 2411 | pt = GetPosition(); |
| 2412 | if (line) |
| 2413 | pt = pt + line->GetPosition(); |
| 2414 | |
| 2415 | *height = dc.GetCharHeight(); |
| 2416 | |
| 2417 | return true; |
| 2418 | } |
| 2419 | |
| 2420 | // The final position in a paragraph is taken to mean the position |
| 2421 | // at the start of the next paragraph. |
| 2422 | if (index == GetRange().GetEnd()) |
| 2423 | { |
| 2424 | wxRichTextParagraphLayoutBox* parent = wxDynamicCast(GetParent(), wxRichTextParagraphLayoutBox); |
| 2425 | wxASSERT( parent != NULL ); |
| 2426 | |
| 2427 | // Find the height at the next paragraph, if any |
| 2428 | wxRichTextLine* line = parent->GetLineAtPosition(index + 1); |
| 2429 | if (line) |
| 2430 | { |
| 2431 | *height = line->GetSize().y; |
| 2432 | pt = line->GetAbsolutePosition(); |
| 2433 | } |
| 2434 | else |
| 2435 | { |
| 2436 | *height = dc.GetCharHeight(); |
| 2437 | int indent = ConvertTenthsMMToPixels(dc, m_attributes.GetLeftIndent()); |
| 2438 | pt = wxPoint(indent, GetCachedSize().y); |
| 2439 | } |
| 2440 | |
| 2441 | return true; |
| 2442 | } |
| 2443 | |
| 2444 | if (index < GetRange().GetStart() || index > GetRange().GetEnd()) |
| 2445 | return false; |
| 2446 | |
| 2447 | wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst(); |
| 2448 | while (node) |
| 2449 | { |
| 2450 | wxRichTextLine* line = node->GetData(); |
| 2451 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 2452 | if (index >= lineRange.GetStart() && index <= lineRange.GetEnd()) |
| 2453 | { |
| 2454 | // If this is the last point in the line, and we're forcing the |
| 2455 | // returned value to be the start of the next line, do the required |
| 2456 | // thing. |
| 2457 | if (index == lineRange.GetEnd() && forceLineStart) |
| 2458 | { |
| 2459 | if (node->GetNext()) |
| 2460 | { |
| 2461 | wxRichTextLine* nextLine = node->GetNext()->GetData(); |
| 2462 | *height = nextLine->GetSize().y; |
| 2463 | pt = nextLine->GetAbsolutePosition(); |
| 2464 | return true; |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | pt.y = line->GetPosition().y + GetPosition().y; |
| 2469 | |
| 2470 | wxRichTextRange r(lineRange.GetStart(), index); |
| 2471 | wxSize rangeSize; |
| 2472 | int descent = 0; |
| 2473 | |
| 2474 | // We find the size of the line up to this point, |
| 2475 | // then we can add this size to the line start position and |
| 2476 | // paragraph start position to find the actual position. |
| 2477 | |
| 2478 | if (GetRangeSize(r, rangeSize, descent, dc, wxRICHTEXT_UNFORMATTED)) |
| 2479 | { |
| 2480 | pt.x = line->GetPosition().x + GetPosition().x + rangeSize.x; |
| 2481 | *height = line->GetSize().y; |
| 2482 | |
| 2483 | return true; |
| 2484 | } |
| 2485 | |
| 2486 | } |
| 2487 | |
| 2488 | node = node->GetNext(); |
| 2489 | } |
| 2490 | |
| 2491 | return false; |
| 2492 | } |
| 2493 | |
| 2494 | /// Hit-testing: returns a flag indicating hit test details, plus |
| 2495 | /// information about position |
| 2496 | int wxRichTextParagraph::HitTest(wxDC& dc, const wxPoint& pt, long& textPosition) |
| 2497 | { |
| 2498 | wxPoint paraPos = GetPosition(); |
| 2499 | |
| 2500 | wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetFirst(); |
| 2501 | while (node) |
| 2502 | { |
| 2503 | wxRichTextLine* line = node->GetData(); |
| 2504 | wxPoint linePos = paraPos + line->GetPosition(); |
| 2505 | wxSize lineSize = line->GetSize(); |
| 2506 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 2507 | |
| 2508 | if (pt.y >= linePos.y && pt.y <= linePos.y + lineSize.y) |
| 2509 | { |
| 2510 | if (pt.x < linePos.x) |
| 2511 | { |
| 2512 | textPosition = lineRange.GetStart(); |
| 2513 | return wxRICHTEXT_HITTEST_BEFORE; |
| 2514 | } |
| 2515 | else if (pt.x >= (linePos.x + lineSize.x)) |
| 2516 | { |
| 2517 | textPosition = lineRange.GetEnd(); |
| 2518 | return wxRICHTEXT_HITTEST_AFTER; |
| 2519 | } |
| 2520 | else |
| 2521 | { |
| 2522 | long i; |
| 2523 | int lastX = linePos.x; |
| 2524 | for (i = lineRange.GetStart(); i <= lineRange.GetEnd(); i++) |
| 2525 | { |
| 2526 | wxSize childSize; |
| 2527 | int descent = 0; |
| 2528 | |
| 2529 | wxRichTextRange rangeToUse(lineRange.GetStart(), i); |
| 2530 | |
| 2531 | GetRangeSize(rangeToUse, childSize, descent, dc, wxRICHTEXT_UNFORMATTED); |
| 2532 | |
| 2533 | int nextX = childSize.x + linePos.x; |
| 2534 | |
| 2535 | if (pt.x >= lastX && pt.x <= nextX) |
| 2536 | { |
| 2537 | textPosition = i; |
| 2538 | |
| 2539 | // So now we know it's between i-1 and i. |
| 2540 | // Let's see if we can be more precise about |
| 2541 | // which side of the position it's on. |
| 2542 | |
| 2543 | int midPoint = (nextX - lastX)/2 + lastX; |
| 2544 | if (pt.x >= midPoint) |
| 2545 | return wxRICHTEXT_HITTEST_AFTER; |
| 2546 | else |
| 2547 | return wxRICHTEXT_HITTEST_BEFORE; |
| 2548 | } |
| 2549 | else |
| 2550 | { |
| 2551 | lastX = nextX; |
| 2552 | } |
| 2553 | } |
| 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | node = node->GetNext(); |
| 2558 | } |
| 2559 | |
| 2560 | return wxRICHTEXT_HITTEST_NONE; |
| 2561 | } |
| 2562 | |
| 2563 | /// Split an object at this position if necessary, and return |
| 2564 | /// the previous object, or NULL if inserting at beginning. |
| 2565 | wxRichTextObject* wxRichTextParagraph::SplitAt(long pos, wxRichTextObject** previousObject) |
| 2566 | { |
| 2567 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 2568 | while (node) |
| 2569 | { |
| 2570 | wxRichTextObject* child = node->GetData(); |
| 2571 | |
| 2572 | if (pos == child->GetRange().GetStart()) |
| 2573 | { |
| 2574 | if (previousObject) |
| 2575 | { |
| 2576 | if (node->GetPrevious()) |
| 2577 | *previousObject = node->GetPrevious()->GetData(); |
| 2578 | else |
| 2579 | *previousObject = NULL; |
| 2580 | } |
| 2581 | |
| 2582 | return child; |
| 2583 | } |
| 2584 | |
| 2585 | if (child->GetRange().Contains(pos)) |
| 2586 | { |
| 2587 | // This should create a new object, transferring part of |
| 2588 | // the content to the old object and the rest to the new object. |
| 2589 | wxRichTextObject* newObject = child->DoSplit(pos); |
| 2590 | |
| 2591 | // If we couldn't split this object, just insert in front of it. |
| 2592 | if (!newObject) |
| 2593 | { |
| 2594 | // Maybe this is an empty string, try the next one |
| 2595 | // return child; |
| 2596 | } |
| 2597 | else |
| 2598 | { |
| 2599 | // Insert the new object after 'child' |
| 2600 | if (node->GetNext()) |
| 2601 | m_children.Insert(node->GetNext(), newObject); |
| 2602 | else |
| 2603 | m_children.Append(newObject); |
| 2604 | newObject->SetParent(this); |
| 2605 | |
| 2606 | if (previousObject) |
| 2607 | *previousObject = child; |
| 2608 | |
| 2609 | return newObject; |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | node = node->GetNext(); |
| 2614 | } |
| 2615 | if (previousObject) |
| 2616 | *previousObject = NULL; |
| 2617 | return NULL; |
| 2618 | } |
| 2619 | |
| 2620 | /// Move content to a list from obj on |
| 2621 | void wxRichTextParagraph::MoveToList(wxRichTextObject* obj, wxList& list) |
| 2622 | { |
| 2623 | wxRichTextObjectList::compatibility_iterator node = m_children.Find(obj); |
| 2624 | while (node) |
| 2625 | { |
| 2626 | wxRichTextObject* child = node->GetData(); |
| 2627 | list.Append(child); |
| 2628 | |
| 2629 | wxRichTextObjectList::compatibility_iterator oldNode = node; |
| 2630 | |
| 2631 | node = node->GetNext(); |
| 2632 | |
| 2633 | m_children.DeleteNode(oldNode); |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | /// Add content back from list |
| 2638 | void wxRichTextParagraph::MoveFromList(wxList& list) |
| 2639 | { |
| 2640 | for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext()) |
| 2641 | { |
| 2642 | AppendChild((wxRichTextObject*) node->GetData()); |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | /// Calculate range |
| 2647 | void wxRichTextParagraph::CalculateRange(long start, long& end) |
| 2648 | { |
| 2649 | wxRichTextCompositeObject::CalculateRange(start, end); |
| 2650 | |
| 2651 | // Add one for end of paragraph |
| 2652 | end ++; |
| 2653 | |
| 2654 | m_range.SetRange(start, end); |
| 2655 | } |
| 2656 | |
| 2657 | /// Find the object at the given position |
| 2658 | wxRichTextObject* wxRichTextParagraph::FindObjectAtPosition(long position) |
| 2659 | { |
| 2660 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 2661 | while (node) |
| 2662 | { |
| 2663 | wxRichTextObject* obj = node->GetData(); |
| 2664 | if (obj->GetRange().Contains(position)) |
| 2665 | return obj; |
| 2666 | |
| 2667 | node = node->GetNext(); |
| 2668 | } |
| 2669 | return NULL; |
| 2670 | } |
| 2671 | |
| 2672 | /// Get the plain text searching from the start or end of the range. |
| 2673 | /// The resulting string may be shorter than the range given. |
| 2674 | bool wxRichTextParagraph::GetContiguousPlainText(wxString& text, const wxRichTextRange& range, bool fromStart) |
| 2675 | { |
| 2676 | text = wxEmptyString; |
| 2677 | |
| 2678 | if (fromStart) |
| 2679 | { |
| 2680 | wxRichTextObjectList::compatibility_iterator node = m_children.GetFirst(); |
| 2681 | while (node) |
| 2682 | { |
| 2683 | wxRichTextObject* obj = node->GetData(); |
| 2684 | if (!obj->GetRange().IsOutside(range)) |
| 2685 | { |
| 2686 | wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText); |
| 2687 | if (textObj) |
| 2688 | { |
| 2689 | text += textObj->GetTextForRange(range); |
| 2690 | } |
| 2691 | else |
| 2692 | return true; |
| 2693 | } |
| 2694 | |
| 2695 | node = node->GetNext(); |
| 2696 | } |
| 2697 | } |
| 2698 | else |
| 2699 | { |
| 2700 | wxRichTextObjectList::compatibility_iterator node = m_children.GetLast(); |
| 2701 | while (node) |
| 2702 | { |
| 2703 | wxRichTextObject* obj = node->GetData(); |
| 2704 | if (!obj->GetRange().IsOutside(range)) |
| 2705 | { |
| 2706 | wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText); |
| 2707 | if (textObj) |
| 2708 | { |
| 2709 | text = textObj->GetTextForRange(range) + text; |
| 2710 | } |
| 2711 | else |
| 2712 | return true; |
| 2713 | } |
| 2714 | |
| 2715 | node = node->GetPrevious(); |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | return true; |
| 2720 | } |
| 2721 | |
| 2722 | /// Find a suitable wrap position. |
| 2723 | bool wxRichTextParagraph::FindWrapPosition(const wxRichTextRange& range, wxDC& dc, int availableSpace, long& wrapPosition) |
| 2724 | { |
| 2725 | // Find the first position where the line exceeds the available space. |
| 2726 | wxSize sz; |
| 2727 | long i; |
| 2728 | long breakPosition = range.GetEnd(); |
| 2729 | for (i = range.GetStart(); i <= range.GetEnd(); i++) |
| 2730 | { |
| 2731 | int descent = 0; |
| 2732 | GetRangeSize(wxRichTextRange(range.GetStart(), i), sz, descent, dc, wxRICHTEXT_UNFORMATTED); |
| 2733 | |
| 2734 | if (sz.x > availableSpace) |
| 2735 | { |
| 2736 | breakPosition = i-1; |
| 2737 | break; |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | // Now we know the last position on the line. |
| 2742 | // Let's try to find a word break. |
| 2743 | |
| 2744 | wxString plainText; |
| 2745 | if (GetContiguousPlainText(plainText, wxRichTextRange(range.GetStart(), breakPosition), false)) |
| 2746 | { |
| 2747 | int spacePos = plainText.Find(wxT(' '), true); |
| 2748 | if (spacePos != wxNOT_FOUND) |
| 2749 | { |
| 2750 | int positionsFromEndOfString = plainText.Length() - spacePos - 1; |
| 2751 | breakPosition = breakPosition - positionsFromEndOfString; |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | wrapPosition = breakPosition; |
| 2756 | |
| 2757 | return true; |
| 2758 | } |
| 2759 | |
| 2760 | /// Get the bullet text for this paragraph. |
| 2761 | wxString wxRichTextParagraph::GetBulletText() |
| 2762 | { |
| 2763 | if (GetAttributes().GetBulletStyle() == wxTEXT_ATTR_BULLET_STYLE_NONE || |
| 2764 | (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_BITMAP)) |
| 2765 | return wxEmptyString; |
| 2766 | |
| 2767 | int number = GetAttributes().GetBulletNumber(); |
| 2768 | |
| 2769 | wxString text; |
| 2770 | if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ARABIC) |
| 2771 | { |
| 2772 | text.Printf(wxT("%d"), number); |
| 2773 | } |
| 2774 | else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER) |
| 2775 | { |
| 2776 | // TODO: Unicode, and also check if number > 26 |
| 2777 | text.Printf(wxT("%c"), (wxChar) (number+64)); |
| 2778 | } |
| 2779 | else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER) |
| 2780 | { |
| 2781 | // TODO: Unicode, and also check if number > 26 |
| 2782 | text.Printf(wxT("%c"), (wxChar) (number+96)); |
| 2783 | } |
| 2784 | else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER) |
| 2785 | { |
| 2786 | // TODO: convert from number to roman numeral |
| 2787 | if (number == 1) |
| 2788 | text = wxT("I"); |
| 2789 | else if (number == 2) |
| 2790 | text = wxT("II"); |
| 2791 | else if (number == 3) |
| 2792 | text = wxT("III"); |
| 2793 | else if (number == 4) |
| 2794 | text = wxT("IV"); |
| 2795 | else |
| 2796 | text = wxT("TODO"); |
| 2797 | } |
| 2798 | else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER) |
| 2799 | { |
| 2800 | // TODO: convert from number to roman numeral |
| 2801 | if (number == 1) |
| 2802 | text = wxT("i"); |
| 2803 | else if (number == 2) |
| 2804 | text = wxT("ii"); |
| 2805 | else if (number == 3) |
| 2806 | text = wxT("iii"); |
| 2807 | else if (number == 4) |
| 2808 | text = wxT("iv"); |
| 2809 | else |
| 2810 | text = wxT("TODO"); |
| 2811 | } |
| 2812 | else if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL) |
| 2813 | { |
| 2814 | text = GetAttributes().GetBulletSymbol(); |
| 2815 | } |
| 2816 | |
| 2817 | if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PARENTHESES) |
| 2818 | { |
| 2819 | text = wxT("(") + text + wxT(")"); |
| 2820 | } |
| 2821 | if (GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_PERIOD) |
| 2822 | { |
| 2823 | text += wxT("."); |
| 2824 | } |
| 2825 | |
| 2826 | return text; |
| 2827 | } |
| 2828 | |
| 2829 | /// Allocate or reuse a line object |
| 2830 | wxRichTextLine* wxRichTextParagraph::AllocateLine(int pos) |
| 2831 | { |
| 2832 | if (pos < (int) m_cachedLines.GetCount()) |
| 2833 | { |
| 2834 | wxRichTextLine* line = m_cachedLines.Item(pos)->GetData(); |
| 2835 | line->Init(this); |
| 2836 | return line; |
| 2837 | } |
| 2838 | else |
| 2839 | { |
| 2840 | wxRichTextLine* line = new wxRichTextLine(this); |
| 2841 | m_cachedLines.Append(line); |
| 2842 | return line; |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | /// Clear remaining unused line objects, if any |
| 2847 | bool wxRichTextParagraph::ClearUnusedLines(int lineCount) |
| 2848 | { |
| 2849 | int cachedLineCount = m_cachedLines.GetCount(); |
| 2850 | if ((int) cachedLineCount > lineCount) |
| 2851 | { |
| 2852 | for (int i = 0; i < (int) (cachedLineCount - lineCount); i ++) |
| 2853 | { |
| 2854 | wxRichTextLineList::compatibility_iterator node = m_cachedLines.GetLast(); |
| 2855 | wxRichTextLine* line = node->GetData(); |
| 2856 | m_cachedLines.Erase(node); |
| 2857 | delete line; |
| 2858 | } |
| 2859 | } |
| 2860 | return true; |
| 2861 | } |
| 2862 | |
| 2863 | |
| 2864 | /*! |
| 2865 | * wxRichTextLine |
| 2866 | * This object represents a line in a paragraph, and stores |
| 2867 | * offsets from the start of the paragraph representing the |
| 2868 | * start and end positions of the line. |
| 2869 | */ |
| 2870 | |
| 2871 | wxRichTextLine::wxRichTextLine(wxRichTextParagraph* parent) |
| 2872 | { |
| 2873 | Init(parent); |
| 2874 | } |
| 2875 | |
| 2876 | /// Initialisation |
| 2877 | void wxRichTextLine::Init(wxRichTextParagraph* parent) |
| 2878 | { |
| 2879 | m_parent = parent; |
| 2880 | m_range.SetRange(-1, -1); |
| 2881 | m_pos = wxPoint(0, 0); |
| 2882 | m_size = wxSize(0, 0); |
| 2883 | m_descent = 0; |
| 2884 | } |
| 2885 | |
| 2886 | /// Copy |
| 2887 | void wxRichTextLine::Copy(const wxRichTextLine& obj) |
| 2888 | { |
| 2889 | m_range = obj.m_range; |
| 2890 | } |
| 2891 | |
| 2892 | /// Get the absolute object position |
| 2893 | wxPoint wxRichTextLine::GetAbsolutePosition() const |
| 2894 | { |
| 2895 | return m_parent->GetPosition() + m_pos; |
| 2896 | } |
| 2897 | |
| 2898 | /// Get the absolute range |
| 2899 | wxRichTextRange wxRichTextLine::GetAbsoluteRange() const |
| 2900 | { |
| 2901 | wxRichTextRange range(m_range.GetStart() + m_parent->GetRange().GetStart(), 0); |
| 2902 | range.SetEnd(range.GetStart() + m_range.GetLength()-1); |
| 2903 | return range; |
| 2904 | } |
| 2905 | |
| 2906 | /*! |
| 2907 | * wxRichTextPlainText |
| 2908 | * This object represents a single piece of text. |
| 2909 | */ |
| 2910 | |
| 2911 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextPlainText, wxRichTextObject) |
| 2912 | |
| 2913 | wxRichTextPlainText::wxRichTextPlainText(const wxString& text, wxRichTextObject* parent, wxTextAttrEx* style): |
| 2914 | wxRichTextObject(parent) |
| 2915 | { |
| 2916 | if (parent && !style) |
| 2917 | SetAttributes(parent->GetAttributes()); |
| 2918 | if (style) |
| 2919 | SetAttributes(*style); |
| 2920 | |
| 2921 | m_text = text; |
| 2922 | } |
| 2923 | |
| 2924 | /// Draw the item |
| 2925 | bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int descent, int WXUNUSED(style)) |
| 2926 | { |
| 2927 | int offset = GetRange().GetStart(); |
| 2928 | |
| 2929 | long len = range.GetLength(); |
| 2930 | wxString stringChunk = m_text.Mid(range.GetStart() - offset, (size_t) len); |
| 2931 | |
| 2932 | int charHeight = dc.GetCharHeight(); |
| 2933 | |
| 2934 | int x = rect.x; |
| 2935 | int y = rect.y + (rect.height - charHeight - (descent - m_descent)); |
| 2936 | |
| 2937 | // Test for the optimized situations where all is selected, or none |
| 2938 | // is selected. |
| 2939 | |
| 2940 | if (GetAttributes().GetFont().Ok()) |
| 2941 | dc.SetFont(GetAttributes().GetFont()); |
| 2942 | |
| 2943 | // (a) All selected. |
| 2944 | if (selectionRange.GetStart() <= range.GetStart() && selectionRange.GetEnd() >= range.GetEnd()) |
| 2945 | { |
| 2946 | // Draw all selected |
| 2947 | dc.SetBrush(*wxBLACK_BRUSH); |
| 2948 | dc.SetPen(*wxBLACK_PEN); |
| 2949 | wxCoord w, h; |
| 2950 | dc.GetTextExtent(stringChunk, & w, & h); |
| 2951 | wxRect selRect(x, rect.y, w, rect.GetHeight()); |
| 2952 | dc.DrawRectangle(selRect); |
| 2953 | dc.SetTextForeground(*wxWHITE); |
| 2954 | dc.SetBackgroundMode(wxTRANSPARENT); |
| 2955 | dc.DrawText(stringChunk, x, y); |
| 2956 | } |
| 2957 | // (b) None selected. |
| 2958 | else if (selectionRange.GetEnd() < range.GetStart() || selectionRange.GetStart() > range.GetEnd()) |
| 2959 | { |
| 2960 | // Draw all unselected |
| 2961 | dc.SetTextForeground(GetAttributes().GetTextColour()); |
| 2962 | dc.SetBackgroundMode(wxTRANSPARENT); |
| 2963 | dc.DrawText(stringChunk, x, y); |
| 2964 | } |
| 2965 | else |
| 2966 | { |
| 2967 | // (c) Part selected, part not |
| 2968 | // Let's draw unselected chunk, selected chunk, then unselected chunk. |
| 2969 | |
| 2970 | dc.SetBackgroundMode(wxTRANSPARENT); |
| 2971 | |
| 2972 | // 1. Initial unselected chunk, if any, up until start of selection. |
| 2973 | if (selectionRange.GetStart() > range.GetStart() && selectionRange.GetStart() <= range.GetEnd()) |
| 2974 | { |
| 2975 | int r1 = range.GetStart(); |
| 2976 | int s1 = selectionRange.GetStart()-1; |
| 2977 | int fragmentLen = s1 - r1 + 1; |
| 2978 | if (fragmentLen < 0) |
| 2979 | wxLogDebug(wxT("Mid(%d, %d"), (int)(r1 - offset), (int)fragmentLen); |
| 2980 | wxString stringFragment = m_text.Mid(r1 - offset, fragmentLen); |
| 2981 | |
| 2982 | dc.SetTextForeground(GetAttributes().GetTextColour()); |
| 2983 | dc.DrawText(stringFragment, x, y); |
| 2984 | |
| 2985 | wxCoord w, h; |
| 2986 | dc.GetTextExtent(stringFragment, & w, & h); |
| 2987 | x += w; |
| 2988 | } |
| 2989 | |
| 2990 | // 2. Selected chunk, if any. |
| 2991 | if (selectionRange.GetEnd() >= range.GetStart()) |
| 2992 | { |
| 2993 | int s1 = wxMax(selectionRange.GetStart(), range.GetStart()); |
| 2994 | int s2 = wxMin(selectionRange.GetEnd(), range.GetEnd()); |
| 2995 | |
| 2996 | int fragmentLen = s2 - s1 + 1; |
| 2997 | if (fragmentLen < 0) |
| 2998 | wxLogDebug(wxT("Mid(%d, %d"), (int)(s1 - offset), (int)fragmentLen); |
| 2999 | wxString stringFragment = m_text.Mid(s1 - offset, fragmentLen); |
| 3000 | |
| 3001 | wxCoord w, h; |
| 3002 | dc.GetTextExtent(stringFragment, & w, & h); |
| 3003 | wxRect selRect(x, rect.y, w, rect.GetHeight()); |
| 3004 | |
| 3005 | dc.SetBrush(*wxBLACK_BRUSH); |
| 3006 | dc.SetPen(*wxBLACK_PEN); |
| 3007 | dc.DrawRectangle(selRect); |
| 3008 | dc.SetTextForeground(*wxWHITE); |
| 3009 | dc.DrawText(stringFragment, x, y); |
| 3010 | |
| 3011 | x += w; |
| 3012 | } |
| 3013 | |
| 3014 | // 3. Remaining unselected chunk, if any |
| 3015 | if (selectionRange.GetEnd() < range.GetEnd()) |
| 3016 | { |
| 3017 | int s2 = wxMin(selectionRange.GetEnd()+1, range.GetEnd()); |
| 3018 | int r2 = range.GetEnd(); |
| 3019 | |
| 3020 | int fragmentLen = r2 - s2 + 1; |
| 3021 | if (fragmentLen < 0) |
| 3022 | wxLogDebug(wxT("Mid(%d, %d"), (int)(s2 - offset), (int)fragmentLen); |
| 3023 | wxString stringFragment = m_text.Mid(s2 - offset, fragmentLen); |
| 3024 | |
| 3025 | dc.SetTextForeground(GetAttributes().GetTextColour()); |
| 3026 | dc.DrawText(stringFragment, x, y); |
| 3027 | } |
| 3028 | } |
| 3029 | |
| 3030 | return true; |
| 3031 | } |
| 3032 | |
| 3033 | /// Lay the item out |
| 3034 | bool wxRichTextPlainText::Layout(wxDC& dc, const wxRect& WXUNUSED(rect), int WXUNUSED(style)) |
| 3035 | { |
| 3036 | if (GetAttributes().GetFont().Ok()) |
| 3037 | dc.SetFont(GetAttributes().GetFont()); |
| 3038 | |
| 3039 | wxCoord w, h; |
| 3040 | dc.GetTextExtent(m_text, & w, & h, & m_descent); |
| 3041 | m_size = wxSize(w, dc.GetCharHeight()); |
| 3042 | |
| 3043 | return true; |
| 3044 | } |
| 3045 | |
| 3046 | /// Copy |
| 3047 | void wxRichTextPlainText::Copy(const wxRichTextPlainText& obj) |
| 3048 | { |
| 3049 | wxRichTextObject::Copy(obj); |
| 3050 | |
| 3051 | m_text = obj.m_text; |
| 3052 | } |
| 3053 | |
| 3054 | /// Get/set the object size for the given range. Returns false if the range |
| 3055 | /// is invalid for this object. |
| 3056 | bool wxRichTextPlainText::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& descent, wxDC& dc, int WXUNUSED(flags)) const |
| 3057 | { |
| 3058 | if (!range.IsWithin(GetRange())) |
| 3059 | return false; |
| 3060 | |
| 3061 | // Always assume unformatted text, since at this level we have no knowledge |
| 3062 | // of line breaks - and we don't need it, since we'll calculate size within |
| 3063 | // formatted text by doing it in chunks according to the line ranges |
| 3064 | |
| 3065 | if (GetAttributes().GetFont().Ok()) |
| 3066 | dc.SetFont(GetAttributes().GetFont()); |
| 3067 | |
| 3068 | int startPos = range.GetStart() - GetRange().GetStart(); |
| 3069 | long len = range.GetLength(); |
| 3070 | wxString stringChunk = m_text.Mid(startPos, (size_t) len); |
| 3071 | wxCoord w, h; |
| 3072 | dc.GetTextExtent(stringChunk, & w, & h, & descent); |
| 3073 | size = wxSize(w, dc.GetCharHeight()); |
| 3074 | |
| 3075 | return true; |
| 3076 | } |
| 3077 | |
| 3078 | /// Do a split, returning an object containing the second part, and setting |
| 3079 | /// the first part in 'this'. |
| 3080 | wxRichTextObject* wxRichTextPlainText::DoSplit(long pos) |
| 3081 | { |
| 3082 | int index = pos - GetRange().GetStart(); |
| 3083 | if (index < 0 || index >= (int) m_text.Length()) |
| 3084 | return NULL; |
| 3085 | |
| 3086 | wxString firstPart = m_text.Mid(0, index); |
| 3087 | wxString secondPart = m_text.Mid(index); |
| 3088 | |
| 3089 | m_text = firstPart; |
| 3090 | |
| 3091 | wxRichTextPlainText* newObject = new wxRichTextPlainText(secondPart); |
| 3092 | newObject->SetAttributes(GetAttributes()); |
| 3093 | |
| 3094 | newObject->SetRange(wxRichTextRange(pos, GetRange().GetEnd())); |
| 3095 | GetRange().SetEnd(pos-1); |
| 3096 | |
| 3097 | return newObject; |
| 3098 | } |
| 3099 | |
| 3100 | /// Calculate range |
| 3101 | void wxRichTextPlainText::CalculateRange(long start, long& end) |
| 3102 | { |
| 3103 | end = start + m_text.Length() - 1; |
| 3104 | m_range.SetRange(start, end); |
| 3105 | } |
| 3106 | |
| 3107 | /// Delete range |
| 3108 | bool wxRichTextPlainText::DeleteRange(const wxRichTextRange& range) |
| 3109 | { |
| 3110 | wxRichTextRange r = range; |
| 3111 | |
| 3112 | r.LimitTo(GetRange()); |
| 3113 | |
| 3114 | if (r.GetStart() == GetRange().GetStart() && r.GetEnd() == GetRange().GetEnd()) |
| 3115 | { |
| 3116 | m_text.Empty(); |
| 3117 | return true; |
| 3118 | } |
| 3119 | |
| 3120 | long startIndex = r.GetStart() - GetRange().GetStart(); |
| 3121 | long len = r.GetLength(); |
| 3122 | |
| 3123 | m_text = m_text.Mid(0, startIndex) + m_text.Mid(startIndex+len); |
| 3124 | return true; |
| 3125 | } |
| 3126 | |
| 3127 | /// Get text for the given range. |
| 3128 | wxString wxRichTextPlainText::GetTextForRange(const wxRichTextRange& range) const |
| 3129 | { |
| 3130 | wxRichTextRange r = range; |
| 3131 | |
| 3132 | r.LimitTo(GetRange()); |
| 3133 | |
| 3134 | long startIndex = r.GetStart() - GetRange().GetStart(); |
| 3135 | long len = r.GetLength(); |
| 3136 | |
| 3137 | return m_text.Mid(startIndex, len); |
| 3138 | } |
| 3139 | |
| 3140 | /// Returns true if this object can merge itself with the given one. |
| 3141 | bool wxRichTextPlainText::CanMerge(wxRichTextObject* object) const |
| 3142 | { |
| 3143 | return object->GetClassInfo() == CLASSINFO(wxRichTextPlainText) && |
| 3144 | (m_text.empty() || wxTextAttrEq(GetAttributes(), object->GetAttributes())); |
| 3145 | } |
| 3146 | |
| 3147 | /// Returns true if this object merged itself with the given one. |
| 3148 | /// The calling code will then delete the given object. |
| 3149 | bool wxRichTextPlainText::Merge(wxRichTextObject* object) |
| 3150 | { |
| 3151 | wxRichTextPlainText* textObject = wxDynamicCast(object, wxRichTextPlainText); |
| 3152 | wxASSERT( textObject != NULL ); |
| 3153 | |
| 3154 | if (textObject) |
| 3155 | { |
| 3156 | m_text += textObject->GetText(); |
| 3157 | return true; |
| 3158 | } |
| 3159 | else |
| 3160 | return false; |
| 3161 | } |
| 3162 | |
| 3163 | /// Dump to output stream for debugging |
| 3164 | void wxRichTextPlainText::Dump(wxTextOutputStream& stream) |
| 3165 | { |
| 3166 | wxRichTextObject::Dump(stream); |
| 3167 | stream << m_text << wxT("\n"); |
| 3168 | } |
| 3169 | |
| 3170 | /*! |
| 3171 | * wxRichTextBuffer |
| 3172 | * This is a kind of box, used to represent the whole buffer |
| 3173 | */ |
| 3174 | |
| 3175 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextBuffer, wxRichTextParagraphLayoutBox) |
| 3176 | |
| 3177 | wxList wxRichTextBuffer::sm_handlers; |
| 3178 | |
| 3179 | /// Initialisation |
| 3180 | void wxRichTextBuffer::Init() |
| 3181 | { |
| 3182 | m_commandProcessor = new wxCommandProcessor; |
| 3183 | m_styleSheet = NULL; |
| 3184 | m_modified = false; |
| 3185 | m_batchedCommandDepth = 0; |
| 3186 | m_batchedCommand = NULL; |
| 3187 | m_suppressUndo = 0; |
| 3188 | } |
| 3189 | |
| 3190 | /// Initialisation |
| 3191 | wxRichTextBuffer::~wxRichTextBuffer() |
| 3192 | { |
| 3193 | delete m_commandProcessor; |
| 3194 | delete m_batchedCommand; |
| 3195 | |
| 3196 | ClearStyleStack(); |
| 3197 | } |
| 3198 | |
| 3199 | void wxRichTextBuffer::Clear() |
| 3200 | { |
| 3201 | DeleteChildren(); |
| 3202 | GetCommandProcessor()->ClearCommands(); |
| 3203 | Modify(false); |
| 3204 | Invalidate(wxRICHTEXT_ALL); |
| 3205 | } |
| 3206 | |
| 3207 | void wxRichTextBuffer::Reset() |
| 3208 | { |
| 3209 | DeleteChildren(); |
| 3210 | AddParagraph(wxEmptyString); |
| 3211 | GetCommandProcessor()->ClearCommands(); |
| 3212 | Modify(false); |
| 3213 | Invalidate(wxRICHTEXT_ALL); |
| 3214 | } |
| 3215 | |
| 3216 | /// Submit command to insert the given text |
| 3217 | bool wxRichTextBuffer::InsertTextWithUndo(long pos, const wxString& text, wxRichTextCtrl* ctrl) |
| 3218 | { |
| 3219 | wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false); |
| 3220 | |
| 3221 | action->GetNewParagraphs().AddParagraphs(text); |
| 3222 | if (action->GetNewParagraphs().GetChildCount() == 1) |
| 3223 | action->GetNewParagraphs().SetPartialParagraph(true); |
| 3224 | |
| 3225 | action->SetPosition(pos); |
| 3226 | |
| 3227 | // Set the range we'll need to delete in Undo |
| 3228 | action->SetRange(wxRichTextRange(pos, pos + text.Length() - 1)); |
| 3229 | |
| 3230 | SubmitAction(action); |
| 3231 | |
| 3232 | return true; |
| 3233 | } |
| 3234 | |
| 3235 | /// Submit command to insert the given text |
| 3236 | bool wxRichTextBuffer::InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl) |
| 3237 | { |
| 3238 | wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Text"), wxRICHTEXT_INSERT, this, ctrl, false); |
| 3239 | |
| 3240 | wxTextAttrEx attr(GetBasicStyle()); |
| 3241 | wxRichTextApplyStyle(attr, GetDefaultStyle()); |
| 3242 | |
| 3243 | wxRichTextParagraph* newPara = new wxRichTextParagraph(wxEmptyString, this, & attr); |
| 3244 | action->GetNewParagraphs().AppendChild(newPara); |
| 3245 | action->GetNewParagraphs().UpdateRanges(); |
| 3246 | action->GetNewParagraphs().SetPartialParagraph(false); |
| 3247 | action->SetPosition(pos); |
| 3248 | |
| 3249 | // Set the range we'll need to delete in Undo |
| 3250 | action->SetRange(wxRichTextRange(pos, pos)); |
| 3251 | |
| 3252 | SubmitAction(action); |
| 3253 | |
| 3254 | return true; |
| 3255 | } |
| 3256 | |
| 3257 | /// Submit command to insert the given image |
| 3258 | bool wxRichTextBuffer::InsertImageWithUndo(long pos, const wxRichTextImageBlock& imageBlock, wxRichTextCtrl* ctrl) |
| 3259 | { |
| 3260 | wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, ctrl, false); |
| 3261 | |
| 3262 | wxTextAttrEx attr(GetBasicStyle()); |
| 3263 | wxRichTextApplyStyle(attr, GetDefaultStyle()); |
| 3264 | |
| 3265 | wxRichTextParagraph* newPara = new wxRichTextParagraph(this, & attr); |
| 3266 | wxRichTextImage* imageObject = new wxRichTextImage(imageBlock, newPara); |
| 3267 | newPara->AppendChild(imageObject); |
| 3268 | action->GetNewParagraphs().AppendChild(newPara); |
| 3269 | action->GetNewParagraphs().UpdateRanges(); |
| 3270 | |
| 3271 | action->GetNewParagraphs().SetPartialParagraph(true); |
| 3272 | |
| 3273 | action->SetPosition(pos); |
| 3274 | |
| 3275 | // Set the range we'll need to delete in Undo |
| 3276 | action->SetRange(wxRichTextRange(pos, pos)); |
| 3277 | |
| 3278 | SubmitAction(action); |
| 3279 | |
| 3280 | return true; |
| 3281 | } |
| 3282 | |
| 3283 | /// Submit command to delete this range |
| 3284 | bool wxRichTextBuffer::DeleteRangeWithUndo(const wxRichTextRange& range, long initialCaretPosition, long WXUNUSED(newCaretPositon), wxRichTextCtrl* ctrl) |
| 3285 | { |
| 3286 | wxRichTextAction* action = new wxRichTextAction(NULL, _("Delete"), wxRICHTEXT_DELETE, this, ctrl); |
| 3287 | |
| 3288 | action->SetPosition(initialCaretPosition); |
| 3289 | |
| 3290 | // Set the range to delete |
| 3291 | action->SetRange(range); |
| 3292 | |
| 3293 | // Copy the fragment that we'll need to restore in Undo |
| 3294 | CopyFragment(range, action->GetOldParagraphs()); |
| 3295 | |
| 3296 | // Special case: if there is only one (non-partial) paragraph, |
| 3297 | // we must save the *next* paragraph's style, because that |
| 3298 | // is the style we must apply when inserting the content back |
| 3299 | // when undoing the delete. (This is because we're merging the |
| 3300 | // paragraph with the previous paragraph and throwing away |
| 3301 | // the style, and we need to restore it.) |
| 3302 | if (!action->GetOldParagraphs().GetPartialParagraph() && action->GetOldParagraphs().GetChildCount() == 1) |
| 3303 | { |
| 3304 | wxRichTextParagraph* lastPara = GetParagraphAtPosition(range.GetStart()); |
| 3305 | if (lastPara) |
| 3306 | { |
| 3307 | wxRichTextParagraph* nextPara = GetParagraphAtPosition(range.GetEnd()+1); |
| 3308 | if (nextPara) |
| 3309 | { |
| 3310 | wxRichTextParagraph* para = (wxRichTextParagraph*) action->GetOldParagraphs().GetChild(0); |
| 3311 | para->SetAttributes(nextPara->GetAttributes()); |
| 3312 | } |
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | SubmitAction(action); |
| 3317 | |
| 3318 | return true; |
| 3319 | } |
| 3320 | |
| 3321 | /// Collapse undo/redo commands |
| 3322 | bool wxRichTextBuffer::BeginBatchUndo(const wxString& cmdName) |
| 3323 | { |
| 3324 | if (m_batchedCommandDepth == 0) |
| 3325 | { |
| 3326 | wxASSERT(m_batchedCommand == NULL); |
| 3327 | if (m_batchedCommand) |
| 3328 | { |
| 3329 | GetCommandProcessor()->Submit(m_batchedCommand); |
| 3330 | } |
| 3331 | m_batchedCommand = new wxRichTextCommand(cmdName); |
| 3332 | } |
| 3333 | |
| 3334 | m_batchedCommandDepth ++; |
| 3335 | |
| 3336 | return true; |
| 3337 | } |
| 3338 | |
| 3339 | /// Collapse undo/redo commands |
| 3340 | bool wxRichTextBuffer::EndBatchUndo() |
| 3341 | { |
| 3342 | m_batchedCommandDepth --; |
| 3343 | |
| 3344 | wxASSERT(m_batchedCommandDepth >= 0); |
| 3345 | wxASSERT(m_batchedCommand != NULL); |
| 3346 | |
| 3347 | if (m_batchedCommandDepth == 0) |
| 3348 | { |
| 3349 | GetCommandProcessor()->Submit(m_batchedCommand); |
| 3350 | m_batchedCommand = NULL; |
| 3351 | } |
| 3352 | |
| 3353 | return true; |
| 3354 | } |
| 3355 | |
| 3356 | /// Submit immediately, or delay according to whether collapsing is on |
| 3357 | bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action) |
| 3358 | { |
| 3359 | if (BatchingUndo() && m_batchedCommand && !SuppressingUndo()) |
| 3360 | m_batchedCommand->AddAction(action); |
| 3361 | else |
| 3362 | { |
| 3363 | wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName()); |
| 3364 | cmd->AddAction(action); |
| 3365 | |
| 3366 | // Only store it if we're not suppressing undo. |
| 3367 | return GetCommandProcessor()->Submit(cmd, !SuppressingUndo()); |
| 3368 | } |
| 3369 | |
| 3370 | return true; |
| 3371 | } |
| 3372 | |
| 3373 | /// Begin suppressing undo/redo commands. |
| 3374 | bool wxRichTextBuffer::BeginSuppressUndo() |
| 3375 | { |
| 3376 | m_suppressUndo ++; |
| 3377 | |
| 3378 | return true; |
| 3379 | } |
| 3380 | |
| 3381 | /// End suppressing undo/redo commands. |
| 3382 | bool wxRichTextBuffer::EndSuppressUndo() |
| 3383 | { |
| 3384 | m_suppressUndo --; |
| 3385 | |
| 3386 | return true; |
| 3387 | } |
| 3388 | |
| 3389 | /// Begin using a style |
| 3390 | bool wxRichTextBuffer::BeginStyle(const wxTextAttrEx& style) |
| 3391 | { |
| 3392 | wxTextAttrEx newStyle(GetDefaultStyle()); |
| 3393 | |
| 3394 | // Save the old default style |
| 3395 | m_attributeStack.Append((wxObject*) new wxTextAttrEx(GetDefaultStyle())); |
| 3396 | |
| 3397 | wxRichTextApplyStyle(newStyle, style); |
| 3398 | newStyle.SetFlags(style.GetFlags()|newStyle.GetFlags()); |
| 3399 | |
| 3400 | SetDefaultStyle(newStyle); |
| 3401 | |
| 3402 | // wxLogDebug("Default style size = %d", GetDefaultStyle().GetFont().GetPointSize()); |
| 3403 | |
| 3404 | return true; |
| 3405 | } |
| 3406 | |
| 3407 | /// End the style |
| 3408 | bool wxRichTextBuffer::EndStyle() |
| 3409 | { |
| 3410 | if (!m_attributeStack.GetFirst()) |
| 3411 | { |
| 3412 | wxLogDebug(_("Too many EndStyle calls!")); |
| 3413 | return false; |
| 3414 | } |
| 3415 | |
| 3416 | wxList::compatibility_iterator node = m_attributeStack.GetLast(); |
| 3417 | wxTextAttrEx* attr = (wxTextAttrEx*)node->GetData(); |
| 3418 | m_attributeStack.Erase(node); |
| 3419 | |
| 3420 | SetDefaultStyle(*attr); |
| 3421 | |
| 3422 | delete attr; |
| 3423 | return true; |
| 3424 | } |
| 3425 | |
| 3426 | /// End all styles |
| 3427 | bool wxRichTextBuffer::EndAllStyles() |
| 3428 | { |
| 3429 | while (m_attributeStack.GetCount() != 0) |
| 3430 | EndStyle(); |
| 3431 | return true; |
| 3432 | } |
| 3433 | |
| 3434 | /// Clear the style stack |
| 3435 | void wxRichTextBuffer::ClearStyleStack() |
| 3436 | { |
| 3437 | for (wxList::compatibility_iterator node = m_attributeStack.GetFirst(); node; node = node->GetNext()) |
| 3438 | delete (wxTextAttrEx*) node->GetData(); |
| 3439 | m_attributeStack.Clear(); |
| 3440 | } |
| 3441 | |
| 3442 | /// Begin using bold |
| 3443 | bool wxRichTextBuffer::BeginBold() |
| 3444 | { |
| 3445 | wxFont font(GetBasicStyle().GetFont()); |
| 3446 | font.SetWeight(wxBOLD); |
| 3447 | |
| 3448 | wxTextAttrEx attr; |
| 3449 | attr.SetFont(font,wxTEXT_ATTR_FONT_WEIGHT); |
| 3450 | |
| 3451 | return BeginStyle(attr); |
| 3452 | } |
| 3453 | |
| 3454 | /// Begin using italic |
| 3455 | bool wxRichTextBuffer::BeginItalic() |
| 3456 | { |
| 3457 | wxFont font(GetBasicStyle().GetFont()); |
| 3458 | font.SetStyle(wxITALIC); |
| 3459 | |
| 3460 | wxTextAttrEx attr; |
| 3461 | attr.SetFont(font, wxTEXT_ATTR_FONT_ITALIC); |
| 3462 | |
| 3463 | return BeginStyle(attr); |
| 3464 | } |
| 3465 | |
| 3466 | /// Begin using underline |
| 3467 | bool wxRichTextBuffer::BeginUnderline() |
| 3468 | { |
| 3469 | wxFont font(GetBasicStyle().GetFont()); |
| 3470 | font.SetUnderlined(true); |
| 3471 | |
| 3472 | wxTextAttrEx attr; |
| 3473 | attr.SetFont(font, wxTEXT_ATTR_FONT_UNDERLINE); |
| 3474 | |
| 3475 | return BeginStyle(attr); |
| 3476 | } |
| 3477 | |
| 3478 | /// Begin using point size |
| 3479 | bool wxRichTextBuffer::BeginFontSize(int pointSize) |
| 3480 | { |
| 3481 | wxFont font(GetBasicStyle().GetFont()); |
| 3482 | font.SetPointSize(pointSize); |
| 3483 | |
| 3484 | wxTextAttrEx attr; |
| 3485 | attr.SetFont(font, wxTEXT_ATTR_FONT_SIZE); |
| 3486 | |
| 3487 | return BeginStyle(attr); |
| 3488 | } |
| 3489 | |
| 3490 | /// Begin using this font |
| 3491 | bool wxRichTextBuffer::BeginFont(const wxFont& font) |
| 3492 | { |
| 3493 | wxTextAttrEx attr; |
| 3494 | attr.SetFlags(wxTEXT_ATTR_FONT); |
| 3495 | attr.SetFont(font); |
| 3496 | |
| 3497 | return BeginStyle(attr); |
| 3498 | } |
| 3499 | |
| 3500 | /// Begin using this colour |
| 3501 | bool wxRichTextBuffer::BeginTextColour(const wxColour& colour) |
| 3502 | { |
| 3503 | wxTextAttrEx attr; |
| 3504 | attr.SetFlags(wxTEXT_ATTR_TEXT_COLOUR); |
| 3505 | attr.SetTextColour(colour); |
| 3506 | |
| 3507 | return BeginStyle(attr); |
| 3508 | } |
| 3509 | |
| 3510 | /// Begin using alignment |
| 3511 | bool wxRichTextBuffer::BeginAlignment(wxTextAttrAlignment alignment) |
| 3512 | { |
| 3513 | wxTextAttrEx attr; |
| 3514 | attr.SetFlags(wxTEXT_ATTR_ALIGNMENT); |
| 3515 | attr.SetAlignment(alignment); |
| 3516 | |
| 3517 | return BeginStyle(attr); |
| 3518 | } |
| 3519 | |
| 3520 | /// Begin left indent |
| 3521 | bool wxRichTextBuffer::BeginLeftIndent(int leftIndent, int leftSubIndent) |
| 3522 | { |
| 3523 | wxTextAttrEx attr; |
| 3524 | attr.SetFlags(wxTEXT_ATTR_LEFT_INDENT); |
| 3525 | attr.SetLeftIndent(leftIndent, leftSubIndent); |
| 3526 | |
| 3527 | return BeginStyle(attr); |
| 3528 | } |
| 3529 | |
| 3530 | /// Begin right indent |
| 3531 | bool wxRichTextBuffer::BeginRightIndent(int rightIndent) |
| 3532 | { |
| 3533 | wxTextAttrEx attr; |
| 3534 | attr.SetFlags(wxTEXT_ATTR_RIGHT_INDENT); |
| 3535 | attr.SetRightIndent(rightIndent); |
| 3536 | |
| 3537 | return BeginStyle(attr); |
| 3538 | } |
| 3539 | |
| 3540 | /// Begin paragraph spacing |
| 3541 | bool wxRichTextBuffer::BeginParagraphSpacing(int before, int after) |
| 3542 | { |
| 3543 | long flags = 0; |
| 3544 | if (before != 0) |
| 3545 | flags |= wxTEXT_ATTR_PARA_SPACING_BEFORE; |
| 3546 | if (after != 0) |
| 3547 | flags |= wxTEXT_ATTR_PARA_SPACING_AFTER; |
| 3548 | |
| 3549 | wxTextAttrEx attr; |
| 3550 | attr.SetFlags(flags); |
| 3551 | attr.SetParagraphSpacingBefore(before); |
| 3552 | attr.SetParagraphSpacingAfter(after); |
| 3553 | |
| 3554 | return BeginStyle(attr); |
| 3555 | } |
| 3556 | |
| 3557 | /// Begin line spacing |
| 3558 | bool wxRichTextBuffer::BeginLineSpacing(int lineSpacing) |
| 3559 | { |
| 3560 | wxTextAttrEx attr; |
| 3561 | attr.SetFlags(wxTEXT_ATTR_LINE_SPACING); |
| 3562 | attr.SetLineSpacing(lineSpacing); |
| 3563 | |
| 3564 | return BeginStyle(attr); |
| 3565 | } |
| 3566 | |
| 3567 | /// Begin numbered bullet |
| 3568 | bool wxRichTextBuffer::BeginNumberedBullet(int bulletNumber, int leftIndent, int leftSubIndent, int bulletStyle) |
| 3569 | { |
| 3570 | wxTextAttrEx attr; |
| 3571 | attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER|wxTEXT_ATTR_LEFT_INDENT); |
| 3572 | attr.SetBulletStyle(bulletStyle); |
| 3573 | attr.SetBulletNumber(bulletNumber); |
| 3574 | attr.SetLeftIndent(leftIndent, leftSubIndent); |
| 3575 | |
| 3576 | return BeginStyle(attr); |
| 3577 | } |
| 3578 | |
| 3579 | /// Begin symbol bullet |
| 3580 | bool wxRichTextBuffer::BeginSymbolBullet(wxChar symbol, int leftIndent, int leftSubIndent, int bulletStyle) |
| 3581 | { |
| 3582 | wxTextAttrEx attr; |
| 3583 | attr.SetFlags(wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_SYMBOL|wxTEXT_ATTR_LEFT_INDENT); |
| 3584 | attr.SetBulletStyle(bulletStyle); |
| 3585 | attr.SetLeftIndent(leftIndent, leftSubIndent); |
| 3586 | attr.SetBulletSymbol(symbol); |
| 3587 | |
| 3588 | return BeginStyle(attr); |
| 3589 | } |
| 3590 | |
| 3591 | /// Begin named character style |
| 3592 | bool wxRichTextBuffer::BeginCharacterStyle(const wxString& characterStyle) |
| 3593 | { |
| 3594 | if (GetStyleSheet()) |
| 3595 | { |
| 3596 | wxRichTextCharacterStyleDefinition* def = GetStyleSheet()->FindCharacterStyle(characterStyle); |
| 3597 | if (def) |
| 3598 | { |
| 3599 | wxTextAttrEx attr; |
| 3600 | def->GetStyle().CopyTo(attr); |
| 3601 | return BeginStyle(attr); |
| 3602 | } |
| 3603 | } |
| 3604 | return false; |
| 3605 | } |
| 3606 | |
| 3607 | /// Begin named paragraph style |
| 3608 | bool wxRichTextBuffer::BeginParagraphStyle(const wxString& paragraphStyle) |
| 3609 | { |
| 3610 | if (GetStyleSheet()) |
| 3611 | { |
| 3612 | wxRichTextParagraphStyleDefinition* def = GetStyleSheet()->FindParagraphStyle(paragraphStyle); |
| 3613 | if (def) |
| 3614 | { |
| 3615 | wxTextAttrEx attr; |
| 3616 | def->GetStyle().CopyTo(attr); |
| 3617 | return BeginStyle(attr); |
| 3618 | } |
| 3619 | } |
| 3620 | return false; |
| 3621 | } |
| 3622 | |
| 3623 | /// Adds a handler to the end |
| 3624 | void wxRichTextBuffer::AddHandler(wxRichTextFileHandler *handler) |
| 3625 | { |
| 3626 | sm_handlers.Append(handler); |
| 3627 | } |
| 3628 | |
| 3629 | /// Inserts a handler at the front |
| 3630 | void wxRichTextBuffer::InsertHandler(wxRichTextFileHandler *handler) |
| 3631 | { |
| 3632 | sm_handlers.Insert( handler ); |
| 3633 | } |
| 3634 | |
| 3635 | /// Removes a handler |
| 3636 | bool wxRichTextBuffer::RemoveHandler(const wxString& name) |
| 3637 | { |
| 3638 | wxRichTextFileHandler *handler = FindHandler(name); |
| 3639 | if (handler) |
| 3640 | { |
| 3641 | sm_handlers.DeleteObject(handler); |
| 3642 | delete handler; |
| 3643 | return true; |
| 3644 | } |
| 3645 | else |
| 3646 | return false; |
| 3647 | } |
| 3648 | |
| 3649 | /// Finds a handler by filename or, if supplied, type |
| 3650 | wxRichTextFileHandler *wxRichTextBuffer::FindHandlerFilenameOrType(const wxString& filename, int imageType) |
| 3651 | { |
| 3652 | if (imageType != wxRICHTEXT_TYPE_ANY) |
| 3653 | return FindHandler(imageType); |
| 3654 | else |
| 3655 | { |
| 3656 | wxString path, file, ext; |
| 3657 | wxSplitPath(filename, & path, & file, & ext); |
| 3658 | return FindHandler(ext, imageType); |
| 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | |
| 3663 | /// Finds a handler by name |
| 3664 | wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& name) |
| 3665 | { |
| 3666 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
| 3667 | while (node) |
| 3668 | { |
| 3669 | wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData(); |
| 3670 | if (handler->GetName().Lower() == name.Lower()) return handler; |
| 3671 | |
| 3672 | node = node->GetNext(); |
| 3673 | } |
| 3674 | return NULL; |
| 3675 | } |
| 3676 | |
| 3677 | /// Finds a handler by extension and type |
| 3678 | wxRichTextFileHandler* wxRichTextBuffer::FindHandler(const wxString& extension, int type) |
| 3679 | { |
| 3680 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
| 3681 | while (node) |
| 3682 | { |
| 3683 | wxRichTextFileHandler *handler = (wxRichTextFileHandler*)node->GetData(); |
| 3684 | if ( handler->GetExtension().Lower() == extension.Lower() && |
| 3685 | (type == wxRICHTEXT_TYPE_ANY || handler->GetType() == type) ) |
| 3686 | return handler; |
| 3687 | node = node->GetNext(); |
| 3688 | } |
| 3689 | return 0; |
| 3690 | } |
| 3691 | |
| 3692 | /// Finds a handler by type |
| 3693 | wxRichTextFileHandler* wxRichTextBuffer::FindHandler(int type) |
| 3694 | { |
| 3695 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
| 3696 | while (node) |
| 3697 | { |
| 3698 | wxRichTextFileHandler *handler = (wxRichTextFileHandler *)node->GetData(); |
| 3699 | if (handler->GetType() == type) return handler; |
| 3700 | node = node->GetNext(); |
| 3701 | } |
| 3702 | return NULL; |
| 3703 | } |
| 3704 | |
| 3705 | void wxRichTextBuffer::InitStandardHandlers() |
| 3706 | { |
| 3707 | if (!FindHandler(wxRICHTEXT_TYPE_TEXT)) |
| 3708 | AddHandler(new wxRichTextPlainTextHandler); |
| 3709 | } |
| 3710 | |
| 3711 | void wxRichTextBuffer::CleanUpHandlers() |
| 3712 | { |
| 3713 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
| 3714 | while (node) |
| 3715 | { |
| 3716 | wxRichTextFileHandler* handler = (wxRichTextFileHandler*)node->GetData(); |
| 3717 | wxList::compatibility_iterator next = node->GetNext(); |
| 3718 | delete handler; |
| 3719 | node = next; |
| 3720 | } |
| 3721 | |
| 3722 | sm_handlers.Clear(); |
| 3723 | } |
| 3724 | |
| 3725 | wxString wxRichTextBuffer::GetExtWildcard(bool combine, bool save, wxArrayInt* types) |
| 3726 | { |
| 3727 | if (types) |
| 3728 | types->Clear(); |
| 3729 | |
| 3730 | wxString wildcard; |
| 3731 | |
| 3732 | wxList::compatibility_iterator node = GetHandlers().GetFirst(); |
| 3733 | int count = 0; |
| 3734 | while (node) |
| 3735 | { |
| 3736 | wxRichTextFileHandler* handler = (wxRichTextFileHandler*) node->GetData(); |
| 3737 | if (handler->IsVisible() && ((save && handler->CanSave()) || !save && handler->CanLoad())) |
| 3738 | { |
| 3739 | if (combine) |
| 3740 | { |
| 3741 | if (count > 0) |
| 3742 | wildcard += wxT(";"); |
| 3743 | wildcard += wxT("*.") + handler->GetExtension(); |
| 3744 | } |
| 3745 | else |
| 3746 | { |
| 3747 | if (count > 0) |
| 3748 | wildcard += wxT("|"); |
| 3749 | wildcard += handler->GetName(); |
| 3750 | wildcard += wxT(" "); |
| 3751 | wildcard += _("files"); |
| 3752 | wildcard += wxT(" (*."); |
| 3753 | wildcard += handler->GetExtension(); |
| 3754 | wildcard += wxT(")|*."); |
| 3755 | wildcard += handler->GetExtension(); |
| 3756 | if (types) |
| 3757 | types->Add(handler->GetType()); |
| 3758 | } |
| 3759 | count ++; |
| 3760 | } |
| 3761 | |
| 3762 | node = node->GetNext(); |
| 3763 | } |
| 3764 | |
| 3765 | if (combine) |
| 3766 | wildcard = wxT("(") + wildcard + wxT(")|") + wildcard; |
| 3767 | return wildcard; |
| 3768 | } |
| 3769 | |
| 3770 | /// Load a file |
| 3771 | bool wxRichTextBuffer::LoadFile(const wxString& filename, int type) |
| 3772 | { |
| 3773 | wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type); |
| 3774 | if (handler) |
| 3775 | { |
| 3776 | SetDefaultStyle(wxTextAttrEx()); |
| 3777 | |
| 3778 | bool success = handler->LoadFile(this, filename); |
| 3779 | Invalidate(wxRICHTEXT_ALL); |
| 3780 | return success; |
| 3781 | } |
| 3782 | else |
| 3783 | return false; |
| 3784 | } |
| 3785 | |
| 3786 | /// Save a file |
| 3787 | bool wxRichTextBuffer::SaveFile(const wxString& filename, int type) |
| 3788 | { |
| 3789 | wxRichTextFileHandler* handler = FindHandlerFilenameOrType(filename, type); |
| 3790 | if (handler) |
| 3791 | return handler->SaveFile(this, filename); |
| 3792 | else |
| 3793 | return false; |
| 3794 | } |
| 3795 | |
| 3796 | /// Load from a stream |
| 3797 | bool wxRichTextBuffer::LoadFile(wxInputStream& stream, int type) |
| 3798 | { |
| 3799 | wxRichTextFileHandler* handler = FindHandler(type); |
| 3800 | if (handler) |
| 3801 | { |
| 3802 | SetDefaultStyle(wxTextAttrEx()); |
| 3803 | bool success = handler->LoadFile(this, stream); |
| 3804 | Invalidate(wxRICHTEXT_ALL); |
| 3805 | return success; |
| 3806 | } |
| 3807 | else |
| 3808 | return false; |
| 3809 | } |
| 3810 | |
| 3811 | /// Save to a stream |
| 3812 | bool wxRichTextBuffer::SaveFile(wxOutputStream& stream, int type) |
| 3813 | { |
| 3814 | wxRichTextFileHandler* handler = FindHandler(type); |
| 3815 | if (handler) |
| 3816 | return handler->SaveFile(this, stream); |
| 3817 | else |
| 3818 | return false; |
| 3819 | } |
| 3820 | |
| 3821 | /// Copy the range to the clipboard |
| 3822 | bool wxRichTextBuffer::CopyToClipboard(const wxRichTextRange& range) |
| 3823 | { |
| 3824 | bool success = false; |
| 3825 | #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ |
| 3826 | wxString text = GetTextForRange(range); |
| 3827 | if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open()) |
| 3828 | { |
| 3829 | success = wxTheClipboard->SetData(new wxTextDataObject(text)); |
| 3830 | wxTheClipboard->Close(); |
| 3831 | } |
| 3832 | #else |
| 3833 | wxUnusedVar(range); |
| 3834 | #endif |
| 3835 | return success; |
| 3836 | } |
| 3837 | |
| 3838 | /// Paste the clipboard content to the buffer |
| 3839 | bool wxRichTextBuffer::PasteFromClipboard(long position) |
| 3840 | { |
| 3841 | bool success = false; |
| 3842 | #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ |
| 3843 | if (CanPasteFromClipboard()) |
| 3844 | { |
| 3845 | if (wxTheClipboard->Open()) |
| 3846 | { |
| 3847 | if (wxTheClipboard->IsSupported(wxDF_TEXT)) |
| 3848 | { |
| 3849 | wxTextDataObject data; |
| 3850 | wxTheClipboard->GetData(data); |
| 3851 | wxString text(data.GetText()); |
| 3852 | |
| 3853 | InsertTextWithUndo(position+1, text, GetRichTextCtrl()); |
| 3854 | |
| 3855 | success = true; |
| 3856 | } |
| 3857 | else if (wxTheClipboard->IsSupported(wxDF_BITMAP)) |
| 3858 | { |
| 3859 | wxBitmapDataObject data; |
| 3860 | wxTheClipboard->GetData(data); |
| 3861 | wxBitmap bitmap(data.GetBitmap()); |
| 3862 | wxImage image(bitmap.ConvertToImage()); |
| 3863 | |
| 3864 | wxRichTextAction* action = new wxRichTextAction(NULL, _("Insert Image"), wxRICHTEXT_INSERT, this, GetRichTextCtrl(), false); |
| 3865 | |
| 3866 | action->GetNewParagraphs().AddImage(image); |
| 3867 | |
| 3868 | if (action->GetNewParagraphs().GetChildCount() == 1) |
| 3869 | action->GetNewParagraphs().SetPartialParagraph(true); |
| 3870 | |
| 3871 | action->SetPosition(position); |
| 3872 | |
| 3873 | // Set the range we'll need to delete in Undo |
| 3874 | action->SetRange(wxRichTextRange(position, position)); |
| 3875 | |
| 3876 | SubmitAction(action); |
| 3877 | |
| 3878 | success = true; |
| 3879 | } |
| 3880 | wxTheClipboard->Close(); |
| 3881 | } |
| 3882 | } |
| 3883 | #else |
| 3884 | wxUnusedVar(position); |
| 3885 | #endif |
| 3886 | return success; |
| 3887 | } |
| 3888 | |
| 3889 | /// Can we paste from the clipboard? |
| 3890 | bool wxRichTextBuffer::CanPasteFromClipboard() const |
| 3891 | { |
| 3892 | bool canPaste = false; |
| 3893 | #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ |
| 3894 | if (!wxTheClipboard->IsOpened() && wxTheClipboard->Open()) |
| 3895 | { |
| 3896 | if (wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_BITMAP)) |
| 3897 | { |
| 3898 | canPaste = true; |
| 3899 | } |
| 3900 | wxTheClipboard->Close(); |
| 3901 | } |
| 3902 | #endif |
| 3903 | return canPaste; |
| 3904 | } |
| 3905 | |
| 3906 | /// Dumps contents of buffer for debugging purposes |
| 3907 | void wxRichTextBuffer::Dump() |
| 3908 | { |
| 3909 | wxString text; |
| 3910 | { |
| 3911 | wxStringOutputStream stream(& text); |
| 3912 | wxTextOutputStream textStream(stream); |
| 3913 | Dump(textStream); |
| 3914 | } |
| 3915 | |
| 3916 | wxLogDebug(text); |
| 3917 | } |
| 3918 | |
| 3919 | |
| 3920 | /* |
| 3921 | * Module to initialise and clean up handlers |
| 3922 | */ |
| 3923 | |
| 3924 | class wxRichTextModule: public wxModule |
| 3925 | { |
| 3926 | DECLARE_DYNAMIC_CLASS(wxRichTextModule) |
| 3927 | public: |
| 3928 | wxRichTextModule() {} |
| 3929 | bool OnInit() { wxRichTextBuffer::InitStandardHandlers(); return true; }; |
| 3930 | void OnExit() { wxRichTextBuffer::CleanUpHandlers(); }; |
| 3931 | }; |
| 3932 | |
| 3933 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextModule, wxModule) |
| 3934 | |
| 3935 | |
| 3936 | /*! |
| 3937 | * Commands for undo/redo |
| 3938 | * |
| 3939 | */ |
| 3940 | |
| 3941 | wxRichTextCommand::wxRichTextCommand(const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer, |
| 3942 | wxRichTextCtrl* ctrl, bool ignoreFirstTime): wxCommand(true, name) |
| 3943 | { |
| 3944 | /* wxRichTextAction* action = */ new wxRichTextAction(this, name, id, buffer, ctrl, ignoreFirstTime); |
| 3945 | } |
| 3946 | |
| 3947 | wxRichTextCommand::wxRichTextCommand(const wxString& name): wxCommand(true, name) |
| 3948 | { |
| 3949 | } |
| 3950 | |
| 3951 | wxRichTextCommand::~wxRichTextCommand() |
| 3952 | { |
| 3953 | ClearActions(); |
| 3954 | } |
| 3955 | |
| 3956 | void wxRichTextCommand::AddAction(wxRichTextAction* action) |
| 3957 | { |
| 3958 | if (!m_actions.Member(action)) |
| 3959 | m_actions.Append(action); |
| 3960 | } |
| 3961 | |
| 3962 | bool wxRichTextCommand::Do() |
| 3963 | { |
| 3964 | for (wxList::compatibility_iterator node = m_actions.GetFirst(); node; node = node->GetNext()) |
| 3965 | { |
| 3966 | wxRichTextAction* action = (wxRichTextAction*) node->GetData(); |
| 3967 | action->Do(); |
| 3968 | } |
| 3969 | |
| 3970 | return true; |
| 3971 | } |
| 3972 | |
| 3973 | bool wxRichTextCommand::Undo() |
| 3974 | { |
| 3975 | for (wxList::compatibility_iterator node = m_actions.GetLast(); node; node = node->GetPrevious()) |
| 3976 | { |
| 3977 | wxRichTextAction* action = (wxRichTextAction*) node->GetData(); |
| 3978 | action->Undo(); |
| 3979 | } |
| 3980 | |
| 3981 | return true; |
| 3982 | } |
| 3983 | |
| 3984 | void wxRichTextCommand::ClearActions() |
| 3985 | { |
| 3986 | WX_CLEAR_LIST(wxList, m_actions); |
| 3987 | } |
| 3988 | |
| 3989 | /*! |
| 3990 | * Individual action |
| 3991 | * |
| 3992 | */ |
| 3993 | |
| 3994 | wxRichTextAction::wxRichTextAction(wxRichTextCommand* cmd, const wxString& name, wxRichTextCommandId id, wxRichTextBuffer* buffer, |
| 3995 | wxRichTextCtrl* ctrl, bool ignoreFirstTime) |
| 3996 | { |
| 3997 | m_buffer = buffer; |
| 3998 | m_ignoreThis = ignoreFirstTime; |
| 3999 | m_cmdId = id; |
| 4000 | m_position = -1; |
| 4001 | m_ctrl = ctrl; |
| 4002 | m_name = name; |
| 4003 | m_newParagraphs.SetDefaultStyle(buffer->GetDefaultStyle()); |
| 4004 | m_newParagraphs.SetBasicStyle(buffer->GetBasicStyle()); |
| 4005 | if (cmd) |
| 4006 | cmd->AddAction(this); |
| 4007 | } |
| 4008 | |
| 4009 | wxRichTextAction::~wxRichTextAction() |
| 4010 | { |
| 4011 | } |
| 4012 | |
| 4013 | bool wxRichTextAction::Do() |
| 4014 | { |
| 4015 | m_buffer->Modify(true); |
| 4016 | |
| 4017 | switch (m_cmdId) |
| 4018 | { |
| 4019 | case wxRICHTEXT_INSERT: |
| 4020 | { |
| 4021 | m_buffer->InsertFragment(GetPosition(), m_newParagraphs); |
| 4022 | m_buffer->UpdateRanges(); |
| 4023 | m_buffer->Invalidate(GetRange()); |
| 4024 | |
| 4025 | long newCaretPosition = GetPosition() + m_newParagraphs.GetRange().GetLength() - 1; |
| 4026 | if (m_newParagraphs.GetPartialParagraph()) |
| 4027 | newCaretPosition --; |
| 4028 | |
| 4029 | UpdateAppearance(newCaretPosition, true /* send update event */); |
| 4030 | |
| 4031 | break; |
| 4032 | } |
| 4033 | case wxRICHTEXT_DELETE: |
| 4034 | { |
| 4035 | m_buffer->DeleteRange(GetRange()); |
| 4036 | m_buffer->UpdateRanges(); |
| 4037 | m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart())); |
| 4038 | |
| 4039 | UpdateAppearance(GetRange().GetStart()-1, true /* send update event */); |
| 4040 | |
| 4041 | break; |
| 4042 | } |
| 4043 | case wxRICHTEXT_CHANGE_STYLE: |
| 4044 | { |
| 4045 | ApplyParagraphs(GetNewParagraphs()); |
| 4046 | m_buffer->Invalidate(GetRange()); |
| 4047 | |
| 4048 | UpdateAppearance(GetPosition()); |
| 4049 | |
| 4050 | break; |
| 4051 | } |
| 4052 | default: |
| 4053 | break; |
| 4054 | } |
| 4055 | |
| 4056 | return true; |
| 4057 | } |
| 4058 | |
| 4059 | bool wxRichTextAction::Undo() |
| 4060 | { |
| 4061 | m_buffer->Modify(true); |
| 4062 | |
| 4063 | switch (m_cmdId) |
| 4064 | { |
| 4065 | case wxRICHTEXT_INSERT: |
| 4066 | { |
| 4067 | m_buffer->DeleteRange(GetRange()); |
| 4068 | m_buffer->UpdateRanges(); |
| 4069 | m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart(), GetRange().GetStart())); |
| 4070 | |
| 4071 | long newCaretPosition = GetPosition() - 1; |
| 4072 | // if (m_newParagraphs.GetPartialParagraph()) |
| 4073 | // newCaretPosition --; |
| 4074 | |
| 4075 | UpdateAppearance(newCaretPosition, true /* send update event */); |
| 4076 | |
| 4077 | break; |
| 4078 | } |
| 4079 | case wxRICHTEXT_DELETE: |
| 4080 | { |
| 4081 | m_buffer->InsertFragment(GetRange().GetStart(), m_oldParagraphs); |
| 4082 | m_buffer->UpdateRanges(); |
| 4083 | m_buffer->Invalidate(GetRange()); |
| 4084 | |
| 4085 | UpdateAppearance(GetPosition(), true /* send update event */); |
| 4086 | |
| 4087 | break; |
| 4088 | } |
| 4089 | case wxRICHTEXT_CHANGE_STYLE: |
| 4090 | { |
| 4091 | ApplyParagraphs(GetOldParagraphs()); |
| 4092 | m_buffer->Invalidate(GetRange()); |
| 4093 | |
| 4094 | UpdateAppearance(GetPosition()); |
| 4095 | |
| 4096 | break; |
| 4097 | } |
| 4098 | default: |
| 4099 | break; |
| 4100 | } |
| 4101 | |
| 4102 | return true; |
| 4103 | } |
| 4104 | |
| 4105 | /// Update the control appearance |
| 4106 | void wxRichTextAction::UpdateAppearance(long caretPosition, bool sendUpdateEvent) |
| 4107 | { |
| 4108 | if (m_ctrl) |
| 4109 | { |
| 4110 | m_ctrl->SetCaretPosition(caretPosition); |
| 4111 | if (!m_ctrl->IsFrozen()) |
| 4112 | { |
| 4113 | m_ctrl->LayoutContent(); |
| 4114 | m_ctrl->PositionCaret(); |
| 4115 | m_ctrl->Refresh(false); |
| 4116 | |
| 4117 | if (sendUpdateEvent) |
| 4118 | m_ctrl->SendUpdateEvent(); |
| 4119 | } |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | /// Replace the buffer paragraphs with the new ones. |
| 4124 | void wxRichTextAction::ApplyParagraphs(const wxRichTextFragment& fragment) |
| 4125 | { |
| 4126 | wxRichTextObjectList::compatibility_iterator node = fragment.GetChildren().GetFirst(); |
| 4127 | while (node) |
| 4128 | { |
| 4129 | wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph); |
| 4130 | wxASSERT (para != NULL); |
| 4131 | |
| 4132 | // We'll replace the existing paragraph by finding the paragraph at this position, |
| 4133 | // delete its node data, and setting a copy as the new node data. |
| 4134 | // TODO: make more efficient by simply swapping old and new paragraph objects. |
| 4135 | |
| 4136 | wxRichTextParagraph* existingPara = m_buffer->GetParagraphAtPosition(para->GetRange().GetStart()); |
| 4137 | if (existingPara) |
| 4138 | { |
| 4139 | wxRichTextObjectList::compatibility_iterator bufferParaNode = m_buffer->GetChildren().Find(existingPara); |
| 4140 | if (bufferParaNode) |
| 4141 | { |
| 4142 | wxRichTextParagraph* newPara = new wxRichTextParagraph(*para); |
| 4143 | newPara->SetParent(m_buffer); |
| 4144 | |
| 4145 | bufferParaNode->SetData(newPara); |
| 4146 | |
| 4147 | delete existingPara; |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | node = node->GetNext(); |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | |
| 4156 | /*! |
| 4157 | * wxRichTextRange |
| 4158 | * This stores beginning and end positions for a range of data. |
| 4159 | */ |
| 4160 | |
| 4161 | /// Limit this range to be within 'range' |
| 4162 | bool wxRichTextRange::LimitTo(const wxRichTextRange& range) |
| 4163 | { |
| 4164 | if (m_start < range.m_start) |
| 4165 | m_start = range.m_start; |
| 4166 | |
| 4167 | if (m_end > range.m_end) |
| 4168 | m_end = range.m_end; |
| 4169 | |
| 4170 | return true; |
| 4171 | } |
| 4172 | |
| 4173 | /*! |
| 4174 | * wxRichTextImage implementation |
| 4175 | * This object represents an image. |
| 4176 | */ |
| 4177 | |
| 4178 | IMPLEMENT_DYNAMIC_CLASS(wxRichTextImage, wxRichTextObject) |
| 4179 | |
| 4180 | wxRichTextImage::wxRichTextImage(const wxImage& image, wxRichTextObject* parent): |
| 4181 | wxRichTextObject(parent) |
| 4182 | { |
| 4183 | m_image = image; |
| 4184 | } |
| 4185 | |
| 4186 | wxRichTextImage::wxRichTextImage(const wxRichTextImageBlock& imageBlock, wxRichTextObject* parent): |
| 4187 | wxRichTextObject(parent) |
| 4188 | { |
| 4189 | m_imageBlock = imageBlock; |
| 4190 | m_imageBlock.Load(m_image); |
| 4191 | } |
| 4192 | |
| 4193 | /// Load wxImage from the block |
| 4194 | bool wxRichTextImage::LoadFromBlock() |
| 4195 | { |
| 4196 | m_imageBlock.Load(m_image); |
| 4197 | return m_imageBlock.Ok(); |
| 4198 | } |
| 4199 | |
| 4200 | /// Make block from the wxImage |
| 4201 | bool wxRichTextImage::MakeBlock() |
| 4202 | { |
| 4203 | if (m_imageBlock.GetImageType() == wxBITMAP_TYPE_ANY || m_imageBlock.GetImageType() == -1) |
| 4204 | m_imageBlock.SetImageType(wxBITMAP_TYPE_PNG); |
| 4205 | |
| 4206 | m_imageBlock.MakeImageBlock(m_image, m_imageBlock.GetImageType()); |
| 4207 | return m_imageBlock.Ok(); |
| 4208 | } |
| 4209 | |
| 4210 | |
| 4211 | /// Draw the item |
| 4212 | bool wxRichTextImage::Draw(wxDC& dc, const wxRichTextRange& range, const wxRichTextRange& selectionRange, const wxRect& rect, int WXUNUSED(descent), int WXUNUSED(style)) |
| 4213 | { |
| 4214 | if (!m_image.Ok() && m_imageBlock.Ok()) |
| 4215 | LoadFromBlock(); |
| 4216 | |
| 4217 | if (!m_image.Ok()) |
| 4218 | return false; |
| 4219 | |
| 4220 | if (m_image.Ok() && !m_bitmap.Ok()) |
| 4221 | m_bitmap = wxBitmap(m_image); |
| 4222 | |
| 4223 | int y = rect.y + (rect.height - m_image.GetHeight()); |
| 4224 | |
| 4225 | if (m_bitmap.Ok()) |
| 4226 | dc.DrawBitmap(m_bitmap, rect.x, y, true); |
| 4227 | |
| 4228 | if (selectionRange.Contains(range.GetStart())) |
| 4229 | { |
| 4230 | dc.SetBrush(*wxBLACK_BRUSH); |
| 4231 | dc.SetPen(*wxBLACK_PEN); |
| 4232 | dc.SetLogicalFunction(wxINVERT); |
| 4233 | dc.DrawRectangle(rect); |
| 4234 | dc.SetLogicalFunction(wxCOPY); |
| 4235 | } |
| 4236 | |
| 4237 | return true; |
| 4238 | } |
| 4239 | |
| 4240 | /// Lay the item out |
| 4241 | bool wxRichTextImage::Layout(wxDC& WXUNUSED(dc), const wxRect& rect, int WXUNUSED(style)) |
| 4242 | { |
| 4243 | if (!m_image.Ok()) |
| 4244 | LoadFromBlock(); |
| 4245 | |
| 4246 | if (m_image.Ok()) |
| 4247 | { |
| 4248 | SetCachedSize(wxSize(m_image.GetWidth(), m_image.GetHeight())); |
| 4249 | SetPosition(rect.GetPosition()); |
| 4250 | } |
| 4251 | |
| 4252 | return true; |
| 4253 | } |
| 4254 | |
| 4255 | /// Get/set the object size for the given range. Returns false if the range |
| 4256 | /// is invalid for this object. |
| 4257 | bool wxRichTextImage::GetRangeSize(const wxRichTextRange& range, wxSize& size, int& WXUNUSED(descent), wxDC& WXUNUSED(dc), int WXUNUSED(flags)) const |
| 4258 | { |
| 4259 | if (!range.IsWithin(GetRange())) |
| 4260 | return false; |
| 4261 | |
| 4262 | if (!m_image.Ok()) |
| 4263 | return false; |
| 4264 | |
| 4265 | size.x = m_image.GetWidth(); |
| 4266 | size.y = m_image.GetHeight(); |
| 4267 | |
| 4268 | return true; |
| 4269 | } |
| 4270 | |
| 4271 | /// Copy |
| 4272 | void wxRichTextImage::Copy(const wxRichTextImage& obj) |
| 4273 | { |
| 4274 | m_image = obj.m_image; |
| 4275 | m_imageBlock = obj.m_imageBlock; |
| 4276 | } |
| 4277 | |
| 4278 | /*! |
| 4279 | * Utilities |
| 4280 | * |
| 4281 | */ |
| 4282 | |
| 4283 | /// Compare two attribute objects |
| 4284 | bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2) |
| 4285 | { |
| 4286 | return ( |
| 4287 | attr1.GetTextColour() == attr2.GetTextColour() && |
| 4288 | attr1.GetBackgroundColour() == attr2.GetBackgroundColour() && |
| 4289 | attr1.GetFont() == attr2.GetFont() && |
| 4290 | attr1.GetAlignment() == attr2.GetAlignment() && |
| 4291 | attr1.GetLeftIndent() == attr2.GetLeftIndent() && |
| 4292 | attr1.GetRightIndent() == attr2.GetRightIndent() && |
| 4293 | attr1.GetLeftSubIndent() == attr2.GetLeftSubIndent() && |
| 4294 | attr1.GetTabs().GetCount() == attr2.GetTabs().GetCount() && // heuristic |
| 4295 | attr1.GetLineSpacing() == attr2.GetLineSpacing() && |
| 4296 | attr1.GetParagraphSpacingAfter() == attr2.GetParagraphSpacingAfter() && |
| 4297 | attr1.GetParagraphSpacingBefore() == attr2.GetParagraphSpacingBefore() && |
| 4298 | attr1.GetBulletStyle() == attr2.GetBulletStyle() && |
| 4299 | attr1.GetBulletNumber() == attr2.GetBulletNumber() && |
| 4300 | attr1.GetBulletSymbol() == attr2.GetBulletSymbol() && |
| 4301 | attr1.GetCharacterStyleName() == attr2.GetCharacterStyleName() && |
| 4302 | attr1.GetParagraphStyleName() == attr2.GetParagraphStyleName()); |
| 4303 | } |
| 4304 | |
| 4305 | bool wxTextAttrEq(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2) |
| 4306 | { |
| 4307 | return ( |
| 4308 | attr1.GetTextColour() == attr2.GetTextColour() && |
| 4309 | attr1.GetBackgroundColour() == attr2.GetBackgroundColour() && |
| 4310 | attr1.GetFont().GetPointSize() == attr2.GetFontSize() && |
| 4311 | attr1.GetFont().GetStyle() == attr2.GetFontStyle() && |
| 4312 | attr1.GetFont().GetWeight() == attr2.GetFontWeight() && |
| 4313 | attr1.GetFont().GetFaceName() == attr2.GetFontFaceName() && |
| 4314 | attr1.GetFont().GetUnderlined() == attr2.GetFontUnderlined() && |
| 4315 | attr1.GetAlignment() == attr2.GetAlignment() && |
| 4316 | attr1.GetLeftIndent() == attr2.GetLeftIndent() && |
| 4317 | attr1.GetRightIndent() == attr2.GetRightIndent() && |
| 4318 | attr1.GetLeftSubIndent() == attr2.GetLeftSubIndent() && |
| 4319 | attr1.GetTabs().GetCount() == attr2.GetTabs().GetCount() && // heuristic |
| 4320 | attr1.GetLineSpacing() == attr2.GetLineSpacing() && |
| 4321 | attr1.GetParagraphSpacingAfter() == attr2.GetParagraphSpacingAfter() && |
| 4322 | attr1.GetParagraphSpacingBefore() == attr2.GetParagraphSpacingBefore() && |
| 4323 | attr1.GetBulletStyle() == attr2.GetBulletStyle() && |
| 4324 | attr1.GetBulletNumber() == attr2.GetBulletNumber() && |
| 4325 | attr1.GetBulletSymbol() == attr2.GetBulletSymbol() && |
| 4326 | attr1.GetCharacterStyleName() == attr2.GetCharacterStyleName() && |
| 4327 | attr1.GetParagraphStyleName() == attr2.GetParagraphStyleName()); |
| 4328 | } |
| 4329 | |
| 4330 | /// Compare two attribute objects, but take into account the flags |
| 4331 | /// specifying attributes of interest. |
| 4332 | bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxTextAttrEx& attr2, int flags) |
| 4333 | { |
| 4334 | if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && attr1.GetTextColour() != attr2.GetTextColour()) |
| 4335 | return false; |
| 4336 | |
| 4337 | if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && attr1.GetBackgroundColour() != attr2.GetBackgroundColour()) |
| 4338 | return false; |
| 4339 | |
| 4340 | if ((flags & wxTEXT_ATTR_FONT_FACE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() && |
| 4341 | attr1.GetFont().GetFaceName() != attr2.GetFont().GetFaceName()) |
| 4342 | return false; |
| 4343 | |
| 4344 | if ((flags & wxTEXT_ATTR_FONT_SIZE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() && |
| 4345 | attr1.GetFont().GetPointSize() != attr2.GetFont().GetPointSize()) |
| 4346 | return false; |
| 4347 | |
| 4348 | if ((flags & wxTEXT_ATTR_FONT_WEIGHT) && attr1.GetFont().Ok() && attr2.GetFont().Ok() && |
| 4349 | attr1.GetFont().GetWeight() != attr2.GetFont().GetWeight()) |
| 4350 | return false; |
| 4351 | |
| 4352 | if ((flags & wxTEXT_ATTR_FONT_ITALIC) && attr1.GetFont().Ok() && attr2.GetFont().Ok() && |
| 4353 | attr1.GetFont().GetStyle() != attr2.GetFont().GetStyle()) |
| 4354 | return false; |
| 4355 | |
| 4356 | if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) && attr1.GetFont().Ok() && attr2.GetFont().Ok() && |
| 4357 | attr1.GetFont().GetUnderlined() != attr2.GetFont().GetUnderlined()) |
| 4358 | return false; |
| 4359 | |
| 4360 | if ((flags & wxTEXT_ATTR_ALIGNMENT) && attr1.GetAlignment() != attr2.GetAlignment()) |
| 4361 | return false; |
| 4362 | |
| 4363 | if ((flags & wxTEXT_ATTR_LEFT_INDENT) && |
| 4364 | ((attr1.GetLeftIndent() != attr2.GetLeftIndent()) || (attr1.GetLeftSubIndent() != attr2.GetLeftSubIndent()))) |
| 4365 | return false; |
| 4366 | |
| 4367 | if ((flags & wxTEXT_ATTR_RIGHT_INDENT) && |
| 4368 | (attr1.GetRightIndent() != attr2.GetRightIndent())) |
| 4369 | return false; |
| 4370 | |
| 4371 | if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) && |
| 4372 | (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter())) |
| 4373 | return false; |
| 4374 | |
| 4375 | if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) && |
| 4376 | (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore())) |
| 4377 | return false; |
| 4378 | |
| 4379 | if ((flags & wxTEXT_ATTR_LINE_SPACING) && |
| 4380 | (attr1.GetLineSpacing() != attr2.GetLineSpacing())) |
| 4381 | return false; |
| 4382 | |
| 4383 | if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) && |
| 4384 | (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName())) |
| 4385 | return false; |
| 4386 | |
| 4387 | if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) && |
| 4388 | (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName())) |
| 4389 | return false; |
| 4390 | |
| 4391 | if ((flags & wxTEXT_ATTR_BULLET_STYLE) && |
| 4392 | (attr1.GetBulletStyle() != attr2.GetBulletStyle())) |
| 4393 | return false; |
| 4394 | |
| 4395 | if ((flags & wxTEXT_ATTR_BULLET_NUMBER) && |
| 4396 | (attr1.GetBulletNumber() != attr2.GetBulletNumber())) |
| 4397 | return false; |
| 4398 | |
| 4399 | if ((flags & wxTEXT_ATTR_BULLET_SYMBOL) && |
| 4400 | (attr1.GetBulletSymbol() != attr2.GetBulletSymbol())) |
| 4401 | return false; |
| 4402 | |
| 4403 | /* TODO |
| 4404 | if ((flags & wxTEXT_ATTR_TABS) && |
| 4405 | return false; |
| 4406 | */ |
| 4407 | |
| 4408 | return true; |
| 4409 | } |
| 4410 | |
| 4411 | bool wxTextAttrEqPartial(const wxTextAttrEx& attr1, const wxRichTextAttr& attr2, int flags) |
| 4412 | { |
| 4413 | if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && attr1.GetTextColour() != attr2.GetTextColour()) |
| 4414 | return false; |
| 4415 | |
| 4416 | if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && attr1.GetBackgroundColour() != attr2.GetBackgroundColour()) |
| 4417 | return false; |
| 4418 | |
| 4419 | if ((flags & (wxTEXT_ATTR_FONT)) && !attr1.GetFont().Ok()) |
| 4420 | return false; |
| 4421 | |
| 4422 | if ((flags & wxTEXT_ATTR_FONT_FACE) && attr1.GetFont().Ok() && |
| 4423 | attr1.GetFont().GetFaceName() != attr2.GetFontFaceName()) |
| 4424 | return false; |
| 4425 | |
| 4426 | if ((flags & wxTEXT_ATTR_FONT_SIZE) && attr1.GetFont().Ok() && |
| 4427 | attr1.GetFont().GetPointSize() != attr2.GetFontSize()) |
| 4428 | return false; |
| 4429 | |
| 4430 | if ((flags & wxTEXT_ATTR_FONT_WEIGHT) && attr1.GetFont().Ok() && |
| 4431 | attr1.GetFont().GetWeight() != attr2.GetFontWeight()) |
| 4432 | return false; |
| 4433 | |
| 4434 | if ((flags & wxTEXT_ATTR_FONT_ITALIC) && attr1.GetFont().Ok() && |
| 4435 | attr1.GetFont().GetStyle() != attr2.GetFontStyle()) |
| 4436 | return false; |
| 4437 | |
| 4438 | if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) && attr1.GetFont().Ok() && |
| 4439 | attr1.GetFont().GetUnderlined() != attr2.GetFontUnderlined()) |
| 4440 | return false; |
| 4441 | |
| 4442 | if ((flags & wxTEXT_ATTR_ALIGNMENT) && attr1.GetAlignment() != attr2.GetAlignment()) |
| 4443 | return false; |
| 4444 | |
| 4445 | if ((flags & wxTEXT_ATTR_LEFT_INDENT) && |
| 4446 | ((attr1.GetLeftIndent() != attr2.GetLeftIndent()) || (attr1.GetLeftSubIndent() != attr2.GetLeftSubIndent()))) |
| 4447 | return false; |
| 4448 | |
| 4449 | if ((flags & wxTEXT_ATTR_RIGHT_INDENT) && |
| 4450 | (attr1.GetRightIndent() != attr2.GetRightIndent())) |
| 4451 | return false; |
| 4452 | |
| 4453 | if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) && |
| 4454 | (attr1.GetParagraphSpacingAfter() != attr2.GetParagraphSpacingAfter())) |
| 4455 | return false; |
| 4456 | |
| 4457 | if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) && |
| 4458 | (attr1.GetParagraphSpacingBefore() != attr2.GetParagraphSpacingBefore())) |
| 4459 | return false; |
| 4460 | |
| 4461 | if ((flags & wxTEXT_ATTR_LINE_SPACING) && |
| 4462 | (attr1.GetLineSpacing() != attr2.GetLineSpacing())) |
| 4463 | return false; |
| 4464 | |
| 4465 | if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) && |
| 4466 | (attr1.GetCharacterStyleName() != attr2.GetCharacterStyleName())) |
| 4467 | return false; |
| 4468 | |
| 4469 | if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) && |
| 4470 | (attr1.GetParagraphStyleName() != attr2.GetParagraphStyleName())) |
| 4471 | return false; |
| 4472 | |
| 4473 | if ((flags & wxTEXT_ATTR_BULLET_STYLE) && |
| 4474 | (attr1.GetBulletStyle() != attr2.GetBulletStyle())) |
| 4475 | return false; |
| 4476 | |
| 4477 | if ((flags & wxTEXT_ATTR_BULLET_NUMBER) && |
| 4478 | (attr1.GetBulletNumber() != attr2.GetBulletNumber())) |
| 4479 | return false; |
| 4480 | |
| 4481 | if ((flags & wxTEXT_ATTR_BULLET_SYMBOL) && |
| 4482 | (attr1.GetBulletSymbol() != attr2.GetBulletSymbol())) |
| 4483 | return false; |
| 4484 | |
| 4485 | /* TODO |
| 4486 | if ((flags & wxTEXT_ATTR_TABS) && |
| 4487 | return false; |
| 4488 | */ |
| 4489 | |
| 4490 | return true; |
| 4491 | } |
| 4492 | |
| 4493 | |
| 4494 | /// Apply one style to another |
| 4495 | bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxTextAttrEx& style) |
| 4496 | { |
| 4497 | // Whole font |
| 4498 | if (style.GetFont().Ok() && ((style.GetFlags() & (wxTEXT_ATTR_FONT)) == (wxTEXT_ATTR_FONT))) |
| 4499 | destStyle.SetFont(style.GetFont()); |
| 4500 | else if (style.GetFont().Ok()) |
| 4501 | { |
| 4502 | wxFont font = destStyle.GetFont(); |
| 4503 | |
| 4504 | if (style.GetFlags() & wxTEXT_ATTR_FONT_FACE) |
| 4505 | font.SetFaceName(style.GetFont().GetFaceName()); |
| 4506 | |
| 4507 | if (style.GetFlags() & wxTEXT_ATTR_FONT_SIZE) |
| 4508 | font.SetPointSize(style.GetFont().GetPointSize()); |
| 4509 | |
| 4510 | if (style.GetFlags() & wxTEXT_ATTR_FONT_ITALIC) |
| 4511 | font.SetStyle(style.GetFont().GetStyle()); |
| 4512 | |
| 4513 | if (style.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT) |
| 4514 | font.SetWeight(style.GetFont().GetWeight()); |
| 4515 | |
| 4516 | if (style.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE) |
| 4517 | font.SetUnderlined(style.GetFont().GetUnderlined()); |
| 4518 | |
| 4519 | if (font != destStyle.GetFont()) |
| 4520 | destStyle.SetFont(font); |
| 4521 | } |
| 4522 | |
| 4523 | if ( style.GetTextColour().Ok() && style.HasTextColour()) |
| 4524 | destStyle.SetTextColour(style.GetTextColour()); |
| 4525 | |
| 4526 | if ( style.GetBackgroundColour().Ok() && style.HasBackgroundColour()) |
| 4527 | destStyle.SetBackgroundColour(style.GetBackgroundColour()); |
| 4528 | |
| 4529 | if (style.HasAlignment()) |
| 4530 | destStyle.SetAlignment(style.GetAlignment()); |
| 4531 | |
| 4532 | if (style.HasTabs()) |
| 4533 | destStyle.SetTabs(style.GetTabs()); |
| 4534 | |
| 4535 | if (style.HasLeftIndent()) |
| 4536 | destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent()); |
| 4537 | |
| 4538 | if (style.HasRightIndent()) |
| 4539 | destStyle.SetRightIndent(style.GetRightIndent()); |
| 4540 | |
| 4541 | if (style.HasParagraphSpacingAfter()) |
| 4542 | destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter()); |
| 4543 | |
| 4544 | if (style.HasParagraphSpacingBefore()) |
| 4545 | destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore()); |
| 4546 | |
| 4547 | if (style.HasLineSpacing()) |
| 4548 | destStyle.SetLineSpacing(style.GetLineSpacing()); |
| 4549 | |
| 4550 | if (style.HasCharacterStyleName()) |
| 4551 | destStyle.SetCharacterStyleName(style.GetCharacterStyleName()); |
| 4552 | |
| 4553 | if (style.HasParagraphStyleName()) |
| 4554 | destStyle.SetParagraphStyleName(style.GetParagraphStyleName()); |
| 4555 | |
| 4556 | if (style.HasBulletStyle()) |
| 4557 | { |
| 4558 | destStyle.SetBulletStyle(style.GetBulletStyle()); |
| 4559 | destStyle.SetBulletSymbol(style.GetBulletSymbol()); |
| 4560 | } |
| 4561 | |
| 4562 | if (style.HasBulletNumber()) |
| 4563 | destStyle.SetBulletNumber(style.GetBulletNumber()); |
| 4564 | |
| 4565 | return true; |
| 4566 | } |
| 4567 | |
| 4568 | bool wxRichTextApplyStyle(wxRichTextAttr& destStyle, const wxTextAttrEx& style) |
| 4569 | { |
| 4570 | wxTextAttrEx destStyle2; |
| 4571 | destStyle.CopyTo(destStyle2); |
| 4572 | wxRichTextApplyStyle(destStyle2, style); |
| 4573 | destStyle = destStyle2; |
| 4574 | return true; |
| 4575 | } |
| 4576 | |
| 4577 | bool wxRichTextApplyStyle(wxTextAttrEx& destStyle, const wxRichTextAttr& style) |
| 4578 | { |
| 4579 | |
| 4580 | // Whole font. Avoiding setting individual attributes if possible, since |
| 4581 | // it recreates the font each time. |
| 4582 | if ((style.GetFlags() & (wxTEXT_ATTR_FONT)) == (wxTEXT_ATTR_FONT)) |
| 4583 | { |
| 4584 | destStyle.SetFont(wxFont(style.GetFontSize(), destStyle.GetFont().Ok() ? destStyle.GetFont().GetFamily() : wxDEFAULT, |
| 4585 | style.GetFontStyle(), style.GetFontWeight(), style.GetFontUnderlined(), style.GetFontFaceName())); |
| 4586 | } |
| 4587 | else if (style.GetFlags() & (wxTEXT_ATTR_FONT)) |
| 4588 | { |
| 4589 | wxFont font = destStyle.GetFont(); |
| 4590 | |
| 4591 | if (style.GetFlags() & wxTEXT_ATTR_FONT_FACE) |
| 4592 | font.SetFaceName(style.GetFontFaceName()); |
| 4593 | |
| 4594 | if (style.GetFlags() & wxTEXT_ATTR_FONT_SIZE) |
| 4595 | font.SetPointSize(style.GetFontSize()); |
| 4596 | |
| 4597 | if (style.GetFlags() & wxTEXT_ATTR_FONT_ITALIC) |
| 4598 | font.SetStyle(style.GetFontStyle()); |
| 4599 | |
| 4600 | if (style.GetFlags() & wxTEXT_ATTR_FONT_WEIGHT) |
| 4601 | font.SetWeight(style.GetFontWeight()); |
| 4602 | |
| 4603 | if (style.GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE) |
| 4604 | font.SetUnderlined(style.GetFontUnderlined()); |
| 4605 | |
| 4606 | if (font != destStyle.GetFont()) |
| 4607 | destStyle.SetFont(font); |
| 4608 | } |
| 4609 | |
| 4610 | if ( style.GetTextColour().Ok() && style.HasTextColour()) |
| 4611 | destStyle.SetTextColour(style.GetTextColour()); |
| 4612 | |
| 4613 | if ( style.GetBackgroundColour().Ok() && style.HasBackgroundColour()) |
| 4614 | destStyle.SetBackgroundColour(style.GetBackgroundColour()); |
| 4615 | |
| 4616 | if (style.HasAlignment()) |
| 4617 | destStyle.SetAlignment(style.GetAlignment()); |
| 4618 | |
| 4619 | if (style.HasTabs()) |
| 4620 | destStyle.SetTabs(style.GetTabs()); |
| 4621 | |
| 4622 | if (style.HasLeftIndent()) |
| 4623 | destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent()); |
| 4624 | |
| 4625 | if (style.HasRightIndent()) |
| 4626 | destStyle.SetRightIndent(style.GetRightIndent()); |
| 4627 | |
| 4628 | if (style.HasParagraphSpacingAfter()) |
| 4629 | destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter()); |
| 4630 | |
| 4631 | if (style.HasParagraphSpacingBefore()) |
| 4632 | destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore()); |
| 4633 | |
| 4634 | if (style.HasLineSpacing()) |
| 4635 | destStyle.SetLineSpacing(style.GetLineSpacing()); |
| 4636 | |
| 4637 | if (style.HasCharacterStyleName()) |
| 4638 | destStyle.SetCharacterStyleName(style.GetCharacterStyleName()); |
| 4639 | |
| 4640 | if (style.HasParagraphStyleName()) |
| 4641 | destStyle.SetParagraphStyleName(style.GetParagraphStyleName()); |
| 4642 | |
| 4643 | if (style.HasBulletStyle()) |
| 4644 | { |
| 4645 | destStyle.SetBulletStyle(style.GetBulletStyle()); |
| 4646 | destStyle.SetBulletSymbol(style.GetBulletSymbol()); |
| 4647 | } |
| 4648 | |
| 4649 | if (style.HasBulletNumber()) |
| 4650 | destStyle.SetBulletNumber(style.GetBulletNumber()); |
| 4651 | |
| 4652 | return true; |
| 4653 | } |
| 4654 | |
| 4655 | |
| 4656 | /*! |
| 4657 | * wxRichTextAttr stores attributes without a wxFont object, so is a much more |
| 4658 | * efficient way to query styles. |
| 4659 | */ |
| 4660 | |
| 4661 | // ctors |
| 4662 | wxRichTextAttr::wxRichTextAttr(const wxColour& colText, |
| 4663 | const wxColour& colBack, |
| 4664 | wxTextAttrAlignment alignment): m_textAlignment(alignment), m_colText(colText), m_colBack(colBack) |
| 4665 | { |
| 4666 | Init(); |
| 4667 | |
| 4668 | if (m_colText.Ok()) m_flags |= wxTEXT_ATTR_TEXT_COLOUR; |
| 4669 | if (m_colBack.Ok()) m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR; |
| 4670 | if (alignment != wxTEXT_ALIGNMENT_DEFAULT) |
| 4671 | m_flags |= wxTEXT_ATTR_ALIGNMENT; |
| 4672 | } |
| 4673 | |
| 4674 | wxRichTextAttr::wxRichTextAttr(const wxTextAttrEx& attr) |
| 4675 | { |
| 4676 | Init(); |
| 4677 | |
| 4678 | (*this) = attr; |
| 4679 | } |
| 4680 | |
| 4681 | // operations |
| 4682 | void wxRichTextAttr::Init() |
| 4683 | { |
| 4684 | m_textAlignment = wxTEXT_ALIGNMENT_DEFAULT; |
| 4685 | m_flags = 0; |
| 4686 | m_leftIndent = 0; |
| 4687 | m_leftSubIndent = 0; |
| 4688 | m_rightIndent = 0; |
| 4689 | |
| 4690 | m_fontSize = 12; |
| 4691 | m_fontStyle = wxNORMAL; |
| 4692 | m_fontWeight = wxNORMAL; |
| 4693 | m_fontUnderlined = false; |
| 4694 | |
| 4695 | m_paragraphSpacingAfter = 0; |
| 4696 | m_paragraphSpacingBefore = 0; |
| 4697 | m_lineSpacing = 0; |
| 4698 | m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE; |
| 4699 | m_bulletNumber = 0; |
| 4700 | m_bulletSymbol = wxT('*'); |
| 4701 | } |
| 4702 | |
| 4703 | // operators |
| 4704 | void wxRichTextAttr::operator= (const wxRichTextAttr& attr) |
| 4705 | { |
| 4706 | m_colText = attr.m_colText; |
| 4707 | m_colBack = attr.m_colBack; |
| 4708 | m_textAlignment = attr.m_textAlignment; |
| 4709 | m_leftIndent = attr.m_leftIndent; |
| 4710 | m_leftSubIndent = attr.m_leftSubIndent; |
| 4711 | m_rightIndent = attr.m_rightIndent; |
| 4712 | m_tabs = attr.m_tabs; |
| 4713 | m_flags = attr.m_flags; |
| 4714 | |
| 4715 | m_fontSize = attr.m_fontSize; |
| 4716 | m_fontStyle = attr.m_fontStyle; |
| 4717 | m_fontWeight = attr.m_fontWeight; |
| 4718 | m_fontUnderlined = attr.m_fontUnderlined; |
| 4719 | m_fontFaceName = attr.m_fontFaceName; |
| 4720 | |
| 4721 | m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter; |
| 4722 | m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore; |
| 4723 | m_lineSpacing = attr.m_lineSpacing; |
| 4724 | m_characterStyleName = attr.m_characterStyleName; |
| 4725 | m_paragraphStyleName = attr.m_paragraphStyleName; |
| 4726 | m_bulletStyle = attr.m_bulletStyle; |
| 4727 | m_bulletNumber = attr.m_bulletNumber; |
| 4728 | m_bulletSymbol = attr.m_bulletSymbol; |
| 4729 | } |
| 4730 | |
| 4731 | // operators |
| 4732 | void wxRichTextAttr::operator= (const wxTextAttrEx& attr) |
| 4733 | { |
| 4734 | m_colText = attr.GetTextColour(); |
| 4735 | m_colBack = attr.GetBackgroundColour(); |
| 4736 | m_textAlignment = attr.GetAlignment(); |
| 4737 | m_leftIndent = attr.GetLeftIndent(); |
| 4738 | m_leftSubIndent = attr.GetLeftSubIndent(); |
| 4739 | m_rightIndent = attr.GetRightIndent(); |
| 4740 | m_tabs = attr.GetTabs(); |
| 4741 | m_flags = attr.GetFlags(); |
| 4742 | |
| 4743 | m_paragraphSpacingAfter = attr.GetParagraphSpacingAfter(); |
| 4744 | m_paragraphSpacingBefore = attr.GetParagraphSpacingBefore(); |
| 4745 | m_lineSpacing = attr.GetLineSpacing(); |
| 4746 | m_characterStyleName = attr.GetCharacterStyleName(); |
| 4747 | m_paragraphStyleName = attr.GetParagraphStyleName(); |
| 4748 | |
| 4749 | if (attr.GetFont().Ok()) |
| 4750 | GetFontAttributes(attr.GetFont()); |
| 4751 | } |
| 4752 | |
| 4753 | // Making a wxTextAttrEx object. |
| 4754 | wxRichTextAttr::operator wxTextAttrEx () const |
| 4755 | { |
| 4756 | wxTextAttrEx attr; |
| 4757 | CopyTo(attr); |
| 4758 | return attr; |
| 4759 | } |
| 4760 | |
| 4761 | // Copy to a wxTextAttr |
| 4762 | void wxRichTextAttr::CopyTo(wxTextAttrEx& attr) const |
| 4763 | { |
| 4764 | attr.SetTextColour(GetTextColour()); |
| 4765 | attr.SetBackgroundColour(GetBackgroundColour()); |
| 4766 | attr.SetAlignment(GetAlignment()); |
| 4767 | attr.SetTabs(GetTabs()); |
| 4768 | attr.SetLeftIndent(GetLeftIndent(), GetLeftSubIndent()); |
| 4769 | attr.SetRightIndent(GetRightIndent()); |
| 4770 | attr.SetFont(CreateFont()); |
| 4771 | attr.SetFlags(GetFlags()); // Important: set after SetFont, since SetFont sets flags |
| 4772 | |
| 4773 | attr.SetParagraphSpacingAfter(m_paragraphSpacingAfter); |
| 4774 | attr.SetParagraphSpacingBefore(m_paragraphSpacingBefore); |
| 4775 | attr.SetLineSpacing(m_lineSpacing); |
| 4776 | attr.SetBulletStyle(m_bulletStyle); |
| 4777 | attr.SetBulletNumber(m_bulletNumber); |
| 4778 | attr.SetBulletSymbol(m_bulletSymbol); |
| 4779 | attr.SetCharacterStyleName(m_characterStyleName); |
| 4780 | attr.SetParagraphStyleName(m_paragraphStyleName); |
| 4781 | |
| 4782 | } |
| 4783 | |
| 4784 | // Create font from font attributes. |
| 4785 | wxFont wxRichTextAttr::CreateFont() const |
| 4786 | { |
| 4787 | wxFont font(m_fontSize, wxDEFAULT, m_fontStyle, m_fontWeight, m_fontUnderlined, m_fontFaceName); |
| 4788 | #ifdef __WXMAC__ |
| 4789 | font.SetNoAntiAliasing(true); |
| 4790 | #endif |
| 4791 | return font; |
| 4792 | } |
| 4793 | |
| 4794 | // Get attributes from font. |
| 4795 | bool wxRichTextAttr::GetFontAttributes(const wxFont& font) |
| 4796 | { |
| 4797 | if (!font.Ok()) |
| 4798 | return false; |
| 4799 | |
| 4800 | m_fontSize = font.GetPointSize(); |
| 4801 | m_fontStyle = font.GetStyle(); |
| 4802 | m_fontWeight = font.GetWeight(); |
| 4803 | m_fontUnderlined = font.GetUnderlined(); |
| 4804 | m_fontFaceName = font.GetFaceName(); |
| 4805 | |
| 4806 | return true; |
| 4807 | } |
| 4808 | |
| 4809 | /*! |
| 4810 | * wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes. |
| 4811 | */ |
| 4812 | |
| 4813 | wxTextAttrEx::wxTextAttrEx(const wxTextAttrEx& attr): wxTextAttr(attr) |
| 4814 | { |
| 4815 | m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter; |
| 4816 | m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore; |
| 4817 | m_lineSpacing = attr.m_lineSpacing; |
| 4818 | m_paragraphStyleName = attr.m_paragraphStyleName; |
| 4819 | m_characterStyleName = attr.m_characterStyleName; |
| 4820 | m_bulletStyle = attr.m_bulletStyle; |
| 4821 | m_bulletNumber = attr.m_bulletNumber; |
| 4822 | m_bulletSymbol = attr.m_bulletSymbol; |
| 4823 | } |
| 4824 | |
| 4825 | // Initialise this object. |
| 4826 | void wxTextAttrEx::Init() |
| 4827 | { |
| 4828 | m_paragraphSpacingAfter = 0; |
| 4829 | m_paragraphSpacingBefore = 0; |
| 4830 | m_lineSpacing = 0; |
| 4831 | m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE; |
| 4832 | m_bulletNumber = 0; |
| 4833 | m_bulletSymbol = 0; |
| 4834 | m_bulletSymbol = wxT('*'); |
| 4835 | } |
| 4836 | |
| 4837 | // Assignment from a wxTextAttrEx object |
| 4838 | void wxTextAttrEx::operator= (const wxTextAttrEx& attr) |
| 4839 | { |
| 4840 | wxTextAttr::operator= (attr); |
| 4841 | |
| 4842 | m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter; |
| 4843 | m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore; |
| 4844 | m_lineSpacing = attr.m_lineSpacing; |
| 4845 | m_characterStyleName = attr.m_characterStyleName; |
| 4846 | m_paragraphStyleName = attr.m_paragraphStyleName; |
| 4847 | m_bulletStyle = attr.m_bulletStyle; |
| 4848 | m_bulletNumber = attr.m_bulletNumber; |
| 4849 | m_bulletSymbol = attr.m_bulletSymbol; |
| 4850 | } |
| 4851 | |
| 4852 | // Assignment from a wxTextAttr object. |
| 4853 | void wxTextAttrEx::operator= (const wxTextAttr& attr) |
| 4854 | { |
| 4855 | wxTextAttr::operator= (attr); |
| 4856 | } |
| 4857 | |
| 4858 | /*! |
| 4859 | * wxRichTextFileHandler |
| 4860 | * Base class for file handlers |
| 4861 | */ |
| 4862 | |
| 4863 | IMPLEMENT_CLASS(wxRichTextFileHandler, wxObject) |
| 4864 | |
| 4865 | #if wxUSE_STREAMS |
| 4866 | bool wxRichTextFileHandler::LoadFile(wxRichTextBuffer *buffer, const wxString& filename) |
| 4867 | { |
| 4868 | wxFFileInputStream stream(filename); |
| 4869 | if (stream.Ok()) |
| 4870 | return LoadFile(buffer, stream); |
| 4871 | |
| 4872 | return false; |
| 4873 | } |
| 4874 | |
| 4875 | bool wxRichTextFileHandler::SaveFile(wxRichTextBuffer *buffer, const wxString& filename) |
| 4876 | { |
| 4877 | wxFFileOutputStream stream(filename); |
| 4878 | if (stream.Ok()) |
| 4879 | return SaveFile(buffer, stream); |
| 4880 | |
| 4881 | return false; |
| 4882 | } |
| 4883 | #endif // wxUSE_STREAMS |
| 4884 | |
| 4885 | /// Can we handle this filename (if using files)? By default, checks the extension. |
| 4886 | bool wxRichTextFileHandler::CanHandle(const wxString& filename) const |
| 4887 | { |
| 4888 | wxString path, file, ext; |
| 4889 | wxSplitPath(filename, & path, & file, & ext); |
| 4890 | |
| 4891 | return (ext.Lower() == GetExtension()); |
| 4892 | } |
| 4893 | |
| 4894 | /*! |
| 4895 | * wxRichTextTextHandler |
| 4896 | * Plain text handler |
| 4897 | */ |
| 4898 | |
| 4899 | IMPLEMENT_CLASS(wxRichTextPlainTextHandler, wxRichTextFileHandler) |
| 4900 | |
| 4901 | #if wxUSE_STREAMS |
| 4902 | bool wxRichTextPlainTextHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream) |
| 4903 | { |
| 4904 | if (!stream.IsOk()) |
| 4905 | return false; |
| 4906 | |
| 4907 | wxString str; |
| 4908 | int lastCh = 0; |
| 4909 | |
| 4910 | while (!stream.Eof()) |
| 4911 | { |
| 4912 | int ch = stream.GetC(); |
| 4913 | |
| 4914 | if (!stream.Eof()) |
| 4915 | { |
| 4916 | if (ch == 10 && lastCh != 13) |
| 4917 | str += wxT('\n'); |
| 4918 | |
| 4919 | if (ch > 0 && ch != 10) |
| 4920 | str += wxChar(ch); |
| 4921 | |
| 4922 | lastCh = ch; |
| 4923 | } |
| 4924 | } |
| 4925 | |
| 4926 | buffer->Clear(); |
| 4927 | buffer->AddParagraphs(str); |
| 4928 | buffer->UpdateRanges(); |
| 4929 | |
| 4930 | return true; |
| 4931 | |
| 4932 | } |
| 4933 | |
| 4934 | bool wxRichTextPlainTextHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream) |
| 4935 | { |
| 4936 | if (!stream.IsOk()) |
| 4937 | return false; |
| 4938 | |
| 4939 | wxString text = buffer->GetText(); |
| 4940 | wxCharBuffer buf = text.ToAscii(); |
| 4941 | |
| 4942 | stream.Write((const char*) buf, text.Length()); |
| 4943 | return true; |
| 4944 | } |
| 4945 | #endif // wxUSE_STREAMS |
| 4946 | |
| 4947 | /* |
| 4948 | * Stores information about an image, in binary in-memory form |
| 4949 | */ |
| 4950 | |
| 4951 | wxRichTextImageBlock::wxRichTextImageBlock() |
| 4952 | { |
| 4953 | Init(); |
| 4954 | } |
| 4955 | |
| 4956 | wxRichTextImageBlock::wxRichTextImageBlock(const wxRichTextImageBlock& block):wxObject() |
| 4957 | { |
| 4958 | Init(); |
| 4959 | Copy(block); |
| 4960 | } |
| 4961 | |
| 4962 | wxRichTextImageBlock::~wxRichTextImageBlock() |
| 4963 | { |
| 4964 | if (m_data) |
| 4965 | { |
| 4966 | delete[] m_data; |
| 4967 | m_data = NULL; |
| 4968 | } |
| 4969 | } |
| 4970 | |
| 4971 | void wxRichTextImageBlock::Init() |
| 4972 | { |
| 4973 | m_data = NULL; |
| 4974 | m_dataSize = 0; |
| 4975 | m_imageType = -1; |
| 4976 | } |
| 4977 | |
| 4978 | void wxRichTextImageBlock::Clear() |
| 4979 | { |
| 4980 | delete[] m_data; |
| 4981 | m_data = NULL; |
| 4982 | m_dataSize = 0; |
| 4983 | m_imageType = -1; |
| 4984 | } |
| 4985 | |
| 4986 | |
| 4987 | // Load the original image into a memory block. |
| 4988 | // If the image is not a JPEG, we must convert it into a JPEG |
| 4989 | // to conserve space. |
| 4990 | // If it's not a JPEG we can make use of 'image', already scaled, so we don't have to |
| 4991 | // load the image a 2nd time. |
| 4992 | |
| 4993 | bool wxRichTextImageBlock::MakeImageBlock(const wxString& filename, int imageType, wxImage& image, bool convertToJPEG) |
| 4994 | { |
| 4995 | m_imageType = imageType; |
| 4996 | |
| 4997 | wxString filenameToRead(filename); |
| 4998 | bool removeFile = false; |
| 4999 | |
| 5000 | if (imageType == -1) |
| 5001 | return false; // Could not determine image type |
| 5002 | |
| 5003 | if ((imageType != wxBITMAP_TYPE_JPEG) && convertToJPEG) |
| 5004 | { |
| 5005 | wxString tempFile; |
| 5006 | bool success = wxGetTempFileName(_("image"), tempFile) ; |
| 5007 | |
| 5008 | wxASSERT(success); |
| 5009 | |
| 5010 | wxUnusedVar(success); |
| 5011 | |
| 5012 | image.SaveFile(tempFile, wxBITMAP_TYPE_JPEG); |
| 5013 | filenameToRead = tempFile; |
| 5014 | removeFile = true; |
| 5015 | |
| 5016 | m_imageType = wxBITMAP_TYPE_JPEG; |
| 5017 | } |
| 5018 | wxFile file; |
| 5019 | if (!file.Open(filenameToRead)) |
| 5020 | return false; |
| 5021 | |
| 5022 | m_dataSize = (size_t) file.Length(); |
| 5023 | file.Close(); |
| 5024 | |
| 5025 | if (m_data) |
| 5026 | delete[] m_data; |
| 5027 | m_data = ReadBlock(filenameToRead, m_dataSize); |
| 5028 | |
| 5029 | if (removeFile) |
| 5030 | wxRemoveFile(filenameToRead); |
| 5031 | |
| 5032 | return (m_data != NULL); |
| 5033 | } |
| 5034 | |
| 5035 | // Make an image block from the wxImage in the given |
| 5036 | // format. |
| 5037 | bool wxRichTextImageBlock::MakeImageBlock(wxImage& image, int imageType, int quality) |
| 5038 | { |
| 5039 | m_imageType = imageType; |
| 5040 | image.SetOption(wxT("quality"), quality); |
| 5041 | |
| 5042 | if (imageType == -1) |
| 5043 | return false; // Could not determine image type |
| 5044 | |
| 5045 | wxString tempFile; |
| 5046 | bool success = wxGetTempFileName(_("image"), tempFile) ; |
| 5047 | |
| 5048 | wxASSERT(success); |
| 5049 | wxUnusedVar(success); |
| 5050 | |
| 5051 | if (!image.SaveFile(tempFile, m_imageType)) |
| 5052 | { |
| 5053 | if (wxFileExists(tempFile)) |
| 5054 | wxRemoveFile(tempFile); |
| 5055 | return false; |
| 5056 | } |
| 5057 | |
| 5058 | wxFile file; |
| 5059 | if (!file.Open(tempFile)) |
| 5060 | return false; |
| 5061 | |
| 5062 | m_dataSize = (size_t) file.Length(); |
| 5063 | file.Close(); |
| 5064 | |
| 5065 | if (m_data) |
| 5066 | delete[] m_data; |
| 5067 | m_data = ReadBlock(tempFile, m_dataSize); |
| 5068 | |
| 5069 | wxRemoveFile(tempFile); |
| 5070 | |
| 5071 | return (m_data != NULL); |
| 5072 | } |
| 5073 | |
| 5074 | |
| 5075 | // Write to a file |
| 5076 | bool wxRichTextImageBlock::Write(const wxString& filename) |
| 5077 | { |
| 5078 | return WriteBlock(filename, m_data, m_dataSize); |
| 5079 | } |
| 5080 | |
| 5081 | void wxRichTextImageBlock::Copy(const wxRichTextImageBlock& block) |
| 5082 | { |
| 5083 | m_imageType = block.m_imageType; |
| 5084 | if (m_data) |
| 5085 | { |
| 5086 | delete[] m_data; |
| 5087 | m_data = NULL; |
| 5088 | } |
| 5089 | m_dataSize = block.m_dataSize; |
| 5090 | if (m_dataSize == 0) |
| 5091 | return; |
| 5092 | |
| 5093 | m_data = new unsigned char[m_dataSize]; |
| 5094 | unsigned int i; |
| 5095 | for (i = 0; i < m_dataSize; i++) |
| 5096 | m_data[i] = block.m_data[i]; |
| 5097 | } |
| 5098 | |
| 5099 | //// Operators |
| 5100 | void wxRichTextImageBlock::operator=(const wxRichTextImageBlock& block) |
| 5101 | { |
| 5102 | Copy(block); |
| 5103 | } |
| 5104 | |
| 5105 | // Load a wxImage from the block |
| 5106 | bool wxRichTextImageBlock::Load(wxImage& image) |
| 5107 | { |
| 5108 | if (!m_data) |
| 5109 | return false; |
| 5110 | |
| 5111 | // Read in the image. |
| 5112 | #if 1 |
| 5113 | wxMemoryInputStream mstream(m_data, m_dataSize); |
| 5114 | bool success = image.LoadFile(mstream, GetImageType()); |
| 5115 | #else |
| 5116 | wxString tempFile; |
| 5117 | bool success = wxGetTempFileName(_("image"), tempFile) ; |
| 5118 | wxASSERT(success); |
| 5119 | |
| 5120 | if (!WriteBlock(tempFile, m_data, m_dataSize)) |
| 5121 | { |
| 5122 | return false; |
| 5123 | } |
| 5124 | success = image.LoadFile(tempFile, GetImageType()); |
| 5125 | wxRemoveFile(tempFile); |
| 5126 | #endif |
| 5127 | |
| 5128 | return success; |
| 5129 | } |
| 5130 | |
| 5131 | // Write data in hex to a stream |
| 5132 | bool wxRichTextImageBlock::WriteHex(wxOutputStream& stream) |
| 5133 | { |
| 5134 | wxString hex; |
| 5135 | int i; |
| 5136 | for (i = 0; i < (int) m_dataSize; i++) |
| 5137 | { |
| 5138 | hex = wxDecToHex(m_data[i]); |
| 5139 | wxCharBuffer buf = hex.ToAscii(); |
| 5140 | |
| 5141 | stream.Write((const char*) buf, hex.Length()); |
| 5142 | } |
| 5143 | |
| 5144 | return true; |
| 5145 | } |
| 5146 | |
| 5147 | // Read data in hex from a stream |
| 5148 | bool wxRichTextImageBlock::ReadHex(wxInputStream& stream, int length, int imageType) |
| 5149 | { |
| 5150 | int dataSize = length/2; |
| 5151 | |
| 5152 | if (m_data) |
| 5153 | delete[] m_data; |
| 5154 | |
| 5155 | wxString str(wxT(" ")); |
| 5156 | m_data = new unsigned char[dataSize]; |
| 5157 | int i; |
| 5158 | for (i = 0; i < dataSize; i ++) |
| 5159 | { |
| 5160 | str[0] = stream.GetC(); |
| 5161 | str[1] = stream.GetC(); |
| 5162 | |
| 5163 | m_data[i] = (unsigned char)wxHexToDec(str); |
| 5164 | } |
| 5165 | |
| 5166 | m_dataSize = dataSize; |
| 5167 | m_imageType = imageType; |
| 5168 | |
| 5169 | return true; |
| 5170 | } |
| 5171 | |
| 5172 | |
| 5173 | // Allocate and read from stream as a block of memory |
| 5174 | unsigned char* wxRichTextImageBlock::ReadBlock(wxInputStream& stream, size_t size) |
| 5175 | { |
| 5176 | unsigned char* block = new unsigned char[size]; |
| 5177 | if (!block) |
| 5178 | return NULL; |
| 5179 | |
| 5180 | stream.Read(block, size); |
| 5181 | |
| 5182 | return block; |
| 5183 | } |
| 5184 | |
| 5185 | unsigned char* wxRichTextImageBlock::ReadBlock(const wxString& filename, size_t size) |
| 5186 | { |
| 5187 | wxFileInputStream stream(filename); |
| 5188 | if (!stream.Ok()) |
| 5189 | return NULL; |
| 5190 | |
| 5191 | return ReadBlock(stream, size); |
| 5192 | } |
| 5193 | |
| 5194 | // Write memory block to stream |
| 5195 | bool wxRichTextImageBlock::WriteBlock(wxOutputStream& stream, unsigned char* block, size_t size) |
| 5196 | { |
| 5197 | stream.Write((void*) block, size); |
| 5198 | return stream.IsOk(); |
| 5199 | |
| 5200 | } |
| 5201 | |
| 5202 | // Write memory block to file |
| 5203 | bool wxRichTextImageBlock::WriteBlock(const wxString& filename, unsigned char* block, size_t size) |
| 5204 | { |
| 5205 | wxFileOutputStream outStream(filename); |
| 5206 | if (!outStream.Ok()) |
| 5207 | return false; |
| 5208 | |
| 5209 | return WriteBlock(outStream, block, size); |
| 5210 | } |
| 5211 | |
| 5212 | #endif |
| 5213 | // wxUSE_RICHTEXT |