blob: 8e68a76b2a8a2f25c4836e0df6bca6a0763efd87 [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.
Kurt B. Kaisere54710b2002-12-12 19:15:39 +00005"""
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -04006from tkinter import Toplevel, Label, LEFT, SOLID, TclError
David Scherer7aced172000-08-15 01:13:23 +00007
Kurt B. Kaiser3b148ca2005-11-22 02:17:10 +00008HIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-hide>>"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +00009HIDE_SEQUENCES = ("<Key-Escape>", "<FocusOut>")
10CHECKHIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-checkhide>>"
11CHECKHIDE_SEQUENCES = ("<KeyRelease>", "<ButtonRelease>")
12CHECKHIDE_TIME = 100 # miliseconds
13
14MARK_RIGHT = "calltipwindowregion_right"
15
David Scherer7aced172000-08-15 01:13:23 +000016class CallTip:
17
18 def __init__(self, widget):
19 self.widget = widget
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000020 self.tipwindow = self.label = None
21 self.parenline = self.parencol = None
22 self.lastline = None
23 self.hideid = self.checkhideid = None
Martin v. Löwisee381a02012-03-13 14:18:36 -070024 self.checkhide_after_id = 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 """
Terry Jan Reedy0e3b0e32014-01-21 21:12:24 -050050 # Only called in CallTips, where lines are truncated
David Scherer7aced172000-08-15 01:13:23 +000051 self.text = text
52 if self.tipwindow or not self.text:
53 return
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000054
55 self.widget.mark_set(MARK_RIGHT, parenright)
56 self.parenline, self.parencol = map(
57 int, self.widget.index(parenleft).split("."))
58
David Scherer7aced172000-08-15 01:13:23 +000059 self.tipwindow = tw = Toplevel(self.widget)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000060 self.position_window()
Kurt B. Kaiser3b148ca2005-11-22 02:17:10 +000061 # remove border on calltip window
David Scherer7aced172000-08-15 01:13:23 +000062 tw.wm_overrideredirect(1)
Tony Lownds8b1b8d62002-09-23 01:04:05 +000063 try:
64 # This command is only needed and available on Tk >= 8.4.0 for OSX
65 # Without it, call tips intrude on the typing process by grabbing
66 # the focus.
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000067 tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w,
Tony Lownds8b1b8d62002-09-23 01:04:05 +000068 "help", "noActivates")
69 except TclError:
70 pass
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000071 self.label = Label(tw, text=self.text, justify=LEFT,
72 background="#ffffe0", relief=SOLID, borderwidth=1,
73 font = self.widget['font'])
74 self.label.pack()
Terry Jan Reedyd2134c72015-09-26 20:03:57 -040075 tw.lift() # work around bug in Tk 8.5.18+ (issue #24570)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000076
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
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -0400136def _calltip_window(parent): # htest #
Terry Jan Reedycd567362014-10-17 01:31:35 -0400137 from tkinter import Toplevel, Text, LEFT, BOTH
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -0400138
Terry Jan Reedycd567362014-10-17 01:31:35 -0400139 top = Toplevel(parent)
140 top.title("Test calltips")
141 top.geometry("200x100+%d+%d" % (parent.winfo_rootx() + 200,
142 parent.winfo_rooty() + 150))
143 text = Text(top)
144 text.pack(side=LEFT, fill=BOTH, expand=1)
145 text.insert("insert", "string.split")
146 top.update()
147 calltip = CallTip(text)
David Scherer7aced172000-08-15 01:13:23 +0000148
Terry Jan Reedycd567362014-10-17 01:31:35 -0400149 def calltip_show(event):
150 calltip.showtip("(s=Hello world)", "insert", "end")
151 def calltip_hide(event):
152 calltip.hidetip()
153 text.event_add("<<calltip-show>>", "(")
154 text.event_add("<<calltip-hide>>", ")")
155 text.bind("<<calltip-show>>", calltip_show)
156 text.bind("<<calltip-hide>>", calltip_hide)
157 text.focus_set()
David Scherer7aced172000-08-15 01:13:23 +0000158
159if __name__=='__main__':
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400160 from idlelib.idle_test.htest import run
161 run(_calltip_window)