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