#!/usr/bin/python

import sys

from gi.repository import GObject
from gi.repository import Gtk

ui1 = """
<interface>
  <object class="GtkWindow" id="window">
    <child>
      <object class="GtkEntry" id="entry">
        <property name="visible">True</property>
        <signal name="activate" handler="cb" swapped="no"/>
      </object>
    </child>
  </object>
</interface>
"""

ui2 = """
<interface>
  <object class="GtkWindow" id="window">
    <child>
      <object class="GtkTreeView" id="treeview">
        <property name="visible">True</property>
        <child internal-child="selection">
          <object class="GtkTreeSelection" id="selection">
            <signal name="changed" handler="cb" swapped="no"/>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>
"""


class mygobj(object):
    def __init__(self, uistr):
        self.builder = Gtk.Builder()
        self.builder.add_from_string(uistr)
        self.topwin = self.builder.get_object("window")

    def cb(self, *args):
        pass

    def __del__(self):
        print "delete", self

    def register_signals(self):
        self.builder.connect_signals({"cb": self.cb})



g1 = mygobj(ui1)
child = g1.builder.get_object("entry")
print "g1 initial"
print sys.getrefcount(g1)
g1.register_signals()
print "g1 after connect"
print sys.getrefcount(g1)
g1.topwin.destroy()
print "g1 after destroy"
print sys.getrefcount(g1)
child = None
g1 = None

print
print

g2 = mygobj(ui2)
child = g2.builder.get_object("treeview").get_selection()
print "g2 initial"
print sys.getrefcount(g2)
g2.register_signals()
print "g2 after connect"
print sys.getrefcount(g2)
g2.topwin.destroy()
print "g2 after destroy"
print sys.getrefcount(g2)
child = None
g2 = None


print
print "exiting"
sys.exit(0)
