1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richeditctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
29 #include "wx/textfile.h"
31 #include "wx/filename.h"
32 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 #include "wx/fontenum.h"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED
)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED
)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
)
52 IMPLEMENT_CLASS( wxRichTextCtrl
, wxControl
)
54 IMPLEMENT_CLASS( wxRichTextEvent
, wxNotifyEvent
)
56 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
57 EVT_PAINT(wxRichTextCtrl::OnPaint
)
58 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
59 EVT_IDLE(wxRichTextCtrl::OnIdle
)
60 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
61 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
62 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
63 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
64 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
65 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
66 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
67 EVT_CHAR(wxRichTextCtrl::OnChar
)
68 EVT_SIZE(wxRichTextCtrl::OnSize
)
69 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
70 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
71 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
73 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
74 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
76 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
77 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
79 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
80 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
82 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
83 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
85 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
86 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
88 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
89 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
91 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
92 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
99 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
101 wxRichTextCtrl::wxRichTextCtrl()
102 : wxScrollHelper(this)
107 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
109 const wxString
& value
,
113 const wxValidator
& validator
,
114 const wxString
& name
)
115 : wxScrollHelper(this)
118 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
122 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
123 const wxValidator
& validator
, const wxString
& name
)
125 if (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
126 style
|wxFULL_REPAINT_ON_RESIZE
,
132 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
136 GetBuffer().SetRichTextCtrl(this);
138 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
141 if (style
& wxTE_READONLY
)
144 // The base attributes must all have default values
145 wxTextAttrEx attributes
;
146 attributes
.SetFont(GetFont());
147 attributes
.SetTextColour(*wxBLACK
);
148 attributes
.SetBackgroundColour(*wxWHITE
);
149 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
150 attributes
.SetLineSpacing(10);
151 attributes
.SetParagraphSpacingAfter(10);
152 attributes
.SetParagraphSpacingBefore(0);
153 attributes
.SetTextEffects(0);
154 attributes
.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_CAPITALS
);
156 SetBasicStyle(attributes
);
158 // The default attributes will be merged with base attributes, so
159 // can be empty to begin with
160 wxTextAttrEx defaultAttributes
;
161 SetDefaultStyle(defaultAttributes
);
163 SetBackgroundColour(*wxWHITE
);
164 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
166 // Tell the sizers to use the given or best size
167 SetInitialSize(size
);
169 #if wxRICHTEXT_BUFFERED_PAINTING
171 RecreateBuffer(size
);
174 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
175 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
177 SetCursor(m_textCursor
);
179 if (!value
.IsEmpty())
182 GetBuffer().AddEventHandler(this);
187 wxRichTextCtrl::~wxRichTextCtrl()
189 GetBuffer().RemoveEventHandler(this);
191 delete m_contextMenu
;
194 /// Member initialisation
195 void wxRichTextCtrl::Init()
198 m_contextMenu
= NULL
;
200 m_caretPosition
= -1;
201 m_selectionRange
.SetRange(-2, -2);
202 m_selectionAnchor
= -2;
204 m_caretAtLineStart
= false;
206 m_fullLayoutRequired
= false;
207 m_fullLayoutTime
= 0;
208 m_fullLayoutSavedPosition
= 0;
209 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
210 m_caretPositionForDefaultStyle
= -2;
213 /// Call Freeze to prevent refresh
214 void wxRichTextCtrl::Freeze()
219 /// Call Thaw to refresh
220 void wxRichTextCtrl::Thaw()
224 if (m_freezeCount
== 0)
232 void wxRichTextCtrl::Clear()
234 m_buffer
.ResetAndClearCommands();
235 m_buffer
.SetDirty(true);
236 m_caretPosition
= -1;
237 m_caretPositionForDefaultStyle
= -2;
238 m_caretAtLineStart
= false;
239 m_selectionRange
.SetRange(-2, -2);
241 SetScrollbars(0, 0, 0, 0, 0, 0);
243 if (m_freezeCount
== 0)
248 SendTextUpdatedEvent();
252 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
254 if (GetCaret() && GetCaret()->IsVisible())
258 #if wxRICHTEXT_BUFFERED_PAINTING
259 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
265 if (m_freezeCount
> 0)
268 dc
.SetFont(GetFont());
270 // Paint the background
273 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
275 wxRect
drawingArea(GetUpdateRegion().GetBox());
276 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
278 wxRect
availableSpace(GetClientSize());
279 if (GetBuffer().GetDirty())
281 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
282 GetBuffer().SetDirty(false);
286 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
289 if (GetCaret() && !GetCaret()->IsVisible())
295 // Empty implementation, to prevent flicker
296 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
300 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
304 if (!GetCaret()->IsVisible())
313 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
315 if (GetCaret() && GetCaret()->IsVisible())
323 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
329 dc
.SetFont(GetFont());
332 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
334 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
336 m_dragStart
= event
.GetLogicalPosition(dc
);
342 bool caretAtLineStart
= false;
344 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
346 // If we're at the start of a line (but not first in para)
347 // then we should keep the caret showing at the start of the line
348 // by showing the m_caretAtLineStart flag.
349 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
350 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
352 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
353 caretAtLineStart
= true;
357 MoveCaret(position
, caretAtLineStart
);
358 SetDefaultStyleToCursorStyle();
365 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
370 if (GetCapture() == this)
373 // See if we clicked on a URL
376 dc
.SetFont(GetFont());
379 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
380 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
382 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
385 if (GetStyle(position
, attr
))
387 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
389 wxString urlTarget
= attr
.GetURL();
390 if (!urlTarget
.IsEmpty())
392 wxMouseEvent
mouseEvent(event
);
394 long startPos
= 0, endPos
= 0;
395 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
398 startPos
= obj
->GetRange().GetStart();
399 endPos
= obj
->GetRange().GetEnd();
402 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
403 InitCommandEvent(urlEvent
);
405 urlEvent
.SetString(urlTarget
);
407 GetEventHandler()->ProcessEvent(urlEvent
);
416 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
420 dc
.SetFont(GetFont());
423 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
424 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
426 // See if we need to change the cursor
429 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
432 if (GetStyle(position
, attr
))
434 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
436 SetCursor(m_urlCursor
);
438 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
440 SetCursor(m_textCursor
);
446 if (!event
.Dragging())
452 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
454 // TODO: test closeness
456 bool caretAtLineStart
= false;
458 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
460 // If we're at the start of a line (but not first in para)
461 // then we should keep the caret showing at the start of the line
462 // by showing the m_caretAtLineStart flag.
463 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
464 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
466 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
467 caretAtLineStart
= true;
471 if (m_caretPosition
!= position
)
473 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
475 MoveCaret(position
, caretAtLineStart
);
476 SetDefaultStyleToCursorStyle();
485 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
491 /// Left-double-click
492 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
494 SelectWord(GetCaretPosition()+1);
499 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
505 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
509 flags
|= wxRICHTEXT_CTRL_DOWN
;
510 if (event
.ShiftDown())
511 flags
|= wxRICHTEXT_SHIFT_DOWN
;
513 flags
|= wxRICHTEXT_ALT_DOWN
;
515 if (event
.GetKeyCode() == WXK_LEFT
||
516 event
.GetKeyCode() == WXK_RIGHT
||
517 event
.GetKeyCode() == WXK_UP
||
518 event
.GetKeyCode() == WXK_DOWN
||
519 event
.GetKeyCode() == WXK_HOME
||
520 event
.GetKeyCode() == WXK_PAGEUP
||
521 event
.GetKeyCode() == WXK_PAGEDOWN
||
522 event
.GetKeyCode() == WXK_END
||
524 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
525 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
526 event
.GetKeyCode() == WXK_NUMPAD_UP
||
527 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
528 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
529 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
530 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
531 event
.GetKeyCode() == WXK_NUMPAD_END
)
533 KeyboardNavigate(event
.GetKeyCode(), flags
);
537 // all the other keys modify the controls contents which shouldn't be
538 // possible if we're read-only
545 if (event
.GetKeyCode() == WXK_RETURN
)
547 BeginBatchUndo(_("Insert Text"));
549 long newPos
= m_caretPosition
;
551 DeleteSelectedContent(& newPos
);
553 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
555 SetDefaultStyleToCursorStyle();
557 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
559 wxRichTextEvent
cmdEvent(
560 wxEVT_COMMAND_RICHTEXT_RETURN
,
562 cmdEvent
.SetEventObject(this);
563 cmdEvent
.SetFlags(flags
);
564 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
566 // Generate conventional event
567 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
568 InitCommandEvent(textEvent
);
570 GetEventHandler()->ProcessEvent(textEvent
);
574 else if (event
.GetKeyCode() == WXK_BACK
)
576 BeginBatchUndo(_("Delete Text"));
578 // Submit range in character positions, which are greater than caret positions,
579 // so subtract 1 for deleted character and add 1 for conversion to character position.
580 if (m_caretPosition
> -1 && !HasSelection())
582 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
583 m_caretPosition
, // Current caret position
584 m_caretPosition
-1, // New caret position
588 DeleteSelectedContent();
592 if (GetLastPosition() == -1)
596 m_caretPosition
= -1;
598 SetDefaultStyleToCursorStyle();
601 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
604 else if (event
.GetKeyCode() == WXK_DELETE
)
606 BeginBatchUndo(_("Delete Text"));
608 // Submit range in character positions, which are greater than caret positions,
609 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
611 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
612 m_caretPosition
, // Current caret position
613 m_caretPosition
+1, // New caret position
617 DeleteSelectedContent();
621 if (GetLastPosition() == -1)
625 m_caretPosition
= -1;
627 SetDefaultStyleToCursorStyle();
633 long keycode
= event
.GetKeyCode();
707 case WXK_NUMPAD_SPACE
:
709 case WXK_NUMPAD_ENTER
:
714 case WXK_NUMPAD_HOME
:
715 case WXK_NUMPAD_LEFT
:
717 case WXK_NUMPAD_RIGHT
:
718 case WXK_NUMPAD_DOWN
:
719 case WXK_NUMPAD_PAGEUP
:
720 case WXK_NUMPAD_PAGEDOWN
:
722 case WXK_NUMPAD_BEGIN
:
723 case WXK_NUMPAD_INSERT
:
724 case WXK_NUMPAD_DELETE
:
725 case WXK_NUMPAD_EQUAL
:
726 case WXK_NUMPAD_MULTIPLY
:
728 case WXK_NUMPAD_SEPARATOR
:
729 case WXK_NUMPAD_SUBTRACT
:
730 case WXK_NUMPAD_DECIMAL
:
738 if (event
.CmdDown() || event
.AltDown())
744 if (keycode
== wxT('\t'))
746 // See if we need to promote or demote the selection or paragraph at the cursor
747 // position, instead of inserting a tab.
748 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
749 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
750 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
752 wxRichTextRange range
;
754 range
= GetSelectionRange();
756 range
= para
->GetRange().FromInternal();
758 int promoteBy
= event
.ShiftDown() ? 1 : -1;
760 PromoteList(promoteBy
, range
, NULL
);
766 BeginBatchUndo(_("Insert Text"));
768 long newPos
= m_caretPosition
;
769 DeleteSelectedContent(& newPos
);
771 wxString str
= (wxChar
) event
.GetKeyCode();
772 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
776 SetDefaultStyleToCursorStyle();
777 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
784 /// Delete content if there is a selection, e.g. when pressing a key.
785 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
789 long pos
= m_selectionRange
.GetStart();
790 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
791 m_caretPosition
, // Current caret position
792 pos
, // New caret position
794 m_selectionRange
.SetRange(-2, -2);
804 /// Keyboard navigation
808 Left: left one character
809 Right: right one character
812 Ctrl-Left: left one word
813 Ctrl-Right: right one word
814 Ctrl-Up: previous paragraph start
815 Ctrl-Down: next start of paragraph
818 Ctrl-Home: start of document
819 Ctrl-End: end of document
821 Page-Down: Down a screen
825 Ctrl-Alt-PgUp: Start of window
826 Ctrl-Alt-PgDn: End of window
827 F8: Start selection mode
828 Esc: End selection mode
830 Adding Shift does the above but starts/extends selection.
835 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
837 bool success
= false;
839 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
841 if (flags
& wxRICHTEXT_CTRL_DOWN
)
842 success
= WordRight(1, flags
);
844 success
= MoveRight(1, flags
);
846 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
848 if (flags
& wxRICHTEXT_CTRL_DOWN
)
849 success
= WordLeft(1, flags
);
851 success
= MoveLeft(1, flags
);
853 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
855 if (flags
& wxRICHTEXT_CTRL_DOWN
)
856 success
= MoveToParagraphStart(flags
);
858 success
= MoveUp(1, flags
);
860 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
862 if (flags
& wxRICHTEXT_CTRL_DOWN
)
863 success
= MoveToParagraphEnd(flags
);
865 success
= MoveDown(1, flags
);
867 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
869 success
= PageUp(1, flags
);
871 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
873 success
= PageDown(1, flags
);
875 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
877 if (flags
& wxRICHTEXT_CTRL_DOWN
)
878 success
= MoveHome(flags
);
880 success
= MoveToLineStart(flags
);
882 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
884 if (flags
& wxRICHTEXT_CTRL_DOWN
)
885 success
= MoveEnd(flags
);
887 success
= MoveToLineEnd(flags
);
892 ScrollIntoView(m_caretPosition
, keyCode
);
893 SetDefaultStyleToCursorStyle();
899 /// Extend the selection. Selections are in caret positions.
900 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
902 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
904 // If not currently selecting, start selecting
905 if (m_selectionRange
.GetStart() == -2)
907 m_selectionAnchor
= oldPos
;
910 m_selectionRange
.SetRange(newPos
+1, oldPos
);
912 m_selectionRange
.SetRange(oldPos
+1, newPos
);
916 // Always ensure that the selection range start is greater than
918 if (newPos
> m_selectionAnchor
)
919 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
921 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
924 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
926 wxLogDebug(wxT("Strange selection range"));
935 /// Scroll into view, returning true if we scrolled.
936 /// This takes a _caret_ position.
937 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
939 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
945 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
947 int startXUnits
, startYUnits
;
948 GetViewStart(& startXUnits
, & startYUnits
);
949 int startY
= startYUnits
* ppuY
;
952 GetVirtualSize(& sx
, & sy
);
958 wxRect rect
= line
->GetRect();
960 bool scrolled
= false;
962 wxSize clientSize
= GetClientSize();
965 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
966 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
967 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
968 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
970 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
972 // Make it scroll so this item is at the bottom
974 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
975 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
977 // If we're still off the screen, scroll another line down
978 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
981 if (startYUnits
!= yUnits
)
983 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
987 else if (rect
.y
< startY
)
989 // Make it scroll so this item is at the top
992 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
994 if (startYUnits
!= yUnits
)
996 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1002 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1003 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1004 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1005 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1007 if (rect
.y
< startY
)
1009 // Make it scroll so this item is at the top
1012 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1014 if (startYUnits
!= yUnits
)
1016 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1020 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1022 // Make it scroll so this item is at the bottom
1024 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1025 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1027 // If we're still off the screen, scroll another line down
1028 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1031 if (startYUnits
!= yUnits
)
1033 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1043 /// Is the given position visible on the screen?
1044 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1046 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1052 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1055 GetViewStart(& startX
, & startY
);
1057 startY
= startY
* ppuY
;
1060 GetVirtualSize(& sx
, & sy
);
1065 wxRect rect
= line
->GetRect();
1067 wxSize clientSize
= GetClientSize();
1069 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1072 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1074 m_caretPosition
= position
;
1075 m_caretAtLineStart
= showAtLineStart
;
1078 /// Move caret one visual step forward: this may mean setting a flag
1079 /// and keeping the same position if we're going from the end of one line
1080 /// to the start of the next, which may be the exact same caret position.
1081 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1083 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1085 // Only do the check if we're not at the end of the paragraph (where things work OK
1087 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1089 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1093 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1095 // We're at the end of a line. See whether we need to
1096 // stay at the same actual caret position but change visual
1097 // position, or not.
1098 if (oldPosition
== lineRange
.GetEnd())
1100 if (m_caretAtLineStart
)
1102 // We're already at the start of the line, so actually move on now.
1103 m_caretPosition
= oldPosition
+ 1;
1104 m_caretAtLineStart
= false;
1108 // We're showing at the end of the line, so keep to
1109 // the same position but indicate that we're to show
1110 // at the start of the next line.
1111 m_caretPosition
= oldPosition
;
1112 m_caretAtLineStart
= true;
1114 SetDefaultStyleToCursorStyle();
1120 SetDefaultStyleToCursorStyle();
1123 /// Move caret one visual step backward: this may mean setting a flag
1124 /// and keeping the same position if we're going from the end of one line
1125 /// to the start of the next, which may be the exact same caret position.
1126 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1128 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1130 // Only do the check if we're not at the start of the paragraph (where things work OK
1132 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1134 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1138 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1140 // We're at the start of a line. See whether we need to
1141 // stay at the same actual caret position but change visual
1142 // position, or not.
1143 if (oldPosition
== lineRange
.GetStart())
1145 m_caretPosition
= oldPosition
-1;
1146 m_caretAtLineStart
= true;
1149 else if (oldPosition
== lineRange
.GetEnd())
1151 if (m_caretAtLineStart
)
1153 // We're at the start of the line, so keep the same caret position
1154 // but clear the start-of-line flag.
1155 m_caretPosition
= oldPosition
;
1156 m_caretAtLineStart
= false;
1160 // We're showing at the end of the line, so go back
1161 // to the previous character position.
1162 m_caretPosition
= oldPosition
- 1;
1164 SetDefaultStyleToCursorStyle();
1170 SetDefaultStyleToCursorStyle();
1174 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1176 long endPos
= GetBuffer().GetRange().GetEnd();
1178 if (m_caretPosition
+ noPositions
< endPos
)
1180 long oldPos
= m_caretPosition
;
1181 long newPos
= m_caretPosition
+ noPositions
;
1183 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1187 // Determine by looking at oldPos and m_caretPosition whether
1188 // we moved from the end of a line to the start of the next line, in which case
1189 // we want to adjust the caret position such that it is positioned at the
1190 // start of the next line, rather than jumping past the first character of the
1192 if (noPositions
== 1 && !extendSel
)
1193 MoveCaretForward(oldPos
);
1195 SetCaretPosition(newPos
);
1198 SetDefaultStyleToCursorStyle();
1209 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1213 if (m_caretPosition
> startPos
- noPositions
+ 1)
1215 long oldPos
= m_caretPosition
;
1216 long newPos
= m_caretPosition
- noPositions
;
1217 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1221 if (noPositions
== 1 && !extendSel
)
1222 MoveCaretBack(oldPos
);
1224 SetCaretPosition(newPos
);
1227 SetDefaultStyleToCursorStyle();
1238 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1240 return MoveDown(- noLines
, flags
);
1244 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1249 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1250 wxPoint pt
= GetCaret()->GetPosition();
1251 long newLine
= lineNumber
+ noLines
;
1253 if (lineNumber
!= -1)
1257 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1259 if (newLine
> lastLine
)
1269 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1272 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1278 wxClientDC
dc(this);
1280 dc
.SetFont(GetFont());
1282 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1284 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1286 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1287 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1288 // so we view the caret at the start of the line.
1289 bool caretLineStart
= false;
1290 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1292 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1293 wxRichTextRange lineRange
;
1295 lineRange
= thisLine
->GetAbsoluteRange();
1297 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1300 caretLineStart
= true;
1304 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1305 if (para
&& para
->GetRange().GetStart() == newPos
)
1310 long newSelEnd
= newPos
;
1312 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1316 SetCaretPosition(newPos
, caretLineStart
);
1318 SetDefaultStyleToCursorStyle();
1328 /// Move to the end of the paragraph
1329 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1331 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1334 long newPos
= para
->GetRange().GetEnd() - 1;
1335 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1339 SetCaretPosition(newPos
);
1341 SetDefaultStyleToCursorStyle();
1351 /// Move to the start of the paragraph
1352 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1354 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1357 long newPos
= para
->GetRange().GetStart() - 1;
1358 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1362 SetCaretPosition(newPos
);
1364 SetDefaultStyleToCursorStyle();
1374 /// Move to the end of the line
1375 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1377 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1381 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1382 long newPos
= lineRange
.GetEnd();
1383 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1387 SetCaretPosition(newPos
);
1389 SetDefaultStyleToCursorStyle();
1399 /// Move to the start of the line
1400 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1402 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1405 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1406 long newPos
= lineRange
.GetStart()-1;
1408 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1412 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1414 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1416 SetDefaultStyleToCursorStyle();
1426 /// Move to the start of the buffer
1427 bool wxRichTextCtrl::MoveHome(int flags
)
1429 if (m_caretPosition
!= -1)
1431 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1435 SetCaretPosition(-1);
1437 SetDefaultStyleToCursorStyle();
1447 /// Move to the end of the buffer
1448 bool wxRichTextCtrl::MoveEnd(int flags
)
1450 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1452 if (m_caretPosition
!= endPos
)
1454 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1458 SetCaretPosition(endPos
);
1460 SetDefaultStyleToCursorStyle();
1470 /// Move noPages pages up
1471 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1473 return PageDown(- noPages
, flags
);
1476 /// Move noPages pages down
1477 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1479 // Calculate which line occurs noPages * screen height further down.
1480 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1483 wxSize clientSize
= GetClientSize();
1484 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1486 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1489 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1490 long pos
= lineRange
.GetStart()-1;
1491 if (pos
!= m_caretPosition
)
1493 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1495 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1499 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1501 SetDefaultStyleToCursorStyle();
1513 // Finds the caret position for the next word
1514 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1516 long endPos
= GetBuffer().GetRange().GetEnd();
1520 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1522 // First skip current text to space
1523 while (i
< endPos
&& i
> -1)
1525 // i is in character, not caret positions
1526 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1527 if (text
!= wxT(" ") && !text
.empty())
1534 while (i
< endPos
&& i
> -1)
1536 // i is in character, not caret positions
1537 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1538 if (text
.empty()) // End of paragraph, or maybe an image
1539 return wxMax(-1, i
- 1);
1540 else if (text
== wxT(" ") || text
.empty())
1544 // Convert to caret position
1545 return wxMax(-1, i
- 1);
1554 long i
= m_caretPosition
;
1556 // First skip white space
1557 while (i
< endPos
&& i
> -1)
1559 // i is in character, not caret positions
1560 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1561 if (text
.empty()) // End of paragraph, or maybe an image
1563 else if (text
== wxT(" ") || text
.empty())
1568 // Next skip current text to space
1569 while (i
< endPos
&& i
> -1)
1571 // i is in character, not caret positions
1572 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1573 if (text
!= wxT(" ") /* && !text.empty() */)
1586 /// Move n words left
1587 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1589 long pos
= FindNextWordPosition(-1);
1590 if (pos
!= m_caretPosition
)
1592 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1594 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1598 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1600 SetDefaultStyleToCursorStyle();
1610 /// Move n words right
1611 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1613 long pos
= FindNextWordPosition(1);
1614 if (pos
!= m_caretPosition
)
1616 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1618 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1622 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1624 SetDefaultStyleToCursorStyle();
1635 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1637 // Only do sizing optimization for large buffers
1638 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1640 m_fullLayoutRequired
= true;
1641 m_fullLayoutTime
= wxGetLocalTimeMillis();
1642 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1643 LayoutContent(true /* onlyVisibleRect */);
1646 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1648 #if wxRICHTEXT_BUFFERED_PAINTING
1656 /// Idle-time processing
1657 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1659 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1661 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1663 m_fullLayoutRequired
= false;
1664 m_fullLayoutTime
= 0;
1665 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1666 ShowPosition(m_fullLayoutSavedPosition
);
1670 if (m_caretPositionForDefaultStyle
!= -2)
1672 // If the caret position has changed, no longer reflect the default style
1674 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1675 m_caretPositionForDefaultStyle
= -2;
1682 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1688 /// Set up scrollbars, e.g. after a resize
1689 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1694 if (GetBuffer().IsEmpty())
1696 SetScrollbars(0, 0, 0, 0, 0, 0);
1700 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1701 // of pixels. See e.g. wxVScrolledWindow for ideas.
1702 int pixelsPerUnit
= 5;
1703 wxSize clientSize
= GetClientSize();
1705 int maxHeight
= GetBuffer().GetCachedSize().y
;
1707 // Round up so we have at least maxHeight pixels
1708 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1710 int startX
= 0, startY
= 0;
1712 GetViewStart(& startX
, & startY
);
1714 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1715 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1717 // Move to previous scroll position if
1719 SetScrollbars(0, pixelsPerUnit
,
1721 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1724 /// Paint the background
1725 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1727 wxColour backgroundColour
= GetBackgroundColour();
1728 if (!backgroundColour
.Ok())
1729 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1731 // Clear the background
1732 dc
.SetBrush(wxBrush(backgroundColour
));
1733 dc
.SetPen(*wxTRANSPARENT_PEN
);
1734 wxRect
windowRect(GetClientSize());
1735 windowRect
.x
-= 2; windowRect
.y
-= 2;
1736 windowRect
.width
+= 4; windowRect
.height
+= 4;
1738 // We need to shift the rectangle to take into account
1739 // scrolling. Converting device to logical coordinates.
1740 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1741 dc
.DrawRectangle(windowRect
);
1744 #if wxRICHTEXT_BUFFERED_PAINTING
1745 /// Recreate buffer bitmap if necessary
1746 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1749 if (sz
== wxDefaultSize
)
1750 sz
= GetClientSize();
1752 if (sz
.x
< 1 || sz
.y
< 1)
1755 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1756 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1757 return m_bufferBitmap
.Ok();
1761 // ----------------------------------------------------------------------------
1762 // file IO functions
1763 // ----------------------------------------------------------------------------
1765 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1767 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1769 m_filename
= filename
;
1772 SetInsertionPoint(0);
1775 SetupScrollbars(true);
1777 SendTextUpdatedEvent();
1783 wxLogError(_("File couldn't be loaded."));
1789 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1791 if (GetBuffer().SaveFile(filename
, fileType
))
1793 m_filename
= filename
;
1800 wxLogError(_("The text couldn't be saved."));
1805 // ----------------------------------------------------------------------------
1806 // wxRichTextCtrl specific functionality
1807 // ----------------------------------------------------------------------------
1809 /// Add a new paragraph of text to the end of the buffer
1810 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1812 return GetBuffer().AddParagraph(text
);
1816 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1818 return GetBuffer().AddImage(image
);
1821 // ----------------------------------------------------------------------------
1822 // selection and ranges
1823 // ----------------------------------------------------------------------------
1825 void wxRichTextCtrl::SelectAll()
1827 SetSelection(0, GetLastPosition()+1);
1828 m_selectionAnchor
= -1;
1832 void wxRichTextCtrl::SelectNone()
1834 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1835 SetSelection(-2, -2);
1836 m_selectionAnchor
= -2;
1839 static bool wxIsWordDelimiter(const wxString
& text
)
1841 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1844 /// Select the word at the given character position
1845 bool wxRichTextCtrl::SelectWord(long position
)
1847 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1850 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1854 long positionStart
= position
;
1855 long positionEnd
= position
;
1857 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1859 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1860 if (wxIsWordDelimiter(text
))
1866 if (positionStart
< para
->GetRange().GetStart())
1867 positionStart
= para
->GetRange().GetStart();
1869 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1871 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1872 if (wxIsWordDelimiter(text
))
1878 if (positionEnd
>= para
->GetRange().GetEnd())
1879 positionEnd
= para
->GetRange().GetEnd();
1881 SetSelection(positionStart
, positionEnd
+1);
1883 if (positionStart
>= 0)
1885 MoveCaret(positionStart
-1, true);
1886 SetDefaultStyleToCursorStyle();
1892 wxString
wxRichTextCtrl::GetStringSelection() const
1895 GetSelection(&from
, &to
);
1897 return GetRange(from
, to
);
1900 // ----------------------------------------------------------------------------
1902 // ----------------------------------------------------------------------------
1904 wxTextCtrlHitTestResult
1905 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1907 // implement in terms of the other overload as the native ports typically
1908 // can get the position and not (x, y) pair directly (although wxUniv
1909 // directly gets x and y -- and so overrides this method as well)
1911 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1913 if ( rc
!= wxTE_HT_UNKNOWN
)
1915 PositionToXY(pos
, x
, y
);
1921 wxTextCtrlHitTestResult
1922 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1925 wxClientDC
dc((wxRichTextCtrl
*) this);
1926 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1928 // Buffer uses logical position (relative to start of buffer)
1930 wxPoint pt2
= GetLogicalPoint(pt
);
1932 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1936 case wxRICHTEXT_HITTEST_BEFORE
:
1937 return wxTE_HT_BEFORE
;
1939 case wxRICHTEXT_HITTEST_AFTER
:
1940 return wxTE_HT_BEYOND
;
1942 case wxRICHTEXT_HITTEST_ON
:
1943 return wxTE_HT_ON_TEXT
;
1946 return wxTE_HT_UNKNOWN
;
1949 // ----------------------------------------------------------------------------
1950 // set/get the controls text
1951 // ----------------------------------------------------------------------------
1953 wxString
wxRichTextCtrl::GetValue() const
1955 return GetBuffer().GetText();
1958 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1960 // Public API for range is different from internals
1961 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1964 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1968 // if the text is long enough, it's faster to just set it instead of first
1969 // comparing it with the old one (chances are that it will be different
1970 // anyhow, this comparison is there to avoid flicker for small single-line
1971 // edit controls mostly)
1972 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1976 // for compatibility, don't move the cursor when doing SetValue()
1977 SetInsertionPoint(0);
1981 if ( flags
& SetValue_SendEvent
)
1983 // still send an event for consistency
1984 SendTextUpdatedEvent();
1988 // we should reset the modified flag even if the value didn't really change
1990 // mark the control as being not dirty - we changed its text, not the
1995 void wxRichTextCtrl::WriteText(const wxString
& value
)
2000 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2002 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2004 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
2006 if ( flags
& SetValue_SendEvent
)
2007 SendTextUpdatedEvent();
2010 void wxRichTextCtrl::AppendText(const wxString
& text
)
2012 SetInsertionPointEnd();
2017 /// Write an image at the current insertion point
2018 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2020 wxRichTextImageBlock imageBlock
;
2022 wxImage image2
= image
;
2023 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2024 return WriteImage(imageBlock
);
2029 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2031 wxRichTextImageBlock imageBlock
;
2034 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2035 return WriteImage(imageBlock
);
2040 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2042 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2045 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2049 wxRichTextImageBlock imageBlock
;
2051 wxImage image
= bitmap
.ConvertToImage();
2052 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2053 return WriteImage(imageBlock
);
2059 /// Insert a newline (actually paragraph) at the current insertion point.
2060 bool wxRichTextCtrl::Newline()
2062 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2066 // ----------------------------------------------------------------------------
2067 // Clipboard operations
2068 // ----------------------------------------------------------------------------
2070 void wxRichTextCtrl::Copy()
2074 wxRichTextRange range
= GetInternalSelectionRange();
2075 GetBuffer().CopyToClipboard(range
);
2079 void wxRichTextCtrl::Cut()
2083 wxRichTextRange range
= GetInternalSelectionRange();
2084 GetBuffer().CopyToClipboard(range
);
2086 DeleteSelectedContent();
2092 void wxRichTextCtrl::Paste()
2096 BeginBatchUndo(_("Paste"));
2098 long newPos
= m_caretPosition
;
2099 DeleteSelectedContent(& newPos
);
2101 GetBuffer().PasteFromClipboard(newPos
);
2107 void wxRichTextCtrl::DeleteSelection()
2109 if (CanDeleteSelection())
2111 DeleteSelectedContent();
2115 bool wxRichTextCtrl::HasSelection() const
2117 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2120 bool wxRichTextCtrl::CanCopy() const
2122 // Can copy if there's a selection
2123 return HasSelection();
2126 bool wxRichTextCtrl::CanCut() const
2128 return HasSelection() && IsEditable();
2131 bool wxRichTextCtrl::CanPaste() const
2133 if ( !IsEditable() )
2136 return GetBuffer().CanPasteFromClipboard();
2139 bool wxRichTextCtrl::CanDeleteSelection() const
2141 return HasSelection() && IsEditable();
2145 // ----------------------------------------------------------------------------
2147 // ----------------------------------------------------------------------------
2149 void wxRichTextCtrl::SetEditable(bool editable
)
2151 m_editable
= editable
;
2154 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2158 m_caretPosition
= pos
- 1;
2161 void wxRichTextCtrl::SetInsertionPointEnd()
2163 long pos
= GetLastPosition();
2164 SetInsertionPoint(pos
);
2167 long wxRichTextCtrl::GetInsertionPoint() const
2169 return m_caretPosition
+1;
2172 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2174 return GetBuffer().GetRange().GetEnd();
2177 // If the return values from and to are the same, there is no
2179 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2181 *from
= m_selectionRange
.GetStart();
2182 *to
= m_selectionRange
.GetEnd();
2183 if ((*to
) != -1 && (*to
) != -2)
2187 bool wxRichTextCtrl::IsEditable() const
2192 // ----------------------------------------------------------------------------
2194 // ----------------------------------------------------------------------------
2196 void wxRichTextCtrl::SetSelection(long from
, long to
)
2198 // if from and to are both -1, it means (in wxWidgets) that all text should
2200 if ( (from
== -1) && (to
== -1) )
2203 to
= GetLastPosition()+1;
2206 DoSetSelection(from
, to
);
2209 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2211 m_selectionAnchor
= from
;
2212 m_selectionRange
.SetRange(from
, to
-1);
2218 // ----------------------------------------------------------------------------
2220 // ----------------------------------------------------------------------------
2222 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2223 const wxString
& value
)
2225 BeginBatchUndo(_("Replace"));
2227 DeleteSelectedContent();
2229 DoWriteText(value
, SetValue_SelectionOnly
);
2234 void wxRichTextCtrl::Remove(long from
, long to
)
2238 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2239 m_caretPosition
, // Current caret position
2240 from
, // New caret position
2248 bool wxRichTextCtrl::IsModified() const
2250 return m_buffer
.IsModified();
2253 void wxRichTextCtrl::MarkDirty()
2255 m_buffer
.Modify(true);
2258 void wxRichTextCtrl::DiscardEdits()
2260 m_caretPositionForDefaultStyle
= -2;
2261 m_buffer
.Modify(false);
2262 m_buffer
.GetCommandProcessor()->ClearCommands();
2265 int wxRichTextCtrl::GetNumberOfLines() const
2267 return GetBuffer().GetParagraphCount();
2270 // ----------------------------------------------------------------------------
2271 // Positions <-> coords
2272 // ----------------------------------------------------------------------------
2274 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2276 return GetBuffer().XYToPosition(x
, y
);
2279 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2281 return GetBuffer().PositionToXY(pos
, x
, y
);
2284 // ----------------------------------------------------------------------------
2286 // ----------------------------------------------------------------------------
2288 void wxRichTextCtrl::ShowPosition(long pos
)
2290 if (!IsPositionVisible(pos
))
2291 ScrollIntoView(pos
-1, WXK_DOWN
);
2294 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2296 return GetBuffer().GetParagraphLength(lineNo
);
2299 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2301 return GetBuffer().GetParagraphText(lineNo
);
2304 // ----------------------------------------------------------------------------
2306 // ----------------------------------------------------------------------------
2308 void wxRichTextCtrl::Undo()
2312 GetCommandProcessor()->Undo();
2316 void wxRichTextCtrl::Redo()
2320 GetCommandProcessor()->Redo();
2324 bool wxRichTextCtrl::CanUndo() const
2326 return GetCommandProcessor()->CanUndo();
2329 bool wxRichTextCtrl::CanRedo() const
2331 return GetCommandProcessor()->CanRedo();
2334 // ----------------------------------------------------------------------------
2335 // implementation details
2336 // ----------------------------------------------------------------------------
2338 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2340 SetValue(event
.GetString());
2341 GetEventHandler()->ProcessEvent(event
);
2344 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2346 // By default, load the first file into the text window.
2347 if (event
.GetNumberOfFiles() > 0)
2349 LoadFile(event
.GetFiles()[0]);
2353 wxSize
wxRichTextCtrl::DoGetBestSize() const
2355 return wxSize(10, 10);
2358 // ----------------------------------------------------------------------------
2359 // standard handlers for standard edit menu events
2360 // ----------------------------------------------------------------------------
2362 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2367 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2372 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2377 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2382 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2387 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2392 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2394 event
.Enable( CanCut() );
2397 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2399 event
.Enable( CanCopy() );
2402 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2404 event
.Enable( CanDeleteSelection() );
2407 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2409 event
.Enable( CanPaste() );
2412 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2414 event
.Enable( CanUndo() );
2415 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2418 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2420 event
.Enable( CanRedo() );
2421 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2424 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2429 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2431 event
.Enable(GetLastPosition() > 0);
2434 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2438 m_contextMenu
= new wxMenu
;
2439 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2440 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2441 m_contextMenu
->AppendSeparator();
2442 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2443 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2444 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2445 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2446 m_contextMenu
->AppendSeparator();
2447 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2449 PopupMenu(m_contextMenu
);
2453 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2455 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2458 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2460 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2463 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2465 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2468 // extended style setting operation with flags including:
2469 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2470 // see richtextbuffer.h for more details.
2471 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2473 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2476 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2478 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2481 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2483 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2486 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2488 return GetBuffer().SetDefaultStyle(style
);
2491 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2493 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2496 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2498 return GetBuffer().GetDefaultStyle();
2501 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2503 return GetBuffer().GetDefaultStyle();
2506 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2508 wxTextAttrEx
attr(style
);
2509 if (GetBuffer().GetStyle(position
, attr
))
2518 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2520 return GetBuffer().GetStyle(position
, style
);
2523 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2525 return GetBuffer().GetStyle(position
, style
);
2528 /// Get the content (uncombined) attributes for this position.
2530 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2532 wxTextAttrEx
attr(style
);
2533 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2542 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2544 return GetBuffer().GetUncombinedStyle(position
, style
);
2547 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2549 return GetBuffer().GetUncombinedStyle(position
, style
);
2552 /// Set font, and also the buffer attributes
2553 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2555 wxControl::SetFont(font
);
2557 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2559 GetBuffer().SetBasicStyle(attr
);
2561 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2567 /// Transform logical to physical
2568 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2571 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2576 /// Transform physical to logical
2577 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2580 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2585 /// Position the caret
2586 void wxRichTextCtrl::PositionCaret()
2591 //wxLogDebug(wxT("PositionCaret"));
2594 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2596 wxPoint originalPt
= caretRect
.GetPosition();
2597 wxPoint pt
= GetPhysicalPoint(originalPt
);
2598 if (GetCaret()->GetPosition() != pt
)
2600 GetCaret()->Move(pt
);
2601 GetCaret()->SetSize(caretRect
.GetSize());
2606 /// Get the caret height and position for the given character position
2607 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2609 wxClientDC
dc(this);
2610 dc
.SetFont(GetFont());
2617 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2619 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2626 /// Gets the line for the visible caret position. If the caret is
2627 /// shown at the very end of the line, it means the next character is actually
2628 /// on the following line. So let's get the line we're expecting to find
2629 /// if this is the case.
2630 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2632 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2633 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2636 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2637 if (caretPosition
== lineRange
.GetStart()-1 &&
2638 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2640 if (!m_caretAtLineStart
)
2641 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2648 /// Move the caret to the given character position
2649 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2651 if (GetBuffer().GetDirty())
2654 if (pos
<= GetBuffer().GetRange().GetEnd())
2656 SetCaretPosition(pos
, showAtLineStart
);
2666 /// Layout the buffer: which we must do before certain operations, such as
2667 /// setting the caret position.
2668 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2670 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2672 wxRect
availableSpace(GetClientSize());
2673 if (availableSpace
.width
== 0)
2674 availableSpace
.width
= 10;
2675 if (availableSpace
.height
== 0)
2676 availableSpace
.height
= 10;
2678 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2679 if (onlyVisibleRect
)
2681 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2682 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2685 wxClientDC
dc(this);
2686 dc
.SetFont(GetFont());
2690 GetBuffer().Defragment();
2691 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2692 GetBuffer().Layout(dc
, availableSpace
, flags
);
2693 GetBuffer().SetDirty(false);
2702 /// Is all of the selection bold?
2703 bool wxRichTextCtrl::IsSelectionBold()
2707 wxRichTextAttr attr
;
2708 wxRichTextRange range
= GetInternalSelectionRange();
2709 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2710 attr
.SetFontWeight(wxBOLD
);
2712 return HasCharacterAttributes(range
, attr
);
2716 // If no selection, then we need to combine current style with default style
2717 // to see what the effect would be if we started typing.
2718 wxRichTextAttr attr
;
2719 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2721 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2722 if (GetStyle(pos
, attr
))
2724 if (IsDefaultStyleShowing())
2725 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2726 return attr
.GetFontWeight() == wxBOLD
;
2732 /// Is all of the selection italics?
2733 bool wxRichTextCtrl::IsSelectionItalics()
2737 wxRichTextRange range
= GetInternalSelectionRange();
2738 wxRichTextAttr attr
;
2739 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2740 attr
.SetFontStyle(wxITALIC
);
2742 return HasCharacterAttributes(range
, attr
);
2746 // If no selection, then we need to combine current style with default style
2747 // to see what the effect would be if we started typing.
2748 wxRichTextAttr attr
;
2749 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2751 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2752 if (GetStyle(pos
, attr
))
2754 if (IsDefaultStyleShowing())
2755 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2756 return attr
.GetFontStyle() == wxITALIC
;
2762 /// Is all of the selection underlined?
2763 bool wxRichTextCtrl::IsSelectionUnderlined()
2767 wxRichTextRange range
= GetInternalSelectionRange();
2768 wxRichTextAttr attr
;
2769 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2770 attr
.SetFontUnderlined(true);
2772 return HasCharacterAttributes(range
, attr
);
2776 // If no selection, then we need to combine current style with default style
2777 // to see what the effect would be if we started typing.
2778 wxRichTextAttr attr
;
2779 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2780 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2782 if (GetStyle(pos
, attr
))
2784 if (IsDefaultStyleShowing())
2785 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2786 return attr
.GetFontUnderlined();
2792 /// Apply bold to the selection
2793 bool wxRichTextCtrl::ApplyBoldToSelection()
2795 wxRichTextAttr attr
;
2796 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2797 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2800 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2802 SetAndShowDefaultStyle(attr
);
2806 /// Apply italic to the selection
2807 bool wxRichTextCtrl::ApplyItalicToSelection()
2809 wxRichTextAttr attr
;
2810 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2811 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2814 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2816 SetAndShowDefaultStyle(attr
);
2820 /// Apply underline to the selection
2821 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2823 wxRichTextAttr attr
;
2824 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2825 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2828 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2830 SetAndShowDefaultStyle(attr
);
2834 /// Is all of the selection aligned according to the specified flag?
2835 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2837 wxRichTextRange range
;
2839 range
= GetInternalSelectionRange();
2841 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2843 wxRichTextAttr attr
;
2844 attr
.SetAlignment(alignment
);
2846 return HasParagraphAttributes(range
, attr
);
2849 /// Apply alignment to the selection
2850 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2852 wxRichTextAttr attr
;
2853 attr
.SetAlignment(alignment
);
2855 return SetStyle(GetSelectionRange(), attr
);
2858 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2860 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2865 /// Apply a named style to the selection
2866 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2868 // Flags are defined within each definition, so only certain
2869 // attributes are applied.
2870 wxRichTextAttr
attr(def
->GetStyle());
2872 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2874 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2876 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2878 wxRichTextRange range
;
2881 range
= GetSelectionRange();
2884 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2885 range
= wxRichTextRange(pos
, pos
+1);
2888 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2891 // Make sure the attr has the style name
2892 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2894 attr
.SetParagraphStyleName(def
->GetName());
2896 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2897 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2898 // to change its style independently.
2899 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2902 attr
.SetCharacterStyleName(def
->GetName());
2905 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2908 SetAndShowDefaultStyle(attr
);
2913 /// Apply the style sheet to the buffer, for example if the styles have changed.
2914 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2917 styleSheet
= GetBuffer().GetStyleSheet();
2921 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2923 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2931 /// Sets the default style to the style under the cursor
2932 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2935 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2937 // If at the start of a paragraph, use the next position.
2938 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2940 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2941 if (GetUncombinedStyle(pos
, attr
))
2943 if (GetStyle(pos
, attr
))
2946 SetDefaultStyle(attr
);
2953 /// Returns the first visible position in the current view
2954 long wxRichTextCtrl::GetFirstVisiblePosition() const
2956 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2958 return line
->GetAbsoluteRange().GetStart();
2963 /// Get the first visible point in the window
2964 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
2967 int startXUnits
, startYUnits
;
2969 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
2970 GetViewStart(& startXUnits
, & startYUnits
);
2972 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
2975 /// The adjusted caret position is the character position adjusted to take
2976 /// into account whether we're at the start of a paragraph, in which case
2977 /// style information should be taken from the next position, not current one.
2978 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2980 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2982 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2987 /// Get/set the selection range in character positions. -1, -1 means no selection.
2988 /// The range is in API convention, i.e. a single character selection is denoted
2990 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2992 wxRichTextRange range
= GetInternalSelectionRange();
2993 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2994 range
.SetEnd(range
.GetEnd() + 1);
2998 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3000 wxRichTextRange
range1(range
);
3001 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
3002 range1
.SetEnd(range1
.GetEnd() - 1);
3004 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
3006 SetInternalSelectionRange(range1
);
3010 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3012 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3015 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3017 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3020 /// Clear list for given range
3021 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3023 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3026 /// Number/renumber any list elements in the given range
3027 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3029 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3032 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3034 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3037 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3038 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3040 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3043 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3045 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3048 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3050 if (sm_availableFontNames
.GetCount() == 0)
3052 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3053 sm_availableFontNames
.Sort();
3055 return sm_availableFontNames
;
3058 void wxRichTextCtrl::ClearAvailableFontNames()
3060 sm_availableFontNames
.Clear();