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