Close button in GtkNotebook

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Close button in GtkNotebook

Augusto Fraga Giachero

Hi,

I've been playing with Glade and GtkNotebook for while, and I couldn't
figure a way to enable the close button on the tabs like this:



With I've read online it seems that I need to add a GtkBox to the tab
title area and add the label and a standard GtkButton to the right, but
the GtkButton is too large (even with no text inside it) and I couldn't
find out how to use the stock close icon for the GtkButton through
Glade.

Ideas?

Thanks,
Augusto Fraga Giachero.

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Close button in GtkNotebook

Gtk+ - Apps Dev mailing list

 
Hi Augusto,

This doesn't use glade but it might help out. You can add a label and button to a box and add it to the notebook tab. In the button "clicked" callback you can us the notebook pointer if you need that variable. If you want to be able to really customize the look and size of the button you could replace it with a drawing area and draw your own button.

Eric

/*
   gcc -Wall notebook1.c -o notebook1 `pkg-config --cflags --libs gtk+-3.0`
   Tested with GTK3.18 on Ubuntu16.04
*/

#include<gtk/gtk.h>

static void button_clicked(GtkWidget *button, GtkWidget *notebook)
  {
    g_print("Clicked\n");
  }
int main(int argc, char *argv[])
  {
    gtk_init (&argc, &argv);

    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Notebook");
    gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *label1=gtk_label_new("page1");
    GtkWidget *label2=gtk_label_new("page2");
    GtkWidget *nb_label1=gtk_label_new("tab1");
    GtkWidget *nb_label2=gtk_label_new("tab2");
 
    GtkWidget *x_label=gtk_label_new(NULL);
    gtk_label_set_markup(GTK_LABEL(x_label), "<span foreground='red'>x</span>");

    GtkWidget *button=gtk_button_new();
    gtk_container_add(GTK_CONTAINER(button), x_label);

    GtkWidget *box=gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
    gtk_box_pack_start(GTK_BOX(box), nb_label2, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
    gtk_widget_show_all(box);

    GtkWidget *notebook=gtk_notebook_new();
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), label1, nb_label1);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), label2, box);

    g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), notebook);
 
    gtk_container_add(GTK_CONTAINER(window), notebook);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
  }  


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Close button in GtkNotebook

Stefan Salewski-2
In reply to this post by Augusto Fraga Giachero
On Sun, 2017-05-28 at 16:08 -0300, Augusto Fraga Giachero wrote:
> I've been playing with Glade and GtkNotebook for while, and I
> couldn't
> figure a way to enable the close button on the tabs

I guess what you want is what gedit does with the tabs?

The gedit code has been modified a few times in the last years, I am
currently using code from GTK 3.20 in my Nim editor. See

# from 3.20 gedit-documents-panel.c


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Close button in GtkNotebook

Augusto Fraga Giachero
In reply to this post by Gtk+ - Apps Dev mailing list
Hi Eric,

I was doing something similar with what you have proposed, it works but
the end result differs from what I've observed on other gnome
applications (Nautilus, Gedit etc...). I want my application to be as
consistent and theme agnostic as possible. But the central idea of your
implementation is right, you need to encapsulate the button and the
label inside a GtkBox.

Stefan Salewski <[hidden email]> wrote:
> The gedit code has been modified a few times in the last years, I am
> currently using code from GTK 3.20 in my Nim editor. See
>
> # from 3.20 gedit-documents-panel.c

I've looked in the Gedit source code, and this seems to me the best
solution. For future reference the tab creation occurs in the
'row_create' function, the relevant bits are here:
---
generic_row->close_button = GTK_WIDGET (g_object_new (GTK_TYPE_BUTTON,
                                                           "relief",
GTK_RELIEF_NONE,
"focus-on-click", FALSE,
                                                           NULL));

     context = gtk_widget_get_style_context (generic_row->close_button);
     gtk_style_context_add_class (context, "flat");
     gtk_style_context_add_class (context, "small-button");

     icon = g_themed_icon_new_with_default_fallbacks
("window-close-symbolic");

---

Would be great to if the Gtk documentation included it, something like
'Gtk Best Practices', so we don't keep re-inventing the wheel.

Thanks!
Augusto Fraga Giachero
On 28-05-2017 20:32, [hidden email] wrote:

>
> Hi Augusto,
>
> This doesn't use glade but it might help out. You can add a label and
> button to a box and add it to the notebook tab. In the button
> "clicked" callback you can us the notebook pointer if you need that
> variable. If you want to be able to really customize the look and size
> of the button you could replace it with a drawing area and draw your
> own button.
>
> Eric
>
> /*
>    gcc -Wall notebook1.c -o notebook1 `pkg-config --cflags --libs
> gtk+-3.0`
>    Tested with GTK3.18 on Ubuntu16.04
> */
>
> #include<gtk/gtk.h>
>
> static void button_clicked(GtkWidget *button, GtkWidget *notebook)
>   {
>     g_print("Clicked\n");
>   }
> int main(int argc, char *argv[])
>   {
>     gtk_init (&argc, &argv);
>
>     GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
>     gtk_window_set_title(GTK_WINDOW(window), "Notebook");
>     gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
>     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
>     g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
>
>     GtkWidget *label1=gtk_label_new("page1");
>     GtkWidget *label2=gtk_label_new("page2");
>     GtkWidget *nb_label1=gtk_label_new("tab1");
>     GtkWidget *nb_label2=gtk_label_new("tab2");
>
>     GtkWidget *x_label=gtk_label_new(NULL);
>     gtk_label_set_markup(GTK_LABEL(x_label), "<span
> foreground='red'>x</span>");
>
>     GtkWidget *button=gtk_button_new();
>     gtk_container_add(GTK_CONTAINER(button), x_label);
>
>     GtkWidget *box=gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
>     gtk_box_pack_start(GTK_BOX(box), nb_label2, TRUE, TRUE, 0);
>     gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
>     gtk_widget_show_all(box);
>
>     GtkWidget *notebook=gtk_notebook_new();
>     gtk_notebook_append_page(GTK_NOTEBOOK(notebook), label1, nb_label1);
>     gtk_notebook_append_page(GTK_NOTEBOOK(notebook), label2, box);
>
>     g_signal_connect(button, "clicked", G_CALLBACK(button_clicked),
> notebook);
>
>     gtk_container_add(GTK_CONTAINER(window), notebook);
>
>     gtk_widget_show_all(window);
>
>     gtk_main();
>
>     return 0;
>   }
>

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Loading...