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
, wxControl
)
125 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
127 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
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
)
199 if (!wxControl::Create(parent
, id
, pos
, size
,
200 style
|wxFULL_REPAINT_ON_RESIZE
,
206 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
209 // No physical scrolling, so we can preserve margins
210 EnableScrolling(false, false);
212 if (style
& wxTE_READONLY
)
215 // The base attributes must all have default values
216 wxTextAttr attributes
;
217 attributes
.SetFont(GetFont());
218 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
219 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
220 attributes
.SetLineSpacing(10);
221 attributes
.SetParagraphSpacingAfter(10);
222 attributes
.SetParagraphSpacingBefore(0);
224 SetBasicStyle(attributes
);
226 // The default attributes will be merged with base attributes, so
227 // can be empty to begin with
228 wxTextAttr defaultAttributes
;
229 SetDefaultStyle(defaultAttributes
);
231 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
232 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
235 GetBuffer().SetRichTextCtrl(this);
237 #if wxRICHTEXT_USE_OWN_CARET
238 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
240 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
243 // Tell the sizers to use the given or best size
244 SetInitialSize(size
);
246 #if wxRICHTEXT_BUFFERED_PAINTING
248 RecreateBuffer(size
);
251 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
252 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
254 SetCursor(m_textCursor
);
256 if (!value
.IsEmpty())
259 GetBuffer().AddEventHandler(this);
262 wxAcceleratorEntry entries
[6];
264 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
265 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
266 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
267 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
268 entries
[4].Set(wxACCEL_CMD
, (int) 'Z', wxID_UNDO
);
269 entries
[5].Set(wxACCEL_CMD
, (int) 'Y', wxID_REDO
);
271 wxAcceleratorTable
accel(6, entries
);
272 SetAcceleratorTable(accel
);
277 wxRichTextCtrl::~wxRichTextCtrl()
279 GetBuffer().RemoveEventHandler(this);
281 delete m_contextMenu
;
284 /// Member initialisation
285 void wxRichTextCtrl::Init()
287 m_contextMenu
= NULL
;
289 m_caretPosition
= -1;
290 m_selectionRange
.SetRange(-2, -2);
291 m_selectionAnchor
= -2;
293 m_caretAtLineStart
= false;
295 m_fullLayoutRequired
= false;
296 m_fullLayoutTime
= 0;
297 m_fullLayoutSavedPosition
= 0;
298 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
299 m_caretPositionForDefaultStyle
= -2;
302 void wxRichTextCtrl::DoThaw()
304 if (GetBuffer().GetDirty())
313 void wxRichTextCtrl::Clear()
315 m_buffer
.ResetAndClearCommands();
316 m_buffer
.SetDirty(true);
317 m_caretPosition
= -1;
318 m_caretPositionForDefaultStyle
= -2;
319 m_caretAtLineStart
= false;
320 m_selectionRange
.SetRange(-2, -2);
330 wxTextCtrl::SendTextUpdatedEvent(this);
334 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
336 #if !wxRICHTEXT_USE_OWN_CARET
337 if (GetCaret() && !IsFrozen())
342 #if wxRICHTEXT_BUFFERED_PAINTING
343 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
353 dc
.SetFont(GetFont());
355 // Paint the background
358 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
360 wxRect
drawingArea(GetUpdateRegion().GetBox());
361 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
363 wxRect
availableSpace(GetClientSize());
364 if (GetBuffer().GetDirty())
366 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
367 GetBuffer().SetDirty(false);
371 wxRect
clipRect(availableSpace
);
372 clipRect
.x
+= GetBuffer().GetLeftMargin();
373 clipRect
.y
+= GetBuffer().GetTopMargin();
374 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
375 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
376 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
377 dc
.SetClippingRegion(clipRect
);
379 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
381 dc
.DestroyClippingRegion();
383 #if wxRICHTEXT_USE_OWN_CARET
384 if (GetCaret()->IsVisible())
386 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
391 #if !wxRICHTEXT_USE_OWN_CARET
398 // Empty implementation, to prevent flicker
399 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
403 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
407 #if !wxRICHTEXT_USE_OWN_CARET
413 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
414 // Work around dropouts when control is focused
422 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
427 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
428 // Work around dropouts when control is focused
436 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
442 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
448 dc
.SetFont(GetFont());
451 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
453 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
455 m_dragStart
= event
.GetLogicalPosition(dc
);
459 bool caretAtLineStart
= false;
461 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
463 // If we're at the start of a line (but not first in para)
464 // then we should keep the caret showing at the start of the line
465 // by showing the m_caretAtLineStart flag.
466 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
467 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
469 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
470 caretAtLineStart
= true;
474 long oldCaretPos
= m_caretPosition
;
476 MoveCaret(position
, caretAtLineStart
);
477 SetDefaultStyleToCursorStyle();
479 if (event
.ShiftDown())
481 if (m_selectionRange
.GetStart() == -2)
482 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
484 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
494 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
499 if (GetCapture() == this)
502 // See if we clicked on a URL
505 dc
.SetFont(GetFont());
508 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
509 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
511 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
513 wxRichTextEvent
cmdEvent(
514 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
516 cmdEvent
.SetEventObject(this);
517 cmdEvent
.SetPosition(m_caretPosition
+1);
519 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
522 if (GetStyle(position
, attr
))
524 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
526 wxString urlTarget
= attr
.GetURL();
527 if (!urlTarget
.IsEmpty())
529 wxMouseEvent
mouseEvent(event
);
531 long startPos
= 0, endPos
= 0;
532 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
535 startPos
= obj
->GetRange().GetStart();
536 endPos
= obj
->GetRange().GetEnd();
539 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
540 InitCommandEvent(urlEvent
);
542 urlEvent
.SetString(urlTarget
);
544 GetEventHandler()->ProcessEvent(urlEvent
);
554 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
558 dc
.SetFont(GetFont());
561 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
562 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
564 // See if we need to change the cursor
567 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
570 if (GetStyle(position
, attr
))
572 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
574 SetCursor(m_urlCursor
);
576 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
578 SetCursor(m_textCursor
);
583 SetCursor(m_textCursor
);
586 if (!event
.Dragging())
592 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
594 // TODO: test closeness
596 bool caretAtLineStart
= false;
598 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
600 // If we're at the start of a line (but not first in para)
601 // then we should keep the caret showing at the start of the line
602 // by showing the m_caretAtLineStart flag.
603 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
604 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
606 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
607 caretAtLineStart
= true;
611 if (m_caretPosition
!= position
)
613 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
615 MoveCaret(position
, caretAtLineStart
);
616 SetDefaultStyleToCursorStyle();
622 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
626 wxRichTextEvent
cmdEvent(
627 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
629 cmdEvent
.SetEventObject(this);
630 cmdEvent
.SetPosition(m_caretPosition
+1);
632 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
636 /// Left-double-click
637 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
639 wxRichTextEvent
cmdEvent(
640 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
642 cmdEvent
.SetEventObject(this);
643 cmdEvent
.SetPosition(m_caretPosition
+1);
645 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
647 SelectWord(GetCaretPosition()+1);
652 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
654 wxRichTextEvent
cmdEvent(
655 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
657 cmdEvent
.SetEventObject(this);
658 cmdEvent
.SetPosition(m_caretPosition
+1);
660 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
665 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
669 flags
|= wxRICHTEXT_CTRL_DOWN
;
670 if (event
.ShiftDown())
671 flags
|= wxRICHTEXT_SHIFT_DOWN
;
673 flags
|= wxRICHTEXT_ALT_DOWN
;
675 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
677 if (event
.GetKeyCode() == WXK_LEFT
||
678 event
.GetKeyCode() == WXK_RIGHT
||
679 event
.GetKeyCode() == WXK_UP
||
680 event
.GetKeyCode() == WXK_DOWN
||
681 event
.GetKeyCode() == WXK_HOME
||
682 event
.GetKeyCode() == WXK_PAGEUP
||
683 event
.GetKeyCode() == WXK_PAGEDOWN
||
684 event
.GetKeyCode() == WXK_END
||
686 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
687 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
688 event
.GetKeyCode() == WXK_NUMPAD_UP
||
689 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
690 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
691 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
692 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
693 event
.GetKeyCode() == WXK_NUMPAD_END
)
695 KeyboardNavigate(event
.GetKeyCode(), flags
);
699 long keycode
= event
.GetKeyCode();
759 case WXK_NUMPAD_HOME
:
760 case WXK_NUMPAD_LEFT
:
762 case WXK_NUMPAD_RIGHT
:
763 case WXK_NUMPAD_DOWN
:
764 case WXK_NUMPAD_PAGEUP
:
765 case WXK_NUMPAD_PAGEDOWN
:
767 case WXK_NUMPAD_BEGIN
:
768 case WXK_NUMPAD_INSERT
:
769 case WXK_NUMPAD_DELETE
:
770 case WXK_WINDOWS_LEFT
:
779 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
780 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
782 BeginBatchUndo(_("Delete Text"));
784 long newPos
= m_caretPosition
;
786 bool processed
= DeleteSelectedContent(& newPos
);
788 // Submit range in character positions, which are greater than caret positions,
789 // so subtract 1 for deleted character and add 1 for conversion to character position.
794 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
797 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
803 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
808 if (GetLastPosition() == -1)
812 m_caretPosition
= -1;
814 SetDefaultStyleToCursorStyle();
817 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
819 wxRichTextEvent
cmdEvent(
820 wxEVT_COMMAND_RICHTEXT_DELETE
,
822 cmdEvent
.SetEventObject(this);
823 cmdEvent
.SetFlags(flags
);
824 cmdEvent
.SetPosition(m_caretPosition
+1);
825 GetEventHandler()->ProcessEvent(cmdEvent
);
835 // all the other keys modify the controls contents which shouldn't be
836 // possible if we're read-only
843 if (event
.GetKeyCode() == WXK_RETURN
)
845 BeginBatchUndo(_("Insert Text"));
847 long newPos
= m_caretPosition
;
849 DeleteSelectedContent(& newPos
);
851 if (event
.ShiftDown())
854 text
= wxRichTextLineBreakChar
;
855 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
856 m_caretAtLineStart
= true;
860 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
863 SetDefaultStyleToCursorStyle();
865 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
867 wxRichTextEvent
cmdEvent(
868 wxEVT_COMMAND_RICHTEXT_RETURN
,
870 cmdEvent
.SetEventObject(this);
871 cmdEvent
.SetFlags(flags
);
872 cmdEvent
.SetPosition(newPos
+1);
874 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
876 // Generate conventional event
877 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
878 InitCommandEvent(textEvent
);
880 GetEventHandler()->ProcessEvent(textEvent
);
884 else if (event
.GetKeyCode() == WXK_BACK
)
886 BeginBatchUndo(_("Delete Text"));
888 long newPos
= m_caretPosition
;
890 bool processed
= DeleteSelectedContent(& newPos
);
892 // Submit range in character positions, which are greater than caret positions,
893 // so subtract 1 for deleted character and add 1 for conversion to character position.
898 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
901 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this);
907 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this);
912 if (GetLastPosition() == -1)
916 m_caretPosition
= -1;
918 SetDefaultStyleToCursorStyle();
921 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
923 wxRichTextEvent
cmdEvent(
924 wxEVT_COMMAND_RICHTEXT_DELETE
,
926 cmdEvent
.SetEventObject(this);
927 cmdEvent
.SetFlags(flags
);
928 cmdEvent
.SetPosition(m_caretPosition
+1);
929 GetEventHandler()->ProcessEvent(cmdEvent
);
933 else if (event
.GetKeyCode() == WXK_DELETE
)
935 BeginBatchUndo(_("Delete Text"));
937 long newPos
= m_caretPosition
;
939 bool processed
= DeleteSelectedContent(& newPos
);
941 // Submit range in character positions, which are greater than caret positions,
942 if (newPos
< GetBuffer().GetRange().GetEnd()+1)
946 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
947 if (pos
!= -1 && (pos
> newPos
))
949 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this);
955 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this);
960 if (GetLastPosition() == -1)
964 m_caretPosition
= -1;
966 SetDefaultStyleToCursorStyle();
969 wxRichTextEvent
cmdEvent(
970 wxEVT_COMMAND_RICHTEXT_DELETE
,
972 cmdEvent
.SetEventObject(this);
973 cmdEvent
.SetFlags(flags
);
974 cmdEvent
.SetPosition(m_caretPosition
+1);
975 GetEventHandler()->ProcessEvent(cmdEvent
);
981 long keycode
= event
.GetKeyCode();
995 if (event
.CmdDown() || event
.AltDown())
1002 wxRichTextEvent
cmdEvent(
1003 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1005 cmdEvent
.SetEventObject(this);
1006 cmdEvent
.SetFlags(flags
);
1008 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1010 cmdEvent
.SetCharacter((wxChar
) keycode
);
1012 cmdEvent
.SetPosition(m_caretPosition
+1);
1014 if (keycode
== wxT('\t'))
1016 // See if we need to promote or demote the selection or paragraph at the cursor
1017 // position, instead of inserting a tab.
1018 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1019 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
1020 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1022 wxRichTextRange range
;
1024 range
= GetSelectionRange();
1026 range
= para
->GetRange().FromInternal();
1028 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1030 PromoteList(promoteBy
, range
, NULL
);
1032 GetEventHandler()->ProcessEvent(cmdEvent
);
1038 BeginBatchUndo(_("Insert Text"));
1040 long newPos
= m_caretPosition
;
1041 DeleteSelectedContent(& newPos
);
1044 wxString str
= event
.GetUnicodeKey();
1046 wxString str
= (wxChar
) event
.GetKeyCode();
1048 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
1052 SetDefaultStyleToCursorStyle();
1053 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1055 GetEventHandler()->ProcessEvent(cmdEvent
);
1063 /// Delete content if there is a selection, e.g. when pressing a key.
1064 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1068 long pos
= m_selectionRange
.GetStart();
1069 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
1070 m_selectionRange
.SetRange(-2, -2);
1080 /// Keyboard navigation
1084 Left: left one character
1085 Right: right one character
1088 Ctrl-Left: left one word
1089 Ctrl-Right: right one word
1090 Ctrl-Up: previous paragraph start
1091 Ctrl-Down: next start of paragraph
1094 Ctrl-Home: start of document
1095 Ctrl-End: end of document
1096 Page-Up: Up a screen
1097 Page-Down: Down a screen
1101 Ctrl-Alt-PgUp: Start of window
1102 Ctrl-Alt-PgDn: End of window
1103 F8: Start selection mode
1104 Esc: End selection mode
1106 Adding Shift does the above but starts/extends selection.
1111 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1113 bool success
= false;
1115 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1117 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1118 success
= WordRight(1, flags
);
1120 success
= MoveRight(1, flags
);
1122 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1124 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1125 success
= WordLeft(1, flags
);
1127 success
= MoveLeft(1, flags
);
1129 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1131 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1132 success
= MoveToParagraphStart(flags
);
1134 success
= MoveUp(1, flags
);
1136 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1138 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1139 success
= MoveToParagraphEnd(flags
);
1141 success
= MoveDown(1, flags
);
1143 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1145 success
= PageUp(1, flags
);
1147 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1149 success
= PageDown(1, flags
);
1151 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1153 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1154 success
= MoveHome(flags
);
1156 success
= MoveToLineStart(flags
);
1158 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1160 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1161 success
= MoveEnd(flags
);
1163 success
= MoveToLineEnd(flags
);
1168 ScrollIntoView(m_caretPosition
, keyCode
);
1169 SetDefaultStyleToCursorStyle();
1175 /// Extend the selection. Selections are in caret positions.
1176 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1178 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1180 if (oldPos
== newPos
)
1183 wxRichTextRange oldSelection
= m_selectionRange
;
1185 // If not currently selecting, start selecting
1186 if (m_selectionRange
.GetStart() == -2)
1188 m_selectionAnchor
= oldPos
;
1190 if (oldPos
> newPos
)
1191 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1193 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1197 // Always ensure that the selection range start is greater than
1199 if (newPos
> m_selectionAnchor
)
1200 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1201 else if (newPos
== m_selectionAnchor
)
1202 m_selectionRange
= wxRichTextRange(-2, -2);
1204 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1207 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
1209 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1211 wxLogDebug(wxT("Strange selection range"));
1220 /// Scroll into view, returning true if we scrolled.
1221 /// This takes a _caret_ position.
1222 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1224 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1230 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1232 int startXUnits
, startYUnits
;
1233 GetViewStart(& startXUnits
, & startYUnits
);
1234 int startY
= startYUnits
* ppuY
;
1237 GetVirtualSize(& sx
, & sy
);
1243 wxRect rect
= line
->GetRect();
1245 bool scrolled
= false;
1247 wxSize clientSize
= GetClientSize();
1248 clientSize
.y
-= GetBuffer().GetBottomMargin();
1250 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1252 int y
= rect
.y
- GetClientSize().y
/2;
1253 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1254 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1256 if (startYUnits
!= yUnits
)
1258 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1261 #if !wxRICHTEXT_USE_OWN_CARET
1271 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1272 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1273 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1274 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1276 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1278 // Make it scroll so this item is at the bottom
1280 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1281 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1283 // If we're still off the screen, scroll another line down
1284 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1287 if (startYUnits
!= yUnits
)
1289 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1293 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1295 // Make it scroll so this item is at the top
1297 int y
= rect
.y
- GetBuffer().GetTopMargin();
1298 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1300 if (startYUnits
!= yUnits
)
1302 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1308 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1309 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1310 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1311 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1313 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1315 // Make it scroll so this item is at the top
1317 int y
= rect
.y
- GetBuffer().GetTopMargin();
1318 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1320 if (startYUnits
!= yUnits
)
1322 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1326 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1328 // Make it scroll so this item is at the bottom
1330 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1331 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1333 // If we're still off the screen, scroll another line down
1334 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1337 if (startYUnits
!= yUnits
)
1339 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1345 #if !wxRICHTEXT_USE_OWN_CARET
1353 /// Is the given position visible on the screen?
1354 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1356 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1362 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1365 GetViewStart(& startX
, & startY
);
1367 startY
= startY
* ppuY
;
1369 wxRect rect
= line
->GetRect();
1370 wxSize clientSize
= GetClientSize();
1371 clientSize
.y
-= GetBuffer().GetBottomMargin();
1373 return (rect
.GetBottom() > (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1376 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1378 m_caretPosition
= position
;
1379 m_caretAtLineStart
= showAtLineStart
;
1382 /// Move caret one visual step forward: this may mean setting a flag
1383 /// and keeping the same position if we're going from the end of one line
1384 /// to the start of the next, which may be the exact same caret position.
1385 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1387 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1389 // Only do the check if we're not at the end of the paragraph (where things work OK
1391 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1393 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1397 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1399 // We're at the end of a line. See whether we need to
1400 // stay at the same actual caret position but change visual
1401 // position, or not.
1402 if (oldPosition
== lineRange
.GetEnd())
1404 if (m_caretAtLineStart
)
1406 // We're already at the start of the line, so actually move on now.
1407 m_caretPosition
= oldPosition
+ 1;
1408 m_caretAtLineStart
= false;
1412 // We're showing at the end of the line, so keep to
1413 // the same position but indicate that we're to show
1414 // at the start of the next line.
1415 m_caretPosition
= oldPosition
;
1416 m_caretAtLineStart
= true;
1418 SetDefaultStyleToCursorStyle();
1424 SetDefaultStyleToCursorStyle();
1427 /// Move caret one visual step backward: this may mean setting a flag
1428 /// and keeping the same position if we're going from the end of one line
1429 /// to the start of the next, which may be the exact same caret position.
1430 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1432 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1434 // Only do the check if we're not at the start of the paragraph (where things work OK
1436 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1438 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1442 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1444 // We're at the start of a line. See whether we need to
1445 // stay at the same actual caret position but change visual
1446 // position, or not.
1447 if (oldPosition
== lineRange
.GetStart())
1449 m_caretPosition
= oldPosition
-1;
1450 m_caretAtLineStart
= true;
1453 else if (oldPosition
== lineRange
.GetEnd())
1455 if (m_caretAtLineStart
)
1457 // We're at the start of the line, so keep the same caret position
1458 // but clear the start-of-line flag.
1459 m_caretPosition
= oldPosition
;
1460 m_caretAtLineStart
= false;
1464 // We're showing at the end of the line, so go back
1465 // to the previous character position.
1466 m_caretPosition
= oldPosition
- 1;
1468 SetDefaultStyleToCursorStyle();
1474 SetDefaultStyleToCursorStyle();
1478 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1480 long endPos
= GetBuffer().GetRange().GetEnd();
1482 if (m_caretPosition
+ noPositions
< endPos
)
1484 long oldPos
= m_caretPosition
;
1485 long newPos
= m_caretPosition
+ noPositions
;
1487 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1491 // Determine by looking at oldPos and m_caretPosition whether
1492 // we moved from the end of a line to the start of the next line, in which case
1493 // we want to adjust the caret position such that it is positioned at the
1494 // start of the next line, rather than jumping past the first character of the
1496 if (noPositions
== 1 && !extendSel
)
1497 MoveCaretForward(oldPos
);
1499 SetCaretPosition(newPos
);
1502 SetDefaultStyleToCursorStyle();
1511 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1515 if (m_caretPosition
> startPos
- noPositions
+ 1)
1517 long oldPos
= m_caretPosition
;
1518 long newPos
= m_caretPosition
- noPositions
;
1519 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1523 if (noPositions
== 1 && !extendSel
)
1524 MoveCaretBack(oldPos
);
1526 SetCaretPosition(newPos
);
1529 SetDefaultStyleToCursorStyle();
1538 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1540 return MoveDown(- noLines
, flags
);
1544 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1549 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1550 wxPoint pt
= GetCaret()->GetPosition();
1551 long newLine
= lineNumber
+ noLines
;
1553 if (lineNumber
!= -1)
1557 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1559 if (newLine
> lastLine
)
1569 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1572 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1578 wxClientDC
dc(this);
1580 dc
.SetFont(GetFont());
1582 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1584 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1586 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1587 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1588 // so we view the caret at the start of the line.
1589 bool caretLineStart
= false;
1590 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1592 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1593 wxRichTextRange lineRange
;
1595 lineRange
= thisLine
->GetAbsoluteRange();
1597 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1600 caretLineStart
= true;
1604 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1605 if (para
&& para
->GetRange().GetStart() == newPos
)
1610 long newSelEnd
= newPos
;
1612 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1616 SetCaretPosition(newPos
, caretLineStart
);
1618 SetDefaultStyleToCursorStyle();
1626 /// Move to the end of the paragraph
1627 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1629 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1632 long newPos
= para
->GetRange().GetEnd() - 1;
1633 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1637 SetCaretPosition(newPos
);
1639 SetDefaultStyleToCursorStyle();
1647 /// Move to the start of the paragraph
1648 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1650 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1653 long newPos
= para
->GetRange().GetStart() - 1;
1654 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1658 SetCaretPosition(newPos
);
1660 SetDefaultStyleToCursorStyle();
1668 /// Move to the end of the line
1669 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1671 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1675 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1676 long newPos
= lineRange
.GetEnd();
1677 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1681 SetCaretPosition(newPos
);
1683 SetDefaultStyleToCursorStyle();
1691 /// Move to the start of the line
1692 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1694 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1697 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1698 long newPos
= lineRange
.GetStart()-1;
1700 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1704 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1706 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1708 SetDefaultStyleToCursorStyle();
1716 /// Move to the start of the buffer
1717 bool wxRichTextCtrl::MoveHome(int flags
)
1719 if (m_caretPosition
!= -1)
1721 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1725 SetCaretPosition(-1);
1727 SetDefaultStyleToCursorStyle();
1735 /// Move to the end of the buffer
1736 bool wxRichTextCtrl::MoveEnd(int flags
)
1738 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1740 if (m_caretPosition
!= endPos
)
1742 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1746 SetCaretPosition(endPos
);
1748 SetDefaultStyleToCursorStyle();
1756 /// Move noPages pages up
1757 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1759 return PageDown(- noPages
, flags
);
1762 /// Move noPages pages down
1763 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1765 // Calculate which line occurs noPages * screen height further down.
1766 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1769 wxSize clientSize
= GetClientSize();
1770 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1772 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1775 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1776 long pos
= lineRange
.GetStart()-1;
1777 if (pos
!= m_caretPosition
)
1779 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1781 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1785 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1787 SetDefaultStyleToCursorStyle();
1797 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1799 return str
== wxT(" ") || str
== wxT("\t");
1802 // Finds the caret position for the next word
1803 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1805 long endPos
= GetBuffer().GetRange().GetEnd();
1809 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1811 // First skip current text to space
1812 while (i
< endPos
&& i
> -1)
1814 // i is in character, not caret positions
1815 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1816 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1817 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1821 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1828 while (i
< endPos
&& i
> -1)
1830 // i is in character, not caret positions
1831 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1832 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1833 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1834 return wxMax(-1, i
);
1836 if (text
.empty()) // End of paragraph, or maybe an image
1837 return wxMax(-1, i
- 1);
1838 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1842 // Convert to caret position
1843 return wxMax(-1, i
- 1);
1852 long i
= m_caretPosition
;
1854 // First skip white space
1855 while (i
< endPos
&& i
> -1)
1857 // i is in character, not caret positions
1858 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1859 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1861 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1863 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1868 // Next skip current text to space
1869 while (i
< endPos
&& i
> -1)
1871 // i is in character, not caret positions
1872 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1873 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1874 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1877 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1890 /// Move n words left
1891 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1893 long pos
= FindNextWordPosition(-1);
1894 if (pos
!= m_caretPosition
)
1896 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1898 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1902 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1904 SetDefaultStyleToCursorStyle();
1912 /// Move n words right
1913 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1915 long pos
= FindNextWordPosition(1);
1916 if (pos
!= m_caretPosition
)
1918 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1920 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1924 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1926 SetDefaultStyleToCursorStyle();
1935 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1937 // Only do sizing optimization for large buffers
1938 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1940 m_fullLayoutRequired
= true;
1941 m_fullLayoutTime
= wxGetLocalTimeMillis();
1942 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1943 LayoutContent(true /* onlyVisibleRect */);
1946 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1948 #if wxRICHTEXT_BUFFERED_PAINTING
1956 /// Idle-time processing
1957 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1959 #if wxRICHTEXT_USE_OWN_CARET
1960 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1962 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
1968 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1970 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1972 m_fullLayoutRequired
= false;
1973 m_fullLayoutTime
= 0;
1974 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1975 ShowPosition(m_fullLayoutSavedPosition
);
1979 if (m_caretPositionForDefaultStyle
!= -2)
1981 // If the caret position has changed, no longer reflect the default style
1983 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1984 m_caretPositionForDefaultStyle
= -2;
1991 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1993 #if wxRICHTEXT_USE_OWN_CARET
1994 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
1997 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2004 /// Set up scrollbars, e.g. after a resize
2005 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2010 if (GetBuffer().IsEmpty())
2012 SetScrollbars(0, 0, 0, 0, 0, 0);
2016 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2017 // of pixels. See e.g. wxVScrolledWindow for ideas.
2018 int pixelsPerUnit
= 5;
2019 wxSize clientSize
= GetClientSize();
2021 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2023 // Round up so we have at least maxHeight pixels
2024 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2026 int startX
= 0, startY
= 0;
2028 GetViewStart(& startX
, & startY
);
2030 int maxPositionX
= 0;
2031 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2033 int newStartX
= wxMin(maxPositionX
, startX
);
2034 int newStartY
= wxMin(maxPositionY
, startY
);
2036 int oldPPUX
, oldPPUY
;
2037 int oldStartX
, oldStartY
;
2038 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2039 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2040 GetViewStart(& oldStartX
, & oldStartY
);
2041 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2043 oldVirtualSizeY
/= oldPPUY
;
2045 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2048 // Don't set scrollbars if there were none before, and there will be none now.
2049 if (oldPPUY
!= 0 && (oldVirtualSizeY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2052 // Move to previous scroll position if
2054 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2057 /// Paint the background
2058 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2060 wxColour backgroundColour
= GetBackgroundColour();
2061 if (!backgroundColour
.Ok())
2062 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2064 // Clear the background
2065 dc
.SetBrush(wxBrush(backgroundColour
));
2066 dc
.SetPen(*wxTRANSPARENT_PEN
);
2067 wxRect
windowRect(GetClientSize());
2068 windowRect
.x
-= 2; windowRect
.y
-= 2;
2069 windowRect
.width
+= 4; windowRect
.height
+= 4;
2071 // We need to shift the rectangle to take into account
2072 // scrolling. Converting device to logical coordinates.
2073 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2074 dc
.DrawRectangle(windowRect
);
2077 #if wxRICHTEXT_BUFFERED_PAINTING
2078 /// Recreate buffer bitmap if necessary
2079 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2082 if (sz
== wxDefaultSize
)
2083 sz
= GetClientSize();
2085 if (sz
.x
< 1 || sz
.y
< 1)
2088 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2089 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2090 return m_bufferBitmap
.Ok();
2094 // ----------------------------------------------------------------------------
2095 // file IO functions
2096 // ----------------------------------------------------------------------------
2098 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2100 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2102 m_filename
= filename
;
2105 SetInsertionPoint(0);
2108 SetupScrollbars(true);
2110 wxTextCtrl::SendTextUpdatedEvent(this);
2116 wxLogError(_("File couldn't be loaded."));
2122 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2124 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2126 m_filename
= filename
;
2133 wxLogError(_("The text couldn't be saved."));
2138 // ----------------------------------------------------------------------------
2139 // wxRichTextCtrl specific functionality
2140 // ----------------------------------------------------------------------------
2142 /// Add a new paragraph of text to the end of the buffer
2143 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2145 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
2151 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2153 wxRichTextRange range
= GetBuffer().AddImage(image
);
2158 // ----------------------------------------------------------------------------
2159 // selection and ranges
2160 // ----------------------------------------------------------------------------
2162 void wxRichTextCtrl::SelectAll()
2164 SetSelection(0, GetLastPosition()+1);
2165 m_selectionAnchor
= -1;
2169 void wxRichTextCtrl::SelectNone()
2171 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2173 wxRichTextRange oldSelection
= m_selectionRange
;
2175 m_selectionRange
= wxRichTextRange(-2, -2);
2177 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2179 m_selectionAnchor
= -2;
2182 static bool wxIsWordDelimiter(const wxString
& text
)
2184 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2187 /// Select the word at the given character position
2188 bool wxRichTextCtrl::SelectWord(long position
)
2190 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
2193 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
2197 if (position
== para
->GetRange().GetEnd())
2200 long positionStart
= position
;
2201 long positionEnd
= position
;
2203 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2205 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2206 if (wxIsWordDelimiter(text
))
2212 if (positionStart
< para
->GetRange().GetStart())
2213 positionStart
= para
->GetRange().GetStart();
2215 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2217 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2218 if (wxIsWordDelimiter(text
))
2224 if (positionEnd
>= para
->GetRange().GetEnd())
2225 positionEnd
= para
->GetRange().GetEnd();
2227 if (positionEnd
< positionStart
)
2230 SetSelection(positionStart
, positionEnd
+1);
2232 if (positionStart
>= 0)
2234 MoveCaret(positionStart
-1, true);
2235 SetDefaultStyleToCursorStyle();
2241 wxString
wxRichTextCtrl::GetStringSelection() const
2244 GetSelection(&from
, &to
);
2246 return GetRange(from
, to
);
2249 // ----------------------------------------------------------------------------
2251 // ----------------------------------------------------------------------------
2253 wxTextCtrlHitTestResult
2254 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2256 // implement in terms of the other overload as the native ports typically
2257 // can get the position and not (x, y) pair directly (although wxUniv
2258 // directly gets x and y -- and so overrides this method as well)
2260 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2262 if ( rc
!= wxTE_HT_UNKNOWN
)
2264 PositionToXY(pos
, x
, y
);
2270 wxTextCtrlHitTestResult
2271 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2274 wxClientDC
dc((wxRichTextCtrl
*) this);
2275 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2277 // Buffer uses logical position (relative to start of buffer)
2279 wxPoint pt2
= GetLogicalPoint(pt
);
2281 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2283 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2284 return wxTE_HT_BEFORE
;
2285 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2286 return wxTE_HT_BEYOND
;
2287 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2288 return wxTE_HT_ON_TEXT
;
2290 return wxTE_HT_UNKNOWN
;
2293 // ----------------------------------------------------------------------------
2294 // set/get the controls text
2295 // ----------------------------------------------------------------------------
2297 wxString
wxRichTextCtrl::GetValue() const
2299 return GetBuffer().GetText();
2302 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2304 // Public API for range is different from internals
2305 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2308 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2310 // Don't call Clear here, since it always sends a text updated event
2311 m_buffer
.ResetAndClearCommands();
2312 m_buffer
.SetDirty(true);
2313 m_caretPosition
= -1;
2314 m_caretPositionForDefaultStyle
= -2;
2315 m_caretAtLineStart
= false;
2316 m_selectionRange
.SetRange(-2, -2);
2326 if (!value
.IsEmpty())
2328 // Remove empty paragraph
2329 GetBuffer().Clear();
2330 DoWriteText(value
, flags
);
2332 // for compatibility, don't move the cursor when doing SetValue()
2333 SetInsertionPoint(0);
2337 // still send an event for consistency
2338 if (flags
& SetValue_SendEvent
)
2339 wxTextCtrl::SendTextUpdatedEvent(this);
2344 void wxRichTextCtrl::WriteText(const wxString
& value
)
2349 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2351 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2353 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2355 if ( flags
& SetValue_SendEvent
)
2356 wxTextCtrl::SendTextUpdatedEvent(this);
2359 void wxRichTextCtrl::AppendText(const wxString
& text
)
2361 SetInsertionPointEnd();
2366 /// Write an image at the current insertion point
2367 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
)
2369 wxRichTextImageBlock imageBlock
;
2371 wxImage image2
= image
;
2372 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2373 return WriteImage(imageBlock
);
2378 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
)
2380 wxRichTextImageBlock imageBlock
;
2383 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2384 return WriteImage(imageBlock
);
2389 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2391 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2394 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
)
2398 wxRichTextImageBlock imageBlock
;
2400 wxImage image
= bitmap
.ConvertToImage();
2401 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2402 return WriteImage(imageBlock
);
2408 /// Insert a newline (actually paragraph) at the current insertion point.
2409 bool wxRichTextCtrl::Newline()
2411 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2414 /// Insert a line break at the current insertion point.
2415 bool wxRichTextCtrl::LineBreak()
2418 text
= wxRichTextLineBreakChar
;
2419 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2422 // ----------------------------------------------------------------------------
2423 // Clipboard operations
2424 // ----------------------------------------------------------------------------
2426 void wxRichTextCtrl::Copy()
2430 wxRichTextRange range
= GetInternalSelectionRange();
2431 GetBuffer().CopyToClipboard(range
);
2435 void wxRichTextCtrl::Cut()
2439 wxRichTextRange range
= GetInternalSelectionRange();
2440 GetBuffer().CopyToClipboard(range
);
2442 DeleteSelectedContent();
2448 void wxRichTextCtrl::Paste()
2452 BeginBatchUndo(_("Paste"));
2454 long newPos
= m_caretPosition
;
2455 DeleteSelectedContent(& newPos
);
2457 GetBuffer().PasteFromClipboard(newPos
);
2463 void wxRichTextCtrl::DeleteSelection()
2465 if (CanDeleteSelection())
2467 DeleteSelectedContent();
2471 bool wxRichTextCtrl::HasSelection() const
2473 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2476 bool wxRichTextCtrl::CanCopy() const
2478 // Can copy if there's a selection
2479 return HasSelection();
2482 bool wxRichTextCtrl::CanCut() const
2484 return HasSelection() && IsEditable();
2487 bool wxRichTextCtrl::CanPaste() const
2489 if ( !IsEditable() )
2492 return GetBuffer().CanPasteFromClipboard();
2495 bool wxRichTextCtrl::CanDeleteSelection() const
2497 return HasSelection() && IsEditable();
2501 // ----------------------------------------------------------------------------
2503 // ----------------------------------------------------------------------------
2505 void wxRichTextCtrl::SetEditable(bool editable
)
2507 m_editable
= editable
;
2510 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2514 m_caretPosition
= pos
- 1;
2519 void wxRichTextCtrl::SetInsertionPointEnd()
2521 long pos
= GetLastPosition();
2522 SetInsertionPoint(pos
);
2525 long wxRichTextCtrl::GetInsertionPoint() const
2527 return m_caretPosition
+1;
2530 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2532 return GetBuffer().GetRange().GetEnd();
2535 // If the return values from and to are the same, there is no
2537 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2539 *from
= m_selectionRange
.GetStart();
2540 *to
= m_selectionRange
.GetEnd();
2541 if ((*to
) != -1 && (*to
) != -2)
2545 bool wxRichTextCtrl::IsEditable() const
2550 // ----------------------------------------------------------------------------
2552 // ----------------------------------------------------------------------------
2554 void wxRichTextCtrl::SetSelection(long from
, long to
)
2556 // if from and to are both -1, it means (in wxWidgets) that all text should
2558 if ( (from
== -1) && (to
== -1) )
2561 to
= GetLastPosition()+1;
2564 DoSetSelection(from
, to
);
2567 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2575 wxRichTextRange oldSelection
= m_selectionRange
;
2576 m_selectionAnchor
= from
;
2577 m_selectionRange
.SetRange(from
, to
-1);
2579 m_caretPosition
= from
-1;
2581 RefreshForSelectionChange(oldSelection
, m_selectionRange
);
2586 // ----------------------------------------------------------------------------
2588 // ----------------------------------------------------------------------------
2590 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2591 const wxString
& value
)
2593 BeginBatchUndo(_("Replace"));
2595 DeleteSelectedContent();
2597 DoWriteText(value
, SetValue_SelectionOnly
);
2602 void wxRichTextCtrl::Remove(long from
, long to
)
2606 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2613 bool wxRichTextCtrl::IsModified() const
2615 return m_buffer
.IsModified();
2618 void wxRichTextCtrl::MarkDirty()
2620 m_buffer
.Modify(true);
2623 void wxRichTextCtrl::DiscardEdits()
2625 m_caretPositionForDefaultStyle
= -2;
2626 m_buffer
.Modify(false);
2627 m_buffer
.GetCommandProcessor()->ClearCommands();
2630 int wxRichTextCtrl::GetNumberOfLines() const
2632 return GetBuffer().GetParagraphCount();
2635 // ----------------------------------------------------------------------------
2636 // Positions <-> coords
2637 // ----------------------------------------------------------------------------
2639 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2641 return GetBuffer().XYToPosition(x
, y
);
2644 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2646 return GetBuffer().PositionToXY(pos
, x
, y
);
2649 // ----------------------------------------------------------------------------
2651 // ----------------------------------------------------------------------------
2653 void wxRichTextCtrl::ShowPosition(long pos
)
2655 if (!IsPositionVisible(pos
))
2656 ScrollIntoView(pos
-1, WXK_DOWN
);
2659 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2661 return GetBuffer().GetParagraphLength(lineNo
);
2664 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2666 return GetBuffer().GetParagraphText(lineNo
);
2669 // ----------------------------------------------------------------------------
2671 // ----------------------------------------------------------------------------
2673 void wxRichTextCtrl::Undo()
2677 GetCommandProcessor()->Undo();
2681 void wxRichTextCtrl::Redo()
2685 GetCommandProcessor()->Redo();
2689 bool wxRichTextCtrl::CanUndo() const
2691 return GetCommandProcessor()->CanUndo();
2694 bool wxRichTextCtrl::CanRedo() const
2696 return GetCommandProcessor()->CanRedo();
2699 // ----------------------------------------------------------------------------
2700 // implementation details
2701 // ----------------------------------------------------------------------------
2703 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2705 SetValue(event
.GetString());
2706 GetEventHandler()->ProcessEvent(event
);
2709 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2711 // By default, load the first file into the text window.
2712 if (event
.GetNumberOfFiles() > 0)
2714 LoadFile(event
.GetFiles()[0]);
2718 wxSize
wxRichTextCtrl::DoGetBestSize() const
2720 return wxSize(10, 10);
2723 // ----------------------------------------------------------------------------
2724 // standard handlers for standard edit menu events
2725 // ----------------------------------------------------------------------------
2727 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2732 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2737 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2742 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2747 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2752 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2757 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2759 event
.Enable( CanCut() );
2762 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2764 event
.Enable( CanCopy() );
2767 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2769 event
.Enable( CanDeleteSelection() );
2772 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2774 event
.Enable( CanPaste() );
2777 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2779 event
.Enable( CanUndo() );
2780 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2783 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2785 event
.Enable( CanRedo() );
2786 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2789 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2794 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2796 event
.Enable(GetLastPosition() > 0);
2799 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2801 if (event
.GetEventObject() != this)
2809 m_contextMenu
= new wxMenu
;
2810 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2811 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2812 m_contextMenu
->AppendSeparator();
2813 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2814 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2815 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2816 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2817 m_contextMenu
->AppendSeparator();
2818 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2820 PopupMenu(m_contextMenu
);
2824 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2826 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2829 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2831 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2834 // extended style setting operation with flags including:
2835 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2836 // see richtextbuffer.h for more details.
2838 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2840 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2843 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2845 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2848 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2850 return GetBuffer().GetDefaultStyle();
2853 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2855 return GetBuffer().GetStyle(position
, style
);
2858 // get the common set of styles for the range
2859 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2861 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2864 /// Get the content (uncombined) attributes for this position.
2865 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2867 return GetBuffer().GetUncombinedStyle(position
, style
);
2870 /// Set font, and also the buffer attributes
2871 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2873 wxControl::SetFont(font
);
2875 wxTextAttr attr
= GetBuffer().GetAttributes();
2877 GetBuffer().SetBasicStyle(attr
);
2879 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2885 /// Transform logical to physical
2886 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2889 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2894 /// Transform physical to logical
2895 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2898 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2903 /// Position the caret
2904 void wxRichTextCtrl::PositionCaret()
2909 //wxLogDebug(wxT("PositionCaret"));
2912 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2914 wxPoint newPt
= caretRect
.GetPosition();
2915 wxSize newSz
= caretRect
.GetSize();
2916 wxPoint pt
= GetPhysicalPoint(newPt
);
2917 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2920 if (GetCaret()->GetSize() != newSz
)
2921 GetCaret()->SetSize(newSz
);
2923 int halfSize
= newSz
.y
/2;
2924 // If the caret is beyond the margin, hide it by moving it out of the way
2925 if (((pt
.y
+ halfSize
) < GetBuffer().GetTopMargin()) || ((pt
.y
+ halfSize
) > (GetClientSize().y
- GetBuffer().GetBottomMargin())))
2928 GetCaret()->Move(pt
);
2934 /// Get the caret height and position for the given character position
2935 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2937 wxClientDC
dc(this);
2938 dc
.SetFont(GetFont());
2945 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2947 // Caret height can't be zero
2949 height
= dc
.GetCharHeight();
2951 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2958 /// Gets the line for the visible caret position. If the caret is
2959 /// shown at the very end of the line, it means the next character is actually
2960 /// on the following line. So let's get the line we're expecting to find
2961 /// if this is the case.
2962 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2964 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2965 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2968 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2969 if (caretPosition
== lineRange
.GetStart()-1 &&
2970 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2972 if (!m_caretAtLineStart
)
2973 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2980 /// Move the caret to the given character position
2981 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2983 if (GetBuffer().GetDirty())
2986 if (pos
<= GetBuffer().GetRange().GetEnd())
2988 SetCaretPosition(pos
, showAtLineStart
);
2998 /// Layout the buffer: which we must do before certain operations, such as
2999 /// setting the caret position.
3000 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3002 if (GetBuffer().GetDirty() || onlyVisibleRect
)
3004 wxRect
availableSpace(GetClientSize());
3005 if (availableSpace
.width
== 0)
3006 availableSpace
.width
= 10;
3007 if (availableSpace
.height
== 0)
3008 availableSpace
.height
= 10;
3010 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3011 if (onlyVisibleRect
)
3013 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3014 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3017 wxClientDC
dc(this);
3018 dc
.SetFont(GetFont());
3022 GetBuffer().Defragment();
3023 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3024 GetBuffer().Layout(dc
, availableSpace
, flags
);
3025 GetBuffer().SetDirty(false);
3034 /// Is all of the selection bold?
3035 bool wxRichTextCtrl::IsSelectionBold()
3040 wxRichTextRange range
= GetSelectionRange();
3041 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3042 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3044 return HasCharacterAttributes(range
, attr
);
3048 // If no selection, then we need to combine current style with default style
3049 // to see what the effect would be if we started typing.
3051 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3053 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3054 if (GetStyle(pos
, attr
))
3056 if (IsDefaultStyleShowing())
3057 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3058 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3064 /// Is all of the selection italics?
3065 bool wxRichTextCtrl::IsSelectionItalics()
3069 wxRichTextRange range
= GetSelectionRange();
3071 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3072 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3074 return HasCharacterAttributes(range
, attr
);
3078 // If no selection, then we need to combine current style with default style
3079 // to see what the effect would be if we started typing.
3081 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3083 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3084 if (GetStyle(pos
, attr
))
3086 if (IsDefaultStyleShowing())
3087 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3088 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3094 /// Is all of the selection underlined?
3095 bool wxRichTextCtrl::IsSelectionUnderlined()
3099 wxRichTextRange range
= GetSelectionRange();
3101 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3102 attr
.SetFontUnderlined(true);
3104 return HasCharacterAttributes(range
, attr
);
3108 // If no selection, then we need to combine current style with default style
3109 // to see what the effect would be if we started typing.
3111 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3112 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3114 if (GetStyle(pos
, attr
))
3116 if (IsDefaultStyleShowing())
3117 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3118 return attr
.GetFontUnderlined();
3124 /// Apply bold to the selection
3125 bool wxRichTextCtrl::ApplyBoldToSelection()
3128 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3129 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3132 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3135 wxRichTextAttr current
= GetDefaultStyleEx();
3136 current
.Apply(attr
);
3137 SetAndShowDefaultStyle(current
);
3142 /// Apply italic to the selection
3143 bool wxRichTextCtrl::ApplyItalicToSelection()
3146 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3147 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3150 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3153 wxRichTextAttr current
= GetDefaultStyleEx();
3154 current
.Apply(attr
);
3155 SetAndShowDefaultStyle(current
);
3160 /// Apply underline to the selection
3161 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3164 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3165 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3168 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3171 wxRichTextAttr current
= GetDefaultStyleEx();
3172 current
.Apply(attr
);
3173 SetAndShowDefaultStyle(current
);
3178 /// Is all of the selection aligned according to the specified flag?
3179 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3181 wxRichTextRange range
;
3183 range
= GetSelectionRange();
3185 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3188 attr
.SetAlignment(alignment
);
3190 return HasParagraphAttributes(range
, attr
);
3193 /// Apply alignment to the selection
3194 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3197 attr
.SetAlignment(alignment
);
3199 return SetStyle(GetSelectionRange(), attr
);
3202 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3204 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3209 /// Apply a named style to the selection
3210 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3212 // Flags are defined within each definition, so only certain
3213 // attributes are applied.
3214 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3216 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3218 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3220 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3222 wxRichTextRange range
;
3225 range
= GetSelectionRange();
3228 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3229 range
= wxRichTextRange(pos
, pos
+1);
3232 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3235 // Make sure the attr has the style name
3236 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3238 attr
.SetParagraphStyleName(def
->GetName());
3240 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3241 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3242 // to change its style independently.
3243 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3246 attr
.SetCharacterStyleName(def
->GetName());
3249 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3252 wxRichTextAttr current
= GetDefaultStyleEx();
3253 current
.Apply(attr
);
3254 SetAndShowDefaultStyle(current
);
3259 /// Apply the style sheet to the buffer, for example if the styles have changed.
3260 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3263 styleSheet
= GetBuffer().GetStyleSheet();
3267 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3269 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3277 /// Sets the default style to the style under the cursor
3278 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3281 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3283 // If at the start of a paragraph, use the next position.
3284 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3286 if (GetUncombinedStyle(pos
, attr
))
3288 SetDefaultStyle(attr
);
3295 /// Returns the first visible position in the current view
3296 long wxRichTextCtrl::GetFirstVisiblePosition() const
3298 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3300 return line
->GetAbsoluteRange().GetStart();
3305 /// Get the first visible point in the window
3306 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3309 int startXUnits
, startYUnits
;
3311 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3312 GetViewStart(& startXUnits
, & startYUnits
);
3314 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3317 /// The adjusted caret position is the character position adjusted to take
3318 /// into account whether we're at the start of a paragraph, in which case
3319 /// style information should be taken from the next position, not current one.
3320 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3322 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3324 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3329 /// Get/set the selection range in character positions. -1, -1 means no selection.
3330 /// The range is in API convention, i.e. a single character selection is denoted
3332 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3334 wxRichTextRange range
= GetInternalSelectionRange();
3335 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3336 range
.SetEnd(range
.GetEnd() + 1);
3340 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3342 wxRichTextRange
range1(range
);
3343 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3344 range1
.SetEnd(range1
.GetEnd() - 1);
3346 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3348 SetInternalSelectionRange(range1
);
3352 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3354 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3357 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3359 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3362 /// Clear list for given range
3363 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3365 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3368 /// Number/renumber any list elements in the given range
3369 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3371 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3374 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3376 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3379 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3380 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3382 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3385 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3387 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3390 /// Deletes the content in the given range
3391 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3393 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3396 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3398 if (sm_availableFontNames
.GetCount() == 0)
3400 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3401 sm_availableFontNames
.Sort();
3403 return sm_availableFontNames
;
3406 void wxRichTextCtrl::ClearAvailableFontNames()
3408 sm_availableFontNames
.Clear();
3411 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3413 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3415 wxTextAttrEx basicStyle
= GetBasicStyle();
3416 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3417 SetBasicStyle(basicStyle
);
3418 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3423 // Refresh the area affected by a selection change
3424 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange
& oldSelection
, const wxRichTextRange
& newSelection
)
3426 // Calculate the refresh rectangle - just the affected lines
3427 long firstPos
, lastPos
;
3428 if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() != -2)
3430 firstPos
= newSelection
.GetStart();
3431 lastPos
= newSelection
.GetEnd();
3433 else if (oldSelection
.GetStart() != -2 && newSelection
.GetStart() == -2)
3435 firstPos
= oldSelection
.GetStart();
3436 lastPos
= oldSelection
.GetEnd();
3438 else if (oldSelection
.GetStart() == -2 && newSelection
.GetStart() == -2)
3444 firstPos
= wxMin(oldSelection
.GetStart(), newSelection
.GetStart());
3445 lastPos
= wxMax(oldSelection
.GetEnd(), newSelection
.GetEnd());
3448 wxRichTextLine
* firstLine
= GetBuffer().GetLineAtPosition(firstPos
);
3449 wxRichTextLine
* lastLine
= GetBuffer().GetLineAtPosition(lastPos
);
3451 if (firstLine
&& lastLine
)
3453 wxSize clientSize
= GetClientSize();
3454 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3455 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3458 pt1
.y
= wxMax(0, pt1
.y
);
3460 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3462 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3463 RefreshRect(rect
, false);
3471 #if wxRICHTEXT_USE_OWN_CARET
3473 // ----------------------------------------------------------------------------
3474 // initialization and destruction
3475 // ----------------------------------------------------------------------------
3477 void wxRichTextCaret::Init()
3483 m_richTextCtrl
= NULL
;
3484 m_needsUpdate
= false;
3487 wxRichTextCaret::~wxRichTextCaret()
3491 // ----------------------------------------------------------------------------
3492 // showing/hiding/moving the caret (base class interface)
3493 // ----------------------------------------------------------------------------
3495 void wxRichTextCaret::DoShow()
3500 void wxRichTextCaret::DoHide()
3505 void wxRichTextCaret::DoMove()
3511 if (m_xOld
!= -1 && m_yOld
!= -1)
3515 wxRect
rect(GetPosition(), GetSize());
3516 m_richTextCtrl
->RefreshRect(rect
, false);
3525 void wxRichTextCaret::DoSize()
3527 int countVisible
= m_countVisible
;
3528 if (countVisible
> 0)
3534 if (countVisible
> 0)
3536 m_countVisible
= countVisible
;
3541 // ----------------------------------------------------------------------------
3542 // handling the focus
3543 // ----------------------------------------------------------------------------
3545 void wxRichTextCaret::OnSetFocus()
3553 void wxRichTextCaret::OnKillFocus()
3558 // ----------------------------------------------------------------------------
3559 // drawing the caret
3560 // ----------------------------------------------------------------------------
3562 void wxRichTextCaret::Refresh()
3566 wxRect
rect(GetPosition(), GetSize());
3567 m_richTextCtrl
->RefreshRect(rect
, false);
3571 void wxRichTextCaret::DoDraw(wxDC
*dc
)
3573 dc
->SetPen( *wxBLACK_PEN
);
3575 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
3576 dc
->SetPen(*wxBLACK_PEN
);
3578 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3579 // done under wxGTK - no idea why
3580 //dc->SetLogicalFunction(wxINVERT);
3582 wxPoint
pt(m_x
, m_y
);
3586 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
3588 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
3591 // wxRICHTEXT_USE_OWN_CARET