Position the popup menu.
[ibus.git] / panel / handle.py
blobb70aa667e222f383e28dd6be0e96a48fce935c95
1 # vim:set noet ts=4:
3 # ibus - The Input Bus
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program; if not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 # Boston, MA 02111-1307 USA
22 import gtk
23 import gtk.gdk as gdk
24 import gobject
26 class Handle (gtk.EventBox):
27 def __init__ (self):
28 gtk.EventBox.__init__ (self)
29 self.set_visible_window (False)
30 self.set_size_request (10, -1)
31 self.set_events (
32 gdk.EXPOSURE_MASK | \
33 gdk.BUTTON_PRESS_MASK | \
34 gdk.BUTTON_RELEASE_MASK | \
35 gdk.BUTTON1_MOTION_MASK)
37 self._move_begined = False
39 root = gdk.get_default_root_window ()
40 workarea = root.property_get ("_NET_WORKAREA")[2]
42 def do_button_press_event (self, event):
43 if event.button == 1:
44 root = gdk.get_default_root_window ()
45 desktop = root.property_get ("_NET_CURRENT_DESKTOP")[2][0]
46 self._workarea = root.property_get ("_NET_WORKAREA")[2][desktop * 4: (desktop + 1) * 4]
47 self._move_begined = True
48 toplevel = self.get_toplevel ()
49 x, y = toplevel.get_position ()
50 self._press_pos = event.x_root - x, event.y_root - y
51 self.window.set_cursor (gdk.Cursor (gdk.FLEUR))
52 return True
53 return False
55 def do_button_release_event (self, event):
56 if event.button == 1:
57 self._move_begined = False
58 del self._press_pos
59 del self._workarea
60 self.window.set_cursor (gdk.Cursor (gdk.LEFT_PTR))
61 return True
63 return False
65 def do_motion_notify_event (self, event):
66 if not self._move_begined:
67 return
68 toplevel = self.get_toplevel ()
69 x, y = toplevel.get_position ()
70 x = int (event.x_root - self._press_pos[0])
71 y = int (event.y_root - self._press_pos[1])
73 if x < self._workarea[0] and x > self._workarea[0] - 16:
74 x = self._workarea[0]
75 if y < self._workarea[1] and y > self._workarea[1] - 16:
76 y = self._workarea[1]
78 w, h = toplevel.get_size ()
79 if x + w > self._workarea[0] + self._workarea[2] and \
80 x + w < self._workarea[0] + self._workarea[2] + 16:
81 x = self._workarea[0] + self._workarea[2] - w
82 if y + h > self._workarea[1] + self._workarea[3] and \
83 y + h < self._workarea[1] + self._workarea[3] + 16:
84 y = self._workarea[1] + self._workarea[3] - h
86 toplevel.move (x, y)
88 def do_expose_event (self, event):
89 self.style.paint_handle (
90 self.window,
91 gtk.STATE_NORMAL,
92 gtk.SHADOW_OUT,
93 event.area,
94 self,
95 "",
96 self.allocation.x, self.allocation.y,
97 10, self.allocation.height,
98 gtk.ORIENTATION_VERTICAL)
99 return True
101 gobject.type_register (Handle, "IBusHandle")