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 // DLL options compatibility check:
40 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
42 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
43 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
44 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
45 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
46 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN
, wxRichTextEvent
);
47 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER
, wxRichTextEvent
);
48 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE
, wxRichTextEvent
);
50 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
51 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
59 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
60 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
, wxRichTextEvent
);
62 #if wxRICHTEXT_USE_OWN_CARET
67 * This implements a non-flashing cursor in case there
68 * are platform-specific problems with the generic caret.
69 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
72 class wxRichTextCaret
;
73 class wxRichTextCaretTimer
: public wxTimer
76 wxRichTextCaretTimer(wxRichTextCaret
* caret
)
80 virtual void Notify();
81 wxRichTextCaret
* m_caret
;
84 class wxRichTextCaret
: public wxCaret
89 // default - use Create()
90 wxRichTextCaret(): m_timer(this) { Init(); }
91 // creates a block caret associated with the given window
92 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
93 : wxCaret(window
, width
, height
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
94 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
95 : wxCaret(window
, size
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
97 virtual ~wxRichTextCaret();
102 // called by wxWindow (not using the event tables)
103 virtual void OnSetFocus();
104 virtual void OnKillFocus();
106 // draw the caret on the given DC
107 void DoDraw(wxDC
*dc
);
109 // get the visible count
110 int GetVisibleCount() const { return m_countVisible
; }
112 // delay repositioning
113 bool GetNeedsUpdate() const { return m_needsUpdate
; }
114 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
119 virtual void DoShow();
120 virtual void DoHide();
121 virtual void DoMove();
122 virtual void DoSize();
132 bool m_hasFocus
; // true => our window has focus
133 bool m_needsUpdate
; // must be repositioned
135 wxRichTextCaretTimer m_timer
;
136 wxRichTextCtrl
* m_richTextCtrl
;
140 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxControl
)
142 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
144 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
145 EVT_PAINT(wxRichTextCtrl::OnPaint
)
146 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
147 EVT_IDLE(wxRichTextCtrl::OnIdle
)
148 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
149 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
150 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
151 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
152 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
153 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
154 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
155 EVT_CHAR(wxRichTextCtrl::OnChar
)
156 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
157 EVT_SIZE(wxRichTextCtrl::OnSize
)
158 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
159 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
160 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
161 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
162 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
164 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
165 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
167 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
168 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
170 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
171 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
173 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
174 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
176 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
177 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
179 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
180 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
182 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
183 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
185 EVT_MENU(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnProperties
)
186 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnUpdateProperties
)
188 EVT_MENU(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnProperties
)
189 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnUpdateProperties
)
191 EVT_MENU(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnProperties
)
192 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnUpdateProperties
)
200 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
202 wxRichTextCtrl::wxRichTextCtrl()
203 : wxScrollHelper(this)
208 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
210 const wxString
& value
,
214 const wxValidator
& validator
,
215 const wxString
& name
)
216 : wxScrollHelper(this)
219 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
223 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
224 const wxValidator
& validator
, const wxString
& name
)
228 if (!wxControl::Create(parent
, id
, pos
, size
,
229 style
|wxFULL_REPAINT_ON_RESIZE
,
235 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
238 // No physical scrolling, so we can preserve margins
239 EnableScrolling(false, false);
241 if (style
& wxTE_READONLY
)
244 // The base attributes must all have default values
245 wxRichTextAttr attributes
;
246 attributes
.SetFont(GetFont());
247 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
248 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
249 attributes
.SetLineSpacing(10);
250 attributes
.SetParagraphSpacingAfter(10);
251 attributes
.SetParagraphSpacingBefore(0);
253 SetBasicStyle(attributes
);
256 SetMargins(margin
, margin
);
258 // The default attributes will be merged with base attributes, so
259 // can be empty to begin with
260 wxRichTextAttr defaultAttributes
;
261 SetDefaultStyle(defaultAttributes
);
263 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
264 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
267 GetBuffer().SetRichTextCtrl(this);
269 #if wxRICHTEXT_USE_OWN_CARET
270 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
272 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
275 // Tell the sizers to use the given or best size
276 SetInitialSize(size
);
278 #if wxRICHTEXT_BUFFERED_PAINTING
280 RecreateBuffer(size
);
283 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
284 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
286 SetCursor(m_textCursor
);
288 if (!value
.IsEmpty())
291 GetBuffer().AddEventHandler(this);
294 wxAcceleratorEntry entries
[6];
296 entries
[0].Set(wxACCEL_CMD
, (int) 'C', wxID_COPY
);
297 entries
[1].Set(wxACCEL_CMD
, (int) 'X', wxID_CUT
);
298 entries
[2].Set(wxACCEL_CMD
, (int) 'V', wxID_PASTE
);
299 entries
[3].Set(wxACCEL_CMD
, (int) 'A', wxID_SELECTALL
);
300 entries
[4].Set(wxACCEL_CMD
, (int) 'Z', wxID_UNDO
);
301 entries
[5].Set(wxACCEL_CMD
, (int) 'Y', wxID_REDO
);
303 wxAcceleratorTable
accel(6, entries
);
304 SetAcceleratorTable(accel
);
306 m_contextMenu
= new wxMenu
;
307 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
308 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
309 m_contextMenu
->AppendSeparator();
310 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
311 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
312 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
313 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
314 m_contextMenu
->AppendSeparator();
315 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
316 m_contextMenu
->AppendSeparator();
317 m_contextMenu
->Append(wxID_RICHTEXT_PROPERTIES1
, _("&Properties"));
322 wxRichTextCtrl::~wxRichTextCtrl()
324 SetFocusObject(& GetBuffer(), false);
325 GetBuffer().RemoveEventHandler(this);
327 delete m_contextMenu
;
330 /// Member initialisation
331 void wxRichTextCtrl::Init()
333 m_contextMenu
= NULL
;
335 m_caretPosition
= -1;
336 m_selectionAnchor
= -2;
337 m_selectionAnchorObject
= NULL
;
338 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
340 m_caretAtLineStart
= false;
342 m_fullLayoutRequired
= false;
343 m_fullLayoutTime
= 0;
344 m_fullLayoutSavedPosition
= 0;
345 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
346 m_caretPositionForDefaultStyle
= -2;
347 m_focusObject
= & m_buffer
;
350 void wxRichTextCtrl::DoThaw()
352 if (GetBuffer().IsDirty())
361 void wxRichTextCtrl::Clear()
363 if (GetFocusObject() == & GetBuffer())
365 m_buffer
.ResetAndClearCommands();
366 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
370 GetFocusObject()->Reset();
373 m_caretPosition
= -1;
374 m_caretPositionForDefaultStyle
= -2;
375 m_caretAtLineStart
= false;
377 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
387 wxTextCtrl::SendTextUpdatedEvent(this);
391 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
393 #if !wxRICHTEXT_USE_OWN_CARET
394 if (GetCaret() && !IsFrozen())
399 #if wxRICHTEXT_BUFFERED_PAINTING
400 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
410 dc
.SetFont(GetFont());
412 // Paint the background
415 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
417 wxRect
drawingArea(GetUpdateRegion().GetBox());
418 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
420 wxRect
availableSpace(GetClientSize());
421 if (GetBuffer().IsDirty())
423 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
424 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
428 wxRect
clipRect(availableSpace
);
429 clipRect
.x
+= GetBuffer().GetLeftMargin();
430 clipRect
.y
+= GetBuffer().GetTopMargin();
431 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
432 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
433 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
434 dc
.SetClippingRegion(clipRect
);
437 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
438 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
440 GetBuffer().Draw(dc
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
442 dc
.DestroyClippingRegion();
444 // Other user defined painting after everything else (i.e. all text) is painted
445 PaintAboveContent(dc
);
447 #if wxRICHTEXT_USE_OWN_CARET
448 if (GetCaret()->IsVisible())
450 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
455 #if !wxRICHTEXT_USE_OWN_CARET
462 // Empty implementation, to prevent flicker
463 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
467 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
471 #if !wxRICHTEXT_USE_OWN_CARET
477 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
478 // Work around dropouts when control is focused
486 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
491 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
492 // Work around dropouts when control is focused
500 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
505 // Set up the caret for the given position and container, after a mouse click
506 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
508 bool caretAtLineStart
= false;
510 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
512 // If we're at the start of a line (but not first in para)
513 // then we should keep the caret showing at the start of the line
514 // by showing the m_caretAtLineStart flag.
515 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
516 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
518 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
519 caretAtLineStart
= true;
523 if (extendSelection
&& (m_caretPosition
!= position
))
524 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
526 MoveCaret(position
, caretAtLineStart
);
527 SetDefaultStyleToCursorStyle();
533 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
539 dc
.SetFont(GetFont());
541 // TODO: detect change of focus object
543 wxRichTextObject
* hitObj
= NULL
;
544 wxRichTextObject
* contextObj
= NULL
;
545 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
);
547 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
549 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
550 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
551 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
553 SetFocusObject(container
, false /* don't set caret position yet */);
556 m_dragStart
= event
.GetLogicalPosition(dc
);
560 long oldCaretPos
= m_caretPosition
;
562 SetCaretPositionAfterClick(container
, position
, hit
);
564 // For now, don't handle shift-click when we're selecting multiple objects.
565 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
567 if (!m_selection
.IsValid())
568 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
570 ExtendSelection(m_caretPosition
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
580 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
585 if (GetCapture() == this)
588 // See if we clicked on a URL
591 dc
.SetFont(GetFont());
594 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
595 wxRichTextObject
* hitObj
= NULL
;
596 wxRichTextObject
* contextObj
= NULL
;
597 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
598 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
600 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
602 wxRichTextEvent
cmdEvent(
603 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
605 cmdEvent
.SetEventObject(this);
606 cmdEvent
.SetPosition(position
);
608 cmdEvent
.SetContainer(hitObj
->GetContainer());
610 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
613 if (GetStyle(position
, attr
))
615 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
617 wxString urlTarget
= attr
.GetURL();
618 if (!urlTarget
.IsEmpty())
620 wxMouseEvent
mouseEvent(event
);
622 long startPos
= 0, endPos
= 0;
623 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
626 startPos
= obj
->GetRange().GetStart();
627 endPos
= obj
->GetRange().GetEnd();
630 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
631 InitCommandEvent(urlEvent
);
633 urlEvent
.SetString(urlTarget
);
635 GetEventHandler()->ProcessEvent(urlEvent
);
645 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
649 dc
.SetFont(GetFont());
652 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
653 wxRichTextObject
* hitObj
= NULL
;
654 wxRichTextObject
* contextObj
= NULL
;
658 // If we're dragging, let's only consider positions at this level; otherwise
659 // selecting a range is not going to work.
660 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
663 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
664 container
= GetFocusObject();
666 int hit
= container
->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
, flags
);
668 // See if we need to change the cursor
671 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
673 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
675 if (actualContainer
&& GetStyle(position
, attr
, actualContainer
))
677 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
679 SetCursor(m_urlCursor
);
681 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
683 SetCursor(m_textCursor
);
688 SetCursor(m_textCursor
);
691 if (!event
.Dragging())
699 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
700 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
701 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
703 // Check for dragging across multiple containers
705 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
706 int hit2
= GetBuffer().HitTest(dc
, logicalPt
, position2
, & hitObj2
, & contextObj2
, 0);
707 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
709 // See if we can find a common ancestor
710 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
712 firstContainer
= GetFocusObject();
713 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
717 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
718 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
719 // is the common ancestor.
720 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
723 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
725 wxRichTextObject
* p
= hitObj2
;
728 if (p
->GetParent() == commonAncestor
)
730 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
737 if (commonAncestor
&& firstContainer
&& otherContainer
)
739 // We have now got a second container that shares a parent with the current or anchor object.
740 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
742 // Don't go into common-ancestor selection mode if we still have the same
744 if (otherContainer
!= firstContainer
)
746 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
747 m_selectionAnchorObject
= firstContainer
;
748 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
750 // The common ancestor, such as a table, returns the cell selection
751 // between the anchor and current position.
752 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
757 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
762 if (otherContainer
->AcceptsFocus())
763 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
764 MoveCaret(-1, false);
765 SetDefaultStyleToCursorStyle();
770 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
772 // TODO: test closeness
773 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
778 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
784 dc
.SetFont(GetFont());
787 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
788 wxRichTextObject
* hitObj
= NULL
;
789 wxRichTextObject
* contextObj
= NULL
;
790 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
);
792 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
794 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
795 if (actualContainer
&& actualContainer
->AcceptsFocus())
797 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
798 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
802 wxRichTextEvent
cmdEvent(
803 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
805 cmdEvent
.SetEventObject(this);
806 cmdEvent
.SetPosition(position
);
808 cmdEvent
.SetContainer(hitObj
->GetContainer());
810 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
814 /// Left-double-click
815 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
817 wxRichTextEvent
cmdEvent(
818 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
820 cmdEvent
.SetEventObject(this);
821 cmdEvent
.SetPosition(m_caretPosition
+1);
822 cmdEvent
.SetContainer(GetFocusObject());
824 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
826 SelectWord(GetCaretPosition()+1);
831 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
833 wxRichTextEvent
cmdEvent(
834 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
836 cmdEvent
.SetEventObject(this);
837 cmdEvent
.SetPosition(m_caretPosition
+1);
838 cmdEvent
.SetContainer(GetFocusObject());
840 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
845 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
849 flags
|= wxRICHTEXT_CTRL_DOWN
;
850 if (event
.ShiftDown())
851 flags
|= wxRICHTEXT_SHIFT_DOWN
;
853 flags
|= wxRICHTEXT_ALT_DOWN
;
855 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
857 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
859 KeyboardNavigate(event
.GetKeyCode(), flags
);
863 long keycode
= event
.GetKeyCode();
923 case WXK_NUMPAD_HOME
:
924 case WXK_NUMPAD_LEFT
:
926 case WXK_NUMPAD_RIGHT
:
927 case WXK_NUMPAD_DOWN
:
928 case WXK_NUMPAD_PAGEUP
:
929 case WXK_NUMPAD_PAGEDOWN
:
931 case WXK_NUMPAD_BEGIN
:
932 case WXK_NUMPAD_INSERT
:
933 case WXK_WINDOWS_LEFT
:
942 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
943 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
945 BeginBatchUndo(_("Delete Text"));
947 long newPos
= m_caretPosition
;
949 bool processed
= DeleteSelectedContent(& newPos
);
951 // Submit range in character positions, which are greater than caret positions,
952 // so subtract 1 for deleted character and add 1 for conversion to character position.
957 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
960 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
966 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
971 if (GetLastPosition() == -1)
973 GetFocusObject()->Reset();
975 m_caretPosition
= -1;
977 SetDefaultStyleToCursorStyle();
980 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
982 wxRichTextEvent
cmdEvent(
983 wxEVT_COMMAND_RICHTEXT_DELETE
,
985 cmdEvent
.SetEventObject(this);
986 cmdEvent
.SetFlags(flags
);
987 cmdEvent
.SetPosition(m_caretPosition
+1);
988 cmdEvent
.SetContainer(GetFocusObject());
989 GetEventHandler()->ProcessEvent(cmdEvent
);
999 // all the other keys modify the controls contents which shouldn't be
1000 // possible if we're read-only
1001 if ( !IsEditable() )
1007 if (event
.GetKeyCode() == WXK_RETURN
)
1009 BeginBatchUndo(_("Insert Text"));
1011 long newPos
= m_caretPosition
;
1013 DeleteSelectedContent(& newPos
);
1015 if (event
.ShiftDown())
1018 text
= wxRichTextLineBreakChar
;
1019 GetFocusObject()->InsertTextWithUndo(newPos
+1, text
, this, & GetBuffer());
1020 m_caretAtLineStart
= true;
1024 GetFocusObject()->InsertNewlineWithUndo(newPos
+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1027 SetDefaultStyleToCursorStyle();
1029 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1031 wxRichTextEvent
cmdEvent(
1032 wxEVT_COMMAND_RICHTEXT_RETURN
,
1034 cmdEvent
.SetEventObject(this);
1035 cmdEvent
.SetFlags(flags
);
1036 cmdEvent
.SetPosition(newPos
+1);
1037 cmdEvent
.SetContainer(GetFocusObject());
1039 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1041 // Generate conventional event
1042 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1043 InitCommandEvent(textEvent
);
1045 GetEventHandler()->ProcessEvent(textEvent
);
1049 else if (event
.GetKeyCode() == WXK_BACK
)
1051 BeginBatchUndo(_("Delete Text"));
1053 long newPos
= m_caretPosition
;
1055 bool processed
= DeleteSelectedContent(& newPos
);
1057 // Submit range in character positions, which are greater than caret positions,
1058 // so subtract 1 for deleted character and add 1 for conversion to character position.
1061 if (event
.CmdDown())
1063 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1066 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
1072 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
1077 if (GetLastPosition() == -1)
1079 GetFocusObject()->Reset();
1081 m_caretPosition
= -1;
1083 SetDefaultStyleToCursorStyle();
1086 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1088 wxRichTextEvent
cmdEvent(
1089 wxEVT_COMMAND_RICHTEXT_DELETE
,
1091 cmdEvent
.SetEventObject(this);
1092 cmdEvent
.SetFlags(flags
);
1093 cmdEvent
.SetPosition(m_caretPosition
+1);
1094 cmdEvent
.SetContainer(GetFocusObject());
1095 GetEventHandler()->ProcessEvent(cmdEvent
);
1099 else if (event
.GetKeyCode() == WXK_DELETE
)
1101 BeginBatchUndo(_("Delete Text"));
1103 long newPos
= m_caretPosition
;
1105 bool processed
= DeleteSelectedContent(& newPos
);
1107 // Submit range in character positions, which are greater than caret positions,
1108 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1110 if (event
.CmdDown())
1112 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1113 if (pos
!= -1 && (pos
> newPos
))
1115 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this, & GetBuffer());
1120 if (!processed
&& newPos
< (GetLastPosition()-1))
1121 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this, & GetBuffer());
1126 if (GetLastPosition() == -1)
1128 GetFocusObject()->Reset();
1130 m_caretPosition
= -1;
1132 SetDefaultStyleToCursorStyle();
1135 wxRichTextEvent
cmdEvent(
1136 wxEVT_COMMAND_RICHTEXT_DELETE
,
1138 cmdEvent
.SetEventObject(this);
1139 cmdEvent
.SetFlags(flags
);
1140 cmdEvent
.SetPosition(m_caretPosition
+1);
1141 cmdEvent
.SetContainer(GetFocusObject());
1142 GetEventHandler()->ProcessEvent(cmdEvent
);
1148 long keycode
= event
.GetKeyCode();
1160 if (event
.CmdDown())
1162 // Fixes AltGr+key with European input languages on Windows
1163 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1170 wxRichTextEvent
cmdEvent(
1171 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1173 cmdEvent
.SetEventObject(this);
1174 cmdEvent
.SetFlags(flags
);
1176 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1178 cmdEvent
.SetCharacter((wxChar
) keycode
);
1180 cmdEvent
.SetPosition(m_caretPosition
+1);
1181 cmdEvent
.SetContainer(GetFocusObject());
1183 if (keycode
== wxT('\t'))
1185 // See if we need to promote or demote the selection or paragraph at the cursor
1186 // position, instead of inserting a tab.
1187 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1188 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1189 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1191 wxRichTextRange range
;
1193 range
= GetSelectionRange();
1195 range
= para
->GetRange().FromInternal();
1197 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1199 PromoteList(promoteBy
, range
, NULL
);
1201 GetEventHandler()->ProcessEvent(cmdEvent
);
1207 BeginBatchUndo(_("Insert Text"));
1209 long newPos
= m_caretPosition
;
1210 DeleteSelectedContent(& newPos
);
1213 wxString str
= event
.GetUnicodeKey();
1215 wxString str
= (wxChar
) event
.GetKeyCode();
1217 GetFocusObject()->InsertTextWithUndo(newPos
+1, str
, this, & GetBuffer(), 0);
1221 SetDefaultStyleToCursorStyle();
1222 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1224 cmdEvent
.SetPosition(m_caretPosition
);
1225 GetEventHandler()->ProcessEvent(cmdEvent
);
1233 /// Delete content if there is a selection, e.g. when pressing a key.
1234 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1238 long pos
= m_selection
.GetRange().GetStart();
1239 wxRichTextRange range
= m_selection
.GetRange();
1241 // SelectAll causes more to be selected than doing it interactively,
1242 // and causes a new paragraph to be inserted. So for multiline buffers,
1243 // don't delete the final position.
1244 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1245 range
.SetEnd(range
.GetEnd()-1);
1247 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1248 m_selection
.Reset();
1249 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1259 /// Keyboard navigation
1263 Left: left one character
1264 Right: right one character
1267 Ctrl-Left: left one word
1268 Ctrl-Right: right one word
1269 Ctrl-Up: previous paragraph start
1270 Ctrl-Down: next start of paragraph
1273 Ctrl-Home: start of document
1274 Ctrl-End: end of document
1275 Page-Up: Up a screen
1276 Page-Down: Down a screen
1280 Ctrl-Alt-PgUp: Start of window
1281 Ctrl-Alt-PgDn: End of window
1282 F8: Start selection mode
1283 Esc: End selection mode
1285 Adding Shift does the above but starts/extends selection.
1290 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1292 bool success
= false;
1294 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1296 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1297 success
= WordRight(1, flags
);
1299 success
= MoveRight(1, flags
);
1301 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1303 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1304 success
= WordLeft(1, flags
);
1306 success
= MoveLeft(1, flags
);
1308 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1310 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1311 success
= MoveToParagraphStart(flags
);
1313 success
= MoveUp(1, flags
);
1315 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1317 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1318 success
= MoveToParagraphEnd(flags
);
1320 success
= MoveDown(1, flags
);
1322 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1324 success
= PageUp(1, flags
);
1326 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1328 success
= PageDown(1, flags
);
1330 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1332 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1333 success
= MoveHome(flags
);
1335 success
= MoveToLineStart(flags
);
1337 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1339 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1340 success
= MoveEnd(flags
);
1342 success
= MoveToLineEnd(flags
);
1347 ScrollIntoView(m_caretPosition
, keyCode
);
1348 SetDefaultStyleToCursorStyle();
1354 /// Extend the selection. Selections are in caret positions.
1355 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1357 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1359 if (oldPos
== newPos
)
1362 wxRichTextSelection oldSelection
= m_selection
;
1364 m_selection
.SetContainer(GetFocusObject());
1366 wxRichTextRange oldRange
;
1367 if (m_selection
.IsValid())
1368 oldRange
= m_selection
.GetRange();
1370 oldRange
= wxRICHTEXT_NO_SELECTION
;
1371 wxRichTextRange newRange
;
1373 // If not currently selecting, start selecting
1374 if (oldRange
.GetStart() == -2)
1376 m_selectionAnchor
= oldPos
;
1378 if (oldPos
> newPos
)
1379 newRange
.SetRange(newPos
+1, oldPos
);
1381 newRange
.SetRange(oldPos
+1, newPos
);
1385 // Always ensure that the selection range start is greater than
1387 if (newPos
> m_selectionAnchor
)
1388 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1389 else if (newPos
== m_selectionAnchor
)
1390 newRange
= wxRichTextRange(-2, -2);
1392 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1395 m_selection
.SetRange(newRange
);
1397 RefreshForSelectionChange(oldSelection
, m_selection
);
1399 if (newRange
.GetStart() > newRange
.GetEnd())
1401 wxLogDebug(wxT("Strange selection range"));
1410 /// Scroll into view, returning true if we scrolled.
1411 /// This takes a _caret_ position.
1412 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1414 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1420 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1422 int startXUnits
, startYUnits
;
1423 GetViewStart(& startXUnits
, & startYUnits
);
1424 int startY
= startYUnits
* ppuY
;
1427 GetVirtualSize(& sx
, & sy
);
1433 wxRect rect
= line
->GetRect();
1435 bool scrolled
= false;
1437 wxSize clientSize
= GetClientSize();
1439 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1442 wxClientDC
dc(this);
1443 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1444 topMargin
, bottomMargin
);
1446 // clientSize.y -= GetBuffer().GetBottomMargin();
1447 clientSize
.y
-= bottomMargin
;
1449 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1451 int y
= rect
.y
- GetClientSize().y
/2;
1452 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1453 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1455 if (startYUnits
!= yUnits
)
1457 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1460 #if !wxRICHTEXT_USE_OWN_CARET
1470 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1471 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1472 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1473 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1475 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1477 // Make it scroll so this item is at the bottom
1479 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1480 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1482 // If we're still off the screen, scroll another line down
1483 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1486 if (startYUnits
!= yUnits
)
1488 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1492 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1494 // Make it scroll so this item is at the top
1496 int y
= rect
.y
- GetBuffer().GetTopMargin();
1497 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1499 if (startYUnits
!= yUnits
)
1501 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1507 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1508 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1509 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1510 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1512 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1514 // Make it scroll so this item is at the top
1516 int y
= rect
.y
- GetBuffer().GetTopMargin();
1517 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1519 if (startYUnits
!= yUnits
)
1521 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1525 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1527 // Make it scroll so this item is at the bottom
1529 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1530 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1532 // If we're still off the screen, scroll another line down
1533 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1536 if (startYUnits
!= yUnits
)
1538 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1544 #if !wxRICHTEXT_USE_OWN_CARET
1552 /// Is the given position visible on the screen?
1553 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1555 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1561 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1564 GetViewStart(& startX
, & startY
);
1566 startY
= startY
* ppuY
;
1568 wxRect rect
= line
->GetRect();
1569 wxSize clientSize
= GetClientSize();
1570 clientSize
.y
-= GetBuffer().GetBottomMargin();
1572 return (rect
.GetTop() >= (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1575 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1577 m_caretPosition
= position
;
1578 m_caretAtLineStart
= showAtLineStart
;
1581 /// Move caret one visual step forward: this may mean setting a flag
1582 /// and keeping the same position if we're going from the end of one line
1583 /// to the start of the next, which may be the exact same caret position.
1584 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1586 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1588 // Only do the check if we're not at the end of the paragraph (where things work OK
1590 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1592 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1596 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1598 // We're at the end of a line. See whether we need to
1599 // stay at the same actual caret position but change visual
1600 // position, or not.
1601 if (oldPosition
== lineRange
.GetEnd())
1603 if (m_caretAtLineStart
)
1605 // We're already at the start of the line, so actually move on now.
1606 m_caretPosition
= oldPosition
+ 1;
1607 m_caretAtLineStart
= false;
1611 // We're showing at the end of the line, so keep to
1612 // the same position but indicate that we're to show
1613 // at the start of the next line.
1614 m_caretPosition
= oldPosition
;
1615 m_caretAtLineStart
= true;
1617 SetDefaultStyleToCursorStyle();
1623 SetDefaultStyleToCursorStyle();
1626 /// Move caret one visual step backward: this may mean setting a flag
1627 /// and keeping the same position if we're going from the end of one line
1628 /// to the start of the next, which may be the exact same caret position.
1629 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1631 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1633 // Only do the check if we're not at the start of the paragraph (where things work OK
1635 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1637 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1641 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1643 // We're at the start of a line. See whether we need to
1644 // stay at the same actual caret position but change visual
1645 // position, or not.
1646 if (oldPosition
== lineRange
.GetStart())
1648 m_caretPosition
= oldPosition
-1;
1649 m_caretAtLineStart
= true;
1652 else if (oldPosition
== lineRange
.GetEnd())
1654 if (m_caretAtLineStart
)
1656 // We're at the start of the line, so keep the same caret position
1657 // but clear the start-of-line flag.
1658 m_caretPosition
= oldPosition
;
1659 m_caretAtLineStart
= false;
1663 // We're showing at the end of the line, so go back
1664 // to the previous character position.
1665 m_caretPosition
= oldPosition
- 1;
1667 SetDefaultStyleToCursorStyle();
1673 SetDefaultStyleToCursorStyle();
1677 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1679 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1681 if (m_caretPosition
+ noPositions
< endPos
)
1683 long oldPos
= m_caretPosition
;
1684 long newPos
= m_caretPosition
+ noPositions
;
1686 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1690 // Determine by looking at oldPos and m_caretPosition whether
1691 // we moved from the end of a line to the start of the next line, in which case
1692 // we want to adjust the caret position such that it is positioned at the
1693 // start of the next line, rather than jumping past the first character of the
1695 if (noPositions
== 1 && !extendSel
)
1696 MoveCaretForward(oldPos
);
1698 SetCaretPosition(newPos
);
1701 SetDefaultStyleToCursorStyle();
1710 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1714 if (m_caretPosition
> startPos
- noPositions
+ 1)
1716 long oldPos
= m_caretPosition
;
1717 long newPos
= m_caretPosition
- noPositions
;
1718 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1722 if (noPositions
== 1 && !extendSel
)
1723 MoveCaretBack(oldPos
);
1725 SetCaretPosition(newPos
);
1728 SetDefaultStyleToCursorStyle();
1736 // Find the caret position for the combination of hit-test flags and character position.
1737 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
1738 // since this is ambiguous (same position used for end of line and start of next).
1739 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
1740 bool& caretLineStart
)
1742 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1743 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1744 // so we view the caret at the start of the line.
1745 caretLineStart
= false;
1746 long caretPosition
= position
;
1748 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
1750 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
1751 wxRichTextRange lineRange
;
1753 lineRange
= thisLine
->GetAbsoluteRange();
1755 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
1758 caretLineStart
= true;
1762 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
1763 if (para
&& para
->GetRange().GetStart() == position
)
1767 return caretPosition
;
1771 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1773 return MoveDown(- noLines
, flags
);
1777 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1782 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1783 wxPoint pt
= GetCaret()->GetPosition();
1784 long newLine
= lineNumber
+ noLines
;
1785 bool notInThisObject
= false;
1787 if (lineNumber
!= -1)
1791 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
1792 if (newLine
> lastLine
)
1793 notInThisObject
= true;
1798 notInThisObject
= true;
1802 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
1803 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
;
1805 if (notInThisObject
)
1807 // If we know we're navigating out of the current object,
1808 // try to find an object anywhere in the buffer at the new position (up or down a bit)
1809 container
= & GetBuffer();
1810 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
1812 if (noLines
> 0) // going down
1814 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
1818 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
1823 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
1825 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1831 wxClientDC
dc(this);
1833 dc
.SetFont(GetFont());
1835 wxRichTextObject
* hitObj
= NULL
;
1836 wxRichTextObject
* contextObj
= NULL
;
1837 int hitTest
= container
->HitTest(dc
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
1840 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
1841 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
1844 if (notInThisObject
)
1846 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
1847 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
1849 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
1851 container
= actualContainer
;
1855 bool caretLineStart
= true;
1856 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
1857 long newSelEnd
= caretPosition
;
1860 if (notInThisObject
)
1863 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1868 SetCaretPosition(caretPosition
, caretLineStart
);
1870 SetDefaultStyleToCursorStyle();
1878 /// Move to the end of the paragraph
1879 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1881 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1884 long newPos
= para
->GetRange().GetEnd() - 1;
1885 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1889 SetCaretPosition(newPos
);
1891 SetDefaultStyleToCursorStyle();
1899 /// Move to the start of the paragraph
1900 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1902 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1905 long newPos
= para
->GetRange().GetStart() - 1;
1906 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1910 SetCaretPosition(newPos
);
1912 SetDefaultStyleToCursorStyle();
1920 /// Move to the end of the line
1921 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1923 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1927 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1928 long newPos
= lineRange
.GetEnd();
1929 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1933 SetCaretPosition(newPos
);
1935 SetDefaultStyleToCursorStyle();
1943 /// Move to the start of the line
1944 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1946 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1949 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1950 long newPos
= lineRange
.GetStart()-1;
1952 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1956 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
1958 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1960 SetDefaultStyleToCursorStyle();
1968 /// Move to the start of the buffer
1969 bool wxRichTextCtrl::MoveHome(int flags
)
1971 if (m_caretPosition
!= -1)
1973 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
1977 SetCaretPosition(-1);
1979 SetDefaultStyleToCursorStyle();
1987 /// Move to the end of the buffer
1988 bool wxRichTextCtrl::MoveEnd(int flags
)
1990 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
1992 if (m_caretPosition
!= endPos
)
1994 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
1998 SetCaretPosition(endPos
);
2000 SetDefaultStyleToCursorStyle();
2008 /// Move noPages pages up
2009 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2011 return PageDown(- noPages
, flags
);
2014 /// Move noPages pages down
2015 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2017 // Calculate which line occurs noPages * screen height further down.
2018 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2021 wxSize clientSize
= GetClientSize();
2022 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2024 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2027 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2028 long pos
= lineRange
.GetStart()-1;
2029 if (pos
!= m_caretPosition
)
2031 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2033 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2037 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2039 SetDefaultStyleToCursorStyle();
2049 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2051 return str
== wxT(" ") || str
== wxT("\t");
2054 // Finds the caret position for the next word
2055 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2057 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2061 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2063 // First skip current text to space
2064 while (i
< endPos
&& i
> -1)
2066 // i is in character, not caret positions
2067 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2068 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2069 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2073 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2080 while (i
< endPos
&& i
> -1)
2082 // i is in character, not caret positions
2083 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2084 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2085 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2086 return wxMax(-1, i
);
2088 if (text
.empty()) // End of paragraph, or maybe an image
2089 return wxMax(-1, i
- 1);
2090 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2094 // Convert to caret position
2095 return wxMax(-1, i
- 1);
2104 long i
= m_caretPosition
;
2106 // First skip white space
2107 while (i
< endPos
&& i
> -1)
2109 // i is in character, not caret positions
2110 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2111 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2113 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2115 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2120 // Next skip current text to space
2121 while (i
< endPos
&& i
> -1)
2123 // i is in character, not caret positions
2124 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2125 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2126 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2129 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2142 /// Move n words left
2143 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2145 long pos
= FindNextWordPosition(-1);
2146 if (pos
!= m_caretPosition
)
2148 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2150 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2154 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2156 SetDefaultStyleToCursorStyle();
2164 /// Move n words right
2165 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2167 long pos
= FindNextWordPosition(1);
2168 if (pos
!= m_caretPosition
)
2170 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2172 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2176 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2178 SetDefaultStyleToCursorStyle();
2187 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2189 // Only do sizing optimization for large buffers
2190 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2192 m_fullLayoutRequired
= true;
2193 m_fullLayoutTime
= wxGetLocalTimeMillis();
2194 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2195 LayoutContent(true /* onlyVisibleRect */);
2198 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2200 #if wxRICHTEXT_BUFFERED_PAINTING
2208 /// Idle-time processing
2209 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2211 #if wxRICHTEXT_USE_OWN_CARET
2212 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2214 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2220 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2222 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2224 m_fullLayoutRequired
= false;
2225 m_fullLayoutTime
= 0;
2226 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2227 ShowPosition(m_fullLayoutSavedPosition
);
2231 if (m_caretPositionForDefaultStyle
!= -2)
2233 // If the caret position has changed, no longer reflect the default style
2235 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2236 m_caretPositionForDefaultStyle
= -2;
2243 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2245 #if wxRICHTEXT_USE_OWN_CARET
2246 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2249 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2256 /// Set up scrollbars, e.g. after a resize
2257 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2262 if (GetBuffer().IsEmpty())
2264 SetScrollbars(0, 0, 0, 0, 0, 0);
2268 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2269 // of pixels. See e.g. wxVScrolledWindow for ideas.
2270 int pixelsPerUnit
= 5;
2271 wxSize clientSize
= GetClientSize();
2273 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2275 // Round up so we have at least maxHeight pixels
2276 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2278 int startX
= 0, startY
= 0;
2280 GetViewStart(& startX
, & startY
);
2282 int maxPositionX
= 0;
2283 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2285 int newStartX
= wxMin(maxPositionX
, startX
);
2286 int newStartY
= wxMin(maxPositionY
, startY
);
2288 int oldPPUX
, oldPPUY
;
2289 int oldStartX
, oldStartY
;
2290 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2291 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2292 GetViewStart(& oldStartX
, & oldStartY
);
2293 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2295 oldVirtualSizeY
/= oldPPUY
;
2297 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2300 // Don't set scrollbars if there were none before, and there will be none now.
2301 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2304 // Move to previous scroll position if
2306 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2309 /// Paint the background
2310 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2312 wxColour backgroundColour
= GetBackgroundColour();
2313 if (!backgroundColour
.Ok())
2314 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2316 // Clear the background
2317 dc
.SetBrush(wxBrush(backgroundColour
));
2318 dc
.SetPen(*wxTRANSPARENT_PEN
);
2319 wxRect
windowRect(GetClientSize());
2320 windowRect
.x
-= 2; windowRect
.y
-= 2;
2321 windowRect
.width
+= 4; windowRect
.height
+= 4;
2323 // We need to shift the rectangle to take into account
2324 // scrolling. Converting device to logical coordinates.
2325 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2326 dc
.DrawRectangle(windowRect
);
2329 #if wxRICHTEXT_BUFFERED_PAINTING
2330 /// Recreate buffer bitmap if necessary
2331 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2334 if (sz
== wxDefaultSize
)
2335 sz
= GetClientSize();
2337 if (sz
.x
< 1 || sz
.y
< 1)
2340 if (!m_bufferBitmap
.Ok() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2341 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2342 return m_bufferBitmap
.Ok();
2346 // ----------------------------------------------------------------------------
2347 // file IO functions
2348 // ----------------------------------------------------------------------------
2350 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2352 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2354 m_filename
= filename
;
2357 SetInsertionPoint(0);
2360 SetupScrollbars(true);
2362 wxTextCtrl::SendTextUpdatedEvent(this);
2368 wxLogError(_("File couldn't be loaded."));
2374 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2376 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2378 m_filename
= filename
;
2385 wxLogError(_("The text couldn't be saved."));
2390 // ----------------------------------------------------------------------------
2391 // wxRichTextCtrl specific functionality
2392 // ----------------------------------------------------------------------------
2394 /// Add a new paragraph of text to the end of the buffer
2395 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2397 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2398 GetBuffer().Invalidate();
2404 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2406 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2407 GetBuffer().Invalidate();
2412 // ----------------------------------------------------------------------------
2413 // selection and ranges
2414 // ----------------------------------------------------------------------------
2416 void wxRichTextCtrl::SelectAll()
2418 SetSelection(-1, -1);
2422 void wxRichTextCtrl::SelectNone()
2424 if (m_selection
.IsValid())
2426 wxRichTextSelection oldSelection
= m_selection
;
2428 m_selection
.Reset();
2430 RefreshForSelectionChange(oldSelection
, m_selection
);
2432 m_selectionAnchor
= -2;
2433 m_selectionAnchorObject
= NULL
;
2434 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2437 static bool wxIsWordDelimiter(const wxString
& text
)
2439 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2442 /// Select the word at the given character position
2443 bool wxRichTextCtrl::SelectWord(long position
)
2445 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2448 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2452 if (position
== para
->GetRange().GetEnd())
2455 long positionStart
= position
;
2456 long positionEnd
= position
;
2458 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2460 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2461 if (wxIsWordDelimiter(text
))
2467 if (positionStart
< para
->GetRange().GetStart())
2468 positionStart
= para
->GetRange().GetStart();
2470 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2472 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2473 if (wxIsWordDelimiter(text
))
2479 if (positionEnd
>= para
->GetRange().GetEnd())
2480 positionEnd
= para
->GetRange().GetEnd();
2482 if (positionEnd
< positionStart
)
2485 SetSelection(positionStart
, positionEnd
+1);
2487 if (positionStart
>= 0)
2489 MoveCaret(positionStart
-1, true);
2490 SetDefaultStyleToCursorStyle();
2496 wxString
wxRichTextCtrl::GetStringSelection() const
2499 GetSelection(&from
, &to
);
2501 return GetRange(from
, to
);
2504 // ----------------------------------------------------------------------------
2506 // ----------------------------------------------------------------------------
2508 wxTextCtrlHitTestResult
2509 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2511 // implement in terms of the other overload as the native ports typically
2512 // can get the position and not (x, y) pair directly (although wxUniv
2513 // directly gets x and y -- and so overrides this method as well)
2515 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2517 if ( rc
!= wxTE_HT_UNKNOWN
)
2519 PositionToXY(pos
, x
, y
);
2525 wxTextCtrlHitTestResult
2526 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2529 wxClientDC
dc((wxRichTextCtrl
*) this);
2530 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2532 // Buffer uses logical position (relative to start of buffer)
2534 wxPoint pt2
= GetLogicalPoint(pt
);
2536 wxRichTextObject
* hitObj
= NULL
;
2537 wxRichTextObject
* contextObj
= NULL
;
2538 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2540 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2541 return wxTE_HT_BEFORE
;
2542 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2543 return wxTE_HT_BEYOND
;
2544 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2545 return wxTE_HT_ON_TEXT
;
2547 return wxTE_HT_UNKNOWN
;
2550 // ----------------------------------------------------------------------------
2551 // set/get the controls text
2552 // ----------------------------------------------------------------------------
2554 wxString
wxRichTextCtrl::DoGetValue() const
2556 return GetBuffer().GetText();
2559 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2561 // Public API for range is different from internals
2562 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2565 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2567 // Don't call Clear here, since it always sends a text updated event
2568 m_buffer
.ResetAndClearCommands();
2569 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2570 m_caretPosition
= -1;
2571 m_caretPositionForDefaultStyle
= -2;
2572 m_caretAtLineStart
= false;
2573 m_selection
.Reset();
2574 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2584 if (!value
.IsEmpty())
2586 // Remove empty paragraph
2587 GetBuffer().Clear();
2588 DoWriteText(value
, flags
);
2590 // for compatibility, don't move the cursor when doing SetValue()
2591 SetInsertionPoint(0);
2595 // still send an event for consistency
2596 if (flags
& SetValue_SendEvent
)
2597 wxTextCtrl::SendTextUpdatedEvent(this);
2602 void wxRichTextCtrl::WriteText(const wxString
& value
)
2607 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2609 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2611 GetFocusObject()->InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2613 if ( flags
& SetValue_SendEvent
)
2614 wxTextCtrl::SendTextUpdatedEvent(this);
2617 void wxRichTextCtrl::AppendText(const wxString
& text
)
2619 SetInsertionPointEnd();
2624 /// Write an image at the current insertion point
2625 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2627 wxRichTextImageBlock imageBlock
;
2629 wxImage image2
= image
;
2630 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2631 return WriteImage(imageBlock
, textAttr
);
2636 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2638 wxRichTextImageBlock imageBlock
;
2641 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2642 return WriteImage(imageBlock
, textAttr
);
2647 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2649 return GetFocusObject()->InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this, & GetBuffer(), 0, textAttr
);
2652 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2656 wxRichTextImageBlock imageBlock
;
2658 wxImage image
= bitmap
.ConvertToImage();
2659 if (image
.Ok() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2660 return WriteImage(imageBlock
, textAttr
);
2666 // Write a text box at the current insertion point.
2667 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2669 wxRichTextBox
* textBox
= new wxRichTextBox
;
2670 textBox
->SetAttributes(textAttr
);
2671 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2672 textBox
->AddParagraph(wxEmptyString
);
2673 textBox
->SetParent(NULL
);
2675 // The object returned is the one actually inserted into the buffer,
2676 // while the original one is deleted.
2677 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(m_caretPosition
+1, textBox
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2678 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
2682 // Write a table at the current insertion point, returning the table.
2683 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
2685 wxASSERT(rows
> 0 && cols
> 0);
2687 if (rows
<= 0 || cols
<= 0)
2690 wxRichTextTable
* table
= new wxRichTextTable
;
2691 table
->SetAttributes(tableAttr
);
2692 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2694 table
->CreateTable(rows
, cols
);
2696 table
->SetParent(NULL
);
2699 for (j
= 0; j
< rows
; j
++)
2701 for (i
= 0; i
< cols
; i
++)
2703 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
2707 // The object returned is the one actually inserted into the buffer,
2708 // while the original one is deleted.
2709 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(m_caretPosition
+1, table
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2710 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
2715 /// Insert a newline (actually paragraph) at the current insertion point.
2716 bool wxRichTextCtrl::Newline()
2718 return GetFocusObject()->InsertNewlineWithUndo(m_caretPosition
+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2721 /// Insert a line break at the current insertion point.
2722 bool wxRichTextCtrl::LineBreak()
2725 text
= wxRichTextLineBreakChar
;
2726 return GetFocusObject()->InsertTextWithUndo(m_caretPosition
+1, text
, this, & GetBuffer());
2729 // ----------------------------------------------------------------------------
2730 // Clipboard operations
2731 // ----------------------------------------------------------------------------
2733 void wxRichTextCtrl::Copy()
2737 wxRichTextRange range
= GetInternalSelectionRange();
2738 GetBuffer().CopyToClipboard(range
);
2742 void wxRichTextCtrl::Cut()
2746 wxRichTextRange range
= GetInternalSelectionRange();
2747 GetBuffer().CopyToClipboard(range
);
2749 DeleteSelectedContent();
2755 void wxRichTextCtrl::Paste()
2759 BeginBatchUndo(_("Paste"));
2761 long newPos
= m_caretPosition
;
2762 DeleteSelectedContent(& newPos
);
2764 GetBuffer().PasteFromClipboard(newPos
);
2770 void wxRichTextCtrl::DeleteSelection()
2772 if (CanDeleteSelection())
2774 DeleteSelectedContent();
2778 bool wxRichTextCtrl::HasSelection() const
2780 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
2783 bool wxRichTextCtrl::HasUnfocusedSelection() const
2785 return m_selection
.IsValid();
2788 bool wxRichTextCtrl::CanCopy() const
2790 // Can copy if there's a selection
2791 return HasSelection();
2794 bool wxRichTextCtrl::CanCut() const
2796 return HasSelection() && IsEditable();
2799 bool wxRichTextCtrl::CanPaste() const
2801 if ( !IsEditable() )
2804 return GetBuffer().CanPasteFromClipboard();
2807 bool wxRichTextCtrl::CanDeleteSelection() const
2809 return HasSelection() && IsEditable();
2813 // ----------------------------------------------------------------------------
2815 // ----------------------------------------------------------------------------
2817 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
2819 if (m_contextMenu
&& m_contextMenu
!= menu
)
2820 delete m_contextMenu
;
2821 m_contextMenu
= menu
;
2824 void wxRichTextCtrl::SetEditable(bool editable
)
2826 m_editable
= editable
;
2829 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2833 m_caretPosition
= pos
- 1;
2837 SetDefaultStyleToCursorStyle();
2840 void wxRichTextCtrl::SetInsertionPointEnd()
2842 long pos
= GetLastPosition();
2843 SetInsertionPoint(pos
);
2846 long wxRichTextCtrl::GetInsertionPoint() const
2848 return m_caretPosition
+1;
2851 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2853 return GetFocusObject()->GetOwnRange().GetEnd();
2856 // If the return values from and to are the same, there is no
2858 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2860 if (m_selection
.IsValid())
2862 *from
= m_selection
.GetRange().GetStart();
2863 *to
= m_selection
.GetRange().GetEnd();
2873 bool wxRichTextCtrl::IsEditable() const
2878 // ----------------------------------------------------------------------------
2880 // ----------------------------------------------------------------------------
2882 void wxRichTextCtrl::SetSelection(long from
, long to
)
2884 // if from and to are both -1, it means (in wxWidgets) that all text should
2886 if ( (from
== -1) && (to
== -1) )
2889 to
= GetLastPosition()+1;
2898 wxRichTextSelection oldSelection
= m_selection
;
2900 m_selectionAnchor
= from
-1;
2901 m_selectionAnchorObject
= NULL
;
2902 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
2904 m_caretPosition
= wxMax(-1, to
-1);
2906 RefreshForSelectionChange(oldSelection
, m_selection
);
2911 // ----------------------------------------------------------------------------
2913 // ----------------------------------------------------------------------------
2915 void wxRichTextCtrl::Replace(long WXUNUSED(from
), long WXUNUSED(to
),
2916 const wxString
& value
)
2918 BeginBatchUndo(_("Replace"));
2920 DeleteSelectedContent();
2922 DoWriteText(value
, SetValue_SelectionOnly
);
2927 void wxRichTextCtrl::Remove(long from
, long to
)
2931 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
2938 bool wxRichTextCtrl::IsModified() const
2940 return m_buffer
.IsModified();
2943 void wxRichTextCtrl::MarkDirty()
2945 m_buffer
.Modify(true);
2948 void wxRichTextCtrl::DiscardEdits()
2950 m_caretPositionForDefaultStyle
= -2;
2951 m_buffer
.Modify(false);
2952 m_buffer
.GetCommandProcessor()->ClearCommands();
2955 int wxRichTextCtrl::GetNumberOfLines() const
2957 return GetFocusObject()->GetParagraphCount();
2960 // ----------------------------------------------------------------------------
2961 // Positions <-> coords
2962 // ----------------------------------------------------------------------------
2964 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
2966 return GetFocusObject()->XYToPosition(x
, y
);
2969 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
2971 return GetFocusObject()->PositionToXY(pos
, x
, y
);
2974 // ----------------------------------------------------------------------------
2976 // ----------------------------------------------------------------------------
2978 void wxRichTextCtrl::ShowPosition(long pos
)
2980 if (!IsPositionVisible(pos
))
2981 ScrollIntoView(pos
-1, WXK_DOWN
);
2984 int wxRichTextCtrl::GetLineLength(long lineNo
) const
2986 return GetFocusObject()->GetParagraphLength(lineNo
);
2989 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
2991 return GetFocusObject()->GetParagraphText(lineNo
);
2994 // ----------------------------------------------------------------------------
2996 // ----------------------------------------------------------------------------
2998 void wxRichTextCtrl::Undo()
3002 GetCommandProcessor()->Undo();
3006 void wxRichTextCtrl::Redo()
3010 GetCommandProcessor()->Redo();
3014 bool wxRichTextCtrl::CanUndo() const
3016 return GetCommandProcessor()->CanUndo() && IsEditable();
3019 bool wxRichTextCtrl::CanRedo() const
3021 return GetCommandProcessor()->CanRedo() && IsEditable();
3024 // ----------------------------------------------------------------------------
3025 // implementation details
3026 // ----------------------------------------------------------------------------
3028 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3030 SetValue(event
.GetString());
3031 GetEventHandler()->ProcessEvent(event
);
3034 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3036 // By default, load the first file into the text window.
3037 if (event
.GetNumberOfFiles() > 0)
3039 LoadFile(event
.GetFiles()[0]);
3043 wxSize
wxRichTextCtrl::DoGetBestSize() const
3045 return wxSize(10, 10);
3048 // ----------------------------------------------------------------------------
3049 // standard handlers for standard edit menu events
3050 // ----------------------------------------------------------------------------
3052 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3057 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3062 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3067 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3072 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3077 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3082 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3084 event
.Enable( CanCut() );
3087 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3089 event
.Enable( CanCopy() );
3092 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3094 event
.Enable( CanDeleteSelection() );
3097 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3099 event
.Enable( CanPaste() );
3102 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3104 event
.Enable( CanUndo() );
3105 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3108 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3110 event
.Enable( CanRedo() );
3111 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3114 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3116 if (GetLastPosition() > 0)
3120 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3122 event
.Enable(GetLastPosition() > 0);
3125 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3127 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3128 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3130 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3131 if (obj
&& obj
->CanEditProperties())
3132 obj
->EditProperties(this, & GetBuffer());
3134 m_contextMenuPropertiesInfo
.Clear();
3138 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3140 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3141 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3144 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3146 if (event
.GetEventObject() != this)
3152 wxClientDC
dc(this);
3154 dc
.SetFont(GetFont());
3157 wxPoint pt
= event
.GetPosition();
3158 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3159 wxRichTextObject
* hitObj
= NULL
;
3160 wxRichTextObject
* contextObj
= NULL
;
3161 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
);
3163 m_contextMenuPropertiesInfo
.Clear();
3165 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3167 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3168 if (hitObj
&& actualContainer
)
3170 if (actualContainer
->AcceptsFocus())
3172 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3173 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3176 m_contextMenuPropertiesInfo
.AddItems(actualContainer
, hitObj
);
3179 m_contextMenuPropertiesInfo
.AddItems(GetFocusObject(), NULL
);
3183 m_contextMenuPropertiesInfo
.AddItems(GetFocusObject(), NULL
);
3188 m_contextMenuPropertiesInfo
.AddMenuItems(m_contextMenu
);
3189 PopupMenu(m_contextMenu
);
3193 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3195 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3198 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3200 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3203 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3205 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3208 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3210 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3213 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
)
3215 GetFocusObject()->SetStyle(obj
, textAttr
);
3218 // extended style setting operation with flags including:
3219 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3220 // see richtextbuffer.h for more details.
3222 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3224 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3227 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3229 return GetBuffer().SetDefaultStyle(style
);
3232 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3234 wxRichTextAttr
attr1(style
);
3235 attr1
.GetTextBoxAttr().Reset();
3236 return GetBuffer().SetDefaultStyle(attr1
);
3239 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3241 return GetBuffer().GetDefaultStyle();
3244 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3246 wxRichTextAttr attr
;
3247 if (GetFocusObject()->GetStyle(position
, attr
))
3256 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3258 return GetFocusObject()->GetStyle(position
, style
);
3261 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3263 wxRichTextAttr attr
;
3264 if (container
->GetStyle(position
, attr
))
3273 // get the common set of styles for the range
3274 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3276 wxRichTextAttr attr
;
3277 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3286 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3288 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3291 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3293 return container
->GetStyleForRange(range
.ToInternal(), style
);
3296 /// Get the content (uncombined) attributes for this position.
3297 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3299 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3302 /// Get the content (uncombined) attributes for this position.
3303 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3305 return container
->GetUncombinedStyle(position
, style
);
3308 /// Set font, and also the buffer attributes
3309 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3311 wxControl::SetFont(font
);
3313 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3315 GetBuffer().SetBasicStyle(attr
);
3317 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3323 /// Transform logical to physical
3324 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3327 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3332 /// Transform physical to logical
3333 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3336 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3341 /// Position the caret
3342 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3347 //wxLogDebug(wxT("PositionCaret"));
3350 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3352 wxPoint newPt
= caretRect
.GetPosition();
3353 wxSize newSz
= caretRect
.GetSize();
3354 wxPoint pt
= GetPhysicalPoint(newPt
);
3355 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3358 if (GetCaret()->GetSize() != newSz
)
3359 GetCaret()->SetSize(newSz
);
3361 int halfSize
= newSz
.y
/2;
3362 // If the caret is beyond the margin, hide it by moving it out of the way
3363 if (((pt
.y
+ halfSize
) < GetBuffer().GetTopMargin()) || ((pt
.y
+ halfSize
) > (GetClientSize().y
- GetBuffer().GetBottomMargin())))
3369 GetCaret()->Move(pt
);
3375 /// Get the caret height and position for the given character position
3376 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3378 wxClientDC
dc(this);
3379 dc
.SetFont(GetFont());
3387 container
= GetFocusObject();
3389 if (container
->FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
3391 // Caret height can't be zero
3393 height
= dc
.GetCharHeight();
3395 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3402 /// Gets the line for the visible caret position. If the caret is
3403 /// shown at the very end of the line, it means the next character is actually
3404 /// on the following line. So let's get the line we're expecting to find
3405 /// if this is the case.
3406 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3408 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3409 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3412 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3413 if (caretPosition
== lineRange
.GetStart()-1 &&
3414 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3416 if (!m_caretAtLineStart
)
3417 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3424 /// Move the caret to the given character position
3425 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3427 if (GetBuffer().IsDirty())
3431 container
= GetFocusObject();
3433 if (pos
<= container
->GetOwnRange().GetEnd())
3435 SetCaretPosition(pos
, showAtLineStart
);
3437 PositionCaret(container
);
3445 /// Layout the buffer: which we must do before certain operations, such as
3446 /// setting the caret position.
3447 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3449 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3451 wxRect
availableSpace(GetClientSize());
3452 if (availableSpace
.width
== 0)
3453 availableSpace
.width
= 10;
3454 if (availableSpace
.height
== 0)
3455 availableSpace
.height
= 10;
3457 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3458 if (onlyVisibleRect
)
3460 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3461 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3464 wxClientDC
dc(this);
3465 dc
.SetFont(GetFont());
3469 GetBuffer().Defragment();
3470 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3471 GetBuffer().Layout(dc
, availableSpace
, flags
);
3472 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3481 /// Is all of the selection bold?
3482 bool wxRichTextCtrl::IsSelectionBold()
3486 wxRichTextAttr attr
;
3487 wxRichTextRange range
= GetSelectionRange();
3488 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3489 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3491 return HasCharacterAttributes(range
, attr
);
3495 // If no selection, then we need to combine current style with default style
3496 // to see what the effect would be if we started typing.
3497 wxRichTextAttr attr
;
3498 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3500 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3501 if (GetStyle(pos
, attr
))
3503 if (IsDefaultStyleShowing())
3504 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3505 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3511 /// Is all of the selection italics?
3512 bool wxRichTextCtrl::IsSelectionItalics()
3516 wxRichTextRange range
= GetSelectionRange();
3517 wxRichTextAttr attr
;
3518 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3519 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3521 return HasCharacterAttributes(range
, attr
);
3525 // If no selection, then we need to combine current style with default style
3526 // to see what the effect would be if we started typing.
3527 wxRichTextAttr attr
;
3528 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3530 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3531 if (GetStyle(pos
, attr
))
3533 if (IsDefaultStyleShowing())
3534 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3535 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3541 /// Is all of the selection underlined?
3542 bool wxRichTextCtrl::IsSelectionUnderlined()
3546 wxRichTextRange range
= GetSelectionRange();
3547 wxRichTextAttr attr
;
3548 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3549 attr
.SetFontUnderlined(true);
3551 return HasCharacterAttributes(range
, attr
);
3555 // If no selection, then we need to combine current style with default style
3556 // to see what the effect would be if we started typing.
3557 wxRichTextAttr attr
;
3558 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3559 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3561 if (GetStyle(pos
, attr
))
3563 if (IsDefaultStyleShowing())
3564 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3565 return attr
.GetFontUnderlined();
3571 /// Apply bold to the selection
3572 bool wxRichTextCtrl::ApplyBoldToSelection()
3574 wxRichTextAttr attr
;
3575 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3576 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3579 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3582 wxRichTextAttr current
= GetDefaultStyleEx();
3583 current
.Apply(attr
);
3584 SetAndShowDefaultStyle(current
);
3589 /// Apply italic to the selection
3590 bool wxRichTextCtrl::ApplyItalicToSelection()
3592 wxRichTextAttr attr
;
3593 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3594 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3597 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3600 wxRichTextAttr current
= GetDefaultStyleEx();
3601 current
.Apply(attr
);
3602 SetAndShowDefaultStyle(current
);
3607 /// Apply underline to the selection
3608 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3610 wxRichTextAttr attr
;
3611 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3612 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3615 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3618 wxRichTextAttr current
= GetDefaultStyleEx();
3619 current
.Apply(attr
);
3620 SetAndShowDefaultStyle(current
);
3625 /// Is all of the selection aligned according to the specified flag?
3626 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3628 wxRichTextRange range
;
3630 range
= GetSelectionRange();
3632 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3634 wxRichTextAttr attr
;
3635 attr
.SetAlignment(alignment
);
3637 return HasParagraphAttributes(range
, attr
);
3640 /// Apply alignment to the selection
3641 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3643 wxRichTextAttr attr
;
3644 attr
.SetAlignment(alignment
);
3646 return SetStyle(GetSelectionRange(), attr
);
3649 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
3651 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3656 /// Apply a named style to the selection
3657 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3659 // Flags are defined within each definition, so only certain
3660 // attributes are applied.
3661 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3663 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3665 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3667 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3669 wxRichTextRange range
;
3672 range
= GetSelectionRange();
3675 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3676 range
= wxRichTextRange(pos
, pos
+1);
3679 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3682 bool isPara
= false;
3684 // Make sure the attr has the style name
3685 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3688 attr
.SetParagraphStyleName(def
->GetName());
3690 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3691 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3692 // to change its style independently.
3693 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3696 attr
.SetCharacterStyleName(def
->GetName());
3699 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3702 wxRichTextAttr current
= GetDefaultStyleEx();
3703 wxRichTextAttr
defaultStyle(attr
);
3706 // Don't apply extra character styles since they are already implied
3707 // in the paragraph style
3708 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
3710 current
.Apply(defaultStyle
);
3711 SetAndShowDefaultStyle(current
);
3713 // If it's a paragraph style, we want to apply the style to the
3714 // current paragraph even if we didn't select any text.
3717 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3718 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
3721 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
3728 /// Apply the style sheet to the buffer, for example if the styles have changed.
3729 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3732 styleSheet
= GetBuffer().GetStyleSheet();
3736 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3738 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3746 /// Sets the default style to the style under the cursor
3747 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3749 wxRichTextAttr attr
;
3750 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3752 // If at the start of a paragraph, use the next position.
3753 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3755 if (GetUncombinedStyle(pos
, attr
))
3757 SetDefaultStyle(attr
);
3764 /// Returns the first visible position in the current view
3765 long wxRichTextCtrl::GetFirstVisiblePosition() const
3767 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3769 return line
->GetAbsoluteRange().GetStart();
3774 /// Get the first visible point in the window
3775 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3778 int startXUnits
, startYUnits
;
3780 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3781 GetViewStart(& startXUnits
, & startYUnits
);
3783 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3786 /// The adjusted caret position is the character position adjusted to take
3787 /// into account whether we're at the start of a paragraph, in which case
3788 /// style information should be taken from the next position, not current one.
3789 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3791 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
3793 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3798 /// Get/set the selection range in character positions. -1, -1 means no selection.
3799 /// The range is in API convention, i.e. a single character selection is denoted
3801 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3803 wxRichTextRange range
= GetInternalSelectionRange();
3804 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3805 range
.SetEnd(range
.GetEnd() + 1);
3809 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3811 SetSelection(range
.GetStart(), range
.GetEnd());
3815 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3817 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3820 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3822 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3825 /// Clear list for given range
3826 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3828 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
3831 /// Number/renumber any list elements in the given range
3832 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3834 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3837 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3839 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3842 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3843 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3845 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3848 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3850 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3853 /// Deletes the content in the given range
3854 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3856 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
3859 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3861 if (sm_availableFontNames
.GetCount() == 0)
3863 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3864 sm_availableFontNames
.Sort();
3866 return sm_availableFontNames
;
3869 void wxRichTextCtrl::ClearAvailableFontNames()
3871 sm_availableFontNames
.Clear();
3874 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3876 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3878 wxTextAttrEx basicStyle
= GetBasicStyle();
3879 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3880 SetBasicStyle(basicStyle
);
3881 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3886 // Refresh the area affected by a selection change
3887 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
3889 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
3890 // the selection contains whole containers rather than just text, so refresh everything
3891 // for now as it would be hard to compute the rectangle bounding all selections.
3892 // TODO: improve on this.
3893 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
3894 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
3900 wxRichTextRange oldRange
, newRange
;
3901 if (oldSelection
.IsValid())
3902 oldRange
= oldSelection
.GetRange();
3904 oldRange
= wxRICHTEXT_NO_SELECTION
;
3905 if (newSelection
.IsValid())
3906 newRange
= newSelection
.GetRange();
3908 newRange
= wxRICHTEXT_NO_SELECTION
;
3910 // Calculate the refresh rectangle - just the affected lines
3911 long firstPos
, lastPos
;
3912 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
3914 firstPos
= newRange
.GetStart();
3915 lastPos
= newRange
.GetEnd();
3917 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
3919 firstPos
= oldRange
.GetStart();
3920 lastPos
= oldRange
.GetEnd();
3922 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
3928 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
3929 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
3932 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
3933 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
3935 if (firstLine
&& lastLine
)
3937 wxSize clientSize
= GetClientSize();
3938 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
3939 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
3942 pt1
.y
= wxMax(0, pt1
.y
);
3944 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
3946 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
3947 RefreshRect(rect
, false);
3955 // margins functions
3956 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
3958 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
3959 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
3960 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
3961 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
3966 wxPoint
wxRichTextCtrl::DoGetMargins() const
3968 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
3969 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
3972 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
3974 if (obj
&& !obj
->AcceptsFocus())
3977 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
3978 bool changingContainer
= (m_focusObject
!= obj
);
3980 m_focusObject
= obj
;
3983 m_focusObject
= & m_buffer
;
3985 if (setCaretPosition
&& changingContainer
)
3987 m_selection
.Reset();
3988 m_selectionAnchor
= -2;
3989 m_selectionAnchorObject
= NULL
;
3990 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
3994 m_caretAtLineStart
= false;
3995 MoveCaret(pos
, m_caretAtLineStart
);
3996 SetDefaultStyleToCursorStyle();
3998 wxRichTextEvent
cmdEvent(
3999 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4001 cmdEvent
.SetEventObject(this);
4002 cmdEvent
.SetPosition(m_caretPosition
+1);
4003 cmdEvent
.SetOldContainer(oldContainer
);
4004 cmdEvent
.SetContainer(m_focusObject
);
4006 GetEventHandler()->ProcessEvent(cmdEvent
);
4011 #if wxRICHTEXT_USE_OWN_CARET
4013 // ----------------------------------------------------------------------------
4014 // initialization and destruction
4015 // ----------------------------------------------------------------------------
4017 void wxRichTextCaret::Init()
4023 m_richTextCtrl
= NULL
;
4024 m_needsUpdate
= false;
4028 wxRichTextCaret::~wxRichTextCaret()
4030 if (m_timer
.IsRunning())
4034 // ----------------------------------------------------------------------------
4035 // showing/hiding/moving the caret (base class interface)
4036 // ----------------------------------------------------------------------------
4038 void wxRichTextCaret::DoShow()
4042 if (!m_timer
.IsRunning())
4043 m_timer
.Start(GetBlinkTime());
4048 void wxRichTextCaret::DoHide()
4050 if (m_timer
.IsRunning())
4056 void wxRichTextCaret::DoMove()
4062 if (m_xOld
!= -1 && m_yOld
!= -1)
4066 wxRect
rect(GetPosition(), GetSize());
4067 m_richTextCtrl
->RefreshRect(rect
, false);
4076 void wxRichTextCaret::DoSize()
4078 int countVisible
= m_countVisible
;
4079 if (countVisible
> 0)
4085 if (countVisible
> 0)
4087 m_countVisible
= countVisible
;
4092 // ----------------------------------------------------------------------------
4093 // handling the focus
4094 // ----------------------------------------------------------------------------
4096 void wxRichTextCaret::OnSetFocus()
4104 void wxRichTextCaret::OnKillFocus()
4109 // ----------------------------------------------------------------------------
4110 // drawing the caret
4111 // ----------------------------------------------------------------------------
4113 void wxRichTextCaret::Refresh()
4117 wxRect
rect(GetPosition(), GetSize());
4118 m_richTextCtrl
->RefreshRect(rect
, false);
4122 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4124 dc
->SetPen( *wxBLACK_PEN
);
4126 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4127 dc
->SetPen(*wxBLACK_PEN
);
4129 wxPoint
pt(m_x
, m_y
);
4133 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4135 if (IsVisible() && m_flashOn
)
4136 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4139 void wxRichTextCaret::Notify()
4141 m_flashOn
= !m_flashOn
;
4145 void wxRichTextCaretTimer::Notify()
4150 // wxRICHTEXT_USE_OWN_CARET
4153 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4157 m_labels
.Add(label
);
4165 // Returns number of menu items were added.
4166 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4168 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4169 // If none of the standard properties identifiers are in the menu, assume it's
4170 // a custom menu without properties commands, and don't add them.
4173 // If no items, to add just set the text to something generic
4174 if (GetCount() == 0)
4176 menu
->SetLabel(startCmd
, _("&Properties"));
4178 // Delete the others if necessary
4180 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4182 if (menu
->FindItem(i
))
4192 // Find the position of the first properties item
4193 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4195 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4196 if (item
&& item
->GetId() == startCmd
)
4205 int insertBefore
= pos
+1;
4206 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4208 if (menu
->FindItem(i
))
4210 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4214 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4215 menu
->Append(i
, m_labels
[i
- startCmd
]);
4217 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4222 // Delete any old items still left on the menu
4223 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4225 if (menu
->FindItem(i
))
4237 // Add appropriate menu items for the current container and clicked on object
4238 // (and container's parent, if appropriate).
4239 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextObject
* container
, wxRichTextObject
* obj
)
4242 if (obj
&& obj
->CanEditProperties())
4243 AddItem(obj
->GetPropertiesMenuLabel(), obj
);
4245 if (container
&& container
!= obj
&& container
->CanEditProperties() && m_labels
.Index(container
->GetPropertiesMenuLabel()) == wxNOT_FOUND
)
4246 AddItem(container
->GetPropertiesMenuLabel(), container
);
4248 if (container
&& container
->GetParent() && container
->GetParent()->CanEditProperties() && m_labels
.Index(container
->GetParent()->GetPropertiesMenuLabel()) == wxNOT_FOUND
)
4249 AddItem(container
->GetParent()->GetPropertiesMenuLabel(), container
->GetParent());