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