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