// valac plugin2.vala --pkg gtk+-3.0 --vapidir=~/geary/bindings/vapi --metadatadir=~/geary/bindings/metadata --pkg WebKit-3.0

class PluginTest : GLib.Object {
    
    public static int main(string[] args) {
        Gtk.init(ref args);
        
        string HTML = """<html>
<body>
<embed width='100%' height='90%' type='type' src='source' style='background: #eee' />
</body>
</html>""";
        
        var window = new Gtk.Window();
        window.destroy.connect(Gtk.main_quit);
        window.set_default_size(300, 400);
        
        var sw = new Gtk.ScrolledWindow(null, null);
        var view = new WebKit.WebView();
        sw.add(view);
        window.add(sw);
        window.show_all();
        
        view.create_plugin_widget.connect(on_create_plugin);
        view.load_string(HTML, "text/html", "UTF-8", "");
        
        Gtk.main();
        
        return 0;
    }
    
    public static Gtk.Widget on_create_plugin(WebKit.WebView view, string mime_type, string uri,
        GLib.HashTable<string,string> param) {
        var widget = new PluginWidget();
        return widget;
    }
}

public class PluginWidget : Gtk.Box {
    
    private bool hidden = false;
    private Gtk.Label label;
    
    public PluginWidget() {
        Object(orientation: Gtk.Orientation.VERTICAL);
        
        var add = new Gtk.Button.from_stock("gtk-add");
        pack_start(add, false, false);
        add.clicked.connect(on_add);
        
        label = new Gtk.Label("Now you see me...");
        pack_end(label, false, false);
        
        var hide = new Gtk.Button.with_label("Toggle hidden");
        pack_end(hide, false, false);
        hide.clicked.connect(on_hide_toggle);
        
        show_all();
    }
    
    private void on_add() {
        var label = new Gtk.Label("Peek-a-boo!");
        pack_start(label, false, false);
        label.show();
    }
    
    private void on_hide_toggle() {
        if (hidden)
            label.show();
        else
            label.hide();
        hidden = !hidden;
    }
}

