Rust bindings and wrappers for GLib, GDK 3, GTK+ 3 and Cairo. Online documentation.
gtk expects GTK+, GLib and Cairo development files to be installed on your system. Optionally, it is recommended to install the debug packages containing helpful debug symbols.
> sudo apt-get install libgtk-3-dev
> sudo apt-get install libgtk-3-0-dbg libglib2.0-0-dbg libcairo2-dbg
> sudo dnf install gtk3-devel glib2-devel
# Fedora 21 and earlier
> sudo yum install gtk3-devel glib2-devel
> brew install gtk+3
Make sure you have the GNU ABI version of the rust compiler installed. Since we need our own toolchain make sure to uncheck "Linker and platform libraries" in the Rust setup.
If you already have installed Rust, check if your Rust installation has
gcc.exe
and ld.exe
in its bin
directory. In that case remove those
executables, they will be provided by mingw instead. If you won't you'll
probably get a linking error ld: cannot find -limm32
.
For the toolchain one has two possibilities:
- Install msys2 with a mingw toolchain and the gtk3 package
- Install the mingw-w64 toolchain separately and then install a GTK+ SDK
This method is recommended according to the [GTK+ project] (http://www.gtk.org/download/windows.php). You can follow [this guide] (https://blogs.gnome.org/nacho/2014/08/01/how-to-build-your-gtk-application-on-windows/) or follow these steps:
- Install and update msys2
- Install the mingw toolchain:
$ pacman -S mingw-w64-x86_64-toolchain
- Install the gtk3 package:
$ pacman -S mingw-w64-x86_64-gtk3
Make sure that either <your msys installation folder>\mingw32\bin
or <your msys installation folder>\mingw64\bin
is in your PATH
e.g. (if you have msys32 installed at c:\msys32
, add c:\msys32\mingw32\bin
or c:\msys32\mingw64\bin
to your PATH
).
Install mingw-w64 (select the win32 threading model) and download a GTK+ SDK:
- The GNOME project hosts distributions of GTK+ 3.4 upto 3.10
- GTK+ for Windows Runtime Environment Installer: 64-bit supports GTK+ 3.14, its SDK download links can currently be found here.
Make sure both mingw's and the sdk's bin
directories are in your PATH
e.g. (assuming mingw is installed in C:\mingw-w64
and the SDK unpacked into C:\gtk
)
C:\> set PATH="C:\mingw-w64\bin;C:\gtk\bin;%PATH%"
The build script will query the installed library versions from pkg-config
and instruct rustc
via cfg
arguments to compile the appropriate set of APIs.
All the APIs available in the installed library will just work but if you attempt to use newer ones, the build will fail. Presently, Rust doesn't allow to generate custom error messages so there doesn't appear to be a way to make such errors more friendly.
Examples are providing in the gtk-rs/examples repository, you can find some tests showing off the functionality, these can be built and run as follows:
> cargo build --release
> ./target/release/gtktest
> ./target/release/cairotest
Browse the documentation at our website.
Examples of gtk applications can be found in the examples repository.
To include gtk as a cargo dependency you have to add it to your Cargo.toml:
[dependencies]
gtk = "0.0"
To implement GTK+ inheritance in rust, we implemented gtk superclasses as traits located in gtk::gtk::traits::*
. The various widgets implement these traits and live in gtk::gtk::*
.
For your convenience the various traits are reexported in the gtk::*
namespace as Gtk{trait_name}Trait
so you can just use...
extern crate gtk;
use gtk::*;
...to easily access all the gtk widgets and all traits methods:
let button = gtk::Button:new(); // You have access to the struct methods of gtk::Button aswell
// as the trait methods from gtk::traits::Button as GtkButtonTrait.
If you want yours to be added to this list, please create a Pull Request for it!
##Screenshots
Contributor you're welcome!
You probably know but Gtk+ uses its own GObject system: inherited class and interface.
To respect this design, I follow a special design on gtk:
- Interface -> Implement them on a trait with only default methods.
- Class -> Implement the construct on the class impl and other methods on a traits.
- Sub-class -> Implement all the methods on the class.
Example for GtkOrientable, GtkBox, GtkButtonBox:
GtkOrientable is an interface with all methods implemented as default method of the trait gtk::traits::Orientable.
GtkBox is a class with constructors implemented on the struct gtk::Box
, and the other method as default methods of the trait gtk::traits::Box
. So gtk::Box
implements gtk::traits::Orientable
and gtk::traits::Box
.
GtkButtonBox is a sub-class of GtkBox, the struct gtk::ButtonBox
implements all the methods of GtkButtonBox and the traits gtk::traits::Orientable
and gtk::traits::Box
.
Finally, all the gtk widgets implement the trait gtk::traits::Widget.
gtk is available under the MIT License, please refer to it.