Re: [gtkmm] Automatically destroying a dialog



> Marc Canaleta wrote:
>
> >I want a dialog to be deleted when it is closed (similar to widgets
> >created with manage()). The problem is that it isn't modal and I can
> >have various of them simultanelly, so I must create it using new, and
I
> >don't know where to delete it when it is closed.
>
> You can override the virtual
Gtk::Widget::on_delete_event(GdkEventAny*)
> method in your dialog class, and have it delete itself:
>
> bool my_dialog::on_delete_event(GdkEventAny*)
> {
>       delete this;
>       return true;
> }
>
> .... note that you are probably going to want to handle key events
such
> as "Escape" also, and that you will have to ensure that your dialog
> class instances don't outlive their data.  In K-3D we have several
> dialogs that use this technique to manage their own lifetimes; they
all
> front for data contained within a "document" class that represents an
> open user document.  The document class has a signal that is emitted
> when the document is closed, and the dialogs connect to that signal,
> deleting themselves when it is received.

I had the same problem and I solved it with using Glib::Dispatcher.If a
dialog
wants to delete themself it calls the
DlgDestroyer::destroy(Gtk::Dialog *) member function. This function
doesn't deletes the dialog only adds the pointer to destList, and sends
a signal accross the dispatcher. After that the destroy_callback
function deletes the dialog(s), that are in the destList. This function
called from the main loop, and not from a member function of the dialog.

I attached my sources.

Gabor
/***************************************************************************
                          dlgdestroyer.cpp  -  description
                             -------------------
    begin                : h jún 28 2004
    copyright            : (C) 2004 by Hamar Gábor
    email                : ghamar freemail hu
 ***************************************************************************/

#include "dlgdestroyer.h"

DlgDestroyer::DlgDestroyer(){
  connect(SigC::slot(*this,&DlgDestroyer::destroy_callback));
}
DlgDestroyer::~DlgDestroyer(){
}


void DlgDestroyer::destroy_callback(){
  while(!destList.empty()) {
    delete destList.front();
    destList.pop_front();
  }
}
/***************************************************************************
                          dlgdestroyer.h  -  description
                             -------------------
    begin                : h jún 28 2004
    copyright            : (C) 2004 by Hamar Gábor
    email                : ghamar freemail hu
 ***************************************************************************/

#ifndef DLGDESTROYER_H
#define DLGDESTROYER_H

#include <glibmm/dispatcher.h>
#include <gtkmm/dialog.h>

#include <list>


class DlgDestroyer : private Glib::Dispatcher, public SigC::Object  {
public: 
	DlgDestroyer();
	virtual ~DlgDestroyer();
  void destroy (Gtk::Dialog * dlg) { destList.push_back(dlg); emit(); }
private: // Private methods
  
  void destroy_callback();
private:
  
  std::list<Gtk::Dialog *> destList;
};


#endif


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