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
744 // See if we're starting Drag'n'Drop
747 int x
= m_dragStartPoint
.x
- event
.GetPosition().x
;
748 int y
= m_dragStartPoint
.y
- event
.GetPosition().y
;
749 size_t distance
= abs(x
) + abs(y
);
751 wxTimeSpan diff
= wxDateTime::UNow() - m_dragStartTime
;
755 && (diff
.GetMilliseconds() > 100)
762 wxRichTextRange range
= GetInternalSelectionRange();
763 if (range
== wxRICHTEXT_NONE
)
765 // Don't try to drag an empty range
770 // Cache the current situation, to be restored if Drag'n'Drop is cancelled
771 long oldPos
= GetCaretPosition();
772 wxRichTextParagraphLayoutBox
* oldFocus
= GetFocusObject();
774 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
775 wxString text
= GetFocusObject()->GetTextForRange(range
);
777 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
779 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
781 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
782 GetFocusObject()->CopyFragment(range
, *richTextBuf
);
783 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
785 wxRichTextDropSource
source(*compositeObject
, this);
786 // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise
787 // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed
788 BeginBatchUndo(_("Drag"));
789 switch (source
.DoDragDrop(wxDrag_AllowMove
| wxDrag_DefaultMove
))
792 case wxDragCopy
: break;
795 wxLogError(wxT("An error occurred during drag and drop operation"));
798 Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take'
799 SetCaretPosition(oldPos
);
800 SetFocusObject(oldFocus
, false);
809 #endif // wxUSE_DRAG_AND_DROP
813 dc
.SetFont(GetFont());
816 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
817 wxRichTextObject
* hitObj
= NULL
;
818 wxRichTextObject
* contextObj
= NULL
;
822 // If we're dragging, let's only consider positions at this level; otherwise
823 // selecting a range is not going to work.
824 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
827 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
828 container
= GetFocusObject();
830 wxRichTextDrawingContext
context(& GetBuffer());
831 int hit
= container
->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
, flags
);
833 // See if we need to change the cursor
836 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
838 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
840 ProcessMouseMovement(actualContainer
, hitObj
, position
, logicalPt
);
843 SetCursor(m_textCursor
);
846 if (!event
.Dragging())
853 #if wxUSE_DRAG_AND_DROP
858 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
859 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
860 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
862 // Check for dragging across multiple containers
864 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
865 int hit2
= GetBuffer().HitTest(dc
, context
, logicalPt
, position2
, & hitObj2
, & contextObj2
, 0);
866 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
868 // See if we can find a common ancestor
869 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
871 firstContainer
= GetFocusObject();
872 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
876 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
877 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
878 // is the common ancestor.
879 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
882 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
884 wxRichTextObject
* p
= hitObj2
;
887 if (p
->GetParent() == commonAncestor
)
889 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
896 if (commonAncestor
&& firstContainer
&& otherContainer
)
898 // We have now got a second container that shares a parent with the current or anchor object.
899 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
901 // Don't go into common-ancestor selection mode if we still have the same
903 if (otherContainer
!= firstContainer
)
905 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
906 m_selectionAnchorObject
= firstContainer
;
907 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
909 // The common ancestor, such as a table, returns the cell selection
910 // between the anchor and current position.
911 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
916 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
921 if (otherContainer
->AcceptsFocus())
922 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
923 MoveCaret(-1, false);
924 SetDefaultStyleToCursorStyle();
929 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
930 #if wxUSE_DRAG_AND_DROP
935 // TODO: test closeness
936 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
941 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
947 dc
.SetFont(GetFont());
950 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
951 wxRichTextObject
* hitObj
= NULL
;
952 wxRichTextObject
* contextObj
= NULL
;
953 wxRichTextDrawingContext
context(& GetBuffer());
954 int hit
= GetFocusObject()->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
);
956 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
958 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
959 if (actualContainer
&& actualContainer
->AcceptsFocus())
961 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
962 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
966 wxRichTextEvent
cmdEvent(
967 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
969 cmdEvent
.SetEventObject(this);
970 cmdEvent
.SetPosition(position
);
972 cmdEvent
.SetContainer(hitObj
->GetContainer());
974 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
978 /// Left-double-click
979 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
981 wxRichTextEvent
cmdEvent(
982 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
984 cmdEvent
.SetEventObject(this);
985 cmdEvent
.SetPosition(m_caretPosition
+1);
986 cmdEvent
.SetContainer(GetFocusObject());
988 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
990 SelectWord(GetCaretPosition()+1);
995 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
997 wxRichTextEvent
cmdEvent(
998 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
1000 cmdEvent
.SetEventObject(this);
1001 cmdEvent
.SetPosition(m_caretPosition
+1);
1002 cmdEvent
.SetContainer(GetFocusObject());
1004 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1007 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1008 // Paste any PRIMARY selection, if it exists
1009 wxTheClipboard
->UsePrimarySelection(true);
1011 wxTheClipboard
->UsePrimarySelection(false);
1016 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
1019 if (event
.CmdDown())
1020 flags
|= wxRICHTEXT_CTRL_DOWN
;
1021 if (event
.ShiftDown())
1022 flags
|= wxRICHTEXT_SHIFT_DOWN
;
1023 if (event
.AltDown())
1024 flags
|= wxRICHTEXT_ALT_DOWN
;
1026 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
1028 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
1030 KeyboardNavigate(event
.GetKeyCode(), flags
);
1034 long keycode
= event
.GetKeyCode();
1094 case WXK_NUMPAD_HOME
:
1095 case WXK_NUMPAD_LEFT
:
1097 case WXK_NUMPAD_RIGHT
:
1098 case WXK_NUMPAD_DOWN
:
1099 case WXK_NUMPAD_PAGEUP
:
1100 case WXK_NUMPAD_PAGEDOWN
:
1101 case WXK_NUMPAD_END
:
1102 case WXK_NUMPAD_BEGIN
:
1103 case WXK_NUMPAD_INSERT
:
1104 case WXK_WINDOWS_LEFT
:
1113 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1114 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
1121 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1126 BeginBatchUndo(_("Delete Text"));
1128 long newPos
= m_caretPosition
;
1130 bool processed
= DeleteSelectedContent(& newPos
);
1136 // Submit range in character positions, which are greater than caret positions,
1137 // so subtract 1 for deleted character and add 1 for conversion to character position.
1140 if (event
.CmdDown())
1142 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1145 wxRichTextRange
range(pos
+1, newPos
);
1146 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1148 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1157 wxRichTextRange
range(newPos
, newPos
);
1158 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1160 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1168 if (GetLastPosition() == -1)
1170 GetFocusObject()->Reset();
1172 m_caretPosition
= -1;
1174 SetDefaultStyleToCursorStyle();
1177 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1179 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1180 //if (deletions > 0)
1182 wxRichTextEvent
cmdEvent(
1183 wxEVT_COMMAND_RICHTEXT_DELETE
,
1185 cmdEvent
.SetEventObject(this);
1186 cmdEvent
.SetFlags(flags
);
1187 cmdEvent
.SetPosition(m_caretPosition
+1);
1188 cmdEvent
.SetContainer(GetFocusObject());
1189 GetEventHandler()->ProcessEvent(cmdEvent
);
1200 // all the other keys modify the controls contents which shouldn't be
1201 // possible if we're read-only
1202 if ( !IsEditable() )
1208 if (event
.GetKeyCode() == WXK_RETURN
)
1210 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1213 long newPos
= m_caretPosition
;
1215 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1220 BeginBatchUndo(_("Insert Text"));
1222 DeleteSelectedContent(& newPos
);
1224 if (event
.ShiftDown())
1227 text
= wxRichTextLineBreakChar
;
1228 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, text
, this);
1229 m_caretAtLineStart
= true;
1233 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1236 SetDefaultStyleToCursorStyle();
1238 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1240 wxRichTextEvent
cmdEvent(
1241 wxEVT_COMMAND_RICHTEXT_RETURN
,
1243 cmdEvent
.SetEventObject(this);
1244 cmdEvent
.SetFlags(flags
);
1245 cmdEvent
.SetPosition(newPos
+1);
1246 cmdEvent
.SetContainer(GetFocusObject());
1248 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1250 // Generate conventional event
1251 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1252 InitCommandEvent(textEvent
);
1254 GetEventHandler()->ProcessEvent(textEvent
);
1258 else if (event
.GetKeyCode() == WXK_BACK
)
1260 long newPos
= m_caretPosition
;
1262 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1267 BeginBatchUndo(_("Delete Text"));
1269 bool processed
= DeleteSelectedContent(& newPos
);
1275 // Submit range in character positions, which are greater than caret positions,
1276 // so subtract 1 for deleted character and add 1 for conversion to character position.
1279 if (event
.CmdDown())
1281 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1284 wxRichTextRange
range(pos
+1, newPos
);
1285 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1287 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1296 wxRichTextRange
range(newPos
, newPos
);
1297 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1299 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1307 if (GetLastPosition() == -1)
1309 GetFocusObject()->Reset();
1311 m_caretPosition
= -1;
1313 SetDefaultStyleToCursorStyle();
1316 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1318 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1319 //if (deletions > 0)
1321 wxRichTextEvent
cmdEvent(
1322 wxEVT_COMMAND_RICHTEXT_DELETE
,
1324 cmdEvent
.SetEventObject(this);
1325 cmdEvent
.SetFlags(flags
);
1326 cmdEvent
.SetPosition(m_caretPosition
+1);
1327 cmdEvent
.SetContainer(GetFocusObject());
1328 GetEventHandler()->ProcessEvent(cmdEvent
);
1333 else if (event
.GetKeyCode() == WXK_DELETE
)
1335 long newPos
= m_caretPosition
;
1337 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1342 BeginBatchUndo(_("Delete Text"));
1344 bool processed
= DeleteSelectedContent(& newPos
);
1350 // Submit range in character positions, which are greater than caret positions,
1351 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1353 if (event
.CmdDown())
1355 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1356 if (pos
!= -1 && (pos
> newPos
))
1358 wxRichTextRange
range(newPos
+1, pos
);
1359 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1361 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1368 if (!processed
&& newPos
< (GetLastPosition()-1))
1370 wxRichTextRange
range(newPos
+1, newPos
+1);
1371 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1373 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1381 if (GetLastPosition() == -1)
1383 GetFocusObject()->Reset();
1385 m_caretPosition
= -1;
1387 SetDefaultStyleToCursorStyle();
1390 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1392 // Always send this event; wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion.
1393 //if (deletions > 0)
1395 wxRichTextEvent
cmdEvent(
1396 wxEVT_COMMAND_RICHTEXT_DELETE
,
1398 cmdEvent
.SetEventObject(this);
1399 cmdEvent
.SetFlags(flags
);
1400 cmdEvent
.SetPosition(m_caretPosition
+1);
1401 cmdEvent
.SetContainer(GetFocusObject());
1402 GetEventHandler()->ProcessEvent(cmdEvent
);
1409 long keycode
= event
.GetKeyCode();
1421 if (event
.CmdDown())
1423 // Fixes AltGr+key with European input languages on Windows
1424 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1431 wxRichTextEvent
cmdEvent(
1432 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1434 cmdEvent
.SetEventObject(this);
1435 cmdEvent
.SetFlags(flags
);
1437 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1439 cmdEvent
.SetCharacter((wxChar
) keycode
);
1441 cmdEvent
.SetPosition(m_caretPosition
+1);
1442 cmdEvent
.SetContainer(GetFocusObject());
1444 if (keycode
== wxT('\t'))
1446 // See if we need to promote or demote the selection or paragraph at the cursor
1447 // position, instead of inserting a tab.
1448 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1449 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1450 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1452 wxRichTextRange range
;
1454 range
= GetSelectionRange();
1456 range
= para
->GetRange().FromInternal();
1458 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1460 PromoteList(promoteBy
, range
, NULL
);
1462 GetEventHandler()->ProcessEvent(cmdEvent
);
1468 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1471 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1474 BeginBatchUndo(_("Insert Text"));
1476 long newPos
= m_caretPosition
;
1477 DeleteSelectedContent(& newPos
);
1480 wxString str
= event
.GetUnicodeKey();
1482 wxString str
= (wxChar
) event
.GetKeyCode();
1484 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, str
, this, 0);
1488 SetDefaultStyleToCursorStyle();
1489 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1491 cmdEvent
.SetPosition(m_caretPosition
);
1492 GetEventHandler()->ProcessEvent(cmdEvent
);
1500 bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox
* container
, wxRichTextObject
* WXUNUSED(obj
), long position
, const wxPoint
& WXUNUSED(pos
))
1502 wxRichTextAttr attr
;
1503 if (container
&& GetStyle(position
, attr
, container
))
1505 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
1507 SetCursor(m_urlCursor
);
1509 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
1511 SetCursor(m_textCursor
);
1519 /// Delete content if there is a selection, e.g. when pressing a key.
1520 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1524 long pos
= m_selection
.GetRange().GetStart();
1525 wxRichTextRange range
= m_selection
.GetRange();
1527 // SelectAll causes more to be selected than doing it interactively,
1528 // and causes a new paragraph to be inserted. So for multiline buffers,
1529 // don't delete the final position.
1530 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1531 range
.SetEnd(range
.GetEnd()-1);
1533 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1534 m_selection
.Reset();
1535 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1545 /// Keyboard navigation
1549 Left: left one character
1550 Right: right one character
1553 Ctrl-Left: left one word
1554 Ctrl-Right: right one word
1555 Ctrl-Up: previous paragraph start
1556 Ctrl-Down: next start of paragraph
1559 Ctrl-Home: start of document
1560 Ctrl-End: end of document
1561 Page-Up: Up a screen
1562 Page-Down: Down a screen
1566 Ctrl-Alt-PgUp: Start of window
1567 Ctrl-Alt-PgDn: End of window
1568 F8: Start selection mode
1569 Esc: End selection mode
1571 Adding Shift does the above but starts/extends selection.
1576 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1578 bool success
= false;
1580 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1582 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1583 success
= WordRight(1, flags
);
1585 success
= MoveRight(1, flags
);
1587 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1589 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1590 success
= WordLeft(1, flags
);
1592 success
= MoveLeft(1, flags
);
1594 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1596 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1597 success
= MoveToParagraphStart(flags
);
1599 success
= MoveUp(1, flags
);
1601 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1603 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1604 success
= MoveToParagraphEnd(flags
);
1606 success
= MoveDown(1, flags
);
1608 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1610 success
= PageUp(1, flags
);
1612 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1614 success
= PageDown(1, flags
);
1616 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1618 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1619 success
= MoveHome(flags
);
1621 success
= MoveToLineStart(flags
);
1623 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1625 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1626 success
= MoveEnd(flags
);
1628 success
= MoveToLineEnd(flags
);
1633 ScrollIntoView(m_caretPosition
, keyCode
);
1634 SetDefaultStyleToCursorStyle();
1640 /// Extend the selection. Selections are in caret positions.
1641 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1643 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1645 if (oldPos
== newPos
)
1648 wxRichTextSelection oldSelection
= m_selection
;
1650 m_selection
.SetContainer(GetFocusObject());
1652 wxRichTextRange oldRange
;
1653 if (m_selection
.IsValid())
1654 oldRange
= m_selection
.GetRange();
1656 oldRange
= wxRICHTEXT_NO_SELECTION
;
1657 wxRichTextRange newRange
;
1659 // If not currently selecting, start selecting
1660 if (oldRange
.GetStart() == -2)
1662 m_selectionAnchor
= oldPos
;
1664 if (oldPos
> newPos
)
1665 newRange
.SetRange(newPos
+1, oldPos
);
1667 newRange
.SetRange(oldPos
+1, newPos
);
1671 // Always ensure that the selection range start is greater than
1673 if (newPos
> m_selectionAnchor
)
1674 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1675 else if (newPos
== m_selectionAnchor
)
1676 newRange
= wxRichTextRange(-2, -2);
1678 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1681 m_selection
.SetRange(newRange
);
1683 RefreshForSelectionChange(oldSelection
, m_selection
);
1685 if (newRange
.GetStart() > newRange
.GetEnd())
1687 wxLogDebug(wxT("Strange selection range"));
1696 /// Scroll into view, returning true if we scrolled.
1697 /// This takes a _caret_ position.
1698 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1700 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1706 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1708 int startXUnits
, startYUnits
;
1709 GetViewStart(& startXUnits
, & startYUnits
);
1710 int startY
= startYUnits
* ppuY
;
1713 GetVirtualSize(& sx
, & sy
);
1719 wxRect rect
= line
->GetRect();
1721 bool scrolled
= false;
1723 wxSize clientSize
= GetClientSize();
1725 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1728 wxClientDC
dc(this);
1729 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1730 topMargin
, bottomMargin
);
1732 // clientSize.y -= GetBuffer().GetBottomMargin();
1733 clientSize
.y
-= bottomMargin
;
1735 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1737 int y
= rect
.y
- GetClientSize().y
/2;
1738 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1739 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1741 if (startYUnits
!= yUnits
)
1743 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1746 #if !wxRICHTEXT_USE_OWN_CARET
1756 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1757 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1758 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1759 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1761 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1763 // Make it scroll so this item is at the bottom
1765 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1766 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1768 // If we're still off the screen, scroll another line down
1769 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1772 if (startYUnits
!= yUnits
)
1774 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1778 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1780 // Make it scroll so this item is at the top
1782 int y
= rect
.y
- GetBuffer().GetTopMargin();
1783 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1785 if (startYUnits
!= yUnits
)
1787 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1793 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1794 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1795 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1796 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1798 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1800 // Make it scroll so this item is at the top
1802 int y
= rect
.y
- GetBuffer().GetTopMargin();
1803 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1805 if (startYUnits
!= yUnits
)
1807 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1811 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1813 // Make it scroll so this item is at the bottom
1815 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1816 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1818 // If we're still off the screen, scroll another line down
1819 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1822 if (startYUnits
!= yUnits
)
1824 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1830 #if !wxRICHTEXT_USE_OWN_CARET
1838 /// Is the given position visible on the screen?
1839 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1841 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1847 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1850 GetViewStart(& startX
, & startY
);
1852 startY
= startY
* ppuY
;
1854 wxRect rect
= line
->GetRect();
1855 wxSize clientSize
= GetClientSize();
1856 clientSize
.y
-= GetBuffer().GetBottomMargin();
1858 return (rect
.GetTop() >= (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1861 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1863 m_caretPosition
= position
;
1864 m_caretAtLineStart
= showAtLineStart
;
1867 /// Move caret one visual step forward: this may mean setting a flag
1868 /// and keeping the same position if we're going from the end of one line
1869 /// to the start of the next, which may be the exact same caret position.
1870 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1872 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1874 // Only do the check if we're not at the end of the paragraph (where things work OK
1876 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1878 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1882 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1884 // We're at the end of a line. See whether we need to
1885 // stay at the same actual caret position but change visual
1886 // position, or not.
1887 if (oldPosition
== lineRange
.GetEnd())
1889 if (m_caretAtLineStart
)
1891 // We're already at the start of the line, so actually move on now.
1892 m_caretPosition
= oldPosition
+ 1;
1893 m_caretAtLineStart
= false;
1897 // We're showing at the end of the line, so keep to
1898 // the same position but indicate that we're to show
1899 // at the start of the next line.
1900 m_caretPosition
= oldPosition
;
1901 m_caretAtLineStart
= true;
1903 SetDefaultStyleToCursorStyle();
1909 SetDefaultStyleToCursorStyle();
1912 /// Move caret one visual step backward: this may mean setting a flag
1913 /// and keeping the same position if we're going from the end of one line
1914 /// to the start of the next, which may be the exact same caret position.
1915 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1917 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1919 // Only do the check if we're not at the start of the paragraph (where things work OK
1921 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1923 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1927 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1929 // We're at the start of a line. See whether we need to
1930 // stay at the same actual caret position but change visual
1931 // position, or not.
1932 if (oldPosition
== lineRange
.GetStart())
1934 m_caretPosition
= oldPosition
-1;
1935 m_caretAtLineStart
= true;
1938 else if (oldPosition
== lineRange
.GetEnd())
1940 if (m_caretAtLineStart
)
1942 // We're at the start of the line, so keep the same caret position
1943 // but clear the start-of-line flag.
1944 m_caretPosition
= oldPosition
;
1945 m_caretAtLineStart
= false;
1949 // We're showing at the end of the line, so go back
1950 // to the previous character position.
1951 m_caretPosition
= oldPosition
- 1;
1953 SetDefaultStyleToCursorStyle();
1959 SetDefaultStyleToCursorStyle();
1963 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1965 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1967 if (m_caretPosition
+ noPositions
< endPos
)
1969 long oldPos
= m_caretPosition
;
1970 long newPos
= m_caretPosition
+ noPositions
;
1972 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1976 // Determine by looking at oldPos and m_caretPosition whether
1977 // we moved from the end of a line to the start of the next line, in which case
1978 // we want to adjust the caret position such that it is positioned at the
1979 // start of the next line, rather than jumping past the first character of the
1981 if (noPositions
== 1 && !extendSel
)
1982 MoveCaretForward(oldPos
);
1984 SetCaretPosition(newPos
);
1987 SetDefaultStyleToCursorStyle();
1996 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
2000 if (m_caretPosition
> startPos
- noPositions
+ 1)
2002 long oldPos
= m_caretPosition
;
2003 long newPos
= m_caretPosition
- noPositions
;
2004 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2008 if (noPositions
== 1 && !extendSel
)
2009 MoveCaretBack(oldPos
);
2011 SetCaretPosition(newPos
);
2014 SetDefaultStyleToCursorStyle();
2022 // Find the caret position for the combination of hit-test flags and character position.
2023 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
2024 // since this is ambiguous (same position used for end of line and start of next).
2025 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
2026 bool& caretLineStart
)
2028 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
2029 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
2030 // so we view the caret at the start of the line.
2031 caretLineStart
= false;
2032 long caretPosition
= position
;
2034 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
2036 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
2037 wxRichTextRange lineRange
;
2039 lineRange
= thisLine
->GetAbsoluteRange();
2041 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
2044 caretLineStart
= true;
2048 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
2049 if (para
&& para
->GetRange().GetStart() == position
)
2053 return caretPosition
;
2057 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
2059 return MoveDown(- noLines
, flags
);
2063 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
2068 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
2069 wxPoint pt
= GetCaret()->GetPosition();
2070 long newLine
= lineNumber
+ noLines
;
2071 bool notInThisObject
= false;
2073 if (lineNumber
!= -1)
2077 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
2078 if (newLine
> lastLine
)
2079 notInThisObject
= true;
2084 notInThisObject
= true;
2088 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
2089 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
;
2091 if (notInThisObject
)
2093 // If we know we're navigating out of the current object,
2094 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2095 container
= & GetBuffer();
2096 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
2098 if (noLines
> 0) // going down
2100 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
2104 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
2109 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
2111 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
2117 wxClientDC
dc(this);
2119 dc
.SetFont(GetFont());
2121 wxRichTextObject
* hitObj
= NULL
;
2122 wxRichTextObject
* contextObj
= NULL
;
2123 wxRichTextDrawingContext
context(& GetBuffer());
2124 int hitTest
= container
->HitTest(dc
, context
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
2127 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
2128 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
2131 if (notInThisObject
)
2133 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2134 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
2136 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
2138 container
= actualContainer
;
2142 bool caretLineStart
= true;
2143 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
2144 long newSelEnd
= caretPosition
;
2147 if (notInThisObject
)
2150 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
2155 SetCaretPosition(caretPosition
, caretLineStart
);
2157 SetDefaultStyleToCursorStyle();
2165 /// Move to the end of the paragraph
2166 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
2168 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2171 long newPos
= para
->GetRange().GetEnd() - 1;
2172 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2176 SetCaretPosition(newPos
);
2178 SetDefaultStyleToCursorStyle();
2186 /// Move to the start of the paragraph
2187 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
2189 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2192 long newPos
= para
->GetRange().GetStart() - 1;
2193 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2197 SetCaretPosition(newPos
);
2199 SetDefaultStyleToCursorStyle();
2207 /// Move to the end of the line
2208 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
2210 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2214 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2215 long newPos
= lineRange
.GetEnd();
2216 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2220 SetCaretPosition(newPos
);
2222 SetDefaultStyleToCursorStyle();
2230 /// Move to the start of the line
2231 bool wxRichTextCtrl::MoveToLineStart(int flags
)
2233 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2236 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2237 long newPos
= lineRange
.GetStart()-1;
2239 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2243 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
2245 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2247 SetDefaultStyleToCursorStyle();
2255 /// Move to the start of the buffer
2256 bool wxRichTextCtrl::MoveHome(int flags
)
2258 if (m_caretPosition
!= -1)
2260 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2264 SetCaretPosition(-1);
2266 SetDefaultStyleToCursorStyle();
2274 /// Move to the end of the buffer
2275 bool wxRichTextCtrl::MoveEnd(int flags
)
2277 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2279 if (m_caretPosition
!= endPos
)
2281 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2285 SetCaretPosition(endPos
);
2287 SetDefaultStyleToCursorStyle();
2295 /// Move noPages pages up
2296 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2298 return PageDown(- noPages
, flags
);
2301 /// Move noPages pages down
2302 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2304 // Calculate which line occurs noPages * screen height further down.
2305 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2308 wxSize clientSize
= GetClientSize();
2309 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2311 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2314 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2315 long pos
= lineRange
.GetStart()-1;
2316 if (pos
!= m_caretPosition
)
2318 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2320 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2324 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2326 SetDefaultStyleToCursorStyle();
2336 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2338 return str
== wxT(" ") || str
== wxT("\t");
2341 // Finds the caret position for the next word
2342 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2344 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2348 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2350 // First skip current text to space
2351 while (i
< endPos
&& i
> -1)
2353 // i is in character, not caret positions
2354 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2355 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2356 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2360 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2367 while (i
< endPos
&& i
> -1)
2369 // i is in character, not caret positions
2370 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2371 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2372 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2373 return wxMax(-1, i
);
2375 if (text
.empty()) // End of paragraph, or maybe an image
2376 return wxMax(-1, i
- 1);
2377 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2381 // Convert to caret position
2382 return wxMax(-1, i
- 1);
2391 long i
= m_caretPosition
;
2393 // First skip white space
2394 while (i
< endPos
&& i
> -1)
2396 // i is in character, not caret positions
2397 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2398 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2400 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2402 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2407 // Next skip current text to space
2408 while (i
< endPos
&& i
> -1)
2410 // i is in character, not caret positions
2411 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2412 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2413 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2416 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2429 /// Move n words left
2430 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2432 long pos
= FindNextWordPosition(-1);
2433 if (pos
!= m_caretPosition
)
2435 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2437 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2441 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2443 SetDefaultStyleToCursorStyle();
2451 /// Move n words right
2452 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2454 long pos
= FindNextWordPosition(1);
2455 if (pos
!= m_caretPosition
)
2457 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2459 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2463 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2465 SetDefaultStyleToCursorStyle();
2474 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2476 // Only do sizing optimization for large buffers
2477 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2479 m_fullLayoutRequired
= true;
2480 m_fullLayoutTime
= wxGetLocalTimeMillis();
2481 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2482 LayoutContent(true /* onlyVisibleRect */);
2485 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2487 #if wxRICHTEXT_BUFFERED_PAINTING
2494 // Force any pending layout due to large buffer
2495 void wxRichTextCtrl::ForceDelayedLayout()
2497 if (m_fullLayoutRequired
)
2499 m_fullLayoutRequired
= false;
2500 m_fullLayoutTime
= 0;
2501 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2502 ShowPosition(m_fullLayoutSavedPosition
);
2508 /// Idle-time processing
2509 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2511 #if wxRICHTEXT_USE_OWN_CARET
2512 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2514 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2520 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2522 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2524 m_fullLayoutRequired
= false;
2525 m_fullLayoutTime
= 0;
2526 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2527 ShowPosition(m_fullLayoutSavedPosition
);
2531 if (m_caretPositionForDefaultStyle
!= -2)
2533 // If the caret position has changed, no longer reflect the default style
2535 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2536 m_caretPositionForDefaultStyle
= -2;
2543 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2545 #if wxRICHTEXT_USE_OWN_CARET
2546 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2549 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2556 /// Set up scrollbars, e.g. after a resize
2557 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2562 if (GetBuffer().IsEmpty() || !m_verticalScrollbarEnabled
)
2564 SetScrollbars(0, 0, 0, 0, 0, 0);
2568 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2569 // of pixels. See e.g. wxVScrolledWindow for ideas.
2570 int pixelsPerUnit
= 5;
2571 wxSize clientSize
= GetClientSize();
2573 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2575 // Round up so we have at least maxHeight pixels
2576 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2578 int startX
= 0, startY
= 0;
2580 GetViewStart(& startX
, & startY
);
2582 int maxPositionX
= 0;
2583 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2585 int newStartX
= wxMin(maxPositionX
, startX
);
2586 int newStartY
= wxMin(maxPositionY
, startY
);
2588 int oldPPUX
, oldPPUY
;
2589 int oldStartX
, oldStartY
;
2590 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2591 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2592 GetViewStart(& oldStartX
, & oldStartY
);
2593 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2595 oldVirtualSizeY
/= oldPPUY
;
2597 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2600 // Don't set scrollbars if there were none before, and there will be none now.
2601 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2604 // Move to previous scroll position if
2606 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2609 /// Paint the background
2610 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2612 wxColour backgroundColour
= GetBackgroundColour();
2613 if (!backgroundColour
.IsOk())
2614 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2616 // Clear the background
2617 dc
.SetBrush(wxBrush(backgroundColour
));
2618 dc
.SetPen(*wxTRANSPARENT_PEN
);
2619 wxRect
windowRect(GetClientSize());
2620 windowRect
.x
-= 2; windowRect
.y
-= 2;
2621 windowRect
.width
+= 4; windowRect
.height
+= 4;
2623 // We need to shift the rectangle to take into account
2624 // scrolling. Converting device to logical coordinates.
2625 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2626 dc
.DrawRectangle(windowRect
);
2629 #if wxRICHTEXT_BUFFERED_PAINTING
2630 /// Recreate buffer bitmap if necessary
2631 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2634 if (sz
== wxDefaultSize
)
2635 sz
= GetClientSize();
2637 if (sz
.x
< 1 || sz
.y
< 1)
2640 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2641 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2642 return m_bufferBitmap
.IsOk();
2646 // ----------------------------------------------------------------------------
2647 // file IO functions
2648 // ----------------------------------------------------------------------------
2650 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2652 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2654 m_filename
= filename
;
2657 SetInsertionPoint(0);
2660 SetupScrollbars(true);
2662 wxTextCtrl::SendTextUpdatedEvent(this);
2668 wxLogError(_("File couldn't be loaded."));
2674 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2676 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2678 m_filename
= filename
;
2685 wxLogError(_("The text couldn't be saved."));
2690 // ----------------------------------------------------------------------------
2691 // wxRichTextCtrl specific functionality
2692 // ----------------------------------------------------------------------------
2694 /// Add a new paragraph of text to the end of the buffer
2695 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2697 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2698 GetBuffer().Invalidate();
2704 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2706 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2707 GetBuffer().Invalidate();
2712 // ----------------------------------------------------------------------------
2713 // selection and ranges
2714 // ----------------------------------------------------------------------------
2716 void wxRichTextCtrl::SelectAll()
2718 SetSelection(-1, -1);
2722 void wxRichTextCtrl::SelectNone()
2724 if (m_selection
.IsValid())
2726 wxRichTextSelection oldSelection
= m_selection
;
2728 m_selection
.Reset();
2730 RefreshForSelectionChange(oldSelection
, m_selection
);
2732 m_selectionAnchor
= -2;
2733 m_selectionAnchorObject
= NULL
;
2734 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2737 static bool wxIsWordDelimiter(const wxString
& text
)
2739 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2742 /// Select the word at the given character position
2743 bool wxRichTextCtrl::SelectWord(long position
)
2745 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2748 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2752 if (position
== para
->GetRange().GetEnd())
2755 long positionStart
= position
;
2756 long positionEnd
= position
;
2758 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2760 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2761 if (wxIsWordDelimiter(text
))
2767 if (positionStart
< para
->GetRange().GetStart())
2768 positionStart
= para
->GetRange().GetStart();
2770 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2772 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2773 if (wxIsWordDelimiter(text
))
2779 if (positionEnd
>= para
->GetRange().GetEnd())
2780 positionEnd
= para
->GetRange().GetEnd();
2782 if (positionEnd
< positionStart
)
2785 SetSelection(positionStart
, positionEnd
+1);
2787 if (positionStart
>= 0)
2789 MoveCaret(positionStart
-1, true);
2790 SetDefaultStyleToCursorStyle();
2796 wxString
wxRichTextCtrl::GetStringSelection() const
2799 GetSelection(&from
, &to
);
2801 return GetRange(from
, to
);
2804 // ----------------------------------------------------------------------------
2806 // ----------------------------------------------------------------------------
2808 wxTextCtrlHitTestResult
2809 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2811 // implement in terms of the other overload as the native ports typically
2812 // can get the position and not (x, y) pair directly (although wxUniv
2813 // directly gets x and y -- and so overrides this method as well)
2815 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2817 if ( rc
!= wxTE_HT_UNKNOWN
)
2819 PositionToXY(pos
, x
, y
);
2825 wxTextCtrlHitTestResult
2826 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2829 wxClientDC
dc((wxRichTextCtrl
*) this);
2830 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2832 // Buffer uses logical position (relative to start of buffer)
2834 wxPoint pt2
= GetLogicalPoint(pt
);
2836 wxRichTextObject
* hitObj
= NULL
;
2837 wxRichTextObject
* contextObj
= NULL
;
2838 wxRichTextDrawingContext
context((wxRichTextBuffer
*) & GetBuffer());
2839 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, context
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2841 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2842 return wxTE_HT_BEFORE
;
2843 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2844 return wxTE_HT_BEYOND
;
2845 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2846 return wxTE_HT_ON_TEXT
;
2848 return wxTE_HT_UNKNOWN
;
2851 wxRichTextParagraphLayoutBox
*
2852 wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt
, long& position
, int& hit
, wxRichTextObject
* hitObj
, int flags
/* = 0*/)
2854 wxClientDC
dc(this);
2856 dc
.SetFont(GetFont());
2858 wxPoint logicalPt
= GetLogicalPoint(pt
);
2860 wxRichTextObject
* contextObj
= NULL
;
2861 wxRichTextDrawingContext
context(& GetBuffer());
2862 hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, &hitObj
, &contextObj
, flags
);
2863 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2869 // ----------------------------------------------------------------------------
2870 // set/get the controls text
2871 // ----------------------------------------------------------------------------
2873 wxString
wxRichTextCtrl::DoGetValue() const
2875 return GetBuffer().GetText();
2878 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2880 // Public API for range is different from internals
2881 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2884 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2886 // Don't call Clear here, since it always sends a text updated event
2887 m_buffer
.ResetAndClearCommands();
2888 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2889 m_caretPosition
= -1;
2890 m_caretPositionForDefaultStyle
= -2;
2891 m_caretAtLineStart
= false;
2892 m_selection
.Reset();
2893 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2903 if (!value
.IsEmpty())
2905 // Remove empty paragraph
2906 GetBuffer().Clear();
2907 DoWriteText(value
, flags
);
2909 // for compatibility, don't move the cursor when doing SetValue()
2910 SetInsertionPoint(0);
2914 // still send an event for consistency
2915 if (flags
& SetValue_SendEvent
)
2916 wxTextCtrl::SendTextUpdatedEvent(this);
2921 void wxRichTextCtrl::WriteText(const wxString
& value
)
2926 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2928 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2930 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2932 if ( flags
& SetValue_SendEvent
)
2933 wxTextCtrl::SendTextUpdatedEvent(this);
2936 void wxRichTextCtrl::AppendText(const wxString
& text
)
2938 SetInsertionPointEnd();
2943 /// Write an image at the current insertion point
2944 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2946 wxRichTextImageBlock imageBlock
;
2948 wxImage image2
= image
;
2949 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2950 return WriteImage(imageBlock
, textAttr
);
2955 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2957 wxRichTextImageBlock imageBlock
;
2960 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2961 return WriteImage(imageBlock
, textAttr
);
2966 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2968 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition
+1, imageBlock
, this, 0, textAttr
);
2971 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2975 wxRichTextImageBlock imageBlock
;
2977 wxImage image
= bitmap
.ConvertToImage();
2978 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2979 return WriteImage(imageBlock
, textAttr
);
2985 // Write a text box at the current insertion point.
2986 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2988 wxRichTextBox
* textBox
= new wxRichTextBox
;
2989 textBox
->SetAttributes(textAttr
);
2990 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2991 textBox
->AddParagraph(wxEmptyString
);
2992 textBox
->SetParent(NULL
);
2994 // The object returned is the one actually inserted into the buffer,
2995 // while the original one is deleted.
2996 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, textBox
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2997 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
3001 // Write a table at the current insertion point, returning the table.
3002 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
3004 wxASSERT(rows
> 0 && cols
> 0);
3006 if (rows
<= 0 || cols
<= 0)
3009 wxRichTextTable
* table
= new wxRichTextTable
;
3010 table
->SetAttributes(tableAttr
);
3011 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3013 table
->CreateTable(rows
, cols
);
3015 table
->SetParent(NULL
);
3018 for (j
= 0; j
< rows
; j
++)
3020 for (i
= 0; i
< cols
; i
++)
3022 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
3026 // The object returned is the one actually inserted into the buffer,
3027 // while the original one is deleted.
3028 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, table
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3029 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
3034 /// Insert a newline (actually paragraph) at the current insertion point.
3035 bool wxRichTextCtrl::Newline()
3037 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3040 /// Insert a line break at the current insertion point.
3041 bool wxRichTextCtrl::LineBreak()
3044 text
= wxRichTextLineBreakChar
;
3045 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, text
, this);
3048 // ----------------------------------------------------------------------------
3049 // Clipboard operations
3050 // ----------------------------------------------------------------------------
3052 void wxRichTextCtrl::Copy()
3056 wxRichTextRange range
= GetInternalSelectionRange();
3057 GetBuffer().CopyToClipboard(range
);
3061 void wxRichTextCtrl::Cut()
3065 wxRichTextRange range
= GetInternalSelectionRange();
3066 GetBuffer().CopyToClipboard(range
);
3068 DeleteSelectedContent();
3074 void wxRichTextCtrl::Paste()
3078 BeginBatchUndo(_("Paste"));
3080 long newPos
= m_caretPosition
;
3081 DeleteSelectedContent(& newPos
);
3083 GetBuffer().PasteFromClipboard(newPos
);
3089 void wxRichTextCtrl::DeleteSelection()
3091 if (CanDeleteSelection())
3093 DeleteSelectedContent();
3097 bool wxRichTextCtrl::HasSelection() const
3099 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
3102 bool wxRichTextCtrl::HasUnfocusedSelection() const
3104 return m_selection
.IsValid();
3107 bool wxRichTextCtrl::CanCopy() const
3109 // Can copy if there's a selection
3110 return HasSelection();
3113 bool wxRichTextCtrl::CanCut() const
3115 return CanDeleteSelection();
3118 bool wxRichTextCtrl::CanPaste() const
3120 if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
3123 return GetBuffer().CanPasteFromClipboard();
3126 bool wxRichTextCtrl::CanDeleteSelection() const
3128 return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange());
3132 // ----------------------------------------------------------------------------
3134 // ----------------------------------------------------------------------------
3136 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
3138 if (m_contextMenu
&& m_contextMenu
!= menu
)
3139 delete m_contextMenu
;
3140 m_contextMenu
= menu
;
3143 void wxRichTextCtrl::SetEditable(bool editable
)
3145 m_editable
= editable
;
3148 void wxRichTextCtrl::SetInsertionPoint(long pos
)
3152 m_caretPosition
= pos
- 1;
3156 SetDefaultStyleToCursorStyle();
3159 void wxRichTextCtrl::SetInsertionPointEnd()
3161 long pos
= GetLastPosition();
3162 SetInsertionPoint(pos
);
3165 long wxRichTextCtrl::GetInsertionPoint() const
3167 return m_caretPosition
+1;
3170 wxTextPos
wxRichTextCtrl::GetLastPosition() const
3172 return GetFocusObject()->GetOwnRange().GetEnd();
3175 // If the return values from and to are the same, there is no
3177 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
3179 if (m_selection
.IsValid())
3181 *from
= m_selection
.GetRange().GetStart();
3182 *to
= m_selection
.GetRange().GetEnd();
3192 bool wxRichTextCtrl::IsEditable() const
3197 // ----------------------------------------------------------------------------
3199 // ----------------------------------------------------------------------------
3201 void wxRichTextCtrl::SetSelection(long from
, long to
)
3203 // if from and to are both -1, it means (in wxWidgets) that all text should
3205 if ( (from
== -1) && (to
== -1) )
3208 to
= GetLastPosition()+1;
3217 wxRichTextSelection oldSelection
= m_selection
;
3219 m_selectionAnchor
= from
-1;
3220 m_selectionAnchorObject
= NULL
;
3221 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
3223 m_caretPosition
= wxMax(-1, to
-1);
3225 RefreshForSelectionChange(oldSelection
, m_selection
);
3230 // ----------------------------------------------------------------------------
3232 // ----------------------------------------------------------------------------
3234 void wxRichTextCtrl::Replace(long from
, long to
,
3235 const wxString
& value
)
3237 BeginBatchUndo(_("Replace"));
3239 SetSelection(from
, to
);
3241 wxRichTextAttr attr
= GetDefaultStyle();
3243 DeleteSelectedContent();
3245 SetDefaultStyle(attr
);
3247 DoWriteText(value
, SetValue_SelectionOnly
);
3252 void wxRichTextCtrl::Remove(long from
, long to
)
3256 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
3263 bool wxRichTextCtrl::IsModified() const
3265 return m_buffer
.IsModified();
3268 void wxRichTextCtrl::MarkDirty()
3270 m_buffer
.Modify(true);
3273 void wxRichTextCtrl::DiscardEdits()
3275 m_caretPositionForDefaultStyle
= -2;
3276 m_buffer
.Modify(false);
3277 m_buffer
.GetCommandProcessor()->ClearCommands();
3280 int wxRichTextCtrl::GetNumberOfLines() const
3282 return GetFocusObject()->GetParagraphCount();
3285 // ----------------------------------------------------------------------------
3286 // Positions <-> coords
3287 // ----------------------------------------------------------------------------
3289 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3291 return GetFocusObject()->XYToPosition(x
, y
);
3294 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3296 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3299 // ----------------------------------------------------------------------------
3301 // ----------------------------------------------------------------------------
3303 void wxRichTextCtrl::ShowPosition(long pos
)
3305 if (!IsPositionVisible(pos
))
3306 ScrollIntoView(pos
-1, WXK_DOWN
);
3309 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3311 return GetFocusObject()->GetParagraphLength(lineNo
);
3314 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3316 return GetFocusObject()->GetParagraphText(lineNo
);
3319 // ----------------------------------------------------------------------------
3321 // ----------------------------------------------------------------------------
3323 void wxRichTextCtrl::Undo()
3327 GetCommandProcessor()->Undo();
3331 void wxRichTextCtrl::Redo()
3335 GetCommandProcessor()->Redo();
3339 bool wxRichTextCtrl::CanUndo() const
3341 return GetCommandProcessor()->CanUndo() && IsEditable();
3344 bool wxRichTextCtrl::CanRedo() const
3346 return GetCommandProcessor()->CanRedo() && IsEditable();
3349 // ----------------------------------------------------------------------------
3350 // implementation details
3351 // ----------------------------------------------------------------------------
3353 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3355 SetValue(event
.GetString());
3356 GetEventHandler()->ProcessEvent(event
);
3359 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3361 // By default, load the first file into the text window.
3362 if (event
.GetNumberOfFiles() > 0)
3364 LoadFile(event
.GetFiles()[0]);
3368 wxSize
wxRichTextCtrl::DoGetBestSize() const
3370 return wxSize(10, 10);
3373 // ----------------------------------------------------------------------------
3374 // standard handlers for standard edit menu events
3375 // ----------------------------------------------------------------------------
3377 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3382 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3387 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3392 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3397 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3402 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3407 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3409 event
.Enable( CanCut() );
3412 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3414 event
.Enable( CanCopy() );
3417 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3419 event
.Enable( CanDeleteSelection() );
3422 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3424 event
.Enable( CanPaste() );
3427 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3429 event
.Enable( CanUndo() );
3430 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3433 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3435 event
.Enable( CanRedo() );
3436 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3439 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3441 if (GetLastPosition() > 0)
3445 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3447 event
.Enable(GetLastPosition() > 0);
3450 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3452 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3453 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3455 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3456 if (obj
&& CanEditProperties(obj
))
3457 EditProperties(obj
, this);
3459 m_contextMenuPropertiesInfo
.Clear();
3463 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3465 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3466 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3469 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3471 if (event
.GetEventObject() != this)
3477 ShowContextMenu(m_contextMenu
, event
.GetPosition());
3480 // Prepares the context menu, adding appropriate property-editing commands.
3481 // Returns the number of property commands added.
3482 int wxRichTextCtrl::PrepareContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3484 wxClientDC
dc(this);
3486 dc
.SetFont(GetFont());
3488 m_contextMenuPropertiesInfo
.Clear();
3491 wxRichTextObject
* hitObj
= NULL
;
3492 wxRichTextObject
* contextObj
= NULL
;
3493 if (pt
!= wxDefaultPosition
)
3495 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3496 wxRichTextDrawingContext
context(& GetBuffer());
3497 int hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
);
3499 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3501 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3502 if (hitObj
&& actualContainer
)
3504 if (actualContainer
->AcceptsFocus())
3506 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3507 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3510 if (addPropertyCommands
)
3511 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3515 if (addPropertyCommands
)
3516 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3521 if (addPropertyCommands
)
3522 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3527 // Invoked from the keyboard, so don't set the caret position and don't use the event
3529 hitObj
= GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition
+1);
3531 contextObj
= hitObj
->GetParentContainer();
3533 contextObj
= GetFocusObject();
3535 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3536 if (hitObj
&& actualContainer
)
3538 if (addPropertyCommands
)
3539 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3543 if (addPropertyCommands
)
3544 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3550 if (addPropertyCommands
)
3551 m_contextMenuPropertiesInfo
.AddMenuItems(menu
);
3552 return m_contextMenuPropertiesInfo
.GetCount();
3558 // Shows the context menu, adding appropriate property-editing commands
3559 bool wxRichTextCtrl::ShowContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3563 PrepareContextMenu(menu
, pt
, addPropertyCommands
);
3571 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3573 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3576 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3578 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3581 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3583 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3586 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3588 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3591 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
, int flags
)
3593 GetFocusObject()->SetStyle(obj
, textAttr
, flags
);
3596 // extended style setting operation with flags including:
3597 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3598 // see richtextbuffer.h for more details.
3600 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3602 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3605 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3607 return GetBuffer().SetDefaultStyle(style
);
3610 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3612 wxRichTextAttr
attr1(style
);
3613 attr1
.GetTextBoxAttr().Reset();
3614 return GetBuffer().SetDefaultStyle(attr1
);
3617 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3619 return GetBuffer().GetDefaultStyle();
3622 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3624 wxRichTextAttr attr
;
3625 if (GetFocusObject()->GetStyle(position
, attr
))
3634 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3636 return GetFocusObject()->GetStyle(position
, style
);
3639 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3641 wxRichTextAttr attr
;
3642 if (container
->GetStyle(position
, attr
))
3651 // get the common set of styles for the range
3652 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3654 wxRichTextAttr attr
;
3655 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3664 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3666 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3669 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3671 return container
->GetStyleForRange(range
.ToInternal(), style
);
3674 /// Get the content (uncombined) attributes for this position.
3675 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3677 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3680 /// Get the content (uncombined) attributes for this position.
3681 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3683 return container
->GetUncombinedStyle(position
, style
);
3686 bool wxRichTextCtrl::SetProperties(const wxRichTextRange
& range
, const wxRichTextProperties
& properties
, int flags
)
3688 return GetFocusObject()->SetProperties(range
.ToInternal(), properties
, flags
);
3691 /// Set font, and also the buffer attributes
3692 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3694 wxControl::SetFont(font
);
3696 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3698 GetBuffer().SetBasicStyle(attr
);
3700 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3706 /// Transform logical to physical
3707 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3710 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3715 /// Transform physical to logical
3716 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3719 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3724 /// Position the caret
3725 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3730 //wxLogDebug(wxT("PositionCaret"));
3733 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3735 wxPoint newPt
= caretRect
.GetPosition();
3736 wxSize newSz
= caretRect
.GetSize();
3737 wxPoint pt
= GetPhysicalPoint(newPt
);
3738 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3741 if (GetCaret()->GetSize() != newSz
)
3742 GetCaret()->SetSize(newSz
);
3744 // Adjust size so the caret size and position doesn't appear in the margins
3745 if (((pt
.y
+ newSz
.y
) <= GetBuffer().GetTopMargin()) || (pt
.y
>= (GetClientSize().y
- GetBuffer().GetBottomMargin())))
3750 else if (pt
.y
< GetBuffer().GetTopMargin() && (pt
.y
+ newSz
.y
) > GetBuffer().GetTopMargin())
3752 newSz
.y
-= (GetBuffer().GetTopMargin() - pt
.y
);
3755 pt
.y
= GetBuffer().GetTopMargin();
3756 GetCaret()->SetSize(newSz
);
3759 else if (pt
.y
< (GetClientSize().y
- GetBuffer().GetBottomMargin()) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- GetBuffer().GetBottomMargin()))
3761 newSz
.y
= GetClientSize().y
- GetBuffer().GetBottomMargin() - pt
.y
;
3762 GetCaret()->SetSize(newSz
);
3765 GetCaret()->Move(pt
);
3771 /// Get the caret height and position for the given character position
3772 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3774 wxClientDC
dc(this);
3775 dc
.SetFont(GetFont());
3783 container
= GetFocusObject();
3785 wxRichTextDrawingContext
context(& GetBuffer());
3786 if (container
->FindPosition(dc
, context
, position
, pt
, & height
, m_caretAtLineStart
))
3788 // Caret height can't be zero
3790 height
= dc
.GetCharHeight();
3792 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3799 /// Gets the line for the visible caret position. If the caret is
3800 /// shown at the very end of the line, it means the next character is actually
3801 /// on the following line. So let's get the line we're expecting to find
3802 /// if this is the case.
3803 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3805 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3806 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3809 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3810 if (caretPosition
== lineRange
.GetStart()-1 &&
3811 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3813 if (!m_caretAtLineStart
)
3814 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3821 /// Move the caret to the given character position
3822 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3824 if (GetBuffer().IsDirty())
3828 container
= GetFocusObject();
3830 if (pos
<= container
->GetOwnRange().GetEnd())
3832 SetCaretPosition(pos
, showAtLineStart
);
3834 PositionCaret(container
);
3842 /// Layout the buffer: which we must do before certain operations, such as
3843 /// setting the caret position.
3844 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3846 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3848 wxRect
availableSpace(GetClientSize());
3849 if (availableSpace
.width
== 0)
3850 availableSpace
.width
= 10;
3851 if (availableSpace
.height
== 0)
3852 availableSpace
.height
= 10;
3854 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3855 if (onlyVisibleRect
)
3857 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3858 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3861 wxClientDC
dc(this);
3862 dc
.SetFont(GetFont());
3866 wxRichTextDrawingContext
context(& GetBuffer());
3867 GetBuffer().Defragment();
3868 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3869 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, flags
);
3870 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3879 /// Is all of the selection, or the current caret position, bold?
3880 bool wxRichTextCtrl::IsSelectionBold()
3884 wxRichTextAttr attr
;
3885 wxRichTextRange range
= GetSelectionRange();
3886 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3887 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3889 return HasCharacterAttributes(range
, attr
);
3893 // If no selection, then we need to combine current style with default style
3894 // to see what the effect would be if we started typing.
3895 wxRichTextAttr attr
;
3896 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3898 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3899 if (GetStyle(pos
, attr
))
3901 if (IsDefaultStyleShowing())
3902 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3903 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3909 /// Is all of the selection, or the current caret position, italics?
3910 bool wxRichTextCtrl::IsSelectionItalics()
3914 wxRichTextRange range
= GetSelectionRange();
3915 wxRichTextAttr attr
;
3916 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3917 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3919 return HasCharacterAttributes(range
, attr
);
3923 // If no selection, then we need to combine current style with default style
3924 // to see what the effect would be if we started typing.
3925 wxRichTextAttr attr
;
3926 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3928 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3929 if (GetStyle(pos
, attr
))
3931 if (IsDefaultStyleShowing())
3932 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3933 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3939 /// Is all of the selection, or the current caret position, underlined?
3940 bool wxRichTextCtrl::IsSelectionUnderlined()
3944 wxRichTextRange range
= GetSelectionRange();
3945 wxRichTextAttr attr
;
3946 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3947 attr
.SetFontUnderlined(true);
3949 return HasCharacterAttributes(range
, attr
);
3953 // If no selection, then we need to combine current style with default style
3954 // to see what the effect would be if we started typing.
3955 wxRichTextAttr attr
;
3956 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3957 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3959 if (GetStyle(pos
, attr
))
3961 if (IsDefaultStyleShowing())
3962 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3963 return attr
.GetFontUnderlined();
3969 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3970 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
3972 wxRichTextAttr attr
;
3973 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3974 attr
.SetTextEffectFlags(flag
);
3975 attr
.SetTextEffects(flag
);
3979 return HasCharacterAttributes(GetSelectionRange(), attr
);
3983 // If no selection, then we need to combine current style with default style
3984 // to see what the effect would be if we started typing.
3985 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3986 if (GetStyle(pos
, attr
))
3988 if (IsDefaultStyleShowing())
3989 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3990 return (attr
.GetTextEffectFlags() & flag
) != 0;
3996 /// Apply bold to the selection
3997 bool wxRichTextCtrl::ApplyBoldToSelection()
3999 wxRichTextAttr attr
;
4000 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
4001 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
4004 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4007 wxRichTextAttr current
= GetDefaultStyleEx();
4008 current
.Apply(attr
);
4009 SetAndShowDefaultStyle(current
);
4014 /// Apply italic to the selection
4015 bool wxRichTextCtrl::ApplyItalicToSelection()
4017 wxRichTextAttr attr
;
4018 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
4019 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
4022 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4025 wxRichTextAttr current
= GetDefaultStyleEx();
4026 current
.Apply(attr
);
4027 SetAndShowDefaultStyle(current
);
4032 /// Apply underline to the selection
4033 bool wxRichTextCtrl::ApplyUnderlineToSelection()
4035 wxRichTextAttr attr
;
4036 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4037 attr
.SetFontUnderlined(!IsSelectionUnderlined());
4040 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4043 wxRichTextAttr current
= GetDefaultStyleEx();
4044 current
.Apply(attr
);
4045 SetAndShowDefaultStyle(current
);
4050 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
4051 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
4053 wxRichTextAttr attr
;
4054 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4055 attr
.SetTextEffectFlags(flags
);
4056 if (!DoesSelectionHaveTextEffectFlag(flags
))
4057 attr
.SetTextEffects(flags
);
4059 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
4062 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4065 wxRichTextAttr current
= GetDefaultStyleEx();
4066 current
.Apply(attr
);
4067 SetAndShowDefaultStyle(current
);
4072 /// Is all of the selection aligned according to the specified flag?
4073 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
4075 wxRichTextRange range
;
4077 range
= GetSelectionRange();
4079 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
4081 wxRichTextAttr attr
;
4082 attr
.SetAlignment(alignment
);
4084 return HasParagraphAttributes(range
, attr
);
4087 /// Apply alignment to the selection
4088 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
4090 wxRichTextAttr attr
;
4091 attr
.SetAlignment(alignment
);
4093 return SetStyle(GetSelectionRange(), attr
);
4096 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
4098 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
4103 /// Apply a named style to the selection
4104 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
4106 // Flags are defined within each definition, so only certain
4107 // attributes are applied.
4108 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
4110 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
4112 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
4114 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4116 wxRichTextRange range
;
4119 range
= GetSelectionRange();
4122 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4123 range
= wxRichTextRange(pos
, pos
+1);
4126 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
4129 bool isPara
= false;
4131 // Make sure the attr has the style name
4132 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
4135 attr
.SetParagraphStyleName(def
->GetName());
4137 // If applying a paragraph style, we only want the paragraph nodes to adopt these
4138 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
4139 // to change its style independently.
4140 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4142 else if (def
->IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition
)))
4143 attr
.SetCharacterStyleName(def
->GetName());
4144 else if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4145 attr
.GetTextBoxAttr().SetBoxStyleName(def
->GetName());
4147 if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4149 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4151 SetStyle(GetFocusObject(), attr
);
4157 else if (HasSelection())
4158 return SetStyleEx(GetSelectionRange(), attr
, flags
);
4161 wxRichTextAttr current
= GetDefaultStyleEx();
4162 wxRichTextAttr
defaultStyle(attr
);
4165 // Don't apply extra character styles since they are already implied
4166 // in the paragraph style
4167 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
4169 current
.Apply(defaultStyle
);
4170 SetAndShowDefaultStyle(current
);
4172 // If it's a paragraph style, we want to apply the style to the
4173 // current paragraph even if we didn't select any text.
4176 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4177 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
4180 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
4187 /// Apply the style sheet to the buffer, for example if the styles have changed.
4188 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4191 styleSheet
= GetBuffer().GetStyleSheet();
4195 if (GetBuffer().ApplyStyleSheet(styleSheet
))
4197 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4205 /// Sets the default style to the style under the cursor
4206 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4208 wxRichTextAttr attr
;
4209 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
4211 // If at the start of a paragraph, use the next position.
4212 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4214 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(pos
);
4215 if (obj
&& obj
->IsTopLevel())
4217 // Don't use the attributes of a top-level object, since they might apply
4218 // to content of the object, e.g. background colour.
4219 SetDefaultStyle(wxRichTextAttr());
4222 else if (GetUncombinedStyle(pos
, attr
))
4224 SetDefaultStyle(attr
);
4231 /// Returns the first visible position in the current view
4232 long wxRichTextCtrl::GetFirstVisiblePosition() const
4234 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
4236 return line
->GetAbsoluteRange().GetStart();
4241 /// Get the first visible point in the window
4242 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
4245 int startXUnits
, startYUnits
;
4247 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
4248 GetViewStart(& startXUnits
, & startYUnits
);
4250 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
4253 /// The adjusted caret position is the character position adjusted to take
4254 /// into account whether we're at the start of a paragraph, in which case
4255 /// style information should be taken from the next position, not current one.
4256 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
4258 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
4260 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
4265 /// Get/set the selection range in character positions. -1, -1 means no selection.
4266 /// The range is in API convention, i.e. a single character selection is denoted
4268 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
4270 wxRichTextRange range
= GetInternalSelectionRange();
4271 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
4272 range
.SetEnd(range
.GetEnd() + 1);
4276 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
4278 SetSelection(range
.GetStart(), range
.GetEnd());
4282 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4284 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4287 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4289 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4292 /// Clear list for given range
4293 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
4295 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
4298 /// Number/renumber any list elements in the given range
4299 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4301 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4304 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4306 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4309 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4310 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4312 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
4315 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4317 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
4320 /// Deletes the content in the given range
4321 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
4323 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
4326 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
4328 if (sm_availableFontNames
.GetCount() == 0)
4330 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
4331 sm_availableFontNames
.Sort();
4333 return sm_availableFontNames
;
4336 void wxRichTextCtrl::ClearAvailableFontNames()
4338 sm_availableFontNames
.Clear();
4341 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
4343 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4345 wxTextAttrEx basicStyle
= GetBasicStyle();
4346 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4347 SetBasicStyle(basicStyle
);
4348 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4353 // Refresh the area affected by a selection change
4354 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4356 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4357 // the selection contains whole containers rather than just text, so refresh everything
4358 // for now as it would be hard to compute the rectangle bounding all selections.
4359 // TODO: improve on this.
4360 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4361 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4367 wxRichTextRange oldRange
, newRange
;
4368 if (oldSelection
.IsValid())
4369 oldRange
= oldSelection
.GetRange();
4371 oldRange
= wxRICHTEXT_NO_SELECTION
;
4372 if (newSelection
.IsValid())
4373 newRange
= newSelection
.GetRange();
4375 newRange
= wxRICHTEXT_NO_SELECTION
;
4377 // Calculate the refresh rectangle - just the affected lines
4378 long firstPos
, lastPos
;
4379 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4381 firstPos
= newRange
.GetStart();
4382 lastPos
= newRange
.GetEnd();
4384 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4386 firstPos
= oldRange
.GetStart();
4387 lastPos
= oldRange
.GetEnd();
4389 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4395 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4396 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4399 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4400 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4402 if (firstLine
&& lastLine
)
4404 wxSize clientSize
= GetClientSize();
4405 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
4406 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
4409 pt1
.y
= wxMax(0, pt1
.y
);
4411 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4413 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4414 RefreshRect(rect
, false);
4422 // margins functions
4423 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4425 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4426 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4427 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4428 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4433 wxPoint
wxRichTextCtrl::DoGetMargins() const
4435 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4436 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4439 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4441 if (obj
&& !obj
->AcceptsFocus())
4444 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4445 bool changingContainer
= (m_focusObject
!= obj
);
4447 if (changingContainer
&& HasSelection())
4450 m_focusObject
= obj
;
4453 m_focusObject
= & m_buffer
;
4455 if (setCaretPosition
&& changingContainer
)
4457 m_selection
.Reset();
4458 m_selectionAnchor
= -2;
4459 m_selectionAnchorObject
= NULL
;
4460 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4464 m_caretAtLineStart
= false;
4465 MoveCaret(pos
, m_caretAtLineStart
);
4466 SetDefaultStyleToCursorStyle();
4468 wxRichTextEvent
cmdEvent(
4469 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4471 cmdEvent
.SetEventObject(this);
4472 cmdEvent
.SetPosition(m_caretPosition
+1);
4473 cmdEvent
.SetOldContainer(oldContainer
);
4474 cmdEvent
.SetContainer(m_focusObject
);
4476 GetEventHandler()->ProcessEvent(cmdEvent
);
4481 #if wxUSE_DRAG_AND_DROP
4482 void wxRichTextCtrl::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxDragResult def
, wxDataObject
* DataObj
)
4486 if ((def
!= wxDragCopy
) && (def
!= wxDragMove
))
4491 if (!GetSelection().IsValid())
4496 wxRichTextParagraphLayoutBox
* originContainer
= GetSelection().GetContainer();
4497 wxRichTextParagraphLayoutBox
* destContainer
= GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4499 wxRichTextBuffer
* richTextBuffer
= ((wxRichTextBufferDataObject
*)DataObj
)->GetRichTextBuffer();
4502 long position
= GetCaretPosition();
4503 wxRichTextRange selectionrange
= GetInternalSelectionRange();
4504 if (selectionrange
.Contains(position
) && (def
== wxDragMove
))
4506 // It doesn't make sense to move onto itself
4510 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4511 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4512 bool DeleteAfter
= (def
== wxDragMove
) && (position
> selectionrange
.GetEnd());
4513 if ((def
== wxDragMove
) && !DeleteAfter
)
4515 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4516 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4519 destContainer
->InsertParagraphsWithUndo(&GetBuffer(), position
+1, *richTextBuffer
, this, 0);
4520 ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
4522 delete richTextBuffer
;
4526 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4527 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4534 #endif // wxUSE_DRAG_AND_DROP
4537 #if wxUSE_DRAG_AND_DROP
4538 bool wxRichTextDropSource::GiveFeedback(wxDragResult
WXUNUSED(effect
))
4540 wxCHECK_MSG(m_rtc
, false, wxT("NULL m_rtc"));
4544 wxRichTextObject
* hitObj
= NULL
;
4545 wxRichTextParagraphLayoutBox
* container
= m_rtc
->FindContainerAtPoint(m_rtc
->ScreenToClient(wxGetMousePosition()), position
, hit
, hitObj
);
4547 if (!(hit
& wxRICHTEXT_HITTEST_NONE
) && container
&& container
->AcceptsFocus())
4549 m_rtc
->StoreFocusObject(container
);
4550 m_rtc
->SetCaretPositionAfterClick(container
, position
, hit
);
4553 return false; // so that the base-class sets a cursor
4555 #endif // wxUSE_DRAG_AND_DROP
4557 bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), const wxRichTextRange
& WXUNUSED(range
)) const
4562 bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), long WXUNUSED(pos
)) const
4567 void wxRichTextCtrl::EnableVerticalScrollbar(bool enable
)
4569 m_verticalScrollbarEnabled
= enable
;
4574 #if wxRICHTEXT_USE_OWN_CARET
4576 // ----------------------------------------------------------------------------
4577 // initialization and destruction
4578 // ----------------------------------------------------------------------------
4580 void wxRichTextCaret::Init()
4583 m_refreshEnabled
= true;
4587 m_richTextCtrl
= NULL
;
4588 m_needsUpdate
= false;
4592 wxRichTextCaret::~wxRichTextCaret()
4594 if (m_timer
.IsRunning())
4598 // ----------------------------------------------------------------------------
4599 // showing/hiding/moving the caret (base class interface)
4600 // ----------------------------------------------------------------------------
4602 void wxRichTextCaret::DoShow()
4606 if (!m_timer
.IsRunning())
4607 m_timer
.Start(GetBlinkTime());
4612 void wxRichTextCaret::DoHide()
4614 if (m_timer
.IsRunning())
4620 void wxRichTextCaret::DoMove()
4626 if (m_xOld
!= -1 && m_yOld
!= -1)
4628 if (m_richTextCtrl
&& m_refreshEnabled
)
4630 wxRect
rect(GetPosition(), GetSize());
4631 m_richTextCtrl
->RefreshRect(rect
, false);
4640 void wxRichTextCaret::DoSize()
4642 int countVisible
= m_countVisible
;
4643 if (countVisible
> 0)
4649 if (countVisible
> 0)
4651 m_countVisible
= countVisible
;
4656 // ----------------------------------------------------------------------------
4657 // handling the focus
4658 // ----------------------------------------------------------------------------
4660 void wxRichTextCaret::OnSetFocus()
4668 void wxRichTextCaret::OnKillFocus()
4673 // ----------------------------------------------------------------------------
4674 // drawing the caret
4675 // ----------------------------------------------------------------------------
4677 void wxRichTextCaret::Refresh()
4679 if (m_richTextCtrl
&& m_refreshEnabled
)
4681 wxRect
rect(GetPosition(), GetSize());
4682 m_richTextCtrl
->RefreshRect(rect
, false);
4686 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4688 dc
->SetPen( *wxBLACK_PEN
);
4690 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4691 dc
->SetPen(*wxBLACK_PEN
);
4693 wxPoint
pt(m_x
, m_y
);
4697 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4699 if (IsVisible() && m_flashOn
)
4700 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4703 void wxRichTextCaret::Notify()
4705 m_flashOn
= !m_flashOn
;
4709 void wxRichTextCaretTimer::Notify()
4714 // wxRICHTEXT_USE_OWN_CARET
4717 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4721 m_labels
.Add(label
);
4729 // Returns number of menu items were added.
4730 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4732 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4733 // If none of the standard properties identifiers are in the menu, add them if necessary.
4734 // If no items to add, just set the text to something generic
4735 if (GetCount() == 0)
4739 menu
->SetLabel(startCmd
, _("&Properties"));
4741 // Delete the others if necessary
4743 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4745 if (menu
->FindItem(i
))
4756 // Find the position of the first properties item
4757 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4759 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4760 if (item
&& item
->GetId() == startCmd
)
4769 int insertBefore
= pos
+1;
4770 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4772 if (menu
->FindItem(i
))
4774 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4778 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4779 menu
->Append(i
, m_labels
[i
- startCmd
]);
4781 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4786 // Delete any old items still left on the menu
4787 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4789 if (menu
->FindItem(i
))
4797 // No existing property identifiers were found, so append to the end of the menu.
4798 menu
->AppendSeparator();
4799 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4801 menu
->Append(i
, m_labels
[i
- startCmd
]);
4809 // Add appropriate menu items for the current container and clicked on object
4810 // (and container's parent, if appropriate).
4811 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl
* ctrl
, wxRichTextObject
* container
, wxRichTextObject
* obj
)
4814 if (obj
&& ctrl
->CanEditProperties(obj
))
4815 AddItem(ctrl
->GetPropertiesMenuLabel(obj
), obj
);
4817 if (container
&& container
!= obj
&& ctrl
->CanEditProperties(container
) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
)) == wxNOT_FOUND
)
4818 AddItem(ctrl
->GetPropertiesMenuLabel(container
), container
);
4820 if (container
&& container
->GetParent() && ctrl
->CanEditProperties(container
->GetParent()) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
->GetParent())) == wxNOT_FOUND
)
4821 AddItem(ctrl
->GetPropertiesMenuLabel(container
->GetParent()), container
->GetParent());