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