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