conformal / gotk3

Go bindings for GTK3
ISC License
470 stars 81 forks source link

added basic support of GtkCssProvider #26

Open solusipse opened 10 years ago

solusipse commented 10 years ago

Simple example of gradient background:

package main

import (
    "github.com/conformal/gotk3/gtk"
    "github.com/conformal/gotk3/gdk"
    "log"
)

func main() {
    // Initialize GTK without parsing any command line arguments.
    gtk.Init(nil)

    // Create a new toplevel window, set its title, and connect it to the
    // "destroy" signal to exit the GTK main loop when it is destroyed.
    win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
    if err != nil {
        log.Fatal("Unable to create window:", err)
    }
    win.SetTitle("Simple Example")
    win.Connect("destroy", func() {
        gtk.MainQuit()
    })

    // Create a new label widget to show in the window.
    l, err := gtk.LabelNew("Hello, gotk3!")
    if err != nil {
        log.Fatal("Unable to create label:", err)
    }

    // Add the label to the window.
    win.Add(l)

    // Set the default window size.
    win.SetDefaultSize(800, 600)

    provider, _ := gtk.NewCssProvider()
    display, _ := gdk.DisplayGetDefault()
    screen, _ := display.GetDefaultScreen()

    provider.AddToScreen(screen)
    provider.LoadFromData(" GtkWindow {background: -gtk-gradient (linear,0 0,0 1,color-stop(0, #333),color-stop(0.2, #666))}")

    // Recursively show all widgets contained in this window.
    win.ShowAll()

    // Begin executing the GTK main loop.  This blocks until
    // gtk.MainQuit() is run. 
    gtk.Main()
}
jrick commented 10 years ago

I merged these changes against the latest master branch, but found that there were several issues with the code so they haven't been committed to the master branch yet. See http://sprunge.us/FajA for a diff.

Besides just the generic improvements (adding comments, etc.) I also changed CssProvider to embed a glib.Object, rather than saving the C.GtkCssProvider itself. Additionally, some of the wrappers that optionally take GErrors now also return a Go error created from the GError's message if they were set. Finally, I removed your AddToScreen wrapper for now. I believe we want to use GInterfaces here. See some of the other gotk3 objects that interfaces are implemented for (such as ComboBox, Grid, ListStore, etc.).