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 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
662 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
664 BeginBatchUndo(_("Delete Text"));
666 long newPos
= m_caretPosition
;
668 DeleteSelectedContent(& newPos
);
670 // Submit range in character positions, which are greater than caret positions,
671 // so subtract 1 for deleted character and add 1 for conversion to character position.
674 bool processed
= false;
677 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
680 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
686 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
691 if (GetLastPosition() == -1)
695 m_caretPosition
= -1;
697 SetDefaultStyleToCursorStyle();
700 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
702 wxRichTextEvent
cmdEvent(
703 wxEVT_COMMAND_RICHTEXT_DELETE
,
705 cmdEvent
.SetEventObject(this);
706 cmdEvent
.SetFlags(flags
);
707 cmdEvent
.SetPosition(m_caretPosition
+1);
708 GetEventHandler()->ProcessEvent(cmdEvent
);
718 if (event
.GetKeyCode() == WXK_LEFT
||
719 event
.GetKeyCode() == WXK_RIGHT
||
720 event
.GetKeyCode() == WXK_UP
||
721 event
.GetKeyCode() == WXK_DOWN
||
722 event
.GetKeyCode() == WXK_HOME
||
723 event
.GetKeyCode() == WXK_PAGEUP
||
724 event
.GetKeyCode() == WXK_PAGEDOWN
||
725 event
.GetKeyCode() == WXK_END
||
727 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
728 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
729 event
.GetKeyCode() == WXK_NUMPAD_UP
||
730 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
731 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
732 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
733 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
734 event
.GetKeyCode() == WXK_NUMPAD_END
)
736 KeyboardNavigate(event
.GetKeyCode(), flags
);
740 // all the other keys modify the controls contents which shouldn't be
741 // possible if we're read-only
748 if (event
.GetKeyCode() == WXK_RETURN
)
750 BeginBatchUndo(_("Insert Text"));
752 long newPos
= m_caretPosition
;
754 DeleteSelectedContent(& newPos
);
756 if (event
.ShiftDown())
759 text
= wxRichTextLineBreakChar
;
760 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
761 m_caretAtLineStart
= true;
765 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
768 SetDefaultStyleToCursorStyle();
770 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
772 wxRichTextEvent
cmdEvent(
773 wxEVT_COMMAND_RICHTEXT_RETURN
,
775 cmdEvent
.SetEventObject(this);
776 cmdEvent
.SetFlags(flags
);
777 cmdEvent
.SetPosition(newPos
+1);
779 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
781 // Generate conventional event
782 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
783 InitCommandEvent(textEvent
);
785 GetEventHandler()->ProcessEvent(textEvent
);
789 else if (event
.GetKeyCode() == WXK_BACK
)
791 BeginBatchUndo(_("Delete Text"));
793 long newPos
= m_caretPosition
;
795 DeleteSelectedContent(& newPos
);
797 // Submit range in character positions, which are greater than caret positions,
798 // so subtract 1 for deleted character and add 1 for conversion to character position.
801 bool processed
= false;
804 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
807 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
813 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
818 if (GetLastPosition() == -1)
822 m_caretPosition
= -1;
824 SetDefaultStyleToCursorStyle();
827 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
829 wxRichTextEvent
cmdEvent(
830 wxEVT_COMMAND_RICHTEXT_DELETE
,
832 cmdEvent
.SetEventObject(this);
833 cmdEvent
.SetFlags(flags
);
834 cmdEvent
.SetPosition(m_caretPosition
+1);
835 GetEventHandler()->ProcessEvent(cmdEvent
);
839 else if (event
.GetKeyCode() == WXK_DELETE
)
841 BeginBatchUndo(_("Delete Text"));
843 long newPos
= m_caretPosition
;
845 DeleteSelectedContent(& newPos
);
847 // Submit range in character positions, which are greater than caret positions,
848 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
850 bool processed
= false;
853 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
854 if (pos
!= -1 && (pos
> newPos
))
856 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
862 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
867 if (GetLastPosition() == -1)
871 m_caretPosition
= -1;
873 SetDefaultStyleToCursorStyle();
876 wxRichTextEvent
cmdEvent(
877 wxEVT_COMMAND_RICHTEXT_DELETE
,
879 cmdEvent
.SetEventObject(this);
880 cmdEvent
.SetFlags(flags
);
881 cmdEvent
.SetPosition(m_caretPosition
+1);
882 GetEventHandler()->ProcessEvent(cmdEvent
);
888 long keycode
= event
.GetKeyCode();
961 case WXK_NUMPAD_SPACE
:
963 case WXK_NUMPAD_ENTER
:
968 case WXK_NUMPAD_HOME
:
969 case WXK_NUMPAD_LEFT
:
971 case WXK_NUMPAD_RIGHT
:
972 case WXK_NUMPAD_DOWN
:
973 case WXK_NUMPAD_PAGEUP
:
974 case WXK_NUMPAD_PAGEDOWN
:
976 case WXK_NUMPAD_BEGIN
:
977 case WXK_NUMPAD_INSERT
:
978 case WXK_NUMPAD_DELETE
:
979 case WXK_NUMPAD_EQUAL
:
980 case WXK_NUMPAD_MULTIPLY
:
982 case WXK_NUMPAD_SEPARATOR
:
983 case WXK_NUMPAD_SUBTRACT
:
984 case WXK_NUMPAD_DECIMAL
:
985 case WXK_WINDOWS_LEFT
:
993 if (event
.CmdDown() || event
.AltDown())
999 wxRichTextEvent
cmdEvent(
1000 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1002 cmdEvent
.SetEventObject(this);
1003 cmdEvent
.SetFlags(flags
);
1005 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1007 cmdEvent
.SetCharacter((wxChar
) keycode
);
1009 cmdEvent
.SetPosition(m_caretPosition
+1);
1011 if (keycode
== wxT('\t'))
1013 // See if we need to promote or demote the selection or paragraph at the cursor
1014 // position, instead of inserting a tab.
1015 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1016 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1017 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1019 wxRichTextRange range
;
1021 range
= GetSelectionRange();
1023 range
= para
->GetRange().FromInternal();
1025 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1027 PromoteList(promoteBy
, range
, NULL
);
1029 GetEventHandler()->ProcessEvent(cmdEvent
);
1035 BeginBatchUndo(_("Insert Text"));
1037 long newPos
= m_caretPosition
;
1038 DeleteSelectedContent(& newPos
);
1041 wxString str
= event
.GetUnicodeKey();
1043 wxString str
= (wxChar
) event
.GetKeyCode();
1045 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1049 SetDefaultStyleToCursorStyle();
1050 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1052 GetEventHandler()->ProcessEvent(cmdEvent
);
1060 /// Delete content if there is a selection, e.g. when pressing a key.
1061 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1065 long pos
= m_selectionRange
.GetStart();
1066 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1067 m_selectionRange
.SetRange(-2, -2);
1077 /// Keyboard navigation
1081 Left: left one character
1082 Right: right one character
1085 Ctrl-Left: left one word
1086 Ctrl-Right: right one word
1087 Ctrl-Up: previous paragraph start
1088 Ctrl-Down: next start of paragraph
1091 Ctrl-Home: start of document
1092 Ctrl-End: end of document
1093 Page-Up: Up a screen
1094 Page-Down: Down a screen
1098 Ctrl-Alt-PgUp: Start of window
1099 Ctrl-Alt-PgDn: End of window
1100 F8: Start selection mode
1101 Esc: End selection mode
1103 Adding Shift does the above but starts/extends selection.
1108 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1110 bool success
= false;
1112 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1114 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1115 success
= WordRight(1, flags
);
1117 success
= MoveRight(1, flags
);
1119 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1121 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1122 success
= WordLeft(1, flags
);
1124 success
= MoveLeft(1, flags
);
1126 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1128 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1129 success
= MoveToParagraphStart(flags
);
1131 success
= MoveUp(1, flags
);
1133 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1135 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1136 success
= MoveToParagraphEnd(flags
);
1138 success
= MoveDown(1, flags
);
1140 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1142 success
= PageUp(1, flags
);
1144 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1146 success
= PageDown(1, flags
);
1148 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1150 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1151 success
= MoveHome(flags
);
1153 success
= MoveToLineStart(flags
);
1155 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1157 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1158 success
= MoveEnd(flags
);
1160 success
= MoveToLineEnd(flags
);
1165 ScrollIntoView(m_caretPosition
, keyCode
);
1166 SetDefaultStyleToCursorStyle();
1172 /// Extend the selection. Selections are in caret positions.
1173 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1175 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1177 if (oldPos
== newPos
)
1180 wxRichTextRange oldSelection
= m_selectionRange
;
1182 // If not currently selecting, start selecting
1183 if (m_selectionRange
.GetStart() == -2)
1185 m_selectionAnchor
= oldPos
;
1187 if (oldPos
> newPos
)
1188 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1190 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1194 // Always ensure that the selection range start is greater than
1196 if (newPos
> m_selectionAnchor
)
1197 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1198 else if (newPos
== m_selectionAnchor
)
1199 m_selectionRange
= wxRichTextRange(-2, -2);
1201 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1204 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1206 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1208 wxLogDebug(wxT("Strange selection range"));
1217 /// Scroll into view, returning true if we scrolled.
1218 /// This takes a _caret_ position.
1219 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1221 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1227 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1229 int startXUnits
, startYUnits
;
1230 GetViewStart(& startXUnits
, & startYUnits
);
1231 int startY
= startYUnits
* ppuY
;
1234 GetVirtualSize(& sx
, & sy
);
1240 wxRect rect
= line
->GetRect();
1242 bool scrolled
= false;
1244 wxSize clientSize
= GetClientSize();
1247 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1248 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1249 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1250 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1252 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1254 // Make it scroll so this item is at the bottom
1256 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1257 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1259 // If we're still off the screen, scroll another line down
1260 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1263 if (startYUnits
!= yUnits
)
1265 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1269 else if (rect
.y
< startY
)
1271 // Make it scroll so this item is at the top
1274 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1276 if (startYUnits
!= yUnits
)
1278 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1284 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1285 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1286 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1287 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1289 if (rect
.y
< startY
)
1291 // Make it scroll so this item is at the top
1294 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1296 if (startYUnits
!= yUnits
)
1298 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1302 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1304 // Make it scroll so this item is at the bottom
1306 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1307 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1309 // If we're still off the screen, scroll another line down
1310 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1313 if (startYUnits
!= yUnits
)
1315 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1321 #if !wxRICHTEXT_USE_OWN_CARET
1329 /// Is the given position visible on the screen?
1330 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1332 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1338 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1341 GetViewStart(& startX
, & startY
);
1343 startY
= startY
* ppuY
;
1345 wxRect rect
= line
->GetRect();
1346 wxSize clientSize
= GetClientSize();
1348 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1351 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1353 m_caretPosition
= position
;
1354 m_caretAtLineStart
= showAtLineStart
;
1357 /// Move caret one visual step forward: this may mean setting a flag
1358 /// and keeping the same position if we're going from the end of one line
1359 /// to the start of the next, which may be the exact same caret position.
1360 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1362 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1364 // Only do the check if we're not at the end of the paragraph (where things work OK
1366 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1368 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1372 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1374 // We're at the end of a line. See whether we need to
1375 // stay at the same actual caret position but change visual
1376 // position, or not.
1377 if (oldPosition
== lineRange
.GetEnd())
1379 if (m_caretAtLineStart
)
1381 // We're already at the start of the line, so actually move on now.
1382 m_caretPosition
= oldPosition
+ 1;
1383 m_caretAtLineStart
= false;
1387 // We're showing at the end of the line, so keep to
1388 // the same position but indicate that we're to show
1389 // at the start of the next line.
1390 m_caretPosition
= oldPosition
;
1391 m_caretAtLineStart
= true;
1393 SetDefaultStyleToCursorStyle();
1399 SetDefaultStyleToCursorStyle();
1402 /// Move caret one visual step backward: this may mean setting a flag
1403 /// and keeping the same position if we're going from the end of one line
1404 /// to the start of the next, which may be the exact same caret position.
1405 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1407 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1409 // Only do the check if we're not at the start of the paragraph (where things work OK
1411 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1413 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1417 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1419 // We're at the start of a line. See whether we need to
1420 // stay at the same actual caret position but change visual
1421 // position, or not.
1422 if (oldPosition
== lineRange
.GetStart())
1424 m_caretPosition
= oldPosition
-1;
1425 m_caretAtLineStart
= true;
1428 else if (oldPosition
== lineRange
.GetEnd())
1430 if (m_caretAtLineStart
)
1432 // We're at the start of the line, so keep the same caret position
1433 // but clear the start-of-line flag.
1434 m_caretPosition
= oldPosition
;
1435 m_caretAtLineStart
= false;
1439 // We're showing at the end of the line, so go back
1440 // to the previous character position.
1441 m_caretPosition
= oldPosition
- 1;
1443 SetDefaultStyleToCursorStyle();
1449 SetDefaultStyleToCursorStyle();
1453 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1455 long endPos
= GetBuffer().GetRange().GetEnd();
1457 if (m_caretPosition
+ noPositions
< endPos
)
1459 long oldPos
= m_caretPosition
;
1460 long newPos
= m_caretPosition
+ noPositions
;
1462 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1466 // Determine by looking at oldPos and m_caretPosition whether
1467 // we moved from the end of a line to the start of the next line, in which case
1468 // we want to adjust the caret position such that it is positioned at the
1469 // start of the next line, rather than jumping past the first character of the
1471 if (noPositions
== 1 && !extendSel
)
1472 MoveCaretForward(oldPos
);
1474 SetCaretPosition(newPos
);
1477 SetDefaultStyleToCursorStyle();
1486 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1490 if (m_caretPosition
> startPos
- noPositions
+ 1)
1492 long oldPos
= m_caretPosition
;
1493 long newPos
= m_caretPosition
- noPositions
;
1494 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1498 if (noPositions
== 1 && !extendSel
)
1499 MoveCaretBack(oldPos
);
1501 SetCaretPosition(newPos
);
1504 SetDefaultStyleToCursorStyle();
1513 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1515 return MoveDown(- noLines
, flags
);
1519 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1524 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1525 wxPoint pt
= GetCaret()->GetPosition();
1526 long newLine
= lineNumber
+ noLines
;
1528 if (lineNumber
!= -1)
1532 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1534 if (newLine
> lastLine
)
1544 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1547 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1553 wxClientDC
dc(this);
1555 dc
.SetFont(GetFont());
1557 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1559 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1561 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1562 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1563 // so we view the caret at the start of the line.
1564 bool caretLineStart
= false;
1565 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1567 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1568 wxRichTextRange lineRange
;
1570 lineRange
= thisLine
->GetAbsoluteRange();
1572 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1575 caretLineStart
= true;
1579 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1580 if (para
&& para
->GetRange().GetStart() == newPos
)
1585 long newSelEnd
= newPos
;
1587 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1591 SetCaretPosition(newPos
, caretLineStart
);
1593 SetDefaultStyleToCursorStyle();
1601 /// Move to the end of the paragraph
1602 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1604 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1607 long newPos
= para
->GetRange().GetEnd() - 1;
1608 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1612 SetCaretPosition(newPos
);
1614 SetDefaultStyleToCursorStyle();
1622 /// Move to the start of the paragraph
1623 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1625 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1628 long newPos
= para
->GetRange().GetStart() - 1;
1629 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1633 SetCaretPosition(newPos
);
1635 SetDefaultStyleToCursorStyle();
1643 /// Move to the end of the line
1644 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1646 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1650 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1651 long newPos
= lineRange
.GetEnd();
1652 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1656 SetCaretPosition(newPos
);
1658 SetDefaultStyleToCursorStyle();
1666 /// Move to the start of the line
1667 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1669 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1672 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1673 long newPos
= lineRange
.GetStart()-1;
1675 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1679 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1681 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1683 SetDefaultStyleToCursorStyle();
1691 /// Move to the start of the buffer
1692 bool wxRichTextCtrl::MoveHome(int flags
)
1694 if (m_caretPosition
!= -1)
1696 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1700 SetCaretPosition(-1);
1702 SetDefaultStyleToCursorStyle();
1710 /// Move to the end of the buffer
1711 bool wxRichTextCtrl::MoveEnd(int flags
)
1713 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1715 if (m_caretPosition
!= endPos
)
1717 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1721 SetCaretPosition(endPos
);
1723 SetDefaultStyleToCursorStyle();
1731 /// Move noPages pages up
1732 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1734 return PageDown(- noPages
, flags
);
1737 /// Move noPages pages down
1738 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1740 // Calculate which line occurs noPages * screen height further down.
1741 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1744 wxSize clientSize
= GetClientSize();
1745 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1747 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1750 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1751 long pos
= lineRange
.GetStart()-1;
1752 if (pos
!= m_caretPosition
)
1754 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1756 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1760 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1762 SetDefaultStyleToCursorStyle();
1772 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1774 return str
== wxT(" ") || str
== wxT("\t");
1777 // Finds the caret position for the next word
1778 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1780 long endPos
= GetBuffer().GetRange().GetEnd();
1784 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1786 // First skip current text to space
1787 while (i
< endPos
&& i
> -1)
1789 // i is in character, not caret positions
1790 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1791 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1792 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1796 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1803 while (i
< endPos
&& i
> -1)
1805 // i is in character, not caret positions
1806 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1807 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1808 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1809 return wxMax(-1, i
);
1811 if (text
.empty()) // End of paragraph, or maybe an image
1812 return wxMax(-1, i
- 1);
1813 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1817 // Convert to caret position
1818 return wxMax(-1, i
- 1);
1827 long i
= m_caretPosition
;
1829 // First skip white space
1830 while (i
< endPos
&& i
> -1)
1832 // i is in character, not caret positions
1833 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1834 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1836 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1838 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1843 // Next skip current text to space
1844 while (i
< endPos
&& i
> -1)
1846 // i is in character, not caret positions
1847 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1848 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1849 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1852 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1865 /// Move n words left
1866 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1868 long pos
= FindNextWordPosition(-1);
1869 if (pos
!= m_caretPosition
)
1871 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1873 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1877 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1879 SetDefaultStyleToCursorStyle();
1887 /// Move n words right
1888 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1890 long pos
= FindNextWordPosition(1);
1891 if (pos
!= m_caretPosition
)
1893 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1895 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1899 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1901 SetDefaultStyleToCursorStyle();
1910 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1912 // Only do sizing optimization for large buffers
1913 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1915 m_fullLayoutRequired
= true;
1916 m_fullLayoutTime
= wxGetLocalTimeMillis();
1917 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1918 LayoutContent(true /* onlyVisibleRect */);
1921 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1923 #if wxRICHTEXT_BUFFERED_PAINTING
1931 /// Idle-time processing
1932 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1934 #if wxRICHTEXT_USE_OWN_CARET
1935 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1937 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1943 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1945 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1947 m_fullLayoutRequired
= false;
1948 m_fullLayoutTime
= 0;
1949 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1950 ShowPosition(m_fullLayoutSavedPosition
);
1954 if (m_caretPositionForDefaultStyle
!= -2)
1956 // If the caret position has changed, no longer reflect the default style
1958 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1959 m_caretPositionForDefaultStyle
= -2;
1966 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1968 #if wxRICHTEXT_USE_OWN_CARET
1969 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1972 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1979 /// Set up scrollbars, e.g. after a resize
1980 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1985 if (GetBuffer().IsEmpty())
1987 SetScrollbars(0, 0, 0, 0, 0, 0);
1991 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1992 // of pixels. See e.g. wxVScrolledWindow for ideas.
1993 int pixelsPerUnit
= 5;
1994 wxSize clientSize
= GetClientSize();
1996 int maxHeight
= GetBuffer().GetCachedSize().y
;
1998 // Round up so we have at least maxHeight pixels
1999 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2001 int startX
= 0, startY
= 0;
2003 GetViewStart(& startX
, & startY
);
2005 int maxPositionX
= 0;
2006 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2008 int newStartX
= wxMin(maxPositionX
, startX
);
2009 int newStartY
= wxMin(maxPositionY
, startY
);
2011 int oldPPUX
, oldPPUY
;
2012 int oldStartX
, oldStartY
;
2013 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2014 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2015 GetViewStart(& oldStartX
, & oldStartY
);
2016 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2018 oldVirtualSizeY
/= oldPPUY
;
2020 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2023 // Don't set scrollbars if there were none before, and there will be none now.
2024 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2027 // Move to previous scroll position if
2029 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2032 /// Paint the background
2033 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2035 wxColour backgroundColour
= GetBackgroundColour();
2036 if (!backgroundColour
.Ok())
2037 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2039 // Clear the background
2040 dc
.SetBrush(wxBrush(backgroundColour
));
2041 dc
.SetPen(*wxTRANSPARENT_PEN
);
2042 wxRect
windowRect(GetClientSize());
2043 windowRect
.x
-= 2; windowRect
.y
-= 2;
2044 windowRect
.width
+= 4; windowRect
.height
+= 4;
2046 // We need to shift the rectangle to take into account
2047 // scrolling. Converting device to logical coordinates.
2048 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2049 dc
.DrawRectangle(windowRect
);
2052 #if wxRICHTEXT_BUFFERED_PAINTING
2053 /// Recreate buffer bitmap if necessary
2054 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2057 if (sz
== wxDefaultSize
)
2058 sz
= GetClientSize();
2060 if (sz
.x
< 1 || sz
.y
< 1)
2063 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2064 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2065 return m_bufferBitmap
.Ok();
2069 // ----------------------------------------------------------------------------
2070 // file IO functions
2071 // ----------------------------------------------------------------------------
2073 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2075 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2077 m_filename
= filename
;
2080 SetInsertionPoint(0);
2083 SetupScrollbars(true);
2085 wxTextCtrl::SendTextUpdatedEvent(this);
2091 wxLogError(_("File couldn't be loaded."));
2097 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2099 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2101 m_filename
= filename
;
2108 wxLogError(_("The text couldn't be saved."));
2113 // ----------------------------------------------------------------------------
2114 // wxRichTextCtrl specific functionality
2115 // ----------------------------------------------------------------------------
2117 /// Add a new paragraph of text to the end of the buffer
2118 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2120 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2126 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2128 wxRichTextRange range
= GetBuffer().AddImage(image
);
2133 // ----------------------------------------------------------------------------
2134 // selection and ranges
2135 // ----------------------------------------------------------------------------
2137 void wxRichTextCtrl::SelectAll()
2139 SetSelection(0, GetLastPosition()+1);
2140 m_selectionAnchor
= -1;
2144 void wxRichTextCtrl::SelectNone()
2146 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2148 wxRichTextRange oldSelection
= m_selectionRange
;
2150 m_selectionRange
= wxRichTextRange(-2, -2);
2152 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2154 m_selectionAnchor
= -2;
2157 static bool wxIsWordDelimiter(const wxString
& text
)
2159 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2162 /// Select the word at the given character position
2163 bool wxRichTextCtrl::SelectWord(long position
)
2165 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2168 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2172 if (position
== para
->GetRange().GetEnd())
2175 long positionStart
= position
;
2176 long positionEnd
= position
;
2178 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2180 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2181 if (wxIsWordDelimiter(text
))
2187 if (positionStart
< para
->GetRange().GetStart())
2188 positionStart
= para
->GetRange().GetStart();
2190 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2192 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2193 if (wxIsWordDelimiter(text
))
2199 if (positionEnd
>= para
->GetRange().GetEnd())
2200 positionEnd
= para
->GetRange().GetEnd();
2202 if (positionEnd
< positionStart
)
2205 SetSelection(positionStart
, positionEnd
+1);
2207 if (positionStart
>= 0)
2209 MoveCaret(positionStart
-1, true);
2210 SetDefaultStyleToCursorStyle();
2216 wxString
wxRichTextCtrl::GetStringSelection() const
2219 GetSelection(&from
, &to
);
2221 return GetRange(from
, to
);
2224 // ----------------------------------------------------------------------------
2226 // ----------------------------------------------------------------------------
2228 wxTextCtrlHitTestResult
2229 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2231 // implement in terms of the other overload as the native ports typically
2232 // can get the position and not (x, y) pair directly (although wxUniv
2233 // directly gets x and y -- and so overrides this method as well)
2235 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2237 if ( rc
!= wxTE_HT_UNKNOWN
)
2239 PositionToXY(pos
, x
, y
);
2245 wxTextCtrlHitTestResult
2246 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2249 wxClientDC
dc((wxRichTextCtrl
*) this);
2250 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2252 // Buffer uses logical position (relative to start of buffer)
2254 wxPoint pt2
= GetLogicalPoint(pt
);
2256 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2258 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2259 return wxTE_HT_BEFORE
;
2260 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2261 return wxTE_HT_BEYOND
;
2262 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2263 return wxTE_HT_ON_TEXT
;
2265 return wxTE_HT_UNKNOWN
;
2268 // ----------------------------------------------------------------------------
2269 // set/get the controls text
2270 // ----------------------------------------------------------------------------
2272 wxString
wxRichTextCtrl::GetValue() const
2274 return GetBuffer().GetText();
2277 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2279 // Public API for range is different from internals
2280 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2283 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2285 // Don't call Clear here, since it always sends a text updated event
2286 m_buffer
.ResetAndClearCommands();
2287 m_buffer
.SetDirty(true);
2288 m_caretPosition
= -1;
2289 m_caretPositionForDefaultStyle
= -2;
2290 m_caretAtLineStart
= false;
2291 m_selectionRange
.SetRange(-2, -2);
2301 if (!value
.IsEmpty())
2303 // Remove empty paragraph
2304 GetBuffer().Clear();
2305 DoWriteText(value
, flags
);
2307 // for compatibility, don't move the cursor when doing SetValue()
2308 SetInsertionPoint(0);
2312 // still send an event for consistency
2313 if (flags
& SetValue_SendEvent
)
2314 wxTextCtrl::SendTextUpdatedEvent(this);
2319 void wxRichTextCtrl::WriteText(const wxString
& value
)
2324 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2326 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2328 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2330 if ( flags
& SetValue_SendEvent
)
2331 wxTextCtrl::SendTextUpdatedEvent(this);
2334 void wxRichTextCtrl::AppendText(const wxString
& text
)
2336 SetInsertionPointEnd();
2341 /// Write an image at the current insertion point
2342 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2344 wxRichTextImageBlock imageBlock
;
2346 wxImage image2
= image
;
2347 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2348 return WriteImage(imageBlock
);
2353 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2355 wxRichTextImageBlock imageBlock
;
2358 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2359 return WriteImage(imageBlock
);
2364 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2366 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2369 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2373 wxRichTextImageBlock imageBlock
;
2375 wxImage image
= bitmap
.ConvertToImage();
2376 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2377 return WriteImage(imageBlock
);
2383 /// Insert a newline (actually paragraph) at the current insertion point.
2384 bool wxRichTextCtrl::Newline()
2386 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2389 /// Insert a line break at the current insertion point.
2390 bool wxRichTextCtrl::LineBreak()
2393 text
= wxRichTextLineBreakChar
;
2394 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2397 // ----------------------------------------------------------------------------
2398 // Clipboard operations
2399 // ----------------------------------------------------------------------------
2401 void wxRichTextCtrl::Copy()
2405 wxRichTextRange range
= GetInternalSelectionRange();
2406 GetBuffer().CopyToClipboard(range
);
2410 void wxRichTextCtrl::Cut()
2414 wxRichTextRange range
= GetInternalSelectionRange();
2415 GetBuffer().CopyToClipboard(range
);
2417 DeleteSelectedContent();
2423 void wxRichTextCtrl::Paste()
2427 BeginBatchUndo(_("Paste"));
2429 long newPos
= m_caretPosition
;
2430 DeleteSelectedContent(& newPos
);
2432 GetBuffer().PasteFromClipboard(newPos
);
2438 void wxRichTextCtrl::DeleteSelection()
2440 if (CanDeleteSelection())
2442 DeleteSelectedContent();
2446 bool wxRichTextCtrl::HasSelection() const
2448 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2451 bool wxRichTextCtrl::CanCopy() const
2453 // Can copy if there's a selection
2454 return HasSelection();
2457 bool wxRichTextCtrl::CanCut() const
2459 return HasSelection() && IsEditable();
2462 bool wxRichTextCtrl::CanPaste() const
2464 if ( !IsEditable() )
2467 return GetBuffer().CanPasteFromClipboard();
2470 bool wxRichTextCtrl::CanDeleteSelection() const
2472 return HasSelection() && IsEditable();
2476 // ----------------------------------------------------------------------------
2478 // ----------------------------------------------------------------------------
2480 void wxRichTextCtrl::SetEditable(bool editable
)
2482 m_editable
= editable
;
2485 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2489 m_caretPosition
= pos
- 1;
2494 void wxRichTextCtrl::SetInsertionPointEnd()
2496 long pos
= GetLastPosition();
2497 SetInsertionPoint(pos
);
2500 long wxRichTextCtrl::GetInsertionPoint() const
2502 return m_caretPosition
+1;
2505 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2507 return GetBuffer().GetRange().GetEnd();
2510 // If the return values from and to are the same, there is no
2512 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2514 *from
= m_selectionRange
.GetStart();
2515 *to
= m_selectionRange
.GetEnd();
2516 if ((*to
) != -1 && (*to
) != -2)
2520 bool wxRichTextCtrl::IsEditable() const
2525 // ----------------------------------------------------------------------------
2527 // ----------------------------------------------------------------------------
2529 void wxRichTextCtrl::SetSelection(long from
, long to
)
2531 // if from and to are both -1, it means (in wxWidgets) that all text should
2533 if ( (from
== -1) && (to
== -1) )
2536 to
= GetLastPosition()+1;
2539 DoSetSelection(from
, to
);
2542 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2550 wxRichTextRange oldSelection
= m_selectionRange
;
2551 m_selectionAnchor
= from
;
2552 m_selectionRange
.SetRange(from
, to
-1);
2554 m_caretPosition
= from
-1;
2556 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2561 // ----------------------------------------------------------------------------
2563 // ----------------------------------------------------------------------------
2565 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2566 const wxString
& value
)
2568 BeginBatchUndo(_("Replace"));
2570 DeleteSelectedContent();
2572 DoWriteText(value
, SetValue_SelectionOnly
);
2577 void wxRichTextCtrl::Remove(long from
, long to
)
2581 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2588 bool wxRichTextCtrl::IsModified() const
2590 return m_buffer
.IsModified();
2593 void wxRichTextCtrl::MarkDirty()
2595 m_buffer
.Modify(true);
2598 void wxRichTextCtrl::DiscardEdits()
2600 m_caretPositionForDefaultStyle
= -2;
2601 m_buffer
.Modify(false);
2602 m_buffer
.GetCommandProcessor()->ClearCommands();
2605 int wxRichTextCtrl::GetNumberOfLines() const
2607 return GetBuffer().GetParagraphCount();
2610 // ----------------------------------------------------------------------------
2611 // Positions <-> coords
2612 // ----------------------------------------------------------------------------
2614 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2616 return GetBuffer().XYToPosition(x
, y
);
2619 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2621 return GetBuffer().PositionToXY(pos
, x
, y
);
2624 // ----------------------------------------------------------------------------
2626 // ----------------------------------------------------------------------------
2628 void wxRichTextCtrl::ShowPosition(long pos
)
2630 if (!IsPositionVisible(pos
))
2631 ScrollIntoView(pos
-1, WXK_DOWN
);
2634 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2636 return GetBuffer().GetParagraphLength(lineNo
);
2639 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2641 return GetBuffer().GetParagraphText(lineNo
);
2644 // ----------------------------------------------------------------------------
2646 // ----------------------------------------------------------------------------
2648 void wxRichTextCtrl::Undo()
2652 GetCommandProcessor()->Undo();
2656 void wxRichTextCtrl::Redo()
2660 GetCommandProcessor()->Redo();
2664 bool wxRichTextCtrl::CanUndo() const
2666 return GetCommandProcessor()->CanUndo();
2669 bool wxRichTextCtrl::CanRedo() const
2671 return GetCommandProcessor()->CanRedo();
2674 // ----------------------------------------------------------------------------
2675 // implementation details
2676 // ----------------------------------------------------------------------------
2678 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2680 SetValue(event
.GetString());
2681 GetEventHandler()->ProcessEvent(event
);
2684 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2686 // By default, load the first file into the text window.
2687 if (event
.GetNumberOfFiles() > 0)
2689 LoadFile(event
.GetFiles()[0]);
2693 wxSize
wxRichTextCtrl::DoGetBestSize() const
2695 return wxSize(10, 10);
2698 // ----------------------------------------------------------------------------
2699 // standard handlers for standard edit menu events
2700 // ----------------------------------------------------------------------------
2702 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2707 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2712 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2717 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2722 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2727 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2732 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2734 event
.Enable( CanCut() );
2737 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2739 event
.Enable( CanCopy() );
2742 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2744 event
.Enable( CanDeleteSelection() );
2747 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2749 event
.Enable( CanPaste() );
2752 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2754 event
.Enable( CanUndo() );
2755 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2758 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2760 event
.Enable( CanRedo() );
2761 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2764 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2769 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2771 event
.Enable(GetLastPosition() > 0);
2774 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2776 if (event
.GetEventObject() != this)
2784 m_contextMenu
= new wxMenu
;
2785 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2786 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2787 m_contextMenu
->AppendSeparator();
2788 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2789 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2790 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2791 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2792 m_contextMenu
->AppendSeparator();
2793 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2795 PopupMenu(m_contextMenu
);
2799 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2801 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2804 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2806 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2809 // extended style setting operation with flags including:
2810 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2811 // see richtextbuffer.h for more details.
2813 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2815 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2818 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2820 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2823 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2825 return GetBuffer().GetDefaultStyle();
2828 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2830 return GetBuffer().GetStyle(position
, style
);
2833 // get the common set of styles for the range
2834 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2836 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2839 /// Get the content (uncombined) attributes for this position.
2840 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2842 return GetBuffer().GetUncombinedStyle(position
, style
);
2845 /// Set font, and also the buffer attributes
2846 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2848 wxControl::SetFont(font
);
2850 wxTextAttr attr
= GetBuffer().GetAttributes();
2852 GetBuffer().SetBasicStyle(attr
);
2854 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2860 /// Transform logical to physical
2861 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2864 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2869 /// Transform physical to logical
2870 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2873 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2878 /// Position the caret
2879 void wxRichTextCtrl::PositionCaret()
2884 //wxLogDebug(wxT("PositionCaret"));
2887 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2889 wxPoint newPt
= caretRect
.GetPosition();
2890 wxSize newSz
= caretRect
.GetSize();
2891 wxPoint pt
= GetPhysicalPoint(newPt
);
2892 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2895 if (GetCaret()->GetSize() != newSz
)
2896 GetCaret()->SetSize(newSz
);
2897 GetCaret()->Move(pt
);
2903 /// Get the caret height and position for the given character position
2904 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2906 wxClientDC
dc(this);
2907 dc
.SetFont(GetFont());
2914 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2916 // Caret height can't be zero
2918 height
= dc
.GetCharHeight();
2920 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2927 /// Gets the line for the visible caret position. If the caret is
2928 /// shown at the very end of the line, it means the next character is actually
2929 /// on the following line. So let's get the line we're expecting to find
2930 /// if this is the case.
2931 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2933 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2934 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2937 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2938 if (caretPosition
== lineRange
.GetStart()-1 &&
2939 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2941 if (!m_caretAtLineStart
)
2942 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2949 /// Move the caret to the given character position
2950 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2952 if (GetBuffer().GetDirty())
2955 if (pos
<= GetBuffer().GetRange().GetEnd())
2957 SetCaretPosition(pos
, showAtLineStart
);
2967 /// Layout the buffer: which we must do before certain operations, such as
2968 /// setting the caret position.
2969 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2971 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2973 wxRect
availableSpace(GetClientSize());
2974 if (availableSpace
.width
== 0)
2975 availableSpace
.width
= 10;
2976 if (availableSpace
.height
== 0)
2977 availableSpace
.height
= 10;
2979 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2980 if (onlyVisibleRect
)
2982 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2983 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2986 wxClientDC
dc(this);
2987 dc
.SetFont(GetFont());
2991 GetBuffer().Defragment();
2992 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2993 GetBuffer().Layout(dc
, availableSpace
, flags
);
2994 GetBuffer().SetDirty(false);
3003 /// Is all of the selection bold?
3004 bool wxRichTextCtrl::IsSelectionBold()
3009 wxRichTextRange range
= GetSelectionRange();
3010 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3011 attr
.SetFontWeight(wxBOLD
);
3013 return HasCharacterAttributes(range
, attr
);
3017 // If no selection, then we need to combine current style with default style
3018 // to see what the effect would be if we started typing.
3020 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3022 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3023 if (GetStyle(pos
, attr
))
3025 if (IsDefaultStyleShowing())
3026 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3027 return attr
.GetFontWeight() == wxBOLD
;
3033 /// Is all of the selection italics?
3034 bool wxRichTextCtrl::IsSelectionItalics()
3038 wxRichTextRange range
= GetSelectionRange();
3040 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3041 attr
.SetFontStyle(wxITALIC
);
3043 return HasCharacterAttributes(range
, attr
);
3047 // If no selection, then we need to combine current style with default style
3048 // to see what the effect would be if we started typing.
3050 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3052 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3053 if (GetStyle(pos
, attr
))
3055 if (IsDefaultStyleShowing())
3056 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3057 return attr
.GetFontStyle() == wxITALIC
;
3063 /// Is all of the selection underlined?
3064 bool wxRichTextCtrl::IsSelectionUnderlined()
3068 wxRichTextRange range
= GetSelectionRange();
3070 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3071 attr
.SetFontUnderlined(true);
3073 return HasCharacterAttributes(range
, attr
);
3077 // If no selection, then we need to combine current style with default style
3078 // to see what the effect would be if we started typing.
3080 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3081 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3083 if (GetStyle(pos
, attr
))
3085 if (IsDefaultStyleShowing())
3086 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3087 return attr
.GetFontUnderlined();
3093 /// Apply bold to the selection
3094 bool wxRichTextCtrl::ApplyBoldToSelection()
3097 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3098 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
3101 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3104 wxRichTextAttr current
= GetDefaultStyleEx();
3105 current
.Apply(attr
);
3106 SetAndShowDefaultStyle(current
);
3111 /// Apply italic to the selection
3112 bool wxRichTextCtrl::ApplyItalicToSelection()
3115 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3116 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
3119 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3122 wxRichTextAttr current
= GetDefaultStyleEx();
3123 current
.Apply(attr
);
3124 SetAndShowDefaultStyle(current
);
3129 /// Apply underline to the selection
3130 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3133 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3134 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3137 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3140 wxRichTextAttr current
= GetDefaultStyleEx();
3141 current
.Apply(attr
);
3142 SetAndShowDefaultStyle(current
);
3147 /// Is all of the selection aligned according to the specified flag?
3148 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3150 wxRichTextRange range
;
3152 range
= GetSelectionRange();
3154 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3157 attr
.SetAlignment(alignment
);
3159 return HasParagraphAttributes(range
, attr
);
3162 /// Apply alignment to the selection
3163 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3166 attr
.SetAlignment(alignment
);
3168 return SetStyle(GetSelectionRange(), attr
);
3171 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3173 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3178 /// Apply a named style to the selection
3179 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3181 // Flags are defined within each definition, so only certain
3182 // attributes are applied.
3183 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3185 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3187 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3189 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3191 wxRichTextRange range
;
3194 range
= GetSelectionRange();
3197 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3198 range
= wxRichTextRange(pos
, pos
+1);
3201 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3204 // Make sure the attr has the style name
3205 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3207 attr
.SetParagraphStyleName(def
->GetName());
3209 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3210 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3211 // to change its style independently.
3212 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3215 attr
.SetCharacterStyleName(def
->GetName());
3218 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3221 wxRichTextAttr current
= GetDefaultStyleEx();
3222 current
.Apply(attr
);
3223 SetAndShowDefaultStyle(current
);
3228 /// Apply the style sheet to the buffer, for example if the styles have changed.
3229 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3232 styleSheet
= GetBuffer().GetStyleSheet();
3236 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3238 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3246 /// Sets the default style to the style under the cursor
3247 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3250 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3252 // If at the start of a paragraph, use the next position.
3253 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3255 if (GetUncombinedStyle(pos
, attr
))
3257 SetDefaultStyle(attr
);
3264 /// Returns the first visible position in the current view
3265 long wxRichTextCtrl::GetFirstVisiblePosition() const
3267 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3269 return line
->GetAbsoluteRange().GetStart();
3274 /// Get the first visible point in the window
3275 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3278 int startXUnits
, startYUnits
;
3280 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3281 GetViewStart(& startXUnits
, & startYUnits
);
3283 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3286 /// The adjusted caret position is the character position adjusted to take
3287 /// into account whether we're at the start of a paragraph, in which case
3288 /// style information should be taken from the next position, not current one.
3289 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3291 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3293 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3298 /// Get/set the selection range in character positions. -1, -1 means no selection.
3299 /// The range is in API convention, i.e. a single character selection is denoted
3301 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3303 wxRichTextRange range
= GetInternalSelectionRange();
3304 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3305 range
.SetEnd(range
.GetEnd() + 1);
3309 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3311 wxRichTextRange
range1(range
);
3312 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3313 range1
.SetEnd(range1
.GetEnd() - 1);
3315 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3317 SetInternalSelectionRange(range1
);
3321 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3323 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3326 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3328 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3331 /// Clear list for given range
3332 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3334 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3337 /// Number/renumber any list elements in the given range
3338 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3340 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3343 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3345 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3348 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3349 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3351 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3354 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3356 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3359 /// Deletes the content in the given range
3360 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3362 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3365 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3367 if (sm_availableFontNames
.GetCount() == 0)
3369 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3370 sm_availableFontNames
.Sort();
3372 return sm_availableFontNames
;
3375 void wxRichTextCtrl::ClearAvailableFontNames()
3377 sm_availableFontNames
.Clear();
3380 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3382 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3384 wxTextAttrEx basicStyle
= GetBasicStyle();
3385 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3386 SetBasicStyle(basicStyle
);
3387 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3392 // Refresh the area affected by a selection change
3393 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3395 // Calculate the refresh rectangle - just the affected lines
3396 long firstPos
, lastPos
;
3397 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3399 firstPos
= newSelection
.GetStart();
3400 lastPos
= newSelection
.GetEnd();
3402 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3404 firstPos
= oldSelection
.GetStart();
3405 lastPos
= oldSelection
.GetEnd();
3407 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3413 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3414 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3417 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3418 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3420 if (firstLine
&& lastLine
)
3422 wxSize clientSize
= GetClientSize();
3423 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3424 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3427 pt1
.y
= wxMax(0, pt1
.y
);
3429 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3431 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3432 RefreshRect(rect
, false);
3440 #if wxRICHTEXT_USE_OWN_CARET
3442 // ----------------------------------------------------------------------------
3443 // initialization and destruction
3444 // ----------------------------------------------------------------------------
3446 void wxRichTextCaret::Init()
3452 m_richTextCtrl
= NULL
;
3453 m_needsUpdate
= false;
3456 wxRichTextCaret::~wxRichTextCaret()
3460 // ----------------------------------------------------------------------------
3461 // showing/hiding/moving the caret (base class interface)
3462 // ----------------------------------------------------------------------------
3464 void wxRichTextCaret::DoShow()
3469 void wxRichTextCaret::DoHide()
3474 void wxRichTextCaret::DoMove()
3480 if (m_xOld
!= -1 && m_yOld
!= -1)
3484 wxRect
rect(GetPosition(), GetSize());
3485 m_richTextCtrl
->RefreshRect(rect
, false);
3494 void wxRichTextCaret::DoSize()
3496 int countVisible
= m_countVisible
;
3497 if (countVisible
> 0)
3503 if (countVisible
> 0)
3505 m_countVisible
= countVisible
;
3510 // ----------------------------------------------------------------------------
3511 // handling the focus
3512 // ----------------------------------------------------------------------------
3514 void wxRichTextCaret::OnSetFocus()
3522 void wxRichTextCaret::OnKillFocus()
3527 // ----------------------------------------------------------------------------
3528 // drawing the caret
3529 // ----------------------------------------------------------------------------
3531 void wxRichTextCaret::Refresh()
3535 wxRect
rect(GetPosition(), GetSize());
3536 m_richTextCtrl
->RefreshRect(rect
, false);
3540 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3542 dc
->SetPen( *wxBLACK_PEN
);
3544 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3545 dc
->SetPen(*wxBLACK_PEN
);
3547 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3548 // done under wxGTK - no idea why
3549 //dc->SetLogicalFunction(wxINVERT);
3551 wxPoint
pt(m_x
, m_y
);
3555 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3557 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3560 // wxRICHTEXT_USE_OWN_CARET