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 = 5;
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;
106 public int focus_size {
107 get { return _focus_size; }
110 focus_info = new FocusInfo[_focus_size+2*steps+1];
111 focus_layout_needed = true;
116 protected int _label_column;
117 public int label_column {
118 get { return _label_column; }
120 _label_column = value;
121 base_layout_needed = true;
126 protected string _prefix_filter;
127 public string prefix_filter {
128 get { return _prefix_filter; }
130 _prefix_filter = value;
131 base_layout_needed = true;
136 //public virtual signal void cursor_changed ();
137 public signal void row_activated(Gtk.TreePath path);
139 public FisheyeListView()
142 backing_store = null;
147 prefix_filter = null;
149 renderers = new Gtk.CellRendererText[steps];
150 renderer_sizes = new int[steps];
151 prefix_layout = null;
153 // Defaults for properties
159 focus_locked = false;
161 add_events(Gdk.EventMask.POINTER_MOTION_MASK
162 | Gdk.EventMask.BUTTON_PRESS_MASK
163 | Gdk.EventMask.BUTTON_RELEASE_MASK);
166 public unowned Gtk.TreeModel get_model() { return model; }
167 public void set_model (Gtk.TreeModel? model)
169 if (this.model != null)
171 this.model.row_changed -= on_row_changed;
172 this.model.row_deleted -= on_row_deleted;
173 this.model.row_has_child_toggled -= on_row_has_child_toggled;
174 this.model.row_inserted -= on_row_inserted;
175 this.model.rows_reordered -= on_rows_reordered;
178 this.model.row_changed += on_row_changed;
179 this.model.row_deleted += on_row_deleted;
180 this.model.row_has_child_toggled += on_row_has_child_toggled;
181 this.model.row_inserted += on_row_inserted;
182 this.model.rows_reordered += on_rows_reordered;
183 base_layout_needed = true;
187 private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
188 private void on_row_deleted(Gtk.TreePath path) { base_layout_needed = true; }
189 private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
190 private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
191 private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { base_layout_needed = true; }
193 /* Mouse button got pressed over widget */
195 public override bool button_press_event(Gdk.EventButton event)
197 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
202 /* Mouse button got released */
203 public override bool button_release_event(Gdk.EventButton event)
205 if (model == null) return false;
206 //stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
208 // Emit row_activated if applicable
209 int x = (int)event.x;
210 if (x < allocation.width / 3)
213 if (model.iter_nth_child(out iter, null, label_cache[cur_el].index))
215 Gtk.TreePath path = model.get_path(iter);
218 } else if (cur_pfx != null && x > allocation.width * 2 / 3) {
219 //stderr.printf("Mouse released on prefix %s\n", cur_pfx.prefix);
220 prefix_filter = cur_pfx.prefix;
225 /* Mouse pointer moved over widget */
226 public override bool motion_notify_event(Gdk.EventMotion event)
228 int old_cur_el = cur_el;
229 int x = (int)event.x;
230 int y = (int)event.y;
234 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;
236 if (!focus_locked || y < focus_area_start+focus_info[0].y || y >= focus_area_start+focus_info[focus_end - focus_first].y)
237 cur_el = y * (label_count+1) / allocation.height;
239 for (int idx = 0; idx < focus_info.length; ++idx)
240 if (y - focus_area_start < focus_info[idx].y + renderer_sizes[focus_info[idx].renderer])
242 cur_el = idx + focus_first;
246 if (prefixes != null)
248 cur_pfx = prefixes.data;
249 for (weak List<PrefixInfo> i = prefixes; i != null && y > i.data.layout_pos; i = i.next)
255 if (!focus_locked && old_cur_el != cur_el)
256 focus_layout_needed = true;
258 cur_el = y / max_renderer_size;
259 if (cur_el >= label_count)
260 cur_el = label_count - 1;
263 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
264 if (old_cur_el != cur_el)
272 public override bool configure_event (Gdk.EventConfigure event)
274 base_layout_needed = true;
279 /* Widget is asked to draw itself */
280 public override bool expose_event (Gdk.EventExpose event)
284 window.draw_drawable(
285 get_style().fg_gc[Gtk.StateType.NORMAL],
287 event.area.x, event.area.y,
288 event.area.x, event.area.y,
289 event.area.width, event.area.height);
294 public override void style_set(Gtk.Style? previous_style)
296 base_layout_needed = true;
298 public override void direction_changed(Gtk.TextDirection previous_direction)
300 base_layout_needed = true;
303 protected Gtk.CellRendererText make_cell_renderer()
305 var res = new Gtk.CellRendererText();
306 res.font_desc = get_style().font_desc;
311 protected void base_layout()
313 // Rebuild label cache
315 prefixes = new List<PrefixInfo>();
322 if (!model.get_iter_first(out iter))
329 int count = model.iter_n_children(null);
330 label_cache = new LabelInfo[count];
334 PrefixInfo pi = null;
335 int min_prefix_size = _prefix_filter == null ? 1 : (int)_prefix_filter.size() + 1;
338 model.get(iter, _label_column, out val, -1);
340 // Apply prefix filter
341 if (_prefix_filter == null || val.has_prefix(_prefix_filter))
343 label_cache[lc_idx].label = val;
344 label_cache[lc_idx].index = model_idx;
345 if (pi == null || !pi.refine(val, min_prefix_size))
347 if (pi != null) prefixes.append(pi);
348 pi = new PrefixInfo(val, lc_idx);
353 } while (model.iter_next(ref iter));
355 label_count = lc_idx;
358 for (weak List<PrefixInfo> i = prefixes; i != null; i = i.next)
359 i.data.layout_pos = i.data.pos * allocation.height / (label_count+1);
361 background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
362 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
364 // Recreate the renderers
365 renderers[renderers.length-1] = make_cell_renderer();
366 renderers[renderers.length-1].set_fixed_height_from_font(1);
367 renderers[renderers.length-1].get_size(this, null, null, null, null, out max_renderer_size);
368 renderer_sizes[renderers.length-1] = max_renderer_size;
370 is_fisheye = label_count * max_renderer_size > allocation.height;
374 renderers[0] = make_cell_renderer();
375 renderers[0].size = Pango.SCALE;
376 renderers[0].set_fixed_height_from_font(1);
377 int min_renderer_size;
378 renderers[0].get_size(this, null, null, null, null, out min_renderer_size);
379 renderer_sizes[0] = min_renderer_size;
381 focus_step_size = 0; // Size of the diminishing area
382 for (int i = 1; i < renderers.length-1; ++i)
384 renderers[i] = make_cell_renderer();
385 renderers[i].scale = (double)i / renderers.length;
386 renderers[i].set_fixed_height_from_font(1);
388 renderers[i].get_size(this, null, null, null, null, out size);
389 renderer_sizes[i] = size;
390 focus_step_size += size;
395 draw_background(background);
397 base_layout_needed = false;
398 focus_layout_needed = true;
401 protected int el_renderer(int idx)
403 int fs2 = _focus_size/2;
406 renderer_idx = idx - (cur_el-fs2-steps);
408 renderer_idx = (cur_el+fs2+steps) - idx;
409 if (renderer_idx < 0)
411 if (renderer_idx >= renderer_sizes.length)
412 return renderer_sizes.length-1;
416 protected void focus_layout()
418 if (!is_fisheye || label_count == 0)
422 focus_layout_needed = false;
426 focus_first = cur_el - _focus_size/2 - steps;
427 if (focus_first < 0) focus_first = 0;
428 if (focus_first + focus_info.length > label_count)
429 focus_first = label_count - focus_info.length;
433 int focus_area_pre = 0;
434 while (cur_pos < allocation.height && cur_idx < focus_info.length)
436 if (focus_first + cur_idx == cur_el)
437 focus_area_pre = cur_pos;
438 focus_info[cur_idx].y = cur_pos;
439 focus_info[cur_idx].renderer = el_renderer(focus_first + cur_idx);
440 // stderr.printf("LAYOUT %d+[0-%d-%d] item %d/%d: pos %d rend %d rsz %d\n",
441 // focus_first, cur_idx, focus_info.length, focus_first + cur_idx, label_count,
442 // cur_pos, focus_info[cur_idx].renderer, renderer_sizes[focus_info[cur_idx].renderer]);
443 cur_pos += renderer_sizes[focus_info[cur_idx].renderer];
447 focus_info[cur_idx].y = cur_pos;
448 focus_info[cur_idx].renderer = 0;
449 focus_end = focus_first + cur_idx;
451 int anchor_y = cur_el * allocation.height / (label_count+1);
452 int focus_area_size = cur_pos;
454 focus_area_start = anchor_y - focus_area_pre;
455 if (focus_area_start < 0) focus_area_start = 0;
456 if (focus_area_start + focus_area_size > allocation.height)
457 focus_area_start = allocation.height - focus_area_size;
459 // stderr.printf("FA [0 [%d+%d=%d] %d]\n", focus_area_start, focus_area_size, focus_area_start+focus_area_size, allocation.height);
461 focus_layout_needed = false;
464 protected void draw_background(Gdk.Drawable drawable)
466 Gtk.Style style = get_style();
469 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
474 // Focus movement area
475 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
476 allocation.width/3, 0, allocation.width/3, allocation.height);
478 for (int y = 0; y < allocation.height/2 - 30; y += 30)
480 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
481 null, this, null, Gtk.ArrowType.UP, false,
482 allocation.width/3, y, allocation.width/3, 10);
483 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
484 null, this, null, Gtk.ArrowType.DOWN, false,
485 allocation.width/3, allocation.height-y-30, allocation.width/3, 10);
489 Gdk.Rectangle expose_area = Gdk.Rectangle();
490 expose_area.x = expose_area.y = 0;
491 expose_area.width = allocation.width;
492 expose_area.height = allocation.height;
494 Gdk.Rectangle cell_area = Gdk.Rectangle();
496 cell_area.width = allocation.width;
497 cell_area.height = renderer_sizes[0];
498 if (label_count * cell_area.height >= allocation.height)
500 for (int y = 0; y < allocation.height; y += cell_area.height)
502 int idx = y * label_count / allocation.height;
504 renderers[0].text = label_cache[idx].label;
505 renderers[0].render((Gdk.Window*)drawable, this,
512 int count = int.min(allocation.height/(2*cell_area.height), label_count);
513 for (int idx = 0; idx < count; ++idx)
515 cell_area.y = idx * cell_area.height;
516 renderers[0].text = label_cache[idx].label;
517 renderers[0].render((Gdk.Window*)drawable, this,
523 cell_area.y = allocation.height-cell_area.y;
524 renderers[0].text = label_cache[label_count-idx].label;
525 renderers[0].render((Gdk.Window*)drawable, this,
533 // Draw prefix labels
534 prefix_layout = create_pango_layout(null);
536 prefix_layout.get_extents(null, out lr);
537 int label_height = lr.height / Pango.SCALE;
538 prefix_layout.set_alignment(Pango.Alignment.RIGHT);
539 prefix_layout.set_width(allocation.width*Pango.SCALE/3);
540 prefix_layout.set_height(0);
541 prefix_layout.set_ellipsize(Pango.EllipsizeMode.END);
543 int last_covered = 0;
545 for (weak List<PrefixInfo> i = prefixes; i != null; i = i.next)
547 if (i.data.layout_pos < last_covered)
548 i.data.layout_pos = last_covered;
549 prefix_layout.set_text(i.data.prefix, (int)i.data.pfx_len);
550 Gtk.paint_layout(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, true, null, this, null, allocation.width*2/3, i.data.layout_pos, prefix_layout);
551 last_covered = i.data.layout_pos + label_height;
555 protected void draw()
557 if (base_layout_needed)
559 if (focus_layout_needed)
562 var drawable = backing_store;
563 Gtk.Style style = get_style();
564 Gdk.Rectangle expose_area = Gdk.Rectangle();
565 expose_area.x = expose_area.y = 0;
566 expose_area.width = allocation.width;
567 expose_area.height = allocation.height;
570 drawable.draw_drawable(
571 get_style().fg_gc[Gtk.StateType.NORMAL],
574 allocation.width, allocation.height);
578 // Paint current prefix
581 prefix_layout.set_text(cur_pfx.prefix, (int)cur_pfx.pfx_len);
582 Gtk.paint_layout(style, (Gdk.Window*)drawable, Gtk.StateType.NORMAL, true, null, this, null, allocation.width*2/3, cur_pfx.layout_pos, prefix_layout);
586 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
587 0, focus_area_start + focus_info[0].y, allocation.width/3, focus_info[focus_end - focus_first].y);
589 // Paint items around focus
590 Gdk.Rectangle cell_area = Gdk.Rectangle();
592 cell_area.width = expose_area.width;
593 for (int idx = 0; idx < focus_info.length; ++idx)
595 var renderer = renderers[focus_info[idx].renderer];
596 cell_area.y = focus_area_start + focus_info[idx].y;
597 cell_area.height = renderer_sizes[focus_info[idx].renderer];
599 Gtk.CellRendererState rflags = 0;
600 if (idx + focus_first == cur_el)
602 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
603 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
604 cell_area.x, cell_area.y, allocation.width, cell_area.height);
607 renderer.text = label_cache[idx + focus_first].label;
608 renderer.render((Gdk.Window*)drawable, this,
614 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
617 // Paint all items sequentially
618 var renderer = renderers[renderers.length-1];
619 Gdk.Rectangle cell_area = Gdk.Rectangle();
621 cell_area.width = allocation.width;
622 cell_area.height = max_renderer_size;
623 for (int idx = 0; idx < label_count; ++idx)
625 cell_area.y = idx * cell_area.height;
627 Gtk.CellRendererState rflags = 0;
630 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
631 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
632 cell_area.x, cell_area.y, cell_area.width, cell_area.height);
635 renderer.text = label_cache[idx].label;
636 renderer.render((Gdk.Window*)drawable, this,
646 * A relevant annotation from Prefuse:
648 * For more details on this form of transformation, see Manojit Sarkar and
649 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
650 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
651 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
652 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
654 * See also http://www.cs.umd.edu/hcil/fisheyemenu/