1

I'm trying to make a custom widget that resembles the "quick search" entry that Gtk uses on all TreeView-like widgets.

Here's a simplified example of my initial idea:

from gi.repository import Gtk

class QuickSearch(Gtk.Bin):
    def __init__(self, *args, **kwargs):
        super(QuickSearch, self).__init__(*args, **kwargs)
        self.add(Gtk.Entry())

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
search = QuickSearch()
win.add(search)
win.show_all()
Gtk.main()

The problems are the following:

  • If I use it alone, as in this example, there seems to be space allocated to the custom widget, but the GtkEntry is not shown on the screen. I tried various combinations of properties on the GtkEntry and the custom widget (hexpand, vexpand, margins, aligns, etc) with no avail.
  • If combined with other widgets, when I interactively query for the widget allocated height, it aparently is 1. On the screen, the widget effectively doesn't appear (since it's somewhat "squashed" between the other widgets).

So, there's something I'm missing on the object initialization or how I'm using this custom widget? Is this a problem specific to GtkBin? I tried doing the same with a GtkBox and works flawlesly, but I think GtkBin is better suited for this particular widget.

liberforce
  • 11,189
  • 37
  • 48
asermax
  • 3,053
  • 2
  • 23
  • 28
  • Why not just subclass `Gtk.Entry`? – ptomato Jan 10 '13 at 20:57
  • The idea is that the widget haves a "border", the same as the mentioned quick search widget used in nautilus (Like [this](https://dl.dropbox.com/u/36017964/images/nautilus.png)). The other option is to draw that extra border myself, but given that case, I'll just extend GtkBox. The thing is that GtkBin was created to be extended like this, so the point in my question is to figure out *why* it doesn't work as intended. – asermax Jan 10 '13 at 22:12
  • In that case, why not look at how Nautilus implements that search widget? – ptomato Jan 11 '13 at 13:43
  • That's beyond the point, I know how to implement it, and it's working with a GtkBox. The point of the question is **why I can't use GtkBin**. I would like to use that widget since its ideal for my implementation. I don't know what else I can say to make it clearer. – asermax Jan 11 '13 at 23:10

1 Answers1

3

For some reason, Gtk.Bin is not allocating space to its child. If you implement the do_size_allocate virtual method then it works. Why the default implementation is not being called, I have no idea.

from gi.repository import Gtk, Gdk

class QuickSearch(Gtk.Bin):
    def __init__(self, *args, **kwargs):
        super(QuickSearch, self).__init__(*args, **kwargs)
        self.add(Gtk.Entry())

    def do_size_allocate(self, allocation):
        self.set_allocation(allocation)
        child_allocation = Gdk.Rectangle()
        border_width = self.get_border_width()
        child_allocation.x = allocation.x + border_width
        child_allocation.y = allocation.y + border_width
        child_allocation.width = allocation.width - 2 * border_width
        child_allocation.height = allocation.height - 2 * border_width
        self.get_child().size_allocate(child_allocation)

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
search = QuickSearch()
win.add(search)
win.show_all()
Gtk.main()

However, I think inheriting from a Gtk.Frame is more appropriate for your purpose; it is a one-child container with a styleable border around it, which is what you want and more. And its size allocate method works.

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • Thank you for the answer! I'll try it out(both this workaround and using GtkFrame) and see what works better in my case :3 – asermax Jan 12 '13 at 16:06