Int-only rendering
[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
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 FisheyeList()
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_info[0].y && y < focus_info[focus_end - focus_first].y;
168
169                         if (y < focus_info[0].y || y >= focus_info[focus_end - focus_first].y)
170                                 cur_el = y * (label_cache.length+1) / allocation.height;
171                         else
172                                 for (int idx = focus_first; idx < focus_end; ++idx)
173                                         if (y < focus_info[idx-focus_first+1].y)
174                                         {
175                                                 cur_el = idx;
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 int el_y(int idx)
228         {
229                 // Distorted position
230                 int pos = fisheye(idx);
231                 //stderr.printf("%d %f %f\n", idx, undy, pos);
232                 return pos;
233         }
234
235         protected Gtk.CellRendererText make_cell_renderer()
236         {
237                 var res = new Gtk.CellRendererText();
238                 res.font_desc = get_style().font_desc;
239                 res.ypad = 0;
240                 return res;
241         }
242
243         protected void base_layout()
244         {
245                 // Rebuild label cache
246                 if (model == null)
247                 {
248                         label_cache = new string[0];
249                 } else {
250                         Gtk.TreeIter iter;
251                         if (!model.get_iter_first(out iter))
252                         {
253                                 label_cache = new string[0];
254                         }
255                         else
256                         {
257                                 int count = model.iter_n_children(null);
258                                 label_cache = new string[count];
259
260                                 int i = 0;
261                                 do {
262                                         string val;
263                                         model.get(iter, _label_column, out val, -1);
264                                         label_cache[i] = val;
265                                         ++i;
266                                 } while (model.iter_next(ref iter));
267                         }
268                 }
269
270                 background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
271                 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
272
273                 // Recreate the renderers
274                 renderers[renderers.length-1] = make_cell_renderer();
275                 renderers[renderers.length-1].set_fixed_height_from_font(1);
276                 renderers[renderers.length-1].get_size(this, null, null, null, null, out max_renderer_size);
277                 renderer_sizes[renderers.length-1] = max_renderer_size;
278
279                 is_fisheye = label_cache.length * max_renderer_size > allocation.height;
280
281                 if (is_fisheye)
282                 {
283                         renderers[0] = make_cell_renderer();
284                         renderers[0].size = Pango.SCALE;
285                         renderers[0].set_fixed_height_from_font(1);
286                         int min_renderer_size;
287                         renderers[0].get_size(this, null, null, null, null, out min_renderer_size);
288                         renderer_sizes[0] = min_renderer_size;
289
290                         focus_step_size = 0;    // Size of the diminishing area
291                         for (int i = 1; i < renderers.length-1; ++i)
292                         {
293                                 renderers[i] = make_cell_renderer();
294                                 renderers[i].scale = (double)i / renderers.length;
295                                 renderers[i].set_fixed_height_from_font(1);
296                                 int size;
297                                 renderers[i].get_size(this, null, null, null, null, out size);
298                                 renderer_sizes[i] = size;
299                                 focus_step_size += size;
300                         }
301                 }
302
303                 // Draw background
304                 draw_background(background);
305
306                 base_layout_needed = false;
307                 focus_layout_needed = true;
308         }
309
310         protected int el_renderer(int idx)
311         {
312                 int fs2 = _focus_size/2;
313                 int renderer_idx;
314                 if (idx < cur_el)
315                         renderer_idx = idx - (cur_el-fs2-steps);
316                 else
317                         renderer_idx = (cur_el+fs2+steps) - idx;
318                 if (renderer_idx < 0)
319                         return 0;
320                 if (renderer_idx >= renderer_sizes.length)
321                         return renderer_sizes.length-1;
322                 return renderer_idx;
323         }
324
325         protected void focus_layout()
326         {
327                 if (!is_fisheye || label_cache.length == 0)
328                 {
329                         focus_first = 0;
330                         focus_end = 0;
331                         focus_layout_needed = false;
332                         return;
333                 }
334
335                 int focus_area_size = 2 * focus_step_size + _focus_size * max_renderer_size;
336                 int anchor_y = cur_el * allocation.height / (label_cache.length+1);
337                 int focus_area_start = anchor_y - (focus_area_size - max_renderer_size) / 2;
338                 if (focus_area_start < 0) focus_area_start = 0;
339
340                 focus_first = cur_el - _focus_size/2 - steps;
341                 if (focus_first < 0) focus_first = 0;
342
343                 /*
344                 int full_focus_start = anchor_y - (_focus_size/2) * max_renderer_size;
345                 int full_focus_end = anchor_y + (_focus_size/2) * max_renderer_size;
346                 int steps_start = full_focus_start - focus_step_size;
347                 */
348
349                 int cur_pos = focus_area_start;
350                 int cur_idx = focus_first;
351                 while (cur_pos < allocation.height && cur_idx < label_cache.length && cur_idx - focus_first < focus_info.length)
352                 {
353                         focus_info[cur_idx - focus_first].y = cur_pos;
354                         focus_info[cur_idx - focus_first].renderer = el_renderer(cur_idx);
355 //                      stderr.printf("LAYOUT [%d-%d-?] at %d/%d: pos %d rend %d rsz %d\n",
356 //                              focus_first, cur_idx, cur_idx - focus_first, focus_info.length, cur_pos, focus_info[cur_idx - focus_first].renderer, renderer_sizes[focus_info[cur_idx - focus_first].renderer]);
357                         cur_pos += renderer_sizes[focus_info[cur_idx - focus_first].renderer];
358                         ++cur_idx;
359                 }
360
361                 focus_end = cur_idx;
362                 focus_info[focus_end - focus_first].y = cur_pos;
363                 focus_info[focus_end - focus_first].renderer = 0;
364
365                 focus_layout_needed = false;
366         }
367
368         protected void draw_background(Gdk.Drawable drawable)
369         {
370                 Gtk.Style style = get_style();
371
372                 // Background
373                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
374
375                 if (!is_fisheye)
376                         return;
377
378                 // Focus movement area
379                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
380                         allocation.width/2, 0, allocation.width, allocation.height);
381
382                 for (int y = 0; y < allocation.height/2 - 30; y += 30)
383                 {
384                         Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
385                                 null, this, null, Gtk.ArrowType.UP, false,
386                                 allocation.width/2, y, allocation.width/2, 10);
387                         Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
388                                 null, this, null, Gtk.ArrowType.DOWN, false,
389                                 allocation.width/2, allocation.height-y-30, allocation.width/2, 10);
390                 }
391
392                 Gdk.Rectangle expose_area = Gdk.Rectangle();
393                 expose_area.x = expose_area.y = 0;
394                 expose_area.width = allocation.width;
395                 expose_area.height = allocation.height;
396
397                 Gdk.Rectangle cell_area = Gdk.Rectangle();
398                 cell_area.x = 0;
399                 cell_area.width = allocation.width;
400                 cell_area.height = renderer_sizes[0];
401                 if (label_cache.length * cell_area.height >= allocation.height)
402                 {
403                         for (int y = 0; y < allocation.height; y += cell_area.height)
404                         {
405                                 int idx = y * label_cache.length / allocation.height;
406                                 cell_area.y = y;
407                                 renderers[0].text = label_cache[idx];
408                                 renderers[0].render((Gdk.Window*)drawable, this, 
409                                                 cell_area,
410                                                 cell_area,
411                                                 expose_area,
412                                                 0);
413                         }
414                 } else {
415                         int count = int.min(allocation.height/(2*cell_area.height), label_cache.length);
416                         for (int idx = 0; idx < count; ++idx)
417                         {
418                                 cell_area.y = idx * cell_area.height;
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                                 cell_area.y = allocation.height-cell_area.y;
427                                 renderers[0].text = label_cache[label_cache.length-idx];
428                                 renderers[0].render((Gdk.Window*)drawable, this, 
429                                                 cell_area,
430                                                 cell_area,
431                                                 expose_area,
432                                                 0);
433                         }
434                 }
435         }
436
437         protected void draw()
438         {
439                 if (base_layout_needed)
440                         base_layout();
441                 if (focus_layout_needed)
442                         focus_layout();
443
444                 var drawable = backing_store;
445                 Gtk.Style style = get_style();
446                 Gdk.Rectangle expose_area = Gdk.Rectangle();
447                 expose_area.x = expose_area.y = 0;
448                 expose_area.width = allocation.width;
449                 expose_area.height = allocation.height;
450
451                 // Background
452                 drawable.draw_drawable(
453                         get_style().fg_gc[Gtk.StateType.NORMAL],
454                         background,
455                         0, 0, 0, 0,
456                         allocation.width, allocation.height);
457
458                 if (is_fisheye)
459                 {
460                         // Focus lock area
461                         drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
462                                 0, focus_info[0].y, allocation.width/2, focus_info[focus_end - focus_first].y-focus_info[0].y);
463
464                         // Paint items around focus
465                         Gdk.Rectangle cell_area = Gdk.Rectangle();
466                         cell_area.x = 0;
467                         cell_area.width = allocation.width;
468                         for (int idx = 0; idx < focus_end-focus_first; ++idx)
469                         {
470                                 var renderer = renderers[focus_info[idx].renderer];
471                                 cell_area.y = focus_info[idx].y;
472                                 cell_area.height = renderer_sizes[focus_info[idx].renderer];
473
474                                 Gtk.CellRendererState rflags = 0;
475                                 if (idx + focus_first == cur_el)
476                                 {
477                                         rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
478                                         drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
479                                                         cell_area.x, cell_area.y, cell_area.width, cell_area.height);
480                                 }
481                         
482                                 renderer.text = label_cache[idx + focus_first];
483                                 renderer.render((Gdk.Window*)drawable, this, 
484                                                 cell_area,
485                                                 cell_area,
486                                                 expose_area,
487                                                 rflags);
488
489                                 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
490                         }
491                 } else {
492                         // Paint all items sequentially
493                         var renderer = renderers[renderers.length-1];
494                         Gdk.Rectangle cell_area = Gdk.Rectangle();
495                         cell_area.x = 0;
496                         cell_area.width = allocation.width;
497                         cell_area.height = max_renderer_size;
498                         for (int idx = 0; idx < label_cache.length; ++idx)
499                         {
500                                 cell_area.y = idx * cell_area.height;
501
502                                 Gtk.CellRendererState rflags = 0;
503                                 if (idx == cur_el)
504                                 {
505                                         rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
506                                         drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
507                                                         cell_area.x, cell_area.y, cell_area.width, cell_area.height);
508                                 }
509
510                                 renderer.text = label_cache[idx];
511                                 renderer.render((Gdk.Window*)drawable, this, 
512                                                 cell_area,
513                                                 cell_area,
514                                                 expose_area,
515                                                 rflags);
516                         }
517                 }
518         }
519
520         /*
521          * The following function is adapted from Prefuse's FisheyeDistortion.java.
522          *
523          * A relevant annotation from Prefuse:
524          *
525          * For more details on this form of transformation, see Manojit Sarkar and 
526          * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
527          * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
528          * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
529          * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
530          */
531
532         /*
533          * Distorts an item's coordinate.
534          * @param x the undistorted coordinate
535          * @param coordinate of the anchor or focus point
536          * @param d disortion factor
537          * @param min the beginning of the display
538          * @param max the end of the display
539          * @return the distorted coordinate
540          */
541         private int fisheye(int idx)
542         {
543                 // Autocompute distortion factor
544                 // 20 is the pixel size of the item at centre of focus
545                 double d = label_cache.length * 20 / allocation.height;
546                 if ( d <= 1 )
547                         return idx * allocation.height / label_cache.length;
548
549                 double a = (double)cur_el * allocation.height / label_cache.length;
550                 double x = (double)idx * allocation.height / label_cache.length;
551                 double max = (double)allocation.height;
552
553                 if (idx < cur_el)
554                 {
555                         double m = a;
556                         if ( m == 0 ) m = max;
557                         double v = (double)(a - x) / m;
558                         v = (double)(d+1)/(d+(1/v));
559                         return (int)Math.round(a - m*v);
560                 } else {
561                         double m = max-a;
562                         if ( m == 0 ) m = max;
563                         double v = (double)(x - a) / m;
564                         v = (double)(d+1)/(d+(1/v));
565                         return (int)Math.round(a + m*v);
566                 }
567         }
568 }
569
570 public class Fisheye : Gtk.Window
571 {
572         public Fisheye()
573         {
574                 title = "Fisheye";
575                 destroy += Gtk.main_quit;
576
577                 var list = new FisheyeList();
578                 add(list);
579
580                 var store = new Gtk.ListStore(1, typeof(string));
581                 Gtk.TreeIter iter;
582                 var infd = FileStream.open("/tmp/names", "r");
583                 if (infd == null)
584                 {
585                         for (int i = 0; i < 300; ++i)
586                         {
587                                 store.append(out iter);
588                                 store.set(iter, 0, "Antani %d".printf(i), -1);
589                         }
590                 } else {
591                         char buf[255];
592                         while (true)
593                         {
594                                 string line = infd.gets(buf);
595                                 if (line == null)
596                                         break;
597                                 store.append(out iter);
598                                 store.set(iter, 0, line, -1);
599                         }
600                 }
601
602                 list.set_model(store);
603         }
604 }
605
606 static int main (string[] args) {
607         Gtk.init (ref args);
608
609         var fe = new Fisheye();
610         fe.set_size_request(200, 300);
611         fe.show_all();
612
613         Gtk.main();
614
615         return 0;
616 }