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