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