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_caretAtLineStart
= false;
361 #if wxUSE_DRAG_AND_DROP
364 m_fullLayoutRequired
= false;
365 m_fullLayoutTime
= 0;
366 m_fullLayoutSavedPosition
= 0;
367 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
368 m_caretPositionForDefaultStyle
= -2;
369 m_focusObject
= & m_buffer
;
372 void wxRichTextCtrl::DoThaw()
374 if (GetBuffer().IsDirty())
383 void wxRichTextCtrl::Clear()
385 if (GetFocusObject() == & GetBuffer())
387 m_buffer
.ResetAndClearCommands();
388 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
392 GetFocusObject()->Reset();
395 m_caretPosition
= -1;
396 m_caretPositionForDefaultStyle
= -2;
397 m_caretAtLineStart
= false;
399 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
409 wxTextCtrl::SendTextUpdatedEvent(this);
413 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
415 #if !wxRICHTEXT_USE_OWN_CARET
416 if (GetCaret() && !IsFrozen())
419 // Stop the caret refreshing the control from within the
422 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(false);
426 #if wxRICHTEXT_BUFFERED_PAINTING
427 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
437 dc
.SetFont(GetFont());
439 // Paint the background
442 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
444 wxRect
drawingArea(GetUpdateRegion().GetBox());
445 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
447 wxRect
availableSpace(GetClientSize());
448 wxRichTextDrawingContext
context(& GetBuffer());
449 if (GetBuffer().IsDirty())
451 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
452 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
456 wxRect
clipRect(availableSpace
);
457 clipRect
.x
+= GetBuffer().GetLeftMargin();
458 clipRect
.y
+= GetBuffer().GetTopMargin();
459 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
460 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
461 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
462 dc
.SetClippingRegion(clipRect
);
465 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
466 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
468 GetBuffer().Draw(dc
, context
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
470 dc
.DestroyClippingRegion();
472 // Other user defined painting after everything else (i.e. all text) is painted
473 PaintAboveContent(dc
);
475 #if wxRICHTEXT_USE_OWN_CARET
476 if (GetCaret()->IsVisible())
479 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
484 #if !wxRICHTEXT_USE_OWN_CARET
490 ((wxRichTextCaret
*) GetCaret())->EnableRefresh(true);
494 // Empty implementation, to prevent flicker
495 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
499 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
503 #if !wxRICHTEXT_USE_OWN_CARET
509 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
510 // Work around dropouts when control is focused
518 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
523 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
524 // Work around dropouts when control is focused
532 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
537 // Set up the caret for the given position and container, after a mouse click
538 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
540 bool caretAtLineStart
= false;
542 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
544 // If we're at the start of a line (but not first in para)
545 // then we should keep the caret showing at the start of the line
546 // by showing the m_caretAtLineStart flag.
547 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
548 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
550 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
551 caretAtLineStart
= true;
555 if (extendSelection
&& (m_caretPosition
!= position
))
556 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
558 MoveCaret(position
, caretAtLineStart
);
559 SetDefaultStyleToCursorStyle();
565 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
571 dc
.SetFont(GetFont());
573 // TODO: detect change of focus object
575 wxRichTextObject
* hitObj
= NULL
;
576 wxRichTextObject
* contextObj
= NULL
;
577 wxRichTextDrawingContext
context(& GetBuffer());
578 int hit
= GetBuffer().HitTest(dc
, context
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
);
580 #if wxUSE_DRAG_AND_DROP
581 // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop
582 if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position
))
584 // This might be an attempt at initiating Drag'n'Drop. So set the time & flags
586 m_dragStartPoint
= event
.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt
589 m_dragStartTime
= wxDateTime::UNow();
590 #endif // wxUSE_DATETIME
592 // Preserve behaviour of clicking on an object within the selection
593 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
596 return; // Don't skip the event, else the selection will be lost
598 #endif // wxUSE_DRAG_AND_DROP
600 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
602 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
603 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
604 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
606 SetFocusObject(container
, false /* don't set caret position yet */);
612 long oldCaretPos
= m_caretPosition
;
614 SetCaretPositionAfterClick(container
, position
, hit
);
616 // For now, don't handle shift-click when we're selecting multiple objects.
617 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
618 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
627 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
632 if (GetCapture() == this)
635 // See if we clicked on a URL
638 dc
.SetFont(GetFont());
641 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
642 wxRichTextObject
* hitObj
= NULL
;
643 wxRichTextObject
* contextObj
= NULL
;
644 wxRichTextDrawingContext
context(& GetBuffer());
645 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
646 int hit
= GetFocusObject()->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
648 #if wxUSE_DRAG_AND_DROP
651 // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick
652 m_preDrag
= false; // Tell DnD not to happen now: we are processing Left Up ourselves.
654 // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag
656 wxRichTextObject
* hitObj
= NULL
;
657 wxRichTextObject
* contextObj
= NULL
;
658 int hit
= GetBuffer().HitTest(dc
, context
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
);
659 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
660 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
661 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
663 SetFocusObject(container
, false /* don't set caret position yet */);
666 long oldCaretPos
= m_caretPosition
;
668 SetCaretPositionAfterClick(container
, position
, hit
);
670 // For now, don't handle shift-click when we're selecting multiple objects.
671 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
672 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
678 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
680 wxRichTextEvent
cmdEvent(
681 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
683 cmdEvent
.SetEventObject(this);
684 cmdEvent
.SetPosition(position
);
686 cmdEvent
.SetContainer(hitObj
->GetContainer());
688 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
691 if (GetStyle(position
, attr
))
693 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
695 wxString urlTarget
= attr
.GetURL();
696 if (!urlTarget
.IsEmpty())
698 wxMouseEvent
mouseEvent(event
);
700 long startPos
= 0, endPos
= 0;
701 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
704 startPos
= obj
->GetRange().GetStart();
705 endPos
= obj
->GetRange().GetEnd();
708 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
709 InitCommandEvent(urlEvent
);
711 urlEvent
.SetString(urlTarget
);
713 GetEventHandler()->ProcessEvent(urlEvent
);
721 #if wxUSE_DRAG_AND_DROP
723 #endif // wxUSE_DRAG_AND_DROP
725 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
726 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
728 // Put the selection in PRIMARY, if it exists
729 wxTheClipboard
->UsePrimarySelection(true);
731 wxRichTextRange range
= GetInternalSelectionRange();
732 GetFocusObject()->GetBuffer()->CopyToClipboard(range
);
734 wxTheClipboard
->UsePrimarySelection(false);
740 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
742 #if wxUSE_DRAG_AND_DROP
743 // See if we're starting Drag'n'Drop
746 int x
= m_dragStartPoint
.x
- event
.GetPosition().x
;
747 int y
= m_dragStartPoint
.y
- event
.GetPosition().y
;
748 size_t distance
= abs(x
) + abs(y
);
750 wxTimeSpan diff
= wxDateTime::UNow() - m_dragStartTime
;
754 && (diff
.GetMilliseconds() > 100)
761 wxRichTextRange range
= GetInternalSelectionRange();
762 if (range
== wxRICHTEXT_NONE
)
764 // Don't try to drag an empty range
769 // Cache the current situation, to be restored if Drag'n'Drop is cancelled
770 long oldPos
= GetCaretPosition();
771 wxRichTextParagraphLayoutBox
* oldFocus
= GetFocusObject();
773 wxDataObjectComposite
* compositeObject
= new wxDataObjectComposite();
774 wxString text
= GetFocusObject()->GetTextForRange(range
);
776 text
= wxTextFile::Translate(text
, wxTextFileType_Dos
);
778 compositeObject
->Add(new wxTextDataObject(text
), false /* not preferred */);
780 wxRichTextBuffer
* richTextBuf
= new wxRichTextBuffer
;
781 GetFocusObject()->CopyFragment(range
, *richTextBuf
);
782 compositeObject
->Add(new wxRichTextBufferDataObject(richTextBuf
), true /* preferred */);
784 wxRichTextDropSource
source(*compositeObject
, this);
785 // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise
786 // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed
787 BeginBatchUndo(_("Drag"));
788 switch (source
.DoDragDrop(wxDrag_AllowMove
| wxDrag_DefaultMove
))
791 case wxDragCopy
: break;
794 wxLogError(wxT("An error occurred during drag and drop operation"));
797 Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take'
798 SetCaretPosition(oldPos
);
799 SetFocusObject(oldFocus
, false);
808 #endif // wxUSE_DRAG_AND_DROP
812 dc
.SetFont(GetFont());
815 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
816 wxRichTextObject
* hitObj
= NULL
;
817 wxRichTextObject
* contextObj
= NULL
;
821 // If we're dragging, let's only consider positions at this level; otherwise
822 // selecting a range is not going to work.
823 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
826 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
827 container
= GetFocusObject();
829 wxRichTextDrawingContext
context(& GetBuffer());
830 int hit
= container
->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
, flags
);
832 // See if we need to change the cursor
835 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
837 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
839 ProcessMouseMovement(actualContainer
, hitObj
, position
, logicalPt
);
842 SetCursor(m_textCursor
);
845 if (!event
.Dragging())
852 #if wxUSE_DRAG_AND_DROP
857 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
858 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
859 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
861 // Check for dragging across multiple containers
863 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
864 int hit2
= GetBuffer().HitTest(dc
, context
, logicalPt
, position2
, & hitObj2
, & contextObj2
, 0);
865 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
867 // See if we can find a common ancestor
868 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
870 firstContainer
= GetFocusObject();
871 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
875 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
876 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
877 // is the common ancestor.
878 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
881 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
883 wxRichTextObject
* p
= hitObj2
;
886 if (p
->GetParent() == commonAncestor
)
888 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
895 if (commonAncestor
&& firstContainer
&& otherContainer
)
897 // We have now got a second container that shares a parent with the current or anchor object.
898 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
900 // Don't go into common-ancestor selection mode if we still have the same
902 if (otherContainer
!= firstContainer
)
904 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
905 m_selectionAnchorObject
= firstContainer
;
906 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
908 // The common ancestor, such as a table, returns the cell selection
909 // between the anchor and current position.
910 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
915 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
920 if (otherContainer
->AcceptsFocus())
921 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
922 MoveCaret(-1, false);
923 SetDefaultStyleToCursorStyle();
928 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
929 #if wxUSE_DRAG_AND_DROP
934 // TODO: test closeness
935 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
940 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
946 dc
.SetFont(GetFont());
949 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
950 wxRichTextObject
* hitObj
= NULL
;
951 wxRichTextObject
* contextObj
= NULL
;
952 wxRichTextDrawingContext
context(& GetBuffer());
953 int hit
= GetFocusObject()->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
);
955 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
957 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
958 if (actualContainer
&& actualContainer
->AcceptsFocus())
960 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
961 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
965 wxRichTextEvent
cmdEvent(
966 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
968 cmdEvent
.SetEventObject(this);
969 cmdEvent
.SetPosition(position
);
971 cmdEvent
.SetContainer(hitObj
->GetContainer());
973 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
977 /// Left-double-click
978 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
980 wxRichTextEvent
cmdEvent(
981 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
983 cmdEvent
.SetEventObject(this);
984 cmdEvent
.SetPosition(m_caretPosition
+1);
985 cmdEvent
.SetContainer(GetFocusObject());
987 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
989 SelectWord(GetCaretPosition()+1);
994 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
996 wxRichTextEvent
cmdEvent(
997 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
999 cmdEvent
.SetEventObject(this);
1000 cmdEvent
.SetPosition(m_caretPosition
+1);
1001 cmdEvent
.SetContainer(GetFocusObject());
1003 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1006 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1007 // Paste any PRIMARY selection, if it exists
1008 wxTheClipboard
->UsePrimarySelection(true);
1010 wxTheClipboard
->UsePrimarySelection(false);
1015 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
1018 if (event
.CmdDown())
1019 flags
|= wxRICHTEXT_CTRL_DOWN
;
1020 if (event
.ShiftDown())
1021 flags
|= wxRICHTEXT_SHIFT_DOWN
;
1022 if (event
.AltDown())
1023 flags
|= wxRICHTEXT_ALT_DOWN
;
1025 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
1027 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
1029 KeyboardNavigate(event
.GetKeyCode(), flags
);
1033 long keycode
= event
.GetKeyCode();
1093 case WXK_NUMPAD_HOME
:
1094 case WXK_NUMPAD_LEFT
:
1096 case WXK_NUMPAD_RIGHT
:
1097 case WXK_NUMPAD_DOWN
:
1098 case WXK_NUMPAD_PAGEUP
:
1099 case WXK_NUMPAD_PAGEDOWN
:
1100 case WXK_NUMPAD_END
:
1101 case WXK_NUMPAD_BEGIN
:
1102 case WXK_NUMPAD_INSERT
:
1103 case WXK_WINDOWS_LEFT
:
1112 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1113 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
1120 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1125 BeginBatchUndo(_("Delete Text"));
1127 long newPos
= m_caretPosition
;
1129 bool processed
= DeleteSelectedContent(& newPos
);
1135 // Submit range in character positions, which are greater than caret positions,
1136 // so subtract 1 for deleted character and add 1 for conversion to character position.
1139 if (event
.CmdDown())
1141 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1144 wxRichTextRange
range(pos
+1, newPos
);
1145 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1147 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1156 wxRichTextRange
range(newPos
, newPos
);
1157 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1159 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1167 if (GetLastPosition() == -1)
1169 GetFocusObject()->Reset();
1171 m_caretPosition
= -1;
1173 SetDefaultStyleToCursorStyle();
1176 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1180 wxRichTextEvent
cmdEvent(
1181 wxEVT_COMMAND_RICHTEXT_DELETE
,
1183 cmdEvent
.SetEventObject(this);
1184 cmdEvent
.SetFlags(flags
);
1185 cmdEvent
.SetPosition(m_caretPosition
+1);
1186 cmdEvent
.SetContainer(GetFocusObject());
1187 GetEventHandler()->ProcessEvent(cmdEvent
);
1198 // all the other keys modify the controls contents which shouldn't be
1199 // possible if we're read-only
1200 if ( !IsEditable() )
1206 if (event
.GetKeyCode() == WXK_RETURN
)
1208 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1211 long newPos
= m_caretPosition
;
1213 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1218 BeginBatchUndo(_("Insert Text"));
1220 DeleteSelectedContent(& newPos
);
1222 if (event
.ShiftDown())
1225 text
= wxRichTextLineBreakChar
;
1226 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, text
, this);
1227 m_caretAtLineStart
= true;
1231 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1234 SetDefaultStyleToCursorStyle();
1236 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1238 wxRichTextEvent
cmdEvent(
1239 wxEVT_COMMAND_RICHTEXT_RETURN
,
1241 cmdEvent
.SetEventObject(this);
1242 cmdEvent
.SetFlags(flags
);
1243 cmdEvent
.SetPosition(newPos
+1);
1244 cmdEvent
.SetContainer(GetFocusObject());
1246 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1248 // Generate conventional event
1249 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1250 InitCommandEvent(textEvent
);
1252 GetEventHandler()->ProcessEvent(textEvent
);
1256 else if (event
.GetKeyCode() == WXK_BACK
)
1258 long newPos
= m_caretPosition
;
1260 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1265 BeginBatchUndo(_("Delete Text"));
1267 bool processed
= DeleteSelectedContent(& newPos
);
1273 // Submit range in character positions, which are greater than caret positions,
1274 // so subtract 1 for deleted character and add 1 for conversion to character position.
1277 if (event
.CmdDown())
1279 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1282 wxRichTextRange
range(pos
+1, newPos
);
1283 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1285 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1294 wxRichTextRange
range(newPos
, newPos
);
1295 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1297 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1305 if (GetLastPosition() == -1)
1307 GetFocusObject()->Reset();
1309 m_caretPosition
= -1;
1311 SetDefaultStyleToCursorStyle();
1314 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1318 wxRichTextEvent
cmdEvent(
1319 wxEVT_COMMAND_RICHTEXT_DELETE
,
1321 cmdEvent
.SetEventObject(this);
1322 cmdEvent
.SetFlags(flags
);
1323 cmdEvent
.SetPosition(m_caretPosition
+1);
1324 cmdEvent
.SetContainer(GetFocusObject());
1325 GetEventHandler()->ProcessEvent(cmdEvent
);
1330 else if (event
.GetKeyCode() == WXK_DELETE
)
1332 long newPos
= m_caretPosition
;
1334 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1339 BeginBatchUndo(_("Delete Text"));
1341 bool processed
= DeleteSelectedContent(& newPos
);
1347 // Submit range in character positions, which are greater than caret positions,
1348 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1350 if (event
.CmdDown())
1352 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1353 if (pos
!= -1 && (pos
> newPos
))
1355 wxRichTextRange
range(newPos
+1, pos
);
1356 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1358 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1365 if (!processed
&& newPos
< (GetLastPosition()-1))
1367 wxRichTextRange
range(newPos
+1, newPos
+1);
1368 if (CanDeleteRange(* GetFocusObject(), range
.FromInternal()))
1370 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1378 if (GetLastPosition() == -1)
1380 GetFocusObject()->Reset();
1382 m_caretPosition
= -1;
1384 SetDefaultStyleToCursorStyle();
1387 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1391 wxRichTextEvent
cmdEvent(
1392 wxEVT_COMMAND_RICHTEXT_DELETE
,
1394 cmdEvent
.SetEventObject(this);
1395 cmdEvent
.SetFlags(flags
);
1396 cmdEvent
.SetPosition(m_caretPosition
+1);
1397 cmdEvent
.SetContainer(GetFocusObject());
1398 GetEventHandler()->ProcessEvent(cmdEvent
);
1405 long keycode
= event
.GetKeyCode();
1417 if (event
.CmdDown())
1419 // Fixes AltGr+key with European input languages on Windows
1420 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1427 wxRichTextEvent
cmdEvent(
1428 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1430 cmdEvent
.SetEventObject(this);
1431 cmdEvent
.SetFlags(flags
);
1433 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1435 cmdEvent
.SetCharacter((wxChar
) keycode
);
1437 cmdEvent
.SetPosition(m_caretPosition
+1);
1438 cmdEvent
.SetContainer(GetFocusObject());
1440 if (keycode
== wxT('\t'))
1442 // See if we need to promote or demote the selection or paragraph at the cursor
1443 // position, instead of inserting a tab.
1444 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1445 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1446 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1448 wxRichTextRange range
;
1450 range
= GetSelectionRange();
1452 range
= para
->GetRange().FromInternal();
1454 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1456 PromoteList(promoteBy
, range
, NULL
);
1458 GetEventHandler()->ProcessEvent(cmdEvent
);
1464 if (!CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
1467 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1470 BeginBatchUndo(_("Insert Text"));
1472 long newPos
= m_caretPosition
;
1473 DeleteSelectedContent(& newPos
);
1476 wxString str
= event
.GetUnicodeKey();
1478 wxString str
= (wxChar
) event
.GetKeyCode();
1480 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, str
, this, 0);
1484 SetDefaultStyleToCursorStyle();
1485 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1487 cmdEvent
.SetPosition(m_caretPosition
);
1488 GetEventHandler()->ProcessEvent(cmdEvent
);
1496 bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox
* container
, wxRichTextObject
* WXUNUSED(obj
), long position
, const wxPoint
& WXUNUSED(pos
))
1498 wxRichTextAttr attr
;
1499 if (container
&& GetStyle(position
, attr
, container
))
1501 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
1503 SetCursor(m_urlCursor
);
1505 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
1507 SetCursor(m_textCursor
);
1515 /// Delete content if there is a selection, e.g. when pressing a key.
1516 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1520 long pos
= m_selection
.GetRange().GetStart();
1521 wxRichTextRange range
= m_selection
.GetRange();
1523 // SelectAll causes more to be selected than doing it interactively,
1524 // and causes a new paragraph to be inserted. So for multiline buffers,
1525 // don't delete the final position.
1526 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1527 range
.SetEnd(range
.GetEnd()-1);
1529 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1530 m_selection
.Reset();
1531 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1541 /// Keyboard navigation
1545 Left: left one character
1546 Right: right one character
1549 Ctrl-Left: left one word
1550 Ctrl-Right: right one word
1551 Ctrl-Up: previous paragraph start
1552 Ctrl-Down: next start of paragraph
1555 Ctrl-Home: start of document
1556 Ctrl-End: end of document
1557 Page-Up: Up a screen
1558 Page-Down: Down a screen
1562 Ctrl-Alt-PgUp: Start of window
1563 Ctrl-Alt-PgDn: End of window
1564 F8: Start selection mode
1565 Esc: End selection mode
1567 Adding Shift does the above but starts/extends selection.
1572 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1574 bool success
= false;
1576 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1578 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1579 success
= WordRight(1, flags
);
1581 success
= MoveRight(1, flags
);
1583 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1585 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1586 success
= WordLeft(1, flags
);
1588 success
= MoveLeft(1, flags
);
1590 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1592 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1593 success
= MoveToParagraphStart(flags
);
1595 success
= MoveUp(1, flags
);
1597 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1599 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1600 success
= MoveToParagraphEnd(flags
);
1602 success
= MoveDown(1, flags
);
1604 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1606 success
= PageUp(1, flags
);
1608 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1610 success
= PageDown(1, flags
);
1612 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1614 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1615 success
= MoveHome(flags
);
1617 success
= MoveToLineStart(flags
);
1619 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1621 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1622 success
= MoveEnd(flags
);
1624 success
= MoveToLineEnd(flags
);
1629 ScrollIntoView(m_caretPosition
, keyCode
);
1630 SetDefaultStyleToCursorStyle();
1636 /// Extend the selection. Selections are in caret positions.
1637 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1639 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1641 if (oldPos
== newPos
)
1644 wxRichTextSelection oldSelection
= m_selection
;
1646 m_selection
.SetContainer(GetFocusObject());
1648 wxRichTextRange oldRange
;
1649 if (m_selection
.IsValid())
1650 oldRange
= m_selection
.GetRange();
1652 oldRange
= wxRICHTEXT_NO_SELECTION
;
1653 wxRichTextRange newRange
;
1655 // If not currently selecting, start selecting
1656 if (oldRange
.GetStart() == -2)
1658 m_selectionAnchor
= oldPos
;
1660 if (oldPos
> newPos
)
1661 newRange
.SetRange(newPos
+1, oldPos
);
1663 newRange
.SetRange(oldPos
+1, newPos
);
1667 // Always ensure that the selection range start is greater than
1669 if (newPos
> m_selectionAnchor
)
1670 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1671 else if (newPos
== m_selectionAnchor
)
1672 newRange
= wxRichTextRange(-2, -2);
1674 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1677 m_selection
.SetRange(newRange
);
1679 RefreshForSelectionChange(oldSelection
, m_selection
);
1681 if (newRange
.GetStart() > newRange
.GetEnd())
1683 wxLogDebug(wxT("Strange selection range"));
1692 /// Scroll into view, returning true if we scrolled.
1693 /// This takes a _caret_ position.
1694 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1696 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1702 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1704 int startXUnits
, startYUnits
;
1705 GetViewStart(& startXUnits
, & startYUnits
);
1706 int startY
= startYUnits
* ppuY
;
1709 GetVirtualSize(& sx
, & sy
);
1715 wxRect rect
= line
->GetRect();
1717 bool scrolled
= false;
1719 wxSize clientSize
= GetClientSize();
1721 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1724 wxClientDC
dc(this);
1725 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1726 topMargin
, bottomMargin
);
1728 // clientSize.y -= GetBuffer().GetBottomMargin();
1729 clientSize
.y
-= bottomMargin
;
1731 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1733 int y
= rect
.y
- GetClientSize().y
/2;
1734 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1735 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1737 if (startYUnits
!= yUnits
)
1739 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1742 #if !wxRICHTEXT_USE_OWN_CARET
1752 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1753 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1754 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1755 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1757 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1759 // Make it scroll so this item is at the bottom
1761 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1762 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1764 // If we're still off the screen, scroll another line down
1765 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1768 if (startYUnits
!= yUnits
)
1770 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1774 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1776 // Make it scroll so this item is at the top
1778 int y
= rect
.y
- GetBuffer().GetTopMargin();
1779 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1781 if (startYUnits
!= yUnits
)
1783 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1789 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1790 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1791 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1792 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1794 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1796 // Make it scroll so this item is at the top
1798 int y
= rect
.y
- GetBuffer().GetTopMargin();
1799 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1801 if (startYUnits
!= yUnits
)
1803 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1807 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1809 // Make it scroll so this item is at the bottom
1811 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1812 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1814 // If we're still off the screen, scroll another line down
1815 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1818 if (startYUnits
!= yUnits
)
1820 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1826 #if !wxRICHTEXT_USE_OWN_CARET
1834 /// Is the given position visible on the screen?
1835 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1837 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1843 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1846 GetViewStart(& startX
, & startY
);
1848 startY
= startY
* ppuY
;
1850 wxRect rect
= line
->GetRect();
1851 wxSize clientSize
= GetClientSize();
1852 clientSize
.y
-= GetBuffer().GetBottomMargin();
1854 return (rect
.GetTop() >= (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1857 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1859 m_caretPosition
= position
;
1860 m_caretAtLineStart
= showAtLineStart
;
1863 /// Move caret one visual step forward: this may mean setting a flag
1864 /// and keeping the same position if we're going from the end of one line
1865 /// to the start of the next, which may be the exact same caret position.
1866 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1868 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1870 // Only do the check if we're not at the end of the paragraph (where things work OK
1872 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1874 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1878 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1880 // We're at the end of a line. See whether we need to
1881 // stay at the same actual caret position but change visual
1882 // position, or not.
1883 if (oldPosition
== lineRange
.GetEnd())
1885 if (m_caretAtLineStart
)
1887 // We're already at the start of the line, so actually move on now.
1888 m_caretPosition
= oldPosition
+ 1;
1889 m_caretAtLineStart
= false;
1893 // We're showing at the end of the line, so keep to
1894 // the same position but indicate that we're to show
1895 // at the start of the next line.
1896 m_caretPosition
= oldPosition
;
1897 m_caretAtLineStart
= true;
1899 SetDefaultStyleToCursorStyle();
1905 SetDefaultStyleToCursorStyle();
1908 /// Move caret one visual step backward: this may mean setting a flag
1909 /// and keeping the same position if we're going from the end of one line
1910 /// to the start of the next, which may be the exact same caret position.
1911 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1913 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1915 // Only do the check if we're not at the start of the paragraph (where things work OK
1917 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1919 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1923 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1925 // We're at the start of a line. See whether we need to
1926 // stay at the same actual caret position but change visual
1927 // position, or not.
1928 if (oldPosition
== lineRange
.GetStart())
1930 m_caretPosition
= oldPosition
-1;
1931 m_caretAtLineStart
= true;
1934 else if (oldPosition
== lineRange
.GetEnd())
1936 if (m_caretAtLineStart
)
1938 // We're at the start of the line, so keep the same caret position
1939 // but clear the start-of-line flag.
1940 m_caretPosition
= oldPosition
;
1941 m_caretAtLineStart
= false;
1945 // We're showing at the end of the line, so go back
1946 // to the previous character position.
1947 m_caretPosition
= oldPosition
- 1;
1949 SetDefaultStyleToCursorStyle();
1955 SetDefaultStyleToCursorStyle();
1959 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1961 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1963 if (m_caretPosition
+ noPositions
< endPos
)
1965 long oldPos
= m_caretPosition
;
1966 long newPos
= m_caretPosition
+ noPositions
;
1968 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1972 // Determine by looking at oldPos and m_caretPosition whether
1973 // we moved from the end of a line to the start of the next line, in which case
1974 // we want to adjust the caret position such that it is positioned at the
1975 // start of the next line, rather than jumping past the first character of the
1977 if (noPositions
== 1 && !extendSel
)
1978 MoveCaretForward(oldPos
);
1980 SetCaretPosition(newPos
);
1983 SetDefaultStyleToCursorStyle();
1992 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1996 if (m_caretPosition
> startPos
- noPositions
+ 1)
1998 long oldPos
= m_caretPosition
;
1999 long newPos
= m_caretPosition
- noPositions
;
2000 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2004 if (noPositions
== 1 && !extendSel
)
2005 MoveCaretBack(oldPos
);
2007 SetCaretPosition(newPos
);
2010 SetDefaultStyleToCursorStyle();
2018 // Find the caret position for the combination of hit-test flags and character position.
2019 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
2020 // since this is ambiguous (same position used for end of line and start of next).
2021 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
2022 bool& caretLineStart
)
2024 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
2025 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
2026 // so we view the caret at the start of the line.
2027 caretLineStart
= false;
2028 long caretPosition
= position
;
2030 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
2032 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
2033 wxRichTextRange lineRange
;
2035 lineRange
= thisLine
->GetAbsoluteRange();
2037 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
2040 caretLineStart
= true;
2044 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
2045 if (para
&& para
->GetRange().GetStart() == position
)
2049 return caretPosition
;
2053 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
2055 return MoveDown(- noLines
, flags
);
2059 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
2064 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
2065 wxPoint pt
= GetCaret()->GetPosition();
2066 long newLine
= lineNumber
+ noLines
;
2067 bool notInThisObject
= false;
2069 if (lineNumber
!= -1)
2073 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
2074 if (newLine
> lastLine
)
2075 notInThisObject
= true;
2080 notInThisObject
= true;
2084 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
2085 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
;
2087 if (notInThisObject
)
2089 // If we know we're navigating out of the current object,
2090 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2091 container
= & GetBuffer();
2092 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
2094 if (noLines
> 0) // going down
2096 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
2100 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
2105 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
2107 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
2113 wxClientDC
dc(this);
2115 dc
.SetFont(GetFont());
2117 wxRichTextObject
* hitObj
= NULL
;
2118 wxRichTextObject
* contextObj
= NULL
;
2119 wxRichTextDrawingContext
context(& GetBuffer());
2120 int hitTest
= container
->HitTest(dc
, context
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
2123 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
2124 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
2127 if (notInThisObject
)
2129 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2130 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
2132 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
2134 container
= actualContainer
;
2138 bool caretLineStart
= true;
2139 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
2140 long newSelEnd
= caretPosition
;
2143 if (notInThisObject
)
2146 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
2151 SetCaretPosition(caretPosition
, caretLineStart
);
2153 SetDefaultStyleToCursorStyle();
2161 /// Move to the end of the paragraph
2162 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
2164 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2167 long newPos
= para
->GetRange().GetEnd() - 1;
2168 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2172 SetCaretPosition(newPos
);
2174 SetDefaultStyleToCursorStyle();
2182 /// Move to the start of the paragraph
2183 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
2185 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2188 long newPos
= para
->GetRange().GetStart() - 1;
2189 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2193 SetCaretPosition(newPos
);
2195 SetDefaultStyleToCursorStyle();
2203 /// Move to the end of the line
2204 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
2206 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2210 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2211 long newPos
= lineRange
.GetEnd();
2212 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2216 SetCaretPosition(newPos
);
2218 SetDefaultStyleToCursorStyle();
2226 /// Move to the start of the line
2227 bool wxRichTextCtrl::MoveToLineStart(int flags
)
2229 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2232 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2233 long newPos
= lineRange
.GetStart()-1;
2235 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2239 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
2241 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2243 SetDefaultStyleToCursorStyle();
2251 /// Move to the start of the buffer
2252 bool wxRichTextCtrl::MoveHome(int flags
)
2254 if (m_caretPosition
!= -1)
2256 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2260 SetCaretPosition(-1);
2262 SetDefaultStyleToCursorStyle();
2270 /// Move to the end of the buffer
2271 bool wxRichTextCtrl::MoveEnd(int flags
)
2273 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2275 if (m_caretPosition
!= endPos
)
2277 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2281 SetCaretPosition(endPos
);
2283 SetDefaultStyleToCursorStyle();
2291 /// Move noPages pages up
2292 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2294 return PageDown(- noPages
, flags
);
2297 /// Move noPages pages down
2298 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2300 // Calculate which line occurs noPages * screen height further down.
2301 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2304 wxSize clientSize
= GetClientSize();
2305 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2307 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2310 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2311 long pos
= lineRange
.GetStart()-1;
2312 if (pos
!= m_caretPosition
)
2314 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2316 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2320 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2322 SetDefaultStyleToCursorStyle();
2332 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2334 return str
== wxT(" ") || str
== wxT("\t");
2337 // Finds the caret position for the next word
2338 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2340 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2344 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2346 // First skip current text to space
2347 while (i
< endPos
&& i
> -1)
2349 // i is in character, not caret positions
2350 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2351 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2352 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2356 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2363 while (i
< endPos
&& i
> -1)
2365 // i is in character, not caret positions
2366 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2367 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2368 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2369 return wxMax(-1, i
);
2371 if (text
.empty()) // End of paragraph, or maybe an image
2372 return wxMax(-1, i
- 1);
2373 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2377 // Convert to caret position
2378 return wxMax(-1, i
- 1);
2387 long i
= m_caretPosition
;
2389 // First skip white space
2390 while (i
< endPos
&& i
> -1)
2392 // i is in character, not caret positions
2393 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2394 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2396 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2398 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2403 // Next skip current text to space
2404 while (i
< endPos
&& i
> -1)
2406 // i is in character, not caret positions
2407 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2408 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2409 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2412 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2425 /// Move n words left
2426 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2428 long pos
= FindNextWordPosition(-1);
2429 if (pos
!= m_caretPosition
)
2431 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2433 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2437 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2439 SetDefaultStyleToCursorStyle();
2447 /// Move n words right
2448 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2450 long pos
= FindNextWordPosition(1);
2451 if (pos
!= m_caretPosition
)
2453 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2455 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2459 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2461 SetDefaultStyleToCursorStyle();
2470 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2472 // Only do sizing optimization for large buffers
2473 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2475 m_fullLayoutRequired
= true;
2476 m_fullLayoutTime
= wxGetLocalTimeMillis();
2477 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2478 LayoutContent(true /* onlyVisibleRect */);
2481 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2483 #if wxRICHTEXT_BUFFERED_PAINTING
2490 // Force any pending layout due to large buffer
2491 void wxRichTextCtrl::ForceDelayedLayout()
2493 if (m_fullLayoutRequired
)
2495 m_fullLayoutRequired
= false;
2496 m_fullLayoutTime
= 0;
2497 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2498 ShowPosition(m_fullLayoutSavedPosition
);
2504 /// Idle-time processing
2505 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2507 #if wxRICHTEXT_USE_OWN_CARET
2508 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2510 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2516 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2518 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2520 m_fullLayoutRequired
= false;
2521 m_fullLayoutTime
= 0;
2522 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2523 ShowPosition(m_fullLayoutSavedPosition
);
2527 if (m_caretPositionForDefaultStyle
!= -2)
2529 // If the caret position has changed, no longer reflect the default style
2531 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2532 m_caretPositionForDefaultStyle
= -2;
2539 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2541 #if wxRICHTEXT_USE_OWN_CARET
2542 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2545 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2552 /// Set up scrollbars, e.g. after a resize
2553 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2558 if (GetBuffer().IsEmpty())
2560 SetScrollbars(0, 0, 0, 0, 0, 0);
2564 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2565 // of pixels. See e.g. wxVScrolledWindow for ideas.
2566 int pixelsPerUnit
= 5;
2567 wxSize clientSize
= GetClientSize();
2569 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2571 // Round up so we have at least maxHeight pixels
2572 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2574 int startX
= 0, startY
= 0;
2576 GetViewStart(& startX
, & startY
);
2578 int maxPositionX
= 0;
2579 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2581 int newStartX
= wxMin(maxPositionX
, startX
);
2582 int newStartY
= wxMin(maxPositionY
, startY
);
2584 int oldPPUX
, oldPPUY
;
2585 int oldStartX
, oldStartY
;
2586 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2587 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2588 GetViewStart(& oldStartX
, & oldStartY
);
2589 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2591 oldVirtualSizeY
/= oldPPUY
;
2593 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2596 // Don't set scrollbars if there were none before, and there will be none now.
2597 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2600 // Move to previous scroll position if
2602 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2605 /// Paint the background
2606 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2608 wxColour backgroundColour
= GetBackgroundColour();
2609 if (!backgroundColour
.IsOk())
2610 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2612 // Clear the background
2613 dc
.SetBrush(wxBrush(backgroundColour
));
2614 dc
.SetPen(*wxTRANSPARENT_PEN
);
2615 wxRect
windowRect(GetClientSize());
2616 windowRect
.x
-= 2; windowRect
.y
-= 2;
2617 windowRect
.width
+= 4; windowRect
.height
+= 4;
2619 // We need to shift the rectangle to take into account
2620 // scrolling. Converting device to logical coordinates.
2621 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2622 dc
.DrawRectangle(windowRect
);
2625 #if wxRICHTEXT_BUFFERED_PAINTING
2626 /// Recreate buffer bitmap if necessary
2627 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2630 if (sz
== wxDefaultSize
)
2631 sz
= GetClientSize();
2633 if (sz
.x
< 1 || sz
.y
< 1)
2636 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2637 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2638 return m_bufferBitmap
.IsOk();
2642 // ----------------------------------------------------------------------------
2643 // file IO functions
2644 // ----------------------------------------------------------------------------
2646 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2648 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2650 m_filename
= filename
;
2653 SetInsertionPoint(0);
2656 SetupScrollbars(true);
2658 wxTextCtrl::SendTextUpdatedEvent(this);
2664 wxLogError(_("File couldn't be loaded."));
2670 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2672 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2674 m_filename
= filename
;
2681 wxLogError(_("The text couldn't be saved."));
2686 // ----------------------------------------------------------------------------
2687 // wxRichTextCtrl specific functionality
2688 // ----------------------------------------------------------------------------
2690 /// Add a new paragraph of text to the end of the buffer
2691 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2693 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2694 GetBuffer().Invalidate();
2700 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2702 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2703 GetBuffer().Invalidate();
2708 // ----------------------------------------------------------------------------
2709 // selection and ranges
2710 // ----------------------------------------------------------------------------
2712 void wxRichTextCtrl::SelectAll()
2714 SetSelection(-1, -1);
2718 void wxRichTextCtrl::SelectNone()
2720 if (m_selection
.IsValid())
2722 wxRichTextSelection oldSelection
= m_selection
;
2724 m_selection
.Reset();
2726 RefreshForSelectionChange(oldSelection
, m_selection
);
2728 m_selectionAnchor
= -2;
2729 m_selectionAnchorObject
= NULL
;
2730 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2733 static bool wxIsWordDelimiter(const wxString
& text
)
2735 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2738 /// Select the word at the given character position
2739 bool wxRichTextCtrl::SelectWord(long position
)
2741 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2744 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2748 if (position
== para
->GetRange().GetEnd())
2751 long positionStart
= position
;
2752 long positionEnd
= position
;
2754 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2756 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2757 if (wxIsWordDelimiter(text
))
2763 if (positionStart
< para
->GetRange().GetStart())
2764 positionStart
= para
->GetRange().GetStart();
2766 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2768 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2769 if (wxIsWordDelimiter(text
))
2775 if (positionEnd
>= para
->GetRange().GetEnd())
2776 positionEnd
= para
->GetRange().GetEnd();
2778 if (positionEnd
< positionStart
)
2781 SetSelection(positionStart
, positionEnd
+1);
2783 if (positionStart
>= 0)
2785 MoveCaret(positionStart
-1, true);
2786 SetDefaultStyleToCursorStyle();
2792 wxString
wxRichTextCtrl::GetStringSelection() const
2795 GetSelection(&from
, &to
);
2797 return GetRange(from
, to
);
2800 // ----------------------------------------------------------------------------
2802 // ----------------------------------------------------------------------------
2804 wxTextCtrlHitTestResult
2805 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2807 // implement in terms of the other overload as the native ports typically
2808 // can get the position and not (x, y) pair directly (although wxUniv
2809 // directly gets x and y -- and so overrides this method as well)
2811 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2813 if ( rc
!= wxTE_HT_UNKNOWN
)
2815 PositionToXY(pos
, x
, y
);
2821 wxTextCtrlHitTestResult
2822 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2825 wxClientDC
dc((wxRichTextCtrl
*) this);
2826 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2828 // Buffer uses logical position (relative to start of buffer)
2830 wxPoint pt2
= GetLogicalPoint(pt
);
2832 wxRichTextObject
* hitObj
= NULL
;
2833 wxRichTextObject
* contextObj
= NULL
;
2834 wxRichTextDrawingContext
context((wxRichTextBuffer
*) & GetBuffer());
2835 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, context
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2837 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2838 return wxTE_HT_BEFORE
;
2839 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2840 return wxTE_HT_BEYOND
;
2841 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2842 return wxTE_HT_ON_TEXT
;
2844 return wxTE_HT_UNKNOWN
;
2847 wxRichTextParagraphLayoutBox
*
2848 wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt
, long& position
, int& hit
, wxRichTextObject
* hitObj
, int flags
/* = 0*/)
2850 wxClientDC
dc(this);
2852 dc
.SetFont(GetFont());
2854 wxPoint logicalPt
= GetLogicalPoint(pt
);
2856 wxRichTextObject
* contextObj
= NULL
;
2857 wxRichTextDrawingContext
context(& GetBuffer());
2858 hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, &hitObj
, &contextObj
, flags
);
2859 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2865 // ----------------------------------------------------------------------------
2866 // set/get the controls text
2867 // ----------------------------------------------------------------------------
2869 wxString
wxRichTextCtrl::DoGetValue() const
2871 return GetBuffer().GetText();
2874 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2876 // Public API for range is different from internals
2877 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2880 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2882 // Don't call Clear here, since it always sends a text updated event
2883 m_buffer
.ResetAndClearCommands();
2884 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2885 m_caretPosition
= -1;
2886 m_caretPositionForDefaultStyle
= -2;
2887 m_caretAtLineStart
= false;
2888 m_selection
.Reset();
2889 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2899 if (!value
.IsEmpty())
2901 // Remove empty paragraph
2902 GetBuffer().Clear();
2903 DoWriteText(value
, flags
);
2905 // for compatibility, don't move the cursor when doing SetValue()
2906 SetInsertionPoint(0);
2910 // still send an event for consistency
2911 if (flags
& SetValue_SendEvent
)
2912 wxTextCtrl::SendTextUpdatedEvent(this);
2917 void wxRichTextCtrl::WriteText(const wxString
& value
)
2922 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2924 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2926 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2928 if ( flags
& SetValue_SendEvent
)
2929 wxTextCtrl::SendTextUpdatedEvent(this);
2932 void wxRichTextCtrl::AppendText(const wxString
& text
)
2934 SetInsertionPointEnd();
2939 /// Write an image at the current insertion point
2940 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2942 wxRichTextImageBlock imageBlock
;
2944 wxImage image2
= image
;
2945 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2946 return WriteImage(imageBlock
, textAttr
);
2951 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2953 wxRichTextImageBlock imageBlock
;
2956 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2957 return WriteImage(imageBlock
, textAttr
);
2962 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2964 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition
+1, imageBlock
, this, 0, textAttr
);
2967 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2971 wxRichTextImageBlock imageBlock
;
2973 wxImage image
= bitmap
.ConvertToImage();
2974 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2975 return WriteImage(imageBlock
, textAttr
);
2981 // Write a text box at the current insertion point.
2982 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2984 wxRichTextBox
* textBox
= new wxRichTextBox
;
2985 textBox
->SetAttributes(textAttr
);
2986 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2987 textBox
->AddParagraph(wxEmptyString
);
2988 textBox
->SetParent(NULL
);
2990 // The object returned is the one actually inserted into the buffer,
2991 // while the original one is deleted.
2992 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, textBox
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2993 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
2997 // Write a table at the current insertion point, returning the table.
2998 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
3000 wxASSERT(rows
> 0 && cols
> 0);
3002 if (rows
<= 0 || cols
<= 0)
3005 wxRichTextTable
* table
= new wxRichTextTable
;
3006 table
->SetAttributes(tableAttr
);
3007 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
3009 table
->CreateTable(rows
, cols
);
3011 table
->SetParent(NULL
);
3014 for (j
= 0; j
< rows
; j
++)
3016 for (i
= 0; i
< cols
; i
++)
3018 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
3022 // The object returned is the one actually inserted into the buffer,
3023 // while the original one is deleted.
3024 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, table
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3025 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
3030 /// Insert a newline (actually paragraph) at the current insertion point.
3031 bool wxRichTextCtrl::Newline()
3033 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
3036 /// Insert a line break at the current insertion point.
3037 bool wxRichTextCtrl::LineBreak()
3040 text
= wxRichTextLineBreakChar
;
3041 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, text
, this);
3044 // ----------------------------------------------------------------------------
3045 // Clipboard operations
3046 // ----------------------------------------------------------------------------
3048 void wxRichTextCtrl::Copy()
3052 wxRichTextRange range
= GetInternalSelectionRange();
3053 GetBuffer().CopyToClipboard(range
);
3057 void wxRichTextCtrl::Cut()
3061 wxRichTextRange range
= GetInternalSelectionRange();
3062 GetBuffer().CopyToClipboard(range
);
3064 DeleteSelectedContent();
3070 void wxRichTextCtrl::Paste()
3074 BeginBatchUndo(_("Paste"));
3076 long newPos
= m_caretPosition
;
3077 DeleteSelectedContent(& newPos
);
3079 GetBuffer().PasteFromClipboard(newPos
);
3085 void wxRichTextCtrl::DeleteSelection()
3087 if (CanDeleteSelection())
3089 DeleteSelectedContent();
3093 bool wxRichTextCtrl::HasSelection() const
3095 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
3098 bool wxRichTextCtrl::HasUnfocusedSelection() const
3100 return m_selection
.IsValid();
3103 bool wxRichTextCtrl::CanCopy() const
3105 // Can copy if there's a selection
3106 return HasSelection();
3109 bool wxRichTextCtrl::CanCut() const
3111 return CanDeleteSelection();
3114 bool wxRichTextCtrl::CanPaste() const
3116 if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition
+1))
3119 return GetBuffer().CanPasteFromClipboard();
3122 bool wxRichTextCtrl::CanDeleteSelection() const
3124 return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange());
3128 // ----------------------------------------------------------------------------
3130 // ----------------------------------------------------------------------------
3132 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
3134 if (m_contextMenu
&& m_contextMenu
!= menu
)
3135 delete m_contextMenu
;
3136 m_contextMenu
= menu
;
3139 void wxRichTextCtrl::SetEditable(bool editable
)
3141 m_editable
= editable
;
3144 void wxRichTextCtrl::SetInsertionPoint(long pos
)
3148 m_caretPosition
= pos
- 1;
3152 SetDefaultStyleToCursorStyle();
3155 void wxRichTextCtrl::SetInsertionPointEnd()
3157 long pos
= GetLastPosition();
3158 SetInsertionPoint(pos
);
3161 long wxRichTextCtrl::GetInsertionPoint() const
3163 return m_caretPosition
+1;
3166 wxTextPos
wxRichTextCtrl::GetLastPosition() const
3168 return GetFocusObject()->GetOwnRange().GetEnd();
3171 // If the return values from and to are the same, there is no
3173 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
3175 if (m_selection
.IsValid())
3177 *from
= m_selection
.GetRange().GetStart();
3178 *to
= m_selection
.GetRange().GetEnd();
3188 bool wxRichTextCtrl::IsEditable() const
3193 // ----------------------------------------------------------------------------
3195 // ----------------------------------------------------------------------------
3197 void wxRichTextCtrl::SetSelection(long from
, long to
)
3199 // if from and to are both -1, it means (in wxWidgets) that all text should
3201 if ( (from
== -1) && (to
== -1) )
3204 to
= GetLastPosition()+1;
3213 wxRichTextSelection oldSelection
= m_selection
;
3215 m_selectionAnchor
= from
-1;
3216 m_selectionAnchorObject
= NULL
;
3217 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
3219 m_caretPosition
= wxMax(-1, to
-1);
3221 RefreshForSelectionChange(oldSelection
, m_selection
);
3226 // ----------------------------------------------------------------------------
3228 // ----------------------------------------------------------------------------
3230 void wxRichTextCtrl::Replace(long from
, long to
,
3231 const wxString
& value
)
3233 BeginBatchUndo(_("Replace"));
3235 SetSelection(from
, to
);
3237 wxRichTextAttr attr
= GetDefaultStyle();
3239 DeleteSelectedContent();
3241 SetDefaultStyle(attr
);
3243 DoWriteText(value
, SetValue_SelectionOnly
);
3248 void wxRichTextCtrl::Remove(long from
, long to
)
3252 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
3259 bool wxRichTextCtrl::IsModified() const
3261 return m_buffer
.IsModified();
3264 void wxRichTextCtrl::MarkDirty()
3266 m_buffer
.Modify(true);
3269 void wxRichTextCtrl::DiscardEdits()
3271 m_caretPositionForDefaultStyle
= -2;
3272 m_buffer
.Modify(false);
3273 m_buffer
.GetCommandProcessor()->ClearCommands();
3276 int wxRichTextCtrl::GetNumberOfLines() const
3278 return GetFocusObject()->GetParagraphCount();
3281 // ----------------------------------------------------------------------------
3282 // Positions <-> coords
3283 // ----------------------------------------------------------------------------
3285 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3287 return GetFocusObject()->XYToPosition(x
, y
);
3290 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3292 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3295 // ----------------------------------------------------------------------------
3297 // ----------------------------------------------------------------------------
3299 void wxRichTextCtrl::ShowPosition(long pos
)
3301 if (!IsPositionVisible(pos
))
3302 ScrollIntoView(pos
-1, WXK_DOWN
);
3305 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3307 return GetFocusObject()->GetParagraphLength(lineNo
);
3310 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3312 return GetFocusObject()->GetParagraphText(lineNo
);
3315 // ----------------------------------------------------------------------------
3317 // ----------------------------------------------------------------------------
3319 void wxRichTextCtrl::Undo()
3323 GetCommandProcessor()->Undo();
3327 void wxRichTextCtrl::Redo()
3331 GetCommandProcessor()->Redo();
3335 bool wxRichTextCtrl::CanUndo() const
3337 return GetCommandProcessor()->CanUndo() && IsEditable();
3340 bool wxRichTextCtrl::CanRedo() const
3342 return GetCommandProcessor()->CanRedo() && IsEditable();
3345 // ----------------------------------------------------------------------------
3346 // implementation details
3347 // ----------------------------------------------------------------------------
3349 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3351 SetValue(event
.GetString());
3352 GetEventHandler()->ProcessEvent(event
);
3355 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3357 // By default, load the first file into the text window.
3358 if (event
.GetNumberOfFiles() > 0)
3360 LoadFile(event
.GetFiles()[0]);
3364 wxSize
wxRichTextCtrl::DoGetBestSize() const
3366 return wxSize(10, 10);
3369 // ----------------------------------------------------------------------------
3370 // standard handlers for standard edit menu events
3371 // ----------------------------------------------------------------------------
3373 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3378 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3383 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3388 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3393 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3398 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3403 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3405 event
.Enable( CanCut() );
3408 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3410 event
.Enable( CanCopy() );
3413 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3415 event
.Enable( CanDeleteSelection() );
3418 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3420 event
.Enable( CanPaste() );
3423 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3425 event
.Enable( CanUndo() );
3426 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3429 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3431 event
.Enable( CanRedo() );
3432 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3435 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3437 if (GetLastPosition() > 0)
3441 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3443 event
.Enable(GetLastPosition() > 0);
3446 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3448 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3449 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3451 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3452 if (obj
&& CanEditProperties(obj
))
3453 EditProperties(obj
, this);
3455 m_contextMenuPropertiesInfo
.Clear();
3459 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3461 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3462 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3465 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3467 if (event
.GetEventObject() != this)
3473 ShowContextMenu(m_contextMenu
, event
.GetPosition());
3476 // Prepares the context menu, adding appropriate property-editing commands.
3477 // Returns the number of property commands added.
3478 int wxRichTextCtrl::PrepareContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3480 wxClientDC
dc(this);
3482 dc
.SetFont(GetFont());
3484 m_contextMenuPropertiesInfo
.Clear();
3487 wxRichTextObject
* hitObj
= NULL
;
3488 wxRichTextObject
* contextObj
= NULL
;
3489 if (pt
!= wxDefaultPosition
)
3491 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3492 wxRichTextDrawingContext
context(& GetBuffer());
3493 int hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
);
3495 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3497 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3498 if (hitObj
&& actualContainer
)
3500 if (actualContainer
->AcceptsFocus())
3502 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3503 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3506 if (addPropertyCommands
)
3507 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3511 if (addPropertyCommands
)
3512 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3517 if (addPropertyCommands
)
3518 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3523 // Invoked from the keyboard, so don't set the caret position and don't use the event
3525 hitObj
= GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition
+1);
3527 contextObj
= hitObj
->GetParentContainer();
3529 contextObj
= GetFocusObject();
3531 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3532 if (hitObj
&& actualContainer
)
3534 if (addPropertyCommands
)
3535 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3539 if (addPropertyCommands
)
3540 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3546 if (addPropertyCommands
)
3547 m_contextMenuPropertiesInfo
.AddMenuItems(menu
);
3548 return m_contextMenuPropertiesInfo
.GetCount();
3554 // Shows the context menu, adding appropriate property-editing commands
3555 bool wxRichTextCtrl::ShowContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3559 PrepareContextMenu(menu
, pt
, addPropertyCommands
);
3567 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3569 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3572 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3574 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3577 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3579 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3582 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3584 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3587 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
)
3589 GetFocusObject()->SetStyle(obj
, textAttr
);
3592 // extended style setting operation with flags including:
3593 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3594 // see richtextbuffer.h for more details.
3596 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3598 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3601 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3603 return GetBuffer().SetDefaultStyle(style
);
3606 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3608 wxRichTextAttr
attr1(style
);
3609 attr1
.GetTextBoxAttr().Reset();
3610 return GetBuffer().SetDefaultStyle(attr1
);
3613 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3615 return GetBuffer().GetDefaultStyle();
3618 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3620 wxRichTextAttr attr
;
3621 if (GetFocusObject()->GetStyle(position
, attr
))
3630 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3632 return GetFocusObject()->GetStyle(position
, style
);
3635 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3637 wxRichTextAttr attr
;
3638 if (container
->GetStyle(position
, attr
))
3647 // get the common set of styles for the range
3648 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3650 wxRichTextAttr attr
;
3651 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3660 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3662 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3665 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3667 return container
->GetStyleForRange(range
.ToInternal(), style
);
3670 /// Get the content (uncombined) attributes for this position.
3671 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3673 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3676 /// Get the content (uncombined) attributes for this position.
3677 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3679 return container
->GetUncombinedStyle(position
, style
);
3682 bool wxRichTextCtrl::SetProperties(const wxRichTextRange
& range
, const wxRichTextProperties
& properties
, int flags
)
3684 return GetFocusObject()->SetProperties(range
.ToInternal(), properties
, flags
);
3687 /// Set font, and also the buffer attributes
3688 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3690 wxControl::SetFont(font
);
3692 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3694 GetBuffer().SetBasicStyle(attr
);
3696 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3702 /// Transform logical to physical
3703 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3706 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3711 /// Transform physical to logical
3712 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3715 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3720 /// Position the caret
3721 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3726 //wxLogDebug(wxT("PositionCaret"));
3729 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3731 wxPoint newPt
= caretRect
.GetPosition();
3732 wxSize newSz
= caretRect
.GetSize();
3733 wxPoint pt
= GetPhysicalPoint(newPt
);
3734 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3737 if (GetCaret()->GetSize() != newSz
)
3738 GetCaret()->SetSize(newSz
);
3740 // Adjust size so the caret size and position doesn't appear in the margins
3741 if (((pt
.y
+ newSz
.y
) <= GetBuffer().GetTopMargin()) || (pt
.y
>= (GetClientSize().y
- GetBuffer().GetBottomMargin())))
3746 else if (pt
.y
< GetBuffer().GetTopMargin() && (pt
.y
+ newSz
.y
) > GetBuffer().GetTopMargin())
3748 newSz
.y
-= (GetBuffer().GetTopMargin() - pt
.y
);
3751 pt
.y
= GetBuffer().GetTopMargin();
3752 GetCaret()->SetSize(newSz
);
3755 else if (pt
.y
< (GetClientSize().y
- GetBuffer().GetBottomMargin()) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- GetBuffer().GetBottomMargin()))
3757 newSz
.y
= GetClientSize().y
- GetBuffer().GetBottomMargin() - pt
.y
;
3758 GetCaret()->SetSize(newSz
);
3761 GetCaret()->Move(pt
);
3767 /// Get the caret height and position for the given character position
3768 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3770 wxClientDC
dc(this);
3771 dc
.SetFont(GetFont());
3779 container
= GetFocusObject();
3781 wxRichTextDrawingContext
context(& GetBuffer());
3782 if (container
->FindPosition(dc
, context
, position
, pt
, & height
, m_caretAtLineStart
))
3784 // Caret height can't be zero
3786 height
= dc
.GetCharHeight();
3788 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3795 /// Gets the line for the visible caret position. If the caret is
3796 /// shown at the very end of the line, it means the next character is actually
3797 /// on the following line. So let's get the line we're expecting to find
3798 /// if this is the case.
3799 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3801 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3802 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3805 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3806 if (caretPosition
== lineRange
.GetStart()-1 &&
3807 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3809 if (!m_caretAtLineStart
)
3810 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3817 /// Move the caret to the given character position
3818 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3820 if (GetBuffer().IsDirty())
3824 container
= GetFocusObject();
3826 if (pos
<= container
->GetOwnRange().GetEnd())
3828 SetCaretPosition(pos
, showAtLineStart
);
3830 PositionCaret(container
);
3838 /// Layout the buffer: which we must do before certain operations, such as
3839 /// setting the caret position.
3840 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3842 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3844 wxRect
availableSpace(GetClientSize());
3845 if (availableSpace
.width
== 0)
3846 availableSpace
.width
= 10;
3847 if (availableSpace
.height
== 0)
3848 availableSpace
.height
= 10;
3850 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3851 if (onlyVisibleRect
)
3853 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3854 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3857 wxClientDC
dc(this);
3858 dc
.SetFont(GetFont());
3862 wxRichTextDrawingContext
context(& GetBuffer());
3863 GetBuffer().Defragment();
3864 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3865 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, flags
);
3866 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3875 /// Is all of the selection, or the current caret position, bold?
3876 bool wxRichTextCtrl::IsSelectionBold()
3880 wxRichTextAttr attr
;
3881 wxRichTextRange range
= GetSelectionRange();
3882 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3883 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3885 return HasCharacterAttributes(range
, attr
);
3889 // If no selection, then we need to combine current style with default style
3890 // to see what the effect would be if we started typing.
3891 wxRichTextAttr attr
;
3892 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3894 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3895 if (GetStyle(pos
, attr
))
3897 if (IsDefaultStyleShowing())
3898 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3899 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3905 /// Is all of the selection, or the current caret position, italics?
3906 bool wxRichTextCtrl::IsSelectionItalics()
3910 wxRichTextRange range
= GetSelectionRange();
3911 wxRichTextAttr attr
;
3912 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3913 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3915 return HasCharacterAttributes(range
, attr
);
3919 // If no selection, then we need to combine current style with default style
3920 // to see what the effect would be if we started typing.
3921 wxRichTextAttr attr
;
3922 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3924 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3925 if (GetStyle(pos
, attr
))
3927 if (IsDefaultStyleShowing())
3928 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3929 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3935 /// Is all of the selection, or the current caret position, underlined?
3936 bool wxRichTextCtrl::IsSelectionUnderlined()
3940 wxRichTextRange range
= GetSelectionRange();
3941 wxRichTextAttr attr
;
3942 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3943 attr
.SetFontUnderlined(true);
3945 return HasCharacterAttributes(range
, attr
);
3949 // If no selection, then we need to combine current style with default style
3950 // to see what the effect would be if we started typing.
3951 wxRichTextAttr attr
;
3952 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3953 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3955 if (GetStyle(pos
, attr
))
3957 if (IsDefaultStyleShowing())
3958 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3959 return attr
.GetFontUnderlined();
3965 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3966 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
3968 wxRichTextAttr attr
;
3969 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3970 attr
.SetTextEffectFlags(flag
);
3971 attr
.SetTextEffects(flag
);
3975 return HasCharacterAttributes(GetSelectionRange(), attr
);
3979 // If no selection, then we need to combine current style with default style
3980 // to see what the effect would be if we started typing.
3981 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3982 if (GetStyle(pos
, attr
))
3984 if (IsDefaultStyleShowing())
3985 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3986 return (attr
.GetTextEffectFlags() & flag
) != 0;
3992 /// Apply bold to the selection
3993 bool wxRichTextCtrl::ApplyBoldToSelection()
3995 wxRichTextAttr attr
;
3996 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3997 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
4000 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4003 wxRichTextAttr current
= GetDefaultStyleEx();
4004 current
.Apply(attr
);
4005 SetAndShowDefaultStyle(current
);
4010 /// Apply italic to the selection
4011 bool wxRichTextCtrl::ApplyItalicToSelection()
4013 wxRichTextAttr attr
;
4014 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
4015 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
4018 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4021 wxRichTextAttr current
= GetDefaultStyleEx();
4022 current
.Apply(attr
);
4023 SetAndShowDefaultStyle(current
);
4028 /// Apply underline to the selection
4029 bool wxRichTextCtrl::ApplyUnderlineToSelection()
4031 wxRichTextAttr attr
;
4032 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
4033 attr
.SetFontUnderlined(!IsSelectionUnderlined());
4036 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4039 wxRichTextAttr current
= GetDefaultStyleEx();
4040 current
.Apply(attr
);
4041 SetAndShowDefaultStyle(current
);
4046 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
4047 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
4049 wxRichTextAttr attr
;
4050 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
4051 attr
.SetTextEffectFlags(flags
);
4052 if (!DoesSelectionHaveTextEffectFlag(flags
))
4053 attr
.SetTextEffects(flags
);
4055 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
4058 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
4061 wxRichTextAttr current
= GetDefaultStyleEx();
4062 current
.Apply(attr
);
4063 SetAndShowDefaultStyle(current
);
4068 /// Is all of the selection aligned according to the specified flag?
4069 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
4071 wxRichTextRange range
;
4073 range
= GetSelectionRange();
4075 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
4077 wxRichTextAttr attr
;
4078 attr
.SetAlignment(alignment
);
4080 return HasParagraphAttributes(range
, attr
);
4083 /// Apply alignment to the selection
4084 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
4086 wxRichTextAttr attr
;
4087 attr
.SetAlignment(alignment
);
4089 return SetStyle(GetSelectionRange(), attr
);
4092 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
4094 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
4099 /// Apply a named style to the selection
4100 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
4102 // Flags are defined within each definition, so only certain
4103 // attributes are applied.
4104 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
4106 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
4108 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
4110 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4112 wxRichTextRange range
;
4115 range
= GetSelectionRange();
4118 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4119 range
= wxRichTextRange(pos
, pos
+1);
4122 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
4125 bool isPara
= false;
4127 // Make sure the attr has the style name
4128 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
4131 attr
.SetParagraphStyleName(def
->GetName());
4133 // If applying a paragraph style, we only want the paragraph nodes to adopt these
4134 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
4135 // to change its style independently.
4136 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4138 else if (def
->IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition
)))
4139 attr
.SetCharacterStyleName(def
->GetName());
4140 else if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4141 attr
.GetTextBoxAttr().SetBoxStyleName(def
->GetName());
4143 if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4145 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4147 SetStyle(GetFocusObject(), attr
);
4153 else if (HasSelection())
4154 return SetStyleEx(GetSelectionRange(), attr
, flags
);
4157 wxRichTextAttr current
= GetDefaultStyleEx();
4158 wxRichTextAttr
defaultStyle(attr
);
4161 // Don't apply extra character styles since they are already implied
4162 // in the paragraph style
4163 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
4165 current
.Apply(defaultStyle
);
4166 SetAndShowDefaultStyle(current
);
4168 // If it's a paragraph style, we want to apply the style to the
4169 // current paragraph even if we didn't select any text.
4172 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4173 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
4176 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
4183 /// Apply the style sheet to the buffer, for example if the styles have changed.
4184 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4187 styleSheet
= GetBuffer().GetStyleSheet();
4191 if (GetBuffer().ApplyStyleSheet(styleSheet
))
4193 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4201 /// Sets the default style to the style under the cursor
4202 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4204 wxRichTextAttr attr
;
4205 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
4207 // If at the start of a paragraph, use the next position.
4208 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4210 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(pos
);
4211 if (obj
&& obj
->IsTopLevel())
4213 // Don't use the attributes of a top-level object, since they might apply
4214 // to content of the object, e.g. background colour.
4215 SetDefaultStyle(wxRichTextAttr());
4218 else if (GetUncombinedStyle(pos
, attr
))
4220 SetDefaultStyle(attr
);
4227 /// Returns the first visible position in the current view
4228 long wxRichTextCtrl::GetFirstVisiblePosition() const
4230 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
4232 return line
->GetAbsoluteRange().GetStart();
4237 /// Get the first visible point in the window
4238 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
4241 int startXUnits
, startYUnits
;
4243 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
4244 GetViewStart(& startXUnits
, & startYUnits
);
4246 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
4249 /// The adjusted caret position is the character position adjusted to take
4250 /// into account whether we're at the start of a paragraph, in which case
4251 /// style information should be taken from the next position, not current one.
4252 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
4254 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
4256 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
4261 /// Get/set the selection range in character positions. -1, -1 means no selection.
4262 /// The range is in API convention, i.e. a single character selection is denoted
4264 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
4266 wxRichTextRange range
= GetInternalSelectionRange();
4267 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
4268 range
.SetEnd(range
.GetEnd() + 1);
4272 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
4274 SetSelection(range
.GetStart(), range
.GetEnd());
4278 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4280 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4283 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4285 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4288 /// Clear list for given range
4289 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
4291 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
4294 /// Number/renumber any list elements in the given range
4295 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4297 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4300 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4302 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4305 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4306 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4308 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
4311 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4313 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
4316 /// Deletes the content in the given range
4317 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
4319 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
4322 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
4324 if (sm_availableFontNames
.GetCount() == 0)
4326 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
4327 sm_availableFontNames
.Sort();
4329 return sm_availableFontNames
;
4332 void wxRichTextCtrl::ClearAvailableFontNames()
4334 sm_availableFontNames
.Clear();
4337 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
4339 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4341 wxTextAttrEx basicStyle
= GetBasicStyle();
4342 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4343 SetBasicStyle(basicStyle
);
4344 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4349 // Refresh the area affected by a selection change
4350 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4352 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4353 // the selection contains whole containers rather than just text, so refresh everything
4354 // for now as it would be hard to compute the rectangle bounding all selections.
4355 // TODO: improve on this.
4356 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4357 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4363 wxRichTextRange oldRange
, newRange
;
4364 if (oldSelection
.IsValid())
4365 oldRange
= oldSelection
.GetRange();
4367 oldRange
= wxRICHTEXT_NO_SELECTION
;
4368 if (newSelection
.IsValid())
4369 newRange
= newSelection
.GetRange();
4371 newRange
= wxRICHTEXT_NO_SELECTION
;
4373 // Calculate the refresh rectangle - just the affected lines
4374 long firstPos
, lastPos
;
4375 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4377 firstPos
= newRange
.GetStart();
4378 lastPos
= newRange
.GetEnd();
4380 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4382 firstPos
= oldRange
.GetStart();
4383 lastPos
= oldRange
.GetEnd();
4385 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4391 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4392 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4395 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4396 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4398 if (firstLine
&& lastLine
)
4400 wxSize clientSize
= GetClientSize();
4401 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
4402 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
4405 pt1
.y
= wxMax(0, pt1
.y
);
4407 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4409 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4410 RefreshRect(rect
, false);
4418 // margins functions
4419 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4421 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4422 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4423 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4424 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4429 wxPoint
wxRichTextCtrl::DoGetMargins() const
4431 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4432 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4435 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4437 if (obj
&& !obj
->AcceptsFocus())
4440 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4441 bool changingContainer
= (m_focusObject
!= obj
);
4443 if (changingContainer
&& HasSelection())
4446 m_focusObject
= obj
;
4449 m_focusObject
= & m_buffer
;
4451 if (setCaretPosition
&& changingContainer
)
4453 m_selection
.Reset();
4454 m_selectionAnchor
= -2;
4455 m_selectionAnchorObject
= NULL
;
4456 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4460 m_caretAtLineStart
= false;
4461 MoveCaret(pos
, m_caretAtLineStart
);
4462 SetDefaultStyleToCursorStyle();
4464 wxRichTextEvent
cmdEvent(
4465 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4467 cmdEvent
.SetEventObject(this);
4468 cmdEvent
.SetPosition(m_caretPosition
+1);
4469 cmdEvent
.SetOldContainer(oldContainer
);
4470 cmdEvent
.SetContainer(m_focusObject
);
4472 GetEventHandler()->ProcessEvent(cmdEvent
);
4477 #if wxUSE_DRAG_AND_DROP
4478 void wxRichTextCtrl::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxDragResult def
, wxDataObject
* DataObj
)
4482 if ((def
!= wxDragCopy
) && (def
!= wxDragMove
))
4487 if (!GetSelection().IsValid())
4492 wxRichTextParagraphLayoutBox
* originContainer
= GetSelection().GetContainer();
4493 wxRichTextParagraphLayoutBox
* destContainer
= GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4495 wxRichTextBuffer
* richTextBuffer
= ((wxRichTextBufferDataObject
*)DataObj
)->GetRichTextBuffer();
4498 long position
= GetCaretPosition();
4499 wxRichTextRange selectionrange
= GetInternalSelectionRange();
4500 if (selectionrange
.Contains(position
) && (def
== wxDragMove
))
4502 // It doesn't make sense to move onto itself
4506 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4507 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4508 bool DeleteAfter
= (def
== wxDragMove
) && (position
> selectionrange
.GetEnd());
4509 if ((def
== wxDragMove
) && !DeleteAfter
)
4511 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4512 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4515 destContainer
->InsertParagraphsWithUndo(&GetBuffer(), position
+1, *richTextBuffer
, this, 0);
4516 ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
4518 delete richTextBuffer
;
4522 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4523 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4530 #endif // wxUSE_DRAG_AND_DROP
4533 #if wxUSE_DRAG_AND_DROP
4534 bool wxRichTextDropSource::GiveFeedback(wxDragResult
WXUNUSED(effect
))
4536 wxCHECK_MSG(m_rtc
, false, wxT("NULL m_rtc"));
4540 wxRichTextObject
* hitObj
= NULL
;
4541 wxRichTextParagraphLayoutBox
* container
= m_rtc
->FindContainerAtPoint(m_rtc
->ScreenToClient(wxGetMousePosition()), position
, hit
, hitObj
);
4543 if (!(hit
& wxRICHTEXT_HITTEST_NONE
) && container
&& container
->AcceptsFocus())
4545 m_rtc
->StoreFocusObject(container
);
4546 m_rtc
->SetCaretPositionAfterClick(container
, position
, hit
);
4549 return false; // so that the base-class sets a cursor
4551 #endif // wxUSE_DRAG_AND_DROP
4553 bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), const wxRichTextRange
& WXUNUSED(range
)) const
4558 bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox
& WXUNUSED(container
), long WXUNUSED(pos
)) const
4564 #if wxRICHTEXT_USE_OWN_CARET
4566 // ----------------------------------------------------------------------------
4567 // initialization and destruction
4568 // ----------------------------------------------------------------------------
4570 void wxRichTextCaret::Init()
4573 m_refreshEnabled
= true;
4577 m_richTextCtrl
= NULL
;
4578 m_needsUpdate
= false;
4582 wxRichTextCaret::~wxRichTextCaret()
4584 if (m_timer
.IsRunning())
4588 // ----------------------------------------------------------------------------
4589 // showing/hiding/moving the caret (base class interface)
4590 // ----------------------------------------------------------------------------
4592 void wxRichTextCaret::DoShow()
4596 if (!m_timer
.IsRunning())
4597 m_timer
.Start(GetBlinkTime());
4602 void wxRichTextCaret::DoHide()
4604 if (m_timer
.IsRunning())
4610 void wxRichTextCaret::DoMove()
4616 if (m_xOld
!= -1 && m_yOld
!= -1)
4618 if (m_richTextCtrl
&& m_refreshEnabled
)
4620 wxRect
rect(GetPosition(), GetSize());
4621 m_richTextCtrl
->RefreshRect(rect
, false);
4630 void wxRichTextCaret::DoSize()
4632 int countVisible
= m_countVisible
;
4633 if (countVisible
> 0)
4639 if (countVisible
> 0)
4641 m_countVisible
= countVisible
;
4646 // ----------------------------------------------------------------------------
4647 // handling the focus
4648 // ----------------------------------------------------------------------------
4650 void wxRichTextCaret::OnSetFocus()
4658 void wxRichTextCaret::OnKillFocus()
4663 // ----------------------------------------------------------------------------
4664 // drawing the caret
4665 // ----------------------------------------------------------------------------
4667 void wxRichTextCaret::Refresh()
4669 if (m_richTextCtrl
&& m_refreshEnabled
)
4671 wxRect
rect(GetPosition(), GetSize());
4672 m_richTextCtrl
->RefreshRect(rect
, false);
4676 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4678 dc
->SetPen( *wxBLACK_PEN
);
4680 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4681 dc
->SetPen(*wxBLACK_PEN
);
4683 wxPoint
pt(m_x
, m_y
);
4687 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4689 if (IsVisible() && m_flashOn
)
4690 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4693 void wxRichTextCaret::Notify()
4695 m_flashOn
= !m_flashOn
;
4699 void wxRichTextCaretTimer::Notify()
4704 // wxRICHTEXT_USE_OWN_CARET
4707 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4711 m_labels
.Add(label
);
4719 // Returns number of menu items were added.
4720 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4722 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4723 // If none of the standard properties identifiers are in the menu, add them if necessary.
4724 // If no items to add, just set the text to something generic
4725 if (GetCount() == 0)
4729 menu
->SetLabel(startCmd
, _("&Properties"));
4731 // Delete the others if necessary
4733 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4735 if (menu
->FindItem(i
))
4746 // Find the position of the first properties item
4747 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4749 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4750 if (item
&& item
->GetId() == startCmd
)
4759 int insertBefore
= pos
+1;
4760 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4762 if (menu
->FindItem(i
))
4764 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4768 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4769 menu
->Append(i
, m_labels
[i
- startCmd
]);
4771 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4776 // Delete any old items still left on the menu
4777 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4779 if (menu
->FindItem(i
))
4787 // No existing property identifiers were found, so append to the end of the menu.
4788 menu
->AppendSeparator();
4789 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4791 menu
->Append(i
, m_labels
[i
- startCmd
]);
4799 // Add appropriate menu items for the current container and clicked on object
4800 // (and container's parent, if appropriate).
4801 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl
* ctrl
, wxRichTextObject
* container
, wxRichTextObject
* obj
)
4804 if (obj
&& ctrl
->CanEditProperties(obj
))
4805 AddItem(ctrl
->GetPropertiesMenuLabel(obj
), obj
);
4807 if (container
&& container
!= obj
&& ctrl
->CanEditProperties(container
) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
)) == wxNOT_FOUND
)
4808 AddItem(ctrl
->GetPropertiesMenuLabel(container
), container
);
4810 if (container
&& container
->GetParent() && ctrl
->CanEditProperties(container
->GetParent()) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
->GetParent())) == wxNOT_FOUND
)
4811 AddItem(ctrl
->GetPropertiesMenuLabel(container
->GetParent()), container
->GetParent());