Gtk4 C - change bacground color of whole window

,

Hello,
few weeks ago you have helped me with changing color of widget (labels and buttons):

Now I need change background of whole window.
so I have created:

GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));
GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);
gtk_window_set_child (GTK_WINDOW (win), vBox);
.
.
PangoAttribute  *colorWinBacground  = pango_attr_background_new(0,0,10000);
PangoAttrList *tempSeznamWinAtribBack = pango_attr_list_new();
pango_attr_list_change(colorWinBacground, colorWinBacground);

but I can not find functions such a:

gtk_window_set_attributes

or

gtk_box_set_attributes

I have read something about CSS, but it seems me too complicated.
Please could anybody helsp me if exisst some similar (simple) soulutiol like for labes?
Thank you a lot

Use of CSS is not that easy for beginners, but it is the way GTK3 and GTK4 uses. There are reasons for that – e.g. as not only plain colors are used, but also gradients or pictures. But I know that often the first question of kids new to GTK was: How can I change this color – and they expected a simple command, and were disappointed when it was not possible with one plain command. But it is not too difficult, and I tried to explain it a bit in the book. I just tried to change the example from GTK4 for Graphical User Interfaces a little to color the window background, and it works fine. You can directly port that code to your C or C++, just use the long C names like gtk_button_new() instead of newButton() and so. And you should find more about CSS somewhere in the C API docs. Unfortunately CSS styling works not always so nice, I had use cases where my styling attempts just did not work.

import gintro/[gtk4, gobject, gio]

proc buttonCb(b: Button) =
  echo "click"

proc activate(app: gtk4.Application) =
  let window = newApplicationWindow(app)
  let box = newBox(Orientation.horizontal, 10)
  window.setChild( box)
  let b1 = newButton("Click Me")
  let b2 = newButtonWithMnemonic("_Mnemonic")
  let b3 = newButtonFromIconName("document-print")
  let b4 = newButton("\u2654")
  let b5 = newToggleButton("Toggle Button")
  let b0 = newButton("CSS Styled")
  let cssProvider = newCssProvider()
  let data = "window {color: yellow; background: green;}"
  cssProvider.loadFromData(data)
  let styleContext = window.getStyleContext
  assert styleContext != nil
  addProvider(styleContext, cssProvider, STYLE_PROVIDER_PRIORITY_USER)
  for b in [b0, b1, b2, b3, b4, b5]:
    box.append(b)
    b.connect("clicked", buttonCb)
  window.show

proc main =
  let app = newApplication("org.gtk.example")
  app.connect("activate", activate)
  let status = app.run
  quit(status)

main()

Ok, thanks - I have now window with changed bacground color.

And now I am looking for how to define my color (from RGB parts) to use:

string data = "window {background: myColor;}";

thanks

You can use hexadecimal values like

let data = "window {color: yellow; background: #ff00aa;}"

This #ff00aa would result in some purple background. It is RGB, red, green, blue, with ff=255 max value.
Other notations may work also, you may search with google for “CSS color values”.

great! thanks for help

Hello, I have some additionall question.
I have created second window in app.

GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));
GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);
gtk_window_set_child (GTK_WINDOW (win), vBox);
GtkWidget  *labText = gtk_label_new("text 1");
gtk_box_append (GTK_BOX (vBox), labText);

GtkCssProvider *provider = gtk_css_provider_new();
string data = "window {background: blue;}";
gtk_css_provider_load_from_data(provider,data.c_str(),-1);
GdkDisplay *displej = gtk_widget_get_display(GTK_WIDGET(win));
gtk_style_context_add_provider_for_display(displej,GTK_STYLE_PROVIDER(provider),GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_widget_show (win);

GtkWidget* windowSecond = gtk_window_new();
GtkWidget  *vBoxSecond = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);
gtk_window_set_child (GTK_WINDOW (windowSecond), vBoxSecond);
GtkWidget* labText4 = gtk_label_new("text 4");
gtk_box_append (GTK_BOX (vBoxSecond), labText4);
gtk_widget_show (windowSecond);

I need that this second window has different background color (or color from system). But it has the same color as the first window.

Please could anybody give me any advice how to do that.
Thanks a lot.

Can not remember details, but you should get the idea from c - how to style gtk widgets individually with css code - Stack Overflow

thanks for link - it helps to find solution:

instead
gtk_style_context_add_provider_for_display

I use:
gtk_style_context_add_provider

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.