1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
29 #include "wx/textfile.h"
31 #include "wx/filename.h"
32 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 #include "wx/fontenum.h"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
)
59 #if wxRICHTEXT_USE_OWN_CARET
64 * This implements a non-flashing cursor in case there
65 * are platform-specific problems with the generic caret.
66 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
69 class wxRichTextCaret
: public wxCaret
74 // default - use Create()
75 wxRichTextCaret() { Init(); }
76 // creates a block caret associated with the given window
77 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
78 : wxCaret(window
, width
, height
) { Init(); m_richTextCtrl
= window
; }
79 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
80 : wxCaret(window
, size
) { Init(); m_richTextCtrl
= window
; }
82 virtual ~wxRichTextCaret();
87 // called by wxWindow (not using the event tables)
88 virtual void OnSetFocus();
89 virtual void OnKillFocus();
91 // draw the caret on the given DC
92 void DoDraw(wxDC
*dc
);
94 // get the visible count
95 int GetVisibleCount() const { return m_countVisible
; }
97 // delay repositioning
98 bool GetNeedsUpdate() const { return m_needsUpdate
; }
99 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
102 virtual void DoShow();
103 virtual void DoHide();
104 virtual void DoMove();
105 virtual void DoSize();
115 bool m_hasFocus
; // true => our window has focus
116 bool m_needsUpdate
; // must be repositioned
118 wxRichTextCtrl
* m_richTextCtrl
;
122 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
124 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
126 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
127 EVT_PAINT(wxRichTextCtrl::OnPaint
)
128 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
129 EVT_IDLE(wxRichTextCtrl::OnIdle
)
130 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
131 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
132 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
133 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
134 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
135 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
136 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
137 EVT_CHAR(wxRichTextCtrl::OnChar
)
138 EVT_SIZE(wxRichTextCtrl::OnSize
)
139 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
140 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
141 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
142 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
143 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
145 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
146 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
148 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
149 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
151 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
152 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
154 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
155 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
157 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
158 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
160 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
161 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
163 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
164 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
171 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
173 wxRichTextCtrl::wxRichTextCtrl()
174 : wxScrollHelper(this)
179 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
181 const wxString
& value
,
185 const wxValidator
& validator
,
186 const wxString
& name
)
187 : wxScrollHelper(this)
190 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
194 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
195 const wxValidator
& validator
, const wxString
& name
)
197 if (!wxControl::Create(parent
, id
, pos
, size
,
198 style
|wxFULL_REPAINT_ON_RESIZE
,
204 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
207 if (style
& wxTE_READONLY
)
210 // The base attributes must all have default values
211 wxTextAttr attributes
;
212 attributes
.SetFont(GetFont());
213 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
214 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
215 attributes
.SetLineSpacing(10);
216 attributes
.SetParagraphSpacingAfter(10);
217 attributes
.SetParagraphSpacingBefore(0);
218 attributes
.SetTextEffects(0);
219 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
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);
261 wxRichTextCtrl::~wxRichTextCtrl()
263 GetBuffer().RemoveEventHandler(this);
265 delete m_contextMenu
;
268 /// Member initialisation
269 void wxRichTextCtrl::Init()
271 m_contextMenu
= NULL
;
273 m_caretPosition
= -1;
274 m_selectionRange
.SetRange(-2, -2);
275 m_selectionAnchor
= -2;
277 m_caretAtLineStart
= false;
279 m_fullLayoutRequired
= false;
280 m_fullLayoutTime
= 0;
281 m_fullLayoutSavedPosition
= 0;
282 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
283 m_caretPositionForDefaultStyle
= -2;
286 void wxRichTextCtrl::DoThaw()
288 if (GetBuffer().GetDirty())
296 void wxRichTextCtrl::Clear()
298 m_buffer
.ResetAndClearCommands();
299 m_buffer
.SetDirty(true);
300 m_caretPosition
= -1;
301 m_caretPositionForDefaultStyle
= -2;
302 m_caretAtLineStart
= false;
303 m_selectionRange
.SetRange(-2, -2);
313 wxTextCtrl::SendTextUpdatedEvent(this);
317 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
319 #if !wxRICHTEXT_USE_OWN_CARET
320 if (GetCaret() && !IsFrozen())
325 #if wxRICHTEXT_BUFFERED_PAINTING
326 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
336 dc
.SetFont(GetFont());
338 // Paint the background
341 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
343 wxRect
drawingArea(GetUpdateRegion().GetBox());
344 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
346 wxRect
availableSpace(GetClientSize());
347 if (GetBuffer().GetDirty())
349 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
350 GetBuffer().SetDirty(false);
354 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
355 #if wxRICHTEXT_USE_OWN_CARET
356 if (GetCaret()->IsVisible())
358 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
363 #if !wxRICHTEXT_USE_OWN_CARET
370 // Empty implementation, to prevent flicker
371 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
375 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
379 #if !wxRICHTEXT_USE_OWN_CARET
385 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
386 // Work around dropouts when control is focused
394 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
399 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
400 // Work around dropouts when control is focused
408 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
414 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
420 dc
.SetFont(GetFont());
423 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
425 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
427 m_dragStart
= event
.GetLogicalPosition(dc
);
431 bool caretAtLineStart
= false;
433 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
435 // If we're at the start of a line (but not first in para)
436 // then we should keep the caret showing at the start of the line
437 // by showing the m_caretAtLineStart flag.
438 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
439 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
441 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
442 caretAtLineStart
= true;
446 long oldCaretPos
= m_caretPosition
;
448 MoveCaret(position
, caretAtLineStart
);
449 SetDefaultStyleToCursorStyle();
451 if (event
.ShiftDown())
453 if (m_selectionRange
.GetStart() == -2)
454 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
456 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
466 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
471 if (GetCapture() == this)
474 // See if we clicked on a URL
477 dc
.SetFont(GetFont());
480 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
481 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
483 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
485 wxRichTextEvent
cmdEvent(
486 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
488 cmdEvent
.SetEventObject(this);
489 cmdEvent
.SetPosition(m_caretPosition
+1);
491 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
494 if (GetStyle(position
, attr
))
496 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
498 wxString urlTarget
= attr
.GetURL();
499 if (!urlTarget
.IsEmpty())
501 wxMouseEvent
mouseEvent(event
);
503 long startPos
= 0, endPos
= 0;
504 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
507 startPos
= obj
->GetRange().GetStart();
508 endPos
= obj
->GetRange().GetEnd();
511 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
512 InitCommandEvent(urlEvent
);
514 urlEvent
.SetString(urlTarget
);
516 GetEventHandler()->ProcessEvent(urlEvent
);
526 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
530 dc
.SetFont(GetFont());
533 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
534 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
536 // See if we need to change the cursor
539 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
542 if (GetStyle(position
, attr
))
544 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
546 SetCursor(m_urlCursor
);
548 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
550 SetCursor(m_textCursor
);
555 SetCursor(m_textCursor
);
558 if (!event
.Dragging())
564 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
566 // TODO: test closeness
568 bool caretAtLineStart
= false;
570 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
572 // If we're at the start of a line (but not first in para)
573 // then we should keep the caret showing at the start of the line
574 // by showing the m_caretAtLineStart flag.
575 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
576 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
578 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
579 caretAtLineStart
= true;
583 if (m_caretPosition
!= position
)
585 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
587 MoveCaret(position
, caretAtLineStart
);
588 SetDefaultStyleToCursorStyle();
594 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
598 wxRichTextEvent
cmdEvent(
599 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
601 cmdEvent
.SetEventObject(this);
602 cmdEvent
.SetPosition(m_caretPosition
+1);
604 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
608 /// Left-double-click
609 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
611 wxRichTextEvent
cmdEvent(
612 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
614 cmdEvent
.SetEventObject(this);
615 cmdEvent
.SetPosition(m_caretPosition
+1);
617 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
619 SelectWord(GetCaretPosition()+1);
624 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
626 wxRichTextEvent
cmdEvent(
627 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
629 cmdEvent
.SetEventObject(this);
630 cmdEvent
.SetPosition(m_caretPosition
+1);
632 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
637 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
641 flags
|= wxRICHTEXT_CTRL_DOWN
;
642 if (event
.ShiftDown())
643 flags
|= wxRICHTEXT_SHIFT_DOWN
;
645 flags
|= wxRICHTEXT_ALT_DOWN
;
647 if (event
.GetKeyCode() == WXK_LEFT
||
648 event
.GetKeyCode() == WXK_RIGHT
||
649 event
.GetKeyCode() == WXK_UP
||
650 event
.GetKeyCode() == WXK_DOWN
||
651 event
.GetKeyCode() == WXK_HOME
||
652 event
.GetKeyCode() == WXK_PAGEUP
||
653 event
.GetKeyCode() == WXK_PAGEDOWN
||
654 event
.GetKeyCode() == WXK_END
||
656 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
657 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
658 event
.GetKeyCode() == WXK_NUMPAD_UP
||
659 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
660 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
661 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
662 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
663 event
.GetKeyCode() == WXK_NUMPAD_END
)
665 KeyboardNavigate(event
.GetKeyCode(), flags
);
669 // all the other keys modify the controls contents which shouldn't be
670 // possible if we're read-only
677 if (event
.GetKeyCode() == WXK_RETURN
)
679 BeginBatchUndo(_("Insert Text"));
681 long newPos
= m_caretPosition
;
683 DeleteSelectedContent(& newPos
);
685 if (event
.ShiftDown())
688 text
= wxRichTextLineBreakChar
;
689 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
692 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
695 SetDefaultStyleToCursorStyle();
697 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
699 wxRichTextEvent
cmdEvent(
700 wxEVT_COMMAND_RICHTEXT_RETURN
,
702 cmdEvent
.SetEventObject(this);
703 cmdEvent
.SetFlags(flags
);
704 cmdEvent
.SetPosition(newPos
+1);
706 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
708 // Generate conventional event
709 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
710 InitCommandEvent(textEvent
);
712 GetEventHandler()->ProcessEvent(textEvent
);
716 else if (event
.GetKeyCode() == WXK_BACK
)
718 BeginBatchUndo(_("Delete Text"));
720 // Submit range in character positions, which are greater than caret positions,
721 // so subtract 1 for deleted character and add 1 for conversion to character position.
722 if (m_caretPosition
> -1 && !HasSelection())
724 bool processed
= false;
727 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
728 if (pos
!= -1 && (pos
< m_caretPosition
))
730 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, m_caretPosition
), this);
736 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
739 DeleteSelectedContent();
743 if (GetLastPosition() == -1)
747 m_caretPosition
= -1;
749 SetDefaultStyleToCursorStyle();
752 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
754 wxRichTextEvent
cmdEvent(
755 wxEVT_COMMAND_RICHTEXT_DELETE
,
757 cmdEvent
.SetEventObject(this);
758 cmdEvent
.SetFlags(flags
);
759 cmdEvent
.SetPosition(m_caretPosition
+1);
760 GetEventHandler()->ProcessEvent(cmdEvent
);
764 else if (event
.GetKeyCode() == WXK_DELETE
)
766 BeginBatchUndo(_("Delete Text"));
768 // Submit range in character positions, which are greater than caret positions,
769 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
771 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
774 DeleteSelectedContent();
778 if (GetLastPosition() == -1)
782 m_caretPosition
= -1;
784 SetDefaultStyleToCursorStyle();
787 wxRichTextEvent
cmdEvent(
788 wxEVT_COMMAND_RICHTEXT_DELETE
,
790 cmdEvent
.SetEventObject(this);
791 cmdEvent
.SetFlags(flags
);
792 cmdEvent
.SetPosition(m_caretPosition
+1);
793 GetEventHandler()->ProcessEvent(cmdEvent
);
799 long keycode
= event
.GetKeyCode();
872 case WXK_NUMPAD_SPACE
:
874 case WXK_NUMPAD_ENTER
:
879 case WXK_NUMPAD_HOME
:
880 case WXK_NUMPAD_LEFT
:
882 case WXK_NUMPAD_RIGHT
:
883 case WXK_NUMPAD_DOWN
:
884 case WXK_NUMPAD_PAGEUP
:
885 case WXK_NUMPAD_PAGEDOWN
:
887 case WXK_NUMPAD_BEGIN
:
888 case WXK_NUMPAD_INSERT
:
889 case WXK_NUMPAD_DELETE
:
890 case WXK_NUMPAD_EQUAL
:
891 case WXK_NUMPAD_MULTIPLY
:
893 case WXK_NUMPAD_SEPARATOR
:
894 case WXK_NUMPAD_SUBTRACT
:
895 case WXK_NUMPAD_DECIMAL
:
896 case WXK_WINDOWS_LEFT
:
904 if (event
.CmdDown() || event
.AltDown())
910 wxRichTextEvent
cmdEvent(
911 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
913 cmdEvent
.SetEventObject(this);
914 cmdEvent
.SetFlags(flags
);
916 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
918 cmdEvent
.SetCharacter((wxChar
) keycode
);
920 cmdEvent
.SetPosition(m_caretPosition
+1);
922 if (keycode
== wxT('\t'))
924 // See if we need to promote or demote the selection or paragraph at the cursor
925 // position, instead of inserting a tab.
926 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
927 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
928 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
930 wxRichTextRange range
;
932 range
= GetSelectionRange();
934 range
= para
->GetRange().FromInternal();
936 int promoteBy
= event
.ShiftDown() ? 1 : -1;
938 PromoteList(promoteBy
, range
, NULL
);
940 GetEventHandler()->ProcessEvent(cmdEvent
);
946 BeginBatchUndo(_("Insert Text"));
948 long newPos
= m_caretPosition
;
949 DeleteSelectedContent(& newPos
);
952 wxString str
= event
.GetUnicodeKey();
954 wxString str
= (wxChar
) event
.GetKeyCode();
956 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
960 SetDefaultStyleToCursorStyle();
961 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
963 GetEventHandler()->ProcessEvent(cmdEvent
);
971 /// Delete content if there is a selection, e.g. when pressing a key.
972 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
976 long pos
= m_selectionRange
.GetStart();
977 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
978 m_selectionRange
.SetRange(-2, -2);
988 /// Keyboard navigation
992 Left: left one character
993 Right: right one character
996 Ctrl-Left: left one word
997 Ctrl-Right: right one word
998 Ctrl-Up: previous paragraph start
999 Ctrl-Down: next start of paragraph
1002 Ctrl-Home: start of document
1003 Ctrl-End: end of document
1004 Page-Up: Up a screen
1005 Page-Down: Down a screen
1009 Ctrl-Alt-PgUp: Start of window
1010 Ctrl-Alt-PgDn: End of window
1011 F8: Start selection mode
1012 Esc: End selection mode
1014 Adding Shift does the above but starts/extends selection.
1019 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1021 bool success
= false;
1023 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1025 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1026 success
= WordRight(1, flags
);
1028 success
= MoveRight(1, flags
);
1030 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1032 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1033 success
= WordLeft(1, flags
);
1035 success
= MoveLeft(1, flags
);
1037 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1039 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1040 success
= MoveToParagraphStart(flags
);
1042 success
= MoveUp(1, flags
);
1044 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1046 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1047 success
= MoveToParagraphEnd(flags
);
1049 success
= MoveDown(1, flags
);
1051 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1053 success
= PageUp(1, flags
);
1055 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1057 success
= PageDown(1, flags
);
1059 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1061 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1062 success
= MoveHome(flags
);
1064 success
= MoveToLineStart(flags
);
1066 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1068 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1069 success
= MoveEnd(flags
);
1071 success
= MoveToLineEnd(flags
);
1076 ScrollIntoView(m_caretPosition
, keyCode
);
1077 SetDefaultStyleToCursorStyle();
1083 /// Extend the selection. Selections are in caret positions.
1084 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1086 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1088 wxRichTextRange oldSelection
= m_selectionRange
;
1090 // If not currently selecting, start selecting
1091 if (m_selectionRange
.GetStart() == -2)
1093 m_selectionAnchor
= oldPos
;
1095 if (oldPos
> newPos
)
1096 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1098 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1102 // Always ensure that the selection range start is greater than
1104 if (newPos
> m_selectionAnchor
)
1105 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1107 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1110 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1112 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1114 wxLogDebug(wxT("Strange selection range"));
1123 /// Scroll into view, returning true if we scrolled.
1124 /// This takes a _caret_ position.
1125 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1127 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1133 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1135 int startXUnits
, startYUnits
;
1136 GetViewStart(& startXUnits
, & startYUnits
);
1137 int startY
= startYUnits
* ppuY
;
1140 GetVirtualSize(& sx
, & sy
);
1146 wxRect rect
= line
->GetRect();
1148 bool scrolled
= false;
1150 wxSize clientSize
= GetClientSize();
1153 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1154 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1155 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1156 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1158 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1160 // Make it scroll so this item is at the bottom
1162 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1163 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1165 // If we're still off the screen, scroll another line down
1166 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1169 if (startYUnits
!= yUnits
)
1171 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1175 else if (rect
.y
< startY
)
1177 // Make it scroll so this item is at the top
1180 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1182 if (startYUnits
!= yUnits
)
1184 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1190 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1191 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1192 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1193 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1195 if (rect
.y
< startY
)
1197 // Make it scroll so this item is at the top
1200 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1202 if (startYUnits
!= yUnits
)
1204 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1208 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1210 // Make it scroll so this item is at the bottom
1212 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1213 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1215 // If we're still off the screen, scroll another line down
1216 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1219 if (startYUnits
!= yUnits
)
1221 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1227 #if !wxRICHTEXT_USE_OWN_CARET
1235 /// Is the given position visible on the screen?
1236 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1238 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1244 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1247 GetViewStart(& startX
, & startY
);
1249 startY
= startY
* ppuY
;
1251 wxRect rect
= line
->GetRect();
1252 wxSize clientSize
= GetClientSize();
1254 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1257 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1259 m_caretPosition
= position
;
1260 m_caretAtLineStart
= showAtLineStart
;
1263 /// Move caret one visual step forward: this may mean setting a flag
1264 /// and keeping the same position if we're going from the end of one line
1265 /// to the start of the next, which may be the exact same caret position.
1266 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1268 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1270 // Only do the check if we're not at the end of the paragraph (where things work OK
1272 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1274 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1278 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1280 // We're at the end of a line. See whether we need to
1281 // stay at the same actual caret position but change visual
1282 // position, or not.
1283 if (oldPosition
== lineRange
.GetEnd())
1285 if (m_caretAtLineStart
)
1287 // We're already at the start of the line, so actually move on now.
1288 m_caretPosition
= oldPosition
+ 1;
1289 m_caretAtLineStart
= false;
1293 // We're showing at the end of the line, so keep to
1294 // the same position but indicate that we're to show
1295 // at the start of the next line.
1296 m_caretPosition
= oldPosition
;
1297 m_caretAtLineStart
= true;
1299 SetDefaultStyleToCursorStyle();
1305 SetDefaultStyleToCursorStyle();
1308 /// Move caret one visual step backward: this may mean setting a flag
1309 /// and keeping the same position if we're going from the end of one line
1310 /// to the start of the next, which may be the exact same caret position.
1311 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1313 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1315 // Only do the check if we're not at the start of the paragraph (where things work OK
1317 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1319 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1323 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1325 // We're at the start of a line. See whether we need to
1326 // stay at the same actual caret position but change visual
1327 // position, or not.
1328 if (oldPosition
== lineRange
.GetStart())
1330 m_caretPosition
= oldPosition
-1;
1331 m_caretAtLineStart
= true;
1334 else if (oldPosition
== lineRange
.GetEnd())
1336 if (m_caretAtLineStart
)
1338 // We're at the start of the line, so keep the same caret position
1339 // but clear the start-of-line flag.
1340 m_caretPosition
= oldPosition
;
1341 m_caretAtLineStart
= false;
1345 // We're showing at the end of the line, so go back
1346 // to the previous character position.
1347 m_caretPosition
= oldPosition
- 1;
1349 SetDefaultStyleToCursorStyle();
1355 SetDefaultStyleToCursorStyle();
1359 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1361 long endPos
= GetBuffer().GetRange().GetEnd();
1363 if (m_caretPosition
+ noPositions
< endPos
)
1365 long oldPos
= m_caretPosition
;
1366 long newPos
= m_caretPosition
+ noPositions
;
1368 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1372 // Determine by looking at oldPos and m_caretPosition whether
1373 // we moved from the end of a line to the start of the next line, in which case
1374 // we want to adjust the caret position such that it is positioned at the
1375 // start of the next line, rather than jumping past the first character of the
1377 if (noPositions
== 1 && !extendSel
)
1378 MoveCaretForward(oldPos
);
1380 SetCaretPosition(newPos
);
1383 SetDefaultStyleToCursorStyle();
1392 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1396 if (m_caretPosition
> startPos
- noPositions
+ 1)
1398 long oldPos
= m_caretPosition
;
1399 long newPos
= m_caretPosition
- noPositions
;
1400 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1404 if (noPositions
== 1 && !extendSel
)
1405 MoveCaretBack(oldPos
);
1407 SetCaretPosition(newPos
);
1410 SetDefaultStyleToCursorStyle();
1419 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1421 return MoveDown(- noLines
, flags
);
1425 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1430 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1431 wxPoint pt
= GetCaret()->GetPosition();
1432 long newLine
= lineNumber
+ noLines
;
1434 if (lineNumber
!= -1)
1438 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1440 if (newLine
> lastLine
)
1450 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1453 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1459 wxClientDC
dc(this);
1461 dc
.SetFont(GetFont());
1463 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1465 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1467 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1468 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1469 // so we view the caret at the start of the line.
1470 bool caretLineStart
= false;
1471 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1473 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1474 wxRichTextRange lineRange
;
1476 lineRange
= thisLine
->GetAbsoluteRange();
1478 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1481 caretLineStart
= true;
1485 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1486 if (para
&& para
->GetRange().GetStart() == newPos
)
1491 long newSelEnd
= newPos
;
1493 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1497 SetCaretPosition(newPos
, caretLineStart
);
1499 SetDefaultStyleToCursorStyle();
1507 /// Move to the end of the paragraph
1508 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1510 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1513 long newPos
= para
->GetRange().GetEnd() - 1;
1514 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1518 SetCaretPosition(newPos
);
1520 SetDefaultStyleToCursorStyle();
1528 /// Move to the start of the paragraph
1529 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1531 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1534 long newPos
= para
->GetRange().GetStart() - 1;
1535 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1539 SetCaretPosition(newPos
);
1541 SetDefaultStyleToCursorStyle();
1549 /// Move to the end of the line
1550 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1552 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1556 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1557 long newPos
= lineRange
.GetEnd();
1558 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1562 SetCaretPosition(newPos
);
1564 SetDefaultStyleToCursorStyle();
1572 /// Move to the start of the line
1573 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1575 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1578 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1579 long newPos
= lineRange
.GetStart()-1;
1581 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1585 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1587 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1589 SetDefaultStyleToCursorStyle();
1597 /// Move to the start of the buffer
1598 bool wxRichTextCtrl::MoveHome(int flags
)
1600 if (m_caretPosition
!= -1)
1602 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1606 SetCaretPosition(-1);
1608 SetDefaultStyleToCursorStyle();
1616 /// Move to the end of the buffer
1617 bool wxRichTextCtrl::MoveEnd(int flags
)
1619 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1621 if (m_caretPosition
!= endPos
)
1623 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1627 SetCaretPosition(endPos
);
1629 SetDefaultStyleToCursorStyle();
1637 /// Move noPages pages up
1638 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1640 return PageDown(- noPages
, flags
);
1643 /// Move noPages pages down
1644 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1646 // Calculate which line occurs noPages * screen height further down.
1647 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1650 wxSize clientSize
= GetClientSize();
1651 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1653 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1656 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1657 long pos
= lineRange
.GetStart()-1;
1658 if (pos
!= m_caretPosition
)
1660 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1662 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1666 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1668 SetDefaultStyleToCursorStyle();
1678 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1680 return str
== wxT(" ") || str
== wxT("\t");
1683 // Finds the caret position for the next word
1684 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1686 long endPos
= GetBuffer().GetRange().GetEnd();
1690 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1692 // First skip current text to space
1693 while (i
< endPos
&& i
> -1)
1695 // i is in character, not caret positions
1696 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1697 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1698 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1702 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1709 while (i
< endPos
&& i
> -1)
1711 // i is in character, not caret positions
1712 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1713 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1714 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1715 return wxMax(-1, i
);
1717 if (text
.empty()) // End of paragraph, or maybe an image
1718 return wxMax(-1, i
- 1);
1719 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1723 // Convert to caret position
1724 return wxMax(-1, i
- 1);
1733 long i
= m_caretPosition
;
1735 // First skip white space
1736 while (i
< endPos
&& i
> -1)
1738 // i is in character, not caret positions
1739 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1740 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1742 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1744 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1749 // Next skip current text to space
1750 while (i
< endPos
&& i
> -1)
1752 // i is in character, not caret positions
1753 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1754 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1755 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1758 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1771 /// Move n words left
1772 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1774 long pos
= FindNextWordPosition(-1);
1775 if (pos
!= m_caretPosition
)
1777 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1779 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1783 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1785 SetDefaultStyleToCursorStyle();
1793 /// Move n words right
1794 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1796 long pos
= FindNextWordPosition(1);
1797 if (pos
!= m_caretPosition
)
1799 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1801 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1805 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1807 SetDefaultStyleToCursorStyle();
1816 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1818 // Only do sizing optimization for large buffers
1819 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1821 m_fullLayoutRequired
= true;
1822 m_fullLayoutTime
= wxGetLocalTimeMillis();
1823 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1824 LayoutContent(true /* onlyVisibleRect */);
1827 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1829 #if wxRICHTEXT_BUFFERED_PAINTING
1837 /// Idle-time processing
1838 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1840 #if wxRICHTEXT_USE_OWN_CARET
1841 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1843 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1849 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1851 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1853 m_fullLayoutRequired
= false;
1854 m_fullLayoutTime
= 0;
1855 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1856 ShowPosition(m_fullLayoutSavedPosition
);
1860 if (m_caretPositionForDefaultStyle
!= -2)
1862 // If the caret position has changed, no longer reflect the default style
1864 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1865 m_caretPositionForDefaultStyle
= -2;
1872 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1874 #if wxRICHTEXT_USE_OWN_CARET
1875 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1878 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1885 /// Set up scrollbars, e.g. after a resize
1886 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1891 if (GetBuffer().IsEmpty())
1893 SetScrollbars(0, 0, 0, 0, 0, 0);
1897 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1898 // of pixels. See e.g. wxVScrolledWindow for ideas.
1899 int pixelsPerUnit
= 5;
1900 wxSize clientSize
= GetClientSize();
1902 int maxHeight
= GetBuffer().GetCachedSize().y
;
1904 // Round up so we have at least maxHeight pixels
1905 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1907 int startX
= 0, startY
= 0;
1909 GetViewStart(& startX
, & startY
);
1911 int maxPositionX
= 0;
1912 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1914 int newStartX
= wxMin(maxPositionX
, startX
);
1915 int newStartY
= wxMin(maxPositionY
, startY
);
1917 int oldPPUX
, oldPPUY
;
1918 int oldStartX
, oldStartY
;
1919 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
1920 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
1921 GetViewStart(& oldStartX
, & oldStartY
);
1922 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
1924 oldVirtualSizeY
/= oldPPUY
;
1926 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
1929 // Don't set scrollbars if there were none before, and there will be none now.
1930 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
1933 // Move to previous scroll position if
1935 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
1938 /// Paint the background
1939 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1941 wxColour backgroundColour
= GetBackgroundColour();
1942 if (!backgroundColour
.Ok())
1943 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1945 // Clear the background
1946 dc
.SetBrush(wxBrush(backgroundColour
));
1947 dc
.SetPen(*wxTRANSPARENT_PEN
);
1948 wxRect
windowRect(GetClientSize());
1949 windowRect
.x
-= 2; windowRect
.y
-= 2;
1950 windowRect
.width
+= 4; windowRect
.height
+= 4;
1952 // We need to shift the rectangle to take into account
1953 // scrolling. Converting device to logical coordinates.
1954 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1955 dc
.DrawRectangle(windowRect
);
1958 #if wxRICHTEXT_BUFFERED_PAINTING
1959 /// Recreate buffer bitmap if necessary
1960 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1963 if (sz
== wxDefaultSize
)
1964 sz
= GetClientSize();
1966 if (sz
.x
< 1 || sz
.y
< 1)
1969 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1970 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1971 return m_bufferBitmap
.Ok();
1975 // ----------------------------------------------------------------------------
1976 // file IO functions
1977 // ----------------------------------------------------------------------------
1979 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1981 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1983 m_filename
= filename
;
1986 SetInsertionPoint(0);
1989 SetupScrollbars(true);
1991 wxTextCtrl::SendTextUpdatedEvent(this);
1997 wxLogError(_("File couldn't be loaded."));
2003 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2005 if (GetBuffer().SaveFile(filename
, fileType
))
2007 m_filename
= filename
;
2014 wxLogError(_("The text couldn't be saved."));
2019 // ----------------------------------------------------------------------------
2020 // wxRichTextCtrl specific functionality
2021 // ----------------------------------------------------------------------------
2023 /// Add a new paragraph of text to the end of the buffer
2024 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2026 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2032 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2034 wxRichTextRange range
= GetBuffer().AddImage(image
);
2039 // ----------------------------------------------------------------------------
2040 // selection and ranges
2041 // ----------------------------------------------------------------------------
2043 void wxRichTextCtrl::SelectAll()
2045 SetSelection(0, GetLastPosition()+1);
2046 m_selectionAnchor
= -1;
2050 void wxRichTextCtrl::SelectNone()
2052 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2054 wxRichTextRange oldSelection
= m_selectionRange
;
2056 m_selectionRange
= wxRichTextRange(-2, -2);
2058 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2060 m_selectionAnchor
= -2;
2063 static bool wxIsWordDelimiter(const wxString
& text
)
2065 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2068 /// Select the word at the given character position
2069 bool wxRichTextCtrl::SelectWord(long position
)
2071 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2074 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2078 if (position
== para
->GetRange().GetEnd())
2081 long positionStart
= position
;
2082 long positionEnd
= position
;
2084 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2086 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2087 if (wxIsWordDelimiter(text
))
2093 if (positionStart
< para
->GetRange().GetStart())
2094 positionStart
= para
->GetRange().GetStart();
2096 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2098 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2099 if (wxIsWordDelimiter(text
))
2105 if (positionEnd
>= para
->GetRange().GetEnd())
2106 positionEnd
= para
->GetRange().GetEnd();
2108 if (positionEnd
< positionStart
)
2111 SetSelection(positionStart
, positionEnd
+1);
2113 if (positionStart
>= 0)
2115 MoveCaret(positionStart
-1, true);
2116 SetDefaultStyleToCursorStyle();
2122 wxString
wxRichTextCtrl::GetStringSelection() const
2125 GetSelection(&from
, &to
);
2127 return GetRange(from
, to
);
2130 // ----------------------------------------------------------------------------
2132 // ----------------------------------------------------------------------------
2134 wxTextCtrlHitTestResult
2135 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2137 // implement in terms of the other overload as the native ports typically
2138 // can get the position and not (x, y) pair directly (although wxUniv
2139 // directly gets x and y -- and so overrides this method as well)
2141 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2143 if ( rc
!= wxTE_HT_UNKNOWN
)
2145 PositionToXY(pos
, x
, y
);
2151 wxTextCtrlHitTestResult
2152 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2155 wxClientDC
dc((wxRichTextCtrl
*) this);
2156 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2158 // Buffer uses logical position (relative to start of buffer)
2160 wxPoint pt2
= GetLogicalPoint(pt
);
2162 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2164 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2165 return wxTE_HT_BEFORE
;
2166 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2167 return wxTE_HT_BEYOND
;
2168 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2169 return wxTE_HT_ON_TEXT
;
2171 return wxTE_HT_UNKNOWN
;
2174 // ----------------------------------------------------------------------------
2175 // set/get the controls text
2176 // ----------------------------------------------------------------------------
2178 wxString
wxRichTextCtrl::GetValue() const
2180 return GetBuffer().GetText();
2183 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2185 // Public API for range is different from internals
2186 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2189 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2191 // Don't call Clear here, since it always sends a text updated event
2192 m_buffer
.ResetAndClearCommands();
2193 m_buffer
.SetDirty(true);
2194 m_caretPosition
= -1;
2195 m_caretPositionForDefaultStyle
= -2;
2196 m_caretAtLineStart
= false;
2197 m_selectionRange
.SetRange(-2, -2);
2207 if (!value
.IsEmpty())
2209 // Remove empty paragraph
2210 GetBuffer().Clear();
2211 DoWriteText(value
, flags
);
2213 // for compatibility, don't move the cursor when doing SetValue()
2214 SetInsertionPoint(0);
2218 // still send an event for consistency
2219 if (flags
& SetValue_SendEvent
)
2220 wxTextCtrl::SendTextUpdatedEvent(this);
2225 void wxRichTextCtrl::WriteText(const wxString
& value
)
2230 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2232 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2234 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2236 if ( flags
& SetValue_SendEvent
)
2237 wxTextCtrl::SendTextUpdatedEvent(this);
2240 void wxRichTextCtrl::AppendText(const wxString
& text
)
2242 SetInsertionPointEnd();
2247 /// Write an image at the current insertion point
2248 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2250 wxRichTextImageBlock imageBlock
;
2252 wxImage image2
= image
;
2253 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2254 return WriteImage(imageBlock
);
2259 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2261 wxRichTextImageBlock imageBlock
;
2264 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2265 return WriteImage(imageBlock
);
2270 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2272 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2275 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2279 wxRichTextImageBlock imageBlock
;
2281 wxImage image
= bitmap
.ConvertToImage();
2282 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2283 return WriteImage(imageBlock
);
2289 /// Insert a newline (actually paragraph) at the current insertion point.
2290 bool wxRichTextCtrl::Newline()
2292 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2295 /// Insert a line break at the current insertion point.
2296 bool wxRichTextCtrl::LineBreak()
2299 text
= wxRichTextLineBreakChar
;
2300 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2303 // ----------------------------------------------------------------------------
2304 // Clipboard operations
2305 // ----------------------------------------------------------------------------
2307 void wxRichTextCtrl::Copy()
2311 wxRichTextRange range
= GetInternalSelectionRange();
2312 GetBuffer().CopyToClipboard(range
);
2316 void wxRichTextCtrl::Cut()
2320 wxRichTextRange range
= GetInternalSelectionRange();
2321 GetBuffer().CopyToClipboard(range
);
2323 DeleteSelectedContent();
2329 void wxRichTextCtrl::Paste()
2333 BeginBatchUndo(_("Paste"));
2335 long newPos
= m_caretPosition
;
2336 DeleteSelectedContent(& newPos
);
2338 GetBuffer().PasteFromClipboard(newPos
);
2344 void wxRichTextCtrl::DeleteSelection()
2346 if (CanDeleteSelection())
2348 DeleteSelectedContent();
2352 bool wxRichTextCtrl::HasSelection() const
2354 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2357 bool wxRichTextCtrl::CanCopy() const
2359 // Can copy if there's a selection
2360 return HasSelection();
2363 bool wxRichTextCtrl::CanCut() const
2365 return HasSelection() && IsEditable();
2368 bool wxRichTextCtrl::CanPaste() const
2370 if ( !IsEditable() )
2373 return GetBuffer().CanPasteFromClipboard();
2376 bool wxRichTextCtrl::CanDeleteSelection() const
2378 return HasSelection() && IsEditable();
2382 // ----------------------------------------------------------------------------
2384 // ----------------------------------------------------------------------------
2386 void wxRichTextCtrl::SetEditable(bool editable
)
2388 m_editable
= editable
;
2391 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2395 m_caretPosition
= pos
- 1;
2400 void wxRichTextCtrl::SetInsertionPointEnd()
2402 long pos
= GetLastPosition();
2403 SetInsertionPoint(pos
);
2406 long wxRichTextCtrl::GetInsertionPoint() const
2408 return m_caretPosition
+1;
2411 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2413 return GetBuffer().GetRange().GetEnd();
2416 // If the return values from and to are the same, there is no
2418 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2420 *from
= m_selectionRange
.GetStart();
2421 *to
= m_selectionRange
.GetEnd();
2422 if ((*to
) != -1 && (*to
) != -2)
2426 bool wxRichTextCtrl::IsEditable() const
2431 // ----------------------------------------------------------------------------
2433 // ----------------------------------------------------------------------------
2435 void wxRichTextCtrl::SetSelection(long from
, long to
)
2437 // if from and to are both -1, it means (in wxWidgets) that all text should
2439 if ( (from
== -1) && (to
== -1) )
2442 to
= GetLastPosition()+1;
2445 DoSetSelection(from
, to
);
2446 SetDefaultStyleToCursorStyle();
2449 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2457 wxRichTextRange oldSelection
= m_selectionRange
;
2458 m_selectionAnchor
= from
;
2459 m_selectionRange
.SetRange(from
, to
-1);
2461 m_caretPosition
= from
-1;
2463 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2468 // ----------------------------------------------------------------------------
2470 // ----------------------------------------------------------------------------
2472 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2473 const wxString
& value
)
2475 BeginBatchUndo(_("Replace"));
2477 DeleteSelectedContent();
2479 DoWriteText(value
, SetValue_SelectionOnly
);
2484 void wxRichTextCtrl::Remove(long from
, long to
)
2488 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2495 bool wxRichTextCtrl::IsModified() const
2497 return m_buffer
.IsModified();
2500 void wxRichTextCtrl::MarkDirty()
2502 m_buffer
.Modify(true);
2505 void wxRichTextCtrl::DiscardEdits()
2507 m_caretPositionForDefaultStyle
= -2;
2508 m_buffer
.Modify(false);
2509 m_buffer
.GetCommandProcessor()->ClearCommands();
2512 int wxRichTextCtrl::GetNumberOfLines() const
2514 return GetBuffer().GetParagraphCount();
2517 // ----------------------------------------------------------------------------
2518 // Positions <-> coords
2519 // ----------------------------------------------------------------------------
2521 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2523 return GetBuffer().XYToPosition(x
, y
);
2526 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2528 return GetBuffer().PositionToXY(pos
, x
, y
);
2531 // ----------------------------------------------------------------------------
2533 // ----------------------------------------------------------------------------
2535 void wxRichTextCtrl::ShowPosition(long pos
)
2537 if (!IsPositionVisible(pos
))
2538 ScrollIntoView(pos
-1, WXK_DOWN
);
2541 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2543 return GetBuffer().GetParagraphLength(lineNo
);
2546 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2548 return GetBuffer().GetParagraphText(lineNo
);
2551 // ----------------------------------------------------------------------------
2553 // ----------------------------------------------------------------------------
2555 void wxRichTextCtrl::Undo()
2559 GetCommandProcessor()->Undo();
2563 void wxRichTextCtrl::Redo()
2567 GetCommandProcessor()->Redo();
2571 bool wxRichTextCtrl::CanUndo() const
2573 return GetCommandProcessor()->CanUndo();
2576 bool wxRichTextCtrl::CanRedo() const
2578 return GetCommandProcessor()->CanRedo();
2581 // ----------------------------------------------------------------------------
2582 // implementation details
2583 // ----------------------------------------------------------------------------
2585 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2587 SetValue(event
.GetString());
2588 GetEventHandler()->ProcessEvent(event
);
2591 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2593 // By default, load the first file into the text window.
2594 if (event
.GetNumberOfFiles() > 0)
2596 LoadFile(event
.GetFiles()[0]);
2600 wxSize
wxRichTextCtrl::DoGetBestSize() const
2602 return wxSize(10, 10);
2605 // ----------------------------------------------------------------------------
2606 // standard handlers for standard edit menu events
2607 // ----------------------------------------------------------------------------
2609 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2614 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2619 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2624 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2629 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2634 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2639 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2641 event
.Enable( CanCut() );
2644 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2646 event
.Enable( CanCopy() );
2649 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2651 event
.Enable( CanDeleteSelection() );
2654 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2656 event
.Enable( CanPaste() );
2659 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2661 event
.Enable( CanUndo() );
2662 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2665 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2667 event
.Enable( CanRedo() );
2668 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2671 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2676 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2678 event
.Enable(GetLastPosition() > 0);
2681 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2683 if (event
.GetEventObject() != this)
2691 m_contextMenu
= new wxMenu
;
2692 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2693 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2694 m_contextMenu
->AppendSeparator();
2695 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2696 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2697 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2698 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2699 m_contextMenu
->AppendSeparator();
2700 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2702 PopupMenu(m_contextMenu
);
2706 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2708 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2711 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2713 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2716 // extended style setting operation with flags including:
2717 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2718 // see richtextbuffer.h for more details.
2720 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2722 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2725 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2727 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2730 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2732 return GetBuffer().GetDefaultStyle();
2735 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2737 return GetBuffer().GetStyle(position
, style
);
2740 // get the common set of styles for the range
2741 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2743 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2746 /// Get the content (uncombined) attributes for this position.
2747 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2749 return GetBuffer().GetUncombinedStyle(position
, style
);
2752 /// Set font, and also the buffer attributes
2753 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2755 wxControl::SetFont(font
);
2757 wxTextAttr attr
= GetBuffer().GetAttributes();
2759 GetBuffer().SetBasicStyle(attr
);
2761 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2767 /// Transform logical to physical
2768 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2771 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2776 /// Transform physical to logical
2777 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2780 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2785 /// Position the caret
2786 void wxRichTextCtrl::PositionCaret()
2791 //wxLogDebug(wxT("PositionCaret"));
2794 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2796 wxPoint newPt
= caretRect
.GetPosition();
2797 wxSize newSz
= caretRect
.GetSize();
2798 wxPoint pt
= GetPhysicalPoint(newPt
);
2799 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2802 if (GetCaret()->GetSize() != newSz
)
2803 GetCaret()->SetSize(newSz
);
2804 GetCaret()->Move(pt
);
2810 /// Get the caret height and position for the given character position
2811 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2813 wxClientDC
dc(this);
2814 dc
.SetFont(GetFont());
2821 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2823 // Caret height can't be zero
2825 height
= dc
.GetCharHeight();
2827 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2834 /// Gets the line for the visible caret position. If the caret is
2835 /// shown at the very end of the line, it means the next character is actually
2836 /// on the following line. So let's get the line we're expecting to find
2837 /// if this is the case.
2838 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2840 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2841 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2844 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2845 if (caretPosition
== lineRange
.GetStart()-1 &&
2846 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2848 if (!m_caretAtLineStart
)
2849 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2856 /// Move the caret to the given character position
2857 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2859 if (GetBuffer().GetDirty())
2862 if (pos
<= GetBuffer().GetRange().GetEnd())
2864 SetCaretPosition(pos
, showAtLineStart
);
2874 /// Layout the buffer: which we must do before certain operations, such as
2875 /// setting the caret position.
2876 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2878 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2880 wxRect
availableSpace(GetClientSize());
2881 if (availableSpace
.width
== 0)
2882 availableSpace
.width
= 10;
2883 if (availableSpace
.height
== 0)
2884 availableSpace
.height
= 10;
2886 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2887 if (onlyVisibleRect
)
2889 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2890 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2893 wxClientDC
dc(this);
2894 dc
.SetFont(GetFont());
2898 GetBuffer().Defragment();
2899 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2900 GetBuffer().Layout(dc
, availableSpace
, flags
);
2901 GetBuffer().SetDirty(false);
2910 /// Is all of the selection bold?
2911 bool wxRichTextCtrl::IsSelectionBold()
2916 wxRichTextRange range
= GetSelectionRange();
2917 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2918 attr
.SetFontWeight(wxBOLD
);
2920 return HasCharacterAttributes(range
, attr
);
2924 // If no selection, then we need to combine current style with default style
2925 // to see what the effect would be if we started typing.
2927 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2929 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2930 if (GetStyle(pos
, attr
))
2932 if (IsDefaultStyleShowing())
2933 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2934 return attr
.GetFontWeight() == wxBOLD
;
2940 /// Is all of the selection italics?
2941 bool wxRichTextCtrl::IsSelectionItalics()
2945 wxRichTextRange range
= GetSelectionRange();
2947 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2948 attr
.SetFontStyle(wxITALIC
);
2950 return HasCharacterAttributes(range
, attr
);
2954 // If no selection, then we need to combine current style with default style
2955 // to see what the effect would be if we started typing.
2957 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2959 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2960 if (GetStyle(pos
, attr
))
2962 if (IsDefaultStyleShowing())
2963 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2964 return attr
.GetFontStyle() == wxITALIC
;
2970 /// Is all of the selection underlined?
2971 bool wxRichTextCtrl::IsSelectionUnderlined()
2975 wxRichTextRange range
= GetSelectionRange();
2977 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2978 attr
.SetFontUnderlined(true);
2980 return HasCharacterAttributes(range
, attr
);
2984 // If no selection, then we need to combine current style with default style
2985 // to see what the effect would be if we started typing.
2987 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2988 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2990 if (GetStyle(pos
, attr
))
2992 if (IsDefaultStyleShowing())
2993 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2994 return attr
.GetFontUnderlined();
3000 /// Apply bold to the selection
3001 bool wxRichTextCtrl::ApplyBoldToSelection()
3004 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3005 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
3008 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3011 wxRichTextAttr current
= GetDefaultStyleEx();
3012 current
.Apply(attr
);
3013 SetAndShowDefaultStyle(current
);
3018 /// Apply italic to the selection
3019 bool wxRichTextCtrl::ApplyItalicToSelection()
3022 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3023 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
3026 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3029 wxRichTextAttr current
= GetDefaultStyleEx();
3030 current
.Apply(attr
);
3031 SetAndShowDefaultStyle(current
);
3036 /// Apply underline to the selection
3037 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3040 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3041 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3044 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3047 wxRichTextAttr current
= GetDefaultStyleEx();
3048 current
.Apply(attr
);
3049 SetAndShowDefaultStyle(current
);
3054 /// Is all of the selection aligned according to the specified flag?
3055 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3057 wxRichTextRange range
;
3059 range
= GetSelectionRange();
3061 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3064 attr
.SetAlignment(alignment
);
3066 return HasParagraphAttributes(range
, attr
);
3069 /// Apply alignment to the selection
3070 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3073 attr
.SetAlignment(alignment
);
3075 return SetStyle(GetSelectionRange(), attr
);
3078 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3080 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3085 /// Apply a named style to the selection
3086 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3088 // Flags are defined within each definition, so only certain
3089 // attributes are applied.
3090 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3092 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3094 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3096 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3098 wxRichTextRange range
;
3101 range
= GetSelectionRange();
3104 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3105 range
= wxRichTextRange(pos
, pos
+1);
3108 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3111 // Make sure the attr has the style name
3112 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3114 attr
.SetParagraphStyleName(def
->GetName());
3116 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3117 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3118 // to change its style independently.
3119 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3122 attr
.SetCharacterStyleName(def
->GetName());
3125 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3128 wxRichTextAttr current
= GetDefaultStyleEx();
3129 current
.Apply(attr
);
3130 SetAndShowDefaultStyle(current
);
3135 /// Apply the style sheet to the buffer, for example if the styles have changed.
3136 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3139 styleSheet
= GetBuffer().GetStyleSheet();
3143 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3145 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3153 /// Sets the default style to the style under the cursor
3154 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3157 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3159 // If at the start of a paragraph, use the next position.
3160 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3162 if (GetUncombinedStyle(pos
, attr
))
3164 SetDefaultStyle(attr
);
3171 /// Returns the first visible position in the current view
3172 long wxRichTextCtrl::GetFirstVisiblePosition() const
3174 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3176 return line
->GetAbsoluteRange().GetStart();
3181 /// Get the first visible point in the window
3182 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3185 int startXUnits
, startYUnits
;
3187 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3188 GetViewStart(& startXUnits
, & startYUnits
);
3190 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3193 /// The adjusted caret position is the character position adjusted to take
3194 /// into account whether we're at the start of a paragraph, in which case
3195 /// style information should be taken from the next position, not current one.
3196 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3198 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3200 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3205 /// Get/set the selection range in character positions. -1, -1 means no selection.
3206 /// The range is in API convention, i.e. a single character selection is denoted
3208 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3210 wxRichTextRange range
= GetInternalSelectionRange();
3211 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3212 range
.SetEnd(range
.GetEnd() + 1);
3216 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3218 wxRichTextRange
range1(range
);
3219 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3220 range1
.SetEnd(range1
.GetEnd() - 1);
3222 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3224 SetInternalSelectionRange(range1
);
3228 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3230 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3233 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3235 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3238 /// Clear list for given range
3239 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3241 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3244 /// Number/renumber any list elements in the given range
3245 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3247 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3250 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3252 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3255 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3256 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3258 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3261 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3263 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3266 /// Deletes the content in the given range
3267 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3269 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3272 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3274 if (sm_availableFontNames
.GetCount() == 0)
3276 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3277 sm_availableFontNames
.Sort();
3279 return sm_availableFontNames
;
3282 void wxRichTextCtrl::ClearAvailableFontNames()
3284 sm_availableFontNames
.Clear();
3287 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3289 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3291 wxTextAttrEx basicStyle
= GetBasicStyle();
3292 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3293 SetBasicStyle(basicStyle
);
3294 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3299 // Refresh the area affected by a selection change
3300 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3302 // Calculate the refresh rectangle - just the affected lines
3303 long firstPos
, lastPos
;
3304 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3306 firstPos
= newSelection
.GetStart();
3307 lastPos
= newSelection
.GetEnd();
3309 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3311 firstPos
= oldSelection
.GetStart();
3312 lastPos
= oldSelection
.GetEnd();
3314 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3320 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3321 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3324 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3325 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3327 if (firstLine
&& lastLine
)
3329 wxSize clientSize
= GetClientSize();
3330 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3331 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3334 pt1
.y
= wxMax(0, pt1
.y
);
3336 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3338 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3339 RefreshRect(rect
, false);
3347 #if wxRICHTEXT_USE_OWN_CARET
3349 // ----------------------------------------------------------------------------
3350 // initialization and destruction
3351 // ----------------------------------------------------------------------------
3353 void wxRichTextCaret::Init()
3359 m_richTextCtrl
= NULL
;
3360 m_needsUpdate
= false;
3363 wxRichTextCaret::~wxRichTextCaret()
3367 // ----------------------------------------------------------------------------
3368 // showing/hiding/moving the caret (base class interface)
3369 // ----------------------------------------------------------------------------
3371 void wxRichTextCaret::DoShow()
3376 void wxRichTextCaret::DoHide()
3381 void wxRichTextCaret::DoMove()
3387 if (m_xOld
!= -1 && m_yOld
!= -1)
3391 wxRect
rect(GetPosition(), GetSize());
3392 m_richTextCtrl
->RefreshRect(rect
, false);
3401 void wxRichTextCaret::DoSize()
3403 int countVisible
= m_countVisible
;
3404 if (countVisible
> 0)
3410 if (countVisible
> 0)
3412 m_countVisible
= countVisible
;
3417 // ----------------------------------------------------------------------------
3418 // handling the focus
3419 // ----------------------------------------------------------------------------
3421 void wxRichTextCaret::OnSetFocus()
3429 void wxRichTextCaret::OnKillFocus()
3434 // ----------------------------------------------------------------------------
3435 // drawing the caret
3436 // ----------------------------------------------------------------------------
3438 void wxRichTextCaret::Refresh()
3442 wxRect
rect(GetPosition(), GetSize());
3443 m_richTextCtrl
->RefreshRect(rect
, false);
3447 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3449 dc
->SetPen( *wxBLACK_PEN
);
3451 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3452 dc
->SetPen(*wxBLACK_PEN
);
3454 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3455 // done under wxGTK - no idea why
3456 //dc->SetLogicalFunction(wxINVERT);
3458 wxPoint
pt(m_x
, m_y
);
3462 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3464 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3467 // wxRICHTEXT_USE_OWN_CARET