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_SIZE(wxRichTextCtrl::OnSize
)
140 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
141 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
142 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
143 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
144 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
146 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
147 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
149 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
150 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
152 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
153 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
155 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
156 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
158 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
159 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
161 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
162 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
164 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
165 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
172 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
174 wxRichTextCtrl::wxRichTextCtrl()
175 : wxScrollHelper(this)
180 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
182 const wxString
& value
,
186 const wxValidator
& validator
,
187 const wxString
& name
)
188 : wxScrollHelper(this)
191 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
195 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
196 const wxValidator
& validator
, const wxString
& name
)
198 if (!wxControl::Create(parent
, id
, pos
, size
,
199 style
|wxFULL_REPAINT_ON_RESIZE
,
205 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
208 if (style
& wxTE_READONLY
)
211 // The base attributes must all have default values
212 wxTextAttr attributes
;
213 attributes
.SetFont(GetFont());
214 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
215 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
216 attributes
.SetLineSpacing(10);
217 attributes
.SetParagraphSpacingAfter(10);
218 attributes
.SetParagraphSpacingBefore(0);
219 attributes
.SetTextEffects(0);
220 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
222 SetBasicStyle(attributes
);
224 // The default attributes will be merged with base attributes, so
225 // can be empty to begin with
226 wxTextAttr defaultAttributes
;
227 SetDefaultStyle(defaultAttributes
);
229 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
230 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
233 GetBuffer().SetRichTextCtrl(this);
235 #if wxRICHTEXT_USE_OWN_CARET
236 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
238 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
241 // Tell the sizers to use the given or best size
242 SetInitialSize(size
);
244 #if wxRICHTEXT_BUFFERED_PAINTING
246 RecreateBuffer(size
);
249 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
250 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
252 SetCursor(m_textCursor
);
254 if (!value
.IsEmpty())
257 GetBuffer().AddEventHandler(this);
260 wxAcceleratorEntry entries
[4];
262 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
263 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
264 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
265 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
267 wxAcceleratorTable
accel(4, entries
);
268 SetAcceleratorTable(accel
);
273 wxRichTextCtrl::~wxRichTextCtrl()
275 GetBuffer().RemoveEventHandler(this);
277 delete m_contextMenu
;
280 /// Member initialisation
281 void wxRichTextCtrl::Init()
283 m_contextMenu
= NULL
;
285 m_caretPosition
= -1;
286 m_selectionRange
.SetRange(-2, -2);
287 m_selectionAnchor
= -2;
289 m_caretAtLineStart
= false;
291 m_fullLayoutRequired
= false;
292 m_fullLayoutTime
= 0;
293 m_fullLayoutSavedPosition
= 0;
294 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
295 m_caretPositionForDefaultStyle
= -2;
298 void wxRichTextCtrl::DoThaw()
300 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
);
462 if (event
.ShiftDown())
464 if (m_selectionRange
.GetStart() == -2)
465 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
467 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
477 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
482 if (GetCapture() == this)
485 // See if we clicked on a URL
488 dc
.SetFont(GetFont());
491 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
492 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
494 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
496 wxRichTextEvent
cmdEvent(
497 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
499 cmdEvent
.SetEventObject(this);
500 cmdEvent
.SetPosition(m_caretPosition
+1);
502 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
505 if (GetStyle(position
, attr
))
507 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
509 wxString urlTarget
= attr
.GetURL();
510 if (!urlTarget
.IsEmpty())
512 wxMouseEvent
mouseEvent(event
);
514 long startPos
= 0, endPos
= 0;
515 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
518 startPos
= obj
->GetRange().GetStart();
519 endPos
= obj
->GetRange().GetEnd();
522 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
523 InitCommandEvent(urlEvent
);
525 urlEvent
.SetString(urlTarget
);
527 GetEventHandler()->ProcessEvent(urlEvent
);
537 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
541 dc
.SetFont(GetFont());
544 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
545 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
547 // See if we need to change the cursor
550 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
553 if (GetStyle(position
, attr
))
555 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
557 SetCursor(m_urlCursor
);
559 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
561 SetCursor(m_textCursor
);
566 SetCursor(m_textCursor
);
569 if (!event
.Dragging())
575 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
577 // TODO: test closeness
579 bool caretAtLineStart
= false;
581 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
583 // If we're at the start of a line (but not first in para)
584 // then we should keep the caret showing at the start of the line
585 // by showing the m_caretAtLineStart flag.
586 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
587 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
589 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
590 caretAtLineStart
= true;
594 if (m_caretPosition
!= position
)
596 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
598 MoveCaret(position
, caretAtLineStart
);
604 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
608 wxRichTextEvent
cmdEvent(
609 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
611 cmdEvent
.SetEventObject(this);
612 cmdEvent
.SetPosition(m_caretPosition
+1);
614 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
618 /// Left-double-click
619 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
621 wxRichTextEvent
cmdEvent(
622 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
624 cmdEvent
.SetEventObject(this);
625 cmdEvent
.SetPosition(m_caretPosition
+1);
627 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
629 SelectWord(GetCaretPosition()+1);
634 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
636 wxRichTextEvent
cmdEvent(
637 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
639 cmdEvent
.SetEventObject(this);
640 cmdEvent
.SetPosition(m_caretPosition
+1);
642 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
647 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
651 flags
|= wxRICHTEXT_CTRL_DOWN
;
652 if (event
.ShiftDown())
653 flags
|= wxRICHTEXT_SHIFT_DOWN
;
655 flags
|= wxRICHTEXT_ALT_DOWN
;
657 if (event
.GetKeyCode() == WXK_LEFT
||
658 event
.GetKeyCode() == WXK_RIGHT
||
659 event
.GetKeyCode() == WXK_UP
||
660 event
.GetKeyCode() == WXK_DOWN
||
661 event
.GetKeyCode() == WXK_HOME
||
662 event
.GetKeyCode() == WXK_PAGEUP
||
663 event
.GetKeyCode() == WXK_PAGEDOWN
||
664 event
.GetKeyCode() == WXK_END
||
666 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
667 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
668 event
.GetKeyCode() == WXK_NUMPAD_UP
||
669 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
670 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
671 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
672 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
673 event
.GetKeyCode() == WXK_NUMPAD_END
)
675 KeyboardNavigate(event
.GetKeyCode(), flags
);
679 // all the other keys modify the controls contents which shouldn't be
680 // possible if we're read-only
687 if (event
.GetKeyCode() == WXK_RETURN
)
689 SetDefaultStyleToCursorStyle();
690 BeginBatchUndo(_("Insert Text"));
692 long newPos
= m_caretPosition
;
694 DeleteSelectedContent(& newPos
);
696 if (event
.ShiftDown())
699 text
= wxRichTextLineBreakChar
;
700 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
703 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
707 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
709 wxRichTextEvent
cmdEvent(
710 wxEVT_COMMAND_RICHTEXT_RETURN
,
712 cmdEvent
.SetEventObject(this);
713 cmdEvent
.SetFlags(flags
);
714 cmdEvent
.SetPosition(newPos
+1);
716 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
718 // Generate conventional event
719 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
720 InitCommandEvent(textEvent
);
722 GetEventHandler()->ProcessEvent(textEvent
);
726 else if (event
.GetKeyCode() == WXK_BACK
)
728 BeginBatchUndo(_("Delete Text"));
730 // Submit range in character positions, which are greater than caret positions,
731 // so subtract 1 for deleted character and add 1 for conversion to character position.
732 if (m_caretPosition
> -1 && !HasSelection())
734 bool processed
= false;
737 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
738 if (pos
!= -1 && (pos
< m_caretPosition
))
740 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, m_caretPosition
), this);
746 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
749 DeleteSelectedContent();
753 if (GetLastPosition() == -1)
757 m_caretPosition
= -1;
761 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
763 wxRichTextEvent
cmdEvent(
764 wxEVT_COMMAND_RICHTEXT_DELETE
,
766 cmdEvent
.SetEventObject(this);
767 cmdEvent
.SetFlags(flags
);
768 cmdEvent
.SetPosition(m_caretPosition
+1);
769 GetEventHandler()->ProcessEvent(cmdEvent
);
773 else if (event
.GetKeyCode() == WXK_DELETE
)
775 BeginBatchUndo(_("Delete Text"));
777 // Submit range in character positions, which are greater than caret positions,
778 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
780 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
783 DeleteSelectedContent();
787 if (GetLastPosition() == -1)
791 m_caretPosition
= -1;
795 wxRichTextEvent
cmdEvent(
796 wxEVT_COMMAND_RICHTEXT_DELETE
,
798 cmdEvent
.SetEventObject(this);
799 cmdEvent
.SetFlags(flags
);
800 cmdEvent
.SetPosition(m_caretPosition
+1);
801 GetEventHandler()->ProcessEvent(cmdEvent
);
807 long keycode
= event
.GetKeyCode();
880 case WXK_NUMPAD_SPACE
:
882 case WXK_NUMPAD_ENTER
:
887 case WXK_NUMPAD_HOME
:
888 case WXK_NUMPAD_LEFT
:
890 case WXK_NUMPAD_RIGHT
:
891 case WXK_NUMPAD_DOWN
:
892 case WXK_NUMPAD_PAGEUP
:
893 case WXK_NUMPAD_PAGEDOWN
:
895 case WXK_NUMPAD_BEGIN
:
896 case WXK_NUMPAD_INSERT
:
897 case WXK_NUMPAD_DELETE
:
898 case WXK_NUMPAD_EQUAL
:
899 case WXK_NUMPAD_MULTIPLY
:
901 case WXK_NUMPAD_SEPARATOR
:
902 case WXK_NUMPAD_SUBTRACT
:
903 case WXK_NUMPAD_DECIMAL
:
904 case WXK_WINDOWS_LEFT
:
912 if (event
.CmdDown() || event
.AltDown())
918 wxRichTextEvent
cmdEvent(
919 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
921 cmdEvent
.SetEventObject(this);
922 cmdEvent
.SetFlags(flags
);
924 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
926 cmdEvent
.SetCharacter((wxChar
) keycode
);
928 cmdEvent
.SetPosition(m_caretPosition
+1);
930 if (keycode
== wxT('\t'))
932 // See if we need to promote or demote the selection or paragraph at the cursor
933 // position, instead of inserting a tab.
934 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
935 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
936 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
938 wxRichTextRange range
;
940 range
= GetSelectionRange();
942 range
= para
->GetRange().FromInternal();
944 int promoteBy
= event
.ShiftDown() ? 1 : -1;
946 PromoteList(promoteBy
, range
, NULL
);
948 GetEventHandler()->ProcessEvent(cmdEvent
);
954 SetDefaultStyleToCursorStyle();
955 BeginBatchUndo(_("Insert Text"));
957 long newPos
= m_caretPosition
;
958 DeleteSelectedContent(& newPos
);
961 wxString str
= event
.GetUnicodeKey();
963 wxString str
= (wxChar
) event
.GetKeyCode();
965 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
969 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
971 GetEventHandler()->ProcessEvent(cmdEvent
);
979 /// Delete content if there is a selection, e.g. when pressing a key.
980 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
984 long pos
= m_selectionRange
.GetStart();
985 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
986 m_selectionRange
.SetRange(-2, -2);
996 /// Keyboard navigation
1000 Left: left one character
1001 Right: right one character
1004 Ctrl-Left: left one word
1005 Ctrl-Right: right one word
1006 Ctrl-Up: previous paragraph start
1007 Ctrl-Down: next start of paragraph
1010 Ctrl-Home: start of document
1011 Ctrl-End: end of document
1012 Page-Up: Up a screen
1013 Page-Down: Down a screen
1017 Ctrl-Alt-PgUp: Start of window
1018 Ctrl-Alt-PgDn: End of window
1019 F8: Start selection mode
1020 Esc: End selection mode
1022 Adding Shift does the above but starts/extends selection.
1027 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1029 bool success
= false;
1031 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1033 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1034 success
= WordRight(1, flags
);
1036 success
= MoveRight(1, flags
);
1038 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1040 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1041 success
= WordLeft(1, flags
);
1043 success
= MoveLeft(1, flags
);
1045 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1047 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1048 success
= MoveToParagraphStart(flags
);
1050 success
= MoveUp(1, flags
);
1052 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1054 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1055 success
= MoveToParagraphEnd(flags
);
1057 success
= MoveDown(1, flags
);
1059 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1061 success
= PageUp(1, flags
);
1063 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1065 success
= PageDown(1, flags
);
1067 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1069 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1070 success
= MoveHome(flags
);
1072 success
= MoveToLineStart(flags
);
1074 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1076 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1077 success
= MoveEnd(flags
);
1079 success
= MoveToLineEnd(flags
);
1084 ScrollIntoView(m_caretPosition
, keyCode
);
1090 /// Extend the selection. Selections are in caret positions.
1091 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1093 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1095 wxRichTextRange oldSelection
= m_selectionRange
;
1097 // If not currently selecting, start selecting
1098 if (m_selectionRange
.GetStart() == -2)
1100 m_selectionAnchor
= oldPos
;
1102 if (oldPos
> newPos
)
1103 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1105 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1109 // Always ensure that the selection range start is greater than
1111 if (newPos
> m_selectionAnchor
)
1112 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1114 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1117 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1119 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1121 wxLogDebug(wxT("Strange selection range"));
1130 /// Scroll into view, returning true if we scrolled.
1131 /// This takes a _caret_ position.
1132 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1134 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1140 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1142 int startXUnits
, startYUnits
;
1143 GetViewStart(& startXUnits
, & startYUnits
);
1144 int startY
= startYUnits
* ppuY
;
1147 GetVirtualSize(& sx
, & sy
);
1153 wxRect rect
= line
->GetRect();
1155 bool scrolled
= false;
1157 wxSize clientSize
= GetClientSize();
1160 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1161 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1162 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1163 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1165 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1167 // Make it scroll so this item is at the bottom
1169 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1170 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1172 // If we're still off the screen, scroll another line down
1173 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1176 if (startYUnits
!= yUnits
)
1178 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1182 else if (rect
.y
< startY
)
1184 // Make it scroll so this item is at the top
1187 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1189 if (startYUnits
!= yUnits
)
1191 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1197 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1198 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1199 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1200 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1202 if (rect
.y
< startY
)
1204 // Make it scroll so this item is at the top
1207 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1209 if (startYUnits
!= yUnits
)
1211 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1215 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1217 // Make it scroll so this item is at the bottom
1219 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1220 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1222 // If we're still off the screen, scroll another line down
1223 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1226 if (startYUnits
!= yUnits
)
1228 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1234 #if !wxRICHTEXT_USE_OWN_CARET
1242 /// Is the given position visible on the screen?
1243 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1245 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1251 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1254 GetViewStart(& startX
, & startY
);
1256 startY
= startY
* ppuY
;
1258 wxRect rect
= line
->GetRect();
1259 wxSize clientSize
= GetClientSize();
1261 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1264 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1266 m_caretPosition
= position
;
1267 m_caretAtLineStart
= showAtLineStart
;
1270 /// Move caret one visual step forward: this may mean setting a flag
1271 /// and keeping the same position if we're going from the end of one line
1272 /// to the start of the next, which may be the exact same caret position.
1273 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1275 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1277 // Only do the check if we're not at the end of the paragraph (where things work OK
1279 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1281 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1285 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1287 // We're at the end of a line. See whether we need to
1288 // stay at the same actual caret position but change visual
1289 // position, or not.
1290 if (oldPosition
== lineRange
.GetEnd())
1292 if (m_caretAtLineStart
)
1294 // We're already at the start of the line, so actually move on now.
1295 m_caretPosition
= oldPosition
+ 1;
1296 m_caretAtLineStart
= false;
1300 // We're showing at the end of the line, so keep to
1301 // the same position but indicate that we're to show
1302 // at the start of the next line.
1303 m_caretPosition
= oldPosition
;
1304 m_caretAtLineStart
= true;
1313 /// Move caret one visual step backward: this may mean setting a flag
1314 /// and keeping the same position if we're going from the end of one line
1315 /// to the start of the next, which may be the exact same caret position.
1316 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1318 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1320 // Only do the check if we're not at the start of the paragraph (where things work OK
1322 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1324 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1328 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1330 // We're at the start of a line. See whether we need to
1331 // stay at the same actual caret position but change visual
1332 // position, or not.
1333 if (oldPosition
== lineRange
.GetStart())
1335 m_caretPosition
= oldPosition
-1;
1336 m_caretAtLineStart
= true;
1339 else if (oldPosition
== lineRange
.GetEnd())
1341 if (m_caretAtLineStart
)
1343 // We're at the start of the line, so keep the same caret position
1344 // but clear the start-of-line flag.
1345 m_caretPosition
= oldPosition
;
1346 m_caretAtLineStart
= false;
1350 // We're showing at the end of the line, so go back
1351 // to the previous character position.
1352 m_caretPosition
= oldPosition
- 1;
1362 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1364 long endPos
= GetBuffer().GetRange().GetEnd();
1366 if (m_caretPosition
+ noPositions
< endPos
)
1368 long oldPos
= m_caretPosition
;
1369 long newPos
= m_caretPosition
+ noPositions
;
1371 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1375 // Determine by looking at oldPos and m_caretPosition whether
1376 // we moved from the end of a line to the start of the next line, in which case
1377 // we want to adjust the caret position such that it is positioned at the
1378 // start of the next line, rather than jumping past the first character of the
1380 if (noPositions
== 1 && !extendSel
)
1381 MoveCaretForward(oldPos
);
1383 SetCaretPosition(newPos
);
1394 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1398 if (m_caretPosition
> startPos
- noPositions
+ 1)
1400 long oldPos
= m_caretPosition
;
1401 long newPos
= m_caretPosition
- noPositions
;
1402 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1406 if (noPositions
== 1 && !extendSel
)
1407 MoveCaretBack(oldPos
);
1409 SetCaretPosition(newPos
);
1420 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1422 return MoveDown(- noLines
, flags
);
1426 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1431 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1432 wxPoint pt
= GetCaret()->GetPosition();
1433 long newLine
= lineNumber
+ noLines
;
1435 if (lineNumber
!= -1)
1439 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1441 if (newLine
> lastLine
)
1451 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1454 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1460 wxClientDC
dc(this);
1462 dc
.SetFont(GetFont());
1464 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1466 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1468 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1469 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1470 // so we view the caret at the start of the line.
1471 bool caretLineStart
= false;
1472 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1474 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1475 wxRichTextRange lineRange
;
1477 lineRange
= thisLine
->GetAbsoluteRange();
1479 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1482 caretLineStart
= true;
1486 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1487 if (para
&& para
->GetRange().GetStart() == newPos
)
1492 long newSelEnd
= newPos
;
1494 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1498 SetCaretPosition(newPos
, caretLineStart
);
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
);
1527 /// Move to the start of the paragraph
1528 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1530 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1533 long newPos
= para
->GetRange().GetStart() - 1;
1534 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1538 SetCaretPosition(newPos
);
1547 /// Move to the end of the line
1548 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1550 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1554 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1555 long newPos
= lineRange
.GetEnd();
1556 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1560 SetCaretPosition(newPos
);
1569 /// Move to the start of the line
1570 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1572 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1575 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1576 long newPos
= lineRange
.GetStart()-1;
1578 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1582 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1584 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1593 /// Move to the start of the buffer
1594 bool wxRichTextCtrl::MoveHome(int flags
)
1596 if (m_caretPosition
!= -1)
1598 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1602 SetCaretPosition(-1);
1611 /// Move to the end of the buffer
1612 bool wxRichTextCtrl::MoveEnd(int flags
)
1614 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1616 if (m_caretPosition
!= endPos
)
1618 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1622 SetCaretPosition(endPos
);
1631 /// Move noPages pages up
1632 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1634 return PageDown(- noPages
, flags
);
1637 /// Move noPages pages down
1638 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1640 // Calculate which line occurs noPages * screen height further down.
1641 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1644 wxSize clientSize
= GetClientSize();
1645 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1647 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1650 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1651 long pos
= lineRange
.GetStart()-1;
1652 if (pos
!= m_caretPosition
)
1654 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1656 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1660 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1671 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1673 return str
== wxT(" ") || str
== wxT("\t");
1676 // Finds the caret position for the next word
1677 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1679 long endPos
= GetBuffer().GetRange().GetEnd();
1683 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1685 // First skip current text to space
1686 while (i
< endPos
&& i
> -1)
1688 // i is in character, not caret positions
1689 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1690 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1691 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1695 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1702 while (i
< endPos
&& i
> -1)
1704 // i is in character, not caret positions
1705 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1706 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1707 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1708 return wxMax(-1, i
);
1710 if (text
.empty()) // End of paragraph, or maybe an image
1711 return wxMax(-1, i
- 1);
1712 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1716 // Convert to caret position
1717 return wxMax(-1, i
- 1);
1726 long i
= m_caretPosition
;
1728 // First skip white space
1729 while (i
< endPos
&& i
> -1)
1731 // i is in character, not caret positions
1732 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1733 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1735 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1737 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1742 // Next skip current text to space
1743 while (i
< endPos
&& i
> -1)
1745 // i is in character, not caret positions
1746 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1747 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1748 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1751 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1764 /// Move n words left
1765 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1767 long pos
= FindNextWordPosition(-1);
1768 if (pos
!= m_caretPosition
)
1770 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1772 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1776 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1785 /// Move n words right
1786 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1788 long pos
= FindNextWordPosition(1);
1789 if (pos
!= m_caretPosition
)
1791 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1793 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1797 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1807 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1809 // Only do sizing optimization for large buffers
1810 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1812 m_fullLayoutRequired
= true;
1813 m_fullLayoutTime
= wxGetLocalTimeMillis();
1814 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1815 LayoutContent(true /* onlyVisibleRect */);
1818 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1820 #if wxRICHTEXT_BUFFERED_PAINTING
1828 /// Idle-time processing
1829 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1831 #if wxRICHTEXT_USE_OWN_CARET
1832 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1834 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1840 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1842 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1844 m_fullLayoutRequired
= false;
1845 m_fullLayoutTime
= 0;
1846 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1847 ShowPosition(m_fullLayoutSavedPosition
);
1851 if (m_caretPositionForDefaultStyle
!= -2)
1853 // If the caret position has changed, no longer reflect the default style
1855 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1856 m_caretPositionForDefaultStyle
= -2;
1863 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1865 #if wxRICHTEXT_USE_OWN_CARET
1866 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1869 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1876 /// Set up scrollbars, e.g. after a resize
1877 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1882 if (GetBuffer().IsEmpty())
1884 SetScrollbars(0, 0, 0, 0, 0, 0);
1888 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1889 // of pixels. See e.g. wxVScrolledWindow for ideas.
1890 int pixelsPerUnit
= 5;
1891 wxSize clientSize
= GetClientSize();
1893 int maxHeight
= GetBuffer().GetCachedSize().y
;
1895 // Round up so we have at least maxHeight pixels
1896 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1898 int startX
= 0, startY
= 0;
1900 GetViewStart(& startX
, & startY
);
1902 int maxPositionX
= 0;
1903 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1905 int newStartX
= wxMin(maxPositionX
, startX
);
1906 int newStartY
= wxMin(maxPositionY
, startY
);
1908 int oldPPUX
, oldPPUY
;
1909 int oldStartX
, oldStartY
;
1910 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
1911 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
1912 GetViewStart(& oldStartX
, & oldStartY
);
1913 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
1915 oldVirtualSizeY
/= oldPPUY
;
1917 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
1920 // Don't set scrollbars if there were none before, and there will be none now.
1921 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
1924 // Move to previous scroll position if
1926 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
1929 /// Paint the background
1930 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1932 wxColour backgroundColour
= GetBackgroundColour();
1933 if (!backgroundColour
.Ok())
1934 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1936 // Clear the background
1937 dc
.SetBrush(wxBrush(backgroundColour
));
1938 dc
.SetPen(*wxTRANSPARENT_PEN
);
1939 wxRect
windowRect(GetClientSize());
1940 windowRect
.x
-= 2; windowRect
.y
-= 2;
1941 windowRect
.width
+= 4; windowRect
.height
+= 4;
1943 // We need to shift the rectangle to take into account
1944 // scrolling. Converting device to logical coordinates.
1945 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1946 dc
.DrawRectangle(windowRect
);
1949 #if wxRICHTEXT_BUFFERED_PAINTING
1950 /// Recreate buffer bitmap if necessary
1951 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1954 if (sz
== wxDefaultSize
)
1955 sz
= GetClientSize();
1957 if (sz
.x
< 1 || sz
.y
< 1)
1960 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1961 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1962 return m_bufferBitmap
.Ok();
1966 // ----------------------------------------------------------------------------
1967 // file IO functions
1968 // ----------------------------------------------------------------------------
1970 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1972 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1974 m_filename
= filename
;
1977 SetInsertionPoint(0);
1980 SetupScrollbars(true);
1982 wxTextCtrl::SendTextUpdatedEvent(this);
1988 wxLogError(_("File couldn't be loaded."));
1994 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1996 if (GetBuffer().SaveFile(filename
, fileType
))
1998 m_filename
= filename
;
2005 wxLogError(_("The text couldn't be saved."));
2010 // ----------------------------------------------------------------------------
2011 // wxRichTextCtrl specific functionality
2012 // ----------------------------------------------------------------------------
2014 /// Add a new paragraph of text to the end of the buffer
2015 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2017 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2023 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2025 wxRichTextRange range
= GetBuffer().AddImage(image
);
2030 // ----------------------------------------------------------------------------
2031 // selection and ranges
2032 // ----------------------------------------------------------------------------
2034 void wxRichTextCtrl::SelectAll()
2036 SetSelection(0, GetLastPosition()+1);
2037 m_selectionAnchor
= -1;
2041 void wxRichTextCtrl::SelectNone()
2043 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2045 wxRichTextRange oldSelection
= m_selectionRange
;
2047 m_selectionRange
= wxRichTextRange(-2, -2);
2049 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2051 m_selectionAnchor
= -2;
2054 static bool wxIsWordDelimiter(const wxString
& text
)
2056 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2059 /// Select the word at the given character position
2060 bool wxRichTextCtrl::SelectWord(long position
)
2062 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2065 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2069 if (position
== para
->GetRange().GetEnd())
2072 long positionStart
= position
;
2073 long positionEnd
= position
;
2075 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2077 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2078 if (wxIsWordDelimiter(text
))
2084 if (positionStart
< para
->GetRange().GetStart())
2085 positionStart
= para
->GetRange().GetStart();
2087 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2089 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2090 if (wxIsWordDelimiter(text
))
2096 if (positionEnd
>= para
->GetRange().GetEnd())
2097 positionEnd
= para
->GetRange().GetEnd();
2099 if (positionEnd
< positionStart
)
2102 SetSelection(positionStart
, positionEnd
+1);
2104 if (positionStart
>= 0)
2106 MoveCaret(positionStart
-1, true);
2112 wxString
wxRichTextCtrl::GetStringSelection() const
2115 GetSelection(&from
, &to
);
2117 return GetRange(from
, to
);
2120 // ----------------------------------------------------------------------------
2122 // ----------------------------------------------------------------------------
2124 wxTextCtrlHitTestResult
2125 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2127 // implement in terms of the other overload as the native ports typically
2128 // can get the position and not (x, y) pair directly (although wxUniv
2129 // directly gets x and y -- and so overrides this method as well)
2131 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2133 if ( rc
!= wxTE_HT_UNKNOWN
)
2135 PositionToXY(pos
, x
, y
);
2141 wxTextCtrlHitTestResult
2142 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2145 wxClientDC
dc((wxRichTextCtrl
*) this);
2146 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2148 // Buffer uses logical position (relative to start of buffer)
2150 wxPoint pt2
= GetLogicalPoint(pt
);
2152 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2154 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2155 return wxTE_HT_BEFORE
;
2156 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2157 return wxTE_HT_BEYOND
;
2158 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2159 return wxTE_HT_ON_TEXT
;
2161 return wxTE_HT_UNKNOWN
;
2164 // ----------------------------------------------------------------------------
2165 // set/get the controls text
2166 // ----------------------------------------------------------------------------
2168 wxString
wxRichTextCtrl::GetValue() const
2170 return GetBuffer().GetText();
2173 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2175 // Public API for range is different from internals
2176 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2179 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2181 // Don't call Clear here, since it always sends a text updated event
2182 m_buffer
.ResetAndClearCommands();
2183 m_buffer
.SetDirty(true);
2184 m_caretPosition
= -1;
2185 m_caretPositionForDefaultStyle
= -2;
2186 m_caretAtLineStart
= false;
2187 m_selectionRange
.SetRange(-2, -2);
2197 if (!value
.IsEmpty())
2199 // Remove empty paragraph
2200 GetBuffer().Clear();
2201 DoWriteText(value
, flags
);
2203 // for compatibility, don't move the cursor when doing SetValue()
2204 SetInsertionPoint(0);
2208 // still send an event for consistency
2209 if (flags
& SetValue_SendEvent
)
2210 wxTextCtrl::SendTextUpdatedEvent(this);
2215 void wxRichTextCtrl::WriteText(const wxString
& value
)
2220 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2222 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2224 SetDefaultStyleToCursorStyle();
2225 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2227 if ( flags
& SetValue_SendEvent
)
2228 wxTextCtrl::SendTextUpdatedEvent(this);
2231 void wxRichTextCtrl::AppendText(const wxString
& text
)
2233 SetInsertionPointEnd();
2238 /// Write an image at the current insertion point
2239 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2241 wxRichTextImageBlock imageBlock
;
2243 wxImage image2
= image
;
2244 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2245 return WriteImage(imageBlock
);
2250 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2252 wxRichTextImageBlock imageBlock
;
2255 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2256 return WriteImage(imageBlock
);
2261 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2263 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2266 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2270 wxRichTextImageBlock imageBlock
;
2272 wxImage image
= bitmap
.ConvertToImage();
2273 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2274 return WriteImage(imageBlock
);
2280 /// Insert a newline (actually paragraph) at the current insertion point.
2281 bool wxRichTextCtrl::Newline()
2283 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2286 /// Insert a line break at the current insertion point.
2287 bool wxRichTextCtrl::LineBreak()
2290 text
= wxRichTextLineBreakChar
;
2291 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2294 // ----------------------------------------------------------------------------
2295 // Clipboard operations
2296 // ----------------------------------------------------------------------------
2298 void wxRichTextCtrl::Copy()
2302 wxRichTextRange range
= GetInternalSelectionRange();
2303 GetBuffer().CopyToClipboard(range
);
2307 void wxRichTextCtrl::Cut()
2311 wxRichTextRange range
= GetInternalSelectionRange();
2312 GetBuffer().CopyToClipboard(range
);
2314 DeleteSelectedContent();
2320 void wxRichTextCtrl::Paste()
2324 SetDefaultStyleToCursorStyle();
2325 BeginBatchUndo(_("Paste"));
2327 long newPos
= m_caretPosition
;
2328 DeleteSelectedContent(& newPos
);
2330 GetBuffer().PasteFromClipboard(newPos
);
2336 void wxRichTextCtrl::DeleteSelection()
2338 if (CanDeleteSelection())
2340 DeleteSelectedContent();
2344 bool wxRichTextCtrl::HasSelection() const
2346 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2349 bool wxRichTextCtrl::CanCopy() const
2351 // Can copy if there's a selection
2352 return HasSelection();
2355 bool wxRichTextCtrl::CanCut() const
2357 return HasSelection() && IsEditable();
2360 bool wxRichTextCtrl::CanPaste() const
2362 if ( !IsEditable() )
2365 return GetBuffer().CanPasteFromClipboard();
2368 bool wxRichTextCtrl::CanDeleteSelection() const
2370 return HasSelection() && IsEditable();
2374 // ----------------------------------------------------------------------------
2376 // ----------------------------------------------------------------------------
2378 void wxRichTextCtrl::SetEditable(bool editable
)
2380 m_editable
= editable
;
2383 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2387 m_caretPosition
= pos
- 1;
2392 void wxRichTextCtrl::SetInsertionPointEnd()
2394 long pos
= GetLastPosition();
2395 SetInsertionPoint(pos
);
2398 long wxRichTextCtrl::GetInsertionPoint() const
2400 return m_caretPosition
+1;
2403 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2405 return GetBuffer().GetRange().GetEnd();
2408 // If the return values from and to are the same, there is no
2410 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2412 *from
= m_selectionRange
.GetStart();
2413 *to
= m_selectionRange
.GetEnd();
2414 if ((*to
) != -1 && (*to
) != -2)
2418 bool wxRichTextCtrl::IsEditable() const
2423 // ----------------------------------------------------------------------------
2425 // ----------------------------------------------------------------------------
2427 void wxRichTextCtrl::SetSelection(long from
, long to
)
2429 // if from and to are both -1, it means (in wxWidgets) that all text should
2431 if ( (from
== -1) && (to
== -1) )
2434 to
= GetLastPosition()+1;
2437 DoSetSelection(from
, to
);
2440 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2448 wxRichTextRange oldSelection
= m_selectionRange
;
2449 m_selectionAnchor
= from
;
2450 m_selectionRange
.SetRange(from
, to
-1);
2452 m_caretPosition
= from
-1;
2454 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2459 // ----------------------------------------------------------------------------
2461 // ----------------------------------------------------------------------------
2463 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2464 const wxString
& value
)
2466 BeginBatchUndo(_("Replace"));
2468 DeleteSelectedContent();
2470 DoWriteText(value
, SetValue_SelectionOnly
);
2475 void wxRichTextCtrl::Remove(long from
, long to
)
2479 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2486 bool wxRichTextCtrl::IsModified() const
2488 return m_buffer
.IsModified();
2491 void wxRichTextCtrl::MarkDirty()
2493 m_buffer
.Modify(true);
2496 void wxRichTextCtrl::DiscardEdits()
2498 m_caretPositionForDefaultStyle
= -2;
2499 m_buffer
.Modify(false);
2500 m_buffer
.GetCommandProcessor()->ClearCommands();
2503 int wxRichTextCtrl::GetNumberOfLines() const
2505 return GetBuffer().GetParagraphCount();
2508 // ----------------------------------------------------------------------------
2509 // Positions <-> coords
2510 // ----------------------------------------------------------------------------
2512 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2514 return GetBuffer().XYToPosition(x
, y
);
2517 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2519 return GetBuffer().PositionToXY(pos
, x
, y
);
2522 // ----------------------------------------------------------------------------
2524 // ----------------------------------------------------------------------------
2526 void wxRichTextCtrl::ShowPosition(long pos
)
2528 if (!IsPositionVisible(pos
))
2529 ScrollIntoView(pos
-1, WXK_DOWN
);
2532 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2534 return GetBuffer().GetParagraphLength(lineNo
);
2537 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2539 return GetBuffer().GetParagraphText(lineNo
);
2542 // ----------------------------------------------------------------------------
2544 // ----------------------------------------------------------------------------
2546 void wxRichTextCtrl::Undo()
2550 GetCommandProcessor()->Undo();
2554 void wxRichTextCtrl::Redo()
2558 GetCommandProcessor()->Redo();
2562 bool wxRichTextCtrl::CanUndo() const
2564 return GetCommandProcessor()->CanUndo();
2567 bool wxRichTextCtrl::CanRedo() const
2569 return GetCommandProcessor()->CanRedo();
2572 // ----------------------------------------------------------------------------
2573 // implementation details
2574 // ----------------------------------------------------------------------------
2576 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2578 SetValue(event
.GetString());
2579 GetEventHandler()->ProcessEvent(event
);
2582 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2584 // By default, load the first file into the text window.
2585 if (event
.GetNumberOfFiles() > 0)
2587 LoadFile(event
.GetFiles()[0]);
2591 wxSize
wxRichTextCtrl::DoGetBestSize() const
2593 return wxSize(10, 10);
2596 // ----------------------------------------------------------------------------
2597 // standard handlers for standard edit menu events
2598 // ----------------------------------------------------------------------------
2600 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2605 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2610 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2615 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2620 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2625 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2630 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2632 event
.Enable( CanCut() );
2635 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2637 event
.Enable( CanCopy() );
2640 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2642 event
.Enable( CanDeleteSelection() );
2645 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2647 event
.Enable( CanPaste() );
2650 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2652 event
.Enable( CanUndo() );
2653 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2656 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2658 event
.Enable( CanRedo() );
2659 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2662 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2667 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2669 event
.Enable(GetLastPosition() > 0);
2672 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2674 if (event
.GetEventObject() != this)
2682 m_contextMenu
= new wxMenu
;
2683 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2684 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2685 m_contextMenu
->AppendSeparator();
2686 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2687 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2688 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2689 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2690 m_contextMenu
->AppendSeparator();
2691 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2693 PopupMenu(m_contextMenu
);
2697 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2699 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2702 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2704 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2707 // extended style setting operation with flags including:
2708 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2709 // see richtextbuffer.h for more details.
2711 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2713 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2716 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2718 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2721 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2723 return GetBuffer().GetDefaultStyle();
2726 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2728 return GetBuffer().GetStyle(position
, style
);
2731 // get the common set of styles for the range
2732 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2734 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2737 /// Get the content (uncombined) attributes for this position.
2738 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2740 return GetBuffer().GetUncombinedStyle(position
, style
);
2743 /// Set font, and also the buffer attributes
2744 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2746 wxControl::SetFont(font
);
2748 wxTextAttr attr
= GetBuffer().GetAttributes();
2750 GetBuffer().SetBasicStyle(attr
);
2752 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2758 /// Transform logical to physical
2759 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2762 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2767 /// Transform physical to logical
2768 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2771 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2776 /// Position the caret
2777 void wxRichTextCtrl::PositionCaret()
2782 //wxLogDebug(wxT("PositionCaret"));
2785 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2787 wxPoint newPt
= caretRect
.GetPosition();
2788 wxSize newSz
= caretRect
.GetSize();
2789 wxPoint pt
= GetPhysicalPoint(newPt
);
2790 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2793 if (GetCaret()->GetSize() != newSz
)
2794 GetCaret()->SetSize(newSz
);
2795 GetCaret()->Move(pt
);
2801 /// Get the caret height and position for the given character position
2802 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2804 wxClientDC
dc(this);
2805 dc
.SetFont(GetFont());
2812 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2814 // Caret height can't be zero
2816 height
= dc
.GetCharHeight();
2818 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2825 /// Gets the line for the visible caret position. If the caret is
2826 /// shown at the very end of the line, it means the next character is actually
2827 /// on the following line. So let's get the line we're expecting to find
2828 /// if this is the case.
2829 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2831 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2832 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2835 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2836 if (caretPosition
== lineRange
.GetStart()-1 &&
2837 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2839 if (!m_caretAtLineStart
)
2840 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2847 /// Move the caret to the given character position
2848 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2850 if (GetBuffer().GetDirty())
2853 if (pos
<= GetBuffer().GetRange().GetEnd())
2855 SetCaretPosition(pos
, showAtLineStart
);
2865 /// Layout the buffer: which we must do before certain operations, such as
2866 /// setting the caret position.
2867 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2869 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2871 wxRect
availableSpace(GetClientSize());
2872 if (availableSpace
.width
== 0)
2873 availableSpace
.width
= 10;
2874 if (availableSpace
.height
== 0)
2875 availableSpace
.height
= 10;
2877 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2878 if (onlyVisibleRect
)
2880 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2881 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2884 wxClientDC
dc(this);
2885 dc
.SetFont(GetFont());
2889 GetBuffer().Defragment();
2890 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2891 GetBuffer().Layout(dc
, availableSpace
, flags
);
2892 GetBuffer().SetDirty(false);
2901 /// Is all of the selection bold?
2902 bool wxRichTextCtrl::IsSelectionBold()
2907 wxRichTextRange range
= GetSelectionRange();
2908 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2909 attr
.SetFontWeight(wxBOLD
);
2911 return HasCharacterAttributes(range
, attr
);
2915 // If no selection, then we need to combine current style with default style
2916 // to see what the effect would be if we started typing.
2918 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2920 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2921 if (GetStyle(pos
, attr
))
2923 if (IsDefaultStyleShowing())
2924 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2925 return attr
.GetFontWeight() == wxBOLD
;
2931 /// Is all of the selection italics?
2932 bool wxRichTextCtrl::IsSelectionItalics()
2936 wxRichTextRange range
= GetSelectionRange();
2938 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2939 attr
.SetFontStyle(wxITALIC
);
2941 return HasCharacterAttributes(range
, attr
);
2945 // If no selection, then we need to combine current style with default style
2946 // to see what the effect would be if we started typing.
2948 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2950 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2951 if (GetStyle(pos
, attr
))
2953 if (IsDefaultStyleShowing())
2954 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2955 return attr
.GetFontStyle() == wxITALIC
;
2961 /// Is all of the selection underlined?
2962 bool wxRichTextCtrl::IsSelectionUnderlined()
2966 wxRichTextRange range
= GetSelectionRange();
2968 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2969 attr
.SetFontUnderlined(true);
2971 return HasCharacterAttributes(range
, attr
);
2975 // If no selection, then we need to combine current style with default style
2976 // to see what the effect would be if we started typing.
2978 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2979 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2981 if (GetStyle(pos
, attr
))
2983 if (IsDefaultStyleShowing())
2984 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2985 return attr
.GetFontUnderlined();
2991 /// Apply bold to the selection
2992 bool wxRichTextCtrl::ApplyBoldToSelection()
2995 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2996 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2999 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3002 wxRichTextAttr current
= GetDefaultStyleEx();
3003 current
.Apply(attr
);
3004 SetAndShowDefaultStyle(current
);
3009 /// Apply italic to the selection
3010 bool wxRichTextCtrl::ApplyItalicToSelection()
3013 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3014 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
3017 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3020 wxRichTextAttr current
= GetDefaultStyleEx();
3021 current
.Apply(attr
);
3022 SetAndShowDefaultStyle(current
);
3027 /// Apply underline to the selection
3028 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3031 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3032 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3035 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3038 wxRichTextAttr current
= GetDefaultStyleEx();
3039 current
.Apply(attr
);
3040 SetAndShowDefaultStyle(current
);
3045 /// Is all of the selection aligned according to the specified flag?
3046 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3048 wxRichTextRange range
;
3050 range
= GetSelectionRange();
3052 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3055 attr
.SetAlignment(alignment
);
3057 return HasParagraphAttributes(range
, attr
);
3060 /// Apply alignment to the selection
3061 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3064 attr
.SetAlignment(alignment
);
3066 return SetStyle(GetSelectionRange(), attr
);
3069 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3071 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3076 /// Apply a named style to the selection
3077 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3079 // Flags are defined within each definition, so only certain
3080 // attributes are applied.
3081 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3083 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3085 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3087 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3089 wxRichTextRange range
;
3092 range
= GetSelectionRange();
3095 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3096 range
= wxRichTextRange(pos
, pos
+1);
3099 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3102 // Make sure the attr has the style name
3103 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3105 attr
.SetParagraphStyleName(def
->GetName());
3107 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3108 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3109 // to change its style independently.
3110 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3113 attr
.SetCharacterStyleName(def
->GetName());
3116 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3119 wxRichTextAttr current
= GetDefaultStyleEx();
3120 current
.Apply(attr
);
3121 SetAndShowDefaultStyle(current
);
3126 /// Apply the style sheet to the buffer, for example if the styles have changed.
3127 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3130 styleSheet
= GetBuffer().GetStyleSheet();
3134 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3136 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3144 /// Sets the default style to the style under the cursor
3145 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3148 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3150 // If at the start of a paragraph, use the next position.
3151 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3153 if (GetUncombinedStyle(pos
, attr
))
3155 SetDefaultStyle(attr
);
3162 /// Returns the first visible position in the current view
3163 long wxRichTextCtrl::GetFirstVisiblePosition() const
3165 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3167 return line
->GetAbsoluteRange().GetStart();
3172 /// Get the first visible point in the window
3173 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3176 int startXUnits
, startYUnits
;
3178 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3179 GetViewStart(& startXUnits
, & startYUnits
);
3181 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3184 /// The adjusted caret position is the character position adjusted to take
3185 /// into account whether we're at the start of a paragraph, in which case
3186 /// style information should be taken from the next position, not current one.
3187 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3189 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3191 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3196 /// Get/set the selection range in character positions. -1, -1 means no selection.
3197 /// The range is in API convention, i.e. a single character selection is denoted
3199 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3201 wxRichTextRange range
= GetInternalSelectionRange();
3202 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3203 range
.SetEnd(range
.GetEnd() + 1);
3207 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3209 wxRichTextRange
range1(range
);
3210 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3211 range1
.SetEnd(range1
.GetEnd() - 1);
3213 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3215 SetInternalSelectionRange(range1
);
3219 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3221 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3224 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3226 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3229 /// Clear list for given range
3230 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3232 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3235 /// Number/renumber any list elements in the given range
3236 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3238 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3241 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3243 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3246 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3247 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3249 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3252 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3254 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3257 /// Deletes the content in the given range
3258 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3260 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3263 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3265 if (sm_availableFontNames
.GetCount() == 0)
3267 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3268 sm_availableFontNames
.Sort();
3270 return sm_availableFontNames
;
3273 void wxRichTextCtrl::ClearAvailableFontNames()
3275 sm_availableFontNames
.Clear();
3278 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3280 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3282 wxTextAttrEx basicStyle
= GetBasicStyle();
3283 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3284 SetBasicStyle(basicStyle
);
3285 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3290 // Refresh the area affected by a selection change
3291 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3293 // Calculate the refresh rectangle - just the affected lines
3294 long firstPos
, lastPos
;
3295 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3297 firstPos
= newSelection
.GetStart();
3298 lastPos
= newSelection
.GetEnd();
3300 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3302 firstPos
= oldSelection
.GetStart();
3303 lastPos
= oldSelection
.GetEnd();
3305 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3311 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3312 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3315 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3316 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3318 if (firstLine
&& lastLine
)
3320 wxSize clientSize
= GetClientSize();
3321 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3322 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3325 pt1
.y
= wxMax(0, pt1
.y
);
3327 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3329 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3330 RefreshRect(rect
, false);
3338 #if wxRICHTEXT_USE_OWN_CARET
3340 // ----------------------------------------------------------------------------
3341 // initialization and destruction
3342 // ----------------------------------------------------------------------------
3344 void wxRichTextCaret::Init()
3350 m_richTextCtrl
= NULL
;
3351 m_needsUpdate
= false;
3354 wxRichTextCaret::~wxRichTextCaret()
3358 // ----------------------------------------------------------------------------
3359 // showing/hiding/moving the caret (base class interface)
3360 // ----------------------------------------------------------------------------
3362 void wxRichTextCaret::DoShow()
3367 void wxRichTextCaret::DoHide()
3372 void wxRichTextCaret::DoMove()
3378 if (m_xOld
!= -1 && m_yOld
!= -1)
3382 wxRect
rect(GetPosition(), GetSize());
3383 m_richTextCtrl
->RefreshRect(rect
, false);
3392 void wxRichTextCaret::DoSize()
3394 int countVisible
= m_countVisible
;
3395 if (countVisible
> 0)
3401 if (countVisible
> 0)
3403 m_countVisible
= countVisible
;
3408 // ----------------------------------------------------------------------------
3409 // handling the focus
3410 // ----------------------------------------------------------------------------
3412 void wxRichTextCaret::OnSetFocus()
3420 void wxRichTextCaret::OnKillFocus()
3425 // ----------------------------------------------------------------------------
3426 // drawing the caret
3427 // ----------------------------------------------------------------------------
3429 void wxRichTextCaret::Refresh()
3433 wxRect
rect(GetPosition(), GetSize());
3434 m_richTextCtrl
->RefreshRect(rect
, false);
3438 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3440 dc
->SetPen( *wxBLACK_PEN
);
3442 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3443 dc
->SetPen(*wxBLACK_PEN
);
3445 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3446 // done under wxGTK - no idea why
3447 //dc->SetLogicalFunction(wxINVERT);
3449 wxPoint
pt(m_x
, m_y
);
3453 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3455 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3458 // wxRICHTEXT_USE_OWN_CARET