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 public override bool configure_event (Gdk.EventConfigure event)
111 /* Widget is asked to draw itself */
112 public override bool expose_event (Gdk.EventExpose event) {
114 // Create a Cairo context
115 var cr = Gdk.cairo_create (this.window);
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);
127 protected int el_y(int idx)
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);
137 protected void focus_layout()
140 focus_centre = (double)cur_el*allocation.height/list.length;
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;
146 // Compute starting positions for all items in focus
147 for (int idx = focus_first; idx < focus_end; ++idx)
149 int posprev = idx == 0 ? 0 : el_y(idx-1);
151 int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
152 int y0 = (pos+posprev)/2;
153 int y1 = (pos+posnext)/2;
155 focus_starts[idx - focus_first] = y0;
156 focus_starts[idx - focus_first + 1] = y1;
160 protected void draw(Cairo.Context context)
162 Gtk.Style style = get_style();
164 public enum StateType {
171 style.bg[Gtk.StateType.NORMAL];
176 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.NORMAL]);
180 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.ACTIVE]);
182 context.rectangle(0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
186 // Focus movement area
187 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.INSENSITIVE]);
189 context.rectangle(allocation.width/2, 0, allocation.width, allocation.height);
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)
197 int y0 = focus_starts[idx - focus_first];
198 int y1 = focus_starts[idx - focus_first + 1];
202 Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.SELECTED]);
204 context.rectangle(0, y0, allocation.width, y1-y0);
208 Gdk.cairo_set_source_color(context, style.fg[Gtk.StateType.SELECTED]);
210 Gdk.cairo_set_source_color(context, style.fg[Gtk.StateType.NORMAL]);
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]);
225 * The following function is adapted from Prefuse's FisheyeDistortion.java.
227 * A relevant annotation from Prefuse:
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>.
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
245 private double fisheye(double x, double a, double d, double min, double max)
250 double m = (left ? a-min : max-a);
251 if ( m == 0 ) m = max-min;
252 v = Math.fabs(x - a) / m;
254 return (left?-1:1)*m*v + a;
261 public class Fisheye : Gtk.Window
266 destroy += Gtk.main_quit;
268 var list = new FisheyeList();
273 static int main (string[] args) {
276 var fe = new Fisheye();
277 fe.set_size_request(200, 300);