use warnings;
use strict;
use Test::More tests => 5;
use Glib qw(TRUE FALSE);
use Gtk2 -init;

ok( my $dialog = Foo::Dialog->new, 'Created dialog' );

$dialog->signal_emit('destroy');
require Scalar::Util;
Scalar::Util::weaken($dialog);
is( $dialog, undef, 'destroyed' );

$dialog = Foo::Dialog->new;
$dialog->signal_emit( 'delete_event', undef );
Scalar::Util::weaken($dialog);
is( $dialog, undef, 'destroyed on delete_event' );

$dialog = Foo::Dialog->new( destroy => FALSE );
$dialog->signal_emit('destroy');
Scalar::Util::weaken($dialog);
isnt( $dialog, undef, 'hidden on destroy signal' );

$dialog->signal_emit( 'delete_event', undef );
isnt( $dialog, undef, 'hidden on delete_event' );

#########################

package Foo::Dialog;

use warnings;
use strict;
use Gtk2;
use Carp;
use Glib 1.220 qw(TRUE FALSE);

use Glib::Object::Subclass Gtk2::Window::,
  signals => {
 delete_event    => \&on_delete_event,
 destroy         => \&on_destroy,
  },
  properties => [
 Glib::ParamSpec->boolean(
  'destroy',                                                       # name
  'Destroy',                                                       # nickname
  'Whether to destroy or hide the dialog when it is dismissed',    # blurb
  TRUE,                                                            # default
  [qw/readable writable/]                                          # flags
 ),
  ];

sub on_delete_event {
 my ( $widget, $event ) = @_;
 unless ( $widget->get('destroy') ) {
  $widget->hide;
  return Gtk2::EVENT_STOP;    # ensures that the window is not destroyed
 }
 $widget->signal_chain_from_overridden($event);
 return Gtk2::EVENT_PROPAGATE;
}

sub on_destroy {
 my ( $widget, $event ) = @_;
 if ( $widget->get('destroy') ) {
  $widget->signal_chain_from_overridden;
  return Gtk2::EVENT_PROPAGATE;
 }
 return Gtk2::EVENT_STOP;     # ensures that the window is not destroyed
}
