4 # Copyright (C) 2010 Stefanos Harhalakis
6 # This file is part of wifieye.
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.
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.
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/>.
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
28 from hildondesktop import *
34 from portrait import FremantleRotation
35 #from xdg.IconTheme import getIconPath
41 from icons import Icons
43 #def getIcon(name, iconsize):
44 # ico=getIconPath(name, iconsize)
45 # ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, iconsize, iconsize)
49 #class IconGrid(gtk.Widget, FremantleRotation):
50 class IconGrid(object): #(gobject.GObject):
51 def __init__(self, isconfig=False):
52 # self.__gobject_init__()
58 self.isconfig=isconfig
61 self.lasticon=None # The last icon that got selected
63 self.draw_pending=False
67 # If this is False then animations are forcefully disabled
68 self.do_animations=True
70 self.angle_timer_start=0
72 # Duration of the rotation effect
73 self.rotation_time=0.8
75 def do_realize(self, config):
78 self.icons=Icons(self.isconfig, self.config)
83 def connect(self, what, *args):
84 if what in icon.signals:
85 self.icons.connect(what, *args)
87 super(IconGrid, self).connect(what, *args)
89 def setSize(self, size):
91 self.icons.setSize(size)
94 ret=self.icons.getSize()
97 def setMode(self, mode):
103 if not isinstance(self, gtk.Widget):
109 v=self.get_property('is-on-current-desktop')
113 self.draw_pending=True
117 if do_draw and self.config.getAnimate() and self.do_animations:
119 # Don't start another timer
120 # Instead adjust the time start to produce a nice effect ;-)
121 if self.angle_timer_start==0:
122 self.angle_timer_start=time.time()
123 gobject.timeout_add(20, self.timerAngle)
125 dt=time.time()-self.angle_timer_start
126 da=90.0*dt/self.rotation_time
129 dt2=da2*self.rotation_time/90.0
130 self.angle_timer_start=time.time()-dt2
140 def disableAnimation(self):
141 self.do_animations=False
143 def enableAnimation(self):
144 self.do_animations=True
146 def setAnimationEnable(self, value):
148 self.enableAnimation()
150 self.disableAnimation()
152 def timerAngle(self):
153 if self.angle_timer_start==0:
154 self.angle_timer_start=time.time()-0.05
156 dt=time.time()-self.angle_timer_start
158 da=90.0*dt/self.rotation_time
174 if self.setAngle(angle):
178 self.angle_timer_start=0
182 def iconAt(self, x, y):
183 """ Get icon at coordinates x,y. X and Y are in pixels """
185 w=self.config.iconsize + self.config.iconspace
187 if self.mode=='l' or self.config.getIndiv():
191 x2=self.size[1] - int(y/w) - 1
199 ret=self.icons.get(x,y)
203 def _draw(self, cr, event):
204 self.draw_pending=False
206 w=self.config.iconsize + self.config.iconspace
207 for x,y in self.icons:
208 if self.mode=='l' or self.config.getIndiv():
209 x2=x * (self.config.iconsize + self.config.iconspace)
210 y2=y * (self.config.iconsize + self.config.iconspace)
212 x2=y * (self.config.iconsize + self.config.iconspace)
213 y2=(self.size[1]-x-1) * \
214 (self.config.iconsize + self.config.iconspace)
216 # Only repaint the needed icons
217 rect=gdk.Rectangle(x2, y2, w, w)
218 t=rect.intersect(event.area)
219 if t.width==0 and t.height==0:
222 ico=self.icons.get(x,y)
225 def setAngle(self, angle):
226 """ Return True/False indicating that angle has changed """
228 for x,y in self.icons:
229 ic=self.icons.get(x,y)
230 if ic.setAngle(angle):
235 def clearAnimationCache(self):
236 """ Clear animation cache, freeing memory """
237 for x,y in self.icons:
238 ic=self.icons.get(x,y)
239 ic.clearAnimationCache()
241 def do_expose_event(self, event):
242 cr=self.window.cairo_create()
244 cr.rectangle(event.area.x, event.area.y,
245 event.area.width, event.area.height)
249 if not self.init_done:
250 self.icons.setWindow(self.window)
253 self._draw(cr, event)
255 def setLastIcon(self, icon):
256 if icon==self.lasticon:
259 if self.lasticon!=None:
260 self.lasticon.doCancel()
261 self.lasticon.invalidate(self.window)
264 def do_button_press_event(self, event):
265 icon=self.iconAt(event.x, event.y)
268 # rect=gdk.Rectangle(event.x,event.y,1,1)
269 # rect=gdk.Rectangle(0, 0, 100, 100)
271 icon.invalidate(self.window)
272 self.setLastIcon(icon)
274 # gdk.Window.invalidate_rect(self.window, rect, True)
278 def do_button_release_event(self, event):
279 if self.lasticon!=None:
280 self.lasticon.invalidate(self.window)
281 self.lasticon.doRelease()
283 self.setLastIcon(None)
287 def do_leave_notify_event(self, event):
288 self.setLastIcon(None)
291 def do_pproperty_notify_event(self, event):
292 icon=self.iconAt(event.x, event.y)
296 icon.invalidate(self.window)
299 def do_motion_notify_event(self, event):
300 icon=self.iconAt(event.x, event.y)
301 if self.lasticon==icon:
304 self.setLastIcon(None)
306 icon.invalidate(self.window)
309 def do_button_press_event_old(self, event):
310 if event.type==gdk.BUTTON_PRESS:
319 def do_event1(self, event):
320 print "event:", event, event.type
322 def reloadIcons(self):
325 # def on_orientation_changed(self, orientation):
326 # print "orch:", orientation
330 class IconGridWidget(IconGrid, gtk.Widget):
331 def __init__(self, isconfig, config, animation=True):
332 IconGrid.__init__(self, isconfig)
333 gtk.Widget.__init__(self)
335 # This must be called before do_realize
336 self.setAnimationEnable(animation)
340 IconGrid.do_realize(self, self.config)
342 self.setSize(self.size)
344 def setSize(self, size):
345 IconGrid.setSize(self, size)
347 w=self.size[0] * (self.config.iconsize + self.config.iconspace)
348 h=self.size[1] * (self.config.iconsize + self.config.iconspace)
350 self.set_size_request(w, h)
352 def do_realize(self):
353 screen=self.get_screen()
354 self.set_colormap(screen.get_rgba_colormap())
355 self.set_app_paintable(True)
357 self.set_flags(self.flags() | gtk.REALIZED)
359 self.window=gdk.Window(
360 self.get_parent_window(),
361 width=self.allocation.width,
362 height=self.allocation.height,
363 window_type=gdk.WINDOW_CHILD,
364 wclass=gdk.INPUT_OUTPUT,
365 event_mask=self.get_events() | gdk.EXPOSURE_MASK
366 | gdk.BUTTON_PRESS_MASK
367 | gdk.BUTTON_RELEASE_MASK
368 | gdk.BUTTON_MOTION_MASK
369 | gdk.POINTER_MOTION_MASK
370 | gdk.POINTER_MOTION_HINT_MASK
371 | gdk.ENTER_NOTIFY_MASK
372 | gdk.LEAVE_NOTIFY_MASK )
374 self.window.set_user_data(self)
375 self.style.attach(self.window)
377 # self.style.set_background(self.window, gtk.STATE_NORMAL)
378 self.window.move_resize(*self.allocation)
380 # self.pixmap, mask = gtk.gdk.pixmap_create_from_xpm_d(
381 # self.window, self.style.bg[gtk.STATE_NORMAL], STAR_PIXMAP)
383 # self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
385 #gtk.Widget.do_realize(self)
386 #HomePluginItem.do_realize(self)
388 # screen=self.get_screen()
389 # self.set_colormap(screen.get_rgba_colormap())
390 # self.set_app_paintable(True)
392 def do_unrealize(self):
393 #self.window.set_user_data(None)
394 self.window.destroy()
396 #gobject.type_register(IconGrid)
397 gobject.type_register(IconGridWidget)
400 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent: