blob: 09d6414f62ef462110007154c454624635e7a89d [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"""
David Scherer7aced172000-08-15 01:13:23 +00007from Tkinter import *
8
Kurt B. Kaiserb1754452005-11-18 22:05:48 +00009HIDE_VIRTUAL_EVENT_NAME = "<<caltipwindow-hide>>"
10HIDE_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
David Scherer7aced172000-08-15 01:13:23 +000025
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000026 def position_window(self):
27 """Check if needs to reposition the window, and if so - do it."""
28 curline = int(self.widget.index("insert").split('.')[0])
29 if curline == self.lastline:
30 return
31 self.lastline = curline
32 self.widget.see("insert")
33 if curline == self.parenline:
34 box = self.widget.bbox("%d.%d" % (self.parenline,
35 self.parencol))
36 else:
37 box = self.widget.bbox("%d.0" % curline)
38 if not box:
39 box = list(self.widget.bbox("insert"))
40 # align to left of window
41 box[0] = 0
42 box[2] = 0
43 x = box[0] + self.widget.winfo_rootx() + 2
44 y = box[1] + box[3] + self.widget.winfo_rooty()
45 self.tipwindow.wm_geometry("+%d+%d" % (x, y))
46
47 def showtip(self, text, parenleft, parenright):
48 """Show the calltip, bind events which will close it and reposition it.
49 """
Kurt B. Kaisere54710b2002-12-12 19:15:39 +000050 # truncate overly long calltip
Kurt B. Kaisere72f05d2002-09-15 21:43:13 +000051 if len(text) >= 79:
52 text = text[:75] + ' ...'
David Scherer7aced172000-08-15 01:13:23 +000053 self.text = text
54 if self.tipwindow or not self.text:
55 return
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000056
57 self.widget.mark_set(MARK_RIGHT, parenright)
58 self.parenline, self.parencol = map(
59 int, self.widget.index(parenleft).split("."))
60
David Scherer7aced172000-08-15 01:13:23 +000061 self.tipwindow = tw = Toplevel(self.widget)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000062 self.position_window()
Kurt B. Kaisere54710b2002-12-12 19:15:39 +000063 # XXX 12 Dec 2002 KBK The following command has two effects: It removes
64 # the calltip window border (good) but also causes (at least on
65 # Linux) the calltip to show as a top level window, burning through
66 # any other window dragged over it. Also, shows on all viewports!
David Scherer7aced172000-08-15 01:13:23 +000067 tw.wm_overrideredirect(1)
Tony Lownds8b1b8d62002-09-23 01:04:05 +000068 try:
69 # This command is only needed and available on Tk >= 8.4.0 for OSX
70 # Without it, call tips intrude on the typing process by grabbing
71 # the focus.
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000072 tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w,
Tony Lownds8b1b8d62002-09-23 01:04:05 +000073 "help", "noActivates")
74 except TclError:
75 pass
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000076 self.label = Label(tw, text=self.text, justify=LEFT,
77 background="#ffffe0", relief=SOLID, borderwidth=1,
78 font = self.widget['font'])
79 self.label.pack()
80
81 self.checkhideid = self.widget.bind(CHECKHIDE_VIRTUAL_EVENT_NAME,
82 self.checkhide_event)
83 for seq in CHECKHIDE_SEQUENCES:
84 self.widget.event_add(CHECKHIDE_VIRTUAL_EVENT_NAME, seq)
85 self.widget.after(CHECKHIDE_TIME, self.checkhide_event)
86 self.hideid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME,
87 self.hide_event)
88 for seq in HIDE_SEQUENCES:
89 self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq)
90
91 def checkhide_event(self, event=None):
92 if not self.tipwindow:
93 # If the event was triggered by the same event that unbinded
94 # this function, the function will be called nevertheless,
95 # so do nothing in this case.
96 return
97 curline, curcol = map(int, self.widget.index("insert").split('.'))
98 if curline < self.parenline or \
99 (curline == self.parenline and curcol <= self.parencol) or \
100 self.widget.compare("insert", ">", MARK_RIGHT):
101 self.hidetip()
102 else:
103 self.position_window()
104 self.widget.after(CHECKHIDE_TIME, self.checkhide_event)
105
106 def hide_event(self, event):
107 if not self.tipwindow:
108 # See the explanation in checkhide_event.
109 return
110 self.hidetip()
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000111
David Scherer7aced172000-08-15 01:13:23 +0000112 def hidetip(self):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000113 if not self.tipwindow:
114 return
115
116 for seq in CHECKHIDE_SEQUENCES:
117 self.widget.event_delete(CHECKHIDE_VIRTUAL_EVENT_NAME, seq)
118 self.widget.unbind(CHECKHIDE_VIRTUAL_EVENT_NAME, self.checkhideid)
119 self.checkhideid = None
120 for seq in HIDE_SEQUENCES:
121 self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq)
122 self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideid)
123 self.hideid = None
124
125 self.label.destroy()
126 self.label = None
127 self.tipwindow.destroy()
David Scherer7aced172000-08-15 01:13:23 +0000128 self.tipwindow = None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000129
130 self.widget.mark_unset(MARK_RIGHT)
131 self.parenline = self.parencol = self.lastline = None
132
133 def is_active(self):
134 return bool(self.tipwindow)
135
David Scherer7aced172000-08-15 01:13:23 +0000136
137
138###############################
139#
140# Test Code
141#
142class container: # Conceptually an editor_window
143 def __init__(self):
144 root = Tk()
145 text = self.text = Text(root)
146 text.pack(side=LEFT, fill=BOTH, expand=1)
147 text.insert("insert", "string.split")
148 root.update()
149 self.calltip = CallTip(text)
150
151 text.event_add("<<calltip-show>>", "(")
152 text.event_add("<<calltip-hide>>", ")")
153 text.bind("<<calltip-show>>", self.calltip_show)
154 text.bind("<<calltip-hide>>", self.calltip_hide)
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000155
David Scherer7aced172000-08-15 01:13:23 +0000156 text.focus_set()
Kurt B. Kaisere54710b2002-12-12 19:15:39 +0000157 root.mainloop()
David Scherer7aced172000-08-15 01:13:23 +0000158
159 def calltip_show(self, event):
160 self.calltip.showtip("Hello world")
161
162 def calltip_hide(self, event):
163 self.calltip.hidetip()
164
165def main():
166 # Test code
167 c=container()
168
169if __name__=='__main__':
170 main()