Don't delete menu pos property after reading.
[rox-lib/lack.git] / python / rox / applet.py
blobccdc6c7ab5dfb342fec7f57b6d03bd7a2eea06fc
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 position_menu(self, menu):
41 """Use this as the third argument to Menu.popup()."""
42 x, y, mods = _root_window.get_pointer()
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 width, height = g.gdk.screen_width(), g.gdk.screen_height()
54 req = menu.size_request()
56 if side == 'Top':
57 y = margin
58 x -= 8 + req[0] / 4
59 elif side == 'Bottom':
60 y = height - margin - req[1]
61 x -= 8 + req[0] / 4
62 elif side == 'Left':
63 x = margin
64 y -= 16
65 elif side == 'Right':
66 x = width - margin - req[0]
67 y -= 16
68 else:
69 x -= req[0] / 2
70 y -= 32
72 def limit(v, min, max):
73 if v < min: return min
74 if v > max: return max
75 return v
77 x = limit(x, 4, width - 4 - req[0])
78 y = limit(y, 4, height - 4 - req[1])
80 return (x, y, True)