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
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
)
59 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
61 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
63 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
64 EVT_PAINT(wxRichTextCtrl::OnPaint
)
65 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
66 EVT_IDLE(wxRichTextCtrl::OnIdle
)
67 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
68 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
69 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
70 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
71 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
72 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
73 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
74 EVT_CHAR(wxRichTextCtrl::OnChar
)
75 EVT_SIZE(wxRichTextCtrl::OnSize
)
76 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
77 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
78 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
79 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
80 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
82 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
83 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
85 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
86 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
88 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
89 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
91 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
92 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
94 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
95 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
97 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
98 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
100 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
101 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
108 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
110 wxRichTextCtrl::wxRichTextCtrl()
111 : wxScrollHelper(this)
116 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
118 const wxString
& value
,
122 const wxValidator
& validator
,
123 const wxString
& name
)
124 : wxScrollHelper(this)
127 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
131 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
132 const wxValidator
& validator
, const wxString
& name
)
134 if (!wxControl::Create(parent
, id
, pos
, size
,
135 style
|wxFULL_REPAINT_ON_RESIZE
,
141 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
144 if (style
& wxTE_READONLY
)
147 // The base attributes must all have default values
148 wxTextAttr attributes
;
149 attributes
.SetFont(GetFont());
150 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
151 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
152 attributes
.SetLineSpacing(10);
153 attributes
.SetParagraphSpacingAfter(10);
154 attributes
.SetParagraphSpacingBefore(0);
155 attributes
.SetTextEffects(0);
156 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
158 SetBasicStyle(attributes
);
160 // The default attributes will be merged with base attributes, so
161 // can be empty to begin with
162 wxTextAttr defaultAttributes
;
163 SetDefaultStyle(defaultAttributes
);
165 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
166 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
169 GetBuffer().SetRichTextCtrl(this);
171 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
174 // Tell the sizers to use the given or best size
175 SetInitialSize(size
);
177 #if wxRICHTEXT_BUFFERED_PAINTING
179 RecreateBuffer(size
);
182 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
183 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
185 SetCursor(m_textCursor
);
187 if (!value
.IsEmpty())
190 GetBuffer().AddEventHandler(this);
195 wxRichTextCtrl::~wxRichTextCtrl()
197 GetBuffer().RemoveEventHandler(this);
199 delete m_contextMenu
;
202 /// Member initialisation
203 void wxRichTextCtrl::Init()
205 m_contextMenu
= NULL
;
207 m_caretPosition
= -1;
208 m_selectionRange
.SetRange(-2, -2);
209 m_selectionAnchor
= -2;
211 m_caretAtLineStart
= false;
213 m_fullLayoutRequired
= false;
214 m_fullLayoutTime
= 0;
215 m_fullLayoutSavedPosition
= 0;
216 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
217 m_caretPositionForDefaultStyle
= -2;
220 void wxRichTextCtrl::DoThaw()
222 if (GetBuffer().GetDirty())
230 void wxRichTextCtrl::Clear()
232 m_buffer
.ResetAndClearCommands();
233 m_buffer
.SetDirty(true);
234 m_caretPosition
= -1;
235 m_caretPositionForDefaultStyle
= -2;
236 m_caretAtLineStart
= false;
237 m_selectionRange
.SetRange(-2, -2);
247 wxTextCtrl::SendTextUpdatedEvent(this);
251 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
253 if (GetCaret() && GetCaret()->IsVisible())
257 #if wxRICHTEXT_BUFFERED_PAINTING
258 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
268 dc
.SetFont(GetFont());
270 // Paint the background
273 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
275 wxRect
drawingArea(GetUpdateRegion().GetBox());
276 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
278 wxRect
availableSpace(GetClientSize());
279 if (GetBuffer().GetDirty())
281 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
282 GetBuffer().SetDirty(false);
286 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
289 if (GetCaret() && !GetCaret()->IsVisible())
295 // Empty implementation, to prevent flicker
296 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
300 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
304 if (!GetCaret()->IsVisible())
313 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
315 if (GetCaret() && GetCaret()->IsVisible())
322 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
328 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
334 dc
.SetFont(GetFont());
337 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
339 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
341 m_dragStart
= event
.GetLogicalPosition(dc
);
345 bool caretAtLineStart
= false;
347 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
349 // If we're at the start of a line (but not first in para)
350 // then we should keep the caret showing at the start of the line
351 // by showing the m_caretAtLineStart flag.
352 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
353 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
355 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
356 caretAtLineStart
= true;
360 long oldCaretPos
= m_caretPosition
;
362 MoveCaret(position
, caretAtLineStart
);
363 SetDefaultStyleToCursorStyle();
365 if (event
.ShiftDown())
367 bool extendSel
= false;
368 if (m_selectionRange
.GetStart() == -2)
369 extendSel
= ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
371 extendSel
= ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
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
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
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
& event
)
519 wxRichTextEvent
cmdEvent(
520 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
522 cmdEvent
.SetEventObject(this);
523 cmdEvent
.SetPosition(m_caretPosition
+1);
525 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
529 /// Left-double-click
530 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
532 wxRichTextEvent
cmdEvent(
533 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
535 cmdEvent
.SetEventObject(this);
536 cmdEvent
.SetPosition(m_caretPosition
+1);
538 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
540 SelectWord(GetCaretPosition()+1);
545 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
547 wxRichTextEvent
cmdEvent(
548 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
550 cmdEvent
.SetEventObject(this);
551 cmdEvent
.SetPosition(m_caretPosition
+1);
553 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
558 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
562 flags
|= wxRICHTEXT_CTRL_DOWN
;
563 if (event
.ShiftDown())
564 flags
|= wxRICHTEXT_SHIFT_DOWN
;
566 flags
|= wxRICHTEXT_ALT_DOWN
;
568 if (event
.GetKeyCode() == WXK_LEFT
||
569 event
.GetKeyCode() == WXK_RIGHT
||
570 event
.GetKeyCode() == WXK_UP
||
571 event
.GetKeyCode() == WXK_DOWN
||
572 event
.GetKeyCode() == WXK_HOME
||
573 event
.GetKeyCode() == WXK_PAGEUP
||
574 event
.GetKeyCode() == WXK_PAGEDOWN
||
575 event
.GetKeyCode() == WXK_END
||
577 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
578 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
579 event
.GetKeyCode() == WXK_NUMPAD_UP
||
580 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
581 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
582 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
583 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
584 event
.GetKeyCode() == WXK_NUMPAD_END
)
586 KeyboardNavigate(event
.GetKeyCode(), flags
);
590 // all the other keys modify the controls contents which shouldn't be
591 // possible if we're read-only
598 if (event
.GetKeyCode() == WXK_RETURN
)
600 BeginBatchUndo(_("Insert Text"));
602 long newPos
= m_caretPosition
;
604 DeleteSelectedContent(& newPos
);
606 if (event
.ShiftDown())
609 text
= wxRichTextLineBreakChar
;
610 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
613 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
616 SetDefaultStyleToCursorStyle();
618 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
620 wxRichTextEvent
cmdEvent(
621 wxEVT_COMMAND_RICHTEXT_RETURN
,
623 cmdEvent
.SetEventObject(this);
624 cmdEvent
.SetFlags(flags
);
625 cmdEvent
.SetPosition(newPos
+1);
627 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
629 // Generate conventional event
630 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
631 InitCommandEvent(textEvent
);
633 GetEventHandler()->ProcessEvent(textEvent
);
637 else if (event
.GetKeyCode() == WXK_BACK
)
639 BeginBatchUndo(_("Delete Text"));
641 // Submit range in character positions, which are greater than caret positions,
642 // so subtract 1 for deleted character and add 1 for conversion to character position.
643 if (m_caretPosition
> -1 && !HasSelection())
645 bool processed
= false;
648 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
649 if (pos
!= -1 && (pos
< m_caretPosition
))
651 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos
+1, m_caretPosition
), this);
657 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
660 DeleteSelectedContent();
664 if (GetLastPosition() == -1)
668 m_caretPosition
= -1;
670 SetDefaultStyleToCursorStyle();
673 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
675 wxRichTextEvent
cmdEvent(
676 wxEVT_COMMAND_RICHTEXT_DELETE
,
678 cmdEvent
.SetEventObject(this);
679 cmdEvent
.SetFlags(flags
);
680 cmdEvent
.SetPosition(m_caretPosition
+1);
681 GetEventHandler()->ProcessEvent(cmdEvent
);
685 else if (event
.GetKeyCode() == WXK_DELETE
)
687 BeginBatchUndo(_("Delete Text"));
689 // Submit range in character positions, which are greater than caret positions,
690 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
692 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
695 DeleteSelectedContent();
699 if (GetLastPosition() == -1)
703 m_caretPosition
= -1;
705 SetDefaultStyleToCursorStyle();
708 wxRichTextEvent
cmdEvent(
709 wxEVT_COMMAND_RICHTEXT_DELETE
,
711 cmdEvent
.SetEventObject(this);
712 cmdEvent
.SetFlags(flags
);
713 cmdEvent
.SetPosition(m_caretPosition
+1);
714 GetEventHandler()->ProcessEvent(cmdEvent
);
720 long keycode
= event
.GetKeyCode();
793 case WXK_NUMPAD_SPACE
:
795 case WXK_NUMPAD_ENTER
:
800 case WXK_NUMPAD_HOME
:
801 case WXK_NUMPAD_LEFT
:
803 case WXK_NUMPAD_RIGHT
:
804 case WXK_NUMPAD_DOWN
:
805 case WXK_NUMPAD_PAGEUP
:
806 case WXK_NUMPAD_PAGEDOWN
:
808 case WXK_NUMPAD_BEGIN
:
809 case WXK_NUMPAD_INSERT
:
810 case WXK_NUMPAD_DELETE
:
811 case WXK_NUMPAD_EQUAL
:
812 case WXK_NUMPAD_MULTIPLY
:
814 case WXK_NUMPAD_SEPARATOR
:
815 case WXK_NUMPAD_SUBTRACT
:
816 case WXK_NUMPAD_DECIMAL
:
817 case WXK_WINDOWS_LEFT
:
825 if (event
.CmdDown() || event
.AltDown())
831 wxRichTextEvent
cmdEvent(
832 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
834 cmdEvent
.SetEventObject(this);
835 cmdEvent
.SetFlags(flags
);
837 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
839 cmdEvent
.SetCharacter((wxChar
) keycode
);
841 cmdEvent
.SetPosition(m_caretPosition
+1);
843 if (keycode
== wxT('\t'))
845 // See if we need to promote or demote the selection or paragraph at the cursor
846 // position, instead of inserting a tab.
847 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
848 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
849 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
851 wxRichTextRange range
;
853 range
= GetSelectionRange();
855 range
= para
->GetRange().FromInternal();
857 int promoteBy
= event
.ShiftDown() ? 1 : -1;
859 PromoteList(promoteBy
, range
, NULL
);
861 GetEventHandler()->ProcessEvent(cmdEvent
);
867 BeginBatchUndo(_("Insert Text"));
869 long newPos
= m_caretPosition
;
870 DeleteSelectedContent(& newPos
);
873 wxString str
= event
.GetUnicodeKey();
875 wxString str
= (wxChar
) event
.GetKeyCode();
877 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
881 SetDefaultStyleToCursorStyle();
882 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
884 GetEventHandler()->ProcessEvent(cmdEvent
);
892 /// Delete content if there is a selection, e.g. when pressing a key.
893 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
897 long pos
= m_selectionRange
.GetStart();
898 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
899 m_selectionRange
.SetRange(-2, -2);
909 /// Keyboard navigation
913 Left: left one character
914 Right: right one character
917 Ctrl-Left: left one word
918 Ctrl-Right: right one word
919 Ctrl-Up: previous paragraph start
920 Ctrl-Down: next start of paragraph
923 Ctrl-Home: start of document
924 Ctrl-End: end of document
926 Page-Down: Down a screen
930 Ctrl-Alt-PgUp: Start of window
931 Ctrl-Alt-PgDn: End of window
932 F8: Start selection mode
933 Esc: End selection mode
935 Adding Shift does the above but starts/extends selection.
940 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
942 bool success
= false;
944 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
946 if (flags
& wxRICHTEXT_CTRL_DOWN
)
947 success
= WordRight(1, flags
);
949 success
= MoveRight(1, flags
);
951 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
953 if (flags
& wxRICHTEXT_CTRL_DOWN
)
954 success
= WordLeft(1, flags
);
956 success
= MoveLeft(1, flags
);
958 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
960 if (flags
& wxRICHTEXT_CTRL_DOWN
)
961 success
= MoveToParagraphStart(flags
);
963 success
= MoveUp(1, flags
);
965 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
967 if (flags
& wxRICHTEXT_CTRL_DOWN
)
968 success
= MoveToParagraphEnd(flags
);
970 success
= MoveDown(1, flags
);
972 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
974 success
= PageUp(1, flags
);
976 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
978 success
= PageDown(1, flags
);
980 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
982 if (flags
& wxRICHTEXT_CTRL_DOWN
)
983 success
= MoveHome(flags
);
985 success
= MoveToLineStart(flags
);
987 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
989 if (flags
& wxRICHTEXT_CTRL_DOWN
)
990 success
= MoveEnd(flags
);
992 success
= MoveToLineEnd(flags
);
997 ScrollIntoView(m_caretPosition
, keyCode
);
998 SetDefaultStyleToCursorStyle();
1004 /// Extend the selection. Selections are in caret positions.
1005 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1007 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1009 // If not currently selecting, start selecting
1010 if (m_selectionRange
.GetStart() == -2)
1012 m_selectionAnchor
= oldPos
;
1014 if (oldPos
> newPos
)
1015 m_selectionRange
.SetRange(newPos
+1, oldPos
);
1017 m_selectionRange
.SetRange(oldPos
+1, newPos
);
1021 // Always ensure that the selection range start is greater than
1023 if (newPos
> m_selectionAnchor
)
1024 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
1026 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
1029 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
1031 wxLogDebug(wxT("Strange selection range"));
1040 /// Scroll into view, returning true if we scrolled.
1041 /// This takes a _caret_ position.
1042 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1044 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1050 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1052 int startXUnits
, startYUnits
;
1053 GetViewStart(& startXUnits
, & startYUnits
);
1054 int startY
= startYUnits
* ppuY
;
1057 GetVirtualSize(& sx
, & sy
);
1063 wxRect rect
= line
->GetRect();
1065 bool scrolled
= false;
1067 wxSize clientSize
= GetClientSize();
1070 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1071 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1072 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1073 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1075 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1077 // Make it scroll so this item is at the bottom
1079 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1080 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1082 // If we're still off the screen, scroll another line down
1083 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1086 if (startYUnits
!= yUnits
)
1088 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1092 else 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
);
1107 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1108 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1109 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1110 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1112 if (rect
.y
< startY
)
1114 // Make it scroll so this item is at the top
1117 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1119 if (startYUnits
!= yUnits
)
1121 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1125 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1127 // Make it scroll so this item is at the bottom
1129 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1130 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1132 // If we're still off the screen, scroll another line down
1133 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1136 if (startYUnits
!= yUnits
)
1138 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1148 /// Is the given position visible on the screen?
1149 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1151 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1157 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1160 GetViewStart(& startX
, & startY
);
1162 startY
= startY
* ppuY
;
1164 wxRect rect
= line
->GetRect();
1165 wxSize clientSize
= GetClientSize();
1167 return (rect
.GetBottom() > startY
) && (rect
.GetTop() < (startY
+ clientSize
.y
));
1170 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1172 m_caretPosition
= position
;
1173 m_caretAtLineStart
= showAtLineStart
;
1176 /// Move caret one visual step forward: this may mean setting a flag
1177 /// and keeping the same position if we're going from the end of one line
1178 /// to the start of the next, which may be the exact same caret position.
1179 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1181 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1183 // Only do the check if we're not at the end of the paragraph (where things work OK
1185 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1187 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1191 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1193 // We're at the end of a line. See whether we need to
1194 // stay at the same actual caret position but change visual
1195 // position, or not.
1196 if (oldPosition
== lineRange
.GetEnd())
1198 if (m_caretAtLineStart
)
1200 // We're already at the start of the line, so actually move on now.
1201 m_caretPosition
= oldPosition
+ 1;
1202 m_caretAtLineStart
= false;
1206 // We're showing at the end of the line, so keep to
1207 // the same position but indicate that we're to show
1208 // at the start of the next line.
1209 m_caretPosition
= oldPosition
;
1210 m_caretAtLineStart
= true;
1212 SetDefaultStyleToCursorStyle();
1218 SetDefaultStyleToCursorStyle();
1221 /// Move caret one visual step backward: this may mean setting a flag
1222 /// and keeping the same position if we're going from the end of one line
1223 /// to the start of the next, which may be the exact same caret position.
1224 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1226 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1228 // Only do the check if we're not at the start of the paragraph (where things work OK
1230 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1232 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1236 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1238 // We're at the start of a line. See whether we need to
1239 // stay at the same actual caret position but change visual
1240 // position, or not.
1241 if (oldPosition
== lineRange
.GetStart())
1243 m_caretPosition
= oldPosition
-1;
1244 m_caretAtLineStart
= true;
1247 else if (oldPosition
== lineRange
.GetEnd())
1249 if (m_caretAtLineStart
)
1251 // We're at the start of the line, so keep the same caret position
1252 // but clear the start-of-line flag.
1253 m_caretPosition
= oldPosition
;
1254 m_caretAtLineStart
= false;
1258 // We're showing at the end of the line, so go back
1259 // to the previous character position.
1260 m_caretPosition
= oldPosition
- 1;
1262 SetDefaultStyleToCursorStyle();
1268 SetDefaultStyleToCursorStyle();
1272 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1274 long endPos
= GetBuffer().GetRange().GetEnd();
1276 if (m_caretPosition
+ noPositions
< endPos
)
1278 long oldPos
= m_caretPosition
;
1279 long newPos
= m_caretPosition
+ noPositions
;
1281 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1285 // Determine by looking at oldPos and m_caretPosition whether
1286 // we moved from the end of a line to the start of the next line, in which case
1287 // we want to adjust the caret position such that it is positioned at the
1288 // start of the next line, rather than jumping past the first character of the
1290 if (noPositions
== 1 && !extendSel
)
1291 MoveCaretForward(oldPos
);
1293 SetCaretPosition(newPos
);
1296 SetDefaultStyleToCursorStyle();
1307 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1311 if (m_caretPosition
> startPos
- noPositions
+ 1)
1313 long oldPos
= m_caretPosition
;
1314 long newPos
= m_caretPosition
- noPositions
;
1315 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1319 if (noPositions
== 1 && !extendSel
)
1320 MoveCaretBack(oldPos
);
1322 SetCaretPosition(newPos
);
1325 SetDefaultStyleToCursorStyle();
1336 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1338 return MoveDown(- noLines
, flags
);
1342 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1347 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1348 wxPoint pt
= GetCaret()->GetPosition();
1349 long newLine
= lineNumber
+ noLines
;
1351 if (lineNumber
!= -1)
1355 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1357 if (newLine
> lastLine
)
1367 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1370 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1376 wxClientDC
dc(this);
1378 dc
.SetFont(GetFont());
1380 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1382 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1384 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1385 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1386 // so we view the caret at the start of the line.
1387 bool caretLineStart
= false;
1388 if (hitTest
& wxRICHTEXT_HITTEST_BEFORE
)
1390 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1391 wxRichTextRange lineRange
;
1393 lineRange
= thisLine
->GetAbsoluteRange();
1395 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1398 caretLineStart
= true;
1402 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1403 if (para
&& para
->GetRange().GetStart() == newPos
)
1408 long newSelEnd
= newPos
;
1410 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1414 SetCaretPosition(newPos
, caretLineStart
);
1416 SetDefaultStyleToCursorStyle();
1426 /// Move to the end of the paragraph
1427 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1429 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1432 long newPos
= para
->GetRange().GetEnd() - 1;
1433 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1437 SetCaretPosition(newPos
);
1439 SetDefaultStyleToCursorStyle();
1449 /// Move to the start of the paragraph
1450 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1452 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1455 long newPos
= para
->GetRange().GetStart() - 1;
1456 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1460 SetCaretPosition(newPos
);
1462 SetDefaultStyleToCursorStyle();
1472 /// Move to the end of the line
1473 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1475 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1479 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1480 long newPos
= lineRange
.GetEnd();
1481 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1485 SetCaretPosition(newPos
);
1487 SetDefaultStyleToCursorStyle();
1497 /// Move to the start of the line
1498 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1500 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1503 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1504 long newPos
= lineRange
.GetStart()-1;
1506 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1510 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1512 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1514 SetDefaultStyleToCursorStyle();
1524 /// Move to the start of the buffer
1525 bool wxRichTextCtrl::MoveHome(int flags
)
1527 if (m_caretPosition
!= -1)
1529 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1533 SetCaretPosition(-1);
1535 SetDefaultStyleToCursorStyle();
1545 /// Move to the end of the buffer
1546 bool wxRichTextCtrl::MoveEnd(int flags
)
1548 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1550 if (m_caretPosition
!= endPos
)
1552 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1556 SetCaretPosition(endPos
);
1558 SetDefaultStyleToCursorStyle();
1568 /// Move noPages pages up
1569 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1571 return PageDown(- noPages
, flags
);
1574 /// Move noPages pages down
1575 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1577 // Calculate which line occurs noPages * screen height further down.
1578 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1581 wxSize clientSize
= GetClientSize();
1582 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1584 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1587 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1588 long pos
= lineRange
.GetStart()-1;
1589 if (pos
!= m_caretPosition
)
1591 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1593 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1597 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1599 SetDefaultStyleToCursorStyle();
1611 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
1613 return str
== wxT(" ") || str
== wxT("\t");
1616 // Finds the caret position for the next word
1617 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1619 long endPos
= GetBuffer().GetRange().GetEnd();
1623 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1625 // First skip current text to space
1626 while (i
< endPos
&& i
> -1)
1628 // i is in character, not caret positions
1629 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1630 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1631 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1635 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
1642 while (i
< endPos
&& i
> -1)
1644 // i is in character, not caret positions
1645 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1646 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1647 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
1648 return wxMax(-1, i
);
1650 if (text
.empty()) // End of paragraph, or maybe an image
1651 return wxMax(-1, i
- 1);
1652 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1656 // Convert to caret position
1657 return wxMax(-1, i
- 1);
1666 long i
= m_caretPosition
;
1668 // First skip white space
1669 while (i
< endPos
&& i
> -1)
1671 // i is in character, not caret positions
1672 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1673 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1675 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1677 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
1682 // Next skip current text to space
1683 while (i
< endPos
&& i
> -1)
1685 // i is in character, not caret positions
1686 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1687 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(i
, false);
1688 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
1691 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
1704 /// Move n words left
1705 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1707 long pos
= FindNextWordPosition(-1);
1708 if (pos
!= m_caretPosition
)
1710 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1712 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1716 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1718 SetDefaultStyleToCursorStyle();
1728 /// Move n words right
1729 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1731 long pos
= FindNextWordPosition(1);
1732 if (pos
!= m_caretPosition
)
1734 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1736 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1740 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1742 SetDefaultStyleToCursorStyle();
1753 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1755 // Only do sizing optimization for large buffers
1756 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1758 m_fullLayoutRequired
= true;
1759 m_fullLayoutTime
= wxGetLocalTimeMillis();
1760 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1761 LayoutContent(true /* onlyVisibleRect */);
1764 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1766 #if wxRICHTEXT_BUFFERED_PAINTING
1774 /// Idle-time processing
1775 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1777 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1779 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1781 m_fullLayoutRequired
= false;
1782 m_fullLayoutTime
= 0;
1783 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1784 ShowPosition(m_fullLayoutSavedPosition
);
1788 if (m_caretPositionForDefaultStyle
!= -2)
1790 // If the caret position has changed, no longer reflect the default style
1792 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1793 m_caretPositionForDefaultStyle
= -2;
1800 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1806 /// Set up scrollbars, e.g. after a resize
1807 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1812 if (GetBuffer().IsEmpty())
1814 SetScrollbars(0, 0, 0, 0, 0, 0);
1818 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1819 // of pixels. See e.g. wxVScrolledWindow for ideas.
1820 int pixelsPerUnit
= 5;
1821 wxSize clientSize
= GetClientSize();
1823 int maxHeight
= GetBuffer().GetCachedSize().y
;
1825 // Round up so we have at least maxHeight pixels
1826 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1828 int startX
= 0, startY
= 0;
1830 GetViewStart(& startX
, & startY
);
1832 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1833 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1835 // Move to previous scroll position if
1837 SetScrollbars(0, pixelsPerUnit
,
1839 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1842 /// Paint the background
1843 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1845 wxColour backgroundColour
= GetBackgroundColour();
1846 if (!backgroundColour
.Ok())
1847 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1849 // Clear the background
1850 dc
.SetBrush(wxBrush(backgroundColour
));
1851 dc
.SetPen(*wxTRANSPARENT_PEN
);
1852 wxRect
windowRect(GetClientSize());
1853 windowRect
.x
-= 2; windowRect
.y
-= 2;
1854 windowRect
.width
+= 4; windowRect
.height
+= 4;
1856 // We need to shift the rectangle to take into account
1857 // scrolling. Converting device to logical coordinates.
1858 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1859 dc
.DrawRectangle(windowRect
);
1862 #if wxRICHTEXT_BUFFERED_PAINTING
1863 /// Recreate buffer bitmap if necessary
1864 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1867 if (sz
== wxDefaultSize
)
1868 sz
= GetClientSize();
1870 if (sz
.x
< 1 || sz
.y
< 1)
1873 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1874 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1875 return m_bufferBitmap
.Ok();
1879 // ----------------------------------------------------------------------------
1880 // file IO functions
1881 // ----------------------------------------------------------------------------
1883 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1885 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1887 m_filename
= filename
;
1890 SetInsertionPoint(0);
1893 SetupScrollbars(true);
1895 wxTextCtrl::SendTextUpdatedEvent(this);
1901 wxLogError(_("File couldn't be loaded."));
1907 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1909 if (GetBuffer().SaveFile(filename
, fileType
))
1911 m_filename
= filename
;
1918 wxLogError(_("The text couldn't be saved."));
1923 // ----------------------------------------------------------------------------
1924 // wxRichTextCtrl specific functionality
1925 // ----------------------------------------------------------------------------
1927 /// Add a new paragraph of text to the end of the buffer
1928 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1930 wxRichTextRange range
= GetBuffer().AddParagraph(text
);
1936 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1938 wxRichTextRange range
= GetBuffer().AddImage(image
);
1943 // ----------------------------------------------------------------------------
1944 // selection and ranges
1945 // ----------------------------------------------------------------------------
1947 void wxRichTextCtrl::SelectAll()
1949 SetSelection(0, GetLastPosition()+1);
1950 m_selectionAnchor
= -1;
1954 void wxRichTextCtrl::SelectNone()
1956 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1959 m_selectionRange
= wxRichTextRange(-2, -2);
1961 m_selectionAnchor
= -2;
1964 static bool wxIsWordDelimiter(const wxString
& text
)
1966 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1969 /// Select the word at the given character position
1970 bool wxRichTextCtrl::SelectWord(long position
)
1972 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1975 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1979 if (position
== para
->GetRange().GetEnd())
1982 long positionStart
= position
;
1983 long positionEnd
= position
;
1985 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1987 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1988 if (wxIsWordDelimiter(text
))
1994 if (positionStart
< para
->GetRange().GetStart())
1995 positionStart
= para
->GetRange().GetStart();
1997 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1999 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2000 if (wxIsWordDelimiter(text
))
2006 if (positionEnd
>= para
->GetRange().GetEnd())
2007 positionEnd
= para
->GetRange().GetEnd();
2009 if (positionEnd
< positionStart
)
2012 SetSelection(positionStart
, positionEnd
+1);
2014 if (positionStart
>= 0)
2016 MoveCaret(positionStart
-1, true);
2017 SetDefaultStyleToCursorStyle();
2023 wxString
wxRichTextCtrl::GetStringSelection() const
2026 GetSelection(&from
, &to
);
2028 return GetRange(from
, to
);
2031 // ----------------------------------------------------------------------------
2033 // ----------------------------------------------------------------------------
2035 wxTextCtrlHitTestResult
2036 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2038 // implement in terms of the other overload as the native ports typically
2039 // can get the position and not (x, y) pair directly (although wxUniv
2040 // directly gets x and y -- and so overrides this method as well)
2042 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2044 if ( rc
!= wxTE_HT_UNKNOWN
)
2046 PositionToXY(pos
, x
, y
);
2052 wxTextCtrlHitTestResult
2053 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2056 wxClientDC
dc((wxRichTextCtrl
*) this);
2057 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2059 // Buffer uses logical position (relative to start of buffer)
2061 wxPoint pt2
= GetLogicalPoint(pt
);
2063 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
2065 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2066 return wxTE_HT_BEFORE
;
2067 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2068 return wxTE_HT_BEYOND
;
2069 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2070 return wxTE_HT_ON_TEXT
;
2072 return wxTE_HT_UNKNOWN
;
2075 // ----------------------------------------------------------------------------
2076 // set/get the controls text
2077 // ----------------------------------------------------------------------------
2079 wxString
wxRichTextCtrl::GetValue() const
2081 return GetBuffer().GetText();
2084 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2086 // Public API for range is different from internals
2087 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2090 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2092 // Don't call Clear here, since it always sends a text updated event
2093 m_buffer
.ResetAndClearCommands();
2094 m_buffer
.SetDirty(true);
2095 m_caretPosition
= -1;
2096 m_caretPositionForDefaultStyle
= -2;
2097 m_caretAtLineStart
= false;
2098 m_selectionRange
.SetRange(-2, -2);
2108 if (!value
.IsEmpty())
2110 // Remove empty paragraph
2111 GetBuffer().Clear();
2112 DoWriteText(value
, flags
);
2114 // for compatibility, don't move the cursor when doing SetValue()
2115 SetInsertionPoint(0);
2119 // still send an event for consistency
2120 if (flags
& SetValue_SendEvent
)
2121 wxTextCtrl::SendTextUpdatedEvent(this);
2126 void wxRichTextCtrl::WriteText(const wxString
& value
)
2131 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2133 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2135 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2137 if ( flags
& SetValue_SendEvent
)
2138 wxTextCtrl::SendTextUpdatedEvent(this);
2141 void wxRichTextCtrl::AppendText(const wxString
& text
)
2143 SetInsertionPointEnd();
2148 /// Write an image at the current insertion point
2149 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2151 wxRichTextImageBlock imageBlock
;
2153 wxImage image2
= image
;
2154 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2155 return WriteImage(imageBlock
);
2160 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2162 wxRichTextImageBlock imageBlock
;
2165 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2166 return WriteImage(imageBlock
);
2171 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2173 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2176 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2180 wxRichTextImageBlock imageBlock
;
2182 wxImage image
= bitmap
.ConvertToImage();
2183 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2184 return WriteImage(imageBlock
);
2190 /// Insert a newline (actually paragraph) at the current insertion point.
2191 bool wxRichTextCtrl::Newline()
2193 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2196 /// Insert a line break at the current insertion point.
2197 bool wxRichTextCtrl::LineBreak()
2200 text
= wxRichTextLineBreakChar
;
2201 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2204 // ----------------------------------------------------------------------------
2205 // Clipboard operations
2206 // ----------------------------------------------------------------------------
2208 void wxRichTextCtrl::Copy()
2212 wxRichTextRange range
= GetInternalSelectionRange();
2213 GetBuffer().CopyToClipboard(range
);
2217 void wxRichTextCtrl::Cut()
2221 wxRichTextRange range
= GetInternalSelectionRange();
2222 GetBuffer().CopyToClipboard(range
);
2224 DeleteSelectedContent();
2230 void wxRichTextCtrl::Paste()
2234 BeginBatchUndo(_("Paste"));
2236 long newPos
= m_caretPosition
;
2237 DeleteSelectedContent(& newPos
);
2239 GetBuffer().PasteFromClipboard(newPos
);
2245 void wxRichTextCtrl::DeleteSelection()
2247 if (CanDeleteSelection())
2249 DeleteSelectedContent();
2253 bool wxRichTextCtrl::HasSelection() const
2255 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2258 bool wxRichTextCtrl::CanCopy() const
2260 // Can copy if there's a selection
2261 return HasSelection();
2264 bool wxRichTextCtrl::CanCut() const
2266 return HasSelection() && IsEditable();
2269 bool wxRichTextCtrl::CanPaste() const
2271 if ( !IsEditable() )
2274 return GetBuffer().CanPasteFromClipboard();
2277 bool wxRichTextCtrl::CanDeleteSelection() const
2279 return HasSelection() && IsEditable();
2283 // ----------------------------------------------------------------------------
2285 // ----------------------------------------------------------------------------
2287 void wxRichTextCtrl::SetEditable(bool editable
)
2289 m_editable
= editable
;
2292 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2296 m_caretPosition
= pos
- 1;
2301 void wxRichTextCtrl::SetInsertionPointEnd()
2303 long pos
= GetLastPosition();
2304 SetInsertionPoint(pos
);
2307 long wxRichTextCtrl::GetInsertionPoint() const
2309 return m_caretPosition
+1;
2312 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2314 return GetBuffer().GetRange().GetEnd();
2317 // If the return values from and to are the same, there is no
2319 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2321 *from
= m_selectionRange
.GetStart();
2322 *to
= m_selectionRange
.GetEnd();
2323 if ((*to
) != -1 && (*to
) != -2)
2327 bool wxRichTextCtrl::IsEditable() const
2332 // ----------------------------------------------------------------------------
2334 // ----------------------------------------------------------------------------
2336 void wxRichTextCtrl::SetSelection(long from
, long to
)
2338 // if from and to are both -1, it means (in wxWidgets) that all text should
2340 if ( (from
== -1) && (to
== -1) )
2343 to
= GetLastPosition()+1;
2346 DoSetSelection(from
, to
);
2347 SetDefaultStyleToCursorStyle();
2350 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2358 m_selectionAnchor
= from
;
2359 m_selectionRange
.SetRange(from
, to
-1);
2361 m_caretPosition
= from
-1;
2368 // ----------------------------------------------------------------------------
2370 // ----------------------------------------------------------------------------
2372 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2373 const wxString
& value
)
2375 BeginBatchUndo(_("Replace"));
2377 DeleteSelectedContent();
2379 DoWriteText(value
, SetValue_SelectionOnly
);
2384 void wxRichTextCtrl::Remove(long from
, long to
)
2388 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this);
2395 bool wxRichTextCtrl::IsModified() const
2397 return m_buffer
.IsModified();
2400 void wxRichTextCtrl::MarkDirty()
2402 m_buffer
.Modify(true);
2405 void wxRichTextCtrl::DiscardEdits()
2407 m_caretPositionForDefaultStyle
= -2;
2408 m_buffer
.Modify(false);
2409 m_buffer
.GetCommandProcessor()->ClearCommands();
2412 int wxRichTextCtrl::GetNumberOfLines() const
2414 return GetBuffer().GetParagraphCount();
2417 // ----------------------------------------------------------------------------
2418 // Positions <-> coords
2419 // ----------------------------------------------------------------------------
2421 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2423 return GetBuffer().XYToPosition(x
, y
);
2426 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2428 return GetBuffer().PositionToXY(pos
, x
, y
);
2431 // ----------------------------------------------------------------------------
2433 // ----------------------------------------------------------------------------
2435 void wxRichTextCtrl::ShowPosition(long pos
)
2437 if (!IsPositionVisible(pos
))
2438 ScrollIntoView(pos
-1, WXK_DOWN
);
2441 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2443 return GetBuffer().GetParagraphLength(lineNo
);
2446 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2448 return GetBuffer().GetParagraphText(lineNo
);
2451 // ----------------------------------------------------------------------------
2453 // ----------------------------------------------------------------------------
2455 void wxRichTextCtrl::Undo()
2459 GetCommandProcessor()->Undo();
2463 void wxRichTextCtrl::Redo()
2467 GetCommandProcessor()->Redo();
2471 bool wxRichTextCtrl::CanUndo() const
2473 return GetCommandProcessor()->CanUndo();
2476 bool wxRichTextCtrl::CanRedo() const
2478 return GetCommandProcessor()->CanRedo();
2481 // ----------------------------------------------------------------------------
2482 // implementation details
2483 // ----------------------------------------------------------------------------
2485 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2487 SetValue(event
.GetString());
2488 GetEventHandler()->ProcessEvent(event
);
2491 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2493 // By default, load the first file into the text window.
2494 if (event
.GetNumberOfFiles() > 0)
2496 LoadFile(event
.GetFiles()[0]);
2500 wxSize
wxRichTextCtrl::DoGetBestSize() const
2502 return wxSize(10, 10);
2505 // ----------------------------------------------------------------------------
2506 // standard handlers for standard edit menu events
2507 // ----------------------------------------------------------------------------
2509 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2514 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2519 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2524 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2529 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2534 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2539 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2541 event
.Enable( CanCut() );
2544 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2546 event
.Enable( CanCopy() );
2549 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2551 event
.Enable( CanDeleteSelection() );
2554 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2556 event
.Enable( CanPaste() );
2559 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2561 event
.Enable( CanUndo() );
2562 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2565 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2567 event
.Enable( CanRedo() );
2568 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2571 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2576 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2578 event
.Enable(GetLastPosition() > 0);
2581 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
2583 if (event
.GetEventObject() != this)
2591 m_contextMenu
= new wxMenu
;
2592 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2593 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2594 m_contextMenu
->AppendSeparator();
2595 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2596 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2597 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2598 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2599 m_contextMenu
->AppendSeparator();
2600 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2602 PopupMenu(m_contextMenu
);
2606 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2608 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttr(style
));
2611 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
2613 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2616 // extended style setting operation with flags including:
2617 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2618 // see richtextbuffer.h for more details.
2620 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttr
& style
, int flags
)
2622 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2625 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2627 return GetBuffer().SetDefaultStyle(wxTextAttr(style
));
2630 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2632 return GetBuffer().GetDefaultStyle();
2635 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2637 return GetBuffer().GetStyle(position
, style
);
2640 // get the common set of styles for the range
2641 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
2643 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2646 /// Get the content (uncombined) attributes for this position.
2647 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2649 return GetBuffer().GetUncombinedStyle(position
, style
);
2652 /// Set font, and also the buffer attributes
2653 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2655 wxControl::SetFont(font
);
2657 wxTextAttr attr
= GetBuffer().GetAttributes();
2659 GetBuffer().SetBasicStyle(attr
);
2661 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2667 /// Transform logical to physical
2668 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2671 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2676 /// Transform physical to logical
2677 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2680 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2685 /// Position the caret
2686 void wxRichTextCtrl::PositionCaret()
2691 //wxLogDebug(wxT("PositionCaret"));
2694 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2696 wxPoint newPt
= caretRect
.GetPosition();
2697 wxSize newSz
= caretRect
.GetSize();
2698 wxPoint pt
= GetPhysicalPoint(newPt
);
2699 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
2701 GetCaret()->Move(pt
);
2702 GetCaret()->SetSize(newSz
);
2707 /// Get the caret height and position for the given character position
2708 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2710 wxClientDC
dc(this);
2711 dc
.SetFont(GetFont());
2718 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2720 // Caret height can't be zero
2722 height
= dc
.GetCharHeight();
2724 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2731 /// Gets the line for the visible caret position. If the caret is
2732 /// shown at the very end of the line, it means the next character is actually
2733 /// on the following line. So let's get the line we're expecting to find
2734 /// if this is the case.
2735 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2737 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2738 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2741 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2742 if (caretPosition
== lineRange
.GetStart()-1 &&
2743 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2745 if (!m_caretAtLineStart
)
2746 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2753 /// Move the caret to the given character position
2754 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2756 if (GetBuffer().GetDirty())
2759 if (pos
<= GetBuffer().GetRange().GetEnd())
2761 SetCaretPosition(pos
, showAtLineStart
);
2771 /// Layout the buffer: which we must do before certain operations, such as
2772 /// setting the caret position.
2773 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2775 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2777 wxRect
availableSpace(GetClientSize());
2778 if (availableSpace
.width
== 0)
2779 availableSpace
.width
= 10;
2780 if (availableSpace
.height
== 0)
2781 availableSpace
.height
= 10;
2783 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2784 if (onlyVisibleRect
)
2786 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2787 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2790 wxClientDC
dc(this);
2791 dc
.SetFont(GetFont());
2795 GetBuffer().Defragment();
2796 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2797 GetBuffer().Layout(dc
, availableSpace
, flags
);
2798 GetBuffer().SetDirty(false);
2807 /// Is all of the selection bold?
2808 bool wxRichTextCtrl::IsSelectionBold()
2813 wxRichTextRange range
= GetSelectionRange();
2814 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2815 attr
.SetFontWeight(wxBOLD
);
2817 return HasCharacterAttributes(range
, attr
);
2821 // If no selection, then we need to combine current style with default style
2822 // to see what the effect would be if we started typing.
2824 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2826 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2827 if (GetStyle(pos
, attr
))
2829 if (IsDefaultStyleShowing())
2830 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2831 return attr
.GetFontWeight() == wxBOLD
;
2837 /// Is all of the selection italics?
2838 bool wxRichTextCtrl::IsSelectionItalics()
2842 wxRichTextRange range
= GetSelectionRange();
2844 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2845 attr
.SetFontStyle(wxITALIC
);
2847 return HasCharacterAttributes(range
, attr
);
2851 // If no selection, then we need to combine current style with default style
2852 // to see what the effect would be if we started typing.
2854 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2856 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2857 if (GetStyle(pos
, attr
))
2859 if (IsDefaultStyleShowing())
2860 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2861 return attr
.GetFontStyle() == wxITALIC
;
2867 /// Is all of the selection underlined?
2868 bool wxRichTextCtrl::IsSelectionUnderlined()
2872 wxRichTextRange range
= GetSelectionRange();
2874 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2875 attr
.SetFontUnderlined(true);
2877 return HasCharacterAttributes(range
, attr
);
2881 // If no selection, then we need to combine current style with default style
2882 // to see what the effect would be if we started typing.
2884 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2885 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2887 if (GetStyle(pos
, attr
))
2889 if (IsDefaultStyleShowing())
2890 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2891 return attr
.GetFontUnderlined();
2897 /// Apply bold to the selection
2898 bool wxRichTextCtrl::ApplyBoldToSelection()
2901 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2902 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2905 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2908 wxRichTextAttr current
= GetDefaultStyleEx();
2909 current
.Apply(attr
);
2910 SetAndShowDefaultStyle(current
);
2915 /// Apply italic to the selection
2916 bool wxRichTextCtrl::ApplyItalicToSelection()
2919 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2920 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2923 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2926 wxRichTextAttr current
= GetDefaultStyleEx();
2927 current
.Apply(attr
);
2928 SetAndShowDefaultStyle(current
);
2933 /// Apply underline to the selection
2934 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2937 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2938 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2941 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2944 wxRichTextAttr current
= GetDefaultStyleEx();
2945 current
.Apply(attr
);
2946 SetAndShowDefaultStyle(current
);
2951 /// Is all of the selection aligned according to the specified flag?
2952 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2954 wxRichTextRange range
;
2956 range
= GetSelectionRange();
2958 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2961 attr
.SetAlignment(alignment
);
2963 return HasParagraphAttributes(range
, attr
);
2966 /// Apply alignment to the selection
2967 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2970 attr
.SetAlignment(alignment
);
2972 return SetStyle(GetSelectionRange(), attr
);
2975 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2977 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2982 /// Apply a named style to the selection
2983 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2985 // Flags are defined within each definition, so only certain
2986 // attributes are applied.
2987 wxTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2989 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
2991 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2993 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2995 wxRichTextRange range
;
2998 range
= GetSelectionRange();
3001 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3002 range
= wxRichTextRange(pos
, pos
+1);
3005 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3008 // Make sure the attr has the style name
3009 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3011 attr
.SetParagraphStyleName(def
->GetName());
3013 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3014 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3015 // to change its style independently.
3016 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3019 attr
.SetCharacterStyleName(def
->GetName());
3022 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3025 wxRichTextAttr current
= GetDefaultStyleEx();
3026 current
.Apply(attr
);
3027 SetAndShowDefaultStyle(current
);
3032 /// Apply the style sheet to the buffer, for example if the styles have changed.
3033 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3036 styleSheet
= GetBuffer().GetStyleSheet();
3040 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3042 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3050 /// Sets the default style to the style under the cursor
3051 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3054 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3056 // If at the start of a paragraph, use the next position.
3057 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3059 if (GetUncombinedStyle(pos
, attr
))
3061 SetDefaultStyle(attr
);
3068 /// Returns the first visible position in the current view
3069 long wxRichTextCtrl::GetFirstVisiblePosition() const
3071 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3073 return line
->GetAbsoluteRange().GetStart();
3078 /// Get the first visible point in the window
3079 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3082 int startXUnits
, startYUnits
;
3084 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3085 GetViewStart(& startXUnits
, & startYUnits
);
3087 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3090 /// The adjusted caret position is the character position adjusted to take
3091 /// into account whether we're at the start of a paragraph, in which case
3092 /// style information should be taken from the next position, not current one.
3093 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3095 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3097 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3102 /// Get/set the selection range in character positions. -1, -1 means no selection.
3103 /// The range is in API convention, i.e. a single character selection is denoted
3105 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3107 wxRichTextRange range
= GetInternalSelectionRange();
3108 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3109 range
.SetEnd(range
.GetEnd() + 1);
3113 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3115 wxRichTextRange
range1(range
);
3116 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3117 range1
.SetEnd(range1
.GetEnd() - 1);
3119 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3121 SetInternalSelectionRange(range1
);
3125 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3127 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3130 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3132 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3135 /// Clear list for given range
3136 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3138 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3141 /// Number/renumber any list elements in the given range
3142 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3144 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3147 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3149 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3152 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3153 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3155 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3158 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3160 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3163 /// Deletes the content in the given range
3164 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3166 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3169 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3171 if (sm_availableFontNames
.GetCount() == 0)
3173 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3174 sm_availableFontNames
.Sort();
3176 return sm_availableFontNames
;
3179 void wxRichTextCtrl::ClearAvailableFontNames()
3181 sm_availableFontNames
.Clear();
3184 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3186 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3188 wxTextAttrEx basicStyle
= GetBasicStyle();
3189 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3190 SetBasicStyle(basicStyle
);
3191 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));