Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[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
27 class TOOLTIP:
28     def __init__(self,widget):
29         self.widget=widget
30         self.text = None
31         self.timer = None
32         self.tooltip = None
33         self.label = None
34         self.bindings = []
35         self.bindings.append(self.widget.bind("<Enter>", self._enter))
36         self.bindings.append(self.widget.bind("<Leave>", self._leave))
37         self.bindings.append(self.widget.bind("<ButtonPress>", self._leave))
38         # user overrideable settings
39         self.time = 1000                    # milliseconds
40         self.relief = Tkinter.SOLID
41         self.justify = Tkinter.LEFT
42         self.fg = "#000000"
43         self.bg = "#ffffe0"
44         self.xoffset = 20
45         self.yoffset = 1
46
47     def setText(self, text):
48         self.text = text
49
50     def _unbind(self):
51         if self.bindings and self.widget:
52             self.widget.unbind("<Enter>", self.bindings[0])
53             self.widget.unbind("<Leave>", self.bindings[1])
54             self.widget.unbind("<ButtonPress>", self.bindings[2])
55             self.bindings = []
56
57     def destroy(self):
58         self._unbind()
59         self._leave()
60
61     def _enter(self, *event):
62         after_cancel(self.timer)
63         self.timer = after(self.widget, self.time, self._showTip)
64
65     def _leave(self, *event):
66         after_cancel(self.timer)
67         self.timer = None
68         if self.tooltip:
69             self.label.destroy()
70             destruct(self.label)
71             self.label = None
72             self.tooltip.destroy()
73             destruct(self.tooltip)
74             self.tooltip = None
75
76     def _showTip(self):
77         if self.tooltip or not self.text:
78             return
79         c = self.widget.__class__
80         if c in (Tkinter.Button,):
81             if self.widget["state"] == Tkinter.DISABLED:
82                 return
83         x = self.widget.winfo_rootx()
84         y = self.widget.winfo_rooty() + self.widget.winfo_height()
85         x = x + self.xoffset
86         y = y + self.yoffset
87         self.tooltip = Tkinter.Toplevel()
88         self.tooltip.wm_iconify()
89         self.tooltip.wm_overrideredirect(1)
90         self.tooltip.wm_protocol("WM_DELETE_WINDOW", self.destroy)
91         self.label = Tkinter.Label(self.tooltip, text=self.text,
92                          relief=self.relief, justify=self.justify,
93                          fg=self.fg, bg=self.bg, bd=1, takefocus=0)
94         self.label.pack(ipadx=1, ipady=1)
95         self.tooltip.wm_geometry("%+d%+d" % (x, y))
96         self.tooltip.wm_deiconify()
97