Re: How to create a new Window?



On Sun, Dec 11, 2005 at 06:07:05PM -0600, MERT TUGCU wrote:
> Hi,
> 
> I am using gtkmm-2.0. I looked into the documentation but I couldn't find 
> the answers for couple of things.
> 
> I've created a class that derives Gtk::Window and named it as 
> SomeInterface. I put all the interface elemets (buttons labels, etc).
> In the top-level window, I have a button to pop-up this new window. The 
> button callback function is like:
> 
> void TopLevelWindow::on_button_showSomeInterface()
> {
> SomeInterface* SomeInterfaceObject = new SomeInterface();
> 	SomeInterface->show();
> }
> 
> Now, this creates the window, but I don't know how to get the number of 
> windows created and how to delete them upon the program exit.  

This is really a C++ question.  You either need to make use of Glib::RefPtr
or do all memory management directly, which means creating an STL container
within TopLevelWindow to which all the pointers are stored so you could
deallocate all the memory by looping through that container.  Using smart
pointers, like RefPtr, may get very very tricky if your code is multithreaded,
so I, personally, would memory-manage by hand.

Btw, your code above will cause a memory leak, since you create a window,
show it, and drop immediately on the ground.  Btw(2), shouldn't show() be
replaced with show_all() (see docs)?

> At this 
> point, my questions are: Are the instances automatically deleted from the 
> memory when the user clicks the quit button located at top right (X button) 

No, you should delete them from within your own code, as per explanation above.
You may want, however, to explore the Gtk::manage(Gtk::Widget*) feature.  See
http://www.gtkmm.org/docs/gtkmm-2.4/docs/FAQ/html/index.html#id2511146.  Unfortunately,
you can't pass Gtk::Window* to Gtk::manage, as windows are not hierarchically organized,
as far as GTK is concerned.

> How can I know the number of 
> instaced created?

Check the size of the container above.

> Also, how can I capture the quit signal event when quit 
> button is clicked?

... by overriding the 'virtual bool Window::on_delete_event(GdkEventAny*);'
callback in your own Window class (most likely TopLevelWindow, in your particular
case).



Regards,

Nickolai



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