8000 Text box widget, take over of PR5375 by fariza · Pull Request #6988 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Text box widget, take over of PR5375 #6988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
enabled moving the cursor by clicking
  • Loading branch information
HastingsGreer authored and fariza committed Aug 29, 2016
commit 893962f4117280f2d08040b666c21abaf5a73586
2 changes: 1 addition & 1 deletion doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Added TextBox Widget
````````````````````

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


Active state of Selectors
Expand Down
36 changes: 29 additions & 7 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ def __init__(self, ax, label, initial='',
self.connect_event('key_press_event', self._keypress)
self.connect_event('resize_event', self._resize)
ax.set_navigate(False)
ax.set_axis_bgcolor(color)
ax.set_facecolor(color)
ax.set_xticks([])
ax.set_yticks([])
self.color = color
Expand Down Expand Up @@ -817,18 +817,14 @@ def begin_typing(self, x):
for key in self.params_to_disable:
self.reset_params[key] = rcParams[key]
rcParams[key] = []
#now, we have to figure out where the cursor goes.
#approximate it based on assuming all characters the same length
self.cursor_index = len(self.text)
self._rendercursor()

def stop_typing(self):
notifysubmit = False
# because _notify_submit_users might throw an error in the
# user's code, we only want to call it once we've already done
# our cleanup.
if self.capturekeystrokes:
#since the user is no longer typing,
#since the user is no longer typing,
#reactivate the standard command keys
for key in self.params_to_disable:
rcParams[key] = self.reset_params[key]
Expand All @@ -839,6 +835,31 @@ def stop_typing(self):
if notifysubmit:
self._notify_submit_observers()

def position_cursor(self, x):
#now, we have to figure out where the cursor goes.
#approximate it based on assuming all characters the same length
if len(self.text) == 0:
self.cursor_index = 0
else:
bb = self.text_disp.get_window_extent()

trans = self.ax.transData
inv = self.ax.transData.inverted()
bb = trans.transform(inv.transform(bb))

text_start = bb[0, 0]
text_end = bb[1, 0]

ratio = (x - text_start) / (text_end - text_start)

if ratio < 0:
ratio = 0
if ratio > 1:
ratio = 1

self.cursor_index = int(len(self.text) * ratio)

self._rendercursor()

def _click(self, event):
if self.ignore(event):
Expand All @@ -852,6 +873,7 @@ def _click(self, event):
event.canvas.grab_mouse(self.ax)
if not(self.capturekeystrokes):
self.begin_typing(event.x)
self.position_cursor(event.x)

def _resize(self, event):
self.stop_typing()
Expand All @@ -864,7 +886,7 @@ def _motion(self, event):
else:
c = self.color
if c != self._lastcolor:
self.ax.set_axis_bgcolor(c)
self.ax.set_facecolor(c)
self._lastcolor = c
if self.drawon:
self.ax.figure.canvas.draw()
Expand Down
0