| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/richtext/richeditctrl.cpp |
| 3 | // Purpose: A rich edit control |
| 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/richtextctrl.h" |
| 22 | |
| 23 | #ifndef WX_PRECOMP |
| 24 | #include "wx/wx.h" |
| 25 | #include "wx/settings.h" |
| 26 | #endif |
| 27 | |
| 28 | #include "wx/textfile.h" |
| 29 | #include "wx/ffile.h" |
| 30 | #include "wx/filename.h" |
| 31 | #include "wx/dcbuffer.h" |
| 32 | #include "wx/arrimpl.cpp" |
| 33 | |
| 34 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED) |
| 35 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED) |
| 36 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK) |
| 37 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK) |
| 38 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK) |
| 39 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK) |
| 40 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN) |
| 41 | |
| 42 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
| 43 | IMPLEMENT_CLASS( wxRichTextCtrl, wxControl ) |
| 44 | #else |
| 45 | IMPLEMENT_CLASS( wxRichTextCtrl, wxScrolledWindow ) |
| 46 | #endif |
| 47 | |
| 48 | IMPLEMENT_CLASS( wxRichTextEvent, wxNotifyEvent ) |
| 49 | |
| 50 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
| 51 | BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl ) |
| 52 | #else |
| 53 | BEGIN_EVENT_TABLE( wxRichTextCtrl, wxScrolledWindow ) |
| 54 | #endif |
| 55 | EVT_PAINT(wxRichTextCtrl::OnPaint) |
| 56 | EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground) |
| 57 | EVT_IDLE(wxRichTextCtrl::OnIdle) |
| 58 | EVT_SCROLLWIN(wxRichTextCtrl::OnScroll) |
| 59 | EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick) |
| 60 | EVT_MOTION(wxRichTextCtrl::OnMoveMouse) |
| 61 | EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp) |
| 62 | EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick) |
| 63 | EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick) |
| 64 | EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick) |
| 65 | EVT_CHAR(wxRichTextCtrl::OnChar) |
| 66 | EVT_SIZE(wxRichTextCtrl::OnSize) |
| 67 | EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus) |
| 68 | EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus) |
| 69 | EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu) |
| 70 | |
| 71 | EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo) |
| 72 | EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo) |
| 73 | |
| 74 | EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo) |
| 75 | EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo) |
| 76 | |
| 77 | EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy) |
| 78 | EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy) |
| 79 | |
| 80 | EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste) |
| 81 | EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste) |
| 82 | |
| 83 | EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut) |
| 84 | EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut) |
| 85 | |
| 86 | EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear) |
| 87 | EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear) |
| 88 | |
| 89 | EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll) |
| 90 | EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll) |
| 91 | END_EVENT_TABLE() |
| 92 | |
| 93 | /*! |
| 94 | * wxRichTextCtrl |
| 95 | */ |
| 96 | |
| 97 | wxRichTextCtrl::wxRichTextCtrl() |
| 98 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
| 99 | : wxScrollHelper(this) |
| 100 | #endif |
| 101 | { |
| 102 | Init(); |
| 103 | } |
| 104 | |
| 105 | wxRichTextCtrl::wxRichTextCtrl( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) |
| 106 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
| 107 | : wxScrollHelper(this) |
| 108 | #endif |
| 109 | { |
| 110 | Init(); |
| 111 | Create(parent, id, pos, size, style); |
| 112 | } |
| 113 | |
| 114 | /// Creation |
| 115 | bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) |
| 116 | { |
| 117 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
| 118 | if (!wxTextCtrlBase::Create(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE |
| 119 | )) |
| 120 | return false; |
| 121 | #else |
| 122 | if (!wxScrolledWindow::Create(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE |
| 123 | )) |
| 124 | return false; |
| 125 | #endif |
| 126 | |
| 127 | if (!GetFont().Ok()) |
| 128 | { |
| 129 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
| 130 | } |
| 131 | |
| 132 | GetBuffer().SetRichTextCtrl(this); |
| 133 | |
| 134 | wxTextAttrEx attributes; |
| 135 | attributes.SetFont(GetFont()); |
| 136 | attributes.SetTextColour(*wxBLACK); |
| 137 | attributes.SetBackgroundColour(*wxWHITE); |
| 138 | attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT); |
| 139 | attributes.SetFlags(wxTEXT_ATTR_ALL); |
| 140 | |
| 141 | SetDefaultStyle(attributes); |
| 142 | SetBasicStyle(attributes); |
| 143 | |
| 144 | SetBackgroundColour(*wxWHITE); |
| 145 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); |
| 146 | |
| 147 | // Tell the sizers to use the given or best size |
| 148 | SetBestFittingSize(size); |
| 149 | |
| 150 | // Create a buffer |
| 151 | RecreateBuffer(size); |
| 152 | |
| 153 | SetCursor(wxCursor(wxCURSOR_IBEAM)); |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | wxRichTextCtrl::~wxRichTextCtrl() |
| 159 | { |
| 160 | delete m_contextMenu; |
| 161 | } |
| 162 | |
| 163 | /// Member initialisation |
| 164 | void wxRichTextCtrl::Init() |
| 165 | { |
| 166 | m_freezeCount = 0; |
| 167 | m_contextMenu = NULL; |
| 168 | m_caret = NULL; |
| 169 | m_caretPosition = -1; |
| 170 | m_selectionRange.SetRange(-2, -2); |
| 171 | m_selectionAnchor = -2; |
| 172 | m_editable = true; |
| 173 | m_caretAtLineStart = false; |
| 174 | m_dragging = false; |
| 175 | m_fullLayoutRequired = false; |
| 176 | m_fullLayoutTime = 0; |
| 177 | m_fullLayoutSavedPosition = 0; |
| 178 | m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD; |
| 179 | } |
| 180 | |
| 181 | /// Call Freeze to prevent refresh |
| 182 | void wxRichTextCtrl::Freeze() |
| 183 | { |
| 184 | m_freezeCount ++; |
| 185 | } |
| 186 | |
| 187 | /// Call Thaw to refresh |
| 188 | void wxRichTextCtrl::Thaw() |
| 189 | { |
| 190 | m_freezeCount --; |
| 191 | |
| 192 | if (m_freezeCount == 0) |
| 193 | { |
| 194 | SetupScrollbars(); |
| 195 | Refresh(false); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /// Clear all text |
| 200 | void wxRichTextCtrl::Clear() |
| 201 | { |
| 202 | m_buffer.Reset(); |
| 203 | m_buffer.SetDirty(true); |
| 204 | m_caretPosition = -1; |
| 205 | m_caretAtLineStart = false; |
| 206 | m_selectionRange.SetRange(-2, -2); |
| 207 | |
| 208 | if (m_freezeCount == 0) |
| 209 | { |
| 210 | SetupScrollbars(); |
| 211 | Refresh(false); |
| 212 | } |
| 213 | SendUpdateEvent(); |
| 214 | } |
| 215 | |
| 216 | /// Painting |
| 217 | void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) |
| 218 | { |
| 219 | if (GetCaret()) |
| 220 | GetCaret()->Hide(); |
| 221 | |
| 222 | { |
| 223 | wxBufferedPaintDC dc(this, m_bufferBitmap); |
| 224 | //wxLogDebug(wxT("OnPaint")); |
| 225 | |
| 226 | PrepareDC(dc); |
| 227 | |
| 228 | if (m_freezeCount > 0) |
| 229 | return; |
| 230 | |
| 231 | dc.SetFont(GetFont()); |
| 232 | |
| 233 | // Paint the background |
| 234 | PaintBackground(dc); |
| 235 | |
| 236 | wxRegion dirtyRegion = GetUpdateRegion(); |
| 237 | |
| 238 | wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize()); |
| 239 | wxRect availableSpace(GetClientSize()); |
| 240 | if (GetBuffer().GetDirty()) |
| 241 | { |
| 242 | GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT); |
| 243 | GetBuffer().SetDirty(false); |
| 244 | SetupScrollbars(); |
| 245 | } |
| 246 | |
| 247 | GetBuffer().Draw(dc, GetBuffer().GetRange(), GetSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */); |
| 248 | } |
| 249 | |
| 250 | if (GetCaret()) |
| 251 | GetCaret()->Show(); |
| 252 | |
| 253 | PositionCaret(); |
| 254 | } |
| 255 | |
| 256 | // Empty implementation, to prevent flicker |
| 257 | void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
| 258 | { |
| 259 | } |
| 260 | |
| 261 | void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) |
| 262 | { |
| 263 | wxCaret* caret = new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16); |
| 264 | SetCaret(caret); |
| 265 | caret->Show(); |
| 266 | PositionCaret(); |
| 267 | |
| 268 | if (!IsFrozen()) |
| 269 | Refresh(false); |
| 270 | } |
| 271 | |
| 272 | void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event)) |
| 273 | { |
| 274 | SetCaret(NULL); |
| 275 | |
| 276 | if (!IsFrozen()) |
| 277 | Refresh(false); |
| 278 | } |
| 279 | |
| 280 | /// Left-click |
| 281 | void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event) |
| 282 | { |
| 283 | SetFocus(); |
| 284 | |
| 285 | wxClientDC dc(this); |
| 286 | PrepareDC(dc); |
| 287 | dc.SetFont(GetFont()); |
| 288 | |
| 289 | long position = 0; |
| 290 | int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position); |
| 291 | |
| 292 | if (hit != wxRICHTEXT_HITTEST_NONE) |
| 293 | { |
| 294 | m_dragStart = event.GetLogicalPosition(dc); |
| 295 | m_dragging = true; |
| 296 | CaptureMouse(); |
| 297 | |
| 298 | SelectNone(); |
| 299 | |
| 300 | bool caretAtLineStart = false; |
| 301 | |
| 302 | if (hit & wxRICHTEXT_HITTEST_BEFORE) |
| 303 | { |
| 304 | // If we're at the start of a line (but not first in para) |
| 305 | // then we should keep the caret showing at the start of the line |
| 306 | // by showing the m_caretAtLineStart flag. |
| 307 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position); |
| 308 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(position); |
| 309 | |
| 310 | if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position) |
| 311 | caretAtLineStart = true; |
| 312 | position --; |
| 313 | } |
| 314 | |
| 315 | MoveCaret(position, caretAtLineStart); |
| 316 | SetDefaultStyleToCursorStyle(); |
| 317 | |
| 318 | #if 0 |
| 319 | wxWindow* p = GetParent(); |
| 320 | while (p && !p->IsKindOf(CLASSINFO(wxFrame))) |
| 321 | p = p->GetParent(); |
| 322 | |
| 323 | wxFrame* frame = wxDynamicCast(p, wxFrame); |
| 324 | if (frame) |
| 325 | { |
| 326 | wxString msg = wxString::Format(wxT("Found position %ld"), position); |
| 327 | frame->SetStatusText(msg, 1); |
| 328 | } |
| 329 | #endif |
| 330 | } |
| 331 | |
| 332 | event.Skip(); |
| 333 | } |
| 334 | |
| 335 | /// Left-up |
| 336 | void wxRichTextCtrl::OnLeftUp(wxMouseEvent& WXUNUSED(event)) |
| 337 | { |
| 338 | if (m_dragging) |
| 339 | { |
| 340 | m_dragging = false; |
| 341 | if (GetCapture() == this) |
| 342 | ReleaseMouse(); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /// Left-click |
| 347 | void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event) |
| 348 | { |
| 349 | if (!event.Dragging()) |
| 350 | { |
| 351 | event.Skip(); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | wxClientDC dc(this); |
| 356 | PrepareDC(dc); |
| 357 | dc.SetFont(GetFont()); |
| 358 | |
| 359 | long position = 0; |
| 360 | wxPoint logicalPt = event.GetLogicalPosition(dc); |
| 361 | int hit = GetBuffer().HitTest(dc, logicalPt, position); |
| 362 | |
| 363 | if (m_dragging && hit != wxRICHTEXT_HITTEST_NONE) |
| 364 | { |
| 365 | // TODO: test closeness |
| 366 | |
| 367 | bool caretAtLineStart = false; |
| 368 | |
| 369 | if (hit & wxRICHTEXT_HITTEST_BEFORE) |
| 370 | { |
| 371 | // If we're at the start of a line (but not first in para) |
| 372 | // then we should keep the caret showing at the start of the line |
| 373 | // by showing the m_caretAtLineStart flag. |
| 374 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position); |
| 375 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(position); |
| 376 | |
| 377 | if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position) |
| 378 | caretAtLineStart = true; |
| 379 | position --; |
| 380 | } |
| 381 | |
| 382 | if (m_caretPosition != position) |
| 383 | { |
| 384 | bool extendSel = ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN); |
| 385 | |
| 386 | MoveCaret(position, caretAtLineStart); |
| 387 | SetDefaultStyleToCursorStyle(); |
| 388 | |
| 389 | if (extendSel) |
| 390 | Refresh(false); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /// Right-click |
| 396 | void wxRichTextCtrl::OnRightClick(wxMouseEvent& event) |
| 397 | { |
| 398 | SetFocus(); |
| 399 | event.Skip(); |
| 400 | } |
| 401 | |
| 402 | /// Left-double-click |
| 403 | void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& event) |
| 404 | { |
| 405 | event.Skip(); |
| 406 | } |
| 407 | |
| 408 | /// Middle-click |
| 409 | void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event) |
| 410 | { |
| 411 | event.Skip(); |
| 412 | } |
| 413 | |
| 414 | /// Key press |
| 415 | void wxRichTextCtrl::OnChar(wxKeyEvent& event) |
| 416 | { |
| 417 | int flags = 0; |
| 418 | if (event.ControlDown()) |
| 419 | flags |= wxRICHTEXT_CTRL_DOWN; |
| 420 | if (event.ShiftDown()) |
| 421 | flags |= wxRICHTEXT_SHIFT_DOWN; |
| 422 | if (event.AltDown()) |
| 423 | flags |= wxRICHTEXT_ALT_DOWN; |
| 424 | |
| 425 | if (event.GetKeyCode() == WXK_LEFT || |
| 426 | event.GetKeyCode() == WXK_RIGHT || |
| 427 | event.GetKeyCode() == WXK_UP || |
| 428 | event.GetKeyCode() == WXK_DOWN || |
| 429 | event.GetKeyCode() == WXK_HOME || |
| 430 | event.GetKeyCode() == WXK_PAGEUP || |
| 431 | event.GetKeyCode() == WXK_PAGEDOWN || |
| 432 | event.GetKeyCode() == WXK_END) |
| 433 | { |
| 434 | KeyboardNavigate(event.GetKeyCode(), flags); |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | // all the other keys modify the controls contents which shouldn't be |
| 439 | // possible if we're read-only |
| 440 | if ( !IsEditable() ) |
| 441 | { |
| 442 | event.Skip(); |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | if (event.GetKeyCode() == WXK_RETURN) |
| 447 | { |
| 448 | BeginBatchUndo(_("Insert Text")); |
| 449 | |
| 450 | long newPos = m_caretPosition; |
| 451 | |
| 452 | DeleteSelectedContent(& newPos); |
| 453 | |
| 454 | GetBuffer().InsertNewlineWithUndo(newPos+1, this); |
| 455 | |
| 456 | wxRichTextEvent cmdEvent( |
| 457 | wxEVT_COMMAND_RICHTEXT_RETURN, |
| 458 | GetId()); |
| 459 | cmdEvent.SetEventObject(this); |
| 460 | cmdEvent.SetFlags(flags); |
| 461 | GetEventHandler()->ProcessEvent(cmdEvent); |
| 462 | |
| 463 | EndBatchUndo(); |
| 464 | SetDefaultStyleToCursorStyle(); |
| 465 | |
| 466 | ScrollIntoView(m_caretPosition, WXK_RIGHT); |
| 467 | } |
| 468 | else if (event.GetKeyCode() == WXK_BACK) |
| 469 | { |
| 470 | BeginBatchUndo(_("Delete Text")); |
| 471 | |
| 472 | // Submit range in character positions, which are greater than caret positions, |
| 473 | // so subtract 1 for deleted character and add 1 for conversion to character position. |
| 474 | if (m_caretPosition > -1 && !HasSelection()) |
| 475 | { |
| 476 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition, m_caretPosition), |
| 477 | m_caretPosition, // Current caret position |
| 478 | m_caretPosition-1, // New caret position |
| 479 | this); |
| 480 | } |
| 481 | else |
| 482 | DeleteSelectedContent(); |
| 483 | |
| 484 | EndBatchUndo(); |
| 485 | |
| 486 | // Shouldn't this be in Do()? |
| 487 | if (GetLastPosition() == -1) |
| 488 | { |
| 489 | GetBuffer().Reset(); |
| 490 | |
| 491 | m_caretPosition = -1; |
| 492 | PositionCaret(); |
| 493 | SetDefaultStyleToCursorStyle(); |
| 494 | } |
| 495 | |
| 496 | ScrollIntoView(m_caretPosition, WXK_LEFT); |
| 497 | } |
| 498 | else if (event.GetKeyCode() == WXK_DELETE) |
| 499 | { |
| 500 | BeginBatchUndo(_("Delete Text")); |
| 501 | |
| 502 | // Submit range in character positions, which are greater than caret positions, |
| 503 | if (m_caretPosition < GetBuffer().GetRange().GetEnd()+1 && !HasSelection()) |
| 504 | { |
| 505 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition+1, m_caretPosition+1), |
| 506 | m_caretPosition, // Current caret position |
| 507 | m_caretPosition+1, // New caret position |
| 508 | this); |
| 509 | } |
| 510 | else |
| 511 | DeleteSelectedContent(); |
| 512 | |
| 513 | EndBatchUndo(); |
| 514 | |
| 515 | // Shouldn't this be in Do()? |
| 516 | if (GetLastPosition() == -1) |
| 517 | { |
| 518 | GetBuffer().Reset(); |
| 519 | |
| 520 | m_caretPosition = -1; |
| 521 | PositionCaret(); |
| 522 | SetDefaultStyleToCursorStyle(); |
| 523 | } |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | BeginBatchUndo(_("Insert Text")); |
| 528 | |
| 529 | long newPos = m_caretPosition; |
| 530 | DeleteSelectedContent(& newPos); |
| 531 | |
| 532 | wxString str = (wxChar) event.GetKeyCode(); |
| 533 | GetBuffer().InsertTextWithUndo(newPos+1, str, this); |
| 534 | |
| 535 | EndBatchUndo(); |
| 536 | |
| 537 | SetDefaultStyleToCursorStyle(); |
| 538 | ScrollIntoView(m_caretPosition, WXK_RIGHT); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /// Delete content if there is a selection, e.g. when pressing a key. |
| 543 | bool wxRichTextCtrl::DeleteSelectedContent(long* newPos) |
| 544 | { |
| 545 | if (HasSelection()) |
| 546 | { |
| 547 | long pos = m_selectionRange.GetStart(); |
| 548 | GetBuffer().DeleteRangeWithUndo(m_selectionRange, |
| 549 | m_caretPosition, // Current caret position |
| 550 | pos, // New caret position |
| 551 | this); |
| 552 | m_selectionRange.SetRange(-2, -2); |
| 553 | |
| 554 | if (newPos) |
| 555 | *newPos = pos-1; |
| 556 | return true; |
| 557 | } |
| 558 | else |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | /// Keyboard navigation |
| 563 | |
| 564 | /* |
| 565 | |
| 566 | Left: left one character |
| 567 | Right: right one character |
| 568 | Up: up one line |
| 569 | Down: down one line |
| 570 | Ctrl-Left: left one word |
| 571 | Ctrl-Right: right one word |
| 572 | Ctrl-Up: previous paragraph start |
| 573 | Ctrl-Down: next start of paragraph |
| 574 | Home: start of line |
| 575 | End: end of line |
| 576 | Ctrl-Home: start of document |
| 577 | Ctrl-End: end of document |
| 578 | Page-Up: Up a screen |
| 579 | Page-Down: Down a screen |
| 580 | |
| 581 | Maybe: |
| 582 | |
| 583 | Ctrl-Alt-PgUp: Start of window |
| 584 | Ctrl-Alt-PgDn: End of window |
| 585 | F8: Start selection mode |
| 586 | Esc: End selection mode |
| 587 | |
| 588 | Adding Shift does the above but starts/extends selection. |
| 589 | |
| 590 | |
| 591 | */ |
| 592 | |
| 593 | bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags) |
| 594 | { |
| 595 | bool success = false; |
| 596 | |
| 597 | if (keyCode == WXK_RIGHT) |
| 598 | { |
| 599 | if (flags & wxRICHTEXT_CTRL_DOWN) |
| 600 | success = WordRight(1, flags); |
| 601 | else |
| 602 | success = MoveRight(1, flags); |
| 603 | } |
| 604 | else if (keyCode == WXK_LEFT) |
| 605 | { |
| 606 | if (flags & wxRICHTEXT_CTRL_DOWN) |
| 607 | success = WordLeft(1, flags); |
| 608 | else |
| 609 | success = MoveLeft(1, flags); |
| 610 | } |
| 611 | else if (keyCode == WXK_UP) |
| 612 | { |
| 613 | if (flags & wxRICHTEXT_CTRL_DOWN) |
| 614 | success = MoveToParagraphStart(flags); |
| 615 | else |
| 616 | success = MoveUp(1, flags); |
| 617 | } |
| 618 | else if (keyCode == WXK_DOWN) |
| 619 | { |
| 620 | if (flags & wxRICHTEXT_CTRL_DOWN) |
| 621 | success = MoveToParagraphEnd(flags); |
| 622 | else |
| 623 | success = MoveDown(1, flags); |
| 624 | } |
| 625 | else if (keyCode == WXK_PAGEUP) |
| 626 | { |
| 627 | success = PageUp(1, flags); |
| 628 | } |
| 629 | else if (keyCode == WXK_PAGEDOWN) |
| 630 | { |
| 631 | success = PageDown(1, flags); |
| 632 | } |
| 633 | else if (keyCode == WXK_HOME) |
| 634 | { |
| 635 | if (flags & wxRICHTEXT_CTRL_DOWN) |
| 636 | success = MoveHome(flags); |
| 637 | else |
| 638 | success = MoveToLineStart(flags); |
| 639 | } |
| 640 | else if (keyCode == WXK_END) |
| 641 | { |
| 642 | if (flags & wxRICHTEXT_CTRL_DOWN) |
| 643 | success = MoveEnd(flags); |
| 644 | else |
| 645 | success = MoveToLineEnd(flags); |
| 646 | } |
| 647 | |
| 648 | if (success) |
| 649 | { |
| 650 | ScrollIntoView(m_caretPosition, keyCode); |
| 651 | SetDefaultStyleToCursorStyle(); |
| 652 | } |
| 653 | |
| 654 | return success; |
| 655 | } |
| 656 | |
| 657 | /// Extend the selection. Selections are in caret positions. |
| 658 | bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags) |
| 659 | { |
| 660 | if (flags & wxRICHTEXT_SHIFT_DOWN) |
| 661 | { |
| 662 | // If not currently selecting, start selecting |
| 663 | if (m_selectionRange.GetStart() == -2) |
| 664 | { |
| 665 | m_selectionAnchor = oldPos; |
| 666 | |
| 667 | if (oldPos > newPos) |
| 668 | m_selectionRange.SetRange(newPos+1, oldPos); |
| 669 | else |
| 670 | m_selectionRange.SetRange(oldPos+1, newPos); |
| 671 | } |
| 672 | else |
| 673 | { |
| 674 | // Always ensure that the selection range start is greater than |
| 675 | // the end. |
| 676 | if (newPos > m_selectionAnchor) |
| 677 | m_selectionRange.SetRange(m_selectionAnchor+1, newPos); |
| 678 | else |
| 679 | m_selectionRange.SetRange(newPos+1, m_selectionAnchor); |
| 680 | } |
| 681 | |
| 682 | if (m_selectionRange.GetStart() > m_selectionRange.GetEnd()) |
| 683 | { |
| 684 | wxLogDebug(wxT("Strange selection range")); |
| 685 | } |
| 686 | |
| 687 | return true; |
| 688 | } |
| 689 | else |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | /// Scroll into view, returning true if we scrolled. |
| 694 | /// This takes a _caret_ position. |
| 695 | bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) |
| 696 | { |
| 697 | wxRichTextLine* line = GetVisibleLineForCaretPosition(position); |
| 698 | |
| 699 | if (!line) |
| 700 | return false; |
| 701 | |
| 702 | int ppuX, ppuY; |
| 703 | GetScrollPixelsPerUnit(& ppuX, & ppuY); |
| 704 | |
| 705 | int startX, startY; |
| 706 | GetViewStart(& startX, & startY); |
| 707 | startX = 0; |
| 708 | startY = startY * ppuY; |
| 709 | |
| 710 | int sx = 0, sy = 0; |
| 711 | GetVirtualSize(& sx, & sy); |
| 712 | sx = 0; |
| 713 | if (ppuY != 0) |
| 714 | sy = sy/ppuY; |
| 715 | |
| 716 | wxRect rect = line->GetRect(); |
| 717 | |
| 718 | bool scrolled = false; |
| 719 | |
| 720 | wxSize clientSize = GetClientSize(); |
| 721 | |
| 722 | // Going down |
| 723 | if (keyCode == WXK_DOWN || keyCode == WXK_RIGHT || keyCode == WXK_END || keyCode == WXK_PAGEDOWN) |
| 724 | { |
| 725 | if ((rect.y + rect.height) > (clientSize.y + startY)) |
| 726 | { |
| 727 | // Make it scroll so this item is at the bottom |
| 728 | // of the window |
| 729 | int y = rect.y - (clientSize.y - rect.height); |
| 730 | y = (int) (0.5 + y/ppuY); |
| 731 | |
| 732 | if (startY != y) |
| 733 | { |
| 734 | SetScrollbars(ppuX, ppuY, sx, sy, 0, y); |
| 735 | scrolled = true; |
| 736 | } |
| 737 | } |
| 738 | else if (rect.y < startY) |
| 739 | { |
| 740 | // Make it scroll so this item is at the top |
| 741 | // of the window |
| 742 | int y = rect.y ; |
| 743 | y = (int) (0.5 + y/ppuY); |
| 744 | |
| 745 | if (startY != y) |
| 746 | { |
| 747 | SetScrollbars(ppuX, ppuY, sx, sy, 0, y); |
| 748 | scrolled = true; |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | // Going up |
| 753 | else if (keyCode == WXK_UP || keyCode == WXK_LEFT || keyCode == WXK_HOME || keyCode == WXK_PAGEUP ) |
| 754 | { |
| 755 | if (rect.y < startY) |
| 756 | { |
| 757 | // Make it scroll so this item is at the top |
| 758 | // of the window |
| 759 | int y = rect.y ; |
| 760 | y = (int) (0.5 + y/ppuY); |
| 761 | |
| 762 | if (startY != y) |
| 763 | { |
| 764 | SetScrollbars(ppuX, ppuY, sx, sy, 0, y); |
| 765 | scrolled = true; |
| 766 | } |
| 767 | } |
| 768 | else if ((rect.y + rect.height) > (clientSize.y + startY)) |
| 769 | { |
| 770 | // Make it scroll so this item is at the bottom |
| 771 | // of the window |
| 772 | int y = rect.y - (clientSize.y - rect.height); |
| 773 | y = (int) (0.5 + y/ppuY); |
| 774 | |
| 775 | if (startY != y) |
| 776 | { |
| 777 | SetScrollbars(ppuX, ppuY, sx, sy, 0, y); |
| 778 | scrolled = true; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | PositionCaret(); |
| 783 | |
| 784 | return scrolled; |
| 785 | } |
| 786 | |
| 787 | /// Is the given position visible on the screen? |
| 788 | bool wxRichTextCtrl::IsPositionVisible(long pos) const |
| 789 | { |
| 790 | wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1); |
| 791 | |
| 792 | if (!line) |
| 793 | return false; |
| 794 | |
| 795 | int ppuX, ppuY; |
| 796 | GetScrollPixelsPerUnit(& ppuX, & ppuY); |
| 797 | |
| 798 | int startX, startY; |
| 799 | GetViewStart(& startX, & startY); |
| 800 | startX = 0; |
| 801 | startY = startY * ppuY; |
| 802 | |
| 803 | int sx = 0, sy = 0; |
| 804 | GetVirtualSize(& sx, & sy); |
| 805 | sx = 0; |
| 806 | if (ppuY != 0) |
| 807 | sy = sy/ppuY; |
| 808 | |
| 809 | wxRect rect = line->GetRect(); |
| 810 | |
| 811 | wxSize clientSize = GetClientSize(); |
| 812 | |
| 813 | return !(((rect.y + rect.height) > (clientSize.y + startY)) || rect.y < startY); |
| 814 | } |
| 815 | |
| 816 | void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart) |
| 817 | { |
| 818 | m_caretPosition = position; |
| 819 | m_caretAtLineStart = showAtLineStart; |
| 820 | } |
| 821 | |
| 822 | /// Move caret one visual step forward: this may mean setting a flag |
| 823 | /// and keeping the same position if we're going from the end of one line |
| 824 | /// to the start of the next, which may be the exact same caret position. |
| 825 | void wxRichTextCtrl::MoveCaretForward(long oldPosition) |
| 826 | { |
| 827 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition); |
| 828 | |
| 829 | // Only do the check if we're not at the end of the paragraph (where things work OK |
| 830 | // anyway) |
| 831 | if (para && (oldPosition != para->GetRange().GetEnd() - 1)) |
| 832 | { |
| 833 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition); |
| 834 | |
| 835 | if (line) |
| 836 | { |
| 837 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 838 | |
| 839 | // We're at the end of a line. See whether we need to |
| 840 | // stay at the same actual caret position but change visual |
| 841 | // position, or not. |
| 842 | if (oldPosition == lineRange.GetEnd()) |
| 843 | { |
| 844 | if (m_caretAtLineStart) |
| 845 | { |
| 846 | // We're already at the start of the line, so actually move on now. |
| 847 | m_caretPosition = oldPosition + 1; |
| 848 | m_caretAtLineStart = false; |
| 849 | } |
| 850 | else |
| 851 | { |
| 852 | // We're showing at the end of the line, so keep to |
| 853 | // the same position but indicate that we're to show |
| 854 | // at the start of the next line. |
| 855 | m_caretPosition = oldPosition; |
| 856 | m_caretAtLineStart = true; |
| 857 | } |
| 858 | SetDefaultStyleToCursorStyle(); |
| 859 | return; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | m_caretPosition ++; |
| 864 | SetDefaultStyleToCursorStyle(); |
| 865 | } |
| 866 | |
| 867 | /// Move caret one visual step backward: this may mean setting a flag |
| 868 | /// and keeping the same position if we're going from the end of one line |
| 869 | /// to the start of the next, which may be the exact same caret position. |
| 870 | void wxRichTextCtrl::MoveCaretBack(long oldPosition) |
| 871 | { |
| 872 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition); |
| 873 | |
| 874 | // Only do the check if we're not at the start of the paragraph (where things work OK |
| 875 | // anyway) |
| 876 | if (para && (oldPosition != para->GetRange().GetStart())) |
| 877 | { |
| 878 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition); |
| 879 | |
| 880 | if (line) |
| 881 | { |
| 882 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 883 | |
| 884 | // We're at the start of a line. See whether we need to |
| 885 | // stay at the same actual caret position but change visual |
| 886 | // position, or not. |
| 887 | if (oldPosition == lineRange.GetStart()) |
| 888 | { |
| 889 | m_caretPosition = oldPosition-1; |
| 890 | m_caretAtLineStart = true; |
| 891 | return; |
| 892 | } |
| 893 | else if (oldPosition == lineRange.GetEnd()) |
| 894 | { |
| 895 | if (m_caretAtLineStart) |
| 896 | { |
| 897 | // We're at the start of the line, so keep the same caret position |
| 898 | // but clear the start-of-line flag. |
| 899 | m_caretPosition = oldPosition; |
| 900 | m_caretAtLineStart = false; |
| 901 | } |
| 902 | else |
| 903 | { |
| 904 | // We're showing at the end of the line, so go back |
| 905 | // to the previous character position. |
| 906 | m_caretPosition = oldPosition - 1; |
| 907 | } |
| 908 | SetDefaultStyleToCursorStyle(); |
| 909 | return; |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | m_caretPosition --; |
| 914 | SetDefaultStyleToCursorStyle(); |
| 915 | } |
| 916 | |
| 917 | /// Move right |
| 918 | bool wxRichTextCtrl::MoveRight(int noPositions, int flags) |
| 919 | { |
| 920 | long endPos = GetBuffer().GetRange().GetEnd(); |
| 921 | |
| 922 | if (m_caretPosition + noPositions < endPos) |
| 923 | { |
| 924 | long oldPos = m_caretPosition; |
| 925 | long newPos = m_caretPosition + noPositions; |
| 926 | |
| 927 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
| 928 | if (!extendSel) |
| 929 | SelectNone(); |
| 930 | |
| 931 | // Determine by looking at oldPos and m_caretPosition whether |
| 932 | // we moved from the end of a line to the start of the next line, in which case |
| 933 | // we want to adjust the caret position such that it is positioned at the |
| 934 | // start of the next line, rather than jumping past the first character of the |
| 935 | // line. |
| 936 | if (noPositions == 1 && !extendSel) |
| 937 | MoveCaretForward(oldPos); |
| 938 | else |
| 939 | SetCaretPosition(newPos); |
| 940 | |
| 941 | PositionCaret(); |
| 942 | SetDefaultStyleToCursorStyle(); |
| 943 | |
| 944 | if (extendSel) |
| 945 | Refresh(false); |
| 946 | return true; |
| 947 | } |
| 948 | else |
| 949 | return false; |
| 950 | } |
| 951 | |
| 952 | /// Move left |
| 953 | bool wxRichTextCtrl::MoveLeft(int noPositions, int flags) |
| 954 | { |
| 955 | long startPos = -1; |
| 956 | |
| 957 | if (m_caretPosition > startPos - noPositions + 1) |
| 958 | { |
| 959 | long oldPos = m_caretPosition; |
| 960 | long newPos = m_caretPosition - noPositions; |
| 961 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
| 962 | if (!extendSel) |
| 963 | SelectNone(); |
| 964 | |
| 965 | if (noPositions == 1 && !extendSel) |
| 966 | MoveCaretBack(oldPos); |
| 967 | else |
| 968 | SetCaretPosition(newPos); |
| 969 | |
| 970 | PositionCaret(); |
| 971 | SetDefaultStyleToCursorStyle(); |
| 972 | |
| 973 | if (extendSel) |
| 974 | Refresh(false); |
| 975 | return true; |
| 976 | } |
| 977 | else |
| 978 | return false; |
| 979 | } |
| 980 | |
| 981 | /// Move up |
| 982 | bool wxRichTextCtrl::MoveUp(int noLines, int flags) |
| 983 | { |
| 984 | return MoveDown(- noLines, flags); |
| 985 | } |
| 986 | |
| 987 | /// Move up |
| 988 | bool wxRichTextCtrl::MoveDown(int noLines, int flags) |
| 989 | { |
| 990 | if (!GetCaret()) |
| 991 | return false; |
| 992 | |
| 993 | long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart); |
| 994 | wxPoint pt = GetCaret()->GetPosition(); |
| 995 | long newLine = lineNumber + noLines; |
| 996 | |
| 997 | if (lineNumber != -1) |
| 998 | { |
| 999 | if (noLines > 0) |
| 1000 | { |
| 1001 | long lastLine = GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd()); |
| 1002 | |
| 1003 | if (newLine > lastLine) |
| 1004 | return false; |
| 1005 | } |
| 1006 | else |
| 1007 | { |
| 1008 | if (newLine < 0) |
| 1009 | return false; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | wxRichTextLine* lineObj = GetBuffer().GetLineForVisibleLineNumber(newLine); |
| 1014 | if (lineObj) |
| 1015 | { |
| 1016 | pt.y = lineObj->GetAbsolutePosition().y + 2; |
| 1017 | } |
| 1018 | else |
| 1019 | return false; |
| 1020 | |
| 1021 | long newPos = 0; |
| 1022 | wxClientDC dc(this); |
| 1023 | PrepareDC(dc); |
| 1024 | dc.SetFont(GetFont()); |
| 1025 | |
| 1026 | int hitTest = GetBuffer().HitTest(dc, pt, newPos); |
| 1027 | |
| 1028 | if (hitTest != wxRICHTEXT_HITTEST_NONE) |
| 1029 | { |
| 1030 | // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE, |
| 1031 | // we want to be at the end of the last line but with m_caretAtLineStart set to true, |
| 1032 | // so we view the caret at the start of the line. |
| 1033 | bool caretLineStart = false; |
| 1034 | if (hitTest == wxRICHTEXT_HITTEST_BEFORE) |
| 1035 | { |
| 1036 | wxRichTextLine* thisLine = GetBuffer().GetLineAtPosition(newPos-1); |
| 1037 | wxRichTextRange lineRange; |
| 1038 | if (thisLine) |
| 1039 | lineRange = thisLine->GetAbsoluteRange(); |
| 1040 | |
| 1041 | if (thisLine && (newPos-1) == lineRange.GetEnd()) |
| 1042 | { |
| 1043 | newPos --; |
| 1044 | caretLineStart = true; |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(newPos); |
| 1049 | if (para && para->GetRange().GetStart() == newPos) |
| 1050 | newPos --; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | long newSelEnd = newPos; |
| 1055 | |
| 1056 | bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags); |
| 1057 | if (!extendSel) |
| 1058 | SelectNone(); |
| 1059 | |
| 1060 | SetCaretPosition(newPos, caretLineStart); |
| 1061 | PositionCaret(); |
| 1062 | SetDefaultStyleToCursorStyle(); |
| 1063 | |
| 1064 | if (extendSel) |
| 1065 | Refresh(false); |
| 1066 | return true; |
| 1067 | } |
| 1068 | |
| 1069 | return false; |
| 1070 | } |
| 1071 | |
| 1072 | /// Move to the end of the paragraph |
| 1073 | bool wxRichTextCtrl::MoveToParagraphEnd(int flags) |
| 1074 | { |
| 1075 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true); |
| 1076 | if (para) |
| 1077 | { |
| 1078 | long newPos = para->GetRange().GetEnd() - 1; |
| 1079 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
| 1080 | if (!extendSel) |
| 1081 | SelectNone(); |
| 1082 | |
| 1083 | SetCaretPosition(newPos); |
| 1084 | PositionCaret(); |
| 1085 | SetDefaultStyleToCursorStyle(); |
| 1086 | |
| 1087 | if (extendSel) |
| 1088 | Refresh(false); |
| 1089 | return true; |
| 1090 | } |
| 1091 | |
| 1092 | return false; |
| 1093 | } |
| 1094 | |
| 1095 | /// Move to the start of the paragraph |
| 1096 | bool wxRichTextCtrl::MoveToParagraphStart(int flags) |
| 1097 | { |
| 1098 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true); |
| 1099 | if (para) |
| 1100 | { |
| 1101 | long newPos = para->GetRange().GetStart() - 1; |
| 1102 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
| 1103 | if (!extendSel) |
| 1104 | SelectNone(); |
| 1105 | |
| 1106 | SetCaretPosition(newPos); |
| 1107 | PositionCaret(); |
| 1108 | SetDefaultStyleToCursorStyle(); |
| 1109 | |
| 1110 | if (extendSel) |
| 1111 | Refresh(false); |
| 1112 | return true; |
| 1113 | } |
| 1114 | |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
| 1118 | /// Move to the end of the line |
| 1119 | bool wxRichTextCtrl::MoveToLineEnd(int flags) |
| 1120 | { |
| 1121 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); |
| 1122 | |
| 1123 | if (line) |
| 1124 | { |
| 1125 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 1126 | long newPos = lineRange.GetEnd(); |
| 1127 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
| 1128 | if (!extendSel) |
| 1129 | SelectNone(); |
| 1130 | |
| 1131 | SetCaretPosition(newPos); |
| 1132 | PositionCaret(); |
| 1133 | SetDefaultStyleToCursorStyle(); |
| 1134 | |
| 1135 | if (extendSel) |
| 1136 | Refresh(false); |
| 1137 | return true; |
| 1138 | } |
| 1139 | |
| 1140 | return false; |
| 1141 | } |
| 1142 | |
| 1143 | /// Move to the start of the line |
| 1144 | bool wxRichTextCtrl::MoveToLineStart(int flags) |
| 1145 | { |
| 1146 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); |
| 1147 | if (line) |
| 1148 | { |
| 1149 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 1150 | long newPos = lineRange.GetStart()-1; |
| 1151 | |
| 1152 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
| 1153 | if (!extendSel) |
| 1154 | SelectNone(); |
| 1155 | |
| 1156 | wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line); |
| 1157 | |
| 1158 | SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart()); |
| 1159 | PositionCaret(); |
| 1160 | SetDefaultStyleToCursorStyle(); |
| 1161 | |
| 1162 | if (extendSel) |
| 1163 | Refresh(false); |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
| 1167 | return false; |
| 1168 | } |
| 1169 | |
| 1170 | /// Move to the start of the buffer |
| 1171 | bool wxRichTextCtrl::MoveHome(int flags) |
| 1172 | { |
| 1173 | if (m_caretPosition != -1) |
| 1174 | { |
| 1175 | bool extendSel = ExtendSelection(m_caretPosition, -1, flags); |
| 1176 | if (!extendSel) |
| 1177 | SelectNone(); |
| 1178 | |
| 1179 | SetCaretPosition(-1); |
| 1180 | PositionCaret(); |
| 1181 | SetDefaultStyleToCursorStyle(); |
| 1182 | |
| 1183 | if (extendSel) |
| 1184 | Refresh(false); |
| 1185 | return true; |
| 1186 | } |
| 1187 | else |
| 1188 | return false; |
| 1189 | } |
| 1190 | |
| 1191 | /// Move to the end of the buffer |
| 1192 | bool wxRichTextCtrl::MoveEnd(int flags) |
| 1193 | { |
| 1194 | long endPos = GetBuffer().GetRange().GetEnd()-1; |
| 1195 | |
| 1196 | if (m_caretPosition != endPos) |
| 1197 | { |
| 1198 | bool extendSel = ExtendSelection(m_caretPosition, endPos, flags); |
| 1199 | if (!extendSel) |
| 1200 | SelectNone(); |
| 1201 | |
| 1202 | SetCaretPosition(endPos); |
| 1203 | PositionCaret(); |
| 1204 | SetDefaultStyleToCursorStyle(); |
| 1205 | |
| 1206 | if (extendSel) |
| 1207 | Refresh(false); |
| 1208 | return true; |
| 1209 | } |
| 1210 | else |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
| 1214 | /// Move noPages pages up |
| 1215 | bool wxRichTextCtrl::PageUp(int noPages, int flags) |
| 1216 | { |
| 1217 | return PageDown(- noPages, flags); |
| 1218 | } |
| 1219 | |
| 1220 | /// Move noPages pages down |
| 1221 | bool wxRichTextCtrl::PageDown(int noPages, int flags) |
| 1222 | { |
| 1223 | // Calculate which line occurs noPages * screen height further down. |
| 1224 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); |
| 1225 | if (line) |
| 1226 | { |
| 1227 | wxSize clientSize = GetClientSize(); |
| 1228 | int newY = line->GetAbsolutePosition().y + noPages*clientSize.y; |
| 1229 | |
| 1230 | wxRichTextLine* newLine = GetBuffer().GetLineAtYPosition(newY); |
| 1231 | if (newLine) |
| 1232 | { |
| 1233 | wxRichTextRange lineRange = newLine->GetAbsoluteRange(); |
| 1234 | long pos = lineRange.GetStart()-1; |
| 1235 | if (pos != m_caretPosition) |
| 1236 | { |
| 1237 | wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine); |
| 1238 | |
| 1239 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
| 1240 | if (!extendSel) |
| 1241 | SelectNone(); |
| 1242 | |
| 1243 | SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart()); |
| 1244 | PositionCaret(); |
| 1245 | SetDefaultStyleToCursorStyle(); |
| 1246 | |
| 1247 | if (extendSel) |
| 1248 | Refresh(false); |
| 1249 | return true; |
| 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | return false; |
| 1255 | } |
| 1256 | |
| 1257 | // Finds the caret position for the next word |
| 1258 | long wxRichTextCtrl::FindNextWordPosition(int direction) const |
| 1259 | { |
| 1260 | long endPos = GetBuffer().GetRange().GetEnd(); |
| 1261 | |
| 1262 | if (direction > 0) |
| 1263 | { |
| 1264 | long i = m_caretPosition+1+direction; // +1 for conversion to character pos |
| 1265 | |
| 1266 | // First skip current text to space |
| 1267 | while (i < endPos && i > -1) |
| 1268 | { |
| 1269 | // i is in character, not caret positions |
| 1270 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); |
| 1271 | if (text != wxT(" ") && !text.empty()) |
| 1272 | i += direction; |
| 1273 | else |
| 1274 | { |
| 1275 | break; |
| 1276 | } |
| 1277 | } |
| 1278 | while (i < endPos && i > -1) |
| 1279 | { |
| 1280 | // i is in character, not caret positions |
| 1281 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); |
| 1282 | if (text.empty()) // End of paragraph, or maybe an image |
| 1283 | return wxMax(-1, i - 1); |
| 1284 | else if (text == wxT(" ") || text.empty()) |
| 1285 | i += direction; |
| 1286 | else |
| 1287 | { |
| 1288 | // Convert to caret position |
| 1289 | return wxMax(-1, i - 1); |
| 1290 | } |
| 1291 | } |
| 1292 | if (i >= endPos) |
| 1293 | return endPos-1; |
| 1294 | return i-1; |
| 1295 | } |
| 1296 | else |
| 1297 | { |
| 1298 | long i = m_caretPosition; |
| 1299 | |
| 1300 | // First skip white space |
| 1301 | while (i < endPos && i > -1) |
| 1302 | { |
| 1303 | // i is in character, not caret positions |
| 1304 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); |
| 1305 | if (text.empty()) // End of paragraph, or maybe an image |
| 1306 | break; |
| 1307 | else if (text == wxT(" ") || text.empty()) |
| 1308 | i += direction; |
| 1309 | else |
| 1310 | break; |
| 1311 | } |
| 1312 | // Next skip current text to space |
| 1313 | while (i < endPos && i > -1) |
| 1314 | { |
| 1315 | // i is in character, not caret positions |
| 1316 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); |
| 1317 | if (text != wxT(" ") /* && !text.empty() */) |
| 1318 | i += direction; |
| 1319 | else |
| 1320 | { |
| 1321 | return i; |
| 1322 | } |
| 1323 | } |
| 1324 | if (i < -1) |
| 1325 | return -1; |
| 1326 | return i; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | /// Move n words left |
| 1331 | bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags) |
| 1332 | { |
| 1333 | long pos = FindNextWordPosition(-1); |
| 1334 | if (pos != m_caretPosition) |
| 1335 | { |
| 1336 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); |
| 1337 | |
| 1338 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
| 1339 | if (!extendSel) |
| 1340 | SelectNone(); |
| 1341 | |
| 1342 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); |
| 1343 | PositionCaret(); |
| 1344 | SetDefaultStyleToCursorStyle(); |
| 1345 | |
| 1346 | if (extendSel) |
| 1347 | Refresh(false); |
| 1348 | return true; |
| 1349 | } |
| 1350 | |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
| 1354 | /// Move n words right |
| 1355 | bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags) |
| 1356 | { |
| 1357 | long pos = FindNextWordPosition(1); |
| 1358 | if (pos != m_caretPosition) |
| 1359 | { |
| 1360 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); |
| 1361 | |
| 1362 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
| 1363 | if (!extendSel) |
| 1364 | SelectNone(); |
| 1365 | |
| 1366 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); |
| 1367 | PositionCaret(); |
| 1368 | SetDefaultStyleToCursorStyle(); |
| 1369 | |
| 1370 | if (extendSel) |
| 1371 | Refresh(false); |
| 1372 | return true; |
| 1373 | } |
| 1374 | |
| 1375 | return false; |
| 1376 | } |
| 1377 | |
| 1378 | /// Sizing |
| 1379 | void wxRichTextCtrl::OnSize(wxSizeEvent& event) |
| 1380 | { |
| 1381 | // Only do sizing optimization for large buffers |
| 1382 | if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold) |
| 1383 | { |
| 1384 | m_fullLayoutRequired = true; |
| 1385 | m_fullLayoutTime = wxGetLocalTimeMillis(); |
| 1386 | m_fullLayoutSavedPosition = GetFirstVisiblePosition(); |
| 1387 | LayoutContent(true /* onlyVisibleRect */); |
| 1388 | } |
| 1389 | else |
| 1390 | GetBuffer().Invalidate(wxRICHTEXT_ALL); |
| 1391 | |
| 1392 | RecreateBuffer(); |
| 1393 | |
| 1394 | event.Skip(); |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | /// Idle-time processing |
| 1399 | void wxRichTextCtrl::OnIdle(wxIdleEvent& event) |
| 1400 | { |
| 1401 | const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL; |
| 1402 | |
| 1403 | if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval))) |
| 1404 | { |
| 1405 | m_fullLayoutRequired = false; |
| 1406 | m_fullLayoutTime = 0; |
| 1407 | GetBuffer().Invalidate(wxRICHTEXT_ALL); |
| 1408 | ShowPosition(m_fullLayoutSavedPosition); |
| 1409 | Refresh(false); |
| 1410 | } |
| 1411 | event.Skip(); |
| 1412 | } |
| 1413 | |
| 1414 | /// Scrolling |
| 1415 | void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event) |
| 1416 | { |
| 1417 | // Not used |
| 1418 | event.Skip(); |
| 1419 | } |
| 1420 | |
| 1421 | /// Set up scrollbars, e.g. after a resize |
| 1422 | void wxRichTextCtrl::SetupScrollbars(bool atTop) |
| 1423 | { |
| 1424 | if (m_freezeCount) |
| 1425 | return; |
| 1426 | |
| 1427 | if (GetBuffer().IsEmpty()) |
| 1428 | { |
| 1429 | SetScrollbars(0, 0, 0, 0, 0, 0); |
| 1430 | return; |
| 1431 | } |
| 1432 | |
| 1433 | // TODO: reimplement scrolling so we scroll by line, not by fixed number |
| 1434 | // of pixels. See e.g. wxVScrolledWindow for ideas. |
| 1435 | int pixelsPerUnit = 5; // 10; |
| 1436 | wxSize clientSize = GetClientSize(); |
| 1437 | |
| 1438 | int maxHeight = GetBuffer().GetCachedSize().y; |
| 1439 | |
| 1440 | int unitsY = maxHeight/pixelsPerUnit; |
| 1441 | |
| 1442 | int startX = 0, startY = 0; |
| 1443 | if (!atTop) |
| 1444 | GetViewStart(& startX, & startY); |
| 1445 | |
| 1446 | int maxPositionX = 0; // wxMax(sz.x - clientSize.x, 0); |
| 1447 | int maxPositionY = (wxMax(maxHeight - clientSize.y, 0))/pixelsPerUnit; |
| 1448 | |
| 1449 | // Move to previous scroll position if |
| 1450 | // possible |
| 1451 | SetScrollbars(0, pixelsPerUnit, |
| 1452 | 0, unitsY, |
| 1453 | wxMin(maxPositionX, startX), wxMin(maxPositionY, startY)); |
| 1454 | } |
| 1455 | |
| 1456 | /// Paint the background |
| 1457 | void wxRichTextCtrl::PaintBackground(wxDC& dc) |
| 1458 | { |
| 1459 | wxColour backgroundColour = GetBackgroundColour(); |
| 1460 | if (!backgroundColour.Ok()) |
| 1461 | backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
| 1462 | |
| 1463 | // Clear the background |
| 1464 | dc.SetBrush(wxBrush(backgroundColour)); |
| 1465 | dc.SetPen(*wxTRANSPARENT_PEN); |
| 1466 | wxRect windowRect(GetClientSize()); |
| 1467 | windowRect.x -= 2; windowRect.y -= 2; |
| 1468 | windowRect.width += 4; windowRect.height += 4; |
| 1469 | |
| 1470 | // We need to shift the rectangle to take into account |
| 1471 | // scrolling. Converting device to logical coordinates. |
| 1472 | CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y); |
| 1473 | dc.DrawRectangle(windowRect); |
| 1474 | } |
| 1475 | |
| 1476 | /// Recreate buffer bitmap if necessary |
| 1477 | bool wxRichTextCtrl::RecreateBuffer(const wxSize& size) |
| 1478 | { |
| 1479 | wxSize sz = size; |
| 1480 | if (sz == wxDefaultSize) |
| 1481 | sz = GetClientSize(); |
| 1482 | |
| 1483 | if (sz.x < 1 || sz.y < 1) |
| 1484 | return false; |
| 1485 | |
| 1486 | if (!m_bufferBitmap.Ok() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y) |
| 1487 | m_bufferBitmap = wxBitmap(sz.x, sz.y); |
| 1488 | return m_bufferBitmap.Ok(); |
| 1489 | } |
| 1490 | |
| 1491 | // ---------------------------------------------------------------------------- |
| 1492 | // file IO functions |
| 1493 | // ---------------------------------------------------------------------------- |
| 1494 | |
| 1495 | bool wxRichTextCtrl::LoadFile(const wxString& filename, int type) |
| 1496 | { |
| 1497 | bool success = GetBuffer().LoadFile(filename, type); |
| 1498 | if (success) |
| 1499 | m_filename = filename; |
| 1500 | |
| 1501 | DiscardEdits(); |
| 1502 | SetInsertionPoint(0); |
| 1503 | LayoutContent(); |
| 1504 | PositionCaret(); |
| 1505 | SetupScrollbars(true); |
| 1506 | Refresh(false); |
| 1507 | SendUpdateEvent(); |
| 1508 | |
| 1509 | if (success) |
| 1510 | return true; |
| 1511 | else |
| 1512 | { |
| 1513 | wxLogError(_("File couldn't be loaded.")); |
| 1514 | |
| 1515 | return false; |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | bool wxRichTextCtrl::SaveFile(const wxString& filename, int type) |
| 1520 | { |
| 1521 | wxString filenameToUse = filename.empty() ? m_filename : filename; |
| 1522 | if ( filenameToUse.empty() ) |
| 1523 | { |
| 1524 | // what kind of message to give? is it an error or a program bug? |
| 1525 | wxLogDebug(wxT("Can't save textctrl to file without filename.")); |
| 1526 | |
| 1527 | return false; |
| 1528 | } |
| 1529 | |
| 1530 | if (GetBuffer().SaveFile(filenameToUse, type)) |
| 1531 | { |
| 1532 | m_filename = filenameToUse; |
| 1533 | |
| 1534 | DiscardEdits(); |
| 1535 | |
| 1536 | return true; |
| 1537 | |
| 1538 | } |
| 1539 | |
| 1540 | wxLogError(_("The text couldn't be saved.")); |
| 1541 | |
| 1542 | return false; |
| 1543 | } |
| 1544 | |
| 1545 | // ---------------------------------------------------------------------------- |
| 1546 | // wxRichTextCtrl specific functionality |
| 1547 | // ---------------------------------------------------------------------------- |
| 1548 | |
| 1549 | /// Add a new paragraph of text to the end of the buffer |
| 1550 | wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text) |
| 1551 | { |
| 1552 | return GetBuffer().AddParagraph(text); |
| 1553 | } |
| 1554 | |
| 1555 | /// Add an image |
| 1556 | wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image) |
| 1557 | { |
| 1558 | return GetBuffer().AddImage(image); |
| 1559 | } |
| 1560 | |
| 1561 | // ---------------------------------------------------------------------------- |
| 1562 | // selection and ranges |
| 1563 | // ---------------------------------------------------------------------------- |
| 1564 | |
| 1565 | void wxRichTextCtrl::SelectAll() |
| 1566 | { |
| 1567 | SetSelection(0, GetLastPosition()); |
| 1568 | m_selectionAnchor = -1; |
| 1569 | } |
| 1570 | |
| 1571 | /// Select none |
| 1572 | void wxRichTextCtrl::SelectNone() |
| 1573 | { |
| 1574 | if (!(GetSelectionRange() == wxRichTextRange(-2, -2))) |
| 1575 | SetSelection(-2, -2); |
| 1576 | m_selectionAnchor = -2; |
| 1577 | } |
| 1578 | |
| 1579 | wxString wxRichTextCtrl::GetStringSelection() const |
| 1580 | { |
| 1581 | long from, to; |
| 1582 | GetSelection(&from, &to); |
| 1583 | |
| 1584 | return GetRange(from, to); |
| 1585 | } |
| 1586 | |
| 1587 | // do the window-specific processing after processing the update event |
| 1588 | #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
| 1589 | void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event) |
| 1590 | { |
| 1591 | // call inherited |
| 1592 | wxWindowBase::DoUpdateWindowUI(event); |
| 1593 | |
| 1594 | // update text |
| 1595 | if ( event.GetSetText() ) |
| 1596 | { |
| 1597 | if ( event.GetText() != GetValue() ) |
| 1598 | SetValue(event.GetText()); |
| 1599 | } |
| 1600 | } |
| 1601 | #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
| 1602 | |
| 1603 | // ---------------------------------------------------------------------------- |
| 1604 | // hit testing |
| 1605 | // ---------------------------------------------------------------------------- |
| 1606 | |
| 1607 | wxTextCtrlHitTestResult |
| 1608 | wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const |
| 1609 | { |
| 1610 | // implement in terms of the other overload as the native ports typically |
| 1611 | // can get the position and not (x, y) pair directly (although wxUniv |
| 1612 | // directly gets x and y -- and so overrides this method as well) |
| 1613 | long pos; |
| 1614 | wxTextCtrlHitTestResult rc = HitTest(pt, &pos); |
| 1615 | |
| 1616 | if ( rc != wxTE_HT_UNKNOWN ) |
| 1617 | { |
| 1618 | PositionToXY(pos, x, y); |
| 1619 | } |
| 1620 | |
| 1621 | return rc; |
| 1622 | } |
| 1623 | |
| 1624 | wxTextCtrlHitTestResult |
| 1625 | wxRichTextCtrl::HitTest(const wxPoint& pt, |
| 1626 | long * pos) const |
| 1627 | { |
| 1628 | wxClientDC dc((wxRichTextCtrl*) this); |
| 1629 | ((wxRichTextCtrl*)this)->PrepareDC(dc); |
| 1630 | |
| 1631 | int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt, *pos); |
| 1632 | |
| 1633 | switch ( hit ) |
| 1634 | { |
| 1635 | case wxRICHTEXT_HITTEST_BEFORE: |
| 1636 | return wxTE_HT_BEFORE; |
| 1637 | |
| 1638 | case wxRICHTEXT_HITTEST_AFTER: |
| 1639 | return wxTE_HT_BEYOND; |
| 1640 | |
| 1641 | case wxRICHTEXT_HITTEST_ON: |
| 1642 | return wxTE_HT_ON_TEXT; |
| 1643 | } |
| 1644 | |
| 1645 | return wxTE_HT_UNKNOWN; |
| 1646 | } |
| 1647 | |
| 1648 | // ---------------------------------------------------------------------------- |
| 1649 | // set/get the controls text |
| 1650 | // ---------------------------------------------------------------------------- |
| 1651 | |
| 1652 | wxString wxRichTextCtrl::GetValue() const |
| 1653 | { |
| 1654 | return GetBuffer().GetText(); |
| 1655 | } |
| 1656 | |
| 1657 | wxString wxRichTextCtrl::GetRange(long from, long to) const |
| 1658 | { |
| 1659 | return GetBuffer().GetTextForRange(wxRichTextRange(from, to)); |
| 1660 | } |
| 1661 | |
| 1662 | void wxRichTextCtrl::SetValue(const wxString& value) |
| 1663 | { |
| 1664 | Clear(); |
| 1665 | |
| 1666 | // if the text is long enough, it's faster to just set it instead of first |
| 1667 | // comparing it with the old one (chances are that it will be different |
| 1668 | // anyhow, this comparison is there to avoid flicker for small single-line |
| 1669 | // edit controls mostly) |
| 1670 | if ( (value.length() > 0x400) || (value != GetValue()) ) |
| 1671 | { |
| 1672 | DoWriteText(value, false /* not selection only */); |
| 1673 | |
| 1674 | // for compatibility, don't move the cursor when doing SetValue() |
| 1675 | SetInsertionPoint(0); |
| 1676 | } |
| 1677 | else // same text |
| 1678 | { |
| 1679 | // still send an event for consistency |
| 1680 | SendUpdateEvent(); |
| 1681 | } |
| 1682 | |
| 1683 | // we should reset the modified flag even if the value didn't really change |
| 1684 | |
| 1685 | // mark the control as being not dirty - we changed its text, not the |
| 1686 | // user |
| 1687 | DiscardEdits(); |
| 1688 | } |
| 1689 | |
| 1690 | void wxRichTextCtrl::WriteText(const wxString& value) |
| 1691 | { |
| 1692 | DoWriteText(value); |
| 1693 | } |
| 1694 | |
| 1695 | void wxRichTextCtrl::DoWriteText(const wxString& value, bool WXUNUSED(selectionOnly)) |
| 1696 | { |
| 1697 | GetBuffer().InsertTextWithUndo(m_caretPosition+1, value, this); |
| 1698 | } |
| 1699 | |
| 1700 | void wxRichTextCtrl::AppendText(const wxString& text) |
| 1701 | { |
| 1702 | SetInsertionPointEnd(); |
| 1703 | |
| 1704 | WriteText(text); |
| 1705 | } |
| 1706 | |
| 1707 | /// Write an image at the current insertion point |
| 1708 | bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType) |
| 1709 | { |
| 1710 | wxRichTextImageBlock imageBlock; |
| 1711 | |
| 1712 | wxImage image2 = image; |
| 1713 | if (imageBlock.MakeImageBlock(image2, bitmapType)) |
| 1714 | return WriteImage(imageBlock); |
| 1715 | |
| 1716 | return false; |
| 1717 | } |
| 1718 | |
| 1719 | bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType) |
| 1720 | { |
| 1721 | wxRichTextImageBlock imageBlock; |
| 1722 | |
| 1723 | wxImage image; |
| 1724 | if (imageBlock.MakeImageBlock(filename, bitmapType, image, false)) |
| 1725 | return WriteImage(imageBlock); |
| 1726 | |
| 1727 | return false; |
| 1728 | } |
| 1729 | |
| 1730 | bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock) |
| 1731 | { |
| 1732 | return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this); |
| 1733 | } |
| 1734 | |
| 1735 | bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType) |
| 1736 | { |
| 1737 | if (bitmap.Ok()) |
| 1738 | { |
| 1739 | wxRichTextImageBlock imageBlock; |
| 1740 | |
| 1741 | wxImage image = bitmap.ConvertToImage(); |
| 1742 | if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType)) |
| 1743 | return WriteImage(imageBlock); |
| 1744 | } |
| 1745 | |
| 1746 | return false; |
| 1747 | } |
| 1748 | |
| 1749 | /// Insert a newline (actually paragraph) at the current insertion point. |
| 1750 | bool wxRichTextCtrl::Newline() |
| 1751 | { |
| 1752 | return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this); |
| 1753 | } |
| 1754 | |
| 1755 | |
| 1756 | // ---------------------------------------------------------------------------- |
| 1757 | // Clipboard operations |
| 1758 | // ---------------------------------------------------------------------------- |
| 1759 | |
| 1760 | void wxRichTextCtrl::Copy() |
| 1761 | { |
| 1762 | if (CanCopy()) |
| 1763 | { |
| 1764 | wxRichTextRange range = GetSelectionRange(); |
| 1765 | GetBuffer().CopyToClipboard(range); |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | void wxRichTextCtrl::Cut() |
| 1770 | { |
| 1771 | if (CanCut()) |
| 1772 | { |
| 1773 | wxRichTextRange range = GetSelectionRange(); |
| 1774 | GetBuffer().CopyToClipboard(range); |
| 1775 | |
| 1776 | DeleteSelectedContent(); |
| 1777 | LayoutContent(); |
| 1778 | Refresh(false); |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | void wxRichTextCtrl::Paste() |
| 1783 | { |
| 1784 | if (CanPaste()) |
| 1785 | { |
| 1786 | BeginBatchUndo(_("Paste")); |
| 1787 | |
| 1788 | long newPos = m_caretPosition; |
| 1789 | DeleteSelectedContent(& newPos); |
| 1790 | |
| 1791 | GetBuffer().PasteFromClipboard(newPos); |
| 1792 | |
| 1793 | EndBatchUndo(); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | void wxRichTextCtrl::DeleteSelection() |
| 1798 | { |
| 1799 | if (CanDeleteSelection()) |
| 1800 | { |
| 1801 | DeleteSelectedContent(); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | bool wxRichTextCtrl::HasSelection() const |
| 1806 | { |
| 1807 | return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2; |
| 1808 | } |
| 1809 | |
| 1810 | bool wxRichTextCtrl::CanCopy() const |
| 1811 | { |
| 1812 | // Can copy if there's a selection |
| 1813 | return HasSelection(); |
| 1814 | } |
| 1815 | |
| 1816 | bool wxRichTextCtrl::CanCut() const |
| 1817 | { |
| 1818 | return HasSelection() && IsEditable(); |
| 1819 | } |
| 1820 | |
| 1821 | bool wxRichTextCtrl::CanPaste() const |
| 1822 | { |
| 1823 | if ( !IsEditable() ) |
| 1824 | return false; |
| 1825 | |
| 1826 | return GetBuffer().CanPasteFromClipboard(); |
| 1827 | } |
| 1828 | |
| 1829 | bool wxRichTextCtrl::CanDeleteSelection() const |
| 1830 | { |
| 1831 | return HasSelection() && IsEditable(); |
| 1832 | } |
| 1833 | |
| 1834 | |
| 1835 | // ---------------------------------------------------------------------------- |
| 1836 | // Accessors |
| 1837 | // ---------------------------------------------------------------------------- |
| 1838 | |
| 1839 | void wxRichTextCtrl::SetEditable(bool editable) |
| 1840 | { |
| 1841 | m_editable = editable; |
| 1842 | } |
| 1843 | |
| 1844 | void wxRichTextCtrl::SetInsertionPoint(long pos) |
| 1845 | { |
| 1846 | SelectNone(); |
| 1847 | |
| 1848 | m_caretPosition = pos - 1; |
| 1849 | } |
| 1850 | |
| 1851 | void wxRichTextCtrl::SetInsertionPointEnd() |
| 1852 | { |
| 1853 | long pos = GetLastPosition(); |
| 1854 | SetInsertionPoint(pos); |
| 1855 | } |
| 1856 | |
| 1857 | long wxRichTextCtrl::GetInsertionPoint() const |
| 1858 | { |
| 1859 | return m_caretPosition+1; |
| 1860 | } |
| 1861 | |
| 1862 | wxTextPos wxRichTextCtrl::GetLastPosition() const |
| 1863 | { |
| 1864 | return GetBuffer().GetRange().GetEnd(); |
| 1865 | } |
| 1866 | |
| 1867 | // If the return values from and to are the same, there is no |
| 1868 | // selection. |
| 1869 | void wxRichTextCtrl::GetSelection(long* from, long* to) const |
| 1870 | { |
| 1871 | *from = m_selectionRange.GetStart(); |
| 1872 | *to = m_selectionRange.GetEnd(); |
| 1873 | } |
| 1874 | |
| 1875 | bool wxRichTextCtrl::IsEditable() const |
| 1876 | { |
| 1877 | return m_editable; |
| 1878 | } |
| 1879 | |
| 1880 | // ---------------------------------------------------------------------------- |
| 1881 | // selection |
| 1882 | // ---------------------------------------------------------------------------- |
| 1883 | |
| 1884 | void wxRichTextCtrl::SetSelection(long from, long to) |
| 1885 | { |
| 1886 | // if from and to are both -1, it means (in wxWidgets) that all text should |
| 1887 | // be selected. |
| 1888 | if ( (from == -1) && (to == -1) ) |
| 1889 | { |
| 1890 | from = 0; |
| 1891 | to = GetLastPosition(); |
| 1892 | } |
| 1893 | |
| 1894 | DoSetSelection(from, to); |
| 1895 | } |
| 1896 | |
| 1897 | void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret)) |
| 1898 | { |
| 1899 | m_selectionAnchor = from; |
| 1900 | m_selectionRange.SetRange(from, to); |
| 1901 | Refresh(false); |
| 1902 | PositionCaret(); |
| 1903 | } |
| 1904 | |
| 1905 | // ---------------------------------------------------------------------------- |
| 1906 | // Editing |
| 1907 | // ---------------------------------------------------------------------------- |
| 1908 | |
| 1909 | void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to), const wxString& value) |
| 1910 | { |
| 1911 | BeginBatchUndo(_("Replace")); |
| 1912 | |
| 1913 | DeleteSelectedContent(); |
| 1914 | |
| 1915 | DoWriteText(value, true /* selection only */); |
| 1916 | |
| 1917 | EndBatchUndo(); |
| 1918 | } |
| 1919 | |
| 1920 | void wxRichTextCtrl::Remove(long from, long to) |
| 1921 | { |
| 1922 | SelectNone(); |
| 1923 | |
| 1924 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to), |
| 1925 | m_caretPosition, // Current caret position |
| 1926 | from, // New caret position |
| 1927 | this); |
| 1928 | |
| 1929 | LayoutContent(); |
| 1930 | if (!IsFrozen()) |
| 1931 | Refresh(false); |
| 1932 | } |
| 1933 | |
| 1934 | bool wxRichTextCtrl::IsModified() const |
| 1935 | { |
| 1936 | return m_buffer.IsModified(); |
| 1937 | } |
| 1938 | |
| 1939 | void wxRichTextCtrl::MarkDirty() |
| 1940 | { |
| 1941 | m_buffer.Modify(true); |
| 1942 | } |
| 1943 | |
| 1944 | void wxRichTextCtrl::DiscardEdits() |
| 1945 | { |
| 1946 | m_buffer.Modify(false); |
| 1947 | m_buffer.GetCommandProcessor()->ClearCommands(); |
| 1948 | } |
| 1949 | |
| 1950 | int wxRichTextCtrl::GetNumberOfLines() const |
| 1951 | { |
| 1952 | return GetBuffer().GetParagraphCount(); |
| 1953 | } |
| 1954 | |
| 1955 | // ---------------------------------------------------------------------------- |
| 1956 | // Positions <-> coords |
| 1957 | // ---------------------------------------------------------------------------- |
| 1958 | |
| 1959 | long wxRichTextCtrl::XYToPosition(long x, long y) const |
| 1960 | { |
| 1961 | return GetBuffer().XYToPosition(x, y); |
| 1962 | } |
| 1963 | |
| 1964 | bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const |
| 1965 | { |
| 1966 | return GetBuffer().PositionToXY(pos, x, y); |
| 1967 | } |
| 1968 | |
| 1969 | // ---------------------------------------------------------------------------- |
| 1970 | // |
| 1971 | // ---------------------------------------------------------------------------- |
| 1972 | |
| 1973 | void wxRichTextCtrl::ShowPosition(long pos) |
| 1974 | { |
| 1975 | if (!IsPositionVisible(pos)) |
| 1976 | ScrollIntoView(pos-1, WXK_DOWN); |
| 1977 | } |
| 1978 | |
| 1979 | int wxRichTextCtrl::GetLineLength(long lineNo) const |
| 1980 | { |
| 1981 | return GetBuffer().GetParagraphLength(lineNo); |
| 1982 | } |
| 1983 | |
| 1984 | wxString wxRichTextCtrl::GetLineText(long lineNo) const |
| 1985 | { |
| 1986 | return GetBuffer().GetParagraphText(lineNo); |
| 1987 | } |
| 1988 | |
| 1989 | // ---------------------------------------------------------------------------- |
| 1990 | // Undo/redo |
| 1991 | // ---------------------------------------------------------------------------- |
| 1992 | |
| 1993 | void wxRichTextCtrl::Undo() |
| 1994 | { |
| 1995 | if (CanUndo()) |
| 1996 | { |
| 1997 | GetCommandProcessor()->Undo(); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | void wxRichTextCtrl::Redo() |
| 2002 | { |
| 2003 | if (CanRedo()) |
| 2004 | { |
| 2005 | GetCommandProcessor()->Redo(); |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | bool wxRichTextCtrl::CanUndo() const |
| 2010 | { |
| 2011 | return GetCommandProcessor()->CanUndo(); |
| 2012 | } |
| 2013 | |
| 2014 | bool wxRichTextCtrl::CanRedo() const |
| 2015 | { |
| 2016 | return GetCommandProcessor()->CanRedo(); |
| 2017 | } |
| 2018 | |
| 2019 | // ---------------------------------------------------------------------------- |
| 2020 | // implemenation details |
| 2021 | // ---------------------------------------------------------------------------- |
| 2022 | |
| 2023 | void wxRichTextCtrl::Command(wxCommandEvent & event) |
| 2024 | { |
| 2025 | SetValue(event.GetString()); |
| 2026 | GetEventHandler()->ProcessEvent(event); |
| 2027 | } |
| 2028 | |
| 2029 | void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event) |
| 2030 | { |
| 2031 | // By default, load the first file into the text window. |
| 2032 | if (event.GetNumberOfFiles() > 0) |
| 2033 | { |
| 2034 | LoadFile(event.GetFiles()[0]); |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | // ---------------------------------------------------------------------------- |
| 2039 | // text control event processing |
| 2040 | // ---------------------------------------------------------------------------- |
| 2041 | |
| 2042 | bool wxRichTextCtrl::SendUpdateEvent() |
| 2043 | { |
| 2044 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); |
| 2045 | InitCommandEvent(event); |
| 2046 | |
| 2047 | return GetEventHandler()->ProcessEvent(event); |
| 2048 | } |
| 2049 | |
| 2050 | void wxRichTextCtrl::InitCommandEvent(wxCommandEvent& event) const |
| 2051 | { |
| 2052 | event.SetEventObject((wxControlBase *)this); // const_cast |
| 2053 | |
| 2054 | switch ( m_clientDataType ) |
| 2055 | { |
| 2056 | case wxClientData_Void: |
| 2057 | event.SetClientData(GetClientData()); |
| 2058 | break; |
| 2059 | |
| 2060 | case wxClientData_Object: |
| 2061 | event.SetClientObject(GetClientObject()); |
| 2062 | break; |
| 2063 | |
| 2064 | case wxClientData_None: |
| 2065 | // nothing to do |
| 2066 | ; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | wxSize wxRichTextCtrl::DoGetBestSize() const |
| 2072 | { |
| 2073 | return wxSize(10, 10); |
| 2074 | } |
| 2075 | |
| 2076 | // ---------------------------------------------------------------------------- |
| 2077 | // standard handlers for standard edit menu events |
| 2078 | // ---------------------------------------------------------------------------- |
| 2079 | |
| 2080 | void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
| 2081 | { |
| 2082 | Cut(); |
| 2083 | } |
| 2084 | |
| 2085 | void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event)) |
| 2086 | { |
| 2087 | DeleteSelection(); |
| 2088 | } |
| 2089 | |
| 2090 | void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) |
| 2091 | { |
| 2092 | Copy(); |
| 2093 | } |
| 2094 | |
| 2095 | void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) |
| 2096 | { |
| 2097 | Paste(); |
| 2098 | } |
| 2099 | |
| 2100 | void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) |
| 2101 | { |
| 2102 | Undo(); |
| 2103 | } |
| 2104 | |
| 2105 | void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) |
| 2106 | { |
| 2107 | Redo(); |
| 2108 | } |
| 2109 | |
| 2110 | void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) |
| 2111 | { |
| 2112 | event.Enable( CanCut() ); |
| 2113 | } |
| 2114 | |
| 2115 | void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) |
| 2116 | { |
| 2117 | event.Enable( CanCopy() ); |
| 2118 | } |
| 2119 | |
| 2120 | void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event) |
| 2121 | { |
| 2122 | event.Enable( CanDeleteSelection() ); |
| 2123 | } |
| 2124 | |
| 2125 | void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) |
| 2126 | { |
| 2127 | event.Enable( CanPaste() ); |
| 2128 | } |
| 2129 | |
| 2130 | void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) |
| 2131 | { |
| 2132 | event.Enable( CanUndo() ); |
| 2133 | event.SetText( GetCommandProcessor()->GetUndoMenuLabel() ); |
| 2134 | } |
| 2135 | |
| 2136 | void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) |
| 2137 | { |
| 2138 | event.Enable( CanRedo() ); |
| 2139 | event.SetText( GetCommandProcessor()->GetRedoMenuLabel() ); |
| 2140 | } |
| 2141 | |
| 2142 | void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) |
| 2143 | { |
| 2144 | SelectAll(); |
| 2145 | } |
| 2146 | |
| 2147 | void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event) |
| 2148 | { |
| 2149 | event.Enable(GetLastPosition() > 0); |
| 2150 | } |
| 2151 | |
| 2152 | void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& WXUNUSED(event)) |
| 2153 | { |
| 2154 | if (!m_contextMenu) |
| 2155 | { |
| 2156 | m_contextMenu = new wxMenu; |
| 2157 | m_contextMenu->Append(wxID_UNDO, _("&Undo")); |
| 2158 | m_contextMenu->Append(wxID_REDO, _("&Redo")); |
| 2159 | m_contextMenu->AppendSeparator(); |
| 2160 | m_contextMenu->Append(wxID_CUT, _("Cu&t")); |
| 2161 | m_contextMenu->Append(wxID_COPY, _("&Copy")); |
| 2162 | m_contextMenu->Append(wxID_PASTE, _("&Paste")); |
| 2163 | m_contextMenu->Append(wxID_CLEAR, _("&Delete")); |
| 2164 | m_contextMenu->AppendSeparator(); |
| 2165 | m_contextMenu->Append(wxID_SELECTALL, _("Select &All")); |
| 2166 | } |
| 2167 | PopupMenu(m_contextMenu); |
| 2168 | return; |
| 2169 | } |
| 2170 | |
| 2171 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style) |
| 2172 | { |
| 2173 | return GetBuffer().SetStyle(wxRichTextRange(start, end), style); |
| 2174 | } |
| 2175 | |
| 2176 | bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style) |
| 2177 | { |
| 2178 | return GetBuffer().SetStyle(range, style); |
| 2179 | } |
| 2180 | |
| 2181 | bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style) |
| 2182 | { |
| 2183 | return GetBuffer().SetDefaultStyle(style); |
| 2184 | } |
| 2185 | |
| 2186 | const wxTextAttrEx& wxRichTextCtrl::GetDefaultStyleEx() const |
| 2187 | { |
| 2188 | return GetBuffer().GetDefaultStyle(); |
| 2189 | } |
| 2190 | |
| 2191 | bool wxRichTextCtrl::GetStyle(long position, wxTextAttrEx& style) const |
| 2192 | { |
| 2193 | return GetBuffer().GetStyle(position, style); |
| 2194 | } |
| 2195 | |
| 2196 | bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style) const |
| 2197 | { |
| 2198 | return GetBuffer().GetStyle(position, style); |
| 2199 | } |
| 2200 | |
| 2201 | /// Set font, and also the buffer attributes |
| 2202 | bool wxRichTextCtrl::SetFont(const wxFont& font) |
| 2203 | { |
| 2204 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
| 2205 | wxControl::SetFont(font); |
| 2206 | #else |
| 2207 | wxScrolledWindow::SetFont(font); |
| 2208 | #endif |
| 2209 | |
| 2210 | wxTextAttrEx attr = GetBuffer().GetAttributes(); |
| 2211 | attr.SetFont(font); |
| 2212 | GetBuffer().SetBasicStyle(attr); |
| 2213 | GetBuffer().SetDefaultStyle(attr); |
| 2214 | |
| 2215 | return true; |
| 2216 | } |
| 2217 | |
| 2218 | /// Transform logical to physical |
| 2219 | wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const |
| 2220 | { |
| 2221 | wxPoint pt; |
| 2222 | CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y); |
| 2223 | |
| 2224 | return pt; |
| 2225 | } |
| 2226 | |
| 2227 | /// Transform physical to logical |
| 2228 | wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const |
| 2229 | { |
| 2230 | wxPoint pt; |
| 2231 | CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y); |
| 2232 | |
| 2233 | return pt; |
| 2234 | } |
| 2235 | |
| 2236 | /// Position the caret |
| 2237 | void wxRichTextCtrl::PositionCaret() |
| 2238 | { |
| 2239 | if (!GetCaret()) |
| 2240 | return; |
| 2241 | |
| 2242 | //wxLogDebug(wxT("PositionCaret")); |
| 2243 | |
| 2244 | wxRect caretRect; |
| 2245 | if (GetCaretPositionForIndex(GetCaretPosition(), caretRect)) |
| 2246 | { |
| 2247 | wxPoint originalPt = caretRect.GetPosition(); |
| 2248 | wxPoint pt = GetPhysicalPoint(originalPt); |
| 2249 | if (GetCaret()->GetPosition() != pt) |
| 2250 | { |
| 2251 | GetCaret()->Move(pt); |
| 2252 | GetCaret()->SetSize(caretRect.GetSize()); |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | /// Get the caret height and position for the given character position |
| 2258 | bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect) |
| 2259 | { |
| 2260 | wxClientDC dc(this); |
| 2261 | dc.SetFont(GetFont()); |
| 2262 | |
| 2263 | PrepareDC(dc); |
| 2264 | |
| 2265 | wxPoint pt; |
| 2266 | int height = 0; |
| 2267 | |
| 2268 | if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart)) |
| 2269 | { |
| 2270 | rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height)); |
| 2271 | return true; |
| 2272 | } |
| 2273 | |
| 2274 | return false; |
| 2275 | } |
| 2276 | |
| 2277 | /// Gets the line for the visible caret position. If the caret is |
| 2278 | /// shown at the very end of the line, it means the next character is actually |
| 2279 | /// on the following line. So let's get the line we're expecting to find |
| 2280 | /// if this is the case. |
| 2281 | wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const |
| 2282 | { |
| 2283 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true); |
| 2284 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true); |
| 2285 | if (line) |
| 2286 | { |
| 2287 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
| 2288 | if (caretPosition == lineRange.GetStart()-1 && |
| 2289 | (para->GetRange().GetStart() != lineRange.GetStart())) |
| 2290 | { |
| 2291 | if (!m_caretAtLineStart) |
| 2292 | line = GetBuffer().GetLineAtPosition(caretPosition-1, true); |
| 2293 | } |
| 2294 | } |
| 2295 | return line; |
| 2296 | } |
| 2297 | |
| 2298 | |
| 2299 | /// Move the caret to the given character position |
| 2300 | bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart) |
| 2301 | { |
| 2302 | if (GetBuffer().GetDirty()) |
| 2303 | LayoutContent(); |
| 2304 | |
| 2305 | if (pos <= GetBuffer().GetRange().GetEnd()) |
| 2306 | { |
| 2307 | SetCaretPosition(pos, showAtLineStart); |
| 2308 | |
| 2309 | PositionCaret(); |
| 2310 | |
| 2311 | return true; |
| 2312 | } |
| 2313 | else |
| 2314 | return false; |
| 2315 | } |
| 2316 | |
| 2317 | /// Layout the buffer: which we must do before certain operations, such as |
| 2318 | /// setting the caret position. |
| 2319 | bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect) |
| 2320 | { |
| 2321 | if (GetBuffer().GetDirty() || onlyVisibleRect) |
| 2322 | { |
| 2323 | wxRect availableSpace(GetClientSize()); |
| 2324 | if (availableSpace.width == 0) |
| 2325 | availableSpace.width = 10; |
| 2326 | if (availableSpace.height == 0) |
| 2327 | availableSpace.height = 10; |
| 2328 | |
| 2329 | int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT; |
| 2330 | if (onlyVisibleRect) |
| 2331 | { |
| 2332 | flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT; |
| 2333 | availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0))); |
| 2334 | } |
| 2335 | |
| 2336 | wxClientDC dc(this); |
| 2337 | dc.SetFont(GetFont()); |
| 2338 | |
| 2339 | PrepareDC(dc); |
| 2340 | |
| 2341 | GetBuffer().Defragment(); |
| 2342 | GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation |
| 2343 | GetBuffer().Layout(dc, availableSpace, flags); |
| 2344 | GetBuffer().SetDirty(false); |
| 2345 | |
| 2346 | if (!IsFrozen()) |
| 2347 | SetupScrollbars(); |
| 2348 | } |
| 2349 | |
| 2350 | return true; |
| 2351 | } |
| 2352 | |
| 2353 | /// Is all of the selection bold? |
| 2354 | bool wxRichTextCtrl::IsSelectionBold() const |
| 2355 | { |
| 2356 | if (HasSelection()) |
| 2357 | { |
| 2358 | wxRichTextAttr attr; |
| 2359 | wxRichTextRange range = GetSelectionRange(); |
| 2360 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
| 2361 | attr.SetFontWeight(wxBOLD); |
| 2362 | |
| 2363 | return HasCharacterAttributes(range, attr); |
| 2364 | } |
| 2365 | else |
| 2366 | { |
| 2367 | // If no selection, then we need to combine current style with default style |
| 2368 | // to see what the effect would be if we started typing. |
| 2369 | wxRichTextAttr attr; |
| 2370 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
| 2371 | if (GetStyle(GetCaretPosition()+1, attr)) |
| 2372 | { |
| 2373 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); |
| 2374 | return attr.GetFontWeight() == wxBOLD; |
| 2375 | } |
| 2376 | } |
| 2377 | return false; |
| 2378 | } |
| 2379 | |
| 2380 | /// Is all of the selection italics? |
| 2381 | bool wxRichTextCtrl::IsSelectionItalics() const |
| 2382 | { |
| 2383 | if (HasSelection()) |
| 2384 | { |
| 2385 | wxRichTextRange range = GetSelectionRange(); |
| 2386 | wxRichTextAttr attr; |
| 2387 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); |
| 2388 | attr.SetFontStyle(wxITALIC); |
| 2389 | |
| 2390 | return HasCharacterAttributes(range, attr); |
| 2391 | } |
| 2392 | else |
| 2393 | { |
| 2394 | // If no selection, then we need to combine current style with default style |
| 2395 | // to see what the effect would be if we started typing. |
| 2396 | wxRichTextAttr attr; |
| 2397 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); |
| 2398 | if (GetStyle(GetCaretPosition()+1, attr)) |
| 2399 | { |
| 2400 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); |
| 2401 | return attr.GetFontStyle() == wxITALIC; |
| 2402 | } |
| 2403 | } |
| 2404 | return false; |
| 2405 | } |
| 2406 | |
| 2407 | /// Is all of the selection underlined? |
| 2408 | bool wxRichTextCtrl::IsSelectionUnderlined() const |
| 2409 | { |
| 2410 | if (HasSelection()) |
| 2411 | { |
| 2412 | wxRichTextRange range = GetSelectionRange(); |
| 2413 | wxRichTextAttr attr; |
| 2414 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); |
| 2415 | attr.SetFontUnderlined(true); |
| 2416 | |
| 2417 | return HasCharacterAttributes(range, attr); |
| 2418 | } |
| 2419 | else |
| 2420 | { |
| 2421 | // If no selection, then we need to combine current style with default style |
| 2422 | // to see what the effect would be if we started typing. |
| 2423 | wxRichTextAttr attr; |
| 2424 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); |
| 2425 | if (GetStyle(GetCaretPosition()+1, attr)) |
| 2426 | { |
| 2427 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); |
| 2428 | return attr.GetFontUnderlined(); |
| 2429 | } |
| 2430 | } |
| 2431 | return false; |
| 2432 | } |
| 2433 | |
| 2434 | /// Apply bold to the selection |
| 2435 | bool wxRichTextCtrl::ApplyBoldToSelection() |
| 2436 | { |
| 2437 | wxRichTextAttr attr; |
| 2438 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
| 2439 | attr.SetFontWeight(IsSelectionBold() ? wxNORMAL : wxBOLD); |
| 2440 | |
| 2441 | if (HasSelection()) |
| 2442 | return SetStyle(GetSelectionRange(), attr); |
| 2443 | else |
| 2444 | SetDefaultStyle(attr); |
| 2445 | return true; |
| 2446 | } |
| 2447 | |
| 2448 | /// Apply italic to the selection |
| 2449 | bool wxRichTextCtrl::ApplyItalicToSelection() |
| 2450 | { |
| 2451 | wxRichTextAttr attr; |
| 2452 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); |
| 2453 | attr.SetFontStyle(IsSelectionItalics() ? wxNORMAL : wxITALIC); |
| 2454 | |
| 2455 | if (HasSelection()) |
| 2456 | return SetStyle(GetSelectionRange(), attr); |
| 2457 | else |
| 2458 | SetDefaultStyle(attr); |
| 2459 | return true; |
| 2460 | } |
| 2461 | |
| 2462 | /// Apply underline to the selection |
| 2463 | bool wxRichTextCtrl::ApplyUnderlineToSelection() |
| 2464 | { |
| 2465 | wxRichTextAttr attr; |
| 2466 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); |
| 2467 | attr.SetFontUnderlined(!IsSelectionUnderlined()); |
| 2468 | |
| 2469 | if (HasSelection()) |
| 2470 | return SetStyle(GetSelectionRange(), attr); |
| 2471 | else |
| 2472 | SetDefaultStyle(attr); |
| 2473 | return true; |
| 2474 | } |
| 2475 | |
| 2476 | /// Is all of the selection aligned according to the specified flag? |
| 2477 | bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment) const |
| 2478 | { |
| 2479 | if (HasSelection()) |
| 2480 | { |
| 2481 | wxRichTextRange range = GetSelectionRange(); |
| 2482 | wxRichTextAttr attr; |
| 2483 | attr.SetAlignment(alignment); |
| 2484 | |
| 2485 | return HasParagraphAttributes(range, attr); |
| 2486 | } |
| 2487 | else |
| 2488 | { |
| 2489 | // If no selection, then we need to get information from the current paragraph. |
| 2490 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1); |
| 2491 | if (para) |
| 2492 | return para->GetAttributes().GetAlignment() == alignment; |
| 2493 | } |
| 2494 | return false; |
| 2495 | } |
| 2496 | |
| 2497 | /// Apply alignment to the selection |
| 2498 | bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment) |
| 2499 | { |
| 2500 | wxRichTextAttr attr; |
| 2501 | attr.SetAlignment(alignment); |
| 2502 | if (HasSelection()) |
| 2503 | return SetStyle(GetSelectionRange(), attr); |
| 2504 | else |
| 2505 | { |
| 2506 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1); |
| 2507 | if (para) |
| 2508 | return SetStyle(para->GetRange(), attr); |
| 2509 | } |
| 2510 | return true; |
| 2511 | } |
| 2512 | |
| 2513 | /// Sets the default style to the style under the cursor |
| 2514 | bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() |
| 2515 | { |
| 2516 | wxTextAttrEx attr; |
| 2517 | attr.SetFlags(wxTEXT_ATTR_CHARACTER); |
| 2518 | |
| 2519 | if (GetStyle(GetCaretPosition(), attr)) |
| 2520 | { |
| 2521 | SetDefaultStyle(attr); |
| 2522 | return true; |
| 2523 | } |
| 2524 | |
| 2525 | return false; |
| 2526 | } |
| 2527 | |
| 2528 | /// Returns the first visible position in the current view |
| 2529 | long wxRichTextCtrl::GetFirstVisiblePosition() const |
| 2530 | { |
| 2531 | wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y); |
| 2532 | if (line) |
| 2533 | return line->GetAbsoluteRange().GetStart(); |
| 2534 | else |
| 2535 | return 0; |
| 2536 | } |
| 2537 | |
| 2538 | #endif |
| 2539 | // wxUSE_RICHTEXT |