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