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);
700 m_caretAtLineStart
= true;
704 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
707 SetDefaultStyleToCursorStyle();
709 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
711 wxRichTextEvent
cmdEvent(
712 wxEVT_COMMAND_RICHTEXT_RETURN
,
714 cmdEvent
.SetEventObject(this);
715 cmdEvent
.SetFlags(flags
);
716 cmdEvent
.SetPosition(newPos
+1);
718 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
720 // Generate conventional event
721 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
722 InitCommandEvent(textEvent
);
724 GetEventHandler()->ProcessEvent(textEvent
);
728 else if (event
.GetKeyCode() == WXK_BACK
)
730 BeginBatchUndo(_("Delete Text"));
732 // Submit range in character positions, which are greater than caret positions,
733 // so subtract 1 for deleted character and add 1 for conversion to character position.
734 if (m_caretPosition
> -1 && !HasSelection())
736 bool processed
= false;
739 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
740 if (pos
!= -1 && (pos
< m_caretPosition
))
742 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, m_caretPosition
), this);
748 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
751 DeleteSelectedContent();
755 if (GetLastPosition() == -1)
759 m_caretPosition
= -1;
761 SetDefaultStyleToCursorStyle();
764 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
766 wxRichTextEvent
cmdEvent(
767 wxEVT_COMMAND_RICHTEXT_DELETE
,
769 cmdEvent
.SetEventObject(this);
770 cmdEvent
.SetFlags(flags
);
771 cmdEvent
.SetPosition(m_caretPosition
+1);
772 GetEventHandler()->ProcessEvent(cmdEvent
);
776 else if (event
.GetKeyCode() == WXK_DELETE
)
778 BeginBatchUndo(_("Delete Text"));
780 // Submit range in character positions, which are greater than caret positions,
781 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
783 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
786 DeleteSelectedContent();
790 if (GetLastPosition() == -1)
794 m_caretPosition
= -1;
796 SetDefaultStyleToCursorStyle();
799 wxRichTextEvent
cmdEvent(
800 wxEVT_COMMAND_RICHTEXT_DELETE
,
802 cmdEvent
.SetEventObject(this);
803 cmdEvent
.SetFlags(flags
);
804 cmdEvent
.SetPosition(m_caretPosition
+1);
805 GetEventHandler()->ProcessEvent(cmdEvent
);
811 long keycode
= event
.GetKeyCode();
884 case WXK_NUMPAD_SPACE
:
886 case WXK_NUMPAD_ENTER
:
891 case WXK_NUMPAD_HOME
:
892 case WXK_NUMPAD_LEFT
:
894 case WXK_NUMPAD_RIGHT
:
895 case WXK_NUMPAD_DOWN
:
896 case WXK_NUMPAD_PAGEUP
:
897 case WXK_NUMPAD_PAGEDOWN
:
899 case WXK_NUMPAD_BEGIN
:
900 case WXK_NUMPAD_INSERT
:
901 case WXK_NUMPAD_DELETE
:
902 case WXK_NUMPAD_EQUAL
:
903 case WXK_NUMPAD_MULTIPLY
:
905 case WXK_NUMPAD_SEPARATOR
:
906 case WXK_NUMPAD_SUBTRACT
:
907 case WXK_NUMPAD_DECIMAL
:
908 case WXK_WINDOWS_LEFT
:
916 if (event
.CmdDown() || event
.AltDown())
922 wxRichTextEvent
cmdEvent(
923 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
925 cmdEvent
.SetEventObject(this);
926 cmdEvent
.SetFlags(flags
);
928 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
930 cmdEvent
.SetCharacter((wxChar
) keycode
);
932 cmdEvent
.SetPosition(m_caretPosition
+1);
934 if (keycode
== wxT('\t'))
936 // See if we need to promote or demote the selection or paragraph at the cursor
937 // position, instead of inserting a tab.
938 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
939 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
940 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
942 wxRichTextRange range
;
944 range
= GetSelectionRange();
946 range
= para
->GetRange().FromInternal();
948 int promoteBy
= event
.ShiftDown() ? 1 : -1;
950 PromoteList(promoteBy
, range
, NULL
);
952 GetEventHandler()->ProcessEvent(cmdEvent
);
958 BeginBatchUndo(_("Insert Text"));
960 long newPos
= m_caretPosition
;
961 DeleteSelectedContent(& newPos
);
964 wxString str
= event
.GetUnicodeKey();
966 wxString str
= (wxChar
) event
.GetKeyCode();
968 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
972 SetDefaultStyleToCursorStyle();
973 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
975 GetEventHandler()->ProcessEvent(cmdEvent
);
983 /// Delete content if there is a selection, e.g. when pressing a key.
984 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
988 long pos
= m_selectionRange
.GetStart();
989 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
990 m_selectionRange
.SetRange(-2, -2);
1000 /// Keyboard navigation
1004 Left: left one character
1005 Right: right one character
1008 Ctrl-Left: left one word
1009 Ctrl-Right: right one word
1010 Ctrl-Up: previous paragraph start
1011 Ctrl-Down: next start of paragraph
1014 Ctrl-Home: start of document
1015 Ctrl-End: end of document
1016 Page-Up: Up a screen
1017 Page-Down: Down a screen
1021 Ctrl-Alt-PgUp: Start of window
1022 Ctrl-Alt-PgDn: End of window
1023 F8: Start selection mode
1024 Esc: End selection mode
1026 Adding Shift does the above but starts/extends selection.
1031 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1033 bool success
= false;
1035 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1037 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1038 success
= WordRight(1, flags
);
1040 success
= MoveRight(1, flags
);
1042 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1044 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1045 success
= WordLeft(1, flags
);
1047 success
= MoveLeft(1, flags
);
1049 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1051 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1052 success
= MoveToParagraphStart(flags
);
1054 success
= MoveUp(1, flags
);
1056 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1058 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1059 success
= MoveToParagraphEnd(flags
);
1061 success
= MoveDown(1, flags
);
1063 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1065 success
= PageUp(1, flags
);
1067 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1069 success
= PageDown(1, flags
);
1071 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1073 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1074 success
= MoveHome(flags
);
1076 success
= MoveToLineStart(flags
);
1078 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1080 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1081 success
= MoveEnd(flags
);
1083 success
= MoveToLineEnd(flags
);
1088 ScrollIntoView(m_caretPosition
, keyCode
);
1089 SetDefaultStyleToCursorStyle();
1095 /// Extend the selection. Selections are in caret positions.
1096 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1098 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1100 wxRichTextRange oldSelection
= m_selectionRange
;
1102 // If not currently selecting, start selecting
1103 if (m_selectionRange
.GetStart() == -2)
1105 m_selectionAnchor
= oldPos
;
1107 if (oldPos
> newPos
)
1108 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1110 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1114 // Always ensure that the selection range start is greater than
1116 if (newPos
> m_selectionAnchor
)
1117 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1119 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1122 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1124 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1126 wxLogDebug(wxT("Strange selection range"));
1135 /// Scroll into view, returning true if we scrolled.
1136 /// This takes a _caret_ position.
1137 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1139 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1145 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1147 int startXUnits
, startYUnits
;
1148 GetViewStart(& startXUnits
, & startYUnits
);
1149 int startY
= startYUnits
* ppuY
;
1152 GetVirtualSize(& sx
, & sy
);
1158 wxRect rect
= line
->GetRect();
1160 bool scrolled
= false;
1162 wxSize clientSize
= GetClientSize();
1165 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1166 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1167 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1168 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1170 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1172 // Make it scroll so this item is at the bottom
1174 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1175 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1177 // If we're still off the screen, scroll another line down
1178 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1181 if (startYUnits
!= yUnits
)
1183 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1187 else if (rect
.y
< startY
)
1189 // Make it scroll so this item is at the top
1192 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1194 if (startYUnits
!= yUnits
)
1196 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1202 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1203 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1204 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1205 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1207 if (rect
.y
< startY
)
1209 // Make it scroll so this item is at the top
1212 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1214 if (startYUnits
!= yUnits
)
1216 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1220 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1222 // Make it scroll so this item is at the bottom
1224 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1225 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1227 // If we're still off the screen, scroll another line down
1228 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1231 if (startYUnits
!= yUnits
)
1233 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1239 #if !wxRICHTEXT_USE_OWN_CARET
1247 /// Is the given position visible on the screen?
1248 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1250 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1256 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1259 GetViewStart(& startX
, & startY
);
1261 startY
= startY
* ppuY
;
1263 wxRect rect
= line
->GetRect();
1264 wxSize clientSize
= GetClientSize();
1266 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1269 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1271 m_caretPosition
= position
;
1272 m_caretAtLineStart
= showAtLineStart
;
1275 /// Move caret one visual step forward: this may mean setting a flag
1276 /// and keeping the same position if we're going from the end of one line
1277 /// to the start of the next, which may be the exact same caret position.
1278 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1280 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1282 // Only do the check if we're not at the end of the paragraph (where things work OK
1284 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1286 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1290 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1292 // We're at the end of a line. See whether we need to
1293 // stay at the same actual caret position but change visual
1294 // position, or not.
1295 if (oldPosition
== lineRange
.GetEnd())
1297 if (m_caretAtLineStart
)
1299 // We're already at the start of the line, so actually move on now.
1300 m_caretPosition
= oldPosition
+ 1;
1301 m_caretAtLineStart
= false;
1305 // We're showing at the end of the line, so keep to
1306 // the same position but indicate that we're to show
1307 // at the start of the next line.
1308 m_caretPosition
= oldPosition
;
1309 m_caretAtLineStart
= true;
1311 SetDefaultStyleToCursorStyle();
1317 SetDefaultStyleToCursorStyle();
1320 /// Move caret one visual step backward: this may mean setting a flag
1321 /// and keeping the same position if we're going from the end of one line
1322 /// to the start of the next, which may be the exact same caret position.
1323 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1325 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1327 // Only do the check if we're not at the start of the paragraph (where things work OK
1329 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1331 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1335 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1337 // We're at the start of a line. See whether we need to
1338 // stay at the same actual caret position but change visual
1339 // position, or not.
1340 if (oldPosition
== lineRange
.GetStart())
1342 m_caretPosition
= oldPosition
-1;
1343 m_caretAtLineStart
= true;
1346 else if (oldPosition
== lineRange
.GetEnd())
1348 if (m_caretAtLineStart
)
1350 // We're at the start of the line, so keep the same caret position
1351 // but clear the start-of-line flag.
1352 m_caretPosition
= oldPosition
;
1353 m_caretAtLineStart
= false;
1357 // We're showing at the end of the line, so go back
1358 // to the previous character position.
1359 m_caretPosition
= oldPosition
- 1;
1361 SetDefaultStyleToCursorStyle();
1367 SetDefaultStyleToCursorStyle();
1371 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1373 long endPos
= GetBuffer().GetRange().GetEnd();
1375 if (m_caretPosition
+ noPositions
< endPos
)
1377 long oldPos
= m_caretPosition
;
1378 long newPos
= m_caretPosition
+ noPositions
;
1380 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1384 // Determine by looking at oldPos and m_caretPosition whether
1385 // we moved from the end of a line to the start of the next line, in which case
1386 // we want to adjust the caret position such that it is positioned at the
1387 // start of the next line, rather than jumping past the first character of the
1389 if (noPositions
== 1 && !extendSel
)
1390 MoveCaretForward(oldPos
);
1392 SetCaretPosition(newPos
);
1395 SetDefaultStyleToCursorStyle();
1404 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1408 if (m_caretPosition
> startPos
- noPositions
+ 1)
1410 long oldPos
= m_caretPosition
;
1411 long newPos
= m_caretPosition
- noPositions
;
1412 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1416 if (noPositions
== 1 && !extendSel
)
1417 MoveCaretBack(oldPos
);
1419 SetCaretPosition(newPos
);
1422 SetDefaultStyleToCursorStyle();
1431 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1433 return MoveDown(- noLines
, flags
);
1437 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1442 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1443 wxPoint pt
= GetCaret()->GetPosition();
1444 long newLine
= lineNumber
+ noLines
;
1446 if (lineNumber
!= -1)
1450 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1452 if (newLine
> lastLine
)
1462 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1465 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1471 wxClientDC
dc(this);
1473 dc
.SetFont(GetFont());
1475 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1477 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1479 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1480 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1481 // so we view the caret at the start of the line.
1482 bool caretLineStart
= false;
1483 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1485 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1486 wxRichTextRange lineRange
;
1488 lineRange
= thisLine
->GetAbsoluteRange();
1490 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1493 caretLineStart
= true;
1497 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1498 if (para
&& para
->GetRange().GetStart() == newPos
)
1503 long newSelEnd
= newPos
;
1505 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1509 SetCaretPosition(newPos
, caretLineStart
);
1511 SetDefaultStyleToCursorStyle();
1519 /// Move to the end of the paragraph
1520 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1522 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1525 long newPos
= para
->GetRange().GetEnd() - 1;
1526 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1530 SetCaretPosition(newPos
);
1532 SetDefaultStyleToCursorStyle();
1540 /// Move to the start of the paragraph
1541 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1543 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1546 long newPos
= para
->GetRange().GetStart() - 1;
1547 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1551 SetCaretPosition(newPos
);
1553 SetDefaultStyleToCursorStyle();
1561 /// Move to the end of the line
1562 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1564 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1568 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1569 long newPos
= lineRange
.GetEnd();
1570 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1574 SetCaretPosition(newPos
);
1576 SetDefaultStyleToCursorStyle();
1584 /// Move to the start of the line
1585 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1587 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1590 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1591 long newPos
= lineRange
.GetStart()-1;
1593 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1597 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1599 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1601 SetDefaultStyleToCursorStyle();
1609 /// Move to the start of the buffer
1610 bool wxRichTextCtrl::MoveHome(int flags
)
1612 if (m_caretPosition
!= -1)
1614 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1618 SetCaretPosition(-1);
1620 SetDefaultStyleToCursorStyle();
1628 /// Move to the end of the buffer
1629 bool wxRichTextCtrl::MoveEnd(int flags
)
1631 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1633 if (m_caretPosition
!= endPos
)
1635 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1639 SetCaretPosition(endPos
);
1641 SetDefaultStyleToCursorStyle();
1649 /// Move noPages pages up
1650 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1652 return PageDown(- noPages
, flags
);
1655 /// Move noPages pages down
1656 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1658 // Calculate which line occurs noPages * screen height further down.
1659 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1662 wxSize clientSize
= GetClientSize();
1663 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1665 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1668 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1669 long pos
= lineRange
.GetStart()-1;
1670 if (pos
!= m_caretPosition
)
1672 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1674 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1678 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1680 SetDefaultStyleToCursorStyle();
1690 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1692 return str
== wxT(" ") || str
== wxT("\t");
1695 // Finds the caret position for the next word
1696 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1698 long endPos
= GetBuffer().GetRange().GetEnd();
1702 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1704 // First skip current text to space
1705 while (i
< endPos
&& i
> -1)
1707 // i is in character, not caret positions
1708 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1709 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1710 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1714 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1721 while (i
< endPos
&& i
> -1)
1723 // i is in character, not caret positions
1724 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1725 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1726 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1727 return wxMax(-1, i
);
1729 if (text
.empty()) // End of paragraph, or maybe an image
1730 return wxMax(-1, i
- 1);
1731 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1735 // Convert to caret position
1736 return wxMax(-1, i
- 1);
1745 long i
= m_caretPosition
;
1747 // First skip white space
1748 while (i
< endPos
&& i
> -1)
1750 // i is in character, not caret positions
1751 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1752 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1754 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1756 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1761 // Next skip current text to space
1762 while (i
< endPos
&& i
> -1)
1764 // i is in character, not caret positions
1765 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1766 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1767 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1770 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1783 /// Move n words left
1784 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1786 long pos
= FindNextWordPosition(-1);
1787 if (pos
!= m_caretPosition
)
1789 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1791 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1795 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1797 SetDefaultStyleToCursorStyle();
1805 /// Move n words right
1806 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1808 long pos
= FindNextWordPosition(1);
1809 if (pos
!= m_caretPosition
)
1811 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1813 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1817 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1819 SetDefaultStyleToCursorStyle();
1828 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1830 // Only do sizing optimization for large buffers
1831 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1833 m_fullLayoutRequired
= true;
1834 m_fullLayoutTime
= wxGetLocalTimeMillis();
1835 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1836 LayoutContent(true /* onlyVisibleRect */);
1839 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1841 #if wxRICHTEXT_BUFFERED_PAINTING
1849 /// Idle-time processing
1850 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1852 #if wxRICHTEXT_USE_OWN_CARET
1853 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1855 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1861 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1863 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1865 m_fullLayoutRequired
= false;
1866 m_fullLayoutTime
= 0;
1867 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1868 ShowPosition(m_fullLayoutSavedPosition
);
1872 if (m_caretPositionForDefaultStyle
!= -2)
1874 // If the caret position has changed, no longer reflect the default style
1876 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1877 m_caretPositionForDefaultStyle
= -2;
1884 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1886 #if wxRICHTEXT_USE_OWN_CARET
1887 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1890 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1897 /// Set up scrollbars, e.g. after a resize
1898 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1903 if (GetBuffer().IsEmpty())
1905 SetScrollbars(0, 0, 0, 0, 0, 0);
1909 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1910 // of pixels. See e.g. wxVScrolledWindow for ideas.
1911 int pixelsPerUnit
= 5;
1912 wxSize clientSize
= GetClientSize();
1914 int maxHeight
= GetBuffer().GetCachedSize().y
;
1916 // Round up so we have at least maxHeight pixels
1917 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1919 int startX
= 0, startY
= 0;
1921 GetViewStart(& startX
, & startY
);
1923 int maxPositionX
= 0;
1924 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1926 int newStartX
= wxMin(maxPositionX
, startX
);
1927 int newStartY
= wxMin(maxPositionY
, startY
);
1929 int oldPPUX
, oldPPUY
;
1930 int oldStartX
, oldStartY
;
1931 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
1932 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
1933 GetViewStart(& oldStartX
, & oldStartY
);
1934 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
1936 oldVirtualSizeY
/= oldPPUY
;
1938 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
1941 // Don't set scrollbars if there were none before, and there will be none now.
1942 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
1945 // Move to previous scroll position if
1947 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
1950 /// Paint the background
1951 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1953 wxColour backgroundColour
= GetBackgroundColour();
1954 if (!backgroundColour
.Ok())
1955 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1957 // Clear the background
1958 dc
.SetBrush(wxBrush(backgroundColour
));
1959 dc
.SetPen(*wxTRANSPARENT_PEN
);
1960 wxRect
windowRect(GetClientSize());
1961 windowRect
.x
-= 2; windowRect
.y
-= 2;
1962 windowRect
.width
+= 4; windowRect
.height
+= 4;
1964 // We need to shift the rectangle to take into account
1965 // scrolling. Converting device to logical coordinates.
1966 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1967 dc
.DrawRectangle(windowRect
);
1970 #if wxRICHTEXT_BUFFERED_PAINTING
1971 /// Recreate buffer bitmap if necessary
1972 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1975 if (sz
== wxDefaultSize
)
1976 sz
= GetClientSize();
1978 if (sz
.x
< 1 || sz
.y
< 1)
1981 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1982 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1983 return m_bufferBitmap
.Ok();
1987 // ----------------------------------------------------------------------------
1988 // file IO functions
1989 // ----------------------------------------------------------------------------
1991 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1993 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
1995 m_filename
= filename
;
1998 SetInsertionPoint(0);
2001 SetupScrollbars(true);
2003 wxTextCtrl::SendTextUpdatedEvent(this);
2009 wxLogError(_("File couldn't be loaded."));
2015 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2017 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2019 m_filename
= filename
;
2026 wxLogError(_("The text couldn't be saved."));
2031 // ----------------------------------------------------------------------------
2032 // wxRichTextCtrl specific functionality
2033 // ----------------------------------------------------------------------------
2035 /// Add a new paragraph of text to the end of the buffer
2036 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2038 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2044 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2046 wxRichTextRange range
= GetBuffer().AddImage(image
);
2051 // ----------------------------------------------------------------------------
2052 // selection and ranges
2053 // ----------------------------------------------------------------------------
2055 void wxRichTextCtrl::SelectAll()
2057 SetSelection(0, GetLastPosition()+1);
2058 m_selectionAnchor
= -1;
2062 void wxRichTextCtrl::SelectNone()
2064 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2066 wxRichTextRange oldSelection
= m_selectionRange
;
2068 m_selectionRange
= wxRichTextRange(-2, -2);
2070 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2072 m_selectionAnchor
= -2;
2075 static bool wxIsWordDelimiter(const wxString
& text
)
2077 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2080 /// Select the word at the given character position
2081 bool wxRichTextCtrl::SelectWord(long position
)
2083 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2086 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2090 if (position
== para
->GetRange().GetEnd())
2093 long positionStart
= position
;
2094 long positionEnd
= position
;
2096 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2098 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2099 if (wxIsWordDelimiter(text
))
2105 if (positionStart
< para
->GetRange().GetStart())
2106 positionStart
= para
->GetRange().GetStart();
2108 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2110 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2111 if (wxIsWordDelimiter(text
))
2117 if (positionEnd
>= para
->GetRange().GetEnd())
2118 positionEnd
= para
->GetRange().GetEnd();
2120 if (positionEnd
< positionStart
)
2123 SetSelection(positionStart
, positionEnd
+1);
2125 if (positionStart
>= 0)
2127 MoveCaret(positionStart
-1, true);
2128 SetDefaultStyleToCursorStyle();
2134 wxString
wxRichTextCtrl::GetStringSelection() const
2137 GetSelection(&from
, &to
);
2139 return GetRange(from
, to
);
2142 // ----------------------------------------------------------------------------
2144 // ----------------------------------------------------------------------------
2146 wxTextCtrlHitTestResult
2147 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2149 // implement in terms of the other overload as the native ports typically
2150 // can get the position and not (x, y) pair directly (although wxUniv
2151 // directly gets x and y -- and so overrides this method as well)
2153 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2155 if ( rc
!= wxTE_HT_UNKNOWN
)
2157 PositionToXY(pos
, x
, y
);
2163 wxTextCtrlHitTestResult
2164 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2167 wxClientDC
dc((wxRichTextCtrl
*) this);
2168 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2170 // Buffer uses logical position (relative to start of buffer)
2172 wxPoint pt2
= GetLogicalPoint(pt
);
2174 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2176 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2177 return wxTE_HT_BEFORE
;
2178 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2179 return wxTE_HT_BEYOND
;
2180 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2181 return wxTE_HT_ON_TEXT
;
2183 return wxTE_HT_UNKNOWN
;
2186 // ----------------------------------------------------------------------------
2187 // set/get the controls text
2188 // ----------------------------------------------------------------------------
2190 wxString
wxRichTextCtrl::GetValue() const
2192 return GetBuffer().GetText();
2195 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2197 // Public API for range is different from internals
2198 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2201 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2203 // Don't call Clear here, since it always sends a text updated event
2204 m_buffer
.ResetAndClearCommands();
2205 m_buffer
.SetDirty(true);
2206 m_caretPosition
= -1;
2207 m_caretPositionForDefaultStyle
= -2;
2208 m_caretAtLineStart
= false;
2209 m_selectionRange
.SetRange(-2, -2);
2219 if (!value
.IsEmpty())
2221 // Remove empty paragraph
2222 GetBuffer().Clear();
2223 DoWriteText(value
, flags
);
2225 // for compatibility, don't move the cursor when doing SetValue()
2226 SetInsertionPoint(0);
2230 // still send an event for consistency
2231 if (flags
& SetValue_SendEvent
)
2232 wxTextCtrl::SendTextUpdatedEvent(this);
2237 void wxRichTextCtrl::WriteText(const wxString
& value
)
2242 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2244 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2246 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2248 if ( flags
& SetValue_SendEvent
)
2249 wxTextCtrl::SendTextUpdatedEvent(this);
2252 void wxRichTextCtrl::AppendText(const wxString
& text
)
2254 SetInsertionPointEnd();
2259 /// Write an image at the current insertion point
2260 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2262 wxRichTextImageBlock imageBlock
;
2264 wxImage image2
= image
;
2265 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2266 return WriteImage(imageBlock
);
2271 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2273 wxRichTextImageBlock imageBlock
;
2276 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2277 return WriteImage(imageBlock
);
2282 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2284 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2287 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2291 wxRichTextImageBlock imageBlock
;
2293 wxImage image
= bitmap
.ConvertToImage();
2294 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2295 return WriteImage(imageBlock
);
2301 /// Insert a newline (actually paragraph) at the current insertion point.
2302 bool wxRichTextCtrl::Newline()
2304 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2307 /// Insert a line break at the current insertion point.
2308 bool wxRichTextCtrl::LineBreak()
2311 text
= wxRichTextLineBreakChar
;
2312 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2315 // ----------------------------------------------------------------------------
2316 // Clipboard operations
2317 // ----------------------------------------------------------------------------
2319 void wxRichTextCtrl::Copy()
2323 wxRichTextRange range
= GetInternalSelectionRange();
2324 GetBuffer().CopyToClipboard(range
);
2328 void wxRichTextCtrl::Cut()
2332 wxRichTextRange range
= GetInternalSelectionRange();
2333 GetBuffer().CopyToClipboard(range
);
2335 DeleteSelectedContent();
2341 void wxRichTextCtrl::Paste()
2345 BeginBatchUndo(_("Paste"));
2347 long newPos
= m_caretPosition
;
2348 DeleteSelectedContent(& newPos
);
2350 GetBuffer().PasteFromClipboard(newPos
);
2356 void wxRichTextCtrl::DeleteSelection()
2358 if (CanDeleteSelection())
2360 DeleteSelectedContent();
2364 bool wxRichTextCtrl::HasSelection() const
2366 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2369 bool wxRichTextCtrl::CanCopy() const
2371 // Can copy if there's a selection
2372 return HasSelection();
2375 bool wxRichTextCtrl::CanCut() const
2377 return HasSelection() && IsEditable();
2380 bool wxRichTextCtrl::CanPaste() const
2382 if ( !IsEditable() )
2385 return GetBuffer().CanPasteFromClipboard();
2388 bool wxRichTextCtrl::CanDeleteSelection() const
2390 return HasSelection() && IsEditable();
2394 // ----------------------------------------------------------------------------
2396 // ----------------------------------------------------------------------------
2398 void wxRichTextCtrl::SetEditable(bool editable
)
2400 m_editable
= editable
;
2403 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2407 m_caretPosition
= pos
- 1;
2412 void wxRichTextCtrl::SetInsertionPointEnd()
2414 long pos
= GetLastPosition();
2415 SetInsertionPoint(pos
);
2418 long wxRichTextCtrl::GetInsertionPoint() const
2420 return m_caretPosition
+1;
2423 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2425 return GetBuffer().GetRange().GetEnd();
2428 // If the return values from and to are the same, there is no
2430 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2432 *from
= m_selectionRange
.GetStart();
2433 *to
= m_selectionRange
.GetEnd();
2434 if ((*to
) != -1 && (*to
) != -2)
2438 bool wxRichTextCtrl::IsEditable() const
2443 // ----------------------------------------------------------------------------
2445 // ----------------------------------------------------------------------------
2447 void wxRichTextCtrl::SetSelection(long from
, long to
)
2449 // if from and to are both -1, it means (in wxWidgets) that all text should
2451 if ( (from
== -1) && (to
== -1) )
2454 to
= GetLastPosition()+1;
2457 DoSetSelection(from
, to
);
2460 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2468 wxRichTextRange oldSelection
= m_selectionRange
;
2469 m_selectionAnchor
= from
;
2470 m_selectionRange
.SetRange(from
, to
-1);
2472 m_caretPosition
= from
-1;
2474 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2479 // ----------------------------------------------------------------------------
2481 // ----------------------------------------------------------------------------
2483 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2484 const wxString
& value
)
2486 BeginBatchUndo(_("Replace"));
2488 DeleteSelectedContent();
2490 DoWriteText(value
, SetValue_SelectionOnly
);
2495 void wxRichTextCtrl::Remove(long from
, long to
)
2499 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2506 bool wxRichTextCtrl::IsModified() const
2508 return m_buffer
.IsModified();
2511 void wxRichTextCtrl::MarkDirty()
2513 m_buffer
.Modify(true);
2516 void wxRichTextCtrl::DiscardEdits()
2518 m_caretPositionForDefaultStyle
= -2;
2519 m_buffer
.Modify(false);
2520 m_buffer
.GetCommandProcessor()->ClearCommands();
2523 int wxRichTextCtrl::GetNumberOfLines() const
2525 return GetBuffer().GetParagraphCount();
2528 // ----------------------------------------------------------------------------
2529 // Positions <-> coords
2530 // ----------------------------------------------------------------------------
2532 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2534 return GetBuffer().XYToPosition(x
, y
);
2537 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2539 return GetBuffer().PositionToXY(pos
, x
, y
);
2542 // ----------------------------------------------------------------------------
2544 // ----------------------------------------------------------------------------
2546 void wxRichTextCtrl::ShowPosition(long pos
)
2548 if (!IsPositionVisible(pos
))
2549 ScrollIntoView(pos
-1, WXK_DOWN
);
2552 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2554 return GetBuffer().GetParagraphLength(lineNo
);
2557 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2559 return GetBuffer().GetParagraphText(lineNo
);
2562 // ----------------------------------------------------------------------------
2564 // ----------------------------------------------------------------------------
2566 void wxRichTextCtrl::Undo()
2570 GetCommandProcessor()->Undo();
2574 void wxRichTextCtrl::Redo()
2578 GetCommandProcessor()->Redo();
2582 bool wxRichTextCtrl::CanUndo() const
2584 return GetCommandProcessor()->CanUndo();
2587 bool wxRichTextCtrl::CanRedo() const
2589 return GetCommandProcessor()->CanRedo();
2592 // ----------------------------------------------------------------------------
2593 // implementation details
2594 // ----------------------------------------------------------------------------
2596 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2598 SetValue(event
.GetString());
2599 GetEventHandler()->ProcessEvent(event
);
2602 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2604 // By default, load the first file into the text window.
2605 if (event
.GetNumberOfFiles() > 0)
2607 LoadFile(event
.GetFiles()[0]);
2611 wxSize
wxRichTextCtrl::DoGetBestSize() const
2613 return wxSize(10, 10);
2616 // ----------------------------------------------------------------------------
2617 // standard handlers for standard edit menu events
2618 // ----------------------------------------------------------------------------
2620 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2625 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2630 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2635 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2640 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2645 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2650 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2652 event
.Enable( CanCut() );
2655 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2657 event
.Enable( CanCopy() );
2660 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2662 event
.Enable( CanDeleteSelection() );
2665 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2667 event
.Enable( CanPaste() );
2670 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2672 event
.Enable( CanUndo() );
2673 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2676 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2678 event
.Enable( CanRedo() );
2679 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2682 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2687 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2689 event
.Enable(GetLastPosition() > 0);
2692 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2694 if (event
.GetEventObject() != this)
2702 m_contextMenu
= new wxMenu
;
2703 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2704 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2705 m_contextMenu
->AppendSeparator();
2706 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2707 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2708 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2709 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2710 m_contextMenu
->AppendSeparator();
2711 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2713 PopupMenu(m_contextMenu
);
2717 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2719 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2722 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2724 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2727 // extended style setting operation with flags including:
2728 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2729 // see richtextbuffer.h for more details.
2731 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2733 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2736 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2738 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2741 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2743 return GetBuffer().GetDefaultStyle();
2746 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2748 return GetBuffer().GetStyle(position
, style
);
2751 // get the common set of styles for the range
2752 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2754 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2757 /// Get the content (uncombined) attributes for this position.
2758 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2760 return GetBuffer().GetUncombinedStyle(position
, style
);
2763 /// Set font, and also the buffer attributes
2764 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2766 wxControl::SetFont(font
);
2768 wxTextAttr attr
= GetBuffer().GetAttributes();
2770 GetBuffer().SetBasicStyle(attr
);
2772 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2778 /// Transform logical to physical
2779 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2782 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2787 /// Transform physical to logical
2788 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2791 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2796 /// Position the caret
2797 void wxRichTextCtrl::PositionCaret()
2802 //wxLogDebug(wxT("PositionCaret"));
2805 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2807 wxPoint newPt
= caretRect
.GetPosition();
2808 wxSize newSz
= caretRect
.GetSize();
2809 wxPoint pt
= GetPhysicalPoint(newPt
);
2810 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2813 if (GetCaret()->GetSize() != newSz
)
2814 GetCaret()->SetSize(newSz
);
2815 GetCaret()->Move(pt
);
2821 /// Get the caret height and position for the given character position
2822 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2824 wxClientDC
dc(this);
2825 dc
.SetFont(GetFont());
2832 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2834 // Caret height can't be zero
2836 height
= dc
.GetCharHeight();
2838 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2845 /// Gets the line for the visible caret position. If the caret is
2846 /// shown at the very end of the line, it means the next character is actually
2847 /// on the following line. So let's get the line we're expecting to find
2848 /// if this is the case.
2849 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2851 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2852 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2855 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2856 if (caretPosition
== lineRange
.GetStart()-1 &&
2857 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2859 if (!m_caretAtLineStart
)
2860 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2867 /// Move the caret to the given character position
2868 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2870 if (GetBuffer().GetDirty())
2873 if (pos
<= GetBuffer().GetRange().GetEnd())
2875 SetCaretPosition(pos
, showAtLineStart
);
2885 /// Layout the buffer: which we must do before certain operations, such as
2886 /// setting the caret position.
2887 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2889 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2891 wxRect
availableSpace(GetClientSize());
2892 if (availableSpace
.width
== 0)
2893 availableSpace
.width
= 10;
2894 if (availableSpace
.height
== 0)
2895 availableSpace
.height
= 10;
2897 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2898 if (onlyVisibleRect
)
2900 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2901 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2904 wxClientDC
dc(this);
2905 dc
.SetFont(GetFont());
2909 GetBuffer().Defragment();
2910 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2911 GetBuffer().Layout(dc
, availableSpace
, flags
);
2912 GetBuffer().SetDirty(false);
2921 /// Is all of the selection bold?
2922 bool wxRichTextCtrl::IsSelectionBold()
2927 wxRichTextRange range
= GetSelectionRange();
2928 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2929 attr
.SetFontWeight(wxBOLD
);
2931 return HasCharacterAttributes(range
, attr
);
2935 // If no selection, then we need to combine current style with default style
2936 // to see what the effect would be if we started typing.
2938 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2940 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2941 if (GetStyle(pos
, attr
))
2943 if (IsDefaultStyleShowing())
2944 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2945 return attr
.GetFontWeight() == wxBOLD
;
2951 /// Is all of the selection italics?
2952 bool wxRichTextCtrl::IsSelectionItalics()
2956 wxRichTextRange range
= GetSelectionRange();
2958 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2959 attr
.SetFontStyle(wxITALIC
);
2961 return HasCharacterAttributes(range
, attr
);
2965 // If no selection, then we need to combine current style with default style
2966 // to see what the effect would be if we started typing.
2968 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2970 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2971 if (GetStyle(pos
, attr
))
2973 if (IsDefaultStyleShowing())
2974 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2975 return attr
.GetFontStyle() == wxITALIC
;
2981 /// Is all of the selection underlined?
2982 bool wxRichTextCtrl::IsSelectionUnderlined()
2986 wxRichTextRange range
= GetSelectionRange();
2988 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2989 attr
.SetFontUnderlined(true);
2991 return HasCharacterAttributes(range
, attr
);
2995 // If no selection, then we need to combine current style with default style
2996 // to see what the effect would be if we started typing.
2998 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2999 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3001 if (GetStyle(pos
, attr
))
3003 if (IsDefaultStyleShowing())
3004 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3005 return attr
.GetFontUnderlined();
3011 /// Apply bold to the selection
3012 bool wxRichTextCtrl::ApplyBoldToSelection()
3015 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3016 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
3019 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3022 wxRichTextAttr current
= GetDefaultStyleEx();
3023 current
.Apply(attr
);
3024 SetAndShowDefaultStyle(current
);
3029 /// Apply italic to the selection
3030 bool wxRichTextCtrl::ApplyItalicToSelection()
3033 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3034 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
3037 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3040 wxRichTextAttr current
= GetDefaultStyleEx();
3041 current
.Apply(attr
);
3042 SetAndShowDefaultStyle(current
);
3047 /// Apply underline to the selection
3048 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3051 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3052 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3055 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3058 wxRichTextAttr current
= GetDefaultStyleEx();
3059 current
.Apply(attr
);
3060 SetAndShowDefaultStyle(current
);
3065 /// Is all of the selection aligned according to the specified flag?
3066 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3068 wxRichTextRange range
;
3070 range
= GetSelectionRange();
3072 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3075 attr
.SetAlignment(alignment
);
3077 return HasParagraphAttributes(range
, attr
);
3080 /// Apply alignment to the selection
3081 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3084 attr
.SetAlignment(alignment
);
3086 return SetStyle(GetSelectionRange(), attr
);
3089 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3091 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3096 /// Apply a named style to the selection
3097 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3099 // Flags are defined within each definition, so only certain
3100 // attributes are applied.
3101 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3103 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3105 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3107 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3109 wxRichTextRange range
;
3112 range
= GetSelectionRange();
3115 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3116 range
= wxRichTextRange(pos
, pos
+1);
3119 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3122 // Make sure the attr has the style name
3123 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3125 attr
.SetParagraphStyleName(def
->GetName());
3127 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3128 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3129 // to change its style independently.
3130 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3133 attr
.SetCharacterStyleName(def
->GetName());
3136 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3139 wxRichTextAttr current
= GetDefaultStyleEx();
3140 current
.Apply(attr
);
3141 SetAndShowDefaultStyle(current
);
3146 /// Apply the style sheet to the buffer, for example if the styles have changed.
3147 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3150 styleSheet
= GetBuffer().GetStyleSheet();
3154 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3156 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3164 /// Sets the default style to the style under the cursor
3165 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3168 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3170 // If at the start of a paragraph, use the next position.
3171 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3173 if (GetUncombinedStyle(pos
, attr
))
3175 SetDefaultStyle(attr
);
3182 /// Returns the first visible position in the current view
3183 long wxRichTextCtrl::GetFirstVisiblePosition() const
3185 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3187 return line
->GetAbsoluteRange().GetStart();
3192 /// Get the first visible point in the window
3193 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3196 int startXUnits
, startYUnits
;
3198 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3199 GetViewStart(& startXUnits
, & startYUnits
);
3201 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3204 /// The adjusted caret position is the character position adjusted to take
3205 /// into account whether we're at the start of a paragraph, in which case
3206 /// style information should be taken from the next position, not current one.
3207 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3209 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3211 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3216 /// Get/set the selection range in character positions. -1, -1 means no selection.
3217 /// The range is in API convention, i.e. a single character selection is denoted
3219 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3221 wxRichTextRange range
= GetInternalSelectionRange();
3222 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3223 range
.SetEnd(range
.GetEnd() + 1);
3227 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3229 wxRichTextRange
range1(range
);
3230 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3231 range1
.SetEnd(range1
.GetEnd() - 1);
3233 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3235 SetInternalSelectionRange(range1
);
3239 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3241 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3244 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3246 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3249 /// Clear list for given range
3250 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3252 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3255 /// Number/renumber any list elements in the given range
3256 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3258 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3261 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3263 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3266 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3267 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3269 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3272 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3274 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3277 /// Deletes the content in the given range
3278 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3280 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3283 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3285 if (sm_availableFontNames
.GetCount() == 0)
3287 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3288 sm_availableFontNames
.Sort();
3290 return sm_availableFontNames
;
3293 void wxRichTextCtrl::ClearAvailableFontNames()
3295 sm_availableFontNames
.Clear();
3298 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3300 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3302 wxTextAttrEx basicStyle
= GetBasicStyle();
3303 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3304 SetBasicStyle(basicStyle
);
3305 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3310 // Refresh the area affected by a selection change
3311 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3313 // Calculate the refresh rectangle - just the affected lines
3314 long firstPos
, lastPos
;
3315 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3317 firstPos
= newSelection
.GetStart();
3318 lastPos
= newSelection
.GetEnd();
3320 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3322 firstPos
= oldSelection
.GetStart();
3323 lastPos
= oldSelection
.GetEnd();
3325 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3331 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3332 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3335 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3336 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3338 if (firstLine
&& lastLine
)
3340 wxSize clientSize
= GetClientSize();
3341 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3342 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3345 pt1
.y
= wxMax(0, pt1
.y
);
3347 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3349 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3350 RefreshRect(rect
, false);
3358 #if wxRICHTEXT_USE_OWN_CARET
3360 // ----------------------------------------------------------------------------
3361 // initialization and destruction
3362 // ----------------------------------------------------------------------------
3364 void wxRichTextCaret::Init()
3370 m_richTextCtrl
= NULL
;
3371 m_needsUpdate
= false;
3374 wxRichTextCaret::~wxRichTextCaret()
3378 // ----------------------------------------------------------------------------
3379 // showing/hiding/moving the caret (base class interface)
3380 // ----------------------------------------------------------------------------
3382 void wxRichTextCaret::DoShow()
3387 void wxRichTextCaret::DoHide()
3392 void wxRichTextCaret::DoMove()
3398 if (m_xOld
!= -1 && m_yOld
!= -1)
3402 wxRect
rect(GetPosition(), GetSize());
3403 m_richTextCtrl
->RefreshRect(rect
, false);
3412 void wxRichTextCaret::DoSize()
3414 int countVisible
= m_countVisible
;
3415 if (countVisible
> 0)
3421 if (countVisible
> 0)
3423 m_countVisible
= countVisible
;
3428 // ----------------------------------------------------------------------------
3429 // handling the focus
3430 // ----------------------------------------------------------------------------
3432 void wxRichTextCaret::OnSetFocus()
3440 void wxRichTextCaret::OnKillFocus()
3445 // ----------------------------------------------------------------------------
3446 // drawing the caret
3447 // ----------------------------------------------------------------------------
3449 void wxRichTextCaret::Refresh()
3453 wxRect
rect(GetPosition(), GetSize());
3454 m_richTextCtrl
->RefreshRect(rect
, false);
3458 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3460 dc
->SetPen( *wxBLACK_PEN
);
3462 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3463 dc
->SetPen(*wxBLACK_PEN
);
3465 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3466 // done under wxGTK - no idea why
3467 //dc->SetLogicalFunction(wxINVERT);
3469 wxPoint
pt(m_x
, m_y
);
3473 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3475 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3478 // wxRICHTEXT_USE_OWN_CARET