tadeboro wrote:
Hello.
I modified your code a bit to accommodate that additional close button on tab. Current results aren't the most beautiful ones, but you should be able to tweak them to suit your needs.
Code:
#include <gtk/gtk.h>
static void destroy (GtkWidget*, gpointer);
static void switch_page (GtkButton*, GtkNotebook*);
static GtkWidget *
create_notebook_label( const gchar *text,
GtkWidget *notebook,
gint page );
static void
cb_close_tab( GtkButton *button,
GtkNotebook *notebook );
int main (int argc,
char *argv[])
{
GtkWidget *window, *notebook;
GtkWidget *label1, *label2, *child1, *child2;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Notebook");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_set_size_request (window, 250, 100);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);
notebook = gtk_notebook_new ();
/* TB: Create composed tab widget for first tab */
label1 = create_notebook_label( "Page One", notebook, 0 );
label2 = create_notebook_label( "Page Two", notebook, 1 );
child1 = gtk_button_new_with_label ("Go to page 2 to find the answer.");
child2 = gtk_button_new_with_label ("Go to page 1 to find the answer.");
/* Notice that two widgets were connected to the same callback function! */
g_signal_connect (G_OBJECT (child1), "clicked",
G_CALLBACK (switch_page),
(gpointer) notebook);
g_signal_connect (G_OBJECT (child2), "clicked",
G_CALLBACK (switch_page),
(gpointer) notebook);
/* Append to pages to the notebook container. */
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child1, label1);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child2, label2);
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_BOTTOM);
gtk_container_add (GTK_CONTAINER (window), notebook);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
/* Switch between the current GtkNotebook page. */
static void
switch_page (GtkButton *button,
GtkNotebook *notebook)
{
gint page = gtk_notebook_get_current_page (notebook);
if (page == 0)
gtk_notebook_set_current_page (notebook, 1);
else
gtk_notebook_set_current_page (notebook, 0);
}
static void
destroy (GtkWidget *window,
gpointer data)
{
gtk_main_quit ();
}
static GtkWidget *
create_notebook_label( const gchar *text,
GtkWidget *notebook,
gint page )
{
GtkWidget *hbox,
*label,
*button,
*image;
hbox = gtk_hbox_new (FALSE, 3);
label = gtk_label_new (text);
gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
button = gtk_button_new();
g_object_set_data( G_OBJECT( button ), "page", GINT_TO_POINTER( page ) );
g_signal_connect( G_OBJECT( button ), "clicked",
G_CALLBACK( cb_close_tab ),
GTK_NOTEBOOK( notebook ) );
gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
gtk_container_add (GTK_CONTAINER (button), image);
gtk_widget_show_all( hbox );
return( hbox );
}
static void
cb_close_tab( GtkButton *button,
GtkNotebook *notebook )
{
gint page;
page = GPOINTER_TO_INT( g_object_get_data( G_OBJECT( button ), "page" ) );
gtk_notebook_remove_page( notebook, page );
}
-------------------------
I know is 2012, but maybe it help somebody
Code:
/* TB: Create composed tab widget for first tab */
label1 = create_notebook_label( "Page One", notebook, 0 );
label2 = create_notebook_label( "Page Two", notebook, 1 );
child1 = gtk_button_new_with_label ("Go to page 2 to find the answer.");
child2 = gtk_button_new_with_label ("Go to page 1 to find the answer.");
changed like this:
Code:
child1 = gtk_button_new_with_label ("Go to page 2 to find the answer.");
child2 = gtk_button_new_with_label ("Go to page 1 to find the answer.");
label1 = create_notebook_label( child1, "Page One", notebook );
label2 = create_notebook_label( child2, "Page Two", notebook );
and:
Code:
static GtkWidget *
create_notebook_label( GtkWidget *child,
const gchar *text,
GtkWidget *notebook )
{
....
button = gtk_button_new();
g_object_set_data( G_OBJECT( button ), "child", child );
.........
}
and:
Code:
static void
cb_close_tab( GtkButton *button,
GtkNotebook *notebook )
{
GtkWidget *child;
gint page;
child = GTK_WIDGET( g_object_get_data( G_OBJECT( button ), "child" ));
page = gtk_notebook_page_num( notebook, child );
gtk_notebook_remove_page( notebook, page );
}