2 * FisheyeListView - View a TreeModel as a list that does not need scrolling
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 protected struct FocusInfo
29 protected class PrefixInfo
32 public size_t pfx_len;
34 public int layout_pos;
36 public PrefixInfo(string prefix, int pos)
39 this.pfx_len = prefix.size();
45 * If s and prefix share a prefix, set prefix to the common prefix, and
49 public bool refine(string s, int min_prefix_size=1)
52 for (len = 0; prefix[len] != 0 && s[len] != 0 && s[len] == prefix[len]; ++len)
54 if (len < min_prefix_size) return false;
57 prefix = prefix.substring(0, (int)len);
64 protected struct LabelInfo
70 public class FisheyeListView : Gtk.DrawingArea
72 protected Gtk.TreeModel model;
73 protected Gdk.Pixmap background;
74 protected Gdk.Pixmap backing_store;
76 // Renderers used at different sizes
77 protected const int steps = 7;
78 protected Gtk.CellRendererText[] renderers;
79 protected int[] renderer_sizes;
80 protected int max_renderer_size;
82 // Labels to show, extracted from the model
83 protected LabelInfo[] label_cache;
84 protected int label_count;
85 protected bool base_layout_needed;
88 protected List<PrefixInfo> prefixes;
89 protected Pango.Layout prefix_layout;
92 protected weak PrefixInfo cur_pfx;
95 protected int focus_first;
96 protected int focus_end;
97 protected FocusInfo[] focus_info;
98 protected bool focus_locked;
99 protected bool focus_layout_needed;
100 protected bool is_fisheye;
101 protected int focus_step_size;
102 protected int focus_area_start;
104 // Number of elements in full focus
105 protected int focus_size;
107 protected int _label_column;
108 public int label_column {
109 get { return _label_column; }
111 _label_column = value;
112 base_layout_needed = true;
117 protected string _prefix_filter;
118 public string prefix_filter {
119 get { return _prefix_filter; }
121 _prefix_filter = value;
122 base_layout_needed = true;
127 //public virtual signal void cursor_changed ();
128 public signal void row_activated(Gtk.TreePath path);
130 public FisheyeListView()
133 backing_store = null;
138 prefix_filter = null;
140 renderers = new Gtk.CellRendererText[steps];
141 renderer_sizes = new int[steps];
142 prefix_layout = null;
144 // Defaults for properties
150 focus_locked = false;
152 add_events(Gdk.EventMask.POINTER_MOTION_MASK
153 | Gdk.EventMask.BUTTON_PRESS_MASK
154 | Gdk.EventMask.BUTTON_RELEASE_MASK);
156 base_layout_needed = true;
157 focus_layout_needed = true;
161 public unowned Gtk.TreeModel get_model() { return model; }
162 public void set_model (Gtk.TreeModel? model)
164 if (this.model != null)
166 this.model.row_changed -= on_row_changed;
167 this.model.row_deleted -= on_row_deleted;
168 this.model.row_has_child_toggled -= on_row_has_child_toggled;
169 this.model.row_inserted -= on_row_inserted;
170 this.model.rows_reordered -= on_rows_reordered;
173 this.model.row_changed += on_row_changed;
174 this.model.row_deleted += on_row_deleted;
175 this.model.row_has_child_toggled += on_row_has_child_toggled;
176 this.model.row_inserted += on_row_inserted;
177 this.model.rows_reordered += on_rows_reordered;
178 base_layout_needed = true;
182 private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
183 private void on_row_deleted(Gtk.TreePath path) { base_layout_needed = true; }
184 private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
185 private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
186 private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { base_layout_needed = true; }
188 /* Mouse button got pressed over widget */
190 public override bool button_press_event(Gdk.EventButton event)
192 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
197 /* Mouse button got released */
198 public override bool button_release_event(Gdk.EventButton event)
200 if (model == null) return false;
201 //stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
203 // Emit row_activated if applicable
204 int x = (int)event.x;
205 if (x < allocation.width / 3)
207 if (label_cache[cur_el].index == -1)
209 // The case of "reset filter"
210 prefix_filter = null;
213 if (model.iter_nth_child(out iter, null, label_cache[cur_el].index))
215 Gtk.TreePath path = model.get_path(iter);
219 } else if (cur_pfx != null && x > allocation.width * 2 / 3) {
220 //stderr.printf("Mouse released on prefix %s\n", cur_pfx.prefix);
221 prefix_filter = cur_pfx.prefix;
226 /* Mouse pointer moved over widget */
227 public override bool motion_notify_event(Gdk.EventMotion event)
229 int old_cur_el = cur_el;
230 int x = (int)event.x;
231 int y = (int)event.y;
235 focus_locked = !focus_layout_needed && x < allocation.width/3 && y >= focus_area_start+focus_info[0].y && y < focus_area_start+focus_info[focus_end - focus_first].y;
237 if (!focus_locked || y < focus_area_start+focus_info[0].y || y >= focus_area_start+focus_info[focus_end - focus_first].y)
238 cur_el = y * (label_count+1) / allocation.height;
240 for (int idx = 0; idx < focus_info.length; ++idx)
241 if (y - focus_area_start < focus_info[idx].y + renderer_sizes[focus_info[idx].renderer])
243 cur_el = idx + focus_first;
247 if (prefixes != null)
249 cur_pfx = prefixes.data;
250 for (weak List<PrefixInfo> i = prefixes; i != null && y > i.data.layout_pos; i = i.next)
256 if (!focus_locked && old_cur_el != cur_el)
257 focus_layout_needed = true;
259 cur_el = y / max_renderer_size;
260 if (cur_el >= label_count)
261 cur_el = label_count - 1;
264 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
265 if (old_cur_el != cur_el)
273 public override bool configure_event (Gdk.EventConfigure event)
275 base_layout_needed = true;
280 /* Widget is asked to draw itself */
281 public override bool expose_event (Gdk.EventExpose event)
285 window.draw_drawable(
286 get_style().fg_gc[Gtk.StateType.NORMAL],
288 event.area.x, event.area.y,
289 event.area.x, event.area.y,
290 event.area.width, event.area.height);
295 public override void style_set(Gtk.Style? previous_style)
297 base_layout_needed = true;
299 public override void direction_changed(Gtk.TextDirection previous_direction)
301 base_layout_needed = true;
304 protected Gtk.CellRendererText make_cell_renderer()
306 var res = new Gtk.CellRendererText();
307 res.font_desc = get_style().font_desc;
312 protected void base_layout()
314 // Rebuild label cache
316 prefixes = new List<PrefixInfo>();
323 if (!model.get_iter_first(out iter))
330 int count = model.iter_n_children(null);
331 label_cache = new LabelInfo[count + 1];
335 PrefixInfo pi = null;
336 int min_prefix_size = _prefix_filter == null ? 1 : (int)_prefix_filter.size() + 1;
339 model.get(iter, _label_column, out val, -1);
341 // Apply prefix filter
342 if (_prefix_filter == null || val.has_prefix(_prefix_filter))
344 label_cache[lc_idx].label = val;
345 label_cache[lc_idx].index = model_idx;
346 if (pi == null || !pi.refine(val, min_prefix_size))
348 if (pi != null) prefixes.append(pi);
349 pi = new PrefixInfo(val, lc_idx);
354 } while (model.iter_next(ref iter));
355 if (_prefix_filter != null)
357 label_cache[lc_idx].label = "(reset prefix)";
358 label_cache[lc_idx].index = -1;
362 label_count = lc_idx;
365 for (weak List<PrefixInfo> i = prefixes; i != null; i = i.next)
366 i.data.layout_pos = i.data.pos * allocation.height / (label_count+1);
368 background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
369 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
371 // Recreate the renderers
372 renderers[renderers.length-1] = make_cell_renderer();
373 renderers[renderers.length-1].set_fixed_height_from_font(1);
374 renderers[renderers.length-1].get_size(this, null, null, null, null, out max_renderer_size);
375 renderer_sizes[renderers.length-1] = max_renderer_size;
377 focus_size = allocation.height / (2*max_renderer_size);
378 if (focus_size % 2 == 1)
380 focus_info = new FocusInfo[focus_size+2*steps+1];
382 is_fisheye = label_count * max_renderer_size > allocation.height;
386 renderers[0] = make_cell_renderer();
387 renderers[0].size = Pango.SCALE;
388 renderers[0].set_fixed_height_from_font(1);
389 int min_renderer_size;
390 renderers[0].get_size(this, null, null, null, null, out min_renderer_size);
391 renderer_sizes[0] = min_renderer_size;
393 focus_step_size = 0; // Size of the diminishing area
394 for (int i = 1; i < renderers.length-1; ++i)
396 renderers[i] = make_cell_renderer();
397 renderers[i].scale = Math.sqrt((double)i / renderers.length);
398 renderers[i].set_fixed_height_from_font(1);
400 renderers[i].get_size(this, null, null, null, null, out size);
401 renderer_sizes[i] = size;
402 focus_step_size += size;
407 draw_background(background);
409 base_layout_needed = false;
410 focus_layout_needed = true;
413 protected int el_renderer(int idx)
415 int fs2 = focus_size/2;
418 renderer_idx = idx - (cur_el-fs2-steps);
420 renderer_idx = (cur_el+fs2+steps) - idx;
421 if (renderer_idx < 0)
423 if (renderer_idx >= renderer_sizes.length)
424 return renderer_sizes.length-1;
428 protected void focus_layout()
430 if (!is_fisheye || label_count == 0)
434 focus_layout_needed = false;
438 focus_first = cur_el - focus_size/2 - steps;
439 if (focus_first < 0) focus_first = 0;
440 if (focus_first + focus_info.length > label_count)
441 focus_first = label_count - focus_info.length;
445 int focus_area_pre = 0;
446 while (cur_pos < allocation.height && cur_idx < focus_info.length)
448 if (focus_first + cur_idx == cur_el)
449 focus_area_pre = cur_pos;
450 focus_info[cur_idx].y = cur_pos;
451 focus_info[cur_idx].renderer = el_renderer(focus_first + cur_idx);
452 // stderr.printf("LAYOUT %d+[0-%d-%d] item %d/%d: pos %d rend %d rsz %d\n",
453 // focus_first, cur_idx, focus_info.length, focus_first + cur_idx, label_count,
454 // cur_pos, focus_info[cur_idx].renderer, renderer_sizes[focus_info[cur_idx].renderer]);
455 cur_pos += renderer_sizes[focus_info[cur_idx].renderer];
459 focus_info[cur_idx].y = cur_pos;
460 focus_info[cur_idx].renderer = 0;
461 focus_end = focus_first + cur_idx;
463 int anchor_y = cur_el * allocation.height / (label_count+1);
464 int focus_area_size = cur_pos;
466 focus_area_start = anchor_y - focus_area_pre;
467 if (focus_area_start < 0) focus_area_start = 0;
468 if (focus_area_start + focus_area_size > allocation.height)
469 focus_area_start = allocation.height - focus_area_size;
471 // stderr.printf("FA [0 [%d+%d=%d] %d]\n", focus_area_start, focus_area_size, focus_area_start+focus_area_size, allocation.height);
473 focus_layout_needed = false;
476 protected void draw_background(Gdk.Drawable drawable)
478 Gtk.Style style = get_style();
481 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
486 // Focus movement area
487 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
488 allocation.width/3, 0, allocation.width/3, allocation.height);
490 for (int y = 0; y < allocation.height/2 - 30; y += 30)
492 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
493 null, this, null, Gtk.ArrowType.UP, false,
494 allocation.width/3, y, allocation.width/3, 10);
495 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
496 null, this, null, Gtk.ArrowType.DOWN, false,
497 allocation.width/3, allocation.height-y-30, allocation.width/3, 10);
501 Gdk.Rectangle expose_area = Gdk.Rectangle();
502 expose_area.x = expose_area.y = 0;
503 expose_area.width = allocation.width;
504 expose_area.height = allocation.height;
506 Gdk.Rectangle cell_area = Gdk.Rectangle();
508 cell_area.width = allocation.width;
509 cell_area.height = renderer_sizes[0];
510 if (label_count * cell_area.height >= allocation.height)
512 for (int y = 0; y < allocation.height; y += cell_area.height)
514 int idx = y * label_count / allocation.height;
516 renderers[0].text = label_cache[idx].label;
517 renderers[0].render((Gdk.Window*)drawable, this,
524 int count = int.min(allocation.height/(2*cell_area.height), label_count);
525 for (int idx = 0; idx < count; ++idx)
527 cell_area.y = idx * cell_area.height;
528 renderers[0].text = label_cache[idx].label;
529 renderers[0].render((Gdk.Window*)drawable, this,
535 cell_area.y = allocation.height-cell_area.y;
536 renderers[0].text = label_cache[label_count-idx].label;
537 renderers[0].render((Gdk.Window*)drawable, this,
545 // Draw prefix labels
546 prefix_layout = create_pango_layout(null);
548 prefix_layout.get_extents(null, out lr);
549 int label_height = lr.height / Pango.SCALE;
550 prefix_layout.set_alignment(Pango.Alignment.RIGHT);
551 prefix_layout.set_width(allocation.width*Pango.SCALE/3);
552 prefix_layout.set_height(0);
553 prefix_layout.set_ellipsize(Pango.EllipsizeMode.END);
555 int last_covered = 0;
557 for (weak List<PrefixInfo> i = prefixes; i != null; i = i.next)
559 if (i.data.layout_pos < last_covered)
560 i.data.layout_pos = last_covered;
561 prefix_layout.set_text(i.data.prefix, (int)i.data.pfx_len);
562 Gtk.paint_layout(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, true, null, this, null, allocation.width*2/3, i.data.layout_pos, prefix_layout);
563 last_covered = i.data.layout_pos + label_height;
567 protected void draw()
569 if (base_layout_needed)
571 if (focus_layout_needed)
574 var drawable = backing_store;
575 Gtk.Style style = get_style();
576 Gdk.Rectangle expose_area = Gdk.Rectangle();
577 expose_area.x = expose_area.y = 0;
578 expose_area.width = allocation.width;
579 expose_area.height = allocation.height;
582 drawable.draw_drawable(
583 get_style().fg_gc[Gtk.StateType.NORMAL],
586 allocation.width, allocation.height);
590 // Paint current prefix
593 prefix_layout.set_text(cur_pfx.prefix, (int)cur_pfx.pfx_len);
594 Gtk.paint_layout(style, (Gdk.Window*)drawable, Gtk.StateType.NORMAL, true, null, this, null, allocation.width*2/3, cur_pfx.layout_pos, prefix_layout);
598 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
599 0, focus_area_start + focus_info[0].y, allocation.width/3, focus_info[focus_end - focus_first].y);
601 // Paint items around focus
602 Gdk.Rectangle cell_area = Gdk.Rectangle();
604 cell_area.width = expose_area.width;
605 for (int idx = 0; idx < focus_info.length; ++idx)
607 var renderer = renderers[focus_info[idx].renderer];
608 cell_area.y = focus_area_start + focus_info[idx].y;
609 cell_area.height = renderer_sizes[focus_info[idx].renderer];
611 Gtk.CellRendererState rflags = 0;
612 if (idx + focus_first == cur_el)
614 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
615 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
616 cell_area.x, cell_area.y, allocation.width, cell_area.height);
619 renderer.text = label_cache[idx + focus_first].label;
620 renderer.render((Gdk.Window*)drawable, this,
626 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
629 // Paint all items sequentially
630 var renderer = renderers[renderers.length-1];
631 Gdk.Rectangle cell_area = Gdk.Rectangle();
633 cell_area.width = allocation.width;
634 cell_area.height = max_renderer_size;
635 for (int idx = 0; idx < label_count; ++idx)
637 cell_area.y = idx * cell_area.height;
639 Gtk.CellRendererState rflags = 0;
642 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
643 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
644 cell_area.x, cell_area.y, cell_area.width, cell_area.height);
647 renderer.text = label_cache[idx].label;
648 renderer.render((Gdk.Window*)drawable, this,
658 * A relevant annotation from Prefuse:
660 * For more details on this form of transformation, see Manojit Sarkar and
661 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
662 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
663 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
664 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
666 * See also http://www.cs.umd.edu/hcil/fisheyemenu/