1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextctrl.cpp
3 // Purpose: A rich edit control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtextctrl.h"
22 #include "wx/richtext/richtextstyles.h"
26 #include "wx/settings.h"
30 #include "wx/textfile.h"
32 #include "wx/filename.h"
33 #include "wx/dcbuffer.h"
34 #include "wx/arrimpl.cpp"
35 #include "wx/fontenum.h"
38 #if defined (__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
39 #define wxHAVE_PRIMARY_SELECTION 1
41 #define wxHAVE_PRIMARY_SELECTION 0
44 #if wxUSE_CLIPBOARD && wxHAVE_PRIMARY_SELECTION
45 #include "wx/clipbrd.h"
48 // DLL options compatibility check:
50 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
, wxRichTextEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
, wxRichTextEvent
);
54 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
, wxRichTextEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
, wxRichTextEvent
);
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN
, wxRichTextEvent
);
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER
, wxRichTextEvent
);
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE
, wxRichTextEvent
);
60 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING
, wxRichTextEvent
);
61 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED
, wxRichTextEvent
);
62 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING
, wxRichTextEvent
);
63 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED
, wxRichTextEvent
);
65 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED
, wxRichTextEvent
);
66 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED
, wxRichTextEvent
);
67 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED
, wxRichTextEvent
);
68 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED
, wxRichTextEvent
);
69 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET
, wxRichTextEvent
);
70 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
, wxRichTextEvent
);
72 #if wxRICHTEXT_USE_OWN_CARET
77 * This implements a non-flashing cursor in case there
78 * are platform-specific problems with the generic caret.
79 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
82 class wxRichTextCaret
;
83 class wxRichTextCaretTimer
: public wxTimer
86 wxRichTextCaretTimer(wxRichTextCaret
* caret
)
90 virtual void Notify();
91 wxRichTextCaret
* m_caret
;
94 class wxRichTextCaret
: public wxCaret
99 // default - use Create()
100 wxRichTextCaret(): m_timer(this) { Init(); }
101 // creates a block caret associated with the given window
102 wxRichTextCaret(wxRichTextCtrl
*window
, int width
, int height
)
103 : wxCaret(window
, width
, height
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
104 wxRichTextCaret(wxRichTextCtrl
*window
, const wxSize
& size
)
105 : wxCaret(window
, size
), m_timer(this) { Init(); m_richTextCtrl
= window
; }
107 virtual ~wxRichTextCaret();
112 // called by wxWindow (not using the event tables)
113 virtual void OnSetFocus();
114 virtual void OnKillFocus();
116 // draw the caret on the given DC
117 void DoDraw(wxDC
*dc
);
119 // get the visible count
120 int GetVisibleCount() const { return m_countVisible
; }
122 // delay repositioning
123 bool GetNeedsUpdate() const { return m_needsUpdate
; }
124 void SetNeedsUpdate(bool needsUpdate
= true ) { m_needsUpdate
= needsUpdate
; }
129 virtual void DoShow();
130 virtual void DoHide();
131 virtual void DoMove();
132 virtual void DoSize();
142 bool m_hasFocus
; // true => our window has focus
143 bool m_needsUpdate
; // must be repositioned
145 wxRichTextCaretTimer m_timer
;
146 wxRichTextCtrl
* m_richTextCtrl
;
150 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl
, wxControl
)
152 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent
, wxNotifyEvent
)
154 BEGIN_EVENT_TABLE( wxRichTextCtrl
, wxControl
)
155 EVT_PAINT(wxRichTextCtrl::OnPaint
)
156 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground
)
157 EVT_IDLE(wxRichTextCtrl::OnIdle
)
158 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll
)
159 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick
)
160 EVT_MOTION(wxRichTextCtrl::OnMoveMouse
)
161 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp
)
162 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick
)
163 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick
)
164 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick
)
165 EVT_CHAR(wxRichTextCtrl::OnChar
)
166 EVT_KEY_DOWN(wxRichTextCtrl::OnChar
)
167 EVT_SIZE(wxRichTextCtrl::OnSize
)
168 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus
)
169 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus
)
170 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost
)
171 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu
)
172 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged
)
174 EVT_MENU(wxID_UNDO
, wxRichTextCtrl::OnUndo
)
175 EVT_UPDATE_UI(wxID_UNDO
, wxRichTextCtrl::OnUpdateUndo
)
177 EVT_MENU(wxID_REDO
, wxRichTextCtrl::OnRedo
)
178 EVT_UPDATE_UI(wxID_REDO
, wxRichTextCtrl::OnUpdateRedo
)
180 EVT_MENU(wxID_COPY
, wxRichTextCtrl::OnCopy
)
181 EVT_UPDATE_UI(wxID_COPY
, wxRichTextCtrl::OnUpdateCopy
)
183 EVT_MENU(wxID_PASTE
, wxRichTextCtrl::OnPaste
)
184 EVT_UPDATE_UI(wxID_PASTE
, wxRichTextCtrl::OnUpdatePaste
)
186 EVT_MENU(wxID_CUT
, wxRichTextCtrl::OnCut
)
187 EVT_UPDATE_UI(wxID_CUT
, wxRichTextCtrl::OnUpdateCut
)
189 EVT_MENU(wxID_CLEAR
, wxRichTextCtrl::OnClear
)
190 EVT_UPDATE_UI(wxID_CLEAR
, wxRichTextCtrl::OnUpdateClear
)
192 EVT_MENU(wxID_SELECTALL
, wxRichTextCtrl::OnSelectAll
)
193 EVT_UPDATE_UI(wxID_SELECTALL
, wxRichTextCtrl::OnUpdateSelectAll
)
195 EVT_MENU(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnProperties
)
196 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1
, wxRichTextCtrl::OnUpdateProperties
)
198 EVT_MENU(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnProperties
)
199 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2
, wxRichTextCtrl::OnUpdateProperties
)
201 EVT_MENU(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnProperties
)
202 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3
, wxRichTextCtrl::OnUpdateProperties
)
210 wxArrayString
wxRichTextCtrl::sm_availableFontNames
;
212 wxRichTextCtrl::wxRichTextCtrl()
213 : wxScrollHelper(this)
218 wxRichTextCtrl::wxRichTextCtrl(wxWindow
* parent
,
220 const wxString
& value
,
224 const wxValidator
& validator
,
225 const wxString
& name
)
226 : wxScrollHelper(this)
229 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
233 bool wxRichTextCtrl::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& value
, const wxPoint
& pos
, const wxSize
& size
, long style
,
234 const wxValidator
& validator
, const wxString
& name
)
238 if (!wxControl::Create(parent
, id
, pos
, size
,
239 style
|wxFULL_REPAINT_ON_RESIZE
,
243 if (!GetFont().IsOk())
245 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
248 // No physical scrolling, so we can preserve margins
249 EnableScrolling(false, false);
251 if (style
& wxTE_READONLY
)
254 // The base attributes must all have default values
255 wxRichTextAttr attributes
;
256 attributes
.SetFont(GetFont());
257 attributes
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
258 attributes
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
259 attributes
.SetLineSpacing(10);
260 attributes
.SetParagraphSpacingAfter(10);
261 attributes
.SetParagraphSpacingBefore(0);
263 SetBasicStyle(attributes
);
266 SetMargins(margin
, margin
);
268 // The default attributes will be merged with base attributes, so
269 // can be empty to begin with
270 wxRichTextAttr defaultAttributes
;
271 SetDefaultStyle(defaultAttributes
);
273 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
274 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
277 GetBuffer().SetRichTextCtrl(this);
279 #if wxRICHTEXT_USE_OWN_CARET
280 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
282 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH
, 16));
285 // Tell the sizers to use the given or best size
286 SetInitialSize(size
);
288 #if wxRICHTEXT_BUFFERED_PAINTING
290 RecreateBuffer(size
);
293 m_textCursor
= wxCursor(wxCURSOR_IBEAM
);
294 m_urlCursor
= wxCursor(wxCURSOR_HAND
);
296 SetCursor(m_textCursor
);
298 if (!value
.IsEmpty())
301 GetBuffer().AddEventHandler(this);
304 wxAcceleratorEntry entries
[6];
306 entries
[0].Set(wxACCEL_CTRL
, (int) 'C', wxID_COPY
);
307 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', wxID_CUT
);
308 entries
[2].Set(wxACCEL_CTRL
, (int) 'V', wxID_PASTE
);
309 entries
[3].Set(wxACCEL_CTRL
, (int) 'A', wxID_SELECTALL
);
310 entries
[4].Set(wxACCEL_CTRL
, (int) 'Z', wxID_UNDO
);
311 entries
[5].Set(wxACCEL_CTRL
, (int) 'Y', wxID_REDO
);
313 wxAcceleratorTable
accel(6, entries
);
314 SetAcceleratorTable(accel
);
316 m_contextMenu
= new wxMenu
;
317 m_contextMenu
->Append(wxID_UNDO
, _("&Undo"));
318 m_contextMenu
->Append(wxID_REDO
, _("&Redo"));
319 m_contextMenu
->AppendSeparator();
320 m_contextMenu
->Append(wxID_CUT
, _("Cu&t"));
321 m_contextMenu
->Append(wxID_COPY
, _("&Copy"));
322 m_contextMenu
->Append(wxID_PASTE
, _("&Paste"));
323 m_contextMenu
->Append(wxID_CLEAR
, _("&Delete"));
324 m_contextMenu
->AppendSeparator();
325 m_contextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
326 m_contextMenu
->AppendSeparator();
327 m_contextMenu
->Append(wxID_RICHTEXT_PROPERTIES1
, _("&Properties"));
332 wxRichTextCtrl::~wxRichTextCtrl()
334 SetFocusObject(& GetBuffer(), false);
335 GetBuffer().RemoveEventHandler(this);
337 delete m_contextMenu
;
340 /// Member initialisation
341 void wxRichTextCtrl::Init()
343 m_contextMenu
= NULL
;
345 m_caretPosition
= -1;
346 m_selectionAnchor
= -2;
347 m_selectionAnchorObject
= NULL
;
348 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
350 m_caretAtLineStart
= false;
352 m_fullLayoutRequired
= false;
353 m_fullLayoutTime
= 0;
354 m_fullLayoutSavedPosition
= 0;
355 m_delayedLayoutThreshold
= wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD
;
356 m_caretPositionForDefaultStyle
= -2;
357 m_focusObject
= & m_buffer
;
360 void wxRichTextCtrl::DoThaw()
362 if (GetBuffer().IsDirty())
371 void wxRichTextCtrl::Clear()
373 if (GetFocusObject() == & GetBuffer())
375 m_buffer
.ResetAndClearCommands();
376 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
380 GetFocusObject()->Reset();
383 m_caretPosition
= -1;
384 m_caretPositionForDefaultStyle
= -2;
385 m_caretAtLineStart
= false;
387 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
397 wxTextCtrl::SendTextUpdatedEvent(this);
401 void wxRichTextCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
403 #if !wxRICHTEXT_USE_OWN_CARET
404 if (GetCaret() && !IsFrozen())
409 #if wxRICHTEXT_BUFFERED_PAINTING
410 wxBufferedPaintDC
dc(this, m_bufferBitmap
);
420 dc
.SetFont(GetFont());
422 // Paint the background
425 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
427 wxRect
drawingArea(GetUpdateRegion().GetBox());
428 drawingArea
.SetPosition(GetLogicalPoint(drawingArea
.GetPosition()));
430 wxRect
availableSpace(GetClientSize());
431 if (GetBuffer().IsDirty())
433 GetBuffer().Layout(dc
, availableSpace
, wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
);
434 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
438 wxRect
clipRect(availableSpace
);
439 clipRect
.x
+= GetBuffer().GetLeftMargin();
440 clipRect
.y
+= GetBuffer().GetTopMargin();
441 clipRect
.width
-= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
442 clipRect
.height
-= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
443 clipRect
.SetPosition(GetLogicalPoint(clipRect
.GetPosition()));
444 dc
.SetClippingRegion(clipRect
);
447 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES
) == 0)
448 flags
|= wxRICHTEXT_DRAW_GUIDELINES
;
450 GetBuffer().Draw(dc
, GetBuffer().GetOwnRange(), GetSelection(), drawingArea
, 0 /* descent */, flags
);
452 dc
.DestroyClippingRegion();
454 // Other user defined painting after everything else (i.e. all text) is painted
455 PaintAboveContent(dc
);
457 #if wxRICHTEXT_USE_OWN_CARET
458 if (GetCaret()->IsVisible())
460 ((wxRichTextCaret
*) GetCaret())->DoDraw(& dc
);
465 #if !wxRICHTEXT_USE_OWN_CARET
470 #if !defined(__WXMAC__)
471 // Causes caret dropouts on Mac
477 // Empty implementation, to prevent flicker
478 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
482 void wxRichTextCtrl::OnSetFocus(wxFocusEvent
& WXUNUSED(event
))
486 #if !wxRICHTEXT_USE_OWN_CARET
492 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
493 // Work around dropouts when control is focused
501 void wxRichTextCtrl::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
506 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
507 // Work around dropouts when control is focused
515 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
520 // Set up the caret for the given position and container, after a mouse click
521 bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox
* container
, long position
, int hitTestFlags
, bool extendSelection
)
523 bool caretAtLineStart
= false;
525 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
527 // If we're at the start of a line (but not first in para)
528 // then we should keep the caret showing at the start of the line
529 // by showing the m_caretAtLineStart flag.
530 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
531 wxRichTextLine
* line
= container
->GetLineAtPosition(position
);
533 if (line
&& para
&& line
->GetAbsoluteRange().GetStart() == position
&& para
->GetRange().GetStart() != position
)
534 caretAtLineStart
= true;
538 if (extendSelection
&& (m_caretPosition
!= position
))
539 ExtendSelection(m_caretPosition
, position
, wxRICHTEXT_SHIFT_DOWN
);
541 MoveCaret(position
, caretAtLineStart
);
542 SetDefaultStyleToCursorStyle();
548 void wxRichTextCtrl::OnLeftClick(wxMouseEvent
& event
)
554 dc
.SetFont(GetFont());
556 // TODO: detect change of focus object
558 wxRichTextObject
* hitObj
= NULL
;
559 wxRichTextObject
* contextObj
= NULL
;
560 int hit
= GetBuffer().HitTest(dc
, event
.GetLogicalPosition(dc
), position
, & hitObj
, & contextObj
);
562 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& hitObj
)
564 wxRichTextParagraphLayoutBox
* oldFocusObject
= GetFocusObject();
565 wxRichTextParagraphLayoutBox
* container
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
566 if (container
&& container
!= GetFocusObject() && container
->AcceptsFocus())
568 SetFocusObject(container
, false /* don't set caret position yet */);
571 m_dragStart
= event
.GetLogicalPosition(dc
);
575 long oldCaretPos
= m_caretPosition
;
577 SetCaretPositionAfterClick(container
, position
, hit
);
579 // For now, don't handle shift-click when we're selecting multiple objects.
580 if (event
.ShiftDown() && GetFocusObject() == oldFocusObject
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
581 ExtendSelection(oldCaretPos
, m_caretPosition
, wxRICHTEXT_SHIFT_DOWN
);
590 void wxRichTextCtrl::OnLeftUp(wxMouseEvent
& event
)
595 if (GetCapture() == this)
598 // See if we clicked on a URL
601 dc
.SetFont(GetFont());
604 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
605 wxRichTextObject
* hitObj
= NULL
;
606 wxRichTextObject
* contextObj
= NULL
;
607 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
608 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
610 if ((hit
!= wxRICHTEXT_HITTEST_NONE
) && !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
612 wxRichTextEvent
cmdEvent(
613 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK
,
615 cmdEvent
.SetEventObject(this);
616 cmdEvent
.SetPosition(position
);
618 cmdEvent
.SetContainer(hitObj
->GetContainer());
620 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
623 if (GetStyle(position
, attr
))
625 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
627 wxString urlTarget
= attr
.GetURL();
628 if (!urlTarget
.IsEmpty())
630 wxMouseEvent
mouseEvent(event
);
632 long startPos
= 0, endPos
= 0;
633 wxRichTextObject
* obj
= GetFocusObject()->GetLeafObjectAtPosition(position
);
636 startPos
= obj
->GetRange().GetStart();
637 endPos
= obj
->GetRange().GetEnd();
640 wxTextUrlEvent
urlEvent(GetId(), mouseEvent
, startPos
, endPos
);
641 InitCommandEvent(urlEvent
);
643 urlEvent
.SetString(urlTarget
);
645 GetEventHandler()->ProcessEvent(urlEvent
);
653 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
654 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
656 // Put the selection in PRIMARY, if it exists
657 wxTheClipboard
->UsePrimarySelection(true);
659 wxRichTextRange range
= GetInternalSelectionRange();
660 GetFocusObject()->GetBuffer()->CopyToClipboard(range
);
662 wxTheClipboard
->UsePrimarySelection(false);
668 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent
& event
)
672 dc
.SetFont(GetFont());
675 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
676 wxRichTextObject
* hitObj
= NULL
;
677 wxRichTextObject
* contextObj
= NULL
;
681 // If we're dragging, let's only consider positions at this level; otherwise
682 // selecting a range is not going to work.
683 wxRichTextParagraphLayoutBox
* container
= & GetBuffer();
686 flags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
687 container
= GetFocusObject();
689 int hit
= container
->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
, flags
);
691 // See if we need to change the cursor
694 if (hit
!= wxRICHTEXT_HITTEST_NONE
&& !(hit
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj
)
696 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
698 if (actualContainer
&& GetStyle(position
, attr
, actualContainer
))
700 if (attr
.HasFlag(wxTEXT_ATTR_URL
))
702 SetCursor(m_urlCursor
);
704 else if (!attr
.HasFlag(wxTEXT_ATTR_URL
))
706 SetCursor(m_textCursor
);
711 SetCursor(m_textCursor
);
714 if (!event
.Dragging())
722 wxRichTextParagraphLayoutBox
* commonAncestor
= NULL
;
723 wxRichTextParagraphLayoutBox
* otherContainer
= NULL
;
724 wxRichTextParagraphLayoutBox
* firstContainer
= NULL
;
726 // Check for dragging across multiple containers
728 wxRichTextObject
* hitObj2
= NULL
, *contextObj2
= NULL
;
729 int hit2
= GetBuffer().HitTest(dc
, logicalPt
, position2
, & hitObj2
, & contextObj2
, 0);
730 if (hit2
!= wxRICHTEXT_HITTEST_NONE
&& !(hit2
& wxRICHTEXT_HITTEST_OUTSIDE
) && hitObj2
&& hitObj
!= hitObj2
)
732 // See if we can find a common ancestor
733 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
735 firstContainer
= GetFocusObject();
736 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
740 firstContainer
= wxDynamicCast(m_selectionAnchorObject
, wxRichTextParagraphLayoutBox
);
741 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
742 // is the common ancestor.
743 commonAncestor
= wxDynamicCast(firstContainer
->GetParent(), wxRichTextParagraphLayoutBox
);
746 if (commonAncestor
&& commonAncestor
->HandlesChildSelections())
748 wxRichTextObject
* p
= hitObj2
;
751 if (p
->GetParent() == commonAncestor
)
753 otherContainer
= wxDynamicCast(p
, wxRichTextParagraphLayoutBox
);
760 if (commonAncestor
&& firstContainer
&& otherContainer
)
762 // We have now got a second container that shares a parent with the current or anchor object.
763 if (m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
765 // Don't go into common-ancestor selection mode if we still have the same
767 if (otherContainer
!= firstContainer
)
769 m_selectionState
= wxRichTextCtrlSelectionState_CommonAncestor
;
770 m_selectionAnchorObject
= firstContainer
;
771 m_selectionAnchor
= firstContainer
->GetRange().GetStart();
773 // The common ancestor, such as a table, returns the cell selection
774 // between the anchor and current position.
775 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
780 m_selection
= commonAncestor
->GetSelection(m_selectionAnchor
, otherContainer
->GetRange().GetStart());
785 if (otherContainer
->AcceptsFocus())
786 SetFocusObject(otherContainer
, false /* don't set caret and clear selection */);
787 MoveCaret(-1, false);
788 SetDefaultStyleToCursorStyle();
793 if (hitObj
&& m_dragging
&& hit
!= wxRICHTEXT_HITTEST_NONE
&& m_selectionState
== wxRichTextCtrlSelectionState_Normal
)
795 // TODO: test closeness
796 SetCaretPositionAfterClick(container
, position
, hit
, true /* extend selection */);
801 void wxRichTextCtrl::OnRightClick(wxMouseEvent
& event
)
807 dc
.SetFont(GetFont());
810 wxPoint logicalPt
= event
.GetLogicalPosition(dc
);
811 wxRichTextObject
* hitObj
= NULL
;
812 wxRichTextObject
* contextObj
= NULL
;
813 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
);
815 if (hitObj
&& hitObj
->GetContainer() != GetFocusObject())
817 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
818 if (actualContainer
&& actualContainer
->AcceptsFocus())
820 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
821 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
825 wxRichTextEvent
cmdEvent(
826 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK
,
828 cmdEvent
.SetEventObject(this);
829 cmdEvent
.SetPosition(position
);
831 cmdEvent
.SetContainer(hitObj
->GetContainer());
833 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
837 /// Left-double-click
838 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent
& WXUNUSED(event
))
840 wxRichTextEvent
cmdEvent(
841 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK
,
843 cmdEvent
.SetEventObject(this);
844 cmdEvent
.SetPosition(m_caretPosition
+1);
845 cmdEvent
.SetContainer(GetFocusObject());
847 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
849 SelectWord(GetCaretPosition()+1);
854 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent
& event
)
856 wxRichTextEvent
cmdEvent(
857 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK
,
859 cmdEvent
.SetEventObject(this);
860 cmdEvent
.SetPosition(m_caretPosition
+1);
861 cmdEvent
.SetContainer(GetFocusObject());
863 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
866 #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
867 // Paste any PRIMARY selection, if it exists
868 wxTheClipboard
->UsePrimarySelection(true);
870 wxTheClipboard
->UsePrimarySelection(false);
875 void wxRichTextCtrl::OnChar(wxKeyEvent
& event
)
879 flags
|= wxRICHTEXT_CTRL_DOWN
;
880 if (event
.ShiftDown())
881 flags
|= wxRICHTEXT_SHIFT_DOWN
;
883 flags
|= wxRICHTEXT_ALT_DOWN
;
885 if (event
.GetEventType() == wxEVT_KEY_DOWN
)
887 if (event
.IsKeyInCategory(WXK_CATEGORY_NAVIGATION
))
889 KeyboardNavigate(event
.GetKeyCode(), flags
);
893 long keycode
= event
.GetKeyCode();
953 case WXK_NUMPAD_HOME
:
954 case WXK_NUMPAD_LEFT
:
956 case WXK_NUMPAD_RIGHT
:
957 case WXK_NUMPAD_DOWN
:
958 case WXK_NUMPAD_PAGEUP
:
959 case WXK_NUMPAD_PAGEDOWN
:
961 case WXK_NUMPAD_BEGIN
:
962 case WXK_NUMPAD_INSERT
:
963 case WXK_WINDOWS_LEFT
:
972 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
973 if (event
.CmdDown() && event
.GetKeyCode() == WXK_BACK
)
975 BeginBatchUndo(_("Delete Text"));
977 long newPos
= m_caretPosition
;
979 bool processed
= DeleteSelectedContent(& newPos
);
981 // Submit range in character positions, which are greater than caret positions,
982 // so subtract 1 for deleted character and add 1 for conversion to character position.
987 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
990 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
996 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
1001 if (GetLastPosition() == -1)
1003 GetFocusObject()->Reset();
1005 m_caretPosition
= -1;
1007 SetDefaultStyleToCursorStyle();
1010 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1012 wxRichTextEvent
cmdEvent(
1013 wxEVT_COMMAND_RICHTEXT_DELETE
,
1015 cmdEvent
.SetEventObject(this);
1016 cmdEvent
.SetFlags(flags
);
1017 cmdEvent
.SetPosition(m_caretPosition
+1);
1018 cmdEvent
.SetContainer(GetFocusObject());
1019 GetEventHandler()->ProcessEvent(cmdEvent
);
1029 // all the other keys modify the controls contents which shouldn't be
1030 // possible if we're read-only
1031 if ( !IsEditable() )
1037 if (event
.GetKeyCode() == WXK_RETURN
)
1039 BeginBatchUndo(_("Insert Text"));
1041 long newPos
= m_caretPosition
;
1043 DeleteSelectedContent(& newPos
);
1045 if (event
.ShiftDown())
1048 text
= wxRichTextLineBreakChar
;
1049 GetFocusObject()->InsertTextWithUndo(newPos
+1, text
, this, & GetBuffer());
1050 m_caretAtLineStart
= true;
1054 GetFocusObject()->InsertNewlineWithUndo(newPos
+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
|wxRICHTEXT_INSERT_INTERACTIVE
);
1057 SetDefaultStyleToCursorStyle();
1059 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1061 wxRichTextEvent
cmdEvent(
1062 wxEVT_COMMAND_RICHTEXT_RETURN
,
1064 cmdEvent
.SetEventObject(this);
1065 cmdEvent
.SetFlags(flags
);
1066 cmdEvent
.SetPosition(newPos
+1);
1067 cmdEvent
.SetContainer(GetFocusObject());
1069 if (!GetEventHandler()->ProcessEvent(cmdEvent
))
1071 // Generate conventional event
1072 wxCommandEvent
textEvent(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1073 InitCommandEvent(textEvent
);
1075 GetEventHandler()->ProcessEvent(textEvent
);
1079 else if (event
.GetKeyCode() == WXK_BACK
)
1081 BeginBatchUndo(_("Delete Text"));
1083 long newPos
= m_caretPosition
;
1085 bool processed
= DeleteSelectedContent(& newPos
);
1087 // Submit range in character positions, which are greater than caret positions,
1088 // so subtract 1 for deleted character and add 1 for conversion to character position.
1091 if (event
.CmdDown())
1093 long pos
= wxRichTextCtrl::FindNextWordPosition(-1);
1096 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos
+1, newPos
), this, & GetBuffer());
1102 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
, newPos
), this, & GetBuffer());
1107 if (GetLastPosition() == -1)
1109 GetFocusObject()->Reset();
1111 m_caretPosition
= -1;
1113 SetDefaultStyleToCursorStyle();
1116 ScrollIntoView(m_caretPosition
, WXK_LEFT
);
1118 wxRichTextEvent
cmdEvent(
1119 wxEVT_COMMAND_RICHTEXT_DELETE
,
1121 cmdEvent
.SetEventObject(this);
1122 cmdEvent
.SetFlags(flags
);
1123 cmdEvent
.SetPosition(m_caretPosition
+1);
1124 cmdEvent
.SetContainer(GetFocusObject());
1125 GetEventHandler()->ProcessEvent(cmdEvent
);
1129 else if (event
.GetKeyCode() == WXK_DELETE
)
1131 BeginBatchUndo(_("Delete Text"));
1133 long newPos
= m_caretPosition
;
1135 bool processed
= DeleteSelectedContent(& newPos
);
1137 // Submit range in character positions, which are greater than caret positions,
1138 if (newPos
< GetFocusObject()->GetOwnRange().GetEnd()+1)
1140 if (event
.CmdDown())
1142 long pos
= wxRichTextCtrl::FindNextWordPosition(1);
1143 if (pos
!= -1 && (pos
> newPos
))
1145 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, pos
), this, & GetBuffer());
1150 if (!processed
&& newPos
< (GetLastPosition()-1))
1151 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos
+1, newPos
+1), this, & GetBuffer());
1156 if (GetLastPosition() == -1)
1158 GetFocusObject()->Reset();
1160 m_caretPosition
= -1;
1162 SetDefaultStyleToCursorStyle();
1165 wxRichTextEvent
cmdEvent(
1166 wxEVT_COMMAND_RICHTEXT_DELETE
,
1168 cmdEvent
.SetEventObject(this);
1169 cmdEvent
.SetFlags(flags
);
1170 cmdEvent
.SetPosition(m_caretPosition
+1);
1171 cmdEvent
.SetContainer(GetFocusObject());
1172 GetEventHandler()->ProcessEvent(cmdEvent
);
1178 long keycode
= event
.GetKeyCode();
1190 if (event
.CmdDown())
1192 // Fixes AltGr+key with European input languages on Windows
1193 if ((event
.CmdDown() && !event
.AltDown()) || (event
.AltDown() && !event
.CmdDown()))
1200 wxRichTextEvent
cmdEvent(
1201 wxEVT_COMMAND_RICHTEXT_CHARACTER
,
1203 cmdEvent
.SetEventObject(this);
1204 cmdEvent
.SetFlags(flags
);
1206 cmdEvent
.SetCharacter(event
.GetUnicodeKey());
1208 cmdEvent
.SetCharacter((wxChar
) keycode
);
1210 cmdEvent
.SetPosition(m_caretPosition
+1);
1211 cmdEvent
.SetContainer(GetFocusObject());
1213 if (keycode
== wxT('\t'))
1215 // See if we need to promote or demote the selection or paragraph at the cursor
1216 // position, instead of inserting a tab.
1217 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
1218 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
1219 if (para
&& para
->GetRange().GetStart() == pos
&& para
->GetAttributes().HasListStyleName())
1221 wxRichTextRange range
;
1223 range
= GetSelectionRange();
1225 range
= para
->GetRange().FromInternal();
1227 int promoteBy
= event
.ShiftDown() ? 1 : -1;
1229 PromoteList(promoteBy
, range
, NULL
);
1231 GetEventHandler()->ProcessEvent(cmdEvent
);
1237 BeginBatchUndo(_("Insert Text"));
1239 long newPos
= m_caretPosition
;
1240 DeleteSelectedContent(& newPos
);
1243 wxString str
= event
.GetUnicodeKey();
1245 wxString str
= (wxChar
) event
.GetKeyCode();
1247 GetFocusObject()->InsertTextWithUndo(newPos
+1, str
, this, & GetBuffer(), 0);
1251 SetDefaultStyleToCursorStyle();
1252 ScrollIntoView(m_caretPosition
, WXK_RIGHT
);
1254 cmdEvent
.SetPosition(m_caretPosition
);
1255 GetEventHandler()->ProcessEvent(cmdEvent
);
1263 /// Delete content if there is a selection, e.g. when pressing a key.
1264 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos
)
1268 long pos
= m_selection
.GetRange().GetStart();
1269 wxRichTextRange range
= m_selection
.GetRange();
1271 // SelectAll causes more to be selected than doing it interactively,
1272 // and causes a new paragraph to be inserted. So for multiline buffers,
1273 // don't delete the final position.
1274 if (range
.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1275 range
.SetEnd(range
.GetEnd()-1);
1277 GetFocusObject()->DeleteRangeWithUndo(range
, this, & GetBuffer());
1278 m_selection
.Reset();
1279 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
1289 /// Keyboard navigation
1293 Left: left one character
1294 Right: right one character
1297 Ctrl-Left: left one word
1298 Ctrl-Right: right one word
1299 Ctrl-Up: previous paragraph start
1300 Ctrl-Down: next start of paragraph
1303 Ctrl-Home: start of document
1304 Ctrl-End: end of document
1305 Page-Up: Up a screen
1306 Page-Down: Down a screen
1310 Ctrl-Alt-PgUp: Start of window
1311 Ctrl-Alt-PgDn: End of window
1312 F8: Start selection mode
1313 Esc: End selection mode
1315 Adding Shift does the above but starts/extends selection.
1320 bool wxRichTextCtrl::KeyboardNavigate(int keyCode
, int flags
)
1322 bool success
= false;
1324 if (keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
)
1326 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1327 success
= WordRight(1, flags
);
1329 success
= MoveRight(1, flags
);
1331 else if (keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
)
1333 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1334 success
= WordLeft(1, flags
);
1336 success
= MoveLeft(1, flags
);
1338 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
)
1340 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1341 success
= MoveToParagraphStart(flags
);
1343 success
= MoveUp(1, flags
);
1345 else if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
)
1347 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1348 success
= MoveToParagraphEnd(flags
);
1350 success
= MoveDown(1, flags
);
1352 else if (keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1354 success
= PageUp(1, flags
);
1356 else if (keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1358 success
= PageDown(1, flags
);
1360 else if (keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
)
1362 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1363 success
= MoveHome(flags
);
1365 success
= MoveToLineStart(flags
);
1367 else if (keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
)
1369 if (flags
& wxRICHTEXT_CTRL_DOWN
)
1370 success
= MoveEnd(flags
);
1372 success
= MoveToLineEnd(flags
);
1377 ScrollIntoView(m_caretPosition
, keyCode
);
1378 SetDefaultStyleToCursorStyle();
1384 /// Extend the selection. Selections are in caret positions.
1385 bool wxRichTextCtrl::ExtendSelection(long oldPos
, long newPos
, int flags
)
1387 if (flags
& wxRICHTEXT_SHIFT_DOWN
)
1389 if (oldPos
== newPos
)
1392 wxRichTextSelection oldSelection
= m_selection
;
1394 m_selection
.SetContainer(GetFocusObject());
1396 wxRichTextRange oldRange
;
1397 if (m_selection
.IsValid())
1398 oldRange
= m_selection
.GetRange();
1400 oldRange
= wxRICHTEXT_NO_SELECTION
;
1401 wxRichTextRange newRange
;
1403 // If not currently selecting, start selecting
1404 if (oldRange
.GetStart() == -2)
1406 m_selectionAnchor
= oldPos
;
1408 if (oldPos
> newPos
)
1409 newRange
.SetRange(newPos
+1, oldPos
);
1411 newRange
.SetRange(oldPos
+1, newPos
);
1415 // Always ensure that the selection range start is greater than
1417 if (newPos
> m_selectionAnchor
)
1418 newRange
.SetRange(m_selectionAnchor
+1, newPos
);
1419 else if (newPos
== m_selectionAnchor
)
1420 newRange
= wxRichTextRange(-2, -2);
1422 newRange
.SetRange(newPos
+1, m_selectionAnchor
);
1425 m_selection
.SetRange(newRange
);
1427 RefreshForSelectionChange(oldSelection
, m_selection
);
1429 if (newRange
.GetStart() > newRange
.GetEnd())
1431 wxLogDebug(wxT("Strange selection range"));
1440 /// Scroll into view, returning true if we scrolled.
1441 /// This takes a _caret_ position.
1442 bool wxRichTextCtrl::ScrollIntoView(long position
, int keyCode
)
1444 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(position
);
1450 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1452 int startXUnits
, startYUnits
;
1453 GetViewStart(& startXUnits
, & startYUnits
);
1454 int startY
= startYUnits
* ppuY
;
1457 GetVirtualSize(& sx
, & sy
);
1463 wxRect rect
= line
->GetRect();
1465 bool scrolled
= false;
1467 wxSize clientSize
= GetClientSize();
1469 int leftMargin
, rightMargin
, topMargin
, bottomMargin
;
1472 wxClientDC
dc(this);
1473 wxRichTextObject::GetTotalMargin(dc
, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin
, rightMargin
,
1474 topMargin
, bottomMargin
);
1476 // clientSize.y -= GetBuffer().GetBottomMargin();
1477 clientSize
.y
-= bottomMargin
;
1479 if (GetWindowStyle() & wxRE_CENTRE_CARET
)
1481 int y
= rect
.y
- GetClientSize().y
/2;
1482 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1483 if (y
>= 0 && (y
+ clientSize
.y
) < GetBuffer().GetCachedSize().y
)
1485 if (startYUnits
!= yUnits
)
1487 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1490 #if !wxRICHTEXT_USE_OWN_CARET
1500 if (keyCode
== WXK_DOWN
|| keyCode
== WXK_NUMPAD_DOWN
||
1501 keyCode
== WXK_RIGHT
|| keyCode
== WXK_NUMPAD_RIGHT
||
1502 keyCode
== WXK_END
|| keyCode
== WXK_NUMPAD_END
||
1503 keyCode
== WXK_PAGEDOWN
|| keyCode
== WXK_NUMPAD_PAGEDOWN
)
1505 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1507 // Make it scroll so this item is at the bottom
1509 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1510 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1512 // If we're still off the screen, scroll another line down
1513 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1516 if (startYUnits
!= yUnits
)
1518 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1522 else if (rect
.y
< (startY
+ GetBuffer().GetTopMargin()))
1524 // Make it scroll so this item is at the top
1526 int y
= rect
.y
- GetBuffer().GetTopMargin();
1527 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1529 if (startYUnits
!= yUnits
)
1531 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1537 else if (keyCode
== WXK_UP
|| keyCode
== WXK_NUMPAD_UP
||
1538 keyCode
== WXK_LEFT
|| keyCode
== WXK_NUMPAD_LEFT
||
1539 keyCode
== WXK_HOME
|| keyCode
== WXK_NUMPAD_HOME
||
1540 keyCode
== WXK_PAGEUP
|| keyCode
== WXK_NUMPAD_PAGEUP
)
1542 if (rect
.y
< (startY
+ GetBuffer().GetBottomMargin()))
1544 // Make it scroll so this item is at the top
1546 int y
= rect
.y
- GetBuffer().GetTopMargin();
1547 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1549 if (startYUnits
!= yUnits
)
1551 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1555 else if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ startY
))
1557 // Make it scroll so this item is at the bottom
1559 int y
= rect
.y
- (clientSize
.y
- rect
.height
);
1560 int yUnits
= (int) (0.5 + ((float) y
)/(float) ppuY
);
1562 // If we're still off the screen, scroll another line down
1563 if ((rect
.y
+ rect
.height
) > (clientSize
.y
+ (yUnits
*ppuY
)))
1566 if (startYUnits
!= yUnits
)
1568 SetScrollbars(ppuX
, ppuY
, sxUnits
, syUnits
, 0, yUnits
);
1574 #if !wxRICHTEXT_USE_OWN_CARET
1582 /// Is the given position visible on the screen?
1583 bool wxRichTextCtrl::IsPositionVisible(long pos
) const
1585 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(pos
-1);
1591 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
1594 GetViewStart(& startX
, & startY
);
1596 startY
= startY
* ppuY
;
1598 wxRect rect
= line
->GetRect();
1599 wxSize clientSize
= GetClientSize();
1600 clientSize
.y
-= GetBuffer().GetBottomMargin();
1602 return (rect
.GetTop() >= (startY
+ GetBuffer().GetTopMargin())) && (rect
.GetBottom() <= (startY
+ clientSize
.y
));
1605 void wxRichTextCtrl::SetCaretPosition(long position
, bool showAtLineStart
)
1607 m_caretPosition
= position
;
1608 m_caretAtLineStart
= showAtLineStart
;
1611 /// Move caret one visual step forward: this may mean setting a flag
1612 /// and keeping the same position if we're going from the end of one line
1613 /// to the start of the next, which may be the exact same caret position.
1614 void wxRichTextCtrl::MoveCaretForward(long oldPosition
)
1616 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1618 // Only do the check if we're not at the end of the paragraph (where things work OK
1620 if (para
&& (oldPosition
!= para
->GetRange().GetEnd() - 1))
1622 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1626 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1628 // We're at the end of a line. See whether we need to
1629 // stay at the same actual caret position but change visual
1630 // position, or not.
1631 if (oldPosition
== lineRange
.GetEnd())
1633 if (m_caretAtLineStart
)
1635 // We're already at the start of the line, so actually move on now.
1636 m_caretPosition
= oldPosition
+ 1;
1637 m_caretAtLineStart
= false;
1641 // We're showing at the end of the line, so keep to
1642 // the same position but indicate that we're to show
1643 // at the start of the next line.
1644 m_caretPosition
= oldPosition
;
1645 m_caretAtLineStart
= true;
1647 SetDefaultStyleToCursorStyle();
1653 SetDefaultStyleToCursorStyle();
1656 /// Move caret one visual step backward: this may mean setting a flag
1657 /// and keeping the same position if we're going from the end of one line
1658 /// to the start of the next, which may be the exact same caret position.
1659 void wxRichTextCtrl::MoveCaretBack(long oldPosition
)
1661 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(oldPosition
);
1663 // Only do the check if we're not at the start of the paragraph (where things work OK
1665 if (para
&& (oldPosition
!= para
->GetRange().GetStart()))
1667 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(oldPosition
);
1671 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1673 // We're at the start of a line. See whether we need to
1674 // stay at the same actual caret position but change visual
1675 // position, or not.
1676 if (oldPosition
== lineRange
.GetStart())
1678 m_caretPosition
= oldPosition
-1;
1679 m_caretAtLineStart
= true;
1682 else if (oldPosition
== lineRange
.GetEnd())
1684 if (m_caretAtLineStart
)
1686 // We're at the start of the line, so keep the same caret position
1687 // but clear the start-of-line flag.
1688 m_caretPosition
= oldPosition
;
1689 m_caretAtLineStart
= false;
1693 // We're showing at the end of the line, so go back
1694 // to the previous character position.
1695 m_caretPosition
= oldPosition
- 1;
1697 SetDefaultStyleToCursorStyle();
1703 SetDefaultStyleToCursorStyle();
1707 bool wxRichTextCtrl::MoveRight(int noPositions
, int flags
)
1709 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
1711 if (m_caretPosition
+ noPositions
< endPos
)
1713 long oldPos
= m_caretPosition
;
1714 long newPos
= m_caretPosition
+ noPositions
;
1716 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1720 // Determine by looking at oldPos and m_caretPosition whether
1721 // we moved from the end of a line to the start of the next line, in which case
1722 // we want to adjust the caret position such that it is positioned at the
1723 // start of the next line, rather than jumping past the first character of the
1725 if (noPositions
== 1 && !extendSel
)
1726 MoveCaretForward(oldPos
);
1728 SetCaretPosition(newPos
);
1731 SetDefaultStyleToCursorStyle();
1740 bool wxRichTextCtrl::MoveLeft(int noPositions
, int flags
)
1744 if (m_caretPosition
> startPos
- noPositions
+ 1)
1746 long oldPos
= m_caretPosition
;
1747 long newPos
= m_caretPosition
- noPositions
;
1748 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1752 if (noPositions
== 1 && !extendSel
)
1753 MoveCaretBack(oldPos
);
1755 SetCaretPosition(newPos
);
1758 SetDefaultStyleToCursorStyle();
1766 // Find the caret position for the combination of hit-test flags and character position.
1767 // Returns the caret position and also an indication of where to place the caret (caretLineStart)
1768 // since this is ambiguous (same position used for end of line and start of next).
1769 long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position
, int hitTestFlags
, wxRichTextParagraphLayoutBox
* container
,
1770 bool& caretLineStart
)
1772 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1773 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1774 // so we view the caret at the start of the line.
1775 caretLineStart
= false;
1776 long caretPosition
= position
;
1778 if (hitTestFlags
& wxRICHTEXT_HITTEST_BEFORE
)
1780 wxRichTextLine
* thisLine
= container
->GetLineAtPosition(position
-1);
1781 wxRichTextRange lineRange
;
1783 lineRange
= thisLine
->GetAbsoluteRange();
1785 if (thisLine
&& (position
-1) == lineRange
.GetEnd())
1788 caretLineStart
= true;
1792 wxRichTextParagraph
* para
= container
->GetParagraphAtPosition(position
);
1793 if (para
&& para
->GetRange().GetStart() == position
)
1797 return caretPosition
;
1801 bool wxRichTextCtrl::MoveUp(int noLines
, int flags
)
1803 return MoveDown(- noLines
, flags
);
1807 bool wxRichTextCtrl::MoveDown(int noLines
, int flags
)
1812 long lineNumber
= GetFocusObject()->GetVisibleLineNumber(m_caretPosition
, true, m_caretAtLineStart
);
1813 wxPoint pt
= GetCaret()->GetPosition();
1814 long newLine
= lineNumber
+ noLines
;
1815 bool notInThisObject
= false;
1817 if (lineNumber
!= -1)
1821 long lastLine
= GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
1822 if (newLine
> lastLine
)
1823 notInThisObject
= true;
1828 notInThisObject
= true;
1832 wxRichTextParagraphLayoutBox
* container
= GetFocusObject();
1833 int hitTestFlags
= wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS
;
1835 if (notInThisObject
)
1837 // If we know we're navigating out of the current object,
1838 // try to find an object anywhere in the buffer at the new position (up or down a bit)
1839 container
= & GetBuffer();
1840 hitTestFlags
&= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
;
1842 if (noLines
> 0) // going down
1844 pt
.y
= GetFocusObject()->GetPosition().y
+ GetFocusObject()->GetCachedSize().y
+ 2;
1848 pt
.y
= GetFocusObject()->GetPosition().y
- 2;
1853 wxRichTextLine
* lineObj
= GetFocusObject()->GetLineForVisibleLineNumber(newLine
);
1855 pt
.y
= lineObj
->GetAbsolutePosition().y
+ 2;
1861 wxClientDC
dc(this);
1863 dc
.SetFont(GetFont());
1865 wxRichTextObject
* hitObj
= NULL
;
1866 wxRichTextObject
* contextObj
= NULL
;
1867 int hitTest
= container
->HitTest(dc
, pt
, newPos
, & hitObj
, & contextObj
, hitTestFlags
);
1870 ((hitTest
& wxRICHTEXT_HITTEST_NONE
) == 0) &&
1871 (! (hitObj
== (& m_buffer
) && ((hitTest
& wxRICHTEXT_HITTEST_OUTSIDE
) != 0))) // outside the buffer counts as 'do nothing'
1874 if (notInThisObject
)
1876 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
1877 if (actualContainer
&& actualContainer
!= GetFocusObject() && actualContainer
->AcceptsFocus())
1879 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
1881 container
= actualContainer
;
1885 bool caretLineStart
= true;
1886 long caretPosition
= FindCaretPositionForCharacterPosition(newPos
, hitTest
, container
, caretLineStart
);
1887 long newSelEnd
= caretPosition
;
1890 if (notInThisObject
)
1893 extendSel
= ExtendSelection(m_caretPosition
, newSelEnd
, flags
);
1898 SetCaretPosition(caretPosition
, caretLineStart
);
1900 SetDefaultStyleToCursorStyle();
1908 /// Move to the end of the paragraph
1909 bool wxRichTextCtrl::MoveToParagraphEnd(int flags
)
1911 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1914 long newPos
= para
->GetRange().GetEnd() - 1;
1915 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1919 SetCaretPosition(newPos
);
1921 SetDefaultStyleToCursorStyle();
1929 /// Move to the start of the paragraph
1930 bool wxRichTextCtrl::MoveToParagraphStart(int flags
)
1932 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(m_caretPosition
, true);
1935 long newPos
= para
->GetRange().GetStart() - 1;
1936 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1940 SetCaretPosition(newPos
);
1942 SetDefaultStyleToCursorStyle();
1950 /// Move to the end of the line
1951 bool wxRichTextCtrl::MoveToLineEnd(int flags
)
1953 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1957 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1958 long newPos
= lineRange
.GetEnd();
1959 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1963 SetCaretPosition(newPos
);
1965 SetDefaultStyleToCursorStyle();
1973 /// Move to the start of the line
1974 bool wxRichTextCtrl::MoveToLineStart(int flags
)
1976 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
1979 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
1980 long newPos
= lineRange
.GetStart()-1;
1982 bool extendSel
= ExtendSelection(m_caretPosition
, newPos
, flags
);
1986 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(line
);
1988 SetCaretPosition(newPos
, para
->GetRange().GetStart() != lineRange
.GetStart());
1990 SetDefaultStyleToCursorStyle();
1998 /// Move to the start of the buffer
1999 bool wxRichTextCtrl::MoveHome(int flags
)
2001 if (m_caretPosition
!= -1)
2003 bool extendSel
= ExtendSelection(m_caretPosition
, -1, flags
);
2007 SetCaretPosition(-1);
2009 SetDefaultStyleToCursorStyle();
2017 /// Move to the end of the buffer
2018 bool wxRichTextCtrl::MoveEnd(int flags
)
2020 long endPos
= GetFocusObject()->GetOwnRange().GetEnd()-1;
2022 if (m_caretPosition
!= endPos
)
2024 bool extendSel
= ExtendSelection(m_caretPosition
, endPos
, flags
);
2028 SetCaretPosition(endPos
);
2030 SetDefaultStyleToCursorStyle();
2038 /// Move noPages pages up
2039 bool wxRichTextCtrl::PageUp(int noPages
, int flags
)
2041 return PageDown(- noPages
, flags
);
2044 /// Move noPages pages down
2045 bool wxRichTextCtrl::PageDown(int noPages
, int flags
)
2047 // Calculate which line occurs noPages * screen height further down.
2048 wxRichTextLine
* line
= GetVisibleLineForCaretPosition(m_caretPosition
);
2051 wxSize clientSize
= GetClientSize();
2052 int newY
= line
->GetAbsolutePosition().y
+ noPages
*clientSize
.y
;
2054 wxRichTextLine
* newLine
= GetFocusObject()->GetLineAtYPosition(newY
);
2057 wxRichTextRange lineRange
= newLine
->GetAbsoluteRange();
2058 long pos
= lineRange
.GetStart()-1;
2059 if (pos
!= m_caretPosition
)
2061 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphForLine(newLine
);
2063 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2067 SetCaretPosition(pos
, para
->GetRange().GetStart() != lineRange
.GetStart());
2069 SetDefaultStyleToCursorStyle();
2079 static bool wxRichTextCtrlIsWhitespace(const wxString
& str
)
2081 return str
== wxT(" ") || str
== wxT("\t");
2084 // Finds the caret position for the next word
2085 long wxRichTextCtrl::FindNextWordPosition(int direction
) const
2087 long endPos
= GetFocusObject()->GetOwnRange().GetEnd();
2091 long i
= m_caretPosition
+1+direction
; // +1 for conversion to character pos
2093 // First skip current text to space
2094 while (i
< endPos
&& i
> -1)
2096 // i is in character, not caret positions
2097 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2098 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2099 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2103 else if (!wxRichTextCtrlIsWhitespace(text
) && !text
.empty())
2110 while (i
< endPos
&& i
> -1)
2112 // i is in character, not caret positions
2113 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2114 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2115 if (line
&& (i
== line
->GetAbsoluteRange().GetEnd()))
2116 return wxMax(-1, i
);
2118 if (text
.empty()) // End of paragraph, or maybe an image
2119 return wxMax(-1, i
- 1);
2120 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2124 // Convert to caret position
2125 return wxMax(-1, i
- 1);
2134 long i
= m_caretPosition
;
2136 // First skip white space
2137 while (i
< endPos
&& i
> -1)
2139 // i is in character, not caret positions
2140 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2141 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2143 if (text
.empty() || (line
&& (i
== line
->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
2145 else if (wxRichTextCtrlIsWhitespace(text
) || text
.empty())
2150 // Next skip current text to space
2151 while (i
< endPos
&& i
> -1)
2153 // i is in character, not caret positions
2154 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(i
, i
));
2155 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(i
, false);
2156 if (line
&& line
->GetAbsoluteRange().GetStart() == i
)
2159 if (!wxRichTextCtrlIsWhitespace(text
) /* && !text.empty() */)
2172 /// Move n words left
2173 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n
), int flags
)
2175 long pos
= FindNextWordPosition(-1);
2176 if (pos
!= m_caretPosition
)
2178 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2180 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2184 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2186 SetDefaultStyleToCursorStyle();
2194 /// Move n words right
2195 bool wxRichTextCtrl::WordRight(int WXUNUSED(n
), int flags
)
2197 long pos
= FindNextWordPosition(1);
2198 if (pos
!= m_caretPosition
)
2200 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
, true);
2202 bool extendSel
= ExtendSelection(m_caretPosition
, pos
, flags
);
2206 SetCaretPosition(pos
, para
->GetRange().GetStart() != pos
);
2208 SetDefaultStyleToCursorStyle();
2217 void wxRichTextCtrl::OnSize(wxSizeEvent
& event
)
2219 // Only do sizing optimization for large buffers
2220 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold
)
2222 m_fullLayoutRequired
= true;
2223 m_fullLayoutTime
= wxGetLocalTimeMillis();
2224 m_fullLayoutSavedPosition
= GetFirstVisiblePosition();
2225 LayoutContent(true /* onlyVisibleRect */);
2228 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2230 #if wxRICHTEXT_BUFFERED_PAINTING
2237 // Force any pending layout due to large buffer
2238 void wxRichTextCtrl::ForceDelayedLayout()
2240 if (m_fullLayoutRequired
)
2242 m_fullLayoutRequired
= false;
2243 m_fullLayoutTime
= 0;
2244 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2245 ShowPosition(m_fullLayoutSavedPosition
);
2251 /// Idle-time processing
2252 void wxRichTextCtrl::OnIdle(wxIdleEvent
& event
)
2254 #if wxRICHTEXT_USE_OWN_CARET
2255 if (((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2257 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate(false);
2263 const int layoutInterval
= wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL
;
2265 if (m_fullLayoutRequired
&& (wxGetLocalTimeMillis() > (m_fullLayoutTime
+ layoutInterval
)))
2267 m_fullLayoutRequired
= false;
2268 m_fullLayoutTime
= 0;
2269 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
2270 ShowPosition(m_fullLayoutSavedPosition
);
2274 if (m_caretPositionForDefaultStyle
!= -2)
2276 // If the caret position has changed, no longer reflect the default style
2278 if (GetCaretPosition() != m_caretPositionForDefaultStyle
)
2279 m_caretPositionForDefaultStyle
= -2;
2286 void wxRichTextCtrl::OnScroll(wxScrollWinEvent
& event
)
2288 #if wxRICHTEXT_USE_OWN_CARET
2289 if (!((wxRichTextCaret
*) GetCaret())->GetNeedsUpdate())
2292 ((wxRichTextCaret
*) GetCaret())->SetNeedsUpdate();
2299 /// Set up scrollbars, e.g. after a resize
2300 void wxRichTextCtrl::SetupScrollbars(bool atTop
)
2305 if (GetBuffer().IsEmpty())
2307 SetScrollbars(0, 0, 0, 0, 0, 0);
2311 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2312 // of pixels. See e.g. wxVScrolledWindow for ideas.
2313 int pixelsPerUnit
= 5;
2314 wxSize clientSize
= GetClientSize();
2316 int maxHeight
= GetBuffer().GetCachedSize().y
+ GetBuffer().GetTopMargin();
2318 // Round up so we have at least maxHeight pixels
2319 int unitsY
= (int) (((float)maxHeight
/(float)pixelsPerUnit
) + 0.5);
2321 int startX
= 0, startY
= 0;
2323 GetViewStart(& startX
, & startY
);
2325 int maxPositionX
= 0;
2326 int maxPositionY
= (int) ((((float)(wxMax((unitsY
*pixelsPerUnit
) - clientSize
.y
, 0)))/((float)pixelsPerUnit
)) + 0.5);
2328 int newStartX
= wxMin(maxPositionX
, startX
);
2329 int newStartY
= wxMin(maxPositionY
, startY
);
2331 int oldPPUX
, oldPPUY
;
2332 int oldStartX
, oldStartY
;
2333 int oldVirtualSizeX
= 0, oldVirtualSizeY
= 0;
2334 GetScrollPixelsPerUnit(& oldPPUX
, & oldPPUY
);
2335 GetViewStart(& oldStartX
, & oldStartY
);
2336 GetVirtualSize(& oldVirtualSizeX
, & oldVirtualSizeY
);
2338 oldVirtualSizeY
/= oldPPUY
;
2340 if (oldPPUX
== 0 && oldPPUY
== pixelsPerUnit
&& oldVirtualSizeY
== unitsY
&& oldStartX
== newStartX
&& oldStartY
== newStartY
)
2343 // Don't set scrollbars if there were none before, and there will be none now.
2344 if (oldPPUY
!= 0 && (oldVirtualSizeY
*oldPPUY
< clientSize
.y
) && (unitsY
*pixelsPerUnit
< clientSize
.y
))
2347 // Move to previous scroll position if
2349 SetScrollbars(0, pixelsPerUnit
, 0, unitsY
, newStartX
, newStartY
);
2352 /// Paint the background
2353 void wxRichTextCtrl::PaintBackground(wxDC
& dc
)
2355 wxColour backgroundColour
= GetBackgroundColour();
2356 if (!backgroundColour
.IsOk())
2357 backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
2359 // Clear the background
2360 dc
.SetBrush(wxBrush(backgroundColour
));
2361 dc
.SetPen(*wxTRANSPARENT_PEN
);
2362 wxRect
windowRect(GetClientSize());
2363 windowRect
.x
-= 2; windowRect
.y
-= 2;
2364 windowRect
.width
+= 4; windowRect
.height
+= 4;
2366 // We need to shift the rectangle to take into account
2367 // scrolling. Converting device to logical coordinates.
2368 CalcUnscrolledPosition(windowRect
.x
, windowRect
.y
, & windowRect
.x
, & windowRect
.y
);
2369 dc
.DrawRectangle(windowRect
);
2372 #if wxRICHTEXT_BUFFERED_PAINTING
2373 /// Recreate buffer bitmap if necessary
2374 bool wxRichTextCtrl::RecreateBuffer(const wxSize
& size
)
2377 if (sz
== wxDefaultSize
)
2378 sz
= GetClientSize();
2380 if (sz
.x
< 1 || sz
.y
< 1)
2383 if (!m_bufferBitmap
.IsOk() || m_bufferBitmap
.GetWidth() < sz
.x
|| m_bufferBitmap
.GetHeight() < sz
.y
)
2384 m_bufferBitmap
= wxBitmap(sz
.x
, sz
.y
);
2385 return m_bufferBitmap
.IsOk();
2389 // ----------------------------------------------------------------------------
2390 // file IO functions
2391 // ----------------------------------------------------------------------------
2393 bool wxRichTextCtrl::DoLoadFile(const wxString
& filename
, int fileType
)
2395 bool success
= GetBuffer().LoadFile(filename
, (wxRichTextFileType
)fileType
);
2397 m_filename
= filename
;
2400 SetInsertionPoint(0);
2403 SetupScrollbars(true);
2405 wxTextCtrl::SendTextUpdatedEvent(this);
2411 wxLogError(_("File couldn't be loaded."));
2417 bool wxRichTextCtrl::DoSaveFile(const wxString
& filename
, int fileType
)
2419 if (GetBuffer().SaveFile(filename
, (wxRichTextFileType
)fileType
))
2421 m_filename
= filename
;
2428 wxLogError(_("The text couldn't be saved."));
2433 // ----------------------------------------------------------------------------
2434 // wxRichTextCtrl specific functionality
2435 // ----------------------------------------------------------------------------
2437 /// Add a new paragraph of text to the end of the buffer
2438 wxRichTextRange
wxRichTextCtrl::AddParagraph(const wxString
& text
)
2440 wxRichTextRange range
= GetFocusObject()->AddParagraph(text
);
2441 GetBuffer().Invalidate();
2447 wxRichTextRange
wxRichTextCtrl::AddImage(const wxImage
& image
)
2449 wxRichTextRange range
= GetFocusObject()->AddImage(image
);
2450 GetBuffer().Invalidate();
2455 // ----------------------------------------------------------------------------
2456 // selection and ranges
2457 // ----------------------------------------------------------------------------
2459 void wxRichTextCtrl::SelectAll()
2461 SetSelection(-1, -1);
2465 void wxRichTextCtrl::SelectNone()
2467 if (m_selection
.IsValid())
2469 wxRichTextSelection oldSelection
= m_selection
;
2471 m_selection
.Reset();
2473 RefreshForSelectionChange(oldSelection
, m_selection
);
2475 m_selectionAnchor
= -2;
2476 m_selectionAnchorObject
= NULL
;
2477 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2480 static bool wxIsWordDelimiter(const wxString
& text
)
2482 return !text
.IsEmpty() && !wxIsalnum(text
[0]);
2485 /// Select the word at the given character position
2486 bool wxRichTextCtrl::SelectWord(long position
)
2488 if (position
< 0 || position
> GetFocusObject()->GetOwnRange().GetEnd())
2491 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(position
);
2495 if (position
== para
->GetRange().GetEnd())
2498 long positionStart
= position
;
2499 long positionEnd
= position
;
2501 for (positionStart
= position
; positionStart
>= para
->GetRange().GetStart(); positionStart
--)
2503 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart
, positionStart
));
2504 if (wxIsWordDelimiter(text
))
2510 if (positionStart
< para
->GetRange().GetStart())
2511 positionStart
= para
->GetRange().GetStart();
2513 for (positionEnd
= position
; positionEnd
< para
->GetRange().GetEnd(); positionEnd
++)
2515 wxString text
= GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd
, positionEnd
));
2516 if (wxIsWordDelimiter(text
))
2522 if (positionEnd
>= para
->GetRange().GetEnd())
2523 positionEnd
= para
->GetRange().GetEnd();
2525 if (positionEnd
< positionStart
)
2528 SetSelection(positionStart
, positionEnd
+1);
2530 if (positionStart
>= 0)
2532 MoveCaret(positionStart
-1, true);
2533 SetDefaultStyleToCursorStyle();
2539 wxString
wxRichTextCtrl::GetStringSelection() const
2542 GetSelection(&from
, &to
);
2544 return GetRange(from
, to
);
2547 // ----------------------------------------------------------------------------
2549 // ----------------------------------------------------------------------------
2551 wxTextCtrlHitTestResult
2552 wxRichTextCtrl::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
2554 // implement in terms of the other overload as the native ports typically
2555 // can get the position and not (x, y) pair directly (although wxUniv
2556 // directly gets x and y -- and so overrides this method as well)
2558 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
2560 if ( rc
!= wxTE_HT_UNKNOWN
)
2562 PositionToXY(pos
, x
, y
);
2568 wxTextCtrlHitTestResult
2569 wxRichTextCtrl::HitTest(const wxPoint
& pt
,
2572 wxClientDC
dc((wxRichTextCtrl
*) this);
2573 ((wxRichTextCtrl
*)this)->PrepareDC(dc
);
2575 // Buffer uses logical position (relative to start of buffer)
2577 wxPoint pt2
= GetLogicalPoint(pt
);
2579 wxRichTextObject
* hitObj
= NULL
;
2580 wxRichTextObject
* contextObj
= NULL
;
2581 int hit
= ((wxRichTextCtrl
*)this)->GetFocusObject()->HitTest(dc
, pt2
, *pos
, & hitObj
, & contextObj
, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS
);
2583 if ((hit
& wxRICHTEXT_HITTEST_BEFORE
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2584 return wxTE_HT_BEFORE
;
2585 else if ((hit
& wxRICHTEXT_HITTEST_AFTER
) && (hit
& wxRICHTEXT_HITTEST_OUTSIDE
))
2586 return wxTE_HT_BEYOND
;
2587 else if (hit
& (wxRICHTEXT_HITTEST_BEFORE
|wxRICHTEXT_HITTEST_AFTER
))
2588 return wxTE_HT_ON_TEXT
;
2590 return wxTE_HT_UNKNOWN
;
2593 // ----------------------------------------------------------------------------
2594 // set/get the controls text
2595 // ----------------------------------------------------------------------------
2597 wxString
wxRichTextCtrl::DoGetValue() const
2599 return GetBuffer().GetText();
2602 wxString
wxRichTextCtrl::GetRange(long from
, long to
) const
2604 // Public API for range is different from internals
2605 return GetFocusObject()->GetTextForRange(wxRichTextRange(from
, to
-1));
2608 void wxRichTextCtrl::DoSetValue(const wxString
& value
, int flags
)
2610 // Don't call Clear here, since it always sends a text updated event
2611 m_buffer
.ResetAndClearCommands();
2612 m_buffer
.Invalidate(wxRICHTEXT_ALL
);
2613 m_caretPosition
= -1;
2614 m_caretPositionForDefaultStyle
= -2;
2615 m_caretAtLineStart
= false;
2616 m_selection
.Reset();
2617 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
2627 if (!value
.IsEmpty())
2629 // Remove empty paragraph
2630 GetBuffer().Clear();
2631 DoWriteText(value
, flags
);
2633 // for compatibility, don't move the cursor when doing SetValue()
2634 SetInsertionPoint(0);
2638 // still send an event for consistency
2639 if (flags
& SetValue_SendEvent
)
2640 wxTextCtrl::SendTextUpdatedEvent(this);
2645 void wxRichTextCtrl::WriteText(const wxString
& value
)
2650 void wxRichTextCtrl::DoWriteText(const wxString
& value
, int flags
)
2652 wxString valueUnix
= wxTextFile::Translate(value
, wxTextFileType_Unix
);
2654 GetFocusObject()->InsertTextWithUndo(m_caretPosition
+1, valueUnix
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2656 if ( flags
& SetValue_SendEvent
)
2657 wxTextCtrl::SendTextUpdatedEvent(this);
2660 void wxRichTextCtrl::AppendText(const wxString
& text
)
2662 SetInsertionPointEnd();
2667 /// Write an image at the current insertion point
2668 bool wxRichTextCtrl::WriteImage(const wxImage
& image
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2670 wxRichTextImageBlock imageBlock
;
2672 wxImage image2
= image
;
2673 if (imageBlock
.MakeImageBlock(image2
, bitmapType
))
2674 return WriteImage(imageBlock
, textAttr
);
2679 bool wxRichTextCtrl::WriteImage(const wxString
& filename
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2681 wxRichTextImageBlock imageBlock
;
2684 if (imageBlock
.MakeImageBlock(filename
, bitmapType
, image
, false))
2685 return WriteImage(imageBlock
, textAttr
);
2690 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock
& imageBlock
, const wxRichTextAttr
& textAttr
)
2692 return GetFocusObject()->InsertImageWithUndo(m_caretPosition
+1, imageBlock
, this, & GetBuffer(), 0, textAttr
);
2695 bool wxRichTextCtrl::WriteImage(const wxBitmap
& bitmap
, wxBitmapType bitmapType
, const wxRichTextAttr
& textAttr
)
2699 wxRichTextImageBlock imageBlock
;
2701 wxImage image
= bitmap
.ConvertToImage();
2702 if (image
.IsOk() && imageBlock
.MakeImageBlock(image
, bitmapType
))
2703 return WriteImage(imageBlock
, textAttr
);
2709 // Write a text box at the current insertion point.
2710 wxRichTextBox
* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr
& textAttr
)
2712 wxRichTextBox
* textBox
= new wxRichTextBox
;
2713 textBox
->SetAttributes(textAttr
);
2714 textBox
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2715 textBox
->AddParagraph(wxEmptyString
);
2716 textBox
->SetParent(NULL
);
2718 // The object returned is the one actually inserted into the buffer,
2719 // while the original one is deleted.
2720 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(m_caretPosition
+1, textBox
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2721 wxRichTextBox
* box
= wxDynamicCast(obj
, wxRichTextBox
);
2725 // Write a table at the current insertion point, returning the table.
2726 wxRichTextTable
* wxRichTextCtrl::WriteTable(int rows
, int cols
, const wxRichTextAttr
& tableAttr
, const wxRichTextAttr
& cellAttr
)
2728 wxASSERT(rows
> 0 && cols
> 0);
2730 if (rows
<= 0 || cols
<= 0)
2733 wxRichTextTable
* table
= new wxRichTextTable
;
2734 table
->SetAttributes(tableAttr
);
2735 table
->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2737 table
->CreateTable(rows
, cols
);
2739 table
->SetParent(NULL
);
2742 for (j
= 0; j
< rows
; j
++)
2744 for (i
= 0; i
< cols
; i
++)
2746 table
->GetCell(j
, i
)->GetAttributes() = cellAttr
;
2750 // The object returned is the one actually inserted into the buffer,
2751 // while the original one is deleted.
2752 wxRichTextObject
* obj
= GetFocusObject()->InsertObjectWithUndo(m_caretPosition
+1, table
, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2753 wxRichTextTable
* tableResult
= wxDynamicCast(obj
, wxRichTextTable
);
2758 /// Insert a newline (actually paragraph) at the current insertion point.
2759 bool wxRichTextCtrl::Newline()
2761 return GetFocusObject()->InsertNewlineWithUndo(m_caretPosition
+1, this, & GetBuffer(), wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE
);
2764 /// Insert a line break at the current insertion point.
2765 bool wxRichTextCtrl::LineBreak()
2768 text
= wxRichTextLineBreakChar
;
2769 return GetFocusObject()->InsertTextWithUndo(m_caretPosition
+1, text
, this, & GetBuffer());
2772 // ----------------------------------------------------------------------------
2773 // Clipboard operations
2774 // ----------------------------------------------------------------------------
2776 void wxRichTextCtrl::Copy()
2780 wxRichTextRange range
= GetInternalSelectionRange();
2781 GetBuffer().CopyToClipboard(range
);
2785 void wxRichTextCtrl::Cut()
2789 wxRichTextRange range
= GetInternalSelectionRange();
2790 GetBuffer().CopyToClipboard(range
);
2792 DeleteSelectedContent();
2798 void wxRichTextCtrl::Paste()
2802 BeginBatchUndo(_("Paste"));
2804 long newPos
= m_caretPosition
;
2805 DeleteSelectedContent(& newPos
);
2807 GetBuffer().PasteFromClipboard(newPos
);
2813 void wxRichTextCtrl::DeleteSelection()
2815 if (CanDeleteSelection())
2817 DeleteSelectedContent();
2821 bool wxRichTextCtrl::HasSelection() const
2823 return (m_selection
.IsValid() && m_selection
.GetContainer() == GetFocusObject());
2826 bool wxRichTextCtrl::HasUnfocusedSelection() const
2828 return m_selection
.IsValid();
2831 bool wxRichTextCtrl::CanCopy() const
2833 // Can copy if there's a selection
2834 return HasSelection();
2837 bool wxRichTextCtrl::CanCut() const
2839 return HasSelection() && IsEditable();
2842 bool wxRichTextCtrl::CanPaste() const
2844 if ( !IsEditable() )
2847 return GetBuffer().CanPasteFromClipboard();
2850 bool wxRichTextCtrl::CanDeleteSelection() const
2852 return HasSelection() && IsEditable();
2856 // ----------------------------------------------------------------------------
2858 // ----------------------------------------------------------------------------
2860 void wxRichTextCtrl::SetContextMenu(wxMenu
* menu
)
2862 if (m_contextMenu
&& m_contextMenu
!= menu
)
2863 delete m_contextMenu
;
2864 m_contextMenu
= menu
;
2867 void wxRichTextCtrl::SetEditable(bool editable
)
2869 m_editable
= editable
;
2872 void wxRichTextCtrl::SetInsertionPoint(long pos
)
2876 m_caretPosition
= pos
- 1;
2880 SetDefaultStyleToCursorStyle();
2883 void wxRichTextCtrl::SetInsertionPointEnd()
2885 long pos
= GetLastPosition();
2886 SetInsertionPoint(pos
);
2889 long wxRichTextCtrl::GetInsertionPoint() const
2891 return m_caretPosition
+1;
2894 wxTextPos
wxRichTextCtrl::GetLastPosition() const
2896 return GetFocusObject()->GetOwnRange().GetEnd();
2899 // If the return values from and to are the same, there is no
2901 void wxRichTextCtrl::GetSelection(long* from
, long* to
) const
2903 if (m_selection
.IsValid())
2905 *from
= m_selection
.GetRange().GetStart();
2906 *to
= m_selection
.GetRange().GetEnd();
2916 bool wxRichTextCtrl::IsEditable() const
2921 // ----------------------------------------------------------------------------
2923 // ----------------------------------------------------------------------------
2925 void wxRichTextCtrl::SetSelection(long from
, long to
)
2927 // if from and to are both -1, it means (in wxWidgets) that all text should
2929 if ( (from
== -1) && (to
== -1) )
2932 to
= GetLastPosition()+1;
2941 wxRichTextSelection oldSelection
= m_selection
;
2943 m_selectionAnchor
= from
-1;
2944 m_selectionAnchorObject
= NULL
;
2945 m_selection
.Set(wxRichTextRange(from
, to
-1), GetFocusObject());
2947 m_caretPosition
= wxMax(-1, to
-1);
2949 RefreshForSelectionChange(oldSelection
, m_selection
);
2954 // ----------------------------------------------------------------------------
2956 // ----------------------------------------------------------------------------
2958 void wxRichTextCtrl::Replace(long from
, long to
,
2959 const wxString
& value
)
2961 BeginBatchUndo(_("Replace"));
2963 SetSelection(from
, to
);
2965 wxRichTextAttr attr
= GetDefaultStyle();
2967 DeleteSelectedContent();
2969 SetDefaultStyle(attr
);
2971 DoWriteText(value
, SetValue_SelectionOnly
);
2976 void wxRichTextCtrl::Remove(long from
, long to
)
2980 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from
, to
-1), this, & GetBuffer());
2987 bool wxRichTextCtrl::IsModified() const
2989 return m_buffer
.IsModified();
2992 void wxRichTextCtrl::MarkDirty()
2994 m_buffer
.Modify(true);
2997 void wxRichTextCtrl::DiscardEdits()
2999 m_caretPositionForDefaultStyle
= -2;
3000 m_buffer
.Modify(false);
3001 m_buffer
.GetCommandProcessor()->ClearCommands();
3004 int wxRichTextCtrl::GetNumberOfLines() const
3006 return GetFocusObject()->GetParagraphCount();
3009 // ----------------------------------------------------------------------------
3010 // Positions <-> coords
3011 // ----------------------------------------------------------------------------
3013 long wxRichTextCtrl::XYToPosition(long x
, long y
) const
3015 return GetFocusObject()->XYToPosition(x
, y
);
3018 bool wxRichTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
3020 return GetFocusObject()->PositionToXY(pos
, x
, y
);
3023 // ----------------------------------------------------------------------------
3025 // ----------------------------------------------------------------------------
3027 void wxRichTextCtrl::ShowPosition(long pos
)
3029 if (!IsPositionVisible(pos
))
3030 ScrollIntoView(pos
-1, WXK_DOWN
);
3033 int wxRichTextCtrl::GetLineLength(long lineNo
) const
3035 return GetFocusObject()->GetParagraphLength(lineNo
);
3038 wxString
wxRichTextCtrl::GetLineText(long lineNo
) const
3040 return GetFocusObject()->GetParagraphText(lineNo
);
3043 // ----------------------------------------------------------------------------
3045 // ----------------------------------------------------------------------------
3047 void wxRichTextCtrl::Undo()
3051 GetCommandProcessor()->Undo();
3055 void wxRichTextCtrl::Redo()
3059 GetCommandProcessor()->Redo();
3063 bool wxRichTextCtrl::CanUndo() const
3065 return GetCommandProcessor()->CanUndo() && IsEditable();
3068 bool wxRichTextCtrl::CanRedo() const
3070 return GetCommandProcessor()->CanRedo() && IsEditable();
3073 // ----------------------------------------------------------------------------
3074 // implementation details
3075 // ----------------------------------------------------------------------------
3077 void wxRichTextCtrl::Command(wxCommandEvent
& event
)
3079 SetValue(event
.GetString());
3080 GetEventHandler()->ProcessEvent(event
);
3083 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
3085 // By default, load the first file into the text window.
3086 if (event
.GetNumberOfFiles() > 0)
3088 LoadFile(event
.GetFiles()[0]);
3092 wxSize
wxRichTextCtrl::DoGetBestSize() const
3094 return wxSize(10, 10);
3097 // ----------------------------------------------------------------------------
3098 // standard handlers for standard edit menu events
3099 // ----------------------------------------------------------------------------
3101 void wxRichTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
3106 void wxRichTextCtrl::OnClear(wxCommandEvent
& WXUNUSED(event
))
3111 void wxRichTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
3116 void wxRichTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
3121 void wxRichTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
3126 void wxRichTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
3131 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
3133 event
.Enable( CanCut() );
3136 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
3138 event
.Enable( CanCopy() );
3141 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent
& event
)
3143 event
.Enable( CanDeleteSelection() );
3146 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
3148 event
.Enable( CanPaste() );
3151 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
3153 event
.Enable( CanUndo() );
3154 event
.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3157 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
3159 event
.Enable( CanRedo() );
3160 event
.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3163 void wxRichTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
3165 if (GetLastPosition() > 0)
3169 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
3171 event
.Enable(GetLastPosition() > 0);
3174 void wxRichTextCtrl::OnProperties(wxCommandEvent
& event
)
3176 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3177 if (idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount())
3179 wxRichTextObject
* obj
= m_contextMenuPropertiesInfo
.GetObject(idx
);
3180 if (obj
&& obj
->CanEditProperties())
3181 obj
->EditProperties(this, & GetBuffer());
3183 m_contextMenuPropertiesInfo
.Clear();
3187 void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent
& event
)
3189 int idx
= event
.GetId() - wxID_RICHTEXT_PROPERTIES1
;
3190 event
.Enable(idx
>= 0 && idx
< m_contextMenuPropertiesInfo
.GetCount());
3193 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
3195 if (event
.GetEventObject() != this)
3201 wxClientDC
dc(this);
3203 dc
.SetFont(GetFont());
3206 wxPoint pt
= event
.GetPosition();
3207 wxPoint logicalPt
= GetLogicalPoint(ScreenToClient(pt
));
3208 wxRichTextObject
* hitObj
= NULL
;
3209 wxRichTextObject
* contextObj
= NULL
;
3210 int hit
= GetFocusObject()->HitTest(dc
, logicalPt
, position
, & hitObj
, & contextObj
);
3212 m_contextMenuPropertiesInfo
.Clear();
3214 if (hit
== wxRICHTEXT_HITTEST_ON
|| hit
== wxRICHTEXT_HITTEST_BEFORE
|| hit
== wxRICHTEXT_HITTEST_AFTER
)
3216 wxRichTextParagraphLayoutBox
* actualContainer
= wxDynamicCast(contextObj
, wxRichTextParagraphLayoutBox
);
3217 if (hitObj
&& actualContainer
)
3219 if (actualContainer
->AcceptsFocus())
3221 SetFocusObject(actualContainer
, false /* don't set caret position yet */);
3222 SetCaretPositionAfterClick(actualContainer
, position
, hit
);
3225 m_contextMenuPropertiesInfo
.AddItems(actualContainer
, hitObj
);
3228 m_contextMenuPropertiesInfo
.AddItems(GetFocusObject(), NULL
);
3232 m_contextMenuPropertiesInfo
.AddItems(GetFocusObject(), NULL
);
3237 m_contextMenuPropertiesInfo
.AddMenuItems(m_contextMenu
);
3238 PopupMenu(m_contextMenu
);
3242 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
3244 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), wxRichTextAttr(style
));
3247 bool wxRichTextCtrl::SetStyle(long start
, long end
, const wxRichTextAttr
& style
)
3249 return GetFocusObject()->SetStyle(wxRichTextRange(start
, end
-1), style
);
3252 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxTextAttr
& style
)
3254 return GetFocusObject()->SetStyle(range
.ToInternal(), wxRichTextAttr(style
));
3257 bool wxRichTextCtrl::SetStyle(const wxRichTextRange
& range
, const wxRichTextAttr
& style
)
3259 return GetFocusObject()->SetStyle(range
.ToInternal(), style
);
3262 void wxRichTextCtrl::SetStyle(wxRichTextObject
*obj
, const wxRichTextAttr
& textAttr
)
3264 GetFocusObject()->SetStyle(obj
, textAttr
);
3267 // extended style setting operation with flags including:
3268 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3269 // see richtextbuffer.h for more details.
3271 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange
& range
, const wxRichTextAttr
& style
, int flags
)
3273 return GetFocusObject()->SetStyle(range
.ToInternal(), style
, flags
);
3276 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
3278 return GetBuffer().SetDefaultStyle(style
);
3281 bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr
& style
)
3283 wxRichTextAttr
attr1(style
);
3284 attr1
.GetTextBoxAttr().Reset();
3285 return GetBuffer().SetDefaultStyle(attr1
);
3288 const wxRichTextAttr
& wxRichTextCtrl::GetDefaultStyleEx() const
3290 return GetBuffer().GetDefaultStyle();
3293 bool wxRichTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
3295 wxRichTextAttr attr
;
3296 if (GetFocusObject()->GetStyle(position
, attr
))
3305 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
)
3307 return GetFocusObject()->GetStyle(position
, style
);
3310 bool wxRichTextCtrl::GetStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3312 wxRichTextAttr attr
;
3313 if (container
->GetStyle(position
, attr
))
3322 // get the common set of styles for the range
3323 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxTextAttr
& style
)
3325 wxRichTextAttr attr
;
3326 if (GetFocusObject()->GetStyleForRange(range
.ToInternal(), attr
))
3335 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
)
3337 return GetFocusObject()->GetStyleForRange(range
.ToInternal(), style
);
3340 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange
& range
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3342 return container
->GetStyleForRange(range
.ToInternal(), style
);
3345 /// Get the content (uncombined) attributes for this position.
3346 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
)
3348 return GetFocusObject()->GetUncombinedStyle(position
, style
);
3351 /// Get the content (uncombined) attributes for this position.
3352 bool wxRichTextCtrl::GetUncombinedStyle(long position
, wxRichTextAttr
& style
, wxRichTextParagraphLayoutBox
* container
)
3354 return container
->GetUncombinedStyle(position
, style
);
3357 /// Set font, and also the buffer attributes
3358 bool wxRichTextCtrl::SetFont(const wxFont
& font
)
3360 wxControl::SetFont(font
);
3362 wxRichTextAttr attr
= GetBuffer().GetAttributes();
3364 GetBuffer().SetBasicStyle(attr
);
3366 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3372 /// Transform logical to physical
3373 wxPoint
wxRichTextCtrl::GetPhysicalPoint(const wxPoint
& ptLogical
) const
3376 CalcScrolledPosition(ptLogical
.x
, ptLogical
.y
, & pt
.x
, & pt
.y
);
3381 /// Transform physical to logical
3382 wxPoint
wxRichTextCtrl::GetLogicalPoint(const wxPoint
& ptPhysical
) const
3385 CalcUnscrolledPosition(ptPhysical
.x
, ptPhysical
.y
, & pt
.x
, & pt
.y
);
3390 /// Position the caret
3391 void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox
* container
)
3396 //wxLogDebug(wxT("PositionCaret"));
3399 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect
, container
))
3401 wxPoint newPt
= caretRect
.GetPosition();
3402 wxSize newSz
= caretRect
.GetSize();
3403 wxPoint pt
= GetPhysicalPoint(newPt
);
3404 if (GetCaret()->GetPosition() != pt
|| GetCaret()->GetSize() != newSz
)
3407 if (GetCaret()->GetSize() != newSz
)
3408 GetCaret()->SetSize(newSz
);
3410 // Adjust size so the caret size and position doesn't appear in the margins
3411 if (((pt
.y
+ newSz
.y
) <= GetBuffer().GetTopMargin()) || (pt
.y
>= (GetClientSize().y
- GetBuffer().GetBottomMargin())))
3416 else if (pt
.y
< GetBuffer().GetTopMargin() && (pt
.y
+ newSz
.y
) > GetBuffer().GetTopMargin())
3418 newSz
.y
-= (GetBuffer().GetTopMargin() - pt
.y
);
3421 pt
.y
= GetBuffer().GetTopMargin();
3422 GetCaret()->SetSize(newSz
);
3425 else if (pt
.y
< (GetClientSize().y
- GetBuffer().GetBottomMargin()) && (pt
.y
+ newSz
.y
) > (GetClientSize().y
- GetBuffer().GetBottomMargin()))
3427 newSz
.y
= GetClientSize().y
- GetBuffer().GetBottomMargin() - pt
.y
;
3428 GetCaret()->SetSize(newSz
);
3431 GetCaret()->Move(pt
);
3437 /// Get the caret height and position for the given character position
3438 bool wxRichTextCtrl::GetCaretPositionForIndex(long position
, wxRect
& rect
, wxRichTextParagraphLayoutBox
* container
)
3440 wxClientDC
dc(this);
3441 dc
.SetFont(GetFont());
3449 container
= GetFocusObject();
3451 if (container
->FindPosition(dc
, position
, pt
, & height
, m_caretAtLineStart
))
3453 // Caret height can't be zero
3455 height
= dc
.GetCharHeight();
3457 rect
= wxRect(pt
, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH
, height
));
3464 /// Gets the line for the visible caret position. If the caret is
3465 /// shown at the very end of the line, it means the next character is actually
3466 /// on the following line. So let's get the line we're expecting to find
3467 /// if this is the case.
3468 wxRichTextLine
* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition
) const
3470 wxRichTextLine
* line
= GetFocusObject()->GetLineAtPosition(caretPosition
, true);
3471 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPosition
, true);
3474 wxRichTextRange lineRange
= line
->GetAbsoluteRange();
3475 if (caretPosition
== lineRange
.GetStart()-1 &&
3476 (para
->GetRange().GetStart() != lineRange
.GetStart()))
3478 if (!m_caretAtLineStart
)
3479 line
= GetFocusObject()->GetLineAtPosition(caretPosition
-1, true);
3486 /// Move the caret to the given character position
3487 bool wxRichTextCtrl::MoveCaret(long pos
, bool showAtLineStart
, wxRichTextParagraphLayoutBox
* container
)
3489 if (GetBuffer().IsDirty())
3493 container
= GetFocusObject();
3495 if (pos
<= container
->GetOwnRange().GetEnd())
3497 SetCaretPosition(pos
, showAtLineStart
);
3499 PositionCaret(container
);
3507 /// Layout the buffer: which we must do before certain operations, such as
3508 /// setting the caret position.
3509 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect
)
3511 if (GetBuffer().IsDirty() || onlyVisibleRect
)
3513 wxRect
availableSpace(GetClientSize());
3514 if (availableSpace
.width
== 0)
3515 availableSpace
.width
= 10;
3516 if (availableSpace
.height
== 0)
3517 availableSpace
.height
= 10;
3519 int flags
= wxRICHTEXT_FIXED_WIDTH
|wxRICHTEXT_VARIABLE_HEIGHT
;
3520 if (onlyVisibleRect
)
3522 flags
|= wxRICHTEXT_LAYOUT_SPECIFIED_RECT
;
3523 availableSpace
.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3526 wxClientDC
dc(this);
3527 dc
.SetFont(GetFont());
3531 GetBuffer().Defragment();
3532 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3533 GetBuffer().Layout(dc
, availableSpace
, flags
);
3534 GetBuffer().Invalidate(wxRICHTEXT_NONE
);
3543 /// Is all of the selection, or the current caret position, bold?
3544 bool wxRichTextCtrl::IsSelectionBold()
3548 wxRichTextAttr attr
;
3549 wxRichTextRange range
= GetSelectionRange();
3550 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3551 attr
.SetFontWeight(wxFONTWEIGHT_BOLD
);
3553 return HasCharacterAttributes(range
, attr
);
3557 // If no selection, then we need to combine current style with default style
3558 // to see what the effect would be if we started typing.
3559 wxRichTextAttr attr
;
3560 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3562 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3563 if (GetStyle(pos
, attr
))
3565 if (IsDefaultStyleShowing())
3566 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3567 return attr
.GetFontWeight() == wxFONTWEIGHT_BOLD
;
3573 /// Is all of the selection, or the current caret position, italics?
3574 bool wxRichTextCtrl::IsSelectionItalics()
3578 wxRichTextRange range
= GetSelectionRange();
3579 wxRichTextAttr attr
;
3580 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3581 attr
.SetFontStyle(wxFONTSTYLE_ITALIC
);
3583 return HasCharacterAttributes(range
, attr
);
3587 // If no selection, then we need to combine current style with default style
3588 // to see what the effect would be if we started typing.
3589 wxRichTextAttr attr
;
3590 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3592 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3593 if (GetStyle(pos
, attr
))
3595 if (IsDefaultStyleShowing())
3596 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3597 return attr
.GetFontStyle() == wxFONTSTYLE_ITALIC
;
3603 /// Is all of the selection, or the current caret position, underlined?
3604 bool wxRichTextCtrl::IsSelectionUnderlined()
3608 wxRichTextRange range
= GetSelectionRange();
3609 wxRichTextAttr attr
;
3610 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3611 attr
.SetFontUnderlined(true);
3613 return HasCharacterAttributes(range
, attr
);
3617 // If no selection, then we need to combine current style with default style
3618 // to see what the effect would be if we started typing.
3619 wxRichTextAttr attr
;
3620 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3621 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3623 if (GetStyle(pos
, attr
))
3625 if (IsDefaultStyleShowing())
3626 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3627 return attr
.GetFontUnderlined();
3633 /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3634 bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag
)
3636 wxRichTextAttr attr
;
3637 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3638 attr
.SetTextEffectFlags(flag
);
3639 attr
.SetTextEffects(flag
);
3643 return HasCharacterAttributes(GetSelectionRange(), attr
);
3647 // If no selection, then we need to combine current style with default style
3648 // to see what the effect would be if we started typing.
3649 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3650 if (GetStyle(pos
, attr
))
3652 if (IsDefaultStyleShowing())
3653 wxRichTextApplyStyle(attr
, GetDefaultStyleEx());
3654 return (attr
.GetTextEffectFlags() & flag
) != 0;
3660 /// Apply bold to the selection
3661 bool wxRichTextCtrl::ApplyBoldToSelection()
3663 wxRichTextAttr attr
;
3664 attr
.SetFlags(wxTEXT_ATTR_FONT_WEIGHT
);
3665 attr
.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL
: wxFONTWEIGHT_BOLD
);
3668 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3671 wxRichTextAttr current
= GetDefaultStyleEx();
3672 current
.Apply(attr
);
3673 SetAndShowDefaultStyle(current
);
3678 /// Apply italic to the selection
3679 bool wxRichTextCtrl::ApplyItalicToSelection()
3681 wxRichTextAttr attr
;
3682 attr
.SetFlags(wxTEXT_ATTR_FONT_ITALIC
);
3683 attr
.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL
: wxFONTSTYLE_ITALIC
);
3686 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3689 wxRichTextAttr current
= GetDefaultStyleEx();
3690 current
.Apply(attr
);
3691 SetAndShowDefaultStyle(current
);
3696 /// Apply underline to the selection
3697 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3699 wxRichTextAttr attr
;
3700 attr
.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE
);
3701 attr
.SetFontUnderlined(!IsSelectionUnderlined());
3704 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3707 wxRichTextAttr current
= GetDefaultStyleEx();
3708 current
.Apply(attr
);
3709 SetAndShowDefaultStyle(current
);
3714 /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
3715 bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags
)
3717 wxRichTextAttr attr
;
3718 attr
.SetFlags(wxTEXT_ATTR_EFFECTS
);
3719 attr
.SetTextEffectFlags(flags
);
3720 if (!DoesSelectionHaveTextEffectFlag(flags
))
3721 attr
.SetTextEffects(flags
);
3723 attr
.SetTextEffects(attr
.GetTextEffectFlags() & ~flags
);
3726 return SetStyleEx(GetSelectionRange(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY
);
3729 wxRichTextAttr current
= GetDefaultStyleEx();
3730 current
.Apply(attr
);
3731 SetAndShowDefaultStyle(current
);
3736 /// Is all of the selection aligned according to the specified flag?
3737 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment
)
3739 wxRichTextRange range
;
3741 range
= GetSelectionRange();
3743 range
= wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3745 wxRichTextAttr attr
;
3746 attr
.SetAlignment(alignment
);
3748 return HasParagraphAttributes(range
, attr
);
3751 /// Apply alignment to the selection
3752 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment
)
3754 wxRichTextAttr attr
;
3755 attr
.SetAlignment(alignment
);
3757 return SetStyle(GetSelectionRange(), attr
);
3760 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
3762 return SetStyleEx(para
->GetRange().FromInternal(), attr
, wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
);
3767 /// Apply a named style to the selection
3768 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition
* def
)
3770 // Flags are defined within each definition, so only certain
3771 // attributes are applied.
3772 wxRichTextAttr
attr(GetStyleSheet() ? def
->GetStyleMergedWithBase(GetStyleSheet()) : def
->GetStyle());
3774 int flags
= wxRICHTEXT_SETSTYLE_WITH_UNDO
|wxRICHTEXT_SETSTYLE_OPTIMIZE
|wxRICHTEXT_SETSTYLE_RESET
;
3776 if (def
->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition
)))
3778 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3780 wxRichTextRange range
;
3783 range
= GetSelectionRange();
3786 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3787 range
= wxRichTextRange(pos
, pos
+1);
3790 return SetListStyle(range
, (wxRichTextListStyleDefinition
*) def
, flags
);
3793 bool isPara
= false;
3795 // Make sure the attr has the style name
3796 if (def
->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition
)))
3799 attr
.SetParagraphStyleName(def
->GetName());
3801 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3802 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3803 // to change its style independently.
3804 flags
|= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY
;
3807 attr
.SetCharacterStyleName(def
->GetName());
3810 return SetStyleEx(GetSelectionRange(), attr
, flags
);
3813 wxRichTextAttr current
= GetDefaultStyleEx();
3814 wxRichTextAttr
defaultStyle(attr
);
3817 // Don't apply extra character styles since they are already implied
3818 // in the paragraph style
3819 defaultStyle
.SetFlags(defaultStyle
.GetFlags() & ~wxTEXT_ATTR_CHARACTER
);
3821 current
.Apply(defaultStyle
);
3822 SetAndShowDefaultStyle(current
);
3824 // If it's a paragraph style, we want to apply the style to the
3825 // current paragraph even if we didn't select any text.
3828 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3829 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(pos
);
3832 return SetStyleEx(para
->GetRange().FromInternal(), attr
, flags
);
3839 /// Apply the style sheet to the buffer, for example if the styles have changed.
3840 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet
* styleSheet
)
3843 styleSheet
= GetBuffer().GetStyleSheet();
3847 if (GetBuffer().ApplyStyleSheet(styleSheet
))
3849 GetBuffer().Invalidate(wxRICHTEXT_ALL
);
3857 /// Sets the default style to the style under the cursor
3858 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3860 wxRichTextAttr attr
;
3861 attr
.SetFlags(wxTEXT_ATTR_CHARACTER
);
3863 // If at the start of a paragraph, use the next position.
3864 long pos
= GetAdjustedCaretPosition(GetCaretPosition());
3866 if (GetUncombinedStyle(pos
, attr
))
3868 SetDefaultStyle(attr
);
3875 /// Returns the first visible position in the current view
3876 long wxRichTextCtrl::GetFirstVisiblePosition() const
3878 wxRichTextLine
* line
= GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y
);
3880 return line
->GetAbsoluteRange().GetStart();
3885 /// Get the first visible point in the window
3886 wxPoint
wxRichTextCtrl::GetFirstVisiblePoint() const
3889 int startXUnits
, startYUnits
;
3891 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
3892 GetViewStart(& startXUnits
, & startYUnits
);
3894 return wxPoint(startXUnits
* ppuX
, startYUnits
* ppuY
);
3897 /// The adjusted caret position is the character position adjusted to take
3898 /// into account whether we're at the start of a paragraph, in which case
3899 /// style information should be taken from the next position, not current one.
3900 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos
) const
3902 wxRichTextParagraph
* para
= GetFocusObject()->GetParagraphAtPosition(caretPos
+1);
3904 if (para
&& (caretPos
+1 == para
->GetRange().GetStart()))
3909 /// Get/set the selection range in character positions. -1, -1 means no selection.
3910 /// The range is in API convention, i.e. a single character selection is denoted
3912 wxRichTextRange
wxRichTextCtrl::GetSelectionRange() const
3914 wxRichTextRange range
= GetInternalSelectionRange();
3915 if (range
!= wxRichTextRange(-2,-2) && range
!= wxRichTextRange(-1,-1))
3916 range
.SetEnd(range
.GetEnd() + 1);
3920 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange
& range
)
3922 SetSelection(range
.GetStart(), range
.GetEnd());
3926 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3928 return GetFocusObject()->SetListStyle(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3931 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3933 return GetFocusObject()->SetListStyle(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3936 /// Clear list for given range
3937 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange
& range
, int flags
)
3939 return GetFocusObject()->ClearListStyle(range
.ToInternal(), flags
);
3942 /// Number/renumber any list elements in the given range
3943 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int startFrom
, int specifiedLevel
)
3945 return GetFocusObject()->NumberList(range
.ToInternal(), def
, flags
, startFrom
, specifiedLevel
);
3948 bool wxRichTextCtrl::NumberList(const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int startFrom
, int specifiedLevel
)
3950 return GetFocusObject()->NumberList(range
.ToInternal(), defName
, flags
, startFrom
, specifiedLevel
);
3953 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3954 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, wxRichTextListStyleDefinition
* def
, int flags
, int specifiedLevel
)
3956 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), def
, flags
, specifiedLevel
);
3959 bool wxRichTextCtrl::PromoteList(int promoteBy
, const wxRichTextRange
& range
, const wxString
& defName
, int flags
, int specifiedLevel
)
3961 return GetFocusObject()->PromoteList(promoteBy
, range
.ToInternal(), defName
, flags
, specifiedLevel
);
3964 /// Deletes the content in the given range
3965 bool wxRichTextCtrl::Delete(const wxRichTextRange
& range
)
3967 return GetFocusObject()->DeleteRangeWithUndo(range
.ToInternal(), this, & GetBuffer());
3970 const wxArrayString
& wxRichTextCtrl::GetAvailableFontNames()
3972 if (sm_availableFontNames
.GetCount() == 0)
3974 sm_availableFontNames
= wxFontEnumerator::GetFacenames();
3975 sm_availableFontNames
.Sort();
3977 return sm_availableFontNames
;
3980 void wxRichTextCtrl::ClearAvailableFontNames()
3982 sm_availableFontNames
.Clear();
3985 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
3987 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3989 wxTextAttrEx basicStyle
= GetBasicStyle();
3990 basicStyle
.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3991 SetBasicStyle(basicStyle
);
3992 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3997 // Refresh the area affected by a selection change
3998 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection
& oldSelection
, const wxRichTextSelection
& newSelection
)
4000 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4001 // the selection contains whole containers rather than just text, so refresh everything
4002 // for now as it would be hard to compute the rectangle bounding all selections.
4003 // TODO: improve on this.
4004 if ((oldSelection
.IsValid() && (oldSelection
.GetContainer() != GetFocusObject() || oldSelection
.GetCount() > 1)) ||
4005 (newSelection
.IsValid() && (newSelection
.GetContainer() != GetFocusObject() || newSelection
.GetCount() > 1)))
4011 wxRichTextRange oldRange
, newRange
;
4012 if (oldSelection
.IsValid())
4013 oldRange
= oldSelection
.GetRange();
4015 oldRange
= wxRICHTEXT_NO_SELECTION
;
4016 if (newSelection
.IsValid())
4017 newRange
= newSelection
.GetRange();
4019 newRange
= wxRICHTEXT_NO_SELECTION
;
4021 // Calculate the refresh rectangle - just the affected lines
4022 long firstPos
, lastPos
;
4023 if (oldRange
.GetStart() == -2 && newRange
.GetStart() != -2)
4025 firstPos
= newRange
.GetStart();
4026 lastPos
= newRange
.GetEnd();
4028 else if (oldRange
.GetStart() != -2 && newRange
.GetStart() == -2)
4030 firstPos
= oldRange
.GetStart();
4031 lastPos
= oldRange
.GetEnd();
4033 else if (oldRange
.GetStart() == -2 && newRange
.GetStart() == -2)
4039 firstPos
= wxMin(oldRange
.GetStart(), newRange
.GetStart());
4040 lastPos
= wxMax(oldRange
.GetEnd(), newRange
.GetEnd());
4043 wxRichTextLine
* firstLine
= GetFocusObject()->GetLineAtPosition(firstPos
);
4044 wxRichTextLine
* lastLine
= GetFocusObject()->GetLineAtPosition(lastPos
);
4046 if (firstLine
&& lastLine
)
4048 wxSize clientSize
= GetClientSize();
4049 wxPoint pt1
= GetPhysicalPoint(firstLine
->GetAbsolutePosition());
4050 wxPoint pt2
= GetPhysicalPoint(lastLine
->GetAbsolutePosition()) + wxPoint(0, lastLine
->GetSize().y
);
4053 pt1
.y
= wxMax(0, pt1
.y
);
4055 pt2
.y
= wxMin(clientSize
.y
, pt2
.y
);
4057 wxRect
rect(pt1
, wxSize(clientSize
.x
, pt2
.y
- pt1
.y
));
4058 RefreshRect(rect
, false);
4066 // margins functions
4067 bool wxRichTextCtrl::DoSetMargins(const wxPoint
& pt
)
4069 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4070 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt
.x
, wxTEXT_ATTR_UNITS_PIXELS
);
4071 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4072 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt
.y
, wxTEXT_ATTR_UNITS_PIXELS
);
4077 wxPoint
wxRichTextCtrl::DoGetMargins() const
4079 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4080 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4083 bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox
* obj
, bool setCaretPosition
)
4085 if (obj
&& !obj
->AcceptsFocus())
4088 wxRichTextParagraphLayoutBox
* oldContainer
= GetFocusObject();
4089 bool changingContainer
= (m_focusObject
!= obj
);
4091 m_focusObject
= obj
;
4094 m_focusObject
= & m_buffer
;
4096 if (setCaretPosition
&& changingContainer
)
4098 m_selection
.Reset();
4099 m_selectionAnchor
= -2;
4100 m_selectionAnchorObject
= NULL
;
4101 m_selectionState
= wxRichTextCtrlSelectionState_Normal
;
4105 m_caretAtLineStart
= false;
4106 MoveCaret(pos
, m_caretAtLineStart
);
4107 SetDefaultStyleToCursorStyle();
4109 wxRichTextEvent
cmdEvent(
4110 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED
,
4112 cmdEvent
.SetEventObject(this);
4113 cmdEvent
.SetPosition(m_caretPosition
+1);
4114 cmdEvent
.SetOldContainer(oldContainer
);
4115 cmdEvent
.SetContainer(m_focusObject
);
4117 GetEventHandler()->ProcessEvent(cmdEvent
);
4122 #if wxRICHTEXT_USE_OWN_CARET
4124 // ----------------------------------------------------------------------------
4125 // initialization and destruction
4126 // ----------------------------------------------------------------------------
4128 void wxRichTextCaret::Init()
4134 m_richTextCtrl
= NULL
;
4135 m_needsUpdate
= false;
4139 wxRichTextCaret::~wxRichTextCaret()
4141 if (m_timer
.IsRunning())
4145 // ----------------------------------------------------------------------------
4146 // showing/hiding/moving the caret (base class interface)
4147 // ----------------------------------------------------------------------------
4149 void wxRichTextCaret::DoShow()
4153 if (!m_timer
.IsRunning())
4154 m_timer
.Start(GetBlinkTime());
4159 void wxRichTextCaret::DoHide()
4161 if (m_timer
.IsRunning())
4167 void wxRichTextCaret::DoMove()
4173 if (m_xOld
!= -1 && m_yOld
!= -1)
4177 wxRect
rect(GetPosition(), GetSize());
4178 m_richTextCtrl
->RefreshRect(rect
, false);
4187 void wxRichTextCaret::DoSize()
4189 int countVisible
= m_countVisible
;
4190 if (countVisible
> 0)
4196 if (countVisible
> 0)
4198 m_countVisible
= countVisible
;
4203 // ----------------------------------------------------------------------------
4204 // handling the focus
4205 // ----------------------------------------------------------------------------
4207 void wxRichTextCaret::OnSetFocus()
4215 void wxRichTextCaret::OnKillFocus()
4220 // ----------------------------------------------------------------------------
4221 // drawing the caret
4222 // ----------------------------------------------------------------------------
4224 void wxRichTextCaret::Refresh()
4228 wxRect
rect(GetPosition(), GetSize());
4229 m_richTextCtrl
->RefreshRect(rect
, false);
4233 void wxRichTextCaret::DoDraw(wxDC
*dc
)
4235 dc
->SetPen( *wxBLACK_PEN
);
4237 dc
->SetBrush(*(m_hasFocus
? wxBLACK_BRUSH
: wxTRANSPARENT_BRUSH
));
4238 dc
->SetPen(*wxBLACK_PEN
);
4240 wxPoint
pt(m_x
, m_y
);
4244 pt
= m_richTextCtrl
->GetLogicalPoint(pt
);
4246 if (IsVisible() && m_flashOn
)
4247 dc
->DrawRectangle(pt
.x
, pt
.y
, m_width
, m_height
);
4250 void wxRichTextCaret::Notify()
4252 m_flashOn
= !m_flashOn
;
4256 void wxRichTextCaretTimer::Notify()
4261 // wxRICHTEXT_USE_OWN_CARET
4264 bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString
& label
, wxRichTextObject
* obj
)
4268 m_labels
.Add(label
);
4276 // Returns number of menu items were added.
4277 int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu
* menu
, int startCmd
) const
4279 wxMenuItem
* item
= menu
->FindItem(startCmd
);
4280 // If none of the standard properties identifiers are in the menu, assume it's
4281 // a custom menu without properties commands, and don't add them.
4284 // If no items, to add just set the text to something generic
4285 if (GetCount() == 0)
4287 menu
->SetLabel(startCmd
, _("&Properties"));
4289 // Delete the others if necessary
4291 for (i
= startCmd
+1; i
< startCmd
+3; i
++)
4293 if (menu
->FindItem(i
))
4303 // Find the position of the first properties item
4304 for (i
= 0; i
< (int) menu
->GetMenuItemCount(); i
++)
4306 wxMenuItem
* item
= menu
->FindItemByPosition(i
);
4307 if (item
&& item
->GetId() == startCmd
)
4316 int insertBefore
= pos
+1;
4317 for (i
= startCmd
; i
< startCmd
+GetCount(); i
++)
4319 if (menu
->FindItem(i
))
4321 menu
->SetLabel(i
, m_labels
[i
- startCmd
]);
4325 if (insertBefore
>= (int) menu
->GetMenuItemCount())
4326 menu
->Append(i
, m_labels
[i
- startCmd
]);
4328 menu
->Insert(insertBefore
, i
, m_labels
[i
- startCmd
]);
4333 // Delete any old items still left on the menu
4334 for (i
= startCmd
+ GetCount(); i
< startCmd
+3; i
++)
4336 if (menu
->FindItem(i
))
4348 // Add appropriate menu items for the current container and clicked on object
4349 // (and container's parent, if appropriate).
4350 int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextObject
* container
, wxRichTextObject
* obj
)
4353 if (obj
&& obj
->CanEditProperties())
4354 AddItem(obj
->GetPropertiesMenuLabel(), obj
);
4356 if (container
&& container
!= obj
&& container
->CanEditProperties() && m_labels
.Index(container
->GetPropertiesMenuLabel()) == wxNOT_FOUND
)
4357 AddItem(container
->GetPropertiesMenuLabel(), container
);
4359 if (container
&& container
->GetParent() && container
->GetParent()->CanEditProperties() && m_labels
.Index(container
->GetParent()->GetPropertiesMenuLabel()) == wxNOT_FOUND
)
4360 AddItem(container
->GetParent()->GetPropertiesMenuLabel(), container
->GetParent());