A bit more ints
[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 Gdk.Pixmap backing_store;
26
27         private string[] list;
28         protected int cur_el;
29         protected double distortion_factor;
30
31         // Number of items shown before and after the focus element
32         protected int focus_first;
33         protected int focus_end;
34         protected int focus_size;
35         protected int[] focus_starts;
36         protected bool focus_locked;
37         protected int focus_centre;
38
39         public FisheyeList()
40         {
41                 backing_store = null;
42
43                 list = new string[300];
44                 for (int i = 0; i < 300; ++i)
45                         list[i] = "Lorem Ipsum %d".printf(i);
46
47                 cur_el = 0;
48                 focus_centre = 0;
49                 focus_size = 20;
50                 focus_starts = new int[focus_size + 1];
51                 focus_locked = false;
52                 distortion_factor = 30;
53                 focus_layout();
54
55                 add_events(Gdk.EventMask.POINTER_MOTION_MASK
56                          | Gdk.EventMask.BUTTON_PRESS_MASK
57                          | Gdk.EventMask.BUTTON_RELEASE_MASK);
58         }
59
60         /* Mouse button got pressed over widget */
61         public override bool button_press_event(Gdk.EventButton event)
62         {
63                 stderr.printf("Mouse pressed on %d %s\n", cur_el, list[cur_el]);
64                 return false;
65         }
66
67         /* Mouse button got released */
68         public override bool button_release_event(Gdk.EventButton event)
69         {
70                 stderr.printf("Mouse released on %d %s\n", cur_el, list[cur_el]);
71                 // ...
72                 return false;
73         }
74
75         /* Mouse pointer moved over widget */
76         public override bool motion_notify_event(Gdk.EventMotion event)
77         {
78                 int old_cur_el = cur_el;
79                 int x = (int)event.x;
80                 int y = (int)event.y;
81
82                 focus_locked = x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
83
84                 if (focus_locked)
85                 {
86                         for (int idx = focus_first; idx < focus_end; ++idx)
87                                 if (y < focus_starts[idx-focus_first+1])
88                                 {
89                                         cur_el = idx;
90                                         break;
91                                 }
92
93                 } else {
94                         cur_el = y * list.length / allocation.height;
95                         if (old_cur_el != cur_el)
96                                 focus_layout();
97                 }
98
99                 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
100                 if (old_cur_el != cur_el)
101                 {
102                         queue_draw();
103                         old_cur_el = cur_el;
104                 }
105                 return false;
106         }
107
108         public override bool configure_event (Gdk.EventConfigure event)
109         {
110                 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
111                 focus_layout();
112                 queue_draw();
113                 return false;
114         }
115
116         /* Widget is asked to draw itself */
117         public override bool expose_event (Gdk.EventExpose event)
118         {
119                 if (backing_store == null)
120                         return false;
121
122                 draw(backing_store);
123
124                 window.draw_drawable(
125                         get_style().fg_gc[Gtk.StateType.NORMAL],
126                         backing_store,
127                         event.area.x, event.area.y,
128                         event.area.x, event.area.y,
129                         event.area.width, event.area.height);
130
131                 return false;
132         }
133
134         protected int el_y(int idx)
135         {
136                 // Undistorted Y
137                 int undy = idx * allocation.height / list.length;
138                 // Distorted position
139                 int pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
140                 //stderr.printf("%d %f %f\n", idx, undy, pos);
141                 return pos;
142         }
143
144         protected void focus_layout()
145         {
146                 // Anchor point
147                 focus_centre = cur_el*allocation.height/list.length;
148
149                 focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
150                 focus_end = focus_first + focus_size;
151                 if (focus_end >= list.length) focus_end = list.length;
152
153                 // Compute starting positions for all items in focus
154                 for (int idx = focus_first; idx < focus_end; ++idx)
155                 {
156                         int posprev = idx == 0 ? 0 : el_y(idx-1);
157                         int pos = el_y(idx);
158                         int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
159                         int y0 = (pos+posprev)/2;
160                         int y1 = (pos+posnext)/2;
161
162                         focus_starts[idx - focus_first] = y0;
163                         focus_starts[idx - focus_first + 1] = y1;
164                 }
165         }
166
167         protected void draw(Gdk.Drawable drawable)
168         {
169                 Gtk.Style style = get_style();
170
171                 // Background
172                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
173
174                 // Focus lock area
175                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
176                         0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
177
178                 // Focus movement area
179                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
180                         allocation.width/2, 0, allocation.width, allocation.height);
181
182                 // Create a Cairo context
183                 //var context = Gdk.cairo_create (drawable);
184
185                 // Paint items around focus
186                 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
187                 for (int idx = focus_first; idx < focus_end; ++idx)
188                 {
189                         int y0 = focus_starts[idx - focus_first];
190                         int y1 = focus_starts[idx - focus_first + 1];
191
192                         Gtk.StateType itemState = Gtk.StateType.NORMAL;
193                         if (idx == cur_el)
194                         {
195                                 itemState = Gtk.StateType.SELECTED;
196                                 drawable.draw_rectangle(style.bg_gc[itemState], true,
197                                         0, y0, allocation.width, y1-y0);
198                         }
199
200                         var layout = create_pango_layout(list[idx]);
201
202                         var fd = style.font_desc.copy();
203                         //fd.set_size((y1-y0)*Pango.SCALE);
204                         int size = (y1-y0)*Pango.SCALE*80/100;
205                         if (size <= 0) size = Pango.SCALE;
206                         fd.set_absolute_size(size);
207                         layout.set_font_description(fd);
208                         //stderr.printf("AZAZA %p\n", layout.get_attributes());
209                         //var attrlist = layout.get_attributes().copy();
210                         //stderr.printf("AL %p\n", attrlist);
211                         //var attrlist = new Pango.AttrList();
212                         //stderr.printf("SIZE %d\n", y1-y0);
213                         //attrlist.insert(new Pango.AttrSize(y1-y0));
214                         //var attrlist = layout.get_attributes();
215                         //attrlist.change(new Pango.AttrSize(y1-y0));
216                         //layout.set_attributes(attrlist);
217                         //layout.set_height(y1-y0);
218                         //int w, h;
219                         //layout.get_pixel_size(out w, out h);
220                         Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
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 int fisheye(int x, int a, double d, int min, int max)
246     {
247         if ( d != 0 ) {
248             bool left = x<a;
249             double v;
250             int m = (left ? a-min : max-a);
251             if ( m == 0 ) m = max-min;
252             v = (double)(x - a).abs() / m;
253             v = (d+1)/(d+(1/v));
254             return (int)Math.round((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 }