]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextctrl.cpp
Compile fixes for wxUSE_COLLPANE=0
[wxWidgets.git] / src / richtext / richtextctrl.cpp
CommitLineData
5d7836c4 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/richtext/richtextctrl.cpp
5d7836c4
JS
3// Purpose: A rich edit control
4// Author: Julian Smart
7fe8059f 5// Modified by:
5d7836c4 6// Created: 2005-09-30
7fe8059f 7// RCS-ID: $Id$
5d7836c4
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
9eddec69 16 #pragma hdrstop
5d7836c4
JS
17#endif
18
b01ca8b6
JS
19#if wxUSE_RICHTEXT
20
21#include "wx/richtext/richtextctrl.h"
ab14c7aa 22#include "wx/richtext/richtextstyles.h"
b01ca8b6 23
5d7836c4 24#ifndef WX_PRECOMP
fe5aa22c 25 #include "wx/wx.h"
9eddec69 26 #include "wx/settings.h"
5d7836c4
JS
27#endif
28
a188ac29 29#include "wx/timer.h"
5d7836c4
JS
30#include "wx/textfile.h"
31#include "wx/ffile.h"
5d7836c4
JS
32#include "wx/filename.h"
33#include "wx/dcbuffer.h"
5d7836c4 34#include "wx/arrimpl.cpp"
dadd4f55 35#include "wx/fontenum.h"
addcf76c 36#include "wx/accel.h"
5d7836c4 37
5dec941a
JS
38#if defined (__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__)
39#define wxHAVE_PRIMARY_SELECTION 1
40#else
41#define wxHAVE_PRIMARY_SELECTION 0
42#endif
43
44#if wxUSE_CLIPBOARD && wxHAVE_PRIMARY_SELECTION
45#include "wx/clipbrd.h"
46#endif
47
27e20452
JS
48// DLL options compatibility check:
49#include "wx/app.h"
50WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
51
9b11752c
VZ
52wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK, wxRichTextEvent );
53wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK, wxRichTextEvent );
54wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK, wxRichTextEvent );
55wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK, wxRichTextEvent );
56wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN, wxRichTextEvent );
57wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER, wxRichTextEvent );
58wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE, wxRichTextEvent );
5912d19e 59
9b11752c
VZ
60wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, wxRichTextEvent );
61wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED, wxRichTextEvent );
62wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING, wxRichTextEvent );
63wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED, wxRichTextEvent );
5d7836c4 64
9b11752c
VZ
65wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, wxRichTextEvent );
66wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, wxRichTextEvent );
67wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED, wxRichTextEvent );
590a0f8b 68wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_PROPERTIES_CHANGED, wxRichTextEvent );
9b11752c
VZ
69wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED, wxRichTextEvent );
70wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, wxRichTextEvent );
603f702b 71wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED, wxRichTextEvent );
5912d19e 72
1c13f06e
JS
73#if wxRICHTEXT_USE_OWN_CARET
74
75/*!
76 * wxRichTextCaret
77 *
78 * This implements a non-flashing cursor in case there
79 * are platform-specific problems with the generic caret.
80 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
81 */
82
a188ac29
JS
83class wxRichTextCaret;
84class wxRichTextCaretTimer: public wxTimer
85{
86 public:
87 wxRichTextCaretTimer(wxRichTextCaret* caret)
88 {
89 m_caret = caret;
90 }
91 virtual void Notify();
92 wxRichTextCaret* m_caret;
93};
94
1c13f06e
JS
95class wxRichTextCaret: public wxCaret
96{
97public:
98 // ctors
99 // -----
100 // default - use Create()
a188ac29 101 wxRichTextCaret(): m_timer(this) { Init(); }
1c13f06e
JS
102 // creates a block caret associated with the given window
103 wxRichTextCaret(wxRichTextCtrl *window, int width, int height)
a188ac29 104 : wxCaret(window, width, height), m_timer(this) { Init(); m_richTextCtrl = window; }
1c13f06e 105 wxRichTextCaret(wxRichTextCtrl *window, const wxSize& size)
a188ac29 106 : wxCaret(window, size), m_timer(this) { Init(); m_richTextCtrl = window; }
1c13f06e
JS
107
108 virtual ~wxRichTextCaret();
109
110 // implementation
111 // --------------
112
113 // called by wxWindow (not using the event tables)
114 virtual void OnSetFocus();
115 virtual void OnKillFocus();
116
117 // draw the caret on the given DC
118 void DoDraw(wxDC *dc);
119
120 // get the visible count
121 int GetVisibleCount() const { return m_countVisible; }
122
123 // delay repositioning
124 bool GetNeedsUpdate() const { return m_needsUpdate; }
125 void SetNeedsUpdate(bool needsUpdate = true ) { m_needsUpdate = needsUpdate; }
126
a188ac29
JS
127 void Notify();
128
cd45ed36
JS
129 bool GetRefreshEnabled() const { return m_refreshEnabled; }
130 void EnableRefresh(bool b) { m_refreshEnabled = b; }
131
1c13f06e
JS
132protected:
133 virtual void DoShow();
134 virtual void DoHide();
135 virtual void DoMove();
136 virtual void DoSize();
137
138 // refresh the caret
139 void Refresh();
140
141private:
142 void Init();
143
144 int m_xOld,
145 m_yOld;
146 bool m_hasFocus; // true => our window has focus
147 bool m_needsUpdate; // must be repositioned
a188ac29
JS
148 bool m_flashOn;
149 wxRichTextCaretTimer m_timer;
1c13f06e 150 wxRichTextCtrl* m_richTextCtrl;
cd45ed36 151 bool m_refreshEnabled;
1c13f06e
JS
152};
153#endif
154
24777478 155IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl, wxControl )
5d7836c4 156
d0960f6d 157IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent, wxNotifyEvent )
5d7836c4 158
24777478 159BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl )
5d7836c4
JS
160 EVT_PAINT(wxRichTextCtrl::OnPaint)
161 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground)
4d551ad5 162 EVT_IDLE(wxRichTextCtrl::OnIdle)
c59f6793 163 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll)
5d7836c4
JS
164 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick)
165 EVT_MOTION(wxRichTextCtrl::OnMoveMouse)
166 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp)
167 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick)
168 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick)
169 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick)
170 EVT_CHAR(wxRichTextCtrl::OnChar)
c80e8cbe 171 EVT_KEY_DOWN(wxRichTextCtrl::OnChar)
5d7836c4
JS
172 EVT_SIZE(wxRichTextCtrl::OnSize)
173 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus)
174 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus)
61f84e24 175 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost)
5d7836c4 176 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu)
52170c5b 177 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged)
5d7836c4
JS
178
179 EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo)
180 EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo)
181
182 EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo)
183 EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo)
184
185 EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy)
186 EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy)
187
188 EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste)
189 EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste)
190
191 EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut)
192 EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut)
193
194 EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear)
195 EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear)
196
197 EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll)
198 EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll)
603f702b
JS
199
200 EVT_MENU(wxID_RICHTEXT_PROPERTIES1, wxRichTextCtrl::OnProperties)
201 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1, wxRichTextCtrl::OnUpdateProperties)
202
203 EVT_MENU(wxID_RICHTEXT_PROPERTIES2, wxRichTextCtrl::OnProperties)
204 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2, wxRichTextCtrl::OnUpdateProperties)
205
206 EVT_MENU(wxID_RICHTEXT_PROPERTIES3, wxRichTextCtrl::OnProperties)
207 EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3, wxRichTextCtrl::OnUpdateProperties)
208
5d7836c4
JS
209END_EVENT_TABLE()
210
211/*!
212 * wxRichTextCtrl
213 */
214
dadd4f55
JS
215wxArrayString wxRichTextCtrl::sm_availableFontNames;
216
5d7836c4 217wxRichTextCtrl::wxRichTextCtrl()
343ae70d 218 : wxScrollHelper(this)
5d7836c4
JS
219{
220 Init();
221}
222
343ae70d
VZ
223wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent,
224 wxWindowID id,
225 const wxString& value,
226 const wxPoint& pos,
227 const wxSize& size,
4e8d9558
JS
228 long style,
229 const wxValidator& validator,
230 const wxString& name)
343ae70d 231 : wxScrollHelper(this)
5d7836c4
JS
232{
233 Init();
4e8d9558 234 Create(parent, id, value, pos, size, style, validator, name);
5d7836c4
JS
235}
236
237/// Creation
4e8d9558
JS
238bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style,
239 const wxValidator& validator, const wxString& name)
5d7836c4 240{
6d595f3d
JS
241 style |= wxVSCROLL;
242
24777478 243 if (!wxControl::Create(parent, id, pos, size,
0ec1179b
VZ
244 style|wxFULL_REPAINT_ON_RESIZE,
245 validator, name))
5d7836c4 246 return false;
5d7836c4 247
a1b806b9 248 if (!GetFont().IsOk())
5d7836c4
JS
249 {
250 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
251 }
7fe8059f 252
e9f10004
JS
253 // No physical scrolling, so we can preserve margins
254 EnableScrolling(false, false);
255
dadd4f55
JS
256 if (style & wxTE_READONLY)
257 SetEditable(false);
258
42688aea 259 // The base attributes must all have default values
24777478 260 wxRichTextAttr attributes;
5d7836c4 261 attributes.SetFont(GetFont());
79f955ec 262 attributes.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
5d7836c4 263 attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
3e3a754f
JS
264 attributes.SetLineSpacing(10);
265 attributes.SetParagraphSpacingAfter(10);
266 attributes.SetParagraphSpacingBefore(0);
4fe83b93 267
5d7836c4
JS
268 SetBasicStyle(attributes);
269
603f702b
JS
270 int margin = 5;
271 SetMargins(margin, margin);
272
fe5aa22c
JS
273 // The default attributes will be merged with base attributes, so
274 // can be empty to begin with
24777478 275 wxRichTextAttr defaultAttributes;
fe5aa22c
JS
276 SetDefaultStyle(defaultAttributes);
277
79f955ec 278 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
5d7836c4
JS
279 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
280
cd8ba0d9
JS
281 GetBuffer().Reset();
282 GetBuffer().SetRichTextCtrl(this);
283
1c13f06e
JS
284#if wxRICHTEXT_USE_OWN_CARET
285 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
286#else
cd8ba0d9 287 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
1c13f06e 288#endif
cd8ba0d9 289
7fe8059f 290 // Tell the sizers to use the given or best size
170acdc9 291 SetInitialSize(size);
7fe8059f 292
3e3a754f 293#if wxRICHTEXT_BUFFERED_PAINTING
5d7836c4
JS
294 // Create a buffer
295 RecreateBuffer(size);
3e3a754f 296#endif
5d7836c4 297
d2d0adc7
JS
298 m_textCursor = wxCursor(wxCURSOR_IBEAM);
299 m_urlCursor = wxCursor(wxCURSOR_HAND);
300
301 SetCursor(m_textCursor);
61399247 302
27e20452
JS
303 if (!value.IsEmpty())
304 SetValue(value);
7fe8059f 305
d2d0adc7
JS
306 GetBuffer().AddEventHandler(this);
307
addcf76c 308 // Accelerators
0951fe0a 309 wxAcceleratorEntry entries[6];
addcf76c 310
675ec665
SC
311 entries[0].Set(wxACCEL_CTRL, (int) 'C', wxID_COPY);
312 entries[1].Set(wxACCEL_CTRL, (int) 'X', wxID_CUT);
313 entries[2].Set(wxACCEL_CTRL, (int) 'V', wxID_PASTE);
314 entries[3].Set(wxACCEL_CTRL, (int) 'A', wxID_SELECTALL);
315 entries[4].Set(wxACCEL_CTRL, (int) 'Z', wxID_UNDO);
316 entries[5].Set(wxACCEL_CTRL, (int) 'Y', wxID_REDO);
addcf76c 317
0951fe0a 318 wxAcceleratorTable accel(6, entries);
addcf76c
JS
319 SetAcceleratorTable(accel);
320
8e43fc77
JS
321 m_contextMenu = new wxMenu;
322 m_contextMenu->Append(wxID_UNDO, _("&Undo"));
323 m_contextMenu->Append(wxID_REDO, _("&Redo"));
324 m_contextMenu->AppendSeparator();
325 m_contextMenu->Append(wxID_CUT, _("Cu&t"));
326 m_contextMenu->Append(wxID_COPY, _("&Copy"));
327 m_contextMenu->Append(wxID_PASTE, _("&Paste"));
328 m_contextMenu->Append(wxID_CLEAR, _("&Delete"));
329 m_contextMenu->AppendSeparator();
330 m_contextMenu->Append(wxID_SELECTALL, _("Select &All"));
cdaed652 331 m_contextMenu->AppendSeparator();
603f702b 332 m_contextMenu->Append(wxID_RICHTEXT_PROPERTIES1, _("&Properties"));
cdaed652 333
590a0f8b
JS
334#if wxUSE_DRAG_AND_DROP
335 SetDropTarget(new wxRichTextDropTarget(this));
336#endif
337
5d7836c4
JS
338 return true;
339}
340
341wxRichTextCtrl::~wxRichTextCtrl()
342{
603f702b 343 SetFocusObject(& GetBuffer(), false);
d2d0adc7 344 GetBuffer().RemoveEventHandler(this);
88bee7e6 345
5d7836c4
JS
346 delete m_contextMenu;
347}
348
349/// Member initialisation
350void wxRichTextCtrl::Init()
351{
5d7836c4
JS
352 m_contextMenu = NULL;
353 m_caret = NULL;
354 m_caretPosition = -1;
5d7836c4 355 m_selectionAnchor = -2;
603f702b
JS
356 m_selectionAnchorObject = NULL;
357 m_selectionState = wxRichTextCtrlSelectionState_Normal;
5d7836c4
JS
358 m_editable = true;
359 m_caretAtLineStart = false;
360 m_dragging = false;
9fa7b436 361#if wxUSE_DRAG_AND_DROP
0c0e063e 362 m_preDrag = false;
9fa7b436 363#endif
4d551ad5
JS
364 m_fullLayoutRequired = false;
365 m_fullLayoutTime = 0;
366 m_fullLayoutSavedPosition = 0;
367 m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD;
ab14c7aa 368 m_caretPositionForDefaultStyle = -2;
603f702b 369 m_focusObject = & m_buffer;
5d7836c4
JS
370}
371
17808a75 372void wxRichTextCtrl::DoThaw()
5d7836c4 373{
603f702b 374 if (GetBuffer().IsDirty())
17808a75
VZ
375 LayoutContent();
376 else
377 SetupScrollbars();
8251c171 378
6a8e61e0 379 wxWindow::DoThaw();
5d7836c4
JS
380}
381
382/// Clear all text
383void wxRichTextCtrl::Clear()
384{
603f702b
JS
385 if (GetFocusObject() == & GetBuffer())
386 {
387 m_buffer.ResetAndClearCommands();
388 m_buffer.Invalidate(wxRICHTEXT_ALL);
389 }
390 else
391 {
392 GetFocusObject()->Reset();
393 }
394
5d7836c4 395 m_caretPosition = -1;
ab14c7aa 396 m_caretPositionForDefaultStyle = -2;
5d7836c4 397 m_caretAtLineStart = false;
603f702b
JS
398 m_selection.Reset();
399 m_selectionState = wxRichTextCtrlSelectionState_Normal;
5d7836c4 400
495a248e
JS
401 Scroll(0,0);
402
17808a75 403 if (!IsFrozen())
5d7836c4 404 {
cecf8eee 405 LayoutContent();
76bcd815 406 Refresh(false);
5d7836c4 407 }
0ec1179b
VZ
408
409 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4
JS
410}
411
412/// Painting
413void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
414{
1c13f06e
JS
415#if !wxRICHTEXT_USE_OWN_CARET
416 if (GetCaret() && !IsFrozen())
d2142335 417 GetCaret()->Hide();
cd45ed36
JS
418#else
419 // Stop the caret refreshing the control from within the
420 // paint handler
421 if (GetCaret())
422 ((wxRichTextCaret*) GetCaret())->EnableRefresh(false);
1c13f06e 423#endif
d2142335 424
5d7836c4 425 {
3e3a754f 426#if wxRICHTEXT_BUFFERED_PAINTING
c59f6793 427 wxBufferedPaintDC dc(this, m_bufferBitmap);
3e3a754f
JS
428#else
429 wxPaintDC dc(this);
430#endif
41e155b4 431
17808a75 432 if (IsFrozen())
c59f6793 433 return;
41e155b4 434
17808a75
VZ
435 PrepareDC(dc);
436
c59f6793 437 dc.SetFont(GetFont());
41e155b4 438
c59f6793
JS
439 // Paint the background
440 PaintBackground(dc);
41e155b4 441
ea160b2e
JS
442 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
443
444 wxRect drawingArea(GetUpdateRegion().GetBox());
445 drawingArea.SetPosition(GetLogicalPoint(drawingArea.GetPosition()));
446
c59f6793 447 wxRect availableSpace(GetClientSize());
8db2e3ef 448 wxRichTextDrawingContext context(& GetBuffer());
603f702b 449 if (GetBuffer().IsDirty())
c59f6793 450 {
8db2e3ef 451 GetBuffer().Layout(dc, context, availableSpace, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
603f702b 452 GetBuffer().Invalidate(wxRICHTEXT_NONE);
c59f6793
JS
453 SetupScrollbars();
454 }
41e155b4 455
e9f10004
JS
456 wxRect clipRect(availableSpace);
457 clipRect.x += GetBuffer().GetLeftMargin();
458 clipRect.y += GetBuffer().GetTopMargin();
459 clipRect.width -= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
460 clipRect.height -= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
461 clipRect.SetPosition(GetLogicalPoint(clipRect.GetPosition()));
462 dc.SetClippingRegion(clipRect);
463
603f702b
JS
464 int flags = 0;
465 if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES) == 0)
466 flags |= wxRICHTEXT_DRAW_GUIDELINES;
4fe83b93 467
8db2e3ef 468 GetBuffer().Draw(dc, context, GetBuffer().GetOwnRange(), GetSelection(), drawingArea, 0 /* descent */, flags);
e9f10004
JS
469
470 dc.DestroyClippingRegion();
471
35bdbae5
JS
472 // Other user defined painting after everything else (i.e. all text) is painted
473 PaintAboveContent(dc);
474
1c13f06e
JS
475#if wxRICHTEXT_USE_OWN_CARET
476 if (GetCaret()->IsVisible())
477 {
cd45ed36 478 PositionCaret();
1c13f06e
JS
479 ((wxRichTextCaret*) GetCaret())->DoDraw(& dc);
480 }
481#endif
5d7836c4 482 }
d2142335 483
1c13f06e
JS
484#if !wxRICHTEXT_USE_OWN_CARET
485 if (GetCaret())
d2142335 486 GetCaret()->Show();
7383cf9d 487 PositionCaret();
5219cf05 488#else
cd45ed36
JS
489 if (GetCaret())
490 ((wxRichTextCaret*) GetCaret())->EnableRefresh(true);
1c13f06e 491#endif
5d7836c4
JS
492}
493
494// Empty implementation, to prevent flicker
495void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
496{
497}
498
499void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
500{
ea160b2e
JS
501 if (GetCaret())
502 {
1c13f06e 503#if !wxRICHTEXT_USE_OWN_CARET
ea160b2e 504 PositionCaret();
1c13f06e
JS
505#endif
506 GetCaret()->Show();
ea160b2e 507 }
c59f6793 508
1c13f06e
JS
509#if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
510 // Work around dropouts when control is focused
511 if (!IsFrozen())
512 {
513 Refresh(false);
514 }
515#endif
5d7836c4
JS
516}
517
518void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
519{
1c13f06e 520 if (GetCaret())
ea160b2e 521 GetCaret()->Hide();
c59f6793 522
1c13f06e
JS
523#if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
524 // Work around dropouts when control is focused
525 if (!IsFrozen())
526 {
527 Refresh(false);
528 }
529#endif
5d7836c4
JS
530}
531
61f84e24
JS
532void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
533{
88bee7e6 534 m_dragging = false;
61f84e24
JS
535}
536
603f702b
JS
537// Set up the caret for the given position and container, after a mouse click
538bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox* container, long position, int hitTestFlags, bool extendSelection)
539{
540 bool caretAtLineStart = false;
541
542 if (hitTestFlags & wxRICHTEXT_HITTEST_BEFORE)
543 {
544 // If we're at the start of a line (but not first in para)
545 // then we should keep the caret showing at the start of the line
546 // by showing the m_caretAtLineStart flag.
547 wxRichTextParagraph* para = container->GetParagraphAtPosition(position);
548 wxRichTextLine* line = container->GetLineAtPosition(position);
549
550 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
551 caretAtLineStart = true;
552 position --;
553 }
554
555 if (extendSelection && (m_caretPosition != position))
556 ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN);
557
558 MoveCaret(position, caretAtLineStart);
559 SetDefaultStyleToCursorStyle();
4fe83b93 560
603f702b
JS
561 return true;
562}
563
5d7836c4
JS
564/// Left-click
565void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
566{
567 SetFocus();
568
569 wxClientDC dc(this);
570 PrepareDC(dc);
571 dc.SetFont(GetFont());
7fe8059f 572
603f702b 573 // TODO: detect change of focus object
5d7836c4 574 long position = 0;
603f702b
JS
575 wxRichTextObject* hitObj = NULL;
576 wxRichTextObject* contextObj = NULL;
8db2e3ef
JS
577 wxRichTextDrawingContext context(& GetBuffer());
578 int hit = GetBuffer().HitTest(dc, context, event.GetLogicalPosition(dc), position, & hitObj, & contextObj);
5d7836c4 579
0c0e063e
JS
580#if wxUSE_DRAG_AND_DROP
581 // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop
b33e0c2c 582 if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position))
0c0e063e
JS
583 {
584 // This might be an attempt at initiating Drag'n'Drop. So set the time & flags
585 m_preDrag = true;
586 m_dragStartPoint = event.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt
587
588#if wxUSE_DATETIME
589 m_dragStartTime = wxDateTime::UNow();
590#endif // wxUSE_DATETIME
591
592 // Preserve behaviour of clicking on an object within the selection
593 if (hit != wxRICHTEXT_HITTEST_NONE && hitObj)
594 m_dragging = true;
595
596 return; // Don't skip the event, else the selection will be lost
597 }
598#endif // wxUSE_DRAG_AND_DROP
599
603f702b 600 if (hit != wxRICHTEXT_HITTEST_NONE && hitObj)
5d7836c4 601 {
603f702b
JS
602 wxRichTextParagraphLayoutBox* oldFocusObject = GetFocusObject();
603 wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
604 if (container && container != GetFocusObject() && container->AcceptsFocus())
605 {
606 SetFocusObject(container, false /* don't set caret position yet */);
607 }
4fe83b93 608
5d7836c4
JS
609 m_dragging = true;
610 CaptureMouse();
611
f7222f4c
JS
612 long oldCaretPos = m_caretPosition;
613
603f702b 614 SetCaretPositionAfterClick(container, position, hit);
f7222f4c 615
603f702b
JS
616 // For now, don't handle shift-click when we're selecting multiple objects.
617 if (event.ShiftDown() && GetFocusObject() == oldFocusObject && m_selectionState == wxRichTextCtrlSelectionState_Normal)
77ea6c48 618 ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
f7222f4c
JS
619 else
620 SelectNone();
5d7836c4
JS
621 }
622
623 event.Skip();
624}
625
626/// Left-up
d2d0adc7 627void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event)
5d7836c4
JS
628{
629 if (m_dragging)
630 {
631 m_dragging = false;
632 if (GetCapture() == this)
633 ReleaseMouse();
d2d0adc7
JS
634
635 // See if we clicked on a URL
636 wxClientDC dc(this);
637 PrepareDC(dc);
638 dc.SetFont(GetFont());
639
640 long position = 0;
641 wxPoint logicalPt = event.GetLogicalPosition(dc);
603f702b
JS
642 wxRichTextObject* hitObj = NULL;
643 wxRichTextObject* contextObj = NULL;
8db2e3ef 644 wxRichTextDrawingContext context(& GetBuffer());
603f702b 645 // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level.
8db2e3ef 646 int hit = GetFocusObject()->HitTest(dc, context, logicalPt, position, & hitObj, & contextObj, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS);
88bee7e6 647
0c0e063e
JS
648#if wxUSE_DRAG_AND_DROP
649 if (m_preDrag)
650 {
651 // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick
652 m_preDrag = false; // Tell DnD not to happen now: we are processing Left Up ourselves.
653
654 // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag
655 long position = 0;
656 wxRichTextObject* hitObj = NULL;
657 wxRichTextObject* contextObj = NULL;
8db2e3ef 658 int hit = GetBuffer().HitTest(dc, context, event.GetLogicalPosition(dc), position, & hitObj, & contextObj);
0c0e063e
JS
659 wxRichTextParagraphLayoutBox* oldFocusObject = GetFocusObject();
660 wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
661 if (container && container != GetFocusObject() && container->AcceptsFocus())
662 {
663 SetFocusObject(container, false /* don't set caret position yet */);
664 }
665
666 long oldCaretPos = m_caretPosition;
667
668 SetCaretPositionAfterClick(container, position, hit);
669
670 // For now, don't handle shift-click when we're selecting multiple objects.
671 if (event.ShiftDown() && GetFocusObject() == oldFocusObject && m_selectionState == wxRichTextCtrlSelectionState_Normal)
672 ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
673 else
674 SelectNone();
675 }
676#endif
677
62381daa 678 if ((hit != wxRICHTEXT_HITTEST_NONE) && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
d2d0adc7 679 {
db6634e0
JS
680 wxRichTextEvent cmdEvent(
681 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK,
682 GetId());
683 cmdEvent.SetEventObject(this);
603f702b
JS
684 cmdEvent.SetPosition(position);
685 if (hitObj)
686 cmdEvent.SetContainer(hitObj->GetContainer());
db6634e0
JS
687
688 if (!GetEventHandler()->ProcessEvent(cmdEvent))
d2d0adc7 689 {
24777478 690 wxRichTextAttr attr;
db6634e0 691 if (GetStyle(position, attr))
d2d0adc7 692 {
db6634e0 693 if (attr.HasFlag(wxTEXT_ATTR_URL))
d2d0adc7 694 {
db6634e0
JS
695 wxString urlTarget = attr.GetURL();
696 if (!urlTarget.IsEmpty())
d2d0adc7 697 {
db6634e0 698 wxMouseEvent mouseEvent(event);
1a75935d 699
db6634e0 700 long startPos = 0, endPos = 0;
603f702b 701 wxRichTextObject* obj = GetFocusObject()->GetLeafObjectAtPosition(position);
db6634e0
JS
702 if (obj)
703 {
704 startPos = obj->GetRange().GetStart();
705 endPos = obj->GetRange().GetEnd();
706 }
1a75935d 707
db6634e0
JS
708 wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos);
709 InitCommandEvent(urlEvent);
1a75935d 710
db6634e0 711 urlEvent.SetString(urlTarget);
1a75935d 712
db6634e0 713 GetEventHandler()->ProcessEvent(urlEvent);
88bee7e6 714 }
d2d0adc7
JS
715 }
716 }
717 }
718 }
5d7836c4 719 }
5dec941a 720
0c0e063e
JS
721#if wxUSE_DRAG_AND_DROP
722 m_preDrag = false;
723#endif // wxUSE_DRAG_AND_DROP
724
5dec941a
JS
725#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
726 if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer())
727 {
728 // Put the selection in PRIMARY, if it exists
729 wxTheClipboard->UsePrimarySelection(true);
730
731 wxRichTextRange range = GetInternalSelectionRange();
732 GetFocusObject()->GetBuffer()->CopyToClipboard(range);
733
734 wxTheClipboard->UsePrimarySelection(false);
735 }
736#endif
5d7836c4
JS
737}
738
0c0e063e 739/// Mouse-movements
5d7836c4
JS
740void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event)
741{
0c0e063e
JS
742#if wxUSE_DRAG_AND_DROP
743 // See if we're starting Drag'n'Drop
744 if (m_preDrag)
745 {
746 int x = m_dragStartPoint.x - event.GetPosition().x;
747 int y = m_dragStartPoint.y - event.GetPosition().y;
748 size_t distance = abs(x) + abs(y);
749#if wxUSE_DATETIME
750 wxTimeSpan diff = wxDateTime::UNow() - m_dragStartTime;
751#endif
752 if ((distance > 10)
753#if wxUSE_DATETIME
754 && (diff.GetMilliseconds() > 100)
755#endif
756 )
757 {
758 m_dragging = false;
759
760 // Start drag'n'drop
761 wxRichTextRange range = GetInternalSelectionRange();
762 if (range == wxRICHTEXT_NONE)
763 {
764 // Don't try to drag an empty range
765 m_preDrag = false;
766 return;
767 }
768
769 // Cache the current situation, to be restored if Drag'n'Drop is cancelled
770 long oldPos = GetCaretPosition();
771 wxRichTextParagraphLayoutBox* oldFocus = GetFocusObject();
772
773 wxDataObjectComposite* compositeObject = new wxDataObjectComposite();
774 wxString text = GetFocusObject()->GetTextForRange(range);
775#ifdef __WXMSW__
776 text = wxTextFile::Translate(text, wxTextFileType_Dos);
777#endif
778 compositeObject->Add(new wxTextDataObject(text), false /* not preferred */);
779
780 wxRichTextBuffer* richTextBuf = new wxRichTextBuffer;
781 GetFocusObject()->CopyFragment(range, *richTextBuf);
782 compositeObject->Add(new wxRichTextBufferDataObject(richTextBuf), true /* preferred */);
783
784 wxRichTextDropSource source(*compositeObject, this);
785 // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise
786 // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed
787 BeginBatchUndo(_("Drag"));
788 switch (source.DoDragDrop(wxDrag_AllowMove | wxDrag_DefaultMove))
789 {
790 case wxDragMove:
791 case wxDragCopy: break;
792
793 case wxDragError:
794 wxLogError(wxT("An error occurred during drag and drop operation"));
795 case wxDragNone:
796 case wxDragCancel:
797 Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take'
798 SetCaretPosition(oldPos);
799 SetFocusObject(oldFocus, false);
800 default: break;
801 }
802 EndBatchUndo();
803
804 m_preDrag = false;
805 return;
806 }
807 }
808#endif // wxUSE_DRAG_AND_DROP
809
5d7836c4
JS
810 wxClientDC dc(this);
811 PrepareDC(dc);
812 dc.SetFont(GetFont());
7fe8059f 813
5d7836c4
JS
814 long position = 0;
815 wxPoint logicalPt = event.GetLogicalPosition(dc);
603f702b
JS
816 wxRichTextObject* hitObj = NULL;
817 wxRichTextObject* contextObj = NULL;
4fe83b93 818
603f702b 819 int flags = 0;
4fe83b93 820
603f702b
JS
821 // If we're dragging, let's only consider positions at this level; otherwise
822 // selecting a range is not going to work.
823 wxRichTextParagraphLayoutBox* container = & GetBuffer();
824 if (m_dragging)
825 {
826 flags = wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS;
827 container = GetFocusObject();
828 }
8db2e3ef
JS
829 wxRichTextDrawingContext context(& GetBuffer());
830 int hit = container->HitTest(dc, context, logicalPt, position, & hitObj, & contextObj, flags);
4fe83b93 831
d2d0adc7 832 // See if we need to change the cursor
88bee7e6 833
d2d0adc7 834 {
603f702b 835 if (hit != wxRICHTEXT_HITTEST_NONE && !(hit & wxRICHTEXT_HITTEST_OUTSIDE) && hitObj)
d2d0adc7 836 {
603f702b 837 wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
590a0f8b
JS
838 if (actualContainer)
839 ProcessMouseMovement(actualContainer, hitObj, position, logicalPt);
d2d0adc7 840 }
f262b25c
JS
841 else
842 SetCursor(m_textCursor);
d2d0adc7
JS
843 }
844
845 if (!event.Dragging())
846 {
847 event.Skip();
848 return;
849 }
4fe83b93 850
0c0e063e
JS
851 if (m_dragging
852#if wxUSE_DRAG_AND_DROP
853 && !m_preDrag
854#endif
855 )
5d7836c4 856 {
603f702b
JS
857 wxRichTextParagraphLayoutBox* commonAncestor = NULL;
858 wxRichTextParagraphLayoutBox* otherContainer = NULL;
859 wxRichTextParagraphLayoutBox* firstContainer = NULL;
860
861 // Check for dragging across multiple containers
862 long position2 = 0;
863 wxRichTextObject* hitObj2 = NULL, *contextObj2 = NULL;
8db2e3ef 864 int hit2 = GetBuffer().HitTest(dc, context, logicalPt, position2, & hitObj2, & contextObj2, 0);
603f702b
JS
865 if (hit2 != wxRICHTEXT_HITTEST_NONE && !(hit2 & wxRICHTEXT_HITTEST_OUTSIDE) && hitObj2 && hitObj != hitObj2)
866 {
867 // See if we can find a common ancestor
868 if (m_selectionState == wxRichTextCtrlSelectionState_Normal)
869 {
870 firstContainer = GetFocusObject();
871 commonAncestor = wxDynamicCast(firstContainer->GetParent(), wxRichTextParagraphLayoutBox);
872 }
873 else
874 {
875 firstContainer = wxDynamicCast(m_selectionAnchorObject, wxRichTextParagraphLayoutBox);
876 //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table)
877 // is the common ancestor.
878 commonAncestor = wxDynamicCast(firstContainer->GetParent(), wxRichTextParagraphLayoutBox);
879 }
4fe83b93 880
603f702b
JS
881 if (commonAncestor && commonAncestor->HandlesChildSelections())
882 {
883 wxRichTextObject* p = hitObj2;
884 while (p)
885 {
886 if (p->GetParent() == commonAncestor)
887 {
888 otherContainer = wxDynamicCast(p, wxRichTextParagraphLayoutBox);
889 break;
890 }
891 p = p->GetParent();
892 }
893 }
5d7836c4 894
603f702b
JS
895 if (commonAncestor && firstContainer && otherContainer)
896 {
897 // We have now got a second container that shares a parent with the current or anchor object.
898 if (m_selectionState == wxRichTextCtrlSelectionState_Normal)
899 {
900 // Don't go into common-ancestor selection mode if we still have the same
901 // container.
902 if (otherContainer != firstContainer)
903 {
904 m_selectionState = wxRichTextCtrlSelectionState_CommonAncestor;
905 m_selectionAnchorObject = firstContainer;
906 m_selectionAnchor = firstContainer->GetRange().GetStart();
5d7836c4 907
603f702b
JS
908 // The common ancestor, such as a table, returns the cell selection
909 // between the anchor and current position.
910 m_selection = commonAncestor->GetSelection(m_selectionAnchor, otherContainer->GetRange().GetStart());
911 }
912 }
913 else
914 {
915 m_selection = commonAncestor->GetSelection(m_selectionAnchor, otherContainer->GetRange().GetStart());
916 }
5d7836c4 917
603f702b 918 Refresh();
7fe8059f 919
603f702b
JS
920 if (otherContainer->AcceptsFocus())
921 SetFocusObject(otherContainer, false /* don't set caret and clear selection */);
4fe83b93 922 MoveCaret(-1, false);
603f702b
JS
923 SetDefaultStyleToCursorStyle();
924 }
5d7836c4
JS
925 }
926 }
603f702b 927
0c0e063e
JS
928 if (hitObj && m_dragging && hit != wxRICHTEXT_HITTEST_NONE && m_selectionState == wxRichTextCtrlSelectionState_Normal
929#if wxUSE_DRAG_AND_DROP
930 && !m_preDrag
931#endif
932 )
603f702b
JS
933 {
934 // TODO: test closeness
935 SetCaretPositionAfterClick(container, position, hit, true /* extend selection */);
936 }
5d7836c4
JS
937}
938
939/// Right-click
52170c5b 940void wxRichTextCtrl::OnRightClick(wxMouseEvent& event)
5d7836c4
JS
941{
942 SetFocus();
4fe83b93 943
603f702b
JS
944 wxClientDC dc(this);
945 PrepareDC(dc);
946 dc.SetFont(GetFont());
947
948 long position = 0;
949 wxPoint logicalPt = event.GetLogicalPosition(dc);
950 wxRichTextObject* hitObj = NULL;
951 wxRichTextObject* contextObj = NULL;
8db2e3ef
JS
952 wxRichTextDrawingContext context(& GetBuffer());
953 int hit = GetFocusObject()->HitTest(dc, context, logicalPt, position, & hitObj, & contextObj);
603f702b
JS
954
955 if (hitObj && hitObj->GetContainer() != GetFocusObject())
956 {
957 wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
958 if (actualContainer && actualContainer->AcceptsFocus())
959 {
960 SetFocusObject(actualContainer, false /* don't set caret position yet */);
961 SetCaretPositionAfterClick(actualContainer, position, hit);
962 }
963 }
db6634e0
JS
964
965 wxRichTextEvent cmdEvent(
966 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK,
967 GetId());
968 cmdEvent.SetEventObject(this);
603f702b
JS
969 cmdEvent.SetPosition(position);
970 if (hitObj)
971 cmdEvent.SetContainer(hitObj->GetContainer());
1a75935d 972
52170c5b
JS
973 if (!GetEventHandler()->ProcessEvent(cmdEvent))
974 event.Skip();
5d7836c4
JS
975}
976
977/// Left-double-click
db6634e0 978void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& WXUNUSED(event))
5d7836c4 979{
db6634e0
JS
980 wxRichTextEvent cmdEvent(
981 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK,
982 GetId());
983 cmdEvent.SetEventObject(this);
984 cmdEvent.SetPosition(m_caretPosition+1);
603f702b 985 cmdEvent.SetContainer(GetFocusObject());
1a75935d 986
db6634e0
JS
987 if (!GetEventHandler()->ProcessEvent(cmdEvent))
988 {
989 SelectWord(GetCaretPosition()+1);
990 }
5d7836c4
JS
991}
992
993/// Middle-click
994void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event)
995{
db6634e0
JS
996 wxRichTextEvent cmdEvent(
997 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK,
998 GetId());
999 cmdEvent.SetEventObject(this);
1000 cmdEvent.SetPosition(m_caretPosition+1);
603f702b 1001 cmdEvent.SetContainer(GetFocusObject());
1a75935d 1002
db6634e0
JS
1003 if (!GetEventHandler()->ProcessEvent(cmdEvent))
1004 event.Skip();
5dec941a
JS
1005
1006#if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION
1007 // Paste any PRIMARY selection, if it exists
1008 wxTheClipboard->UsePrimarySelection(true);
1009 Paste();
1010 wxTheClipboard->UsePrimarySelection(false);
1011#endif
5d7836c4
JS
1012}
1013
1014/// Key press
1015void wxRichTextCtrl::OnChar(wxKeyEvent& event)
1016{
1017 int flags = 0;
3e3a754f 1018 if (event.CmdDown())
5d7836c4
JS
1019 flags |= wxRICHTEXT_CTRL_DOWN;
1020 if (event.ShiftDown())
1021 flags |= wxRICHTEXT_SHIFT_DOWN;
1022 if (event.AltDown())
1023 flags |= wxRICHTEXT_ALT_DOWN;
1024
c80e8cbe
JS
1025 if (event.GetEventType() == wxEVT_KEY_DOWN)
1026 {
7a34307e 1027 if (event.IsKeyInCategory(WXK_CATEGORY_NAVIGATION))
8c9e72f9
JS
1028 {
1029 KeyboardNavigate(event.GetKeyCode(), flags);
1030 return;
1031 }
1032
1033 long keycode = event.GetKeyCode();
1034 switch ( keycode )
1035 {
1036 case WXK_ESCAPE:
1037 case WXK_START:
1038 case WXK_LBUTTON:
1039 case WXK_RBUTTON:
1040 case WXK_CANCEL:
1041 case WXK_MBUTTON:
1042 case WXK_CLEAR:
1043 case WXK_SHIFT:
1044 case WXK_ALT:
1045 case WXK_CONTROL:
1046 case WXK_MENU:
1047 case WXK_PAUSE:
1048 case WXK_CAPITAL:
1049 case WXK_END:
1050 case WXK_HOME:
1051 case WXK_LEFT:
1052 case WXK_UP:
1053 case WXK_RIGHT:
1054 case WXK_DOWN:
1055 case WXK_SELECT:
1056 case WXK_PRINT:
1057 case WXK_EXECUTE:
1058 case WXK_SNAPSHOT:
1059 case WXK_INSERT:
1060 case WXK_HELP:
1061 case WXK_F1:
1062 case WXK_F2:
1063 case WXK_F3:
1064 case WXK_F4:
1065 case WXK_F5:
1066 case WXK_F6:
1067 case WXK_F7:
1068 case WXK_F8:
1069 case WXK_F9:
1070 case WXK_F10:
1071 case WXK_F11:
1072 case WXK_F12:
1073 case WXK_F13:
1074 case WXK_F14:
1075 case WXK_F15:
1076 case WXK_F16:
1077 case WXK_F17:
1078 case WXK_F18:
1079 case WXK_F19:
1080 case WXK_F20:
1081 case WXK_F21:
1082 case WXK_F22:
1083 case WXK_F23:
1084 case WXK_F24:
1085 case WXK_NUMLOCK:
1086 case WXK_SCROLL:
1087 case WXK_PAGEUP:
1088 case WXK_PAGEDOWN:
1089 case WXK_NUMPAD_F1:
1090 case WXK_NUMPAD_F2:
1091 case WXK_NUMPAD_F3:
1092 case WXK_NUMPAD_F4:
1093 case WXK_NUMPAD_HOME:
1094 case WXK_NUMPAD_LEFT:
1095 case WXK_NUMPAD_UP:
1096 case WXK_NUMPAD_RIGHT:
1097 case WXK_NUMPAD_DOWN:
1098 case WXK_NUMPAD_PAGEUP:
1099 case WXK_NUMPAD_PAGEDOWN:
1100 case WXK_NUMPAD_END:
1101 case WXK_NUMPAD_BEGIN:
1102 case WXK_NUMPAD_INSERT:
8c9e72f9
JS
1103 case WXK_WINDOWS_LEFT:
1104 {
1105 return;
1106 }
1107 default:
1108 {
1109 }
1110 }
1111
c80e8cbe
JS
1112 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
1113 if (event.CmdDown() && event.GetKeyCode() == WXK_BACK)
1114 {
cc2aecde
JS
1115 if (!IsEditable())
1116 {
1117 return;
1118 }
1119
1120 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1121 {
1122 return;
1123 }
1124
c80e8cbe
JS
1125 BeginBatchUndo(_("Delete Text"));
1126
1127 long newPos = m_caretPosition;
1128
29dbface 1129 bool processed = DeleteSelectedContent(& newPos);
c80e8cbe 1130
cc2aecde
JS
1131 int deletions = 0;
1132 if (processed)
1133 deletions ++;
1134
c80e8cbe
JS
1135 // Submit range in character positions, which are greater than caret positions,
1136 // so subtract 1 for deleted character and add 1 for conversion to character position.
1137 if (newPos > -1)
1138 {
c80e8cbe
JS
1139 if (event.CmdDown())
1140 {
1141 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
8251c171 1142 if (pos < newPos)
c80e8cbe 1143 {
cc2aecde
JS
1144 wxRichTextRange range(pos+1, newPos);
1145 if (CanDeleteRange(* GetFocusObject(), range.FromInternal()))
1146 {
1147 GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer());
1148 deletions ++;
1149 }
c80e8cbe
JS
1150 processed = true;
1151 }
1152 }
1153
1154 if (!processed)
cc2aecde
JS
1155 {
1156 wxRichTextRange range(newPos, newPos);
1157 if (CanDeleteRange(* GetFocusObject(), range.FromInternal()))
1158 {
1159 GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer());
1160 deletions ++;
1161 }
1162 }
c80e8cbe
JS
1163 }
1164
1165 EndBatchUndo();
1166
1167 if (GetLastPosition() == -1)
1168 {
603f702b 1169 GetFocusObject()->Reset();
c80e8cbe
JS
1170
1171 m_caretPosition = -1;
1172 PositionCaret();
1173 SetDefaultStyleToCursorStyle();
1174 }
1175
1176 ScrollIntoView(m_caretPosition, WXK_LEFT);
1177
cc2aecde
JS
1178 if (deletions > 0)
1179 {
1180 wxRichTextEvent cmdEvent(
1181 wxEVT_COMMAND_RICHTEXT_DELETE,
1182 GetId());
1183 cmdEvent.SetEventObject(this);
1184 cmdEvent.SetFlags(flags);
1185 cmdEvent.SetPosition(m_caretPosition+1);
1186 cmdEvent.SetContainer(GetFocusObject());
1187 GetEventHandler()->ProcessEvent(cmdEvent);
1188 }
c80e8cbe
JS
1189
1190 Update();
1191 }
1192 else
1193 event.Skip();
1194
1195 return;
1196 }
1197
99257cbe
VZ
1198 // all the other keys modify the controls contents which shouldn't be
1199 // possible if we're read-only
1200 if ( !IsEditable() )
1201 {
1202 event.Skip();
1203 return;
1204 }
1205
1206 if (event.GetKeyCode() == WXK_RETURN)
5d7836c4 1207 {
cc2aecde
JS
1208 if (!CanInsertContent(* GetFocusObject(), m_caretPosition+1))
1209 return;
5d7836c4
JS
1210
1211 long newPos = m_caretPosition;
1212
cc2aecde
JS
1213 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1214 {
1215 return;
1216 }
1217
1218 BeginBatchUndo(_("Insert Text"));
1219
5d7836c4
JS
1220 DeleteSelectedContent(& newPos);
1221
ff76711f
JS
1222 if (event.ShiftDown())
1223 {
1224 wxString text;
1225 text = wxRichTextLineBreakChar;
4e63bfb9 1226 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos+1, text, this);
4bf9e5bc 1227 m_caretAtLineStart = true;
043c0d58 1228 PositionCaret();
ff76711f
JS
1229 }
1230 else
4e63bfb9 1231 GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE|wxRICHTEXT_INSERT_INTERACTIVE);
ff76711f 1232
d2d0adc7 1233 EndBatchUndo();
0a21bea0 1234 SetDefaultStyleToCursorStyle();
d2d0adc7
JS
1235
1236 ScrollIntoView(m_caretPosition, WXK_RIGHT);
5d7836c4
JS
1237
1238 wxRichTextEvent cmdEvent(
1239 wxEVT_COMMAND_RICHTEXT_RETURN,
1240 GetId());
1241 cmdEvent.SetEventObject(this);
1242 cmdEvent.SetFlags(flags);
5912d19e 1243 cmdEvent.SetPosition(newPos+1);
603f702b 1244 cmdEvent.SetContainer(GetFocusObject());
5912d19e 1245
d2d0adc7
JS
1246 if (!GetEventHandler()->ProcessEvent(cmdEvent))
1247 {
1248 // Generate conventional event
1249 wxCommandEvent textEvent(wxEVT_COMMAND_TEXT_ENTER, GetId());
1250 InitCommandEvent(textEvent);
88bee7e6 1251
d2d0adc7
JS
1252 GetEventHandler()->ProcessEvent(textEvent);
1253 }
621ae374 1254 Update();
5d7836c4
JS
1255 }
1256 else if (event.GetKeyCode() == WXK_BACK)
1257 {
c80e8cbe
JS
1258 long newPos = m_caretPosition;
1259
cc2aecde
JS
1260 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1261 {
1262 return;
1263 }
1264
1265 BeginBatchUndo(_("Delete Text"));
1266
dc5de0dd 1267 bool processed = DeleteSelectedContent(& newPos);
c80e8cbe 1268
cc2aecde
JS
1269 int deletions = 0;
1270 if (processed)
1271 deletions ++;
1272
5d7836c4
JS
1273 // Submit range in character positions, which are greater than caret positions,
1274 // so subtract 1 for deleted character and add 1 for conversion to character position.
c80e8cbe 1275 if (newPos > -1)
5d7836c4 1276 {
62381daa
JS
1277 if (event.CmdDown())
1278 {
1279 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
8251c171 1280 if (pos < newPos)
62381daa 1281 {
cc2aecde
JS
1282 wxRichTextRange range(pos+1, newPos);
1283 if (CanDeleteRange(* GetFocusObject(), range.FromInternal()))
1284 {
1285 GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer());
1286 deletions ++;
1287 }
62381daa
JS
1288 processed = true;
1289 }
1290 }
1291
1292 if (!processed)
cc2aecde
JS
1293 {
1294 wxRichTextRange range(newPos, newPos);
1295 if (CanDeleteRange(* GetFocusObject(), range.FromInternal()))
1296 {
1297 GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer());
1298 deletions ++;
1299 }
1300 }
5d7836c4 1301 }
5d7836c4
JS
1302
1303 EndBatchUndo();
1304
5d7836c4
JS
1305 if (GetLastPosition() == -1)
1306 {
603f702b 1307 GetFocusObject()->Reset();
5d7836c4
JS
1308
1309 m_caretPosition = -1;
1310 PositionCaret();
0a21bea0 1311 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1312 }
1313
f9ed2d16 1314 ScrollIntoView(m_caretPosition, WXK_LEFT);
5912d19e 1315
cc2aecde
JS
1316 if (deletions > 0)
1317 {
1318 wxRichTextEvent cmdEvent(
1319 wxEVT_COMMAND_RICHTEXT_DELETE,
1320 GetId());
1321 cmdEvent.SetEventObject(this);
1322 cmdEvent.SetFlags(flags);
1323 cmdEvent.SetPosition(m_caretPosition+1);
1324 cmdEvent.SetContainer(GetFocusObject());
1325 GetEventHandler()->ProcessEvent(cmdEvent);
1326 }
5912d19e 1327
621ae374 1328 Update();
5d7836c4
JS
1329 }
1330 else if (event.GetKeyCode() == WXK_DELETE)
1331 {
c80e8cbe
JS
1332 long newPos = m_caretPosition;
1333
cc2aecde
JS
1334 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1335 {
1336 return;
1337 }
1338
1339 BeginBatchUndo(_("Delete Text"));
1340
dc5de0dd 1341 bool processed = DeleteSelectedContent(& newPos);
c80e8cbe 1342
cc2aecde
JS
1343 int deletions = 0;
1344 if (processed)
1345 deletions ++;
1346
5d7836c4 1347 // Submit range in character positions, which are greater than caret positions,
603f702b 1348 if (newPos < GetFocusObject()->GetOwnRange().GetEnd()+1)
5d7836c4 1349 {
c80e8cbe
JS
1350 if (event.CmdDown())
1351 {
1352 long pos = wxRichTextCtrl::FindNextWordPosition(1);
1353 if (pos != -1 && (pos > newPos))
1354 {
cc2aecde
JS
1355 wxRichTextRange range(newPos+1, pos);
1356 if (CanDeleteRange(* GetFocusObject(), range.FromInternal()))
1357 {
1358 GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer());
1359 deletions ++;
1360 }
c80e8cbe
JS
1361 processed = true;
1362 }
1363 }
1364
fc6d9d0d 1365 if (!processed && newPos < (GetLastPosition()-1))
cc2aecde
JS
1366 {
1367 wxRichTextRange range(newPos+1, newPos+1);
1368 if (CanDeleteRange(* GetFocusObject(), range.FromInternal()))
1369 {
1370 GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer());
1371 deletions ++;
1372 }
1373 }
5d7836c4 1374 }
5d7836c4
JS
1375
1376 EndBatchUndo();
1377
5d7836c4
JS
1378 if (GetLastPosition() == -1)
1379 {
603f702b 1380 GetFocusObject()->Reset();
5d7836c4
JS
1381
1382 m_caretPosition = -1;
1383 PositionCaret();
0a21bea0 1384 SetDefaultStyleToCursorStyle();
5d7836c4 1385 }
5912d19e 1386
575be976
JS
1387 ScrollIntoView(m_caretPosition, WXK_LEFT);
1388
cc2aecde
JS
1389 if (deletions > 0)
1390 {
1391 wxRichTextEvent cmdEvent(
1392 wxEVT_COMMAND_RICHTEXT_DELETE,
1393 GetId());
1394 cmdEvent.SetEventObject(this);
1395 cmdEvent.SetFlags(flags);
1396 cmdEvent.SetPosition(m_caretPosition+1);
1397 cmdEvent.SetContainer(GetFocusObject());
1398 GetEventHandler()->ProcessEvent(cmdEvent);
1399 }
5912d19e 1400
621ae374 1401 Update();
5d7836c4
JS
1402 }
1403 else
1404 {
3e3a754f
JS
1405 long keycode = event.GetKeyCode();
1406 switch ( keycode )
1407 {
1408 case WXK_ESCAPE:
3e3a754f
JS
1409 {
1410 event.Skip();
1411 return;
1412 }
1807a1f3 1413
3e3a754f
JS
1414 default:
1415 {
cfceb4a8 1416#ifdef __WXMAC__
91babb9d 1417 if (event.CmdDown())
cfceb4a8 1418#else
d2bb72ff
JS
1419 // Fixes AltGr+key with European input languages on Windows
1420 if ((event.CmdDown() && !event.AltDown()) || (event.AltDown() && !event.CmdDown()))
cfceb4a8 1421#endif
3e3a754f
JS
1422 {
1423 event.Skip();
1424 return;
1425 }
41a85215 1426
5912d19e
JS
1427 wxRichTextEvent cmdEvent(
1428 wxEVT_COMMAND_RICHTEXT_CHARACTER,
1429 GetId());
1430 cmdEvent.SetEventObject(this);
1431 cmdEvent.SetFlags(flags);
0b14a7bd
JS
1432#if wxUSE_UNICODE
1433 cmdEvent.SetCharacter(event.GetUnicodeKey());
1434#else
5912d19e 1435 cmdEvent.SetCharacter((wxChar) keycode);
0b14a7bd 1436#endif
5912d19e 1437 cmdEvent.SetPosition(m_caretPosition+1);
603f702b 1438 cmdEvent.SetContainer(GetFocusObject());
88bee7e6 1439
dadd4f55
JS
1440 if (keycode == wxT('\t'))
1441 {
1442 // See if we need to promote or demote the selection or paragraph at the cursor
1443 // position, instead of inserting a tab.
1444 long pos = GetAdjustedCaretPosition(GetCaretPosition());
603f702b 1445 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos);
dadd4f55
JS
1446 if (para && para->GetRange().GetStart() == pos && para->GetAttributes().HasListStyleName())
1447 {
1448 wxRichTextRange range;
1449 if (HasSelection())
1450 range = GetSelectionRange();
1451 else
1452 range = para->GetRange().FromInternal();
41a85215 1453
dadd4f55
JS
1454 int promoteBy = event.ShiftDown() ? 1 : -1;
1455
1456 PromoteList(promoteBy, range, NULL);
d2d0adc7 1457
5912d19e
JS
1458 GetEventHandler()->ProcessEvent(cmdEvent);
1459
dadd4f55
JS
1460 return;
1461 }
1462 }
5d7836c4 1463
cc2aecde
JS
1464 if (!CanInsertContent(* GetFocusObject(), m_caretPosition+1))
1465 return;
1466
1467 if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange()))
1468 return;
1469
3e3a754f 1470 BeginBatchUndo(_("Insert Text"));
5d7836c4 1471
3e3a754f
JS
1472 long newPos = m_caretPosition;
1473 DeleteSelectedContent(& newPos);
5d7836c4 1474
0b14a7bd
JS
1475#if wxUSE_UNICODE
1476 wxString str = event.GetUnicodeKey();
1477#else
3e3a754f 1478 wxString str = (wxChar) event.GetKeyCode();
0b14a7bd 1479#endif
4e63bfb9 1480 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos+1, str, this, 0);
5d7836c4 1481
3e3a754f
JS
1482 EndBatchUndo();
1483
0a21bea0 1484 SetDefaultStyleToCursorStyle();
3e3a754f 1485 ScrollIntoView(m_caretPosition, WXK_RIGHT);
88bee7e6 1486
c1dd924d 1487 cmdEvent.SetPosition(m_caretPosition);
5912d19e
JS
1488 GetEventHandler()->ProcessEvent(cmdEvent);
1489
621ae374 1490 Update();
3e3a754f
JS
1491 }
1492 }
5d7836c4 1493 }
5d7836c4
JS
1494}
1495
8db2e3ef 1496bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox* container, wxRichTextObject* WXUNUSED(obj), long position, const wxPoint& WXUNUSED(pos))
590a0f8b
JS
1497{
1498 wxRichTextAttr attr;
1499 if (container && GetStyle(position, attr, container))
1500 {
1501 if (attr.HasFlag(wxTEXT_ATTR_URL))
1502 {
1503 SetCursor(m_urlCursor);
1504 }
1505 else if (!attr.HasFlag(wxTEXT_ATTR_URL))
1506 {
1507 SetCursor(m_textCursor);
1508 }
1509 return true;
1510 }
1511 else
1512 return false;
1513}
1514
5d7836c4
JS
1515/// Delete content if there is a selection, e.g. when pressing a key.
1516bool wxRichTextCtrl::DeleteSelectedContent(long* newPos)
1517{
1518 if (HasSelection())
1519 {
603f702b
JS
1520 long pos = m_selection.GetRange().GetStart();
1521 wxRichTextRange range = m_selection.GetRange();
205a1ad9
JS
1522
1523 // SelectAll causes more to be selected than doing it interactively,
1524 // and causes a new paragraph to be inserted. So for multiline buffers,
1525 // don't delete the final position.
1526 if (range.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0)
1527 range.SetEnd(range.GetEnd()-1);
1528
603f702b
JS
1529 GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer());
1530 m_selection.Reset();
1531 m_selectionState = wxRichTextCtrlSelectionState_Normal;
5d7836c4
JS
1532
1533 if (newPos)
1534 *newPos = pos-1;
1535 return true;
1536 }
1537 else
1538 return false;
1539}
1540
1541/// Keyboard navigation
1542
1543/*
1544
1545Left: left one character
1546Right: right one character
1547Up: up one line
1548Down: down one line
1549Ctrl-Left: left one word
1550Ctrl-Right: right one word
1551Ctrl-Up: previous paragraph start
1552Ctrl-Down: next start of paragraph
1553Home: start of line
1554End: end of line
1555Ctrl-Home: start of document
1556Ctrl-End: end of document
1557Page-Up: Up a screen
1558Page-Down: Down a screen
1559
1560Maybe:
1561
1562Ctrl-Alt-PgUp: Start of window
1563Ctrl-Alt-PgDn: End of window
1564F8: Start selection mode
1565Esc: End selection mode
1566
1567Adding Shift does the above but starts/extends selection.
1568
1569
1570 */
1571
0bab774b 1572bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags)
5d7836c4
JS
1573{
1574 bool success = false;
5d7836c4 1575
3e3a754f 1576 if (keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT)
5d7836c4
JS
1577 {
1578 if (flags & wxRICHTEXT_CTRL_DOWN)
1579 success = WordRight(1, flags);
1580 else
1581 success = MoveRight(1, flags);
1582 }
3e3a754f 1583 else if (keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT)
5d7836c4
JS
1584 {
1585 if (flags & wxRICHTEXT_CTRL_DOWN)
1586 success = WordLeft(1, flags);
1587 else
1588 success = MoveLeft(1, flags);
1589 }
3e3a754f 1590 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP)
5d7836c4
JS
1591 {
1592 if (flags & wxRICHTEXT_CTRL_DOWN)
1593 success = MoveToParagraphStart(flags);
1594 else
1595 success = MoveUp(1, flags);
1596 }
3e3a754f 1597 else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN)
5d7836c4
JS
1598 {
1599 if (flags & wxRICHTEXT_CTRL_DOWN)
1600 success = MoveToParagraphEnd(flags);
1601 else
1602 success = MoveDown(1, flags);
1603 }
3e3a754f 1604 else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP)
5d7836c4
JS
1605 {
1606 success = PageUp(1, flags);
1607 }
3e3a754f 1608 else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
5d7836c4
JS
1609 {
1610 success = PageDown(1, flags);
1611 }
3e3a754f 1612 else if (keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME)
5d7836c4
JS
1613 {
1614 if (flags & wxRICHTEXT_CTRL_DOWN)
1615 success = MoveHome(flags);
1616 else
1617 success = MoveToLineStart(flags);
1618 }
3e3a754f 1619 else if (keyCode == WXK_END || keyCode == WXK_NUMPAD_END)
5d7836c4
JS
1620 {
1621 if (flags & wxRICHTEXT_CTRL_DOWN)
1622 success = MoveEnd(flags);
1623 else
1624 success = MoveToLineEnd(flags);
1625 }
1626
1627 if (success)
1628 {
1629 ScrollIntoView(m_caretPosition, keyCode);
0a21bea0 1630 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1631 }
1632
5d7836c4
JS
1633 return success;
1634}
1635
1636/// Extend the selection. Selections are in caret positions.
1637bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags)
1638{
1639 if (flags & wxRICHTEXT_SHIFT_DOWN)
1640 {
e2d0875a
JS
1641 if (oldPos == newPos)
1642 return false;
1643
603f702b 1644 wxRichTextSelection oldSelection = m_selection;
4fe83b93 1645
603f702b 1646 m_selection.SetContainer(GetFocusObject());
4fe83b93 1647
603f702b
JS
1648 wxRichTextRange oldRange;
1649 if (m_selection.IsValid())
1650 oldRange = m_selection.GetRange();
1651 else
1652 oldRange = wxRICHTEXT_NO_SELECTION;
1653 wxRichTextRange newRange;
7051fa41 1654
5d7836c4 1655 // If not currently selecting, start selecting
603f702b 1656 if (oldRange.GetStart() == -2)
5d7836c4
JS
1657 {
1658 m_selectionAnchor = oldPos;
1659
1660 if (oldPos > newPos)
603f702b 1661 newRange.SetRange(newPos+1, oldPos);
5d7836c4 1662 else
603f702b 1663 newRange.SetRange(oldPos+1, newPos);
5d7836c4
JS
1664 }
1665 else
1666 {
1667 // Always ensure that the selection range start is greater than
1668 // the end.
1669 if (newPos > m_selectionAnchor)
603f702b 1670 newRange.SetRange(m_selectionAnchor+1, newPos);
e2d0875a 1671 else if (newPos == m_selectionAnchor)
603f702b 1672 newRange = wxRichTextRange(-2, -2);
5d7836c4 1673 else
603f702b 1674 newRange.SetRange(newPos+1, m_selectionAnchor);
5d7836c4 1675 }
4fe83b93 1676
603f702b 1677 m_selection.SetRange(newRange);
5d7836c4 1678
603f702b 1679 RefreshForSelectionChange(oldSelection, m_selection);
7051fa41 1680
603f702b 1681 if (newRange.GetStart() > newRange.GetEnd())
5d7836c4
JS
1682 {
1683 wxLogDebug(wxT("Strange selection range"));
1684 }
7fe8059f 1685
5d7836c4
JS
1686 return true;
1687 }
1688 else
1689 return false;
1690}
1691
1692/// Scroll into view, returning true if we scrolled.
1693/// This takes a _caret_ position.
1694bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode)
1695{
1696 wxRichTextLine* line = GetVisibleLineForCaretPosition(position);
1697
1698 if (!line)
1699 return false;
1700
1701 int ppuX, ppuY;
1702 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1703
3956d02a
JS
1704 int startXUnits, startYUnits;
1705 GetViewStart(& startXUnits, & startYUnits);
1706 int startY = startYUnits * ppuY;
5d7836c4 1707
8d7eaf91 1708 int sx = 0, sy = 0;
5d7836c4 1709 GetVirtualSize(& sx, & sy);
3956d02a
JS
1710 int sxUnits = 0;
1711 int syUnits = 0;
5d7836c4 1712 if (ppuY != 0)
3956d02a 1713 syUnits = sy/ppuY;
5d7836c4
JS
1714
1715 wxRect rect = line->GetRect();
1716
1717 bool scrolled = false;
1718
1719 wxSize clientSize = GetClientSize();
4fe83b93 1720
603f702b
JS
1721 int leftMargin, rightMargin, topMargin, bottomMargin;
1722
1723 {
1724 wxClientDC dc(this);
1725 wxRichTextObject::GetTotalMargin(dc, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin, rightMargin,
1726 topMargin, bottomMargin);
1727 }
1728// clientSize.y -= GetBuffer().GetBottomMargin();
1729 clientSize.y -= bottomMargin;
e9f10004
JS
1730
1731 if (GetWindowStyle() & wxRE_CENTRE_CARET)
1732 {
1733 int y = rect.y - GetClientSize().y/2;
1734 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1735 if (y >= 0 && (y + clientSize.y) < GetBuffer().GetCachedSize().y)
1736 {
1737 if (startYUnits != yUnits)
1738 {
1739 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1740 scrolled = true;
1741 }
1742#if !wxRICHTEXT_USE_OWN_CARET
1743 if (scrolled)
1744#endif
1745 PositionCaret();
1746
1747 return scrolled;
1748 }
1749 }
5d7836c4
JS
1750
1751 // Going down
3e3a754f 1752 if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN ||
ec36b155 1753 keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT ||
3e3a754f
JS
1754 keyCode == WXK_END || keyCode == WXK_NUMPAD_END ||
1755 keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
5d7836c4
JS
1756 {
1757 if ((rect.y + rect.height) > (clientSize.y + startY))
1758 {
1759 // Make it scroll so this item is at the bottom
1760 // of the window
1761 int y = rect.y - (clientSize.y - rect.height);
3956d02a 1762 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 1763
3956d02a
JS
1764 // If we're still off the screen, scroll another line down
1765 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1766 yUnits ++;
1767
1768 if (startYUnits != yUnits)
c59f6793 1769 {
3956d02a 1770 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1771 scrolled = true;
1772 }
5d7836c4 1773 }
e9f10004 1774 else if (rect.y < (startY + GetBuffer().GetTopMargin()))
5d7836c4
JS
1775 {
1776 // Make it scroll so this item is at the top
1777 // of the window
e9f10004 1778 int y = rect.y - GetBuffer().GetTopMargin();
3956d02a 1779 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 1780
3956d02a 1781 if (startYUnits != yUnits)
c59f6793 1782 {
3956d02a 1783 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1784 scrolled = true;
1785 }
5d7836c4 1786 }
5d7836c4
JS
1787 }
1788 // Going up
3e3a754f
JS
1789 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP ||
1790 keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT ||
1791 keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME ||
1792 keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP )
5d7836c4 1793 {
e9f10004 1794 if (rect.y < (startY + GetBuffer().GetBottomMargin()))
5d7836c4
JS
1795 {
1796 // Make it scroll so this item is at the top
1797 // of the window
e9f10004 1798 int y = rect.y - GetBuffer().GetTopMargin();
3956d02a 1799 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 1800
3956d02a 1801 if (startYUnits != yUnits)
c59f6793 1802 {
3956d02a 1803 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1804 scrolled = true;
1805 }
5d7836c4
JS
1806 }
1807 else if ((rect.y + rect.height) > (clientSize.y + startY))
1808 {
1809 // Make it scroll so this item is at the bottom
1810 // of the window
1811 int y = rect.y - (clientSize.y - rect.height);
3956d02a
JS
1812 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1813
1814 // If we're still off the screen, scroll another line down
1815 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1816 yUnits ++;
c59f6793 1817
3956d02a 1818 if (startYUnits != yUnits)
c59f6793 1819 {
3956d02a 1820 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
1821 scrolled = true;
1822 }
5d7836c4 1823 }
5d7836c4 1824 }
109bfc88 1825
1c13f06e 1826#if !wxRICHTEXT_USE_OWN_CARET
109bfc88 1827 if (scrolled)
1c13f06e 1828#endif
109bfc88 1829 PositionCaret();
5d7836c4
JS
1830
1831 return scrolled;
1832}
1833
1834/// Is the given position visible on the screen?
1835bool wxRichTextCtrl::IsPositionVisible(long pos) const
1836{
1837 wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1);
1838
1839 if (!line)
1840 return false;
1841
1842 int ppuX, ppuY;
1843 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1844
1845 int startX, startY;
1846 GetViewStart(& startX, & startY);
1847 startX = 0;
1848 startY = startY * ppuY;
1849
5d7836c4 1850 wxRect rect = line->GetRect();
5d7836c4 1851 wxSize clientSize = GetClientSize();
e9f10004 1852 clientSize.y -= GetBuffer().GetBottomMargin();
5d7836c4 1853
1437c7fb 1854 return (rect.GetTop() >= (startY + GetBuffer().GetTopMargin())) && (rect.GetBottom() <= (startY + clientSize.y));
5d7836c4
JS
1855}
1856
1857void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart)
1858{
1859 m_caretPosition = position;
1860 m_caretAtLineStart = showAtLineStart;
1861}
1862
1863/// Move caret one visual step forward: this may mean setting a flag
1864/// and keeping the same position if we're going from the end of one line
1865/// to the start of the next, which may be the exact same caret position.
1866void wxRichTextCtrl::MoveCaretForward(long oldPosition)
1867{
603f702b 1868 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(oldPosition);
5d7836c4
JS
1869
1870 // Only do the check if we're not at the end of the paragraph (where things work OK
1871 // anyway)
1872 if (para && (oldPosition != para->GetRange().GetEnd() - 1))
1873 {
603f702b 1874 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(oldPosition);
5d7836c4
JS
1875
1876 if (line)
1877 {
1e967276
JS
1878 wxRichTextRange lineRange = line->GetAbsoluteRange();
1879
5d7836c4
JS
1880 // We're at the end of a line. See whether we need to
1881 // stay at the same actual caret position but change visual
1882 // position, or not.
1e967276 1883 if (oldPosition == lineRange.GetEnd())
5d7836c4
JS
1884 {
1885 if (m_caretAtLineStart)
1886 {
1887 // We're already at the start of the line, so actually move on now.
1888 m_caretPosition = oldPosition + 1;
1889 m_caretAtLineStart = false;
1890 }
1891 else
1892 {
1893 // We're showing at the end of the line, so keep to
1894 // the same position but indicate that we're to show
1895 // at the start of the next line.
1896 m_caretPosition = oldPosition;
7fe8059f 1897 m_caretAtLineStart = true;
5d7836c4 1898 }
0a21bea0 1899 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1900 return;
1901 }
1902 }
1903 }
1904 m_caretPosition ++;
0a21bea0 1905 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1906}
1907
1908/// Move caret one visual step backward: this may mean setting a flag
1909/// and keeping the same position if we're going from the end of one line
1910/// to the start of the next, which may be the exact same caret position.
1911void wxRichTextCtrl::MoveCaretBack(long oldPosition)
1912{
603f702b 1913 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(oldPosition);
5d7836c4
JS
1914
1915 // Only do the check if we're not at the start of the paragraph (where things work OK
1916 // anyway)
1917 if (para && (oldPosition != para->GetRange().GetStart()))
1918 {
603f702b 1919 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(oldPosition);
5d7836c4
JS
1920
1921 if (line)
1922 {
1e967276
JS
1923 wxRichTextRange lineRange = line->GetAbsoluteRange();
1924
5d7836c4
JS
1925 // We're at the start of a line. See whether we need to
1926 // stay at the same actual caret position but change visual
1927 // position, or not.
1e967276 1928 if (oldPosition == lineRange.GetStart())
5d7836c4
JS
1929 {
1930 m_caretPosition = oldPosition-1;
1931 m_caretAtLineStart = true;
1932 return;
1933 }
1e967276 1934 else if (oldPosition == lineRange.GetEnd())
5d7836c4
JS
1935 {
1936 if (m_caretAtLineStart)
1937 {
1938 // We're at the start of the line, so keep the same caret position
1939 // but clear the start-of-line flag.
1940 m_caretPosition = oldPosition;
1941 m_caretAtLineStart = false;
1942 }
1943 else
1944 {
1945 // We're showing at the end of the line, so go back
1946 // to the previous character position.
1947 m_caretPosition = oldPosition - 1;
1948 }
0a21bea0 1949 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1950 return;
1951 }
1952 }
1953 }
1954 m_caretPosition --;
0a21bea0 1955 SetDefaultStyleToCursorStyle();
5d7836c4
JS
1956}
1957
1958/// Move right
1959bool wxRichTextCtrl::MoveRight(int noPositions, int flags)
1960{
603f702b 1961 long endPos = GetFocusObject()->GetOwnRange().GetEnd();
5d7836c4
JS
1962
1963 if (m_caretPosition + noPositions < endPos)
1964 {
1965 long oldPos = m_caretPosition;
1966 long newPos = m_caretPosition + noPositions;
1967
1968 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1969 if (!extendSel)
1970 SelectNone();
1971
1972 // Determine by looking at oldPos and m_caretPosition whether
1973 // we moved from the end of a line to the start of the next line, in which case
1974 // we want to adjust the caret position such that it is positioned at the
1975 // start of the next line, rather than jumping past the first character of the
1976 // line.
1977 if (noPositions == 1 && !extendSel)
1978 MoveCaretForward(oldPos);
1979 else
1980 SetCaretPosition(newPos);
1981
1982 PositionCaret();
0a21bea0 1983 SetDefaultStyleToCursorStyle();
5d7836c4 1984
5d7836c4
JS
1985 return true;
1986 }
1987 else
7fe8059f 1988 return false;
5d7836c4
JS
1989}
1990
1991/// Move left
1992bool wxRichTextCtrl::MoveLeft(int noPositions, int flags)
1993{
1994 long startPos = -1;
1995
1996 if (m_caretPosition > startPos - noPositions + 1)
1997 {
1998 long oldPos = m_caretPosition;
1999 long newPos = m_caretPosition - noPositions;
2000 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
2001 if (!extendSel)
2002 SelectNone();
2003
2004 if (noPositions == 1 && !extendSel)
2005 MoveCaretBack(oldPos);
2006 else
2007 SetCaretPosition(newPos);
2008
2009 PositionCaret();
0a21bea0 2010 SetDefaultStyleToCursorStyle();
5d7836c4 2011
5d7836c4
JS
2012 return true;
2013 }
2014 else
7fe8059f 2015 return false;
5d7836c4
JS
2016}
2017
603f702b
JS
2018// Find the caret position for the combination of hit-test flags and character position.
2019// Returns the caret position and also an indication of where to place the caret (caretLineStart)
2020// since this is ambiguous (same position used for end of line and start of next).
2021long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position, int hitTestFlags, wxRichTextParagraphLayoutBox* container,
2022 bool& caretLineStart)
2023{
2024 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
2025 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
2026 // so we view the caret at the start of the line.
2027 caretLineStart = false;
2028 long caretPosition = position;
2029
2030 if (hitTestFlags & wxRICHTEXT_HITTEST_BEFORE)
2031 {
2032 wxRichTextLine* thisLine = container->GetLineAtPosition(position-1);
2033 wxRichTextRange lineRange;
2034 if (thisLine)
2035 lineRange = thisLine->GetAbsoluteRange();
2036
2037 if (thisLine && (position-1) == lineRange.GetEnd())
2038 {
2039 caretPosition --;
2040 caretLineStart = true;
2041 }
2042 else
2043 {
2044 wxRichTextParagraph* para = container->GetParagraphAtPosition(position);
2045 if (para && para->GetRange().GetStart() == position)
2046 caretPosition --;
2047 }
2048 }
2049 return caretPosition;
2050}
2051
5d7836c4
JS
2052/// Move up
2053bool wxRichTextCtrl::MoveUp(int noLines, int flags)
2054{
2055 return MoveDown(- noLines, flags);
2056}
2057
2058/// Move up
2059bool wxRichTextCtrl::MoveDown(int noLines, int flags)
2060{
c59f6793
JS
2061 if (!GetCaret())
2062 return false;
2063
603f702b 2064 long lineNumber = GetFocusObject()->GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart);
5d7836c4 2065 wxPoint pt = GetCaret()->GetPosition();
4fe83b93 2066 long newLine = lineNumber + noLines;
603f702b 2067 bool notInThisObject = false;
5d7836c4
JS
2068
2069 if (lineNumber != -1)
2070 {
2071 if (noLines > 0)
2072 {
603f702b 2073 long lastLine = GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd());
5d7836c4 2074 if (newLine > lastLine)
603f702b 2075 notInThisObject = true;
5d7836c4
JS
2076 }
2077 else
2078 {
2079 if (newLine < 0)
603f702b 2080 notInThisObject = true;
5d7836c4
JS
2081 }
2082 }
4fe83b93 2083
603f702b 2084 wxRichTextParagraphLayoutBox* container = GetFocusObject();
343ef639 2085 int hitTestFlags = wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS;
5d7836c4 2086
603f702b 2087 if (notInThisObject)
7fe8059f 2088 {
603f702b
JS
2089 // If we know we're navigating out of the current object,
2090 // try to find an object anywhere in the buffer at the new position (up or down a bit)
2091 container = & GetBuffer();
343ef639 2092 hitTestFlags &= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS;
4fe83b93 2093
603f702b
JS
2094 if (noLines > 0) // going down
2095 {
2096 pt.y = GetFocusObject()->GetPosition().y + GetFocusObject()->GetCachedSize().y + 2;
2097 }
2098 else // going up
2099 {
2100 pt.y = GetFocusObject()->GetPosition().y - 2;
2101 }
5d7836c4
JS
2102 }
2103 else
603f702b
JS
2104 {
2105 wxRichTextLine* lineObj = GetFocusObject()->GetLineForVisibleLineNumber(newLine);
2106 if (lineObj)
2107 pt.y = lineObj->GetAbsolutePosition().y + 2;
2108 else
2109 return false;
2110 }
5d7836c4
JS
2111
2112 long newPos = 0;
2113 wxClientDC dc(this);
2114 PrepareDC(dc);
2115 dc.SetFont(GetFont());
7fe8059f 2116
603f702b
JS
2117 wxRichTextObject* hitObj = NULL;
2118 wxRichTextObject* contextObj = NULL;
8db2e3ef
JS
2119 wxRichTextDrawingContext context(& GetBuffer());
2120 int hitTest = container->HitTest(dc, context, pt, newPos, & hitObj, & contextObj, hitTestFlags);
7fe8059f 2121
343ef639
JS
2122 if (hitObj &&
2123 ((hitTest & wxRICHTEXT_HITTEST_NONE) == 0) &&
2124 (! (hitObj == (& m_buffer) && ((hitTest & wxRICHTEXT_HITTEST_OUTSIDE) != 0))) // outside the buffer counts as 'do nothing'
2125 )
5d7836c4 2126 {
603f702b 2127 if (notInThisObject)
5d7836c4 2128 {
603f702b
JS
2129 wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
2130 if (actualContainer && actualContainer != GetFocusObject() && actualContainer->AcceptsFocus())
5d7836c4 2131 {
603f702b 2132 SetFocusObject(actualContainer, false /* don't set caret position yet */);
4fe83b93 2133
603f702b 2134 container = actualContainer;
5d7836c4
JS
2135 }
2136 }
4fe83b93 2137
603f702b
JS
2138 bool caretLineStart = true;
2139 long caretPosition = FindCaretPositionForCharacterPosition(newPos, hitTest, container, caretLineStart);
2140 long newSelEnd = caretPosition;
2141 bool extendSel;
2142
2143 if (notInThisObject)
2144 extendSel = false;
2145 else
2146 extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags);
5d7836c4 2147
c59f6793 2148 if (!extendSel)
5d7836c4
JS
2149 SelectNone();
2150
603f702b 2151 SetCaretPosition(caretPosition, caretLineStart);
5d7836c4 2152 PositionCaret();
0a21bea0 2153 SetDefaultStyleToCursorStyle();
5d7836c4 2154
5d7836c4
JS
2155 return true;
2156 }
41e155b4
WS
2157
2158 return false;
5d7836c4
JS
2159}
2160
2161/// Move to the end of the paragraph
2162bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
2163{
603f702b 2164 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(m_caretPosition, true);
5d7836c4
JS
2165 if (para)
2166 {
2167 long newPos = para->GetRange().GetEnd() - 1;
c59f6793
JS
2168 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
2169 if (!extendSel)
5d7836c4
JS
2170 SelectNone();
2171
2172 SetCaretPosition(newPos);
2173 PositionCaret();
0a21bea0 2174 SetDefaultStyleToCursorStyle();
5d7836c4 2175
5d7836c4
JS
2176 return true;
2177 }
2178
2179 return false;
2180}
2181
2182/// Move to the start of the paragraph
2183bool wxRichTextCtrl::MoveToParagraphStart(int flags)
2184{
603f702b 2185 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(m_caretPosition, true);
5d7836c4
JS
2186 if (para)
2187 {
2188 long newPos = para->GetRange().GetStart() - 1;
c59f6793
JS
2189 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
2190 if (!extendSel)
5d7836c4
JS
2191 SelectNone();
2192
2193 SetCaretPosition(newPos);
2194 PositionCaret();
0a21bea0 2195 SetDefaultStyleToCursorStyle();
5d7836c4 2196
5d7836c4
JS
2197 return true;
2198 }
2199
2200 return false;
2201}
2202
2203/// Move to the end of the line
2204bool wxRichTextCtrl::MoveToLineEnd(int flags)
2205{
2206 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
2207
2208 if (line)
2209 {
1e967276
JS
2210 wxRichTextRange lineRange = line->GetAbsoluteRange();
2211 long newPos = lineRange.GetEnd();
c59f6793
JS
2212 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
2213 if (!extendSel)
5d7836c4
JS
2214 SelectNone();
2215
2216 SetCaretPosition(newPos);
2217 PositionCaret();
0a21bea0 2218 SetDefaultStyleToCursorStyle();
5d7836c4 2219
5d7836c4
JS
2220 return true;
2221 }
2222
2223 return false;
2224}
2225
2226/// Move to the start of the line
2227bool wxRichTextCtrl::MoveToLineStart(int flags)
2228{
2229 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
2230 if (line)
2231 {
1e967276
JS
2232 wxRichTextRange lineRange = line->GetAbsoluteRange();
2233 long newPos = lineRange.GetStart()-1;
5d7836c4 2234
c59f6793
JS
2235 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
2236 if (!extendSel)
5d7836c4
JS
2237 SelectNone();
2238
603f702b 2239 wxRichTextParagraph* para = GetFocusObject()->GetParagraphForLine(line);
5d7836c4 2240
1e967276 2241 SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart());
5d7836c4 2242 PositionCaret();
0a21bea0 2243 SetDefaultStyleToCursorStyle();
5d7836c4 2244
5d7836c4
JS
2245 return true;
2246 }
2247
2248 return false;
2249}
2250
2251/// Move to the start of the buffer
2252bool wxRichTextCtrl::MoveHome(int flags)
2253{
2254 if (m_caretPosition != -1)
2255 {
c59f6793
JS
2256 bool extendSel = ExtendSelection(m_caretPosition, -1, flags);
2257 if (!extendSel)
5d7836c4
JS
2258 SelectNone();
2259
2260 SetCaretPosition(-1);
2261 PositionCaret();
0a21bea0 2262 SetDefaultStyleToCursorStyle();
5d7836c4 2263
5d7836c4
JS
2264 return true;
2265 }
2266 else
7fe8059f 2267 return false;
5d7836c4
JS
2268}
2269
2270/// Move to the end of the buffer
2271bool wxRichTextCtrl::MoveEnd(int flags)
2272{
603f702b 2273 long endPos = GetFocusObject()->GetOwnRange().GetEnd()-1;
5d7836c4
JS
2274
2275 if (m_caretPosition != endPos)
2276 {
c59f6793
JS
2277 bool extendSel = ExtendSelection(m_caretPosition, endPos, flags);
2278 if (!extendSel)
5d7836c4
JS
2279 SelectNone();
2280
2281 SetCaretPosition(endPos);
2282 PositionCaret();
0a21bea0 2283 SetDefaultStyleToCursorStyle();
5d7836c4 2284
5d7836c4
JS
2285 return true;
2286 }
2287 else
7fe8059f 2288 return false;
5d7836c4
JS
2289}
2290
2291/// Move noPages pages up
2292bool wxRichTextCtrl::PageUp(int noPages, int flags)
2293{
2294 return PageDown(- noPages, flags);
2295}
2296
2297/// Move noPages pages down
2298bool wxRichTextCtrl::PageDown(int noPages, int flags)
2299{
2300 // Calculate which line occurs noPages * screen height further down.
2301 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
2302 if (line)
2303 {
2304 wxSize clientSize = GetClientSize();
2305 int newY = line->GetAbsolutePosition().y + noPages*clientSize.y;
2306
603f702b 2307 wxRichTextLine* newLine = GetFocusObject()->GetLineAtYPosition(newY);
5d7836c4
JS
2308 if (newLine)
2309 {
1e967276
JS
2310 wxRichTextRange lineRange = newLine->GetAbsoluteRange();
2311 long pos = lineRange.GetStart()-1;
5d7836c4
JS
2312 if (pos != m_caretPosition)
2313 {
603f702b 2314 wxRichTextParagraph* para = GetFocusObject()->GetParagraphForLine(newLine);
5d7836c4 2315
c59f6793
JS
2316 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
2317 if (!extendSel)
5d7836c4
JS
2318 SelectNone();
2319
1e967276 2320 SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart());
5d7836c4 2321 PositionCaret();
0a21bea0 2322 SetDefaultStyleToCursorStyle();
5d7836c4 2323
5d7836c4
JS
2324 return true;
2325 }
2326 }
2327 }
2328
2329 return false;
2330}
2331
0871b343
JS
2332static bool wxRichTextCtrlIsWhitespace(const wxString& str)
2333{
2334 return str == wxT(" ") || str == wxT("\t");
2335}
2336
5d7836c4
JS
2337// Finds the caret position for the next word
2338long wxRichTextCtrl::FindNextWordPosition(int direction) const
7fe8059f 2339{
603f702b 2340 long endPos = GetFocusObject()->GetOwnRange().GetEnd();
5d7836c4
JS
2341
2342 if (direction > 0)
2343 {
2344 long i = m_caretPosition+1+direction; // +1 for conversion to character pos
7fe8059f 2345
5d7836c4
JS
2346 // First skip current text to space
2347 while (i < endPos && i > -1)
2348 {
2349 // i is in character, not caret positions
603f702b
JS
2350 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i));
2351 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false);
0871b343
JS
2352 if (line && (i == line->GetAbsoluteRange().GetEnd()))
2353 {
2354 break;
2355 }
2356 else if (!wxRichTextCtrlIsWhitespace(text) && !text.empty())
5d7836c4
JS
2357 i += direction;
2358 else
2359 {
2360 break;
2361 }
2362 }
2363 while (i < endPos && i > -1)
2364 {
2365 // i is in character, not caret positions
603f702b
JS
2366 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i));
2367 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false);
0871b343
JS
2368 if (line && (i == line->GetAbsoluteRange().GetEnd()))
2369 return wxMax(-1, i);
2370
7fe8059f 2371 if (text.empty()) // End of paragraph, or maybe an image
5d7836c4 2372 return wxMax(-1, i - 1);
0871b343 2373 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
5d7836c4
JS
2374 i += direction;
2375 else
2376 {
2377 // Convert to caret position
2378 return wxMax(-1, i - 1);
2379 }
2380 }
2381 if (i >= endPos)
2382 return endPos-1;
2383 return i-1;
2384 }
2385 else
2386 {
2387 long i = m_caretPosition;
2388
2389 // First skip white space
2390 while (i < endPos && i > -1)
2391 {
2392 // i is in character, not caret positions
603f702b
JS
2393 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i));
2394 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false);
0871b343
JS
2395
2396 if (text.empty() || (line && (i == line->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
5d7836c4 2397 break;
0871b343 2398 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
5d7836c4
JS
2399 i += direction;
2400 else
2401 break;
2402 }
2403 // Next skip current text to space
2404 while (i < endPos && i > -1)
2405 {
2406 // i is in character, not caret positions
603f702b
JS
2407 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i));
2408 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false);
0871b343
JS
2409 if (line && line->GetAbsoluteRange().GetStart() == i)
2410 return i-1;
2411
2412 if (!wxRichTextCtrlIsWhitespace(text) /* && !text.empty() */)
5d7836c4
JS
2413 i += direction;
2414 else
2415 {
2416 return i;
5d7836c4
JS
2417 }
2418 }
2419 if (i < -1)
2420 return -1;
2421 return i;
2422 }
2423}
2424
2425/// Move n words left
2426bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags)
2427{
2428 long pos = FindNextWordPosition(-1);
2429 if (pos != m_caretPosition)
2430 {
603f702b 2431 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos, true);
7fe8059f 2432
c59f6793
JS
2433 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
2434 if (!extendSel)
5d7836c4 2435 SelectNone();
7fe8059f 2436
5d7836c4
JS
2437 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
2438 PositionCaret();
0a21bea0 2439 SetDefaultStyleToCursorStyle();
5d7836c4 2440
5d7836c4
JS
2441 return true;
2442 }
7fe8059f 2443
5d7836c4
JS
2444 return false;
2445}
2446
2447/// Move n words right
2448bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags)
2449{
2450 long pos = FindNextWordPosition(1);
2451 if (pos != m_caretPosition)
2452 {
603f702b 2453 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos, true);
7fe8059f 2454
c59f6793
JS
2455 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
2456 if (!extendSel)
5d7836c4 2457 SelectNone();
7fe8059f 2458
5d7836c4
JS
2459 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
2460 PositionCaret();
0a21bea0 2461 SetDefaultStyleToCursorStyle();
5d7836c4 2462
5d7836c4
JS
2463 return true;
2464 }
7fe8059f 2465
5d7836c4
JS
2466 return false;
2467}
2468
2469/// Sizing
2470void wxRichTextCtrl::OnSize(wxSizeEvent& event)
2471{
4d551ad5 2472 // Only do sizing optimization for large buffers
603f702b 2473 if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold)
4d551ad5
JS
2474 {
2475 m_fullLayoutRequired = true;
2476 m_fullLayoutTime = wxGetLocalTimeMillis();
2477 m_fullLayoutSavedPosition = GetFirstVisiblePosition();
2f36e8dc 2478 LayoutContent(true /* onlyVisibleRect */);
4d551ad5
JS
2479 }
2480 else
2481 GetBuffer().Invalidate(wxRICHTEXT_ALL);
7fe8059f 2482
3e3a754f 2483#if wxRICHTEXT_BUFFERED_PAINTING
5d7836c4 2484 RecreateBuffer();
3e3a754f 2485#endif
5d7836c4
JS
2486
2487 event.Skip();
2488}
2489
98769552
JS
2490// Force any pending layout due to large buffer
2491void wxRichTextCtrl::ForceDelayedLayout()
2492{
2493 if (m_fullLayoutRequired)
2494 {
2495 m_fullLayoutRequired = false;
2496 m_fullLayoutTime = 0;
2497 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2498 ShowPosition(m_fullLayoutSavedPosition);
2499 Refresh(false);
2500 Update();
2501 }
2502}
4d551ad5
JS
2503
2504/// Idle-time processing
2505void wxRichTextCtrl::OnIdle(wxIdleEvent& event)
2506{
1c13f06e
JS
2507#if wxRICHTEXT_USE_OWN_CARET
2508 if (((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
2509 {
2510 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate(false);
2511 PositionCaret();
2512 GetCaret()->Show();
2513 }
2514#endif
2515
4d551ad5
JS
2516 const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL;
2517
2518 if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval)))
2519 {
2520 m_fullLayoutRequired = false;
2521 m_fullLayoutTime = 0;
2522 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2523 ShowPosition(m_fullLayoutSavedPosition);
76bcd815 2524 Refresh(false);
4d551ad5 2525 }
ab14c7aa
JS
2526
2527 if (m_caretPositionForDefaultStyle != -2)
2528 {
2529 // If the caret position has changed, no longer reflect the default style
2530 // in the UI.
2531 if (GetCaretPosition() != m_caretPositionForDefaultStyle)
2532 m_caretPositionForDefaultStyle = -2;
2533 }
2534
4d551ad5
JS
2535 event.Skip();
2536}
2537
c59f6793
JS
2538/// Scrolling
2539void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
2540{
1c13f06e
JS
2541#if wxRICHTEXT_USE_OWN_CARET
2542 if (!((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
2543 {
2544 GetCaret()->Hide();
2545 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate();
2546 }
2547#endif
2548
c59f6793
JS
2549 event.Skip();
2550}
2551
5d7836c4 2552/// Set up scrollbars, e.g. after a resize
169adfa9 2553void wxRichTextCtrl::SetupScrollbars(bool atTop)
5d7836c4 2554{
17808a75 2555 if (IsFrozen())
5d7836c4
JS
2556 return;
2557
2558 if (GetBuffer().IsEmpty())
2559 {
2560 SetScrollbars(0, 0, 0, 0, 0, 0);
2561 return;
2562 }
2563
2564 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2565 // of pixels. See e.g. wxVScrolledWindow for ideas.
3956d02a 2566 int pixelsPerUnit = 5;
5d7836c4
JS
2567 wxSize clientSize = GetClientSize();
2568
e9f10004 2569 int maxHeight = GetBuffer().GetCachedSize().y + GetBuffer().GetTopMargin();
5d7836c4 2570
3956d02a
JS
2571 // Round up so we have at least maxHeight pixels
2572 int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5);
5d7836c4 2573
169adfa9
JS
2574 int startX = 0, startY = 0;
2575 if (!atTop)
2576 GetViewStart(& startX, & startY);
7fe8059f 2577
7051fa41 2578 int maxPositionX = 0;
3956d02a 2579 int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5);
7fe8059f 2580
7051fa41
JS
2581 int newStartX = wxMin(maxPositionX, startX);
2582 int newStartY = wxMin(maxPositionY, startY);
2583
2584 int oldPPUX, oldPPUY;
2585 int oldStartX, oldStartY;
2586 int oldVirtualSizeX = 0, oldVirtualSizeY = 0;
2587 GetScrollPixelsPerUnit(& oldPPUX, & oldPPUY);
2588 GetViewStart(& oldStartX, & oldStartY);
2589 GetVirtualSize(& oldVirtualSizeX, & oldVirtualSizeY);
2590 if (oldPPUY > 0)
2591 oldVirtualSizeY /= oldPPUY;
2592
2593 if (oldPPUX == 0 && oldPPUY == pixelsPerUnit && oldVirtualSizeY == unitsY && oldStartX == newStartX && oldStartY == newStartY)
2594 return;
2595
2596 // Don't set scrollbars if there were none before, and there will be none now.
205a1ad9 2597 if (oldPPUY != 0 && (oldVirtualSizeY*oldPPUY < clientSize.y) && (unitsY*pixelsPerUnit < clientSize.y))
7051fa41
JS
2598 return;
2599
5d7836c4
JS
2600 // Move to previous scroll position if
2601 // possible
7051fa41 2602 SetScrollbars(0, pixelsPerUnit, 0, unitsY, newStartX, newStartY);
5d7836c4
JS
2603}
2604
2605/// Paint the background
2606void wxRichTextCtrl::PaintBackground(wxDC& dc)
2607{
2608 wxColour backgroundColour = GetBackgroundColour();
a1b806b9 2609 if (!backgroundColour.IsOk())
5d7836c4
JS
2610 backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
2611
2612 // Clear the background
2613 dc.SetBrush(wxBrush(backgroundColour));
2614 dc.SetPen(*wxTRANSPARENT_PEN);
7fe8059f 2615 wxRect windowRect(GetClientSize());
5d7836c4
JS
2616 windowRect.x -= 2; windowRect.y -= 2;
2617 windowRect.width += 4; windowRect.height += 4;
7fe8059f 2618
5d7836c4
JS
2619 // We need to shift the rectangle to take into account
2620 // scrolling. Converting device to logical coordinates.
2621 CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y);
2622 dc.DrawRectangle(windowRect);
2623}
2624
3e3a754f 2625#if wxRICHTEXT_BUFFERED_PAINTING
5d7836c4
JS
2626/// Recreate buffer bitmap if necessary
2627bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
2628{
2629 wxSize sz = size;
2630 if (sz == wxDefaultSize)
2631 sz = GetClientSize();
7fe8059f 2632
5d7836c4
JS
2633 if (sz.x < 1 || sz.y < 1)
2634 return false;
7fe8059f 2635
a1b806b9 2636 if (!m_bufferBitmap.IsOk() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y)
5d7836c4 2637 m_bufferBitmap = wxBitmap(sz.x, sz.y);
a1b806b9 2638 return m_bufferBitmap.IsOk();
5d7836c4 2639}
3e3a754f 2640#endif
5d7836c4
JS
2641
2642// ----------------------------------------------------------------------------
2643// file IO functions
2644// ----------------------------------------------------------------------------
2645
3306aec1
JS
2646bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
2647{
d75a69e8 2648 bool success = GetBuffer().LoadFile(filename, (wxRichTextFileType)fileType);
5d7836c4
JS
2649 if (success)
2650 m_filename = filename;
2651
2652 DiscardEdits();
2653 SetInsertionPoint(0);
2f36e8dc 2654 LayoutContent();
5d7836c4 2655 PositionCaret();
169adfa9 2656 SetupScrollbars(true);
76bcd815 2657 Refresh(false);
0ec1179b 2658 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4
JS
2659
2660 if (success)
2661 return true;
2662 else
2663 {
2664 wxLogError(_("File couldn't be loaded."));
7fe8059f 2665
5d7836c4
JS
2666 return false;
2667 }
2668}
2669
3306aec1 2670bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
5d7836c4 2671{
d75a69e8 2672 if (GetBuffer().SaveFile(filename, (wxRichTextFileType)fileType))
5d7836c4 2673 {
3306aec1 2674 m_filename = filename;
5d7836c4
JS
2675
2676 DiscardEdits();
2677
2678 return true;
5d7836c4
JS
2679 }
2680
2681 wxLogError(_("The text couldn't be saved."));
2682
2683 return false;
2684}
2685
2686// ----------------------------------------------------------------------------
2687// wxRichTextCtrl specific functionality
2688// ----------------------------------------------------------------------------
2689
2690/// Add a new paragraph of text to the end of the buffer
2691wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
2692{
603f702b 2693 wxRichTextRange range = GetFocusObject()->AddParagraph(text);
4fe83b93 2694 GetBuffer().Invalidate();
62381daa
JS
2695 LayoutContent();
2696 return range;
5d7836c4
JS
2697}
2698
2699/// Add an image
2700wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
2701{
603f702b 2702 wxRichTextRange range = GetFocusObject()->AddImage(image);
4fe83b93 2703 GetBuffer().Invalidate();
62381daa
JS
2704 LayoutContent();
2705 return range;
5d7836c4
JS
2706}
2707
2708// ----------------------------------------------------------------------------
2709// selection and ranges
2710// ----------------------------------------------------------------------------
2711
2712void wxRichTextCtrl::SelectAll()
2713{
76d7fc29 2714 SetSelection(-1, -1);
5d7836c4
JS
2715}
2716
2717/// Select none
2718void wxRichTextCtrl::SelectNone()
2719{
603f702b 2720 if (m_selection.IsValid())
c1aec196 2721 {
603f702b 2722 wxRichTextSelection oldSelection = m_selection;
7051fa41 2723
603f702b 2724 m_selection.Reset();
7051fa41 2725
603f702b 2726 RefreshForSelectionChange(oldSelection, m_selection);
c1aec196 2727 }
5d7836c4 2728 m_selectionAnchor = -2;
603f702b
JS
2729 m_selectionAnchorObject = NULL;
2730 m_selectionState = wxRichTextCtrlSelectionState_Normal;
5d7836c4
JS
2731}
2732
0ca07313
JS
2733static bool wxIsWordDelimiter(const wxString& text)
2734{
1c07e924 2735 return !text.IsEmpty() && !wxIsalnum(text[0]);
0ca07313
JS
2736}
2737
2738/// Select the word at the given character position
2739bool wxRichTextCtrl::SelectWord(long position)
2740{
603f702b 2741 if (position < 0 || position > GetFocusObject()->GetOwnRange().GetEnd())
0ca07313 2742 return false;
1807a1f3 2743
603f702b 2744 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(position);
0ca07313
JS
2745 if (!para)
2746 return false;
2747
62381daa
JS
2748 if (position == para->GetRange().GetEnd())
2749 position --;
2750
0ca07313
JS
2751 long positionStart = position;
2752 long positionEnd = position;
1807a1f3 2753
0ca07313
JS
2754 for (positionStart = position; positionStart >= para->GetRange().GetStart(); positionStart --)
2755 {
603f702b 2756 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart, positionStart));
0ca07313
JS
2757 if (wxIsWordDelimiter(text))
2758 {
2759 positionStart ++;
2760 break;
2761 }
2762 }
2763 if (positionStart < para->GetRange().GetStart())
2764 positionStart = para->GetRange().GetStart();
1807a1f3 2765
0ca07313
JS
2766 for (positionEnd = position; positionEnd < para->GetRange().GetEnd(); positionEnd ++)
2767 {
603f702b 2768 wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd, positionEnd));
0ca07313
JS
2769 if (wxIsWordDelimiter(text))
2770 {
2771 positionEnd --;
2772 break;
2773 }
2774 }
2775 if (positionEnd >= para->GetRange().GetEnd())
2776 positionEnd = para->GetRange().GetEnd();
1807a1f3 2777
62381daa
JS
2778 if (positionEnd < positionStart)
2779 return false;
2780
0ca07313
JS
2781 SetSelection(positionStart, positionEnd+1);
2782
2783 if (positionStart >= 0)
2784 {
2785 MoveCaret(positionStart-1, true);
0a21bea0 2786 SetDefaultStyleToCursorStyle();
0ca07313
JS
2787 }
2788
2789 return true;
2790}
2791
5d7836c4
JS
2792wxString wxRichTextCtrl::GetStringSelection() const
2793{
2794 long from, to;
2795 GetSelection(&from, &to);
2796
2797 return GetRange(from, to);
2798}
2799
5d7836c4
JS
2800// ----------------------------------------------------------------------------
2801// hit testing
2802// ----------------------------------------------------------------------------
2803
2804wxTextCtrlHitTestResult
2805wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
2806{
2807 // implement in terms of the other overload as the native ports typically
2808 // can get the position and not (x, y) pair directly (although wxUniv
2809 // directly gets x and y -- and so overrides this method as well)
2810 long pos;
2811 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
2812
2813 if ( rc != wxTE_HT_UNKNOWN )
2814 {
2815 PositionToXY(pos, x, y);
2816 }
2817
2818 return rc;
2819}
2820
2821wxTextCtrlHitTestResult
2822wxRichTextCtrl::HitTest(const wxPoint& pt,
2823 long * pos) const
2824{
2825 wxClientDC dc((wxRichTextCtrl*) this);
2826 ((wxRichTextCtrl*)this)->PrepareDC(dc);
61399247 2827
563d25ba
JS
2828 // Buffer uses logical position (relative to start of buffer)
2829 // so convert
2830 wxPoint pt2 = GetLogicalPoint(pt);
5d7836c4 2831
603f702b
JS
2832 wxRichTextObject* hitObj = NULL;
2833 wxRichTextObject* contextObj = NULL;
8db2e3ef
JS
2834 wxRichTextDrawingContext context((wxRichTextBuffer*) & GetBuffer());
2835 int hit = ((wxRichTextCtrl*)this)->GetFocusObject()->HitTest(dc, context, pt2, *pos, & hitObj, & contextObj, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS);
41e155b4 2836
f262b25c
JS
2837 if ((hit & wxRICHTEXT_HITTEST_BEFORE) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2838 return wxTE_HT_BEFORE;
2839 else if ((hit & wxRICHTEXT_HITTEST_AFTER) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2840 return wxTE_HT_BEYOND;
25d6eb3a 2841 else if (hit & (wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER))
f262b25c 2842 return wxTE_HT_ON_TEXT;
41e155b4
WS
2843
2844 return wxTE_HT_UNKNOWN;
5d7836c4
JS
2845}
2846
0c0e063e
JS
2847wxRichTextParagraphLayoutBox*
2848wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt, long& position, int& hit, wxRichTextObject* hitObj, int flags/* = 0*/)
2849{
2850 wxClientDC dc(this);
2851 PrepareDC(dc);
2852 dc.SetFont(GetFont());
2853
2854 wxPoint logicalPt = GetLogicalPoint(pt);
2855
2856 wxRichTextObject* contextObj = NULL;
8db2e3ef
JS
2857 wxRichTextDrawingContext context(& GetBuffer());
2858 hit = GetBuffer().HitTest(dc, context, logicalPt, position, &hitObj, &contextObj, flags);
0c0e063e
JS
2859 wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
2860
2861 return container;
2862}
2863
2864
5d7836c4
JS
2865// ----------------------------------------------------------------------------
2866// set/get the controls text
2867// ----------------------------------------------------------------------------
2868
805c8d92 2869wxString wxRichTextCtrl::DoGetValue() const
5d7836c4
JS
2870{
2871 return GetBuffer().GetText();
2872}
2873
2874wxString wxRichTextCtrl::GetRange(long from, long to) const
2875{
96c9f0f6 2876 // Public API for range is different from internals
603f702b 2877 return GetFocusObject()->GetTextForRange(wxRichTextRange(from, to-1));
5d7836c4
JS
2878}
2879
343ae70d 2880void wxRichTextCtrl::DoSetValue(const wxString& value, int flags)
5d7836c4 2881{
d081739d
JS
2882 // Don't call Clear here, since it always sends a text updated event
2883 m_buffer.ResetAndClearCommands();
603f702b 2884 m_buffer.Invalidate(wxRICHTEXT_ALL);
d081739d
JS
2885 m_caretPosition = -1;
2886 m_caretPositionForDefaultStyle = -2;
2887 m_caretAtLineStart = false;
603f702b
JS
2888 m_selection.Reset();
2889 m_selectionState = wxRichTextCtrlSelectionState_Normal;
d081739d
JS
2890
2891 Scroll(0,0);
2892
2893 if (!IsFrozen())
2894 {
2895 LayoutContent();
2896 Refresh(false);
2897 }
5d7836c4 2898
1a75935d 2899 if (!value.IsEmpty())
5d7836c4 2900 {
1a75935d
JS
2901 // Remove empty paragraph
2902 GetBuffer().Clear();
d081739d 2903 DoWriteText(value, flags);
5d7836c4
JS
2904
2905 // for compatibility, don't move the cursor when doing SetValue()
2906 SetInsertionPoint(0);
2907 }
1a75935d 2908 else
5d7836c4 2909 {
1a75935d
JS
2910 // still send an event for consistency
2911 if (flags & SetValue_SendEvent)
0ec1179b 2912 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4 2913 }
5d7836c4
JS
2914 DiscardEdits();
2915}
2916
2917void wxRichTextCtrl::WriteText(const wxString& value)
2918{
2919 DoWriteText(value);
2920}
2921
343ae70d 2922void wxRichTextCtrl::DoWriteText(const wxString& value, int flags)
5d7836c4 2923{
0ca07313 2924 wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix);
fe5aa22c 2925
4e63bfb9 2926 GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition+1, valueUnix, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
343ae70d
VZ
2927
2928 if ( flags & SetValue_SendEvent )
0ec1179b 2929 wxTextCtrl::SendTextUpdatedEvent(this);
5d7836c4
JS
2930}
2931
2932void wxRichTextCtrl::AppendText(const wxString& text)
2933{
2934 SetInsertionPointEnd();
2935
2936 WriteText(text);
2937}
2938
2939/// Write an image at the current insertion point
24777478 2940bool wxRichTextCtrl::WriteImage(const wxImage& image, wxBitmapType bitmapType, const wxRichTextAttr& textAttr)
5d7836c4
JS
2941{
2942 wxRichTextImageBlock imageBlock;
2943
2944 wxImage image2 = image;
2945 if (imageBlock.MakeImageBlock(image2, bitmapType))
24777478 2946 return WriteImage(imageBlock, textAttr);
41e155b4
WS
2947
2948 return false;
5d7836c4
JS
2949}
2950
24777478 2951bool wxRichTextCtrl::WriteImage(const wxString& filename, wxBitmapType bitmapType, const wxRichTextAttr& textAttr)
5d7836c4
JS
2952{
2953 wxRichTextImageBlock imageBlock;
2954
2955 wxImage image;
2956 if (imageBlock.MakeImageBlock(filename, bitmapType, image, false))
24777478 2957 return WriteImage(imageBlock, textAttr);
41e155b4
WS
2958
2959 return false;
5d7836c4
JS
2960}
2961
24777478 2962bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock, const wxRichTextAttr& textAttr)
5d7836c4 2963{
4e63bfb9 2964 return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition+1, imageBlock, this, 0, textAttr);
5d7836c4
JS
2965}
2966
24777478 2967bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, wxBitmapType bitmapType, const wxRichTextAttr& textAttr)
5d7836c4 2968{
a1b806b9 2969 if (bitmap.IsOk())
5d7836c4
JS
2970 {
2971 wxRichTextImageBlock imageBlock;
7fe8059f 2972
5d7836c4 2973 wxImage image = bitmap.ConvertToImage();
a1b806b9 2974 if (image.IsOk() && imageBlock.MakeImageBlock(image, bitmapType))
24777478 2975 return WriteImage(imageBlock, textAttr);
5d7836c4 2976 }
41e155b4 2977
5d7836c4
JS
2978 return false;
2979}
2980
603f702b
JS
2981// Write a text box at the current insertion point.
2982wxRichTextBox* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr& textAttr)
2983{
2984 wxRichTextBox* textBox = new wxRichTextBox;
2985 textBox->SetAttributes(textAttr);
2986 textBox->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
2987 textBox->AddParagraph(wxEmptyString);
2988 textBox->SetParent(NULL);
2989
2990 // The object returned is the one actually inserted into the buffer,
2991 // while the original one is deleted.
4e63bfb9 2992 wxRichTextObject* obj = GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition+1, textBox, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
603f702b
JS
2993 wxRichTextBox* box = wxDynamicCast(obj, wxRichTextBox);
2994 return box;
2995}
2996
2997// Write a table at the current insertion point, returning the table.
2998wxRichTextTable* wxRichTextCtrl::WriteTable(int rows, int cols, const wxRichTextAttr& tableAttr, const wxRichTextAttr& cellAttr)
2999{
3000 wxASSERT(rows > 0 && cols > 0);
3001
3002 if (rows <= 0 || cols <= 0)
3003 return NULL;
3004
3005 wxRichTextTable* table = new wxRichTextTable;
3006 table->SetAttributes(tableAttr);
3007 table->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style
4fe83b93 3008
603f702b
JS
3009 table->CreateTable(rows, cols);
3010
3011 table->SetParent(NULL);
3012
3013 int i, j;
3014 for (j = 0; j < rows; j++)
3015 {
3016 for (i = 0; i < cols; i++)
3017 {
3018 table->GetCell(j, i)->GetAttributes() = cellAttr;
3019 }
3020 }
3021
3022 // The object returned is the one actually inserted into the buffer,
3023 // while the original one is deleted.
4e63bfb9 3024 wxRichTextObject* obj = GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition+1, table, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
603f702b
JS
3025 wxRichTextTable* tableResult = wxDynamicCast(obj, wxRichTextTable);
3026 return tableResult;
3027}
3028
3029
5d7836c4
JS
3030/// Insert a newline (actually paragraph) at the current insertion point.
3031bool wxRichTextCtrl::Newline()
3032{
4e63bfb9 3033 return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
5d7836c4
JS
3034}
3035
ff76711f
JS
3036/// Insert a line break at the current insertion point.
3037bool wxRichTextCtrl::LineBreak()
3038{
3039 wxString text;
3040 text = wxRichTextLineBreakChar;
4e63bfb9 3041 return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition+1, text, this);
ff76711f 3042}
5d7836c4
JS
3043
3044// ----------------------------------------------------------------------------
3045// Clipboard operations
3046// ----------------------------------------------------------------------------
3047
3048void wxRichTextCtrl::Copy()
3049{
3050 if (CanCopy())
3051 {
fe5aa22c 3052 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
3053 GetBuffer().CopyToClipboard(range);
3054 }
3055}
3056
3057void wxRichTextCtrl::Cut()
3058{
3059 if (CanCut())
3060 {
fe5aa22c 3061 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
3062 GetBuffer().CopyToClipboard(range);
3063
3064 DeleteSelectedContent();
2f36e8dc 3065 LayoutContent();
76bcd815 3066 Refresh(false);
5d7836c4
JS
3067 }
3068}
3069
3070void wxRichTextCtrl::Paste()
3071{
3072 if (CanPaste())
3073 {
3074 BeginBatchUndo(_("Paste"));
7fe8059f 3075
5d7836c4
JS
3076 long newPos = m_caretPosition;
3077 DeleteSelectedContent(& newPos);
7fe8059f 3078
5d7836c4
JS
3079 GetBuffer().PasteFromClipboard(newPos);
3080
3081 EndBatchUndo();
3082 }
3083}
3084
3085void wxRichTextCtrl::DeleteSelection()
3086{
3087 if (CanDeleteSelection())
3088 {
3089 DeleteSelectedContent();
3090 }
3091}
3092
3093bool wxRichTextCtrl::HasSelection() const
3094{
603f702b
JS
3095 return (m_selection.IsValid() && m_selection.GetContainer() == GetFocusObject());
3096}
3097
3098bool wxRichTextCtrl::HasUnfocusedSelection() const
3099{
3100 return m_selection.IsValid();
5d7836c4
JS
3101}
3102
3103bool wxRichTextCtrl::CanCopy() const
3104{
3105 // Can copy if there's a selection
3106 return HasSelection();
3107}
3108
3109bool wxRichTextCtrl::CanCut() const
3110{
cc2aecde 3111 return CanDeleteSelection();
5d7836c4
JS
3112}
3113
3114bool wxRichTextCtrl::CanPaste() const
3115{
cc2aecde 3116 if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition+1))
5d7836c4
JS
3117 return false;
3118
3119 return GetBuffer().CanPasteFromClipboard();
3120}
3121
3122bool wxRichTextCtrl::CanDeleteSelection() const
3123{
cc2aecde 3124 return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange());
5d7836c4
JS
3125}
3126
3127
3128// ----------------------------------------------------------------------------
3129// Accessors
3130// ----------------------------------------------------------------------------
3131
8e43fc77
JS
3132void wxRichTextCtrl::SetContextMenu(wxMenu* menu)
3133{
3134 if (m_contextMenu && m_contextMenu != menu)
3135 delete m_contextMenu;
3136 m_contextMenu = menu;
3137}
3138
5d7836c4
JS
3139void wxRichTextCtrl::SetEditable(bool editable)
3140{
3141 m_editable = editable;
3142}
3143
3144void wxRichTextCtrl::SetInsertionPoint(long pos)
3145{
3146 SelectNone();
3147
3148 m_caretPosition = pos - 1;
6f5d63bc
JS
3149
3150 PositionCaret();
4427f297
JS
3151
3152 SetDefaultStyleToCursorStyle();
5d7836c4
JS
3153}
3154
3155void wxRichTextCtrl::SetInsertionPointEnd()
3156{
3157 long pos = GetLastPosition();
3158 SetInsertionPoint(pos);
3159}
3160
3161long wxRichTextCtrl::GetInsertionPoint() const
3162{
3163 return m_caretPosition+1;
3164}
3165
3166wxTextPos wxRichTextCtrl::GetLastPosition() const
3167{
603f702b 3168 return GetFocusObject()->GetOwnRange().GetEnd();
5d7836c4
JS
3169}
3170
3171// If the return values from and to are the same, there is no
3172// selection.
3173void wxRichTextCtrl::GetSelection(long* from, long* to) const
3174{
603f702b
JS
3175 if (m_selection.IsValid())
3176 {
3177 *from = m_selection.GetRange().GetStart();
3178 *to = m_selection.GetRange().GetEnd();
96c9f0f6 3179 (*to) ++;
603f702b
JS
3180 }
3181 else
3182 {
3183 *from = -2;
3184 *to = -2;
3185 }
5d7836c4
JS
3186}
3187
3188bool wxRichTextCtrl::IsEditable() const
3189{
3190 return m_editable;
3191}
3192
3193// ----------------------------------------------------------------------------
3194// selection
3195// ----------------------------------------------------------------------------
3196
3197void wxRichTextCtrl::SetSelection(long from, long to)
3198{
3199 // if from and to are both -1, it means (in wxWidgets) that all text should
3200 // be selected.
3201 if ( (from == -1) && (to == -1) )
3202 {
3203 from = 0;
96c9f0f6 3204 to = GetLastPosition()+1;
5d7836c4
JS
3205 }
3206
c1aec196
JS
3207 if (from == to)
3208 {
3209 SelectNone();
3210 }
3211 else
3212 {
603f702b
JS
3213 wxRichTextSelection oldSelection = m_selection;
3214
205a1ad9 3215 m_selectionAnchor = from-1;
603f702b
JS
3216 m_selectionAnchorObject = NULL;
3217 m_selection.Set(wxRichTextRange(from, to-1), GetFocusObject());
76d7fc29 3218
a216dfa2 3219 m_caretPosition = wxMax(-1, to-1);
0ca07313 3220
603f702b 3221 RefreshForSelectionChange(oldSelection, m_selection);
c1aec196
JS
3222 PositionCaret();
3223 }
5d7836c4
JS
3224}
3225
3226// ----------------------------------------------------------------------------
3227// Editing
3228// ----------------------------------------------------------------------------
3229
e3bb4c54 3230void wxRichTextCtrl::Replace(long from, long to,
343ae70d 3231 const wxString& value)
5d7836c4
JS
3232{
3233 BeginBatchUndo(_("Replace"));
3234
e3bb4c54
JS
3235 SetSelection(from, to);
3236
3237 wxRichTextAttr attr = GetDefaultStyle();
3238
5d7836c4
JS
3239 DeleteSelectedContent();
3240
e3bb4c54
JS
3241 SetDefaultStyle(attr);
3242
343ae70d 3243 DoWriteText(value, SetValue_SelectionOnly);
5d7836c4
JS
3244
3245 EndBatchUndo();
3246}
3247
3248void wxRichTextCtrl::Remove(long from, long to)
3249{
3250 SelectNone();
3251
603f702b 3252 GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from, to-1), this, & GetBuffer());
5d7836c4 3253
2f36e8dc 3254 LayoutContent();
5d7836c4 3255 if (!IsFrozen())
76bcd815 3256 Refresh(false);
5d7836c4
JS
3257}
3258
3259bool wxRichTextCtrl::IsModified() const
3260{
3261 return m_buffer.IsModified();
3262}
3263
3264void wxRichTextCtrl::MarkDirty()
3265{
3266 m_buffer.Modify(true);
3267}
3268
3269void wxRichTextCtrl::DiscardEdits()
3270{
ab14c7aa 3271 m_caretPositionForDefaultStyle = -2;
5d7836c4
JS
3272 m_buffer.Modify(false);
3273 m_buffer.GetCommandProcessor()->ClearCommands();
3274}
3275
3276int wxRichTextCtrl::GetNumberOfLines() const
3277{
603f702b 3278 return GetFocusObject()->GetParagraphCount();
5d7836c4
JS
3279}
3280
3281// ----------------------------------------------------------------------------
3282// Positions <-> coords
3283// ----------------------------------------------------------------------------
3284
3285long wxRichTextCtrl::XYToPosition(long x, long y) const
3286{
603f702b 3287 return GetFocusObject()->XYToPosition(x, y);
5d7836c4
JS
3288}
3289
3290bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
3291{
603f702b 3292 return GetFocusObject()->PositionToXY(pos, x, y);
5d7836c4
JS
3293}
3294
3295// ----------------------------------------------------------------------------
3296//
3297// ----------------------------------------------------------------------------
3298
3299void wxRichTextCtrl::ShowPosition(long pos)
3300{
3301 if (!IsPositionVisible(pos))
3302 ScrollIntoView(pos-1, WXK_DOWN);
3303}
3304
3305int wxRichTextCtrl::GetLineLength(long lineNo) const
3306{
603f702b 3307 return GetFocusObject()->GetParagraphLength(lineNo);
5d7836c4
JS
3308}
3309
3310wxString wxRichTextCtrl::GetLineText(long lineNo) const
3311{
603f702b 3312 return GetFocusObject()->GetParagraphText(lineNo);
5d7836c4
JS
3313}
3314
3315// ----------------------------------------------------------------------------
3316// Undo/redo
3317// ----------------------------------------------------------------------------
3318
3319void wxRichTextCtrl::Undo()
3320{
3321 if (CanUndo())
3322 {
3323 GetCommandProcessor()->Undo();
3324 }
3325}
3326
3327void wxRichTextCtrl::Redo()
3328{
3329 if (CanRedo())
3330 {
3331 GetCommandProcessor()->Redo();
3332 }
3333}
3334
3335bool wxRichTextCtrl::CanUndo() const
3336{
3a4a38c1 3337 return GetCommandProcessor()->CanUndo() && IsEditable();
5d7836c4
JS
3338}
3339
3340bool wxRichTextCtrl::CanRedo() const
3341{
3a4a38c1 3342 return GetCommandProcessor()->CanRedo() && IsEditable();
5d7836c4
JS
3343}
3344
3345// ----------------------------------------------------------------------------
563d25ba 3346// implementation details
5d7836c4
JS
3347// ----------------------------------------------------------------------------
3348
563d25ba 3349void wxRichTextCtrl::Command(wxCommandEvent& event)
5d7836c4
JS
3350{
3351 SetValue(event.GetString());
3352 GetEventHandler()->ProcessEvent(event);
3353}
3354
3355void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event)
3356{
3357 // By default, load the first file into the text window.
3358 if (event.GetNumberOfFiles() > 0)
3359 {
3360 LoadFile(event.GetFiles()[0]);
3361 }
3362}
3363
5d7836c4
JS
3364wxSize wxRichTextCtrl::DoGetBestSize() const
3365{
3366 return wxSize(10, 10);
3367}
3368
3369// ----------------------------------------------------------------------------
3370// standard handlers for standard edit menu events
3371// ----------------------------------------------------------------------------
3372
3373void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
3374{
3375 Cut();
3376}
3377
3378void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
3379{
3380 DeleteSelection();
3381}
3382
3383void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
3384{
3385 Copy();
3386}
3387
3388void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
3389{
3390 Paste();
3391}
3392
3393void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
3394{
3395 Undo();
3396}
3397
3398void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
3399{
3400 Redo();
3401}
3402
3403void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
3404{
3405 event.Enable( CanCut() );
3406}
3407
3408void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
3409{
3410 event.Enable( CanCopy() );
3411}
3412
3413void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
3414{
3415 event.Enable( CanDeleteSelection() );
3416}
3417
3418void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
3419{
3420 event.Enable( CanPaste() );
3421}
3422
3423void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
3424{
3425 event.Enable( CanUndo() );
3426 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
3427}
3428
3429void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
3430{
3431 event.Enable( CanRedo() );
3432 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
3433}
3434
3435void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
3436{
b30661fd
JS
3437 if (GetLastPosition() > 0)
3438 SelectAll();
5d7836c4
JS
3439}
3440
3441void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
3442{
b30661fd 3443 event.Enable(GetLastPosition() > 0);
5d7836c4
JS
3444}
3445
603f702b 3446void wxRichTextCtrl::OnProperties(wxCommandEvent& event)
cdaed652 3447{
603f702b
JS
3448 int idx = event.GetId() - wxID_RICHTEXT_PROPERTIES1;
3449 if (idx >= 0 && idx < m_contextMenuPropertiesInfo.GetCount())
3450 {
3451 wxRichTextObject* obj = m_contextMenuPropertiesInfo.GetObject(idx);
303f0be7
JS
3452 if (obj && CanEditProperties(obj))
3453 EditProperties(obj, this);
603f702b
JS
3454
3455 m_contextMenuPropertiesInfo.Clear();
3456 }
cdaed652
VZ
3457}
3458
603f702b 3459void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent& event)
cdaed652 3460{
603f702b
JS
3461 int idx = event.GetId() - wxID_RICHTEXT_PROPERTIES1;
3462 event.Enable(idx >= 0 && idx < m_contextMenuPropertiesInfo.GetCount());
cdaed652
VZ
3463}
3464
b4ae0def 3465void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event)
5d7836c4 3466{
b4ae0def
JS
3467 if (event.GetEventObject() != this)
3468 {
3469 event.Skip();
3470 return;
3471 }
3472
9b794421
JS
3473 ShowContextMenu(m_contextMenu, event.GetPosition());
3474}
3475
3476// Prepares the context menu, adding appropriate property-editing commands.
3477// Returns the number of property commands added.
3478int wxRichTextCtrl::PrepareContextMenu(wxMenu* menu, const wxPoint& pt, bool addPropertyCommands)
3479{
cdaed652
VZ
3480 wxClientDC dc(this);
3481 PrepareDC(dc);
3482 dc.SetFont(GetFont());
3483
9b794421
JS
3484 m_contextMenuPropertiesInfo.Clear();
3485
cdaed652 3486 long position = 0;
603f702b
JS
3487 wxRichTextObject* hitObj = NULL;
3488 wxRichTextObject* contextObj = NULL;
9b794421
JS
3489 if (pt != wxDefaultPosition)
3490 {
3491 wxPoint logicalPt = GetLogicalPoint(ScreenToClient(pt));
8db2e3ef
JS
3492 wxRichTextDrawingContext context(& GetBuffer());
3493 int hit = GetBuffer().HitTest(dc, context, logicalPt, position, & hitObj, & contextObj);
4fe83b93 3494
9b794421
JS
3495 if (hit == wxRICHTEXT_HITTEST_ON || hit == wxRICHTEXT_HITTEST_BEFORE || hit == wxRICHTEXT_HITTEST_AFTER)
3496 {
3497 wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
3498 if (hitObj && actualContainer)
3499 {
3500 if (actualContainer->AcceptsFocus())
3501 {
3502 SetFocusObject(actualContainer, false /* don't set caret position yet */);
3503 SetCaretPositionAfterClick(actualContainer, position, hit);
3504 }
4fe83b93 3505
9b794421 3506 if (addPropertyCommands)
303f0be7 3507 m_contextMenuPropertiesInfo.AddItems(this, actualContainer, hitObj);
9b794421
JS
3508 }
3509 else
3510 {
3511 if (addPropertyCommands)
303f0be7 3512 m_contextMenuPropertiesInfo.AddItems(this, GetFocusObject(), NULL);
9b794421
JS
3513 }
3514 }
3515 else
3516 {
3517 if (addPropertyCommands)
303f0be7 3518 m_contextMenuPropertiesInfo.AddItems(this, GetFocusObject(), NULL);
9b794421
JS
3519 }
3520 }
3521 else
cdaed652 3522 {
9b794421
JS
3523 // Invoked from the keyboard, so don't set the caret position and don't use the event
3524 // position
3525 hitObj = GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition+1);
3526 if (hitObj)
3527 contextObj = hitObj->GetParentContainer();
3528 else
3529 contextObj = GetFocusObject();
3530
603f702b
JS
3531 wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox);
3532 if (hitObj && actualContainer)
3533 {
9b794421 3534 if (addPropertyCommands)
303f0be7 3535 m_contextMenuPropertiesInfo.AddItems(this, actualContainer, hitObj);
603f702b
JS
3536 }
3537 else
9b794421
JS
3538 {
3539 if (addPropertyCommands)
303f0be7 3540 m_contextMenuPropertiesInfo.AddItems(this, GetFocusObject(), NULL);
9b794421 3541 }
cdaed652 3542 }
9b794421
JS
3543
3544 if (menu)
cdaed652 3545 {
9b794421
JS
3546 if (addPropertyCommands)
3547 m_contextMenuPropertiesInfo.AddMenuItems(menu);
3548 return m_contextMenuPropertiesInfo.GetCount();
cdaed652 3549 }
9b794421
JS
3550 else
3551 return 0;
3552}
cdaed652 3553
9b794421
JS
3554// Shows the context menu, adding appropriate property-editing commands
3555bool wxRichTextCtrl::ShowContextMenu(wxMenu* menu, const wxPoint& pt, bool addPropertyCommands)
3556{
3557 if (menu)
603f702b 3558 {
9b794421
JS
3559 PrepareContextMenu(menu, pt, addPropertyCommands);
3560 PopupMenu(menu);
3561 return true;
603f702b 3562 }
9b794421
JS
3563 else
3564 return false;
5d7836c4
JS
3565}
3566
27e20452
JS
3567bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
3568{
603f702b 3569 return GetFocusObject()->SetStyle(wxRichTextRange(start, end-1), wxRichTextAttr(style));
24777478
JS
3570}
3571
3572bool wxRichTextCtrl::SetStyle(long start, long end, const wxRichTextAttr& style)
3573{
603f702b 3574 return GetFocusObject()->SetStyle(wxRichTextRange(start, end-1), style);
27e20452
JS
3575}
3576
44cc96a8 3577bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxTextAttr& style)
24777478 3578{
603f702b 3579 return GetFocusObject()->SetStyle(range.ToInternal(), wxRichTextAttr(style));
24777478
JS
3580}
3581
3582bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style)
5d7836c4 3583{
603f702b 3584 return GetFocusObject()->SetStyle(range.ToInternal(), style);
5d7836c4
JS
3585}
3586
603f702b 3587void wxRichTextCtrl::SetStyle(wxRichTextObject *obj, const wxRichTextAttr& textAttr)
cdaed652 3588{
603f702b 3589 GetFocusObject()->SetStyle(obj, textAttr);
cdaed652
VZ
3590}
3591
3e3a754f
JS
3592// extended style setting operation with flags including:
3593// wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
3594// see richtextbuffer.h for more details.
3e3a754f 3595
24777478 3596bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxRichTextAttr& style, int flags)
3e3a754f 3597{
603f702b 3598 return GetFocusObject()->SetStyle(range.ToInternal(), style, flags);
3e3a754f
JS
3599}
3600
27e20452
JS
3601bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style)
3602{
24777478 3603 return GetBuffer().SetDefaultStyle(style);
5d7836c4
JS
3604}
3605
24777478
JS
3606bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr& style)
3607{
603f702b
JS
3608 wxRichTextAttr attr1(style);
3609 attr1.GetTextBoxAttr().Reset();
3610 return GetBuffer().SetDefaultStyle(attr1);
24777478
JS
3611}
3612
3613const wxRichTextAttr& wxRichTextCtrl::GetDefaultStyleEx() const
27e20452 3614{
61399247 3615 return GetBuffer().GetDefaultStyle();
27e20452
JS
3616}
3617
3966a9f4 3618bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style)
24777478
JS
3619{
3620 wxRichTextAttr attr;
603f702b 3621 if (GetFocusObject()->GetStyle(position, attr))
24777478
JS
3622 {
3623 style = attr;
3624 return true;
3625 }
3626 else
3627 return false;
3628}
3629
3630bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style)
5d7836c4 3631{
603f702b
JS
3632 return GetFocusObject()->GetStyle(position, style);
3633}
3634
3635bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container)
3636{
3637 wxRichTextAttr attr;
3638 if (container->GetStyle(position, attr))
3639 {
3640 style = attr;
3641 return true;
3642 }
3643 else
3644 return false;
5d7836c4
JS
3645}
3646
a7ed48a5 3647// get the common set of styles for the range
44cc96a8 3648bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttr& style)
24777478
JS
3649{
3650 wxRichTextAttr attr;
603f702b 3651 if (GetFocusObject()->GetStyleForRange(range.ToInternal(), attr))
24777478
JS
3652 {
3653 style = attr;
3654 return true;
3655 }
3656 else
3657 return false;
3658}
3659
3660bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style)
a7ed48a5 3661{
603f702b
JS
3662 return GetFocusObject()->GetStyleForRange(range.ToInternal(), style);
3663}
3664
3665bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container)
3666{
3667 return container->GetStyleForRange(range.ToInternal(), style);
a7ed48a5
JS
3668}
3669
fe5aa22c 3670/// Get the content (uncombined) attributes for this position.
24777478 3671bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style)
fe5aa22c 3672{
603f702b
JS
3673 return GetFocusObject()->GetUncombinedStyle(position, style);
3674}
3675
3676/// Get the content (uncombined) attributes for this position.
3677bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container)
3678{
3679 return container->GetUncombinedStyle(position, style);
fe5aa22c
JS
3680}
3681
590a0f8b
JS
3682bool wxRichTextCtrl::SetProperties(const wxRichTextRange& range, const wxRichTextProperties& properties, int flags)
3683{
3684 return GetFocusObject()->SetProperties(range.ToInternal(), properties, flags);
3685}
3686
5d7836c4
JS
3687/// Set font, and also the buffer attributes
3688bool wxRichTextCtrl::SetFont(const wxFont& font)
3689{
24777478 3690 wxControl::SetFont(font);
5d7836c4 3691
24777478 3692 wxRichTextAttr attr = GetBuffer().GetAttributes();
5d7836c4
JS
3693 attr.SetFont(font);
3694 GetBuffer().SetBasicStyle(attr);
fe5aa22c 3695
fe5aa22c
JS
3696 GetBuffer().Invalidate(wxRICHTEXT_ALL);
3697 Refresh(false);
5d7836c4
JS
3698
3699 return true;
3700}
3701
4d551ad5
JS
3702/// Transform logical to physical
3703wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const
5d7836c4
JS
3704{
3705 wxPoint pt;
3706 CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y);
3707
3708 return pt;
3709}
3710
3711/// Transform physical to logical
4d551ad5 3712wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
5d7836c4
JS
3713{
3714 wxPoint pt;
3715 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
3716
3717 return pt;
3718}
3719
3720/// Position the caret
603f702b 3721void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox* container)
5d7836c4 3722{
c59f6793
JS
3723 if (!GetCaret())
3724 return;
3725
3726 //wxLogDebug(wxT("PositionCaret"));
3727
5d7836c4 3728 wxRect caretRect;
603f702b 3729 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect, container))
5d7836c4 3730 {
7e4fe193
JS
3731 wxPoint newPt = caretRect.GetPosition();
3732 wxSize newSz = caretRect.GetSize();
3733 wxPoint pt = GetPhysicalPoint(newPt);
3734 if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz)
c59f6793 3735 {
1c13f06e
JS
3736 GetCaret()->Hide();
3737 if (GetCaret()->GetSize() != newSz)
3738 GetCaret()->SetSize(newSz);
e9f10004 3739
423b9ba5
JS
3740 // Adjust size so the caret size and position doesn't appear in the margins
3741 if (((pt.y + newSz.y) <= GetBuffer().GetTopMargin()) || (pt.y >= (GetClientSize().y - GetBuffer().GetBottomMargin())))
603f702b
JS
3742 {
3743 pt.x = -200;
e9f10004 3744 pt.y = -200;
603f702b 3745 }
423b9ba5
JS
3746 else if (pt.y < GetBuffer().GetTopMargin() && (pt.y + newSz.y) > GetBuffer().GetTopMargin())
3747 {
3748 newSz.y -= (GetBuffer().GetTopMargin() - pt.y);
3749 if (newSz.y > 0)
3750 {
3751 pt.y = GetBuffer().GetTopMargin();
3752 GetCaret()->SetSize(newSz);
3753 }
3754 }
3755 else if (pt.y < (GetClientSize().y - GetBuffer().GetBottomMargin()) && (pt.y + newSz.y) > (GetClientSize().y - GetBuffer().GetBottomMargin()))
3756 {
3757 newSz.y = GetClientSize().y - GetBuffer().GetBottomMargin() - pt.y;
3758 GetCaret()->SetSize(newSz);
3759 }
e9f10004 3760
c59f6793 3761 GetCaret()->Move(pt);
1c13f06e 3762 GetCaret()->Show();
c59f6793 3763 }
5d7836c4
JS
3764 }
3765}
3766
3767/// Get the caret height and position for the given character position
603f702b 3768bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect, wxRichTextParagraphLayoutBox* container)
5d7836c4
JS
3769{
3770 wxClientDC dc(this);
3771 dc.SetFont(GetFont());
7fe8059f 3772
5d7836c4
JS
3773 PrepareDC(dc);
3774
3775 wxPoint pt;
3776 int height = 0;
4fe83b93 3777
603f702b
JS
3778 if (!container)
3779 container = GetFocusObject();
5d7836c4 3780
8db2e3ef
JS
3781 wxRichTextDrawingContext context(& GetBuffer());
3782 if (container->FindPosition(dc, context, position, pt, & height, m_caretAtLineStart))
5d7836c4 3783 {
88bee7e6
JS
3784 // Caret height can't be zero
3785 if (height == 0)
3786 height = dc.GetCharHeight();
3787
5d7836c4
JS
3788 rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height));
3789 return true;
3790 }
41e155b4
WS
3791
3792 return false;
5d7836c4
JS
3793}
3794
3795/// Gets the line for the visible caret position. If the caret is
3796/// shown at the very end of the line, it means the next character is actually
3797/// on the following line. So let's get the line we're expecting to find
3798/// if this is the case.
3799wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
3800{
603f702b
JS
3801 wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(caretPosition, true);
3802 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(caretPosition, true);
5d7836c4
JS
3803 if (line)
3804 {
1e967276
JS
3805 wxRichTextRange lineRange = line->GetAbsoluteRange();
3806 if (caretPosition == lineRange.GetStart()-1 &&
3807 (para->GetRange().GetStart() != lineRange.GetStart()))
5d7836c4
JS
3808 {
3809 if (!m_caretAtLineStart)
603f702b 3810 line = GetFocusObject()->GetLineAtPosition(caretPosition-1, true);
5d7836c4
JS
3811 }
3812 }
3813 return line;
3814}
3815
3816
3817/// Move the caret to the given character position
603f702b 3818bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart, wxRichTextParagraphLayoutBox* container)
5d7836c4 3819{
603f702b 3820 if (GetBuffer().IsDirty())
2f36e8dc 3821 LayoutContent();
5d7836c4 3822
603f702b
JS
3823 if (!container)
3824 container = GetFocusObject();
3825
3826 if (pos <= container->GetOwnRange().GetEnd())
5d7836c4
JS
3827 {
3828 SetCaretPosition(pos, showAtLineStart);
3829
603f702b 3830 PositionCaret(container);
5d7836c4
JS
3831
3832 return true;
3833 }
3834 else
3835 return false;
3836}
3837
3838/// Layout the buffer: which we must do before certain operations, such as
3839/// setting the caret position.
2f36e8dc 3840bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
5d7836c4 3841{
603f702b 3842 if (GetBuffer().IsDirty() || onlyVisibleRect)
1e967276
JS
3843 {
3844 wxRect availableSpace(GetClientSize());
3845 if (availableSpace.width == 0)
3846 availableSpace.width = 10;
3847 if (availableSpace.height == 0)
3848 availableSpace.height = 10;
4d551ad5
JS
3849
3850 int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT;
3851 if (onlyVisibleRect)
3852 {
3853 flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
3854 availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3855 }
41e155b4 3856
1e967276
JS
3857 wxClientDC dc(this);
3858 dc.SetFont(GetFont());
41e155b4 3859
1e967276 3860 PrepareDC(dc);
41e155b4 3861
8db2e3ef 3862 wxRichTextDrawingContext context(& GetBuffer());
1e967276
JS
3863 GetBuffer().Defragment();
3864 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
8db2e3ef 3865 GetBuffer().Layout(dc, context, availableSpace, availableSpace, flags);
603f702b 3866 GetBuffer().Invalidate(wxRICHTEXT_NONE);
41e155b4 3867
1e967276
JS
3868 if (!IsFrozen())
3869 SetupScrollbars();
3870 }
5d7836c4
JS
3871
3872 return true;
3873}
3874
75936ec6 3875/// Is all of the selection, or the current caret position, bold?
3966a9f4 3876bool wxRichTextCtrl::IsSelectionBold()
5d7836c4
JS
3877{
3878 if (HasSelection())
3879 {
24777478 3880 wxRichTextAttr attr;
a7ed48a5 3881 wxRichTextRange range = GetSelectionRange();
5d7836c4 3882 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
7d76fbd5 3883 attr.SetFontWeight(wxFONTWEIGHT_BOLD);
7fe8059f 3884
5d7836c4
JS
3885 return HasCharacterAttributes(range, attr);
3886 }
3887 else
3888 {
3889 // If no selection, then we need to combine current style with default style
3890 // to see what the effect would be if we started typing.
24777478 3891 wxRichTextAttr attr;
5d7836c4 3892 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
ab14c7aa
JS
3893
3894 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3895 if (GetStyle(pos, attr))
5d7836c4 3896 {
ab14c7aa
JS
3897 if (IsDefaultStyleShowing())
3898 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
7d76fbd5 3899 return attr.GetFontWeight() == wxFONTWEIGHT_BOLD;
5d7836c4
JS
3900 }
3901 }
3902 return false;
3903}
3904
75936ec6 3905/// Is all of the selection, or the current caret position, italics?
3966a9f4 3906bool wxRichTextCtrl::IsSelectionItalics()
5d7836c4
JS
3907{
3908 if (HasSelection())
3909 {
a7ed48a5 3910 wxRichTextRange range = GetSelectionRange();
24777478 3911 wxRichTextAttr attr;
5d7836c4 3912 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
7d76fbd5 3913 attr.SetFontStyle(wxFONTSTYLE_ITALIC);
7fe8059f 3914
5d7836c4
JS
3915 return HasCharacterAttributes(range, attr);
3916 }
3917 else
3918 {
3919 // If no selection, then we need to combine current style with default style
3920 // to see what the effect would be if we started typing.
24777478 3921 wxRichTextAttr attr;
5d7836c4 3922 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
ab14c7aa
JS
3923
3924 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3925 if (GetStyle(pos, attr))
5d7836c4 3926 {
ab14c7aa
JS
3927 if (IsDefaultStyleShowing())
3928 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
7d76fbd5 3929 return attr.GetFontStyle() == wxFONTSTYLE_ITALIC;
5d7836c4
JS
3930 }
3931 }
3932 return false;
3933}
3934
75936ec6 3935/// Is all of the selection, or the current caret position, underlined?
3966a9f4 3936bool wxRichTextCtrl::IsSelectionUnderlined()
5d7836c4
JS
3937{
3938 if (HasSelection())
3939 {
a7ed48a5 3940 wxRichTextRange range = GetSelectionRange();
24777478 3941 wxRichTextAttr attr;
5d7836c4
JS
3942 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3943 attr.SetFontUnderlined(true);
7fe8059f 3944
5d7836c4
JS
3945 return HasCharacterAttributes(range, attr);
3946 }
3947 else
3948 {
3949 // If no selection, then we need to combine current style with default style
3950 // to see what the effect would be if we started typing.
24777478 3951 wxRichTextAttr attr;
5d7836c4 3952 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
ab14c7aa 3953 long pos = GetAdjustedCaretPosition(GetCaretPosition());
fe5aa22c 3954
ab14c7aa 3955 if (GetStyle(pos, attr))
5d7836c4 3956 {
ab14c7aa
JS
3957 if (IsDefaultStyleShowing())
3958 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
5d7836c4
JS
3959 return attr.GetFontUnderlined();
3960 }
3961 }
3962 return false;
3963}
3964
75936ec6
JS
3965/// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
3966bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag)
3967{
3968 wxRichTextAttr attr;
3969 attr.SetFlags(wxTEXT_ATTR_EFFECTS);
3970 attr.SetTextEffectFlags(flag);
3971 attr.SetTextEffects(flag);
3972
3973 if (HasSelection())
3974 {
3975 return HasCharacterAttributes(GetSelectionRange(), attr);
3976 }
3977 else
3978 {
3979 // If no selection, then we need to combine current style with default style
3980 // to see what the effect would be if we started typing.
3981 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3982 if (GetStyle(pos, attr))
3983 {
3984 if (IsDefaultStyleShowing())
3985 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
dabf7342 3986 return (attr.GetTextEffectFlags() & flag) != 0;
75936ec6
JS
3987 }
3988 }
3989 return false;
3990}
3991
5d7836c4
JS
3992/// Apply bold to the selection
3993bool wxRichTextCtrl::ApplyBoldToSelection()
3994{
24777478 3995 wxRichTextAttr attr;
5d7836c4 3996 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
7d76fbd5 3997 attr.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL : wxFONTWEIGHT_BOLD);
5d7836c4
JS
3998
3999 if (HasSelection())
3e3a754f 4000 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
5d7836c4 4001 else
063dbace
JS
4002 {
4003 wxRichTextAttr current = GetDefaultStyleEx();
4004 current.Apply(attr);
4005 SetAndShowDefaultStyle(current);
4006 }
5d7836c4
JS
4007 return true;
4008}
4009
4010/// Apply italic to the selection
4011bool wxRichTextCtrl::ApplyItalicToSelection()
4012{
24777478 4013 wxRichTextAttr attr;
5d7836c4 4014 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
7d76fbd5 4015 attr.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL : wxFONTSTYLE_ITALIC);
5d7836c4
JS
4016
4017 if (HasSelection())
3e3a754f 4018 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
5d7836c4 4019 else
063dbace
JS
4020 {
4021 wxRichTextAttr current = GetDefaultStyleEx();
4022 current.Apply(attr);
4023 SetAndShowDefaultStyle(current);
4024 }
5d7836c4
JS
4025 return true;
4026}
4027
4028/// Apply underline to the selection
4029bool wxRichTextCtrl::ApplyUnderlineToSelection()
4030{
24777478 4031 wxRichTextAttr attr;
5d7836c4 4032 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
4d551ad5 4033 attr.SetFontUnderlined(!IsSelectionUnderlined());
5d7836c4 4034
75936ec6
JS
4035 if (HasSelection())
4036 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
4037 else
4038 {
4039 wxRichTextAttr current = GetDefaultStyleEx();
4040 current.Apply(attr);
4041 SetAndShowDefaultStyle(current);
4042 }
4043 return true;
4044}
4045
4046/// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
4047bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags)
4048{
4049 wxRichTextAttr attr;
4050 attr.SetFlags(wxTEXT_ATTR_EFFECTS);
4051 attr.SetTextEffectFlags(flags);
4052 if (!DoesSelectionHaveTextEffectFlag(flags))
4053 attr.SetTextEffects(flags);
4054 else
4055 attr.SetTextEffects(attr.GetTextEffectFlags() & ~flags);
4056
5d7836c4 4057 if (HasSelection())
3e3a754f 4058 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
5d7836c4 4059 else
063dbace
JS
4060 {
4061 wxRichTextAttr current = GetDefaultStyleEx();
4062 current.Apply(attr);
4063 SetAndShowDefaultStyle(current);
4064 }
5d7836c4
JS
4065 return true;
4066}
4067
4068/// Is all of the selection aligned according to the specified flag?
3966a9f4 4069bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
5d7836c4 4070{
0ca07313 4071 wxRichTextRange range;
5d7836c4 4072 if (HasSelection())
a7ed48a5 4073 range = GetSelectionRange();
5d7836c4 4074 else
a7ed48a5 4075 range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
0ca07313 4076
24777478 4077 wxRichTextAttr attr;
0ca07313
JS
4078 attr.SetAlignment(alignment);
4079
4080 return HasParagraphAttributes(range, attr);
5d7836c4
JS
4081}
4082
4083/// Apply alignment to the selection
4084bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
4085{
24777478 4086 wxRichTextAttr attr;
5d7836c4
JS
4087 attr.SetAlignment(alignment);
4088 if (HasSelection())
4089 return SetStyle(GetSelectionRange(), attr);
4090 else
4091 {
603f702b 4092 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1);
5d7836c4 4093 if (para)
3e3a754f 4094 return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY);
5d7836c4
JS
4095 }
4096 return true;
4097}
4098
ab14c7aa 4099/// Apply a named style to the selection
1807a1f3 4100bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
ab14c7aa
JS
4101{
4102 // Flags are defined within each definition, so only certain
4103 // attributes are applied.
24777478 4104 wxRichTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle());
1807a1f3 4105
f2ab7bf3 4106 int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_RESET;
ab14c7aa 4107
38f833b1
JS
4108 if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
4109 {
4110 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
41a85215 4111
38f833b1 4112 wxRichTextRange range;
41a85215 4113
38f833b1
JS
4114 if (HasSelection())
4115 range = GetSelectionRange();
4116 else
4117 {
41a85215 4118 long pos = GetAdjustedCaretPosition(GetCaretPosition());
38f833b1
JS
4119 range = wxRichTextRange(pos, pos+1);
4120 }
41a85215
WS
4121
4122 return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags);
4123 }
4124
edae63b0
JS
4125 bool isPara = false;
4126
ab14c7aa
JS
4127 // Make sure the attr has the style name
4128 if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
3e3a754f 4129 {
edae63b0 4130 isPara = true;
ab14c7aa 4131 attr.SetParagraphStyleName(def->GetName());
1807a1f3 4132
3e3a754f 4133 // If applying a paragraph style, we only want the paragraph nodes to adopt these
38f833b1 4134 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3e3a754f
JS
4135 // to change its style independently.
4136 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
4137 }
b9433288 4138 else if (def->IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition)))
ab14c7aa 4139 attr.SetCharacterStyleName(def->GetName());
b9433288
JS
4140 else if (def->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition)))
4141 attr.GetTextBoxAttr().SetBoxStyleName(def->GetName());
ab14c7aa 4142
b9433288
JS
4143 if (def->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition)))
4144 {
4145 if (GetFocusObject() && (GetFocusObject() != & GetBuffer()))
4146 {
4147 SetStyle(GetFocusObject(), attr);
4148 return true;
4149 }
4150 else
4151 return false;
4152 }
4153 else if (HasSelection())
1807a1f3 4154 return SetStyleEx(GetSelectionRange(), attr, flags);
ab14c7aa 4155 else
1807a1f3 4156 {
24777478
JS
4157 wxRichTextAttr current = GetDefaultStyleEx();
4158 wxRichTextAttr defaultStyle(attr);
edae63b0
JS
4159 if (isPara)
4160 {
4161 // Don't apply extra character styles since they are already implied
4162 // in the paragraph style
4163 defaultStyle.SetFlags(defaultStyle.GetFlags() & ~wxTEXT_ATTR_CHARACTER);
4164 }
4165 current.Apply(defaultStyle);
063dbace 4166 SetAndShowDefaultStyle(current);
edae63b0
JS
4167
4168 // If it's a paragraph style, we want to apply the style to the
4169 // current paragraph even if we didn't select any text.
4170 if (isPara)
4171 {
4172 long pos = GetAdjustedCaretPosition(GetCaretPosition());
603f702b 4173 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos);
edae63b0
JS
4174 if (para)
4175 {
4176 return SetStyleEx(para->GetRange().FromInternal(), attr, flags);
4177 }
4178 }
1807a1f3
JS
4179 return true;
4180 }
ab14c7aa
JS
4181}
4182
fe5aa22c
JS
4183/// Apply the style sheet to the buffer, for example if the styles have changed.
4184bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
4185{
4186 if (!styleSheet)
4187 styleSheet = GetBuffer().GetStyleSheet();
4188 if (!styleSheet)
4189 return false;
4190
4191 if (GetBuffer().ApplyStyleSheet(styleSheet))
4192 {
4193 GetBuffer().Invalidate(wxRICHTEXT_ALL);
4194 Refresh(false);
4195 return true;
4196 }
4197 else
4198 return false;
4199}
4200
5d7836c4
JS
4201/// Sets the default style to the style under the cursor
4202bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
4203{
24777478 4204 wxRichTextAttr attr;
5d7836c4
JS
4205 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
4206
ab14c7aa
JS
4207 // If at the start of a paragraph, use the next position.
4208 long pos = GetAdjustedCaretPosition(GetCaretPosition());
4209
1199fbd4
JS
4210 wxRichTextObject* obj = GetFocusObject()->GetLeafObjectAtPosition(pos);
4211 if (obj && obj->IsTopLevel())
4212 {
4213 // Don't use the attributes of a top-level object, since they might apply
4214 // to content of the object, e.g. background colour.
4215 SetDefaultStyle(wxRichTextAttr());
4216 return true;
4217 }
4218 else if (GetUncombinedStyle(pos, attr))
5d7836c4
JS
4219 {
4220 SetDefaultStyle(attr);
4221 return true;
4222 }
41e155b4
WS
4223
4224 return false;
5d7836c4
JS
4225}
4226
4d551ad5
JS
4227/// Returns the first visible position in the current view
4228long wxRichTextCtrl::GetFirstVisiblePosition() const
4229{
603f702b 4230 wxRichTextLine* line = GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y);
4d551ad5
JS
4231 if (line)
4232 return line->GetAbsoluteRange().GetStart();
4233 else
4234 return 0;
4235}
4236
ea160b2e
JS
4237/// Get the first visible point in the window
4238wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const
4239{
4240 int ppuX, ppuY;
4241 int startXUnits, startYUnits;
4242
4243 GetScrollPixelsPerUnit(& ppuX, & ppuY);
4244 GetViewStart(& startXUnits, & startYUnits);
4245
4246 return wxPoint(startXUnits * ppuX, startYUnits * ppuY);
4247}
4248
ab14c7aa
JS
4249/// The adjusted caret position is the character position adjusted to take
4250/// into account whether we're at the start of a paragraph, in which case
4251/// style information should be taken from the next position, not current one.
4252long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
4253{
603f702b 4254 wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(caretPos+1);
ab14c7aa
JS
4255
4256 if (para && (caretPos+1 == para->GetRange().GetStart()))
4257 caretPos ++;
4258 return caretPos;
4259}
4260
96c9f0f6
JS
4261/// Get/set the selection range in character positions. -1, -1 means no selection.
4262/// The range is in API convention, i.e. a single character selection is denoted
4263/// by (n, n+1)
4264wxRichTextRange wxRichTextCtrl::GetSelectionRange() const
4265{
4266 wxRichTextRange range = GetInternalSelectionRange();
4267 if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1))
4268 range.SetEnd(range.GetEnd() + 1);
4269 return range;
4270}
4271
4272void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range)
4273{
76d7fc29 4274 SetSelection(range.GetStart(), range.GetEnd());
96c9f0f6
JS
4275}
4276
38f833b1
JS
4277/// Set list style
4278bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
4279{
603f702b 4280 return GetFocusObject()->SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel);
38f833b1
JS
4281}
4282
4283bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
4284{
603f702b 4285 return GetFocusObject()->SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
38f833b1
JS
4286}
4287
4288/// Clear list for given range
4289bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags)
4290{
603f702b 4291 return GetFocusObject()->ClearListStyle(range.ToInternal(), flags);
38f833b1
JS
4292}
4293
4294/// Number/renumber any list elements in the given range
4295bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
4296{
603f702b 4297 return GetFocusObject()->NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel);
38f833b1
JS
4298}
4299
4300bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
4301{
603f702b 4302 return GetFocusObject()->NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
38f833b1
JS
4303}
4304
4305/// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
4306bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel)
4307{
603f702b 4308 return GetFocusObject()->PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel);
38f833b1
JS
4309}
4310
4311bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel)
4312{
603f702b 4313 return GetFocusObject()->PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel);
38f833b1
JS
4314}
4315
12cc29c5
JS
4316/// Deletes the content in the given range
4317bool wxRichTextCtrl::Delete(const wxRichTextRange& range)
4318{
603f702b 4319 return GetFocusObject()->DeleteRangeWithUndo(range.ToInternal(), this, & GetBuffer());
12cc29c5
JS
4320}
4321
dadd4f55
JS
4322const wxArrayString& wxRichTextCtrl::GetAvailableFontNames()
4323{
4324 if (sm_availableFontNames.GetCount() == 0)
4325 {
4326 sm_availableFontNames = wxFontEnumerator::GetFacenames();
4327 sm_availableFontNames.Sort();
4328 }
4329 return sm_availableFontNames;
4330}
4331
4332void wxRichTextCtrl::ClearAvailableFontNames()
4333{
4334 sm_availableFontNames.Clear();
4335}
4336
c0a5145a 4337void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
52170c5b
JS
4338{
4339 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
4340
4341 wxTextAttrEx basicStyle = GetBasicStyle();
4342 basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
4343 SetBasicStyle(basicStyle);
4344 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
4345
4346 Refresh();
4347}
4348
7051fa41 4349// Refresh the area affected by a selection change
603f702b
JS
4350bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection& oldSelection, const wxRichTextSelection& newSelection)
4351{
4352 // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
4353 // the selection contains whole containers rather than just text, so refresh everything
4354 // for now as it would be hard to compute the rectangle bounding all selections.
4355 // TODO: improve on this.
4356 if ((oldSelection.IsValid() && (oldSelection.GetContainer() != GetFocusObject() || oldSelection.GetCount() > 1)) ||
4357 (newSelection.IsValid() && (newSelection.GetContainer() != GetFocusObject() || newSelection.GetCount() > 1)))
4358 {
4359 Refresh(false);
4360 return true;
4361 }
4fe83b93 4362
603f702b
JS
4363 wxRichTextRange oldRange, newRange;
4364 if (oldSelection.IsValid())
4365 oldRange = oldSelection.GetRange();
4366 else
4367 oldRange = wxRICHTEXT_NO_SELECTION;
4368 if (newSelection.IsValid())
4369 newRange = newSelection.GetRange();
4370 else
4371 newRange = wxRICHTEXT_NO_SELECTION;
4fe83b93 4372
7051fa41
JS
4373 // Calculate the refresh rectangle - just the affected lines
4374 long firstPos, lastPos;
603f702b 4375 if (oldRange.GetStart() == -2 && newRange.GetStart() != -2)
7051fa41 4376 {
603f702b
JS
4377 firstPos = newRange.GetStart();
4378 lastPos = newRange.GetEnd();
7051fa41 4379 }
603f702b 4380 else if (oldRange.GetStart() != -2 && newRange.GetStart() == -2)
7051fa41 4381 {
603f702b
JS
4382 firstPos = oldRange.GetStart();
4383 lastPos = oldRange.GetEnd();
7051fa41 4384 }
603f702b 4385 else if (oldRange.GetStart() == -2 && newRange.GetStart() == -2)
7051fa41
JS
4386 {
4387 return false;
4388 }
4389 else
4390 {
603f702b
JS
4391 firstPos = wxMin(oldRange.GetStart(), newRange.GetStart());
4392 lastPos = wxMax(oldRange.GetEnd(), newRange.GetEnd());
7051fa41
JS
4393 }
4394
603f702b
JS
4395 wxRichTextLine* firstLine = GetFocusObject()->GetLineAtPosition(firstPos);
4396 wxRichTextLine* lastLine = GetFocusObject()->GetLineAtPosition(lastPos);
7051fa41
JS
4397
4398 if (firstLine && lastLine)
4399 {
4400 wxSize clientSize = GetClientSize();
4401 wxPoint pt1 = GetPhysicalPoint(firstLine->GetAbsolutePosition());
4402 wxPoint pt2 = GetPhysicalPoint(lastLine->GetAbsolutePosition()) + wxPoint(0, lastLine->GetSize().y);
4403
4404 pt1.x = 0;
4405 pt1.y = wxMax(0, pt1.y);
4406 pt2.x = 0;
4407 pt2.y = wxMin(clientSize.y, pt2.y);
4408
4409 wxRect rect(pt1, wxSize(clientSize.x, pt2.y - pt1.y));
4410 RefreshRect(rect, false);
4411 }
4412 else
4413 Refresh(false);
4414
4415 return true;
4416}
4417
603f702b
JS
4418// margins functions
4419bool wxRichTextCtrl::DoSetMargins(const wxPoint& pt)
4420{
4421 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt.x, wxTEXT_ATTR_UNITS_PIXELS);
4422 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt.x, wxTEXT_ATTR_UNITS_PIXELS);
4423 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt.y, wxTEXT_ATTR_UNITS_PIXELS);
4424 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt.y, wxTEXT_ATTR_UNITS_PIXELS);
4fe83b93 4425
603f702b
JS
4426 return true;
4427}
4428
4429wxPoint wxRichTextCtrl::DoGetMargins() const
4430{
4431 return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
4432 GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
4433}
4434
4435bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox* obj, bool setCaretPosition)
4436{
4437 if (obj && !obj->AcceptsFocus())
4438 return false;
4439
4440 wxRichTextParagraphLayoutBox* oldContainer = GetFocusObject();
4441 bool changingContainer = (m_focusObject != obj);
4442
eadb039e
JS
4443 if (changingContainer && HasSelection())
4444 SelectNone();
4445
603f702b 4446 m_focusObject = obj;
4fe83b93 4447
603f702b
JS
4448 if (!obj)
4449 m_focusObject = & m_buffer;
4450
4451 if (setCaretPosition && changingContainer)
4452 {
4453 m_selection.Reset();
4454 m_selectionAnchor = -2;
4455 m_selectionAnchorObject = NULL;
4456 m_selectionState = wxRichTextCtrlSelectionState_Normal;
4457
4458 long pos = -1;
4fe83b93 4459
603f702b
JS
4460 m_caretAtLineStart = false;
4461 MoveCaret(pos, m_caretAtLineStart);
4462 SetDefaultStyleToCursorStyle();
4463
4464 wxRichTextEvent cmdEvent(
4465 wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED,
4466 GetId());
4467 cmdEvent.SetEventObject(this);
4468 cmdEvent.SetPosition(m_caretPosition+1);
4469 cmdEvent.SetOldContainer(oldContainer);
4470 cmdEvent.SetContainer(m_focusObject);
4471
4472 GetEventHandler()->ProcessEvent(cmdEvent);
4473 }
4474 return true;
4475}
4476
0c0e063e
JS
4477#if wxUSE_DRAG_AND_DROP
4478void wxRichTextCtrl::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def, wxDataObject* DataObj)
4479{
4480 m_preDrag = false;
4481
4482 if ((def != wxDragCopy) && (def != wxDragMove))
4483 {
4484 return;
4485 }
4486
4487 if (!GetSelection().IsValid())
4488 {
4489 return;
4490 }
4491
4492 wxRichTextParagraphLayoutBox* originContainer = GetSelection().GetContainer();
4493 wxRichTextParagraphLayoutBox* destContainer = GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
4494
0c0e063e
JS
4495 wxRichTextBuffer* richTextBuffer = ((wxRichTextBufferDataObject*)DataObj)->GetRichTextBuffer();
4496 if (richTextBuffer)
4497 {
4498 long position = GetCaretPosition();
4499 wxRichTextRange selectionrange = GetInternalSelectionRange();
4500 if (selectionrange.Contains(position) && (def == wxDragMove))
4501 {
4502 // It doesn't make sense to move onto itself
4503 return;
4504 }
4505
4506 // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
4507 // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
4508 bool DeleteAfter = (def == wxDragMove) && (position > selectionrange.GetEnd());
4509 if ((def == wxDragMove) && !DeleteAfter)
4510 {
4511 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4512 originContainer->DeleteRangeWithUndo(selectionrange, this, &GetBuffer());
4513 }
4514
4e63bfb9 4515 destContainer->InsertParagraphsWithUndo(&GetBuffer(), position+1, *richTextBuffer, this, 0);
0c0e063e
JS
4516 ShowPosition(position + richTextBuffer->GetOwnRange().GetEnd());
4517
4518 delete richTextBuffer;
4519
4520 if (DeleteAfter)
4521 {
4522 // We can't use e.g. DeleteSelectedContent() as it uses the focus container
4523 originContainer->DeleteRangeWithUndo(selectionrange, this, &GetBuffer());
4524 }
4525
0c0e063e
JS
4526 SelectNone();
4527 Refresh();
4528 }
4529}
4530#endif // wxUSE_DRAG_AND_DROP
4531
4532
4533#if wxUSE_DRAG_AND_DROP
4534bool wxRichTextDropSource::GiveFeedback(wxDragResult WXUNUSED(effect))
4535{
4536 wxCHECK_MSG(m_rtc, false, wxT("NULL m_rtc"));
4537
4538 long position = 0;
4539 int hit = 0;
4540 wxRichTextObject* hitObj = NULL;
4541 wxRichTextParagraphLayoutBox* container = m_rtc->FindContainerAtPoint(m_rtc->ScreenToClient(wxGetMousePosition()), position, hit, hitObj);
4542
4543 if (!(hit & wxRICHTEXT_HITTEST_NONE) && container && container->AcceptsFocus())
4544 {
4545 m_rtc->StoreFocusObject(container);
4546 m_rtc->SetCaretPositionAfterClick(container, position, hit);
4547 }
4548
4549 return false; // so that the base-class sets a cursor
4550}
4551#endif // wxUSE_DRAG_AND_DROP
4552
cc2aecde
JS
4553bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox& WXUNUSED(container), const wxRichTextRange& WXUNUSED(range)) const
4554{
4555 return true;
4556}
4557
4558bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox& WXUNUSED(container), long WXUNUSED(pos)) const
4559{
4560 return true;
4561}
4562
0c0e063e 4563
1c13f06e
JS
4564#if wxRICHTEXT_USE_OWN_CARET
4565
4566// ----------------------------------------------------------------------------
4567// initialization and destruction
4568// ----------------------------------------------------------------------------
4569
4570void wxRichTextCaret::Init()
4571{
4572 m_hasFocus = true;
cd45ed36 4573 m_refreshEnabled = true;
1c13f06e
JS
4574
4575 m_xOld =
4576 m_yOld = -1;
4577 m_richTextCtrl = NULL;
4578 m_needsUpdate = false;
a188ac29 4579 m_flashOn = true;
1c13f06e
JS
4580}
4581
4582wxRichTextCaret::~wxRichTextCaret()
4583{
a188ac29
JS
4584 if (m_timer.IsRunning())
4585 m_timer.Stop();
1c13f06e
JS
4586}
4587
4588// ----------------------------------------------------------------------------
4589// showing/hiding/moving the caret (base class interface)
4590// ----------------------------------------------------------------------------
4591
4592void wxRichTextCaret::DoShow()
4593{
a188ac29 4594 m_flashOn = true;
ab52bac8 4595
a188ac29
JS
4596 if (!m_timer.IsRunning())
4597 m_timer.Start(GetBlinkTime());
4598
1c13f06e
JS
4599 Refresh();
4600}
4601
4602void wxRichTextCaret::DoHide()
4603{
a188ac29
JS
4604 if (m_timer.IsRunning())
4605 m_timer.Stop();
4606
1c13f06e
JS
4607 Refresh();
4608}
4609
4610void wxRichTextCaret::DoMove()
4611{
4612 if (IsVisible())
4613 {
4614 Refresh();
4615
4616 if (m_xOld != -1 && m_yOld != -1)
4617 {
cd45ed36 4618 if (m_richTextCtrl && m_refreshEnabled)
1c13f06e
JS
4619 {
4620 wxRect rect(GetPosition(), GetSize());
4621 m_richTextCtrl->RefreshRect(rect, false);
4622 }
4623 }
4624 }
4625
4626 m_xOld = m_x;
4627 m_yOld = m_y;
4628}
4629
4630void wxRichTextCaret::DoSize()
4631{
4632 int countVisible = m_countVisible;
4633 if (countVisible > 0)
4634 {
4635 m_countVisible = 0;
4636 DoHide();
4637 }
4638
4639 if (countVisible > 0)
4640 {
4641 m_countVisible = countVisible;
4642 DoShow();
4643 }
4644}
4645
4646// ----------------------------------------------------------------------------
4647// handling the focus
4648// ----------------------------------------------------------------------------
4649
4650void wxRichTextCaret::OnSetFocus()
4651{
4652 m_hasFocus = true;
4653
4654 if ( IsVisible() )
4655 Refresh();
4656}
4657
4658void wxRichTextCaret::OnKillFocus()
4659{
4660 m_hasFocus = false;
4661}
4662
4663// ----------------------------------------------------------------------------
4664// drawing the caret
4665// ----------------------------------------------------------------------------
4666
4667void wxRichTextCaret::Refresh()
4668{
cd45ed36 4669 if (m_richTextCtrl && m_refreshEnabled)
1c13f06e
JS
4670 {
4671 wxRect rect(GetPosition(), GetSize());
4672 m_richTextCtrl->RefreshRect(rect, false);
4673 }
4674}
4675
4676void wxRichTextCaret::DoDraw(wxDC *dc)
4677{
4678 dc->SetPen( *wxBLACK_PEN );
4679
4680 dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH));
4681 dc->SetPen(*wxBLACK_PEN);
4682
1c13f06e
JS
4683 wxPoint pt(m_x, m_y);
4684
4685 if (m_richTextCtrl)
4686 {
4687 pt = m_richTextCtrl->GetLogicalPoint(pt);
4688 }
a188ac29
JS
4689 if (IsVisible() && m_flashOn)
4690 dc->DrawRectangle(pt.x, pt.y, m_width, m_height);
4691}
4692
4693void wxRichTextCaret::Notify()
4694{
4695 m_flashOn = !m_flashOn;
4696 Refresh();
4697}
4698
4699void wxRichTextCaretTimer::Notify()
4700{
4701 m_caret->Notify();
1c13f06e
JS
4702}
4703#endif
4704 // wxRICHTEXT_USE_OWN_CARET
4705
603f702b
JS
4706// Add an item
4707bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString& label, wxRichTextObject* obj)
4708{
4709 if (GetCount() < 3)
4710 {
4711 m_labels.Add(label);
4712 m_objects.Add(obj);
4713 return true;
4714 }
4715 else
4716 return false;
4717}
4718
4719// Returns number of menu items were added.
4720int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu* menu, int startCmd) const
4721{
4722 wxMenuItem* item = menu->FindItem(startCmd);
9b794421
JS
4723 // If none of the standard properties identifiers are in the menu, add them if necessary.
4724 // If no items to add, just set the text to something generic
4725 if (GetCount() == 0)
603f702b 4726 {
9b794421 4727 if (item)
603f702b
JS
4728 {
4729 menu->SetLabel(startCmd, _("&Properties"));
4fe83b93 4730
603f702b
JS
4731 // Delete the others if necessary
4732 int i;
4733 for (i = startCmd+1; i < startCmd+3; i++)
4734 {
4735 if (menu->FindItem(i))
4736 {
4737 menu->Delete(i);
4738 }
4739 }
4740 }
9b794421
JS
4741 }
4742 else
4743 {
4744 int i;
4745 int pos = -1;
4746 // Find the position of the first properties item
4747 for (i = 0; i < (int) menu->GetMenuItemCount(); i++)
603f702b 4748 {
9b794421
JS
4749 wxMenuItem* item = menu->FindItemByPosition(i);
4750 if (item && item->GetId() == startCmd)
603f702b 4751 {
9b794421
JS
4752 pos = i;
4753 break;
603f702b 4754 }
9b794421 4755 }
4fe83b93 4756
9b794421
JS
4757 if (pos != -1)
4758 {
4759 int insertBefore = pos+1;
4760 for (i = startCmd; i < startCmd+GetCount(); i++)
603f702b 4761 {
9b794421 4762 if (menu->FindItem(i))
603f702b 4763 {
9b794421
JS
4764 menu->SetLabel(i, m_labels[i - startCmd]);
4765 }
4766 else
4767 {
4768 if (insertBefore >= (int) menu->GetMenuItemCount())
4769 menu->Append(i, m_labels[i - startCmd]);
603f702b 4770 else
9b794421 4771 menu->Insert(insertBefore, i, m_labels[i - startCmd]);
603f702b 4772 }
9b794421
JS
4773 insertBefore ++;
4774 }
4fe83b93 4775
9b794421
JS
4776 // Delete any old items still left on the menu
4777 for (i = startCmd + GetCount(); i < startCmd+3; i++)
4778 {
4779 if (menu->FindItem(i))
603f702b 4780 {
9b794421 4781 menu->Delete(i);
603f702b
JS
4782 }
4783 }
4784 }
9b794421
JS
4785 else
4786 {
4787 // No existing property identifiers were found, so append to the end of the menu.
4788 menu->AppendSeparator();
4789 for (i = startCmd; i < startCmd+GetCount(); i++)
4790 {
4791 menu->Append(i, m_labels[i - startCmd]);
4792 }
4793 }
603f702b
JS
4794 }
4795
4796 return GetCount();
4797}
4798
4799// Add appropriate menu items for the current container and clicked on object
4800// (and container's parent, if appropriate).
303f0be7 4801int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl* ctrl, wxRichTextObject* container, wxRichTextObject* obj)
603f702b
JS
4802{
4803 Clear();
303f0be7 4804 if (obj && ctrl->CanEditProperties(obj))
590a0f8b 4805 AddItem(ctrl->GetPropertiesMenuLabel(obj), obj);
603f702b 4806
590a0f8b
JS
4807 if (container && container != obj && ctrl->CanEditProperties(container) && m_labels.Index(ctrl->GetPropertiesMenuLabel(container)) == wxNOT_FOUND)
4808 AddItem(ctrl->GetPropertiesMenuLabel(container), container);
603f702b 4809
590a0f8b
JS
4810 if (container && container->GetParent() && ctrl->CanEditProperties(container->GetParent()) && m_labels.Index(ctrl->GetPropertiesMenuLabel(container->GetParent())) == wxNOT_FOUND)
4811 AddItem(ctrl->GetPropertiesMenuLabel(container->GetParent()), container->GetParent());
603f702b
JS
4812
4813 return GetCount();
4814}
4815
5d7836c4
JS
4816#endif
4817 // wxUSE_RICHTEXT