1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
29 #include "wx/textfile.h"
31 #include "wx/filename.h"
32 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 #include "wx/fontenum.h"
37 // DLL options compatibility check:
39 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
41 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
42 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
43 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
44 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
45 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN
, wxRichTextEvent
);
46 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER
, wxRichTextEvent
);
47 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE
, wxRichTextEvent
);
49 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
50 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
51 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
54 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
60 #if wxRICHTEXT_USE_OWN_CARET
65 * This implements a non-flashing cursor in case there
66 * are platform-specific problems with the generic caret.
67 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
70 class wxRichTextCaret
: public wxCaret
75 // default - use Create()
76 wxRichTextCaret() { Init(); }
77 // creates a block caret associated with the given window
78 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
79 : wxCaret(window
, width
, height
) { Init(); m_richTextCtrl
= window
; }
80 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
81 : wxCaret(window
, size
) { Init(); m_richTextCtrl
= window
; }
83 virtual ~wxRichTextCaret();
88 // called by wxWindow (not using the event tables)
89 virtual void OnSetFocus();
90 virtual void OnKillFocus();
92 // draw the caret on the given DC
93 void DoDraw(wxDC
*dc
);
95 // get the visible count
96 int GetVisibleCount() const { return m_countVisible
; }
98 // delay repositioning
99 bool GetNeedsUpdate() const { return m_needsUpdate
; }
100 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
103 virtual void DoShow();
104 virtual void DoHide();
105 virtual void DoMove();
106 virtual void DoSize();
116 bool m_hasFocus
; // true => our window has focus
117 bool m_needsUpdate
; // must be repositioned
119 wxRichTextCtrl
* m_richTextCtrl
;
123 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxTextCtrlBase
)
125 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
127 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxTextCtrlBase
)
128 EVT_PAINT(wxRichTextCtrl::OnPaint
)
129 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
130 EVT_IDLE(wxRichTextCtrl::OnIdle
)
131 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
132 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
133 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
134 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
135 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
136 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
137 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
138 EVT_CHAR(wxRichTextCtrl::OnChar
)
139 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
140 EVT_SIZE(wxRichTextCtrl::OnSize
)
141 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
142 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
143 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
144 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
145 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
147 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
148 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
150 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
151 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
153 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
154 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
156 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
157 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
159 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
160 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
162 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
163 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
165 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
166 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
173 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
175 wxRichTextCtrl::wxRichTextCtrl()
176 : wxScrollHelper(this)
181 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
183 const wxString
& value
,
187 const wxValidator
& validator
,
188 const wxString
& name
)
189 : wxScrollHelper(this)
192 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
196 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
197 const wxValidator
& validator
, const wxString
& name
)
201 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
202 style
|wxFULL_REPAINT_ON_RESIZE
,
208 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
211 // No physical scrolling, so we can preserve margins
212 EnableScrolling(false, false);
214 if (style
& wxTE_READONLY
)
217 // The base attributes must all have default values
218 wxTextAttr attributes
;
219 attributes
.SetFont(GetFont());
220 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
221 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
222 attributes
.SetLineSpacing(10);
223 attributes
.SetParagraphSpacingAfter(10);
224 attributes
.SetParagraphSpacingBefore(0);
226 SetBasicStyle(attributes
);
228 // The default attributes will be merged with base attributes, so
229 // can be empty to begin with
230 wxTextAttr defaultAttributes
;
231 SetDefaultStyle(defaultAttributes
);
233 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
234 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
237 GetBuffer().SetRichTextCtrl(this);
239 #if wxRICHTEXT_USE_OWN_CARET
240 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
242 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
245 // Tell the sizers to use the given or best size
246 SetInitialSize(size
);
248 #if wxRICHTEXT_BUFFERED_PAINTING
250 RecreateBuffer(size
);
253 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
254 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
256 SetCursor(m_textCursor
);
258 if (!value
.IsEmpty())
261 GetBuffer().AddEventHandler(this);
264 wxAcceleratorEntry entries
[6];
266 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
267 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
268 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
269 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
270 entries
[4].Set(wxACCEL_CMD
, (int) 'Z', wxID_UNDO
);
271 entries
[5].Set(wxACCEL_CMD
, (int) 'Y', wxID_REDO
);
273 wxAcceleratorTable
accel(6, entries
);
274 SetAcceleratorTable(accel
);
279 wxRichTextCtrl::~wxRichTextCtrl()
281 GetBuffer().RemoveEventHandler(this);
283 delete m_contextMenu
;
286 /// Member initialisation
287 void wxRichTextCtrl::Init()
289 m_contextMenu
= NULL
;
291 m_caretPosition
= -1;
292 m_selectionRange
.SetRange(-2, -2);
293 m_selectionAnchor
= -2;
295 m_caretAtLineStart
= false;
297 m_fullLayoutRequired
= false;
298 m_fullLayoutTime
= 0;
299 m_fullLayoutSavedPosition
= 0;
300 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
301 m_caretPositionForDefaultStyle
= -2;
304 void wxRichTextCtrl::DoThaw()
306 if (GetBuffer().GetDirty())
315 void wxRichTextCtrl::Clear()
317 m_buffer
.ResetAndClearCommands();
318 m_buffer
.SetDirty(true);
319 m_caretPosition
= -1;
320 m_caretPositionForDefaultStyle
= -2;
321 m_caretAtLineStart
= false;
322 m_selectionRange
.SetRange(-2, -2);
332 wxTextCtrl::SendTextUpdatedEvent(this);
336 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
338 #if !wxRICHTEXT_USE_OWN_CARET
339 if (GetCaret() && !IsFrozen())
344 #if wxRICHTEXT_BUFFERED_PAINTING
345 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
355 dc
.SetFont(GetFont());
357 // Paint the background
360 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
362 wxRect
drawingArea(GetUpdateRegion().GetBox());
363 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
365 wxRect
availableSpace(GetClientSize());
366 if (GetBuffer().GetDirty())
368 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
369 GetBuffer().SetDirty(false);
373 wxRect
clipRect(availableSpace
);
374 clipRect
.x
+= GetBuffer().GetLeftMargin();
375 clipRect
.y
+= GetBuffer().GetTopMargin();
376 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
377 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
378 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
379 dc
.SetClippingRegion(clipRect
);
381 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
383 dc
.DestroyClippingRegion();
385 #if wxRICHTEXT_USE_OWN_CARET
386 if (GetCaret()->IsVisible())
388 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
393 #if !wxRICHTEXT_USE_OWN_CARET
400 // Empty implementation, to prevent flicker
401 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
405 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
409 #if !wxRICHTEXT_USE_OWN_CARET
415 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
416 // Work around dropouts when control is focused
424 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
429 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
430 // Work around dropouts when control is focused
438 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
444 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
450 dc
.SetFont(GetFont());
453 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
455 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
457 m_dragStart
= event
.GetLogicalPosition(dc
);
461 bool caretAtLineStart
= false;
463 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
465 // If we're at the start of a line (but not first in para)
466 // then we should keep the caret showing at the start of the line
467 // by showing the m_caretAtLineStart flag.
468 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
469 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
471 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
472 caretAtLineStart
= true;
476 long oldCaretPos
= m_caretPosition
;
478 MoveCaret(position
, caretAtLineStart
);
479 SetDefaultStyleToCursorStyle();
481 if (event
.ShiftDown())
483 if (m_selectionRange
.GetStart() == -2)
484 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
486 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
496 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
501 if (GetCapture() == this)
504 // See if we clicked on a URL
507 dc
.SetFont(GetFont());
510 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
511 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
513 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
515 wxRichTextEvent
cmdEvent(
516 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
518 cmdEvent
.SetEventObject(this);
519 cmdEvent
.SetPosition(m_caretPosition
+1);
521 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
524 if (GetStyle(position
, attr
))
526 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
528 wxString urlTarget
= attr
.GetURL();
529 if (!urlTarget
.IsEmpty())
531 wxMouseEvent
mouseEvent(event
);
533 long startPos
= 0, endPos
= 0;
534 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
537 startPos
= obj
->GetRange().GetStart();
538 endPos
= obj
->GetRange().GetEnd();
541 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
542 InitCommandEvent(urlEvent
);
544 urlEvent
.SetString(urlTarget
);
546 GetEventHandler()->ProcessEvent(urlEvent
);
556 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
560 dc
.SetFont(GetFont());
563 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
564 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
566 // See if we need to change the cursor
569 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
572 if (GetStyle(position
, attr
))
574 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
576 SetCursor(m_urlCursor
);
578 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
580 SetCursor(m_textCursor
);
585 SetCursor(m_textCursor
);
588 if (!event
.Dragging())
594 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
596 // TODO: test closeness
598 bool caretAtLineStart
= false;
600 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
602 // If we're at the start of a line (but not first in para)
603 // then we should keep the caret showing at the start of the line
604 // by showing the m_caretAtLineStart flag.
605 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
606 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
608 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
609 caretAtLineStart
= true;
613 if (m_caretPosition
!= position
)
615 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
617 MoveCaret(position
, caretAtLineStart
);
618 SetDefaultStyleToCursorStyle();
624 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
628 wxRichTextEvent
cmdEvent(
629 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
631 cmdEvent
.SetEventObject(this);
632 cmdEvent
.SetPosition(m_caretPosition
+1);
634 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
638 /// Left-double-click
639 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
641 wxRichTextEvent
cmdEvent(
642 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
644 cmdEvent
.SetEventObject(this);
645 cmdEvent
.SetPosition(m_caretPosition
+1);
647 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
649 SelectWord(GetCaretPosition()+1);
654 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
656 wxRichTextEvent
cmdEvent(
657 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
659 cmdEvent
.SetEventObject(this);
660 cmdEvent
.SetPosition(m_caretPosition
+1);
662 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
667 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
671 flags
|= wxRICHTEXT_CTRL_DOWN
;
672 if (event
.ShiftDown())
673 flags
|= wxRICHTEXT_SHIFT_DOWN
;
675 flags
|= wxRICHTEXT_ALT_DOWN
;
677 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
679 if (event
.GetKeyCode() == WXK_LEFT
||
680 event
.GetKeyCode() == WXK_RIGHT
||
681 event
.GetKeyCode() == WXK_UP
||
682 event
.GetKeyCode() == WXK_DOWN
||
683 event
.GetKeyCode() == WXK_HOME
||
684 event
.GetKeyCode() == WXK_PAGEUP
||
685 event
.GetKeyCode() == WXK_PAGEDOWN
||
686 event
.GetKeyCode() == WXK_END
||
688 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
689 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
690 event
.GetKeyCode() == WXK_NUMPAD_UP
||
691 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
692 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
693 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
694 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
695 event
.GetKeyCode() == WXK_NUMPAD_END
)
697 KeyboardNavigate(event
.GetKeyCode(), flags
);
701 long keycode
= event
.GetKeyCode();
761 case WXK_NUMPAD_HOME
:
762 case WXK_NUMPAD_LEFT
:
764 case WXK_NUMPAD_RIGHT
:
765 case WXK_NUMPAD_DOWN
:
766 case WXK_NUMPAD_PAGEUP
:
767 case WXK_NUMPAD_PAGEDOWN
:
769 case WXK_NUMPAD_BEGIN
:
770 case WXK_NUMPAD_INSERT
:
771 case WXK_NUMPAD_DELETE
:
772 case WXK_WINDOWS_LEFT
:
781 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
782 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
784 BeginBatchUndo(_("Delete Text"));
786 long newPos
= m_caretPosition
;
788 bool processed
= DeleteSelectedContent(& newPos
);
790 // Submit range in character positions, which are greater than caret positions,
791 // so subtract 1 for deleted character and add 1 for conversion to character position.
796 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
799 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
805 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
810 if (GetLastPosition() == -1)
814 m_caretPosition
= -1;
816 SetDefaultStyleToCursorStyle();
819 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
821 wxRichTextEvent
cmdEvent(
822 wxEVT_COMMAND_RICHTEXT_DELETE
,
824 cmdEvent
.SetEventObject(this);
825 cmdEvent
.SetFlags(flags
);
826 cmdEvent
.SetPosition(m_caretPosition
+1);
827 GetEventHandler()->ProcessEvent(cmdEvent
);
837 // all the other keys modify the controls contents which shouldn't be
838 // possible if we're read-only
845 if (event
.GetKeyCode() == WXK_RETURN
)
847 BeginBatchUndo(_("Insert Text"));
849 long newPos
= m_caretPosition
;
851 DeleteSelectedContent(& newPos
);
853 if (event
.ShiftDown())
856 text
= wxRichTextLineBreakChar
;
857 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
858 m_caretAtLineStart
= true;
862 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
865 SetDefaultStyleToCursorStyle();
867 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
869 wxRichTextEvent
cmdEvent(
870 wxEVT_COMMAND_RICHTEXT_RETURN
,
872 cmdEvent
.SetEventObject(this);
873 cmdEvent
.SetFlags(flags
);
874 cmdEvent
.SetPosition(newPos
+1);
876 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
878 // Generate conventional event
879 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
880 InitCommandEvent(textEvent
);
882 GetEventHandler()->ProcessEvent(textEvent
);
886 else if (event
.GetKeyCode() == WXK_BACK
)
888 BeginBatchUndo(_("Delete Text"));
890 long newPos
= m_caretPosition
;
892 bool processed
= DeleteSelectedContent(& newPos
);
894 // Submit range in character positions, which are greater than caret positions,
895 // so subtract 1 for deleted character and add 1 for conversion to character position.
900 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
903 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
909 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
914 if (GetLastPosition() == -1)
918 m_caretPosition
= -1;
920 SetDefaultStyleToCursorStyle();
923 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
925 wxRichTextEvent
cmdEvent(
926 wxEVT_COMMAND_RICHTEXT_DELETE
,
928 cmdEvent
.SetEventObject(this);
929 cmdEvent
.SetFlags(flags
);
930 cmdEvent
.SetPosition(m_caretPosition
+1);
931 GetEventHandler()->ProcessEvent(cmdEvent
);
935 else if (event
.GetKeyCode() == WXK_DELETE
)
937 BeginBatchUndo(_("Delete Text"));
939 long newPos
= m_caretPosition
;
941 bool processed
= DeleteSelectedContent(& newPos
);
943 // Submit range in character positions, which are greater than caret positions,
944 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
948 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
949 if (pos
!= -1 && (pos
> newPos
))
951 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
957 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
962 if (GetLastPosition() == -1)
966 m_caretPosition
= -1;
968 SetDefaultStyleToCursorStyle();
971 wxRichTextEvent
cmdEvent(
972 wxEVT_COMMAND_RICHTEXT_DELETE
,
974 cmdEvent
.SetEventObject(this);
975 cmdEvent
.SetFlags(flags
);
976 cmdEvent
.SetPosition(m_caretPosition
+1);
977 GetEventHandler()->ProcessEvent(cmdEvent
);
983 long keycode
= event
.GetKeyCode();
997 // Fixes AltGr+key with European input languages on Windows
998 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1005 wxRichTextEvent
cmdEvent(
1006 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1008 cmdEvent
.SetEventObject(this);
1009 cmdEvent
.SetFlags(flags
);
1011 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1013 cmdEvent
.SetCharacter((wxChar
) keycode
);
1015 cmdEvent
.SetPosition(m_caretPosition
+1);
1017 if (keycode
== wxT('\t'))
1019 // See if we need to promote or demote the selection or paragraph at the cursor
1020 // position, instead of inserting a tab.
1021 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1022 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1023 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1025 wxRichTextRange range
;
1027 range
= GetSelectionRange();
1029 range
= para
->GetRange().FromInternal();
1031 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1033 PromoteList(promoteBy
, range
, NULL
);
1035 GetEventHandler()->ProcessEvent(cmdEvent
);
1041 BeginBatchUndo(_("Insert Text"));
1043 long newPos
= m_caretPosition
;
1044 DeleteSelectedContent(& newPos
);
1047 wxString str
= event
.GetUnicodeKey();
1049 wxString str
= (wxChar
) event
.GetKeyCode();
1051 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1055 SetDefaultStyleToCursorStyle();
1056 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1058 GetEventHandler()->ProcessEvent(cmdEvent
);
1066 /// Delete content if there is a selection, e.g. when pressing a key.
1067 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1071 long pos
= m_selectionRange
.GetStart();
1072 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1073 m_selectionRange
.SetRange(-2, -2);
1083 /// Keyboard navigation
1087 Left: left one character
1088 Right: right one character
1091 Ctrl-Left: left one word
1092 Ctrl-Right: right one word
1093 Ctrl-Up: previous paragraph start
1094 Ctrl-Down: next start of paragraph
1097 Ctrl-Home: start of document
1098 Ctrl-End: end of document
1099 Page-Up: Up a screen
1100 Page-Down: Down a screen
1104 Ctrl-Alt-PgUp: Start of window
1105 Ctrl-Alt-PgDn: End of window
1106 F8: Start selection mode
1107 Esc: End selection mode
1109 Adding Shift does the above but starts/extends selection.
1114 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1116 bool success
= false;
1118 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1120 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1121 success
= WordRight(1, flags
);
1123 success
= MoveRight(1, flags
);
1125 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1127 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1128 success
= WordLeft(1, flags
);
1130 success
= MoveLeft(1, flags
);
1132 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1134 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1135 success
= MoveToParagraphStart(flags
);
1137 success
= MoveUp(1, flags
);
1139 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1141 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1142 success
= MoveToParagraphEnd(flags
);
1144 success
= MoveDown(1, flags
);
1146 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1148 success
= PageUp(1, flags
);
1150 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1152 success
= PageDown(1, flags
);
1154 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1156 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1157 success
= MoveHome(flags
);
1159 success
= MoveToLineStart(flags
);
1161 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1163 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1164 success
= MoveEnd(flags
);
1166 success
= MoveToLineEnd(flags
);
1171 ScrollIntoView(m_caretPosition
, keyCode
);
1172 SetDefaultStyleToCursorStyle();
1178 /// Extend the selection. Selections are in caret positions.
1179 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1181 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1183 if (oldPos
== newPos
)
1186 wxRichTextRange oldSelection
= m_selectionRange
;
1188 // If not currently selecting, start selecting
1189 if (m_selectionRange
.GetStart() == -2)
1191 m_selectionAnchor
= oldPos
;
1193 if (oldPos
> newPos
)
1194 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1196 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1200 // Always ensure that the selection range start is greater than
1202 if (newPos
> m_selectionAnchor
)
1203 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1204 else if (newPos
== m_selectionAnchor
)
1205 m_selectionRange
= wxRichTextRange(-2, -2);
1207 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1210 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1212 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1214 wxLogDebug(wxT("Strange selection range"));
1223 /// Scroll into view, returning true if we scrolled.
1224 /// This takes a _caret_ position.
1225 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1227 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1233 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1235 int startXUnits
, startYUnits
;
1236 GetViewStart(& startXUnits
, & startYUnits
);
1237 int startY
= startYUnits
* ppuY
;
1240 GetVirtualSize(& sx
, & sy
);
1246 wxRect rect
= line
->GetRect();
1248 bool scrolled
= false;
1250 wxSize clientSize
= GetClientSize();
1251 clientSize
.y
-= GetBuffer().GetBottomMargin();
1253 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1255 int y
= rect
.y
- GetClientSize().y
/2;
1256 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1257 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1259 if (startYUnits
!= yUnits
)
1261 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1264 #if !wxRICHTEXT_USE_OWN_CARET
1274 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1275 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1276 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1277 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1279 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1281 // Make it scroll so this item is at the bottom
1283 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1284 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1286 // If we're still off the screen, scroll another line down
1287 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1290 if (startYUnits
!= yUnits
)
1292 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1296 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1298 // Make it scroll so this item is at the top
1300 int y
= rect
.y
- GetBuffer().GetTopMargin();
1301 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1303 if (startYUnits
!= yUnits
)
1305 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1311 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1312 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1313 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1314 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1316 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1318 // Make it scroll so this item is at the top
1320 int y
= rect
.y
- GetBuffer().GetTopMargin();
1321 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1323 if (startYUnits
!= yUnits
)
1325 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1329 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1331 // Make it scroll so this item is at the bottom
1333 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1334 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1336 // If we're still off the screen, scroll another line down
1337 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1340 if (startYUnits
!= yUnits
)
1342 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1348 #if !wxRICHTEXT_USE_OWN_CARET
1356 /// Is the given position visible on the screen?
1357 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1359 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1365 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1368 GetViewStart(& startX
, & startY
);
1370 startY
= startY
* ppuY
;
1372 wxRect rect
= line
->GetRect();
1373 wxSize clientSize
= GetClientSize();
1374 clientSize
.y
-= GetBuffer().GetBottomMargin();
1376 return (rect
.GetBottom() > (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1379 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1381 m_caretPosition
= position
;
1382 m_caretAtLineStart
= showAtLineStart
;
1385 /// Move caret one visual step forward: this may mean setting a flag
1386 /// and keeping the same position if we're going from the end of one line
1387 /// to the start of the next, which may be the exact same caret position.
1388 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1390 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1392 // Only do the check if we're not at the end of the paragraph (where things work OK
1394 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1396 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1400 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1402 // We're at the end of a line. See whether we need to
1403 // stay at the same actual caret position but change visual
1404 // position, or not.
1405 if (oldPosition
== lineRange
.GetEnd())
1407 if (m_caretAtLineStart
)
1409 // We're already at the start of the line, so actually move on now.
1410 m_caretPosition
= oldPosition
+ 1;
1411 m_caretAtLineStart
= false;
1415 // We're showing at the end of the line, so keep to
1416 // the same position but indicate that we're to show
1417 // at the start of the next line.
1418 m_caretPosition
= oldPosition
;
1419 m_caretAtLineStart
= true;
1421 SetDefaultStyleToCursorStyle();
1427 SetDefaultStyleToCursorStyle();
1430 /// Move caret one visual step backward: this may mean setting a flag
1431 /// and keeping the same position if we're going from the end of one line
1432 /// to the start of the next, which may be the exact same caret position.
1433 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1435 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1437 // Only do the check if we're not at the start of the paragraph (where things work OK
1439 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1441 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1445 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1447 // We're at the start of a line. See whether we need to
1448 // stay at the same actual caret position but change visual
1449 // position, or not.
1450 if (oldPosition
== lineRange
.GetStart())
1452 m_caretPosition
= oldPosition
-1;
1453 m_caretAtLineStart
= true;
1456 else if (oldPosition
== lineRange
.GetEnd())
1458 if (m_caretAtLineStart
)
1460 // We're at the start of the line, so keep the same caret position
1461 // but clear the start-of-line flag.
1462 m_caretPosition
= oldPosition
;
1463 m_caretAtLineStart
= false;
1467 // We're showing at the end of the line, so go back
1468 // to the previous character position.
1469 m_caretPosition
= oldPosition
- 1;
1471 SetDefaultStyleToCursorStyle();
1477 SetDefaultStyleToCursorStyle();
1481 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1483 long endPos
= GetBuffer().GetRange().GetEnd();
1485 if (m_caretPosition
+ noPositions
< endPos
)
1487 long oldPos
= m_caretPosition
;
1488 long newPos
= m_caretPosition
+ noPositions
;
1490 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1494 // Determine by looking at oldPos and m_caretPosition whether
1495 // we moved from the end of a line to the start of the next line, in which case
1496 // we want to adjust the caret position such that it is positioned at the
1497 // start of the next line, rather than jumping past the first character of the
1499 if (noPositions
== 1 && !extendSel
)
1500 MoveCaretForward(oldPos
);
1502 SetCaretPosition(newPos
);
1505 SetDefaultStyleToCursorStyle();
1514 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1518 if (m_caretPosition
> startPos
- noPositions
+ 1)
1520 long oldPos
= m_caretPosition
;
1521 long newPos
= m_caretPosition
- noPositions
;
1522 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1526 if (noPositions
== 1 && !extendSel
)
1527 MoveCaretBack(oldPos
);
1529 SetCaretPosition(newPos
);
1532 SetDefaultStyleToCursorStyle();
1541 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1543 return MoveDown(- noLines
, flags
);
1547 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1552 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1553 wxPoint pt
= GetCaret()->GetPosition();
1554 long newLine
= lineNumber
+ noLines
;
1556 if (lineNumber
!= -1)
1560 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1562 if (newLine
> lastLine
)
1572 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1575 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1581 wxClientDC
dc(this);
1583 dc
.SetFont(GetFont());
1585 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1587 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1589 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1590 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1591 // so we view the caret at the start of the line.
1592 bool caretLineStart
= false;
1593 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1595 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1596 wxRichTextRange lineRange
;
1598 lineRange
= thisLine
->GetAbsoluteRange();
1600 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1603 caretLineStart
= true;
1607 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1608 if (para
&& para
->GetRange().GetStart() == newPos
)
1613 long newSelEnd
= newPos
;
1615 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1619 SetCaretPosition(newPos
, caretLineStart
);
1621 SetDefaultStyleToCursorStyle();
1629 /// Move to the end of the paragraph
1630 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1632 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1635 long newPos
= para
->GetRange().GetEnd() - 1;
1636 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1640 SetCaretPosition(newPos
);
1642 SetDefaultStyleToCursorStyle();
1650 /// Move to the start of the paragraph
1651 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1653 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1656 long newPos
= para
->GetRange().GetStart() - 1;
1657 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1661 SetCaretPosition(newPos
);
1663 SetDefaultStyleToCursorStyle();
1671 /// Move to the end of the line
1672 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1674 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1678 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1679 long newPos
= lineRange
.GetEnd();
1680 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1684 SetCaretPosition(newPos
);
1686 SetDefaultStyleToCursorStyle();
1694 /// Move to the start of the line
1695 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1697 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1700 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1701 long newPos
= lineRange
.GetStart()-1;
1703 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1707 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1709 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1711 SetDefaultStyleToCursorStyle();
1719 /// Move to the start of the buffer
1720 bool wxRichTextCtrl::MoveHome(int flags
)
1722 if (m_caretPosition
!= -1)
1724 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1728 SetCaretPosition(-1);
1730 SetDefaultStyleToCursorStyle();
1738 /// Move to the end of the buffer
1739 bool wxRichTextCtrl::MoveEnd(int flags
)
1741 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1743 if (m_caretPosition
!= endPos
)
1745 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1749 SetCaretPosition(endPos
);
1751 SetDefaultStyleToCursorStyle();
1759 /// Move noPages pages up
1760 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1762 return PageDown(- noPages
, flags
);
1765 /// Move noPages pages down
1766 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1768 // Calculate which line occurs noPages * screen height further down.
1769 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1772 wxSize clientSize
= GetClientSize();
1773 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1775 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1778 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1779 long pos
= lineRange
.GetStart()-1;
1780 if (pos
!= m_caretPosition
)
1782 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1784 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1788 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1790 SetDefaultStyleToCursorStyle();
1800 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1802 return str
== wxT(" ") || str
== wxT("\t");
1805 // Finds the caret position for the next word
1806 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1808 long endPos
= GetBuffer().GetRange().GetEnd();
1812 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1814 // First skip current text to space
1815 while (i
< endPos
&& i
> -1)
1817 // i is in character, not caret positions
1818 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1819 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1820 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1824 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1831 while (i
< endPos
&& i
> -1)
1833 // i is in character, not caret positions
1834 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1835 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1836 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1837 return wxMax(-1, i
);
1839 if (text
.empty()) // End of paragraph, or maybe an image
1840 return wxMax(-1, i
- 1);
1841 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1845 // Convert to caret position
1846 return wxMax(-1, i
- 1);
1855 long i
= m_caretPosition
;
1857 // First skip white space
1858 while (i
< endPos
&& i
> -1)
1860 // i is in character, not caret positions
1861 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1862 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1864 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1866 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1871 // Next skip current text to space
1872 while (i
< endPos
&& i
> -1)
1874 // i is in character, not caret positions
1875 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1876 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1877 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1880 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1893 /// Move n words left
1894 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1896 long pos
= FindNextWordPosition(-1);
1897 if (pos
!= m_caretPosition
)
1899 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1901 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1905 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1907 SetDefaultStyleToCursorStyle();
1915 /// Move n words right
1916 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1918 long pos
= FindNextWordPosition(1);
1919 if (pos
!= m_caretPosition
)
1921 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1923 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1927 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1929 SetDefaultStyleToCursorStyle();
1938 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1940 // Only do sizing optimization for large buffers
1941 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1943 m_fullLayoutRequired
= true;
1944 m_fullLayoutTime
= wxGetLocalTimeMillis();
1945 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1946 LayoutContent(true /* onlyVisibleRect */);
1949 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1951 #if wxRICHTEXT_BUFFERED_PAINTING
1959 /// Idle-time processing
1960 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1962 #if wxRICHTEXT_USE_OWN_CARET
1963 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1965 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1971 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1973 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1975 m_fullLayoutRequired
= false;
1976 m_fullLayoutTime
= 0;
1977 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1978 ShowPosition(m_fullLayoutSavedPosition
);
1982 if (m_caretPositionForDefaultStyle
!= -2)
1984 // If the caret position has changed, no longer reflect the default style
1986 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1987 m_caretPositionForDefaultStyle
= -2;
1994 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1996 #if wxRICHTEXT_USE_OWN_CARET
1997 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2000 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2007 /// Set up scrollbars, e.g. after a resize
2008 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2013 if (GetBuffer().IsEmpty())
2015 SetScrollbars(0, 0, 0, 0, 0, 0);
2019 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2020 // of pixels. See e.g. wxVScrolledWindow for ideas.
2021 int pixelsPerUnit
= 5;
2022 wxSize clientSize
= GetClientSize();
2024 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2026 // Round up so we have at least maxHeight pixels
2027 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2029 int startX
= 0, startY
= 0;
2031 GetViewStart(& startX
, & startY
);
2033 int maxPositionX
= 0;
2034 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2036 int newStartX
= wxMin(maxPositionX
, startX
);
2037 int newStartY
= wxMin(maxPositionY
, startY
);
2039 int oldPPUX
, oldPPUY
;
2040 int oldStartX
, oldStartY
;
2041 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2042 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2043 GetViewStart(& oldStartX
, & oldStartY
);
2044 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2046 oldVirtualSizeY
/= oldPPUY
;
2048 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2051 // Don't set scrollbars if there were none before, and there will be none now.
2052 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2055 // Move to previous scroll position if
2057 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2060 /// Paint the background
2061 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2063 wxColour backgroundColour
= GetBackgroundColour();
2064 if (!backgroundColour
.Ok())
2065 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2067 // Clear the background
2068 dc
.SetBrush(wxBrush(backgroundColour
));
2069 dc
.SetPen(*wxTRANSPARENT_PEN
);
2070 wxRect
windowRect(GetClientSize());
2071 windowRect
.x
-= 2; windowRect
.y
-= 2;
2072 windowRect
.width
+= 4; windowRect
.height
+= 4;
2074 // We need to shift the rectangle to take into account
2075 // scrolling. Converting device to logical coordinates.
2076 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2077 dc
.DrawRectangle(windowRect
);
2080 #if wxRICHTEXT_BUFFERED_PAINTING
2081 /// Recreate buffer bitmap if necessary
2082 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2085 if (sz
== wxDefaultSize
)
2086 sz
= GetClientSize();
2088 if (sz
.x
< 1 || sz
.y
< 1)
2091 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2092 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2093 return m_bufferBitmap
.Ok();
2097 // ----------------------------------------------------------------------------
2098 // file IO functions
2099 // ----------------------------------------------------------------------------
2101 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2103 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2105 m_filename
= filename
;
2108 SetInsertionPoint(0);
2111 SetupScrollbars(true);
2113 wxTextCtrl::SendTextUpdatedEvent(this);
2119 wxLogError(_("File couldn't be loaded."));
2125 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2127 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2129 m_filename
= filename
;
2136 wxLogError(_("The text couldn't be saved."));
2141 // ----------------------------------------------------------------------------
2142 // wxRichTextCtrl specific functionality
2143 // ----------------------------------------------------------------------------
2145 /// Add a new paragraph of text to the end of the buffer
2146 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2148 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2154 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2156 wxRichTextRange range
= GetBuffer().AddImage(image
);
2161 // ----------------------------------------------------------------------------
2162 // selection and ranges
2163 // ----------------------------------------------------------------------------
2165 void wxRichTextCtrl::SelectAll()
2167 SetSelection(0, GetLastPosition()+1);
2168 m_selectionAnchor
= -1;
2172 void wxRichTextCtrl::SelectNone()
2174 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2176 wxRichTextRange oldSelection
= m_selectionRange
;
2178 m_selectionRange
= wxRichTextRange(-2, -2);
2180 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2182 m_selectionAnchor
= -2;
2185 static bool wxIsWordDelimiter(const wxString
& text
)
2187 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2190 /// Select the word at the given character position
2191 bool wxRichTextCtrl::SelectWord(long position
)
2193 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2196 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2200 if (position
== para
->GetRange().GetEnd())
2203 long positionStart
= position
;
2204 long positionEnd
= position
;
2206 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2208 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2209 if (wxIsWordDelimiter(text
))
2215 if (positionStart
< para
->GetRange().GetStart())
2216 positionStart
= para
->GetRange().GetStart();
2218 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2220 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2221 if (wxIsWordDelimiter(text
))
2227 if (positionEnd
>= para
->GetRange().GetEnd())
2228 positionEnd
= para
->GetRange().GetEnd();
2230 if (positionEnd
< positionStart
)
2233 SetSelection(positionStart
, positionEnd
+1);
2235 if (positionStart
>= 0)
2237 MoveCaret(positionStart
-1, true);
2238 SetDefaultStyleToCursorStyle();
2244 wxString
wxRichTextCtrl::GetStringSelection() const
2247 GetSelection(&from
, &to
);
2249 return GetRange(from
, to
);
2252 // ----------------------------------------------------------------------------
2254 // ----------------------------------------------------------------------------
2256 wxTextCtrlHitTestResult
2257 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2259 // implement in terms of the other overload as the native ports typically
2260 // can get the position and not (x, y) pair directly (although wxUniv
2261 // directly gets x and y -- and so overrides this method as well)
2263 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2265 if ( rc
!= wxTE_HT_UNKNOWN
)
2267 PositionToXY(pos
, x
, y
);
2273 wxTextCtrlHitTestResult
2274 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2277 wxClientDC
dc((wxRichTextCtrl
*) this);
2278 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2280 // Buffer uses logical position (relative to start of buffer)
2282 wxPoint pt2
= GetLogicalPoint(pt
);
2284 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2286 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2287 return wxTE_HT_BEFORE
;
2288 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2289 return wxTE_HT_BEYOND
;
2290 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2291 return wxTE_HT_ON_TEXT
;
2293 return wxTE_HT_UNKNOWN
;
2296 // ----------------------------------------------------------------------------
2297 // set/get the controls text
2298 // ----------------------------------------------------------------------------
2300 wxString
wxRichTextCtrl::DoGetValue() const
2302 return GetBuffer().GetText();
2305 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2307 // Public API for range is different from internals
2308 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2311 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2313 // Don't call Clear here, since it always sends a text updated event
2314 m_buffer
.ResetAndClearCommands();
2315 m_buffer
.SetDirty(true);
2316 m_caretPosition
= -1;
2317 m_caretPositionForDefaultStyle
= -2;
2318 m_caretAtLineStart
= false;
2319 m_selectionRange
.SetRange(-2, -2);
2329 if (!value
.IsEmpty())
2331 // Remove empty paragraph
2332 GetBuffer().Clear();
2333 DoWriteText(value
, flags
);
2335 // for compatibility, don't move the cursor when doing SetValue()
2336 SetInsertionPoint(0);
2340 // still send an event for consistency
2341 if (flags
& SetValue_SendEvent
)
2342 wxTextCtrl::SendTextUpdatedEvent(this);
2347 void wxRichTextCtrl::WriteText(const wxString
& value
)
2352 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2354 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2356 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2358 if ( flags
& SetValue_SendEvent
)
2359 wxTextCtrl::SendTextUpdatedEvent(this);
2362 void wxRichTextCtrl::AppendText(const wxString
& text
)
2364 SetInsertionPointEnd();
2369 /// Write an image at the current insertion point
2370 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2372 wxRichTextImageBlock imageBlock
;
2374 wxImage image2
= image
;
2375 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2376 return WriteImage(imageBlock
);
2381 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2383 wxRichTextImageBlock imageBlock
;
2386 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2387 return WriteImage(imageBlock
);
2392 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2394 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2397 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2401 wxRichTextImageBlock imageBlock
;
2403 wxImage image
= bitmap
.ConvertToImage();
2404 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2405 return WriteImage(imageBlock
);
2411 /// Insert a newline (actually paragraph) at the current insertion point.
2412 bool wxRichTextCtrl::Newline()
2414 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2417 /// Insert a line break at the current insertion point.
2418 bool wxRichTextCtrl::LineBreak()
2421 text
= wxRichTextLineBreakChar
;
2422 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2425 // ----------------------------------------------------------------------------
2426 // Clipboard operations
2427 // ----------------------------------------------------------------------------
2429 void wxRichTextCtrl::Copy()
2433 wxRichTextRange range
= GetInternalSelectionRange();
2434 GetBuffer().CopyToClipboard(range
);
2438 void wxRichTextCtrl::Cut()
2442 wxRichTextRange range
= GetInternalSelectionRange();
2443 GetBuffer().CopyToClipboard(range
);
2445 DeleteSelectedContent();
2451 void wxRichTextCtrl::Paste()
2455 BeginBatchUndo(_("Paste"));
2457 long newPos
= m_caretPosition
;
2458 DeleteSelectedContent(& newPos
);
2460 GetBuffer().PasteFromClipboard(newPos
);
2466 void wxRichTextCtrl::DeleteSelection()
2468 if (CanDeleteSelection())
2470 DeleteSelectedContent();
2474 bool wxRichTextCtrl::HasSelection() const
2476 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2479 bool wxRichTextCtrl::CanCopy() const
2481 // Can copy if there's a selection
2482 return HasSelection();
2485 bool wxRichTextCtrl::CanCut() const
2487 return HasSelection() && IsEditable();
2490 bool wxRichTextCtrl::CanPaste() const
2492 if ( !IsEditable() )
2495 return GetBuffer().CanPasteFromClipboard();
2498 bool wxRichTextCtrl::CanDeleteSelection() const
2500 return HasSelection() && IsEditable();
2504 // ----------------------------------------------------------------------------
2506 // ----------------------------------------------------------------------------
2508 void wxRichTextCtrl::SetEditable(bool editable
)
2510 m_editable
= editable
;
2513 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2517 m_caretPosition
= pos
- 1;
2522 void wxRichTextCtrl::SetInsertionPointEnd()
2524 long pos
= GetLastPosition();
2525 SetInsertionPoint(pos
);
2528 long wxRichTextCtrl::GetInsertionPoint() const
2530 return m_caretPosition
+1;
2533 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2535 return GetBuffer().GetRange().GetEnd();
2538 // If the return values from and to are the same, there is no
2540 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2542 *from
= m_selectionRange
.GetStart();
2543 *to
= m_selectionRange
.GetEnd();
2544 if ((*to
) != -1 && (*to
) != -2)
2548 bool wxRichTextCtrl::IsEditable() const
2553 // ----------------------------------------------------------------------------
2555 // ----------------------------------------------------------------------------
2557 void wxRichTextCtrl::SetSelection(long from
, long to
)
2559 // if from and to are both -1, it means (in wxWidgets) that all text should
2561 if ( (from
== -1) && (to
== -1) )
2564 to
= GetLastPosition()+1;
2573 wxRichTextRange oldSelection
= m_selectionRange
;
2574 m_selectionAnchor
= from
;
2575 m_selectionRange
.SetRange(from
, to
-1);
2577 m_caretPosition
= from
-1;
2579 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2584 // ----------------------------------------------------------------------------
2586 // ----------------------------------------------------------------------------
2588 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2589 const wxString
& value
)
2591 BeginBatchUndo(_("Replace"));
2593 DeleteSelectedContent();
2595 DoWriteText(value
, SetValue_SelectionOnly
);
2600 void wxRichTextCtrl::Remove(long from
, long to
)
2604 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2611 bool wxRichTextCtrl::IsModified() const
2613 return m_buffer
.IsModified();
2616 void wxRichTextCtrl::MarkDirty()
2618 m_buffer
.Modify(true);
2621 void wxRichTextCtrl::DiscardEdits()
2623 m_caretPositionForDefaultStyle
= -2;
2624 m_buffer
.Modify(false);
2625 m_buffer
.GetCommandProcessor()->ClearCommands();
2628 int wxRichTextCtrl::GetNumberOfLines() const
2630 return GetBuffer().GetParagraphCount();
2633 // ----------------------------------------------------------------------------
2634 // Positions <-> coords
2635 // ----------------------------------------------------------------------------
2637 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2639 return GetBuffer().XYToPosition(x
, y
);
2642 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2644 return GetBuffer().PositionToXY(pos
, x
, y
);
2647 // ----------------------------------------------------------------------------
2649 // ----------------------------------------------------------------------------
2651 void wxRichTextCtrl::ShowPosition(long pos
)
2653 if (!IsPositionVisible(pos
))
2654 ScrollIntoView(pos
-1, WXK_DOWN
);
2657 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2659 return GetBuffer().GetParagraphLength(lineNo
);
2662 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2664 return GetBuffer().GetParagraphText(lineNo
);
2667 // ----------------------------------------------------------------------------
2669 // ----------------------------------------------------------------------------
2671 void wxRichTextCtrl::Undo()
2675 GetCommandProcessor()->Undo();
2679 void wxRichTextCtrl::Redo()
2683 GetCommandProcessor()->Redo();
2687 bool wxRichTextCtrl::CanUndo() const
2689 return GetCommandProcessor()->CanUndo();
2692 bool wxRichTextCtrl::CanRedo() const
2694 return GetCommandProcessor()->CanRedo();
2697 // ----------------------------------------------------------------------------
2698 // implementation details
2699 // ----------------------------------------------------------------------------
2701 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2703 SetValue(event
.GetString());
2704 GetEventHandler()->ProcessEvent(event
);
2707 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2709 // By default, load the first file into the text window.
2710 if (event
.GetNumberOfFiles() > 0)
2712 LoadFile(event
.GetFiles()[0]);
2716 wxSize
wxRichTextCtrl::DoGetBestSize() const
2718 return wxSize(10, 10);
2721 // ----------------------------------------------------------------------------
2722 // standard handlers for standard edit menu events
2723 // ----------------------------------------------------------------------------
2725 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2730 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2735 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2740 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2745 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2750 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2755 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2757 event
.Enable( CanCut() );
2760 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2762 event
.Enable( CanCopy() );
2765 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2767 event
.Enable( CanDeleteSelection() );
2770 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2772 event
.Enable( CanPaste() );
2775 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2777 event
.Enable( CanUndo() );
2778 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2781 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2783 event
.Enable( CanRedo() );
2784 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2787 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2792 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2794 event
.Enable(GetLastPosition() > 0);
2797 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2799 if (event
.GetEventObject() != this)
2807 m_contextMenu
= new wxMenu
;
2808 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2809 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2810 m_contextMenu
->AppendSeparator();
2811 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2812 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2813 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2814 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2815 m_contextMenu
->AppendSeparator();
2816 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2818 PopupMenu(m_contextMenu
);
2822 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2824 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2827 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2829 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2832 // extended style setting operation with flags including:
2833 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2834 // see richtextbuffer.h for more details.
2836 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2838 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2841 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2843 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2846 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2848 return GetBuffer().GetDefaultStyle();
2851 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2853 return GetBuffer().GetStyle(position
, style
);
2856 // get the common set of styles for the range
2857 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2859 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2862 /// Get the content (uncombined) attributes for this position.
2863 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2865 return GetBuffer().GetUncombinedStyle(position
, style
);
2868 /// Set font, and also the buffer attributes
2869 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2871 wxTextCtrlBase::SetFont(font
);
2873 wxTextAttr attr
= GetBuffer().GetAttributes();
2875 GetBuffer().SetBasicStyle(attr
);
2877 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2883 /// Transform logical to physical
2884 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2887 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2892 /// Transform physical to logical
2893 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2896 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2901 /// Position the caret
2902 void wxRichTextCtrl::PositionCaret()
2907 //wxLogDebug(wxT("PositionCaret"));
2910 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2912 wxPoint newPt
= caretRect
.GetPosition();
2913 wxSize newSz
= caretRect
.GetSize();
2914 wxPoint pt
= GetPhysicalPoint(newPt
);
2915 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2918 if (GetCaret()->GetSize() != newSz
)
2919 GetCaret()->SetSize(newSz
);
2921 int halfSize
= newSz
.y
/2;
2922 // If the caret is beyond the margin, hide it by moving it out of the way
2923 if (((pt
.y
+ halfSize
) < GetBuffer().GetTopMargin()) || ((pt
.y
+ halfSize
) > (GetClientSize().y
- GetBuffer().GetBottomMargin())))
2926 GetCaret()->Move(pt
);
2932 /// Get the caret height and position for the given character position
2933 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2935 wxClientDC
dc(this);
2936 dc
.SetFont(GetFont());
2943 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2945 // Caret height can't be zero
2947 height
= dc
.GetCharHeight();
2949 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2956 /// Gets the line for the visible caret position. If the caret is
2957 /// shown at the very end of the line, it means the next character is actually
2958 /// on the following line. So let's get the line we're expecting to find
2959 /// if this is the case.
2960 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2962 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2963 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2966 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2967 if (caretPosition
== lineRange
.GetStart()-1 &&
2968 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2970 if (!m_caretAtLineStart
)
2971 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2978 /// Move the caret to the given character position
2979 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2981 if (GetBuffer().GetDirty())
2984 if (pos
<= GetBuffer().GetRange().GetEnd())
2986 SetCaretPosition(pos
, showAtLineStart
);
2996 /// Layout the buffer: which we must do before certain operations, such as
2997 /// setting the caret position.
2998 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3000 if (GetBuffer().GetDirty() || onlyVisibleRect
)
3002 wxRect
availableSpace(GetClientSize());
3003 if (availableSpace
.width
== 0)
3004 availableSpace
.width
= 10;
3005 if (availableSpace
.height
== 0)
3006 availableSpace
.height
= 10;
3008 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3009 if (onlyVisibleRect
)
3011 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3012 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3015 wxClientDC
dc(this);
3016 dc
.SetFont(GetFont());
3020 GetBuffer().Defragment();
3021 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3022 GetBuffer().Layout(dc
, availableSpace
, flags
);
3023 GetBuffer().SetDirty(false);
3032 /// Is all of the selection bold?
3033 bool wxRichTextCtrl::IsSelectionBold()
3038 wxRichTextRange range
= GetSelectionRange();
3039 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3040 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3042 return HasCharacterAttributes(range
, attr
);
3046 // If no selection, then we need to combine current style with default style
3047 // to see what the effect would be if we started typing.
3049 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3051 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3052 if (GetStyle(pos
, attr
))
3054 if (IsDefaultStyleShowing())
3055 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3056 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3062 /// Is all of the selection italics?
3063 bool wxRichTextCtrl::IsSelectionItalics()
3067 wxRichTextRange range
= GetSelectionRange();
3069 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3070 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3072 return HasCharacterAttributes(range
, attr
);
3076 // If no selection, then we need to combine current style with default style
3077 // to see what the effect would be if we started typing.
3079 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3081 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3082 if (GetStyle(pos
, attr
))
3084 if (IsDefaultStyleShowing())
3085 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3086 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3092 /// Is all of the selection underlined?
3093 bool wxRichTextCtrl::IsSelectionUnderlined()
3097 wxRichTextRange range
= GetSelectionRange();
3099 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3100 attr
.SetFontUnderlined(true);
3102 return HasCharacterAttributes(range
, attr
);
3106 // If no selection, then we need to combine current style with default style
3107 // to see what the effect would be if we started typing.
3109 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3110 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3112 if (GetStyle(pos
, attr
))
3114 if (IsDefaultStyleShowing())
3115 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3116 return attr
.GetFontUnderlined();
3122 /// Apply bold to the selection
3123 bool wxRichTextCtrl::ApplyBoldToSelection()
3126 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3127 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3130 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3133 wxRichTextAttr current
= GetDefaultStyleEx();
3134 current
.Apply(attr
);
3135 SetAndShowDefaultStyle(current
);
3140 /// Apply italic to the selection
3141 bool wxRichTextCtrl::ApplyItalicToSelection()
3144 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3145 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
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 underline to the selection
3159 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3162 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3163 attr
.SetFontUnderlined(!IsSelectionUnderlined());
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 /// Is all of the selection aligned according to the specified flag?
3177 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3179 wxRichTextRange range
;
3181 range
= GetSelectionRange();
3183 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3186 attr
.SetAlignment(alignment
);
3188 return HasParagraphAttributes(range
, attr
);
3191 /// Apply alignment to the selection
3192 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3195 attr
.SetAlignment(alignment
);
3197 return SetStyle(GetSelectionRange(), attr
);
3200 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3202 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3207 /// Apply a named style to the selection
3208 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3210 // Flags are defined within each definition, so only certain
3211 // attributes are applied.
3212 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3214 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3216 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3218 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3220 wxRichTextRange range
;
3223 range
= GetSelectionRange();
3226 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3227 range
= wxRichTextRange(pos
, pos
+1);
3230 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3233 // Make sure the attr has the style name
3234 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3236 attr
.SetParagraphStyleName(def
->GetName());
3238 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3239 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3240 // to change its style independently.
3241 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3244 attr
.SetCharacterStyleName(def
->GetName());
3247 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3250 wxRichTextAttr current
= GetDefaultStyleEx();
3251 current
.Apply(attr
);
3252 SetAndShowDefaultStyle(current
);
3257 /// Apply the style sheet to the buffer, for example if the styles have changed.
3258 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3261 styleSheet
= GetBuffer().GetStyleSheet();
3265 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3267 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3275 /// Sets the default style to the style under the cursor
3276 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3279 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3281 // If at the start of a paragraph, use the next position.
3282 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3284 if (GetUncombinedStyle(pos
, attr
))
3286 SetDefaultStyle(attr
);
3293 /// Returns the first visible position in the current view
3294 long wxRichTextCtrl::GetFirstVisiblePosition() const
3296 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3298 return line
->GetAbsoluteRange().GetStart();
3303 /// Get the first visible point in the window
3304 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3307 int startXUnits
, startYUnits
;
3309 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3310 GetViewStart(& startXUnits
, & startYUnits
);
3312 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3315 /// The adjusted caret position is the character position adjusted to take
3316 /// into account whether we're at the start of a paragraph, in which case
3317 /// style information should be taken from the next position, not current one.
3318 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3320 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3322 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3327 /// Get/set the selection range in character positions. -1, -1 means no selection.
3328 /// The range is in API convention, i.e. a single character selection is denoted
3330 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3332 wxRichTextRange range
= GetInternalSelectionRange();
3333 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3334 range
.SetEnd(range
.GetEnd() + 1);
3338 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3340 wxRichTextRange
range1(range
);
3341 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3342 range1
.SetEnd(range1
.GetEnd() - 1);
3344 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3346 SetInternalSelectionRange(range1
);
3350 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3352 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3355 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3357 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3360 /// Clear list for given range
3361 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3363 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3366 /// Number/renumber any list elements in the given range
3367 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3369 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3372 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3374 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3377 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3378 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3380 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3383 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3385 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3388 /// Deletes the content in the given range
3389 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3391 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3394 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3396 if (sm_availableFontNames
.GetCount() == 0)
3398 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3399 sm_availableFontNames
.Sort();
3401 return sm_availableFontNames
;
3404 void wxRichTextCtrl::ClearAvailableFontNames()
3406 sm_availableFontNames
.Clear();
3409 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3411 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3413 wxTextAttrEx basicStyle
= GetBasicStyle();
3414 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3415 SetBasicStyle(basicStyle
);
3416 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3421 // Refresh the area affected by a selection change
3422 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3424 // Calculate the refresh rectangle - just the affected lines
3425 long firstPos
, lastPos
;
3426 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3428 firstPos
= newSelection
.GetStart();
3429 lastPos
= newSelection
.GetEnd();
3431 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3433 firstPos
= oldSelection
.GetStart();
3434 lastPos
= oldSelection
.GetEnd();
3436 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3442 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3443 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3446 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3447 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3449 if (firstLine
&& lastLine
)
3451 wxSize clientSize
= GetClientSize();
3452 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3453 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3456 pt1
.y
= wxMax(0, pt1
.y
);
3458 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3460 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3461 RefreshRect(rect
, false);
3469 #if wxRICHTEXT_USE_OWN_CARET
3471 // ----------------------------------------------------------------------------
3472 // initialization and destruction
3473 // ----------------------------------------------------------------------------
3475 void wxRichTextCaret::Init()
3481 m_richTextCtrl
= NULL
;
3482 m_needsUpdate
= false;
3485 wxRichTextCaret::~wxRichTextCaret()
3489 // ----------------------------------------------------------------------------
3490 // showing/hiding/moving the caret (base class interface)
3491 // ----------------------------------------------------------------------------
3493 void wxRichTextCaret::DoShow()
3498 void wxRichTextCaret::DoHide()
3503 void wxRichTextCaret::DoMove()
3509 if (m_xOld
!= -1 && m_yOld
!= -1)
3513 wxRect
rect(GetPosition(), GetSize());
3514 m_richTextCtrl
->RefreshRect(rect
, false);
3523 void wxRichTextCaret::DoSize()
3525 int countVisible
= m_countVisible
;
3526 if (countVisible
> 0)
3532 if (countVisible
> 0)
3534 m_countVisible
= countVisible
;
3539 // ----------------------------------------------------------------------------
3540 // handling the focus
3541 // ----------------------------------------------------------------------------
3543 void wxRichTextCaret::OnSetFocus()
3551 void wxRichTextCaret::OnKillFocus()
3556 // ----------------------------------------------------------------------------
3557 // drawing the caret
3558 // ----------------------------------------------------------------------------
3560 void wxRichTextCaret::Refresh()
3564 wxRect
rect(GetPosition(), GetSize());
3565 m_richTextCtrl
->RefreshRect(rect
, false);
3569 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3571 dc
->SetPen( *wxBLACK_PEN
);
3573 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3574 dc
->SetPen(*wxBLACK_PEN
);
3576 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3577 // done under wxGTK - no idea why
3578 //dc->SetLogicalFunction(wxINVERT);
3580 wxPoint
pt(m_x
, m_y
);
3584 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3586 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3589 // wxRICHTEXT_USE_OWN_CARET