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