xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #405: pythonicon.py

File pythonicon.py, 3.7 KB (added by Antoine Martin, 8 years ago)

simple gtk + xlib tray example (GPL taken from awn-extras-applets)

Line 
1#!/usr/bin/python
2"""
3Python Icon - An example System tray Icon
4Copyright (c) 2008 Nathan Howard (triggerhapp@googlemail.com)
5
6   This program is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19"""
20
21# Very litterally, this creates a small window and tells the system tray
22# it wants to be embedded. For RGB system trays, this will probably
23# cause it a problem since, even though it reads _NET_SYSYTEM_TRAY_VISUAL
24# it ignores it and aggressively uses RGBA.
25
26# Since this was created to test RGBA PyNot, why not?
27
28# currently it chooses a random colour, with 0.6 alpha,
29# and fills its window with it, OR draws the "default application" icon
30
31# Hacked together from small fragments I wrote to do the same thing in
32# PyNot
33
34import sys
35from Xlib import X, display, error,Xatom,Xutil
36from Xlib.ext import shape
37import Xlib.protocol.event
38import gtk
39from gtk import gdk
40import gobject      #Gtk/Gdk/GObj for interfacing with the applet
41import select
42import random
43import cairo
44
45dsp=display.Display()
46scr = dsp.screen()
47root = scr.root
48
49def sendEv(dest,type,data,evty):
50    data = (data+[0]*(5-len(data)))[:5]
51    myevent=Xlib.protocol.event.ClientMessage(
52               window=dest.id,
53               client_type=type,
54               data=(32,(data)),
55               type=X.ClientMessage
56               )
57    dest.send_event(myevent,event_mask=evty)
58_OPCODE = dsp.intern_atom("_NET_SYSTEM_TRAY_OPCODE")
59_VISUAL = dsp.intern_atom("_NET_SYSTEM_TRAY_VISUAL")
60_XEMBED_INFO= dsp.intern_atom("_XEMBED_INFO")
61_MANAGER = dsp.intern_atom("_NET_SYSTEM_TRAY_S%d"%dsp.get_default_screen())
62_DESKTOP = dsp.intern_atom("_NET_WM_DESKTOP")
63
64tray= dsp.get_selection_owner(_MANAGER)
65traywin= dsp.create_resource_object("window",tray.id)
66
67print traywin.get_full_property(_VISUAL,Xatom.VISUALID)
68
69dudwindow = gtk.Window()
70dudwindow.set_title("PyNoT Test Icon")
71dudwindow.set_decorated(False)
72dudwindow.add_events(gdk.BUTTON_RELEASE_MASK)
73dudwindow.add_events(gdk.EXPOSURE_MASK)
74dudwindow.add_events(gdk.BUTTON_PRESS_MASK)
75dudwindow.set_app_paintable(True)
76screen = dudwindow.get_screen()
77colormap = screen.get_rgba_colormap()
78#print colormap
79dudwindow.set_colormap(colormap)
80dudwindow.show()
81wind_id= dudwindow.window.xid
82wind=dsp.create_resource_object("window",wind_id)
83
84def expose(widget, event):
85    if(random.random() < 0.8):
86        cr=dudwindow.window.cairo_create()
87        cr.set_source_rgba(random.random(),random.random(),random.random(),
88                           0.6)
89
90        cr.set_operator(cairo.OPERATOR_SOURCE)
91        cr.paint()
92    else:
93        cr=dudwindow.window.cairo_create()
94        cr.set_source_rgba(0.0,0.0,0.0,0.0)
95        cr.set_operator(cairo.OPERATOR_SOURCE)
96        cr.paint()
97        imagefile = "/usr/share/icons/xpra.png"
98        image=cairo.ImageSurface.create_from_png(imagefile)
99        pattern =cairo.SurfacePattern(image)
100        cr.set_source(pattern)
101        cr.paint()
102
103
104
105def button(widget,event):
106    print event.button
107
108dudwindow.connect("expose-event",expose)
109dudwindow.connect("button-press-event",button)
110
111wind.configure(width=24,height=24)
112
113sendEv(traywin,_OPCODE,[X.CurrentTime,0L,wind.id,0L,0L],X.NoEventMask)
114
115dsp.flush()
116
117gtk.main()