Re: [gtkmm] Glade-2, libgtkmm, Xml::create, callbacks



El Wed, Sep 03, 2003 at 09:32:09PM +0200, Christer Palm escribio:
> 
[...]
> 
> You have 3 different choices in how to use the tools;
> 
> 1: Use only glademm.
> 2: Use only libglademm.
> 3: Use glademm and libglademm together.
> 
[...]
> 
> Choice 2: You write the C++ code yourself. No C++ code whatsoever is 
> generated. You will need to write C++ code that 1) loads the XML file(s) 
> generated by glade (not glademm!), 2) implements your signal handlers, 
> and 3) connect your signal handlers to the user interface. All three is 
> usually a snap using the libglademm API - very little code needs to be 
> written. If you want to make adjustments to your user interface, you 
> only have to tweak the XML file, even after your program has been compiled.

Folowing the discusion, I think that libglademm users will be a lot
happier if what is already possible in C could be done in C++,
automatically connect signals from the generated UI to methods in
users code.

This can't currently be done because in C++ you need to pass an object
pointer as the first argument to the method, and inside libglademm the
correct object isn't known. Also, there is the problem of name mangling,
but it seems that it could be solved.

Now, I think that a better way could be to extend libglademm, adding
a new way to connect signals to methods.

Imagine that the folowing code could work:

-----------------------------------------------------
class myClass : public Gnome::Glade::Connector
{
   /* My fields */
   int a,b;

   /* My methods */
   public:
     void buttonClicked();
};

myClass::buttonClicked()
{
   std::cout << "Hello, a=" << a << " b=" << b << "\n";
}

main()
{
  /* Loads glade XML, etc.. */
  Glib::RefPtr<Gnome::Glade::Xml> refXml =
          Gnome::Glade::Xml::create("my.glade");
  /* Instantiates myClass oject */
  myClass obj("obj_name_used_in_glade_file");
  
  /* Connect */
  refXml.Connect();

  /* etcetera */
  
}
-----------------------------------------------------

The idea is that the constructor could register the object pointer,
object name and class name in a global (ugly, but I think there is no
other way) list. Then, the Xml::Connect method can lookup names of the
form "object::method", first lookup object in list, then construct
the mangled method name using the class name, method name and method
signature, get's the method pointer using the same algorithm used
by libglade and finally made a trmapoline to dispatch the signal
to the correct method.

Could work, I'll investigate it further tonight :-)


    Daniel.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]