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 if (_focus_size >= label_cache.length)
267 focus_end = label_cache.length;
269 focus_first = cur_el -_focus_size/2;
270 if (focus_first < 0) focus_first = 0;
272 focus_end = focus_first + _focus_size;
273 if (focus_end > label_cache.length)
275 focus_end = label_cache.length;
276 focus_first = focus_end - _focus_size;
280 // Compute starting positions for all items in focus
281 for (int idx = focus_first; idx <= focus_end; ++idx)
282 focus_starts[idx - focus_first] = el_y(idx);
284 focus_layout_needed = false;
287 protected void draw(Gdk.Drawable drawable)
289 if (build_label_cache_needed)
291 if (focus_layout_needed)
294 Gtk.Style style = get_style();
297 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
300 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
301 0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]-focus_starts[0]);
303 // Focus movement area
304 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
305 allocation.width/2, 0, allocation.width, allocation.height);
307 // Paint items around focus
308 for (int idx = 0; idx < focus_end-focus_first; ++idx)
310 int y0 = focus_starts[idx];
311 int y1 = focus_starts[idx + 1];
313 Gtk.StateType itemState = Gtk.StateType.NORMAL;
314 if (idx + focus_first == cur_el)
316 itemState = Gtk.StateType.SELECTED;
317 drawable.draw_rectangle(style.bg_gc[itemState], true,
318 0, y0, allocation.width, y1-y0);
322 int size = (y1-y0)*80/100;
323 if (size <= 0) size = 1;
324 if (size >= pango_cache.length) size = pango_cache.length - 1;
325 if (pango_cache[size] == null)
327 var fd = style.font_desc.copy();
328 fd.set_absolute_size(size*Pango.SCALE);
329 var pc = create_pango_context();
330 pc.set_font_description(fd);
331 pango_cache[size] = new Pango.Layout(pc);
333 pango_cache[size].set_text(label_cache[idx + focus_first], -1);
334 var layout = pango_cache[size];
335 Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
337 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
342 * The following function is adapted from Prefuse's FisheyeDistortion.java.
344 * A relevant annotation from Prefuse:
346 * For more details on this form of transformation, see Manojit Sarkar and
347 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
348 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
349 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
350 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
354 * Distorts an item's coordinate.
355 * @param x the undistorted coordinate
356 * @param coordinate of the anchor or focus point
357 * @param d disortion factor
358 * @param min the beginning of the display
359 * @param max the end of the display
360 * @return the distorted coordinate
362 private int fisheye(int idx)
364 // Autocompute distortion factor
365 // 20 is the pixel size of the item at centre of focus
366 double d = label_cache.length * 20 / allocation.height;
368 return idx * allocation.height / label_cache.length;
370 double a = (double)cur_el * allocation.height / label_cache.length;
371 double x = (double)idx * allocation.height / label_cache.length;
372 double max = (double)allocation.height;
377 if ( m == 0 ) m = max;
378 double v = (double)(a - x) / m;
379 v = (double)(d+1)/(d+(1/v));
380 return (int)Math.round(a - m*v);
383 if ( m == 0 ) m = max;
384 double v = (double)(x - a) / m;
385 v = (double)(d+1)/(d+(1/v));
386 return (int)Math.round(a + m*v);
391 public class Fisheye : Gtk.Window
396 destroy += Gtk.main_quit;
398 var list = new FisheyeList();
401 var store = new Gtk.ListStore(1, typeof(string));
403 var infd = FileStream.open("/tmp/names", "r");
406 for (int i = 0; i < 300; ++i)
408 store.append(out iter);
409 store.set(iter, 0, "Antani %d".printf(i), -1);
415 string line = infd.gets(buf);
418 store.append(out iter);
419 store.set(iter, 0, line, -1);
423 list.set_model(store);
427 static int main (string[] args) {
430 var fe = new Fisheye();
431 fe.set_size_request(200, 300);