1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextctrl.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"
30 #include "wx/textfile.h"
32 #include "wx/filename.h"
33 #include "wx/dcbuffer.h"
34 #include "wx/arrimpl.cpp"
35 #include "wx/fontenum.h"
38 #if defined (__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
39 #define wxHAVE_PRIMARY_SELECTION 1
41 #define wxHAVE_PRIMARY_SELECTION 0
44 #if wxUSE_CLIPBOARD && wxHAVE_PRIMARY_SELECTION
45 #include "wx/clipbrd.h"
48 // DLL options compatibility check:
50 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
54 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER
, wxRichTextEvent
);
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE
, wxRichTextEvent
);
60 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
61 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
62 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
63 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
65 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
66 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
67 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
68 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_PROPERTIES_CHANGED
, wxRichTextEvent
);
69 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
70 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
71 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
, wxRichTextEvent
);
73 #if wxRICHTEXT_USE_OWN_CARET
78 * This implements a non-flashing cursor in case there
79 * are platform-specific problems with the generic caret.
80 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
83 class wxRichTextCaret
;
84 class wxRichTextCaretTimer
: public wxTimer
87 wxRichTextCaretTimer(wxRichTextCaret
* caret
)
91 virtual void Notify();
92 wxRichTextCaret
* m_caret
;
95 class wxRichTextCaret
: public wxCaret
100 // default - use Create()
101 wxRichTextCaret(): m_timer(this) { Init(); }
102 // creates a block caret associated with the given window
103 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
104 : wxCaret(window
, width
, height
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
105 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
106 : wxCaret(window
, size
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
108 virtual ~wxRichTextCaret();
113 // called by wxWindow (not using the event tables)
114 virtual void OnSetFocus();
115 virtual void OnKillFocus();
117 // draw the caret on the given DC
118 void DoDraw(wxDC
*dc
);
120 // get the visible count
121 int GetVisibleCount() const { return m_countVisible
; }
123 // delay repositioning
124 bool GetNeedsUpdate() const { return m_needsUpdate
; }
125 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
129 bool GetRefreshEnabled() const { return m_refreshEnabled
; }
130 void EnableRefresh(bool b
) { m_refreshEnabled
= b
; }
133 virtual void DoShow();
134 virtual void DoHide();
135 virtual void DoMove();
136 virtual void DoSize();
146 bool m_hasFocus
; // true => our window has focus
147 bool m_needsUpdate
; // must be repositioned
149 wxRichTextCaretTimer m_timer
;
150 wxRichTextCtrl
* m_richTextCtrl
;
151 bool m_refreshEnabled
;
155 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxControl
)
157 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
159 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
160 EVT_PAINT(wxRichTextCtrl::OnPaint
)
161 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
162 EVT_IDLE(wxRichTextCtrl::OnIdle
)
163 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
164 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
165 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
166 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
167 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
168 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
169 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
170 EVT_CHAR(wxRichTextCtrl::OnChar
)
171 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
172 EVT_SIZE(wxRichTextCtrl::OnSize
)
173 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
174 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
175 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
176 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
177 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
179 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
180 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
182 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
183 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
185 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
186 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
188 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
189 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
191 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
192 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
194 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
195 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
197 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
198 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
200 EVT_MENU(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnProperties
)
201 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnUpdateProperties
)
203 EVT_MENU(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnProperties
)
204 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnUpdateProperties
)
206 EVT_MENU(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnProperties
)
207 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnUpdateProperties
)
215 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
217 wxRichTextCtrl::wxRichTextCtrl()
218 : wxScrollHelper(this)
223 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
225 const wxString
& value
,
229 const wxValidator
& validator
,
230 const wxString
& name
)
231 : wxScrollHelper(this)
234 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
238 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
239 const wxValidator
& validator
, const wxString
& name
)
243 if (!wxControl::Create(parent
, id
, pos
, size
,
244 style
|wxFULL_REPAINT_ON_RESIZE
,
248 if (!GetFont().IsOk())
250 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
253 // No physical scrolling, so we can preserve margins
254 EnableScrolling(false, false);
256 if (style
& wxTE_READONLY
)
259 // The base attributes must all have default values
260 wxRichTextAttr attributes
;
261 attributes
.SetFont(GetFont());
262 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
263 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
264 attributes
.SetLineSpacing(10);
265 attributes
.SetParagraphSpacingAfter(10);
266 attributes
.SetParagraphSpacingBefore(0);
268 SetBasicStyle(attributes
);
271 SetMargins(margin
, margin
);
273 // The default attributes will be merged with base attributes, so
274 // can be empty to begin with
275 wxRichTextAttr defaultAttributes
;
276 SetDefaultStyle(defaultAttributes
);
278 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
279 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
282 GetBuffer().SetRichTextCtrl(this);
284 #if wxRICHTEXT_USE_OWN_CARET
285 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
287 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
290 // Tell the sizers to use the given or best size
291 SetInitialSize(size
);
293 #if wxRICHTEXT_BUFFERED_PAINTING
295 RecreateBuffer(size
);
298 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
299 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
301 SetCursor(m_textCursor
);
303 if (!value
.IsEmpty())
306 GetBuffer().AddEventHandler(this);
309 wxAcceleratorEntry entries
[6];
311 entries
[0].Set(wxACCEL_CTRL
, (int) 'C', wxID_COPY
);
312 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', wxID_CUT
);
313 entries
[2].Set(wxACCEL_CTRL
, (int) 'V', wxID_PASTE
);
314 entries
[3].Set(wxACCEL_CTRL
, (int) 'A', wxID_SELECTALL
);
315 entries
[4].Set(wxACCEL_CTRL
, (int) 'Z', wxID_UNDO
);
316 entries
[5].Set(wxACCEL_CTRL
, (int) 'Y', wxID_REDO
);
318 wxAcceleratorTable
accel(6, entries
);
319 SetAcceleratorTable(accel
);
321 m_contextMenu
= new wxMenu
;
322 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
323 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
324 m_contextMenu
->AppendSeparator();
325 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
326 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
327 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
328 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
329 m_contextMenu
->AppendSeparator();
330 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
331 m_contextMenu
->AppendSeparator();
332 m_contextMenu
->Append(wxID_RICHTEXT_PROPERTIES1
, _("&Properties"));
334 #if wxUSE_DRAG_AND_DROP
335 SetDropTarget(new wxRichTextDropTarget(this));
341 wxRichTextCtrl::~wxRichTextCtrl()
343 SetFocusObject(& GetBuffer(), false);
344 GetBuffer().RemoveEventHandler(this);
346 delete m_contextMenu
;
349 /// Member initialisation
350 void wxRichTextCtrl::Init()
352 m_contextMenu
= NULL
;
354 m_caretPosition
= -1;
355 m_selectionAnchor
= -2;
356 m_selectionAnchorObject
= NULL
;
357 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
359 m_verticalScrollbarEnabled
= true;
360 m_caretAtLineStart
= false;
362 #if wxUSE_DRAG_AND_DROP
365 m_fullLayoutRequired
= false;
366 m_fullLayoutTime
= 0;
367 m_fullLayoutSavedPosition
= 0;
368 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
369 m_caretPositionForDefaultStyle
= -2;
370 m_focusObject
= & m_buffer
;
373 void wxRichTextCtrl::DoThaw()
375 if (GetBuffer().IsDirty())
384 void wxRichTextCtrl::Clear()
386 if (GetFocusObject() == & GetBuffer())
388 m_buffer
.ResetAndClearCommands();
389 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
393 GetFocusObject()->Reset();
396 m_caretPosition
= -1;
397 m_caretPositionForDefaultStyle
= -2;
398 m_caretAtLineStart
= false;
400 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
410 wxTextCtrl::SendTextUpdatedEvent(this);
414 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
416 #if !wxRICHTEXT_USE_OWN_CARET
417 if (GetCaret() && !IsFrozen())
420 // Stop the caret refreshing the control from within the
423 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(false);
427 #if wxRICHTEXT_BUFFERED_PAINTING
428 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
438 dc
.SetFont(GetFont());
440 // Paint the background
443 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
445 wxRect
drawingArea(GetUpdateRegion().GetBox());
446 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
448 wxRect
availableSpace(GetClientSize());
449 wxRichTextDrawingContext
context(& GetBuffer());
450 if (GetBuffer().IsDirty())
452 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
453 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
457 wxRect
clipRect(availableSpace
);
458 clipRect
.x
+= GetBuffer().GetLeftMargin();
459 clipRect
.y
+= GetBuffer().GetTopMargin();
460 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
461 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
462 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
463 dc
.SetClippingRegion(clipRect
);
466 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
467 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
469 GetBuffer().Draw(dc
, context
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
471 dc
.DestroyClippingRegion();
473 // Other user defined painting after everything else (i.e. all text) is painted
474 PaintAboveContent(dc
);
476 #if wxRICHTEXT_USE_OWN_CARET
477 if (GetCaret()->IsVisible())
480 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
485 #if !wxRICHTEXT_USE_OWN_CARET
491 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(true);
495 // Empty implementation, to prevent flicker
496 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
500 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
504 #if !wxRICHTEXT_USE_OWN_CARET
510 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
511 // Work around dropouts when control is focused
519 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
524 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
525 // Work around dropouts when control is focused
533 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
538 // Set up the caret for the given position and container, after a mouse click
539 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
541 bool caretAtLineStart
= false;
543 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
545 // If we're at the start of a line (but not first in para)
546 // then we should keep the caret showing at the start of the line
547 // by showing the m_caretAtLineStart flag.
548 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
549 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
551 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
552 caretAtLineStart
= true;
556 if (extendSelection
&& (m_caretPosition
!= position
))
557 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
559 MoveCaret(position
, caretAtLineStart
);
560 SetDefaultStyleToCursorStyle();
566 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
572 dc
.SetFont(GetFont());
574 // TODO: detect change of focus object
576 wxRichTextObject
* hitObj
= NULL
;
577 wxRichTextObject
* contextObj
= NULL
;
578 wxRichTextDrawingContext
context(& GetBuffer());
579 int hit
= GetBuffer().HitTest(dc
, context
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
);
581 #if wxUSE_DRAG_AND_DROP
582 // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop
583 if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position
))
585 // This might be an attempt at initiating Drag'n'Drop. So set the time & flags
587 m_dragStartPoint
= event
.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt
590 m_dragStartTime
= wxDateTime::UNow();
591 #endif // wxUSE_DATETIME
593 // Preserve behaviour of clicking on an object within the selection
594 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
597 return; // Don't skip the event, else the selection will be lost
599 #endif // wxUSE_DRAG_AND_DROP
601 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
603 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
604 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
605 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
607 SetFocusObject(container
, false /* don't set caret position yet */);
613 long oldCaretPos
= m_caretPosition
;
615 SetCaretPositionAfterClick(container
, position
, hit
);
617 // For now, don't handle shift-click when we're selecting multiple objects.
618 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
619 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
628 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
633 if (GetCapture() == this)
636 // See if we clicked on a URL
639 dc
.SetFont(GetFont());
642 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
643 wxRichTextObject
* hitObj
= NULL
;
644 wxRichTextObject
* contextObj
= NULL
;
645 wxRichTextDrawingContext
context(& GetBuffer());
646 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
647 int hit
= GetFocusObject()->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
649 #if wxUSE_DRAG_AND_DROP
652 // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick
653 m_preDrag
= false; // Tell DnD not to happen now: we are processing Left Up ourselves.
655 // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag
657 wxRichTextObject
* hitObj
= NULL
;
658 wxRichTextObject
* contextObj
= NULL
;
659 int hit
= GetBuffer().HitTest(dc
, context
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
);
660 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
661 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
662 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
664 SetFocusObject(container
, false /* don't set caret position yet */);
667 long oldCaretPos
= m_caretPosition
;
669 SetCaretPositionAfterClick(container
, position
, hit
);
671 // For now, don't handle shift-click when we're selecting multiple objects.
672 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
673 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
679 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
681 wxRichTextEvent
cmdEvent(
682 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
684 cmdEvent
.SetEventObject(this);
685 cmdEvent
.SetPosition(position
);
687 cmdEvent
.SetContainer(hitObj
->GetContainer());
689 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
692 if (GetStyle(position
, attr
))
694 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
696 wxString urlTarget
= attr
.GetURL();
697 if (!urlTarget
.IsEmpty())
699 wxMouseEvent
mouseEvent(event
);
701 long startPos
= 0, endPos
= 0;
702 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
705 startPos
= obj
->GetRange().GetStart();
706 endPos
= obj
->GetRange().GetEnd();
709 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
710 InitCommandEvent(urlEvent
);
712 urlEvent
.SetString(urlTarget
);
714 GetEventHandler()->ProcessEvent(urlEvent
);
722 #if wxUSE_DRAG_AND_DROP
724 #endif // wxUSE_DRAG_AND_DROP
726 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
727 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
729 // Put the selection in PRIMARY, if it exists
730 wxTheClipboard
->UsePrimarySelection(true);
732 wxRichTextRange range
= GetInternalSelectionRange();
733 GetFocusObject()->GetBuffer()->CopyToClipboard(range
);
735 wxTheClipboard
->UsePrimarySelection(false);
741 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
743 #if wxUSE_DRAG_AND_DROP
745 if (m_preDrag
|| m_dragging
)
747 int x
= m_dragStartPoint
.x
- event
.GetPosition().x
;
748 int y
= m_dragStartPoint
.y
- event
.GetPosition().y
;
749 distance
= abs(x
) + abs(y
);
752 // See if we're starting Drag'n'Drop
756 wxTimeSpan diff
= wxDateTime::UNow() - m_dragStartTime
;
760 && (diff
.GetMilliseconds() > 100)
767 wxRichTextRange range
= GetInternalSelectionRange();
768 if (range
== wxRICHTEXT_NONE
)
770 // Don't try to drag an empty range
775 // Cache the current situation, to be restored if Drag'n'Drop is cancelled
776 long oldPos
= GetCaretPosition();
777 wxRichTextParagraphLayoutBox
* oldFocus
= GetFocusObject();
779 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
780 wxString text
= GetFocusObject()->GetTextForRange(range
);
782 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
784 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
786 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
787 GetFocusObject()->CopyFragment(range
, *richTextBuf
);
788 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
790 wxRichTextDropSource
source(*compositeObject
, this);
791 // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise
792 // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed
793 BeginBatchUndo(_("Drag"));
794 switch (source
.DoDragDrop(wxDrag_AllowMove
| wxDrag_DefaultMove
))
797 case wxDragCopy
: break;
800 wxLogError(wxT("An error occurred during drag and drop operation"));
803 Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take'
804 SetCaretPosition(oldPos
);
805 SetFocusObject(oldFocus
, false);
814 #endif // wxUSE_DRAG_AND_DROP
818 dc
.SetFont(GetFont());
821 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
822 wxRichTextObject
* hitObj
= NULL
;
823 wxRichTextObject
* contextObj
= NULL
;
827 // If we're dragging, let's only consider positions at this level; otherwise
828 // selecting a range is not going to work.
829 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
832 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
833 container
= GetFocusObject();
835 wxRichTextDrawingContext
context(& GetBuffer());
836 int hit
= container
->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
, flags
);
838 // See if we need to change the cursor
841 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
843 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
845 ProcessMouseMovement(actualContainer
, hitObj
, position
, logicalPt
);
848 SetCursor(m_textCursor
);
851 if (!event
.Dragging())
858 #if wxUSE_DRAG_AND_DROP
864 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
865 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
866 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
868 // Check for dragging across multiple containers
870 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
871 int hit2
= GetBuffer().HitTest(dc
, context
, logicalPt
, position2
, & hitObj2
, & contextObj2
, 0);
872 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
874 // See if we can find a common ancestor
875 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
877 firstContainer
= GetFocusObject();
878 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
882 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
883 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
884 // is the common ancestor.
885 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
888 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
890 wxRichTextObject
* p
= hitObj2
;
893 if (p
->GetParent() == commonAncestor
)
895 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
902 if (commonAncestor
&& firstContainer
&& otherContainer
)
904 // We have now got a second container that shares a parent with the current or anchor object.
905 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
907 // Don't go into common-ancestor selection mode if we still have the same
909 if (otherContainer
!= firstContainer
)
911 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
912 m_selectionAnchorObject
= firstContainer
;
913 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
915 // The common ancestor, such as a table, returns the cell selection
916 // between the anchor and current position.
917 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
922 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
927 if (otherContainer
->AcceptsFocus())
928 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
929 MoveCaret(-1, false);
930 SetDefaultStyleToCursorStyle();
935 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
936 #if wxUSE_DRAG_AND_DROP
942 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
947 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
953 dc
.SetFont(GetFont());
956 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
957 wxRichTextObject
* hitObj
= NULL
;
958 wxRichTextObject
* contextObj
= NULL
;
959 wxRichTextDrawingContext
context(& GetBuffer());
960 int hit
= GetFocusObject()->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
);
962 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
964 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
965 if (actualContainer
&& actualContainer
->AcceptsFocus())
967 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
968 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
972 wxRichTextEvent
cmdEvent(
973 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
975 cmdEvent
.SetEventObject(this);
976 cmdEvent
.SetPosition(position
);
978 cmdEvent
.SetContainer(hitObj
->GetContainer());
980 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
984 /// Left-double-click
985 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
987 wxRichTextEvent
cmdEvent(
988 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
990 cmdEvent
.SetEventObject(this);
991 cmdEvent
.SetPosition(m_caretPosition
+1);
992 cmdEvent
.SetContainer(GetFocusObject());
994 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
996 SelectWord(GetCaretPosition()+1);
1001 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
1003 wxRichTextEvent
cmdEvent(
1004 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
1006 cmdEvent
.SetEventObject(this);
1007 cmdEvent
.SetPosition(m_caretPosition
+1);
1008 cmdEvent
.SetContainer(GetFocusObject());
1010 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1013 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1014 // Paste any PRIMARY selection, if it exists
1015 wxTheClipboard
->UsePrimarySelection(true);
1017 wxTheClipboard
->UsePrimarySelection(false);
1022 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
1025 if (event
.CmdDown())
1026 flags
|= wxRICHTEXT_CTRL_DOWN
;
1027 if (event
.ShiftDown())
1028 flags
|= wxRICHTEXT_SHIFT_DOWN
;
1029 if (event
.AltDown())
1030 flags
|= wxRICHTEXT_ALT_DOWN
;
1032 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
1034 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
1036 KeyboardNavigate(event
.GetKeyCode(), flags
);
1040 long keycode
= event
.GetKeyCode();
1100 case WXK_NUMPAD_HOME
:
1101 case WXK_NUMPAD_LEFT
:
1103 case WXK_NUMPAD_RIGHT
:
1104 case WXK_NUMPAD_DOWN
:
1105 case WXK_NUMPAD_PAGEUP
:
1106 case WXK_NUMPAD_PAGEDOWN
:
1107 case WXK_NUMPAD_END
:
1108 case WXK_NUMPAD_BEGIN
:
1109 case WXK_NUMPAD_INSERT
:
1110 case WXK_WINDOWS_LEFT
:
1119 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1120 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
1127 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1132 BeginBatchUndo(_("Delete Text"));
1134 long newPos
= m_caretPosition
;
1136 bool processed
= DeleteSelectedContent(& newPos
);
1142 // Submit range in character positions, which are greater than caret positions,
1143 // so subtract 1 for deleted character and add 1 for conversion to character position.
1146 if (event
.CmdDown())
1148 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1151 wxRichTextRange
range(pos
+1, newPos
);
1152 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1154 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1163 wxRichTextRange
range(newPos
, newPos
);
1164 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1166 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1174 if (GetLastPosition() == -1)
1176 GetFocusObject()->Reset();
1178 m_caretPosition
= -1;
1180 SetDefaultStyleToCursorStyle();
1183 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1185 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1186 //if (deletions > 0)
1188 wxRichTextEvent
cmdEvent(
1189 wxEVT_COMMAND_RICHTEXT_DELETE
,
1191 cmdEvent
.SetEventObject(this);
1192 cmdEvent
.SetFlags(flags
);
1193 cmdEvent
.SetPosition(m_caretPosition
+1);
1194 cmdEvent
.SetContainer(GetFocusObject());
1195 GetEventHandler()->ProcessEvent(cmdEvent
);
1206 // all the other keys modify the controls contents which shouldn't be
1207 // possible if we're read-only
1208 if ( !IsEditable() )
1214 if (event
.GetKeyCode() == WXK_RETURN
)
1216 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1219 long newPos
= m_caretPosition
;
1221 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1226 BeginBatchUndo(_("Insert Text"));
1228 DeleteSelectedContent(& newPos
);
1230 if (event
.ShiftDown())
1233 text
= wxRichTextLineBreakChar
;
1234 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, text
, this);
1235 m_caretAtLineStart
= true;
1239 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1242 SetDefaultStyleToCursorStyle();
1244 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1246 wxRichTextEvent
cmdEvent(
1247 wxEVT_COMMAND_RICHTEXT_RETURN
,
1249 cmdEvent
.SetEventObject(this);
1250 cmdEvent
.SetFlags(flags
);
1251 cmdEvent
.SetPosition(newPos
+1);
1252 cmdEvent
.SetContainer(GetFocusObject());
1254 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1256 // Generate conventional event
1257 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1258 InitCommandEvent(textEvent
);
1260 GetEventHandler()->ProcessEvent(textEvent
);
1264 else if (event
.GetKeyCode() == WXK_BACK
)
1266 long newPos
= m_caretPosition
;
1268 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1273 BeginBatchUndo(_("Delete Text"));
1275 bool processed
= DeleteSelectedContent(& newPos
);
1281 // Submit range in character positions, which are greater than caret positions,
1282 // so subtract 1 for deleted character and add 1 for conversion to character position.
1285 if (event
.CmdDown())
1287 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1290 wxRichTextRange
range(pos
+1, newPos
);
1291 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1293 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1302 wxRichTextRange
range(newPos
, newPos
);
1303 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1305 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1313 if (GetLastPosition() == -1)
1315 GetFocusObject()->Reset();
1317 m_caretPosition
= -1;
1319 SetDefaultStyleToCursorStyle();
1322 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1324 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1325 //if (deletions > 0)
1327 wxRichTextEvent
cmdEvent(
1328 wxEVT_COMMAND_RICHTEXT_DELETE
,
1330 cmdEvent
.SetEventObject(this);
1331 cmdEvent
.SetFlags(flags
);
1332 cmdEvent
.SetPosition(m_caretPosition
+1);
1333 cmdEvent
.SetContainer(GetFocusObject());
1334 GetEventHandler()->ProcessEvent(cmdEvent
);
1339 else if (event
.GetKeyCode() == WXK_DELETE
)
1341 long newPos
= m_caretPosition
;
1343 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1348 BeginBatchUndo(_("Delete Text"));
1350 bool processed
= DeleteSelectedContent(& newPos
);
1356 // Submit range in character positions, which are greater than caret positions,
1357 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1359 if (event
.CmdDown())
1361 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1362 if (pos
!= -1 && (pos
> newPos
))
1364 wxRichTextRange
range(newPos
+1, pos
);
1365 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1367 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1374 if (!processed
&& newPos
< (GetLastPosition()-1))
1376 wxRichTextRange
range(newPos
+1, newPos
+1);
1377 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1379 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1387 if (GetLastPosition() == -1)
1389 GetFocusObject()->Reset();
1391 m_caretPosition
= -1;
1393 SetDefaultStyleToCursorStyle();
1396 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1398 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1399 //if (deletions > 0)
1401 wxRichTextEvent
cmdEvent(
1402 wxEVT_COMMAND_RICHTEXT_DELETE
,
1404 cmdEvent
.SetEventObject(this);
1405 cmdEvent
.SetFlags(flags
);
1406 cmdEvent
.SetPosition(m_caretPosition
+1);
1407 cmdEvent
.SetContainer(GetFocusObject());
1408 GetEventHandler()->ProcessEvent(cmdEvent
);
1415 long keycode
= event
.GetKeyCode();
1427 if (event
.CmdDown())
1429 // Fixes AltGr+key with European input languages on Windows
1430 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1437 wxRichTextEvent
cmdEvent(
1438 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1440 cmdEvent
.SetEventObject(this);
1441 cmdEvent
.SetFlags(flags
);
1443 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1445 cmdEvent
.SetCharacter((wxChar
) keycode
);
1447 cmdEvent
.SetPosition(m_caretPosition
+1);
1448 cmdEvent
.SetContainer(GetFocusObject());
1450 if (keycode
== wxT('\t'))
1452 // See if we need to promote or demote the selection or paragraph at the cursor
1453 // position, instead of inserting a tab.
1454 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1455 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1456 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1458 wxRichTextRange range
;
1460 range
= GetSelectionRange();
1462 range
= para
->GetRange().FromInternal();
1464 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1466 PromoteList(promoteBy
, range
, NULL
);
1468 GetEventHandler()->ProcessEvent(cmdEvent
);
1474 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1477 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1480 BeginBatchUndo(_("Insert Text"));
1482 long newPos
= m_caretPosition
;
1483 DeleteSelectedContent(& newPos
);
1486 wxString str
= event
.GetUnicodeKey();
1488 wxString str
= (wxChar
) event
.GetKeyCode();
1490 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, str
, this, 0);
1494 SetDefaultStyleToCursorStyle();
1495 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1497 cmdEvent
.SetPosition(m_caretPosition
);
1498 GetEventHandler()->ProcessEvent(cmdEvent
);
1506 bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox
* container
, wxRichTextObject
* WXUNUSED(obj
), long position
, const wxPoint
& WXUNUSED(pos
))
1508 wxRichTextAttr attr
;
1509 if (container
&& GetStyle(position
, attr
, container
))
1511 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
1513 SetCursor(m_urlCursor
);
1515 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
1517 SetCursor(m_textCursor
);
1525 /// Delete content if there is a selection, e.g. when pressing a key.
1526 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1530 long pos
= m_selection
.GetRange().GetStart();
1531 wxRichTextRange range
= m_selection
.GetRange();
1533 // SelectAll causes more to be selected than doing it interactively,
1534 // and causes a new paragraph to be inserted. So for multiline buffers,
1535 // don't delete the final position.
1536 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1537 range
.SetEnd(range
.GetEnd()-1);
1539 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1540 m_selection
.Reset();
1541 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1551 /// Keyboard navigation
1555 Left: left one character
1556 Right: right one character
1559 Ctrl-Left: left one word
1560 Ctrl-Right: right one word
1561 Ctrl-Up: previous paragraph start
1562 Ctrl-Down: next start of paragraph
1565 Ctrl-Home: start of document
1566 Ctrl-End: end of document
1567 Page-Up: Up a screen
1568 Page-Down: Down a screen
1572 Ctrl-Alt-PgUp: Start of window
1573 Ctrl-Alt-PgDn: End of window
1574 F8: Start selection mode
1575 Esc: End selection mode
1577 Adding Shift does the above but starts/extends selection.
1582 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1584 bool success
= false;
1586 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1588 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1589 success
= WordRight(1, flags
);
1591 success
= MoveRight(1, flags
);
1593 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1595 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1596 success
= WordLeft(1, flags
);
1598 success
= MoveLeft(1, flags
);
1600 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1602 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1603 success
= MoveToParagraphStart(flags
);
1605 success
= MoveUp(1, flags
);
1607 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1609 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1610 success
= MoveToParagraphEnd(flags
);
1612 success
= MoveDown(1, flags
);
1614 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1616 success
= PageUp(1, flags
);
1618 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1620 success
= PageDown(1, flags
);
1622 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1624 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1625 success
= MoveHome(flags
);
1627 success
= MoveToLineStart(flags
);
1629 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1631 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1632 success
= MoveEnd(flags
);
1634 success
= MoveToLineEnd(flags
);
1639 ScrollIntoView(m_caretPosition
, keyCode
);
1640 SetDefaultStyleToCursorStyle();
1646 /// Extend the selection. Selections are in caret positions.
1647 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1649 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1651 if (oldPos
== newPos
)
1654 wxRichTextSelection oldSelection
= m_selection
;
1656 m_selection
.SetContainer(GetFocusObject());
1658 wxRichTextRange oldRange
;
1659 if (m_selection
.IsValid())
1660 oldRange
= m_selection
.GetRange();
1662 oldRange
= wxRICHTEXT_NO_SELECTION
;
1663 wxRichTextRange newRange
;
1665 // If not currently selecting, start selecting
1666 if (oldRange
.GetStart() == -2)
1668 m_selectionAnchor
= oldPos
;
1670 if (oldPos
> newPos
)
1671 newRange
.SetRange(newPos
+1, oldPos
);
1673 newRange
.SetRange(oldPos
+1, newPos
);
1677 // Always ensure that the selection range start is greater than
1679 if (newPos
> m_selectionAnchor
)
1680 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1681 else if (newPos
== m_selectionAnchor
)
1682 newRange
= wxRichTextRange(-2, -2);
1684 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1687 m_selection
.SetRange(newRange
);
1689 RefreshForSelectionChange(oldSelection
, m_selection
);
1691 if (newRange
.GetStart() > newRange
.GetEnd())
1693 wxLogDebug(wxT("Strange selection range"));
1702 /// Scroll into view, returning true if we scrolled.
1703 /// This takes a _caret_ position.
1704 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1706 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1712 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1714 int startXUnits
, startYUnits
;
1715 GetViewStart(& startXUnits
, & startYUnits
);
1716 int startY
= startYUnits
* ppuY
;
1719 GetVirtualSize(& sx
, & sy
);
1725 wxRect rect
= line
->GetRect();
1727 bool scrolled
= false;
1729 wxSize clientSize
= GetClientSize();
1731 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1734 wxClientDC
dc(this);
1735 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1736 topMargin
, bottomMargin
);
1738 // clientSize.y -= GetBuffer().GetBottomMargin();
1739 clientSize
.y
-= bottomMargin
;
1741 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1743 int y
= rect
.y
- GetClientSize().y
/2;
1744 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1745 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1747 if (startYUnits
!= yUnits
)
1749 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1752 #if !wxRICHTEXT_USE_OWN_CARET
1762 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1763 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1764 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1765 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1767 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1769 // Make it scroll so this item is at the bottom
1771 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1772 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1774 // If we're still off the screen, scroll another line down
1775 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1778 if (startYUnits
!= yUnits
)
1780 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1784 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1786 // Make it scroll so this item is at the top
1788 int y
= rect
.y
- GetBuffer().GetTopMargin();
1789 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1791 if (startYUnits
!= yUnits
)
1793 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1799 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1800 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1801 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1802 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1804 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1806 // Make it scroll so this item is at the top
1808 int y
= rect
.y
- GetBuffer().GetTopMargin();
1809 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1811 if (startYUnits
!= yUnits
)
1813 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1817 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1819 // Make it scroll so this item is at the bottom
1821 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1822 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1824 // If we're still off the screen, scroll another line down
1825 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1828 if (startYUnits
!= yUnits
)
1830 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1836 #if !wxRICHTEXT_USE_OWN_CARET
1844 /// Is the given position visible on the screen?
1845 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1847 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1853 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1856 GetViewStart(& startX
, & startY
);
1858 startY
= startY
* ppuY
;
1860 wxRect rect
= line
->GetRect();
1861 wxSize clientSize
= GetClientSize();
1862 clientSize
.y
-= GetBuffer().GetBottomMargin();
1864 return (rect
.GetTop() >= (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1867 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1869 m_caretPosition
= position
;
1870 m_caretAtLineStart
= showAtLineStart
;
1873 /// Move caret one visual step forward: this may mean setting a flag
1874 /// and keeping the same position if we're going from the end of one line
1875 /// to the start of the next, which may be the exact same caret position.
1876 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1878 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1880 // Only do the check if we're not at the end of the paragraph (where things work OK
1882 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1884 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1888 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1890 // We're at the end of a line. See whether we need to
1891 // stay at the same actual caret position but change visual
1892 // position, or not.
1893 if (oldPosition
== lineRange
.GetEnd())
1895 if (m_caretAtLineStart
)
1897 // We're already at the start of the line, so actually move on now.
1898 m_caretPosition
= oldPosition
+ 1;
1899 m_caretAtLineStart
= false;
1903 // We're showing at the end of the line, so keep to
1904 // the same position but indicate that we're to show
1905 // at the start of the next line.
1906 m_caretPosition
= oldPosition
;
1907 m_caretAtLineStart
= true;
1909 SetDefaultStyleToCursorStyle();
1915 SetDefaultStyleToCursorStyle();
1918 /// Move caret one visual step backward: this may mean setting a flag
1919 /// and keeping the same position if we're going from the end of one line
1920 /// to the start of the next, which may be the exact same caret position.
1921 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1923 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1925 // Only do the check if we're not at the start of the paragraph (where things work OK
1927 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1929 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1933 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1935 // We're at the start of a line. See whether we need to
1936 // stay at the same actual caret position but change visual
1937 // position, or not.
1938 if (oldPosition
== lineRange
.GetStart())
1940 m_caretPosition
= oldPosition
-1;
1941 m_caretAtLineStart
= true;
1944 else if (oldPosition
== lineRange
.GetEnd())
1946 if (m_caretAtLineStart
)
1948 // We're at the start of the line, so keep the same caret position
1949 // but clear the start-of-line flag.
1950 m_caretPosition
= oldPosition
;
1951 m_caretAtLineStart
= false;
1955 // We're showing at the end of the line, so go back
1956 // to the previous character position.
1957 m_caretPosition
= oldPosition
- 1;
1959 SetDefaultStyleToCursorStyle();
1965 SetDefaultStyleToCursorStyle();
1969 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1971 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1973 if (m_caretPosition
+ noPositions
< endPos
)
1975 long oldPos
= m_caretPosition
;
1976 long newPos
= m_caretPosition
+ noPositions
;
1978 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1982 // Determine by looking at oldPos and m_caretPosition whether
1983 // we moved from the end of a line to the start of the next line, in which case
1984 // we want to adjust the caret position such that it is positioned at the
1985 // start of the next line, rather than jumping past the first character of the
1987 if (noPositions
== 1 && !extendSel
)
1988 MoveCaretForward(oldPos
);
1990 SetCaretPosition(newPos
);
1993 SetDefaultStyleToCursorStyle();
2002 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
2006 if (m_caretPosition
> startPos
- noPositions
+ 1)
2008 long oldPos
= m_caretPosition
;
2009 long newPos
= m_caretPosition
- noPositions
;
2010 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2014 if (noPositions
== 1 && !extendSel
)
2015 MoveCaretBack(oldPos
);
2017 SetCaretPosition(newPos
);
2020 SetDefaultStyleToCursorStyle();
2028 // Find the caret position for the combination of hit-test flags and character position.
2029 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
2030 // since this is ambiguous (same position used for end of line and start of next).
2031 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
2032 bool& caretLineStart
)
2034 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
2035 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
2036 // so we view the caret at the start of the line.
2037 caretLineStart
= false;
2038 long caretPosition
= position
;
2040 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
2042 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
2043 wxRichTextRange lineRange
;
2045 lineRange
= thisLine
->GetAbsoluteRange();
2047 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
2050 caretLineStart
= true;
2054 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
2055 if (para
&& para
->GetRange().GetStart() == position
)
2059 return caretPosition
;
2063 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
2065 return MoveDown(- noLines
, flags
);
2069 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
2074 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
2075 wxPoint pt
= GetCaret()->GetPosition();
2076 long newLine
= lineNumber
+ noLines
;
2077 bool notInThisObject
= false;
2079 if (lineNumber
!= -1)
2083 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
2084 if (newLine
> lastLine
)
2085 notInThisObject
= true;
2090 notInThisObject
= true;
2094 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
2095 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
;
2097 if (notInThisObject
)
2099 // If we know we're navigating out of the current object,
2100 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2101 container
= & GetBuffer();
2102 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
2104 if (noLines
> 0) // going down
2106 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
2110 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
2115 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
2117 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
2123 wxClientDC
dc(this);
2125 dc
.SetFont(GetFont());
2127 wxRichTextObject
* hitObj
= NULL
;
2128 wxRichTextObject
* contextObj
= NULL
;
2129 wxRichTextDrawingContext
context(& GetBuffer());
2130 int hitTest
= container
->HitTest(dc
, context
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
2133 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
2134 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
2137 if (notInThisObject
)
2139 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2140 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
2142 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
2144 container
= actualContainer
;
2148 bool caretLineStart
= true;
2149 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
2150 long newSelEnd
= caretPosition
;
2153 if (notInThisObject
)
2156 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
2161 SetCaretPosition(caretPosition
, caretLineStart
);
2163 SetDefaultStyleToCursorStyle();
2171 /// Move to the end of the paragraph
2172 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
2174 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2177 long newPos
= para
->GetRange().GetEnd() - 1;
2178 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2182 SetCaretPosition(newPos
);
2184 SetDefaultStyleToCursorStyle();
2192 /// Move to the start of the paragraph
2193 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
2195 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2198 long newPos
= para
->GetRange().GetStart() - 1;
2199 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2203 SetCaretPosition(newPos
);
2205 SetDefaultStyleToCursorStyle();
2213 /// Move to the end of the line
2214 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
2216 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2220 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2221 long newPos
= lineRange
.GetEnd();
2222 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2226 SetCaretPosition(newPos
);
2228 SetDefaultStyleToCursorStyle();
2236 /// Move to the start of the line
2237 bool wxRichTextCtrl::MoveToLineStart(int flags
)
2239 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2242 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2243 long newPos
= lineRange
.GetStart()-1;
2245 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2249 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
2251 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2253 SetDefaultStyleToCursorStyle();
2261 /// Move to the start of the buffer
2262 bool wxRichTextCtrl::MoveHome(int flags
)
2264 if (m_caretPosition
!= -1)
2266 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2270 SetCaretPosition(-1);
2272 SetDefaultStyleToCursorStyle();
2280 /// Move to the end of the buffer
2281 bool wxRichTextCtrl::MoveEnd(int flags
)
2283 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2285 if (m_caretPosition
!= endPos
)
2287 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2291 SetCaretPosition(endPos
);
2293 SetDefaultStyleToCursorStyle();
2301 /// Move noPages pages up
2302 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2304 return PageDown(- noPages
, flags
);
2307 /// Move noPages pages down
2308 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2310 // Calculate which line occurs noPages * screen height further down.
2311 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2314 wxSize clientSize
= GetClientSize();
2315 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2317 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2320 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2321 long pos
= lineRange
.GetStart()-1;
2322 if (pos
!= m_caretPosition
)
2324 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2326 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2330 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2332 SetDefaultStyleToCursorStyle();
2342 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2344 return str
== wxT(" ") || str
== wxT("\t") || (!str
.empty() && (str
[0] == (wxChar
) 160));
2347 // Finds the caret position for the next word
2348 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2350 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2354 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2356 // First skip current text to space
2357 while (i
< endPos
&& i
> -1)
2359 // i is in character, not caret positions
2360 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2361 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2362 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2366 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2373 while (i
< endPos
&& i
> -1)
2375 // i is in character, not caret positions
2376 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2377 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2378 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2379 return wxMax(-1, i
);
2381 if (text
.empty()) // End of paragraph, or maybe an image
2382 return wxMax(-1, i
- 1);
2383 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2387 // Convert to caret position
2388 return wxMax(-1, i
- 1);
2397 long i
= m_caretPosition
;
2399 // First skip white space
2400 while (i
< endPos
&& i
> -1)
2402 // i is in character, not caret positions
2403 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2404 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2406 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2408 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2413 // Next skip current text to space
2414 while (i
< endPos
&& i
> -1)
2416 // i is in character, not caret positions
2417 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2418 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2419 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2422 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2435 /// Move n words left
2436 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2438 long pos
= FindNextWordPosition(-1);
2439 if (pos
!= m_caretPosition
)
2441 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2443 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2447 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2449 SetDefaultStyleToCursorStyle();
2457 /// Move n words right
2458 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2460 long pos
= FindNextWordPosition(1);
2461 if (pos
!= m_caretPosition
)
2463 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2465 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2469 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2471 SetDefaultStyleToCursorStyle();
2480 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2482 // Only do sizing optimization for large buffers
2483 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2485 m_fullLayoutRequired
= true;
2486 m_fullLayoutTime
= wxGetLocalTimeMillis();
2487 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2488 LayoutContent(true /* onlyVisibleRect */);
2491 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2493 #if wxRICHTEXT_BUFFERED_PAINTING
2500 // Force any pending layout due to large buffer
2501 void wxRichTextCtrl::ForceDelayedLayout()
2503 if (m_fullLayoutRequired
)
2505 m_fullLayoutRequired
= false;
2506 m_fullLayoutTime
= 0;
2507 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2508 ShowPosition(m_fullLayoutSavedPosition
);
2514 /// Idle-time processing
2515 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2517 #if wxRICHTEXT_USE_OWN_CARET
2518 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2520 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2526 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2528 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2530 m_fullLayoutRequired
= false;
2531 m_fullLayoutTime
= 0;
2532 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2533 ShowPosition(m_fullLayoutSavedPosition
);
2537 if (m_caretPositionForDefaultStyle
!= -2)
2539 // If the caret position has changed, no longer reflect the default style
2541 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2542 m_caretPositionForDefaultStyle
= -2;
2549 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2551 #if wxRICHTEXT_USE_OWN_CARET
2552 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2555 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2562 /// Set up scrollbars, e.g. after a resize
2563 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2568 if (GetBuffer().IsEmpty() || !m_verticalScrollbarEnabled
)
2570 SetScrollbars(0, 0, 0, 0, 0, 0);
2574 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2575 // of pixels. See e.g. wxVScrolledWindow for ideas.
2576 int pixelsPerUnit
= 5;
2577 wxSize clientSize
= GetClientSize();
2579 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2581 // Round up so we have at least maxHeight pixels
2582 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2584 int startX
= 0, startY
= 0;
2586 GetViewStart(& startX
, & startY
);
2588 int maxPositionX
= 0;
2589 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2591 int newStartX
= wxMin(maxPositionX
, startX
);
2592 int newStartY
= wxMin(maxPositionY
, startY
);
2594 int oldPPUX
, oldPPUY
;
2595 int oldStartX
, oldStartY
;
2596 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2597 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2598 GetViewStart(& oldStartX
, & oldStartY
);
2599 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2601 oldVirtualSizeY
/= oldPPUY
;
2603 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2606 // Don't set scrollbars if there were none before, and there will be none now.
2607 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2610 // Move to previous scroll position if
2612 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2615 /// Paint the background
2616 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2618 wxColour backgroundColour
= GetBackgroundColour();
2619 if (!backgroundColour
.IsOk())
2620 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2622 // Clear the background
2623 dc
.SetBrush(wxBrush(backgroundColour
));
2624 dc
.SetPen(*wxTRANSPARENT_PEN
);
2625 wxRect
windowRect(GetClientSize());
2626 windowRect
.x
-= 2; windowRect
.y
-= 2;
2627 windowRect
.width
+= 4; windowRect
.height
+= 4;
2629 // We need to shift the rectangle to take into account
2630 // scrolling. Converting device to logical coordinates.
2631 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2632 dc
.DrawRectangle(windowRect
);
2635 #if wxRICHTEXT_BUFFERED_PAINTING
2636 /// Recreate buffer bitmap if necessary
2637 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2640 if (sz
== wxDefaultSize
)
2641 sz
= GetClientSize();
2643 if (sz
.x
< 1 || sz
.y
< 1)
2646 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2647 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2648 return m_bufferBitmap
.IsOk();
2652 // ----------------------------------------------------------------------------
2653 // file IO functions
2654 // ----------------------------------------------------------------------------
2656 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2658 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2660 m_filename
= filename
;
2663 SetInsertionPoint(0);
2666 SetupScrollbars(true);
2668 wxTextCtrl::SendTextUpdatedEvent(this);
2674 wxLogError(_("File couldn't be loaded."));
2680 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2682 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2684 m_filename
= filename
;
2691 wxLogError(_("The text couldn't be saved."));
2696 // ----------------------------------------------------------------------------
2697 // wxRichTextCtrl specific functionality
2698 // ----------------------------------------------------------------------------
2700 /// Add a new paragraph of text to the end of the buffer
2701 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2703 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2704 GetBuffer().Invalidate();
2710 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2712 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2713 GetBuffer().Invalidate();
2718 // ----------------------------------------------------------------------------
2719 // selection and ranges
2720 // ----------------------------------------------------------------------------
2722 void wxRichTextCtrl::SelectAll()
2724 SetSelection(-1, -1);
2728 void wxRichTextCtrl::SelectNone()
2730 if (m_selection
.IsValid())
2732 wxRichTextSelection oldSelection
= m_selection
;
2734 m_selection
.Reset();
2736 RefreshForSelectionChange(oldSelection
, m_selection
);
2738 m_selectionAnchor
= -2;
2739 m_selectionAnchorObject
= NULL
;
2740 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2743 static bool wxIsWordDelimiter(const wxString
& text
)
2745 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2748 /// Select the word at the given character position
2749 bool wxRichTextCtrl::SelectWord(long position
)
2751 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2754 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2758 if (position
== para
->GetRange().GetEnd())
2761 long positionStart
= position
;
2762 long positionEnd
= position
;
2764 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2766 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2767 if (wxIsWordDelimiter(text
))
2773 if (positionStart
< para
->GetRange().GetStart())
2774 positionStart
= para
->GetRange().GetStart();
2776 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2778 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2779 if (wxIsWordDelimiter(text
))
2785 if (positionEnd
>= para
->GetRange().GetEnd())
2786 positionEnd
= para
->GetRange().GetEnd();
2788 if (positionEnd
< positionStart
)
2791 SetSelection(positionStart
, positionEnd
+1);
2793 if (positionStart
>= 0)
2795 MoveCaret(positionStart
-1, true);
2796 SetDefaultStyleToCursorStyle();
2802 wxString
wxRichTextCtrl::GetStringSelection() const
2805 GetSelection(&from
, &to
);
2807 return GetRange(from
, to
);
2810 // ----------------------------------------------------------------------------
2812 // ----------------------------------------------------------------------------
2814 wxTextCtrlHitTestResult
2815 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2817 // implement in terms of the other overload as the native ports typically
2818 // can get the position and not (x, y) pair directly (although wxUniv
2819 // directly gets x and y -- and so overrides this method as well)
2821 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2823 if ( rc
!= wxTE_HT_UNKNOWN
)
2825 PositionToXY(pos
, x
, y
);
2831 wxTextCtrlHitTestResult
2832 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2835 wxClientDC
dc((wxRichTextCtrl
*) this);
2836 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2838 // Buffer uses logical position (relative to start of buffer)
2840 wxPoint pt2
= GetLogicalPoint(pt
);
2842 wxRichTextObject
* hitObj
= NULL
;
2843 wxRichTextObject
* contextObj
= NULL
;
2844 wxRichTextDrawingContext
context((wxRichTextBuffer
*) & GetBuffer());
2845 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, context
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2847 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2848 return wxTE_HT_BEFORE
;
2849 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2850 return wxTE_HT_BEYOND
;
2851 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2852 return wxTE_HT_ON_TEXT
;
2854 return wxTE_HT_UNKNOWN
;
2857 wxRichTextParagraphLayoutBox
*
2858 wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt
, long& position
, int& hit
, wxRichTextObject
* hitObj
, int flags
/* = 0*/)
2860 wxClientDC
dc(this);
2862 dc
.SetFont(GetFont());
2864 wxPoint logicalPt
= GetLogicalPoint(pt
);
2866 wxRichTextObject
* contextObj
= NULL
;
2867 wxRichTextDrawingContext
context(& GetBuffer());
2868 hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, &hitObj
, &contextObj
, flags
);
2869 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2875 // ----------------------------------------------------------------------------
2876 // set/get the controls text
2877 // ----------------------------------------------------------------------------
2879 wxString
wxRichTextCtrl::DoGetValue() const
2881 return GetBuffer().GetText();
2884 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2886 // Public API for range is different from internals
2887 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2890 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2892 // Don't call Clear here, since it always sends a text updated event
2893 m_buffer
.ResetAndClearCommands();
2894 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2895 m_caretPosition
= -1;
2896 m_caretPositionForDefaultStyle
= -2;
2897 m_caretAtLineStart
= false;
2898 m_selection
.Reset();
2899 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2909 if (!value
.IsEmpty())
2911 // Remove empty paragraph
2912 GetBuffer().Clear();
2913 DoWriteText(value
, flags
);
2915 // for compatibility, don't move the cursor when doing SetValue()
2916 SetInsertionPoint(0);
2920 // still send an event for consistency
2921 if (flags
& SetValue_SendEvent
)
2922 wxTextCtrl::SendTextUpdatedEvent(this);
2927 void wxRichTextCtrl::WriteText(const wxString
& value
)
2932 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2934 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2936 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2938 if ( flags
& SetValue_SendEvent
)
2939 wxTextCtrl::SendTextUpdatedEvent(this);
2942 void wxRichTextCtrl::AppendText(const wxString
& text
)
2944 SetInsertionPointEnd();
2949 /// Write an image at the current insertion point
2950 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2952 wxRichTextImageBlock imageBlock
;
2954 wxImage image2
= image
;
2955 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2956 return WriteImage(imageBlock
, textAttr
);
2961 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2963 wxRichTextImageBlock imageBlock
;
2966 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2967 return WriteImage(imageBlock
, textAttr
);
2972 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2974 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition
+1, imageBlock
, this, 0, textAttr
);
2977 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2981 wxRichTextImageBlock imageBlock
;
2983 wxImage image
= bitmap
.ConvertToImage();
2984 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2985 return WriteImage(imageBlock
, textAttr
);
2991 // Write a text box at the current insertion point.
2992 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2994 wxRichTextBox
* textBox
= new wxRichTextBox
;
2995 textBox
->SetAttributes(textAttr
);
2996 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2997 textBox
->AddParagraph(wxEmptyString
);
2998 textBox
->SetParent(NULL
);
3000 // The object returned is the one actually inserted into the buffer,
3001 // while the original one is deleted.
3002 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, textBox
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3003 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
3007 // Write a table at the current insertion point, returning the table.
3008 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
3010 wxASSERT(rows
> 0 && cols
> 0);
3012 if (rows
<= 0 || cols
<= 0)
3015 wxRichTextTable
* table
= new wxRichTextTable
;
3016 table
->SetAttributes(tableAttr
);
3017 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3019 table
->CreateTable(rows
, cols
);
3021 table
->SetParent(NULL
);
3024 for (j
= 0; j
< rows
; j
++)
3026 for (i
= 0; i
< cols
; i
++)
3028 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
3032 // The object returned is the one actually inserted into the buffer,
3033 // while the original one is deleted.
3034 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, table
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3035 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
3040 /// Insert a newline (actually paragraph) at the current insertion point.
3041 bool wxRichTextCtrl::Newline()
3043 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3046 /// Insert a line break at the current insertion point.
3047 bool wxRichTextCtrl::LineBreak()
3050 text
= wxRichTextLineBreakChar
;
3051 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, text
, this);
3054 // ----------------------------------------------------------------------------
3055 // Clipboard operations
3056 // ----------------------------------------------------------------------------
3058 void wxRichTextCtrl::Copy()
3062 wxRichTextRange range
= GetInternalSelectionRange();
3063 GetBuffer().CopyToClipboard(range
);
3067 void wxRichTextCtrl::Cut()
3071 wxRichTextRange range
= GetInternalSelectionRange();
3072 GetBuffer().CopyToClipboard(range
);
3074 DeleteSelectedContent();
3080 void wxRichTextCtrl::Paste()
3084 BeginBatchUndo(_("Paste"));
3086 long newPos
= m_caretPosition
;
3087 DeleteSelectedContent(& newPos
);
3089 GetBuffer().PasteFromClipboard(newPos
);
3095 void wxRichTextCtrl::DeleteSelection()
3097 if (CanDeleteSelection())
3099 DeleteSelectedContent();
3103 bool wxRichTextCtrl::HasSelection() const
3105 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
3108 bool wxRichTextCtrl::HasUnfocusedSelection() const
3110 return m_selection
.IsValid();
3113 bool wxRichTextCtrl::CanCopy() const
3115 // Can copy if there's a selection
3116 return HasSelection();
3119 bool wxRichTextCtrl::CanCut() const
3121 return CanDeleteSelection();
3124 bool wxRichTextCtrl::CanPaste() const
3126 if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
3129 return GetBuffer().CanPasteFromClipboard();
3132 bool wxRichTextCtrl::CanDeleteSelection() const
3134 return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange());
3138 // ----------------------------------------------------------------------------
3140 // ----------------------------------------------------------------------------
3142 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
3144 if (m_contextMenu
&& m_contextMenu
!= menu
)
3145 delete m_contextMenu
;
3146 m_contextMenu
= menu
;
3149 void wxRichTextCtrl::SetEditable(bool editable
)
3151 m_editable
= editable
;
3154 void wxRichTextCtrl::SetInsertionPoint(long pos
)
3158 m_caretPosition
= pos
- 1;
3162 SetDefaultStyleToCursorStyle();
3165 void wxRichTextCtrl::SetInsertionPointEnd()
3167 long pos
= GetLastPosition();
3168 SetInsertionPoint(pos
);
3171 long wxRichTextCtrl::GetInsertionPoint() const
3173 return m_caretPosition
+1;
3176 wxTextPos
wxRichTextCtrl::GetLastPosition() const
3178 return GetFocusObject()->GetOwnRange().GetEnd();
3181 // If the return values from and to are the same, there is no
3183 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
3185 if (m_selection
.IsValid())
3187 *from
= m_selection
.GetRange().GetStart();
3188 *to
= m_selection
.GetRange().GetEnd();
3198 bool wxRichTextCtrl::IsEditable() const
3203 // ----------------------------------------------------------------------------
3205 // ----------------------------------------------------------------------------
3207 void wxRichTextCtrl::SetSelection(long from
, long to
)
3209 // if from and to are both -1, it means (in wxWidgets) that all text should
3211 if ( (from
== -1) && (to
== -1) )
3214 to
= GetLastPosition()+1;
3223 wxRichTextSelection oldSelection
= m_selection
;
3225 m_selectionAnchor
= from
-1;
3226 m_selectionAnchorObject
= NULL
;
3227 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
3229 m_caretPosition
= wxMax(-1, to
-1);
3231 RefreshForSelectionChange(oldSelection
, m_selection
);
3236 // ----------------------------------------------------------------------------
3238 // ----------------------------------------------------------------------------
3240 void wxRichTextCtrl::Replace(long from
, long to
,
3241 const wxString
& value
)
3243 BeginBatchUndo(_("Replace"));
3245 SetSelection(from
, to
);
3247 wxRichTextAttr attr
= GetDefaultStyle();
3249 DeleteSelectedContent();
3251 SetDefaultStyle(attr
);
3253 DoWriteText(value
, SetValue_SelectionOnly
);
3258 void wxRichTextCtrl::Remove(long from
, long to
)
3262 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
3269 bool wxRichTextCtrl::IsModified() const
3271 return m_buffer
.IsModified();
3274 void wxRichTextCtrl::MarkDirty()
3276 m_buffer
.Modify(true);
3279 void wxRichTextCtrl::DiscardEdits()
3281 m_caretPositionForDefaultStyle
= -2;
3282 m_buffer
.Modify(false);
3283 m_buffer
.GetCommandProcessor()->ClearCommands();
3286 int wxRichTextCtrl::GetNumberOfLines() const
3288 return GetFocusObject()->GetParagraphCount();
3291 // ----------------------------------------------------------------------------
3292 // Positions <-> coords
3293 // ----------------------------------------------------------------------------
3295 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3297 return GetFocusObject()->XYToPosition(x
, y
);
3300 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3302 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3305 // ----------------------------------------------------------------------------
3307 // ----------------------------------------------------------------------------
3309 void wxRichTextCtrl::ShowPosition(long pos
)
3311 if (!IsPositionVisible(pos
))
3312 ScrollIntoView(pos
-1, WXK_DOWN
);
3315 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3317 return GetFocusObject()->GetParagraphLength(lineNo
);
3320 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3322 return GetFocusObject()->GetParagraphText(lineNo
);
3325 // ----------------------------------------------------------------------------
3327 // ----------------------------------------------------------------------------
3329 void wxRichTextCtrl::Undo()
3333 GetCommandProcessor()->Undo();
3337 void wxRichTextCtrl::Redo()
3341 GetCommandProcessor()->Redo();
3345 bool wxRichTextCtrl::CanUndo() const
3347 return GetCommandProcessor()->CanUndo() && IsEditable();
3350 bool wxRichTextCtrl::CanRedo() const
3352 return GetCommandProcessor()->CanRedo() && IsEditable();
3355 // ----------------------------------------------------------------------------
3356 // implementation details
3357 // ----------------------------------------------------------------------------
3359 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3361 SetValue(event
.GetString());
3362 GetEventHandler()->ProcessEvent(event
);
3365 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3367 // By default, load the first file into the text window.
3368 if (event
.GetNumberOfFiles() > 0)
3370 LoadFile(event
.GetFiles()[0]);
3374 wxSize
wxRichTextCtrl::DoGetBestSize() const
3376 return wxSize(10, 10);
3379 // ----------------------------------------------------------------------------
3380 // standard handlers for standard edit menu events
3381 // ----------------------------------------------------------------------------
3383 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3388 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3393 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3398 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3403 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3408 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3413 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3415 event
.Enable( CanCut() );
3418 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3420 event
.Enable( CanCopy() );
3423 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3425 event
.Enable( CanDeleteSelection() );
3428 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3430 event
.Enable( CanPaste() );
3433 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3435 event
.Enable( CanUndo() );
3436 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3439 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3441 event
.Enable( CanRedo() );
3442 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3445 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3447 if (GetLastPosition() > 0)
3451 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3453 event
.Enable(GetLastPosition() > 0);
3456 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3458 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3459 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3461 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3462 if (obj
&& CanEditProperties(obj
))
3463 EditProperties(obj
, this);
3465 m_contextMenuPropertiesInfo
.Clear();
3469 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3471 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3472 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3475 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3477 if (event
.GetEventObject() != this)
3483 ShowContextMenu(m_contextMenu
, event
.GetPosition());
3486 // Prepares the context menu, adding appropriate property-editing commands.
3487 // Returns the number of property commands added.
3488 int wxRichTextCtrl::PrepareContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3490 wxClientDC
dc(this);
3492 dc
.SetFont(GetFont());
3494 m_contextMenuPropertiesInfo
.Clear();
3497 wxRichTextObject
* hitObj
= NULL
;
3498 wxRichTextObject
* contextObj
= NULL
;
3499 if (pt
!= wxDefaultPosition
)
3501 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3502 wxRichTextDrawingContext
context(& GetBuffer());
3503 int hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
);
3505 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3507 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3508 if (hitObj
&& actualContainer
)
3510 if (actualContainer
->AcceptsFocus())
3512 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3513 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3516 if (addPropertyCommands
)
3517 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3521 if (addPropertyCommands
)
3522 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3527 if (addPropertyCommands
)
3528 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3533 // Invoked from the keyboard, so don't set the caret position and don't use the event
3535 hitObj
= GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition
+1);
3537 contextObj
= hitObj
->GetParentContainer();
3539 contextObj
= GetFocusObject();
3541 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3542 if (hitObj
&& actualContainer
)
3544 if (addPropertyCommands
)
3545 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3549 if (addPropertyCommands
)
3550 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3556 if (addPropertyCommands
)
3557 m_contextMenuPropertiesInfo
.AddMenuItems(menu
);
3558 return m_contextMenuPropertiesInfo
.GetCount();
3564 // Shows the context menu, adding appropriate property-editing commands
3565 bool wxRichTextCtrl::ShowContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3569 PrepareContextMenu(menu
, pt
, addPropertyCommands
);
3577 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3579 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3582 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3584 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3587 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3589 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3592 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3594 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3597 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
, int flags
)
3599 GetFocusObject()->SetStyle(obj
, textAttr
, flags
);
3602 // extended style setting operation with flags including:
3603 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3604 // see richtextbuffer.h for more details.
3606 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3608 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3611 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3613 return GetBuffer().SetDefaultStyle(style
);
3616 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3618 wxRichTextAttr
attr1(style
);
3619 attr1
.GetTextBoxAttr().Reset();
3620 return GetBuffer().SetDefaultStyle(attr1
);
3623 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3625 return GetBuffer().GetDefaultStyle();
3628 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3630 wxRichTextAttr attr
;
3631 if (GetFocusObject()->GetStyle(position
, attr
))
3640 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3642 return GetFocusObject()->GetStyle(position
, style
);
3645 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3647 wxRichTextAttr attr
;
3648 if (container
->GetStyle(position
, attr
))
3657 // get the common set of styles for the range
3658 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3660 wxRichTextAttr attr
;
3661 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3670 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3672 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3675 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3677 return container
->GetStyleForRange(range
.ToInternal(), style
);
3680 /// Get the content (uncombined) attributes for this position.
3681 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3683 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3686 /// Get the content (uncombined) attributes for this position.
3687 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3689 return container
->GetUncombinedStyle(position
, style
);
3692 bool wxRichTextCtrl::SetProperties(const wxRichTextRange
& range
, const wxRichTextProperties
& properties
, int flags
)
3694 return GetFocusObject()->SetProperties(range
.ToInternal(), properties
, flags
);
3697 /// Set font, and also the buffer attributes
3698 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3700 wxControl::SetFont(font
);
3702 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3704 GetBuffer().SetBasicStyle(attr
);
3706 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3712 /// Transform logical to physical
3713 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3716 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3721 /// Transform physical to logical
3722 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3725 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3730 /// Position the caret
3731 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3736 //wxLogDebug(wxT("PositionCaret"));
3739 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3741 wxPoint newPt
= caretRect
.GetPosition();
3742 wxSize newSz
= caretRect
.GetSize();
3743 wxPoint pt
= GetPhysicalPoint(newPt
);
3744 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3747 if (GetCaret()->GetSize() != newSz
)
3748 GetCaret()->SetSize(newSz
);
3750 // Adjust size so the caret size and position doesn't appear in the margins
3751 if (((pt
.y
+ newSz
.y
) <= GetBuffer().GetTopMargin()) || (pt
.y
>= (GetClientSize().y
- GetBuffer().GetBottomMargin())))
3756 else if (pt
.y
< GetBuffer().GetTopMargin() && (pt
.y
+ newSz
.y
) > GetBuffer().GetTopMargin())
3758 newSz
.y
-= (GetBuffer().GetTopMargin() - pt
.y
);
3761 pt
.y
= GetBuffer().GetTopMargin();
3762 GetCaret()->SetSize(newSz
);
3765 else if (pt
.y
< (GetClientSize().y
- GetBuffer().GetBottomMargin()) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- GetBuffer().GetBottomMargin()))
3767 newSz
.y
= GetClientSize().y
- GetBuffer().GetBottomMargin() - pt
.y
;
3768 GetCaret()->SetSize(newSz
);
3771 GetCaret()->Move(pt
);
3777 /// Get the caret height and position for the given character position
3778 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3780 wxClientDC
dc(this);
3781 dc
.SetFont(GetFont());
3789 container
= GetFocusObject();
3791 wxRichTextDrawingContext
context(& GetBuffer());
3792 if (container
->FindPosition(dc
, context
, position
, pt
, & height
, m_caretAtLineStart
))
3794 // Caret height can't be zero
3796 height
= dc
.GetCharHeight();
3798 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3805 /// Gets the line for the visible caret position. If the caret is
3806 /// shown at the very end of the line, it means the next character is actually
3807 /// on the following line. So let's get the line we're expecting to find
3808 /// if this is the case.
3809 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3811 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3812 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3815 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3816 if (caretPosition
== lineRange
.GetStart()-1 &&
3817 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3819 if (!m_caretAtLineStart
)
3820 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3827 /// Move the caret to the given character position
3828 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3830 if (GetBuffer().IsDirty())
3834 container
= GetFocusObject();
3836 if (pos
<= container
->GetOwnRange().GetEnd())
3838 SetCaretPosition(pos
, showAtLineStart
);
3840 PositionCaret(container
);
3848 /// Layout the buffer: which we must do before certain operations, such as
3849 /// setting the caret position.
3850 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3852 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3854 wxRect
availableSpace(GetClientSize());
3855 if (availableSpace
.width
== 0)
3856 availableSpace
.width
= 10;
3857 if (availableSpace
.height
== 0)
3858 availableSpace
.height
= 10;
3860 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3861 if (onlyVisibleRect
)
3863 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3864 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3867 wxClientDC
dc(this);
3868 dc
.SetFont(GetFont());
3872 wxRichTextDrawingContext
context(& GetBuffer());
3873 GetBuffer().Defragment();
3874 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3875 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, flags
);
3876 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3885 /// Is all of the selection, or the current caret position, bold?
3886 bool wxRichTextCtrl::IsSelectionBold()
3890 wxRichTextAttr attr
;
3891 wxRichTextRange range
= GetSelectionRange();
3892 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3893 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3895 return HasCharacterAttributes(range
, attr
);
3899 // If no selection, then we need to combine current style with default style
3900 // to see what the effect would be if we started typing.
3901 wxRichTextAttr attr
;
3902 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3904 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3905 if (GetStyle(pos
, attr
))
3907 if (IsDefaultStyleShowing())
3908 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3909 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3915 /// Is all of the selection, or the current caret position, italics?
3916 bool wxRichTextCtrl::IsSelectionItalics()
3920 wxRichTextRange range
= GetSelectionRange();
3921 wxRichTextAttr attr
;
3922 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3923 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3925 return HasCharacterAttributes(range
, attr
);
3929 // If no selection, then we need to combine current style with default style
3930 // to see what the effect would be if we started typing.
3931 wxRichTextAttr attr
;
3932 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3934 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3935 if (GetStyle(pos
, attr
))
3937 if (IsDefaultStyleShowing())
3938 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3939 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3945 /// Is all of the selection, or the current caret position, underlined?
3946 bool wxRichTextCtrl::IsSelectionUnderlined()
3950 wxRichTextRange range
= GetSelectionRange();
3951 wxRichTextAttr attr
;
3952 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3953 attr
.SetFontUnderlined(true);
3955 return HasCharacterAttributes(range
, attr
);
3959 // If no selection, then we need to combine current style with default style
3960 // to see what the effect would be if we started typing.
3961 wxRichTextAttr attr
;
3962 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3963 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3965 if (GetStyle(pos
, attr
))
3967 if (IsDefaultStyleShowing())
3968 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3969 return attr
.GetFontUnderlined();
3975 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3976 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
3978 wxRichTextAttr attr
;
3979 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3980 attr
.SetTextEffectFlags(flag
);
3981 attr
.SetTextEffects(flag
);
3985 return HasCharacterAttributes(GetSelectionRange(), attr
);
3989 // If no selection, then we need to combine current style with default style
3990 // to see what the effect would be if we started typing.
3991 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3992 if (GetStyle(pos
, attr
))
3994 if (IsDefaultStyleShowing())
3995 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3996 return (attr
.GetTextEffectFlags() & flag
) != 0;
4002 /// Apply bold to the selection
4003 bool wxRichTextCtrl::ApplyBoldToSelection()
4005 wxRichTextAttr attr
;
4006 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
4007 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
4010 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4013 wxRichTextAttr current
= GetDefaultStyleEx();
4014 current
.Apply(attr
);
4015 SetAndShowDefaultStyle(current
);
4020 /// Apply italic to the selection
4021 bool wxRichTextCtrl::ApplyItalicToSelection()
4023 wxRichTextAttr attr
;
4024 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
4025 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
4028 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4031 wxRichTextAttr current
= GetDefaultStyleEx();
4032 current
.Apply(attr
);
4033 SetAndShowDefaultStyle(current
);
4038 /// Apply underline to the selection
4039 bool wxRichTextCtrl::ApplyUnderlineToSelection()
4041 wxRichTextAttr attr
;
4042 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4043 attr
.SetFontUnderlined(!IsSelectionUnderlined());
4046 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4049 wxRichTextAttr current
= GetDefaultStyleEx();
4050 current
.Apply(attr
);
4051 SetAndShowDefaultStyle(current
);
4056 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
4057 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
4059 wxRichTextAttr attr
;
4060 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4061 attr
.SetTextEffectFlags(flags
);
4062 if (!DoesSelectionHaveTextEffectFlag(flags
))
4063 attr
.SetTextEffects(flags
);
4065 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
4068 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4071 wxRichTextAttr current
= GetDefaultStyleEx();
4072 current
.Apply(attr
);
4073 SetAndShowDefaultStyle(current
);
4078 /// Is all of the selection aligned according to the specified flag?
4079 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
4081 wxRichTextRange range
;
4083 range
= GetSelectionRange();
4085 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
4087 wxRichTextAttr attr
;
4088 attr
.SetAlignment(alignment
);
4090 return HasParagraphAttributes(range
, attr
);
4093 /// Apply alignment to the selection
4094 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
4096 wxRichTextAttr attr
;
4097 attr
.SetAlignment(alignment
);
4099 return SetStyle(GetSelectionRange(), attr
);
4102 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
4104 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
4109 /// Apply a named style to the selection
4110 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
4112 // Flags are defined within each definition, so only certain
4113 // attributes are applied.
4114 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
4116 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
4118 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
4120 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4122 wxRichTextRange range
;
4125 range
= GetSelectionRange();
4128 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4129 range
= wxRichTextRange(pos
, pos
+1);
4132 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
4135 bool isPara
= false;
4137 // Make sure the attr has the style name
4138 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
4141 attr
.SetParagraphStyleName(def
->GetName());
4143 // If applying a paragraph style, we only want the paragraph nodes to adopt these
4144 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
4145 // to change its style independently.
4146 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4148 else if (def
->IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition
)))
4149 attr
.SetCharacterStyleName(def
->GetName());
4150 else if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4151 attr
.GetTextBoxAttr().SetBoxStyleName(def
->GetName());
4153 if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4155 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4157 SetStyle(GetFocusObject(), attr
);
4163 else if (HasSelection())
4164 return SetStyleEx(GetSelectionRange(), attr
, flags
);
4167 wxRichTextAttr current
= GetDefaultStyleEx();
4168 wxRichTextAttr
defaultStyle(attr
);
4171 // Don't apply extra character styles since they are already implied
4172 // in the paragraph style
4173 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
4175 current
.Apply(defaultStyle
);
4176 SetAndShowDefaultStyle(current
);
4178 // If it's a paragraph style, we want to apply the style to the
4179 // current paragraph even if we didn't select any text.
4182 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4183 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
4186 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
4193 /// Apply the style sheet to the buffer, for example if the styles have changed.
4194 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4197 styleSheet
= GetBuffer().GetStyleSheet();
4201 if (GetBuffer().ApplyStyleSheet(styleSheet
))
4203 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4211 /// Sets the default style to the style under the cursor
4212 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4214 wxRichTextAttr attr
;
4215 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
4217 // If at the start of a paragraph, use the next position.
4218 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4220 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(pos
);
4221 if (obj
&& obj
->IsTopLevel())
4223 // Don't use the attributes of a top-level object, since they might apply
4224 // to content of the object, e.g. background colour.
4225 SetDefaultStyle(wxRichTextAttr());
4228 else if (GetUncombinedStyle(pos
, attr
))
4230 SetDefaultStyle(attr
);
4237 /// Returns the first visible position in the current view
4238 long wxRichTextCtrl::GetFirstVisiblePosition() const
4240 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
4242 return line
->GetAbsoluteRange().GetStart();
4247 /// Get the first visible point in the window
4248 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
4251 int startXUnits
, startYUnits
;
4253 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
4254 GetViewStart(& startXUnits
, & startYUnits
);
4256 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
4259 /// The adjusted caret position is the character position adjusted to take
4260 /// into account whether we're at the start of a paragraph, in which case
4261 /// style information should be taken from the next position, not current one.
4262 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
4264 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
4266 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
4271 /// Get/set the selection range in character positions. -1, -1 means no selection.
4272 /// The range is in API convention, i.e. a single character selection is denoted
4274 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
4276 wxRichTextRange range
= GetInternalSelectionRange();
4277 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
4278 range
.SetEnd(range
.GetEnd() + 1);
4282 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
4284 SetSelection(range
.GetStart(), range
.GetEnd());
4288 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4290 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4293 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4295 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4298 /// Clear list for given range
4299 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
4301 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
4304 /// Number/renumber any list elements in the given range
4305 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4307 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4310 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4312 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4315 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4316 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4318 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
4321 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4323 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
4326 /// Deletes the content in the given range
4327 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
4329 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
4332 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
4334 if (sm_availableFontNames
.GetCount() == 0)
4336 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
4337 sm_availableFontNames
.Sort();
4339 return sm_availableFontNames
;
4342 void wxRichTextCtrl::ClearAvailableFontNames()
4344 sm_availableFontNames
.Clear();
4347 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
4349 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4351 wxTextAttrEx basicStyle
= GetBasicStyle();
4352 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4353 SetBasicStyle(basicStyle
);
4354 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4359 // Refresh the area affected by a selection change
4360 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4362 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4363 // the selection contains whole containers rather than just text, so refresh everything
4364 // for now as it would be hard to compute the rectangle bounding all selections.
4365 // TODO: improve on this.
4366 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4367 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4373 wxRichTextRange oldRange
, newRange
;
4374 if (oldSelection
.IsValid())
4375 oldRange
= oldSelection
.GetRange();
4377 oldRange
= wxRICHTEXT_NO_SELECTION
;
4378 if (newSelection
.IsValid())
4379 newRange
= newSelection
.GetRange();
4381 newRange
= wxRICHTEXT_NO_SELECTION
;
4383 // Calculate the refresh rectangle - just the affected lines
4384 long firstPos
, lastPos
;
4385 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4387 firstPos
= newRange
.GetStart();
4388 lastPos
= newRange
.GetEnd();
4390 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4392 firstPos
= oldRange
.GetStart();
4393 lastPos
= oldRange
.GetEnd();
4395 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4401 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4402 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4405 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4406 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4408 if (firstLine
&& lastLine
)
4410 wxSize clientSize
= GetClientSize();
4411 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
4412 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
4415 pt1
.y
= wxMax(0, pt1
.y
);
4417 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4419 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4420 RefreshRect(rect
, false);
4428 // margins functions
4429 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4431 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4432 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4433 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4434 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4439 wxPoint
wxRichTextCtrl::DoGetMargins() const
4441 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4442 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4445 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4447 if (obj
&& !obj
->AcceptsFocus())
4450 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4451 bool changingContainer
= (m_focusObject
!= obj
);
4453 if (changingContainer
&& HasSelection())
4456 m_focusObject
= obj
;
4459 m_focusObject
= & m_buffer
;
4461 if (setCaretPosition
&& changingContainer
)
4463 m_selection
.Reset();
4464 m_selectionAnchor
= -2;
4465 m_selectionAnchorObject
= NULL
;
4466 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4470 m_caretAtLineStart
= false;
4471 MoveCaret(pos
, m_caretAtLineStart
);
4472 SetDefaultStyleToCursorStyle();
4474 wxRichTextEvent
cmdEvent(
4475 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4477 cmdEvent
.SetEventObject(this);
4478 cmdEvent
.SetPosition(m_caretPosition
+1);
4479 cmdEvent
.SetOldContainer(oldContainer
);
4480 cmdEvent
.SetContainer(m_focusObject
);
4482 GetEventHandler()->ProcessEvent(cmdEvent
);
4487 #if wxUSE_DRAG_AND_DROP
4488 void wxRichTextCtrl::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxDragResult def
, wxDataObject
* DataObj
)
4492 if ((def
!= wxDragCopy
) && (def
!= wxDragMove
))
4497 if (!GetSelection().IsValid())
4502 wxRichTextParagraphLayoutBox
* originContainer
= GetSelection().GetContainer();
4503 wxRichTextParagraphLayoutBox
* destContainer
= GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4505 wxRichTextBuffer
* richTextBuffer
= ((wxRichTextBufferDataObject
*)DataObj
)->GetRichTextBuffer();
4508 long position
= GetCaretPosition();
4509 wxRichTextRange selectionrange
= GetInternalSelectionRange();
4510 if (selectionrange
.Contains(position
) && (def
== wxDragMove
))
4512 // It doesn't make sense to move onto itself
4516 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4517 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4518 bool DeleteAfter
= (def
== wxDragMove
) && (position
> selectionrange
.GetEnd());
4519 if ((def
== wxDragMove
) && !DeleteAfter
)
4521 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4522 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4525 destContainer
->InsertParagraphsWithUndo(&GetBuffer(), position
+1, *richTextBuffer
, this, 0);
4526 ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
4528 delete richTextBuffer
;
4532 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4533 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4540 #endif // wxUSE_DRAG_AND_DROP
4543 #if wxUSE_DRAG_AND_DROP
4544 bool wxRichTextDropSource::GiveFeedback(wxDragResult
WXUNUSED(effect
))
4546 wxCHECK_MSG(m_rtc
, false, wxT("NULL m_rtc"));
4550 wxRichTextObject
* hitObj
= NULL
;
4551 wxRichTextParagraphLayoutBox
* container
= m_rtc
->FindContainerAtPoint(m_rtc
->ScreenToClient(wxGetMousePosition()), position
, hit
, hitObj
);
4553 if (!(hit
& wxRICHTEXT_HITTEST_NONE
) && container
&& container
->AcceptsFocus())
4555 m_rtc
->StoreFocusObject(container
);
4556 m_rtc
->SetCaretPositionAfterClick(container
, position
, hit
);
4559 return false; // so that the base-class sets a cursor
4561 #endif // wxUSE_DRAG_AND_DROP
4563 bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), const wxRichTextRange
& WXUNUSED(range
)) const
4568 bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), long WXUNUSED(pos
)) const
4573 void wxRichTextCtrl::EnableVerticalScrollbar(bool enable
)
4575 m_verticalScrollbarEnabled
= enable
;
4580 #if wxRICHTEXT_USE_OWN_CARET
4582 // ----------------------------------------------------------------------------
4583 // initialization and destruction
4584 // ----------------------------------------------------------------------------
4586 void wxRichTextCaret::Init()
4589 m_refreshEnabled
= true;
4593 m_richTextCtrl
= NULL
;
4594 m_needsUpdate
= false;
4598 wxRichTextCaret::~wxRichTextCaret()
4600 if (m_timer
.IsRunning())
4604 // ----------------------------------------------------------------------------
4605 // showing/hiding/moving the caret (base class interface)
4606 // ----------------------------------------------------------------------------
4608 void wxRichTextCaret::DoShow()
4612 if (!m_timer
.IsRunning())
4613 m_timer
.Start(GetBlinkTime());
4618 void wxRichTextCaret::DoHide()
4620 if (m_timer
.IsRunning())
4626 void wxRichTextCaret::DoMove()
4632 if (m_xOld
!= -1 && m_yOld
!= -1)
4634 if (m_richTextCtrl
&& m_refreshEnabled
)
4636 wxRect
rect(GetPosition(), GetSize());
4637 m_richTextCtrl
->RefreshRect(rect
, false);
4646 void wxRichTextCaret::DoSize()
4648 int countVisible
= m_countVisible
;
4649 if (countVisible
> 0)
4655 if (countVisible
> 0)
4657 m_countVisible
= countVisible
;
4662 // ----------------------------------------------------------------------------
4663 // handling the focus
4664 // ----------------------------------------------------------------------------
4666 void wxRichTextCaret::OnSetFocus()
4674 void wxRichTextCaret::OnKillFocus()
4679 // ----------------------------------------------------------------------------
4680 // drawing the caret
4681 // ----------------------------------------------------------------------------
4683 void wxRichTextCaret::Refresh()
4685 if (m_richTextCtrl
&& m_refreshEnabled
)
4687 wxRect
rect(GetPosition(), GetSize());
4688 m_richTextCtrl
->RefreshRect(rect
, false);
4692 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4694 dc
->SetPen( *wxBLACK_PEN
);
4696 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4697 dc
->SetPen(*wxBLACK_PEN
);
4699 wxPoint
pt(m_x
, m_y
);
4703 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4705 if (IsVisible() && m_flashOn
)
4706 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4709 void wxRichTextCaret::Notify()
4711 m_flashOn
= !m_flashOn
;
4715 void wxRichTextCaretTimer::Notify()
4720 // wxRICHTEXT_USE_OWN_CARET
4723 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4727 m_labels
.Add(label
);
4735 // Returns number of menu items were added.
4736 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4738 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4739 // If none of the standard properties identifiers are in the menu, add them if necessary.
4740 // If no items to add, just set the text to something generic
4741 if (GetCount() == 0)
4745 menu
->SetLabel(startCmd
, _("&Properties"));
4747 // Delete the others if necessary
4749 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4751 if (menu
->FindItem(i
))
4762 // Find the position of the first properties item
4763 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4765 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4766 if (item
&& item
->GetId() == startCmd
)
4775 int insertBefore
= pos
+1;
4776 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4778 if (menu
->FindItem(i
))
4780 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4784 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4785 menu
->Append(i
, m_labels
[i
- startCmd
]);
4787 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4792 // Delete any old items still left on the menu
4793 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4795 if (menu
->FindItem(i
))
4803 // No existing property identifiers were found, so append to the end of the menu.
4804 menu
->AppendSeparator();
4805 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4807 menu
->Append(i
, m_labels
[i
- startCmd
]);
4815 // Add appropriate menu items for the current container and clicked on object
4816 // (and container's parent, if appropriate).
4817 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl
* ctrl
, wxRichTextObject
* container
, wxRichTextObject
* obj
)
4820 if (obj
&& ctrl
->CanEditProperties(obj
))
4821 AddItem(ctrl
->GetPropertiesMenuLabel(obj
), obj
);
4823 if (container
&& container
!= obj
&& ctrl
->CanEditProperties(container
) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
)) == wxNOT_FOUND
)
4824 AddItem(ctrl
->GetPropertiesMenuLabel(container
), container
);
4826 if (container
&& container
->GetParent() && ctrl
->CanEditProperties(container
->GetParent()) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
->GetParent())) == wxNOT_FOUND
)
4827 AddItem(ctrl
->GetPropertiesMenuLabel(container
->GetParent()), container
->GetParent());