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 int focus_centre;
43 list = new string[300];
44 for (int i = 0; i < 300; ++i)
45 list[i] = "Lorem Ipsum %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 int undy = idx * allocation.height / list.length;
138 // Distorted position
139 int pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
140 //stderr.printf("%d %f %f\n", idx, undy, pos);
144 protected void focus_layout()
147 focus_centre = 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();
172 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
175 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
176 0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
178 // Focus movement area
179 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
180 allocation.width/2, 0, allocation.width, allocation.height);
182 // Create a Cairo context
183 //var context = Gdk.cairo_create (drawable);
185 // Paint items around focus
186 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
187 for (int idx = focus_first; idx < focus_end; ++idx)
189 int y0 = focus_starts[idx - focus_first];
190 int y1 = focus_starts[idx - focus_first + 1];
192 Gtk.StateType itemState = Gtk.StateType.NORMAL;
195 itemState = Gtk.StateType.SELECTED;
196 drawable.draw_rectangle(style.bg_gc[itemState], true,
197 0, y0, allocation.width, y1-y0);
200 var layout = create_pango_layout(list[idx]);
202 var fd = style.font_desc.copy();
203 //fd.set_size((y1-y0)*Pango.SCALE);
204 int size = (y1-y0)*Pango.SCALE*80/100;
205 if (size <= 0) size = Pango.SCALE;
206 fd.set_absolute_size(size);
207 layout.set_font_description(fd);
208 //stderr.printf("AZAZA %p\n", layout.get_attributes());
209 //var attrlist = layout.get_attributes().copy();
210 //stderr.printf("AL %p\n", attrlist);
211 //var attrlist = new Pango.AttrList();
212 //stderr.printf("SIZE %d\n", y1-y0);
213 //attrlist.insert(new Pango.AttrSize(y1-y0));
214 //var attrlist = layout.get_attributes();
215 //attrlist.change(new Pango.AttrSize(y1-y0));
216 //layout.set_attributes(attrlist);
217 //layout.set_height(y1-y0);
219 //layout.get_pixel_size(out w, out h);
220 Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
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 int fisheye(int x, int a, double d, int min, int max)
250 int m = (left ? a-min : max-a);
251 if ( m == 0 ) m = max-min;
252 v = Math.fabs(x - a) / m;
254 return (int)Math.round((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);