Configurable focus size
[gregoa/zavai.git] / src / fisheye.vala
1 /*
2  * zavai - simple interface to the OpenMoko (or to the FSO stack)
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 using GLib;
22
23 public class FisheyeList : Gtk.DrawingArea
24 {
25         protected Gtk.TreeModel model;
26         protected Gdk.Pixmap backing_store;
27
28         private string[] list;
29         protected int cur_el;
30         protected double distortion_factor;
31
32         protected Pango.Layout[] pango_cache;
33
34         // Number of items shown before and after the focus element
35         protected int focus_first;
36         protected int focus_end;
37         protected int[] focus_starts;
38         protected bool focus_locked;
39         protected int focus_centre;
40         protected bool focus_layout_needed;
41
42         protected int _focus_size;
43         public int focus_size {
44                 get { return _focus_size; }
45                 set {
46                         _focus_size = value;
47                         focus_starts = new int[value+1];
48                         focus_layout_needed = true;
49                 }
50         }
51
52         public FisheyeList()
53         {
54                 model = null;
55                 backing_store = null;
56
57                 list = new string[300];
58                 for (int i = 0; i < 300; ++i)
59                         list[i] = "Lorem Ipsum %d".printf(i);
60
61                 pango_cache = new Pango.Layout[30];
62                 for (int i = 0; i < 30; ++i)
63                         pango_cache[i] = null;
64
65                 cur_el = 0;
66                 focus_centre = 0;
67                 focus_size = 20;
68                 focus_locked = false;
69                 distortion_factor = 30;
70
71                 add_events(Gdk.EventMask.POINTER_MOTION_MASK
72                          | Gdk.EventMask.BUTTON_PRESS_MASK
73                          | Gdk.EventMask.BUTTON_RELEASE_MASK);
74         }
75
76         public unowned Gtk.TreeModel get_model() { return model; }
77         public void set_model (Gtk.TreeModel? model)
78         {
79                 this.model = model;
80         }
81
82         /* Mouse button got pressed over widget */
83         public override bool button_press_event(Gdk.EventButton event)
84         {
85                 stderr.printf("Mouse pressed on %d %s\n", cur_el, list[cur_el]);
86                 return false;
87         }
88
89         /* Mouse button got released */
90         public override bool button_release_event(Gdk.EventButton event)
91         {
92                 stderr.printf("Mouse released on %d %s\n", cur_el, list[cur_el]);
93                 // ...
94                 return false;
95         }
96
97         /* Mouse pointer moved over widget */
98         public override bool motion_notify_event(Gdk.EventMotion event)
99         {
100                 int old_cur_el = cur_el;
101                 int x = (int)event.x;
102                 int y = (int)event.y;
103
104                 focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
105
106                 if (focus_locked)
107                 {
108                         for (int idx = focus_first; idx < focus_end; ++idx)
109                                 if (y < focus_starts[idx-focus_first+1])
110                                 {
111                                         cur_el = idx;
112                                         break;
113                                 }
114
115                 } else {
116                         cur_el = y * list.length / allocation.height;
117                         if (old_cur_el != cur_el)
118                                 focus_layout_needed = true;
119                 }
120
121                 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
122                 if (old_cur_el != cur_el)
123                 {
124                         queue_draw();
125                         old_cur_el = cur_el;
126                 }
127                 return false;
128         }
129
130         public override bool configure_event (Gdk.EventConfigure event)
131         {
132                 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
133                 focus_layout_needed = true;
134                 queue_draw();
135                 return false;
136         }
137
138         /* Widget is asked to draw itself */
139         public override bool expose_event (Gdk.EventExpose event)
140         {
141                 if (backing_store == null)
142                         return false;
143
144                 draw(backing_store);
145
146                 window.draw_drawable(
147                         get_style().fg_gc[Gtk.StateType.NORMAL],
148                         backing_store,
149                         event.area.x, event.area.y,
150                         event.area.x, event.area.y,
151                         event.area.width, event.area.height);
152
153                 return false;
154         }
155
156         protected int el_y(int idx)
157         {
158                 // Undistorted Y
159                 int undy = idx * allocation.height / list.length;
160                 // Distorted position
161                 int pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
162                 //stderr.printf("%d %f %f\n", idx, undy, pos);
163                 return pos;
164         }
165
166         protected void focus_layout()
167         {
168                 // Anchor point
169                 focus_centre = cur_el*allocation.height/list.length;
170
171                 focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
172                 focus_end = focus_first + focus_size;
173                 if (focus_end >= list.length) focus_end = list.length;
174
175                 // Compute starting positions for all items in focus
176                 for (int idx = focus_first; idx < focus_end; ++idx)
177                 {
178                         int posprev = idx == 0 ? 0 : el_y(idx-1);
179                         int pos = el_y(idx);
180                         int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
181                         int y0 = (pos+posprev)/2;
182                         int y1 = (pos+posnext)/2;
183
184                         focus_starts[idx - focus_first] = y0;
185                         focus_starts[idx - focus_first + 1] = y1;
186                 }
187                 focus_layout_needed = false;
188         }
189
190         protected void draw(Gdk.Drawable drawable)
191         {
192                 if (focus_layout_needed)
193                         focus_layout();
194
195                 Gtk.Style style = get_style();
196
197                 // Background
198                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
199
200                 // Focus lock area
201                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
202                         0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
203
204                 // Focus movement area
205                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
206                         allocation.width/2, 0, allocation.width, allocation.height);
207
208                 // Create a Cairo context
209                 //var context = Gdk.cairo_create (drawable);
210
211                 // Paint items around focus
212                 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
213                 for (int idx = focus_first; idx < focus_end; ++idx)
214                 {
215                         int y0 = focus_starts[idx - focus_first];
216                         int y1 = focus_starts[idx - focus_first + 1];
217
218                         Gtk.StateType itemState = Gtk.StateType.NORMAL;
219                         if (idx == cur_el)
220                         {
221                                 itemState = Gtk.StateType.SELECTED;
222                                 drawable.draw_rectangle(style.bg_gc[itemState], true,
223                                         0, y0, allocation.width, y1-y0);
224                         }
225
226                 
227                         // TODO: cache pango contexts instead of fontdescs
228                         int size = (y1-y0)*80/100;
229                         if (size <= 0) size = 1;
230                         if (size >= pango_cache.length) size = pango_cache.length - 1;
231                         if (pango_cache[size] == null)
232                         {
233                                 var fd = style.font_desc.copy();
234                                 fd.set_absolute_size(size*Pango.SCALE);
235                                 var pc = create_pango_context();
236                                 pc.set_font_description(fd);
237                                 pango_cache[size] = new Pango.Layout(pc);
238                         }
239                         pango_cache[size].set_text(list[idx], -1);
240                         var layout = pango_cache[size];
241                         //stderr.printf("AZAZA %p\n", layout.get_attributes());
242                         //var attrlist = layout.get_attributes().copy();
243                         //stderr.printf("AL %p\n", attrlist);
244                         //var attrlist = new Pango.AttrList();
245                         //stderr.printf("SIZE %d\n", y1-y0);
246                         //attrlist.insert(new Pango.AttrSize(y1-y0));
247                         //var attrlist = layout.get_attributes();
248                         //attrlist.change(new Pango.AttrSize(y1-y0));
249                         //layout.set_attributes(attrlist);
250                         //layout.set_height(y1-y0);
251                         //int w, h;
252                         //layout.get_pixel_size(out w, out h);
253                         Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
254                 }
255         }
256
257     /*
258      * The following function is adapted from Prefuse's FisheyeDistortion.java.
259      *
260      * A relevant annotation from Prefuse:
261      *
262      * For more details on this form of transformation, see Manojit Sarkar and 
263      * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
264      * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
265      * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
266      * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
267      */
268
269     /*
270      * Distorts an item's coordinate.
271      * @param x the undistorted coordinate
272      * @param coordinate of the anchor or focus point
273      * @param d disortion factor
274      * @param min the beginning of the display
275      * @param max the end of the display
276      * @return the distorted coordinate
277      */
278     private int fisheye(int x, int a, double d, int min, int max)
279     {
280         if ( d != 0 ) {
281             bool left = x<a;
282             double v;
283             int m = (left ? a-min : max-a);
284             if ( m == 0 ) m = max-min;
285             v = (double)(x - a).abs() / m;
286             v = (d+1)/(d+(1/v));
287             return (int)Math.round((left?-1:1)*m*v + a);
288         } else {
289             return x;
290         }
291     }
292 }
293
294 public class Fisheye : Gtk.Window
295 {
296         public Fisheye()
297         {
298                 title = "Fisheye";
299                 destroy += Gtk.main_quit;
300
301                 var list = new FisheyeList();
302                 add(list);
303         }
304 }
305
306 static int main (string[] args) {
307         Gtk.init (ref args);
308
309         var fe = new Fisheye();
310         fe.set_size_request(200, 300);
311         fe.show_all();
312
313         Gtk.main();
314
315         return 0;
316 }