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 : 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 (!wxTextCtrlBase::Create(parent
, id
, pos
, size
,
123 style
|wxFULL_REPAINT_ON_RESIZE
))
128 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
132 GetBuffer().SetRichTextCtrl(this);
134 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
137 if (style
& wxTE_READONLY
)
140 wxTextAttrEx attributes
;
141 attributes
.SetFont(GetFont());
142 attributes
.SetTextColour(*wxBLACK
);
143 attributes
.SetBackgroundColour(*wxWHITE
);
144 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
145 attributes
.SetLineSpacing(10);
146 attributes
.SetParagraphSpacingAfter(10);
147 attributes
.SetParagraphSpacingBefore(0);
149 SetBasicStyle(attributes
);
151 // The default attributes will be merged with base attributes, so
152 // can be empty to begin with
153 wxTextAttrEx defaultAttributes
;
154 SetDefaultStyle(defaultAttributes
);
156 SetBackgroundColour(*wxWHITE
);
157 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
159 // Tell the sizers to use the given or best size
160 SetBestFittingSize(size
);
162 #if wxRICHTEXT_BUFFERED_PAINTING
164 RecreateBuffer(size
);
167 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
168 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
170 SetCursor(m_textCursor
);
172 if (!value
.IsEmpty())
175 GetBuffer().AddEventHandler(this);
180 wxRichTextCtrl::~wxRichTextCtrl()
182 GetBuffer().RemoveEventHandler(this);
184 delete m_contextMenu
;
187 /// Member initialisation
188 void wxRichTextCtrl::Init()
191 m_contextMenu
= NULL
;
193 m_caretPosition
= -1;
194 m_selectionRange
.SetRange(-2, -2);
195 m_selectionAnchor
= -2;
197 m_caretAtLineStart
= false;
199 m_fullLayoutRequired
= false;
200 m_fullLayoutTime
= 0;
201 m_fullLayoutSavedPosition
= 0;
202 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
203 m_caretPositionForDefaultStyle
= -2;
206 /// Call Freeze to prevent refresh
207 void wxRichTextCtrl::Freeze()
212 /// Call Thaw to refresh
213 void wxRichTextCtrl::Thaw()
217 if (m_freezeCount
== 0)
225 void wxRichTextCtrl::Clear()
227 m_buffer
.ResetAndClearCommands();
228 m_buffer
.SetDirty(true);
229 m_caretPosition
= -1;
230 m_caretPositionForDefaultStyle
= -2;
231 m_caretAtLineStart
= false;
232 m_selectionRange
.SetRange(-2, -2);
234 SetScrollbars(0, 0, 0, 0, 0, 0);
236 if (m_freezeCount
== 0)
241 SendTextUpdatedEvent();
245 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
247 if (GetCaret() && GetCaret()->IsVisible())
251 #if wxRICHTEXT_BUFFERED_PAINTING
252 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
258 if (m_freezeCount
> 0)
261 dc
.SetFont(GetFont());
263 // Paint the background
266 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
268 wxRect
drawingArea(GetUpdateRegion().GetBox());
269 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
271 wxRect
availableSpace(GetClientSize());
272 if (GetBuffer().GetDirty())
274 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
275 GetBuffer().SetDirty(false);
279 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
282 if (GetCaret() && !GetCaret()->IsVisible())
288 // Empty implementation, to prevent flicker
289 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
293 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
297 if (!GetCaret()->IsVisible())
306 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
308 if (GetCaret() && GetCaret()->IsVisible())
316 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
322 dc
.SetFont(GetFont());
325 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
327 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
329 m_dragStart
= event
.GetLogicalPosition(dc
);
335 bool caretAtLineStart
= false;
337 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
339 // If we're at the start of a line (but not first in para)
340 // then we should keep the caret showing at the start of the line
341 // by showing the m_caretAtLineStart flag.
342 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
343 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
345 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
346 caretAtLineStart
= true;
350 MoveCaret(position
, caretAtLineStart
);
351 SetDefaultStyleToCursorStyle();
358 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
363 if (GetCapture() == this)
366 // See if we clicked on a URL
369 dc
.SetFont(GetFont());
372 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
373 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
375 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
378 if (GetStyle(position
, attr
))
380 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
382 wxString urlTarget
= attr
.GetURL();
383 if (!urlTarget
.IsEmpty())
385 wxMouseEvent
mouseEvent(event
);
387 long startPos
= 0, endPos
= 0;
388 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
391 startPos
= obj
->GetRange().GetStart();
392 endPos
= obj
->GetRange().GetEnd();
395 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
396 InitCommandEvent(urlEvent
);
398 urlEvent
.SetString(urlTarget
);
400 GetEventHandler()->ProcessEvent(urlEvent
);
409 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
413 dc
.SetFont(GetFont());
416 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
417 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
419 // See if we need to change the cursor
422 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
425 if (GetStyle(position
, attr
))
427 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
429 SetCursor(m_urlCursor
);
431 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
433 SetCursor(m_textCursor
);
439 if (!event
.Dragging())
445 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
447 // TODO: test closeness
449 bool caretAtLineStart
= false;
451 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
453 // If we're at the start of a line (but not first in para)
454 // then we should keep the caret showing at the start of the line
455 // by showing the m_caretAtLineStart flag.
456 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
457 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
459 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
460 caretAtLineStart
= true;
464 if (m_caretPosition
!= position
)
466 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
468 MoveCaret(position
, caretAtLineStart
);
469 SetDefaultStyleToCursorStyle();
478 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
484 /// Left-double-click
485 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
487 SelectWord(GetCaretPosition()+1);
492 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
498 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
502 flags
|= wxRICHTEXT_CTRL_DOWN
;
503 if (event
.ShiftDown())
504 flags
|= wxRICHTEXT_SHIFT_DOWN
;
506 flags
|= wxRICHTEXT_ALT_DOWN
;
508 if (event
.GetKeyCode() == WXK_LEFT
||
509 event
.GetKeyCode() == WXK_RIGHT
||
510 event
.GetKeyCode() == WXK_UP
||
511 event
.GetKeyCode() == WXK_DOWN
||
512 event
.GetKeyCode() == WXK_HOME
||
513 event
.GetKeyCode() == WXK_PAGEUP
||
514 event
.GetKeyCode() == WXK_PAGEDOWN
||
515 event
.GetKeyCode() == WXK_END
||
517 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
518 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
519 event
.GetKeyCode() == WXK_NUMPAD_UP
||
520 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
521 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
522 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
523 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
524 event
.GetKeyCode() == WXK_NUMPAD_END
)
526 KeyboardNavigate(event
.GetKeyCode(), flags
);
530 // all the other keys modify the controls contents which shouldn't be
531 // possible if we're read-only
538 if (event
.GetKeyCode() == WXK_RETURN
)
540 BeginBatchUndo(_("Insert Text"));
542 long newPos
= m_caretPosition
;
544 DeleteSelectedContent(& newPos
);
546 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
548 SetDefaultStyleToCursorStyle();
550 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
552 wxRichTextEvent
cmdEvent(
553 wxEVT_COMMAND_RICHTEXT_RETURN
,
555 cmdEvent
.SetEventObject(this);
556 cmdEvent
.SetFlags(flags
);
557 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
559 // Generate conventional event
560 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
561 InitCommandEvent(textEvent
);
563 GetEventHandler()->ProcessEvent(textEvent
);
567 else if (event
.GetKeyCode() == WXK_BACK
)
569 BeginBatchUndo(_("Delete Text"));
571 // Submit range in character positions, which are greater than caret positions,
572 // so subtract 1 for deleted character and add 1 for conversion to character position.
573 if (m_caretPosition
> -1 && !HasSelection())
575 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
576 m_caretPosition
, // Current caret position
577 m_caretPosition
-1, // New caret position
581 DeleteSelectedContent();
585 if (GetLastPosition() == -1)
589 m_caretPosition
= -1;
591 SetDefaultStyleToCursorStyle();
594 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
597 else if (event
.GetKeyCode() == WXK_DELETE
)
599 BeginBatchUndo(_("Delete Text"));
601 // Submit range in character positions, which are greater than caret positions,
602 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
604 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
605 m_caretPosition
, // Current caret position
606 m_caretPosition
+1, // New caret position
610 DeleteSelectedContent();
614 if (GetLastPosition() == -1)
618 m_caretPosition
= -1;
620 SetDefaultStyleToCursorStyle();
626 long keycode
= event
.GetKeyCode();
700 case WXK_NUMPAD_SPACE
:
702 case WXK_NUMPAD_ENTER
:
707 case WXK_NUMPAD_HOME
:
708 case WXK_NUMPAD_LEFT
:
710 case WXK_NUMPAD_RIGHT
:
711 case WXK_NUMPAD_DOWN
:
712 case WXK_NUMPAD_PAGEUP
:
713 case WXK_NUMPAD_PAGEDOWN
:
715 case WXK_NUMPAD_BEGIN
:
716 case WXK_NUMPAD_INSERT
:
717 case WXK_NUMPAD_DELETE
:
718 case WXK_NUMPAD_EQUAL
:
719 case WXK_NUMPAD_MULTIPLY
:
721 case WXK_NUMPAD_SEPARATOR
:
722 case WXK_NUMPAD_SUBTRACT
:
723 case WXK_NUMPAD_DECIMAL
:
731 if (event
.CmdDown() || event
.AltDown())
737 if (keycode
== wxT('\t'))
739 // See if we need to promote or demote the selection or paragraph at the cursor
740 // position, instead of inserting a tab.
741 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
742 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
743 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
745 wxRichTextRange range
;
747 range
= GetSelectionRange();
749 range
= para
->GetRange().FromInternal();
751 int promoteBy
= event
.ShiftDown() ? 1 : -1;
753 PromoteList(promoteBy
, range
, NULL
);
759 BeginBatchUndo(_("Insert Text"));
761 long newPos
= m_caretPosition
;
762 DeleteSelectedContent(& newPos
);
764 wxString str
= (wxChar
) event
.GetKeyCode();
765 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
769 SetDefaultStyleToCursorStyle();
770 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
777 /// Delete content if there is a selection, e.g. when pressing a key.
778 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
782 long pos
= m_selectionRange
.GetStart();
783 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
784 m_caretPosition
, // Current caret position
785 pos
, // New caret position
787 m_selectionRange
.SetRange(-2, -2);
797 /// Keyboard navigation
801 Left: left one character
802 Right: right one character
805 Ctrl-Left: left one word
806 Ctrl-Right: right one word
807 Ctrl-Up: previous paragraph start
808 Ctrl-Down: next start of paragraph
811 Ctrl-Home: start of document
812 Ctrl-End: end of document
814 Page-Down: Down a screen
818 Ctrl-Alt-PgUp: Start of window
819 Ctrl-Alt-PgDn: End of window
820 F8: Start selection mode
821 Esc: End selection mode
823 Adding Shift does the above but starts/extends selection.
828 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
830 bool success
= false;
832 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
834 if (flags
& wxRICHTEXT_CTRL_DOWN
)
835 success
= WordRight(1, flags
);
837 success
= MoveRight(1, flags
);
839 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
841 if (flags
& wxRICHTEXT_CTRL_DOWN
)
842 success
= WordLeft(1, flags
);
844 success
= MoveLeft(1, flags
);
846 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
848 if (flags
& wxRICHTEXT_CTRL_DOWN
)
849 success
= MoveToParagraphStart(flags
);
851 success
= MoveUp(1, flags
);
853 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
855 if (flags
& wxRICHTEXT_CTRL_DOWN
)
856 success
= MoveToParagraphEnd(flags
);
858 success
= MoveDown(1, flags
);
860 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
862 success
= PageUp(1, flags
);
864 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
866 success
= PageDown(1, flags
);
868 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
870 if (flags
& wxRICHTEXT_CTRL_DOWN
)
871 success
= MoveHome(flags
);
873 success
= MoveToLineStart(flags
);
875 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
877 if (flags
& wxRICHTEXT_CTRL_DOWN
)
878 success
= MoveEnd(flags
);
880 success
= MoveToLineEnd(flags
);
885 ScrollIntoView(m_caretPosition
, keyCode
);
886 SetDefaultStyleToCursorStyle();
892 /// Extend the selection. Selections are in caret positions.
893 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
895 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
897 // If not currently selecting, start selecting
898 if (m_selectionRange
.GetStart() == -2)
900 m_selectionAnchor
= oldPos
;
903 m_selectionRange
.SetRange(newPos
+1, oldPos
);
905 m_selectionRange
.SetRange(oldPos
+1, newPos
);
909 // Always ensure that the selection range start is greater than
911 if (newPos
> m_selectionAnchor
)
912 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
914 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
917 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
919 wxLogDebug(wxT("Strange selection range"));
928 /// Scroll into view, returning true if we scrolled.
929 /// This takes a _caret_ position.
930 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
932 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
938 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
940 int startXUnits
, startYUnits
;
941 GetViewStart(& startXUnits
, & startYUnits
);
942 int startY
= startYUnits
* ppuY
;
945 GetVirtualSize(& sx
, & sy
);
951 wxRect rect
= line
->GetRect();
953 bool scrolled
= false;
955 wxSize clientSize
= GetClientSize();
958 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
959 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
960 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
961 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
963 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
965 // Make it scroll so this item is at the bottom
967 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
968 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
970 // If we're still off the screen, scroll another line down
971 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
974 if (startYUnits
!= yUnits
)
976 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
980 else if (rect
.y
< startY
)
982 // Make it scroll so this item is at the top
985 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
987 if (startYUnits
!= yUnits
)
989 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
995 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
996 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
997 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
998 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1000 if (rect
.y
< startY
)
1002 // Make it scroll so this item is at the top
1005 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1007 if (startYUnits
!= yUnits
)
1009 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1013 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1015 // Make it scroll so this item is at the bottom
1017 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1018 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1020 // If we're still off the screen, scroll another line down
1021 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1024 if (startYUnits
!= yUnits
)
1026 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1036 /// Is the given position visible on the screen?
1037 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1039 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1045 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1048 GetViewStart(& startX
, & startY
);
1050 startY
= startY
* ppuY
;
1053 GetVirtualSize(& sx
, & sy
);
1058 wxRect rect
= line
->GetRect();
1060 wxSize clientSize
= GetClientSize();
1062 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1065 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1067 m_caretPosition
= position
;
1068 m_caretAtLineStart
= showAtLineStart
;
1071 /// Move caret one visual step forward: this may mean setting a flag
1072 /// and keeping the same position if we're going from the end of one line
1073 /// to the start of the next, which may be the exact same caret position.
1074 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1076 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1078 // Only do the check if we're not at the end of the paragraph (where things work OK
1080 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1082 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1086 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1088 // We're at the end of a line. See whether we need to
1089 // stay at the same actual caret position but change visual
1090 // position, or not.
1091 if (oldPosition
== lineRange
.GetEnd())
1093 if (m_caretAtLineStart
)
1095 // We're already at the start of the line, so actually move on now.
1096 m_caretPosition
= oldPosition
+ 1;
1097 m_caretAtLineStart
= false;
1101 // We're showing at the end of the line, so keep to
1102 // the same position but indicate that we're to show
1103 // at the start of the next line.
1104 m_caretPosition
= oldPosition
;
1105 m_caretAtLineStart
= true;
1107 SetDefaultStyleToCursorStyle();
1113 SetDefaultStyleToCursorStyle();
1116 /// Move caret one visual step backward: this may mean setting a flag
1117 /// and keeping the same position if we're going from the end of one line
1118 /// to the start of the next, which may be the exact same caret position.
1119 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1121 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1123 // Only do the check if we're not at the start of the paragraph (where things work OK
1125 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1127 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1131 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1133 // We're at the start of a line. See whether we need to
1134 // stay at the same actual caret position but change visual
1135 // position, or not.
1136 if (oldPosition
== lineRange
.GetStart())
1138 m_caretPosition
= oldPosition
-1;
1139 m_caretAtLineStart
= true;
1142 else if (oldPosition
== lineRange
.GetEnd())
1144 if (m_caretAtLineStart
)
1146 // We're at the start of the line, so keep the same caret position
1147 // but clear the start-of-line flag.
1148 m_caretPosition
= oldPosition
;
1149 m_caretAtLineStart
= false;
1153 // We're showing at the end of the line, so go back
1154 // to the previous character position.
1155 m_caretPosition
= oldPosition
- 1;
1157 SetDefaultStyleToCursorStyle();
1163 SetDefaultStyleToCursorStyle();
1167 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1169 long endPos
= GetBuffer().GetRange().GetEnd();
1171 if (m_caretPosition
+ noPositions
< endPos
)
1173 long oldPos
= m_caretPosition
;
1174 long newPos
= m_caretPosition
+ noPositions
;
1176 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1180 // Determine by looking at oldPos and m_caretPosition whether
1181 // we moved from the end of a line to the start of the next line, in which case
1182 // we want to adjust the caret position such that it is positioned at the
1183 // start of the next line, rather than jumping past the first character of the
1185 if (noPositions
== 1 && !extendSel
)
1186 MoveCaretForward(oldPos
);
1188 SetCaretPosition(newPos
);
1191 SetDefaultStyleToCursorStyle();
1202 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1206 if (m_caretPosition
> startPos
- noPositions
+ 1)
1208 long oldPos
= m_caretPosition
;
1209 long newPos
= m_caretPosition
- noPositions
;
1210 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1214 if (noPositions
== 1 && !extendSel
)
1215 MoveCaretBack(oldPos
);
1217 SetCaretPosition(newPos
);
1220 SetDefaultStyleToCursorStyle();
1231 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1233 return MoveDown(- noLines
, flags
);
1237 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1242 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1243 wxPoint pt
= GetCaret()->GetPosition();
1244 long newLine
= lineNumber
+ noLines
;
1246 if (lineNumber
!= -1)
1250 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1252 if (newLine
> lastLine
)
1262 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1265 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1271 wxClientDC
dc(this);
1273 dc
.SetFont(GetFont());
1275 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1277 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1279 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1280 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1281 // so we view the caret at the start of the line.
1282 bool caretLineStart
= false;
1283 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1285 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1286 wxRichTextRange lineRange
;
1288 lineRange
= thisLine
->GetAbsoluteRange();
1290 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1293 caretLineStart
= true;
1297 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1298 if (para
&& para
->GetRange().GetStart() == newPos
)
1303 long newSelEnd
= newPos
;
1305 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1309 SetCaretPosition(newPos
, caretLineStart
);
1311 SetDefaultStyleToCursorStyle();
1321 /// Move to the end of the paragraph
1322 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1324 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1327 long newPos
= para
->GetRange().GetEnd() - 1;
1328 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1332 SetCaretPosition(newPos
);
1334 SetDefaultStyleToCursorStyle();
1344 /// Move to the start of the paragraph
1345 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1347 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1350 long newPos
= para
->GetRange().GetStart() - 1;
1351 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1355 SetCaretPosition(newPos
);
1357 SetDefaultStyleToCursorStyle();
1367 /// Move to the end of the line
1368 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1370 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1374 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1375 long newPos
= lineRange
.GetEnd();
1376 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1380 SetCaretPosition(newPos
);
1382 SetDefaultStyleToCursorStyle();
1392 /// Move to the start of the line
1393 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1395 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1398 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1399 long newPos
= lineRange
.GetStart()-1;
1401 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1405 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1407 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1409 SetDefaultStyleToCursorStyle();
1419 /// Move to the start of the buffer
1420 bool wxRichTextCtrl::MoveHome(int flags
)
1422 if (m_caretPosition
!= -1)
1424 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1428 SetCaretPosition(-1);
1430 SetDefaultStyleToCursorStyle();
1440 /// Move to the end of the buffer
1441 bool wxRichTextCtrl::MoveEnd(int flags
)
1443 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1445 if (m_caretPosition
!= endPos
)
1447 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1451 SetCaretPosition(endPos
);
1453 SetDefaultStyleToCursorStyle();
1463 /// Move noPages pages up
1464 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1466 return PageDown(- noPages
, flags
);
1469 /// Move noPages pages down
1470 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1472 // Calculate which line occurs noPages * screen height further down.
1473 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1476 wxSize clientSize
= GetClientSize();
1477 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1479 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1482 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1483 long pos
= lineRange
.GetStart()-1;
1484 if (pos
!= m_caretPosition
)
1486 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1488 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1492 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1494 SetDefaultStyleToCursorStyle();
1506 // Finds the caret position for the next word
1507 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1509 long endPos
= GetBuffer().GetRange().GetEnd();
1513 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1515 // First skip current text to space
1516 while (i
< endPos
&& i
> -1)
1518 // i is in character, not caret positions
1519 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1520 if (text
!= wxT(" ") && !text
.empty())
1527 while (i
< endPos
&& i
> -1)
1529 // i is in character, not caret positions
1530 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1531 if (text
.empty()) // End of paragraph, or maybe an image
1532 return wxMax(-1, i
- 1);
1533 else if (text
== wxT(" ") || text
.empty())
1537 // Convert to caret position
1538 return wxMax(-1, i
- 1);
1547 long i
= m_caretPosition
;
1549 // First skip white space
1550 while (i
< endPos
&& i
> -1)
1552 // i is in character, not caret positions
1553 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1554 if (text
.empty()) // End of paragraph, or maybe an image
1556 else if (text
== wxT(" ") || text
.empty())
1561 // Next skip current text to space
1562 while (i
< endPos
&& i
> -1)
1564 // i is in character, not caret positions
1565 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1566 if (text
!= wxT(" ") /* && !text.empty() */)
1579 /// Move n words left
1580 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1582 long pos
= FindNextWordPosition(-1);
1583 if (pos
!= m_caretPosition
)
1585 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1587 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1591 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1593 SetDefaultStyleToCursorStyle();
1603 /// Move n words right
1604 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1606 long pos
= FindNextWordPosition(1);
1607 if (pos
!= m_caretPosition
)
1609 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1611 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1615 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1617 SetDefaultStyleToCursorStyle();
1628 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1630 // Only do sizing optimization for large buffers
1631 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1633 m_fullLayoutRequired
= true;
1634 m_fullLayoutTime
= wxGetLocalTimeMillis();
1635 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1636 LayoutContent(true /* onlyVisibleRect */);
1639 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1641 #if wxRICHTEXT_BUFFERED_PAINTING
1649 /// Idle-time processing
1650 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1652 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1654 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1656 m_fullLayoutRequired
= false;
1657 m_fullLayoutTime
= 0;
1658 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1659 ShowPosition(m_fullLayoutSavedPosition
);
1663 if (m_caretPositionForDefaultStyle
!= -2)
1665 // If the caret position has changed, no longer reflect the default style
1667 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1668 m_caretPositionForDefaultStyle
= -2;
1675 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1681 /// Set up scrollbars, e.g. after a resize
1682 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1687 if (GetBuffer().IsEmpty())
1689 SetScrollbars(0, 0, 0, 0, 0, 0);
1693 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1694 // of pixels. See e.g. wxVScrolledWindow for ideas.
1695 int pixelsPerUnit
= 5;
1696 wxSize clientSize
= GetClientSize();
1698 int maxHeight
= GetBuffer().GetCachedSize().y
;
1700 // Round up so we have at least maxHeight pixels
1701 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1703 int startX
= 0, startY
= 0;
1705 GetViewStart(& startX
, & startY
);
1707 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1708 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1710 // Move to previous scroll position if
1712 SetScrollbars(0, pixelsPerUnit
,
1714 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1717 /// Paint the background
1718 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1720 wxColour backgroundColour
= GetBackgroundColour();
1721 if (!backgroundColour
.Ok())
1722 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1724 // Clear the background
1725 dc
.SetBrush(wxBrush(backgroundColour
));
1726 dc
.SetPen(*wxTRANSPARENT_PEN
);
1727 wxRect
windowRect(GetClientSize());
1728 windowRect
.x
-= 2; windowRect
.y
-= 2;
1729 windowRect
.width
+= 4; windowRect
.height
+= 4;
1731 // We need to shift the rectangle to take into account
1732 // scrolling. Converting device to logical coordinates.
1733 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1734 dc
.DrawRectangle(windowRect
);
1737 #if wxRICHTEXT_BUFFERED_PAINTING
1738 /// Recreate buffer bitmap if necessary
1739 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1742 if (sz
== wxDefaultSize
)
1743 sz
= GetClientSize();
1745 if (sz
.x
< 1 || sz
.y
< 1)
1748 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1749 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1750 return m_bufferBitmap
.Ok();
1754 // ----------------------------------------------------------------------------
1755 // file IO functions
1756 // ----------------------------------------------------------------------------
1758 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1760 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1762 m_filename
= filename
;
1765 SetInsertionPoint(0);
1768 SetupScrollbars(true);
1770 SendTextUpdatedEvent();
1776 wxLogError(_("File couldn't be loaded."));
1782 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1784 if (GetBuffer().SaveFile(filename
, fileType
))
1786 m_filename
= filename
;
1793 wxLogError(_("The text couldn't be saved."));
1798 // ----------------------------------------------------------------------------
1799 // wxRichTextCtrl specific functionality
1800 // ----------------------------------------------------------------------------
1802 /// Add a new paragraph of text to the end of the buffer
1803 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1805 return GetBuffer().AddParagraph(text
);
1809 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1811 return GetBuffer().AddImage(image
);
1814 // ----------------------------------------------------------------------------
1815 // selection and ranges
1816 // ----------------------------------------------------------------------------
1818 void wxRichTextCtrl::SelectAll()
1820 SetSelection(0, GetLastPosition()+1);
1821 m_selectionAnchor
= -1;
1825 void wxRichTextCtrl::SelectNone()
1827 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1828 SetSelection(-2, -2);
1829 m_selectionAnchor
= -2;
1832 static bool wxIsWordDelimiter(const wxString
& text
)
1834 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1837 /// Select the word at the given character position
1838 bool wxRichTextCtrl::SelectWord(long position
)
1840 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1843 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1847 long positionStart
= position
;
1848 long positionEnd
= position
;
1850 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1852 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1853 if (wxIsWordDelimiter(text
))
1859 if (positionStart
< para
->GetRange().GetStart())
1860 positionStart
= para
->GetRange().GetStart();
1862 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1864 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1865 if (wxIsWordDelimiter(text
))
1871 if (positionEnd
>= para
->GetRange().GetEnd())
1872 positionEnd
= para
->GetRange().GetEnd();
1874 SetSelection(positionStart
, positionEnd
+1);
1876 if (positionStart
>= 0)
1878 MoveCaret(positionStart
-1, true);
1879 SetDefaultStyleToCursorStyle();
1885 wxString
wxRichTextCtrl::GetStringSelection() const
1888 GetSelection(&from
, &to
);
1890 return GetRange(from
, to
);
1893 // ----------------------------------------------------------------------------
1895 // ----------------------------------------------------------------------------
1897 wxTextCtrlHitTestResult
1898 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1900 // implement in terms of the other overload as the native ports typically
1901 // can get the position and not (x, y) pair directly (although wxUniv
1902 // directly gets x and y -- and so overrides this method as well)
1904 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1906 if ( rc
!= wxTE_HT_UNKNOWN
)
1908 PositionToXY(pos
, x
, y
);
1914 wxTextCtrlHitTestResult
1915 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1918 wxClientDC
dc((wxRichTextCtrl
*) this);
1919 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1921 // Buffer uses logical position (relative to start of buffer)
1923 wxPoint pt2
= GetLogicalPoint(pt
);
1925 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1929 case wxRICHTEXT_HITTEST_BEFORE
:
1930 return wxTE_HT_BEFORE
;
1932 case wxRICHTEXT_HITTEST_AFTER
:
1933 return wxTE_HT_BEYOND
;
1935 case wxRICHTEXT_HITTEST_ON
:
1936 return wxTE_HT_ON_TEXT
;
1939 return wxTE_HT_UNKNOWN
;
1942 // ----------------------------------------------------------------------------
1943 // set/get the controls text
1944 // ----------------------------------------------------------------------------
1946 wxString
wxRichTextCtrl::GetValue() const
1948 return GetBuffer().GetText();
1951 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1953 // Public API for range is different from internals
1954 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1957 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1961 // if the text is long enough, it's faster to just set it instead of first
1962 // comparing it with the old one (chances are that it will be different
1963 // anyhow, this comparison is there to avoid flicker for small single-line
1964 // edit controls mostly)
1965 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1969 // for compatibility, don't move the cursor when doing SetValue()
1970 SetInsertionPoint(0);
1974 if ( flags
& SetValue_SendEvent
)
1976 // still send an event for consistency
1977 SendTextUpdatedEvent();
1981 // we should reset the modified flag even if the value didn't really change
1983 // mark the control as being not dirty - we changed its text, not the
1988 void wxRichTextCtrl::WriteText(const wxString
& value
)
1993 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
1995 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1997 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
1999 if ( flags
& SetValue_SendEvent
)
2000 SendTextUpdatedEvent();
2003 void wxRichTextCtrl::AppendText(const wxString
& text
)
2005 SetInsertionPointEnd();
2010 /// Write an image at the current insertion point
2011 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2013 wxRichTextImageBlock imageBlock
;
2015 wxImage image2
= image
;
2016 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2017 return WriteImage(imageBlock
);
2022 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2024 wxRichTextImageBlock imageBlock
;
2027 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2028 return WriteImage(imageBlock
);
2033 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2035 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2038 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2042 wxRichTextImageBlock imageBlock
;
2044 wxImage image
= bitmap
.ConvertToImage();
2045 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2046 return WriteImage(imageBlock
);
2052 /// Insert a newline (actually paragraph) at the current insertion point.
2053 bool wxRichTextCtrl::Newline()
2055 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2059 // ----------------------------------------------------------------------------
2060 // Clipboard operations
2061 // ----------------------------------------------------------------------------
2063 void wxRichTextCtrl::Copy()
2067 wxRichTextRange range
= GetInternalSelectionRange();
2068 GetBuffer().CopyToClipboard(range
);
2072 void wxRichTextCtrl::Cut()
2076 wxRichTextRange range
= GetInternalSelectionRange();
2077 GetBuffer().CopyToClipboard(range
);
2079 DeleteSelectedContent();
2085 void wxRichTextCtrl::Paste()
2089 BeginBatchUndo(_("Paste"));
2091 long newPos
= m_caretPosition
;
2092 DeleteSelectedContent(& newPos
);
2094 GetBuffer().PasteFromClipboard(newPos
);
2100 void wxRichTextCtrl::DeleteSelection()
2102 if (CanDeleteSelection())
2104 DeleteSelectedContent();
2108 bool wxRichTextCtrl::HasSelection() const
2110 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2113 bool wxRichTextCtrl::CanCopy() const
2115 // Can copy if there's a selection
2116 return HasSelection();
2119 bool wxRichTextCtrl::CanCut() const
2121 return HasSelection() && IsEditable();
2124 bool wxRichTextCtrl::CanPaste() const
2126 if ( !IsEditable() )
2129 return GetBuffer().CanPasteFromClipboard();
2132 bool wxRichTextCtrl::CanDeleteSelection() const
2134 return HasSelection() && IsEditable();
2138 // ----------------------------------------------------------------------------
2140 // ----------------------------------------------------------------------------
2142 void wxRichTextCtrl::SetEditable(bool editable
)
2144 m_editable
= editable
;
2147 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2151 m_caretPosition
= pos
- 1;
2154 void wxRichTextCtrl::SetInsertionPointEnd()
2156 long pos
= GetLastPosition();
2157 SetInsertionPoint(pos
);
2160 long wxRichTextCtrl::GetInsertionPoint() const
2162 return m_caretPosition
+1;
2165 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2167 return GetBuffer().GetRange().GetEnd();
2170 // If the return values from and to are the same, there is no
2172 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2174 *from
= m_selectionRange
.GetStart();
2175 *to
= m_selectionRange
.GetEnd();
2176 if ((*to
) != -1 && (*to
) != -2)
2180 bool wxRichTextCtrl::IsEditable() const
2185 // ----------------------------------------------------------------------------
2187 // ----------------------------------------------------------------------------
2189 void wxRichTextCtrl::SetSelection(long from
, long to
)
2191 // if from and to are both -1, it means (in wxWidgets) that all text should
2193 if ( (from
== -1) && (to
== -1) )
2196 to
= GetLastPosition()+1;
2199 DoSetSelection(from
, to
);
2202 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2204 m_selectionAnchor
= from
;
2205 m_selectionRange
.SetRange(from
, to
-1);
2211 // ----------------------------------------------------------------------------
2213 // ----------------------------------------------------------------------------
2215 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2216 const wxString
& value
)
2218 BeginBatchUndo(_("Replace"));
2220 DeleteSelectedContent();
2222 DoWriteText(value
, SetValue_SelectionOnly
);
2227 void wxRichTextCtrl::Remove(long from
, long to
)
2231 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2232 m_caretPosition
, // Current caret position
2233 from
, // New caret position
2241 bool wxRichTextCtrl::IsModified() const
2243 return m_buffer
.IsModified();
2246 void wxRichTextCtrl::MarkDirty()
2248 m_buffer
.Modify(true);
2251 void wxRichTextCtrl::DiscardEdits()
2253 m_caretPositionForDefaultStyle
= -2;
2254 m_buffer
.Modify(false);
2255 m_buffer
.GetCommandProcessor()->ClearCommands();
2258 int wxRichTextCtrl::GetNumberOfLines() const
2260 return GetBuffer().GetParagraphCount();
2263 // ----------------------------------------------------------------------------
2264 // Positions <-> coords
2265 // ----------------------------------------------------------------------------
2267 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2269 return GetBuffer().XYToPosition(x
, y
);
2272 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2274 return GetBuffer().PositionToXY(pos
, x
, y
);
2277 // ----------------------------------------------------------------------------
2279 // ----------------------------------------------------------------------------
2281 void wxRichTextCtrl::ShowPosition(long pos
)
2283 if (!IsPositionVisible(pos
))
2284 ScrollIntoView(pos
-1, WXK_DOWN
);
2287 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2289 return GetBuffer().GetParagraphLength(lineNo
);
2292 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2294 return GetBuffer().GetParagraphText(lineNo
);
2297 // ----------------------------------------------------------------------------
2299 // ----------------------------------------------------------------------------
2301 void wxRichTextCtrl::Undo()
2305 GetCommandProcessor()->Undo();
2309 void wxRichTextCtrl::Redo()
2313 GetCommandProcessor()->Redo();
2317 bool wxRichTextCtrl::CanUndo() const
2319 return GetCommandProcessor()->CanUndo();
2322 bool wxRichTextCtrl::CanRedo() const
2324 return GetCommandProcessor()->CanRedo();
2327 // ----------------------------------------------------------------------------
2328 // implementation details
2329 // ----------------------------------------------------------------------------
2331 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2333 SetValue(event
.GetString());
2334 GetEventHandler()->ProcessEvent(event
);
2337 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2339 // By default, load the first file into the text window.
2340 if (event
.GetNumberOfFiles() > 0)
2342 LoadFile(event
.GetFiles()[0]);
2346 wxSize
wxRichTextCtrl::DoGetBestSize() const
2348 return wxSize(10, 10);
2351 // ----------------------------------------------------------------------------
2352 // standard handlers for standard edit menu events
2353 // ----------------------------------------------------------------------------
2355 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2360 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2365 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2370 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2375 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2380 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2385 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2387 event
.Enable( CanCut() );
2390 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2392 event
.Enable( CanCopy() );
2395 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2397 event
.Enable( CanDeleteSelection() );
2400 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2402 event
.Enable( CanPaste() );
2405 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2407 event
.Enable( CanUndo() );
2408 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2411 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2413 event
.Enable( CanRedo() );
2414 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2417 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2422 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2424 event
.Enable(GetLastPosition() > 0);
2427 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2431 m_contextMenu
= new wxMenu
;
2432 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2433 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2434 m_contextMenu
->AppendSeparator();
2435 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2436 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2437 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2438 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2439 m_contextMenu
->AppendSeparator();
2440 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2442 PopupMenu(m_contextMenu
);
2446 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2448 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2451 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2453 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2456 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2458 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2461 // extended style setting operation with flags including:
2462 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2463 // see richtextbuffer.h for more details.
2464 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2466 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2469 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2471 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2474 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2476 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2479 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2481 return GetBuffer().SetDefaultStyle(style
);
2484 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2486 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2489 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2491 return GetBuffer().GetDefaultStyle();
2494 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2496 return GetBuffer().GetDefaultStyle();
2499 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2501 wxTextAttrEx
attr(style
);
2502 if (GetBuffer().GetStyle(position
, attr
))
2511 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2513 return GetBuffer().GetStyle(position
, style
);
2516 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2518 return GetBuffer().GetStyle(position
, style
);
2521 /// Get the content (uncombined) attributes for this position.
2523 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2525 wxTextAttrEx
attr(style
);
2526 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2535 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2537 return GetBuffer().GetUncombinedStyle(position
, style
);
2540 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2542 return GetBuffer().GetUncombinedStyle(position
, style
);
2545 /// Set font, and also the buffer attributes
2546 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2548 wxControl::SetFont(font
);
2550 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2552 GetBuffer().SetBasicStyle(attr
);
2554 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2560 /// Transform logical to physical
2561 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2564 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2569 /// Transform physical to logical
2570 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2573 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2578 /// Position the caret
2579 void wxRichTextCtrl::PositionCaret()
2584 //wxLogDebug(wxT("PositionCaret"));
2587 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2589 wxPoint originalPt
= caretRect
.GetPosition();
2590 wxPoint pt
= GetPhysicalPoint(originalPt
);
2591 if (GetCaret()->GetPosition() != pt
)
2593 GetCaret()->Move(pt
);
2594 GetCaret()->SetSize(caretRect
.GetSize());
2599 /// Get the caret height and position for the given character position
2600 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2602 wxClientDC
dc(this);
2603 dc
.SetFont(GetFont());
2610 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2612 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2619 /// Gets the line for the visible caret position. If the caret is
2620 /// shown at the very end of the line, it means the next character is actually
2621 /// on the following line. So let's get the line we're expecting to find
2622 /// if this is the case.
2623 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2625 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2626 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2629 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2630 if (caretPosition
== lineRange
.GetStart()-1 &&
2631 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2633 if (!m_caretAtLineStart
)
2634 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2641 /// Move the caret to the given character position
2642 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2644 if (GetBuffer().GetDirty())
2647 if (pos
<= GetBuffer().GetRange().GetEnd())
2649 SetCaretPosition(pos
, showAtLineStart
);
2659 /// Layout the buffer: which we must do before certain operations, such as
2660 /// setting the caret position.
2661 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2663 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2665 wxRect
availableSpace(GetClientSize());
2666 if (availableSpace
.width
== 0)
2667 availableSpace
.width
= 10;
2668 if (availableSpace
.height
== 0)
2669 availableSpace
.height
= 10;
2671 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2672 if (onlyVisibleRect
)
2674 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2675 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2678 wxClientDC
dc(this);
2679 dc
.SetFont(GetFont());
2683 GetBuffer().Defragment();
2684 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2685 GetBuffer().Layout(dc
, availableSpace
, flags
);
2686 GetBuffer().SetDirty(false);
2695 /// Is all of the selection bold?
2696 bool wxRichTextCtrl::IsSelectionBold()
2700 wxRichTextAttr attr
;
2701 wxRichTextRange range
= GetInternalSelectionRange();
2702 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2703 attr
.SetFontWeight(wxBOLD
);
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_WEIGHT
);
2714 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2715 if (GetStyle(pos
, attr
))
2717 if (IsDefaultStyleShowing())
2718 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2719 return attr
.GetFontWeight() == wxBOLD
;
2725 /// Is all of the selection italics?
2726 bool wxRichTextCtrl::IsSelectionItalics()
2730 wxRichTextRange range
= GetInternalSelectionRange();
2731 wxRichTextAttr attr
;
2732 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2733 attr
.SetFontStyle(wxITALIC
);
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_ITALIC
);
2744 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2745 if (GetStyle(pos
, attr
))
2747 if (IsDefaultStyleShowing())
2748 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2749 return attr
.GetFontStyle() == wxITALIC
;
2755 /// Is all of the selection underlined?
2756 bool wxRichTextCtrl::IsSelectionUnderlined()
2760 wxRichTextRange range
= GetInternalSelectionRange();
2761 wxRichTextAttr attr
;
2762 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2763 attr
.SetFontUnderlined(true);
2765 return HasCharacterAttributes(range
, attr
);
2769 // If no selection, then we need to combine current style with default style
2770 // to see what the effect would be if we started typing.
2771 wxRichTextAttr attr
;
2772 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2773 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2775 if (GetStyle(pos
, attr
))
2777 if (IsDefaultStyleShowing())
2778 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2779 return attr
.GetFontUnderlined();
2785 /// Apply bold to the selection
2786 bool wxRichTextCtrl::ApplyBoldToSelection()
2788 wxRichTextAttr attr
;
2789 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2790 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2793 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2795 SetAndShowDefaultStyle(attr
);
2799 /// Apply italic to the selection
2800 bool wxRichTextCtrl::ApplyItalicToSelection()
2802 wxRichTextAttr attr
;
2803 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2804 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2807 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2809 SetAndShowDefaultStyle(attr
);
2813 /// Apply underline to the selection
2814 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2816 wxRichTextAttr attr
;
2817 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2818 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2821 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2823 SetAndShowDefaultStyle(attr
);
2827 /// Is all of the selection aligned according to the specified flag?
2828 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2830 wxRichTextRange range
;
2832 range
= GetInternalSelectionRange();
2834 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2836 wxRichTextAttr attr
;
2837 attr
.SetAlignment(alignment
);
2839 return HasParagraphAttributes(range
, attr
);
2842 /// Apply alignment to the selection
2843 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2845 wxRichTextAttr attr
;
2846 attr
.SetAlignment(alignment
);
2848 return SetStyle(GetSelectionRange(), attr
);
2851 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2853 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2858 /// Apply a named style to the selection
2859 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2861 // Flags are defined within each definition, so only certain
2862 // attributes are applied.
2863 wxRichTextAttr
attr(def
->GetStyle());
2865 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2867 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2869 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2871 wxRichTextRange range
;
2874 range
= GetSelectionRange();
2877 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2878 range
= wxRichTextRange(pos
, pos
+1);
2881 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2884 // Make sure the attr has the style name
2885 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2887 attr
.SetParagraphStyleName(def
->GetName());
2889 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2890 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2891 // to change its style independently.
2892 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2895 attr
.SetCharacterStyleName(def
->GetName());
2898 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2901 SetAndShowDefaultStyle(attr
);
2906 /// Apply the style sheet to the buffer, for example if the styles have changed.
2907 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2910 styleSheet
= GetBuffer().GetStyleSheet();
2914 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2916 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2924 /// Sets the default style to the style under the cursor
2925 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2928 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2930 // If at the start of a paragraph, use the next position.
2931 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2933 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2934 if (GetUncombinedStyle(pos
, attr
))
2936 if (GetStyle(pos
, attr
))
2939 SetDefaultStyle(attr
);
2946 /// Returns the first visible position in the current view
2947 long wxRichTextCtrl::GetFirstVisiblePosition() const
2949 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2951 return line
->GetAbsoluteRange().GetStart();
2956 /// Get the first visible point in the window
2957 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
2960 int startXUnits
, startYUnits
;
2962 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
2963 GetViewStart(& startXUnits
, & startYUnits
);
2965 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
2968 /// The adjusted caret position is the character position adjusted to take
2969 /// into account whether we're at the start of a paragraph, in which case
2970 /// style information should be taken from the next position, not current one.
2971 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2973 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2975 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2980 /// Get/set the selection range in character positions. -1, -1 means no selection.
2981 /// The range is in API convention, i.e. a single character selection is denoted
2983 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2985 wxRichTextRange range
= GetInternalSelectionRange();
2986 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2987 range
.SetEnd(range
.GetEnd() + 1);
2991 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2993 wxRichTextRange
range1(range
);
2994 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2995 range1
.SetEnd(range1
.GetEnd() - 1);
2997 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2999 SetInternalSelectionRange(range1
);
3003 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3005 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3008 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3010 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3013 /// Clear list for given range
3014 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3016 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
3019 /// Number/renumber any list elements in the given range
3020 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3022 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3025 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3027 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3030 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3031 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3033 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3036 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3038 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3041 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3043 if (sm_availableFontNames
.GetCount() == 0)
3045 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3046 sm_availableFontNames
.Sort();
3048 return sm_availableFontNames
;
3051 void wxRichTextCtrl::ClearAvailableFontNames()
3053 sm_availableFontNames
.Clear();