Started to prototype a fisheye list
[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
28         public FisheyeList()
29         {
30                 list = new string[300];
31                 for (int i = 0; i < 300; ++i)
32                         list[i] = "Antani %d".printf(i);
33
34                 cur_el = 50;
35
36                 add_events(Gdk.EventMask.POINTER_MOTION_MASK
37                          | Gdk.EventMask.BUTTON_PRESS_MASK
38                          | Gdk.EventMask.BUTTON_RELEASE_MASK);
39         }
40
41         /* Mouse button got pressed over widget */
42         public override bool button_press_event(Gdk.EventButton event)
43         {
44                 // ...
45                 return false;
46         }
47
48         /* Mouse button got released */
49         public override bool button_release_event(Gdk.EventButton event)
50         {
51                 // ...
52                 return false;
53         }
54
55         /* Mouse pointer moved over widget */
56         public override bool motion_notify_event(Gdk.EventMotion event)
57         {
58                 cur_el = (int)Math.round(event.y * list.length / allocation.height);
59                 stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
60                 queue_draw();
61                 return false;
62         }
63
64         /* Widget is asked to draw itself */
65         public override bool expose_event (Gdk.EventExpose event) {
66
67                 // Create a Cairo context
68                 var cr = Gdk.cairo_create (this.window);
69
70                 // Set clipping area in order to avoid unnecessary drawing
71                 cr.rectangle(event.area.x, event.area.y,
72                                 event.area.width, event.area.height);
73                 cr.clip();
74
75                 draw(cr);
76
77                 return false;
78         }
79
80         protected double el_y(int idx)
81         {
82                 double layout_min = 0;
83                 double layout_max = 1;
84                 double distortion_factor = 30;
85                 // Anchor point
86                 double anchor = (double)cur_el/list.length;
87                 // Undistorted Y
88                 double undy = (double)idx/list.length;
89                 // Distorted position
90                 double pos = fisheye(undy, anchor, distortion_factor, layout_min, layout_max);
91                 //stderr.printf("%d %f %f\n", idx, undy, pos);
92                 return pos;
93         }
94
95         protected void draw(Cairo.Context context)
96         {
97                 context.set_source_rgb(1, 1, 1);
98                 context.paint();
99                 context.set_source_rgb(0, 0, 0);
100
101                 context.translate (0, 0);
102                 context.scale(allocation.width, allocation.height);
103
104
105                 // Layout items
106                 // item_pos(idx) = idx/list.length
107
108                 context.set_line_width (0.001);
109                 context.select_font_face("Sans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
110                 for (int idx = 0; idx < list.length; ++idx)
111                 {
112                         double posprev = idx == 0 ? 0 : el_y(idx-1);
113                         double pos = el_y(idx);
114                         double posnext = idx == list.length-1 ? 1 : el_y(idx+1);
115                         double y0 = (pos+posprev)/2;
116                         double y1 = (pos+posnext)/2;
117
118                         //stderr.printf("  %f->%f  (%f)\n", y0, y1, y1-y0);
119
120                         context.set_source_rgba(idx%2, 1-idx%2, 0, 1);
121                         context.new_path();
122                         context.rectangle(0, y0, 1, y1-y0);
123                         context.fill();
124                         context.stroke();
125
126                         context.set_source_rgba(0, 0, 0, 1);
127                         context.new_path();
128                         context.move_to(0, pos);
129                         context.set_font_size(y1-y0);
130                         context.text_path(list[idx]);
131                         context.fill();
132                         context.stroke();
133                 }
134         }
135
136     /*
137      * The following function is adapted from Prefuse's FisheyeDistortion.java.
138      *
139      * A relevant annotation from Prefuse:
140      *
141      * For more details on this form of transformation, see Manojit Sarkar and 
142      * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
143      * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
144      * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
145      * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
146      */
147
148     /*
149      * Distorts an item's coordinate.
150      * @param x the undistorted coordinate
151      * @param coordinate of the anchor or focus point
152      * @param d disortion factor
153      * @param min the beginning of the display
154      * @param max the end of the display
155      * @return the distorted coordinate
156      */
157     private double fisheye(double x, double a, double d, double min, double max)
158     {
159         if ( d != 0 ) {
160             bool left = x<a;
161             double v;
162             double m = (left ? a-min : max-a);
163             if ( m == 0 ) m = max-min;
164             v = Math.fabs(x - a) / m;
165             v = (d+1)/(d+(1/v));
166             return (left?-1:1)*m*v + a;
167         } else {
168             return x;
169         }
170     }
171 }
172
173 public class Fisheye : Gtk.Window
174 {
175         public Fisheye()
176         {
177                 title = "Fisheye";
178                 destroy += Gtk.main_quit;
179
180                 var list = new FisheyeList();
181                 add(list);
182         }
183 }
184
185 static int main (string[] args) {
186         Gtk.init (ref args);
187
188         var fe = new Fisheye();
189         fe.set_size_request(300, 500);
190         fe.show_all();
191
192         Gtk.main();
193
194         return 0;
195 }