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 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
48 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
50 IMPLEMENT_CLASS( wxRichTextCtrl
, wxScrolledWindow
)
53 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
55 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
56 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
58 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxScrolledWindow
)
60 EVT_PAINT(wxRichTextCtrl::OnPaint
)
61 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
62 EVT_IDLE(wxRichTextCtrl::OnIdle
)
63 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
64 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
65 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
66 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
67 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
68 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
69 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
70 EVT_CHAR(wxRichTextCtrl::OnChar
)
71 EVT_SIZE(wxRichTextCtrl::OnSize
)
72 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
73 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
74 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
76 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
77 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
79 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
80 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
82 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
83 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
85 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
86 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
88 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
89 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
91 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
92 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
94 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
95 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
102 wxRichTextCtrl::wxRichTextCtrl()
103 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
104 : wxScrollHelper(this)
110 wxRichTextCtrl::wxRichTextCtrl( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
111 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
112 : wxScrollHelper(this)
116 Create(parent
, id
, value
, pos
, size
, style
);
120 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
)
122 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
123 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
127 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
, style
|wxFULL_REPAINT_ON_RESIZE
134 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
137 GetBuffer().SetRichTextCtrl(this);
139 wxTextAttrEx attributes
;
140 attributes
.SetFont(GetFont());
141 attributes
.SetTextColour(*wxBLACK
);
142 attributes
.SetBackgroundColour(*wxWHITE
);
143 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
144 attributes
.SetLineSpacing(10);
145 attributes
.SetParagraphSpacingAfter(10);
146 attributes
.SetParagraphSpacingBefore(0);
147 attributes
.SetFlags(wxTEXT_ATTR_ALL
);
148 SetBasicStyle(attributes
);
150 // The default attributes will be merged with base attributes, so
151 // can be empty to begin with
152 wxTextAttrEx defaultAttributes
;
153 SetDefaultStyle(defaultAttributes
);
155 SetBackgroundColour(*wxWHITE
);
156 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
158 // Tell the sizers to use the given or best size
159 SetBestFittingSize(size
);
161 #if wxRICHTEXT_BUFFERED_PAINTING
163 RecreateBuffer(size
);
166 SetCursor(wxCursor(wxCURSOR_IBEAM
));
168 if (!value
.IsEmpty())
174 wxRichTextCtrl::~wxRichTextCtrl()
176 delete m_contextMenu
;
179 /// Member initialisation
180 void wxRichTextCtrl::Init()
183 m_contextMenu
= NULL
;
185 m_caretPosition
= -1;
186 m_selectionRange
.SetRange(-2, -2);
187 m_selectionAnchor
= -2;
189 m_caretAtLineStart
= false;
191 m_fullLayoutRequired
= false;
192 m_fullLayoutTime
= 0;
193 m_fullLayoutSavedPosition
= 0;
194 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
195 m_caretPositionForDefaultStyle
= -2;
198 /// Call Freeze to prevent refresh
199 void wxRichTextCtrl::Freeze()
204 /// Call Thaw to refresh
205 void wxRichTextCtrl::Thaw()
209 if (m_freezeCount
== 0)
217 void wxRichTextCtrl::Clear()
220 m_buffer
.SetDirty(true);
221 m_caretPosition
= -1;
222 m_caretPositionForDefaultStyle
= -2;
223 m_caretAtLineStart
= false;
224 m_selectionRange
.SetRange(-2, -2);
226 SetScrollbars(0, 0, 0, 0, 0, 0);
228 if (m_freezeCount
== 0)
237 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
243 #if wxRICHTEXT_BUFFERED_PAINTING
244 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
250 if (m_freezeCount
> 0)
253 dc
.SetFont(GetFont());
255 // Paint the background
258 wxRegion dirtyRegion
= GetUpdateRegion();
260 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
261 wxRect
availableSpace(GetClientSize());
262 if (GetBuffer().GetDirty())
264 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
265 GetBuffer().SetDirty(false);
269 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
278 // Empty implementation, to prevent flicker
279 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
283 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
285 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
294 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
303 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
309 dc
.SetFont(GetFont());
312 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
314 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
316 m_dragStart
= event
.GetLogicalPosition(dc
);
322 bool caretAtLineStart
= false;
324 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
326 // If we're at the start of a line (but not first in para)
327 // then we should keep the caret showing at the start of the line
328 // by showing the m_caretAtLineStart flag.
329 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
330 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
332 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
333 caretAtLineStart
= true;
337 MoveCaret(position
, caretAtLineStart
);
338 SetDefaultStyleToCursorStyle();
345 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
350 if (GetCapture() == this)
356 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
358 if (!event
.Dragging())
366 dc
.SetFont(GetFont());
369 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
370 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
372 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
374 // TODO: test closeness
376 bool caretAtLineStart
= false;
378 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
380 // If we're at the start of a line (but not first in para)
381 // then we should keep the caret showing at the start of the line
382 // by showing the m_caretAtLineStart flag.
383 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
384 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
386 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
387 caretAtLineStart
= true;
391 if (m_caretPosition
!= position
)
393 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
395 MoveCaret(position
, caretAtLineStart
);
396 SetDefaultStyleToCursorStyle();
405 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
411 /// Left-double-click
412 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
414 SelectWord(GetCaretPosition()+1);
419 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
425 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
429 flags
|= wxRICHTEXT_CTRL_DOWN
;
430 if (event
.ShiftDown())
431 flags
|= wxRICHTEXT_SHIFT_DOWN
;
433 flags
|= wxRICHTEXT_ALT_DOWN
;
435 if (event
.GetKeyCode() == WXK_LEFT
||
436 event
.GetKeyCode() == WXK_RIGHT
||
437 event
.GetKeyCode() == WXK_UP
||
438 event
.GetKeyCode() == WXK_DOWN
||
439 event
.GetKeyCode() == WXK_HOME
||
440 event
.GetKeyCode() == WXK_PAGEUP
||
441 event
.GetKeyCode() == WXK_PAGEDOWN
||
442 event
.GetKeyCode() == WXK_END
||
444 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
445 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
446 event
.GetKeyCode() == WXK_NUMPAD_UP
||
447 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
448 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
449 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
450 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
451 event
.GetKeyCode() == WXK_NUMPAD_END
)
453 KeyboardNavigate(event
.GetKeyCode(), flags
);
457 // all the other keys modify the controls contents which shouldn't be
458 // possible if we're read-only
465 if (event
.GetKeyCode() == WXK_RETURN
)
467 BeginBatchUndo(_("Insert Text"));
469 long newPos
= m_caretPosition
;
471 DeleteSelectedContent(& newPos
);
473 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
475 wxRichTextEvent
cmdEvent(
476 wxEVT_COMMAND_RICHTEXT_RETURN
,
478 cmdEvent
.SetEventObject(this);
479 cmdEvent
.SetFlags(flags
);
480 GetEventHandler()->ProcessEvent(cmdEvent
);
483 SetDefaultStyleToCursorStyle();
485 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
487 else if (event
.GetKeyCode() == WXK_BACK
)
489 BeginBatchUndo(_("Delete Text"));
491 // Submit range in character positions, which are greater than caret positions,
492 // so subtract 1 for deleted character and add 1 for conversion to character position.
493 if (m_caretPosition
> -1 && !HasSelection())
495 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
496 m_caretPosition
, // Current caret position
497 m_caretPosition
-1, // New caret position
501 DeleteSelectedContent();
505 // Shouldn't this be in Do()?
506 if (GetLastPosition() == -1)
510 m_caretPosition
= -1;
512 SetDefaultStyleToCursorStyle();
515 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
517 else if (event
.GetKeyCode() == WXK_DELETE
)
519 BeginBatchUndo(_("Delete Text"));
521 // Submit range in character positions, which are greater than caret positions,
522 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
524 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
525 m_caretPosition
, // Current caret position
526 m_caretPosition
+1, // New caret position
530 DeleteSelectedContent();
534 // Shouldn't this be in Do()?
535 if (GetLastPosition() == -1)
539 m_caretPosition
= -1;
541 SetDefaultStyleToCursorStyle();
546 long keycode
= event
.GetKeyCode();
620 case WXK_NUMPAD_SPACE
:
622 case WXK_NUMPAD_ENTER
:
627 case WXK_NUMPAD_HOME
:
628 case WXK_NUMPAD_LEFT
:
630 case WXK_NUMPAD_RIGHT
:
631 case WXK_NUMPAD_DOWN
:
632 case WXK_NUMPAD_PAGEUP
:
633 case WXK_NUMPAD_PAGEDOWN
:
635 case WXK_NUMPAD_BEGIN
:
636 case WXK_NUMPAD_INSERT
:
637 case WXK_NUMPAD_DELETE
:
638 case WXK_NUMPAD_EQUAL
:
639 case WXK_NUMPAD_MULTIPLY
:
641 case WXK_NUMPAD_SEPARATOR
:
642 case WXK_NUMPAD_SUBTRACT
:
643 case WXK_NUMPAD_DECIMAL
:
651 if (event
.CmdDown() || event
.AltDown())
657 BeginBatchUndo(_("Insert Text"));
659 long newPos
= m_caretPosition
;
660 DeleteSelectedContent(& newPos
);
662 wxString str
= (wxChar
) event
.GetKeyCode();
663 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
667 SetDefaultStyleToCursorStyle();
668 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
674 /// Delete content if there is a selection, e.g. when pressing a key.
675 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
679 long pos
= m_selectionRange
.GetStart();
680 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
681 m_caretPosition
, // Current caret position
682 pos
, // New caret position
684 m_selectionRange
.SetRange(-2, -2);
694 /// Keyboard navigation
698 Left: left one character
699 Right: right one character
702 Ctrl-Left: left one word
703 Ctrl-Right: right one word
704 Ctrl-Up: previous paragraph start
705 Ctrl-Down: next start of paragraph
708 Ctrl-Home: start of document
709 Ctrl-End: end of document
711 Page-Down: Down a screen
715 Ctrl-Alt-PgUp: Start of window
716 Ctrl-Alt-PgDn: End of window
717 F8: Start selection mode
718 Esc: End selection mode
720 Adding Shift does the above but starts/extends selection.
725 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
727 bool success
= false;
729 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
731 if (flags
& wxRICHTEXT_CTRL_DOWN
)
732 success
= WordRight(1, flags
);
734 success
= MoveRight(1, flags
);
736 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
738 if (flags
& wxRICHTEXT_CTRL_DOWN
)
739 success
= WordLeft(1, flags
);
741 success
= MoveLeft(1, flags
);
743 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
745 if (flags
& wxRICHTEXT_CTRL_DOWN
)
746 success
= MoveToParagraphStart(flags
);
748 success
= MoveUp(1, flags
);
750 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
752 if (flags
& wxRICHTEXT_CTRL_DOWN
)
753 success
= MoveToParagraphEnd(flags
);
755 success
= MoveDown(1, flags
);
757 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
759 success
= PageUp(1, flags
);
761 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
763 success
= PageDown(1, flags
);
765 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
767 if (flags
& wxRICHTEXT_CTRL_DOWN
)
768 success
= MoveHome(flags
);
770 success
= MoveToLineStart(flags
);
772 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
774 if (flags
& wxRICHTEXT_CTRL_DOWN
)
775 success
= MoveEnd(flags
);
777 success
= MoveToLineEnd(flags
);
782 ScrollIntoView(m_caretPosition
, keyCode
);
783 SetDefaultStyleToCursorStyle();
789 /// Extend the selection. Selections are in caret positions.
790 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
792 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
794 // If not currently selecting, start selecting
795 if (m_selectionRange
.GetStart() == -2)
797 m_selectionAnchor
= oldPos
;
800 m_selectionRange
.SetRange(newPos
+1, oldPos
);
802 m_selectionRange
.SetRange(oldPos
+1, newPos
);
806 // Always ensure that the selection range start is greater than
808 if (newPos
> m_selectionAnchor
)
809 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
811 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
814 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
816 wxLogDebug(wxT("Strange selection range"));
825 /// Scroll into view, returning true if we scrolled.
826 /// This takes a _caret_ position.
827 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
829 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
835 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
837 int startXUnits
, startYUnits
;
838 GetViewStart(& startXUnits
, & startYUnits
);
839 int startY
= startYUnits
* ppuY
;
842 GetVirtualSize(& sx
, & sy
);
848 wxRect rect
= line
->GetRect();
850 bool scrolled
= false;
852 wxSize clientSize
= GetClientSize();
855 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
856 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
857 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
858 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
860 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
862 // Make it scroll so this item is at the bottom
864 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
865 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
867 // If we're still off the screen, scroll another line down
868 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
871 if (startYUnits
!= yUnits
)
873 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
877 else if (rect
.y
< startY
)
879 // Make it scroll so this item is at the top
882 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
884 if (startYUnits
!= yUnits
)
886 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
892 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
893 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
894 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
895 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
899 // Make it scroll so this item is at the top
902 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
904 if (startYUnits
!= yUnits
)
906 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
910 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
912 // Make it scroll so this item is at the bottom
914 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
915 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
917 // If we're still off the screen, scroll another line down
918 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
921 if (startYUnits
!= yUnits
)
923 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
933 /// Is the given position visible on the screen?
934 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
936 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
942 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
945 GetViewStart(& startX
, & startY
);
947 startY
= startY
* ppuY
;
950 GetVirtualSize(& sx
, & sy
);
955 wxRect rect
= line
->GetRect();
957 wxSize clientSize
= GetClientSize();
959 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
962 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
964 m_caretPosition
= position
;
965 m_caretAtLineStart
= showAtLineStart
;
968 /// Move caret one visual step forward: this may mean setting a flag
969 /// and keeping the same position if we're going from the end of one line
970 /// to the start of the next, which may be the exact same caret position.
971 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
973 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
975 // Only do the check if we're not at the end of the paragraph (where things work OK
977 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
979 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
983 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
985 // We're at the end of a line. See whether we need to
986 // stay at the same actual caret position but change visual
988 if (oldPosition
== lineRange
.GetEnd())
990 if (m_caretAtLineStart
)
992 // We're already at the start of the line, so actually move on now.
993 m_caretPosition
= oldPosition
+ 1;
994 m_caretAtLineStart
= false;
998 // We're showing at the end of the line, so keep to
999 // the same position but indicate that we're to show
1000 // at the start of the next line.
1001 m_caretPosition
= oldPosition
;
1002 m_caretAtLineStart
= true;
1004 SetDefaultStyleToCursorStyle();
1010 SetDefaultStyleToCursorStyle();
1013 /// Move caret one visual step backward: this may mean setting a flag
1014 /// and keeping the same position if we're going from the end of one line
1015 /// to the start of the next, which may be the exact same caret position.
1016 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1018 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1020 // Only do the check if we're not at the start of the paragraph (where things work OK
1022 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1024 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1028 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1030 // We're at the start of a line. See whether we need to
1031 // stay at the same actual caret position but change visual
1032 // position, or not.
1033 if (oldPosition
== lineRange
.GetStart())
1035 m_caretPosition
= oldPosition
-1;
1036 m_caretAtLineStart
= true;
1039 else if (oldPosition
== lineRange
.GetEnd())
1041 if (m_caretAtLineStart
)
1043 // We're at the start of the line, so keep the same caret position
1044 // but clear the start-of-line flag.
1045 m_caretPosition
= oldPosition
;
1046 m_caretAtLineStart
= false;
1050 // We're showing at the end of the line, so go back
1051 // to the previous character position.
1052 m_caretPosition
= oldPosition
- 1;
1054 SetDefaultStyleToCursorStyle();
1060 SetDefaultStyleToCursorStyle();
1064 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1066 long endPos
= GetBuffer().GetRange().GetEnd();
1068 if (m_caretPosition
+ noPositions
< endPos
)
1070 long oldPos
= m_caretPosition
;
1071 long newPos
= m_caretPosition
+ noPositions
;
1073 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1077 // Determine by looking at oldPos and m_caretPosition whether
1078 // we moved from the end of a line to the start of the next line, in which case
1079 // we want to adjust the caret position such that it is positioned at the
1080 // start of the next line, rather than jumping past the first character of the
1082 if (noPositions
== 1 && !extendSel
)
1083 MoveCaretForward(oldPos
);
1085 SetCaretPosition(newPos
);
1088 SetDefaultStyleToCursorStyle();
1099 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1103 if (m_caretPosition
> startPos
- noPositions
+ 1)
1105 long oldPos
= m_caretPosition
;
1106 long newPos
= m_caretPosition
- noPositions
;
1107 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1111 if (noPositions
== 1 && !extendSel
)
1112 MoveCaretBack(oldPos
);
1114 SetCaretPosition(newPos
);
1117 SetDefaultStyleToCursorStyle();
1128 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1130 return MoveDown(- noLines
, flags
);
1134 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1139 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1140 wxPoint pt
= GetCaret()->GetPosition();
1141 long newLine
= lineNumber
+ noLines
;
1143 if (lineNumber
!= -1)
1147 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1149 if (newLine
> lastLine
)
1159 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1162 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1168 wxClientDC
dc(this);
1170 dc
.SetFont(GetFont());
1172 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1174 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1176 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1177 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1178 // so we view the caret at the start of the line.
1179 bool caretLineStart
= false;
1180 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1182 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1183 wxRichTextRange lineRange
;
1185 lineRange
= thisLine
->GetAbsoluteRange();
1187 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1190 caretLineStart
= true;
1194 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1195 if (para
&& para
->GetRange().GetStart() == newPos
)
1200 long newSelEnd
= newPos
;
1202 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1206 SetCaretPosition(newPos
, caretLineStart
);
1208 SetDefaultStyleToCursorStyle();
1218 /// Move to the end of the paragraph
1219 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1221 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1224 long newPos
= para
->GetRange().GetEnd() - 1;
1225 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1229 SetCaretPosition(newPos
);
1231 SetDefaultStyleToCursorStyle();
1241 /// Move to the start of the paragraph
1242 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1244 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1247 long newPos
= para
->GetRange().GetStart() - 1;
1248 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1252 SetCaretPosition(newPos
);
1254 SetDefaultStyleToCursorStyle();
1264 /// Move to the end of the line
1265 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1267 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1271 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1272 long newPos
= lineRange
.GetEnd();
1273 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1277 SetCaretPosition(newPos
);
1279 SetDefaultStyleToCursorStyle();
1289 /// Move to the start of the line
1290 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1292 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1295 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1296 long newPos
= lineRange
.GetStart()-1;
1298 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1302 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1304 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1306 SetDefaultStyleToCursorStyle();
1316 /// Move to the start of the buffer
1317 bool wxRichTextCtrl::MoveHome(int flags
)
1319 if (m_caretPosition
!= -1)
1321 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1325 SetCaretPosition(-1);
1327 SetDefaultStyleToCursorStyle();
1337 /// Move to the end of the buffer
1338 bool wxRichTextCtrl::MoveEnd(int flags
)
1340 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1342 if (m_caretPosition
!= endPos
)
1344 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1348 SetCaretPosition(endPos
);
1350 SetDefaultStyleToCursorStyle();
1360 /// Move noPages pages up
1361 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1363 return PageDown(- noPages
, flags
);
1366 /// Move noPages pages down
1367 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1369 // Calculate which line occurs noPages * screen height further down.
1370 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1373 wxSize clientSize
= GetClientSize();
1374 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1376 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1379 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1380 long pos
= lineRange
.GetStart()-1;
1381 if (pos
!= m_caretPosition
)
1383 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1385 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1389 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1391 SetDefaultStyleToCursorStyle();
1403 // Finds the caret position for the next word
1404 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1406 long endPos
= GetBuffer().GetRange().GetEnd();
1410 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1412 // First skip current text to space
1413 while (i
< endPos
&& i
> -1)
1415 // i is in character, not caret positions
1416 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1417 if (text
!= wxT(" ") && !text
.empty())
1424 while (i
< endPos
&& i
> -1)
1426 // i is in character, not caret positions
1427 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1428 if (text
.empty()) // End of paragraph, or maybe an image
1429 return wxMax(-1, i
- 1);
1430 else if (text
== wxT(" ") || text
.empty())
1434 // Convert to caret position
1435 return wxMax(-1, i
- 1);
1444 long i
= m_caretPosition
;
1446 // First skip white space
1447 while (i
< endPos
&& i
> -1)
1449 // i is in character, not caret positions
1450 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1451 if (text
.empty()) // End of paragraph, or maybe an image
1453 else if (text
== wxT(" ") || text
.empty())
1458 // Next skip current text to space
1459 while (i
< endPos
&& i
> -1)
1461 // i is in character, not caret positions
1462 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1463 if (text
!= wxT(" ") /* && !text.empty() */)
1476 /// Move n words left
1477 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1479 long pos
= FindNextWordPosition(-1);
1480 if (pos
!= m_caretPosition
)
1482 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1484 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1488 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1490 SetDefaultStyleToCursorStyle();
1500 /// Move n words right
1501 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1503 long pos
= FindNextWordPosition(1);
1504 if (pos
!= m_caretPosition
)
1506 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1508 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1512 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1514 SetDefaultStyleToCursorStyle();
1525 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1527 // Only do sizing optimization for large buffers
1528 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1530 m_fullLayoutRequired
= true;
1531 m_fullLayoutTime
= wxGetLocalTimeMillis();
1532 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1533 LayoutContent(true /* onlyVisibleRect */);
1536 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1538 #if wxRICHTEXT_BUFFERED_PAINTING
1546 /// Idle-time processing
1547 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1549 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1551 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1553 m_fullLayoutRequired
= false;
1554 m_fullLayoutTime
= 0;
1555 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1556 ShowPosition(m_fullLayoutSavedPosition
);
1560 if (m_caretPositionForDefaultStyle
!= -2)
1562 // If the caret position has changed, no longer reflect the default style
1564 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1565 m_caretPositionForDefaultStyle
= -2;
1572 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1578 /// Set up scrollbars, e.g. after a resize
1579 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1584 if (GetBuffer().IsEmpty())
1586 SetScrollbars(0, 0, 0, 0, 0, 0);
1590 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1591 // of pixels. See e.g. wxVScrolledWindow for ideas.
1592 int pixelsPerUnit
= 5;
1593 wxSize clientSize
= GetClientSize();
1595 int maxHeight
= GetBuffer().GetCachedSize().y
;
1597 // Round up so we have at least maxHeight pixels
1598 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1600 int startX
= 0, startY
= 0;
1602 GetViewStart(& startX
, & startY
);
1604 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1605 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1607 // Move to previous scroll position if
1609 SetScrollbars(0, pixelsPerUnit
,
1611 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1614 /// Paint the background
1615 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1617 wxColour backgroundColour
= GetBackgroundColour();
1618 if (!backgroundColour
.Ok())
1619 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1621 // Clear the background
1622 dc
.SetBrush(wxBrush(backgroundColour
));
1623 dc
.SetPen(*wxTRANSPARENT_PEN
);
1624 wxRect
windowRect(GetClientSize());
1625 windowRect
.x
-= 2; windowRect
.y
-= 2;
1626 windowRect
.width
+= 4; windowRect
.height
+= 4;
1628 // We need to shift the rectangle to take into account
1629 // scrolling. Converting device to logical coordinates.
1630 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1631 dc
.DrawRectangle(windowRect
);
1634 #if wxRICHTEXT_BUFFERED_PAINTING
1635 /// Recreate buffer bitmap if necessary
1636 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1639 if (sz
== wxDefaultSize
)
1640 sz
= GetClientSize();
1642 if (sz
.x
< 1 || sz
.y
< 1)
1645 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1646 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1647 return m_bufferBitmap
.Ok();
1651 // ----------------------------------------------------------------------------
1652 // file IO functions
1653 // ----------------------------------------------------------------------------
1655 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1656 bool wxRichTextCtrl::LoadFile(const wxString
& filename
, int fileType
)
1658 return DoLoadFile(filename
, fileType
);
1661 bool wxRichTextCtrl::SaveFile(const wxString
& filename
, int fileType
)
1663 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
1664 if ( filenameToUse
.empty() )
1666 // what kind of message to give? is it an error or a program bug?
1667 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1672 return DoSaveFile(filenameToUse
, fileType
);
1676 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1678 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1680 m_filename
= filename
;
1683 SetInsertionPoint(0);
1686 SetupScrollbars(true);
1694 wxLogError(_("File couldn't be loaded."));
1700 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1702 if (GetBuffer().SaveFile(filename
, fileType
))
1704 m_filename
= filename
;
1711 wxLogError(_("The text couldn't be saved."));
1716 // ----------------------------------------------------------------------------
1717 // wxRichTextCtrl specific functionality
1718 // ----------------------------------------------------------------------------
1720 /// Add a new paragraph of text to the end of the buffer
1721 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1723 return GetBuffer().AddParagraph(text
);
1727 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1729 return GetBuffer().AddImage(image
);
1732 // ----------------------------------------------------------------------------
1733 // selection and ranges
1734 // ----------------------------------------------------------------------------
1736 void wxRichTextCtrl::SelectAll()
1738 SetSelection(0, GetLastPosition()+1);
1739 m_selectionAnchor
= -1;
1743 void wxRichTextCtrl::SelectNone()
1745 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1746 SetSelection(-2, -2);
1747 m_selectionAnchor
= -2;
1750 static bool wxIsWordDelimiter(const wxString
& text
)
1752 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1755 /// Select the word at the given character position
1756 bool wxRichTextCtrl::SelectWord(long position
)
1758 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1761 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1765 long positionStart
= position
;
1766 long positionEnd
= position
;
1768 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1770 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1771 if (wxIsWordDelimiter(text
))
1777 if (positionStart
< para
->GetRange().GetStart())
1778 positionStart
= para
->GetRange().GetStart();
1780 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1782 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1783 if (wxIsWordDelimiter(text
))
1789 if (positionEnd
>= para
->GetRange().GetEnd())
1790 positionEnd
= para
->GetRange().GetEnd();
1792 SetSelection(positionStart
, positionEnd
+1);
1794 if (positionStart
>= 0)
1796 MoveCaret(positionStart
-1, true);
1797 SetDefaultStyleToCursorStyle();
1803 wxString
wxRichTextCtrl::GetStringSelection() const
1806 GetSelection(&from
, &to
);
1808 return GetRange(from
, to
);
1811 // do the window-specific processing after processing the update event
1812 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1813 void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1816 wxWindowBase::DoUpdateWindowUI(event
);
1819 if ( event
.GetSetText() )
1821 if ( event
.GetText() != GetValue() )
1822 SetValue(event
.GetText());
1825 #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1827 // ----------------------------------------------------------------------------
1829 // ----------------------------------------------------------------------------
1831 wxTextCtrlHitTestResult
1832 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1834 // implement in terms of the other overload as the native ports typically
1835 // can get the position and not (x, y) pair directly (although wxUniv
1836 // directly gets x and y -- and so overrides this method as well)
1838 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1840 if ( rc
!= wxTE_HT_UNKNOWN
)
1842 PositionToXY(pos
, x
, y
);
1848 wxTextCtrlHitTestResult
1849 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1852 wxClientDC
dc((wxRichTextCtrl
*) this);
1853 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1855 // Buffer uses logical position (relative to start of buffer)
1857 wxPoint pt2
= GetLogicalPoint(pt
);
1859 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1863 case wxRICHTEXT_HITTEST_BEFORE
:
1864 return wxTE_HT_BEFORE
;
1866 case wxRICHTEXT_HITTEST_AFTER
:
1867 return wxTE_HT_BEYOND
;
1869 case wxRICHTEXT_HITTEST_ON
:
1870 return wxTE_HT_ON_TEXT
;
1873 return wxTE_HT_UNKNOWN
;
1876 // ----------------------------------------------------------------------------
1877 // set/get the controls text
1878 // ----------------------------------------------------------------------------
1880 wxString
wxRichTextCtrl::GetValue() const
1882 return GetBuffer().GetText();
1885 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1887 // Public API for range is different from internals
1888 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1891 void wxRichTextCtrl::SetValue(const wxString
& value
)
1895 // if the text is long enough, it's faster to just set it instead of first
1896 // comparing it with the old one (chances are that it will be different
1897 // anyhow, this comparison is there to avoid flicker for small single-line
1898 // edit controls mostly)
1899 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1901 DoWriteText(value
, false /* not selection only */);
1903 // for compatibility, don't move the cursor when doing SetValue()
1904 SetInsertionPoint(0);
1908 // still send an event for consistency
1912 // we should reset the modified flag even if the value didn't really change
1914 // mark the control as being not dirty - we changed its text, not the
1919 void wxRichTextCtrl::WriteText(const wxString
& value
)
1924 void wxRichTextCtrl::DoWriteText(const wxString
& value
, bool WXUNUSED(selectionOnly
))
1926 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1928 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
1931 void wxRichTextCtrl::AppendText(const wxString
& text
)
1933 SetInsertionPointEnd();
1938 /// Write an image at the current insertion point
1939 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
1941 wxRichTextImageBlock imageBlock
;
1943 wxImage image2
= image
;
1944 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
1945 return WriteImage(imageBlock
);
1950 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
1952 wxRichTextImageBlock imageBlock
;
1955 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
1956 return WriteImage(imageBlock
);
1961 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
1963 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
1966 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
1970 wxRichTextImageBlock imageBlock
;
1972 wxImage image
= bitmap
.ConvertToImage();
1973 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
1974 return WriteImage(imageBlock
);
1980 /// Insert a newline (actually paragraph) at the current insertion point.
1981 bool wxRichTextCtrl::Newline()
1983 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
1987 // ----------------------------------------------------------------------------
1988 // Clipboard operations
1989 // ----------------------------------------------------------------------------
1991 void wxRichTextCtrl::Copy()
1995 wxRichTextRange range
= GetInternalSelectionRange();
1996 GetBuffer().CopyToClipboard(range
);
2000 void wxRichTextCtrl::Cut()
2004 wxRichTextRange range
= GetInternalSelectionRange();
2005 GetBuffer().CopyToClipboard(range
);
2007 DeleteSelectedContent();
2013 void wxRichTextCtrl::Paste()
2017 BeginBatchUndo(_("Paste"));
2019 long newPos
= m_caretPosition
;
2020 DeleteSelectedContent(& newPos
);
2022 GetBuffer().PasteFromClipboard(newPos
);
2028 void wxRichTextCtrl::DeleteSelection()
2030 if (CanDeleteSelection())
2032 DeleteSelectedContent();
2036 bool wxRichTextCtrl::HasSelection() const
2038 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2041 bool wxRichTextCtrl::CanCopy() const
2043 // Can copy if there's a selection
2044 return HasSelection();
2047 bool wxRichTextCtrl::CanCut() const
2049 return HasSelection() && IsEditable();
2052 bool wxRichTextCtrl::CanPaste() const
2054 if ( !IsEditable() )
2057 return GetBuffer().CanPasteFromClipboard();
2060 bool wxRichTextCtrl::CanDeleteSelection() const
2062 return HasSelection() && IsEditable();
2066 // ----------------------------------------------------------------------------
2068 // ----------------------------------------------------------------------------
2070 void wxRichTextCtrl::SetEditable(bool editable
)
2072 m_editable
= editable
;
2075 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2079 m_caretPosition
= pos
- 1;
2082 void wxRichTextCtrl::SetInsertionPointEnd()
2084 long pos
= GetLastPosition();
2085 SetInsertionPoint(pos
);
2088 long wxRichTextCtrl::GetInsertionPoint() const
2090 return m_caretPosition
+1;
2093 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2095 return GetBuffer().GetRange().GetEnd();
2098 // If the return values from and to are the same, there is no
2100 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2102 *from
= m_selectionRange
.GetStart();
2103 *to
= m_selectionRange
.GetEnd();
2104 if ((*to
) != -1 && (*to
) != -2)
2108 bool wxRichTextCtrl::IsEditable() const
2113 // ----------------------------------------------------------------------------
2115 // ----------------------------------------------------------------------------
2117 void wxRichTextCtrl::SetSelection(long from
, long to
)
2119 // if from and to are both -1, it means (in wxWidgets) that all text should
2121 if ( (from
== -1) && (to
== -1) )
2124 to
= GetLastPosition()+1;
2127 DoSetSelection(from
, to
);
2130 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2132 m_selectionAnchor
= from
;
2133 m_selectionRange
.SetRange(from
, to
-1);
2139 // ----------------------------------------------------------------------------
2141 // ----------------------------------------------------------------------------
2143 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
), const wxString
& value
)
2145 BeginBatchUndo(_("Replace"));
2147 DeleteSelectedContent();
2149 DoWriteText(value
, true /* selection only */);
2154 void wxRichTextCtrl::Remove(long from
, long to
)
2158 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2159 m_caretPosition
, // Current caret position
2160 from
, // New caret position
2168 bool wxRichTextCtrl::IsModified() const
2170 return m_buffer
.IsModified();
2173 void wxRichTextCtrl::MarkDirty()
2175 m_buffer
.Modify(true);
2178 void wxRichTextCtrl::DiscardEdits()
2180 m_caretPositionForDefaultStyle
= -2;
2181 m_buffer
.Modify(false);
2182 m_buffer
.GetCommandProcessor()->ClearCommands();
2185 int wxRichTextCtrl::GetNumberOfLines() const
2187 return GetBuffer().GetParagraphCount();
2190 // ----------------------------------------------------------------------------
2191 // Positions <-> coords
2192 // ----------------------------------------------------------------------------
2194 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2196 return GetBuffer().XYToPosition(x
, y
);
2199 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2201 return GetBuffer().PositionToXY(pos
, x
, y
);
2204 // ----------------------------------------------------------------------------
2206 // ----------------------------------------------------------------------------
2208 void wxRichTextCtrl::ShowPosition(long pos
)
2210 if (!IsPositionVisible(pos
))
2211 ScrollIntoView(pos
-1, WXK_DOWN
);
2214 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2216 return GetBuffer().GetParagraphLength(lineNo
);
2219 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2221 return GetBuffer().GetParagraphText(lineNo
);
2224 // ----------------------------------------------------------------------------
2226 // ----------------------------------------------------------------------------
2228 void wxRichTextCtrl::Undo()
2232 GetCommandProcessor()->Undo();
2236 void wxRichTextCtrl::Redo()
2240 GetCommandProcessor()->Redo();
2244 bool wxRichTextCtrl::CanUndo() const
2246 return GetCommandProcessor()->CanUndo();
2249 bool wxRichTextCtrl::CanRedo() const
2251 return GetCommandProcessor()->CanRedo();
2254 // ----------------------------------------------------------------------------
2255 // implementation details
2256 // ----------------------------------------------------------------------------
2258 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2260 SetValue(event
.GetString());
2261 GetEventHandler()->ProcessEvent(event
);
2264 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2266 // By default, load the first file into the text window.
2267 if (event
.GetNumberOfFiles() > 0)
2269 LoadFile(event
.GetFiles()[0]);
2273 // ----------------------------------------------------------------------------
2274 // text control event processing
2275 // ----------------------------------------------------------------------------
2277 bool wxRichTextCtrl::SendUpdateEvent()
2279 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
2280 InitCommandEvent(event
);
2282 return GetEventHandler()->ProcessEvent(event
);
2285 void wxRichTextCtrl::InitCommandEvent(wxCommandEvent
& event
) const
2287 event
.SetEventObject((wxControlBase
*)this); // const_cast
2289 switch ( m_clientDataType
)
2291 case wxClientData_Void
:
2292 event
.SetClientData(GetClientData());
2295 case wxClientData_Object
:
2296 event
.SetClientObject(GetClientObject());
2299 case wxClientData_None
:
2306 wxSize
wxRichTextCtrl::DoGetBestSize() const
2308 return wxSize(10, 10);
2311 // ----------------------------------------------------------------------------
2312 // standard handlers for standard edit menu events
2313 // ----------------------------------------------------------------------------
2315 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2320 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2325 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2330 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2335 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2340 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2345 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2347 event
.Enable( CanCut() );
2350 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2352 event
.Enable( CanCopy() );
2355 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2357 event
.Enable( CanDeleteSelection() );
2360 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2362 event
.Enable( CanPaste() );
2365 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2367 event
.Enable( CanUndo() );
2368 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2371 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2373 event
.Enable( CanRedo() );
2374 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2377 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2382 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2384 event
.Enable(GetLastPosition() > 0);
2387 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2391 m_contextMenu
= new wxMenu
;
2392 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2393 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2394 m_contextMenu
->AppendSeparator();
2395 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2396 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2397 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2398 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2399 m_contextMenu
->AppendSeparator();
2400 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2402 PopupMenu(m_contextMenu
);
2406 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2408 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2411 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2413 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2416 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2418 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2421 // extended style setting operation with flags including:
2422 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2423 // see richtextbuffer.h for more details.
2424 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2426 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2429 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2431 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2434 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2436 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2439 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2441 return GetBuffer().SetDefaultStyle(style
);
2444 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2446 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2449 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2451 return GetBuffer().GetDefaultStyle();
2454 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2456 return GetBuffer().GetDefaultStyle();
2459 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2461 wxTextAttrEx
attr(style
);
2462 if (GetBuffer().GetStyle(position
, attr
))
2471 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2473 return GetBuffer().GetStyle(position
, style
);
2476 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2478 return GetBuffer().GetStyle(position
, style
);
2481 /// Get the content (uncombined) attributes for this position.
2483 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2485 wxTextAttrEx
attr(style
);
2486 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2495 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2497 return GetBuffer().GetUncombinedStyle(position
, style
);
2500 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2502 return GetBuffer().GetUncombinedStyle(position
, style
);
2505 /// Set font, and also the buffer attributes
2506 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2508 #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2509 wxControl::SetFont(font
);
2511 wxScrolledWindow::SetFont(font
);
2514 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2516 GetBuffer().SetBasicStyle(attr
);
2518 #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2519 // Don't set the default style, since it will be inherited from
2521 GetBuffer().SetDefaultStyle(attr
);
2524 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2530 /// Transform logical to physical
2531 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2534 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2539 /// Transform physical to logical
2540 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2543 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2548 /// Position the caret
2549 void wxRichTextCtrl::PositionCaret()
2554 //wxLogDebug(wxT("PositionCaret"));
2557 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2559 wxPoint originalPt
= caretRect
.GetPosition();
2560 wxPoint pt
= GetPhysicalPoint(originalPt
);
2561 if (GetCaret()->GetPosition() != pt
)
2563 GetCaret()->Move(pt
);
2564 GetCaret()->SetSize(caretRect
.GetSize());
2569 /// Get the caret height and position for the given character position
2570 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2572 wxClientDC
dc(this);
2573 dc
.SetFont(GetFont());
2580 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2582 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2589 /// Gets the line for the visible caret position. If the caret is
2590 /// shown at the very end of the line, it means the next character is actually
2591 /// on the following line. So let's get the line we're expecting to find
2592 /// if this is the case.
2593 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2595 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2596 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2599 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2600 if (caretPosition
== lineRange
.GetStart()-1 &&
2601 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2603 if (!m_caretAtLineStart
)
2604 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2611 /// Move the caret to the given character position
2612 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2614 if (GetBuffer().GetDirty())
2617 if (pos
<= GetBuffer().GetRange().GetEnd())
2619 SetCaretPosition(pos
, showAtLineStart
);
2629 /// Layout the buffer: which we must do before certain operations, such as
2630 /// setting the caret position.
2631 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2633 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2635 wxRect
availableSpace(GetClientSize());
2636 if (availableSpace
.width
== 0)
2637 availableSpace
.width
= 10;
2638 if (availableSpace
.height
== 0)
2639 availableSpace
.height
= 10;
2641 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2642 if (onlyVisibleRect
)
2644 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2645 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2648 wxClientDC
dc(this);
2649 dc
.SetFont(GetFont());
2653 GetBuffer().Defragment();
2654 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2655 GetBuffer().Layout(dc
, availableSpace
, flags
);
2656 GetBuffer().SetDirty(false);
2665 /// Is all of the selection bold?
2666 bool wxRichTextCtrl::IsSelectionBold()
2670 wxRichTextAttr attr
;
2671 wxRichTextRange range
= GetInternalSelectionRange();
2672 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2673 attr
.SetFontWeight(wxBOLD
);
2675 return HasCharacterAttributes(range
, attr
);
2679 // If no selection, then we need to combine current style with default style
2680 // to see what the effect would be if we started typing.
2681 wxRichTextAttr attr
;
2682 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2684 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2685 if (GetStyle(pos
, attr
))
2687 if (IsDefaultStyleShowing())
2688 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2689 return attr
.GetFontWeight() == wxBOLD
;
2695 /// Is all of the selection italics?
2696 bool wxRichTextCtrl::IsSelectionItalics()
2700 wxRichTextRange range
= GetInternalSelectionRange();
2701 wxRichTextAttr attr
;
2702 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2703 attr
.SetFontStyle(wxITALIC
);
2705 return HasCharacterAttributes(range
, attr
);
2709 // If no selection, then we need to combine current style with default style
2710 // to see what the effect would be if we started typing.
2711 wxRichTextAttr attr
;
2712 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2714 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2715 if (GetStyle(pos
, attr
))
2717 if (IsDefaultStyleShowing())
2718 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2719 return attr
.GetFontStyle() == wxITALIC
;
2725 /// Is all of the selection underlined?
2726 bool wxRichTextCtrl::IsSelectionUnderlined()
2730 wxRichTextRange range
= GetInternalSelectionRange();
2731 wxRichTextAttr attr
;
2732 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2733 attr
.SetFontUnderlined(true);
2735 return HasCharacterAttributes(range
, attr
);
2739 // If no selection, then we need to combine current style with default style
2740 // to see what the effect would be if we started typing.
2741 wxRichTextAttr attr
;
2742 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2743 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2745 if (GetStyle(pos
, attr
))
2747 if (IsDefaultStyleShowing())
2748 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2749 return attr
.GetFontUnderlined();
2755 /// Apply bold to the selection
2756 bool wxRichTextCtrl::ApplyBoldToSelection()
2758 wxRichTextAttr attr
;
2759 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2760 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2763 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2765 SetAndShowDefaultStyle(attr
);
2769 /// Apply italic to the selection
2770 bool wxRichTextCtrl::ApplyItalicToSelection()
2772 wxRichTextAttr attr
;
2773 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2774 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2777 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2779 SetAndShowDefaultStyle(attr
);
2783 /// Apply underline to the selection
2784 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2786 wxRichTextAttr attr
;
2787 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2788 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2791 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2793 SetAndShowDefaultStyle(attr
);
2797 /// Is all of the selection aligned according to the specified flag?
2798 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2800 wxRichTextRange range
;
2802 range
= GetInternalSelectionRange();
2804 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2806 wxRichTextAttr attr
;
2807 attr
.SetAlignment(alignment
);
2809 return HasParagraphAttributes(range
, attr
);
2812 /// Apply alignment to the selection
2813 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2815 wxRichTextAttr attr
;
2816 attr
.SetAlignment(alignment
);
2818 return SetStyle(GetSelectionRange(), attr
);
2821 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2823 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2828 /// Apply a named style to the selection
2829 void wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2831 // Flags are defined within each definition, so only certain
2832 // attributes are applied.
2833 wxRichTextAttr
attr(def
->GetStyle());
2835 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2837 // Make sure the attr has the style name
2838 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2840 attr
.SetParagraphStyleName(def
->GetName());
2842 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2843 // attributes, and not the leaf nodes. This will allow the context (e.g. text)
2844 // to change its style independently.
2845 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2848 attr
.SetCharacterStyleName(def
->GetName());
2851 SetStyleEx(GetSelectionRange(), attr
, flags
);
2853 SetAndShowDefaultStyle(attr
);
2856 /// Apply the style sheet to the buffer, for example if the styles have changed.
2857 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2860 styleSheet
= GetBuffer().GetStyleSheet();
2864 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2866 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2874 /// Sets the default style to the style under the cursor
2875 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2878 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2880 // If at the start of a paragraph, use the next position.
2881 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2883 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2884 if (GetUncombinedStyle(pos
, attr
))
2886 if (GetStyle(pos
, attr
))
2889 SetDefaultStyle(attr
);
2896 /// Returns the first visible position in the current view
2897 long wxRichTextCtrl::GetFirstVisiblePosition() const
2899 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2901 return line
->GetAbsoluteRange().GetStart();
2906 /// The adjusted caret position is the character position adjusted to take
2907 /// into account whether we're at the start of a paragraph, in which case
2908 /// style information should be taken from the next position, not current one.
2909 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2911 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2913 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2918 /// Get/set the selection range in character positions. -1, -1 means no selection.
2919 /// The range is in API convention, i.e. a single character selection is denoted
2921 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2923 wxRichTextRange range
= GetInternalSelectionRange();
2924 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2925 range
.SetEnd(range
.GetEnd() + 1);
2929 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2931 wxRichTextRange
range1(range
);
2932 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2933 range1
.SetEnd(range1
.GetEnd() - 1);
2935 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2937 SetInternalSelectionRange(range1
);