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