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 int[] 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 int[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;
78 focus_locked = x < allocation.width/2 && 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 = y * list.length / allocation.height;
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 int el_y(int idx)
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);
130 protected void focus_layout()
133 focus_centre = (double)cur_el*allocation.height/list.length;
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;
139 // Compute starting positions for all items in focus
140 for (int idx = focus_first; idx < focus_end; ++idx)
142 int posprev = idx == 0 ? 0 : el_y(idx-1);
144 int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
145 int y0 = (pos+posprev)/2;
146 int y1 = (pos+posnext)/2;
148 focus_starts[idx - focus_first] = y0;
149 focus_starts[idx - focus_first + 1] = y1;
153 protected void draw(Cairo.Context context)
155 Gtk.Style style = get_style();
157 public enum StateType {
164 style.bg[Gtk.StateType.NORMAL];
169 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.NORMAL]);
173 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.ACTIVE]);
175 context.rectangle(0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
179 // Focus movement area
180 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.INSENSITIVE]);
182 context.rectangle(allocation.width/2, 0, allocation.width, allocation.height);
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)
190 int y0 = focus_starts[idx - focus_first];
191 int y1 = focus_starts[idx - focus_first + 1];
195 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.SELECTED]);
197 context.rectangle(0, y0, allocation.width, y1-y0);
201 Gdk.cairo_set_source_color(context, style.fg[Gtk.StateType.SELECTED]);
203 Gdk.cairo_set_source_color(context, style.fg[Gtk.StateType.NORMAL]);
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]);
218 * The following function is adapted from Prefuse's FisheyeDistortion.java.
220 * A relevant annotation from Prefuse:
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>.
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
238 private double fisheye(double x, double a, double d, double min, double max)
243 double m = (left ? a-min : max-a);
244 if ( m == 0 ) m = max-min;
245 v = Math.fabs(x - a) / m;
247 return (left?-1:1)*m*v + a;
254 public class Fisheye : Gtk.Window
259 destroy += Gtk.main_quit;
261 var list = new FisheyeList();
266 static int main (string[] args) {
269 var fe = new Fisheye();
270 fe.set_size_request(300, 500);