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