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
.GetKeyCode() == WXK_LEFT
||
696 event
.GetKeyCode() == WXK_RIGHT
||
697 event
.GetKeyCode() == WXK_UP
||
698 event
.GetKeyCode() == WXK_DOWN
||
699 event
.GetKeyCode() == WXK_HOME
||
700 event
.GetKeyCode() == WXK_PAGEUP
||
701 event
.GetKeyCode() == WXK_PAGEDOWN
||
702 event
.GetKeyCode() == WXK_END
||
704 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
705 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
706 event
.GetKeyCode() == WXK_NUMPAD_UP
||
707 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
708 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
709 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
710 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
711 event
.GetKeyCode() == WXK_NUMPAD_END
)
713 KeyboardNavigate(event
.GetKeyCode(), flags
);
717 long keycode
= event
.GetKeyCode();
777 case WXK_NUMPAD_HOME
:
778 case WXK_NUMPAD_LEFT
:
780 case WXK_NUMPAD_RIGHT
:
781 case WXK_NUMPAD_DOWN
:
782 case WXK_NUMPAD_PAGEUP
:
783 case WXK_NUMPAD_PAGEDOWN
:
785 case WXK_NUMPAD_BEGIN
:
786 case WXK_NUMPAD_INSERT
:
787 case WXK_NUMPAD_DELETE
:
788 case WXK_WINDOWS_LEFT
:
797 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
798 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
800 BeginBatchUndo(_("Delete Text"));
802 long newPos
= m_caretPosition
;
804 bool processed
= DeleteSelectedContent(& newPos
);
806 // Submit range in character positions, which are greater than caret positions,
807 // so subtract 1 for deleted character and add 1 for conversion to character position.
812 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
815 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
821 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
826 if (GetLastPosition() == -1)
830 m_caretPosition
= -1;
832 SetDefaultStyleToCursorStyle();
835 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
837 wxRichTextEvent
cmdEvent(
838 wxEVT_COMMAND_RICHTEXT_DELETE
,
840 cmdEvent
.SetEventObject(this);
841 cmdEvent
.SetFlags(flags
);
842 cmdEvent
.SetPosition(m_caretPosition
+1);
843 GetEventHandler()->ProcessEvent(cmdEvent
);
853 // all the other keys modify the controls contents which shouldn't be
854 // possible if we're read-only
861 if (event
.GetKeyCode() == WXK_RETURN
)
863 BeginBatchUndo(_("Insert Text"));
865 long newPos
= m_caretPosition
;
867 DeleteSelectedContent(& newPos
);
869 if (event
.ShiftDown())
872 text
= wxRichTextLineBreakChar
;
873 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
874 m_caretAtLineStart
= true;
878 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
881 SetDefaultStyleToCursorStyle();
883 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
885 wxRichTextEvent
cmdEvent(
886 wxEVT_COMMAND_RICHTEXT_RETURN
,
888 cmdEvent
.SetEventObject(this);
889 cmdEvent
.SetFlags(flags
);
890 cmdEvent
.SetPosition(newPos
+1);
892 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
894 // Generate conventional event
895 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
896 InitCommandEvent(textEvent
);
898 GetEventHandler()->ProcessEvent(textEvent
);
902 else if (event
.GetKeyCode() == WXK_BACK
)
904 BeginBatchUndo(_("Delete Text"));
906 long newPos
= m_caretPosition
;
908 bool processed
= DeleteSelectedContent(& newPos
);
910 // Submit range in character positions, which are greater than caret positions,
911 // so subtract 1 for deleted character and add 1 for conversion to character position.
916 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
919 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
925 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
930 if (GetLastPosition() == -1)
934 m_caretPosition
= -1;
936 SetDefaultStyleToCursorStyle();
939 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
941 wxRichTextEvent
cmdEvent(
942 wxEVT_COMMAND_RICHTEXT_DELETE
,
944 cmdEvent
.SetEventObject(this);
945 cmdEvent
.SetFlags(flags
);
946 cmdEvent
.SetPosition(m_caretPosition
+1);
947 GetEventHandler()->ProcessEvent(cmdEvent
);
951 else if (event
.GetKeyCode() == WXK_DELETE
)
953 BeginBatchUndo(_("Delete Text"));
955 long newPos
= m_caretPosition
;
957 bool processed
= DeleteSelectedContent(& newPos
);
959 // Submit range in character positions, which are greater than caret positions,
960 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
964 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
965 if (pos
!= -1 && (pos
> newPos
))
967 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
973 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
978 if (GetLastPosition() == -1)
982 m_caretPosition
= -1;
984 SetDefaultStyleToCursorStyle();
987 wxRichTextEvent
cmdEvent(
988 wxEVT_COMMAND_RICHTEXT_DELETE
,
990 cmdEvent
.SetEventObject(this);
991 cmdEvent
.SetFlags(flags
);
992 cmdEvent
.SetPosition(m_caretPosition
+1);
993 GetEventHandler()->ProcessEvent(cmdEvent
);
999 long keycode
= event
.GetKeyCode();
1011 if (event
.CmdDown())
1013 // Fixes AltGr+key with European input languages on Windows
1014 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1021 wxRichTextEvent
cmdEvent(
1022 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1024 cmdEvent
.SetEventObject(this);
1025 cmdEvent
.SetFlags(flags
);
1027 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1029 cmdEvent
.SetCharacter((wxChar
) keycode
);
1031 cmdEvent
.SetPosition(m_caretPosition
+1);
1033 if (keycode
== wxT('\t'))
1035 // See if we need to promote or demote the selection or paragraph at the cursor
1036 // position, instead of inserting a tab.
1037 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1038 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1039 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1041 wxRichTextRange range
;
1043 range
= GetSelectionRange();
1045 range
= para
->GetRange().FromInternal();
1047 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1049 PromoteList(promoteBy
, range
, NULL
);
1051 GetEventHandler()->ProcessEvent(cmdEvent
);
1057 BeginBatchUndo(_("Insert Text"));
1059 long newPos
= m_caretPosition
;
1060 DeleteSelectedContent(& newPos
);
1063 wxString str
= event
.GetUnicodeKey();
1065 wxString str
= (wxChar
) event
.GetKeyCode();
1067 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1071 SetDefaultStyleToCursorStyle();
1072 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1074 GetEventHandler()->ProcessEvent(cmdEvent
);
1082 /// Delete content if there is a selection, e.g. when pressing a key.
1083 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1087 long pos
= m_selectionRange
.GetStart();
1088 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1089 m_selectionRange
.SetRange(-2, -2);
1099 /// Keyboard navigation
1103 Left: left one character
1104 Right: right one character
1107 Ctrl-Left: left one word
1108 Ctrl-Right: right one word
1109 Ctrl-Up: previous paragraph start
1110 Ctrl-Down: next start of paragraph
1113 Ctrl-Home: start of document
1114 Ctrl-End: end of document
1115 Page-Up: Up a screen
1116 Page-Down: Down a screen
1120 Ctrl-Alt-PgUp: Start of window
1121 Ctrl-Alt-PgDn: End of window
1122 F8: Start selection mode
1123 Esc: End selection mode
1125 Adding Shift does the above but starts/extends selection.
1130 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1132 bool success
= false;
1134 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1136 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1137 success
= WordRight(1, flags
);
1139 success
= MoveRight(1, flags
);
1141 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1143 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1144 success
= WordLeft(1, flags
);
1146 success
= MoveLeft(1, flags
);
1148 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1150 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1151 success
= MoveToParagraphStart(flags
);
1153 success
= MoveUp(1, flags
);
1155 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1157 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1158 success
= MoveToParagraphEnd(flags
);
1160 success
= MoveDown(1, flags
);
1162 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1164 success
= PageUp(1, flags
);
1166 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1168 success
= PageDown(1, flags
);
1170 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1172 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1173 success
= MoveHome(flags
);
1175 success
= MoveToLineStart(flags
);
1177 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1179 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1180 success
= MoveEnd(flags
);
1182 success
= MoveToLineEnd(flags
);
1187 ScrollIntoView(m_caretPosition
, keyCode
);
1188 SetDefaultStyleToCursorStyle();
1194 /// Extend the selection. Selections are in caret positions.
1195 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1197 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1199 if (oldPos
== newPos
)
1202 wxRichTextRange oldSelection
= m_selectionRange
;
1204 // If not currently selecting, start selecting
1205 if (m_selectionRange
.GetStart() == -2)
1207 m_selectionAnchor
= oldPos
;
1209 if (oldPos
> newPos
)
1210 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1212 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1216 // Always ensure that the selection range start is greater than
1218 if (newPos
> m_selectionAnchor
)
1219 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1220 else if (newPos
== m_selectionAnchor
)
1221 m_selectionRange
= wxRichTextRange(-2, -2);
1223 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1226 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1228 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1230 wxLogDebug(wxT("Strange selection range"));
1239 /// Scroll into view, returning true if we scrolled.
1240 /// This takes a _caret_ position.
1241 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1243 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1249 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1251 int startXUnits
, startYUnits
;
1252 GetViewStart(& startXUnits
, & startYUnits
);
1253 int startY
= startYUnits
* ppuY
;
1256 GetVirtualSize(& sx
, & sy
);
1262 wxRect rect
= line
->GetRect();
1264 bool scrolled
= false;
1266 wxSize clientSize
= GetClientSize();
1267 clientSize
.y
-= GetBuffer().GetBottomMargin();
1269 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1271 int y
= rect
.y
- GetClientSize().y
/2;
1272 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1273 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1275 if (startYUnits
!= yUnits
)
1277 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1280 #if !wxRICHTEXT_USE_OWN_CARET
1290 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1291 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1292 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1293 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1295 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1297 // Make it scroll so this item is at the bottom
1299 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1300 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1302 // If we're still off the screen, scroll another line down
1303 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1306 if (startYUnits
!= yUnits
)
1308 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1312 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1314 // Make it scroll so this item is at the top
1316 int y
= rect
.y
- GetBuffer().GetTopMargin();
1317 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1319 if (startYUnits
!= yUnits
)
1321 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1327 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1328 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1329 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1330 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1332 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1334 // Make it scroll so this item is at the top
1336 int y
= rect
.y
- GetBuffer().GetTopMargin();
1337 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1339 if (startYUnits
!= yUnits
)
1341 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1345 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1347 // Make it scroll so this item is at the bottom
1349 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1350 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1352 // If we're still off the screen, scroll another line down
1353 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1356 if (startYUnits
!= yUnits
)
1358 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1364 #if !wxRICHTEXT_USE_OWN_CARET
1372 /// Is the given position visible on the screen?
1373 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1375 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1381 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1384 GetViewStart(& startX
, & startY
);
1386 startY
= startY
* ppuY
;
1388 wxRect rect
= line
->GetRect();
1389 wxSize clientSize
= GetClientSize();
1390 clientSize
.y
-= GetBuffer().GetBottomMargin();
1392 return (rect
.GetBottom() > (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1395 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1397 m_caretPosition
= position
;
1398 m_caretAtLineStart
= showAtLineStart
;
1401 /// Move caret one visual step forward: this may mean setting a flag
1402 /// and keeping the same position if we're going from the end of one line
1403 /// to the start of the next, which may be the exact same caret position.
1404 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1406 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1408 // Only do the check if we're not at the end of the paragraph (where things work OK
1410 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1412 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1416 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1418 // We're at the end of a line. See whether we need to
1419 // stay at the same actual caret position but change visual
1420 // position, or not.
1421 if (oldPosition
== lineRange
.GetEnd())
1423 if (m_caretAtLineStart
)
1425 // We're already at the start of the line, so actually move on now.
1426 m_caretPosition
= oldPosition
+ 1;
1427 m_caretAtLineStart
= false;
1431 // We're showing at the end of the line, so keep to
1432 // the same position but indicate that we're to show
1433 // at the start of the next line.
1434 m_caretPosition
= oldPosition
;
1435 m_caretAtLineStart
= true;
1437 SetDefaultStyleToCursorStyle();
1443 SetDefaultStyleToCursorStyle();
1446 /// Move caret one visual step backward: this may mean setting a flag
1447 /// and keeping the same position if we're going from the end of one line
1448 /// to the start of the next, which may be the exact same caret position.
1449 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1451 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1453 // Only do the check if we're not at the start of the paragraph (where things work OK
1455 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1457 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1461 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1463 // We're at the start of a line. See whether we need to
1464 // stay at the same actual caret position but change visual
1465 // position, or not.
1466 if (oldPosition
== lineRange
.GetStart())
1468 m_caretPosition
= oldPosition
-1;
1469 m_caretAtLineStart
= true;
1472 else if (oldPosition
== lineRange
.GetEnd())
1474 if (m_caretAtLineStart
)
1476 // We're at the start of the line, so keep the same caret position
1477 // but clear the start-of-line flag.
1478 m_caretPosition
= oldPosition
;
1479 m_caretAtLineStart
= false;
1483 // We're showing at the end of the line, so go back
1484 // to the previous character position.
1485 m_caretPosition
= oldPosition
- 1;
1487 SetDefaultStyleToCursorStyle();
1493 SetDefaultStyleToCursorStyle();
1497 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1499 long endPos
= GetBuffer().GetRange().GetEnd();
1501 if (m_caretPosition
+ noPositions
< endPos
)
1503 long oldPos
= m_caretPosition
;
1504 long newPos
= m_caretPosition
+ noPositions
;
1506 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1510 // Determine by looking at oldPos and m_caretPosition whether
1511 // we moved from the end of a line to the start of the next line, in which case
1512 // we want to adjust the caret position such that it is positioned at the
1513 // start of the next line, rather than jumping past the first character of the
1515 if (noPositions
== 1 && !extendSel
)
1516 MoveCaretForward(oldPos
);
1518 SetCaretPosition(newPos
);
1521 SetDefaultStyleToCursorStyle();
1530 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1534 if (m_caretPosition
> startPos
- noPositions
+ 1)
1536 long oldPos
= m_caretPosition
;
1537 long newPos
= m_caretPosition
- noPositions
;
1538 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1542 if (noPositions
== 1 && !extendSel
)
1543 MoveCaretBack(oldPos
);
1545 SetCaretPosition(newPos
);
1548 SetDefaultStyleToCursorStyle();
1557 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1559 return MoveDown(- noLines
, flags
);
1563 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1568 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1569 wxPoint pt
= GetCaret()->GetPosition();
1570 long newLine
= lineNumber
+ noLines
;
1572 if (lineNumber
!= -1)
1576 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1578 if (newLine
> lastLine
)
1588 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1591 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1597 wxClientDC
dc(this);
1599 dc
.SetFont(GetFont());
1601 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1603 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1605 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1606 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1607 // so we view the caret at the start of the line.
1608 bool caretLineStart
= false;
1609 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1611 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1612 wxRichTextRange lineRange
;
1614 lineRange
= thisLine
->GetAbsoluteRange();
1616 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1619 caretLineStart
= true;
1623 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1624 if (para
&& para
->GetRange().GetStart() == newPos
)
1629 long newSelEnd
= newPos
;
1631 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1635 SetCaretPosition(newPos
, caretLineStart
);
1637 SetDefaultStyleToCursorStyle();
1645 /// Move to the end of the paragraph
1646 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1648 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1651 long newPos
= para
->GetRange().GetEnd() - 1;
1652 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1656 SetCaretPosition(newPos
);
1658 SetDefaultStyleToCursorStyle();
1666 /// Move to the start of the paragraph
1667 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1669 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1672 long newPos
= para
->GetRange().GetStart() - 1;
1673 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1677 SetCaretPosition(newPos
);
1679 SetDefaultStyleToCursorStyle();
1687 /// Move to the end of the line
1688 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1690 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1694 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1695 long newPos
= lineRange
.GetEnd();
1696 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1700 SetCaretPosition(newPos
);
1702 SetDefaultStyleToCursorStyle();
1710 /// Move to the start of the line
1711 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1713 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1716 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1717 long newPos
= lineRange
.GetStart()-1;
1719 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1723 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1725 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1727 SetDefaultStyleToCursorStyle();
1735 /// Move to the start of the buffer
1736 bool wxRichTextCtrl::MoveHome(int flags
)
1738 if (m_caretPosition
!= -1)
1740 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1744 SetCaretPosition(-1);
1746 SetDefaultStyleToCursorStyle();
1754 /// Move to the end of the buffer
1755 bool wxRichTextCtrl::MoveEnd(int flags
)
1757 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1759 if (m_caretPosition
!= endPos
)
1761 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1765 SetCaretPosition(endPos
);
1767 SetDefaultStyleToCursorStyle();
1775 /// Move noPages pages up
1776 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1778 return PageDown(- noPages
, flags
);
1781 /// Move noPages pages down
1782 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1784 // Calculate which line occurs noPages * screen height further down.
1785 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1788 wxSize clientSize
= GetClientSize();
1789 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1791 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1794 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1795 long pos
= lineRange
.GetStart()-1;
1796 if (pos
!= m_caretPosition
)
1798 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1800 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1804 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1806 SetDefaultStyleToCursorStyle();
1816 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1818 return str
== wxT(" ") || str
== wxT("\t");
1821 // Finds the caret position for the next word
1822 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1824 long endPos
= GetBuffer().GetRange().GetEnd();
1828 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1830 // First skip current text to space
1831 while (i
< endPos
&& i
> -1)
1833 // i is in character, not caret positions
1834 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1835 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1836 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1840 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1847 while (i
< endPos
&& i
> -1)
1849 // i is in character, not caret positions
1850 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1851 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1852 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1853 return wxMax(-1, i
);
1855 if (text
.empty()) // End of paragraph, or maybe an image
1856 return wxMax(-1, i
- 1);
1857 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1861 // Convert to caret position
1862 return wxMax(-1, i
- 1);
1871 long i
= m_caretPosition
;
1873 // First skip white space
1874 while (i
< endPos
&& i
> -1)
1876 // i is in character, not caret positions
1877 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1878 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1880 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1882 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1887 // Next skip current text to space
1888 while (i
< endPos
&& i
> -1)
1890 // i is in character, not caret positions
1891 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1892 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1893 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1896 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1909 /// Move n words left
1910 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1912 long pos
= FindNextWordPosition(-1);
1913 if (pos
!= m_caretPosition
)
1915 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1917 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1921 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1923 SetDefaultStyleToCursorStyle();
1931 /// Move n words right
1932 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1934 long pos
= FindNextWordPosition(1);
1935 if (pos
!= m_caretPosition
)
1937 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1939 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1943 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1945 SetDefaultStyleToCursorStyle();
1954 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1956 // Only do sizing optimization for large buffers
1957 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1959 m_fullLayoutRequired
= true;
1960 m_fullLayoutTime
= wxGetLocalTimeMillis();
1961 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1962 LayoutContent(true /* onlyVisibleRect */);
1965 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1967 #if wxRICHTEXT_BUFFERED_PAINTING
1975 /// Idle-time processing
1976 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1978 #if wxRICHTEXT_USE_OWN_CARET
1979 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1981 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1987 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1989 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1991 m_fullLayoutRequired
= false;
1992 m_fullLayoutTime
= 0;
1993 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1994 ShowPosition(m_fullLayoutSavedPosition
);
1998 if (m_caretPositionForDefaultStyle
!= -2)
2000 // If the caret position has changed, no longer reflect the default style
2002 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2003 m_caretPositionForDefaultStyle
= -2;
2010 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2012 #if wxRICHTEXT_USE_OWN_CARET
2013 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2016 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2023 /// Set up scrollbars, e.g. after a resize
2024 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2029 if (GetBuffer().IsEmpty())
2031 SetScrollbars(0, 0, 0, 0, 0, 0);
2035 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2036 // of pixels. See e.g. wxVScrolledWindow for ideas.
2037 int pixelsPerUnit
= 5;
2038 wxSize clientSize
= GetClientSize();
2040 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2042 // Round up so we have at least maxHeight pixels
2043 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2045 int startX
= 0, startY
= 0;
2047 GetViewStart(& startX
, & startY
);
2049 int maxPositionX
= 0;
2050 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2052 int newStartX
= wxMin(maxPositionX
, startX
);
2053 int newStartY
= wxMin(maxPositionY
, startY
);
2055 int oldPPUX
, oldPPUY
;
2056 int oldStartX
, oldStartY
;
2057 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2058 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2059 GetViewStart(& oldStartX
, & oldStartY
);
2060 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2062 oldVirtualSizeY
/= oldPPUY
;
2064 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2067 // Don't set scrollbars if there were none before, and there will be none now.
2068 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2071 // Move to previous scroll position if
2073 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2076 /// Paint the background
2077 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2079 wxColour backgroundColour
= GetBackgroundColour();
2080 if (!backgroundColour
.Ok())
2081 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2083 // Clear the background
2084 dc
.SetBrush(wxBrush(backgroundColour
));
2085 dc
.SetPen(*wxTRANSPARENT_PEN
);
2086 wxRect
windowRect(GetClientSize());
2087 windowRect
.x
-= 2; windowRect
.y
-= 2;
2088 windowRect
.width
+= 4; windowRect
.height
+= 4;
2090 // We need to shift the rectangle to take into account
2091 // scrolling. Converting device to logical coordinates.
2092 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2093 dc
.DrawRectangle(windowRect
);
2096 #if wxRICHTEXT_BUFFERED_PAINTING
2097 /// Recreate buffer bitmap if necessary
2098 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2101 if (sz
== wxDefaultSize
)
2102 sz
= GetClientSize();
2104 if (sz
.x
< 1 || sz
.y
< 1)
2107 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2108 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2109 return m_bufferBitmap
.Ok();
2113 // ----------------------------------------------------------------------------
2114 // file IO functions
2115 // ----------------------------------------------------------------------------
2117 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2119 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2121 m_filename
= filename
;
2124 SetInsertionPoint(0);
2127 SetupScrollbars(true);
2129 wxTextCtrl::SendTextUpdatedEvent(this);
2135 wxLogError(_("File couldn't be loaded."));
2141 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2143 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2145 m_filename
= filename
;
2152 wxLogError(_("The text couldn't be saved."));
2157 // ----------------------------------------------------------------------------
2158 // wxRichTextCtrl specific functionality
2159 // ----------------------------------------------------------------------------
2161 /// Add a new paragraph of text to the end of the buffer
2162 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2164 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2170 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2172 wxRichTextRange range
= GetBuffer().AddImage(image
);
2177 // ----------------------------------------------------------------------------
2178 // selection and ranges
2179 // ----------------------------------------------------------------------------
2181 void wxRichTextCtrl::SelectAll()
2183 SetSelection(0, GetLastPosition()+1);
2184 m_selectionAnchor
= -1;
2188 void wxRichTextCtrl::SelectNone()
2190 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2192 wxRichTextRange oldSelection
= m_selectionRange
;
2194 m_selectionRange
= wxRichTextRange(-2, -2);
2196 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2198 m_selectionAnchor
= -2;
2201 static bool wxIsWordDelimiter(const wxString
& text
)
2203 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2206 /// Select the word at the given character position
2207 bool wxRichTextCtrl::SelectWord(long position
)
2209 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2212 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2216 if (position
== para
->GetRange().GetEnd())
2219 long positionStart
= position
;
2220 long positionEnd
= position
;
2222 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2224 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2225 if (wxIsWordDelimiter(text
))
2231 if (positionStart
< para
->GetRange().GetStart())
2232 positionStart
= para
->GetRange().GetStart();
2234 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2236 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2237 if (wxIsWordDelimiter(text
))
2243 if (positionEnd
>= para
->GetRange().GetEnd())
2244 positionEnd
= para
->GetRange().GetEnd();
2246 if (positionEnd
< positionStart
)
2249 SetSelection(positionStart
, positionEnd
+1);
2251 if (positionStart
>= 0)
2253 MoveCaret(positionStart
-1, true);
2254 SetDefaultStyleToCursorStyle();
2260 wxString
wxRichTextCtrl::GetStringSelection() const
2263 GetSelection(&from
, &to
);
2265 return GetRange(from
, to
);
2268 // ----------------------------------------------------------------------------
2270 // ----------------------------------------------------------------------------
2272 wxTextCtrlHitTestResult
2273 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2275 // implement in terms of the other overload as the native ports typically
2276 // can get the position and not (x, y) pair directly (although wxUniv
2277 // directly gets x and y -- and so overrides this method as well)
2279 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2281 if ( rc
!= wxTE_HT_UNKNOWN
)
2283 PositionToXY(pos
, x
, y
);
2289 wxTextCtrlHitTestResult
2290 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2293 wxClientDC
dc((wxRichTextCtrl
*) this);
2294 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2296 // Buffer uses logical position (relative to start of buffer)
2298 wxPoint pt2
= GetLogicalPoint(pt
);
2300 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2302 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2303 return wxTE_HT_BEFORE
;
2304 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2305 return wxTE_HT_BEYOND
;
2306 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2307 return wxTE_HT_ON_TEXT
;
2309 return wxTE_HT_UNKNOWN
;
2312 // ----------------------------------------------------------------------------
2313 // set/get the controls text
2314 // ----------------------------------------------------------------------------
2316 wxString
wxRichTextCtrl::DoGetValue() const
2318 return GetBuffer().GetText();
2321 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2323 // Public API for range is different from internals
2324 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2327 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2329 // Don't call Clear here, since it always sends a text updated event
2330 m_buffer
.ResetAndClearCommands();
2331 m_buffer
.SetDirty(true);
2332 m_caretPosition
= -1;
2333 m_caretPositionForDefaultStyle
= -2;
2334 m_caretAtLineStart
= false;
2335 m_selectionRange
.SetRange(-2, -2);
2345 if (!value
.IsEmpty())
2347 // Remove empty paragraph
2348 GetBuffer().Clear();
2349 DoWriteText(value
, flags
);
2351 // for compatibility, don't move the cursor when doing SetValue()
2352 SetInsertionPoint(0);
2356 // still send an event for consistency
2357 if (flags
& SetValue_SendEvent
)
2358 wxTextCtrl::SendTextUpdatedEvent(this);
2363 void wxRichTextCtrl::WriteText(const wxString
& value
)
2368 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2370 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2372 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2374 if ( flags
& SetValue_SendEvent
)
2375 wxTextCtrl::SendTextUpdatedEvent(this);
2378 void wxRichTextCtrl::AppendText(const wxString
& text
)
2380 SetInsertionPointEnd();
2385 /// Write an image at the current insertion point
2386 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2388 wxRichTextImageBlock imageBlock
;
2390 wxImage image2
= image
;
2391 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2392 return WriteImage(imageBlock
);
2397 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2399 wxRichTextImageBlock imageBlock
;
2402 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2403 return WriteImage(imageBlock
);
2408 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2410 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2413 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2417 wxRichTextImageBlock imageBlock
;
2419 wxImage image
= bitmap
.ConvertToImage();
2420 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2421 return WriteImage(imageBlock
);
2427 /// Insert a newline (actually paragraph) at the current insertion point.
2428 bool wxRichTextCtrl::Newline()
2430 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2433 /// Insert a line break at the current insertion point.
2434 bool wxRichTextCtrl::LineBreak()
2437 text
= wxRichTextLineBreakChar
;
2438 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2441 // ----------------------------------------------------------------------------
2442 // Clipboard operations
2443 // ----------------------------------------------------------------------------
2445 void wxRichTextCtrl::Copy()
2449 wxRichTextRange range
= GetInternalSelectionRange();
2450 GetBuffer().CopyToClipboard(range
);
2454 void wxRichTextCtrl::Cut()
2458 wxRichTextRange range
= GetInternalSelectionRange();
2459 GetBuffer().CopyToClipboard(range
);
2461 DeleteSelectedContent();
2467 void wxRichTextCtrl::Paste()
2471 BeginBatchUndo(_("Paste"));
2473 long newPos
= m_caretPosition
;
2474 DeleteSelectedContent(& newPos
);
2476 GetBuffer().PasteFromClipboard(newPos
);
2482 void wxRichTextCtrl::DeleteSelection()
2484 if (CanDeleteSelection())
2486 DeleteSelectedContent();
2490 bool wxRichTextCtrl::HasSelection() const
2492 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2495 bool wxRichTextCtrl::CanCopy() const
2497 // Can copy if there's a selection
2498 return HasSelection();
2501 bool wxRichTextCtrl::CanCut() const
2503 return HasSelection() && IsEditable();
2506 bool wxRichTextCtrl::CanPaste() const
2508 if ( !IsEditable() )
2511 return GetBuffer().CanPasteFromClipboard();
2514 bool wxRichTextCtrl::CanDeleteSelection() const
2516 return HasSelection() && IsEditable();
2520 // ----------------------------------------------------------------------------
2522 // ----------------------------------------------------------------------------
2524 void wxRichTextCtrl::SetEditable(bool editable
)
2526 m_editable
= editable
;
2529 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2533 m_caretPosition
= pos
- 1;
2538 void wxRichTextCtrl::SetInsertionPointEnd()
2540 long pos
= GetLastPosition();
2541 SetInsertionPoint(pos
);
2544 long wxRichTextCtrl::GetInsertionPoint() const
2546 return m_caretPosition
+1;
2549 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2551 return GetBuffer().GetRange().GetEnd();
2554 // If the return values from and to are the same, there is no
2556 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2558 *from
= m_selectionRange
.GetStart();
2559 *to
= m_selectionRange
.GetEnd();
2560 if ((*to
) != -1 && (*to
) != -2)
2564 bool wxRichTextCtrl::IsEditable() const
2569 // ----------------------------------------------------------------------------
2571 // ----------------------------------------------------------------------------
2573 void wxRichTextCtrl::SetSelection(long from
, long to
)
2575 // if from and to are both -1, it means (in wxWidgets) that all text should
2577 if ( (from
== -1) && (to
== -1) )
2580 to
= GetLastPosition()+1;
2589 wxRichTextRange oldSelection
= m_selectionRange
;
2590 m_selectionAnchor
= from
;
2591 m_selectionRange
.SetRange(from
, to
-1);
2593 m_caretPosition
= from
-1;
2595 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2600 // ----------------------------------------------------------------------------
2602 // ----------------------------------------------------------------------------
2604 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2605 const wxString
& value
)
2607 BeginBatchUndo(_("Replace"));
2609 DeleteSelectedContent();
2611 DoWriteText(value
, SetValue_SelectionOnly
);
2616 void wxRichTextCtrl::Remove(long from
, long to
)
2620 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2627 bool wxRichTextCtrl::IsModified() const
2629 return m_buffer
.IsModified();
2632 void wxRichTextCtrl::MarkDirty()
2634 m_buffer
.Modify(true);
2637 void wxRichTextCtrl::DiscardEdits()
2639 m_caretPositionForDefaultStyle
= -2;
2640 m_buffer
.Modify(false);
2641 m_buffer
.GetCommandProcessor()->ClearCommands();
2644 int wxRichTextCtrl::GetNumberOfLines() const
2646 return GetBuffer().GetParagraphCount();
2649 // ----------------------------------------------------------------------------
2650 // Positions <-> coords
2651 // ----------------------------------------------------------------------------
2653 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2655 return GetBuffer().XYToPosition(x
, y
);
2658 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2660 return GetBuffer().PositionToXY(pos
, x
, y
);
2663 // ----------------------------------------------------------------------------
2665 // ----------------------------------------------------------------------------
2667 void wxRichTextCtrl::ShowPosition(long pos
)
2669 if (!IsPositionVisible(pos
))
2670 ScrollIntoView(pos
-1, WXK_DOWN
);
2673 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2675 return GetBuffer().GetParagraphLength(lineNo
);
2678 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2680 return GetBuffer().GetParagraphText(lineNo
);
2683 // ----------------------------------------------------------------------------
2685 // ----------------------------------------------------------------------------
2687 void wxRichTextCtrl::Undo()
2691 GetCommandProcessor()->Undo();
2695 void wxRichTextCtrl::Redo()
2699 GetCommandProcessor()->Redo();
2703 bool wxRichTextCtrl::CanUndo() const
2705 return GetCommandProcessor()->CanUndo();
2708 bool wxRichTextCtrl::CanRedo() const
2710 return GetCommandProcessor()->CanRedo();
2713 // ----------------------------------------------------------------------------
2714 // implementation details
2715 // ----------------------------------------------------------------------------
2717 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2719 SetValue(event
.GetString());
2720 GetEventHandler()->ProcessEvent(event
);
2723 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2725 // By default, load the first file into the text window.
2726 if (event
.GetNumberOfFiles() > 0)
2728 LoadFile(event
.GetFiles()[0]);
2732 wxSize
wxRichTextCtrl::DoGetBestSize() const
2734 return wxSize(10, 10);
2737 // ----------------------------------------------------------------------------
2738 // standard handlers for standard edit menu events
2739 // ----------------------------------------------------------------------------
2741 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2746 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2751 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2756 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2761 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2766 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2771 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2773 event
.Enable( CanCut() );
2776 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2778 event
.Enable( CanCopy() );
2781 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2783 event
.Enable( CanDeleteSelection() );
2786 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2788 event
.Enable( CanPaste() );
2791 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2793 event
.Enable( CanUndo() );
2794 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2797 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2799 event
.Enable( CanRedo() );
2800 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2803 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2808 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2810 event
.Enable(GetLastPosition() > 0);
2813 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2815 if (event
.GetEventObject() != this)
2823 m_contextMenu
= new wxMenu
;
2824 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2825 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2826 m_contextMenu
->AppendSeparator();
2827 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2828 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2829 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2830 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2831 m_contextMenu
->AppendSeparator();
2832 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2834 PopupMenu(m_contextMenu
);
2838 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2840 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2843 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2845 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2848 // extended style setting operation with flags including:
2849 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2850 // see richtextbuffer.h for more details.
2852 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2854 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2857 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2859 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2862 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2864 return GetBuffer().GetDefaultStyle();
2867 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2869 return GetBuffer().GetStyle(position
, style
);
2872 // get the common set of styles for the range
2873 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2875 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2878 /// Get the content (uncombined) attributes for this position.
2879 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2881 return GetBuffer().GetUncombinedStyle(position
, style
);
2884 /// Set font, and also the buffer attributes
2885 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2887 wxTextCtrlBase::SetFont(font
);
2889 wxTextAttr attr
= GetBuffer().GetAttributes();
2891 GetBuffer().SetBasicStyle(attr
);
2893 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2899 /// Transform logical to physical
2900 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2903 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2908 /// Transform physical to logical
2909 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2912 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2917 /// Position the caret
2918 void wxRichTextCtrl::PositionCaret()
2923 //wxLogDebug(wxT("PositionCaret"));
2926 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2928 wxPoint newPt
= caretRect
.GetPosition();
2929 wxSize newSz
= caretRect
.GetSize();
2930 wxPoint pt
= GetPhysicalPoint(newPt
);
2931 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2934 if (GetCaret()->GetSize() != newSz
)
2935 GetCaret()->SetSize(newSz
);
2937 int halfSize
= newSz
.y
/2;
2938 // If the caret is beyond the margin, hide it by moving it out of the way
2939 if (((pt
.y
+ halfSize
) < GetBuffer().GetTopMargin()) || ((pt
.y
+ halfSize
) > (GetClientSize().y
- GetBuffer().GetBottomMargin())))
2942 GetCaret()->Move(pt
);
2948 /// Get the caret height and position for the given character position
2949 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2951 wxClientDC
dc(this);
2952 dc
.SetFont(GetFont());
2959 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2961 // Caret height can't be zero
2963 height
= dc
.GetCharHeight();
2965 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2972 /// Gets the line for the visible caret position. If the caret is
2973 /// shown at the very end of the line, it means the next character is actually
2974 /// on the following line. So let's get the line we're expecting to find
2975 /// if this is the case.
2976 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2978 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2979 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2982 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2983 if (caretPosition
== lineRange
.GetStart()-1 &&
2984 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2986 if (!m_caretAtLineStart
)
2987 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2994 /// Move the caret to the given character position
2995 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2997 if (GetBuffer().GetDirty())
3000 if (pos
<= GetBuffer().GetRange().GetEnd())
3002 SetCaretPosition(pos
, showAtLineStart
);
3012 /// Layout the buffer: which we must do before certain operations, such as
3013 /// setting the caret position.
3014 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3016 if (GetBuffer().GetDirty() || onlyVisibleRect
)
3018 wxRect
availableSpace(GetClientSize());
3019 if (availableSpace
.width
== 0)
3020 availableSpace
.width
= 10;
3021 if (availableSpace
.height
== 0)
3022 availableSpace
.height
= 10;
3024 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3025 if (onlyVisibleRect
)
3027 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3028 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3031 wxClientDC
dc(this);
3032 dc
.SetFont(GetFont());
3036 GetBuffer().Defragment();
3037 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3038 GetBuffer().Layout(dc
, availableSpace
, flags
);
3039 GetBuffer().SetDirty(false);
3048 /// Is all of the selection bold?
3049 bool wxRichTextCtrl::IsSelectionBold()
3054 wxRichTextRange range
= GetSelectionRange();
3055 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3056 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3058 return HasCharacterAttributes(range
, attr
);
3062 // If no selection, then we need to combine current style with default style
3063 // to see what the effect would be if we started typing.
3065 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3067 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3068 if (GetStyle(pos
, attr
))
3070 if (IsDefaultStyleShowing())
3071 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3072 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3078 /// Is all of the selection italics?
3079 bool wxRichTextCtrl::IsSelectionItalics()
3083 wxRichTextRange range
= GetSelectionRange();
3085 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3086 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3088 return HasCharacterAttributes(range
, attr
);
3092 // If no selection, then we need to combine current style with default style
3093 // to see what the effect would be if we started typing.
3095 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3097 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3098 if (GetStyle(pos
, attr
))
3100 if (IsDefaultStyleShowing())
3101 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3102 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3108 /// Is all of the selection underlined?
3109 bool wxRichTextCtrl::IsSelectionUnderlined()
3113 wxRichTextRange range
= GetSelectionRange();
3115 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3116 attr
.SetFontUnderlined(true);
3118 return HasCharacterAttributes(range
, attr
);
3122 // If no selection, then we need to combine current style with default style
3123 // to see what the effect would be if we started typing.
3125 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3126 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3128 if (GetStyle(pos
, attr
))
3130 if (IsDefaultStyleShowing())
3131 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3132 return attr
.GetFontUnderlined();
3138 /// Apply bold to the selection
3139 bool wxRichTextCtrl::ApplyBoldToSelection()
3142 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3143 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3146 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3149 wxRichTextAttr current
= GetDefaultStyleEx();
3150 current
.Apply(attr
);
3151 SetAndShowDefaultStyle(current
);
3156 /// Apply italic to the selection
3157 bool wxRichTextCtrl::ApplyItalicToSelection()
3160 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3161 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3164 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3167 wxRichTextAttr current
= GetDefaultStyleEx();
3168 current
.Apply(attr
);
3169 SetAndShowDefaultStyle(current
);
3174 /// Apply underline to the selection
3175 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3178 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3179 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3182 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3185 wxRichTextAttr current
= GetDefaultStyleEx();
3186 current
.Apply(attr
);
3187 SetAndShowDefaultStyle(current
);
3192 /// Is all of the selection aligned according to the specified flag?
3193 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3195 wxRichTextRange range
;
3197 range
= GetSelectionRange();
3199 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3202 attr
.SetAlignment(alignment
);
3204 return HasParagraphAttributes(range
, attr
);
3207 /// Apply alignment to the selection
3208 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3211 attr
.SetAlignment(alignment
);
3213 return SetStyle(GetSelectionRange(), attr
);
3216 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3218 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3223 /// Apply a named style to the selection
3224 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3226 // Flags are defined within each definition, so only certain
3227 // attributes are applied.
3228 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3230 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3232 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3234 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3236 wxRichTextRange range
;
3239 range
= GetSelectionRange();
3242 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3243 range
= wxRichTextRange(pos
, pos
+1);
3246 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3249 // Make sure the attr has the style name
3250 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3252 attr
.SetParagraphStyleName(def
->GetName());
3254 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3255 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3256 // to change its style independently.
3257 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3260 attr
.SetCharacterStyleName(def
->GetName());
3263 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3266 wxRichTextAttr current
= GetDefaultStyleEx();
3267 current
.Apply(attr
);
3268 SetAndShowDefaultStyle(current
);
3273 /// Apply the style sheet to the buffer, for example if the styles have changed.
3274 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3277 styleSheet
= GetBuffer().GetStyleSheet();
3281 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3283 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3291 /// Sets the default style to the style under the cursor
3292 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3295 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3297 // If at the start of a paragraph, use the next position.
3298 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3300 if (GetUncombinedStyle(pos
, attr
))
3302 SetDefaultStyle(attr
);
3309 /// Returns the first visible position in the current view
3310 long wxRichTextCtrl::GetFirstVisiblePosition() const
3312 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3314 return line
->GetAbsoluteRange().GetStart();
3319 /// Get the first visible point in the window
3320 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3323 int startXUnits
, startYUnits
;
3325 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3326 GetViewStart(& startXUnits
, & startYUnits
);
3328 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3331 /// The adjusted caret position is the character position adjusted to take
3332 /// into account whether we're at the start of a paragraph, in which case
3333 /// style information should be taken from the next position, not current one.
3334 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3336 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3338 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3343 /// Get/set the selection range in character positions. -1, -1 means no selection.
3344 /// The range is in API convention, i.e. a single character selection is denoted
3346 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3348 wxRichTextRange range
= GetInternalSelectionRange();
3349 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3350 range
.SetEnd(range
.GetEnd() + 1);
3354 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3356 wxRichTextRange
range1(range
);
3357 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3358 range1
.SetEnd(range1
.GetEnd() - 1);
3360 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3362 SetInternalSelectionRange(range1
);
3366 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3368 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3371 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3373 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3376 /// Clear list for given range
3377 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3379 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3382 /// Number/renumber any list elements in the given range
3383 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3385 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3388 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3390 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3393 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3394 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3396 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3399 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3401 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3404 /// Deletes the content in the given range
3405 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3407 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3410 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3412 if (sm_availableFontNames
.GetCount() == 0)
3414 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3415 sm_availableFontNames
.Sort();
3417 return sm_availableFontNames
;
3420 void wxRichTextCtrl::ClearAvailableFontNames()
3422 sm_availableFontNames
.Clear();
3425 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3427 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3429 wxTextAttrEx basicStyle
= GetBasicStyle();
3430 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3431 SetBasicStyle(basicStyle
);
3432 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3437 // Refresh the area affected by a selection change
3438 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3440 // Calculate the refresh rectangle - just the affected lines
3441 long firstPos
, lastPos
;
3442 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3444 firstPos
= newSelection
.GetStart();
3445 lastPos
= newSelection
.GetEnd();
3447 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3449 firstPos
= oldSelection
.GetStart();
3450 lastPos
= oldSelection
.GetEnd();
3452 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3458 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3459 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3462 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3463 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3465 if (firstLine
&& lastLine
)
3467 wxSize clientSize
= GetClientSize();
3468 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3469 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3472 pt1
.y
= wxMax(0, pt1
.y
);
3474 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3476 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3477 RefreshRect(rect
, false);
3485 #if wxRICHTEXT_USE_OWN_CARET
3487 // ----------------------------------------------------------------------------
3488 // initialization and destruction
3489 // ----------------------------------------------------------------------------
3491 void wxRichTextCaret::Init()
3497 m_richTextCtrl
= NULL
;
3498 m_needsUpdate
= false;
3502 wxRichTextCaret::~wxRichTextCaret()
3504 if (m_timer
.IsRunning())
3508 // ----------------------------------------------------------------------------
3509 // showing/hiding/moving the caret (base class interface)
3510 // ----------------------------------------------------------------------------
3512 void wxRichTextCaret::DoShow()
3516 if (!m_timer
.IsRunning())
3517 m_timer
.Start(GetBlinkTime());
3522 void wxRichTextCaret::DoHide()
3524 if (m_timer
.IsRunning())
3530 void wxRichTextCaret::DoMove()
3536 if (m_xOld
!= -1 && m_yOld
!= -1)
3540 wxRect
rect(GetPosition(), GetSize());
3541 m_richTextCtrl
->RefreshRect(rect
, false);
3550 void wxRichTextCaret::DoSize()
3552 int countVisible
= m_countVisible
;
3553 if (countVisible
> 0)
3559 if (countVisible
> 0)
3561 m_countVisible
= countVisible
;
3566 // ----------------------------------------------------------------------------
3567 // handling the focus
3568 // ----------------------------------------------------------------------------
3570 void wxRichTextCaret::OnSetFocus()
3578 void wxRichTextCaret::OnKillFocus()
3583 // ----------------------------------------------------------------------------
3584 // drawing the caret
3585 // ----------------------------------------------------------------------------
3587 void wxRichTextCaret::Refresh()
3591 wxRect
rect(GetPosition(), GetSize());
3592 m_richTextCtrl
->RefreshRect(rect
, false);
3596 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3598 dc
->SetPen( *wxBLACK_PEN
);
3600 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3601 dc
->SetPen(*wxBLACK_PEN
);
3603 wxPoint
pt(m_x
, m_y
);
3607 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3609 if (IsVisible() && m_flashOn
)
3610 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3613 void wxRichTextCaret::Notify()
3615 m_flashOn
= !m_flashOn
;
3619 void wxRichTextCaretTimer::Notify()
3624 // wxRICHTEXT_USE_OWN_CARET