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 background;
27 protected Gdk.Pixmap backing_store;
29 // Pango layouts cached for speed
30 protected const int steps = 5;
31 protected Gtk.CellRendererText[] renderers;
33 // Labels to show, extracted from the model
34 protected string[] label_cache;
35 protected bool base_layout_needed;
40 protected int focus_first;
41 protected int focus_end;
42 protected int[] focus_starts;
43 protected bool focus_locked;
44 protected bool focus_layout_needed;
46 protected int _focus_size;
47 public int focus_size {
48 get { return _focus_size; }
51 focus_starts = new int[value+1];
52 focus_layout_needed = true;
57 protected int _label_column;
58 public int label_column {
59 get { return _label_column; }
61 _label_column = value;
62 base_layout_needed = true;
67 //public virtual signal void cursor_changed ();
68 public signal void row_activated(Gtk.TreePath path);
77 renderers = new Gtk.CellRendererText[steps];
79 // Defaults for properties
86 add_events(Gdk.EventMask.POINTER_MOTION_MASK
87 | Gdk.EventMask.BUTTON_PRESS_MASK
88 | Gdk.EventMask.BUTTON_RELEASE_MASK);
91 public unowned Gtk.TreeModel get_model() { return model; }
92 public void set_model (Gtk.TreeModel? model)
94 if (this.model != null)
96 this.model.row_changed -= on_row_changed;
97 this.model.row_deleted -= on_row_deleted;
98 this.model.row_has_child_toggled -= on_row_has_child_toggled;
99 this.model.row_inserted -= on_row_inserted;
100 this.model.rows_reordered -= on_rows_reordered;
103 this.model.row_changed += on_row_changed;
104 this.model.row_deleted += on_row_deleted;
105 this.model.row_has_child_toggled += on_row_has_child_toggled;
106 this.model.row_inserted += on_row_inserted;
107 this.model.rows_reordered += on_rows_reordered;
108 base_layout_needed = true;
112 private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
113 private void on_row_deleted(Gtk.TreePath path) { base_layout_needed = true; }
114 private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
115 private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
116 private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { base_layout_needed = true; }
118 /* Mouse button got pressed over widget */
120 public override bool button_press_event(Gdk.EventButton event)
122 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
127 /* Mouse button got released */
128 public override bool button_release_event(Gdk.EventButton event)
130 stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
132 // Emit row_activated if applicable
136 if (model.iter_nth_child(out iter, null, cur_el))
138 Gtk.TreePath path = model.get_path(iter);
145 /* Mouse pointer moved over widget */
146 public override bool motion_notify_event(Gdk.EventMotion event)
148 int old_cur_el = cur_el;
149 int x = (int)event.x;
150 int y = (int)event.y;
152 focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
156 for (int idx = focus_first; idx < focus_end; ++idx)
157 if (y < focus_starts[idx-focus_first+1])
164 cur_el = y * label_cache.length / allocation.height;
165 if (old_cur_el != cur_el)
166 focus_layout_needed = true;
169 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
170 if (old_cur_el != cur_el)
178 public override bool configure_event (Gdk.EventConfigure event)
180 base_layout_needed = true;
185 /* Widget is asked to draw itself */
186 public override bool expose_event (Gdk.EventExpose event)
190 window.draw_drawable(
191 get_style().fg_gc[Gtk.StateType.NORMAL],
193 event.area.x, event.area.y,
194 event.area.x, event.area.y,
195 event.area.width, event.area.height);
200 public override void style_set(Gtk.Style? previous_style)
202 base_layout_needed = true;
204 public override void direction_changed(Gtk.TextDirection previous_direction)
206 base_layout_needed = true;
209 protected int el_y(int idx)
211 // Distorted position
212 int pos = fisheye(idx);
213 //stderr.printf("%d %f %f\n", idx, undy, pos);
217 protected Gtk.CellRendererText make_cell_renderer()
219 var res = new Gtk.CellRendererText();
220 res.font_desc = get_style().font_desc;
225 protected void base_layout()
227 background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
228 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
230 // Recreate the renderers
231 renderers[renderers.length-1] = make_cell_renderer();
232 renderers[renderers.length-1].set_fixed_height_from_font(1);
234 renderers[0] = make_cell_renderer();
235 renderers[0].size = Pango.SCALE;
236 renderers[0].set_fixed_height_from_font(1);
238 for (int i = 1; i < renderers.length-1; ++i)
240 renderers[i] = make_cell_renderer();
241 renderers[i].scale = (double)i / renderers.length;
242 renderers[i].set_fixed_height_from_font(1);
247 label_cache = new string[0];
250 if (!model.get_iter_first(out iter))
252 label_cache = new string[0];
256 int count = model.iter_n_children(null);
257 label_cache = new string[count];
262 model.get(iter, _label_column, out val, -1);
263 label_cache[i] = val;
265 } while (model.iter_next(ref iter));
270 draw_background(background);
272 base_layout_needed = false;
273 focus_layout_needed = true;
276 protected void focus_layout()
278 if (label_cache.length == 0)
284 if (_focus_size >= label_cache.length)
287 focus_end = label_cache.length;
289 focus_first = cur_el -_focus_size/2;
290 if (focus_first < 0) focus_first = 0;
292 focus_end = focus_first + _focus_size;
293 if (focus_end > label_cache.length)
295 focus_end = label_cache.length;
296 focus_first = focus_end - _focus_size;
300 // Compute starting positions for all items in focus
301 for (int idx = focus_first; idx <= focus_end; ++idx)
302 focus_starts[idx - focus_first] = el_y(idx);
304 focus_layout_needed = false;
307 protected void draw_background(Gdk.Drawable drawable)
309 Gtk.Style style = get_style();
312 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
314 // Focus movement area
315 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
316 allocation.width/2, 0, allocation.width, allocation.height);
318 for (int y = 0; y < allocation.height/2 - 30; y += 30)
320 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
321 null, this, null, Gtk.ArrowType.UP, false,
322 allocation.width/2, y, allocation.width/2, 10);
323 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
324 null, this, null, Gtk.ArrowType.DOWN, false,
325 allocation.width/2, allocation.height-y-30, allocation.width/2, 10);
328 Gdk.Rectangle expose_area = Gdk.Rectangle();
329 expose_area.x = expose_area.y = 0;
330 expose_area.width = allocation.width;
331 expose_area.height = allocation.height;
333 Gdk.Rectangle cell_area = Gdk.Rectangle();
335 cell_area.width = allocation.width;
336 renderers[0].get_size(this, null, null, null, null, out cell_area.height);
337 if (label_cache.length * cell_area.height >= allocation.height)
339 for (int y = 0; y < allocation.height; ++y)
341 int idx = y * label_cache.length / allocation.height;
343 renderers[0].text = label_cache[idx];
344 renderers[0].render((Gdk.Window*)drawable, this,
351 int count = int.min(allocation.height/(2*cell_area.height), label_cache.length);
352 for (int idx = 0; idx < count; ++idx)
354 cell_area.y = idx * cell_area.height;
355 renderers[0].text = label_cache[idx];
356 renderers[0].render((Gdk.Window*)drawable, this,
362 cell_area.y = allocation.height-cell_area.y;
363 renderers[0].text = label_cache[label_cache.length-idx];
364 renderers[0].render((Gdk.Window*)drawable, this,
373 protected void draw()
375 if (base_layout_needed)
377 if (focus_layout_needed)
380 var drawable = backing_store;
381 Gtk.Style style = get_style();
382 Gdk.Rectangle expose_area = Gdk.Rectangle();
383 expose_area.x = expose_area.y = 0;
384 expose_area.width = allocation.width;
385 expose_area.height = allocation.height;
388 drawable.draw_drawable(
389 get_style().fg_gc[Gtk.StateType.NORMAL],
392 allocation.width, allocation.height);
395 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
396 0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]-focus_starts[0]);
398 // Paint items around focus
399 Gdk.Rectangle cell_area = Gdk.Rectangle();
401 cell_area.width = allocation.width;
402 for (int idx = 0; idx < focus_end-focus_first; ++idx)
404 int y0 = focus_starts[idx];
405 int y1 = focus_starts[idx + 1];
407 cell_area.height = y1-y0;
409 Gtk.CellRendererState rflags = 0;
410 if (idx + focus_first == cur_el)
412 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
413 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
414 cell_area.x, cell_area.y, cell_area.width, cell_area.height);
418 if (size <= 0) size = 1;
419 if (size >= renderers.length) size = renderers.length - 1;
420 renderers[size].text = label_cache[idx + focus_first];
421 renderers[size].render((Gdk.Window*)drawable, this,
427 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
432 * The following function is adapted from Prefuse's FisheyeDistortion.java.
434 * A relevant annotation from Prefuse:
436 * For more details on this form of transformation, see Manojit Sarkar and
437 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
438 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
439 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
440 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
444 * Distorts an item's coordinate.
445 * @param x the undistorted coordinate
446 * @param coordinate of the anchor or focus point
447 * @param d disortion factor
448 * @param min the beginning of the display
449 * @param max the end of the display
450 * @return the distorted coordinate
452 private int fisheye(int idx)
454 // Autocompute distortion factor
455 // 20 is the pixel size of the item at centre of focus
456 double d = label_cache.length * 20 / allocation.height;
458 return idx * allocation.height / label_cache.length;
460 double a = (double)cur_el * allocation.height / label_cache.length;
461 double x = (double)idx * allocation.height / label_cache.length;
462 double max = (double)allocation.height;
467 if ( m == 0 ) m = max;
468 double v = (double)(a - x) / m;
469 v = (double)(d+1)/(d+(1/v));
470 return (int)Math.round(a - m*v);
473 if ( m == 0 ) m = max;
474 double v = (double)(x - a) / m;
475 v = (double)(d+1)/(d+(1/v));
476 return (int)Math.round(a + m*v);
481 public class Fisheye : Gtk.Window
486 destroy += Gtk.main_quit;
488 var list = new FisheyeList();
491 var store = new Gtk.ListStore(1, typeof(string));
493 var infd = FileStream.open("/tmp/names", "r");
496 for (int i = 0; i < 300; ++i)
498 store.append(out iter);
499 store.set(iter, 0, "Antani %d".printf(i), -1);
505 string line = infd.gets(buf);
508 store.append(out iter);
509 store.set(iter, 0, line, -1);
513 list.set_model(store);
517 static int main (string[] args) {
520 var fe = new Fisheye();
521 fe.set_size_request(200, 300);