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