2 * zavai - simple interface to the OpenMoko (or to the FSO stack)
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
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.
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.
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
23 public class FisheyeList : Gtk.DrawingArea
25 private string[] list;
27 protected double distortion_factor;
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;
39 list = new string[300];
40 for (int i = 0; i < 300; ++i)
41 list[i] = "Antani %d".printf(i);
46 focus_starts = new double[focus_size + 1];
48 distortion_factor = 30;
51 add_events(Gdk.EventMask.POINTER_MOTION_MASK
52 | Gdk.EventMask.BUTTON_PRESS_MASK
53 | Gdk.EventMask.BUTTON_RELEASE_MASK);
56 /* Mouse button got pressed over widget */
57 public override bool button_press_event(Gdk.EventButton event)
59 stderr.printf("Mouse pressed on %d %s\n", cur_el, list[cur_el]);
63 /* Mouse button got released */
64 public override bool button_release_event(Gdk.EventButton event)
66 stderr.printf("Mouse released on %d %s\n", cur_el, list[cur_el]);
71 /* Mouse pointer moved over widget */
72 public override bool motion_notify_event(Gdk.EventMotion event)
74 int old_cur_el = cur_el;
75 double x = event.x / allocation.width;
76 double y = event.y / allocation.height;
78 focus_locked = x > 0.5 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
82 for (int idx = focus_first; idx < focus_end; ++idx)
83 if (y < focus_starts[idx-focus_first+1])
90 cur_el = (int)Math.round(y * list.length);
91 if (old_cur_el != cur_el)
95 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
96 if (old_cur_el != cur_el)
104 /* Widget is asked to draw itself */
105 public override bool expose_event (Gdk.EventExpose event) {
107 // Create a Cairo context
108 var cr = Gdk.cairo_create (this.window);
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);
120 protected double el_y(int idx)
122 double layout_min = 0;
123 double layout_max = 1;
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);
132 protected void focus_layout()
135 focus_centre = (double)cur_el/list.length;
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;
141 // Compute starting positions for all items in focus
142 for (int idx = focus_first; idx < focus_end; ++idx)
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;
150 focus_starts[idx - focus_first] = y0;
151 focus_starts[idx - focus_first + 1] = y1;
155 protected void draw(Cairo.Context context)
157 // White background (FIXME: get from style)
158 context.set_source_rgb(1, 1, 1);
160 context.set_source_rgb(0, 0, 0);
162 // Normalise coordinates
163 context.translate (0, 0);
164 context.scale(allocation.width, allocation.height);
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)
171 double y0 = focus_starts[idx - focus_first];
172 double y1 = focus_starts[idx - focus_first + 1];
176 context.set_source_rgba(0.4, 0.4, 1, 1);
178 context.rectangle(0, y0, 1, y1-y0);
183 context.set_source_rgba(0, 0, 0, 1);
185 context.move_to(0, (y1+y0)/2);
186 context.set_font_size(y1-y0);
187 context.text_path(list[idx]);
192 // Paint focus lock area
193 context.set_source_rgba(0.8, 1, 1, 0.8);
195 context.rectangle(0.5, focus_starts[0], 1, focus_starts[focus_end - focus_first]);
201 * The following function is adapted from Prefuse's FisheyeDistortion.java.
203 * A relevant annotation from Prefuse:
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>.
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
221 private double fisheye(double x, double a, double d, double min, double max)
226 double m = (left ? a-min : max-a);
227 if ( m == 0 ) m = max-min;
228 v = Math.fabs(x - a) / m;
230 return (left?-1:1)*m*v + a;
237 public class Fisheye : Gtk.Window
242 destroy += Gtk.main_quit;
244 var list = new FisheyeList();
249 static int main (string[] args) {
252 var fe = new Fisheye();
253 fe.set_size_request(300, 500);