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