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())
307 void wxRichTextCtrl::Clear()
309 m_buffer
.ResetAndClearCommands();
310 m_buffer
.SetDirty(true);
311 m_caretPosition
= -1;
312 m_caretPositionForDefaultStyle
= -2;
313 m_caretAtLineStart
= false;
314 m_selectionRange
.SetRange(-2, -2);
324 wxTextCtrl::SendTextUpdatedEvent(this);
328 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
330 #if !wxRICHTEXT_USE_OWN_CARET
331 if (GetCaret() && !IsFrozen())
336 #if wxRICHTEXT_BUFFERED_PAINTING
337 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
347 dc
.SetFont(GetFont());
349 // Paint the background
352 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
354 wxRect
drawingArea(GetUpdateRegion().GetBox());
355 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
357 wxRect
availableSpace(GetClientSize());
358 if (GetBuffer().GetDirty())
360 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
361 GetBuffer().SetDirty(false);
365 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
366 #if wxRICHTEXT_USE_OWN_CARET
367 if (GetCaret()->IsVisible())
369 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
374 #if !wxRICHTEXT_USE_OWN_CARET
381 // Empty implementation, to prevent flicker
382 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
386 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
390 #if !wxRICHTEXT_USE_OWN_CARET
396 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
397 // Work around dropouts when control is focused
405 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
410 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
411 // Work around dropouts when control is focused
419 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
425 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
431 dc
.SetFont(GetFont());
434 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
436 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
438 m_dragStart
= event
.GetLogicalPosition(dc
);
442 bool caretAtLineStart
= false;
444 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
446 // If we're at the start of a line (but not first in para)
447 // then we should keep the caret showing at the start of the line
448 // by showing the m_caretAtLineStart flag.
449 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
450 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
452 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
453 caretAtLineStart
= true;
457 long oldCaretPos
= m_caretPosition
;
459 MoveCaret(position
, caretAtLineStart
);
460 SetDefaultStyleToCursorStyle();
462 if (event
.ShiftDown())
464 if (m_selectionRange
.GetStart() == -2)
465 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
467 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
477 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
482 if (GetCapture() == this)
485 // See if we clicked on a URL
488 dc
.SetFont(GetFont());
491 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
492 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
494 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
496 wxRichTextEvent
cmdEvent(
497 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
499 cmdEvent
.SetEventObject(this);
500 cmdEvent
.SetPosition(m_caretPosition
+1);
502 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
505 if (GetStyle(position
, attr
))
507 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
509 wxString urlTarget
= attr
.GetURL();
510 if (!urlTarget
.IsEmpty())
512 wxMouseEvent
mouseEvent(event
);
514 long startPos
= 0, endPos
= 0;
515 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
518 startPos
= obj
->GetRange().GetStart();
519 endPos
= obj
->GetRange().GetEnd();
522 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
523 InitCommandEvent(urlEvent
);
525 urlEvent
.SetString(urlTarget
);
527 GetEventHandler()->ProcessEvent(urlEvent
);
537 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
541 dc
.SetFont(GetFont());
544 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
545 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
547 // See if we need to change the cursor
550 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
553 if (GetStyle(position
, attr
))
555 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
557 SetCursor(m_urlCursor
);
559 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
561 SetCursor(m_textCursor
);
566 SetCursor(m_textCursor
);
569 if (!event
.Dragging())
575 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
577 // TODO: test closeness
579 bool caretAtLineStart
= false;
581 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
583 // If we're at the start of a line (but not first in para)
584 // then we should keep the caret showing at the start of the line
585 // by showing the m_caretAtLineStart flag.
586 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
587 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
589 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
590 caretAtLineStart
= true;
594 if (m_caretPosition
!= position
)
596 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
598 MoveCaret(position
, caretAtLineStart
);
599 SetDefaultStyleToCursorStyle();
605 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
609 wxRichTextEvent
cmdEvent(
610 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
612 cmdEvent
.SetEventObject(this);
613 cmdEvent
.SetPosition(m_caretPosition
+1);
615 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
619 /// Left-double-click
620 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
622 wxRichTextEvent
cmdEvent(
623 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
625 cmdEvent
.SetEventObject(this);
626 cmdEvent
.SetPosition(m_caretPosition
+1);
628 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
630 SelectWord(GetCaretPosition()+1);
635 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
637 wxRichTextEvent
cmdEvent(
638 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
640 cmdEvent
.SetEventObject(this);
641 cmdEvent
.SetPosition(m_caretPosition
+1);
643 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
648 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
652 flags
|= wxRICHTEXT_CTRL_DOWN
;
653 if (event
.ShiftDown())
654 flags
|= wxRICHTEXT_SHIFT_DOWN
;
656 flags
|= wxRICHTEXT_ALT_DOWN
;
658 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
660 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
661 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
663 BeginBatchUndo(_("Delete Text"));
665 long newPos
= m_caretPosition
;
667 DeleteSelectedContent(& newPos
);
669 // Submit range in character positions, which are greater than caret positions,
670 // so subtract 1 for deleted character and add 1 for conversion to character position.
673 bool processed
= false;
676 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
677 if (pos
!= -1 && (pos
< newPos
))
679 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
685 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
690 if (GetLastPosition() == -1)
694 m_caretPosition
= -1;
696 SetDefaultStyleToCursorStyle();
699 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
701 wxRichTextEvent
cmdEvent(
702 wxEVT_COMMAND_RICHTEXT_DELETE
,
704 cmdEvent
.SetEventObject(this);
705 cmdEvent
.SetFlags(flags
);
706 cmdEvent
.SetPosition(m_caretPosition
+1);
707 GetEventHandler()->ProcessEvent(cmdEvent
);
717 if (event
.GetKeyCode() == WXK_LEFT
||
718 event
.GetKeyCode() == WXK_RIGHT
||
719 event
.GetKeyCode() == WXK_UP
||
720 event
.GetKeyCode() == WXK_DOWN
||
721 event
.GetKeyCode() == WXK_HOME
||
722 event
.GetKeyCode() == WXK_PAGEUP
||
723 event
.GetKeyCode() == WXK_PAGEDOWN
||
724 event
.GetKeyCode() == WXK_END
||
726 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
727 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
728 event
.GetKeyCode() == WXK_NUMPAD_UP
||
729 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
730 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
731 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
732 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
733 event
.GetKeyCode() == WXK_NUMPAD_END
)
735 KeyboardNavigate(event
.GetKeyCode(), flags
);
739 // all the other keys modify the controls contents which shouldn't be
740 // possible if we're read-only
747 if (event
.GetKeyCode() == WXK_RETURN
)
749 BeginBatchUndo(_("Insert Text"));
751 long newPos
= m_caretPosition
;
753 DeleteSelectedContent(& newPos
);
755 if (event
.ShiftDown())
758 text
= wxRichTextLineBreakChar
;
759 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
760 m_caretAtLineStart
= true;
764 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
767 SetDefaultStyleToCursorStyle();
769 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
771 wxRichTextEvent
cmdEvent(
772 wxEVT_COMMAND_RICHTEXT_RETURN
,
774 cmdEvent
.SetEventObject(this);
775 cmdEvent
.SetFlags(flags
);
776 cmdEvent
.SetPosition(newPos
+1);
778 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
780 // Generate conventional event
781 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
782 InitCommandEvent(textEvent
);
784 GetEventHandler()->ProcessEvent(textEvent
);
788 else if (event
.GetKeyCode() == WXK_BACK
)
790 BeginBatchUndo(_("Delete Text"));
792 long newPos
= m_caretPosition
;
794 DeleteSelectedContent(& newPos
);
796 // Submit range in character positions, which are greater than caret positions,
797 // so subtract 1 for deleted character and add 1 for conversion to character position.
800 bool processed
= false;
803 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
804 if (pos
!= -1 && (pos
< newPos
))
806 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
812 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
817 if (GetLastPosition() == -1)
821 m_caretPosition
= -1;
823 SetDefaultStyleToCursorStyle();
826 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
828 wxRichTextEvent
cmdEvent(
829 wxEVT_COMMAND_RICHTEXT_DELETE
,
831 cmdEvent
.SetEventObject(this);
832 cmdEvent
.SetFlags(flags
);
833 cmdEvent
.SetPosition(m_caretPosition
+1);
834 GetEventHandler()->ProcessEvent(cmdEvent
);
838 else if (event
.GetKeyCode() == WXK_DELETE
)
840 BeginBatchUndo(_("Delete Text"));
842 long newPos
= m_caretPosition
;
844 DeleteSelectedContent(& newPos
);
846 // Submit range in character positions, which are greater than caret positions,
847 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
849 bool processed
= false;
852 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
853 if (pos
!= -1 && (pos
> newPos
))
855 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
861 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
866 if (GetLastPosition() == -1)
870 m_caretPosition
= -1;
872 SetDefaultStyleToCursorStyle();
875 wxRichTextEvent
cmdEvent(
876 wxEVT_COMMAND_RICHTEXT_DELETE
,
878 cmdEvent
.SetEventObject(this);
879 cmdEvent
.SetFlags(flags
);
880 cmdEvent
.SetPosition(m_caretPosition
+1);
881 GetEventHandler()->ProcessEvent(cmdEvent
);
887 long keycode
= event
.GetKeyCode();
960 case WXK_NUMPAD_SPACE
:
962 case WXK_NUMPAD_ENTER
:
967 case WXK_NUMPAD_HOME
:
968 case WXK_NUMPAD_LEFT
:
970 case WXK_NUMPAD_RIGHT
:
971 case WXK_NUMPAD_DOWN
:
972 case WXK_NUMPAD_PAGEUP
:
973 case WXK_NUMPAD_PAGEDOWN
:
975 case WXK_NUMPAD_BEGIN
:
976 case WXK_NUMPAD_INSERT
:
977 case WXK_NUMPAD_DELETE
:
978 case WXK_NUMPAD_EQUAL
:
979 case WXK_NUMPAD_MULTIPLY
:
981 case WXK_NUMPAD_SEPARATOR
:
982 case WXK_NUMPAD_SUBTRACT
:
983 case WXK_NUMPAD_DECIMAL
:
984 case WXK_WINDOWS_LEFT
:
992 if (event
.CmdDown() || event
.AltDown())
998 wxRichTextEvent
cmdEvent(
999 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1001 cmdEvent
.SetEventObject(this);
1002 cmdEvent
.SetFlags(flags
);
1004 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1006 cmdEvent
.SetCharacter((wxChar
) keycode
);
1008 cmdEvent
.SetPosition(m_caretPosition
+1);
1010 if (keycode
== wxT('\t'))
1012 // See if we need to promote or demote the selection or paragraph at the cursor
1013 // position, instead of inserting a tab.
1014 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1015 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1016 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1018 wxRichTextRange range
;
1020 range
= GetSelectionRange();
1022 range
= para
->GetRange().FromInternal();
1024 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1026 PromoteList(promoteBy
, range
, NULL
);
1028 GetEventHandler()->ProcessEvent(cmdEvent
);
1034 BeginBatchUndo(_("Insert Text"));
1036 long newPos
= m_caretPosition
;
1037 DeleteSelectedContent(& newPos
);
1040 wxString str
= event
.GetUnicodeKey();
1042 wxString str
= (wxChar
) event
.GetKeyCode();
1044 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1048 SetDefaultStyleToCursorStyle();
1049 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1051 GetEventHandler()->ProcessEvent(cmdEvent
);
1059 /// Delete content if there is a selection, e.g. when pressing a key.
1060 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1064 long pos
= m_selectionRange
.GetStart();
1065 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1066 m_selectionRange
.SetRange(-2, -2);
1076 /// Keyboard navigation
1080 Left: left one character
1081 Right: right one character
1084 Ctrl-Left: left one word
1085 Ctrl-Right: right one word
1086 Ctrl-Up: previous paragraph start
1087 Ctrl-Down: next start of paragraph
1090 Ctrl-Home: start of document
1091 Ctrl-End: end of document
1092 Page-Up: Up a screen
1093 Page-Down: Down a screen
1097 Ctrl-Alt-PgUp: Start of window
1098 Ctrl-Alt-PgDn: End of window
1099 F8: Start selection mode
1100 Esc: End selection mode
1102 Adding Shift does the above but starts/extends selection.
1107 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1109 bool success
= false;
1111 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1113 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1114 success
= WordRight(1, flags
);
1116 success
= MoveRight(1, flags
);
1118 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1120 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1121 success
= WordLeft(1, flags
);
1123 success
= MoveLeft(1, flags
);
1125 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1127 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1128 success
= MoveToParagraphStart(flags
);
1130 success
= MoveUp(1, flags
);
1132 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1134 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1135 success
= MoveToParagraphEnd(flags
);
1137 success
= MoveDown(1, flags
);
1139 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1141 success
= PageUp(1, flags
);
1143 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1145 success
= PageDown(1, flags
);
1147 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1149 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1150 success
= MoveHome(flags
);
1152 success
= MoveToLineStart(flags
);
1154 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1156 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1157 success
= MoveEnd(flags
);
1159 success
= MoveToLineEnd(flags
);
1164 ScrollIntoView(m_caretPosition
, keyCode
);
1165 SetDefaultStyleToCursorStyle();
1171 /// Extend the selection. Selections are in caret positions.
1172 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1174 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1176 if (oldPos
== newPos
)
1179 wxRichTextRange oldSelection
= m_selectionRange
;
1181 // If not currently selecting, start selecting
1182 if (m_selectionRange
.GetStart() == -2)
1184 m_selectionAnchor
= oldPos
;
1186 if (oldPos
> newPos
)
1187 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1189 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1193 // Always ensure that the selection range start is greater than
1195 if (newPos
> m_selectionAnchor
)
1196 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1197 else if (newPos
== m_selectionAnchor
)
1198 m_selectionRange
= wxRichTextRange(-2, -2);
1200 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1203 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1205 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1207 wxLogDebug(wxT("Strange selection range"));
1216 /// Scroll into view, returning true if we scrolled.
1217 /// This takes a _caret_ position.
1218 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1220 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1226 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1228 int startXUnits
, startYUnits
;
1229 GetViewStart(& startXUnits
, & startYUnits
);
1230 int startY
= startYUnits
* ppuY
;
1233 GetVirtualSize(& sx
, & sy
);
1239 wxRect rect
= line
->GetRect();
1241 bool scrolled
= false;
1243 wxSize clientSize
= GetClientSize();
1246 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1247 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1248 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1249 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1251 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1253 // Make it scroll so this item is at the bottom
1255 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1256 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1258 // If we're still off the screen, scroll another line down
1259 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1262 if (startYUnits
!= yUnits
)
1264 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1268 else if (rect
.y
< startY
)
1270 // Make it scroll so this item is at the top
1273 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1275 if (startYUnits
!= yUnits
)
1277 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1283 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1284 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1285 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1286 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1288 if (rect
.y
< startY
)
1290 // Make it scroll so this item is at the top
1293 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1295 if (startYUnits
!= yUnits
)
1297 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1301 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1303 // Make it scroll so this item is at the bottom
1305 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1306 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1308 // If we're still off the screen, scroll another line down
1309 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1312 if (startYUnits
!= yUnits
)
1314 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1320 #if !wxRICHTEXT_USE_OWN_CARET
1328 /// Is the given position visible on the screen?
1329 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1331 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1337 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1340 GetViewStart(& startX
, & startY
);
1342 startY
= startY
* ppuY
;
1344 wxRect rect
= line
->GetRect();
1345 wxSize clientSize
= GetClientSize();
1347 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1350 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1352 m_caretPosition
= position
;
1353 m_caretAtLineStart
= showAtLineStart
;
1356 /// Move caret one visual step forward: this may mean setting a flag
1357 /// and keeping the same position if we're going from the end of one line
1358 /// to the start of the next, which may be the exact same caret position.
1359 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1361 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1363 // Only do the check if we're not at the end of the paragraph (where things work OK
1365 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1367 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1371 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1373 // We're at the end of a line. See whether we need to
1374 // stay at the same actual caret position but change visual
1375 // position, or not.
1376 if (oldPosition
== lineRange
.GetEnd())
1378 if (m_caretAtLineStart
)
1380 // We're already at the start of the line, so actually move on now.
1381 m_caretPosition
= oldPosition
+ 1;
1382 m_caretAtLineStart
= false;
1386 // We're showing at the end of the line, so keep to
1387 // the same position but indicate that we're to show
1388 // at the start of the next line.
1389 m_caretPosition
= oldPosition
;
1390 m_caretAtLineStart
= true;
1392 SetDefaultStyleToCursorStyle();
1398 SetDefaultStyleToCursorStyle();
1401 /// Move caret one visual step backward: this may mean setting a flag
1402 /// and keeping the same position if we're going from the end of one line
1403 /// to the start of the next, which may be the exact same caret position.
1404 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1406 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1408 // Only do the check if we're not at the start of the paragraph (where things work OK
1410 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1412 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1416 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1418 // We're at the start of a line. See whether we need to
1419 // stay at the same actual caret position but change visual
1420 // position, or not.
1421 if (oldPosition
== lineRange
.GetStart())
1423 m_caretPosition
= oldPosition
-1;
1424 m_caretAtLineStart
= true;
1427 else if (oldPosition
== lineRange
.GetEnd())
1429 if (m_caretAtLineStart
)
1431 // We're at the start of the line, so keep the same caret position
1432 // but clear the start-of-line flag.
1433 m_caretPosition
= oldPosition
;
1434 m_caretAtLineStart
= false;
1438 // We're showing at the end of the line, so go back
1439 // to the previous character position.
1440 m_caretPosition
= oldPosition
- 1;
1442 SetDefaultStyleToCursorStyle();
1448 SetDefaultStyleToCursorStyle();
1452 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1454 long endPos
= GetBuffer().GetRange().GetEnd();
1456 if (m_caretPosition
+ noPositions
< endPos
)
1458 long oldPos
= m_caretPosition
;
1459 long newPos
= m_caretPosition
+ noPositions
;
1461 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1465 // Determine by looking at oldPos and m_caretPosition whether
1466 // we moved from the end of a line to the start of the next line, in which case
1467 // we want to adjust the caret position such that it is positioned at the
1468 // start of the next line, rather than jumping past the first character of the
1470 if (noPositions
== 1 && !extendSel
)
1471 MoveCaretForward(oldPos
);
1473 SetCaretPosition(newPos
);
1476 SetDefaultStyleToCursorStyle();
1485 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1489 if (m_caretPosition
> startPos
- noPositions
+ 1)
1491 long oldPos
= m_caretPosition
;
1492 long newPos
= m_caretPosition
- noPositions
;
1493 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1497 if (noPositions
== 1 && !extendSel
)
1498 MoveCaretBack(oldPos
);
1500 SetCaretPosition(newPos
);
1503 SetDefaultStyleToCursorStyle();
1512 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1514 return MoveDown(- noLines
, flags
);
1518 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1523 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1524 wxPoint pt
= GetCaret()->GetPosition();
1525 long newLine
= lineNumber
+ noLines
;
1527 if (lineNumber
!= -1)
1531 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1533 if (newLine
> lastLine
)
1543 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1546 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1552 wxClientDC
dc(this);
1554 dc
.SetFont(GetFont());
1556 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1558 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1560 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1561 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1562 // so we view the caret at the start of the line.
1563 bool caretLineStart
= false;
1564 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1566 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1567 wxRichTextRange lineRange
;
1569 lineRange
= thisLine
->GetAbsoluteRange();
1571 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1574 caretLineStart
= true;
1578 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1579 if (para
&& para
->GetRange().GetStart() == newPos
)
1584 long newSelEnd
= newPos
;
1586 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1590 SetCaretPosition(newPos
, caretLineStart
);
1592 SetDefaultStyleToCursorStyle();
1600 /// Move to the end of the paragraph
1601 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1603 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1606 long newPos
= para
->GetRange().GetEnd() - 1;
1607 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1611 SetCaretPosition(newPos
);
1613 SetDefaultStyleToCursorStyle();
1621 /// Move to the start of the paragraph
1622 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1624 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1627 long newPos
= para
->GetRange().GetStart() - 1;
1628 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1632 SetCaretPosition(newPos
);
1634 SetDefaultStyleToCursorStyle();
1642 /// Move to the end of the line
1643 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1645 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1649 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1650 long newPos
= lineRange
.GetEnd();
1651 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1655 SetCaretPosition(newPos
);
1657 SetDefaultStyleToCursorStyle();
1665 /// Move to the start of the line
1666 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1668 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1671 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1672 long newPos
= lineRange
.GetStart()-1;
1674 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1678 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1680 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1682 SetDefaultStyleToCursorStyle();
1690 /// Move to the start of the buffer
1691 bool wxRichTextCtrl::MoveHome(int flags
)
1693 if (m_caretPosition
!= -1)
1695 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1699 SetCaretPosition(-1);
1701 SetDefaultStyleToCursorStyle();
1709 /// Move to the end of the buffer
1710 bool wxRichTextCtrl::MoveEnd(int flags
)
1712 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1714 if (m_caretPosition
!= endPos
)
1716 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1720 SetCaretPosition(endPos
);
1722 SetDefaultStyleToCursorStyle();
1730 /// Move noPages pages up
1731 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1733 return PageDown(- noPages
, flags
);
1736 /// Move noPages pages down
1737 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1739 // Calculate which line occurs noPages * screen height further down.
1740 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1743 wxSize clientSize
= GetClientSize();
1744 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1746 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1749 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1750 long pos
= lineRange
.GetStart()-1;
1751 if (pos
!= m_caretPosition
)
1753 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1755 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1759 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1761 SetDefaultStyleToCursorStyle();
1771 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1773 return str
== wxT(" ") || str
== wxT("\t");
1776 // Finds the caret position for the next word
1777 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1779 long endPos
= GetBuffer().GetRange().GetEnd();
1783 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1785 // First skip current text to space
1786 while (i
< endPos
&& i
> -1)
1788 // i is in character, not caret positions
1789 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1790 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1791 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1795 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1802 while (i
< endPos
&& i
> -1)
1804 // i is in character, not caret positions
1805 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1806 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1807 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1808 return wxMax(-1, i
);
1810 if (text
.empty()) // End of paragraph, or maybe an image
1811 return wxMax(-1, i
- 1);
1812 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1816 // Convert to caret position
1817 return wxMax(-1, i
- 1);
1826 long i
= m_caretPosition
;
1828 // First skip white space
1829 while (i
< endPos
&& i
> -1)
1831 // i is in character, not caret positions
1832 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1833 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1835 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1837 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1842 // Next skip current text to space
1843 while (i
< endPos
&& i
> -1)
1845 // i is in character, not caret positions
1846 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1847 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1848 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1851 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1864 /// Move n words left
1865 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1867 long pos
= FindNextWordPosition(-1);
1868 if (pos
!= m_caretPosition
)
1870 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1872 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1876 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1878 SetDefaultStyleToCursorStyle();
1886 /// Move n words right
1887 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1889 long pos
= FindNextWordPosition(1);
1890 if (pos
!= m_caretPosition
)
1892 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1894 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1898 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1900 SetDefaultStyleToCursorStyle();
1909 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1911 // Only do sizing optimization for large buffers
1912 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1914 m_fullLayoutRequired
= true;
1915 m_fullLayoutTime
= wxGetLocalTimeMillis();
1916 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1917 LayoutContent(true /* onlyVisibleRect */);
1920 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1922 #if wxRICHTEXT_BUFFERED_PAINTING
1930 /// Idle-time processing
1931 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1933 #if wxRICHTEXT_USE_OWN_CARET
1934 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1936 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1942 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1944 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1946 m_fullLayoutRequired
= false;
1947 m_fullLayoutTime
= 0;
1948 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1949 ShowPosition(m_fullLayoutSavedPosition
);
1953 if (m_caretPositionForDefaultStyle
!= -2)
1955 // If the caret position has changed, no longer reflect the default style
1957 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1958 m_caretPositionForDefaultStyle
= -2;
1965 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1967 #if wxRICHTEXT_USE_OWN_CARET
1968 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1971 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1978 /// Set up scrollbars, e.g. after a resize
1979 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1984 if (GetBuffer().IsEmpty())
1986 SetScrollbars(0, 0, 0, 0, 0, 0);
1990 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1991 // of pixels. See e.g. wxVScrolledWindow for ideas.
1992 int pixelsPerUnit
= 5;
1993 wxSize clientSize
= GetClientSize();
1995 int maxHeight
= GetBuffer().GetCachedSize().y
;
1997 // Round up so we have at least maxHeight pixels
1998 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2000 int startX
= 0, startY
= 0;
2002 GetViewStart(& startX
, & startY
);
2004 int maxPositionX
= 0;
2005 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2007 int newStartX
= wxMin(maxPositionX
, startX
);
2008 int newStartY
= wxMin(maxPositionY
, startY
);
2010 int oldPPUX
, oldPPUY
;
2011 int oldStartX
, oldStartY
;
2012 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2013 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2014 GetViewStart(& oldStartX
, & oldStartY
);
2015 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2017 oldVirtualSizeY
/= oldPPUY
;
2019 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2022 // Don't set scrollbars if there were none before, and there will be none now.
2023 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2026 // Move to previous scroll position if
2028 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2031 /// Paint the background
2032 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2034 wxColour backgroundColour
= GetBackgroundColour();
2035 if (!backgroundColour
.Ok())
2036 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2038 // Clear the background
2039 dc
.SetBrush(wxBrush(backgroundColour
));
2040 dc
.SetPen(*wxTRANSPARENT_PEN
);
2041 wxRect
windowRect(GetClientSize());
2042 windowRect
.x
-= 2; windowRect
.y
-= 2;
2043 windowRect
.width
+= 4; windowRect
.height
+= 4;
2045 // We need to shift the rectangle to take into account
2046 // scrolling. Converting device to logical coordinates.
2047 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2048 dc
.DrawRectangle(windowRect
);
2051 #if wxRICHTEXT_BUFFERED_PAINTING
2052 /// Recreate buffer bitmap if necessary
2053 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2056 if (sz
== wxDefaultSize
)
2057 sz
= GetClientSize();
2059 if (sz
.x
< 1 || sz
.y
< 1)
2062 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2063 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2064 return m_bufferBitmap
.Ok();
2068 // ----------------------------------------------------------------------------
2069 // file IO functions
2070 // ----------------------------------------------------------------------------
2072 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2074 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2076 m_filename
= filename
;
2079 SetInsertionPoint(0);
2082 SetupScrollbars(true);
2084 wxTextCtrl::SendTextUpdatedEvent(this);
2090 wxLogError(_("File couldn't be loaded."));
2096 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2098 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2100 m_filename
= filename
;
2107 wxLogError(_("The text couldn't be saved."));
2112 // ----------------------------------------------------------------------------
2113 // wxRichTextCtrl specific functionality
2114 // ----------------------------------------------------------------------------
2116 /// Add a new paragraph of text to the end of the buffer
2117 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2119 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2125 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2127 wxRichTextRange range
= GetBuffer().AddImage(image
);
2132 // ----------------------------------------------------------------------------
2133 // selection and ranges
2134 // ----------------------------------------------------------------------------
2136 void wxRichTextCtrl::SelectAll()
2138 SetSelection(0, GetLastPosition()+1);
2139 m_selectionAnchor
= -1;
2143 void wxRichTextCtrl::SelectNone()
2145 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2147 wxRichTextRange oldSelection
= m_selectionRange
;
2149 m_selectionRange
= wxRichTextRange(-2, -2);
2151 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2153 m_selectionAnchor
= -2;
2156 static bool wxIsWordDelimiter(const wxString
& text
)
2158 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2161 /// Select the word at the given character position
2162 bool wxRichTextCtrl::SelectWord(long position
)
2164 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2167 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2171 if (position
== para
->GetRange().GetEnd())
2174 long positionStart
= position
;
2175 long positionEnd
= position
;
2177 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2179 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2180 if (wxIsWordDelimiter(text
))
2186 if (positionStart
< para
->GetRange().GetStart())
2187 positionStart
= para
->GetRange().GetStart();
2189 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2191 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2192 if (wxIsWordDelimiter(text
))
2198 if (positionEnd
>= para
->GetRange().GetEnd())
2199 positionEnd
= para
->GetRange().GetEnd();
2201 if (positionEnd
< positionStart
)
2204 SetSelection(positionStart
, positionEnd
+1);
2206 if (positionStart
>= 0)
2208 MoveCaret(positionStart
-1, true);
2209 SetDefaultStyleToCursorStyle();
2215 wxString
wxRichTextCtrl::GetStringSelection() const
2218 GetSelection(&from
, &to
);
2220 return GetRange(from
, to
);
2223 // ----------------------------------------------------------------------------
2225 // ----------------------------------------------------------------------------
2227 wxTextCtrlHitTestResult
2228 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2230 // implement in terms of the other overload as the native ports typically
2231 // can get the position and not (x, y) pair directly (although wxUniv
2232 // directly gets x and y -- and so overrides this method as well)
2234 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2236 if ( rc
!= wxTE_HT_UNKNOWN
)
2238 PositionToXY(pos
, x
, y
);
2244 wxTextCtrlHitTestResult
2245 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2248 wxClientDC
dc((wxRichTextCtrl
*) this);
2249 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2251 // Buffer uses logical position (relative to start of buffer)
2253 wxPoint pt2
= GetLogicalPoint(pt
);
2255 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2257 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2258 return wxTE_HT_BEFORE
;
2259 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2260 return wxTE_HT_BEYOND
;
2261 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2262 return wxTE_HT_ON_TEXT
;
2264 return wxTE_HT_UNKNOWN
;
2267 // ----------------------------------------------------------------------------
2268 // set/get the controls text
2269 // ----------------------------------------------------------------------------
2271 wxString
wxRichTextCtrl::GetValue() const
2273 return GetBuffer().GetText();
2276 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2278 // Public API for range is different from internals
2279 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2282 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2284 // Don't call Clear here, since it always sends a text updated event
2285 m_buffer
.ResetAndClearCommands();
2286 m_buffer
.SetDirty(true);
2287 m_caretPosition
= -1;
2288 m_caretPositionForDefaultStyle
= -2;
2289 m_caretAtLineStart
= false;
2290 m_selectionRange
.SetRange(-2, -2);
2300 if (!value
.IsEmpty())
2302 // Remove empty paragraph
2303 GetBuffer().Clear();
2304 DoWriteText(value
, flags
);
2306 // for compatibility, don't move the cursor when doing SetValue()
2307 SetInsertionPoint(0);
2311 // still send an event for consistency
2312 if (flags
& SetValue_SendEvent
)
2313 wxTextCtrl::SendTextUpdatedEvent(this);
2318 void wxRichTextCtrl::WriteText(const wxString
& value
)
2323 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2325 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2327 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2329 if ( flags
& SetValue_SendEvent
)
2330 wxTextCtrl::SendTextUpdatedEvent(this);
2333 void wxRichTextCtrl::AppendText(const wxString
& text
)
2335 SetInsertionPointEnd();
2340 /// Write an image at the current insertion point
2341 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2343 wxRichTextImageBlock imageBlock
;
2345 wxImage image2
= image
;
2346 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2347 return WriteImage(imageBlock
);
2352 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2354 wxRichTextImageBlock imageBlock
;
2357 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2358 return WriteImage(imageBlock
);
2363 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2365 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2368 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2372 wxRichTextImageBlock imageBlock
;
2374 wxImage image
= bitmap
.ConvertToImage();
2375 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2376 return WriteImage(imageBlock
);
2382 /// Insert a newline (actually paragraph) at the current insertion point.
2383 bool wxRichTextCtrl::Newline()
2385 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2388 /// Insert a line break at the current insertion point.
2389 bool wxRichTextCtrl::LineBreak()
2392 text
= wxRichTextLineBreakChar
;
2393 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2396 // ----------------------------------------------------------------------------
2397 // Clipboard operations
2398 // ----------------------------------------------------------------------------
2400 void wxRichTextCtrl::Copy()
2404 wxRichTextRange range
= GetInternalSelectionRange();
2405 GetBuffer().CopyToClipboard(range
);
2409 void wxRichTextCtrl::Cut()
2413 wxRichTextRange range
= GetInternalSelectionRange();
2414 GetBuffer().CopyToClipboard(range
);
2416 DeleteSelectedContent();
2422 void wxRichTextCtrl::Paste()
2426 BeginBatchUndo(_("Paste"));
2428 long newPos
= m_caretPosition
;
2429 DeleteSelectedContent(& newPos
);
2431 GetBuffer().PasteFromClipboard(newPos
);
2437 void wxRichTextCtrl::DeleteSelection()
2439 if (CanDeleteSelection())
2441 DeleteSelectedContent();
2445 bool wxRichTextCtrl::HasSelection() const
2447 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2450 bool wxRichTextCtrl::CanCopy() const
2452 // Can copy if there's a selection
2453 return HasSelection();
2456 bool wxRichTextCtrl::CanCut() const
2458 return HasSelection() && IsEditable();
2461 bool wxRichTextCtrl::CanPaste() const
2463 if ( !IsEditable() )
2466 return GetBuffer().CanPasteFromClipboard();
2469 bool wxRichTextCtrl::CanDeleteSelection() const
2471 return HasSelection() && IsEditable();
2475 // ----------------------------------------------------------------------------
2477 // ----------------------------------------------------------------------------
2479 void wxRichTextCtrl::SetEditable(bool editable
)
2481 m_editable
= editable
;
2484 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2488 m_caretPosition
= pos
- 1;
2493 void wxRichTextCtrl::SetInsertionPointEnd()
2495 long pos
= GetLastPosition();
2496 SetInsertionPoint(pos
);
2499 long wxRichTextCtrl::GetInsertionPoint() const
2501 return m_caretPosition
+1;
2504 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2506 return GetBuffer().GetRange().GetEnd();
2509 // If the return values from and to are the same, there is no
2511 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2513 *from
= m_selectionRange
.GetStart();
2514 *to
= m_selectionRange
.GetEnd();
2515 if ((*to
) != -1 && (*to
) != -2)
2519 bool wxRichTextCtrl::IsEditable() const
2524 // ----------------------------------------------------------------------------
2526 // ----------------------------------------------------------------------------
2528 void wxRichTextCtrl::SetSelection(long from
, long to
)
2530 // if from and to are both -1, it means (in wxWidgets) that all text should
2532 if ( (from
== -1) && (to
== -1) )
2535 to
= GetLastPosition()+1;
2538 DoSetSelection(from
, to
);
2541 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2549 wxRichTextRange oldSelection
= m_selectionRange
;
2550 m_selectionAnchor
= from
;
2551 m_selectionRange
.SetRange(from
, to
-1);
2553 m_caretPosition
= from
-1;
2555 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2560 // ----------------------------------------------------------------------------
2562 // ----------------------------------------------------------------------------
2564 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2565 const wxString
& value
)
2567 BeginBatchUndo(_("Replace"));
2569 DeleteSelectedContent();
2571 DoWriteText(value
, SetValue_SelectionOnly
);
2576 void wxRichTextCtrl::Remove(long from
, long to
)
2580 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2587 bool wxRichTextCtrl::IsModified() const
2589 return m_buffer
.IsModified();
2592 void wxRichTextCtrl::MarkDirty()
2594 m_buffer
.Modify(true);
2597 void wxRichTextCtrl::DiscardEdits()
2599 m_caretPositionForDefaultStyle
= -2;
2600 m_buffer
.Modify(false);
2601 m_buffer
.GetCommandProcessor()->ClearCommands();
2604 int wxRichTextCtrl::GetNumberOfLines() const
2606 return GetBuffer().GetParagraphCount();
2609 // ----------------------------------------------------------------------------
2610 // Positions <-> coords
2611 // ----------------------------------------------------------------------------
2613 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2615 return GetBuffer().XYToPosition(x
, y
);
2618 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2620 return GetBuffer().PositionToXY(pos
, x
, y
);
2623 // ----------------------------------------------------------------------------
2625 // ----------------------------------------------------------------------------
2627 void wxRichTextCtrl::ShowPosition(long pos
)
2629 if (!IsPositionVisible(pos
))
2630 ScrollIntoView(pos
-1, WXK_DOWN
);
2633 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2635 return GetBuffer().GetParagraphLength(lineNo
);
2638 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2640 return GetBuffer().GetParagraphText(lineNo
);
2643 // ----------------------------------------------------------------------------
2645 // ----------------------------------------------------------------------------
2647 void wxRichTextCtrl::Undo()
2651 GetCommandProcessor()->Undo();
2655 void wxRichTextCtrl::Redo()
2659 GetCommandProcessor()->Redo();
2663 bool wxRichTextCtrl::CanUndo() const
2665 return GetCommandProcessor()->CanUndo();
2668 bool wxRichTextCtrl::CanRedo() const
2670 return GetCommandProcessor()->CanRedo();
2673 // ----------------------------------------------------------------------------
2674 // implementation details
2675 // ----------------------------------------------------------------------------
2677 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2679 SetValue(event
.GetString());
2680 GetEventHandler()->ProcessEvent(event
);
2683 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2685 // By default, load the first file into the text window.
2686 if (event
.GetNumberOfFiles() > 0)
2688 LoadFile(event
.GetFiles()[0]);
2692 wxSize
wxRichTextCtrl::DoGetBestSize() const
2694 return wxSize(10, 10);
2697 // ----------------------------------------------------------------------------
2698 // standard handlers for standard edit menu events
2699 // ----------------------------------------------------------------------------
2701 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2706 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2711 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2716 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2721 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2726 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2731 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2733 event
.Enable( CanCut() );
2736 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2738 event
.Enable( CanCopy() );
2741 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2743 event
.Enable( CanDeleteSelection() );
2746 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2748 event
.Enable( CanPaste() );
2751 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2753 event
.Enable( CanUndo() );
2754 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2757 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2759 event
.Enable( CanRedo() );
2760 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2763 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2768 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2770 event
.Enable(GetLastPosition() > 0);
2773 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2775 if (event
.GetEventObject() != this)
2783 m_contextMenu
= new wxMenu
;
2784 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2785 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2786 m_contextMenu
->AppendSeparator();
2787 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2788 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2789 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2790 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2791 m_contextMenu
->AppendSeparator();
2792 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2794 PopupMenu(m_contextMenu
);
2798 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2800 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2803 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2805 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2808 // extended style setting operation with flags including:
2809 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2810 // see richtextbuffer.h for more details.
2812 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2814 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2817 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2819 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2822 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2824 return GetBuffer().GetDefaultStyle();
2827 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2829 return GetBuffer().GetStyle(position
, style
);
2832 // get the common set of styles for the range
2833 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2835 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2838 /// Get the content (uncombined) attributes for this position.
2839 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2841 return GetBuffer().GetUncombinedStyle(position
, style
);
2844 /// Set font, and also the buffer attributes
2845 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2847 wxControl::SetFont(font
);
2849 wxTextAttr attr
= GetBuffer().GetAttributes();
2851 GetBuffer().SetBasicStyle(attr
);
2853 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2859 /// Transform logical to physical
2860 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2863 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2868 /// Transform physical to logical
2869 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2872 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2877 /// Position the caret
2878 void wxRichTextCtrl::PositionCaret()
2883 //wxLogDebug(wxT("PositionCaret"));
2886 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2888 wxPoint newPt
= caretRect
.GetPosition();
2889 wxSize newSz
= caretRect
.GetSize();
2890 wxPoint pt
= GetPhysicalPoint(newPt
);
2891 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2894 if (GetCaret()->GetSize() != newSz
)
2895 GetCaret()->SetSize(newSz
);
2896 GetCaret()->Move(pt
);
2902 /// Get the caret height and position for the given character position
2903 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2905 wxClientDC
dc(this);
2906 dc
.SetFont(GetFont());
2913 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2915 // Caret height can't be zero
2917 height
= dc
.GetCharHeight();
2919 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2926 /// Gets the line for the visible caret position. If the caret is
2927 /// shown at the very end of the line, it means the next character is actually
2928 /// on the following line. So let's get the line we're expecting to find
2929 /// if this is the case.
2930 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2932 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2933 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2936 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2937 if (caretPosition
== lineRange
.GetStart()-1 &&
2938 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2940 if (!m_caretAtLineStart
)
2941 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2948 /// Move the caret to the given character position
2949 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2951 if (GetBuffer().GetDirty())
2954 if (pos
<= GetBuffer().GetRange().GetEnd())
2956 SetCaretPosition(pos
, showAtLineStart
);
2966 /// Layout the buffer: which we must do before certain operations, such as
2967 /// setting the caret position.
2968 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2970 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2972 wxRect
availableSpace(GetClientSize());
2973 if (availableSpace
.width
== 0)
2974 availableSpace
.width
= 10;
2975 if (availableSpace
.height
== 0)
2976 availableSpace
.height
= 10;
2978 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2979 if (onlyVisibleRect
)
2981 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2982 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2985 wxClientDC
dc(this);
2986 dc
.SetFont(GetFont());
2990 GetBuffer().Defragment();
2991 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2992 GetBuffer().Layout(dc
, availableSpace
, flags
);
2993 GetBuffer().SetDirty(false);
3002 /// Is all of the selection bold?
3003 bool wxRichTextCtrl::IsSelectionBold()
3008 wxRichTextRange range
= GetSelectionRange();
3009 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3010 attr
.SetFontWeight(wxBOLD
);
3012 return HasCharacterAttributes(range
, attr
);
3016 // If no selection, then we need to combine current style with default style
3017 // to see what the effect would be if we started typing.
3019 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3021 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3022 if (GetStyle(pos
, attr
))
3024 if (IsDefaultStyleShowing())
3025 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3026 return attr
.GetFontWeight() == wxBOLD
;
3032 /// Is all of the selection italics?
3033 bool wxRichTextCtrl::IsSelectionItalics()
3037 wxRichTextRange range
= GetSelectionRange();
3039 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3040 attr
.SetFontStyle(wxITALIC
);
3042 return HasCharacterAttributes(range
, attr
);
3046 // If no selection, then we need to combine current style with default style
3047 // to see what the effect would be if we started typing.
3049 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3051 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3052 if (GetStyle(pos
, attr
))
3054 if (IsDefaultStyleShowing())
3055 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3056 return attr
.GetFontStyle() == wxITALIC
;
3062 /// Is all of the selection underlined?
3063 bool wxRichTextCtrl::IsSelectionUnderlined()
3067 wxRichTextRange range
= GetSelectionRange();
3069 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3070 attr
.SetFontUnderlined(true);
3072 return HasCharacterAttributes(range
, attr
);
3076 // If no selection, then we need to combine current style with default style
3077 // to see what the effect would be if we started typing.
3079 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3080 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3082 if (GetStyle(pos
, attr
))
3084 if (IsDefaultStyleShowing())
3085 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3086 return attr
.GetFontUnderlined();
3092 /// Apply bold to the selection
3093 bool wxRichTextCtrl::ApplyBoldToSelection()
3096 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3097 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
3100 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3103 wxRichTextAttr current
= GetDefaultStyleEx();
3104 current
.Apply(attr
);
3105 SetAndShowDefaultStyle(current
);
3110 /// Apply italic to the selection
3111 bool wxRichTextCtrl::ApplyItalicToSelection()
3114 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3115 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
3118 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3121 wxRichTextAttr current
= GetDefaultStyleEx();
3122 current
.Apply(attr
);
3123 SetAndShowDefaultStyle(current
);
3128 /// Apply underline to the selection
3129 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3132 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3133 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3136 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3139 wxRichTextAttr current
= GetDefaultStyleEx();
3140 current
.Apply(attr
);
3141 SetAndShowDefaultStyle(current
);
3146 /// Is all of the selection aligned according to the specified flag?
3147 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3149 wxRichTextRange range
;
3151 range
= GetSelectionRange();
3153 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3156 attr
.SetAlignment(alignment
);
3158 return HasParagraphAttributes(range
, attr
);
3161 /// Apply alignment to the selection
3162 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3165 attr
.SetAlignment(alignment
);
3167 return SetStyle(GetSelectionRange(), attr
);
3170 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3172 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3177 /// Apply a named style to the selection
3178 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3180 // Flags are defined within each definition, so only certain
3181 // attributes are applied.
3182 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3184 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3186 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3188 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3190 wxRichTextRange range
;
3193 range
= GetSelectionRange();
3196 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3197 range
= wxRichTextRange(pos
, pos
+1);
3200 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3203 // Make sure the attr has the style name
3204 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3206 attr
.SetParagraphStyleName(def
->GetName());
3208 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3209 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3210 // to change its style independently.
3211 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3214 attr
.SetCharacterStyleName(def
->GetName());
3217 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3220 wxRichTextAttr current
= GetDefaultStyleEx();
3221 current
.Apply(attr
);
3222 SetAndShowDefaultStyle(current
);
3227 /// Apply the style sheet to the buffer, for example if the styles have changed.
3228 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3231 styleSheet
= GetBuffer().GetStyleSheet();
3235 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3237 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3245 /// Sets the default style to the style under the cursor
3246 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3249 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3251 // If at the start of a paragraph, use the next position.
3252 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3254 if (GetUncombinedStyle(pos
, attr
))
3256 SetDefaultStyle(attr
);
3263 /// Returns the first visible position in the current view
3264 long wxRichTextCtrl::GetFirstVisiblePosition() const
3266 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3268 return line
->GetAbsoluteRange().GetStart();
3273 /// Get the first visible point in the window
3274 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3277 int startXUnits
, startYUnits
;
3279 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3280 GetViewStart(& startXUnits
, & startYUnits
);
3282 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3285 /// The adjusted caret position is the character position adjusted to take
3286 /// into account whether we're at the start of a paragraph, in which case
3287 /// style information should be taken from the next position, not current one.
3288 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3290 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3292 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3297 /// Get/set the selection range in character positions. -1, -1 means no selection.
3298 /// The range is in API convention, i.e. a single character selection is denoted
3300 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3302 wxRichTextRange range
= GetInternalSelectionRange();
3303 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3304 range
.SetEnd(range
.GetEnd() + 1);
3308 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3310 wxRichTextRange
range1(range
);
3311 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3312 range1
.SetEnd(range1
.GetEnd() - 1);
3314 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3316 SetInternalSelectionRange(range1
);
3320 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3322 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3325 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3327 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3330 /// Clear list for given range
3331 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3333 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3336 /// Number/renumber any list elements in the given range
3337 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3339 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3342 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3344 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3347 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3348 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3350 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3353 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3355 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3358 /// Deletes the content in the given range
3359 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3361 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3364 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3366 if (sm_availableFontNames
.GetCount() == 0)
3368 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3369 sm_availableFontNames
.Sort();
3371 return sm_availableFontNames
;
3374 void wxRichTextCtrl::ClearAvailableFontNames()
3376 sm_availableFontNames
.Clear();
3379 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3381 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3383 wxTextAttrEx basicStyle
= GetBasicStyle();
3384 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3385 SetBasicStyle(basicStyle
);
3386 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3391 // Refresh the area affected by a selection change
3392 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3394 // Calculate the refresh rectangle - just the affected lines
3395 long firstPos
, lastPos
;
3396 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3398 firstPos
= newSelection
.GetStart();
3399 lastPos
= newSelection
.GetEnd();
3401 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3403 firstPos
= oldSelection
.GetStart();
3404 lastPos
= oldSelection
.GetEnd();
3406 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3412 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3413 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3416 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3417 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3419 if (firstLine
&& lastLine
)
3421 wxSize clientSize
= GetClientSize();
3422 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3423 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3426 pt1
.y
= wxMax(0, pt1
.y
);
3428 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3430 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3431 RefreshRect(rect
, false);
3439 #if wxRICHTEXT_USE_OWN_CARET
3441 // ----------------------------------------------------------------------------
3442 // initialization and destruction
3443 // ----------------------------------------------------------------------------
3445 void wxRichTextCaret::Init()
3451 m_richTextCtrl
= NULL
;
3452 m_needsUpdate
= false;
3455 wxRichTextCaret::~wxRichTextCaret()
3459 // ----------------------------------------------------------------------------
3460 // showing/hiding/moving the caret (base class interface)
3461 // ----------------------------------------------------------------------------
3463 void wxRichTextCaret::DoShow()
3468 void wxRichTextCaret::DoHide()
3473 void wxRichTextCaret::DoMove()
3479 if (m_xOld
!= -1 && m_yOld
!= -1)
3483 wxRect
rect(GetPosition(), GetSize());
3484 m_richTextCtrl
->RefreshRect(rect
, false);
3493 void wxRichTextCaret::DoSize()
3495 int countVisible
= m_countVisible
;
3496 if (countVisible
> 0)
3502 if (countVisible
> 0)
3504 m_countVisible
= countVisible
;
3509 // ----------------------------------------------------------------------------
3510 // handling the focus
3511 // ----------------------------------------------------------------------------
3513 void wxRichTextCaret::OnSetFocus()
3521 void wxRichTextCaret::OnKillFocus()
3526 // ----------------------------------------------------------------------------
3527 // drawing the caret
3528 // ----------------------------------------------------------------------------
3530 void wxRichTextCaret::Refresh()
3534 wxRect
rect(GetPosition(), GetSize());
3535 m_richTextCtrl
->RefreshRect(rect
, false);
3539 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3541 dc
->SetPen( *wxBLACK_PEN
);
3543 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3544 dc
->SetPen(*wxBLACK_PEN
);
3546 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3547 // done under wxGTK - no idea why
3548 //dc->SetLogicalFunction(wxINVERT);
3550 wxPoint
pt(m_x
, m_y
);
3554 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3556 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3559 // wxRICHTEXT_USE_OWN_CARET