bb549492715fb61c6acf70439fd9293f3127ccd2
[platform/upstream/ibus.git] / ui / gtk2 / handle.py
1 # vim:set et sts=4 sw=4:
2 #
3 # ibus - The Input Bus
4 #
5 # Copyright(c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com>
6 # Copyright(c) 2007-2010 Red Hat, Inc.
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
21 # USA
22
23 import gtk
24 import gtk.gdk as gdk
25 import gobject
26
27 class Handle(gtk.EventBox):
28     __gtype_name__ = "IBusHandle"
29     __gsignals__ = {
30         "move-begin" : (
31             gobject.SIGNAL_RUN_LAST,
32             gobject.TYPE_NONE,
33             ()),
34         "move-end" : (
35             gobject.SIGNAL_RUN_LAST,
36             gobject.TYPE_NONE,
37             ()),
38         }
39
40     def __init__ (self):
41         super(Handle, self).__init__()
42         self.set_visible_window(False)
43         self.set_size_request(10, -1)
44         self.set_events(
45             gdk.EXPOSURE_MASK | \
46             gdk.BUTTON_PRESS_MASK | \
47             gdk.BUTTON_RELEASE_MASK | \
48             gdk.BUTTON1_MOTION_MASK)
49
50         self.__move_begined = False
51
52         root = gdk.get_default_root_window()
53
54     def do_button_press_event(self, event):
55         if event.button == 1:
56             root = gdk.get_default_root_window()
57             try:
58                 desktop = root.property_get("_NET_CURRENT_DESKTOP")[2][0]
59                 self.__workarea = root.property_get("_NET_WORKAREA")[2][desktop * 4: (desktop + 1) * 4]
60             except:
61                 self.__workarea = None
62             self.__move_begined = True
63             toplevel = self.get_toplevel()
64             x, y = toplevel.get_position()
65             self.__press_pos = event.x_root - x, event.y_root - y
66             self.window.set_cursor(gdk.Cursor(gdk.FLEUR))
67             self.emit("move-begin")
68             return True
69         return False
70
71     def do_button_release_event(self, event):
72         if event.button == 1:
73             self.__move_begined = False
74             del self.__press_pos
75             del self.__workarea
76             self.window.set_cursor(gdk.Cursor(gdk.LEFT_PTR))
77             self.emit("move-end")
78             return True
79
80         return False
81
82     def do_motion_notify_event(self, event):
83         if not self.__move_begined:
84             return
85         toplevel = self.get_toplevel()
86         x, y = toplevel.get_position()
87         x  = int(event.x_root - self.__press_pos[0])
88         y  = int(event.y_root - self.__press_pos[1])
89
90         if self.__workarea == None:
91             toplevel.move(x, y)
92             return
93
94         if x < self.__workarea[0] and x > self.__workarea[0] - 16:
95             x = self.__workarea[0]
96         if y < self.__workarea[1] and y > self.__workarea[1] - 16:
97             y = self.__workarea[1]
98
99         w, h = toplevel.get_size()
100         if x + w > self.__workarea[0] + self.__workarea[2] and \
101             x + w < self.__workarea[0] + self.__workarea[2] + 16:
102             x = self.__workarea[0] + self.__workarea[2] - w
103         if y + h > self.__workarea[1] + self.__workarea[3] and \
104             y + h < self.__workarea[1] + self.__workarea[3] + 16:
105             y =  self.__workarea[1] + self.__workarea[3] - h
106
107         toplevel.move(x, y)
108
109     def do_expose_event(self, event):
110         self.style.paint_handle(
111                     self.window,
112                     gtk.STATE_NORMAL,
113                     gtk.SHADOW_OUT,
114                     event.area,
115                     self,
116                     "",
117                     self.allocation.x, self.allocation.y, 
118                     10, self.allocation.height,
119                     gtk.ORIENTATION_VERTICAL)
120         return True
121