Light current prefix
[gregoa/zavai.git] / gtkfisheyelist / gtkfisheyelistview.vala
1 /*
2  * FisheyeListView - View a TreeModel as a list that does not need scrolling
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22
23 protected struct FocusInfo
24 {
25         int y;
26         int renderer;
27 }
28
29 protected class PrefixInfo
30 {
31         public string prefix;
32         public size_t pfx_len;
33         public int pos;
34         public int layout_pos;
35
36         public PrefixInfo(string prefix, int pos)
37         {
38                 this.prefix = prefix;
39                 this.pfx_len = prefix.size();
40                 this.pos = pos;
41                 this.layout_pos = 0;
42         }
43
44         /**
45          * If s and prefix share a prefix, set prefix to the common prefix, and
46          * return true.
47          * Else, return false.
48          */
49         public bool refine(string s)
50         {
51                 size_t len;
52                 for (len = 0; prefix[len] != 0 && s[len] != 0 && s[len] == prefix[len]; ++len)
53                         ;
54                 if (len == 0) return false;
55                 if (len != pfx_len)
56                 {
57                         prefix = prefix.substring(0, (int)len);
58                         pfx_len = len;
59                 }
60                 return true;
61         }
62 }
63
64 public class FisheyeListView : Gtk.DrawingArea
65 {
66         protected Gtk.TreeModel model;
67         protected Gdk.Pixmap background;
68         protected Gdk.Pixmap backing_store;
69
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;
75
76         // Labels to show, extracted from the model
77         protected string[] label_cache;
78         protected bool base_layout_needed;
79
80         // Prefix information
81         protected List<PrefixInfo> prefixes;
82         protected Pango.Layout prefix_layout;
83
84         protected int cur_el;
85         protected weak PrefixInfo cur_pfx;
86
87         // Layout information
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;
96
97         // Number of elements in full focus
98         protected int _focus_size;
99         public int focus_size {
100                 get { return _focus_size; }
101                 set {
102                         _focus_size = value;
103                         focus_info = new FocusInfo[_focus_size+2*steps+1];
104                         focus_layout_needed = true;
105                         queue_draw();
106                 }
107         }
108
109         protected int _label_column;
110         public int label_column {
111                 get { return _label_column; }
112                 set {
113                         _label_column = value;
114                         base_layout_needed = true;
115                         queue_draw();
116                 }
117         }
118
119         //public virtual signal void cursor_changed ();
120         public signal void row_activated(Gtk.TreePath path);
121
122         public FisheyeListView()
123         {
124                 model = null;
125                 backing_store = null;
126
127                 label_cache = null;
128                 prefixes = null;
129
130                 renderers = new Gtk.CellRendererText[steps];
131                 renderer_sizes = new int[steps];
132                 prefix_layout = null;
133
134                 // Defaults for properties
135                 focus_size = 10;
136                 label_column = 0;
137
138                 cur_el = 0;
139                 cur_pfx = null;
140                 focus_locked = false;
141
142                 add_events(Gdk.EventMask.POINTER_MOTION_MASK
143                          | Gdk.EventMask.BUTTON_PRESS_MASK
144                          | Gdk.EventMask.BUTTON_RELEASE_MASK);
145         }
146
147         public unowned Gtk.TreeModel get_model() { return model; }
148         public void set_model (Gtk.TreeModel? model)
149         {
150                 if (this.model != null)
151                 {
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;
157                 }
158                 this.model = model;
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;
165                 queue_draw();
166         }
167
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; }
173
174         /* Mouse button got pressed over widget */
175         /*
176         public override bool button_press_event(Gdk.EventButton event)
177         {
178                 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
179                 return false;
180         }
181         */
182
183         /* Mouse button got released */
184         public override bool button_release_event(Gdk.EventButton event)
185         {
186                 //stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
187
188                 // Emit row_activated if applicable
189                 if (model != null)
190                 {
191                         Gtk.TreeIter iter;
192                         if (model.iter_nth_child(out iter, null, cur_el))
193                         {
194                                 Gtk.TreePath path = model.get_path(iter);
195                                 row_activated(path);
196                         }
197                 }
198                 return false;
199         }
200
201         /* Mouse pointer moved over widget */
202         public override bool motion_notify_event(Gdk.EventMotion event)
203         {
204                 int old_cur_el = cur_el;
205                 int x = (int)event.x;
206                 int y = (int)event.y;
207
208                 if (is_fisheye)
209                 {
210                         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;
211
212                         if (!focus_locked || y < focus_area_start+focus_info[0].y || y >= focus_area_start+focus_info[focus_end - focus_first].y)
213                                 cur_el = y * (label_cache.length+1) / allocation.height;
214                         else
215                                 for (int idx = 0; idx < focus_info.length; ++idx)
216                                         if (y - focus_area_start < focus_info[idx].y + renderer_sizes[focus_info[idx].renderer])
217                                         {
218                                                 cur_el = idx + focus_first;
219                                                 break;
220                                         }
221
222                         if (prefixes != null)
223                         {
224                                 cur_pfx = prefixes.data;
225                                 for (weak List<PrefixInfo> i = prefixes; i != null && y > i.data.layout_pos; i = i.next)
226                                         cur_pfx = i.data;
227                         } else {
228                                 cur_pfx = null;
229                         }
230
231                         if (!focus_locked && old_cur_el != cur_el)
232                                 focus_layout_needed = true;
233                 } else {
234                         cur_el = y / max_renderer_size;
235                         if (cur_el >= label_cache.length)
236                                 cur_el = label_cache.length - 1;
237                 }
238
239                 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
240                 if (old_cur_el != cur_el)
241                 {
242                         queue_draw();
243                         old_cur_el = cur_el;
244                 }
245                 return false;
246         }
247
248         public override bool configure_event (Gdk.EventConfigure event)
249         {
250                 base_layout_needed = true;
251                 queue_draw();
252                 return false;
253         }
254
255         /* Widget is asked to draw itself */
256         public override bool expose_event (Gdk.EventExpose event)
257         {
258                 draw();
259
260                 window.draw_drawable(
261                         get_style().fg_gc[Gtk.StateType.NORMAL],
262                         backing_store,
263                         event.area.x, event.area.y,
264                         event.area.x, event.area.y,
265                         event.area.width, event.area.height);
266
267                 return false;
268         }
269
270         public override void style_set(Gtk.Style? previous_style)
271         {
272                 base_layout_needed = true;
273         }
274         public override void direction_changed(Gtk.TextDirection previous_direction)
275         {
276                 base_layout_needed = true;
277         }
278
279         protected Gtk.CellRendererText make_cell_renderer()
280         {
281                 var res = new Gtk.CellRendererText();
282                 res.font_desc = get_style().font_desc;
283                 res.ypad = 0;
284                 return res;
285         }
286
287         protected void base_layout()
288         {
289                 // Rebuild label cache
290                 prefixes = new List<PrefixInfo>();
291                 if (model == null)
292                 {
293                         label_cache = new string[0];
294                 } else {
295                         Gtk.TreeIter iter;
296                         if (!model.get_iter_first(out iter))
297                         {
298                                 label_cache = new string[0];
299                         }
300                         else
301                         {
302                                 int count = model.iter_n_children(null);
303                                 label_cache = new string[count];
304
305                                 int i = 0;
306                                 PrefixInfo pi = null;
307                                 do {
308                                         string val;
309                                         model.get(iter, _label_column, out val, -1);
310                                         label_cache[i] = val;
311                                         if (pi == null || !pi.refine(val))
312                                         {
313                                                 if (pi != null) prefixes.append(pi);
314                                                 pi = new PrefixInfo(val, i);
315                                         }
316                                         ++i;
317                                 } while (model.iter_next(ref iter));
318                                 prefixes.append(pi);
319                         }
320                 }
321                 for (weak List<PrefixInfo> i = prefixes; i != null; i = i.next)
322                         i.data.layout_pos = i.data.pos * allocation.height / (label_cache.length+1);
323
324                 background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
325                 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
326
327                 // Recreate the renderers
328                 renderers[renderers.length-1] = make_cell_renderer();
329                 renderers[renderers.length-1].set_fixed_height_from_font(1);
330                 renderers[renderers.length-1].get_size(this, null, null, null, null, out max_renderer_size);
331                 renderer_sizes[renderers.length-1] = max_renderer_size;
332
333                 is_fisheye = label_cache.length * max_renderer_size > allocation.height;
334
335                 if (is_fisheye)
336                 {
337                         renderers[0] = make_cell_renderer();
338                         renderers[0].size = Pango.SCALE;
339                         renderers[0].set_fixed_height_from_font(1);
340                         int min_renderer_size;
341                         renderers[0].get_size(this, null, null, null, null, out min_renderer_size);
342                         renderer_sizes[0] = min_renderer_size;
343
344                         focus_step_size = 0;    // Size of the diminishing area
345                         for (int i = 1; i < renderers.length-1; ++i)
346                         {
347                                 renderers[i] = make_cell_renderer();
348                                 renderers[i].scale = (double)i / renderers.length;
349                                 renderers[i].set_fixed_height_from_font(1);
350                                 int size;
351                                 renderers[i].get_size(this, null, null, null, null, out size);
352                                 renderer_sizes[i] = size;
353                                 focus_step_size += size;
354                         }
355                 }
356
357                 // Draw background
358                 draw_background(background);
359
360                 base_layout_needed = false;
361                 focus_layout_needed = true;
362         }
363
364         protected int el_renderer(int idx)
365         {
366                 int fs2 = _focus_size/2;
367                 int renderer_idx;
368                 if (idx < cur_el)
369                         renderer_idx = idx - (cur_el-fs2-steps);
370                 else
371                         renderer_idx = (cur_el+fs2+steps) - idx;
372                 if (renderer_idx < 0)
373                         return 0;
374                 if (renderer_idx >= renderer_sizes.length)
375                         return renderer_sizes.length-1;
376                 return renderer_idx;
377         }
378
379         protected void focus_layout()
380         {
381                 if (!is_fisheye || label_cache.length == 0)
382                 {
383                         focus_first = 0;
384                         focus_end = 0;
385                         focus_layout_needed = false;
386                         return;
387                 }
388
389                 focus_first = cur_el - _focus_size/2 - steps;
390                 if (focus_first < 0) focus_first = 0;
391                 if (focus_first + focus_info.length > label_cache.length)
392                         focus_first = label_cache.length - focus_info.length;
393
394                 int cur_pos = 0;
395                 int cur_idx = 0;
396                 int focus_area_pre = 0;
397                 while (cur_pos < allocation.height && cur_idx < focus_info.length)
398                 {
399                         if (focus_first + cur_idx == cur_el)
400                                 focus_area_pre = cur_pos;
401                         focus_info[cur_idx].y = cur_pos;
402                         focus_info[cur_idx].renderer = el_renderer(focus_first + cur_idx);
403 //                      stderr.printf("LAYOUT %d+[0-%d-%d] item %d/%d: pos %d rend %d rsz %d\n",
404 //                              focus_first, cur_idx, focus_info.length, focus_first + cur_idx, label_cache.length,
405 //                              cur_pos, focus_info[cur_idx].renderer, renderer_sizes[focus_info[cur_idx].renderer]);
406                         cur_pos += renderer_sizes[focus_info[cur_idx].renderer];
407                         ++cur_idx;
408                 }
409
410                 focus_info[cur_idx].y = cur_pos;
411                 focus_info[cur_idx].renderer = 0;
412                 focus_end = focus_first + cur_idx;
413
414                 int anchor_y = cur_el * allocation.height / (label_cache.length+1);
415                 int focus_area_size = cur_pos;
416
417                 focus_area_start = anchor_y - focus_area_pre;
418                 if (focus_area_start < 0) focus_area_start = 0;
419                 if (focus_area_start + focus_area_size > allocation.height)
420                         focus_area_start = allocation.height - focus_area_size;
421
422 //              stderr.printf("FA [0 [%d+%d=%d] %d]\n", focus_area_start, focus_area_size, focus_area_start+focus_area_size, allocation.height);
423
424                 focus_layout_needed = false;
425         }
426
427         protected void draw_background(Gdk.Drawable drawable)
428         {
429                 Gtk.Style style = get_style();
430
431                 // Background
432                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
433
434                 if (!is_fisheye)
435                         return;
436
437                 // Focus movement area
438                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
439                         allocation.width/3, 0, allocation.width/3, allocation.height);
440
441                 for (int y = 0; y < allocation.height/2 - 30; y += 30)
442                 {
443                         Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
444                                 null, this, null, Gtk.ArrowType.UP, false,
445                                 allocation.width/3, y, allocation.width/3, 10);
446                         Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
447                                 null, this, null, Gtk.ArrowType.DOWN, false,
448                                 allocation.width/3, allocation.height-y-30, allocation.width/3, 10);
449                 }
450
451                 // Draw tiny labels
452                 Gdk.Rectangle expose_area = Gdk.Rectangle();
453                 expose_area.x = expose_area.y = 0;
454                 expose_area.width = allocation.width;
455                 expose_area.height = allocation.height;
456
457                 Gdk.Rectangle cell_area = Gdk.Rectangle();
458                 cell_area.x = 0;
459                 cell_area.width = allocation.width;
460                 cell_area.height = renderer_sizes[0];
461                 if (label_cache.length * cell_area.height >= allocation.height)
462                 {
463                         for (int y = 0; y < allocation.height; y += cell_area.height)
464                         {
465                                 int idx = y * label_cache.length / allocation.height;
466                                 cell_area.y = y;
467                                 renderers[0].text = label_cache[idx];
468                                 renderers[0].render((Gdk.Window*)drawable, this, 
469                                                 cell_area,
470                                                 cell_area,
471                                                 expose_area,
472                                                 0);
473                         }
474                 } else {
475                         int count = int.min(allocation.height/(2*cell_area.height), label_cache.length);
476                         for (int idx = 0; idx < count; ++idx)
477                         {
478                                 cell_area.y = idx * cell_area.height;
479                                 renderers[0].text = label_cache[idx];
480                                 renderers[0].render((Gdk.Window*)drawable, this, 
481                                                 cell_area,
482                                                 cell_area,
483                                                 expose_area,
484                                                 0);
485
486                                 cell_area.y = allocation.height-cell_area.y;
487                                 renderers[0].text = label_cache[label_cache.length-idx];
488                                 renderers[0].render((Gdk.Window*)drawable, this, 
489                                                 cell_area,
490                                                 cell_area,
491                                                 expose_area,
492                                                 0);
493                         }
494                 }
495
496                 // Draw prefix labels
497                 prefix_layout = create_pango_layout(null);
498                 Pango.Rectangle lr;
499                 prefix_layout.get_extents(null, out lr);
500                 int label_height = lr.height / Pango.SCALE;
501                 prefix_layout.set_alignment(Pango.Alignment.RIGHT);
502                 prefix_layout.set_width(allocation.width*Pango.SCALE/3);
503                 prefix_layout.set_height(0);
504                 prefix_layout.set_ellipsize(Pango.EllipsizeMode.END);
505
506                 int last_covered = 0;
507
508                 for (weak List<PrefixInfo> i = prefixes; i != null; i = i.next)
509                 {
510                         if (i.data.layout_pos < last_covered)
511                                 i.data.layout_pos = last_covered;
512                         prefix_layout.set_text(i.data.prefix, (int)i.data.pfx_len);
513                         Gtk.paint_layout(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, true, null, this, null, allocation.width*2/3, i.data.layout_pos, prefix_layout);
514                         last_covered = i.data.layout_pos + label_height;
515                 }
516         }
517
518         protected void draw()
519         {
520                 if (base_layout_needed)
521                         base_layout();
522                 if (focus_layout_needed)
523                         focus_layout();
524
525                 var drawable = backing_store;
526                 Gtk.Style style = get_style();
527                 Gdk.Rectangle expose_area = Gdk.Rectangle();
528                 expose_area.x = expose_area.y = 0;
529                 expose_area.width = allocation.width;
530                 expose_area.height = allocation.height;
531
532                 // Background
533                 drawable.draw_drawable(
534                         get_style().fg_gc[Gtk.StateType.NORMAL],
535                         background,
536                         0, 0, 0, 0,
537                         allocation.width, allocation.height);
538
539                 if (is_fisheye)
540                 {
541                         // Paint current prefix
542                         if (cur_pfx != null)
543                         {
544                                 prefix_layout.set_text(cur_pfx.prefix, (int)cur_pfx.pfx_len);
545                                 Gtk.paint_layout(style, (Gdk.Window*)drawable, Gtk.StateType.NORMAL, true, null, this, null, allocation.width*2/3, cur_pfx.layout_pos, prefix_layout);
546                         }
547
548                         // Focus lock area
549                         drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
550                                 0, focus_area_start + focus_info[0].y, allocation.width/3, focus_info[focus_end - focus_first].y);
551
552                         // Paint items around focus
553                         Gdk.Rectangle cell_area = Gdk.Rectangle();
554                         cell_area.x = 0;
555                         cell_area.width = expose_area.width;
556                         for (int idx = 0; idx < focus_info.length; ++idx)
557                         {
558                                 var renderer = renderers[focus_info[idx].renderer];
559                                 cell_area.y = focus_area_start + focus_info[idx].y;
560                                 cell_area.height = renderer_sizes[focus_info[idx].renderer];
561
562                                 Gtk.CellRendererState rflags = 0;
563                                 if (idx + focus_first == cur_el)
564                                 {
565                                         rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
566                                         drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
567                                                         cell_area.x, cell_area.y, allocation.width, cell_area.height);
568                                 }
569                         
570                                 renderer.text = label_cache[idx + focus_first];
571                                 renderer.render((Gdk.Window*)drawable, this, 
572                                                 cell_area,
573                                                 cell_area,
574                                                 expose_area,
575                                                 rflags);
576
577                                 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
578                         }
579                 } else {
580                         // Paint all items sequentially
581                         var renderer = renderers[renderers.length-1];
582                         Gdk.Rectangle cell_area = Gdk.Rectangle();
583                         cell_area.x = 0;
584                         cell_area.width = allocation.width;
585                         cell_area.height = max_renderer_size;
586                         for (int idx = 0; idx < label_cache.length; ++idx)
587                         {
588                                 cell_area.y = idx * cell_area.height;
589
590                                 Gtk.CellRendererState rflags = 0;
591                                 if (idx == cur_el)
592                                 {
593                                         rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
594                                         drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
595                                                         cell_area.x, cell_area.y, cell_area.width, cell_area.height);
596                                 }
597
598                                 renderer.text = label_cache[idx];
599                                 renderer.render((Gdk.Window*)drawable, this, 
600                                                 cell_area,
601                                                 cell_area,
602                                                 expose_area,
603                                                 rflags);
604                         }
605                 }
606         }
607
608         /*
609          * A relevant annotation from Prefuse:
610          *
611          * For more details on this form of transformation, see Manojit Sarkar and 
612          * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
613          * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
614          * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
615          * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
616          *
617          * See also http://www.cs.umd.edu/hcil/fisheyemenu/
618          */
619 }