]> git.saurik.com Git - wxWidgets.git/blob - src/richtext/richtextctrl.cpp
ff48ec1d7d2b76c01b21f1372bce7da22fa3b324
[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 wxRichTextRange oldSelection = m_selectionRange;
1101
1102 // If not currently selecting, start selecting
1103 if (m_selectionRange.GetStart() == -2)
1104 {
1105 m_selectionAnchor = oldPos;
1106
1107 if (oldPos > newPos)
1108 m_selectionRange.SetRange(newPos+1, oldPos);
1109 else
1110 m_selectionRange.SetRange(oldPos+1, newPos);
1111 }
1112 else
1113 {
1114 // Always ensure that the selection range start is greater than
1115 // the end.
1116 if (newPos > m_selectionAnchor)
1117 m_selectionRange.SetRange(m_selectionAnchor+1, newPos);
1118 else
1119 m_selectionRange.SetRange(newPos+1, m_selectionAnchor);
1120 }
1121
1122 RefreshForSelectionChange(oldSelection, m_selectionRange);
1123
1124 if (m_selectionRange.GetStart() > m_selectionRange.GetEnd())
1125 {
1126 wxLogDebug(wxT("Strange selection range"));
1127 }
1128
1129 return true;
1130 }
1131 else
1132 return false;
1133 }
1134
1135 /// Scroll into view, returning true if we scrolled.
1136 /// This takes a _caret_ position.
1137 bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode)
1138 {
1139 wxRichTextLine* line = GetVisibleLineForCaretPosition(position);
1140
1141 if (!line)
1142 return false;
1143
1144 int ppuX, ppuY;
1145 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1146
1147 int startXUnits, startYUnits;
1148 GetViewStart(& startXUnits, & startYUnits);
1149 int startY = startYUnits * ppuY;
1150
1151 int sx = 0, sy = 0;
1152 GetVirtualSize(& sx, & sy);
1153 int sxUnits = 0;
1154 int syUnits = 0;
1155 if (ppuY != 0)
1156 syUnits = sy/ppuY;
1157
1158 wxRect rect = line->GetRect();
1159
1160 bool scrolled = false;
1161
1162 wxSize clientSize = GetClientSize();
1163
1164 // Going down
1165 if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN ||
1166 keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT ||
1167 keyCode == WXK_END || keyCode == WXK_NUMPAD_END ||
1168 keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN)
1169 {
1170 if ((rect.y + rect.height) > (clientSize.y + startY))
1171 {
1172 // Make it scroll so this item is at the bottom
1173 // of the window
1174 int y = rect.y - (clientSize.y - rect.height);
1175 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1176
1177 // If we're still off the screen, scroll another line down
1178 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1179 yUnits ++;
1180
1181 if (startYUnits != yUnits)
1182 {
1183 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1184 scrolled = true;
1185 }
1186 }
1187 else if (rect.y < startY)
1188 {
1189 // Make it scroll so this item is at the top
1190 // of the window
1191 int y = rect.y ;
1192 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1193
1194 if (startYUnits != yUnits)
1195 {
1196 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1197 scrolled = true;
1198 }
1199 }
1200 }
1201 // Going up
1202 else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP ||
1203 keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT ||
1204 keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME ||
1205 keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP )
1206 {
1207 if (rect.y < startY)
1208 {
1209 // Make it scroll so this item is at the top
1210 // of the window
1211 int y = rect.y ;
1212 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1213
1214 if (startYUnits != yUnits)
1215 {
1216 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1217 scrolled = true;
1218 }
1219 }
1220 else if ((rect.y + rect.height) > (clientSize.y + startY))
1221 {
1222 // Make it scroll so this item is at the bottom
1223 // of the window
1224 int y = rect.y - (clientSize.y - rect.height);
1225 int yUnits = (int) (0.5 + ((float) y)/(float) ppuY);
1226
1227 // If we're still off the screen, scroll another line down
1228 if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY)))
1229 yUnits ++;
1230
1231 if (startYUnits != yUnits)
1232 {
1233 SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits);
1234 scrolled = true;
1235 }
1236 }
1237 }
1238
1239 #if !wxRICHTEXT_USE_OWN_CARET
1240 if (scrolled)
1241 #endif
1242 PositionCaret();
1243
1244 return scrolled;
1245 }
1246
1247 /// Is the given position visible on the screen?
1248 bool wxRichTextCtrl::IsPositionVisible(long pos) const
1249 {
1250 wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1);
1251
1252 if (!line)
1253 return false;
1254
1255 int ppuX, ppuY;
1256 GetScrollPixelsPerUnit(& ppuX, & ppuY);
1257
1258 int startX, startY;
1259 GetViewStart(& startX, & startY);
1260 startX = 0;
1261 startY = startY * ppuY;
1262
1263 wxRect rect = line->GetRect();
1264 wxSize clientSize = GetClientSize();
1265
1266 return (rect.GetBottom() > startY) && (rect.GetTop() < (startY + clientSize.y));
1267 }
1268
1269 void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart)
1270 {
1271 m_caretPosition = position;
1272 m_caretAtLineStart = showAtLineStart;
1273 }
1274
1275 /// Move caret one visual step forward: this may mean setting a flag
1276 /// and keeping the same position if we're going from the end of one line
1277 /// to the start of the next, which may be the exact same caret position.
1278 void wxRichTextCtrl::MoveCaretForward(long oldPosition)
1279 {
1280 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
1281
1282 // Only do the check if we're not at the end of the paragraph (where things work OK
1283 // anyway)
1284 if (para && (oldPosition != para->GetRange().GetEnd() - 1))
1285 {
1286 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
1287
1288 if (line)
1289 {
1290 wxRichTextRange lineRange = line->GetAbsoluteRange();
1291
1292 // We're at the end of a line. See whether we need to
1293 // stay at the same actual caret position but change visual
1294 // position, or not.
1295 if (oldPosition == lineRange.GetEnd())
1296 {
1297 if (m_caretAtLineStart)
1298 {
1299 // We're already at the start of the line, so actually move on now.
1300 m_caretPosition = oldPosition + 1;
1301 m_caretAtLineStart = false;
1302 }
1303 else
1304 {
1305 // We're showing at the end of the line, so keep to
1306 // the same position but indicate that we're to show
1307 // at the start of the next line.
1308 m_caretPosition = oldPosition;
1309 m_caretAtLineStart = true;
1310 }
1311 SetDefaultStyleToCursorStyle();
1312 return;
1313 }
1314 }
1315 }
1316 m_caretPosition ++;
1317 SetDefaultStyleToCursorStyle();
1318 }
1319
1320 /// Move caret one visual step backward: this may mean setting a flag
1321 /// and keeping the same position if we're going from the end of one line
1322 /// to the start of the next, which may be the exact same caret position.
1323 void wxRichTextCtrl::MoveCaretBack(long oldPosition)
1324 {
1325 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition);
1326
1327 // Only do the check if we're not at the start of the paragraph (where things work OK
1328 // anyway)
1329 if (para && (oldPosition != para->GetRange().GetStart()))
1330 {
1331 wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition);
1332
1333 if (line)
1334 {
1335 wxRichTextRange lineRange = line->GetAbsoluteRange();
1336
1337 // We're at the start of a line. See whether we need to
1338 // stay at the same actual caret position but change visual
1339 // position, or not.
1340 if (oldPosition == lineRange.GetStart())
1341 {
1342 m_caretPosition = oldPosition-1;
1343 m_caretAtLineStart = true;
1344 return;
1345 }
1346 else if (oldPosition == lineRange.GetEnd())
1347 {
1348 if (m_caretAtLineStart)
1349 {
1350 // We're at the start of the line, so keep the same caret position
1351 // but clear the start-of-line flag.
1352 m_caretPosition = oldPosition;
1353 m_caretAtLineStart = false;
1354 }
1355 else
1356 {
1357 // We're showing at the end of the line, so go back
1358 // to the previous character position.
1359 m_caretPosition = oldPosition - 1;
1360 }
1361 SetDefaultStyleToCursorStyle();
1362 return;
1363 }
1364 }
1365 }
1366 m_caretPosition --;
1367 SetDefaultStyleToCursorStyle();
1368 }
1369
1370 /// Move right
1371 bool wxRichTextCtrl::MoveRight(int noPositions, int flags)
1372 {
1373 long endPos = GetBuffer().GetRange().GetEnd();
1374
1375 if (m_caretPosition + noPositions < endPos)
1376 {
1377 long oldPos = m_caretPosition;
1378 long newPos = m_caretPosition + noPositions;
1379
1380 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1381 if (!extendSel)
1382 SelectNone();
1383
1384 // Determine by looking at oldPos and m_caretPosition whether
1385 // we moved from the end of a line to the start of the next line, in which case
1386 // we want to adjust the caret position such that it is positioned at the
1387 // start of the next line, rather than jumping past the first character of the
1388 // line.
1389 if (noPositions == 1 && !extendSel)
1390 MoveCaretForward(oldPos);
1391 else
1392 SetCaretPosition(newPos);
1393
1394 PositionCaret();
1395 SetDefaultStyleToCursorStyle();
1396
1397 return true;
1398 }
1399 else
1400 return false;
1401 }
1402
1403 /// Move left
1404 bool wxRichTextCtrl::MoveLeft(int noPositions, int flags)
1405 {
1406 long startPos = -1;
1407
1408 if (m_caretPosition > startPos - noPositions + 1)
1409 {
1410 long oldPos = m_caretPosition;
1411 long newPos = m_caretPosition - noPositions;
1412 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1413 if (!extendSel)
1414 SelectNone();
1415
1416 if (noPositions == 1 && !extendSel)
1417 MoveCaretBack(oldPos);
1418 else
1419 SetCaretPosition(newPos);
1420
1421 PositionCaret();
1422 SetDefaultStyleToCursorStyle();
1423
1424 return true;
1425 }
1426 else
1427 return false;
1428 }
1429
1430 /// Move up
1431 bool wxRichTextCtrl::MoveUp(int noLines, int flags)
1432 {
1433 return MoveDown(- noLines, flags);
1434 }
1435
1436 /// Move up
1437 bool wxRichTextCtrl::MoveDown(int noLines, int flags)
1438 {
1439 if (!GetCaret())
1440 return false;
1441
1442 long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart);
1443 wxPoint pt = GetCaret()->GetPosition();
1444 long newLine = lineNumber + noLines;
1445
1446 if (lineNumber != -1)
1447 {
1448 if (noLines > 0)
1449 {
1450 long lastLine = GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd());
1451
1452 if (newLine > lastLine)
1453 return false;
1454 }
1455 else
1456 {
1457 if (newLine < 0)
1458 return false;
1459 }
1460 }
1461
1462 wxRichTextLine* lineObj = GetBuffer().GetLineForVisibleLineNumber(newLine);
1463 if (lineObj)
1464 {
1465 pt.y = lineObj->GetAbsolutePosition().y + 2;
1466 }
1467 else
1468 return false;
1469
1470 long newPos = 0;
1471 wxClientDC dc(this);
1472 PrepareDC(dc);
1473 dc.SetFont(GetFont());
1474
1475 int hitTest = GetBuffer().HitTest(dc, pt, newPos);
1476
1477 if (hitTest != wxRICHTEXT_HITTEST_NONE)
1478 {
1479 // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE,
1480 // we want to be at the end of the last line but with m_caretAtLineStart set to true,
1481 // so we view the caret at the start of the line.
1482 bool caretLineStart = false;
1483 if (hitTest & wxRICHTEXT_HITTEST_BEFORE)
1484 {
1485 wxRichTextLine* thisLine = GetBuffer().GetLineAtPosition(newPos-1);
1486 wxRichTextRange lineRange;
1487 if (thisLine)
1488 lineRange = thisLine->GetAbsoluteRange();
1489
1490 if (thisLine && (newPos-1) == lineRange.GetEnd())
1491 {
1492 newPos --;
1493 caretLineStart = true;
1494 }
1495 else
1496 {
1497 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(newPos);
1498 if (para && para->GetRange().GetStart() == newPos)
1499 newPos --;
1500 }
1501 }
1502
1503 long newSelEnd = newPos;
1504
1505 bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags);
1506 if (!extendSel)
1507 SelectNone();
1508
1509 SetCaretPosition(newPos, caretLineStart);
1510 PositionCaret();
1511 SetDefaultStyleToCursorStyle();
1512
1513 return true;
1514 }
1515
1516 return false;
1517 }
1518
1519 /// Move to the end of the paragraph
1520 bool wxRichTextCtrl::MoveToParagraphEnd(int flags)
1521 {
1522 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1523 if (para)
1524 {
1525 long newPos = para->GetRange().GetEnd() - 1;
1526 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1527 if (!extendSel)
1528 SelectNone();
1529
1530 SetCaretPosition(newPos);
1531 PositionCaret();
1532 SetDefaultStyleToCursorStyle();
1533
1534 return true;
1535 }
1536
1537 return false;
1538 }
1539
1540 /// Move to the start of the paragraph
1541 bool wxRichTextCtrl::MoveToParagraphStart(int flags)
1542 {
1543 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true);
1544 if (para)
1545 {
1546 long newPos = para->GetRange().GetStart() - 1;
1547 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1548 if (!extendSel)
1549 SelectNone();
1550
1551 SetCaretPosition(newPos);
1552 PositionCaret();
1553 SetDefaultStyleToCursorStyle();
1554
1555 return true;
1556 }
1557
1558 return false;
1559 }
1560
1561 /// Move to the end of the line
1562 bool wxRichTextCtrl::MoveToLineEnd(int flags)
1563 {
1564 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1565
1566 if (line)
1567 {
1568 wxRichTextRange lineRange = line->GetAbsoluteRange();
1569 long newPos = lineRange.GetEnd();
1570 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1571 if (!extendSel)
1572 SelectNone();
1573
1574 SetCaretPosition(newPos);
1575 PositionCaret();
1576 SetDefaultStyleToCursorStyle();
1577
1578 return true;
1579 }
1580
1581 return false;
1582 }
1583
1584 /// Move to the start of the line
1585 bool wxRichTextCtrl::MoveToLineStart(int flags)
1586 {
1587 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1588 if (line)
1589 {
1590 wxRichTextRange lineRange = line->GetAbsoluteRange();
1591 long newPos = lineRange.GetStart()-1;
1592
1593 bool extendSel = ExtendSelection(m_caretPosition, newPos, flags);
1594 if (!extendSel)
1595 SelectNone();
1596
1597 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line);
1598
1599 SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart());
1600 PositionCaret();
1601 SetDefaultStyleToCursorStyle();
1602
1603 return true;
1604 }
1605
1606 return false;
1607 }
1608
1609 /// Move to the start of the buffer
1610 bool wxRichTextCtrl::MoveHome(int flags)
1611 {
1612 if (m_caretPosition != -1)
1613 {
1614 bool extendSel = ExtendSelection(m_caretPosition, -1, flags);
1615 if (!extendSel)
1616 SelectNone();
1617
1618 SetCaretPosition(-1);
1619 PositionCaret();
1620 SetDefaultStyleToCursorStyle();
1621
1622 return true;
1623 }
1624 else
1625 return false;
1626 }
1627
1628 /// Move to the end of the buffer
1629 bool wxRichTextCtrl::MoveEnd(int flags)
1630 {
1631 long endPos = GetBuffer().GetRange().GetEnd()-1;
1632
1633 if (m_caretPosition != endPos)
1634 {
1635 bool extendSel = ExtendSelection(m_caretPosition, endPos, flags);
1636 if (!extendSel)
1637 SelectNone();
1638
1639 SetCaretPosition(endPos);
1640 PositionCaret();
1641 SetDefaultStyleToCursorStyle();
1642
1643 return true;
1644 }
1645 else
1646 return false;
1647 }
1648
1649 /// Move noPages pages up
1650 bool wxRichTextCtrl::PageUp(int noPages, int flags)
1651 {
1652 return PageDown(- noPages, flags);
1653 }
1654
1655 /// Move noPages pages down
1656 bool wxRichTextCtrl::PageDown(int noPages, int flags)
1657 {
1658 // Calculate which line occurs noPages * screen height further down.
1659 wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition);
1660 if (line)
1661 {
1662 wxSize clientSize = GetClientSize();
1663 int newY = line->GetAbsolutePosition().y + noPages*clientSize.y;
1664
1665 wxRichTextLine* newLine = GetBuffer().GetLineAtYPosition(newY);
1666 if (newLine)
1667 {
1668 wxRichTextRange lineRange = newLine->GetAbsoluteRange();
1669 long pos = lineRange.GetStart()-1;
1670 if (pos != m_caretPosition)
1671 {
1672 wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine);
1673
1674 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1675 if (!extendSel)
1676 SelectNone();
1677
1678 SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart());
1679 PositionCaret();
1680 SetDefaultStyleToCursorStyle();
1681
1682 return true;
1683 }
1684 }
1685 }
1686
1687 return false;
1688 }
1689
1690 static bool wxRichTextCtrlIsWhitespace(const wxString& str)
1691 {
1692 return str == wxT(" ") || str == wxT("\t");
1693 }
1694
1695 // Finds the caret position for the next word
1696 long wxRichTextCtrl::FindNextWordPosition(int direction) const
1697 {
1698 long endPos = GetBuffer().GetRange().GetEnd();
1699
1700 if (direction > 0)
1701 {
1702 long i = m_caretPosition+1+direction; // +1 for conversion to character pos
1703
1704 // First skip current text to space
1705 while (i < endPos && i > -1)
1706 {
1707 // i is in character, not caret positions
1708 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1709 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1710 if (line && (i == line->GetAbsoluteRange().GetEnd()))
1711 {
1712 break;
1713 }
1714 else if (!wxRichTextCtrlIsWhitespace(text) && !text.empty())
1715 i += direction;
1716 else
1717 {
1718 break;
1719 }
1720 }
1721 while (i < endPos && i > -1)
1722 {
1723 // i is in character, not caret positions
1724 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1725 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1726 if (line && (i == line->GetAbsoluteRange().GetEnd()))
1727 return wxMax(-1, i);
1728
1729 if (text.empty()) // End of paragraph, or maybe an image
1730 return wxMax(-1, i - 1);
1731 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
1732 i += direction;
1733 else
1734 {
1735 // Convert to caret position
1736 return wxMax(-1, i - 1);
1737 }
1738 }
1739 if (i >= endPos)
1740 return endPos-1;
1741 return i-1;
1742 }
1743 else
1744 {
1745 long i = m_caretPosition;
1746
1747 // First skip white space
1748 while (i < endPos && i > -1)
1749 {
1750 // i is in character, not caret positions
1751 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1752 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1753
1754 if (text.empty() || (line && (i == line->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image
1755 break;
1756 else if (wxRichTextCtrlIsWhitespace(text) || text.empty())
1757 i += direction;
1758 else
1759 break;
1760 }
1761 // Next skip current text to space
1762 while (i < endPos && i > -1)
1763 {
1764 // i is in character, not caret positions
1765 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i));
1766 wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false);
1767 if (line && line->GetAbsoluteRange().GetStart() == i)
1768 return i-1;
1769
1770 if (!wxRichTextCtrlIsWhitespace(text) /* && !text.empty() */)
1771 i += direction;
1772 else
1773 {
1774 return i;
1775 }
1776 }
1777 if (i < -1)
1778 return -1;
1779 return i;
1780 }
1781 }
1782
1783 /// Move n words left
1784 bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags)
1785 {
1786 long pos = FindNextWordPosition(-1);
1787 if (pos != m_caretPosition)
1788 {
1789 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
1790
1791 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1792 if (!extendSel)
1793 SelectNone();
1794
1795 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1796 PositionCaret();
1797 SetDefaultStyleToCursorStyle();
1798
1799 return true;
1800 }
1801
1802 return false;
1803 }
1804
1805 /// Move n words right
1806 bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags)
1807 {
1808 long pos = FindNextWordPosition(1);
1809 if (pos != m_caretPosition)
1810 {
1811 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true);
1812
1813 bool extendSel = ExtendSelection(m_caretPosition, pos, flags);
1814 if (!extendSel)
1815 SelectNone();
1816
1817 SetCaretPosition(pos, para->GetRange().GetStart() != pos);
1818 PositionCaret();
1819 SetDefaultStyleToCursorStyle();
1820
1821 return true;
1822 }
1823
1824 return false;
1825 }
1826
1827 /// Sizing
1828 void wxRichTextCtrl::OnSize(wxSizeEvent& event)
1829 {
1830 // Only do sizing optimization for large buffers
1831 if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold)
1832 {
1833 m_fullLayoutRequired = true;
1834 m_fullLayoutTime = wxGetLocalTimeMillis();
1835 m_fullLayoutSavedPosition = GetFirstVisiblePosition();
1836 LayoutContent(true /* onlyVisibleRect */);
1837 }
1838 else
1839 GetBuffer().Invalidate(wxRICHTEXT_ALL);
1840
1841 #if wxRICHTEXT_BUFFERED_PAINTING
1842 RecreateBuffer();
1843 #endif
1844
1845 event.Skip();
1846 }
1847
1848
1849 /// Idle-time processing
1850 void wxRichTextCtrl::OnIdle(wxIdleEvent& event)
1851 {
1852 #if wxRICHTEXT_USE_OWN_CARET
1853 if (((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
1854 {
1855 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate(false);
1856 PositionCaret();
1857 GetCaret()->Show();
1858 }
1859 #endif
1860
1861 const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL;
1862
1863 if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval)))
1864 {
1865 m_fullLayoutRequired = false;
1866 m_fullLayoutTime = 0;
1867 GetBuffer().Invalidate(wxRICHTEXT_ALL);
1868 ShowPosition(m_fullLayoutSavedPosition);
1869 Refresh(false);
1870 }
1871
1872 if (m_caretPositionForDefaultStyle != -2)
1873 {
1874 // If the caret position has changed, no longer reflect the default style
1875 // in the UI.
1876 if (GetCaretPosition() != m_caretPositionForDefaultStyle)
1877 m_caretPositionForDefaultStyle = -2;
1878 }
1879
1880 event.Skip();
1881 }
1882
1883 /// Scrolling
1884 void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
1885 {
1886 #if wxRICHTEXT_USE_OWN_CARET
1887 if (!((wxRichTextCaret*) GetCaret())->GetNeedsUpdate())
1888 {
1889 GetCaret()->Hide();
1890 ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate();
1891 }
1892 #endif
1893
1894 event.Skip();
1895 }
1896
1897 /// Set up scrollbars, e.g. after a resize
1898 void wxRichTextCtrl::SetupScrollbars(bool atTop)
1899 {
1900 if (IsFrozen())
1901 return;
1902
1903 if (GetBuffer().IsEmpty())
1904 {
1905 SetScrollbars(0, 0, 0, 0, 0, 0);
1906 return;
1907 }
1908
1909 // TODO: reimplement scrolling so we scroll by line, not by fixed number
1910 // of pixels. See e.g. wxVScrolledWindow for ideas.
1911 int pixelsPerUnit = 5;
1912 wxSize clientSize = GetClientSize();
1913
1914 int maxHeight = GetBuffer().GetCachedSize().y;
1915
1916 // Round up so we have at least maxHeight pixels
1917 int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5);
1918
1919 int startX = 0, startY = 0;
1920 if (!atTop)
1921 GetViewStart(& startX, & startY);
1922
1923 int maxPositionX = 0;
1924 int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5);
1925
1926 int newStartX = wxMin(maxPositionX, startX);
1927 int newStartY = wxMin(maxPositionY, startY);
1928
1929 int oldPPUX, oldPPUY;
1930 int oldStartX, oldStartY;
1931 int oldVirtualSizeX = 0, oldVirtualSizeY = 0;
1932 GetScrollPixelsPerUnit(& oldPPUX, & oldPPUY);
1933 GetViewStart(& oldStartX, & oldStartY);
1934 GetVirtualSize(& oldVirtualSizeX, & oldVirtualSizeY);
1935 if (oldPPUY > 0)
1936 oldVirtualSizeY /= oldPPUY;
1937
1938 if (oldPPUX == 0 && oldPPUY == pixelsPerUnit && oldVirtualSizeY == unitsY && oldStartX == newStartX && oldStartY == newStartY)
1939 return;
1940
1941 // Don't set scrollbars if there were none before, and there will be none now.
1942 if (oldPPUY != 0 && (oldVirtualSizeY < clientSize.y) && (unitsY*pixelsPerUnit < clientSize.y))
1943 return;
1944
1945 // Move to previous scroll position if
1946 // possible
1947 SetScrollbars(0, pixelsPerUnit, 0, unitsY, newStartX, newStartY);
1948 }
1949
1950 /// Paint the background
1951 void wxRichTextCtrl::PaintBackground(wxDC& dc)
1952 {
1953 wxColour backgroundColour = GetBackgroundColour();
1954 if (!backgroundColour.Ok())
1955 backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1956
1957 // Clear the background
1958 dc.SetBrush(wxBrush(backgroundColour));
1959 dc.SetPen(*wxTRANSPARENT_PEN);
1960 wxRect windowRect(GetClientSize());
1961 windowRect.x -= 2; windowRect.y -= 2;
1962 windowRect.width += 4; windowRect.height += 4;
1963
1964 // We need to shift the rectangle to take into account
1965 // scrolling. Converting device to logical coordinates.
1966 CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y);
1967 dc.DrawRectangle(windowRect);
1968 }
1969
1970 #if wxRICHTEXT_BUFFERED_PAINTING
1971 /// Recreate buffer bitmap if necessary
1972 bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
1973 {
1974 wxSize sz = size;
1975 if (sz == wxDefaultSize)
1976 sz = GetClientSize();
1977
1978 if (sz.x < 1 || sz.y < 1)
1979 return false;
1980
1981 if (!m_bufferBitmap.Ok() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y)
1982 m_bufferBitmap = wxBitmap(sz.x, sz.y);
1983 return m_bufferBitmap.Ok();
1984 }
1985 #endif
1986
1987 // ----------------------------------------------------------------------------
1988 // file IO functions
1989 // ----------------------------------------------------------------------------
1990
1991 bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
1992 {
1993 bool success = GetBuffer().LoadFile(filename, (wxRichTextFileType)fileType);
1994 if (success)
1995 m_filename = filename;
1996
1997 DiscardEdits();
1998 SetInsertionPoint(0);
1999 LayoutContent();
2000 PositionCaret();
2001 SetupScrollbars(true);
2002 Refresh(false);
2003 wxTextCtrl::SendTextUpdatedEvent(this);
2004
2005 if (success)
2006 return true;
2007 else
2008 {
2009 wxLogError(_("File couldn't be loaded."));
2010
2011 return false;
2012 }
2013 }
2014
2015 bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
2016 {
2017 if (GetBuffer().SaveFile(filename, (wxRichTextFileType)fileType))
2018 {
2019 m_filename = filename;
2020
2021 DiscardEdits();
2022
2023 return true;
2024 }
2025
2026 wxLogError(_("The text couldn't be saved."));
2027
2028 return false;
2029 }
2030
2031 // ----------------------------------------------------------------------------
2032 // wxRichTextCtrl specific functionality
2033 // ----------------------------------------------------------------------------
2034
2035 /// Add a new paragraph of text to the end of the buffer
2036 wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
2037 {
2038 wxRichTextRange range = GetBuffer().AddParagraph(text);
2039 LayoutContent();
2040 return range;
2041 }
2042
2043 /// Add an image
2044 wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
2045 {
2046 wxRichTextRange range = GetBuffer().AddImage(image);
2047 LayoutContent();
2048 return range;
2049 }
2050
2051 // ----------------------------------------------------------------------------
2052 // selection and ranges
2053 // ----------------------------------------------------------------------------
2054
2055 void wxRichTextCtrl::SelectAll()
2056 {
2057 SetSelection(0, GetLastPosition()+1);
2058 m_selectionAnchor = -1;
2059 }
2060
2061 /// Select none
2062 void wxRichTextCtrl::SelectNone()
2063 {
2064 if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
2065 {
2066 wxRichTextRange oldSelection = m_selectionRange;
2067
2068 m_selectionRange = wxRichTextRange(-2, -2);
2069
2070 RefreshForSelectionChange(oldSelection, m_selectionRange);
2071 }
2072 m_selectionAnchor = -2;
2073 }
2074
2075 static bool wxIsWordDelimiter(const wxString& text)
2076 {
2077 return !text.IsEmpty() && !wxIsalnum(text[0]);
2078 }
2079
2080 /// Select the word at the given character position
2081 bool wxRichTextCtrl::SelectWord(long position)
2082 {
2083 if (position < 0 || position > GetBuffer().GetRange().GetEnd())
2084 return false;
2085
2086 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position);
2087 if (!para)
2088 return false;
2089
2090 if (position == para->GetRange().GetEnd())
2091 position --;
2092
2093 long positionStart = position;
2094 long positionEnd = position;
2095
2096 for (positionStart = position; positionStart >= para->GetRange().GetStart(); positionStart --)
2097 {
2098 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionStart, positionStart));
2099 if (wxIsWordDelimiter(text))
2100 {
2101 positionStart ++;
2102 break;
2103 }
2104 }
2105 if (positionStart < para->GetRange().GetStart())
2106 positionStart = para->GetRange().GetStart();
2107
2108 for (positionEnd = position; positionEnd < para->GetRange().GetEnd(); positionEnd ++)
2109 {
2110 wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionEnd, positionEnd));
2111 if (wxIsWordDelimiter(text))
2112 {
2113 positionEnd --;
2114 break;
2115 }
2116 }
2117 if (positionEnd >= para->GetRange().GetEnd())
2118 positionEnd = para->GetRange().GetEnd();
2119
2120 if (positionEnd < positionStart)
2121 return false;
2122
2123 SetSelection(positionStart, positionEnd+1);
2124
2125 if (positionStart >= 0)
2126 {
2127 MoveCaret(positionStart-1, true);
2128 SetDefaultStyleToCursorStyle();
2129 }
2130
2131 return true;
2132 }
2133
2134 wxString wxRichTextCtrl::GetStringSelection() const
2135 {
2136 long from, to;
2137 GetSelection(&from, &to);
2138
2139 return GetRange(from, to);
2140 }
2141
2142 // ----------------------------------------------------------------------------
2143 // hit testing
2144 // ----------------------------------------------------------------------------
2145
2146 wxTextCtrlHitTestResult
2147 wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
2148 {
2149 // implement in terms of the other overload as the native ports typically
2150 // can get the position and not (x, y) pair directly (although wxUniv
2151 // directly gets x and y -- and so overrides this method as well)
2152 long pos;
2153 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
2154
2155 if ( rc != wxTE_HT_UNKNOWN )
2156 {
2157 PositionToXY(pos, x, y);
2158 }
2159
2160 return rc;
2161 }
2162
2163 wxTextCtrlHitTestResult
2164 wxRichTextCtrl::HitTest(const wxPoint& pt,
2165 long * pos) const
2166 {
2167 wxClientDC dc((wxRichTextCtrl*) this);
2168 ((wxRichTextCtrl*)this)->PrepareDC(dc);
2169
2170 // Buffer uses logical position (relative to start of buffer)
2171 // so convert
2172 wxPoint pt2 = GetLogicalPoint(pt);
2173
2174 int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt2, *pos);
2175
2176 if ((hit & wxRICHTEXT_HITTEST_BEFORE) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2177 return wxTE_HT_BEFORE;
2178 else if ((hit & wxRICHTEXT_HITTEST_AFTER) && (hit & wxRICHTEXT_HITTEST_OUTSIDE))
2179 return wxTE_HT_BEYOND;
2180 else if (hit & (wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER))
2181 return wxTE_HT_ON_TEXT;
2182
2183 return wxTE_HT_UNKNOWN;
2184 }
2185
2186 // ----------------------------------------------------------------------------
2187 // set/get the controls text
2188 // ----------------------------------------------------------------------------
2189
2190 wxString wxRichTextCtrl::GetValue() const
2191 {
2192 return GetBuffer().GetText();
2193 }
2194
2195 wxString wxRichTextCtrl::GetRange(long from, long to) const
2196 {
2197 // Public API for range is different from internals
2198 return GetBuffer().GetTextForRange(wxRichTextRange(from, to-1));
2199 }
2200
2201 void wxRichTextCtrl::DoSetValue(const wxString& value, int flags)
2202 {
2203 // Don't call Clear here, since it always sends a text updated event
2204 m_buffer.ResetAndClearCommands();
2205 m_buffer.SetDirty(true);
2206 m_caretPosition = -1;
2207 m_caretPositionForDefaultStyle = -2;
2208 m_caretAtLineStart = false;
2209 m_selectionRange.SetRange(-2, -2);
2210
2211 Scroll(0,0);
2212
2213 if (!IsFrozen())
2214 {
2215 LayoutContent();
2216 Refresh(false);
2217 }
2218
2219 if (!value.IsEmpty())
2220 {
2221 // Remove empty paragraph
2222 GetBuffer().Clear();
2223 DoWriteText(value, flags);
2224
2225 // for compatibility, don't move the cursor when doing SetValue()
2226 SetInsertionPoint(0);
2227 }
2228 else
2229 {
2230 // still send an event for consistency
2231 if (flags & SetValue_SendEvent)
2232 wxTextCtrl::SendTextUpdatedEvent(this);
2233 }
2234 DiscardEdits();
2235 }
2236
2237 void wxRichTextCtrl::WriteText(const wxString& value)
2238 {
2239 DoWriteText(value);
2240 }
2241
2242 void wxRichTextCtrl::DoWriteText(const wxString& value, int flags)
2243 {
2244 wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix);
2245
2246 GetBuffer().InsertTextWithUndo(m_caretPosition+1, valueUnix, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
2247
2248 if ( flags & SetValue_SendEvent )
2249 wxTextCtrl::SendTextUpdatedEvent(this);
2250 }
2251
2252 void wxRichTextCtrl::AppendText(const wxString& text)
2253 {
2254 SetInsertionPointEnd();
2255
2256 WriteText(text);
2257 }
2258
2259 /// Write an image at the current insertion point
2260 bool wxRichTextCtrl::WriteImage(const wxImage& image, wxBitmapType bitmapType)
2261 {
2262 wxRichTextImageBlock imageBlock;
2263
2264 wxImage image2 = image;
2265 if (imageBlock.MakeImageBlock(image2, bitmapType))
2266 return WriteImage(imageBlock);
2267
2268 return false;
2269 }
2270
2271 bool wxRichTextCtrl::WriteImage(const wxString& filename, wxBitmapType bitmapType)
2272 {
2273 wxRichTextImageBlock imageBlock;
2274
2275 wxImage image;
2276 if (imageBlock.MakeImageBlock(filename, bitmapType, image, false))
2277 return WriteImage(imageBlock);
2278
2279 return false;
2280 }
2281
2282 bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock)
2283 {
2284 return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this);
2285 }
2286
2287 bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, wxBitmapType bitmapType)
2288 {
2289 if (bitmap.Ok())
2290 {
2291 wxRichTextImageBlock imageBlock;
2292
2293 wxImage image = bitmap.ConvertToImage();
2294 if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType))
2295 return WriteImage(imageBlock);
2296 }
2297
2298 return false;
2299 }
2300
2301 /// Insert a newline (actually paragraph) at the current insertion point.
2302 bool wxRichTextCtrl::Newline()
2303 {
2304 return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this);
2305 }
2306
2307 /// Insert a line break at the current insertion point.
2308 bool wxRichTextCtrl::LineBreak()
2309 {
2310 wxString text;
2311 text = wxRichTextLineBreakChar;
2312 return GetBuffer().InsertTextWithUndo(m_caretPosition+1, text, this);
2313 }
2314
2315 // ----------------------------------------------------------------------------
2316 // Clipboard operations
2317 // ----------------------------------------------------------------------------
2318
2319 void wxRichTextCtrl::Copy()
2320 {
2321 if (CanCopy())
2322 {
2323 wxRichTextRange range = GetInternalSelectionRange();
2324 GetBuffer().CopyToClipboard(range);
2325 }
2326 }
2327
2328 void wxRichTextCtrl::Cut()
2329 {
2330 if (CanCut())
2331 {
2332 wxRichTextRange range = GetInternalSelectionRange();
2333 GetBuffer().CopyToClipboard(range);
2334
2335 DeleteSelectedContent();
2336 LayoutContent();
2337 Refresh(false);
2338 }
2339 }
2340
2341 void wxRichTextCtrl::Paste()
2342 {
2343 if (CanPaste())
2344 {
2345 BeginBatchUndo(_("Paste"));
2346
2347 long newPos = m_caretPosition;
2348 DeleteSelectedContent(& newPos);
2349
2350 GetBuffer().PasteFromClipboard(newPos);
2351
2352 EndBatchUndo();
2353 }
2354 }
2355
2356 void wxRichTextCtrl::DeleteSelection()
2357 {
2358 if (CanDeleteSelection())
2359 {
2360 DeleteSelectedContent();
2361 }
2362 }
2363
2364 bool wxRichTextCtrl::HasSelection() const
2365 {
2366 return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2;
2367 }
2368
2369 bool wxRichTextCtrl::CanCopy() const
2370 {
2371 // Can copy if there's a selection
2372 return HasSelection();
2373 }
2374
2375 bool wxRichTextCtrl::CanCut() const
2376 {
2377 return HasSelection() && IsEditable();
2378 }
2379
2380 bool wxRichTextCtrl::CanPaste() const
2381 {
2382 if ( !IsEditable() )
2383 return false;
2384
2385 return GetBuffer().CanPasteFromClipboard();
2386 }
2387
2388 bool wxRichTextCtrl::CanDeleteSelection() const
2389 {
2390 return HasSelection() && IsEditable();
2391 }
2392
2393
2394 // ----------------------------------------------------------------------------
2395 // Accessors
2396 // ----------------------------------------------------------------------------
2397
2398 void wxRichTextCtrl::SetEditable(bool editable)
2399 {
2400 m_editable = editable;
2401 }
2402
2403 void wxRichTextCtrl::SetInsertionPoint(long pos)
2404 {
2405 SelectNone();
2406
2407 m_caretPosition = pos - 1;
2408
2409 PositionCaret();
2410 }
2411
2412 void wxRichTextCtrl::SetInsertionPointEnd()
2413 {
2414 long pos = GetLastPosition();
2415 SetInsertionPoint(pos);
2416 }
2417
2418 long wxRichTextCtrl::GetInsertionPoint() const
2419 {
2420 return m_caretPosition+1;
2421 }
2422
2423 wxTextPos wxRichTextCtrl::GetLastPosition() const
2424 {
2425 return GetBuffer().GetRange().GetEnd();
2426 }
2427
2428 // If the return values from and to are the same, there is no
2429 // selection.
2430 void wxRichTextCtrl::GetSelection(long* from, long* to) const
2431 {
2432 *from = m_selectionRange.GetStart();
2433 *to = m_selectionRange.GetEnd();
2434 if ((*to) != -1 && (*to) != -2)
2435 (*to) ++;
2436 }
2437
2438 bool wxRichTextCtrl::IsEditable() const
2439 {
2440 return m_editable;
2441 }
2442
2443 // ----------------------------------------------------------------------------
2444 // selection
2445 // ----------------------------------------------------------------------------
2446
2447 void wxRichTextCtrl::SetSelection(long from, long to)
2448 {
2449 // if from and to are both -1, it means (in wxWidgets) that all text should
2450 // be selected.
2451 if ( (from == -1) && (to == -1) )
2452 {
2453 from = 0;
2454 to = GetLastPosition()+1;
2455 }
2456
2457 DoSetSelection(from, to);
2458 }
2459
2460 void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))
2461 {
2462 if (from == to)
2463 {
2464 SelectNone();
2465 }
2466 else
2467 {
2468 wxRichTextRange oldSelection = m_selectionRange;
2469 m_selectionAnchor = from;
2470 m_selectionRange.SetRange(from, to-1);
2471 if (from > -2)
2472 m_caretPosition = from-1;
2473
2474 RefreshForSelectionChange(oldSelection, m_selectionRange);
2475 PositionCaret();
2476 }
2477 }
2478
2479 // ----------------------------------------------------------------------------
2480 // Editing
2481 // ----------------------------------------------------------------------------
2482
2483 void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to),
2484 const wxString& value)
2485 {
2486 BeginBatchUndo(_("Replace"));
2487
2488 DeleteSelectedContent();
2489
2490 DoWriteText(value, SetValue_SelectionOnly);
2491
2492 EndBatchUndo();
2493 }
2494
2495 void wxRichTextCtrl::Remove(long from, long to)
2496 {
2497 SelectNone();
2498
2499 GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to-1), this);
2500
2501 LayoutContent();
2502 if (!IsFrozen())
2503 Refresh(false);
2504 }
2505
2506 bool wxRichTextCtrl::IsModified() const
2507 {
2508 return m_buffer.IsModified();
2509 }
2510
2511 void wxRichTextCtrl::MarkDirty()
2512 {
2513 m_buffer.Modify(true);
2514 }
2515
2516 void wxRichTextCtrl::DiscardEdits()
2517 {
2518 m_caretPositionForDefaultStyle = -2;
2519 m_buffer.Modify(false);
2520 m_buffer.GetCommandProcessor()->ClearCommands();
2521 }
2522
2523 int wxRichTextCtrl::GetNumberOfLines() const
2524 {
2525 return GetBuffer().GetParagraphCount();
2526 }
2527
2528 // ----------------------------------------------------------------------------
2529 // Positions <-> coords
2530 // ----------------------------------------------------------------------------
2531
2532 long wxRichTextCtrl::XYToPosition(long x, long y) const
2533 {
2534 return GetBuffer().XYToPosition(x, y);
2535 }
2536
2537 bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const
2538 {
2539 return GetBuffer().PositionToXY(pos, x, y);
2540 }
2541
2542 // ----------------------------------------------------------------------------
2543 //
2544 // ----------------------------------------------------------------------------
2545
2546 void wxRichTextCtrl::ShowPosition(long pos)
2547 {
2548 if (!IsPositionVisible(pos))
2549 ScrollIntoView(pos-1, WXK_DOWN);
2550 }
2551
2552 int wxRichTextCtrl::GetLineLength(long lineNo) const
2553 {
2554 return GetBuffer().GetParagraphLength(lineNo);
2555 }
2556
2557 wxString wxRichTextCtrl::GetLineText(long lineNo) const
2558 {
2559 return GetBuffer().GetParagraphText(lineNo);
2560 }
2561
2562 // ----------------------------------------------------------------------------
2563 // Undo/redo
2564 // ----------------------------------------------------------------------------
2565
2566 void wxRichTextCtrl::Undo()
2567 {
2568 if (CanUndo())
2569 {
2570 GetCommandProcessor()->Undo();
2571 }
2572 }
2573
2574 void wxRichTextCtrl::Redo()
2575 {
2576 if (CanRedo())
2577 {
2578 GetCommandProcessor()->Redo();
2579 }
2580 }
2581
2582 bool wxRichTextCtrl::CanUndo() const
2583 {
2584 return GetCommandProcessor()->CanUndo();
2585 }
2586
2587 bool wxRichTextCtrl::CanRedo() const
2588 {
2589 return GetCommandProcessor()->CanRedo();
2590 }
2591
2592 // ----------------------------------------------------------------------------
2593 // implementation details
2594 // ----------------------------------------------------------------------------
2595
2596 void wxRichTextCtrl::Command(wxCommandEvent& event)
2597 {
2598 SetValue(event.GetString());
2599 GetEventHandler()->ProcessEvent(event);
2600 }
2601
2602 void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event)
2603 {
2604 // By default, load the first file into the text window.
2605 if (event.GetNumberOfFiles() > 0)
2606 {
2607 LoadFile(event.GetFiles()[0]);
2608 }
2609 }
2610
2611 wxSize wxRichTextCtrl::DoGetBestSize() const
2612 {
2613 return wxSize(10, 10);
2614 }
2615
2616 // ----------------------------------------------------------------------------
2617 // standard handlers for standard edit menu events
2618 // ----------------------------------------------------------------------------
2619
2620 void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2621 {
2622 Cut();
2623 }
2624
2625 void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event))
2626 {
2627 DeleteSelection();
2628 }
2629
2630 void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2631 {
2632 Copy();
2633 }
2634
2635 void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2636 {
2637 Paste();
2638 }
2639
2640 void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2641 {
2642 Undo();
2643 }
2644
2645 void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2646 {
2647 Redo();
2648 }
2649
2650 void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2651 {
2652 event.Enable( CanCut() );
2653 }
2654
2655 void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2656 {
2657 event.Enable( CanCopy() );
2658 }
2659
2660 void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event)
2661 {
2662 event.Enable( CanDeleteSelection() );
2663 }
2664
2665 void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2666 {
2667 event.Enable( CanPaste() );
2668 }
2669
2670 void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2671 {
2672 event.Enable( CanUndo() );
2673 event.SetText( GetCommandProcessor()->GetUndoMenuLabel() );
2674 }
2675
2676 void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2677 {
2678 event.Enable( CanRedo() );
2679 event.SetText( GetCommandProcessor()->GetRedoMenuLabel() );
2680 }
2681
2682 void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
2683 {
2684 SelectAll();
2685 }
2686
2687 void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
2688 {
2689 event.Enable(GetLastPosition() > 0);
2690 }
2691
2692 void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event)
2693 {
2694 if (event.GetEventObject() != this)
2695 {
2696 event.Skip();
2697 return;
2698 }
2699
2700 if (!m_contextMenu)
2701 {
2702 m_contextMenu = new wxMenu;
2703 m_contextMenu->Append(wxID_UNDO, _("&Undo"));
2704 m_contextMenu->Append(wxID_REDO, _("&Redo"));
2705 m_contextMenu->AppendSeparator();
2706 m_contextMenu->Append(wxID_CUT, _("Cu&t"));
2707 m_contextMenu->Append(wxID_COPY, _("&Copy"));
2708 m_contextMenu->Append(wxID_PASTE, _("&Paste"));
2709 m_contextMenu->Append(wxID_CLEAR, _("&Delete"));
2710 m_contextMenu->AppendSeparator();
2711 m_contextMenu->Append(wxID_SELECTALL, _("Select &All"));
2712 }
2713 PopupMenu(m_contextMenu);
2714 return;
2715 }
2716
2717 bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
2718 {
2719 return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttr(style));
2720 }
2721
2722 bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxTextAttr& style)
2723 {
2724 return GetBuffer().SetStyle(range.ToInternal(), style);
2725 }
2726
2727 // extended style setting operation with flags including:
2728 // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY.
2729 // see richtextbuffer.h for more details.
2730
2731 bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxTextAttr& style, int flags)
2732 {
2733 return GetBuffer().SetStyle(range.ToInternal(), style, flags);
2734 }
2735
2736 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style)
2737 {
2738 return GetBuffer().SetDefaultStyle(wxTextAttr(style));
2739 }
2740
2741 const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const
2742 {
2743 return GetBuffer().GetDefaultStyle();
2744 }
2745
2746 bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style)
2747 {
2748 return GetBuffer().GetStyle(position, style);
2749 }
2750
2751 // get the common set of styles for the range
2752 bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttr& style)
2753 {
2754 return GetBuffer().GetStyleForRange(range.ToInternal(), style);
2755 }
2756
2757 /// Get the content (uncombined) attributes for this position.
2758 bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttr& style)
2759 {
2760 return GetBuffer().GetUncombinedStyle(position, style);
2761 }
2762
2763 /// Set font, and also the buffer attributes
2764 bool wxRichTextCtrl::SetFont(const wxFont& font)
2765 {
2766 wxControl::SetFont(font);
2767
2768 wxTextAttr attr = GetBuffer().GetAttributes();
2769 attr.SetFont(font);
2770 GetBuffer().SetBasicStyle(attr);
2771
2772 GetBuffer().Invalidate(wxRICHTEXT_ALL);
2773 Refresh(false);
2774
2775 return true;
2776 }
2777
2778 /// Transform logical to physical
2779 wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const
2780 {
2781 wxPoint pt;
2782 CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y);
2783
2784 return pt;
2785 }
2786
2787 /// Transform physical to logical
2788 wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const
2789 {
2790 wxPoint pt;
2791 CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y);
2792
2793 return pt;
2794 }
2795
2796 /// Position the caret
2797 void wxRichTextCtrl::PositionCaret()
2798 {
2799 if (!GetCaret())
2800 return;
2801
2802 //wxLogDebug(wxT("PositionCaret"));
2803
2804 wxRect caretRect;
2805 if (GetCaretPositionForIndex(GetCaretPosition(), caretRect))
2806 {
2807 wxPoint newPt = caretRect.GetPosition();
2808 wxSize newSz = caretRect.GetSize();
2809 wxPoint pt = GetPhysicalPoint(newPt);
2810 if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz)
2811 {
2812 GetCaret()->Hide();
2813 if (GetCaret()->GetSize() != newSz)
2814 GetCaret()->SetSize(newSz);
2815 GetCaret()->Move(pt);
2816 GetCaret()->Show();
2817 }
2818 }
2819 }
2820
2821 /// Get the caret height and position for the given character position
2822 bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect)
2823 {
2824 wxClientDC dc(this);
2825 dc.SetFont(GetFont());
2826
2827 PrepareDC(dc);
2828
2829 wxPoint pt;
2830 int height = 0;
2831
2832 if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart))
2833 {
2834 // Caret height can't be zero
2835 if (height == 0)
2836 height = dc.GetCharHeight();
2837
2838 rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height));
2839 return true;
2840 }
2841
2842 return false;
2843 }
2844
2845 /// Gets the line for the visible caret position. If the caret is
2846 /// shown at the very end of the line, it means the next character is actually
2847 /// on the following line. So let's get the line we're expecting to find
2848 /// if this is the case.
2849 wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const
2850 {
2851 wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true);
2852 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true);
2853 if (line)
2854 {
2855 wxRichTextRange lineRange = line->GetAbsoluteRange();
2856 if (caretPosition == lineRange.GetStart()-1 &&
2857 (para->GetRange().GetStart() != lineRange.GetStart()))
2858 {
2859 if (!m_caretAtLineStart)
2860 line = GetBuffer().GetLineAtPosition(caretPosition-1, true);
2861 }
2862 }
2863 return line;
2864 }
2865
2866
2867 /// Move the caret to the given character position
2868 bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart)
2869 {
2870 if (GetBuffer().GetDirty())
2871 LayoutContent();
2872
2873 if (pos <= GetBuffer().GetRange().GetEnd())
2874 {
2875 SetCaretPosition(pos, showAtLineStart);
2876
2877 PositionCaret();
2878
2879 return true;
2880 }
2881 else
2882 return false;
2883 }
2884
2885 /// Layout the buffer: which we must do before certain operations, such as
2886 /// setting the caret position.
2887 bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
2888 {
2889 if (GetBuffer().GetDirty() || onlyVisibleRect)
2890 {
2891 wxRect availableSpace(GetClientSize());
2892 if (availableSpace.width == 0)
2893 availableSpace.width = 10;
2894 if (availableSpace.height == 0)
2895 availableSpace.height = 10;
2896
2897 int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT;
2898 if (onlyVisibleRect)
2899 {
2900 flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT;
2901 availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0)));
2902 }
2903
2904 wxClientDC dc(this);
2905 dc.SetFont(GetFont());
2906
2907 PrepareDC(dc);
2908
2909 GetBuffer().Defragment();
2910 GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation
2911 GetBuffer().Layout(dc, availableSpace, flags);
2912 GetBuffer().SetDirty(false);
2913
2914 if (!IsFrozen())
2915 SetupScrollbars();
2916 }
2917
2918 return true;
2919 }
2920
2921 /// Is all of the selection bold?
2922 bool wxRichTextCtrl::IsSelectionBold()
2923 {
2924 if (HasSelection())
2925 {
2926 wxTextAttr attr;
2927 wxRichTextRange range = GetSelectionRange();
2928 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2929 attr.SetFontWeight(wxBOLD);
2930
2931 return HasCharacterAttributes(range, attr);
2932 }
2933 else
2934 {
2935 // If no selection, then we need to combine current style with default style
2936 // to see what the effect would be if we started typing.
2937 wxTextAttr attr;
2938 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
2939
2940 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2941 if (GetStyle(pos, attr))
2942 {
2943 if (IsDefaultStyleShowing())
2944 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2945 return attr.GetFontWeight() == wxBOLD;
2946 }
2947 }
2948 return false;
2949 }
2950
2951 /// Is all of the selection italics?
2952 bool wxRichTextCtrl::IsSelectionItalics()
2953 {
2954 if (HasSelection())
2955 {
2956 wxRichTextRange range = GetSelectionRange();
2957 wxTextAttr attr;
2958 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2959 attr.SetFontStyle(wxITALIC);
2960
2961 return HasCharacterAttributes(range, attr);
2962 }
2963 else
2964 {
2965 // If no selection, then we need to combine current style with default style
2966 // to see what the effect would be if we started typing.
2967 wxTextAttr attr;
2968 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
2969
2970 long pos = GetAdjustedCaretPosition(GetCaretPosition());
2971 if (GetStyle(pos, attr))
2972 {
2973 if (IsDefaultStyleShowing())
2974 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
2975 return attr.GetFontStyle() == wxITALIC;
2976 }
2977 }
2978 return false;
2979 }
2980
2981 /// Is all of the selection underlined?
2982 bool wxRichTextCtrl::IsSelectionUnderlined()
2983 {
2984 if (HasSelection())
2985 {
2986 wxRichTextRange range = GetSelectionRange();
2987 wxTextAttr attr;
2988 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2989 attr.SetFontUnderlined(true);
2990
2991 return HasCharacterAttributes(range, attr);
2992 }
2993 else
2994 {
2995 // If no selection, then we need to combine current style with default style
2996 // to see what the effect would be if we started typing.
2997 wxTextAttr attr;
2998 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
2999 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3000
3001 if (GetStyle(pos, attr))
3002 {
3003 if (IsDefaultStyleShowing())
3004 wxRichTextApplyStyle(attr, GetDefaultStyleEx());
3005 return attr.GetFontUnderlined();
3006 }
3007 }
3008 return false;
3009 }
3010
3011 /// Apply bold to the selection
3012 bool wxRichTextCtrl::ApplyBoldToSelection()
3013 {
3014 wxTextAttr attr;
3015 attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
3016 attr.SetFontWeight(IsSelectionBold() ? wxNORMAL : wxBOLD);
3017
3018 if (HasSelection())
3019 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3020 else
3021 {
3022 wxRichTextAttr current = GetDefaultStyleEx();
3023 current.Apply(attr);
3024 SetAndShowDefaultStyle(current);
3025 }
3026 return true;
3027 }
3028
3029 /// Apply italic to the selection
3030 bool wxRichTextCtrl::ApplyItalicToSelection()
3031 {
3032 wxTextAttr attr;
3033 attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
3034 attr.SetFontStyle(IsSelectionItalics() ? wxNORMAL : wxITALIC);
3035
3036 if (HasSelection())
3037 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3038 else
3039 {
3040 wxRichTextAttr current = GetDefaultStyleEx();
3041 current.Apply(attr);
3042 SetAndShowDefaultStyle(current);
3043 }
3044 return true;
3045 }
3046
3047 /// Apply underline to the selection
3048 bool wxRichTextCtrl::ApplyUnderlineToSelection()
3049 {
3050 wxTextAttr attr;
3051 attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
3052 attr.SetFontUnderlined(!IsSelectionUnderlined());
3053
3054 if (HasSelection())
3055 return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
3056 else
3057 {
3058 wxRichTextAttr current = GetDefaultStyleEx();
3059 current.Apply(attr);
3060 SetAndShowDefaultStyle(current);
3061 }
3062 return true;
3063 }
3064
3065 /// Is all of the selection aligned according to the specified flag?
3066 bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
3067 {
3068 wxRichTextRange range;
3069 if (HasSelection())
3070 range = GetSelectionRange();
3071 else
3072 range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2);
3073
3074 wxTextAttr attr;
3075 attr.SetAlignment(alignment);
3076
3077 return HasParagraphAttributes(range, attr);
3078 }
3079
3080 /// Apply alignment to the selection
3081 bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
3082 {
3083 wxTextAttr attr;
3084 attr.SetAlignment(alignment);
3085 if (HasSelection())
3086 return SetStyle(GetSelectionRange(), attr);
3087 else
3088 {
3089 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
3090 if (para)
3091 return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY);
3092 }
3093 return true;
3094 }
3095
3096 /// Apply a named style to the selection
3097 bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
3098 {
3099 // Flags are defined within each definition, so only certain
3100 // attributes are applied.
3101 wxTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle());
3102
3103 int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_RESET;
3104
3105 if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
3106 {
3107 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
3108
3109 wxRichTextRange range;
3110
3111 if (HasSelection())
3112 range = GetSelectionRange();
3113 else
3114 {
3115 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3116 range = wxRichTextRange(pos, pos+1);
3117 }
3118
3119 return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags);
3120 }
3121
3122 // Make sure the attr has the style name
3123 if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
3124 {
3125 attr.SetParagraphStyleName(def->GetName());
3126
3127 // If applying a paragraph style, we only want the paragraph nodes to adopt these
3128 // attributes, and not the leaf nodes. This will allow the content (e.g. text)
3129 // to change its style independently.
3130 flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY;
3131 }
3132 else
3133 attr.SetCharacterStyleName(def->GetName());
3134
3135 if (HasSelection())
3136 return SetStyleEx(GetSelectionRange(), attr, flags);
3137 else
3138 {
3139 wxRichTextAttr current = GetDefaultStyleEx();
3140 current.Apply(attr);
3141 SetAndShowDefaultStyle(current);
3142 return true;
3143 }
3144 }
3145
3146 /// Apply the style sheet to the buffer, for example if the styles have changed.
3147 bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet)
3148 {
3149 if (!styleSheet)
3150 styleSheet = GetBuffer().GetStyleSheet();
3151 if (!styleSheet)
3152 return false;
3153
3154 if (GetBuffer().ApplyStyleSheet(styleSheet))
3155 {
3156 GetBuffer().Invalidate(wxRICHTEXT_ALL);
3157 Refresh(false);
3158 return true;
3159 }
3160 else
3161 return false;
3162 }
3163
3164 /// Sets the default style to the style under the cursor
3165 bool wxRichTextCtrl::SetDefaultStyleToCursorStyle()
3166 {
3167 wxTextAttr attr;
3168 attr.SetFlags(wxTEXT_ATTR_CHARACTER);
3169
3170 // If at the start of a paragraph, use the next position.
3171 long pos = GetAdjustedCaretPosition(GetCaretPosition());
3172
3173 if (GetUncombinedStyle(pos, attr))
3174 {
3175 SetDefaultStyle(attr);
3176 return true;
3177 }
3178
3179 return false;
3180 }
3181
3182 /// Returns the first visible position in the current view
3183 long wxRichTextCtrl::GetFirstVisiblePosition() const
3184 {
3185 wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y);
3186 if (line)
3187 return line->GetAbsoluteRange().GetStart();
3188 else
3189 return 0;
3190 }
3191
3192 /// Get the first visible point in the window
3193 wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const
3194 {
3195 int ppuX, ppuY;
3196 int startXUnits, startYUnits;
3197
3198 GetScrollPixelsPerUnit(& ppuX, & ppuY);
3199 GetViewStart(& startXUnits, & startYUnits);
3200
3201 return wxPoint(startXUnits * ppuX, startYUnits * ppuY);
3202 }
3203
3204 /// The adjusted caret position is the character position adjusted to take
3205 /// into account whether we're at the start of a paragraph, in which case
3206 /// style information should be taken from the next position, not current one.
3207 long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
3208 {
3209 wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPos+1);
3210
3211 if (para && (caretPos+1 == para->GetRange().GetStart()))
3212 caretPos ++;
3213 return caretPos;
3214 }
3215
3216 /// Get/set the selection range in character positions. -1, -1 means no selection.
3217 /// The range is in API convention, i.e. a single character selection is denoted
3218 /// by (n, n+1)
3219 wxRichTextRange wxRichTextCtrl::GetSelectionRange() const
3220 {
3221 wxRichTextRange range = GetInternalSelectionRange();
3222 if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1))
3223 range.SetEnd(range.GetEnd() + 1);
3224 return range;
3225 }
3226
3227 void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range)
3228 {
3229 wxRichTextRange range1(range);
3230 if (range1 != wxRichTextRange(-2,-2) && range1 != wxRichTextRange(-1,-1) )
3231 range1.SetEnd(range1.GetEnd() - 1);
3232
3233 wxASSERT( range1.GetStart() > range1.GetEnd() );
3234
3235 SetInternalSelectionRange(range1);
3236 }
3237
3238 /// Set list style
3239 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3240 {
3241 return GetBuffer().SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel);
3242 }
3243
3244 bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3245 {
3246 return GetBuffer().SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
3247 }
3248
3249 /// Clear list for given range
3250 bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags)
3251 {
3252 return GetBuffer().ClearListStyle(range.ToInternal(), flags);
3253 }
3254
3255 /// Number/renumber any list elements in the given range
3256 bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel)
3257 {
3258 return GetBuffer().NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel);
3259 }
3260
3261 bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel)
3262 {
3263 return GetBuffer().NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel);
3264 }
3265
3266 /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1
3267 bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel)
3268 {
3269 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel);
3270 }
3271
3272 bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel)
3273 {
3274 return GetBuffer().PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel);
3275 }
3276
3277 /// Deletes the content in the given range
3278 bool wxRichTextCtrl::Delete(const wxRichTextRange& range)
3279 {
3280 return GetBuffer().DeleteRangeWithUndo(range.ToInternal(), this);
3281 }
3282
3283 const wxArrayString& wxRichTextCtrl::GetAvailableFontNames()
3284 {
3285 if (sm_availableFontNames.GetCount() == 0)
3286 {
3287 sm_availableFontNames = wxFontEnumerator::GetFacenames();
3288 sm_availableFontNames.Sort();
3289 }
3290 return sm_availableFontNames;
3291 }
3292
3293 void wxRichTextCtrl::ClearAvailableFontNames()
3294 {
3295 sm_availableFontNames.Clear();
3296 }
3297
3298 void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
3299 {
3300 //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
3301
3302 wxTextAttrEx basicStyle = GetBasicStyle();
3303 basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
3304 SetBasicStyle(basicStyle);
3305 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
3306
3307 Refresh();
3308 }
3309
3310 // Refresh the area affected by a selection change
3311 bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextRange& oldSelection, const wxRichTextRange& newSelection)
3312 {
3313 // Calculate the refresh rectangle - just the affected lines
3314 long firstPos, lastPos;
3315 if (oldSelection.GetStart() == -2 && newSelection.GetStart() != -2)
3316 {
3317 firstPos = newSelection.GetStart();
3318 lastPos = newSelection.GetEnd();
3319 }
3320 else if (oldSelection.GetStart() != -2 && newSelection.GetStart() == -2)
3321 {
3322 firstPos = oldSelection.GetStart();
3323 lastPos = oldSelection.GetEnd();
3324 }
3325 else if (oldSelection.GetStart() == -2 && newSelection.GetStart() == -2)
3326 {
3327 return false;
3328 }
3329 else
3330 {
3331 firstPos = wxMin(oldSelection.GetStart(), newSelection.GetStart());
3332 lastPos = wxMax(oldSelection.GetEnd(), newSelection.GetEnd());
3333 }
3334
3335 wxRichTextLine* firstLine = GetBuffer().GetLineAtPosition(firstPos);
3336 wxRichTextLine* lastLine = GetBuffer().GetLineAtPosition(lastPos);
3337
3338 if (firstLine && lastLine)
3339 {
3340 wxSize clientSize = GetClientSize();
3341 wxPoint pt1 = GetPhysicalPoint(firstLine->GetAbsolutePosition());
3342 wxPoint pt2 = GetPhysicalPoint(lastLine->GetAbsolutePosition()) + wxPoint(0, lastLine->GetSize().y);
3343
3344 pt1.x = 0;
3345 pt1.y = wxMax(0, pt1.y);
3346 pt2.x = 0;
3347 pt2.y = wxMin(clientSize.y, pt2.y);
3348
3349 wxRect rect(pt1, wxSize(clientSize.x, pt2.y - pt1.y));
3350 RefreshRect(rect, false);
3351 }
3352 else
3353 Refresh(false);
3354
3355 return true;
3356 }
3357
3358 #if wxRICHTEXT_USE_OWN_CARET
3359
3360 // ----------------------------------------------------------------------------
3361 // initialization and destruction
3362 // ----------------------------------------------------------------------------
3363
3364 void wxRichTextCaret::Init()
3365 {
3366 m_hasFocus = true;
3367
3368 m_xOld =
3369 m_yOld = -1;
3370 m_richTextCtrl = NULL;
3371 m_needsUpdate = false;
3372 }
3373
3374 wxRichTextCaret::~wxRichTextCaret()
3375 {
3376 }
3377
3378 // ----------------------------------------------------------------------------
3379 // showing/hiding/moving the caret (base class interface)
3380 // ----------------------------------------------------------------------------
3381
3382 void wxRichTextCaret::DoShow()
3383 {
3384 Refresh();
3385 }
3386
3387 void wxRichTextCaret::DoHide()
3388 {
3389 Refresh();
3390 }
3391
3392 void wxRichTextCaret::DoMove()
3393 {
3394 if (IsVisible())
3395 {
3396 Refresh();
3397
3398 if (m_xOld != -1 && m_yOld != -1)
3399 {
3400 if (m_richTextCtrl)
3401 {
3402 wxRect rect(GetPosition(), GetSize());
3403 m_richTextCtrl->RefreshRect(rect, false);
3404 }
3405 }
3406 }
3407
3408 m_xOld = m_x;
3409 m_yOld = m_y;
3410 }
3411
3412 void wxRichTextCaret::DoSize()
3413 {
3414 int countVisible = m_countVisible;
3415 if (countVisible > 0)
3416 {
3417 m_countVisible = 0;
3418 DoHide();
3419 }
3420
3421 if (countVisible > 0)
3422 {
3423 m_countVisible = countVisible;
3424 DoShow();
3425 }
3426 }
3427
3428 // ----------------------------------------------------------------------------
3429 // handling the focus
3430 // ----------------------------------------------------------------------------
3431
3432 void wxRichTextCaret::OnSetFocus()
3433 {
3434 m_hasFocus = true;
3435
3436 if ( IsVisible() )
3437 Refresh();
3438 }
3439
3440 void wxRichTextCaret::OnKillFocus()
3441 {
3442 m_hasFocus = false;
3443 }
3444
3445 // ----------------------------------------------------------------------------
3446 // drawing the caret
3447 // ----------------------------------------------------------------------------
3448
3449 void wxRichTextCaret::Refresh()
3450 {
3451 if (m_richTextCtrl)
3452 {
3453 wxRect rect(GetPosition(), GetSize());
3454 m_richTextCtrl->RefreshRect(rect, false);
3455 }
3456 }
3457
3458 void wxRichTextCaret::DoDraw(wxDC *dc)
3459 {
3460 dc->SetPen( *wxBLACK_PEN );
3461
3462 dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH));
3463 dc->SetPen(*wxBLACK_PEN);
3464
3465 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
3466 // done under wxGTK - no idea why
3467 //dc->SetLogicalFunction(wxINVERT);
3468
3469 wxPoint pt(m_x, m_y);
3470
3471 if (m_richTextCtrl)
3472 {
3473 pt = m_richTextCtrl->GetLogicalPoint(pt);
3474 }
3475 dc->DrawRectangle(pt.x, pt.y, m_width, m_height);
3476 }
3477 #endif
3478 // wxRICHTEXT_USE_OWN_CARET
3479
3480 #endif
3481 // wxUSE_RICHTEXT