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