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