]>
Commit | Line | Data |
---|---|---|
5d7836c4 | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/richtext/richtextctrl.cpp |
5d7836c4 JS |
3 | // Purpose: A rich edit control |
4 | // Author: Julian Smart | |
7fe8059f | 5 | // Modified by: |
5d7836c4 | 6 | // Created: 2005-09-30 |
7fe8059f | 7 | // RCS-ID: $Id$ |
5d7836c4 JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
9eddec69 | 16 | #pragma hdrstop |
5d7836c4 JS |
17 | #endif |
18 | ||
b01ca8b6 JS |
19 | #if wxUSE_RICHTEXT |
20 | ||
21 | #include "wx/richtext/richtextctrl.h" | |
ab14c7aa | 22 | #include "wx/richtext/richtextstyles.h" |
b01ca8b6 | 23 | |
5d7836c4 | 24 | #ifndef WX_PRECOMP |
fe5aa22c | 25 | #include "wx/wx.h" |
9eddec69 | 26 | #include "wx/settings.h" |
5d7836c4 JS |
27 | #endif |
28 | ||
a188ac29 | 29 | #include "wx/timer.h" |
5d7836c4 JS |
30 | #include "wx/textfile.h" |
31 | #include "wx/ffile.h" | |
5d7836c4 JS |
32 | #include "wx/filename.h" |
33 | #include "wx/dcbuffer.h" | |
5d7836c4 | 34 | #include "wx/arrimpl.cpp" |
dadd4f55 | 35 | #include "wx/fontenum.h" |
addcf76c | 36 | #include "wx/accel.h" |
5d7836c4 | 37 | |
5dec941a JS |
38 | #if defined (__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) |
39 | #define wxHAVE_PRIMARY_SELECTION 1 | |
40 | #else | |
41 | #define wxHAVE_PRIMARY_SELECTION 0 | |
42 | #endif | |
43 | ||
44 | #if wxUSE_CLIPBOARD && wxHAVE_PRIMARY_SELECTION | |
45 | #include "wx/clipbrd.h" | |
46 | #endif | |
47 | ||
27e20452 JS |
48 | // DLL options compatibility check: |
49 | #include "wx/app.h" | |
50 | WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl") | |
51 | ||
ce7fe42e VZ |
52 | wxDEFINE_EVENT( wxEVT_RICHTEXT_LEFT_CLICK, wxRichTextEvent ); |
53 | wxDEFINE_EVENT( wxEVT_RICHTEXT_MIDDLE_CLICK, wxRichTextEvent ); | |
54 | wxDEFINE_EVENT( wxEVT_RICHTEXT_RIGHT_CLICK, wxRichTextEvent ); | |
55 | wxDEFINE_EVENT( wxEVT_RICHTEXT_LEFT_DCLICK, wxRichTextEvent ); | |
56 | wxDEFINE_EVENT( wxEVT_RICHTEXT_RETURN, wxRichTextEvent ); | |
57 | wxDEFINE_EVENT( wxEVT_RICHTEXT_CHARACTER, wxRichTextEvent ); | |
58 | wxDEFINE_EVENT( wxEVT_RICHTEXT_DELETE, wxRichTextEvent ); | |
59 | ||
60 | wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLESHEET_REPLACING, wxRichTextEvent ); | |
61 | wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLESHEET_REPLACED, wxRichTextEvent ); | |
62 | wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLESHEET_CHANGING, wxRichTextEvent ); | |
63 | wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLESHEET_CHANGED, wxRichTextEvent ); | |
64 | ||
65 | wxDEFINE_EVENT( wxEVT_RICHTEXT_CONTENT_INSERTED, wxRichTextEvent ); | |
66 | wxDEFINE_EVENT( wxEVT_RICHTEXT_CONTENT_DELETED, wxRichTextEvent ); | |
67 | wxDEFINE_EVENT( wxEVT_RICHTEXT_STYLE_CHANGED, wxRichTextEvent ); | |
68 | wxDEFINE_EVENT( wxEVT_RICHTEXT_PROPERTIES_CHANGED, wxRichTextEvent ); | |
69 | wxDEFINE_EVENT( wxEVT_RICHTEXT_SELECTION_CHANGED, wxRichTextEvent ); | |
70 | wxDEFINE_EVENT( wxEVT_RICHTEXT_BUFFER_RESET, wxRichTextEvent ); | |
71 | wxDEFINE_EVENT( wxEVT_RICHTEXT_FOCUS_OBJECT_CHANGED, wxRichTextEvent ); | |
5912d19e | 72 | |
1c13f06e JS |
73 | #if wxRICHTEXT_USE_OWN_CARET |
74 | ||
75 | /*! | |
76 | * wxRichTextCaret | |
77 | * | |
78 | * This implements a non-flashing cursor in case there | |
79 | * are platform-specific problems with the generic caret. | |
80 | * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h. | |
81 | */ | |
82 | ||
a188ac29 JS |
83 | class wxRichTextCaret; |
84 | class wxRichTextCaretTimer: public wxTimer | |
85 | { | |
86 | public: | |
87 | wxRichTextCaretTimer(wxRichTextCaret* caret) | |
88 | { | |
89 | m_caret = caret; | |
90 | } | |
91 | virtual void Notify(); | |
92 | wxRichTextCaret* m_caret; | |
93 | }; | |
94 | ||
1c13f06e JS |
95 | class wxRichTextCaret: public wxCaret |
96 | { | |
97 | public: | |
98 | // ctors | |
99 | // ----- | |
100 | // default - use Create() | |
a188ac29 | 101 | wxRichTextCaret(): m_timer(this) { Init(); } |
1c13f06e JS |
102 | // creates a block caret associated with the given window |
103 | wxRichTextCaret(wxRichTextCtrl *window, int width, int height) | |
a188ac29 | 104 | : wxCaret(window, width, height), m_timer(this) { Init(); m_richTextCtrl = window; } |
1c13f06e | 105 | wxRichTextCaret(wxRichTextCtrl *window, const wxSize& size) |
a188ac29 | 106 | : wxCaret(window, size), m_timer(this) { Init(); m_richTextCtrl = window; } |
1c13f06e JS |
107 | |
108 | virtual ~wxRichTextCaret(); | |
109 | ||
110 | // implementation | |
111 | // -------------- | |
112 | ||
113 | // called by wxWindow (not using the event tables) | |
114 | virtual void OnSetFocus(); | |
115 | virtual void OnKillFocus(); | |
116 | ||
117 | // draw the caret on the given DC | |
118 | void DoDraw(wxDC *dc); | |
119 | ||
120 | // get the visible count | |
121 | int GetVisibleCount() const { return m_countVisible; } | |
122 | ||
123 | // delay repositioning | |
124 | bool GetNeedsUpdate() const { return m_needsUpdate; } | |
125 | void SetNeedsUpdate(bool needsUpdate = true ) { m_needsUpdate = needsUpdate; } | |
126 | ||
a188ac29 JS |
127 | void Notify(); |
128 | ||
cd45ed36 JS |
129 | bool GetRefreshEnabled() const { return m_refreshEnabled; } |
130 | void EnableRefresh(bool b) { m_refreshEnabled = b; } | |
131 | ||
1c13f06e JS |
132 | protected: |
133 | virtual void DoShow(); | |
134 | virtual void DoHide(); | |
135 | virtual void DoMove(); | |
136 | virtual void DoSize(); | |
137 | ||
138 | // refresh the caret | |
139 | void Refresh(); | |
140 | ||
141 | private: | |
142 | void Init(); | |
143 | ||
144 | int m_xOld, | |
145 | m_yOld; | |
146 | bool m_hasFocus; // true => our window has focus | |
147 | bool m_needsUpdate; // must be repositioned | |
a188ac29 JS |
148 | bool m_flashOn; |
149 | wxRichTextCaretTimer m_timer; | |
1c13f06e | 150 | wxRichTextCtrl* m_richTextCtrl; |
cd45ed36 | 151 | bool m_refreshEnabled; |
eaaf10f6 JS |
152 | wxPen m_caretPen; |
153 | wxBrush m_caretBrush; | |
1c13f06e JS |
154 | }; |
155 | #endif | |
156 | ||
24777478 | 157 | IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl, wxControl ) |
5d7836c4 | 158 | |
d0960f6d | 159 | IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent, wxNotifyEvent ) |
5d7836c4 | 160 | |
24777478 | 161 | BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl ) |
5d7836c4 JS |
162 | EVT_PAINT(wxRichTextCtrl::OnPaint) |
163 | EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground) | |
4d551ad5 | 164 | EVT_IDLE(wxRichTextCtrl::OnIdle) |
c59f6793 | 165 | EVT_SCROLLWIN(wxRichTextCtrl::OnScroll) |
5d7836c4 JS |
166 | EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick) |
167 | EVT_MOTION(wxRichTextCtrl::OnMoveMouse) | |
168 | EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp) | |
169 | EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick) | |
170 | EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick) | |
171 | EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick) | |
172 | EVT_CHAR(wxRichTextCtrl::OnChar) | |
c80e8cbe | 173 | EVT_KEY_DOWN(wxRichTextCtrl::OnChar) |
5d7836c4 JS |
174 | EVT_SIZE(wxRichTextCtrl::OnSize) |
175 | EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus) | |
176 | EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus) | |
61f84e24 | 177 | EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost) |
5d7836c4 | 178 | EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu) |
52170c5b | 179 | EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged) |
5d7836c4 JS |
180 | |
181 | EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo) | |
182 | EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo) | |
183 | ||
184 | EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo) | |
185 | EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo) | |
186 | ||
187 | EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy) | |
188 | EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy) | |
189 | ||
190 | EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste) | |
191 | EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste) | |
192 | ||
193 | EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut) | |
194 | EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut) | |
195 | ||
196 | EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear) | |
197 | EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear) | |
198 | ||
199 | EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll) | |
200 | EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll) | |
603f702b JS |
201 | |
202 | EVT_MENU(wxID_RICHTEXT_PROPERTIES1, wxRichTextCtrl::OnProperties) | |
203 | EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1, wxRichTextCtrl::OnUpdateProperties) | |
204 | ||
205 | EVT_MENU(wxID_RICHTEXT_PROPERTIES2, wxRichTextCtrl::OnProperties) | |
206 | EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2, wxRichTextCtrl::OnUpdateProperties) | |
207 | ||
208 | EVT_MENU(wxID_RICHTEXT_PROPERTIES3, wxRichTextCtrl::OnProperties) | |
209 | EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3, wxRichTextCtrl::OnUpdateProperties) | |
210 | ||
5d7836c4 JS |
211 | END_EVENT_TABLE() |
212 | ||
213 | /*! | |
214 | * wxRichTextCtrl | |
215 | */ | |
216 | ||
dadd4f55 JS |
217 | wxArrayString wxRichTextCtrl::sm_availableFontNames; |
218 | ||
5d7836c4 | 219 | wxRichTextCtrl::wxRichTextCtrl() |
343ae70d | 220 | : wxScrollHelper(this) |
5d7836c4 JS |
221 | { |
222 | Init(); | |
223 | } | |
224 | ||
343ae70d VZ |
225 | wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent, |
226 | wxWindowID id, | |
227 | const wxString& value, | |
228 | const wxPoint& pos, | |
229 | const wxSize& size, | |
4e8d9558 JS |
230 | long style, |
231 | const wxValidator& validator, | |
232 | const wxString& name) | |
343ae70d | 233 | : wxScrollHelper(this) |
5d7836c4 JS |
234 | { |
235 | Init(); | |
4e8d9558 | 236 | Create(parent, id, value, pos, size, style, validator, name); |
5d7836c4 JS |
237 | } |
238 | ||
239 | /// Creation | |
4e8d9558 JS |
240 | bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style, |
241 | const wxValidator& validator, const wxString& name) | |
5d7836c4 | 242 | { |
6d595f3d JS |
243 | style |= wxVSCROLL; |
244 | ||
24777478 | 245 | if (!wxControl::Create(parent, id, pos, size, |
0ec1179b VZ |
246 | style|wxFULL_REPAINT_ON_RESIZE, |
247 | validator, name)) | |
5d7836c4 | 248 | return false; |
5d7836c4 | 249 | |
a1b806b9 | 250 | if (!GetFont().IsOk()) |
5d7836c4 JS |
251 | { |
252 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
253 | } | |
7fe8059f | 254 | |
e9f10004 JS |
255 | // No physical scrolling, so we can preserve margins |
256 | EnableScrolling(false, false); | |
257 | ||
dadd4f55 JS |
258 | if (style & wxTE_READONLY) |
259 | SetEditable(false); | |
260 | ||
42688aea | 261 | // The base attributes must all have default values |
24777478 | 262 | wxRichTextAttr attributes; |
5d7836c4 | 263 | attributes.SetFont(GetFont()); |
79f955ec | 264 | attributes.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); |
5d7836c4 | 265 | attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT); |
3e3a754f JS |
266 | attributes.SetLineSpacing(10); |
267 | attributes.SetParagraphSpacingAfter(10); | |
268 | attributes.SetParagraphSpacingBefore(0); | |
5d7836c4 JS |
269 | SetBasicStyle(attributes); |
270 | ||
603f702b JS |
271 | int margin = 5; |
272 | SetMargins(margin, margin); | |
273 | ||
fe5aa22c JS |
274 | // The default attributes will be merged with base attributes, so |
275 | // can be empty to begin with | |
24777478 | 276 | wxRichTextAttr defaultAttributes; |
fe5aa22c JS |
277 | SetDefaultStyle(defaultAttributes); |
278 | ||
79f955ec | 279 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
5d7836c4 JS |
280 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); |
281 | ||
cd8ba0d9 JS |
282 | GetBuffer().Reset(); |
283 | GetBuffer().SetRichTextCtrl(this); | |
284 | ||
1c13f06e JS |
285 | #if wxRICHTEXT_USE_OWN_CARET |
286 | SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16)); | |
287 | #else | |
cd8ba0d9 | 288 | SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16)); |
1c13f06e | 289 | #endif |
cd8ba0d9 | 290 | |
7fe8059f | 291 | // Tell the sizers to use the given or best size |
170acdc9 | 292 | SetInitialSize(size); |
7fe8059f | 293 | |
3e3a754f | 294 | #if wxRICHTEXT_BUFFERED_PAINTING |
5d7836c4 JS |
295 | // Create a buffer |
296 | RecreateBuffer(size); | |
3e3a754f | 297 | #endif |
5d7836c4 | 298 | |
d2d0adc7 JS |
299 | m_textCursor = wxCursor(wxCURSOR_IBEAM); |
300 | m_urlCursor = wxCursor(wxCURSOR_HAND); | |
301 | ||
302 | SetCursor(m_textCursor); | |
61399247 | 303 | |
27e20452 JS |
304 | if (!value.IsEmpty()) |
305 | SetValue(value); | |
7fe8059f | 306 | |
d2d0adc7 JS |
307 | GetBuffer().AddEventHandler(this); |
308 | ||
addcf76c | 309 | // Accelerators |
0951fe0a | 310 | wxAcceleratorEntry entries[6]; |
addcf76c | 311 | |
675ec665 SC |
312 | entries[0].Set(wxACCEL_CTRL, (int) 'C', wxID_COPY); |
313 | entries[1].Set(wxACCEL_CTRL, (int) 'X', wxID_CUT); | |
314 | entries[2].Set(wxACCEL_CTRL, (int) 'V', wxID_PASTE); | |
315 | entries[3].Set(wxACCEL_CTRL, (int) 'A', wxID_SELECTALL); | |
316 | entries[4].Set(wxACCEL_CTRL, (int) 'Z', wxID_UNDO); | |
317 | entries[5].Set(wxACCEL_CTRL, (int) 'Y', wxID_REDO); | |
addcf76c | 318 | |
0951fe0a | 319 | wxAcceleratorTable accel(6, entries); |
addcf76c JS |
320 | SetAcceleratorTable(accel); |
321 | ||
8e43fc77 JS |
322 | m_contextMenu = new wxMenu; |
323 | m_contextMenu->Append(wxID_UNDO, _("&Undo")); | |
324 | m_contextMenu->Append(wxID_REDO, _("&Redo")); | |
325 | m_contextMenu->AppendSeparator(); | |
326 | m_contextMenu->Append(wxID_CUT, _("Cu&t")); | |
327 | m_contextMenu->Append(wxID_COPY, _("&Copy")); | |
328 | m_contextMenu->Append(wxID_PASTE, _("&Paste")); | |
329 | m_contextMenu->Append(wxID_CLEAR, _("&Delete")); | |
330 | m_contextMenu->AppendSeparator(); | |
331 | m_contextMenu->Append(wxID_SELECTALL, _("Select &All")); | |
cdaed652 | 332 | m_contextMenu->AppendSeparator(); |
603f702b | 333 | m_contextMenu->Append(wxID_RICHTEXT_PROPERTIES1, _("&Properties")); |
cdaed652 | 334 | |
590a0f8b JS |
335 | #if wxUSE_DRAG_AND_DROP |
336 | SetDropTarget(new wxRichTextDropTarget(this)); | |
337 | #endif | |
338 | ||
5d7836c4 JS |
339 | return true; |
340 | } | |
341 | ||
342 | wxRichTextCtrl::~wxRichTextCtrl() | |
343 | { | |
603f702b | 344 | SetFocusObject(& GetBuffer(), false); |
d2d0adc7 | 345 | GetBuffer().RemoveEventHandler(this); |
88bee7e6 | 346 | |
5d7836c4 JS |
347 | delete m_contextMenu; |
348 | } | |
349 | ||
350 | /// Member initialisation | |
351 | void wxRichTextCtrl::Init() | |
352 | { | |
5d7836c4 JS |
353 | m_contextMenu = NULL; |
354 | m_caret = NULL; | |
355 | m_caretPosition = -1; | |
5d7836c4 | 356 | m_selectionAnchor = -2; |
603f702b JS |
357 | m_selectionAnchorObject = NULL; |
358 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
5d7836c4 | 359 | m_editable = true; |
f7667b84 | 360 | m_useVirtualAttributes = false; |
12f5219f | 361 | m_verticalScrollbarEnabled = true; |
5d7836c4 JS |
362 | m_caretAtLineStart = false; |
363 | m_dragging = false; | |
9fa7b436 | 364 | #if wxUSE_DRAG_AND_DROP |
0c0e063e | 365 | m_preDrag = false; |
9fa7b436 | 366 | #endif |
4d551ad5 JS |
367 | m_fullLayoutRequired = false; |
368 | m_fullLayoutTime = 0; | |
369 | m_fullLayoutSavedPosition = 0; | |
370 | m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD; | |
ab14c7aa | 371 | m_caretPositionForDefaultStyle = -2; |
603f702b | 372 | m_focusObject = & m_buffer; |
4ba36292 | 373 | m_scale = 1.0; |
5d7836c4 JS |
374 | } |
375 | ||
17808a75 | 376 | void wxRichTextCtrl::DoThaw() |
5d7836c4 | 377 | { |
603f702b | 378 | if (GetBuffer().IsDirty()) |
17808a75 VZ |
379 | LayoutContent(); |
380 | else | |
381 | SetupScrollbars(); | |
8251c171 | 382 | |
6a8e61e0 | 383 | wxWindow::DoThaw(); |
5d7836c4 JS |
384 | } |
385 | ||
386 | /// Clear all text | |
387 | void wxRichTextCtrl::Clear() | |
388 | { | |
603f702b JS |
389 | if (GetFocusObject() == & GetBuffer()) |
390 | { | |
391 | m_buffer.ResetAndClearCommands(); | |
392 | m_buffer.Invalidate(wxRICHTEXT_ALL); | |
393 | } | |
394 | else | |
395 | { | |
396 | GetFocusObject()->Reset(); | |
397 | } | |
398 | ||
5d7836c4 | 399 | m_caretPosition = -1; |
ab14c7aa | 400 | m_caretPositionForDefaultStyle = -2; |
5d7836c4 | 401 | m_caretAtLineStart = false; |
603f702b JS |
402 | m_selection.Reset(); |
403 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
5d7836c4 | 404 | |
495a248e JS |
405 | Scroll(0,0); |
406 | ||
17808a75 | 407 | if (!IsFrozen()) |
5d7836c4 | 408 | { |
cecf8eee | 409 | LayoutContent(); |
76bcd815 | 410 | Refresh(false); |
5d7836c4 | 411 | } |
0ec1179b VZ |
412 | |
413 | wxTextCtrl::SendTextUpdatedEvent(this); | |
5d7836c4 JS |
414 | } |
415 | ||
416 | /// Painting | |
417 | void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
418 | { | |
1c13f06e JS |
419 | #if !wxRICHTEXT_USE_OWN_CARET |
420 | if (GetCaret() && !IsFrozen()) | |
d2142335 | 421 | GetCaret()->Hide(); |
cd45ed36 JS |
422 | #else |
423 | // Stop the caret refreshing the control from within the | |
424 | // paint handler | |
425 | if (GetCaret()) | |
426 | ((wxRichTextCaret*) GetCaret())->EnableRefresh(false); | |
1c13f06e | 427 | #endif |
d2142335 | 428 | |
5d7836c4 | 429 | { |
3e3a754f | 430 | #if wxRICHTEXT_BUFFERED_PAINTING |
c59f6793 | 431 | wxBufferedPaintDC dc(this, m_bufferBitmap); |
3e3a754f JS |
432 | #else |
433 | wxPaintDC dc(this); | |
434 | #endif | |
41e155b4 | 435 | |
17808a75 | 436 | if (IsFrozen()) |
c59f6793 | 437 | return; |
41e155b4 | 438 | |
17808a75 VZ |
439 | PrepareDC(dc); |
440 | ||
c59f6793 | 441 | dc.SetFont(GetFont()); |
41e155b4 | 442 | |
ea160b2e | 443 | wxRect drawingArea(GetUpdateRegion().GetBox()); |
4ba36292 JS |
444 | drawingArea.SetPosition(GetUnscaledPoint(GetLogicalPoint(drawingArea.GetPosition()))); |
445 | drawingArea.SetSize(GetUnscaledSize(drawingArea.GetSize())); | |
ea160b2e | 446 | |
4ba36292 | 447 | wxRect availableSpace(GetUnscaledSize(GetClientSize())); |
8db2e3ef | 448 | wxRichTextDrawingContext context(& GetBuffer()); |
603f702b | 449 | if (GetBuffer().IsDirty()) |
c59f6793 | 450 | { |
4ba36292 JS |
451 | dc.SetUserScale(GetScale(), GetScale()); |
452 | ||
8db2e3ef | 453 | GetBuffer().Layout(dc, context, availableSpace, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT); |
603f702b | 454 | GetBuffer().Invalidate(wxRICHTEXT_NONE); |
4ba36292 JS |
455 | |
456 | dc.SetUserScale(1.0, 1.0); | |
457 | ||
c59f6793 JS |
458 | SetupScrollbars(); |
459 | } | |
41e155b4 | 460 | |
914a4e23 JS |
461 | // Paint the background |
462 | PaintBackground(dc); | |
463 | ||
e9f10004 JS |
464 | wxRect clipRect(availableSpace); |
465 | clipRect.x += GetBuffer().GetLeftMargin(); | |
466 | clipRect.y += GetBuffer().GetTopMargin(); | |
467 | clipRect.width -= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin()); | |
468 | clipRect.height -= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin()); | |
4ba36292 JS |
469 | |
470 | clipRect = GetScaledRect(clipRect); | |
e9f10004 | 471 | clipRect.SetPosition(GetLogicalPoint(clipRect.GetPosition())); |
4ba36292 | 472 | |
e9f10004 JS |
473 | dc.SetClippingRegion(clipRect); |
474 | ||
603f702b JS |
475 | int flags = 0; |
476 | if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES) == 0) | |
477 | flags |= wxRICHTEXT_DRAW_GUIDELINES; | |
4fe83b93 | 478 | |
4ba36292 JS |
479 | dc.SetUserScale(GetScale(), GetScale()); |
480 | ||
8db2e3ef | 481 | GetBuffer().Draw(dc, context, GetBuffer().GetOwnRange(), GetSelection(), drawingArea, 0 /* descent */, flags); |
e9f10004 JS |
482 | |
483 | dc.DestroyClippingRegion(); | |
484 | ||
35bdbae5 JS |
485 | // Other user defined painting after everything else (i.e. all text) is painted |
486 | PaintAboveContent(dc); | |
487 | ||
1c13f06e JS |
488 | #if wxRICHTEXT_USE_OWN_CARET |
489 | if (GetCaret()->IsVisible()) | |
490 | { | |
cd45ed36 | 491 | PositionCaret(); |
1c13f06e JS |
492 | ((wxRichTextCaret*) GetCaret())->DoDraw(& dc); |
493 | } | |
494 | #endif | |
4ba36292 JS |
495 | |
496 | dc.SetUserScale(1.0, 1.0); | |
5d7836c4 | 497 | } |
d2142335 | 498 | |
1c13f06e JS |
499 | #if !wxRICHTEXT_USE_OWN_CARET |
500 | if (GetCaret()) | |
d2142335 | 501 | GetCaret()->Show(); |
7383cf9d | 502 | PositionCaret(); |
5219cf05 | 503 | #else |
cd45ed36 JS |
504 | if (GetCaret()) |
505 | ((wxRichTextCaret*) GetCaret())->EnableRefresh(true); | |
1c13f06e | 506 | #endif |
5d7836c4 JS |
507 | } |
508 | ||
509 | // Empty implementation, to prevent flicker | |
510 | void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) | |
511 | { | |
512 | } | |
513 | ||
514 | void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) | |
515 | { | |
ea160b2e JS |
516 | if (GetCaret()) |
517 | { | |
1c13f06e | 518 | #if !wxRICHTEXT_USE_OWN_CARET |
ea160b2e | 519 | PositionCaret(); |
1c13f06e JS |
520 | #endif |
521 | GetCaret()->Show(); | |
ea160b2e | 522 | } |
c59f6793 | 523 | |
1c13f06e JS |
524 | #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET |
525 | // Work around dropouts when control is focused | |
526 | if (!IsFrozen()) | |
527 | { | |
528 | Refresh(false); | |
529 | } | |
530 | #endif | |
5d7836c4 JS |
531 | } |
532 | ||
533 | void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event)) | |
534 | { | |
1c13f06e | 535 | if (GetCaret()) |
ea160b2e | 536 | GetCaret()->Hide(); |
c59f6793 | 537 | |
1c13f06e JS |
538 | #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET |
539 | // Work around dropouts when control is focused | |
540 | if (!IsFrozen()) | |
541 | { | |
542 | Refresh(false); | |
543 | } | |
544 | #endif | |
5d7836c4 JS |
545 | } |
546 | ||
61f84e24 JS |
547 | void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) |
548 | { | |
88bee7e6 | 549 | m_dragging = false; |
61f84e24 JS |
550 | } |
551 | ||
603f702b JS |
552 | // Set up the caret for the given position and container, after a mouse click |
553 | bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox* container, long position, int hitTestFlags, bool extendSelection) | |
554 | { | |
555 | bool caretAtLineStart = false; | |
556 | ||
557 | if (hitTestFlags & wxRICHTEXT_HITTEST_BEFORE) | |
558 | { | |
559 | // If we're at the start of a line (but not first in para) | |
560 | // then we should keep the caret showing at the start of the line | |
561 | // by showing the m_caretAtLineStart flag. | |
562 | wxRichTextParagraph* para = container->GetParagraphAtPosition(position); | |
563 | wxRichTextLine* line = container->GetLineAtPosition(position); | |
564 | ||
565 | if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position) | |
566 | caretAtLineStart = true; | |
567 | position --; | |
568 | } | |
569 | ||
570 | if (extendSelection && (m_caretPosition != position)) | |
571 | ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN); | |
572 | ||
573 | MoveCaret(position, caretAtLineStart); | |
574 | SetDefaultStyleToCursorStyle(); | |
4fe83b93 | 575 | |
603f702b JS |
576 | return true; |
577 | } | |
578 | ||
5d7836c4 JS |
579 | /// Left-click |
580 | void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event) | |
581 | { | |
582 | SetFocus(); | |
583 | ||
584 | wxClientDC dc(this); | |
585 | PrepareDC(dc); | |
586 | dc.SetFont(GetFont()); | |
7fe8059f | 587 | |
603f702b | 588 | // TODO: detect change of focus object |
5d7836c4 | 589 | long position = 0; |
603f702b JS |
590 | wxRichTextObject* hitObj = NULL; |
591 | wxRichTextObject* contextObj = NULL; | |
8db2e3ef | 592 | wxRichTextDrawingContext context(& GetBuffer()); |
4ba36292 | 593 | int hit = GetBuffer().HitTest(dc, context, GetUnscaledPoint(event.GetLogicalPosition(dc)), position, & hitObj, & contextObj, wxRICHTEXT_HITTEST_HONOUR_ATOMIC); |
5d7836c4 | 594 | |
0c0e063e JS |
595 | #if wxUSE_DRAG_AND_DROP |
596 | // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop | |
b33e0c2c | 597 | if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position)) |
0c0e063e JS |
598 | { |
599 | // This might be an attempt at initiating Drag'n'Drop. So set the time & flags | |
600 | m_preDrag = true; | |
601 | m_dragStartPoint = event.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt | |
602 | ||
603 | #if wxUSE_DATETIME | |
604 | m_dragStartTime = wxDateTime::UNow(); | |
605 | #endif // wxUSE_DATETIME | |
606 | ||
607 | // Preserve behaviour of clicking on an object within the selection | |
608 | if (hit != wxRICHTEXT_HITTEST_NONE && hitObj) | |
609 | m_dragging = true; | |
610 | ||
611 | return; // Don't skip the event, else the selection will be lost | |
612 | } | |
613 | #endif // wxUSE_DRAG_AND_DROP | |
614 | ||
603f702b | 615 | if (hit != wxRICHTEXT_HITTEST_NONE && hitObj) |
5d7836c4 | 616 | { |
603f702b JS |
617 | wxRichTextParagraphLayoutBox* oldFocusObject = GetFocusObject(); |
618 | wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); | |
619 | if (container && container != GetFocusObject() && container->AcceptsFocus()) | |
620 | { | |
621 | SetFocusObject(container, false /* don't set caret position yet */); | |
622 | } | |
4fe83b93 | 623 | |
5d7836c4 JS |
624 | m_dragging = true; |
625 | CaptureMouse(); | |
626 | ||
f7222f4c JS |
627 | long oldCaretPos = m_caretPosition; |
628 | ||
603f702b | 629 | SetCaretPositionAfterClick(container, position, hit); |
f7222f4c | 630 | |
603f702b JS |
631 | // For now, don't handle shift-click when we're selecting multiple objects. |
632 | if (event.ShiftDown() && GetFocusObject() == oldFocusObject && m_selectionState == wxRichTextCtrlSelectionState_Normal) | |
77ea6c48 | 633 | ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN); |
f7222f4c JS |
634 | else |
635 | SelectNone(); | |
5d7836c4 JS |
636 | } |
637 | ||
638 | event.Skip(); | |
639 | } | |
640 | ||
641 | /// Left-up | |
d2d0adc7 | 642 | void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event) |
5d7836c4 JS |
643 | { |
644 | if (m_dragging) | |
645 | { | |
646 | m_dragging = false; | |
647 | if (GetCapture() == this) | |
648 | ReleaseMouse(); | |
d2d0adc7 JS |
649 | |
650 | // See if we clicked on a URL | |
651 | wxClientDC dc(this); | |
652 | PrepareDC(dc); | |
653 | dc.SetFont(GetFont()); | |
654 | ||
655 | long position = 0; | |
656 | wxPoint logicalPt = event.GetLogicalPosition(dc); | |
603f702b JS |
657 | wxRichTextObject* hitObj = NULL; |
658 | wxRichTextObject* contextObj = NULL; | |
8db2e3ef | 659 | wxRichTextDrawingContext context(& GetBuffer()); |
603f702b | 660 | // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level. |
4ba36292 | 661 | int hit = GetFocusObject()->HitTest(dc, context, GetUnscaledPoint(logicalPt), position, & hitObj, & contextObj, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS|wxRICHTEXT_HITTEST_HONOUR_ATOMIC); |
88bee7e6 | 662 | |
0c0e063e JS |
663 | #if wxUSE_DRAG_AND_DROP |
664 | if (m_preDrag) | |
665 | { | |
666 | // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick | |
667 | m_preDrag = false; // Tell DnD not to happen now: we are processing Left Up ourselves. | |
668 | ||
669 | // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag | |
670 | long position = 0; | |
671 | wxRichTextObject* hitObj = NULL; | |
672 | wxRichTextObject* contextObj = NULL; | |
4ba36292 | 673 | int hit = GetBuffer().HitTest(dc, context, GetUnscaledPoint(event.GetLogicalPosition(dc)), position, & hitObj, & contextObj, wxRICHTEXT_HITTEST_HONOUR_ATOMIC); |
0c0e063e JS |
674 | wxRichTextParagraphLayoutBox* oldFocusObject = GetFocusObject(); |
675 | wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); | |
676 | if (container && container != GetFocusObject() && container->AcceptsFocus()) | |
677 | { | |
678 | SetFocusObject(container, false /* don't set caret position yet */); | |
679 | } | |
680 | ||
681 | long oldCaretPos = m_caretPosition; | |
682 | ||
683 | SetCaretPositionAfterClick(container, position, hit); | |
684 | ||
685 | // For now, don't handle shift-click when we're selecting multiple objects. | |
686 | if (event.ShiftDown() && GetFocusObject() == oldFocusObject && m_selectionState == wxRichTextCtrlSelectionState_Normal) | |
687 | ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN); | |
688 | else | |
689 | SelectNone(); | |
690 | } | |
691 | #endif | |
692 | ||
62381daa | 693 | if ((hit != wxRICHTEXT_HITTEST_NONE) && !(hit & wxRICHTEXT_HITTEST_OUTSIDE)) |
d2d0adc7 | 694 | { |
db6634e0 | 695 | wxRichTextEvent cmdEvent( |
ce7fe42e | 696 | wxEVT_RICHTEXT_LEFT_CLICK, |
db6634e0 JS |
697 | GetId()); |
698 | cmdEvent.SetEventObject(this); | |
603f702b JS |
699 | cmdEvent.SetPosition(position); |
700 | if (hitObj) | |
701 | cmdEvent.SetContainer(hitObj->GetContainer()); | |
db6634e0 JS |
702 | |
703 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) | |
d2d0adc7 | 704 | { |
24777478 | 705 | wxRichTextAttr attr; |
db6634e0 | 706 | if (GetStyle(position, attr)) |
d2d0adc7 | 707 | { |
db6634e0 | 708 | if (attr.HasFlag(wxTEXT_ATTR_URL)) |
d2d0adc7 | 709 | { |
db6634e0 JS |
710 | wxString urlTarget = attr.GetURL(); |
711 | if (!urlTarget.IsEmpty()) | |
d2d0adc7 | 712 | { |
db6634e0 | 713 | wxMouseEvent mouseEvent(event); |
1a75935d | 714 | |
db6634e0 | 715 | long startPos = 0, endPos = 0; |
603f702b | 716 | wxRichTextObject* obj = GetFocusObject()->GetLeafObjectAtPosition(position); |
db6634e0 JS |
717 | if (obj) |
718 | { | |
719 | startPos = obj->GetRange().GetStart(); | |
720 | endPos = obj->GetRange().GetEnd(); | |
721 | } | |
1a75935d | 722 | |
db6634e0 JS |
723 | wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos); |
724 | InitCommandEvent(urlEvent); | |
1a75935d | 725 | |
db6634e0 | 726 | urlEvent.SetString(urlTarget); |
1a75935d | 727 | |
db6634e0 | 728 | GetEventHandler()->ProcessEvent(urlEvent); |
88bee7e6 | 729 | } |
d2d0adc7 JS |
730 | } |
731 | } | |
732 | } | |
733 | } | |
5d7836c4 | 734 | } |
5dec941a | 735 | |
0c0e063e JS |
736 | #if wxUSE_DRAG_AND_DROP |
737 | m_preDrag = false; | |
738 | #endif // wxUSE_DRAG_AND_DROP | |
739 | ||
5dec941a JS |
740 | #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION |
741 | if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer()) | |
742 | { | |
743 | // Put the selection in PRIMARY, if it exists | |
744 | wxTheClipboard->UsePrimarySelection(true); | |
745 | ||
746 | wxRichTextRange range = GetInternalSelectionRange(); | |
747 | GetFocusObject()->GetBuffer()->CopyToClipboard(range); | |
748 | ||
749 | wxTheClipboard->UsePrimarySelection(false); | |
750 | } | |
751 | #endif | |
5d7836c4 JS |
752 | } |
753 | ||
0c0e063e | 754 | /// Mouse-movements |
5d7836c4 JS |
755 | void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event) |
756 | { | |
f5613c9b JS |
757 | if (!event.Dragging() && m_dragging) |
758 | { | |
759 | // We may have accidentally lost a mouse-up event, especially on Linux | |
760 | m_dragging = false; | |
761 | if (GetCapture() == this) | |
762 | ReleaseMouse(); | |
763 | } | |
764 | ||
0c0e063e | 765 | #if wxUSE_DRAG_AND_DROP |
f819ed5d JS |
766 | size_t distance = 0; |
767 | if (m_preDrag || m_dragging) | |
0c0e063e JS |
768 | { |
769 | int x = m_dragStartPoint.x - event.GetPosition().x; | |
770 | int y = m_dragStartPoint.y - event.GetPosition().y; | |
f819ed5d JS |
771 | distance = abs(x) + abs(y); |
772 | } | |
773 | ||
774 | // See if we're starting Drag'n'Drop | |
775 | if (m_preDrag) | |
776 | { | |
0c0e063e JS |
777 | #if wxUSE_DATETIME |
778 | wxTimeSpan diff = wxDateTime::UNow() - m_dragStartTime; | |
779 | #endif | |
780 | if ((distance > 10) | |
781 | #if wxUSE_DATETIME | |
782 | && (diff.GetMilliseconds() > 100) | |
783 | #endif | |
784 | ) | |
785 | { | |
786 | m_dragging = false; | |
787 | ||
788 | // Start drag'n'drop | |
789 | wxRichTextRange range = GetInternalSelectionRange(); | |
790 | if (range == wxRICHTEXT_NONE) | |
791 | { | |
792 | // Don't try to drag an empty range | |
793 | m_preDrag = false; | |
794 | return; | |
795 | } | |
796 | ||
797 | // Cache the current situation, to be restored if Drag'n'Drop is cancelled | |
798 | long oldPos = GetCaretPosition(); | |
799 | wxRichTextParagraphLayoutBox* oldFocus = GetFocusObject(); | |
800 | ||
801 | wxDataObjectComposite* compositeObject = new wxDataObjectComposite(); | |
802 | wxString text = GetFocusObject()->GetTextForRange(range); | |
803 | #ifdef __WXMSW__ | |
804 | text = wxTextFile::Translate(text, wxTextFileType_Dos); | |
805 | #endif | |
806 | compositeObject->Add(new wxTextDataObject(text), false /* not preferred */); | |
807 | ||
808 | wxRichTextBuffer* richTextBuf = new wxRichTextBuffer; | |
809 | GetFocusObject()->CopyFragment(range, *richTextBuf); | |
810 | compositeObject->Add(new wxRichTextBufferDataObject(richTextBuf), true /* preferred */); | |
811 | ||
812 | wxRichTextDropSource source(*compositeObject, this); | |
813 | // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise | |
814 | // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed | |
815 | BeginBatchUndo(_("Drag")); | |
816 | switch (source.DoDragDrop(wxDrag_AllowMove | wxDrag_DefaultMove)) | |
817 | { | |
818 | case wxDragMove: | |
819 | case wxDragCopy: break; | |
820 | ||
821 | case wxDragError: | |
822 | wxLogError(wxT("An error occurred during drag and drop operation")); | |
823 | case wxDragNone: | |
824 | case wxDragCancel: | |
825 | Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take' | |
826 | SetCaretPosition(oldPos); | |
827 | SetFocusObject(oldFocus, false); | |
828 | default: break; | |
829 | } | |
830 | EndBatchUndo(); | |
831 | ||
832 | m_preDrag = false; | |
833 | return; | |
834 | } | |
835 | } | |
836 | #endif // wxUSE_DRAG_AND_DROP | |
837 | ||
5d7836c4 JS |
838 | wxClientDC dc(this); |
839 | PrepareDC(dc); | |
840 | dc.SetFont(GetFont()); | |
7fe8059f | 841 | |
5d7836c4 JS |
842 | long position = 0; |
843 | wxPoint logicalPt = event.GetLogicalPosition(dc); | |
603f702b JS |
844 | wxRichTextObject* hitObj = NULL; |
845 | wxRichTextObject* contextObj = NULL; | |
4fe83b93 | 846 | |
603f702b | 847 | int flags = 0; |
4fe83b93 | 848 | |
603f702b JS |
849 | // If we're dragging, let's only consider positions at this level; otherwise |
850 | // selecting a range is not going to work. | |
851 | wxRichTextParagraphLayoutBox* container = & GetBuffer(); | |
852 | if (m_dragging) | |
853 | { | |
854 | flags = wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS; | |
855 | container = GetFocusObject(); | |
856 | } | |
8db2e3ef | 857 | wxRichTextDrawingContext context(& GetBuffer()); |
4ba36292 | 858 | int hit = container->HitTest(dc, context, GetUnscaledPoint(logicalPt), position, & hitObj, & contextObj, flags); |
4fe83b93 | 859 | |
d2d0adc7 | 860 | // See if we need to change the cursor |
88bee7e6 | 861 | |
d2d0adc7 | 862 | { |
603f702b | 863 | if (hit != wxRICHTEXT_HITTEST_NONE && !(hit & wxRICHTEXT_HITTEST_OUTSIDE) && hitObj) |
d2d0adc7 | 864 | { |
603f702b | 865 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); |
590a0f8b JS |
866 | if (actualContainer) |
867 | ProcessMouseMovement(actualContainer, hitObj, position, logicalPt); | |
d2d0adc7 | 868 | } |
f262b25c JS |
869 | else |
870 | SetCursor(m_textCursor); | |
d2d0adc7 JS |
871 | } |
872 | ||
873 | if (!event.Dragging()) | |
874 | { | |
875 | event.Skip(); | |
876 | return; | |
877 | } | |
4fe83b93 | 878 | |
0c0e063e JS |
879 | if (m_dragging |
880 | #if wxUSE_DRAG_AND_DROP | |
881 | && !m_preDrag | |
f819ed5d | 882 | && (distance > 4) |
55e01008 | 883 | #endif |
0c0e063e | 884 | ) |
5d7836c4 | 885 | { |
603f702b JS |
886 | wxRichTextParagraphLayoutBox* commonAncestor = NULL; |
887 | wxRichTextParagraphLayoutBox* otherContainer = NULL; | |
888 | wxRichTextParagraphLayoutBox* firstContainer = NULL; | |
889 | ||
890 | // Check for dragging across multiple containers | |
891 | long position2 = 0; | |
892 | wxRichTextObject* hitObj2 = NULL, *contextObj2 = NULL; | |
4ba36292 | 893 | int hit2 = GetBuffer().HitTest(dc, context, GetUnscaledPoint(logicalPt), position2, & hitObj2, & contextObj2, 0); |
603f702b JS |
894 | if (hit2 != wxRICHTEXT_HITTEST_NONE && !(hit2 & wxRICHTEXT_HITTEST_OUTSIDE) && hitObj2 && hitObj != hitObj2) |
895 | { | |
896 | // See if we can find a common ancestor | |
897 | if (m_selectionState == wxRichTextCtrlSelectionState_Normal) | |
898 | { | |
899 | firstContainer = GetFocusObject(); | |
900 | commonAncestor = wxDynamicCast(firstContainer->GetParent(), wxRichTextParagraphLayoutBox); | |
901 | } | |
902 | else | |
903 | { | |
904 | firstContainer = wxDynamicCast(m_selectionAnchorObject, wxRichTextParagraphLayoutBox); | |
905 | //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table) | |
906 | // is the common ancestor. | |
907 | commonAncestor = wxDynamicCast(firstContainer->GetParent(), wxRichTextParagraphLayoutBox); | |
908 | } | |
4fe83b93 | 909 | |
603f702b JS |
910 | if (commonAncestor && commonAncestor->HandlesChildSelections()) |
911 | { | |
912 | wxRichTextObject* p = hitObj2; | |
913 | while (p) | |
914 | { | |
915 | if (p->GetParent() == commonAncestor) | |
916 | { | |
917 | otherContainer = wxDynamicCast(p, wxRichTextParagraphLayoutBox); | |
918 | break; | |
919 | } | |
920 | p = p->GetParent(); | |
921 | } | |
922 | } | |
5d7836c4 | 923 | |
603f702b JS |
924 | if (commonAncestor && firstContainer && otherContainer) |
925 | { | |
926 | // We have now got a second container that shares a parent with the current or anchor object. | |
927 | if (m_selectionState == wxRichTextCtrlSelectionState_Normal) | |
928 | { | |
929 | // Don't go into common-ancestor selection mode if we still have the same | |
930 | // container. | |
931 | if (otherContainer != firstContainer) | |
932 | { | |
933 | m_selectionState = wxRichTextCtrlSelectionState_CommonAncestor; | |
934 | m_selectionAnchorObject = firstContainer; | |
935 | m_selectionAnchor = firstContainer->GetRange().GetStart(); | |
5d7836c4 | 936 | |
603f702b JS |
937 | // The common ancestor, such as a table, returns the cell selection |
938 | // between the anchor and current position. | |
939 | m_selection = commonAncestor->GetSelection(m_selectionAnchor, otherContainer->GetRange().GetStart()); | |
940 | } | |
941 | } | |
942 | else | |
943 | { | |
944 | m_selection = commonAncestor->GetSelection(m_selectionAnchor, otherContainer->GetRange().GetStart()); | |
945 | } | |
5d7836c4 | 946 | |
603f702b | 947 | Refresh(); |
7fe8059f | 948 | |
603f702b JS |
949 | if (otherContainer->AcceptsFocus()) |
950 | SetFocusObject(otherContainer, false /* don't set caret and clear selection */); | |
4fe83b93 | 951 | MoveCaret(-1, false); |
603f702b JS |
952 | SetDefaultStyleToCursorStyle(); |
953 | } | |
5d7836c4 JS |
954 | } |
955 | } | |
603f702b | 956 | |
0c0e063e JS |
957 | if (hitObj && m_dragging && hit != wxRICHTEXT_HITTEST_NONE && m_selectionState == wxRichTextCtrlSelectionState_Normal |
958 | #if wxUSE_DRAG_AND_DROP | |
959 | && !m_preDrag | |
f819ed5d | 960 | && (distance > 4) |
55e01008 | 961 | #endif |
0c0e063e | 962 | ) |
603f702b | 963 | { |
603f702b JS |
964 | SetCaretPositionAfterClick(container, position, hit, true /* extend selection */); |
965 | } | |
5d7836c4 JS |
966 | } |
967 | ||
968 | /// Right-click | |
52170c5b | 969 | void wxRichTextCtrl::OnRightClick(wxMouseEvent& event) |
5d7836c4 JS |
970 | { |
971 | SetFocus(); | |
4fe83b93 | 972 | |
603f702b JS |
973 | wxClientDC dc(this); |
974 | PrepareDC(dc); | |
975 | dc.SetFont(GetFont()); | |
976 | ||
977 | long position = 0; | |
978 | wxPoint logicalPt = event.GetLogicalPosition(dc); | |
979 | wxRichTextObject* hitObj = NULL; | |
980 | wxRichTextObject* contextObj = NULL; | |
8db2e3ef | 981 | wxRichTextDrawingContext context(& GetBuffer()); |
4ba36292 | 982 | int hit = GetFocusObject()->HitTest(dc, context, GetUnscaledPoint(logicalPt), position, & hitObj, & contextObj, wxRICHTEXT_HITTEST_HONOUR_ATOMIC); |
603f702b JS |
983 | |
984 | if (hitObj && hitObj->GetContainer() != GetFocusObject()) | |
985 | { | |
986 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); | |
987 | if (actualContainer && actualContainer->AcceptsFocus()) | |
988 | { | |
989 | SetFocusObject(actualContainer, false /* don't set caret position yet */); | |
990 | SetCaretPositionAfterClick(actualContainer, position, hit); | |
991 | } | |
992 | } | |
db6634e0 JS |
993 | |
994 | wxRichTextEvent cmdEvent( | |
ce7fe42e | 995 | wxEVT_RICHTEXT_RIGHT_CLICK, |
db6634e0 JS |
996 | GetId()); |
997 | cmdEvent.SetEventObject(this); | |
603f702b JS |
998 | cmdEvent.SetPosition(position); |
999 | if (hitObj) | |
1000 | cmdEvent.SetContainer(hitObj->GetContainer()); | |
1a75935d | 1001 | |
52170c5b JS |
1002 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) |
1003 | event.Skip(); | |
5d7836c4 JS |
1004 | } |
1005 | ||
1006 | /// Left-double-click | |
db6634e0 | 1007 | void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& WXUNUSED(event)) |
5d7836c4 | 1008 | { |
db6634e0 | 1009 | wxRichTextEvent cmdEvent( |
ce7fe42e | 1010 | wxEVT_RICHTEXT_LEFT_DCLICK, |
db6634e0 JS |
1011 | GetId()); |
1012 | cmdEvent.SetEventObject(this); | |
1013 | cmdEvent.SetPosition(m_caretPosition+1); | |
603f702b | 1014 | cmdEvent.SetContainer(GetFocusObject()); |
1a75935d | 1015 | |
db6634e0 JS |
1016 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) |
1017 | { | |
1018 | SelectWord(GetCaretPosition()+1); | |
1019 | } | |
5d7836c4 JS |
1020 | } |
1021 | ||
1022 | /// Middle-click | |
1023 | void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event) | |
1024 | { | |
db6634e0 | 1025 | wxRichTextEvent cmdEvent( |
ce7fe42e | 1026 | wxEVT_RICHTEXT_MIDDLE_CLICK, |
db6634e0 JS |
1027 | GetId()); |
1028 | cmdEvent.SetEventObject(this); | |
1029 | cmdEvent.SetPosition(m_caretPosition+1); | |
603f702b | 1030 | cmdEvent.SetContainer(GetFocusObject()); |
1a75935d | 1031 | |
db6634e0 JS |
1032 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) |
1033 | event.Skip(); | |
5dec941a JS |
1034 | |
1035 | #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION | |
1036 | // Paste any PRIMARY selection, if it exists | |
1037 | wxTheClipboard->UsePrimarySelection(true); | |
1038 | Paste(); | |
1039 | wxTheClipboard->UsePrimarySelection(false); | |
1040 | #endif | |
5d7836c4 JS |
1041 | } |
1042 | ||
1043 | /// Key press | |
1044 | void wxRichTextCtrl::OnChar(wxKeyEvent& event) | |
1045 | { | |
1046 | int flags = 0; | |
3e3a754f | 1047 | if (event.CmdDown()) |
5d7836c4 JS |
1048 | flags |= wxRICHTEXT_CTRL_DOWN; |
1049 | if (event.ShiftDown()) | |
1050 | flags |= wxRICHTEXT_SHIFT_DOWN; | |
1051 | if (event.AltDown()) | |
1052 | flags |= wxRICHTEXT_ALT_DOWN; | |
1053 | ||
c80e8cbe JS |
1054 | if (event.GetEventType() == wxEVT_KEY_DOWN) |
1055 | { | |
7a34307e | 1056 | if (event.IsKeyInCategory(WXK_CATEGORY_NAVIGATION)) |
8c9e72f9 JS |
1057 | { |
1058 | KeyboardNavigate(event.GetKeyCode(), flags); | |
1059 | return; | |
1060 | } | |
1061 | ||
1062 | long keycode = event.GetKeyCode(); | |
1063 | switch ( keycode ) | |
1064 | { | |
1065 | case WXK_ESCAPE: | |
1066 | case WXK_START: | |
1067 | case WXK_LBUTTON: | |
1068 | case WXK_RBUTTON: | |
1069 | case WXK_CANCEL: | |
1070 | case WXK_MBUTTON: | |
1071 | case WXK_CLEAR: | |
1072 | case WXK_SHIFT: | |
1073 | case WXK_ALT: | |
1074 | case WXK_CONTROL: | |
1075 | case WXK_MENU: | |
1076 | case WXK_PAUSE: | |
1077 | case WXK_CAPITAL: | |
1078 | case WXK_END: | |
1079 | case WXK_HOME: | |
1080 | case WXK_LEFT: | |
1081 | case WXK_UP: | |
1082 | case WXK_RIGHT: | |
1083 | case WXK_DOWN: | |
1084 | case WXK_SELECT: | |
1085 | case WXK_PRINT: | |
1086 | case WXK_EXECUTE: | |
1087 | case WXK_SNAPSHOT: | |
1088 | case WXK_INSERT: | |
1089 | case WXK_HELP: | |
1090 | case WXK_F1: | |
1091 | case WXK_F2: | |
1092 | case WXK_F3: | |
1093 | case WXK_F4: | |
1094 | case WXK_F5: | |
1095 | case WXK_F6: | |
1096 | case WXK_F7: | |
1097 | case WXK_F8: | |
1098 | case WXK_F9: | |
1099 | case WXK_F10: | |
1100 | case WXK_F11: | |
1101 | case WXK_F12: | |
1102 | case WXK_F13: | |
1103 | case WXK_F14: | |
1104 | case WXK_F15: | |
1105 | case WXK_F16: | |
1106 | case WXK_F17: | |
1107 | case WXK_F18: | |
1108 | case WXK_F19: | |
1109 | case WXK_F20: | |
1110 | case WXK_F21: | |
1111 | case WXK_F22: | |
1112 | case WXK_F23: | |
1113 | case WXK_F24: | |
1114 | case WXK_NUMLOCK: | |
1115 | case WXK_SCROLL: | |
1116 | case WXK_PAGEUP: | |
1117 | case WXK_PAGEDOWN: | |
1118 | case WXK_NUMPAD_F1: | |
1119 | case WXK_NUMPAD_F2: | |
1120 | case WXK_NUMPAD_F3: | |
1121 | case WXK_NUMPAD_F4: | |
1122 | case WXK_NUMPAD_HOME: | |
1123 | case WXK_NUMPAD_LEFT: | |
1124 | case WXK_NUMPAD_UP: | |
1125 | case WXK_NUMPAD_RIGHT: | |
1126 | case WXK_NUMPAD_DOWN: | |
1127 | case WXK_NUMPAD_PAGEUP: | |
1128 | case WXK_NUMPAD_PAGEDOWN: | |
1129 | case WXK_NUMPAD_END: | |
1130 | case WXK_NUMPAD_BEGIN: | |
1131 | case WXK_NUMPAD_INSERT: | |
8c9e72f9 JS |
1132 | case WXK_WINDOWS_LEFT: |
1133 | { | |
1134 | return; | |
1135 | } | |
1136 | default: | |
1137 | { | |
1138 | } | |
1139 | } | |
1140 | ||
c80e8cbe JS |
1141 | // Must process this before translation, otherwise it's translated into a WXK_DELETE event. |
1142 | if (event.CmdDown() && event.GetKeyCode() == WXK_BACK) | |
1143 | { | |
4ce3ebd3 | 1144 | if (!ProcessBackKey(event, flags)) |
cc2aecde | 1145 | return; |
c80e8cbe JS |
1146 | } |
1147 | else | |
1148 | event.Skip(); | |
1149 | ||
1150 | return; | |
1151 | } | |
1152 | ||
99257cbe VZ |
1153 | // all the other keys modify the controls contents which shouldn't be |
1154 | // possible if we're read-only | |
1155 | if ( !IsEditable() ) | |
1156 | { | |
1157 | event.Skip(); | |
1158 | return; | |
1159 | } | |
1160 | ||
1161 | if (event.GetKeyCode() == WXK_RETURN) | |
5d7836c4 | 1162 | { |
cc2aecde JS |
1163 | if (!CanInsertContent(* GetFocusObject(), m_caretPosition+1)) |
1164 | return; | |
5d7836c4 JS |
1165 | |
1166 | long newPos = m_caretPosition; | |
1167 | ||
cc2aecde JS |
1168 | if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange())) |
1169 | { | |
1170 | return; | |
1171 | } | |
1172 | ||
1173 | BeginBatchUndo(_("Insert Text")); | |
1174 | ||
5d7836c4 JS |
1175 | DeleteSelectedContent(& newPos); |
1176 | ||
ff76711f JS |
1177 | if (event.ShiftDown()) |
1178 | { | |
1179 | wxString text; | |
1180 | text = wxRichTextLineBreakChar; | |
4e63bfb9 | 1181 | GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos+1, text, this); |
4bf9e5bc | 1182 | m_caretAtLineStart = true; |
043c0d58 | 1183 | PositionCaret(); |
ff76711f JS |
1184 | } |
1185 | else | |
4e63bfb9 | 1186 | GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE|wxRICHTEXT_INSERT_INTERACTIVE); |
ff76711f | 1187 | |
4ce3ebd3 JS |
1188 | // Automatically renumber list |
1189 | bool isNumberedList = false; | |
1190 | wxRichTextRange numberedListRange = FindRangeForList(newPos+1, isNumberedList); | |
1191 | if (isNumberedList && numberedListRange != wxRichTextRange(-1, -1)) | |
1192 | { | |
1193 | NumberList(numberedListRange, NULL, wxRICHTEXT_SETSTYLE_RENUMBER|wxRICHTEXT_SETSTYLE_WITH_UNDO); | |
1194 | } | |
1195 | ||
d2d0adc7 | 1196 | EndBatchUndo(); |
0a21bea0 | 1197 | SetDefaultStyleToCursorStyle(); |
d2d0adc7 JS |
1198 | |
1199 | ScrollIntoView(m_caretPosition, WXK_RIGHT); | |
5d7836c4 JS |
1200 | |
1201 | wxRichTextEvent cmdEvent( | |
ce7fe42e | 1202 | wxEVT_RICHTEXT_RETURN, |
5d7836c4 JS |
1203 | GetId()); |
1204 | cmdEvent.SetEventObject(this); | |
1205 | cmdEvent.SetFlags(flags); | |
5912d19e | 1206 | cmdEvent.SetPosition(newPos+1); |
603f702b | 1207 | cmdEvent.SetContainer(GetFocusObject()); |
5912d19e | 1208 | |
d2d0adc7 JS |
1209 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) |
1210 | { | |
1211 | // Generate conventional event | |
ce7fe42e | 1212 | wxCommandEvent textEvent(wxEVT_TEXT_ENTER, GetId()); |
d2d0adc7 | 1213 | InitCommandEvent(textEvent); |
88bee7e6 | 1214 | |
d2d0adc7 JS |
1215 | GetEventHandler()->ProcessEvent(textEvent); |
1216 | } | |
621ae374 | 1217 | Update(); |
5d7836c4 JS |
1218 | } |
1219 | else if (event.GetKeyCode() == WXK_BACK) | |
1220 | { | |
4ce3ebd3 | 1221 | ProcessBackKey(event, flags); |
5d7836c4 JS |
1222 | } |
1223 | else if (event.GetKeyCode() == WXK_DELETE) | |
1224 | { | |
c80e8cbe JS |
1225 | long newPos = m_caretPosition; |
1226 | ||
cc2aecde JS |
1227 | if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange())) |
1228 | { | |
1229 | return; | |
1230 | } | |
1231 | ||
1232 | BeginBatchUndo(_("Delete Text")); | |
1233 | ||
dc5de0dd | 1234 | bool processed = DeleteSelectedContent(& newPos); |
c80e8cbe | 1235 | |
cc2aecde JS |
1236 | int deletions = 0; |
1237 | if (processed) | |
1238 | deletions ++; | |
1239 | ||
5d7836c4 | 1240 | // Submit range in character positions, which are greater than caret positions, |
603f702b | 1241 | if (newPos < GetFocusObject()->GetOwnRange().GetEnd()+1) |
5d7836c4 | 1242 | { |
c80e8cbe JS |
1243 | if (event.CmdDown()) |
1244 | { | |
1245 | long pos = wxRichTextCtrl::FindNextWordPosition(1); | |
1246 | if (pos != -1 && (pos > newPos)) | |
1247 | { | |
cc2aecde JS |
1248 | wxRichTextRange range(newPos+1, pos); |
1249 | if (CanDeleteRange(* GetFocusObject(), range.FromInternal())) | |
1250 | { | |
1251 | GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer()); | |
1252 | deletions ++; | |
1253 | } | |
c80e8cbe JS |
1254 | processed = true; |
1255 | } | |
1256 | } | |
1257 | ||
fc6d9d0d | 1258 | if (!processed && newPos < (GetLastPosition()-1)) |
cc2aecde JS |
1259 | { |
1260 | wxRichTextRange range(newPos+1, newPos+1); | |
1261 | if (CanDeleteRange(* GetFocusObject(), range.FromInternal())) | |
1262 | { | |
1263 | GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer()); | |
1264 | deletions ++; | |
1265 | } | |
1266 | } | |
5d7836c4 | 1267 | } |
5d7836c4 JS |
1268 | |
1269 | EndBatchUndo(); | |
1270 | ||
5d7836c4 JS |
1271 | if (GetLastPosition() == -1) |
1272 | { | |
603f702b | 1273 | GetFocusObject()->Reset(); |
5d7836c4 JS |
1274 | |
1275 | m_caretPosition = -1; | |
1276 | PositionCaret(); | |
0a21bea0 | 1277 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 1278 | } |
5912d19e | 1279 | |
575be976 JS |
1280 | ScrollIntoView(m_caretPosition, WXK_LEFT); |
1281 | ||
ce7fe42e | 1282 | // Always send this event; wxEVT_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion. |
cc2aecde JS |
1283 | { |
1284 | wxRichTextEvent cmdEvent( | |
ce7fe42e | 1285 | wxEVT_RICHTEXT_DELETE, |
cc2aecde JS |
1286 | GetId()); |
1287 | cmdEvent.SetEventObject(this); | |
1288 | cmdEvent.SetFlags(flags); | |
1289 | cmdEvent.SetPosition(m_caretPosition+1); | |
1290 | cmdEvent.SetContainer(GetFocusObject()); | |
1291 | GetEventHandler()->ProcessEvent(cmdEvent); | |
1292 | } | |
5912d19e | 1293 | |
621ae374 | 1294 | Update(); |
5d7836c4 JS |
1295 | } |
1296 | else | |
1297 | { | |
3e3a754f JS |
1298 | long keycode = event.GetKeyCode(); |
1299 | switch ( keycode ) | |
1300 | { | |
1301 | case WXK_ESCAPE: | |
3e3a754f JS |
1302 | { |
1303 | event.Skip(); | |
1304 | return; | |
1305 | } | |
1807a1f3 | 1306 | |
3e3a754f JS |
1307 | default: |
1308 | { | |
cfceb4a8 | 1309 | #ifdef __WXMAC__ |
91babb9d | 1310 | if (event.CmdDown()) |
cfceb4a8 | 1311 | #else |
d2bb72ff JS |
1312 | // Fixes AltGr+key with European input languages on Windows |
1313 | if ((event.CmdDown() && !event.AltDown()) || (event.AltDown() && !event.CmdDown())) | |
cfceb4a8 | 1314 | #endif |
3e3a754f JS |
1315 | { |
1316 | event.Skip(); | |
1317 | return; | |
1318 | } | |
41a85215 | 1319 | |
5912d19e | 1320 | wxRichTextEvent cmdEvent( |
ce7fe42e | 1321 | wxEVT_RICHTEXT_CHARACTER, |
5912d19e JS |
1322 | GetId()); |
1323 | cmdEvent.SetEventObject(this); | |
1324 | cmdEvent.SetFlags(flags); | |
0b14a7bd JS |
1325 | #if wxUSE_UNICODE |
1326 | cmdEvent.SetCharacter(event.GetUnicodeKey()); | |
1327 | #else | |
5912d19e | 1328 | cmdEvent.SetCharacter((wxChar) keycode); |
0b14a7bd | 1329 | #endif |
5912d19e | 1330 | cmdEvent.SetPosition(m_caretPosition+1); |
603f702b | 1331 | cmdEvent.SetContainer(GetFocusObject()); |
88bee7e6 | 1332 | |
dadd4f55 JS |
1333 | if (keycode == wxT('\t')) |
1334 | { | |
1335 | // See if we need to promote or demote the selection or paragraph at the cursor | |
1336 | // position, instead of inserting a tab. | |
1337 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
603f702b | 1338 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos); |
dadd4f55 JS |
1339 | if (para && para->GetRange().GetStart() == pos && para->GetAttributes().HasListStyleName()) |
1340 | { | |
1341 | wxRichTextRange range; | |
1342 | if (HasSelection()) | |
1343 | range = GetSelectionRange(); | |
1344 | else | |
1345 | range = para->GetRange().FromInternal(); | |
41a85215 | 1346 | |
dadd4f55 JS |
1347 | int promoteBy = event.ShiftDown() ? 1 : -1; |
1348 | ||
1349 | PromoteList(promoteBy, range, NULL); | |
d2d0adc7 | 1350 | |
5912d19e JS |
1351 | GetEventHandler()->ProcessEvent(cmdEvent); |
1352 | ||
dadd4f55 JS |
1353 | return; |
1354 | } | |
1355 | } | |
5d7836c4 | 1356 | |
cc2aecde JS |
1357 | if (!CanInsertContent(* GetFocusObject(), m_caretPosition+1)) |
1358 | return; | |
1359 | ||
1360 | if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange())) | |
1361 | return; | |
1362 | ||
3e3a754f | 1363 | BeginBatchUndo(_("Insert Text")); |
5d7836c4 | 1364 | |
3e3a754f JS |
1365 | long newPos = m_caretPosition; |
1366 | DeleteSelectedContent(& newPos); | |
5d7836c4 | 1367 | |
0b14a7bd JS |
1368 | #if wxUSE_UNICODE |
1369 | wxString str = event.GetUnicodeKey(); | |
1370 | #else | |
3e3a754f | 1371 | wxString str = (wxChar) event.GetKeyCode(); |
0b14a7bd | 1372 | #endif |
4e63bfb9 | 1373 | GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos+1, str, this, 0); |
5d7836c4 | 1374 | |
3e3a754f JS |
1375 | EndBatchUndo(); |
1376 | ||
0a21bea0 | 1377 | SetDefaultStyleToCursorStyle(); |
3e3a754f | 1378 | ScrollIntoView(m_caretPosition, WXK_RIGHT); |
88bee7e6 | 1379 | |
c1dd924d | 1380 | cmdEvent.SetPosition(m_caretPosition); |
5912d19e JS |
1381 | GetEventHandler()->ProcessEvent(cmdEvent); |
1382 | ||
621ae374 | 1383 | Update(); |
3e3a754f JS |
1384 | } |
1385 | } | |
5d7836c4 | 1386 | } |
5d7836c4 JS |
1387 | } |
1388 | ||
8db2e3ef | 1389 | bool wxRichTextCtrl::ProcessMouseMovement(wxRichTextParagraphLayoutBox* container, wxRichTextObject* WXUNUSED(obj), long position, const wxPoint& WXUNUSED(pos)) |
590a0f8b JS |
1390 | { |
1391 | wxRichTextAttr attr; | |
1392 | if (container && GetStyle(position, attr, container)) | |
1393 | { | |
1394 | if (attr.HasFlag(wxTEXT_ATTR_URL)) | |
1395 | { | |
1396 | SetCursor(m_urlCursor); | |
1397 | } | |
1398 | else if (!attr.HasFlag(wxTEXT_ATTR_URL)) | |
1399 | { | |
1400 | SetCursor(m_textCursor); | |
1401 | } | |
1402 | return true; | |
1403 | } | |
1404 | else | |
1405 | return false; | |
1406 | } | |
1407 | ||
4ce3ebd3 JS |
1408 | // Processes the back key |
1409 | bool wxRichTextCtrl::ProcessBackKey(wxKeyEvent& event, int flags) | |
1410 | { | |
1411 | if (!IsEditable()) | |
1412 | { | |
1413 | return false; | |
1414 | } | |
1415 | ||
1416 | if (HasSelection() && !CanDeleteRange(* GetFocusObject(), GetSelectionRange())) | |
1417 | { | |
1418 | return false; | |
1419 | } | |
1420 | ||
1421 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(m_caretPosition, true); | |
1422 | ||
1423 | // If we're at the start of a list item with a bullet, let's 'delete' the bullet, i.e. | |
1424 | // make it a continuation paragraph. | |
1425 | if (!HasSelection() && para && ((m_caretPosition+1) == para->GetRange().GetStart()) && | |
1426 | para->GetAttributes().HasBulletStyle() && (para->GetAttributes().GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_CONTINUATION) == 0) | |
1427 | { | |
1428 | wxRichTextParagraph* newPara = wxDynamicCast(para->Clone(), wxRichTextParagraph); | |
1429 | newPara->GetAttributes().SetBulletStyle(newPara->GetAttributes().GetBulletStyle() | wxTEXT_ATTR_BULLET_STYLE_CONTINUATION); | |
1430 | ||
1431 | wxRichTextAction* action = new wxRichTextAction(NULL, _("Remove Bullet"), wxRICHTEXT_CHANGE_STYLE, & GetBuffer(), GetFocusObject(), this); | |
1432 | action->SetRange(newPara->GetRange()); | |
1433 | action->SetPosition(GetCaretPosition()); | |
1434 | action->GetNewParagraphs().AppendChild(newPara); | |
1435 | // Also store the old ones for Undo | |
1436 | action->GetOldParagraphs().AppendChild(new wxRichTextParagraph(*para)); | |
1437 | ||
1438 | GetBuffer().Invalidate(para->GetRange()); | |
1439 | GetBuffer().SubmitAction(action); | |
1440 | ||
1441 | // Automatically renumber list | |
1442 | bool isNumberedList = false; | |
1443 | wxRichTextRange numberedListRange = FindRangeForList(m_caretPosition, isNumberedList); | |
1444 | if (isNumberedList && numberedListRange != wxRichTextRange(-1, -1)) | |
1445 | { | |
1446 | NumberList(numberedListRange, NULL, wxRICHTEXT_SETSTYLE_RENUMBER|wxRICHTEXT_SETSTYLE_WITH_UNDO); | |
1447 | } | |
1448 | ||
1449 | Update(); | |
1450 | } | |
1451 | else | |
1452 | { | |
1453 | BeginBatchUndo(_("Delete Text")); | |
1454 | ||
1455 | long newPos = m_caretPosition; | |
1456 | ||
1457 | bool processed = DeleteSelectedContent(& newPos); | |
1458 | ||
1459 | int deletions = 0; | |
1460 | if (processed) | |
1461 | deletions ++; | |
1462 | ||
1463 | // Submit range in character positions, which are greater than caret positions, | |
1464 | // so subtract 1 for deleted character and add 1 for conversion to character position. | |
1465 | if (newPos > -1) | |
1466 | { | |
1467 | if (event.CmdDown()) | |
1468 | { | |
1469 | long pos = wxRichTextCtrl::FindNextWordPosition(-1); | |
1470 | if (pos < newPos) | |
1471 | { | |
1472 | wxRichTextRange range(pos+1, newPos); | |
1473 | if (CanDeleteRange(* GetFocusObject(), range.FromInternal())) | |
1474 | { | |
1475 | GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer()); | |
1476 | deletions ++; | |
1477 | } | |
1478 | processed = true; | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | if (!processed) | |
1483 | { | |
1484 | wxRichTextRange range(newPos, newPos); | |
1485 | if (CanDeleteRange(* GetFocusObject(), range.FromInternal())) | |
1486 | { | |
1487 | GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer()); | |
1488 | deletions ++; | |
1489 | } | |
1490 | } | |
1491 | } | |
1492 | ||
1493 | EndBatchUndo(); | |
1494 | ||
1495 | if (GetLastPosition() == -1) | |
1496 | { | |
1497 | GetFocusObject()->Reset(); | |
1498 | ||
1499 | m_caretPosition = -1; | |
1500 | PositionCaret(); | |
1501 | SetDefaultStyleToCursorStyle(); | |
1502 | } | |
1503 | ||
1504 | ScrollIntoView(m_caretPosition, WXK_LEFT); | |
1505 | ||
ce7fe42e | 1506 | // Always send this event; wxEVT_RICHTEXT_CONTENT_DELETED will be sent only if there is an actual deletion. |
4ce3ebd3 JS |
1507 | { |
1508 | wxRichTextEvent cmdEvent( | |
ce7fe42e | 1509 | wxEVT_RICHTEXT_DELETE, |
4ce3ebd3 JS |
1510 | GetId()); |
1511 | cmdEvent.SetEventObject(this); | |
1512 | cmdEvent.SetFlags(flags); | |
1513 | cmdEvent.SetPosition(m_caretPosition+1); | |
1514 | cmdEvent.SetContainer(GetFocusObject()); | |
1515 | GetEventHandler()->ProcessEvent(cmdEvent); | |
1516 | } | |
1517 | ||
1518 | Update(); | |
1519 | } | |
1520 | ||
1521 | return true; | |
1522 | } | |
1523 | ||
5d7836c4 JS |
1524 | /// Delete content if there is a selection, e.g. when pressing a key. |
1525 | bool wxRichTextCtrl::DeleteSelectedContent(long* newPos) | |
1526 | { | |
1527 | if (HasSelection()) | |
1528 | { | |
603f702b JS |
1529 | long pos = m_selection.GetRange().GetStart(); |
1530 | wxRichTextRange range = m_selection.GetRange(); | |
205a1ad9 JS |
1531 | |
1532 | // SelectAll causes more to be selected than doing it interactively, | |
1533 | // and causes a new paragraph to be inserted. So for multiline buffers, | |
1534 | // don't delete the final position. | |
1535 | if (range.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0) | |
1536 | range.SetEnd(range.GetEnd()-1); | |
1537 | ||
603f702b JS |
1538 | GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer()); |
1539 | m_selection.Reset(); | |
1540 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
5d7836c4 JS |
1541 | |
1542 | if (newPos) | |
1543 | *newPos = pos-1; | |
1544 | return true; | |
1545 | } | |
1546 | else | |
1547 | return false; | |
1548 | } | |
1549 | ||
1550 | /// Keyboard navigation | |
1551 | ||
1552 | /* | |
1553 | ||
1554 | Left: left one character | |
1555 | Right: right one character | |
1556 | Up: up one line | |
1557 | Down: down one line | |
1558 | Ctrl-Left: left one word | |
1559 | Ctrl-Right: right one word | |
1560 | Ctrl-Up: previous paragraph start | |
1561 | Ctrl-Down: next start of paragraph | |
1562 | Home: start of line | |
1563 | End: end of line | |
1564 | Ctrl-Home: start of document | |
1565 | Ctrl-End: end of document | |
1566 | Page-Up: Up a screen | |
1567 | Page-Down: Down a screen | |
1568 | ||
1569 | Maybe: | |
1570 | ||
1571 | Ctrl-Alt-PgUp: Start of window | |
1572 | Ctrl-Alt-PgDn: End of window | |
1573 | F8: Start selection mode | |
1574 | Esc: End selection mode | |
1575 | ||
1576 | Adding Shift does the above but starts/extends selection. | |
1577 | ||
1578 | ||
1579 | */ | |
1580 | ||
0bab774b | 1581 | bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags) |
5d7836c4 JS |
1582 | { |
1583 | bool success = false; | |
5d7836c4 | 1584 | |
3e3a754f | 1585 | if (keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT) |
5d7836c4 JS |
1586 | { |
1587 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1588 | success = WordRight(1, flags); | |
1589 | else | |
1590 | success = MoveRight(1, flags); | |
1591 | } | |
3e3a754f | 1592 | else if (keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT) |
5d7836c4 JS |
1593 | { |
1594 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1595 | success = WordLeft(1, flags); | |
1596 | else | |
1597 | success = MoveLeft(1, flags); | |
1598 | } | |
3e3a754f | 1599 | else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP) |
5d7836c4 JS |
1600 | { |
1601 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1602 | success = MoveToParagraphStart(flags); | |
1603 | else | |
1604 | success = MoveUp(1, flags); | |
1605 | } | |
3e3a754f | 1606 | else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN) |
5d7836c4 JS |
1607 | { |
1608 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1609 | success = MoveToParagraphEnd(flags); | |
1610 | else | |
1611 | success = MoveDown(1, flags); | |
1612 | } | |
3e3a754f | 1613 | else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP) |
5d7836c4 JS |
1614 | { |
1615 | success = PageUp(1, flags); | |
1616 | } | |
3e3a754f | 1617 | else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN) |
5d7836c4 JS |
1618 | { |
1619 | success = PageDown(1, flags); | |
1620 | } | |
3e3a754f | 1621 | else if (keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME) |
5d7836c4 JS |
1622 | { |
1623 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1624 | success = MoveHome(flags); | |
1625 | else | |
1626 | success = MoveToLineStart(flags); | |
1627 | } | |
3e3a754f | 1628 | else if (keyCode == WXK_END || keyCode == WXK_NUMPAD_END) |
5d7836c4 JS |
1629 | { |
1630 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1631 | success = MoveEnd(flags); | |
1632 | else | |
1633 | success = MoveToLineEnd(flags); | |
1634 | } | |
1635 | ||
1636 | if (success) | |
1637 | { | |
1638 | ScrollIntoView(m_caretPosition, keyCode); | |
0a21bea0 | 1639 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1640 | } |
1641 | ||
5d7836c4 JS |
1642 | return success; |
1643 | } | |
1644 | ||
1645 | /// Extend the selection. Selections are in caret positions. | |
1646 | bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags) | |
1647 | { | |
1648 | if (flags & wxRICHTEXT_SHIFT_DOWN) | |
1649 | { | |
e2d0875a JS |
1650 | if (oldPos == newPos) |
1651 | return false; | |
1652 | ||
603f702b | 1653 | wxRichTextSelection oldSelection = m_selection; |
4fe83b93 | 1654 | |
603f702b | 1655 | m_selection.SetContainer(GetFocusObject()); |
4fe83b93 | 1656 | |
603f702b JS |
1657 | wxRichTextRange oldRange; |
1658 | if (m_selection.IsValid()) | |
1659 | oldRange = m_selection.GetRange(); | |
1660 | else | |
1661 | oldRange = wxRICHTEXT_NO_SELECTION; | |
1662 | wxRichTextRange newRange; | |
7051fa41 | 1663 | |
5d7836c4 | 1664 | // If not currently selecting, start selecting |
603f702b | 1665 | if (oldRange.GetStart() == -2) |
5d7836c4 JS |
1666 | { |
1667 | m_selectionAnchor = oldPos; | |
1668 | ||
1669 | if (oldPos > newPos) | |
603f702b | 1670 | newRange.SetRange(newPos+1, oldPos); |
5d7836c4 | 1671 | else |
603f702b | 1672 | newRange.SetRange(oldPos+1, newPos); |
5d7836c4 JS |
1673 | } |
1674 | else | |
1675 | { | |
1676 | // Always ensure that the selection range start is greater than | |
1677 | // the end. | |
1678 | if (newPos > m_selectionAnchor) | |
603f702b | 1679 | newRange.SetRange(m_selectionAnchor+1, newPos); |
e2d0875a | 1680 | else if (newPos == m_selectionAnchor) |
603f702b | 1681 | newRange = wxRichTextRange(-2, -2); |
5d7836c4 | 1682 | else |
603f702b | 1683 | newRange.SetRange(newPos+1, m_selectionAnchor); |
5d7836c4 | 1684 | } |
4fe83b93 | 1685 | |
603f702b | 1686 | m_selection.SetRange(newRange); |
5d7836c4 | 1687 | |
603f702b | 1688 | RefreshForSelectionChange(oldSelection, m_selection); |
7051fa41 | 1689 | |
603f702b | 1690 | if (newRange.GetStart() > newRange.GetEnd()) |
5d7836c4 JS |
1691 | { |
1692 | wxLogDebug(wxT("Strange selection range")); | |
1693 | } | |
7fe8059f | 1694 | |
5d7836c4 JS |
1695 | return true; |
1696 | } | |
1697 | else | |
1698 | return false; | |
1699 | } | |
1700 | ||
1701 | /// Scroll into view, returning true if we scrolled. | |
1702 | /// This takes a _caret_ position. | |
1703 | bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) | |
1704 | { | |
1705 | wxRichTextLine* line = GetVisibleLineForCaretPosition(position); | |
1706 | ||
1707 | if (!line) | |
1708 | return false; | |
1709 | ||
1710 | int ppuX, ppuY; | |
1711 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
1712 | ||
3956d02a JS |
1713 | int startXUnits, startYUnits; |
1714 | GetViewStart(& startXUnits, & startYUnits); | |
1715 | int startY = startYUnits * ppuY; | |
5d7836c4 | 1716 | |
8d7eaf91 | 1717 | int sx = 0, sy = 0; |
5d7836c4 | 1718 | GetVirtualSize(& sx, & sy); |
3956d02a JS |
1719 | int sxUnits = 0; |
1720 | int syUnits = 0; | |
5d7836c4 | 1721 | if (ppuY != 0) |
3956d02a | 1722 | syUnits = sy/ppuY; |
5d7836c4 | 1723 | |
4ba36292 | 1724 | wxRect rect = GetScaledRect(line->GetRect()); |
5d7836c4 JS |
1725 | |
1726 | bool scrolled = false; | |
1727 | ||
1728 | wxSize clientSize = GetClientSize(); | |
4fe83b93 | 1729 | |
603f702b JS |
1730 | int leftMargin, rightMargin, topMargin, bottomMargin; |
1731 | ||
1732 | { | |
1733 | wxClientDC dc(this); | |
1734 | wxRichTextObject::GetTotalMargin(dc, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin, rightMargin, | |
1735 | topMargin, bottomMargin); | |
1736 | } | |
4ba36292 | 1737 | clientSize.y -= (int) (0.5 + bottomMargin * GetScale()); |
e9f10004 JS |
1738 | |
1739 | if (GetWindowStyle() & wxRE_CENTRE_CARET) | |
1740 | { | |
1741 | int y = rect.y - GetClientSize().y/2; | |
1742 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); | |
4ba36292 | 1743 | if (y >= 0 && (y + clientSize.y) < (int) (0.5 + GetBuffer().GetCachedSize().y * GetScale())) |
e9f10004 JS |
1744 | { |
1745 | if (startYUnits != yUnits) | |
1746 | { | |
1747 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); | |
1748 | scrolled = true; | |
1749 | } | |
1750 | #if !wxRICHTEXT_USE_OWN_CARET | |
1751 | if (scrolled) | |
1752 | #endif | |
1753 | PositionCaret(); | |
1754 | ||
1755 | return scrolled; | |
1756 | } | |
1757 | } | |
5d7836c4 JS |
1758 | |
1759 | // Going down | |
3e3a754f | 1760 | if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN || |
ec36b155 | 1761 | keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT || |
3e3a754f JS |
1762 | keyCode == WXK_END || keyCode == WXK_NUMPAD_END || |
1763 | keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN) | |
5d7836c4 JS |
1764 | { |
1765 | if ((rect.y + rect.height) > (clientSize.y + startY)) | |
1766 | { | |
1767 | // Make it scroll so this item is at the bottom | |
1768 | // of the window | |
1769 | int y = rect.y - (clientSize.y - rect.height); | |
3956d02a | 1770 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); |
c59f6793 | 1771 | |
3956d02a JS |
1772 | // If we're still off the screen, scroll another line down |
1773 | if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY))) | |
1774 | yUnits ++; | |
1775 | ||
1776 | if (startYUnits != yUnits) | |
c59f6793 | 1777 | { |
3956d02a | 1778 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); |
c59f6793 JS |
1779 | scrolled = true; |
1780 | } | |
5d7836c4 | 1781 | } |
4ba36292 | 1782 | else if (rect.y < (startY + (int) (0.5 + GetBuffer().GetTopMargin() * GetScale()))) |
5d7836c4 JS |
1783 | { |
1784 | // Make it scroll so this item is at the top | |
1785 | // of the window | |
4ba36292 | 1786 | int y = rect.y - (int) (0.5 + GetBuffer().GetTopMargin() * GetScale()); |
3956d02a | 1787 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); |
c59f6793 | 1788 | |
3956d02a | 1789 | if (startYUnits != yUnits) |
c59f6793 | 1790 | { |
3956d02a | 1791 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); |
c59f6793 JS |
1792 | scrolled = true; |
1793 | } | |
5d7836c4 | 1794 | } |
5d7836c4 JS |
1795 | } |
1796 | // Going up | |
3e3a754f JS |
1797 | else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP || |
1798 | keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT || | |
1799 | keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME || | |
1800 | keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP ) | |
5d7836c4 | 1801 | { |
4ba36292 | 1802 | if (rect.y < (startY + (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale()))) |
5d7836c4 JS |
1803 | { |
1804 | // Make it scroll so this item is at the top | |
1805 | // of the window | |
4ba36292 | 1806 | int y = rect.y - (int) (0.5 + GetBuffer().GetTopMargin() * GetScale()); |
3956d02a | 1807 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); |
c59f6793 | 1808 | |
3956d02a | 1809 | if (startYUnits != yUnits) |
c59f6793 | 1810 | { |
3956d02a | 1811 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); |
c59f6793 JS |
1812 | scrolled = true; |
1813 | } | |
5d7836c4 JS |
1814 | } |
1815 | else if ((rect.y + rect.height) > (clientSize.y + startY)) | |
1816 | { | |
1817 | // Make it scroll so this item is at the bottom | |
1818 | // of the window | |
1819 | int y = rect.y - (clientSize.y - rect.height); | |
3956d02a JS |
1820 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); |
1821 | ||
1822 | // If we're still off the screen, scroll another line down | |
1823 | if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY))) | |
1824 | yUnits ++; | |
c59f6793 | 1825 | |
3956d02a | 1826 | if (startYUnits != yUnits) |
c59f6793 | 1827 | { |
3956d02a | 1828 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); |
c59f6793 JS |
1829 | scrolled = true; |
1830 | } | |
5d7836c4 | 1831 | } |
5d7836c4 | 1832 | } |
109bfc88 | 1833 | |
1c13f06e | 1834 | #if !wxRICHTEXT_USE_OWN_CARET |
109bfc88 | 1835 | if (scrolled) |
1c13f06e | 1836 | #endif |
109bfc88 | 1837 | PositionCaret(); |
5d7836c4 JS |
1838 | |
1839 | return scrolled; | |
1840 | } | |
1841 | ||
1842 | /// Is the given position visible on the screen? | |
1843 | bool wxRichTextCtrl::IsPositionVisible(long pos) const | |
1844 | { | |
1845 | wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1); | |
1846 | ||
1847 | if (!line) | |
1848 | return false; | |
1849 | ||
1850 | int ppuX, ppuY; | |
1851 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
1852 | ||
1853 | int startX, startY; | |
1854 | GetViewStart(& startX, & startY); | |
1855 | startX = 0; | |
1856 | startY = startY * ppuY; | |
1857 | ||
4ba36292 | 1858 | wxRect rect = GetScaledRect(line->GetRect()); |
5d7836c4 | 1859 | wxSize clientSize = GetClientSize(); |
4ba36292 | 1860 | clientSize.y -= (int) (0.5 + GetBuffer().GetBottomMargin() * GetScale()); |
5d7836c4 | 1861 | |
4ba36292 JS |
1862 | return (rect.GetTop() >= (startY + (int) (0.5 + GetBuffer().GetTopMargin() * GetScale()))) && |
1863 | (rect.GetBottom() <= (startY + clientSize.y)); | |
5d7836c4 JS |
1864 | } |
1865 | ||
1866 | void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart) | |
1867 | { | |
1868 | m_caretPosition = position; | |
1869 | m_caretAtLineStart = showAtLineStart; | |
1870 | } | |
1871 | ||
1872 | /// Move caret one visual step forward: this may mean setting a flag | |
1873 | /// and keeping the same position if we're going from the end of one line | |
1874 | /// to the start of the next, which may be the exact same caret position. | |
1875 | void wxRichTextCtrl::MoveCaretForward(long oldPosition) | |
1876 | { | |
603f702b | 1877 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(oldPosition); |
5d7836c4 JS |
1878 | |
1879 | // Only do the check if we're not at the end of the paragraph (where things work OK | |
1880 | // anyway) | |
1881 | if (para && (oldPosition != para->GetRange().GetEnd() - 1)) | |
1882 | { | |
603f702b | 1883 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(oldPosition); |
5d7836c4 JS |
1884 | |
1885 | if (line) | |
1886 | { | |
1e967276 JS |
1887 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
1888 | ||
5d7836c4 JS |
1889 | // We're at the end of a line. See whether we need to |
1890 | // stay at the same actual caret position but change visual | |
1891 | // position, or not. | |
1e967276 | 1892 | if (oldPosition == lineRange.GetEnd()) |
5d7836c4 JS |
1893 | { |
1894 | if (m_caretAtLineStart) | |
1895 | { | |
1896 | // We're already at the start of the line, so actually move on now. | |
1897 | m_caretPosition = oldPosition + 1; | |
1898 | m_caretAtLineStart = false; | |
1899 | } | |
1900 | else | |
1901 | { | |
1902 | // We're showing at the end of the line, so keep to | |
1903 | // the same position but indicate that we're to show | |
1904 | // at the start of the next line. | |
1905 | m_caretPosition = oldPosition; | |
7fe8059f | 1906 | m_caretAtLineStart = true; |
5d7836c4 | 1907 | } |
0a21bea0 | 1908 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1909 | return; |
1910 | } | |
1911 | } | |
1912 | } | |
1913 | m_caretPosition ++; | |
0a21bea0 | 1914 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1915 | } |
1916 | ||
1917 | /// Move caret one visual step backward: this may mean setting a flag | |
1918 | /// and keeping the same position if we're going from the end of one line | |
1919 | /// to the start of the next, which may be the exact same caret position. | |
1920 | void wxRichTextCtrl::MoveCaretBack(long oldPosition) | |
1921 | { | |
603f702b | 1922 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(oldPosition); |
5d7836c4 JS |
1923 | |
1924 | // Only do the check if we're not at the start of the paragraph (where things work OK | |
1925 | // anyway) | |
1926 | if (para && (oldPosition != para->GetRange().GetStart())) | |
1927 | { | |
603f702b | 1928 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(oldPosition); |
5d7836c4 JS |
1929 | |
1930 | if (line) | |
1931 | { | |
1e967276 JS |
1932 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
1933 | ||
5d7836c4 JS |
1934 | // We're at the start of a line. See whether we need to |
1935 | // stay at the same actual caret position but change visual | |
1936 | // position, or not. | |
1e967276 | 1937 | if (oldPosition == lineRange.GetStart()) |
5d7836c4 JS |
1938 | { |
1939 | m_caretPosition = oldPosition-1; | |
1940 | m_caretAtLineStart = true; | |
1941 | return; | |
1942 | } | |
1e967276 | 1943 | else if (oldPosition == lineRange.GetEnd()) |
5d7836c4 JS |
1944 | { |
1945 | if (m_caretAtLineStart) | |
1946 | { | |
1947 | // We're at the start of the line, so keep the same caret position | |
1948 | // but clear the start-of-line flag. | |
1949 | m_caretPosition = oldPosition; | |
1950 | m_caretAtLineStart = false; | |
1951 | } | |
1952 | else | |
1953 | { | |
1954 | // We're showing at the end of the line, so go back | |
1955 | // to the previous character position. | |
1956 | m_caretPosition = oldPosition - 1; | |
1957 | } | |
0a21bea0 | 1958 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1959 | return; |
1960 | } | |
1961 | } | |
1962 | } | |
1963 | m_caretPosition --; | |
0a21bea0 | 1964 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1965 | } |
1966 | ||
1967 | /// Move right | |
1968 | bool wxRichTextCtrl::MoveRight(int noPositions, int flags) | |
1969 | { | |
603f702b | 1970 | long endPos = GetFocusObject()->GetOwnRange().GetEnd(); |
5d7836c4 JS |
1971 | |
1972 | if (m_caretPosition + noPositions < endPos) | |
1973 | { | |
1974 | long oldPos = m_caretPosition; | |
1975 | long newPos = m_caretPosition + noPositions; | |
1976 | ||
1977 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
1978 | if (!extendSel) | |
1979 | SelectNone(); | |
1980 | ||
1981 | // Determine by looking at oldPos and m_caretPosition whether | |
1982 | // we moved from the end of a line to the start of the next line, in which case | |
1983 | // we want to adjust the caret position such that it is positioned at the | |
1984 | // start of the next line, rather than jumping past the first character of the | |
1985 | // line. | |
75e50f45 | 1986 | if (noPositions == 1) |
5d7836c4 JS |
1987 | MoveCaretForward(oldPos); |
1988 | else | |
1989 | SetCaretPosition(newPos); | |
1990 | ||
1991 | PositionCaret(); | |
0a21bea0 | 1992 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 1993 | |
5d7836c4 JS |
1994 | return true; |
1995 | } | |
1996 | else | |
7fe8059f | 1997 | return false; |
5d7836c4 JS |
1998 | } |
1999 | ||
2000 | /// Move left | |
2001 | bool wxRichTextCtrl::MoveLeft(int noPositions, int flags) | |
2002 | { | |
2003 | long startPos = -1; | |
2004 | ||
2005 | if (m_caretPosition > startPos - noPositions + 1) | |
2006 | { | |
2007 | long oldPos = m_caretPosition; | |
2008 | long newPos = m_caretPosition - noPositions; | |
2009 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
2010 | if (!extendSel) | |
2011 | SelectNone(); | |
2012 | ||
75e50f45 | 2013 | if (noPositions == 1) |
5d7836c4 JS |
2014 | MoveCaretBack(oldPos); |
2015 | else | |
2016 | SetCaretPosition(newPos); | |
2017 | ||
2018 | PositionCaret(); | |
0a21bea0 | 2019 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2020 | |
5d7836c4 JS |
2021 | return true; |
2022 | } | |
2023 | else | |
7fe8059f | 2024 | return false; |
5d7836c4 JS |
2025 | } |
2026 | ||
603f702b JS |
2027 | // Find the caret position for the combination of hit-test flags and character position. |
2028 | // Returns the caret position and also an indication of where to place the caret (caretLineStart) | |
2029 | // since this is ambiguous (same position used for end of line and start of next). | |
2030 | long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position, int hitTestFlags, wxRichTextParagraphLayoutBox* container, | |
2031 | bool& caretLineStart) | |
2032 | { | |
2033 | // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE, | |
2034 | // we want to be at the end of the last line but with m_caretAtLineStart set to true, | |
2035 | // so we view the caret at the start of the line. | |
2036 | caretLineStart = false; | |
2037 | long caretPosition = position; | |
2038 | ||
2039 | if (hitTestFlags & wxRICHTEXT_HITTEST_BEFORE) | |
2040 | { | |
2041 | wxRichTextLine* thisLine = container->GetLineAtPosition(position-1); | |
2042 | wxRichTextRange lineRange; | |
2043 | if (thisLine) | |
2044 | lineRange = thisLine->GetAbsoluteRange(); | |
2045 | ||
2046 | if (thisLine && (position-1) == lineRange.GetEnd()) | |
2047 | { | |
2048 | caretPosition --; | |
2049 | caretLineStart = true; | |
2050 | } | |
2051 | else | |
2052 | { | |
2053 | wxRichTextParagraph* para = container->GetParagraphAtPosition(position); | |
2054 | if (para && para->GetRange().GetStart() == position) | |
2055 | caretPosition --; | |
2056 | } | |
2057 | } | |
2058 | return caretPosition; | |
2059 | } | |
2060 | ||
5d7836c4 JS |
2061 | /// Move up |
2062 | bool wxRichTextCtrl::MoveUp(int noLines, int flags) | |
2063 | { | |
2064 | return MoveDown(- noLines, flags); | |
2065 | } | |
2066 | ||
2067 | /// Move up | |
2068 | bool wxRichTextCtrl::MoveDown(int noLines, int flags) | |
2069 | { | |
c59f6793 JS |
2070 | if (!GetCaret()) |
2071 | return false; | |
2072 | ||
603f702b | 2073 | long lineNumber = GetFocusObject()->GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart); |
5d7836c4 | 2074 | wxPoint pt = GetCaret()->GetPosition(); |
4fe83b93 | 2075 | long newLine = lineNumber + noLines; |
603f702b | 2076 | bool notInThisObject = false; |
5d7836c4 JS |
2077 | |
2078 | if (lineNumber != -1) | |
2079 | { | |
2080 | if (noLines > 0) | |
2081 | { | |
603f702b | 2082 | long lastLine = GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd()); |
5d7836c4 | 2083 | if (newLine > lastLine) |
603f702b | 2084 | notInThisObject = true; |
5d7836c4 JS |
2085 | } |
2086 | else | |
2087 | { | |
2088 | if (newLine < 0) | |
603f702b | 2089 | notInThisObject = true; |
5d7836c4 JS |
2090 | } |
2091 | } | |
4fe83b93 | 2092 | |
603f702b | 2093 | wxRichTextParagraphLayoutBox* container = GetFocusObject(); |
7c9fdebe | 2094 | int hitTestFlags = wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS|wxRICHTEXT_HITTEST_HONOUR_ATOMIC; |
5d7836c4 | 2095 | |
76e36b94 | 2096 | bool lineIsEmpty = false; |
603f702b | 2097 | if (notInThisObject) |
7fe8059f | 2098 | { |
603f702b JS |
2099 | // If we know we're navigating out of the current object, |
2100 | // try to find an object anywhere in the buffer at the new position (up or down a bit) | |
2101 | container = & GetBuffer(); | |
343ef639 | 2102 | hitTestFlags &= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS; |
4fe83b93 | 2103 | |
603f702b JS |
2104 | if (noLines > 0) // going down |
2105 | { | |
2106 | pt.y = GetFocusObject()->GetPosition().y + GetFocusObject()->GetCachedSize().y + 2; | |
2107 | } | |
2108 | else // going up | |
2109 | { | |
2110 | pt.y = GetFocusObject()->GetPosition().y - 2; | |
2111 | } | |
5d7836c4 JS |
2112 | } |
2113 | else | |
603f702b JS |
2114 | { |
2115 | wxRichTextLine* lineObj = GetFocusObject()->GetLineForVisibleLineNumber(newLine); | |
2116 | if (lineObj) | |
76e36b94 | 2117 | { |
603f702b | 2118 | pt.y = lineObj->GetAbsolutePosition().y + 2; |
76e36b94 JS |
2119 | if (lineObj->GetRange().GetStart() == lineObj->GetRange().GetEnd()) |
2120 | lineIsEmpty = true; | |
2121 | } | |
603f702b JS |
2122 | else |
2123 | return false; | |
2124 | } | |
5d7836c4 JS |
2125 | |
2126 | long newPos = 0; | |
2127 | wxClientDC dc(this); | |
2128 | PrepareDC(dc); | |
2129 | dc.SetFont(GetFont()); | |
7fe8059f | 2130 | |
603f702b JS |
2131 | wxRichTextObject* hitObj = NULL; |
2132 | wxRichTextObject* contextObj = NULL; | |
8db2e3ef JS |
2133 | wxRichTextDrawingContext context(& GetBuffer()); |
2134 | int hitTest = container->HitTest(dc, context, pt, newPos, & hitObj, & contextObj, hitTestFlags); | |
7fe8059f | 2135 | |
343ef639 JS |
2136 | if (hitObj && |
2137 | ((hitTest & wxRICHTEXT_HITTEST_NONE) == 0) && | |
2138 | (! (hitObj == (& m_buffer) && ((hitTest & wxRICHTEXT_HITTEST_OUTSIDE) != 0))) // outside the buffer counts as 'do nothing' | |
2139 | ) | |
5d7836c4 | 2140 | { |
603f702b | 2141 | if (notInThisObject) |
5d7836c4 | 2142 | { |
603f702b JS |
2143 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); |
2144 | if (actualContainer && actualContainer != GetFocusObject() && actualContainer->AcceptsFocus()) | |
5d7836c4 | 2145 | { |
603f702b | 2146 | SetFocusObject(actualContainer, false /* don't set caret position yet */); |
4fe83b93 | 2147 | |
603f702b | 2148 | container = actualContainer; |
5d7836c4 JS |
2149 | } |
2150 | } | |
4fe83b93 | 2151 | |
603f702b | 2152 | bool caretLineStart = true; |
76e36b94 JS |
2153 | |
2154 | // If the line is empty, there is only one possible position for the caret, | |
2155 | // so force the 'before' state so FindCaretPositionForCharacterPosition doesn't | |
2156 | // just return the same position. | |
2157 | if (lineIsEmpty) | |
2158 | { | |
2159 | hitTest &= ~wxRICHTEXT_HITTEST_AFTER; | |
2160 | hitTest |= wxRICHTEXT_HITTEST_BEFORE; | |
2161 | } | |
603f702b JS |
2162 | long caretPosition = FindCaretPositionForCharacterPosition(newPos, hitTest, container, caretLineStart); |
2163 | long newSelEnd = caretPosition; | |
2164 | bool extendSel; | |
2165 | ||
2166 | if (notInThisObject) | |
2167 | extendSel = false; | |
2168 | else | |
2169 | extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags); | |
5d7836c4 | 2170 | |
c59f6793 | 2171 | if (!extendSel) |
5d7836c4 JS |
2172 | SelectNone(); |
2173 | ||
603f702b | 2174 | SetCaretPosition(caretPosition, caretLineStart); |
5d7836c4 | 2175 | PositionCaret(); |
0a21bea0 | 2176 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2177 | |
5d7836c4 JS |
2178 | return true; |
2179 | } | |
41e155b4 WS |
2180 | |
2181 | return false; | |
5d7836c4 JS |
2182 | } |
2183 | ||
2184 | /// Move to the end of the paragraph | |
2185 | bool wxRichTextCtrl::MoveToParagraphEnd(int flags) | |
2186 | { | |
603f702b | 2187 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(m_caretPosition, true); |
5d7836c4 JS |
2188 | if (para) |
2189 | { | |
2190 | long newPos = para->GetRange().GetEnd() - 1; | |
c59f6793 JS |
2191 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
2192 | if (!extendSel) | |
5d7836c4 JS |
2193 | SelectNone(); |
2194 | ||
2195 | SetCaretPosition(newPos); | |
2196 | PositionCaret(); | |
0a21bea0 | 2197 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2198 | |
5d7836c4 JS |
2199 | return true; |
2200 | } | |
2201 | ||
2202 | return false; | |
2203 | } | |
2204 | ||
2205 | /// Move to the start of the paragraph | |
2206 | bool wxRichTextCtrl::MoveToParagraphStart(int flags) | |
2207 | { | |
603f702b | 2208 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(m_caretPosition, true); |
5d7836c4 JS |
2209 | if (para) |
2210 | { | |
2211 | long newPos = para->GetRange().GetStart() - 1; | |
c59f6793 JS |
2212 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
2213 | if (!extendSel) | |
5d7836c4 JS |
2214 | SelectNone(); |
2215 | ||
04b2b47a | 2216 | SetCaretPosition(newPos, true); |
5d7836c4 | 2217 | PositionCaret(); |
0a21bea0 | 2218 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2219 | |
5d7836c4 JS |
2220 | return true; |
2221 | } | |
2222 | ||
2223 | return false; | |
2224 | } | |
2225 | ||
2226 | /// Move to the end of the line | |
2227 | bool wxRichTextCtrl::MoveToLineEnd(int flags) | |
2228 | { | |
2229 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
2230 | ||
2231 | if (line) | |
2232 | { | |
1e967276 JS |
2233 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
2234 | long newPos = lineRange.GetEnd(); | |
c59f6793 JS |
2235 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
2236 | if (!extendSel) | |
5d7836c4 JS |
2237 | SelectNone(); |
2238 | ||
2239 | SetCaretPosition(newPos); | |
2240 | PositionCaret(); | |
0a21bea0 | 2241 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2242 | |
5d7836c4 JS |
2243 | return true; |
2244 | } | |
2245 | ||
2246 | return false; | |
2247 | } | |
2248 | ||
2249 | /// Move to the start of the line | |
2250 | bool wxRichTextCtrl::MoveToLineStart(int flags) | |
2251 | { | |
2252 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
2253 | if (line) | |
2254 | { | |
1e967276 JS |
2255 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
2256 | long newPos = lineRange.GetStart()-1; | |
5d7836c4 | 2257 | |
c59f6793 JS |
2258 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
2259 | if (!extendSel) | |
5d7836c4 JS |
2260 | SelectNone(); |
2261 | ||
603f702b | 2262 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphForLine(line); |
5d7836c4 | 2263 | |
1e967276 | 2264 | SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart()); |
5d7836c4 | 2265 | PositionCaret(); |
0a21bea0 | 2266 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2267 | |
5d7836c4 JS |
2268 | return true; |
2269 | } | |
2270 | ||
2271 | return false; | |
2272 | } | |
2273 | ||
2274 | /// Move to the start of the buffer | |
2275 | bool wxRichTextCtrl::MoveHome(int flags) | |
2276 | { | |
2277 | if (m_caretPosition != -1) | |
2278 | { | |
c59f6793 JS |
2279 | bool extendSel = ExtendSelection(m_caretPosition, -1, flags); |
2280 | if (!extendSel) | |
5d7836c4 JS |
2281 | SelectNone(); |
2282 | ||
2283 | SetCaretPosition(-1); | |
2284 | PositionCaret(); | |
0a21bea0 | 2285 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2286 | |
5d7836c4 JS |
2287 | return true; |
2288 | } | |
2289 | else | |
7fe8059f | 2290 | return false; |
5d7836c4 JS |
2291 | } |
2292 | ||
2293 | /// Move to the end of the buffer | |
2294 | bool wxRichTextCtrl::MoveEnd(int flags) | |
2295 | { | |
603f702b | 2296 | long endPos = GetFocusObject()->GetOwnRange().GetEnd()-1; |
5d7836c4 JS |
2297 | |
2298 | if (m_caretPosition != endPos) | |
2299 | { | |
c59f6793 JS |
2300 | bool extendSel = ExtendSelection(m_caretPosition, endPos, flags); |
2301 | if (!extendSel) | |
5d7836c4 JS |
2302 | SelectNone(); |
2303 | ||
2304 | SetCaretPosition(endPos); | |
2305 | PositionCaret(); | |
0a21bea0 | 2306 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2307 | |
5d7836c4 JS |
2308 | return true; |
2309 | } | |
2310 | else | |
7fe8059f | 2311 | return false; |
5d7836c4 JS |
2312 | } |
2313 | ||
2314 | /// Move noPages pages up | |
2315 | bool wxRichTextCtrl::PageUp(int noPages, int flags) | |
2316 | { | |
2317 | return PageDown(- noPages, flags); | |
2318 | } | |
2319 | ||
2320 | /// Move noPages pages down | |
2321 | bool wxRichTextCtrl::PageDown(int noPages, int flags) | |
2322 | { | |
2323 | // Calculate which line occurs noPages * screen height further down. | |
2324 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
2325 | if (line) | |
2326 | { | |
2327 | wxSize clientSize = GetClientSize(); | |
2328 | int newY = line->GetAbsolutePosition().y + noPages*clientSize.y; | |
2329 | ||
603f702b | 2330 | wxRichTextLine* newLine = GetFocusObject()->GetLineAtYPosition(newY); |
5d7836c4 JS |
2331 | if (newLine) |
2332 | { | |
1e967276 JS |
2333 | wxRichTextRange lineRange = newLine->GetAbsoluteRange(); |
2334 | long pos = lineRange.GetStart()-1; | |
5d7836c4 JS |
2335 | if (pos != m_caretPosition) |
2336 | { | |
603f702b | 2337 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphForLine(newLine); |
5d7836c4 | 2338 | |
c59f6793 JS |
2339 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
2340 | if (!extendSel) | |
5d7836c4 JS |
2341 | SelectNone(); |
2342 | ||
1e967276 | 2343 | SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart()); |
5d7836c4 | 2344 | PositionCaret(); |
0a21bea0 | 2345 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2346 | |
5d7836c4 JS |
2347 | return true; |
2348 | } | |
2349 | } | |
2350 | } | |
2351 | ||
2352 | return false; | |
2353 | } | |
2354 | ||
0871b343 JS |
2355 | static bool wxRichTextCtrlIsWhitespace(const wxString& str) |
2356 | { | |
bea12bf0 | 2357 | return str == wxT(" ") || str == wxT("\t") || (!str.empty() && (str[0] == (wxChar) 160)); |
0871b343 JS |
2358 | } |
2359 | ||
5d7836c4 JS |
2360 | // Finds the caret position for the next word |
2361 | long wxRichTextCtrl::FindNextWordPosition(int direction) const | |
7fe8059f | 2362 | { |
603f702b | 2363 | long endPos = GetFocusObject()->GetOwnRange().GetEnd(); |
5d7836c4 JS |
2364 | |
2365 | if (direction > 0) | |
2366 | { | |
2367 | long i = m_caretPosition+1+direction; // +1 for conversion to character pos | |
7fe8059f | 2368 | |
5d7836c4 JS |
2369 | // First skip current text to space |
2370 | while (i < endPos && i > -1) | |
2371 | { | |
2372 | // i is in character, not caret positions | |
603f702b JS |
2373 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i)); |
2374 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false); | |
0871b343 JS |
2375 | if (line && (i == line->GetAbsoluteRange().GetEnd())) |
2376 | { | |
2377 | break; | |
2378 | } | |
2379 | else if (!wxRichTextCtrlIsWhitespace(text) && !text.empty()) | |
5d7836c4 JS |
2380 | i += direction; |
2381 | else | |
2382 | { | |
2383 | break; | |
2384 | } | |
2385 | } | |
2386 | while (i < endPos && i > -1) | |
2387 | { | |
2388 | // i is in character, not caret positions | |
603f702b JS |
2389 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i)); |
2390 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false); | |
0871b343 JS |
2391 | if (line && (i == line->GetAbsoluteRange().GetEnd())) |
2392 | return wxMax(-1, i); | |
2393 | ||
7fe8059f | 2394 | if (text.empty()) // End of paragraph, or maybe an image |
5d7836c4 | 2395 | return wxMax(-1, i - 1); |
0871b343 | 2396 | else if (wxRichTextCtrlIsWhitespace(text) || text.empty()) |
5d7836c4 JS |
2397 | i += direction; |
2398 | else | |
2399 | { | |
2400 | // Convert to caret position | |
2401 | return wxMax(-1, i - 1); | |
2402 | } | |
2403 | } | |
2404 | if (i >= endPos) | |
2405 | return endPos-1; | |
2406 | return i-1; | |
2407 | } | |
2408 | else | |
2409 | { | |
2410 | long i = m_caretPosition; | |
2411 | ||
2412 | // First skip white space | |
2413 | while (i < endPos && i > -1) | |
2414 | { | |
2415 | // i is in character, not caret positions | |
603f702b JS |
2416 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i)); |
2417 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false); | |
0871b343 JS |
2418 | |
2419 | if (text.empty() || (line && (i == line->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image | |
5d7836c4 | 2420 | break; |
0871b343 | 2421 | else if (wxRichTextCtrlIsWhitespace(text) || text.empty()) |
5d7836c4 JS |
2422 | i += direction; |
2423 | else | |
2424 | break; | |
2425 | } | |
2426 | // Next skip current text to space | |
2427 | while (i < endPos && i > -1) | |
2428 | { | |
2429 | // i is in character, not caret positions | |
603f702b JS |
2430 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i)); |
2431 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false); | |
0871b343 JS |
2432 | if (line && line->GetAbsoluteRange().GetStart() == i) |
2433 | return i-1; | |
2434 | ||
2435 | if (!wxRichTextCtrlIsWhitespace(text) /* && !text.empty() */) | |
5d7836c4 JS |
2436 | i += direction; |
2437 | else | |
2438 | { | |
2439 | return i; | |
5d7836c4 JS |
2440 | } |
2441 | } | |
2442 | if (i < -1) | |
2443 | return -1; | |
2444 | return i; | |
2445 | } | |
2446 | } | |
2447 | ||
2448 | /// Move n words left | |
2449 | bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags) | |
2450 | { | |
2451 | long pos = FindNextWordPosition(-1); | |
2452 | if (pos != m_caretPosition) | |
2453 | { | |
603f702b | 2454 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos, true); |
7fe8059f | 2455 | |
c59f6793 JS |
2456 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
2457 | if (!extendSel) | |
5d7836c4 | 2458 | SelectNone(); |
7fe8059f | 2459 | |
5d7836c4 JS |
2460 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); |
2461 | PositionCaret(); | |
0a21bea0 | 2462 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2463 | |
5d7836c4 JS |
2464 | return true; |
2465 | } | |
7fe8059f | 2466 | |
5d7836c4 JS |
2467 | return false; |
2468 | } | |
2469 | ||
2470 | /// Move n words right | |
2471 | bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags) | |
2472 | { | |
2473 | long pos = FindNextWordPosition(1); | |
2474 | if (pos != m_caretPosition) | |
2475 | { | |
603f702b | 2476 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos, true); |
7fe8059f | 2477 | |
c59f6793 JS |
2478 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
2479 | if (!extendSel) | |
5d7836c4 | 2480 | SelectNone(); |
7fe8059f | 2481 | |
5d7836c4 JS |
2482 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); |
2483 | PositionCaret(); | |
0a21bea0 | 2484 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2485 | |
5d7836c4 JS |
2486 | return true; |
2487 | } | |
7fe8059f | 2488 | |
5d7836c4 JS |
2489 | return false; |
2490 | } | |
2491 | ||
2492 | /// Sizing | |
2493 | void wxRichTextCtrl::OnSize(wxSizeEvent& event) | |
2494 | { | |
4d551ad5 | 2495 | // Only do sizing optimization for large buffers |
603f702b | 2496 | if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold) |
4d551ad5 JS |
2497 | { |
2498 | m_fullLayoutRequired = true; | |
2499 | m_fullLayoutTime = wxGetLocalTimeMillis(); | |
2500 | m_fullLayoutSavedPosition = GetFirstVisiblePosition(); | |
2f36e8dc | 2501 | LayoutContent(true /* onlyVisibleRect */); |
4d551ad5 JS |
2502 | } |
2503 | else | |
2504 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
7fe8059f | 2505 | |
3e3a754f | 2506 | #if wxRICHTEXT_BUFFERED_PAINTING |
5d7836c4 | 2507 | RecreateBuffer(); |
3e3a754f | 2508 | #endif |
5d7836c4 JS |
2509 | |
2510 | event.Skip(); | |
2511 | } | |
2512 | ||
98769552 JS |
2513 | // Force any pending layout due to large buffer |
2514 | void wxRichTextCtrl::ForceDelayedLayout() | |
2515 | { | |
2516 | if (m_fullLayoutRequired) | |
2517 | { | |
2518 | m_fullLayoutRequired = false; | |
2519 | m_fullLayoutTime = 0; | |
2520 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
2521 | ShowPosition(m_fullLayoutSavedPosition); | |
2522 | Refresh(false); | |
2523 | Update(); | |
2524 | } | |
2525 | } | |
4d551ad5 JS |
2526 | |
2527 | /// Idle-time processing | |
2528 | void wxRichTextCtrl::OnIdle(wxIdleEvent& event) | |
2529 | { | |
1c13f06e JS |
2530 | #if wxRICHTEXT_USE_OWN_CARET |
2531 | if (((wxRichTextCaret*) GetCaret())->GetNeedsUpdate()) | |
2532 | { | |
2533 | ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate(false); | |
2534 | PositionCaret(); | |
2535 | GetCaret()->Show(); | |
2536 | } | |
2537 | #endif | |
2538 | ||
4d551ad5 JS |
2539 | const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL; |
2540 | ||
2541 | if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval))) | |
2542 | { | |
2543 | m_fullLayoutRequired = false; | |
2544 | m_fullLayoutTime = 0; | |
2545 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
2546 | ShowPosition(m_fullLayoutSavedPosition); | |
76bcd815 | 2547 | Refresh(false); |
4d551ad5 | 2548 | } |
ab14c7aa JS |
2549 | |
2550 | if (m_caretPositionForDefaultStyle != -2) | |
2551 | { | |
2552 | // If the caret position has changed, no longer reflect the default style | |
2553 | // in the UI. | |
2554 | if (GetCaretPosition() != m_caretPositionForDefaultStyle) | |
2555 | m_caretPositionForDefaultStyle = -2; | |
2556 | } | |
2557 | ||
4d551ad5 JS |
2558 | event.Skip(); |
2559 | } | |
2560 | ||
c59f6793 JS |
2561 | /// Scrolling |
2562 | void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event) | |
2563 | { | |
1c13f06e JS |
2564 | #if wxRICHTEXT_USE_OWN_CARET |
2565 | if (!((wxRichTextCaret*) GetCaret())->GetNeedsUpdate()) | |
2566 | { | |
2567 | GetCaret()->Hide(); | |
2568 | ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate(); | |
2569 | } | |
2570 | #endif | |
2571 | ||
c59f6793 JS |
2572 | event.Skip(); |
2573 | } | |
2574 | ||
5d7836c4 | 2575 | /// Set up scrollbars, e.g. after a resize |
169adfa9 | 2576 | void wxRichTextCtrl::SetupScrollbars(bool atTop) |
5d7836c4 | 2577 | { |
17808a75 | 2578 | if (IsFrozen()) |
5d7836c4 JS |
2579 | return; |
2580 | ||
12f5219f | 2581 | if (GetBuffer().IsEmpty() || !m_verticalScrollbarEnabled) |
5d7836c4 JS |
2582 | { |
2583 | SetScrollbars(0, 0, 0, 0, 0, 0); | |
2584 | return; | |
2585 | } | |
2586 | ||
2587 | // TODO: reimplement scrolling so we scroll by line, not by fixed number | |
2588 | // of pixels. See e.g. wxVScrolledWindow for ideas. | |
3956d02a | 2589 | int pixelsPerUnit = 5; |
5d7836c4 JS |
2590 | wxSize clientSize = GetClientSize(); |
2591 | ||
4ba36292 | 2592 | int maxHeight = (int) (0.5 + GetScale() * (GetBuffer().GetCachedSize().y + GetBuffer().GetTopMargin())); |
5d7836c4 | 2593 | |
3956d02a JS |
2594 | // Round up so we have at least maxHeight pixels |
2595 | int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5); | |
5d7836c4 | 2596 | |
169adfa9 JS |
2597 | int startX = 0, startY = 0; |
2598 | if (!atTop) | |
2599 | GetViewStart(& startX, & startY); | |
7fe8059f | 2600 | |
7051fa41 | 2601 | int maxPositionX = 0; |
3956d02a | 2602 | int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5); |
7fe8059f | 2603 | |
7051fa41 JS |
2604 | int newStartX = wxMin(maxPositionX, startX); |
2605 | int newStartY = wxMin(maxPositionY, startY); | |
2606 | ||
2607 | int oldPPUX, oldPPUY; | |
2608 | int oldStartX, oldStartY; | |
2609 | int oldVirtualSizeX = 0, oldVirtualSizeY = 0; | |
2610 | GetScrollPixelsPerUnit(& oldPPUX, & oldPPUY); | |
2611 | GetViewStart(& oldStartX, & oldStartY); | |
2612 | GetVirtualSize(& oldVirtualSizeX, & oldVirtualSizeY); | |
2613 | if (oldPPUY > 0) | |
2614 | oldVirtualSizeY /= oldPPUY; | |
2615 | ||
2616 | if (oldPPUX == 0 && oldPPUY == pixelsPerUnit && oldVirtualSizeY == unitsY && oldStartX == newStartX && oldStartY == newStartY) | |
2617 | return; | |
2618 | ||
2619 | // Don't set scrollbars if there were none before, and there will be none now. | |
205a1ad9 | 2620 | if (oldPPUY != 0 && (oldVirtualSizeY*oldPPUY < clientSize.y) && (unitsY*pixelsPerUnit < clientSize.y)) |
7051fa41 JS |
2621 | return; |
2622 | ||
5d7836c4 JS |
2623 | // Move to previous scroll position if |
2624 | // possible | |
7051fa41 | 2625 | SetScrollbars(0, pixelsPerUnit, 0, unitsY, newStartX, newStartY); |
5d7836c4 JS |
2626 | } |
2627 | ||
2628 | /// Paint the background | |
2629 | void wxRichTextCtrl::PaintBackground(wxDC& dc) | |
2630 | { | |
2631 | wxColour backgroundColour = GetBackgroundColour(); | |
a1b806b9 | 2632 | if (!backgroundColour.IsOk()) |
5d7836c4 JS |
2633 | backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
2634 | ||
2635 | // Clear the background | |
2636 | dc.SetBrush(wxBrush(backgroundColour)); | |
2637 | dc.SetPen(*wxTRANSPARENT_PEN); | |
7fe8059f | 2638 | wxRect windowRect(GetClientSize()); |
5d7836c4 JS |
2639 | windowRect.x -= 2; windowRect.y -= 2; |
2640 | windowRect.width += 4; windowRect.height += 4; | |
7fe8059f | 2641 | |
5d7836c4 JS |
2642 | // We need to shift the rectangle to take into account |
2643 | // scrolling. Converting device to logical coordinates. | |
2644 | CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y); | |
2645 | dc.DrawRectangle(windowRect); | |
2646 | } | |
2647 | ||
3e3a754f | 2648 | #if wxRICHTEXT_BUFFERED_PAINTING |
5d7836c4 JS |
2649 | /// Recreate buffer bitmap if necessary |
2650 | bool wxRichTextCtrl::RecreateBuffer(const wxSize& size) | |
2651 | { | |
2652 | wxSize sz = size; | |
2653 | if (sz == wxDefaultSize) | |
2654 | sz = GetClientSize(); | |
7fe8059f | 2655 | |
5d7836c4 JS |
2656 | if (sz.x < 1 || sz.y < 1) |
2657 | return false; | |
7fe8059f | 2658 | |
a1b806b9 | 2659 | if (!m_bufferBitmap.IsOk() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y) |
5d7836c4 | 2660 | m_bufferBitmap = wxBitmap(sz.x, sz.y); |
a1b806b9 | 2661 | return m_bufferBitmap.IsOk(); |
5d7836c4 | 2662 | } |
3e3a754f | 2663 | #endif |
5d7836c4 JS |
2664 | |
2665 | // ---------------------------------------------------------------------------- | |
2666 | // file IO functions | |
2667 | // ---------------------------------------------------------------------------- | |
2668 | ||
3306aec1 JS |
2669 | bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType) |
2670 | { | |
5a376169 JS |
2671 | SetFocusObject(& GetBuffer(), true); |
2672 | ||
d75a69e8 | 2673 | bool success = GetBuffer().LoadFile(filename, (wxRichTextFileType)fileType); |
5d7836c4 JS |
2674 | if (success) |
2675 | m_filename = filename; | |
2676 | ||
2677 | DiscardEdits(); | |
2678 | SetInsertionPoint(0); | |
2f36e8dc | 2679 | LayoutContent(); |
5d7836c4 | 2680 | PositionCaret(); |
169adfa9 | 2681 | SetupScrollbars(true); |
76bcd815 | 2682 | Refresh(false); |
0ec1179b | 2683 | wxTextCtrl::SendTextUpdatedEvent(this); |
5d7836c4 JS |
2684 | |
2685 | if (success) | |
2686 | return true; | |
2687 | else | |
2688 | { | |
2689 | wxLogError(_("File couldn't be loaded.")); | |
7fe8059f | 2690 | |
5d7836c4 JS |
2691 | return false; |
2692 | } | |
2693 | } | |
2694 | ||
3306aec1 | 2695 | bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType) |
5d7836c4 | 2696 | { |
d75a69e8 | 2697 | if (GetBuffer().SaveFile(filename, (wxRichTextFileType)fileType)) |
5d7836c4 | 2698 | { |
3306aec1 | 2699 | m_filename = filename; |
5d7836c4 JS |
2700 | |
2701 | DiscardEdits(); | |
2702 | ||
2703 | return true; | |
5d7836c4 JS |
2704 | } |
2705 | ||
2706 | wxLogError(_("The text couldn't be saved.")); | |
2707 | ||
2708 | return false; | |
2709 | } | |
2710 | ||
2711 | // ---------------------------------------------------------------------------- | |
2712 | // wxRichTextCtrl specific functionality | |
2713 | // ---------------------------------------------------------------------------- | |
2714 | ||
2715 | /// Add a new paragraph of text to the end of the buffer | |
2716 | wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text) | |
2717 | { | |
603f702b | 2718 | wxRichTextRange range = GetFocusObject()->AddParagraph(text); |
4fe83b93 | 2719 | GetBuffer().Invalidate(); |
62381daa JS |
2720 | LayoutContent(); |
2721 | return range; | |
5d7836c4 JS |
2722 | } |
2723 | ||
2724 | /// Add an image | |
2725 | wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image) | |
2726 | { | |
603f702b | 2727 | wxRichTextRange range = GetFocusObject()->AddImage(image); |
4fe83b93 | 2728 | GetBuffer().Invalidate(); |
62381daa JS |
2729 | LayoutContent(); |
2730 | return range; | |
5d7836c4 JS |
2731 | } |
2732 | ||
2733 | // ---------------------------------------------------------------------------- | |
2734 | // selection and ranges | |
2735 | // ---------------------------------------------------------------------------- | |
2736 | ||
5d7836c4 JS |
2737 | /// Select none |
2738 | void wxRichTextCtrl::SelectNone() | |
2739 | { | |
603f702b | 2740 | if (m_selection.IsValid()) |
c1aec196 | 2741 | { |
603f702b | 2742 | wxRichTextSelection oldSelection = m_selection; |
7051fa41 | 2743 | |
603f702b | 2744 | m_selection.Reset(); |
7051fa41 | 2745 | |
603f702b | 2746 | RefreshForSelectionChange(oldSelection, m_selection); |
c1aec196 | 2747 | } |
5d7836c4 | 2748 | m_selectionAnchor = -2; |
603f702b JS |
2749 | m_selectionAnchorObject = NULL; |
2750 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
5d7836c4 JS |
2751 | } |
2752 | ||
0ca07313 JS |
2753 | static bool wxIsWordDelimiter(const wxString& text) |
2754 | { | |
1c07e924 | 2755 | return !text.IsEmpty() && !wxIsalnum(text[0]); |
0ca07313 JS |
2756 | } |
2757 | ||
2758 | /// Select the word at the given character position | |
2759 | bool wxRichTextCtrl::SelectWord(long position) | |
2760 | { | |
603f702b | 2761 | if (position < 0 || position > GetFocusObject()->GetOwnRange().GetEnd()) |
0ca07313 | 2762 | return false; |
1807a1f3 | 2763 | |
603f702b | 2764 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(position); |
0ca07313 JS |
2765 | if (!para) |
2766 | return false; | |
2767 | ||
62381daa JS |
2768 | if (position == para->GetRange().GetEnd()) |
2769 | position --; | |
2770 | ||
0ca07313 JS |
2771 | long positionStart = position; |
2772 | long positionEnd = position; | |
1807a1f3 | 2773 | |
0ca07313 JS |
2774 | for (positionStart = position; positionStart >= para->GetRange().GetStart(); positionStart --) |
2775 | { | |
603f702b | 2776 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart, positionStart)); |
0ca07313 JS |
2777 | if (wxIsWordDelimiter(text)) |
2778 | { | |
2779 | positionStart ++; | |
2780 | break; | |
2781 | } | |
2782 | } | |
2783 | if (positionStart < para->GetRange().GetStart()) | |
2784 | positionStart = para->GetRange().GetStart(); | |
1807a1f3 | 2785 | |
0ca07313 JS |
2786 | for (positionEnd = position; positionEnd < para->GetRange().GetEnd(); positionEnd ++) |
2787 | { | |
603f702b | 2788 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd, positionEnd)); |
0ca07313 JS |
2789 | if (wxIsWordDelimiter(text)) |
2790 | { | |
2791 | positionEnd --; | |
2792 | break; | |
2793 | } | |
2794 | } | |
2795 | if (positionEnd >= para->GetRange().GetEnd()) | |
2796 | positionEnd = para->GetRange().GetEnd(); | |
1807a1f3 | 2797 | |
62381daa JS |
2798 | if (positionEnd < positionStart) |
2799 | return false; | |
2800 | ||
0ca07313 JS |
2801 | SetSelection(positionStart, positionEnd+1); |
2802 | ||
2803 | if (positionStart >= 0) | |
2804 | { | |
2805 | MoveCaret(positionStart-1, true); | |
0a21bea0 | 2806 | SetDefaultStyleToCursorStyle(); |
0ca07313 JS |
2807 | } |
2808 | ||
2809 | return true; | |
2810 | } | |
2811 | ||
5d7836c4 JS |
2812 | wxString wxRichTextCtrl::GetStringSelection() const |
2813 | { | |
2814 | long from, to; | |
2815 | GetSelection(&from, &to); | |
2816 | ||
2817 | return GetRange(from, to); | |
2818 | } | |
2819 | ||
5d7836c4 JS |
2820 | // ---------------------------------------------------------------------------- |
2821 | // hit testing | |
2822 | // ---------------------------------------------------------------------------- | |
2823 | ||
2824 | wxTextCtrlHitTestResult | |
2825 | wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const | |
2826 | { | |
2827 | // implement in terms of the other overload as the native ports typically | |
2828 | // can get the position and not (x, y) pair directly (although wxUniv | |
2829 | // directly gets x and y -- and so overrides this method as well) | |
2830 | long pos; | |
2831 | wxTextCtrlHitTestResult rc = HitTest(pt, &pos); | |
2832 | ||
2833 | if ( rc != wxTE_HT_UNKNOWN ) | |
2834 | { | |
2835 | PositionToXY(pos, x, y); | |
2836 | } | |
2837 | ||
2838 | return rc; | |
2839 | } | |
2840 | ||
2841 | wxTextCtrlHitTestResult | |
2842 | wxRichTextCtrl::HitTest(const wxPoint& pt, | |
2843 | long * pos) const | |
2844 | { | |
2845 | wxClientDC dc((wxRichTextCtrl*) this); | |
2846 | ((wxRichTextCtrl*)this)->PrepareDC(dc); | |
61399247 | 2847 | |
563d25ba JS |
2848 | // Buffer uses logical position (relative to start of buffer) |
2849 | // so convert | |
2850 | wxPoint pt2 = GetLogicalPoint(pt); | |
5d7836c4 | 2851 | |
603f702b JS |
2852 | wxRichTextObject* hitObj = NULL; |
2853 | wxRichTextObject* contextObj = NULL; | |
8db2e3ef JS |
2854 | wxRichTextDrawingContext context((wxRichTextBuffer*) & GetBuffer()); |
2855 | int hit = ((wxRichTextCtrl*)this)->GetFocusObject()->HitTest(dc, context, pt2, *pos, & hitObj, & contextObj, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS); | |
41e155b4 | 2856 | |
f262b25c JS |
2857 | if ((hit & wxRICHTEXT_HITTEST_BEFORE) && (hit & wxRICHTEXT_HITTEST_OUTSIDE)) |
2858 | return wxTE_HT_BEFORE; | |
2859 | else if ((hit & wxRICHTEXT_HITTEST_AFTER) && (hit & wxRICHTEXT_HITTEST_OUTSIDE)) | |
2860 | return wxTE_HT_BEYOND; | |
25d6eb3a | 2861 | else if (hit & (wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER)) |
f262b25c | 2862 | return wxTE_HT_ON_TEXT; |
41e155b4 WS |
2863 | |
2864 | return wxTE_HT_UNKNOWN; | |
5d7836c4 JS |
2865 | } |
2866 | ||
0c0e063e JS |
2867 | wxRichTextParagraphLayoutBox* |
2868 | wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt, long& position, int& hit, wxRichTextObject* hitObj, int flags/* = 0*/) | |
2869 | { | |
2870 | wxClientDC dc(this); | |
2871 | PrepareDC(dc); | |
2872 | dc.SetFont(GetFont()); | |
2873 | ||
2874 | wxPoint logicalPt = GetLogicalPoint(pt); | |
2875 | ||
2876 | wxRichTextObject* contextObj = NULL; | |
8db2e3ef | 2877 | wxRichTextDrawingContext context(& GetBuffer()); |
4ba36292 | 2878 | hit = GetBuffer().HitTest(dc, context, GetUnscaledPoint(logicalPt), position, &hitObj, &contextObj, flags); |
0c0e063e JS |
2879 | wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); |
2880 | ||
2881 | return container; | |
2882 | } | |
2883 | ||
2884 | ||
5d7836c4 JS |
2885 | // ---------------------------------------------------------------------------- |
2886 | // set/get the controls text | |
2887 | // ---------------------------------------------------------------------------- | |
2888 | ||
805c8d92 | 2889 | wxString wxRichTextCtrl::DoGetValue() const |
5d7836c4 JS |
2890 | { |
2891 | return GetBuffer().GetText(); | |
2892 | } | |
2893 | ||
2894 | wxString wxRichTextCtrl::GetRange(long from, long to) const | |
2895 | { | |
96c9f0f6 | 2896 | // Public API for range is different from internals |
603f702b | 2897 | return GetFocusObject()->GetTextForRange(wxRichTextRange(from, to-1)); |
5d7836c4 JS |
2898 | } |
2899 | ||
343ae70d | 2900 | void wxRichTextCtrl::DoSetValue(const wxString& value, int flags) |
5d7836c4 | 2901 | { |
d081739d JS |
2902 | // Don't call Clear here, since it always sends a text updated event |
2903 | m_buffer.ResetAndClearCommands(); | |
603f702b | 2904 | m_buffer.Invalidate(wxRICHTEXT_ALL); |
d081739d JS |
2905 | m_caretPosition = -1; |
2906 | m_caretPositionForDefaultStyle = -2; | |
2907 | m_caretAtLineStart = false; | |
603f702b JS |
2908 | m_selection.Reset(); |
2909 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
d081739d JS |
2910 | |
2911 | Scroll(0,0); | |
2912 | ||
2913 | if (!IsFrozen()) | |
2914 | { | |
2915 | LayoutContent(); | |
2916 | Refresh(false); | |
2917 | } | |
5d7836c4 | 2918 | |
1a75935d | 2919 | if (!value.IsEmpty()) |
5d7836c4 | 2920 | { |
1a75935d JS |
2921 | // Remove empty paragraph |
2922 | GetBuffer().Clear(); | |
d081739d | 2923 | DoWriteText(value, flags); |
5d7836c4 JS |
2924 | |
2925 | // for compatibility, don't move the cursor when doing SetValue() | |
2926 | SetInsertionPoint(0); | |
2927 | } | |
1a75935d | 2928 | else |
5d7836c4 | 2929 | { |
1a75935d JS |
2930 | // still send an event for consistency |
2931 | if (flags & SetValue_SendEvent) | |
0ec1179b | 2932 | wxTextCtrl::SendTextUpdatedEvent(this); |
5d7836c4 | 2933 | } |
5d7836c4 JS |
2934 | DiscardEdits(); |
2935 | } | |
2936 | ||
2937 | void wxRichTextCtrl::WriteText(const wxString& value) | |
2938 | { | |
2939 | DoWriteText(value); | |
2940 | } | |
2941 | ||
343ae70d | 2942 | void wxRichTextCtrl::DoWriteText(const wxString& value, int flags) |
5d7836c4 | 2943 | { |
0ca07313 | 2944 | wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix); |
fe5aa22c | 2945 | |
4e63bfb9 | 2946 | GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition+1, valueUnix, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); |
f7667b84 JS |
2947 | wxRichTextDrawingContext context(& GetBuffer()); |
2948 | GetBuffer().Defragment(context); | |
343ae70d VZ |
2949 | |
2950 | if ( flags & SetValue_SendEvent ) | |
0ec1179b | 2951 | wxTextCtrl::SendTextUpdatedEvent(this); |
5d7836c4 JS |
2952 | } |
2953 | ||
2954 | void wxRichTextCtrl::AppendText(const wxString& text) | |
2955 | { | |
2956 | SetInsertionPointEnd(); | |
2957 | ||
2958 | WriteText(text); | |
2959 | } | |
2960 | ||
2961 | /// Write an image at the current insertion point | |
24777478 | 2962 | bool wxRichTextCtrl::WriteImage(const wxImage& image, wxBitmapType bitmapType, const wxRichTextAttr& textAttr) |
5d7836c4 JS |
2963 | { |
2964 | wxRichTextImageBlock imageBlock; | |
2965 | ||
2966 | wxImage image2 = image; | |
2967 | if (imageBlock.MakeImageBlock(image2, bitmapType)) | |
24777478 | 2968 | return WriteImage(imageBlock, textAttr); |
41e155b4 WS |
2969 | |
2970 | return false; | |
5d7836c4 JS |
2971 | } |
2972 | ||
24777478 | 2973 | bool wxRichTextCtrl::WriteImage(const wxString& filename, wxBitmapType bitmapType, const wxRichTextAttr& textAttr) |
5d7836c4 JS |
2974 | { |
2975 | wxRichTextImageBlock imageBlock; | |
2976 | ||
2977 | wxImage image; | |
2978 | if (imageBlock.MakeImageBlock(filename, bitmapType, image, false)) | |
24777478 | 2979 | return WriteImage(imageBlock, textAttr); |
41e155b4 WS |
2980 | |
2981 | return false; | |
5d7836c4 JS |
2982 | } |
2983 | ||
24777478 | 2984 | bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock, const wxRichTextAttr& textAttr) |
5d7836c4 | 2985 | { |
4e63bfb9 | 2986 | return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition+1, imageBlock, this, 0, textAttr); |
5d7836c4 JS |
2987 | } |
2988 | ||
24777478 | 2989 | bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, wxBitmapType bitmapType, const wxRichTextAttr& textAttr) |
5d7836c4 | 2990 | { |
a1b806b9 | 2991 | if (bitmap.IsOk()) |
5d7836c4 JS |
2992 | { |
2993 | wxRichTextImageBlock imageBlock; | |
7fe8059f | 2994 | |
5d7836c4 | 2995 | wxImage image = bitmap.ConvertToImage(); |
a1b806b9 | 2996 | if (image.IsOk() && imageBlock.MakeImageBlock(image, bitmapType)) |
24777478 | 2997 | return WriteImage(imageBlock, textAttr); |
5d7836c4 | 2998 | } |
41e155b4 | 2999 | |
5d7836c4 JS |
3000 | return false; |
3001 | } | |
3002 | ||
603f702b JS |
3003 | // Write a text box at the current insertion point. |
3004 | wxRichTextBox* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr& textAttr) | |
3005 | { | |
3006 | wxRichTextBox* textBox = new wxRichTextBox; | |
3007 | textBox->SetAttributes(textAttr); | |
3008 | textBox->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style | |
3009 | textBox->AddParagraph(wxEmptyString); | |
3010 | textBox->SetParent(NULL); | |
3011 | ||
3012 | // The object returned is the one actually inserted into the buffer, | |
3013 | // while the original one is deleted. | |
4e63bfb9 | 3014 | wxRichTextObject* obj = GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition+1, textBox, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); |
603f702b JS |
3015 | wxRichTextBox* box = wxDynamicCast(obj, wxRichTextBox); |
3016 | return box; | |
3017 | } | |
3018 | ||
7c9fdebe JS |
3019 | wxRichTextField* wxRichTextCtrl::WriteField(const wxString& fieldType, const wxRichTextProperties& properties, |
3020 | const wxRichTextAttr& textAttr) | |
3021 | { | |
3022 | return GetFocusObject()->InsertFieldWithUndo(& GetBuffer(), m_caretPosition+1, fieldType, properties, | |
3023 | this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE, textAttr); | |
3024 | } | |
3025 | ||
603f702b JS |
3026 | // Write a table at the current insertion point, returning the table. |
3027 | wxRichTextTable* wxRichTextCtrl::WriteTable(int rows, int cols, const wxRichTextAttr& tableAttr, const wxRichTextAttr& cellAttr) | |
3028 | { | |
3029 | wxASSERT(rows > 0 && cols > 0); | |
3030 | ||
3031 | if (rows <= 0 || cols <= 0) | |
3032 | return NULL; | |
3033 | ||
3034 | wxRichTextTable* table = new wxRichTextTable; | |
3035 | table->SetAttributes(tableAttr); | |
3036 | table->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style | |
4fe83b93 | 3037 | |
603f702b JS |
3038 | table->CreateTable(rows, cols); |
3039 | ||
3040 | table->SetParent(NULL); | |
3041 | ||
3042 | int i, j; | |
3043 | for (j = 0; j < rows; j++) | |
3044 | { | |
3045 | for (i = 0; i < cols; i++) | |
3046 | { | |
3047 | table->GetCell(j, i)->GetAttributes() = cellAttr; | |
3048 | } | |
3049 | } | |
3050 | ||
3051 | // The object returned is the one actually inserted into the buffer, | |
3052 | // while the original one is deleted. | |
4e63bfb9 | 3053 | wxRichTextObject* obj = GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition+1, table, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); |
603f702b JS |
3054 | wxRichTextTable* tableResult = wxDynamicCast(obj, wxRichTextTable); |
3055 | return tableResult; | |
3056 | } | |
3057 | ||
3058 | ||
5d7836c4 JS |
3059 | /// Insert a newline (actually paragraph) at the current insertion point. |
3060 | bool wxRichTextCtrl::Newline() | |
3061 | { | |
4e63bfb9 | 3062 | return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); |
5d7836c4 JS |
3063 | } |
3064 | ||
ff76711f JS |
3065 | /// Insert a line break at the current insertion point. |
3066 | bool wxRichTextCtrl::LineBreak() | |
3067 | { | |
3068 | wxString text; | |
3069 | text = wxRichTextLineBreakChar; | |
4e63bfb9 | 3070 | return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition+1, text, this); |
ff76711f | 3071 | } |
5d7836c4 JS |
3072 | |
3073 | // ---------------------------------------------------------------------------- | |
3074 | // Clipboard operations | |
3075 | // ---------------------------------------------------------------------------- | |
3076 | ||
3077 | void wxRichTextCtrl::Copy() | |
3078 | { | |
3079 | if (CanCopy()) | |
3080 | { | |
fe5aa22c | 3081 | wxRichTextRange range = GetInternalSelectionRange(); |
5d7836c4 JS |
3082 | GetBuffer().CopyToClipboard(range); |
3083 | } | |
3084 | } | |
3085 | ||
3086 | void wxRichTextCtrl::Cut() | |
3087 | { | |
3088 | if (CanCut()) | |
3089 | { | |
fe5aa22c | 3090 | wxRichTextRange range = GetInternalSelectionRange(); |
5d7836c4 JS |
3091 | GetBuffer().CopyToClipboard(range); |
3092 | ||
3093 | DeleteSelectedContent(); | |
2f36e8dc | 3094 | LayoutContent(); |
76bcd815 | 3095 | Refresh(false); |
5d7836c4 JS |
3096 | } |
3097 | } | |
3098 | ||
3099 | void wxRichTextCtrl::Paste() | |
3100 | { | |
3101 | if (CanPaste()) | |
3102 | { | |
3103 | BeginBatchUndo(_("Paste")); | |
7fe8059f | 3104 | |
5d7836c4 JS |
3105 | long newPos = m_caretPosition; |
3106 | DeleteSelectedContent(& newPos); | |
7fe8059f | 3107 | |
5d7836c4 JS |
3108 | GetBuffer().PasteFromClipboard(newPos); |
3109 | ||
3110 | EndBatchUndo(); | |
3111 | } | |
3112 | } | |
3113 | ||
3114 | void wxRichTextCtrl::DeleteSelection() | |
3115 | { | |
3116 | if (CanDeleteSelection()) | |
3117 | { | |
3118 | DeleteSelectedContent(); | |
3119 | } | |
3120 | } | |
3121 | ||
3122 | bool wxRichTextCtrl::HasSelection() const | |
3123 | { | |
603f702b JS |
3124 | return (m_selection.IsValid() && m_selection.GetContainer() == GetFocusObject()); |
3125 | } | |
3126 | ||
3127 | bool wxRichTextCtrl::HasUnfocusedSelection() const | |
3128 | { | |
3129 | return m_selection.IsValid(); | |
5d7836c4 JS |
3130 | } |
3131 | ||
3132 | bool wxRichTextCtrl::CanCopy() const | |
3133 | { | |
3134 | // Can copy if there's a selection | |
3135 | return HasSelection(); | |
3136 | } | |
3137 | ||
3138 | bool wxRichTextCtrl::CanCut() const | |
3139 | { | |
cc2aecde | 3140 | return CanDeleteSelection(); |
5d7836c4 JS |
3141 | } |
3142 | ||
3143 | bool wxRichTextCtrl::CanPaste() const | |
3144 | { | |
cc2aecde | 3145 | if ( !IsEditable() || !GetFocusObject() || !CanInsertContent(* GetFocusObject(), m_caretPosition+1)) |
5d7836c4 JS |
3146 | return false; |
3147 | ||
3148 | return GetBuffer().CanPasteFromClipboard(); | |
3149 | } | |
3150 | ||
3151 | bool wxRichTextCtrl::CanDeleteSelection() const | |
3152 | { | |
cc2aecde | 3153 | return HasSelection() && IsEditable() && CanDeleteRange(* GetFocusObject(), GetSelectionRange()); |
5d7836c4 JS |
3154 | } |
3155 | ||
3156 | ||
3157 | // ---------------------------------------------------------------------------- | |
3158 | // Accessors | |
3159 | // ---------------------------------------------------------------------------- | |
3160 | ||
8e43fc77 JS |
3161 | void wxRichTextCtrl::SetContextMenu(wxMenu* menu) |
3162 | { | |
3163 | if (m_contextMenu && m_contextMenu != menu) | |
3164 | delete m_contextMenu; | |
3165 | m_contextMenu = menu; | |
3166 | } | |
3167 | ||
5d7836c4 JS |
3168 | void wxRichTextCtrl::SetEditable(bool editable) |
3169 | { | |
3170 | m_editable = editable; | |
3171 | } | |
3172 | ||
3173 | void wxRichTextCtrl::SetInsertionPoint(long pos) | |
3174 | { | |
3175 | SelectNone(); | |
3176 | ||
3177 | m_caretPosition = pos - 1; | |
04b2b47a | 3178 | m_caretAtLineStart = true; |
6f5d63bc JS |
3179 | |
3180 | PositionCaret(); | |
4427f297 JS |
3181 | |
3182 | SetDefaultStyleToCursorStyle(); | |
5d7836c4 JS |
3183 | } |
3184 | ||
3185 | void wxRichTextCtrl::SetInsertionPointEnd() | |
3186 | { | |
3187 | long pos = GetLastPosition(); | |
3188 | SetInsertionPoint(pos); | |
3189 | } | |
3190 | ||
3191 | long wxRichTextCtrl::GetInsertionPoint() const | |
3192 | { | |
3193 | return m_caretPosition+1; | |
3194 | } | |
3195 | ||
3196 | wxTextPos wxRichTextCtrl::GetLastPosition() const | |
3197 | { | |
603f702b | 3198 | return GetFocusObject()->GetOwnRange().GetEnd(); |
5d7836c4 JS |
3199 | } |
3200 | ||
3201 | // If the return values from and to are the same, there is no | |
3202 | // selection. | |
3203 | void wxRichTextCtrl::GetSelection(long* from, long* to) const | |
3204 | { | |
603f702b JS |
3205 | if (m_selection.IsValid()) |
3206 | { | |
3207 | *from = m_selection.GetRange().GetStart(); | |
3208 | *to = m_selection.GetRange().GetEnd(); | |
96c9f0f6 | 3209 | (*to) ++; |
603f702b JS |
3210 | } |
3211 | else | |
3212 | { | |
3213 | *from = -2; | |
3214 | *to = -2; | |
3215 | } | |
5d7836c4 JS |
3216 | } |
3217 | ||
3218 | bool wxRichTextCtrl::IsEditable() const | |
3219 | { | |
3220 | return m_editable; | |
3221 | } | |
3222 | ||
3223 | // ---------------------------------------------------------------------------- | |
3224 | // selection | |
3225 | // ---------------------------------------------------------------------------- | |
3226 | ||
3227 | void wxRichTextCtrl::SetSelection(long from, long to) | |
3228 | { | |
3229 | // if from and to are both -1, it means (in wxWidgets) that all text should | |
3230 | // be selected. | |
3231 | if ( (from == -1) && (to == -1) ) | |
3232 | { | |
3233 | from = 0; | |
96c9f0f6 | 3234 | to = GetLastPosition()+1; |
5d7836c4 JS |
3235 | } |
3236 | ||
c1aec196 JS |
3237 | if (from == to) |
3238 | { | |
3239 | SelectNone(); | |
3240 | } | |
3241 | else | |
3242 | { | |
603f702b JS |
3243 | wxRichTextSelection oldSelection = m_selection; |
3244 | ||
205a1ad9 | 3245 | m_selectionAnchor = from-1; |
603f702b JS |
3246 | m_selectionAnchorObject = NULL; |
3247 | m_selection.Set(wxRichTextRange(from, to-1), GetFocusObject()); | |
76d7fc29 | 3248 | |
a216dfa2 | 3249 | m_caretPosition = wxMax(-1, to-1); |
0ca07313 | 3250 | |
603f702b | 3251 | RefreshForSelectionChange(oldSelection, m_selection); |
c1aec196 JS |
3252 | PositionCaret(); |
3253 | } | |
5d7836c4 JS |
3254 | } |
3255 | ||
3256 | // ---------------------------------------------------------------------------- | |
3257 | // Editing | |
3258 | // ---------------------------------------------------------------------------- | |
3259 | ||
e3bb4c54 | 3260 | void wxRichTextCtrl::Replace(long from, long to, |
343ae70d | 3261 | const wxString& value) |
5d7836c4 JS |
3262 | { |
3263 | BeginBatchUndo(_("Replace")); | |
3264 | ||
e3bb4c54 JS |
3265 | SetSelection(from, to); |
3266 | ||
914a4e23 | 3267 | wxRichTextAttr attr(GetDefaultStyle()); |
e3bb4c54 | 3268 | |
5d7836c4 JS |
3269 | DeleteSelectedContent(); |
3270 | ||
e3bb4c54 JS |
3271 | SetDefaultStyle(attr); |
3272 | ||
4aa079bf JS |
3273 | if (!value.IsEmpty()) |
3274 | DoWriteText(value, SetValue_SelectionOnly); | |
5d7836c4 JS |
3275 | |
3276 | EndBatchUndo(); | |
3277 | } | |
3278 | ||
3279 | void wxRichTextCtrl::Remove(long from, long to) | |
3280 | { | |
3281 | SelectNone(); | |
3282 | ||
603f702b | 3283 | GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from, to-1), this, & GetBuffer()); |
5d7836c4 | 3284 | |
2f36e8dc | 3285 | LayoutContent(); |
5d7836c4 | 3286 | if (!IsFrozen()) |
76bcd815 | 3287 | Refresh(false); |
5d7836c4 JS |
3288 | } |
3289 | ||
3290 | bool wxRichTextCtrl::IsModified() const | |
3291 | { | |
3292 | return m_buffer.IsModified(); | |
3293 | } | |
3294 | ||
3295 | void wxRichTextCtrl::MarkDirty() | |
3296 | { | |
3297 | m_buffer.Modify(true); | |
3298 | } | |
3299 | ||
3300 | void wxRichTextCtrl::DiscardEdits() | |
3301 | { | |
ab14c7aa | 3302 | m_caretPositionForDefaultStyle = -2; |
5d7836c4 JS |
3303 | m_buffer.Modify(false); |
3304 | m_buffer.GetCommandProcessor()->ClearCommands(); | |
3305 | } | |
3306 | ||
3307 | int wxRichTextCtrl::GetNumberOfLines() const | |
3308 | { | |
603f702b | 3309 | return GetFocusObject()->GetParagraphCount(); |
5d7836c4 JS |
3310 | } |
3311 | ||
3312 | // ---------------------------------------------------------------------------- | |
3313 | // Positions <-> coords | |
3314 | // ---------------------------------------------------------------------------- | |
3315 | ||
3316 | long wxRichTextCtrl::XYToPosition(long x, long y) const | |
3317 | { | |
603f702b | 3318 | return GetFocusObject()->XYToPosition(x, y); |
5d7836c4 JS |
3319 | } |
3320 | ||
3321 | bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
3322 | { | |
603f702b | 3323 | return GetFocusObject()->PositionToXY(pos, x, y); |
5d7836c4 JS |
3324 | } |
3325 | ||
3326 | // ---------------------------------------------------------------------------- | |
3327 | // | |
3328 | // ---------------------------------------------------------------------------- | |
3329 | ||
3330 | void wxRichTextCtrl::ShowPosition(long pos) | |
3331 | { | |
3332 | if (!IsPositionVisible(pos)) | |
3333 | ScrollIntoView(pos-1, WXK_DOWN); | |
3334 | } | |
3335 | ||
3336 | int wxRichTextCtrl::GetLineLength(long lineNo) const | |
3337 | { | |
603f702b | 3338 | return GetFocusObject()->GetParagraphLength(lineNo); |
5d7836c4 JS |
3339 | } |
3340 | ||
3341 | wxString wxRichTextCtrl::GetLineText(long lineNo) const | |
3342 | { | |
603f702b | 3343 | return GetFocusObject()->GetParagraphText(lineNo); |
5d7836c4 JS |
3344 | } |
3345 | ||
3346 | // ---------------------------------------------------------------------------- | |
3347 | // Undo/redo | |
3348 | // ---------------------------------------------------------------------------- | |
3349 | ||
3350 | void wxRichTextCtrl::Undo() | |
3351 | { | |
3352 | if (CanUndo()) | |
3353 | { | |
3354 | GetCommandProcessor()->Undo(); | |
3355 | } | |
3356 | } | |
3357 | ||
3358 | void wxRichTextCtrl::Redo() | |
3359 | { | |
3360 | if (CanRedo()) | |
3361 | { | |
3362 | GetCommandProcessor()->Redo(); | |
3363 | } | |
3364 | } | |
3365 | ||
3366 | bool wxRichTextCtrl::CanUndo() const | |
3367 | { | |
3a4a38c1 | 3368 | return GetCommandProcessor()->CanUndo() && IsEditable(); |
5d7836c4 JS |
3369 | } |
3370 | ||
3371 | bool wxRichTextCtrl::CanRedo() const | |
3372 | { | |
3a4a38c1 | 3373 | return GetCommandProcessor()->CanRedo() && IsEditable(); |
5d7836c4 JS |
3374 | } |
3375 | ||
3376 | // ---------------------------------------------------------------------------- | |
563d25ba | 3377 | // implementation details |
5d7836c4 JS |
3378 | // ---------------------------------------------------------------------------- |
3379 | ||
563d25ba | 3380 | void wxRichTextCtrl::Command(wxCommandEvent& event) |
5d7836c4 JS |
3381 | { |
3382 | SetValue(event.GetString()); | |
3383 | GetEventHandler()->ProcessEvent(event); | |
3384 | } | |
3385 | ||
3386 | void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
3387 | { | |
3388 | // By default, load the first file into the text window. | |
3389 | if (event.GetNumberOfFiles() > 0) | |
3390 | { | |
3391 | LoadFile(event.GetFiles()[0]); | |
3392 | } | |
3393 | } | |
3394 | ||
5d7836c4 JS |
3395 | wxSize wxRichTextCtrl::DoGetBestSize() const |
3396 | { | |
3397 | return wxSize(10, 10); | |
3398 | } | |
3399 | ||
3400 | // ---------------------------------------------------------------------------- | |
3401 | // standard handlers for standard edit menu events | |
3402 | // ---------------------------------------------------------------------------- | |
3403 | ||
3404 | void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
3405 | { | |
3406 | Cut(); | |
3407 | } | |
3408 | ||
3409 | void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event)) | |
3410 | { | |
3411 | DeleteSelection(); | |
3412 | } | |
3413 | ||
3414 | void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
3415 | { | |
3416 | Copy(); | |
3417 | } | |
3418 | ||
3419 | void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
3420 | { | |
3421 | Paste(); | |
3422 | } | |
3423 | ||
3424 | void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
3425 | { | |
3426 | Undo(); | |
3427 | } | |
3428 | ||
3429 | void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
3430 | { | |
3431 | Redo(); | |
3432 | } | |
3433 | ||
3434 | void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
3435 | { | |
3436 | event.Enable( CanCut() ); | |
3437 | } | |
3438 | ||
3439 | void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
3440 | { | |
3441 | event.Enable( CanCopy() ); | |
3442 | } | |
3443 | ||
3444 | void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event) | |
3445 | { | |
3446 | event.Enable( CanDeleteSelection() ); | |
3447 | } | |
3448 | ||
3449 | void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
3450 | { | |
3451 | event.Enable( CanPaste() ); | |
3452 | } | |
3453 | ||
3454 | void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
3455 | { | |
3456 | event.Enable( CanUndo() ); | |
3457 | event.SetText( GetCommandProcessor()->GetUndoMenuLabel() ); | |
3458 | } | |
3459 | ||
3460 | void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
3461 | { | |
3462 | event.Enable( CanRedo() ); | |
3463 | event.SetText( GetCommandProcessor()->GetRedoMenuLabel() ); | |
3464 | } | |
3465 | ||
3466 | void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
3467 | { | |
b30661fd JS |
3468 | if (GetLastPosition() > 0) |
3469 | SelectAll(); | |
5d7836c4 JS |
3470 | } |
3471 | ||
3472 | void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
3473 | { | |
b30661fd | 3474 | event.Enable(GetLastPosition() > 0); |
5d7836c4 JS |
3475 | } |
3476 | ||
603f702b | 3477 | void wxRichTextCtrl::OnProperties(wxCommandEvent& event) |
cdaed652 | 3478 | { |
603f702b JS |
3479 | int idx = event.GetId() - wxID_RICHTEXT_PROPERTIES1; |
3480 | if (idx >= 0 && idx < m_contextMenuPropertiesInfo.GetCount()) | |
3481 | { | |
3482 | wxRichTextObject* obj = m_contextMenuPropertiesInfo.GetObject(idx); | |
303f0be7 JS |
3483 | if (obj && CanEditProperties(obj)) |
3484 | EditProperties(obj, this); | |
603f702b JS |
3485 | |
3486 | m_contextMenuPropertiesInfo.Clear(); | |
3487 | } | |
cdaed652 VZ |
3488 | } |
3489 | ||
603f702b | 3490 | void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent& event) |
cdaed652 | 3491 | { |
603f702b JS |
3492 | int idx = event.GetId() - wxID_RICHTEXT_PROPERTIES1; |
3493 | event.Enable(idx >= 0 && idx < m_contextMenuPropertiesInfo.GetCount()); | |
cdaed652 VZ |
3494 | } |
3495 | ||
b4ae0def | 3496 | void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event) |
5d7836c4 | 3497 | { |
b4ae0def JS |
3498 | if (event.GetEventObject() != this) |
3499 | { | |
3500 | event.Skip(); | |
3501 | return; | |
3502 | } | |
3503 | ||
9b794421 JS |
3504 | ShowContextMenu(m_contextMenu, event.GetPosition()); |
3505 | } | |
3506 | ||
3507 | // Prepares the context menu, adding appropriate property-editing commands. | |
3508 | // Returns the number of property commands added. | |
3509 | int wxRichTextCtrl::PrepareContextMenu(wxMenu* menu, const wxPoint& pt, bool addPropertyCommands) | |
3510 | { | |
cdaed652 VZ |
3511 | wxClientDC dc(this); |
3512 | PrepareDC(dc); | |
3513 | dc.SetFont(GetFont()); | |
3514 | ||
9b794421 JS |
3515 | m_contextMenuPropertiesInfo.Clear(); |
3516 | ||
cdaed652 | 3517 | long position = 0; |
603f702b JS |
3518 | wxRichTextObject* hitObj = NULL; |
3519 | wxRichTextObject* contextObj = NULL; | |
9b794421 JS |
3520 | if (pt != wxDefaultPosition) |
3521 | { | |
3522 | wxPoint logicalPt = GetLogicalPoint(ScreenToClient(pt)); | |
8db2e3ef | 3523 | wxRichTextDrawingContext context(& GetBuffer()); |
4ba36292 | 3524 | int hit = GetBuffer().HitTest(dc, context, GetUnscaledPoint(logicalPt), position, & hitObj, & contextObj); |
4fe83b93 | 3525 | |
9b794421 JS |
3526 | if (hit == wxRICHTEXT_HITTEST_ON || hit == wxRICHTEXT_HITTEST_BEFORE || hit == wxRICHTEXT_HITTEST_AFTER) |
3527 | { | |
3528 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); | |
3529 | if (hitObj && actualContainer) | |
3530 | { | |
3531 | if (actualContainer->AcceptsFocus()) | |
3532 | { | |
3533 | SetFocusObject(actualContainer, false /* don't set caret position yet */); | |
3534 | SetCaretPositionAfterClick(actualContainer, position, hit); | |
3535 | } | |
4fe83b93 | 3536 | |
9b794421 | 3537 | if (addPropertyCommands) |
303f0be7 | 3538 | m_contextMenuPropertiesInfo.AddItems(this, actualContainer, hitObj); |
9b794421 JS |
3539 | } |
3540 | else | |
3541 | { | |
3542 | if (addPropertyCommands) | |
303f0be7 | 3543 | m_contextMenuPropertiesInfo.AddItems(this, GetFocusObject(), NULL); |
9b794421 JS |
3544 | } |
3545 | } | |
3546 | else | |
3547 | { | |
3548 | if (addPropertyCommands) | |
303f0be7 | 3549 | m_contextMenuPropertiesInfo.AddItems(this, GetFocusObject(), NULL); |
9b794421 JS |
3550 | } |
3551 | } | |
3552 | else | |
cdaed652 | 3553 | { |
9b794421 JS |
3554 | // Invoked from the keyboard, so don't set the caret position and don't use the event |
3555 | // position | |
3556 | hitObj = GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition+1); | |
3557 | if (hitObj) | |
3558 | contextObj = hitObj->GetParentContainer(); | |
3559 | else | |
3560 | contextObj = GetFocusObject(); | |
3561 | ||
603f702b JS |
3562 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); |
3563 | if (hitObj && actualContainer) | |
3564 | { | |
9b794421 | 3565 | if (addPropertyCommands) |
303f0be7 | 3566 | m_contextMenuPropertiesInfo.AddItems(this, actualContainer, hitObj); |
603f702b JS |
3567 | } |
3568 | else | |
9b794421 JS |
3569 | { |
3570 | if (addPropertyCommands) | |
303f0be7 | 3571 | m_contextMenuPropertiesInfo.AddItems(this, GetFocusObject(), NULL); |
9b794421 | 3572 | } |
cdaed652 | 3573 | } |
9b794421 JS |
3574 | |
3575 | if (menu) | |
cdaed652 | 3576 | { |
9b794421 JS |
3577 | if (addPropertyCommands) |
3578 | m_contextMenuPropertiesInfo.AddMenuItems(menu); | |
3579 | return m_contextMenuPropertiesInfo.GetCount(); | |
cdaed652 | 3580 | } |
9b794421 JS |
3581 | else |
3582 | return 0; | |
3583 | } | |
cdaed652 | 3584 | |
9b794421 JS |
3585 | // Shows the context menu, adding appropriate property-editing commands |
3586 | bool wxRichTextCtrl::ShowContextMenu(wxMenu* menu, const wxPoint& pt, bool addPropertyCommands) | |
3587 | { | |
3588 | if (menu) | |
603f702b | 3589 | { |
9b794421 JS |
3590 | PrepareContextMenu(menu, pt, addPropertyCommands); |
3591 | PopupMenu(menu); | |
3592 | return true; | |
603f702b | 3593 | } |
9b794421 JS |
3594 | else |
3595 | return false; | |
5d7836c4 JS |
3596 | } |
3597 | ||
27e20452 JS |
3598 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) |
3599 | { | |
603f702b | 3600 | return GetFocusObject()->SetStyle(wxRichTextRange(start, end-1), wxRichTextAttr(style)); |
24777478 JS |
3601 | } |
3602 | ||
3603 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxRichTextAttr& style) | |
3604 | { | |
603f702b | 3605 | return GetFocusObject()->SetStyle(wxRichTextRange(start, end-1), style); |
27e20452 JS |
3606 | } |
3607 | ||
44cc96a8 | 3608 | bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxTextAttr& style) |
24777478 | 3609 | { |
603f702b | 3610 | return GetFocusObject()->SetStyle(range.ToInternal(), wxRichTextAttr(style)); |
24777478 JS |
3611 | } |
3612 | ||
3613 | bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style) | |
5d7836c4 | 3614 | { |
603f702b | 3615 | return GetFocusObject()->SetStyle(range.ToInternal(), style); |
5d7836c4 JS |
3616 | } |
3617 | ||
e059e1bb | 3618 | void wxRichTextCtrl::SetStyle(wxRichTextObject *obj, const wxRichTextAttr& textAttr, int flags) |
cdaed652 | 3619 | { |
e059e1bb | 3620 | GetFocusObject()->SetStyle(obj, textAttr, flags); |
cdaed652 VZ |
3621 | } |
3622 | ||
3e3a754f JS |
3623 | // extended style setting operation with flags including: |
3624 | // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY. | |
3625 | // see richtextbuffer.h for more details. | |
3e3a754f | 3626 | |
24777478 | 3627 | bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxRichTextAttr& style, int flags) |
3e3a754f | 3628 | { |
603f702b | 3629 | return GetFocusObject()->SetStyle(range.ToInternal(), style, flags); |
3e3a754f JS |
3630 | } |
3631 | ||
27e20452 JS |
3632 | bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style) |
3633 | { | |
24777478 | 3634 | return GetBuffer().SetDefaultStyle(style); |
5d7836c4 JS |
3635 | } |
3636 | ||
24777478 JS |
3637 | bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr& style) |
3638 | { | |
603f702b JS |
3639 | wxRichTextAttr attr1(style); |
3640 | attr1.GetTextBoxAttr().Reset(); | |
3641 | return GetBuffer().SetDefaultStyle(attr1); | |
24777478 JS |
3642 | } |
3643 | ||
3644 | const wxRichTextAttr& wxRichTextCtrl::GetDefaultStyleEx() const | |
27e20452 | 3645 | { |
61399247 | 3646 | return GetBuffer().GetDefaultStyle(); |
27e20452 JS |
3647 | } |
3648 | ||
3966a9f4 | 3649 | bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style) |
24777478 JS |
3650 | { |
3651 | wxRichTextAttr attr; | |
603f702b | 3652 | if (GetFocusObject()->GetStyle(position, attr)) |
24777478 JS |
3653 | { |
3654 | style = attr; | |
3655 | return true; | |
3656 | } | |
3657 | else | |
3658 | return false; | |
3659 | } | |
3660 | ||
3661 | bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style) | |
5d7836c4 | 3662 | { |
603f702b JS |
3663 | return GetFocusObject()->GetStyle(position, style); |
3664 | } | |
3665 | ||
3666 | bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container) | |
3667 | { | |
3668 | wxRichTextAttr attr; | |
3669 | if (container->GetStyle(position, attr)) | |
3670 | { | |
3671 | style = attr; | |
3672 | return true; | |
3673 | } | |
3674 | else | |
3675 | return false; | |
5d7836c4 JS |
3676 | } |
3677 | ||
a7ed48a5 | 3678 | // get the common set of styles for the range |
44cc96a8 | 3679 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttr& style) |
24777478 JS |
3680 | { |
3681 | wxRichTextAttr attr; | |
603f702b | 3682 | if (GetFocusObject()->GetStyleForRange(range.ToInternal(), attr)) |
24777478 JS |
3683 | { |
3684 | style = attr; | |
3685 | return true; | |
3686 | } | |
3687 | else | |
3688 | return false; | |
3689 | } | |
3690 | ||
3691 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style) | |
a7ed48a5 | 3692 | { |
603f702b JS |
3693 | return GetFocusObject()->GetStyleForRange(range.ToInternal(), style); |
3694 | } | |
3695 | ||
3696 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container) | |
3697 | { | |
3698 | return container->GetStyleForRange(range.ToInternal(), style); | |
a7ed48a5 JS |
3699 | } |
3700 | ||
fe5aa22c | 3701 | /// Get the content (uncombined) attributes for this position. |
24777478 | 3702 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style) |
fe5aa22c | 3703 | { |
603f702b JS |
3704 | return GetFocusObject()->GetUncombinedStyle(position, style); |
3705 | } | |
3706 | ||
3707 | /// Get the content (uncombined) attributes for this position. | |
3708 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container) | |
3709 | { | |
3710 | return container->GetUncombinedStyle(position, style); | |
fe5aa22c JS |
3711 | } |
3712 | ||
590a0f8b JS |
3713 | bool wxRichTextCtrl::SetProperties(const wxRichTextRange& range, const wxRichTextProperties& properties, int flags) |
3714 | { | |
3715 | return GetFocusObject()->SetProperties(range.ToInternal(), properties, flags); | |
3716 | } | |
3717 | ||
5d7836c4 JS |
3718 | /// Set font, and also the buffer attributes |
3719 | bool wxRichTextCtrl::SetFont(const wxFont& font) | |
3720 | { | |
24777478 | 3721 | wxControl::SetFont(font); |
5d7836c4 | 3722 | |
24777478 | 3723 | wxRichTextAttr attr = GetBuffer().GetAttributes(); |
5d7836c4 JS |
3724 | attr.SetFont(font); |
3725 | GetBuffer().SetBasicStyle(attr); | |
fe5aa22c | 3726 | |
fe5aa22c JS |
3727 | GetBuffer().Invalidate(wxRICHTEXT_ALL); |
3728 | Refresh(false); | |
5d7836c4 JS |
3729 | |
3730 | return true; | |
3731 | } | |
3732 | ||
4d551ad5 JS |
3733 | /// Transform logical to physical |
3734 | wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const | |
5d7836c4 JS |
3735 | { |
3736 | wxPoint pt; | |
3737 | CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y); | |
3738 | ||
3739 | return pt; | |
3740 | } | |
3741 | ||
3742 | /// Transform physical to logical | |
4d551ad5 | 3743 | wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const |
5d7836c4 JS |
3744 | { |
3745 | wxPoint pt; | |
3746 | CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y); | |
3747 | ||
3748 | return pt; | |
3749 | } | |
3750 | ||
3751 | /// Position the caret | |
603f702b | 3752 | void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox* container) |
5d7836c4 | 3753 | { |
c59f6793 JS |
3754 | if (!GetCaret()) |
3755 | return; | |
3756 | ||
3757 | //wxLogDebug(wxT("PositionCaret")); | |
3758 | ||
5d7836c4 | 3759 | wxRect caretRect; |
603f702b | 3760 | if (GetCaretPositionForIndex(GetCaretPosition(), caretRect, container)) |
5d7836c4 | 3761 | { |
0b289133 | 3762 | #if !wxRICHTEXT_USE_OWN_CARET |
4ba36292 | 3763 | caretRect = GetScaledRect(caretRect); |
0b289133 | 3764 | #endif |
4ba36292 JS |
3765 | int topMargin = (int) (0.5 + GetScale()*GetBuffer().GetTopMargin()); |
3766 | int bottomMargin = (int) (0.5 + GetScale()*GetBuffer().GetBottomMargin()); | |
7e4fe193 JS |
3767 | wxPoint newPt = caretRect.GetPosition(); |
3768 | wxSize newSz = caretRect.GetSize(); | |
3769 | wxPoint pt = GetPhysicalPoint(newPt); | |
3770 | if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz) | |
c59f6793 | 3771 | { |
1c13f06e JS |
3772 | GetCaret()->Hide(); |
3773 | if (GetCaret()->GetSize() != newSz) | |
3774 | GetCaret()->SetSize(newSz); | |
e9f10004 | 3775 | |
423b9ba5 | 3776 | // Adjust size so the caret size and position doesn't appear in the margins |
4ba36292 | 3777 | if (((pt.y + newSz.y) <= topMargin) || (pt.y >= (GetClientSize().y - bottomMargin))) |
603f702b JS |
3778 | { |
3779 | pt.x = -200; | |
e9f10004 | 3780 | pt.y = -200; |
603f702b | 3781 | } |
4ba36292 | 3782 | else if (pt.y < topMargin && (pt.y + newSz.y) > topMargin) |
423b9ba5 | 3783 | { |
4ba36292 | 3784 | newSz.y -= (topMargin - pt.y); |
423b9ba5 JS |
3785 | if (newSz.y > 0) |
3786 | { | |
4ba36292 | 3787 | pt.y = topMargin; |
423b9ba5 JS |
3788 | GetCaret()->SetSize(newSz); |
3789 | } | |
3790 | } | |
4ba36292 | 3791 | else if (pt.y < (GetClientSize().y - bottomMargin) && (pt.y + newSz.y) > (GetClientSize().y - bottomMargin)) |
423b9ba5 | 3792 | { |
4ba36292 | 3793 | newSz.y = GetClientSize().y - bottomMargin - pt.y; |
423b9ba5 JS |
3794 | GetCaret()->SetSize(newSz); |
3795 | } | |
e9f10004 | 3796 | |
c59f6793 | 3797 | GetCaret()->Move(pt); |
1c13f06e | 3798 | GetCaret()->Show(); |
c59f6793 | 3799 | } |
5d7836c4 JS |
3800 | } |
3801 | } | |
3802 | ||
3803 | /// Get the caret height and position for the given character position | |
603f702b | 3804 | bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect, wxRichTextParagraphLayoutBox* container) |
5d7836c4 JS |
3805 | { |
3806 | wxClientDC dc(this); | |
5d7836c4 | 3807 | PrepareDC(dc); |
4ba36292 JS |
3808 | dc.SetUserScale(GetScale(), GetScale()); |
3809 | dc.SetFont(GetFont()); | |
5d7836c4 JS |
3810 | |
3811 | wxPoint pt; | |
3812 | int height = 0; | |
4fe83b93 | 3813 | |
603f702b JS |
3814 | if (!container) |
3815 | container = GetFocusObject(); | |
5d7836c4 | 3816 | |
8db2e3ef JS |
3817 | wxRichTextDrawingContext context(& GetBuffer()); |
3818 | if (container->FindPosition(dc, context, position, pt, & height, m_caretAtLineStart)) | |
5d7836c4 | 3819 | { |
88bee7e6 JS |
3820 | // Caret height can't be zero |
3821 | if (height == 0) | |
3822 | height = dc.GetCharHeight(); | |
3823 | ||
5d7836c4 JS |
3824 | rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height)); |
3825 | return true; | |
3826 | } | |
41e155b4 WS |
3827 | |
3828 | return false; | |
5d7836c4 JS |
3829 | } |
3830 | ||
3831 | /// Gets the line for the visible caret position. If the caret is | |
3832 | /// shown at the very end of the line, it means the next character is actually | |
3833 | /// on the following line. So let's get the line we're expecting to find | |
3834 | /// if this is the case. | |
3835 | wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const | |
3836 | { | |
603f702b JS |
3837 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(caretPosition, true); |
3838 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(caretPosition, true); | |
5d7836c4 JS |
3839 | if (line) |
3840 | { | |
1e967276 JS |
3841 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
3842 | if (caretPosition == lineRange.GetStart()-1 && | |
3843 | (para->GetRange().GetStart() != lineRange.GetStart())) | |
5d7836c4 JS |
3844 | { |
3845 | if (!m_caretAtLineStart) | |
603f702b | 3846 | line = GetFocusObject()->GetLineAtPosition(caretPosition-1, true); |
5d7836c4 JS |
3847 | } |
3848 | } | |
3849 | return line; | |
3850 | } | |
3851 | ||
3852 | ||
3853 | /// Move the caret to the given character position | |
603f702b | 3854 | bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart, wxRichTextParagraphLayoutBox* container) |
5d7836c4 | 3855 | { |
603f702b | 3856 | if (GetBuffer().IsDirty()) |
2f36e8dc | 3857 | LayoutContent(); |
5d7836c4 | 3858 | |
603f702b JS |
3859 | if (!container) |
3860 | container = GetFocusObject(); | |
3861 | ||
3862 | if (pos <= container->GetOwnRange().GetEnd()) | |
5d7836c4 JS |
3863 | { |
3864 | SetCaretPosition(pos, showAtLineStart); | |
3865 | ||
603f702b | 3866 | PositionCaret(container); |
5d7836c4 JS |
3867 | |
3868 | return true; | |
3869 | } | |
3870 | else | |
3871 | return false; | |
3872 | } | |
3873 | ||
3874 | /// Layout the buffer: which we must do before certain operations, such as | |
3875 | /// setting the caret position. | |
2f36e8dc | 3876 | bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect) |
5d7836c4 | 3877 | { |
603f702b | 3878 | if (GetBuffer().IsDirty() || onlyVisibleRect) |
1e967276 | 3879 | { |
4ba36292 | 3880 | wxRect availableSpace(GetUnscaledSize(GetClientSize())); |
1e967276 JS |
3881 | if (availableSpace.width == 0) |
3882 | availableSpace.width = 10; | |
3883 | if (availableSpace.height == 0) | |
3884 | availableSpace.height = 10; | |
4d551ad5 JS |
3885 | |
3886 | int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT; | |
3887 | if (onlyVisibleRect) | |
3888 | { | |
3889 | flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT; | |
4ba36292 | 3890 | availableSpace.SetPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0)))); |
4d551ad5 | 3891 | } |
41e155b4 | 3892 | |
1e967276 | 3893 | wxClientDC dc(this); |
41e155b4 | 3894 | |
1e967276 | 3895 | PrepareDC(dc); |
4ba36292 | 3896 | dc.SetFont(GetFont()); |
29ec9f9b | 3897 | dc.SetUserScale(GetScale(), GetScale()); |
41e155b4 | 3898 | |
8db2e3ef | 3899 | wxRichTextDrawingContext context(& GetBuffer()); |
f7667b84 | 3900 | GetBuffer().Defragment(context); |
1e967276 | 3901 | GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation |
8db2e3ef | 3902 | GetBuffer().Layout(dc, context, availableSpace, availableSpace, flags); |
603f702b | 3903 | GetBuffer().Invalidate(wxRICHTEXT_NONE); |
41e155b4 | 3904 | |
29ec9f9b JS |
3905 | dc.SetUserScale(1.0, 1.0); |
3906 | ||
5a0259e6 | 3907 | if (!IsFrozen() && !onlyVisibleRect) |
1e967276 JS |
3908 | SetupScrollbars(); |
3909 | } | |
5d7836c4 JS |
3910 | |
3911 | return true; | |
3912 | } | |
3913 | ||
75936ec6 | 3914 | /// Is all of the selection, or the current caret position, bold? |
3966a9f4 | 3915 | bool wxRichTextCtrl::IsSelectionBold() |
5d7836c4 JS |
3916 | { |
3917 | if (HasSelection()) | |
3918 | { | |
24777478 | 3919 | wxRichTextAttr attr; |
a7ed48a5 | 3920 | wxRichTextRange range = GetSelectionRange(); |
5d7836c4 | 3921 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
7d76fbd5 | 3922 | attr.SetFontWeight(wxFONTWEIGHT_BOLD); |
7fe8059f | 3923 | |
5d7836c4 JS |
3924 | return HasCharacterAttributes(range, attr); |
3925 | } | |
3926 | else | |
3927 | { | |
3928 | // If no selection, then we need to combine current style with default style | |
3929 | // to see what the effect would be if we started typing. | |
24777478 | 3930 | wxRichTextAttr attr; |
5d7836c4 | 3931 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
ab14c7aa JS |
3932 | |
3933 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
3934 | if (GetStyle(pos, attr)) | |
5d7836c4 | 3935 | { |
ab14c7aa JS |
3936 | if (IsDefaultStyleShowing()) |
3937 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
7d76fbd5 | 3938 | return attr.GetFontWeight() == wxFONTWEIGHT_BOLD; |
5d7836c4 JS |
3939 | } |
3940 | } | |
3941 | return false; | |
3942 | } | |
3943 | ||
75936ec6 | 3944 | /// Is all of the selection, or the current caret position, italics? |
3966a9f4 | 3945 | bool wxRichTextCtrl::IsSelectionItalics() |
5d7836c4 JS |
3946 | { |
3947 | if (HasSelection()) | |
3948 | { | |
a7ed48a5 | 3949 | wxRichTextRange range = GetSelectionRange(); |
24777478 | 3950 | wxRichTextAttr attr; |
5d7836c4 | 3951 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); |
7d76fbd5 | 3952 | attr.SetFontStyle(wxFONTSTYLE_ITALIC); |
7fe8059f | 3953 | |
5d7836c4 JS |
3954 | return HasCharacterAttributes(range, attr); |
3955 | } | |
3956 | else | |
3957 | { | |
3958 | // If no selection, then we need to combine current style with default style | |
3959 | // to see what the effect would be if we started typing. | |
24777478 | 3960 | wxRichTextAttr attr; |
5d7836c4 | 3961 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); |
ab14c7aa JS |
3962 | |
3963 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
3964 | if (GetStyle(pos, attr)) | |
5d7836c4 | 3965 | { |
ab14c7aa JS |
3966 | if (IsDefaultStyleShowing()) |
3967 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
7d76fbd5 | 3968 | return attr.GetFontStyle() == wxFONTSTYLE_ITALIC; |
5d7836c4 JS |
3969 | } |
3970 | } | |
3971 | return false; | |
3972 | } | |
3973 | ||
75936ec6 | 3974 | /// Is all of the selection, or the current caret position, underlined? |
3966a9f4 | 3975 | bool wxRichTextCtrl::IsSelectionUnderlined() |
5d7836c4 JS |
3976 | { |
3977 | if (HasSelection()) | |
3978 | { | |
a7ed48a5 | 3979 | wxRichTextRange range = GetSelectionRange(); |
24777478 | 3980 | wxRichTextAttr attr; |
5d7836c4 JS |
3981 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); |
3982 | attr.SetFontUnderlined(true); | |
7fe8059f | 3983 | |
5d7836c4 JS |
3984 | return HasCharacterAttributes(range, attr); |
3985 | } | |
3986 | else | |
3987 | { | |
3988 | // If no selection, then we need to combine current style with default style | |
3989 | // to see what the effect would be if we started typing. | |
24777478 | 3990 | wxRichTextAttr attr; |
5d7836c4 | 3991 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); |
ab14c7aa | 3992 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); |
fe5aa22c | 3993 | |
ab14c7aa | 3994 | if (GetStyle(pos, attr)) |
5d7836c4 | 3995 | { |
ab14c7aa JS |
3996 | if (IsDefaultStyleShowing()) |
3997 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
5d7836c4 JS |
3998 | return attr.GetFontUnderlined(); |
3999 | } | |
4000 | } | |
4001 | return false; | |
4002 | } | |
4003 | ||
75936ec6 JS |
4004 | /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)? |
4005 | bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag) | |
4006 | { | |
4007 | wxRichTextAttr attr; | |
4008 | attr.SetFlags(wxTEXT_ATTR_EFFECTS); | |
4009 | attr.SetTextEffectFlags(flag); | |
4010 | attr.SetTextEffects(flag); | |
4011 | ||
4012 | if (HasSelection()) | |
4013 | { | |
4014 | return HasCharacterAttributes(GetSelectionRange(), attr); | |
4015 | } | |
4016 | else | |
4017 | { | |
4018 | // If no selection, then we need to combine current style with default style | |
4019 | // to see what the effect would be if we started typing. | |
4020 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
4021 | if (GetStyle(pos, attr)) | |
4022 | { | |
4023 | if (IsDefaultStyleShowing()) | |
4024 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
dabf7342 | 4025 | return (attr.GetTextEffectFlags() & flag) != 0; |
75936ec6 JS |
4026 | } |
4027 | } | |
4028 | return false; | |
4029 | } | |
4030 | ||
5d7836c4 JS |
4031 | /// Apply bold to the selection |
4032 | bool wxRichTextCtrl::ApplyBoldToSelection() | |
4033 | { | |
24777478 | 4034 | wxRichTextAttr attr; |
5d7836c4 | 4035 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
7d76fbd5 | 4036 | attr.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL : wxFONTWEIGHT_BOLD); |
5d7836c4 JS |
4037 | |
4038 | if (HasSelection()) | |
3e3a754f | 4039 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); |
5d7836c4 | 4040 | else |
063dbace JS |
4041 | { |
4042 | wxRichTextAttr current = GetDefaultStyleEx(); | |
4043 | current.Apply(attr); | |
4044 | SetAndShowDefaultStyle(current); | |
4045 | } | |
5d7836c4 JS |
4046 | return true; |
4047 | } | |
4048 | ||
4049 | /// Apply italic to the selection | |
4050 | bool wxRichTextCtrl::ApplyItalicToSelection() | |
4051 | { | |
24777478 | 4052 | wxRichTextAttr attr; |
5d7836c4 | 4053 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); |
7d76fbd5 | 4054 | attr.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL : wxFONTSTYLE_ITALIC); |
5d7836c4 JS |
4055 | |
4056 | if (HasSelection()) | |
3e3a754f | 4057 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); |
5d7836c4 | 4058 | else |
063dbace JS |
4059 | { |
4060 | wxRichTextAttr current = GetDefaultStyleEx(); | |
4061 | current.Apply(attr); | |
4062 | SetAndShowDefaultStyle(current); | |
4063 | } | |
5d7836c4 JS |
4064 | return true; |
4065 | } | |
4066 | ||
4067 | /// Apply underline to the selection | |
4068 | bool wxRichTextCtrl::ApplyUnderlineToSelection() | |
4069 | { | |
24777478 | 4070 | wxRichTextAttr attr; |
5d7836c4 | 4071 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); |
4d551ad5 | 4072 | attr.SetFontUnderlined(!IsSelectionUnderlined()); |
5d7836c4 | 4073 | |
75936ec6 JS |
4074 | if (HasSelection()) |
4075 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); | |
4076 | else | |
4077 | { | |
4078 | wxRichTextAttr current = GetDefaultStyleEx(); | |
4079 | current.Apply(attr); | |
4080 | SetAndShowDefaultStyle(current); | |
4081 | } | |
4082 | return true; | |
4083 | } | |
4084 | ||
4085 | /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection | |
4086 | bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags) | |
4087 | { | |
4088 | wxRichTextAttr attr; | |
4089 | attr.SetFlags(wxTEXT_ATTR_EFFECTS); | |
4090 | attr.SetTextEffectFlags(flags); | |
4091 | if (!DoesSelectionHaveTextEffectFlag(flags)) | |
4092 | attr.SetTextEffects(flags); | |
4093 | else | |
4094 | attr.SetTextEffects(attr.GetTextEffectFlags() & ~flags); | |
4095 | ||
5d7836c4 | 4096 | if (HasSelection()) |
3e3a754f | 4097 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); |
5d7836c4 | 4098 | else |
063dbace JS |
4099 | { |
4100 | wxRichTextAttr current = GetDefaultStyleEx(); | |
4101 | current.Apply(attr); | |
4102 | SetAndShowDefaultStyle(current); | |
4103 | } | |
5d7836c4 JS |
4104 | return true; |
4105 | } | |
4106 | ||
4107 | /// Is all of the selection aligned according to the specified flag? | |
3966a9f4 | 4108 | bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment) |
5d7836c4 | 4109 | { |
0ca07313 | 4110 | wxRichTextRange range; |
5d7836c4 | 4111 | if (HasSelection()) |
a7ed48a5 | 4112 | range = GetSelectionRange(); |
5d7836c4 | 4113 | else |
a7ed48a5 | 4114 | range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2); |
0ca07313 | 4115 | |
24777478 | 4116 | wxRichTextAttr attr; |
0ca07313 JS |
4117 | attr.SetAlignment(alignment); |
4118 | ||
4119 | return HasParagraphAttributes(range, attr); | |
5d7836c4 JS |
4120 | } |
4121 | ||
4122 | /// Apply alignment to the selection | |
4123 | bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment) | |
4124 | { | |
24777478 | 4125 | wxRichTextAttr attr; |
5d7836c4 JS |
4126 | attr.SetAlignment(alignment); |
4127 | if (HasSelection()) | |
4128 | return SetStyle(GetSelectionRange(), attr); | |
4129 | else | |
4130 | { | |
603f702b | 4131 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1); |
5d7836c4 | 4132 | if (para) |
3e3a754f | 4133 | return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY); |
5d7836c4 JS |
4134 | } |
4135 | return true; | |
4136 | } | |
4137 | ||
ab14c7aa | 4138 | /// Apply a named style to the selection |
1807a1f3 | 4139 | bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def) |
ab14c7aa JS |
4140 | { |
4141 | // Flags are defined within each definition, so only certain | |
4142 | // attributes are applied. | |
24777478 | 4143 | wxRichTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle()); |
1807a1f3 | 4144 | |
f2ab7bf3 | 4145 | int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_RESET; |
ab14c7aa | 4146 | |
345c78ca | 4147 | if (wxDynamicCast(def, wxRichTextListStyleDefinition)) |
38f833b1 JS |
4148 | { |
4149 | flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY; | |
41a85215 | 4150 | |
38f833b1 | 4151 | wxRichTextRange range; |
41a85215 | 4152 | |
38f833b1 JS |
4153 | if (HasSelection()) |
4154 | range = GetSelectionRange(); | |
4155 | else | |
4156 | { | |
41a85215 | 4157 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); |
38f833b1 JS |
4158 | range = wxRichTextRange(pos, pos+1); |
4159 | } | |
41a85215 WS |
4160 | |
4161 | return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags); | |
4162 | } | |
4163 | ||
edae63b0 JS |
4164 | bool isPara = false; |
4165 | ||
ab14c7aa | 4166 | // Make sure the attr has the style name |
345c78ca | 4167 | if (wxDynamicCast(def, wxRichTextParagraphStyleDefinition)) |
3e3a754f | 4168 | { |
edae63b0 | 4169 | isPara = true; |
ab14c7aa | 4170 | attr.SetParagraphStyleName(def->GetName()); |
1807a1f3 | 4171 | |
3e3a754f | 4172 | // If applying a paragraph style, we only want the paragraph nodes to adopt these |
38f833b1 | 4173 | // attributes, and not the leaf nodes. This will allow the content (e.g. text) |
3e3a754f JS |
4174 | // to change its style independently. |
4175 | flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY; | |
4176 | } | |
345c78ca | 4177 | else if (wxDynamicCast(def, wxRichTextCharacterStyleDefinition)) |
ab14c7aa | 4178 | attr.SetCharacterStyleName(def->GetName()); |
345c78ca | 4179 | else if (wxDynamicCast(def, wxRichTextBoxStyleDefinition)) |
b9433288 | 4180 | attr.GetTextBoxAttr().SetBoxStyleName(def->GetName()); |
ab14c7aa | 4181 | |
345c78ca | 4182 | if (wxDynamicCast(def, wxRichTextBoxStyleDefinition)) |
b9433288 JS |
4183 | { |
4184 | if (GetFocusObject() && (GetFocusObject() != & GetBuffer())) | |
4185 | { | |
4186 | SetStyle(GetFocusObject(), attr); | |
4187 | return true; | |
4188 | } | |
4189 | else | |
4190 | return false; | |
4191 | } | |
4192 | else if (HasSelection()) | |
1807a1f3 | 4193 | return SetStyleEx(GetSelectionRange(), attr, flags); |
ab14c7aa | 4194 | else |
1807a1f3 | 4195 | { |
24777478 JS |
4196 | wxRichTextAttr current = GetDefaultStyleEx(); |
4197 | wxRichTextAttr defaultStyle(attr); | |
edae63b0 JS |
4198 | if (isPara) |
4199 | { | |
4200 | // Don't apply extra character styles since they are already implied | |
4201 | // in the paragraph style | |
4202 | defaultStyle.SetFlags(defaultStyle.GetFlags() & ~wxTEXT_ATTR_CHARACTER); | |
4203 | } | |
4204 | current.Apply(defaultStyle); | |
063dbace | 4205 | SetAndShowDefaultStyle(current); |
edae63b0 JS |
4206 | |
4207 | // If it's a paragraph style, we want to apply the style to the | |
4208 | // current paragraph even if we didn't select any text. | |
4209 | if (isPara) | |
4210 | { | |
4211 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
603f702b | 4212 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos); |
edae63b0 JS |
4213 | if (para) |
4214 | { | |
4215 | return SetStyleEx(para->GetRange().FromInternal(), attr, flags); | |
4216 | } | |
4217 | } | |
1807a1f3 JS |
4218 | return true; |
4219 | } | |
ab14c7aa JS |
4220 | } |
4221 | ||
fe5aa22c JS |
4222 | /// Apply the style sheet to the buffer, for example if the styles have changed. |
4223 | bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet) | |
4224 | { | |
4225 | if (!styleSheet) | |
4226 | styleSheet = GetBuffer().GetStyleSheet(); | |
4227 | if (!styleSheet) | |
4228 | return false; | |
4229 | ||
4230 | if (GetBuffer().ApplyStyleSheet(styleSheet)) | |
4231 | { | |
4232 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
4233 | Refresh(false); | |
4234 | return true; | |
4235 | } | |
4236 | else | |
4237 | return false; | |
4238 | } | |
4239 | ||
5d7836c4 JS |
4240 | /// Sets the default style to the style under the cursor |
4241 | bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() | |
4242 | { | |
24777478 | 4243 | wxRichTextAttr attr; |
5d7836c4 JS |
4244 | attr.SetFlags(wxTEXT_ATTR_CHARACTER); |
4245 | ||
ab14c7aa JS |
4246 | // If at the start of a paragraph, use the next position. |
4247 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
4248 | ||
1199fbd4 JS |
4249 | wxRichTextObject* obj = GetFocusObject()->GetLeafObjectAtPosition(pos); |
4250 | if (obj && obj->IsTopLevel()) | |
4251 | { | |
4252 | // Don't use the attributes of a top-level object, since they might apply | |
4253 | // to content of the object, e.g. background colour. | |
4254 | SetDefaultStyle(wxRichTextAttr()); | |
4255 | return true; | |
4256 | } | |
4257 | else if (GetUncombinedStyle(pos, attr)) | |
5d7836c4 JS |
4258 | { |
4259 | SetDefaultStyle(attr); | |
4260 | return true; | |
4261 | } | |
41e155b4 WS |
4262 | |
4263 | return false; | |
5d7836c4 JS |
4264 | } |
4265 | ||
4d551ad5 JS |
4266 | /// Returns the first visible position in the current view |
4267 | long wxRichTextCtrl::GetFirstVisiblePosition() const | |
4268 | { | |
4ba36292 | 4269 | wxRichTextLine* line = GetFocusObject()->GetLineAtYPosition(GetUnscaledPoint(GetLogicalPoint(wxPoint(0, 0))).y); |
4d551ad5 JS |
4270 | if (line) |
4271 | return line->GetAbsoluteRange().GetStart(); | |
4272 | else | |
4273 | return 0; | |
4274 | } | |
4275 | ||
ea160b2e JS |
4276 | /// Get the first visible point in the window |
4277 | wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const | |
4278 | { | |
4279 | int ppuX, ppuY; | |
4280 | int startXUnits, startYUnits; | |
4281 | ||
4282 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
4283 | GetViewStart(& startXUnits, & startYUnits); | |
4284 | ||
4285 | return wxPoint(startXUnits * ppuX, startYUnits * ppuY); | |
4286 | } | |
4287 | ||
ab14c7aa JS |
4288 | /// The adjusted caret position is the character position adjusted to take |
4289 | /// into account whether we're at the start of a paragraph, in which case | |
4290 | /// style information should be taken from the next position, not current one. | |
4291 | long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const | |
4292 | { | |
603f702b | 4293 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(caretPos+1); |
ab14c7aa JS |
4294 | |
4295 | if (para && (caretPos+1 == para->GetRange().GetStart())) | |
4296 | caretPos ++; | |
4297 | return caretPos; | |
4298 | } | |
4299 | ||
96c9f0f6 JS |
4300 | /// Get/set the selection range in character positions. -1, -1 means no selection. |
4301 | /// The range is in API convention, i.e. a single character selection is denoted | |
4302 | /// by (n, n+1) | |
4303 | wxRichTextRange wxRichTextCtrl::GetSelectionRange() const | |
4304 | { | |
4305 | wxRichTextRange range = GetInternalSelectionRange(); | |
4306 | if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1)) | |
4307 | range.SetEnd(range.GetEnd() + 1); | |
4308 | return range; | |
4309 | } | |
4310 | ||
4311 | void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range) | |
4312 | { | |
76d7fc29 | 4313 | SetSelection(range.GetStart(), range.GetEnd()); |
96c9f0f6 JS |
4314 | } |
4315 | ||
38f833b1 JS |
4316 | /// Set list style |
4317 | bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel) | |
4318 | { | |
603f702b | 4319 | return GetFocusObject()->SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel); |
38f833b1 JS |
4320 | } |
4321 | ||
4322 | bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel) | |
4323 | { | |
603f702b | 4324 | return GetFocusObject()->SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel); |
38f833b1 JS |
4325 | } |
4326 | ||
4327 | /// Clear list for given range | |
4328 | bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags) | |
4329 | { | |
603f702b | 4330 | return GetFocusObject()->ClearListStyle(range.ToInternal(), flags); |
38f833b1 JS |
4331 | } |
4332 | ||
4333 | /// Number/renumber any list elements in the given range | |
4334 | bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel) | |
4335 | { | |
603f702b | 4336 | return GetFocusObject()->NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel); |
38f833b1 JS |
4337 | } |
4338 | ||
4339 | bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel) | |
4340 | { | |
603f702b | 4341 | return GetFocusObject()->NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel); |
38f833b1 JS |
4342 | } |
4343 | ||
4344 | /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1 | |
4345 | bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel) | |
4346 | { | |
603f702b | 4347 | return GetFocusObject()->PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel); |
38f833b1 JS |
4348 | } |
4349 | ||
4350 | bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel) | |
4351 | { | |
603f702b | 4352 | return GetFocusObject()->PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel); |
38f833b1 JS |
4353 | } |
4354 | ||
4ce3ebd3 JS |
4355 | // Given a character position at which there is a list style, find the range |
4356 | // encompassing the same list style by looking backwards and forwards. | |
4357 | wxRichTextRange wxRichTextCtrl::FindRangeForList(long pos, bool& isNumberedList) | |
4358 | { | |
4359 | wxRichTextParagraphLayoutBox* focusObject = GetFocusObject(); | |
4360 | wxRichTextRange range = wxRichTextRange(-1, -1); | |
4361 | wxRichTextParagraph* para = focusObject->GetParagraphAtPosition(pos); | |
4362 | if (!para || !para->GetAttributes().HasListStyleName()) | |
4363 | return range; | |
4364 | else | |
4365 | { | |
4366 | wxString listStyle = para->GetAttributes().GetListStyleName(); | |
4367 | range = para->GetRange(); | |
4368 | ||
4369 | isNumberedList = para->GetAttributes().HasBulletNumber(); | |
4370 | ||
4371 | // Search back | |
4372 | wxRichTextObjectList::compatibility_iterator initialNode = focusObject->GetChildren().Find(para); | |
4373 | if (initialNode) | |
4374 | { | |
4375 | wxRichTextObjectList::compatibility_iterator startNode = initialNode->GetPrevious(); | |
4376 | while (startNode) | |
4377 | { | |
4378 | wxRichTextParagraph* p = wxDynamicCast(startNode->GetData(), wxRichTextParagraph); | |
4379 | if (p) | |
4380 | { | |
4381 | if (!p->GetAttributes().HasListStyleName() || p->GetAttributes().GetListStyleName() != listStyle) | |
4382 | break; | |
4383 | else | |
4384 | range.SetStart(p->GetRange().GetStart()); | |
4385 | } | |
4386 | ||
4387 | startNode = startNode->GetPrevious(); | |
4388 | } | |
4389 | ||
4390 | // Search forward | |
4391 | wxRichTextObjectList::compatibility_iterator endNode = initialNode->GetNext(); | |
4392 | while (endNode) | |
4393 | { | |
4394 | wxRichTextParagraph* p = wxDynamicCast(endNode->GetData(), wxRichTextParagraph); | |
4395 | if (p) | |
4396 | { | |
4397 | if (!p->GetAttributes().HasListStyleName() || p->GetAttributes().GetListStyleName() != listStyle) | |
4398 | break; | |
4399 | else | |
4400 | range.SetEnd(p->GetRange().GetEnd()); | |
4401 | } | |
4402 | ||
4403 | endNode = endNode->GetNext(); | |
4404 | } | |
4405 | } | |
4406 | } | |
4407 | return range; | |
4408 | } | |
4409 | ||
12cc29c5 JS |
4410 | /// Deletes the content in the given range |
4411 | bool wxRichTextCtrl::Delete(const wxRichTextRange& range) | |
4412 | { | |
603f702b | 4413 | return GetFocusObject()->DeleteRangeWithUndo(range.ToInternal(), this, & GetBuffer()); |
12cc29c5 JS |
4414 | } |
4415 | ||
dadd4f55 JS |
4416 | const wxArrayString& wxRichTextCtrl::GetAvailableFontNames() |
4417 | { | |
4418 | if (sm_availableFontNames.GetCount() == 0) | |
4419 | { | |
4420 | sm_availableFontNames = wxFontEnumerator::GetFacenames(); | |
4421 | sm_availableFontNames.Sort(); | |
4422 | } | |
4423 | return sm_availableFontNames; | |
4424 | } | |
4425 | ||
4426 | void wxRichTextCtrl::ClearAvailableFontNames() | |
4427 | { | |
4428 | sm_availableFontNames.Clear(); | |
4429 | } | |
4430 | ||
c0a5145a | 4431 | void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) |
52170c5b JS |
4432 | { |
4433 | //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged")); | |
4434 | ||
4435 | wxTextAttrEx basicStyle = GetBasicStyle(); | |
4436 | basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); | |
4437 | SetBasicStyle(basicStyle); | |
4438 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
4439 | ||
4440 | Refresh(); | |
4441 | } | |
4442 | ||
7051fa41 | 4443 | // Refresh the area affected by a selection change |
603f702b JS |
4444 | bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection& oldSelection, const wxRichTextSelection& newSelection) |
4445 | { | |
4446 | // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that | |
4447 | // the selection contains whole containers rather than just text, so refresh everything | |
4448 | // for now as it would be hard to compute the rectangle bounding all selections. | |
4449 | // TODO: improve on this. | |
4450 | if ((oldSelection.IsValid() && (oldSelection.GetContainer() != GetFocusObject() || oldSelection.GetCount() > 1)) || | |
4451 | (newSelection.IsValid() && (newSelection.GetContainer() != GetFocusObject() || newSelection.GetCount() > 1))) | |
4452 | { | |
4453 | Refresh(false); | |
4454 | return true; | |
4455 | } | |
4fe83b93 | 4456 | |
603f702b JS |
4457 | wxRichTextRange oldRange, newRange; |
4458 | if (oldSelection.IsValid()) | |
4459 | oldRange = oldSelection.GetRange(); | |
4460 | else | |
4461 | oldRange = wxRICHTEXT_NO_SELECTION; | |
4462 | if (newSelection.IsValid()) | |
4463 | newRange = newSelection.GetRange(); | |
4464 | else | |
4465 | newRange = wxRICHTEXT_NO_SELECTION; | |
4fe83b93 | 4466 | |
7051fa41 JS |
4467 | // Calculate the refresh rectangle - just the affected lines |
4468 | long firstPos, lastPos; | |
603f702b | 4469 | if (oldRange.GetStart() == -2 && newRange.GetStart() != -2) |
7051fa41 | 4470 | { |
603f702b JS |
4471 | firstPos = newRange.GetStart(); |
4472 | lastPos = newRange.GetEnd(); | |
7051fa41 | 4473 | } |
603f702b | 4474 | else if (oldRange.GetStart() != -2 && newRange.GetStart() == -2) |
7051fa41 | 4475 | { |
603f702b JS |
4476 | firstPos = oldRange.GetStart(); |
4477 | lastPos = oldRange.GetEnd(); | |
7051fa41 | 4478 | } |
603f702b | 4479 | else if (oldRange.GetStart() == -2 && newRange.GetStart() == -2) |
7051fa41 JS |
4480 | { |
4481 | return false; | |
4482 | } | |
4483 | else | |
4484 | { | |
603f702b JS |
4485 | firstPos = wxMin(oldRange.GetStart(), newRange.GetStart()); |
4486 | lastPos = wxMax(oldRange.GetEnd(), newRange.GetEnd()); | |
7051fa41 JS |
4487 | } |
4488 | ||
603f702b JS |
4489 | wxRichTextLine* firstLine = GetFocusObject()->GetLineAtPosition(firstPos); |
4490 | wxRichTextLine* lastLine = GetFocusObject()->GetLineAtPosition(lastPos); | |
7051fa41 JS |
4491 | |
4492 | if (firstLine && lastLine) | |
4493 | { | |
4494 | wxSize clientSize = GetClientSize(); | |
4ba36292 JS |
4495 | wxPoint pt1 = GetPhysicalPoint(GetScaledPoint(firstLine->GetAbsolutePosition())); |
4496 | wxPoint pt2 = GetPhysicalPoint(GetScaledPoint(lastLine->GetAbsolutePosition())) + wxPoint(0, (int) (0.5 + lastLine->GetSize().y * GetScale())); | |
7051fa41 JS |
4497 | |
4498 | pt1.x = 0; | |
4499 | pt1.y = wxMax(0, pt1.y); | |
4500 | pt2.x = 0; | |
4501 | pt2.y = wxMin(clientSize.y, pt2.y); | |
4502 | ||
4503 | wxRect rect(pt1, wxSize(clientSize.x, pt2.y - pt1.y)); | |
4504 | RefreshRect(rect, false); | |
4505 | } | |
4506 | else | |
4507 | Refresh(false); | |
4508 | ||
4509 | return true; | |
4510 | } | |
4511 | ||
603f702b JS |
4512 | // margins functions |
4513 | bool wxRichTextCtrl::DoSetMargins(const wxPoint& pt) | |
4514 | { | |
4515 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt.x, wxTEXT_ATTR_UNITS_PIXELS); | |
4516 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt.x, wxTEXT_ATTR_UNITS_PIXELS); | |
4517 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt.y, wxTEXT_ATTR_UNITS_PIXELS); | |
4518 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt.y, wxTEXT_ATTR_UNITS_PIXELS); | |
4fe83b93 | 4519 | |
603f702b JS |
4520 | return true; |
4521 | } | |
4522 | ||
4523 | wxPoint wxRichTextCtrl::DoGetMargins() const | |
4524 | { | |
4525 | return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(), | |
4526 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue()); | |
4527 | } | |
4528 | ||
4529 | bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox* obj, bool setCaretPosition) | |
4530 | { | |
4531 | if (obj && !obj->AcceptsFocus()) | |
4532 | return false; | |
4533 | ||
4534 | wxRichTextParagraphLayoutBox* oldContainer = GetFocusObject(); | |
4535 | bool changingContainer = (m_focusObject != obj); | |
4536 | ||
eadb039e JS |
4537 | if (changingContainer && HasSelection()) |
4538 | SelectNone(); | |
4539 | ||
603f702b | 4540 | m_focusObject = obj; |
4fe83b93 | 4541 | |
603f702b JS |
4542 | if (!obj) |
4543 | m_focusObject = & m_buffer; | |
4544 | ||
4545 | if (setCaretPosition && changingContainer) | |
4546 | { | |
4547 | m_selection.Reset(); | |
4548 | m_selectionAnchor = -2; | |
4549 | m_selectionAnchorObject = NULL; | |
4550 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
4551 | ||
4552 | long pos = -1; | |
4fe83b93 | 4553 | |
603f702b JS |
4554 | m_caretAtLineStart = false; |
4555 | MoveCaret(pos, m_caretAtLineStart); | |
4556 | SetDefaultStyleToCursorStyle(); | |
4557 | ||
4558 | wxRichTextEvent cmdEvent( | |
ce7fe42e | 4559 | wxEVT_RICHTEXT_FOCUS_OBJECT_CHANGED, |
603f702b JS |
4560 | GetId()); |
4561 | cmdEvent.SetEventObject(this); | |
4562 | cmdEvent.SetPosition(m_caretPosition+1); | |
4563 | cmdEvent.SetOldContainer(oldContainer); | |
4564 | cmdEvent.SetContainer(m_focusObject); | |
4565 | ||
4566 | GetEventHandler()->ProcessEvent(cmdEvent); | |
4567 | } | |
4568 | return true; | |
4569 | } | |
4570 | ||
0c0e063e JS |
4571 | #if wxUSE_DRAG_AND_DROP |
4572 | void wxRichTextCtrl::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def, wxDataObject* DataObj) | |
4573 | { | |
4574 | m_preDrag = false; | |
4575 | ||
4576 | if ((def != wxDragCopy) && (def != wxDragMove)) | |
4577 | { | |
4578 | return; | |
4579 | } | |
4580 | ||
4581 | if (!GetSelection().IsValid()) | |
4582 | { | |
4583 | return; | |
4584 | } | |
4585 | ||
4586 | wxRichTextParagraphLayoutBox* originContainer = GetSelection().GetContainer(); | |
4587 | wxRichTextParagraphLayoutBox* destContainer = GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one | |
4588 | ||
0c0e063e JS |
4589 | wxRichTextBuffer* richTextBuffer = ((wxRichTextBufferDataObject*)DataObj)->GetRichTextBuffer(); |
4590 | if (richTextBuffer) | |
4591 | { | |
4592 | long position = GetCaretPosition(); | |
4593 | wxRichTextRange selectionrange = GetInternalSelectionRange(); | |
4594 | if (selectionrange.Contains(position) && (def == wxDragMove)) | |
4595 | { | |
4596 | // It doesn't make sense to move onto itself | |
4597 | return; | |
4598 | } | |
4599 | ||
4600 | // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection | |
4601 | // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway | |
4602 | bool DeleteAfter = (def == wxDragMove) && (position > selectionrange.GetEnd()); | |
4603 | if ((def == wxDragMove) && !DeleteAfter) | |
4604 | { | |
4605 | // We can't use e.g. DeleteSelectedContent() as it uses the focus container | |
4606 | originContainer->DeleteRangeWithUndo(selectionrange, this, &GetBuffer()); | |
4607 | } | |
4608 | ||
4e63bfb9 | 4609 | destContainer->InsertParagraphsWithUndo(&GetBuffer(), position+1, *richTextBuffer, this, 0); |
0c0e063e JS |
4610 | ShowPosition(position + richTextBuffer->GetOwnRange().GetEnd()); |
4611 | ||
4612 | delete richTextBuffer; | |
4613 | ||
4614 | if (DeleteAfter) | |
4615 | { | |
4616 | // We can't use e.g. DeleteSelectedContent() as it uses the focus container | |
4617 | originContainer->DeleteRangeWithUndo(selectionrange, this, &GetBuffer()); | |
4618 | } | |
4619 | ||
0c0e063e JS |
4620 | SelectNone(); |
4621 | Refresh(); | |
4622 | } | |
4623 | } | |
4624 | #endif // wxUSE_DRAG_AND_DROP | |
4625 | ||
4626 | ||
4627 | #if wxUSE_DRAG_AND_DROP | |
4628 | bool wxRichTextDropSource::GiveFeedback(wxDragResult WXUNUSED(effect)) | |
4629 | { | |
4630 | wxCHECK_MSG(m_rtc, false, wxT("NULL m_rtc")); | |
4631 | ||
4632 | long position = 0; | |
4633 | int hit = 0; | |
4634 | wxRichTextObject* hitObj = NULL; | |
4ba36292 | 4635 | wxRichTextParagraphLayoutBox* container = m_rtc->FindContainerAtPoint(m_rtc->GetUnscaledPoint(m_rtc->ScreenToClient(wxGetMousePosition())), position, hit, hitObj); |
0c0e063e JS |
4636 | |
4637 | if (!(hit & wxRICHTEXT_HITTEST_NONE) && container && container->AcceptsFocus()) | |
4638 | { | |
4639 | m_rtc->StoreFocusObject(container); | |
4640 | m_rtc->SetCaretPositionAfterClick(container, position, hit); | |
4641 | } | |
4642 | ||
4643 | return false; // so that the base-class sets a cursor | |
4644 | } | |
4645 | #endif // wxUSE_DRAG_AND_DROP | |
4646 | ||
cc2aecde JS |
4647 | bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox& WXUNUSED(container), const wxRichTextRange& WXUNUSED(range)) const |
4648 | { | |
4649 | return true; | |
4650 | } | |
4651 | ||
4652 | bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox& WXUNUSED(container), long WXUNUSED(pos)) const | |
4653 | { | |
4654 | return true; | |
4655 | } | |
4656 | ||
12f5219f JS |
4657 | void wxRichTextCtrl::EnableVerticalScrollbar(bool enable) |
4658 | { | |
4659 | m_verticalScrollbarEnabled = enable; | |
4660 | SetupScrollbars(); | |
4661 | } | |
4662 | ||
32423dd8 JS |
4663 | void wxRichTextCtrl::SetFontScale(double fontScale, bool refresh) |
4664 | { | |
4665 | GetBuffer().SetFontScale(fontScale); | |
4666 | if (refresh) | |
4667 | { | |
4668 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
4669 | Refresh(); | |
4670 | } | |
4671 | } | |
4672 | ||
4673 | void wxRichTextCtrl::SetDimensionScale(double dimScale, bool refresh) | |
4674 | { | |
4675 | GetBuffer().SetDimensionScale(dimScale); | |
4676 | if (refresh) | |
4677 | { | |
4678 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
4679 | Refresh(); | |
4680 | } | |
4681 | } | |
4682 | ||
4ba36292 JS |
4683 | // Sets an overall scale factor for displaying and editing the content. |
4684 | void wxRichTextCtrl::SetScale(double scale, bool refresh) | |
4685 | { | |
4686 | m_scale = scale; | |
4687 | if (refresh) | |
4688 | { | |
4689 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
4690 | Refresh(); | |
4691 | } | |
4692 | } | |
4693 | ||
4694 | // Get an unscaled point | |
4695 | wxPoint wxRichTextCtrl::GetUnscaledPoint(const wxPoint& pt) const | |
4696 | { | |
4697 | if (GetScale() == 1.0) | |
4698 | return pt; | |
4699 | else | |
4700 | return wxPoint((int) (0.5 + double(pt.x) / GetScale()), (int) (0.5 + double(pt.y) / GetScale())); | |
4701 | } | |
4702 | ||
4703 | // Get a scaled point | |
4704 | wxPoint wxRichTextCtrl::GetScaledPoint(const wxPoint& pt) const | |
4705 | { | |
4706 | if (GetScale() == 1.0) | |
4707 | return pt; | |
4708 | else | |
4709 | return wxPoint((int) (0.5 + double(pt.x) * GetScale()), (int) (0.5 + double(pt.y) * GetScale())); | |
4710 | } | |
4711 | ||
4712 | // Get an unscaled size | |
4713 | wxSize wxRichTextCtrl::GetUnscaledSize(const wxSize& sz) const | |
4714 | { | |
4715 | if (GetScale() == 1.0) | |
4716 | return sz; | |
4717 | else | |
4718 | return wxSize((int) (0.5 + double(sz.x) / GetScale()), (int) (0.5 + double(sz.y) / GetScale())); | |
4719 | } | |
4720 | ||
4721 | // Get a scaled size | |
4722 | wxSize wxRichTextCtrl::GetScaledSize(const wxSize& sz) const | |
4723 | { | |
4724 | if (GetScale() == 1.0) | |
4725 | return sz; | |
4726 | else | |
4727 | return wxSize((int) (0.5 + double(sz.x) * GetScale()), (int) (0.5 + double(sz.y) * GetScale())); | |
4728 | } | |
4729 | ||
4730 | // Get an unscaled rect | |
4731 | wxRect wxRichTextCtrl::GetUnscaledRect(const wxRect& rect) const | |
4732 | { | |
4733 | if (GetScale() == 1.0) | |
4734 | return rect; | |
4735 | else | |
4736 | return wxRect((int) (0.5 + double(rect.x) / GetScale()), (int) (0.5 + double(rect.y) / GetScale()), | |
4737 | (int) (0.5 + double(rect.width) / GetScale()), (int) (0.5 + double(rect.height) / GetScale())); | |
4738 | } | |
4739 | ||
4740 | // Get a scaled rect | |
4741 | wxRect wxRichTextCtrl::GetScaledRect(const wxRect& rect) const | |
4742 | { | |
4743 | if (GetScale() == 1.0) | |
4744 | return rect; | |
4745 | else | |
4746 | return wxRect((int) (0.5 + double(rect.x) * GetScale()), (int) (0.5 + double(rect.y) * GetScale()), | |
4747 | (int) (0.5 + double(rect.width) * GetScale()), (int) (0.5 + double(rect.height) * GetScale())); | |
4748 | } | |
4749 | ||
1c13f06e JS |
4750 | #if wxRICHTEXT_USE_OWN_CARET |
4751 | ||
4752 | // ---------------------------------------------------------------------------- | |
4753 | // initialization and destruction | |
4754 | // ---------------------------------------------------------------------------- | |
4755 | ||
4756 | void wxRichTextCaret::Init() | |
4757 | { | |
4758 | m_hasFocus = true; | |
cd45ed36 | 4759 | m_refreshEnabled = true; |
1c13f06e JS |
4760 | |
4761 | m_xOld = | |
4762 | m_yOld = -1; | |
4763 | m_richTextCtrl = NULL; | |
4764 | m_needsUpdate = false; | |
a188ac29 | 4765 | m_flashOn = true; |
1c13f06e JS |
4766 | } |
4767 | ||
4768 | wxRichTextCaret::~wxRichTextCaret() | |
4769 | { | |
a188ac29 JS |
4770 | if (m_timer.IsRunning()) |
4771 | m_timer.Stop(); | |
1c13f06e JS |
4772 | } |
4773 | ||
4774 | // ---------------------------------------------------------------------------- | |
4775 | // showing/hiding/moving the caret (base class interface) | |
4776 | // ---------------------------------------------------------------------------- | |
4777 | ||
4778 | void wxRichTextCaret::DoShow() | |
4779 | { | |
a188ac29 | 4780 | m_flashOn = true; |
ab52bac8 | 4781 | |
eaaf10f6 | 4782 | if (!m_timer.IsRunning() && GetBlinkTime() > 0) |
a188ac29 JS |
4783 | m_timer.Start(GetBlinkTime()); |
4784 | ||
1c13f06e JS |
4785 | Refresh(); |
4786 | } | |
4787 | ||
4788 | void wxRichTextCaret::DoHide() | |
4789 | { | |
a188ac29 JS |
4790 | if (m_timer.IsRunning()) |
4791 | m_timer.Stop(); | |
4792 | ||
1c13f06e JS |
4793 | Refresh(); |
4794 | } | |
4795 | ||
4796 | void wxRichTextCaret::DoMove() | |
4797 | { | |
4798 | if (IsVisible()) | |
4799 | { | |
4800 | Refresh(); | |
4801 | ||
4802 | if (m_xOld != -1 && m_yOld != -1) | |
4803 | { | |
cd45ed36 | 4804 | if (m_richTextCtrl && m_refreshEnabled) |
1c13f06e | 4805 | { |
0b289133 JS |
4806 | wxRect rect(wxPoint(m_xOld, m_yOld), GetSize()); |
4807 | wxRect scaledRect = m_richTextCtrl->GetScaledRect(rect); | |
4808 | m_richTextCtrl->RefreshRect(scaledRect, false); | |
1c13f06e JS |
4809 | } |
4810 | } | |
4811 | } | |
4812 | ||
4813 | m_xOld = m_x; | |
4814 | m_yOld = m_y; | |
4815 | } | |
4816 | ||
4817 | void wxRichTextCaret::DoSize() | |
4818 | { | |
4819 | int countVisible = m_countVisible; | |
4820 | if (countVisible > 0) | |
4821 | { | |
4822 | m_countVisible = 0; | |
4823 | DoHide(); | |
4824 | } | |
4825 | ||
4826 | if (countVisible > 0) | |
4827 | { | |
4828 | m_countVisible = countVisible; | |
4829 | DoShow(); | |
4830 | } | |
4831 | } | |
4832 | ||
4833 | // ---------------------------------------------------------------------------- | |
4834 | // handling the focus | |
4835 | // ---------------------------------------------------------------------------- | |
4836 | ||
4837 | void wxRichTextCaret::OnSetFocus() | |
4838 | { | |
4839 | m_hasFocus = true; | |
4840 | ||
4841 | if ( IsVisible() ) | |
4842 | Refresh(); | |
4843 | } | |
4844 | ||
4845 | void wxRichTextCaret::OnKillFocus() | |
4846 | { | |
4847 | m_hasFocus = false; | |
4848 | } | |
4849 | ||
4850 | // ---------------------------------------------------------------------------- | |
4851 | // drawing the caret | |
4852 | // ---------------------------------------------------------------------------- | |
4853 | ||
4854 | void wxRichTextCaret::Refresh() | |
4855 | { | |
cd45ed36 | 4856 | if (m_richTextCtrl && m_refreshEnabled) |
1c13f06e JS |
4857 | { |
4858 | wxRect rect(GetPosition(), GetSize()); | |
0b289133 JS |
4859 | wxRect rectScaled = m_richTextCtrl->GetScaledRect(rect); |
4860 | m_richTextCtrl->RefreshRect(rectScaled, false); | |
1c13f06e JS |
4861 | } |
4862 | } | |
4863 | ||
4864 | void wxRichTextCaret::DoDraw(wxDC *dc) | |
4865 | { | |
d8d44113 JS |
4866 | wxBrush brush(m_caretBrush); |
4867 | wxPen pen(m_caretPen); | |
4868 | if (m_richTextCtrl && m_richTextCtrl->GetBasicStyle().HasTextColour()) | |
4869 | { | |
4870 | brush = wxBrush(m_richTextCtrl->GetBasicStyle().GetTextColour()); | |
4871 | pen = wxPen(m_richTextCtrl->GetBasicStyle().GetTextColour()); | |
4872 | } | |
4873 | dc->SetBrush((m_hasFocus ? brush : *wxTRANSPARENT_BRUSH)); | |
4874 | dc->SetPen(pen); | |
1c13f06e | 4875 | |
1c13f06e JS |
4876 | wxPoint pt(m_x, m_y); |
4877 | ||
4878 | if (m_richTextCtrl) | |
4879 | { | |
4880 | pt = m_richTextCtrl->GetLogicalPoint(pt); | |
4881 | } | |
a188ac29 JS |
4882 | if (IsVisible() && m_flashOn) |
4883 | dc->DrawRectangle(pt.x, pt.y, m_width, m_height); | |
4884 | } | |
4885 | ||
4886 | void wxRichTextCaret::Notify() | |
4887 | { | |
4888 | m_flashOn = !m_flashOn; | |
4889 | Refresh(); | |
4890 | } | |
4891 | ||
4892 | void wxRichTextCaretTimer::Notify() | |
4893 | { | |
4894 | m_caret->Notify(); | |
1c13f06e JS |
4895 | } |
4896 | #endif | |
4897 | // wxRICHTEXT_USE_OWN_CARET | |
4898 | ||
603f702b JS |
4899 | // Add an item |
4900 | bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString& label, wxRichTextObject* obj) | |
4901 | { | |
4902 | if (GetCount() < 3) | |
4903 | { | |
4904 | m_labels.Add(label); | |
4905 | m_objects.Add(obj); | |
4906 | return true; | |
4907 | } | |
4908 | else | |
4909 | return false; | |
4910 | } | |
4911 | ||
4912 | // Returns number of menu items were added. | |
4913 | int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu* menu, int startCmd) const | |
4914 | { | |
4915 | wxMenuItem* item = menu->FindItem(startCmd); | |
9b794421 JS |
4916 | // If none of the standard properties identifiers are in the menu, add them if necessary. |
4917 | // If no items to add, just set the text to something generic | |
4918 | if (GetCount() == 0) | |
603f702b | 4919 | { |
9b794421 | 4920 | if (item) |
603f702b JS |
4921 | { |
4922 | menu->SetLabel(startCmd, _("&Properties")); | |
4fe83b93 | 4923 | |
603f702b JS |
4924 | // Delete the others if necessary |
4925 | int i; | |
4926 | for (i = startCmd+1; i < startCmd+3; i++) | |
4927 | { | |
4928 | if (menu->FindItem(i)) | |
4929 | { | |
4930 | menu->Delete(i); | |
4931 | } | |
4932 | } | |
4933 | } | |
9b794421 JS |
4934 | } |
4935 | else | |
4936 | { | |
4937 | int i; | |
4938 | int pos = -1; | |
4939 | // Find the position of the first properties item | |
4940 | for (i = 0; i < (int) menu->GetMenuItemCount(); i++) | |
603f702b | 4941 | { |
9b794421 JS |
4942 | wxMenuItem* item = menu->FindItemByPosition(i); |
4943 | if (item && item->GetId() == startCmd) | |
603f702b | 4944 | { |
9b794421 JS |
4945 | pos = i; |
4946 | break; | |
603f702b | 4947 | } |
9b794421 | 4948 | } |
4fe83b93 | 4949 | |
9b794421 JS |
4950 | if (pos != -1) |
4951 | { | |
4952 | int insertBefore = pos+1; | |
4953 | for (i = startCmd; i < startCmd+GetCount(); i++) | |
603f702b | 4954 | { |
9b794421 | 4955 | if (menu->FindItem(i)) |
603f702b | 4956 | { |
9b794421 JS |
4957 | menu->SetLabel(i, m_labels[i - startCmd]); |
4958 | } | |
4959 | else | |
4960 | { | |
4961 | if (insertBefore >= (int) menu->GetMenuItemCount()) | |
4962 | menu->Append(i, m_labels[i - startCmd]); | |
603f702b | 4963 | else |
9b794421 | 4964 | menu->Insert(insertBefore, i, m_labels[i - startCmd]); |
603f702b | 4965 | } |
9b794421 JS |
4966 | insertBefore ++; |
4967 | } | |
4fe83b93 | 4968 | |
9b794421 JS |
4969 | // Delete any old items still left on the menu |
4970 | for (i = startCmd + GetCount(); i < startCmd+3; i++) | |
4971 | { | |
4972 | if (menu->FindItem(i)) | |
603f702b | 4973 | { |
9b794421 | 4974 | menu->Delete(i); |
603f702b JS |
4975 | } |
4976 | } | |
4977 | } | |
9b794421 JS |
4978 | else |
4979 | { | |
4980 | // No existing property identifiers were found, so append to the end of the menu. | |
4981 | menu->AppendSeparator(); | |
4982 | for (i = startCmd; i < startCmd+GetCount(); i++) | |
4983 | { | |
4984 | menu->Append(i, m_labels[i - startCmd]); | |
4985 | } | |
4986 | } | |
603f702b JS |
4987 | } |
4988 | ||
4989 | return GetCount(); | |
4990 | } | |
4991 | ||
4992 | // Add appropriate menu items for the current container and clicked on object | |
4993 | // (and container's parent, if appropriate). | |
303f0be7 | 4994 | int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl* ctrl, wxRichTextObject* container, wxRichTextObject* obj) |
603f702b JS |
4995 | { |
4996 | Clear(); | |
303f0be7 | 4997 | if (obj && ctrl->CanEditProperties(obj)) |
590a0f8b | 4998 | AddItem(ctrl->GetPropertiesMenuLabel(obj), obj); |
603f702b | 4999 | |
590a0f8b JS |
5000 | if (container && container != obj && ctrl->CanEditProperties(container) && m_labels.Index(ctrl->GetPropertiesMenuLabel(container)) == wxNOT_FOUND) |
5001 | AddItem(ctrl->GetPropertiesMenuLabel(container), container); | |
603f702b | 5002 | |
590a0f8b JS |
5003 | if (container && container->GetParent() && ctrl->CanEditProperties(container->GetParent()) && m_labels.Index(ctrl->GetPropertiesMenuLabel(container->GetParent())) == wxNOT_FOUND) |
5004 | AddItem(ctrl->GetPropertiesMenuLabel(container->GetParent()), container->GetParent()); | |
603f702b JS |
5005 | |
5006 | return GetCount(); | |
5007 | } | |
5008 | ||
5d7836c4 JS |
5009 | #endif |
5010 | // wxUSE_RICHTEXT |