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