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)
52 for (len = 0; prefix[len] != 0 && s[len] != 0 && s[len] == prefix[len]; ++len)
54 if (len == 0) return false;
57 prefix = prefix.substring(0, (int)len);
64 public class FisheyeListView : Gtk.DrawingArea
66 protected Gtk.TreeModel model;
67 protected Gdk.Pixmap background;
68 protected Gdk.Pixmap backing_store;
70 // Renderers used at different sizes
71 protected const int steps = 5;
72 protected Gtk.CellRendererText[] renderers;
73 protected int[] renderer_sizes;
74 protected int max_renderer_size;
76 // Labels to show, extracted from the model
77 protected string[] label_cache;
78 protected bool base_layout_needed;
81 protected List<PrefixInfo> prefixes;
82 protected Pango.Layout prefix_layout;
85 protected weak PrefixInfo cur_pfx;
88 protected int focus_first;
89 protected int focus_end;
90 protected FocusInfo[] focus_info;
91 protected bool focus_locked;
92 protected bool focus_layout_needed;
93 protected bool is_fisheye;
94 protected int focus_step_size;
95 protected int focus_area_start;
97 // Number of elements in full focus
98 protected int _focus_size;
99 public int focus_size {
100 get { return _focus_size; }
103 focus_info = new FocusInfo[_focus_size+2*steps+1];
104 focus_layout_needed = true;
109 protected int _label_column;
110 public int label_column {
111 get { return _label_column; }
113 _label_column = value;
114 base_layout_needed = true;
119 //public virtual signal void cursor_changed ();
120 public signal void row_activated(Gtk.TreePath path);
122 public FisheyeListView()
125 backing_store = null;
130 renderers = new Gtk.CellRendererText[steps];
131 renderer_sizes = new int[steps];
132 prefix_layout = null;
134 // Defaults for properties
140 focus_locked = false;
142 add_events(Gdk.EventMask.POINTER_MOTION_MASK
143 | Gdk.EventMask.BUTTON_PRESS_MASK
144 | Gdk.EventMask.BUTTON_RELEASE_MASK);
147 public unowned Gtk.TreeModel get_model() { return model; }
148 public void set_model (Gtk.TreeModel? model)
150 if (this.model != null)
152 this.model.row_changed -= on_row_changed;
153 this.model.row_deleted -= on_row_deleted;
154 this.model.row_has_child_toggled -= on_row_has_child_toggled;
155 this.model.row_inserted -= on_row_inserted;
156 this.model.rows_reordered -= on_rows_reordered;
159 this.model.row_changed += on_row_changed;
160 this.model.row_deleted += on_row_deleted;
161 this.model.row_has_child_toggled += on_row_has_child_toggled;
162 this.model.row_inserted += on_row_inserted;
163 this.model.rows_reordered += on_rows_reordered;
164 base_layout_needed = true;
168 private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
169 private void on_row_deleted(Gtk.TreePath path) { base_layout_needed = true; }
170 private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
171 private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
172 private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { base_layout_needed = true; }
174 /* Mouse button got pressed over widget */
176 public override bool button_press_event(Gdk.EventButton event)
178 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
183 /* Mouse button got released */
184 public override bool button_release_event(Gdk.EventButton event)
186 if (model == null) return false;
187 //stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
189 // Emit row_activated if applicable
190 int x = (int)event.x;
191 if (x < allocation.width / 3)
194 if (model.iter_nth_child(out iter, null, cur_el))
196 Gtk.TreePath path = model.get_path(iter);
199 } else if (cur_pfx != null && x > allocation.width * 2 / 3) {
200 stderr.printf("Mouse released on prefix %s\n", cur_pfx.prefix);
205 /* Mouse pointer moved over widget */
206 public override bool motion_notify_event(Gdk.EventMotion event)
208 int old_cur_el = cur_el;
209 int x = (int)event.x;
210 int y = (int)event.y;
214 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;
216 if (!focus_locked || y < focus_area_start+focus_info[0].y || y >= focus_area_start+focus_info[focus_end - focus_first].y)
217 cur_el = y * (label_cache.length+1) / allocation.height;
219 for (int idx = 0; idx < focus_info.length; ++idx)
220 if (y - focus_area_start < focus_info[idx].y + renderer_sizes[focus_info[idx].renderer])
222 cur_el = idx + focus_first;
226 if (prefixes != null)
228 cur_pfx = prefixes.data;
229 for (weak List<PrefixInfo> i = prefixes; i != null && y > i.data.layout_pos; i = i.next)
235 if (!focus_locked && old_cur_el != cur_el)
236 focus_layout_needed = true;
238 cur_el = y / max_renderer_size;
239 if (cur_el >= label_cache.length)
240 cur_el = label_cache.length - 1;
243 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
244 if (old_cur_el != cur_el)
252 public override bool configure_event (Gdk.EventConfigure event)
254 base_layout_needed = true;
259 /* Widget is asked to draw itself */
260 public override bool expose_event (Gdk.EventExpose event)
264 window.draw_drawable(
265 get_style().fg_gc[Gtk.StateType.NORMAL],
267 event.area.x, event.area.y,
268 event.area.x, event.area.y,
269 event.area.width, event.area.height);
274 public override void style_set(Gtk.Style? previous_style)
276 base_layout_needed = true;
278 public override void direction_changed(Gtk.TextDirection previous_direction)
280 base_layout_needed = true;
283 protected Gtk.CellRendererText make_cell_renderer()
285 var res = new Gtk.CellRendererText();
286 res.font_desc = get_style().font_desc;
291 protected void base_layout()
293 // Rebuild label cache
294 prefixes = new List<PrefixInfo>();
297 label_cache = new string[0];
300 if (!model.get_iter_first(out iter))
302 label_cache = new string[0];
306 int count = model.iter_n_children(null);
307 label_cache = new string[count];
310 PrefixInfo pi = null;
313 model.get(iter, _label_column, out val, -1);
314 label_cache[i] = val;
315 if (pi == null || !pi.refine(val))
317 if (pi != null) prefixes.append(pi);
318 pi = new PrefixInfo(val, i);
321 } while (model.iter_next(ref iter));
325 for (weak List<PrefixInfo> i = prefixes; i != null; i = i.next)
326 i.data.layout_pos = i.data.pos * allocation.height / (label_cache.length+1);
328 background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
329 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
331 // Recreate the renderers
332 renderers[renderers.length-1] = make_cell_renderer();
333 renderers[renderers.length-1].set_fixed_height_from_font(1);
334 renderers[renderers.length-1].get_size(this, null, null, null, null, out max_renderer_size);
335 renderer_sizes[renderers.length-1] = max_renderer_size;
337 is_fisheye = label_cache.length * max_renderer_size > allocation.height;
341 renderers[0] = make_cell_renderer();
342 renderers[0].size = Pango.SCALE;
343 renderers[0].set_fixed_height_from_font(1);
344 int min_renderer_size;
345 renderers[0].get_size(this, null, null, null, null, out min_renderer_size);
346 renderer_sizes[0] = min_renderer_size;
348 focus_step_size = 0; // Size of the diminishing area
349 for (int i = 1; i < renderers.length-1; ++i)
351 renderers[i] = make_cell_renderer();
352 renderers[i].scale = (double)i / renderers.length;
353 renderers[i].set_fixed_height_from_font(1);
355 renderers[i].get_size(this, null, null, null, null, out size);
356 renderer_sizes[i] = size;
357 focus_step_size += size;
362 draw_background(background);
364 base_layout_needed = false;
365 focus_layout_needed = true;
368 protected int el_renderer(int idx)
370 int fs2 = _focus_size/2;
373 renderer_idx = idx - (cur_el-fs2-steps);
375 renderer_idx = (cur_el+fs2+steps) - idx;
376 if (renderer_idx < 0)
378 if (renderer_idx >= renderer_sizes.length)
379 return renderer_sizes.length-1;
383 protected void focus_layout()
385 if (!is_fisheye || label_cache.length == 0)
389 focus_layout_needed = false;
393 focus_first = cur_el - _focus_size/2 - steps;
394 if (focus_first < 0) focus_first = 0;
395 if (focus_first + focus_info.length > label_cache.length)
396 focus_first = label_cache.length - focus_info.length;
400 int focus_area_pre = 0;
401 while (cur_pos < allocation.height && cur_idx < focus_info.length)
403 if (focus_first + cur_idx == cur_el)
404 focus_area_pre = cur_pos;
405 focus_info[cur_idx].y = cur_pos;
406 focus_info[cur_idx].renderer = el_renderer(focus_first + cur_idx);
407 // stderr.printf("LAYOUT %d+[0-%d-%d] item %d/%d: pos %d rend %d rsz %d\n",
408 // focus_first, cur_idx, focus_info.length, focus_first + cur_idx, label_cache.length,
409 // cur_pos, focus_info[cur_idx].renderer, renderer_sizes[focus_info[cur_idx].renderer]);
410 cur_pos += renderer_sizes[focus_info[cur_idx].renderer];
414 focus_info[cur_idx].y = cur_pos;
415 focus_info[cur_idx].renderer = 0;
416 focus_end = focus_first + cur_idx;
418 int anchor_y = cur_el * allocation.height / (label_cache.length+1);
419 int focus_area_size = cur_pos;
421 focus_area_start = anchor_y - focus_area_pre;
422 if (focus_area_start < 0) focus_area_start = 0;
423 if (focus_area_start + focus_area_size > allocation.height)
424 focus_area_start = allocation.height - focus_area_size;
426 // stderr.printf("FA [0 [%d+%d=%d] %d]\n", focus_area_start, focus_area_size, focus_area_start+focus_area_size, allocation.height);
428 focus_layout_needed = false;
431 protected void draw_background(Gdk.Drawable drawable)
433 Gtk.Style style = get_style();
436 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
441 // Focus movement area
442 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
443 allocation.width/3, 0, allocation.width/3, allocation.height);
445 for (int y = 0; y < allocation.height/2 - 30; y += 30)
447 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
448 null, this, null, Gtk.ArrowType.UP, false,
449 allocation.width/3, y, allocation.width/3, 10);
450 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
451 null, this, null, Gtk.ArrowType.DOWN, false,
452 allocation.width/3, allocation.height-y-30, allocation.width/3, 10);
456 Gdk.Rectangle expose_area = Gdk.Rectangle();
457 expose_area.x = expose_area.y = 0;
458 expose_area.width = allocation.width;
459 expose_area.height = allocation.height;
461 Gdk.Rectangle cell_area = Gdk.Rectangle();
463 cell_area.width = allocation.width;
464 cell_area.height = renderer_sizes[0];
465 if (label_cache.length * cell_area.height >= allocation.height)
467 for (int y = 0; y < allocation.height; y += cell_area.height)
469 int idx = y * label_cache.length / allocation.height;
471 renderers[0].text = label_cache[idx];
472 renderers[0].render((Gdk.Window*)drawable, this,
479 int count = int.min(allocation.height/(2*cell_area.height), label_cache.length);
480 for (int idx = 0; idx < count; ++idx)
482 cell_area.y = idx * cell_area.height;
483 renderers[0].text = label_cache[idx];
484 renderers[0].render((Gdk.Window*)drawable, this,
490 cell_area.y = allocation.height-cell_area.y;
491 renderers[0].text = label_cache[label_cache.length-idx];
492 renderers[0].render((Gdk.Window*)drawable, this,
500 // Draw prefix labels
501 prefix_layout = create_pango_layout(null);
503 prefix_layout.get_extents(null, out lr);
504 int label_height = lr.height / Pango.SCALE;
505 prefix_layout.set_alignment(Pango.Alignment.RIGHT);
506 prefix_layout.set_width(allocation.width*Pango.SCALE/3);
507 prefix_layout.set_height(0);
508 prefix_layout.set_ellipsize(Pango.EllipsizeMode.END);
510 int last_covered = 0;
512 for (weak List<PrefixInfo> i = prefixes; i != null; i = i.next)
514 if (i.data.layout_pos < last_covered)
515 i.data.layout_pos = last_covered;
516 prefix_layout.set_text(i.data.prefix, (int)i.data.pfx_len);
517 Gtk.paint_layout(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, true, null, this, null, allocation.width*2/3, i.data.layout_pos, prefix_layout);
518 last_covered = i.data.layout_pos + label_height;
522 protected void draw()
524 if (base_layout_needed)
526 if (focus_layout_needed)
529 var drawable = backing_store;
530 Gtk.Style style = get_style();
531 Gdk.Rectangle expose_area = Gdk.Rectangle();
532 expose_area.x = expose_area.y = 0;
533 expose_area.width = allocation.width;
534 expose_area.height = allocation.height;
537 drawable.draw_drawable(
538 get_style().fg_gc[Gtk.StateType.NORMAL],
541 allocation.width, allocation.height);
545 // Paint current prefix
548 prefix_layout.set_text(cur_pfx.prefix, (int)cur_pfx.pfx_len);
549 Gtk.paint_layout(style, (Gdk.Window*)drawable, Gtk.StateType.NORMAL, true, null, this, null, allocation.width*2/3, cur_pfx.layout_pos, prefix_layout);
553 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
554 0, focus_area_start + focus_info[0].y, allocation.width/3, focus_info[focus_end - focus_first].y);
556 // Paint items around focus
557 Gdk.Rectangle cell_area = Gdk.Rectangle();
559 cell_area.width = expose_area.width;
560 for (int idx = 0; idx < focus_info.length; ++idx)
562 var renderer = renderers[focus_info[idx].renderer];
563 cell_area.y = focus_area_start + focus_info[idx].y;
564 cell_area.height = renderer_sizes[focus_info[idx].renderer];
566 Gtk.CellRendererState rflags = 0;
567 if (idx + focus_first == cur_el)
569 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
570 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
571 cell_area.x, cell_area.y, allocation.width, cell_area.height);
574 renderer.text = label_cache[idx + focus_first];
575 renderer.render((Gdk.Window*)drawable, this,
581 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
584 // Paint all items sequentially
585 var renderer = renderers[renderers.length-1];
586 Gdk.Rectangle cell_area = Gdk.Rectangle();
588 cell_area.width = allocation.width;
589 cell_area.height = max_renderer_size;
590 for (int idx = 0; idx < label_cache.length; ++idx)
592 cell_area.y = idx * cell_area.height;
594 Gtk.CellRendererState rflags = 0;
597 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
598 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
599 cell_area.x, cell_area.y, cell_area.width, cell_area.height);
602 renderer.text = label_cache[idx];
603 renderer.render((Gdk.Window*)drawable, this,
613 * A relevant annotation from Prefuse:
615 * For more details on this form of transformation, see Manojit Sarkar and
616 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
617 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
618 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
619 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
621 * See also http://www.cs.umd.edu/hcil/fisheyemenu/