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