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