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);
220 SetBasicStyle(attributes
);
222 // The default attributes will be merged with base attributes, so
223 // can be empty to begin with
224 wxTextAttr defaultAttributes
;
225 SetDefaultStyle(defaultAttributes
);
227 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
228 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
231 GetBuffer().SetRichTextCtrl(this);
233 #if wxRICHTEXT_USE_OWN_CARET
234 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
236 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
239 // Tell the sizers to use the given or best size
240 SetInitialSize(size
);
242 #if wxRICHTEXT_BUFFERED_PAINTING
244 RecreateBuffer(size
);
247 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
248 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
250 SetCursor(m_textCursor
);
252 if (!value
.IsEmpty())
255 GetBuffer().AddEventHandler(this);
258 wxAcceleratorEntry entries
[4];
260 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
261 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
262 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
263 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
265 wxAcceleratorTable
accel(4, entries
);
266 SetAcceleratorTable(accel
);
271 wxRichTextCtrl::~wxRichTextCtrl()
273 GetBuffer().RemoveEventHandler(this);
275 delete m_contextMenu
;
278 /// Member initialisation
279 void wxRichTextCtrl::Init()
281 m_contextMenu
= NULL
;
283 m_caretPosition
= -1;
284 m_selectionRange
.SetRange(-2, -2);
285 m_selectionAnchor
= -2;
287 m_caretAtLineStart
= false;
289 m_fullLayoutRequired
= false;
290 m_fullLayoutTime
= 0;
291 m_fullLayoutSavedPosition
= 0;
292 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
293 m_caretPositionForDefaultStyle
= -2;
296 void wxRichTextCtrl::DoThaw()
298 if (GetBuffer().GetDirty())
306 void wxRichTextCtrl::Clear()
308 m_buffer
.ResetAndClearCommands();
309 m_buffer
.SetDirty(true);
310 m_caretPosition
= -1;
311 m_caretPositionForDefaultStyle
= -2;
312 m_caretAtLineStart
= false;
313 m_selectionRange
.SetRange(-2, -2);
323 wxTextCtrl::SendTextUpdatedEvent(this);
327 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
329 #if !wxRICHTEXT_USE_OWN_CARET
330 if (GetCaret() && !IsFrozen())
335 #if wxRICHTEXT_BUFFERED_PAINTING
336 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
346 dc
.SetFont(GetFont());
348 // Paint the background
351 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
353 wxRect
drawingArea(GetUpdateRegion().GetBox());
354 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
356 wxRect
availableSpace(GetClientSize());
357 if (GetBuffer().GetDirty())
359 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
360 GetBuffer().SetDirty(false);
364 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
365 #if wxRICHTEXT_USE_OWN_CARET
366 if (GetCaret()->IsVisible())
368 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
373 #if !wxRICHTEXT_USE_OWN_CARET
380 // Empty implementation, to prevent flicker
381 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
385 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
389 #if !wxRICHTEXT_USE_OWN_CARET
395 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
396 // Work around dropouts when control is focused
404 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
409 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
410 // Work around dropouts when control is focused
418 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
424 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
430 dc
.SetFont(GetFont());
433 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
435 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
437 m_dragStart
= event
.GetLogicalPosition(dc
);
441 bool caretAtLineStart
= false;
443 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
445 // If we're at the start of a line (but not first in para)
446 // then we should keep the caret showing at the start of the line
447 // by showing the m_caretAtLineStart flag.
448 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
449 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
451 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
452 caretAtLineStart
= true;
456 long oldCaretPos
= m_caretPosition
;
458 MoveCaret(position
, caretAtLineStart
);
459 SetDefaultStyleToCursorStyle();
461 if (event
.ShiftDown())
463 if (m_selectionRange
.GetStart() == -2)
464 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
466 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
476 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
481 if (GetCapture() == this)
484 // See if we clicked on a URL
487 dc
.SetFont(GetFont());
490 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
491 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
493 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
495 wxRichTextEvent
cmdEvent(
496 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
498 cmdEvent
.SetEventObject(this);
499 cmdEvent
.SetPosition(m_caretPosition
+1);
501 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
504 if (GetStyle(position
, attr
))
506 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
508 wxString urlTarget
= attr
.GetURL();
509 if (!urlTarget
.IsEmpty())
511 wxMouseEvent
mouseEvent(event
);
513 long startPos
= 0, endPos
= 0;
514 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
517 startPos
= obj
->GetRange().GetStart();
518 endPos
= obj
->GetRange().GetEnd();
521 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
522 InitCommandEvent(urlEvent
);
524 urlEvent
.SetString(urlTarget
);
526 GetEventHandler()->ProcessEvent(urlEvent
);
536 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
540 dc
.SetFont(GetFont());
543 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
544 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
546 // See if we need to change the cursor
549 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
552 if (GetStyle(position
, attr
))
554 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
556 SetCursor(m_urlCursor
);
558 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
560 SetCursor(m_textCursor
);
565 SetCursor(m_textCursor
);
568 if (!event
.Dragging())
574 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
576 // TODO: test closeness
578 bool caretAtLineStart
= false;
580 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
582 // If we're at the start of a line (but not first in para)
583 // then we should keep the caret showing at the start of the line
584 // by showing the m_caretAtLineStart flag.
585 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
586 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
588 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
589 caretAtLineStart
= true;
593 if (m_caretPosition
!= position
)
595 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
597 MoveCaret(position
, caretAtLineStart
);
598 SetDefaultStyleToCursorStyle();
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 BeginBatchUndo(_("Insert Text"));
691 long newPos
= m_caretPosition
;
693 DeleteSelectedContent(& newPos
);
695 if (event
.ShiftDown())
698 text
= wxRichTextLineBreakChar
;
699 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
702 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
705 SetDefaultStyleToCursorStyle();
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;
759 SetDefaultStyleToCursorStyle();
762 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
764 wxRichTextEvent
cmdEvent(
765 wxEVT_COMMAND_RICHTEXT_DELETE
,
767 cmdEvent
.SetEventObject(this);
768 cmdEvent
.SetFlags(flags
);
769 cmdEvent
.SetPosition(m_caretPosition
+1);
770 GetEventHandler()->ProcessEvent(cmdEvent
);
774 else if (event
.GetKeyCode() == WXK_DELETE
)
776 BeginBatchUndo(_("Delete Text"));
778 // Submit range in character positions, which are greater than caret positions,
779 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
781 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
784 DeleteSelectedContent();
788 if (GetLastPosition() == -1)
792 m_caretPosition
= -1;
794 SetDefaultStyleToCursorStyle();
797 wxRichTextEvent
cmdEvent(
798 wxEVT_COMMAND_RICHTEXT_DELETE
,
800 cmdEvent
.SetEventObject(this);
801 cmdEvent
.SetFlags(flags
);
802 cmdEvent
.SetPosition(m_caretPosition
+1);
803 GetEventHandler()->ProcessEvent(cmdEvent
);
809 long keycode
= event
.GetKeyCode();
882 case WXK_NUMPAD_SPACE
:
884 case WXK_NUMPAD_ENTER
:
889 case WXK_NUMPAD_HOME
:
890 case WXK_NUMPAD_LEFT
:
892 case WXK_NUMPAD_RIGHT
:
893 case WXK_NUMPAD_DOWN
:
894 case WXK_NUMPAD_PAGEUP
:
895 case WXK_NUMPAD_PAGEDOWN
:
897 case WXK_NUMPAD_BEGIN
:
898 case WXK_NUMPAD_INSERT
:
899 case WXK_NUMPAD_DELETE
:
900 case WXK_NUMPAD_EQUAL
:
901 case WXK_NUMPAD_MULTIPLY
:
903 case WXK_NUMPAD_SEPARATOR
:
904 case WXK_NUMPAD_SUBTRACT
:
905 case WXK_NUMPAD_DECIMAL
:
906 case WXK_WINDOWS_LEFT
:
914 if (event
.CmdDown() || event
.AltDown())
920 wxRichTextEvent
cmdEvent(
921 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
923 cmdEvent
.SetEventObject(this);
924 cmdEvent
.SetFlags(flags
);
926 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
928 cmdEvent
.SetCharacter((wxChar
) keycode
);
930 cmdEvent
.SetPosition(m_caretPosition
+1);
932 if (keycode
== wxT('\t'))
934 // See if we need to promote or demote the selection or paragraph at the cursor
935 // position, instead of inserting a tab.
936 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
937 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
938 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
940 wxRichTextRange range
;
942 range
= GetSelectionRange();
944 range
= para
->GetRange().FromInternal();
946 int promoteBy
= event
.ShiftDown() ? 1 : -1;
948 PromoteList(promoteBy
, range
, NULL
);
950 GetEventHandler()->ProcessEvent(cmdEvent
);
956 BeginBatchUndo(_("Insert Text"));
958 long newPos
= m_caretPosition
;
959 DeleteSelectedContent(& newPos
);
962 wxString str
= event
.GetUnicodeKey();
964 wxString str
= (wxChar
) event
.GetKeyCode();
966 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
970 SetDefaultStyleToCursorStyle();
971 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
973 GetEventHandler()->ProcessEvent(cmdEvent
);
981 /// Delete content if there is a selection, e.g. when pressing a key.
982 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
986 long pos
= m_selectionRange
.GetStart();
987 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
988 m_selectionRange
.SetRange(-2, -2);
998 /// Keyboard navigation
1002 Left: left one character
1003 Right: right one character
1006 Ctrl-Left: left one word
1007 Ctrl-Right: right one word
1008 Ctrl-Up: previous paragraph start
1009 Ctrl-Down: next start of paragraph
1012 Ctrl-Home: start of document
1013 Ctrl-End: end of document
1014 Page-Up: Up a screen
1015 Page-Down: Down a screen
1019 Ctrl-Alt-PgUp: Start of window
1020 Ctrl-Alt-PgDn: End of window
1021 F8: Start selection mode
1022 Esc: End selection mode
1024 Adding Shift does the above but starts/extends selection.
1029 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1031 bool success
= false;
1033 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1035 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1036 success
= WordRight(1, flags
);
1038 success
= MoveRight(1, flags
);
1040 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1042 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1043 success
= WordLeft(1, flags
);
1045 success
= MoveLeft(1, flags
);
1047 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1049 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1050 success
= MoveToParagraphStart(flags
);
1052 success
= MoveUp(1, flags
);
1054 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1056 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1057 success
= MoveToParagraphEnd(flags
);
1059 success
= MoveDown(1, flags
);
1061 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1063 success
= PageUp(1, flags
);
1065 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1067 success
= PageDown(1, flags
);
1069 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1071 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1072 success
= MoveHome(flags
);
1074 success
= MoveToLineStart(flags
);
1076 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1078 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1079 success
= MoveEnd(flags
);
1081 success
= MoveToLineEnd(flags
);
1086 ScrollIntoView(m_caretPosition
, keyCode
);
1087 SetDefaultStyleToCursorStyle();
1093 /// Extend the selection. Selections are in caret positions.
1094 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1096 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1098 wxRichTextRange oldSelection
= m_selectionRange
;
1100 // If not currently selecting, start selecting
1101 if (m_selectionRange
.GetStart() == -2)
1103 m_selectionAnchor
= oldPos
;
1105 if (oldPos
> newPos
)
1106 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1108 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1112 // Always ensure that the selection range start is greater than
1114 if (newPos
> m_selectionAnchor
)
1115 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1117 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1120 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1122 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1124 wxLogDebug(wxT("Strange selection range"));
1133 /// Scroll into view, returning true if we scrolled.
1134 /// This takes a _caret_ position.
1135 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1137 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1143 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1145 int startXUnits
, startYUnits
;
1146 GetViewStart(& startXUnits
, & startYUnits
);
1147 int startY
= startYUnits
* ppuY
;
1150 GetVirtualSize(& sx
, & sy
);
1156 wxRect rect
= line
->GetRect();
1158 bool scrolled
= false;
1160 wxSize clientSize
= GetClientSize();
1163 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1164 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1165 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1166 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1168 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1170 // Make it scroll so this item is at the bottom
1172 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1173 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1175 // If we're still off the screen, scroll another line down
1176 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1179 if (startYUnits
!= yUnits
)
1181 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1185 else if (rect
.y
< startY
)
1187 // Make it scroll so this item is at the top
1190 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1192 if (startYUnits
!= yUnits
)
1194 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1200 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1201 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1202 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1203 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1205 if (rect
.y
< startY
)
1207 // Make it scroll so this item is at the top
1210 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1212 if (startYUnits
!= yUnits
)
1214 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1218 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1220 // Make it scroll so this item is at the bottom
1222 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1223 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1225 // If we're still off the screen, scroll another line down
1226 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1229 if (startYUnits
!= yUnits
)
1231 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1237 #if !wxRICHTEXT_USE_OWN_CARET
1245 /// Is the given position visible on the screen?
1246 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1248 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1254 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1257 GetViewStart(& startX
, & startY
);
1259 startY
= startY
* ppuY
;
1261 wxRect rect
= line
->GetRect();
1262 wxSize clientSize
= GetClientSize();
1264 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1267 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1269 m_caretPosition
= position
;
1270 m_caretAtLineStart
= showAtLineStart
;
1273 /// Move caret one visual step forward: this may mean setting a flag
1274 /// and keeping the same position if we're going from the end of one line
1275 /// to the start of the next, which may be the exact same caret position.
1276 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1278 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1280 // Only do the check if we're not at the end of the paragraph (where things work OK
1282 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1284 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1288 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1290 // We're at the end of a line. See whether we need to
1291 // stay at the same actual caret position but change visual
1292 // position, or not.
1293 if (oldPosition
== lineRange
.GetEnd())
1295 if (m_caretAtLineStart
)
1297 // We're already at the start of the line, so actually move on now.
1298 m_caretPosition
= oldPosition
+ 1;
1299 m_caretAtLineStart
= false;
1303 // We're showing at the end of the line, so keep to
1304 // the same position but indicate that we're to show
1305 // at the start of the next line.
1306 m_caretPosition
= oldPosition
;
1307 m_caretAtLineStart
= true;
1309 SetDefaultStyleToCursorStyle();
1315 SetDefaultStyleToCursorStyle();
1318 /// Move caret one visual step backward: this may mean setting a flag
1319 /// and keeping the same position if we're going from the end of one line
1320 /// to the start of the next, which may be the exact same caret position.
1321 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1323 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1325 // Only do the check if we're not at the start of the paragraph (where things work OK
1327 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1329 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1333 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1335 // We're at the start of a line. See whether we need to
1336 // stay at the same actual caret position but change visual
1337 // position, or not.
1338 if (oldPosition
== lineRange
.GetStart())
1340 m_caretPosition
= oldPosition
-1;
1341 m_caretAtLineStart
= true;
1344 else if (oldPosition
== lineRange
.GetEnd())
1346 if (m_caretAtLineStart
)
1348 // We're at the start of the line, so keep the same caret position
1349 // but clear the start-of-line flag.
1350 m_caretPosition
= oldPosition
;
1351 m_caretAtLineStart
= false;
1355 // We're showing at the end of the line, so go back
1356 // to the previous character position.
1357 m_caretPosition
= oldPosition
- 1;
1359 SetDefaultStyleToCursorStyle();
1365 SetDefaultStyleToCursorStyle();
1369 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1371 long endPos
= GetBuffer().GetRange().GetEnd();
1373 if (m_caretPosition
+ noPositions
< endPos
)
1375 long oldPos
= m_caretPosition
;
1376 long newPos
= m_caretPosition
+ noPositions
;
1378 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1382 // Determine by looking at oldPos and m_caretPosition whether
1383 // we moved from the end of a line to the start of the next line, in which case
1384 // we want to adjust the caret position such that it is positioned at the
1385 // start of the next line, rather than jumping past the first character of the
1387 if (noPositions
== 1 && !extendSel
)
1388 MoveCaretForward(oldPos
);
1390 SetCaretPosition(newPos
);
1393 SetDefaultStyleToCursorStyle();
1402 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1406 if (m_caretPosition
> startPos
- noPositions
+ 1)
1408 long oldPos
= m_caretPosition
;
1409 long newPos
= m_caretPosition
- noPositions
;
1410 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1414 if (noPositions
== 1 && !extendSel
)
1415 MoveCaretBack(oldPos
);
1417 SetCaretPosition(newPos
);
1420 SetDefaultStyleToCursorStyle();
1429 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1431 return MoveDown(- noLines
, flags
);
1435 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1440 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1441 wxPoint pt
= GetCaret()->GetPosition();
1442 long newLine
= lineNumber
+ noLines
;
1444 if (lineNumber
!= -1)
1448 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1450 if (newLine
> lastLine
)
1460 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1463 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1469 wxClientDC
dc(this);
1471 dc
.SetFont(GetFont());
1473 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1475 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1477 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1478 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1479 // so we view the caret at the start of the line.
1480 bool caretLineStart
= false;
1481 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1483 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1484 wxRichTextRange lineRange
;
1486 lineRange
= thisLine
->GetAbsoluteRange();
1488 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1491 caretLineStart
= true;
1495 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1496 if (para
&& para
->GetRange().GetStart() == newPos
)
1501 long newSelEnd
= newPos
;
1503 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1507 SetCaretPosition(newPos
, caretLineStart
);
1509 SetDefaultStyleToCursorStyle();
1517 /// Move to the end of the paragraph
1518 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1520 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1523 long newPos
= para
->GetRange().GetEnd() - 1;
1524 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1528 SetCaretPosition(newPos
);
1530 SetDefaultStyleToCursorStyle();
1538 /// Move to the start of the paragraph
1539 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1541 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1544 long newPos
= para
->GetRange().GetStart() - 1;
1545 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1549 SetCaretPosition(newPos
);
1551 SetDefaultStyleToCursorStyle();
1559 /// Move to the end of the line
1560 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1562 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1566 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1567 long newPos
= lineRange
.GetEnd();
1568 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1572 SetCaretPosition(newPos
);
1574 SetDefaultStyleToCursorStyle();
1582 /// Move to the start of the line
1583 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1585 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1588 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1589 long newPos
= lineRange
.GetStart()-1;
1591 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1595 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1597 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1599 SetDefaultStyleToCursorStyle();
1607 /// Move to the start of the buffer
1608 bool wxRichTextCtrl::MoveHome(int flags
)
1610 if (m_caretPosition
!= -1)
1612 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1616 SetCaretPosition(-1);
1618 SetDefaultStyleToCursorStyle();
1626 /// Move to the end of the buffer
1627 bool wxRichTextCtrl::MoveEnd(int flags
)
1629 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1631 if (m_caretPosition
!= endPos
)
1633 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1637 SetCaretPosition(endPos
);
1639 SetDefaultStyleToCursorStyle();
1647 /// Move noPages pages up
1648 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1650 return PageDown(- noPages
, flags
);
1653 /// Move noPages pages down
1654 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1656 // Calculate which line occurs noPages * screen height further down.
1657 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1660 wxSize clientSize
= GetClientSize();
1661 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1663 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1666 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1667 long pos
= lineRange
.GetStart()-1;
1668 if (pos
!= m_caretPosition
)
1670 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1672 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1676 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1678 SetDefaultStyleToCursorStyle();
1688 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1690 return str
== wxT(" ") || str
== wxT("\t");
1693 // Finds the caret position for the next word
1694 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1696 long endPos
= GetBuffer().GetRange().GetEnd();
1700 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1702 // First skip current text to space
1703 while (i
< endPos
&& i
> -1)
1705 // i is in character, not caret positions
1706 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1707 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1708 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1712 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1719 while (i
< endPos
&& i
> -1)
1721 // i is in character, not caret positions
1722 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1723 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1724 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1725 return wxMax(-1, i
);
1727 if (text
.empty()) // End of paragraph, or maybe an image
1728 return wxMax(-1, i
- 1);
1729 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1733 // Convert to caret position
1734 return wxMax(-1, i
- 1);
1743 long i
= m_caretPosition
;
1745 // First skip white space
1746 while (i
< endPos
&& i
> -1)
1748 // i is in character, not caret positions
1749 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1750 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1752 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1754 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1759 // Next skip current text to space
1760 while (i
< endPos
&& i
> -1)
1762 // i is in character, not caret positions
1763 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1764 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1765 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1768 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1781 /// Move n words left
1782 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1784 long pos
= FindNextWordPosition(-1);
1785 if (pos
!= m_caretPosition
)
1787 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1789 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1793 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1795 SetDefaultStyleToCursorStyle();
1803 /// Move n words right
1804 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1806 long pos
= FindNextWordPosition(1);
1807 if (pos
!= m_caretPosition
)
1809 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1811 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1815 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1817 SetDefaultStyleToCursorStyle();
1826 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1828 // Only do sizing optimization for large buffers
1829 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1831 m_fullLayoutRequired
= true;
1832 m_fullLayoutTime
= wxGetLocalTimeMillis();
1833 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1834 LayoutContent(true /* onlyVisibleRect */);
1837 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1839 #if wxRICHTEXT_BUFFERED_PAINTING
1847 /// Idle-time processing
1848 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1850 #if wxRICHTEXT_USE_OWN_CARET
1851 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1853 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1859 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1861 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1863 m_fullLayoutRequired
= false;
1864 m_fullLayoutTime
= 0;
1865 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1866 ShowPosition(m_fullLayoutSavedPosition
);
1870 if (m_caretPositionForDefaultStyle
!= -2)
1872 // If the caret position has changed, no longer reflect the default style
1874 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1875 m_caretPositionForDefaultStyle
= -2;
1882 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1884 #if wxRICHTEXT_USE_OWN_CARET
1885 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1888 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1895 /// Set up scrollbars, e.g. after a resize
1896 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1901 if (GetBuffer().IsEmpty())
1903 SetScrollbars(0, 0, 0, 0, 0, 0);
1907 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1908 // of pixels. See e.g. wxVScrolledWindow for ideas.
1909 int pixelsPerUnit
= 5;
1910 wxSize clientSize
= GetClientSize();
1912 int maxHeight
= GetBuffer().GetCachedSize().y
;
1914 // Round up so we have at least maxHeight pixels
1915 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1917 int startX
= 0, startY
= 0;
1919 GetViewStart(& startX
, & startY
);
1921 int maxPositionX
= 0;
1922 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1924 int newStartX
= wxMin(maxPositionX
, startX
);
1925 int newStartY
= wxMin(maxPositionY
, startY
);
1927 int oldPPUX
, oldPPUY
;
1928 int oldStartX
, oldStartY
;
1929 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
1930 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
1931 GetViewStart(& oldStartX
, & oldStartY
);
1932 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
1934 oldVirtualSizeY
/= oldPPUY
;
1936 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
1939 // Don't set scrollbars if there were none before, and there will be none now.
1940 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
1943 // Move to previous scroll position if
1945 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
1948 /// Paint the background
1949 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1951 wxColour backgroundColour
= GetBackgroundColour();
1952 if (!backgroundColour
.Ok())
1953 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1955 // Clear the background
1956 dc
.SetBrush(wxBrush(backgroundColour
));
1957 dc
.SetPen(*wxTRANSPARENT_PEN
);
1958 wxRect
windowRect(GetClientSize());
1959 windowRect
.x
-= 2; windowRect
.y
-= 2;
1960 windowRect
.width
+= 4; windowRect
.height
+= 4;
1962 // We need to shift the rectangle to take into account
1963 // scrolling. Converting device to logical coordinates.
1964 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1965 dc
.DrawRectangle(windowRect
);
1968 #if wxRICHTEXT_BUFFERED_PAINTING
1969 /// Recreate buffer bitmap if necessary
1970 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1973 if (sz
== wxDefaultSize
)
1974 sz
= GetClientSize();
1976 if (sz
.x
< 1 || sz
.y
< 1)
1979 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1980 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1981 return m_bufferBitmap
.Ok();
1985 // ----------------------------------------------------------------------------
1986 // file IO functions
1987 // ----------------------------------------------------------------------------
1989 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1991 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
1993 m_filename
= filename
;
1996 SetInsertionPoint(0);
1999 SetupScrollbars(true);
2001 wxTextCtrl::SendTextUpdatedEvent(this);
2007 wxLogError(_("File couldn't be loaded."));
2013 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2015 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2017 m_filename
= filename
;
2024 wxLogError(_("The text couldn't be saved."));
2029 // ----------------------------------------------------------------------------
2030 // wxRichTextCtrl specific functionality
2031 // ----------------------------------------------------------------------------
2033 /// Add a new paragraph of text to the end of the buffer
2034 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2036 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2042 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2044 wxRichTextRange range
= GetBuffer().AddImage(image
);
2049 // ----------------------------------------------------------------------------
2050 // selection and ranges
2051 // ----------------------------------------------------------------------------
2053 void wxRichTextCtrl::SelectAll()
2055 SetSelection(0, GetLastPosition()+1);
2056 m_selectionAnchor
= -1;
2060 void wxRichTextCtrl::SelectNone()
2062 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2064 wxRichTextRange oldSelection
= m_selectionRange
;
2066 m_selectionRange
= wxRichTextRange(-2, -2);
2068 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2070 m_selectionAnchor
= -2;
2073 static bool wxIsWordDelimiter(const wxString
& text
)
2075 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2078 /// Select the word at the given character position
2079 bool wxRichTextCtrl::SelectWord(long position
)
2081 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2084 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2088 if (position
== para
->GetRange().GetEnd())
2091 long positionStart
= position
;
2092 long positionEnd
= position
;
2094 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2096 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2097 if (wxIsWordDelimiter(text
))
2103 if (positionStart
< para
->GetRange().GetStart())
2104 positionStart
= para
->GetRange().GetStart();
2106 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2108 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2109 if (wxIsWordDelimiter(text
))
2115 if (positionEnd
>= para
->GetRange().GetEnd())
2116 positionEnd
= para
->GetRange().GetEnd();
2118 if (positionEnd
< positionStart
)
2121 SetSelection(positionStart
, positionEnd
+1);
2123 if (positionStart
>= 0)
2125 MoveCaret(positionStart
-1, true);
2126 SetDefaultStyleToCursorStyle();
2132 wxString
wxRichTextCtrl::GetStringSelection() const
2135 GetSelection(&from
, &to
);
2137 return GetRange(from
, to
);
2140 // ----------------------------------------------------------------------------
2142 // ----------------------------------------------------------------------------
2144 wxTextCtrlHitTestResult
2145 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2147 // implement in terms of the other overload as the native ports typically
2148 // can get the position and not (x, y) pair directly (although wxUniv
2149 // directly gets x and y -- and so overrides this method as well)
2151 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2153 if ( rc
!= wxTE_HT_UNKNOWN
)
2155 PositionToXY(pos
, x
, y
);
2161 wxTextCtrlHitTestResult
2162 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2165 wxClientDC
dc((wxRichTextCtrl
*) this);
2166 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2168 // Buffer uses logical position (relative to start of buffer)
2170 wxPoint pt2
= GetLogicalPoint(pt
);
2172 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2174 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2175 return wxTE_HT_BEFORE
;
2176 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2177 return wxTE_HT_BEYOND
;
2178 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2179 return wxTE_HT_ON_TEXT
;
2181 return wxTE_HT_UNKNOWN
;
2184 // ----------------------------------------------------------------------------
2185 // set/get the controls text
2186 // ----------------------------------------------------------------------------
2188 wxString
wxRichTextCtrl::GetValue() const
2190 return GetBuffer().GetText();
2193 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2195 // Public API for range is different from internals
2196 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2199 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2201 // Don't call Clear here, since it always sends a text updated event
2202 m_buffer
.ResetAndClearCommands();
2203 m_buffer
.SetDirty(true);
2204 m_caretPosition
= -1;
2205 m_caretPositionForDefaultStyle
= -2;
2206 m_caretAtLineStart
= false;
2207 m_selectionRange
.SetRange(-2, -2);
2217 if (!value
.IsEmpty())
2219 // Remove empty paragraph
2220 GetBuffer().Clear();
2221 DoWriteText(value
, flags
);
2223 // for compatibility, don't move the cursor when doing SetValue()
2224 SetInsertionPoint(0);
2228 // still send an event for consistency
2229 if (flags
& SetValue_SendEvent
)
2230 wxTextCtrl::SendTextUpdatedEvent(this);
2235 void wxRichTextCtrl::WriteText(const wxString
& value
)
2240 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2242 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2244 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2246 if ( flags
& SetValue_SendEvent
)
2247 wxTextCtrl::SendTextUpdatedEvent(this);
2250 void wxRichTextCtrl::AppendText(const wxString
& text
)
2252 SetInsertionPointEnd();
2257 /// Write an image at the current insertion point
2258 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2260 wxRichTextImageBlock imageBlock
;
2262 wxImage image2
= image
;
2263 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2264 return WriteImage(imageBlock
);
2269 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2271 wxRichTextImageBlock imageBlock
;
2274 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2275 return WriteImage(imageBlock
);
2280 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2282 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2285 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2289 wxRichTextImageBlock imageBlock
;
2291 wxImage image
= bitmap
.ConvertToImage();
2292 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2293 return WriteImage(imageBlock
);
2299 /// Insert a newline (actually paragraph) at the current insertion point.
2300 bool wxRichTextCtrl::Newline()
2302 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2305 /// Insert a line break at the current insertion point.
2306 bool wxRichTextCtrl::LineBreak()
2309 text
= wxRichTextLineBreakChar
;
2310 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2313 // ----------------------------------------------------------------------------
2314 // Clipboard operations
2315 // ----------------------------------------------------------------------------
2317 void wxRichTextCtrl::Copy()
2321 wxRichTextRange range
= GetInternalSelectionRange();
2322 GetBuffer().CopyToClipboard(range
);
2326 void wxRichTextCtrl::Cut()
2330 wxRichTextRange range
= GetInternalSelectionRange();
2331 GetBuffer().CopyToClipboard(range
);
2333 DeleteSelectedContent();
2339 void wxRichTextCtrl::Paste()
2343 BeginBatchUndo(_("Paste"));
2345 long newPos
= m_caretPosition
;
2346 DeleteSelectedContent(& newPos
);
2348 GetBuffer().PasteFromClipboard(newPos
);
2354 void wxRichTextCtrl::DeleteSelection()
2356 if (CanDeleteSelection())
2358 DeleteSelectedContent();
2362 bool wxRichTextCtrl::HasSelection() const
2364 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2367 bool wxRichTextCtrl::CanCopy() const
2369 // Can copy if there's a selection
2370 return HasSelection();
2373 bool wxRichTextCtrl::CanCut() const
2375 return HasSelection() && IsEditable();
2378 bool wxRichTextCtrl::CanPaste() const
2380 if ( !IsEditable() )
2383 return GetBuffer().CanPasteFromClipboard();
2386 bool wxRichTextCtrl::CanDeleteSelection() const
2388 return HasSelection() && IsEditable();
2392 // ----------------------------------------------------------------------------
2394 // ----------------------------------------------------------------------------
2396 void wxRichTextCtrl::SetEditable(bool editable
)
2398 m_editable
= editable
;
2401 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2405 m_caretPosition
= pos
- 1;
2410 void wxRichTextCtrl::SetInsertionPointEnd()
2412 long pos
= GetLastPosition();
2413 SetInsertionPoint(pos
);
2416 long wxRichTextCtrl::GetInsertionPoint() const
2418 return m_caretPosition
+1;
2421 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2423 return GetBuffer().GetRange().GetEnd();
2426 // If the return values from and to are the same, there is no
2428 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2430 *from
= m_selectionRange
.GetStart();
2431 *to
= m_selectionRange
.GetEnd();
2432 if ((*to
) != -1 && (*to
) != -2)
2436 bool wxRichTextCtrl::IsEditable() const
2441 // ----------------------------------------------------------------------------
2443 // ----------------------------------------------------------------------------
2445 void wxRichTextCtrl::SetSelection(long from
, long to
)
2447 // if from and to are both -1, it means (in wxWidgets) that all text should
2449 if ( (from
== -1) && (to
== -1) )
2452 to
= GetLastPosition()+1;
2455 DoSetSelection(from
, to
);
2458 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2466 wxRichTextRange oldSelection
= m_selectionRange
;
2467 m_selectionAnchor
= from
;
2468 m_selectionRange
.SetRange(from
, to
-1);
2470 m_caretPosition
= from
-1;
2472 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2477 // ----------------------------------------------------------------------------
2479 // ----------------------------------------------------------------------------
2481 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2482 const wxString
& value
)
2484 BeginBatchUndo(_("Replace"));
2486 DeleteSelectedContent();
2488 DoWriteText(value
, SetValue_SelectionOnly
);
2493 void wxRichTextCtrl::Remove(long from
, long to
)
2497 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2504 bool wxRichTextCtrl::IsModified() const
2506 return m_buffer
.IsModified();
2509 void wxRichTextCtrl::MarkDirty()
2511 m_buffer
.Modify(true);
2514 void wxRichTextCtrl::DiscardEdits()
2516 m_caretPositionForDefaultStyle
= -2;
2517 m_buffer
.Modify(false);
2518 m_buffer
.GetCommandProcessor()->ClearCommands();
2521 int wxRichTextCtrl::GetNumberOfLines() const
2523 return GetBuffer().GetParagraphCount();
2526 // ----------------------------------------------------------------------------
2527 // Positions <-> coords
2528 // ----------------------------------------------------------------------------
2530 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2532 return GetBuffer().XYToPosition(x
, y
);
2535 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2537 return GetBuffer().PositionToXY(pos
, x
, y
);
2540 // ----------------------------------------------------------------------------
2542 // ----------------------------------------------------------------------------
2544 void wxRichTextCtrl::ShowPosition(long pos
)
2546 if (!IsPositionVisible(pos
))
2547 ScrollIntoView(pos
-1, WXK_DOWN
);
2550 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2552 return GetBuffer().GetParagraphLength(lineNo
);
2555 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2557 return GetBuffer().GetParagraphText(lineNo
);
2560 // ----------------------------------------------------------------------------
2562 // ----------------------------------------------------------------------------
2564 void wxRichTextCtrl::Undo()
2568 GetCommandProcessor()->Undo();
2572 void wxRichTextCtrl::Redo()
2576 GetCommandProcessor()->Redo();
2580 bool wxRichTextCtrl::CanUndo() const
2582 return GetCommandProcessor()->CanUndo();
2585 bool wxRichTextCtrl::CanRedo() const
2587 return GetCommandProcessor()->CanRedo();
2590 // ----------------------------------------------------------------------------
2591 // implementation details
2592 // ----------------------------------------------------------------------------
2594 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2596 SetValue(event
.GetString());
2597 GetEventHandler()->ProcessEvent(event
);
2600 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2602 // By default, load the first file into the text window.
2603 if (event
.GetNumberOfFiles() > 0)
2605 LoadFile(event
.GetFiles()[0]);
2609 wxSize
wxRichTextCtrl::DoGetBestSize() const
2611 return wxSize(10, 10);
2614 // ----------------------------------------------------------------------------
2615 // standard handlers for standard edit menu events
2616 // ----------------------------------------------------------------------------
2618 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2623 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2628 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2633 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2638 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2643 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2648 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2650 event
.Enable( CanCut() );
2653 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2655 event
.Enable( CanCopy() );
2658 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2660 event
.Enable( CanDeleteSelection() );
2663 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2665 event
.Enable( CanPaste() );
2668 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2670 event
.Enable( CanUndo() );
2671 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2674 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2676 event
.Enable( CanRedo() );
2677 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2680 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2685 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2687 event
.Enable(GetLastPosition() > 0);
2690 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2692 if (event
.GetEventObject() != this)
2700 m_contextMenu
= new wxMenu
;
2701 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2702 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2703 m_contextMenu
->AppendSeparator();
2704 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2705 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2706 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2707 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2708 m_contextMenu
->AppendSeparator();
2709 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2711 PopupMenu(m_contextMenu
);
2715 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2717 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2720 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2722 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2725 // extended style setting operation with flags including:
2726 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2727 // see richtextbuffer.h for more details.
2729 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2731 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2734 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2736 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2739 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2741 return GetBuffer().GetDefaultStyle();
2744 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2746 return GetBuffer().GetStyle(position
, style
);
2749 // get the common set of styles for the range
2750 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2752 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2755 /// Get the content (uncombined) attributes for this position.
2756 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2758 return GetBuffer().GetUncombinedStyle(position
, style
);
2761 /// Set font, and also the buffer attributes
2762 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2764 wxControl::SetFont(font
);
2766 wxTextAttr attr
= GetBuffer().GetAttributes();
2768 GetBuffer().SetBasicStyle(attr
);
2770 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2776 /// Transform logical to physical
2777 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2780 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2785 /// Transform physical to logical
2786 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2789 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2794 /// Position the caret
2795 void wxRichTextCtrl::PositionCaret()
2800 //wxLogDebug(wxT("PositionCaret"));
2803 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2805 wxPoint newPt
= caretRect
.GetPosition();
2806 wxSize newSz
= caretRect
.GetSize();
2807 wxPoint pt
= GetPhysicalPoint(newPt
);
2808 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2811 if (GetCaret()->GetSize() != newSz
)
2812 GetCaret()->SetSize(newSz
);
2813 GetCaret()->Move(pt
);
2819 /// Get the caret height and position for the given character position
2820 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2822 wxClientDC
dc(this);
2823 dc
.SetFont(GetFont());
2830 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2832 // Caret height can't be zero
2834 height
= dc
.GetCharHeight();
2836 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2843 /// Gets the line for the visible caret position. If the caret is
2844 /// shown at the very end of the line, it means the next character is actually
2845 /// on the following line. So let's get the line we're expecting to find
2846 /// if this is the case.
2847 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2849 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2850 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2853 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2854 if (caretPosition
== lineRange
.GetStart()-1 &&
2855 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2857 if (!m_caretAtLineStart
)
2858 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2865 /// Move the caret to the given character position
2866 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2868 if (GetBuffer().GetDirty())
2871 if (pos
<= GetBuffer().GetRange().GetEnd())
2873 SetCaretPosition(pos
, showAtLineStart
);
2883 /// Layout the buffer: which we must do before certain operations, such as
2884 /// setting the caret position.
2885 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2887 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2889 wxRect
availableSpace(GetClientSize());
2890 if (availableSpace
.width
== 0)
2891 availableSpace
.width
= 10;
2892 if (availableSpace
.height
== 0)
2893 availableSpace
.height
= 10;
2895 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2896 if (onlyVisibleRect
)
2898 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2899 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2902 wxClientDC
dc(this);
2903 dc
.SetFont(GetFont());
2907 GetBuffer().Defragment();
2908 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2909 GetBuffer().Layout(dc
, availableSpace
, flags
);
2910 GetBuffer().SetDirty(false);
2919 /// Is all of the selection bold?
2920 bool wxRichTextCtrl::IsSelectionBold()
2925 wxRichTextRange range
= GetSelectionRange();
2926 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2927 attr
.SetFontWeight(wxBOLD
);
2929 return HasCharacterAttributes(range
, attr
);
2933 // If no selection, then we need to combine current style with default style
2934 // to see what the effect would be if we started typing.
2936 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2938 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2939 if (GetStyle(pos
, attr
))
2941 if (IsDefaultStyleShowing())
2942 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2943 return attr
.GetFontWeight() == wxBOLD
;
2949 /// Is all of the selection italics?
2950 bool wxRichTextCtrl::IsSelectionItalics()
2954 wxRichTextRange range
= GetSelectionRange();
2956 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2957 attr
.SetFontStyle(wxITALIC
);
2959 return HasCharacterAttributes(range
, attr
);
2963 // If no selection, then we need to combine current style with default style
2964 // to see what the effect would be if we started typing.
2966 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2968 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2969 if (GetStyle(pos
, attr
))
2971 if (IsDefaultStyleShowing())
2972 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2973 return attr
.GetFontStyle() == wxITALIC
;
2979 /// Is all of the selection underlined?
2980 bool wxRichTextCtrl::IsSelectionUnderlined()
2984 wxRichTextRange range
= GetSelectionRange();
2986 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2987 attr
.SetFontUnderlined(true);
2989 return HasCharacterAttributes(range
, attr
);
2993 // If no selection, then we need to combine current style with default style
2994 // to see what the effect would be if we started typing.
2996 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2997 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2999 if (GetStyle(pos
, attr
))
3001 if (IsDefaultStyleShowing())
3002 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3003 return attr
.GetFontUnderlined();
3009 /// Apply bold to the selection
3010 bool wxRichTextCtrl::ApplyBoldToSelection()
3013 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3014 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
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 italic to the selection
3028 bool wxRichTextCtrl::ApplyItalicToSelection()
3031 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3032 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
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 /// Apply underline to the selection
3046 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3049 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3050 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3053 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3056 wxRichTextAttr current
= GetDefaultStyleEx();
3057 current
.Apply(attr
);
3058 SetAndShowDefaultStyle(current
);
3063 /// Is all of the selection aligned according to the specified flag?
3064 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3066 wxRichTextRange range
;
3068 range
= GetSelectionRange();
3070 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3073 attr
.SetAlignment(alignment
);
3075 return HasParagraphAttributes(range
, attr
);
3078 /// Apply alignment to the selection
3079 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3082 attr
.SetAlignment(alignment
);
3084 return SetStyle(GetSelectionRange(), attr
);
3087 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3089 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3094 /// Apply a named style to the selection
3095 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3097 // Flags are defined within each definition, so only certain
3098 // attributes are applied.
3099 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3101 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3103 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3105 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3107 wxRichTextRange range
;
3110 range
= GetSelectionRange();
3113 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3114 range
= wxRichTextRange(pos
, pos
+1);
3117 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3120 // Make sure the attr has the style name
3121 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3123 attr
.SetParagraphStyleName(def
->GetName());
3125 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3126 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3127 // to change its style independently.
3128 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3131 attr
.SetCharacterStyleName(def
->GetName());
3134 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3137 wxRichTextAttr current
= GetDefaultStyleEx();
3138 current
.Apply(attr
);
3139 SetAndShowDefaultStyle(current
);
3144 /// Apply the style sheet to the buffer, for example if the styles have changed.
3145 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3148 styleSheet
= GetBuffer().GetStyleSheet();
3152 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3154 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3162 /// Sets the default style to the style under the cursor
3163 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3166 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3168 // If at the start of a paragraph, use the next position.
3169 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3171 if (GetUncombinedStyle(pos
, attr
))
3173 SetDefaultStyle(attr
);
3180 /// Returns the first visible position in the current view
3181 long wxRichTextCtrl::GetFirstVisiblePosition() const
3183 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3185 return line
->GetAbsoluteRange().GetStart();
3190 /// Get the first visible point in the window
3191 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3194 int startXUnits
, startYUnits
;
3196 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3197 GetViewStart(& startXUnits
, & startYUnits
);
3199 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3202 /// The adjusted caret position is the character position adjusted to take
3203 /// into account whether we're at the start of a paragraph, in which case
3204 /// style information should be taken from the next position, not current one.
3205 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3207 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3209 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3214 /// Get/set the selection range in character positions. -1, -1 means no selection.
3215 /// The range is in API convention, i.e. a single character selection is denoted
3217 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3219 wxRichTextRange range
= GetInternalSelectionRange();
3220 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3221 range
.SetEnd(range
.GetEnd() + 1);
3225 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3227 wxRichTextRange
range1(range
);
3228 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3229 range1
.SetEnd(range1
.GetEnd() - 1);
3231 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3233 SetInternalSelectionRange(range1
);
3237 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3239 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3242 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3244 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3247 /// Clear list for given range
3248 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3250 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3253 /// Number/renumber any list elements in the given range
3254 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3256 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3259 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3261 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3264 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3265 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3267 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3270 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3272 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3275 /// Deletes the content in the given range
3276 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3278 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3281 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3283 if (sm_availableFontNames
.GetCount() == 0)
3285 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3286 sm_availableFontNames
.Sort();
3288 return sm_availableFontNames
;
3291 void wxRichTextCtrl::ClearAvailableFontNames()
3293 sm_availableFontNames
.Clear();
3296 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3298 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3300 wxTextAttrEx basicStyle
= GetBasicStyle();
3301 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3302 SetBasicStyle(basicStyle
);
3303 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3308 // Refresh the area affected by a selection change
3309 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3311 // Calculate the refresh rectangle - just the affected lines
3312 long firstPos
, lastPos
;
3313 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3315 firstPos
= newSelection
.GetStart();
3316 lastPos
= newSelection
.GetEnd();
3318 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3320 firstPos
= oldSelection
.GetStart();
3321 lastPos
= oldSelection
.GetEnd();
3323 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3329 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3330 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3333 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3334 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3336 if (firstLine
&& lastLine
)
3338 wxSize clientSize
= GetClientSize();
3339 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3340 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3343 pt1
.y
= wxMax(0, pt1
.y
);
3345 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3347 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3348 RefreshRect(rect
, false);
3356 #if wxRICHTEXT_USE_OWN_CARET
3358 // ----------------------------------------------------------------------------
3359 // initialization and destruction
3360 // ----------------------------------------------------------------------------
3362 void wxRichTextCaret::Init()
3368 m_richTextCtrl
= NULL
;
3369 m_needsUpdate
= false;
3372 wxRichTextCaret::~wxRichTextCaret()
3376 // ----------------------------------------------------------------------------
3377 // showing/hiding/moving the caret (base class interface)
3378 // ----------------------------------------------------------------------------
3380 void wxRichTextCaret::DoShow()
3385 void wxRichTextCaret::DoHide()
3390 void wxRichTextCaret::DoMove()
3396 if (m_xOld
!= -1 && m_yOld
!= -1)
3400 wxRect
rect(GetPosition(), GetSize());
3401 m_richTextCtrl
->RefreshRect(rect
, false);
3410 void wxRichTextCaret::DoSize()
3412 int countVisible
= m_countVisible
;
3413 if (countVisible
> 0)
3419 if (countVisible
> 0)
3421 m_countVisible
= countVisible
;
3426 // ----------------------------------------------------------------------------
3427 // handling the focus
3428 // ----------------------------------------------------------------------------
3430 void wxRichTextCaret::OnSetFocus()
3438 void wxRichTextCaret::OnKillFocus()
3443 // ----------------------------------------------------------------------------
3444 // drawing the caret
3445 // ----------------------------------------------------------------------------
3447 void wxRichTextCaret::Refresh()
3451 wxRect
rect(GetPosition(), GetSize());
3452 m_richTextCtrl
->RefreshRect(rect
, false);
3456 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3458 dc
->SetPen( *wxBLACK_PEN
);
3460 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3461 dc
->SetPen(*wxBLACK_PEN
);
3463 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3464 // done under wxGTK - no idea why
3465 //dc->SetLogicalFunction(wxINVERT);
3467 wxPoint
pt(m_x
, m_y
);
3471 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3473 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3476 // wxRICHTEXT_USE_OWN_CARET