Re: Custom GtkHeaderBar




Hi Fujiwara,

The GtkBox is going to use the background window for it's color. The box just does the layout. If you create 
a header bar from a box you will have to draw on the background where your header bar is going to be. This 
can get a little tricky to get the measurements that you need. A simple case shouldn't be too difficult 
though. If you pass the box to the main window "draw" callback you can get something going. You can draw with 
cairo or get your CSS color. I don't have a C# setup to test with so I hope C will do.

By the way, I was just programming something similar looking at cairo meshes. Don't know if you like mesh 
drawing but there is a little program at the following that changes some background windows from the UI.

https://github.com/cecashon/OrderedSetVelociRaptor/blob/master/Misc/cairo_drawings/draw_mesh1.c

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_box(GtkWidget *widget, cairo_t *cr, gpointer 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);

    GtkWidget *label1=gtk_label_new("Header");    
    GtkWidget *label2=gtk_label_new("Bar");     

    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), label1, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(box), label2, TRUE, TRUE, 0);
    g_signal_connect(window, "draw", G_CALLBACK(draw_box), box);

    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), box, 0, 0, 1, 1); 
    gtk_grid_attach(GTK_GRID(grid), label3, 0, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), label4, 0, 2, 1, 1);  

    gtk_container_add(GTK_CONTAINER(window), grid);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
  }  
static gboolean draw_box(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);
    //Get the dimensions to draw header bar.
    gint width=gtk_widget_get_allocated_width(GTK_WIDGET(widget));
    gint height=gtk_widget_get_allocated_height(GTK_WIDGET(data));
    cairo_set_source_rgb(cr, 1.0, 0.0, 1.0);
    //Add 20 to height for 2 time contain width.
    cairo_rectangle(cr, 0.0, 0.0, width, height+20);
    cairo_fill(cr);
    return FALSE;
  }



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