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