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