Fix for #4696: added SetDefaultStyleToCursorStyle() to SetInsertionPoint() so style...
[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/timer.h"
30 #include "wx/textfile.h"
31 #include "wx/ffile.h"
32 #include "wx/filename.h"
33 #include "wx/dcbuffer.h"
34 #include "wx/arrimpl.cpp"
35 #include "wx/fontenum.h"
36 #include "wx/accel.h"
37
38 // DLL options compatibility check:
39 #include "wx/app.h"
40 WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl")
41
42 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK, wxRichTextEvent );
43 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK, wxRichTextEvent );
44 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK, wxRichTextEvent );
45 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK, wxRichTextEvent );
46 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN, wxRichTextEvent );
47 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER, wxRichTextEvent );
48 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE, wxRichTextEvent );
49
50 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, wxRichTextEvent );
51 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED, wxRichTextEvent );
52 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING, wxRichTextEvent );
53 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED, wxRichTextEvent );
54
55 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, wxRichTextEvent );
56 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, wxRichTextEvent );
57 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED, wxRichTextEvent );
58 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED, wxRichTextEvent );
59 wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, wxRichTextEvent );
60
61 #if wxRICHTEXT_USE_OWN_CARET
62
63 /*!
64 * wxRichTextCaret
65 *
66 * This implements a non-flashing cursor in case there
67 * are platform-specific problems with the generic caret.
68 * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h.
69 */
70
71 class wxRichTextCaret;
72 class wxRichTextCaretTimer: public wxTimer
73 {
74 public:
75 wxRichTextCaretTimer(wxRichTextCaret* caret)
76 {
77 m_caret = caret;
78 }
79 virtual void Notify();
80 wxRichTextCaret* m_caret;
81 };
82
83 class wxRichTextCaret: public wxCaret
84 {
85 public:
86 // ctors
87 // -----
88 // default - use Create()
89 wxRichTextCaret(): m_timer(this) { Init(); }
90 // creates a block caret associated with the given window
91 wxRichTextCaret(wxRichTextCtrl *window, int width, int height)
92 : wxCaret(window, width, height), m_timer(this) { Init(); m_richTextCtrl = window; }
93 wxRichTextCaret(wxRichTextCtrl *window, const wxSize& size)
94 : wxCaret(window, size), m_timer(this) { Init(); m_richTextCtrl = window; }
95
96 virtual ~wxRichTextCaret();
97
98 // implementation
99 // --------------
100
101 // called by wxWindow (not using the event tables)
102 virtual void OnSetFocus();
103 virtual void OnKillFocus();
104
105 // draw the caret on the given DC
106 void DoDraw(wxDC *dc);
107
108 // get the visible count
109 int GetVisibleCount() const { return m_countVisible; }
110
111 // delay repositioning
112 bool GetNeedsUpdate() const { return m_needsUpdate; }
113 void SetNeedsUpdate(bool needsUpdate = true ) { m_needsUpdate = needsUpdate; }
114
115 void Notify();
116
117 protected:
118 virtual void DoShow();
119 virtual void DoHide();
120 virtual void DoMove();
121 virtual void DoSize();
122
123 // refresh the caret
124 void Refresh();
125
126 private:
127 void Init();
128
129 int m_xOld,
130 m_yOld;
131 bool m_hasFocus; // true => our window has focus
132 bool m_needsUpdate; // must be repositioned
133 bool m_flashOn;
134 wxRichTextCaretTimer m_timer;
135 wxRichTextCtrl* m_richTextCtrl;
136 };
137 #endif
138
139 IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl, wxTextCtrlBase )
140
141 IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent, wxNotifyEvent )
142
143 BEGIN_EVENT_TABLE( wxRichTextCtrl, wxTextCtrlBase )
144 EVT_PAINT(wxRichTextCtrl::OnPaint)
145 EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground)
146 EVT_IDLE(wxRichTextCtrl::OnIdle)
147 EVT_SCROLLWIN(wxRichTextCtrl::OnScroll)
148 EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick)
149 EVT_MOTION(wxRichTextCtrl::OnMoveMouse)
150 EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp)
151 EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick)
152 EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick)
153 EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick)
154 EVT_CHAR(wxRichTextCtrl::OnChar)
155 EVT_KEY_DOWN(wxRichTextCtrl::OnChar)
156 EVT_SIZE(wxRichTextCtrl::OnSize)
157 EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus)
158 EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus)
159 EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost)
160 EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu)
161 EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged)
162
163 EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo)
164 EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo)
165
166 EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo)
167 EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo)
168
169 EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy)
170 EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy)
171
172 EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste)
173 EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste)
174
175 EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut)
176 EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut)
177
178 EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear)
179 EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear)
180
181 EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll)
182 EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll)
183 END_EVENT_TABLE()
184
185 /*!
186 * wxRichTextCtrl
187 */
188
189 wxArrayString wxRichTextCtrl::sm_availableFontNames;
190
191 wxRichTextCtrl::wxRichTextCtrl()
192 : wxScrollHelper(this)
193 {
194 Init();
195 }
196
197 wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent,
198 wxWindowID id,
199 const wxString& value,
200 const wxPoint& pos,
201 const wxSize& size,
202 long style,
203 const wxValidator& validator,
204 const wxString& name)
205 : wxScrollHelper(this)
206 {
207 Init();
208 Create(parent, id, value, pos, size, style, validator, name);
209 }
210
211 /// Creation
212 bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style,
213 const wxValidator& validator, const wxString& name)
214 {
215 style |= wxVSCROLL;
216
217 if (!wxTextCtrlBase::Create(parent, id, pos, size,
218 style|wxFULL_REPAINT_ON_RESIZE,
219 validator, name))
220 return false;
221
222 if (!GetFont().Ok())
223 {
224 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
225 }
226
227 // No physical scrolling, so we can preserve margins
228 EnableScrolling(false, false);
229
230 if (style & wxTE_READONLY)
231 SetEditable(false);
232
233 // The base attributes must all have default values
234 wxTextAttr attributes;
235 attributes.SetFont(GetFont());
236 attributes.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
237 attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
238 attributes.SetLineSpacing(10);
239 attributes.SetParagraphSpacingAfter(10);
240 attributes.SetParagraphSpacingBefore(0);
241
242 SetBasicStyle(attributes);
243
244 // The default attributes will be merged with base attributes, so
245 // can be empty to begin with
246 wxTextAttr defaultAttributes;
247 SetDefaultStyle(defaultAttributes);
248
249 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
250 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
251
252 GetBuffer().Reset();
253 GetBuffer().SetRichTextCtrl(this);
254
255 #if wxRICHTEXT_USE_OWN_CARET
256 SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
257 #else
258 SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
259 #endif
260
261 // Tell the sizers to use the given or best size
262 SetInitialSize(size);
263
264 #if wxRICHTEXT_BUFFERED_PAINTING
265 // Create a buffer
266 RecreateBuffer(size);
267 #endif
268
269 m_textCursor = wxCursor(wxCURSOR_IBEAM);
270 m_urlCursor = wxCursor(wxCURSOR_HAND);
271
272 SetCursor(m_textCursor);
273
274 if (!value.IsEmpty())
275 SetValue(value);
276
277 GetBuffer().AddEventHandler(this);
278
279 // Accelerators
280 wxAcceleratorEntry entries[6];
281
282 entries[0].Set(wxACCEL_CMD, (int) 'C', wxID_COPY);
283 entries[1].Set(wxACCEL_CMD, (int) 'X', wxID_CUT);
284 entries[2].Set(wxACCEL_CMD, (int) 'V', wxID_PASTE);
285 entries[3].Set(wxACCEL_CMD, (int) 'A', wxID_SELECTALL);
286 entries[4].Set(wxACCEL_CMD, (int) 'Z', wxID_UNDO);
287 entries[5].Set(wxACCEL_CMD, (int) 'Y', wxID_REDO);
288
289 wxAcceleratorTable accel(6, entries);
290 SetAcceleratorTable(accel);
291
292 return true;
293 }
294
295 wxRichTextCtrl::~wxRichTextCtrl()
296 {
297 GetBuffer().RemoveEventHandler(this);
298
299 delete m_contextMenu;
300 }
301
302 /// Member initialisation
303 void wxRichTextCtrl::Init()
304 {
305 m_contextMenu = NULL;
306 m_caret = NULL;
307 m_caretPosition = -1;
308 m_selectionRange.SetRange(-2, -2);
309 m_selectionAnchor = -2;
310 m_editable = true;
311 m_caretAtLineStart = false;
312 m_dragging = false;
313 m_fullLayoutRequired = false;
314 m_fullLayoutTime = 0;
315 m_fullLayoutSavedPosition = 0;
316 m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD;
317 m_caretPositionForDefaultStyle = -2;
318 }
319
320 void wxRichTextCtrl::DoThaw()
321 {
322 if (GetBuffer().GetDirty())
323 LayoutContent();
324 else
325 SetupScrollbars();
326
327 wxWindow::DoThaw();
328 }
329
330 /// Clear all text
331 void wxRichTextCtrl::Clear()
332 {
333 m_buffer.ResetAndClearCommands();
334 m_buffer.SetDirty(true);
335 m_caretPosition = -1;
336 m_caretPositionForDefaultStyle = -2;
337 m_caretAtLineStart = false;
338 m_selectionRange.SetRange(-2, -2);
339
340 Scroll(0,0);
341
342 if (!IsFrozen())
343 {
344 LayoutContent();
345 Refresh(false);
346 }
347
348 wxTextCtrl::SendTextUpdatedEvent(this);
349 }
350
351 /// Painting
352 void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
353 {
354 #if !wxRICHTEXT_USE_OWN_CARET
355 if (GetCaret() && !IsFrozen())
356 GetCaret()->Hide();
357 #endif
358
359 {
360 #if wxRICHTEXT_BUFFERED_PAINTING
361 wxBufferedPaintDC dc(this, m_bufferBitmap);
362 #else
363 wxPaintDC dc(this);
364 #endif
365
366 if (IsFrozen())
367 return;
368
369 PrepareDC(dc);
370
371 dc.SetFont(GetFont());
372
373 // Paint the background
374 PaintBackground(dc);
375
376 // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize());
377
378 wxRect drawingArea(GetUpdateRegion().GetBox());
379 drawingArea.SetPosition(GetLogicalPoint(drawingArea.GetPosition()));
380
381 wxRect availableSpace(GetClientSize());
382 if (GetBuffer().GetDirty())
383 {
384 GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
385 GetBuffer().SetDirty(false);
386 SetupScrollbars();
387 }
388
389 wxRect clipRect(availableSpace);
390 clipRect.x += GetBuffer().GetLeftMargin();
391 clipRect.y += GetBuffer().GetTopMargin();
392 clipRect.width -= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin());
393 clipRect.height -= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin());
394 clipRect.SetPosition(GetLogicalPoint(clipRect.GetPosition()));
395 dc.SetClippingRegion(clipRect);
396
397 GetBuffer().Draw(dc, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */);
398
399 dc.DestroyClippingRegion();
400
401 #if wxRICHTEXT_USE_OWN_CARET
402 if (GetCaret()->IsVisible())
403 {
404 ((wxRichTextCaret*) GetCaret())->DoDraw(& dc);
405 }
406 #endif
407 }
408
409 #if !wxRICHTEXT_USE_OWN_CARET
410 if (GetCaret())
411 GetCaret()->Show();
412 PositionCaret();
413 #endif
414 }
415
416 // Empty implementation, to prevent flicker
417 void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
418 {
419 }
420
421 void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
422 {
423 if (GetCaret())
424 {
425 #if !wxRICHTEXT_USE_OWN_CARET
426 PositionCaret();
427 #endif
428 GetCaret()->Show();
429 }
430
431 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
432 // Work around dropouts when control is focused
433 if (!IsFrozen())
434 {
435 Refresh(false);
436 }
437 #endif
438 }
439
440 void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
441 {
442 if (GetCaret())
443 GetCaret()->Hide();
444
445 #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET
446 // Work around dropouts when control is focused
447 if (!IsFrozen())
448 {
449 Refresh(false);
450 }
451 #endif
452 }
453
454 void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
455 {
456 m_dragging = false;
457 }
458
459 /// Left-click
460 void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
461 {
462 SetFocus();
463
464 wxClientDC dc(this);
465 PrepareDC(dc);
466 dc.SetFont(GetFont());
467
468 long position = 0;
469 int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position);
470
471 if (hit != wxRICHTEXT_HITTEST_NONE)
472 {
473 m_dragStart = event.GetLogicalPosition(dc);
474 m_dragging = true;
475 CaptureMouse();
476
477 bool caretAtLineStart = false;
478
479 if (hit & wxRICHTEXT_HITTEST_BEFORE)
480 {
481 // If we're at the start of a line (but not first in para)
482 // then we should keep the caret showing at the start of the line
483 // by showing the m_caretAtLineStart flag.
484 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
485 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
486
487 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
488 caretAtLineStart = true;
489 position --;
490 }
491
492 long oldCaretPos = m_caretPosition;
493
494 MoveCaret(position, caretAtLineStart);
495 SetDefaultStyleToCursorStyle();
496
497 if (event.ShiftDown())
498 {
499 if (m_selectionRange.GetStart() == -2)
500 ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
501 else
502 ExtendSelection(m_caretPosition, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
503 }
504 else
505 SelectNone();
506 }
507
508 event.Skip();
509 }
510
511 /// Left-up
512 void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event)
513 {
514 if (m_dragging)
515 {
516 m_dragging = false;
517 if (GetCapture() == this)
518 ReleaseMouse();
519
520 // See if we clicked on a URL
521 wxClientDC dc(this);
522 PrepareDC(dc);
523 dc.SetFont(GetFont());
524
525 long position = 0;
526 wxPoint logicalPt = event.GetLogicalPosition(dc);
527 int hit = GetBuffer().HitTest(dc, logicalPt, position);
528
529 if ((hit != wxRICHTEXT_HITTEST_NONE) && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
530 {
531 wxRichTextEvent cmdEvent(
532 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK,
533 GetId());
534 cmdEvent.SetEventObject(this);
535 cmdEvent.SetPosition(m_caretPosition+1);
536
537 if (!GetEventHandler()->ProcessEvent(cmdEvent))
538 {
539 wxTextAttr attr;
540 if (GetStyle(position, attr))
541 {
542 if (attr.HasFlag(wxTEXT_ATTR_URL))
543 {
544 wxString urlTarget = attr.GetURL();
545 if (!urlTarget.IsEmpty())
546 {
547 wxMouseEvent mouseEvent(event);
548
549 long startPos = 0, endPos = 0;
550 wxRichTextObject* obj = GetBuffer().GetLeafObjectAtPosition(position);
551 if (obj)
552 {
553 startPos = obj->GetRange().GetStart();
554 endPos = obj->GetRange().GetEnd();
555 }
556
557 wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos);
558 InitCommandEvent(urlEvent);
559
560 urlEvent.SetString(urlTarget);
561
562 GetEventHandler()->ProcessEvent(urlEvent);
563 }
564 }
565 }
566 }
567 }
568 }
569 }
570
571 /// Left-click
572 void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event)
573 {
574 wxClientDC dc(this);
575 PrepareDC(dc);
576 dc.SetFont(GetFont());
577
578 long position = 0;
579 wxPoint logicalPt = event.GetLogicalPosition(dc);
580 int hit = GetBuffer().HitTest(dc, logicalPt, position);
581
582 // See if we need to change the cursor
583
584 {
585 if (hit != wxRICHTEXT_HITTEST_NONE && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
586 {
587 wxTextAttr attr;
588 if (GetStyle(position, attr))
589 {
590 if (attr.HasFlag(wxTEXT_ATTR_URL))
591 {
592 SetCursor(m_urlCursor);
593 }
594 else if (!attr.HasFlag(wxTEXT_ATTR_URL))
595 {
596 SetCursor(m_textCursor);
597 }
598 }
599 }
600 else
601 SetCursor(m_textCursor);
602 }
603
604 if (!event.Dragging())
605 {
606 event.Skip();
607 return;
608 }
609
610 if (m_dragging && hit != wxRICHTEXT_HITTEST_NONE)
611 {
612 // TODO: test closeness
613
614 bool caretAtLineStart = false;
615
616 if (hit & wxRICHTEXT_HITTEST_BEFORE)
617 {
618 // If we're at the start of a line (but not first in para)
619 // then we should keep the caret showing at the start of the line
620 // by showing the m_caretAtLineStart flag.
621 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
622 wxRichTextLine* line = GetBuffer().GetLineAtPosition(position);
623
624 if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position)
625 caretAtLineStart = true;
626 position --;
627 }
628
629 if (m_caretPosition != position)
630 {
631 ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN);
632
633 MoveCaret(position, caretAtLineStart);
634 SetDefaultStyleToCursorStyle();
635 }
636 }
637 }
638
639 /// Right-click
640 void wxRichTextCtrl::OnRightClick(wxMouseEvent& event)
641 {
642 SetFocus();
643
644 wxRichTextEvent cmdEvent(
645 wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK,
646 GetId());
647 cmdEvent.SetEventObject(this);
648 cmdEvent.SetPosition(m_caretPosition+1);
649
650 if (!GetEventHandler()->ProcessEvent(cmdEvent))
651 event.Skip();
652 }
653
654 /// Left-double-click
655 void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& WXUNUSED(event))
656 {
657 wxRichTextEvent cmdEvent(
658 wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK,
659 GetId());
660 cmdEvent.SetEventObject(this);
661 cmdEvent.SetPosition(m_caretPosition+1);
662
663 if (!GetEventHandler()->ProcessEvent(cmdEvent))
664 {
665 SelectWord(GetCaretPosition()+1);
666 }
667 }
668
669 /// Middle-click
670 void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event)
671 {
672 wxRichTextEvent cmdEvent(
673 wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK,
674 GetId());
675 cmdEvent.SetEventObject(this);
676 cmdEvent.SetPosition(m_caretPosition+1);
677
678 if (!GetEventHandler()->ProcessEvent(cmdEvent))
679 event.Skip();
680 }
681
682 /// Key press
683 void wxRichTextCtrl::OnChar(wxKeyEvent& event)
684 {
685 int flags = 0;
686 if (event.CmdDown())
687 flags |= wxRICHTEXT_CTRL_DOWN;
688 if (event.ShiftDown())
689 flags |= wxRICHTEXT_SHIFT_DOWN;
690 if (event.AltDown())
691 flags |= wxRICHTEXT_ALT_DOWN;
692
693 if (event.GetEventType() == wxEVT_KEY_DOWN)
694 {
695 if (event.IsKeyInCategory(WXK_CATEGORY_NAVIGATION))
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_WINDOWS_LEFT:
772 {
773 return;
774 }
775 default:
776 {
777 }
778 }
779
780 // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
781 if (event.CmdDown() && event.GetKeyCode() == WXK_BACK)
782 {
783 BeginBatchUndo(_("Delete Text"));
784
785 long newPos = m_caretPosition;
786
787 bool processed = DeleteSelectedContent(& newPos);
788
789 // Submit range in character positions, which are greater than caret positions,
790 // so subtract 1 for deleted character and add 1 for conversion to character position.
791 if (newPos > -1)
792 {
793 if (event.CmdDown())
794 {
795 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
796 if (pos < newPos)
797 {
798 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this);
799 processed = true;
800 }
801 }
802
803 if (!processed)
804 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this);
805 }
806
807 EndBatchUndo();
808
809 if (GetLastPosition() == -1)
810 {
811 GetBuffer().Reset();
812
813 m_caretPosition = -1;
814 PositionCaret();
815 SetDefaultStyleToCursorStyle();
816 }
817
818 ScrollIntoView(m_caretPosition, WXK_LEFT);
819
820 wxRichTextEvent cmdEvent(
821 wxEVT_COMMAND_RICHTEXT_DELETE,
822 GetId());
823 cmdEvent.SetEventObject(this);
824 cmdEvent.SetFlags(flags);
825 cmdEvent.SetPosition(m_caretPosition+1);
826 GetEventHandler()->ProcessEvent(cmdEvent);
827
828 Update();
829 }
830 else
831 event.Skip();
832
833 return;
834 }
835
836 // all the other keys modify the controls contents which shouldn't be
837 // possible if we're read-only
838 if ( !IsEditable() )
839 {
840 event.Skip();
841 return;
842 }
843
844 if (event.GetKeyCode() == WXK_RETURN)
845 {
846 BeginBatchUndo(_("Insert Text"));
847
848 long newPos = m_caretPosition;
849
850 DeleteSelectedContent(& newPos);
851
852 if (event.ShiftDown())
853 {
854 wxString text;
855 text = wxRichTextLineBreakChar;
856 GetBuffer().InsertTextWithUndo(newPos+1, text, this);
857 m_caretAtLineStart = true;
858 PositionCaret();
859 }
860 else
861 GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE|wxRICHTEXT_INSERT_INTERACTIVE);
862
863 EndBatchUndo();
864 SetDefaultStyleToCursorStyle();
865
866 ScrollIntoView(m_caretPosition, WXK_RIGHT);
867
868 wxRichTextEvent cmdEvent(
869 wxEVT_COMMAND_RICHTEXT_RETURN,
870 GetId());
871 cmdEvent.SetEventObject(this);
872 cmdEvent.SetFlags(flags);
873 cmdEvent.SetPosition(newPos+1);
874
875 if (!GetEventHandler()->ProcessEvent(cmdEvent))
876 {
877 // Generate conventional event
878 wxCommandEvent textEvent(wxEVT_COMMAND_TEXT_ENTER, GetId());
879 InitCommandEvent(textEvent);
880
881 GetEventHandler()->ProcessEvent(textEvent);
882 }
883 Update();
884 }
885 else if (event.GetKeyCode() == WXK_BACK)
886 {
887 BeginBatchUndo(_("Delete Text"));
888
889 long newPos = m_caretPosition;
890
891 bool processed = DeleteSelectedContent(& newPos);
892
893 // Submit range in character positions, which are greater than caret positions,
894 // so subtract 1 for deleted character and add 1 for conversion to character position.
895 if (newPos > -1)
896 {
897 if (event.CmdDown())
898 {
899 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
900 if (pos < newPos)
901 {
902 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this);
903 processed = true;
904 }
905 }
906
907 if (!processed)
908 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this);
909 }
910
911 EndBatchUndo();
912
913 if (GetLastPosition() == -1)
914 {
915 GetBuffer().Reset();
916
917 m_caretPosition = -1;
918 PositionCaret();
919 SetDefaultStyleToCursorStyle();
920 }
921
922 ScrollIntoView(m_caretPosition, WXK_LEFT);
923
924 wxRichTextEvent cmdEvent(
925 wxEVT_COMMAND_RICHTEXT_DELETE,
926 GetId());
927 cmdEvent.SetEventObject(this);
928 cmdEvent.SetFlags(flags);
929 cmdEvent.SetPosition(m_caretPosition+1);
930 GetEventHandler()->ProcessEvent(cmdEvent);
931
932 Update();
933 }
934 else if (event.GetKeyCode() == WXK_DELETE)
935 {
936 BeginBatchUndo(_("Delete Text"));
937
938 long newPos = m_caretPosition;
939
940 bool processed = DeleteSelectedContent(& newPos);
941
942 // Submit range in character positions, which are greater than caret positions,
943 if (newPos < GetBuffer().GetRange().GetEnd()+1)
944 {
945 if (event.CmdDown())
946 {
947 long pos = wxRichTextCtrl::FindNextWordPosition(1);
948 if (pos != -1 && (pos > newPos))
949 {
950 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos+1, pos), this);
951 processed = true;
952 }
953 }
954
955 if (!processed)
956 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos+1, newPos+1), this);
957 }
958
959 EndBatchUndo();
960
961 if (GetLastPosition() == -1)
962 {
963 GetBuffer().Reset();
964
965 m_caretPosition = -1;
966 PositionCaret();
967 SetDefaultStyleToCursorStyle();
968 }
969
970 wxRichTextEvent cmdEvent(
971 wxEVT_COMMAND_RICHTEXT_DELETE,
972 GetId());
973 cmdEvent.SetEventObject(this);
974 cmdEvent.SetFlags(flags);
975 cmdEvent.SetPosition(m_caretPosition+1);
976 GetEventHandler()->ProcessEvent(cmdEvent);
977
978 Update();
979 }
980 else
981 {
982 long keycode = event.GetKeyCode();
983 switch ( keycode )
984 {
985 case WXK_ESCAPE:
986 {
987 event.Skip();
988 return;
989 }
990
991 default:
992 {
993 #ifdef __WXMAC__
994 if (event.CmdDown())
995 #else
996 // Fixes AltGr+key with European input languages on Windows
997 if ((event.CmdDown() && !event.AltDown()) || (event.AltDown() && !event.CmdDown()))
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 SetDefaultStyleToCursorStyle();
2521 }
2522
2523 void wxRichTextCtrl::SetInsertionPointEnd()
2524 {
2525 long pos = GetLastPosition();
2526 SetInsertionPoint(pos);
2527 }
2528
2529 long wxRichTextCtrl::GetInsertionPoint() const
2530 {
2531 return m_caretPosition+1;
2532 }
2533
2534 wxTextPos wxRichTextCtrl::GetLastPosition() const
2535 {
2536 return GetBuffer().GetRange().GetEnd();
2537 }
2538
2539 // If the return values from and to are the same, there is no
2540 // selection.
2541 void wxRichTextCtrl::GetSelection(long* from, long* to) const
2542 {
2543 *from = m_selectionRange.GetStart();
2544 *to = m_selectionRange.GetEnd();
2545 if ((*to) != -1 && (*to) != -2)
2546 (*to) ++;
2547 }
2548
2549 bool wxRichTextCtrl::IsEditable() const
2550 {
2551 return m_editable;
2552 }
2553
2554 // ----------------------------------------------------------------------------
2555 // selection
2556 // ----------------------------------------------------------------------------
2557
2558 void wxRichTextCtrl::SetSelection(long from, long to)
2559 {
2560 // if from and to are both -1, it means (in wxWidgets) that all text should
2561 // be selected.
2562 if ( (from == -1) && (to == -1) )
2563 {
2564 from = 0;
2565 to = GetLastPosition()+1;
2566 }
2567
2568 if (from == to)
2569 {
2570 SelectNone();
2571 }
2572 else
2573 {
2574 wxRichTextRange oldSelection = m_selectionRange;
2575 m_selectionAnchor = from;
2576 m_selectionRange.SetRange(from, to-1);
2577 if (from > -2)
2578 m_caretPosition = from-1;
2579
2580 RefreshForSelectionChange(oldSelection, m_selectionRange);
2581 PositionCaret();
2582 }
2583 }
2584
2585 // ----------------------------------------------------------------------------
2586 // Editing
2587 // ----------------------------------------------------------------------------
2588
2589 void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to),
2590 const wxString& value)
2591 {
2592 BeginBatchUndo(_("Replace"));
2593
2594 DeleteSelectedContent();
2595
2596 DoWriteText(value, SetValue_SelectionOnly);
2597
2598 EndBatchUndo();
2599 }
2600
2601 void wxRichTextCtrl::Remove(long from, long to)
2602 {
2603 SelectNone();
2604
2605 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to-1), this);
2606
2607 LayoutContent();
2608 if (!IsFrozen())
2609 Refresh(false);
2610 }
2611
2612 bool wxRichTextCtrl::IsModified() const
2613 {
2614 return m_buffer.IsModified();
2615 }
2616
2617 void wxRichTextCtrl::MarkDirty()
2618 {
2619 m_buffer.Modify(true);
2620 }
2621
2622 void wxRichTextCtrl::DiscardEdits()
2623 {
2624 m_caretPositionForDefaultStyle = -2;
2625 m_buffer.Modify(false);
2626 m_buffer.GetCommandProcessor()->ClearCommands();
2627 }
2628
2629 int wxRichTextCtrl::GetNumberOfLines() const
2630 {
2631 return GetBuffer().GetParagraphCount();
2632 }
2633
2634 // ----------------------------------------------------------------------------
2635 // Positions <-> coords
2636 // ----------------------------------------------------------------------------
2637
2638 long wxRichTextCtrl::XYToPosition(long x, long y) const
2639 {
2640 return GetBuffer().XYToPosition(x, y);
2641 }
2642
2643 bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
2644 {
2645 return GetBuffer().PositionToXY(pos, x, y);
2646 }
2647
2648 // ----------------------------------------------------------------------------
2649 //
2650 // ----------------------------------------------------------------------------
2651
2652 void wxRichTextCtrl::ShowPosition(long pos)
2653 {
2654 if (!IsPositionVisible(pos))
2655 ScrollIntoView(pos-1, WXK_DOWN);
2656 }
2657
2658 int wxRichTextCtrl::GetLineLength(long lineNo) const
2659 {
2660 return GetBuffer().GetParagraphLength(lineNo);
2661 }
2662
2663 wxString wxRichTextCtrl::GetLineText(long lineNo) const
2664 {
2665 return GetBuffer().GetParagraphText(lineNo);
2666 }
2667
2668 // ----------------------------------------------------------------------------
2669 // Undo/redo
2670 // ----------------------------------------------------------------------------
2671
2672 void wxRichTextCtrl::Undo()
2673 {
2674 if (CanUndo())
2675 {
2676 GetCommandProcessor()->Undo();
2677 }
2678 }
2679
2680 void wxRichTextCtrl::Redo()
2681 {
2682 if (CanRedo())
2683 {
2684 GetCommandProcessor()->Redo();
2685 }
2686 }
2687
2688 bool wxRichTextCtrl::CanUndo() const
2689 {
2690 return GetCommandProcessor()->CanUndo();
2691 }
2692
2693 bool wxRichTextCtrl::CanRedo() const
2694 {
2695 return GetCommandProcessor()->CanRedo();
2696 }
2697
2698 // ----------------------------------------------------------------------------
2699 // implementation details
2700 // ----------------------------------------------------------------------------
2701
2702 void wxRichTextCtrl::Command(wxCommandEvent& event)
2703 {
2704 SetValue(event.GetString());
2705 GetEventHandler()->ProcessEvent(event);
2706 }
2707
2708 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event)
2709 {
2710 // By default, load the first file into the text window.
2711 if (event.GetNumberOfFiles() > 0)
2712 {
2713 LoadFile(event.GetFiles()[0]);
2714 }
2715 }
2716
2717 wxSize wxRichTextCtrl::DoGetBestSize() const
2718 {
2719 return wxSize(10, 10);
2720 }
2721
2722 // ----------------------------------------------------------------------------
2723 // standard handlers for standard edit menu events
2724 // ----------------------------------------------------------------------------
2725
2726 void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2727 {
2728 Cut();
2729 }
2730
2731 void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
2732 {
2733 DeleteSelection();
2734 }
2735
2736 void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2737 {
2738 Copy();
2739 }
2740
2741 void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2742 {
2743 Paste();
2744 }
2745
2746 void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2747 {
2748 Undo();
2749 }
2750
2751 void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2752 {
2753 Redo();
2754 }
2755
2756 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2757 {
2758 event.Enable( CanCut() );
2759 }
2760
2761 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2762 {
2763 event.Enable( CanCopy() );
2764 }
2765
2766 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
2767 {
2768 event.Enable( CanDeleteSelection() );
2769 }
2770
2771 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2772 {
2773 event.Enable( CanPaste() );
2774 }
2775
2776 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2777 {
2778 event.Enable( CanUndo() );
2779 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2780 }
2781
2782 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2783 {
2784 event.Enable( CanRedo() );
2785 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2786 }
2787
2788 void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
2789 {
2790 SelectAll();
2791 }
2792
2793 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
2794 {
2795 event.Enable(GetLastPosition() > 0);
2796 }
2797
2798 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event)
2799 {
2800 if (event.GetEventObject() != this)
2801 {
2802 event.Skip();
2803 return;
2804 }
2805
2806 if (!m_contextMenu)
2807 {
2808 m_contextMenu = new wxMenu;
2809 m_contextMenu->Append(wxID_UNDO, _("&Undo"));
2810 m_contextMenu->Append(wxID_REDO, _("&Redo"));
2811 m_contextMenu->AppendSeparator();
2812 m_contextMenu->Append(wxID_CUT, _("Cu&t"));
2813 m_contextMenu->Append(wxID_COPY, _("&Copy"));
2814 m_contextMenu->Append(wxID_PASTE, _("&Paste"));
2815 m_contextMenu->Append(wxID_CLEAR, _("&Delete"));
2816 m_contextMenu->AppendSeparator();
2817 m_contextMenu->Append(wxID_SELECTALL, _("Select &All"));
2818 }
2819 PopupMenu(m_contextMenu);
2820 return;
2821 }
2822
2823 bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
2824 {
2825 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttr(style));
2826 }
2827
2828 bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxTextAttr& style)
2829 {
2830 return GetBuffer().SetStyle(range.ToInternal(), style);
2831 }
2832
2833 // extended style setting operation with flags including:
2834 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2835 // see richtextbuffer.h for more details.
2836
2837 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxTextAttr& style, int flags)
2838 {
2839 return GetBuffer().SetStyle(range.ToInternal(), style, flags);
2840 }
2841
2842 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2843 {
2844 return GetBuffer().SetDefaultStyle(wxTextAttr(style));
2845 }
2846
2847 const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const
2848 {
2849 return GetBuffer().GetDefaultStyle();
2850 }
2851
2852 bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style)
2853 {
2854 return GetBuffer().GetStyle(position, style);
2855 }
2856
2857 // get the common set of styles for the range
2858 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttr& style)
2859 {
2860 return GetBuffer().GetStyleForRange(range.ToInternal(), style);
2861 }
2862
2863 /// Get the content (uncombined) attributes for this position.
2864 bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttr& style)
2865 {
2866 return GetBuffer().GetUncombinedStyle(position, style);
2867 }
2868
2869 /// Set font, and also the buffer attributes
2870 bool wxRichTextCtrl::SetFont(const wxFont& font)
2871 {
2872 wxTextCtrlBase::SetFont(font);
2873
2874 wxTextAttr attr = GetBuffer().GetAttributes();
2875 attr.SetFont(font);
2876 GetBuffer().SetBasicStyle(attr);
2877
2878 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2879 Refresh(false);
2880
2881 return true;
2882 }
2883
2884 /// Transform logical to physical
2885 wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const
2886 {
2887 wxPoint pt;
2888 CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y);
2889
2890 return pt;
2891 }
2892
2893 /// Transform physical to logical
2894 wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
2895 {
2896 wxPoint pt;
2897 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
2898
2899 return pt;
2900 }
2901
2902 /// Position the caret
2903 void wxRichTextCtrl::PositionCaret()
2904 {
2905 if (!GetCaret())
2906 return;
2907
2908 //wxLogDebug(wxT("PositionCaret"));
2909
2910 wxRect caretRect;
2911 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect))
2912 {
2913 wxPoint newPt = caretRect.GetPosition();
2914 wxSize newSz = caretRect.GetSize();
2915 wxPoint pt = GetPhysicalPoint(newPt);
2916 if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz)
2917 {
2918 GetCaret()->Hide();
2919 if (GetCaret()->GetSize() != newSz)
2920 GetCaret()->SetSize(newSz);
2921
2922 int halfSize = newSz.y/2;
2923 // If the caret is beyond the margin, hide it by moving it out of the way
2924 if (((pt.y + halfSize) < GetBuffer().GetTopMargin()) || ((pt.y + halfSize) > (GetClientSize().y - GetBuffer().GetBottomMargin())))
2925 pt.y = -200;
2926
2927 GetCaret()->Move(pt);
2928 GetCaret()->Show();
2929 }
2930 }
2931 }
2932
2933 /// Get the caret height and position for the given character position
2934 bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect)
2935 {
2936 wxClientDC dc(this);
2937 dc.SetFont(GetFont());
2938
2939 PrepareDC(dc);
2940
2941 wxPoint pt;
2942 int height = 0;
2943
2944 if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart))
2945 {
2946 // Caret height can't be zero
2947 if (height == 0)
2948 height = dc.GetCharHeight();
2949
2950 rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height));
2951 return true;
2952 }
2953
2954 return false;
2955 }
2956
2957 /// Gets the line for the visible caret position. If the caret is
2958 /// shown at the very end of the line, it means the next character is actually
2959 /// on the following line. So let's get the line we're expecting to find
2960 /// if this is the case.
2961 wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
2962 {
2963 wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true);
2964 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true);
2965 if (line)
2966 {
2967 wxRichTextRange lineRange = line->GetAbsoluteRange();
2968 if (caretPosition == lineRange.GetStart()-1 &&
2969 (para->GetRange().GetStart() != lineRange.GetStart()))
2970 {
2971 if (!m_caretAtLineStart)
2972 line = GetBuffer().GetLineAtPosition(caretPosition-1, true);
2973 }
2974 }
2975 return line;
2976 }
2977
2978
2979 /// Move the caret to the given character position
2980 bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart)
2981 {
2982 if (GetBuffer().GetDirty())
2983 LayoutContent();
2984
2985 if (pos <= GetBuffer().GetRange().GetEnd())
2986 {
2987 SetCaretPosition(pos, showAtLineStart);
2988
2989 PositionCaret();
2990
2991 return true;
2992 }
2993 else
2994 return false;
2995 }
2996
2997 /// Layout the buffer: which we must do before certain operations, such as
2998 /// setting the caret position.
2999 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
3000 {
3001 if (GetBuffer().GetDirty() || onlyVisibleRect)
3002 {
3003 wxRect availableSpace(GetClientSize());
3004 if (availableSpace.width == 0)
3005 availableSpace.width = 10;
3006 if (availableSpace.height == 0)
3007 availableSpace.height = 10;
3008
3009 int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT;
3010 if (onlyVisibleRect)
3011 {
3012 flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
3013 availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
3014 }
3015
3016 wxClientDC dc(this);
3017 dc.SetFont(GetFont());
3018
3019 PrepareDC(dc);
3020
3021 GetBuffer().Defragment();
3022 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
3023 GetBuffer().Layout(dc, availableSpace, flags);
3024 GetBuffer().SetDirty(false);
3025
3026 if (!IsFrozen())
3027 SetupScrollbars();
3028 }
3029
3030 return true;
3031 }
3032
3033 /// Is all of the selection bold?
3034 bool wxRichTextCtrl::IsSelectionBold()
3035 {
3036 if (HasSelection())
3037 {
3038 wxTextAttr attr;
3039 wxRichTextRange range = GetSelectionRange();
3040 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
3041 attr.SetFontWeight(wxFONTWEIGHT_BOLD);
3042
3043 return HasCharacterAttributes(range, attr);
3044 }
3045 else
3046 {
3047 // If no selection, then we need to combine current style with default style
3048 // to see what the effect would be if we started typing.
3049 wxTextAttr attr;
3050 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
3051
3052 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3053 if (GetStyle(pos, attr))
3054 {
3055 if (IsDefaultStyleShowing())
3056 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
3057 return attr.GetFontWeight() == wxFONTWEIGHT_BOLD;
3058 }
3059 }
3060 return false;
3061 }
3062
3063 /// Is all of the selection italics?
3064 bool wxRichTextCtrl::IsSelectionItalics()
3065 {
3066 if (HasSelection())
3067 {
3068 wxRichTextRange range = GetSelectionRange();
3069 wxTextAttr attr;
3070 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
3071 attr.SetFontStyle(wxFONTSTYLE_ITALIC);
3072
3073 return HasCharacterAttributes(range, attr);
3074 }
3075 else
3076 {
3077 // If no selection, then we need to combine current style with default style
3078 // to see what the effect would be if we started typing.
3079 wxTextAttr attr;
3080 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
3081
3082 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3083 if (GetStyle(pos, attr))
3084 {
3085 if (IsDefaultStyleShowing())
3086 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
3087 return attr.GetFontStyle() == wxFONTSTYLE_ITALIC;
3088 }
3089 }
3090 return false;
3091 }
3092
3093 /// Is all of the selection underlined?
3094 bool wxRichTextCtrl::IsSelectionUnderlined()
3095 {
3096 if (HasSelection())
3097 {
3098 wxRichTextRange range = GetSelectionRange();
3099 wxTextAttr attr;
3100 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3101 attr.SetFontUnderlined(true);
3102
3103 return HasCharacterAttributes(range, attr);
3104 }
3105 else
3106 {
3107 // If no selection, then we need to combine current style with default style
3108 // to see what the effect would be if we started typing.
3109 wxTextAttr attr;
3110 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3111 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3112
3113 if (GetStyle(pos, attr))
3114 {
3115 if (IsDefaultStyleShowing())
3116 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
3117 return attr.GetFontUnderlined();
3118 }
3119 }
3120 return false;
3121 }
3122
3123 /// Apply bold to the selection
3124 bool wxRichTextCtrl::ApplyBoldToSelection()
3125 {
3126 wxTextAttr attr;
3127 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
3128 attr.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL : wxFONTWEIGHT_BOLD);
3129
3130 if (HasSelection())
3131 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3132 else
3133 {
3134 wxRichTextAttr current = GetDefaultStyleEx();
3135 current.Apply(attr);
3136 SetAndShowDefaultStyle(current);
3137 }
3138 return true;
3139 }
3140
3141 /// Apply italic to the selection
3142 bool wxRichTextCtrl::ApplyItalicToSelection()
3143 {
3144 wxTextAttr attr;
3145 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
3146 attr.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL : wxFONTSTYLE_ITALIC);
3147
3148 if (HasSelection())
3149 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3150 else
3151 {
3152 wxRichTextAttr current = GetDefaultStyleEx();
3153 current.Apply(attr);
3154 SetAndShowDefaultStyle(current);
3155 }
3156 return true;
3157 }
3158
3159 /// Apply underline to the selection
3160 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3161 {
3162 wxTextAttr attr;
3163 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3164 attr.SetFontUnderlined(!IsSelectionUnderlined());
3165
3166 if (HasSelection())
3167 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3168 else
3169 {
3170 wxRichTextAttr current = GetDefaultStyleEx();
3171 current.Apply(attr);
3172 SetAndShowDefaultStyle(current);
3173 }
3174 return true;
3175 }
3176
3177 /// Is all of the selection aligned according to the specified flag?
3178 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
3179 {
3180 wxRichTextRange range;
3181 if (HasSelection())
3182 range = GetSelectionRange();
3183 else
3184 range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3185
3186 wxTextAttr attr;
3187 attr.SetAlignment(alignment);
3188
3189 return HasParagraphAttributes(range, attr);
3190 }
3191
3192 /// Apply alignment to the selection
3193 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
3194 {
3195 wxTextAttr attr;
3196 attr.SetAlignment(alignment);
3197 if (HasSelection())
3198 return SetStyle(GetSelectionRange(), attr);
3199 else
3200 {
3201 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3202 if (para)
3203 return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY);
3204 }
3205 return true;
3206 }
3207
3208 /// Apply a named style to the selection
3209 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
3210 {
3211 // Flags are defined within each definition, so only certain
3212 // attributes are applied.
3213 wxTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle());
3214
3215 int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_RESET;
3216
3217 if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
3218 {
3219 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
3220
3221 wxRichTextRange range;
3222
3223 if (HasSelection())
3224 range = GetSelectionRange();
3225 else
3226 {
3227 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3228 range = wxRichTextRange(pos, pos+1);
3229 }
3230
3231 return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags);
3232 }
3233
3234 // Make sure the attr has the style name
3235 if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
3236 {
3237 attr.SetParagraphStyleName(def->GetName());
3238
3239 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3240 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3241 // to change its style independently.
3242 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
3243 }
3244 else
3245 attr.SetCharacterStyleName(def->GetName());
3246
3247 if (HasSelection())
3248 return SetStyleEx(GetSelectionRange(), attr, flags);
3249 else
3250 {
3251 wxRichTextAttr current = GetDefaultStyleEx();
3252 current.Apply(attr);
3253 SetAndShowDefaultStyle(current);
3254 return true;
3255 }
3256 }
3257
3258 /// Apply the style sheet to the buffer, for example if the styles have changed.
3259 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
3260 {
3261 if (!styleSheet)
3262 styleSheet = GetBuffer().GetStyleSheet();
3263 if (!styleSheet)
3264 return false;
3265
3266 if (GetBuffer().ApplyStyleSheet(styleSheet))
3267 {
3268 GetBuffer().Invalidate(wxRICHTEXT_ALL);
3269 Refresh(false);
3270 return true;
3271 }
3272 else
3273 return false;
3274 }
3275
3276 /// Sets the default style to the style under the cursor
3277 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3278 {
3279 wxTextAttr attr;
3280 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
3281
3282 // If at the start of a paragraph, use the next position.
3283 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3284
3285 if (GetUncombinedStyle(pos, attr))
3286 {
3287 SetDefaultStyle(attr);
3288 return true;
3289 }
3290
3291 return false;
3292 }
3293
3294 /// Returns the first visible position in the current view
3295 long wxRichTextCtrl::GetFirstVisiblePosition() const
3296 {
3297 wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y);
3298 if (line)
3299 return line->GetAbsoluteRange().GetStart();
3300 else
3301 return 0;
3302 }
3303
3304 /// Get the first visible point in the window
3305 wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const
3306 {
3307 int ppuX, ppuY;
3308 int startXUnits, startYUnits;
3309
3310 GetScrollPixelsPerUnit(& ppuX, & ppuY);
3311 GetViewStart(& startXUnits, & startYUnits);
3312
3313 return wxPoint(startXUnits * ppuX, startYUnits * ppuY);
3314 }
3315
3316 /// The adjusted caret position is the character position adjusted to take
3317 /// into account whether we're at the start of a paragraph, in which case
3318 /// style information should be taken from the next position, not current one.
3319 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
3320 {
3321 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPos+1);
3322
3323 if (para && (caretPos+1 == para->GetRange().GetStart()))
3324 caretPos ++;
3325 return caretPos;
3326 }
3327
3328 /// Get/set the selection range in character positions. -1, -1 means no selection.
3329 /// The range is in API convention, i.e. a single character selection is denoted
3330 /// by (n, n+1)
3331 wxRichTextRange wxRichTextCtrl::GetSelectionRange() const
3332 {
3333 wxRichTextRange range = GetInternalSelectionRange();
3334 if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1))
3335 range.SetEnd(range.GetEnd() + 1);
3336 return range;
3337 }
3338
3339 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range)
3340 {
3341 wxRichTextRange range1(range);
3342 if (range1 != wxRichTextRange(-2,-2) && range1 != wxRichTextRange(-1,-1) )
3343 range1.SetEnd(range1.GetEnd() - 1);
3344
3345 wxASSERT( range1.GetStart() > range1.GetEnd() );
3346
3347 SetInternalSelectionRange(range1);
3348 }
3349
3350 /// Set list style
3351 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3352 {
3353 return GetBuffer().SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel);
3354 }
3355
3356 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3357 {
3358 return GetBuffer().SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
3359 }
3360
3361 /// Clear list for given range
3362 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags)
3363 {
3364 return GetBuffer().ClearListStyle(range.ToInternal(), flags);
3365 }
3366
3367 /// Number/renumber any list elements in the given range
3368 bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3369 {
3370 return GetBuffer().NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel);
3371 }
3372
3373 bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3374 {
3375 return GetBuffer().NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
3376 }
3377
3378 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3379 bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel)
3380 {
3381 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel);
3382 }
3383
3384 bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel)
3385 {
3386 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel);
3387 }
3388
3389 /// Deletes the content in the given range
3390 bool wxRichTextCtrl::Delete(const wxRichTextRange& range)
3391 {
3392 return GetBuffer().DeleteRangeWithUndo(range.ToInternal(), this);
3393 }
3394
3395 const wxArrayString& wxRichTextCtrl::GetAvailableFontNames()
3396 {
3397 if (sm_availableFontNames.GetCount() == 0)
3398 {
3399 sm_availableFontNames = wxFontEnumerator::GetFacenames();
3400 sm_availableFontNames.Sort();
3401 }
3402 return sm_availableFontNames;
3403 }
3404
3405 void wxRichTextCtrl::ClearAvailableFontNames()
3406 {
3407 sm_availableFontNames.Clear();
3408 }
3409
3410 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
3411 {
3412 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3413
3414 wxTextAttrEx basicStyle = GetBasicStyle();
3415 basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3416 SetBasicStyle(basicStyle);
3417 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3418
3419 Refresh();
3420 }
3421
3422 // Refresh the area affected by a selection change
3423 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange& oldSelection, const wxRichTextRange& newSelection)
3424 {
3425 // Calculate the refresh rectangle - just the affected lines
3426 long firstPos, lastPos;
3427 if (oldSelection.GetStart() == -2 && newSelection.GetStart() != -2)
3428 {
3429 firstPos = newSelection.GetStart();
3430 lastPos = newSelection.GetEnd();
3431 }
3432 else if (oldSelection.GetStart() != -2 && newSelection.GetStart() == -2)
3433 {
3434 firstPos = oldSelection.GetStart();
3435 lastPos = oldSelection.GetEnd();
3436 }
3437 else if (oldSelection.GetStart() == -2 && newSelection.GetStart() == -2)
3438 {
3439 return false;
3440 }
3441 else
3442 {
3443 firstPos = wxMin(oldSelection.GetStart(), newSelection.GetStart());
3444 lastPos = wxMax(oldSelection.GetEnd(), newSelection.GetEnd());
3445 }
3446
3447 wxRichTextLine* firstLine = GetBuffer().GetLineAtPosition(firstPos);
3448 wxRichTextLine* lastLine = GetBuffer().GetLineAtPosition(lastPos);
3449
3450 if (firstLine && lastLine)
3451 {
3452 wxSize clientSize = GetClientSize();
3453 wxPoint pt1 = GetPhysicalPoint(firstLine->GetAbsolutePosition());
3454 wxPoint pt2 = GetPhysicalPoint(lastLine->GetAbsolutePosition()) + wxPoint(0, lastLine->GetSize().y);
3455
3456 pt1.x = 0;
3457 pt1.y = wxMax(0, pt1.y);
3458 pt2.x = 0;
3459 pt2.y = wxMin(clientSize.y, pt2.y);
3460
3461 wxRect rect(pt1, wxSize(clientSize.x, pt2.y - pt1.y));
3462 RefreshRect(rect, false);
3463 }
3464 else
3465 Refresh(false);
3466
3467 return true;
3468 }
3469
3470 #if wxRICHTEXT_USE_OWN_CARET
3471
3472 // ----------------------------------------------------------------------------
3473 // initialization and destruction
3474 // ----------------------------------------------------------------------------
3475
3476 void wxRichTextCaret::Init()
3477 {
3478 m_hasFocus = true;
3479
3480 m_xOld =
3481 m_yOld = -1;
3482 m_richTextCtrl = NULL;
3483 m_needsUpdate = false;
3484 m_flashOn = true;
3485 }
3486
3487 wxRichTextCaret::~wxRichTextCaret()
3488 {
3489 if (m_timer.IsRunning())
3490 m_timer.Stop();
3491 }
3492
3493 // ----------------------------------------------------------------------------
3494 // showing/hiding/moving the caret (base class interface)
3495 // ----------------------------------------------------------------------------
3496
3497 void wxRichTextCaret::DoShow()
3498 {
3499 m_flashOn = true;
3500
3501 if (!m_timer.IsRunning())
3502 m_timer.Start(GetBlinkTime());
3503
3504 Refresh();
3505 }
3506
3507 void wxRichTextCaret::DoHide()
3508 {
3509 if (m_timer.IsRunning())
3510 m_timer.Stop();
3511
3512 Refresh();
3513 }
3514
3515 void wxRichTextCaret::DoMove()
3516 {
3517 if (IsVisible())
3518 {
3519 Refresh();
3520
3521 if (m_xOld != -1 && m_yOld != -1)
3522 {
3523 if (m_richTextCtrl)
3524 {
3525 wxRect rect(GetPosition(), GetSize());
3526 m_richTextCtrl->RefreshRect(rect, false);
3527 }
3528 }
3529 }
3530
3531 m_xOld = m_x;
3532 m_yOld = m_y;
3533 }
3534
3535 void wxRichTextCaret::DoSize()
3536 {
3537 int countVisible = m_countVisible;
3538 if (countVisible > 0)
3539 {
3540 m_countVisible = 0;
3541 DoHide();
3542 }
3543
3544 if (countVisible > 0)
3545 {
3546 m_countVisible = countVisible;
3547 DoShow();
3548 }
3549 }
3550
3551 // ----------------------------------------------------------------------------
3552 // handling the focus
3553 // ----------------------------------------------------------------------------
3554
3555 void wxRichTextCaret::OnSetFocus()
3556 {
3557 m_hasFocus = true;
3558
3559 if ( IsVisible() )
3560 Refresh();
3561 }
3562
3563 void wxRichTextCaret::OnKillFocus()
3564 {
3565 m_hasFocus = false;
3566 }
3567
3568 // ----------------------------------------------------------------------------
3569 // drawing the caret
3570 // ----------------------------------------------------------------------------
3571
3572 void wxRichTextCaret::Refresh()
3573 {
3574 if (m_richTextCtrl)
3575 {
3576 wxRect rect(GetPosition(), GetSize());
3577 m_richTextCtrl->RefreshRect(rect, false);
3578 }
3579 }
3580
3581 void wxRichTextCaret::DoDraw(wxDC *dc)
3582 {
3583 dc->SetPen( *wxBLACK_PEN );
3584
3585 dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH));
3586 dc->SetPen(*wxBLACK_PEN);
3587
3588 wxPoint pt(m_x, m_y);
3589
3590 if (m_richTextCtrl)
3591 {
3592 pt = m_richTextCtrl->GetLogicalPoint(pt);
3593 }
3594 if (IsVisible() && m_flashOn)
3595 dc->DrawRectangle(pt.x, pt.y, m_width, m_height);
3596 }
3597
3598 void wxRichTextCaret::Notify()
3599 {
3600 m_flashOn = !m_flashOn;
3601 Refresh();
3602 }
3603
3604 void wxRichTextCaretTimer::Notify()
3605 {
3606 m_caret->Notify();
3607 }
3608 #endif
3609 // wxRICHTEXT_USE_OWN_CARET
3610
3611 #endif
3612 // wxUSE_RICHTEXT