~jconti/recent-notifications/trunk

« back to all changes in this revision

Viewing changes to applet.py

  • Committer: Jason Conti
  • Date: 2010-02-16 22:26:39 UTC
  • Revision ID: jason.conti@gmail.com-20100216222639-nm9svgs4os15no3m
Created a basic shell of an applet, had to figure out how to stop the toggle button from trapping the right click for the context menu, which I found in clock.c of the GNOME clock applet code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
 
2
"""
 
3
applet.py
 
4
by Jason Conti
 
5
February 16, 2010
 
6
 
 
7
The main applet code.
 
8
"""
 
9
 
2
10
import pygtk
3
11
pygtk.require('2.0')
4
12
 
6
14
import gnomeapplet
7
15
import gobject
8
16
 
9
 
def background_show(applet):
10
 
  print "background: ", applet.get_background()
11
 
 
12
 
def sample_factory(applet, iid):
13
 
  print "Creating new applet instance"
14
 
  label = gtk.Label("Success!")
15
 
  applet.add(label)
16
 
  applet.show_all()
17
 
  gobject.timeout_add(1000, background_show, applet)
 
17
class Applet(object):
 
18
  """The main applet wrapper."""
 
19
  def __init__(self, applet, iid):
 
20
    self._applet = applet
 
21
 
 
22
    self._button = gtk.ToggleButton("Sample Very Very Long String")
 
23
    self._button.connect("button_press_event", self.do_not_eat_button_press)
 
24
    self._button.connect("clicked", self.on_button_clicked)
 
25
    self._applet.add(self._button)
 
26
 
 
27
    self._applet.setup_menu("""
 
28
      <popup name="button3">
 
29
        <menuitem name="About" verb="About" _label="_About" pixtype="stock" pixname="gtk-about"/>
 
30
      </popup>
 
31
      """, [
 
32
      ("About", lambda a, b: self.on_show_about(a))
 
33
      ])
 
34
 
 
35
    self._applet.connect("destroy", self.on_applet_destroyed)
 
36
    self._applet.show_all()
 
37
 
 
38
  # Callbacks
 
39
 
 
40
  def do_not_eat_button_press(self, widget, event):
 
41
    if event.button != 1:
 
42
      widget.stop_emission("button_press_event")
 
43
 
 
44
    return False
 
45
 
 
46
  def on_applet_destroyed(self, *args):
 
47
    pass
 
48
 
 
49
  def on_button_clicked(self, button, *args):
 
50
    print "Button clicked!"
 
51
 
 
52
  def on_show_about(self, *args):
 
53
    print "About clicked"
 
54
 
 
55
def applet_factory(applet, iid):
 
56
  Applet(applet, iid)
18
57
  return True
19
58
 
20
 
#print "Starting factory"
21
 
#gnomeapplet.bonobo_factory("OAFIID:GNOME_PythonAppletSample_Factory", 
22
 
#                           gnomeapplet.Applet.__gtype__, 
23
 
#                           "hello", "0", sample_factory)
24
 
#print "Factory ended"
25
 
main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
26
 
main_window.set_title("Python Applet")
27
 
main_window.connect("destroy", gtk.main_quit)
28
 
app = gnomeapplet.Applet()
29
 
sample_factory(app, None)
30
 
app.reparent(main_window)
31
 
main_window.show_all()
32
 
gtk.main()
33
 
 
 
59
def test_applet():
 
60
  main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
61
  main_window.set_title("Applet")
 
62
  main_window.connect("destroy", gtk.main_quit)
 
63
  app = gnomeapplet.Applet()
 
64
  applet_factory(app, None)
 
65
  app.reparent(main_window)
 
66
  main_window.show_all()
 
67
  gtk.main()
 
68
 
 
69
def initialize_applet():
 
70
  gnomeapplet.bonobo_factory("OAFIID:NotifyLogApplet_Factory", 
 
71
      gnomeapplet.Applet.__gtype__, 
 
72
      "hello", "0", applet_factory)
 
73
 
 
74
def main():
 
75
  #initialize_applet()
 
76
  test_applet()
 
77
 
 
78
if __name__ == '__main__':
 
79
  main()