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"
30 #include "wx/textfile.h"
32 #include "wx/filename.h"
33 #include "wx/dcbuffer.h"
34 #include "wx/arrimpl.cpp"
35 #include "wx/fontenum.h"
38 // DLL options compatibility check:
40 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
42 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
43 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
44 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
45 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
46 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN
, wxRichTextEvent
);
47 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER
, wxRichTextEvent
);
48 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE
, wxRichTextEvent
);
50 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
51 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
59 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
61 #if wxRICHTEXT_USE_OWN_CARET
66 * This implements a non-flashing cursor in case there
67 * are platform-specific problems with the generic caret.
68 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
71 class wxRichTextCaret
;
72 class wxRichTextCaretTimer
: public wxTimer
75 wxRichTextCaretTimer(wxRichTextCaret
* caret
)
79 virtual void Notify();
80 wxRichTextCaret
* m_caret
;
83 class wxRichTextCaret
: public wxCaret
88 // default - use Create()
89 wxRichTextCaret(): m_timer(this) { Init(); }
90 // creates a block caret associated with the given window
91 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
92 : wxCaret(window
, width
, height
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
93 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
94 : wxCaret(window
, size
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
96 virtual ~wxRichTextCaret();
101 // called by wxWindow (not using the event tables)
102 virtual void OnSetFocus();
103 virtual void OnKillFocus();
105 // draw the caret on the given DC
106 void DoDraw(wxDC
*dc
);
108 // get the visible count
109 int GetVisibleCount() const { return m_countVisible
; }
111 // delay repositioning
112 bool GetNeedsUpdate() const { return m_needsUpdate
; }
113 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
118 virtual void DoShow();
119 virtual void DoHide();
120 virtual void DoMove();
121 virtual void DoSize();
131 bool m_hasFocus
; // true => our window has focus
132 bool m_needsUpdate
; // must be repositioned
134 wxRichTextCaretTimer m_timer
;
135 wxRichTextCtrl
* m_richTextCtrl
;
139 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxTextCtrlBase
)
141 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
143 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxTextCtrlBase
)
144 EVT_PAINT(wxRichTextCtrl::OnPaint
)
145 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
146 EVT_IDLE(wxRichTextCtrl::OnIdle
)
147 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
148 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
149 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
150 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
151 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
152 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
153 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
154 EVT_CHAR(wxRichTextCtrl::OnChar
)
155 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
156 EVT_SIZE(wxRichTextCtrl::OnSize
)
157 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
158 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
159 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
160 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
161 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
163 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
164 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
166 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
167 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
169 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
170 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
172 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
173 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
175 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
176 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
178 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
179 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
181 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
182 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
189 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
191 wxRichTextCtrl::wxRichTextCtrl()
192 : wxScrollHelper(this)
197 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
199 const wxString
& value
,
203 const wxValidator
& validator
,
204 const wxString
& name
)
205 : wxScrollHelper(this)
208 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
212 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
213 const wxValidator
& validator
, const wxString
& name
)
217 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
218 style
|wxFULL_REPAINT_ON_RESIZE
,
224 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
227 // No physical scrolling, so we can preserve margins
228 EnableScrolling(false, false);
230 if (style
& wxTE_READONLY
)
233 // The base attributes must all have default values
234 wxTextAttr attributes
;
235 attributes
.SetFont(GetFont());
236 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
237 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
238 attributes
.SetLineSpacing(10);
239 attributes
.SetParagraphSpacingAfter(10);
240 attributes
.SetParagraphSpacingBefore(0);
242 SetBasicStyle(attributes
);
244 // The default attributes will be merged with base attributes, so
245 // can be empty to begin with
246 wxTextAttr defaultAttributes
;
247 SetDefaultStyle(defaultAttributes
);
249 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
250 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
253 GetBuffer().SetRichTextCtrl(this);
255 #if wxRICHTEXT_USE_OWN_CARET
256 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
258 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
261 // Tell the sizers to use the given or best size
262 SetInitialSize(size
);
264 #if wxRICHTEXT_BUFFERED_PAINTING
266 RecreateBuffer(size
);
269 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
270 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
272 SetCursor(m_textCursor
);
274 if (!value
.IsEmpty())
277 GetBuffer().AddEventHandler(this);
280 wxAcceleratorEntry entries
[6];
282 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
283 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
284 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
285 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
286 entries
[4].Set(wxACCEL_CMD
, (int) 'Z', wxID_UNDO
);
287 entries
[5].Set(wxACCEL_CMD
, (int) 'Y', wxID_REDO
);
289 wxAcceleratorTable
accel(6, entries
);
290 SetAcceleratorTable(accel
);
295 wxRichTextCtrl::~wxRichTextCtrl()
297 GetBuffer().RemoveEventHandler(this);
299 delete m_contextMenu
;
302 /// Member initialisation
303 void wxRichTextCtrl::Init()
305 m_contextMenu
= NULL
;
307 m_caretPosition
= -1;
308 m_selectionRange
.SetRange(-2, -2);
309 m_selectionAnchor
= -2;
311 m_caretAtLineStart
= false;
313 m_fullLayoutRequired
= false;
314 m_fullLayoutTime
= 0;
315 m_fullLayoutSavedPosition
= 0;
316 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
317 m_caretPositionForDefaultStyle
= -2;
320 void wxRichTextCtrl::DoThaw()
322 if (GetBuffer().GetDirty())
331 void wxRichTextCtrl::Clear()
333 m_buffer
.ResetAndClearCommands();
334 m_buffer
.SetDirty(true);
335 m_caretPosition
= -1;
336 m_caretPositionForDefaultStyle
= -2;
337 m_caretAtLineStart
= false;
338 m_selectionRange
.SetRange(-2, -2);
348 wxTextCtrl::SendTextUpdatedEvent(this);
352 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
354 #if !wxRICHTEXT_USE_OWN_CARET
355 if (GetCaret() && !IsFrozen())
360 #if wxRICHTEXT_BUFFERED_PAINTING
361 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
371 dc
.SetFont(GetFont());
373 // Paint the background
376 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
378 wxRect
drawingArea(GetUpdateRegion().GetBox());
379 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
381 wxRect
availableSpace(GetClientSize());
382 if (GetBuffer().GetDirty())
384 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
385 GetBuffer().SetDirty(false);
389 wxRect
clipRect(availableSpace
);
390 clipRect
.x
+= GetBuffer().GetLeftMargin();
391 clipRect
.y
+= GetBuffer().GetTopMargin();
392 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
393 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
394 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
395 dc
.SetClippingRegion(clipRect
);
397 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
399 dc
.DestroyClippingRegion();
401 #if wxRICHTEXT_USE_OWN_CARET
402 if (GetCaret()->IsVisible())
404 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
409 #if !wxRICHTEXT_USE_OWN_CARET
416 // Empty implementation, to prevent flicker
417 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
421 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
425 #if !wxRICHTEXT_USE_OWN_CARET
431 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
432 // Work around dropouts when control is focused
440 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
445 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
446 // Work around dropouts when control is focused
454 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
460 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
466 dc
.SetFont(GetFont());
469 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
471 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
473 m_dragStart
= event
.GetLogicalPosition(dc
);
477 bool caretAtLineStart
= false;
479 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
481 // If we're at the start of a line (but not first in para)
482 // then we should keep the caret showing at the start of the line
483 // by showing the m_caretAtLineStart flag.
484 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
485 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
487 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
488 caretAtLineStart
= true;
492 long oldCaretPos
= m_caretPosition
;
494 MoveCaret(position
, caretAtLineStart
);
495 SetDefaultStyleToCursorStyle();
497 if (event
.ShiftDown())
499 if (m_selectionRange
.GetStart() == -2)
500 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
502 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
512 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
517 if (GetCapture() == this)
520 // See if we clicked on a URL
523 dc
.SetFont(GetFont());
526 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
527 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
529 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
531 wxRichTextEvent
cmdEvent(
532 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
534 cmdEvent
.SetEventObject(this);
535 cmdEvent
.SetPosition(m_caretPosition
+1);
537 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
540 if (GetStyle(position
, attr
))
542 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
544 wxString urlTarget
= attr
.GetURL();
545 if (!urlTarget
.IsEmpty())
547 wxMouseEvent
mouseEvent(event
);
549 long startPos
= 0, endPos
= 0;
550 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
553 startPos
= obj
->GetRange().GetStart();
554 endPos
= obj
->GetRange().GetEnd();
557 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
558 InitCommandEvent(urlEvent
);
560 urlEvent
.SetString(urlTarget
);
562 GetEventHandler()->ProcessEvent(urlEvent
);
572 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
576 dc
.SetFont(GetFont());
579 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
580 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
582 // See if we need to change the cursor
585 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
588 if (GetStyle(position
, attr
))
590 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
592 SetCursor(m_urlCursor
);
594 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
596 SetCursor(m_textCursor
);
601 SetCursor(m_textCursor
);
604 if (!event
.Dragging())
610 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
612 // TODO: test closeness
614 bool caretAtLineStart
= false;
616 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
618 // If we're at the start of a line (but not first in para)
619 // then we should keep the caret showing at the start of the line
620 // by showing the m_caretAtLineStart flag.
621 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
622 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
624 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
625 caretAtLineStart
= true;
629 if (m_caretPosition
!= position
)
631 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
633 MoveCaret(position
, caretAtLineStart
);
634 SetDefaultStyleToCursorStyle();
640 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
644 wxRichTextEvent
cmdEvent(
645 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
647 cmdEvent
.SetEventObject(this);
648 cmdEvent
.SetPosition(m_caretPosition
+1);
650 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
654 /// Left-double-click
655 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
657 wxRichTextEvent
cmdEvent(
658 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
660 cmdEvent
.SetEventObject(this);
661 cmdEvent
.SetPosition(m_caretPosition
+1);
663 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
665 SelectWord(GetCaretPosition()+1);
670 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
672 wxRichTextEvent
cmdEvent(
673 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
675 cmdEvent
.SetEventObject(this);
676 cmdEvent
.SetPosition(m_caretPosition
+1);
678 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
683 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
687 flags
|= wxRICHTEXT_CTRL_DOWN
;
688 if (event
.ShiftDown())
689 flags
|= wxRICHTEXT_SHIFT_DOWN
;
691 flags
|= wxRICHTEXT_ALT_DOWN
;
693 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
695 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
697 KeyboardNavigate(event
.GetKeyCode(), flags
);
701 long keycode
= event
.GetKeyCode();
761 case WXK_NUMPAD_HOME
:
762 case WXK_NUMPAD_LEFT
:
764 case WXK_NUMPAD_RIGHT
:
765 case WXK_NUMPAD_DOWN
:
766 case WXK_NUMPAD_PAGEUP
:
767 case WXK_NUMPAD_PAGEDOWN
:
769 case WXK_NUMPAD_BEGIN
:
770 case WXK_NUMPAD_INSERT
:
771 case WXK_WINDOWS_LEFT
:
780 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
781 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
783 BeginBatchUndo(_("Delete Text"));
785 long newPos
= m_caretPosition
;
787 bool processed
= DeleteSelectedContent(& newPos
);
789 // Submit range in character positions, which are greater than caret positions,
790 // so subtract 1 for deleted character and add 1 for conversion to character position.
795 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
798 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
804 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
809 if (GetLastPosition() == -1)
813 m_caretPosition
= -1;
815 SetDefaultStyleToCursorStyle();
818 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
820 wxRichTextEvent
cmdEvent(
821 wxEVT_COMMAND_RICHTEXT_DELETE
,
823 cmdEvent
.SetEventObject(this);
824 cmdEvent
.SetFlags(flags
);
825 cmdEvent
.SetPosition(m_caretPosition
+1);
826 GetEventHandler()->ProcessEvent(cmdEvent
);
836 // all the other keys modify the controls contents which shouldn't be
837 // possible if we're read-only
844 if (event
.GetKeyCode() == WXK_RETURN
)
846 BeginBatchUndo(_("Insert Text"));
848 long newPos
= m_caretPosition
;
850 DeleteSelectedContent(& newPos
);
852 if (event
.ShiftDown())
855 text
= wxRichTextLineBreakChar
;
856 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
857 m_caretAtLineStart
= true;
861 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
864 SetDefaultStyleToCursorStyle();
866 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
868 wxRichTextEvent
cmdEvent(
869 wxEVT_COMMAND_RICHTEXT_RETURN
,
871 cmdEvent
.SetEventObject(this);
872 cmdEvent
.SetFlags(flags
);
873 cmdEvent
.SetPosition(newPos
+1);
875 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
877 // Generate conventional event
878 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
879 InitCommandEvent(textEvent
);
881 GetEventHandler()->ProcessEvent(textEvent
);
885 else if (event
.GetKeyCode() == WXK_BACK
)
887 BeginBatchUndo(_("Delete Text"));
889 long newPos
= m_caretPosition
;
891 bool processed
= DeleteSelectedContent(& newPos
);
893 // Submit range in character positions, which are greater than caret positions,
894 // so subtract 1 for deleted character and add 1 for conversion to character position.
899 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
902 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
908 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
913 if (GetLastPosition() == -1)
917 m_caretPosition
= -1;
919 SetDefaultStyleToCursorStyle();
922 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
924 wxRichTextEvent
cmdEvent(
925 wxEVT_COMMAND_RICHTEXT_DELETE
,
927 cmdEvent
.SetEventObject(this);
928 cmdEvent
.SetFlags(flags
);
929 cmdEvent
.SetPosition(m_caretPosition
+1);
930 GetEventHandler()->ProcessEvent(cmdEvent
);
934 else if (event
.GetKeyCode() == WXK_DELETE
)
936 BeginBatchUndo(_("Delete Text"));
938 long newPos
= m_caretPosition
;
940 bool processed
= DeleteSelectedContent(& newPos
);
942 // Submit range in character positions, which are greater than caret positions,
943 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
947 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
948 if (pos
!= -1 && (pos
> newPos
))
950 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
956 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
961 if (GetLastPosition() == -1)
965 m_caretPosition
= -1;
967 SetDefaultStyleToCursorStyle();
970 wxRichTextEvent
cmdEvent(
971 wxEVT_COMMAND_RICHTEXT_DELETE
,
973 cmdEvent
.SetEventObject(this);
974 cmdEvent
.SetFlags(flags
);
975 cmdEvent
.SetPosition(m_caretPosition
+1);
976 GetEventHandler()->ProcessEvent(cmdEvent
);
982 long keycode
= event
.GetKeyCode();
996 // Fixes AltGr+key with European input languages on Windows
997 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1004 wxRichTextEvent
cmdEvent(
1005 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1007 cmdEvent
.SetEventObject(this);
1008 cmdEvent
.SetFlags(flags
);
1010 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1012 cmdEvent
.SetCharacter((wxChar
) keycode
);
1014 cmdEvent
.SetPosition(m_caretPosition
+1);
1016 if (keycode
== wxT('\t'))
1018 // See if we need to promote or demote the selection or paragraph at the cursor
1019 // position, instead of inserting a tab.
1020 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1021 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1022 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1024 wxRichTextRange range
;
1026 range
= GetSelectionRange();
1028 range
= para
->GetRange().FromInternal();
1030 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1032 PromoteList(promoteBy
, range
, NULL
);
1034 GetEventHandler()->ProcessEvent(cmdEvent
);
1040 BeginBatchUndo(_("Insert Text"));
1042 long newPos
= m_caretPosition
;
1043 DeleteSelectedContent(& newPos
);
1046 wxString str
= event
.GetUnicodeKey();
1048 wxString str
= (wxChar
) event
.GetKeyCode();
1050 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1054 SetDefaultStyleToCursorStyle();
1055 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1057 GetEventHandler()->ProcessEvent(cmdEvent
);
1065 /// Delete content if there is a selection, e.g. when pressing a key.
1066 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1070 long pos
= m_selectionRange
.GetStart();
1071 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1072 m_selectionRange
.SetRange(-2, -2);
1082 /// Keyboard navigation
1086 Left: left one character
1087 Right: right one character
1090 Ctrl-Left: left one word
1091 Ctrl-Right: right one word
1092 Ctrl-Up: previous paragraph start
1093 Ctrl-Down: next start of paragraph
1096 Ctrl-Home: start of document
1097 Ctrl-End: end of document
1098 Page-Up: Up a screen
1099 Page-Down: Down a screen
1103 Ctrl-Alt-PgUp: Start of window
1104 Ctrl-Alt-PgDn: End of window
1105 F8: Start selection mode
1106 Esc: End selection mode
1108 Adding Shift does the above but starts/extends selection.
1113 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1115 bool success
= false;
1117 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1119 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1120 success
= WordRight(1, flags
);
1122 success
= MoveRight(1, flags
);
1124 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1126 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1127 success
= WordLeft(1, flags
);
1129 success
= MoveLeft(1, flags
);
1131 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1133 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1134 success
= MoveToParagraphStart(flags
);
1136 success
= MoveUp(1, flags
);
1138 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1140 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1141 success
= MoveToParagraphEnd(flags
);
1143 success
= MoveDown(1, flags
);
1145 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1147 success
= PageUp(1, flags
);
1149 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1151 success
= PageDown(1, flags
);
1153 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1155 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1156 success
= MoveHome(flags
);
1158 success
= MoveToLineStart(flags
);
1160 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1162 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1163 success
= MoveEnd(flags
);
1165 success
= MoveToLineEnd(flags
);
1170 ScrollIntoView(m_caretPosition
, keyCode
);
1171 SetDefaultStyleToCursorStyle();
1177 /// Extend the selection. Selections are in caret positions.
1178 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1180 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1182 if (oldPos
== newPos
)
1185 wxRichTextRange oldSelection
= m_selectionRange
;
1187 // If not currently selecting, start selecting
1188 if (m_selectionRange
.GetStart() == -2)
1190 m_selectionAnchor
= oldPos
;
1192 if (oldPos
> newPos
)
1193 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1195 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1199 // Always ensure that the selection range start is greater than
1201 if (newPos
> m_selectionAnchor
)
1202 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1203 else if (newPos
== m_selectionAnchor
)
1204 m_selectionRange
= wxRichTextRange(-2, -2);
1206 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1209 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1211 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1213 wxLogDebug(wxT("Strange selection range"));
1222 /// Scroll into view, returning true if we scrolled.
1223 /// This takes a _caret_ position.
1224 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1226 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1232 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1234 int startXUnits
, startYUnits
;
1235 GetViewStart(& startXUnits
, & startYUnits
);
1236 int startY
= startYUnits
* ppuY
;
1239 GetVirtualSize(& sx
, & sy
);
1245 wxRect rect
= line
->GetRect();
1247 bool scrolled
= false;
1249 wxSize clientSize
= GetClientSize();
1250 clientSize
.y
-= GetBuffer().GetBottomMargin();
1252 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1254 int y
= rect
.y
- GetClientSize().y
/2;
1255 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1256 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1258 if (startYUnits
!= yUnits
)
1260 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1263 #if !wxRICHTEXT_USE_OWN_CARET
1273 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1274 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1275 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1276 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1278 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1280 // Make it scroll so this item is at the bottom
1282 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1283 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1285 // If we're still off the screen, scroll another line down
1286 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1289 if (startYUnits
!= yUnits
)
1291 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1295 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1297 // Make it scroll so this item is at the top
1299 int y
= rect
.y
- GetBuffer().GetTopMargin();
1300 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1302 if (startYUnits
!= yUnits
)
1304 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1310 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1311 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1312 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1313 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1315 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1317 // Make it scroll so this item is at the top
1319 int y
= rect
.y
- GetBuffer().GetTopMargin();
1320 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1322 if (startYUnits
!= yUnits
)
1324 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1328 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1330 // Make it scroll so this item is at the bottom
1332 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1333 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1335 // If we're still off the screen, scroll another line down
1336 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1339 if (startYUnits
!= yUnits
)
1341 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1347 #if !wxRICHTEXT_USE_OWN_CARET
1355 /// Is the given position visible on the screen?
1356 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1358 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1364 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1367 GetViewStart(& startX
, & startY
);
1369 startY
= startY
* ppuY
;
1371 wxRect rect
= line
->GetRect();
1372 wxSize clientSize
= GetClientSize();
1373 clientSize
.y
-= GetBuffer().GetBottomMargin();
1375 return (rect
.GetBottom() > (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1378 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1380 m_caretPosition
= position
;
1381 m_caretAtLineStart
= showAtLineStart
;
1384 /// Move caret one visual step forward: this may mean setting a flag
1385 /// and keeping the same position if we're going from the end of one line
1386 /// to the start of the next, which may be the exact same caret position.
1387 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1389 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1391 // Only do the check if we're not at the end of the paragraph (where things work OK
1393 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1395 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1399 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1401 // We're at the end of a line. See whether we need to
1402 // stay at the same actual caret position but change visual
1403 // position, or not.
1404 if (oldPosition
== lineRange
.GetEnd())
1406 if (m_caretAtLineStart
)
1408 // We're already at the start of the line, so actually move on now.
1409 m_caretPosition
= oldPosition
+ 1;
1410 m_caretAtLineStart
= false;
1414 // We're showing at the end of the line, so keep to
1415 // the same position but indicate that we're to show
1416 // at the start of the next line.
1417 m_caretPosition
= oldPosition
;
1418 m_caretAtLineStart
= true;
1420 SetDefaultStyleToCursorStyle();
1426 SetDefaultStyleToCursorStyle();
1429 /// Move caret one visual step backward: this may mean setting a flag
1430 /// and keeping the same position if we're going from the end of one line
1431 /// to the start of the next, which may be the exact same caret position.
1432 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1434 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1436 // Only do the check if we're not at the start of the paragraph (where things work OK
1438 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1440 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1444 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1446 // We're at the start of a line. See whether we need to
1447 // stay at the same actual caret position but change visual
1448 // position, or not.
1449 if (oldPosition
== lineRange
.GetStart())
1451 m_caretPosition
= oldPosition
-1;
1452 m_caretAtLineStart
= true;
1455 else if (oldPosition
== lineRange
.GetEnd())
1457 if (m_caretAtLineStart
)
1459 // We're at the start of the line, so keep the same caret position
1460 // but clear the start-of-line flag.
1461 m_caretPosition
= oldPosition
;
1462 m_caretAtLineStart
= false;
1466 // We're showing at the end of the line, so go back
1467 // to the previous character position.
1468 m_caretPosition
= oldPosition
- 1;
1470 SetDefaultStyleToCursorStyle();
1476 SetDefaultStyleToCursorStyle();
1480 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1482 long endPos
= GetBuffer().GetRange().GetEnd();
1484 if (m_caretPosition
+ noPositions
< endPos
)
1486 long oldPos
= m_caretPosition
;
1487 long newPos
= m_caretPosition
+ noPositions
;
1489 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1493 // Determine by looking at oldPos and m_caretPosition whether
1494 // we moved from the end of a line to the start of the next line, in which case
1495 // we want to adjust the caret position such that it is positioned at the
1496 // start of the next line, rather than jumping past the first character of the
1498 if (noPositions
== 1 && !extendSel
)
1499 MoveCaretForward(oldPos
);
1501 SetCaretPosition(newPos
);
1504 SetDefaultStyleToCursorStyle();
1513 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1517 if (m_caretPosition
> startPos
- noPositions
+ 1)
1519 long oldPos
= m_caretPosition
;
1520 long newPos
= m_caretPosition
- noPositions
;
1521 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1525 if (noPositions
== 1 && !extendSel
)
1526 MoveCaretBack(oldPos
);
1528 SetCaretPosition(newPos
);
1531 SetDefaultStyleToCursorStyle();
1540 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1542 return MoveDown(- noLines
, flags
);
1546 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1551 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1552 wxPoint pt
= GetCaret()->GetPosition();
1553 long newLine
= lineNumber
+ noLines
;
1555 if (lineNumber
!= -1)
1559 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1561 if (newLine
> lastLine
)
1571 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1574 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1580 wxClientDC
dc(this);
1582 dc
.SetFont(GetFont());
1584 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1586 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1588 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1589 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1590 // so we view the caret at the start of the line.
1591 bool caretLineStart
= false;
1592 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1594 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1595 wxRichTextRange lineRange
;
1597 lineRange
= thisLine
->GetAbsoluteRange();
1599 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1602 caretLineStart
= true;
1606 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1607 if (para
&& para
->GetRange().GetStart() == newPos
)
1612 long newSelEnd
= newPos
;
1614 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1618 SetCaretPosition(newPos
, caretLineStart
);
1620 SetDefaultStyleToCursorStyle();
1628 /// Move to the end of the paragraph
1629 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1631 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1634 long newPos
= para
->GetRange().GetEnd() - 1;
1635 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1639 SetCaretPosition(newPos
);
1641 SetDefaultStyleToCursorStyle();
1649 /// Move to the start of the paragraph
1650 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1652 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1655 long newPos
= para
->GetRange().GetStart() - 1;
1656 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1660 SetCaretPosition(newPos
);
1662 SetDefaultStyleToCursorStyle();
1670 /// Move to the end of the line
1671 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1673 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1677 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1678 long newPos
= lineRange
.GetEnd();
1679 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1683 SetCaretPosition(newPos
);
1685 SetDefaultStyleToCursorStyle();
1693 /// Move to the start of the line
1694 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1696 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1699 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1700 long newPos
= lineRange
.GetStart()-1;
1702 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1706 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1708 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1710 SetDefaultStyleToCursorStyle();
1718 /// Move to the start of the buffer
1719 bool wxRichTextCtrl::MoveHome(int flags
)
1721 if (m_caretPosition
!= -1)
1723 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1727 SetCaretPosition(-1);
1729 SetDefaultStyleToCursorStyle();
1737 /// Move to the end of the buffer
1738 bool wxRichTextCtrl::MoveEnd(int flags
)
1740 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1742 if (m_caretPosition
!= endPos
)
1744 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1748 SetCaretPosition(endPos
);
1750 SetDefaultStyleToCursorStyle();
1758 /// Move noPages pages up
1759 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1761 return PageDown(- noPages
, flags
);
1764 /// Move noPages pages down
1765 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1767 // Calculate which line occurs noPages * screen height further down.
1768 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1771 wxSize clientSize
= GetClientSize();
1772 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1774 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1777 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1778 long pos
= lineRange
.GetStart()-1;
1779 if (pos
!= m_caretPosition
)
1781 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1783 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1787 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1789 SetDefaultStyleToCursorStyle();
1799 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1801 return str
== wxT(" ") || str
== wxT("\t");
1804 // Finds the caret position for the next word
1805 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1807 long endPos
= GetBuffer().GetRange().GetEnd();
1811 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1813 // First skip current text to space
1814 while (i
< endPos
&& i
> -1)
1816 // i is in character, not caret positions
1817 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1818 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1819 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1823 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
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);
1835 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1836 return wxMax(-1, i
);
1838 if (text
.empty()) // End of paragraph, or maybe an image
1839 return wxMax(-1, i
- 1);
1840 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1844 // Convert to caret position
1845 return wxMax(-1, i
- 1);
1854 long i
= m_caretPosition
;
1856 // First skip white space
1857 while (i
< endPos
&& i
> -1)
1859 // i is in character, not caret positions
1860 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1861 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1863 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1865 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1870 // Next skip current text to space
1871 while (i
< endPos
&& i
> -1)
1873 // i is in character, not caret positions
1874 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1875 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1876 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1879 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1892 /// Move n words left
1893 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1895 long pos
= FindNextWordPosition(-1);
1896 if (pos
!= m_caretPosition
)
1898 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1900 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1904 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1906 SetDefaultStyleToCursorStyle();
1914 /// Move n words right
1915 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1917 long pos
= FindNextWordPosition(1);
1918 if (pos
!= m_caretPosition
)
1920 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1922 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1926 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1928 SetDefaultStyleToCursorStyle();
1937 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1939 // Only do sizing optimization for large buffers
1940 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1942 m_fullLayoutRequired
= true;
1943 m_fullLayoutTime
= wxGetLocalTimeMillis();
1944 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1945 LayoutContent(true /* onlyVisibleRect */);
1948 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1950 #if wxRICHTEXT_BUFFERED_PAINTING
1958 /// Idle-time processing
1959 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1961 #if wxRICHTEXT_USE_OWN_CARET
1962 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1964 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1970 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1972 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1974 m_fullLayoutRequired
= false;
1975 m_fullLayoutTime
= 0;
1976 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1977 ShowPosition(m_fullLayoutSavedPosition
);
1981 if (m_caretPositionForDefaultStyle
!= -2)
1983 // If the caret position has changed, no longer reflect the default style
1985 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1986 m_caretPositionForDefaultStyle
= -2;
1993 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1995 #if wxRICHTEXT_USE_OWN_CARET
1996 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1999 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2006 /// Set up scrollbars, e.g. after a resize
2007 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2012 if (GetBuffer().IsEmpty())
2014 SetScrollbars(0, 0, 0, 0, 0, 0);
2018 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2019 // of pixels. See e.g. wxVScrolledWindow for ideas.
2020 int pixelsPerUnit
= 5;
2021 wxSize clientSize
= GetClientSize();
2023 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2025 // Round up so we have at least maxHeight pixels
2026 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2028 int startX
= 0, startY
= 0;
2030 GetViewStart(& startX
, & startY
);
2032 int maxPositionX
= 0;
2033 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2035 int newStartX
= wxMin(maxPositionX
, startX
);
2036 int newStartY
= wxMin(maxPositionY
, startY
);
2038 int oldPPUX
, oldPPUY
;
2039 int oldStartX
, oldStartY
;
2040 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2041 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2042 GetViewStart(& oldStartX
, & oldStartY
);
2043 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2045 oldVirtualSizeY
/= oldPPUY
;
2047 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2050 // Don't set scrollbars if there were none before, and there will be none now.
2051 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2054 // Move to previous scroll position if
2056 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2059 /// Paint the background
2060 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2062 wxColour backgroundColour
= GetBackgroundColour();
2063 if (!backgroundColour
.Ok())
2064 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2066 // Clear the background
2067 dc
.SetBrush(wxBrush(backgroundColour
));
2068 dc
.SetPen(*wxTRANSPARENT_PEN
);
2069 wxRect
windowRect(GetClientSize());
2070 windowRect
.x
-= 2; windowRect
.y
-= 2;
2071 windowRect
.width
+= 4; windowRect
.height
+= 4;
2073 // We need to shift the rectangle to take into account
2074 // scrolling. Converting device to logical coordinates.
2075 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2076 dc
.DrawRectangle(windowRect
);
2079 #if wxRICHTEXT_BUFFERED_PAINTING
2080 /// Recreate buffer bitmap if necessary
2081 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2084 if (sz
== wxDefaultSize
)
2085 sz
= GetClientSize();
2087 if (sz
.x
< 1 || sz
.y
< 1)
2090 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2091 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2092 return m_bufferBitmap
.Ok();
2096 // ----------------------------------------------------------------------------
2097 // file IO functions
2098 // ----------------------------------------------------------------------------
2100 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2102 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2104 m_filename
= filename
;
2107 SetInsertionPoint(0);
2110 SetupScrollbars(true);
2112 wxTextCtrl::SendTextUpdatedEvent(this);
2118 wxLogError(_("File couldn't be loaded."));
2124 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2126 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2128 m_filename
= filename
;
2135 wxLogError(_("The text couldn't be saved."));
2140 // ----------------------------------------------------------------------------
2141 // wxRichTextCtrl specific functionality
2142 // ----------------------------------------------------------------------------
2144 /// Add a new paragraph of text to the end of the buffer
2145 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2147 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2153 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2155 wxRichTextRange range
= GetBuffer().AddImage(image
);
2160 // ----------------------------------------------------------------------------
2161 // selection and ranges
2162 // ----------------------------------------------------------------------------
2164 void wxRichTextCtrl::SelectAll()
2166 SetSelection(0, GetLastPosition()+1);
2167 m_selectionAnchor
= -1;
2171 void wxRichTextCtrl::SelectNone()
2173 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2175 wxRichTextRange oldSelection
= m_selectionRange
;
2177 m_selectionRange
= wxRichTextRange(-2, -2);
2179 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2181 m_selectionAnchor
= -2;
2184 static bool wxIsWordDelimiter(const wxString
& text
)
2186 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2189 /// Select the word at the given character position
2190 bool wxRichTextCtrl::SelectWord(long position
)
2192 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2195 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2199 if (position
== para
->GetRange().GetEnd())
2202 long positionStart
= position
;
2203 long positionEnd
= position
;
2205 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2207 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2208 if (wxIsWordDelimiter(text
))
2214 if (positionStart
< para
->GetRange().GetStart())
2215 positionStart
= para
->GetRange().GetStart();
2217 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2219 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2220 if (wxIsWordDelimiter(text
))
2226 if (positionEnd
>= para
->GetRange().GetEnd())
2227 positionEnd
= para
->GetRange().GetEnd();
2229 if (positionEnd
< positionStart
)
2232 SetSelection(positionStart
, positionEnd
+1);
2234 if (positionStart
>= 0)
2236 MoveCaret(positionStart
-1, true);
2237 SetDefaultStyleToCursorStyle();
2243 wxString
wxRichTextCtrl::GetStringSelection() const
2246 GetSelection(&from
, &to
);
2248 return GetRange(from
, to
);
2251 // ----------------------------------------------------------------------------
2253 // ----------------------------------------------------------------------------
2255 wxTextCtrlHitTestResult
2256 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2258 // implement in terms of the other overload as the native ports typically
2259 // can get the position and not (x, y) pair directly (although wxUniv
2260 // directly gets x and y -- and so overrides this method as well)
2262 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2264 if ( rc
!= wxTE_HT_UNKNOWN
)
2266 PositionToXY(pos
, x
, y
);
2272 wxTextCtrlHitTestResult
2273 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2276 wxClientDC
dc((wxRichTextCtrl
*) this);
2277 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2279 // Buffer uses logical position (relative to start of buffer)
2281 wxPoint pt2
= GetLogicalPoint(pt
);
2283 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2285 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2286 return wxTE_HT_BEFORE
;
2287 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2288 return wxTE_HT_BEYOND
;
2289 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2290 return wxTE_HT_ON_TEXT
;
2292 return wxTE_HT_UNKNOWN
;
2295 // ----------------------------------------------------------------------------
2296 // set/get the controls text
2297 // ----------------------------------------------------------------------------
2299 wxString
wxRichTextCtrl::DoGetValue() const
2301 return GetBuffer().GetText();
2304 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2306 // Public API for range is different from internals
2307 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2310 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2312 // Don't call Clear here, since it always sends a text updated event
2313 m_buffer
.ResetAndClearCommands();
2314 m_buffer
.SetDirty(true);
2315 m_caretPosition
= -1;
2316 m_caretPositionForDefaultStyle
= -2;
2317 m_caretAtLineStart
= false;
2318 m_selectionRange
.SetRange(-2, -2);
2328 if (!value
.IsEmpty())
2330 // Remove empty paragraph
2331 GetBuffer().Clear();
2332 DoWriteText(value
, flags
);
2334 // for compatibility, don't move the cursor when doing SetValue()
2335 SetInsertionPoint(0);
2339 // still send an event for consistency
2340 if (flags
& SetValue_SendEvent
)
2341 wxTextCtrl::SendTextUpdatedEvent(this);
2346 void wxRichTextCtrl::WriteText(const wxString
& value
)
2351 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2353 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2355 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2357 if ( flags
& SetValue_SendEvent
)
2358 wxTextCtrl::SendTextUpdatedEvent(this);
2361 void wxRichTextCtrl::AppendText(const wxString
& text
)
2363 SetInsertionPointEnd();
2368 /// Write an image at the current insertion point
2369 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2371 wxRichTextImageBlock imageBlock
;
2373 wxImage image2
= image
;
2374 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2375 return WriteImage(imageBlock
);
2380 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2382 wxRichTextImageBlock imageBlock
;
2385 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2386 return WriteImage(imageBlock
);
2391 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2393 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2396 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2400 wxRichTextImageBlock imageBlock
;
2402 wxImage image
= bitmap
.ConvertToImage();
2403 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2404 return WriteImage(imageBlock
);
2410 /// Insert a newline (actually paragraph) at the current insertion point.
2411 bool wxRichTextCtrl::Newline()
2413 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2416 /// Insert a line break at the current insertion point.
2417 bool wxRichTextCtrl::LineBreak()
2420 text
= wxRichTextLineBreakChar
;
2421 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2424 // ----------------------------------------------------------------------------
2425 // Clipboard operations
2426 // ----------------------------------------------------------------------------
2428 void wxRichTextCtrl::Copy()
2432 wxRichTextRange range
= GetInternalSelectionRange();
2433 GetBuffer().CopyToClipboard(range
);
2437 void wxRichTextCtrl::Cut()
2441 wxRichTextRange range
= GetInternalSelectionRange();
2442 GetBuffer().CopyToClipboard(range
);
2444 DeleteSelectedContent();
2450 void wxRichTextCtrl::Paste()
2454 BeginBatchUndo(_("Paste"));
2456 long newPos
= m_caretPosition
;
2457 DeleteSelectedContent(& newPos
);
2459 GetBuffer().PasteFromClipboard(newPos
);
2465 void wxRichTextCtrl::DeleteSelection()
2467 if (CanDeleteSelection())
2469 DeleteSelectedContent();
2473 bool wxRichTextCtrl::HasSelection() const
2475 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2478 bool wxRichTextCtrl::CanCopy() const
2480 // Can copy if there's a selection
2481 return HasSelection();
2484 bool wxRichTextCtrl::CanCut() const
2486 return HasSelection() && IsEditable();
2489 bool wxRichTextCtrl::CanPaste() const
2491 if ( !IsEditable() )
2494 return GetBuffer().CanPasteFromClipboard();
2497 bool wxRichTextCtrl::CanDeleteSelection() const
2499 return HasSelection() && IsEditable();
2503 // ----------------------------------------------------------------------------
2505 // ----------------------------------------------------------------------------
2507 void wxRichTextCtrl::SetEditable(bool editable
)
2509 m_editable
= editable
;
2512 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2516 m_caretPosition
= pos
- 1;
2521 void wxRichTextCtrl::SetInsertionPointEnd()
2523 long pos
= GetLastPosition();
2524 SetInsertionPoint(pos
);
2527 long wxRichTextCtrl::GetInsertionPoint() const
2529 return m_caretPosition
+1;
2532 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2534 return GetBuffer().GetRange().GetEnd();
2537 // If the return values from and to are the same, there is no
2539 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2541 *from
= m_selectionRange
.GetStart();
2542 *to
= m_selectionRange
.GetEnd();
2543 if ((*to
) != -1 && (*to
) != -2)
2547 bool wxRichTextCtrl::IsEditable() const
2552 // ----------------------------------------------------------------------------
2554 // ----------------------------------------------------------------------------
2556 void wxRichTextCtrl::SetSelection(long from
, long to
)
2558 // if from and to are both -1, it means (in wxWidgets) that all text should
2560 if ( (from
== -1) && (to
== -1) )
2563 to
= GetLastPosition()+1;
2572 wxRichTextRange oldSelection
= m_selectionRange
;
2573 m_selectionAnchor
= from
;
2574 m_selectionRange
.SetRange(from
, to
-1);
2576 m_caretPosition
= from
-1;
2578 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2583 // ----------------------------------------------------------------------------
2585 // ----------------------------------------------------------------------------
2587 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2588 const wxString
& value
)
2590 BeginBatchUndo(_("Replace"));
2592 DeleteSelectedContent();
2594 DoWriteText(value
, SetValue_SelectionOnly
);
2599 void wxRichTextCtrl::Remove(long from
, long to
)
2603 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2610 bool wxRichTextCtrl::IsModified() const
2612 return m_buffer
.IsModified();
2615 void wxRichTextCtrl::MarkDirty()
2617 m_buffer
.Modify(true);
2620 void wxRichTextCtrl::DiscardEdits()
2622 m_caretPositionForDefaultStyle
= -2;
2623 m_buffer
.Modify(false);
2624 m_buffer
.GetCommandProcessor()->ClearCommands();
2627 int wxRichTextCtrl::GetNumberOfLines() const
2629 return GetBuffer().GetParagraphCount();
2632 // ----------------------------------------------------------------------------
2633 // Positions <-> coords
2634 // ----------------------------------------------------------------------------
2636 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2638 return GetBuffer().XYToPosition(x
, y
);
2641 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2643 return GetBuffer().PositionToXY(pos
, x
, y
);
2646 // ----------------------------------------------------------------------------
2648 // ----------------------------------------------------------------------------
2650 void wxRichTextCtrl::ShowPosition(long pos
)
2652 if (!IsPositionVisible(pos
))
2653 ScrollIntoView(pos
-1, WXK_DOWN
);
2656 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2658 return GetBuffer().GetParagraphLength(lineNo
);
2661 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2663 return GetBuffer().GetParagraphText(lineNo
);
2666 // ----------------------------------------------------------------------------
2668 // ----------------------------------------------------------------------------
2670 void wxRichTextCtrl::Undo()
2674 GetCommandProcessor()->Undo();
2678 void wxRichTextCtrl::Redo()
2682 GetCommandProcessor()->Redo();
2686 bool wxRichTextCtrl::CanUndo() const
2688 return GetCommandProcessor()->CanUndo();
2691 bool wxRichTextCtrl::CanRedo() const
2693 return GetCommandProcessor()->CanRedo();
2696 // ----------------------------------------------------------------------------
2697 // implementation details
2698 // ----------------------------------------------------------------------------
2700 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2702 SetValue(event
.GetString());
2703 GetEventHandler()->ProcessEvent(event
);
2706 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2708 // By default, load the first file into the text window.
2709 if (event
.GetNumberOfFiles() > 0)
2711 LoadFile(event
.GetFiles()[0]);
2715 wxSize
wxRichTextCtrl::DoGetBestSize() const
2717 return wxSize(10, 10);
2720 // ----------------------------------------------------------------------------
2721 // standard handlers for standard edit menu events
2722 // ----------------------------------------------------------------------------
2724 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2729 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2734 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2739 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2744 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2749 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2754 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2756 event
.Enable( CanCut() );
2759 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2761 event
.Enable( CanCopy() );
2764 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2766 event
.Enable( CanDeleteSelection() );
2769 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2771 event
.Enable( CanPaste() );
2774 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2776 event
.Enable( CanUndo() );
2777 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2780 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2782 event
.Enable( CanRedo() );
2783 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2786 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2791 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2793 event
.Enable(GetLastPosition() > 0);
2796 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2798 if (event
.GetEventObject() != this)
2806 m_contextMenu
= new wxMenu
;
2807 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2808 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2809 m_contextMenu
->AppendSeparator();
2810 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2811 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2812 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2813 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2814 m_contextMenu
->AppendSeparator();
2815 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2817 PopupMenu(m_contextMenu
);
2821 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2823 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2826 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2828 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2831 // extended style setting operation with flags including:
2832 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2833 // see richtextbuffer.h for more details.
2835 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2837 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2840 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2842 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2845 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2847 return GetBuffer().GetDefaultStyle();
2850 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2852 return GetBuffer().GetStyle(position
, style
);
2855 // get the common set of styles for the range
2856 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2858 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2861 /// Get the content (uncombined) attributes for this position.
2862 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2864 return GetBuffer().GetUncombinedStyle(position
, style
);
2867 /// Set font, and also the buffer attributes
2868 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2870 wxTextCtrlBase::SetFont(font
);
2872 wxTextAttr attr
= GetBuffer().GetAttributes();
2874 GetBuffer().SetBasicStyle(attr
);
2876 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2882 /// Transform logical to physical
2883 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2886 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2891 /// Transform physical to logical
2892 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2895 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2900 /// Position the caret
2901 void wxRichTextCtrl::PositionCaret()
2906 //wxLogDebug(wxT("PositionCaret"));
2909 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2911 wxPoint newPt
= caretRect
.GetPosition();
2912 wxSize newSz
= caretRect
.GetSize();
2913 wxPoint pt
= GetPhysicalPoint(newPt
);
2914 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2917 if (GetCaret()->GetSize() != newSz
)
2918 GetCaret()->SetSize(newSz
);
2920 int halfSize
= newSz
.y
/2;
2921 // If the caret is beyond the margin, hide it by moving it out of the way
2922 if (((pt
.y
+ halfSize
) < GetBuffer().GetTopMargin()) || ((pt
.y
+ halfSize
) > (GetClientSize().y
- GetBuffer().GetBottomMargin())))
2925 GetCaret()->Move(pt
);
2931 /// Get the caret height and position for the given character position
2932 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2934 wxClientDC
dc(this);
2935 dc
.SetFont(GetFont());
2942 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2944 // Caret height can't be zero
2946 height
= dc
.GetCharHeight();
2948 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2955 /// Gets the line for the visible caret position. If the caret is
2956 /// shown at the very end of the line, it means the next character is actually
2957 /// on the following line. So let's get the line we're expecting to find
2958 /// if this is the case.
2959 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2961 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2962 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2965 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2966 if (caretPosition
== lineRange
.GetStart()-1 &&
2967 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2969 if (!m_caretAtLineStart
)
2970 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2977 /// Move the caret to the given character position
2978 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2980 if (GetBuffer().GetDirty())
2983 if (pos
<= GetBuffer().GetRange().GetEnd())
2985 SetCaretPosition(pos
, showAtLineStart
);
2995 /// Layout the buffer: which we must do before certain operations, such as
2996 /// setting the caret position.
2997 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2999 if (GetBuffer().GetDirty() || onlyVisibleRect
)
3001 wxRect
availableSpace(GetClientSize());
3002 if (availableSpace
.width
== 0)
3003 availableSpace
.width
= 10;
3004 if (availableSpace
.height
== 0)
3005 availableSpace
.height
= 10;
3007 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3008 if (onlyVisibleRect
)
3010 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3011 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3014 wxClientDC
dc(this);
3015 dc
.SetFont(GetFont());
3019 GetBuffer().Defragment();
3020 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3021 GetBuffer().Layout(dc
, availableSpace
, flags
);
3022 GetBuffer().SetDirty(false);
3031 /// Is all of the selection bold?
3032 bool wxRichTextCtrl::IsSelectionBold()
3037 wxRichTextRange range
= GetSelectionRange();
3038 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3039 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3041 return HasCharacterAttributes(range
, attr
);
3045 // If no selection, then we need to combine current style with default style
3046 // to see what the effect would be if we started typing.
3048 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3050 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3051 if (GetStyle(pos
, attr
))
3053 if (IsDefaultStyleShowing())
3054 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3055 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3061 /// Is all of the selection italics?
3062 bool wxRichTextCtrl::IsSelectionItalics()
3066 wxRichTextRange range
= GetSelectionRange();
3068 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3069 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3071 return HasCharacterAttributes(range
, attr
);
3075 // If no selection, then we need to combine current style with default style
3076 // to see what the effect would be if we started typing.
3078 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3080 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3081 if (GetStyle(pos
, attr
))
3083 if (IsDefaultStyleShowing())
3084 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3085 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3091 /// Is all of the selection underlined?
3092 bool wxRichTextCtrl::IsSelectionUnderlined()
3096 wxRichTextRange range
= GetSelectionRange();
3098 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3099 attr
.SetFontUnderlined(true);
3101 return HasCharacterAttributes(range
, attr
);
3105 // If no selection, then we need to combine current style with default style
3106 // to see what the effect would be if we started typing.
3108 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3109 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3111 if (GetStyle(pos
, attr
))
3113 if (IsDefaultStyleShowing())
3114 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3115 return attr
.GetFontUnderlined();
3121 /// Apply bold to the selection
3122 bool wxRichTextCtrl::ApplyBoldToSelection()
3125 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3126 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3129 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3132 wxRichTextAttr current
= GetDefaultStyleEx();
3133 current
.Apply(attr
);
3134 SetAndShowDefaultStyle(current
);
3139 /// Apply italic to the selection
3140 bool wxRichTextCtrl::ApplyItalicToSelection()
3143 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3144 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3147 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3150 wxRichTextAttr current
= GetDefaultStyleEx();
3151 current
.Apply(attr
);
3152 SetAndShowDefaultStyle(current
);
3157 /// Apply underline to the selection
3158 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3161 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3162 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3165 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3168 wxRichTextAttr current
= GetDefaultStyleEx();
3169 current
.Apply(attr
);
3170 SetAndShowDefaultStyle(current
);
3175 /// Is all of the selection aligned according to the specified flag?
3176 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3178 wxRichTextRange range
;
3180 range
= GetSelectionRange();
3182 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3185 attr
.SetAlignment(alignment
);
3187 return HasParagraphAttributes(range
, attr
);
3190 /// Apply alignment to the selection
3191 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3194 attr
.SetAlignment(alignment
);
3196 return SetStyle(GetSelectionRange(), attr
);
3199 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3201 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3206 /// Apply a named style to the selection
3207 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3209 // Flags are defined within each definition, so only certain
3210 // attributes are applied.
3211 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3213 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3215 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3217 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3219 wxRichTextRange range
;
3222 range
= GetSelectionRange();
3225 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3226 range
= wxRichTextRange(pos
, pos
+1);
3229 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3232 // Make sure the attr has the style name
3233 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3235 attr
.SetParagraphStyleName(def
->GetName());
3237 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3238 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3239 // to change its style independently.
3240 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3243 attr
.SetCharacterStyleName(def
->GetName());
3246 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3249 wxRichTextAttr current
= GetDefaultStyleEx();
3250 current
.Apply(attr
);
3251 SetAndShowDefaultStyle(current
);
3256 /// Apply the style sheet to the buffer, for example if the styles have changed.
3257 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3260 styleSheet
= GetBuffer().GetStyleSheet();
3264 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3266 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3274 /// Sets the default style to the style under the cursor
3275 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3278 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3280 // If at the start of a paragraph, use the next position.
3281 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3283 if (GetUncombinedStyle(pos
, attr
))
3285 SetDefaultStyle(attr
);
3292 /// Returns the first visible position in the current view
3293 long wxRichTextCtrl::GetFirstVisiblePosition() const
3295 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3297 return line
->GetAbsoluteRange().GetStart();
3302 /// Get the first visible point in the window
3303 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3306 int startXUnits
, startYUnits
;
3308 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3309 GetViewStart(& startXUnits
, & startYUnits
);
3311 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3314 /// The adjusted caret position is the character position adjusted to take
3315 /// into account whether we're at the start of a paragraph, in which case
3316 /// style information should be taken from the next position, not current one.
3317 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3319 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3321 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3326 /// Get/set the selection range in character positions. -1, -1 means no selection.
3327 /// The range is in API convention, i.e. a single character selection is denoted
3329 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3331 wxRichTextRange range
= GetInternalSelectionRange();
3332 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3333 range
.SetEnd(range
.GetEnd() + 1);
3337 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3339 wxRichTextRange
range1(range
);
3340 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3341 range1
.SetEnd(range1
.GetEnd() - 1);
3343 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3345 SetInternalSelectionRange(range1
);
3349 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3351 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3354 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3356 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3359 /// Clear list for given range
3360 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3362 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3365 /// Number/renumber any list elements in the given range
3366 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3368 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3371 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3373 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3376 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3377 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3379 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3382 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3384 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3387 /// Deletes the content in the given range
3388 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3390 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3393 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3395 if (sm_availableFontNames
.GetCount() == 0)
3397 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3398 sm_availableFontNames
.Sort();
3400 return sm_availableFontNames
;
3403 void wxRichTextCtrl::ClearAvailableFontNames()
3405 sm_availableFontNames
.Clear();
3408 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3410 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3412 wxTextAttrEx basicStyle
= GetBasicStyle();
3413 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3414 SetBasicStyle(basicStyle
);
3415 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3420 // Refresh the area affected by a selection change
3421 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3423 // Calculate the refresh rectangle - just the affected lines
3424 long firstPos
, lastPos
;
3425 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3427 firstPos
= newSelection
.GetStart();
3428 lastPos
= newSelection
.GetEnd();
3430 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3432 firstPos
= oldSelection
.GetStart();
3433 lastPos
= oldSelection
.GetEnd();
3435 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3441 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3442 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3445 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3446 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3448 if (firstLine
&& lastLine
)
3450 wxSize clientSize
= GetClientSize();
3451 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3452 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3455 pt1
.y
= wxMax(0, pt1
.y
);
3457 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3459 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3460 RefreshRect(rect
, false);
3468 #if wxRICHTEXT_USE_OWN_CARET
3470 // ----------------------------------------------------------------------------
3471 // initialization and destruction
3472 // ----------------------------------------------------------------------------
3474 void wxRichTextCaret::Init()
3480 m_richTextCtrl
= NULL
;
3481 m_needsUpdate
= false;
3485 wxRichTextCaret::~wxRichTextCaret()
3487 if (m_timer
.IsRunning())
3491 // ----------------------------------------------------------------------------
3492 // showing/hiding/moving the caret (base class interface)
3493 // ----------------------------------------------------------------------------
3495 void wxRichTextCaret::DoShow()
3499 if (!m_timer
.IsRunning())
3500 m_timer
.Start(GetBlinkTime());
3505 void wxRichTextCaret::DoHide()
3507 if (m_timer
.IsRunning())
3513 void wxRichTextCaret::DoMove()
3519 if (m_xOld
!= -1 && m_yOld
!= -1)
3523 wxRect
rect(GetPosition(), GetSize());
3524 m_richTextCtrl
->RefreshRect(rect
, false);
3533 void wxRichTextCaret::DoSize()
3535 int countVisible
= m_countVisible
;
3536 if (countVisible
> 0)
3542 if (countVisible
> 0)
3544 m_countVisible
= countVisible
;
3549 // ----------------------------------------------------------------------------
3550 // handling the focus
3551 // ----------------------------------------------------------------------------
3553 void wxRichTextCaret::OnSetFocus()
3561 void wxRichTextCaret::OnKillFocus()
3566 // ----------------------------------------------------------------------------
3567 // drawing the caret
3568 // ----------------------------------------------------------------------------
3570 void wxRichTextCaret::Refresh()
3574 wxRect
rect(GetPosition(), GetSize());
3575 m_richTextCtrl
->RefreshRect(rect
, false);
3579 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3581 dc
->SetPen( *wxBLACK_PEN
);
3583 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3584 dc
->SetPen(*wxBLACK_PEN
);
3586 wxPoint
pt(m_x
, m_y
);
3590 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3592 if (IsVisible() && m_flashOn
)
3593 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3596 void wxRichTextCaret::Notify()
3598 m_flashOn
= !m_flashOn
;
3602 void wxRichTextCaretTimer::Notify()
3607 // wxRICHTEXT_USE_OWN_CARET