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