Code cleanup
[gregoa/zavai.git] / src / fisheye.vala
index b8342393263718defac1526b0de5cbbf119af49c..33efc2ace369a2670ac30a00a607e1a188320c6b 100644 (file)
 
 using GLib;
 
-public class FisheyeList : Gtk.DrawingArea
+protected struct FocusInfo
+{
+       int y;
+       int renderer;
+}
+
+
+public class FisheyeListView : Gtk.DrawingArea
 {
        protected Gtk.TreeModel model;
        protected Gdk.Pixmap background;
        protected Gdk.Pixmap backing_store;
 
-       // Pango layouts cached for speed
+       // Renderers used at different sizes
        protected const int steps = 5;
        protected Gtk.CellRendererText[] renderers;
+       protected int[] renderer_sizes;
+       protected int max_renderer_size;
 
        // Labels to show, extracted from the model
        protected string[] label_cache;
@@ -39,16 +48,20 @@ public class FisheyeList : Gtk.DrawingArea
        // Layout information
        protected int focus_first;
        protected int focus_end;
-       protected int[] focus_starts;
+       protected FocusInfo[] focus_info;
        protected bool focus_locked;
        protected bool focus_layout_needed;
+       protected bool is_fisheye;
+       protected int focus_step_size;
+       protected int focus_area_start;
 
+       // Number of elements in full focus
        protected int _focus_size;
        public int focus_size {
                get { return _focus_size; }
                set {
                        _focus_size = value;
-                       focus_starts = new int[value+1];
+                       focus_info = new FocusInfo[_focus_size+2*steps+1];
                        focus_layout_needed = true;
                        queue_draw();
                }
@@ -67,7 +80,7 @@ public class FisheyeList : Gtk.DrawingArea
        //public virtual signal void cursor_changed ();
        public signal void row_activated(Gtk.TreePath path);
 
-       public FisheyeList()
+       public FisheyeListView()
        {
                model = null;
                backing_store = null;
@@ -75,9 +88,10 @@ public class FisheyeList : Gtk.DrawingArea
                label_cache = null;
 
                renderers = new Gtk.CellRendererText[steps];
+               renderer_sizes = new int[steps];
 
                // Defaults for properties
-               focus_size = 21;
+               focus_size = 10;
                label_column = 0;
 
                cur_el = 0;
@@ -127,7 +141,7 @@ public class FisheyeList : Gtk.DrawingArea
        /* Mouse button got released */
        public override bool button_release_event(Gdk.EventButton event)
        {
-               stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
+               //stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
 
                // Emit row_activated if applicable
                if (model != null)
@@ -149,21 +163,26 @@ public class FisheyeList : Gtk.DrawingArea
                int x = (int)event.x;
                int y = (int)event.y;
 
-               focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
-
-               if (focus_locked)
+               if (is_fisheye)
                {
-                       for (int idx = focus_first; idx < focus_end; ++idx)
-                               if (y < focus_starts[idx-focus_first+1])
-                               {
-                                       cur_el = idx;
-                                       break;
-                               }
+                       focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_area_start+focus_info[0].y && y < focus_area_start+focus_info[focus_end - focus_first].y;
 
-               } else {
-                       cur_el = y * label_cache.length / allocation.height;
-                       if (old_cur_el != cur_el)
+                       if (y < focus_area_start+focus_info[0].y || y >= focus_area_start+focus_info[focus_end - focus_first].y)
+                               cur_el = y * (label_cache.length+1) / allocation.height;
+                       else
+                               for (int idx = 0; idx < focus_info.length; ++idx)
+                                       if (y - focus_area_start < focus_info[idx].y + renderer_sizes[focus_info[idx].renderer])
+                                       {
+                                               cur_el = idx + focus_first;
+                                               break;
+                                       }
+
+                       if (!focus_locked && old_cur_el != cur_el)
                                focus_layout_needed = true;
+               } else {
+                       cur_el = y / max_renderer_size;
+                       if (cur_el >= label_cache.length)
+                               cur_el = label_cache.length - 1;
                }
 
                //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
@@ -206,14 +225,6 @@ public class FisheyeList : Gtk.DrawingArea
                base_layout_needed = true;
        }
 
-       protected int el_y(int idx)
-       {
-               // Distorted position
-               int pos = fisheye(idx);
-               //stderr.printf("%d %f %f\n", idx, undy, pos);
-               return pos;
-       }
-
        protected Gtk.CellRendererText make_cell_renderer()
        {
                var res = new Gtk.CellRendererText();
@@ -224,24 +235,7 @@ public class FisheyeList : Gtk.DrawingArea
 
        protected void base_layout()
        {
-               background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
-               backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
-
-               // Recreate the renderers
-               renderers[renderers.length-1] = make_cell_renderer();
-               renderers[renderers.length-1].set_fixed_height_from_font(1);
-
-               renderers[0] = make_cell_renderer();
-               renderers[0].size = Pango.SCALE;
-               renderers[0].set_fixed_height_from_font(1);
-
-               for (int i = 1; i < renderers.length-1; ++i)
-               {
-                       renderers[i] = make_cell_renderer();
-                       renderers[i].scale = (double)i / renderers.length;
-                       renderers[i].set_fixed_height_from_font(1);
-               }
-
+               // Rebuild label cache
                if (model == null)
                {
                        label_cache = new string[0];
@@ -266,6 +260,39 @@ public class FisheyeList : Gtk.DrawingArea
                        }
                }
 
+               background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
+               backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
+
+               // Recreate the renderers
+               renderers[renderers.length-1] = make_cell_renderer();
+               renderers[renderers.length-1].set_fixed_height_from_font(1);
+               renderers[renderers.length-1].get_size(this, null, null, null, null, out max_renderer_size);
+               renderer_sizes[renderers.length-1] = max_renderer_size;
+
+               is_fisheye = label_cache.length * max_renderer_size > allocation.height;
+
+               if (is_fisheye)
+               {
+                       renderers[0] = make_cell_renderer();
+                       renderers[0].size = Pango.SCALE;
+                       renderers[0].set_fixed_height_from_font(1);
+                       int min_renderer_size;
+                       renderers[0].get_size(this, null, null, null, null, out min_renderer_size);
+                       renderer_sizes[0] = min_renderer_size;
+
+                       focus_step_size = 0;    // Size of the diminishing area
+                       for (int i = 1; i < renderers.length-1; ++i)
+                       {
+                               renderers[i] = make_cell_renderer();
+                               renderers[i].scale = (double)i / renderers.length;
+                               renderers[i].set_fixed_height_from_font(1);
+                               int size;
+                               renderers[i].get_size(this, null, null, null, null, out size);
+                               renderer_sizes[i] = size;
+                               focus_step_size += size;
+                       }
+               }
+
                // Draw background
                draw_background(background);
 
@@ -273,34 +300,72 @@ public class FisheyeList : Gtk.DrawingArea
                focus_layout_needed = true;
        }
 
+       protected int el_renderer(int idx)
+       {
+               int fs2 = _focus_size/2;
+               int renderer_idx;
+               if (idx < cur_el)
+                       renderer_idx = idx - (cur_el-fs2-steps);
+               else
+                       renderer_idx = (cur_el+fs2+steps) - idx;
+               if (renderer_idx < 0)
+                       return 0;
+               if (renderer_idx >= renderer_sizes.length)
+                       return renderer_sizes.length-1;
+               return renderer_idx;
+       }
+
        protected void focus_layout()
        {
-               if (label_cache.length == 0)
+               if (!is_fisheye || label_cache.length == 0)
                {
                        focus_first = 0;
                        focus_end = 0;
-                       focus_starts[0] = 0;
-               } else {
-                       if (_focus_size >= label_cache.length)
-                       {
-                               focus_first = 0;
-                               focus_end = label_cache.length;
-                       } else {
-                               focus_first = cur_el -_focus_size/2;
-                               if (focus_first < 0) focus_first = 0;
-
-                               focus_end = focus_first + _focus_size;
-                               if (focus_end > label_cache.length)
-                               {
-                                       focus_end = label_cache.length;
-                                       focus_first = focus_end - _focus_size;
-                               }
-                       }
+                       focus_layout_needed = false;
+                       return;
+               }
 
-                       // Compute starting positions for all items in focus
-                       for (int idx = focus_first; idx <= focus_end; ++idx)
-                               focus_starts[idx - focus_first] = el_y(idx);
+               focus_first = cur_el - _focus_size/2 - steps;
+               if (focus_first < 0) focus_first = 0;
+               if (focus_first + focus_info.length > label_cache.length)
+                       focus_first = label_cache.length - focus_info.length;
+
+               /*
+               int full_focus_start = anchor_y - (_focus_size/2) * max_renderer_size;
+               int full_focus_end = anchor_y + (_focus_size/2) * max_renderer_size;
+               int steps_start = full_focus_start - focus_step_size;
+               */
+
+               int cur_pos = 0;
+               int cur_idx = 0;
+               int focus_area_pre = 0;
+               while (cur_pos < allocation.height && cur_idx < focus_info.length)
+               {
+                       if (focus_first + cur_idx == cur_el)
+                               focus_area_pre = cur_pos;
+                       focus_info[cur_idx].y = cur_pos;
+                       focus_info[cur_idx].renderer = el_renderer(focus_first + cur_idx);
+//                     stderr.printf("LAYOUT %d+[0-%d-%d] item %d/%d: pos %d rend %d rsz %d\n",
+//                             focus_first, cur_idx, focus_info.length, focus_first + cur_idx, label_cache.length,
+//                             cur_pos, focus_info[cur_idx].renderer, renderer_sizes[focus_info[cur_idx].renderer]);
+                       cur_pos += renderer_sizes[focus_info[cur_idx].renderer];
+                       ++cur_idx;
                }
+
+               focus_info[cur_idx].y = cur_pos;
+               focus_info[cur_idx].renderer = 0;
+               focus_end = focus_first + cur_idx;
+
+               int anchor_y = cur_el * allocation.height / (label_cache.length+1);
+               int focus_area_size = cur_pos;
+
+               focus_area_start = anchor_y - focus_area_pre;
+               if (focus_area_start < 0) focus_area_start = 0;
+               if (focus_area_start + focus_area_size > allocation.height)
+                       focus_area_start = allocation.height - focus_area_size;
+
+//             stderr.printf("FA [0 [%d+%d=%d] %d]\n", focus_area_start, focus_area_size, focus_area_start+focus_area_size, allocation.height);
+
                focus_layout_needed = false;
        }
 
@@ -311,10 +376,23 @@ public class FisheyeList : Gtk.DrawingArea
                // Background
                drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
 
+               if (!is_fisheye)
+                       return;
+
                // Focus movement area
                drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
                        allocation.width/2, 0, allocation.width, allocation.height);
 
+               for (int y = 0; y < allocation.height/2 - 30; y += 30)
+               {
+                       Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
+                               null, this, null, Gtk.ArrowType.UP, false,
+                               allocation.width/2, y, allocation.width/2, 10);
+                       Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
+                               null, this, null, Gtk.ArrowType.DOWN, false,
+                               allocation.width/2, allocation.height-y-30, allocation.width/2, 10);
+               }
+
                Gdk.Rectangle expose_area = Gdk.Rectangle();
                expose_area.x = expose_area.y = 0;
                expose_area.width = allocation.width;
@@ -323,10 +401,10 @@ public class FisheyeList : Gtk.DrawingArea
                Gdk.Rectangle cell_area = Gdk.Rectangle();
                cell_area.x = 0;
                cell_area.width = allocation.width;
-               renderers[0].get_size(this, null, null, null, null, out cell_area.height);
+               cell_area.height = renderer_sizes[0];
                if (label_cache.length * cell_area.height >= allocation.height)
                {
-                       for (int y = 0; y < allocation.height; ++y)
+                       for (int y = 0; y < allocation.height; y += cell_area.height)
                        {
                                int idx = y * label_cache.length / allocation.height;
                                cell_area.y = y;
@@ -381,46 +459,69 @@ public class FisheyeList : Gtk.DrawingArea
                        0, 0, 0, 0,
                        allocation.width, allocation.height);
 
-               // Focus lock area
-               drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
-                       0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]-focus_starts[0]);
-
-               // Paint items around focus
-               Gdk.Rectangle cell_area = Gdk.Rectangle();
-               cell_area.x = 0;
-               cell_area.width = allocation.width;
-               for (int idx = 0; idx < focus_end-focus_first; ++idx)
+               if (is_fisheye)
                {
-                       int y0 = focus_starts[idx];
-                       int y1 = focus_starts[idx + 1];
-                       cell_area.y = y0;
-                       cell_area.height = y1-y0;
+                       // Focus lock area
+                       drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
+                               0, focus_area_start + focus_info[0].y, allocation.width/2, focus_info[focus_end - focus_first].y);
+
+                       // Paint items around focus
+                       Gdk.Rectangle cell_area = Gdk.Rectangle();
+                       cell_area.x = 0;
+                       cell_area.width = allocation.width;
+                       for (int idx = 0; idx < focus_info.length; ++idx)
+                       {
+                               var renderer = renderers[focus_info[idx].renderer];
+                               cell_area.y = focus_area_start + focus_info[idx].y;
+                               cell_area.height = renderer_sizes[focus_info[idx].renderer];
+
+                               Gtk.CellRendererState rflags = 0;
+                               if (idx + focus_first == cur_el)
+                               {
+                                       rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
+                                       drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
+                                                       cell_area.x, cell_area.y, cell_area.width, cell_area.height);
+                               }
+                       
+                               renderer.text = label_cache[idx + focus_first];
+                               renderer.render((Gdk.Window*)drawable, this, 
+                                               cell_area,
+                                               cell_area,
+                                               expose_area,
+                                               rflags);
 
-                       Gtk.CellRendererState rflags = 0;
-                       if (idx + focus_first == cur_el)
+                               //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
+                       }
+               } else {
+                       // Paint all items sequentially
+                       var renderer = renderers[renderers.length-1];
+                       Gdk.Rectangle cell_area = Gdk.Rectangle();
+                       cell_area.x = 0;
+                       cell_area.width = allocation.width;
+                       cell_area.height = max_renderer_size;
+                       for (int idx = 0; idx < label_cache.length; ++idx)
                        {
-                               rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
-                               drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
-                                               cell_area.x, cell_area.y, cell_area.width, cell_area.height);
+                               cell_area.y = idx * cell_area.height;
+
+                               Gtk.CellRendererState rflags = 0;
+                               if (idx == cur_el)
+                               {
+                                       rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
+                                       drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
+                                                       cell_area.x, cell_area.y, cell_area.width, cell_area.height);
+                               }
+
+                               renderer.text = label_cache[idx];
+                               renderer.render((Gdk.Window*)drawable, this, 
+                                               cell_area,
+                                               cell_area,
+                                               expose_area,
+                                               rflags);
                        }
-               
-                       int size = (y1-y0);
-                       if (size <= 0) size = 1;
-                       if (size >= renderers.length) size = renderers.length - 1;
-                       renderers[size].text = label_cache[idx + focus_first];
-                       renderers[size].render((Gdk.Window*)drawable, this, 
-                                       cell_area,
-                                       cell_area,
-                                       expose_area,
-                                       rflags);
-
-                       //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
                }
        }
 
        /*
-        * The following function is adapted from Prefuse's FisheyeDistortion.java.
-        *
         * A relevant annotation from Prefuse:
         *
         * For more details on this form of transformation, see Manojit Sarkar and 
@@ -428,65 +529,33 @@ public class FisheyeList : Gtk.DrawingArea
         * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
         * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
         * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
+        *
+        * See also http://www.cs.umd.edu/hcil/fisheyemenu/
         */
-
-       /*
-        * Distorts an item's coordinate.
-        * @param x the undistorted coordinate
-        * @param coordinate of the anchor or focus point
-        * @param d disortion factor
-        * @param min the beginning of the display
-        * @param max the end of the display
-        * @return the distorted coordinate
-        */
-       private int fisheye(int idx)
-       {
-               // Autocompute distortion factor
-               // 20 is the pixel size of the item at centre of focus
-               double d = label_cache.length * 20 / allocation.height;
-               if ( d <= 1 )
-                       return idx * allocation.height / label_cache.length;
-
-               double a = (double)cur_el * allocation.height / label_cache.length;
-               double x = (double)idx * allocation.height / label_cache.length;
-               double max = (double)allocation.height;
-
-               if (idx < cur_el)
-               {
-                       double m = a;
-                       if ( m == 0 ) m = max;
-                       double v = (double)(a - x) / m;
-                       v = (double)(d+1)/(d+(1/v));
-                       return (int)Math.round(a - m*v);
-               } else {
-                       double m = max-a;
-                       if ( m == 0 ) m = max;
-                       double v = (double)(x - a) / m;
-                       v = (double)(d+1)/(d+(1/v));
-                       return (int)Math.round(a + m*v);
-               }
-       }
 }
 
 public class Fisheye : Gtk.Window
 {
+       Gtk.ListStore model;
+       FisheyeListView flv;
+
        public Fisheye()
        {
                title = "Fisheye";
                destroy += Gtk.main_quit;
 
-               var list = new FisheyeList();
-               add(list);
+               flv = new FisheyeListView();
+               add(flv);
 
-               var store = new Gtk.ListStore(1, typeof(string));
+               model = new Gtk.ListStore(1, typeof(string));
                Gtk.TreeIter iter;
                var infd = FileStream.open("/tmp/names", "r");
                if (infd == null)
                {
                        for (int i = 0; i < 300; ++i)
                        {
-                               store.append(out iter);
-                               store.set(iter, 0, "Antani %d".printf(i), -1);
+                               model.append(out iter);
+                               model.set(iter, 0, "Antani %d".printf(i), -1);
                        }
                } else {
                        char buf[255];
@@ -495,12 +564,24 @@ public class Fisheye : Gtk.Window
                                string line = infd.gets(buf);
                                if (line == null)
                                        break;
-                               store.append(out iter);
-                               store.set(iter, 0, line, -1);
+                               model.append(out iter);
+                               model.set(iter, 0, line, -1);
                        }
                }
 
-               list.set_model(store);
+               flv.set_model(model);
+
+               flv.row_activated += on_row_activated;
+               //stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
+       }
+
+       public void on_row_activated(Gtk.TreePath path)
+       {
+               Gtk.TreeIter iter;
+               model.get_iter(out iter, path);
+               string val;
+               model.get(iter, 0, out val, -1);
+               stdout.printf("Clicked on %s\n", val);
        }
 }