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