blob: a2431f8eff3022ba610511f4e15d653d4b13fbff [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 """
Kurt B. Kaisere54710b2002-12-12 19:15:39 +000051 # truncate overly long calltip
Kurt B. Kaisere72f05d2002-09-15 21:43:13 +000052 if len(text) >= 79:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000053 textlines = text.splitlines()
54 for i, line in enumerate(textlines):
55 if len(line) > 79:
56 textlines[i] = line[:75] + ' ...'
57 text = '\n'.join(textlines)
David Scherer7aced172000-08-15 01:13:23 +000058 self.text = text
59 if self.tipwindow or not self.text:
60 return
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000061
62 self.widget.mark_set(MARK_RIGHT, parenright)
63 self.parenline, self.parencol = map(
64 int, self.widget.index(parenleft).split("."))
65
David Scherer7aced172000-08-15 01:13:23 +000066 self.tipwindow = tw = Toplevel(self.widget)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000067 self.position_window()
Kurt B. Kaiser3b148ca2005-11-22 02:17:10 +000068 # remove border on calltip window
David Scherer7aced172000-08-15 01:13:23 +000069 tw.wm_overrideredirect(1)
Tony Lownds8b1b8d62002-09-23 01:04:05 +000070 try:
71 # This command is only needed and available on Tk >= 8.4.0 for OSX
72 # Without it, call tips intrude on the typing process by grabbing
73 # the focus.
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000074 tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w,
Tony Lownds8b1b8d62002-09-23 01:04:05 +000075 "help", "noActivates")
76 except TclError:
77 pass
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000078 self.label = Label(tw, text=self.text, justify=LEFT,
79 background="#ffffe0", relief=SOLID, borderwidth=1,
80 font = self.widget['font'])
81 self.label.pack()
82
83 self.checkhideid = self.widget.bind(CHECKHIDE_VIRTUAL_EVENT_NAME,
84 self.checkhide_event)
85 for seq in CHECKHIDE_SEQUENCES:
86 self.widget.event_add(CHECKHIDE_VIRTUAL_EVENT_NAME, seq)
87 self.widget.after(CHECKHIDE_TIME, self.checkhide_event)
88 self.hideid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME,
89 self.hide_event)
90 for seq in HIDE_SEQUENCES:
91 self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq)
92
93 def checkhide_event(self, event=None):
94 if not self.tipwindow:
95 # If the event was triggered by the same event that unbinded
96 # this function, the function will be called nevertheless,
97 # so do nothing in this case.
98 return
99 curline, curcol = map(int, self.widget.index("insert").split('.'))
100 if curline < self.parenline or \
101 (curline == self.parenline and curcol <= self.parencol) or \
102 self.widget.compare("insert", ">", MARK_RIGHT):
103 self.hidetip()
104 else:
105 self.position_window()
Martin v. Löwisee381a02012-03-13 14:18:36 -0700106 if self.checkhide_after_id is not None:
107 self.widget.after_cancel(self.checkhide_after_id)
108 self.checkhide_after_id = \
109 self.widget.after(CHECKHIDE_TIME, self.checkhide_event)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000110
111 def hide_event(self, event):
112 if not self.tipwindow:
113 # See the explanation in checkhide_event.
114 return
115 self.hidetip()
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000116
David Scherer7aced172000-08-15 01:13:23 +0000117 def hidetip(self):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000118 if not self.tipwindow:
119 return
120
121 for seq in CHECKHIDE_SEQUENCES:
122 self.widget.event_delete(CHECKHIDE_VIRTUAL_EVENT_NAME, seq)
123 self.widget.unbind(CHECKHIDE_VIRTUAL_EVENT_NAME, self.checkhideid)
124 self.checkhideid = None
125 for seq in HIDE_SEQUENCES:
126 self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq)
127 self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideid)
128 self.hideid = None
129
130 self.label.destroy()
131 self.label = None
132 self.tipwindow.destroy()
David Scherer7aced172000-08-15 01:13:23 +0000133 self.tipwindow = None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000134
135 self.widget.mark_unset(MARK_RIGHT)
136 self.parenline = self.parencol = self.lastline = None
137
138 def is_active(self):
139 return bool(self.tipwindow)
140
David Scherer7aced172000-08-15 01:13:23 +0000141
142
143###############################
144#
145# Test Code
146#
147class container: # Conceptually an editor_window
148 def __init__(self):
149 root = Tk()
150 text = self.text = Text(root)
151 text.pack(side=LEFT, fill=BOTH, expand=1)
152 text.insert("insert", "string.split")
153 root.update()
154 self.calltip = CallTip(text)
155
156 text.event_add("<<calltip-show>>", "(")
157 text.event_add("<<calltip-hide>>", ")")
158 text.bind("<<calltip-show>>", self.calltip_show)
159 text.bind("<<calltip-hide>>", self.calltip_hide)
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000160
David Scherer7aced172000-08-15 01:13:23 +0000161 text.focus_set()
Kurt B. Kaisere54710b2002-12-12 19:15:39 +0000162 root.mainloop()
David Scherer7aced172000-08-15 01:13:23 +0000163
164 def calltip_show(self, event):
165 self.calltip.showtip("Hello world")
166
167 def calltip_hide(self, event):
168 self.calltip.hidetip()
169
170def main():
171 # Test code
172 c=container()
173
174if __name__=='__main__':
175 main()