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