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