Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 893962f

Browse files
HastingsGreerfariza
authored andcommitted
enabled moving the cursor by clicking
1 parent 7961826 commit 893962f

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

doc/users/whats_new.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ Added TextBox Widget
248248
````````````````````
249249

250250
Added a widget that allows text entry by reading key events when it is active.
251-
Text caret in text box is visible when it is active, can be moved using arrow keys but not mouse
251+
Text caret in text box is visible when it is active, can be moved using arrow keys and mouse
252252

253253

254254
Active state of Selectors

lib/matplotlib/widgets.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ def __init__(self, ax, label, initial='',
717717
self.connect_event('key_press_event', self._keypress)
718718
self.connect_event('resize_event', self._resize)
719719
ax.set_navigate(False)
720-
ax.set_axis_bgcolor(color)
720+
ax.set_facecolor(color)
721721
ax.set_xticks([])
722722
ax.set_yticks([])
723723
self.color = color
@@ -817,18 +817,14 @@ def begin_typing(self, x):
817817
for key in self.params_to_disable:
818818
self.reset_params[key] = rcParams[key]
819819
rcParams[key] = []
820-
#now, we have to figure out where the cursor goes.
821-
#approximate it based on assuming all characters the same length
822-
self.cursor_index = len(self.text)
823-
self._rendercursor()
824820

825821
def stop_typing(self):
826822
notifysubmit = False
827823
# because _notify_submit_users might throw an error in the
828824
# user's code, we only want to call it once we've already done
829825
# our cleanup.
830826
if self.capturekeystrokes:
831-
#since the user is no longer typing,
827+
#since the user is no longer typing,
832828
#reactivate the standard command keys
833829
for key in self.params_to_disable:
834830
rcParams[key] = self.reset_params[key]
@@ -839,6 +835,31 @@ def stop_typing(self):
839835
if notifysubmit:
840836
self._notify_submit_observers()
841837

838+
def position_cursor(self, x):
839+
#now, we have to figure out where the cursor goes.
840+
#approximate it based on assuming all characters the same length
841+
if len(self.text) == 0:
842+
self.cursor_index = 0
843+
else:
844+
bb = self.text_disp.get_window_extent()
845+
846+
trans = self.ax.transData
847+
inv = self.ax.transData.inverted()
848+
bb = trans.transform(inv.transform(bb))
849+
850+
text_start = bb[0, 0]
851+
text_end = bb[1, 0]
852+
853+
ratio = (x - text_start) / (text_end - text_start)
854+
855+
if ratio < 0:
856+
ratio = 0
857+
if ratio > 1:
858+
ratio = 1
859+
860+
self.cursor_index = int(len(self.text) * ratio)
861+
862+
self._rendercursor()
842863

843864
def _click(self, event):
844865
if self.ignore(event):
@@ -852,6 +873,7 @@ def _click(self, event):
852873
event.canvas.grab_mouse(self.ax)
853874
if not(self.capturekeystrokes):
854875
self.begin_typing(event.x)
876+
self.position_cursor(event.x)
855877

856878
def _resize(self, event):
857879
self.stop_typing()
@@ -864,7 +886,7 @@ def _motion(self, event):
864886
else:
865887
c = self.color
866888
if c != self._lastcolor:
867-
self.ax.set_axis_bgcolor(c)
889+
self.ax.set_facecolor(c)
868890
self._lastcolor = c
869891
if self.drawon:
870892
self.ax.figure.canvas.draw()

0 commit comments

Comments
 (0)