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