prefix_filter now works on everything
[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, int min_prefix_size=1)
50         {
51                 size_t len;
52                 for (len = 0; prefix[len] != 0 && s[len] != 0 && s[len] == prefix[len]; ++len)
53                         ;
54                 if (len < min_prefix_size) 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 protected struct LabelInfo
65 {
66         string label;
67         int index;
68 }
69
70 public class FisheyeListView : Gtk.DrawingArea
71 {
72         protected Gtk.TreeModel model;
73         protected Gdk.Pixmap background;
74         protected Gdk.Pixmap backing_store;
75
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;
81
82         // Labels to show, extracted from the model
83         protected LabelInfo[] label_cache;
84         protected int label_count;
85         protected bool base_layout_needed;
86
87         // Prefix information
88         protected List<PrefixInfo> prefixes;
89         protected Pango.Layout prefix_layout;
90
91         protected int cur_el;
92         protected weak PrefixInfo cur_pfx;
93
94         // Layout information
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;
103
104         // Number of elements in full focus
105         protected int _focus_size;
106         public int focus_size {
107                 get { return _focus_size; }
108                 set {
109                         _focus_size = value;
110                         focus_info = new FocusInfo[_focus_size+2*steps+1];
111                         focus_layout_needed = true;
112                         queue_draw();
113                 }
114         }
115
116         protected int _label_column;
117         public int label_column {
118                 get { return _label_column; }
119                 set {
120                         _label_column = value;
121                         base_layout_needed = true;
122                         queue_draw();
123                 }
124         }
125
126         protected string _prefix_filter;
127         public string prefix_filter {
128                 get { return _prefix_filter; }
129                 set {
130                         _prefix_filter = value;
131                         base_layout_needed = true;
132                         queue_draw();
133                 }
134         }
135
136         //public virtual signal void cursor_changed ();
137         public signal void row_activated(Gtk.TreePath path);
138
139         public FisheyeListView()
140         {
141                 model = null;
142                 backing_store = null;
143
144                 label_cache = null;
145                 label_count = 0;
146                 prefixes = null;
147                 prefix_filter = null;
148
149                 renderers = new Gtk.CellRendererText[steps];
150                 renderer_sizes = new int[steps];
151                 prefix_layout = null;
152
153                 // Defaults for properties
154                 focus_size = 10;
155                 label_column = 0;
156
157                 cur_el = 0;
158                 cur_pfx = null;
159                 focus_locked = false;
160
161                 add_events(Gdk.EventMask.POINTER_MOTION_MASK
162                          | Gdk.EventMask.BUTTON_PRESS_MASK
163                          | Gdk.EventMask.BUTTON_RELEASE_MASK);
164         }
165
166         public unowned Gtk.TreeModel get_model() { return model; }
167         public void set_model (Gtk.TreeModel? model)
168         {
169                 if (this.model != null)
170                 {
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;
176                 }
177                 this.model = model;
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;
184                 queue_draw();
185         }
186
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; }
192
193         /* Mouse button got pressed over widget */
194         /*
195         public override bool button_press_event(Gdk.EventButton event)
196         {
197                 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
198                 return false;
199         }
200         */
201
202         /* Mouse button got released */
203         public override bool button_release_event(Gdk.EventButton event)
204         {
205                 if (model == null) return false;
206                 //stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
207
208                 // Emit row_activated if applicable
209                 int x = (int)event.x;
210                 if (x < allocation.width / 3)
211                 {
212                         Gtk.TreeIter iter;
213                         if (model.iter_nth_child(out iter, null, label_cache[cur_el].index))
214                         {
215                                 Gtk.TreePath path = model.get_path(iter);
216                                 row_activated(path);
217                         }
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;
221                 }
222                 return false;
223         }
224
225         /* Mouse pointer moved over widget */
226         public override bool motion_notify_event(Gdk.EventMotion event)
227         {
228                 int old_cur_el = cur_el;
229                 int x = (int)event.x;
230                 int y = (int)event.y;
231
232                 if (is_fisheye)
233                 {
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;
235
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;
238                         else
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])
241                                         {
242                                                 cur_el = idx + focus_first;
243                                                 break;
244                                         }
245
246                         if (prefixes != null)
247                         {
248                                 cur_pfx = prefixes.data;
249                                 for (weak List<PrefixInfo> i = prefixes; i != null && y > i.data.layout_pos; i = i.next)
250                                         cur_pfx = i.data;
251                         } else {
252                                 cur_pfx = null;
253                         }
254
255                         if (!focus_locked && old_cur_el != cur_el)
256                                 focus_layout_needed = true;
257                 } else {
258                         cur_el = y / max_renderer_size;
259                         if (cur_el >= label_count)
260                                 cur_el = label_count - 1;
261                 }
262
263                 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
264                 if (old_cur_el != cur_el)
265                 {
266                         queue_draw();
267                         old_cur_el = cur_el;
268                 }
269                 return false;
270         }
271
272         public override bool configure_event (Gdk.EventConfigure event)
273         {
274                 base_layout_needed = true;
275                 queue_draw();
276                 return false;
277         }
278
279         /* Widget is asked to draw itself */
280         public override bool expose_event (Gdk.EventExpose event)
281         {
282                 draw();
283
284                 window.draw_drawable(
285                         get_style().fg_gc[Gtk.StateType.NORMAL],
286                         backing_store,
287                         event.area.x, event.area.y,
288                         event.area.x, event.area.y,
289                         event.area.width, event.area.height);
290
291                 return false;
292         }
293
294         public override void style_set(Gtk.Style? previous_style)
295         {
296                 base_layout_needed = true;
297         }
298         public override void direction_changed(Gtk.TextDirection previous_direction)
299         {
300                 base_layout_needed = true;
301         }
302
303         protected Gtk.CellRendererText make_cell_renderer()
304         {
305                 var res = new Gtk.CellRendererText();
306                 res.font_desc = get_style().font_desc;
307                 res.ypad = 0;
308                 return res;
309         }
310
311         protected void base_layout()
312         {
313                 // Rebuild label cache
314                 cur_pfx = null;
315                 prefixes = new List<PrefixInfo>();
316                 if (model == null)
317                 {
318                         label_cache = null;
319                         label_count = 0;
320                 } else {
321                         Gtk.TreeIter iter;
322                         if (!model.get_iter_first(out iter))
323                         {
324                                 label_cache = null;
325                                 label_count = 0;
326                         }
327                         else
328                         {
329                                 int count = model.iter_n_children(null);
330                                 label_cache = new LabelInfo[count];
331
332                                 int lc_idx = 0;
333                                 int model_idx = 0;
334                                 PrefixInfo pi = null;
335                                 int min_prefix_size = _prefix_filter == null ? 1 : (int)_prefix_filter.size() + 1;
336                                 do {
337                                         string val;
338                                         model.get(iter, _label_column, out val, -1);
339
340                                         // Apply prefix filter
341                                         if (_prefix_filter == null || val.has_prefix(_prefix_filter))
342                                         {
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))
346                                                 {
347                                                         if (pi != null) prefixes.append(pi);
348                                                         pi = new PrefixInfo(val, lc_idx);
349                                                 }
350                                                 ++lc_idx;
351                                         }
352                                         ++model_idx;
353                                 } while (model.iter_next(ref iter));
354                                 prefixes.append(pi);
355                                 label_count = lc_idx;
356                         }
357                 }
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);
360
361                 background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
362                 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
363
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;
369
370                 is_fisheye = label_count * max_renderer_size > allocation.height;
371
372                 if (is_fisheye)
373                 {
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;
380
381                         focus_step_size = 0;    // Size of the diminishing area
382                         for (int i = 1; i < renderers.length-1; ++i)
383                         {
384                                 renderers[i] = make_cell_renderer();
385                                 renderers[i].scale = (double)i / renderers.length;
386                                 renderers[i].set_fixed_height_from_font(1);
387                                 int size;
388                                 renderers[i].get_size(this, null, null, null, null, out size);
389                                 renderer_sizes[i] = size;
390                                 focus_step_size += size;
391                         }
392                 }
393
394                 // Draw background
395                 draw_background(background);
396
397                 base_layout_needed = false;
398                 focus_layout_needed = true;
399         }
400
401         protected int el_renderer(int idx)
402         {
403                 int fs2 = _focus_size/2;
404                 int renderer_idx;
405                 if (idx < cur_el)
406                         renderer_idx = idx - (cur_el-fs2-steps);
407                 else
408                         renderer_idx = (cur_el+fs2+steps) - idx;
409                 if (renderer_idx < 0)
410                         return 0;
411                 if (renderer_idx >= renderer_sizes.length)
412                         return renderer_sizes.length-1;
413                 return renderer_idx;
414         }
415
416         protected void focus_layout()
417         {
418                 if (!is_fisheye || label_count == 0)
419                 {
420                         focus_first = 0;
421                         focus_end = 0;
422                         focus_layout_needed = false;
423                         return;
424                 }
425
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;
430
431                 int cur_pos = 0;
432                 int cur_idx = 0;
433                 int focus_area_pre = 0;
434                 while (cur_pos < allocation.height && cur_idx < focus_info.length)
435                 {
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];
444                         ++cur_idx;
445                 }
446
447                 focus_info[cur_idx].y = cur_pos;
448                 focus_info[cur_idx].renderer = 0;
449                 focus_end = focus_first + cur_idx;
450
451                 int anchor_y = cur_el * allocation.height / (label_count+1);
452                 int focus_area_size = cur_pos;
453
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;
458
459 //              stderr.printf("FA [0 [%d+%d=%d] %d]\n", focus_area_start, focus_area_size, focus_area_start+focus_area_size, allocation.height);
460
461                 focus_layout_needed = false;
462         }
463
464         protected void draw_background(Gdk.Drawable drawable)
465         {
466                 Gtk.Style style = get_style();
467
468                 // Background
469                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
470
471                 if (!is_fisheye)
472                         return;
473
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);
477
478                 for (int y = 0; y < allocation.height/2 - 30; y += 30)
479                 {
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);
486                 }
487
488                 // Draw tiny labels
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;
493
494                 Gdk.Rectangle cell_area = Gdk.Rectangle();
495                 cell_area.x = 0;
496                 cell_area.width = allocation.width;
497                 cell_area.height = renderer_sizes[0];
498                 if (label_count * cell_area.height >= allocation.height)
499                 {
500                         for (int y = 0; y < allocation.height; y += cell_area.height)
501                         {
502                                 int idx = y * label_count / allocation.height;
503                                 cell_area.y = y;
504                                 renderers[0].text = label_cache[idx].label;
505                                 renderers[0].render((Gdk.Window*)drawable, this, 
506                                                 cell_area,
507                                                 cell_area,
508                                                 expose_area,
509                                                 0);
510                         }
511                 } else {
512                         int count = int.min(allocation.height/(2*cell_area.height), label_count);
513                         for (int idx = 0; idx < count; ++idx)
514                         {
515                                 cell_area.y = idx * cell_area.height;
516                                 renderers[0].text = label_cache[idx].label;
517                                 renderers[0].render((Gdk.Window*)drawable, this, 
518                                                 cell_area,
519                                                 cell_area,
520                                                 expose_area,
521                                                 0);
522
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, 
526                                                 cell_area,
527                                                 cell_area,
528                                                 expose_area,
529                                                 0);
530                         }
531                 }
532
533                 // Draw prefix labels
534                 prefix_layout = create_pango_layout(null);
535                 Pango.Rectangle lr;
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);
542
543                 int last_covered = 0;
544
545                 for (weak List<PrefixInfo> i = prefixes; i != null; i = i.next)
546                 {
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;
552                 }
553         }
554
555         protected void draw()
556         {
557                 if (base_layout_needed)
558                         base_layout();
559                 if (focus_layout_needed)
560                         focus_layout();
561
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;
568
569                 // Background
570                 drawable.draw_drawable(
571                         get_style().fg_gc[Gtk.StateType.NORMAL],
572                         background,
573                         0, 0, 0, 0,
574                         allocation.width, allocation.height);
575
576                 if (is_fisheye)
577                 {
578                         // Paint current prefix
579                         if (cur_pfx != null)
580                         {
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);
583                         }
584
585                         // Focus lock area
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);
588
589                         // Paint items around focus
590                         Gdk.Rectangle cell_area = Gdk.Rectangle();
591                         cell_area.x = 0;
592                         cell_area.width = expose_area.width;
593                         for (int idx = 0; idx < focus_info.length; ++idx)
594                         {
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];
598
599                                 Gtk.CellRendererState rflags = 0;
600                                 if (idx + focus_first == cur_el)
601                                 {
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);
605                                 }
606                         
607                                 renderer.text = label_cache[idx + focus_first].label;
608                                 renderer.render((Gdk.Window*)drawable, this, 
609                                                 cell_area,
610                                                 cell_area,
611                                                 expose_area,
612                                                 rflags);
613
614                                 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
615                         }
616                 } else {
617                         // Paint all items sequentially
618                         var renderer = renderers[renderers.length-1];
619                         Gdk.Rectangle cell_area = Gdk.Rectangle();
620                         cell_area.x = 0;
621                         cell_area.width = allocation.width;
622                         cell_area.height = max_renderer_size;
623                         for (int idx = 0; idx < label_count; ++idx)
624                         {
625                                 cell_area.y = idx * cell_area.height;
626
627                                 Gtk.CellRendererState rflags = 0;
628                                 if (idx == cur_el)
629                                 {
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);
633                                 }
634
635                                 renderer.text = label_cache[idx].label;
636                                 renderer.render((Gdk.Window*)drawable, this, 
637                                                 cell_area,
638                                                 cell_area,
639                                                 expose_area,
640                                                 rflags);
641                         }
642                 }
643         }
644
645         /*
646          * A relevant annotation from Prefuse:
647          *
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>. 
653          *
654          * See also http://www.cs.umd.edu/hcil/fisheyemenu/
655          */
656 }