File: balloontip.py

package info (click to toggle)
taskcoach 1.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,496 kB
  • ctags: 17,810
  • sloc: python: 72,170; makefile: 254; ansic: 120; xml: 29; sh: 16
file content (225 lines) | stat: -rw-r--r-- 8,752 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'''
Task Coach - Your friendly task manager
Copyright (C) 2012 Task Coach developers <developers@taskcoach.org>

Task Coach is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Task Coach is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

# Not using agw.balloontip because it doesn't position properly and
# lacks events

import wx
from wx.lib.embeddedimage import PyEmbeddedImage


class BalloonTip(wx.Frame):
    ARROWSIZE = 16
    MAXWIDTH = 300

    def __init__(self, parent, target, message=None, title=None, bitmap=None, getRect=None):
        """Baloon tip."""

        super(BalloonTip, self).__init__(parent,
            style=wx.NO_BORDER|wx.FRAME_FLOAT_ON_PARENT|wx.FRAME_NO_TASKBAR|wx.FRAME_SHAPED|wx.POPUP_WINDOW)

        wheat = wx.ColourDatabase().Find('WHEAT')
        self.SetBackgroundColour(wheat)

        self._target = target
        self._getRect = getRect
        self._interior = wx.Panel(self)
        self._interior.Bind(wx.EVT_LEFT_DOWN, self.DoClose)
        self._interior.SetBackgroundColour(wheat)
        vsizer = wx.BoxSizer(wx.VERTICAL)
        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        if bitmap is not None:
            hsizer.Add(wx.StaticBitmap(self._interior, wx.ID_ANY, bitmap), 0, wx.ALIGN_CENTRE|wx.ALL, 3)
        if title is not None:
            titleCtrl = wx.StaticText(self._interior, wx.ID_ANY, title)
            hsizer.Add(titleCtrl, 1, wx.ALL|wx.ALIGN_CENTRE, 3)
            titleCtrl.Bind(wx.EVT_LEFT_DOWN, self.DoClose)
        vsizer.Add(hsizer, 0, wx.EXPAND)
        if message is not None:
            msg = wx.StaticText(self._interior, wx.ID_ANY, message)
            msg.Wrap(self.MAXWIDTH)
            vsizer.Add(msg, 1, wx.EXPAND|wx.ALL, 3)
            msg.Bind(wx.EVT_LEFT_DOWN, self.DoClose)

        self._interior.SetSizer(vsizer)
        self._interior.Fit()

        class Sizer(wx.PySizer):
            def __init__(self, interior, direction, offset):
                self._interior = interior
                self._direction = direction
                self._offset = offset
                super(Sizer, self).__init__()

            def SetDirection(self, direction):
                self._direction = direction

            def CalcMin(self):
                w, h = self._interior.GetClientSize()
                return wx.Size(w, h + self._offset)

            def RecalcSizes(self):
                if self._direction == 'bottom':
                    self._interior.SetPosition((0, 0))
                else:
                    self._interior.SetPosition((0, self._offset))

        self._sizer = Sizer(self._interior, 'bottom', self.ARROWSIZE)
        self.SetSizer(self._sizer)
        self.Position()
        self.Show()

        wx.GetTopLevelParent(target).Bind(wx.EVT_SIZE, self._OnDim)
        wx.GetTopLevelParent(target).Bind(wx.EVT_MOVE, self._OnDim)

    def _Unbind(self):
        wx.GetTopLevelParent(self._target).Unbind(wx.EVT_SIZE)
        wx.GetTopLevelParent(self._target).Unbind(wx.EVT_MOVE)

    def _OnDim(self, event):
        wx.CallAfter(self.Position)
        event.Skip()

    def DoClose(self, event, unbind=True):
        if unbind:
            self._Unbind()
        self.Close()

    def Position(self):
        w, h = self._interior.GetClientSizeTuple()
        h += self.ARROWSIZE
        if self._getRect is None:
            tw, th = self._target.GetSizeTuple()
            tx, ty = 0, 0
        else:
            tx, ty, tw, th = self._getRect()
        tx, ty = self._target.ClientToScreen(wx.Point(tx, ty))
        dpyIndex = max(0, wx.Display.GetFromPoint(wx.Point(tx, ty)) or 0)
        rect = wx.Display(dpyIndex).GetClientArea()

        x = max(rect.GetLeft(), min(rect.GetRight() - w, int(tx + tw / 2 - w / 2)))
        y = ty - h
        direction = 'bottom'
        if y < rect.GetTop():
            y = ty + th
            direction = 'top'

        mask = wx.EmptyBitmap(w, h)
        memDC = wx.MemoryDC()
        memDC.SelectObject(mask)
        try:
            memDC.SetBrush(wx.BLACK_BRUSH)
            memDC.SetPen(wx.BLACK_PEN)
            memDC.DrawRectangle(0, 0, w, h)

            memDC.SetBrush(wx.WHITE_BRUSH)
            memDC.SetPen(wx.WHITE_PEN)
            if direction == 'bottom':
                memDC.DrawPolygon([(0, 0),
                                   (w, 0),
                                   (w, h - self.ARROWSIZE),
                                   (tx + int(tw / 2) - x + int(self.ARROWSIZE / 2), h - self.ARROWSIZE),
                                   (tx + int(tw / 2) - x, h),
                                   (tx + int(tw / 2) - x - int(self.ARROWSIZE / 2), h - self.ARROWSIZE),
                                   (0, h - self.ARROWSIZE)])
            else:
                memDC.DrawPolygon([(0, self.ARROWSIZE),
                                   (tx + int(tw / 2) - x - int(self.ARROWSIZE / 2), self.ARROWSIZE),
                                   (tx + int(tw / 2) - x, 0),
                                   (tx + int(tw / 2) - x + int(self.ARROWSIZE / 2), self.ARROWSIZE),
                                   (w, self.ARROWSIZE),
                                   (w, h),
                                   (0, h)])
            self._sizer.SetDirection(direction)
        finally:
            memDC.SelectObject(wx.NullBitmap)
        self.SetDimensions(x, y, w, h)
        self.SetShape(wx.RegionFromBitmapColour(mask, wx.Colour(0, 0, 0)))
        self.Layout()


class BalloonTipManager(object):
    """
    Use this as a mixin in the top-level window that hosts balloon tip targets, to
    avoid them appearing all at once.
    """

    def __init__(self, *args, **kwargs):
        self.__tips = list()
        self.__displaying = None
        self.__kwargs = dict()
        self.__shutdown = False
        super(BalloonTipManager, self).__init__(*args, **kwargs)

        self.Bind(wx.EVT_CLOSE, self.__OnClose)

    def AddBalloonTip(self, target, message=None, title=None, bitmap=None, getRect=None, **kwargs):
        """Schedules a tip. Extra keyword arguments will be passed to L{OnBalloonTipShow} and L{OnBalloonTipClosed}."""
        for eTarget, eMessage, eTitle, eBitmap, eGetRect, eArgs in self.__tips:
            if (eTitle, eMessage) == (title, message):
                return
        self.__tips.append((target, message, title, bitmap, getRect, kwargs))
        self.__Try()

    def __Try(self):
        if self.__tips and not self.__shutdown and self.__displaying is None:
            target, message, title, bitmap, getRect, kwargs = self.__tips.pop(0)
            tip = BalloonTip(self, target, message=message, title=title, bitmap=bitmap, getRect=getRect)
            self.__displaying = tip
            self.OnBalloonTipShow(**kwargs)
            self.__kwargs = kwargs
            tip.Bind(wx.EVT_CLOSE, self.__OnCloseTip)

    def __OnClose(self, event):
        self.__shutdown = True
        event.Skip()

    def __OnCloseTip(self, event):
        event.Skip()
        self.__displaying = None
        self.OnBalloonTipClosed(**self.__kwargs)
        self.__Try()

    def OnBalloonTipShow(self, **kwargs):
        pass

    def OnBalloonTipClosed(self, **kwargs):
        pass


if __name__ == '__main__':
    class Frame(wx.Frame):
        def __init__(self):
            super(Frame, self).__init__(None, wx.ID_ANY, 'Test')

            self.btn = wx.Button(self, wx.ID_ANY, 'Show balloon')
            wx.EVT_BUTTON(self.btn, wx.ID_ANY, self.OnClick)
            s = wx.BoxSizer()
            s.Add(self.btn, 1, wx.EXPAND)
            self.SetSizer(s)
            self.Fit()

        def OnClick(self, event):
            BalloonTip(self, self.btn, '''Your bones don't break, mine do. That's clear. Your cells react to bacteria and viruses differently than mine. You don't get sick, I do. That's also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke. We get some in our lungs, we drown. However unreal it may seem, we are connected, you and I. We're on the same curve, just on opposite ends.''', title='Title', bitmap=wx.ArtProvider.GetBitmap(wx.ART_TIP, wx.ART_MENU, (16, 16)))

    class App(wx.App):
        def OnInit(self):
            Frame().Show()
            return True

    App(0).MainLoop()