Fixed size of focus lock area
[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 = 21;
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                         if (_focus_size >= label_cache.length)
265                         {
266                                 focus_first = 0;
267                                 focus_end = label_cache.length;
268                         } else {
269                                 focus_first = cur_el -_focus_size/2;
270                                 if (focus_first < 0) focus_first = 0;
271
272                                 focus_end = focus_first + _focus_size;
273                                 if (focus_end > label_cache.length)
274                                 {
275                                         focus_end = label_cache.length;
276                                         focus_first = focus_end - _focus_size;
277                                 }
278                         }
279
280                         // Compute starting positions for all items in focus
281                         for (int idx = focus_first; idx <= focus_end; ++idx)
282                                 focus_starts[idx - focus_first] = el_y(idx);
283                 }
284                 focus_layout_needed = false;
285         }
286
287         protected void draw(Gdk.Drawable drawable)
288         {
289                 if (build_label_cache_needed)
290                         build_label_cache();
291                 if (focus_layout_needed)
292                         focus_layout();
293
294                 Gtk.Style style = get_style();
295
296                 // Background
297                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
298
299                 // Focus lock area
300                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
301                         0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]-focus_starts[0]);
302
303                 // Focus movement area
304                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
305                         allocation.width/2, 0, allocation.width, allocation.height);
306
307                 // Paint items around focus
308                 for (int idx = 0; idx < focus_end-focus_first; ++idx)
309                 {
310                         int y0 = focus_starts[idx];
311                         int y1 = focus_starts[idx + 1];
312
313                         Gtk.StateType itemState = Gtk.StateType.NORMAL;
314                         if (idx + focus_first == cur_el)
315                         {
316                                 itemState = Gtk.StateType.SELECTED;
317                                 drawable.draw_rectangle(style.bg_gc[itemState], true,
318                                         0, y0, allocation.width, y1-y0);
319                         }
320
321                 
322                         int size = (y1-y0)*80/100;
323                         if (size <= 0) size = 1;
324                         if (size >= pango_cache.length) size = pango_cache.length - 1;
325                         if (pango_cache[size] == null)
326                         {
327                                 var fd = style.font_desc.copy();
328                                 fd.set_absolute_size(size*Pango.SCALE);
329                                 var pc = create_pango_context();
330                                 pc.set_font_description(fd);
331                                 pango_cache[size] = new Pango.Layout(pc);
332                         }
333                         pango_cache[size].set_text(label_cache[idx + focus_first], -1);
334                         var layout = pango_cache[size];
335                         Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
336
337                         //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
338                 }
339         }
340
341         /*
342          * The following function is adapted from Prefuse's FisheyeDistortion.java.
343          *
344          * A relevant annotation from Prefuse:
345          *
346          * For more details on this form of transformation, see Manojit Sarkar and 
347          * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
348          * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
349          * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
350          * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
351          */
352
353         /*
354          * Distorts an item's coordinate.
355          * @param x the undistorted coordinate
356          * @param coordinate of the anchor or focus point
357          * @param d disortion factor
358          * @param min the beginning of the display
359          * @param max the end of the display
360          * @return the distorted coordinate
361          */
362         private int fisheye(int idx)
363         {
364                 // Autocompute distortion factor
365                 // 20 is the pixel size of the item at centre of focus
366                 double d = label_cache.length * 20 / allocation.height;
367                 if ( d <= 1 )
368                         return idx * allocation.height / label_cache.length;
369
370                 double a = (double)cur_el * allocation.height / label_cache.length;
371                 double x = (double)idx * allocation.height / label_cache.length;
372                 double max = (double)allocation.height;
373
374                 if (idx < cur_el)
375                 {
376                         double m = a;
377                         if ( m == 0 ) m = max;
378                         double v = (double)(a - x) / m;
379                         v = (double)(d+1)/(d+(1/v));
380                         return (int)Math.round(a - m*v);
381                 } else {
382                         double m = max-a;
383                         if ( m == 0 ) m = max;
384                         double v = (double)(x - a) / m;
385                         v = (double)(d+1)/(d+(1/v));
386                         return (int)Math.round(a + m*v);
387                 }
388         }
389 }
390
391 public class Fisheye : Gtk.Window
392 {
393         public Fisheye()
394         {
395                 title = "Fisheye";
396                 destroy += Gtk.main_quit;
397
398                 var list = new FisheyeList();
399                 add(list);
400
401                 var store = new Gtk.ListStore(1, typeof(string));
402                 Gtk.TreeIter iter;
403                 var infd = FileStream.open("/tmp/names", "r");
404                 if (infd == null)
405                 {
406                         for (int i = 0; i < 300; ++i)
407                         {
408                                 store.append(out iter);
409                                 store.set(iter, 0, "Antani %d".printf(i), -1);
410                         }
411                 } else {
412                         char buf[255];
413                         while (true)
414                         {
415                                 string line = infd.gets(buf);
416                                 if (line == null)
417                                         break;
418                                 store.append(out iter);
419                                 store.set(iter, 0, line, -1);
420                         }
421                 }
422
423                 list.set_model(store);
424         }
425 }
426
427 static int main (string[] args) {
428         Gtk.init (ref args);
429
430         var fe = new Fisheye();
431         fe.set_size_request(200, 300);
432         fe.show_all();
433
434         Gtk.main();
435
436         return 0;
437 }