Re: C++ call constructor via GtkItemFactoryEntry



William L. Thomson Jr. wrote:

Jumping in with both feet.

I have an app that each inner window is launched by the main app/window.
Each window has it's own class.

So far things are working the way I want them to for the most part.
However I need to deviate from how I am currently doing things.
Currently I call a function/method within the class directly from the
main GTK menu.

Ex:
GtkItemFactoryEntry menu_items[] = {
        {"/Manage/Invoices","<control>I",&ManageInvoices::displayWindow,0,null_gchar},
        {"/_Help",null_gchar,NULL,0,"<LastBranch>"},
        {"/Help/About",null_gchar,&About::displayWindow,0,null_gchar},
};

That works fine however I have a constructor to the class that does not
get called. I need the constructor/destructor to be called so DSO's can
be loaded/unloaded and my app work without a seg fault.

I could just put the DSO loading code in the displayWindow function, but
then I have to come up with a way to close out the DSO. Which currently
is handled by the destructor. I also am having problems with calling the
destructor.

So my questions are as follows.
1.) How can I call a C++ constructor from within my GTK menu without
having to wrap it in another C or C++ function?

I thought about doing an init() function like
void ManageInvoices::init() {
        ManageInvoices mi = new ManageInvoices();
        mi.displayWindow;
}

Although I am not sure that's a good way to go, not to mention the
additional codding. Is it OK to have an object create a new version of
itself. Like in the above example?
For menu_items above to work ManageInvoices::displayWindow has to be a static function, since C doesn't know about classes. Static class functions don't have a 'this' and don't know about construcors/destructors either. For a C function/struct like menu_items, I think you will have to write a static creation function, like 'init' to create an instance of your window. This is common practice in application Frameworks..

2.) How can I call my C++ destructor during window closing?

You don't call destructors directly. If the window is a transient window just create it on the stack and the destructor will be called when it goes out of scope. If it is created by a call to 'new' you will have to call 'delete', which will call your destructor. You can then destroy any GTK+ C resources from inside the window's destructor. So in response to a user action to close the window, call something like 'delete window' and this call the window destructor.

Of course I can place a button on my new window and have it call some
function that will call the destructor and close the window. Although if
someone closes the window via the window managers close/X button I need
to detect that and call the destructor.

GTK+ provides the mechanism to add a C function to a widget that will get called when a widget is destroyed. You can use this function to call 'delete'. This works no matter how the window is closed, by calling gtk_object_destroy() or by the window manager. (I can post some code if you need it.)

Neither my c'tors and d'tors take any arguments, and for some reason I
am thinking that a c'tor or d'tor is being called for the object/class
to exist when I see the window.

However I know that my c'tors and d'tors are not being called.

Further more I know I may not be doing things the best or proper way.
Currently I am still learning C/C++ and GTK in the process. So please be
kind in your responses if I am asking the obvious.


Regards Jeff Franks.




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