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