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 wxRegion dirtyRegion
= GetUpdateRegion();
247 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
248 wxRect
availableSpace(GetClientSize());
249 if (GetBuffer().GetDirty())
251 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
252 GetBuffer().SetDirty(false);
256 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
265 // Empty implementation, to prevent flicker
266 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
270 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
272 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
281 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
290 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
296 dc
.SetFont(GetFont());
299 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
301 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
303 m_dragStart
= event
.GetLogicalPosition(dc
);
309 bool caretAtLineStart
= false;
311 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
313 // If we're at the start of a line (but not first in para)
314 // then we should keep the caret showing at the start of the line
315 // by showing the m_caretAtLineStart flag.
316 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
317 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
319 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
320 caretAtLineStart
= true;
324 MoveCaret(position
, caretAtLineStart
);
325 SetDefaultStyleToCursorStyle();
332 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
337 if (GetCapture() == this)
343 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
345 if (!event
.Dragging())
353 dc
.SetFont(GetFont());
356 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
357 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
359 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
361 // TODO: test closeness
363 bool caretAtLineStart
= false;
365 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
367 // If we're at the start of a line (but not first in para)
368 // then we should keep the caret showing at the start of the line
369 // by showing the m_caretAtLineStart flag.
370 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
371 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
373 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
374 caretAtLineStart
= true;
378 if (m_caretPosition
!= position
)
380 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
382 MoveCaret(position
, caretAtLineStart
);
383 SetDefaultStyleToCursorStyle();
392 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
398 /// Left-double-click
399 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
401 SelectWord(GetCaretPosition()+1);
406 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
412 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
416 flags
|= wxRICHTEXT_CTRL_DOWN
;
417 if (event
.ShiftDown())
418 flags
|= wxRICHTEXT_SHIFT_DOWN
;
420 flags
|= wxRICHTEXT_ALT_DOWN
;
422 if (event
.GetKeyCode() == WXK_LEFT
||
423 event
.GetKeyCode() == WXK_RIGHT
||
424 event
.GetKeyCode() == WXK_UP
||
425 event
.GetKeyCode() == WXK_DOWN
||
426 event
.GetKeyCode() == WXK_HOME
||
427 event
.GetKeyCode() == WXK_PAGEUP
||
428 event
.GetKeyCode() == WXK_PAGEDOWN
||
429 event
.GetKeyCode() == WXK_END
||
431 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
432 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
433 event
.GetKeyCode() == WXK_NUMPAD_UP
||
434 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
435 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
436 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
437 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
438 event
.GetKeyCode() == WXK_NUMPAD_END
)
440 KeyboardNavigate(event
.GetKeyCode(), flags
);
444 // all the other keys modify the controls contents which shouldn't be
445 // possible if we're read-only
452 if (event
.GetKeyCode() == WXK_RETURN
)
454 BeginBatchUndo(_("Insert Text"));
456 long newPos
= m_caretPosition
;
458 DeleteSelectedContent(& newPos
);
460 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
462 wxRichTextEvent
cmdEvent(
463 wxEVT_COMMAND_RICHTEXT_RETURN
,
465 cmdEvent
.SetEventObject(this);
466 cmdEvent
.SetFlags(flags
);
467 GetEventHandler()->ProcessEvent(cmdEvent
);
470 SetDefaultStyleToCursorStyle();
472 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
474 else if (event
.GetKeyCode() == WXK_BACK
)
476 BeginBatchUndo(_("Delete Text"));
478 // Submit range in character positions, which are greater than caret positions,
479 // so subtract 1 for deleted character and add 1 for conversion to character position.
480 if (m_caretPosition
> -1 && !HasSelection())
482 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
483 m_caretPosition
, // Current caret position
484 m_caretPosition
-1, // New caret position
488 DeleteSelectedContent();
492 // Shouldn't this be in Do()?
493 if (GetLastPosition() == -1)
497 m_caretPosition
= -1;
499 SetDefaultStyleToCursorStyle();
502 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
504 else if (event
.GetKeyCode() == WXK_DELETE
)
506 BeginBatchUndo(_("Delete Text"));
508 // Submit range in character positions, which are greater than caret positions,
509 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
511 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
512 m_caretPosition
, // Current caret position
513 m_caretPosition
+1, // New caret position
517 DeleteSelectedContent();
521 // Shouldn't this be in Do()?
522 if (GetLastPosition() == -1)
526 m_caretPosition
= -1;
528 SetDefaultStyleToCursorStyle();
533 long keycode
= event
.GetKeyCode();
607 case WXK_NUMPAD_SPACE
:
609 case WXK_NUMPAD_ENTER
:
614 case WXK_NUMPAD_HOME
:
615 case WXK_NUMPAD_LEFT
:
617 case WXK_NUMPAD_RIGHT
:
618 case WXK_NUMPAD_DOWN
:
619 case WXK_NUMPAD_PAGEUP
:
620 case WXK_NUMPAD_PAGEDOWN
:
622 case WXK_NUMPAD_BEGIN
:
623 case WXK_NUMPAD_INSERT
:
624 case WXK_NUMPAD_DELETE
:
625 case WXK_NUMPAD_EQUAL
:
626 case WXK_NUMPAD_MULTIPLY
:
628 case WXK_NUMPAD_SEPARATOR
:
629 case WXK_NUMPAD_SUBTRACT
:
630 case WXK_NUMPAD_DECIMAL
:
638 if (event
.CmdDown() || event
.AltDown())
644 BeginBatchUndo(_("Insert Text"));
646 long newPos
= m_caretPosition
;
647 DeleteSelectedContent(& newPos
);
649 wxString str
= (wxChar
) event
.GetKeyCode();
650 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
654 SetDefaultStyleToCursorStyle();
655 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
661 /// Delete content if there is a selection, e.g. when pressing a key.
662 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
666 long pos
= m_selectionRange
.GetStart();
667 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
668 m_caretPosition
, // Current caret position
669 pos
, // New caret position
671 m_selectionRange
.SetRange(-2, -2);
681 /// Keyboard navigation
685 Left: left one character
686 Right: right one character
689 Ctrl-Left: left one word
690 Ctrl-Right: right one word
691 Ctrl-Up: previous paragraph start
692 Ctrl-Down: next start of paragraph
695 Ctrl-Home: start of document
696 Ctrl-End: end of document
698 Page-Down: Down a screen
702 Ctrl-Alt-PgUp: Start of window
703 Ctrl-Alt-PgDn: End of window
704 F8: Start selection mode
705 Esc: End selection mode
707 Adding Shift does the above but starts/extends selection.
712 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
714 bool success
= false;
716 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
718 if (flags
& wxRICHTEXT_CTRL_DOWN
)
719 success
= WordRight(1, flags
);
721 success
= MoveRight(1, flags
);
723 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
725 if (flags
& wxRICHTEXT_CTRL_DOWN
)
726 success
= WordLeft(1, flags
);
728 success
= MoveLeft(1, flags
);
730 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
732 if (flags
& wxRICHTEXT_CTRL_DOWN
)
733 success
= MoveToParagraphStart(flags
);
735 success
= MoveUp(1, flags
);
737 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
739 if (flags
& wxRICHTEXT_CTRL_DOWN
)
740 success
= MoveToParagraphEnd(flags
);
742 success
= MoveDown(1, flags
);
744 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
746 success
= PageUp(1, flags
);
748 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
750 success
= PageDown(1, flags
);
752 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
754 if (flags
& wxRICHTEXT_CTRL_DOWN
)
755 success
= MoveHome(flags
);
757 success
= MoveToLineStart(flags
);
759 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
761 if (flags
& wxRICHTEXT_CTRL_DOWN
)
762 success
= MoveEnd(flags
);
764 success
= MoveToLineEnd(flags
);
769 ScrollIntoView(m_caretPosition
, keyCode
);
770 SetDefaultStyleToCursorStyle();
776 /// Extend the selection. Selections are in caret positions.
777 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
779 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
781 // If not currently selecting, start selecting
782 if (m_selectionRange
.GetStart() == -2)
784 m_selectionAnchor
= oldPos
;
787 m_selectionRange
.SetRange(newPos
+1, oldPos
);
789 m_selectionRange
.SetRange(oldPos
+1, newPos
);
793 // Always ensure that the selection range start is greater than
795 if (newPos
> m_selectionAnchor
)
796 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
798 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
801 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
803 wxLogDebug(wxT("Strange selection range"));
812 /// Scroll into view, returning true if we scrolled.
813 /// This takes a _caret_ position.
814 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
816 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
822 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
824 int startXUnits
, startYUnits
;
825 GetViewStart(& startXUnits
, & startYUnits
);
826 int startY
= startYUnits
* ppuY
;
829 GetVirtualSize(& sx
, & sy
);
835 wxRect rect
= line
->GetRect();
837 bool scrolled
= false;
839 wxSize clientSize
= GetClientSize();
842 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
843 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
844 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
845 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
847 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
849 // Make it scroll so this item is at the bottom
851 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
852 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
854 // If we're still off the screen, scroll another line down
855 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
858 if (startYUnits
!= yUnits
)
860 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
864 else if (rect
.y
< startY
)
866 // Make it scroll so this item is at the top
869 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
871 if (startYUnits
!= yUnits
)
873 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
879 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
880 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
881 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
882 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
886 // Make it scroll so this item is at the top
889 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
891 if (startYUnits
!= yUnits
)
893 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
897 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
899 // Make it scroll so this item is at the bottom
901 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
902 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
904 // If we're still off the screen, scroll another line down
905 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
908 if (startYUnits
!= yUnits
)
910 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
920 /// Is the given position visible on the screen?
921 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
923 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
929 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
932 GetViewStart(& startX
, & startY
);
934 startY
= startY
* ppuY
;
937 GetVirtualSize(& sx
, & sy
);
942 wxRect rect
= line
->GetRect();
944 wxSize clientSize
= GetClientSize();
946 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
949 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
951 m_caretPosition
= position
;
952 m_caretAtLineStart
= showAtLineStart
;
955 /// Move caret one visual step forward: this may mean setting a flag
956 /// and keeping the same position if we're going from the end of one line
957 /// to the start of the next, which may be the exact same caret position.
958 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
960 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
962 // Only do the check if we're not at the end of the paragraph (where things work OK
964 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
966 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
970 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
972 // We're at the end of a line. See whether we need to
973 // stay at the same actual caret position but change visual
975 if (oldPosition
== lineRange
.GetEnd())
977 if (m_caretAtLineStart
)
979 // We're already at the start of the line, so actually move on now.
980 m_caretPosition
= oldPosition
+ 1;
981 m_caretAtLineStart
= false;
985 // We're showing at the end of the line, so keep to
986 // the same position but indicate that we're to show
987 // at the start of the next line.
988 m_caretPosition
= oldPosition
;
989 m_caretAtLineStart
= true;
991 SetDefaultStyleToCursorStyle();
997 SetDefaultStyleToCursorStyle();
1000 /// Move caret one visual step backward: this may mean setting a flag
1001 /// and keeping the same position if we're going from the end of one line
1002 /// to the start of the next, which may be the exact same caret position.
1003 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1005 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1007 // Only do the check if we're not at the start of the paragraph (where things work OK
1009 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1011 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1015 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1017 // We're at the start of a line. See whether we need to
1018 // stay at the same actual caret position but change visual
1019 // position, or not.
1020 if (oldPosition
== lineRange
.GetStart())
1022 m_caretPosition
= oldPosition
-1;
1023 m_caretAtLineStart
= true;
1026 else if (oldPosition
== lineRange
.GetEnd())
1028 if (m_caretAtLineStart
)
1030 // We're at the start of the line, so keep the same caret position
1031 // but clear the start-of-line flag.
1032 m_caretPosition
= oldPosition
;
1033 m_caretAtLineStart
= false;
1037 // We're showing at the end of the line, so go back
1038 // to the previous character position.
1039 m_caretPosition
= oldPosition
- 1;
1041 SetDefaultStyleToCursorStyle();
1047 SetDefaultStyleToCursorStyle();
1051 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1053 long endPos
= GetBuffer().GetRange().GetEnd();
1055 if (m_caretPosition
+ noPositions
< endPos
)
1057 long oldPos
= m_caretPosition
;
1058 long newPos
= m_caretPosition
+ noPositions
;
1060 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1064 // Determine by looking at oldPos and m_caretPosition whether
1065 // we moved from the end of a line to the start of the next line, in which case
1066 // we want to adjust the caret position such that it is positioned at the
1067 // start of the next line, rather than jumping past the first character of the
1069 if (noPositions
== 1 && !extendSel
)
1070 MoveCaretForward(oldPos
);
1072 SetCaretPosition(newPos
);
1075 SetDefaultStyleToCursorStyle();
1086 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1090 if (m_caretPosition
> startPos
- noPositions
+ 1)
1092 long oldPos
= m_caretPosition
;
1093 long newPos
= m_caretPosition
- noPositions
;
1094 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1098 if (noPositions
== 1 && !extendSel
)
1099 MoveCaretBack(oldPos
);
1101 SetCaretPosition(newPos
);
1104 SetDefaultStyleToCursorStyle();
1115 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1117 return MoveDown(- noLines
, flags
);
1121 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1126 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1127 wxPoint pt
= GetCaret()->GetPosition();
1128 long newLine
= lineNumber
+ noLines
;
1130 if (lineNumber
!= -1)
1134 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1136 if (newLine
> lastLine
)
1146 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1149 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1155 wxClientDC
dc(this);
1157 dc
.SetFont(GetFont());
1159 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1161 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1163 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1164 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1165 // so we view the caret at the start of the line.
1166 bool caretLineStart
= false;
1167 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1169 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1170 wxRichTextRange lineRange
;
1172 lineRange
= thisLine
->GetAbsoluteRange();
1174 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1177 caretLineStart
= true;
1181 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1182 if (para
&& para
->GetRange().GetStart() == newPos
)
1187 long newSelEnd
= newPos
;
1189 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1193 SetCaretPosition(newPos
, caretLineStart
);
1195 SetDefaultStyleToCursorStyle();
1205 /// Move to the end of the paragraph
1206 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1208 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1211 long newPos
= para
->GetRange().GetEnd() - 1;
1212 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1216 SetCaretPosition(newPos
);
1218 SetDefaultStyleToCursorStyle();
1228 /// Move to the start of the paragraph
1229 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1231 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1234 long newPos
= para
->GetRange().GetStart() - 1;
1235 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1239 SetCaretPosition(newPos
);
1241 SetDefaultStyleToCursorStyle();
1251 /// Move to the end of the line
1252 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1254 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1258 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1259 long newPos
= lineRange
.GetEnd();
1260 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1264 SetCaretPosition(newPos
);
1266 SetDefaultStyleToCursorStyle();
1276 /// Move to the start of the line
1277 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1279 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1282 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1283 long newPos
= lineRange
.GetStart()-1;
1285 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1289 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1291 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1293 SetDefaultStyleToCursorStyle();
1303 /// Move to the start of the buffer
1304 bool wxRichTextCtrl::MoveHome(int flags
)
1306 if (m_caretPosition
!= -1)
1308 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1312 SetCaretPosition(-1);
1314 SetDefaultStyleToCursorStyle();
1324 /// Move to the end of the buffer
1325 bool wxRichTextCtrl::MoveEnd(int flags
)
1327 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1329 if (m_caretPosition
!= endPos
)
1331 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1335 SetCaretPosition(endPos
);
1337 SetDefaultStyleToCursorStyle();
1347 /// Move noPages pages up
1348 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1350 return PageDown(- noPages
, flags
);
1353 /// Move noPages pages down
1354 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1356 // Calculate which line occurs noPages * screen height further down.
1357 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1360 wxSize clientSize
= GetClientSize();
1361 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1363 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1366 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1367 long pos
= lineRange
.GetStart()-1;
1368 if (pos
!= m_caretPosition
)
1370 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1372 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1376 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1378 SetDefaultStyleToCursorStyle();
1390 // Finds the caret position for the next word
1391 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1393 long endPos
= GetBuffer().GetRange().GetEnd();
1397 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1399 // First skip current text to space
1400 while (i
< endPos
&& i
> -1)
1402 // i is in character, not caret positions
1403 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1404 if (text
!= wxT(" ") && !text
.empty())
1411 while (i
< endPos
&& i
> -1)
1413 // i is in character, not caret positions
1414 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1415 if (text
.empty()) // End of paragraph, or maybe an image
1416 return wxMax(-1, i
- 1);
1417 else if (text
== wxT(" ") || text
.empty())
1421 // Convert to caret position
1422 return wxMax(-1, i
- 1);
1431 long i
= m_caretPosition
;
1433 // First skip white space
1434 while (i
< endPos
&& i
> -1)
1436 // i is in character, not caret positions
1437 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1438 if (text
.empty()) // End of paragraph, or maybe an image
1440 else if (text
== wxT(" ") || text
.empty())
1445 // Next skip current text to space
1446 while (i
< endPos
&& i
> -1)
1448 // i is in character, not caret positions
1449 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1450 if (text
!= wxT(" ") /* && !text.empty() */)
1463 /// Move n words left
1464 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1466 long pos
= FindNextWordPosition(-1);
1467 if (pos
!= m_caretPosition
)
1469 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1471 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1475 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1477 SetDefaultStyleToCursorStyle();
1487 /// Move n words right
1488 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1490 long pos
= FindNextWordPosition(1);
1491 if (pos
!= m_caretPosition
)
1493 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1495 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1499 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1501 SetDefaultStyleToCursorStyle();
1512 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1514 // Only do sizing optimization for large buffers
1515 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1517 m_fullLayoutRequired
= true;
1518 m_fullLayoutTime
= wxGetLocalTimeMillis();
1519 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1520 LayoutContent(true /* onlyVisibleRect */);
1523 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1525 #if wxRICHTEXT_BUFFERED_PAINTING
1533 /// Idle-time processing
1534 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1536 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1538 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1540 m_fullLayoutRequired
= false;
1541 m_fullLayoutTime
= 0;
1542 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1543 ShowPosition(m_fullLayoutSavedPosition
);
1547 if (m_caretPositionForDefaultStyle
!= -2)
1549 // If the caret position has changed, no longer reflect the default style
1551 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1552 m_caretPositionForDefaultStyle
= -2;
1559 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1565 /// Set up scrollbars, e.g. after a resize
1566 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1571 if (GetBuffer().IsEmpty())
1573 SetScrollbars(0, 0, 0, 0, 0, 0);
1577 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1578 // of pixels. See e.g. wxVScrolledWindow for ideas.
1579 int pixelsPerUnit
= 5;
1580 wxSize clientSize
= GetClientSize();
1582 int maxHeight
= GetBuffer().GetCachedSize().y
;
1584 // Round up so we have at least maxHeight pixels
1585 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1587 int startX
= 0, startY
= 0;
1589 GetViewStart(& startX
, & startY
);
1591 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1592 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1594 // Move to previous scroll position if
1596 SetScrollbars(0, pixelsPerUnit
,
1598 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1601 /// Paint the background
1602 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1604 wxColour backgroundColour
= GetBackgroundColour();
1605 if (!backgroundColour
.Ok())
1606 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1608 // Clear the background
1609 dc
.SetBrush(wxBrush(backgroundColour
));
1610 dc
.SetPen(*wxTRANSPARENT_PEN
);
1611 wxRect
windowRect(GetClientSize());
1612 windowRect
.x
-= 2; windowRect
.y
-= 2;
1613 windowRect
.width
+= 4; windowRect
.height
+= 4;
1615 // We need to shift the rectangle to take into account
1616 // scrolling. Converting device to logical coordinates.
1617 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1618 dc
.DrawRectangle(windowRect
);
1621 #if wxRICHTEXT_BUFFERED_PAINTING
1622 /// Recreate buffer bitmap if necessary
1623 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1626 if (sz
== wxDefaultSize
)
1627 sz
= GetClientSize();
1629 if (sz
.x
< 1 || sz
.y
< 1)
1632 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1633 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1634 return m_bufferBitmap
.Ok();
1638 // ----------------------------------------------------------------------------
1639 // file IO functions
1640 // ----------------------------------------------------------------------------
1642 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1644 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1646 m_filename
= filename
;
1649 SetInsertionPoint(0);
1652 SetupScrollbars(true);
1654 SendTextUpdatedEvent();
1660 wxLogError(_("File couldn't be loaded."));
1666 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1668 if (GetBuffer().SaveFile(filename
, fileType
))
1670 m_filename
= filename
;
1677 wxLogError(_("The text couldn't be saved."));
1682 // ----------------------------------------------------------------------------
1683 // wxRichTextCtrl specific functionality
1684 // ----------------------------------------------------------------------------
1686 /// Add a new paragraph of text to the end of the buffer
1687 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1689 return GetBuffer().AddParagraph(text
);
1693 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1695 return GetBuffer().AddImage(image
);
1698 // ----------------------------------------------------------------------------
1699 // selection and ranges
1700 // ----------------------------------------------------------------------------
1702 void wxRichTextCtrl::SelectAll()
1704 SetSelection(0, GetLastPosition()+1);
1705 m_selectionAnchor
= -1;
1709 void wxRichTextCtrl::SelectNone()
1711 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1712 SetSelection(-2, -2);
1713 m_selectionAnchor
= -2;
1716 static bool wxIsWordDelimiter(const wxString
& text
)
1718 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1721 /// Select the word at the given character position
1722 bool wxRichTextCtrl::SelectWord(long position
)
1724 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1727 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1731 long positionStart
= position
;
1732 long positionEnd
= position
;
1734 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1736 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1737 if (wxIsWordDelimiter(text
))
1743 if (positionStart
< para
->GetRange().GetStart())
1744 positionStart
= para
->GetRange().GetStart();
1746 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1748 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1749 if (wxIsWordDelimiter(text
))
1755 if (positionEnd
>= para
->GetRange().GetEnd())
1756 positionEnd
= para
->GetRange().GetEnd();
1758 SetSelection(positionStart
, positionEnd
+1);
1760 if (positionStart
>= 0)
1762 MoveCaret(positionStart
-1, true);
1763 SetDefaultStyleToCursorStyle();
1769 wxString
wxRichTextCtrl::GetStringSelection() const
1772 GetSelection(&from
, &to
);
1774 return GetRange(from
, to
);
1777 // ----------------------------------------------------------------------------
1779 // ----------------------------------------------------------------------------
1781 wxTextCtrlHitTestResult
1782 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1784 // implement in terms of the other overload as the native ports typically
1785 // can get the position and not (x, y) pair directly (although wxUniv
1786 // directly gets x and y -- and so overrides this method as well)
1788 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1790 if ( rc
!= wxTE_HT_UNKNOWN
)
1792 PositionToXY(pos
, x
, y
);
1798 wxTextCtrlHitTestResult
1799 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1802 wxClientDC
dc((wxRichTextCtrl
*) this);
1803 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1805 // Buffer uses logical position (relative to start of buffer)
1807 wxPoint pt2
= GetLogicalPoint(pt
);
1809 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1813 case wxRICHTEXT_HITTEST_BEFORE
:
1814 return wxTE_HT_BEFORE
;
1816 case wxRICHTEXT_HITTEST_AFTER
:
1817 return wxTE_HT_BEYOND
;
1819 case wxRICHTEXT_HITTEST_ON
:
1820 return wxTE_HT_ON_TEXT
;
1823 return wxTE_HT_UNKNOWN
;
1826 // ----------------------------------------------------------------------------
1827 // set/get the controls text
1828 // ----------------------------------------------------------------------------
1830 wxString
wxRichTextCtrl::GetValue() const
1832 return GetBuffer().GetText();
1835 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1837 // Public API for range is different from internals
1838 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1841 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1845 // if the text is long enough, it's faster to just set it instead of first
1846 // comparing it with the old one (chances are that it will be different
1847 // anyhow, this comparison is there to avoid flicker for small single-line
1848 // edit controls mostly)
1849 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1853 // for compatibility, don't move the cursor when doing SetValue()
1854 SetInsertionPoint(0);
1858 if ( flags
& SetValue_SendEvent
)
1860 // still send an event for consistency
1861 SendTextUpdatedEvent();
1865 // we should reset the modified flag even if the value didn't really change
1867 // mark the control as being not dirty - we changed its text, not the
1872 void wxRichTextCtrl::WriteText(const wxString
& value
)
1877 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
1879 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1881 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
1883 if ( flags
& SetValue_SendEvent
)
1884 SendTextUpdatedEvent();
1887 void wxRichTextCtrl::AppendText(const wxString
& text
)
1889 SetInsertionPointEnd();
1894 /// Write an image at the current insertion point
1895 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1897 wxRichTextImageBlock imageBlock
;
1899 wxImage image2
= image
;
1900 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1901 return WriteImage(imageBlock
);
1906 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1908 wxRichTextImageBlock imageBlock
;
1911 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1912 return WriteImage(imageBlock
);
1917 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1919 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1922 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1926 wxRichTextImageBlock imageBlock
;
1928 wxImage image
= bitmap
.ConvertToImage();
1929 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1930 return WriteImage(imageBlock
);
1936 /// Insert a newline (actually paragraph) at the current insertion point.
1937 bool wxRichTextCtrl::Newline()
1939 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1943 // ----------------------------------------------------------------------------
1944 // Clipboard operations
1945 // ----------------------------------------------------------------------------
1947 void wxRichTextCtrl::Copy()
1951 wxRichTextRange range
= GetInternalSelectionRange();
1952 GetBuffer().CopyToClipboard(range
);
1956 void wxRichTextCtrl::Cut()
1960 wxRichTextRange range
= GetInternalSelectionRange();
1961 GetBuffer().CopyToClipboard(range
);
1963 DeleteSelectedContent();
1969 void wxRichTextCtrl::Paste()
1973 BeginBatchUndo(_("Paste"));
1975 long newPos
= m_caretPosition
;
1976 DeleteSelectedContent(& newPos
);
1978 GetBuffer().PasteFromClipboard(newPos
);
1984 void wxRichTextCtrl::DeleteSelection()
1986 if (CanDeleteSelection())
1988 DeleteSelectedContent();
1992 bool wxRichTextCtrl::HasSelection() const
1994 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
1997 bool wxRichTextCtrl::CanCopy() const
1999 // Can copy if there's a selection
2000 return HasSelection();
2003 bool wxRichTextCtrl::CanCut() const
2005 return HasSelection() && IsEditable();
2008 bool wxRichTextCtrl::CanPaste() const
2010 if ( !IsEditable() )
2013 return GetBuffer().CanPasteFromClipboard();
2016 bool wxRichTextCtrl::CanDeleteSelection() const
2018 return HasSelection() && IsEditable();
2022 // ----------------------------------------------------------------------------
2024 // ----------------------------------------------------------------------------
2026 void wxRichTextCtrl::SetEditable(bool editable
)
2028 m_editable
= editable
;
2031 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2035 m_caretPosition
= pos
- 1;
2038 void wxRichTextCtrl::SetInsertionPointEnd()
2040 long pos
= GetLastPosition();
2041 SetInsertionPoint(pos
);
2044 long wxRichTextCtrl::GetInsertionPoint() const
2046 return m_caretPosition
+1;
2049 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2051 return GetBuffer().GetRange().GetEnd();
2054 // If the return values from and to are the same, there is no
2056 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2058 *from
= m_selectionRange
.GetStart();
2059 *to
= m_selectionRange
.GetEnd();
2060 if ((*to
) != -1 && (*to
) != -2)
2064 bool wxRichTextCtrl::IsEditable() const
2069 // ----------------------------------------------------------------------------
2071 // ----------------------------------------------------------------------------
2073 void wxRichTextCtrl::SetSelection(long from
, long to
)
2075 // if from and to are both -1, it means (in wxWidgets) that all text should
2077 if ( (from
== -1) && (to
== -1) )
2080 to
= GetLastPosition()+1;
2083 DoSetSelection(from
, to
);
2086 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2088 m_selectionAnchor
= from
;
2089 m_selectionRange
.SetRange(from
, to
-1);
2095 // ----------------------------------------------------------------------------
2097 // ----------------------------------------------------------------------------
2099 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2100 const wxString
& value
)
2102 BeginBatchUndo(_("Replace"));
2104 DeleteSelectedContent();
2106 DoWriteText(value
, SetValue_SelectionOnly
);
2111 void wxRichTextCtrl::Remove(long from
, long to
)
2115 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2116 m_caretPosition
, // Current caret position
2117 from
, // New caret position
2125 bool wxRichTextCtrl::IsModified() const
2127 return m_buffer
.IsModified();
2130 void wxRichTextCtrl::MarkDirty()
2132 m_buffer
.Modify(true);
2135 void wxRichTextCtrl::DiscardEdits()
2137 m_caretPositionForDefaultStyle
= -2;
2138 m_buffer
.Modify(false);
2139 m_buffer
.GetCommandProcessor()->ClearCommands();
2142 int wxRichTextCtrl::GetNumberOfLines() const
2144 return GetBuffer().GetParagraphCount();
2147 // ----------------------------------------------------------------------------
2148 // Positions <-> coords
2149 // ----------------------------------------------------------------------------
2151 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2153 return GetBuffer().XYToPosition(x
, y
);
2156 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2158 return GetBuffer().PositionToXY(pos
, x
, y
);
2161 // ----------------------------------------------------------------------------
2163 // ----------------------------------------------------------------------------
2165 void wxRichTextCtrl::ShowPosition(long pos
)
2167 if (!IsPositionVisible(pos
))
2168 ScrollIntoView(pos
-1, WXK_DOWN
);
2171 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2173 return GetBuffer().GetParagraphLength(lineNo
);
2176 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2178 return GetBuffer().GetParagraphText(lineNo
);
2181 // ----------------------------------------------------------------------------
2183 // ----------------------------------------------------------------------------
2185 void wxRichTextCtrl::Undo()
2189 GetCommandProcessor()->Undo();
2193 void wxRichTextCtrl::Redo()
2197 GetCommandProcessor()->Redo();
2201 bool wxRichTextCtrl::CanUndo() const
2203 return GetCommandProcessor()->CanUndo();
2206 bool wxRichTextCtrl::CanRedo() const
2208 return GetCommandProcessor()->CanRedo();
2211 // ----------------------------------------------------------------------------
2212 // implementation details
2213 // ----------------------------------------------------------------------------
2215 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2217 SetValue(event
.GetString());
2218 GetEventHandler()->ProcessEvent(event
);
2221 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2223 // By default, load the first file into the text window.
2224 if (event
.GetNumberOfFiles() > 0)
2226 LoadFile(event
.GetFiles()[0]);
2230 wxSize
wxRichTextCtrl::DoGetBestSize() const
2232 return wxSize(10, 10);
2235 // ----------------------------------------------------------------------------
2236 // standard handlers for standard edit menu events
2237 // ----------------------------------------------------------------------------
2239 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2244 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2249 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2254 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2259 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2264 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2269 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2271 event
.Enable( CanCut() );
2274 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2276 event
.Enable( CanCopy() );
2279 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2281 event
.Enable( CanDeleteSelection() );
2284 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2286 event
.Enable( CanPaste() );
2289 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2291 event
.Enable( CanUndo() );
2292 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2295 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2297 event
.Enable( CanRedo() );
2298 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2301 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2306 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2308 event
.Enable(GetLastPosition() > 0);
2311 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2315 m_contextMenu
= new wxMenu
;
2316 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2317 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2318 m_contextMenu
->AppendSeparator();
2319 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2320 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2321 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2322 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2323 m_contextMenu
->AppendSeparator();
2324 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2326 PopupMenu(m_contextMenu
);
2330 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2332 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2335 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2337 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2340 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2342 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2345 // extended style setting operation with flags including:
2346 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2347 // see richtextbuffer.h for more details.
2348 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2350 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2353 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2355 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2358 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2360 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2363 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2365 return GetBuffer().SetDefaultStyle(style
);
2368 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2370 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2373 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2375 return GetBuffer().GetDefaultStyle();
2378 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2380 return GetBuffer().GetDefaultStyle();
2383 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2385 wxTextAttrEx
attr(style
);
2386 if (GetBuffer().GetStyle(position
, attr
))
2395 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2397 return GetBuffer().GetStyle(position
, style
);
2400 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2402 return GetBuffer().GetStyle(position
, style
);
2405 /// Get the content (uncombined) attributes for this position.
2407 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2409 wxTextAttrEx
attr(style
);
2410 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2419 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2421 return GetBuffer().GetUncombinedStyle(position
, style
);
2424 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2426 return GetBuffer().GetUncombinedStyle(position
, style
);
2429 /// Set font, and also the buffer attributes
2430 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2432 wxControl::SetFont(font
);
2434 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2436 GetBuffer().SetBasicStyle(attr
);
2438 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2444 /// Transform logical to physical
2445 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2448 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2453 /// Transform physical to logical
2454 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2457 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2462 /// Position the caret
2463 void wxRichTextCtrl::PositionCaret()
2468 //wxLogDebug(wxT("PositionCaret"));
2471 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2473 wxPoint originalPt
= caretRect
.GetPosition();
2474 wxPoint pt
= GetPhysicalPoint(originalPt
);
2475 if (GetCaret()->GetPosition() != pt
)
2477 GetCaret()->Move(pt
);
2478 GetCaret()->SetSize(caretRect
.GetSize());
2483 /// Get the caret height and position for the given character position
2484 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2486 wxClientDC
dc(this);
2487 dc
.SetFont(GetFont());
2494 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2496 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2503 /// Gets the line for the visible caret position. If the caret is
2504 /// shown at the very end of the line, it means the next character is actually
2505 /// on the following line. So let's get the line we're expecting to find
2506 /// if this is the case.
2507 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2509 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2510 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2513 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2514 if (caretPosition
== lineRange
.GetStart()-1 &&
2515 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2517 if (!m_caretAtLineStart
)
2518 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2525 /// Move the caret to the given character position
2526 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2528 if (GetBuffer().GetDirty())
2531 if (pos
<= GetBuffer().GetRange().GetEnd())
2533 SetCaretPosition(pos
, showAtLineStart
);
2543 /// Layout the buffer: which we must do before certain operations, such as
2544 /// setting the caret position.
2545 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2547 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2549 wxRect
availableSpace(GetClientSize());
2550 if (availableSpace
.width
== 0)
2551 availableSpace
.width
= 10;
2552 if (availableSpace
.height
== 0)
2553 availableSpace
.height
= 10;
2555 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2556 if (onlyVisibleRect
)
2558 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2559 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2562 wxClientDC
dc(this);
2563 dc
.SetFont(GetFont());
2567 GetBuffer().Defragment();
2568 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2569 GetBuffer().Layout(dc
, availableSpace
, flags
);
2570 GetBuffer().SetDirty(false);
2579 /// Is all of the selection bold?
2580 bool wxRichTextCtrl::IsSelectionBold()
2584 wxRichTextAttr attr
;
2585 wxRichTextRange range
= GetInternalSelectionRange();
2586 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2587 attr
.SetFontWeight(wxBOLD
);
2589 return HasCharacterAttributes(range
, attr
);
2593 // If no selection, then we need to combine current style with default style
2594 // to see what the effect would be if we started typing.
2595 wxRichTextAttr attr
;
2596 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2598 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2599 if (GetStyle(pos
, attr
))
2601 if (IsDefaultStyleShowing())
2602 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2603 return attr
.GetFontWeight() == wxBOLD
;
2609 /// Is all of the selection italics?
2610 bool wxRichTextCtrl::IsSelectionItalics()
2614 wxRichTextRange range
= GetInternalSelectionRange();
2615 wxRichTextAttr attr
;
2616 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2617 attr
.SetFontStyle(wxITALIC
);
2619 return HasCharacterAttributes(range
, attr
);
2623 // If no selection, then we need to combine current style with default style
2624 // to see what the effect would be if we started typing.
2625 wxRichTextAttr attr
;
2626 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2628 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2629 if (GetStyle(pos
, attr
))
2631 if (IsDefaultStyleShowing())
2632 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2633 return attr
.GetFontStyle() == wxITALIC
;
2639 /// Is all of the selection underlined?
2640 bool wxRichTextCtrl::IsSelectionUnderlined()
2644 wxRichTextRange range
= GetInternalSelectionRange();
2645 wxRichTextAttr attr
;
2646 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2647 attr
.SetFontUnderlined(true);
2649 return HasCharacterAttributes(range
, attr
);
2653 // If no selection, then we need to combine current style with default style
2654 // to see what the effect would be if we started typing.
2655 wxRichTextAttr attr
;
2656 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2657 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2659 if (GetStyle(pos
, attr
))
2661 if (IsDefaultStyleShowing())
2662 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2663 return attr
.GetFontUnderlined();
2669 /// Apply bold to the selection
2670 bool wxRichTextCtrl::ApplyBoldToSelection()
2672 wxRichTextAttr attr
;
2673 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2674 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2677 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2679 SetAndShowDefaultStyle(attr
);
2683 /// Apply italic to the selection
2684 bool wxRichTextCtrl::ApplyItalicToSelection()
2686 wxRichTextAttr attr
;
2687 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2688 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2691 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2693 SetAndShowDefaultStyle(attr
);
2697 /// Apply underline to the selection
2698 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2700 wxRichTextAttr attr
;
2701 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2702 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2705 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2707 SetAndShowDefaultStyle(attr
);
2711 /// Is all of the selection aligned according to the specified flag?
2712 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2714 wxRichTextRange range
;
2716 range
= GetInternalSelectionRange();
2718 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2720 wxRichTextAttr attr
;
2721 attr
.SetAlignment(alignment
);
2723 return HasParagraphAttributes(range
, attr
);
2726 /// Apply alignment to the selection
2727 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2729 wxRichTextAttr attr
;
2730 attr
.SetAlignment(alignment
);
2732 return SetStyle(GetSelectionRange(), attr
);
2735 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2737 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2742 /// Apply a named style to the selection
2743 void wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2745 // Flags are defined within each definition, so only certain
2746 // attributes are applied.
2747 wxRichTextAttr
attr(def
->GetStyle());
2749 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2751 // Make sure the attr has the style name
2752 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2754 attr
.SetParagraphStyleName(def
->GetName());
2756 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2757 // attributes, and not the leaf nodes. This will allow the context (e.g. text)
2758 // to change its style independently.
2759 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2762 attr
.SetCharacterStyleName(def
->GetName());
2765 SetStyleEx(GetSelectionRange(), attr
, flags
);
2767 SetAndShowDefaultStyle(attr
);
2770 /// Apply the style sheet to the buffer, for example if the styles have changed.
2771 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2774 styleSheet
= GetBuffer().GetStyleSheet();
2778 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2780 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2788 /// Sets the default style to the style under the cursor
2789 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2792 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2794 // If at the start of a paragraph, use the next position.
2795 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2797 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2798 if (GetUncombinedStyle(pos
, attr
))
2800 if (GetStyle(pos
, attr
))
2803 SetDefaultStyle(attr
);
2810 /// Returns the first visible position in the current view
2811 long wxRichTextCtrl::GetFirstVisiblePosition() const
2813 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2815 return line
->GetAbsoluteRange().GetStart();
2820 /// The adjusted caret position is the character position adjusted to take
2821 /// into account whether we're at the start of a paragraph, in which case
2822 /// style information should be taken from the next position, not current one.
2823 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2825 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2827 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2832 /// Get/set the selection range in character positions. -1, -1 means no selection.
2833 /// The range is in API convention, i.e. a single character selection is denoted
2835 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2837 wxRichTextRange range
= GetInternalSelectionRange();
2838 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2839 range
.SetEnd(range
.GetEnd() + 1);
2843 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2845 wxRichTextRange
range1(range
);
2846 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2847 range1
.SetEnd(range1
.GetEnd() - 1);
2849 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2851 SetInternalSelectionRange(range1
);