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 protected const int max_font_size = 30;
30 protected Pango.Layout[] pango_cache;
32 // Labels to show, extracted from the model
33 protected string[] label_cache;
34 protected bool build_label_cache_needed;
39 protected int focus_first;
40 protected int focus_end;
41 protected int[] focus_starts;
42 protected bool focus_locked;
43 protected bool focus_layout_needed;
45 protected int _focus_size;
46 public int focus_size {
47 get { return _focus_size; }
50 focus_starts = new int[value+1];
51 focus_layout_needed = true;
56 protected int _label_column;
57 public int label_column {
58 get { return _label_column; }
60 _label_column = value;
61 build_label_cache_needed = true;
66 //public virtual signal void cursor_changed ();
67 public signal void row_activated(Gtk.TreePath path);
76 pango_cache = new Pango.Layout[max_font_size];
77 for (int i = 0; i < pango_cache.length; ++i)
78 pango_cache[i] = null;
80 // Defaults for properties
87 add_events(Gdk.EventMask.POINTER_MOTION_MASK
88 | Gdk.EventMask.BUTTON_PRESS_MASK
89 | Gdk.EventMask.BUTTON_RELEASE_MASK);
92 public unowned Gtk.TreeModel get_model() { return model; }
93 public void set_model (Gtk.TreeModel? model)
95 if (this.model != null)
97 this.model.row_changed -= on_row_changed;
98 this.model.row_deleted -= on_row_deleted;
99 this.model.row_has_child_toggled -= on_row_has_child_toggled;
100 this.model.row_inserted -= on_row_inserted;
101 this.model.rows_reordered -= on_rows_reordered;
104 this.model.row_changed += on_row_changed;
105 this.model.row_deleted += on_row_deleted;
106 this.model.row_has_child_toggled += on_row_has_child_toggled;
107 this.model.row_inserted += on_row_inserted;
108 this.model.rows_reordered += on_rows_reordered;
109 build_label_cache_needed = true;
113 private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { build_label_cache_needed = true; }
114 private void on_row_deleted(Gtk.TreePath path) { build_label_cache_needed = true; }
115 private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { build_label_cache_needed = true; }
116 private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { build_label_cache_needed = true; }
117 private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { build_label_cache_needed = true; }
119 /* Mouse button got pressed over widget */
121 public override bool button_press_event(Gdk.EventButton event)
123 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
128 /* Mouse button got released */
129 public override bool button_release_event(Gdk.EventButton event)
131 stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
133 // Emit row_activated if applicable
137 if (model.iter_nth_child(out iter, null, cur_el))
139 Gtk.TreePath path = model.get_path(iter);
146 /* Mouse pointer moved over widget */
147 public override bool motion_notify_event(Gdk.EventMotion event)
149 int old_cur_el = cur_el;
150 int x = (int)event.x;
151 int y = (int)event.y;
153 focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
157 for (int idx = focus_first; idx < focus_end; ++idx)
158 if (y < focus_starts[idx-focus_first+1])
165 cur_el = y * label_cache.length / allocation.height;
166 if (old_cur_el != cur_el)
167 focus_layout_needed = true;
170 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
171 if (old_cur_el != cur_el)
179 public override bool configure_event (Gdk.EventConfigure event)
181 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
182 focus_layout_needed = true;
187 /* Widget is asked to draw itself */
188 public override bool expose_event (Gdk.EventExpose event)
190 if (backing_store == null)
195 window.draw_drawable(
196 get_style().fg_gc[Gtk.StateType.NORMAL],
198 event.area.x, event.area.y,
199 event.area.x, event.area.y,
200 event.area.width, event.area.height);
205 public override void style_set(Gtk.Style? previous_style)
207 // Reset the pango cache if the pango context changes
208 for (int i = 0; i < pango_cache.length; ++i)
209 pango_cache[i] = null;
211 public override void direction_changed(Gtk.TextDirection previous_direction)
213 // Reset the pango cache if the pango context changes
214 for (int i = 0; i < pango_cache.length; ++i)
215 pango_cache[i] = null;
218 protected int el_y(int idx)
220 // Distorted position
221 int pos = fisheye(idx);
222 //stderr.printf("%d %f %f\n", idx, undy, pos);
226 protected void build_label_cache()
230 label_cache = new string[0];
233 if (!model.get_iter_first(out iter))
235 label_cache = new string[0];
239 int count = model.iter_n_children(null);
240 label_cache = new string[count];
245 model.get(iter, _label_column, out val, -1);
246 label_cache[i] = val;
248 } while (model.iter_next(ref iter));
252 build_label_cache_needed = false;
253 focus_layout_needed = true;
256 protected void focus_layout()
258 if (label_cache.length == 0)
264 focus_first = cur_el > _focus_size/2 ? cur_el-_focus_size/2 : 0;
265 focus_end = focus_first + _focus_size;
266 if (focus_end >= label_cache.length) focus_end = label_cache.length;
268 // Compute starting positions for all items in focus
269 for (int idx = focus_first; idx <= focus_end; ++idx)
271 focus_starts[idx - focus_first] = el_y(idx);
274 focus_layout_needed = false;
277 protected void draw(Gdk.Drawable drawable)
279 if (build_label_cache_needed)
281 if (focus_layout_needed)
284 Gtk.Style style = get_style();
287 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
290 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
291 0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
293 // Focus movement area
294 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
295 allocation.width/2, 0, allocation.width, allocation.height);
297 // Create a Cairo context
298 //var context = Gdk.cairo_create (drawable);
300 // Paint items around focus
301 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
302 for (int idx = focus_first; idx < focus_end; ++idx)
304 int y0 = focus_starts[idx - focus_first];
305 int y1 = focus_starts[idx - focus_first + 1];
307 Gtk.StateType itemState = Gtk.StateType.NORMAL;
310 itemState = Gtk.StateType.SELECTED;
311 drawable.draw_rectangle(style.bg_gc[itemState], true,
312 0, y0, allocation.width, y1-y0);
316 // TODO: cache pango contexts instead of fontdescs
317 int size = (y1-y0)*80/100;
318 if (size <= 0) size = 1;
319 if (size >= pango_cache.length) size = pango_cache.length - 1;
320 if (pango_cache[size] == null)
322 var fd = style.font_desc.copy();
323 fd.set_absolute_size(size*Pango.SCALE);
324 var pc = create_pango_context();
325 pc.set_font_description(fd);
326 pango_cache[size] = new Pango.Layout(pc);
328 pango_cache[size].set_text(label_cache[idx], -1);
329 var layout = pango_cache[size];
330 //stderr.printf("AZAZA %p\n", layout.get_attributes());
331 //var attrlist = layout.get_attributes().copy();
332 //stderr.printf("AL %p\n", attrlist);
333 //var attrlist = new Pango.AttrList();
334 //stderr.printf("SIZE %d\n", y1-y0);
335 //attrlist.insert(new Pango.AttrSize(y1-y0));
336 //var attrlist = layout.get_attributes();
337 //attrlist.change(new Pango.AttrSize(y1-y0));
338 //layout.set_attributes(attrlist);
339 //layout.set_height(y1-y0);
341 //layout.get_pixel_size(out w, out h);
342 Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
347 * The following function is adapted from Prefuse's FisheyeDistortion.java.
349 * A relevant annotation from Prefuse:
351 * For more details on this form of transformation, see Manojit Sarkar and
352 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
353 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
354 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
355 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
359 * Distorts an item's coordinate.
360 * @param x the undistorted coordinate
361 * @param coordinate of the anchor or focus point
362 * @param d disortion factor
363 * @param min the beginning of the display
364 * @param max the end of the display
365 * @return the distorted coordinate
367 private int fisheye(int idx)
369 // Autocompute distortion factor
370 // 20 is the pixel size of the item at centre of focus
371 double d = label_cache.length * 20 / allocation.height;
373 return idx * allocation.height / label_cache.length;
375 double a = (double)cur_el * allocation.height / label_cache.length;
376 double x = (double)idx * allocation.height / label_cache.length;
377 double max = (double)allocation.height;
382 if ( m == 0 ) m = max;
383 double v = (double)(a - x) / m;
384 v = (double)(d+1)/(d+(1/v));
385 return (int)Math.round(a - m*v);
388 if ( m == 0 ) m = max;
389 double v = (double)(x - a) / m;
390 v = (double)(d+1)/(d+(1/v));
391 return (int)Math.round(a + m*v);
396 public class Fisheye : Gtk.Window
401 destroy += Gtk.main_quit;
403 var list = new FisheyeList();
406 var store = new Gtk.ListStore(1, typeof(string));
408 var infd = FileStream.open("/tmp/names", "r");
411 for (int i = 0; i < 300; ++i)
413 store.append(out iter);
414 store.set(iter, 0, "Antani %d".printf(i), -1);
420 string line = infd.gets(buf);
423 store.append(out iter);
424 store.set(iter, 0, line, -1);
428 list.set_model(store);
432 static int main (string[] args) {
435 var fe = new Fisheye();
436 fe.set_size_request(200, 300);