c40c8a73d24afc3dd400664d234f3c38572208f7
[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 public class FisheyeList : Gtk.DrawingArea
24 {
25         protected Gtk.TreeModel model;
26         protected Gdk.Pixmap backing_store;
27
28         // Pango layouts cached for speed
29         protected const int max_font_size = 30;
30         protected Pango.Layout[] pango_cache;
31
32         // Labels to show, extracted from the model
33         protected string[] label_cache;
34         protected bool build_label_cache_needed;
35
36         protected int cur_el;
37
38         // Layout information
39         protected int focus_first;
40         protected int focus_end;
41         protected int[] focus_starts;
42         protected bool focus_locked;
43         protected bool focus_layout_needed;
44
45         protected int _focus_size;
46         public int focus_size {
47                 get { return _focus_size; }
48                 set {
49                         _focus_size = value;
50                         focus_starts = new int[value+1];
51                         focus_layout_needed = true;
52                         queue_draw();
53                 }
54         }
55
56         protected int _label_column;
57         public int label_column {
58                 get { return _label_column; }
59                 set {
60                         _label_column = value;
61                         build_label_cache_needed = true;
62                         queue_draw();
63                 }
64         }
65
66         //public virtual signal void cursor_changed ();
67         public signal void row_activated(Gtk.TreePath path);
68
69         public FisheyeList()
70         {
71                 model = null;
72                 backing_store = null;
73
74                 label_cache = null;
75
76                 pango_cache = new Pango.Layout[max_font_size];
77                 for (int i = 0; i < pango_cache.length; ++i)
78                         pango_cache[i] = null;
79
80                 // Defaults for properties
81                 focus_size = 20;
82                 label_column = 0;
83
84                 cur_el = 0;
85                 focus_locked = false;
86
87                 add_events(Gdk.EventMask.POINTER_MOTION_MASK
88                          | Gdk.EventMask.BUTTON_PRESS_MASK
89                          | Gdk.EventMask.BUTTON_RELEASE_MASK);
90         }
91
92         public unowned Gtk.TreeModel get_model() { return model; }
93         public void set_model (Gtk.TreeModel? model)
94         {
95                 if (this.model != null)
96                 {
97                         this.model.row_changed -= on_row_changed;
98                         this.model.row_deleted -= on_row_deleted;
99                         this.model.row_has_child_toggled -= on_row_has_child_toggled;
100                         this.model.row_inserted -= on_row_inserted;
101                         this.model.rows_reordered -= on_rows_reordered;
102                 }
103                 this.model = model;
104                 this.model.row_changed += on_row_changed;
105                 this.model.row_deleted += on_row_deleted;
106                 this.model.row_has_child_toggled += on_row_has_child_toggled;
107                 this.model.row_inserted += on_row_inserted;
108                 this.model.rows_reordered += on_rows_reordered;
109                 build_label_cache_needed = true;
110                 queue_draw();
111         }
112
113         private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { build_label_cache_needed = true; }
114         private void on_row_deleted(Gtk.TreePath path) { build_label_cache_needed = true; }
115         private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { build_label_cache_needed = true; }
116         private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { build_label_cache_needed = true; }
117         private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { build_label_cache_needed = true; }
118
119         /* Mouse button got pressed over widget */
120         /*
121         public override bool button_press_event(Gdk.EventButton event)
122         {
123                 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
124                 return false;
125         }
126         */
127
128         /* Mouse button got released */
129         public override bool button_release_event(Gdk.EventButton event)
130         {
131                 stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
132
133                 // Emit row_activated if applicable
134                 if (model != null)
135                 {
136                         Gtk.TreeIter iter;
137                         if (model.iter_nth_child(out iter, null, cur_el))
138                         {
139                                 Gtk.TreePath path = model.get_path(iter);
140                                 row_activated(path);
141                         }
142                 }
143                 return false;
144         }
145
146         /* Mouse pointer moved over widget */
147         public override bool motion_notify_event(Gdk.EventMotion event)
148         {
149                 int old_cur_el = cur_el;
150                 int x = (int)event.x;
151                 int y = (int)event.y;
152
153                 focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
154
155                 if (focus_locked)
156                 {
157                         for (int idx = focus_first; idx < focus_end; ++idx)
158                                 if (y < focus_starts[idx-focus_first+1])
159                                 {
160                                         cur_el = idx;
161                                         break;
162                                 }
163
164                 } else {
165                         cur_el = y * label_cache.length / allocation.height;
166                         if (old_cur_el != cur_el)
167                                 focus_layout_needed = true;
168                 }
169
170                 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
171                 if (old_cur_el != cur_el)
172                 {
173                         queue_draw();
174                         old_cur_el = cur_el;
175                 }
176                 return false;
177         }
178
179         public override bool configure_event (Gdk.EventConfigure event)
180         {
181                 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
182                 focus_layout_needed = true;
183                 queue_draw();
184                 return false;
185         }
186
187         /* Widget is asked to draw itself */
188         public override bool expose_event (Gdk.EventExpose event)
189         {
190                 if (backing_store == null)
191                         return false;
192
193                 draw(backing_store);
194
195                 window.draw_drawable(
196                         get_style().fg_gc[Gtk.StateType.NORMAL],
197                         backing_store,
198                         event.area.x, event.area.y,
199                         event.area.x, event.area.y,
200                         event.area.width, event.area.height);
201
202                 return false;
203         }
204
205         public override void style_set(Gtk.Style? previous_style)
206         {
207                 // Reset the pango cache if the pango context changes
208                 for (int i = 0; i < pango_cache.length; ++i)
209                         pango_cache[i] = null;
210         }
211         public override void direction_changed(Gtk.TextDirection previous_direction)
212         {
213                 // Reset the pango cache if the pango context changes
214                 for (int i = 0; i < pango_cache.length; ++i)
215                         pango_cache[i] = null;
216         }
217
218         protected int el_y(int idx)
219         {
220                 // Distorted position
221                 int pos = fisheye(idx);
222                 //stderr.printf("%d %f %f\n", idx, undy, pos);
223                 return pos;
224         }
225
226         protected void build_label_cache()
227         {
228                 if (model == null)
229                 {
230                         label_cache = new string[0];
231                 } else {
232                         Gtk.TreeIter iter;
233                         if (!model.get_iter_first(out iter))
234                         {
235                                 label_cache = new string[0];
236                         }
237                         else
238                         {
239                                 int count = model.iter_n_children(null);
240                                 label_cache = new string[count];
241
242                                 int i = 0;
243                                 do {
244                                         string val;
245                                         model.get(iter, _label_column, out val, -1);
246                                         label_cache[i] = val;
247                                         ++i;
248                                 } while (model.iter_next(ref iter));
249                         }
250                 }
251
252                 build_label_cache_needed = false;
253                 focus_layout_needed = true;
254         }
255
256         protected void focus_layout()
257         {
258                 if (label_cache.length == 0)
259                 {
260                         focus_first = 0;
261                         focus_end = 0;
262                         focus_starts[0] = 0;
263                 } else {
264                         focus_first = cur_el > _focus_size/2 ? cur_el-_focus_size/2 : 0;
265                         focus_end = focus_first + _focus_size;
266                         if (focus_end >= label_cache.length) focus_end = label_cache.length;
267
268                         // Compute starting positions for all items in focus
269                         for (int idx = focus_first; idx <= focus_end; ++idx)
270                         {
271                                 focus_starts[idx - focus_first] = el_y(idx);
272                         }
273                 }
274                 focus_layout_needed = false;
275         }
276
277         protected void draw(Gdk.Drawable drawable)
278         {
279                 if (build_label_cache_needed)
280                         build_label_cache();
281                 if (focus_layout_needed)
282                         focus_layout();
283
284                 Gtk.Style style = get_style();
285
286                 // Background
287                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
288
289                 // Focus lock area
290                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
291                         0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
292
293                 // Focus movement area
294                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
295                         allocation.width/2, 0, allocation.width, allocation.height);
296
297                 // Create a Cairo context
298                 //var context = Gdk.cairo_create (drawable);
299
300                 // Paint items around focus
301                 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
302                 for (int idx = focus_first; idx < focus_end; ++idx)
303                 {
304                         int y0 = focus_starts[idx - focus_first];
305                         int y1 = focus_starts[idx - focus_first + 1];
306
307                         Gtk.StateType itemState = Gtk.StateType.NORMAL;
308                         if (idx == cur_el)
309                         {
310                                 itemState = Gtk.StateType.SELECTED;
311                                 drawable.draw_rectangle(style.bg_gc[itemState], true,
312                                         0, y0, allocation.width, y1-y0);
313                         }
314
315                 
316                         // TODO: cache pango contexts instead of fontdescs
317                         int size = (y1-y0)*80/100;
318                         if (size <= 0) size = 1;
319                         if (size >= pango_cache.length) size = pango_cache.length - 1;
320                         if (pango_cache[size] == null)
321                         {
322                                 var fd = style.font_desc.copy();
323                                 fd.set_absolute_size(size*Pango.SCALE);
324                                 var pc = create_pango_context();
325                                 pc.set_font_description(fd);
326                                 pango_cache[size] = new Pango.Layout(pc);
327                         }
328                         pango_cache[size].set_text(label_cache[idx], -1);
329                         var layout = pango_cache[size];
330                         //stderr.printf("AZAZA %p\n", layout.get_attributes());
331                         //var attrlist = layout.get_attributes().copy();
332                         //stderr.printf("AL %p\n", attrlist);
333                         //var attrlist = new Pango.AttrList();
334                         //stderr.printf("SIZE %d\n", y1-y0);
335                         //attrlist.insert(new Pango.AttrSize(y1-y0));
336                         //var attrlist = layout.get_attributes();
337                         //attrlist.change(new Pango.AttrSize(y1-y0));
338                         //layout.set_attributes(attrlist);
339                         //layout.set_height(y1-y0);
340                         //int w, h;
341                         //layout.get_pixel_size(out w, out h);
342                         Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
343                 }
344         }
345
346         /*
347          * The following function is adapted from Prefuse's FisheyeDistortion.java.
348          *
349          * A relevant annotation from Prefuse:
350          *
351          * For more details on this form of transformation, see Manojit Sarkar and 
352          * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
353          * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
354          * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
355          * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
356          */
357
358         /*
359          * Distorts an item's coordinate.
360          * @param x the undistorted coordinate
361          * @param coordinate of the anchor or focus point
362          * @param d disortion factor
363          * @param min the beginning of the display
364          * @param max the end of the display
365          * @return the distorted coordinate
366          */
367         private int fisheye(int idx)
368         {
369                 // Autocompute distortion factor
370                 // 20 is the pixel size of the item at centre of focus
371                 double d = label_cache.length * 20 / allocation.height;
372                 if ( d <= 1 )
373                         return idx * allocation.height / label_cache.length;
374
375                 double a = (double)cur_el * allocation.height / label_cache.length;
376                 double x = (double)idx * allocation.height / label_cache.length;
377                 double max = (double)allocation.height;
378
379                 if (idx < cur_el)
380                 {
381                         double m = a;
382                         if ( m == 0 ) m = max;
383                         double v = (double)(a - x) / m;
384                         v = (double)(d+1)/(d+(1/v));
385                         return (int)Math.round(a - m*v);
386                 } else {
387                         double m = max-a;
388                         if ( m == 0 ) m = max;
389                         double v = (double)(x - a) / m;
390                         v = (double)(d+1)/(d+(1/v));
391                         return (int)Math.round(a + m*v);
392                 }
393         }
394 }
395
396 public class Fisheye : Gtk.Window
397 {
398         public Fisheye()
399         {
400                 title = "Fisheye";
401                 destroy += Gtk.main_quit;
402
403                 var list = new FisheyeList();
404                 add(list);
405
406                 var store = new Gtk.ListStore(1, typeof(string));
407                 Gtk.TreeIter iter;
408                 var infd = FileStream.open("/tmp/names", "r");
409                 if (infd == null)
410                 {
411                         for (int i = 0; i < 300; ++i)
412                         {
413                                 store.append(out iter);
414                                 store.set(iter, 0, "Antani %d".printf(i), -1);
415                         }
416                 } else {
417                         char buf[255];
418                         while (true)
419                         {
420                                 string line = infd.gets(buf);
421                                 if (line == null)
422                                         break;
423                                 store.append(out iter);
424                                 store.set(iter, 0, line, -1);
425                         }
426                 }
427
428                 list.set_model(store);
429         }
430 }
431
432 static int main (string[] args) {
433         Gtk.init (ref args);
434
435         var fe = new Fisheye();
436         fe.set_size_request(200, 300);
437         fe.show_all();
438
439         Gtk.main();
440
441         return 0;
442 }