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 Gtk.TreeModel model;
26 protected Gdk.Pixmap backing_store;
28 // Pango layouts cached for speed
29 // TODO: If you create and keep a PangoLayout using this context, you
30 // must deal with changes to the context by calling
31 // pango_layout_context_changed() on the layout in response to the
32 // "style-set" and "direction-changed" signals for the widget.
33 protected Pango.Layout[] pango_cache;
35 // Labels to show, extracted from the model
36 protected string[] label_cache;
37 protected bool build_label_cache_needed;
42 protected int focus_first;
43 protected int focus_end;
44 protected int[] focus_starts;
45 protected bool focus_locked;
46 protected int focus_centre;
47 protected bool focus_layout_needed;
49 protected int _focus_size;
50 public int focus_size {
51 get { return _focus_size; }
54 focus_starts = new int[value+1];
55 focus_layout_needed = true;
60 protected int _distortion_factor;
61 public int distortion_factor {
62 get { return _distortion_factor; }
64 _distortion_factor = value;
65 focus_layout_needed = true;
76 build_label_cache_needed = true;
78 pango_cache = new Pango.Layout[30];
79 for (int i = 0; i < 30; ++i)
80 pango_cache[i] = null;
82 // Defaults for properties
84 distortion_factor = 30;
90 add_events(Gdk.EventMask.POINTER_MOTION_MASK
91 | Gdk.EventMask.BUTTON_PRESS_MASK
92 | Gdk.EventMask.BUTTON_RELEASE_MASK);
95 public unowned Gtk.TreeModel get_model() { return model; }
96 public void set_model (Gtk.TreeModel? model)
99 build_label_cache_needed = true;
103 /* Mouse button got pressed over widget */
104 public override bool button_press_event(Gdk.EventButton event)
106 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
110 /* Mouse button got released */
111 public override bool button_release_event(Gdk.EventButton event)
113 stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
118 /* Mouse pointer moved over widget */
119 public override bool motion_notify_event(Gdk.EventMotion event)
121 int old_cur_el = cur_el;
122 int x = (int)event.x;
123 int y = (int)event.y;
125 focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
129 for (int idx = focus_first; idx < focus_end; ++idx)
130 if (y < focus_starts[idx-focus_first+1])
137 cur_el = y * label_cache.length / allocation.height;
138 if (old_cur_el != cur_el)
139 focus_layout_needed = true;
142 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
143 if (old_cur_el != cur_el)
151 public override bool configure_event (Gdk.EventConfigure event)
153 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
154 focus_layout_needed = true;
159 /* Widget is asked to draw itself */
160 public override bool expose_event (Gdk.EventExpose event)
162 if (backing_store == null)
167 window.draw_drawable(
168 get_style().fg_gc[Gtk.StateType.NORMAL],
170 event.area.x, event.area.y,
171 event.area.x, event.area.y,
172 event.area.width, event.area.height);
177 protected int el_y(int idx)
180 int undy = idx * allocation.height / label_cache.length;
181 // Distorted position
182 int pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
183 //stderr.printf("%d %f %f\n", idx, undy, pos);
187 protected void build_label_cache()
191 label_cache = new string[0];
194 if (!model.get_iter_first(out iter))
196 label_cache = new string[0];
200 int count = model.iter_n_children(null);
201 label_cache = new string[count];
206 model.get(iter, 0, out val, -1);
207 label_cache[i] = val;
209 } while (model.iter_next(ref iter));
213 build_label_cache_needed = false;
214 focus_layout_needed = true;
217 protected void focus_layout()
219 if (label_cache.length == 0)
227 focus_centre = cur_el*allocation.height/label_cache.length;
229 focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
230 focus_end = focus_first + focus_size;
231 if (focus_end >= label_cache.length) focus_end = label_cache.length;
233 // Compute starting positions for all items in focus
234 for (int idx = focus_first; idx < focus_end; ++idx)
236 int posprev = idx == 0 ? 0 : el_y(idx-1);
238 int posnext = idx == label_cache.length-1 ? 1 : el_y(idx+1);
239 int y0 = (pos+posprev)/2;
240 int y1 = (pos+posnext)/2;
242 focus_starts[idx - focus_first] = y0;
243 focus_starts[idx - focus_first + 1] = y1;
246 focus_layout_needed = false;
249 protected void draw(Gdk.Drawable drawable)
251 if (build_label_cache_needed)
253 if (focus_layout_needed)
256 Gtk.Style style = get_style();
259 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
262 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
263 0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
265 // Focus movement area
266 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
267 allocation.width/2, 0, allocation.width, allocation.height);
269 // Create a Cairo context
270 //var context = Gdk.cairo_create (drawable);
272 // Paint items around focus
273 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
274 for (int idx = focus_first; idx < focus_end; ++idx)
276 int y0 = focus_starts[idx - focus_first];
277 int y1 = focus_starts[idx - focus_first + 1];
279 Gtk.StateType itemState = Gtk.StateType.NORMAL;
282 itemState = Gtk.StateType.SELECTED;
283 drawable.draw_rectangle(style.bg_gc[itemState], true,
284 0, y0, allocation.width, y1-y0);
288 // TODO: cache pango contexts instead of fontdescs
289 int size = (y1-y0)*80/100;
290 if (size <= 0) size = 1;
291 if (size >= pango_cache.length) size = pango_cache.length - 1;
292 if (pango_cache[size] == null)
294 var fd = style.font_desc.copy();
295 fd.set_absolute_size(size*Pango.SCALE);
296 var pc = create_pango_context();
297 pc.set_font_description(fd);
298 pango_cache[size] = new Pango.Layout(pc);
300 pango_cache[size].set_text(label_cache[idx], -1);
301 var layout = pango_cache[size];
302 //stderr.printf("AZAZA %p\n", layout.get_attributes());
303 //var attrlist = layout.get_attributes().copy();
304 //stderr.printf("AL %p\n", attrlist);
305 //var attrlist = new Pango.AttrList();
306 //stderr.printf("SIZE %d\n", y1-y0);
307 //attrlist.insert(new Pango.AttrSize(y1-y0));
308 //var attrlist = layout.get_attributes();
309 //attrlist.change(new Pango.AttrSize(y1-y0));
310 //layout.set_attributes(attrlist);
311 //layout.set_height(y1-y0);
313 //layout.get_pixel_size(out w, out h);
314 Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
319 * The following function is adapted from Prefuse's FisheyeDistortion.java.
321 * A relevant annotation from Prefuse:
323 * For more details on this form of transformation, see Manojit Sarkar and
324 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
325 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
326 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
327 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
331 * Distorts an item's coordinate.
332 * @param x the undistorted coordinate
333 * @param coordinate of the anchor or focus point
334 * @param d disortion factor
335 * @param min the beginning of the display
336 * @param max the end of the display
337 * @return the distorted coordinate
339 private int fisheye(int x, int a, int d, int min, int max)
344 int m = (left ? a-min : max-a);
345 if ( m == 0 ) m = max-min;
346 v = (double)(x - a).abs() / m;
347 v = (double)(d+1)/(d+(1/v));
348 return (int)Math.round((left?-1:1)*m*v + a);
355 public class Fisheye : Gtk.Window
360 destroy += Gtk.main_quit;
362 var list = new FisheyeList();
365 var store = new Gtk.ListStore(1, typeof(string));
367 for (int i = 0; i < 300; ++i)
369 store.append(out iter);
370 store.set(iter, 0, "Antani %d".printf(i), -1);
373 list.set_model(store);
377 static int main (string[] args) {
380 var fe = new Fisheye();
381 fe.set_size_request(200, 300);