GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Dec 22, 2014 1:19 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Add close button to GTK notebook tab?
PostPosted: Sat Nov 05, 2011 5:25 pm 
Offline
Familiar Face

Joined: Thu Aug 18, 2011 8:26 am
Posts: 16
I have followed various tutorials which use the += delegate feature to delegate a close action to a button on a tab page.

I know I am not the only person having difficulty making this work, because when I googled it there were many people asking questions.

However, I have yet to see an answer which actually works the way a professional piece of software would.

Any ideas?

thanks.


Top
 Profile  
 
 Post subject: Re: Add close button to GTK notebook tab?
PostPosted: Sat Nov 05, 2011 7:08 pm 
Offline

Joined: Tue Nov 01, 2011 3:56 pm
Posts: 1
I also would like to know how to do this. Could anyone give some advice?

_________________
iphone development


Top
 Profile  
 
 Post subject: Re: Add close button to GTK notebook tab?
PostPosted: Sat Nov 05, 2011 9:59 pm 
Offline
GTK+ Geek

Joined: Sat Sep 03, 2011 6:36 pm
Posts: 94
Hey guys,finally i can help somebody (:

I was also wondering this and came up with a solution(you can also search the forums for a past thread maybe)

Heres some pseudocode :

THE WIDGETS:
label = gtk_label_new(title);
tab_container = gtk_hbox_new(false, 0); // the horizontal container to place the label and the close button (:
button = gtk_button_new(); <!--the close button

gtk_container_add(button,

gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU)); //here we add the close image to the button

BOX PACKING
gtk_box_pack_start(tab_container, label, TRUE, TRUE, 0);
//lets add them to the box
gtk_box_pack_start(tab_container, button, FALSE, FALSE, 0); // >>


//We add the tab container to the notebook AS TAB TITLE <--
gtk_notebook_append_page(notebook,the_widget_to_add, tab_container);

gtk_widget_show_all(tab_container); and we are ready to go ;)


Top
 Profile  
 
 Post subject: Re: Add close button to GTK notebook tab?
PostPosted: Sun Nov 06, 2011 2:22 pm 
Offline
Familiar Face

Joined: Thu Aug 18, 2011 8:26 am
Posts: 16
Thanks qwertyui, but that is what I already have.

I have a close button, and it even closes tabs, but after you open a number of tabs it starts to close the wrong ones... I have absolutely no idea why.


Top
 Profile  
 
 Post subject: Re: Add close button to GTK notebook tab?
PostPosted: Sun Nov 06, 2011 3:37 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello.

How do you determine the page that should be closed? Getting page by number will break as soon as one non-last page is removed from notebook and deleting current page will fail if user tries to close non-active page.

The way I usually do this is to store the actual widget that holds content of the notebook page into close button itself. This serves as a unique identifier to the notebook page (since widget cannot be added to two pages at the same time) and removing that page is then really painless and safe.

Sample code is attached below.

Cheers,
Tadej

Code:
#include <gtk/gtk.h>

static const char *images[] = { GTK_STOCK_FLOPPY,
                                GTK_STOCK_HARDDISK,
                                GTK_STOCK_PRINT,
                                GTK_STOCK_CUT };

static void
cb_close_tab (GtkButton   *button,
              GtkNotebook *notebook)
{
  GtkWidget *page;
  int page_no;

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

static void
append_notebook_page (GtkNotebook *notebook,
                      char const  *name)
{
  GtkWidget *image, *hbox, *close, *label, *icon;

  /* Main contents */
  image = gtk_image_new_from_stock (name, GTK_ICON_SIZE_DIALOG);
  gtk_widget_show (image);


  /* Tab contents */
  hbox = gtk_hbox_new (FALSE, 5);

  label = gtk_label_new (name);
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

  close = gtk_button_new ();
  g_object_set_data (G_OBJECT (close), "page", image);
  g_signal_connect (close, "clicked", G_CALLBACK (cb_close_tab), notebook);
  gtk_box_pack_start (GTK_BOX (hbox), close, FALSE, FALSE, 0);

  icon = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
  gtk_container_add (GTK_CONTAINER (close), icon);

  gtk_widget_show_all (hbox);


  /* Add page to the end */
  gtk_notebook_append_page (notebook, image, hbox);
}

int
main (int    argc,
      char **argv)
{
  GtkWidget *window,
            *notebook;
  size_t i, size;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);

  notebook = gtk_notebook_new ();
  gtk_container_add (GTK_CONTAINER (window), notebook);


  /* Create several pages */
  size = G_N_ELEMENTS (images);
  for (i = 0; i < size; i++)
    {
      append_notebook_page (GTK_NOTEBOOK (notebook),
                            images[i]);
    }

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 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