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