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