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_WINDOWS_LEFT
:
796 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
797 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
799 BeginBatchUndo(_("Delete Text"));
801 long newPos
= m_caretPosition
;
803 bool processed
= DeleteSelectedContent(& newPos
);
805 // Submit range in character positions, which are greater than caret positions,
806 // so subtract 1 for deleted character and add 1 for conversion to character position.
811 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
814 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
820 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
825 if (GetLastPosition() == -1)
829 m_caretPosition
= -1;
831 SetDefaultStyleToCursorStyle();
834 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
836 wxRichTextEvent
cmdEvent(
837 wxEVT_COMMAND_RICHTEXT_DELETE
,
839 cmdEvent
.SetEventObject(this);
840 cmdEvent
.SetFlags(flags
);
841 cmdEvent
.SetPosition(m_caretPosition
+1);
842 GetEventHandler()->ProcessEvent(cmdEvent
);
852 // all the other keys modify the controls contents which shouldn't be
853 // possible if we're read-only
860 if (event
.GetKeyCode() == WXK_RETURN
)
862 BeginBatchUndo(_("Insert Text"));
864 long newPos
= m_caretPosition
;
866 DeleteSelectedContent(& newPos
);
868 if (event
.ShiftDown())
871 text
= wxRichTextLineBreakChar
;
872 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
873 m_caretAtLineStart
= true;
877 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
880 SetDefaultStyleToCursorStyle();
882 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
884 wxRichTextEvent
cmdEvent(
885 wxEVT_COMMAND_RICHTEXT_RETURN
,
887 cmdEvent
.SetEventObject(this);
888 cmdEvent
.SetFlags(flags
);
889 cmdEvent
.SetPosition(newPos
+1);
891 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
893 // Generate conventional event
894 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
895 InitCommandEvent(textEvent
);
897 GetEventHandler()->ProcessEvent(textEvent
);
901 else if (event
.GetKeyCode() == WXK_BACK
)
903 BeginBatchUndo(_("Delete Text"));
905 long newPos
= m_caretPosition
;
907 bool processed
= DeleteSelectedContent(& newPos
);
909 // Submit range in character positions, which are greater than caret positions,
910 // so subtract 1 for deleted character and add 1 for conversion to character position.
915 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
918 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
924 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
929 if (GetLastPosition() == -1)
933 m_caretPosition
= -1;
935 SetDefaultStyleToCursorStyle();
938 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
940 wxRichTextEvent
cmdEvent(
941 wxEVT_COMMAND_RICHTEXT_DELETE
,
943 cmdEvent
.SetEventObject(this);
944 cmdEvent
.SetFlags(flags
);
945 cmdEvent
.SetPosition(m_caretPosition
+1);
946 GetEventHandler()->ProcessEvent(cmdEvent
);
950 else if (event
.GetKeyCode() == WXK_DELETE
)
952 BeginBatchUndo(_("Delete Text"));
954 long newPos
= m_caretPosition
;
956 bool processed
= DeleteSelectedContent(& newPos
);
958 // Submit range in character positions, which are greater than caret positions,
959 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
963 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
964 if (pos
!= -1 && (pos
> newPos
))
966 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
972 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
977 if (GetLastPosition() == -1)
981 m_caretPosition
= -1;
983 SetDefaultStyleToCursorStyle();
986 wxRichTextEvent
cmdEvent(
987 wxEVT_COMMAND_RICHTEXT_DELETE
,
989 cmdEvent
.SetEventObject(this);
990 cmdEvent
.SetFlags(flags
);
991 cmdEvent
.SetPosition(m_caretPosition
+1);
992 GetEventHandler()->ProcessEvent(cmdEvent
);
998 long keycode
= event
.GetKeyCode();
1010 if (event
.CmdDown())
1012 // Fixes AltGr+key with European input languages on Windows
1013 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1020 wxRichTextEvent
cmdEvent(
1021 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1023 cmdEvent
.SetEventObject(this);
1024 cmdEvent
.SetFlags(flags
);
1026 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1028 cmdEvent
.SetCharacter((wxChar
) keycode
);
1030 cmdEvent
.SetPosition(m_caretPosition
+1);
1032 if (keycode
== wxT('\t'))
1034 // See if we need to promote or demote the selection or paragraph at the cursor
1035 // position, instead of inserting a tab.
1036 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1037 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1038 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1040 wxRichTextRange range
;
1042 range
= GetSelectionRange();
1044 range
= para
->GetRange().FromInternal();
1046 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1048 PromoteList(promoteBy
, range
, NULL
);
1050 GetEventHandler()->ProcessEvent(cmdEvent
);
1056 BeginBatchUndo(_("Insert Text"));
1058 long newPos
= m_caretPosition
;
1059 DeleteSelectedContent(& newPos
);
1062 wxString str
= event
.GetUnicodeKey();
1064 wxString str
= (wxChar
) event
.GetKeyCode();
1066 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1070 SetDefaultStyleToCursorStyle();
1071 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1073 GetEventHandler()->ProcessEvent(cmdEvent
);
1081 /// Delete content if there is a selection, e.g. when pressing a key.
1082 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1086 long pos
= m_selectionRange
.GetStart();
1087 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1088 m_selectionRange
.SetRange(-2, -2);
1098 /// Keyboard navigation
1102 Left: left one character
1103 Right: right one character
1106 Ctrl-Left: left one word
1107 Ctrl-Right: right one word
1108 Ctrl-Up: previous paragraph start
1109 Ctrl-Down: next start of paragraph
1112 Ctrl-Home: start of document
1113 Ctrl-End: end of document
1114 Page-Up: Up a screen
1115 Page-Down: Down a screen
1119 Ctrl-Alt-PgUp: Start of window
1120 Ctrl-Alt-PgDn: End of window
1121 F8: Start selection mode
1122 Esc: End selection mode
1124 Adding Shift does the above but starts/extends selection.
1129 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1131 bool success
= false;
1133 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1135 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1136 success
= WordRight(1, flags
);
1138 success
= MoveRight(1, flags
);
1140 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1142 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1143 success
= WordLeft(1, flags
);
1145 success
= MoveLeft(1, flags
);
1147 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1149 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1150 success
= MoveToParagraphStart(flags
);
1152 success
= MoveUp(1, flags
);
1154 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1156 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1157 success
= MoveToParagraphEnd(flags
);
1159 success
= MoveDown(1, flags
);
1161 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1163 success
= PageUp(1, flags
);
1165 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1167 success
= PageDown(1, flags
);
1169 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1171 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1172 success
= MoveHome(flags
);
1174 success
= MoveToLineStart(flags
);
1176 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1178 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1179 success
= MoveEnd(flags
);
1181 success
= MoveToLineEnd(flags
);
1186 ScrollIntoView(m_caretPosition
, keyCode
);
1187 SetDefaultStyleToCursorStyle();
1193 /// Extend the selection. Selections are in caret positions.
1194 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1196 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1198 if (oldPos
== newPos
)
1201 wxRichTextRange oldSelection
= m_selectionRange
;
1203 // If not currently selecting, start selecting
1204 if (m_selectionRange
.GetStart() == -2)
1206 m_selectionAnchor
= oldPos
;
1208 if (oldPos
> newPos
)
1209 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1211 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1215 // Always ensure that the selection range start is greater than
1217 if (newPos
> m_selectionAnchor
)
1218 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1219 else if (newPos
== m_selectionAnchor
)
1220 m_selectionRange
= wxRichTextRange(-2, -2);
1222 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1225 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1227 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1229 wxLogDebug(wxT("Strange selection range"));
1238 /// Scroll into view, returning true if we scrolled.
1239 /// This takes a _caret_ position.
1240 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1242 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1248 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1250 int startXUnits
, startYUnits
;
1251 GetViewStart(& startXUnits
, & startYUnits
);
1252 int startY
= startYUnits
* ppuY
;
1255 GetVirtualSize(& sx
, & sy
);
1261 wxRect rect
= line
->GetRect();
1263 bool scrolled
= false;
1265 wxSize clientSize
= GetClientSize();
1266 clientSize
.y
-= GetBuffer().GetBottomMargin();
1268 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1270 int y
= rect
.y
- GetClientSize().y
/2;
1271 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1272 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1274 if (startYUnits
!= yUnits
)
1276 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1279 #if !wxRICHTEXT_USE_OWN_CARET
1289 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1290 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1291 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1292 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1294 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1296 // Make it scroll so this item is at the bottom
1298 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1299 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1301 // If we're still off the screen, scroll another line down
1302 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1305 if (startYUnits
!= yUnits
)
1307 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1311 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1313 // Make it scroll so this item is at the top
1315 int y
= rect
.y
- GetBuffer().GetTopMargin();
1316 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1318 if (startYUnits
!= yUnits
)
1320 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1326 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1327 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1328 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1329 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1331 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1333 // Make it scroll so this item is at the top
1335 int y
= rect
.y
- GetBuffer().GetTopMargin();
1336 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1338 if (startYUnits
!= yUnits
)
1340 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1344 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1346 // Make it scroll so this item is at the bottom
1348 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1349 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1351 // If we're still off the screen, scroll another line down
1352 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1355 if (startYUnits
!= yUnits
)
1357 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1363 #if !wxRICHTEXT_USE_OWN_CARET
1371 /// Is the given position visible on the screen?
1372 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1374 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1380 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1383 GetViewStart(& startX
, & startY
);
1385 startY
= startY
* ppuY
;
1387 wxRect rect
= line
->GetRect();
1388 wxSize clientSize
= GetClientSize();
1389 clientSize
.y
-= GetBuffer().GetBottomMargin();
1391 return (rect
.GetBottom() > (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1394 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1396 m_caretPosition
= position
;
1397 m_caretAtLineStart
= showAtLineStart
;
1400 /// Move caret one visual step forward: this may mean setting a flag
1401 /// and keeping the same position if we're going from the end of one line
1402 /// to the start of the next, which may be the exact same caret position.
1403 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1405 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1407 // Only do the check if we're not at the end of the paragraph (where things work OK
1409 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1411 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1415 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1417 // We're at the end of a line. See whether we need to
1418 // stay at the same actual caret position but change visual
1419 // position, or not.
1420 if (oldPosition
== lineRange
.GetEnd())
1422 if (m_caretAtLineStart
)
1424 // We're already at the start of the line, so actually move on now.
1425 m_caretPosition
= oldPosition
+ 1;
1426 m_caretAtLineStart
= false;
1430 // We're showing at the end of the line, so keep to
1431 // the same position but indicate that we're to show
1432 // at the start of the next line.
1433 m_caretPosition
= oldPosition
;
1434 m_caretAtLineStart
= true;
1436 SetDefaultStyleToCursorStyle();
1442 SetDefaultStyleToCursorStyle();
1445 /// Move caret one visual step backward: this may mean setting a flag
1446 /// and keeping the same position if we're going from the end of one line
1447 /// to the start of the next, which may be the exact same caret position.
1448 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1450 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1452 // Only do the check if we're not at the start of the paragraph (where things work OK
1454 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1456 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1460 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1462 // We're at the start of a line. See whether we need to
1463 // stay at the same actual caret position but change visual
1464 // position, or not.
1465 if (oldPosition
== lineRange
.GetStart())
1467 m_caretPosition
= oldPosition
-1;
1468 m_caretAtLineStart
= true;
1471 else if (oldPosition
== lineRange
.GetEnd())
1473 if (m_caretAtLineStart
)
1475 // We're at the start of the line, so keep the same caret position
1476 // but clear the start-of-line flag.
1477 m_caretPosition
= oldPosition
;
1478 m_caretAtLineStart
= false;
1482 // We're showing at the end of the line, so go back
1483 // to the previous character position.
1484 m_caretPosition
= oldPosition
- 1;
1486 SetDefaultStyleToCursorStyle();
1492 SetDefaultStyleToCursorStyle();
1496 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1498 long endPos
= GetBuffer().GetRange().GetEnd();
1500 if (m_caretPosition
+ noPositions
< endPos
)
1502 long oldPos
= m_caretPosition
;
1503 long newPos
= m_caretPosition
+ noPositions
;
1505 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1509 // Determine by looking at oldPos and m_caretPosition whether
1510 // we moved from the end of a line to the start of the next line, in which case
1511 // we want to adjust the caret position such that it is positioned at the
1512 // start of the next line, rather than jumping past the first character of the
1514 if (noPositions
== 1 && !extendSel
)
1515 MoveCaretForward(oldPos
);
1517 SetCaretPosition(newPos
);
1520 SetDefaultStyleToCursorStyle();
1529 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1533 if (m_caretPosition
> startPos
- noPositions
+ 1)
1535 long oldPos
= m_caretPosition
;
1536 long newPos
= m_caretPosition
- noPositions
;
1537 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1541 if (noPositions
== 1 && !extendSel
)
1542 MoveCaretBack(oldPos
);
1544 SetCaretPosition(newPos
);
1547 SetDefaultStyleToCursorStyle();
1556 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1558 return MoveDown(- noLines
, flags
);
1562 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1567 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1568 wxPoint pt
= GetCaret()->GetPosition();
1569 long newLine
= lineNumber
+ noLines
;
1571 if (lineNumber
!= -1)
1575 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1577 if (newLine
> lastLine
)
1587 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1590 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1596 wxClientDC
dc(this);
1598 dc
.SetFont(GetFont());
1600 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1602 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1604 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1605 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1606 // so we view the caret at the start of the line.
1607 bool caretLineStart
= false;
1608 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1610 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1611 wxRichTextRange lineRange
;
1613 lineRange
= thisLine
->GetAbsoluteRange();
1615 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1618 caretLineStart
= true;
1622 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1623 if (para
&& para
->GetRange().GetStart() == newPos
)
1628 long newSelEnd
= newPos
;
1630 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1634 SetCaretPosition(newPos
, caretLineStart
);
1636 SetDefaultStyleToCursorStyle();
1644 /// Move to the end of the paragraph
1645 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1647 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1650 long newPos
= para
->GetRange().GetEnd() - 1;
1651 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1655 SetCaretPosition(newPos
);
1657 SetDefaultStyleToCursorStyle();
1665 /// Move to the start of the paragraph
1666 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1668 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1671 long newPos
= para
->GetRange().GetStart() - 1;
1672 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1676 SetCaretPosition(newPos
);
1678 SetDefaultStyleToCursorStyle();
1686 /// Move to the end of the line
1687 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1689 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1693 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1694 long newPos
= lineRange
.GetEnd();
1695 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1699 SetCaretPosition(newPos
);
1701 SetDefaultStyleToCursorStyle();
1709 /// Move to the start of the line
1710 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1712 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1715 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1716 long newPos
= lineRange
.GetStart()-1;
1718 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1722 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1724 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1726 SetDefaultStyleToCursorStyle();
1734 /// Move to the start of the buffer
1735 bool wxRichTextCtrl::MoveHome(int flags
)
1737 if (m_caretPosition
!= -1)
1739 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1743 SetCaretPosition(-1);
1745 SetDefaultStyleToCursorStyle();
1753 /// Move to the end of the buffer
1754 bool wxRichTextCtrl::MoveEnd(int flags
)
1756 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1758 if (m_caretPosition
!= endPos
)
1760 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1764 SetCaretPosition(endPos
);
1766 SetDefaultStyleToCursorStyle();
1774 /// Move noPages pages up
1775 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1777 return PageDown(- noPages
, flags
);
1780 /// Move noPages pages down
1781 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1783 // Calculate which line occurs noPages * screen height further down.
1784 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1787 wxSize clientSize
= GetClientSize();
1788 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1790 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1793 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1794 long pos
= lineRange
.GetStart()-1;
1795 if (pos
!= m_caretPosition
)
1797 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1799 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1803 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1805 SetDefaultStyleToCursorStyle();
1815 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1817 return str
== wxT(" ") || str
== wxT("\t");
1820 // Finds the caret position for the next word
1821 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1823 long endPos
= GetBuffer().GetRange().GetEnd();
1827 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1829 // First skip current text to space
1830 while (i
< endPos
&& i
> -1)
1832 // i is in character, not caret positions
1833 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1834 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1835 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1839 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1846 while (i
< endPos
&& i
> -1)
1848 // i is in character, not caret positions
1849 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1850 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1851 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1852 return wxMax(-1, i
);
1854 if (text
.empty()) // End of paragraph, or maybe an image
1855 return wxMax(-1, i
- 1);
1856 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1860 // Convert to caret position
1861 return wxMax(-1, i
- 1);
1870 long i
= m_caretPosition
;
1872 // First skip white space
1873 while (i
< endPos
&& i
> -1)
1875 // i is in character, not caret positions
1876 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1877 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1879 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1881 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1886 // Next skip current text to space
1887 while (i
< endPos
&& i
> -1)
1889 // i is in character, not caret positions
1890 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1891 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1892 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1895 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1908 /// Move n words left
1909 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1911 long pos
= FindNextWordPosition(-1);
1912 if (pos
!= m_caretPosition
)
1914 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1916 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1920 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1922 SetDefaultStyleToCursorStyle();
1930 /// Move n words right
1931 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1933 long pos
= FindNextWordPosition(1);
1934 if (pos
!= m_caretPosition
)
1936 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1938 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1942 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1944 SetDefaultStyleToCursorStyle();
1953 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1955 // Only do sizing optimization for large buffers
1956 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1958 m_fullLayoutRequired
= true;
1959 m_fullLayoutTime
= wxGetLocalTimeMillis();
1960 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1961 LayoutContent(true /* onlyVisibleRect */);
1964 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1966 #if wxRICHTEXT_BUFFERED_PAINTING
1974 /// Idle-time processing
1975 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1977 #if wxRICHTEXT_USE_OWN_CARET
1978 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1980 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1986 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1988 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1990 m_fullLayoutRequired
= false;
1991 m_fullLayoutTime
= 0;
1992 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1993 ShowPosition(m_fullLayoutSavedPosition
);
1997 if (m_caretPositionForDefaultStyle
!= -2)
1999 // If the caret position has changed, no longer reflect the default style
2001 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2002 m_caretPositionForDefaultStyle
= -2;
2009 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2011 #if wxRICHTEXT_USE_OWN_CARET
2012 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2015 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2022 /// Set up scrollbars, e.g. after a resize
2023 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2028 if (GetBuffer().IsEmpty())
2030 SetScrollbars(0, 0, 0, 0, 0, 0);
2034 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2035 // of pixels. See e.g. wxVScrolledWindow for ideas.
2036 int pixelsPerUnit
= 5;
2037 wxSize clientSize
= GetClientSize();
2039 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2041 // Round up so we have at least maxHeight pixels
2042 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2044 int startX
= 0, startY
= 0;
2046 GetViewStart(& startX
, & startY
);
2048 int maxPositionX
= 0;
2049 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2051 int newStartX
= wxMin(maxPositionX
, startX
);
2052 int newStartY
= wxMin(maxPositionY
, startY
);
2054 int oldPPUX
, oldPPUY
;
2055 int oldStartX
, oldStartY
;
2056 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2057 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2058 GetViewStart(& oldStartX
, & oldStartY
);
2059 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2061 oldVirtualSizeY
/= oldPPUY
;
2063 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2066 // Don't set scrollbars if there were none before, and there will be none now.
2067 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2070 // Move to previous scroll position if
2072 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2075 /// Paint the background
2076 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2078 wxColour backgroundColour
= GetBackgroundColour();
2079 if (!backgroundColour
.Ok())
2080 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2082 // Clear the background
2083 dc
.SetBrush(wxBrush(backgroundColour
));
2084 dc
.SetPen(*wxTRANSPARENT_PEN
);
2085 wxRect
windowRect(GetClientSize());
2086 windowRect
.x
-= 2; windowRect
.y
-= 2;
2087 windowRect
.width
+= 4; windowRect
.height
+= 4;
2089 // We need to shift the rectangle to take into account
2090 // scrolling. Converting device to logical coordinates.
2091 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2092 dc
.DrawRectangle(windowRect
);
2095 #if wxRICHTEXT_BUFFERED_PAINTING
2096 /// Recreate buffer bitmap if necessary
2097 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2100 if (sz
== wxDefaultSize
)
2101 sz
= GetClientSize();
2103 if (sz
.x
< 1 || sz
.y
< 1)
2106 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2107 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2108 return m_bufferBitmap
.Ok();
2112 // ----------------------------------------------------------------------------
2113 // file IO functions
2114 // ----------------------------------------------------------------------------
2116 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2118 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2120 m_filename
= filename
;
2123 SetInsertionPoint(0);
2126 SetupScrollbars(true);
2128 wxTextCtrl::SendTextUpdatedEvent(this);
2134 wxLogError(_("File couldn't be loaded."));
2140 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2142 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2144 m_filename
= filename
;
2151 wxLogError(_("The text couldn't be saved."));
2156 // ----------------------------------------------------------------------------
2157 // wxRichTextCtrl specific functionality
2158 // ----------------------------------------------------------------------------
2160 /// Add a new paragraph of text to the end of the buffer
2161 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2163 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2169 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2171 wxRichTextRange range
= GetBuffer().AddImage(image
);
2176 // ----------------------------------------------------------------------------
2177 // selection and ranges
2178 // ----------------------------------------------------------------------------
2180 void wxRichTextCtrl::SelectAll()
2182 SetSelection(0, GetLastPosition()+1);
2183 m_selectionAnchor
= -1;
2187 void wxRichTextCtrl::SelectNone()
2189 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2191 wxRichTextRange oldSelection
= m_selectionRange
;
2193 m_selectionRange
= wxRichTextRange(-2, -2);
2195 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2197 m_selectionAnchor
= -2;
2200 static bool wxIsWordDelimiter(const wxString
& text
)
2202 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2205 /// Select the word at the given character position
2206 bool wxRichTextCtrl::SelectWord(long position
)
2208 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2211 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2215 if (position
== para
->GetRange().GetEnd())
2218 long positionStart
= position
;
2219 long positionEnd
= position
;
2221 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2223 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2224 if (wxIsWordDelimiter(text
))
2230 if (positionStart
< para
->GetRange().GetStart())
2231 positionStart
= para
->GetRange().GetStart();
2233 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2235 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2236 if (wxIsWordDelimiter(text
))
2242 if (positionEnd
>= para
->GetRange().GetEnd())
2243 positionEnd
= para
->GetRange().GetEnd();
2245 if (positionEnd
< positionStart
)
2248 SetSelection(positionStart
, positionEnd
+1);
2250 if (positionStart
>= 0)
2252 MoveCaret(positionStart
-1, true);
2253 SetDefaultStyleToCursorStyle();
2259 wxString
wxRichTextCtrl::GetStringSelection() const
2262 GetSelection(&from
, &to
);
2264 return GetRange(from
, to
);
2267 // ----------------------------------------------------------------------------
2269 // ----------------------------------------------------------------------------
2271 wxTextCtrlHitTestResult
2272 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2274 // implement in terms of the other overload as the native ports typically
2275 // can get the position and not (x, y) pair directly (although wxUniv
2276 // directly gets x and y -- and so overrides this method as well)
2278 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2280 if ( rc
!= wxTE_HT_UNKNOWN
)
2282 PositionToXY(pos
, x
, y
);
2288 wxTextCtrlHitTestResult
2289 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2292 wxClientDC
dc((wxRichTextCtrl
*) this);
2293 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2295 // Buffer uses logical position (relative to start of buffer)
2297 wxPoint pt2
= GetLogicalPoint(pt
);
2299 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2301 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2302 return wxTE_HT_BEFORE
;
2303 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2304 return wxTE_HT_BEYOND
;
2305 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2306 return wxTE_HT_ON_TEXT
;
2308 return wxTE_HT_UNKNOWN
;
2311 // ----------------------------------------------------------------------------
2312 // set/get the controls text
2313 // ----------------------------------------------------------------------------
2315 wxString
wxRichTextCtrl::DoGetValue() const
2317 return GetBuffer().GetText();
2320 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2322 // Public API for range is different from internals
2323 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2326 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2328 // Don't call Clear here, since it always sends a text updated event
2329 m_buffer
.ResetAndClearCommands();
2330 m_buffer
.SetDirty(true);
2331 m_caretPosition
= -1;
2332 m_caretPositionForDefaultStyle
= -2;
2333 m_caretAtLineStart
= false;
2334 m_selectionRange
.SetRange(-2, -2);
2344 if (!value
.IsEmpty())
2346 // Remove empty paragraph
2347 GetBuffer().Clear();
2348 DoWriteText(value
, flags
);
2350 // for compatibility, don't move the cursor when doing SetValue()
2351 SetInsertionPoint(0);
2355 // still send an event for consistency
2356 if (flags
& SetValue_SendEvent
)
2357 wxTextCtrl::SendTextUpdatedEvent(this);
2362 void wxRichTextCtrl::WriteText(const wxString
& value
)
2367 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2369 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2371 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2373 if ( flags
& SetValue_SendEvent
)
2374 wxTextCtrl::SendTextUpdatedEvent(this);
2377 void wxRichTextCtrl::AppendText(const wxString
& text
)
2379 SetInsertionPointEnd();
2384 /// Write an image at the current insertion point
2385 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2387 wxRichTextImageBlock imageBlock
;
2389 wxImage image2
= image
;
2390 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2391 return WriteImage(imageBlock
);
2396 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2398 wxRichTextImageBlock imageBlock
;
2401 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2402 return WriteImage(imageBlock
);
2407 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2409 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2412 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2416 wxRichTextImageBlock imageBlock
;
2418 wxImage image
= bitmap
.ConvertToImage();
2419 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2420 return WriteImage(imageBlock
);
2426 /// Insert a newline (actually paragraph) at the current insertion point.
2427 bool wxRichTextCtrl::Newline()
2429 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2432 /// Insert a line break at the current insertion point.
2433 bool wxRichTextCtrl::LineBreak()
2436 text
= wxRichTextLineBreakChar
;
2437 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2440 // ----------------------------------------------------------------------------
2441 // Clipboard operations
2442 // ----------------------------------------------------------------------------
2444 void wxRichTextCtrl::Copy()
2448 wxRichTextRange range
= GetInternalSelectionRange();
2449 GetBuffer().CopyToClipboard(range
);
2453 void wxRichTextCtrl::Cut()
2457 wxRichTextRange range
= GetInternalSelectionRange();
2458 GetBuffer().CopyToClipboard(range
);
2460 DeleteSelectedContent();
2466 void wxRichTextCtrl::Paste()
2470 BeginBatchUndo(_("Paste"));
2472 long newPos
= m_caretPosition
;
2473 DeleteSelectedContent(& newPos
);
2475 GetBuffer().PasteFromClipboard(newPos
);
2481 void wxRichTextCtrl::DeleteSelection()
2483 if (CanDeleteSelection())
2485 DeleteSelectedContent();
2489 bool wxRichTextCtrl::HasSelection() const
2491 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2494 bool wxRichTextCtrl::CanCopy() const
2496 // Can copy if there's a selection
2497 return HasSelection();
2500 bool wxRichTextCtrl::CanCut() const
2502 return HasSelection() && IsEditable();
2505 bool wxRichTextCtrl::CanPaste() const
2507 if ( !IsEditable() )
2510 return GetBuffer().CanPasteFromClipboard();
2513 bool wxRichTextCtrl::CanDeleteSelection() const
2515 return HasSelection() && IsEditable();
2519 // ----------------------------------------------------------------------------
2521 // ----------------------------------------------------------------------------
2523 void wxRichTextCtrl::SetEditable(bool editable
)
2525 m_editable
= editable
;
2528 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2532 m_caretPosition
= pos
- 1;
2537 void wxRichTextCtrl::SetInsertionPointEnd()
2539 long pos
= GetLastPosition();
2540 SetInsertionPoint(pos
);
2543 long wxRichTextCtrl::GetInsertionPoint() const
2545 return m_caretPosition
+1;
2548 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2550 return GetBuffer().GetRange().GetEnd();
2553 // If the return values from and to are the same, there is no
2555 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2557 *from
= m_selectionRange
.GetStart();
2558 *to
= m_selectionRange
.GetEnd();
2559 if ((*to
) != -1 && (*to
) != -2)
2563 bool wxRichTextCtrl::IsEditable() const
2568 // ----------------------------------------------------------------------------
2570 // ----------------------------------------------------------------------------
2572 void wxRichTextCtrl::SetSelection(long from
, long to
)
2574 // if from and to are both -1, it means (in wxWidgets) that all text should
2576 if ( (from
== -1) && (to
== -1) )
2579 to
= GetLastPosition()+1;
2588 wxRichTextRange oldSelection
= m_selectionRange
;
2589 m_selectionAnchor
= from
;
2590 m_selectionRange
.SetRange(from
, to
-1);
2592 m_caretPosition
= from
-1;
2594 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2599 // ----------------------------------------------------------------------------
2601 // ----------------------------------------------------------------------------
2603 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2604 const wxString
& value
)
2606 BeginBatchUndo(_("Replace"));
2608 DeleteSelectedContent();
2610 DoWriteText(value
, SetValue_SelectionOnly
);
2615 void wxRichTextCtrl::Remove(long from
, long to
)
2619 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2626 bool wxRichTextCtrl::IsModified() const
2628 return m_buffer
.IsModified();
2631 void wxRichTextCtrl::MarkDirty()
2633 m_buffer
.Modify(true);
2636 void wxRichTextCtrl::DiscardEdits()
2638 m_caretPositionForDefaultStyle
= -2;
2639 m_buffer
.Modify(false);
2640 m_buffer
.GetCommandProcessor()->ClearCommands();
2643 int wxRichTextCtrl::GetNumberOfLines() const
2645 return GetBuffer().GetParagraphCount();
2648 // ----------------------------------------------------------------------------
2649 // Positions <-> coords
2650 // ----------------------------------------------------------------------------
2652 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2654 return GetBuffer().XYToPosition(x
, y
);
2657 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2659 return GetBuffer().PositionToXY(pos
, x
, y
);
2662 // ----------------------------------------------------------------------------
2664 // ----------------------------------------------------------------------------
2666 void wxRichTextCtrl::ShowPosition(long pos
)
2668 if (!IsPositionVisible(pos
))
2669 ScrollIntoView(pos
-1, WXK_DOWN
);
2672 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2674 return GetBuffer().GetParagraphLength(lineNo
);
2677 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2679 return GetBuffer().GetParagraphText(lineNo
);
2682 // ----------------------------------------------------------------------------
2684 // ----------------------------------------------------------------------------
2686 void wxRichTextCtrl::Undo()
2690 GetCommandProcessor()->Undo();
2694 void wxRichTextCtrl::Redo()
2698 GetCommandProcessor()->Redo();
2702 bool wxRichTextCtrl::CanUndo() const
2704 return GetCommandProcessor()->CanUndo();
2707 bool wxRichTextCtrl::CanRedo() const
2709 return GetCommandProcessor()->CanRedo();
2712 // ----------------------------------------------------------------------------
2713 // implementation details
2714 // ----------------------------------------------------------------------------
2716 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2718 SetValue(event
.GetString());
2719 GetEventHandler()->ProcessEvent(event
);
2722 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2724 // By default, load the first file into the text window.
2725 if (event
.GetNumberOfFiles() > 0)
2727 LoadFile(event
.GetFiles()[0]);
2731 wxSize
wxRichTextCtrl::DoGetBestSize() const
2733 return wxSize(10, 10);
2736 // ----------------------------------------------------------------------------
2737 // standard handlers for standard edit menu events
2738 // ----------------------------------------------------------------------------
2740 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2745 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2750 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2755 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2760 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2765 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2770 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2772 event
.Enable( CanCut() );
2775 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2777 event
.Enable( CanCopy() );
2780 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2782 event
.Enable( CanDeleteSelection() );
2785 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2787 event
.Enable( CanPaste() );
2790 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2792 event
.Enable( CanUndo() );
2793 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2796 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2798 event
.Enable( CanRedo() );
2799 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2802 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2807 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2809 event
.Enable(GetLastPosition() > 0);
2812 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2814 if (event
.GetEventObject() != this)
2822 m_contextMenu
= new wxMenu
;
2823 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2824 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2825 m_contextMenu
->AppendSeparator();
2826 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2827 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2828 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2829 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2830 m_contextMenu
->AppendSeparator();
2831 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2833 PopupMenu(m_contextMenu
);
2837 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2839 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2842 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2844 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2847 // extended style setting operation with flags including:
2848 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2849 // see richtextbuffer.h for more details.
2851 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2853 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2856 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2858 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2861 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2863 return GetBuffer().GetDefaultStyle();
2866 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2868 return GetBuffer().GetStyle(position
, style
);
2871 // get the common set of styles for the range
2872 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2874 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2877 /// Get the content (uncombined) attributes for this position.
2878 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2880 return GetBuffer().GetUncombinedStyle(position
, style
);
2883 /// Set font, and also the buffer attributes
2884 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2886 wxTextCtrlBase::SetFont(font
);
2888 wxTextAttr attr
= GetBuffer().GetAttributes();
2890 GetBuffer().SetBasicStyle(attr
);
2892 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2898 /// Transform logical to physical
2899 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2902 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2907 /// Transform physical to logical
2908 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2911 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2916 /// Position the caret
2917 void wxRichTextCtrl::PositionCaret()
2922 //wxLogDebug(wxT("PositionCaret"));
2925 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2927 wxPoint newPt
= caretRect
.GetPosition();
2928 wxSize newSz
= caretRect
.GetSize();
2929 wxPoint pt
= GetPhysicalPoint(newPt
);
2930 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2933 if (GetCaret()->GetSize() != newSz
)
2934 GetCaret()->SetSize(newSz
);
2936 int halfSize
= newSz
.y
/2;
2937 // If the caret is beyond the margin, hide it by moving it out of the way
2938 if (((pt
.y
+ halfSize
) < GetBuffer().GetTopMargin()) || ((pt
.y
+ halfSize
) > (GetClientSize().y
- GetBuffer().GetBottomMargin())))
2941 GetCaret()->Move(pt
);
2947 /// Get the caret height and position for the given character position
2948 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2950 wxClientDC
dc(this);
2951 dc
.SetFont(GetFont());
2958 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2960 // Caret height can't be zero
2962 height
= dc
.GetCharHeight();
2964 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2971 /// Gets the line for the visible caret position. If the caret is
2972 /// shown at the very end of the line, it means the next character is actually
2973 /// on the following line. So let's get the line we're expecting to find
2974 /// if this is the case.
2975 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2977 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2978 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2981 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2982 if (caretPosition
== lineRange
.GetStart()-1 &&
2983 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2985 if (!m_caretAtLineStart
)
2986 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2993 /// Move the caret to the given character position
2994 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2996 if (GetBuffer().GetDirty())
2999 if (pos
<= GetBuffer().GetRange().GetEnd())
3001 SetCaretPosition(pos
, showAtLineStart
);
3011 /// Layout the buffer: which we must do before certain operations, such as
3012 /// setting the caret position.
3013 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3015 if (GetBuffer().GetDirty() || onlyVisibleRect
)
3017 wxRect
availableSpace(GetClientSize());
3018 if (availableSpace
.width
== 0)
3019 availableSpace
.width
= 10;
3020 if (availableSpace
.height
== 0)
3021 availableSpace
.height
= 10;
3023 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3024 if (onlyVisibleRect
)
3026 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3027 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3030 wxClientDC
dc(this);
3031 dc
.SetFont(GetFont());
3035 GetBuffer().Defragment();
3036 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3037 GetBuffer().Layout(dc
, availableSpace
, flags
);
3038 GetBuffer().SetDirty(false);
3047 /// Is all of the selection bold?
3048 bool wxRichTextCtrl::IsSelectionBold()
3053 wxRichTextRange range
= GetSelectionRange();
3054 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3055 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3057 return HasCharacterAttributes(range
, attr
);
3061 // If no selection, then we need to combine current style with default style
3062 // to see what the effect would be if we started typing.
3064 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3066 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3067 if (GetStyle(pos
, attr
))
3069 if (IsDefaultStyleShowing())
3070 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3071 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3077 /// Is all of the selection italics?
3078 bool wxRichTextCtrl::IsSelectionItalics()
3082 wxRichTextRange range
= GetSelectionRange();
3084 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3085 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3087 return HasCharacterAttributes(range
, attr
);
3091 // If no selection, then we need to combine current style with default style
3092 // to see what the effect would be if we started typing.
3094 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3096 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3097 if (GetStyle(pos
, attr
))
3099 if (IsDefaultStyleShowing())
3100 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3101 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3107 /// Is all of the selection underlined?
3108 bool wxRichTextCtrl::IsSelectionUnderlined()
3112 wxRichTextRange range
= GetSelectionRange();
3114 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3115 attr
.SetFontUnderlined(true);
3117 return HasCharacterAttributes(range
, attr
);
3121 // If no selection, then we need to combine current style with default style
3122 // to see what the effect would be if we started typing.
3124 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3125 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3127 if (GetStyle(pos
, attr
))
3129 if (IsDefaultStyleShowing())
3130 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3131 return attr
.GetFontUnderlined();
3137 /// Apply bold to the selection
3138 bool wxRichTextCtrl::ApplyBoldToSelection()
3141 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3142 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3145 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3148 wxRichTextAttr current
= GetDefaultStyleEx();
3149 current
.Apply(attr
);
3150 SetAndShowDefaultStyle(current
);
3155 /// Apply italic to the selection
3156 bool wxRichTextCtrl::ApplyItalicToSelection()
3159 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3160 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3163 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3166 wxRichTextAttr current
= GetDefaultStyleEx();
3167 current
.Apply(attr
);
3168 SetAndShowDefaultStyle(current
);
3173 /// Apply underline to the selection
3174 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3177 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3178 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3181 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3184 wxRichTextAttr current
= GetDefaultStyleEx();
3185 current
.Apply(attr
);
3186 SetAndShowDefaultStyle(current
);
3191 /// Is all of the selection aligned according to the specified flag?
3192 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3194 wxRichTextRange range
;
3196 range
= GetSelectionRange();
3198 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3201 attr
.SetAlignment(alignment
);
3203 return HasParagraphAttributes(range
, attr
);
3206 /// Apply alignment to the selection
3207 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3210 attr
.SetAlignment(alignment
);
3212 return SetStyle(GetSelectionRange(), attr
);
3215 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3217 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3222 /// Apply a named style to the selection
3223 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3225 // Flags are defined within each definition, so only certain
3226 // attributes are applied.
3227 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3229 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3231 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3233 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3235 wxRichTextRange range
;
3238 range
= GetSelectionRange();
3241 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3242 range
= wxRichTextRange(pos
, pos
+1);
3245 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3248 // Make sure the attr has the style name
3249 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3251 attr
.SetParagraphStyleName(def
->GetName());
3253 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3254 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3255 // to change its style independently.
3256 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3259 attr
.SetCharacterStyleName(def
->GetName());
3262 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3265 wxRichTextAttr current
= GetDefaultStyleEx();
3266 current
.Apply(attr
);
3267 SetAndShowDefaultStyle(current
);
3272 /// Apply the style sheet to the buffer, for example if the styles have changed.
3273 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3276 styleSheet
= GetBuffer().GetStyleSheet();
3280 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3282 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3290 /// Sets the default style to the style under the cursor
3291 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3294 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3296 // If at the start of a paragraph, use the next position.
3297 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3299 if (GetUncombinedStyle(pos
, attr
))
3301 SetDefaultStyle(attr
);
3308 /// Returns the first visible position in the current view
3309 long wxRichTextCtrl::GetFirstVisiblePosition() const
3311 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3313 return line
->GetAbsoluteRange().GetStart();
3318 /// Get the first visible point in the window
3319 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3322 int startXUnits
, startYUnits
;
3324 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3325 GetViewStart(& startXUnits
, & startYUnits
);
3327 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3330 /// The adjusted caret position is the character position adjusted to take
3331 /// into account whether we're at the start of a paragraph, in which case
3332 /// style information should be taken from the next position, not current one.
3333 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3335 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3337 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3342 /// Get/set the selection range in character positions. -1, -1 means no selection.
3343 /// The range is in API convention, i.e. a single character selection is denoted
3345 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3347 wxRichTextRange range
= GetInternalSelectionRange();
3348 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3349 range
.SetEnd(range
.GetEnd() + 1);
3353 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3355 wxRichTextRange
range1(range
);
3356 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3357 range1
.SetEnd(range1
.GetEnd() - 1);
3359 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3361 SetInternalSelectionRange(range1
);
3365 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3367 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3370 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3372 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3375 /// Clear list for given range
3376 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3378 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3381 /// Number/renumber any list elements in the given range
3382 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3384 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3387 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3389 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3392 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3393 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3395 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3398 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3400 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3403 /// Deletes the content in the given range
3404 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3406 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3409 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3411 if (sm_availableFontNames
.GetCount() == 0)
3413 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3414 sm_availableFontNames
.Sort();
3416 return sm_availableFontNames
;
3419 void wxRichTextCtrl::ClearAvailableFontNames()
3421 sm_availableFontNames
.Clear();
3424 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3426 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3428 wxTextAttrEx basicStyle
= GetBasicStyle();
3429 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3430 SetBasicStyle(basicStyle
);
3431 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3436 // Refresh the area affected by a selection change
3437 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3439 // Calculate the refresh rectangle - just the affected lines
3440 long firstPos
, lastPos
;
3441 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3443 firstPos
= newSelection
.GetStart();
3444 lastPos
= newSelection
.GetEnd();
3446 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3448 firstPos
= oldSelection
.GetStart();
3449 lastPos
= oldSelection
.GetEnd();
3451 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3457 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3458 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3461 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3462 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3464 if (firstLine
&& lastLine
)
3466 wxSize clientSize
= GetClientSize();
3467 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3468 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3471 pt1
.y
= wxMax(0, pt1
.y
);
3473 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3475 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3476 RefreshRect(rect
, false);
3484 #if wxRICHTEXT_USE_OWN_CARET
3486 // ----------------------------------------------------------------------------
3487 // initialization and destruction
3488 // ----------------------------------------------------------------------------
3490 void wxRichTextCaret::Init()
3496 m_richTextCtrl
= NULL
;
3497 m_needsUpdate
= false;
3501 wxRichTextCaret::~wxRichTextCaret()
3503 if (m_timer
.IsRunning())
3507 // ----------------------------------------------------------------------------
3508 // showing/hiding/moving the caret (base class interface)
3509 // ----------------------------------------------------------------------------
3511 void wxRichTextCaret::DoShow()
3515 if (!m_timer
.IsRunning())
3516 m_timer
.Start(GetBlinkTime());
3521 void wxRichTextCaret::DoHide()
3523 if (m_timer
.IsRunning())
3529 void wxRichTextCaret::DoMove()
3535 if (m_xOld
!= -1 && m_yOld
!= -1)
3539 wxRect
rect(GetPosition(), GetSize());
3540 m_richTextCtrl
->RefreshRect(rect
, false);
3549 void wxRichTextCaret::DoSize()
3551 int countVisible
= m_countVisible
;
3552 if (countVisible
> 0)
3558 if (countVisible
> 0)
3560 m_countVisible
= countVisible
;
3565 // ----------------------------------------------------------------------------
3566 // handling the focus
3567 // ----------------------------------------------------------------------------
3569 void wxRichTextCaret::OnSetFocus()
3577 void wxRichTextCaret::OnKillFocus()
3582 // ----------------------------------------------------------------------------
3583 // drawing the caret
3584 // ----------------------------------------------------------------------------
3586 void wxRichTextCaret::Refresh()
3590 wxRect
rect(GetPosition(), GetSize());
3591 m_richTextCtrl
->RefreshRect(rect
, false);
3595 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3597 dc
->SetPen( *wxBLACK_PEN
);
3599 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3600 dc
->SetPen(*wxBLACK_PEN
);
3602 wxPoint
pt(m_x
, m_y
);
3606 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3608 if (IsVisible() && m_flashOn
)
3609 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3612 void wxRichTextCaret::Notify()
3614 m_flashOn
= !m_flashOn
;
3618 void wxRichTextCaretTimer::Notify()
3623 // wxRICHTEXT_USE_OWN_CARET