Added buttons to notebook tabs in testtoplevelembed

Now you can remove and reembed the toplevels (deleting the
toplevels put them back in the notebook).
This commit is contained in:
Tristan Van Berkom 2010-12-24 12:35:56 +09:00
parent 2dfa855bc4
commit 387d745e0e

View File

@ -1,16 +1,63 @@
#include "config.h"
#include <gtk/gtk.h>
GtkWidget *notebook;
static void
remove_notebook_page (GtkWidget *button,
GtkWidget *toplevel)
{
gtk_container_remove (GTK_CONTAINER (notebook), toplevel);
gtk_widget_hide (toplevel);
gtk_widget_show (toplevel);
}
GtkWidget *
create_tab_label (GtkWidget *toplevel)
{
GtkWidget *box = gtk_hbox_new (FALSE, 2);
GtkWidget *label = gtk_label_new (G_OBJECT_TYPE_NAME (toplevel));
GtkWidget *button = gtk_button_new ();
GtkWidget *image = gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_SMALL_TOOLBAR);
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);
}
gint
main (gint argc, gchar **argv)
{
GtkWidget *window;
GtkWidget *notebook;
GtkWidget *widget;
GtkWidget *label;
GdkWindow *gdk_win;
gtk_init (&argc, &argv);
@ -22,43 +69,35 @@ main (gint argc, gchar **argv)
gtk_container_add (GTK_CONTAINER (window), notebook);
gtk_widget_realize (notebook);
gdk_win = gtk_widget_get_window (notebook);
g_assert (gdk_win);
widget = gtk_about_dialog_new ();
label = gtk_label_new ("GtkAboutDialog");
gtk_widget_set_parent_window (widget, gdk_win);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label);
toplevel_delete_event (widget, NULL, NULL);
g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
widget = gtk_file_chooser_dialog_new ("the chooser", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
label = gtk_label_new ("GtkFileChooser");
gtk_widget_set_parent_window (widget, gdk_win);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label);
toplevel_delete_event (widget, NULL, NULL);
g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
widget = gtk_color_selection_dialog_new ("the colorsel");
label = gtk_label_new ("GtkColorSelDialog");
gtk_widget_set_parent_window (widget, gdk_win);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label);
toplevel_delete_event (widget, NULL, NULL);
g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
widget = gtk_font_selection_dialog_new ("the fontsel");
label = gtk_label_new ("GtkFontSelectionDialog");
gtk_widget_set_parent_window (widget, gdk_win);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label);
toplevel_delete_event (widget, NULL, NULL);
g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
widget = gtk_recent_chooser_dialog_new ("the recent chooser", NULL,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
label = gtk_label_new ("GtkRecentChooserDialog");
gtk_widget_set_parent_window (widget, gdk_win);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label);
toplevel_delete_event (widget, NULL, NULL);
g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
widget = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
"Do you have any questions ?");
label = gtk_label_new ("GtkMessageDialog");
gtk_widget_set_parent_window (widget, gdk_win);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label);
toplevel_delete_event (widget, NULL, NULL);
g_signal_connect (widget, "delete-event", G_CALLBACK (toplevel_delete_event), NULL);
gtk_widget_show_all (window);
gtk_main ();