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
:
996 if (event
.CmdDown() || event
.AltDown())
1003 wxRichTextEvent
cmdEvent(
1004 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1006 cmdEvent
.SetEventObject(this);
1007 cmdEvent
.SetFlags(flags
);
1009 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1011 cmdEvent
.SetCharacter((wxChar
) keycode
);
1013 cmdEvent
.SetPosition(m_caretPosition
+1);
1015 if (keycode
== wxT('\t'))
1017 // See if we need to promote or demote the selection or paragraph at the cursor
1018 // position, instead of inserting a tab.
1019 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1020 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1021 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1023 wxRichTextRange range
;
1025 range
= GetSelectionRange();
1027 range
= para
->GetRange().FromInternal();
1029 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1031 PromoteList(promoteBy
, range
, NULL
);
1033 GetEventHandler()->ProcessEvent(cmdEvent
);
1039 BeginBatchUndo(_("Insert Text"));
1041 long newPos
= m_caretPosition
;
1042 DeleteSelectedContent(& newPos
);
1045 wxString str
= event
.GetUnicodeKey();
1047 wxString str
= (wxChar
) event
.GetKeyCode();
1049 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1053 SetDefaultStyleToCursorStyle();
1054 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1056 GetEventHandler()->ProcessEvent(cmdEvent
);
1064 /// Delete content if there is a selection, e.g. when pressing a key.
1065 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1069 long pos
= m_selectionRange
.GetStart();
1070 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1071 m_selectionRange
.SetRange(-2, -2);
1081 /// Keyboard navigation
1085 Left: left one character
1086 Right: right one character
1089 Ctrl-Left: left one word
1090 Ctrl-Right: right one word
1091 Ctrl-Up: previous paragraph start
1092 Ctrl-Down: next start of paragraph
1095 Ctrl-Home: start of document
1096 Ctrl-End: end of document
1097 Page-Up: Up a screen
1098 Page-Down: Down a screen
1102 Ctrl-Alt-PgUp: Start of window
1103 Ctrl-Alt-PgDn: End of window
1104 F8: Start selection mode
1105 Esc: End selection mode
1107 Adding Shift does the above but starts/extends selection.
1112 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1114 bool success
= false;
1116 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1118 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1119 success
= WordRight(1, flags
);
1121 success
= MoveRight(1, flags
);
1123 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1125 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1126 success
= WordLeft(1, flags
);
1128 success
= MoveLeft(1, flags
);
1130 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1132 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1133 success
= MoveToParagraphStart(flags
);
1135 success
= MoveUp(1, flags
);
1137 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1139 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1140 success
= MoveToParagraphEnd(flags
);
1142 success
= MoveDown(1, flags
);
1144 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1146 success
= PageUp(1, flags
);
1148 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1150 success
= PageDown(1, flags
);
1152 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1154 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1155 success
= MoveHome(flags
);
1157 success
= MoveToLineStart(flags
);
1159 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1161 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1162 success
= MoveEnd(flags
);
1164 success
= MoveToLineEnd(flags
);
1169 ScrollIntoView(m_caretPosition
, keyCode
);
1170 SetDefaultStyleToCursorStyle();
1176 /// Extend the selection. Selections are in caret positions.
1177 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1179 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1181 if (oldPos
== newPos
)
1184 wxRichTextRange oldSelection
= m_selectionRange
;
1186 // If not currently selecting, start selecting
1187 if (m_selectionRange
.GetStart() == -2)
1189 m_selectionAnchor
= oldPos
;
1191 if (oldPos
> newPos
)
1192 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1194 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1198 // Always ensure that the selection range start is greater than
1200 if (newPos
> m_selectionAnchor
)
1201 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1202 else if (newPos
== m_selectionAnchor
)
1203 m_selectionRange
= wxRichTextRange(-2, -2);
1205 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1208 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1210 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1212 wxLogDebug(wxT("Strange selection range"));
1221 /// Scroll into view, returning true if we scrolled.
1222 /// This takes a _caret_ position.
1223 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1225 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1231 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1233 int startXUnits
, startYUnits
;
1234 GetViewStart(& startXUnits
, & startYUnits
);
1235 int startY
= startYUnits
* ppuY
;
1238 GetVirtualSize(& sx
, & sy
);
1244 wxRect rect
= line
->GetRect();
1246 bool scrolled
= false;
1248 wxSize clientSize
= GetClientSize();
1251 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1252 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1253 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1254 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1256 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1258 // Make it scroll so this item is at the bottom
1260 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1261 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1263 // If we're still off the screen, scroll another line down
1264 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1267 if (startYUnits
!= yUnits
)
1269 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1273 else if (rect
.y
< startY
)
1275 // Make it scroll so this item is at the top
1278 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1280 if (startYUnits
!= yUnits
)
1282 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1288 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1289 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1290 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1291 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1293 if (rect
.y
< startY
)
1295 // Make it scroll so this item is at the top
1298 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1300 if (startYUnits
!= yUnits
)
1302 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1306 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1308 // Make it scroll so this item is at the bottom
1310 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1311 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1313 // If we're still off the screen, scroll another line down
1314 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1317 if (startYUnits
!= yUnits
)
1319 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1325 #if !wxRICHTEXT_USE_OWN_CARET
1333 /// Is the given position visible on the screen?
1334 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1336 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1342 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1345 GetViewStart(& startX
, & startY
);
1347 startY
= startY
* ppuY
;
1349 wxRect rect
= line
->GetRect();
1350 wxSize clientSize
= GetClientSize();
1352 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1355 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1357 m_caretPosition
= position
;
1358 m_caretAtLineStart
= showAtLineStart
;
1361 /// Move caret one visual step forward: this may mean setting a flag
1362 /// and keeping the same position if we're going from the end of one line
1363 /// to the start of the next, which may be the exact same caret position.
1364 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1366 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1368 // Only do the check if we're not at the end of the paragraph (where things work OK
1370 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1372 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1376 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1378 // We're at the end of a line. See whether we need to
1379 // stay at the same actual caret position but change visual
1380 // position, or not.
1381 if (oldPosition
== lineRange
.GetEnd())
1383 if (m_caretAtLineStart
)
1385 // We're already at the start of the line, so actually move on now.
1386 m_caretPosition
= oldPosition
+ 1;
1387 m_caretAtLineStart
= false;
1391 // We're showing at the end of the line, so keep to
1392 // the same position but indicate that we're to show
1393 // at the start of the next line.
1394 m_caretPosition
= oldPosition
;
1395 m_caretAtLineStart
= true;
1397 SetDefaultStyleToCursorStyle();
1403 SetDefaultStyleToCursorStyle();
1406 /// Move caret one visual step backward: this may mean setting a flag
1407 /// and keeping the same position if we're going from the end of one line
1408 /// to the start of the next, which may be the exact same caret position.
1409 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1411 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1413 // Only do the check if we're not at the start of the paragraph (where things work OK
1415 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1417 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1421 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1423 // We're at the start of a line. See whether we need to
1424 // stay at the same actual caret position but change visual
1425 // position, or not.
1426 if (oldPosition
== lineRange
.GetStart())
1428 m_caretPosition
= oldPosition
-1;
1429 m_caretAtLineStart
= true;
1432 else if (oldPosition
== lineRange
.GetEnd())
1434 if (m_caretAtLineStart
)
1436 // We're at the start of the line, so keep the same caret position
1437 // but clear the start-of-line flag.
1438 m_caretPosition
= oldPosition
;
1439 m_caretAtLineStart
= false;
1443 // We're showing at the end of the line, so go back
1444 // to the previous character position.
1445 m_caretPosition
= oldPosition
- 1;
1447 SetDefaultStyleToCursorStyle();
1453 SetDefaultStyleToCursorStyle();
1457 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1459 long endPos
= GetBuffer().GetRange().GetEnd();
1461 if (m_caretPosition
+ noPositions
< endPos
)
1463 long oldPos
= m_caretPosition
;
1464 long newPos
= m_caretPosition
+ noPositions
;
1466 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1470 // Determine by looking at oldPos and m_caretPosition whether
1471 // we moved from the end of a line to the start of the next line, in which case
1472 // we want to adjust the caret position such that it is positioned at the
1473 // start of the next line, rather than jumping past the first character of the
1475 if (noPositions
== 1 && !extendSel
)
1476 MoveCaretForward(oldPos
);
1478 SetCaretPosition(newPos
);
1481 SetDefaultStyleToCursorStyle();
1490 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1494 if (m_caretPosition
> startPos
- noPositions
+ 1)
1496 long oldPos
= m_caretPosition
;
1497 long newPos
= m_caretPosition
- noPositions
;
1498 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1502 if (noPositions
== 1 && !extendSel
)
1503 MoveCaretBack(oldPos
);
1505 SetCaretPosition(newPos
);
1508 SetDefaultStyleToCursorStyle();
1517 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1519 return MoveDown(- noLines
, flags
);
1523 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1528 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1529 wxPoint pt
= GetCaret()->GetPosition();
1530 long newLine
= lineNumber
+ noLines
;
1532 if (lineNumber
!= -1)
1536 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1538 if (newLine
> lastLine
)
1548 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1551 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1557 wxClientDC
dc(this);
1559 dc
.SetFont(GetFont());
1561 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1563 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1565 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1566 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1567 // so we view the caret at the start of the line.
1568 bool caretLineStart
= false;
1569 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1571 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1572 wxRichTextRange lineRange
;
1574 lineRange
= thisLine
->GetAbsoluteRange();
1576 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1579 caretLineStart
= true;
1583 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1584 if (para
&& para
->GetRange().GetStart() == newPos
)
1589 long newSelEnd
= newPos
;
1591 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1595 SetCaretPosition(newPos
, caretLineStart
);
1597 SetDefaultStyleToCursorStyle();
1605 /// Move to the end of the paragraph
1606 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1608 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1611 long newPos
= para
->GetRange().GetEnd() - 1;
1612 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1616 SetCaretPosition(newPos
);
1618 SetDefaultStyleToCursorStyle();
1626 /// Move to the start of the paragraph
1627 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1629 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1632 long newPos
= para
->GetRange().GetStart() - 1;
1633 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1637 SetCaretPosition(newPos
);
1639 SetDefaultStyleToCursorStyle();
1647 /// Move to the end of the line
1648 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1650 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1654 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1655 long newPos
= lineRange
.GetEnd();
1656 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1660 SetCaretPosition(newPos
);
1662 SetDefaultStyleToCursorStyle();
1670 /// Move to the start of the line
1671 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1673 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1676 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1677 long newPos
= lineRange
.GetStart()-1;
1679 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1683 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1685 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1687 SetDefaultStyleToCursorStyle();
1695 /// Move to the start of the buffer
1696 bool wxRichTextCtrl::MoveHome(int flags
)
1698 if (m_caretPosition
!= -1)
1700 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1704 SetCaretPosition(-1);
1706 SetDefaultStyleToCursorStyle();
1714 /// Move to the end of the buffer
1715 bool wxRichTextCtrl::MoveEnd(int flags
)
1717 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1719 if (m_caretPosition
!= endPos
)
1721 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1725 SetCaretPosition(endPos
);
1727 SetDefaultStyleToCursorStyle();
1735 /// Move noPages pages up
1736 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1738 return PageDown(- noPages
, flags
);
1741 /// Move noPages pages down
1742 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1744 // Calculate which line occurs noPages * screen height further down.
1745 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1748 wxSize clientSize
= GetClientSize();
1749 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1751 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1754 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1755 long pos
= lineRange
.GetStart()-1;
1756 if (pos
!= m_caretPosition
)
1758 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1760 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1764 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1766 SetDefaultStyleToCursorStyle();
1776 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1778 return str
== wxT(" ") || str
== wxT("\t");
1781 // Finds the caret position for the next word
1782 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1784 long endPos
= GetBuffer().GetRange().GetEnd();
1788 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1790 // First skip current text to space
1791 while (i
< endPos
&& i
> -1)
1793 // i is in character, not caret positions
1794 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1795 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1796 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1800 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1807 while (i
< endPos
&& i
> -1)
1809 // i is in character, not caret positions
1810 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1811 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1812 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1813 return wxMax(-1, i
);
1815 if (text
.empty()) // End of paragraph, or maybe an image
1816 return wxMax(-1, i
- 1);
1817 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1821 // Convert to caret position
1822 return wxMax(-1, i
- 1);
1831 long i
= m_caretPosition
;
1833 // First skip white space
1834 while (i
< endPos
&& i
> -1)
1836 // i is in character, not caret positions
1837 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1838 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1840 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1842 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1847 // Next skip current text to space
1848 while (i
< endPos
&& i
> -1)
1850 // i is in character, not caret positions
1851 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1852 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1853 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1856 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1869 /// Move n words left
1870 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1872 long pos
= FindNextWordPosition(-1);
1873 if (pos
!= m_caretPosition
)
1875 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1877 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1881 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1883 SetDefaultStyleToCursorStyle();
1891 /// Move n words right
1892 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1894 long pos
= FindNextWordPosition(1);
1895 if (pos
!= m_caretPosition
)
1897 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1899 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1903 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1905 SetDefaultStyleToCursorStyle();
1914 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1916 // Only do sizing optimization for large buffers
1917 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1919 m_fullLayoutRequired
= true;
1920 m_fullLayoutTime
= wxGetLocalTimeMillis();
1921 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1922 LayoutContent(true /* onlyVisibleRect */);
1925 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1927 #if wxRICHTEXT_BUFFERED_PAINTING
1935 /// Idle-time processing
1936 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1938 #if wxRICHTEXT_USE_OWN_CARET
1939 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1941 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1947 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1949 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1951 m_fullLayoutRequired
= false;
1952 m_fullLayoutTime
= 0;
1953 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1954 ShowPosition(m_fullLayoutSavedPosition
);
1958 if (m_caretPositionForDefaultStyle
!= -2)
1960 // If the caret position has changed, no longer reflect the default style
1962 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1963 m_caretPositionForDefaultStyle
= -2;
1970 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1972 #if wxRICHTEXT_USE_OWN_CARET
1973 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1976 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
1983 /// Set up scrollbars, e.g. after a resize
1984 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1989 if (GetBuffer().IsEmpty())
1991 SetScrollbars(0, 0, 0, 0, 0, 0);
1995 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1996 // of pixels. See e.g. wxVScrolledWindow for ideas.
1997 int pixelsPerUnit
= 5;
1998 wxSize clientSize
= GetClientSize();
2000 int maxHeight
= GetBuffer().GetCachedSize().y
;
2002 // Round up so we have at least maxHeight pixels
2003 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2005 int startX
= 0, startY
= 0;
2007 GetViewStart(& startX
, & startY
);
2009 int maxPositionX
= 0;
2010 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2012 int newStartX
= wxMin(maxPositionX
, startX
);
2013 int newStartY
= wxMin(maxPositionY
, startY
);
2015 int oldPPUX
, oldPPUY
;
2016 int oldStartX
, oldStartY
;
2017 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2018 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2019 GetViewStart(& oldStartX
, & oldStartY
);
2020 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2022 oldVirtualSizeY
/= oldPPUY
;
2024 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2027 // Don't set scrollbars if there were none before, and there will be none now.
2028 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2031 // Move to previous scroll position if
2033 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2036 /// Paint the background
2037 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2039 wxColour backgroundColour
= GetBackgroundColour();
2040 if (!backgroundColour
.Ok())
2041 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2043 // Clear the background
2044 dc
.SetBrush(wxBrush(backgroundColour
));
2045 dc
.SetPen(*wxTRANSPARENT_PEN
);
2046 wxRect
windowRect(GetClientSize());
2047 windowRect
.x
-= 2; windowRect
.y
-= 2;
2048 windowRect
.width
+= 4; windowRect
.height
+= 4;
2050 // We need to shift the rectangle to take into account
2051 // scrolling. Converting device to logical coordinates.
2052 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2053 dc
.DrawRectangle(windowRect
);
2056 #if wxRICHTEXT_BUFFERED_PAINTING
2057 /// Recreate buffer bitmap if necessary
2058 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2061 if (sz
== wxDefaultSize
)
2062 sz
= GetClientSize();
2064 if (sz
.x
< 1 || sz
.y
< 1)
2067 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2068 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2069 return m_bufferBitmap
.Ok();
2073 // ----------------------------------------------------------------------------
2074 // file IO functions
2075 // ----------------------------------------------------------------------------
2077 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2079 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2081 m_filename
= filename
;
2084 SetInsertionPoint(0);
2087 SetupScrollbars(true);
2089 wxTextCtrl::SendTextUpdatedEvent(this);
2095 wxLogError(_("File couldn't be loaded."));
2101 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2103 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2105 m_filename
= filename
;
2112 wxLogError(_("The text couldn't be saved."));
2117 // ----------------------------------------------------------------------------
2118 // wxRichTextCtrl specific functionality
2119 // ----------------------------------------------------------------------------
2121 /// Add a new paragraph of text to the end of the buffer
2122 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2124 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2130 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2132 wxRichTextRange range
= GetBuffer().AddImage(image
);
2137 // ----------------------------------------------------------------------------
2138 // selection and ranges
2139 // ----------------------------------------------------------------------------
2141 void wxRichTextCtrl::SelectAll()
2143 SetSelection(0, GetLastPosition()+1);
2144 m_selectionAnchor
= -1;
2148 void wxRichTextCtrl::SelectNone()
2150 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2152 wxRichTextRange oldSelection
= m_selectionRange
;
2154 m_selectionRange
= wxRichTextRange(-2, -2);
2156 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2158 m_selectionAnchor
= -2;
2161 static bool wxIsWordDelimiter(const wxString
& text
)
2163 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2166 /// Select the word at the given character position
2167 bool wxRichTextCtrl::SelectWord(long position
)
2169 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2172 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2176 if (position
== para
->GetRange().GetEnd())
2179 long positionStart
= position
;
2180 long positionEnd
= position
;
2182 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2184 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2185 if (wxIsWordDelimiter(text
))
2191 if (positionStart
< para
->GetRange().GetStart())
2192 positionStart
= para
->GetRange().GetStart();
2194 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2196 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2197 if (wxIsWordDelimiter(text
))
2203 if (positionEnd
>= para
->GetRange().GetEnd())
2204 positionEnd
= para
->GetRange().GetEnd();
2206 if (positionEnd
< positionStart
)
2209 SetSelection(positionStart
, positionEnd
+1);
2211 if (positionStart
>= 0)
2213 MoveCaret(positionStart
-1, true);
2214 SetDefaultStyleToCursorStyle();
2220 wxString
wxRichTextCtrl::GetStringSelection() const
2223 GetSelection(&from
, &to
);
2225 return GetRange(from
, to
);
2228 // ----------------------------------------------------------------------------
2230 // ----------------------------------------------------------------------------
2232 wxTextCtrlHitTestResult
2233 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2235 // implement in terms of the other overload as the native ports typically
2236 // can get the position and not (x, y) pair directly (although wxUniv
2237 // directly gets x and y -- and so overrides this method as well)
2239 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2241 if ( rc
!= wxTE_HT_UNKNOWN
)
2243 PositionToXY(pos
, x
, y
);
2249 wxTextCtrlHitTestResult
2250 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2253 wxClientDC
dc((wxRichTextCtrl
*) this);
2254 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2256 // Buffer uses logical position (relative to start of buffer)
2258 wxPoint pt2
= GetLogicalPoint(pt
);
2260 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2262 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2263 return wxTE_HT_BEFORE
;
2264 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2265 return wxTE_HT_BEYOND
;
2266 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2267 return wxTE_HT_ON_TEXT
;
2269 return wxTE_HT_UNKNOWN
;
2272 // ----------------------------------------------------------------------------
2273 // set/get the controls text
2274 // ----------------------------------------------------------------------------
2276 wxString
wxRichTextCtrl::GetValue() const
2278 return GetBuffer().GetText();
2281 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2283 // Public API for range is different from internals
2284 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2287 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2289 // Don't call Clear here, since it always sends a text updated event
2290 m_buffer
.ResetAndClearCommands();
2291 m_buffer
.SetDirty(true);
2292 m_caretPosition
= -1;
2293 m_caretPositionForDefaultStyle
= -2;
2294 m_caretAtLineStart
= false;
2295 m_selectionRange
.SetRange(-2, -2);
2305 if (!value
.IsEmpty())
2307 // Remove empty paragraph
2308 GetBuffer().Clear();
2309 DoWriteText(value
, flags
);
2311 // for compatibility, don't move the cursor when doing SetValue()
2312 SetInsertionPoint(0);
2316 // still send an event for consistency
2317 if (flags
& SetValue_SendEvent
)
2318 wxTextCtrl::SendTextUpdatedEvent(this);
2323 void wxRichTextCtrl::WriteText(const wxString
& value
)
2328 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2330 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2332 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2334 if ( flags
& SetValue_SendEvent
)
2335 wxTextCtrl::SendTextUpdatedEvent(this);
2338 void wxRichTextCtrl::AppendText(const wxString
& text
)
2340 SetInsertionPointEnd();
2345 /// Write an image at the current insertion point
2346 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2348 wxRichTextImageBlock imageBlock
;
2350 wxImage image2
= image
;
2351 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2352 return WriteImage(imageBlock
);
2357 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2359 wxRichTextImageBlock imageBlock
;
2362 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2363 return WriteImage(imageBlock
);
2368 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2370 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2373 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2377 wxRichTextImageBlock imageBlock
;
2379 wxImage image
= bitmap
.ConvertToImage();
2380 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2381 return WriteImage(imageBlock
);
2387 /// Insert a newline (actually paragraph) at the current insertion point.
2388 bool wxRichTextCtrl::Newline()
2390 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2393 /// Insert a line break at the current insertion point.
2394 bool wxRichTextCtrl::LineBreak()
2397 text
= wxRichTextLineBreakChar
;
2398 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2401 // ----------------------------------------------------------------------------
2402 // Clipboard operations
2403 // ----------------------------------------------------------------------------
2405 void wxRichTextCtrl::Copy()
2409 wxRichTextRange range
= GetInternalSelectionRange();
2410 GetBuffer().CopyToClipboard(range
);
2414 void wxRichTextCtrl::Cut()
2418 wxRichTextRange range
= GetInternalSelectionRange();
2419 GetBuffer().CopyToClipboard(range
);
2421 DeleteSelectedContent();
2427 void wxRichTextCtrl::Paste()
2431 BeginBatchUndo(_("Paste"));
2433 long newPos
= m_caretPosition
;
2434 DeleteSelectedContent(& newPos
);
2436 GetBuffer().PasteFromClipboard(newPos
);
2442 void wxRichTextCtrl::DeleteSelection()
2444 if (CanDeleteSelection())
2446 DeleteSelectedContent();
2450 bool wxRichTextCtrl::HasSelection() const
2452 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2455 bool wxRichTextCtrl::CanCopy() const
2457 // Can copy if there's a selection
2458 return HasSelection();
2461 bool wxRichTextCtrl::CanCut() const
2463 return HasSelection() && IsEditable();
2466 bool wxRichTextCtrl::CanPaste() const
2468 if ( !IsEditable() )
2471 return GetBuffer().CanPasteFromClipboard();
2474 bool wxRichTextCtrl::CanDeleteSelection() const
2476 return HasSelection() && IsEditable();
2480 // ----------------------------------------------------------------------------
2482 // ----------------------------------------------------------------------------
2484 void wxRichTextCtrl::SetEditable(bool editable
)
2486 m_editable
= editable
;
2489 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2493 m_caretPosition
= pos
- 1;
2498 void wxRichTextCtrl::SetInsertionPointEnd()
2500 long pos
= GetLastPosition();
2501 SetInsertionPoint(pos
);
2504 long wxRichTextCtrl::GetInsertionPoint() const
2506 return m_caretPosition
+1;
2509 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2511 return GetBuffer().GetRange().GetEnd();
2514 // If the return values from and to are the same, there is no
2516 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2518 *from
= m_selectionRange
.GetStart();
2519 *to
= m_selectionRange
.GetEnd();
2520 if ((*to
) != -1 && (*to
) != -2)
2524 bool wxRichTextCtrl::IsEditable() const
2529 // ----------------------------------------------------------------------------
2531 // ----------------------------------------------------------------------------
2533 void wxRichTextCtrl::SetSelection(long from
, long to
)
2535 // if from and to are both -1, it means (in wxWidgets) that all text should
2537 if ( (from
== -1) && (to
== -1) )
2540 to
= GetLastPosition()+1;
2543 DoSetSelection(from
, to
);
2546 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2554 wxRichTextRange oldSelection
= m_selectionRange
;
2555 m_selectionAnchor
= from
;
2556 m_selectionRange
.SetRange(from
, to
-1);
2558 m_caretPosition
= from
-1;
2560 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2565 // ----------------------------------------------------------------------------
2567 // ----------------------------------------------------------------------------
2569 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2570 const wxString
& value
)
2572 BeginBatchUndo(_("Replace"));
2574 DeleteSelectedContent();
2576 DoWriteText(value
, SetValue_SelectionOnly
);
2581 void wxRichTextCtrl::Remove(long from
, long to
)
2585 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2592 bool wxRichTextCtrl::IsModified() const
2594 return m_buffer
.IsModified();
2597 void wxRichTextCtrl::MarkDirty()
2599 m_buffer
.Modify(true);
2602 void wxRichTextCtrl::DiscardEdits()
2604 m_caretPositionForDefaultStyle
= -2;
2605 m_buffer
.Modify(false);
2606 m_buffer
.GetCommandProcessor()->ClearCommands();
2609 int wxRichTextCtrl::GetNumberOfLines() const
2611 return GetBuffer().GetParagraphCount();
2614 // ----------------------------------------------------------------------------
2615 // Positions <-> coords
2616 // ----------------------------------------------------------------------------
2618 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2620 return GetBuffer().XYToPosition(x
, y
);
2623 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2625 return GetBuffer().PositionToXY(pos
, x
, y
);
2628 // ----------------------------------------------------------------------------
2630 // ----------------------------------------------------------------------------
2632 void wxRichTextCtrl::ShowPosition(long pos
)
2634 if (!IsPositionVisible(pos
))
2635 ScrollIntoView(pos
-1, WXK_DOWN
);
2638 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2640 return GetBuffer().GetParagraphLength(lineNo
);
2643 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2645 return GetBuffer().GetParagraphText(lineNo
);
2648 // ----------------------------------------------------------------------------
2650 // ----------------------------------------------------------------------------
2652 void wxRichTextCtrl::Undo()
2656 GetCommandProcessor()->Undo();
2660 void wxRichTextCtrl::Redo()
2664 GetCommandProcessor()->Redo();
2668 bool wxRichTextCtrl::CanUndo() const
2670 return GetCommandProcessor()->CanUndo();
2673 bool wxRichTextCtrl::CanRedo() const
2675 return GetCommandProcessor()->CanRedo();
2678 // ----------------------------------------------------------------------------
2679 // implementation details
2680 // ----------------------------------------------------------------------------
2682 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2684 SetValue(event
.GetString());
2685 GetEventHandler()->ProcessEvent(event
);
2688 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2690 // By default, load the first file into the text window.
2691 if (event
.GetNumberOfFiles() > 0)
2693 LoadFile(event
.GetFiles()[0]);
2697 wxSize
wxRichTextCtrl::DoGetBestSize() const
2699 return wxSize(10, 10);
2702 // ----------------------------------------------------------------------------
2703 // standard handlers for standard edit menu events
2704 // ----------------------------------------------------------------------------
2706 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2711 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2716 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2721 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2726 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2731 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2736 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2738 event
.Enable( CanCut() );
2741 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2743 event
.Enable( CanCopy() );
2746 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2748 event
.Enable( CanDeleteSelection() );
2751 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2753 event
.Enable( CanPaste() );
2756 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2758 event
.Enable( CanUndo() );
2759 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2762 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2764 event
.Enable( CanRedo() );
2765 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2768 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2773 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2775 event
.Enable(GetLastPosition() > 0);
2778 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2780 if (event
.GetEventObject() != this)
2788 m_contextMenu
= new wxMenu
;
2789 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2790 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2791 m_contextMenu
->AppendSeparator();
2792 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2793 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2794 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2795 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2796 m_contextMenu
->AppendSeparator();
2797 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2799 PopupMenu(m_contextMenu
);
2803 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2805 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2808 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2810 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2813 // extended style setting operation with flags including:
2814 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2815 // see richtextbuffer.h for more details.
2817 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2819 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2822 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2824 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2827 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2829 return GetBuffer().GetDefaultStyle();
2832 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2834 return GetBuffer().GetStyle(position
, style
);
2837 // get the common set of styles for the range
2838 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2840 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2843 /// Get the content (uncombined) attributes for this position.
2844 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2846 return GetBuffer().GetUncombinedStyle(position
, style
);
2849 /// Set font, and also the buffer attributes
2850 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2852 wxControl::SetFont(font
);
2854 wxTextAttr attr
= GetBuffer().GetAttributes();
2856 GetBuffer().SetBasicStyle(attr
);
2858 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2864 /// Transform logical to physical
2865 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2868 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2873 /// Transform physical to logical
2874 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2877 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2882 /// Position the caret
2883 void wxRichTextCtrl::PositionCaret()
2888 //wxLogDebug(wxT("PositionCaret"));
2891 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2893 wxPoint newPt
= caretRect
.GetPosition();
2894 wxSize newSz
= caretRect
.GetSize();
2895 wxPoint pt
= GetPhysicalPoint(newPt
);
2896 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2899 if (GetCaret()->GetSize() != newSz
)
2900 GetCaret()->SetSize(newSz
);
2901 GetCaret()->Move(pt
);
2907 /// Get the caret height and position for the given character position
2908 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2910 wxClientDC
dc(this);
2911 dc
.SetFont(GetFont());
2918 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2920 // Caret height can't be zero
2922 height
= dc
.GetCharHeight();
2924 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2931 /// Gets the line for the visible caret position. If the caret is
2932 /// shown at the very end of the line, it means the next character is actually
2933 /// on the following line. So let's get the line we're expecting to find
2934 /// if this is the case.
2935 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2937 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2938 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2941 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2942 if (caretPosition
== lineRange
.GetStart()-1 &&
2943 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2945 if (!m_caretAtLineStart
)
2946 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2953 /// Move the caret to the given character position
2954 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2956 if (GetBuffer().GetDirty())
2959 if (pos
<= GetBuffer().GetRange().GetEnd())
2961 SetCaretPosition(pos
, showAtLineStart
);
2971 /// Layout the buffer: which we must do before certain operations, such as
2972 /// setting the caret position.
2973 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2975 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2977 wxRect
availableSpace(GetClientSize());
2978 if (availableSpace
.width
== 0)
2979 availableSpace
.width
= 10;
2980 if (availableSpace
.height
== 0)
2981 availableSpace
.height
= 10;
2983 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2984 if (onlyVisibleRect
)
2986 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2987 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2990 wxClientDC
dc(this);
2991 dc
.SetFont(GetFont());
2995 GetBuffer().Defragment();
2996 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2997 GetBuffer().Layout(dc
, availableSpace
, flags
);
2998 GetBuffer().SetDirty(false);
3007 /// Is all of the selection bold?
3008 bool wxRichTextCtrl::IsSelectionBold()
3013 wxRichTextRange range
= GetSelectionRange();
3014 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3015 attr
.SetFontWeight(wxBOLD
);
3017 return HasCharacterAttributes(range
, attr
);
3021 // If no selection, then we need to combine current style with default style
3022 // to see what the effect would be if we started typing.
3024 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3026 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3027 if (GetStyle(pos
, attr
))
3029 if (IsDefaultStyleShowing())
3030 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3031 return attr
.GetFontWeight() == wxBOLD
;
3037 /// Is all of the selection italics?
3038 bool wxRichTextCtrl::IsSelectionItalics()
3042 wxRichTextRange range
= GetSelectionRange();
3044 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3045 attr
.SetFontStyle(wxITALIC
);
3047 return HasCharacterAttributes(range
, attr
);
3051 // If no selection, then we need to combine current style with default style
3052 // to see what the effect would be if we started typing.
3054 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3056 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3057 if (GetStyle(pos
, attr
))
3059 if (IsDefaultStyleShowing())
3060 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3061 return attr
.GetFontStyle() == wxITALIC
;
3067 /// Is all of the selection underlined?
3068 bool wxRichTextCtrl::IsSelectionUnderlined()
3072 wxRichTextRange range
= GetSelectionRange();
3074 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3075 attr
.SetFontUnderlined(true);
3077 return HasCharacterAttributes(range
, attr
);
3081 // If no selection, then we need to combine current style with default style
3082 // to see what the effect would be if we started typing.
3084 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3085 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3087 if (GetStyle(pos
, attr
))
3089 if (IsDefaultStyleShowing())
3090 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3091 return attr
.GetFontUnderlined();
3097 /// Apply bold to the selection
3098 bool wxRichTextCtrl::ApplyBoldToSelection()
3101 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3102 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
3105 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3108 wxRichTextAttr current
= GetDefaultStyleEx();
3109 current
.Apply(attr
);
3110 SetAndShowDefaultStyle(current
);
3115 /// Apply italic to the selection
3116 bool wxRichTextCtrl::ApplyItalicToSelection()
3119 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3120 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
3123 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3126 wxRichTextAttr current
= GetDefaultStyleEx();
3127 current
.Apply(attr
);
3128 SetAndShowDefaultStyle(current
);
3133 /// Apply underline to the selection
3134 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3137 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3138 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3141 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3144 wxRichTextAttr current
= GetDefaultStyleEx();
3145 current
.Apply(attr
);
3146 SetAndShowDefaultStyle(current
);
3151 /// Is all of the selection aligned according to the specified flag?
3152 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3154 wxRichTextRange range
;
3156 range
= GetSelectionRange();
3158 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3161 attr
.SetAlignment(alignment
);
3163 return HasParagraphAttributes(range
, attr
);
3166 /// Apply alignment to the selection
3167 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3170 attr
.SetAlignment(alignment
);
3172 return SetStyle(GetSelectionRange(), attr
);
3175 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3177 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3182 /// Apply a named style to the selection
3183 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3185 // Flags are defined within each definition, so only certain
3186 // attributes are applied.
3187 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3189 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3191 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3193 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3195 wxRichTextRange range
;
3198 range
= GetSelectionRange();
3201 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3202 range
= wxRichTextRange(pos
, pos
+1);
3205 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3208 // Make sure the attr has the style name
3209 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3211 attr
.SetParagraphStyleName(def
->GetName());
3213 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3214 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3215 // to change its style independently.
3216 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3219 attr
.SetCharacterStyleName(def
->GetName());
3222 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3225 wxRichTextAttr current
= GetDefaultStyleEx();
3226 current
.Apply(attr
);
3227 SetAndShowDefaultStyle(current
);
3232 /// Apply the style sheet to the buffer, for example if the styles have changed.
3233 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3236 styleSheet
= GetBuffer().GetStyleSheet();
3240 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3242 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3250 /// Sets the default style to the style under the cursor
3251 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3254 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3256 // If at the start of a paragraph, use the next position.
3257 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3259 if (GetUncombinedStyle(pos
, attr
))
3261 SetDefaultStyle(attr
);
3268 /// Returns the first visible position in the current view
3269 long wxRichTextCtrl::GetFirstVisiblePosition() const
3271 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3273 return line
->GetAbsoluteRange().GetStart();
3278 /// Get the first visible point in the window
3279 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3282 int startXUnits
, startYUnits
;
3284 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3285 GetViewStart(& startXUnits
, & startYUnits
);
3287 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3290 /// The adjusted caret position is the character position adjusted to take
3291 /// into account whether we're at the start of a paragraph, in which case
3292 /// style information should be taken from the next position, not current one.
3293 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3295 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3297 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3302 /// Get/set the selection range in character positions. -1, -1 means no selection.
3303 /// The range is in API convention, i.e. a single character selection is denoted
3305 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3307 wxRichTextRange range
= GetInternalSelectionRange();
3308 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3309 range
.SetEnd(range
.GetEnd() + 1);
3313 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3315 wxRichTextRange
range1(range
);
3316 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3317 range1
.SetEnd(range1
.GetEnd() - 1);
3319 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3321 SetInternalSelectionRange(range1
);
3325 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3327 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3330 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3332 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3335 /// Clear list for given range
3336 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3338 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3341 /// Number/renumber any list elements in the given range
3342 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3344 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3347 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3349 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3352 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3353 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3355 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3358 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3360 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3363 /// Deletes the content in the given range
3364 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3366 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3369 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3371 if (sm_availableFontNames
.GetCount() == 0)
3373 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3374 sm_availableFontNames
.Sort();
3376 return sm_availableFontNames
;
3379 void wxRichTextCtrl::ClearAvailableFontNames()
3381 sm_availableFontNames
.Clear();
3384 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3386 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3388 wxTextAttrEx basicStyle
= GetBasicStyle();
3389 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3390 SetBasicStyle(basicStyle
);
3391 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3396 // Refresh the area affected by a selection change
3397 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3399 // Calculate the refresh rectangle - just the affected lines
3400 long firstPos
, lastPos
;
3401 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3403 firstPos
= newSelection
.GetStart();
3404 lastPos
= newSelection
.GetEnd();
3406 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3408 firstPos
= oldSelection
.GetStart();
3409 lastPos
= oldSelection
.GetEnd();
3411 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3417 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3418 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3421 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3422 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3424 if (firstLine
&& lastLine
)
3426 wxSize clientSize
= GetClientSize();
3427 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3428 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3431 pt1
.y
= wxMax(0, pt1
.y
);
3433 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3435 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3436 RefreshRect(rect
, false);
3444 #if wxRICHTEXT_USE_OWN_CARET
3446 // ----------------------------------------------------------------------------
3447 // initialization and destruction
3448 // ----------------------------------------------------------------------------
3450 void wxRichTextCaret::Init()
3456 m_richTextCtrl
= NULL
;
3457 m_needsUpdate
= false;
3460 wxRichTextCaret::~wxRichTextCaret()
3464 // ----------------------------------------------------------------------------
3465 // showing/hiding/moving the caret (base class interface)
3466 // ----------------------------------------------------------------------------
3468 void wxRichTextCaret::DoShow()
3473 void wxRichTextCaret::DoHide()
3478 void wxRichTextCaret::DoMove()
3484 if (m_xOld
!= -1 && m_yOld
!= -1)
3488 wxRect
rect(GetPosition(), GetSize());
3489 m_richTextCtrl
->RefreshRect(rect
, false);
3498 void wxRichTextCaret::DoSize()
3500 int countVisible
= m_countVisible
;
3501 if (countVisible
> 0)
3507 if (countVisible
> 0)
3509 m_countVisible
= countVisible
;
3514 // ----------------------------------------------------------------------------
3515 // handling the focus
3516 // ----------------------------------------------------------------------------
3518 void wxRichTextCaret::OnSetFocus()
3526 void wxRichTextCaret::OnKillFocus()
3531 // ----------------------------------------------------------------------------
3532 // drawing the caret
3533 // ----------------------------------------------------------------------------
3535 void wxRichTextCaret::Refresh()
3539 wxRect
rect(GetPosition(), GetSize());
3540 m_richTextCtrl
->RefreshRect(rect
, false);
3544 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3546 dc
->SetPen( *wxBLACK_PEN
);
3548 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3549 dc
->SetPen(*wxBLACK_PEN
);
3551 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3552 // done under wxGTK - no idea why
3553 //dc->SetLogicalFunction(wxINVERT);
3555 wxPoint
pt(m_x
, m_y
);
3559 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3561 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3564 // wxRICHTEXT_USE_OWN_CARET