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 for (int idx = 0; idx < list.length; ++idx)
112 double posprev = idx == 0 ? 0 : el_y(idx-1);
113 double pos = el_y(idx);
114 double posnext = idx == list.length-1 ? 1 : el_y(idx+1);
115 double y0 = (pos+posprev)/2;
116 double y1 = (pos+posnext)/2;
118 //stderr.printf(" %f->%f (%f)\n", y0, y1, y1-y0);
120 context.set_source_rgba(idx%2, 1-idx%2, 0, 1);
122 context.rectangle(0, y0, 1, y1-y0);
126 context.set_source_rgba(0, 0, 0, 1);
128 context.move_to(0, pos);
129 context.set_font_size(y1-y0);
130 context.text_path(list[idx]);
137 * The following function is adapted from Prefuse's FisheyeDistortion.java.
139 * A relevant annotation from Prefuse:
141 * For more details on this form of transformation, see Manojit Sarkar and
142 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
143 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
144 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
145 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
149 * Distorts an item's coordinate.
150 * @param x the undistorted coordinate
151 * @param coordinate of the anchor or focus point
152 * @param d disortion factor
153 * @param min the beginning of the display
154 * @param max the end of the display
155 * @return the distorted coordinate
157 private double fisheye(double x, double a, double d, double min, double max)
162 double m = (left ? a-min : max-a);
163 if ( m == 0 ) m = max-min;
164 v = Math.fabs(x - a) / m;
166 return (left?-1:1)*m*v + a;
173 public class Fisheye : Gtk.Window
178 destroy += Gtk.main_quit;
180 var list = new FisheyeList();
185 static int main (string[] args) {
188 var fe = new Fisheye();
189 fe.set_size_request(300, 500);