Re: Custom GtkHeaderBar
- From: cecashon aol com
- To: tfujiwar redhat com, gtk-app-devel-list gnome org
- Subject: Re: Custom GtkHeaderBar
- Date: Sat, 15 Apr 2017 15:25:41 -0400
I gave it another try and "header" gtk_style_context_add_class() is transparent. I can use "menu" and that
returns a darker color that is used on the title bar.
It is C again. I did get foo.vala output to foo.c. Too much stuff.
Eric
/*
gcc -Wall box1.c -o box1 `pkg-config --cflags --libs gtk+-3.0`
Tested with GTK3.18 on Ubuntu16.04
*/
#include<gtk/gtk.h>
static gboolean draw_main_window(GtkWidget *widget, cairo_t *cr, gpointer data);
static gboolean draw_event_box(GtkWidget *widget, cairo_t *cr, gpointer data);
static void close_program(GtkWidget *widget, gpointer user_data);
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), "Title Bar");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_set_app_paintable(window, TRUE);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(window, "draw", G_CALLBACK(draw_main_window), NULL);
GtkWidget *button=gtk_button_new_with_label("Close");
g_signal_connect(button, "clicked", G_CALLBACK(close_program), NULL);
GtkWidget *label1=gtk_label_new("Header");
gtk_label_set_markup(GTK_LABEL(label1), "<span foreground=\"white\">Header</span>");
GtkWidget *label2=gtk_label_new("Bar");
gtk_label_set_markup(GTK_LABEL(label2), "<span foreground=\"white\">Bar</span>");
GtkWidget *box=gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_container_set_border_width(GTK_CONTAINER(box), 10);
gtk_widget_set_hexpand(box, TRUE);
gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(box), label1, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(box), label2, TRUE, TRUE, 0);
GtkWidget *event=gtk_event_box_new();
GtkStyleContext *context=gtk_widget_get_style_context(event);
//Use the menu background.
gtk_style_context_add_class(context, "menu");
gtk_container_add(GTK_CONTAINER(event), box);
g_signal_connect(event, "draw", G_CALLBACK(draw_event_box), NULL);
gtk_window_set_titlebar(GTK_WINDOW(window), event);
GtkWidget *label3=gtk_label_new("Main");
gtk_widget_set_vexpand(label3, TRUE);
gtk_widget_set_hexpand(label3, TRUE);
GtkWidget *label4=gtk_label_new("Window");
gtk_widget_set_vexpand(label4, TRUE);
gtk_widget_set_hexpand(label4, TRUE);
GtkWidget *grid=gtk_grid_new();
gtk_grid_attach(GTK_GRID(grid), label3, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), label4, 0, 1, 1, 1);
gtk_container_add(GTK_CONTAINER(window), grid);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static gboolean draw_main_window(GtkWidget *widget, cairo_t *cr, gpointer data)
{
//Paint the main window.
cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
cairo_paint(cr);
return FALSE;
}
static gboolean draw_event_box(GtkWidget *widget, cairo_t *cr, gpointer data)
{
//Paint the event box header bar.
gint width=gtk_widget_get_allocated_width(widget);
gint height=gtk_widget_get_allocated_height(widget);
GtkStyleContext *context=gtk_widget_get_style_context(widget);
gtk_render_background(context, cr, 0, 0, width, height);
return FALSE;
}
static void close_program(GtkWidget *widget, gpointer user_data)
{
gtk_main_quit();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]