blob: 7553dfefc55c0bd8a52a0525a752893bf390e020 [file] [log] [blame]
Tal Einat87e59ac2018-08-05 09:21:08 +03001"""A call-tip window class for Tkinter/IDLE.
David Scherer7aced172000-08-15 01:13:23 +00002
Tal Einat87e59ac2018-08-05 09:21:08 +03003After tooltip.py, which uses ideas gleaned from PySol.
4Used by calltip.py.
Kurt B. Kaisere54710b2002-12-12 19:15:39 +00005"""
Tal Einat87e59ac2018-08-05 09:21:08 +03006from tkinter import Label, LEFT, SOLID, TclError
David Scherer7aced172000-08-15 01:13:23 +00007
Tal Einat87e59ac2018-08-05 09:21:08 +03008from idlelib.tooltip import TooltipBase
9
10HIDE_EVENT = "<<calltipwindow-hide>>"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000011HIDE_SEQUENCES = ("<Key-Escape>", "<FocusOut>")
Tal Einat87e59ac2018-08-05 09:21:08 +030012CHECKHIDE_EVENT = "<<calltipwindow-checkhide>>"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000013CHECKHIDE_SEQUENCES = ("<KeyRelease>", "<ButtonRelease>")
Tal Einat87e59ac2018-08-05 09:21:08 +030014CHECKHIDE_TIME = 100 # milliseconds
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000015
16MARK_RIGHT = "calltipwindowregion_right"
17
David Scherer7aced172000-08-15 01:13:23 +000018
Tal Einat87e59ac2018-08-05 09:21:08 +030019class CalltipWindow(TooltipBase):
20 """A call-tip widget for tkinter text widgets."""
21
22 def __init__(self, text_widget):
23 """Create a call-tip; shown by showtip().
24
25 text_widget: a Text widget with code for which call-tips are desired
26 """
27 # Note: The Text widget will be accessible as self.anchor_widget
28 super(CalltipWindow, self).__init__(text_widget)
29
30 self.label = self.text = None
31 self.parenline = self.parencol = self.lastline = None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000032 self.hideid = self.checkhideid = None
Martin v. Löwisee381a02012-03-13 14:18:36 -070033 self.checkhide_after_id = None
David Scherer7aced172000-08-15 01:13:23 +000034
Tal Einat87e59ac2018-08-05 09:21:08 +030035 def get_position(self):
36 """Choose the position of the call-tip."""
37 curline = int(self.anchor_widget.index("insert").split('.')[0])
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000038 if curline == self.parenline:
Tal Einat87e59ac2018-08-05 09:21:08 +030039 anchor_index = (self.parenline, self.parencol)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000040 else:
Tal Einat87e59ac2018-08-05 09:21:08 +030041 anchor_index = (curline, 0)
42 box = self.anchor_widget.bbox("%d.%d" % anchor_index)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000043 if not box:
Tal Einat87e59ac2018-08-05 09:21:08 +030044 box = list(self.anchor_widget.bbox("insert"))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000045 # align to left of window
46 box[0] = 0
47 box[2] = 0
Tal Einat87e59ac2018-08-05 09:21:08 +030048 return box[0] + 2, box[1] + box[3]
49
50 def position_window(self):
51 "Reposition the window if needed."
52 curline = int(self.anchor_widget.index("insert").split('.')[0])
53 if curline == self.lastline:
54 return
55 self.lastline = curline
56 self.anchor_widget.see("insert")
57 super(CalltipWindow, self).position_window()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000058
59 def showtip(self, text, parenleft, parenright):
Tal Einat87e59ac2018-08-05 09:21:08 +030060 """Show the call-tip, bind events which will close it and reposition it.
61
62 text: the text to display in the call-tip
63 parenleft: index of the opening parenthesis in the text widget
64 parenright: index of the closing parenthesis in the text widget,
65 or the end of the line if there is no closing parenthesis
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000066 """
Terry Jan Reedy9af18362018-06-20 02:18:49 -040067 # Only called in calltip.Calltip, where lines are truncated
David Scherer7aced172000-08-15 01:13:23 +000068 self.text = text
69 if self.tipwindow or not self.text:
70 return
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000071
Tal Einat87e59ac2018-08-05 09:21:08 +030072 self.anchor_widget.mark_set(MARK_RIGHT, parenright)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000073 self.parenline, self.parencol = map(
Tal Einat87e59ac2018-08-05 09:21:08 +030074 int, self.anchor_widget.index(parenleft).split("."))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000075
Tal Einat87e59ac2018-08-05 09:21:08 +030076 super(CalltipWindow, self).showtip()
77
78 self._bind_events()
79
80 def showcontents(self):
81 """Create the call-tip widget."""
82 self.label = Label(self.tipwindow, text=self.text, justify=LEFT,
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000083 background="#ffffe0", relief=SOLID, borderwidth=1,
Tal Einat87e59ac2018-08-05 09:21:08 +030084 font=self.anchor_widget['font'])
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000085 self.label.pack()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000086
87 def checkhide_event(self, event=None):
Tal Einat87e59ac2018-08-05 09:21:08 +030088 """Handle CHECK_HIDE_EVENT: call hidetip or reschedule."""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000089 if not self.tipwindow:
Tal Einat87e59ac2018-08-05 09:21:08 +030090 # If the event was triggered by the same event that unbound
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000091 # this function, the function will be called nevertheless,
92 # so do nothing in this case.
Serhiy Storchaka213ce122017-06-27 07:02:32 +030093 return None
Tal Einat87e59ac2018-08-05 09:21:08 +030094
95 # Hide the call-tip if the insertion cursor moves outside of the
96 # parenthesis.
97 curline, curcol = map(int, self.anchor_widget.index("insert").split('.'))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000098 if curline < self.parenline or \
99 (curline == self.parenline and curcol <= self.parencol) or \
Tal Einat87e59ac2018-08-05 09:21:08 +0300100 self.anchor_widget.compare("insert", ">", MARK_RIGHT):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000101 self.hidetip()
Serhiy Storchaka213ce122017-06-27 07:02:32 +0300102 return "break"
Tal Einat87e59ac2018-08-05 09:21:08 +0300103
104 # Not hiding the call-tip.
105
106 self.position_window()
107 # Re-schedule this function to be called again in a short while.
108 if self.checkhide_after_id is not None:
109 self.anchor_widget.after_cancel(self.checkhide_after_id)
110 self.checkhide_after_id = \
111 self.anchor_widget.after(CHECKHIDE_TIME, self.checkhide_event)
112 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000113
114 def hide_event(self, event):
Tal Einat87e59ac2018-08-05 09:21:08 +0300115 """Handle HIDE_EVENT by calling hidetip."""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000116 if not self.tipwindow:
117 # See the explanation in checkhide_event.
Serhiy Storchaka213ce122017-06-27 07:02:32 +0300118 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000119 self.hidetip()
Serhiy Storchaka213ce122017-06-27 07:02:32 +0300120 return "break"
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000121
David Scherer7aced172000-08-15 01:13:23 +0000122 def hidetip(self):
Tal Einat87e59ac2018-08-05 09:21:08 +0300123 """Hide the call-tip."""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000124 if not self.tipwindow:
125 return
126
Tal Einat87e59ac2018-08-05 09:21:08 +0300127 try:
128 self.label.destroy()
129 except TclError:
130 pass
131 self.label = None
132
133 self.parenline = self.parencol = self.lastline = None
134 try:
135 self.anchor_widget.mark_unset(MARK_RIGHT)
136 except TclError:
137 pass
138
139 try:
140 self._unbind_events()
141 except (TclError, ValueError):
142 # ValueError may be raised by MultiCall
143 pass
144
145 super(CalltipWindow, self).hidetip()
146
147 def _bind_events(self):
148 """Bind event handlers."""
149 self.checkhideid = self.anchor_widget.bind(CHECKHIDE_EVENT,
150 self.checkhide_event)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000151 for seq in CHECKHIDE_SEQUENCES:
Tal Einat87e59ac2018-08-05 09:21:08 +0300152 self.anchor_widget.event_add(CHECKHIDE_EVENT, seq)
153 self.anchor_widget.after(CHECKHIDE_TIME, self.checkhide_event)
154 self.hideid = self.anchor_widget.bind(HIDE_EVENT,
155 self.hide_event)
156 for seq in HIDE_SEQUENCES:
157 self.anchor_widget.event_add(HIDE_EVENT, seq)
158
159 def _unbind_events(self):
160 """Unbind event handlers."""
161 for seq in CHECKHIDE_SEQUENCES:
162 self.anchor_widget.event_delete(CHECKHIDE_EVENT, seq)
163 self.anchor_widget.unbind(CHECKHIDE_EVENT, self.checkhideid)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000164 self.checkhideid = None
165 for seq in HIDE_SEQUENCES:
Tal Einat87e59ac2018-08-05 09:21:08 +0300166 self.anchor_widget.event_delete(HIDE_EVENT, seq)
167 self.anchor_widget.unbind(HIDE_EVENT, self.hideid)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000168 self.hideid = None
169
David Scherer7aced172000-08-15 01:13:23 +0000170
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -0400171def _calltip_window(parent): # htest #
Terry Jan Reedycd567362014-10-17 01:31:35 -0400172 from tkinter import Toplevel, Text, LEFT, BOTH
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -0400173
Terry Jan Reedycd567362014-10-17 01:31:35 -0400174 top = Toplevel(parent)
Tal Einat87e59ac2018-08-05 09:21:08 +0300175 top.title("Test call-tips")
Terry Jan Reedya7480322016-07-10 17:28:10 -0400176 x, y = map(int, parent.geometry().split('+')[1:])
Tal Einat87e59ac2018-08-05 09:21:08 +0300177 top.geometry("250x100+%d+%d" % (x + 175, y + 150))
Terry Jan Reedycd567362014-10-17 01:31:35 -0400178 text = Text(top)
179 text.pack(side=LEFT, fill=BOTH, expand=1)
180 text.insert("insert", "string.split")
181 top.update()
David Scherer7aced172000-08-15 01:13:23 +0000182
Tal Einat87e59ac2018-08-05 09:21:08 +0300183 calltip = CalltipWindow(text)
Terry Jan Reedycd567362014-10-17 01:31:35 -0400184 def calltip_show(event):
Tal Einat87e59ac2018-08-05 09:21:08 +0300185 calltip.showtip("(s='Hello world')", "insert", "end")
Terry Jan Reedycd567362014-10-17 01:31:35 -0400186 def calltip_hide(event):
187 calltip.hidetip()
188 text.event_add("<<calltip-show>>", "(")
189 text.event_add("<<calltip-hide>>", ")")
190 text.bind("<<calltip-show>>", calltip_show)
191 text.bind("<<calltip-hide>>", calltip_hide)
Tal Einat87e59ac2018-08-05 09:21:08 +0300192
Terry Jan Reedycd567362014-10-17 01:31:35 -0400193 text.focus_set()
David Scherer7aced172000-08-15 01:13:23 +0000194
Terry Jan Reedyee5ef302018-06-15 18:20:55 -0400195if __name__ == '__main__':
196 from unittest import main
Terry Jan Reedy06e20292018-06-19 23:00:35 -0400197 main('idlelib.idle_test.test_calltip_w', verbosity=2, exit=False)
Terry Jan Reedyee5ef302018-06-15 18:20:55 -0400198
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400199 from idlelib.idle_test.htest import run
200 run(_calltip_window)