blob: 8e29dabaea3d5a79850502376f7c80ccdfe68c4d [file] [log] [blame]
Kurt B. Kaisere54710b2002-12-12 19:15:39 +00001"""A CallTip window class for Tkinter/IDLE.
David Scherer7aced172000-08-15 01:13:23 +00002
Kurt B. Kaisere54710b2002-12-12 19:15:39 +00003After ToolTip.py, which uses ideas gleaned from PySol
4Used by the CallTips IDLE extension.
5
6"""
Georg Brandl14fc4272008-05-17 18:39:55 +00007from tkinter import *
David Scherer7aced172000-08-15 01:13:23 +00008
Kurt B. Kaiser3b148ca2005-11-22 02:17:10 +00009HIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-hide>>"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000010HIDE_SEQUENCES = ("<Key-Escape>", "<FocusOut>")
11CHECKHIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-checkhide>>"
12CHECKHIDE_SEQUENCES = ("<KeyRelease>", "<ButtonRelease>")
13CHECKHIDE_TIME = 100 # miliseconds
14
15MARK_RIGHT = "calltipwindowregion_right"
16
David Scherer7aced172000-08-15 01:13:23 +000017class CallTip:
18
19 def __init__(self, widget):
20 self.widget = widget
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000021 self.tipwindow = self.label = None
22 self.parenline = self.parencol = None
23 self.lastline = None
24 self.hideid = self.checkhideid = None
Martin v. Löwisee381a02012-03-13 14:18:36 -070025 self.checkhide_after_id = None
David Scherer7aced172000-08-15 01:13:23 +000026
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000027 def position_window(self):
28 """Check if needs to reposition the window, and if so - do it."""
29 curline = int(self.widget.index("insert").split('.')[0])
30 if curline == self.lastline:
31 return
32 self.lastline = curline
33 self.widget.see("insert")
34 if curline == self.parenline:
35 box = self.widget.bbox("%d.%d" % (self.parenline,
36 self.parencol))
37 else:
38 box = self.widget.bbox("%d.0" % curline)
39 if not box:
40 box = list(self.widget.bbox("insert"))
41 # align to left of window
42 box[0] = 0
43 box[2] = 0
44 x = box[0] + self.widget.winfo_rootx() + 2
45 y = box[1] + box[3] + self.widget.winfo_rooty()
46 self.tipwindow.wm_geometry("+%d+%d" % (x, y))
47
48 def showtip(self, text, parenleft, parenright):
49 """Show the calltip, bind events which will close it and reposition it.
50 """
Terry Jan Reedy0e3b0e32014-01-21 21:12:24 -050051 # Only called in CallTips, where lines are truncated
David Scherer7aced172000-08-15 01:13:23 +000052 self.text = text
53 if self.tipwindow or not self.text:
54 return
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000055
56 self.widget.mark_set(MARK_RIGHT, parenright)
57 self.parenline, self.parencol = map(
58 int, self.widget.index(parenleft).split("."))
59
David Scherer7aced172000-08-15 01:13:23 +000060 self.tipwindow = tw = Toplevel(self.widget)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000061 self.position_window()
Kurt B. Kaiser3b148ca2005-11-22 02:17:10 +000062 # remove border on calltip window
David Scherer7aced172000-08-15 01:13:23 +000063 tw.wm_overrideredirect(1)
Tony Lownds8b1b8d62002-09-23 01:04:05 +000064 try:
65 # This command is only needed and available on Tk >= 8.4.0 for OSX
66 # Without it, call tips intrude on the typing process by grabbing
67 # the focus.
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000068 tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w,
Tony Lownds8b1b8d62002-09-23 01:04:05 +000069 "help", "noActivates")
70 except TclError:
71 pass
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000072 self.label = Label(tw, text=self.text, justify=LEFT,
73 background="#ffffe0", relief=SOLID, borderwidth=1,
74 font = self.widget['font'])
75 self.label.pack()
76
77 self.checkhideid = self.widget.bind(CHECKHIDE_VIRTUAL_EVENT_NAME,
78 self.checkhide_event)
79 for seq in CHECKHIDE_SEQUENCES:
80 self.widget.event_add(CHECKHIDE_VIRTUAL_EVENT_NAME, seq)
81 self.widget.after(CHECKHIDE_TIME, self.checkhide_event)
82 self.hideid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME,
83 self.hide_event)
84 for seq in HIDE_SEQUENCES:
85 self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq)
86
87 def checkhide_event(self, event=None):
88 if not self.tipwindow:
89 # If the event was triggered by the same event that unbinded
90 # this function, the function will be called nevertheless,
91 # so do nothing in this case.
92 return
93 curline, curcol = map(int, self.widget.index("insert").split('.'))
94 if curline < self.parenline or \
95 (curline == self.parenline and curcol <= self.parencol) or \
96 self.widget.compare("insert", ">", MARK_RIGHT):
97 self.hidetip()
98 else:
99 self.position_window()
Martin v. Löwisee381a02012-03-13 14:18:36 -0700100 if self.checkhide_after_id is not None:
101 self.widget.after_cancel(self.checkhide_after_id)
102 self.checkhide_after_id = \
103 self.widget.after(CHECKHIDE_TIME, self.checkhide_event)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000104
105 def hide_event(self, event):
106 if not self.tipwindow:
107 # See the explanation in checkhide_event.
108 return
109 self.hidetip()
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000110
David Scherer7aced172000-08-15 01:13:23 +0000111 def hidetip(self):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000112 if not self.tipwindow:
113 return
114
115 for seq in CHECKHIDE_SEQUENCES:
116 self.widget.event_delete(CHECKHIDE_VIRTUAL_EVENT_NAME, seq)
117 self.widget.unbind(CHECKHIDE_VIRTUAL_EVENT_NAME, self.checkhideid)
118 self.checkhideid = None
119 for seq in HIDE_SEQUENCES:
120 self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq)
121 self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideid)
122 self.hideid = None
123
124 self.label.destroy()
125 self.label = None
126 self.tipwindow.destroy()
David Scherer7aced172000-08-15 01:13:23 +0000127 self.tipwindow = None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000128
129 self.widget.mark_unset(MARK_RIGHT)
130 self.parenline = self.parencol = self.lastline = None
131
132 def is_active(self):
133 return bool(self.tipwindow)
134
David Scherer7aced172000-08-15 01:13:23 +0000135
136
137###############################
138#
139# Test Code
140#
141class container: # Conceptually an editor_window
142 def __init__(self):
143 root = Tk()
144 text = self.text = Text(root)
145 text.pack(side=LEFT, fill=BOTH, expand=1)
146 text.insert("insert", "string.split")
147 root.update()
148 self.calltip = CallTip(text)
149
150 text.event_add("<<calltip-show>>", "(")
151 text.event_add("<<calltip-hide>>", ")")
152 text.bind("<<calltip-show>>", self.calltip_show)
153 text.bind("<<calltip-hide>>", self.calltip_hide)
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000154
David Scherer7aced172000-08-15 01:13:23 +0000155 text.focus_set()
Kurt B. Kaisere54710b2002-12-12 19:15:39 +0000156 root.mainloop()
David Scherer7aced172000-08-15 01:13:23 +0000157
158 def calltip_show(self, event):
159 self.calltip.showtip("Hello world")
160
161 def calltip_hide(self, event):
162 self.calltip.hidetip()
163
164def main():
165 # Test code
166 c=container()
167
168if __name__=='__main__':
169 main()