Re: [gtk-list] Re: Message Box?




"Michael K. Johnson" <johnsonm@redhat.com> writes:
> If you've made it work, please tell me how.  I used functions with
> "grab" in their names, unsuccessfully.  I tried several different
> things, and none of them worked.

Looking at the grabbing code some more, I was clearly mistaken in
thinking that I got it working before. I was probably fooled into
thinking that gtk_grab_add did something similar to XtAddGrab.  Which
isn't true. gtk_grab_add just sends all events to the grab widget,
while XtAddGrab keeps track of a "modal cascade." 

It would probably be sufficient to add something simpler to gtk which
would just discard all events that didn't occur in the grab window or
its children.

But for the moment, here's some code that uses the current
gtk_grab_add to simulate a "modal dialog". (It doesn't really
handle event propagation right, but that could be fixed fairly
simply.)

Regards,
                                        Owen

-------

#include <gtk/gtk.h>

/* Events sent to the grab window could be anything - check if they
   correspond to a child of the grab window, and if so forward them.
   If they aren't destined for a child, or for the grab window, throw
   them out */
int
handle_event (GtkWidget *widget, GdkEvent *event)
{
  GtkWidget *event_widget;
  GtkWidget *w;

  /* Find the window in which the event occurred */
  gdk_window_get_user_data (event->any.window, (void**) &event_widget);

  /* If it was really for the grab window, continue processing for
     the specific event type */
  if (event_widget == widget)
    {
      return FALSE;
    }

  w = event_widget;
  while (w && w != widget)
    w = w->parent;

  if (w)			/* event was in child of grab widget */
    {
      /* This would more aproporiately be gtk_propagate_event from
	 gtkmain.c, but that isn't exported */
      gtk_widget_event(event_widget,event);
    }

  return TRUE;
}

void
hello (void)
{
  g_print ("Hello World\n");
  gtk_exit (0);
}

void
destroy (void)
{
  gtk_exit (0);
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *grab_window;

  gtk_init (&argc, &argv);

  /* Create a main window */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_signal_connect (GTK_OBJECT (window), "destroy",
		      GTK_SIGNAL_FUNC (destroy), NULL);
  gtk_container_border_width (GTK_CONTAINER (window), 10);

  button = gtk_button_new_with_label("Hello");
  gtk_container_add(GTK_CONTAINER(window),button);
  gtk_widget_show(button);
  gtk_signal_connect(GTK_OBJECT(button),"clicked",(GtkSignalFunc)hello,
		     NULL);

  gtk_widget_show (window);

  /* and a "modal dialog" */
  
  grab_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_border_width (GTK_CONTAINER (grab_window), 10);

  button = gtk_button_new_with_label("Go away");
  gtk_container_add(GTK_CONTAINER(grab_window),button);
  gtk_widget_show(button);
  gtk_signal_connect_object(GTK_OBJECT(button),"clicked",
			    GTK_SIGNAL_FUNC (gtk_widget_destroy), 
			    GTK_OBJECT(grab_window));

  /* redirect events sent to the grab window if necessary */
  gtk_signal_connect(GTK_OBJECT(grab_window),"event",
		     GTK_SIGNAL_FUNC (handle_event), NULL);

  gtk_widget_show (grab_window);
  gtk_grab_add(grab_window);

  gtk_main ();

  return 0;
}




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