blob: 1e0404aa49f56287462d48400ecc76c1eaa57262 [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,
Terry Jan Reedy491ef532019-03-10 20:18:40 -040083 background="#ffffd0", foreground="black",
84 relief=SOLID, borderwidth=1,
Tal Einat87e59ac2018-08-05 09:21:08 +030085 font=self.anchor_widget['font'])
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000086 self.label.pack()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000087
88 def checkhide_event(self, event=None):
Tal Einat87e59ac2018-08-05 09:21:08 +030089 """Handle CHECK_HIDE_EVENT: call hidetip or reschedule."""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000090 if not self.tipwindow:
Tal Einat87e59ac2018-08-05 09:21:08 +030091 # If the event was triggered by the same event that unbound
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000092 # this function, the function will be called nevertheless,
93 # so do nothing in this case.
Serhiy Storchaka213ce122017-06-27 07:02:32 +030094 return None
Tal Einat87e59ac2018-08-05 09:21:08 +030095
96 # Hide the call-tip if the insertion cursor moves outside of the
97 # parenthesis.
98 curline, curcol = map(int, self.anchor_widget.index("insert").split('.'))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000099 if curline < self.parenline or \
100 (curline == self.parenline and curcol <= self.parencol) or \
Tal Einat87e59ac2018-08-05 09:21:08 +0300101 self.anchor_widget.compare("insert", ">", MARK_RIGHT):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000102 self.hidetip()
Serhiy Storchaka213ce122017-06-27 07:02:32 +0300103 return "break"
Tal Einat87e59ac2018-08-05 09:21:08 +0300104
105 # Not hiding the call-tip.
106
107 self.position_window()
108 # Re-schedule this function to be called again in a short while.
109 if self.checkhide_after_id is not None:
110 self.anchor_widget.after_cancel(self.checkhide_after_id)
111 self.checkhide_after_id = \
112 self.anchor_widget.after(CHECKHIDE_TIME, self.checkhide_event)
113 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000114
115 def hide_event(self, event):
Tal Einat87e59ac2018-08-05 09:21:08 +0300116 """Handle HIDE_EVENT by calling hidetip."""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000117 if not self.tipwindow:
118 # See the explanation in checkhide_event.
Serhiy Storchaka213ce122017-06-27 07:02:32 +0300119 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000120 self.hidetip()
Serhiy Storchaka213ce122017-06-27 07:02:32 +0300121 return "break"
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000122
David Scherer7aced172000-08-15 01:13:23 +0000123 def hidetip(self):
Tal Einat87e59ac2018-08-05 09:21:08 +0300124 """Hide the call-tip."""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000125 if not self.tipwindow:
126 return
127
Tal Einat87e59ac2018-08-05 09:21:08 +0300128 try:
129 self.label.destroy()
130 except TclError:
131 pass
132 self.label = None
133
134 self.parenline = self.parencol = self.lastline = None
135 try:
136 self.anchor_widget.mark_unset(MARK_RIGHT)
137 except TclError:
138 pass
139
140 try:
141 self._unbind_events()
142 except (TclError, ValueError):
143 # ValueError may be raised by MultiCall
144 pass
145
146 super(CalltipWindow, self).hidetip()
147
148 def _bind_events(self):
149 """Bind event handlers."""
150 self.checkhideid = self.anchor_widget.bind(CHECKHIDE_EVENT,
151 self.checkhide_event)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000152 for seq in CHECKHIDE_SEQUENCES:
Tal Einat87e59ac2018-08-05 09:21:08 +0300153 self.anchor_widget.event_add(CHECKHIDE_EVENT, seq)
154 self.anchor_widget.after(CHECKHIDE_TIME, self.checkhide_event)
155 self.hideid = self.anchor_widget.bind(HIDE_EVENT,
156 self.hide_event)
157 for seq in HIDE_SEQUENCES:
158 self.anchor_widget.event_add(HIDE_EVENT, seq)
159
160 def _unbind_events(self):
161 """Unbind event handlers."""
162 for seq in CHECKHIDE_SEQUENCES:
163 self.anchor_widget.event_delete(CHECKHIDE_EVENT, seq)
164 self.anchor_widget.unbind(CHECKHIDE_EVENT, self.checkhideid)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000165 self.checkhideid = None
166 for seq in HIDE_SEQUENCES:
Tal Einat87e59ac2018-08-05 09:21:08 +0300167 self.anchor_widget.event_delete(HIDE_EVENT, seq)
168 self.anchor_widget.unbind(HIDE_EVENT, self.hideid)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000169 self.hideid = None
170
David Scherer7aced172000-08-15 01:13:23 +0000171
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -0400172def _calltip_window(parent): # htest #
Terry Jan Reedycd567362014-10-17 01:31:35 -0400173 from tkinter import Toplevel, Text, LEFT, BOTH
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -0400174
Terry Jan Reedycd567362014-10-17 01:31:35 -0400175 top = Toplevel(parent)
Tal Einat87e59ac2018-08-05 09:21:08 +0300176 top.title("Test call-tips")
Terry Jan Reedya7480322016-07-10 17:28:10 -0400177 x, y = map(int, parent.geometry().split('+')[1:])
Tal Einat87e59ac2018-08-05 09:21:08 +0300178 top.geometry("250x100+%d+%d" % (x + 175, y + 150))
Terry Jan Reedycd567362014-10-17 01:31:35 -0400179 text = Text(top)
180 text.pack(side=LEFT, fill=BOTH, expand=1)
181 text.insert("insert", "string.split")
182 top.update()
David Scherer7aced172000-08-15 01:13:23 +0000183
Tal Einat87e59ac2018-08-05 09:21:08 +0300184 calltip = CalltipWindow(text)
Terry Jan Reedycd567362014-10-17 01:31:35 -0400185 def calltip_show(event):
Tal Einat87e59ac2018-08-05 09:21:08 +0300186 calltip.showtip("(s='Hello world')", "insert", "end")
Terry Jan Reedycd567362014-10-17 01:31:35 -0400187 def calltip_hide(event):
188 calltip.hidetip()
189 text.event_add("<<calltip-show>>", "(")
190 text.event_add("<<calltip-hide>>", ")")
191 text.bind("<<calltip-show>>", calltip_show)
192 text.bind("<<calltip-hide>>", calltip_hide)
Tal Einat87e59ac2018-08-05 09:21:08 +0300193
Terry Jan Reedycd567362014-10-17 01:31:35 -0400194 text.focus_set()
David Scherer7aced172000-08-15 01:13:23 +0000195
Terry Jan Reedyee5ef302018-06-15 18:20:55 -0400196if __name__ == '__main__':
197 from unittest import main
Terry Jan Reedy06e20292018-06-19 23:00:35 -0400198 main('idlelib.idle_test.test_calltip_w', verbosity=2, exit=False)
Terry Jan Reedyee5ef302018-06-15 18:20:55 -0400199
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400200 from idlelib.idle_test.htest import run
201 run(_calltip_window)