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
37 from config import dump
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 # IconGrid is the main class that implements tha drawing of the grid
50 # However, since it will be used both by the desktop plugin and the
51 # configuration window, it cannot derive either gtk.Widget or HomePluginItem.
52 # It is created here in a way that will allow it to work on both cases
53 # and it is inherited by appropriate classes (one for the plugin and one
54 # for the config widget)
56 #class IconGrid(gtk.Widget, FremantleRotation):
57 class IconGrid: #(gobject.GObject):
58 def __init__(self, isconfig=False):
59 # self.__gobject_init__()
60 # gtk.Widget.__init__(self)
66 self.isconfig=isconfig
69 self.lasticon=None # The last icon that got selected
71 self.draw_pending=False
75 # If this is False then animations are forcefully disabled
76 self.do_animations=True
78 self.angle_timer_start=0
80 # Duration of the rotation effect
81 self.rotation_time=0.8
88 def do_realize(self, config):
96 print "WTF??????????????????????"
101 self.icons=Icons(self.isconfig, self.config)
103 #self.icons.set_parent(self)
105 self.setSize(config.getMaxSize())
108 def do_unrealize(self):
109 # print "ig-unrealize"
115 def connect(self, what, *args):
116 if what in Icon.gsignals.keys():
117 ret=self.icons.connect(what, *args)
119 ret=gobject.GObject.connect(self, what, *args)
120 #ret=super(IconGrid, self).connect(what, *args)
124 def setSize(self, size):
126 self.icons.setSize(size)
129 ret=self.icons.getSize()
132 def setMode(self, mode):
138 if not isinstance(self, gtk.Widget):
144 v=self.get_property('is-on-current-desktop')
148 self.draw_pending=True
152 if do_draw and self.config.getAnimate() and self.do_animations:
154 # Don't start another timer
155 # Instead adjust the time start to produce a nice effect ;-)
156 if self.angle_timer_start==0:
157 self.angle_timer_start=time.time()
158 gobject.timeout_add(20, self.timerAngle)
160 dt=time.time()-self.angle_timer_start
161 da=90.0*dt/self.rotation_time
164 dt2=da2*self.rotation_time/90.0
165 self.angle_timer_start=time.time()-dt2
175 def disableAnimation(self):
176 self.do_animations=False
178 def enableAnimation(self):
179 self.do_animations=True
181 def setAnimationEnable(self, value):
183 self.enableAnimation()
185 self.disableAnimation()
187 def timerAngle(self):
188 if self.angle_timer_start==0:
189 self.angle_timer_start=time.time()-0.05
191 dt=time.time()-self.angle_timer_start
193 da=90.0*dt/self.rotation_time
209 if self.setAngle(angle):
213 self.angle_timer_start=0
217 def iconAt(self, x, y):
218 """ Get icon at coordinates x,y. X and Y are in pixels """
220 w=self.config.getIconSizeFull()
222 if self.mode=='l' or self.config.getIndiv():
226 x2=self.size[1] - int(y/w) - 1
234 ret=self.icons.get(x,y)
238 def _draw(self, cr, event):
239 self.draw_pending=False
241 w=self.config.getIconSizeFull()
242 for x,y in self.icons:
243 if self.mode=='l' or self.config.getIndiv():
244 #x2=x * (self.config.iconsize + self.config.iconspace)
245 #y2=y * (self.config.iconsize + self.config.iconspace)
246 x2=x * self.config.getIconSizeFull()
247 y2=y * self.config.getIconSizeFull()
249 #x2=y * (self.config.iconsize + self.config.iconspace)
250 #y2=(self.size[1]-x-1) * \
251 # (self.config.iconsize + self.config.iconspace)
252 x2=y * self.config.getIconSizeFull()
253 y2=(self.size[1]-x-1) * self.config.getIconSizeFull()
255 # Only repaint the needed icons
256 rect=gdk.Rectangle(x2, y2, w, w)
257 t=rect.intersect(event.area)
258 if t.width==0 and t.height==0:
261 ico=self.icons.get(x,y)
264 def setAngle(self, angle):
265 """ Return True/False indicating that angle has changed """
267 for x,y in self.icons:
268 ic=self.icons.get(x,y)
269 if ic.setAngle(angle):
274 def clearAnimationCache(self):
275 """ Clear animation cache, freeing memory """
276 for x,y in self.icons:
277 ic=self.icons.get(x,y)
278 ic.clearAnimationCache()
280 def clearBgCache(self):
281 """ Clear backgrounds cache """
282 for x,y in self.icons:
283 ic=self.icons.get(x,y)
286 def do_expose_event(self, event):
287 cr=self.window.cairo_create()
289 cr.rectangle(event.area.x, event.area.y,
290 event.area.width, event.area.height)
294 if not self.init_done:
295 self.icons.setWindow(self.window)
298 self._draw(cr, event)
300 def setLastIcon(self, icon):
301 if icon==self.lasticon:
304 if self.lasticon!=None:
305 self.lasticon.doCancel()
306 self.lasticon.invalidate(self.window)
309 def do_button_press_event(self, event):
310 icon=self.iconAt(event.x, event.y)
313 # rect=gdk.Rectangle(event.x,event.y,1,1)
314 # rect=gdk.Rectangle(0, 0, 100, 100)
316 icon.invalidate(self.window)
317 self.setLastIcon(icon)
319 # gdk.Window.invalidate_rect(self.window, rect, True)
323 def do_button_release_event(self, event):
324 if self.lasticon!=None:
325 self.lasticon.invalidate(self.window)
326 self.lasticon.doRelease()
328 self.setLastIcon(None)
332 def do_leave_notify_event(self, event):
333 self.setLastIcon(None)
336 def do_pproperty_notify_event(self, event):
337 icon=self.iconAt(event.x, event.y)
341 icon.invalidate(self.window)
344 def do_motion_notify_event(self, event):
345 icon=self.iconAt(event.x, event.y)
346 if self.lasticon==icon:
349 self.setLastIcon(None)
351 icon.invalidate(self.window)
354 def do_button_press_event_old(self, event):
355 if event.type==gdk.BUTTON_PRESS:
364 def do_event1(self, event):
365 print "event:", event, event.type
367 def reloadIcons(self):
371 # def on_orientation_changed(self, orientation):
372 # print "orch:", orientation
376 class IconGridWidget(IconGrid, gtk.Widget):
377 def __init__(self, isconfig, config, animation=True):
378 IconGrid.__init__(self, isconfig)
379 gtk.Widget.__init__(self)
381 # This must be called before do_realize
382 self.setAnimationEnable(animation)
386 IconGrid.do_realize(self, self.config)
388 self.setSize(self.size)
395 def setSize(self, size):
396 IconGrid.setSize(self, size)
398 w=self.size[0] * self.config.getIconSizeFull()
399 h=self.size[1] * self.config.getIconSizeFull()
401 self.set_size_request(w, h)
405 self.clearAnimationCache()
407 self.setSize(self.size)
408 self.icons.resizeMax()
411 def do_realize(self):
412 # print "igw-realize"
413 screen=self.get_screen()
414 self.set_colormap(screen.get_rgba_colormap())
415 self.set_app_paintable(True)
417 self.set_flags(self.flags() | gtk.REALIZED)
419 self.window=gdk.Window(
420 self.get_parent_window(),
421 width=self.allocation.width,
422 height=self.allocation.height,
423 window_type=gdk.WINDOW_CHILD,
424 wclass=gdk.INPUT_OUTPUT,
425 event_mask=self.get_events() | gdk.EXPOSURE_MASK
426 | gdk.BUTTON_PRESS_MASK
427 | gdk.BUTTON_RELEASE_MASK
428 | gdk.BUTTON_MOTION_MASK
429 | gdk.POINTER_MOTION_MASK
430 | gdk.POINTER_MOTION_HINT_MASK
431 | gdk.ENTER_NOTIFY_MASK
432 | gdk.LEAVE_NOTIFY_MASK )
434 self.window.set_user_data(self)
436 # No matter what the pygtk widget demo says, this is NOT CORRECT!!!
437 # Don't call style.attach(self.window) or else the program will crash
438 # after some time! It seems that there is a style already.
439 # If we want to use the style the we use get_style() instead.
440 # This one was very hard to solve. Thanks to gnome2-globalmenu guys.
441 # It was solved by looking the commit 2666:
443 # svn diff -r2665:2666 http://gnome2-globalmenu.googlecode.com/svn/trunk
444 # which solved cse 490:
445 # http://code.google.com/p/gnome2-globalmenu/issues/detail?id=490
446 # self.style.attach(self.window)
447 style=self.get_style()
449 # style.set_background(self.window, gtk.STATE_NORMAL)
450 self.window.move_resize(*self.allocation)
452 # self.pixmap, mask = gtk.gdk.pixmap_create_from_xpm_d(
453 # self.window, self.style.bg[gtk.STATE_NORMAL], STAR_PIXMAP)
455 # self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
457 #HomePluginItem.do_realize(self)
459 # screen=self.get_screen()
460 # self.set_colormap(screen.get_rgba_colormap())
461 # self.set_app_paintable(True)
463 def do_unrealize(self):
464 # print "igw-unrealize", self
465 IconGrid.do_unrealize(self)
466 self.window.destroy()
467 self.window.set_user_data(None)
469 def do_expose_event(self, event):
470 cr=self.window.cairo_create()
472 cr.rectangle(event.area.x, event.area.y,
473 event.area.width, event.area.height)
476 style=self.get_style()
477 col=style.bg[gtk.STATE_NORMAL]
478 cr.set_source_color(col)
481 IconGrid.do_expose_event(self, event)
483 #gobject.type_register(IconGrid)
484 gobject.type_register(IconGridWidget)
487 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent: