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