Configurable distortion factor
[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
31         protected int _distortion_factor;
32         public int distortion_factor {
33                 get { return _distortion_factor; }
34                 set {
35                         _distortion_factor = value;
36                         focus_layout_needed = true;
37                 }
38         }
39
40
41         // Pango layouts cached for speed
42         // TODO: If you create and keep a PangoLayout using this context, you
43         // must deal with changes to the context by calling
44         // pango_layout_context_changed() on the layout in response to the
45         // "style-set" and "direction-changed" signals for the widget.
46         protected Pango.Layout[] pango_cache;
47
48         // Layout information
49         protected int focus_first;
50         protected int focus_end;
51         protected int[] focus_starts;
52         protected bool focus_locked;
53         protected int focus_centre;
54         protected bool focus_layout_needed;
55
56         protected int _focus_size;
57         public int focus_size {
58                 get { return _focus_size; }
59                 set {
60                         _focus_size = value;
61                         focus_starts = new int[value+1];
62                         focus_layout_needed = true;
63                 }
64         }
65
66         public FisheyeList()
67         {
68                 model = null;
69                 backing_store = null;
70
71                 list = new string[300];
72                 for (int i = 0; i < 300; ++i)
73                         list[i] = "Lorem Ipsum %d".printf(i);
74
75                 pango_cache = new Pango.Layout[30];
76                 for (int i = 0; i < 30; ++i)
77                         pango_cache[i] = null;
78
79                 // Defaults for properties
80                 focus_size = 20;
81                 distortion_factor = 30;
82
83                 cur_el = 0;
84                 focus_centre = 0;
85                 focus_locked = false;
86
87                 add_events(Gdk.EventMask.POINTER_MOTION_MASK
88                          | Gdk.EventMask.BUTTON_PRESS_MASK
89                          | Gdk.EventMask.BUTTON_RELEASE_MASK);
90         }
91
92         public unowned Gtk.TreeModel get_model() { return model; }
93         public void set_model (Gtk.TreeModel? model)
94         {
95                 this.model = model;
96         }
97
98         /* Mouse button got pressed over widget */
99         public override bool button_press_event(Gdk.EventButton event)
100         {
101                 stderr.printf("Mouse pressed on %d %s\n", cur_el, list[cur_el]);
102                 return false;
103         }
104
105         /* Mouse button got released */
106         public override bool button_release_event(Gdk.EventButton event)
107         {
108                 stderr.printf("Mouse released on %d %s\n", cur_el, list[cur_el]);
109                 // ...
110                 return false;
111         }
112
113         /* Mouse pointer moved over widget */
114         public override bool motion_notify_event(Gdk.EventMotion event)
115         {
116                 int old_cur_el = cur_el;
117                 int x = (int)event.x;
118                 int y = (int)event.y;
119
120                 focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
121
122                 if (focus_locked)
123                 {
124                         for (int idx = focus_first; idx < focus_end; ++idx)
125                                 if (y < focus_starts[idx-focus_first+1])
126                                 {
127                                         cur_el = idx;
128                                         break;
129                                 }
130
131                 } else {
132                         cur_el = y * list.length / allocation.height;
133                         if (old_cur_el != cur_el)
134                                 focus_layout_needed = true;
135                 }
136
137                 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
138                 if (old_cur_el != cur_el)
139                 {
140                         queue_draw();
141                         old_cur_el = cur_el;
142                 }
143                 return false;
144         }
145
146         public override bool configure_event (Gdk.EventConfigure event)
147         {
148                 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
149                 focus_layout_needed = true;
150                 queue_draw();
151                 return false;
152         }
153
154         /* Widget is asked to draw itself */
155         public override bool expose_event (Gdk.EventExpose event)
156         {
157                 if (backing_store == null)
158                         return false;
159
160                 draw(backing_store);
161
162                 window.draw_drawable(
163                         get_style().fg_gc[Gtk.StateType.NORMAL],
164                         backing_store,
165                         event.area.x, event.area.y,
166                         event.area.x, event.area.y,
167                         event.area.width, event.area.height);
168
169                 return false;
170         }
171
172         protected int el_y(int idx)
173         {
174                 // Undistorted Y
175                 int undy = idx * allocation.height / list.length;
176                 // Distorted position
177                 int pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
178                 //stderr.printf("%d %f %f\n", idx, undy, pos);
179                 return pos;
180         }
181
182         protected void focus_layout()
183         {
184                 // Anchor point
185                 focus_centre = cur_el*allocation.height/list.length;
186
187                 focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
188                 focus_end = focus_first + focus_size;
189                 if (focus_end >= list.length) focus_end = list.length;
190
191                 // Compute starting positions for all items in focus
192                 for (int idx = focus_first; idx < focus_end; ++idx)
193                 {
194                         int posprev = idx == 0 ? 0 : el_y(idx-1);
195                         int pos = el_y(idx);
196                         int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
197                         int y0 = (pos+posprev)/2;
198                         int y1 = (pos+posnext)/2;
199
200                         focus_starts[idx - focus_first] = y0;
201                         focus_starts[idx - focus_first + 1] = y1;
202                 }
203                 focus_layout_needed = false;
204         }
205
206         protected void draw(Gdk.Drawable drawable)
207         {
208                 if (focus_layout_needed)
209                         focus_layout();
210
211                 Gtk.Style style = get_style();
212
213                 // Background
214                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
215
216                 // Focus lock area
217                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
218                         0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
219
220                 // Focus movement area
221                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
222                         allocation.width/2, 0, allocation.width, allocation.height);
223
224                 // Create a Cairo context
225                 //var context = Gdk.cairo_create (drawable);
226
227                 // Paint items around focus
228                 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
229                 for (int idx = focus_first; idx < focus_end; ++idx)
230                 {
231                         int y0 = focus_starts[idx - focus_first];
232                         int y1 = focus_starts[idx - focus_first + 1];
233
234                         Gtk.StateType itemState = Gtk.StateType.NORMAL;
235                         if (idx == cur_el)
236                         {
237                                 itemState = Gtk.StateType.SELECTED;
238                                 drawable.draw_rectangle(style.bg_gc[itemState], true,
239                                         0, y0, allocation.width, y1-y0);
240                         }
241
242                 
243                         // TODO: cache pango contexts instead of fontdescs
244                         int size = (y1-y0)*80/100;
245                         if (size <= 0) size = 1;
246                         if (size >= pango_cache.length) size = pango_cache.length - 1;
247                         if (pango_cache[size] == null)
248                         {
249                                 var fd = style.font_desc.copy();
250                                 fd.set_absolute_size(size*Pango.SCALE);
251                                 var pc = create_pango_context();
252                                 pc.set_font_description(fd);
253                                 pango_cache[size] = new Pango.Layout(pc);
254                         }
255                         pango_cache[size].set_text(list[idx], -1);
256                         var layout = pango_cache[size];
257                         //stderr.printf("AZAZA %p\n", layout.get_attributes());
258                         //var attrlist = layout.get_attributes().copy();
259                         //stderr.printf("AL %p\n", attrlist);
260                         //var attrlist = new Pango.AttrList();
261                         //stderr.printf("SIZE %d\n", y1-y0);
262                         //attrlist.insert(new Pango.AttrSize(y1-y0));
263                         //var attrlist = layout.get_attributes();
264                         //attrlist.change(new Pango.AttrSize(y1-y0));
265                         //layout.set_attributes(attrlist);
266                         //layout.set_height(y1-y0);
267                         //int w, h;
268                         //layout.get_pixel_size(out w, out h);
269                         Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
270                 }
271         }
272
273     /*
274      * The following function is adapted from Prefuse's FisheyeDistortion.java.
275      *
276      * A relevant annotation from Prefuse:
277      *
278      * For more details on this form of transformation, see Manojit Sarkar and 
279      * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
280      * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
281      * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
282      * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
283      */
284
285     /*
286      * Distorts an item's coordinate.
287      * @param x the undistorted coordinate
288      * @param coordinate of the anchor or focus point
289      * @param d disortion factor
290      * @param min the beginning of the display
291      * @param max the end of the display
292      * @return the distorted coordinate
293      */
294     private int fisheye(int x, int a, int d, int min, int max)
295     {
296         if ( d != 0 ) {
297             bool left = x<a;
298             double v;
299             int m = (left ? a-min : max-a);
300             if ( m == 0 ) m = max-min;
301             v = (double)(x - a).abs() / m;
302             v = (double)(d+1)/(d+(1/v));
303             return (int)Math.round((left?-1:1)*m*v + a);
304         } else {
305             return x;
306         }
307     }
308 }
309
310 public class Fisheye : Gtk.Window
311 {
312         public Fisheye()
313         {
314                 title = "Fisheye";
315                 destroy += Gtk.main_quit;
316
317                 var list = new FisheyeList();
318                 add(list);
319         }
320 }
321
322 static int main (string[] args) {
323         Gtk.init (ref args);
324
325         var fe = new Fisheye();
326         fe.set_size_request(200, 300);
327         fe.show_all();
328
329         Gtk.main();
330
331         return 0;
332 }