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

Re: destroying widgets on C++



Germano Leichsenring wrote:
> 
> I have a class which have a window in it; I want the class to be destroyed
> when the widget is destroyed, and the widget be destroyed when the class is
> destroyed. I've been trying it for a while, but can't find a solution that
> works. Has anybody done that?

> the problem on this is:
> - if I destroy the class, the widget is destroyed;
> - but if I destroy the widget, i'm destroying the widget 2 times.

Here's my solution:

class my_class {
public:

GtkWidget *window;  
 
static void my_destroy(GtkWidget *wi, gpointer d);

my_class() {
	window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
      	gtk_signal_connect(GTK_OBJECT(window),"destroy",
				 GTK_SIGNAL_FUNC(my_class::my_destroy),
				 this);	
    	gtk_widget_show(window);
}

~my_class() {
    	if (window) {
 		GtkWidget * tmp=window;
      		window=NULL;
      		gtk_widget_destroy(tmp);
    	}
   	g_print("widget deleted\n");
  }
};


void my_class::my_destroy(GtkWidget *wi, gpointer d) {
	my_class *w=d;
	if(w->window) {
    		w->window=NULL;
    		delete w;
  	}
  	g_print("object destroyed\n");
}

My idea is : if one of the destroyers (~my_class
and my_destroy) notices that window is set to NULL
in the object, it assumes that the other one is
doing the call.

If soemone knows a better way.

Eric.



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