Removed conflicting virtual function
[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 #include "wx/richtext/richtextstyles.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #include "wx/settings.h"
27 #endif
28
29 #include "wx/textfile.h"
30 #include "wx/ffile.h"
31 #include "wx/filename.h"
32 #include "wx/dcbuffer.h"
33 #include "wx/arrimpl.cpp"
34 #include "wx/fontenum.h"
35 #include "wx/accel.h"
36
37 // DLL options compatibility check:
38 #include "wx/app.h"
39 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
40
41 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK, wxRichTextEvent );
42 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK, wxRichTextEvent );
43 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK, wxRichTextEvent );
44 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK, wxRichTextEvent );
45 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN, wxRichTextEvent );
46 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER, wxRichTextEvent );
47 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE, wxRichTextEvent );
48
49 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, wxRichTextEvent );
50 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED, wxRichTextEvent );
51 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING, wxRichTextEvent );
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED, wxRichTextEvent );
53
54 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, wxRichTextEvent );
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, wxRichTextEvent );
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED, wxRichTextEvent );
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED, wxRichTextEvent );
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, wxRichTextEvent );
59
60 #if wxRICHTEXT_USE_OWN_CARET
61
62 /*!
63 * wxRichTextCaret
64 *
65 * This implements a non-flashing cursor in case there
66 * are platform-specific problems with the generic caret.
67 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
68 */
69
70 class wxRichTextCaret: public wxCaret
71 {
72 public:
73 // ctors
74 // -----
75 // default - use Create()
76 wxRichTextCaret() { Init(); }
77 // creates a block caret associated with the given window
78 wxRichTextCaret(wxRichTextCtrl *window, int width, int height)
79 : wxCaret(window, width, height) { Init(); m_richTextCtrl = window; }
80 wxRichTextCaret(wxRichTextCtrl *window, const wxSize& size)
81 : wxCaret(window, size) { Init(); m_richTextCtrl = window; }
82
83 virtual ~wxRichTextCaret();
84
85 // implementation
86 // --------------
87
88 // called by wxWindow (not using the event tables)
89 virtual void OnSetFocus();
90 virtual void OnKillFocus();
91
92 // draw the caret on the given DC
93 void DoDraw(wxDC *dc);
94
95 // get the visible count
96 int GetVisibleCount() const { return m_countVisible; }
97
98 // delay repositioning
99 bool GetNeedsUpdate() const { return m_needsUpdate; }
100 void SetNeedsUpdate(bool needsUpdate = true ) { m_needsUpdate = needsUpdate; }
101
102 protected:
103 virtual void DoShow();
104 virtual void DoHide();
105 virtual void DoMove();
106 virtual void DoSize();
107
108 // refresh the caret
109 void Refresh();
110
111 private:
112 void Init();
113
114 int m_xOld,
115 m_yOld;
116 bool m_hasFocus; // true => our window has focus
117 bool m_needsUpdate; // must be repositioned
118
119 wxRichTextCtrl* m_richTextCtrl;
120 };
121 #endif
122
123 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl, wxTextCtrlBase )
124
125 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent, wxNotifyEvent )
126
127 BEGIN_EVENT_TABLE( wxRichTextCtrl, wxTextCtrlBase )
128 EVT_PAINT(wxRichTextCtrl::OnPaint)
129 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground)
130 EVT_IDLE(wxRichTextCtrl::OnIdle)
131 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll)
132 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick)
133 EVT_MOTION(wxRichTextCtrl::OnMoveMouse)
134 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp)
135 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick)
136 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick)
137 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick)
138 EVT_CHAR(wxRichTextCtrl::OnChar)
139 EVT_KEY_DOWN(wxRichTextCtrl::OnChar)
140 EVT_SIZE(wxRichTextCtrl::OnSize)
141 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus)
142 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus)
143 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost)
144 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu)
145 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged)
146
147 EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo)
148 EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo)
149
150 EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo)
151 EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo)
152
153 EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy)
154 EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy)
155
156 EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste)
157 EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste)
158
159 EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut)
160 EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut)
161
162 EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear)
163 EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear)
164
165 EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll)
166 EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll)
167 END_EVENT_TABLE()
168
169 /*!
170 * wxRichTextCtrl
171 */
172
173 wxArrayString wxRichTextCtrl::sm_availableFontNames;
174
175 wxRichTextCtrl::wxRichTextCtrl()
176 : wxScrollHelper(this)
177 {
178 Init();
179 }
180
181 wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent,
182 wxWindowID id,
183 const wxString& value,
184 const wxPoint& pos,
185 const wxSize& size,
186 long style,
187 const wxValidator& validator,
188 const wxString& name)
189 : wxScrollHelper(this)
190 {
191 Init();
192 Create(parent, id, value, pos, size, style, validator, name);
193 }
194
195 /// Creation
196 bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style,
197 const wxValidator& validator, const wxString& name)
198 {
199 style |= wxVSCROLL;
200
201 if (!wxTextCtrlBase::Create(parent, id, pos, size,
202 style|wxFULL_REPAINT_ON_RESIZE,
203 validator, name))
204 return false;
205
206 if (!GetFont().Ok())
207 {
208 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
209 }
210
211 // No physical scrolling, so we can preserve margins
212 EnableScrolling(false, false);
213
214 if (style & wxTE_READONLY)
215 SetEditable(false);
216
217 // The base attributes must all have default values
218 wxTextAttr attributes;
219 attributes.SetFont(GetFont());
220 attributes.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
221 attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
222 attributes.SetLineSpacing(10);
223 attributes.SetParagraphSpacingAfter(10);
224 attributes.SetParagraphSpacingBefore(0);
225
226 SetBasicStyle(attributes);
227
228 // The default attributes will be merged with base attributes, so
229 // can be empty to begin with
230 wxTextAttr defaultAttributes;
231 SetDefaultStyle(defaultAttributes);
232
233 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
234 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
235
236 GetBuffer().Reset();
237 GetBuffer().SetRichTextCtrl(this);
238
239 #if wxRICHTEXT_USE_OWN_CARET
240 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
241 #else
242 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
243 #endif
244
245 // Tell the sizers to use the given or best size
246 SetInitialSize(size);
247
248 #if wxRICHTEXT_BUFFERED_PAINTING
249 // Create a buffer
250 RecreateBuffer(size);
251 #endif
252
253 m_textCursor = wxCursor(wxCURSOR_IBEAM);
254 m_urlCursor = wxCursor(wxCURSOR_HAND);
255
256 SetCursor(m_textCursor);
257
258 if (!value.IsEmpty())
259 SetValue(value);
260
261 GetBuffer().AddEventHandler(this);
262
263 // Accelerators
264 wxAcceleratorEntry entries[6];
265
266 entries[0].Set(wxACCEL_CMD, (int) 'C', wxID_COPY);
267 entries[1].Set(wxACCEL_CMD, (int) 'X', wxID_CUT);
268 entries[2].Set(wxACCEL_CMD, (int) 'V', wxID_PASTE);
269 entries[3].Set(wxACCEL_CMD, (int) 'A', wxID_SELECTALL);
270 entries[4].Set(wxACCEL_CMD, (int) 'Z', wxID_UNDO);
271 entries[5].Set(wxACCEL_CMD, (int) 'Y', wxID_REDO);
272
273 wxAcceleratorTable accel(6, entries);
274 SetAcceleratorTable(accel);
275
276 return true;
277 }
278
279 wxRichTextCtrl::~wxRichTextCtrl()
280 {
281 GetBuffer().RemoveEventHandler(this);
282
283 delete m_contextMenu;
284 }
285
286 /// Member initialisation
287 void wxRichTextCtrl::Init()
288 {
289 m_contextMenu = NULL;
290 m_caret = NULL;
291 m_caretPosition = -1;
292 m_selectionRange.SetRange(-2, -2);
293 m_selectionAnchor = -2;
294 m_editable = true;
295 m_caretAtLineStart = false;
296 m_dragging = false;
297 m_fullLayoutRequired = false;
298 m_fullLayoutTime = 0;
299 m_fullLayoutSavedPosition = 0;
300 m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD;
301 m_caretPositionForDefaultStyle = -2;
302 }
303
304 void wxRichTextCtrl::DoThaw()
305 {
306 if (GetBuffer().GetDirty())
307 LayoutContent();
308 else
309 SetupScrollbars();
310
311 wxWindow::DoThaw();
312 }
313
314 /// Clear all text
315 void wxRichTextCtrl::Clear()
316 {
317 m_buffer.ResetAndClearCommands();
318 m_buffer.SetDirty(true);
319 m_caretPosition = -1;
320 m_caretPositionForDefaultStyle = -2;
321 m_caretAtLineStart = false;
322 m_selectionRange.SetRange(-2, -2);
323
324 Scroll(0,0);
325
326 if (!IsFrozen())
327 {
328 LayoutContent();
329 Refresh(false);
330 }
331
332 wxTextCtrl::SendTextUpdatedEvent(this);
333 }
334
335 /// Painting
336 void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
337 {
338 #if !wxRICHTEXT_USE_OWN_CARET
339 if (GetCaret() && !IsFrozen())
340 GetCaret()->Hide();
341 #endif
342
343 {
344 #if wxRICHTEXT_BUFFERED_PAINTING
345 wxBufferedPaintDC dc(this, m_bufferBitmap);
346 #else
347 wxPaintDC dc(this);
348 #endif
349
350 if (IsFrozen())
351 return;
352
353 PrepareDC(dc);
354
355 dc.SetFont(GetFont());
356
357 // Paint the background
358 PaintBackground(dc);
359
360 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
361
362 wxRect drawingArea(GetUpdateRegion().GetBox());
363 drawingArea.SetPosition(GetLogicalPoint(drawingArea.GetPosition()));
364
365 wxRect availableSpace(GetClientSize());
366 if (GetBuffer().GetDirty())
367 {
368 GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
369 GetBuffer().SetDirty(false);
370 SetupScrollbars();
371 }
372
373 wxRect clipRect(availableSpace);
374 clipRect.x += GetBuffer().GetLeftMargin();
375 clipRect.y += GetBuffer().GetTopMargin();
376 clipRect.width -= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
377 clipRect.height -= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
378 clipRect.SetPosition(GetLogicalPoint(clipRect.GetPosition()));
379 dc.SetClippingRegion(clipRect);
380
381 GetBuffer().Draw(dc, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */);
382
383 dc.DestroyClippingRegion();
384
385 #if wxRICHTEXT_USE_OWN_CARET
386 if (GetCaret()->IsVisible())
387 {
388 ((wxRichTextCaret*) GetCaret())->DoDraw(& dc);
389 }
390 #endif
391 }
392
393 #if !wxRICHTEXT_USE_OWN_CARET
394 if (GetCaret())
395 GetCaret()->Show();
396 PositionCaret();
397 #endif
398 }
399
400 // Empty implementation, to prevent flicker
401 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
402 {
403 }
404
405 void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
406 {
407 if (GetCaret())
408 {
409 #if !wxRICHTEXT_USE_OWN_CARET
410 PositionCaret();
411 #endif
412 GetCaret()->Show();
413 }
414
415 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
416 // Work around dropouts when control is focused
417 if (!IsFrozen())
418 {
419 Refresh(false);
420 }
421 #endif
422 }
423
424 void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
425 {
426 if (GetCaret())
427 GetCaret()->Hide();
428
429 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
430 // Work around dropouts when control is focused
431 if (!IsFrozen())
432 {
433 Refresh(false);
434 }
435 #endif
436 }
437
438 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
439 {
440 m_dragging = false;
441 }
442
443 /// Left-click
444 void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
445 {
446 SetFocus();
447
448 wxClientDC dc(this);
449 PrepareDC(dc);
450 dc.SetFont(GetFont());
451
452 long position = 0;
453 int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position);
454
455 if (hit != wxRICHTEXT_HITTEST_NONE)
456 {
457 m_dragStart = event.GetLogicalPosition(dc);
458 m_dragging = true;
459 CaptureMouse();
460
461 bool caretAtLineStart = false;
462
463 if (hit & wxRICHTEXT_HITTEST_BEFORE)
464 {
465 // If we're at the start of a line (but not first in para)
466 // then we should keep the caret showing at the start of the line
467 // by showing the m_caretAtLineStart flag.
468 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
469 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
470
471 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
472 caretAtLineStart = true;
473 position --;
474 }
475
476 long oldCaretPos = m_caretPosition;
477
478 MoveCaret(position, caretAtLineStart);
479 SetDefaultStyleToCursorStyle();
480
481 if (event.ShiftDown())
482 {
483 if (m_selectionRange.GetStart() == -2)
484 ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
485 else
486 ExtendSelection(m_caretPosition, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
487 }
488 else
489 SelectNone();
490 }
491
492 event.Skip();
493 }
494
495 /// Left-up
496 void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event)
497 {
498 if (m_dragging)
499 {
500 m_dragging = false;
501 if (GetCapture() == this)
502 ReleaseMouse();
503
504 // See if we clicked on a URL
505 wxClientDC dc(this);
506 PrepareDC(dc);
507 dc.SetFont(GetFont());
508
509 long position = 0;
510 wxPoint logicalPt = event.GetLogicalPosition(dc);
511 int hit = GetBuffer().HitTest(dc, logicalPt, position);
512
513 if ((hit != wxRICHTEXT_HITTEST_NONE) && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
514 {
515 wxRichTextEvent cmdEvent(
516 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK,
517 GetId());
518 cmdEvent.SetEventObject(this);
519 cmdEvent.SetPosition(m_caretPosition+1);
520
521 if (!GetEventHandler()->ProcessEvent(cmdEvent))
522 {
523 wxTextAttr attr;
524 if (GetStyle(position, attr))
525 {
526 if (attr.HasFlag(wxTEXT_ATTR_URL))
527 {
528 wxString urlTarget = attr.GetURL();
529 if (!urlTarget.IsEmpty())
530 {
531 wxMouseEvent mouseEvent(event);
532
533 long startPos = 0, endPos = 0;
534 wxRichTextObject* obj = GetBuffer().GetLeafObjectAtPosition(position);
535 if (obj)
536 {
537 startPos = obj->GetRange().GetStart();
538 endPos = obj->GetRange().GetEnd();
539 }
540
541 wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos);
542 InitCommandEvent(urlEvent);
543
544 urlEvent.SetString(urlTarget);
545
546 GetEventHandler()->ProcessEvent(urlEvent);
547 }
548 }
549 }
550 }
551 }
552 }
553 }
554
555 /// Left-click
556 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event)
557 {
558 wxClientDC dc(this);
559 PrepareDC(dc);
560 dc.SetFont(GetFont());
561
562 long position = 0;
563 wxPoint logicalPt = event.GetLogicalPosition(dc);
564 int hit = GetBuffer().HitTest(dc, logicalPt, position);
565
566 // See if we need to change the cursor
567
568 {
569 if (hit != wxRICHTEXT_HITTEST_NONE && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
570 {
571 wxTextAttr attr;
572 if (GetStyle(position, attr))
573 {
574 if (attr.HasFlag(wxTEXT_ATTR_URL))
575 {
576 SetCursor(m_urlCursor);
577 }
578 else if (!attr.HasFlag(wxTEXT_ATTR_URL))
579 {
580 SetCursor(m_textCursor);
581 }
582 }
583 }
584 else
585 SetCursor(m_textCursor);
586 }
587
588 if (!event.Dragging())
589 {
590 event.Skip();
591 return;
592 }
593
594 if (m_dragging && hit != wxRICHTEXT_HITTEST_NONE)
595 {
596 // TODO: test closeness
597
598 bool caretAtLineStart = false;
599
600 if (hit & wxRICHTEXT_HITTEST_BEFORE)
601 {
602 // If we're at the start of a line (but not first in para)
603 // then we should keep the caret showing at the start of the line
604 // by showing the m_caretAtLineStart flag.
605 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
606 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
607
608 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
609 caretAtLineStart = true;
610 position --;
611 }
612
613 if (m_caretPosition != position)
614 {
615 ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN);
616
617 MoveCaret(position, caretAtLineStart);
618 SetDefaultStyleToCursorStyle();
619 }
620 }
621 }
622
623 /// Right-click
624 void wxRichTextCtrl::OnRightClick(wxMouseEvent& event)
625 {
626 SetFocus();
627
628 wxRichTextEvent cmdEvent(
629 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK,
630 GetId());
631 cmdEvent.SetEventObject(this);
632 cmdEvent.SetPosition(m_caretPosition+1);
633
634 if (!GetEventHandler()->ProcessEvent(cmdEvent))
635 event.Skip();
636 }
637
638 /// Left-double-click
639 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& WXUNUSED(event))
640 {
641 wxRichTextEvent cmdEvent(
642 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK,
643 GetId());
644 cmdEvent.SetEventObject(this);
645 cmdEvent.SetPosition(m_caretPosition+1);
646
647 if (!GetEventHandler()->ProcessEvent(cmdEvent))
648 {
649 SelectWord(GetCaretPosition()+1);
650 }
651 }
652
653 /// Middle-click
654 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event)
655 {
656 wxRichTextEvent cmdEvent(
657 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK,
658 GetId());
659 cmdEvent.SetEventObject(this);
660 cmdEvent.SetPosition(m_caretPosition+1);
661
662 if (!GetEventHandler()->ProcessEvent(cmdEvent))
663 event.Skip();
664 }
665
666 /// Key press
667 void wxRichTextCtrl::OnChar(wxKeyEvent& event)
668 {
669 int flags = 0;
670 if (event.CmdDown())
671 flags |= wxRICHTEXT_CTRL_DOWN;
672 if (event.ShiftDown())
673 flags |= wxRICHTEXT_SHIFT_DOWN;
674 if (event.AltDown())
675 flags |= wxRICHTEXT_ALT_DOWN;
676
677 if (event.GetEventType() == wxEVT_KEY_DOWN)
678 {
679 if (event.GetKeyCode() == WXK_LEFT ||
680 event.GetKeyCode() == WXK_RIGHT ||
681 event.GetKeyCode() == WXK_UP ||
682 event.GetKeyCode() == WXK_DOWN ||
683 event.GetKeyCode() == WXK_HOME ||
684 event.GetKeyCode() == WXK_PAGEUP ||
685 event.GetKeyCode() == WXK_PAGEDOWN ||
686 event.GetKeyCode() == WXK_END ||
687
688 event.GetKeyCode() == WXK_NUMPAD_LEFT ||
689 event.GetKeyCode() == WXK_NUMPAD_RIGHT ||
690 event.GetKeyCode() == WXK_NUMPAD_UP ||
691 event.GetKeyCode() == WXK_NUMPAD_DOWN ||
692 event.GetKeyCode() == WXK_NUMPAD_HOME ||
693 event.GetKeyCode() == WXK_NUMPAD_PAGEUP ||
694 event.GetKeyCode() == WXK_NUMPAD_PAGEDOWN ||
695 event.GetKeyCode() == WXK_NUMPAD_END)
696 {
697 KeyboardNavigate(event.GetKeyCode(), flags);
698 return;
699 }
700
701 long keycode = event.GetKeyCode();
702 switch ( keycode )
703 {
704 case WXK_ESCAPE:
705 case WXK_START:
706 case WXK_LBUTTON:
707 case WXK_RBUTTON:
708 case WXK_CANCEL:
709 case WXK_MBUTTON:
710 case WXK_CLEAR:
711 case WXK_SHIFT:
712 case WXK_ALT:
713 case WXK_CONTROL:
714 case WXK_MENU:
715 case WXK_PAUSE:
716 case WXK_CAPITAL:
717 case WXK_END:
718 case WXK_HOME:
719 case WXK_LEFT:
720 case WXK_UP:
721 case WXK_RIGHT:
722 case WXK_DOWN:
723 case WXK_SELECT:
724 case WXK_PRINT:
725 case WXK_EXECUTE:
726 case WXK_SNAPSHOT:
727 case WXK_INSERT:
728 case WXK_HELP:
729 case WXK_F1:
730 case WXK_F2:
731 case WXK_F3:
732 case WXK_F4:
733 case WXK_F5:
734 case WXK_F6:
735 case WXK_F7:
736 case WXK_F8:
737 case WXK_F9:
738 case WXK_F10:
739 case WXK_F11:
740 case WXK_F12:
741 case WXK_F13:
742 case WXK_F14:
743 case WXK_F15:
744 case WXK_F16:
745 case WXK_F17:
746 case WXK_F18:
747 case WXK_F19:
748 case WXK_F20:
749 case WXK_F21:
750 case WXK_F22:
751 case WXK_F23:
752 case WXK_F24:
753 case WXK_NUMLOCK:
754 case WXK_SCROLL:
755 case WXK_PAGEUP:
756 case WXK_PAGEDOWN:
757 case WXK_NUMPAD_F1:
758 case WXK_NUMPAD_F2:
759 case WXK_NUMPAD_F3:
760 case WXK_NUMPAD_F4:
761 case WXK_NUMPAD_HOME:
762 case WXK_NUMPAD_LEFT:
763 case WXK_NUMPAD_UP:
764 case WXK_NUMPAD_RIGHT:
765 case WXK_NUMPAD_DOWN:
766 case WXK_NUMPAD_PAGEUP:
767 case WXK_NUMPAD_PAGEDOWN:
768 case WXK_NUMPAD_END:
769 case WXK_NUMPAD_BEGIN:
770 case WXK_NUMPAD_INSERT:
771 case WXK_NUMPAD_DELETE:
772 case WXK_WINDOWS_LEFT:
773 {
774 return;
775 }
776 default:
777 {
778 }
779 }
780
781 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
782 if (event.CmdDown() && event.GetKeyCode() == WXK_BACK)
783 {
784 BeginBatchUndo(_("Delete Text"));
785
786 long newPos = m_caretPosition;
787
788 bool processed = DeleteSelectedContent(& newPos);
789
790 // Submit range in character positions, which are greater than caret positions,
791 // so subtract 1 for deleted character and add 1 for conversion to character position.
792 if (newPos > -1)
793 {
794 if (event.CmdDown())
795 {
796 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
797 if (pos < newPos)
798 {
799 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this);
800 processed = true;
801 }
802 }
803
804 if (!processed)
805 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this);
806 }
807
808 EndBatchUndo();
809
810 if (GetLastPosition() == -1)
811 {
812 GetBuffer().Reset();
813
814 m_caretPosition = -1;
815 PositionCaret();
816 SetDefaultStyleToCursorStyle();
817 }
818
819 ScrollIntoView(m_caretPosition, WXK_LEFT);
820
821 wxRichTextEvent cmdEvent(
822 wxEVT_COMMAND_RICHTEXT_DELETE,
823 GetId());
824 cmdEvent.SetEventObject(this);
825 cmdEvent.SetFlags(flags);
826 cmdEvent.SetPosition(m_caretPosition+1);
827 GetEventHandler()->ProcessEvent(cmdEvent);
828
829 Update();
830 }
831 else
832 event.Skip();
833
834 return;
835 }
836
837 // all the other keys modify the controls contents which shouldn't be
838 // possible if we're read-only
839 if ( !IsEditable() )
840 {
841 event.Skip();
842 return;
843 }
844
845 if (event.GetKeyCode() == WXK_RETURN)
846 {
847 BeginBatchUndo(_("Insert Text"));
848
849 long newPos = m_caretPosition;
850
851 DeleteSelectedContent(& newPos);
852
853 if (event.ShiftDown())
854 {
855 wxString text;
856 text = wxRichTextLineBreakChar;
857 GetBuffer().InsertTextWithUndo(newPos+1, text, this);
858 m_caretAtLineStart = true;
859 PositionCaret();
860 }
861 else
862 GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE|wxRICHTEXT_INSERT_INTERACTIVE);
863
864 EndBatchUndo();
865 SetDefaultStyleToCursorStyle();
866
867 ScrollIntoView(m_caretPosition, WXK_RIGHT);
868
869 wxRichTextEvent cmdEvent(
870 wxEVT_COMMAND_RICHTEXT_RETURN,
871 GetId());
872 cmdEvent.SetEventObject(this);
873 cmdEvent.SetFlags(flags);
874 cmdEvent.SetPosition(newPos+1);
875
876 if (!GetEventHandler()->ProcessEvent(cmdEvent))
877 {
878 // Generate conventional event
879 wxCommandEvent textEvent(wxEVT_COMMAND_TEXT_ENTER, GetId());
880 InitCommandEvent(textEvent);
881
882 GetEventHandler()->ProcessEvent(textEvent);
883 }
884 Update();
885 }
886 else if (event.GetKeyCode() == WXK_BACK)
887 {
888 BeginBatchUndo(_("Delete Text"));
889
890 long newPos = m_caretPosition;
891
892 bool processed = DeleteSelectedContent(& newPos);
893
894 // Submit range in character positions, which are greater than caret positions,
895 // so subtract 1 for deleted character and add 1 for conversion to character position.
896 if (newPos > -1)
897 {
898 if (event.CmdDown())
899 {
900 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
901 if (pos < newPos)
902 {
903 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this);
904 processed = true;
905 }
906 }
907
908 if (!processed)
909 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this);
910 }
911
912 EndBatchUndo();
913
914 if (GetLastPosition() == -1)
915 {
916 GetBuffer().Reset();
917
918 m_caretPosition = -1;
919 PositionCaret();
920 SetDefaultStyleToCursorStyle();
921 }
922
923 ScrollIntoView(m_caretPosition, WXK_LEFT);
924
925 wxRichTextEvent cmdEvent(
926 wxEVT_COMMAND_RICHTEXT_DELETE,
927 GetId());
928 cmdEvent.SetEventObject(this);
929 cmdEvent.SetFlags(flags);
930 cmdEvent.SetPosition(m_caretPosition+1);
931 GetEventHandler()->ProcessEvent(cmdEvent);
932
933 Update();
934 }
935 else if (event.GetKeyCode() == WXK_DELETE)
936 {
937 BeginBatchUndo(_("Delete Text"));
938
939 long newPos = m_caretPosition;
940
941 bool processed = DeleteSelectedContent(& newPos);
942
943 // Submit range in character positions, which are greater than caret positions,
944 if (newPos < GetBuffer().GetRange().GetEnd()+1)
945 {
946 if (event.CmdDown())
947 {
948 long pos = wxRichTextCtrl::FindNextWordPosition(1);
949 if (pos != -1 && (pos > newPos))
950 {
951 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos+1, pos), this);
952 processed = true;
953 }
954 }
955
956 if (!processed)
957 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos+1, newPos+1), this);
958 }
959
960 EndBatchUndo();
961
962 if (GetLastPosition() == -1)
963 {
964 GetBuffer().Reset();
965
966 m_caretPosition = -1;
967 PositionCaret();
968 SetDefaultStyleToCursorStyle();
969 }
970
971 wxRichTextEvent cmdEvent(
972 wxEVT_COMMAND_RICHTEXT_DELETE,
973 GetId());
974 cmdEvent.SetEventObject(this);
975 cmdEvent.SetFlags(flags);
976 cmdEvent.SetPosition(m_caretPosition+1);
977 GetEventHandler()->ProcessEvent(cmdEvent);
978
979 Update();
980 }
981 else
982 {
983 long keycode = event.GetKeyCode();
984 switch ( keycode )
985 {
986 case WXK_ESCAPE:
987 {
988 event.Skip();
989 return;
990 }
991
992 default:
993 {
994 #ifdef __WXMAC__
995 if (event.CmdDown())
996 #else
997 if (event.CmdDown() || event.AltDown())
998 #endif
999 {
1000 event.Skip();
1001 return;
1002 }
1003
1004 wxRichTextEvent cmdEvent(
1005 wxEVT_COMMAND_RICHTEXT_CHARACTER,
1006 GetId());
1007 cmdEvent.SetEventObject(this);
1008 cmdEvent.SetFlags(flags);
1009 #if wxUSE_UNICODE
1010 cmdEvent.SetCharacter(event.GetUnicodeKey());
1011 #else
1012 cmdEvent.SetCharacter((wxChar) keycode);
1013 #endif
1014 cmdEvent.SetPosition(m_caretPosition+1);
1015
1016 if (keycode == wxT('\t'))
1017 {
1018 // See if we need to promote or demote the selection or paragraph at the cursor
1019 // position, instead of inserting a tab.
1020 long pos = GetAdjustedCaretPosition(GetCaretPosition());
1021 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos);
1022 if (para && para->GetRange().GetStart() == pos && para->GetAttributes().HasListStyleName())
1023 {
1024 wxRichTextRange range;
1025 if (HasSelection())
1026 range = GetSelectionRange();
1027 else
1028 range = para->GetRange().FromInternal();
1029
1030 int promoteBy = event.ShiftDown() ? 1 : -1;
1031
1032 PromoteList(promoteBy, range, NULL);
1033
1034 GetEventHandler()->ProcessEvent(cmdEvent);
1035
1036 return;
1037 }
1038 }
1039
1040 BeginBatchUndo(_("Insert Text"));
1041
1042 long newPos = m_caretPosition;
1043 DeleteSelectedContent(& newPos);
1044
1045 #if wxUSE_UNICODE
1046 wxString str = event.GetUnicodeKey();
1047 #else
1048 wxString str = (wxChar) event.GetKeyCode();
1049 #endif
1050 GetBuffer().InsertTextWithUndo(newPos+1, str, this, 0);
1051
1052 EndBatchUndo();
1053
1054 SetDefaultStyleToCursorStyle();
1055 ScrollIntoView(m_caretPosition, WXK_RIGHT);
1056
1057 GetEventHandler()->ProcessEvent(cmdEvent);
1058
1059 Update();
1060 }
1061 }
1062 }
1063 }
1064
1065 /// Delete content if there is a selection, e.g. when pressing a key.
1066 bool wxRichTextCtrl::DeleteSelectedContent(long* newPos)
1067 {
1068 if (HasSelection())
1069 {
1070 long pos = m_selectionRange.GetStart();
1071 GetBuffer().DeleteRangeWithUndo(m_selectionRange, this);
1072 m_selectionRange.SetRange(-2, -2);
1073
1074 if (newPos)
1075 *newPos = pos-1;
1076 return true;
1077 }
1078 else
1079 return false;
1080 }
1081
1082 /// Keyboard navigation
1083
1084 /*
1085
1086 Left: left one character
1087 Right: right one character
1088 Up: up one line
1089 Down: down one line
1090 Ctrl-Left: left one word
1091 Ctrl-Right: right one word
1092 Ctrl-Up: previous paragraph start
1093 Ctrl-Down: next start of paragraph
1094 Home: start of line
1095 End: end of line
1096 Ctrl-Home: start of document
1097 Ctrl-End: end of document
1098 Page-Up: Up a screen
1099 Page-Down: Down a screen
1100
1101 Maybe:
1102
1103 Ctrl-Alt-PgUp: Start of window
1104 Ctrl-Alt-PgDn: End of window
1105 F8: Start selection mode
1106 Esc: End selection mode
1107
1108 Adding Shift does the above but starts/extends selection.
1109
1110
1111 */
1112
1113 bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags)
1114 {
1115 bool success = false;
1116
1117 if (keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT)
1118 {
1119 if (flags & wxRICHTEXT_CTRL_DOWN)
1120 success = WordRight(1, flags);
1121 else
1122 success = MoveRight(1, flags);
1123 }
1124 else if (keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT)
1125 {
1126 if (flags & wxRICHTEXT_CTRL_DOWN)
1127 success = WordLeft(1, flags);
1128 else
1129 success = MoveLeft(1, flags);
1130 }
1131 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP)
1132 {
1133 if (flags & wxRICHTEXT_CTRL_DOWN)
1134 success = MoveToParagraphStart(flags);
1135 else
1136 success = MoveUp(1, flags);
1137 }
1138 else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN)
1139 {
1140 if (flags & wxRICHTEXT_CTRL_DOWN)
1141 success = MoveToParagraphEnd(flags);
1142 else
1143 success = MoveDown(1, flags);
1144 }
1145 else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP)
1146 {
1147 success = PageUp(1, flags);
1148 }
1149 else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
1150 {
1151 success = PageDown(1, flags);
1152 }
1153 else if (keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME)
1154 {
1155 if (flags & wxRICHTEXT_CTRL_DOWN)
1156 success = MoveHome(flags);
1157 else
1158 success = MoveToLineStart(flags);
1159 }
1160 else if (keyCode == WXK_END || keyCode == WXK_NUMPAD_END)
1161 {
1162 if (flags & wxRICHTEXT_CTRL_DOWN)
1163 success = MoveEnd(flags);
1164 else
1165 success = MoveToLineEnd(flags);
1166 }
1167
1168 if (success)
1169 {
1170 ScrollIntoView(m_caretPosition, keyCode);
1171 SetDefaultStyleToCursorStyle();
1172 }
1173
1174 return success;
1175 }
1176
1177 /// Extend the selection. Selections are in caret positions.
1178 bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags)
1179 {
1180 if (flags & wxRICHTEXT_SHIFT_DOWN)
1181 {
1182 if (oldPos == newPos)
1183 return false;
1184
1185 wxRichTextRange oldSelection = m_selectionRange;
1186
1187 // If not currently selecting, start selecting
1188 if (m_selectionRange.GetStart() == -2)
1189 {
1190 m_selectionAnchor = oldPos;
1191
1192 if (oldPos > newPos)
1193 m_selectionRange.SetRange(newPos+1, oldPos);
1194 else
1195 m_selectionRange.SetRange(oldPos+1, newPos);
1196 }
1197 else
1198 {
1199 // Always ensure that the selection range start is greater than
1200 // the end.
1201 if (newPos > m_selectionAnchor)
1202 m_selectionRange.SetRange(m_selectionAnchor+1, newPos);
1203 else if (newPos == m_selectionAnchor)
1204 m_selectionRange = wxRichTextRange(-2, -2);
1205 else
1206 m_selectionRange.SetRange(newPos+1, m_selectionAnchor);
1207 }
1208
1209 RefreshForSelectionChange(oldSelection, m_selectionRange);
1210
1211 if (m_selectionRange.GetStart() > m_selectionRange.GetEnd())
1212 {
1213 wxLogDebug(wxT("Strange selection range"));
1214 }
1215
1216 return true;
1217 }
1218 else
1219 return false;
1220 }
1221
1222 /// Scroll into view, returning true if we scrolled.
1223 /// This takes a _caret_ position.
1224 bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode)
1225 {
1226 wxRichTextLine* line = GetVisibleLineForCaretPosition(position);
1227
1228 if (!line)
1229 return false;
1230
1231 int ppuX, ppuY;
1232 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1233
1234 int startXUnits, startYUnits;
1235 GetViewStart(& startXUnits, & startYUnits);
1236 int startY = startYUnits * ppuY;
1237
1238 int sx = 0, sy = 0;
1239 GetVirtualSize(& sx, & sy);
1240 int sxUnits = 0;
1241 int syUnits = 0;
1242 if (ppuY != 0)
1243 syUnits = sy/ppuY;
1244
1245 wxRect rect = line->GetRect();
1246
1247 bool scrolled = false;
1248
1249 wxSize clientSize = GetClientSize();
1250 clientSize.y -= GetBuffer().GetBottomMargin();
1251
1252 if (GetWindowStyle() & wxRE_CENTRE_CARET)
1253 {
1254 int y = rect.y - GetClientSize().y/2;
1255 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1256 if (y >= 0 && (y + clientSize.y) < GetBuffer().GetCachedSize().y)
1257 {
1258 if (startYUnits != yUnits)
1259 {
1260 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1261 scrolled = true;
1262 }
1263 #if !wxRICHTEXT_USE_OWN_CARET
1264 if (scrolled)
1265 #endif
1266 PositionCaret();
1267
1268 return scrolled;
1269 }
1270 }
1271
1272 // Going down
1273 if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN ||
1274 keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT ||
1275 keyCode == WXK_END || keyCode == WXK_NUMPAD_END ||
1276 keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
1277 {
1278 if ((rect.y + rect.height) > (clientSize.y + startY))
1279 {
1280 // Make it scroll so this item is at the bottom
1281 // of the window
1282 int y = rect.y - (clientSize.y - rect.height);
1283 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1284
1285 // If we're still off the screen, scroll another line down
1286 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1287 yUnits ++;
1288
1289 if (startYUnits != yUnits)
1290 {
1291 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1292 scrolled = true;
1293 }
1294 }
1295 else if (rect.y < (startY + GetBuffer().GetTopMargin()))
1296 {
1297 // Make it scroll so this item is at the top
1298 // of the window
1299 int y = rect.y - GetBuffer().GetTopMargin();
1300 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1301
1302 if (startYUnits != yUnits)
1303 {
1304 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1305 scrolled = true;
1306 }
1307 }
1308 }
1309 // Going up
1310 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP ||
1311 keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT ||
1312 keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME ||
1313 keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP )
1314 {
1315 if (rect.y < (startY + GetBuffer().GetBottomMargin()))
1316 {
1317 // Make it scroll so this item is at the top
1318 // of the window
1319 int y = rect.y - GetBuffer().GetTopMargin();
1320 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1321
1322 if (startYUnits != yUnits)
1323 {
1324 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1325 scrolled = true;
1326 }
1327 }
1328 else if ((rect.y + rect.height) > (clientSize.y + startY))
1329 {
1330 // Make it scroll so this item is at the bottom
1331 // of the window
1332 int y = rect.y - (clientSize.y - rect.height);
1333 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1334
1335 // If we're still off the screen, scroll another line down
1336 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1337 yUnits ++;
1338
1339 if (startYUnits != yUnits)
1340 {
1341 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1342 scrolled = true;
1343 }
1344 }
1345 }
1346
1347 #if !wxRICHTEXT_USE_OWN_CARET
1348 if (scrolled)
1349 #endif
1350 PositionCaret();
1351
1352 return scrolled;
1353 }
1354
1355 /// Is the given position visible on the screen?
1356 bool wxRichTextCtrl::IsPositionVisible(long pos) const
1357 {
1358 wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1);
1359
1360 if (!line)
1361 return false;
1362
1363 int ppuX, ppuY;
1364 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1365
1366 int startX, startY;
1367 GetViewStart(& startX, & startY);
1368 startX = 0;
1369 startY = startY * ppuY;
1370
1371 wxRect rect = line->GetRect();
1372 wxSize clientSize = GetClientSize();
1373 clientSize.y -= GetBuffer().GetBottomMargin();
1374
1375 return (rect.GetBottom() > (startY + GetBuffer().GetTopMargin())) && (rect.GetTop() < (startY + clientSize.y));
1376 }
1377
1378 void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart)
1379 {
1380 m_caretPosition = position;
1381 m_caretAtLineStart = showAtLineStart;
1382 }
1383
1384 /// Move caret one visual step forward: this may mean setting a flag
1385 /// and keeping the same position if we're going from the end of one line
1386 /// to the start of the next, which may be the exact same caret position.
1387 void wxRichTextCtrl::MoveCaretForward(long oldPosition)
1388 {
1389 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
1390
1391 // Only do the check if we're not at the end of the paragraph (where things work OK
1392 // anyway)
1393 if (para && (oldPosition != para->GetRange().GetEnd() - 1))
1394 {
1395 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
1396
1397 if (line)
1398 {
1399 wxRichTextRange lineRange = line->GetAbsoluteRange();
1400
1401 // We're at the end of a line. See whether we need to
1402 // stay at the same actual caret position but change visual
1403 // position, or not.
1404 if (oldPosition == lineRange.GetEnd())
1405 {
1406 if (m_caretAtLineStart)
1407 {
1408 // We're already at the start of the line, so actually move on now.
1409 m_caretPosition = oldPosition + 1;
1410 m_caretAtLineStart = false;
1411 }
1412 else
1413 {
1414 // We're showing at the end of the line, so keep to
1415 // the same position but indicate that we're to show
1416 // at the start of the next line.
1417 m_caretPosition = oldPosition;
1418 m_caretAtLineStart = true;
1419 }
1420 SetDefaultStyleToCursorStyle();
1421 return;
1422 }
1423 }
1424 }
1425 m_caretPosition ++;
1426 SetDefaultStyleToCursorStyle();
1427 }
1428
1429 /// Move caret one visual step backward: this may mean setting a flag
1430 /// and keeping the same position if we're going from the end of one line
1431 /// to the start of the next, which may be the exact same caret position.
1432 void wxRichTextCtrl::MoveCaretBack(long oldPosition)
1433 {
1434 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
1435
1436 // Only do the check if we're not at the start of the paragraph (where things work OK
1437 // anyway)
1438 if (para && (oldPosition != para->GetRange().GetStart()))
1439 {
1440 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
1441
1442 if (line)
1443 {
1444 wxRichTextRange lineRange = line->GetAbsoluteRange();
1445
1446 // We're at the start of a line. See whether we need to
1447 // stay at the same actual caret position but change visual
1448 // position, or not.
1449 if (oldPosition == lineRange.GetStart())
1450 {
1451 m_caretPosition = oldPosition-1;
1452 m_caretAtLineStart = true;
1453 return;
1454 }
1455 else if (oldPosition == lineRange.GetEnd())
1456 {
1457 if (m_caretAtLineStart)
1458 {
1459 // We're at the start of the line, so keep the same caret position
1460 // but clear the start-of-line flag.
1461 m_caretPosition = oldPosition;
1462 m_caretAtLineStart = false;
1463 }
1464 else
1465 {
1466 // We're showing at the end of the line, so go back
1467 // to the previous character position.
1468 m_caretPosition = oldPosition - 1;
1469 }
1470 SetDefaultStyleToCursorStyle();
1471 return;
1472 }
1473 }
1474 }
1475 m_caretPosition --;
1476 SetDefaultStyleToCursorStyle();
1477 }
1478
1479 /// Move right
1480 bool wxRichTextCtrl::MoveRight(int noPositions, int flags)
1481 {
1482 long endPos = GetBuffer().GetRange().GetEnd();
1483
1484 if (m_caretPosition + noPositions < endPos)
1485 {
1486 long oldPos = m_caretPosition;
1487 long newPos = m_caretPosition + noPositions;
1488
1489 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1490 if (!extendSel)
1491 SelectNone();
1492
1493 // Determine by looking at oldPos and m_caretPosition whether
1494 // we moved from the end of a line to the start of the next line, in which case
1495 // we want to adjust the caret position such that it is positioned at the
1496 // start of the next line, rather than jumping past the first character of the
1497 // line.
1498 if (noPositions == 1 && !extendSel)
1499 MoveCaretForward(oldPos);
1500 else
1501 SetCaretPosition(newPos);
1502
1503 PositionCaret();
1504 SetDefaultStyleToCursorStyle();
1505
1506 return true;
1507 }
1508 else
1509 return false;
1510 }
1511
1512 /// Move left
1513 bool wxRichTextCtrl::MoveLeft(int noPositions, int flags)
1514 {
1515 long startPos = -1;
1516
1517 if (m_caretPosition > startPos - noPositions + 1)
1518 {
1519 long oldPos = m_caretPosition;
1520 long newPos = m_caretPosition - noPositions;
1521 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1522 if (!extendSel)
1523 SelectNone();
1524
1525 if (noPositions == 1 && !extendSel)
1526 MoveCaretBack(oldPos);
1527 else
1528 SetCaretPosition(newPos);
1529
1530 PositionCaret();
1531 SetDefaultStyleToCursorStyle();
1532
1533 return true;
1534 }
1535 else
1536 return false;
1537 }
1538
1539 /// Move up
1540 bool wxRichTextCtrl::MoveUp(int noLines, int flags)
1541 {
1542 return MoveDown(- noLines, flags);
1543 }
1544
1545 /// Move up
1546 bool wxRichTextCtrl::MoveDown(int noLines, int flags)
1547 {
1548 if (!GetCaret())
1549 return false;
1550
1551 long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart);
1552 wxPoint pt = GetCaret()->GetPosition();
1553 long newLine = lineNumber + noLines;
1554
1555 if (lineNumber != -1)
1556 {
1557 if (noLines > 0)
1558 {
1559 long lastLine = GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1560
1561 if (newLine > lastLine)
1562 return false;
1563 }
1564 else
1565 {
1566 if (newLine < 0)
1567 return false;
1568 }
1569 }
1570
1571 wxRichTextLine* lineObj = GetBuffer().GetLineForVisibleLineNumber(newLine);
1572 if (lineObj)
1573 {
1574 pt.y = lineObj->GetAbsolutePosition().y + 2;
1575 }
1576 else
1577 return false;
1578
1579 long newPos = 0;
1580 wxClientDC dc(this);
1581 PrepareDC(dc);
1582 dc.SetFont(GetFont());
1583
1584 int hitTest = GetBuffer().HitTest(dc, pt, newPos);
1585
1586 if (hitTest != wxRICHTEXT_HITTEST_NONE)
1587 {
1588 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1589 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1590 // so we view the caret at the start of the line.
1591 bool caretLineStart = false;
1592 if (hitTest & wxRICHTEXT_HITTEST_BEFORE)
1593 {
1594 wxRichTextLine* thisLine = GetBuffer().GetLineAtPosition(newPos-1);
1595 wxRichTextRange lineRange;
1596 if (thisLine)
1597 lineRange = thisLine->GetAbsoluteRange();
1598
1599 if (thisLine && (newPos-1) == lineRange.GetEnd())
1600 {
1601 newPos --;
1602 caretLineStart = true;
1603 }
1604 else
1605 {
1606 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(newPos);
1607 if (para && para->GetRange().GetStart() == newPos)
1608 newPos --;
1609 }
1610 }
1611
1612 long newSelEnd = newPos;
1613
1614 bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags);
1615 if (!extendSel)
1616 SelectNone();
1617
1618 SetCaretPosition(newPos, caretLineStart);
1619 PositionCaret();
1620 SetDefaultStyleToCursorStyle();
1621
1622 return true;
1623 }
1624
1625 return false;
1626 }
1627
1628 /// Move to the end of the paragraph
1629 bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
1630 {
1631 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1632 if (para)
1633 {
1634 long newPos = para->GetRange().GetEnd() - 1;
1635 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1636 if (!extendSel)
1637 SelectNone();
1638
1639 SetCaretPosition(newPos);
1640 PositionCaret();
1641 SetDefaultStyleToCursorStyle();
1642
1643 return true;
1644 }
1645
1646 return false;
1647 }
1648
1649 /// Move to the start of the paragraph
1650 bool wxRichTextCtrl::MoveToParagraphStart(int flags)
1651 {
1652 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1653 if (para)
1654 {
1655 long newPos = para->GetRange().GetStart() - 1;
1656 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1657 if (!extendSel)
1658 SelectNone();
1659
1660 SetCaretPosition(newPos);
1661 PositionCaret();
1662 SetDefaultStyleToCursorStyle();
1663
1664 return true;
1665 }
1666
1667 return false;
1668 }
1669
1670 /// Move to the end of the line
1671 bool wxRichTextCtrl::MoveToLineEnd(int flags)
1672 {
1673 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1674
1675 if (line)
1676 {
1677 wxRichTextRange lineRange = line->GetAbsoluteRange();
1678 long newPos = lineRange.GetEnd();
1679 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1680 if (!extendSel)
1681 SelectNone();
1682
1683 SetCaretPosition(newPos);
1684 PositionCaret();
1685 SetDefaultStyleToCursorStyle();
1686
1687 return true;
1688 }
1689
1690 return false;
1691 }
1692
1693 /// Move to the start of the line
1694 bool wxRichTextCtrl::MoveToLineStart(int flags)
1695 {
1696 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1697 if (line)
1698 {
1699 wxRichTextRange lineRange = line->GetAbsoluteRange();
1700 long newPos = lineRange.GetStart()-1;
1701
1702 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1703 if (!extendSel)
1704 SelectNone();
1705
1706 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line);
1707
1708 SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart());
1709 PositionCaret();
1710 SetDefaultStyleToCursorStyle();
1711
1712 return true;
1713 }
1714
1715 return false;
1716 }
1717
1718 /// Move to the start of the buffer
1719 bool wxRichTextCtrl::MoveHome(int flags)
1720 {
1721 if (m_caretPosition != -1)
1722 {
1723 bool extendSel = ExtendSelection(m_caretPosition, -1, flags);
1724 if (!extendSel)
1725 SelectNone();
1726
1727 SetCaretPosition(-1);
1728 PositionCaret();
1729 SetDefaultStyleToCursorStyle();
1730
1731 return true;
1732 }
1733 else
1734 return false;
1735 }
1736
1737 /// Move to the end of the buffer
1738 bool wxRichTextCtrl::MoveEnd(int flags)
1739 {
1740 long endPos = GetBuffer().GetRange().GetEnd()-1;
1741
1742 if (m_caretPosition != endPos)
1743 {
1744 bool extendSel = ExtendSelection(m_caretPosition, endPos, flags);
1745 if (!extendSel)
1746 SelectNone();
1747
1748 SetCaretPosition(endPos);
1749 PositionCaret();
1750 SetDefaultStyleToCursorStyle();
1751
1752 return true;
1753 }
1754 else
1755 return false;
1756 }
1757
1758 /// Move noPages pages up
1759 bool wxRichTextCtrl::PageUp(int noPages, int flags)
1760 {
1761 return PageDown(- noPages, flags);
1762 }
1763
1764 /// Move noPages pages down
1765 bool wxRichTextCtrl::PageDown(int noPages, int flags)
1766 {
1767 // Calculate which line occurs noPages * screen height further down.
1768 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1769 if (line)
1770 {
1771 wxSize clientSize = GetClientSize();
1772 int newY = line->GetAbsolutePosition().y + noPages*clientSize.y;
1773
1774 wxRichTextLine* newLine = GetBuffer().GetLineAtYPosition(newY);
1775 if (newLine)
1776 {
1777 wxRichTextRange lineRange = newLine->GetAbsoluteRange();
1778 long pos = lineRange.GetStart()-1;
1779 if (pos != m_caretPosition)
1780 {
1781 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine);
1782
1783 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1784 if (!extendSel)
1785 SelectNone();
1786
1787 SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart());
1788 PositionCaret();
1789 SetDefaultStyleToCursorStyle();
1790
1791 return true;
1792 }
1793 }
1794 }
1795
1796 return false;
1797 }
1798
1799 static bool wxRichTextCtrlIsWhitespace(const wxString& str)
1800 {
1801 return str == wxT(" ") || str == wxT("\t");
1802 }
1803
1804 // Finds the caret position for the next word
1805 long wxRichTextCtrl::FindNextWordPosition(int direction) const
1806 {
1807 long endPos = GetBuffer().GetRange().GetEnd();
1808
1809 if (direction > 0)
1810 {
1811 long i = m_caretPosition+1+direction; // +1 for conversion to character pos
1812
1813 // First skip current text to space
1814 while (i < endPos && i > -1)
1815 {
1816 // i is in character, not caret positions
1817 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1818 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1819 if (line && (i == line->GetAbsoluteRange().GetEnd()))
1820 {
1821 break;
1822 }
1823 else if (!wxRichTextCtrlIsWhitespace(text) && !text.empty())
1824 i += direction;
1825 else
1826 {
1827 break;
1828 }
1829 }
1830 while (i < endPos && i > -1)
1831 {
1832 // i is in character, not caret positions
1833 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1834 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1835 if (line && (i == line->GetAbsoluteRange().GetEnd()))
1836 return wxMax(-1, i);
1837
1838 if (text.empty()) // End of paragraph, or maybe an image
1839 return wxMax(-1, i - 1);
1840 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
1841 i += direction;
1842 else
1843 {
1844 // Convert to caret position
1845 return wxMax(-1, i - 1);
1846 }
1847 }
1848 if (i >= endPos)
1849 return endPos-1;
1850 return i-1;
1851 }
1852 else
1853 {
1854 long i = m_caretPosition;
1855
1856 // First skip white space
1857 while (i < endPos && i > -1)
1858 {
1859 // i is in character, not caret positions
1860 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1861 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1862
1863 if (text.empty() || (line && (i == line->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1864 break;
1865 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
1866 i += direction;
1867 else
1868 break;
1869 }
1870 // Next skip current text to space
1871 while (i < endPos && i > -1)
1872 {
1873 // i is in character, not caret positions
1874 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1875 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1876 if (line && line->GetAbsoluteRange().GetStart() == i)
1877 return i-1;
1878
1879 if (!wxRichTextCtrlIsWhitespace(text) /* && !text.empty() */)
1880 i += direction;
1881 else
1882 {
1883 return i;
1884 }
1885 }
1886 if (i < -1)
1887 return -1;
1888 return i;
1889 }
1890 }
1891
1892 /// Move n words left
1893 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags)
1894 {
1895 long pos = FindNextWordPosition(-1);
1896 if (pos != m_caretPosition)
1897 {
1898 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
1899
1900 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1901 if (!extendSel)
1902 SelectNone();
1903
1904 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1905 PositionCaret();
1906 SetDefaultStyleToCursorStyle();
1907
1908 return true;
1909 }
1910
1911 return false;
1912 }
1913
1914 /// Move n words right
1915 bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags)
1916 {
1917 long pos = FindNextWordPosition(1);
1918 if (pos != m_caretPosition)
1919 {
1920 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
1921
1922 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1923 if (!extendSel)
1924 SelectNone();
1925
1926 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1927 PositionCaret();
1928 SetDefaultStyleToCursorStyle();
1929
1930 return true;
1931 }
1932
1933 return false;
1934 }
1935
1936 /// Sizing
1937 void wxRichTextCtrl::OnSize(wxSizeEvent& event)
1938 {
1939 // Only do sizing optimization for large buffers
1940 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold)
1941 {
1942 m_fullLayoutRequired = true;
1943 m_fullLayoutTime = wxGetLocalTimeMillis();
1944 m_fullLayoutSavedPosition = GetFirstVisiblePosition();
1945 LayoutContent(true /* onlyVisibleRect */);
1946 }
1947 else
1948 GetBuffer().Invalidate(wxRICHTEXT_ALL);
1949
1950 #if wxRICHTEXT_BUFFERED_PAINTING
1951 RecreateBuffer();
1952 #endif
1953
1954 event.Skip();
1955 }
1956
1957
1958 /// Idle-time processing
1959 void wxRichTextCtrl::OnIdle(wxIdleEvent& event)
1960 {
1961 #if wxRICHTEXT_USE_OWN_CARET
1962 if (((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
1963 {
1964 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate(false);
1965 PositionCaret();
1966 GetCaret()->Show();
1967 }
1968 #endif
1969
1970 const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL;
1971
1972 if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval)))
1973 {
1974 m_fullLayoutRequired = false;
1975 m_fullLayoutTime = 0;
1976 GetBuffer().Invalidate(wxRICHTEXT_ALL);
1977 ShowPosition(m_fullLayoutSavedPosition);
1978 Refresh(false);
1979 }
1980
1981 if (m_caretPositionForDefaultStyle != -2)
1982 {
1983 // If the caret position has changed, no longer reflect the default style
1984 // in the UI.
1985 if (GetCaretPosition() != m_caretPositionForDefaultStyle)
1986 m_caretPositionForDefaultStyle = -2;
1987 }
1988
1989 event.Skip();
1990 }
1991
1992 /// Scrolling
1993 void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
1994 {
1995 #if wxRICHTEXT_USE_OWN_CARET
1996 if (!((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
1997 {
1998 GetCaret()->Hide();
1999 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate();
2000 }
2001 #endif
2002
2003 event.Skip();
2004 }
2005
2006 /// Set up scrollbars, e.g. after a resize
2007 void wxRichTextCtrl::SetupScrollbars(bool atTop)
2008 {
2009 if (IsFrozen())
2010 return;
2011
2012 if (GetBuffer().IsEmpty())
2013 {
2014 SetScrollbars(0, 0, 0, 0, 0, 0);
2015 return;
2016 }
2017
2018 // TODO: reimplement scrolling so we scroll by line, not by fixed number
2019 // of pixels. See e.g. wxVScrolledWindow for ideas.
2020 int pixelsPerUnit = 5;
2021 wxSize clientSize = GetClientSize();
2022
2023 int maxHeight = GetBuffer().GetCachedSize().y + GetBuffer().GetTopMargin();
2024
2025 // Round up so we have at least maxHeight pixels
2026 int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5);
2027
2028 int startX = 0, startY = 0;
2029 if (!atTop)
2030 GetViewStart(& startX, & startY);
2031
2032 int maxPositionX = 0;
2033 int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5);
2034
2035 int newStartX = wxMin(maxPositionX, startX);
2036 int newStartY = wxMin(maxPositionY, startY);
2037
2038 int oldPPUX, oldPPUY;
2039 int oldStartX, oldStartY;
2040 int oldVirtualSizeX = 0, oldVirtualSizeY = 0;
2041 GetScrollPixelsPerUnit(& oldPPUX, & oldPPUY);
2042 GetViewStart(& oldStartX, & oldStartY);
2043 GetVirtualSize(& oldVirtualSizeX, & oldVirtualSizeY);
2044 if (oldPPUY > 0)
2045 oldVirtualSizeY /= oldPPUY;
2046
2047 if (oldPPUX == 0 && oldPPUY == pixelsPerUnit && oldVirtualSizeY == unitsY && oldStartX == newStartX && oldStartY == newStartY)
2048 return;
2049
2050 // Don't set scrollbars if there were none before, and there will be none now.
2051 if (oldPPUY != 0 && (oldVirtualSizeY < clientSize.y) && (unitsY*pixelsPerUnit < clientSize.y))
2052 return;
2053
2054 // Move to previous scroll position if
2055 // possible
2056 SetScrollbars(0, pixelsPerUnit, 0, unitsY, newStartX, newStartY);
2057 }
2058
2059 /// Paint the background
2060 void wxRichTextCtrl::PaintBackground(wxDC& dc)
2061 {
2062 wxColour backgroundColour = GetBackgroundColour();
2063 if (!backgroundColour.Ok())
2064 backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
2065
2066 // Clear the background
2067 dc.SetBrush(wxBrush(backgroundColour));
2068 dc.SetPen(*wxTRANSPARENT_PEN);
2069 wxRect windowRect(GetClientSize());
2070 windowRect.x -= 2; windowRect.y -= 2;
2071 windowRect.width += 4; windowRect.height += 4;
2072
2073 // We need to shift the rectangle to take into account
2074 // scrolling. Converting device to logical coordinates.
2075 CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y);
2076 dc.DrawRectangle(windowRect);
2077 }
2078
2079 #if wxRICHTEXT_BUFFERED_PAINTING
2080 /// Recreate buffer bitmap if necessary
2081 bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
2082 {
2083 wxSize sz = size;
2084 if (sz == wxDefaultSize)
2085 sz = GetClientSize();
2086
2087 if (sz.x < 1 || sz.y < 1)
2088 return false;
2089
2090 if (!m_bufferBitmap.Ok() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y)
2091 m_bufferBitmap = wxBitmap(sz.x, sz.y);
2092 return m_bufferBitmap.Ok();
2093 }
2094 #endif
2095
2096 // ----------------------------------------------------------------------------
2097 // file IO functions
2098 // ----------------------------------------------------------------------------
2099
2100 bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
2101 {
2102 bool success = GetBuffer().LoadFile(filename, (wxRichTextFileType)fileType);
2103 if (success)
2104 m_filename = filename;
2105
2106 DiscardEdits();
2107 SetInsertionPoint(0);
2108 LayoutContent();
2109 PositionCaret();
2110 SetupScrollbars(true);
2111 Refresh(false);
2112 wxTextCtrl::SendTextUpdatedEvent(this);
2113
2114 if (success)
2115 return true;
2116 else
2117 {
2118 wxLogError(_("File couldn't be loaded."));
2119
2120 return false;
2121 }
2122 }
2123
2124 bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
2125 {
2126 if (GetBuffer().SaveFile(filename, (wxRichTextFileType)fileType))
2127 {
2128 m_filename = filename;
2129
2130 DiscardEdits();
2131
2132 return true;
2133 }
2134
2135 wxLogError(_("The text couldn't be saved."));
2136
2137 return false;
2138 }
2139
2140 // ----------------------------------------------------------------------------
2141 // wxRichTextCtrl specific functionality
2142 // ----------------------------------------------------------------------------
2143
2144 /// Add a new paragraph of text to the end of the buffer
2145 wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
2146 {
2147 wxRichTextRange range = GetBuffer().AddParagraph(text);
2148 LayoutContent();
2149 return range;
2150 }
2151
2152 /// Add an image
2153 wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
2154 {
2155 wxRichTextRange range = GetBuffer().AddImage(image);
2156 LayoutContent();
2157 return range;
2158 }
2159
2160 // ----------------------------------------------------------------------------
2161 // selection and ranges
2162 // ----------------------------------------------------------------------------
2163
2164 void wxRichTextCtrl::SelectAll()
2165 {
2166 SetSelection(0, GetLastPosition()+1);
2167 m_selectionAnchor = -1;
2168 }
2169
2170 /// Select none
2171 void wxRichTextCtrl::SelectNone()
2172 {
2173 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2174 {
2175 wxRichTextRange oldSelection = m_selectionRange;
2176
2177 m_selectionRange = wxRichTextRange(-2, -2);
2178
2179 RefreshForSelectionChange(oldSelection, m_selectionRange);
2180 }
2181 m_selectionAnchor = -2;
2182 }
2183
2184 static bool wxIsWordDelimiter(const wxString& text)
2185 {
2186 return !text.IsEmpty() && !wxIsalnum(text[0]);
2187 }
2188
2189 /// Select the word at the given character position
2190 bool wxRichTextCtrl::SelectWord(long position)
2191 {
2192 if (position < 0 || position > GetBuffer().GetRange().GetEnd())
2193 return false;
2194
2195 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
2196 if (!para)
2197 return false;
2198
2199 if (position == para->GetRange().GetEnd())
2200 position --;
2201
2202 long positionStart = position;
2203 long positionEnd = position;
2204
2205 for (positionStart = position; positionStart >= para->GetRange().GetStart(); positionStart --)
2206 {
2207 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionStart, positionStart));
2208 if (wxIsWordDelimiter(text))
2209 {
2210 positionStart ++;
2211 break;
2212 }
2213 }
2214 if (positionStart < para->GetRange().GetStart())
2215 positionStart = para->GetRange().GetStart();
2216
2217 for (positionEnd = position; positionEnd < para->GetRange().GetEnd(); positionEnd ++)
2218 {
2219 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionEnd, positionEnd));
2220 if (wxIsWordDelimiter(text))
2221 {
2222 positionEnd --;
2223 break;
2224 }
2225 }
2226 if (positionEnd >= para->GetRange().GetEnd())
2227 positionEnd = para->GetRange().GetEnd();
2228
2229 if (positionEnd < positionStart)
2230 return false;
2231
2232 SetSelection(positionStart, positionEnd+1);
2233
2234 if (positionStart >= 0)
2235 {
2236 MoveCaret(positionStart-1, true);
2237 SetDefaultStyleToCursorStyle();
2238 }
2239
2240 return true;
2241 }
2242
2243 wxString wxRichTextCtrl::GetStringSelection() const
2244 {
2245 long from, to;
2246 GetSelection(&from, &to);
2247
2248 return GetRange(from, to);
2249 }
2250
2251 // ----------------------------------------------------------------------------
2252 // hit testing
2253 // ----------------------------------------------------------------------------
2254
2255 wxTextCtrlHitTestResult
2256 wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
2257 {
2258 // implement in terms of the other overload as the native ports typically
2259 // can get the position and not (x, y) pair directly (although wxUniv
2260 // directly gets x and y -- and so overrides this method as well)
2261 long pos;
2262 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
2263
2264 if ( rc != wxTE_HT_UNKNOWN )
2265 {
2266 PositionToXY(pos, x, y);
2267 }
2268
2269 return rc;
2270 }
2271
2272 wxTextCtrlHitTestResult
2273 wxRichTextCtrl::HitTest(const wxPoint& pt,
2274 long * pos) const
2275 {
2276 wxClientDC dc((wxRichTextCtrl*) this);
2277 ((wxRichTextCtrl*)this)->PrepareDC(dc);
2278
2279 // Buffer uses logical position (relative to start of buffer)
2280 // so convert
2281 wxPoint pt2 = GetLogicalPoint(pt);
2282
2283 int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt2, *pos);
2284
2285 if ((hit & wxRICHTEXT_HITTEST_BEFORE) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2286 return wxTE_HT_BEFORE;
2287 else if ((hit & wxRICHTEXT_HITTEST_AFTER) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2288 return wxTE_HT_BEYOND;
2289 else if (hit & (wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER))
2290 return wxTE_HT_ON_TEXT;
2291
2292 return wxTE_HT_UNKNOWN;
2293 }
2294
2295 // ----------------------------------------------------------------------------
2296 // set/get the controls text
2297 // ----------------------------------------------------------------------------
2298
2299 wxString wxRichTextCtrl::DoGetValue() const
2300 {
2301 return GetBuffer().GetText();
2302 }
2303
2304 wxString wxRichTextCtrl::GetRange(long from, long to) const
2305 {
2306 // Public API for range is different from internals
2307 return GetBuffer().GetTextForRange(wxRichTextRange(from, to-1));
2308 }
2309
2310 void wxRichTextCtrl::DoSetValue(const wxString& value, int flags)
2311 {
2312 // Don't call Clear here, since it always sends a text updated event
2313 m_buffer.ResetAndClearCommands();
2314 m_buffer.SetDirty(true);
2315 m_caretPosition = -1;
2316 m_caretPositionForDefaultStyle = -2;
2317 m_caretAtLineStart = false;
2318 m_selectionRange.SetRange(-2, -2);
2319
2320 Scroll(0,0);
2321
2322 if (!IsFrozen())
2323 {
2324 LayoutContent();
2325 Refresh(false);
2326 }
2327
2328 if (!value.IsEmpty())
2329 {
2330 // Remove empty paragraph
2331 GetBuffer().Clear();
2332 DoWriteText(value, flags);
2333
2334 // for compatibility, don't move the cursor when doing SetValue()
2335 SetInsertionPoint(0);
2336 }
2337 else
2338 {
2339 // still send an event for consistency
2340 if (flags & SetValue_SendEvent)
2341 wxTextCtrl::SendTextUpdatedEvent(this);
2342 }
2343 DiscardEdits();
2344 }
2345
2346 void wxRichTextCtrl::WriteText(const wxString& value)
2347 {
2348 DoWriteText(value);
2349 }
2350
2351 void wxRichTextCtrl::DoWriteText(const wxString& value, int flags)
2352 {
2353 wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix);
2354
2355 GetBuffer().InsertTextWithUndo(m_caretPosition+1, valueUnix, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
2356
2357 if ( flags & SetValue_SendEvent )
2358 wxTextCtrl::SendTextUpdatedEvent(this);
2359 }
2360
2361 void wxRichTextCtrl::AppendText(const wxString& text)
2362 {
2363 SetInsertionPointEnd();
2364
2365 WriteText(text);
2366 }
2367
2368 /// Write an image at the current insertion point
2369 bool wxRichTextCtrl::WriteImage(const wxImage& image, wxBitmapType bitmapType)
2370 {
2371 wxRichTextImageBlock imageBlock;
2372
2373 wxImage image2 = image;
2374 if (imageBlock.MakeImageBlock(image2, bitmapType))
2375 return WriteImage(imageBlock);
2376
2377 return false;
2378 }
2379
2380 bool wxRichTextCtrl::WriteImage(const wxString& filename, wxBitmapType bitmapType)
2381 {
2382 wxRichTextImageBlock imageBlock;
2383
2384 wxImage image;
2385 if (imageBlock.MakeImageBlock(filename, bitmapType, image, false))
2386 return WriteImage(imageBlock);
2387
2388 return false;
2389 }
2390
2391 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock)
2392 {
2393 return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this);
2394 }
2395
2396 bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, wxBitmapType bitmapType)
2397 {
2398 if (bitmap.Ok())
2399 {
2400 wxRichTextImageBlock imageBlock;
2401
2402 wxImage image = bitmap.ConvertToImage();
2403 if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType))
2404 return WriteImage(imageBlock);
2405 }
2406
2407 return false;
2408 }
2409
2410 /// Insert a newline (actually paragraph) at the current insertion point.
2411 bool wxRichTextCtrl::Newline()
2412 {
2413 return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
2414 }
2415
2416 /// Insert a line break at the current insertion point.
2417 bool wxRichTextCtrl::LineBreak()
2418 {
2419 wxString text;
2420 text = wxRichTextLineBreakChar;
2421 return GetBuffer().InsertTextWithUndo(m_caretPosition+1, text, this);
2422 }
2423
2424 // ----------------------------------------------------------------------------
2425 // Clipboard operations
2426 // ----------------------------------------------------------------------------
2427
2428 void wxRichTextCtrl::Copy()
2429 {
2430 if (CanCopy())
2431 {
2432 wxRichTextRange range = GetInternalSelectionRange();
2433 GetBuffer().CopyToClipboard(range);
2434 }
2435 }
2436
2437 void wxRichTextCtrl::Cut()
2438 {
2439 if (CanCut())
2440 {
2441 wxRichTextRange range = GetInternalSelectionRange();
2442 GetBuffer().CopyToClipboard(range);
2443
2444 DeleteSelectedContent();
2445 LayoutContent();
2446 Refresh(false);
2447 }
2448 }
2449
2450 void wxRichTextCtrl::Paste()
2451 {
2452 if (CanPaste())
2453 {
2454 BeginBatchUndo(_("Paste"));
2455
2456 long newPos = m_caretPosition;
2457 DeleteSelectedContent(& newPos);
2458
2459 GetBuffer().PasteFromClipboard(newPos);
2460
2461 EndBatchUndo();
2462 }
2463 }
2464
2465 void wxRichTextCtrl::DeleteSelection()
2466 {
2467 if (CanDeleteSelection())
2468 {
2469 DeleteSelectedContent();
2470 }
2471 }
2472
2473 bool wxRichTextCtrl::HasSelection() const
2474 {
2475 return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2;
2476 }
2477
2478 bool wxRichTextCtrl::CanCopy() const
2479 {
2480 // Can copy if there's a selection
2481 return HasSelection();
2482 }
2483
2484 bool wxRichTextCtrl::CanCut() const
2485 {
2486 return HasSelection() && IsEditable();
2487 }
2488
2489 bool wxRichTextCtrl::CanPaste() const
2490 {
2491 if ( !IsEditable() )
2492 return false;
2493
2494 return GetBuffer().CanPasteFromClipboard();
2495 }
2496
2497 bool wxRichTextCtrl::CanDeleteSelection() const
2498 {
2499 return HasSelection() && IsEditable();
2500 }
2501
2502
2503 // ----------------------------------------------------------------------------
2504 // Accessors
2505 // ----------------------------------------------------------------------------
2506
2507 void wxRichTextCtrl::SetEditable(bool editable)
2508 {
2509 m_editable = editable;
2510 }
2511
2512 void wxRichTextCtrl::SetInsertionPoint(long pos)
2513 {
2514 SelectNone();
2515
2516 m_caretPosition = pos - 1;
2517
2518 PositionCaret();
2519 }
2520
2521 void wxRichTextCtrl::SetInsertionPointEnd()
2522 {
2523 long pos = GetLastPosition();
2524 SetInsertionPoint(pos);
2525 }
2526
2527 long wxRichTextCtrl::GetInsertionPoint() const
2528 {
2529 return m_caretPosition+1;
2530 }
2531
2532 wxTextPos wxRichTextCtrl::GetLastPosition() const
2533 {
2534 return GetBuffer().GetRange().GetEnd();
2535 }
2536
2537 // If the return values from and to are the same, there is no
2538 // selection.
2539 void wxRichTextCtrl::GetSelection(long* from, long* to) const
2540 {
2541 *from = m_selectionRange.GetStart();
2542 *to = m_selectionRange.GetEnd();
2543 if ((*to) != -1 && (*to) != -2)
2544 (*to) ++;
2545 }
2546
2547 bool wxRichTextCtrl::IsEditable() const
2548 {
2549 return m_editable;
2550 }
2551
2552 // ----------------------------------------------------------------------------
2553 // selection
2554 // ----------------------------------------------------------------------------
2555
2556 void wxRichTextCtrl::SetSelection(long from, long to)
2557 {
2558 // if from and to are both -1, it means (in wxWidgets) that all text should
2559 // be selected.
2560 if ( (from == -1) && (to == -1) )
2561 {
2562 from = 0;
2563 to = GetLastPosition()+1;
2564 }
2565
2566 if (from == to)
2567 {
2568 SelectNone();
2569 }
2570 else
2571 {
2572 wxRichTextRange oldSelection = m_selectionRange;
2573 m_selectionAnchor = from;
2574 m_selectionRange.SetRange(from, to-1);
2575 if (from > -2)
2576 m_caretPosition = from-1;
2577
2578 RefreshForSelectionChange(oldSelection, m_selectionRange);
2579 PositionCaret();
2580 }
2581 }
2582
2583 // ----------------------------------------------------------------------------
2584 // Editing
2585 // ----------------------------------------------------------------------------
2586
2587 void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to),
2588 const wxString& value)
2589 {
2590 BeginBatchUndo(_("Replace"));
2591
2592 DeleteSelectedContent();
2593
2594 DoWriteText(value, SetValue_SelectionOnly);
2595
2596 EndBatchUndo();
2597 }
2598
2599 void wxRichTextCtrl::Remove(long from, long to)
2600 {
2601 SelectNone();
2602
2603 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to-1), this);
2604
2605 LayoutContent();
2606 if (!IsFrozen())
2607 Refresh(false);
2608 }
2609
2610 bool wxRichTextCtrl::IsModified() const
2611 {
2612 return m_buffer.IsModified();
2613 }
2614
2615 void wxRichTextCtrl::MarkDirty()
2616 {
2617 m_buffer.Modify(true);
2618 }
2619
2620 void wxRichTextCtrl::DiscardEdits()
2621 {
2622 m_caretPositionForDefaultStyle = -2;
2623 m_buffer.Modify(false);
2624 m_buffer.GetCommandProcessor()->ClearCommands();
2625 }
2626
2627 int wxRichTextCtrl::GetNumberOfLines() const
2628 {
2629 return GetBuffer().GetParagraphCount();
2630 }
2631
2632 // ----------------------------------------------------------------------------
2633 // Positions <-> coords
2634 // ----------------------------------------------------------------------------
2635
2636 long wxRichTextCtrl::XYToPosition(long x, long y) const
2637 {
2638 return GetBuffer().XYToPosition(x, y);
2639 }
2640
2641 bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
2642 {
2643 return GetBuffer().PositionToXY(pos, x, y);
2644 }
2645
2646 // ----------------------------------------------------------------------------
2647 //
2648 // ----------------------------------------------------------------------------
2649
2650 void wxRichTextCtrl::ShowPosition(long pos)
2651 {
2652 if (!IsPositionVisible(pos))
2653 ScrollIntoView(pos-1, WXK_DOWN);
2654 }
2655
2656 int wxRichTextCtrl::GetLineLength(long lineNo) const
2657 {
2658 return GetBuffer().GetParagraphLength(lineNo);
2659 }
2660
2661 wxString wxRichTextCtrl::GetLineText(long lineNo) const
2662 {
2663 return GetBuffer().GetParagraphText(lineNo);
2664 }
2665
2666 // ----------------------------------------------------------------------------
2667 // Undo/redo
2668 // ----------------------------------------------------------------------------
2669
2670 void wxRichTextCtrl::Undo()
2671 {
2672 if (CanUndo())
2673 {
2674 GetCommandProcessor()->Undo();
2675 }
2676 }
2677
2678 void wxRichTextCtrl::Redo()
2679 {
2680 if (CanRedo())
2681 {
2682 GetCommandProcessor()->Redo();
2683 }
2684 }
2685
2686 bool wxRichTextCtrl::CanUndo() const
2687 {
2688 return GetCommandProcessor()->CanUndo();
2689 }
2690
2691 bool wxRichTextCtrl::CanRedo() const
2692 {
2693 return GetCommandProcessor()->CanRedo();
2694 }
2695
2696 // ----------------------------------------------------------------------------
2697 // implementation details
2698 // ----------------------------------------------------------------------------
2699
2700 void wxRichTextCtrl::Command(wxCommandEvent& event)
2701 {
2702 SetValue(event.GetString());
2703 GetEventHandler()->ProcessEvent(event);
2704 }
2705
2706 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event)
2707 {
2708 // By default, load the first file into the text window.
2709 if (event.GetNumberOfFiles() > 0)
2710 {
2711 LoadFile(event.GetFiles()[0]);
2712 }
2713 }
2714
2715 wxSize wxRichTextCtrl::DoGetBestSize() const
2716 {
2717 return wxSize(10, 10);
2718 }
2719
2720 // ----------------------------------------------------------------------------
2721 // standard handlers for standard edit menu events
2722 // ----------------------------------------------------------------------------
2723
2724 void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2725 {
2726 Cut();
2727 }
2728
2729 void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
2730 {
2731 DeleteSelection();
2732 }
2733
2734 void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2735 {
2736 Copy();
2737 }
2738
2739 void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2740 {
2741 Paste();
2742 }
2743
2744 void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2745 {
2746 Undo();
2747 }
2748
2749 void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2750 {
2751 Redo();
2752 }
2753
2754 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2755 {
2756 event.Enable( CanCut() );
2757 }
2758
2759 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2760 {
2761 event.Enable( CanCopy() );
2762 }
2763
2764 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
2765 {
2766 event.Enable( CanDeleteSelection() );
2767 }
2768
2769 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2770 {
2771 event.Enable( CanPaste() );
2772 }
2773
2774 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2775 {
2776 event.Enable( CanUndo() );
2777 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2778 }
2779
2780 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2781 {
2782 event.Enable( CanRedo() );
2783 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2784 }
2785
2786 void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
2787 {
2788 SelectAll();
2789 }
2790
2791 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
2792 {
2793 event.Enable(GetLastPosition() > 0);
2794 }
2795
2796 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event)
2797 {
2798 if (event.GetEventObject() != this)
2799 {
2800 event.Skip();
2801 return;
2802 }
2803
2804 if (!m_contextMenu)
2805 {
2806 m_contextMenu = new wxMenu;
2807 m_contextMenu->Append(wxID_UNDO, _("&Undo"));
2808 m_contextMenu->Append(wxID_REDO, _("&Redo"));
2809 m_contextMenu->AppendSeparator();
2810 m_contextMenu->Append(wxID_CUT, _("Cu&t"));
2811 m_contextMenu->Append(wxID_COPY, _("&Copy"));
2812 m_contextMenu->Append(wxID_PASTE, _("&Paste"));
2813 m_contextMenu->Append(wxID_CLEAR, _("&Delete"));
2814 m_contextMenu->AppendSeparator();
2815 m_contextMenu->Append(wxID_SELECTALL, _("Select &All"));
2816 }
2817 PopupMenu(m_contextMenu);
2818 return;
2819 }
2820
2821 bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
2822 {
2823 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttr(style));
2824 }
2825
2826 bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxTextAttr& style)
2827 {
2828 return GetBuffer().SetStyle(range.ToInternal(), style);
2829 }
2830
2831 // extended style setting operation with flags including:
2832 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2833 // see richtextbuffer.h for more details.
2834
2835 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxTextAttr& style, int flags)
2836 {
2837 return GetBuffer().SetStyle(range.ToInternal(), style, flags);
2838 }
2839
2840 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2841 {
2842 return GetBuffer().SetDefaultStyle(wxTextAttr(style));
2843 }
2844
2845 const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const
2846 {
2847 return GetBuffer().GetDefaultStyle();
2848 }
2849
2850 bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style)
2851 {
2852 return GetBuffer().GetStyle(position, style);
2853 }
2854
2855 // get the common set of styles for the range
2856 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttr& style)
2857 {
2858 return GetBuffer().GetStyleForRange(range.ToInternal(), style);
2859 }
2860
2861 /// Get the content (uncombined) attributes for this position.
2862 bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttr& style)
2863 {
2864 return GetBuffer().GetUncombinedStyle(position, style);
2865 }
2866
2867 /// Set font, and also the buffer attributes
2868 bool wxRichTextCtrl::SetFont(const wxFont& font)
2869 {
2870 wxTextCtrlBase::SetFont(font);
2871
2872 wxTextAttr attr = GetBuffer().GetAttributes();
2873 attr.SetFont(font);
2874 GetBuffer().SetBasicStyle(attr);
2875
2876 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2877 Refresh(false);
2878
2879 return true;
2880 }
2881
2882 /// Transform logical to physical
2883 wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const
2884 {
2885 wxPoint pt;
2886 CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y);
2887
2888 return pt;
2889 }
2890
2891 /// Transform physical to logical
2892 wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
2893 {
2894 wxPoint pt;
2895 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
2896
2897 return pt;
2898 }
2899
2900 /// Position the caret
2901 void wxRichTextCtrl::PositionCaret()
2902 {
2903 if (!GetCaret())
2904 return;
2905
2906 //wxLogDebug(wxT("PositionCaret"));
2907
2908 wxRect caretRect;
2909 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect))
2910 {
2911 wxPoint newPt = caretRect.GetPosition();
2912 wxSize newSz = caretRect.GetSize();
2913 wxPoint pt = GetPhysicalPoint(newPt);
2914 if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz)
2915 {
2916 GetCaret()->Hide();
2917 if (GetCaret()->GetSize() != newSz)
2918 GetCaret()->SetSize(newSz);
2919
2920 int halfSize = newSz.y/2;
2921 // If the caret is beyond the margin, hide it by moving it out of the way
2922 if (((pt.y + halfSize) < GetBuffer().GetTopMargin()) || ((pt.y + halfSize) > (GetClientSize().y - GetBuffer().GetBottomMargin())))
2923 pt.y = -200;
2924
2925 GetCaret()->Move(pt);
2926 GetCaret()->Show();
2927 }
2928 }
2929 }
2930
2931 /// Get the caret height and position for the given character position
2932 bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect)
2933 {
2934 wxClientDC dc(this);
2935 dc.SetFont(GetFont());
2936
2937 PrepareDC(dc);
2938
2939 wxPoint pt;
2940 int height = 0;
2941
2942 if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart))
2943 {
2944 // Caret height can't be zero
2945 if (height == 0)
2946 height = dc.GetCharHeight();
2947
2948 rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height));
2949 return true;
2950 }
2951
2952 return false;
2953 }
2954
2955 /// Gets the line for the visible caret position. If the caret is
2956 /// shown at the very end of the line, it means the next character is actually
2957 /// on the following line. So let's get the line we're expecting to find
2958 /// if this is the case.
2959 wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
2960 {
2961 wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true);
2962 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true);
2963 if (line)
2964 {
2965 wxRichTextRange lineRange = line->GetAbsoluteRange();
2966 if (caretPosition == lineRange.GetStart()-1 &&
2967 (para->GetRange().GetStart() != lineRange.GetStart()))
2968 {
2969 if (!m_caretAtLineStart)
2970 line = GetBuffer().GetLineAtPosition(caretPosition-1, true);
2971 }
2972 }
2973 return line;
2974 }
2975
2976
2977 /// Move the caret to the given character position
2978 bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart)
2979 {
2980 if (GetBuffer().GetDirty())
2981 LayoutContent();
2982
2983 if (pos <= GetBuffer().GetRange().GetEnd())
2984 {
2985 SetCaretPosition(pos, showAtLineStart);
2986
2987 PositionCaret();
2988
2989 return true;
2990 }
2991 else
2992 return false;
2993 }
2994
2995 /// Layout the buffer: which we must do before certain operations, such as
2996 /// setting the caret position.
2997 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
2998 {
2999 if (GetBuffer().GetDirty() || onlyVisibleRect)
3000 {
3001 wxRect availableSpace(GetClientSize());
3002 if (availableSpace.width == 0)
3003 availableSpace.width = 10;
3004 if (availableSpace.height == 0)
3005 availableSpace.height = 10;
3006
3007 int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT;
3008 if (onlyVisibleRect)
3009 {
3010 flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
3011 availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3012 }
3013
3014 wxClientDC dc(this);
3015 dc.SetFont(GetFont());
3016
3017 PrepareDC(dc);
3018
3019 GetBuffer().Defragment();
3020 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3021 GetBuffer().Layout(dc, availableSpace, flags);
3022 GetBuffer().SetDirty(false);
3023
3024 if (!IsFrozen())
3025 SetupScrollbars();
3026 }
3027
3028 return true;
3029 }
3030
3031 /// Is all of the selection bold?
3032 bool wxRichTextCtrl::IsSelectionBold()
3033 {
3034 if (HasSelection())
3035 {
3036 wxTextAttr attr;
3037 wxRichTextRange range = GetSelectionRange();
3038 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
3039 attr.SetFontWeight(wxFONTWEIGHT_BOLD);
3040
3041 return HasCharacterAttributes(range, attr);
3042 }
3043 else
3044 {
3045 // If no selection, then we need to combine current style with default style
3046 // to see what the effect would be if we started typing.
3047 wxTextAttr attr;
3048 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
3049
3050 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3051 if (GetStyle(pos, attr))
3052 {
3053 if (IsDefaultStyleShowing())
3054 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
3055 return attr.GetFontWeight() == wxFONTWEIGHT_BOLD;
3056 }
3057 }
3058 return false;
3059 }
3060
3061 /// Is all of the selection italics?
3062 bool wxRichTextCtrl::IsSelectionItalics()
3063 {
3064 if (HasSelection())
3065 {
3066 wxRichTextRange range = GetSelectionRange();
3067 wxTextAttr attr;
3068 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
3069 attr.SetFontStyle(wxFONTSTYLE_ITALIC);
3070
3071 return HasCharacterAttributes(range, attr);
3072 }
3073 else
3074 {
3075 // If no selection, then we need to combine current style with default style
3076 // to see what the effect would be if we started typing.
3077 wxTextAttr attr;
3078 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
3079
3080 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3081 if (GetStyle(pos, attr))
3082 {
3083 if (IsDefaultStyleShowing())
3084 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
3085 return attr.GetFontStyle() == wxFONTSTYLE_ITALIC;
3086 }
3087 }
3088 return false;
3089 }
3090
3091 /// Is all of the selection underlined?
3092 bool wxRichTextCtrl::IsSelectionUnderlined()
3093 {
3094 if (HasSelection())
3095 {
3096 wxRichTextRange range = GetSelectionRange();
3097 wxTextAttr attr;
3098 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3099 attr.SetFontUnderlined(true);
3100
3101 return HasCharacterAttributes(range, attr);
3102 }
3103 else
3104 {
3105 // If no selection, then we need to combine current style with default style
3106 // to see what the effect would be if we started typing.
3107 wxTextAttr attr;
3108 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3109 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3110
3111 if (GetStyle(pos, attr))
3112 {
3113 if (IsDefaultStyleShowing())
3114 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
3115 return attr.GetFontUnderlined();
3116 }
3117 }
3118 return false;
3119 }
3120
3121 /// Apply bold to the selection
3122 bool wxRichTextCtrl::ApplyBoldToSelection()
3123 {
3124 wxTextAttr attr;
3125 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
3126 attr.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL : wxFONTWEIGHT_BOLD);
3127
3128 if (HasSelection())
3129 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3130 else
3131 {
3132 wxRichTextAttr current = GetDefaultStyleEx();
3133 current.Apply(attr);
3134 SetAndShowDefaultStyle(current);
3135 }
3136 return true;
3137 }
3138
3139 /// Apply italic to the selection
3140 bool wxRichTextCtrl::ApplyItalicToSelection()
3141 {
3142 wxTextAttr attr;
3143 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
3144 attr.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL : wxFONTSTYLE_ITALIC);
3145
3146 if (HasSelection())
3147 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3148 else
3149 {
3150 wxRichTextAttr current = GetDefaultStyleEx();
3151 current.Apply(attr);
3152 SetAndShowDefaultStyle(current);
3153 }
3154 return true;
3155 }
3156
3157 /// Apply underline to the selection
3158 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3159 {
3160 wxTextAttr attr;
3161 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3162 attr.SetFontUnderlined(!IsSelectionUnderlined());
3163
3164 if (HasSelection())
3165 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3166 else
3167 {
3168 wxRichTextAttr current = GetDefaultStyleEx();
3169 current.Apply(attr);
3170 SetAndShowDefaultStyle(current);
3171 }
3172 return true;
3173 }
3174
3175 /// Is all of the selection aligned according to the specified flag?
3176 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
3177 {
3178 wxRichTextRange range;
3179 if (HasSelection())
3180 range = GetSelectionRange();
3181 else
3182 range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3183
3184 wxTextAttr attr;
3185 attr.SetAlignment(alignment);
3186
3187 return HasParagraphAttributes(range, attr);
3188 }
3189
3190 /// Apply alignment to the selection
3191 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
3192 {
3193 wxTextAttr attr;
3194 attr.SetAlignment(alignment);
3195 if (HasSelection())
3196 return SetStyle(GetSelectionRange(), attr);
3197 else
3198 {
3199 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3200 if (para)
3201 return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY);
3202 }
3203 return true;
3204 }
3205
3206 /// Apply a named style to the selection
3207 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
3208 {
3209 // Flags are defined within each definition, so only certain
3210 // attributes are applied.
3211 wxTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle());
3212
3213 int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_RESET;
3214
3215 if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
3216 {
3217 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
3218
3219 wxRichTextRange range;
3220
3221 if (HasSelection())
3222 range = GetSelectionRange();
3223 else
3224 {
3225 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3226 range = wxRichTextRange(pos, pos+1);
3227 }
3228
3229 return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags);
3230 }
3231
3232 // Make sure the attr has the style name
3233 if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
3234 {
3235 attr.SetParagraphStyleName(def->GetName());
3236
3237 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3238 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3239 // to change its style independently.
3240 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
3241 }
3242 else
3243 attr.SetCharacterStyleName(def->GetName());
3244
3245 if (HasSelection())
3246 return SetStyleEx(GetSelectionRange(), attr, flags);
3247 else
3248 {
3249 wxRichTextAttr current = GetDefaultStyleEx();
3250 current.Apply(attr);
3251 SetAndShowDefaultStyle(current);
3252 return true;
3253 }
3254 }
3255
3256 /// Apply the style sheet to the buffer, for example if the styles have changed.
3257 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
3258 {
3259 if (!styleSheet)
3260 styleSheet = GetBuffer().GetStyleSheet();
3261 if (!styleSheet)
3262 return false;
3263
3264 if (GetBuffer().ApplyStyleSheet(styleSheet))
3265 {
3266 GetBuffer().Invalidate(wxRICHTEXT_ALL);
3267 Refresh(false);
3268 return true;
3269 }
3270 else
3271 return false;
3272 }
3273
3274 /// Sets the default style to the style under the cursor
3275 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3276 {
3277 wxTextAttr attr;
3278 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
3279
3280 // If at the start of a paragraph, use the next position.
3281 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3282
3283 if (GetUncombinedStyle(pos, attr))
3284 {
3285 SetDefaultStyle(attr);
3286 return true;
3287 }
3288
3289 return false;
3290 }
3291
3292 /// Returns the first visible position in the current view
3293 long wxRichTextCtrl::GetFirstVisiblePosition() const
3294 {
3295 wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y);
3296 if (line)
3297 return line->GetAbsoluteRange().GetStart();
3298 else
3299 return 0;
3300 }
3301
3302 /// Get the first visible point in the window
3303 wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const
3304 {
3305 int ppuX, ppuY;
3306 int startXUnits, startYUnits;
3307
3308 GetScrollPixelsPerUnit(& ppuX, & ppuY);
3309 GetViewStart(& startXUnits, & startYUnits);
3310
3311 return wxPoint(startXUnits * ppuX, startYUnits * ppuY);
3312 }
3313
3314 /// The adjusted caret position is the character position adjusted to take
3315 /// into account whether we're at the start of a paragraph, in which case
3316 /// style information should be taken from the next position, not current one.
3317 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
3318 {
3319 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPos+1);
3320
3321 if (para && (caretPos+1 == para->GetRange().GetStart()))
3322 caretPos ++;
3323 return caretPos;
3324 }
3325
3326 /// Get/set the selection range in character positions. -1, -1 means no selection.
3327 /// The range is in API convention, i.e. a single character selection is denoted
3328 /// by (n, n+1)
3329 wxRichTextRange wxRichTextCtrl::GetSelectionRange() const
3330 {
3331 wxRichTextRange range = GetInternalSelectionRange();
3332 if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1))
3333 range.SetEnd(range.GetEnd() + 1);
3334 return range;
3335 }
3336
3337 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range)
3338 {
3339 wxRichTextRange range1(range);
3340 if (range1 != wxRichTextRange(-2,-2) && range1 != wxRichTextRange(-1,-1) )
3341 range1.SetEnd(range1.GetEnd() - 1);
3342
3343 wxASSERT( range1.GetStart() > range1.GetEnd() );
3344
3345 SetInternalSelectionRange(range1);
3346 }
3347
3348 /// Set list style
3349 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3350 {
3351 return GetBuffer().SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel);
3352 }
3353
3354 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3355 {
3356 return GetBuffer().SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
3357 }
3358
3359 /// Clear list for given range
3360 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags)
3361 {
3362 return GetBuffer().ClearListStyle(range.ToInternal(), flags);
3363 }
3364
3365 /// Number/renumber any list elements in the given range
3366 bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3367 {
3368 return GetBuffer().NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel);
3369 }
3370
3371 bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3372 {
3373 return GetBuffer().NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
3374 }
3375
3376 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3377 bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel)
3378 {
3379 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel);
3380 }
3381
3382 bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel)
3383 {
3384 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel);
3385 }
3386
3387 /// Deletes the content in the given range
3388 bool wxRichTextCtrl::Delete(const wxRichTextRange& range)
3389 {
3390 return GetBuffer().DeleteRangeWithUndo(range.ToInternal(), this);
3391 }
3392
3393 const wxArrayString& wxRichTextCtrl::GetAvailableFontNames()
3394 {
3395 if (sm_availableFontNames.GetCount() == 0)
3396 {
3397 sm_availableFontNames = wxFontEnumerator::GetFacenames();
3398 sm_availableFontNames.Sort();
3399 }
3400 return sm_availableFontNames;
3401 }
3402
3403 void wxRichTextCtrl::ClearAvailableFontNames()
3404 {
3405 sm_availableFontNames.Clear();
3406 }
3407
3408 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
3409 {
3410 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3411
3412 wxTextAttrEx basicStyle = GetBasicStyle();
3413 basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3414 SetBasicStyle(basicStyle);
3415 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3416
3417 Refresh();
3418 }
3419
3420 // Refresh the area affected by a selection change
3421 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange& oldSelection, const wxRichTextRange& newSelection)
3422 {
3423 // Calculate the refresh rectangle - just the affected lines
3424 long firstPos, lastPos;
3425 if (oldSelection.GetStart() == -2 && newSelection.GetStart() != -2)
3426 {
3427 firstPos = newSelection.GetStart();
3428 lastPos = newSelection.GetEnd();
3429 }
3430 else if (oldSelection.GetStart() != -2 && newSelection.GetStart() == -2)
3431 {
3432 firstPos = oldSelection.GetStart();
3433 lastPos = oldSelection.GetEnd();
3434 }
3435 else if (oldSelection.GetStart() == -2 && newSelection.GetStart() == -2)
3436 {
3437 return false;
3438 }
3439 else
3440 {
3441 firstPos = wxMin(oldSelection.GetStart(), newSelection.GetStart());
3442 lastPos = wxMax(oldSelection.GetEnd(), newSelection.GetEnd());
3443 }
3444
3445 wxRichTextLine* firstLine = GetBuffer().GetLineAtPosition(firstPos);
3446 wxRichTextLine* lastLine = GetBuffer().GetLineAtPosition(lastPos);
3447
3448 if (firstLine && lastLine)
3449 {
3450 wxSize clientSize = GetClientSize();
3451 wxPoint pt1 = GetPhysicalPoint(firstLine->GetAbsolutePosition());
3452 wxPoint pt2 = GetPhysicalPoint(lastLine->GetAbsolutePosition()) + wxPoint(0, lastLine->GetSize().y);
3453
3454 pt1.x = 0;
3455 pt1.y = wxMax(0, pt1.y);
3456 pt2.x = 0;
3457 pt2.y = wxMin(clientSize.y, pt2.y);
3458
3459 wxRect rect(pt1, wxSize(clientSize.x, pt2.y - pt1.y));
3460 RefreshRect(rect, false);
3461 }
3462 else
3463 Refresh(false);
3464
3465 return true;
3466 }
3467
3468 #if wxRICHTEXT_USE_OWN_CARET
3469
3470 // ----------------------------------------------------------------------------
3471 // initialization and destruction
3472 // ----------------------------------------------------------------------------
3473
3474 void wxRichTextCaret::Init()
3475 {
3476 m_hasFocus = true;
3477
3478 m_xOld =
3479 m_yOld = -1;
3480 m_richTextCtrl = NULL;
3481 m_needsUpdate = false;
3482 }
3483
3484 wxRichTextCaret::~wxRichTextCaret()
3485 {
3486 }
3487
3488 // ----------------------------------------------------------------------------
3489 // showing/hiding/moving the caret (base class interface)
3490 // ----------------------------------------------------------------------------
3491
3492 void wxRichTextCaret::DoShow()
3493 {
3494 Refresh();
3495 }
3496
3497 void wxRichTextCaret::DoHide()
3498 {
3499 Refresh();
3500 }
3501
3502 void wxRichTextCaret::DoMove()
3503 {
3504 if (IsVisible())
3505 {
3506 Refresh();
3507
3508 if (m_xOld != -1 && m_yOld != -1)
3509 {
3510 if (m_richTextCtrl)
3511 {
3512 wxRect rect(GetPosition(), GetSize());
3513 m_richTextCtrl->RefreshRect(rect, false);
3514 }
3515 }
3516 }
3517
3518 m_xOld = m_x;
3519 m_yOld = m_y;
3520 }
3521
3522 void wxRichTextCaret::DoSize()
3523 {
3524 int countVisible = m_countVisible;
3525 if (countVisible > 0)
3526 {
3527 m_countVisible = 0;
3528 DoHide();
3529 }
3530
3531 if (countVisible > 0)
3532 {
3533 m_countVisible = countVisible;
3534 DoShow();
3535 }
3536 }
3537
3538 // ----------------------------------------------------------------------------
3539 // handling the focus
3540 // ----------------------------------------------------------------------------
3541
3542 void wxRichTextCaret::OnSetFocus()
3543 {
3544 m_hasFocus = true;
3545
3546 if ( IsVisible() )
3547 Refresh();
3548 }
3549
3550 void wxRichTextCaret::OnKillFocus()
3551 {
3552 m_hasFocus = false;
3553 }
3554
3555 // ----------------------------------------------------------------------------
3556 // drawing the caret
3557 // ----------------------------------------------------------------------------
3558
3559 void wxRichTextCaret::Refresh()
3560 {
3561 if (m_richTextCtrl)
3562 {
3563 wxRect rect(GetPosition(), GetSize());
3564 m_richTextCtrl->RefreshRect(rect, false);
3565 }
3566 }
3567
3568 void wxRichTextCaret::DoDraw(wxDC *dc)
3569 {
3570 dc->SetPen( *wxBLACK_PEN );
3571
3572 dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH));
3573 dc->SetPen(*wxBLACK_PEN);
3574
3575 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3576 // done under wxGTK - no idea why
3577 //dc->SetLogicalFunction(wxINVERT);
3578
3579 wxPoint pt(m_x, m_y);
3580
3581 if (m_richTextCtrl)
3582 {
3583 pt = m_richTextCtrl->GetLogicalPoint(pt);
3584 }
3585 dc->DrawRectangle(pt.x, pt.y, m_width, m_height);
3586 }
3587 #endif
3588 // wxRICHTEXT_USE_OWN_CARET
3589
3590 #endif
3591 // wxUSE_RICHTEXT