Multiple instance support.
[drlaunch] / src / icongrid.py
1 #!/usr/bin/env python
2 # coding=UTF-8
3
4 # Copyright (C) 2010 Stefanos Harhalakis
5 #
6 # This file is part of wifieye.
7 #
8 # wifieye is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # wifieye 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
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with wifieye.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
22
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
24
25 import gtk
26 import gobject
27 import hildon
28 from hildondesktop import *
29 from gtk import gdk
30 from math import pi
31 import cairo
32 import time
33
34 from portrait import FremantleRotation
35 #from xdg.IconTheme import getIconPath
36
37 #import config
38 import apps
39 import icon
40 from icon import Icon
41 from icons import Icons
42
43 #def getIcon(name, iconsize):
44 #    ico=getIconPath(name, iconsize)
45 #    ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, iconsize, iconsize)
46 #
47 #    return(ret)
48
49 #class IconGrid(gtk.Widget, FremantleRotation):
50 class IconGrid(object): #(gobject.GObject):
51     def __init__(self, isconfig=False):
52 #       self.__gobject_init__()
53
54         self.size=(0,0)
55
56         self.isconfig=isconfig
57
58         self.icons=None
59         self.lasticon=None  # The last icon that got selected
60
61     def do_realize(self, config):
62         self.config=config
63
64         self.icons=Icons(self.isconfig, self.config)
65         self.setMode('l')
66         self.setSize((4,4))
67         self.reloadIcons()
68
69     def connect(self, what, *args):
70         if what in icon.signals:
71             self.icons.connect(what, *args)
72         else:
73             super(IconGrid, self).connect(what, *args)
74
75     def setSize(self, size):
76         self.size=size
77         self.icons.setSize(size)
78
79     def getSize(self):
80         ret=self.icons.getSize()
81         return(ret)
82
83     def setMode(self, mode):
84         self.mode=mode
85         if isinstance(self, gtk.Widget):
86             self.queue_draw()
87
88     def iconAt(self, x, y):
89         """ Get icon at coordinates x,y. X and Y are in pixels """
90
91         w=self.config.iconsize + self.config.iconspace
92
93         if self.mode=='l' or self.config.getIndiv():
94             x2=int(x / w)
95             y2=int(y / w)
96         else:
97             x2=self.size[1] - int(y/w) - 1
98             y2=int(x/w)
99
100         ret=self.get(x2,y2)
101
102         return(ret)
103
104     def get(self, x, y):
105         ret=self.icons.get(x,y)
106
107         return(ret)
108
109     def _draw(self, cr, event):
110         w=self.config.iconsize + self.config.iconspace
111         for x,y in self.icons:
112             if self.mode=='l' or self.config.getIndiv():
113                 x2=x * (self.config.iconsize + self.config.iconspace)
114                 y2=y * (self.config.iconsize + self.config.iconspace)
115             else:
116                 x2=y * (self.config.iconsize + self.config.iconspace)
117                 y2=(self.size[1]-x-1) * \
118                         (self.config.iconsize + self.config.iconspace)
119
120             # Only repaint the needed icons
121             rect=gdk.Rectangle(x2, y2, w, w)
122             t=rect.intersect(event.area)
123             if t.width==0 and t.height==0:
124                 continue
125
126             ico=self.icons.get(x,y)
127             ico.draw(cr, x2, y2, self.mode)
128
129     def do_expose_event(self, event):
130         cr=self.window.cairo_create()
131
132         cr.rectangle(event.area.x, event.area.y,
133             event.area.width, event.area.height)
134
135         cr.clip()
136
137         self._draw(cr, event)
138
139     def setLastIcon(self, icon):
140         if icon==self.lasticon:
141             return
142
143         if self.lasticon!=None:
144             self.lasticon.doCancel()
145             self.lasticon.invalidate(self.window)
146         self.lasticon=icon
147
148     def do_button_press_event(self, event):
149         icon=self.iconAt(event.x, event.y)
150         if icon==None:
151             return
152 #       rect=gdk.Rectangle(event.x,event.y,1,1)
153 #       rect=gdk.Rectangle(0, 0, 100, 100)
154         icon.doPress()
155         icon.invalidate(self.window)
156         self.setLastIcon(icon)
157
158 #       gdk.Window.invalidate_rect(self.window, rect, True)
159
160         return(True)
161
162     def do_button_release_event(self, event):
163         if self.lasticon!=None:
164             self.lasticon.invalidate(self.window)
165             self.lasticon.doRelease()
166
167         self.setLastIcon(None)
168
169         return(True)
170
171     def do_leave_notify_event(self, event):
172         self.setLastIcon(None)
173         return(True)
174
175     def do_pproperty_notify_event(self, event):
176         icon=self.iconAt(event.x, event.y)
177         if icon==None:
178             return
179         icon.doCancel()
180         icon.invalidate(self.window)
181         return(True)
182
183     def do_motion_notify_event(self, event):
184         icon=self.iconAt(event.x, event.y)
185         if self.lasticon==icon:
186             return(True)
187
188         self.setLastIcon(None)
189         icon.doCancel()
190         icon.invalidate(self.window)
191         return(True)
192
193     def do_button_press_event_old(self, event):
194         if event.type==gdk.BUTTON_PRESS:
195             if self.mode=='p':
196                 self.setMode('l')
197             else:
198                 self.setMode('p')
199             self.queue_draw()
200         return True
201
202     # For debugging
203     def do_event1(self, event):
204         print "event:", event, event.type
205
206     def reloadIcons(self):
207         self.icons.load()
208
209 #    def on_orientation_changed(self, orientation):
210 #       print "orch:", orientation
211 #       o=orientation[0]
212 #       self.setMode(o)
213
214 class IconGridWidget(IconGrid, gtk.Widget):
215     def __init__(self, isconfig, config):
216         IconGrid.__init__(self, isconfig)
217         gtk.Widget.__init__(self)
218
219         self.config=config
220
221         IconGrid.do_realize(self, self.config)
222
223         if isconfig:
224             maxsz=self.config.getMaxSize()
225             w=maxsz[0] * (self.config.iconsize + self.config.iconspace)
226             h=maxsz[1] * (self.config.iconsize + self.config.iconspace)
227         else:
228             w=self.size[0] * (self.config.iconsize + self.config.iconspace)
229             h=self.size[1] * (self.config.iconsize + self.config.iconspace)
230
231         self.set_size_request(w, h)
232
233     def do_realize(self):
234         screen=self.get_screen()
235         self.set_colormap(screen.get_rgba_colormap())
236         self.set_app_paintable(True)
237
238         self.set_flags(self.flags() | gtk.REALIZED)
239
240         self.window=gdk.Window(
241             self.get_parent_window(),
242             width=self.allocation.width,
243             height=self.allocation.height,
244             window_type=gdk.WINDOW_CHILD,
245             wclass=gdk.INPUT_OUTPUT,
246             event_mask=self.get_events() | gdk.EXPOSURE_MASK
247                 | gdk.BUTTON_PRESS_MASK 
248                 | gdk.BUTTON_RELEASE_MASK 
249                 | gdk.BUTTON_MOTION_MASK
250                 | gdk.POINTER_MOTION_MASK
251                 | gdk.POINTER_MOTION_HINT_MASK 
252                 | gdk.ENTER_NOTIFY_MASK
253                 | gdk.LEAVE_NOTIFY_MASK )
254
255         self.window.set_user_data(self)
256         self.style.attach(self.window)
257
258 #       self.style.set_background(self.window, gtk.STATE_NORMAL)
259         self.window.move_resize(*self.allocation)
260
261 #       self.pixmap, mask = gtk.gdk.pixmap_create_from_xpm_d(
262 #             self.window, self.style.bg[gtk.STATE_NORMAL], STAR_PIXMAP)
263         
264 #       self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
265
266         #gtk.Widget.do_realize(self)
267         #HomePluginItem.do_realize(self)
268
269 #       screen=self.get_screen()
270 #       self.set_colormap(screen.get_rgba_colormap())
271 #       self.set_app_paintable(True)
272
273     def do_unrealize(self):
274         #self.window.set_user_data(None)
275         self.window.destroy()
276
277 #gobject.type_register(IconGrid)
278 gobject.type_register(IconGridWidget)
279
280
281 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
282