Skip to content

Instantly share code, notes, and snippets.

@nlowe
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlowe/b0cf93a99dc90372fb6a to your computer and use it in GitHub Desktop.
Save nlowe/b0cf93a99dc90372fb6a to your computer and use it in GitHub Desktop.
//Compile With: valac --thread --pkg gtk+-3.0 TestApp.vala
using Gtk;
public class TestBar : Gtk.HeaderBar{
private unowned TestWindow main_window;
public TestBar(TestWindow parent){
get_style_context().add_class("my-bar");
show_close_button = true;
has_subtitle = true;
title = "Test Application";
subtitle = "CSS Doesn't Work :(";
var test_button = new Gtk.Button.from_icon_name("gtk-add", Gtk.IconSize.LARGE_TOOLBAR);
test_button.clicked.connect(() => {
var ctx = get_style_context();
message(ctx.has_class("my-bar") ? "TRUE" : "FALSE");
message(@"background-color should be $(ctx.get_background_color(ctx.get_state()).to_string())"); // LIER! http://imgur.com/87kGWPC
});
pack_start(test_button);
}
}
public class TestWindow : Gtk.Window{
public static const string STYLE = """
@define-color header-bg #2b2b2b;
@define-color main-bg #8c8c8c;
.my-bar {
background-color: @header-bg;
}
.my-main-window{
background-color: @main-bg;
}
""";
public Gtk.CssProvider css_provider{get; private set;}
private TestBar bar;
public TestWindow(){
get_style_context().add_class("my-main-window");
init_css();
set_size_request(800,600);
bar = new TestBar(this);
set_titlebar(bar);
add(new Gtk.Label("Hello, World!"));
this.destroy.connect(Gtk.main_quit);
}
private void init_css(){
css_provider = new Gtk.CssProvider();
try{
css_provider.load_from_data(TestWindow.STYLE, -1);
Gtk.StyleContext.add_provider_for_screen(get_screen(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
}catch(Error e){
warning(@"Unable to load style information: [$(e.domain.to_string())]: $(e.message)");
}
}
}
public static int main(string[] args){
Gtk.init(ref args);
if (Environment.get_variable ("RTL") != null) {
Gtk.Widget.set_default_direction (Gtk.TextDirection.RTL);
}else{
Gtk.Widget.set_default_direction (Gtk.TextDirection.LTR);
}
var window = new TestWindow();
window.show_all();
Gtk.main();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment