Re: Disable Alt+F4



Ooops I forgot:

  bool
  on_my_toplevel_delete_handler( GdkEventAny* G_GNUC_UNUSED )
  {
    /* do whatever you want to do when the user presses ALT+F4, clicks 'x' or whatever other means the user uses to close the window*/

    return true; // --> IMPORTANT <--
  }

  int main( int argc, char* argv[])
  {
    Gtk::Main kit (argc, argc);
    Gtk::Window my_toplevel ;
    // some other code to setup the app in between
    my_toplevel.signal_delete_event().connect( sigc::ptr_fun( &on_my_toplevel_delete_handler ), true /* important! */ );
    my_toplevel.show();
    kit.run();
  }

2009/12/14 Milosz Derezynski <internalerror gmail com>
That problem is simple to solve but a little difficult to explain and expand on depending on how your setup works.

If you use this mechanism in your app:

  Gtk::Main kit (argc, argc);
  Gtk::Window my_toplevel ;
  // some other code to setup the app in between
  kit.run( my_toplevel );

then the ALT+F4 will be handled by the Gtk::Main instance.

Let me expand on this:

What happens when you press ALT+F4 on a window, or click the 'x' in the titlebar, or use any other mechanism the window manager employs for "closing" a window is that you cause a "delete" event to be emitted for that particular window. That event in itself does nothing, but Gtk::Main run with a window as argument is coded so that it hides the window, possibly even destroys (unrefs?) the main window instance and exits the Gtk+ main loop.

If you don't do it like above, but like this:

  Gtk::Main kit (argc, argc);
  Gtk::Window my_toplevel ;
  // some other code to setup the app in between
  my_toplevel.show();
  kit.run();

you retain full control on what to do when a delete-event occurs. See this documentation: http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1Widget.html#aa97a4453ec12f7fce163353c2bcaf580

You have two ways of dealing with this and avoiding the window to be hidden or actually anything happening at all.

The first is, you derive from Gtk::Window and override on_delete_event:

  class MyOwnWindow : public Gtk::Window
  {
     protected:
     
         virtual bool
         on_delete_event( GdkEventAny* G_GNUC_UNUSED )
         {
            /* do whatever you want to do when the user presses ALT+F4, clicks 'x' or whatever other means the user uses to close the window*/

            return true; // important
         }
  };

The other way is to connect to delete_event(), but using true as the 2nd argument to connect() to indicate that you want it to be called before the default signal handlers will be called: (see http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1Widget.html#a6b266f24d662c145fa301ca0bd011716 )

  bool
  on_my_toplevel_delete_handler( GdkEventAny* G_GNUC_UNUSED )
  {
    /* do whatever you want to do when the user presses ALT+F4, clicks 'x' or whatever other means the user uses to close the window*/
  }

  int main( int argc, char* argv[])
  {
    Gtk::Main kit (argc, argc);
    Gtk::Window my_toplevel ;
    // some other code to setup the app in between
    my_toplevel.signal_delete_event().connect( sigc::ptr_fun( &on_my_toplevel_delete_handler ), true /* important! */ );
    my_toplevel.show();
    kit.run();
  }

That is basically it.

2009/12/11 Robert Hildebrandt <roberts_katz gmx de>

Hi,
I want to disable the Alt+F4 shortcut on a window which has an progressbar for an not interruptable process.
I know how to do it on Win32 - is there a also way to disable it with Gtmm?
lG Robert
_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list



--
Please note that according to the German law on data retention,
information on every electronic information exchange with me is
retained for a period of six months.
[Bitte beachten Sie, dass dem Gesetz zur Vorratsdatenspeicherung zufolge
jeder elektronische Kontakt mit mir sechs Monate lang gespeichert wird.]



--
Please note that according to the German law on data retention,
information on every electronic information exchange with me is
retained for a period of six months.
[Bitte beachten Sie, dass dem Gesetz zur Vorratsdatenspeicherung zufolge
jeder elektronische Kontakt mit mir sechs Monate lang gespeichert wird.]


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