Work on an unscaled drawing 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 int[] 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;
45                 focus_size = 20;
46                 focus_starts = new int[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                 int x = (int)event.x;
76                 int y = (int)event.y;
77
78                 focus_locked = x < allocation.width/2 && 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 = y * list.length / allocation.height;
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 int el_y(int idx)
121         {
122                 // Undistorted Y
123                 double undy = (double)idx * allocation.height / list.length;
124                 // Distorted position
125                 double pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
126                 //stderr.printf("%d %f %f\n", idx, undy, pos);
127                 return (int)Math.round(pos);
128         }
129
130         protected void focus_layout()
131         {
132                 // Anchor point
133                 focus_centre = (double)cur_el*allocation.height/list.length;
134
135                 focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
136                 focus_end = focus_first + focus_size;
137                 if (focus_end >= list.length) focus_end = list.length;
138
139                 // Compute starting positions for all items in focus
140                 for (int idx = focus_first; idx < focus_end; ++idx)
141                 {
142                         int posprev = idx == 0 ? 0 : el_y(idx-1);
143                         int pos = el_y(idx);
144                         int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
145                         int y0 = (pos+posprev)/2;
146                         int y1 = (pos+posnext)/2;
147
148                         focus_starts[idx - focus_first] = y0;
149                         focus_starts[idx - focus_first + 1] = y1;
150                 }
151         }
152
153         protected void draw(Cairo.Context context)
154         {
155                 Gtk.Style style = get_style();
156         /*
157         public enum StateType {
158                 NORMAL,
159                 ACTIVE,
160                 PRELIGHT,
161                 SELECTED,
162                 INSENSITIVE
163         }
164                 style.bg[Gtk.StateType.NORMAL];
165         */
166
167
168                 // Background
169                 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.NORMAL]);
170                 context.paint();
171
172                 // Focus lock area
173                 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.ACTIVE]);
174                 context.new_path();
175                 context.rectangle(0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
176                 context.fill();
177                 context.stroke();
178
179                 // Focus movement area
180                 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.INSENSITIVE]);
181                 context.new_path();
182                 context.rectangle(allocation.width/2, 0, allocation.width, allocation.height);
183                 context.fill();
184                 context.stroke();
185
186                 // Paint items around focus
187                 context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
188                 for (int idx = focus_first; idx < focus_end; ++idx)
189                 {
190                         int y0 = focus_starts[idx - focus_first];
191                         int y1 = focus_starts[idx - focus_first + 1];
192
193                         if (idx == cur_el)
194                         {
195                                 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.SELECTED]);
196                                 context.new_path();
197                                 context.rectangle(0, y0, allocation.width, y1-y0);
198                                 context.fill();
199                                 context.stroke();
200
201                                 Gdk.cairo_set_source_color(context, style.fg[Gtk.StateType.SELECTED]);
202                         } else {
203                                 Gdk.cairo_set_source_color(context, style.fg[Gtk.StateType.NORMAL]);
204                         }
205
206                         context.new_path();
207                         context.set_font_size(y1-y0);
208                         Cairo.FontExtents extents;
209                         context.font_extents(out extents);
210                         context.move_to(0, y1-extents.descent);
211                         context.text_path(list[idx]);
212                         context.fill();
213                         context.stroke();
214                 }
215         }
216
217     /*
218      * The following function is adapted from Prefuse's FisheyeDistortion.java.
219      *
220      * A relevant annotation from Prefuse:
221      *
222      * For more details on this form of transformation, see Manojit Sarkar and 
223      * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
224      * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
225      * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
226      * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
227      */
228
229     /*
230      * Distorts an item's coordinate.
231      * @param x the undistorted coordinate
232      * @param coordinate of the anchor or focus point
233      * @param d disortion factor
234      * @param min the beginning of the display
235      * @param max the end of the display
236      * @return the distorted coordinate
237      */
238     private double fisheye(double x, double a, double d, double min, double max)
239     {
240         if ( d != 0 ) {
241             bool left = x<a;
242             double v;
243             double m = (left ? a-min : max-a);
244             if ( m == 0 ) m = max-min;
245             v = Math.fabs(x - a) / m;
246             v = (d+1)/(d+(1/v));
247             return (left?-1:1)*m*v + a;
248         } else {
249             return x;
250         }
251     }
252 }
253
254 public class Fisheye : Gtk.Window
255 {
256         public Fisheye()
257         {
258                 title = "Fisheye";
259                 destroy += Gtk.main_quit;
260
261                 var list = new FisheyeList();
262                 add(list);
263         }
264 }
265
266 static int main (string[] args) {
267         Gtk.init (ref args);
268
269         var fe = new Fisheye();
270         fe.set_size_request(300, 500);
271         fe.show_all();
272
273         Gtk.main();
274
275         return 0;
276 }