Salome HOME
correction copier-coller de MACRO
[tools/eficas.git] / Editeur / tooltip.py
1 #            CONFIGURATION MANAGEMENT OF EDF VERSION
2 # ======================================================================
3 # COPYRIGHT (C) 1991 - 2002  EDF R&D                  WWW.CODE-ASTER.ORG
4 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
5 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
6 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
7 # (AT YOUR OPTION) ANY LATER VERSION.
8 #
9 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
10 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
11 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
12 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
13 #
14 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
15 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
16 #    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
17 #
18 #
19 # ======================================================================
20 """
21     Ce module propose la classe TOOLTIP pour
22     mettre en oeuvre les bulles d'aide
23 """
24
25 import Tkinter
26 import types
27
28 def destruct(obj):
29     # assist in breaking circular references
30     if obj is not None:
31         assert type(obj) is types.InstanceType
32         for k in obj.__dict__.keys():
33             obj.__dict__[k] = None
34             ##del obj.__dict__[k]
35
36 def after(widget, ms, func, *args):
37     timer = apply(widget.after, (ms, func) + args)
38     command = widget._tclCommands[-1]
39     return (timer, command, widget)
40
41 def after_cancel(t):
42     if t is not None:
43         t[2].after_cancel(t[0])
44         try:
45             t[2].deletecommand(t[1])
46         except Tkinter.TclError:
47             pass
48
49 class TOOLTIP:
50     def __init__(self,widget,text=None):
51         self.widget=widget
52         self.text = text
53         self.timer = None
54         self.tooltip = None
55         self.label = None
56         self.bindings = []
57         self.bindings.append(self.widget.bind("<Enter>", self._enter))
58         self.bindings.append(self.widget.bind("<Leave>", self._leave))
59         self.bindings.append(self.widget.bind("<ButtonPress>", self._leave))
60         # user overrideable settings
61         self.time = 1000                    # milliseconds
62         self.relief = Tkinter.SOLID
63         self.justify = Tkinter.LEFT
64         self.fg = "#000000"
65         self.bg = "#ffffe0"
66         self.xoffset = 20
67         self.yoffset = 1
68
69     def setText(self, text):
70         self.text = text
71
72     def _unbind(self):
73         if self.bindings and self.widget:
74             self.widget.unbind("<Enter>", self.bindings[0])
75             self.widget.unbind("<Leave>", self.bindings[1])
76             self.widget.unbind("<ButtonPress>", self.bindings[2])
77             self.bindings = []
78
79     def destroy(self):
80         self._unbind()
81         self._leave()
82
83     def _enter(self, *event):
84         after_cancel(self.timer)
85         self.timer = after(self.widget, self.time, self._showTip)
86
87     def _leave(self, *event):
88         after_cancel(self.timer)
89         self.timer = None
90         if self.tooltip:
91             self.label.destroy()
92             destruct(self.label)
93             self.label = None
94             self.tooltip.destroy()
95             destruct(self.tooltip)
96             self.tooltip = None
97
98     def _showTip(self):
99         if self.tooltip or not self.text:
100             return
101         c = self.widget.__class__
102         if c in (Tkinter.Button,):
103             if self.widget["state"] == Tkinter.DISABLED:
104                 return
105         x = self.widget.winfo_rootx()
106         y = self.widget.winfo_rooty() + self.widget.winfo_height()
107         x = x + self.xoffset
108         y = y + self.yoffset
109         self.tooltip = Tkinter.Toplevel()
110         self.tooltip.wm_iconify()
111         self.tooltip.wm_overrideredirect(1)
112         self.tooltip.wm_protocol("WM_DELETE_WINDOW", self.destroy)
113         self.label = Tkinter.Label(self.tooltip, text=self.text,
114                          relief=self.relief, justify=self.justify,
115                          fg=self.fg, bg=self.bg, bd=1, takefocus=0)
116         self.label.pack(ipadx=1, ipady=1)
117         self.tooltip.wm_geometry("%+d%+d" % (x, y))
118         self.tooltip.wm_deiconify()
119
120 if __name__ == "__main__":
121    root=Tkinter.Tk()
122    label = Tkinter.Label(root, text="coucou")
123    label.pack()
124    tp=TOOLTIP(label,"texte d'aide")
125    root.mainloop()
126