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" 
  37 // DLL options compatibility check: 
  39 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl") 
  41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
) 
  42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
) 
  43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
) 
  44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
) 
  45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
) 
  46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER
) 
  47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE
) 
  49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
) 
  50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
) 
  51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
) 
  52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
) 
  54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
) 
  55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
) 
  56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
) 
  57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
) 
  58 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
) 
  60 #if wxRICHTEXT_USE_OWN_CARET 
  65  * This implements a non-flashing cursor in case there 
  66  * are platform-specific problems with the generic caret. 
  67  * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h. 
  70 class wxRichTextCaret
: public wxCaret
 
  75         // default - use Create() 
  76     wxRichTextCaret() { Init(); } 
  77         // creates a block caret associated with the given window 
  78     wxRichTextCaret(wxRichTextCtrl 
*window
, int width
, int height
) 
  79         : wxCaret(window
, width
, height
) { Init(); m_richTextCtrl 
= window
; } 
  80     wxRichTextCaret(wxRichTextCtrl 
*window
, const wxSize
& size
) 
  81         : wxCaret(window
, size
) { Init(); m_richTextCtrl 
= window
; } 
  83     virtual ~wxRichTextCaret(); 
  88     // called by wxWindow (not using the event tables) 
  89     virtual void OnSetFocus(); 
  90     virtual void OnKillFocus(); 
  92     // draw the caret on the given DC 
  93     void DoDraw(wxDC 
*dc
); 
  95     // get the visible count 
  96     int GetVisibleCount() const { return m_countVisible
; } 
  98     // delay repositioning 
  99     bool GetNeedsUpdate() const { return m_needsUpdate
; } 
 100     void SetNeedsUpdate(bool needsUpdate 
= true ) { m_needsUpdate 
= needsUpdate
; } 
 103     virtual void DoShow(); 
 104     virtual void DoHide(); 
 105     virtual void DoMove(); 
 106     virtual void DoSize(); 
 116     bool          m_hasFocus
;       // true => our window has focus 
 117     bool          m_needsUpdate
;    // must be repositioned 
 119     wxRichTextCtrl
* m_richTextCtrl
; 
 123 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxControl 
) 
 125 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent 
) 
 127 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl 
) 
 128     EVT_PAINT(wxRichTextCtrl::OnPaint
) 
 129     EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
) 
 130     EVT_IDLE(wxRichTextCtrl::OnIdle
) 
 131     EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
) 
 132     EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
) 
 133     EVT_MOTION(wxRichTextCtrl::OnMoveMouse
) 
 134     EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
) 
 135     EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
) 
 136     EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
) 
 137     EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
) 
 138     EVT_CHAR(wxRichTextCtrl::OnChar
) 
 139     EVT_KEY_DOWN(wxRichTextCtrl::OnChar
) 
 140     EVT_SIZE(wxRichTextCtrl::OnSize
) 
 141     EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
) 
 142     EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
) 
 143     EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
) 
 144     EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
) 
 145     EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
) 
 147     EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
) 
 148     EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
) 
 150     EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
) 
 151     EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
) 
 153     EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
) 
 154     EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
) 
 156     EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
) 
 157     EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
) 
 159     EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
) 
 160     EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
) 
 162     EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
) 
 163     EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
) 
 165     EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
) 
 166     EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
) 
 173 wxArrayString 
wxRichTextCtrl::sm_availableFontNames
; 
 175 wxRichTextCtrl::wxRichTextCtrl() 
 176               : wxScrollHelper(this) 
 181 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
, 
 183                                const wxString
& value
, 
 187                                const wxValidator
& validator
, 
 188                                const wxString
& name
) 
 189               : wxScrollHelper(this) 
 192     Create(parent
, id
, value
, pos
, size
, style
, validator
, name
); 
 196 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
, 
 197                              const wxValidator
& validator
, const wxString
& name
) 
 199     if (!wxControl::Create(parent
, id
, pos
, size
, 
 200                            style
|wxFULL_REPAINT_ON_RESIZE
, 
 206         SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)); 
 209     if (style 
& wxTE_READONLY
) 
 212     // The base attributes must all have default values 
 213     wxTextAttr attributes
; 
 214     attributes
.SetFont(GetFont()); 
 215     attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
)); 
 216     attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
); 
 217     attributes
.SetLineSpacing(10); 
 218     attributes
.SetParagraphSpacingAfter(10); 
 219     attributes
.SetParagraphSpacingBefore(0); 
 221     SetBasicStyle(attributes
); 
 223     // The default attributes will be merged with base attributes, so 
 224     // can be empty to begin with 
 225     wxTextAttr defaultAttributes
; 
 226     SetDefaultStyle(defaultAttributes
); 
 228     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
)); 
 229     SetBackgroundStyle(wxBG_STYLE_CUSTOM
); 
 232     GetBuffer().SetRichTextCtrl(this); 
 234 #if wxRICHTEXT_USE_OWN_CARET 
 235     SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16)); 
 237     SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16)); 
 240     // Tell the sizers to use the given or best size 
 241     SetInitialSize(size
); 
 243 #if wxRICHTEXT_BUFFERED_PAINTING 
 245     RecreateBuffer(size
); 
 248     m_textCursor 
= wxCursor(wxCURSOR_IBEAM
); 
 249     m_urlCursor 
= wxCursor(wxCURSOR_HAND
); 
 251     SetCursor(m_textCursor
); 
 253     if (!value
.IsEmpty()) 
 256     GetBuffer().AddEventHandler(this); 
 259     wxAcceleratorEntry entries
[6]; 
 261     entries
[0].Set(wxACCEL_CMD
,   (int) 'C',       wxID_COPY
); 
 262     entries
[1].Set(wxACCEL_CMD
,   (int) 'X',       wxID_CUT
); 
 263     entries
[2].Set(wxACCEL_CMD
,   (int) 'V',       wxID_PASTE
); 
 264     entries
[3].Set(wxACCEL_CMD
,   (int) 'A',       wxID_SELECTALL
); 
 265     entries
[4].Set(wxACCEL_CMD
,   (int) 'Z',       wxID_UNDO
); 
 266     entries
[5].Set(wxACCEL_CMD
,   (int) 'Y',       wxID_REDO
); 
 268     wxAcceleratorTable 
accel(6, entries
); 
 269     SetAcceleratorTable(accel
); 
 274 wxRichTextCtrl::~wxRichTextCtrl() 
 276     GetBuffer().RemoveEventHandler(this); 
 278     delete m_contextMenu
; 
 281 /// Member initialisation 
 282 void wxRichTextCtrl::Init() 
 284     m_contextMenu 
= NULL
; 
 286     m_caretPosition 
= -1; 
 287     m_selectionRange
.SetRange(-2, -2); 
 288     m_selectionAnchor 
= -2; 
 290     m_caretAtLineStart 
= false; 
 292     m_fullLayoutRequired 
= false; 
 293     m_fullLayoutTime 
= 0; 
 294     m_fullLayoutSavedPosition 
= 0; 
 295     m_delayedLayoutThreshold 
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
; 
 296     m_caretPositionForDefaultStyle 
= -2; 
 299 void wxRichTextCtrl::DoThaw() 
 301     if (GetBuffer().GetDirty()) 
 310 void wxRichTextCtrl::Clear() 
 312     m_buffer
.ResetAndClearCommands(); 
 313     m_buffer
.SetDirty(true); 
 314     m_caretPosition 
= -1; 
 315     m_caretPositionForDefaultStyle 
= -2; 
 316     m_caretAtLineStart 
= false; 
 317     m_selectionRange
.SetRange(-2, -2); 
 327     wxTextCtrl::SendTextUpdatedEvent(this); 
 331 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
)) 
 333 #if !wxRICHTEXT_USE_OWN_CARET 
 334     if (GetCaret() && !IsFrozen()) 
 339 #if wxRICHTEXT_BUFFERED_PAINTING 
 340         wxBufferedPaintDC 
dc(this, m_bufferBitmap
); 
 350         dc
.SetFont(GetFont()); 
 352         // Paint the background 
 355         // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize()); 
 357         wxRect 
drawingArea(GetUpdateRegion().GetBox()); 
 358         drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition())); 
 360         wxRect 
availableSpace(GetClientSize()); 
 361         if (GetBuffer().GetDirty()) 
 363             GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
); 
 364             GetBuffer().SetDirty(false); 
 368         GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */); 
 369 #if wxRICHTEXT_USE_OWN_CARET 
 370         if (GetCaret()->IsVisible()) 
 372             ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
); 
 377 #if !wxRICHTEXT_USE_OWN_CARET 
 384 // Empty implementation, to prevent flicker 
 385 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
)) 
 389 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
)) 
 393 #if !wxRICHTEXT_USE_OWN_CARET 
 399 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET 
 400     // Work around dropouts when control is focused 
 408 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) 
 413 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET 
 414     // Work around dropouts when control is focused 
 422 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
)) 
 428 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
) 
 434     dc
.SetFont(GetFont()); 
 437     int hit 
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
); 
 439     if (hit 
!= wxRICHTEXT_HITTEST_NONE
) 
 441         m_dragStart 
= event
.GetLogicalPosition(dc
); 
 445         bool caretAtLineStart 
= false; 
 447         if (hit 
& wxRICHTEXT_HITTEST_BEFORE
) 
 449             // If we're at the start of a line (but not first in para) 
 450             // then we should keep the caret showing at the start of the line 
 451             // by showing the m_caretAtLineStart flag. 
 452             wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(position
); 
 453             wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(position
); 
 455             if (line 
&& para 
&& line
->GetAbsoluteRange().GetStart() == position 
&& para
->GetRange().GetStart() != position
) 
 456                 caretAtLineStart 
= true; 
 460         long oldCaretPos 
= m_caretPosition
; 
 462         MoveCaret(position
, caretAtLineStart
); 
 463         SetDefaultStyleToCursorStyle(); 
 465         if (event
.ShiftDown()) 
 467             if (m_selectionRange
.GetStart() == -2) 
 468                 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
); 
 470                 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
); 
 480 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
) 
 485         if (GetCapture() == this) 
 488         // See if we clicked on a URL 
 491         dc
.SetFont(GetFont()); 
 494         wxPoint logicalPt 
= event
.GetLogicalPosition(dc
); 
 495         int hit 
= GetBuffer().HitTest(dc
, logicalPt
, position
); 
 497         if ((hit 
!= wxRICHTEXT_HITTEST_NONE
) && !(hit 
& wxRICHTEXT_HITTEST_OUTSIDE
)) 
 499             wxRichTextEvent 
cmdEvent( 
 500                 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, 
 502             cmdEvent
.SetEventObject(this); 
 503             cmdEvent
.SetPosition(m_caretPosition
+1); 
 505             if (!GetEventHandler()->ProcessEvent(cmdEvent
)) 
 508                 if (GetStyle(position
, attr
)) 
 510                     if (attr
.HasFlag(wxTEXT_ATTR_URL
)) 
 512                         wxString urlTarget 
= attr
.GetURL(); 
 513                         if (!urlTarget
.IsEmpty()) 
 515                             wxMouseEvent 
mouseEvent(event
); 
 517                             long startPos 
= 0, endPos 
= 0; 
 518                             wxRichTextObject
* obj 
= GetBuffer().GetLeafObjectAtPosition(position
); 
 521                                 startPos 
= obj
->GetRange().GetStart(); 
 522                                 endPos 
= obj
->GetRange().GetEnd(); 
 525                             wxTextUrlEvent 
urlEvent(GetId(), mouseEvent
, startPos
, endPos
); 
 526                             InitCommandEvent(urlEvent
); 
 528                             urlEvent
.SetString(urlTarget
); 
 530                             GetEventHandler()->ProcessEvent(urlEvent
); 
 540 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
) 
 544     dc
.SetFont(GetFont()); 
 547     wxPoint logicalPt 
= event
.GetLogicalPosition(dc
); 
 548     int hit 
= GetBuffer().HitTest(dc
, logicalPt
, position
); 
 550     // See if we need to change the cursor 
 553         if (hit 
!= wxRICHTEXT_HITTEST_NONE 
&& !(hit 
& wxRICHTEXT_HITTEST_OUTSIDE
)) 
 556             if (GetStyle(position
, attr
)) 
 558                 if (attr
.HasFlag(wxTEXT_ATTR_URL
)) 
 560                     SetCursor(m_urlCursor
); 
 562                 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
)) 
 564                     SetCursor(m_textCursor
); 
 569             SetCursor(m_textCursor
); 
 572     if (!event
.Dragging()) 
 578     if (m_dragging 
&& hit 
!= wxRICHTEXT_HITTEST_NONE
) 
 580         // TODO: test closeness 
 582         bool caretAtLineStart 
= false; 
 584         if (hit 
& wxRICHTEXT_HITTEST_BEFORE
) 
 586             // If we're at the start of a line (but not first in para) 
 587             // then we should keep the caret showing at the start of the line 
 588             // by showing the m_caretAtLineStart flag. 
 589             wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(position
); 
 590             wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(position
); 
 592             if (line 
&& para 
&& line
->GetAbsoluteRange().GetStart() == position 
&& para
->GetRange().GetStart() != position
) 
 593                 caretAtLineStart 
= true; 
 597         if (m_caretPosition 
!= position
) 
 599             ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
); 
 601             MoveCaret(position
, caretAtLineStart
); 
 602             SetDefaultStyleToCursorStyle(); 
 608 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
) 
 612     wxRichTextEvent 
cmdEvent( 
 613         wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, 
 615     cmdEvent
.SetEventObject(this); 
 616     cmdEvent
.SetPosition(m_caretPosition
+1); 
 618     if (!GetEventHandler()->ProcessEvent(cmdEvent
)) 
 622 /// Left-double-click 
 623 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
)) 
 625     wxRichTextEvent 
cmdEvent( 
 626         wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, 
 628     cmdEvent
.SetEventObject(this); 
 629     cmdEvent
.SetPosition(m_caretPosition
+1); 
 631     if (!GetEventHandler()->ProcessEvent(cmdEvent
)) 
 633         SelectWord(GetCaretPosition()+1); 
 638 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
) 
 640     wxRichTextEvent 
cmdEvent( 
 641         wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, 
 643     cmdEvent
.SetEventObject(this); 
 644     cmdEvent
.SetPosition(m_caretPosition
+1); 
 646     if (!GetEventHandler()->ProcessEvent(cmdEvent
)) 
 651 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
) 
 655         flags 
|= wxRICHTEXT_CTRL_DOWN
; 
 656     if (event
.ShiftDown()) 
 657         flags 
|= wxRICHTEXT_SHIFT_DOWN
; 
 659         flags 
|= wxRICHTEXT_ALT_DOWN
; 
 661     if (event
.GetEventType() == wxEVT_KEY_DOWN
) 
 663         if (event
.GetKeyCode() == WXK_LEFT 
|| 
 664             event
.GetKeyCode() == WXK_RIGHT 
|| 
 665             event
.GetKeyCode() == WXK_UP 
|| 
 666             event
.GetKeyCode() == WXK_DOWN 
|| 
 667             event
.GetKeyCode() == WXK_HOME 
|| 
 668             event
.GetKeyCode() == WXK_PAGEUP 
|| 
 669             event
.GetKeyCode() == WXK_PAGEDOWN 
|| 
 670             event
.GetKeyCode() == WXK_END 
|| 
 672             event
.GetKeyCode() == WXK_NUMPAD_LEFT 
|| 
 673             event
.GetKeyCode() == WXK_NUMPAD_RIGHT 
|| 
 674             event
.GetKeyCode() == WXK_NUMPAD_UP 
|| 
 675             event
.GetKeyCode() == WXK_NUMPAD_DOWN 
|| 
 676             event
.GetKeyCode() == WXK_NUMPAD_HOME 
|| 
 677             event
.GetKeyCode() == WXK_NUMPAD_PAGEUP 
|| 
 678             event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN 
|| 
 679             event
.GetKeyCode() == WXK_NUMPAD_END
) 
 681             KeyboardNavigate(event
.GetKeyCode(), flags
); 
 685         long keycode 
= event
.GetKeyCode(); 
 745             case WXK_NUMPAD_HOME
: 
 746             case WXK_NUMPAD_LEFT
: 
 748             case WXK_NUMPAD_RIGHT
: 
 749             case WXK_NUMPAD_DOWN
: 
 750             case WXK_NUMPAD_PAGEUP
: 
 751             case WXK_NUMPAD_PAGEDOWN
: 
 753             case WXK_NUMPAD_BEGIN
: 
 754             case WXK_NUMPAD_INSERT
: 
 755             case WXK_NUMPAD_DELETE
: 
 756             case WXK_WINDOWS_LEFT
: 
 765         // Must process this before translation, otherwise it's translated into a WXK_DELETE event. 
 766         if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
) 
 768             BeginBatchUndo(_("Delete Text")); 
 770             long newPos 
= m_caretPosition
; 
 772             bool processed 
= DeleteSelectedContent(& newPos
); 
 774             // Submit range in character positions, which are greater than caret positions, 
 775             // so subtract 1 for deleted character and add 1 for conversion to character position. 
 780                     long pos 
= wxRichTextCtrl::FindNextWordPosition(-1); 
 783                         GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this); 
 789                     GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this); 
 794             if (GetLastPosition() == -1) 
 798                 m_caretPosition 
= -1; 
 800                 SetDefaultStyleToCursorStyle(); 
 803             ScrollIntoView(m_caretPosition
, WXK_LEFT
); 
 805             wxRichTextEvent 
cmdEvent( 
 806                 wxEVT_COMMAND_RICHTEXT_DELETE
, 
 808             cmdEvent
.SetEventObject(this); 
 809             cmdEvent
.SetFlags(flags
); 
 810             cmdEvent
.SetPosition(m_caretPosition
+1); 
 811             GetEventHandler()->ProcessEvent(cmdEvent
); 
 821     // all the other keys modify the controls contents which shouldn't be 
 822     // possible if we're read-only 
 829     if (event
.GetKeyCode() == WXK_RETURN
) 
 831         BeginBatchUndo(_("Insert Text")); 
 833         long newPos 
= m_caretPosition
; 
 835         DeleteSelectedContent(& newPos
); 
 837         if (event
.ShiftDown()) 
 840             text 
= wxRichTextLineBreakChar
; 
 841             GetBuffer().InsertTextWithUndo(newPos
+1, text
, this); 
 842             m_caretAtLineStart 
= true; 
 846             GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
); 
 849         SetDefaultStyleToCursorStyle(); 
 851         ScrollIntoView(m_caretPosition
, WXK_RIGHT
); 
 853         wxRichTextEvent 
cmdEvent( 
 854             wxEVT_COMMAND_RICHTEXT_RETURN
, 
 856         cmdEvent
.SetEventObject(this); 
 857         cmdEvent
.SetFlags(flags
); 
 858         cmdEvent
.SetPosition(newPos
+1); 
 860         if (!GetEventHandler()->ProcessEvent(cmdEvent
)) 
 862             // Generate conventional event 
 863             wxCommandEvent 
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId()); 
 864             InitCommandEvent(textEvent
); 
 866             GetEventHandler()->ProcessEvent(textEvent
); 
 870     else if (event
.GetKeyCode() == WXK_BACK
) 
 872         BeginBatchUndo(_("Delete Text")); 
 874         long newPos 
= m_caretPosition
; 
 876         bool processed 
= DeleteSelectedContent(& newPos
); 
 878         // Submit range in character positions, which are greater than caret positions, 
 879         // so subtract 1 for deleted character and add 1 for conversion to character position. 
 884                 long pos 
= wxRichTextCtrl::FindNextWordPosition(-1); 
 887                     GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this); 
 893                 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this); 
 898         if (GetLastPosition() == -1) 
 902             m_caretPosition 
= -1; 
 904             SetDefaultStyleToCursorStyle(); 
 907         ScrollIntoView(m_caretPosition
, WXK_LEFT
); 
 909         wxRichTextEvent 
cmdEvent( 
 910             wxEVT_COMMAND_RICHTEXT_DELETE
, 
 912         cmdEvent
.SetEventObject(this); 
 913         cmdEvent
.SetFlags(flags
); 
 914         cmdEvent
.SetPosition(m_caretPosition
+1); 
 915         GetEventHandler()->ProcessEvent(cmdEvent
); 
 919     else if (event
.GetKeyCode() == WXK_DELETE
) 
 921         BeginBatchUndo(_("Delete Text")); 
 923         long newPos 
= m_caretPosition
; 
 925         bool processed 
= DeleteSelectedContent(& newPos
); 
 927         // Submit range in character positions, which are greater than caret positions, 
 928         if (newPos 
< GetBuffer().GetRange().GetEnd()+1) 
 932                 long pos 
= wxRichTextCtrl::FindNextWordPosition(1); 
 933                 if (pos 
!= -1 && (pos 
> newPos
)) 
 935                     GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this); 
 941                 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this); 
 946         if (GetLastPosition() == -1) 
 950             m_caretPosition 
= -1; 
 952             SetDefaultStyleToCursorStyle(); 
 955         wxRichTextEvent 
cmdEvent( 
 956             wxEVT_COMMAND_RICHTEXT_DELETE
, 
 958         cmdEvent
.SetEventObject(this); 
 959         cmdEvent
.SetFlags(flags
); 
 960         cmdEvent
.SetPosition(m_caretPosition
+1); 
 961         GetEventHandler()->ProcessEvent(cmdEvent
); 
 967         long keycode 
= event
.GetKeyCode(); 
 981                 if (event
.CmdDown() || event
.AltDown()) 
 988                 wxRichTextEvent 
cmdEvent( 
 989                     wxEVT_COMMAND_RICHTEXT_CHARACTER
, 
 991                 cmdEvent
.SetEventObject(this); 
 992                 cmdEvent
.SetFlags(flags
); 
 994                 cmdEvent
.SetCharacter(event
.GetUnicodeKey()); 
 996                 cmdEvent
.SetCharacter((wxChar
) keycode
); 
 998                 cmdEvent
.SetPosition(m_caretPosition
+1); 
1000                 if (keycode 
== wxT('\t')) 
1002                     // See if we need to promote or demote the selection or paragraph at the cursor 
1003                     // position, instead of inserting a tab. 
1004                     long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
1005                     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(pos
); 
1006                     if (para 
&& para
->GetRange().GetStart() == pos 
&& para
->GetAttributes().HasListStyleName()) 
1008                         wxRichTextRange range
; 
1010                             range 
= GetSelectionRange(); 
1012                             range 
= para
->GetRange().FromInternal(); 
1014                         int promoteBy 
= event
.ShiftDown() ? 1 : -1; 
1016                         PromoteList(promoteBy
, range
, NULL
); 
1018                         GetEventHandler()->ProcessEvent(cmdEvent
); 
1024                 BeginBatchUndo(_("Insert Text")); 
1026                 long newPos 
= m_caretPosition
; 
1027                 DeleteSelectedContent(& newPos
); 
1030                 wxString str 
= event
.GetUnicodeKey(); 
1032                 wxString str 
= (wxChar
) event
.GetKeyCode(); 
1034                 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0); 
1038                 SetDefaultStyleToCursorStyle(); 
1039                 ScrollIntoView(m_caretPosition
, WXK_RIGHT
); 
1041                 GetEventHandler()->ProcessEvent(cmdEvent
); 
1049 /// Delete content if there is a selection, e.g. when pressing a key. 
1050 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
) 
1054         long pos 
= m_selectionRange
.GetStart(); 
1055         GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this); 
1056         m_selectionRange
.SetRange(-2, -2); 
1066 /// Keyboard navigation 
1070 Left:       left one character 
1071 Right:      right one character 
1074 Ctrl-Left:  left one word 
1075 Ctrl-Right: right one word 
1076 Ctrl-Up:    previous paragraph start 
1077 Ctrl-Down:  next start of paragraph 
1080 Ctrl-Home:  start of document 
1081 Ctrl-End:   end of document 
1082 Page-Up:    Up a screen 
1083 Page-Down:  Down a screen 
1087 Ctrl-Alt-PgUp: Start of window 
1088 Ctrl-Alt-PgDn: End of window 
1089 F8:         Start selection mode 
1090 Esc:        End selection mode 
1092 Adding Shift does the above but starts/extends selection. 
1097 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
) 
1099     bool success 
= false; 
1101     if (keyCode 
== WXK_RIGHT 
|| keyCode 
== WXK_NUMPAD_RIGHT
) 
1103         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
1104             success 
= WordRight(1, flags
); 
1106             success 
= MoveRight(1, flags
); 
1108     else if (keyCode 
== WXK_LEFT 
|| keyCode 
== WXK_NUMPAD_LEFT
) 
1110         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
1111             success 
= WordLeft(1, flags
); 
1113             success 
= MoveLeft(1, flags
); 
1115     else if (keyCode 
== WXK_UP 
|| keyCode 
== WXK_NUMPAD_UP
) 
1117         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
1118             success 
= MoveToParagraphStart(flags
); 
1120             success 
= MoveUp(1, flags
); 
1122     else if (keyCode 
== WXK_DOWN 
|| keyCode 
== WXK_NUMPAD_DOWN
) 
1124         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
1125             success 
= MoveToParagraphEnd(flags
); 
1127             success 
= MoveDown(1, flags
); 
1129     else if (keyCode 
== WXK_PAGEUP 
|| keyCode 
== WXK_NUMPAD_PAGEUP
) 
1131         success 
= PageUp(1, flags
); 
1133     else if (keyCode 
== WXK_PAGEDOWN 
|| keyCode 
== WXK_NUMPAD_PAGEDOWN
) 
1135         success 
= PageDown(1, flags
); 
1137     else if (keyCode 
== WXK_HOME 
|| keyCode 
== WXK_NUMPAD_HOME
) 
1139         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
1140             success 
= MoveHome(flags
); 
1142             success 
= MoveToLineStart(flags
); 
1144     else if (keyCode 
== WXK_END 
|| keyCode 
== WXK_NUMPAD_END
) 
1146         if (flags 
& wxRICHTEXT_CTRL_DOWN
) 
1147             success 
= MoveEnd(flags
); 
1149             success 
= MoveToLineEnd(flags
); 
1154         ScrollIntoView(m_caretPosition
, keyCode
); 
1155         SetDefaultStyleToCursorStyle(); 
1161 /// Extend the selection. Selections are in caret positions. 
1162 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
) 
1164     if (flags 
& wxRICHTEXT_SHIFT_DOWN
) 
1166         if (oldPos 
== newPos
) 
1169         wxRichTextRange oldSelection 
= m_selectionRange
; 
1171         // If not currently selecting, start selecting 
1172         if (m_selectionRange
.GetStart() == -2) 
1174             m_selectionAnchor 
= oldPos
; 
1176             if (oldPos 
> newPos
) 
1177                 m_selectionRange
.SetRange(newPos
+1, oldPos
); 
1179                 m_selectionRange
.SetRange(oldPos
+1, newPos
); 
1183             // Always ensure that the selection range start is greater than 
1185             if (newPos 
> m_selectionAnchor
) 
1186                 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
); 
1187             else if (newPos 
== m_selectionAnchor
) 
1188                 m_selectionRange 
= wxRichTextRange(-2, -2); 
1190                 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
); 
1193         RefreshForSelectionChange(oldSelection
, m_selectionRange
); 
1195         if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd()) 
1197             wxLogDebug(wxT("Strange selection range")); 
1206 /// Scroll into view, returning true if we scrolled. 
1207 /// This takes a _caret_ position. 
1208 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
) 
1210     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(position
); 
1216     GetScrollPixelsPerUnit(& ppuX
, & ppuY
); 
1218     int startXUnits
, startYUnits
; 
1219     GetViewStart(& startXUnits
, & startYUnits
); 
1220     int startY 
= startYUnits 
* ppuY
; 
1223     GetVirtualSize(& sx
, & sy
); 
1229     wxRect rect 
= line
->GetRect(); 
1231     bool scrolled 
= false; 
1233     wxSize clientSize 
= GetClientSize(); 
1236     if (keyCode 
== WXK_DOWN 
|| keyCode 
== WXK_NUMPAD_DOWN 
|| 
1237         keyCode 
== WXK_RIGHT 
|| keyCode 
== WXK_NUMPAD_RIGHT 
|| 
1238         keyCode 
== WXK_END 
|| keyCode 
== WXK_NUMPAD_END 
|| 
1239         keyCode 
== WXK_PAGEDOWN 
|| keyCode 
== WXK_NUMPAD_PAGEDOWN
) 
1241         if ((rect
.y 
+ rect
.height
) > (clientSize
.y 
+ startY
)) 
1243             // Make it scroll so this item is at the bottom 
1245             int y 
= rect
.y 
- (clientSize
.y 
- rect
.height
); 
1246             int yUnits 
= (int) (0.5 + ((float) y
)/(float) ppuY
); 
1248             // If we're still off the screen, scroll another line down 
1249             if ((rect
.y 
+ rect
.height
) > (clientSize
.y 
+ (yUnits
*ppuY
))) 
1252             if (startYUnits 
!= yUnits
) 
1254                 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
); 
1258         else if (rect
.y 
< startY
) 
1260             // Make it scroll so this item is at the top 
1263             int yUnits 
= (int) (0.5 + ((float) y
)/(float) ppuY
); 
1265             if (startYUnits 
!= yUnits
) 
1267                 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
); 
1273     else if (keyCode 
== WXK_UP  
|| keyCode 
== WXK_NUMPAD_UP 
|| 
1274              keyCode 
== WXK_LEFT 
|| keyCode 
== WXK_NUMPAD_LEFT 
|| 
1275              keyCode 
== WXK_HOME 
|| keyCode 
== WXK_NUMPAD_HOME 
|| 
1276              keyCode 
== WXK_PAGEUP 
|| keyCode 
== WXK_NUMPAD_PAGEUP 
) 
1278         if (rect
.y 
< startY
) 
1280             // Make it scroll so this item is at the top 
1283             int yUnits 
= (int) (0.5 + ((float) y
)/(float) ppuY
); 
1285             if (startYUnits 
!= yUnits
) 
1287                 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
); 
1291         else if ((rect
.y 
+ rect
.height
) > (clientSize
.y 
+ startY
)) 
1293             // Make it scroll so this item is at the bottom 
1295             int y 
= rect
.y 
- (clientSize
.y 
- rect
.height
); 
1296             int yUnits 
= (int) (0.5 + ((float) y
)/(float) ppuY
); 
1298             // If we're still off the screen, scroll another line down 
1299             if ((rect
.y 
+ rect
.height
) > (clientSize
.y 
+ (yUnits
*ppuY
))) 
1302             if (startYUnits 
!= yUnits
) 
1304                 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
); 
1310 #if !wxRICHTEXT_USE_OWN_CARET 
1318 /// Is the given position visible on the screen? 
1319 bool wxRichTextCtrl::IsPositionVisible(long pos
) const 
1321     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(pos
-1); 
1327     GetScrollPixelsPerUnit(& ppuX
, & ppuY
); 
1330     GetViewStart(& startX
, & startY
); 
1332     startY 
= startY 
* ppuY
; 
1334     wxRect rect 
= line
->GetRect(); 
1335     wxSize clientSize 
= GetClientSize(); 
1337     return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY 
+ clientSize
.y
)); 
1340 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
) 
1342     m_caretPosition 
= position
; 
1343     m_caretAtLineStart 
= showAtLineStart
; 
1346 /// Move caret one visual step forward: this may mean setting a flag 
1347 /// and keeping the same position if we're going from the end of one line 
1348 /// to the start of the next, which may be the exact same caret position. 
1349 void wxRichTextCtrl::MoveCaretForward(long oldPosition
) 
1351     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(oldPosition
); 
1353     // Only do the check if we're not at the end of the paragraph (where things work OK 
1355     if (para 
&& (oldPosition 
!= para
->GetRange().GetEnd() - 1)) 
1357         wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(oldPosition
); 
1361             wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1363             // We're at the end of a line. See whether we need to 
1364             // stay at the same actual caret position but change visual 
1365             // position, or not. 
1366             if (oldPosition 
== lineRange
.GetEnd()) 
1368                 if (m_caretAtLineStart
) 
1370                     // We're already at the start of the line, so actually move on now. 
1371                     m_caretPosition 
= oldPosition 
+ 1; 
1372                     m_caretAtLineStart 
= false; 
1376                     // We're showing at the end of the line, so keep to 
1377                     // the same position but indicate that we're to show 
1378                     // at the start of the next line. 
1379                     m_caretPosition 
= oldPosition
; 
1380                     m_caretAtLineStart 
= true; 
1382                 SetDefaultStyleToCursorStyle(); 
1388     SetDefaultStyleToCursorStyle(); 
1391 /// Move caret one visual step backward: this may mean setting a flag 
1392 /// and keeping the same position if we're going from the end of one line 
1393 /// to the start of the next, which may be the exact same caret position. 
1394 void wxRichTextCtrl::MoveCaretBack(long oldPosition
) 
1396     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(oldPosition
); 
1398     // Only do the check if we're not at the start of the paragraph (where things work OK 
1400     if (para 
&& (oldPosition 
!= para
->GetRange().GetStart())) 
1402         wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(oldPosition
); 
1406             wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1408             // We're at the start of a line. See whether we need to 
1409             // stay at the same actual caret position but change visual 
1410             // position, or not. 
1411             if (oldPosition 
== lineRange
.GetStart()) 
1413                 m_caretPosition 
= oldPosition
-1; 
1414                 m_caretAtLineStart 
= true; 
1417             else if (oldPosition 
== lineRange
.GetEnd()) 
1419                 if (m_caretAtLineStart
) 
1421                     // We're at the start of the line, so keep the same caret position 
1422                     // but clear the start-of-line flag. 
1423                     m_caretPosition 
= oldPosition
; 
1424                     m_caretAtLineStart 
= false; 
1428                     // We're showing at the end of the line, so go back 
1429                     // to the previous character position. 
1430                     m_caretPosition 
= oldPosition 
- 1; 
1432                 SetDefaultStyleToCursorStyle(); 
1438     SetDefaultStyleToCursorStyle(); 
1442 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
) 
1444     long endPos 
= GetBuffer().GetRange().GetEnd(); 
1446     if (m_caretPosition 
+ noPositions 
< endPos
) 
1448         long oldPos 
= m_caretPosition
; 
1449         long newPos 
= m_caretPosition 
+ noPositions
; 
1451         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1455         // Determine by looking at oldPos and m_caretPosition whether 
1456         // we moved from the end of a line to the start of the next line, in which case 
1457         // we want to adjust the caret position such that it is positioned at the 
1458         // start of the next line, rather than jumping past the first character of the 
1460         if (noPositions 
== 1 && !extendSel
) 
1461             MoveCaretForward(oldPos
); 
1463             SetCaretPosition(newPos
); 
1466         SetDefaultStyleToCursorStyle(); 
1475 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
) 
1479     if (m_caretPosition 
> startPos 
- noPositions 
+ 1) 
1481         long oldPos 
= m_caretPosition
; 
1482         long newPos 
= m_caretPosition 
- noPositions
; 
1483         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1487         if (noPositions 
== 1 && !extendSel
) 
1488             MoveCaretBack(oldPos
); 
1490             SetCaretPosition(newPos
); 
1493         SetDefaultStyleToCursorStyle(); 
1502 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
) 
1504     return MoveDown(- noLines
, flags
); 
1508 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
) 
1513     long lineNumber 
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
); 
1514     wxPoint pt 
= GetCaret()->GetPosition(); 
1515     long newLine 
= lineNumber 
+ noLines
; 
1517     if (lineNumber 
!= -1) 
1521             long lastLine 
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd()); 
1523             if (newLine 
> lastLine
) 
1533     wxRichTextLine
* lineObj 
= GetBuffer().GetLineForVisibleLineNumber(newLine
); 
1536         pt
.y 
= lineObj
->GetAbsolutePosition().y 
+ 2; 
1542     wxClientDC 
dc(this); 
1544     dc
.SetFont(GetFont()); 
1546     int hitTest 
= GetBuffer().HitTest(dc
, pt
, newPos
); 
1548     if (hitTest 
!= wxRICHTEXT_HITTEST_NONE
) 
1550         // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE, 
1551         // we want to be at the end of the last line but with m_caretAtLineStart set to true, 
1552         // so we view the caret at the start of the line. 
1553         bool caretLineStart 
= false; 
1554         if (hitTest 
& wxRICHTEXT_HITTEST_BEFORE
) 
1556             wxRichTextLine
* thisLine 
= GetBuffer().GetLineAtPosition(newPos
-1); 
1557             wxRichTextRange lineRange
; 
1559                 lineRange 
= thisLine
->GetAbsoluteRange(); 
1561             if (thisLine 
&& (newPos
-1) == lineRange
.GetEnd()) 
1564                 caretLineStart 
= true; 
1568                 wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(newPos
); 
1569                 if (para 
&& para
->GetRange().GetStart() == newPos
) 
1574         long newSelEnd 
= newPos
; 
1576         bool extendSel 
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
); 
1580         SetCaretPosition(newPos
, caretLineStart
); 
1582         SetDefaultStyleToCursorStyle(); 
1590 /// Move to the end of the paragraph 
1591 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
) 
1593     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true); 
1596         long newPos 
= para
->GetRange().GetEnd() - 1; 
1597         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1601         SetCaretPosition(newPos
); 
1603         SetDefaultStyleToCursorStyle(); 
1611 /// Move to the start of the paragraph 
1612 bool wxRichTextCtrl::MoveToParagraphStart(int flags
) 
1614     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true); 
1617         long newPos 
= para
->GetRange().GetStart() - 1; 
1618         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1622         SetCaretPosition(newPos
); 
1624         SetDefaultStyleToCursorStyle(); 
1632 /// Move to the end of the line 
1633 bool wxRichTextCtrl::MoveToLineEnd(int flags
) 
1635     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(m_caretPosition
); 
1639         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1640         long newPos 
= lineRange
.GetEnd(); 
1641         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1645         SetCaretPosition(newPos
); 
1647         SetDefaultStyleToCursorStyle(); 
1655 /// Move to the start of the line 
1656 bool wxRichTextCtrl::MoveToLineStart(int flags
) 
1658     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(m_caretPosition
); 
1661         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
1662         long newPos 
= lineRange
.GetStart()-1; 
1664         bool extendSel 
= ExtendSelection(m_caretPosition
, newPos
, flags
); 
1668         wxRichTextParagraph
* para 
= GetBuffer().GetParagraphForLine(line
); 
1670         SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart()); 
1672         SetDefaultStyleToCursorStyle(); 
1680 /// Move to the start of the buffer 
1681 bool wxRichTextCtrl::MoveHome(int flags
) 
1683     if (m_caretPosition 
!= -1) 
1685         bool extendSel 
= ExtendSelection(m_caretPosition
, -1, flags
); 
1689         SetCaretPosition(-1); 
1691         SetDefaultStyleToCursorStyle(); 
1699 /// Move to the end of the buffer 
1700 bool wxRichTextCtrl::MoveEnd(int flags
) 
1702     long endPos 
= GetBuffer().GetRange().GetEnd()-1; 
1704     if (m_caretPosition 
!= endPos
) 
1706         bool extendSel 
= ExtendSelection(m_caretPosition
, endPos
, flags
); 
1710         SetCaretPosition(endPos
); 
1712         SetDefaultStyleToCursorStyle(); 
1720 /// Move noPages pages up 
1721 bool wxRichTextCtrl::PageUp(int noPages
, int flags
) 
1723     return PageDown(- noPages
, flags
); 
1726 /// Move noPages pages down 
1727 bool wxRichTextCtrl::PageDown(int noPages
, int flags
) 
1729     // Calculate which line occurs noPages * screen height further down. 
1730     wxRichTextLine
* line 
= GetVisibleLineForCaretPosition(m_caretPosition
); 
1733         wxSize clientSize 
= GetClientSize(); 
1734         int newY 
= line
->GetAbsolutePosition().y 
+ noPages
*clientSize
.y
; 
1736         wxRichTextLine
* newLine 
= GetBuffer().GetLineAtYPosition(newY
); 
1739             wxRichTextRange lineRange 
= newLine
->GetAbsoluteRange(); 
1740             long pos 
= lineRange
.GetStart()-1; 
1741             if (pos 
!= m_caretPosition
) 
1743                 wxRichTextParagraph
* para 
= GetBuffer().GetParagraphForLine(newLine
); 
1745                 bool extendSel 
= ExtendSelection(m_caretPosition
, pos
, flags
); 
1749                 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart()); 
1751                 SetDefaultStyleToCursorStyle(); 
1761 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
) 
1763     return str 
== wxT(" ") || str 
== wxT("\t"); 
1766 // Finds the caret position for the next word 
1767 long wxRichTextCtrl::FindNextWordPosition(int direction
) const 
1769     long endPos 
= GetBuffer().GetRange().GetEnd(); 
1773         long i 
= m_caretPosition
+1+direction
; // +1 for conversion to character pos 
1775         // First skip current text to space 
1776         while (i 
< endPos 
&& i 
> -1) 
1778             // i is in character, not caret positions 
1779             wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
)); 
1780             wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(i
, false); 
1781             if (line 
&& (i 
== line
->GetAbsoluteRange().GetEnd())) 
1785             else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty()) 
1792         while (i 
< endPos 
&& i 
> -1) 
1794             // i is in character, not caret positions 
1795             wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
)); 
1796             wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(i
, false); 
1797             if (line 
&& (i 
== line
->GetAbsoluteRange().GetEnd())) 
1798                 return wxMax(-1, i
); 
1800             if (text
.empty()) // End of paragraph, or maybe an image 
1801                 return wxMax(-1, i 
- 1); 
1802             else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty()) 
1806                 // Convert to caret position 
1807                 return wxMax(-1, i 
- 1); 
1816         long i 
= m_caretPosition
; 
1818         // First skip white space 
1819         while (i 
< endPos 
&& i 
> -1) 
1821             // i is in character, not caret positions 
1822             wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
)); 
1823             wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(i
, false); 
1825             if (text
.empty() || (line 
&& (i 
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image 
1827             else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty()) 
1832         // Next skip current text to space 
1833         while (i 
< endPos 
&& i 
> -1) 
1835             // i is in character, not caret positions 
1836             wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
)); 
1837             wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(i
, false); 
1838             if (line 
&& line
->GetAbsoluteRange().GetStart() == i
) 
1841             if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */) 
1854 /// Move n words left 
1855 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
) 
1857     long pos 
= FindNextWordPosition(-1); 
1858     if (pos 
!= m_caretPosition
) 
1860         wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(pos
, true); 
1862         bool extendSel 
= ExtendSelection(m_caretPosition
, pos
, flags
); 
1866         SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
); 
1868         SetDefaultStyleToCursorStyle(); 
1876 /// Move n words right 
1877 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
) 
1879     long pos 
= FindNextWordPosition(1); 
1880     if (pos 
!= m_caretPosition
) 
1882         wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(pos
, true); 
1884         bool extendSel 
= ExtendSelection(m_caretPosition
, pos
, flags
); 
1888         SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
); 
1890         SetDefaultStyleToCursorStyle(); 
1899 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
) 
1901     // Only do sizing optimization for large buffers 
1902     if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
) 
1904         m_fullLayoutRequired 
= true; 
1905         m_fullLayoutTime 
= wxGetLocalTimeMillis(); 
1906         m_fullLayoutSavedPosition 
= GetFirstVisiblePosition(); 
1907         LayoutContent(true /* onlyVisibleRect */); 
1910         GetBuffer().Invalidate(wxRICHTEXT_ALL
); 
1912 #if wxRICHTEXT_BUFFERED_PAINTING 
1920 /// Idle-time processing 
1921 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
) 
1923 #if wxRICHTEXT_USE_OWN_CARET 
1924     if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate()) 
1926         ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false); 
1932     const int layoutInterval 
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
; 
1934     if (m_fullLayoutRequired 
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime 
+ layoutInterval
))) 
1936         m_fullLayoutRequired 
= false; 
1937         m_fullLayoutTime 
= 0; 
1938         GetBuffer().Invalidate(wxRICHTEXT_ALL
); 
1939         ShowPosition(m_fullLayoutSavedPosition
); 
1943     if (m_caretPositionForDefaultStyle 
!= -2) 
1945         // If the caret position has changed, no longer reflect the default style 
1947         if (GetCaretPosition() != m_caretPositionForDefaultStyle
) 
1948             m_caretPositionForDefaultStyle 
= -2; 
1955 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
) 
1957 #if wxRICHTEXT_USE_OWN_CARET 
1958     if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate()) 
1961         ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(); 
1968 /// Set up scrollbars, e.g. after a resize 
1969 void wxRichTextCtrl::SetupScrollbars(bool atTop
) 
1974     if (GetBuffer().IsEmpty()) 
1976         SetScrollbars(0, 0, 0, 0, 0, 0); 
1980     // TODO: reimplement scrolling so we scroll by line, not by fixed number 
1981     // of pixels. See e.g. wxVScrolledWindow for ideas. 
1982     int pixelsPerUnit 
= 5; 
1983     wxSize clientSize 
= GetClientSize(); 
1985     int maxHeight 
= GetBuffer().GetCachedSize().y
; 
1987     // Round up so we have at least maxHeight pixels 
1988     int unitsY 
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5); 
1990     int startX 
= 0, startY 
= 0; 
1992         GetViewStart(& startX
, & startY
); 
1994     int maxPositionX 
= 0; 
1995     int maxPositionY 
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5); 
1997     int newStartX 
= wxMin(maxPositionX
, startX
); 
1998     int newStartY 
= wxMin(maxPositionY
, startY
); 
2000     int oldPPUX
, oldPPUY
; 
2001     int oldStartX
, oldStartY
; 
2002     int oldVirtualSizeX 
= 0, oldVirtualSizeY 
= 0; 
2003     GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
); 
2004     GetViewStart(& oldStartX
, & oldStartY
); 
2005     GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
); 
2007         oldVirtualSizeY 
/= oldPPUY
; 
2009     if (oldPPUX 
== 0 && oldPPUY 
== pixelsPerUnit 
&& oldVirtualSizeY 
== unitsY 
&& oldStartX 
== newStartX 
&& oldStartY 
== newStartY
) 
2012     // Don't set scrollbars if there were none before, and there will be none now. 
2013     if (oldPPUY 
!= 0 && (oldVirtualSizeY 
< clientSize
.y
) && (unitsY
*pixelsPerUnit 
< clientSize
.y
)) 
2016     // Move to previous scroll position if 
2018     SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
); 
2021 /// Paint the background 
2022 void wxRichTextCtrl::PaintBackground(wxDC
& dc
) 
2024     wxColour backgroundColour 
= GetBackgroundColour(); 
2025     if (!backgroundColour
.Ok()) 
2026         backgroundColour 
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
); 
2028     // Clear the background 
2029     dc
.SetBrush(wxBrush(backgroundColour
)); 
2030     dc
.SetPen(*wxTRANSPARENT_PEN
); 
2031     wxRect 
windowRect(GetClientSize()); 
2032     windowRect
.x 
-= 2; windowRect
.y 
-= 2; 
2033     windowRect
.width 
+= 4; windowRect
.height 
+= 4; 
2035     // We need to shift the rectangle to take into account 
2036     // scrolling. Converting device to logical coordinates. 
2037     CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
); 
2038     dc
.DrawRectangle(windowRect
); 
2041 #if wxRICHTEXT_BUFFERED_PAINTING 
2042 /// Recreate buffer bitmap if necessary 
2043 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
) 
2046     if (sz 
== wxDefaultSize
) 
2047         sz 
= GetClientSize(); 
2049     if (sz
.x 
< 1 || sz
.y 
< 1) 
2052     if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x 
|| m_bufferBitmap
.GetHeight() < sz
.y
) 
2053         m_bufferBitmap 
= wxBitmap(sz
.x
, sz
.y
); 
2054     return m_bufferBitmap
.Ok(); 
2058 // ---------------------------------------------------------------------------- 
2059 // file IO functions 
2060 // ---------------------------------------------------------------------------- 
2062 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
) 
2064     bool success 
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
); 
2066         m_filename 
= filename
; 
2069     SetInsertionPoint(0); 
2072     SetupScrollbars(true); 
2074     wxTextCtrl::SendTextUpdatedEvent(this); 
2080         wxLogError(_("File couldn't be loaded.")); 
2086 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
) 
2088     if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
)) 
2090         m_filename 
= filename
; 
2097     wxLogError(_("The text couldn't be saved.")); 
2102 // ---------------------------------------------------------------------------- 
2103 // wxRichTextCtrl specific functionality 
2104 // ---------------------------------------------------------------------------- 
2106 /// Add a new paragraph of text to the end of the buffer 
2107 wxRichTextRange 
wxRichTextCtrl::AddParagraph(const wxString
& text
) 
2109     wxRichTextRange range 
= GetBuffer().AddParagraph(text
); 
2115 wxRichTextRange 
wxRichTextCtrl::AddImage(const wxImage
& image
) 
2117     wxRichTextRange range 
= GetBuffer().AddImage(image
); 
2122 // ---------------------------------------------------------------------------- 
2123 // selection and ranges 
2124 // ---------------------------------------------------------------------------- 
2126 void wxRichTextCtrl::SelectAll() 
2128     SetSelection(0, GetLastPosition()+1); 
2129     m_selectionAnchor 
= -1; 
2133 void wxRichTextCtrl::SelectNone() 
2135     if (!(GetSelectionRange() == wxRichTextRange(-2, -2))) 
2137         wxRichTextRange oldSelection 
= m_selectionRange
; 
2139         m_selectionRange 
= wxRichTextRange(-2, -2); 
2141         RefreshForSelectionChange(oldSelection
, m_selectionRange
); 
2143     m_selectionAnchor 
= -2; 
2146 static bool wxIsWordDelimiter(const wxString
& text
) 
2148     return !text
.IsEmpty() && !wxIsalnum(text
[0]); 
2151 /// Select the word at the given character position 
2152 bool wxRichTextCtrl::SelectWord(long position
) 
2154     if (position 
< 0 || position 
> GetBuffer().GetRange().GetEnd()) 
2157     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(position
); 
2161     if (position 
== para
->GetRange().GetEnd()) 
2164     long positionStart 
= position
; 
2165     long positionEnd 
= position
; 
2167     for (positionStart 
= position
; positionStart 
>= para
->GetRange().GetStart(); positionStart 
--) 
2169         wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
)); 
2170         if (wxIsWordDelimiter(text
)) 
2176     if (positionStart 
< para
->GetRange().GetStart()) 
2177         positionStart 
= para
->GetRange().GetStart(); 
2179     for (positionEnd 
= position
; positionEnd 
< para
->GetRange().GetEnd(); positionEnd 
++) 
2181         wxString text 
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
)); 
2182         if (wxIsWordDelimiter(text
)) 
2188     if (positionEnd 
>= para
->GetRange().GetEnd()) 
2189         positionEnd 
= para
->GetRange().GetEnd(); 
2191     if (positionEnd 
< positionStart
) 
2194     SetSelection(positionStart
, positionEnd
+1); 
2196     if (positionStart 
>= 0) 
2198         MoveCaret(positionStart
-1, true); 
2199         SetDefaultStyleToCursorStyle(); 
2205 wxString 
wxRichTextCtrl::GetStringSelection() const 
2208     GetSelection(&from
, &to
); 
2210     return GetRange(from
, to
); 
2213 // ---------------------------------------------------------------------------- 
2215 // ---------------------------------------------------------------------------- 
2217 wxTextCtrlHitTestResult
 
2218 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord 
*x
, wxTextCoord 
*y
) const 
2220     // implement in terms of the other overload as the native ports typically 
2221     // can get the position and not (x, y) pair directly (although wxUniv 
2222     // directly gets x and y -- and so overrides this method as well) 
2224     wxTextCtrlHitTestResult rc 
= HitTest(pt
, &pos
); 
2226     if ( rc 
!= wxTE_HT_UNKNOWN 
) 
2228         PositionToXY(pos
, x
, y
); 
2234 wxTextCtrlHitTestResult
 
2235 wxRichTextCtrl::HitTest(const wxPoint
& pt
, 
2238     wxClientDC 
dc((wxRichTextCtrl
*) this); 
2239     ((wxRichTextCtrl
*)this)->PrepareDC(dc
); 
2241     // Buffer uses logical position (relative to start of buffer) 
2243     wxPoint pt2 
= GetLogicalPoint(pt
); 
2245     int hit 
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
); 
2247     if ((hit 
& wxRICHTEXT_HITTEST_BEFORE
) && (hit 
& wxRICHTEXT_HITTEST_OUTSIDE
)) 
2248         return wxTE_HT_BEFORE
; 
2249     else if ((hit 
& wxRICHTEXT_HITTEST_AFTER
) && (hit 
& wxRICHTEXT_HITTEST_OUTSIDE
)) 
2250         return wxTE_HT_BEYOND
; 
2251     else if (hit 
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
)) 
2252         return wxTE_HT_ON_TEXT
; 
2254     return wxTE_HT_UNKNOWN
; 
2257 // ---------------------------------------------------------------------------- 
2258 // set/get the controls text 
2259 // ---------------------------------------------------------------------------- 
2261 wxString 
wxRichTextCtrl::GetValue() const 
2263     return GetBuffer().GetText(); 
2266 wxString 
wxRichTextCtrl::GetRange(long from
, long to
) const 
2268     // Public API for range is different from internals 
2269     return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1)); 
2272 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
) 
2274     // Don't call Clear here, since it always sends a text updated event 
2275     m_buffer
.ResetAndClearCommands(); 
2276     m_buffer
.SetDirty(true); 
2277     m_caretPosition 
= -1; 
2278     m_caretPositionForDefaultStyle 
= -2; 
2279     m_caretAtLineStart 
= false; 
2280     m_selectionRange
.SetRange(-2, -2); 
2290     if (!value
.IsEmpty()) 
2292         // Remove empty paragraph 
2293         GetBuffer().Clear(); 
2294         DoWriteText(value
, flags
); 
2296         // for compatibility, don't move the cursor when doing SetValue() 
2297         SetInsertionPoint(0); 
2301         // still send an event for consistency 
2302         if (flags 
& SetValue_SendEvent
) 
2303             wxTextCtrl::SendTextUpdatedEvent(this); 
2308 void wxRichTextCtrl::WriteText(const wxString
& value
) 
2313 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
) 
2315     wxString valueUnix 
= wxTextFile::Translate(value
, wxTextFileType_Unix
); 
2317     GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
); 
2319     if ( flags 
& SetValue_SendEvent 
) 
2320         wxTextCtrl::SendTextUpdatedEvent(this); 
2323 void wxRichTextCtrl::AppendText(const wxString
& text
) 
2325     SetInsertionPointEnd(); 
2330 /// Write an image at the current insertion point 
2331 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
) 
2333     wxRichTextImageBlock imageBlock
; 
2335     wxImage image2 
= image
; 
2336     if (imageBlock
.MakeImageBlock(image2
, bitmapType
)) 
2337         return WriteImage(imageBlock
); 
2342 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
) 
2344     wxRichTextImageBlock imageBlock
; 
2347     if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false)) 
2348         return WriteImage(imageBlock
); 
2353 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
) 
2355     return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this); 
2358 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
) 
2362         wxRichTextImageBlock imageBlock
; 
2364         wxImage image 
= bitmap
.ConvertToImage(); 
2365         if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
)) 
2366             return WriteImage(imageBlock
); 
2372 /// Insert a newline (actually paragraph) at the current insertion point. 
2373 bool wxRichTextCtrl::Newline() 
2375     return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
); 
2378 /// Insert a line break at the current insertion point. 
2379 bool wxRichTextCtrl::LineBreak() 
2382     text 
= wxRichTextLineBreakChar
; 
2383     return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this); 
2386 // ---------------------------------------------------------------------------- 
2387 // Clipboard operations 
2388 // ---------------------------------------------------------------------------- 
2390 void wxRichTextCtrl::Copy() 
2394         wxRichTextRange range 
= GetInternalSelectionRange(); 
2395         GetBuffer().CopyToClipboard(range
); 
2399 void wxRichTextCtrl::Cut() 
2403         wxRichTextRange range 
= GetInternalSelectionRange(); 
2404         GetBuffer().CopyToClipboard(range
); 
2406         DeleteSelectedContent(); 
2412 void wxRichTextCtrl::Paste() 
2416         BeginBatchUndo(_("Paste")); 
2418         long newPos 
= m_caretPosition
; 
2419         DeleteSelectedContent(& newPos
); 
2421         GetBuffer().PasteFromClipboard(newPos
); 
2427 void wxRichTextCtrl::DeleteSelection() 
2429     if (CanDeleteSelection()) 
2431         DeleteSelectedContent(); 
2435 bool wxRichTextCtrl::HasSelection() const 
2437     return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2; 
2440 bool wxRichTextCtrl::CanCopy() const 
2442     // Can copy if there's a selection 
2443     return HasSelection(); 
2446 bool wxRichTextCtrl::CanCut() const 
2448     return HasSelection() && IsEditable(); 
2451 bool wxRichTextCtrl::CanPaste() const 
2453     if ( !IsEditable() ) 
2456     return GetBuffer().CanPasteFromClipboard(); 
2459 bool wxRichTextCtrl::CanDeleteSelection() const 
2461     return HasSelection() && IsEditable(); 
2465 // ---------------------------------------------------------------------------- 
2467 // ---------------------------------------------------------------------------- 
2469 void wxRichTextCtrl::SetEditable(bool editable
) 
2471     m_editable 
= editable
; 
2474 void wxRichTextCtrl::SetInsertionPoint(long pos
) 
2478     m_caretPosition 
= pos 
- 1; 
2483 void wxRichTextCtrl::SetInsertionPointEnd() 
2485     long pos 
= GetLastPosition(); 
2486     SetInsertionPoint(pos
); 
2489 long wxRichTextCtrl::GetInsertionPoint() const 
2491     return m_caretPosition
+1; 
2494 wxTextPos 
wxRichTextCtrl::GetLastPosition() const 
2496     return GetBuffer().GetRange().GetEnd(); 
2499 // If the return values from and to are the same, there is no 
2501 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const 
2503     *from 
= m_selectionRange
.GetStart(); 
2504     *to 
= m_selectionRange
.GetEnd(); 
2505     if ((*to
) != -1 && (*to
) != -2) 
2509 bool wxRichTextCtrl::IsEditable() const 
2514 // ---------------------------------------------------------------------------- 
2516 // ---------------------------------------------------------------------------- 
2518 void wxRichTextCtrl::SetSelection(long from
, long to
) 
2520     // if from and to are both -1, it means (in wxWidgets) that all text should 
2522     if ( (from 
== -1) && (to 
== -1) ) 
2525         to 
= GetLastPosition()+1; 
2528     DoSetSelection(from
, to
); 
2531 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
)) 
2539         wxRichTextRange oldSelection 
= m_selectionRange
; 
2540         m_selectionAnchor 
= from
; 
2541         m_selectionRange
.SetRange(from
, to
-1); 
2543             m_caretPosition 
= from
-1; 
2545         RefreshForSelectionChange(oldSelection
, m_selectionRange
); 
2550 // ---------------------------------------------------------------------------- 
2552 // ---------------------------------------------------------------------------- 
2554 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), 
2555                              const wxString
& value
) 
2557     BeginBatchUndo(_("Replace")); 
2559     DeleteSelectedContent(); 
2561     DoWriteText(value
, SetValue_SelectionOnly
); 
2566 void wxRichTextCtrl::Remove(long from
, long to
) 
2570     GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this); 
2577 bool wxRichTextCtrl::IsModified() const 
2579     return m_buffer
.IsModified(); 
2582 void wxRichTextCtrl::MarkDirty() 
2584     m_buffer
.Modify(true); 
2587 void wxRichTextCtrl::DiscardEdits() 
2589     m_caretPositionForDefaultStyle 
= -2; 
2590     m_buffer
.Modify(false); 
2591     m_buffer
.GetCommandProcessor()->ClearCommands(); 
2594 int wxRichTextCtrl::GetNumberOfLines() const 
2596     return GetBuffer().GetParagraphCount(); 
2599 // ---------------------------------------------------------------------------- 
2600 // Positions <-> coords 
2601 // ---------------------------------------------------------------------------- 
2603 long wxRichTextCtrl::XYToPosition(long x
, long y
) const 
2605     return GetBuffer().XYToPosition(x
, y
); 
2608 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const 
2610     return GetBuffer().PositionToXY(pos
, x
, y
); 
2613 // ---------------------------------------------------------------------------- 
2615 // ---------------------------------------------------------------------------- 
2617 void wxRichTextCtrl::ShowPosition(long pos
) 
2619     if (!IsPositionVisible(pos
)) 
2620         ScrollIntoView(pos
-1, WXK_DOWN
); 
2623 int wxRichTextCtrl::GetLineLength(long lineNo
) const 
2625     return GetBuffer().GetParagraphLength(lineNo
); 
2628 wxString 
wxRichTextCtrl::GetLineText(long lineNo
) const 
2630     return GetBuffer().GetParagraphText(lineNo
); 
2633 // ---------------------------------------------------------------------------- 
2635 // ---------------------------------------------------------------------------- 
2637 void wxRichTextCtrl::Undo() 
2641         GetCommandProcessor()->Undo(); 
2645 void wxRichTextCtrl::Redo() 
2649         GetCommandProcessor()->Redo(); 
2653 bool wxRichTextCtrl::CanUndo() const 
2655     return GetCommandProcessor()->CanUndo(); 
2658 bool wxRichTextCtrl::CanRedo() const 
2660     return GetCommandProcessor()->CanRedo(); 
2663 // ---------------------------------------------------------------------------- 
2664 // implementation details 
2665 // ---------------------------------------------------------------------------- 
2667 void wxRichTextCtrl::Command(wxCommandEvent
& event
) 
2669     SetValue(event
.GetString()); 
2670     GetEventHandler()->ProcessEvent(event
); 
2673 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
) 
2675     // By default, load the first file into the text window. 
2676     if (event
.GetNumberOfFiles() > 0) 
2678         LoadFile(event
.GetFiles()[0]); 
2682 wxSize 
wxRichTextCtrl::DoGetBestSize() const 
2684     return wxSize(10, 10); 
2687 // ---------------------------------------------------------------------------- 
2688 // standard handlers for standard edit menu events 
2689 // ---------------------------------------------------------------------------- 
2691 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
)) 
2696 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
)) 
2701 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
)) 
2706 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
)) 
2711 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
)) 
2716 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
)) 
2721 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
) 
2723     event
.Enable( CanCut() ); 
2726 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
) 
2728     event
.Enable( CanCopy() ); 
2731 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
) 
2733     event
.Enable( CanDeleteSelection() ); 
2736 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
) 
2738     event
.Enable( CanPaste() ); 
2741 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
) 
2743     event
.Enable( CanUndo() ); 
2744     event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() ); 
2747 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
) 
2749     event
.Enable( CanRedo() ); 
2750     event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() ); 
2753 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
)) 
2758 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
) 
2760     event
.Enable(GetLastPosition() > 0); 
2763 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
) 
2765     if (event
.GetEventObject() != this) 
2773         m_contextMenu 
= new wxMenu
; 
2774         m_contextMenu
->Append(wxID_UNDO
, _("&Undo")); 
2775         m_contextMenu
->Append(wxID_REDO
, _("&Redo")); 
2776         m_contextMenu
->AppendSeparator(); 
2777         m_contextMenu
->Append(wxID_CUT
, _("Cu&t")); 
2778         m_contextMenu
->Append(wxID_COPY
, _("&Copy")); 
2779         m_contextMenu
->Append(wxID_PASTE
, _("&Paste")); 
2780         m_contextMenu
->Append(wxID_CLEAR
, _("&Delete")); 
2781         m_contextMenu
->AppendSeparator(); 
2782         m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All")); 
2784     PopupMenu(m_contextMenu
); 
2788 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
) 
2790     return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
)); 
2793 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
) 
2795     return GetBuffer().SetStyle(range
.ToInternal(), style
); 
2798 // extended style setting operation with flags including: 
2799 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY. 
2800 // see richtextbuffer.h for more details. 
2802 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
) 
2804     return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
); 
2807 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
) 
2809     return GetBuffer().SetDefaultStyle(wxTextAttr(style
)); 
2812 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const 
2814     return GetBuffer().GetDefaultStyle(); 
2817 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
) 
2819     return GetBuffer().GetStyle(position
, style
); 
2822 // get the common set of styles for the range 
2823 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
) 
2825     return GetBuffer().GetStyleForRange(range
.ToInternal(), style
); 
2828 /// Get the content (uncombined) attributes for this position. 
2829 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
) 
2831     return GetBuffer().GetUncombinedStyle(position
, style
); 
2834 /// Set font, and also the buffer attributes 
2835 bool wxRichTextCtrl::SetFont(const wxFont
& font
) 
2837     wxControl::SetFont(font
); 
2839     wxTextAttr attr 
= GetBuffer().GetAttributes(); 
2841     GetBuffer().SetBasicStyle(attr
); 
2843     GetBuffer().Invalidate(wxRICHTEXT_ALL
); 
2849 /// Transform logical to physical 
2850 wxPoint 
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const 
2853     CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
); 
2858 /// Transform physical to logical 
2859 wxPoint 
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const 
2862     CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
); 
2867 /// Position the caret 
2868 void wxRichTextCtrl::PositionCaret() 
2873     //wxLogDebug(wxT("PositionCaret")); 
2876     if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
)) 
2878         wxPoint newPt 
= caretRect
.GetPosition(); 
2879         wxSize newSz 
= caretRect
.GetSize(); 
2880         wxPoint pt 
= GetPhysicalPoint(newPt
); 
2881         if (GetCaret()->GetPosition() != pt 
|| GetCaret()->GetSize() != newSz
) 
2884             if (GetCaret()->GetSize() != newSz
) 
2885                 GetCaret()->SetSize(newSz
); 
2886             GetCaret()->Move(pt
); 
2892 /// Get the caret height and position for the given character position 
2893 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
) 
2895     wxClientDC 
dc(this); 
2896     dc
.SetFont(GetFont()); 
2903     if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
)) 
2905         // Caret height can't be zero 
2907             height 
= dc
.GetCharHeight(); 
2909         rect 
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
)); 
2916 /// Gets the line for the visible caret position. If the caret is 
2917 /// shown at the very end of the line, it means the next character is actually 
2918 /// on the following line. So let's get the line we're expecting to find 
2919 /// if this is the case. 
2920 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const 
2922     wxRichTextLine
* line 
= GetBuffer().GetLineAtPosition(caretPosition
, true); 
2923     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(caretPosition
, true); 
2926         wxRichTextRange lineRange 
= line
->GetAbsoluteRange(); 
2927         if (caretPosition 
== lineRange
.GetStart()-1 && 
2928             (para
->GetRange().GetStart() != lineRange
.GetStart())) 
2930             if (!m_caretAtLineStart
) 
2931                 line 
= GetBuffer().GetLineAtPosition(caretPosition
-1, true); 
2938 /// Move the caret to the given character position 
2939 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
) 
2941     if (GetBuffer().GetDirty()) 
2944     if (pos 
<= GetBuffer().GetRange().GetEnd()) 
2946         SetCaretPosition(pos
, showAtLineStart
); 
2956 /// Layout the buffer: which we must do before certain operations, such as 
2957 /// setting the caret position. 
2958 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
) 
2960     if (GetBuffer().GetDirty() || onlyVisibleRect
) 
2962         wxRect 
availableSpace(GetClientSize()); 
2963         if (availableSpace
.width 
== 0) 
2964             availableSpace
.width 
= 10; 
2965         if (availableSpace
.height 
== 0) 
2966             availableSpace
.height 
= 10; 
2968         int flags 
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
; 
2969         if (onlyVisibleRect
) 
2971             flags 
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
; 
2972             availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0))); 
2975         wxClientDC 
dc(this); 
2976         dc
.SetFont(GetFont()); 
2980         GetBuffer().Defragment(); 
2981         GetBuffer().UpdateRanges();     // If items were deleted, ranges need recalculation 
2982         GetBuffer().Layout(dc
, availableSpace
, flags
); 
2983         GetBuffer().SetDirty(false); 
2992 /// Is all of the selection bold? 
2993 bool wxRichTextCtrl::IsSelectionBold() 
2998         wxRichTextRange range 
= GetSelectionRange(); 
2999         attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
); 
3000         attr
.SetFontWeight(wxBOLD
); 
3002         return HasCharacterAttributes(range
, attr
); 
3006         // If no selection, then we need to combine current style with default style 
3007         // to see what the effect would be if we started typing. 
3009         attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
); 
3011         long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
3012         if (GetStyle(pos
, attr
)) 
3014             if (IsDefaultStyleShowing()) 
3015                 wxRichTextApplyStyle(attr
, GetDefaultStyleEx()); 
3016             return attr
.GetFontWeight() == wxBOLD
; 
3022 /// Is all of the selection italics? 
3023 bool wxRichTextCtrl::IsSelectionItalics() 
3027         wxRichTextRange range 
= GetSelectionRange(); 
3029         attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
); 
3030         attr
.SetFontStyle(wxITALIC
); 
3032         return HasCharacterAttributes(range
, attr
); 
3036         // If no selection, then we need to combine current style with default style 
3037         // to see what the effect would be if we started typing. 
3039         attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
); 
3041         long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
3042         if (GetStyle(pos
, attr
)) 
3044             if (IsDefaultStyleShowing()) 
3045                 wxRichTextApplyStyle(attr
, GetDefaultStyleEx()); 
3046             return attr
.GetFontStyle() == wxITALIC
; 
3052 /// Is all of the selection underlined? 
3053 bool wxRichTextCtrl::IsSelectionUnderlined() 
3057         wxRichTextRange range 
= GetSelectionRange(); 
3059         attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
); 
3060         attr
.SetFontUnderlined(true); 
3062         return HasCharacterAttributes(range
, attr
); 
3066         // If no selection, then we need to combine current style with default style 
3067         // to see what the effect would be if we started typing. 
3069         attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
); 
3070         long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
3072         if (GetStyle(pos
, attr
)) 
3074             if (IsDefaultStyleShowing()) 
3075                 wxRichTextApplyStyle(attr
, GetDefaultStyleEx()); 
3076             return attr
.GetFontUnderlined(); 
3082 /// Apply bold to the selection 
3083 bool wxRichTextCtrl::ApplyBoldToSelection() 
3086     attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
); 
3087     attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL 
: wxBOLD
); 
3090         return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
); 
3093         wxRichTextAttr current 
= GetDefaultStyleEx(); 
3094         current
.Apply(attr
); 
3095         SetAndShowDefaultStyle(current
); 
3100 /// Apply italic to the selection 
3101 bool wxRichTextCtrl::ApplyItalicToSelection() 
3104     attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
); 
3105     attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL 
: wxITALIC
); 
3108         return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
); 
3111         wxRichTextAttr current 
= GetDefaultStyleEx(); 
3112         current
.Apply(attr
); 
3113         SetAndShowDefaultStyle(current
); 
3118 /// Apply underline to the selection 
3119 bool wxRichTextCtrl::ApplyUnderlineToSelection() 
3122     attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
); 
3123     attr
.SetFontUnderlined(!IsSelectionUnderlined()); 
3126         return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
); 
3129         wxRichTextAttr current 
= GetDefaultStyleEx(); 
3130         current
.Apply(attr
); 
3131         SetAndShowDefaultStyle(current
); 
3136 /// Is all of the selection aligned according to the specified flag? 
3137 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
) 
3139     wxRichTextRange range
; 
3141         range 
= GetSelectionRange(); 
3143         range 
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2); 
3146     attr
.SetAlignment(alignment
); 
3148     return HasParagraphAttributes(range
, attr
); 
3151 /// Apply alignment to the selection 
3152 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
) 
3155     attr
.SetAlignment(alignment
); 
3157         return SetStyle(GetSelectionRange(), attr
); 
3160         wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1); 
3162             return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
); 
3167 /// Apply a named style to the selection 
3168 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
) 
3170     // Flags are defined within each definition, so only certain 
3171     // attributes are applied. 
3172     wxTextAttr 
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle()); 
3174     int flags 
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
; 
3176     if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
))) 
3178         flags 
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
; 
3180         wxRichTextRange range
; 
3183             range 
= GetSelectionRange(); 
3186             long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
3187             range 
= wxRichTextRange(pos
, pos
+1); 
3190         return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
); 
3193     // Make sure the attr has the style name 
3194     if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
))) 
3196         attr
.SetParagraphStyleName(def
->GetName()); 
3198         // If applying a paragraph style, we only want the paragraph nodes to adopt these 
3199         // attributes, and not the leaf nodes. This will allow the content (e.g. text) 
3200         // to change its style independently. 
3201         flags 
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
; 
3204         attr
.SetCharacterStyleName(def
->GetName()); 
3207         return SetStyleEx(GetSelectionRange(), attr
, flags
); 
3210         wxRichTextAttr current 
= GetDefaultStyleEx(); 
3211         current
.Apply(attr
); 
3212         SetAndShowDefaultStyle(current
); 
3217 /// Apply the style sheet to the buffer, for example if the styles have changed. 
3218 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
) 
3221         styleSheet 
= GetBuffer().GetStyleSheet(); 
3225     if (GetBuffer().ApplyStyleSheet(styleSheet
)) 
3227         GetBuffer().Invalidate(wxRICHTEXT_ALL
); 
3235 /// Sets the default style to the style under the cursor 
3236 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() 
3239     attr
.SetFlags(wxTEXT_ATTR_CHARACTER
); 
3241     // If at the start of a paragraph, use the next position. 
3242     long pos 
= GetAdjustedCaretPosition(GetCaretPosition()); 
3244     if (GetUncombinedStyle(pos
, attr
)) 
3246         SetDefaultStyle(attr
); 
3253 /// Returns the first visible position in the current view 
3254 long wxRichTextCtrl::GetFirstVisiblePosition() const 
3256     wxRichTextLine
* line 
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
); 
3258         return line
->GetAbsoluteRange().GetStart(); 
3263 /// Get the first visible point in the window 
3264 wxPoint 
wxRichTextCtrl::GetFirstVisiblePoint() const 
3267     int startXUnits
, startYUnits
; 
3269     GetScrollPixelsPerUnit(& ppuX
, & ppuY
); 
3270     GetViewStart(& startXUnits
, & startYUnits
); 
3272     return wxPoint(startXUnits 
* ppuX
, startYUnits 
* ppuY
); 
3275 /// The adjusted caret position is the character position adjusted to take 
3276 /// into account whether we're at the start of a paragraph, in which case 
3277 /// style information should be taken from the next position, not current one. 
3278 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const 
3280     wxRichTextParagraph
* para 
= GetBuffer().GetParagraphAtPosition(caretPos
+1); 
3282     if (para 
&& (caretPos
+1 == para
->GetRange().GetStart())) 
3287 /// Get/set the selection range in character positions. -1, -1 means no selection. 
3288 /// The range is in API convention, i.e. a single character selection is denoted 
3290 wxRichTextRange 
wxRichTextCtrl::GetSelectionRange() const 
3292     wxRichTextRange range 
= GetInternalSelectionRange(); 
3293     if (range 
!= wxRichTextRange(-2,-2) && range 
!= wxRichTextRange(-1,-1)) 
3294         range
.SetEnd(range
.GetEnd() + 1); 
3298 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
) 
3300     wxRichTextRange 
range1(range
); 
3301     if (range1 
!= wxRichTextRange(-2,-2) && range1 
!= wxRichTextRange(-1,-1) ) 
3302         range1
.SetEnd(range1
.GetEnd() - 1); 
3304     wxASSERT( range1
.GetStart() > range1
.GetEnd() ); 
3306     SetInternalSelectionRange(range1
); 
3310 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
3312     return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
); 
3315 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
) 
3317     return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
); 
3320 /// Clear list for given range 
3321 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
) 
3323     return GetBuffer().ClearListStyle(range
.ToInternal(), flags
); 
3326 /// Number/renumber any list elements in the given range 
3327 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
) 
3329     return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
); 
3332 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
) 
3334     return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
); 
3337 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1 
3338 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
) 
3340     return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
); 
3343 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
) 
3345     return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
); 
3348 /// Deletes the content in the given range 
3349 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
) 
3351     return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this); 
3354 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames() 
3356     if (sm_availableFontNames
.GetCount() == 0) 
3358         sm_availableFontNames 
= wxFontEnumerator::GetFacenames(); 
3359         sm_availableFontNames
.Sort(); 
3361     return sm_availableFontNames
; 
3364 void wxRichTextCtrl::ClearAvailableFontNames() 
3366     sm_availableFontNames
.Clear(); 
3369 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
)) 
3371     //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged")); 
3373     wxTextAttrEx basicStyle 
= GetBasicStyle(); 
3374     basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
)); 
3375     SetBasicStyle(basicStyle
); 
3376     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
)); 
3381 // Refresh the area affected by a selection change 
3382 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
) 
3384     // Calculate the refresh rectangle - just the affected lines 
3385     long firstPos
, lastPos
; 
3386     if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2) 
3388         firstPos 
= newSelection
.GetStart(); 
3389         lastPos 
= newSelection
.GetEnd(); 
3391     else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2) 
3393         firstPos 
= oldSelection
.GetStart(); 
3394         lastPos 
= oldSelection
.GetEnd(); 
3396     else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2) 
3402         firstPos 
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart()); 
3403         lastPos 
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd()); 
3406     wxRichTextLine
* firstLine 
= GetBuffer().GetLineAtPosition(firstPos
); 
3407     wxRichTextLine
* lastLine 
= GetBuffer().GetLineAtPosition(lastPos
); 
3409     if (firstLine 
&& lastLine
) 
3411         wxSize clientSize 
= GetClientSize(); 
3412         wxPoint pt1 
= GetPhysicalPoint(firstLine
->GetAbsolutePosition()); 
3413         wxPoint pt2 
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
); 
3416         pt1
.y 
= wxMax(0, pt1
.y
); 
3418         pt2
.y 
= wxMin(clientSize
.y
, pt2
.y
); 
3420         wxRect 
rect(pt1
, wxSize(clientSize
.x
, pt2
.y 
- pt1
.y
)); 
3421         RefreshRect(rect
, false); 
3429 #if wxRICHTEXT_USE_OWN_CARET 
3431 // ---------------------------------------------------------------------------- 
3432 // initialization and destruction 
3433 // ---------------------------------------------------------------------------- 
3435 void wxRichTextCaret::Init() 
3441     m_richTextCtrl 
= NULL
; 
3442     m_needsUpdate 
= false; 
3445 wxRichTextCaret::~wxRichTextCaret() 
3449 // ---------------------------------------------------------------------------- 
3450 // showing/hiding/moving the caret (base class interface) 
3451 // ---------------------------------------------------------------------------- 
3453 void wxRichTextCaret::DoShow() 
3458 void wxRichTextCaret::DoHide() 
3463 void wxRichTextCaret::DoMove() 
3469         if (m_xOld 
!= -1 && m_yOld 
!= -1) 
3473                 wxRect 
rect(GetPosition(), GetSize()); 
3474                 m_richTextCtrl
->RefreshRect(rect
, false); 
3483 void wxRichTextCaret::DoSize() 
3485     int countVisible 
= m_countVisible
; 
3486     if (countVisible 
> 0) 
3492     if (countVisible 
> 0) 
3494         m_countVisible 
= countVisible
; 
3499 // ---------------------------------------------------------------------------- 
3500 // handling the focus 
3501 // ---------------------------------------------------------------------------- 
3503 void wxRichTextCaret::OnSetFocus() 
3511 void wxRichTextCaret::OnKillFocus() 
3516 // ---------------------------------------------------------------------------- 
3517 // drawing the caret 
3518 // ---------------------------------------------------------------------------- 
3520 void wxRichTextCaret::Refresh() 
3524         wxRect 
rect(GetPosition(), GetSize()); 
3525         m_richTextCtrl
->RefreshRect(rect
, false); 
3529 void wxRichTextCaret::DoDraw(wxDC 
*dc
) 
3531     dc
->SetPen( *wxBLACK_PEN 
); 
3533     dc
->SetBrush(*(m_hasFocus 
? wxBLACK_BRUSH 
: wxTRANSPARENT_BRUSH
)); 
3534     dc
->SetPen(*wxBLACK_PEN
); 
3536     // VZ: unfortunately, the rectangle comes out a pixel smaller when this is 
3537     //     done under wxGTK - no idea why 
3538     //dc->SetLogicalFunction(wxINVERT); 
3540     wxPoint 
pt(m_x
, m_y
); 
3544         pt 
= m_richTextCtrl
->GetLogicalPoint(pt
); 
3546     dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
); 
3549     // wxRICHTEXT_USE_OWN_CARET