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 wxRichTextDrawingContext
context(& GetBuffer());
865 int hit2
= GetBuffer().HitTest(dc
, context
, logicalPt
, position2
, & hitObj2
, & contextObj2
, 0);
866 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
868 // See if we can find a common ancestor
869 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
871 firstContainer
= GetFocusObject();
872 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
876 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
877 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
878 // is the common ancestor.
879 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
882 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
884 wxRichTextObject
* p
= hitObj2
;
887 if (p
->GetParent() == commonAncestor
)
889 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
896 if (commonAncestor
&& firstContainer
&& otherContainer
)
898 // We have now got a second container that shares a parent with the current or anchor object.
899 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
901 // Don't go into common-ancestor selection mode if we still have the same
903 if (otherContainer
!= firstContainer
)
905 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
906 m_selectionAnchorObject
= firstContainer
;
907 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
909 // The common ancestor, such as a table, returns the cell selection
910 // between the anchor and current position.
911 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
916 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
921 if (otherContainer
->AcceptsFocus())
922 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
923 MoveCaret(-1, false);
924 SetDefaultStyleToCursorStyle();
929 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
930 #if wxUSE_DRAG_AND_DROP
935 // TODO: test closeness
936 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
941 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
947 dc
.SetFont(GetFont());
950 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
951 wxRichTextObject
* hitObj
= NULL
;
952 wxRichTextObject
* contextObj
= NULL
;
953 wxRichTextDrawingContext
context(& GetBuffer());
954 int hit
= GetFocusObject()->HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
);
956 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
958 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
959 if (actualContainer
&& actualContainer
->AcceptsFocus())
961 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
962 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
966 wxRichTextEvent
cmdEvent(
967 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
969 cmdEvent
.SetEventObject(this);
970 cmdEvent
.SetPosition(position
);
972 cmdEvent
.SetContainer(hitObj
->GetContainer());
974 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
978 /// Left-double-click
979 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
981 wxRichTextEvent
cmdEvent(
982 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
984 cmdEvent
.SetEventObject(this);
985 cmdEvent
.SetPosition(m_caretPosition
+1);
986 cmdEvent
.SetContainer(GetFocusObject());
988 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
990 SelectWord(GetCaretPosition()+1);
995 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
997 wxRichTextEvent
cmdEvent(
998 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
1000 cmdEvent
.SetEventObject(this);
1001 cmdEvent
.SetPosition(m_caretPosition
+1);
1002 cmdEvent
.SetContainer(GetFocusObject());
1004 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1007 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1008 // Paste any PRIMARY selection, if it exists
1009 wxTheClipboard
->UsePrimarySelection(true);
1011 wxTheClipboard
->UsePrimarySelection(false);
1016 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
1019 if (event
.CmdDown())
1020 flags
|= wxRICHTEXT_CTRL_DOWN
;
1021 if (event
.ShiftDown())
1022 flags
|= wxRICHTEXT_SHIFT_DOWN
;
1023 if (event
.AltDown())
1024 flags
|= wxRICHTEXT_ALT_DOWN
;
1026 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
1028 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
1030 KeyboardNavigate(event
.GetKeyCode(), flags
);
1034 long keycode
= event
.GetKeyCode();
1094 case WXK_NUMPAD_HOME
:
1095 case WXK_NUMPAD_LEFT
:
1097 case WXK_NUMPAD_RIGHT
:
1098 case WXK_NUMPAD_DOWN
:
1099 case WXK_NUMPAD_PAGEUP
:
1100 case WXK_NUMPAD_PAGEDOWN
:
1101 case WXK_NUMPAD_END
:
1102 case WXK_NUMPAD_BEGIN
:
1103 case WXK_NUMPAD_INSERT
:
1104 case WXK_WINDOWS_LEFT
:
1113 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1114 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
1116 BeginBatchUndo(_("Delete Text"));
1118 long newPos
= m_caretPosition
;
1120 bool processed
= DeleteSelectedContent(& newPos
);
1122 // Submit range in character positions, which are greater than caret positions,
1123 // so subtract 1 for deleted character and add 1 for conversion to character position.
1126 if (event
.CmdDown())
1128 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1131 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
1137 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
1142 if (GetLastPosition() == -1)
1144 GetFocusObject()->Reset();
1146 m_caretPosition
= -1;
1148 SetDefaultStyleToCursorStyle();
1151 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1153 wxRichTextEvent
cmdEvent(
1154 wxEVT_COMMAND_RICHTEXT_DELETE
,
1156 cmdEvent
.SetEventObject(this);
1157 cmdEvent
.SetFlags(flags
);
1158 cmdEvent
.SetPosition(m_caretPosition
+1);
1159 cmdEvent
.SetContainer(GetFocusObject());
1160 GetEventHandler()->ProcessEvent(cmdEvent
);
1170 // all the other keys modify the controls contents which shouldn't be
1171 // possible if we're read-only
1172 if ( !IsEditable() )
1178 if (event
.GetKeyCode() == WXK_RETURN
)
1180 BeginBatchUndo(_("Insert Text"));
1182 long newPos
= m_caretPosition
;
1184 DeleteSelectedContent(& newPos
);
1186 if (event
.ShiftDown())
1189 text
= wxRichTextLineBreakChar
;
1190 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, text
, this);
1191 m_caretAtLineStart
= true;
1195 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1198 SetDefaultStyleToCursorStyle();
1200 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1202 wxRichTextEvent
cmdEvent(
1203 wxEVT_COMMAND_RICHTEXT_RETURN
,
1205 cmdEvent
.SetEventObject(this);
1206 cmdEvent
.SetFlags(flags
);
1207 cmdEvent
.SetPosition(newPos
+1);
1208 cmdEvent
.SetContainer(GetFocusObject());
1210 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1212 // Generate conventional event
1213 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1214 InitCommandEvent(textEvent
);
1216 GetEventHandler()->ProcessEvent(textEvent
);
1220 else if (event
.GetKeyCode() == WXK_BACK
)
1222 BeginBatchUndo(_("Delete Text"));
1224 long newPos
= m_caretPosition
;
1226 bool processed
= DeleteSelectedContent(& newPos
);
1228 // Submit range in character positions, which are greater than caret positions,
1229 // so subtract 1 for deleted character and add 1 for conversion to character position.
1232 if (event
.CmdDown())
1234 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1237 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
1243 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
1248 if (GetLastPosition() == -1)
1250 GetFocusObject()->Reset();
1252 m_caretPosition
= -1;
1254 SetDefaultStyleToCursorStyle();
1257 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1259 wxRichTextEvent
cmdEvent(
1260 wxEVT_COMMAND_RICHTEXT_DELETE
,
1262 cmdEvent
.SetEventObject(this);
1263 cmdEvent
.SetFlags(flags
);
1264 cmdEvent
.SetPosition(m_caretPosition
+1);
1265 cmdEvent
.SetContainer(GetFocusObject());
1266 GetEventHandler()->ProcessEvent(cmdEvent
);
1270 else if (event
.GetKeyCode() == WXK_DELETE
)
1272 BeginBatchUndo(_("Delete Text"));
1274 long newPos
= m_caretPosition
;
1276 bool processed
= DeleteSelectedContent(& newPos
);
1278 // Submit range in character positions, which are greater than caret positions,
1279 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1281 if (event
.CmdDown())
1283 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1284 if (pos
!= -1 && (pos
> newPos
))
1286 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this, & GetBuffer());
1291 if (!processed
&& newPos
< (GetLastPosition()-1))
1292 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this, & GetBuffer());
1297 if (GetLastPosition() == -1)
1299 GetFocusObject()->Reset();
1301 m_caretPosition
= -1;
1303 SetDefaultStyleToCursorStyle();
1306 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1308 wxRichTextEvent
cmdEvent(
1309 wxEVT_COMMAND_RICHTEXT_DELETE
,
1311 cmdEvent
.SetEventObject(this);
1312 cmdEvent
.SetFlags(flags
);
1313 cmdEvent
.SetPosition(m_caretPosition
+1);
1314 cmdEvent
.SetContainer(GetFocusObject());
1315 GetEventHandler()->ProcessEvent(cmdEvent
);
1321 long keycode
= event
.GetKeyCode();
1333 if (event
.CmdDown())
1335 // Fixes AltGr+key with European input languages on Windows
1336 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1343 wxRichTextEvent
cmdEvent(
1344 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1346 cmdEvent
.SetEventObject(this);
1347 cmdEvent
.SetFlags(flags
);
1349 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1351 cmdEvent
.SetCharacter((wxChar
) keycode
);
1353 cmdEvent
.SetPosition(m_caretPosition
+1);
1354 cmdEvent
.SetContainer(GetFocusObject());
1356 if (keycode
== wxT('\t'))
1358 // See if we need to promote or demote the selection or paragraph at the cursor
1359 // position, instead of inserting a tab.
1360 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1361 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1362 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1364 wxRichTextRange range
;
1366 range
= GetSelectionRange();
1368 range
= para
->GetRange().FromInternal();
1370 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1372 PromoteList(promoteBy
, range
, NULL
);
1374 GetEventHandler()->ProcessEvent(cmdEvent
);
1380 BeginBatchUndo(_("Insert Text"));
1382 long newPos
= m_caretPosition
;
1383 DeleteSelectedContent(& newPos
);
1386 wxString str
= event
.GetUnicodeKey();
1388 wxString str
= (wxChar
) event
.GetKeyCode();
1390 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos
+1, str
, this, 0);
1394 SetDefaultStyleToCursorStyle();
1395 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1397 cmdEvent
.SetPosition(m_caretPosition
);
1398 GetEventHandler()->ProcessEvent(cmdEvent
);
1406 bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox
* container
, wxRichTextObject
* WXUNUSED(obj
), long position
, const wxPoint
& WXUNUSED(pos
))
1408 wxRichTextAttr attr
;
1409 if (container
&& GetStyle(position
, attr
, container
))
1411 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
1413 SetCursor(m_urlCursor
);
1415 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
1417 SetCursor(m_textCursor
);
1425 /// Delete content if there is a selection, e.g. when pressing a key.
1426 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1430 long pos
= m_selection
.GetRange().GetStart();
1431 wxRichTextRange range
= m_selection
.GetRange();
1433 // SelectAll causes more to be selected than doing it interactively,
1434 // and causes a new paragraph to be inserted. So for multiline buffers,
1435 // don't delete the final position.
1436 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1437 range
.SetEnd(range
.GetEnd()-1);
1439 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1440 m_selection
.Reset();
1441 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1451 /// Keyboard navigation
1455 Left: left one character
1456 Right: right one character
1459 Ctrl-Left: left one word
1460 Ctrl-Right: right one word
1461 Ctrl-Up: previous paragraph start
1462 Ctrl-Down: next start of paragraph
1465 Ctrl-Home: start of document
1466 Ctrl-End: end of document
1467 Page-Up: Up a screen
1468 Page-Down: Down a screen
1472 Ctrl-Alt-PgUp: Start of window
1473 Ctrl-Alt-PgDn: End of window
1474 F8: Start selection mode
1475 Esc: End selection mode
1477 Adding Shift does the above but starts/extends selection.
1482 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1484 bool success
= false;
1486 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1488 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1489 success
= WordRight(1, flags
);
1491 success
= MoveRight(1, flags
);
1493 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1495 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1496 success
= WordLeft(1, flags
);
1498 success
= MoveLeft(1, flags
);
1500 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1502 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1503 success
= MoveToParagraphStart(flags
);
1505 success
= MoveUp(1, flags
);
1507 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1509 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1510 success
= MoveToParagraphEnd(flags
);
1512 success
= MoveDown(1, flags
);
1514 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1516 success
= PageUp(1, flags
);
1518 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1520 success
= PageDown(1, flags
);
1522 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1524 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1525 success
= MoveHome(flags
);
1527 success
= MoveToLineStart(flags
);
1529 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1531 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1532 success
= MoveEnd(flags
);
1534 success
= MoveToLineEnd(flags
);
1539 ScrollIntoView(m_caretPosition
, keyCode
);
1540 SetDefaultStyleToCursorStyle();
1546 /// Extend the selection. Selections are in caret positions.
1547 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1549 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1551 if (oldPos
== newPos
)
1554 wxRichTextSelection oldSelection
= m_selection
;
1556 m_selection
.SetContainer(GetFocusObject());
1558 wxRichTextRange oldRange
;
1559 if (m_selection
.IsValid())
1560 oldRange
= m_selection
.GetRange();
1562 oldRange
= wxRICHTEXT_NO_SELECTION
;
1563 wxRichTextRange newRange
;
1565 // If not currently selecting, start selecting
1566 if (oldRange
.GetStart() == -2)
1568 m_selectionAnchor
= oldPos
;
1570 if (oldPos
> newPos
)
1571 newRange
.SetRange(newPos
+1, oldPos
);
1573 newRange
.SetRange(oldPos
+1, newPos
);
1577 // Always ensure that the selection range start is greater than
1579 if (newPos
> m_selectionAnchor
)
1580 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1581 else if (newPos
== m_selectionAnchor
)
1582 newRange
= wxRichTextRange(-2, -2);
1584 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1587 m_selection
.SetRange(newRange
);
1589 RefreshForSelectionChange(oldSelection
, m_selection
);
1591 if (newRange
.GetStart() > newRange
.GetEnd())
1593 wxLogDebug(wxT("Strange selection range"));
1602 /// Scroll into view, returning true if we scrolled.
1603 /// This takes a _caret_ position.
1604 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1606 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1612 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1614 int startXUnits
, startYUnits
;
1615 GetViewStart(& startXUnits
, & startYUnits
);
1616 int startY
= startYUnits
* ppuY
;
1619 GetVirtualSize(& sx
, & sy
);
1625 wxRect rect
= line
->GetRect();
1627 bool scrolled
= false;
1629 wxSize clientSize
= GetClientSize();
1631 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1634 wxClientDC
dc(this);
1635 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1636 topMargin
, bottomMargin
);
1638 // clientSize.y -= GetBuffer().GetBottomMargin();
1639 clientSize
.y
-= bottomMargin
;
1641 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1643 int y
= rect
.y
- GetClientSize().y
/2;
1644 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1645 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1647 if (startYUnits
!= yUnits
)
1649 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1652 #if !wxRICHTEXT_USE_OWN_CARET
1662 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1663 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1664 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1665 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1667 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1669 // Make it scroll so this item is at the bottom
1671 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1672 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1674 // If we're still off the screen, scroll another line down
1675 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1678 if (startYUnits
!= yUnits
)
1680 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1684 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1686 // Make it scroll so this item is at the top
1688 int y
= rect
.y
- GetBuffer().GetTopMargin();
1689 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1691 if (startYUnits
!= yUnits
)
1693 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1699 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1700 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1701 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1702 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1704 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1706 // Make it scroll so this item is at the top
1708 int y
= rect
.y
- GetBuffer().GetTopMargin();
1709 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1711 if (startYUnits
!= yUnits
)
1713 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1717 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1719 // Make it scroll so this item is at the bottom
1721 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1722 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1724 // If we're still off the screen, scroll another line down
1725 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1728 if (startYUnits
!= yUnits
)
1730 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1736 #if !wxRICHTEXT_USE_OWN_CARET
1744 /// Is the given position visible on the screen?
1745 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1747 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1753 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1756 GetViewStart(& startX
, & startY
);
1758 startY
= startY
* ppuY
;
1760 wxRect rect
= line
->GetRect();
1761 wxSize clientSize
= GetClientSize();
1762 clientSize
.y
-= GetBuffer().GetBottomMargin();
1764 return (rect
.GetTop() >= (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1767 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1769 m_caretPosition
= position
;
1770 m_caretAtLineStart
= showAtLineStart
;
1773 /// Move caret one visual step forward: this may mean setting a flag
1774 /// and keeping the same position if we're going from the end of one line
1775 /// to the start of the next, which may be the exact same caret position.
1776 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1778 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1780 // Only do the check if we're not at the end of the paragraph (where things work OK
1782 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1784 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1788 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1790 // We're at the end of a line. See whether we need to
1791 // stay at the same actual caret position but change visual
1792 // position, or not.
1793 if (oldPosition
== lineRange
.GetEnd())
1795 if (m_caretAtLineStart
)
1797 // We're already at the start of the line, so actually move on now.
1798 m_caretPosition
= oldPosition
+ 1;
1799 m_caretAtLineStart
= false;
1803 // We're showing at the end of the line, so keep to
1804 // the same position but indicate that we're to show
1805 // at the start of the next line.
1806 m_caretPosition
= oldPosition
;
1807 m_caretAtLineStart
= true;
1809 SetDefaultStyleToCursorStyle();
1815 SetDefaultStyleToCursorStyle();
1818 /// Move caret one visual step backward: this may mean setting a flag
1819 /// and keeping the same position if we're going from the end of one line
1820 /// to the start of the next, which may be the exact same caret position.
1821 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1823 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1825 // Only do the check if we're not at the start of the paragraph (where things work OK
1827 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1829 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1833 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1835 // We're at the start of a line. See whether we need to
1836 // stay at the same actual caret position but change visual
1837 // position, or not.
1838 if (oldPosition
== lineRange
.GetStart())
1840 m_caretPosition
= oldPosition
-1;
1841 m_caretAtLineStart
= true;
1844 else if (oldPosition
== lineRange
.GetEnd())
1846 if (m_caretAtLineStart
)
1848 // We're at the start of the line, so keep the same caret position
1849 // but clear the start-of-line flag.
1850 m_caretPosition
= oldPosition
;
1851 m_caretAtLineStart
= false;
1855 // We're showing at the end of the line, so go back
1856 // to the previous character position.
1857 m_caretPosition
= oldPosition
- 1;
1859 SetDefaultStyleToCursorStyle();
1865 SetDefaultStyleToCursorStyle();
1869 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1871 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1873 if (m_caretPosition
+ noPositions
< endPos
)
1875 long oldPos
= m_caretPosition
;
1876 long newPos
= m_caretPosition
+ noPositions
;
1878 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1882 // Determine by looking at oldPos and m_caretPosition whether
1883 // we moved from the end of a line to the start of the next line, in which case
1884 // we want to adjust the caret position such that it is positioned at the
1885 // start of the next line, rather than jumping past the first character of the
1887 if (noPositions
== 1 && !extendSel
)
1888 MoveCaretForward(oldPos
);
1890 SetCaretPosition(newPos
);
1893 SetDefaultStyleToCursorStyle();
1902 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1906 if (m_caretPosition
> startPos
- noPositions
+ 1)
1908 long oldPos
= m_caretPosition
;
1909 long newPos
= m_caretPosition
- noPositions
;
1910 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1914 if (noPositions
== 1 && !extendSel
)
1915 MoveCaretBack(oldPos
);
1917 SetCaretPosition(newPos
);
1920 SetDefaultStyleToCursorStyle();
1928 // Find the caret position for the combination of hit-test flags and character position.
1929 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
1930 // since this is ambiguous (same position used for end of line and start of next).
1931 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
1932 bool& caretLineStart
)
1934 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1935 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1936 // so we view the caret at the start of the line.
1937 caretLineStart
= false;
1938 long caretPosition
= position
;
1940 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
1942 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
1943 wxRichTextRange lineRange
;
1945 lineRange
= thisLine
->GetAbsoluteRange();
1947 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
1950 caretLineStart
= true;
1954 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
1955 if (para
&& para
->GetRange().GetStart() == position
)
1959 return caretPosition
;
1963 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1965 return MoveDown(- noLines
, flags
);
1969 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1974 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1975 wxPoint pt
= GetCaret()->GetPosition();
1976 long newLine
= lineNumber
+ noLines
;
1977 bool notInThisObject
= false;
1979 if (lineNumber
!= -1)
1983 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
1984 if (newLine
> lastLine
)
1985 notInThisObject
= true;
1990 notInThisObject
= true;
1994 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
1995 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
;
1997 if (notInThisObject
)
1999 // If we know we're navigating out of the current object,
2000 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2001 container
= & GetBuffer();
2002 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
2004 if (noLines
> 0) // going down
2006 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
2010 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
2015 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
2017 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
2023 wxClientDC
dc(this);
2025 dc
.SetFont(GetFont());
2027 wxRichTextObject
* hitObj
= NULL
;
2028 wxRichTextObject
* contextObj
= NULL
;
2029 wxRichTextDrawingContext
context(& GetBuffer());
2030 int hitTest
= container
->HitTest(dc
, context
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
2033 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
2034 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
2037 if (notInThisObject
)
2039 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2040 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
2042 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
2044 container
= actualContainer
;
2048 bool caretLineStart
= true;
2049 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
2050 long newSelEnd
= caretPosition
;
2053 if (notInThisObject
)
2056 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
2061 SetCaretPosition(caretPosition
, caretLineStart
);
2063 SetDefaultStyleToCursorStyle();
2071 /// Move to the end of the paragraph
2072 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
2074 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2077 long newPos
= para
->GetRange().GetEnd() - 1;
2078 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2082 SetCaretPosition(newPos
);
2084 SetDefaultStyleToCursorStyle();
2092 /// Move to the start of the paragraph
2093 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
2095 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
2098 long newPos
= para
->GetRange().GetStart() - 1;
2099 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2103 SetCaretPosition(newPos
);
2105 SetDefaultStyleToCursorStyle();
2113 /// Move to the end of the line
2114 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
2116 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2120 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2121 long newPos
= lineRange
.GetEnd();
2122 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2126 SetCaretPosition(newPos
);
2128 SetDefaultStyleToCursorStyle();
2136 /// Move to the start of the line
2137 bool wxRichTextCtrl::MoveToLineStart(int flags
)
2139 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2142 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
2143 long newPos
= lineRange
.GetStart()-1;
2145 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
2149 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
2151 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2153 SetDefaultStyleToCursorStyle();
2161 /// Move to the start of the buffer
2162 bool wxRichTextCtrl::MoveHome(int flags
)
2164 if (m_caretPosition
!= -1)
2166 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2170 SetCaretPosition(-1);
2172 SetDefaultStyleToCursorStyle();
2180 /// Move to the end of the buffer
2181 bool wxRichTextCtrl::MoveEnd(int flags
)
2183 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2185 if (m_caretPosition
!= endPos
)
2187 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2191 SetCaretPosition(endPos
);
2193 SetDefaultStyleToCursorStyle();
2201 /// Move noPages pages up
2202 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2204 return PageDown(- noPages
, flags
);
2207 /// Move noPages pages down
2208 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2210 // Calculate which line occurs noPages * screen height further down.
2211 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2214 wxSize clientSize
= GetClientSize();
2215 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2217 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2220 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2221 long pos
= lineRange
.GetStart()-1;
2222 if (pos
!= m_caretPosition
)
2224 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2226 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2230 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2232 SetDefaultStyleToCursorStyle();
2242 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2244 return str
== wxT(" ") || str
== wxT("\t");
2247 // Finds the caret position for the next word
2248 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2250 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2254 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2256 // First skip current text to space
2257 while (i
< endPos
&& i
> -1)
2259 // i is in character, not caret positions
2260 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2261 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2262 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2266 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2273 while (i
< endPos
&& i
> -1)
2275 // i is in character, not caret positions
2276 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2277 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2278 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2279 return wxMax(-1, i
);
2281 if (text
.empty()) // End of paragraph, or maybe an image
2282 return wxMax(-1, i
- 1);
2283 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2287 // Convert to caret position
2288 return wxMax(-1, i
- 1);
2297 long i
= m_caretPosition
;
2299 // First skip white space
2300 while (i
< endPos
&& i
> -1)
2302 // i is in character, not caret positions
2303 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2304 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2306 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2308 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2313 // Next skip current text to space
2314 while (i
< endPos
&& i
> -1)
2316 // i is in character, not caret positions
2317 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2318 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2319 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2322 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2335 /// Move n words left
2336 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2338 long pos
= FindNextWordPosition(-1);
2339 if (pos
!= m_caretPosition
)
2341 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2343 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2347 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2349 SetDefaultStyleToCursorStyle();
2357 /// Move n words right
2358 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2360 long pos
= FindNextWordPosition(1);
2361 if (pos
!= m_caretPosition
)
2363 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2365 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2369 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2371 SetDefaultStyleToCursorStyle();
2380 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2382 // Only do sizing optimization for large buffers
2383 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2385 m_fullLayoutRequired
= true;
2386 m_fullLayoutTime
= wxGetLocalTimeMillis();
2387 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2388 LayoutContent(true /* onlyVisibleRect */);
2391 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2393 #if wxRICHTEXT_BUFFERED_PAINTING
2400 // Force any pending layout due to large buffer
2401 void wxRichTextCtrl::ForceDelayedLayout()
2403 if (m_fullLayoutRequired
)
2405 m_fullLayoutRequired
= false;
2406 m_fullLayoutTime
= 0;
2407 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2408 ShowPosition(m_fullLayoutSavedPosition
);
2414 /// Idle-time processing
2415 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2417 #if wxRICHTEXT_USE_OWN_CARET
2418 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2420 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2426 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2428 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2430 m_fullLayoutRequired
= false;
2431 m_fullLayoutTime
= 0;
2432 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2433 ShowPosition(m_fullLayoutSavedPosition
);
2437 if (m_caretPositionForDefaultStyle
!= -2)
2439 // If the caret position has changed, no longer reflect the default style
2441 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2442 m_caretPositionForDefaultStyle
= -2;
2449 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2451 #if wxRICHTEXT_USE_OWN_CARET
2452 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2455 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2462 /// Set up scrollbars, e.g. after a resize
2463 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2468 if (GetBuffer().IsEmpty())
2470 SetScrollbars(0, 0, 0, 0, 0, 0);
2474 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2475 // of pixels. See e.g. wxVScrolledWindow for ideas.
2476 int pixelsPerUnit
= 5;
2477 wxSize clientSize
= GetClientSize();
2479 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2481 // Round up so we have at least maxHeight pixels
2482 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2484 int startX
= 0, startY
= 0;
2486 GetViewStart(& startX
, & startY
);
2488 int maxPositionX
= 0;
2489 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2491 int newStartX
= wxMin(maxPositionX
, startX
);
2492 int newStartY
= wxMin(maxPositionY
, startY
);
2494 int oldPPUX
, oldPPUY
;
2495 int oldStartX
, oldStartY
;
2496 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2497 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2498 GetViewStart(& oldStartX
, & oldStartY
);
2499 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2501 oldVirtualSizeY
/= oldPPUY
;
2503 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2506 // Don't set scrollbars if there were none before, and there will be none now.
2507 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2510 // Move to previous scroll position if
2512 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2515 /// Paint the background
2516 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2518 wxColour backgroundColour
= GetBackgroundColour();
2519 if (!backgroundColour
.IsOk())
2520 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2522 // Clear the background
2523 dc
.SetBrush(wxBrush(backgroundColour
));
2524 dc
.SetPen(*wxTRANSPARENT_PEN
);
2525 wxRect
windowRect(GetClientSize());
2526 windowRect
.x
-= 2; windowRect
.y
-= 2;
2527 windowRect
.width
+= 4; windowRect
.height
+= 4;
2529 // We need to shift the rectangle to take into account
2530 // scrolling. Converting device to logical coordinates.
2531 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2532 dc
.DrawRectangle(windowRect
);
2535 #if wxRICHTEXT_BUFFERED_PAINTING
2536 /// Recreate buffer bitmap if necessary
2537 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2540 if (sz
== wxDefaultSize
)
2541 sz
= GetClientSize();
2543 if (sz
.x
< 1 || sz
.y
< 1)
2546 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2547 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2548 return m_bufferBitmap
.IsOk();
2552 // ----------------------------------------------------------------------------
2553 // file IO functions
2554 // ----------------------------------------------------------------------------
2556 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2558 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2560 m_filename
= filename
;
2563 SetInsertionPoint(0);
2566 SetupScrollbars(true);
2568 wxTextCtrl::SendTextUpdatedEvent(this);
2574 wxLogError(_("File couldn't be loaded."));
2580 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2582 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2584 m_filename
= filename
;
2591 wxLogError(_("The text couldn't be saved."));
2596 // ----------------------------------------------------------------------------
2597 // wxRichTextCtrl specific functionality
2598 // ----------------------------------------------------------------------------
2600 /// Add a new paragraph of text to the end of the buffer
2601 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2603 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2604 GetBuffer().Invalidate();
2610 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2612 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2613 GetBuffer().Invalidate();
2618 // ----------------------------------------------------------------------------
2619 // selection and ranges
2620 // ----------------------------------------------------------------------------
2622 void wxRichTextCtrl::SelectAll()
2624 SetSelection(-1, -1);
2628 void wxRichTextCtrl::SelectNone()
2630 if (m_selection
.IsValid())
2632 wxRichTextSelection oldSelection
= m_selection
;
2634 m_selection
.Reset();
2636 RefreshForSelectionChange(oldSelection
, m_selection
);
2638 m_selectionAnchor
= -2;
2639 m_selectionAnchorObject
= NULL
;
2640 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2643 static bool wxIsWordDelimiter(const wxString
& text
)
2645 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2648 /// Select the word at the given character position
2649 bool wxRichTextCtrl::SelectWord(long position
)
2651 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2654 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2658 if (position
== para
->GetRange().GetEnd())
2661 long positionStart
= position
;
2662 long positionEnd
= position
;
2664 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2666 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2667 if (wxIsWordDelimiter(text
))
2673 if (positionStart
< para
->GetRange().GetStart())
2674 positionStart
= para
->GetRange().GetStart();
2676 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2678 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2679 if (wxIsWordDelimiter(text
))
2685 if (positionEnd
>= para
->GetRange().GetEnd())
2686 positionEnd
= para
->GetRange().GetEnd();
2688 if (positionEnd
< positionStart
)
2691 SetSelection(positionStart
, positionEnd
+1);
2693 if (positionStart
>= 0)
2695 MoveCaret(positionStart
-1, true);
2696 SetDefaultStyleToCursorStyle();
2702 wxString
wxRichTextCtrl::GetStringSelection() const
2705 GetSelection(&from
, &to
);
2707 return GetRange(from
, to
);
2710 // ----------------------------------------------------------------------------
2712 // ----------------------------------------------------------------------------
2714 wxTextCtrlHitTestResult
2715 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2717 // implement in terms of the other overload as the native ports typically
2718 // can get the position and not (x, y) pair directly (although wxUniv
2719 // directly gets x and y -- and so overrides this method as well)
2721 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2723 if ( rc
!= wxTE_HT_UNKNOWN
)
2725 PositionToXY(pos
, x
, y
);
2731 wxTextCtrlHitTestResult
2732 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2735 wxClientDC
dc((wxRichTextCtrl
*) this);
2736 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2738 // Buffer uses logical position (relative to start of buffer)
2740 wxPoint pt2
= GetLogicalPoint(pt
);
2742 wxRichTextObject
* hitObj
= NULL
;
2743 wxRichTextObject
* contextObj
= NULL
;
2744 wxRichTextDrawingContext
context((wxRichTextBuffer
*) & GetBuffer());
2745 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, context
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2747 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2748 return wxTE_HT_BEFORE
;
2749 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2750 return wxTE_HT_BEYOND
;
2751 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2752 return wxTE_HT_ON_TEXT
;
2754 return wxTE_HT_UNKNOWN
;
2757 wxRichTextParagraphLayoutBox
*
2758 wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt
, long& position
, int& hit
, wxRichTextObject
* hitObj
, int flags
/* = 0*/)
2760 wxClientDC
dc(this);
2762 dc
.SetFont(GetFont());
2764 wxPoint logicalPt
= GetLogicalPoint(pt
);
2766 wxRichTextObject
* contextObj
= NULL
;
2767 wxRichTextDrawingContext
context(& GetBuffer());
2768 hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, &hitObj
, &contextObj
, flags
);
2769 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
2775 // ----------------------------------------------------------------------------
2776 // set/get the controls text
2777 // ----------------------------------------------------------------------------
2779 wxString
wxRichTextCtrl::DoGetValue() const
2781 return GetBuffer().GetText();
2784 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2786 // Public API for range is different from internals
2787 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2790 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2792 // Don't call Clear here, since it always sends a text updated event
2793 m_buffer
.ResetAndClearCommands();
2794 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2795 m_caretPosition
= -1;
2796 m_caretPositionForDefaultStyle
= -2;
2797 m_caretAtLineStart
= false;
2798 m_selection
.Reset();
2799 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2809 if (!value
.IsEmpty())
2811 // Remove empty paragraph
2812 GetBuffer().Clear();
2813 DoWriteText(value
, flags
);
2815 // for compatibility, don't move the cursor when doing SetValue()
2816 SetInsertionPoint(0);
2820 // still send an event for consistency
2821 if (flags
& SetValue_SendEvent
)
2822 wxTextCtrl::SendTextUpdatedEvent(this);
2827 void wxRichTextCtrl::WriteText(const wxString
& value
)
2832 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2834 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2836 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, valueUnix
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2838 if ( flags
& SetValue_SendEvent
)
2839 wxTextCtrl::SendTextUpdatedEvent(this);
2842 void wxRichTextCtrl::AppendText(const wxString
& text
)
2844 SetInsertionPointEnd();
2849 /// Write an image at the current insertion point
2850 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2852 wxRichTextImageBlock imageBlock
;
2854 wxImage image2
= image
;
2855 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2856 return WriteImage(imageBlock
, textAttr
);
2861 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2863 wxRichTextImageBlock imageBlock
;
2866 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2867 return WriteImage(imageBlock
, textAttr
);
2872 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2874 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition
+1, imageBlock
, this, 0, textAttr
);
2877 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2881 wxRichTextImageBlock imageBlock
;
2883 wxImage image
= bitmap
.ConvertToImage();
2884 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2885 return WriteImage(imageBlock
, textAttr
);
2891 // Write a text box at the current insertion point.
2892 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2894 wxRichTextBox
* textBox
= new wxRichTextBox
;
2895 textBox
->SetAttributes(textAttr
);
2896 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2897 textBox
->AddParagraph(wxEmptyString
);
2898 textBox
->SetParent(NULL
);
2900 // The object returned is the one actually inserted into the buffer,
2901 // while the original one is deleted.
2902 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, textBox
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2903 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
2907 // Write a table at the current insertion point, returning the table.
2908 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
2910 wxASSERT(rows
> 0 && cols
> 0);
2912 if (rows
<= 0 || cols
<= 0)
2915 wxRichTextTable
* table
= new wxRichTextTable
;
2916 table
->SetAttributes(tableAttr
);
2917 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2919 table
->CreateTable(rows
, cols
);
2921 table
->SetParent(NULL
);
2924 for (j
= 0; j
< rows
; j
++)
2926 for (i
= 0; i
< cols
; i
++)
2928 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
2932 // The object returned is the one actually inserted into the buffer,
2933 // while the original one is deleted.
2934 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition
+1, table
, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2935 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
2940 /// Insert a newline (actually paragraph) at the current insertion point.
2941 bool wxRichTextCtrl::Newline()
2943 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition
+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2946 /// Insert a line break at the current insertion point.
2947 bool wxRichTextCtrl::LineBreak()
2950 text
= wxRichTextLineBreakChar
;
2951 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition
+1, text
, this);
2954 // ----------------------------------------------------------------------------
2955 // Clipboard operations
2956 // ----------------------------------------------------------------------------
2958 void wxRichTextCtrl::Copy()
2962 wxRichTextRange range
= GetInternalSelectionRange();
2963 GetBuffer().CopyToClipboard(range
);
2967 void wxRichTextCtrl::Cut()
2971 wxRichTextRange range
= GetInternalSelectionRange();
2972 GetBuffer().CopyToClipboard(range
);
2974 DeleteSelectedContent();
2980 void wxRichTextCtrl::Paste()
2984 BeginBatchUndo(_("Paste"));
2986 long newPos
= m_caretPosition
;
2987 DeleteSelectedContent(& newPos
);
2989 GetBuffer().PasteFromClipboard(newPos
);
2995 void wxRichTextCtrl::DeleteSelection()
2997 if (CanDeleteSelection())
2999 DeleteSelectedContent();
3003 bool wxRichTextCtrl::HasSelection() const
3005 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
3008 bool wxRichTextCtrl::HasUnfocusedSelection() const
3010 return m_selection
.IsValid();
3013 bool wxRichTextCtrl::CanCopy() const
3015 // Can copy if there's a selection
3016 return HasSelection();
3019 bool wxRichTextCtrl::CanCut() const
3021 return HasSelection() && IsEditable();
3024 bool wxRichTextCtrl::CanPaste() const
3026 if ( !IsEditable() )
3029 return GetBuffer().CanPasteFromClipboard();
3032 bool wxRichTextCtrl::CanDeleteSelection() const
3034 return HasSelection() && IsEditable();
3038 // ----------------------------------------------------------------------------
3040 // ----------------------------------------------------------------------------
3042 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
3044 if (m_contextMenu
&& m_contextMenu
!= menu
)
3045 delete m_contextMenu
;
3046 m_contextMenu
= menu
;
3049 void wxRichTextCtrl::SetEditable(bool editable
)
3051 m_editable
= editable
;
3054 void wxRichTextCtrl::SetInsertionPoint(long pos
)
3058 m_caretPosition
= pos
- 1;
3062 SetDefaultStyleToCursorStyle();
3065 void wxRichTextCtrl::SetInsertionPointEnd()
3067 long pos
= GetLastPosition();
3068 SetInsertionPoint(pos
);
3071 long wxRichTextCtrl::GetInsertionPoint() const
3073 return m_caretPosition
+1;
3076 wxTextPos
wxRichTextCtrl::GetLastPosition() const
3078 return GetFocusObject()->GetOwnRange().GetEnd();
3081 // If the return values from and to are the same, there is no
3083 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
3085 if (m_selection
.IsValid())
3087 *from
= m_selection
.GetRange().GetStart();
3088 *to
= m_selection
.GetRange().GetEnd();
3098 bool wxRichTextCtrl::IsEditable() const
3103 // ----------------------------------------------------------------------------
3105 // ----------------------------------------------------------------------------
3107 void wxRichTextCtrl::SetSelection(long from
, long to
)
3109 // if from and to are both -1, it means (in wxWidgets) that all text should
3111 if ( (from
== -1) && (to
== -1) )
3114 to
= GetLastPosition()+1;
3123 wxRichTextSelection oldSelection
= m_selection
;
3125 m_selectionAnchor
= from
-1;
3126 m_selectionAnchorObject
= NULL
;
3127 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
3129 m_caretPosition
= wxMax(-1, to
-1);
3131 RefreshForSelectionChange(oldSelection
, m_selection
);
3136 // ----------------------------------------------------------------------------
3138 // ----------------------------------------------------------------------------
3140 void wxRichTextCtrl::Replace(long from
, long to
,
3141 const wxString
& value
)
3143 BeginBatchUndo(_("Replace"));
3145 SetSelection(from
, to
);
3147 wxRichTextAttr attr
= GetDefaultStyle();
3149 DeleteSelectedContent();
3151 SetDefaultStyle(attr
);
3153 DoWriteText(value
, SetValue_SelectionOnly
);
3158 void wxRichTextCtrl::Remove(long from
, long to
)
3162 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
3169 bool wxRichTextCtrl::IsModified() const
3171 return m_buffer
.IsModified();
3174 void wxRichTextCtrl::MarkDirty()
3176 m_buffer
.Modify(true);
3179 void wxRichTextCtrl::DiscardEdits()
3181 m_caretPositionForDefaultStyle
= -2;
3182 m_buffer
.Modify(false);
3183 m_buffer
.GetCommandProcessor()->ClearCommands();
3186 int wxRichTextCtrl::GetNumberOfLines() const
3188 return GetFocusObject()->GetParagraphCount();
3191 // ----------------------------------------------------------------------------
3192 // Positions <-> coords
3193 // ----------------------------------------------------------------------------
3195 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3197 return GetFocusObject()->XYToPosition(x
, y
);
3200 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3202 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3205 // ----------------------------------------------------------------------------
3207 // ----------------------------------------------------------------------------
3209 void wxRichTextCtrl::ShowPosition(long pos
)
3211 if (!IsPositionVisible(pos
))
3212 ScrollIntoView(pos
-1, WXK_DOWN
);
3215 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3217 return GetFocusObject()->GetParagraphLength(lineNo
);
3220 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3222 return GetFocusObject()->GetParagraphText(lineNo
);
3225 // ----------------------------------------------------------------------------
3227 // ----------------------------------------------------------------------------
3229 void wxRichTextCtrl::Undo()
3233 GetCommandProcessor()->Undo();
3237 void wxRichTextCtrl::Redo()
3241 GetCommandProcessor()->Redo();
3245 bool wxRichTextCtrl::CanUndo() const
3247 return GetCommandProcessor()->CanUndo() && IsEditable();
3250 bool wxRichTextCtrl::CanRedo() const
3252 return GetCommandProcessor()->CanRedo() && IsEditable();
3255 // ----------------------------------------------------------------------------
3256 // implementation details
3257 // ----------------------------------------------------------------------------
3259 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3261 SetValue(event
.GetString());
3262 GetEventHandler()->ProcessEvent(event
);
3265 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3267 // By default, load the first file into the text window.
3268 if (event
.GetNumberOfFiles() > 0)
3270 LoadFile(event
.GetFiles()[0]);
3274 wxSize
wxRichTextCtrl::DoGetBestSize() const
3276 return wxSize(10, 10);
3279 // ----------------------------------------------------------------------------
3280 // standard handlers for standard edit menu events
3281 // ----------------------------------------------------------------------------
3283 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3288 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3293 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3298 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3303 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3308 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3313 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3315 event
.Enable( CanCut() );
3318 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3320 event
.Enable( CanCopy() );
3323 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3325 event
.Enable( CanDeleteSelection() );
3328 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3330 event
.Enable( CanPaste() );
3333 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3335 event
.Enable( CanUndo() );
3336 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3339 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3341 event
.Enable( CanRedo() );
3342 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3345 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3347 if (GetLastPosition() > 0)
3351 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3353 event
.Enable(GetLastPosition() > 0);
3356 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3358 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3359 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3361 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3362 if (obj
&& CanEditProperties(obj
))
3363 EditProperties(obj
, this);
3365 m_contextMenuPropertiesInfo
.Clear();
3369 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3371 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3372 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3375 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3377 if (event
.GetEventObject() != this)
3383 ShowContextMenu(m_contextMenu
, event
.GetPosition());
3386 // Prepares the context menu, adding appropriate property-editing commands.
3387 // Returns the number of property commands added.
3388 int wxRichTextCtrl::PrepareContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3390 wxClientDC
dc(this);
3392 dc
.SetFont(GetFont());
3394 m_contextMenuPropertiesInfo
.Clear();
3397 wxRichTextObject
* hitObj
= NULL
;
3398 wxRichTextObject
* contextObj
= NULL
;
3399 if (pt
!= wxDefaultPosition
)
3401 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3402 wxRichTextDrawingContext
context(& GetBuffer());
3403 int hit
= GetBuffer().HitTest(dc
, context
, logicalPt
, position
, & hitObj
, & contextObj
);
3405 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3407 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3408 if (hitObj
&& actualContainer
)
3410 if (actualContainer
->AcceptsFocus())
3412 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3413 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3416 if (addPropertyCommands
)
3417 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3421 if (addPropertyCommands
)
3422 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3427 if (addPropertyCommands
)
3428 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3433 // Invoked from the keyboard, so don't set the caret position and don't use the event
3435 hitObj
= GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition
+1);
3437 contextObj
= hitObj
->GetParentContainer();
3439 contextObj
= GetFocusObject();
3441 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3442 if (hitObj
&& actualContainer
)
3444 if (addPropertyCommands
)
3445 m_contextMenuPropertiesInfo
.AddItems(this, actualContainer
, hitObj
);
3449 if (addPropertyCommands
)
3450 m_contextMenuPropertiesInfo
.AddItems(this, GetFocusObject(), NULL
);
3456 if (addPropertyCommands
)
3457 m_contextMenuPropertiesInfo
.AddMenuItems(menu
);
3458 return m_contextMenuPropertiesInfo
.GetCount();
3464 // Shows the context menu, adding appropriate property-editing commands
3465 bool wxRichTextCtrl::ShowContextMenu(wxMenu
* menu
, const wxPoint
& pt
, bool addPropertyCommands
)
3469 PrepareContextMenu(menu
, pt
, addPropertyCommands
);
3477 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3479 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3482 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3484 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3487 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3489 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3492 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3494 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3497 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
)
3499 GetFocusObject()->SetStyle(obj
, textAttr
);
3502 // extended style setting operation with flags including:
3503 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3504 // see richtextbuffer.h for more details.
3506 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3508 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3511 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3513 return GetBuffer().SetDefaultStyle(style
);
3516 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3518 wxRichTextAttr
attr1(style
);
3519 attr1
.GetTextBoxAttr().Reset();
3520 return GetBuffer().SetDefaultStyle(attr1
);
3523 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3525 return GetBuffer().GetDefaultStyle();
3528 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3530 wxRichTextAttr attr
;
3531 if (GetFocusObject()->GetStyle(position
, attr
))
3540 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3542 return GetFocusObject()->GetStyle(position
, style
);
3545 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3547 wxRichTextAttr attr
;
3548 if (container
->GetStyle(position
, attr
))
3557 // get the common set of styles for the range
3558 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3560 wxRichTextAttr attr
;
3561 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3570 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3572 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3575 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3577 return container
->GetStyleForRange(range
.ToInternal(), style
);
3580 /// Get the content (uncombined) attributes for this position.
3581 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3583 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3586 /// Get the content (uncombined) attributes for this position.
3587 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3589 return container
->GetUncombinedStyle(position
, style
);
3592 bool wxRichTextCtrl::SetProperties(const wxRichTextRange
& range
, const wxRichTextProperties
& properties
, int flags
)
3594 return GetFocusObject()->SetProperties(range
.ToInternal(), properties
, flags
);
3597 /// Set font, and also the buffer attributes
3598 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3600 wxControl::SetFont(font
);
3602 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3604 GetBuffer().SetBasicStyle(attr
);
3606 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3612 /// Transform logical to physical
3613 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3616 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3621 /// Transform physical to logical
3622 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3625 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3630 /// Position the caret
3631 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3636 //wxLogDebug(wxT("PositionCaret"));
3639 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3641 wxPoint newPt
= caretRect
.GetPosition();
3642 wxSize newSz
= caretRect
.GetSize();
3643 wxPoint pt
= GetPhysicalPoint(newPt
);
3644 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3647 if (GetCaret()->GetSize() != newSz
)
3648 GetCaret()->SetSize(newSz
);
3650 // Adjust size so the caret size and position doesn't appear in the margins
3651 if (((pt
.y
+ newSz
.y
) <= GetBuffer().GetTopMargin()) || (pt
.y
>= (GetClientSize().y
- GetBuffer().GetBottomMargin())))
3656 else if (pt
.y
< GetBuffer().GetTopMargin() && (pt
.y
+ newSz
.y
) > GetBuffer().GetTopMargin())
3658 newSz
.y
-= (GetBuffer().GetTopMargin() - pt
.y
);
3661 pt
.y
= GetBuffer().GetTopMargin();
3662 GetCaret()->SetSize(newSz
);
3665 else if (pt
.y
< (GetClientSize().y
- GetBuffer().GetBottomMargin()) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- GetBuffer().GetBottomMargin()))
3667 newSz
.y
= GetClientSize().y
- GetBuffer().GetBottomMargin() - pt
.y
;
3668 GetCaret()->SetSize(newSz
);
3671 GetCaret()->Move(pt
);
3677 /// Get the caret height and position for the given character position
3678 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3680 wxClientDC
dc(this);
3681 dc
.SetFont(GetFont());
3689 container
= GetFocusObject();
3691 wxRichTextDrawingContext
context(& GetBuffer());
3692 if (container
->FindPosition(dc
, context
, position
, pt
, & height
, m_caretAtLineStart
))
3694 // Caret height can't be zero
3696 height
= dc
.GetCharHeight();
3698 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3705 /// Gets the line for the visible caret position. If the caret is
3706 /// shown at the very end of the line, it means the next character is actually
3707 /// on the following line. So let's get the line we're expecting to find
3708 /// if this is the case.
3709 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3711 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3712 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3715 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3716 if (caretPosition
== lineRange
.GetStart()-1 &&
3717 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3719 if (!m_caretAtLineStart
)
3720 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3727 /// Move the caret to the given character position
3728 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3730 if (GetBuffer().IsDirty())
3734 container
= GetFocusObject();
3736 if (pos
<= container
->GetOwnRange().GetEnd())
3738 SetCaretPosition(pos
, showAtLineStart
);
3740 PositionCaret(container
);
3748 /// Layout the buffer: which we must do before certain operations, such as
3749 /// setting the caret position.
3750 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3752 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3754 wxRect
availableSpace(GetClientSize());
3755 if (availableSpace
.width
== 0)
3756 availableSpace
.width
= 10;
3757 if (availableSpace
.height
== 0)
3758 availableSpace
.height
= 10;
3760 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3761 if (onlyVisibleRect
)
3763 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3764 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3767 wxClientDC
dc(this);
3768 dc
.SetFont(GetFont());
3772 wxRichTextDrawingContext
context(& GetBuffer());
3773 GetBuffer().Defragment();
3774 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3775 GetBuffer().Layout(dc
, context
, availableSpace
, availableSpace
, flags
);
3776 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3785 /// Is all of the selection, or the current caret position, bold?
3786 bool wxRichTextCtrl::IsSelectionBold()
3790 wxRichTextAttr attr
;
3791 wxRichTextRange range
= GetSelectionRange();
3792 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3793 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3795 return HasCharacterAttributes(range
, attr
);
3799 // If no selection, then we need to combine current style with default style
3800 // to see what the effect would be if we started typing.
3801 wxRichTextAttr attr
;
3802 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3804 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3805 if (GetStyle(pos
, attr
))
3807 if (IsDefaultStyleShowing())
3808 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3809 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3815 /// Is all of the selection, or the current caret position, italics?
3816 bool wxRichTextCtrl::IsSelectionItalics()
3820 wxRichTextRange range
= GetSelectionRange();
3821 wxRichTextAttr attr
;
3822 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3823 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3825 return HasCharacterAttributes(range
, attr
);
3829 // If no selection, then we need to combine current style with default style
3830 // to see what the effect would be if we started typing.
3831 wxRichTextAttr attr
;
3832 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3834 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3835 if (GetStyle(pos
, attr
))
3837 if (IsDefaultStyleShowing())
3838 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3839 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3845 /// Is all of the selection, or the current caret position, underlined?
3846 bool wxRichTextCtrl::IsSelectionUnderlined()
3850 wxRichTextRange range
= GetSelectionRange();
3851 wxRichTextAttr attr
;
3852 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3853 attr
.SetFontUnderlined(true);
3855 return HasCharacterAttributes(range
, attr
);
3859 // If no selection, then we need to combine current style with default style
3860 // to see what the effect would be if we started typing.
3861 wxRichTextAttr attr
;
3862 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3863 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3865 if (GetStyle(pos
, attr
))
3867 if (IsDefaultStyleShowing())
3868 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3869 return attr
.GetFontUnderlined();
3875 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3876 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
3878 wxRichTextAttr attr
;
3879 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3880 attr
.SetTextEffectFlags(flag
);
3881 attr
.SetTextEffects(flag
);
3885 return HasCharacterAttributes(GetSelectionRange(), 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 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3892 if (GetStyle(pos
, attr
))
3894 if (IsDefaultStyleShowing())
3895 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3896 return (attr
.GetTextEffectFlags() & flag
) != 0;
3902 /// Apply bold to the selection
3903 bool wxRichTextCtrl::ApplyBoldToSelection()
3905 wxRichTextAttr attr
;
3906 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3907 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3910 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3913 wxRichTextAttr current
= GetDefaultStyleEx();
3914 current
.Apply(attr
);
3915 SetAndShowDefaultStyle(current
);
3920 /// Apply italic to the selection
3921 bool wxRichTextCtrl::ApplyItalicToSelection()
3923 wxRichTextAttr attr
;
3924 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3925 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3928 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3931 wxRichTextAttr current
= GetDefaultStyleEx();
3932 current
.Apply(attr
);
3933 SetAndShowDefaultStyle(current
);
3938 /// Apply underline to the selection
3939 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3941 wxRichTextAttr attr
;
3942 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3943 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3946 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3949 wxRichTextAttr current
= GetDefaultStyleEx();
3950 current
.Apply(attr
);
3951 SetAndShowDefaultStyle(current
);
3956 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
3957 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
3959 wxRichTextAttr attr
;
3960 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3961 attr
.SetTextEffectFlags(flags
);
3962 if (!DoesSelectionHaveTextEffectFlag(flags
))
3963 attr
.SetTextEffects(flags
);
3965 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
3968 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3971 wxRichTextAttr current
= GetDefaultStyleEx();
3972 current
.Apply(attr
);
3973 SetAndShowDefaultStyle(current
);
3978 /// Is all of the selection aligned according to the specified flag?
3979 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3981 wxRichTextRange range
;
3983 range
= GetSelectionRange();
3985 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3987 wxRichTextAttr attr
;
3988 attr
.SetAlignment(alignment
);
3990 return HasParagraphAttributes(range
, attr
);
3993 /// Apply alignment to the selection
3994 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3996 wxRichTextAttr attr
;
3997 attr
.SetAlignment(alignment
);
3999 return SetStyle(GetSelectionRange(), attr
);
4002 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
4004 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
4009 /// Apply a named style to the selection
4010 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
4012 // Flags are defined within each definition, so only certain
4013 // attributes are applied.
4014 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
4016 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
4018 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
4020 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4022 wxRichTextRange range
;
4025 range
= GetSelectionRange();
4028 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4029 range
= wxRichTextRange(pos
, pos
+1);
4032 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
4035 bool isPara
= false;
4037 // Make sure the attr has the style name
4038 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
4041 attr
.SetParagraphStyleName(def
->GetName());
4043 // If applying a paragraph style, we only want the paragraph nodes to adopt these
4044 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
4045 // to change its style independently.
4046 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
4048 else if (def
->IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition
)))
4049 attr
.SetCharacterStyleName(def
->GetName());
4050 else if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4051 attr
.GetTextBoxAttr().SetBoxStyleName(def
->GetName());
4053 if (def
->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition
)))
4055 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4057 SetStyle(GetFocusObject(), attr
);
4063 else if (HasSelection())
4064 return SetStyleEx(GetSelectionRange(), attr
, flags
);
4067 wxRichTextAttr current
= GetDefaultStyleEx();
4068 wxRichTextAttr
defaultStyle(attr
);
4071 // Don't apply extra character styles since they are already implied
4072 // in the paragraph style
4073 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
4075 current
.Apply(defaultStyle
);
4076 SetAndShowDefaultStyle(current
);
4078 // If it's a paragraph style, we want to apply the style to the
4079 // current paragraph even if we didn't select any text.
4082 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4083 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
4086 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
4093 /// Apply the style sheet to the buffer, for example if the styles have changed.
4094 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
4097 styleSheet
= GetBuffer().GetStyleSheet();
4101 if (GetBuffer().ApplyStyleSheet(styleSheet
))
4103 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
4111 /// Sets the default style to the style under the cursor
4112 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4114 wxRichTextAttr attr
;
4115 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
4117 // If at the start of a paragraph, use the next position.
4118 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
4120 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(pos
);
4121 if (obj
&& obj
->IsTopLevel())
4123 // Don't use the attributes of a top-level object, since they might apply
4124 // to content of the object, e.g. background colour.
4125 SetDefaultStyle(wxRichTextAttr());
4128 else if (GetUncombinedStyle(pos
, attr
))
4130 SetDefaultStyle(attr
);
4137 /// Returns the first visible position in the current view
4138 long wxRichTextCtrl::GetFirstVisiblePosition() const
4140 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
4142 return line
->GetAbsoluteRange().GetStart();
4147 /// Get the first visible point in the window
4148 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
4151 int startXUnits
, startYUnits
;
4153 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
4154 GetViewStart(& startXUnits
, & startYUnits
);
4156 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
4159 /// The adjusted caret position is the character position adjusted to take
4160 /// into account whether we're at the start of a paragraph, in which case
4161 /// style information should be taken from the next position, not current one.
4162 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
4164 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
4166 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
4171 /// Get/set the selection range in character positions. -1, -1 means no selection.
4172 /// The range is in API convention, i.e. a single character selection is denoted
4174 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
4176 wxRichTextRange range
= GetInternalSelectionRange();
4177 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
4178 range
.SetEnd(range
.GetEnd() + 1);
4182 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
4184 SetSelection(range
.GetStart(), range
.GetEnd());
4188 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4190 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4193 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4195 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4198 /// Clear list for given range
4199 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
4201 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
4204 /// Number/renumber any list elements in the given range
4205 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
4207 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
4210 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
4212 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
4215 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4216 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
4218 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
4221 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
4223 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
4226 /// Deletes the content in the given range
4227 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
4229 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
4232 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
4234 if (sm_availableFontNames
.GetCount() == 0)
4236 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
4237 sm_availableFontNames
.Sort();
4239 return sm_availableFontNames
;
4242 void wxRichTextCtrl::ClearAvailableFontNames()
4244 sm_availableFontNames
.Clear();
4247 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
4249 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4251 wxTextAttrEx basicStyle
= GetBasicStyle();
4252 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4253 SetBasicStyle(basicStyle
);
4254 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4259 // Refresh the area affected by a selection change
4260 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4262 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4263 // the selection contains whole containers rather than just text, so refresh everything
4264 // for now as it would be hard to compute the rectangle bounding all selections.
4265 // TODO: improve on this.
4266 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4267 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4273 wxRichTextRange oldRange
, newRange
;
4274 if (oldSelection
.IsValid())
4275 oldRange
= oldSelection
.GetRange();
4277 oldRange
= wxRICHTEXT_NO_SELECTION
;
4278 if (newSelection
.IsValid())
4279 newRange
= newSelection
.GetRange();
4281 newRange
= wxRICHTEXT_NO_SELECTION
;
4283 // Calculate the refresh rectangle - just the affected lines
4284 long firstPos
, lastPos
;
4285 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4287 firstPos
= newRange
.GetStart();
4288 lastPos
= newRange
.GetEnd();
4290 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4292 firstPos
= oldRange
.GetStart();
4293 lastPos
= oldRange
.GetEnd();
4295 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4301 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4302 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4305 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4306 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4308 if (firstLine
&& lastLine
)
4310 wxSize clientSize
= GetClientSize();
4311 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
4312 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
4315 pt1
.y
= wxMax(0, pt1
.y
);
4317 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4319 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4320 RefreshRect(rect
, false);
4328 // margins functions
4329 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4331 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4332 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4333 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4334 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4339 wxPoint
wxRichTextCtrl::DoGetMargins() const
4341 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4342 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4345 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4347 if (obj
&& !obj
->AcceptsFocus())
4350 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4351 bool changingContainer
= (m_focusObject
!= obj
);
4353 if (changingContainer
&& HasSelection())
4356 m_focusObject
= obj
;
4359 m_focusObject
= & m_buffer
;
4361 if (setCaretPosition
&& changingContainer
)
4363 m_selection
.Reset();
4364 m_selectionAnchor
= -2;
4365 m_selectionAnchorObject
= NULL
;
4366 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4370 m_caretAtLineStart
= false;
4371 MoveCaret(pos
, m_caretAtLineStart
);
4372 SetDefaultStyleToCursorStyle();
4374 wxRichTextEvent
cmdEvent(
4375 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4377 cmdEvent
.SetEventObject(this);
4378 cmdEvent
.SetPosition(m_caretPosition
+1);
4379 cmdEvent
.SetOldContainer(oldContainer
);
4380 cmdEvent
.SetContainer(m_focusObject
);
4382 GetEventHandler()->ProcessEvent(cmdEvent
);
4387 #if wxUSE_DRAG_AND_DROP
4388 void wxRichTextCtrl::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), wxDragResult def
, wxDataObject
* DataObj
)
4392 if ((def
!= wxDragCopy
) && (def
!= wxDragMove
))
4397 if (!GetSelection().IsValid())
4402 wxRichTextParagraphLayoutBox
* originContainer
= GetSelection().GetContainer();
4403 wxRichTextParagraphLayoutBox
* destContainer
= GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4405 wxRichTextBuffer
* richTextBuffer
= ((wxRichTextBufferDataObject
*)DataObj
)->GetRichTextBuffer();
4408 long position
= GetCaretPosition();
4409 wxRichTextRange selectionrange
= GetInternalSelectionRange();
4410 if (selectionrange
.Contains(position
) && (def
== wxDragMove
))
4412 // It doesn't make sense to move onto itself
4416 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4417 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4418 bool DeleteAfter
= (def
== wxDragMove
) && (position
> selectionrange
.GetEnd());
4419 if ((def
== wxDragMove
) && !DeleteAfter
)
4421 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4422 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4425 destContainer
->InsertParagraphsWithUndo(&GetBuffer(), position
+1, *richTextBuffer
, this, 0);
4426 ShowPosition(position
+ richTextBuffer
->GetOwnRange().GetEnd());
4428 delete richTextBuffer
;
4432 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4433 originContainer
->DeleteRangeWithUndo(selectionrange
, this, &GetBuffer());
4440 #endif // wxUSE_DRAG_AND_DROP
4443 #if wxUSE_DRAG_AND_DROP
4444 bool wxRichTextDropSource::GiveFeedback(wxDragResult
WXUNUSED(effect
))
4446 wxCHECK_MSG(m_rtc
, false, wxT("NULL m_rtc"));
4450 wxRichTextObject
* hitObj
= NULL
;
4451 wxRichTextParagraphLayoutBox
* container
= m_rtc
->FindContainerAtPoint(m_rtc
->ScreenToClient(wxGetMousePosition()), position
, hit
, hitObj
);
4453 if (!(hit
& wxRICHTEXT_HITTEST_NONE
) && container
&& container
->AcceptsFocus())
4455 m_rtc
->StoreFocusObject(container
);
4456 m_rtc
->SetCaretPositionAfterClick(container
, position
, hit
);
4459 return false; // so that the base-class sets a cursor
4461 #endif // wxUSE_DRAG_AND_DROP
4464 #if wxRICHTEXT_USE_OWN_CARET
4466 // ----------------------------------------------------------------------------
4467 // initialization and destruction
4468 // ----------------------------------------------------------------------------
4470 void wxRichTextCaret::Init()
4473 m_refreshEnabled
= true;
4477 m_richTextCtrl
= NULL
;
4478 m_needsUpdate
= false;
4482 wxRichTextCaret::~wxRichTextCaret()
4484 if (m_timer
.IsRunning())
4488 // ----------------------------------------------------------------------------
4489 // showing/hiding/moving the caret (base class interface)
4490 // ----------------------------------------------------------------------------
4492 void wxRichTextCaret::DoShow()
4496 if (!m_timer
.IsRunning())
4497 m_timer
.Start(GetBlinkTime());
4502 void wxRichTextCaret::DoHide()
4504 if (m_timer
.IsRunning())
4510 void wxRichTextCaret::DoMove()
4516 if (m_xOld
!= -1 && m_yOld
!= -1)
4518 if (m_richTextCtrl
&& m_refreshEnabled
)
4520 wxRect
rect(GetPosition(), GetSize());
4521 m_richTextCtrl
->RefreshRect(rect
, false);
4530 void wxRichTextCaret::DoSize()
4532 int countVisible
= m_countVisible
;
4533 if (countVisible
> 0)
4539 if (countVisible
> 0)
4541 m_countVisible
= countVisible
;
4546 // ----------------------------------------------------------------------------
4547 // handling the focus
4548 // ----------------------------------------------------------------------------
4550 void wxRichTextCaret::OnSetFocus()
4558 void wxRichTextCaret::OnKillFocus()
4563 // ----------------------------------------------------------------------------
4564 // drawing the caret
4565 // ----------------------------------------------------------------------------
4567 void wxRichTextCaret::Refresh()
4569 if (m_richTextCtrl
&& m_refreshEnabled
)
4571 wxRect
rect(GetPosition(), GetSize());
4572 m_richTextCtrl
->RefreshRect(rect
, false);
4576 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4578 dc
->SetPen( *wxBLACK_PEN
);
4580 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4581 dc
->SetPen(*wxBLACK_PEN
);
4583 wxPoint
pt(m_x
, m_y
);
4587 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4589 if (IsVisible() && m_flashOn
)
4590 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4593 void wxRichTextCaret::Notify()
4595 m_flashOn
= !m_flashOn
;
4599 void wxRichTextCaretTimer::Notify()
4604 // wxRICHTEXT_USE_OWN_CARET
4607 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4611 m_labels
.Add(label
);
4619 // Returns number of menu items were added.
4620 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4622 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4623 // If none of the standard properties identifiers are in the menu, add them if necessary.
4624 // If no items to add, just set the text to something generic
4625 if (GetCount() == 0)
4629 menu
->SetLabel(startCmd
, _("&Properties"));
4631 // Delete the others if necessary
4633 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4635 if (menu
->FindItem(i
))
4646 // Find the position of the first properties item
4647 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4649 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4650 if (item
&& item
->GetId() == startCmd
)
4659 int insertBefore
= pos
+1;
4660 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4662 if (menu
->FindItem(i
))
4664 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4668 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4669 menu
->Append(i
, m_labels
[i
- startCmd
]);
4671 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4676 // Delete any old items still left on the menu
4677 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4679 if (menu
->FindItem(i
))
4687 // No existing property identifiers were found, so append to the end of the menu.
4688 menu
->AppendSeparator();
4689 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4691 menu
->Append(i
, m_labels
[i
- startCmd
]);
4699 // Add appropriate menu items for the current container and clicked on object
4700 // (and container's parent, if appropriate).
4701 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl
* ctrl
, wxRichTextObject
* container
, wxRichTextObject
* obj
)
4704 if (obj
&& ctrl
->CanEditProperties(obj
))
4705 AddItem(ctrl
->GetPropertiesMenuLabel(obj
), obj
);
4707 if (container
&& container
!= obj
&& ctrl
->CanEditProperties(container
) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
)) == wxNOT_FOUND
)
4708 AddItem(ctrl
->GetPropertiesMenuLabel(container
), container
);
4710 if (container
&& container
->GetParent() && ctrl
->CanEditProperties(container
->GetParent()) && m_labels
.Index(ctrl
->GetPropertiesMenuLabel(container
->GetParent())) == wxNOT_FOUND
)
4711 AddItem(ctrl
->GetPropertiesMenuLabel(container
->GetParent()), container
->GetParent());