Re: Close button in GtkNotebook



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 <mail ssalewski de> 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, cecashon aol com 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;
  }




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]