Int-only rendering
authorEnrico Zini <enrico@enricozini.org>
Thu, 10 Dec 2009 13:05:25 +0000 (13:05 +0000)
committerEnrico Zini <enrico@enricozini.org>
Thu, 10 Dec 2009 13:05:25 +0000 (13:05 +0000)
src/fisheye.vala

index c2e0b6bb71dd663e7228218a2fc6005bb64c2850..4efcadac44834b9cd17576b5edec1db98c04263c 100644 (file)
 
 using GLib;
 
+protected struct FocusInfo
+{
+       int y;
+       int renderer;
+}
+
+
 public class FisheyeList : Gtk.DrawingArea
 {
        protected Gtk.TreeModel model;
@@ -29,6 +36,7 @@ public class FisheyeList : Gtk.DrawingArea
        // Pango layouts cached for speed
        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
@@ -40,17 +48,19 @@ 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;
 
+       // 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();
                }
@@ -77,9 +87,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;
@@ -153,22 +164,20 @@ public class FisheyeList : Gtk.DrawingArea
 
                if (is_fisheye)
                {
-                       focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
+                       focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_info[0].y && y < focus_info[focus_end - focus_first].y;
 
-                       if (focus_locked)
-                       {
+                       if (y < focus_info[0].y || y >= focus_info[focus_end - focus_first].y)
+                               cur_el = y * (label_cache.length+1) / allocation.height;
+                       else
                                for (int idx = focus_first; idx < focus_end; ++idx)
-                                       if (y < focus_starts[idx-focus_first+1])
+                                       if (y < focus_info[idx-focus_first+1].y)
                                        {
                                                cur_el = idx;
                                                break;
                                        }
 
-                       } else {
-                               cur_el = y * label_cache.length / allocation.height;
-                               if (old_cur_el != cur_el)
-                                       focus_layout_needed = true;
-                       }
+                       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)
@@ -265,6 +274,7 @@ public class FisheyeList : Gtk.DrawingArea
                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;
 
@@ -273,8 +283,11 @@ public class FisheyeList : Gtk.DrawingArea
                        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;
 
-                       int step_size = 0;      // Size of the diminishing area
+                       focus_step_size = 0;    // Size of the diminishing area
                        for (int i = 1; i < renderers.length-1; ++i)
                        {
                                renderers[i] = make_cell_renderer();
@@ -282,12 +295,9 @@ public class FisheyeList : Gtk.DrawingArea
                                renderers[i].set_fixed_height_from_font(1);
                                int size;
                                renderers[i].get_size(this, null, null, null, null, out size);
-                               step_size += size;
+                               renderer_sizes[i] = size;
+                               focus_step_size += size;
                        }
-
-                       //int focus_lines_count = allocation.height / (2 * max_renderer_size);
-                       //int focus_area_size = 2 * step_size + focus_lines_count * max_renderer_size;
-                       //int focus_area_start = pos(cur_el) - focus_lines_count*max_renderer_size/2 - step_size;
                }
 
                // Draw background
@@ -297,34 +307,61 @@ 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;
+               }
+
+               int focus_area_size = 2 * focus_step_size + _focus_size * max_renderer_size;
+               int anchor_y = cur_el * allocation.height / (label_cache.length+1);
+               int focus_area_start = anchor_y - (focus_area_size - max_renderer_size) / 2;
+               if (focus_area_start < 0) focus_area_start = 0;
 
-                       // 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;
+
+               /*
+               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 = focus_area_start;
+               int cur_idx = focus_first;
+               while (cur_pos < allocation.height && cur_idx < label_cache.length && cur_idx - focus_first < focus_info.length)
+               {
+                       focus_info[cur_idx - focus_first].y = cur_pos;
+                       focus_info[cur_idx - focus_first].renderer = el_renderer(cur_idx);
+//                     stderr.printf("LAYOUT [%d-%d-?] at %d/%d: pos %d rend %d rsz %d\n",
+//                             focus_first, cur_idx, cur_idx - focus_first, focus_info.length, cur_pos, focus_info[cur_idx - focus_first].renderer, renderer_sizes[focus_info[cur_idx - focus_first].renderer]);
+                       cur_pos += renderer_sizes[focus_info[cur_idx - focus_first].renderer];
+                       ++cur_idx;
                }
+
+               focus_end = cur_idx;
+               focus_info[focus_end - focus_first].y = cur_pos;
+               focus_info[focus_end - focus_first].renderer = 0;
+
                focus_layout_needed = false;
        }
 
@@ -360,7 +397,7 @@ 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 += cell_area.height)
@@ -422,7 +459,7 @@ public class FisheyeList : Gtk.DrawingArea
                {
                        // 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]);
+                               0, focus_info[0].y, allocation.width/2, focus_info[focus_end - focus_first].y-focus_info[0].y);
 
                        // Paint items around focus
                        Gdk.Rectangle cell_area = Gdk.Rectangle();
@@ -430,10 +467,9 @@ public class FisheyeList : Gtk.DrawingArea
                        cell_area.width = allocation.width;
                        for (int idx = 0; idx < focus_end-focus_first; ++idx)
                        {
-                               int y0 = focus_starts[idx];
-                               int y1 = focus_starts[idx + 1];
-                               cell_area.y = y0;
-                               cell_area.height = y1-y0;
+                               var renderer = renderers[focus_info[idx].renderer];
+                               cell_area.y = focus_info[idx].y;
+                               cell_area.height = renderer_sizes[focus_info[idx].renderer];
 
                                Gtk.CellRendererState rflags = 0;
                                if (idx + focus_first == cur_el)
@@ -443,11 +479,8 @@ public class FisheyeList : Gtk.DrawingArea
                                                        cell_area.x, cell_area.y, cell_area.width, cell_area.height);
                                }
                        
-                               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, 
+                               renderer.text = label_cache[idx + focus_first];
+                               renderer.render((Gdk.Window*)drawable, this, 
                                                cell_area,
                                                cell_area,
                                                expose_area,