]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextctrl.cpp
Typo.
[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
JS
33#include "wx/arrimpl.cpp"
34
27e20452
JS
35// DLL options compatibility check:
36#include "wx/app.h"
37WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
38
5d7836c4
JS
39DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED)
40DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED)
41DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK)
42DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK)
43DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK)
44DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK)
45DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN)
46
47#if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
48IMPLEMENT_CLASS( wxRichTextCtrl, wxControl )
49#else
50IMPLEMENT_CLASS( wxRichTextCtrl, wxScrolledWindow )
51#endif
52
53IMPLEMENT_CLASS( wxRichTextEvent, wxNotifyEvent )
54
55#if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
56BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl )
57#else
58BEGIN_EVENT_TABLE( wxRichTextCtrl, wxScrolledWindow )
59#endif
60 EVT_PAINT(wxRichTextCtrl::OnPaint)
61 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground)
4d551ad5 62 EVT_IDLE(wxRichTextCtrl::OnIdle)
c59f6793 63 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll)
5d7836c4
JS
64 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick)
65 EVT_MOTION(wxRichTextCtrl::OnMoveMouse)
66 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp)
67 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick)
68 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick)
69 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick)
70 EVT_CHAR(wxRichTextCtrl::OnChar)
71 EVT_SIZE(wxRichTextCtrl::OnSize)
72 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus)
73 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus)
74 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu)
75
76 EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo)
77 EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo)
78
79 EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo)
80 EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo)
81
82 EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy)
83 EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy)
84
85 EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste)
86 EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste)
87
88 EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut)
89 EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut)
90
91 EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear)
92 EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear)
93
94 EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll)
95 EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll)
96END_EVENT_TABLE()
97
98/*!
99 * wxRichTextCtrl
100 */
101
102wxRichTextCtrl::wxRichTextCtrl()
103#if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
104 : wxScrollHelper(this)
105#endif
106{
107 Init();
108}
109
27e20452 110wxRichTextCtrl::wxRichTextCtrl( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style)
5d7836c4
JS
111#if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
112 : wxScrollHelper(this)
113#endif
114{
115 Init();
27e20452 116 Create(parent, id, value, pos, size, style);
5d7836c4
JS
117}
118
119/// Creation
27e20452 120bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style)
5d7836c4
JS
121{
122#if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
123 if (!wxTextCtrlBase::Create(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE
124 ))
125 return false;
126#else
127 if (!wxScrolledWindow::Create(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE
128 ))
129 return false;
130#endif
131
132 if (!GetFont().Ok())
133 {
134 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
135 }
7fe8059f 136
5d7836c4
JS
137 GetBuffer().SetRichTextCtrl(this);
138
139 wxTextAttrEx attributes;
140 attributes.SetFont(GetFont());
141 attributes.SetTextColour(*wxBLACK);
142 attributes.SetBackgroundColour(*wxWHITE);
143 attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
144 attributes.SetFlags(wxTEXT_ATTR_ALL);
5d7836c4
JS
145 SetBasicStyle(attributes);
146
fe5aa22c
JS
147 // The default attributes will be merged with base attributes, so
148 // can be empty to begin with
149 wxTextAttrEx defaultAttributes;
150 SetDefaultStyle(defaultAttributes);
151
7fe8059f 152 SetBackgroundColour(*wxWHITE);
5d7836c4
JS
153 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
154
7fe8059f 155 // Tell the sizers to use the given or best size
5d7836c4 156 SetBestFittingSize(size);
7fe8059f 157
5d7836c4
JS
158 // Create a buffer
159 RecreateBuffer(size);
160
5d7836c4 161 SetCursor(wxCursor(wxCURSOR_IBEAM));
61399247 162
27e20452
JS
163 if (!value.IsEmpty())
164 SetValue(value);
7fe8059f 165
5d7836c4
JS
166 return true;
167}
168
169wxRichTextCtrl::~wxRichTextCtrl()
170{
171 delete m_contextMenu;
172}
173
174/// Member initialisation
175void wxRichTextCtrl::Init()
176{
177 m_freezeCount = 0;
178 m_contextMenu = NULL;
179 m_caret = NULL;
180 m_caretPosition = -1;
181 m_selectionRange.SetRange(-2, -2);
182 m_selectionAnchor = -2;
183 m_editable = true;
184 m_caretAtLineStart = false;
185 m_dragging = false;
4d551ad5
JS
186 m_fullLayoutRequired = false;
187 m_fullLayoutTime = 0;
188 m_fullLayoutSavedPosition = 0;
189 m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD;
ab14c7aa 190 m_caretPositionForDefaultStyle = -2;
5d7836c4
JS
191}
192
193/// Call Freeze to prevent refresh
194void wxRichTextCtrl::Freeze()
195{
196 m_freezeCount ++;
197}
198
199/// Call Thaw to refresh
0bab774b 200void wxRichTextCtrl::Thaw()
5d7836c4
JS
201{
202 m_freezeCount --;
203
0bab774b 204 if (m_freezeCount == 0)
5d7836c4
JS
205 {
206 SetupScrollbars();
76bcd815 207 Refresh(false);
5d7836c4
JS
208 }
209}
210
211/// Clear all text
212void wxRichTextCtrl::Clear()
213{
214 m_buffer.Reset();
215 m_buffer.SetDirty(true);
216 m_caretPosition = -1;
ab14c7aa 217 m_caretPositionForDefaultStyle = -2;
5d7836c4
JS
218 m_caretAtLineStart = false;
219 m_selectionRange.SetRange(-2, -2);
220
3956d02a
JS
221 SetScrollbars(0, 0, 0, 0, 0, 0);
222
5d7836c4
JS
223 if (m_freezeCount == 0)
224 {
225 SetupScrollbars();
76bcd815 226 Refresh(false);
5d7836c4
JS
227 }
228 SendUpdateEvent();
229}
230
231/// Painting
232void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
233{
d2142335
JS
234 if (GetCaret())
235 GetCaret()->Hide();
236
5d7836c4 237 {
c59f6793
JS
238 wxBufferedPaintDC dc(this, m_bufferBitmap);
239 //wxLogDebug(wxT("OnPaint"));
41e155b4 240
c59f6793 241 PrepareDC(dc);
41e155b4 242
c59f6793
JS
243 if (m_freezeCount > 0)
244 return;
41e155b4 245
c59f6793 246 dc.SetFont(GetFont());
41e155b4 247
c59f6793
JS
248 // Paint the background
249 PaintBackground(dc);
41e155b4 250
c59f6793 251 wxRegion dirtyRegion = GetUpdateRegion();
41e155b4 252
c59f6793
JS
253 wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
254 wxRect availableSpace(GetClientSize());
255 if (GetBuffer().GetDirty())
256 {
257 GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
258 GetBuffer().SetDirty(false);
259 SetupScrollbars();
260 }
41e155b4 261
96c9f0f6 262 GetBuffer().Draw(dc, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */);
5d7836c4 263 }
d2142335
JS
264
265 if (GetCaret())
266 GetCaret()->Show();
267
7383cf9d 268 PositionCaret();
5d7836c4
JS
269}
270
271// Empty implementation, to prevent flicker
272void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
273{
274}
275
276void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
277{
c59f6793
JS
278 wxCaret* caret = new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16);
279 SetCaret(caret);
280 caret->Show();
281 PositionCaret();
282
5d7836c4 283 if (!IsFrozen())
76bcd815 284 Refresh(false);
5d7836c4
JS
285}
286
287void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
288{
c59f6793
JS
289 SetCaret(NULL);
290
5d7836c4 291 if (!IsFrozen())
76bcd815 292 Refresh(false);
5d7836c4
JS
293}
294
295/// Left-click
296void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
297{
298 SetFocus();
299
300 wxClientDC dc(this);
301 PrepareDC(dc);
302 dc.SetFont(GetFont());
7fe8059f 303
5d7836c4
JS
304 long position = 0;
305 int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position);
306
307 if (hit != wxRICHTEXT_HITTEST_NONE)
308 {
309 m_dragStart = event.GetLogicalPosition(dc);
310 m_dragging = true;
311 CaptureMouse();
312
313 SelectNone();
314
315 bool caretAtLineStart = false;
316
317 if (hit & wxRICHTEXT_HITTEST_BEFORE)
318 {
319 // If we're at the start of a line (but not first in para)
320 // then we should keep the caret showing at the start of the line
321 // by showing the m_caretAtLineStart flag.
322 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
323 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
324
1e967276 325 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
5d7836c4
JS
326 caretAtLineStart = true;
327 position --;
328 }
329
330 MoveCaret(position, caretAtLineStart);
331 SetDefaultStyleToCursorStyle();
332
333#if 0
334 wxWindow* p = GetParent();
335 while (p && !p->IsKindOf(CLASSINFO(wxFrame)))
336 p = p->GetParent();
337
338 wxFrame* frame = wxDynamicCast(p, wxFrame);
339 if (frame)
340 {
341 wxString msg = wxString::Format(wxT("Found position %ld"), position);
342 frame->SetStatusText(msg, 1);
343 }
344#endif
345 }
346
347 event.Skip();
348}
349
350/// Left-up
351void wxRichTextCtrl::OnLeftUp(wxMouseEvent& WXUNUSED(event))
352{
353 if (m_dragging)
354 {
355 m_dragging = false;
356 if (GetCapture() == this)
357 ReleaseMouse();
358 }
359}
360
361/// Left-click
362void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event)
363{
364 if (!event.Dragging())
365 {
366 event.Skip();
367 return;
368 }
369
370 wxClientDC dc(this);
371 PrepareDC(dc);
372 dc.SetFont(GetFont());
7fe8059f 373
5d7836c4
JS
374 long position = 0;
375 wxPoint logicalPt = event.GetLogicalPosition(dc);
376 int hit = GetBuffer().HitTest(dc, logicalPt, position);
377
378 if (m_dragging && hit != wxRICHTEXT_HITTEST_NONE)
379 {
380 // TODO: test closeness
381
382 bool caretAtLineStart = false;
383
384 if (hit & wxRICHTEXT_HITTEST_BEFORE)
385 {
386 // If we're at the start of a line (but not first in para)
387 // then we should keep the caret showing at the start of the line
388 // by showing the m_caretAtLineStart flag.
389 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
390 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
391
1e967276 392 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
5d7836c4
JS
393 caretAtLineStart = true;
394 position --;
395 }
396
397 if (m_caretPosition != position)
398 {
399 bool extendSel = ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN);
7fe8059f 400
5d7836c4
JS
401 MoveCaret(position, caretAtLineStart);
402 SetDefaultStyleToCursorStyle();
7fe8059f 403
5d7836c4 404 if (extendSel)
76bcd815 405 Refresh(false);
5d7836c4
JS
406 }
407 }
408}
409
410/// Right-click
411void wxRichTextCtrl::OnRightClick(wxMouseEvent& event)
412{
413 SetFocus();
414 event.Skip();
415}
416
417/// Left-double-click
418void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& event)
419{
420 event.Skip();
421}
422
423/// Middle-click
424void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event)
425{
426 event.Skip();
427}
428
429/// Key press
430void wxRichTextCtrl::OnChar(wxKeyEvent& event)
431{
432 int flags = 0;
433 if (event.ControlDown())
434 flags |= wxRICHTEXT_CTRL_DOWN;
435 if (event.ShiftDown())
436 flags |= wxRICHTEXT_SHIFT_DOWN;
437 if (event.AltDown())
438 flags |= wxRICHTEXT_ALT_DOWN;
439
440 if (event.GetKeyCode() == WXK_LEFT ||
441 event.GetKeyCode() == WXK_RIGHT ||
442 event.GetKeyCode() == WXK_UP ||
443 event.GetKeyCode() == WXK_DOWN ||
444 event.GetKeyCode() == WXK_HOME ||
445 event.GetKeyCode() == WXK_PAGEUP ||
446 event.GetKeyCode() == WXK_PAGEDOWN ||
5d7836c4
JS
447 event.GetKeyCode() == WXK_END)
448 {
0bab774b 449 KeyboardNavigate(event.GetKeyCode(), flags);
99257cbe 450 return;
5d7836c4 451 }
99257cbe
VZ
452
453 // all the other keys modify the controls contents which shouldn't be
454 // possible if we're read-only
455 if ( !IsEditable() )
456 {
457 event.Skip();
458 return;
459 }
460
461 if (event.GetKeyCode() == WXK_RETURN)
5d7836c4
JS
462 {
463 BeginBatchUndo(_("Insert Text"));
464
465 long newPos = m_caretPosition;
466
467 DeleteSelectedContent(& newPos);
468
fe5aa22c 469 GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
5d7836c4
JS
470
471 wxRichTextEvent cmdEvent(
472 wxEVT_COMMAND_RICHTEXT_RETURN,
473 GetId());
474 cmdEvent.SetEventObject(this);
475 cmdEvent.SetFlags(flags);
476 GetEventHandler()->ProcessEvent(cmdEvent);
477
478 EndBatchUndo();
479 SetDefaultStyleToCursorStyle();
f9ed2d16
JS
480
481 ScrollIntoView(m_caretPosition, WXK_RIGHT);
5d7836c4
JS
482 }
483 else if (event.GetKeyCode() == WXK_BACK)
484 {
485 BeginBatchUndo(_("Delete Text"));
486
487 // Submit range in character positions, which are greater than caret positions,
488 // so subtract 1 for deleted character and add 1 for conversion to character position.
489 if (m_caretPosition > -1 && !HasSelection())
490 {
491 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition, m_caretPosition),
492 m_caretPosition, // Current caret position
493 m_caretPosition-1, // New caret position
494 this);
495 }
496 else
497 DeleteSelectedContent();
498
499 EndBatchUndo();
500
501 // Shouldn't this be in Do()?
502 if (GetLastPosition() == -1)
503 {
504 GetBuffer().Reset();
505
506 m_caretPosition = -1;
507 PositionCaret();
508 SetDefaultStyleToCursorStyle();
509 }
510
f9ed2d16 511 ScrollIntoView(m_caretPosition, WXK_LEFT);
5d7836c4
JS
512 }
513 else if (event.GetKeyCode() == WXK_DELETE)
514 {
515 BeginBatchUndo(_("Delete Text"));
516
517 // Submit range in character positions, which are greater than caret positions,
518 if (m_caretPosition < GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
519 {
520 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition+1, m_caretPosition+1),
521 m_caretPosition, // Current caret position
522 m_caretPosition+1, // New caret position
523 this);
524 }
525 else
526 DeleteSelectedContent();
527
528 EndBatchUndo();
529
530 // Shouldn't this be in Do()?
531 if (GetLastPosition() == -1)
532 {
533 GetBuffer().Reset();
534
535 m_caretPosition = -1;
536 PositionCaret();
537 SetDefaultStyleToCursorStyle();
538 }
539 }
540 else
541 {
542 BeginBatchUndo(_("Insert Text"));
543
544 long newPos = m_caretPosition;
545 DeleteSelectedContent(& newPos);
546
547 wxString str = (wxChar) event.GetKeyCode();
fe5aa22c 548 GetBuffer().InsertTextWithUndo(newPos+1, str, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
5d7836c4
JS
549
550 EndBatchUndo();
551
552 SetDefaultStyleToCursorStyle();
f9ed2d16 553 ScrollIntoView(m_caretPosition, WXK_RIGHT);
5d7836c4 554 }
5d7836c4
JS
555}
556
557/// Delete content if there is a selection, e.g. when pressing a key.
558bool wxRichTextCtrl::DeleteSelectedContent(long* newPos)
559{
560 if (HasSelection())
561 {
562 long pos = m_selectionRange.GetStart();
563 GetBuffer().DeleteRangeWithUndo(m_selectionRange,
564 m_caretPosition, // Current caret position
565 pos, // New caret position
566 this);
567 m_selectionRange.SetRange(-2, -2);
568
569 if (newPos)
570 *newPos = pos-1;
571 return true;
572 }
573 else
574 return false;
575}
576
577/// Keyboard navigation
578
579/*
580
581Left: left one character
582Right: right one character
583Up: up one line
584Down: down one line
585Ctrl-Left: left one word
586Ctrl-Right: right one word
587Ctrl-Up: previous paragraph start
588Ctrl-Down: next start of paragraph
589Home: start of line
590End: end of line
591Ctrl-Home: start of document
592Ctrl-End: end of document
593Page-Up: Up a screen
594Page-Down: Down a screen
595
596Maybe:
597
598Ctrl-Alt-PgUp: Start of window
599Ctrl-Alt-PgDn: End of window
600F8: Start selection mode
601Esc: End selection mode
602
603Adding Shift does the above but starts/extends selection.
604
605
606 */
607
0bab774b 608bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags)
5d7836c4
JS
609{
610 bool success = false;
5d7836c4
JS
611
612 if (keyCode == WXK_RIGHT)
613 {
614 if (flags & wxRICHTEXT_CTRL_DOWN)
615 success = WordRight(1, flags);
616 else
617 success = MoveRight(1, flags);
618 }
619 else if (keyCode == WXK_LEFT)
620 {
621 if (flags & wxRICHTEXT_CTRL_DOWN)
622 success = WordLeft(1, flags);
623 else
624 success = MoveLeft(1, flags);
625 }
626 else if (keyCode == WXK_UP)
627 {
628 if (flags & wxRICHTEXT_CTRL_DOWN)
629 success = MoveToParagraphStart(flags);
630 else
631 success = MoveUp(1, flags);
632 }
633 else if (keyCode == WXK_DOWN)
634 {
635 if (flags & wxRICHTEXT_CTRL_DOWN)
636 success = MoveToParagraphEnd(flags);
637 else
638 success = MoveDown(1, flags);
639 }
faa94f3e 640 else if (keyCode == WXK_PAGEUP)
5d7836c4
JS
641 {
642 success = PageUp(1, flags);
643 }
faa94f3e 644 else if (keyCode == WXK_PAGEDOWN)
5d7836c4
JS
645 {
646 success = PageDown(1, flags);
647 }
648 else if (keyCode == WXK_HOME)
649 {
650 if (flags & wxRICHTEXT_CTRL_DOWN)
651 success = MoveHome(flags);
652 else
653 success = MoveToLineStart(flags);
654 }
655 else if (keyCode == WXK_END)
656 {
657 if (flags & wxRICHTEXT_CTRL_DOWN)
658 success = MoveEnd(flags);
659 else
660 success = MoveToLineEnd(flags);
661 }
662
663 if (success)
664 {
665 ScrollIntoView(m_caretPosition, keyCode);
666 SetDefaultStyleToCursorStyle();
667 }
668
5d7836c4
JS
669 return success;
670}
671
672/// Extend the selection. Selections are in caret positions.
673bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags)
674{
675 if (flags & wxRICHTEXT_SHIFT_DOWN)
676 {
677 // If not currently selecting, start selecting
678 if (m_selectionRange.GetStart() == -2)
679 {
680 m_selectionAnchor = oldPos;
681
682 if (oldPos > newPos)
683 m_selectionRange.SetRange(newPos+1, oldPos);
684 else
685 m_selectionRange.SetRange(oldPos+1, newPos);
686 }
687 else
688 {
689 // Always ensure that the selection range start is greater than
690 // the end.
691 if (newPos > m_selectionAnchor)
692 m_selectionRange.SetRange(m_selectionAnchor+1, newPos);
693 else
694 m_selectionRange.SetRange(newPos+1, m_selectionAnchor);
695 }
696
697 if (m_selectionRange.GetStart() > m_selectionRange.GetEnd())
698 {
699 wxLogDebug(wxT("Strange selection range"));
700 }
7fe8059f 701
5d7836c4
JS
702 return true;
703 }
704 else
705 return false;
706}
707
708/// Scroll into view, returning true if we scrolled.
709/// This takes a _caret_ position.
710bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode)
711{
712 wxRichTextLine* line = GetVisibleLineForCaretPosition(position);
713
714 if (!line)
715 return false;
716
717 int ppuX, ppuY;
718 GetScrollPixelsPerUnit(& ppuX, & ppuY);
719
3956d02a
JS
720 int startXUnits, startYUnits;
721 GetViewStart(& startXUnits, & startYUnits);
722 int startY = startYUnits * ppuY;
5d7836c4 723
8d7eaf91 724 int sx = 0, sy = 0;
5d7836c4 725 GetVirtualSize(& sx, & sy);
3956d02a
JS
726 int sxUnits = 0;
727 int syUnits = 0;
5d7836c4 728 if (ppuY != 0)
3956d02a 729 syUnits = sy/ppuY;
5d7836c4
JS
730
731 wxRect rect = line->GetRect();
732
733 bool scrolled = false;
734
735 wxSize clientSize = GetClientSize();
736
737 // Going down
faa94f3e 738 if (keyCode == WXK_DOWN || keyCode == WXK_RIGHT || keyCode == WXK_END || keyCode == WXK_PAGEDOWN)
5d7836c4
JS
739 {
740 if ((rect.y + rect.height) > (clientSize.y + startY))
741 {
742 // Make it scroll so this item is at the bottom
743 // of the window
744 int y = rect.y - (clientSize.y - rect.height);
3956d02a 745 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 746
3956d02a
JS
747 // If we're still off the screen, scroll another line down
748 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
749 yUnits ++;
750
751 if (startYUnits != yUnits)
c59f6793 752 {
3956d02a 753 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
754 scrolled = true;
755 }
5d7836c4
JS
756 }
757 else if (rect.y < startY)
758 {
759 // Make it scroll so this item is at the top
760 // of the window
761 int y = rect.y ;
3956d02a 762 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 763
3956d02a 764 if (startYUnits != yUnits)
c59f6793 765 {
3956d02a 766 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
767 scrolled = true;
768 }
5d7836c4 769 }
5d7836c4
JS
770 }
771 // Going up
faa94f3e 772 else if (keyCode == WXK_UP || keyCode == WXK_LEFT || keyCode == WXK_HOME || keyCode == WXK_PAGEUP )
5d7836c4
JS
773 {
774 if (rect.y < startY)
775 {
776 // Make it scroll so this item is at the top
777 // of the window
778 int y = rect.y ;
3956d02a 779 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
c59f6793 780
3956d02a 781 if (startYUnits != yUnits)
c59f6793 782 {
3956d02a 783 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
784 scrolled = true;
785 }
5d7836c4
JS
786 }
787 else if ((rect.y + rect.height) > (clientSize.y + startY))
788 {
789 // Make it scroll so this item is at the bottom
790 // of the window
791 int y = rect.y - (clientSize.y - rect.height);
3956d02a
JS
792 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
793
794 // If we're still off the screen, scroll another line down
795 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
796 yUnits ++;
c59f6793 797
3956d02a 798 if (startYUnits != yUnits)
c59f6793 799 {
3956d02a 800 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
c59f6793
JS
801 scrolled = true;
802 }
5d7836c4 803 }
5d7836c4
JS
804 }
805 PositionCaret();
806
807 return scrolled;
808}
809
810/// Is the given position visible on the screen?
811bool wxRichTextCtrl::IsPositionVisible(long pos) const
812{
813 wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1);
814
815 if (!line)
816 return false;
817
818 int ppuX, ppuY;
819 GetScrollPixelsPerUnit(& ppuX, & ppuY);
820
821 int startX, startY;
822 GetViewStart(& startX, & startY);
823 startX = 0;
824 startY = startY * ppuY;
825
8d7eaf91 826 int sx = 0, sy = 0;
5d7836c4
JS
827 GetVirtualSize(& sx, & sy);
828 sx = 0;
829 if (ppuY != 0)
830 sy = sy/ppuY;
831
832 wxRect rect = line->GetRect();
833
834 wxSize clientSize = GetClientSize();
835
836 return !(((rect.y + rect.height) > (clientSize.y + startY)) || rect.y < startY);
837}
838
839void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart)
840{
841 m_caretPosition = position;
842 m_caretAtLineStart = showAtLineStart;
843}
844
845/// Move caret one visual step forward: this may mean setting a flag
846/// and keeping the same position if we're going from the end of one line
847/// to the start of the next, which may be the exact same caret position.
848void wxRichTextCtrl::MoveCaretForward(long oldPosition)
849{
850 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
851
852 // Only do the check if we're not at the end of the paragraph (where things work OK
853 // anyway)
854 if (para && (oldPosition != para->GetRange().GetEnd() - 1))
855 {
856 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
857
858 if (line)
859 {
1e967276
JS
860 wxRichTextRange lineRange = line->GetAbsoluteRange();
861
5d7836c4
JS
862 // We're at the end of a line. See whether we need to
863 // stay at the same actual caret position but change visual
864 // position, or not.
1e967276 865 if (oldPosition == lineRange.GetEnd())
5d7836c4
JS
866 {
867 if (m_caretAtLineStart)
868 {
869 // We're already at the start of the line, so actually move on now.
870 m_caretPosition = oldPosition + 1;
871 m_caretAtLineStart = false;
872 }
873 else
874 {
875 // We're showing at the end of the line, so keep to
876 // the same position but indicate that we're to show
877 // at the start of the next line.
878 m_caretPosition = oldPosition;
7fe8059f 879 m_caretAtLineStart = true;
5d7836c4
JS
880 }
881 SetDefaultStyleToCursorStyle();
882 return;
883 }
884 }
885 }
886 m_caretPosition ++;
887 SetDefaultStyleToCursorStyle();
888}
889
890/// Move caret one visual step backward: this may mean setting a flag
891/// and keeping the same position if we're going from the end of one line
892/// to the start of the next, which may be the exact same caret position.
893void wxRichTextCtrl::MoveCaretBack(long oldPosition)
894{
895 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
896
897 // Only do the check if we're not at the start of the paragraph (where things work OK
898 // anyway)
899 if (para && (oldPosition != para->GetRange().GetStart()))
900 {
901 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
902
903 if (line)
904 {
1e967276
JS
905 wxRichTextRange lineRange = line->GetAbsoluteRange();
906
5d7836c4
JS
907 // We're at the start of a line. See whether we need to
908 // stay at the same actual caret position but change visual
909 // position, or not.
1e967276 910 if (oldPosition == lineRange.GetStart())
5d7836c4
JS
911 {
912 m_caretPosition = oldPosition-1;
913 m_caretAtLineStart = true;
914 return;
915 }
1e967276 916 else if (oldPosition == lineRange.GetEnd())
5d7836c4
JS
917 {
918 if (m_caretAtLineStart)
919 {
920 // We're at the start of the line, so keep the same caret position
921 // but clear the start-of-line flag.
922 m_caretPosition = oldPosition;
923 m_caretAtLineStart = false;
924 }
925 else
926 {
927 // We're showing at the end of the line, so go back
928 // to the previous character position.
929 m_caretPosition = oldPosition - 1;
930 }
931 SetDefaultStyleToCursorStyle();
932 return;
933 }
934 }
935 }
936 m_caretPosition --;
937 SetDefaultStyleToCursorStyle();
938}
939
940/// Move right
941bool wxRichTextCtrl::MoveRight(int noPositions, int flags)
942{
943 long endPos = GetBuffer().GetRange().GetEnd();
944
945 if (m_caretPosition + noPositions < endPos)
946 {
947 long oldPos = m_caretPosition;
948 long newPos = m_caretPosition + noPositions;
949
950 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
951 if (!extendSel)
952 SelectNone();
953
954 // Determine by looking at oldPos and m_caretPosition whether
955 // we moved from the end of a line to the start of the next line, in which case
956 // we want to adjust the caret position such that it is positioned at the
957 // start of the next line, rather than jumping past the first character of the
958 // line.
959 if (noPositions == 1 && !extendSel)
960 MoveCaretForward(oldPos);
961 else
962 SetCaretPosition(newPos);
963
964 PositionCaret();
965 SetDefaultStyleToCursorStyle();
966
c59f6793 967 if (extendSel)
76bcd815 968 Refresh(false);
5d7836c4
JS
969 return true;
970 }
971 else
7fe8059f 972 return false;
5d7836c4
JS
973}
974
975/// Move left
976bool wxRichTextCtrl::MoveLeft(int noPositions, int flags)
977{
978 long startPos = -1;
979
980 if (m_caretPosition > startPos - noPositions + 1)
981 {
982 long oldPos = m_caretPosition;
983 long newPos = m_caretPosition - noPositions;
984 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
985 if (!extendSel)
986 SelectNone();
987
988 if (noPositions == 1 && !extendSel)
989 MoveCaretBack(oldPos);
990 else
991 SetCaretPosition(newPos);
992
993 PositionCaret();
994 SetDefaultStyleToCursorStyle();
995
c59f6793 996 if (extendSel)
76bcd815 997 Refresh(false);
5d7836c4
JS
998 return true;
999 }
1000 else
7fe8059f 1001 return false;
5d7836c4
JS
1002}
1003
1004/// Move up
1005bool wxRichTextCtrl::MoveUp(int noLines, int flags)
1006{
1007 return MoveDown(- noLines, flags);
1008}
1009
1010/// Move up
1011bool wxRichTextCtrl::MoveDown(int noLines, int flags)
1012{
c59f6793
JS
1013 if (!GetCaret())
1014 return false;
1015
5d7836c4
JS
1016 long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart);
1017 wxPoint pt = GetCaret()->GetPosition();
1018 long newLine = lineNumber + noLines;
1019
1020 if (lineNumber != -1)
1021 {
1022 if (noLines > 0)
1023 {
1024 long lastLine = GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1025
1026 if (newLine > lastLine)
1027 return false;
1028 }
1029 else
1030 {
1031 if (newLine < 0)
1032 return false;
1033 }
1034 }
1035
1036 wxRichTextLine* lineObj = GetBuffer().GetLineForVisibleLineNumber(newLine);
1037 if (lineObj)
7fe8059f
WS
1038 {
1039 pt.y = lineObj->GetAbsolutePosition().y + 2;
5d7836c4
JS
1040 }
1041 else
1042 return false;
1043
1044 long newPos = 0;
1045 wxClientDC dc(this);
1046 PrepareDC(dc);
1047 dc.SetFont(GetFont());
7fe8059f 1048
5d7836c4 1049 int hitTest = GetBuffer().HitTest(dc, pt, newPos);
7fe8059f 1050
5d7836c4
JS
1051 if (hitTest != wxRICHTEXT_HITTEST_NONE)
1052 {
1053 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1054 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1055 // so we view the caret at the start of the line.
1056 bool caretLineStart = false;
1057 if (hitTest == wxRICHTEXT_HITTEST_BEFORE)
1058 {
1059 wxRichTextLine* thisLine = GetBuffer().GetLineAtPosition(newPos-1);
1e967276
JS
1060 wxRichTextRange lineRange;
1061 if (thisLine)
1062 lineRange = thisLine->GetAbsoluteRange();
1063
1064 if (thisLine && (newPos-1) == lineRange.GetEnd())
5d7836c4 1065 {
1e967276
JS
1066 newPos --;
1067 caretLineStart = true;
5d7836c4
JS
1068 }
1069 else
1070 {
1071 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(newPos);
1072 if (para && para->GetRange().GetStart() == newPos)
1073 newPos --;
1074 }
1075 }
1076
1077 long newSelEnd = newPos;
1078
c59f6793
JS
1079 bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags);
1080 if (!extendSel)
5d7836c4
JS
1081 SelectNone();
1082
1083 SetCaretPosition(newPos, caretLineStart);
1084 PositionCaret();
1085 SetDefaultStyleToCursorStyle();
1086
c59f6793 1087 if (extendSel)
76bcd815 1088 Refresh(false);
5d7836c4
JS
1089 return true;
1090 }
41e155b4
WS
1091
1092 return false;
5d7836c4
JS
1093}
1094
1095/// Move to the end of the paragraph
1096bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
1097{
1098 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1099 if (para)
1100 {
1101 long newPos = para->GetRange().GetEnd() - 1;
c59f6793
JS
1102 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1103 if (!extendSel)
5d7836c4
JS
1104 SelectNone();
1105
1106 SetCaretPosition(newPos);
1107 PositionCaret();
1108 SetDefaultStyleToCursorStyle();
1109
c59f6793 1110 if (extendSel)
76bcd815 1111 Refresh(false);
5d7836c4
JS
1112 return true;
1113 }
1114
1115 return false;
1116}
1117
1118/// Move to the start of the paragraph
1119bool wxRichTextCtrl::MoveToParagraphStart(int flags)
1120{
1121 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1122 if (para)
1123 {
1124 long newPos = para->GetRange().GetStart() - 1;
c59f6793
JS
1125 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1126 if (!extendSel)
5d7836c4
JS
1127 SelectNone();
1128
1129 SetCaretPosition(newPos);
1130 PositionCaret();
1131 SetDefaultStyleToCursorStyle();
1132
c59f6793 1133 if (extendSel)
76bcd815 1134 Refresh(false);
5d7836c4
JS
1135 return true;
1136 }
1137
1138 return false;
1139}
1140
1141/// Move to the end of the line
1142bool wxRichTextCtrl::MoveToLineEnd(int flags)
1143{
1144 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1145
1146 if (line)
1147 {
1e967276
JS
1148 wxRichTextRange lineRange = line->GetAbsoluteRange();
1149 long newPos = lineRange.GetEnd();
c59f6793
JS
1150 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1151 if (!extendSel)
5d7836c4
JS
1152 SelectNone();
1153
1154 SetCaretPosition(newPos);
1155 PositionCaret();
1156 SetDefaultStyleToCursorStyle();
1157
c59f6793 1158 if (extendSel)
76bcd815 1159 Refresh(false);
5d7836c4
JS
1160 return true;
1161 }
1162
1163 return false;
1164}
1165
1166/// Move to the start of the line
1167bool wxRichTextCtrl::MoveToLineStart(int flags)
1168{
1169 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1170 if (line)
1171 {
1e967276
JS
1172 wxRichTextRange lineRange = line->GetAbsoluteRange();
1173 long newPos = lineRange.GetStart()-1;
5d7836c4 1174
c59f6793
JS
1175 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1176 if (!extendSel)
5d7836c4
JS
1177 SelectNone();
1178
1179 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line);
1180
1e967276 1181 SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart());
5d7836c4
JS
1182 PositionCaret();
1183 SetDefaultStyleToCursorStyle();
1184
c59f6793 1185 if (extendSel)
76bcd815 1186 Refresh(false);
5d7836c4
JS
1187 return true;
1188 }
1189
1190 return false;
1191}
1192
1193/// Move to the start of the buffer
1194bool wxRichTextCtrl::MoveHome(int flags)
1195{
1196 if (m_caretPosition != -1)
1197 {
c59f6793
JS
1198 bool extendSel = ExtendSelection(m_caretPosition, -1, flags);
1199 if (!extendSel)
5d7836c4
JS
1200 SelectNone();
1201
1202 SetCaretPosition(-1);
1203 PositionCaret();
1204 SetDefaultStyleToCursorStyle();
1205
c59f6793 1206 if (extendSel)
76bcd815 1207 Refresh(false);
5d7836c4
JS
1208 return true;
1209 }
1210 else
7fe8059f 1211 return false;
5d7836c4
JS
1212}
1213
1214/// Move to the end of the buffer
1215bool wxRichTextCtrl::MoveEnd(int flags)
1216{
1217 long endPos = GetBuffer().GetRange().GetEnd()-1;
1218
1219 if (m_caretPosition != endPos)
1220 {
c59f6793
JS
1221 bool extendSel = ExtendSelection(m_caretPosition, endPos, flags);
1222 if (!extendSel)
5d7836c4
JS
1223 SelectNone();
1224
1225 SetCaretPosition(endPos);
1226 PositionCaret();
1227 SetDefaultStyleToCursorStyle();
1228
c59f6793 1229 if (extendSel)
76bcd815 1230 Refresh(false);
5d7836c4
JS
1231 return true;
1232 }
1233 else
7fe8059f 1234 return false;
5d7836c4
JS
1235}
1236
1237/// Move noPages pages up
1238bool wxRichTextCtrl::PageUp(int noPages, int flags)
1239{
1240 return PageDown(- noPages, flags);
1241}
1242
1243/// Move noPages pages down
1244bool wxRichTextCtrl::PageDown(int noPages, int flags)
1245{
1246 // Calculate which line occurs noPages * screen height further down.
1247 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1248 if (line)
1249 {
1250 wxSize clientSize = GetClientSize();
1251 int newY = line->GetAbsolutePosition().y + noPages*clientSize.y;
1252
1253 wxRichTextLine* newLine = GetBuffer().GetLineAtYPosition(newY);
1254 if (newLine)
1255 {
1e967276
JS
1256 wxRichTextRange lineRange = newLine->GetAbsoluteRange();
1257 long pos = lineRange.GetStart()-1;
5d7836c4
JS
1258 if (pos != m_caretPosition)
1259 {
1260 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine);
1261
c59f6793
JS
1262 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1263 if (!extendSel)
5d7836c4
JS
1264 SelectNone();
1265
1e967276 1266 SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart());
5d7836c4
JS
1267 PositionCaret();
1268 SetDefaultStyleToCursorStyle();
1269
c59f6793 1270 if (extendSel)
76bcd815 1271 Refresh(false);
5d7836c4
JS
1272 return true;
1273 }
1274 }
1275 }
1276
1277 return false;
1278}
1279
1280// Finds the caret position for the next word
1281long wxRichTextCtrl::FindNextWordPosition(int direction) const
7fe8059f 1282{
5d7836c4
JS
1283 long endPos = GetBuffer().GetRange().GetEnd();
1284
1285 if (direction > 0)
1286 {
1287 long i = m_caretPosition+1+direction; // +1 for conversion to character pos
7fe8059f 1288
5d7836c4
JS
1289 // First skip current text to space
1290 while (i < endPos && i > -1)
1291 {
1292 // i is in character, not caret positions
1293 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
7fe8059f 1294 if (text != wxT(" ") && !text.empty())
5d7836c4
JS
1295 i += direction;
1296 else
1297 {
1298 break;
1299 }
1300 }
1301 while (i < endPos && i > -1)
1302 {
1303 // i is in character, not caret positions
1304 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
7fe8059f 1305 if (text.empty()) // End of paragraph, or maybe an image
5d7836c4 1306 return wxMax(-1, i - 1);
7fe8059f 1307 else if (text == wxT(" ") || text.empty())
5d7836c4
JS
1308 i += direction;
1309 else
1310 {
1311 // Convert to caret position
1312 return wxMax(-1, i - 1);
1313 }
1314 }
1315 if (i >= endPos)
1316 return endPos-1;
1317 return i-1;
1318 }
1319 else
1320 {
1321 long i = m_caretPosition;
1322
1323 // First skip white space
1324 while (i < endPos && i > -1)
1325 {
1326 // i is in character, not caret positions
1327 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
7fe8059f 1328 if (text.empty()) // End of paragraph, or maybe an image
5d7836c4 1329 break;
7fe8059f 1330 else if (text == wxT(" ") || text.empty())
5d7836c4
JS
1331 i += direction;
1332 else
1333 break;
1334 }
1335 // Next skip current text to space
1336 while (i < endPos && i > -1)
1337 {
1338 // i is in character, not caret positions
1339 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
7fe8059f 1340 if (text != wxT(" ") /* && !text.empty() */)
5d7836c4
JS
1341 i += direction;
1342 else
1343 {
1344 return i;
5d7836c4
JS
1345 }
1346 }
1347 if (i < -1)
1348 return -1;
1349 return i;
1350 }
1351}
1352
1353/// Move n words left
1354bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags)
1355{
1356 long pos = FindNextWordPosition(-1);
1357 if (pos != m_caretPosition)
1358 {
1359 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
7fe8059f 1360
c59f6793
JS
1361 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1362 if (!extendSel)
5d7836c4 1363 SelectNone();
7fe8059f 1364
5d7836c4
JS
1365 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1366 PositionCaret();
1367 SetDefaultStyleToCursorStyle();
1368
c59f6793 1369 if (extendSel)
76bcd815 1370 Refresh(false);
5d7836c4
JS
1371 return true;
1372 }
7fe8059f 1373
5d7836c4
JS
1374 return false;
1375}
1376
1377/// Move n words right
1378bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags)
1379{
1380 long pos = FindNextWordPosition(1);
1381 if (pos != m_caretPosition)
1382 {
1383 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
7fe8059f 1384
c59f6793
JS
1385 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1386 if (!extendSel)
5d7836c4 1387 SelectNone();
7fe8059f 1388
5d7836c4
JS
1389 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1390 PositionCaret();
1391 SetDefaultStyleToCursorStyle();
1392
c59f6793 1393 if (extendSel)
76bcd815 1394 Refresh(false);
5d7836c4
JS
1395 return true;
1396 }
7fe8059f 1397
5d7836c4
JS
1398 return false;
1399}
1400
1401/// Sizing
1402void wxRichTextCtrl::OnSize(wxSizeEvent& event)
1403{
4d551ad5
JS
1404 // Only do sizing optimization for large buffers
1405 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold)
1406 {
1407 m_fullLayoutRequired = true;
1408 m_fullLayoutTime = wxGetLocalTimeMillis();
1409 m_fullLayoutSavedPosition = GetFirstVisiblePosition();
2f36e8dc 1410 LayoutContent(true /* onlyVisibleRect */);
4d551ad5
JS
1411 }
1412 else
1413 GetBuffer().Invalidate(wxRICHTEXT_ALL);
7fe8059f 1414
5d7836c4
JS
1415 RecreateBuffer();
1416
1417 event.Skip();
1418}
1419
4d551ad5
JS
1420
1421/// Idle-time processing
1422void wxRichTextCtrl::OnIdle(wxIdleEvent& event)
1423{
1424 const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL;
1425
1426 if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval)))
1427 {
1428 m_fullLayoutRequired = false;
1429 m_fullLayoutTime = 0;
1430 GetBuffer().Invalidate(wxRICHTEXT_ALL);
1431 ShowPosition(m_fullLayoutSavedPosition);
76bcd815 1432 Refresh(false);
4d551ad5 1433 }
ab14c7aa
JS
1434
1435 if (m_caretPositionForDefaultStyle != -2)
1436 {
1437 // If the caret position has changed, no longer reflect the default style
1438 // in the UI.
1439 if (GetCaretPosition() != m_caretPositionForDefaultStyle)
1440 m_caretPositionForDefaultStyle = -2;
1441 }
1442
4d551ad5
JS
1443 event.Skip();
1444}
1445
c59f6793
JS
1446/// Scrolling
1447void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
1448{
1449 // Not used
1450 event.Skip();
1451}
1452
5d7836c4 1453/// Set up scrollbars, e.g. after a resize
169adfa9 1454void wxRichTextCtrl::SetupScrollbars(bool atTop)
5d7836c4
JS
1455{
1456 if (m_freezeCount)
1457 return;
1458
1459 if (GetBuffer().IsEmpty())
1460 {
1461 SetScrollbars(0, 0, 0, 0, 0, 0);
1462 return;
1463 }
1464
1465 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1466 // of pixels. See e.g. wxVScrolledWindow for ideas.
3956d02a 1467 int pixelsPerUnit = 5;
5d7836c4
JS
1468 wxSize clientSize = GetClientSize();
1469
1470 int maxHeight = GetBuffer().GetCachedSize().y;
1471
3956d02a
JS
1472 // Round up so we have at least maxHeight pixels
1473 int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5);
5d7836c4 1474
169adfa9
JS
1475 int startX = 0, startY = 0;
1476 if (!atTop)
1477 GetViewStart(& startX, & startY);
7fe8059f 1478
5d7836c4 1479 int maxPositionX = 0; // wxMax(sz.x - clientSize.x, 0);
3956d02a 1480 int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5);
7fe8059f 1481
5d7836c4
JS
1482 // Move to previous scroll position if
1483 // possible
1484 SetScrollbars(0, pixelsPerUnit,
1485 0, unitsY,
1486 wxMin(maxPositionX, startX), wxMin(maxPositionY, startY));
1487}
1488
1489/// Paint the background
1490void wxRichTextCtrl::PaintBackground(wxDC& dc)
1491{
1492 wxColour backgroundColour = GetBackgroundColour();
1493 if (!backgroundColour.Ok())
1494 backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1495
1496 // Clear the background
1497 dc.SetBrush(wxBrush(backgroundColour));
1498 dc.SetPen(*wxTRANSPARENT_PEN);
7fe8059f 1499 wxRect windowRect(GetClientSize());
5d7836c4
JS
1500 windowRect.x -= 2; windowRect.y -= 2;
1501 windowRect.width += 4; windowRect.height += 4;
7fe8059f 1502
5d7836c4
JS
1503 // We need to shift the rectangle to take into account
1504 // scrolling. Converting device to logical coordinates.
1505 CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y);
1506 dc.DrawRectangle(windowRect);
1507}
1508
1509/// Recreate buffer bitmap if necessary
1510bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
1511{
1512 wxSize sz = size;
1513 if (sz == wxDefaultSize)
1514 sz = GetClientSize();
7fe8059f 1515
5d7836c4
JS
1516 if (sz.x < 1 || sz.y < 1)
1517 return false;
7fe8059f 1518
5d7836c4
JS
1519 if (!m_bufferBitmap.Ok() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y)
1520 m_bufferBitmap = wxBitmap(sz.x, sz.y);
1521 return m_bufferBitmap.Ok();
1522}
1523
1524// ----------------------------------------------------------------------------
1525// file IO functions
1526// ----------------------------------------------------------------------------
1527
3306aec1
JS
1528#if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
1529bool wxRichTextCtrl::LoadFile(const wxString& filename, int fileType)
5d7836c4 1530{
fe5aa22c 1531 return DoLoadFile(filename, fileType);
3306aec1
JS
1532}
1533
1534bool wxRichTextCtrl::SaveFile(const wxString& filename, int fileType)
1535{
1536 wxString filenameToUse = filename.empty() ? m_filename : filename;
1537 if ( filenameToUse.empty() )
1538 {
1539 // what kind of message to give? is it an error or a program bug?
1540 wxLogDebug(wxT("Can't save textctrl to file without filename."));
1541
1542 return false;
1543 }
1544
1545 return DoSaveFile(filenameToUse, fileType);
1546}
1547#endif
1548
1549bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
1550{
1551 bool success = GetBuffer().LoadFile(filename, fileType);
5d7836c4
JS
1552 if (success)
1553 m_filename = filename;
1554
1555 DiscardEdits();
1556 SetInsertionPoint(0);
2f36e8dc 1557 LayoutContent();
5d7836c4 1558 PositionCaret();
169adfa9 1559 SetupScrollbars(true);
76bcd815 1560 Refresh(false);
5d7836c4
JS
1561 SendUpdateEvent();
1562
1563 if (success)
1564 return true;
1565 else
1566 {
1567 wxLogError(_("File couldn't be loaded."));
7fe8059f 1568
5d7836c4
JS
1569 return false;
1570 }
1571}
1572
3306aec1 1573bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
5d7836c4 1574{
3306aec1 1575 if (GetBuffer().SaveFile(filename, fileType))
5d7836c4 1576 {
3306aec1 1577 m_filename = filename;
5d7836c4
JS
1578
1579 DiscardEdits();
1580
1581 return true;
5d7836c4
JS
1582 }
1583
1584 wxLogError(_("The text couldn't be saved."));
1585
1586 return false;
1587}
1588
1589// ----------------------------------------------------------------------------
1590// wxRichTextCtrl specific functionality
1591// ----------------------------------------------------------------------------
1592
1593/// Add a new paragraph of text to the end of the buffer
1594wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
1595{
1596 return GetBuffer().AddParagraph(text);
1597}
1598
1599/// Add an image
1600wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
1601{
1602 return GetBuffer().AddImage(image);
1603}
1604
1605// ----------------------------------------------------------------------------
1606// selection and ranges
1607// ----------------------------------------------------------------------------
1608
1609void wxRichTextCtrl::SelectAll()
1610{
96c9f0f6 1611 SetSelection(0, GetLastPosition()+1);
5d7836c4
JS
1612 m_selectionAnchor = -1;
1613}
1614
1615/// Select none
1616void wxRichTextCtrl::SelectNone()
1617{
c59f6793
JS
1618 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
1619 SetSelection(-2, -2);
5d7836c4
JS
1620 m_selectionAnchor = -2;
1621}
1622
1623wxString wxRichTextCtrl::GetStringSelection() const
1624{
1625 long from, to;
1626 GetSelection(&from, &to);
1627
1628 return GetRange(from, to);
1629}
1630
1631// do the window-specific processing after processing the update event
a3a4105d 1632#if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
5d7836c4
JS
1633void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event)
1634{
a3a4105d
VZ
1635 // call inherited
1636 wxWindowBase::DoUpdateWindowUI(event);
5d7836c4 1637
a3a4105d 1638 // update text
5d7836c4
JS
1639 if ( event.GetSetText() )
1640 {
1641 if ( event.GetText() != GetValue() )
1642 SetValue(event.GetText());
1643 }
1644}
a3a4105d 1645#endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
5d7836c4
JS
1646
1647// ----------------------------------------------------------------------------
1648// hit testing
1649// ----------------------------------------------------------------------------
1650
1651wxTextCtrlHitTestResult
1652wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
1653{
1654 // implement in terms of the other overload as the native ports typically
1655 // can get the position and not (x, y) pair directly (although wxUniv
1656 // directly gets x and y -- and so overrides this method as well)
1657 long pos;
1658 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
1659
1660 if ( rc != wxTE_HT_UNKNOWN )
1661 {
1662 PositionToXY(pos, x, y);
1663 }
1664
1665 return rc;
1666}
1667
1668wxTextCtrlHitTestResult
1669wxRichTextCtrl::HitTest(const wxPoint& pt,
1670 long * pos) const
1671{
1672 wxClientDC dc((wxRichTextCtrl*) this);
1673 ((wxRichTextCtrl*)this)->PrepareDC(dc);
61399247 1674
563d25ba
JS
1675 // Buffer uses logical position (relative to start of buffer)
1676 // so convert
1677 wxPoint pt2 = GetLogicalPoint(pt);
5d7836c4 1678
563d25ba 1679 int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt2, *pos);
41e155b4
WS
1680
1681 switch ( hit )
1682 {
1683 case wxRICHTEXT_HITTEST_BEFORE:
1684 return wxTE_HT_BEFORE;
1685
1686 case wxRICHTEXT_HITTEST_AFTER:
1687 return wxTE_HT_BEYOND;
1688
1689 case wxRICHTEXT_HITTEST_ON:
1690 return wxTE_HT_ON_TEXT;
1691 }
1692
1693 return wxTE_HT_UNKNOWN;
5d7836c4
JS
1694}
1695
1696// ----------------------------------------------------------------------------
1697// set/get the controls text
1698// ----------------------------------------------------------------------------
1699
1700wxString wxRichTextCtrl::GetValue() const
1701{
1702 return GetBuffer().GetText();
1703}
1704
1705wxString wxRichTextCtrl::GetRange(long from, long to) const
1706{
96c9f0f6
JS
1707 // Public API for range is different from internals
1708 return GetBuffer().GetTextForRange(wxRichTextRange(from, to-1));
5d7836c4
JS
1709}
1710
1711void wxRichTextCtrl::SetValue(const wxString& value)
1712{
1713 Clear();
1714
1715 // if the text is long enough, it's faster to just set it instead of first
1716 // comparing it with the old one (chances are that it will be different
1717 // anyhow, this comparison is there to avoid flicker for small single-line
1718 // edit controls mostly)
1719 if ( (value.length() > 0x400) || (value != GetValue()) )
1720 {
1721 DoWriteText(value, false /* not selection only */);
1722
1723 // for compatibility, don't move the cursor when doing SetValue()
1724 SetInsertionPoint(0);
1725 }
1726 else // same text
1727 {
1728 // still send an event for consistency
1729 SendUpdateEvent();
1730 }
1731
1732 // we should reset the modified flag even if the value didn't really change
1733
1734 // mark the control as being not dirty - we changed its text, not the
1735 // user
1736 DiscardEdits();
1737}
1738
1739void wxRichTextCtrl::WriteText(const wxString& value)
1740{
1741 DoWriteText(value);
1742}
1743
1744void wxRichTextCtrl::DoWriteText(const wxString& value, bool WXUNUSED(selectionOnly))
1745{
64c47f48 1746 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Unix);
fe5aa22c 1747
64c47f48 1748 GetBuffer().InsertTextWithUndo(m_caretPosition+1, valueDos, this);
5d7836c4
JS
1749}
1750
1751void wxRichTextCtrl::AppendText(const wxString& text)
1752{
1753 SetInsertionPointEnd();
1754
1755 WriteText(text);
1756}
1757
1758/// Write an image at the current insertion point
1759bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType)
1760{
1761 wxRichTextImageBlock imageBlock;
1762
1763 wxImage image2 = image;
1764 if (imageBlock.MakeImageBlock(image2, bitmapType))
1765 return WriteImage(imageBlock);
41e155b4
WS
1766
1767 return false;
5d7836c4
JS
1768}
1769
1770bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType)
1771{
1772 wxRichTextImageBlock imageBlock;
1773
1774 wxImage image;
1775 if (imageBlock.MakeImageBlock(filename, bitmapType, image, false))
1776 return WriteImage(imageBlock);
41e155b4
WS
1777
1778 return false;
5d7836c4
JS
1779}
1780
1781bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock)
1782{
1783 return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this);
1784}
1785
1786bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType)
1787{
1788 if (bitmap.Ok())
1789 {
1790 wxRichTextImageBlock imageBlock;
7fe8059f 1791
5d7836c4
JS
1792 wxImage image = bitmap.ConvertToImage();
1793 if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType))
1794 return WriteImage(imageBlock);
5d7836c4 1795 }
41e155b4 1796
5d7836c4
JS
1797 return false;
1798}
1799
1800/// Insert a newline (actually paragraph) at the current insertion point.
1801bool wxRichTextCtrl::Newline()
1802{
1803 return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this);
1804}
1805
1806
1807// ----------------------------------------------------------------------------
1808// Clipboard operations
1809// ----------------------------------------------------------------------------
1810
1811void wxRichTextCtrl::Copy()
1812{
1813 if (CanCopy())
1814 {
fe5aa22c 1815 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
1816 GetBuffer().CopyToClipboard(range);
1817 }
1818}
1819
1820void wxRichTextCtrl::Cut()
1821{
1822 if (CanCut())
1823 {
fe5aa22c 1824 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
1825 GetBuffer().CopyToClipboard(range);
1826
1827 DeleteSelectedContent();
2f36e8dc 1828 LayoutContent();
76bcd815 1829 Refresh(false);
5d7836c4
JS
1830 }
1831}
1832
1833void wxRichTextCtrl::Paste()
1834{
1835 if (CanPaste())
1836 {
1837 BeginBatchUndo(_("Paste"));
7fe8059f 1838
5d7836c4
JS
1839 long newPos = m_caretPosition;
1840 DeleteSelectedContent(& newPos);
7fe8059f 1841
5d7836c4
JS
1842 GetBuffer().PasteFromClipboard(newPos);
1843
1844 EndBatchUndo();
1845 }
1846}
1847
1848void wxRichTextCtrl::DeleteSelection()
1849{
1850 if (CanDeleteSelection())
1851 {
1852 DeleteSelectedContent();
1853 }
1854}
1855
1856bool wxRichTextCtrl::HasSelection() const
1857{
1858 return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2;
1859}
1860
1861bool wxRichTextCtrl::CanCopy() const
1862{
1863 // Can copy if there's a selection
1864 return HasSelection();
1865}
1866
1867bool wxRichTextCtrl::CanCut() const
1868{
1869 return HasSelection() && IsEditable();
1870}
1871
1872bool wxRichTextCtrl::CanPaste() const
1873{
1874 if ( !IsEditable() )
1875 return false;
1876
1877 return GetBuffer().CanPasteFromClipboard();
1878}
1879
1880bool wxRichTextCtrl::CanDeleteSelection() const
1881{
1882 return HasSelection() && IsEditable();
1883}
1884
1885
1886// ----------------------------------------------------------------------------
1887// Accessors
1888// ----------------------------------------------------------------------------
1889
1890void wxRichTextCtrl::SetEditable(bool editable)
1891{
1892 m_editable = editable;
1893}
1894
1895void wxRichTextCtrl::SetInsertionPoint(long pos)
1896{
1897 SelectNone();
1898
1899 m_caretPosition = pos - 1;
1900}
1901
1902void wxRichTextCtrl::SetInsertionPointEnd()
1903{
1904 long pos = GetLastPosition();
1905 SetInsertionPoint(pos);
1906}
1907
1908long wxRichTextCtrl::GetInsertionPoint() const
1909{
1910 return m_caretPosition+1;
1911}
1912
1913wxTextPos wxRichTextCtrl::GetLastPosition() const
1914{
1915 return GetBuffer().GetRange().GetEnd();
1916}
1917
1918// If the return values from and to are the same, there is no
1919// selection.
1920void wxRichTextCtrl::GetSelection(long* from, long* to) const
1921{
1922 *from = m_selectionRange.GetStart();
1923 *to = m_selectionRange.GetEnd();
96c9f0f6
JS
1924 if ((*to) != -1 && (*to) != -2)
1925 (*to) ++;
5d7836c4
JS
1926}
1927
1928bool wxRichTextCtrl::IsEditable() const
1929{
1930 return m_editable;
1931}
1932
1933// ----------------------------------------------------------------------------
1934// selection
1935// ----------------------------------------------------------------------------
1936
1937void wxRichTextCtrl::SetSelection(long from, long to)
1938{
1939 // if from and to are both -1, it means (in wxWidgets) that all text should
1940 // be selected.
1941 if ( (from == -1) && (to == -1) )
1942 {
1943 from = 0;
96c9f0f6 1944 to = GetLastPosition()+1;
5d7836c4
JS
1945 }
1946
1947 DoSetSelection(from, to);
1948}
1949
1950void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))
1951{
1952 m_selectionAnchor = from;
96c9f0f6 1953 m_selectionRange.SetRange(from, to-1);
76bcd815 1954 Refresh(false);
5d7836c4
JS
1955 PositionCaret();
1956}
1957
1958// ----------------------------------------------------------------------------
1959// Editing
1960// ----------------------------------------------------------------------------
1961
1962void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to), const wxString& value)
1963{
1964 BeginBatchUndo(_("Replace"));
1965
1966 DeleteSelectedContent();
1967
1968 DoWriteText(value, true /* selection only */);
1969
1970 EndBatchUndo();
1971}
1972
1973void wxRichTextCtrl::Remove(long from, long to)
1974{
1975 SelectNone();
1976
1977 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to),
1978 m_caretPosition, // Current caret position
1979 from, // New caret position
1980 this);
1981
2f36e8dc 1982 LayoutContent();
5d7836c4 1983 if (!IsFrozen())
76bcd815 1984 Refresh(false);
5d7836c4
JS
1985}
1986
1987bool wxRichTextCtrl::IsModified() const
1988{
1989 return m_buffer.IsModified();
1990}
1991
1992void wxRichTextCtrl::MarkDirty()
1993{
1994 m_buffer.Modify(true);
1995}
1996
1997void wxRichTextCtrl::DiscardEdits()
1998{
ab14c7aa 1999 m_caretPositionForDefaultStyle = -2;
5d7836c4
JS
2000 m_buffer.Modify(false);
2001 m_buffer.GetCommandProcessor()->ClearCommands();
2002}
2003
2004int wxRichTextCtrl::GetNumberOfLines() const
2005{
2006 return GetBuffer().GetParagraphCount();
2007}
2008
2009// ----------------------------------------------------------------------------
2010// Positions <-> coords
2011// ----------------------------------------------------------------------------
2012
2013long wxRichTextCtrl::XYToPosition(long x, long y) const
2014{
2015 return GetBuffer().XYToPosition(x, y);
2016}
2017
2018bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
2019{
2020 return GetBuffer().PositionToXY(pos, x, y);
2021}
2022
2023// ----------------------------------------------------------------------------
2024//
2025// ----------------------------------------------------------------------------
2026
2027void wxRichTextCtrl::ShowPosition(long pos)
2028{
2029 if (!IsPositionVisible(pos))
2030 ScrollIntoView(pos-1, WXK_DOWN);
2031}
2032
2033int wxRichTextCtrl::GetLineLength(long lineNo) const
2034{
2035 return GetBuffer().GetParagraphLength(lineNo);
2036}
2037
2038wxString wxRichTextCtrl::GetLineText(long lineNo) const
2039{
2040 return GetBuffer().GetParagraphText(lineNo);
2041}
2042
2043// ----------------------------------------------------------------------------
2044// Undo/redo
2045// ----------------------------------------------------------------------------
2046
2047void wxRichTextCtrl::Undo()
2048{
2049 if (CanUndo())
2050 {
2051 GetCommandProcessor()->Undo();
2052 }
2053}
2054
2055void wxRichTextCtrl::Redo()
2056{
2057 if (CanRedo())
2058 {
2059 GetCommandProcessor()->Redo();
2060 }
2061}
2062
2063bool wxRichTextCtrl::CanUndo() const
2064{
2065 return GetCommandProcessor()->CanUndo();
2066}
2067
2068bool wxRichTextCtrl::CanRedo() const
2069{
2070 return GetCommandProcessor()->CanRedo();
2071}
2072
2073// ----------------------------------------------------------------------------
563d25ba 2074// implementation details
5d7836c4
JS
2075// ----------------------------------------------------------------------------
2076
563d25ba 2077void wxRichTextCtrl::Command(wxCommandEvent& event)
5d7836c4
JS
2078{
2079 SetValue(event.GetString());
2080 GetEventHandler()->ProcessEvent(event);
2081}
2082
2083void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event)
2084{
2085 // By default, load the first file into the text window.
2086 if (event.GetNumberOfFiles() > 0)
2087 {
2088 LoadFile(event.GetFiles()[0]);
2089 }
2090}
2091
2092// ----------------------------------------------------------------------------
2093// text control event processing
2094// ----------------------------------------------------------------------------
2095
2096bool wxRichTextCtrl::SendUpdateEvent()
2097{
2098 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
2099 InitCommandEvent(event);
2100
2101 return GetEventHandler()->ProcessEvent(event);
2102}
2103
2104void wxRichTextCtrl::InitCommandEvent(wxCommandEvent& event) const
2105{
2106 event.SetEventObject((wxControlBase *)this); // const_cast
2107
2108 switch ( m_clientDataType )
2109 {
2110 case wxClientData_Void:
2111 event.SetClientData(GetClientData());
2112 break;
2113
2114 case wxClientData_Object:
2115 event.SetClientObject(GetClientObject());
2116 break;
2117
2118 case wxClientData_None:
2119 // nothing to do
2120 ;
2121 }
2122}
2123
2124
2125wxSize wxRichTextCtrl::DoGetBestSize() const
2126{
2127 return wxSize(10, 10);
2128}
2129
2130// ----------------------------------------------------------------------------
2131// standard handlers for standard edit menu events
2132// ----------------------------------------------------------------------------
2133
2134void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2135{
2136 Cut();
2137}
2138
2139void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
2140{
2141 DeleteSelection();
2142}
2143
2144void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2145{
2146 Copy();
2147}
2148
2149void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2150{
2151 Paste();
2152}
2153
2154void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2155{
2156 Undo();
2157}
2158
2159void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2160{
2161 Redo();
2162}
2163
2164void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2165{
2166 event.Enable( CanCut() );
2167}
2168
2169void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2170{
2171 event.Enable( CanCopy() );
2172}
2173
2174void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
2175{
2176 event.Enable( CanDeleteSelection() );
2177}
2178
2179void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2180{
2181 event.Enable( CanPaste() );
2182}
2183
2184void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2185{
2186 event.Enable( CanUndo() );
2187 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2188}
2189
2190void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2191{
2192 event.Enable( CanRedo() );
2193 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2194}
2195
2196void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
2197{
2198 SelectAll();
2199}
2200
2201void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
2202{
2203 event.Enable(GetLastPosition() > 0);
2204}
2205
2206void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& WXUNUSED(event))
2207{
2208 if (!m_contextMenu)
2209 {
2210 m_contextMenu = new wxMenu;
2211 m_contextMenu->Append(wxID_UNDO, _("&Undo"));
2212 m_contextMenu->Append(wxID_REDO, _("&Redo"));
2213 m_contextMenu->AppendSeparator();
2214 m_contextMenu->Append(wxID_CUT, _("Cu&t"));
2215 m_contextMenu->Append(wxID_COPY, _("&Copy"));
2216 m_contextMenu->Append(wxID_PASTE, _("&Paste"));
2217 m_contextMenu->Append(wxID_CLEAR, _("&Delete"));
2218 m_contextMenu->AppendSeparator();
2219 m_contextMenu->Append(wxID_SELECTALL, _("Select &All"));
2220 }
2221 PopupMenu(m_contextMenu);
2222 return;
2223}
2224
2225bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style)
2226{
96c9f0f6 2227 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), style);
5d7836c4
JS
2228}
2229
27e20452
JS
2230bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
2231{
96c9f0f6 2232 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttrEx(style));
27e20452
JS
2233}
2234
5d7836c4
JS
2235bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style)
2236{
96c9f0f6 2237 return GetBuffer().SetStyle(range.ToInternal(), style);
5d7836c4
JS
2238}
2239
2240bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style)
2241{
2242 return GetBuffer().SetDefaultStyle(style);
2243}
2244
27e20452
JS
2245bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2246{
2247 return GetBuffer().SetDefaultStyle(wxTextAttrEx(style));
2248}
2249
5d7836c4
JS
2250const wxTextAttrEx& wxRichTextCtrl::GetDefaultStyleEx() const
2251{
2252 return GetBuffer().GetDefaultStyle();
2253}
2254
27e20452
JS
2255const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const
2256{
61399247 2257 return GetBuffer().GetDefaultStyle();
27e20452
JS
2258}
2259
3966a9f4 2260bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style)
27e20452 2261{
fe5aa22c 2262 wxTextAttrEx attr(style);
27e20452
JS
2263 if (GetBuffer().GetStyle(position, attr))
2264 {
2265 style = attr;
2266 return true;
2267 }
2268 else
2269 return false;
2270}
2271
3966a9f4 2272bool wxRichTextCtrl::GetStyle(long position, wxTextAttrEx& style)
5d7836c4
JS
2273{
2274 return GetBuffer().GetStyle(position, style);
2275}
2276
3966a9f4 2277bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style)
5d7836c4
JS
2278{
2279 return GetBuffer().GetStyle(position, style);
2280}
2281
fe5aa22c
JS
2282/// Get the content (uncombined) attributes for this position.
2283
2284bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttr& style)
2285{
2286 wxTextAttrEx attr(style);
2287 if (GetBuffer().GetUncombinedStyle(position, attr))
2288 {
2289 style = attr;
2290 return true;
2291 }
2292 else
2293 return false;
2294}
2295
2296bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttrEx& style)
2297{
2298 return GetBuffer().GetUncombinedStyle(position, style);
2299}
2300
2301bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style)
2302{
2303 return GetBuffer().GetUncombinedStyle(position, style);
2304}
2305
5d7836c4
JS
2306/// Set font, and also the buffer attributes
2307bool wxRichTextCtrl::SetFont(const wxFont& font)
2308{
2309#if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2310 wxControl::SetFont(font);
2311#else
2312 wxScrolledWindow::SetFont(font);
2313#endif
2314
2315 wxTextAttrEx attr = GetBuffer().GetAttributes();
2316 attr.SetFont(font);
2317 GetBuffer().SetBasicStyle(attr);
fe5aa22c
JS
2318
2319#if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE
2320 // Don't set the default style, since it will be inherited from
2321 // the basic style.
5d7836c4 2322 GetBuffer().SetDefaultStyle(attr);
fe5aa22c
JS
2323#endif
2324
2325 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2326 Refresh(false);
5d7836c4
JS
2327
2328 return true;
2329}
2330
4d551ad5
JS
2331/// Transform logical to physical
2332wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const
5d7836c4
JS
2333{
2334 wxPoint pt;
2335 CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y);
2336
2337 return pt;
2338}
2339
2340/// Transform physical to logical
4d551ad5 2341wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
5d7836c4
JS
2342{
2343 wxPoint pt;
2344 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
2345
2346 return pt;
2347}
2348
2349/// Position the caret
2350void wxRichTextCtrl::PositionCaret()
2351{
c59f6793
JS
2352 if (!GetCaret())
2353 return;
2354
2355 //wxLogDebug(wxT("PositionCaret"));
2356
5d7836c4
JS
2357 wxRect caretRect;
2358 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect))
2359 {
2360 wxPoint originalPt = caretRect.GetPosition();
2361 wxPoint pt = GetPhysicalPoint(originalPt);
c59f6793
JS
2362 if (GetCaret()->GetPosition() != pt)
2363 {
2364 GetCaret()->Move(pt);
2365 GetCaret()->SetSize(caretRect.GetSize());
2366 }
5d7836c4
JS
2367 }
2368}
2369
2370/// Get the caret height and position for the given character position
2371bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect)
2372{
2373 wxClientDC dc(this);
2374 dc.SetFont(GetFont());
7fe8059f 2375
5d7836c4
JS
2376 PrepareDC(dc);
2377
2378 wxPoint pt;
2379 int height = 0;
2380
2381 if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart))
2382 {
2383 rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height));
2384 return true;
2385 }
41e155b4
WS
2386
2387 return false;
5d7836c4
JS
2388}
2389
2390/// Gets the line for the visible caret position. If the caret is
2391/// shown at the very end of the line, it means the next character is actually
2392/// on the following line. So let's get the line we're expecting to find
2393/// if this is the case.
2394wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
2395{
2396 wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true);
2397 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true);
2398 if (line)
2399 {
1e967276
JS
2400 wxRichTextRange lineRange = line->GetAbsoluteRange();
2401 if (caretPosition == lineRange.GetStart()-1 &&
2402 (para->GetRange().GetStart() != lineRange.GetStart()))
5d7836c4
JS
2403 {
2404 if (!m_caretAtLineStart)
2405 line = GetBuffer().GetLineAtPosition(caretPosition-1, true);
2406 }
2407 }
2408 return line;
2409}
2410
2411
2412/// Move the caret to the given character position
2413bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart)
2414{
2415 if (GetBuffer().GetDirty())
2f36e8dc 2416 LayoutContent();
5d7836c4
JS
2417
2418 if (pos <= GetBuffer().GetRange().GetEnd())
2419 {
2420 SetCaretPosition(pos, showAtLineStart);
2421
2422 PositionCaret();
2423
2424 return true;
2425 }
2426 else
2427 return false;
2428}
2429
2430/// Layout the buffer: which we must do before certain operations, such as
2431/// setting the caret position.
2f36e8dc 2432bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
5d7836c4 2433{
4d551ad5 2434 if (GetBuffer().GetDirty() || onlyVisibleRect)
1e967276
JS
2435 {
2436 wxRect availableSpace(GetClientSize());
2437 if (availableSpace.width == 0)
2438 availableSpace.width = 10;
2439 if (availableSpace.height == 0)
2440 availableSpace.height = 10;
4d551ad5
JS
2441
2442 int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT;
2443 if (onlyVisibleRect)
2444 {
2445 flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
2446 availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2447 }
41e155b4 2448
1e967276
JS
2449 wxClientDC dc(this);
2450 dc.SetFont(GetFont());
41e155b4 2451
1e967276 2452 PrepareDC(dc);
41e155b4 2453
1e967276
JS
2454 GetBuffer().Defragment();
2455 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
4d551ad5 2456 GetBuffer().Layout(dc, availableSpace, flags);
1e967276 2457 GetBuffer().SetDirty(false);
41e155b4 2458
1e967276
JS
2459 if (!IsFrozen())
2460 SetupScrollbars();
2461 }
5d7836c4
JS
2462
2463 return true;
2464}
2465
2466/// Is all of the selection bold?
3966a9f4 2467bool wxRichTextCtrl::IsSelectionBold()
5d7836c4
JS
2468{
2469 if (HasSelection())
2470 {
2471 wxRichTextAttr attr;
96c9f0f6 2472 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
2473 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2474 attr.SetFontWeight(wxBOLD);
7fe8059f 2475
5d7836c4
JS
2476 return HasCharacterAttributes(range, attr);
2477 }
2478 else
2479 {
2480 // If no selection, then we need to combine current style with default style
2481 // to see what the effect would be if we started typing.
2482 wxRichTextAttr attr;
2483 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
ab14c7aa
JS
2484
2485 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2486 if (GetStyle(pos, attr))
5d7836c4 2487 {
ab14c7aa
JS
2488 if (IsDefaultStyleShowing())
2489 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
5d7836c4
JS
2490 return attr.GetFontWeight() == wxBOLD;
2491 }
2492 }
2493 return false;
2494}
2495
2496/// Is all of the selection italics?
3966a9f4 2497bool wxRichTextCtrl::IsSelectionItalics()
5d7836c4
JS
2498{
2499 if (HasSelection())
2500 {
96c9f0f6 2501 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
2502 wxRichTextAttr attr;
2503 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2504 attr.SetFontStyle(wxITALIC);
7fe8059f 2505
5d7836c4
JS
2506 return HasCharacterAttributes(range, attr);
2507 }
2508 else
2509 {
2510 // If no selection, then we need to combine current style with default style
2511 // to see what the effect would be if we started typing.
2512 wxRichTextAttr attr;
2513 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
ab14c7aa
JS
2514
2515 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2516 if (GetStyle(pos, attr))
5d7836c4 2517 {
ab14c7aa
JS
2518 if (IsDefaultStyleShowing())
2519 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
5d7836c4
JS
2520 return attr.GetFontStyle() == wxITALIC;
2521 }
2522 }
2523 return false;
2524}
2525
2526/// Is all of the selection underlined?
3966a9f4 2527bool wxRichTextCtrl::IsSelectionUnderlined()
5d7836c4
JS
2528{
2529 if (HasSelection())
2530 {
96c9f0f6 2531 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
2532 wxRichTextAttr attr;
2533 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2534 attr.SetFontUnderlined(true);
7fe8059f 2535
5d7836c4
JS
2536 return HasCharacterAttributes(range, attr);
2537 }
2538 else
2539 {
2540 // If no selection, then we need to combine current style with default style
2541 // to see what the effect would be if we started typing.
2542 wxRichTextAttr attr;
2543 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
ab14c7aa 2544 long pos = GetAdjustedCaretPosition(GetCaretPosition());
fe5aa22c 2545
ab14c7aa 2546 if (GetStyle(pos, attr))
5d7836c4 2547 {
ab14c7aa
JS
2548 if (IsDefaultStyleShowing())
2549 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
5d7836c4
JS
2550 return attr.GetFontUnderlined();
2551 }
2552 }
2553 return false;
2554}
2555
2556/// Apply bold to the selection
2557bool wxRichTextCtrl::ApplyBoldToSelection()
2558{
2559 wxRichTextAttr attr;
2560 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2561 attr.SetFontWeight(IsSelectionBold() ? wxNORMAL : wxBOLD);
2562
2563 if (HasSelection())
2564 return SetStyle(GetSelectionRange(), attr);
2565 else
ab14c7aa 2566 SetAndShowDefaultStyle(attr);
5d7836c4
JS
2567 return true;
2568}
2569
2570/// Apply italic to the selection
2571bool wxRichTextCtrl::ApplyItalicToSelection()
2572{
2573 wxRichTextAttr attr;
2574 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2575 attr.SetFontStyle(IsSelectionItalics() ? wxNORMAL : wxITALIC);
2576
2577 if (HasSelection())
2578 return SetStyle(GetSelectionRange(), attr);
2579 else
ab14c7aa 2580 SetAndShowDefaultStyle(attr);
5d7836c4
JS
2581 return true;
2582}
2583
2584/// Apply underline to the selection
2585bool wxRichTextCtrl::ApplyUnderlineToSelection()
2586{
2587 wxRichTextAttr attr;
2588 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
4d551ad5 2589 attr.SetFontUnderlined(!IsSelectionUnderlined());
5d7836c4
JS
2590
2591 if (HasSelection())
2592 return SetStyle(GetSelectionRange(), attr);
2593 else
ab14c7aa 2594 SetAndShowDefaultStyle(attr);
5d7836c4
JS
2595 return true;
2596}
2597
2598/// Is all of the selection aligned according to the specified flag?
3966a9f4 2599bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
5d7836c4
JS
2600{
2601 if (HasSelection())
2602 {
96c9f0f6 2603 wxRichTextRange range = GetInternalSelectionRange();
5d7836c4
JS
2604 wxRichTextAttr attr;
2605 attr.SetAlignment(alignment);
7fe8059f 2606
5d7836c4
JS
2607 return HasParagraphAttributes(range, attr);
2608 }
2609 else
2610 {
2611 // If no selection, then we need to get information from the current paragraph.
2612 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2613 if (para)
2614 return para->GetAttributes().GetAlignment() == alignment;
2615 }
2616 return false;
2617}
2618
2619/// Apply alignment to the selection
2620bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
2621{
2622 wxRichTextAttr attr;
2623 attr.SetAlignment(alignment);
2624 if (HasSelection())
2625 return SetStyle(GetSelectionRange(), attr);
2626 else
2627 {
2628 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
2629 if (para)
96c9f0f6 2630 return SetStyle(para->GetRange().FromInternal(), attr);
5d7836c4
JS
2631 }
2632 return true;
2633}
2634
ab14c7aa
JS
2635/// Apply a named style to the selection
2636void wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
2637{
2638 // Flags are defined within each definition, so only certain
2639 // attributes are applied.
2640 wxRichTextAttr attr(def->GetStyle());
2641
2642 // Make sure the attr has the style name
2643 if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
2644 attr.SetParagraphStyleName(def->GetName());
2645 else
2646 attr.SetCharacterStyleName(def->GetName());
2647
2648 if (HasSelection())
2649 SetStyle(GetSelectionRange(), attr);
2650 else
2651 SetAndShowDefaultStyle(attr);
2652}
2653
fe5aa22c
JS
2654/// Apply the style sheet to the buffer, for example if the styles have changed.
2655bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
2656{
2657 if (!styleSheet)
2658 styleSheet = GetBuffer().GetStyleSheet();
2659 if (!styleSheet)
2660 return false;
2661
2662 if (GetBuffer().ApplyStyleSheet(styleSheet))
2663 {
2664 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2665 Refresh(false);
2666 return true;
2667 }
2668 else
2669 return false;
2670}
2671
5d7836c4
JS
2672/// Sets the default style to the style under the cursor
2673bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
2674{
2675 wxTextAttrEx attr;
2676 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
2677
ab14c7aa
JS
2678 // If at the start of a paragraph, use the next position.
2679 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2680
fe5aa22c
JS
2681#if wxRICHTEXT_USE_DYNAMIC_STYLES
2682 if (GetUncombinedStyle(pos, attr))
2683#else
ab14c7aa 2684 if (GetStyle(pos, attr))
fe5aa22c 2685#endif
5d7836c4
JS
2686 {
2687 SetDefaultStyle(attr);
2688 return true;
2689 }
41e155b4
WS
2690
2691 return false;
5d7836c4
JS
2692}
2693
4d551ad5
JS
2694/// Returns the first visible position in the current view
2695long wxRichTextCtrl::GetFirstVisiblePosition() const
2696{
2697 wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y);
2698 if (line)
2699 return line->GetAbsoluteRange().GetStart();
2700 else
2701 return 0;
2702}
2703
ab14c7aa
JS
2704/// The adjusted caret position is the character position adjusted to take
2705/// into account whether we're at the start of a paragraph, in which case
2706/// style information should be taken from the next position, not current one.
2707long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
2708{
2709 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPos+1);
2710
2711 if (para && (caretPos+1 == para->GetRange().GetStart()))
2712 caretPos ++;
2713 return caretPos;
2714}
2715
96c9f0f6
JS
2716/// Get/set the selection range in character positions. -1, -1 means no selection.
2717/// The range is in API convention, i.e. a single character selection is denoted
2718/// by (n, n+1)
2719wxRichTextRange wxRichTextCtrl::GetSelectionRange() const
2720{
2721 wxRichTextRange range = GetInternalSelectionRange();
2722 if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1))
2723 range.SetEnd(range.GetEnd() + 1);
2724 return range;
2725}
2726
2727void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range)
2728{
2729 wxRichTextRange range1(range);
2730 if (range1 != wxRichTextRange(-2,-2) && range1 != wxRichTextRange(-1,-1) )
2731 range1.SetEnd(range1.GetEnd() - 1);
2732
2733 wxASSERT( range1.GetStart() > range1.GetEnd() );
fe5aa22c 2734
96c9f0f6
JS
2735 SetInternalSelectionRange(range1);
2736}
2737
5d7836c4
JS
2738#endif
2739 // wxUSE_RICHTEXT