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"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
)
58 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
60 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
62 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
63 EVT_PAINT(wxRichTextCtrl::OnPaint
)
64 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
65 EVT_IDLE(wxRichTextCtrl::OnIdle
)
66 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
67 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
68 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
69 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
70 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
71 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
72 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
73 EVT_CHAR(wxRichTextCtrl::OnChar
)
74 EVT_SIZE(wxRichTextCtrl::OnSize
)
75 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
76 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
77 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
78 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
80 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
81 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
83 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
84 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
86 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
87 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
89 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
90 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
92 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
93 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
95 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
96 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
98 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
99 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
106 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
108 wxRichTextCtrl::wxRichTextCtrl()
109 : wxScrollHelper(this)
114 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
116 const wxString
& value
,
120 const wxValidator
& validator
,
121 const wxString
& name
)
122 : wxScrollHelper(this)
125 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
129 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
130 const wxValidator
& validator
, const wxString
& name
)
132 // Try to make this only necessary in 2.8
135 if ((style
& wxBORDER_MASK
) == wxDEFAULT
)
136 style
|= wxBORDER_THEME
;
140 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
141 style
|wxFULL_REPAINT_ON_RESIZE
,
147 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
151 GetBuffer().SetRichTextCtrl(this);
153 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
156 if (style
& wxTE_READONLY
)
159 // The base attributes must all have default values
160 wxTextAttrEx attributes
;
161 attributes
.SetFont(GetFont());
162 attributes
.SetTextColour(*wxBLACK
);
163 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
164 attributes
.SetLineSpacing(10);
165 attributes
.SetParagraphSpacingAfter(10);
166 attributes
.SetParagraphSpacingBefore(0);
167 attributes
.SetTextEffects(0);
168 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
170 SetBasicStyle(attributes
);
172 // The default attributes will be merged with base attributes, so
173 // can be empty to begin with
174 wxTextAttrEx defaultAttributes
;
175 SetDefaultStyle(defaultAttributes
);
177 SetBackgroundColour(*wxWHITE
);
178 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
180 // Tell the sizers to use the given or best size
181 SetInitialSize(size
);
183 #if wxRICHTEXT_BUFFERED_PAINTING
185 RecreateBuffer(size
);
188 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
189 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
191 SetCursor(m_textCursor
);
193 if (!value
.IsEmpty())
196 GetBuffer().AddEventHandler(this);
201 wxRichTextCtrl::~wxRichTextCtrl()
203 GetBuffer().RemoveEventHandler(this);
205 delete m_contextMenu
;
208 /// Member initialisation
209 void wxRichTextCtrl::Init()
212 m_contextMenu
= NULL
;
214 m_caretPosition
= -1;
215 m_selectionRange
.SetRange(-2, -2);
216 m_selectionAnchor
= -2;
218 m_caretAtLineStart
= false;
220 m_fullLayoutRequired
= false;
221 m_fullLayoutTime
= 0;
222 m_fullLayoutSavedPosition
= 0;
223 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
224 m_caretPositionForDefaultStyle
= -2;
227 /// Call Freeze to prevent refresh
228 void wxRichTextCtrl::Freeze()
233 /// Call Thaw to refresh
234 void wxRichTextCtrl::Thaw()
238 if (m_freezeCount
== 0)
246 void wxRichTextCtrl::Clear()
248 m_buffer
.ResetAndClearCommands();
249 m_buffer
.SetDirty(true);
250 m_caretPosition
= -1;
251 m_caretPositionForDefaultStyle
= -2;
252 m_caretAtLineStart
= false;
253 m_selectionRange
.SetRange(-2, -2);
255 SetScrollbars(0, 0, 0, 0, 0, 0);
257 if (m_freezeCount
== 0)
262 SendTextUpdatedEvent();
266 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
268 if (GetCaret() && GetCaret()->IsVisible())
272 #if wxRICHTEXT_BUFFERED_PAINTING
273 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
279 if (m_freezeCount
> 0)
282 dc
.SetFont(GetFont());
284 // Paint the background
287 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
289 wxRect
drawingArea(GetUpdateRegion().GetBox());
290 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
292 wxRect
availableSpace(GetClientSize());
293 if (GetBuffer().GetDirty())
295 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
296 GetBuffer().SetDirty(false);
300 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
303 if (GetCaret() && !GetCaret()->IsVisible())
309 // Empty implementation, to prevent flicker
310 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
314 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
318 if (!GetCaret()->IsVisible())
327 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
329 if (GetCaret() && GetCaret()->IsVisible())
336 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
342 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
348 dc
.SetFont(GetFont());
351 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
353 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
355 m_dragStart
= event
.GetLogicalPosition(dc
);
361 bool caretAtLineStart
= false;
363 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
365 // If we're at the start of a line (but not first in para)
366 // then we should keep the caret showing at the start of the line
367 // by showing the m_caretAtLineStart flag.
368 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
369 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
371 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
372 caretAtLineStart
= true;
376 MoveCaret(position
, caretAtLineStart
);
377 SetDefaultStyleToCursorStyle();
384 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
389 if (GetCapture() == this)
392 // See if we clicked on a URL
395 dc
.SetFont(GetFont());
398 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
399 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
401 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
403 wxRichTextEvent
cmdEvent(
404 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
406 cmdEvent
.SetEventObject(this);
407 cmdEvent
.SetPosition(m_caretPosition
+1);
409 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
412 if (GetStyle(position
, attr
))
414 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
416 wxString urlTarget
= attr
.GetURL();
417 if (!urlTarget
.IsEmpty())
419 wxMouseEvent
mouseEvent(event
);
421 long startPos
= 0, endPos
= 0;
422 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
425 startPos
= obj
->GetRange().GetStart();
426 endPos
= obj
->GetRange().GetEnd();
429 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
430 InitCommandEvent(urlEvent
);
432 urlEvent
.SetString(urlTarget
);
434 GetEventHandler()->ProcessEvent(urlEvent
);
444 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
448 dc
.SetFont(GetFont());
451 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
452 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
454 // See if we need to change the cursor
457 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
460 if (GetStyle(position
, attr
))
462 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
464 SetCursor(m_urlCursor
);
466 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
468 SetCursor(m_textCursor
);
473 SetCursor(m_textCursor
);
476 if (!event
.Dragging())
482 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
484 // TODO: test closeness
486 bool caretAtLineStart
= false;
488 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
490 // If we're at the start of a line (but not first in para)
491 // then we should keep the caret showing at the start of the line
492 // by showing the m_caretAtLineStart flag.
493 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
494 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
496 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
497 caretAtLineStart
= true;
501 if (m_caretPosition
!= position
)
503 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
505 MoveCaret(position
, caretAtLineStart
);
506 SetDefaultStyleToCursorStyle();
515 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& WXUNUSED(event
))
519 wxRichTextEvent
cmdEvent(
520 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
522 cmdEvent
.SetEventObject(this);
523 cmdEvent
.SetPosition(m_caretPosition
+1);
525 GetEventHandler()->ProcessEvent(cmdEvent
);
528 /// Left-double-click
529 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
531 wxRichTextEvent
cmdEvent(
532 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
534 cmdEvent
.SetEventObject(this);
535 cmdEvent
.SetPosition(m_caretPosition
+1);
537 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
539 SelectWord(GetCaretPosition()+1);
544 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
546 wxRichTextEvent
cmdEvent(
547 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
549 cmdEvent
.SetEventObject(this);
550 cmdEvent
.SetPosition(m_caretPosition
+1);
552 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
557 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
561 flags
|= wxRICHTEXT_CTRL_DOWN
;
562 if (event
.ShiftDown())
563 flags
|= wxRICHTEXT_SHIFT_DOWN
;
565 flags
|= wxRICHTEXT_ALT_DOWN
;
567 if (event
.GetKeyCode() == WXK_LEFT
||
568 event
.GetKeyCode() == WXK_RIGHT
||
569 event
.GetKeyCode() == WXK_UP
||
570 event
.GetKeyCode() == WXK_DOWN
||
571 event
.GetKeyCode() == WXK_HOME
||
572 event
.GetKeyCode() == WXK_PAGEUP
||
573 event
.GetKeyCode() == WXK_PAGEDOWN
||
574 event
.GetKeyCode() == WXK_END
||
576 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
577 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
578 event
.GetKeyCode() == WXK_NUMPAD_UP
||
579 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
580 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
581 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
582 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
583 event
.GetKeyCode() == WXK_NUMPAD_END
)
585 KeyboardNavigate(event
.GetKeyCode(), flags
);
589 // all the other keys modify the controls contents which shouldn't be
590 // possible if we're read-only
597 if (event
.GetKeyCode() == WXK_RETURN
)
599 BeginBatchUndo(_("Insert Text"));
601 long newPos
= m_caretPosition
;
603 DeleteSelectedContent(& newPos
);
605 if (event
.ShiftDown())
608 text
= wxRichTextLineBreakChar
;
609 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
612 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
615 SetDefaultStyleToCursorStyle();
617 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
619 wxRichTextEvent
cmdEvent(
620 wxEVT_COMMAND_RICHTEXT_RETURN
,
622 cmdEvent
.SetEventObject(this);
623 cmdEvent
.SetFlags(flags
);
624 cmdEvent
.SetPosition(newPos
+1);
626 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
628 // Generate conventional event
629 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
630 InitCommandEvent(textEvent
);
632 GetEventHandler()->ProcessEvent(textEvent
);
636 else if (event
.GetKeyCode() == WXK_BACK
)
638 BeginBatchUndo(_("Delete Text"));
640 // Submit range in character positions, which are greater than caret positions,
641 // so subtract 1 for deleted character and add 1 for conversion to character position.
642 if (m_caretPosition
> -1 && !HasSelection())
644 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
647 DeleteSelectedContent();
651 if (GetLastPosition() == -1)
655 m_caretPosition
= -1;
657 SetDefaultStyleToCursorStyle();
660 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
662 wxRichTextEvent
cmdEvent(
663 wxEVT_COMMAND_RICHTEXT_DELETE
,
665 cmdEvent
.SetEventObject(this);
666 cmdEvent
.SetFlags(flags
);
667 cmdEvent
.SetPosition(m_caretPosition
+1);
668 GetEventHandler()->ProcessEvent(cmdEvent
);
672 else if (event
.GetKeyCode() == WXK_DELETE
)
674 BeginBatchUndo(_("Delete Text"));
676 // Submit range in character positions, which are greater than caret positions,
677 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
679 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
682 DeleteSelectedContent();
686 if (GetLastPosition() == -1)
690 m_caretPosition
= -1;
692 SetDefaultStyleToCursorStyle();
695 wxRichTextEvent
cmdEvent(
696 wxEVT_COMMAND_RICHTEXT_DELETE
,
698 cmdEvent
.SetEventObject(this);
699 cmdEvent
.SetFlags(flags
);
700 cmdEvent
.SetPosition(m_caretPosition
+1);
701 GetEventHandler()->ProcessEvent(cmdEvent
);
707 long keycode
= event
.GetKeyCode();
780 case WXK_NUMPAD_SPACE
:
782 case WXK_NUMPAD_ENTER
:
787 case WXK_NUMPAD_HOME
:
788 case WXK_NUMPAD_LEFT
:
790 case WXK_NUMPAD_RIGHT
:
791 case WXK_NUMPAD_DOWN
:
792 case WXK_NUMPAD_PAGEUP
:
793 case WXK_NUMPAD_PAGEDOWN
:
795 case WXK_NUMPAD_BEGIN
:
796 case WXK_NUMPAD_INSERT
:
797 case WXK_NUMPAD_DELETE
:
798 case WXK_NUMPAD_EQUAL
:
799 case WXK_NUMPAD_MULTIPLY
:
801 case WXK_NUMPAD_SEPARATOR
:
802 case WXK_NUMPAD_SUBTRACT
:
803 case WXK_NUMPAD_DECIMAL
:
804 case WXK_WINDOWS_LEFT
:
812 if (event
.CmdDown() || event
.AltDown())
818 wxRichTextEvent
cmdEvent(
819 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
821 cmdEvent
.SetEventObject(this);
822 cmdEvent
.SetFlags(flags
);
824 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
826 cmdEvent
.SetCharacter((wxChar
) keycode
);
828 cmdEvent
.SetPosition(m_caretPosition
+1);
830 if (keycode
== wxT('\t'))
832 // See if we need to promote or demote the selection or paragraph at the cursor
833 // position, instead of inserting a tab.
834 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
835 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
836 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
838 wxRichTextRange range
;
840 range
= GetSelectionRange();
842 range
= para
->GetRange().FromInternal();
844 int promoteBy
= event
.ShiftDown() ? 1 : -1;
846 PromoteList(promoteBy
, range
, NULL
);
848 GetEventHandler()->ProcessEvent(cmdEvent
);
854 BeginBatchUndo(_("Insert Text"));
856 long newPos
= m_caretPosition
;
857 DeleteSelectedContent(& newPos
);
860 wxString str
= event
.GetUnicodeKey();
862 wxString str
= (wxChar
) event
.GetKeyCode();
864 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
868 SetDefaultStyleToCursorStyle();
869 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
871 GetEventHandler()->ProcessEvent(cmdEvent
);
879 /// Delete content if there is a selection, e.g. when pressing a key.
880 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
884 long pos
= m_selectionRange
.GetStart();
885 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
886 m_selectionRange
.SetRange(-2, -2);
896 /// Keyboard navigation
900 Left: left one character
901 Right: right one character
904 Ctrl-Left: left one word
905 Ctrl-Right: right one word
906 Ctrl-Up: previous paragraph start
907 Ctrl-Down: next start of paragraph
910 Ctrl-Home: start of document
911 Ctrl-End: end of document
913 Page-Down: Down a screen
917 Ctrl-Alt-PgUp: Start of window
918 Ctrl-Alt-PgDn: End of window
919 F8: Start selection mode
920 Esc: End selection mode
922 Adding Shift does the above but starts/extends selection.
927 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
929 bool success
= false;
931 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
933 if (flags
& wxRICHTEXT_CTRL_DOWN
)
934 success
= WordRight(1, flags
);
936 success
= MoveRight(1, flags
);
938 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
940 if (flags
& wxRICHTEXT_CTRL_DOWN
)
941 success
= WordLeft(1, flags
);
943 success
= MoveLeft(1, flags
);
945 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
947 if (flags
& wxRICHTEXT_CTRL_DOWN
)
948 success
= MoveToParagraphStart(flags
);
950 success
= MoveUp(1, flags
);
952 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
954 if (flags
& wxRICHTEXT_CTRL_DOWN
)
955 success
= MoveToParagraphEnd(flags
);
957 success
= MoveDown(1, flags
);
959 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
961 success
= PageUp(1, flags
);
963 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
965 success
= PageDown(1, flags
);
967 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
969 if (flags
& wxRICHTEXT_CTRL_DOWN
)
970 success
= MoveHome(flags
);
972 success
= MoveToLineStart(flags
);
974 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
976 if (flags
& wxRICHTEXT_CTRL_DOWN
)
977 success
= MoveEnd(flags
);
979 success
= MoveToLineEnd(flags
);
984 ScrollIntoView(m_caretPosition
, keyCode
);
985 SetDefaultStyleToCursorStyle();
991 /// Extend the selection. Selections are in caret positions.
992 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
994 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
996 // If not currently selecting, start selecting
997 if (m_selectionRange
.GetStart() == -2)
999 m_selectionAnchor
= oldPos
;
1001 if (oldPos
> newPos
)
1002 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1004 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1008 // Always ensure that the selection range start is greater than
1010 if (newPos
> m_selectionAnchor
)
1011 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1013 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1016 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1018 wxLogDebug(wxT("Strange selection range"));
1027 /// Scroll into view, returning true if we scrolled.
1028 /// This takes a _caret_ position.
1029 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1031 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1037 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1039 int startXUnits
, startYUnits
;
1040 GetViewStart(& startXUnits
, & startYUnits
);
1041 int startY
= startYUnits
* ppuY
;
1044 GetVirtualSize(& sx
, & sy
);
1050 wxRect rect
= line
->GetRect();
1052 bool scrolled
= false;
1054 wxSize clientSize
= GetClientSize();
1057 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1058 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1059 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1060 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1062 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1064 // Make it scroll so this item is at the bottom
1066 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1067 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1069 // If we're still off the screen, scroll another line down
1070 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1073 if (startYUnits
!= yUnits
)
1075 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1079 else if (rect
.y
< startY
)
1081 // Make it scroll so this item is at the top
1084 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1086 if (startYUnits
!= yUnits
)
1088 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1094 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1095 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1096 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1097 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1099 if (rect
.y
< startY
)
1101 // Make it scroll so this item is at the top
1104 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1106 if (startYUnits
!= yUnits
)
1108 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1112 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1114 // Make it scroll so this item is at the bottom
1116 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1117 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1119 // If we're still off the screen, scroll another line down
1120 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1123 if (startYUnits
!= yUnits
)
1125 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1135 /// Is the given position visible on the screen?
1136 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1138 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1144 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1147 GetViewStart(& startX
, & startY
);
1149 startY
= startY
* ppuY
;
1152 GetVirtualSize(& sx
, & sy
);
1157 wxRect rect
= line
->GetRect();
1159 wxSize clientSize
= GetClientSize();
1161 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1164 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1166 m_caretPosition
= position
;
1167 m_caretAtLineStart
= showAtLineStart
;
1170 /// Move caret one visual step forward: this may mean setting a flag
1171 /// and keeping the same position if we're going from the end of one line
1172 /// to the start of the next, which may be the exact same caret position.
1173 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1175 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1177 // Only do the check if we're not at the end of the paragraph (where things work OK
1179 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1181 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1185 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1187 // We're at the end of a line. See whether we need to
1188 // stay at the same actual caret position but change visual
1189 // position, or not.
1190 if (oldPosition
== lineRange
.GetEnd())
1192 if (m_caretAtLineStart
)
1194 // We're already at the start of the line, so actually move on now.
1195 m_caretPosition
= oldPosition
+ 1;
1196 m_caretAtLineStart
= false;
1200 // We're showing at the end of the line, so keep to
1201 // the same position but indicate that we're to show
1202 // at the start of the next line.
1203 m_caretPosition
= oldPosition
;
1204 m_caretAtLineStart
= true;
1206 SetDefaultStyleToCursorStyle();
1212 SetDefaultStyleToCursorStyle();
1215 /// Move caret one visual step backward: this may mean setting a flag
1216 /// and keeping the same position if we're going from the end of one line
1217 /// to the start of the next, which may be the exact same caret position.
1218 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1220 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1222 // Only do the check if we're not at the start of the paragraph (where things work OK
1224 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1226 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1230 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1232 // We're at the start of a line. See whether we need to
1233 // stay at the same actual caret position but change visual
1234 // position, or not.
1235 if (oldPosition
== lineRange
.GetStart())
1237 m_caretPosition
= oldPosition
-1;
1238 m_caretAtLineStart
= true;
1241 else if (oldPosition
== lineRange
.GetEnd())
1243 if (m_caretAtLineStart
)
1245 // We're at the start of the line, so keep the same caret position
1246 // but clear the start-of-line flag.
1247 m_caretPosition
= oldPosition
;
1248 m_caretAtLineStart
= false;
1252 // We're showing at the end of the line, so go back
1253 // to the previous character position.
1254 m_caretPosition
= oldPosition
- 1;
1256 SetDefaultStyleToCursorStyle();
1262 SetDefaultStyleToCursorStyle();
1266 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1268 long endPos
= GetBuffer().GetRange().GetEnd();
1270 if (m_caretPosition
+ noPositions
< endPos
)
1272 long oldPos
= m_caretPosition
;
1273 long newPos
= m_caretPosition
+ noPositions
;
1275 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1279 // Determine by looking at oldPos and m_caretPosition whether
1280 // we moved from the end of a line to the start of the next line, in which case
1281 // we want to adjust the caret position such that it is positioned at the
1282 // start of the next line, rather than jumping past the first character of the
1284 if (noPositions
== 1 && !extendSel
)
1285 MoveCaretForward(oldPos
);
1287 SetCaretPosition(newPos
);
1290 SetDefaultStyleToCursorStyle();
1301 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1305 if (m_caretPosition
> startPos
- noPositions
+ 1)
1307 long oldPos
= m_caretPosition
;
1308 long newPos
= m_caretPosition
- noPositions
;
1309 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1313 if (noPositions
== 1 && !extendSel
)
1314 MoveCaretBack(oldPos
);
1316 SetCaretPosition(newPos
);
1319 SetDefaultStyleToCursorStyle();
1330 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1332 return MoveDown(- noLines
, flags
);
1336 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1341 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1342 wxPoint pt
= GetCaret()->GetPosition();
1343 long newLine
= lineNumber
+ noLines
;
1345 if (lineNumber
!= -1)
1349 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1351 if (newLine
> lastLine
)
1361 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1364 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1370 wxClientDC
dc(this);
1372 dc
.SetFont(GetFont());
1374 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1376 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1378 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1379 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1380 // so we view the caret at the start of the line.
1381 bool caretLineStart
= false;
1382 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1384 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1385 wxRichTextRange lineRange
;
1387 lineRange
= thisLine
->GetAbsoluteRange();
1389 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1392 caretLineStart
= true;
1396 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1397 if (para
&& para
->GetRange().GetStart() == newPos
)
1402 long newSelEnd
= newPos
;
1404 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1408 SetCaretPosition(newPos
, caretLineStart
);
1410 SetDefaultStyleToCursorStyle();
1420 /// Move to the end of the paragraph
1421 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1423 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1426 long newPos
= para
->GetRange().GetEnd() - 1;
1427 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1431 SetCaretPosition(newPos
);
1433 SetDefaultStyleToCursorStyle();
1443 /// Move to the start of the paragraph
1444 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1446 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1449 long newPos
= para
->GetRange().GetStart() - 1;
1450 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1454 SetCaretPosition(newPos
);
1456 SetDefaultStyleToCursorStyle();
1466 /// Move to the end of the line
1467 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1469 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1473 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1474 long newPos
= lineRange
.GetEnd();
1475 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1479 SetCaretPosition(newPos
);
1481 SetDefaultStyleToCursorStyle();
1491 /// Move to the start of the line
1492 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1494 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1497 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1498 long newPos
= lineRange
.GetStart()-1;
1500 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1504 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1506 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1508 SetDefaultStyleToCursorStyle();
1518 /// Move to the start of the buffer
1519 bool wxRichTextCtrl::MoveHome(int flags
)
1521 if (m_caretPosition
!= -1)
1523 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1527 SetCaretPosition(-1);
1529 SetDefaultStyleToCursorStyle();
1539 /// Move to the end of the buffer
1540 bool wxRichTextCtrl::MoveEnd(int flags
)
1542 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1544 if (m_caretPosition
!= endPos
)
1546 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1550 SetCaretPosition(endPos
);
1552 SetDefaultStyleToCursorStyle();
1562 /// Move noPages pages up
1563 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1565 return PageDown(- noPages
, flags
);
1568 /// Move noPages pages down
1569 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1571 // Calculate which line occurs noPages * screen height further down.
1572 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1575 wxSize clientSize
= GetClientSize();
1576 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1578 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1581 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1582 long pos
= lineRange
.GetStart()-1;
1583 if (pos
!= m_caretPosition
)
1585 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1587 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1591 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1593 SetDefaultStyleToCursorStyle();
1605 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1607 return str
== wxT(" ") || str
== wxT("\t");
1610 // Finds the caret position for the next word
1611 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1613 long endPos
= GetBuffer().GetRange().GetEnd();
1617 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1619 // First skip current text to space
1620 while (i
< endPos
&& i
> -1)
1622 // i is in character, not caret positions
1623 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1624 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1625 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1629 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1636 while (i
< endPos
&& i
> -1)
1638 // i is in character, not caret positions
1639 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1640 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1641 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1642 return wxMax(-1, i
);
1644 if (text
.empty()) // End of paragraph, or maybe an image
1645 return wxMax(-1, i
- 1);
1646 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1650 // Convert to caret position
1651 return wxMax(-1, i
- 1);
1660 long i
= m_caretPosition
;
1662 // First skip white space
1663 while (i
< endPos
&& i
> -1)
1665 // i is in character, not caret positions
1666 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1667 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1669 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1671 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1676 // Next skip current text to space
1677 while (i
< endPos
&& i
> -1)
1679 // i is in character, not caret positions
1680 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1681 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1682 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1685 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1698 /// Move n words left
1699 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1701 long pos
= FindNextWordPosition(-1);
1702 if (pos
!= m_caretPosition
)
1704 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1706 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1710 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1712 SetDefaultStyleToCursorStyle();
1722 /// Move n words right
1723 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1725 long pos
= FindNextWordPosition(1);
1726 if (pos
!= m_caretPosition
)
1728 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1730 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1734 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1736 SetDefaultStyleToCursorStyle();
1747 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1749 // Only do sizing optimization for large buffers
1750 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1752 m_fullLayoutRequired
= true;
1753 m_fullLayoutTime
= wxGetLocalTimeMillis();
1754 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1755 LayoutContent(true /* onlyVisibleRect */);
1758 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1760 #if wxRICHTEXT_BUFFERED_PAINTING
1768 /// Idle-time processing
1769 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1771 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1773 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1775 m_fullLayoutRequired
= false;
1776 m_fullLayoutTime
= 0;
1777 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1778 ShowPosition(m_fullLayoutSavedPosition
);
1782 if (m_caretPositionForDefaultStyle
!= -2)
1784 // If the caret position has changed, no longer reflect the default style
1786 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1787 m_caretPositionForDefaultStyle
= -2;
1794 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1800 /// Set up scrollbars, e.g. after a resize
1801 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1806 if (GetBuffer().IsEmpty())
1808 SetScrollbars(0, 0, 0, 0, 0, 0);
1812 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1813 // of pixels. See e.g. wxVScrolledWindow for ideas.
1814 int pixelsPerUnit
= 5;
1815 wxSize clientSize
= GetClientSize();
1817 int maxHeight
= GetBuffer().GetCachedSize().y
;
1819 // Round up so we have at least maxHeight pixels
1820 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1822 int startX
= 0, startY
= 0;
1824 GetViewStart(& startX
, & startY
);
1826 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1827 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1829 // Move to previous scroll position if
1831 SetScrollbars(0, pixelsPerUnit
,
1833 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1836 /// Paint the background
1837 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1839 wxColour backgroundColour
= GetBackgroundColour();
1840 if (!backgroundColour
.Ok())
1841 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1843 // Clear the background
1844 dc
.SetBrush(wxBrush(backgroundColour
));
1845 dc
.SetPen(*wxTRANSPARENT_PEN
);
1846 wxRect
windowRect(GetClientSize());
1847 windowRect
.x
-= 2; windowRect
.y
-= 2;
1848 windowRect
.width
+= 4; windowRect
.height
+= 4;
1850 // We need to shift the rectangle to take into account
1851 // scrolling. Converting device to logical coordinates.
1852 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1853 dc
.DrawRectangle(windowRect
);
1856 #if wxRICHTEXT_BUFFERED_PAINTING
1857 /// Recreate buffer bitmap if necessary
1858 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1861 if (sz
== wxDefaultSize
)
1862 sz
= GetClientSize();
1864 if (sz
.x
< 1 || sz
.y
< 1)
1867 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1868 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1869 return m_bufferBitmap
.Ok();
1873 // ----------------------------------------------------------------------------
1874 // file IO functions
1875 // ----------------------------------------------------------------------------
1877 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1879 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1881 m_filename
= filename
;
1884 SetInsertionPoint(0);
1887 SetupScrollbars(true);
1889 SendTextUpdatedEvent();
1895 wxLogError(_("File couldn't be loaded."));
1901 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1903 if (GetBuffer().SaveFile(filename
, fileType
))
1905 m_filename
= filename
;
1912 wxLogError(_("The text couldn't be saved."));
1917 // ----------------------------------------------------------------------------
1918 // wxRichTextCtrl specific functionality
1919 // ----------------------------------------------------------------------------
1921 /// Add a new paragraph of text to the end of the buffer
1922 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1924 return GetBuffer().AddParagraph(text
);
1928 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1930 return GetBuffer().AddImage(image
);
1933 // ----------------------------------------------------------------------------
1934 // selection and ranges
1935 // ----------------------------------------------------------------------------
1937 void wxRichTextCtrl::SelectAll()
1939 SetSelection(0, GetLastPosition()+1);
1940 m_selectionAnchor
= -1;
1944 void wxRichTextCtrl::SelectNone()
1946 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1947 SetSelection(-2, -2);
1948 m_selectionAnchor
= -2;
1951 static bool wxIsWordDelimiter(const wxString
& text
)
1953 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1956 /// Select the word at the given character position
1957 bool wxRichTextCtrl::SelectWord(long position
)
1959 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1962 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1966 long positionStart
= position
;
1967 long positionEnd
= position
;
1969 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1971 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1972 if (wxIsWordDelimiter(text
))
1978 if (positionStart
< para
->GetRange().GetStart())
1979 positionStart
= para
->GetRange().GetStart();
1981 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1983 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1984 if (wxIsWordDelimiter(text
))
1990 if (positionEnd
>= para
->GetRange().GetEnd())
1991 positionEnd
= para
->GetRange().GetEnd();
1993 SetSelection(positionStart
, positionEnd
+1);
1995 if (positionStart
>= 0)
1997 MoveCaret(positionStart
-1, true);
1998 SetDefaultStyleToCursorStyle();
2004 wxString
wxRichTextCtrl::GetStringSelection() const
2007 GetSelection(&from
, &to
);
2009 return GetRange(from
, to
);
2012 // ----------------------------------------------------------------------------
2014 // ----------------------------------------------------------------------------
2016 wxTextCtrlHitTestResult
2017 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2019 // implement in terms of the other overload as the native ports typically
2020 // can get the position and not (x, y) pair directly (although wxUniv
2021 // directly gets x and y -- and so overrides this method as well)
2023 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2025 if ( rc
!= wxTE_HT_UNKNOWN
)
2027 PositionToXY(pos
, x
, y
);
2033 wxTextCtrlHitTestResult
2034 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2037 wxClientDC
dc((wxRichTextCtrl
*) this);
2038 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2040 // Buffer uses logical position (relative to start of buffer)
2042 wxPoint pt2
= GetLogicalPoint(pt
);
2044 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2046 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2047 return wxTE_HT_BEFORE
;
2048 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2049 return wxTE_HT_BEYOND
;
2050 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2051 return wxTE_HT_ON_TEXT
;
2053 return wxTE_HT_UNKNOWN
;
2056 // ----------------------------------------------------------------------------
2057 // set/get the controls text
2058 // ----------------------------------------------------------------------------
2060 wxString
wxRichTextCtrl::GetValue() const
2062 return GetBuffer().GetText();
2065 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2067 // Public API for range is different from internals
2068 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2071 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2075 if (!value
.IsEmpty())
2077 // Remove empty paragraph
2078 GetBuffer().Clear();
2081 // for compatibility, don't move the cursor when doing SetValue()
2082 SetInsertionPoint(0);
2086 // still send an event for consistency
2087 if (flags
& SetValue_SendEvent
)
2088 SendTextUpdatedEvent();
2093 void wxRichTextCtrl::WriteText(const wxString
& value
)
2098 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2100 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2102 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2104 if ( flags
& SetValue_SendEvent
)
2105 SendTextUpdatedEvent();
2108 void wxRichTextCtrl::AppendText(const wxString
& text
)
2110 SetInsertionPointEnd();
2115 /// Write an image at the current insertion point
2116 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2118 wxRichTextImageBlock imageBlock
;
2120 wxImage image2
= image
;
2121 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2122 return WriteImage(imageBlock
);
2127 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2129 wxRichTextImageBlock imageBlock
;
2132 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2133 return WriteImage(imageBlock
);
2138 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2140 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2143 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2147 wxRichTextImageBlock imageBlock
;
2149 wxImage image
= bitmap
.ConvertToImage();
2150 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2151 return WriteImage(imageBlock
);
2157 /// Insert a newline (actually paragraph) at the current insertion point.
2158 bool wxRichTextCtrl::Newline()
2160 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2163 /// Insert a line break at the current insertion point.
2164 bool wxRichTextCtrl::LineBreak()
2167 text
= wxRichTextLineBreakChar
;
2168 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2171 // ----------------------------------------------------------------------------
2172 // Clipboard operations
2173 // ----------------------------------------------------------------------------
2175 void wxRichTextCtrl::Copy()
2179 wxRichTextRange range
= GetInternalSelectionRange();
2180 GetBuffer().CopyToClipboard(range
);
2184 void wxRichTextCtrl::Cut()
2188 wxRichTextRange range
= GetInternalSelectionRange();
2189 GetBuffer().CopyToClipboard(range
);
2191 DeleteSelectedContent();
2197 void wxRichTextCtrl::Paste()
2201 BeginBatchUndo(_("Paste"));
2203 long newPos
= m_caretPosition
;
2204 DeleteSelectedContent(& newPos
);
2206 GetBuffer().PasteFromClipboard(newPos
);
2212 void wxRichTextCtrl::DeleteSelection()
2214 if (CanDeleteSelection())
2216 DeleteSelectedContent();
2220 bool wxRichTextCtrl::HasSelection() const
2222 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2225 bool wxRichTextCtrl::CanCopy() const
2227 // Can copy if there's a selection
2228 return HasSelection();
2231 bool wxRichTextCtrl::CanCut() const
2233 return HasSelection() && IsEditable();
2236 bool wxRichTextCtrl::CanPaste() const
2238 if ( !IsEditable() )
2241 return GetBuffer().CanPasteFromClipboard();
2244 bool wxRichTextCtrl::CanDeleteSelection() const
2246 return HasSelection() && IsEditable();
2250 // ----------------------------------------------------------------------------
2252 // ----------------------------------------------------------------------------
2254 void wxRichTextCtrl::SetEditable(bool editable
)
2256 m_editable
= editable
;
2259 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2263 m_caretPosition
= pos
- 1;
2266 void wxRichTextCtrl::SetInsertionPointEnd()
2268 long pos
= GetLastPosition();
2269 SetInsertionPoint(pos
);
2272 long wxRichTextCtrl::GetInsertionPoint() const
2274 return m_caretPosition
+1;
2277 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2279 return GetBuffer().GetRange().GetEnd();
2282 // If the return values from and to are the same, there is no
2284 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2286 *from
= m_selectionRange
.GetStart();
2287 *to
= m_selectionRange
.GetEnd();
2288 if ((*to
) != -1 && (*to
) != -2)
2292 bool wxRichTextCtrl::IsEditable() const
2297 // ----------------------------------------------------------------------------
2299 // ----------------------------------------------------------------------------
2301 void wxRichTextCtrl::SetSelection(long from
, long to
)
2303 // if from and to are both -1, it means (in wxWidgets) that all text should
2305 if ( (from
== -1) && (to
== -1) )
2308 to
= GetLastPosition()+1;
2311 DoSetSelection(from
, to
);
2314 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2316 m_selectionAnchor
= from
;
2317 m_selectionRange
.SetRange(from
, to
-1);
2319 m_caretPosition
= from
-1;
2325 // ----------------------------------------------------------------------------
2327 // ----------------------------------------------------------------------------
2329 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2330 const wxString
& value
)
2332 BeginBatchUndo(_("Replace"));
2334 DeleteSelectedContent();
2336 DoWriteText(value
, SetValue_SelectionOnly
);
2341 void wxRichTextCtrl::Remove(long from
, long to
)
2345 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2352 bool wxRichTextCtrl::IsModified() const
2354 return m_buffer
.IsModified();
2357 void wxRichTextCtrl::MarkDirty()
2359 m_buffer
.Modify(true);
2362 void wxRichTextCtrl::DiscardEdits()
2364 m_caretPositionForDefaultStyle
= -2;
2365 m_buffer
.Modify(false);
2366 m_buffer
.GetCommandProcessor()->ClearCommands();
2369 int wxRichTextCtrl::GetNumberOfLines() const
2371 return GetBuffer().GetParagraphCount();
2374 // ----------------------------------------------------------------------------
2375 // Positions <-> coords
2376 // ----------------------------------------------------------------------------
2378 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2380 return GetBuffer().XYToPosition(x
, y
);
2383 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2385 return GetBuffer().PositionToXY(pos
, x
, y
);
2388 // ----------------------------------------------------------------------------
2390 // ----------------------------------------------------------------------------
2392 void wxRichTextCtrl::ShowPosition(long pos
)
2394 if (!IsPositionVisible(pos
))
2395 ScrollIntoView(pos
-1, WXK_DOWN
);
2398 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2400 return GetBuffer().GetParagraphLength(lineNo
);
2403 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2405 return GetBuffer().GetParagraphText(lineNo
);
2408 // ----------------------------------------------------------------------------
2410 // ----------------------------------------------------------------------------
2412 void wxRichTextCtrl::Undo()
2416 GetCommandProcessor()->Undo();
2420 void wxRichTextCtrl::Redo()
2424 GetCommandProcessor()->Redo();
2428 bool wxRichTextCtrl::CanUndo() const
2430 return GetCommandProcessor()->CanUndo();
2433 bool wxRichTextCtrl::CanRedo() const
2435 return GetCommandProcessor()->CanRedo();
2438 // ----------------------------------------------------------------------------
2439 // implementation details
2440 // ----------------------------------------------------------------------------
2442 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2444 SetValue(event
.GetString());
2445 GetEventHandler()->ProcessEvent(event
);
2448 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2450 // By default, load the first file into the text window.
2451 if (event
.GetNumberOfFiles() > 0)
2453 LoadFile(event
.GetFiles()[0]);
2457 wxSize
wxRichTextCtrl::DoGetBestSize() const
2459 return wxSize(10, 10);
2462 // ----------------------------------------------------------------------------
2463 // standard handlers for standard edit menu events
2464 // ----------------------------------------------------------------------------
2466 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2471 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2476 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2481 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2486 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2491 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2496 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2498 event
.Enable( CanCut() );
2501 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2503 event
.Enable( CanCopy() );
2506 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2508 event
.Enable( CanDeleteSelection() );
2511 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2513 event
.Enable( CanPaste() );
2516 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2518 event
.Enable( CanUndo() );
2519 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2522 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2524 event
.Enable( CanRedo() );
2525 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2528 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2533 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2535 event
.Enable(GetLastPosition() > 0);
2538 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2542 m_contextMenu
= new wxMenu
;
2543 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2544 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2545 m_contextMenu
->AppendSeparator();
2546 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2547 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2548 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2549 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2550 m_contextMenu
->AppendSeparator();
2551 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2553 PopupMenu(m_contextMenu
);
2557 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2559 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2562 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2564 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2567 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2569 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2572 // extended style setting operation with flags including:
2573 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2574 // see richtextbuffer.h for more details.
2575 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2577 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2580 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2582 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2585 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2587 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2590 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2592 return GetBuffer().SetDefaultStyle(style
);
2595 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2597 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2600 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2602 return GetBuffer().GetDefaultStyle();
2605 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2607 return GetBuffer().GetDefaultStyle();
2610 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2612 wxTextAttrEx
attr(style
);
2613 if (GetBuffer().GetStyle(position
, attr
))
2622 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2624 return GetBuffer().GetStyle(position
, style
);
2627 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2629 return GetBuffer().GetStyle(position
, style
);
2632 // get the common set of styles for the range
2633 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2635 wxTextAttrEx styleEx
;
2636 if (GetBuffer().GetStyleForRange(range
.ToInternal(), styleEx
))
2645 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2647 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2650 /// Get the content (uncombined) attributes for this position.
2652 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2654 wxTextAttrEx
attr(style
);
2655 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2664 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2666 return GetBuffer().GetUncombinedStyle(position
, style
);
2669 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2671 return GetBuffer().GetUncombinedStyle(position
, style
);
2674 /// Set font, and also the buffer attributes
2675 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2677 wxControl::SetFont(font
);
2679 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2681 GetBuffer().SetBasicStyle(attr
);
2683 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2689 /// Transform logical to physical
2690 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2693 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2698 /// Transform physical to logical
2699 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2702 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2707 /// Position the caret
2708 void wxRichTextCtrl::PositionCaret()
2713 //wxLogDebug(wxT("PositionCaret"));
2716 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2718 wxPoint originalPt
= caretRect
.GetPosition();
2719 wxPoint pt
= GetPhysicalPoint(originalPt
);
2720 if (GetCaret()->GetPosition() != pt
)
2722 GetCaret()->Move(pt
);
2723 GetCaret()->SetSize(caretRect
.GetSize());
2728 /// Get the caret height and position for the given character position
2729 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2731 wxClientDC
dc(this);
2732 dc
.SetFont(GetFont());
2739 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2741 // Caret height can't be zero
2743 height
= dc
.GetCharHeight();
2745 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2752 /// Gets the line for the visible caret position. If the caret is
2753 /// shown at the very end of the line, it means the next character is actually
2754 /// on the following line. So let's get the line we're expecting to find
2755 /// if this is the case.
2756 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2758 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2759 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2762 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2763 if (caretPosition
== lineRange
.GetStart()-1 &&
2764 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2766 if (!m_caretAtLineStart
)
2767 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2774 /// Move the caret to the given character position
2775 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2777 if (GetBuffer().GetDirty())
2780 if (pos
<= GetBuffer().GetRange().GetEnd())
2782 SetCaretPosition(pos
, showAtLineStart
);
2792 /// Layout the buffer: which we must do before certain operations, such as
2793 /// setting the caret position.
2794 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2796 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2798 wxRect
availableSpace(GetClientSize());
2799 if (availableSpace
.width
== 0)
2800 availableSpace
.width
= 10;
2801 if (availableSpace
.height
== 0)
2802 availableSpace
.height
= 10;
2804 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2805 if (onlyVisibleRect
)
2807 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2808 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2811 wxClientDC
dc(this);
2812 dc
.SetFont(GetFont());
2816 GetBuffer().Defragment();
2817 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2818 GetBuffer().Layout(dc
, availableSpace
, flags
);
2819 GetBuffer().SetDirty(false);
2828 /// Is all of the selection bold?
2829 bool wxRichTextCtrl::IsSelectionBold()
2833 wxRichTextAttr attr
;
2834 wxRichTextRange range
= GetSelectionRange();
2835 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2836 attr
.SetFontWeight(wxBOLD
);
2838 return HasCharacterAttributes(range
, attr
);
2842 // If no selection, then we need to combine current style with default style
2843 // to see what the effect would be if we started typing.
2844 wxRichTextAttr attr
;
2845 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2847 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2848 if (GetStyle(pos
, attr
))
2850 if (IsDefaultStyleShowing())
2851 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2852 return attr
.GetFontWeight() == wxBOLD
;
2858 /// Is all of the selection italics?
2859 bool wxRichTextCtrl::IsSelectionItalics()
2863 wxRichTextRange range
= GetSelectionRange();
2864 wxRichTextAttr attr
;
2865 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2866 attr
.SetFontStyle(wxITALIC
);
2868 return HasCharacterAttributes(range
, attr
);
2872 // If no selection, then we need to combine current style with default style
2873 // to see what the effect would be if we started typing.
2874 wxRichTextAttr attr
;
2875 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2877 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2878 if (GetStyle(pos
, attr
))
2880 if (IsDefaultStyleShowing())
2881 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2882 return attr
.GetFontStyle() == wxITALIC
;
2888 /// Is all of the selection underlined?
2889 bool wxRichTextCtrl::IsSelectionUnderlined()
2893 wxRichTextRange range
= GetSelectionRange();
2894 wxRichTextAttr attr
;
2895 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2896 attr
.SetFontUnderlined(true);
2898 return HasCharacterAttributes(range
, attr
);
2902 // If no selection, then we need to combine current style with default style
2903 // to see what the effect would be if we started typing.
2904 wxRichTextAttr attr
;
2905 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2906 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2908 if (GetStyle(pos
, attr
))
2910 if (IsDefaultStyleShowing())
2911 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2912 return attr
.GetFontUnderlined();
2918 /// Apply bold to the selection
2919 bool wxRichTextCtrl::ApplyBoldToSelection()
2921 wxRichTextAttr attr
;
2922 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2923 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2926 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2928 SetAndShowDefaultStyle(attr
);
2932 /// Apply italic to the selection
2933 bool wxRichTextCtrl::ApplyItalicToSelection()
2935 wxRichTextAttr attr
;
2936 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2937 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2940 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2942 SetAndShowDefaultStyle(attr
);
2946 /// Apply underline to the selection
2947 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2949 wxRichTextAttr attr
;
2950 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2951 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2954 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2956 SetAndShowDefaultStyle(attr
);
2960 /// Is all of the selection aligned according to the specified flag?
2961 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2963 wxRichTextRange range
;
2965 range
= GetSelectionRange();
2967 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2969 wxRichTextAttr attr
;
2970 attr
.SetAlignment(alignment
);
2972 return HasParagraphAttributes(range
, attr
);
2975 /// Apply alignment to the selection
2976 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2978 wxRichTextAttr attr
;
2979 attr
.SetAlignment(alignment
);
2981 return SetStyle(GetSelectionRange(), attr
);
2984 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2986 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2991 /// Apply a named style to the selection
2992 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2994 // Flags are defined within each definition, so only certain
2995 // attributes are applied.
2996 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2998 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
3000 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3002 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3004 wxRichTextRange range
;
3007 range
= GetSelectionRange();
3010 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3011 range
= wxRichTextRange(pos
, pos
+1);
3014 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3017 // Make sure the attr has the style name
3018 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3020 attr
.SetParagraphStyleName(def
->GetName());
3022 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3023 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3024 // to change its style independently.
3025 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3028 attr
.SetCharacterStyleName(def
->GetName());
3031 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3034 SetAndShowDefaultStyle(attr
);
3039 /// Apply the style sheet to the buffer, for example if the styles have changed.
3040 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3043 styleSheet
= GetBuffer().GetStyleSheet();
3047 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3049 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3057 /// Sets the default style to the style under the cursor
3058 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3061 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3063 // If at the start of a paragraph, use the next position.
3064 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3066 if (GetUncombinedStyle(pos
, attr
))
3068 SetDefaultStyle(attr
);
3075 /// Returns the first visible position in the current view
3076 long wxRichTextCtrl::GetFirstVisiblePosition() const
3078 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3080 return line
->GetAbsoluteRange().GetStart();
3085 /// Get the first visible point in the window
3086 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3089 int startXUnits
, startYUnits
;
3091 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3092 GetViewStart(& startXUnits
, & startYUnits
);
3094 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3097 /// The adjusted caret position is the character position adjusted to take
3098 /// into account whether we're at the start of a paragraph, in which case
3099 /// style information should be taken from the next position, not current one.
3100 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3102 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3104 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3109 /// Get/set the selection range in character positions. -1, -1 means no selection.
3110 /// The range is in API convention, i.e. a single character selection is denoted
3112 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3114 wxRichTextRange range
= GetInternalSelectionRange();
3115 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3116 range
.SetEnd(range
.GetEnd() + 1);
3120 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3122 wxRichTextRange
range1(range
);
3123 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3124 range1
.SetEnd(range1
.GetEnd() - 1);
3126 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3128 SetInternalSelectionRange(range1
);
3132 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3134 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3137 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3139 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3142 /// Clear list for given range
3143 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3145 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3148 /// Number/renumber any list elements in the given range
3149 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3151 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3154 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3156 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3159 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3160 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3162 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3165 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3167 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3170 /// Deletes the content in the given range
3171 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3173 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3176 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3178 if (sm_availableFontNames
.GetCount() == 0)
3180 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3181 sm_availableFontNames
.Sort();
3183 return sm_availableFontNames
;
3186 void wxRichTextCtrl::ClearAvailableFontNames()
3188 sm_availableFontNames
.Clear();