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