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;
30 list = new string[300];
31 for (int i = 0; i < 300; ++i)
32 list[i] = "Antani %d".printf(i);
36 add_events(Gdk.EventMask.POINTER_MOTION_MASK
37 | Gdk.EventMask.BUTTON_PRESS_MASK
38 | Gdk.EventMask.BUTTON_RELEASE_MASK);
41 /* Mouse button got pressed over widget */
42 public override bool button_press_event(Gdk.EventButton event)
48 /* Mouse button got released */
49 public override bool button_release_event(Gdk.EventButton event)
55 /* Mouse pointer moved over widget */
56 public override bool motion_notify_event(Gdk.EventMotion event)
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);
64 /* Widget is asked to draw itself */
65 public override bool expose_event (Gdk.EventExpose event) {
67 // Create a Cairo context
68 var cr = Gdk.cairo_create (this.window);
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);
80 protected double el_y(int idx)
82 double layout_min = 0;
83 double layout_max = 1;
84 double distortion_factor = 30;
86 double anchor = (double)cur_el/list.length;
88 double undy = (double)idx/list.length;
90 double pos = fisheye(undy, anchor, distortion_factor, layout_min, layout_max);
91 //stderr.printf("%d %f %f\n", idx, undy, pos);
95 protected void draw(Cairo.Context context)
97 context.set_source_rgb(1, 1, 1);
99 context.set_source_rgb(0, 0, 0);
101 context.translate (0, 0);
102 context.scale(allocation.width, allocation.height);
106 // item_pos(idx) = idx/list.length
108 context.set_line_width (0.001);
109 context.select_font_face("Sans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
110 int start = cur_el > 20 ? cur_el-20 : 0;
111 int end = cur_el < list.length - 20 ? cur_el + 20 : list.length;
112 for (int idx = start; idx < end; ++idx)
114 double posprev = idx == 0 ? 0 : el_y(idx-1);
115 double pos = el_y(idx);
116 double posnext = idx == list.length-1 ? 1 : el_y(idx+1);
117 double y0 = (pos+posprev)/2;
118 double y1 = (pos+posnext)/2;
120 //stderr.printf(" %f->%f (%f)\n", y0, y1, y1-y0);
122 context.set_source_rgba(idx%2, 1-idx%2, 0, 1);
124 context.rectangle(0, y0, 1, y1-y0);
128 context.set_source_rgba(0, 0, 0, 1);
130 context.move_to(0, pos);
131 context.set_font_size(y1-y0);
132 context.text_path(list[idx]);
139 * The following function is adapted from Prefuse's FisheyeDistortion.java.
141 * A relevant annotation from Prefuse:
143 * For more details on this form of transformation, see Manojit Sarkar and
144 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
145 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
146 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
147 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
151 * Distorts an item's coordinate.
152 * @param x the undistorted coordinate
153 * @param coordinate of the anchor or focus point
154 * @param d disortion factor
155 * @param min the beginning of the display
156 * @param max the end of the display
157 * @return the distorted coordinate
159 private double fisheye(double x, double a, double d, double min, double max)
164 double m = (left ? a-min : max-a);
165 if ( m == 0 ) m = max-min;
166 v = Math.fabs(x - a) / m;
168 return (left?-1:1)*m*v + a;
175 public class Fisheye : Gtk.Window
180 destroy += Gtk.main_quit;
182 var list = new FisheyeList();
187 static int main (string[] args) {
190 var fe = new Fisheye();
191 fe.set_size_request(300, 500);