Read data from /tmp/names if present
[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 virtual signal void cursor_changed ();
78         public signal void row_activated(Gtk.TreePath path);
79
80         public FisheyeList()
81         {
82                 model = null;
83                 backing_store = null;
84
85                 label_cache = null;
86
87                 pango_cache = new Pango.Layout[max_font_size];
88                 for (int i = 0; i < pango_cache.length; ++i)
89                         pango_cache[i] = null;
90
91                 // Defaults for properties
92                 focus_size = 20;
93                 distortion_factor = 30;
94                 label_column = 0;
95
96                 cur_el = 0;
97                 focus_centre = 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                 build_label_cache_needed = true;
123                 queue_draw();
124         }
125
126         private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { build_label_cache_needed = true; }
127         private void on_row_deleted(Gtk.TreePath path) { build_label_cache_needed = true; }
128         private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { build_label_cache_needed = true; }
129         private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { build_label_cache_needed = true; }
130         private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { build_label_cache_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                 focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
167
168                 if (focus_locked)
169                 {
170                         for (int idx = focus_first; idx < focus_end; ++idx)
171                                 if (y < focus_starts[idx-focus_first+1])
172                                 {
173                                         cur_el = idx;
174                                         break;
175                                 }
176
177                 } else {
178                         cur_el = y * label_cache.length / allocation.height;
179                         if (old_cur_el != cur_el)
180                                 focus_layout_needed = true;
181                 }
182
183                 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
184                 if (old_cur_el != cur_el)
185                 {
186                         queue_draw();
187                         old_cur_el = cur_el;
188                 }
189                 return false;
190         }
191
192         public override bool configure_event (Gdk.EventConfigure event)
193         {
194                 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
195                 focus_layout_needed = true;
196                 queue_draw();
197                 return false;
198         }
199
200         /* Widget is asked to draw itself */
201         public override bool expose_event (Gdk.EventExpose event)
202         {
203                 if (backing_store == null)
204                         return false;
205
206                 draw(backing_store);
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                 // Reset the pango cache if the pango context changes
221                 for (int i = 0; i < pango_cache.length; ++i)
222                         pango_cache[i] = null;
223         }
224         public override void direction_changed(Gtk.TextDirection previous_direction)
225         {
226                 // Reset the pango cache if the pango context changes
227                 for (int i = 0; i < pango_cache.length; ++i)
228                         pango_cache[i] = null;
229         }
230
231         protected int el_y(int idx)
232         {
233                 // Undistorted Y
234                 int undy = idx * allocation.height / label_cache.length;
235                 // Distorted position
236                 int pos = fisheye(undy, focus_centre, _distortion_factor, 0, allocation.height);
237                 //stderr.printf("%d %f %f\n", idx, undy, pos);
238                 return pos;
239         }
240
241         protected void build_label_cache()
242         {
243                 if (model == null)
244                 {
245                         label_cache = new string[0];
246                 } else {
247                         Gtk.TreeIter iter;
248                         if (!model.get_iter_first(out iter))
249                         {
250                                 label_cache = new string[0];
251                         }
252                         else
253                         {
254                                 int count = model.iter_n_children(null);
255                                 label_cache = new string[count];
256
257                                 int i = 0;
258                                 do {
259                                         string val;
260                                         model.get(iter, _label_column, out val, -1);
261                                         label_cache[i] = val;
262                                         ++i;
263                                 } while (model.iter_next(ref iter));
264                         }
265                 }
266
267                 build_label_cache_needed = false;
268                 focus_layout_needed = true;
269         }
270
271         protected void focus_layout()
272         {
273                 if (label_cache.length == 0)
274                 {
275                         focus_centre = 0;
276                         focus_first = 0;
277                         focus_end = 0;
278                         focus_starts[0] = 0;
279                 } else {
280                         // Anchor point
281                         focus_centre = cur_el*allocation.height/label_cache.length;
282
283                         focus_first = cur_el > _focus_size/2 ? cur_el-_focus_size/2 : 0;
284                         focus_end = focus_first + _focus_size;
285                         if (focus_end >= label_cache.length) focus_end = label_cache.length;
286
287                         // Compute starting positions for all items in focus
288                         for (int idx = focus_first; idx < focus_end; ++idx)
289                         {
290                                 int posprev = idx == 0 ? 0 : el_y(idx-1);
291                                 int pos = el_y(idx);
292                                 int posnext = idx == label_cache.length-1 ? 1 : el_y(idx+1);
293                                 int y0 = (pos+posprev)/2;
294                                 int y1 = (pos+posnext)/2;
295
296                                 focus_starts[idx - focus_first] = y0;
297                                 focus_starts[idx - focus_first + 1] = y1;
298                         }
299                 }
300                 focus_layout_needed = false;
301         }
302
303         protected void draw(Gdk.Drawable drawable)
304         {
305                 if (build_label_cache_needed)
306                         build_label_cache();
307                 if (focus_layout_needed)
308                         focus_layout();
309
310                 Gtk.Style style = get_style();
311
312                 // Background
313                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
314
315                 // Focus lock area
316                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
317                         0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
318
319                 // Focus movement area
320                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
321                         allocation.width/2, 0, allocation.width, allocation.height);
322
323                 // Create a Cairo context
324                 //var context = Gdk.cairo_create (drawable);
325
326                 // Paint items around focus
327                 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
328                 for (int idx = focus_first; idx < focus_end; ++idx)
329                 {
330                         int y0 = focus_starts[idx - focus_first];
331                         int y1 = focus_starts[idx - focus_first + 1];
332
333                         Gtk.StateType itemState = Gtk.StateType.NORMAL;
334                         if (idx == 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                         // TODO: cache pango contexts instead of fontdescs
343                         int size = (y1-y0)*80/100;
344                         if (size <= 0) size = 1;
345                         if (size >= pango_cache.length) size = pango_cache.length - 1;
346                         if (pango_cache[size] == null)
347                         {
348                                 var fd = style.font_desc.copy();
349                                 fd.set_absolute_size(size*Pango.SCALE);
350                                 var pc = create_pango_context();
351                                 pc.set_font_description(fd);
352                                 pango_cache[size] = new Pango.Layout(pc);
353                         }
354                         pango_cache[size].set_text(label_cache[idx], -1);
355                         var layout = pango_cache[size];
356                         //stderr.printf("AZAZA %p\n", layout.get_attributes());
357                         //var attrlist = layout.get_attributes().copy();
358                         //stderr.printf("AL %p\n", attrlist);
359                         //var attrlist = new Pango.AttrList();
360                         //stderr.printf("SIZE %d\n", y1-y0);
361                         //attrlist.insert(new Pango.AttrSize(y1-y0));
362                         //var attrlist = layout.get_attributes();
363                         //attrlist.change(new Pango.AttrSize(y1-y0));
364                         //layout.set_attributes(attrlist);
365                         //layout.set_height(y1-y0);
366                         //int w, h;
367                         //layout.get_pixel_size(out w, out h);
368                         Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
369                 }
370         }
371
372     /*
373      * The following function is adapted from Prefuse's FisheyeDistortion.java.
374      *
375      * A relevant annotation from Prefuse:
376      *
377      * For more details on this form of transformation, see Manojit Sarkar and 
378      * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
379      * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
380      * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
381      * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
382      */
383
384     /*
385      * Distorts an item's coordinate.
386      * @param x the undistorted coordinate
387      * @param coordinate of the anchor or focus point
388      * @param d disortion factor
389      * @param min the beginning of the display
390      * @param max the end of the display
391      * @return the distorted coordinate
392      */
393     private int fisheye(int x, int a, int d, int min, int max)
394     {
395         if ( d != 0 ) {
396             bool left = x<a;
397             double v;
398             int m = (left ? a-min : max-a);
399             if ( m == 0 ) m = max-min;
400             v = (double)(x - a).abs() / m;
401             v = (double)(d+1)/(d+(1/v));
402             return (int)Math.round((left?-1:1)*m*v + a);
403         } else {
404             return x;
405         }
406     }
407 }
408
409 public class Fisheye : Gtk.Window
410 {
411         public Fisheye()
412         {
413                 title = "Fisheye";
414                 destroy += Gtk.main_quit;
415
416                 var list = new FisheyeList();
417                 add(list);
418
419                 var store = new Gtk.ListStore(1, typeof(string));
420                 Gtk.TreeIter iter;
421                 var infd = FileStream.open("/tmp/names", "r");
422                 if (infd == null)
423                 {
424                         for (int i = 0; i < 300; ++i)
425                         {
426                                 store.append(out iter);
427                                 store.set(iter, 0, "Antani %d".printf(i), -1);
428                         }
429                 } else {
430                         char buf[255];
431                         while (true)
432                         {
433                                 string line = infd.gets(buf);
434                                 if (line == null)
435                                         break;
436                                 store.append(out iter);
437                                 store.set(iter, 0, line, -1);
438                         }
439                 }
440
441                 list.set_model(store);
442         }
443 }
444
445 static int main (string[] args) {
446         Gtk.init (ref args);
447
448         var fe = new Fisheye();
449         fe.set_size_request(200, 300);
450         fe.show_all();
451
452         Gtk.main();
453
454         return 0;
455 }