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
);
292 m_contextMenu
= new wxMenu
;
293 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
294 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
295 m_contextMenu
->AppendSeparator();
296 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
297 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
298 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
299 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
300 m_contextMenu
->AppendSeparator();
301 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
306 wxRichTextCtrl::~wxRichTextCtrl()
308 GetBuffer().RemoveEventHandler(this);
310 delete m_contextMenu
;
313 /// Member initialisation
314 void wxRichTextCtrl::Init()
316 m_contextMenu
= NULL
;
318 m_caretPosition
= -1;
319 m_selectionRange
.SetRange(-2, -2);
320 m_selectionAnchor
= -2;
322 m_caretAtLineStart
= false;
324 m_fullLayoutRequired
= false;
325 m_fullLayoutTime
= 0;
326 m_fullLayoutSavedPosition
= 0;
327 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
328 m_caretPositionForDefaultStyle
= -2;
331 void wxRichTextCtrl::DoThaw()
333 if (GetBuffer().GetDirty())
342 void wxRichTextCtrl::Clear()
344 m_buffer
.ResetAndClearCommands();
345 m_buffer
.SetDirty(true);
346 m_caretPosition
= -1;
347 m_caretPositionForDefaultStyle
= -2;
348 m_caretAtLineStart
= false;
349 m_selectionRange
.SetRange(-2, -2);
359 wxTextCtrl::SendTextUpdatedEvent(this);
363 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
365 #if !wxRICHTEXT_USE_OWN_CARET
366 if (GetCaret() && !IsFrozen())
371 #if wxRICHTEXT_BUFFERED_PAINTING
372 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
382 dc
.SetFont(GetFont());
384 // Paint the background
387 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
389 wxRect
drawingArea(GetUpdateRegion().GetBox());
390 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
392 wxRect
availableSpace(GetClientSize());
393 if (GetBuffer().GetDirty())
395 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
396 GetBuffer().SetDirty(false);
400 wxRect
clipRect(availableSpace
);
401 clipRect
.x
+= GetBuffer().GetLeftMargin();
402 clipRect
.y
+= GetBuffer().GetTopMargin();
403 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
404 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
405 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
406 dc
.SetClippingRegion(clipRect
);
408 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
410 dc
.DestroyClippingRegion();
412 // Other user defined painting after everything else (i.e. all text) is painted
413 PaintAboveContent(dc
);
415 #if wxRICHTEXT_USE_OWN_CARET
416 if (GetCaret()->IsVisible())
418 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
423 #if !wxRICHTEXT_USE_OWN_CARET
430 // Empty implementation, to prevent flicker
431 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
435 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
439 #if !wxRICHTEXT_USE_OWN_CARET
445 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
446 // Work around dropouts when control is focused
454 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
459 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
460 // Work around dropouts when control is focused
468 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
474 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
480 dc
.SetFont(GetFont());
483 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
485 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
487 m_dragStart
= event
.GetLogicalPosition(dc
);
491 bool caretAtLineStart
= false;
493 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
495 // If we're at the start of a line (but not first in para)
496 // then we should keep the caret showing at the start of the line
497 // by showing the m_caretAtLineStart flag.
498 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
499 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
501 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
502 caretAtLineStart
= true;
506 long oldCaretPos
= m_caretPosition
;
508 MoveCaret(position
, caretAtLineStart
);
509 SetDefaultStyleToCursorStyle();
511 if (event
.ShiftDown())
513 if (m_selectionRange
.GetStart() == -2)
514 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
516 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
526 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
531 if (GetCapture() == this)
534 // See if we clicked on a URL
537 dc
.SetFont(GetFont());
540 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
541 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
543 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
545 wxRichTextEvent
cmdEvent(
546 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
548 cmdEvent
.SetEventObject(this);
549 cmdEvent
.SetPosition(m_caretPosition
+1);
551 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
554 if (GetStyle(position
, attr
))
556 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
558 wxString urlTarget
= attr
.GetURL();
559 if (!urlTarget
.IsEmpty())
561 wxMouseEvent
mouseEvent(event
);
563 long startPos
= 0, endPos
= 0;
564 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
567 startPos
= obj
->GetRange().GetStart();
568 endPos
= obj
->GetRange().GetEnd();
571 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
572 InitCommandEvent(urlEvent
);
574 urlEvent
.SetString(urlTarget
);
576 GetEventHandler()->ProcessEvent(urlEvent
);
586 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
590 dc
.SetFont(GetFont());
593 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
594 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
596 // See if we need to change the cursor
599 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
602 if (GetStyle(position
, attr
))
604 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
606 SetCursor(m_urlCursor
);
608 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
610 SetCursor(m_textCursor
);
615 SetCursor(m_textCursor
);
618 if (!event
.Dragging())
624 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
626 // TODO: test closeness
628 bool caretAtLineStart
= false;
630 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
632 // If we're at the start of a line (but not first in para)
633 // then we should keep the caret showing at the start of the line
634 // by showing the m_caretAtLineStart flag.
635 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
636 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
638 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
639 caretAtLineStart
= true;
643 if (m_caretPosition
!= position
)
645 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
647 MoveCaret(position
, caretAtLineStart
);
648 SetDefaultStyleToCursorStyle();
654 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
658 wxRichTextEvent
cmdEvent(
659 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
661 cmdEvent
.SetEventObject(this);
662 cmdEvent
.SetPosition(m_caretPosition
+1);
664 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
668 /// Left-double-click
669 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
671 wxRichTextEvent
cmdEvent(
672 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
674 cmdEvent
.SetEventObject(this);
675 cmdEvent
.SetPosition(m_caretPosition
+1);
677 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
679 SelectWord(GetCaretPosition()+1);
684 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
686 wxRichTextEvent
cmdEvent(
687 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
689 cmdEvent
.SetEventObject(this);
690 cmdEvent
.SetPosition(m_caretPosition
+1);
692 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
697 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
701 flags
|= wxRICHTEXT_CTRL_DOWN
;
702 if (event
.ShiftDown())
703 flags
|= wxRICHTEXT_SHIFT_DOWN
;
705 flags
|= wxRICHTEXT_ALT_DOWN
;
707 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
709 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
711 KeyboardNavigate(event
.GetKeyCode(), flags
);
715 long keycode
= event
.GetKeyCode();
775 case WXK_NUMPAD_HOME
:
776 case WXK_NUMPAD_LEFT
:
778 case WXK_NUMPAD_RIGHT
:
779 case WXK_NUMPAD_DOWN
:
780 case WXK_NUMPAD_PAGEUP
:
781 case WXK_NUMPAD_PAGEDOWN
:
783 case WXK_NUMPAD_BEGIN
:
784 case WXK_NUMPAD_INSERT
:
785 case WXK_WINDOWS_LEFT
:
794 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
795 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
797 BeginBatchUndo(_("Delete Text"));
799 long newPos
= m_caretPosition
;
801 bool processed
= DeleteSelectedContent(& newPos
);
803 // Submit range in character positions, which are greater than caret positions,
804 // so subtract 1 for deleted character and add 1 for conversion to character position.
809 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
812 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
818 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
823 if (GetLastPosition() == -1)
827 m_caretPosition
= -1;
829 SetDefaultStyleToCursorStyle();
832 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
834 wxRichTextEvent
cmdEvent(
835 wxEVT_COMMAND_RICHTEXT_DELETE
,
837 cmdEvent
.SetEventObject(this);
838 cmdEvent
.SetFlags(flags
);
839 cmdEvent
.SetPosition(m_caretPosition
+1);
840 GetEventHandler()->ProcessEvent(cmdEvent
);
850 // all the other keys modify the controls contents which shouldn't be
851 // possible if we're read-only
858 if (event
.GetKeyCode() == WXK_RETURN
)
860 BeginBatchUndo(_("Insert Text"));
862 long newPos
= m_caretPosition
;
864 DeleteSelectedContent(& newPos
);
866 if (event
.ShiftDown())
869 text
= wxRichTextLineBreakChar
;
870 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
871 m_caretAtLineStart
= true;
875 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
878 SetDefaultStyleToCursorStyle();
880 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
882 wxRichTextEvent
cmdEvent(
883 wxEVT_COMMAND_RICHTEXT_RETURN
,
885 cmdEvent
.SetEventObject(this);
886 cmdEvent
.SetFlags(flags
);
887 cmdEvent
.SetPosition(newPos
+1);
889 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
891 // Generate conventional event
892 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
893 InitCommandEvent(textEvent
);
895 GetEventHandler()->ProcessEvent(textEvent
);
899 else if (event
.GetKeyCode() == WXK_BACK
)
901 BeginBatchUndo(_("Delete Text"));
903 long newPos
= m_caretPosition
;
905 bool processed
= DeleteSelectedContent(& newPos
);
907 // Submit range in character positions, which are greater than caret positions,
908 // so subtract 1 for deleted character and add 1 for conversion to character position.
913 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
916 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
922 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
927 if (GetLastPosition() == -1)
931 m_caretPosition
= -1;
933 SetDefaultStyleToCursorStyle();
936 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
938 wxRichTextEvent
cmdEvent(
939 wxEVT_COMMAND_RICHTEXT_DELETE
,
941 cmdEvent
.SetEventObject(this);
942 cmdEvent
.SetFlags(flags
);
943 cmdEvent
.SetPosition(m_caretPosition
+1);
944 GetEventHandler()->ProcessEvent(cmdEvent
);
948 else if (event
.GetKeyCode() == WXK_DELETE
)
950 BeginBatchUndo(_("Delete Text"));
952 long newPos
= m_caretPosition
;
954 bool processed
= DeleteSelectedContent(& newPos
);
956 // Submit range in character positions, which are greater than caret positions,
957 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
961 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
962 if (pos
!= -1 && (pos
> newPos
))
964 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
969 if (!processed
&& newPos
< (GetLastPosition()-1))
970 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
975 if (GetLastPosition() == -1)
979 m_caretPosition
= -1;
981 SetDefaultStyleToCursorStyle();
984 wxRichTextEvent
cmdEvent(
985 wxEVT_COMMAND_RICHTEXT_DELETE
,
987 cmdEvent
.SetEventObject(this);
988 cmdEvent
.SetFlags(flags
);
989 cmdEvent
.SetPosition(m_caretPosition
+1);
990 GetEventHandler()->ProcessEvent(cmdEvent
);
996 long keycode
= event
.GetKeyCode();
1008 if (event
.CmdDown())
1010 // Fixes AltGr+key with European input languages on Windows
1011 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1018 wxRichTextEvent
cmdEvent(
1019 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1021 cmdEvent
.SetEventObject(this);
1022 cmdEvent
.SetFlags(flags
);
1024 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1026 cmdEvent
.SetCharacter((wxChar
) keycode
);
1028 cmdEvent
.SetPosition(m_caretPosition
+1);
1030 if (keycode
== wxT('\t'))
1032 // See if we need to promote or demote the selection or paragraph at the cursor
1033 // position, instead of inserting a tab.
1034 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1035 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1036 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1038 wxRichTextRange range
;
1040 range
= GetSelectionRange();
1042 range
= para
->GetRange().FromInternal();
1044 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1046 PromoteList(promoteBy
, range
, NULL
);
1048 GetEventHandler()->ProcessEvent(cmdEvent
);
1054 BeginBatchUndo(_("Insert Text"));
1056 long newPos
= m_caretPosition
;
1057 DeleteSelectedContent(& newPos
);
1060 wxString str
= event
.GetUnicodeKey();
1062 wxString str
= (wxChar
) event
.GetKeyCode();
1064 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1068 SetDefaultStyleToCursorStyle();
1069 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1071 GetEventHandler()->ProcessEvent(cmdEvent
);
1079 /// Delete content if there is a selection, e.g. when pressing a key.
1080 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1084 long pos
= m_selectionRange
.GetStart();
1085 wxRichTextRange range
= m_selectionRange
;
1087 // SelectAll causes more to be selected than doing it interactively,
1088 // and causes a new paragraph to be inserted. So for multiline buffers,
1089 // don't delete the final position.
1090 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1091 range
.SetEnd(range
.GetEnd()-1);
1093 GetBuffer().DeleteRangeWithUndo(range
, this);
1094 m_selectionRange
.SetRange(-2, -2);
1104 /// Keyboard navigation
1108 Left: left one character
1109 Right: right one character
1112 Ctrl-Left: left one word
1113 Ctrl-Right: right one word
1114 Ctrl-Up: previous paragraph start
1115 Ctrl-Down: next start of paragraph
1118 Ctrl-Home: start of document
1119 Ctrl-End: end of document
1120 Page-Up: Up a screen
1121 Page-Down: Down a screen
1125 Ctrl-Alt-PgUp: Start of window
1126 Ctrl-Alt-PgDn: End of window
1127 F8: Start selection mode
1128 Esc: End selection mode
1130 Adding Shift does the above but starts/extends selection.
1135 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1137 bool success
= false;
1139 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1141 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1142 success
= WordRight(1, flags
);
1144 success
= MoveRight(1, flags
);
1146 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1148 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1149 success
= WordLeft(1, flags
);
1151 success
= MoveLeft(1, flags
);
1153 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1155 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1156 success
= MoveToParagraphStart(flags
);
1158 success
= MoveUp(1, flags
);
1160 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1162 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1163 success
= MoveToParagraphEnd(flags
);
1165 success
= MoveDown(1, flags
);
1167 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1169 success
= PageUp(1, flags
);
1171 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1173 success
= PageDown(1, flags
);
1175 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1177 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1178 success
= MoveHome(flags
);
1180 success
= MoveToLineStart(flags
);
1182 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1184 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1185 success
= MoveEnd(flags
);
1187 success
= MoveToLineEnd(flags
);
1192 ScrollIntoView(m_caretPosition
, keyCode
);
1193 SetDefaultStyleToCursorStyle();
1199 /// Extend the selection. Selections are in caret positions.
1200 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1202 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1204 if (oldPos
== newPos
)
1207 wxRichTextRange oldSelection
= m_selectionRange
;
1209 // If not currently selecting, start selecting
1210 if (m_selectionRange
.GetStart() == -2)
1212 m_selectionAnchor
= oldPos
;
1214 if (oldPos
> newPos
)
1215 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1217 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1221 // Always ensure that the selection range start is greater than
1223 if (newPos
> m_selectionAnchor
)
1224 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1225 else if (newPos
== m_selectionAnchor
)
1226 m_selectionRange
= wxRichTextRange(-2, -2);
1228 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1231 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1233 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1235 wxLogDebug(wxT("Strange selection range"));
1244 /// Scroll into view, returning true if we scrolled.
1245 /// This takes a _caret_ position.
1246 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1248 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1254 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1256 int startXUnits
, startYUnits
;
1257 GetViewStart(& startXUnits
, & startYUnits
);
1258 int startY
= startYUnits
* ppuY
;
1261 GetVirtualSize(& sx
, & sy
);
1267 wxRect rect
= line
->GetRect();
1269 bool scrolled
= false;
1271 wxSize clientSize
= GetClientSize();
1272 clientSize
.y
-= GetBuffer().GetBottomMargin();
1274 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1276 int y
= rect
.y
- GetClientSize().y
/2;
1277 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1278 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1280 if (startYUnits
!= yUnits
)
1282 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1285 #if !wxRICHTEXT_USE_OWN_CARET
1295 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1296 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1297 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1298 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1300 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1302 // Make it scroll so this item is at the bottom
1304 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1305 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1307 // If we're still off the screen, scroll another line down
1308 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1311 if (startYUnits
!= yUnits
)
1313 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1317 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1319 // Make it scroll so this item is at the top
1321 int y
= rect
.y
- GetBuffer().GetTopMargin();
1322 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1324 if (startYUnits
!= yUnits
)
1326 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1332 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1333 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1334 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1335 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1337 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1339 // Make it scroll so this item is at the top
1341 int y
= rect
.y
- GetBuffer().GetTopMargin();
1342 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1344 if (startYUnits
!= yUnits
)
1346 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1350 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1352 // Make it scroll so this item is at the bottom
1354 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1355 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1357 // If we're still off the screen, scroll another line down
1358 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1361 if (startYUnits
!= yUnits
)
1363 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1369 #if !wxRICHTEXT_USE_OWN_CARET
1377 /// Is the given position visible on the screen?
1378 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1380 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1386 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1389 GetViewStart(& startX
, & startY
);
1391 startY
= startY
* ppuY
;
1393 wxRect rect
= line
->GetRect();
1394 wxSize clientSize
= GetClientSize();
1395 clientSize
.y
-= GetBuffer().GetBottomMargin();
1397 return (rect
.GetBottom() > (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1400 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1402 m_caretPosition
= position
;
1403 m_caretAtLineStart
= showAtLineStart
;
1406 /// Move caret one visual step forward: this may mean setting a flag
1407 /// and keeping the same position if we're going from the end of one line
1408 /// to the start of the next, which may be the exact same caret position.
1409 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1411 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1413 // Only do the check if we're not at the end of the paragraph (where things work OK
1415 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1417 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1421 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1423 // We're at the end of a line. See whether we need to
1424 // stay at the same actual caret position but change visual
1425 // position, or not.
1426 if (oldPosition
== lineRange
.GetEnd())
1428 if (m_caretAtLineStart
)
1430 // We're already at the start of the line, so actually move on now.
1431 m_caretPosition
= oldPosition
+ 1;
1432 m_caretAtLineStart
= false;
1436 // We're showing at the end of the line, so keep to
1437 // the same position but indicate that we're to show
1438 // at the start of the next line.
1439 m_caretPosition
= oldPosition
;
1440 m_caretAtLineStart
= true;
1442 SetDefaultStyleToCursorStyle();
1448 SetDefaultStyleToCursorStyle();
1451 /// Move caret one visual step backward: this may mean setting a flag
1452 /// and keeping the same position if we're going from the end of one line
1453 /// to the start of the next, which may be the exact same caret position.
1454 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1456 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1458 // Only do the check if we're not at the start of the paragraph (where things work OK
1460 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1462 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1466 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1468 // We're at the start of a line. See whether we need to
1469 // stay at the same actual caret position but change visual
1470 // position, or not.
1471 if (oldPosition
== lineRange
.GetStart())
1473 m_caretPosition
= oldPosition
-1;
1474 m_caretAtLineStart
= true;
1477 else if (oldPosition
== lineRange
.GetEnd())
1479 if (m_caretAtLineStart
)
1481 // We're at the start of the line, so keep the same caret position
1482 // but clear the start-of-line flag.
1483 m_caretPosition
= oldPosition
;
1484 m_caretAtLineStart
= false;
1488 // We're showing at the end of the line, so go back
1489 // to the previous character position.
1490 m_caretPosition
= oldPosition
- 1;
1492 SetDefaultStyleToCursorStyle();
1498 SetDefaultStyleToCursorStyle();
1502 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1504 long endPos
= GetBuffer().GetRange().GetEnd();
1506 if (m_caretPosition
+ noPositions
< endPos
)
1508 long oldPos
= m_caretPosition
;
1509 long newPos
= m_caretPosition
+ noPositions
;
1511 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1515 // Determine by looking at oldPos and m_caretPosition whether
1516 // we moved from the end of a line to the start of the next line, in which case
1517 // we want to adjust the caret position such that it is positioned at the
1518 // start of the next line, rather than jumping past the first character of the
1520 if (noPositions
== 1 && !extendSel
)
1521 MoveCaretForward(oldPos
);
1523 SetCaretPosition(newPos
);
1526 SetDefaultStyleToCursorStyle();
1535 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1539 if (m_caretPosition
> startPos
- noPositions
+ 1)
1541 long oldPos
= m_caretPosition
;
1542 long newPos
= m_caretPosition
- noPositions
;
1543 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1547 if (noPositions
== 1 && !extendSel
)
1548 MoveCaretBack(oldPos
);
1550 SetCaretPosition(newPos
);
1553 SetDefaultStyleToCursorStyle();
1562 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1564 return MoveDown(- noLines
, flags
);
1568 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1573 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1574 wxPoint pt
= GetCaret()->GetPosition();
1575 long newLine
= lineNumber
+ noLines
;
1577 if (lineNumber
!= -1)
1581 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1583 if (newLine
> lastLine
)
1593 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1596 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1602 wxClientDC
dc(this);
1604 dc
.SetFont(GetFont());
1606 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1608 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1610 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1611 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1612 // so we view the caret at the start of the line.
1613 bool caretLineStart
= false;
1614 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1616 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1617 wxRichTextRange lineRange
;
1619 lineRange
= thisLine
->GetAbsoluteRange();
1621 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1624 caretLineStart
= true;
1628 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1629 if (para
&& para
->GetRange().GetStart() == newPos
)
1634 long newSelEnd
= newPos
;
1636 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1640 SetCaretPosition(newPos
, caretLineStart
);
1642 SetDefaultStyleToCursorStyle();
1650 /// Move to the end of the paragraph
1651 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1653 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1656 long newPos
= para
->GetRange().GetEnd() - 1;
1657 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1661 SetCaretPosition(newPos
);
1663 SetDefaultStyleToCursorStyle();
1671 /// Move to the start of the paragraph
1672 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1674 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1677 long newPos
= para
->GetRange().GetStart() - 1;
1678 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1682 SetCaretPosition(newPos
);
1684 SetDefaultStyleToCursorStyle();
1692 /// Move to the end of the line
1693 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1695 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1699 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1700 long newPos
= lineRange
.GetEnd();
1701 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1705 SetCaretPosition(newPos
);
1707 SetDefaultStyleToCursorStyle();
1715 /// Move to the start of the line
1716 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1718 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1721 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1722 long newPos
= lineRange
.GetStart()-1;
1724 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1728 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1730 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1732 SetDefaultStyleToCursorStyle();
1740 /// Move to the start of the buffer
1741 bool wxRichTextCtrl::MoveHome(int flags
)
1743 if (m_caretPosition
!= -1)
1745 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1749 SetCaretPosition(-1);
1751 SetDefaultStyleToCursorStyle();
1759 /// Move to the end of the buffer
1760 bool wxRichTextCtrl::MoveEnd(int flags
)
1762 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1764 if (m_caretPosition
!= endPos
)
1766 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1770 SetCaretPosition(endPos
);
1772 SetDefaultStyleToCursorStyle();
1780 /// Move noPages pages up
1781 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1783 return PageDown(- noPages
, flags
);
1786 /// Move noPages pages down
1787 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1789 // Calculate which line occurs noPages * screen height further down.
1790 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1793 wxSize clientSize
= GetClientSize();
1794 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1796 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1799 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1800 long pos
= lineRange
.GetStart()-1;
1801 if (pos
!= m_caretPosition
)
1803 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1805 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1809 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1811 SetDefaultStyleToCursorStyle();
1821 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1823 return str
== wxT(" ") || str
== wxT("\t");
1826 // Finds the caret position for the next word
1827 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1829 long endPos
= GetBuffer().GetRange().GetEnd();
1833 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1835 // First skip current text to space
1836 while (i
< endPos
&& i
> -1)
1838 // i is in character, not caret positions
1839 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1840 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1841 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1845 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1852 while (i
< endPos
&& i
> -1)
1854 // i is in character, not caret positions
1855 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1856 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1857 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1858 return wxMax(-1, i
);
1860 if (text
.empty()) // End of paragraph, or maybe an image
1861 return wxMax(-1, i
- 1);
1862 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1866 // Convert to caret position
1867 return wxMax(-1, i
- 1);
1876 long i
= m_caretPosition
;
1878 // First skip white space
1879 while (i
< endPos
&& i
> -1)
1881 // i is in character, not caret positions
1882 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1883 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1885 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1887 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1892 // Next skip current text to space
1893 while (i
< endPos
&& i
> -1)
1895 // i is in character, not caret positions
1896 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1897 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1898 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1901 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1914 /// Move n words left
1915 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1917 long pos
= FindNextWordPosition(-1);
1918 if (pos
!= m_caretPosition
)
1920 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1922 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1926 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1928 SetDefaultStyleToCursorStyle();
1936 /// Move n words right
1937 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1939 long pos
= FindNextWordPosition(1);
1940 if (pos
!= m_caretPosition
)
1942 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1944 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1948 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1950 SetDefaultStyleToCursorStyle();
1959 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1961 // Only do sizing optimization for large buffers
1962 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1964 m_fullLayoutRequired
= true;
1965 m_fullLayoutTime
= wxGetLocalTimeMillis();
1966 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1967 LayoutContent(true /* onlyVisibleRect */);
1970 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1972 #if wxRICHTEXT_BUFFERED_PAINTING
1980 /// Idle-time processing
1981 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1983 #if wxRICHTEXT_USE_OWN_CARET
1984 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1986 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1992 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1994 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1996 m_fullLayoutRequired
= false;
1997 m_fullLayoutTime
= 0;
1998 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1999 ShowPosition(m_fullLayoutSavedPosition
);
2003 if (m_caretPositionForDefaultStyle
!= -2)
2005 // If the caret position has changed, no longer reflect the default style
2007 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2008 m_caretPositionForDefaultStyle
= -2;
2015 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2017 #if wxRICHTEXT_USE_OWN_CARET
2018 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2021 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2028 /// Set up scrollbars, e.g. after a resize
2029 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2034 if (GetBuffer().IsEmpty())
2036 SetScrollbars(0, 0, 0, 0, 0, 0);
2040 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2041 // of pixels. See e.g. wxVScrolledWindow for ideas.
2042 int pixelsPerUnit
= 5;
2043 wxSize clientSize
= GetClientSize();
2045 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2047 // Round up so we have at least maxHeight pixels
2048 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2050 int startX
= 0, startY
= 0;
2052 GetViewStart(& startX
, & startY
);
2054 int maxPositionX
= 0;
2055 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2057 int newStartX
= wxMin(maxPositionX
, startX
);
2058 int newStartY
= wxMin(maxPositionY
, startY
);
2060 int oldPPUX
, oldPPUY
;
2061 int oldStartX
, oldStartY
;
2062 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2063 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2064 GetViewStart(& oldStartX
, & oldStartY
);
2065 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2067 oldVirtualSizeY
/= oldPPUY
;
2069 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2072 // Don't set scrollbars if there were none before, and there will be none now.
2073 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2076 // Move to previous scroll position if
2078 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2081 /// Paint the background
2082 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2084 wxColour backgroundColour
= GetBackgroundColour();
2085 if (!backgroundColour
.Ok())
2086 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2088 // Clear the background
2089 dc
.SetBrush(wxBrush(backgroundColour
));
2090 dc
.SetPen(*wxTRANSPARENT_PEN
);
2091 wxRect
windowRect(GetClientSize());
2092 windowRect
.x
-= 2; windowRect
.y
-= 2;
2093 windowRect
.width
+= 4; windowRect
.height
+= 4;
2095 // We need to shift the rectangle to take into account
2096 // scrolling. Converting device to logical coordinates.
2097 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2098 dc
.DrawRectangle(windowRect
);
2101 #if wxRICHTEXT_BUFFERED_PAINTING
2102 /// Recreate buffer bitmap if necessary
2103 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2106 if (sz
== wxDefaultSize
)
2107 sz
= GetClientSize();
2109 if (sz
.x
< 1 || sz
.y
< 1)
2112 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2113 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2114 return m_bufferBitmap
.Ok();
2118 // ----------------------------------------------------------------------------
2119 // file IO functions
2120 // ----------------------------------------------------------------------------
2122 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2124 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2126 m_filename
= filename
;
2129 SetInsertionPoint(0);
2132 SetupScrollbars(true);
2134 wxTextCtrl::SendTextUpdatedEvent(this);
2140 wxLogError(_("File couldn't be loaded."));
2146 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2148 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2150 m_filename
= filename
;
2157 wxLogError(_("The text couldn't be saved."));
2162 // ----------------------------------------------------------------------------
2163 // wxRichTextCtrl specific functionality
2164 // ----------------------------------------------------------------------------
2166 /// Add a new paragraph of text to the end of the buffer
2167 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2169 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2175 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2177 wxRichTextRange range
= GetBuffer().AddImage(image
);
2182 // ----------------------------------------------------------------------------
2183 // selection and ranges
2184 // ----------------------------------------------------------------------------
2186 void wxRichTextCtrl::SelectAll()
2188 SetSelection(-1, -1);
2192 void wxRichTextCtrl::SelectNone()
2194 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2196 wxRichTextRange oldSelection
= m_selectionRange
;
2198 m_selectionRange
= wxRichTextRange(-2, -2);
2200 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2202 m_selectionAnchor
= -2;
2205 static bool wxIsWordDelimiter(const wxString
& text
)
2207 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2210 /// Select the word at the given character position
2211 bool wxRichTextCtrl::SelectWord(long position
)
2213 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2216 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2220 if (position
== para
->GetRange().GetEnd())
2223 long positionStart
= position
;
2224 long positionEnd
= position
;
2226 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2228 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2229 if (wxIsWordDelimiter(text
))
2235 if (positionStart
< para
->GetRange().GetStart())
2236 positionStart
= para
->GetRange().GetStart();
2238 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2240 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2241 if (wxIsWordDelimiter(text
))
2247 if (positionEnd
>= para
->GetRange().GetEnd())
2248 positionEnd
= para
->GetRange().GetEnd();
2250 if (positionEnd
< positionStart
)
2253 SetSelection(positionStart
, positionEnd
+1);
2255 if (positionStart
>= 0)
2257 MoveCaret(positionStart
-1, true);
2258 SetDefaultStyleToCursorStyle();
2264 wxString
wxRichTextCtrl::GetStringSelection() const
2267 GetSelection(&from
, &to
);
2269 return GetRange(from
, to
);
2272 // ----------------------------------------------------------------------------
2274 // ----------------------------------------------------------------------------
2276 wxTextCtrlHitTestResult
2277 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2279 // implement in terms of the other overload as the native ports typically
2280 // can get the position and not (x, y) pair directly (although wxUniv
2281 // directly gets x and y -- and so overrides this method as well)
2283 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2285 if ( rc
!= wxTE_HT_UNKNOWN
)
2287 PositionToXY(pos
, x
, y
);
2293 wxTextCtrlHitTestResult
2294 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2297 wxClientDC
dc((wxRichTextCtrl
*) this);
2298 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2300 // Buffer uses logical position (relative to start of buffer)
2302 wxPoint pt2
= GetLogicalPoint(pt
);
2304 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2306 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2307 return wxTE_HT_BEFORE
;
2308 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2309 return wxTE_HT_BEYOND
;
2310 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2311 return wxTE_HT_ON_TEXT
;
2313 return wxTE_HT_UNKNOWN
;
2316 // ----------------------------------------------------------------------------
2317 // set/get the controls text
2318 // ----------------------------------------------------------------------------
2320 wxString
wxRichTextCtrl::DoGetValue() const
2322 return GetBuffer().GetText();
2325 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2327 // Public API for range is different from internals
2328 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2331 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2333 // Don't call Clear here, since it always sends a text updated event
2334 m_buffer
.ResetAndClearCommands();
2335 m_buffer
.SetDirty(true);
2336 m_caretPosition
= -1;
2337 m_caretPositionForDefaultStyle
= -2;
2338 m_caretAtLineStart
= false;
2339 m_selectionRange
.SetRange(-2, -2);
2349 if (!value
.IsEmpty())
2351 // Remove empty paragraph
2352 GetBuffer().Clear();
2353 DoWriteText(value
, flags
);
2355 // for compatibility, don't move the cursor when doing SetValue()
2356 SetInsertionPoint(0);
2360 // still send an event for consistency
2361 if (flags
& SetValue_SendEvent
)
2362 wxTextCtrl::SendTextUpdatedEvent(this);
2367 void wxRichTextCtrl::WriteText(const wxString
& value
)
2372 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2374 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2376 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2378 if ( flags
& SetValue_SendEvent
)
2379 wxTextCtrl::SendTextUpdatedEvent(this);
2382 void wxRichTextCtrl::AppendText(const wxString
& text
)
2384 SetInsertionPointEnd();
2389 /// Write an image at the current insertion point
2390 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2392 wxRichTextImageBlock imageBlock
;
2394 wxImage image2
= image
;
2395 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2396 return WriteImage(imageBlock
);
2401 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2403 wxRichTextImageBlock imageBlock
;
2406 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2407 return WriteImage(imageBlock
);
2412 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2414 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2417 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2421 wxRichTextImageBlock imageBlock
;
2423 wxImage image
= bitmap
.ConvertToImage();
2424 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2425 return WriteImage(imageBlock
);
2431 /// Insert a newline (actually paragraph) at the current insertion point.
2432 bool wxRichTextCtrl::Newline()
2434 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2437 /// Insert a line break at the current insertion point.
2438 bool wxRichTextCtrl::LineBreak()
2441 text
= wxRichTextLineBreakChar
;
2442 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2445 // ----------------------------------------------------------------------------
2446 // Clipboard operations
2447 // ----------------------------------------------------------------------------
2449 void wxRichTextCtrl::Copy()
2453 wxRichTextRange range
= GetInternalSelectionRange();
2454 GetBuffer().CopyToClipboard(range
);
2458 void wxRichTextCtrl::Cut()
2462 wxRichTextRange range
= GetInternalSelectionRange();
2463 GetBuffer().CopyToClipboard(range
);
2465 DeleteSelectedContent();
2471 void wxRichTextCtrl::Paste()
2475 BeginBatchUndo(_("Paste"));
2477 long newPos
= m_caretPosition
;
2478 DeleteSelectedContent(& newPos
);
2480 GetBuffer().PasteFromClipboard(newPos
);
2486 void wxRichTextCtrl::DeleteSelection()
2488 if (CanDeleteSelection())
2490 DeleteSelectedContent();
2494 bool wxRichTextCtrl::HasSelection() const
2496 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2499 bool wxRichTextCtrl::CanCopy() const
2501 // Can copy if there's a selection
2502 return HasSelection();
2505 bool wxRichTextCtrl::CanCut() const
2507 return HasSelection() && IsEditable();
2510 bool wxRichTextCtrl::CanPaste() const
2512 if ( !IsEditable() )
2515 return GetBuffer().CanPasteFromClipboard();
2518 bool wxRichTextCtrl::CanDeleteSelection() const
2520 return HasSelection() && IsEditable();
2524 // ----------------------------------------------------------------------------
2526 // ----------------------------------------------------------------------------
2528 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
2530 if (m_contextMenu
&& m_contextMenu
!= menu
)
2531 delete m_contextMenu
;
2532 m_contextMenu
= menu
;
2535 void wxRichTextCtrl::SetEditable(bool editable
)
2537 m_editable
= editable
;
2540 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2544 m_caretPosition
= pos
- 1;
2548 SetDefaultStyleToCursorStyle();
2551 void wxRichTextCtrl::SetInsertionPointEnd()
2553 long pos
= GetLastPosition();
2554 SetInsertionPoint(pos
);
2557 long wxRichTextCtrl::GetInsertionPoint() const
2559 return m_caretPosition
+1;
2562 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2564 return GetBuffer().GetRange().GetEnd();
2567 // If the return values from and to are the same, there is no
2569 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2571 *from
= m_selectionRange
.GetStart();
2572 *to
= m_selectionRange
.GetEnd();
2573 if ((*to
) != -1 && (*to
) != -2)
2577 bool wxRichTextCtrl::IsEditable() const
2582 // ----------------------------------------------------------------------------
2584 // ----------------------------------------------------------------------------
2586 void wxRichTextCtrl::SetSelection(long from
, long to
)
2588 // if from and to are both -1, it means (in wxWidgets) that all text should
2590 if ( (from
== -1) && (to
== -1) )
2593 to
= GetLastPosition()+1;
2602 wxRichTextRange oldSelection
= m_selectionRange
;
2603 m_selectionAnchor
= from
-1;
2604 m_selectionRange
.SetRange(from
, to
-1);
2606 m_caretPosition
= wxMax(-1, to
-1);
2608 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2613 // ----------------------------------------------------------------------------
2615 // ----------------------------------------------------------------------------
2617 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2618 const wxString
& value
)
2620 BeginBatchUndo(_("Replace"));
2622 DeleteSelectedContent();
2624 DoWriteText(value
, SetValue_SelectionOnly
);
2629 void wxRichTextCtrl::Remove(long from
, long to
)
2633 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2640 bool wxRichTextCtrl::IsModified() const
2642 return m_buffer
.IsModified();
2645 void wxRichTextCtrl::MarkDirty()
2647 m_buffer
.Modify(true);
2650 void wxRichTextCtrl::DiscardEdits()
2652 m_caretPositionForDefaultStyle
= -2;
2653 m_buffer
.Modify(false);
2654 m_buffer
.GetCommandProcessor()->ClearCommands();
2657 int wxRichTextCtrl::GetNumberOfLines() const
2659 return GetBuffer().GetParagraphCount();
2662 // ----------------------------------------------------------------------------
2663 // Positions <-> coords
2664 // ----------------------------------------------------------------------------
2666 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2668 return GetBuffer().XYToPosition(x
, y
);
2671 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2673 return GetBuffer().PositionToXY(pos
, x
, y
);
2676 // ----------------------------------------------------------------------------
2678 // ----------------------------------------------------------------------------
2680 void wxRichTextCtrl::ShowPosition(long pos
)
2682 if (!IsPositionVisible(pos
))
2683 ScrollIntoView(pos
-1, WXK_DOWN
);
2686 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2688 return GetBuffer().GetParagraphLength(lineNo
);
2691 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2693 return GetBuffer().GetParagraphText(lineNo
);
2696 // ----------------------------------------------------------------------------
2698 // ----------------------------------------------------------------------------
2700 void wxRichTextCtrl::Undo()
2704 GetCommandProcessor()->Undo();
2708 void wxRichTextCtrl::Redo()
2712 GetCommandProcessor()->Redo();
2716 bool wxRichTextCtrl::CanUndo() const
2718 return GetCommandProcessor()->CanUndo();
2721 bool wxRichTextCtrl::CanRedo() const
2723 return GetCommandProcessor()->CanRedo();
2726 // ----------------------------------------------------------------------------
2727 // implementation details
2728 // ----------------------------------------------------------------------------
2730 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2732 SetValue(event
.GetString());
2733 GetEventHandler()->ProcessEvent(event
);
2736 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2738 // By default, load the first file into the text window.
2739 if (event
.GetNumberOfFiles() > 0)
2741 LoadFile(event
.GetFiles()[0]);
2745 wxSize
wxRichTextCtrl::DoGetBestSize() const
2747 return wxSize(10, 10);
2750 // ----------------------------------------------------------------------------
2751 // standard handlers for standard edit menu events
2752 // ----------------------------------------------------------------------------
2754 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2759 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2764 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2769 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2774 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2779 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2784 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2786 event
.Enable( CanCut() );
2789 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2791 event
.Enable( CanCopy() );
2794 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2796 event
.Enable( CanDeleteSelection() );
2799 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2801 event
.Enable( CanPaste() );
2804 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2806 event
.Enable( CanUndo() );
2807 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2810 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2812 event
.Enable( CanRedo() );
2813 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2816 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2818 if (GetLastPosition() > 0)
2822 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2824 event
.Enable(GetLastPosition() > 0);
2827 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2829 if (event
.GetEventObject() != this)
2836 PopupMenu(m_contextMenu
);
2840 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2842 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2845 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2847 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2850 // extended style setting operation with flags including:
2851 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2852 // see richtextbuffer.h for more details.
2854 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2856 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2859 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2861 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2864 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2866 return GetBuffer().GetDefaultStyle();
2869 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2871 return GetBuffer().GetStyle(position
, style
);
2874 // get the common set of styles for the range
2875 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2877 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2880 /// Get the content (uncombined) attributes for this position.
2881 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2883 return GetBuffer().GetUncombinedStyle(position
, style
);
2886 /// Set font, and also the buffer attributes
2887 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2889 wxTextCtrlBase::SetFont(font
);
2891 wxTextAttr attr
= GetBuffer().GetAttributes();
2893 GetBuffer().SetBasicStyle(attr
);
2895 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2901 /// Transform logical to physical
2902 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2905 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2910 /// Transform physical to logical
2911 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2914 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2919 /// Position the caret
2920 void wxRichTextCtrl::PositionCaret()
2925 //wxLogDebug(wxT("PositionCaret"));
2928 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2930 wxPoint newPt
= caretRect
.GetPosition();
2931 wxSize newSz
= caretRect
.GetSize();
2932 wxPoint pt
= GetPhysicalPoint(newPt
);
2933 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2936 if (GetCaret()->GetSize() != newSz
)
2937 GetCaret()->SetSize(newSz
);
2939 int halfSize
= newSz
.y
/2;
2940 // If the caret is beyond the margin, hide it by moving it out of the way
2941 if (((pt
.y
+ halfSize
) < GetBuffer().GetTopMargin()) || ((pt
.y
+ halfSize
) > (GetClientSize().y
- GetBuffer().GetBottomMargin())))
2944 GetCaret()->Move(pt
);
2950 /// Get the caret height and position for the given character position
2951 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2953 wxClientDC
dc(this);
2954 dc
.SetFont(GetFont());
2961 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2963 // Caret height can't be zero
2965 height
= dc
.GetCharHeight();
2967 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2974 /// Gets the line for the visible caret position. If the caret is
2975 /// shown at the very end of the line, it means the next character is actually
2976 /// on the following line. So let's get the line we're expecting to find
2977 /// if this is the case.
2978 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2980 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2981 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2984 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2985 if (caretPosition
== lineRange
.GetStart()-1 &&
2986 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2988 if (!m_caretAtLineStart
)
2989 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2996 /// Move the caret to the given character position
2997 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2999 if (GetBuffer().GetDirty())
3002 if (pos
<= GetBuffer().GetRange().GetEnd())
3004 SetCaretPosition(pos
, showAtLineStart
);
3014 /// Layout the buffer: which we must do before certain operations, such as
3015 /// setting the caret position.
3016 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3018 if (GetBuffer().GetDirty() || onlyVisibleRect
)
3020 wxRect
availableSpace(GetClientSize());
3021 if (availableSpace
.width
== 0)
3022 availableSpace
.width
= 10;
3023 if (availableSpace
.height
== 0)
3024 availableSpace
.height
= 10;
3026 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3027 if (onlyVisibleRect
)
3029 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3030 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3033 wxClientDC
dc(this);
3034 dc
.SetFont(GetFont());
3038 GetBuffer().Defragment();
3039 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3040 GetBuffer().Layout(dc
, availableSpace
, flags
);
3041 GetBuffer().SetDirty(false);
3050 /// Is all of the selection bold?
3051 bool wxRichTextCtrl::IsSelectionBold()
3056 wxRichTextRange range
= GetSelectionRange();
3057 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3058 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3060 return HasCharacterAttributes(range
, attr
);
3064 // If no selection, then we need to combine current style with default style
3065 // to see what the effect would be if we started typing.
3067 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3069 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3070 if (GetStyle(pos
, attr
))
3072 if (IsDefaultStyleShowing())
3073 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3074 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3080 /// Is all of the selection italics?
3081 bool wxRichTextCtrl::IsSelectionItalics()
3085 wxRichTextRange range
= GetSelectionRange();
3087 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3088 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3090 return HasCharacterAttributes(range
, attr
);
3094 // If no selection, then we need to combine current style with default style
3095 // to see what the effect would be if we started typing.
3097 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3099 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3100 if (GetStyle(pos
, attr
))
3102 if (IsDefaultStyleShowing())
3103 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3104 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3110 /// Is all of the selection underlined?
3111 bool wxRichTextCtrl::IsSelectionUnderlined()
3115 wxRichTextRange range
= GetSelectionRange();
3117 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3118 attr
.SetFontUnderlined(true);
3120 return HasCharacterAttributes(range
, attr
);
3124 // If no selection, then we need to combine current style with default style
3125 // to see what the effect would be if we started typing.
3127 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3128 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3130 if (GetStyle(pos
, attr
))
3132 if (IsDefaultStyleShowing())
3133 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3134 return attr
.GetFontUnderlined();
3140 /// Apply bold to the selection
3141 bool wxRichTextCtrl::ApplyBoldToSelection()
3144 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3145 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3148 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3151 wxRichTextAttr current
= GetDefaultStyleEx();
3152 current
.Apply(attr
);
3153 SetAndShowDefaultStyle(current
);
3158 /// Apply italic to the selection
3159 bool wxRichTextCtrl::ApplyItalicToSelection()
3162 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3163 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3166 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3169 wxRichTextAttr current
= GetDefaultStyleEx();
3170 current
.Apply(attr
);
3171 SetAndShowDefaultStyle(current
);
3176 /// Apply underline to the selection
3177 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3180 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3181 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3184 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3187 wxRichTextAttr current
= GetDefaultStyleEx();
3188 current
.Apply(attr
);
3189 SetAndShowDefaultStyle(current
);
3194 /// Is all of the selection aligned according to the specified flag?
3195 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3197 wxRichTextRange range
;
3199 range
= GetSelectionRange();
3201 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3204 attr
.SetAlignment(alignment
);
3206 return HasParagraphAttributes(range
, attr
);
3209 /// Apply alignment to the selection
3210 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3213 attr
.SetAlignment(alignment
);
3215 return SetStyle(GetSelectionRange(), attr
);
3218 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3220 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3225 /// Apply a named style to the selection
3226 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3228 // Flags are defined within each definition, so only certain
3229 // attributes are applied.
3230 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3232 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3234 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3236 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3238 wxRichTextRange range
;
3241 range
= GetSelectionRange();
3244 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3245 range
= wxRichTextRange(pos
, pos
+1);
3248 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3251 // Make sure the attr has the style name
3252 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3254 attr
.SetParagraphStyleName(def
->GetName());
3256 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3257 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3258 // to change its style independently.
3259 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3262 attr
.SetCharacterStyleName(def
->GetName());
3265 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3268 wxRichTextAttr current
= GetDefaultStyleEx();
3269 current
.Apply(attr
);
3270 SetAndShowDefaultStyle(current
);
3275 /// Apply the style sheet to the buffer, for example if the styles have changed.
3276 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3279 styleSheet
= GetBuffer().GetStyleSheet();
3283 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3285 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3293 /// Sets the default style to the style under the cursor
3294 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3297 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3299 // If at the start of a paragraph, use the next position.
3300 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3302 if (GetUncombinedStyle(pos
, attr
))
3304 SetDefaultStyle(attr
);
3311 /// Returns the first visible position in the current view
3312 long wxRichTextCtrl::GetFirstVisiblePosition() const
3314 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3316 return line
->GetAbsoluteRange().GetStart();
3321 /// Get the first visible point in the window
3322 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3325 int startXUnits
, startYUnits
;
3327 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3328 GetViewStart(& startXUnits
, & startYUnits
);
3330 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3333 /// The adjusted caret position is the character position adjusted to take
3334 /// into account whether we're at the start of a paragraph, in which case
3335 /// style information should be taken from the next position, not current one.
3336 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3338 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3340 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3345 /// Get/set the selection range in character positions. -1, -1 means no selection.
3346 /// The range is in API convention, i.e. a single character selection is denoted
3348 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3350 wxRichTextRange range
= GetInternalSelectionRange();
3351 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3352 range
.SetEnd(range
.GetEnd() + 1);
3356 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3358 SetSelection(range
.GetStart(), range
.GetEnd());
3362 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3364 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3367 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3369 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3372 /// Clear list for given range
3373 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3375 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3378 /// Number/renumber any list elements in the given range
3379 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3381 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3384 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3386 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3389 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3390 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3392 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3395 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3397 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3400 /// Deletes the content in the given range
3401 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3403 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3406 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3408 if (sm_availableFontNames
.GetCount() == 0)
3410 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3411 sm_availableFontNames
.Sort();
3413 return sm_availableFontNames
;
3416 void wxRichTextCtrl::ClearAvailableFontNames()
3418 sm_availableFontNames
.Clear();
3421 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3423 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3425 wxTextAttrEx basicStyle
= GetBasicStyle();
3426 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3427 SetBasicStyle(basicStyle
);
3428 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3433 // Refresh the area affected by a selection change
3434 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3436 // Calculate the refresh rectangle - just the affected lines
3437 long firstPos
, lastPos
;
3438 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3440 firstPos
= newSelection
.GetStart();
3441 lastPos
= newSelection
.GetEnd();
3443 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3445 firstPos
= oldSelection
.GetStart();
3446 lastPos
= oldSelection
.GetEnd();
3448 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3454 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3455 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3458 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3459 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3461 if (firstLine
&& lastLine
)
3463 wxSize clientSize
= GetClientSize();
3464 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3465 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3468 pt1
.y
= wxMax(0, pt1
.y
);
3470 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3472 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3473 RefreshRect(rect
, false);
3481 #if wxRICHTEXT_USE_OWN_CARET
3483 // ----------------------------------------------------------------------------
3484 // initialization and destruction
3485 // ----------------------------------------------------------------------------
3487 void wxRichTextCaret::Init()
3493 m_richTextCtrl
= NULL
;
3494 m_needsUpdate
= false;
3498 wxRichTextCaret::~wxRichTextCaret()
3500 if (m_timer
.IsRunning())
3504 // ----------------------------------------------------------------------------
3505 // showing/hiding/moving the caret (base class interface)
3506 // ----------------------------------------------------------------------------
3508 void wxRichTextCaret::DoShow()
3512 if (!m_timer
.IsRunning())
3513 m_timer
.Start(GetBlinkTime());
3518 void wxRichTextCaret::DoHide()
3520 if (m_timer
.IsRunning())
3526 void wxRichTextCaret::DoMove()
3532 if (m_xOld
!= -1 && m_yOld
!= -1)
3536 wxRect
rect(GetPosition(), GetSize());
3537 m_richTextCtrl
->RefreshRect(rect
, false);
3546 void wxRichTextCaret::DoSize()
3548 int countVisible
= m_countVisible
;
3549 if (countVisible
> 0)
3555 if (countVisible
> 0)
3557 m_countVisible
= countVisible
;
3562 // ----------------------------------------------------------------------------
3563 // handling the focus
3564 // ----------------------------------------------------------------------------
3566 void wxRichTextCaret::OnSetFocus()
3574 void wxRichTextCaret::OnKillFocus()
3579 // ----------------------------------------------------------------------------
3580 // drawing the caret
3581 // ----------------------------------------------------------------------------
3583 void wxRichTextCaret::Refresh()
3587 wxRect
rect(GetPosition(), GetSize());
3588 m_richTextCtrl
->RefreshRect(rect
, false);
3592 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3594 dc
->SetPen( *wxBLACK_PEN
);
3596 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3597 dc
->SetPen(*wxBLACK_PEN
);
3599 wxPoint
pt(m_x
, m_y
);
3603 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3605 if (IsVisible() && m_flashOn
)
3606 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3609 void wxRichTextCaret::Notify()
3611 m_flashOn
= !m_flashOn
;
3615 void wxRichTextCaretTimer::Notify()
3620 // wxRICHTEXT_USE_OWN_CARET