(no commit message)
[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):
44     ico=getIconPath(name, config.iconsize)
45     ret=gtk.gdk.pixbuf_new_from_file_at_size(ico, config.iconsize,
46         config.iconsize)
47
48     return(ret)
49
50 #class IconGrid(gtk.Widget, FremantleRotation):
51 class IconGrid(object): #(gobject.GObject):
52     def __init__(self, isconfig=False):
53 #       self.__gobject_init__()
54
55         self.size=0
56
57         self.isconfig=isconfig
58
59         self.icons=Icons(isconfig)
60
61         self.setMode('l')
62
63         # Maybe fix those:
64 #       w=(config.getSize() * config.iconsize) + \
65 #           (config.getSize()) * config.iconspace
66         #self.set_size_request(w, w)
67
68 #       self.setSize(config.getSize())
69         self.setSize(4)
70
71         self.lasticon=None  # The last icon that got selected
72
73         self.reloadIcons()
74
75     def connect(self, what, *args):
76         if what in icon.signals:
77             self.icons.connect(what, *args)
78         else:
79             super(IconGrid, self).connect(what, *args)
80
81     def setSize(self, size):
82         print "igw::setSize", size
83 #       config.setSize(size)
84         self.size=size
85         self.icons.setSize(size)
86
87     def setMode(self, mode):
88         self.mode=mode
89         if isinstance(self, gtk.Widget):
90             self.queue_draw()
91
92     def iconAt(self, x, y):
93         """ Get icon at coordinates x,y. X and Y are in pixels """
94
95         w=config.iconsize + config.iconspace
96
97         if self.mode=='l':
98             x2=int(x / w)
99             y2=int(y / w)
100         else:
101             x2=self.size - int(y/w) - 1
102             y2=int(x/w)
103
104         print "x2,y2", x2, y2
105         ret=self.get(x2,y2)
106
107         return(ret)
108
109     def get(self, x, y):
110         ret=self.icons.get(x,y)
111
112         return(ret)
113
114     def _draw(self, cr, event):
115 #       print "mode:", self.mode
116 #       print "icons", len(self.icons)
117
118         w=config.iconsize + config.iconspace
119         for x,y in self.icons:
120 #           print x, y
121
122             if self.mode=='l':
123                 x2=x * (config.iconsize + config.iconspace)
124                 y2=y * (config.iconsize + config.iconspace)
125             else:
126                 x2=y * (config.iconsize + config.iconspace)
127                 y2=(self.size-x-1) * (config.iconsize + config.iconspace)
128
129             # Only repaint the needed icons
130             rect=gdk.Rectangle(x2, y2, w, w)
131             t=rect.intersect(event.area)
132             if t.width==0 and t.height==0:
133                 continue
134
135 #           print "draw:", x, y
136             ico=self.icons.get(x,y)
137             ico.draw(cr, x2, y2, self.mode)
138
139     def do_expose_event(self, event):
140         cr=self.window.cairo_create()
141
142         cr.rectangle(event.area.x, event.area.y,
143             event.area.width, event.area.height)
144
145         cr.clip()
146
147         self._draw(cr, event)
148
149     def setLastIcon(self, icon):
150         if icon==self.lasticon:
151             return
152
153         if self.lasticon!=None:
154             self.lasticon.doCancel()
155             self.lasticon.invalidate(self.window)
156         self.lasticon=icon
157
158     def do_button_press_event(self, event):
159         print "press", event.type
160         icon=self.iconAt(event.x, event.y)
161         if icon==None:
162             return
163 #       rect=gdk.Rectangle(event.x,event.y,1,1)
164 #       rect=gdk.Rectangle(0, 0, 100, 100)
165         icon.doPress()
166         icon.invalidate(self.window)
167         self.setLastIcon(icon)
168
169 #       gdk.Window.invalidate_rect(self.window, rect, True)
170
171         return(True)
172
173     def do_button_release_event(self, event):
174         print "release"
175         if self.lasticon!=None:
176             self.lasticon.invalidate(self.window)
177             self.lasticon.doRelease()
178
179         self.setLastIcon(None)
180
181         return(True)
182
183     def do_leave_notify_event(self, event):
184         print "leave"
185         #print "leave", event.x, event.y
186         self.setLastIcon(None)
187         return(True)
188
189     def do_pproperty_notify_event(self, event):
190         print "property"
191         icon=self.iconAt(event.x, event.y)
192         if icon==None:
193             return
194         icon.doCancel()
195         icon.invalidate(self.window)
196         return(True)
197
198     def do_motion_notify_event(self, event):
199         print "motion"
200         icon=self.iconAt(event.x, event.y)
201         if self.lasticon==icon:
202             return(True)
203
204         self.setLastIcon(None)
205         icon.doCancel()
206         icon.invalidate(self.window)
207         return(True)
208
209     def do_button_press_event_old(self, event):
210         #print "press"
211         if event.type==gdk.BUTTON_PRESS:
212             print "press", event.type
213             if self.mode=='p':
214                 self.setMode('l')
215             else:
216                 self.setMode('p')
217             self.queue_draw()
218         return True
219
220     # For debugging
221     def do_event1(self, event):
222         print "event:", event, event.type
223
224     def butTest(self, arg):
225         print "but", arg
226
227     def reloadIcons(self):
228         self.icons.load()
229
230 #    def on_orientation_changed(self, orientation):
231 #       print "orch:", orientation
232 #       o=orientation[0]
233 #       self.setMode(o)
234
235 class IconGridWidget(IconGrid, gtk.Widget):
236     def __init__(self, isconfig):
237         IconGrid.__init__(self, isconfig)
238         gtk.Widget.__init__(self)
239
240         if isconfig:
241             w=4 * (config.iconsize + config.iconspace)
242         else:
243             w=self.size * (config.iconsize + config.iconspace)
244
245         self.set_size_request(w, w)
246
247     def do_realize(self):
248         screen=self.get_screen()
249         self.set_colormap(screen.get_rgba_colormap())
250         self.set_app_paintable(True)
251
252         self.set_flags(self.flags() | gtk.REALIZED)
253
254         self.window=gdk.Window(
255             self.get_parent_window(),
256             width=self.allocation.width,
257             height=self.allocation.height,
258             window_type=gdk.WINDOW_CHILD,
259             wclass=gdk.INPUT_OUTPUT,
260             event_mask=self.get_events() | gdk.EXPOSURE_MASK
261                 | gdk.BUTTON_PRESS_MASK 
262                 | gdk.BUTTON_RELEASE_MASK 
263                 | gdk.BUTTON_MOTION_MASK
264                 | gdk.POINTER_MOTION_MASK
265                 | gdk.POINTER_MOTION_HINT_MASK 
266                 | gdk.ENTER_NOTIFY_MASK
267                 | gdk.LEAVE_NOTIFY_MASK )
268
269         self.window.set_user_data(self)
270         self.style.attach(self.window)
271
272 #       self.style.set_background(self.window, gtk.STATE_NORMAL)
273         self.window.move_resize(*self.allocation)
274
275 #       self.pixmap, mask = gtk.gdk.pixmap_create_from_xpm_d(
276 #             self.window, self.style.bg[gtk.STATE_NORMAL], STAR_PIXMAP)
277         
278 #       self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
279
280         #gtk.Widget.do_realize(self)
281         #HomePluginItem.do_realize(self)
282
283 #       screen=self.get_screen()
284 #       self.set_colormap(screen.get_rgba_colormap())
285 #       self.set_app_paintable(True)
286
287     def do_unrealize(self):
288         #self.window.set_user_data(None)
289         self.window.destroy()
290
291 #gobject.type_register(IconGrid)
292 gobject.type_register(IconGridWidget)
293
294
295 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
296