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