Can't preempt WM destroy event



I'm writing an application that is comprised of a main and a
subordinate top-level window.  A button-press on the main window
toggles between hiding and showing the subordinate window.

What I'm having trouble with is when the subordinate window is closed
using the window manager's "destroy" button, I'd like to preempt the
destruction of the subordinate window, and just hide it instead.

Following is the test program that I've been playing with, to be
compiled with: gcc -Wall `gtk-config --cflags --libs` hide-test.c 
Any advise would be greatly appreciated.

-- John Kodis.

#include <gtk/gtk.h>
#include <stdio.h>

static void on_button_press(GtkWidget *top, GdkEvent *ev, GtkWidget *sub)
{
  printf("on_button_press: button %d\n",
    ((GdkEventButton*)ev)->button);
  if (GTK_WIDGET_VISIBLE(sub))
    gtk_widget_hide(sub);
  else
    gtk_widget_show(sub);
}

static int on_sub_destroy(GtkWidget *sub, void *nil)
{
  printf("on_sub_destroy: hiding %p\n", sub);
  gtk_widget_hide(sub);
  return TRUE;
}

int main(int argc, char *argv[])
{
  GtkWidget *top, *sub, *label;

  gtk_init(&argc, &argv);

  top = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_show(top);
  label = gtk_label_new("TOP: Click me!");
  gtk_widget_show(label);
  gtk_container_add(GTK_CONTAINER(top), label);

  sub = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_show(sub);
  label = gtk_label_new("SUB: Destroy me!");
  gtk_widget_show(label);
  gtk_container_add(GTK_CONTAINER(sub), label);

  gtk_widget_add_events(top, GDK_BUTTON_PRESS_MASK);
  gtk_signal_connect(GTK_OBJECT(top),
    "button_press_event", GTK_SIGNAL_FUNC(on_button_press), sub);
  gtk_signal_connect(GTK_OBJECT(top),
    "destroy", (GtkSignalFunc)gtk_main_quit, NULL);

  gtk_signal_connect(GTK_OBJECT(sub),
    "destroy", (GtkSignalFunc)on_sub_destroy, NULL);

  printf("main: top=%p, sub=%p\n", top, sub);
  gtk_main();
  return 0;
}



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