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