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