Use CellRendererText
authorEnrico Zini <enrico@enricozini.org>
Thu, 10 Dec 2009 09:38:46 +0000 (09:38 +0000)
committerEnrico Zini <enrico@enricozini.org>
Thu, 10 Dec 2009 09:38:46 +0000 (09:38 +0000)
src/fisheye.vala

index 489ca9bddf72f744351611aa6d09b6dfaa439345..1244ef9803e60c6988abc947c226a770da54cbbe 100644 (file)
@@ -28,7 +28,7 @@ public class FisheyeList : Gtk.DrawingArea
 
        // Pango layouts cached for speed
        protected const int max_font_size = 30;
-       protected Pango.Layout[] pango_cache;
+       protected Gtk.CellRendererText[] renderers;
 
        // Labels to show, extracted from the model
        protected string[] label_cache;
@@ -74,9 +74,7 @@ public class FisheyeList : Gtk.DrawingArea
 
                label_cache = null;
 
-               pango_cache = new Pango.Layout[max_font_size];
-               for (int i = 0; i < pango_cache.length; ++i)
-                       pango_cache[i] = null;
+               renderers = new Gtk.CellRendererText[max_font_size];
 
                // Defaults for properties
                focus_size = 21;
@@ -201,15 +199,11 @@ public class FisheyeList : Gtk.DrawingArea
 
        public override void style_set(Gtk.Style? previous_style)
        {
-               // Reset the pango cache if the pango context changes
-               for (int i = 0; i < pango_cache.length; ++i)
-                       pango_cache[i] = null;
+               base_layout_needed = true;
        }
        public override void direction_changed(Gtk.TextDirection previous_direction)
        {
-               // Reset the pango cache if the pango context changes
-               for (int i = 0; i < pango_cache.length; ++i)
-                       pango_cache[i] = null;
+               base_layout_needed = true;
        }
 
        protected int el_y(int idx)
@@ -232,6 +226,14 @@ public class FisheyeList : Gtk.DrawingArea
 
                //int min_size = max_font_size * 3 + FIXME;
 
+               // Recreate the renderers
+               for (int i = 0; i < renderers.length; ++i)
+               {
+                       renderers[i] = new Gtk.CellRendererText();
+                       renderers[i].scale = (double)i / renderers.length;
+                       renderers[i].set_fixed_height_from_font(1);
+               }
+
                if (model == null)
                {
                        label_cache = new string[0];
@@ -312,6 +314,10 @@ public class FisheyeList : Gtk.DrawingArea
 
                var drawable = backing_store;
                 Gtk.Style style = get_style();
+               Gdk.Rectangle expose_area = Gdk.Rectangle();
+               expose_area.x = expose_area.y = 0;
+               expose_area.width = allocation.width;
+               expose_area.height = allocation.height;
 
                // Background
                drawable.draw_drawable(
@@ -325,34 +331,33 @@ public class FisheyeList : Gtk.DrawingArea
                        allocation.width/2, 0, allocation.width, allocation.height);
 
                // 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)
                {
                        int y0 = focus_starts[idx];
                        int y1 = focus_starts[idx + 1];
+                       cell_area.y = y0;
+                       cell_area.height = y1-y0;
 
-                       Gtk.StateType itemState = Gtk.StateType.NORMAL;
+                       Gtk.CellRendererState rflags = 0;
                        if (idx + focus_first == cur_el)
                        {
-                               itemState = Gtk.StateType.SELECTED;
-                               drawable.draw_rectangle(style.bg_gc[itemState], true,
-                                       0, y0, allocation.width, y1-y0);
+                               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);
                        }
-
                
-                       int size = (y1-y0)*80/100;
+                       int size = (y1-y0);
                        if (size <= 0) size = 1;
-                       if (size >= pango_cache.length) size = pango_cache.length - 1;
-                       if (pango_cache[size] == null)
-                       {
-                               var fd = style.font_desc.copy();
-                               fd.set_absolute_size(size*Pango.SCALE);
-                               var pc = create_pango_context();
-                               pc.set_font_description(fd);
-                               pango_cache[size] = new Pango.Layout(pc);
-                       }
-                       pango_cache[size].set_text(label_cache[idx + focus_first], -1);
-                       var layout = pango_cache[size];
-                       Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
+                       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);
                }