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