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 protected Pango.Layout[] pango_cache;
33 // Number of items shown before and after the focus element
34 protected int focus_first;
35 protected int focus_end;
36 protected int focus_size;
37 protected int[] focus_starts;
38 protected bool focus_locked;
39 protected int focus_centre;
45 list = new string[300];
46 for (int i = 0; i < 300; ++i)
47 list[i] = "Lorem Ipsum %d".printf(i);
49 pango_cache = new Pango.Layout[30];
50 for (int i = 0; i < 30; ++i)
51 pango_cache[i] = null;
56 focus_starts = new int[focus_size + 1];
58 distortion_factor = 30;
61 add_events(Gdk.EventMask.POINTER_MOTION_MASK
62 | Gdk.EventMask.BUTTON_PRESS_MASK
63 | Gdk.EventMask.BUTTON_RELEASE_MASK);
66 /* Mouse button got pressed over widget */
67 public override bool button_press_event(Gdk.EventButton event)
69 stderr.printf("Mouse pressed on %d %s\n", cur_el, list[cur_el]);
73 /* Mouse button got released */
74 public override bool button_release_event(Gdk.EventButton event)
76 stderr.printf("Mouse released on %d %s\n", cur_el, list[cur_el]);
81 /* Mouse pointer moved over widget */
82 public override bool motion_notify_event(Gdk.EventMotion event)
84 int old_cur_el = cur_el;
88 focus_locked = x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
92 for (int idx = focus_first; idx < focus_end; ++idx)
93 if (y < focus_starts[idx-focus_first+1])
100 cur_el = y * list.length / allocation.height;
101 if (old_cur_el != cur_el)
105 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
106 if (old_cur_el != cur_el)
114 public override bool configure_event (Gdk.EventConfigure event)
116 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
122 /* Widget is asked to draw itself */
123 public override bool expose_event (Gdk.EventExpose event)
125 if (backing_store == null)
130 window.draw_drawable(
131 get_style().fg_gc[Gtk.StateType.NORMAL],
133 event.area.x, event.area.y,
134 event.area.x, event.area.y,
135 event.area.width, event.area.height);
140 protected int el_y(int idx)
143 int undy = idx * allocation.height / list.length;
144 // Distorted position
145 int pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
146 //stderr.printf("%d %f %f\n", idx, undy, pos);
150 protected void focus_layout()
153 focus_centre = cur_el*allocation.height/list.length;
155 focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
156 focus_end = focus_first + focus_size;
157 if (focus_end >= list.length) focus_end = list.length;
159 // Compute starting positions for all items in focus
160 for (int idx = focus_first; idx < focus_end; ++idx)
162 int posprev = idx == 0 ? 0 : el_y(idx-1);
164 int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
165 int y0 = (pos+posprev)/2;
166 int y1 = (pos+posnext)/2;
168 focus_starts[idx - focus_first] = y0;
169 focus_starts[idx - focus_first + 1] = y1;
173 protected void draw(Gdk.Drawable drawable)
175 Gtk.Style style = get_style();
178 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
181 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
182 0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
184 // Focus movement area
185 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
186 allocation.width/2, 0, allocation.width, allocation.height);
188 // Create a Cairo context
189 //var context = Gdk.cairo_create (drawable);
191 // Paint items around focus
192 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
193 for (int idx = focus_first; idx < focus_end; ++idx)
195 int y0 = focus_starts[idx - focus_first];
196 int y1 = focus_starts[idx - focus_first + 1];
198 Gtk.StateType itemState = Gtk.StateType.NORMAL;
201 itemState = Gtk.StateType.SELECTED;
202 drawable.draw_rectangle(style.bg_gc[itemState], true,
203 0, y0, allocation.width, y1-y0);
207 // TODO: cache pango contexts instead of fontdescs
208 int size = (y1-y0)*80/100;
209 if (size <= 0) size = 1;
210 if (size >= pango_cache.length) size = pango_cache.length - 1;
211 if (pango_cache[size] == null)
213 var fd = style.font_desc.copy();
214 fd.set_absolute_size(size*Pango.SCALE);
215 var pc = create_pango_context();
216 pc.set_font_description(fd);
217 pango_cache[size] = new Pango.Layout(pc);
219 pango_cache[size].set_text(list[idx], -1);
220 var layout = pango_cache[size];
221 //stderr.printf("AZAZA %p\n", layout.get_attributes());
222 //var attrlist = layout.get_attributes().copy();
223 //stderr.printf("AL %p\n", attrlist);
224 //var attrlist = new Pango.AttrList();
225 //stderr.printf("SIZE %d\n", y1-y0);
226 //attrlist.insert(new Pango.AttrSize(y1-y0));
227 //var attrlist = layout.get_attributes();
228 //attrlist.change(new Pango.AttrSize(y1-y0));
229 //layout.set_attributes(attrlist);
230 //layout.set_height(y1-y0);
232 //layout.get_pixel_size(out w, out h);
233 Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
238 * The following function is adapted from Prefuse's FisheyeDistortion.java.
240 * A relevant annotation from Prefuse:
242 * For more details on this form of transformation, see Manojit Sarkar and
243 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
244 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
245 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
246 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
250 * Distorts an item's coordinate.
251 * @param x the undistorted coordinate
252 * @param coordinate of the anchor or focus point
253 * @param d disortion factor
254 * @param min the beginning of the display
255 * @param max the end of the display
256 * @return the distorted coordinate
258 private int fisheye(int x, int a, double d, int min, int max)
263 int m = (left ? a-min : max-a);
264 if ( m == 0 ) m = max-min;
265 v = (double)(x - a).abs() / m;
267 return (int)Math.round((left?-1:1)*m*v + a);
274 public class Fisheye : Gtk.Window
279 destroy += Gtk.main_quit;
281 var list = new FisheyeList();
286 static int main (string[] args) {
289 var fe = new Fisheye();
290 fe.set_size_request(200, 300);