Julio Burgos
Julio Burgos

Reputation: 21

changing background color in gtk 3.0

the compiler works but does not change background of the window why happens that? this is my code:

 #include <gtk/gtk.h>
 #include <stdlib.h>

// initialize the window 
void initwindow(float factor,const char* title,GtkWidget* window){
  gtk_window_set_default_size(GTK_WINDOW(window), gdk_screen_get_width (gdk_screen_get_default ())*factor,gdk_screen_get_height(gdk_screen_get_default ())*factor);
  gtk_window_set_title(GTK_WINDOW(window),title);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);}

// create random doubles
double randdouble(){
  return (double)rand() / (double)RAND_MAX; }

// function to change backgrounf

void changwindowcolor(GtkWidget* button){
  GdkRGBA color;
  color.red=randdouble();
  color.green=randdouble();
  color.blue=randdouble();
  color.alpha=1;
  gtk_widget_override_background_color(button, GTK_STATE_NORMAL,&color);}

void activate(GtkApplication *app,gpointer user_data){
  const char *title="Myapp";
  GtkWidget *window= gtk_application_window_new (app);
  GtkWidget *layout = gtk_grid_new();
  GtkWidget *button = gtk_button_new_with_label("color");
  initwindow(0.5,title,window);
  gtk_container_add (GTK_CONTAINER (window), layout);
  g_signal_connect(button, "clicked", G_CALLBACK(changwindowcolor),button);
  gtk_grid_attach(GTK_GRID (layout), button, 0,0,1,1);
  gtk_widget_show_all(window);}

int main(int argc, char **argv){

  GtkApplication *app = gtk_application_new ("org.gtk.example",G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  int status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);
  return status;}

Upvotes: 0

Views: 3171

Answers (1)

ptomato
ptomato

Reputation: 57870

The gtk_window_override_background_color function is deprecated, so it's probably better to try a different approach than to debug this one.

Instead, create a GtkCssProvider, load CSS such as * { background-color: #123456; } into it, and add it to the button's style context with gtk_style_context_add_provider.

Upvotes: 1

Related Questions