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