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