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 if (style
& wxTE_READONLY
)
137 wxTextAttrEx attributes
;
138 attributes
.SetFont(GetFont());
139 attributes
.SetTextColour(*wxBLACK
);
140 attributes
.SetBackgroundColour(*wxWHITE
);
141 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
142 attributes
.SetLineSpacing(10);
143 attributes
.SetParagraphSpacingAfter(10);
144 attributes
.SetParagraphSpacingBefore(0);
146 SetBasicStyle(attributes
);
148 // The default attributes will be merged with base attributes, so
149 // can be empty to begin with
150 wxTextAttrEx defaultAttributes
;
151 SetDefaultStyle(defaultAttributes
);
153 SetBackgroundColour(*wxWHITE
);
154 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
156 // Tell the sizers to use the given or best size
157 SetBestFittingSize(size
);
159 #if wxRICHTEXT_BUFFERED_PAINTING
161 RecreateBuffer(size
);
164 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
165 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
167 SetCursor(m_textCursor
);
169 if (!value
.IsEmpty())
172 GetBuffer().AddEventHandler(this);
177 wxRichTextCtrl::~wxRichTextCtrl()
179 GetBuffer().RemoveEventHandler(this);
181 delete m_contextMenu
;
184 /// Member initialisation
185 void wxRichTextCtrl::Init()
188 m_contextMenu
= NULL
;
190 m_caretPosition
= -1;
191 m_selectionRange
.SetRange(-2, -2);
192 m_selectionAnchor
= -2;
194 m_caretAtLineStart
= false;
196 m_fullLayoutRequired
= false;
197 m_fullLayoutTime
= 0;
198 m_fullLayoutSavedPosition
= 0;
199 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
200 m_caretPositionForDefaultStyle
= -2;
203 /// Call Freeze to prevent refresh
204 void wxRichTextCtrl::Freeze()
209 /// Call Thaw to refresh
210 void wxRichTextCtrl::Thaw()
214 if (m_freezeCount
== 0)
222 void wxRichTextCtrl::Clear()
224 m_buffer
.ResetAndClearCommands();
225 m_buffer
.SetDirty(true);
226 m_caretPosition
= -1;
227 m_caretPositionForDefaultStyle
= -2;
228 m_caretAtLineStart
= false;
229 m_selectionRange
.SetRange(-2, -2);
231 SetScrollbars(0, 0, 0, 0, 0, 0);
233 if (m_freezeCount
== 0)
238 SendTextUpdatedEvent();
242 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
248 #if wxRICHTEXT_BUFFERED_PAINTING
249 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
255 if (m_freezeCount
> 0)
258 dc
.SetFont(GetFont());
260 // Paint the background
263 wxRect
drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
264 wxRect
availableSpace(GetClientSize());
265 if (GetBuffer().GetDirty())
267 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
268 GetBuffer().SetDirty(false);
272 GetBuffer().Draw(dc
, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea
, 0 /* descent */, 0 /* flags */);
281 // Empty implementation, to prevent flicker
282 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
286 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
288 wxCaret
* caret
= new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16);
297 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
306 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
312 dc
.SetFont(GetFont());
315 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
);
317 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
319 m_dragStart
= event
.GetLogicalPosition(dc
);
325 bool caretAtLineStart
= false;
327 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
329 // If we're at the start of a line (but not first in para)
330 // then we should keep the caret showing at the start of the line
331 // by showing the m_caretAtLineStart flag.
332 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
333 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
335 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
336 caretAtLineStart
= true;
340 MoveCaret(position
, caretAtLineStart
);
341 SetDefaultStyleToCursorStyle();
348 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
353 if (GetCapture() == this)
356 // See if we clicked on a URL
359 dc
.SetFont(GetFont());
362 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
363 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
365 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
368 if (GetStyle(position
, attr
))
370 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
372 wxString urlTarget
= attr
.GetURL();
373 if (!urlTarget
.IsEmpty())
375 wxMouseEvent
mouseEvent(event
);
377 long startPos
= 0, endPos
= 0;
378 wxRichTextObject
* obj
= GetBuffer().GetLeafObjectAtPosition(position
);
381 startPos
= obj
->GetRange().GetStart();
382 endPos
= obj
->GetRange().GetEnd();
385 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
386 InitCommandEvent(urlEvent
);
388 urlEvent
.SetString(urlTarget
);
390 GetEventHandler()->ProcessEvent(urlEvent
);
399 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
403 dc
.SetFont(GetFont());
406 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
407 int hit
= GetBuffer().HitTest(dc
, logicalPt
, position
);
409 // See if we need to change the cursor
412 if (hit
!= wxRICHTEXT_HITTEST_NONE
)
415 if (GetStyle(position
, attr
))
417 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
419 if (GetCursor() != m_urlCursor
)
420 SetCursor(m_urlCursor
);
422 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
424 if (GetCursor() != m_textCursor
)
425 SetCursor(m_textCursor
);
431 if (!event
.Dragging())
437 if (m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
)
439 // TODO: test closeness
441 bool caretAtLineStart
= false;
443 if (hit
& wxRICHTEXT_HITTEST_BEFORE
)
445 // If we're at the start of a line (but not first in para)
446 // then we should keep the caret showing at the start of the line
447 // by showing the m_caretAtLineStart flag.
448 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
449 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(position
);
451 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
452 caretAtLineStart
= true;
456 if (m_caretPosition
!= position
)
458 bool extendSel
= ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
460 MoveCaret(position
, caretAtLineStart
);
461 SetDefaultStyleToCursorStyle();
470 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
476 /// Left-double-click
477 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& event
)
479 SelectWord(GetCaretPosition()+1);
484 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
490 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
494 flags
|= wxRICHTEXT_CTRL_DOWN
;
495 if (event
.ShiftDown())
496 flags
|= wxRICHTEXT_SHIFT_DOWN
;
498 flags
|= wxRICHTEXT_ALT_DOWN
;
500 if (event
.GetKeyCode() == WXK_LEFT
||
501 event
.GetKeyCode() == WXK_RIGHT
||
502 event
.GetKeyCode() == WXK_UP
||
503 event
.GetKeyCode() == WXK_DOWN
||
504 event
.GetKeyCode() == WXK_HOME
||
505 event
.GetKeyCode() == WXK_PAGEUP
||
506 event
.GetKeyCode() == WXK_PAGEDOWN
||
507 event
.GetKeyCode() == WXK_END
||
509 event
.GetKeyCode() == WXK_NUMPAD_LEFT
||
510 event
.GetKeyCode() == WXK_NUMPAD_RIGHT
||
511 event
.GetKeyCode() == WXK_NUMPAD_UP
||
512 event
.GetKeyCode() == WXK_NUMPAD_DOWN
||
513 event
.GetKeyCode() == WXK_NUMPAD_HOME
||
514 event
.GetKeyCode() == WXK_NUMPAD_PAGEUP
||
515 event
.GetKeyCode() == WXK_NUMPAD_PAGEDOWN
||
516 event
.GetKeyCode() == WXK_NUMPAD_END
)
518 KeyboardNavigate(event
.GetKeyCode(), flags
);
522 // all the other keys modify the controls contents which shouldn't be
523 // possible if we're read-only
530 if (event
.GetKeyCode() == WXK_RETURN
)
532 BeginBatchUndo(_("Insert Text"));
534 long newPos
= m_caretPosition
;
536 DeleteSelectedContent(& newPos
);
538 GetBuffer().InsertNewlineWithUndo(newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
540 SetDefaultStyleToCursorStyle();
542 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
544 wxRichTextEvent
cmdEvent(
545 wxEVT_COMMAND_RICHTEXT_RETURN
,
547 cmdEvent
.SetEventObject(this);
548 cmdEvent
.SetFlags(flags
);
549 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
551 // Generate conventional event
552 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
553 InitCommandEvent(textEvent
);
555 GetEventHandler()->ProcessEvent(textEvent
);
559 else if (event
.GetKeyCode() == WXK_BACK
)
561 BeginBatchUndo(_("Delete Text"));
563 // Submit range in character positions, which are greater than caret positions,
564 // so subtract 1 for deleted character and add 1 for conversion to character position.
565 if (m_caretPosition
> -1 && !HasSelection())
567 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
, m_caretPosition
),
568 m_caretPosition
, // Current caret position
569 m_caretPosition
-1, // New caret position
573 DeleteSelectedContent();
577 if (GetLastPosition() == -1)
581 m_caretPosition
= -1;
583 SetDefaultStyleToCursorStyle();
586 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
589 else if (event
.GetKeyCode() == WXK_DELETE
)
591 BeginBatchUndo(_("Delete Text"));
593 // Submit range in character positions, which are greater than caret positions,
594 if (m_caretPosition
< GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
596 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition
+1, m_caretPosition
+1),
597 m_caretPosition
, // Current caret position
598 m_caretPosition
+1, // New caret position
602 DeleteSelectedContent();
606 if (GetLastPosition() == -1)
610 m_caretPosition
= -1;
612 SetDefaultStyleToCursorStyle();
618 long keycode
= event
.GetKeyCode();
692 case WXK_NUMPAD_SPACE
:
694 case WXK_NUMPAD_ENTER
:
699 case WXK_NUMPAD_HOME
:
700 case WXK_NUMPAD_LEFT
:
702 case WXK_NUMPAD_RIGHT
:
703 case WXK_NUMPAD_DOWN
:
704 case WXK_NUMPAD_PAGEUP
:
705 case WXK_NUMPAD_PAGEDOWN
:
707 case WXK_NUMPAD_BEGIN
:
708 case WXK_NUMPAD_INSERT
:
709 case WXK_NUMPAD_DELETE
:
710 case WXK_NUMPAD_EQUAL
:
711 case WXK_NUMPAD_MULTIPLY
:
713 case WXK_NUMPAD_SEPARATOR
:
714 case WXK_NUMPAD_SUBTRACT
:
715 case WXK_NUMPAD_DECIMAL
:
723 if (event
.CmdDown() || event
.AltDown())
729 if (keycode
== wxT('\t'))
731 // See if we need to promote or demote the selection or paragraph at the cursor
732 // position, instead of inserting a tab.
733 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
734 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
);
735 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
737 wxRichTextRange range
;
739 range
= GetSelectionRange();
741 range
= para
->GetRange().FromInternal();
743 int promoteBy
= event
.ShiftDown() ? 1 : -1;
745 PromoteList(promoteBy
, range
, NULL
);
751 BeginBatchUndo(_("Insert Text"));
753 long newPos
= m_caretPosition
;
754 DeleteSelectedContent(& newPos
);
756 wxString str
= (wxChar
) event
.GetKeyCode();
757 GetBuffer().InsertTextWithUndo(newPos
+1, str
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
761 SetDefaultStyleToCursorStyle();
762 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
769 /// Delete content if there is a selection, e.g. when pressing a key.
770 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
774 long pos
= m_selectionRange
.GetStart();
775 GetBuffer().DeleteRangeWithUndo(m_selectionRange
,
776 m_caretPosition
, // Current caret position
777 pos
, // New caret position
779 m_selectionRange
.SetRange(-2, -2);
789 /// Keyboard navigation
793 Left: left one character
794 Right: right one character
797 Ctrl-Left: left one word
798 Ctrl-Right: right one word
799 Ctrl-Up: previous paragraph start
800 Ctrl-Down: next start of paragraph
803 Ctrl-Home: start of document
804 Ctrl-End: end of document
806 Page-Down: Down a screen
810 Ctrl-Alt-PgUp: Start of window
811 Ctrl-Alt-PgDn: End of window
812 F8: Start selection mode
813 Esc: End selection mode
815 Adding Shift does the above but starts/extends selection.
820 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
822 bool success
= false;
824 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
826 if (flags
& wxRICHTEXT_CTRL_DOWN
)
827 success
= WordRight(1, flags
);
829 success
= MoveRight(1, flags
);
831 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
833 if (flags
& wxRICHTEXT_CTRL_DOWN
)
834 success
= WordLeft(1, flags
);
836 success
= MoveLeft(1, flags
);
838 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
840 if (flags
& wxRICHTEXT_CTRL_DOWN
)
841 success
= MoveToParagraphStart(flags
);
843 success
= MoveUp(1, flags
);
845 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
847 if (flags
& wxRICHTEXT_CTRL_DOWN
)
848 success
= MoveToParagraphEnd(flags
);
850 success
= MoveDown(1, flags
);
852 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
854 success
= PageUp(1, flags
);
856 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
858 success
= PageDown(1, flags
);
860 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
862 if (flags
& wxRICHTEXT_CTRL_DOWN
)
863 success
= MoveHome(flags
);
865 success
= MoveToLineStart(flags
);
867 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
869 if (flags
& wxRICHTEXT_CTRL_DOWN
)
870 success
= MoveEnd(flags
);
872 success
= MoveToLineEnd(flags
);
877 ScrollIntoView(m_caretPosition
, keyCode
);
878 SetDefaultStyleToCursorStyle();
884 /// Extend the selection. Selections are in caret positions.
885 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
887 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
889 // If not currently selecting, start selecting
890 if (m_selectionRange
.GetStart() == -2)
892 m_selectionAnchor
= oldPos
;
895 m_selectionRange
.SetRange(newPos
+1, oldPos
);
897 m_selectionRange
.SetRange(oldPos
+1, newPos
);
901 // Always ensure that the selection range start is greater than
903 if (newPos
> m_selectionAnchor
)
904 m_selectionRange
.SetRange(m_selectionAnchor
+1, newPos
);
906 m_selectionRange
.SetRange(newPos
+1, m_selectionAnchor
);
909 if (m_selectionRange
.GetStart() > m_selectionRange
.GetEnd())
911 wxLogDebug(wxT("Strange selection range"));
920 /// Scroll into view, returning true if we scrolled.
921 /// This takes a _caret_ position.
922 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
924 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
930 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
932 int startXUnits
, startYUnits
;
933 GetViewStart(& startXUnits
, & startYUnits
);
934 int startY
= startYUnits
* ppuY
;
937 GetVirtualSize(& sx
, & sy
);
943 wxRect rect
= line
->GetRect();
945 bool scrolled
= false;
947 wxSize clientSize
= GetClientSize();
950 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
951 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_DOWN
||
952 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
953 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
955 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
957 // Make it scroll so this item is at the bottom
959 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
960 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
962 // If we're still off the screen, scroll another line down
963 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
966 if (startYUnits
!= yUnits
)
968 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
972 else if (rect
.y
< startY
)
974 // Make it scroll so this item is at the top
977 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
979 if (startYUnits
!= yUnits
)
981 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
987 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
988 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
989 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
990 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
994 // Make it scroll so this item is at the top
997 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
999 if (startYUnits
!= yUnits
)
1001 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1005 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1007 // Make it scroll so this item is at the bottom
1009 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1010 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1012 // If we're still off the screen, scroll another line down
1013 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1016 if (startYUnits
!= yUnits
)
1018 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1028 /// Is the given position visible on the screen?
1029 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1031 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1037 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1040 GetViewStart(& startX
, & startY
);
1042 startY
= startY
* ppuY
;
1045 GetVirtualSize(& sx
, & sy
);
1050 wxRect rect
= line
->GetRect();
1052 wxSize clientSize
= GetClientSize();
1054 return !(((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
)) || rect
.y
< startY
);
1057 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1059 m_caretPosition
= position
;
1060 m_caretAtLineStart
= showAtLineStart
;
1063 /// Move caret one visual step forward: this may mean setting a flag
1064 /// and keeping the same position if we're going from the end of one line
1065 /// to the start of the next, which may be the exact same caret position.
1066 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1068 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1070 // Only do the check if we're not at the end of the paragraph (where things work OK
1072 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1074 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1078 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1080 // We're at the end of a line. See whether we need to
1081 // stay at the same actual caret position but change visual
1082 // position, or not.
1083 if (oldPosition
== lineRange
.GetEnd())
1085 if (m_caretAtLineStart
)
1087 // We're already at the start of the line, so actually move on now.
1088 m_caretPosition
= oldPosition
+ 1;
1089 m_caretAtLineStart
= false;
1093 // We're showing at the end of the line, so keep to
1094 // the same position but indicate that we're to show
1095 // at the start of the next line.
1096 m_caretPosition
= oldPosition
;
1097 m_caretAtLineStart
= true;
1099 SetDefaultStyleToCursorStyle();
1105 SetDefaultStyleToCursorStyle();
1108 /// Move caret one visual step backward: this may mean setting a flag
1109 /// and keeping the same position if we're going from the end of one line
1110 /// to the start of the next, which may be the exact same caret position.
1111 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1113 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(oldPosition
);
1115 // Only do the check if we're not at the start of the paragraph (where things work OK
1117 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1119 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(oldPosition
);
1123 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1125 // We're at the start of a line. See whether we need to
1126 // stay at the same actual caret position but change visual
1127 // position, or not.
1128 if (oldPosition
== lineRange
.GetStart())
1130 m_caretPosition
= oldPosition
-1;
1131 m_caretAtLineStart
= true;
1134 else if (oldPosition
== lineRange
.GetEnd())
1136 if (m_caretAtLineStart
)
1138 // We're at the start of the line, so keep the same caret position
1139 // but clear the start-of-line flag.
1140 m_caretPosition
= oldPosition
;
1141 m_caretAtLineStart
= false;
1145 // We're showing at the end of the line, so go back
1146 // to the previous character position.
1147 m_caretPosition
= oldPosition
- 1;
1149 SetDefaultStyleToCursorStyle();
1155 SetDefaultStyleToCursorStyle();
1159 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1161 long endPos
= GetBuffer().GetRange().GetEnd();
1163 if (m_caretPosition
+ noPositions
< endPos
)
1165 long oldPos
= m_caretPosition
;
1166 long newPos
= m_caretPosition
+ noPositions
;
1168 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1172 // Determine by looking at oldPos and m_caretPosition whether
1173 // we moved from the end of a line to the start of the next line, in which case
1174 // we want to adjust the caret position such that it is positioned at the
1175 // start of the next line, rather than jumping past the first character of the
1177 if (noPositions
== 1 && !extendSel
)
1178 MoveCaretForward(oldPos
);
1180 SetCaretPosition(newPos
);
1183 SetDefaultStyleToCursorStyle();
1194 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1198 if (m_caretPosition
> startPos
- noPositions
+ 1)
1200 long oldPos
= m_caretPosition
;
1201 long newPos
= m_caretPosition
- noPositions
;
1202 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1206 if (noPositions
== 1 && !extendSel
)
1207 MoveCaretBack(oldPos
);
1209 SetCaretPosition(newPos
);
1212 SetDefaultStyleToCursorStyle();
1223 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1225 return MoveDown(- noLines
, flags
);
1229 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1234 long lineNumber
= GetBuffer().GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1235 wxPoint pt
= GetCaret()->GetPosition();
1236 long newLine
= lineNumber
+ noLines
;
1238 if (lineNumber
!= -1)
1242 long lastLine
= GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1244 if (newLine
> lastLine
)
1254 wxRichTextLine
* lineObj
= GetBuffer().GetLineForVisibleLineNumber(newLine
);
1257 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1263 wxClientDC
dc(this);
1265 dc
.SetFont(GetFont());
1267 int hitTest
= GetBuffer().HitTest(dc
, pt
, newPos
);
1269 if (hitTest
!= wxRICHTEXT_HITTEST_NONE
)
1271 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1272 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1273 // so we view the caret at the start of the line.
1274 bool caretLineStart
= false;
1275 if (hitTest
== wxRICHTEXT_HITTEST_BEFORE
)
1277 wxRichTextLine
* thisLine
= GetBuffer().GetLineAtPosition(newPos
-1);
1278 wxRichTextRange lineRange
;
1280 lineRange
= thisLine
->GetAbsoluteRange();
1282 if (thisLine
&& (newPos
-1) == lineRange
.GetEnd())
1285 caretLineStart
= true;
1289 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(newPos
);
1290 if (para
&& para
->GetRange().GetStart() == newPos
)
1295 long newSelEnd
= newPos
;
1297 bool extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1301 SetCaretPosition(newPos
, caretLineStart
);
1303 SetDefaultStyleToCursorStyle();
1313 /// Move to the end of the paragraph
1314 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1316 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1319 long newPos
= para
->GetRange().GetEnd() - 1;
1320 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1324 SetCaretPosition(newPos
);
1326 SetDefaultStyleToCursorStyle();
1336 /// Move to the start of the paragraph
1337 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1339 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(m_caretPosition
, true);
1342 long newPos
= para
->GetRange().GetStart() - 1;
1343 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1347 SetCaretPosition(newPos
);
1349 SetDefaultStyleToCursorStyle();
1359 /// Move to the end of the line
1360 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1362 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1366 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1367 long newPos
= lineRange
.GetEnd();
1368 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1372 SetCaretPosition(newPos
);
1374 SetDefaultStyleToCursorStyle();
1384 /// Move to the start of the line
1385 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1387 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1390 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1391 long newPos
= lineRange
.GetStart()-1;
1393 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1397 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(line
);
1399 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1401 SetDefaultStyleToCursorStyle();
1411 /// Move to the start of the buffer
1412 bool wxRichTextCtrl::MoveHome(int flags
)
1414 if (m_caretPosition
!= -1)
1416 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1420 SetCaretPosition(-1);
1422 SetDefaultStyleToCursorStyle();
1432 /// Move to the end of the buffer
1433 bool wxRichTextCtrl::MoveEnd(int flags
)
1435 long endPos
= GetBuffer().GetRange().GetEnd()-1;
1437 if (m_caretPosition
!= endPos
)
1439 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1443 SetCaretPosition(endPos
);
1445 SetDefaultStyleToCursorStyle();
1455 /// Move noPages pages up
1456 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
1458 return PageDown(- noPages
, flags
);
1461 /// Move noPages pages down
1462 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
1464 // Calculate which line occurs noPages * screen height further down.
1465 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1468 wxSize clientSize
= GetClientSize();
1469 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
1471 wxRichTextLine
* newLine
= GetBuffer().GetLineAtYPosition(newY
);
1474 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
1475 long pos
= lineRange
.GetStart()-1;
1476 if (pos
!= m_caretPosition
)
1478 wxRichTextParagraph
* para
= GetBuffer().GetParagraphForLine(newLine
);
1480 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1484 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1486 SetDefaultStyleToCursorStyle();
1498 // Finds the caret position for the next word
1499 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
1501 long endPos
= GetBuffer().GetRange().GetEnd();
1505 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
1507 // First skip current text to space
1508 while (i
< endPos
&& i
> -1)
1510 // i is in character, not caret positions
1511 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1512 if (text
!= wxT(" ") && !text
.empty())
1519 while (i
< endPos
&& i
> -1)
1521 // i is in character, not caret positions
1522 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1523 if (text
.empty()) // End of paragraph, or maybe an image
1524 return wxMax(-1, i
- 1);
1525 else if (text
== wxT(" ") || text
.empty())
1529 // Convert to caret position
1530 return wxMax(-1, i
- 1);
1539 long i
= m_caretPosition
;
1541 // First skip white space
1542 while (i
< endPos
&& i
> -1)
1544 // i is in character, not caret positions
1545 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1546 if (text
.empty()) // End of paragraph, or maybe an image
1548 else if (text
== wxT(" ") || text
.empty())
1553 // Next skip current text to space
1554 while (i
< endPos
&& i
> -1)
1556 // i is in character, not caret positions
1557 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(i
, i
));
1558 if (text
!= wxT(" ") /* && !text.empty() */)
1571 /// Move n words left
1572 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
1574 long pos
= FindNextWordPosition(-1);
1575 if (pos
!= m_caretPosition
)
1577 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1579 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1583 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1585 SetDefaultStyleToCursorStyle();
1595 /// Move n words right
1596 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
1598 long pos
= FindNextWordPosition(1);
1599 if (pos
!= m_caretPosition
)
1601 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(pos
, true);
1603 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
1607 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
1609 SetDefaultStyleToCursorStyle();
1620 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
1622 // Only do sizing optimization for large buffers
1623 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold
)
1625 m_fullLayoutRequired
= true;
1626 m_fullLayoutTime
= wxGetLocalTimeMillis();
1627 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
1628 LayoutContent(true /* onlyVisibleRect */);
1631 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1633 #if wxRICHTEXT_BUFFERED_PAINTING
1641 /// Idle-time processing
1642 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
1644 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
1646 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
1648 m_fullLayoutRequired
= false;
1649 m_fullLayoutTime
= 0;
1650 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
1651 ShowPosition(m_fullLayoutSavedPosition
);
1655 if (m_caretPositionForDefaultStyle
!= -2)
1657 // If the caret position has changed, no longer reflect the default style
1659 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
1660 m_caretPositionForDefaultStyle
= -2;
1667 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
1673 /// Set up scrollbars, e.g. after a resize
1674 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
1679 if (GetBuffer().IsEmpty())
1681 SetScrollbars(0, 0, 0, 0, 0, 0);
1685 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1686 // of pixels. See e.g. wxVScrolledWindow for ideas.
1687 int pixelsPerUnit
= 5;
1688 wxSize clientSize
= GetClientSize();
1690 int maxHeight
= GetBuffer().GetCachedSize().y
;
1692 // Round up so we have at least maxHeight pixels
1693 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
1695 int startX
= 0, startY
= 0;
1697 GetViewStart(& startX
, & startY
);
1699 int maxPositionX
= 0; // wxMax(sz.x - clientSize.x, 0);
1700 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
1702 // Move to previous scroll position if
1704 SetScrollbars(0, pixelsPerUnit
,
1706 wxMin(maxPositionX
, startX
), wxMin(maxPositionY
, startY
));
1709 /// Paint the background
1710 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
1712 wxColour backgroundColour
= GetBackgroundColour();
1713 if (!backgroundColour
.Ok())
1714 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1716 // Clear the background
1717 dc
.SetBrush(wxBrush(backgroundColour
));
1718 dc
.SetPen(*wxTRANSPARENT_PEN
);
1719 wxRect
windowRect(GetClientSize());
1720 windowRect
.x
-= 2; windowRect
.y
-= 2;
1721 windowRect
.width
+= 4; windowRect
.height
+= 4;
1723 // We need to shift the rectangle to take into account
1724 // scrolling. Converting device to logical coordinates.
1725 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
1726 dc
.DrawRectangle(windowRect
);
1729 #if wxRICHTEXT_BUFFERED_PAINTING
1730 /// Recreate buffer bitmap if necessary
1731 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
1734 if (sz
== wxDefaultSize
)
1735 sz
= GetClientSize();
1737 if (sz
.x
< 1 || sz
.y
< 1)
1740 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
1741 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
1742 return m_bufferBitmap
.Ok();
1746 // ----------------------------------------------------------------------------
1747 // file IO functions
1748 // ----------------------------------------------------------------------------
1750 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
1752 bool success
= GetBuffer().LoadFile(filename
, fileType
);
1754 m_filename
= filename
;
1757 SetInsertionPoint(0);
1760 SetupScrollbars(true);
1762 SendTextUpdatedEvent();
1768 wxLogError(_("File couldn't be loaded."));
1774 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
1776 if (GetBuffer().SaveFile(filename
, fileType
))
1778 m_filename
= filename
;
1785 wxLogError(_("The text couldn't be saved."));
1790 // ----------------------------------------------------------------------------
1791 // wxRichTextCtrl specific functionality
1792 // ----------------------------------------------------------------------------
1794 /// Add a new paragraph of text to the end of the buffer
1795 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
1797 return GetBuffer().AddParagraph(text
);
1801 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
1803 return GetBuffer().AddImage(image
);
1806 // ----------------------------------------------------------------------------
1807 // selection and ranges
1808 // ----------------------------------------------------------------------------
1810 void wxRichTextCtrl::SelectAll()
1812 SetSelection(0, GetLastPosition()+1);
1813 m_selectionAnchor
= -1;
1817 void wxRichTextCtrl::SelectNone()
1819 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1820 SetSelection(-2, -2);
1821 m_selectionAnchor
= -2;
1824 static bool wxIsWordDelimiter(const wxString
& text
)
1826 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
1829 /// Select the word at the given character position
1830 bool wxRichTextCtrl::SelectWord(long position
)
1832 if (position
< 0 || position
> GetBuffer().GetRange().GetEnd())
1835 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(position
);
1839 long positionStart
= position
;
1840 long positionEnd
= position
;
1842 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
1844 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
1845 if (wxIsWordDelimiter(text
))
1851 if (positionStart
< para
->GetRange().GetStart())
1852 positionStart
= para
->GetRange().GetStart();
1854 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
1856 wxString text
= GetBuffer().GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
1857 if (wxIsWordDelimiter(text
))
1863 if (positionEnd
>= para
->GetRange().GetEnd())
1864 positionEnd
= para
->GetRange().GetEnd();
1866 SetSelection(positionStart
, positionEnd
+1);
1868 if (positionStart
>= 0)
1870 MoveCaret(positionStart
-1, true);
1871 SetDefaultStyleToCursorStyle();
1877 wxString
wxRichTextCtrl::GetStringSelection() const
1880 GetSelection(&from
, &to
);
1882 return GetRange(from
, to
);
1885 // ----------------------------------------------------------------------------
1887 // ----------------------------------------------------------------------------
1889 wxTextCtrlHitTestResult
1890 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1892 // implement in terms of the other overload as the native ports typically
1893 // can get the position and not (x, y) pair directly (although wxUniv
1894 // directly gets x and y -- and so overrides this method as well)
1896 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1898 if ( rc
!= wxTE_HT_UNKNOWN
)
1900 PositionToXY(pos
, x
, y
);
1906 wxTextCtrlHitTestResult
1907 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
1910 wxClientDC
dc((wxRichTextCtrl
*) this);
1911 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
1913 // Buffer uses logical position (relative to start of buffer)
1915 wxPoint pt2
= GetLogicalPoint(pt
);
1917 int hit
= ((wxRichTextCtrl
*)this)->GetBuffer().HitTest(dc
, pt2
, *pos
);
1921 case wxRICHTEXT_HITTEST_BEFORE
:
1922 return wxTE_HT_BEFORE
;
1924 case wxRICHTEXT_HITTEST_AFTER
:
1925 return wxTE_HT_BEYOND
;
1927 case wxRICHTEXT_HITTEST_ON
:
1928 return wxTE_HT_ON_TEXT
;
1931 return wxTE_HT_UNKNOWN
;
1934 // ----------------------------------------------------------------------------
1935 // set/get the controls text
1936 // ----------------------------------------------------------------------------
1938 wxString
wxRichTextCtrl::GetValue() const
1940 return GetBuffer().GetText();
1943 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
1945 // Public API for range is different from internals
1946 return GetBuffer().GetTextForRange(wxRichTextRange(from
, to
-1));
1949 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
1953 // if the text is long enough, it's faster to just set it instead of first
1954 // comparing it with the old one (chances are that it will be different
1955 // anyhow, this comparison is there to avoid flicker for small single-line
1956 // edit controls mostly)
1957 if ( (value
.length() > 0x400) || (value
!= GetValue()) )
1961 // for compatibility, don't move the cursor when doing SetValue()
1962 SetInsertionPoint(0);
1966 if ( flags
& SetValue_SendEvent
)
1968 // still send an event for consistency
1969 SendTextUpdatedEvent();
1973 // we should reset the modified flag even if the value didn't really change
1975 // mark the control as being not dirty - we changed its text, not the
1980 void wxRichTextCtrl::WriteText(const wxString
& value
)
1985 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
1987 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
1989 GetBuffer().InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this);
1991 if ( flags
& SetValue_SendEvent
)
1992 SendTextUpdatedEvent();
1995 void wxRichTextCtrl::AppendText(const wxString
& text
)
1997 SetInsertionPointEnd();
2002 /// Write an image at the current insertion point
2003 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, int bitmapType
)
2005 wxRichTextImageBlock imageBlock
;
2007 wxImage image2
= image
;
2008 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2009 return WriteImage(imageBlock
);
2014 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, int bitmapType
)
2016 wxRichTextImageBlock imageBlock
;
2019 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2020 return WriteImage(imageBlock
);
2025 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
)
2027 return GetBuffer().InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this);
2030 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, int bitmapType
)
2034 wxRichTextImageBlock imageBlock
;
2036 wxImage image
= bitmap
.ConvertToImage();
2037 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2038 return WriteImage(imageBlock
);
2044 /// Insert a newline (actually paragraph) at the current insertion point.
2045 bool wxRichTextCtrl::Newline()
2047 return GetBuffer().InsertNewlineWithUndo(m_caretPosition
+1, this);
2051 // ----------------------------------------------------------------------------
2052 // Clipboard operations
2053 // ----------------------------------------------------------------------------
2055 void wxRichTextCtrl::Copy()
2059 wxRichTextRange range
= GetInternalSelectionRange();
2060 GetBuffer().CopyToClipboard(range
);
2064 void wxRichTextCtrl::Cut()
2068 wxRichTextRange range
= GetInternalSelectionRange();
2069 GetBuffer().CopyToClipboard(range
);
2071 DeleteSelectedContent();
2077 void wxRichTextCtrl::Paste()
2081 BeginBatchUndo(_("Paste"));
2083 long newPos
= m_caretPosition
;
2084 DeleteSelectedContent(& newPos
);
2086 GetBuffer().PasteFromClipboard(newPos
);
2092 void wxRichTextCtrl::DeleteSelection()
2094 if (CanDeleteSelection())
2096 DeleteSelectedContent();
2100 bool wxRichTextCtrl::HasSelection() const
2102 return m_selectionRange
.GetStart() != -2 && m_selectionRange
.GetEnd() != -2;
2105 bool wxRichTextCtrl::CanCopy() const
2107 // Can copy if there's a selection
2108 return HasSelection();
2111 bool wxRichTextCtrl::CanCut() const
2113 return HasSelection() && IsEditable();
2116 bool wxRichTextCtrl::CanPaste() const
2118 if ( !IsEditable() )
2121 return GetBuffer().CanPasteFromClipboard();
2124 bool wxRichTextCtrl::CanDeleteSelection() const
2126 return HasSelection() && IsEditable();
2130 // ----------------------------------------------------------------------------
2132 // ----------------------------------------------------------------------------
2134 void wxRichTextCtrl::SetEditable(bool editable
)
2136 m_editable
= editable
;
2139 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2143 m_caretPosition
= pos
- 1;
2146 void wxRichTextCtrl::SetInsertionPointEnd()
2148 long pos
= GetLastPosition();
2149 SetInsertionPoint(pos
);
2152 long wxRichTextCtrl::GetInsertionPoint() const
2154 return m_caretPosition
+1;
2157 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2159 return GetBuffer().GetRange().GetEnd();
2162 // If the return values from and to are the same, there is no
2164 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2166 *from
= m_selectionRange
.GetStart();
2167 *to
= m_selectionRange
.GetEnd();
2168 if ((*to
) != -1 && (*to
) != -2)
2172 bool wxRichTextCtrl::IsEditable() const
2177 // ----------------------------------------------------------------------------
2179 // ----------------------------------------------------------------------------
2181 void wxRichTextCtrl::SetSelection(long from
, long to
)
2183 // if from and to are both -1, it means (in wxWidgets) that all text should
2185 if ( (from
== -1) && (to
== -1) )
2188 to
= GetLastPosition()+1;
2191 DoSetSelection(from
, to
);
2194 void wxRichTextCtrl::DoSetSelection(long from
, long to
, bool WXUNUSED(scrollCaret
))
2196 m_selectionAnchor
= from
;
2197 m_selectionRange
.SetRange(from
, to
-1);
2203 // ----------------------------------------------------------------------------
2205 // ----------------------------------------------------------------------------
2207 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2208 const wxString
& value
)
2210 BeginBatchUndo(_("Replace"));
2212 DeleteSelectedContent();
2214 DoWriteText(value
, SetValue_SelectionOnly
);
2219 void wxRichTextCtrl::Remove(long from
, long to
)
2223 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from
, to
),
2224 m_caretPosition
, // Current caret position
2225 from
, // New caret position
2233 bool wxRichTextCtrl::IsModified() const
2235 return m_buffer
.IsModified();
2238 void wxRichTextCtrl::MarkDirty()
2240 m_buffer
.Modify(true);
2243 void wxRichTextCtrl::DiscardEdits()
2245 m_caretPositionForDefaultStyle
= -2;
2246 m_buffer
.Modify(false);
2247 m_buffer
.GetCommandProcessor()->ClearCommands();
2250 int wxRichTextCtrl::GetNumberOfLines() const
2252 return GetBuffer().GetParagraphCount();
2255 // ----------------------------------------------------------------------------
2256 // Positions <-> coords
2257 // ----------------------------------------------------------------------------
2259 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2261 return GetBuffer().XYToPosition(x
, y
);
2264 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2266 return GetBuffer().PositionToXY(pos
, x
, y
);
2269 // ----------------------------------------------------------------------------
2271 // ----------------------------------------------------------------------------
2273 void wxRichTextCtrl::ShowPosition(long pos
)
2275 if (!IsPositionVisible(pos
))
2276 ScrollIntoView(pos
-1, WXK_DOWN
);
2279 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2281 return GetBuffer().GetParagraphLength(lineNo
);
2284 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2286 return GetBuffer().GetParagraphText(lineNo
);
2289 // ----------------------------------------------------------------------------
2291 // ----------------------------------------------------------------------------
2293 void wxRichTextCtrl::Undo()
2297 GetCommandProcessor()->Undo();
2301 void wxRichTextCtrl::Redo()
2305 GetCommandProcessor()->Redo();
2309 bool wxRichTextCtrl::CanUndo() const
2311 return GetCommandProcessor()->CanUndo();
2314 bool wxRichTextCtrl::CanRedo() const
2316 return GetCommandProcessor()->CanRedo();
2319 // ----------------------------------------------------------------------------
2320 // implementation details
2321 // ----------------------------------------------------------------------------
2323 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
2325 SetValue(event
.GetString());
2326 GetEventHandler()->ProcessEvent(event
);
2329 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
2331 // By default, load the first file into the text window.
2332 if (event
.GetNumberOfFiles() > 0)
2334 LoadFile(event
.GetFiles()[0]);
2338 wxSize
wxRichTextCtrl::DoGetBestSize() const
2340 return wxSize(10, 10);
2343 // ----------------------------------------------------------------------------
2344 // standard handlers for standard edit menu events
2345 // ----------------------------------------------------------------------------
2347 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2352 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
2357 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2362 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2367 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2372 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2377 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2379 event
.Enable( CanCut() );
2382 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2384 event
.Enable( CanCopy() );
2387 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
2389 event
.Enable( CanDeleteSelection() );
2392 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2394 event
.Enable( CanPaste() );
2397 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2399 event
.Enable( CanUndo() );
2400 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2403 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2405 event
.Enable( CanRedo() );
2406 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2409 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
2414 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
2416 event
.Enable(GetLastPosition() > 0);
2419 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& WXUNUSED(event
))
2423 m_contextMenu
= new wxMenu
;
2424 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
2425 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
2426 m_contextMenu
->AppendSeparator();
2427 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
2428 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
2429 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
2430 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
2431 m_contextMenu
->AppendSeparator();
2432 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
2434 PopupMenu(m_contextMenu
);
2438 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttrEx
& style
)
2440 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
);
2443 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
2445 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), wxTextAttrEx(style
));
2448 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
2450 return GetBuffer().SetStyle(range
.ToInternal(), style
);
2453 // extended style setting operation with flags including:
2454 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2455 // see richtextbuffer.h for more details.
2456 bool wxRichTextCtrl::SetStyleEx(long start
, long end
, const wxTextAttrEx
& style
, int flags
)
2458 return GetBuffer().SetStyle(wxRichTextRange(start
, end
-1), style
, flags
);
2461 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxTextAttrEx
& style
, int flags
)
2463 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2466 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
2468 return GetBuffer().SetStyle(range
.ToInternal(), style
, flags
);
2471 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx
& style
)
2473 return GetBuffer().SetDefaultStyle(style
);
2476 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
2478 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style
));
2481 const wxTextAttrEx
& wxRichTextCtrl::GetDefaultStyleEx() const
2483 return GetBuffer().GetDefaultStyle();
2486 const wxTextAttr
& wxRichTextCtrl::GetDefaultStyle() const
2488 return GetBuffer().GetDefaultStyle();
2491 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
2493 wxTextAttrEx
attr(style
);
2494 if (GetBuffer().GetStyle(position
, attr
))
2503 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttrEx
& style
)
2505 return GetBuffer().GetStyle(position
, style
);
2508 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
2510 return GetBuffer().GetStyle(position
, style
);
2513 /// Get the content (uncombined) attributes for this position.
2515 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttr
& style
)
2517 wxTextAttrEx
attr(style
);
2518 if (GetBuffer().GetUncombinedStyle(position
, attr
))
2527 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxTextAttrEx
& style
)
2529 return GetBuffer().GetUncombinedStyle(position
, style
);
2532 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
2534 return GetBuffer().GetUncombinedStyle(position
, style
);
2537 /// Set font, and also the buffer attributes
2538 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
2540 wxControl::SetFont(font
);
2542 wxTextAttrEx attr
= GetBuffer().GetAttributes();
2544 GetBuffer().SetBasicStyle(attr
);
2546 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2552 /// Transform logical to physical
2553 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
2556 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
2561 /// Transform physical to logical
2562 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
2565 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
2570 /// Position the caret
2571 void wxRichTextCtrl::PositionCaret()
2576 //wxLogDebug(wxT("PositionCaret"));
2579 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
))
2581 wxPoint originalPt
= caretRect
.GetPosition();
2582 wxPoint pt
= GetPhysicalPoint(originalPt
);
2583 if (GetCaret()->GetPosition() != pt
)
2585 GetCaret()->Move(pt
);
2586 GetCaret()->SetSize(caretRect
.GetSize());
2591 /// Get the caret height and position for the given character position
2592 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
)
2594 wxClientDC
dc(this);
2595 dc
.SetFont(GetFont());
2602 if (GetBuffer().FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
2604 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
2611 /// Gets the line for the visible caret position. If the caret is
2612 /// shown at the very end of the line, it means the next character is actually
2613 /// on the following line. So let's get the line we're expecting to find
2614 /// if this is the case.
2615 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
2617 wxRichTextLine
* line
= GetBuffer().GetLineAtPosition(caretPosition
, true);
2618 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPosition
, true);
2621 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2622 if (caretPosition
== lineRange
.GetStart()-1 &&
2623 (para
->GetRange().GetStart() != lineRange
.GetStart()))
2625 if (!m_caretAtLineStart
)
2626 line
= GetBuffer().GetLineAtPosition(caretPosition
-1, true);
2633 /// Move the caret to the given character position
2634 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
)
2636 if (GetBuffer().GetDirty())
2639 if (pos
<= GetBuffer().GetRange().GetEnd())
2641 SetCaretPosition(pos
, showAtLineStart
);
2651 /// Layout the buffer: which we must do before certain operations, such as
2652 /// setting the caret position.
2653 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
2655 if (GetBuffer().GetDirty() || onlyVisibleRect
)
2657 wxRect
availableSpace(GetClientSize());
2658 if (availableSpace
.width
== 0)
2659 availableSpace
.width
= 10;
2660 if (availableSpace
.height
== 0)
2661 availableSpace
.height
= 10;
2663 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
2664 if (onlyVisibleRect
)
2666 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
2667 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2670 wxClientDC
dc(this);
2671 dc
.SetFont(GetFont());
2675 GetBuffer().Defragment();
2676 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2677 GetBuffer().Layout(dc
, availableSpace
, flags
);
2678 GetBuffer().SetDirty(false);
2687 /// Is all of the selection bold?
2688 bool wxRichTextCtrl::IsSelectionBold()
2692 wxRichTextAttr attr
;
2693 wxRichTextRange range
= GetInternalSelectionRange();
2694 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2695 attr
.SetFontWeight(wxBOLD
);
2697 return HasCharacterAttributes(range
, attr
);
2701 // If no selection, then we need to combine current style with default style
2702 // to see what the effect would be if we started typing.
2703 wxRichTextAttr attr
;
2704 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2706 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2707 if (GetStyle(pos
, attr
))
2709 if (IsDefaultStyleShowing())
2710 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2711 return attr
.GetFontWeight() == wxBOLD
;
2717 /// Is all of the selection italics?
2718 bool wxRichTextCtrl::IsSelectionItalics()
2722 wxRichTextRange range
= GetInternalSelectionRange();
2723 wxRichTextAttr attr
;
2724 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2725 attr
.SetFontStyle(wxITALIC
);
2727 return HasCharacterAttributes(range
, attr
);
2731 // If no selection, then we need to combine current style with default style
2732 // to see what the effect would be if we started typing.
2733 wxRichTextAttr attr
;
2734 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2736 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2737 if (GetStyle(pos
, attr
))
2739 if (IsDefaultStyleShowing())
2740 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2741 return attr
.GetFontStyle() == wxITALIC
;
2747 /// Is all of the selection underlined?
2748 bool wxRichTextCtrl::IsSelectionUnderlined()
2752 wxRichTextRange range
= GetInternalSelectionRange();
2753 wxRichTextAttr attr
;
2754 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2755 attr
.SetFontUnderlined(true);
2757 return HasCharacterAttributes(range
, attr
);
2761 // If no selection, then we need to combine current style with default style
2762 // to see what the effect would be if we started typing.
2763 wxRichTextAttr attr
;
2764 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2765 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2767 if (GetStyle(pos
, attr
))
2769 if (IsDefaultStyleShowing())
2770 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
2771 return attr
.GetFontUnderlined();
2777 /// Apply bold to the selection
2778 bool wxRichTextCtrl::ApplyBoldToSelection()
2780 wxRichTextAttr attr
;
2781 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
2782 attr
.SetFontWeight(IsSelectionBold() ? wxNORMAL
: wxBOLD
);
2785 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2787 SetAndShowDefaultStyle(attr
);
2791 /// Apply italic to the selection
2792 bool wxRichTextCtrl::ApplyItalicToSelection()
2794 wxRichTextAttr attr
;
2795 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
2796 attr
.SetFontStyle(IsSelectionItalics() ? wxNORMAL
: wxITALIC
);
2799 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2801 SetAndShowDefaultStyle(attr
);
2805 /// Apply underline to the selection
2806 bool wxRichTextCtrl::ApplyUnderlineToSelection()
2808 wxRichTextAttr attr
;
2809 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
2810 attr
.SetFontUnderlined(!IsSelectionUnderlined());
2813 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
2815 SetAndShowDefaultStyle(attr
);
2819 /// Is all of the selection aligned according to the specified flag?
2820 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
2822 wxRichTextRange range
;
2824 range
= GetInternalSelectionRange();
2826 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+1);
2828 wxRichTextAttr attr
;
2829 attr
.SetAlignment(alignment
);
2831 return HasParagraphAttributes(range
, attr
);
2834 /// Apply alignment to the selection
2835 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
2837 wxRichTextAttr attr
;
2838 attr
.SetAlignment(alignment
);
2840 return SetStyle(GetSelectionRange(), attr
);
2843 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2845 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
2850 /// Apply a named style to the selection
2851 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
2853 // Flags are defined within each definition, so only certain
2854 // attributes are applied.
2855 wxRichTextAttr
attr(def
->GetStyle());
2857 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
;
2859 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
2861 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2863 wxRichTextRange range
;
2866 range
= GetSelectionRange();
2869 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2870 range
= wxRichTextRange(pos
, pos
+1);
2873 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
2876 // Make sure the attr has the style name
2877 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
2879 attr
.SetParagraphStyleName(def
->GetName());
2881 // If applying a paragraph style, we only want the paragraph nodes to adopt these
2882 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
2883 // to change its style independently.
2884 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
2887 attr
.SetCharacterStyleName(def
->GetName());
2890 return SetStyleEx(GetSelectionRange(), attr
, flags
);
2893 SetAndShowDefaultStyle(attr
);
2898 /// Apply the style sheet to the buffer, for example if the styles have changed.
2899 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
2902 styleSheet
= GetBuffer().GetStyleSheet();
2906 if (GetBuffer().ApplyStyleSheet(styleSheet
))
2908 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2916 /// Sets the default style to the style under the cursor
2917 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2920 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
2922 // If at the start of a paragraph, use the next position.
2923 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
2925 #if wxRICHTEXT_USE_DYNAMIC_STYLES
2926 if (GetUncombinedStyle(pos
, attr
))
2928 if (GetStyle(pos
, attr
))
2931 SetDefaultStyle(attr
);
2938 /// Returns the first visible position in the current view
2939 long wxRichTextCtrl::GetFirstVisiblePosition() const
2941 wxRichTextLine
* line
= GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
2943 return line
->GetAbsoluteRange().GetStart();
2948 /// The adjusted caret position is the character position adjusted to take
2949 /// into account whether we're at the start of a paragraph, in which case
2950 /// style information should be taken from the next position, not current one.
2951 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
2953 wxRichTextParagraph
* para
= GetBuffer().GetParagraphAtPosition(caretPos
+1);
2955 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
2960 /// Get/set the selection range in character positions. -1, -1 means no selection.
2961 /// The range is in API convention, i.e. a single character selection is denoted
2963 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
2965 wxRichTextRange range
= GetInternalSelectionRange();
2966 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
2967 range
.SetEnd(range
.GetEnd() + 1);
2971 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
2973 wxRichTextRange
range1(range
);
2974 if (range1
!= wxRichTextRange(-2,-2) && range1
!= wxRichTextRange(-1,-1) )
2975 range1
.SetEnd(range1
.GetEnd() - 1);
2977 wxASSERT( range1
.GetStart() > range1
.GetEnd() );
2979 SetInternalSelectionRange(range1
);
2983 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
2985 return GetBuffer().SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
2988 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
2990 return GetBuffer().SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
2993 /// Clear list for given range
2994 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
2996 return GetBuffer().ClearListStyle(range
.ToInternal(), flags
);
2999 /// Number/renumber any list elements in the given range
3000 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3002 return GetBuffer().NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3005 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3007 return GetBuffer().NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3010 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3011 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3013 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3016 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3018 return GetBuffer().PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3021 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3023 if (sm_availableFontNames
.GetCount() == 0)
3025 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3026 sm_availableFontNames
.Sort();
3028 return sm_availableFontNames
;
3031 void wxRichTextCtrl::ClearAvailableFontNames()
3033 sm_availableFontNames
.Clear();