1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/richtext/richeditctrl.cpp 
   3 // Purpose:     A rich edit control 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx.h". 
  13 #include "wx/wxprec.h" 
  21 #include "wx/richtext/richtextctrl.h" 
  22 #include "wx/richtext/richtextstyles.h" 
  26     #include "wx/settings.h" 
  29 #include "wx/textfile.h" 
  31 #include "wx/filename.h" 
  32 #include "wx/dcbuffer.h" 
  33 #include "wx/arrimpl.cpp" 
  34 #include "wx/fontenum.h" 
  36 // DLL options compatibility check: 
  38 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl") 
  40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
) 
  41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
) 
  42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
) 
  43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
) 
  44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
) 
  45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER
) 
  46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE
) 
  48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
) 
  49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
) 
  50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
) 
  51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
) 
  53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
) 
  54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
) 
  55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
) 
  56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
) 
  58 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl 
) 
  60 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent 
) 
  62 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl 
) 
  63     EVT_PAINT(wxRichTextCtrl::OnPaint
) 
  64     EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
) 
  65     EVT_IDLE(wxRichTextCtrl::OnIdle
) 
  66     EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
) 
  67     EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
) 
  68     EVT_MOTION(wxRichTextCtrl::OnMoveMouse
) 
  69     EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
) 
  70     EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
) 
  71     EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
) 
  72     EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
) 
  73     EVT_CHAR(wxRichTextCtrl::OnChar
) 
  74     EVT_SIZE(wxRichTextCtrl::OnSize
) 
  75     EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
) 
  76     EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
) 
  77     EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
) 
  78     EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
) 
  80     EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
) 
  81     EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
) 
  83     EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
) 
  84     EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
) 
  86     EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
) 
  87     EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
) 
  89     EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
) 
  90     EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
) 
  92     EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
) 
  93     EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
) 
  95     EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
) 
  96     EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
) 
  98     EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
) 
  99     EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
) 
 106 wxArrayString 
wxRichTextCtrl::sm_availableFontNames
; 
 108 wxRichTextCtrl::wxRichTextCtrl() 
 109               : wxScrollHelper(this) 
 114 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
, 
 116                                const wxString
& value
, 
 120                                const wxValidator
& validator
, 
 121                                const wxString
& name
) 
 122               : wxScrollHelper(this) 
 125     Create(parent
, id
, value
, pos
, size
, style
, validator
, name
); 
 129 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
, 
 130                              const wxValidator
& validator
, const wxString
& name
) 
 132     if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, 
 133                                 style
|wxFULL_REPAINT_ON_RESIZE
, 
 139         SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)); 
 143     GetBuffer().SetRichTextCtrl(this); 
 145     SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16)); 
 148     if (style 
& wxTE_READONLY
) 
 151     // The base attributes must all have default values 
 152     wxTextAttrEx attributes
; 
 153     attributes
.SetFont(GetFont()); 
 154     attributes
.SetTextColour(*wxBLACK
); 
 155     attributes
.SetBackgroundColour(*wxWHITE
); 
 156     attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
); 
 157     attributes
.SetLineSpacing(10); 
 158     attributes
.SetParagraphSpacingAfter(10); 
 159     attributes
.SetParagraphSpacingBefore(0); 
 160     attributes
.SetTextEffects(0); 
 161     attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
); 
 163     SetBasicStyle(attributes
); 
 165     // The default attributes will be merged with base attributes, so 
 166     // can be empty to begin with 
 167     wxTextAttrEx defaultAttributes
; 
 168     SetDefaultStyle(defaultAttributes
); 
 170     SetBackgroundColour(*wxWHITE
); 
 171     SetBackgroundStyle(wxBG_STYLE_CUSTOM
); 
 173     // Tell the sizers to use the given or best size 
 174     SetInitialSize(size
); 
 176 #if wxRICHTEXT_BUFFERED_PAINTING 
 178     RecreateBuffer(size
); 
 181     m_textCursor 
= wxCursor(wxCURSOR_IBEAM
); 
 182     m_urlCursor 
= wxCursor(wxCURSOR_HAND
); 
 184     SetCursor(m_textCursor
); 
 186     if (!value
.IsEmpty()) 
 189     GetBuffer().AddEventHandler(this); 
 194 wxRichTextCtrl::~wxRichTextCtrl() 
 196     GetBuffer().RemoveEventHandler(this); 
 198     delete m_contextMenu
; 
 201 /// Member initialisation 
 202 void wxRichTextCtrl::Init() 
 205     m_contextMenu 
= NULL
; 
 207     m_caretPosition 
= -1; 
 208     m_selectionRange
.SetRange(-2, -2); 
 209     m_selectionAnchor 
= -2; 
 211     m_caretAtLineStart 
= false; 
 213     m_fullLayoutRequired 
= false; 
 214     m_fullLayoutTime 
= 0; 
 215     m_fullLayoutSavedPosition 
= 0; 
 216     m_delayedLayoutThreshold 
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
; 
 217     m_caretPositionForDefaultStyle 
= -2; 
 220 /// Call Freeze to prevent refresh 
 221 void wxRichTextCtrl::Freeze() 
 226 /// Call Thaw to refresh 
 227 void wxRichTextCtrl::Thaw() 
 231     if (m_freezeCount 
== 0) 
 239 void wxRichTextCtrl::Clear() 
 241     m_buffer
.ResetAndClearCommands(); 
 242     m_buffer
.SetDirty(true); 
 243     m_caretPosition 
= -1; 
 244     m_caretPositionForDefaultStyle 
= -2; 
 245     m_caretAtLineStart 
= false; 
 246     m_selectionRange
.SetRange(-2, -2); 
 248     SetScrollbars(0, 0, 0, 0, 0, 0); 
 250     if (m_freezeCount 
== 0) 
 255     SendTextUpdatedEvent(); 
 259 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
)) 
 261     if (GetCaret() && GetCaret()->IsVisible()) 
 265 #if wxRICHTEXT_BUFFERED_PAINTING 
 266         wxBufferedPaintDC 
dc(this, m_bufferBitmap
); 
 272         if (m_freezeCount 
> 0) 
 275         dc
.SetFont(GetFont()); 
 277         // Paint the background 
 280         // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize()); 
 282         wxRect 
drawingArea(GetUpdateRegion().GetBox()); 
 283         drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition())); 
 285         wxRect 
availableSpace(GetClientSize()); 
 286         if (GetBuffer().GetDirty()) 
 288             GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
); 
 289             GetBuffer().SetDirty(false); 
 293         GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */); 
 296     if (GetCaret() && !GetCaret()->IsVisible()) 
 302 // Empty implementation, to prevent flicker 
 303 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
)) 
 307 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
)) 
 311         if (!GetCaret()->IsVisible()) 
 320 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) 
 322     if (GetCaret() && GetCaret()->IsVisible()) 
 329 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
)) 
 335 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
) 
 341     dc
.SetFont(GetFont()); 
 344     int hit 
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
); 
 346     if (hit 
!= wxRICHTEXT_HITTEST_NONE
) 
 348         m_dragStart 
= event
.GetLogicalPosition(dc
); 
 354         bool caretAtLineStart 
= false; 
 356         if (hit 
& wxRICHTEXT_HITTEST_BEFORE
) 
 358             // If we're at the start of a line (but not first in para) 
 359             // then we should keep the caret showing at the start of the line 
 360             // by showing the m_caretAtLineStart flag. 
 361             wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(position
); 
 362             wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(position
); 
 364             if (line 
&& para 
&& line
->GetAbsoluteRange().GetStart() == position 
&& para
->GetRange().GetStart() != position
) 
 365                 caretAtLineStart 
= true; 
 369         MoveCaret(position
, caretAtLineStart
); 
 370         SetDefaultStyleToCursorStyle(); 
 377 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
) 
 382         if (GetCapture() == this) 
 385         // See if we clicked on a URL 
 388         dc
.SetFont(GetFont()); 
 391         wxPoint logicalPt 
= event
.GetLogicalPosition(dc
); 
 392         int hit 
= GetBuffer().HitTest(dc
, logicalPt
, position
); 
 394         if (hit 
!= wxRICHTEXT_HITTEST_NONE
) 
 397             if (GetStyle(position
, attr
)) 
 399                 if (attr
.HasFlag(wxTEXT_ATTR_URL
)) 
 401                     wxString urlTarget 
= attr
.GetURL(); 
 402                     if (!urlTarget
.IsEmpty()) 
 404                         wxMouseEvent 
mouseEvent(event
); 
 406                         long startPos 
= 0, endPos 
= 0; 
 407                         wxRichTextObject
* obj 
= GetBuffer().GetLeafObjectAtPosition(position
); 
 410                             startPos 
= obj
->GetRange().GetStart(); 
 411                             endPos 
= obj
->GetRange().GetEnd(); 
 414                         wxTextUrlEvent 
urlEvent(GetId(), mouseEvent
, startPos
, endPos
); 
 415                         InitCommandEvent(urlEvent
); 
 417                         urlEvent
.SetString(urlTarget
); 
 419                         GetEventHandler()->ProcessEvent(urlEvent
); 
 428 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
) 
 432     dc
.SetFont(GetFont()); 
 435     wxPoint logicalPt 
= event
.GetLogicalPosition(dc
); 
 436     int hit 
= GetBuffer().HitTest(dc
, logicalPt
, position
); 
 438     // See if we need to change the cursor 
 441         if (hit 
!= wxRICHTEXT_HITTEST_NONE 
&& !(hit 
& wxRICHTEXT_HITTEST_OUTSIDE
)) 
 444             if (GetStyle(position
, attr
)) 
 446                 if (attr
.HasFlag(wxTEXT_ATTR_URL
)) 
 448                     SetCursor(m_urlCursor
); 
 450                 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
)) 
 452                     SetCursor(m_textCursor
); 
 457             SetCursor(m_textCursor
); 
 460     if (!event
.Dragging()) 
 466     if (m_dragging 
&& hit 
!= wxRICHTEXT_HITTEST_NONE
) 
 468         // TODO: test closeness 
 470         bool caretAtLineStart 
= false; 
 472         if (hit 
& wxRICHTEXT_HITTEST_BEFORE
) 
 474             // If we're at the start of a line (but not first in para) 
 475             // then we should keep the caret showing at the start of the line 
 476             // by showing the m_caretAtLineStart flag. 
 477             wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(position
); 
 478             wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(position
); 
 480             if (line 
&& para 
&& line
->GetAbsoluteRange().GetStart() == position 
&& para
->GetRange().GetStart() != position
) 
 481                 caretAtLineStart 
= true; 
 485         if (m_caretPosition 
!= position
) 
 487             bool extendSel 
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
); 
 489             MoveCaret(position
, caretAtLineStart
); 
 490             SetDefaultStyleToCursorStyle(); 
 499 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
) 
 505 /// Left-double-click 
 506 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
) 
 508     SelectWord(GetCaretPosition()+1); 
 513 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
) 
 519 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
) 
 523         flags 
|= wxRICHTEXT_CTRL_DOWN
; 
 524     if (event
.ShiftDown()) 
 525         flags 
|= wxRICHTEXT_SHIFT_DOWN
; 
 527         flags 
|= wxRICHTEXT_ALT_DOWN
; 
 529     if (event
.GetKeyCode() == WXK_LEFT 
|| 
 530         event
.GetKeyCode() == WXK_RIGHT 
|| 
 531         event
.GetKeyCode() == WXK_UP 
|| 
 532         event
.GetKeyCode() == WXK_DOWN 
|| 
 533         event
.GetKeyCode() == WXK_HOME 
|| 
 534         event
.GetKeyCode() == WXK_PAGEUP 
|| 
 535         event
.GetKeyCode() == WXK_PAGEDOWN 
|| 
 536         event
.GetKeyCode() == WXK_END 
|| 
 538         event
.GetKeyCode() == WXK_NUMPAD_LEFT 
|| 
 539         event
.GetKeyCode() == WXK_NUMPAD_RIGHT 
|| 
 540         event
.GetKeyCode() == WXK_NUMPAD_UP 
|| 
 541         event
.GetKeyCode() == WXK_NUMPAD_DOWN 
|| 
 542         event
.GetKeyCode() == WXK_NUMPAD_HOME 
|| 
 543         event
.GetKeyCode() == WXK_NUMPAD_PAGEUP 
|| 
 544         event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN 
|| 
 545         event
.GetKeyCode() == WXK_NUMPAD_END
) 
 547         KeyboardNavigate(event
.GetKeyCode(), flags
); 
 551     // all the other keys modify the controls contents which shouldn't be 
 552     // possible if we're read-only 
 559     if (event
.GetKeyCode() == WXK_RETURN
) 
 561         BeginBatchUndo(_("Insert Text")); 
 563         long newPos 
= m_caretPosition
; 
 565         DeleteSelectedContent(& newPos
); 
 567         if (event
.ShiftDown()) 
 570             text 
= wxRichTextLineBreakChar
; 
 571             GetBuffer().InsertTextWithUndo(newPos
+1, text
, this); 
 574             GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
); 
 577         SetDefaultStyleToCursorStyle(); 
 579         ScrollIntoView(m_caretPosition
, WXK_RIGHT
); 
 581         wxRichTextEvent 
cmdEvent( 
 582             wxEVT_COMMAND_RICHTEXT_RETURN
, 
 584         cmdEvent
.SetEventObject(this); 
 585         cmdEvent
.SetFlags(flags
); 
 586         cmdEvent
.SetPosition(newPos
+1); 
 588         if (!GetEventHandler()->ProcessEvent(cmdEvent
)) 
 590             // Generate conventional event 
 591             wxCommandEvent 
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId()); 
 592             InitCommandEvent(textEvent
); 
 594             GetEventHandler()->ProcessEvent(textEvent
); 
 598     else if (event
.GetKeyCode() == WXK_BACK
) 
 600         BeginBatchUndo(_("Delete Text")); 
 602         // Submit range in character positions, which are greater than caret positions, 
 603         // so subtract 1 for deleted character and add 1 for conversion to character position. 
 604         if (m_caretPosition 
> -1 && !HasSelection()) 
 606             GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this); 
 609             DeleteSelectedContent(); 
 613         if (GetLastPosition() == -1) 
 617             m_caretPosition 
= -1; 
 619             SetDefaultStyleToCursorStyle(); 
 622         ScrollIntoView(m_caretPosition
, WXK_LEFT
); 
 624         wxRichTextEvent 
cmdEvent( 
 625             wxEVT_COMMAND_RICHTEXT_DELETE
, 
 627         cmdEvent
.SetEventObject(this); 
 628         cmdEvent
.SetFlags(flags
); 
 629         cmdEvent
.SetPosition(m_caretPosition
+1); 
 630         GetEventHandler()->ProcessEvent(cmdEvent
); 
 634     else if (event
.GetKeyCode() == WXK_DELETE
) 
 636         BeginBatchUndo(_("Delete Text")); 
 638         // Submit range in character positions, which are greater than caret positions, 
 639         if (m_caretPosition 
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection()) 
 641             GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this); 
 644             DeleteSelectedContent(); 
 648         if (GetLastPosition() == -1) 
 652             m_caretPosition 
= -1; 
 654             SetDefaultStyleToCursorStyle(); 
 657         wxRichTextEvent 
cmdEvent( 
 658             wxEVT_COMMAND_RICHTEXT_DELETE
, 
 660         cmdEvent
.SetEventObject(this); 
 661         cmdEvent
.SetFlags(flags
); 
 662         cmdEvent
.SetPosition(m_caretPosition
+1); 
 663         GetEventHandler()->ProcessEvent(cmdEvent
); 
 669         long keycode 
= event
.GetKeyCode(); 
 743             case WXK_NUMPAD_SPACE
: 
 745             case WXK_NUMPAD_ENTER
: 
 750             case WXK_NUMPAD_HOME
: 
 751             case WXK_NUMPAD_LEFT
: 
 753             case WXK_NUMPAD_RIGHT
: 
 754             case WXK_NUMPAD_DOWN
: 
 755             case WXK_NUMPAD_PAGEUP
: 
 756             case WXK_NUMPAD_PAGEDOWN
: 
 758             case WXK_NUMPAD_BEGIN
: 
 759             case WXK_NUMPAD_INSERT
: 
 760             case WXK_NUMPAD_DELETE
: 
 761             case WXK_NUMPAD_EQUAL
: 
 762             case WXK_NUMPAD_MULTIPLY
: 
 764             case WXK_NUMPAD_SEPARATOR
: 
 765             case WXK_NUMPAD_SUBTRACT
: 
 766             case WXK_NUMPAD_DECIMAL
: 
 774                 if (event
.CmdDown() || event
.AltDown()) 
 780                 wxRichTextEvent 
cmdEvent( 
 781                     wxEVT_COMMAND_RICHTEXT_CHARACTER
, 
 783                 cmdEvent
.SetEventObject(this); 
 784                 cmdEvent
.SetFlags(flags
); 
 785                 cmdEvent
.SetCharacter((wxChar
) keycode
); 
 786                 cmdEvent
.SetPosition(m_caretPosition
+1); 
 788                 if (keycode 
== wxT('\t')) 
 790                     // See if we need to promote or demote the selection or paragraph at the cursor 
 791                     // position, instead of inserting a tab. 
 792                     long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
 793                     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(pos
); 
 794                     if (para 
&& para
->GetRange().GetStart() == pos 
&& para
->GetAttributes().HasListStyleName()) 
 796                         wxRichTextRange range
; 
 798                             range 
= GetSelectionRange(); 
 800                             range 
= para
->GetRange().FromInternal(); 
 802                         int promoteBy 
= event
.ShiftDown() ? 1 : -1; 
 804                         PromoteList(promoteBy
, range
, NULL
); 
 806                         GetEventHandler()->ProcessEvent(cmdEvent
); 
 812                 BeginBatchUndo(_("Insert Text")); 
 814                 long newPos 
= m_caretPosition
; 
 815                 DeleteSelectedContent(& newPos
); 
 817                 wxString str 
= (wxChar
) event
.GetKeyCode(); 
 818                 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0); 
 822                 SetDefaultStyleToCursorStyle(); 
 823                 ScrollIntoView(m_caretPosition
, WXK_RIGHT
); 
 825                 GetEventHandler()->ProcessEvent(cmdEvent
); 
 833 /// Delete content if there is a selection, e.g. when pressing a key. 
 834 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
) 
 838         long pos 
= m_selectionRange
.GetStart(); 
 839         GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this); 
 840         m_selectionRange
.SetRange(-2, -2); 
 850 /// Keyboard navigation 
 854 Left:       left one character 
 855 Right:      right one character 
 858 Ctrl-Left:  left one word 
 859 Ctrl-Right: right one word 
 860 Ctrl-Up:    previous paragraph start 
 861 Ctrl-Down:  next start of paragraph 
 864 Ctrl-Home:  start of document 
 865 Ctrl-End:   end of document 
 867 Page-Down:  Down a screen 
 871 Ctrl-Alt-PgUp: Start of window 
 872 Ctrl-Alt-PgDn: End of window 
 873 F8:         Start selection mode 
 874 Esc:        End selection mode 
 876 Adding Shift does the above but starts/extends selection. 
 881 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
) 
 883     bool success 
= false; 
 885     if (keyCode 
== WXK_RIGHT 
|| keyCode 
== WXK_NUMPAD_RIGHT
) 
 887         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
 888             success 
= WordRight(1, flags
); 
 890             success 
= MoveRight(1, flags
); 
 892     else if (keyCode 
== WXK_LEFT 
|| keyCode 
== WXK_NUMPAD_LEFT
) 
 894         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
 895             success 
= WordLeft(1, flags
); 
 897             success 
= MoveLeft(1, flags
); 
 899     else if (keyCode 
== WXK_UP 
|| keyCode 
== WXK_NUMPAD_UP
) 
 901         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
 902             success 
= MoveToParagraphStart(flags
); 
 904             success 
= MoveUp(1, flags
); 
 906     else if (keyCode 
== WXK_DOWN 
|| keyCode 
== WXK_NUMPAD_DOWN
) 
 908         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
 909             success 
= MoveToParagraphEnd(flags
); 
 911             success 
= MoveDown(1, flags
); 
 913     else if (keyCode 
== WXK_PAGEUP 
|| keyCode 
== WXK_NUMPAD_PAGEUP
) 
 915         success 
= PageUp(1, flags
); 
 917     else if (keyCode 
== WXK_PAGEDOWN 
|| keyCode 
== WXK_NUMPAD_PAGEDOWN
) 
 919         success 
= PageDown(1, flags
); 
 921     else if (keyCode 
== WXK_HOME 
|| keyCode 
== WXK_NUMPAD_HOME
) 
 923         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
 924             success 
= MoveHome(flags
); 
 926             success 
= MoveToLineStart(flags
); 
 928     else if (keyCode 
== WXK_END 
|| keyCode 
== WXK_NUMPAD_END
) 
 930         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
 931             success 
= MoveEnd(flags
); 
 933             success 
= MoveToLineEnd(flags
); 
 938         ScrollIntoView(m_caretPosition
, keyCode
); 
 939         SetDefaultStyleToCursorStyle(); 
 945 /// Extend the selection. Selections are in caret positions. 
 946 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
) 
 948     if (flags 
& wxRICHTEXT_SHIFT_DOWN
) 
 950         // If not currently selecting, start selecting 
 951         if (m_selectionRange
.GetStart() == -2) 
 953             m_selectionAnchor 
= oldPos
; 
 956                 m_selectionRange
.SetRange(newPos
+1, oldPos
); 
 958                 m_selectionRange
.SetRange(oldPos
+1, newPos
); 
 962             // Always ensure that the selection range start is greater than 
 964             if (newPos 
> m_selectionAnchor
) 
 965                 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
); 
 967                 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
); 
 970         if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd()) 
 972             wxLogDebug(wxT("Strange selection range")); 
 981 /// Scroll into view, returning true if we scrolled. 
 982 /// This takes a _caret_ position. 
 983 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
) 
 985     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(position
); 
 991     GetScrollPixelsPerUnit(& ppuX
, & ppuY
); 
 993     int startXUnits
, startYUnits
; 
 994     GetViewStart(& startXUnits
, & startYUnits
); 
 995     int startY 
= startYUnits 
* ppuY
; 
 998     GetVirtualSize(& sx
, & sy
); 
1004     wxRect rect 
= line
->GetRect(); 
1006     bool scrolled 
= false; 
1008     wxSize clientSize 
= GetClientSize(); 
1011     if (keyCode 
== WXK_DOWN 
|| keyCode 
== WXK_NUMPAD_DOWN 
|| 
1012         keyCode 
== WXK_RIGHT 
|| keyCode 
== WXK_NUMPAD_DOWN 
|| 
1013         keyCode 
== WXK_END 
|| keyCode 
== WXK_NUMPAD_END 
|| 
1014         keyCode 
== WXK_PAGEDOWN 
|| keyCode 
== WXK_NUMPAD_PAGEDOWN
) 
1016         if ((rect
.y 
+ rect
.height
) > (clientSize
.y 
+ startY
)) 
1018             // Make it scroll so this item is at the bottom 
1020             int y 
= rect
.y 
- (clientSize
.y 
- rect
.height
); 
1021             int yUnits 
= (int) (0.5 + ((float) y
)/(float) ppuY
); 
1023             // If we're still off the screen, scroll another line down 
1024             if ((rect
.y 
+ rect
.height
) > (clientSize
.y 
+ (yUnits
*ppuY
))) 
1027             if (startYUnits 
!= yUnits
) 
1029                 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
); 
1033         else if (rect
.y 
< startY
) 
1035             // Make it scroll so this item is at the top 
1038             int yUnits 
= (int) (0.5 + ((float) y
)/(float) ppuY
); 
1040             if (startYUnits 
!= yUnits
) 
1042                 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
); 
1048     else if (keyCode 
== WXK_UP  
|| keyCode 
== WXK_NUMPAD_UP 
|| 
1049              keyCode 
== WXK_LEFT 
|| keyCode 
== WXK_NUMPAD_LEFT 
|| 
1050              keyCode 
== WXK_HOME 
|| keyCode 
== WXK_NUMPAD_HOME 
|| 
1051              keyCode 
== WXK_PAGEUP 
|| keyCode 
== WXK_NUMPAD_PAGEUP 
) 
1053         if (rect
.y 
< startY
) 
1055             // Make it scroll so this item is at the top 
1058             int yUnits 
= (int) (0.5 + ((float) y
)/(float) ppuY
); 
1060             if (startYUnits 
!= yUnits
) 
1062                 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
); 
1066         else if ((rect
.y 
+ rect
.height
) > (clientSize
.y 
+ startY
)) 
1068             // Make it scroll so this item is at the bottom 
1070             int y 
= rect
.y 
- (clientSize
.y 
- rect
.height
); 
1071             int yUnits 
= (int) (0.5 + ((float) y
)/(float) ppuY
); 
1073             // If we're still off the screen, scroll another line down 
1074             if ((rect
.y 
+ rect
.height
) > (clientSize
.y 
+ (yUnits
*ppuY
))) 
1077             if (startYUnits 
!= yUnits
) 
1079                 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
); 
1089 /// Is the given position visible on the screen? 
1090 bool wxRichTextCtrl::IsPositionVisible(long pos
) const 
1092     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(pos
-1); 
1098     GetScrollPixelsPerUnit(& ppuX
, & ppuY
); 
1101     GetViewStart(& startX
, & startY
); 
1103     startY 
= startY 
* ppuY
; 
1106     GetVirtualSize(& sx
, & sy
); 
1111     wxRect rect 
= line
->GetRect(); 
1113     wxSize clientSize 
= GetClientSize(); 
1115     return !(((rect
.y 
+ rect
.height
) > (clientSize
.y 
+ startY
)) || rect
.y 
< startY
); 
1118 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
) 
1120     m_caretPosition 
= position
; 
1121     m_caretAtLineStart 
= showAtLineStart
; 
1124 /// Move caret one visual step forward: this may mean setting a flag 
1125 /// and keeping the same position if we're going from the end of one line 
1126 /// to the start of the next, which may be the exact same caret position. 
1127 void wxRichTextCtrl::MoveCaretForward(long oldPosition
) 
1129     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(oldPosition
); 
1131     // Only do the check if we're not at the end of the paragraph (where things work OK 
1133     if (para 
&& (oldPosition 
!= para
->GetRange().GetEnd() - 1)) 
1135         wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(oldPosition
); 
1139             wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1141             // We're at the end of a line. See whether we need to 
1142             // stay at the same actual caret position but change visual 
1143             // position, or not. 
1144             if (oldPosition 
== lineRange
.GetEnd()) 
1146                 if (m_caretAtLineStart
) 
1148                     // We're already at the start of the line, so actually move on now. 
1149                     m_caretPosition 
= oldPosition 
+ 1; 
1150                     m_caretAtLineStart 
= false; 
1154                     // We're showing at the end of the line, so keep to 
1155                     // the same position but indicate that we're to show 
1156                     // at the start of the next line. 
1157                     m_caretPosition 
= oldPosition
; 
1158                     m_caretAtLineStart 
= true; 
1160                 SetDefaultStyleToCursorStyle(); 
1166     SetDefaultStyleToCursorStyle(); 
1169 /// Move caret one visual step backward: this may mean setting a flag 
1170 /// and keeping the same position if we're going from the end of one line 
1171 /// to the start of the next, which may be the exact same caret position. 
1172 void wxRichTextCtrl::MoveCaretBack(long oldPosition
) 
1174     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(oldPosition
); 
1176     // Only do the check if we're not at the start of the paragraph (where things work OK 
1178     if (para 
&& (oldPosition 
!= para
->GetRange().GetStart())) 
1180         wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(oldPosition
); 
1184             wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1186             // We're at the start of a line. See whether we need to 
1187             // stay at the same actual caret position but change visual 
1188             // position, or not. 
1189             if (oldPosition 
== lineRange
.GetStart()) 
1191                 m_caretPosition 
= oldPosition
-1; 
1192                 m_caretAtLineStart 
= true; 
1195             else if (oldPosition 
== lineRange
.GetEnd()) 
1197                 if (m_caretAtLineStart
) 
1199                     // We're at the start of the line, so keep the same caret position 
1200                     // but clear the start-of-line flag. 
1201                     m_caretPosition 
= oldPosition
; 
1202                     m_caretAtLineStart 
= false; 
1206                     // We're showing at the end of the line, so go back 
1207                     // to the previous character position. 
1208                     m_caretPosition 
= oldPosition 
- 1; 
1210                 SetDefaultStyleToCursorStyle(); 
1216     SetDefaultStyleToCursorStyle(); 
1220 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
) 
1222     long endPos 
= GetBuffer().GetRange().GetEnd(); 
1224     if (m_caretPosition 
+ noPositions 
< endPos
) 
1226         long oldPos 
= m_caretPosition
; 
1227         long newPos 
= m_caretPosition 
+ noPositions
; 
1229         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1233         // Determine by looking at oldPos and m_caretPosition whether 
1234         // we moved from the end of a line to the start of the next line, in which case 
1235         // we want to adjust the caret position such that it is positioned at the 
1236         // start of the next line, rather than jumping past the first character of the 
1238         if (noPositions 
== 1 && !extendSel
) 
1239             MoveCaretForward(oldPos
); 
1241             SetCaretPosition(newPos
); 
1244         SetDefaultStyleToCursorStyle(); 
1255 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
) 
1259     if (m_caretPosition 
> startPos 
- noPositions 
+ 1) 
1261         long oldPos 
= m_caretPosition
; 
1262         long newPos 
= m_caretPosition 
- noPositions
; 
1263         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1267         if (noPositions 
== 1 && !extendSel
) 
1268             MoveCaretBack(oldPos
); 
1270             SetCaretPosition(newPos
); 
1273         SetDefaultStyleToCursorStyle(); 
1284 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
) 
1286     return MoveDown(- noLines
, flags
); 
1290 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
) 
1295     long lineNumber 
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
); 
1296     wxPoint pt 
= GetCaret()->GetPosition(); 
1297     long newLine 
= lineNumber 
+ noLines
; 
1299     if (lineNumber 
!= -1) 
1303             long lastLine 
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd()); 
1305             if (newLine 
> lastLine
) 
1315     wxRichTextLine
* lineObj 
= GetBuffer().GetLineForVisibleLineNumber(newLine
); 
1318         pt
.y 
= lineObj
->GetAbsolutePosition().y 
+ 2; 
1324     wxClientDC 
dc(this); 
1326     dc
.SetFont(GetFont()); 
1328     int hitTest 
= GetBuffer().HitTest(dc
, pt
, newPos
); 
1330     if (hitTest 
!= wxRICHTEXT_HITTEST_NONE
) 
1332         // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE, 
1333         // we want to be at the end of the last line but with m_caretAtLineStart set to true, 
1334         // so we view the caret at the start of the line. 
1335         bool caretLineStart 
= false; 
1336         if (hitTest 
& wxRICHTEXT_HITTEST_BEFORE
) 
1338             wxRichTextLine
* thisLine 
= GetBuffer().GetLineAtPosition(newPos
-1); 
1339             wxRichTextRange lineRange
; 
1341                 lineRange 
= thisLine
->GetAbsoluteRange(); 
1343             if (thisLine 
&& (newPos
-1) == lineRange
.GetEnd()) 
1346                 caretLineStart 
= true; 
1350                 wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(newPos
); 
1351                 if (para 
&& para
->GetRange().GetStart() == newPos
) 
1356         long newSelEnd 
= newPos
; 
1358         bool extendSel 
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
); 
1362         SetCaretPosition(newPos
, caretLineStart
); 
1364         SetDefaultStyleToCursorStyle(); 
1374 /// Move to the end of the paragraph 
1375 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
) 
1377     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true); 
1380         long newPos 
= para
->GetRange().GetEnd() - 1; 
1381         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1385         SetCaretPosition(newPos
); 
1387         SetDefaultStyleToCursorStyle(); 
1397 /// Move to the start of the paragraph 
1398 bool wxRichTextCtrl::MoveToParagraphStart(int flags
) 
1400     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true); 
1403         long newPos 
= para
->GetRange().GetStart() - 1; 
1404         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1408         SetCaretPosition(newPos
); 
1410         SetDefaultStyleToCursorStyle(); 
1420 /// Move to the end of the line 
1421 bool wxRichTextCtrl::MoveToLineEnd(int flags
) 
1423     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(m_caretPosition
); 
1427         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1428         long newPos 
= lineRange
.GetEnd(); 
1429         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1433         SetCaretPosition(newPos
); 
1435         SetDefaultStyleToCursorStyle(); 
1445 /// Move to the start of the line 
1446 bool wxRichTextCtrl::MoveToLineStart(int flags
) 
1448     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(m_caretPosition
); 
1451         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1452         long newPos 
= lineRange
.GetStart()-1; 
1454         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1458         wxRichTextParagraph
* para 
= GetBuffer().GetParagraphForLine(line
); 
1460         SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart()); 
1462         SetDefaultStyleToCursorStyle(); 
1472 /// Move to the start of the buffer 
1473 bool wxRichTextCtrl::MoveHome(int flags
) 
1475     if (m_caretPosition 
!= -1) 
1477         bool extendSel 
= ExtendSelection(m_caretPosition
, -1, flags
); 
1481         SetCaretPosition(-1); 
1483         SetDefaultStyleToCursorStyle(); 
1493 /// Move to the end of the buffer 
1494 bool wxRichTextCtrl::MoveEnd(int flags
) 
1496     long endPos 
= GetBuffer().GetRange().GetEnd()-1; 
1498     if (m_caretPosition 
!= endPos
) 
1500         bool extendSel 
= ExtendSelection(m_caretPosition
, endPos
, flags
); 
1504         SetCaretPosition(endPos
); 
1506         SetDefaultStyleToCursorStyle(); 
1516 /// Move noPages pages up 
1517 bool wxRichTextCtrl::PageUp(int noPages
, int flags
) 
1519     return PageDown(- noPages
, flags
); 
1522 /// Move noPages pages down 
1523 bool wxRichTextCtrl::PageDown(int noPages
, int flags
) 
1525     // Calculate which line occurs noPages * screen height further down. 
1526     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(m_caretPosition
); 
1529         wxSize clientSize 
= GetClientSize(); 
1530         int newY 
= line
->GetAbsolutePosition().y 
+ noPages
*clientSize
.y
; 
1532         wxRichTextLine
* newLine 
= GetBuffer().GetLineAtYPosition(newY
); 
1535             wxRichTextRange lineRange 
= newLine
->GetAbsoluteRange(); 
1536             long pos 
= lineRange
.GetStart()-1; 
1537             if (pos 
!= m_caretPosition
) 
1539                 wxRichTextParagraph
* para 
= GetBuffer().GetParagraphForLine(newLine
); 
1541                 bool extendSel 
= ExtendSelection(m_caretPosition
, pos
, flags
); 
1545                 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart()); 
1547                 SetDefaultStyleToCursorStyle(); 
1559 // Finds the caret position for the next word 
1560 long wxRichTextCtrl::FindNextWordPosition(int direction
) const 
1562     long endPos 
= GetBuffer().GetRange().GetEnd(); 
1566         long i 
= m_caretPosition
+1+direction
; // +1 for conversion to character pos 
1568         // First skip current text to space 
1569         while (i 
< endPos 
&& i 
> -1) 
1571             // i is in character, not caret positions 
1572             wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
)); 
1573             if (text 
!= wxT(" ") && !text
.empty()) 
1580         while (i 
< endPos 
&& i 
> -1) 
1582             // i is in character, not caret positions 
1583             wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
)); 
1584             if (text
.empty()) // End of paragraph, or maybe an image 
1585                 return wxMax(-1, i 
- 1); 
1586             else if (text 
== wxT(" ") || text
.empty()) 
1590                 // Convert to caret position 
1591                 return wxMax(-1, i 
- 1); 
1600         long i 
= m_caretPosition
; 
1602         // First skip white space 
1603         while (i 
< endPos 
&& i 
> -1) 
1605             // i is in character, not caret positions 
1606             wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
)); 
1607             if (text
.empty()) // End of paragraph, or maybe an image 
1609             else if (text 
== wxT(" ") || text
.empty()) 
1614         // Next skip current text to space 
1615         while (i 
< endPos 
&& i 
> -1) 
1617             // i is in character, not caret positions 
1618             wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
)); 
1619             if (text 
!= wxT(" ") /* && !text.empty() */) 
1632 /// Move n words left 
1633 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
) 
1635     long pos 
= FindNextWordPosition(-1); 
1636     if (pos 
!= m_caretPosition
) 
1638         wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(pos
, true); 
1640         bool extendSel 
= ExtendSelection(m_caretPosition
, pos
, flags
); 
1644         SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
); 
1646         SetDefaultStyleToCursorStyle(); 
1656 /// Move n words right 
1657 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
) 
1659     long pos 
= FindNextWordPosition(1); 
1660     if (pos 
!= m_caretPosition
) 
1662         wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(pos
, true); 
1664         bool extendSel 
= ExtendSelection(m_caretPosition
, pos
, flags
); 
1668         SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
); 
1670         SetDefaultStyleToCursorStyle(); 
1681 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
) 
1683     // Only do sizing optimization for large buffers 
1684     if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
) 
1686         m_fullLayoutRequired 
= true; 
1687         m_fullLayoutTime 
= wxGetLocalTimeMillis(); 
1688         m_fullLayoutSavedPosition 
= GetFirstVisiblePosition(); 
1689         LayoutContent(true /* onlyVisibleRect */); 
1692         GetBuffer().Invalidate(wxRICHTEXT_ALL
); 
1694 #if wxRICHTEXT_BUFFERED_PAINTING 
1702 /// Idle-time processing 
1703 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
) 
1705     const int layoutInterval 
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
; 
1707     if (m_fullLayoutRequired 
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime 
+ layoutInterval
))) 
1709         m_fullLayoutRequired 
= false; 
1710         m_fullLayoutTime 
= 0; 
1711         GetBuffer().Invalidate(wxRICHTEXT_ALL
); 
1712         ShowPosition(m_fullLayoutSavedPosition
); 
1716     if (m_caretPositionForDefaultStyle 
!= -2) 
1718         // If the caret position has changed, no longer reflect the default style 
1720         if (GetCaretPosition() != m_caretPositionForDefaultStyle
) 
1721             m_caretPositionForDefaultStyle 
= -2; 
1728 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
) 
1734 /// Set up scrollbars, e.g. after a resize 
1735 void wxRichTextCtrl::SetupScrollbars(bool atTop
) 
1740     if (GetBuffer().IsEmpty()) 
1742         SetScrollbars(0, 0, 0, 0, 0, 0); 
1746     // TODO: reimplement scrolling so we scroll by line, not by fixed number 
1747     // of pixels. See e.g. wxVScrolledWindow for ideas. 
1748     int pixelsPerUnit 
= 5; 
1749     wxSize clientSize 
= GetClientSize(); 
1751     int maxHeight 
= GetBuffer().GetCachedSize().y
; 
1753     // Round up so we have at least maxHeight pixels 
1754     int unitsY 
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5); 
1756     int startX 
= 0, startY 
= 0; 
1758         GetViewStart(& startX
, & startY
); 
1760     int maxPositionX 
= 0; // wxMax(sz.x - clientSize.x, 0); 
1761     int maxPositionY 
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5); 
1763     // Move to previous scroll position if 
1765     SetScrollbars(0, pixelsPerUnit
, 
1767         wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
)); 
1770 /// Paint the background 
1771 void wxRichTextCtrl::PaintBackground(wxDC
& dc
) 
1773     wxColour backgroundColour 
= GetBackgroundColour(); 
1774     if (!backgroundColour
.Ok()) 
1775         backgroundColour 
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
); 
1777     // Clear the background 
1778     dc
.SetBrush(wxBrush(backgroundColour
)); 
1779     dc
.SetPen(*wxTRANSPARENT_PEN
); 
1780     wxRect 
windowRect(GetClientSize()); 
1781     windowRect
.x 
-= 2; windowRect
.y 
-= 2; 
1782     windowRect
.width 
+= 4; windowRect
.height 
+= 4; 
1784     // We need to shift the rectangle to take into account 
1785     // scrolling. Converting device to logical coordinates. 
1786     CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
); 
1787     dc
.DrawRectangle(windowRect
); 
1790 #if wxRICHTEXT_BUFFERED_PAINTING 
1791 /// Recreate buffer bitmap if necessary 
1792 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
) 
1795     if (sz 
== wxDefaultSize
) 
1796         sz 
= GetClientSize(); 
1798     if (sz
.x 
< 1 || sz
.y 
< 1) 
1801     if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x 
|| m_bufferBitmap
.GetHeight() < sz
.y
) 
1802         m_bufferBitmap 
= wxBitmap(sz
.x
, sz
.y
); 
1803     return m_bufferBitmap
.Ok(); 
1807 // ---------------------------------------------------------------------------- 
1808 // file IO functions 
1809 // ---------------------------------------------------------------------------- 
1811 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
) 
1813     bool success 
= GetBuffer().LoadFile(filename
, fileType
); 
1815         m_filename 
= filename
; 
1818     SetInsertionPoint(0); 
1821     SetupScrollbars(true); 
1823     SendTextUpdatedEvent(); 
1829         wxLogError(_("File couldn't be loaded.")); 
1835 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
) 
1837     if (GetBuffer().SaveFile(filename
, fileType
)) 
1839         m_filename 
= filename
; 
1846     wxLogError(_("The text couldn't be saved.")); 
1851 // ---------------------------------------------------------------------------- 
1852 // wxRichTextCtrl specific functionality 
1853 // ---------------------------------------------------------------------------- 
1855 /// Add a new paragraph of text to the end of the buffer 
1856 wxRichTextRange 
wxRichTextCtrl::AddParagraph(const wxString
& text
) 
1858     return GetBuffer().AddParagraph(text
); 
1862 wxRichTextRange 
wxRichTextCtrl::AddImage(const wxImage
& image
) 
1864     return GetBuffer().AddImage(image
); 
1867 // ---------------------------------------------------------------------------- 
1868 // selection and ranges 
1869 // ---------------------------------------------------------------------------- 
1871 void wxRichTextCtrl::SelectAll() 
1873     SetSelection(0, GetLastPosition()+1); 
1874     m_selectionAnchor 
= -1; 
1878 void wxRichTextCtrl::SelectNone() 
1880     if (!(GetSelectionRange() == wxRichTextRange(-2, -2))) 
1881         SetSelection(-2, -2); 
1882     m_selectionAnchor 
= -2; 
1885 static bool wxIsWordDelimiter(const wxString
& text
) 
1887     return !text
.IsEmpty() && !wxIsalnum(text
[0]); 
1890 /// Select the word at the given character position 
1891 bool wxRichTextCtrl::SelectWord(long position
) 
1893     if (position 
< 0 || position 
> GetBuffer().GetRange().GetEnd()) 
1896     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(position
); 
1900     long positionStart 
= position
; 
1901     long positionEnd 
= position
; 
1903     for (positionStart 
= position
; positionStart 
>= para
->GetRange().GetStart(); positionStart 
--) 
1905         wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
)); 
1906         if (wxIsWordDelimiter(text
)) 
1912     if (positionStart 
< para
->GetRange().GetStart()) 
1913         positionStart 
= para
->GetRange().GetStart(); 
1915     for (positionEnd 
= position
; positionEnd 
< para
->GetRange().GetEnd(); positionEnd 
++) 
1917         wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
)); 
1918         if (wxIsWordDelimiter(text
)) 
1924     if (positionEnd 
>= para
->GetRange().GetEnd()) 
1925         positionEnd 
= para
->GetRange().GetEnd(); 
1927     SetSelection(positionStart
, positionEnd
+1); 
1929     if (positionStart 
>= 0) 
1931         MoveCaret(positionStart
-1, true); 
1932         SetDefaultStyleToCursorStyle(); 
1938 wxString 
wxRichTextCtrl::GetStringSelection() const 
1941     GetSelection(&from
, &to
); 
1943     return GetRange(from
, to
); 
1946 // ---------------------------------------------------------------------------- 
1948 // ---------------------------------------------------------------------------- 
1950 wxTextCtrlHitTestResult
 
1951 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord 
*x
, wxTextCoord 
*y
) const 
1953     // implement in terms of the other overload as the native ports typically 
1954     // can get the position and not (x, y) pair directly (although wxUniv 
1955     // directly gets x and y -- and so overrides this method as well) 
1957     wxTextCtrlHitTestResult rc 
= HitTest(pt
, &pos
); 
1959     if ( rc 
!= wxTE_HT_UNKNOWN 
) 
1961         PositionToXY(pos
, x
, y
); 
1967 wxTextCtrlHitTestResult
 
1968 wxRichTextCtrl::HitTest(const wxPoint
& pt
, 
1971     wxClientDC 
dc((wxRichTextCtrl
*) this); 
1972     ((wxRichTextCtrl
*)this)->PrepareDC(dc
); 
1974     // Buffer uses logical position (relative to start of buffer) 
1976     wxPoint pt2 
= GetLogicalPoint(pt
); 
1978     int hit 
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
); 
1980     if ((hit 
& wxRICHTEXT_HITTEST_BEFORE
) && (hit 
& wxRICHTEXT_HITTEST_OUTSIDE
)) 
1981         return wxTE_HT_BEFORE
; 
1982     else if ((hit 
& wxRICHTEXT_HITTEST_AFTER
) && (hit 
& wxRICHTEXT_HITTEST_OUTSIDE
)) 
1983         return wxTE_HT_BEYOND
; 
1984     else if (hit 
& wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
) 
1985         return wxTE_HT_ON_TEXT
; 
1987         return wxTE_HT_UNKNOWN
; 
1989     return wxTE_HT_UNKNOWN
; 
1992 // ---------------------------------------------------------------------------- 
1993 // set/get the controls text 
1994 // ---------------------------------------------------------------------------- 
1996 wxString 
wxRichTextCtrl::GetValue() const 
1998     return GetBuffer().GetText(); 
2001 wxString 
wxRichTextCtrl::GetRange(long from
, long to
) const 
2003     // Public API for range is different from internals 
2004     return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1)); 
2007 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
) 
2011     // if the text is long enough, it's faster to just set it instead of first 
2012     // comparing it with the old one (chances are that it will be different 
2013     // anyhow, this comparison is there to avoid flicker for small single-line 
2014     // edit controls mostly) 
2015     if ( (value
.length() > 0x400) || (value 
!= GetValue()) ) 
2019         // for compatibility, don't move the cursor when doing SetValue() 
2020         SetInsertionPoint(0); 
2024         if ( flags 
& SetValue_SendEvent 
) 
2026             // still send an event for consistency 
2027             SendTextUpdatedEvent(); 
2031     // we should reset the modified flag even if the value didn't really change 
2033     // mark the control as being not dirty - we changed its text, not the 
2038 void wxRichTextCtrl::WriteText(const wxString
& value
) 
2043 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
) 
2045     wxString valueUnix 
= wxTextFile::Translate(value
, wxTextFileType_Unix
); 
2047     GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
); 
2049     if ( flags 
& SetValue_SendEvent 
) 
2050         SendTextUpdatedEvent(); 
2053 void wxRichTextCtrl::AppendText(const wxString
& text
) 
2055     SetInsertionPointEnd(); 
2060 /// Write an image at the current insertion point 
2061 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
) 
2063     wxRichTextImageBlock imageBlock
; 
2065     wxImage image2 
= image
; 
2066     if (imageBlock
.MakeImageBlock(image2
, bitmapType
)) 
2067         return WriteImage(imageBlock
); 
2072 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
) 
2074     wxRichTextImageBlock imageBlock
; 
2077     if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false)) 
2078         return WriteImage(imageBlock
); 
2083 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
) 
2085     return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this); 
2088 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
) 
2092         wxRichTextImageBlock imageBlock
; 
2094         wxImage image 
= bitmap
.ConvertToImage(); 
2095         if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
)) 
2096             return WriteImage(imageBlock
); 
2102 /// Insert a newline (actually paragraph) at the current insertion point. 
2103 bool wxRichTextCtrl::Newline() 
2105     return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this); 
2108 /// Insert a line break at the current insertion point. 
2109 bool wxRichTextCtrl::LineBreak() 
2112     text 
= wxRichTextLineBreakChar
; 
2113     return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this); 
2116 // ---------------------------------------------------------------------------- 
2117 // Clipboard operations 
2118 // ---------------------------------------------------------------------------- 
2120 void wxRichTextCtrl::Copy() 
2124         wxRichTextRange range 
= GetInternalSelectionRange(); 
2125         GetBuffer().CopyToClipboard(range
); 
2129 void wxRichTextCtrl::Cut() 
2133         wxRichTextRange range 
= GetInternalSelectionRange(); 
2134         GetBuffer().CopyToClipboard(range
); 
2136         DeleteSelectedContent(); 
2142 void wxRichTextCtrl::Paste() 
2146         BeginBatchUndo(_("Paste")); 
2148         long newPos 
= m_caretPosition
; 
2149         DeleteSelectedContent(& newPos
); 
2151         GetBuffer().PasteFromClipboard(newPos
); 
2157 void wxRichTextCtrl::DeleteSelection() 
2159     if (CanDeleteSelection()) 
2161         DeleteSelectedContent(); 
2165 bool wxRichTextCtrl::HasSelection() const 
2167     return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2; 
2170 bool wxRichTextCtrl::CanCopy() const 
2172     // Can copy if there's a selection 
2173     return HasSelection(); 
2176 bool wxRichTextCtrl::CanCut() const 
2178     return HasSelection() && IsEditable(); 
2181 bool wxRichTextCtrl::CanPaste() const 
2183     if ( !IsEditable() ) 
2186     return GetBuffer().CanPasteFromClipboard(); 
2189 bool wxRichTextCtrl::CanDeleteSelection() const 
2191     return HasSelection() && IsEditable(); 
2195 // ---------------------------------------------------------------------------- 
2197 // ---------------------------------------------------------------------------- 
2199 void wxRichTextCtrl::SetEditable(bool editable
) 
2201     m_editable 
= editable
; 
2204 void wxRichTextCtrl::SetInsertionPoint(long pos
) 
2208     m_caretPosition 
= pos 
- 1; 
2211 void wxRichTextCtrl::SetInsertionPointEnd() 
2213     long pos 
= GetLastPosition(); 
2214     SetInsertionPoint(pos
); 
2217 long wxRichTextCtrl::GetInsertionPoint() const 
2219     return m_caretPosition
+1; 
2222 wxTextPos 
wxRichTextCtrl::GetLastPosition() const 
2224     return GetBuffer().GetRange().GetEnd(); 
2227 // If the return values from and to are the same, there is no 
2229 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const 
2231     *from 
= m_selectionRange
.GetStart(); 
2232     *to 
= m_selectionRange
.GetEnd(); 
2233     if ((*to
) != -1 && (*to
) != -2) 
2237 bool wxRichTextCtrl::IsEditable() const 
2242 // ---------------------------------------------------------------------------- 
2244 // ---------------------------------------------------------------------------- 
2246 void wxRichTextCtrl::SetSelection(long from
, long to
) 
2248     // if from and to are both -1, it means (in wxWidgets) that all text should 
2250     if ( (from 
== -1) && (to 
== -1) ) 
2253         to 
= GetLastPosition()+1; 
2256     DoSetSelection(from
, to
); 
2259 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
)) 
2261     m_selectionAnchor 
= from
; 
2262     m_selectionRange
.SetRange(from
, to
-1); 
2264         m_caretPosition 
= from
-1; 
2270 // ---------------------------------------------------------------------------- 
2272 // ---------------------------------------------------------------------------- 
2274 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), 
2275                              const wxString
& value
) 
2277     BeginBatchUndo(_("Replace")); 
2279     DeleteSelectedContent(); 
2281     DoWriteText(value
, SetValue_SelectionOnly
); 
2286 void wxRichTextCtrl::Remove(long from
, long to
) 
2290     GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this); 
2297 bool wxRichTextCtrl::IsModified() const 
2299     return m_buffer
.IsModified(); 
2302 void wxRichTextCtrl::MarkDirty() 
2304     m_buffer
.Modify(true); 
2307 void wxRichTextCtrl::DiscardEdits() 
2309     m_caretPositionForDefaultStyle 
= -2; 
2310     m_buffer
.Modify(false); 
2311     m_buffer
.GetCommandProcessor()->ClearCommands(); 
2314 int wxRichTextCtrl::GetNumberOfLines() const 
2316     return GetBuffer().GetParagraphCount(); 
2319 // ---------------------------------------------------------------------------- 
2320 // Positions <-> coords 
2321 // ---------------------------------------------------------------------------- 
2323 long wxRichTextCtrl::XYToPosition(long x
, long y
) const 
2325     return GetBuffer().XYToPosition(x
, y
); 
2328 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const 
2330     return GetBuffer().PositionToXY(pos
, x
, y
); 
2333 // ---------------------------------------------------------------------------- 
2335 // ---------------------------------------------------------------------------- 
2337 void wxRichTextCtrl::ShowPosition(long pos
) 
2339     if (!IsPositionVisible(pos
)) 
2340         ScrollIntoView(pos
-1, WXK_DOWN
); 
2343 int wxRichTextCtrl::GetLineLength(long lineNo
) const 
2345     return GetBuffer().GetParagraphLength(lineNo
); 
2348 wxString 
wxRichTextCtrl::GetLineText(long lineNo
) const 
2350     return GetBuffer().GetParagraphText(lineNo
); 
2353 // ---------------------------------------------------------------------------- 
2355 // ---------------------------------------------------------------------------- 
2357 void wxRichTextCtrl::Undo() 
2361         GetCommandProcessor()->Undo(); 
2365 void wxRichTextCtrl::Redo() 
2369         GetCommandProcessor()->Redo(); 
2373 bool wxRichTextCtrl::CanUndo() const 
2375     return GetCommandProcessor()->CanUndo(); 
2378 bool wxRichTextCtrl::CanRedo() const 
2380     return GetCommandProcessor()->CanRedo(); 
2383 // ---------------------------------------------------------------------------- 
2384 // implementation details 
2385 // ---------------------------------------------------------------------------- 
2387 void wxRichTextCtrl::Command(wxCommandEvent
& event
) 
2389     SetValue(event
.GetString()); 
2390     GetEventHandler()->ProcessEvent(event
); 
2393 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
) 
2395     // By default, load the first file into the text window. 
2396     if (event
.GetNumberOfFiles() > 0) 
2398         LoadFile(event
.GetFiles()[0]); 
2402 wxSize 
wxRichTextCtrl::DoGetBestSize() const 
2404     return wxSize(10, 10); 
2407 // ---------------------------------------------------------------------------- 
2408 // standard handlers for standard edit menu events 
2409 // ---------------------------------------------------------------------------- 
2411 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
)) 
2416 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
)) 
2421 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
)) 
2426 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
)) 
2431 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
)) 
2436 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
)) 
2441 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
) 
2443     event
.Enable( CanCut() ); 
2446 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
) 
2448     event
.Enable( CanCopy() ); 
2451 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
) 
2453     event
.Enable( CanDeleteSelection() ); 
2456 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
) 
2458     event
.Enable( CanPaste() ); 
2461 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
) 
2463     event
.Enable( CanUndo() ); 
2464     event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() ); 
2467 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
) 
2469     event
.Enable( CanRedo() ); 
2470     event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() ); 
2473 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
)) 
2478 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
) 
2480     event
.Enable(GetLastPosition() > 0); 
2483 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
)) 
2487         m_contextMenu 
= new wxMenu
; 
2488         m_contextMenu
->Append(wxID_UNDO
, _("&Undo")); 
2489         m_contextMenu
->Append(wxID_REDO
, _("&Redo")); 
2490         m_contextMenu
->AppendSeparator(); 
2491         m_contextMenu
->Append(wxID_CUT
, _("Cu&t")); 
2492         m_contextMenu
->Append(wxID_COPY
, _("&Copy")); 
2493         m_contextMenu
->Append(wxID_PASTE
, _("&Paste")); 
2494         m_contextMenu
->Append(wxID_CLEAR
, _("&Delete")); 
2495         m_contextMenu
->AppendSeparator(); 
2496         m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All")); 
2498     PopupMenu(m_contextMenu
); 
2502 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
) 
2504     return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
); 
2507 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
) 
2509     return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
)); 
2512 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
) 
2514     return GetBuffer().SetStyle(range
.ToInternal(), style
); 
2517 // extended style setting operation with flags including: 
2518 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY. 
2519 // see richtextbuffer.h for more details. 
2520 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
) 
2522     return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
); 
2525 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
) 
2527     return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
); 
2530 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
) 
2532     return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
); 
2535 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
) 
2537     return GetBuffer().SetDefaultStyle(style
); 
2540 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
) 
2542     return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
)); 
2545 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const 
2547     return GetBuffer().GetDefaultStyle(); 
2550 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const 
2552     return GetBuffer().GetDefaultStyle(); 
2555 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
) 
2557     wxTextAttrEx 
attr(style
); 
2558     if (GetBuffer().GetStyle(position
, attr
)) 
2567 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
) 
2569     return GetBuffer().GetStyle(position
, style
); 
2572 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
) 
2574     return GetBuffer().GetStyle(position
, style
); 
2577 // get the common set of styles for the range 
2578 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
) 
2580     wxTextAttrEx styleEx
; 
2581     if (GetBuffer().GetStyleForRange(range
.ToInternal(), styleEx
)) 
2590 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
) 
2592     return GetBuffer().GetStyleForRange(range
.ToInternal(), style
); 
2595 /// Get the content (uncombined) attributes for this position. 
2597 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
) 
2599     wxTextAttrEx 
attr(style
); 
2600     if (GetBuffer().GetUncombinedStyle(position
, attr
)) 
2609 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
) 
2611     return GetBuffer().GetUncombinedStyle(position
, style
); 
2614 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
) 
2616     return GetBuffer().GetUncombinedStyle(position
, style
); 
2619 /// Set font, and also the buffer attributes 
2620 bool wxRichTextCtrl::SetFont(const wxFont
& font
) 
2622     wxControl::SetFont(font
); 
2624     wxTextAttrEx attr 
= GetBuffer().GetAttributes(); 
2626     GetBuffer().SetBasicStyle(attr
); 
2628     GetBuffer().Invalidate(wxRICHTEXT_ALL
); 
2634 /// Transform logical to physical 
2635 wxPoint 
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const 
2638     CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
); 
2643 /// Transform physical to logical 
2644 wxPoint 
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const 
2647     CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
); 
2652 /// Position the caret 
2653 void wxRichTextCtrl::PositionCaret() 
2658     //wxLogDebug(wxT("PositionCaret")); 
2661     if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
)) 
2663         wxPoint originalPt 
= caretRect
.GetPosition(); 
2664         wxPoint pt 
= GetPhysicalPoint(originalPt
); 
2665         if (GetCaret()->GetPosition() != pt
) 
2667             GetCaret()->Move(pt
); 
2668             GetCaret()->SetSize(caretRect
.GetSize()); 
2673 /// Get the caret height and position for the given character position 
2674 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
) 
2676     wxClientDC 
dc(this); 
2677     dc
.SetFont(GetFont()); 
2684     if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
)) 
2686         rect 
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
)); 
2693 /// Gets the line for the visible caret position. If the caret is 
2694 /// shown at the very end of the line, it means the next character is actually 
2695 /// on the following line. So let's get the line we're expecting to find 
2696 /// if this is the case. 
2697 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const 
2699     wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(caretPosition
, true); 
2700     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(caretPosition
, true); 
2703         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
2704         if (caretPosition 
== lineRange
.GetStart()-1 && 
2705             (para
->GetRange().GetStart() != lineRange
.GetStart())) 
2707             if (!m_caretAtLineStart
) 
2708                 line 
= GetBuffer().GetLineAtPosition(caretPosition
-1, true); 
2715 /// Move the caret to the given character position 
2716 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
) 
2718     if (GetBuffer().GetDirty()) 
2721     if (pos 
<= GetBuffer().GetRange().GetEnd()) 
2723         SetCaretPosition(pos
, showAtLineStart
); 
2733 /// Layout the buffer: which we must do before certain operations, such as 
2734 /// setting the caret position. 
2735 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
) 
2737     if (GetBuffer().GetDirty() || onlyVisibleRect
) 
2739         wxRect 
availableSpace(GetClientSize()); 
2740         if (availableSpace
.width 
== 0) 
2741             availableSpace
.width 
= 10; 
2742         if (availableSpace
.height 
== 0) 
2743             availableSpace
.height 
= 10; 
2745         int flags 
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
; 
2746         if (onlyVisibleRect
) 
2748             flags 
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
; 
2749             availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0))); 
2752         wxClientDC 
dc(this); 
2753         dc
.SetFont(GetFont()); 
2757         GetBuffer().Defragment(); 
2758         GetBuffer().UpdateRanges();     // If items were deleted, ranges need recalculation 
2759         GetBuffer().Layout(dc
, availableSpace
, flags
); 
2760         GetBuffer().SetDirty(false); 
2769 /// Is all of the selection bold? 
2770 bool wxRichTextCtrl::IsSelectionBold() 
2774         wxRichTextAttr attr
; 
2775         wxRichTextRange range 
= GetSelectionRange(); 
2776         attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
); 
2777         attr
.SetFontWeight(wxBOLD
); 
2779         return HasCharacterAttributes(range
, attr
); 
2783         // If no selection, then we need to combine current style with default style 
2784         // to see what the effect would be if we started typing. 
2785         wxRichTextAttr attr
; 
2786         attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
); 
2788         long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
2789         if (GetStyle(pos
, attr
)) 
2791             if (IsDefaultStyleShowing()) 
2792                 wxRichTextApplyStyle(attr
, GetDefaultStyleEx()); 
2793             return attr
.GetFontWeight() == wxBOLD
; 
2799 /// Is all of the selection italics? 
2800 bool wxRichTextCtrl::IsSelectionItalics() 
2804         wxRichTextRange range 
= GetSelectionRange(); 
2805         wxRichTextAttr attr
; 
2806         attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
); 
2807         attr
.SetFontStyle(wxITALIC
); 
2809         return HasCharacterAttributes(range
, attr
); 
2813         // If no selection, then we need to combine current style with default style 
2814         // to see what the effect would be if we started typing. 
2815         wxRichTextAttr attr
; 
2816         attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
); 
2818         long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
2819         if (GetStyle(pos
, attr
)) 
2821             if (IsDefaultStyleShowing()) 
2822                 wxRichTextApplyStyle(attr
, GetDefaultStyleEx()); 
2823             return attr
.GetFontStyle() == wxITALIC
; 
2829 /// Is all of the selection underlined? 
2830 bool wxRichTextCtrl::IsSelectionUnderlined() 
2834         wxRichTextRange range 
= GetSelectionRange(); 
2835         wxRichTextAttr attr
; 
2836         attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
); 
2837         attr
.SetFontUnderlined(true); 
2839         return HasCharacterAttributes(range
, attr
); 
2843         // If no selection, then we need to combine current style with default style 
2844         // to see what the effect would be if we started typing. 
2845         wxRichTextAttr attr
; 
2846         attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
); 
2847         long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
2849         if (GetStyle(pos
, attr
)) 
2851             if (IsDefaultStyleShowing()) 
2852                 wxRichTextApplyStyle(attr
, GetDefaultStyleEx()); 
2853             return attr
.GetFontUnderlined(); 
2859 /// Apply bold to the selection 
2860 bool wxRichTextCtrl::ApplyBoldToSelection() 
2862     wxRichTextAttr attr
; 
2863     attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
); 
2864     attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL 
: wxBOLD
); 
2867         return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
); 
2869         SetAndShowDefaultStyle(attr
); 
2873 /// Apply italic to the selection 
2874 bool wxRichTextCtrl::ApplyItalicToSelection() 
2876     wxRichTextAttr attr
; 
2877     attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
); 
2878     attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL 
: wxITALIC
); 
2881         return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
); 
2883         SetAndShowDefaultStyle(attr
); 
2887 /// Apply underline to the selection 
2888 bool wxRichTextCtrl::ApplyUnderlineToSelection() 
2890     wxRichTextAttr attr
; 
2891     attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
); 
2892     attr
.SetFontUnderlined(!IsSelectionUnderlined()); 
2895         return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
); 
2897         SetAndShowDefaultStyle(attr
); 
2901 /// Is all of the selection aligned according to the specified flag? 
2902 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) 
2904     wxRichTextRange range
; 
2906         range 
= GetSelectionRange(); 
2908         range 
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2); 
2910     wxRichTextAttr attr
; 
2911     attr
.SetAlignment(alignment
); 
2913     return HasParagraphAttributes(range
, attr
); 
2916 /// Apply alignment to the selection 
2917 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
) 
2919     wxRichTextAttr attr
; 
2920     attr
.SetAlignment(alignment
); 
2922         return SetStyle(GetSelectionRange(), attr
); 
2925         wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1); 
2927             return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
); 
2932 /// Apply a named style to the selection 
2933 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
) 
2935     // Flags are defined within each definition, so only certain 
2936     // attributes are applied. 
2937     wxRichTextAttr 
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle()); 
2939     int flags 
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
; 
2941     if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
))) 
2943         flags 
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
; 
2945         wxRichTextRange range
; 
2948             range 
= GetSelectionRange(); 
2951             long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
2952             range 
= wxRichTextRange(pos
, pos
+1); 
2955         return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
); 
2958     // Make sure the attr has the style name 
2959     if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
))) 
2961         attr
.SetParagraphStyleName(def
->GetName()); 
2963         // If applying a paragraph style, we only want the paragraph nodes to adopt these 
2964         // attributes, and not the leaf nodes. This will allow the content (e.g. text) 
2965         // to change its style independently. 
2966         flags 
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
; 
2969         attr
.SetCharacterStyleName(def
->GetName()); 
2972         return SetStyleEx(GetSelectionRange(), attr
, flags
); 
2975         SetAndShowDefaultStyle(attr
); 
2980 /// Apply the style sheet to the buffer, for example if the styles have changed. 
2981 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
) 
2984         styleSheet 
= GetBuffer().GetStyleSheet(); 
2988     if (GetBuffer().ApplyStyleSheet(styleSheet
)) 
2990         GetBuffer().Invalidate(wxRICHTEXT_ALL
); 
2998 /// Sets the default style to the style under the cursor 
2999 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() 
3002     attr
.SetFlags(wxTEXT_ATTR_CHARACTER
); 
3004     // If at the start of a paragraph, use the next position. 
3005     long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
3007     if (GetUncombinedStyle(pos
, attr
)) 
3009         SetDefaultStyle(attr
); 
3016 /// Returns the first visible position in the current view 
3017 long wxRichTextCtrl::GetFirstVisiblePosition() const 
3019     wxRichTextLine
* line 
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
); 
3021         return line
->GetAbsoluteRange().GetStart(); 
3026 /// Get the first visible point in the window 
3027 wxPoint 
wxRichTextCtrl::GetFirstVisiblePoint() const 
3030     int startXUnits
, startYUnits
; 
3032     GetScrollPixelsPerUnit(& ppuX
, & ppuY
); 
3033     GetViewStart(& startXUnits
, & startYUnits
); 
3035     return wxPoint(startXUnits 
* ppuX
, startYUnits 
* ppuY
); 
3038 /// The adjusted caret position is the character position adjusted to take 
3039 /// into account whether we're at the start of a paragraph, in which case 
3040 /// style information should be taken from the next position, not current one. 
3041 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const 
3043     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(caretPos
+1); 
3045     if (para 
&& (caretPos
+1 == para
->GetRange().GetStart())) 
3050 /// Get/set the selection range in character positions. -1, -1 means no selection. 
3051 /// The range is in API convention, i.e. a single character selection is denoted 
3053 wxRichTextRange 
wxRichTextCtrl::GetSelectionRange() const 
3055     wxRichTextRange range 
= GetInternalSelectionRange(); 
3056     if (range 
!= wxRichTextRange(-2,-2) && range 
!= wxRichTextRange(-1,-1)) 
3057         range
.SetEnd(range
.GetEnd() + 1); 
3061 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
) 
3063     wxRichTextRange 
range1(range
); 
3064     if (range1 
!= wxRichTextRange(-2,-2) && range1 
!= wxRichTextRange(-1,-1) ) 
3065         range1
.SetEnd(range1
.GetEnd() - 1); 
3067     wxASSERT( range1
.GetStart() > range1
.GetEnd() ); 
3069     SetInternalSelectionRange(range1
); 
3073 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
3075     return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
); 
3078 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
) 
3080     return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
); 
3083 /// Clear list for given range 
3084 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
) 
3086     return GetBuffer().ClearListStyle(range
.ToInternal(), flags
); 
3089 /// Number/renumber any list elements in the given range 
3090 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
3092     return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
); 
3095 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
) 
3097     return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
); 
3100 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1 
3101 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
) 
3103     return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
); 
3106 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
) 
3108     return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
); 
3111 /// Deletes the content in the given range 
3112 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
) 
3114     return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this); 
3117 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames() 
3119     if (sm_availableFontNames
.GetCount() == 0) 
3121         sm_availableFontNames 
= wxFontEnumerator::GetFacenames(); 
3122         sm_availableFontNames
.Sort(); 
3124     return sm_availableFontNames
; 
3127 void wxRichTextCtrl::ClearAvailableFontNames() 
3129     sm_availableFontNames
.Clear();