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 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
133 style
|wxFULL_REPAINT_ON_RESIZE
,
139 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
143 GetBuffer().SetRichTextCtrl(this);
145 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
148 if (style
& wxTE_READONLY
)
151 // The base attributes must all have default values
152 wxTextAttrEx attributes
;
153 attributes
.SetFont(GetFont());
154 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
155 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
156 attributes
.SetLineSpacing(10);
157 attributes
.SetParagraphSpacingAfter(10);
158 attributes
.SetParagraphSpacingBefore(0);
159 attributes
.SetTextEffects(0);
160 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
162 SetBasicStyle(attributes
);
164 // The default attributes will be merged with base attributes, so
165 // can be empty to begin with
166 wxTextAttrEx defaultAttributes
;
167 SetDefaultStyle(defaultAttributes
);
169 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
170 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
172 // Tell the sizers to use the given or best size
173 SetInitialSize(size
);
175 #if wxRICHTEXT_BUFFERED_PAINTING
177 RecreateBuffer(size
);
180 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
181 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
183 SetCursor(m_textCursor
);
185 if (!value
.IsEmpty())
188 GetBuffer().AddEventHandler(this);
193 wxRichTextCtrl::~wxRichTextCtrl()
195 GetBuffer().RemoveEventHandler(this);
197 delete m_contextMenu
;
200 /// Member initialisation
201 void wxRichTextCtrl::Init()
204 m_contextMenu
= NULL
;
206 m_caretPosition
= -1;
207 m_selectionRange
.SetRange(-2, -2);
208 m_selectionAnchor
= -2;
210 m_caretAtLineStart
= false;
212 m_fullLayoutRequired
= false;
213 m_fullLayoutTime
= 0;
214 m_fullLayoutSavedPosition
= 0;
215 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
216 m_caretPositionForDefaultStyle
= -2;
219 /// Call Freeze to prevent refresh
220 void wxRichTextCtrl::Freeze()
225 /// Call Thaw to refresh
226 void wxRichTextCtrl::Thaw()
230 if (m_freezeCount
== 0)
232 if (GetBuffer().GetDirty())
241 void wxRichTextCtrl::Clear()
243 m_buffer
.ResetAndClearCommands();
244 m_buffer
.SetDirty(true);
245 m_caretPosition
= -1;
246 m_caretPositionForDefaultStyle
= -2;
247 m_caretAtLineStart
= false;
248 m_selectionRange
.SetRange(-2, -2);
250 if (m_freezeCount
== 0)
255 SendTextUpdatedEvent();
259 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
261 if (GetCaret() && GetCaret()->IsVisible())
265 #if wxRICHTEXT_BUFFERED_PAINTING
266 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
272 if (m_freezeCount
> 0)
275 dc
.SetFont(GetFont());
277 // Paint the background
280 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
282 wxRect
drawingArea(GetUpdateRegion().GetBox());
283 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
285 wxRect
availableSpace(GetClientSize());
286 if (GetBuffer().GetDirty())
288 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
289 GetBuffer().SetDirty(false);
293 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
296 if (GetCaret() && !GetCaret()->IsVisible())
302 // Empty implementation, to prevent flicker
303 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
307 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
311 if (!GetCaret()->IsVisible())
320 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
322 if (GetCaret() && GetCaret()->IsVisible())
329 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
335 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
341 dc
.SetFont(GetFont());
344 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
346 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
348 m_dragStart
= event
.GetLogicalPosition(dc
);
354 bool caretAtLineStart
= false;
356 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
358 // If we're at the start of a line (but not first in para)
359 // then we should keep the caret showing at the start of the line
360 // by showing the m_caretAtLineStart flag.
361 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
362 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
364 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
365 caretAtLineStart
= true;
369 MoveCaret(position
, caretAtLineStart
);
370 SetDefaultStyleToCursorStyle();
377 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
382 if (GetCapture() == this)
385 // See if we clicked on a URL
388 dc
.SetFont(GetFont());
391 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
392 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
394 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
396 wxRichTextEvent
cmdEvent(
397 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
399 cmdEvent
.SetEventObject(this);
400 cmdEvent
.SetPosition(m_caretPosition
+1);
402 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
405 if (GetStyle(position
, attr
))
407 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
409 wxString urlTarget
= attr
.GetURL();
410 if (!urlTarget
.IsEmpty())
412 wxMouseEvent
mouseEvent(event
);
414 long startPos
= 0, endPos
= 0;
415 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
418 startPos
= obj
->GetRange().GetStart();
419 endPos
= obj
->GetRange().GetEnd();
422 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
423 InitCommandEvent(urlEvent
);
425 urlEvent
.SetString(urlTarget
);
427 GetEventHandler()->ProcessEvent(urlEvent
);
437 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
441 dc
.SetFont(GetFont());
444 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
445 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
447 // See if we need to change the cursor
450 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
453 if (GetStyle(position
, attr
))
455 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
457 SetCursor(m_urlCursor
);
459 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
461 SetCursor(m_textCursor
);
466 SetCursor(m_textCursor
);
469 if (!event
.Dragging())
475 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
477 // TODO: test closeness
479 bool caretAtLineStart
= false;
481 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
483 // If we're at the start of a line (but not first in para)
484 // then we should keep the caret showing at the start of the line
485 // by showing the m_caretAtLineStart flag.
486 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
487 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
489 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
490 caretAtLineStart
= true;
494 if (m_caretPosition
!= position
)
496 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
498 MoveCaret(position
, caretAtLineStart
);
499 SetDefaultStyleToCursorStyle();
508 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& WXUNUSED(event
))
512 wxRichTextEvent
cmdEvent(
513 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
515 cmdEvent
.SetEventObject(this);
516 cmdEvent
.SetPosition(m_caretPosition
+1);
518 GetEventHandler()->ProcessEvent(cmdEvent
);
521 /// Left-double-click
522 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
524 wxRichTextEvent
cmdEvent(
525 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
527 cmdEvent
.SetEventObject(this);
528 cmdEvent
.SetPosition(m_caretPosition
+1);
530 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
532 SelectWord(GetCaretPosition()+1);
537 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
539 wxRichTextEvent
cmdEvent(
540 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
542 cmdEvent
.SetEventObject(this);
543 cmdEvent
.SetPosition(m_caretPosition
+1);
545 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
550 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
554 flags
|= wxRICHTEXT_CTRL_DOWN
;
555 if (event
.ShiftDown())
556 flags
|= wxRICHTEXT_SHIFT_DOWN
;
558 flags
|= wxRICHTEXT_ALT_DOWN
;
560 if (event
.GetKeyCode() == WXK_LEFT
||
561 event
.GetKeyCode() == WXK_RIGHT
||
562 event
.GetKeyCode() == WXK_UP
||
563 event
.GetKeyCode() == WXK_DOWN
||
564 event
.GetKeyCode() == WXK_HOME
||
565 event
.GetKeyCode() == WXK_PAGEUP
||
566 event
.GetKeyCode() == WXK_PAGEDOWN
||
567 event
.GetKeyCode() == WXK_END
||
569 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
570 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
571 event
.GetKeyCode() == WXK_NUMPAD_UP
||
572 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
573 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
574 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
575 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
576 event
.GetKeyCode() == WXK_NUMPAD_END
)
578 KeyboardNavigate(event
.GetKeyCode(), flags
);
582 // all the other keys modify the controls contents which shouldn't be
583 // possible if we're read-only
590 if (event
.GetKeyCode() == WXK_RETURN
)
592 BeginBatchUndo(_("Insert Text"));
594 long newPos
= m_caretPosition
;
596 DeleteSelectedContent(& newPos
);
598 if (event
.ShiftDown())
601 text
= wxRichTextLineBreakChar
;
602 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
605 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
608 SetDefaultStyleToCursorStyle();
610 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
612 wxRichTextEvent
cmdEvent(
613 wxEVT_COMMAND_RICHTEXT_RETURN
,
615 cmdEvent
.SetEventObject(this);
616 cmdEvent
.SetFlags(flags
);
617 cmdEvent
.SetPosition(newPos
+1);
619 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
621 // Generate conventional event
622 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
623 InitCommandEvent(textEvent
);
625 GetEventHandler()->ProcessEvent(textEvent
);
629 else if (event
.GetKeyCode() == WXK_BACK
)
631 BeginBatchUndo(_("Delete Text"));
633 // Submit range in character positions, which are greater than caret positions,
634 // so subtract 1 for deleted character and add 1 for conversion to character position.
635 if (m_caretPosition
> -1 && !HasSelection())
637 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
640 DeleteSelectedContent();
644 if (GetLastPosition() == -1)
648 m_caretPosition
= -1;
650 SetDefaultStyleToCursorStyle();
653 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
655 wxRichTextEvent
cmdEvent(
656 wxEVT_COMMAND_RICHTEXT_DELETE
,
658 cmdEvent
.SetEventObject(this);
659 cmdEvent
.SetFlags(flags
);
660 cmdEvent
.SetPosition(m_caretPosition
+1);
661 GetEventHandler()->ProcessEvent(cmdEvent
);
665 else if (event
.GetKeyCode() == WXK_DELETE
)
667 BeginBatchUndo(_("Delete Text"));
669 // Submit range in character positions, which are greater than caret positions,
670 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
672 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
675 DeleteSelectedContent();
679 if (GetLastPosition() == -1)
683 m_caretPosition
= -1;
685 SetDefaultStyleToCursorStyle();
688 wxRichTextEvent
cmdEvent(
689 wxEVT_COMMAND_RICHTEXT_DELETE
,
691 cmdEvent
.SetEventObject(this);
692 cmdEvent
.SetFlags(flags
);
693 cmdEvent
.SetPosition(m_caretPosition
+1);
694 GetEventHandler()->ProcessEvent(cmdEvent
);
700 long keycode
= event
.GetKeyCode();
773 case WXK_NUMPAD_SPACE
:
775 case WXK_NUMPAD_ENTER
:
780 case WXK_NUMPAD_HOME
:
781 case WXK_NUMPAD_LEFT
:
783 case WXK_NUMPAD_RIGHT
:
784 case WXK_NUMPAD_DOWN
:
785 case WXK_NUMPAD_PAGEUP
:
786 case WXK_NUMPAD_PAGEDOWN
:
788 case WXK_NUMPAD_BEGIN
:
789 case WXK_NUMPAD_INSERT
:
790 case WXK_NUMPAD_DELETE
:
791 case WXK_NUMPAD_EQUAL
:
792 case WXK_NUMPAD_MULTIPLY
:
794 case WXK_NUMPAD_SEPARATOR
:
795 case WXK_NUMPAD_SUBTRACT
:
796 case WXK_NUMPAD_DECIMAL
:
797 case WXK_WINDOWS_LEFT
:
805 if (event
.CmdDown() || event
.AltDown())
811 wxRichTextEvent
cmdEvent(
812 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
814 cmdEvent
.SetEventObject(this);
815 cmdEvent
.SetFlags(flags
);
817 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
819 cmdEvent
.SetCharacter((wxChar
) keycode
);
821 cmdEvent
.SetPosition(m_caretPosition
+1);
823 if (keycode
== wxT('\t'))
825 // See if we need to promote or demote the selection or paragraph at the cursor
826 // position, instead of inserting a tab.
827 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
828 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
829 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
831 wxRichTextRange range
;
833 range
= GetSelectionRange();
835 range
= para
->GetRange().FromInternal();
837 int promoteBy
= event
.ShiftDown() ? 1 : -1;
839 PromoteList(promoteBy
, range
, NULL
);
841 GetEventHandler()->ProcessEvent(cmdEvent
);
847 BeginBatchUndo(_("Insert Text"));
849 long newPos
= m_caretPosition
;
850 DeleteSelectedContent(& newPos
);
853 wxString str
= event
.GetUnicodeKey();
855 wxString str
= (wxChar
) event
.GetKeyCode();
857 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
861 SetDefaultStyleToCursorStyle();
862 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
864 GetEventHandler()->ProcessEvent(cmdEvent
);
872 /// Delete content if there is a selection, e.g. when pressing a key.
873 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
877 long pos
= m_selectionRange
.GetStart();
878 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
879 m_selectionRange
.SetRange(-2, -2);
889 /// Keyboard navigation
893 Left: left one character
894 Right: right one character
897 Ctrl-Left: left one word
898 Ctrl-Right: right one word
899 Ctrl-Up: previous paragraph start
900 Ctrl-Down: next start of paragraph
903 Ctrl-Home: start of document
904 Ctrl-End: end of document
906 Page-Down: Down a screen
910 Ctrl-Alt-PgUp: Start of window
911 Ctrl-Alt-PgDn: End of window
912 F8: Start selection mode
913 Esc: End selection mode
915 Adding Shift does the above but starts/extends selection.
920 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
922 bool success
= false;
924 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
926 if (flags
& wxRICHTEXT_CTRL_DOWN
)
927 success
= WordRight(1, flags
);
929 success
= MoveRight(1, flags
);
931 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
933 if (flags
& wxRICHTEXT_CTRL_DOWN
)
934 success
= WordLeft(1, flags
);
936 success
= MoveLeft(1, flags
);
938 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
940 if (flags
& wxRICHTEXT_CTRL_DOWN
)
941 success
= MoveToParagraphStart(flags
);
943 success
= MoveUp(1, flags
);
945 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
947 if (flags
& wxRICHTEXT_CTRL_DOWN
)
948 success
= MoveToParagraphEnd(flags
);
950 success
= MoveDown(1, flags
);
952 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
954 success
= PageUp(1, flags
);
956 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
958 success
= PageDown(1, flags
);
960 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
962 if (flags
& wxRICHTEXT_CTRL_DOWN
)
963 success
= MoveHome(flags
);
965 success
= MoveToLineStart(flags
);
967 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
969 if (flags
& wxRICHTEXT_CTRL_DOWN
)
970 success
= MoveEnd(flags
);
972 success
= MoveToLineEnd(flags
);
977 ScrollIntoView(m_caretPosition
, keyCode
);
978 SetDefaultStyleToCursorStyle();
984 /// Extend the selection. Selections are in caret positions.
985 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
987 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
989 // If not currently selecting, start selecting
990 if (m_selectionRange
.GetStart() == -2)
992 m_selectionAnchor
= oldPos
;
995 m_selectionRange
.SetRange(newPos
+1, oldPos
);
997 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1001 // Always ensure that the selection range start is greater than
1003 if (newPos
> m_selectionAnchor
)
1004 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1006 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1009 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1011 wxLogDebug(wxT("Strange selection range"));
1020 /// Scroll into view, returning true if we scrolled.
1021 /// This takes a _caret_ position.
1022 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1024 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1030 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1032 int startXUnits
, startYUnits
;
1033 GetViewStart(& startXUnits
, & startYUnits
);
1034 int startY
= startYUnits
* ppuY
;
1037 GetVirtualSize(& sx
, & sy
);
1043 wxRect rect
= line
->GetRect();
1045 bool scrolled
= false;
1047 wxSize clientSize
= GetClientSize();
1050 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1051 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1052 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1053 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1055 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1057 // Make it scroll so this item is at the bottom
1059 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1060 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1062 // If we're still off the screen, scroll another line down
1063 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1066 if (startYUnits
!= yUnits
)
1068 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1072 else if (rect
.y
< startY
)
1074 // Make it scroll so this item is at the top
1077 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1079 if (startYUnits
!= yUnits
)
1081 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1087 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1088 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1089 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1090 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1092 if (rect
.y
< startY
)
1094 // Make it scroll so this item is at the top
1097 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1099 if (startYUnits
!= yUnits
)
1101 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1105 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1107 // Make it scroll so this item is at the bottom
1109 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1110 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1112 // If we're still off the screen, scroll another line down
1113 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1116 if (startYUnits
!= yUnits
)
1118 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1128 /// Is the given position visible on the screen?
1129 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1131 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1137 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1140 GetViewStart(& startX
, & startY
);
1142 startY
= startY
* ppuY
;
1145 GetVirtualSize(& sx
, & sy
);
1150 wxRect rect
= line
->GetRect();
1152 wxSize clientSize
= GetClientSize();
1154 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1157 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1159 m_caretPosition
= position
;
1160 m_caretAtLineStart
= showAtLineStart
;
1163 /// Move caret one visual step forward: this may mean setting a flag
1164 /// and keeping the same position if we're going from the end of one line
1165 /// to the start of the next, which may be the exact same caret position.
1166 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1168 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1170 // Only do the check if we're not at the end of the paragraph (where things work OK
1172 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1174 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1178 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1180 // We're at the end of a line. See whether we need to
1181 // stay at the same actual caret position but change visual
1182 // position, or not.
1183 if (oldPosition
== lineRange
.GetEnd())
1185 if (m_caretAtLineStart
)
1187 // We're already at the start of the line, so actually move on now.
1188 m_caretPosition
= oldPosition
+ 1;
1189 m_caretAtLineStart
= false;
1193 // We're showing at the end of the line, so keep to
1194 // the same position but indicate that we're to show
1195 // at the start of the next line.
1196 m_caretPosition
= oldPosition
;
1197 m_caretAtLineStart
= true;
1199 SetDefaultStyleToCursorStyle();
1205 SetDefaultStyleToCursorStyle();
1208 /// Move caret one visual step backward: this may mean setting a flag
1209 /// and keeping the same position if we're going from the end of one line
1210 /// to the start of the next, which may be the exact same caret position.
1211 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1213 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1215 // Only do the check if we're not at the start of the paragraph (where things work OK
1217 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1219 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1223 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1225 // We're at the start of a line. See whether we need to
1226 // stay at the same actual caret position but change visual
1227 // position, or not.
1228 if (oldPosition
== lineRange
.GetStart())
1230 m_caretPosition
= oldPosition
-1;
1231 m_caretAtLineStart
= true;
1234 else if (oldPosition
== lineRange
.GetEnd())
1236 if (m_caretAtLineStart
)
1238 // We're at the start of the line, so keep the same caret position
1239 // but clear the start-of-line flag.
1240 m_caretPosition
= oldPosition
;
1241 m_caretAtLineStart
= false;
1245 // We're showing at the end of the line, so go back
1246 // to the previous character position.
1247 m_caretPosition
= oldPosition
- 1;
1249 SetDefaultStyleToCursorStyle();
1255 SetDefaultStyleToCursorStyle();
1259 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1261 long endPos
= GetBuffer().GetRange().GetEnd();
1263 if (m_caretPosition
+ noPositions
< endPos
)
1265 long oldPos
= m_caretPosition
;
1266 long newPos
= m_caretPosition
+ noPositions
;
1268 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1272 // Determine by looking at oldPos and m_caretPosition whether
1273 // we moved from the end of a line to the start of the next line, in which case
1274 // we want to adjust the caret position such that it is positioned at the
1275 // start of the next line, rather than jumping past the first character of the
1277 if (noPositions
== 1 && !extendSel
)
1278 MoveCaretForward(oldPos
);
1280 SetCaretPosition(newPos
);
1283 SetDefaultStyleToCursorStyle();
1294 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1298 if (m_caretPosition
> startPos
- noPositions
+ 1)
1300 long oldPos
= m_caretPosition
;
1301 long newPos
= m_caretPosition
- noPositions
;
1302 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1306 if (noPositions
== 1 && !extendSel
)
1307 MoveCaretBack(oldPos
);
1309 SetCaretPosition(newPos
);
1312 SetDefaultStyleToCursorStyle();
1323 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1325 return MoveDown(- noLines
, flags
);
1329 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1334 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1335 wxPoint pt
= GetCaret()->GetPosition();
1336 long newLine
= lineNumber
+ noLines
;
1338 if (lineNumber
!= -1)
1342 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1344 if (newLine
> lastLine
)
1354 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1357 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1363 wxClientDC
dc(this);
1365 dc
.SetFont(GetFont());
1367 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1369 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1371 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1372 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1373 // so we view the caret at the start of the line.
1374 bool caretLineStart
= false;
1375 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1377 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1378 wxRichTextRange lineRange
;
1380 lineRange
= thisLine
->GetAbsoluteRange();
1382 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1385 caretLineStart
= true;
1389 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1390 if (para
&& para
->GetRange().GetStart() == newPos
)
1395 long newSelEnd
= newPos
;
1397 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1401 SetCaretPosition(newPos
, caretLineStart
);
1403 SetDefaultStyleToCursorStyle();
1413 /// Move to the end of the paragraph
1414 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1416 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1419 long newPos
= para
->GetRange().GetEnd() - 1;
1420 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1424 SetCaretPosition(newPos
);
1426 SetDefaultStyleToCursorStyle();
1436 /// Move to the start of the paragraph
1437 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1439 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1442 long newPos
= para
->GetRange().GetStart() - 1;
1443 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1447 SetCaretPosition(newPos
);
1449 SetDefaultStyleToCursorStyle();
1459 /// Move to the end of the line
1460 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1462 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1466 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1467 long newPos
= lineRange
.GetEnd();
1468 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1472 SetCaretPosition(newPos
);
1474 SetDefaultStyleToCursorStyle();
1484 /// Move to the start of the line
1485 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1487 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1490 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1491 long newPos
= lineRange
.GetStart()-1;
1493 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1497 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1499 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1501 SetDefaultStyleToCursorStyle();
1511 /// Move to the start of the buffer
1512 bool wxRichTextCtrl::MoveHome(int flags
)
1514 if (m_caretPosition
!= -1)
1516 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1520 SetCaretPosition(-1);
1522 SetDefaultStyleToCursorStyle();
1532 /// Move to the end of the buffer
1533 bool wxRichTextCtrl::MoveEnd(int flags
)
1535 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1537 if (m_caretPosition
!= endPos
)
1539 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1543 SetCaretPosition(endPos
);
1545 SetDefaultStyleToCursorStyle();
1555 /// Move noPages pages up
1556 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1558 return PageDown(- noPages
, flags
);
1561 /// Move noPages pages down
1562 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1564 // Calculate which line occurs noPages * screen height further down.
1565 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1568 wxSize clientSize
= GetClientSize();
1569 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1571 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1574 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1575 long pos
= lineRange
.GetStart()-1;
1576 if (pos
!= m_caretPosition
)
1578 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1580 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1584 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1586 SetDefaultStyleToCursorStyle();
1598 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1600 return str
== wxT(" ") || str
== wxT("\t");
1603 // Finds the caret position for the next word
1604 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1606 long endPos
= GetBuffer().GetRange().GetEnd();
1610 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1612 // First skip current text to space
1613 while (i
< endPos
&& i
> -1)
1615 // i is in character, not caret positions
1616 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1617 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1618 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1622 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1629 while (i
< endPos
&& i
> -1)
1631 // i is in character, not caret positions
1632 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1633 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1634 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1635 return wxMax(-1, i
);
1637 if (text
.empty()) // End of paragraph, or maybe an image
1638 return wxMax(-1, i
- 1);
1639 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1643 // Convert to caret position
1644 return wxMax(-1, i
- 1);
1653 long i
= m_caretPosition
;
1655 // First skip white space
1656 while (i
< endPos
&& i
> -1)
1658 // i is in character, not caret positions
1659 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1660 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1662 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1664 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1669 // Next skip current text to space
1670 while (i
< endPos
&& i
> -1)
1672 // i is in character, not caret positions
1673 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1674 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1675 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1678 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1691 /// Move n words left
1692 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1694 long pos
= FindNextWordPosition(-1);
1695 if (pos
!= m_caretPosition
)
1697 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1699 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1703 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1705 SetDefaultStyleToCursorStyle();
1715 /// Move n words right
1716 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1718 long pos
= FindNextWordPosition(1);
1719 if (pos
!= m_caretPosition
)
1721 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1723 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1727 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1729 SetDefaultStyleToCursorStyle();
1740 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1742 // Only do sizing optimization for large buffers
1743 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1745 m_fullLayoutRequired
= true;
1746 m_fullLayoutTime
= wxGetLocalTimeMillis();
1747 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1748 LayoutContent(true /* onlyVisibleRect */);
1751 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1753 #if wxRICHTEXT_BUFFERED_PAINTING
1761 /// Idle-time processing
1762 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1764 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1766 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1768 m_fullLayoutRequired
= false;
1769 m_fullLayoutTime
= 0;
1770 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1771 ShowPosition(m_fullLayoutSavedPosition
);
1775 if (m_caretPositionForDefaultStyle
!= -2)
1777 // If the caret position has changed, no longer reflect the default style
1779 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1780 m_caretPositionForDefaultStyle
= -2;
1787 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1793 /// Set up scrollbars, e.g. after a resize
1794 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1799 if (GetBuffer().IsEmpty())
1801 SetScrollbars(0, 0, 0, 0, 0, 0);
1805 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1806 // of pixels. See e.g. wxVScrolledWindow for ideas.
1807 int pixelsPerUnit
= 5;
1808 wxSize clientSize
= GetClientSize();
1810 int maxHeight
= GetBuffer().GetCachedSize().y
;
1812 // Round up so we have at least maxHeight pixels
1813 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1815 int startX
= 0, startY
= 0;
1817 GetViewStart(& startX
, & startY
);
1819 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1820 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1822 // Move to previous scroll position if
1824 SetScrollbars(0, pixelsPerUnit
,
1826 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1829 /// Paint the background
1830 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1832 wxColour backgroundColour
= GetBackgroundColour();
1833 if (!backgroundColour
.Ok())
1834 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1836 // Clear the background
1837 dc
.SetBrush(wxBrush(backgroundColour
));
1838 dc
.SetPen(*wxTRANSPARENT_PEN
);
1839 wxRect
windowRect(GetClientSize());
1840 windowRect
.x
-= 2; windowRect
.y
-= 2;
1841 windowRect
.width
+= 4; windowRect
.height
+= 4;
1843 // We need to shift the rectangle to take into account
1844 // scrolling. Converting device to logical coordinates.
1845 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1846 dc
.DrawRectangle(windowRect
);
1849 #if wxRICHTEXT_BUFFERED_PAINTING
1850 /// Recreate buffer bitmap if necessary
1851 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1854 if (sz
== wxDefaultSize
)
1855 sz
= GetClientSize();
1857 if (sz
.x
< 1 || sz
.y
< 1)
1860 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1861 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1862 return m_bufferBitmap
.Ok();
1866 // ----------------------------------------------------------------------------
1867 // file IO functions
1868 // ----------------------------------------------------------------------------
1870 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1872 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1874 m_filename
= filename
;
1877 SetInsertionPoint(0);
1880 SetupScrollbars(true);
1882 SendTextUpdatedEvent();
1888 wxLogError(_("File couldn't be loaded."));
1894 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1896 if (GetBuffer().SaveFile(filename
, fileType
))
1898 m_filename
= filename
;
1905 wxLogError(_("The text couldn't be saved."));
1910 // ----------------------------------------------------------------------------
1911 // wxRichTextCtrl specific functionality
1912 // ----------------------------------------------------------------------------
1914 /// Add a new paragraph of text to the end of the buffer
1915 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1917 return GetBuffer().AddParagraph(text
);
1921 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1923 return GetBuffer().AddImage(image
);
1926 // ----------------------------------------------------------------------------
1927 // selection and ranges
1928 // ----------------------------------------------------------------------------
1930 void wxRichTextCtrl::SelectAll()
1932 SetSelection(0, GetLastPosition()+1);
1933 m_selectionAnchor
= -1;
1937 void wxRichTextCtrl::SelectNone()
1939 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1940 SetSelection(-2, -2);
1941 m_selectionAnchor
= -2;
1944 static bool wxIsWordDelimiter(const wxString
& text
)
1946 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1949 /// Select the word at the given character position
1950 bool wxRichTextCtrl::SelectWord(long position
)
1952 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1955 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1959 long positionStart
= position
;
1960 long positionEnd
= position
;
1962 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1964 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1965 if (wxIsWordDelimiter(text
))
1971 if (positionStart
< para
->GetRange().GetStart())
1972 positionStart
= para
->GetRange().GetStart();
1974 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1976 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1977 if (wxIsWordDelimiter(text
))
1983 if (positionEnd
>= para
->GetRange().GetEnd())
1984 positionEnd
= para
->GetRange().GetEnd();
1986 SetSelection(positionStart
, positionEnd
+1);
1988 if (positionStart
>= 0)
1990 MoveCaret(positionStart
-1, true);
1991 SetDefaultStyleToCursorStyle();
1997 wxString
wxRichTextCtrl::GetStringSelection() const
2000 GetSelection(&from
, &to
);
2002 return GetRange(from
, to
);
2005 // ----------------------------------------------------------------------------
2007 // ----------------------------------------------------------------------------
2009 wxTextCtrlHitTestResult
2010 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2012 // implement in terms of the other overload as the native ports typically
2013 // can get the position and not (x, y) pair directly (although wxUniv
2014 // directly gets x and y -- and so overrides this method as well)
2016 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2018 if ( rc
!= wxTE_HT_UNKNOWN
)
2020 PositionToXY(pos
, x
, y
);
2026 wxTextCtrlHitTestResult
2027 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2030 wxClientDC
dc((wxRichTextCtrl
*) this);
2031 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2033 // Buffer uses logical position (relative to start of buffer)
2035 wxPoint pt2
= GetLogicalPoint(pt
);
2037 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2039 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2040 return wxTE_HT_BEFORE
;
2041 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2042 return wxTE_HT_BEYOND
;
2043 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2044 return wxTE_HT_ON_TEXT
;
2046 return wxTE_HT_UNKNOWN
;
2049 // ----------------------------------------------------------------------------
2050 // set/get the controls text
2051 // ----------------------------------------------------------------------------
2053 wxString
wxRichTextCtrl::GetValue() const
2055 return GetBuffer().GetText();
2058 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2060 // Public API for range is different from internals
2061 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2064 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2068 if (!value
.IsEmpty())
2070 // Remove empty paragraph
2071 GetBuffer().Clear();
2074 // for compatibility, don't move the cursor when doing SetValue()
2075 SetInsertionPoint(0);
2079 // still send an event for consistency
2080 if (flags
& SetValue_SendEvent
)
2081 SendTextUpdatedEvent();
2086 void wxRichTextCtrl::WriteText(const wxString
& value
)
2091 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2093 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2095 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2097 if ( flags
& SetValue_SendEvent
)
2098 SendTextUpdatedEvent();
2101 void wxRichTextCtrl::AppendText(const wxString
& text
)
2103 SetInsertionPointEnd();
2108 /// Write an image at the current insertion point
2109 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2111 wxRichTextImageBlock imageBlock
;
2113 wxImage image2
= image
;
2114 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2115 return WriteImage(imageBlock
);
2120 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2122 wxRichTextImageBlock imageBlock
;
2125 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2126 return WriteImage(imageBlock
);
2131 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2133 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2136 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2140 wxRichTextImageBlock imageBlock
;
2142 wxImage image
= bitmap
.ConvertToImage();
2143 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2144 return WriteImage(imageBlock
);
2150 /// Insert a newline (actually paragraph) at the current insertion point.
2151 bool wxRichTextCtrl::Newline()
2153 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2156 /// Insert a line break at the current insertion point.
2157 bool wxRichTextCtrl::LineBreak()
2160 text
= wxRichTextLineBreakChar
;
2161 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2164 // ----------------------------------------------------------------------------
2165 // Clipboard operations
2166 // ----------------------------------------------------------------------------
2168 void wxRichTextCtrl::Copy()
2172 wxRichTextRange range
= GetInternalSelectionRange();
2173 GetBuffer().CopyToClipboard(range
);
2177 void wxRichTextCtrl::Cut()
2181 wxRichTextRange range
= GetInternalSelectionRange();
2182 GetBuffer().CopyToClipboard(range
);
2184 DeleteSelectedContent();
2190 void wxRichTextCtrl::Paste()
2194 BeginBatchUndo(_("Paste"));
2196 long newPos
= m_caretPosition
;
2197 DeleteSelectedContent(& newPos
);
2199 GetBuffer().PasteFromClipboard(newPos
);
2205 void wxRichTextCtrl::DeleteSelection()
2207 if (CanDeleteSelection())
2209 DeleteSelectedContent();
2213 bool wxRichTextCtrl::HasSelection() const
2215 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2218 bool wxRichTextCtrl::CanCopy() const
2220 // Can copy if there's a selection
2221 return HasSelection();
2224 bool wxRichTextCtrl::CanCut() const
2226 return HasSelection() && IsEditable();
2229 bool wxRichTextCtrl::CanPaste() const
2231 if ( !IsEditable() )
2234 return GetBuffer().CanPasteFromClipboard();
2237 bool wxRichTextCtrl::CanDeleteSelection() const
2239 return HasSelection() && IsEditable();
2243 // ----------------------------------------------------------------------------
2245 // ----------------------------------------------------------------------------
2247 void wxRichTextCtrl::SetEditable(bool editable
)
2249 m_editable
= editable
;
2252 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2256 m_caretPosition
= pos
- 1;
2259 void wxRichTextCtrl::SetInsertionPointEnd()
2261 long pos
= GetLastPosition();
2262 SetInsertionPoint(pos
);
2265 long wxRichTextCtrl::GetInsertionPoint() const
2267 return m_caretPosition
+1;
2270 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2272 return GetBuffer().GetRange().GetEnd();
2275 // If the return values from and to are the same, there is no
2277 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2279 *from
= m_selectionRange
.GetStart();
2280 *to
= m_selectionRange
.GetEnd();
2281 if ((*to
) != -1 && (*to
) != -2)
2285 bool wxRichTextCtrl::IsEditable() const
2290 // ----------------------------------------------------------------------------
2292 // ----------------------------------------------------------------------------
2294 void wxRichTextCtrl::SetSelection(long from
, long to
)
2296 // if from and to are both -1, it means (in wxWidgets) that all text should
2298 if ( (from
== -1) && (to
== -1) )
2301 to
= GetLastPosition()+1;
2304 DoSetSelection(from
, to
);
2307 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2309 m_selectionAnchor
= from
;
2310 m_selectionRange
.SetRange(from
, to
-1);
2312 m_caretPosition
= from
-1;
2318 // ----------------------------------------------------------------------------
2320 // ----------------------------------------------------------------------------
2322 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2323 const wxString
& value
)
2325 BeginBatchUndo(_("Replace"));
2327 DeleteSelectedContent();
2329 DoWriteText(value
, SetValue_SelectionOnly
);
2334 void wxRichTextCtrl::Remove(long from
, long to
)
2338 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2345 bool wxRichTextCtrl::IsModified() const
2347 return m_buffer
.IsModified();
2350 void wxRichTextCtrl::MarkDirty()
2352 m_buffer
.Modify(true);
2355 void wxRichTextCtrl::DiscardEdits()
2357 m_caretPositionForDefaultStyle
= -2;
2358 m_buffer
.Modify(false);
2359 m_buffer
.GetCommandProcessor()->ClearCommands();
2362 int wxRichTextCtrl::GetNumberOfLines() const
2364 return GetBuffer().GetParagraphCount();
2367 // ----------------------------------------------------------------------------
2368 // Positions <-> coords
2369 // ----------------------------------------------------------------------------
2371 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2373 return GetBuffer().XYToPosition(x
, y
);
2376 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2378 return GetBuffer().PositionToXY(pos
, x
, y
);
2381 // ----------------------------------------------------------------------------
2383 // ----------------------------------------------------------------------------
2385 void wxRichTextCtrl::ShowPosition(long pos
)
2387 if (!IsPositionVisible(pos
))
2388 ScrollIntoView(pos
-1, WXK_DOWN
);
2391 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2393 return GetBuffer().GetParagraphLength(lineNo
);
2396 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2398 return GetBuffer().GetParagraphText(lineNo
);
2401 // ----------------------------------------------------------------------------
2403 // ----------------------------------------------------------------------------
2405 void wxRichTextCtrl::Undo()
2409 GetCommandProcessor()->Undo();
2413 void wxRichTextCtrl::Redo()
2417 GetCommandProcessor()->Redo();
2421 bool wxRichTextCtrl::CanUndo() const
2423 return GetCommandProcessor()->CanUndo();
2426 bool wxRichTextCtrl::CanRedo() const
2428 return GetCommandProcessor()->CanRedo();
2431 // ----------------------------------------------------------------------------
2432 // implementation details
2433 // ----------------------------------------------------------------------------
2435 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2437 SetValue(event
.GetString());
2438 GetEventHandler()->ProcessEvent(event
);
2441 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2443 // By default, load the first file into the text window.
2444 if (event
.GetNumberOfFiles() > 0)
2446 LoadFile(event
.GetFiles()[0]);
2450 wxSize
wxRichTextCtrl::DoGetBestSize() const
2452 return wxSize(10, 10);
2455 // ----------------------------------------------------------------------------
2456 // standard handlers for standard edit menu events
2457 // ----------------------------------------------------------------------------
2459 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2464 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2469 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2474 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2479 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2484 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2489 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2491 event
.Enable( CanCut() );
2494 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2496 event
.Enable( CanCopy() );
2499 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2501 event
.Enable( CanDeleteSelection() );
2504 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2506 event
.Enable( CanPaste() );
2509 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2511 event
.Enable( CanUndo() );
2512 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2515 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2517 event
.Enable( CanRedo() );
2518 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2521 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2526 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2528 event
.Enable(GetLastPosition() > 0);
2531 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2533 if (event
.GetEventObject() != this)
2541 m_contextMenu
= new wxMenu
;
2542 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2543 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2544 m_contextMenu
->AppendSeparator();
2545 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2546 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2547 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2548 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2549 m_contextMenu
->AppendSeparator();
2550 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2552 PopupMenu(m_contextMenu
);
2556 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2558 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2561 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2563 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2566 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2568 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2571 // extended style setting operation with flags including:
2572 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2573 // see richtextbuffer.h for more details.
2574 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2576 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2579 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2581 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2584 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2586 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2589 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2591 return GetBuffer().SetDefaultStyle(style
);
2594 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2596 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2599 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2601 return GetBuffer().GetDefaultStyle();
2604 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2606 return GetBuffer().GetDefaultStyle();
2609 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2611 wxTextAttrEx
attr(style
);
2612 if (GetBuffer().GetStyle(position
, attr
))
2621 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2623 return GetBuffer().GetStyle(position
, style
);
2626 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2628 return GetBuffer().GetStyle(position
, style
);
2631 // get the common set of styles for the range
2632 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2634 wxTextAttrEx styleEx
;
2635 if (GetBuffer().GetStyleForRange(range
.ToInternal(), styleEx
))
2644 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2646 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2649 /// Get the content (uncombined) attributes for this position.
2651 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2653 wxTextAttrEx
attr(style
);
2654 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2663 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2665 return GetBuffer().GetUncombinedStyle(position
, style
);
2668 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2670 return GetBuffer().GetUncombinedStyle(position
, style
);
2673 /// Set font, and also the buffer attributes
2674 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2676 wxControl::SetFont(font
);
2678 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2680 GetBuffer().SetBasicStyle(attr
);
2682 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2688 /// Transform logical to physical
2689 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2692 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2697 /// Transform physical to logical
2698 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2701 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2706 /// Position the caret
2707 void wxRichTextCtrl::PositionCaret()
2712 //wxLogDebug(wxT("PositionCaret"));
2715 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2717 wxPoint newPt
= caretRect
.GetPosition();
2718 wxSize newSz
= caretRect
.GetSize();
2719 wxPoint pt
= GetPhysicalPoint(newPt
);
2720 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2722 GetCaret()->Move(pt
);
2723 GetCaret()->SetSize(newSz
);
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();