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"
30 #include "wx/textfile.h"
32 #include "wx/filename.h"
33 #include "wx/dcbuffer.h"
34 #include "wx/arrimpl.cpp"
35 #include "wx/fontenum.h"
38 // DLL options compatibility check:
40 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
42 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
43 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
44 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
45 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
46 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN
, wxRichTextEvent
);
47 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER
, wxRichTextEvent
);
48 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE
, wxRichTextEvent
);
50 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
51 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
59 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
61 #if wxRICHTEXT_USE_OWN_CARET
66 * This implements a non-flashing cursor in case there
67 * are platform-specific problems with the generic caret.
68 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
71 class wxRichTextCaret
;
72 class wxRichTextCaretTimer
: public wxTimer
75 wxRichTextCaretTimer(wxRichTextCaret
* caret
)
79 virtual void Notify();
80 wxRichTextCaret
* m_caret
;
83 class wxRichTextCaret
: public wxCaret
88 // default - use Create()
89 wxRichTextCaret(): m_timer(this) { Init(); }
90 // creates a block caret associated with the given window
91 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
92 : wxCaret(window
, width
, height
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
93 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
94 : wxCaret(window
, size
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
96 virtual ~wxRichTextCaret();
101 // called by wxWindow (not using the event tables)
102 virtual void OnSetFocus();
103 virtual void OnKillFocus();
105 // draw the caret on the given DC
106 void DoDraw(wxDC
*dc
);
108 // get the visible count
109 int GetVisibleCount() const { return m_countVisible
; }
111 // delay repositioning
112 bool GetNeedsUpdate() const { return m_needsUpdate
; }
113 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
118 virtual void DoShow();
119 virtual void DoHide();
120 virtual void DoMove();
121 virtual void DoSize();
131 bool m_hasFocus
; // true => our window has focus
132 bool m_needsUpdate
; // must be repositioned
134 wxRichTextCaretTimer m_timer
;
135 wxRichTextCtrl
* m_richTextCtrl
;
139 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxTextCtrlBase
)
141 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
143 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxTextCtrlBase
)
144 EVT_PAINT(wxRichTextCtrl::OnPaint
)
145 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
146 EVT_IDLE(wxRichTextCtrl::OnIdle
)
147 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
148 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
149 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
150 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
151 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
152 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
153 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
154 EVT_CHAR(wxRichTextCtrl::OnChar
)
155 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
156 EVT_SIZE(wxRichTextCtrl::OnSize
)
157 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
158 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
159 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
160 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
161 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
163 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
164 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
166 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
167 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
169 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
170 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
172 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
173 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
175 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
176 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
178 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
179 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
181 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
182 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
189 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
191 wxRichTextCtrl::wxRichTextCtrl()
192 : wxScrollHelper(this)
197 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
199 const wxString
& value
,
203 const wxValidator
& validator
,
204 const wxString
& name
)
205 : wxScrollHelper(this)
208 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
212 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
213 const wxValidator
& validator
, const wxString
& name
)
217 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
218 style
|wxFULL_REPAINT_ON_RESIZE
,
224 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
227 // No physical scrolling, so we can preserve margins
228 EnableScrolling(false, false);
230 if (style
& wxTE_READONLY
)
233 // The base attributes must all have default values
234 wxTextAttr attributes
;
235 attributes
.SetFont(GetFont());
236 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
237 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
238 attributes
.SetLineSpacing(10);
239 attributes
.SetParagraphSpacingAfter(10);
240 attributes
.SetParagraphSpacingBefore(0);
242 SetBasicStyle(attributes
);
244 // The default attributes will be merged with base attributes, so
245 // can be empty to begin with
246 wxTextAttr defaultAttributes
;
247 SetDefaultStyle(defaultAttributes
);
249 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
250 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
253 GetBuffer().SetRichTextCtrl(this);
255 #if wxRICHTEXT_USE_OWN_CARET
256 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
258 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
261 // Tell the sizers to use the given or best size
262 SetInitialSize(size
);
264 #if wxRICHTEXT_BUFFERED_PAINTING
266 RecreateBuffer(size
);
269 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
270 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
272 SetCursor(m_textCursor
);
274 if (!value
.IsEmpty())
277 GetBuffer().AddEventHandler(this);
280 wxAcceleratorEntry entries
[6];
282 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
283 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
284 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
285 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
286 entries
[4].Set(wxACCEL_CMD
, (int) 'Z', wxID_UNDO
);
287 entries
[5].Set(wxACCEL_CMD
, (int) 'Y', wxID_REDO
);
289 wxAcceleratorTable
accel(6, entries
);
290 SetAcceleratorTable(accel
);
292 m_contextMenu
= new wxMenu
;
293 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
294 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
295 m_contextMenu
->AppendSeparator();
296 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
297 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
298 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
299 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
300 m_contextMenu
->AppendSeparator();
301 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
306 wxRichTextCtrl::~wxRichTextCtrl()
308 GetBuffer().RemoveEventHandler(this);
310 delete m_contextMenu
;
313 /// Member initialisation
314 void wxRichTextCtrl::Init()
316 m_contextMenu
= NULL
;
318 m_caretPosition
= -1;
319 m_selectionRange
.SetRange(-2, -2);
320 m_selectionAnchor
= -2;
322 m_caretAtLineStart
= false;
324 m_fullLayoutRequired
= false;
325 m_fullLayoutTime
= 0;
326 m_fullLayoutSavedPosition
= 0;
327 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
328 m_caretPositionForDefaultStyle
= -2;
331 void wxRichTextCtrl::DoThaw()
333 if (GetBuffer().GetDirty())
342 void wxRichTextCtrl::Clear()
344 m_buffer
.ResetAndClearCommands();
345 m_buffer
.SetDirty(true);
346 m_caretPosition
= -1;
347 m_caretPositionForDefaultStyle
= -2;
348 m_caretAtLineStart
= false;
349 m_selectionRange
.SetRange(-2, -2);
359 wxTextCtrl::SendTextUpdatedEvent(this);
363 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
365 #if !wxRICHTEXT_USE_OWN_CARET
366 if (GetCaret() && !IsFrozen())
371 #if wxRICHTEXT_BUFFERED_PAINTING
372 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
382 dc
.SetFont(GetFont());
384 // Paint the background
387 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
389 wxRect
drawingArea(GetUpdateRegion().GetBox());
390 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
392 wxRect
availableSpace(GetClientSize());
393 if (GetBuffer().GetDirty())
395 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
396 GetBuffer().SetDirty(false);
400 wxRect
clipRect(availableSpace
);
401 clipRect
.x
+= GetBuffer().GetLeftMargin();
402 clipRect
.y
+= GetBuffer().GetTopMargin();
403 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
404 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
405 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
406 dc
.SetClippingRegion(clipRect
);
408 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
410 dc
.DestroyClippingRegion();
412 #if wxRICHTEXT_USE_OWN_CARET
413 if (GetCaret()->IsVisible())
415 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
420 #if !wxRICHTEXT_USE_OWN_CARET
427 // Empty implementation, to prevent flicker
428 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
432 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
436 #if !wxRICHTEXT_USE_OWN_CARET
442 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
443 // Work around dropouts when control is focused
451 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
456 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
457 // Work around dropouts when control is focused
465 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
471 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
477 dc
.SetFont(GetFont());
480 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
482 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
484 m_dragStart
= event
.GetLogicalPosition(dc
);
488 bool caretAtLineStart
= false;
490 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
492 // If we're at the start of a line (but not first in para)
493 // then we should keep the caret showing at the start of the line
494 // by showing the m_caretAtLineStart flag.
495 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
496 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
498 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
499 caretAtLineStart
= true;
503 long oldCaretPos
= m_caretPosition
;
505 MoveCaret(position
, caretAtLineStart
);
506 SetDefaultStyleToCursorStyle();
508 if (event
.ShiftDown())
510 if (m_selectionRange
.GetStart() == -2)
511 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
513 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
523 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
528 if (GetCapture() == this)
531 // See if we clicked on a URL
534 dc
.SetFont(GetFont());
537 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
538 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
540 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
542 wxRichTextEvent
cmdEvent(
543 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
545 cmdEvent
.SetEventObject(this);
546 cmdEvent
.SetPosition(m_caretPosition
+1);
548 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
551 if (GetStyle(position
, attr
))
553 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
555 wxString urlTarget
= attr
.GetURL();
556 if (!urlTarget
.IsEmpty())
558 wxMouseEvent
mouseEvent(event
);
560 long startPos
= 0, endPos
= 0;
561 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
564 startPos
= obj
->GetRange().GetStart();
565 endPos
= obj
->GetRange().GetEnd();
568 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
569 InitCommandEvent(urlEvent
);
571 urlEvent
.SetString(urlTarget
);
573 GetEventHandler()->ProcessEvent(urlEvent
);
583 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
587 dc
.SetFont(GetFont());
590 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
591 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
593 // See if we need to change the cursor
596 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
599 if (GetStyle(position
, attr
))
601 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
603 SetCursor(m_urlCursor
);
605 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
607 SetCursor(m_textCursor
);
612 SetCursor(m_textCursor
);
615 if (!event
.Dragging())
621 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
623 // TODO: test closeness
625 bool caretAtLineStart
= false;
627 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
629 // If we're at the start of a line (but not first in para)
630 // then we should keep the caret showing at the start of the line
631 // by showing the m_caretAtLineStart flag.
632 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
633 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
635 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
636 caretAtLineStart
= true;
640 if (m_caretPosition
!= position
)
642 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
644 MoveCaret(position
, caretAtLineStart
);
645 SetDefaultStyleToCursorStyle();
651 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
655 wxRichTextEvent
cmdEvent(
656 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
658 cmdEvent
.SetEventObject(this);
659 cmdEvent
.SetPosition(m_caretPosition
+1);
661 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
665 /// Left-double-click
666 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
668 wxRichTextEvent
cmdEvent(
669 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
671 cmdEvent
.SetEventObject(this);
672 cmdEvent
.SetPosition(m_caretPosition
+1);
674 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
676 SelectWord(GetCaretPosition()+1);
681 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
683 wxRichTextEvent
cmdEvent(
684 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
686 cmdEvent
.SetEventObject(this);
687 cmdEvent
.SetPosition(m_caretPosition
+1);
689 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
694 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
698 flags
|= wxRICHTEXT_CTRL_DOWN
;
699 if (event
.ShiftDown())
700 flags
|= wxRICHTEXT_SHIFT_DOWN
;
702 flags
|= wxRICHTEXT_ALT_DOWN
;
704 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
706 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
708 KeyboardNavigate(event
.GetKeyCode(), flags
);
712 long keycode
= event
.GetKeyCode();
772 case WXK_NUMPAD_HOME
:
773 case WXK_NUMPAD_LEFT
:
775 case WXK_NUMPAD_RIGHT
:
776 case WXK_NUMPAD_DOWN
:
777 case WXK_NUMPAD_PAGEUP
:
778 case WXK_NUMPAD_PAGEDOWN
:
780 case WXK_NUMPAD_BEGIN
:
781 case WXK_NUMPAD_INSERT
:
782 case WXK_WINDOWS_LEFT
:
791 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
792 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
794 BeginBatchUndo(_("Delete Text"));
796 long newPos
= m_caretPosition
;
798 bool processed
= DeleteSelectedContent(& newPos
);
800 // Submit range in character positions, which are greater than caret positions,
801 // so subtract 1 for deleted character and add 1 for conversion to character position.
806 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
809 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
815 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
820 if (GetLastPosition() == -1)
824 m_caretPosition
= -1;
826 SetDefaultStyleToCursorStyle();
829 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
831 wxRichTextEvent
cmdEvent(
832 wxEVT_COMMAND_RICHTEXT_DELETE
,
834 cmdEvent
.SetEventObject(this);
835 cmdEvent
.SetFlags(flags
);
836 cmdEvent
.SetPosition(m_caretPosition
+1);
837 GetEventHandler()->ProcessEvent(cmdEvent
);
847 // all the other keys modify the controls contents which shouldn't be
848 // possible if we're read-only
855 if (event
.GetKeyCode() == WXK_RETURN
)
857 BeginBatchUndo(_("Insert Text"));
859 long newPos
= m_caretPosition
;
861 DeleteSelectedContent(& newPos
);
863 if (event
.ShiftDown())
866 text
= wxRichTextLineBreakChar
;
867 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
868 m_caretAtLineStart
= true;
872 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
875 SetDefaultStyleToCursorStyle();
877 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
879 wxRichTextEvent
cmdEvent(
880 wxEVT_COMMAND_RICHTEXT_RETURN
,
882 cmdEvent
.SetEventObject(this);
883 cmdEvent
.SetFlags(flags
);
884 cmdEvent
.SetPosition(newPos
+1);
886 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
888 // Generate conventional event
889 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
890 InitCommandEvent(textEvent
);
892 GetEventHandler()->ProcessEvent(textEvent
);
896 else if (event
.GetKeyCode() == WXK_BACK
)
898 BeginBatchUndo(_("Delete Text"));
900 long newPos
= m_caretPosition
;
902 bool processed
= DeleteSelectedContent(& newPos
);
904 // Submit range in character positions, which are greater than caret positions,
905 // so subtract 1 for deleted character and add 1 for conversion to character position.
910 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
913 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
919 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
924 if (GetLastPosition() == -1)
928 m_caretPosition
= -1;
930 SetDefaultStyleToCursorStyle();
933 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
935 wxRichTextEvent
cmdEvent(
936 wxEVT_COMMAND_RICHTEXT_DELETE
,
938 cmdEvent
.SetEventObject(this);
939 cmdEvent
.SetFlags(flags
);
940 cmdEvent
.SetPosition(m_caretPosition
+1);
941 GetEventHandler()->ProcessEvent(cmdEvent
);
945 else if (event
.GetKeyCode() == WXK_DELETE
)
947 BeginBatchUndo(_("Delete Text"));
949 long newPos
= m_caretPosition
;
951 bool processed
= DeleteSelectedContent(& newPos
);
953 // Submit range in character positions, which are greater than caret positions,
954 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
958 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
959 if (pos
!= -1 && (pos
> newPos
))
961 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
967 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
972 if (GetLastPosition() == -1)
976 m_caretPosition
= -1;
978 SetDefaultStyleToCursorStyle();
981 wxRichTextEvent
cmdEvent(
982 wxEVT_COMMAND_RICHTEXT_DELETE
,
984 cmdEvent
.SetEventObject(this);
985 cmdEvent
.SetFlags(flags
);
986 cmdEvent
.SetPosition(m_caretPosition
+1);
987 GetEventHandler()->ProcessEvent(cmdEvent
);
993 long keycode
= event
.GetKeyCode();
1005 if (event
.CmdDown())
1007 // Fixes AltGr+key with European input languages on Windows
1008 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1015 wxRichTextEvent
cmdEvent(
1016 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1018 cmdEvent
.SetEventObject(this);
1019 cmdEvent
.SetFlags(flags
);
1021 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1023 cmdEvent
.SetCharacter((wxChar
) keycode
);
1025 cmdEvent
.SetPosition(m_caretPosition
+1);
1027 if (keycode
== wxT('\t'))
1029 // See if we need to promote or demote the selection or paragraph at the cursor
1030 // position, instead of inserting a tab.
1031 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1032 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1033 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1035 wxRichTextRange range
;
1037 range
= GetSelectionRange();
1039 range
= para
->GetRange().FromInternal();
1041 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1043 PromoteList(promoteBy
, range
, NULL
);
1045 GetEventHandler()->ProcessEvent(cmdEvent
);
1051 BeginBatchUndo(_("Insert Text"));
1053 long newPos
= m_caretPosition
;
1054 DeleteSelectedContent(& newPos
);
1057 wxString str
= event
.GetUnicodeKey();
1059 wxString str
= (wxChar
) event
.GetKeyCode();
1061 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1065 SetDefaultStyleToCursorStyle();
1066 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1068 GetEventHandler()->ProcessEvent(cmdEvent
);
1076 /// Delete content if there is a selection, e.g. when pressing a key.
1077 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1081 long pos
= m_selectionRange
.GetStart();
1082 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1083 m_selectionRange
.SetRange(-2, -2);
1093 /// Keyboard navigation
1097 Left: left one character
1098 Right: right one character
1101 Ctrl-Left: left one word
1102 Ctrl-Right: right one word
1103 Ctrl-Up: previous paragraph start
1104 Ctrl-Down: next start of paragraph
1107 Ctrl-Home: start of document
1108 Ctrl-End: end of document
1109 Page-Up: Up a screen
1110 Page-Down: Down a screen
1114 Ctrl-Alt-PgUp: Start of window
1115 Ctrl-Alt-PgDn: End of window
1116 F8: Start selection mode
1117 Esc: End selection mode
1119 Adding Shift does the above but starts/extends selection.
1124 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1126 bool success
= false;
1128 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1130 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1131 success
= WordRight(1, flags
);
1133 success
= MoveRight(1, flags
);
1135 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1137 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1138 success
= WordLeft(1, flags
);
1140 success
= MoveLeft(1, flags
);
1142 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1144 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1145 success
= MoveToParagraphStart(flags
);
1147 success
= MoveUp(1, flags
);
1149 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1151 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1152 success
= MoveToParagraphEnd(flags
);
1154 success
= MoveDown(1, flags
);
1156 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1158 success
= PageUp(1, flags
);
1160 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1162 success
= PageDown(1, flags
);
1164 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1166 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1167 success
= MoveHome(flags
);
1169 success
= MoveToLineStart(flags
);
1171 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1173 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1174 success
= MoveEnd(flags
);
1176 success
= MoveToLineEnd(flags
);
1181 ScrollIntoView(m_caretPosition
, keyCode
);
1182 SetDefaultStyleToCursorStyle();
1188 /// Extend the selection. Selections are in caret positions.
1189 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1191 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1193 if (oldPos
== newPos
)
1196 wxRichTextRange oldSelection
= m_selectionRange
;
1198 // If not currently selecting, start selecting
1199 if (m_selectionRange
.GetStart() == -2)
1201 m_selectionAnchor
= oldPos
;
1203 if (oldPos
> newPos
)
1204 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1206 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1210 // Always ensure that the selection range start is greater than
1212 if (newPos
> m_selectionAnchor
)
1213 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1214 else if (newPos
== m_selectionAnchor
)
1215 m_selectionRange
= wxRichTextRange(-2, -2);
1217 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1220 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1222 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1224 wxLogDebug(wxT("Strange selection range"));
1233 /// Scroll into view, returning true if we scrolled.
1234 /// This takes a _caret_ position.
1235 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1237 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1243 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1245 int startXUnits
, startYUnits
;
1246 GetViewStart(& startXUnits
, & startYUnits
);
1247 int startY
= startYUnits
* ppuY
;
1250 GetVirtualSize(& sx
, & sy
);
1256 wxRect rect
= line
->GetRect();
1258 bool scrolled
= false;
1260 wxSize clientSize
= GetClientSize();
1261 clientSize
.y
-= GetBuffer().GetBottomMargin();
1263 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1265 int y
= rect
.y
- GetClientSize().y
/2;
1266 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1267 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1269 if (startYUnits
!= yUnits
)
1271 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1274 #if !wxRICHTEXT_USE_OWN_CARET
1284 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1285 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1286 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1287 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1289 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1291 // Make it scroll so this item is at the bottom
1293 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1294 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1296 // If we're still off the screen, scroll another line down
1297 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1300 if (startYUnits
!= yUnits
)
1302 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1306 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1308 // Make it scroll so this item is at the top
1310 int y
= rect
.y
- GetBuffer().GetTopMargin();
1311 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1313 if (startYUnits
!= yUnits
)
1315 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1321 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1322 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1323 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1324 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1326 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1328 // Make it scroll so this item is at the top
1330 int y
= rect
.y
- GetBuffer().GetTopMargin();
1331 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1333 if (startYUnits
!= yUnits
)
1335 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1339 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1341 // Make it scroll so this item is at the bottom
1343 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1344 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1346 // If we're still off the screen, scroll another line down
1347 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1350 if (startYUnits
!= yUnits
)
1352 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1358 #if !wxRICHTEXT_USE_OWN_CARET
1366 /// Is the given position visible on the screen?
1367 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1369 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1375 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1378 GetViewStart(& startX
, & startY
);
1380 startY
= startY
* ppuY
;
1382 wxRect rect
= line
->GetRect();
1383 wxSize clientSize
= GetClientSize();
1384 clientSize
.y
-= GetBuffer().GetBottomMargin();
1386 return (rect
.GetBottom() > (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1389 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1391 m_caretPosition
= position
;
1392 m_caretAtLineStart
= showAtLineStart
;
1395 /// Move caret one visual step forward: this may mean setting a flag
1396 /// and keeping the same position if we're going from the end of one line
1397 /// to the start of the next, which may be the exact same caret position.
1398 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1400 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1402 // Only do the check if we're not at the end of the paragraph (where things work OK
1404 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1406 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1410 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1412 // We're at the end of a line. See whether we need to
1413 // stay at the same actual caret position but change visual
1414 // position, or not.
1415 if (oldPosition
== lineRange
.GetEnd())
1417 if (m_caretAtLineStart
)
1419 // We're already at the start of the line, so actually move on now.
1420 m_caretPosition
= oldPosition
+ 1;
1421 m_caretAtLineStart
= false;
1425 // We're showing at the end of the line, so keep to
1426 // the same position but indicate that we're to show
1427 // at the start of the next line.
1428 m_caretPosition
= oldPosition
;
1429 m_caretAtLineStart
= true;
1431 SetDefaultStyleToCursorStyle();
1437 SetDefaultStyleToCursorStyle();
1440 /// Move caret one visual step backward: this may mean setting a flag
1441 /// and keeping the same position if we're going from the end of one line
1442 /// to the start of the next, which may be the exact same caret position.
1443 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1445 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1447 // Only do the check if we're not at the start of the paragraph (where things work OK
1449 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1451 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1455 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1457 // We're at the start of a line. See whether we need to
1458 // stay at the same actual caret position but change visual
1459 // position, or not.
1460 if (oldPosition
== lineRange
.GetStart())
1462 m_caretPosition
= oldPosition
-1;
1463 m_caretAtLineStart
= true;
1466 else if (oldPosition
== lineRange
.GetEnd())
1468 if (m_caretAtLineStart
)
1470 // We're at the start of the line, so keep the same caret position
1471 // but clear the start-of-line flag.
1472 m_caretPosition
= oldPosition
;
1473 m_caretAtLineStart
= false;
1477 // We're showing at the end of the line, so go back
1478 // to the previous character position.
1479 m_caretPosition
= oldPosition
- 1;
1481 SetDefaultStyleToCursorStyle();
1487 SetDefaultStyleToCursorStyle();
1491 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1493 long endPos
= GetBuffer().GetRange().GetEnd();
1495 if (m_caretPosition
+ noPositions
< endPos
)
1497 long oldPos
= m_caretPosition
;
1498 long newPos
= m_caretPosition
+ noPositions
;
1500 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1504 // Determine by looking at oldPos and m_caretPosition whether
1505 // we moved from the end of a line to the start of the next line, in which case
1506 // we want to adjust the caret position such that it is positioned at the
1507 // start of the next line, rather than jumping past the first character of the
1509 if (noPositions
== 1 && !extendSel
)
1510 MoveCaretForward(oldPos
);
1512 SetCaretPosition(newPos
);
1515 SetDefaultStyleToCursorStyle();
1524 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1528 if (m_caretPosition
> startPos
- noPositions
+ 1)
1530 long oldPos
= m_caretPosition
;
1531 long newPos
= m_caretPosition
- noPositions
;
1532 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1536 if (noPositions
== 1 && !extendSel
)
1537 MoveCaretBack(oldPos
);
1539 SetCaretPosition(newPos
);
1542 SetDefaultStyleToCursorStyle();
1551 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1553 return MoveDown(- noLines
, flags
);
1557 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1562 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1563 wxPoint pt
= GetCaret()->GetPosition();
1564 long newLine
= lineNumber
+ noLines
;
1566 if (lineNumber
!= -1)
1570 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1572 if (newLine
> lastLine
)
1582 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1585 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1591 wxClientDC
dc(this);
1593 dc
.SetFont(GetFont());
1595 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1597 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1599 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1600 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1601 // so we view the caret at the start of the line.
1602 bool caretLineStart
= false;
1603 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1605 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1606 wxRichTextRange lineRange
;
1608 lineRange
= thisLine
->GetAbsoluteRange();
1610 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1613 caretLineStart
= true;
1617 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1618 if (para
&& para
->GetRange().GetStart() == newPos
)
1623 long newSelEnd
= newPos
;
1625 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1629 SetCaretPosition(newPos
, caretLineStart
);
1631 SetDefaultStyleToCursorStyle();
1639 /// Move to the end of the paragraph
1640 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1642 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1645 long newPos
= para
->GetRange().GetEnd() - 1;
1646 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1650 SetCaretPosition(newPos
);
1652 SetDefaultStyleToCursorStyle();
1660 /// Move to the start of the paragraph
1661 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1663 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1666 long newPos
= para
->GetRange().GetStart() - 1;
1667 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1671 SetCaretPosition(newPos
);
1673 SetDefaultStyleToCursorStyle();
1681 /// Move to the end of the line
1682 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1684 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1688 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1689 long newPos
= lineRange
.GetEnd();
1690 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1694 SetCaretPosition(newPos
);
1696 SetDefaultStyleToCursorStyle();
1704 /// Move to the start of the line
1705 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1707 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1710 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1711 long newPos
= lineRange
.GetStart()-1;
1713 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1717 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1719 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1721 SetDefaultStyleToCursorStyle();
1729 /// Move to the start of the buffer
1730 bool wxRichTextCtrl::MoveHome(int flags
)
1732 if (m_caretPosition
!= -1)
1734 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1738 SetCaretPosition(-1);
1740 SetDefaultStyleToCursorStyle();
1748 /// Move to the end of the buffer
1749 bool wxRichTextCtrl::MoveEnd(int flags
)
1751 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1753 if (m_caretPosition
!= endPos
)
1755 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1759 SetCaretPosition(endPos
);
1761 SetDefaultStyleToCursorStyle();
1769 /// Move noPages pages up
1770 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1772 return PageDown(- noPages
, flags
);
1775 /// Move noPages pages down
1776 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1778 // Calculate which line occurs noPages * screen height further down.
1779 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1782 wxSize clientSize
= GetClientSize();
1783 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1785 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1788 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1789 long pos
= lineRange
.GetStart()-1;
1790 if (pos
!= m_caretPosition
)
1792 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1794 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1798 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1800 SetDefaultStyleToCursorStyle();
1810 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1812 return str
== wxT(" ") || str
== wxT("\t");
1815 // Finds the caret position for the next word
1816 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1818 long endPos
= GetBuffer().GetRange().GetEnd();
1822 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1824 // First skip current text to space
1825 while (i
< endPos
&& i
> -1)
1827 // i is in character, not caret positions
1828 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1829 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1830 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1834 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1841 while (i
< endPos
&& i
> -1)
1843 // i is in character, not caret positions
1844 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1845 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1846 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1847 return wxMax(-1, i
);
1849 if (text
.empty()) // End of paragraph, or maybe an image
1850 return wxMax(-1, i
- 1);
1851 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1855 // Convert to caret position
1856 return wxMax(-1, i
- 1);
1865 long i
= m_caretPosition
;
1867 // First skip white space
1868 while (i
< endPos
&& i
> -1)
1870 // i is in character, not caret positions
1871 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1872 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1874 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1876 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1881 // Next skip current text to space
1882 while (i
< endPos
&& i
> -1)
1884 // i is in character, not caret positions
1885 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1886 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1887 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1890 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1903 /// Move n words left
1904 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1906 long pos
= FindNextWordPosition(-1);
1907 if (pos
!= m_caretPosition
)
1909 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1911 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1915 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1917 SetDefaultStyleToCursorStyle();
1925 /// Move n words right
1926 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1928 long pos
= FindNextWordPosition(1);
1929 if (pos
!= m_caretPosition
)
1931 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1933 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1937 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1939 SetDefaultStyleToCursorStyle();
1948 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1950 // Only do sizing optimization for large buffers
1951 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1953 m_fullLayoutRequired
= true;
1954 m_fullLayoutTime
= wxGetLocalTimeMillis();
1955 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1956 LayoutContent(true /* onlyVisibleRect */);
1959 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1961 #if wxRICHTEXT_BUFFERED_PAINTING
1969 /// Idle-time processing
1970 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1972 #if wxRICHTEXT_USE_OWN_CARET
1973 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1975 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1981 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1983 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1985 m_fullLayoutRequired
= false;
1986 m_fullLayoutTime
= 0;
1987 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1988 ShowPosition(m_fullLayoutSavedPosition
);
1992 if (m_caretPositionForDefaultStyle
!= -2)
1994 // If the caret position has changed, no longer reflect the default style
1996 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1997 m_caretPositionForDefaultStyle
= -2;
2004 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2006 #if wxRICHTEXT_USE_OWN_CARET
2007 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2010 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2017 /// Set up scrollbars, e.g. after a resize
2018 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2023 if (GetBuffer().IsEmpty())
2025 SetScrollbars(0, 0, 0, 0, 0, 0);
2029 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2030 // of pixels. See e.g. wxVScrolledWindow for ideas.
2031 int pixelsPerUnit
= 5;
2032 wxSize clientSize
= GetClientSize();
2034 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2036 // Round up so we have at least maxHeight pixels
2037 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2039 int startX
= 0, startY
= 0;
2041 GetViewStart(& startX
, & startY
);
2043 int maxPositionX
= 0;
2044 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2046 int newStartX
= wxMin(maxPositionX
, startX
);
2047 int newStartY
= wxMin(maxPositionY
, startY
);
2049 int oldPPUX
, oldPPUY
;
2050 int oldStartX
, oldStartY
;
2051 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2052 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2053 GetViewStart(& oldStartX
, & oldStartY
);
2054 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2056 oldVirtualSizeY
/= oldPPUY
;
2058 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2061 // Don't set scrollbars if there were none before, and there will be none now.
2062 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2065 // Move to previous scroll position if
2067 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2070 /// Paint the background
2071 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2073 wxColour backgroundColour
= GetBackgroundColour();
2074 if (!backgroundColour
.Ok())
2075 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2077 // Clear the background
2078 dc
.SetBrush(wxBrush(backgroundColour
));
2079 dc
.SetPen(*wxTRANSPARENT_PEN
);
2080 wxRect
windowRect(GetClientSize());
2081 windowRect
.x
-= 2; windowRect
.y
-= 2;
2082 windowRect
.width
+= 4; windowRect
.height
+= 4;
2084 // We need to shift the rectangle to take into account
2085 // scrolling. Converting device to logical coordinates.
2086 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2087 dc
.DrawRectangle(windowRect
);
2090 #if wxRICHTEXT_BUFFERED_PAINTING
2091 /// Recreate buffer bitmap if necessary
2092 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2095 if (sz
== wxDefaultSize
)
2096 sz
= GetClientSize();
2098 if (sz
.x
< 1 || sz
.y
< 1)
2101 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2102 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2103 return m_bufferBitmap
.Ok();
2107 // ----------------------------------------------------------------------------
2108 // file IO functions
2109 // ----------------------------------------------------------------------------
2111 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2113 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2115 m_filename
= filename
;
2118 SetInsertionPoint(0);
2121 SetupScrollbars(true);
2123 wxTextCtrl::SendTextUpdatedEvent(this);
2129 wxLogError(_("File couldn't be loaded."));
2135 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2137 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2139 m_filename
= filename
;
2146 wxLogError(_("The text couldn't be saved."));
2151 // ----------------------------------------------------------------------------
2152 // wxRichTextCtrl specific functionality
2153 // ----------------------------------------------------------------------------
2155 /// Add a new paragraph of text to the end of the buffer
2156 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2158 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2164 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2166 wxRichTextRange range
= GetBuffer().AddImage(image
);
2171 // ----------------------------------------------------------------------------
2172 // selection and ranges
2173 // ----------------------------------------------------------------------------
2175 void wxRichTextCtrl::SelectAll()
2177 SetSelection(0, GetLastPosition()+1);
2178 m_selectionAnchor
= -1;
2182 void wxRichTextCtrl::SelectNone()
2184 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2186 wxRichTextRange oldSelection
= m_selectionRange
;
2188 m_selectionRange
= wxRichTextRange(-2, -2);
2190 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2192 m_selectionAnchor
= -2;
2195 static bool wxIsWordDelimiter(const wxString
& text
)
2197 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2200 /// Select the word at the given character position
2201 bool wxRichTextCtrl::SelectWord(long position
)
2203 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2206 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2210 if (position
== para
->GetRange().GetEnd())
2213 long positionStart
= position
;
2214 long positionEnd
= position
;
2216 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2218 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2219 if (wxIsWordDelimiter(text
))
2225 if (positionStart
< para
->GetRange().GetStart())
2226 positionStart
= para
->GetRange().GetStart();
2228 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2230 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2231 if (wxIsWordDelimiter(text
))
2237 if (positionEnd
>= para
->GetRange().GetEnd())
2238 positionEnd
= para
->GetRange().GetEnd();
2240 if (positionEnd
< positionStart
)
2243 SetSelection(positionStart
, positionEnd
+1);
2245 if (positionStart
>= 0)
2247 MoveCaret(positionStart
-1, true);
2248 SetDefaultStyleToCursorStyle();
2254 wxString
wxRichTextCtrl::GetStringSelection() const
2257 GetSelection(&from
, &to
);
2259 return GetRange(from
, to
);
2262 // ----------------------------------------------------------------------------
2264 // ----------------------------------------------------------------------------
2266 wxTextCtrlHitTestResult
2267 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2269 // implement in terms of the other overload as the native ports typically
2270 // can get the position and not (x, y) pair directly (although wxUniv
2271 // directly gets x and y -- and so overrides this method as well)
2273 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2275 if ( rc
!= wxTE_HT_UNKNOWN
)
2277 PositionToXY(pos
, x
, y
);
2283 wxTextCtrlHitTestResult
2284 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2287 wxClientDC
dc((wxRichTextCtrl
*) this);
2288 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2290 // Buffer uses logical position (relative to start of buffer)
2292 wxPoint pt2
= GetLogicalPoint(pt
);
2294 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2296 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2297 return wxTE_HT_BEFORE
;
2298 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2299 return wxTE_HT_BEYOND
;
2300 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2301 return wxTE_HT_ON_TEXT
;
2303 return wxTE_HT_UNKNOWN
;
2306 // ----------------------------------------------------------------------------
2307 // set/get the controls text
2308 // ----------------------------------------------------------------------------
2310 wxString
wxRichTextCtrl::DoGetValue() const
2312 return GetBuffer().GetText();
2315 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2317 // Public API for range is different from internals
2318 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2321 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2323 // Don't call Clear here, since it always sends a text updated event
2324 m_buffer
.ResetAndClearCommands();
2325 m_buffer
.SetDirty(true);
2326 m_caretPosition
= -1;
2327 m_caretPositionForDefaultStyle
= -2;
2328 m_caretAtLineStart
= false;
2329 m_selectionRange
.SetRange(-2, -2);
2339 if (!value
.IsEmpty())
2341 // Remove empty paragraph
2342 GetBuffer().Clear();
2343 DoWriteText(value
, flags
);
2345 // for compatibility, don't move the cursor when doing SetValue()
2346 SetInsertionPoint(0);
2350 // still send an event for consistency
2351 if (flags
& SetValue_SendEvent
)
2352 wxTextCtrl::SendTextUpdatedEvent(this);
2357 void wxRichTextCtrl::WriteText(const wxString
& value
)
2362 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2364 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2366 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2368 if ( flags
& SetValue_SendEvent
)
2369 wxTextCtrl::SendTextUpdatedEvent(this);
2372 void wxRichTextCtrl::AppendText(const wxString
& text
)
2374 SetInsertionPointEnd();
2379 /// Write an image at the current insertion point
2380 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2382 wxRichTextImageBlock imageBlock
;
2384 wxImage image2
= image
;
2385 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2386 return WriteImage(imageBlock
);
2391 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2393 wxRichTextImageBlock imageBlock
;
2396 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2397 return WriteImage(imageBlock
);
2402 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2404 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2407 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2411 wxRichTextImageBlock imageBlock
;
2413 wxImage image
= bitmap
.ConvertToImage();
2414 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2415 return WriteImage(imageBlock
);
2421 /// Insert a newline (actually paragraph) at the current insertion point.
2422 bool wxRichTextCtrl::Newline()
2424 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2427 /// Insert a line break at the current insertion point.
2428 bool wxRichTextCtrl::LineBreak()
2431 text
= wxRichTextLineBreakChar
;
2432 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2435 // ----------------------------------------------------------------------------
2436 // Clipboard operations
2437 // ----------------------------------------------------------------------------
2439 void wxRichTextCtrl::Copy()
2443 wxRichTextRange range
= GetInternalSelectionRange();
2444 GetBuffer().CopyToClipboard(range
);
2448 void wxRichTextCtrl::Cut()
2452 wxRichTextRange range
= GetInternalSelectionRange();
2453 GetBuffer().CopyToClipboard(range
);
2455 DeleteSelectedContent();
2461 void wxRichTextCtrl::Paste()
2465 BeginBatchUndo(_("Paste"));
2467 long newPos
= m_caretPosition
;
2468 DeleteSelectedContent(& newPos
);
2470 GetBuffer().PasteFromClipboard(newPos
);
2476 void wxRichTextCtrl::DeleteSelection()
2478 if (CanDeleteSelection())
2480 DeleteSelectedContent();
2484 bool wxRichTextCtrl::HasSelection() const
2486 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2489 bool wxRichTextCtrl::CanCopy() const
2491 // Can copy if there's a selection
2492 return HasSelection();
2495 bool wxRichTextCtrl::CanCut() const
2497 return HasSelection() && IsEditable();
2500 bool wxRichTextCtrl::CanPaste() const
2502 if ( !IsEditable() )
2505 return GetBuffer().CanPasteFromClipboard();
2508 bool wxRichTextCtrl::CanDeleteSelection() const
2510 return HasSelection() && IsEditable();
2514 // ----------------------------------------------------------------------------
2516 // ----------------------------------------------------------------------------
2518 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
2520 if (m_contextMenu
&& m_contextMenu
!= menu
)
2521 delete m_contextMenu
;
2522 m_contextMenu
= menu
;
2525 void wxRichTextCtrl::SetEditable(bool editable
)
2527 m_editable
= editable
;
2530 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2534 m_caretPosition
= pos
- 1;
2538 SetDefaultStyleToCursorStyle();
2541 void wxRichTextCtrl::SetInsertionPointEnd()
2543 long pos
= GetLastPosition();
2544 SetInsertionPoint(pos
);
2547 long wxRichTextCtrl::GetInsertionPoint() const
2549 return m_caretPosition
+1;
2552 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2554 return GetBuffer().GetRange().GetEnd();
2557 // If the return values from and to are the same, there is no
2559 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2561 *from
= m_selectionRange
.GetStart();
2562 *to
= m_selectionRange
.GetEnd();
2563 if ((*to
) != -1 && (*to
) != -2)
2567 bool wxRichTextCtrl::IsEditable() const
2572 // ----------------------------------------------------------------------------
2574 // ----------------------------------------------------------------------------
2576 void wxRichTextCtrl::SetSelection(long from
, long to
)
2578 // if from and to are both -1, it means (in wxWidgets) that all text should
2580 if ( (from
== -1) && (to
== -1) )
2583 to
= GetLastPosition()+1;
2592 wxRichTextRange oldSelection
= m_selectionRange
;
2593 m_selectionAnchor
= from
;
2594 m_selectionRange
.SetRange(from
, to
-1);
2596 m_caretPosition
= from
-1;
2598 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2603 // ----------------------------------------------------------------------------
2605 // ----------------------------------------------------------------------------
2607 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2608 const wxString
& value
)
2610 BeginBatchUndo(_("Replace"));
2612 DeleteSelectedContent();
2614 DoWriteText(value
, SetValue_SelectionOnly
);
2619 void wxRichTextCtrl::Remove(long from
, long to
)
2623 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2630 bool wxRichTextCtrl::IsModified() const
2632 return m_buffer
.IsModified();
2635 void wxRichTextCtrl::MarkDirty()
2637 m_buffer
.Modify(true);
2640 void wxRichTextCtrl::DiscardEdits()
2642 m_caretPositionForDefaultStyle
= -2;
2643 m_buffer
.Modify(false);
2644 m_buffer
.GetCommandProcessor()->ClearCommands();
2647 int wxRichTextCtrl::GetNumberOfLines() const
2649 return GetBuffer().GetParagraphCount();
2652 // ----------------------------------------------------------------------------
2653 // Positions <-> coords
2654 // ----------------------------------------------------------------------------
2656 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2658 return GetBuffer().XYToPosition(x
, y
);
2661 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2663 return GetBuffer().PositionToXY(pos
, x
, y
);
2666 // ----------------------------------------------------------------------------
2668 // ----------------------------------------------------------------------------
2670 void wxRichTextCtrl::ShowPosition(long pos
)
2672 if (!IsPositionVisible(pos
))
2673 ScrollIntoView(pos
-1, WXK_DOWN
);
2676 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2678 return GetBuffer().GetParagraphLength(lineNo
);
2681 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2683 return GetBuffer().GetParagraphText(lineNo
);
2686 // ----------------------------------------------------------------------------
2688 // ----------------------------------------------------------------------------
2690 void wxRichTextCtrl::Undo()
2694 GetCommandProcessor()->Undo();
2698 void wxRichTextCtrl::Redo()
2702 GetCommandProcessor()->Redo();
2706 bool wxRichTextCtrl::CanUndo() const
2708 return GetCommandProcessor()->CanUndo();
2711 bool wxRichTextCtrl::CanRedo() const
2713 return GetCommandProcessor()->CanRedo();
2716 // ----------------------------------------------------------------------------
2717 // implementation details
2718 // ----------------------------------------------------------------------------
2720 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2722 SetValue(event
.GetString());
2723 GetEventHandler()->ProcessEvent(event
);
2726 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2728 // By default, load the first file into the text window.
2729 if (event
.GetNumberOfFiles() > 0)
2731 LoadFile(event
.GetFiles()[0]);
2735 wxSize
wxRichTextCtrl::DoGetBestSize() const
2737 return wxSize(10, 10);
2740 // ----------------------------------------------------------------------------
2741 // standard handlers for standard edit menu events
2742 // ----------------------------------------------------------------------------
2744 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2749 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2754 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2759 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2764 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2769 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2774 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2776 event
.Enable( CanCut() );
2779 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2781 event
.Enable( CanCopy() );
2784 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2786 event
.Enable( CanDeleteSelection() );
2789 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2791 event
.Enable( CanPaste() );
2794 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2796 event
.Enable( CanUndo() );
2797 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2800 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2802 event
.Enable( CanRedo() );
2803 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2806 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2811 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2813 event
.Enable(GetLastPosition() > 0);
2816 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2818 if (event
.GetEventObject() != this)
2825 PopupMenu(m_contextMenu
);
2829 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2831 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2834 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2836 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2839 // extended style setting operation with flags including:
2840 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2841 // see richtextbuffer.h for more details.
2843 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2845 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2848 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2850 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2853 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2855 return GetBuffer().GetDefaultStyle();
2858 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2860 return GetBuffer().GetStyle(position
, style
);
2863 // get the common set of styles for the range
2864 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2866 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2869 /// Get the content (uncombined) attributes for this position.
2870 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2872 return GetBuffer().GetUncombinedStyle(position
, style
);
2875 /// Set font, and also the buffer attributes
2876 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2878 wxTextCtrlBase::SetFont(font
);
2880 wxTextAttr attr
= GetBuffer().GetAttributes();
2882 GetBuffer().SetBasicStyle(attr
);
2884 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2890 /// Transform logical to physical
2891 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2894 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2899 /// Transform physical to logical
2900 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2903 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2908 /// Position the caret
2909 void wxRichTextCtrl::PositionCaret()
2914 //wxLogDebug(wxT("PositionCaret"));
2917 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2919 wxPoint newPt
= caretRect
.GetPosition();
2920 wxSize newSz
= caretRect
.GetSize();
2921 wxPoint pt
= GetPhysicalPoint(newPt
);
2922 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2925 if (GetCaret()->GetSize() != newSz
)
2926 GetCaret()->SetSize(newSz
);
2928 int halfSize
= newSz
.y
/2;
2929 // If the caret is beyond the margin, hide it by moving it out of the way
2930 if (((pt
.y
+ halfSize
) < GetBuffer().GetTopMargin()) || ((pt
.y
+ halfSize
) > (GetClientSize().y
- GetBuffer().GetBottomMargin())))
2933 GetCaret()->Move(pt
);
2939 /// Get the caret height and position for the given character position
2940 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2942 wxClientDC
dc(this);
2943 dc
.SetFont(GetFont());
2950 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2952 // Caret height can't be zero
2954 height
= dc
.GetCharHeight();
2956 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2963 /// Gets the line for the visible caret position. If the caret is
2964 /// shown at the very end of the line, it means the next character is actually
2965 /// on the following line. So let's get the line we're expecting to find
2966 /// if this is the case.
2967 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2969 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2970 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2973 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2974 if (caretPosition
== lineRange
.GetStart()-1 &&
2975 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2977 if (!m_caretAtLineStart
)
2978 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2985 /// Move the caret to the given character position
2986 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2988 if (GetBuffer().GetDirty())
2991 if (pos
<= GetBuffer().GetRange().GetEnd())
2993 SetCaretPosition(pos
, showAtLineStart
);
3003 /// Layout the buffer: which we must do before certain operations, such as
3004 /// setting the caret position.
3005 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3007 if (GetBuffer().GetDirty() || onlyVisibleRect
)
3009 wxRect
availableSpace(GetClientSize());
3010 if (availableSpace
.width
== 0)
3011 availableSpace
.width
= 10;
3012 if (availableSpace
.height
== 0)
3013 availableSpace
.height
= 10;
3015 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3016 if (onlyVisibleRect
)
3018 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3019 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3022 wxClientDC
dc(this);
3023 dc
.SetFont(GetFont());
3027 GetBuffer().Defragment();
3028 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3029 GetBuffer().Layout(dc
, availableSpace
, flags
);
3030 GetBuffer().SetDirty(false);
3039 /// Is all of the selection bold?
3040 bool wxRichTextCtrl::IsSelectionBold()
3045 wxRichTextRange range
= GetSelectionRange();
3046 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3047 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3049 return HasCharacterAttributes(range
, attr
);
3053 // If no selection, then we need to combine current style with default style
3054 // to see what the effect would be if we started typing.
3056 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3058 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3059 if (GetStyle(pos
, attr
))
3061 if (IsDefaultStyleShowing())
3062 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3063 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3069 /// Is all of the selection italics?
3070 bool wxRichTextCtrl::IsSelectionItalics()
3074 wxRichTextRange range
= GetSelectionRange();
3076 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3077 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3079 return HasCharacterAttributes(range
, attr
);
3083 // If no selection, then we need to combine current style with default style
3084 // to see what the effect would be if we started typing.
3086 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3088 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3089 if (GetStyle(pos
, attr
))
3091 if (IsDefaultStyleShowing())
3092 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3093 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3099 /// Is all of the selection underlined?
3100 bool wxRichTextCtrl::IsSelectionUnderlined()
3104 wxRichTextRange range
= GetSelectionRange();
3106 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3107 attr
.SetFontUnderlined(true);
3109 return HasCharacterAttributes(range
, attr
);
3113 // If no selection, then we need to combine current style with default style
3114 // to see what the effect would be if we started typing.
3116 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3117 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3119 if (GetStyle(pos
, attr
))
3121 if (IsDefaultStyleShowing())
3122 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3123 return attr
.GetFontUnderlined();
3129 /// Apply bold to the selection
3130 bool wxRichTextCtrl::ApplyBoldToSelection()
3133 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3134 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3137 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3140 wxRichTextAttr current
= GetDefaultStyleEx();
3141 current
.Apply(attr
);
3142 SetAndShowDefaultStyle(current
);
3147 /// Apply italic to the selection
3148 bool wxRichTextCtrl::ApplyItalicToSelection()
3151 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3152 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3155 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3158 wxRichTextAttr current
= GetDefaultStyleEx();
3159 current
.Apply(attr
);
3160 SetAndShowDefaultStyle(current
);
3165 /// Apply underline to the selection
3166 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3169 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3170 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3173 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3176 wxRichTextAttr current
= GetDefaultStyleEx();
3177 current
.Apply(attr
);
3178 SetAndShowDefaultStyle(current
);
3183 /// Is all of the selection aligned according to the specified flag?
3184 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3186 wxRichTextRange range
;
3188 range
= GetSelectionRange();
3190 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3193 attr
.SetAlignment(alignment
);
3195 return HasParagraphAttributes(range
, attr
);
3198 /// Apply alignment to the selection
3199 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3202 attr
.SetAlignment(alignment
);
3204 return SetStyle(GetSelectionRange(), attr
);
3207 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3209 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3214 /// Apply a named style to the selection
3215 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3217 // Flags are defined within each definition, so only certain
3218 // attributes are applied.
3219 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3221 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3223 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3225 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3227 wxRichTextRange range
;
3230 range
= GetSelectionRange();
3233 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3234 range
= wxRichTextRange(pos
, pos
+1);
3237 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3240 // Make sure the attr has the style name
3241 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3243 attr
.SetParagraphStyleName(def
->GetName());
3245 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3246 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3247 // to change its style independently.
3248 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3251 attr
.SetCharacterStyleName(def
->GetName());
3254 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3257 wxRichTextAttr current
= GetDefaultStyleEx();
3258 current
.Apply(attr
);
3259 SetAndShowDefaultStyle(current
);
3264 /// Apply the style sheet to the buffer, for example if the styles have changed.
3265 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3268 styleSheet
= GetBuffer().GetStyleSheet();
3272 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3274 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3282 /// Sets the default style to the style under the cursor
3283 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3286 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3288 // If at the start of a paragraph, use the next position.
3289 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3291 if (GetUncombinedStyle(pos
, attr
))
3293 SetDefaultStyle(attr
);
3300 /// Returns the first visible position in the current view
3301 long wxRichTextCtrl::GetFirstVisiblePosition() const
3303 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3305 return line
->GetAbsoluteRange().GetStart();
3310 /// Get the first visible point in the window
3311 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3314 int startXUnits
, startYUnits
;
3316 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3317 GetViewStart(& startXUnits
, & startYUnits
);
3319 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3322 /// The adjusted caret position is the character position adjusted to take
3323 /// into account whether we're at the start of a paragraph, in which case
3324 /// style information should be taken from the next position, not current one.
3325 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3327 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3329 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3334 /// Get/set the selection range in character positions. -1, -1 means no selection.
3335 /// The range is in API convention, i.e. a single character selection is denoted
3337 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3339 wxRichTextRange range
= GetInternalSelectionRange();
3340 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3341 range
.SetEnd(range
.GetEnd() + 1);
3345 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3347 wxRichTextRange
range1(range
);
3348 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3349 range1
.SetEnd(range1
.GetEnd() - 1);
3351 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3353 SetInternalSelectionRange(range1
);
3357 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3359 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3362 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3364 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3367 /// Clear list for given range
3368 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3370 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3373 /// Number/renumber any list elements in the given range
3374 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3376 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3379 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3381 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3384 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3385 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3387 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3390 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3392 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3395 /// Deletes the content in the given range
3396 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3398 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3401 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3403 if (sm_availableFontNames
.GetCount() == 0)
3405 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3406 sm_availableFontNames
.Sort();
3408 return sm_availableFontNames
;
3411 void wxRichTextCtrl::ClearAvailableFontNames()
3413 sm_availableFontNames
.Clear();
3416 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3418 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3420 wxTextAttrEx basicStyle
= GetBasicStyle();
3421 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3422 SetBasicStyle(basicStyle
);
3423 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3428 // Refresh the area affected by a selection change
3429 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3431 // Calculate the refresh rectangle - just the affected lines
3432 long firstPos
, lastPos
;
3433 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3435 firstPos
= newSelection
.GetStart();
3436 lastPos
= newSelection
.GetEnd();
3438 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3440 firstPos
= oldSelection
.GetStart();
3441 lastPos
= oldSelection
.GetEnd();
3443 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3449 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3450 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3453 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3454 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3456 if (firstLine
&& lastLine
)
3458 wxSize clientSize
= GetClientSize();
3459 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3460 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3463 pt1
.y
= wxMax(0, pt1
.y
);
3465 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3467 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3468 RefreshRect(rect
, false);
3476 #if wxRICHTEXT_USE_OWN_CARET
3478 // ----------------------------------------------------------------------------
3479 // initialization and destruction
3480 // ----------------------------------------------------------------------------
3482 void wxRichTextCaret::Init()
3488 m_richTextCtrl
= NULL
;
3489 m_needsUpdate
= false;
3493 wxRichTextCaret::~wxRichTextCaret()
3495 if (m_timer
.IsRunning())
3499 // ----------------------------------------------------------------------------
3500 // showing/hiding/moving the caret (base class interface)
3501 // ----------------------------------------------------------------------------
3503 void wxRichTextCaret::DoShow()
3507 if (!m_timer
.IsRunning())
3508 m_timer
.Start(GetBlinkTime());
3513 void wxRichTextCaret::DoHide()
3515 if (m_timer
.IsRunning())
3521 void wxRichTextCaret::DoMove()
3527 if (m_xOld
!= -1 && m_yOld
!= -1)
3531 wxRect
rect(GetPosition(), GetSize());
3532 m_richTextCtrl
->RefreshRect(rect
, false);
3541 void wxRichTextCaret::DoSize()
3543 int countVisible
= m_countVisible
;
3544 if (countVisible
> 0)
3550 if (countVisible
> 0)
3552 m_countVisible
= countVisible
;
3557 // ----------------------------------------------------------------------------
3558 // handling the focus
3559 // ----------------------------------------------------------------------------
3561 void wxRichTextCaret::OnSetFocus()
3569 void wxRichTextCaret::OnKillFocus()
3574 // ----------------------------------------------------------------------------
3575 // drawing the caret
3576 // ----------------------------------------------------------------------------
3578 void wxRichTextCaret::Refresh()
3582 wxRect
rect(GetPosition(), GetSize());
3583 m_richTextCtrl
->RefreshRect(rect
, false);
3587 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3589 dc
->SetPen( *wxBLACK_PEN
);
3591 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3592 dc
->SetPen(*wxBLACK_PEN
);
3594 wxPoint
pt(m_x
, m_y
);
3598 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3600 if (IsVisible() && m_flashOn
)
3601 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3604 void wxRichTextCaret::Notify()
3606 m_flashOn
= !m_flashOn
;
3610 void wxRichTextCaretTimer::Notify()
3615 // wxRICHTEXT_USE_OWN_CARET