GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Apr 23, 2014 5:47 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: How can i detect current page after a delete in notebook ?
PostPosted: Tue Jun 26, 2012 6:20 am 
Offline
Familiar Face

Joined: Fri Mar 25, 2011 7:55 am
Posts: 35
Location: romania
Hi,

Example below is not mine, i have modified only "delete page" procedure.
After deleting a page how can i detect current page ?

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,
                                          GtkWidget   *child );
static void cb_close_tab( GtkButton   *button,
                          GtkNotebook *notebook );


int main (int argc, char *argv[]) {
  GtkWidget *window, *notebook;
  GtkWidget *label1, *label2, *label3, *child3, *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 ();

 
  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.");
  child3 = gtk_button_new_with_label ("Go to page 2 to find the answer.");

  label1 = create_notebook_label( "Page One", notebook, child1 );
  label2 = create_notebook_label( "Page Two", notebook, child2 );
  label3 = create_notebook_label( "Page Three", notebook, child3 );

 
  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);

 
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child1, label1);
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child2, label2);
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child3, label3);

  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;
}

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,
                                          GtkWidget   *child ) {
  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 ), "child", child );
  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 ) {
  GtkWidget *child;
  gint       page;

  child = GTK_WIDGET( g_object_get_data( G_OBJECT( button ), "child" ));
  page  = gtk_notebook_page_num( GTK_NOTEBOOK( notebook ), child );
  gtk_notebook_remove_page( notebook, page );
}


thanks !

_________________
c/c++ for ever


Top
 Profile  
 
 Post subject: Re: How can i detect current page after a delete in notebook
PostPosted: Tue Jun 26, 2012 5:31 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK
Hi,

You can connect to the "switch-page" signal. This signal is emitted when ever the user or programme changes the page in a GtkNotebook.

This is the proto-type for the call-back function. The function gives you the Widget for the page and the index of the page.
Code:
void                user_function                      (GtkNotebook *notebook,
                                                        GtkWidget   *page,
                                                        guint        page_num,
                                                        gpointer     user_data)


More documentation can be found at http://developer.gnome.org/gtk3/stable/GtkNotebook.html#GtkNotebook-switch-page

There are other signals such as "page-added" and "page-removed" which may be of use.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group