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