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(*wxBLACK
);
155 attributes
.SetBackgroundColour(*wxWHITE
);
156 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
157 attributes
.SetLineSpacing(10);
158 attributes
.SetParagraphSpacingAfter(10);
159 attributes
.SetParagraphSpacingBefore(0);
160 attributes
.SetTextEffects(0);
161 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
163 SetBasicStyle(attributes
);
165 // The default attributes will be merged with base attributes, so
166 // can be empty to begin with
167 wxTextAttrEx defaultAttributes
;
168 SetDefaultStyle(defaultAttributes
);
170 SetBackgroundColour(*wxWHITE
);
171 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
173 // Tell the sizers to use the given or best size
174 SetInitialSize(size
);
176 #if wxRICHTEXT_BUFFERED_PAINTING
178 RecreateBuffer(size
);
181 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
182 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
184 SetCursor(m_textCursor
);
186 if (!value
.IsEmpty())
189 GetBuffer().AddEventHandler(this);
194 wxRichTextCtrl::~wxRichTextCtrl()
196 GetBuffer().RemoveEventHandler(this);
198 delete m_contextMenu
;
201 /// Member initialisation
202 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 /// Call Freeze to prevent refresh
221 void wxRichTextCtrl::Freeze()
226 /// Call Thaw to refresh
227 void wxRichTextCtrl::Thaw()
231 if (m_freezeCount
== 0)
239 void wxRichTextCtrl::Clear()
241 m_buffer
.ResetAndClearCommands();
242 m_buffer
.SetDirty(true);
243 m_caretPosition
= -1;
244 m_caretPositionForDefaultStyle
= -2;
245 m_caretAtLineStart
= false;
246 m_selectionRange
.SetRange(-2, -2);
248 SetScrollbars(0, 0, 0, 0, 0, 0);
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
)
397 if (GetStyle(position
, attr
))
399 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
401 wxString urlTarget
= attr
.GetURL();
402 if (!urlTarget
.IsEmpty())
404 wxMouseEvent
mouseEvent(event
);
406 long startPos
= 0, endPos
= 0;
407 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
410 startPos
= obj
->GetRange().GetStart();
411 endPos
= obj
->GetRange().GetEnd();
414 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
415 InitCommandEvent(urlEvent
);
417 urlEvent
.SetString(urlTarget
);
419 GetEventHandler()->ProcessEvent(urlEvent
);
428 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
432 dc
.SetFont(GetFont());
435 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
436 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
438 // See if we need to change the cursor
441 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
444 if (GetStyle(position
, attr
))
446 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
448 SetCursor(m_urlCursor
);
450 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
452 SetCursor(m_textCursor
);
458 if (!event
.Dragging())
464 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
466 // TODO: test closeness
468 bool caretAtLineStart
= false;
470 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
472 // If we're at the start of a line (but not first in para)
473 // then we should keep the caret showing at the start of the line
474 // by showing the m_caretAtLineStart flag.
475 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
476 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
478 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
479 caretAtLineStart
= true;
483 if (m_caretPosition
!= position
)
485 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
487 MoveCaret(position
, caretAtLineStart
);
488 SetDefaultStyleToCursorStyle();
497 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
503 /// Left-double-click
504 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
506 SelectWord(GetCaretPosition()+1);
511 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
517 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
521 flags
|= wxRICHTEXT_CTRL_DOWN
;
522 if (event
.ShiftDown())
523 flags
|= wxRICHTEXT_SHIFT_DOWN
;
525 flags
|= wxRICHTEXT_ALT_DOWN
;
527 if (event
.GetKeyCode() == WXK_LEFT
||
528 event
.GetKeyCode() == WXK_RIGHT
||
529 event
.GetKeyCode() == WXK_UP
||
530 event
.GetKeyCode() == WXK_DOWN
||
531 event
.GetKeyCode() == WXK_HOME
||
532 event
.GetKeyCode() == WXK_PAGEUP
||
533 event
.GetKeyCode() == WXK_PAGEDOWN
||
534 event
.GetKeyCode() == WXK_END
||
536 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
537 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
538 event
.GetKeyCode() == WXK_NUMPAD_UP
||
539 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
540 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
541 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
542 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
543 event
.GetKeyCode() == WXK_NUMPAD_END
)
545 KeyboardNavigate(event
.GetKeyCode(), flags
);
549 // all the other keys modify the controls contents which shouldn't be
550 // possible if we're read-only
557 if (event
.GetKeyCode() == WXK_RETURN
)
559 BeginBatchUndo(_("Insert Text"));
561 long newPos
= m_caretPosition
;
563 DeleteSelectedContent(& newPos
);
565 if (event
.ShiftDown())
568 text
= wxRichTextLineBreakChar
;
569 GetBuffer().InsertTextWithUndo(newPos
+1, text
, this);
572 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
575 SetDefaultStyleToCursorStyle();
577 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
579 wxRichTextEvent
cmdEvent(
580 wxEVT_COMMAND_RICHTEXT_RETURN
,
582 cmdEvent
.SetEventObject(this);
583 cmdEvent
.SetFlags(flags
);
584 cmdEvent
.SetPosition(newPos
+1);
586 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
588 // Generate conventional event
589 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
590 InitCommandEvent(textEvent
);
592 GetEventHandler()->ProcessEvent(textEvent
);
596 else if (event
.GetKeyCode() == WXK_BACK
)
598 BeginBatchUndo(_("Delete Text"));
600 // Submit range in character positions, which are greater than caret positions,
601 // so subtract 1 for deleted character and add 1 for conversion to character position.
602 if (m_caretPosition
> -1 && !HasSelection())
604 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
), this);
607 DeleteSelectedContent();
611 if (GetLastPosition() == -1)
615 m_caretPosition
= -1;
617 SetDefaultStyleToCursorStyle();
620 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
622 wxRichTextEvent
cmdEvent(
623 wxEVT_COMMAND_RICHTEXT_DELETE
,
625 cmdEvent
.SetEventObject(this);
626 cmdEvent
.SetFlags(flags
);
627 cmdEvent
.SetPosition(m_caretPosition
+1);
628 GetEventHandler()->ProcessEvent(cmdEvent
);
632 else if (event
.GetKeyCode() == WXK_DELETE
)
634 BeginBatchUndo(_("Delete Text"));
636 // Submit range in character positions, which are greater than caret positions,
637 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
639 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1), this);
642 DeleteSelectedContent();
646 if (GetLastPosition() == -1)
650 m_caretPosition
= -1;
652 SetDefaultStyleToCursorStyle();
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
);
667 long keycode
= event
.GetKeyCode();
741 case WXK_NUMPAD_SPACE
:
743 case WXK_NUMPAD_ENTER
:
748 case WXK_NUMPAD_HOME
:
749 case WXK_NUMPAD_LEFT
:
751 case WXK_NUMPAD_RIGHT
:
752 case WXK_NUMPAD_DOWN
:
753 case WXK_NUMPAD_PAGEUP
:
754 case WXK_NUMPAD_PAGEDOWN
:
756 case WXK_NUMPAD_BEGIN
:
757 case WXK_NUMPAD_INSERT
:
758 case WXK_NUMPAD_DELETE
:
759 case WXK_NUMPAD_EQUAL
:
760 case WXK_NUMPAD_MULTIPLY
:
762 case WXK_NUMPAD_SEPARATOR
:
763 case WXK_NUMPAD_SUBTRACT
:
764 case WXK_NUMPAD_DECIMAL
:
772 if (event
.CmdDown() || event
.AltDown())
778 wxRichTextEvent
cmdEvent(
779 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
781 cmdEvent
.SetEventObject(this);
782 cmdEvent
.SetFlags(flags
);
783 cmdEvent
.SetCharacter((wxChar
) keycode
);
784 cmdEvent
.SetPosition(m_caretPosition
+1);
786 if (keycode
== wxT('\t'))
788 // See if we need to promote or demote the selection or paragraph at the cursor
789 // position, instead of inserting a tab.
790 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
791 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
792 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
794 wxRichTextRange range
;
796 range
= GetSelectionRange();
798 range
= para
->GetRange().FromInternal();
800 int promoteBy
= event
.ShiftDown() ? 1 : -1;
802 PromoteList(promoteBy
, range
, NULL
);
804 GetEventHandler()->ProcessEvent(cmdEvent
);
810 BeginBatchUndo(_("Insert Text"));
812 long newPos
= m_caretPosition
;
813 DeleteSelectedContent(& newPos
);
815 wxString str
= (wxChar
) event
.GetKeyCode();
816 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, 0);
820 SetDefaultStyleToCursorStyle();
821 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
823 GetEventHandler()->ProcessEvent(cmdEvent
);
831 /// Delete content if there is a selection, e.g. when pressing a key.
832 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
836 long pos
= m_selectionRange
.GetStart();
837 GetBuffer().DeleteRangeWithUndo(m_selectionRange
, this);
838 m_selectionRange
.SetRange(-2, -2);
848 /// Keyboard navigation
852 Left: left one character
853 Right: right one character
856 Ctrl-Left: left one word
857 Ctrl-Right: right one word
858 Ctrl-Up: previous paragraph start
859 Ctrl-Down: next start of paragraph
862 Ctrl-Home: start of document
863 Ctrl-End: end of document
865 Page-Down: Down a screen
869 Ctrl-Alt-PgUp: Start of window
870 Ctrl-Alt-PgDn: End of window
871 F8: Start selection mode
872 Esc: End selection mode
874 Adding Shift does the above but starts/extends selection.
879 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
881 bool success
= false;
883 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
885 if (flags
& wxRICHTEXT_CTRL_DOWN
)
886 success
= WordRight(1, flags
);
888 success
= MoveRight(1, flags
);
890 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
892 if (flags
& wxRICHTEXT_CTRL_DOWN
)
893 success
= WordLeft(1, flags
);
895 success
= MoveLeft(1, flags
);
897 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
899 if (flags
& wxRICHTEXT_CTRL_DOWN
)
900 success
= MoveToParagraphStart(flags
);
902 success
= MoveUp(1, flags
);
904 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
906 if (flags
& wxRICHTEXT_CTRL_DOWN
)
907 success
= MoveToParagraphEnd(flags
);
909 success
= MoveDown(1, flags
);
911 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
913 success
= PageUp(1, flags
);
915 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
917 success
= PageDown(1, flags
);
919 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
921 if (flags
& wxRICHTEXT_CTRL_DOWN
)
922 success
= MoveHome(flags
);
924 success
= MoveToLineStart(flags
);
926 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
928 if (flags
& wxRICHTEXT_CTRL_DOWN
)
929 success
= MoveEnd(flags
);
931 success
= MoveToLineEnd(flags
);
936 ScrollIntoView(m_caretPosition
, keyCode
);
937 SetDefaultStyleToCursorStyle();
943 /// Extend the selection. Selections are in caret positions.
944 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
946 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
948 // If not currently selecting, start selecting
949 if (m_selectionRange
.GetStart() == -2)
951 m_selectionAnchor
= oldPos
;
954 m_selectionRange
.SetRange(newPos
+1, oldPos
);
956 m_selectionRange
.SetRange(oldPos
+1, newPos
);
960 // Always ensure that the selection range start is greater than
962 if (newPos
> m_selectionAnchor
)
963 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
965 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
968 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
970 wxLogDebug(wxT("Strange selection range"));
979 /// Scroll into view, returning true if we scrolled.
980 /// This takes a _caret_ position.
981 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
983 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
989 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
991 int startXUnits
, startYUnits
;
992 GetViewStart(& startXUnits
, & startYUnits
);
993 int startY
= startYUnits
* ppuY
;
996 GetVirtualSize(& sx
, & sy
);
1002 wxRect rect
= line
->GetRect();
1004 bool scrolled
= false;
1006 wxSize clientSize
= GetClientSize();
1009 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1010 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
1011 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1012 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1014 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1016 // Make it scroll so this item is at the bottom
1018 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1019 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1021 // If we're still off the screen, scroll another line down
1022 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1025 if (startYUnits
!= yUnits
)
1027 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1031 else if (rect
.y
< startY
)
1033 // Make it scroll so this item is at the top
1036 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1038 if (startYUnits
!= yUnits
)
1040 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1046 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1047 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1048 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1049 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1051 if (rect
.y
< startY
)
1053 // Make it scroll so this item is at the top
1056 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1058 if (startYUnits
!= yUnits
)
1060 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1064 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1066 // Make it scroll so this item is at the bottom
1068 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1069 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1071 // If we're still off the screen, scroll another line down
1072 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1075 if (startYUnits
!= yUnits
)
1077 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1087 /// Is the given position visible on the screen?
1088 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1090 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1096 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1099 GetViewStart(& startX
, & startY
);
1101 startY
= startY
* ppuY
;
1104 GetVirtualSize(& sx
, & sy
);
1109 wxRect rect
= line
->GetRect();
1111 wxSize clientSize
= GetClientSize();
1113 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1116 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1118 m_caretPosition
= position
;
1119 m_caretAtLineStart
= showAtLineStart
;
1122 /// Move caret one visual step forward: this may mean setting a flag
1123 /// and keeping the same position if we're going from the end of one line
1124 /// to the start of the next, which may be the exact same caret position.
1125 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1127 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1129 // Only do the check if we're not at the end of the paragraph (where things work OK
1131 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1133 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1137 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1139 // We're at the end of a line. See whether we need to
1140 // stay at the same actual caret position but change visual
1141 // position, or not.
1142 if (oldPosition
== lineRange
.GetEnd())
1144 if (m_caretAtLineStart
)
1146 // We're already at the start of the line, so actually move on now.
1147 m_caretPosition
= oldPosition
+ 1;
1148 m_caretAtLineStart
= false;
1152 // We're showing at the end of the line, so keep to
1153 // the same position but indicate that we're to show
1154 // at the start of the next line.
1155 m_caretPosition
= oldPosition
;
1156 m_caretAtLineStart
= true;
1158 SetDefaultStyleToCursorStyle();
1164 SetDefaultStyleToCursorStyle();
1167 /// Move caret one visual step backward: this may mean setting a flag
1168 /// and keeping the same position if we're going from the end of one line
1169 /// to the start of the next, which may be the exact same caret position.
1170 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1172 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1174 // Only do the check if we're not at the start of the paragraph (where things work OK
1176 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1178 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1182 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1184 // We're at the start of a line. See whether we need to
1185 // stay at the same actual caret position but change visual
1186 // position, or not.
1187 if (oldPosition
== lineRange
.GetStart())
1189 m_caretPosition
= oldPosition
-1;
1190 m_caretAtLineStart
= true;
1193 else if (oldPosition
== lineRange
.GetEnd())
1195 if (m_caretAtLineStart
)
1197 // We're at the start of the line, so keep the same caret position
1198 // but clear the start-of-line flag.
1199 m_caretPosition
= oldPosition
;
1200 m_caretAtLineStart
= false;
1204 // We're showing at the end of the line, so go back
1205 // to the previous character position.
1206 m_caretPosition
= oldPosition
- 1;
1208 SetDefaultStyleToCursorStyle();
1214 SetDefaultStyleToCursorStyle();
1218 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1220 long endPos
= GetBuffer().GetRange().GetEnd();
1222 if (m_caretPosition
+ noPositions
< endPos
)
1224 long oldPos
= m_caretPosition
;
1225 long newPos
= m_caretPosition
+ noPositions
;
1227 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1231 // Determine by looking at oldPos and m_caretPosition whether
1232 // we moved from the end of a line to the start of the next line, in which case
1233 // we want to adjust the caret position such that it is positioned at the
1234 // start of the next line, rather than jumping past the first character of the
1236 if (noPositions
== 1 && !extendSel
)
1237 MoveCaretForward(oldPos
);
1239 SetCaretPosition(newPos
);
1242 SetDefaultStyleToCursorStyle();
1253 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1257 if (m_caretPosition
> startPos
- noPositions
+ 1)
1259 long oldPos
= m_caretPosition
;
1260 long newPos
= m_caretPosition
- noPositions
;
1261 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1265 if (noPositions
== 1 && !extendSel
)
1266 MoveCaretBack(oldPos
);
1268 SetCaretPosition(newPos
);
1271 SetDefaultStyleToCursorStyle();
1282 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1284 return MoveDown(- noLines
, flags
);
1288 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1293 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1294 wxPoint pt
= GetCaret()->GetPosition();
1295 long newLine
= lineNumber
+ noLines
;
1297 if (lineNumber
!= -1)
1301 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1303 if (newLine
> lastLine
)
1313 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1316 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1322 wxClientDC
dc(this);
1324 dc
.SetFont(GetFont());
1326 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1328 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1330 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1331 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1332 // so we view the caret at the start of the line.
1333 bool caretLineStart
= false;
1334 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1336 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1337 wxRichTextRange lineRange
;
1339 lineRange
= thisLine
->GetAbsoluteRange();
1341 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1344 caretLineStart
= true;
1348 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1349 if (para
&& para
->GetRange().GetStart() == newPos
)
1354 long newSelEnd
= newPos
;
1356 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1360 SetCaretPosition(newPos
, caretLineStart
);
1362 SetDefaultStyleToCursorStyle();
1372 /// Move to the end of the paragraph
1373 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1375 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1378 long newPos
= para
->GetRange().GetEnd() - 1;
1379 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1383 SetCaretPosition(newPos
);
1385 SetDefaultStyleToCursorStyle();
1395 /// Move to the start of the paragraph
1396 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1398 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1401 long newPos
= para
->GetRange().GetStart() - 1;
1402 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1406 SetCaretPosition(newPos
);
1408 SetDefaultStyleToCursorStyle();
1418 /// Move to the end of the line
1419 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1421 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1425 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1426 long newPos
= lineRange
.GetEnd();
1427 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1431 SetCaretPosition(newPos
);
1433 SetDefaultStyleToCursorStyle();
1443 /// Move to the start of the line
1444 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1446 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1449 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1450 long newPos
= lineRange
.GetStart()-1;
1452 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1456 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1458 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1460 SetDefaultStyleToCursorStyle();
1470 /// Move to the start of the buffer
1471 bool wxRichTextCtrl::MoveHome(int flags
)
1473 if (m_caretPosition
!= -1)
1475 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1479 SetCaretPosition(-1);
1481 SetDefaultStyleToCursorStyle();
1491 /// Move to the end of the buffer
1492 bool wxRichTextCtrl::MoveEnd(int flags
)
1494 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1496 if (m_caretPosition
!= endPos
)
1498 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1502 SetCaretPosition(endPos
);
1504 SetDefaultStyleToCursorStyle();
1514 /// Move noPages pages up
1515 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1517 return PageDown(- noPages
, flags
);
1520 /// Move noPages pages down
1521 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1523 // Calculate which line occurs noPages * screen height further down.
1524 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1527 wxSize clientSize
= GetClientSize();
1528 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1530 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1533 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1534 long pos
= lineRange
.GetStart()-1;
1535 if (pos
!= m_caretPosition
)
1537 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1539 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1543 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1545 SetDefaultStyleToCursorStyle();
1557 // Finds the caret position for the next word
1558 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1560 long endPos
= GetBuffer().GetRange().GetEnd();
1564 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1566 // First skip current text to space
1567 while (i
< endPos
&& i
> -1)
1569 // i is in character, not caret positions
1570 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1571 if (text
!= wxT(" ") && !text
.empty())
1578 while (i
< endPos
&& i
> -1)
1580 // i is in character, not caret positions
1581 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1582 if (text
.empty()) // End of paragraph, or maybe an image
1583 return wxMax(-1, i
- 1);
1584 else if (text
== wxT(" ") || text
.empty())
1588 // Convert to caret position
1589 return wxMax(-1, i
- 1);
1598 long i
= m_caretPosition
;
1600 // First skip white space
1601 while (i
< endPos
&& i
> -1)
1603 // i is in character, not caret positions
1604 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1605 if (text
.empty()) // End of paragraph, or maybe an image
1607 else if (text
== wxT(" ") || text
.empty())
1612 // Next 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 if (text
!= wxT(" ") /* && !text.empty() */)
1630 /// Move n words left
1631 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1633 long pos
= FindNextWordPosition(-1);
1634 if (pos
!= m_caretPosition
)
1636 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1638 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1642 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1644 SetDefaultStyleToCursorStyle();
1654 /// Move n words right
1655 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1657 long pos
= FindNextWordPosition(1);
1658 if (pos
!= m_caretPosition
)
1660 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1662 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1666 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1668 SetDefaultStyleToCursorStyle();
1679 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1681 // Only do sizing optimization for large buffers
1682 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1684 m_fullLayoutRequired
= true;
1685 m_fullLayoutTime
= wxGetLocalTimeMillis();
1686 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1687 LayoutContent(true /* onlyVisibleRect */);
1690 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1692 #if wxRICHTEXT_BUFFERED_PAINTING
1700 /// Idle-time processing
1701 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1703 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1705 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1707 m_fullLayoutRequired
= false;
1708 m_fullLayoutTime
= 0;
1709 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1710 ShowPosition(m_fullLayoutSavedPosition
);
1714 if (m_caretPositionForDefaultStyle
!= -2)
1716 // If the caret position has changed, no longer reflect the default style
1718 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1719 m_caretPositionForDefaultStyle
= -2;
1726 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1732 /// Set up scrollbars, e.g. after a resize
1733 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1738 if (GetBuffer().IsEmpty())
1740 SetScrollbars(0, 0, 0, 0, 0, 0);
1744 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1745 // of pixels. See e.g. wxVScrolledWindow for ideas.
1746 int pixelsPerUnit
= 5;
1747 wxSize clientSize
= GetClientSize();
1749 int maxHeight
= GetBuffer().GetCachedSize().y
;
1751 // Round up so we have at least maxHeight pixels
1752 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1754 int startX
= 0, startY
= 0;
1756 GetViewStart(& startX
, & startY
);
1758 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1759 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1761 // Move to previous scroll position if
1763 SetScrollbars(0, pixelsPerUnit
,
1765 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1768 /// Paint the background
1769 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1771 wxColour backgroundColour
= GetBackgroundColour();
1772 if (!backgroundColour
.Ok())
1773 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1775 // Clear the background
1776 dc
.SetBrush(wxBrush(backgroundColour
));
1777 dc
.SetPen(*wxTRANSPARENT_PEN
);
1778 wxRect
windowRect(GetClientSize());
1779 windowRect
.x
-= 2; windowRect
.y
-= 2;
1780 windowRect
.width
+= 4; windowRect
.height
+= 4;
1782 // We need to shift the rectangle to take into account
1783 // scrolling. Converting device to logical coordinates.
1784 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1785 dc
.DrawRectangle(windowRect
);
1788 #if wxRICHTEXT_BUFFERED_PAINTING
1789 /// Recreate buffer bitmap if necessary
1790 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1793 if (sz
== wxDefaultSize
)
1794 sz
= GetClientSize();
1796 if (sz
.x
< 1 || sz
.y
< 1)
1799 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1800 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1801 return m_bufferBitmap
.Ok();
1805 // ----------------------------------------------------------------------------
1806 // file IO functions
1807 // ----------------------------------------------------------------------------
1809 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1811 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1813 m_filename
= filename
;
1816 SetInsertionPoint(0);
1819 SetupScrollbars(true);
1821 SendTextUpdatedEvent();
1827 wxLogError(_("File couldn't be loaded."));
1833 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1835 if (GetBuffer().SaveFile(filename
, fileType
))
1837 m_filename
= filename
;
1844 wxLogError(_("The text couldn't be saved."));
1849 // ----------------------------------------------------------------------------
1850 // wxRichTextCtrl specific functionality
1851 // ----------------------------------------------------------------------------
1853 /// Add a new paragraph of text to the end of the buffer
1854 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1856 return GetBuffer().AddParagraph(text
);
1860 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1862 return GetBuffer().AddImage(image
);
1865 // ----------------------------------------------------------------------------
1866 // selection and ranges
1867 // ----------------------------------------------------------------------------
1869 void wxRichTextCtrl::SelectAll()
1871 SetSelection(0, GetLastPosition()+1);
1872 m_selectionAnchor
= -1;
1876 void wxRichTextCtrl::SelectNone()
1878 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1879 SetSelection(-2, -2);
1880 m_selectionAnchor
= -2;
1883 static bool wxIsWordDelimiter(const wxString
& text
)
1885 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1888 /// Select the word at the given character position
1889 bool wxRichTextCtrl::SelectWord(long position
)
1891 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1894 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1898 long positionStart
= position
;
1899 long positionEnd
= position
;
1901 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1903 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1904 if (wxIsWordDelimiter(text
))
1910 if (positionStart
< para
->GetRange().GetStart())
1911 positionStart
= para
->GetRange().GetStart();
1913 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1915 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1916 if (wxIsWordDelimiter(text
))
1922 if (positionEnd
>= para
->GetRange().GetEnd())
1923 positionEnd
= para
->GetRange().GetEnd();
1925 SetSelection(positionStart
, positionEnd
+1);
1927 if (positionStart
>= 0)
1929 MoveCaret(positionStart
-1, true);
1930 SetDefaultStyleToCursorStyle();
1936 wxString
wxRichTextCtrl::GetStringSelection() const
1939 GetSelection(&from
, &to
);
1941 return GetRange(from
, to
);
1944 // ----------------------------------------------------------------------------
1946 // ----------------------------------------------------------------------------
1948 wxTextCtrlHitTestResult
1949 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1951 // implement in terms of the other overload as the native ports typically
1952 // can get the position and not (x, y) pair directly (although wxUniv
1953 // directly gets x and y -- and so overrides this method as well)
1955 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1957 if ( rc
!= wxTE_HT_UNKNOWN
)
1959 PositionToXY(pos
, x
, y
);
1965 wxTextCtrlHitTestResult
1966 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1969 wxClientDC
dc((wxRichTextCtrl
*) this);
1970 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1972 // Buffer uses logical position (relative to start of buffer)
1974 wxPoint pt2
= GetLogicalPoint(pt
);
1976 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1980 case wxRICHTEXT_HITTEST_BEFORE
:
1981 return wxTE_HT_BEFORE
;
1983 case wxRICHTEXT_HITTEST_AFTER
:
1984 return wxTE_HT_BEYOND
;
1986 case wxRICHTEXT_HITTEST_ON
:
1987 return wxTE_HT_ON_TEXT
;
1990 return wxTE_HT_UNKNOWN
;
1993 // ----------------------------------------------------------------------------
1994 // set/get the controls text
1995 // ----------------------------------------------------------------------------
1997 wxString
wxRichTextCtrl::GetValue() const
1999 return GetBuffer().GetText();
2002 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2004 // Public API for range is different from internals
2005 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
2008 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2012 // if the text is long enough, it's faster to just set it instead of first
2013 // comparing it with the old one (chances are that it will be different
2014 // anyhow, this comparison is there to avoid flicker for small single-line
2015 // edit controls mostly)
2016 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
2020 // for compatibility, don't move the cursor when doing SetValue()
2021 SetInsertionPoint(0);
2025 if ( flags
& SetValue_SendEvent
)
2027 // still send an event for consistency
2028 SendTextUpdatedEvent();
2032 // we should reset the modified flag even if the value didn't really change
2034 // mark the control as being not dirty - we changed its text, not the
2039 void wxRichTextCtrl::WriteText(const wxString
& value
)
2044 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2046 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2048 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2050 if ( flags
& SetValue_SendEvent
)
2051 SendTextUpdatedEvent();
2054 void wxRichTextCtrl::AppendText(const wxString
& text
)
2056 SetInsertionPointEnd();
2061 /// Write an image at the current insertion point
2062 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2064 wxRichTextImageBlock imageBlock
;
2066 wxImage image2
= image
;
2067 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2068 return WriteImage(imageBlock
);
2073 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2075 wxRichTextImageBlock imageBlock
;
2078 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2079 return WriteImage(imageBlock
);
2084 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2086 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2089 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2093 wxRichTextImageBlock imageBlock
;
2095 wxImage image
= bitmap
.ConvertToImage();
2096 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2097 return WriteImage(imageBlock
);
2103 /// Insert a newline (actually paragraph) at the current insertion point.
2104 bool wxRichTextCtrl::Newline()
2106 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2109 /// Insert a line break at the current insertion point.
2110 bool wxRichTextCtrl::LineBreak()
2113 text
= wxRichTextLineBreakChar
;
2114 return GetBuffer().InsertTextWithUndo(m_caretPosition
+1, text
, this);
2117 // ----------------------------------------------------------------------------
2118 // Clipboard operations
2119 // ----------------------------------------------------------------------------
2121 void wxRichTextCtrl::Copy()
2125 wxRichTextRange range
= GetInternalSelectionRange();
2126 GetBuffer().CopyToClipboard(range
);
2130 void wxRichTextCtrl::Cut()
2134 wxRichTextRange range
= GetInternalSelectionRange();
2135 GetBuffer().CopyToClipboard(range
);
2137 DeleteSelectedContent();
2143 void wxRichTextCtrl::Paste()
2147 BeginBatchUndo(_("Paste"));
2149 long newPos
= m_caretPosition
;
2150 DeleteSelectedContent(& newPos
);
2152 GetBuffer().PasteFromClipboard(newPos
);
2158 void wxRichTextCtrl::DeleteSelection()
2160 if (CanDeleteSelection())
2162 DeleteSelectedContent();
2166 bool wxRichTextCtrl::HasSelection() const
2168 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2171 bool wxRichTextCtrl::CanCopy() const
2173 // Can copy if there's a selection
2174 return HasSelection();
2177 bool wxRichTextCtrl::CanCut() const
2179 return HasSelection() && IsEditable();
2182 bool wxRichTextCtrl::CanPaste() const
2184 if ( !IsEditable() )
2187 return GetBuffer().CanPasteFromClipboard();
2190 bool wxRichTextCtrl::CanDeleteSelection() const
2192 return HasSelection() && IsEditable();
2196 // ----------------------------------------------------------------------------
2198 // ----------------------------------------------------------------------------
2200 void wxRichTextCtrl::SetEditable(bool editable
)
2202 m_editable
= editable
;
2205 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2209 m_caretPosition
= pos
- 1;
2212 void wxRichTextCtrl::SetInsertionPointEnd()
2214 long pos
= GetLastPosition();
2215 SetInsertionPoint(pos
);
2218 long wxRichTextCtrl::GetInsertionPoint() const
2220 return m_caretPosition
+1;
2223 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2225 return GetBuffer().GetRange().GetEnd();
2228 // If the return values from and to are the same, there is no
2230 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2232 *from
= m_selectionRange
.GetStart();
2233 *to
= m_selectionRange
.GetEnd();
2234 if ((*to
) != -1 && (*to
) != -2)
2238 bool wxRichTextCtrl::IsEditable() const
2243 // ----------------------------------------------------------------------------
2245 // ----------------------------------------------------------------------------
2247 void wxRichTextCtrl::SetSelection(long from
, long to
)
2249 // if from and to are both -1, it means (in wxWidgets) that all text should
2251 if ( (from
== -1) && (to
== -1) )
2254 to
= GetLastPosition()+1;
2257 DoSetSelection(from
, to
);
2260 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2262 m_selectionAnchor
= from
;
2263 m_selectionRange
.SetRange(from
, to
-1);
2265 m_caretPosition
= from
-1;
2271 // ----------------------------------------------------------------------------
2273 // ----------------------------------------------------------------------------
2275 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2276 const wxString
& value
)
2278 BeginBatchUndo(_("Replace"));
2280 DeleteSelectedContent();
2282 DoWriteText(value
, SetValue_SelectionOnly
);
2287 void wxRichTextCtrl::Remove(long from
, long to
)
2291 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
), this);
2298 bool wxRichTextCtrl::IsModified() const
2300 return m_buffer
.IsModified();
2303 void wxRichTextCtrl::MarkDirty()
2305 m_buffer
.Modify(true);
2308 void wxRichTextCtrl::DiscardEdits()
2310 m_caretPositionForDefaultStyle
= -2;
2311 m_buffer
.Modify(false);
2312 m_buffer
.GetCommandProcessor()->ClearCommands();
2315 int wxRichTextCtrl::GetNumberOfLines() const
2317 return GetBuffer().GetParagraphCount();
2320 // ----------------------------------------------------------------------------
2321 // Positions <-> coords
2322 // ----------------------------------------------------------------------------
2324 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2326 return GetBuffer().XYToPosition(x
, y
);
2329 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2331 return GetBuffer().PositionToXY(pos
, x
, y
);
2334 // ----------------------------------------------------------------------------
2336 // ----------------------------------------------------------------------------
2338 void wxRichTextCtrl::ShowPosition(long pos
)
2340 if (!IsPositionVisible(pos
))
2341 ScrollIntoView(pos
-1, WXK_DOWN
);
2344 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2346 return GetBuffer().GetParagraphLength(lineNo
);
2349 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2351 return GetBuffer().GetParagraphText(lineNo
);
2354 // ----------------------------------------------------------------------------
2356 // ----------------------------------------------------------------------------
2358 void wxRichTextCtrl::Undo()
2362 GetCommandProcessor()->Undo();
2366 void wxRichTextCtrl::Redo()
2370 GetCommandProcessor()->Redo();
2374 bool wxRichTextCtrl::CanUndo() const
2376 return GetCommandProcessor()->CanUndo();
2379 bool wxRichTextCtrl::CanRedo() const
2381 return GetCommandProcessor()->CanRedo();
2384 // ----------------------------------------------------------------------------
2385 // implementation details
2386 // ----------------------------------------------------------------------------
2388 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2390 SetValue(event
.GetString());
2391 GetEventHandler()->ProcessEvent(event
);
2394 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2396 // By default, load the first file into the text window.
2397 if (event
.GetNumberOfFiles() > 0)
2399 LoadFile(event
.GetFiles()[0]);
2403 wxSize
wxRichTextCtrl::DoGetBestSize() const
2405 return wxSize(10, 10);
2408 // ----------------------------------------------------------------------------
2409 // standard handlers for standard edit menu events
2410 // ----------------------------------------------------------------------------
2412 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2417 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2422 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2427 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2432 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2437 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2442 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2444 event
.Enable( CanCut() );
2447 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2449 event
.Enable( CanCopy() );
2452 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2454 event
.Enable( CanDeleteSelection() );
2457 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2459 event
.Enable( CanPaste() );
2462 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2464 event
.Enable( CanUndo() );
2465 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2468 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2470 event
.Enable( CanRedo() );
2471 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2474 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2479 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2481 event
.Enable(GetLastPosition() > 0);
2484 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2488 m_contextMenu
= new wxMenu
;
2489 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2490 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2491 m_contextMenu
->AppendSeparator();
2492 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2493 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2494 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2495 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2496 m_contextMenu
->AppendSeparator();
2497 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2499 PopupMenu(m_contextMenu
);
2503 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2505 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2508 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2510 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2513 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2515 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2518 // extended style setting operation with flags including:
2519 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2520 // see richtextbuffer.h for more details.
2521 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2523 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2526 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2528 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2531 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2533 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2536 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2538 return GetBuffer().SetDefaultStyle(style
);
2541 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2543 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2546 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2548 return GetBuffer().GetDefaultStyle();
2551 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2553 return GetBuffer().GetDefaultStyle();
2556 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2558 wxTextAttrEx
attr(style
);
2559 if (GetBuffer().GetStyle(position
, attr
))
2568 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2570 return GetBuffer().GetStyle(position
, style
);
2573 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2575 return GetBuffer().GetStyle(position
, style
);
2578 // get the common set of styles for the range
2579 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
2581 wxTextAttrEx styleEx
;
2582 if (GetBuffer().GetStyleForRange(range
.ToInternal(), styleEx
))
2591 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttrEx
& style
)
2593 return GetBuffer().GetStyleForRange(range
.ToInternal(), style
);
2596 /// Get the content (uncombined) attributes for this position.
2598 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2600 wxTextAttrEx
attr(style
);
2601 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2610 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2612 return GetBuffer().GetUncombinedStyle(position
, style
);
2615 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2617 return GetBuffer().GetUncombinedStyle(position
, style
);
2620 /// Set font, and also the buffer attributes
2621 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2623 wxControl::SetFont(font
);
2625 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2627 GetBuffer().SetBasicStyle(attr
);
2629 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2635 /// Transform logical to physical
2636 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2639 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2644 /// Transform physical to logical
2645 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2648 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2653 /// Position the caret
2654 void wxRichTextCtrl::PositionCaret()
2659 //wxLogDebug(wxT("PositionCaret"));
2662 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2664 wxPoint originalPt
= caretRect
.GetPosition();
2665 wxPoint pt
= GetPhysicalPoint(originalPt
);
2666 if (GetCaret()->GetPosition() != pt
)
2668 GetCaret()->Move(pt
);
2669 GetCaret()->SetSize(caretRect
.GetSize());
2674 /// Get the caret height and position for the given character position
2675 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2677 wxClientDC
dc(this);
2678 dc
.SetFont(GetFont());
2685 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2687 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2694 /// Gets the line for the visible caret position. If the caret is
2695 /// shown at the very end of the line, it means the next character is actually
2696 /// on the following line. So let's get the line we're expecting to find
2697 /// if this is the case.
2698 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2700 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2701 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2704 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2705 if (caretPosition
== lineRange
.GetStart()-1 &&
2706 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2708 if (!m_caretAtLineStart
)
2709 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2716 /// Move the caret to the given character position
2717 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2719 if (GetBuffer().GetDirty())
2722 if (pos
<= GetBuffer().GetRange().GetEnd())
2724 SetCaretPosition(pos
, showAtLineStart
);
2734 /// Layout the buffer: which we must do before certain operations, such as
2735 /// setting the caret position.
2736 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2738 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2740 wxRect
availableSpace(GetClientSize());
2741 if (availableSpace
.width
== 0)
2742 availableSpace
.width
= 10;
2743 if (availableSpace
.height
== 0)
2744 availableSpace
.height
= 10;
2746 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2747 if (onlyVisibleRect
)
2749 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2750 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2753 wxClientDC
dc(this);
2754 dc
.SetFont(GetFont());
2758 GetBuffer().Defragment();
2759 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2760 GetBuffer().Layout(dc
, availableSpace
, flags
);
2761 GetBuffer().SetDirty(false);
2770 /// Is all of the selection bold?
2771 bool wxRichTextCtrl::IsSelectionBold()
2775 wxRichTextAttr attr
;
2776 wxRichTextRange range
= GetSelectionRange();
2777 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2778 attr
.SetFontWeight(wxBOLD
);
2780 return HasCharacterAttributes(range
, attr
);
2784 // If no selection, then we need to combine current style with default style
2785 // to see what the effect would be if we started typing.
2786 wxRichTextAttr attr
;
2787 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2789 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2790 if (GetStyle(pos
, attr
))
2792 if (IsDefaultStyleShowing())
2793 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2794 return attr
.GetFontWeight() == wxBOLD
;
2800 /// Is all of the selection italics?
2801 bool wxRichTextCtrl::IsSelectionItalics()
2805 wxRichTextRange range
= GetSelectionRange();
2806 wxRichTextAttr attr
;
2807 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2808 attr
.SetFontStyle(wxITALIC
);
2810 return HasCharacterAttributes(range
, attr
);
2814 // If no selection, then we need to combine current style with default style
2815 // to see what the effect would be if we started typing.
2816 wxRichTextAttr attr
;
2817 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2819 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2820 if (GetStyle(pos
, attr
))
2822 if (IsDefaultStyleShowing())
2823 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2824 return attr
.GetFontStyle() == wxITALIC
;
2830 /// Is all of the selection underlined?
2831 bool wxRichTextCtrl::IsSelectionUnderlined()
2835 wxRichTextRange range
= GetSelectionRange();
2836 wxRichTextAttr attr
;
2837 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2838 attr
.SetFontUnderlined(true);
2840 return HasCharacterAttributes(range
, attr
);
2844 // If no selection, then we need to combine current style with default style
2845 // to see what the effect would be if we started typing.
2846 wxRichTextAttr attr
;
2847 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2848 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2850 if (GetStyle(pos
, attr
))
2852 if (IsDefaultStyleShowing())
2853 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2854 return attr
.GetFontUnderlined();
2860 /// Apply bold to the selection
2861 bool wxRichTextCtrl::ApplyBoldToSelection()
2863 wxRichTextAttr attr
;
2864 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2865 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2868 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2870 SetAndShowDefaultStyle(attr
);
2874 /// Apply italic to the selection
2875 bool wxRichTextCtrl::ApplyItalicToSelection()
2877 wxRichTextAttr attr
;
2878 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2879 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2882 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2884 SetAndShowDefaultStyle(attr
);
2888 /// Apply underline to the selection
2889 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2891 wxRichTextAttr attr
;
2892 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2893 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2896 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2898 SetAndShowDefaultStyle(attr
);
2902 /// Is all of the selection aligned according to the specified flag?
2903 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2905 wxRichTextRange range
;
2907 range
= GetSelectionRange();
2909 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
2911 wxRichTextAttr attr
;
2912 attr
.SetAlignment(alignment
);
2914 return HasParagraphAttributes(range
, attr
);
2917 /// Apply alignment to the selection
2918 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2920 wxRichTextAttr attr
;
2921 attr
.SetAlignment(alignment
);
2923 return SetStyle(GetSelectionRange(), attr
);
2926 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2928 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2933 /// Apply a named style to the selection
2934 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2936 // Flags are defined within each definition, so only certain
2937 // attributes are applied.
2938 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
2940 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2942 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2944 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2946 wxRichTextRange range
;
2949 range
= GetSelectionRange();
2952 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2953 range
= wxRichTextRange(pos
, pos
+1);
2956 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2959 // Make sure the attr has the style name
2960 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2962 attr
.SetParagraphStyleName(def
->GetName());
2964 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2965 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2966 // to change its style independently.
2967 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2970 attr
.SetCharacterStyleName(def
->GetName());
2973 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2976 SetAndShowDefaultStyle(attr
);
2981 /// Apply the style sheet to the buffer, for example if the styles have changed.
2982 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2985 styleSheet
= GetBuffer().GetStyleSheet();
2989 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2991 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2999 /// Sets the default style to the style under the cursor
3000 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3003 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3005 // If at the start of a paragraph, use the next position.
3006 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3008 if (GetUncombinedStyle(pos
, attr
))
3010 SetDefaultStyle(attr
);
3017 /// Returns the first visible position in the current view
3018 long wxRichTextCtrl::GetFirstVisiblePosition() const
3020 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3022 return line
->GetAbsoluteRange().GetStart();
3027 /// Get the first visible point in the window
3028 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3031 int startXUnits
, startYUnits
;
3033 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3034 GetViewStart(& startXUnits
, & startYUnits
);
3036 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3039 /// The adjusted caret position is the character position adjusted to take
3040 /// into account whether we're at the start of a paragraph, in which case
3041 /// style information should be taken from the next position, not current one.
3042 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3044 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
3046 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3051 /// Get/set the selection range in character positions. -1, -1 means no selection.
3052 /// The range is in API convention, i.e. a single character selection is denoted
3054 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3056 wxRichTextRange range
= GetInternalSelectionRange();
3057 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3058 range
.SetEnd(range
.GetEnd() + 1);
3062 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3064 wxRichTextRange
range1(range
);
3065 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3066 range1
.SetEnd(range1
.GetEnd() - 1);
3068 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3070 SetInternalSelectionRange(range1
);
3074 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3076 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3079 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3081 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3084 /// Clear list for given range
3085 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3087 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3090 /// Number/renumber any list elements in the given range
3091 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3093 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3096 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3098 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3101 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3102 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3104 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3107 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3109 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3112 /// Deletes the content in the given range
3113 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3115 return GetBuffer().DeleteRangeWithUndo(range
.ToInternal(), this);
3118 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3120 if (sm_availableFontNames
.GetCount() == 0)
3122 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3123 sm_availableFontNames
.Sort();
3125 return sm_availableFontNames
;
3128 void wxRichTextCtrl::ClearAvailableFontNames()
3130 sm_availableFontNames
.Clear();