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"
35 // DLL options compatibility check:
37 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
47 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
49 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
51 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
52 EVT_PAINT(wxRichTextCtrl::OnPaint
)
53 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
54 EVT_IDLE(wxRichTextCtrl::OnIdle
)
55 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
56 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
57 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
58 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
59 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
60 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
61 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
62 EVT_CHAR(wxRichTextCtrl::OnChar
)
63 EVT_SIZE(wxRichTextCtrl::OnSize
)
64 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
65 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
66 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
68 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
69 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
71 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
72 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
74 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
75 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
77 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
78 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
80 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
81 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
83 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
84 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
86 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
87 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
94 wxRichTextCtrl::wxRichTextCtrl()
95 : wxScrollHelper(this)
100 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
102 const wxString
& value
,
106 : wxScrollHelper(this)
109 Create(parent
, id
, value
, pos
, size
, style
);
113 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
115 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
116 style
|wxFULL_REPAINT_ON_RESIZE
))
121 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
124 GetBuffer().SetRichTextCtrl(this);
126 wxTextAttrEx attributes
;
127 attributes
.SetFont(GetFont());
128 attributes
.SetTextColour(*wxBLACK
);
129 attributes
.SetBackgroundColour(*wxWHITE
);
130 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
131 attributes
.SetLineSpacing(10);
132 attributes
.SetParagraphSpacingAfter(10);
133 attributes
.SetParagraphSpacingBefore(0);
134 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
135 SetBasicStyle(attributes
);
137 // The default attributes will be merged with base attributes, so
138 // can be empty to begin with
139 wxTextAttrEx defaultAttributes
;
140 SetDefaultStyle(defaultAttributes
);
142 SetBackgroundColour(*wxWHITE
);
143 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
145 // Tell the sizers to use the given or best size
146 SetBestFittingSize(size
);
148 #if wxRICHTEXT_BUFFERED_PAINTING
150 RecreateBuffer(size
);
153 SetCursor(wxCursor(wxCURSOR_IBEAM
));
155 if (!value
.IsEmpty())
161 wxRichTextCtrl::~wxRichTextCtrl()
163 delete m_contextMenu
;
166 /// Member initialisation
167 void wxRichTextCtrl::Init()
170 m_contextMenu
= NULL
;
172 m_caretPosition
= -1;
173 m_selectionRange
.SetRange(-2, -2);
174 m_selectionAnchor
= -2;
176 m_caretAtLineStart
= false;
178 m_fullLayoutRequired
= false;
179 m_fullLayoutTime
= 0;
180 m_fullLayoutSavedPosition
= 0;
181 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
182 m_caretPositionForDefaultStyle
= -2;
185 /// Call Freeze to prevent refresh
186 void wxRichTextCtrl::Freeze()
191 /// Call Thaw to refresh
192 void wxRichTextCtrl::Thaw()
196 if (m_freezeCount
== 0)
204 void wxRichTextCtrl::Clear()
207 m_buffer
.SetDirty(true);
208 m_caretPosition
= -1;
209 m_caretPositionForDefaultStyle
= -2;
210 m_caretAtLineStart
= false;
211 m_selectionRange
.SetRange(-2, -2);
213 SetScrollbars(0, 0, 0, 0, 0, 0);
215 if (m_freezeCount
== 0)
220 SendTextUpdatedEvent();
224 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
230 #if wxRICHTEXT_BUFFERED_PAINTING
231 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
237 if (m_freezeCount
> 0)
240 dc
.SetFont(GetFont());
242 // Paint the background
245 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
246 wxRect
availableSpace(GetClientSize());
247 if (GetBuffer().GetDirty())
249 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
250 GetBuffer().SetDirty(false);
254 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
263 // Empty implementation, to prevent flicker
264 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
268 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
270 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
279 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
288 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
294 dc
.SetFont(GetFont());
297 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
299 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
301 m_dragStart
= event
.GetLogicalPosition(dc
);
307 bool caretAtLineStart
= false;
309 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
311 // If we're at the start of a line (but not first in para)
312 // then we should keep the caret showing at the start of the line
313 // by showing the m_caretAtLineStart flag.
314 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
315 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
317 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
318 caretAtLineStart
= true;
322 MoveCaret(position
, caretAtLineStart
);
323 SetDefaultStyleToCursorStyle();
330 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
335 if (GetCapture() == this)
341 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
343 if (!event
.Dragging())
351 dc
.SetFont(GetFont());
354 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
355 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
357 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
359 // TODO: test closeness
361 bool caretAtLineStart
= false;
363 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
365 // If we're at the start of a line (but not first in para)
366 // then we should keep the caret showing at the start of the line
367 // by showing the m_caretAtLineStart flag.
368 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
369 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
371 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
372 caretAtLineStart
= true;
376 if (m_caretPosition
!= position
)
378 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
380 MoveCaret(position
, caretAtLineStart
);
381 SetDefaultStyleToCursorStyle();
390 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
396 /// Left-double-click
397 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
399 SelectWord(GetCaretPosition()+1);
404 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
410 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
414 flags
|= wxRICHTEXT_CTRL_DOWN
;
415 if (event
.ShiftDown())
416 flags
|= wxRICHTEXT_SHIFT_DOWN
;
418 flags
|= wxRICHTEXT_ALT_DOWN
;
420 if (event
.GetKeyCode() == WXK_LEFT
||
421 event
.GetKeyCode() == WXK_RIGHT
||
422 event
.GetKeyCode() == WXK_UP
||
423 event
.GetKeyCode() == WXK_DOWN
||
424 event
.GetKeyCode() == WXK_HOME
||
425 event
.GetKeyCode() == WXK_PAGEUP
||
426 event
.GetKeyCode() == WXK_PAGEDOWN
||
427 event
.GetKeyCode() == WXK_END
||
429 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
430 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
431 event
.GetKeyCode() == WXK_NUMPAD_UP
||
432 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
433 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
434 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
435 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
436 event
.GetKeyCode() == WXK_NUMPAD_END
)
438 KeyboardNavigate(event
.GetKeyCode(), flags
);
442 // all the other keys modify the controls contents which shouldn't be
443 // possible if we're read-only
450 if (event
.GetKeyCode() == WXK_RETURN
)
452 BeginBatchUndo(_("Insert Text"));
454 long newPos
= m_caretPosition
;
456 DeleteSelectedContent(& newPos
);
458 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
460 wxRichTextEvent
cmdEvent(
461 wxEVT_COMMAND_RICHTEXT_RETURN
,
463 cmdEvent
.SetEventObject(this);
464 cmdEvent
.SetFlags(flags
);
465 GetEventHandler()->ProcessEvent(cmdEvent
);
468 SetDefaultStyleToCursorStyle();
470 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
472 else if (event
.GetKeyCode() == WXK_BACK
)
474 BeginBatchUndo(_("Delete Text"));
476 // Submit range in character positions, which are greater than caret positions,
477 // so subtract 1 for deleted character and add 1 for conversion to character position.
478 if (m_caretPosition
> -1 && !HasSelection())
480 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
481 m_caretPosition
, // Current caret position
482 m_caretPosition
-1, // New caret position
486 DeleteSelectedContent();
490 // Shouldn't this be in Do()?
491 if (GetLastPosition() == -1)
495 m_caretPosition
= -1;
497 SetDefaultStyleToCursorStyle();
500 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
502 else if (event
.GetKeyCode() == WXK_DELETE
)
504 BeginBatchUndo(_("Delete Text"));
506 // Submit range in character positions, which are greater than caret positions,
507 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
509 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
510 m_caretPosition
, // Current caret position
511 m_caretPosition
+1, // New caret position
515 DeleteSelectedContent();
519 // Shouldn't this be in Do()?
520 if (GetLastPosition() == -1)
524 m_caretPosition
= -1;
526 SetDefaultStyleToCursorStyle();
531 long keycode
= event
.GetKeyCode();
605 case WXK_NUMPAD_SPACE
:
607 case WXK_NUMPAD_ENTER
:
612 case WXK_NUMPAD_HOME
:
613 case WXK_NUMPAD_LEFT
:
615 case WXK_NUMPAD_RIGHT
:
616 case WXK_NUMPAD_DOWN
:
617 case WXK_NUMPAD_PAGEUP
:
618 case WXK_NUMPAD_PAGEDOWN
:
620 case WXK_NUMPAD_BEGIN
:
621 case WXK_NUMPAD_INSERT
:
622 case WXK_NUMPAD_DELETE
:
623 case WXK_NUMPAD_EQUAL
:
624 case WXK_NUMPAD_MULTIPLY
:
626 case WXK_NUMPAD_SEPARATOR
:
627 case WXK_NUMPAD_SUBTRACT
:
628 case WXK_NUMPAD_DECIMAL
:
636 if (event
.CmdDown() || event
.AltDown())
642 BeginBatchUndo(_("Insert Text"));
644 long newPos
= m_caretPosition
;
645 DeleteSelectedContent(& newPos
);
647 wxString str
= (wxChar
) event
.GetKeyCode();
648 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
652 SetDefaultStyleToCursorStyle();
653 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
659 /// Delete content if there is a selection, e.g. when pressing a key.
660 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
664 long pos
= m_selectionRange
.GetStart();
665 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
666 m_caretPosition
, // Current caret position
667 pos
, // New caret position
669 m_selectionRange
.SetRange(-2, -2);
679 /// Keyboard navigation
683 Left: left one character
684 Right: right one character
687 Ctrl-Left: left one word
688 Ctrl-Right: right one word
689 Ctrl-Up: previous paragraph start
690 Ctrl-Down: next start of paragraph
693 Ctrl-Home: start of document
694 Ctrl-End: end of document
696 Page-Down: Down a screen
700 Ctrl-Alt-PgUp: Start of window
701 Ctrl-Alt-PgDn: End of window
702 F8: Start selection mode
703 Esc: End selection mode
705 Adding Shift does the above but starts/extends selection.
710 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
712 bool success
= false;
714 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
716 if (flags
& wxRICHTEXT_CTRL_DOWN
)
717 success
= WordRight(1, flags
);
719 success
= MoveRight(1, flags
);
721 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
723 if (flags
& wxRICHTEXT_CTRL_DOWN
)
724 success
= WordLeft(1, flags
);
726 success
= MoveLeft(1, flags
);
728 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
730 if (flags
& wxRICHTEXT_CTRL_DOWN
)
731 success
= MoveToParagraphStart(flags
);
733 success
= MoveUp(1, flags
);
735 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
737 if (flags
& wxRICHTEXT_CTRL_DOWN
)
738 success
= MoveToParagraphEnd(flags
);
740 success
= MoveDown(1, flags
);
742 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
744 success
= PageUp(1, flags
);
746 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
748 success
= PageDown(1, flags
);
750 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
752 if (flags
& wxRICHTEXT_CTRL_DOWN
)
753 success
= MoveHome(flags
);
755 success
= MoveToLineStart(flags
);
757 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
759 if (flags
& wxRICHTEXT_CTRL_DOWN
)
760 success
= MoveEnd(flags
);
762 success
= MoveToLineEnd(flags
);
767 ScrollIntoView(m_caretPosition
, keyCode
);
768 SetDefaultStyleToCursorStyle();
774 /// Extend the selection. Selections are in caret positions.
775 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
777 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
779 // If not currently selecting, start selecting
780 if (m_selectionRange
.GetStart() == -2)
782 m_selectionAnchor
= oldPos
;
785 m_selectionRange
.SetRange(newPos
+1, oldPos
);
787 m_selectionRange
.SetRange(oldPos
+1, newPos
);
791 // Always ensure that the selection range start is greater than
793 if (newPos
> m_selectionAnchor
)
794 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
796 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
799 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
801 wxLogDebug(wxT("Strange selection range"));
810 /// Scroll into view, returning true if we scrolled.
811 /// This takes a _caret_ position.
812 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
814 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
820 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
822 int startXUnits
, startYUnits
;
823 GetViewStart(& startXUnits
, & startYUnits
);
824 int startY
= startYUnits
* ppuY
;
827 GetVirtualSize(& sx
, & sy
);
833 wxRect rect
= line
->GetRect();
835 bool scrolled
= false;
837 wxSize clientSize
= GetClientSize();
840 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
841 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
842 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
843 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
845 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
847 // Make it scroll so this item is at the bottom
849 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
850 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
852 // If we're still off the screen, scroll another line down
853 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
856 if (startYUnits
!= yUnits
)
858 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
862 else if (rect
.y
< startY
)
864 // Make it scroll so this item is at the top
867 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
869 if (startYUnits
!= yUnits
)
871 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
877 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
878 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
879 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
880 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
884 // Make it scroll so this item is at the top
887 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
889 if (startYUnits
!= yUnits
)
891 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
895 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
897 // Make it scroll so this item is at the bottom
899 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
900 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
902 // If we're still off the screen, scroll another line down
903 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
906 if (startYUnits
!= yUnits
)
908 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
918 /// Is the given position visible on the screen?
919 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
921 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
927 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
930 GetViewStart(& startX
, & startY
);
932 startY
= startY
* ppuY
;
935 GetVirtualSize(& sx
, & sy
);
940 wxRect rect
= line
->GetRect();
942 wxSize clientSize
= GetClientSize();
944 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
947 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
949 m_caretPosition
= position
;
950 m_caretAtLineStart
= showAtLineStart
;
953 /// Move caret one visual step forward: this may mean setting a flag
954 /// and keeping the same position if we're going from the end of one line
955 /// to the start of the next, which may be the exact same caret position.
956 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
958 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
960 // Only do the check if we're not at the end of the paragraph (where things work OK
962 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
964 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
968 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
970 // We're at the end of a line. See whether we need to
971 // stay at the same actual caret position but change visual
973 if (oldPosition
== lineRange
.GetEnd())
975 if (m_caretAtLineStart
)
977 // We're already at the start of the line, so actually move on now.
978 m_caretPosition
= oldPosition
+ 1;
979 m_caretAtLineStart
= false;
983 // We're showing at the end of the line, so keep to
984 // the same position but indicate that we're to show
985 // at the start of the next line.
986 m_caretPosition
= oldPosition
;
987 m_caretAtLineStart
= true;
989 SetDefaultStyleToCursorStyle();
995 SetDefaultStyleToCursorStyle();
998 /// Move caret one visual step backward: this may mean setting a flag
999 /// and keeping the same position if we're going from the end of one line
1000 /// to the start of the next, which may be the exact same caret position.
1001 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1003 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1005 // Only do the check if we're not at the start of the paragraph (where things work OK
1007 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1009 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1013 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1015 // We're at the start of a line. See whether we need to
1016 // stay at the same actual caret position but change visual
1017 // position, or not.
1018 if (oldPosition
== lineRange
.GetStart())
1020 m_caretPosition
= oldPosition
-1;
1021 m_caretAtLineStart
= true;
1024 else if (oldPosition
== lineRange
.GetEnd())
1026 if (m_caretAtLineStart
)
1028 // We're at the start of the line, so keep the same caret position
1029 // but clear the start-of-line flag.
1030 m_caretPosition
= oldPosition
;
1031 m_caretAtLineStart
= false;
1035 // We're showing at the end of the line, so go back
1036 // to the previous character position.
1037 m_caretPosition
= oldPosition
- 1;
1039 SetDefaultStyleToCursorStyle();
1045 SetDefaultStyleToCursorStyle();
1049 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1051 long endPos
= GetBuffer().GetRange().GetEnd();
1053 if (m_caretPosition
+ noPositions
< endPos
)
1055 long oldPos
= m_caretPosition
;
1056 long newPos
= m_caretPosition
+ noPositions
;
1058 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1062 // Determine by looking at oldPos and m_caretPosition whether
1063 // we moved from the end of a line to the start of the next line, in which case
1064 // we want to adjust the caret position such that it is positioned at the
1065 // start of the next line, rather than jumping past the first character of the
1067 if (noPositions
== 1 && !extendSel
)
1068 MoveCaretForward(oldPos
);
1070 SetCaretPosition(newPos
);
1073 SetDefaultStyleToCursorStyle();
1084 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1088 if (m_caretPosition
> startPos
- noPositions
+ 1)
1090 long oldPos
= m_caretPosition
;
1091 long newPos
= m_caretPosition
- noPositions
;
1092 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1096 if (noPositions
== 1 && !extendSel
)
1097 MoveCaretBack(oldPos
);
1099 SetCaretPosition(newPos
);
1102 SetDefaultStyleToCursorStyle();
1113 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1115 return MoveDown(- noLines
, flags
);
1119 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1124 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1125 wxPoint pt
= GetCaret()->GetPosition();
1126 long newLine
= lineNumber
+ noLines
;
1128 if (lineNumber
!= -1)
1132 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1134 if (newLine
> lastLine
)
1144 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1147 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1153 wxClientDC
dc(this);
1155 dc
.SetFont(GetFont());
1157 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1159 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1161 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1162 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1163 // so we view the caret at the start of the line.
1164 bool caretLineStart
= false;
1165 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1167 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1168 wxRichTextRange lineRange
;
1170 lineRange
= thisLine
->GetAbsoluteRange();
1172 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1175 caretLineStart
= true;
1179 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1180 if (para
&& para
->GetRange().GetStart() == newPos
)
1185 long newSelEnd
= newPos
;
1187 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1191 SetCaretPosition(newPos
, caretLineStart
);
1193 SetDefaultStyleToCursorStyle();
1203 /// Move to the end of the paragraph
1204 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1206 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1209 long newPos
= para
->GetRange().GetEnd() - 1;
1210 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1214 SetCaretPosition(newPos
);
1216 SetDefaultStyleToCursorStyle();
1226 /// Move to the start of the paragraph
1227 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1229 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1232 long newPos
= para
->GetRange().GetStart() - 1;
1233 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1237 SetCaretPosition(newPos
);
1239 SetDefaultStyleToCursorStyle();
1249 /// Move to the end of the line
1250 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1252 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1256 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1257 long newPos
= lineRange
.GetEnd();
1258 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1262 SetCaretPosition(newPos
);
1264 SetDefaultStyleToCursorStyle();
1274 /// Move to the start of the line
1275 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1277 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1280 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1281 long newPos
= lineRange
.GetStart()-1;
1283 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1287 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1289 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1291 SetDefaultStyleToCursorStyle();
1301 /// Move to the start of the buffer
1302 bool wxRichTextCtrl::MoveHome(int flags
)
1304 if (m_caretPosition
!= -1)
1306 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1310 SetCaretPosition(-1);
1312 SetDefaultStyleToCursorStyle();
1322 /// Move to the end of the buffer
1323 bool wxRichTextCtrl::MoveEnd(int flags
)
1325 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1327 if (m_caretPosition
!= endPos
)
1329 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1333 SetCaretPosition(endPos
);
1335 SetDefaultStyleToCursorStyle();
1345 /// Move noPages pages up
1346 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1348 return PageDown(- noPages
, flags
);
1351 /// Move noPages pages down
1352 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1354 // Calculate which line occurs noPages * screen height further down.
1355 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1358 wxSize clientSize
= GetClientSize();
1359 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1361 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1364 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1365 long pos
= lineRange
.GetStart()-1;
1366 if (pos
!= m_caretPosition
)
1368 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1370 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1374 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1376 SetDefaultStyleToCursorStyle();
1388 // Finds the caret position for the next word
1389 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1391 long endPos
= GetBuffer().GetRange().GetEnd();
1395 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1397 // First skip current text to space
1398 while (i
< endPos
&& i
> -1)
1400 // i is in character, not caret positions
1401 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1402 if (text
!= wxT(" ") && !text
.empty())
1409 while (i
< endPos
&& i
> -1)
1411 // i is in character, not caret positions
1412 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1413 if (text
.empty()) // End of paragraph, or maybe an image
1414 return wxMax(-1, i
- 1);
1415 else if (text
== wxT(" ") || text
.empty())
1419 // Convert to caret position
1420 return wxMax(-1, i
- 1);
1429 long i
= m_caretPosition
;
1431 // First skip white space
1432 while (i
< endPos
&& i
> -1)
1434 // i is in character, not caret positions
1435 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1436 if (text
.empty()) // End of paragraph, or maybe an image
1438 else if (text
== wxT(" ") || text
.empty())
1443 // Next skip current text to space
1444 while (i
< endPos
&& i
> -1)
1446 // i is in character, not caret positions
1447 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1448 if (text
!= wxT(" ") /* && !text.empty() */)
1461 /// Move n words left
1462 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1464 long pos
= FindNextWordPosition(-1);
1465 if (pos
!= m_caretPosition
)
1467 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1469 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1473 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1475 SetDefaultStyleToCursorStyle();
1485 /// Move n words right
1486 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1488 long pos
= FindNextWordPosition(1);
1489 if (pos
!= m_caretPosition
)
1491 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1493 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1497 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1499 SetDefaultStyleToCursorStyle();
1510 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1512 // Only do sizing optimization for large buffers
1513 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1515 m_fullLayoutRequired
= true;
1516 m_fullLayoutTime
= wxGetLocalTimeMillis();
1517 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1518 LayoutContent(true /* onlyVisibleRect */);
1521 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1523 #if wxRICHTEXT_BUFFERED_PAINTING
1531 /// Idle-time processing
1532 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1534 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1536 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1538 m_fullLayoutRequired
= false;
1539 m_fullLayoutTime
= 0;
1540 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1541 ShowPosition(m_fullLayoutSavedPosition
);
1545 if (m_caretPositionForDefaultStyle
!= -2)
1547 // If the caret position has changed, no longer reflect the default style
1549 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1550 m_caretPositionForDefaultStyle
= -2;
1557 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1563 /// Set up scrollbars, e.g. after a resize
1564 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1569 if (GetBuffer().IsEmpty())
1571 SetScrollbars(0, 0, 0, 0, 0, 0);
1575 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1576 // of pixels. See e.g. wxVScrolledWindow for ideas.
1577 int pixelsPerUnit
= 5;
1578 wxSize clientSize
= GetClientSize();
1580 int maxHeight
= GetBuffer().GetCachedSize().y
;
1582 // Round up so we have at least maxHeight pixels
1583 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1585 int startX
= 0, startY
= 0;
1587 GetViewStart(& startX
, & startY
);
1589 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1590 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1592 // Move to previous scroll position if
1594 SetScrollbars(0, pixelsPerUnit
,
1596 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1599 /// Paint the background
1600 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1602 wxColour backgroundColour
= GetBackgroundColour();
1603 if (!backgroundColour
.Ok())
1604 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1606 // Clear the background
1607 dc
.SetBrush(wxBrush(backgroundColour
));
1608 dc
.SetPen(*wxTRANSPARENT_PEN
);
1609 wxRect
windowRect(GetClientSize());
1610 windowRect
.x
-= 2; windowRect
.y
-= 2;
1611 windowRect
.width
+= 4; windowRect
.height
+= 4;
1613 // We need to shift the rectangle to take into account
1614 // scrolling. Converting device to logical coordinates.
1615 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1616 dc
.DrawRectangle(windowRect
);
1619 #if wxRICHTEXT_BUFFERED_PAINTING
1620 /// Recreate buffer bitmap if necessary
1621 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1624 if (sz
== wxDefaultSize
)
1625 sz
= GetClientSize();
1627 if (sz
.x
< 1 || sz
.y
< 1)
1630 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1631 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1632 return m_bufferBitmap
.Ok();
1636 // ----------------------------------------------------------------------------
1637 // file IO functions
1638 // ----------------------------------------------------------------------------
1640 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1642 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1644 m_filename
= filename
;
1647 SetInsertionPoint(0);
1650 SetupScrollbars(true);
1652 SendTextUpdatedEvent();
1658 wxLogError(_("File couldn't be loaded."));
1664 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1666 if (GetBuffer().SaveFile(filename
, fileType
))
1668 m_filename
= filename
;
1675 wxLogError(_("The text couldn't be saved."));
1680 // ----------------------------------------------------------------------------
1681 // wxRichTextCtrl specific functionality
1682 // ----------------------------------------------------------------------------
1684 /// Add a new paragraph of text to the end of the buffer
1685 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1687 return GetBuffer().AddParagraph(text
);
1691 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1693 return GetBuffer().AddImage(image
);
1696 // ----------------------------------------------------------------------------
1697 // selection and ranges
1698 // ----------------------------------------------------------------------------
1700 void wxRichTextCtrl::SelectAll()
1702 SetSelection(0, GetLastPosition()+1);
1703 m_selectionAnchor
= -1;
1707 void wxRichTextCtrl::SelectNone()
1709 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1710 SetSelection(-2, -2);
1711 m_selectionAnchor
= -2;
1714 static bool wxIsWordDelimiter(const wxString
& text
)
1716 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1719 /// Select the word at the given character position
1720 bool wxRichTextCtrl::SelectWord(long position
)
1722 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1725 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1729 long positionStart
= position
;
1730 long positionEnd
= position
;
1732 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1734 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1735 if (wxIsWordDelimiter(text
))
1741 if (positionStart
< para
->GetRange().GetStart())
1742 positionStart
= para
->GetRange().GetStart();
1744 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1746 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1747 if (wxIsWordDelimiter(text
))
1753 if (positionEnd
>= para
->GetRange().GetEnd())
1754 positionEnd
= para
->GetRange().GetEnd();
1756 SetSelection(positionStart
, positionEnd
+1);
1758 if (positionStart
>= 0)
1760 MoveCaret(positionStart
-1, true);
1761 SetDefaultStyleToCursorStyle();
1767 wxString
wxRichTextCtrl::GetStringSelection() const
1770 GetSelection(&from
, &to
);
1772 return GetRange(from
, to
);
1775 // ----------------------------------------------------------------------------
1777 // ----------------------------------------------------------------------------
1779 wxTextCtrlHitTestResult
1780 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1782 // implement in terms of the other overload as the native ports typically
1783 // can get the position and not (x, y) pair directly (although wxUniv
1784 // directly gets x and y -- and so overrides this method as well)
1786 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1788 if ( rc
!= wxTE_HT_UNKNOWN
)
1790 PositionToXY(pos
, x
, y
);
1796 wxTextCtrlHitTestResult
1797 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1800 wxClientDC
dc((wxRichTextCtrl
*) this);
1801 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1803 // Buffer uses logical position (relative to start of buffer)
1805 wxPoint pt2
= GetLogicalPoint(pt
);
1807 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1811 case wxRICHTEXT_HITTEST_BEFORE
:
1812 return wxTE_HT_BEFORE
;
1814 case wxRICHTEXT_HITTEST_AFTER
:
1815 return wxTE_HT_BEYOND
;
1817 case wxRICHTEXT_HITTEST_ON
:
1818 return wxTE_HT_ON_TEXT
;
1821 return wxTE_HT_UNKNOWN
;
1824 // ----------------------------------------------------------------------------
1825 // set/get the controls text
1826 // ----------------------------------------------------------------------------
1828 wxString
wxRichTextCtrl::GetValue() const
1830 return GetBuffer().GetText();
1833 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1835 // Public API for range is different from internals
1836 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1839 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1843 // if the text is long enough, it's faster to just set it instead of first
1844 // comparing it with the old one (chances are that it will be different
1845 // anyhow, this comparison is there to avoid flicker for small single-line
1846 // edit controls mostly)
1847 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1851 // for compatibility, don't move the cursor when doing SetValue()
1852 SetInsertionPoint(0);
1856 if ( flags
& SetValue_SendEvent
)
1858 // still send an event for consistency
1859 SendTextUpdatedEvent();
1863 // we should reset the modified flag even if the value didn't really change
1865 // mark the control as being not dirty - we changed its text, not the
1870 void wxRichTextCtrl::WriteText(const wxString
& value
)
1875 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
1877 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1879 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
1881 if ( flags
& SetValue_SendEvent
)
1882 SendTextUpdatedEvent();
1885 void wxRichTextCtrl::AppendText(const wxString
& text
)
1887 SetInsertionPointEnd();
1892 /// Write an image at the current insertion point
1893 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1895 wxRichTextImageBlock imageBlock
;
1897 wxImage image2
= image
;
1898 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1899 return WriteImage(imageBlock
);
1904 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1906 wxRichTextImageBlock imageBlock
;
1909 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1910 return WriteImage(imageBlock
);
1915 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1917 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1920 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1924 wxRichTextImageBlock imageBlock
;
1926 wxImage image
= bitmap
.ConvertToImage();
1927 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1928 return WriteImage(imageBlock
);
1934 /// Insert a newline (actually paragraph) at the current insertion point.
1935 bool wxRichTextCtrl::Newline()
1937 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1941 // ----------------------------------------------------------------------------
1942 // Clipboard operations
1943 // ----------------------------------------------------------------------------
1945 void wxRichTextCtrl::Copy()
1949 wxRichTextRange range
= GetInternalSelectionRange();
1950 GetBuffer().CopyToClipboard(range
);
1954 void wxRichTextCtrl::Cut()
1958 wxRichTextRange range
= GetInternalSelectionRange();
1959 GetBuffer().CopyToClipboard(range
);
1961 DeleteSelectedContent();
1967 void wxRichTextCtrl::Paste()
1971 BeginBatchUndo(_("Paste"));
1973 long newPos
= m_caretPosition
;
1974 DeleteSelectedContent(& newPos
);
1976 GetBuffer().PasteFromClipboard(newPos
);
1982 void wxRichTextCtrl::DeleteSelection()
1984 if (CanDeleteSelection())
1986 DeleteSelectedContent();
1990 bool wxRichTextCtrl::HasSelection() const
1992 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1995 bool wxRichTextCtrl::CanCopy() const
1997 // Can copy if there's a selection
1998 return HasSelection();
2001 bool wxRichTextCtrl::CanCut() const
2003 return HasSelection() && IsEditable();
2006 bool wxRichTextCtrl::CanPaste() const
2008 if ( !IsEditable() )
2011 return GetBuffer().CanPasteFromClipboard();
2014 bool wxRichTextCtrl::CanDeleteSelection() const
2016 return HasSelection() && IsEditable();
2020 // ----------------------------------------------------------------------------
2022 // ----------------------------------------------------------------------------
2024 void wxRichTextCtrl::SetEditable(bool editable
)
2026 m_editable
= editable
;
2029 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2033 m_caretPosition
= pos
- 1;
2036 void wxRichTextCtrl::SetInsertionPointEnd()
2038 long pos
= GetLastPosition();
2039 SetInsertionPoint(pos
);
2042 long wxRichTextCtrl::GetInsertionPoint() const
2044 return m_caretPosition
+1;
2047 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2049 return GetBuffer().GetRange().GetEnd();
2052 // If the return values from and to are the same, there is no
2054 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2056 *from
= m_selectionRange
.GetStart();
2057 *to
= m_selectionRange
.GetEnd();
2058 if ((*to
) != -1 && (*to
) != -2)
2062 bool wxRichTextCtrl::IsEditable() const
2067 // ----------------------------------------------------------------------------
2069 // ----------------------------------------------------------------------------
2071 void wxRichTextCtrl::SetSelection(long from
, long to
)
2073 // if from and to are both -1, it means (in wxWidgets) that all text should
2075 if ( (from
== -1) && (to
== -1) )
2078 to
= GetLastPosition()+1;
2081 DoSetSelection(from
, to
);
2084 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2086 m_selectionAnchor
= from
;
2087 m_selectionRange
.SetRange(from
, to
-1);
2093 // ----------------------------------------------------------------------------
2095 // ----------------------------------------------------------------------------
2097 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2098 const wxString
& value
)
2100 BeginBatchUndo(_("Replace"));
2102 DeleteSelectedContent();
2104 DoWriteText(value
, SetValue_SelectionOnly
);
2109 void wxRichTextCtrl::Remove(long from
, long to
)
2113 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2114 m_caretPosition
, // Current caret position
2115 from
, // New caret position
2123 bool wxRichTextCtrl::IsModified() const
2125 return m_buffer
.IsModified();
2128 void wxRichTextCtrl::MarkDirty()
2130 m_buffer
.Modify(true);
2133 void wxRichTextCtrl::DiscardEdits()
2135 m_caretPositionForDefaultStyle
= -2;
2136 m_buffer
.Modify(false);
2137 m_buffer
.GetCommandProcessor()->ClearCommands();
2140 int wxRichTextCtrl::GetNumberOfLines() const
2142 return GetBuffer().GetParagraphCount();
2145 // ----------------------------------------------------------------------------
2146 // Positions <-> coords
2147 // ----------------------------------------------------------------------------
2149 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2151 return GetBuffer().XYToPosition(x
, y
);
2154 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2156 return GetBuffer().PositionToXY(pos
, x
, y
);
2159 // ----------------------------------------------------------------------------
2161 // ----------------------------------------------------------------------------
2163 void wxRichTextCtrl::ShowPosition(long pos
)
2165 if (!IsPositionVisible(pos
))
2166 ScrollIntoView(pos
-1, WXK_DOWN
);
2169 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2171 return GetBuffer().GetParagraphLength(lineNo
);
2174 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2176 return GetBuffer().GetParagraphText(lineNo
);
2179 // ----------------------------------------------------------------------------
2181 // ----------------------------------------------------------------------------
2183 void wxRichTextCtrl::Undo()
2187 GetCommandProcessor()->Undo();
2191 void wxRichTextCtrl::Redo()
2195 GetCommandProcessor()->Redo();
2199 bool wxRichTextCtrl::CanUndo() const
2201 return GetCommandProcessor()->CanUndo();
2204 bool wxRichTextCtrl::CanRedo() const
2206 return GetCommandProcessor()->CanRedo();
2209 // ----------------------------------------------------------------------------
2210 // implementation details
2211 // ----------------------------------------------------------------------------
2213 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2215 SetValue(event
.GetString());
2216 GetEventHandler()->ProcessEvent(event
);
2219 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2221 // By default, load the first file into the text window.
2222 if (event
.GetNumberOfFiles() > 0)
2224 LoadFile(event
.GetFiles()[0]);
2228 wxSize
wxRichTextCtrl::DoGetBestSize() const
2230 return wxSize(10, 10);
2233 // ----------------------------------------------------------------------------
2234 // standard handlers for standard edit menu events
2235 // ----------------------------------------------------------------------------
2237 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2242 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2247 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2252 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2257 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2262 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2267 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2269 event
.Enable( CanCut() );
2272 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2274 event
.Enable( CanCopy() );
2277 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2279 event
.Enable( CanDeleteSelection() );
2282 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2284 event
.Enable( CanPaste() );
2287 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2289 event
.Enable( CanUndo() );
2290 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2293 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2295 event
.Enable( CanRedo() );
2296 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2299 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2304 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2306 event
.Enable(GetLastPosition() > 0);
2309 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2313 m_contextMenu
= new wxMenu
;
2314 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2315 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2316 m_contextMenu
->AppendSeparator();
2317 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2318 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2319 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2320 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2321 m_contextMenu
->AppendSeparator();
2322 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2324 PopupMenu(m_contextMenu
);
2328 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2330 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2333 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2335 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2338 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2340 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2343 // extended style setting operation with flags including:
2344 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2345 // see richtextbuffer.h for more details.
2346 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2348 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2351 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2353 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2356 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2358 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2361 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2363 return GetBuffer().SetDefaultStyle(style
);
2366 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2368 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2371 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2373 return GetBuffer().GetDefaultStyle();
2376 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2378 return GetBuffer().GetDefaultStyle();
2381 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2383 wxTextAttrEx
attr(style
);
2384 if (GetBuffer().GetStyle(position
, attr
))
2393 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2395 return GetBuffer().GetStyle(position
, style
);
2398 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2400 return GetBuffer().GetStyle(position
, style
);
2403 /// Get the content (uncombined) attributes for this position.
2405 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2407 wxTextAttrEx
attr(style
);
2408 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2417 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2419 return GetBuffer().GetUncombinedStyle(position
, style
);
2422 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2424 return GetBuffer().GetUncombinedStyle(position
, style
);
2427 /// Set font, and also the buffer attributes
2428 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2430 wxControl::SetFont(font
);
2432 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2434 GetBuffer().SetBasicStyle(attr
);
2436 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2442 /// Transform logical to physical
2443 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2446 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2451 /// Transform physical to logical
2452 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2455 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2460 /// Position the caret
2461 void wxRichTextCtrl::PositionCaret()
2466 //wxLogDebug(wxT("PositionCaret"));
2469 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2471 wxPoint originalPt
= caretRect
.GetPosition();
2472 wxPoint pt
= GetPhysicalPoint(originalPt
);
2473 if (GetCaret()->GetPosition() != pt
)
2475 GetCaret()->Move(pt
);
2476 GetCaret()->SetSize(caretRect
.GetSize());
2481 /// Get the caret height and position for the given character position
2482 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2484 wxClientDC
dc(this);
2485 dc
.SetFont(GetFont());
2492 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2494 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2501 /// Gets the line for the visible caret position. If the caret is
2502 /// shown at the very end of the line, it means the next character is actually
2503 /// on the following line. So let's get the line we're expecting to find
2504 /// if this is the case.
2505 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2507 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2508 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2511 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2512 if (caretPosition
== lineRange
.GetStart()-1 &&
2513 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2515 if (!m_caretAtLineStart
)
2516 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2523 /// Move the caret to the given character position
2524 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2526 if (GetBuffer().GetDirty())
2529 if (pos
<= GetBuffer().GetRange().GetEnd())
2531 SetCaretPosition(pos
, showAtLineStart
);
2541 /// Layout the buffer: which we must do before certain operations, such as
2542 /// setting the caret position.
2543 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2545 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2547 wxRect
availableSpace(GetClientSize());
2548 if (availableSpace
.width
== 0)
2549 availableSpace
.width
= 10;
2550 if (availableSpace
.height
== 0)
2551 availableSpace
.height
= 10;
2553 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2554 if (onlyVisibleRect
)
2556 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2557 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2560 wxClientDC
dc(this);
2561 dc
.SetFont(GetFont());
2565 GetBuffer().Defragment();
2566 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2567 GetBuffer().Layout(dc
, availableSpace
, flags
);
2568 GetBuffer().SetDirty(false);
2577 /// Is all of the selection bold?
2578 bool wxRichTextCtrl::IsSelectionBold()
2582 wxRichTextAttr attr
;
2583 wxRichTextRange range
= GetInternalSelectionRange();
2584 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2585 attr
.SetFontWeight(wxBOLD
);
2587 return HasCharacterAttributes(range
, attr
);
2591 // If no selection, then we need to combine current style with default style
2592 // to see what the effect would be if we started typing.
2593 wxRichTextAttr attr
;
2594 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2596 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2597 if (GetStyle(pos
, attr
))
2599 if (IsDefaultStyleShowing())
2600 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2601 return attr
.GetFontWeight() == wxBOLD
;
2607 /// Is all of the selection italics?
2608 bool wxRichTextCtrl::IsSelectionItalics()
2612 wxRichTextRange range
= GetInternalSelectionRange();
2613 wxRichTextAttr attr
;
2614 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2615 attr
.SetFontStyle(wxITALIC
);
2617 return HasCharacterAttributes(range
, attr
);
2621 // If no selection, then we need to combine current style with default style
2622 // to see what the effect would be if we started typing.
2623 wxRichTextAttr attr
;
2624 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2626 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2627 if (GetStyle(pos
, attr
))
2629 if (IsDefaultStyleShowing())
2630 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2631 return attr
.GetFontStyle() == wxITALIC
;
2637 /// Is all of the selection underlined?
2638 bool wxRichTextCtrl::IsSelectionUnderlined()
2642 wxRichTextRange range
= GetInternalSelectionRange();
2643 wxRichTextAttr attr
;
2644 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2645 attr
.SetFontUnderlined(true);
2647 return HasCharacterAttributes(range
, attr
);
2651 // If no selection, then we need to combine current style with default style
2652 // to see what the effect would be if we started typing.
2653 wxRichTextAttr attr
;
2654 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2655 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2657 if (GetStyle(pos
, attr
))
2659 if (IsDefaultStyleShowing())
2660 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2661 return attr
.GetFontUnderlined();
2667 /// Apply bold to the selection
2668 bool wxRichTextCtrl::ApplyBoldToSelection()
2670 wxRichTextAttr attr
;
2671 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2672 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2675 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2677 SetAndShowDefaultStyle(attr
);
2681 /// Apply italic to the selection
2682 bool wxRichTextCtrl::ApplyItalicToSelection()
2684 wxRichTextAttr attr
;
2685 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2686 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2689 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2691 SetAndShowDefaultStyle(attr
);
2695 /// Apply underline to the selection
2696 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2698 wxRichTextAttr attr
;
2699 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2700 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2703 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2705 SetAndShowDefaultStyle(attr
);
2709 /// Is all of the selection aligned according to the specified flag?
2710 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2712 wxRichTextRange range
;
2714 range
= GetInternalSelectionRange();
2716 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2718 wxRichTextAttr attr
;
2719 attr
.SetAlignment(alignment
);
2721 return HasParagraphAttributes(range
, attr
);
2724 /// Apply alignment to the selection
2725 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2727 wxRichTextAttr attr
;
2728 attr
.SetAlignment(alignment
);
2730 return SetStyle(GetSelectionRange(), attr
);
2733 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2735 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2740 /// Apply a named style to the selection
2741 void wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2743 // Flags are defined within each definition, so only certain
2744 // attributes are applied.
2745 wxRichTextAttr
attr(def
->GetStyle());
2747 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2749 // Make sure the attr has the style name
2750 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2752 attr
.SetParagraphStyleName(def
->GetName());
2754 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2755 // attributes, and not the leaf nodes. This will allow the context (e.g. text)
2756 // to change its style independently.
2757 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2760 attr
.SetCharacterStyleName(def
->GetName());
2763 SetStyleEx(GetSelectionRange(), attr
, flags
);
2765 SetAndShowDefaultStyle(attr
);
2768 /// Apply the style sheet to the buffer, for example if the styles have changed.
2769 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2772 styleSheet
= GetBuffer().GetStyleSheet();
2776 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2778 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2786 /// Sets the default style to the style under the cursor
2787 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2790 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2792 // If at the start of a paragraph, use the next position.
2793 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2795 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2796 if (GetUncombinedStyle(pos
, attr
))
2798 if (GetStyle(pos
, attr
))
2801 SetDefaultStyle(attr
);
2808 /// Returns the first visible position in the current view
2809 long wxRichTextCtrl::GetFirstVisiblePosition() const
2811 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2813 return line
->GetAbsoluteRange().GetStart();
2818 /// The adjusted caret position is the character position adjusted to take
2819 /// into account whether we're at the start of a paragraph, in which case
2820 /// style information should be taken from the next position, not current one.
2821 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2823 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2825 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2830 /// Get/set the selection range in character positions. -1, -1 means no selection.
2831 /// The range is in API convention, i.e. a single character selection is denoted
2833 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2835 wxRichTextRange range
= GetInternalSelectionRange();
2836 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2837 range
.SetEnd(range
.GetEnd() + 1);
2841 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2843 wxRichTextRange
range1(range
);
2844 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2845 range1
.SetEnd(range1
.GetEnd() - 1);
2847 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2849 SetInternalSelectionRange(range1
);