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