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_CLASS( wxRichTextCtrl
, wxControl
)
125 IMPLEMENT_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
[4];
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
);
266 wxAcceleratorTable
accel(4, entries
);
267 SetAcceleratorTable(accel
);
272 wxRichTextCtrl::~wxRichTextCtrl()
274 GetBuffer().RemoveEventHandler(this);
276 delete m_contextMenu
;
279 /// Member initialisation
280 void wxRichTextCtrl::Init()
282 m_contextMenu
= NULL
;
284 m_caretPosition
= -1;
285 m_selectionRange
.SetRange(-2, -2);
286 m_selectionAnchor
= -2;
288 m_caretAtLineStart
= false;
290 m_fullLayoutRequired
= false;
291 m_fullLayoutTime
= 0;
292 m_fullLayoutSavedPosition
= 0;
293 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
294 m_caretPositionForDefaultStyle
= -2;
297 void wxRichTextCtrl::DoThaw()
299 if (GetBuffer().GetDirty())
308 void wxRichTextCtrl::Clear()
310 m_buffer
.ResetAndClearCommands();
311 m_buffer
.SetDirty(true);
312 m_caretPosition
= -1;
313 m_caretPositionForDefaultStyle
= -2;
314 m_caretAtLineStart
= false;
315 m_selectionRange
.SetRange(-2, -2);
325 wxTextCtrl::SendTextUpdatedEvent(this);
329 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
331 #if !wxRICHTEXT_USE_OWN_CARET
332 if (GetCaret() && !IsFrozen())
337 #if wxRICHTEXT_BUFFERED_PAINTING
338 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
348 dc
.SetFont(GetFont());
350 // Paint the background
353 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
355 wxRect
drawingArea(GetUpdateRegion().GetBox());
356 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
358 wxRect
availableSpace(GetClientSize());
359 if (GetBuffer().GetDirty())
361 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
362 GetBuffer().SetDirty(false);
366 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
367 #if wxRICHTEXT_USE_OWN_CARET
368 if (GetCaret()->IsVisible())
370 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
375 #if !wxRICHTEXT_USE_OWN_CARET
382 // Empty implementation, to prevent flicker
383 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
387 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
391 #if !wxRICHTEXT_USE_OWN_CARET
397 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
398 // Work around dropouts when control is focused
406 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
411 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
412 // Work around dropouts when control is focused
420 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
426 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
432 dc
.SetFont(GetFont());
435 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
437 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
439 m_dragStart
= event
.GetLogicalPosition(dc
);
443 bool caretAtLineStart
= false;
445 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
447 // If we're at the start of a line (but not first in para)
448 // then we should keep the caret showing at the start of the line
449 // by showing the m_caretAtLineStart flag.
450 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
451 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
453 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
454 caretAtLineStart
= true;
458 long oldCaretPos
= m_caretPosition
;
460 MoveCaret(position
, caretAtLineStart
);
461 SetDefaultStyleToCursorStyle();
463 if (event
.ShiftDown())
465 if (m_selectionRange
.GetStart() == -2)
466 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
468 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
478 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
483 if (GetCapture() == this)
486 // See if we clicked on a URL
489 dc
.SetFont(GetFont());
492 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
493 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
495 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
497 wxRichTextEvent
cmdEvent(
498 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
500 cmdEvent
.SetEventObject(this);
501 cmdEvent
.SetPosition(m_caretPosition
+1);
503 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
506 if (GetStyle(position
, attr
))
508 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
510 wxString urlTarget
= attr
.GetURL();
511 if (!urlTarget
.IsEmpty())
513 wxMouseEvent
mouseEvent(event
);
515 long startPos
= 0, endPos
= 0;
516 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
519 startPos
= obj
->GetRange().GetStart();
520 endPos
= obj
->GetRange().GetEnd();
523 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
524 InitCommandEvent(urlEvent
);
526 urlEvent
.SetString(urlTarget
);
528 GetEventHandler()->ProcessEvent(urlEvent
);
538 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
542 dc
.SetFont(GetFont());
545 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
546 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
548 // See if we need to change the cursor
551 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
554 if (GetStyle(position
, attr
))
556 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
558 SetCursor(m_urlCursor
);
560 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
562 SetCursor(m_textCursor
);
567 SetCursor(m_textCursor
);
570 if (!event
.Dragging())
576 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
578 // TODO: test closeness
580 bool caretAtLineStart
= false;
582 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
584 // If we're at the start of a line (but not first in para)
585 // then we should keep the caret showing at the start of the line
586 // by showing the m_caretAtLineStart flag.
587 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
588 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
590 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
591 caretAtLineStart
= true;
595 if (m_caretPosition
!= position
)
597 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
599 MoveCaret(position
, caretAtLineStart
);
600 SetDefaultStyleToCursorStyle();
606 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
610 wxRichTextEvent
cmdEvent(
611 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
613 cmdEvent
.SetEventObject(this);
614 cmdEvent
.SetPosition(m_caretPosition
+1);
616 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
620 /// Left-double-click
621 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
623 wxRichTextEvent
cmdEvent(
624 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
626 cmdEvent
.SetEventObject(this);
627 cmdEvent
.SetPosition(m_caretPosition
+1);
629 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
631 SelectWord(GetCaretPosition()+1);
636 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
638 wxRichTextEvent
cmdEvent(
639 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
641 cmdEvent
.SetEventObject(this);
642 cmdEvent
.SetPosition(m_caretPosition
+1);
644 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
649 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
653 flags
|= wxRICHTEXT_CTRL_DOWN
;
654 if (event
.ShiftDown())
655 flags
|= wxRICHTEXT_SHIFT_DOWN
;
657 flags
|= wxRICHTEXT_ALT_DOWN
;
659 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
661 if (event
.GetKeyCode() == WXK_LEFT
||
662 event
.GetKeyCode() == WXK_RIGHT
||
663 event
.GetKeyCode() == WXK_UP
||
664 event
.GetKeyCode() == WXK_DOWN
||
665 event
.GetKeyCode() == WXK_HOME
||
666 event
.GetKeyCode() == WXK_PAGEUP
||
667 event
.GetKeyCode() == WXK_PAGEDOWN
||
668 event
.GetKeyCode() == WXK_END
||
670 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
671 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
672 event
.GetKeyCode() == WXK_NUMPAD_UP
||
673 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
674 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
675 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
676 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
677 event
.GetKeyCode() == WXK_NUMPAD_END
)
679 KeyboardNavigate(event
.GetKeyCode(), flags
);
683 long keycode
= event
.GetKeyCode();
743 case WXK_NUMPAD_HOME
:
744 case WXK_NUMPAD_LEFT
:
746 case WXK_NUMPAD_RIGHT
:
747 case WXK_NUMPAD_DOWN
:
748 case WXK_NUMPAD_PAGEUP
:
749 case WXK_NUMPAD_PAGEDOWN
:
751 case WXK_NUMPAD_BEGIN
:
752 case WXK_NUMPAD_INSERT
:
753 case WXK_NUMPAD_DELETE
:
754 case WXK_WINDOWS_LEFT
:
763 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
764 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
766 BeginBatchUndo(_("Delete Text"));
768 long newPos
= m_caretPosition
;
770 bool processed
= DeleteSelectedContent(& newPos
);
772 // Submit range in character positions, which are greater than caret positions,
773 // so subtract 1 for deleted character and add 1 for conversion to character position.
778 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
781 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
787 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
792 if (GetLastPosition() == -1)
796 m_caretPosition
= -1;
798 SetDefaultStyleToCursorStyle();
801 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
803 wxRichTextEvent
cmdEvent(
804 wxEVT_COMMAND_RICHTEXT_DELETE
,
806 cmdEvent
.SetEventObject(this);
807 cmdEvent
.SetFlags(flags
);
808 cmdEvent
.SetPosition(m_caretPosition
+1);
809 GetEventHandler()->ProcessEvent(cmdEvent
);
819 // all the other keys modify the controls contents which shouldn't be
820 // possible if we're read-only
827 if (event
.GetKeyCode() == WXK_RETURN
)
829 BeginBatchUndo(_("Insert Text"));
831 long newPos
= m_caretPosition
;
833 DeleteSelectedContent(& newPos
);
835 if (event
.ShiftDown())
838 text
= wxRichTextLineBreakChar
;
839 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
840 m_caretAtLineStart
= true;
844 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
847 SetDefaultStyleToCursorStyle();
849 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
851 wxRichTextEvent
cmdEvent(
852 wxEVT_COMMAND_RICHTEXT_RETURN
,
854 cmdEvent
.SetEventObject(this);
855 cmdEvent
.SetFlags(flags
);
856 cmdEvent
.SetPosition(newPos
+1);
858 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
860 // Generate conventional event
861 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
862 InitCommandEvent(textEvent
);
864 GetEventHandler()->ProcessEvent(textEvent
);
868 else if (event
.GetKeyCode() == WXK_BACK
)
870 BeginBatchUndo(_("Delete Text"));
872 long newPos
= m_caretPosition
;
874 bool processed
= DeleteSelectedContent(& newPos
);
876 // Submit range in character positions, which are greater than caret positions,
877 // so subtract 1 for deleted character and add 1 for conversion to character position.
882 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
885 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
891 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
896 if (GetLastPosition() == -1)
900 m_caretPosition
= -1;
902 SetDefaultStyleToCursorStyle();
905 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
907 wxRichTextEvent
cmdEvent(
908 wxEVT_COMMAND_RICHTEXT_DELETE
,
910 cmdEvent
.SetEventObject(this);
911 cmdEvent
.SetFlags(flags
);
912 cmdEvent
.SetPosition(m_caretPosition
+1);
913 GetEventHandler()->ProcessEvent(cmdEvent
);
917 else if (event
.GetKeyCode() == WXK_DELETE
)
919 BeginBatchUndo(_("Delete Text"));
921 long newPos
= m_caretPosition
;
923 bool processed
= DeleteSelectedContent(& newPos
);
925 // Submit range in character positions, which are greater than caret positions,
926 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
930 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
931 if (pos
!= -1 && (pos
> newPos
))
933 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
939 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
944 if (GetLastPosition() == -1)
948 m_caretPosition
= -1;
950 SetDefaultStyleToCursorStyle();
953 wxRichTextEvent
cmdEvent(
954 wxEVT_COMMAND_RICHTEXT_DELETE
,
956 cmdEvent
.SetEventObject(this);
957 cmdEvent
.SetFlags(flags
);
958 cmdEvent
.SetPosition(m_caretPosition
+1);
959 GetEventHandler()->ProcessEvent(cmdEvent
);
965 long keycode
= event
.GetKeyCode();
979 if (event
.CmdDown() || event
.AltDown())
986 wxRichTextEvent
cmdEvent(
987 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
989 cmdEvent
.SetEventObject(this);
990 cmdEvent
.SetFlags(flags
);
992 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
994 cmdEvent
.SetCharacter((wxChar
) keycode
);
996 cmdEvent
.SetPosition(m_caretPosition
+1);
998 if (keycode
== wxT('\t'))
1000 // See if we need to promote or demote the selection or paragraph at the cursor
1001 // position, instead of inserting a tab.
1002 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1003 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1004 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1006 wxRichTextRange range
;
1008 range
= GetSelectionRange();
1010 range
= para
->GetRange().FromInternal();
1012 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1014 PromoteList(promoteBy
, range
, NULL
);
1016 GetEventHandler()->ProcessEvent(cmdEvent
);
1022 BeginBatchUndo(_("Insert Text"));
1024 long newPos
= m_caretPosition
;
1025 DeleteSelectedContent(& newPos
);
1028 wxString str
= event
.GetUnicodeKey();
1030 wxString str
= (wxChar
) event
.GetKeyCode();
1032 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1036 SetDefaultStyleToCursorStyle();
1037 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1039 GetEventHandler()->ProcessEvent(cmdEvent
);
1047 /// Delete content if there is a selection, e.g. when pressing a key.
1048 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1052 long pos
= m_selectionRange
.GetStart();
1053 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1054 m_selectionRange
.SetRange(-2, -2);
1064 /// Keyboard navigation
1068 Left: left one character
1069 Right: right one character
1072 Ctrl-Left: left one word
1073 Ctrl-Right: right one word
1074 Ctrl-Up: previous paragraph start
1075 Ctrl-Down: next start of paragraph
1078 Ctrl-Home: start of document
1079 Ctrl-End: end of document
1080 Page-Up: Up a screen
1081 Page-Down: Down a screen
1085 Ctrl-Alt-PgUp: Start of window
1086 Ctrl-Alt-PgDn: End of window
1087 F8: Start selection mode
1088 Esc: End selection mode
1090 Adding Shift does the above but starts/extends selection.
1095 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1097 bool success
= false;
1099 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1101 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1102 success
= WordRight(1, flags
);
1104 success
= MoveRight(1, flags
);
1106 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1108 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1109 success
= WordLeft(1, flags
);
1111 success
= MoveLeft(1, flags
);
1113 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1115 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1116 success
= MoveToParagraphStart(flags
);
1118 success
= MoveUp(1, flags
);
1120 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1122 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1123 success
= MoveToParagraphEnd(flags
);
1125 success
= MoveDown(1, flags
);
1127 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1129 success
= PageUp(1, flags
);
1131 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1133 success
= PageDown(1, flags
);
1135 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1137 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1138 success
= MoveHome(flags
);
1140 success
= MoveToLineStart(flags
);
1142 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1144 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1145 success
= MoveEnd(flags
);
1147 success
= MoveToLineEnd(flags
);
1152 ScrollIntoView(m_caretPosition
, keyCode
);
1153 SetDefaultStyleToCursorStyle();
1159 /// Extend the selection. Selections are in caret positions.
1160 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1162 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1164 if (oldPos
== newPos
)
1167 wxRichTextRange oldSelection
= m_selectionRange
;
1169 // If not currently selecting, start selecting
1170 if (m_selectionRange
.GetStart() == -2)
1172 m_selectionAnchor
= oldPos
;
1174 if (oldPos
> newPos
)
1175 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1177 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1181 // Always ensure that the selection range start is greater than
1183 if (newPos
> m_selectionAnchor
)
1184 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1185 else if (newPos
== m_selectionAnchor
)
1186 m_selectionRange
= wxRichTextRange(-2, -2);
1188 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1191 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1193 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1195 wxLogDebug(wxT("Strange selection range"));
1204 /// Scroll into view, returning true if we scrolled.
1205 /// This takes a _caret_ position.
1206 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1208 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1214 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1216 int startXUnits
, startYUnits
;
1217 GetViewStart(& startXUnits
, & startYUnits
);
1218 int startY
= startYUnits
* ppuY
;
1221 GetVirtualSize(& sx
, & sy
);
1227 wxRect rect
= line
->GetRect();
1229 bool scrolled
= false;
1231 wxSize clientSize
= GetClientSize();
1234 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1235 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1236 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1237 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1239 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1241 // Make it scroll so this item is at the bottom
1243 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1244 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1246 // If we're still off the screen, scroll another line down
1247 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1250 if (startYUnits
!= yUnits
)
1252 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1256 else if (rect
.y
< startY
)
1258 // Make it scroll so this item is at the top
1261 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1263 if (startYUnits
!= yUnits
)
1265 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1271 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1272 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1273 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1274 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1276 if (rect
.y
< startY
)
1278 // Make it scroll so this item is at the top
1281 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1283 if (startYUnits
!= yUnits
)
1285 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1289 else 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
);
1308 #if !wxRICHTEXT_USE_OWN_CARET
1316 /// Is the given position visible on the screen?
1317 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1319 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1325 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1328 GetViewStart(& startX
, & startY
);
1330 startY
= startY
* ppuY
;
1332 wxRect rect
= line
->GetRect();
1333 wxSize clientSize
= GetClientSize();
1335 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1338 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1340 m_caretPosition
= position
;
1341 m_caretAtLineStart
= showAtLineStart
;
1344 /// Move caret one visual step forward: this may mean setting a flag
1345 /// and keeping the same position if we're going from the end of one line
1346 /// to the start of the next, which may be the exact same caret position.
1347 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1349 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1351 // Only do the check if we're not at the end of the paragraph (where things work OK
1353 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1355 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1359 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1361 // We're at the end of a line. See whether we need to
1362 // stay at the same actual caret position but change visual
1363 // position, or not.
1364 if (oldPosition
== lineRange
.GetEnd())
1366 if (m_caretAtLineStart
)
1368 // We're already at the start of the line, so actually move on now.
1369 m_caretPosition
= oldPosition
+ 1;
1370 m_caretAtLineStart
= false;
1374 // We're showing at the end of the line, so keep to
1375 // the same position but indicate that we're to show
1376 // at the start of the next line.
1377 m_caretPosition
= oldPosition
;
1378 m_caretAtLineStart
= true;
1380 SetDefaultStyleToCursorStyle();
1386 SetDefaultStyleToCursorStyle();
1389 /// Move caret one visual step backward: this may mean setting a flag
1390 /// and keeping the same position if we're going from the end of one line
1391 /// to the start of the next, which may be the exact same caret position.
1392 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1394 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1396 // Only do the check if we're not at the start of the paragraph (where things work OK
1398 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1400 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1404 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1406 // We're at the start of a line. See whether we need to
1407 // stay at the same actual caret position but change visual
1408 // position, or not.
1409 if (oldPosition
== lineRange
.GetStart())
1411 m_caretPosition
= oldPosition
-1;
1412 m_caretAtLineStart
= true;
1415 else if (oldPosition
== lineRange
.GetEnd())
1417 if (m_caretAtLineStart
)
1419 // We're at the start of the line, so keep the same caret position
1420 // but clear the start-of-line flag.
1421 m_caretPosition
= oldPosition
;
1422 m_caretAtLineStart
= false;
1426 // We're showing at the end of the line, so go back
1427 // to the previous character position.
1428 m_caretPosition
= oldPosition
- 1;
1430 SetDefaultStyleToCursorStyle();
1436 SetDefaultStyleToCursorStyle();
1440 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1442 long endPos
= GetBuffer().GetRange().GetEnd();
1444 if (m_caretPosition
+ noPositions
< endPos
)
1446 long oldPos
= m_caretPosition
;
1447 long newPos
= m_caretPosition
+ noPositions
;
1449 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1453 // Determine by looking at oldPos and m_caretPosition whether
1454 // we moved from the end of a line to the start of the next line, in which case
1455 // we want to adjust the caret position such that it is positioned at the
1456 // start of the next line, rather than jumping past the first character of the
1458 if (noPositions
== 1 && !extendSel
)
1459 MoveCaretForward(oldPos
);
1461 SetCaretPosition(newPos
);
1464 SetDefaultStyleToCursorStyle();
1473 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1477 if (m_caretPosition
> startPos
- noPositions
+ 1)
1479 long oldPos
= m_caretPosition
;
1480 long newPos
= m_caretPosition
- noPositions
;
1481 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1485 if (noPositions
== 1 && !extendSel
)
1486 MoveCaretBack(oldPos
);
1488 SetCaretPosition(newPos
);
1491 SetDefaultStyleToCursorStyle();
1500 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1502 return MoveDown(- noLines
, flags
);
1506 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1511 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1512 wxPoint pt
= GetCaret()->GetPosition();
1513 long newLine
= lineNumber
+ noLines
;
1515 if (lineNumber
!= -1)
1519 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1521 if (newLine
> lastLine
)
1531 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1534 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1540 wxClientDC
dc(this);
1542 dc
.SetFont(GetFont());
1544 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1546 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1548 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1549 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1550 // so we view the caret at the start of the line.
1551 bool caretLineStart
= false;
1552 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1554 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1555 wxRichTextRange lineRange
;
1557 lineRange
= thisLine
->GetAbsoluteRange();
1559 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1562 caretLineStart
= true;
1566 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1567 if (para
&& para
->GetRange().GetStart() == newPos
)
1572 long newSelEnd
= newPos
;
1574 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1578 SetCaretPosition(newPos
, caretLineStart
);
1580 SetDefaultStyleToCursorStyle();
1588 /// Move to the end of the paragraph
1589 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1591 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1594 long newPos
= para
->GetRange().GetEnd() - 1;
1595 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1599 SetCaretPosition(newPos
);
1601 SetDefaultStyleToCursorStyle();
1609 /// Move to the start of the paragraph
1610 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1612 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1615 long newPos
= para
->GetRange().GetStart() - 1;
1616 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1620 SetCaretPosition(newPos
);
1622 SetDefaultStyleToCursorStyle();
1630 /// Move to the end of the line
1631 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1633 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1637 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1638 long newPos
= lineRange
.GetEnd();
1639 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1643 SetCaretPosition(newPos
);
1645 SetDefaultStyleToCursorStyle();
1653 /// Move to the start of the line
1654 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1656 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1659 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1660 long newPos
= lineRange
.GetStart()-1;
1662 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1666 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1668 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1670 SetDefaultStyleToCursorStyle();
1678 /// Move to the start of the buffer
1679 bool wxRichTextCtrl::MoveHome(int flags
)
1681 if (m_caretPosition
!= -1)
1683 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1687 SetCaretPosition(-1);
1689 SetDefaultStyleToCursorStyle();
1697 /// Move to the end of the buffer
1698 bool wxRichTextCtrl::MoveEnd(int flags
)
1700 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1702 if (m_caretPosition
!= endPos
)
1704 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1708 SetCaretPosition(endPos
);
1710 SetDefaultStyleToCursorStyle();
1718 /// Move noPages pages up
1719 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1721 return PageDown(- noPages
, flags
);
1724 /// Move noPages pages down
1725 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1727 // Calculate which line occurs noPages * screen height further down.
1728 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1731 wxSize clientSize
= GetClientSize();
1732 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1734 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1737 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1738 long pos
= lineRange
.GetStart()-1;
1739 if (pos
!= m_caretPosition
)
1741 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1743 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1747 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1749 SetDefaultStyleToCursorStyle();
1759 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1761 return str
== wxT(" ") || str
== wxT("\t");
1764 // Finds the caret position for the next word
1765 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1767 long endPos
= GetBuffer().GetRange().GetEnd();
1771 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1773 // First skip current text to space
1774 while (i
< endPos
&& i
> -1)
1776 // i is in character, not caret positions
1777 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1778 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1779 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1783 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1790 while (i
< endPos
&& i
> -1)
1792 // i is in character, not caret positions
1793 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1794 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1795 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1796 return wxMax(-1, i
);
1798 if (text
.empty()) // End of paragraph, or maybe an image
1799 return wxMax(-1, i
- 1);
1800 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1804 // Convert to caret position
1805 return wxMax(-1, i
- 1);
1814 long i
= m_caretPosition
;
1816 // First skip white space
1817 while (i
< endPos
&& i
> -1)
1819 // i is in character, not caret positions
1820 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1821 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1823 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1825 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1830 // Next skip current text to space
1831 while (i
< endPos
&& i
> -1)
1833 // i is in character, not caret positions
1834 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1835 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1836 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1839 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1852 /// Move n words left
1853 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1855 long pos
= FindNextWordPosition(-1);
1856 if (pos
!= m_caretPosition
)
1858 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1860 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1864 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1866 SetDefaultStyleToCursorStyle();
1874 /// Move n words right
1875 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1877 long pos
= FindNextWordPosition(1);
1878 if (pos
!= m_caretPosition
)
1880 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1882 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1886 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1888 SetDefaultStyleToCursorStyle();
1897 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1899 // Only do sizing optimization for large buffers
1900 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1902 m_fullLayoutRequired
= true;
1903 m_fullLayoutTime
= wxGetLocalTimeMillis();
1904 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1905 LayoutContent(true /* onlyVisibleRect */);
1908 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1910 #if wxRICHTEXT_BUFFERED_PAINTING
1918 /// Idle-time processing
1919 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1921 #if wxRICHTEXT_USE_OWN_CARET
1922 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1924 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1930 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1932 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1934 m_fullLayoutRequired
= false;
1935 m_fullLayoutTime
= 0;
1936 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1937 ShowPosition(m_fullLayoutSavedPosition
);
1941 if (m_caretPositionForDefaultStyle
!= -2)
1943 // If the caret position has changed, no longer reflect the default style
1945 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1946 m_caretPositionForDefaultStyle
= -2;
1953 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1955 #if wxRICHTEXT_USE_OWN_CARET
1956 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1959 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1966 /// Set up scrollbars, e.g. after a resize
1967 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1972 if (GetBuffer().IsEmpty())
1974 SetScrollbars(0, 0, 0, 0, 0, 0);
1978 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1979 // of pixels. See e.g. wxVScrolledWindow for ideas.
1980 int pixelsPerUnit
= 5;
1981 wxSize clientSize
= GetClientSize();
1983 int maxHeight
= GetBuffer().GetCachedSize().y
;
1985 // Round up so we have at least maxHeight pixels
1986 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1988 int startX
= 0, startY
= 0;
1990 GetViewStart(& startX
, & startY
);
1992 int maxPositionX
= 0;
1993 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1995 int newStartX
= wxMin(maxPositionX
, startX
);
1996 int newStartY
= wxMin(maxPositionY
, startY
);
1998 int oldPPUX
, oldPPUY
;
1999 int oldStartX
, oldStartY
;
2000 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2001 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2002 GetViewStart(& oldStartX
, & oldStartY
);
2003 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2005 oldVirtualSizeY
/= oldPPUY
;
2007 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2010 // Don't set scrollbars if there were none before, and there will be none now.
2011 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2014 // Move to previous scroll position if
2016 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2019 /// Paint the background
2020 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2022 wxColour backgroundColour
= GetBackgroundColour();
2023 if (!backgroundColour
.Ok())
2024 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2026 // Clear the background
2027 dc
.SetBrush(wxBrush(backgroundColour
));
2028 dc
.SetPen(*wxTRANSPARENT_PEN
);
2029 wxRect
windowRect(GetClientSize());
2030 windowRect
.x
-= 2; windowRect
.y
-= 2;
2031 windowRect
.width
+= 4; windowRect
.height
+= 4;
2033 // We need to shift the rectangle to take into account
2034 // scrolling. Converting device to logical coordinates.
2035 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2036 dc
.DrawRectangle(windowRect
);
2039 #if wxRICHTEXT_BUFFERED_PAINTING
2040 /// Recreate buffer bitmap if necessary
2041 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2044 if (sz
== wxDefaultSize
)
2045 sz
= GetClientSize();
2047 if (sz
.x
< 1 || sz
.y
< 1)
2050 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2051 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2052 return m_bufferBitmap
.Ok();
2056 // ----------------------------------------------------------------------------
2057 // file IO functions
2058 // ----------------------------------------------------------------------------
2060 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2062 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2064 m_filename
= filename
;
2067 SetInsertionPoint(0);
2070 SetupScrollbars(true);
2072 wxTextCtrl::SendTextUpdatedEvent(this);
2078 wxLogError(_("File couldn't be loaded."));
2084 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2086 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2088 m_filename
= filename
;
2095 wxLogError(_("The text couldn't be saved."));
2100 // ----------------------------------------------------------------------------
2101 // wxRichTextCtrl specific functionality
2102 // ----------------------------------------------------------------------------
2104 /// Add a new paragraph of text to the end of the buffer
2105 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2107 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2113 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2115 wxRichTextRange range
= GetBuffer().AddImage(image
);
2120 // ----------------------------------------------------------------------------
2121 // selection and ranges
2122 // ----------------------------------------------------------------------------
2124 void wxRichTextCtrl::SelectAll()
2126 SetSelection(0, GetLastPosition()+1);
2127 m_selectionAnchor
= -1;
2131 void wxRichTextCtrl::SelectNone()
2133 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2135 wxRichTextRange oldSelection
= m_selectionRange
;
2137 m_selectionRange
= wxRichTextRange(-2, -2);
2139 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2141 m_selectionAnchor
= -2;
2144 static bool wxIsWordDelimiter(const wxString
& text
)
2146 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2149 /// Select the word at the given character position
2150 bool wxRichTextCtrl::SelectWord(long position
)
2152 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2155 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2159 if (position
== para
->GetRange().GetEnd())
2162 long positionStart
= position
;
2163 long positionEnd
= position
;
2165 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2167 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2168 if (wxIsWordDelimiter(text
))
2174 if (positionStart
< para
->GetRange().GetStart())
2175 positionStart
= para
->GetRange().GetStart();
2177 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2179 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2180 if (wxIsWordDelimiter(text
))
2186 if (positionEnd
>= para
->GetRange().GetEnd())
2187 positionEnd
= para
->GetRange().GetEnd();
2189 if (positionEnd
< positionStart
)
2192 SetSelection(positionStart
, positionEnd
+1);
2194 if (positionStart
>= 0)
2196 MoveCaret(positionStart
-1, true);
2197 SetDefaultStyleToCursorStyle();
2203 wxString
wxRichTextCtrl::GetStringSelection() const
2206 GetSelection(&from
, &to
);
2208 return GetRange(from
, to
);
2211 // ----------------------------------------------------------------------------
2213 // ----------------------------------------------------------------------------
2215 wxTextCtrlHitTestResult
2216 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2218 // implement in terms of the other overload as the native ports typically
2219 // can get the position and not (x, y) pair directly (although wxUniv
2220 // directly gets x and y -- and so overrides this method as well)
2222 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2224 if ( rc
!= wxTE_HT_UNKNOWN
)
2226 PositionToXY(pos
, x
, y
);
2232 wxTextCtrlHitTestResult
2233 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2236 wxClientDC
dc((wxRichTextCtrl
*) this);
2237 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2239 // Buffer uses logical position (relative to start of buffer)
2241 wxPoint pt2
= GetLogicalPoint(pt
);
2243 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2245 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2246 return wxTE_HT_BEFORE
;
2247 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2248 return wxTE_HT_BEYOND
;
2249 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2250 return wxTE_HT_ON_TEXT
;
2252 return wxTE_HT_UNKNOWN
;
2255 // ----------------------------------------------------------------------------
2256 // set/get the controls text
2257 // ----------------------------------------------------------------------------
2259 wxString
wxRichTextCtrl::GetValue() const
2261 return GetBuffer().GetText();
2264 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2266 // Public API for range is different from internals
2267 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2270 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2272 // Don't call Clear here, since it always sends a text updated event
2273 m_buffer
.ResetAndClearCommands();
2274 m_buffer
.SetDirty(true);
2275 m_caretPosition
= -1;
2276 m_caretPositionForDefaultStyle
= -2;
2277 m_caretAtLineStart
= false;
2278 m_selectionRange
.SetRange(-2, -2);
2288 if (!value
.IsEmpty())
2290 // Remove empty paragraph
2291 GetBuffer().Clear();
2292 DoWriteText(value
, flags
);
2294 // for compatibility, don't move the cursor when doing SetValue()
2295 SetInsertionPoint(0);
2299 // still send an event for consistency
2300 if (flags
& SetValue_SendEvent
)
2301 wxTextCtrl::SendTextUpdatedEvent(this);
2306 void wxRichTextCtrl::WriteText(const wxString
& value
)
2311 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2313 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2315 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2317 if ( flags
& SetValue_SendEvent
)
2318 wxTextCtrl::SendTextUpdatedEvent(this);
2321 void wxRichTextCtrl::AppendText(const wxString
& text
)
2323 SetInsertionPointEnd();
2328 /// Write an image at the current insertion point
2329 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2331 wxRichTextImageBlock imageBlock
;
2333 wxImage image2
= image
;
2334 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2335 return WriteImage(imageBlock
);
2340 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2342 wxRichTextImageBlock imageBlock
;
2345 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2346 return WriteImage(imageBlock
);
2351 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2353 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2356 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2360 wxRichTextImageBlock imageBlock
;
2362 wxImage image
= bitmap
.ConvertToImage();
2363 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2364 return WriteImage(imageBlock
);
2370 /// Insert a newline (actually paragraph) at the current insertion point.
2371 bool wxRichTextCtrl::Newline()
2373 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2376 /// Insert a line break at the current insertion point.
2377 bool wxRichTextCtrl::LineBreak()
2380 text
= wxRichTextLineBreakChar
;
2381 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2384 // ----------------------------------------------------------------------------
2385 // Clipboard operations
2386 // ----------------------------------------------------------------------------
2388 void wxRichTextCtrl::Copy()
2392 wxRichTextRange range
= GetInternalSelectionRange();
2393 GetBuffer().CopyToClipboard(range
);
2397 void wxRichTextCtrl::Cut()
2401 wxRichTextRange range
= GetInternalSelectionRange();
2402 GetBuffer().CopyToClipboard(range
);
2404 DeleteSelectedContent();
2410 void wxRichTextCtrl::Paste()
2414 BeginBatchUndo(_("Paste"));
2416 long newPos
= m_caretPosition
;
2417 DeleteSelectedContent(& newPos
);
2419 GetBuffer().PasteFromClipboard(newPos
);
2425 void wxRichTextCtrl::DeleteSelection()
2427 if (CanDeleteSelection())
2429 DeleteSelectedContent();
2433 bool wxRichTextCtrl::HasSelection() const
2435 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2438 bool wxRichTextCtrl::CanCopy() const
2440 // Can copy if there's a selection
2441 return HasSelection();
2444 bool wxRichTextCtrl::CanCut() const
2446 return HasSelection() && IsEditable();
2449 bool wxRichTextCtrl::CanPaste() const
2451 if ( !IsEditable() )
2454 return GetBuffer().CanPasteFromClipboard();
2457 bool wxRichTextCtrl::CanDeleteSelection() const
2459 return HasSelection() && IsEditable();
2463 // ----------------------------------------------------------------------------
2465 // ----------------------------------------------------------------------------
2467 void wxRichTextCtrl::SetEditable(bool editable
)
2469 m_editable
= editable
;
2472 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2476 m_caretPosition
= pos
- 1;
2481 void wxRichTextCtrl::SetInsertionPointEnd()
2483 long pos
= GetLastPosition();
2484 SetInsertionPoint(pos
);
2487 long wxRichTextCtrl::GetInsertionPoint() const
2489 return m_caretPosition
+1;
2492 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2494 return GetBuffer().GetRange().GetEnd();
2497 // If the return values from and to are the same, there is no
2499 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2501 *from
= m_selectionRange
.GetStart();
2502 *to
= m_selectionRange
.GetEnd();
2503 if ((*to
) != -1 && (*to
) != -2)
2507 bool wxRichTextCtrl::IsEditable() const
2512 // ----------------------------------------------------------------------------
2514 // ----------------------------------------------------------------------------
2516 void wxRichTextCtrl::SetSelection(long from
, long to
)
2518 // if from and to are both -1, it means (in wxWidgets) that all text should
2520 if ( (from
== -1) && (to
== -1) )
2523 to
= GetLastPosition()+1;
2526 DoSetSelection(from
, to
);
2529 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2537 wxRichTextRange oldSelection
= m_selectionRange
;
2538 m_selectionAnchor
= from
;
2539 m_selectionRange
.SetRange(from
, to
-1);
2541 m_caretPosition
= from
-1;
2543 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2548 // ----------------------------------------------------------------------------
2550 // ----------------------------------------------------------------------------
2552 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2553 const wxString
& value
)
2555 BeginBatchUndo(_("Replace"));
2557 DeleteSelectedContent();
2559 DoWriteText(value
, SetValue_SelectionOnly
);
2564 void wxRichTextCtrl::Remove(long from
, long to
)
2568 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2575 bool wxRichTextCtrl::IsModified() const
2577 return m_buffer
.IsModified();
2580 void wxRichTextCtrl::MarkDirty()
2582 m_buffer
.Modify(true);
2585 void wxRichTextCtrl::DiscardEdits()
2587 m_caretPositionForDefaultStyle
= -2;
2588 m_buffer
.Modify(false);
2589 m_buffer
.GetCommandProcessor()->ClearCommands();
2592 int wxRichTextCtrl::GetNumberOfLines() const
2594 return GetBuffer().GetParagraphCount();
2597 // ----------------------------------------------------------------------------
2598 // Positions <-> coords
2599 // ----------------------------------------------------------------------------
2601 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2603 return GetBuffer().XYToPosition(x
, y
);
2606 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2608 return GetBuffer().PositionToXY(pos
, x
, y
);
2611 // ----------------------------------------------------------------------------
2613 // ----------------------------------------------------------------------------
2615 void wxRichTextCtrl::ShowPosition(long pos
)
2617 if (!IsPositionVisible(pos
))
2618 ScrollIntoView(pos
-1, WXK_DOWN
);
2621 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2623 return GetBuffer().GetParagraphLength(lineNo
);
2626 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2628 return GetBuffer().GetParagraphText(lineNo
);
2631 // ----------------------------------------------------------------------------
2633 // ----------------------------------------------------------------------------
2635 void wxRichTextCtrl::Undo()
2639 GetCommandProcessor()->Undo();
2643 void wxRichTextCtrl::Redo()
2647 GetCommandProcessor()->Redo();
2651 bool wxRichTextCtrl::CanUndo() const
2653 return GetCommandProcessor()->CanUndo();
2656 bool wxRichTextCtrl::CanRedo() const
2658 return GetCommandProcessor()->CanRedo();
2661 // ----------------------------------------------------------------------------
2662 // implementation details
2663 // ----------------------------------------------------------------------------
2665 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2667 SetValue(event
.GetString());
2668 GetEventHandler()->ProcessEvent(event
);
2671 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2673 // By default, load the first file into the text window.
2674 if (event
.GetNumberOfFiles() > 0)
2676 LoadFile(event
.GetFiles()[0]);
2680 wxSize
wxRichTextCtrl::DoGetBestSize() const
2682 return wxSize(10, 10);
2685 // ----------------------------------------------------------------------------
2686 // standard handlers for standard edit menu events
2687 // ----------------------------------------------------------------------------
2689 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2694 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2699 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2704 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2709 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2714 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2719 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2721 event
.Enable( CanCut() );
2724 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2726 event
.Enable( CanCopy() );
2729 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2731 event
.Enable( CanDeleteSelection() );
2734 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2736 event
.Enable( CanPaste() );
2739 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2741 event
.Enable( CanUndo() );
2742 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2745 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2747 event
.Enable( CanRedo() );
2748 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2751 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2756 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2758 event
.Enable(GetLastPosition() > 0);
2761 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2763 if (event
.GetEventObject() != this)
2771 m_contextMenu
= new wxMenu
;
2772 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2773 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2774 m_contextMenu
->AppendSeparator();
2775 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2776 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2777 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2778 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2779 m_contextMenu
->AppendSeparator();
2780 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2782 PopupMenu(m_contextMenu
);
2786 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2788 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2791 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2793 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2796 // extended style setting operation with flags including:
2797 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2798 // see richtextbuffer.h for more details.
2800 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2802 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2805 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2807 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2810 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2812 return GetBuffer().GetDefaultStyle();
2815 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2817 return GetBuffer().GetStyle(position
, style
);
2820 // get the common set of styles for the range
2821 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2823 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2826 /// Get the content (uncombined) attributes for this position.
2827 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2829 return GetBuffer().GetUncombinedStyle(position
, style
);
2832 /// Set font, and also the buffer attributes
2833 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2835 wxControl::SetFont(font
);
2837 wxTextAttr attr
= GetBuffer().GetAttributes();
2839 GetBuffer().SetBasicStyle(attr
);
2841 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2847 /// Transform logical to physical
2848 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2851 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2856 /// Transform physical to logical
2857 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2860 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2865 /// Position the caret
2866 void wxRichTextCtrl::PositionCaret()
2871 //wxLogDebug(wxT("PositionCaret"));
2874 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2876 wxPoint newPt
= caretRect
.GetPosition();
2877 wxSize newSz
= caretRect
.GetSize();
2878 wxPoint pt
= GetPhysicalPoint(newPt
);
2879 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2882 if (GetCaret()->GetSize() != newSz
)
2883 GetCaret()->SetSize(newSz
);
2884 GetCaret()->Move(pt
);
2890 /// Get the caret height and position for the given character position
2891 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2893 wxClientDC
dc(this);
2894 dc
.SetFont(GetFont());
2901 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2903 // Caret height can't be zero
2905 height
= dc
.GetCharHeight();
2907 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2914 /// Gets the line for the visible caret position. If the caret is
2915 /// shown at the very end of the line, it means the next character is actually
2916 /// on the following line. So let's get the line we're expecting to find
2917 /// if this is the case.
2918 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2920 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2921 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2924 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2925 if (caretPosition
== lineRange
.GetStart()-1 &&
2926 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2928 if (!m_caretAtLineStart
)
2929 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2936 /// Move the caret to the given character position
2937 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2939 if (GetBuffer().GetDirty())
2942 if (pos
<= GetBuffer().GetRange().GetEnd())
2944 SetCaretPosition(pos
, showAtLineStart
);
2954 /// Layout the buffer: which we must do before certain operations, such as
2955 /// setting the caret position.
2956 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2958 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2960 wxRect
availableSpace(GetClientSize());
2961 if (availableSpace
.width
== 0)
2962 availableSpace
.width
= 10;
2963 if (availableSpace
.height
== 0)
2964 availableSpace
.height
= 10;
2966 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2967 if (onlyVisibleRect
)
2969 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2970 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2973 wxClientDC
dc(this);
2974 dc
.SetFont(GetFont());
2978 GetBuffer().Defragment();
2979 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2980 GetBuffer().Layout(dc
, availableSpace
, flags
);
2981 GetBuffer().SetDirty(false);
2990 /// Is all of the selection bold?
2991 bool wxRichTextCtrl::IsSelectionBold()
2996 wxRichTextRange range
= GetSelectionRange();
2997 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2998 attr
.SetFontWeight(wxBOLD
);
3000 return HasCharacterAttributes(range
, attr
);
3004 // If no selection, then we need to combine current style with default style
3005 // to see what the effect would be if we started typing.
3007 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3009 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3010 if (GetStyle(pos
, attr
))
3012 if (IsDefaultStyleShowing())
3013 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3014 return attr
.GetFontWeight() == wxBOLD
;
3020 /// Is all of the selection italics?
3021 bool wxRichTextCtrl::IsSelectionItalics()
3025 wxRichTextRange range
= GetSelectionRange();
3027 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3028 attr
.SetFontStyle(wxITALIC
);
3030 return HasCharacterAttributes(range
, attr
);
3034 // If no selection, then we need to combine current style with default style
3035 // to see what the effect would be if we started typing.
3037 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3039 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3040 if (GetStyle(pos
, attr
))
3042 if (IsDefaultStyleShowing())
3043 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3044 return attr
.GetFontStyle() == wxITALIC
;
3050 /// Is all of the selection underlined?
3051 bool wxRichTextCtrl::IsSelectionUnderlined()
3055 wxRichTextRange range
= GetSelectionRange();
3057 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3058 attr
.SetFontUnderlined(true);
3060 return HasCharacterAttributes(range
, attr
);
3064 // If no selection, then we need to combine current style with default style
3065 // to see what the effect would be if we started typing.
3067 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3068 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3070 if (GetStyle(pos
, attr
))
3072 if (IsDefaultStyleShowing())
3073 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3074 return attr
.GetFontUnderlined();
3080 /// Apply bold to the selection
3081 bool wxRichTextCtrl::ApplyBoldToSelection()
3084 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3085 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
3088 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3091 wxRichTextAttr current
= GetDefaultStyleEx();
3092 current
.Apply(attr
);
3093 SetAndShowDefaultStyle(current
);
3098 /// Apply italic to the selection
3099 bool wxRichTextCtrl::ApplyItalicToSelection()
3102 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3103 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
3106 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3109 wxRichTextAttr current
= GetDefaultStyleEx();
3110 current
.Apply(attr
);
3111 SetAndShowDefaultStyle(current
);
3116 /// Apply underline to the selection
3117 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3120 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3121 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3124 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3127 wxRichTextAttr current
= GetDefaultStyleEx();
3128 current
.Apply(attr
);
3129 SetAndShowDefaultStyle(current
);
3134 /// Is all of the selection aligned according to the specified flag?
3135 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3137 wxRichTextRange range
;
3139 range
= GetSelectionRange();
3141 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3144 attr
.SetAlignment(alignment
);
3146 return HasParagraphAttributes(range
, attr
);
3149 /// Apply alignment to the selection
3150 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3153 attr
.SetAlignment(alignment
);
3155 return SetStyle(GetSelectionRange(), attr
);
3158 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3160 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3165 /// Apply a named style to the selection
3166 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3168 // Flags are defined within each definition, so only certain
3169 // attributes are applied.
3170 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3172 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3174 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3176 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3178 wxRichTextRange range
;
3181 range
= GetSelectionRange();
3184 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3185 range
= wxRichTextRange(pos
, pos
+1);
3188 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3191 // Make sure the attr has the style name
3192 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3194 attr
.SetParagraphStyleName(def
->GetName());
3196 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3197 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3198 // to change its style independently.
3199 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3202 attr
.SetCharacterStyleName(def
->GetName());
3205 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3208 wxRichTextAttr current
= GetDefaultStyleEx();
3209 current
.Apply(attr
);
3210 SetAndShowDefaultStyle(current
);
3215 /// Apply the style sheet to the buffer, for example if the styles have changed.
3216 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3219 styleSheet
= GetBuffer().GetStyleSheet();
3223 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3225 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3233 /// Sets the default style to the style under the cursor
3234 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3237 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3239 // If at the start of a paragraph, use the next position.
3240 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3242 if (GetUncombinedStyle(pos
, attr
))
3244 SetDefaultStyle(attr
);
3251 /// Returns the first visible position in the current view
3252 long wxRichTextCtrl::GetFirstVisiblePosition() const
3254 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3256 return line
->GetAbsoluteRange().GetStart();
3261 /// Get the first visible point in the window
3262 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3265 int startXUnits
, startYUnits
;
3267 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3268 GetViewStart(& startXUnits
, & startYUnits
);
3270 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3273 /// The adjusted caret position is the character position adjusted to take
3274 /// into account whether we're at the start of a paragraph, in which case
3275 /// style information should be taken from the next position, not current one.
3276 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3278 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3280 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3285 /// Get/set the selection range in character positions. -1, -1 means no selection.
3286 /// The range is in API convention, i.e. a single character selection is denoted
3288 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3290 wxRichTextRange range
= GetInternalSelectionRange();
3291 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3292 range
.SetEnd(range
.GetEnd() + 1);
3296 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3298 wxRichTextRange
range1(range
);
3299 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3300 range1
.SetEnd(range1
.GetEnd() - 1);
3302 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3304 SetInternalSelectionRange(range1
);
3308 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3310 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3313 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3315 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3318 /// Clear list for given range
3319 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3321 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3324 /// Number/renumber any list elements in the given range
3325 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3327 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3330 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3332 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3335 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3336 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3338 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3341 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3343 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3346 /// Deletes the content in the given range
3347 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3349 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3352 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3354 if (sm_availableFontNames
.GetCount() == 0)
3356 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3357 sm_availableFontNames
.Sort();
3359 return sm_availableFontNames
;
3362 void wxRichTextCtrl::ClearAvailableFontNames()
3364 sm_availableFontNames
.Clear();
3367 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3369 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3371 wxTextAttrEx basicStyle
= GetBasicStyle();
3372 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3373 SetBasicStyle(basicStyle
);
3374 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3379 // Refresh the area affected by a selection change
3380 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3382 // Calculate the refresh rectangle - just the affected lines
3383 long firstPos
, lastPos
;
3384 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3386 firstPos
= newSelection
.GetStart();
3387 lastPos
= newSelection
.GetEnd();
3389 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3391 firstPos
= oldSelection
.GetStart();
3392 lastPos
= oldSelection
.GetEnd();
3394 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3400 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3401 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3404 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3405 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3407 if (firstLine
&& lastLine
)
3409 wxSize clientSize
= GetClientSize();
3410 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3411 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3414 pt1
.y
= wxMax(0, pt1
.y
);
3416 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3418 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3419 RefreshRect(rect
, false);
3427 #if wxRICHTEXT_USE_OWN_CARET
3429 // ----------------------------------------------------------------------------
3430 // initialization and destruction
3431 // ----------------------------------------------------------------------------
3433 void wxRichTextCaret::Init()
3439 m_richTextCtrl
= NULL
;
3440 m_needsUpdate
= false;
3443 wxRichTextCaret::~wxRichTextCaret()
3447 // ----------------------------------------------------------------------------
3448 // showing/hiding/moving the caret (base class interface)
3449 // ----------------------------------------------------------------------------
3451 void wxRichTextCaret::DoShow()
3456 void wxRichTextCaret::DoHide()
3461 void wxRichTextCaret::DoMove()
3467 if (m_xOld
!= -1 && m_yOld
!= -1)
3471 wxRect
rect(GetPosition(), GetSize());
3472 m_richTextCtrl
->RefreshRect(rect
, false);
3481 void wxRichTextCaret::DoSize()
3483 int countVisible
= m_countVisible
;
3484 if (countVisible
> 0)
3490 if (countVisible
> 0)
3492 m_countVisible
= countVisible
;
3497 // ----------------------------------------------------------------------------
3498 // handling the focus
3499 // ----------------------------------------------------------------------------
3501 void wxRichTextCaret::OnSetFocus()
3509 void wxRichTextCaret::OnKillFocus()
3514 // ----------------------------------------------------------------------------
3515 // drawing the caret
3516 // ----------------------------------------------------------------------------
3518 void wxRichTextCaret::Refresh()
3522 wxRect
rect(GetPosition(), GetSize());
3523 m_richTextCtrl
->RefreshRect(rect
, false);
3527 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3529 dc
->SetPen( *wxBLACK_PEN
);
3531 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3532 dc
->SetPen(*wxBLACK_PEN
);
3534 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3535 // done under wxGTK - no idea why
3536 //dc->SetLogicalFunction(wxINVERT);
3538 wxPoint
pt(m_x
, m_y
);
3542 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3544 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3547 // wxRICHTEXT_USE_OWN_CARET