1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
29 #include "wx/textfile.h"
31 #include "wx/filename.h"
32 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 #include "wx/fontenum.h"
37 // DLL options compatibility check:
39 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
)
58 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
)
60 #if wxRICHTEXT_USE_OWN_CARET
65 * This implements a non-flashing cursor in case there
66 * are platform-specific problems with the generic caret.
67 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
70 class wxRichTextCaret
: public wxCaret
75 // default - use Create()
76 wxRichTextCaret() { Init(); }
77 // creates a block caret associated with the given window
78 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
79 : wxCaret(window
, width
, height
) { Init(); m_richTextCtrl
= window
; }
80 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
81 : wxCaret(window
, size
) { Init(); m_richTextCtrl
= window
; }
83 virtual ~wxRichTextCaret();
88 // called by wxWindow (not using the event tables)
89 virtual void OnSetFocus();
90 virtual void OnKillFocus();
92 // draw the caret on the given DC
93 void DoDraw(wxDC
*dc
);
95 // get the visible count
96 int GetVisibleCount() const { return m_countVisible
; }
98 // delay repositioning
99 bool GetNeedsUpdate() const { return m_needsUpdate
; }
100 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
103 virtual void DoShow();
104 virtual void DoHide();
105 virtual void DoMove();
106 virtual void DoSize();
116 bool m_hasFocus
; // true => our window has focus
117 bool m_needsUpdate
; // must be repositioned
119 wxRichTextCtrl
* m_richTextCtrl
;
123 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
125 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
127 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
128 EVT_PAINT(wxRichTextCtrl::OnPaint
)
129 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
130 EVT_IDLE(wxRichTextCtrl::OnIdle
)
131 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
132 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
133 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
134 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
135 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
136 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
137 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
138 EVT_CHAR(wxRichTextCtrl::OnChar
)
139 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
140 EVT_SIZE(wxRichTextCtrl::OnSize
)
141 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
142 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
143 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
144 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
145 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
147 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
148 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
150 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
151 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
153 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
154 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
156 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
157 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
159 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
160 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
162 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
163 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
165 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
166 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
173 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
175 wxRichTextCtrl::wxRichTextCtrl()
176 : wxScrollHelper(this)
181 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
183 const wxString
& value
,
187 const wxValidator
& validator
,
188 const wxString
& name
)
189 : wxScrollHelper(this)
192 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
196 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
197 const wxValidator
& validator
, const wxString
& name
)
199 if (!wxControl::Create(parent
, id
, pos
, size
,
200 style
|wxFULL_REPAINT_ON_RESIZE
,
206 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
209 if (style
& wxTE_READONLY
)
212 // The base attributes must all have default values
213 wxTextAttr attributes
;
214 attributes
.SetFont(GetFont());
215 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
216 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
217 attributes
.SetLineSpacing(10);
218 attributes
.SetParagraphSpacingAfter(10);
219 attributes
.SetParagraphSpacingBefore(0);
221 SetBasicStyle(attributes
);
223 // The default attributes will be merged with base attributes, so
224 // can be empty to begin with
225 wxTextAttr defaultAttributes
;
226 SetDefaultStyle(defaultAttributes
);
228 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
229 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
232 GetBuffer().SetRichTextCtrl(this);
234 #if wxRICHTEXT_USE_OWN_CARET
235 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
237 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
240 // Tell the sizers to use the given or best size
241 SetInitialSize(size
);
243 #if wxRICHTEXT_BUFFERED_PAINTING
245 RecreateBuffer(size
);
248 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
249 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
251 SetCursor(m_textCursor
);
253 if (!value
.IsEmpty())
256 GetBuffer().AddEventHandler(this);
259 wxAcceleratorEntry entries
[4];
261 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
262 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
263 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
264 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
266 wxAcceleratorTable
accel(4, entries
);
267 SetAcceleratorTable(accel
);
272 wxRichTextCtrl::~wxRichTextCtrl()
274 GetBuffer().RemoveEventHandler(this);
276 delete m_contextMenu
;
279 /// Member initialisation
280 void wxRichTextCtrl::Init()
282 m_contextMenu
= NULL
;
284 m_caretPosition
= -1;
285 m_selectionRange
.SetRange(-2, -2);
286 m_selectionAnchor
= -2;
288 m_caretAtLineStart
= false;
290 m_fullLayoutRequired
= false;
291 m_fullLayoutTime
= 0;
292 m_fullLayoutSavedPosition
= 0;
293 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
294 m_caretPositionForDefaultStyle
= -2;
297 void wxRichTextCtrl::DoThaw()
299 if (GetBuffer().GetDirty())
308 void wxRichTextCtrl::Clear()
310 m_buffer
.ResetAndClearCommands();
311 m_buffer
.SetDirty(true);
312 m_caretPosition
= -1;
313 m_caretPositionForDefaultStyle
= -2;
314 m_caretAtLineStart
= false;
315 m_selectionRange
.SetRange(-2, -2);
325 wxTextCtrl::SendTextUpdatedEvent(this);
329 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
331 #if !wxRICHTEXT_USE_OWN_CARET
332 if (GetCaret() && !IsFrozen())
337 #if wxRICHTEXT_BUFFERED_PAINTING
338 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
348 dc
.SetFont(GetFont());
350 // Paint the background
353 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
355 wxRect
drawingArea(GetUpdateRegion().GetBox());
356 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
358 wxRect
availableSpace(GetClientSize());
359 if (GetBuffer().GetDirty())
361 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
362 GetBuffer().SetDirty(false);
366 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
367 #if wxRICHTEXT_USE_OWN_CARET
368 if (GetCaret()->IsVisible())
370 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
375 #if !wxRICHTEXT_USE_OWN_CARET
382 // Empty implementation, to prevent flicker
383 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
387 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
391 #if !wxRICHTEXT_USE_OWN_CARET
397 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
398 // Work around dropouts when control is focused
406 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
411 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
412 // Work around dropouts when control is focused
420 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
426 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
432 dc
.SetFont(GetFont());
435 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
437 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
439 m_dragStart
= event
.GetLogicalPosition(dc
);
443 bool caretAtLineStart
= false;
445 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
447 // If we're at the start of a line (but not first in para)
448 // then we should keep the caret showing at the start of the line
449 // by showing the m_caretAtLineStart flag.
450 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
451 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
453 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
454 caretAtLineStart
= true;
458 long oldCaretPos
= m_caretPosition
;
460 MoveCaret(position
, caretAtLineStart
);
461 SetDefaultStyleToCursorStyle();
463 if (event
.ShiftDown())
465 if (m_selectionRange
.GetStart() == -2)
466 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
468 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
478 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
483 if (GetCapture() == this)
486 // See if we clicked on a URL
489 dc
.SetFont(GetFont());
492 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
493 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
495 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
497 wxRichTextEvent
cmdEvent(
498 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
500 cmdEvent
.SetEventObject(this);
501 cmdEvent
.SetPosition(m_caretPosition
+1);
503 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
506 if (GetStyle(position
, attr
))
508 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
510 wxString urlTarget
= attr
.GetURL();
511 if (!urlTarget
.IsEmpty())
513 wxMouseEvent
mouseEvent(event
);
515 long startPos
= 0, endPos
= 0;
516 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
519 startPos
= obj
->GetRange().GetStart();
520 endPos
= obj
->GetRange().GetEnd();
523 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
524 InitCommandEvent(urlEvent
);
526 urlEvent
.SetString(urlTarget
);
528 GetEventHandler()->ProcessEvent(urlEvent
);
538 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
542 dc
.SetFont(GetFont());
545 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
546 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
548 // See if we need to change the cursor
551 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
554 if (GetStyle(position
, attr
))
556 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
558 SetCursor(m_urlCursor
);
560 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
562 SetCursor(m_textCursor
);
567 SetCursor(m_textCursor
);
570 if (!event
.Dragging())
576 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
578 // TODO: test closeness
580 bool caretAtLineStart
= false;
582 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
584 // If we're at the start of a line (but not first in para)
585 // then we should keep the caret showing at the start of the line
586 // by showing the m_caretAtLineStart flag.
587 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
588 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
590 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
591 caretAtLineStart
= true;
595 if (m_caretPosition
!= position
)
597 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
599 MoveCaret(position
, caretAtLineStart
);
600 SetDefaultStyleToCursorStyle();
606 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
610 wxRichTextEvent
cmdEvent(
611 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
613 cmdEvent
.SetEventObject(this);
614 cmdEvent
.SetPosition(m_caretPosition
+1);
616 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
620 /// Left-double-click
621 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
623 wxRichTextEvent
cmdEvent(
624 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
626 cmdEvent
.SetEventObject(this);
627 cmdEvent
.SetPosition(m_caretPosition
+1);
629 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
631 SelectWord(GetCaretPosition()+1);
636 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
638 wxRichTextEvent
cmdEvent(
639 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
641 cmdEvent
.SetEventObject(this);
642 cmdEvent
.SetPosition(m_caretPosition
+1);
644 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
649 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
653 flags
|= wxRICHTEXT_CTRL_DOWN
;
654 if (event
.ShiftDown())
655 flags
|= wxRICHTEXT_SHIFT_DOWN
;
657 flags
|= wxRICHTEXT_ALT_DOWN
;
659 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
661 if (event
.GetKeyCode() == WXK_LEFT
||
662 event
.GetKeyCode() == WXK_RIGHT
||
663 event
.GetKeyCode() == WXK_UP
||
664 event
.GetKeyCode() == WXK_DOWN
||
665 event
.GetKeyCode() == WXK_HOME
||
666 event
.GetKeyCode() == WXK_PAGEUP
||
667 event
.GetKeyCode() == WXK_PAGEDOWN
||
668 event
.GetKeyCode() == WXK_END
||
670 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
671 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
672 event
.GetKeyCode() == WXK_NUMPAD_UP
||
673 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
674 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
675 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
676 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
677 event
.GetKeyCode() == WXK_NUMPAD_END
)
679 KeyboardNavigate(event
.GetKeyCode(), flags
);
683 long keycode
= event
.GetKeyCode();
743 case WXK_NUMPAD_HOME
:
744 case WXK_NUMPAD_LEFT
:
746 case WXK_NUMPAD_RIGHT
:
747 case WXK_NUMPAD_DOWN
:
748 case WXK_NUMPAD_PAGEUP
:
749 case WXK_NUMPAD_PAGEDOWN
:
751 case WXK_NUMPAD_BEGIN
:
752 case WXK_NUMPAD_INSERT
:
753 case WXK_NUMPAD_DELETE
:
754 case WXK_WINDOWS_LEFT
:
763 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
764 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
766 BeginBatchUndo(_("Delete Text"));
768 long newPos
= m_caretPosition
;
770 DeleteSelectedContent(& newPos
);
772 // Submit range in character positions, which are greater than caret positions,
773 // so subtract 1 for deleted character and add 1 for conversion to character position.
776 bool processed
= false;
779 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
782 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
788 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
793 if (GetLastPosition() == -1)
797 m_caretPosition
= -1;
799 SetDefaultStyleToCursorStyle();
802 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
804 wxRichTextEvent
cmdEvent(
805 wxEVT_COMMAND_RICHTEXT_DELETE
,
807 cmdEvent
.SetEventObject(this);
808 cmdEvent
.SetFlags(flags
);
809 cmdEvent
.SetPosition(m_caretPosition
+1);
810 GetEventHandler()->ProcessEvent(cmdEvent
);
820 // all the other keys modify the controls contents which shouldn't be
821 // possible if we're read-only
828 if (event
.GetKeyCode() == WXK_RETURN
)
830 BeginBatchUndo(_("Insert Text"));
832 long newPos
= m_caretPosition
;
834 DeleteSelectedContent(& newPos
);
836 if (event
.ShiftDown())
839 text
= wxRichTextLineBreakChar
;
840 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
841 m_caretAtLineStart
= true;
845 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
848 SetDefaultStyleToCursorStyle();
850 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
852 wxRichTextEvent
cmdEvent(
853 wxEVT_COMMAND_RICHTEXT_RETURN
,
855 cmdEvent
.SetEventObject(this);
856 cmdEvent
.SetFlags(flags
);
857 cmdEvent
.SetPosition(newPos
+1);
859 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
861 // Generate conventional event
862 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
863 InitCommandEvent(textEvent
);
865 GetEventHandler()->ProcessEvent(textEvent
);
869 else if (event
.GetKeyCode() == WXK_BACK
)
871 BeginBatchUndo(_("Delete Text"));
873 long newPos
= m_caretPosition
;
875 bool processed
= DeleteSelectedContent(& newPos
);
877 // Submit range in character positions, which are greater than caret positions,
878 // so subtract 1 for deleted character and add 1 for conversion to character position.
883 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
886 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
892 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
897 if (GetLastPosition() == -1)
901 m_caretPosition
= -1;
903 SetDefaultStyleToCursorStyle();
906 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
908 wxRichTextEvent
cmdEvent(
909 wxEVT_COMMAND_RICHTEXT_DELETE
,
911 cmdEvent
.SetEventObject(this);
912 cmdEvent
.SetFlags(flags
);
913 cmdEvent
.SetPosition(m_caretPosition
+1);
914 GetEventHandler()->ProcessEvent(cmdEvent
);
918 else if (event
.GetKeyCode() == WXK_DELETE
)
920 BeginBatchUndo(_("Delete Text"));
922 long newPos
= m_caretPosition
;
924 bool processed
= DeleteSelectedContent(& newPos
);
926 // Submit range in character positions, which are greater than caret positions,
927 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
931 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
932 if (pos
!= -1 && (pos
> newPos
))
934 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
940 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
945 if (GetLastPosition() == -1)
949 m_caretPosition
= -1;
951 SetDefaultStyleToCursorStyle();
954 wxRichTextEvent
cmdEvent(
955 wxEVT_COMMAND_RICHTEXT_DELETE
,
957 cmdEvent
.SetEventObject(this);
958 cmdEvent
.SetFlags(flags
);
959 cmdEvent
.SetPosition(m_caretPosition
+1);
960 GetEventHandler()->ProcessEvent(cmdEvent
);
966 long keycode
= event
.GetKeyCode();
980 if (event
.CmdDown() || event
.AltDown())
987 wxRichTextEvent
cmdEvent(
988 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
990 cmdEvent
.SetEventObject(this);
991 cmdEvent
.SetFlags(flags
);
993 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
995 cmdEvent
.SetCharacter((wxChar
) keycode
);
997 cmdEvent
.SetPosition(m_caretPosition
+1);
999 if (keycode
== wxT('\t'))
1001 // See if we need to promote or demote the selection or paragraph at the cursor
1002 // position, instead of inserting a tab.
1003 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1004 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1005 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1007 wxRichTextRange range
;
1009 range
= GetSelectionRange();
1011 range
= para
->GetRange().FromInternal();
1013 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1015 PromoteList(promoteBy
, range
, NULL
);
1017 GetEventHandler()->ProcessEvent(cmdEvent
);
1023 BeginBatchUndo(_("Insert Text"));
1025 long newPos
= m_caretPosition
;
1026 DeleteSelectedContent(& newPos
);
1029 wxString str
= event
.GetUnicodeKey();
1031 wxString str
= (wxChar
) event
.GetKeyCode();
1033 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1037 SetDefaultStyleToCursorStyle();
1038 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1040 GetEventHandler()->ProcessEvent(cmdEvent
);
1048 /// Delete content if there is a selection, e.g. when pressing a key.
1049 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1053 long pos
= m_selectionRange
.GetStart();
1054 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1055 m_selectionRange
.SetRange(-2, -2);
1065 /// Keyboard navigation
1069 Left: left one character
1070 Right: right one character
1073 Ctrl-Left: left one word
1074 Ctrl-Right: right one word
1075 Ctrl-Up: previous paragraph start
1076 Ctrl-Down: next start of paragraph
1079 Ctrl-Home: start of document
1080 Ctrl-End: end of document
1081 Page-Up: Up a screen
1082 Page-Down: Down a screen
1086 Ctrl-Alt-PgUp: Start of window
1087 Ctrl-Alt-PgDn: End of window
1088 F8: Start selection mode
1089 Esc: End selection mode
1091 Adding Shift does the above but starts/extends selection.
1096 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1098 bool success
= false;
1100 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1102 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1103 success
= WordRight(1, flags
);
1105 success
= MoveRight(1, flags
);
1107 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1109 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1110 success
= WordLeft(1, flags
);
1112 success
= MoveLeft(1, flags
);
1114 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1116 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1117 success
= MoveToParagraphStart(flags
);
1119 success
= MoveUp(1, flags
);
1121 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1123 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1124 success
= MoveToParagraphEnd(flags
);
1126 success
= MoveDown(1, flags
);
1128 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1130 success
= PageUp(1, flags
);
1132 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1134 success
= PageDown(1, flags
);
1136 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1138 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1139 success
= MoveHome(flags
);
1141 success
= MoveToLineStart(flags
);
1143 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1145 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1146 success
= MoveEnd(flags
);
1148 success
= MoveToLineEnd(flags
);
1153 ScrollIntoView(m_caretPosition
, keyCode
);
1154 SetDefaultStyleToCursorStyle();
1160 /// Extend the selection. Selections are in caret positions.
1161 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1163 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1165 if (oldPos
== newPos
)
1168 wxRichTextRange oldSelection
= m_selectionRange
;
1170 // If not currently selecting, start selecting
1171 if (m_selectionRange
.GetStart() == -2)
1173 m_selectionAnchor
= oldPos
;
1175 if (oldPos
> newPos
)
1176 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1178 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1182 // Always ensure that the selection range start is greater than
1184 if (newPos
> m_selectionAnchor
)
1185 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1186 else if (newPos
== m_selectionAnchor
)
1187 m_selectionRange
= wxRichTextRange(-2, -2);
1189 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1192 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1194 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1196 wxLogDebug(wxT("Strange selection range"));
1205 /// Scroll into view, returning true if we scrolled.
1206 /// This takes a _caret_ position.
1207 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1209 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1215 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1217 int startXUnits
, startYUnits
;
1218 GetViewStart(& startXUnits
, & startYUnits
);
1219 int startY
= startYUnits
* ppuY
;
1222 GetVirtualSize(& sx
, & sy
);
1228 wxRect rect
= line
->GetRect();
1230 bool scrolled
= false;
1232 wxSize clientSize
= GetClientSize();
1235 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1236 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1237 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1238 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1240 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1242 // Make it scroll so this item is at the bottom
1244 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1245 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1247 // If we're still off the screen, scroll another line down
1248 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1251 if (startYUnits
!= yUnits
)
1253 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1257 else if (rect
.y
< startY
)
1259 // Make it scroll so this item is at the top
1262 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1264 if (startYUnits
!= yUnits
)
1266 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1272 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1273 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1274 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1275 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1277 if (rect
.y
< startY
)
1279 // Make it scroll so this item is at the top
1282 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1284 if (startYUnits
!= yUnits
)
1286 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1290 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1292 // Make it scroll so this item is at the bottom
1294 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1295 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1297 // If we're still off the screen, scroll another line down
1298 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1301 if (startYUnits
!= yUnits
)
1303 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1309 #if !wxRICHTEXT_USE_OWN_CARET
1317 /// Is the given position visible on the screen?
1318 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1320 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1326 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1329 GetViewStart(& startX
, & startY
);
1331 startY
= startY
* ppuY
;
1333 wxRect rect
= line
->GetRect();
1334 wxSize clientSize
= GetClientSize();
1336 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1339 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1341 m_caretPosition
= position
;
1342 m_caretAtLineStart
= showAtLineStart
;
1345 /// Move caret one visual step forward: this may mean setting a flag
1346 /// and keeping the same position if we're going from the end of one line
1347 /// to the start of the next, which may be the exact same caret position.
1348 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1350 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1352 // Only do the check if we're not at the end of the paragraph (where things work OK
1354 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1356 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1360 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1362 // We're at the end of a line. See whether we need to
1363 // stay at the same actual caret position but change visual
1364 // position, or not.
1365 if (oldPosition
== lineRange
.GetEnd())
1367 if (m_caretAtLineStart
)
1369 // We're already at the start of the line, so actually move on now.
1370 m_caretPosition
= oldPosition
+ 1;
1371 m_caretAtLineStart
= false;
1375 // We're showing at the end of the line, so keep to
1376 // the same position but indicate that we're to show
1377 // at the start of the next line.
1378 m_caretPosition
= oldPosition
;
1379 m_caretAtLineStart
= true;
1381 SetDefaultStyleToCursorStyle();
1387 SetDefaultStyleToCursorStyle();
1390 /// Move caret one visual step backward: this may mean setting a flag
1391 /// and keeping the same position if we're going from the end of one line
1392 /// to the start of the next, which may be the exact same caret position.
1393 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1395 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1397 // Only do the check if we're not at the start of the paragraph (where things work OK
1399 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1401 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1405 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1407 // We're at the start of a line. See whether we need to
1408 // stay at the same actual caret position but change visual
1409 // position, or not.
1410 if (oldPosition
== lineRange
.GetStart())
1412 m_caretPosition
= oldPosition
-1;
1413 m_caretAtLineStart
= true;
1416 else if (oldPosition
== lineRange
.GetEnd())
1418 if (m_caretAtLineStart
)
1420 // We're at the start of the line, so keep the same caret position
1421 // but clear the start-of-line flag.
1422 m_caretPosition
= oldPosition
;
1423 m_caretAtLineStart
= false;
1427 // We're showing at the end of the line, so go back
1428 // to the previous character position.
1429 m_caretPosition
= oldPosition
- 1;
1431 SetDefaultStyleToCursorStyle();
1437 SetDefaultStyleToCursorStyle();
1441 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1443 long endPos
= GetBuffer().GetRange().GetEnd();
1445 if (m_caretPosition
+ noPositions
< endPos
)
1447 long oldPos
= m_caretPosition
;
1448 long newPos
= m_caretPosition
+ noPositions
;
1450 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1454 // Determine by looking at oldPos and m_caretPosition whether
1455 // we moved from the end of a line to the start of the next line, in which case
1456 // we want to adjust the caret position such that it is positioned at the
1457 // start of the next line, rather than jumping past the first character of the
1459 if (noPositions
== 1 && !extendSel
)
1460 MoveCaretForward(oldPos
);
1462 SetCaretPosition(newPos
);
1465 SetDefaultStyleToCursorStyle();
1474 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1478 if (m_caretPosition
> startPos
- noPositions
+ 1)
1480 long oldPos
= m_caretPosition
;
1481 long newPos
= m_caretPosition
- noPositions
;
1482 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1486 if (noPositions
== 1 && !extendSel
)
1487 MoveCaretBack(oldPos
);
1489 SetCaretPosition(newPos
);
1492 SetDefaultStyleToCursorStyle();
1501 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1503 return MoveDown(- noLines
, flags
);
1507 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1512 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1513 wxPoint pt
= GetCaret()->GetPosition();
1514 long newLine
= lineNumber
+ noLines
;
1516 if (lineNumber
!= -1)
1520 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1522 if (newLine
> lastLine
)
1532 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1535 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1541 wxClientDC
dc(this);
1543 dc
.SetFont(GetFont());
1545 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1547 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1549 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1550 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1551 // so we view the caret at the start of the line.
1552 bool caretLineStart
= false;
1553 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1555 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1556 wxRichTextRange lineRange
;
1558 lineRange
= thisLine
->GetAbsoluteRange();
1560 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1563 caretLineStart
= true;
1567 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1568 if (para
&& para
->GetRange().GetStart() == newPos
)
1573 long newSelEnd
= newPos
;
1575 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1579 SetCaretPosition(newPos
, caretLineStart
);
1581 SetDefaultStyleToCursorStyle();
1589 /// Move to the end of the paragraph
1590 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1592 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1595 long newPos
= para
->GetRange().GetEnd() - 1;
1596 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1600 SetCaretPosition(newPos
);
1602 SetDefaultStyleToCursorStyle();
1610 /// Move to the start of the paragraph
1611 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1613 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1616 long newPos
= para
->GetRange().GetStart() - 1;
1617 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1621 SetCaretPosition(newPos
);
1623 SetDefaultStyleToCursorStyle();
1631 /// Move to the end of the line
1632 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1634 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1638 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1639 long newPos
= lineRange
.GetEnd();
1640 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1644 SetCaretPosition(newPos
);
1646 SetDefaultStyleToCursorStyle();
1654 /// Move to the start of the line
1655 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1657 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1660 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1661 long newPos
= lineRange
.GetStart()-1;
1663 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1667 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1669 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1671 SetDefaultStyleToCursorStyle();
1679 /// Move to the start of the buffer
1680 bool wxRichTextCtrl::MoveHome(int flags
)
1682 if (m_caretPosition
!= -1)
1684 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1688 SetCaretPosition(-1);
1690 SetDefaultStyleToCursorStyle();
1698 /// Move to the end of the buffer
1699 bool wxRichTextCtrl::MoveEnd(int flags
)
1701 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1703 if (m_caretPosition
!= endPos
)
1705 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1709 SetCaretPosition(endPos
);
1711 SetDefaultStyleToCursorStyle();
1719 /// Move noPages pages up
1720 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1722 return PageDown(- noPages
, flags
);
1725 /// Move noPages pages down
1726 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1728 // Calculate which line occurs noPages * screen height further down.
1729 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1732 wxSize clientSize
= GetClientSize();
1733 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1735 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1738 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1739 long pos
= lineRange
.GetStart()-1;
1740 if (pos
!= m_caretPosition
)
1742 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1744 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1748 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1750 SetDefaultStyleToCursorStyle();
1760 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1762 return str
== wxT(" ") || str
== wxT("\t");
1765 // Finds the caret position for the next word
1766 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1768 long endPos
= GetBuffer().GetRange().GetEnd();
1772 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1774 // First skip current text to space
1775 while (i
< endPos
&& i
> -1)
1777 // i is in character, not caret positions
1778 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1779 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1780 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1784 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1791 while (i
< endPos
&& i
> -1)
1793 // i is in character, not caret positions
1794 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1795 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1796 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1797 return wxMax(-1, i
);
1799 if (text
.empty()) // End of paragraph, or maybe an image
1800 return wxMax(-1, i
- 1);
1801 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1805 // Convert to caret position
1806 return wxMax(-1, i
- 1);
1815 long i
= m_caretPosition
;
1817 // First skip white space
1818 while (i
< endPos
&& i
> -1)
1820 // i is in character, not caret positions
1821 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1822 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1824 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1826 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1831 // Next skip current text to space
1832 while (i
< endPos
&& i
> -1)
1834 // i is in character, not caret positions
1835 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1836 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1837 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1840 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1853 /// Move n words left
1854 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1856 long pos
= FindNextWordPosition(-1);
1857 if (pos
!= m_caretPosition
)
1859 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1861 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1865 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1867 SetDefaultStyleToCursorStyle();
1875 /// Move n words right
1876 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1878 long pos
= FindNextWordPosition(1);
1879 if (pos
!= m_caretPosition
)
1881 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1883 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1887 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1889 SetDefaultStyleToCursorStyle();
1898 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1900 // Only do sizing optimization for large buffers
1901 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1903 m_fullLayoutRequired
= true;
1904 m_fullLayoutTime
= wxGetLocalTimeMillis();
1905 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1906 LayoutContent(true /* onlyVisibleRect */);
1909 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1911 #if wxRICHTEXT_BUFFERED_PAINTING
1919 /// Idle-time processing
1920 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1922 #if wxRICHTEXT_USE_OWN_CARET
1923 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1925 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1931 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1933 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1935 m_fullLayoutRequired
= false;
1936 m_fullLayoutTime
= 0;
1937 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1938 ShowPosition(m_fullLayoutSavedPosition
);
1942 if (m_caretPositionForDefaultStyle
!= -2)
1944 // If the caret position has changed, no longer reflect the default style
1946 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1947 m_caretPositionForDefaultStyle
= -2;
1954 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1956 #if wxRICHTEXT_USE_OWN_CARET
1957 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1960 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1967 /// Set up scrollbars, e.g. after a resize
1968 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1973 if (GetBuffer().IsEmpty())
1975 SetScrollbars(0, 0, 0, 0, 0, 0);
1979 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1980 // of pixels. See e.g. wxVScrolledWindow for ideas.
1981 int pixelsPerUnit
= 5;
1982 wxSize clientSize
= GetClientSize();
1984 int maxHeight
= GetBuffer().GetCachedSize().y
;
1986 // Round up so we have at least maxHeight pixels
1987 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1989 int startX
= 0, startY
= 0;
1991 GetViewStart(& startX
, & startY
);
1993 int maxPositionX
= 0;
1994 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1996 int newStartX
= wxMin(maxPositionX
, startX
);
1997 int newStartY
= wxMin(maxPositionY
, startY
);
1999 int oldPPUX
, oldPPUY
;
2000 int oldStartX
, oldStartY
;
2001 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2002 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2003 GetViewStart(& oldStartX
, & oldStartY
);
2004 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2006 oldVirtualSizeY
/= oldPPUY
;
2008 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2011 // Don't set scrollbars if there were none before, and there will be none now.
2012 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2015 // Move to previous scroll position if
2017 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2020 /// Paint the background
2021 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2023 wxColour backgroundColour
= GetBackgroundColour();
2024 if (!backgroundColour
.Ok())
2025 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2027 // Clear the background
2028 dc
.SetBrush(wxBrush(backgroundColour
));
2029 dc
.SetPen(*wxTRANSPARENT_PEN
);
2030 wxRect
windowRect(GetClientSize());
2031 windowRect
.x
-= 2; windowRect
.y
-= 2;
2032 windowRect
.width
+= 4; windowRect
.height
+= 4;
2034 // We need to shift the rectangle to take into account
2035 // scrolling. Converting device to logical coordinates.
2036 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2037 dc
.DrawRectangle(windowRect
);
2040 #if wxRICHTEXT_BUFFERED_PAINTING
2041 /// Recreate buffer bitmap if necessary
2042 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2045 if (sz
== wxDefaultSize
)
2046 sz
= GetClientSize();
2048 if (sz
.x
< 1 || sz
.y
< 1)
2051 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2052 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2053 return m_bufferBitmap
.Ok();
2057 // ----------------------------------------------------------------------------
2058 // file IO functions
2059 // ----------------------------------------------------------------------------
2061 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2063 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2065 m_filename
= filename
;
2068 SetInsertionPoint(0);
2071 SetupScrollbars(true);
2073 wxTextCtrl::SendTextUpdatedEvent(this);
2079 wxLogError(_("File couldn't be loaded."));
2085 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2087 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2089 m_filename
= filename
;
2096 wxLogError(_("The text couldn't be saved."));
2101 // ----------------------------------------------------------------------------
2102 // wxRichTextCtrl specific functionality
2103 // ----------------------------------------------------------------------------
2105 /// Add a new paragraph of text to the end of the buffer
2106 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2108 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2114 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2116 wxRichTextRange range
= GetBuffer().AddImage(image
);
2121 // ----------------------------------------------------------------------------
2122 // selection and ranges
2123 // ----------------------------------------------------------------------------
2125 void wxRichTextCtrl::SelectAll()
2127 SetSelection(0, GetLastPosition()+1);
2128 m_selectionAnchor
= -1;
2132 void wxRichTextCtrl::SelectNone()
2134 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2136 wxRichTextRange oldSelection
= m_selectionRange
;
2138 m_selectionRange
= wxRichTextRange(-2, -2);
2140 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2142 m_selectionAnchor
= -2;
2145 static bool wxIsWordDelimiter(const wxString
& text
)
2147 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2150 /// Select the word at the given character position
2151 bool wxRichTextCtrl::SelectWord(long position
)
2153 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2156 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2160 if (position
== para
->GetRange().GetEnd())
2163 long positionStart
= position
;
2164 long positionEnd
= position
;
2166 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2168 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2169 if (wxIsWordDelimiter(text
))
2175 if (positionStart
< para
->GetRange().GetStart())
2176 positionStart
= para
->GetRange().GetStart();
2178 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2180 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2181 if (wxIsWordDelimiter(text
))
2187 if (positionEnd
>= para
->GetRange().GetEnd())
2188 positionEnd
= para
->GetRange().GetEnd();
2190 if (positionEnd
< positionStart
)
2193 SetSelection(positionStart
, positionEnd
+1);
2195 if (positionStart
>= 0)
2197 MoveCaret(positionStart
-1, true);
2198 SetDefaultStyleToCursorStyle();
2204 wxString
wxRichTextCtrl::GetStringSelection() const
2207 GetSelection(&from
, &to
);
2209 return GetRange(from
, to
);
2212 // ----------------------------------------------------------------------------
2214 // ----------------------------------------------------------------------------
2216 wxTextCtrlHitTestResult
2217 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2219 // implement in terms of the other overload as the native ports typically
2220 // can get the position and not (x, y) pair directly (although wxUniv
2221 // directly gets x and y -- and so overrides this method as well)
2223 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2225 if ( rc
!= wxTE_HT_UNKNOWN
)
2227 PositionToXY(pos
, x
, y
);
2233 wxTextCtrlHitTestResult
2234 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2237 wxClientDC
dc((wxRichTextCtrl
*) this);
2238 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2240 // Buffer uses logical position (relative to start of buffer)
2242 wxPoint pt2
= GetLogicalPoint(pt
);
2244 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2246 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2247 return wxTE_HT_BEFORE
;
2248 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2249 return wxTE_HT_BEYOND
;
2250 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2251 return wxTE_HT_ON_TEXT
;
2253 return wxTE_HT_UNKNOWN
;
2256 // ----------------------------------------------------------------------------
2257 // set/get the controls text
2258 // ----------------------------------------------------------------------------
2260 wxString
wxRichTextCtrl::GetValue() const
2262 return GetBuffer().GetText();
2265 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2267 // Public API for range is different from internals
2268 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2271 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2273 // Don't call Clear here, since it always sends a text updated event
2274 m_buffer
.ResetAndClearCommands();
2275 m_buffer
.SetDirty(true);
2276 m_caretPosition
= -1;
2277 m_caretPositionForDefaultStyle
= -2;
2278 m_caretAtLineStart
= false;
2279 m_selectionRange
.SetRange(-2, -2);
2289 if (!value
.IsEmpty())
2291 // Remove empty paragraph
2292 GetBuffer().Clear();
2293 DoWriteText(value
, flags
);
2295 // for compatibility, don't move the cursor when doing SetValue()
2296 SetInsertionPoint(0);
2300 // still send an event for consistency
2301 if (flags
& SetValue_SendEvent
)
2302 wxTextCtrl::SendTextUpdatedEvent(this);
2307 void wxRichTextCtrl::WriteText(const wxString
& value
)
2312 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2314 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2316 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2318 if ( flags
& SetValue_SendEvent
)
2319 wxTextCtrl::SendTextUpdatedEvent(this);
2322 void wxRichTextCtrl::AppendText(const wxString
& text
)
2324 SetInsertionPointEnd();
2329 /// Write an image at the current insertion point
2330 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2332 wxRichTextImageBlock imageBlock
;
2334 wxImage image2
= image
;
2335 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2336 return WriteImage(imageBlock
);
2341 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2343 wxRichTextImageBlock imageBlock
;
2346 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2347 return WriteImage(imageBlock
);
2352 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2354 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2357 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2361 wxRichTextImageBlock imageBlock
;
2363 wxImage image
= bitmap
.ConvertToImage();
2364 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2365 return WriteImage(imageBlock
);
2371 /// Insert a newline (actually paragraph) at the current insertion point.
2372 bool wxRichTextCtrl::Newline()
2374 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2377 /// Insert a line break at the current insertion point.
2378 bool wxRichTextCtrl::LineBreak()
2381 text
= wxRichTextLineBreakChar
;
2382 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2385 // ----------------------------------------------------------------------------
2386 // Clipboard operations
2387 // ----------------------------------------------------------------------------
2389 void wxRichTextCtrl::Copy()
2393 wxRichTextRange range
= GetInternalSelectionRange();
2394 GetBuffer().CopyToClipboard(range
);
2398 void wxRichTextCtrl::Cut()
2402 wxRichTextRange range
= GetInternalSelectionRange();
2403 GetBuffer().CopyToClipboard(range
);
2405 DeleteSelectedContent();
2411 void wxRichTextCtrl::Paste()
2415 BeginBatchUndo(_("Paste"));
2417 long newPos
= m_caretPosition
;
2418 DeleteSelectedContent(& newPos
);
2420 GetBuffer().PasteFromClipboard(newPos
);
2426 void wxRichTextCtrl::DeleteSelection()
2428 if (CanDeleteSelection())
2430 DeleteSelectedContent();
2434 bool wxRichTextCtrl::HasSelection() const
2436 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2439 bool wxRichTextCtrl::CanCopy() const
2441 // Can copy if there's a selection
2442 return HasSelection();
2445 bool wxRichTextCtrl::CanCut() const
2447 return HasSelection() && IsEditable();
2450 bool wxRichTextCtrl::CanPaste() const
2452 if ( !IsEditable() )
2455 return GetBuffer().CanPasteFromClipboard();
2458 bool wxRichTextCtrl::CanDeleteSelection() const
2460 return HasSelection() && IsEditable();
2464 // ----------------------------------------------------------------------------
2466 // ----------------------------------------------------------------------------
2468 void wxRichTextCtrl::SetEditable(bool editable
)
2470 m_editable
= editable
;
2473 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2477 m_caretPosition
= pos
- 1;
2482 void wxRichTextCtrl::SetInsertionPointEnd()
2484 long pos
= GetLastPosition();
2485 SetInsertionPoint(pos
);
2488 long wxRichTextCtrl::GetInsertionPoint() const
2490 return m_caretPosition
+1;
2493 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2495 return GetBuffer().GetRange().GetEnd();
2498 // If the return values from and to are the same, there is no
2500 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2502 *from
= m_selectionRange
.GetStart();
2503 *to
= m_selectionRange
.GetEnd();
2504 if ((*to
) != -1 && (*to
) != -2)
2508 bool wxRichTextCtrl::IsEditable() const
2513 // ----------------------------------------------------------------------------
2515 // ----------------------------------------------------------------------------
2517 void wxRichTextCtrl::SetSelection(long from
, long to
)
2519 // if from and to are both -1, it means (in wxWidgets) that all text should
2521 if ( (from
== -1) && (to
== -1) )
2524 to
= GetLastPosition()+1;
2527 DoSetSelection(from
, to
);
2530 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2538 wxRichTextRange oldSelection
= m_selectionRange
;
2539 m_selectionAnchor
= from
;
2540 m_selectionRange
.SetRange(from
, to
-1);
2542 m_caretPosition
= from
-1;
2544 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2549 // ----------------------------------------------------------------------------
2551 // ----------------------------------------------------------------------------
2553 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2554 const wxString
& value
)
2556 BeginBatchUndo(_("Replace"));
2558 DeleteSelectedContent();
2560 DoWriteText(value
, SetValue_SelectionOnly
);
2565 void wxRichTextCtrl::Remove(long from
, long to
)
2569 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2576 bool wxRichTextCtrl::IsModified() const
2578 return m_buffer
.IsModified();
2581 void wxRichTextCtrl::MarkDirty()
2583 m_buffer
.Modify(true);
2586 void wxRichTextCtrl::DiscardEdits()
2588 m_caretPositionForDefaultStyle
= -2;
2589 m_buffer
.Modify(false);
2590 m_buffer
.GetCommandProcessor()->ClearCommands();
2593 int wxRichTextCtrl::GetNumberOfLines() const
2595 return GetBuffer().GetParagraphCount();
2598 // ----------------------------------------------------------------------------
2599 // Positions <-> coords
2600 // ----------------------------------------------------------------------------
2602 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2604 return GetBuffer().XYToPosition(x
, y
);
2607 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2609 return GetBuffer().PositionToXY(pos
, x
, y
);
2612 // ----------------------------------------------------------------------------
2614 // ----------------------------------------------------------------------------
2616 void wxRichTextCtrl::ShowPosition(long pos
)
2618 if (!IsPositionVisible(pos
))
2619 ScrollIntoView(pos
-1, WXK_DOWN
);
2622 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2624 return GetBuffer().GetParagraphLength(lineNo
);
2627 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2629 return GetBuffer().GetParagraphText(lineNo
);
2632 // ----------------------------------------------------------------------------
2634 // ----------------------------------------------------------------------------
2636 void wxRichTextCtrl::Undo()
2640 GetCommandProcessor()->Undo();
2644 void wxRichTextCtrl::Redo()
2648 GetCommandProcessor()->Redo();
2652 bool wxRichTextCtrl::CanUndo() const
2654 return GetCommandProcessor()->CanUndo();
2657 bool wxRichTextCtrl::CanRedo() const
2659 return GetCommandProcessor()->CanRedo();
2662 // ----------------------------------------------------------------------------
2663 // implementation details
2664 // ----------------------------------------------------------------------------
2666 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2668 SetValue(event
.GetString());
2669 GetEventHandler()->ProcessEvent(event
);
2672 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2674 // By default, load the first file into the text window.
2675 if (event
.GetNumberOfFiles() > 0)
2677 LoadFile(event
.GetFiles()[0]);
2681 wxSize
wxRichTextCtrl::DoGetBestSize() const
2683 return wxSize(10, 10);
2686 // ----------------------------------------------------------------------------
2687 // standard handlers for standard edit menu events
2688 // ----------------------------------------------------------------------------
2690 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2695 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2700 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2705 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2710 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2715 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2720 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2722 event
.Enable( CanCut() );
2725 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2727 event
.Enable( CanCopy() );
2730 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2732 event
.Enable( CanDeleteSelection() );
2735 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2737 event
.Enable( CanPaste() );
2740 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2742 event
.Enable( CanUndo() );
2743 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2746 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2748 event
.Enable( CanRedo() );
2749 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2752 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2757 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2759 event
.Enable(GetLastPosition() > 0);
2762 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2764 if (event
.GetEventObject() != this)
2772 m_contextMenu
= new wxMenu
;
2773 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2774 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2775 m_contextMenu
->AppendSeparator();
2776 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2777 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2778 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2779 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2780 m_contextMenu
->AppendSeparator();
2781 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2783 PopupMenu(m_contextMenu
);
2787 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2789 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2792 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2794 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2797 // extended style setting operation with flags including:
2798 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2799 // see richtextbuffer.h for more details.
2801 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2803 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2806 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2808 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2811 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2813 return GetBuffer().GetDefaultStyle();
2816 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2818 return GetBuffer().GetStyle(position
, style
);
2821 // get the common set of styles for the range
2822 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2824 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2827 /// Get the content (uncombined) attributes for this position.
2828 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2830 return GetBuffer().GetUncombinedStyle(position
, style
);
2833 /// Set font, and also the buffer attributes
2834 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2836 wxControl::SetFont(font
);
2838 wxTextAttr attr
= GetBuffer().GetAttributes();
2840 GetBuffer().SetBasicStyle(attr
);
2842 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2848 /// Transform logical to physical
2849 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2852 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2857 /// Transform physical to logical
2858 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2861 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2866 /// Position the caret
2867 void wxRichTextCtrl::PositionCaret()
2872 //wxLogDebug(wxT("PositionCaret"));
2875 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2877 wxPoint newPt
= caretRect
.GetPosition();
2878 wxSize newSz
= caretRect
.GetSize();
2879 wxPoint pt
= GetPhysicalPoint(newPt
);
2880 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2883 if (GetCaret()->GetSize() != newSz
)
2884 GetCaret()->SetSize(newSz
);
2885 GetCaret()->Move(pt
);
2891 /// Get the caret height and position for the given character position
2892 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2894 wxClientDC
dc(this);
2895 dc
.SetFont(GetFont());
2902 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2904 // Caret height can't be zero
2906 height
= dc
.GetCharHeight();
2908 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2915 /// Gets the line for the visible caret position. If the caret is
2916 /// shown at the very end of the line, it means the next character is actually
2917 /// on the following line. So let's get the line we're expecting to find
2918 /// if this is the case.
2919 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2921 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2922 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2925 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2926 if (caretPosition
== lineRange
.GetStart()-1 &&
2927 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2929 if (!m_caretAtLineStart
)
2930 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2937 /// Move the caret to the given character position
2938 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2940 if (GetBuffer().GetDirty())
2943 if (pos
<= GetBuffer().GetRange().GetEnd())
2945 SetCaretPosition(pos
, showAtLineStart
);
2955 /// Layout the buffer: which we must do before certain operations, such as
2956 /// setting the caret position.
2957 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2959 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2961 wxRect
availableSpace(GetClientSize());
2962 if (availableSpace
.width
== 0)
2963 availableSpace
.width
= 10;
2964 if (availableSpace
.height
== 0)
2965 availableSpace
.height
= 10;
2967 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2968 if (onlyVisibleRect
)
2970 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2971 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2974 wxClientDC
dc(this);
2975 dc
.SetFont(GetFont());
2979 GetBuffer().Defragment();
2980 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2981 GetBuffer().Layout(dc
, availableSpace
, flags
);
2982 GetBuffer().SetDirty(false);
2991 /// Is all of the selection bold?
2992 bool wxRichTextCtrl::IsSelectionBold()
2997 wxRichTextRange range
= GetSelectionRange();
2998 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2999 attr
.SetFontWeight(wxBOLD
);
3001 return HasCharacterAttributes(range
, attr
);
3005 // If no selection, then we need to combine current style with default style
3006 // to see what the effect would be if we started typing.
3008 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3010 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3011 if (GetStyle(pos
, attr
))
3013 if (IsDefaultStyleShowing())
3014 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3015 return attr
.GetFontWeight() == wxBOLD
;
3021 /// Is all of the selection italics?
3022 bool wxRichTextCtrl::IsSelectionItalics()
3026 wxRichTextRange range
= GetSelectionRange();
3028 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3029 attr
.SetFontStyle(wxITALIC
);
3031 return HasCharacterAttributes(range
, attr
);
3035 // If no selection, then we need to combine current style with default style
3036 // to see what the effect would be if we started typing.
3038 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3040 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3041 if (GetStyle(pos
, attr
))
3043 if (IsDefaultStyleShowing())
3044 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3045 return attr
.GetFontStyle() == wxITALIC
;
3051 /// Is all of the selection underlined?
3052 bool wxRichTextCtrl::IsSelectionUnderlined()
3056 wxRichTextRange range
= GetSelectionRange();
3058 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3059 attr
.SetFontUnderlined(true);
3061 return HasCharacterAttributes(range
, attr
);
3065 // If no selection, then we need to combine current style with default style
3066 // to see what the effect would be if we started typing.
3068 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3069 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3071 if (GetStyle(pos
, attr
))
3073 if (IsDefaultStyleShowing())
3074 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3075 return attr
.GetFontUnderlined();
3081 /// Apply bold to the selection
3082 bool wxRichTextCtrl::ApplyBoldToSelection()
3085 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3086 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
3089 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3092 wxRichTextAttr current
= GetDefaultStyleEx();
3093 current
.Apply(attr
);
3094 SetAndShowDefaultStyle(current
);
3099 /// Apply italic to the selection
3100 bool wxRichTextCtrl::ApplyItalicToSelection()
3103 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3104 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
3107 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3110 wxRichTextAttr current
= GetDefaultStyleEx();
3111 current
.Apply(attr
);
3112 SetAndShowDefaultStyle(current
);
3117 /// Apply underline to the selection
3118 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3121 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3122 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3125 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3128 wxRichTextAttr current
= GetDefaultStyleEx();
3129 current
.Apply(attr
);
3130 SetAndShowDefaultStyle(current
);
3135 /// Is all of the selection aligned according to the specified flag?
3136 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3138 wxRichTextRange range
;
3140 range
= GetSelectionRange();
3142 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3145 attr
.SetAlignment(alignment
);
3147 return HasParagraphAttributes(range
, attr
);
3150 /// Apply alignment to the selection
3151 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3154 attr
.SetAlignment(alignment
);
3156 return SetStyle(GetSelectionRange(), attr
);
3159 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3161 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3166 /// Apply a named style to the selection
3167 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3169 // Flags are defined within each definition, so only certain
3170 // attributes are applied.
3171 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3173 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3175 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3177 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3179 wxRichTextRange range
;
3182 range
= GetSelectionRange();
3185 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3186 range
= wxRichTextRange(pos
, pos
+1);
3189 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3192 // Make sure the attr has the style name
3193 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3195 attr
.SetParagraphStyleName(def
->GetName());
3197 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3198 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3199 // to change its style independently.
3200 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3203 attr
.SetCharacterStyleName(def
->GetName());
3206 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3209 wxRichTextAttr current
= GetDefaultStyleEx();
3210 current
.Apply(attr
);
3211 SetAndShowDefaultStyle(current
);
3216 /// Apply the style sheet to the buffer, for example if the styles have changed.
3217 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3220 styleSheet
= GetBuffer().GetStyleSheet();
3224 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3226 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3234 /// Sets the default style to the style under the cursor
3235 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3238 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3240 // If at the start of a paragraph, use the next position.
3241 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3243 if (GetUncombinedStyle(pos
, attr
))
3245 SetDefaultStyle(attr
);
3252 /// Returns the first visible position in the current view
3253 long wxRichTextCtrl::GetFirstVisiblePosition() const
3255 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3257 return line
->GetAbsoluteRange().GetStart();
3262 /// Get the first visible point in the window
3263 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3266 int startXUnits
, startYUnits
;
3268 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3269 GetViewStart(& startXUnits
, & startYUnits
);
3271 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3274 /// The adjusted caret position is the character position adjusted to take
3275 /// into account whether we're at the start of a paragraph, in which case
3276 /// style information should be taken from the next position, not current one.
3277 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3279 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3281 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3286 /// Get/set the selection range in character positions. -1, -1 means no selection.
3287 /// The range is in API convention, i.e. a single character selection is denoted
3289 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3291 wxRichTextRange range
= GetInternalSelectionRange();
3292 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3293 range
.SetEnd(range
.GetEnd() + 1);
3297 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3299 wxRichTextRange
range1(range
);
3300 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3301 range1
.SetEnd(range1
.GetEnd() - 1);
3303 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3305 SetInternalSelectionRange(range1
);
3309 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3311 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3314 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3316 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3319 /// Clear list for given range
3320 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3322 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3325 /// Number/renumber any list elements in the given range
3326 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3328 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3331 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3333 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3336 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3337 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3339 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3342 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3344 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3347 /// Deletes the content in the given range
3348 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3350 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3353 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3355 if (sm_availableFontNames
.GetCount() == 0)
3357 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3358 sm_availableFontNames
.Sort();
3360 return sm_availableFontNames
;
3363 void wxRichTextCtrl::ClearAvailableFontNames()
3365 sm_availableFontNames
.Clear();
3368 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3370 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3372 wxTextAttrEx basicStyle
= GetBasicStyle();
3373 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3374 SetBasicStyle(basicStyle
);
3375 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3380 // Refresh the area affected by a selection change
3381 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3383 // Calculate the refresh rectangle - just the affected lines
3384 long firstPos
, lastPos
;
3385 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3387 firstPos
= newSelection
.GetStart();
3388 lastPos
= newSelection
.GetEnd();
3390 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3392 firstPos
= oldSelection
.GetStart();
3393 lastPos
= oldSelection
.GetEnd();
3395 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3401 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3402 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3405 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3406 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3408 if (firstLine
&& lastLine
)
3410 wxSize clientSize
= GetClientSize();
3411 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3412 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3415 pt1
.y
= wxMax(0, pt1
.y
);
3417 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3419 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3420 RefreshRect(rect
, false);
3428 #if wxRICHTEXT_USE_OWN_CARET
3430 // ----------------------------------------------------------------------------
3431 // initialization and destruction
3432 // ----------------------------------------------------------------------------
3434 void wxRichTextCaret::Init()
3440 m_richTextCtrl
= NULL
;
3441 m_needsUpdate
= false;
3444 wxRichTextCaret::~wxRichTextCaret()
3448 // ----------------------------------------------------------------------------
3449 // showing/hiding/moving the caret (base class interface)
3450 // ----------------------------------------------------------------------------
3452 void wxRichTextCaret::DoShow()
3457 void wxRichTextCaret::DoHide()
3462 void wxRichTextCaret::DoMove()
3468 if (m_xOld
!= -1 && m_yOld
!= -1)
3472 wxRect
rect(GetPosition(), GetSize());
3473 m_richTextCtrl
->RefreshRect(rect
, false);
3482 void wxRichTextCaret::DoSize()
3484 int countVisible
= m_countVisible
;
3485 if (countVisible
> 0)
3491 if (countVisible
> 0)
3493 m_countVisible
= countVisible
;
3498 // ----------------------------------------------------------------------------
3499 // handling the focus
3500 // ----------------------------------------------------------------------------
3502 void wxRichTextCaret::OnSetFocus()
3510 void wxRichTextCaret::OnKillFocus()
3515 // ----------------------------------------------------------------------------
3516 // drawing the caret
3517 // ----------------------------------------------------------------------------
3519 void wxRichTextCaret::Refresh()
3523 wxRect
rect(GetPosition(), GetSize());
3524 m_richTextCtrl
->RefreshRect(rect
, false);
3528 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3530 dc
->SetPen( *wxBLACK_PEN
);
3532 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3533 dc
->SetPen(*wxBLACK_PEN
);
3535 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3536 // done under wxGTK - no idea why
3537 //dc->SetLogicalFunction(wxINVERT);
3539 wxPoint
pt(m_x
, m_y
);
3543 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3545 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3548 // wxRICHTEXT_USE_OWN_CARET