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