Fisheye with focus lock area
[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         private string[] list;
26         protected int cur_el;
27         protected double distortion_factor;
28
29         // Number of items shown before and after the focus element
30         protected int focus_first;
31         protected int focus_end;
32         protected int focus_size;
33         protected double[] focus_starts;
34         protected bool focus_locked;
35         protected double focus_centre;
36
37         public FisheyeList()
38         {
39                 list = new string[300];
40                 for (int i = 0; i < 300; ++i)
41                         list[i] = "Antani %d".printf(i);
42
43                 cur_el = 0;
44                 focus_centre = 0.0;
45                 focus_size = 20;
46                 focus_starts = new double[focus_size + 1];
47                 focus_locked = false;
48                 distortion_factor = 30;
49                 focus_layout();
50
51                 add_events(Gdk.EventMask.POINTER_MOTION_MASK
52                          | Gdk.EventMask.BUTTON_PRESS_MASK
53                          | Gdk.EventMask.BUTTON_RELEASE_MASK);
54         }
55
56         /* Mouse button got pressed over widget */
57         public override bool button_press_event(Gdk.EventButton event)
58         {
59                 stderr.printf("Mouse pressed on %d %s\n", cur_el, list[cur_el]);
60                 return false;
61         }
62
63         /* Mouse button got released */
64         public override bool button_release_event(Gdk.EventButton event)
65         {
66                 stderr.printf("Mouse released on %d %s\n", cur_el, list[cur_el]);
67                 // ...
68                 return false;
69         }
70
71         /* Mouse pointer moved over widget */
72         public override bool motion_notify_event(Gdk.EventMotion event)
73         {
74                 int old_cur_el = cur_el;
75                 double x = event.x / allocation.width;
76                 double y = event.y / allocation.height;
77
78                 focus_locked = x > 0.5 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
79
80                 if (focus_locked)
81                 {
82                         for (int idx = focus_first; idx < focus_end; ++idx)
83                                 if (y < focus_starts[idx-focus_first+1])
84                                 {
85                                         cur_el = idx;
86                                         break;
87                                 }
88
89                 } else {
90                         cur_el = (int)Math.round(y * list.length);
91                         if (old_cur_el != cur_el)
92                                 focus_layout();
93                 }
94
95                 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
96                 if (old_cur_el != cur_el)
97                 {
98                         queue_draw();
99                         old_cur_el = cur_el;
100                 }
101                 return false;
102         }
103
104         /* Widget is asked to draw itself */
105         public override bool expose_event (Gdk.EventExpose event) {
106
107                 // Create a Cairo context
108                 var cr = Gdk.cairo_create (this.window);
109
110                 // Set clipping area in order to avoid unnecessary drawing
111                 cr.rectangle(event.area.x, event.area.y,
112                                 event.area.width, event.area.height);
113                 cr.clip();
114
115                 draw(cr);
116
117                 return false;
118         }
119
120         protected double el_y(int idx)
121         {
122                 double layout_min = 0;
123                 double layout_max = 1;
124                 // Undistorted Y
125                 double undy = (double)idx/list.length;
126                 // Distorted position
127                 double pos = fisheye(undy, focus_centre, distortion_factor, layout_min, layout_max);
128                 //stderr.printf("%d %f %f\n", idx, undy, pos);
129                 return pos;
130         }
131
132         protected void focus_layout()
133         {
134                 // Anchor point
135                 focus_centre = (double)cur_el/list.length;
136
137                 focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
138                 focus_end = focus_first + focus_size;
139                 if (focus_end >= list.length) focus_end = list.length;
140
141                 // Compute starting positions for all items in focus
142                 for (int idx = focus_first; idx < focus_end; ++idx)
143                 {
144                         double posprev = idx == 0 ? 0 : el_y(idx-1);
145                         double pos = el_y(idx);
146                         double posnext = idx == list.length-1 ? 1 : el_y(idx+1);
147                         double y0 = (pos+posprev)/2;
148                         double y1 = (pos+posnext)/2;
149
150                         focus_starts[idx - focus_first] = y0;
151                         focus_starts[idx - focus_first + 1] = y1;
152                 }
153         }
154
155         protected void draw(Cairo.Context context)
156         {
157                 // White background (FIXME: get from style)
158                 context.set_source_rgb(1, 1, 1);
159                 context.paint();
160                 context.set_source_rgb(0, 0, 0);
161
162                 // Normalise coordinates
163                 context.translate (0, 0);
164                 context.scale(allocation.width, allocation.height);
165
166                 // Paint items around focus
167                 context.set_line_width (0.001);
168                 context.select_font_face("Sans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
169                 for (int idx = focus_first; idx < focus_end; ++idx)
170                 {
171                         double y0 = focus_starts[idx - focus_first];
172                         double y1 = focus_starts[idx - focus_first + 1];
173
174                         if (idx == cur_el)
175                         {
176                                 context.set_source_rgba(0.4, 0.4, 1, 1);
177                                 context.new_path();
178                                 context.rectangle(0, y0, 1, y1-y0);
179                                 context.fill();
180                                 context.stroke();
181                         }
182
183                         context.set_source_rgba(0, 0, 0, 1);
184                         context.new_path();
185                         context.move_to(0, (y1+y0)/2);
186                         context.set_font_size(y1-y0);
187                         context.text_path(list[idx]);
188                         context.fill();
189                         context.stroke();
190                 }
191
192                 // Paint focus lock area
193                 context.set_source_rgba(0.8, 1, 1, 0.8);
194                 context.new_path();
195                 context.rectangle(0.5, focus_starts[0], 1, focus_starts[focus_end - focus_first]);
196                 context.fill();
197                 context.stroke();
198         }
199
200     /*
201      * The following function is adapted from Prefuse's FisheyeDistortion.java.
202      *
203      * A relevant annotation from Prefuse:
204      *
205      * For more details on this form of transformation, see Manojit Sarkar and 
206      * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
207      * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
208      * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
209      * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
210      */
211
212     /*
213      * Distorts an item's coordinate.
214      * @param x the undistorted coordinate
215      * @param coordinate of the anchor or focus point
216      * @param d disortion factor
217      * @param min the beginning of the display
218      * @param max the end of the display
219      * @return the distorted coordinate
220      */
221     private double fisheye(double x, double a, double d, double min, double max)
222     {
223         if ( d != 0 ) {
224             bool left = x<a;
225             double v;
226             double m = (left ? a-min : max-a);
227             if ( m == 0 ) m = max-min;
228             v = Math.fabs(x - a) / m;
229             v = (d+1)/(d+(1/v));
230             return (left?-1:1)*m*v + a;
231         } else {
232             return x;
233         }
234     }
235 }
236
237 public class Fisheye : Gtk.Window
238 {
239         public Fisheye()
240         {
241                 title = "Fisheye";
242                 destroy += Gtk.main_quit;
243
244                 var list = new FisheyeList();
245                 add(list);
246         }
247 }
248
249 static int main (string[] args) {
250         Gtk.init (ref args);
251
252         var fe = new Fisheye();
253         fe.set_size_request(300, 500);
254         fe.show_all();
255
256         Gtk.main();
257
258         return 0;
259 }