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