Languages

[Genie] Cairo transparent window

Your rating: None Average: 5 (4 votes)
[indent=4]
 
/* Build with valac --pkg gtk+-2.0 --pkg cairo yourfile.gs */
 
uses
    Gtk
    Cairo
 
class TransparentWindow : Gtk.Window
 
    init
        this.type = Gtk.WindowType.TOPLEVEL
        /* Set the title is useless ! */ 
        this.title = "My first transparent window with curved rectangle !"
        default_height = 200
        default_width = 300
        window_position = WindowPosition.CENTER
        this.destroy += Gtk.main_quit
        this.set_app_paintable (true)
        /* Set window events */
        this.expose_event.connect (expose_event_cb)
        this.screen_changed.connect (screen_changed_cb)
        /* Add button press event */
        this.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
        this.button_press_event.connect (button_press_cb)
        /* Do not show the window decoration */
        this.set_decorated (false)
        screen_changed_cb (this, null)
        this.show_all()
 
    def private expose_event_cb (widget : Widget, event_expose : Gdk.EventExpose) : bool
        /* Create cairo surface */
        cairo_surface : Surface = widget.window.ref_cairo_surface ()
        cairo_context : Context = new Context (cairo_surface)
        cairo_context.set_source_rgba (1.0, 1.0, 1.0, 0.0)
        cairo_context.set_operator (Operator.SOURCE)
        cairo_context.paint()
        /* Create blue curved rectangle */
        width, height : int
        widget.window.get_size(out width, out height)
        cairo_context.set_source_rgba (0.0, 0.0, 2.5, 0.7)
        cairo_context.move_to (5, 5 + (height - 10) / 2)
        cairo_context.curve_to (5, 5, 5, 5, 5 + (width - 10) / 2, 5)
        cairo_context.curve_to (5 + (width - 10), 5, 5 + (width - 10), 5, 5 + (width - 10), 5 + (height - 10) / 2)
        cairo_context.curve_to (5 + (width - 10), 5 + (height - 10), 5 + (width - 10), 5 + (height - 10), 5 + (width - 10) / 2, 5 + (height - 10))
        cairo_context.curve_to (5, 5 + (height - 10), 5, 5 + (height - 10), 5, 5 + (height - 10) / 2)
        cairo_context.fill_preserve()
        cairo_context.stroke()
        return false
 
    def private screen_changed_cb (widget : Widget, previous_screen : Gdk.Screen?)
        /* Set transparency if possible */
        screen : Gdk.Screen = widget.get_screen()
        colormap : Gdk.Colormap? = screen.get_rgba_colormap()
        if colormap is null
            colormap = screen.get_rgb_colormap()
        widget.set_colormap(colormap)
 
    def private button_press_cb (button_event : Gdk.EventButton) : bool
        /* Move the window */
        if button_event.button is 1
            this.begin_move_drag((int) button_event.button, (int) button_event.x_root, (int) button_event.y_root, button_event.time) 
        return false
 
init
    Gtk.init (ref args)
    var test = new TransparentWindow()
    test.show_all()
    Gtk.main()