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