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