testtoplevelembed.c 3.21 KB
Newer Older
1 2 3
#include "config.h"
#include <gtk/gtk.h>

4
GtkWidget *notebook;
5 6


7 8 9 10 11
static void
remove_notebook_page (GtkWidget *button,
		      GtkWidget *toplevel)
{
  gtk_container_remove (GTK_CONTAINER (notebook), toplevel);
12
  gtk_widget_show (toplevel);
13 14 15 16 17
}

GtkWidget *
create_tab_label (GtkWidget *toplevel)
{
18
  GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
19 20
  GtkWidget *label = gtk_label_new (G_OBJECT_TYPE_NAME (toplevel));
  GtkWidget *button = gtk_button_new ();
21
  GtkWidget *image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

  gtk_container_add (GTK_CONTAINER (button), image);
  gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, TRUE, 0);

  g_signal_connect (button, "clicked",
		    G_CALLBACK (remove_notebook_page), toplevel);

  gtk_widget_show_all (box);
  return box;
}

static void
toplevel_delete_event (GtkWidget *toplevel,
		       GdkEvent  *event,
		       gpointer   none)
{
  GdkWindow *gdk_win;
  GtkWidget *label = create_tab_label (toplevel);

  gdk_win = gtk_widget_get_window (notebook);
  g_assert (gdk_win);

  gtk_widget_hide (toplevel);
  gtk_widget_unrealize (toplevel);

  gtk_widget_set_parent_window (toplevel, gdk_win);
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), toplevel, label);

  gtk_widget_show (toplevel);
}

54 55 56 57 58 59 60 61 62 63 64 65 66
gint
main (gint argc, gchar **argv)
{
  GtkWidget *window;
  GtkWidget *widget;
  
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Toplevel widget embedding example");
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);

  notebook = gtk_notebook_new ();
67
  gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
68 69 70 71 72
  gtk_container_add (GTK_CONTAINER (window), notebook);

  gtk_widget_realize (notebook);

  widget = gtk_about_dialog_new ();
73 74
  toplevel_delete_event (widget, NULL, NULL);
  g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
75 76

  widget = gtk_file_chooser_dialog_new ("the chooser", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
77 78
  toplevel_delete_event (widget, NULL, NULL);
  g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
79

80
  widget = gtk_color_chooser_dialog_new ("the colorsel", NULL);
81 82
  toplevel_delete_event (widget, NULL, NULL);
  g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
83

84
  widget = gtk_font_chooser_dialog_new ("the fontsel", NULL);
85 86
  toplevel_delete_event (widget, NULL, NULL);
  g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
87 88 89 90 91

  widget = gtk_recent_chooser_dialog_new ("the recent chooser", NULL,
					  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					  GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
					  NULL);
92 93
  toplevel_delete_event (widget, NULL, NULL);
  g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
94 95 96 97

  widget = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, 
				   GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
				   "Do you have any questions ?");
98 99
  toplevel_delete_event (widget, NULL, NULL);
  g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
100 101 102

  gtk_widget_show_all (window);
  gtk_main ();
103 104

  return 0;
105
}