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 protected Gdk.Pixmap backing_store;
27 private string[] list;
29 protected double distortion_factor;
31 // Number of items shown before and after the focus element
32 protected int focus_first;
33 protected int focus_end;
34 protected int focus_size;
35 protected int[] focus_starts;
36 protected bool focus_locked;
37 protected double focus_centre;
43 list = new string[300];
44 for (int i = 0; i < 300; ++i)
45 list[i] = "Antani %d".printf(i);
50 focus_starts = new int[focus_size + 1];
52 distortion_factor = 30;
55 add_events(Gdk.EventMask.POINTER_MOTION_MASK
56 | Gdk.EventMask.BUTTON_PRESS_MASK
57 | Gdk.EventMask.BUTTON_RELEASE_MASK);
60 /* Mouse button got pressed over widget */
61 public override bool button_press_event(Gdk.EventButton event)
63 stderr.printf("Mouse pressed on %d %s\n", cur_el, list[cur_el]);
67 /* Mouse button got released */
68 public override bool button_release_event(Gdk.EventButton event)
70 stderr.printf("Mouse released on %d %s\n", cur_el, list[cur_el]);
75 /* Mouse pointer moved over widget */
76 public override bool motion_notify_event(Gdk.EventMotion event)
78 int old_cur_el = cur_el;
82 focus_locked = x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
86 for (int idx = focus_first; idx < focus_end; ++idx)
87 if (y < focus_starts[idx-focus_first+1])
94 cur_el = y * list.length / allocation.height;
95 if (old_cur_el != cur_el)
99 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
100 if (old_cur_el != cur_el)
108 public override bool configure_event (Gdk.EventConfigure event)
110 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
116 /* Widget is asked to draw itself */
117 public override bool expose_event (Gdk.EventExpose event)
119 if (backing_store == null)
124 window.draw_drawable(
125 get_style().fg_gc[Gtk.StateType.NORMAL],
127 event.area.x, event.area.y,
128 event.area.x, event.area.y,
129 event.area.width, event.area.height);
134 protected int el_y(int idx)
137 double undy = (double)idx * allocation.height / list.length;
138 // Distorted position
139 double pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
140 //stderr.printf("%d %f %f\n", idx, undy, pos);
141 return (int)Math.round(pos);
144 protected void focus_layout()
147 focus_centre = (double)cur_el*allocation.height/list.length;
149 focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
150 focus_end = focus_first + focus_size;
151 if (focus_end >= list.length) focus_end = list.length;
153 // Compute starting positions for all items in focus
154 for (int idx = focus_first; idx < focus_end; ++idx)
156 int posprev = idx == 0 ? 0 : el_y(idx-1);
158 int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
159 int y0 = (pos+posprev)/2;
160 int y1 = (pos+posnext)/2;
162 focus_starts[idx - focus_first] = y0;
163 focus_starts[idx - focus_first + 1] = y1;
167 protected void draw(Gdk.Drawable drawable)
169 Gtk.Style style = get_style();
171 public enum StateType {
178 style.bg[Gtk.StateType.NORMAL];
183 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
186 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
187 0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
189 // Focus movement area
190 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
191 allocation.width/2, 0, allocation.width, allocation.height);
193 // Create a Cairo context
194 //var context = Gdk.cairo_create (drawable);
196 // Paint items around focus
197 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
198 for (int idx = focus_first; idx < focus_end; ++idx)
200 int y0 = focus_starts[idx - focus_first];
201 int y1 = focus_starts[idx - focus_first + 1];
203 Gtk.StateType itemState = Gtk.StateType.NORMAL;
206 itemState = Gtk.StateType.SELECTED;
207 drawable.draw_rectangle(style.bg_gc[itemState], true,
208 0, y0, allocation.width, y1-y0);
211 var layout = create_pango_layout(list[idx]);
213 var fd = style.font_desc.copy();
214 //fd.set_size((y1-y0)*Pango.SCALE);
215 fd.set_absolute_size((y1-y0)*Pango.SCALE);
216 layout.set_font_description(fd);
217 //stderr.printf("AZAZA %p\n", layout.get_attributes());
218 //var attrlist = layout.get_attributes().copy();
219 //stderr.printf("AL %p\n", attrlist);
220 //var attrlist = new Pango.AttrList();
221 //stderr.printf("SIZE %d\n", y1-y0);
222 //attrlist.insert(new Pango.AttrSize(y1-y0));
223 //var attrlist = layout.get_attributes();
224 //attrlist.change(new Pango.AttrSize(y1-y0));
225 //layout.set_attributes(attrlist);
226 //layout.set_height(y1-y0);
227 Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
232 * The following function is adapted from Prefuse's FisheyeDistortion.java.
234 * A relevant annotation from Prefuse:
236 * For more details on this form of transformation, see Manojit Sarkar and
237 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
238 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
239 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
240 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
244 * Distorts an item's coordinate.
245 * @param x the undistorted coordinate
246 * @param coordinate of the anchor or focus point
247 * @param d disortion factor
248 * @param min the beginning of the display
249 * @param max the end of the display
250 * @return the distorted coordinate
252 private double fisheye(double x, double a, double d, double min, double max)
257 double m = (left ? a-min : max-a);
258 if ( m == 0 ) m = max-min;
259 v = Math.fabs(x - a) / m;
261 return (left?-1:1)*m*v + a;
268 public class Fisheye : Gtk.Window
273 destroy += Gtk.main_quit;
275 var list = new FisheyeList();
280 static int main (string[] args) {
283 var fe = new Fisheye();
284 fe.set_size_request(200, 300);