blob: 84d22fd6671489e22966aaeff4c1cd9f8844bf10 [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()
75
76 self.checkhideid = self.widget.bind(CHECKHIDE_VIRTUAL_EVENT_NAME,
77 self.checkhide_event)
78 for seq in CHECKHIDE_SEQUENCES:
79 self.widget.event_add(CHECKHIDE_VIRTUAL_EVENT_NAME, seq)
80 self.widget.after(CHECKHIDE_TIME, self.checkhide_event)
81 self.hideid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME,
82 self.hide_event)
83 for seq in HIDE_SEQUENCES:
84 self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq)
85
86 def checkhide_event(self, event=None):
87 if not self.tipwindow:
88 # If the event was triggered by the same event that unbinded
89 # this function, the function will be called nevertheless,
90 # so do nothing in this case.
91 return
92 curline, curcol = map(int, self.widget.index("insert").split('.'))
93 if curline < self.parenline or \
94 (curline == self.parenline and curcol <= self.parencol) or \
95 self.widget.compare("insert", ">", MARK_RIGHT):
96 self.hidetip()
97 else:
98 self.position_window()
Martin v. Löwisee381a02012-03-13 14:18:36 -070099 if self.checkhide_after_id is not None:
100 self.widget.after_cancel(self.checkhide_after_id)
101 self.checkhide_after_id = \
102 self.widget.after(CHECKHIDE_TIME, self.checkhide_event)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000103
104 def hide_event(self, event):
105 if not self.tipwindow:
106 # See the explanation in checkhide_event.
107 return
108 self.hidetip()
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000109
David Scherer7aced172000-08-15 01:13:23 +0000110 def hidetip(self):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000111 if not self.tipwindow:
112 return
113
114 for seq in CHECKHIDE_SEQUENCES:
115 self.widget.event_delete(CHECKHIDE_VIRTUAL_EVENT_NAME, seq)
116 self.widget.unbind(CHECKHIDE_VIRTUAL_EVENT_NAME, self.checkhideid)
117 self.checkhideid = None
118 for seq in HIDE_SEQUENCES:
119 self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq)
120 self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideid)
121 self.hideid = None
122
123 self.label.destroy()
124 self.label = None
125 self.tipwindow.destroy()
David Scherer7aced172000-08-15 01:13:23 +0000126 self.tipwindow = None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000127
128 self.widget.mark_unset(MARK_RIGHT)
129 self.parenline = self.parencol = self.lastline = None
130
131 def is_active(self):
132 return bool(self.tipwindow)
133
David Scherer7aced172000-08-15 01:13:23 +0000134
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -0400135def _calltip_window(parent): # htest #
136 import re
137 from tkinter import Tk, Text, LEFT, BOTH
138
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400139 root = Tk()
140 root.title("Test calltips")
141 width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
142 root.geometry("+%d+%d"%(x, y + 150))
David Scherer7aced172000-08-15 01:13:23 +0000143
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -0400144 class MyEditWin: # conceptually an editor_window
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400145 def __init__(self):
146 text = self.text = Text(root)
147 text.pack(side=LEFT, fill=BOTH, expand=1)
148 text.insert("insert", "string.split")
149 root.update()
150 self.calltip = CallTip(text)
David Scherer7aced172000-08-15 01:13:23 +0000151
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400152 text.event_add("<<calltip-show>>", "(")
153 text.event_add("<<calltip-hide>>", ")")
154 text.bind("<<calltip-show>>", self.calltip_show)
155 text.bind("<<calltip-hide>>", self.calltip_hide)
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +0000156
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400157 text.focus_set()
158 root.mainloop()
David Scherer7aced172000-08-15 01:13:23 +0000159
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400160 def calltip_show(self, event):
161 self.calltip.showtip("Hello world", "insert", "end")
David Scherer7aced172000-08-15 01:13:23 +0000162
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400163 def calltip_hide(self, event):
164 self.calltip.hidetip()
David Scherer7aced172000-08-15 01:13:23 +0000165
Terry Jan Reedy44f09eb2014-07-01 18:52:37 -0400166 MyEditWin()
David Scherer7aced172000-08-15 01:13:23 +0000167
168if __name__=='__main__':
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400169 from idlelib.idle_test.htest import run
170 run(_calltip_window)