If a service property exists but the window it points to doesn't, then
[rox-lib.git] / python / rox / applet.py
blobfd0eb0310035ae1af7aeeb3dc9558321294bc095
1 """To create a panel applet for ROX-Filer, you should add a file called
2 AppletRun to your application. This is run when your applet it dragged onto
3 a panel. It works like the usual AppRun, except that it is passed the XID of
4 the GtkSocket widget that ROX-Filer creates for you on the panel.
6 A sample AppletRun might look like this:
8 #!/usr/bin/env python
9 import findrox; findrox.version(1, 9, 12)
10 import rox
11 import sys
12 from rox import applet, g
14 plug = applet.Applet(sys.argv[1])
16 label = g.Label('Hello\\nWorld!')
17 plug.add(label)
18 plug.show_all()
20 rox.mainloop()
21 """
23 import rox
24 from rox import g
26 _root_window = g.gdk.get_default_root_window()
28 class Applet(g.Plug):
29 """When your AppletRun file is used, create an Applet widget with
30 the argument passed to AppletRun. Show the widget to make it appear
31 in the panel. toplevel_* functions are called automatically."""
32 def __init__(self, xid):
33 """xid is the sys.argv[1] passed to AppletRun."""
34 xid = long(xid)
35 g.Plug.__init__(self, xid)
36 self.socket = g.gdk.window_foreign_new(xid)
37 rox.toplevel_ref()
38 self.connect('destroy', rox.toplevel_unref)
40 def is_vertical_panel(self):
41 """Returns True if the panel this applet is on is a left
42 or right panel."""
43 pos = self.socket.property_get('_ROX_PANEL_MENU_POS',
44 'STRING', False)
45 if pos: pos = pos[2]
46 if pos:
47 side, margin = pos.split(',')
48 margin = int(margin)
49 else:
50 side, margin = None, 2
52 if side == 'Left' or side == 'Right':
53 return True
54 elif side == 'Top' or side == 'Bottom':
55 return False
57 # Couldn't work out the side, return None which will
58 # probably be interpreted as False.
59 return None
61 def position_menu(self, menu):
62 """Use this as the third argument to Menu.popup()."""
63 x, y, mods = _root_window.get_pointer()
64 pos = self.socket.property_get('_ROX_PANEL_MENU_POS',
65 'STRING', False)
66 if pos: pos = pos[2]
67 if pos:
68 side, margin = pos.split(',')
69 margin = int(margin)
70 else:
71 side, margin = None, 2
73 width, height = g.gdk.screen_width(), g.gdk.screen_height()
75 req = menu.size_request()
77 if side == 'Top':
78 y = margin
79 x -= 8 + req[0] / 4
80 elif side == 'Bottom':
81 y = height - margin - req[1]
82 x -= 8 + req[0] / 4
83 elif side == 'Left':
84 x = margin
85 y -= 16
86 elif side == 'Right':
87 x = width - margin - req[0]
88 y -= 16
89 else:
90 x -= req[0] / 2
91 y -= 32
93 def limit(v, min, max):
94 if v < min: return min
95 if v > max: return max
96 return v
98 x = limit(x, 4, width - 4 - req[0])
99 y = limit(y, 4, height - 4 - req[1])
101 return (x, y, True)