File: BalloonTip.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (374 lines) | stat: -rw-r--r-- 14,779 bytes parent folder | download | duplicates (5)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# ----------------------------------------------------------------------------
# BalloonTip Demo Implementation
#
# This Demo Shows How To Use The BalloonTip Control, With Different Styles
# And Behaviors.
# ----------------------------------------------------------------------------


import wx
from wx.lib.stattext import GenStaticText as StaticText
from wx.lib.buttons import GenBitmapButton as BitmapButton

import os
import sys

try:
    dirName = os.path.dirname(os.path.abspath(__file__))
except:
    dirName = os.path.dirname(os.path.abspath(sys.argv[0]))

sys.path.append(os.path.split(dirName)[0])

try:
    from agw import balloontip as BT
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.balloontip as BT

import images

ArtIDs = [ "wx.ART_HELP_PAGE",
           "wx.ART_GO_FORWARD",
           "wx.ART_FILE_OPEN",
           "wx.ART_HELP",
           "wx.ART_ERROR",
           "wx.ART_QUESTION",
           "wx.ART_WARNING",
           "wx.ART_INFORMATION",
           "wx.ART_HELP", 
           ]


# ----------------------------------------------------------------------------
# Beginning Of BalloonTip Demo
# ----------------------------------------------------------------------------

class BalloonTipDemo(wx.Frame):

    def __init__(self, parent, log):

        wx.Frame.__init__(self, parent, title="BalloonTip wxPython Demo ;-)")

        self.statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
        self.statusbar.SetStatusWidths([-2, -1])
        # statusbar fields
        statusbar_fields = [("Welcome To WxPython " + wx.VERSION_STRING),
                            ("BalloonTip Demo")]
        
        for i in range(len(statusbar_fields)):
            self.statusbar.SetStatusText(statusbar_fields[i], i)

        self.SetIcon(images.Mondrian.GetIcon())
        self.SetMenuBar(self.CreateMenuBar())
        
        panel = wx.Panel(self, -1)

        mainsizer = wx.FlexGridSizer(3, 4, hgap=2, vgap=2)

        # Add A Button
        button = wx.Button(panel, -1, "Press Me!")
        # Add A TextCtrl
        textctrl = wx.TextCtrl(panel, -1, "I Am A TextCtrl")
        # Add A CheckBox
        checkbox = wx.CheckBox(panel, -1, "3-State Checkbox",
                               style=wx.CHK_3STATE | wx.CHK_ALLOW_3RD_STATE_FOR_USER)
        samplelist=['One', 'Two', 'Three', 'Four', 'Kick', 'The', 'Demo', 'Out',
                    'The', 'Door', ';-)']
        # Add A Choice
        choice = wx.Choice(panel, -1, choices = samplelist)
        # Add A Gauge
        gauge = wx.Gauge(panel, -1, 50, style=wx.GA_SMOOTH)
        # Add A ListBox
        listbox = wx.ListBox(panel, -1, choices=samplelist, style=wx.LB_SINGLE)
        # Add A TreeCtrl
        isz = (16,16)        
        treecontrol = wx.TreeCtrl(panel, -1)
        il = wx.ImageList(isz[0], isz[1])
        fldridx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, isz))
        fldropenidx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, isz))
        fileidx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_REPORT_VIEW, wx.ART_OTHER, isz))
        treecontrol.SetImageList(il)
        self.il = il
        root = treecontrol.AddRoot("ROOT")
        treecontrol.SetPyData(root, None)
        treecontrol.SetItemImage(root, fldridx, wx.TreeItemIcon_Normal)
        treecontrol.SetItemImage(root, fldropenidx, wx.TreeItemIcon_Expanded)
        for ii in range(11):
            child = treecontrol.AppendItem(root, samplelist[ii])
            treecontrol.SetPyData(child, None)
            treecontrol.SetItemImage(child, fldridx, wx.TreeItemIcon_Normal)
            treecontrol.SetItemImage(child, fldropenidx, wx.TreeItemIcon_Selected)

        # Add A Slider            
        slider = wx.Slider(panel, -1, 25, 1, 100, 
            style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS)# | wx.SL_LABELS)
        slider.SetTickFreq(5, 1)
        # Add Another TextCtrl
        textctrl2 = wx.TextCtrl(panel, -1, "Another TextCtrl")
        # Add A GenStaticText
        statictext = StaticText(panel, -1, "Hello World!")
        statictext.SetFont(wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD, False))
        bmp = wx.ArtProvider_GetBitmap(wx.ART_INFORMATION,
                                       wx.ART_TOOLBAR, (16,16))
        # Add A GenBitmapButton
        bitmapbutton = BitmapButton(panel, -1, bmp)
        button2 = wx.Button(panel, -1, "Disable BalloonTip")

        tbicon = wx.TaskBarIcon()
        tbicon.SetIcon(images.Mondrian.GetIcon())

        controls = list(panel.GetChildren())
        controls.append(tbicon)
        self.tbicon = tbicon

        # Add The Controls To The Main FlexGridSizer
        mainsizer.Add(button, 0, wx.EXPAND | wx.ALL, 10)
        mainsizer.Add(textctrl, 0, wx.EXPAND | wx.ALL, 10)
        mainsizer.Add(checkbox, 0, wx.EXPAND | wx.ALL, 10)
        mainsizer.Add(choice, 0, wx.EXPAND | wx.ALL, 10)
        mainsizer.Add(gauge, 0, wx.ALL, 10)
        mainsizer.Add(listbox, 0, wx.EXPAND | wx.ALL, 10)
        mainsizer.Add(treecontrol, 0, wx.EXPAND, wx.ALL, 10)
        mainsizer.Add(slider, 0, wx.ALL, 10)
        mainsizer.Add(textctrl2, 0, wx.ALL, 10)
        mainsizer.Add(statictext, 0, wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 10)
        mainsizer.Add(bitmapbutton, 0, wx.ALL, 10)
        mainsizer.Add(button2, 0, wx.ALL, 10)
        
        panel.SetSizer(mainsizer)
        mainsizer.Layout()

        # Declare The BalloonTip Background Colours
        bgcolours = [None, wx.WHITE, wx.GREEN, wx.BLUE, wx.CYAN, wx.RED, None, None,
                     wx.LIGHT_GREY, None, wx.WHITE, None, None]

        # Declare The BalloonTip Top-Left Icons
        icons = []
        for ii in xrange(4):
            bmp = wx.ArtProvider_GetBitmap(eval(ArtIDs[ii]), wx.ART_TOOLBAR, (16,16))
            icons.append(bmp)
            
        icons.extend([None]*5)

        for ii in xrange(4, 9):
            bmp = wx.ArtProvider_GetBitmap(eval(ArtIDs[ii]), wx.ART_TOOLBAR, (16,16))
            icons.append(bmp)

        # Declare The BalloonTip Top Titles
        titles = ["Button Help", "Texctrl Help", "CheckBox Help", "Choice Help",
                  "Gauge Help", "", "", "Read Me Carefully!", "SpinCtrl Help",
                  "StaticText Help", "BitmapButton Help", "Button Help", "Taskbar Help"]

        fontone = wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD, True)
        fonttwo = wx.Font(14, wx.SCRIPT, wx.NORMAL, wx.BOLD, False)
        fontthree = wx.Font(9, wx.SWISS, wx.ITALIC, wx.NORMAL, False)
        fontfour = wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, True)

        # Declare The BalloonTip Top Titles Fonts    
        titlefonts = [None, None, fontone, None, fonttwo, fontthree, None, None,
                      None, fontfour, fontthree, None, None]

        # Declare The BalloonTip Top Titles Colours
        titlecolours = [None, None, wx.WHITE, wx.NamedColour("YELLOW"), None, wx.WHITE,
                        wx.BLUE, wx.RED, None, None, wx.LIGHT_GREY, None, None]

        # Declare The BalloonTip Messages
        msg1 = "This Is The Default BalloonTip Window\nYou Can Customize It! "\
               "Look At The Demo!"
        msg2 = "You Can Change The Background Colour\n Of The Balloon Window."
        msg3 = "You Can Also Change The Font And The\nColour For The Title."
        msg4 = "I Have Nothing Special To Suggest!\n\nWelcome To wxPython " + \
               wx.VERSION_STRING + " !"
        msg5 = "What About If I Don't Want The Icon?\nNo Problem!"
        msg6 = "I Don't Have The Icon Nor The Title.\n\nDo You Love Me Anyway?"
        msg7 = "Some Comments On The Window Shape:\n\n- BT_ROUNDED: Creates A "\
               "Rounded Rectangle;\n- BT_RECTANGLE: Creates A Rectangle.\n"
        msg8 = "Some Comments On The BalloonTip Style:\n\n"\
               "BT_LEAVE: The BalloonTip Is Destroyed When\nThe Mouse Leaves"\
               "The Target Widget;\n\nBT_CLICK: The BalloonTip Is Destroyed When\n"\
               "You Click Any Region Of The BalloonTip;\n\nBT_BUTTON: The BalloonTip"\
               " Is Destroyed When\nYou Click On The Top-Right Small Button."
        msg9 = "Some Comments On Delay Time:\n\nBy Default, The Delay Time After Which\n"\
               "The BalloonTip Is Destroyed Is Very Long.\nYou Can Change It By Using"\
               " The\nSetEndDelay() Method."
        msg10 = "I Have Nothing Special To Suggest!\n\nRead Me FAST, You Have Only 3 "\
                "Seconds!"
        msg11 = "I Hope You Will Enjoy BalloonTip!\nIf This Is The Case, Please\n"\
                "Post Some Comments On wxPython\nMailing List!"
        msg12 = "This Button Enable/Disable Globally\nThe BalloonTip On Your Application."
        msg13 = "This Is A BalloonTip For The\nTaskBar Icon Of Your Application.\n"\
                "All The Styles For BalloonTip Work."

        messages = [msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10,
                    msg11, msg12, msg13]

        # Declare The BalloonTip Tip Messages Colours
        messagecolours = [None, None, None, wx.WHITE, wx.BLUE,
                          None, wx.BLUE, None, None, wx.RED, wx.GREEN,
                          wx.BLUE, None]

        fontone = wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, True)
        fonttwo = wx.Font(8, wx.SWISS, wx.ITALIC, wx.NORMAL, False)
        fontthree = wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, True)

        # Declare The BalloonTip Tip Messages Fonts
        messagefonts = [None, None, None, fontone, None, None, fonttwo, None,
                       fontthree, None, None, None, None]

        # Declare The BalloonTip Frame Shapes
        windowshapes = [BT.BT_ROUNDED, BT.BT_RECTANGLE, BT.BT_ROUNDED, BT.BT_RECTANGLE,
                        BT.BT_ROUNDED, BT.BT_RECTANGLE, BT.BT_ROUNDED, BT.BT_ROUNDED,
                        BT.BT_ROUNDED, BT.BT_RECTANGLE, BT.BT_ROUNDED, BT.BT_RECTANGLE, BT.BT_RECTANGLE]

        # Declare The BalloonTip Destruction Style
        tipstyles = [BT.BT_LEAVE, BT.BT_CLICK, BT.BT_BUTTON, BT.BT_LEAVE, BT.BT_CLICK,
                       BT.BT_LEAVE, BT.BT_CLICK, BT.BT_BUTTON, BT.BT_BUTTON, BT.BT_CLICK,
                       BT.BT_LEAVE, BT.BT_LEAVE, BT.BT_BUTTON]

        # Set The Targets/Styles For The BalloonTip
        for ii, widget in enumerate(controls):
            tipballoon = BT.BalloonTip(topicon=icons[ii], toptitle=titles[ii],
                                       message=messages[ii], shape=windowshapes[ii],
                                       tipstyle=tipstyles[ii])
            # Set The Target
            tipballoon.SetTarget(widget)
            # Set The Balloon Colour
            tipballoon.SetBalloonColour(bgcolours[ii])
            # Set The Font For The Top Title
            tipballoon.SetTitleFont(titlefonts[ii])
            # Set The Colour For The Top Title
            tipballoon.SetTitleColour(titlecolours[ii])
            # Set The Font For The Tip Message
            tipballoon.SetMessageFont(messagefonts[ii])
            # Set The Colour For The Tip Message
            tipballoon.SetMessageColour(messagecolours[ii])
            # Set The Delay After Which The BalloonTip Is Created
            tipballoon.SetStartDelay(1000)
            if ii == 9:
                # Set The Delay After Which The BalloonTip Is Destroyed
                tipballoon.SetEndDelay(3000)

        # Store The Last BalloonTip Reference To Enable/Disable Globall The
        # BalloonTip. You Can Store Any Of Them, Not Necessarily The Last One.
        self.lasttip = tipballoon          
        self.gauge = gauge
        self.count = 0
        
        button2.Bind(wx.EVT_BUTTON, self.OnActivateBalloon)        
        self.Bind(wx.EVT_IDLE, self.IdleHandler)

        frameSizer = wx.BoxSizer(wx.VERTICAL)
        frameSizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(frameSizer)
        frameSizer.Layout()
        self.Fit()

        self.CenterOnParent()        


    def IdleHandler(self, event):
        
        self.count = self.count + 1

        if self.count >= 50:
            self.count = 0

        self.gauge.SetValue(self.count)
        

    def CreateMenuBar(self):

        # Make a menubar
        file_menu = wx.Menu()
        help_menu = wx.Menu()
        
        TEST_QUIT = wx.NewId()
        TEST_ABOUT = wx.NewId()
        
        file_menu.Append(TEST_QUIT, "&Exit")
        help_menu.Append(TEST_ABOUT, "&About")

        menu_bar = wx.MenuBar()

        menu_bar.Append(file_menu, "&File")
        menu_bar.Append(help_menu, "&Help")

        self.Bind(wx.EVT_MENU, self.OnAbout, id=TEST_ABOUT)
        self.Bind(wx.EVT_MENU, self.OnQuit, id=TEST_QUIT)
        self.Bind(wx.EVT_CLOSE, self.OnQuit)

        return menu_bar


    def OnQuit(self, event):

        self.tbicon.RemoveIcon()
        self.tbicon.Destroy()
        self.Destroy()


    def OnAbout(self, event):

        msg = "This is the about dialog of the BalloonTip demo.\n\n" + \
              "Author: Andrea Gavana @ 29 May 2005\n\n" + \
              "Please report any bug/requests or improvements\n" + \
              "to me at the following adresses:\n\n" + \
              "andrea.gavana@agip.it\n" + "andrea_gavana@tin.it\n\n" + \
              "Welcome To wxPython " + wx.VERSION_STRING + "!!"
              
        dlg = wx.MessageDialog(self, msg, "BalloonTip Demo",
                               wx.OK | wx.ICON_INFORMATION)
        dlg.SetFont(wx.Font(8, wx.NORMAL, wx.NORMAL, wx.NORMAL, False, "Verdana"))
        dlg.ShowModal()
        dlg.Destroy()
        

    def OnActivateBalloon(self, event):

        button = event.GetEventObject()
        label = button.GetLabel()
        tips = self.lasttip
        
        if label == "Disable BalloonTip":
            button.SetLabel("Enable BalloonTip")
            tips.EnableTip(False)
        else:
            button.SetLabel("Disable BalloonTip")
            tips.EnableTip(True)
            
        event.Skip()

        
#---------------------------------------------------------------------------


class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        self.log = log
        wx.Panel.__init__(self, parent, -1)

        b = wx.Button(self, -1, " Test BalloonTip ", (50,50))
        self.Bind(wx.EVT_BUTTON, self.OnButton, b)


    def OnButton(self, evt):
        self.win = BalloonTipDemo(self, self.log)
        self.win.Show(True)

#----------------------------------------------------------------------

def runTest(frame, nb, log):
    win = TestPanel(nb, log)
    return win

#----------------------------------------------------------------------


overview = BT.__doc__


if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])