1
votes

I have a window in gtk which includes a Build() function as follow:

protected virtual void Build()
{
    global::Stetic.Gui.Initialize(this);
    // Widget Client.Forms.Notification
    this.Name = "Client.Forms.Notification";
    this.Title = "Notification";
    this.TypeHint = Gdk.WindowTypeHint.Normal;
    //this.TypeHint = ((global::Gdk.WindowTypeHint)(4));
    this.WindowPosition = ((global::Gtk.WindowPosition)(4));
    // Container child Client.Forms.Notification.Gtk.Container+ContainerChild
    this.vbox1 = new global::Gtk.VBox();
    this.vbox1.Name = "vbox1";
    this.vbox1.Spacing = 6;
    // Container child vbox1.Gtk.Box+BoxChild
    this.label1 = new global::Gtk.Label();
    this.label1.HeightRequest = 20;
    this.label1.Name = "label1";
    this.label1.LabelProp = "Notification";
    this.vbox1.Add(this.label1);
    global::Gtk.Box.BoxChild w1 = ((global::Gtk.Box.BoxChild)(this.vbox1[this.label1]));
    w1.Position = 0;
    w1.Expand = false;
    w1.Fill = false;
    // Container child vbox1.Gtk.Box+BoxChild
    this.hbox1 = new global::Gtk.HBox();
    this.hbox1.Name = "hbox1";
    this.hbox1.Spacing = 6;
    // Container child hbox1.Gtk.Box+BoxChild
    this.image1 = new global::Gtk.Image();
    this.image1.Name = "image1";
    this.image1.Pixbuf = global::Gdk.Pixbuf.LoadFromResource("Client.Resources.icon.png");
    this.hbox1.Add(this.image1);
    global::Gtk.Box.BoxChild w2 = ((global::Gtk.Box.BoxChild)(this.hbox1[this.image1]));
    w2.Position = 0;
    w2.Expand = false;
    w2.Fill = false;
    // Container child hbox1.Gtk.Box+BoxChild
    this.label2 = new global::Gtk.Label();
    this.label2.Name = "label2";
    this.label2.WidthRequest = 260;
    this.label2.Wrap = true;
    this.label2.LabelProp = "Description";
    this.hbox1.Add(this.label2);
    global::Gtk.Box.BoxChild w3 = ((global::Gtk.Box.BoxChild)(this.hbox1[this.label2]));
    w3.Position = 1;
    w3.Expand = false;
    w3.Fill = false;
    this.vbox1.Add(this.hbox1);
    global::Gtk.Box.BoxChild w4 = ((global::Gtk.Box.BoxChild)(this.vbox1[this.hbox1]));
    w4.Position = 1;
    w4.Expand = false;
    w4.Fill = false;
    this.label1.ModifyBase(Gtk.StateType.Normal, Core.fromColor(System.Drawing.Color.Orange));
    this.ModifyBase(Gtk.StateType.Normal, Core.fromColor(System.Drawing.Color.Orange));
    this.vbox1.ModifyBase(Gtk.StateType.Normal, Core.fromColor(System.Drawing.Color.Orange));
    root = new Gtk.EventBox();
    root.Add(this.vbox1);
    this.Decorated = false;
    this.Add(root);
    if ((this.Child != null))
    {
        this.Child.ShowAll();
    }
    this.DefaultWidth = 460;
    this.DefaultHeight = 220;
}

(source code of this whole window: https://github.com/pidgeonproject/pidgeon/blob/master/Forms/Notification.cs)

As you can see it is calling

this.ModifyBase(Gtk.StateType.Normal, Core.fromColor(System.Drawing.Color.Orange));

which should change the background to orange, however it doesn't.

I tried to insert more similar functions on window elements, but it doesn't work either, does anyone know how to change the background color of the window?

What I want to do is create a small window with no decoration (so just a rectangle) of specific color and some text in it. This window is supposed to be transparent (that works now) and should have a picture and 2 labels with text and disappear on click - all this works now just I can't change the background from gray to some better color. I would be happy for answers that would let me do this using some other way (I can think of creating a small form just with drawing area and paint it with the color and then paint the text as well, but it sounds to me quite complicated for something so simple as I want to do).

NOTE: mono is using GTK 2, and the version of GTK# for .Net is using 2.12.20

3

3 Answers

4
votes

Try to use Event Box. Seems to work

1
votes

For GTK+2 you need to create a gtk resource like so:

char *my_custom_style = "style \"my-style-name\" { bg[NORMAL] = \"#339933\" }\nclass \"GtkWindow\" style \"my-style-name\"";

then load the resource when you start the program, after initialization:

gtk_rc_parse_string (my_custom_style);

Now any GtkWindow will use your custom style for it's background.

The C documentation for Gtk Resource Files

Edit:

If you only want to change one specific window, then you'd change the custom style string to something like

char *my_custom_style="style \"my-style-name\" { bg[NORMAL] = \"#339933\" }\nwidget \"my-custom-window\" style= \"my-style-name\"';

You then set the name of the window you want to change the background with

gtk_widget_set_name (GTK_WIDGET (my_window), "my-custom-window");

Edit 2:

Here's the documentation to the Gtk# Rc parsing function:

http://buttle.shangorilla.com/1.1/handlers/monodoc.ashx?link=M%3aGtk.Rc.Parse(System.String)

0
votes

The documentation (for the C API) says that gtk_widget_modify_color() is deprecated, and that newly written code should use gtk_widget_override_background_color() instead, so try that.

Overriding themes in GTK+ is notoriously hard though, but the 3.0 API really sounds as if it's supporting what you want to do.

Can't test this myself at the moment, unfortunately.