Expand description
§Rust GTK 3 bindings
This project is UNMAINTAINED. Please take a look at gtk4-rs instead!
Rust bindings and wrappers for GTK 3, part of gtk3-rs, a multi-platform GUI toolkit. It is a part of gtk-rs.
GTK 3.22.30 is the lowest supported version for the underlying library.
§Minimum supported Rust version
Currently, the minimum supported Rust version is 1.70.0
.
§Building
gtk expects GTK, GLib and Cairo development files to be installed on your system. See the GTK installation page.
§Using
We recommend using crates from crates.io, as demonstrated here.
If you want to track the bleeding edge, use the git dependency instead:
[dependencies]
gtk = { git = "https://github.com/gtk-rs/gtk3-rs.git" }
Avoid mixing versioned and git crates like this:
# This will not compile
[dependencies]
gtk = "0.13"
gtk = { git = "https://github.com/gtk-rs/gtk3-rs.git" }
§“Hello, World!” example program
//!
GTK needs to be initialized before use by calling init
. Creating an
Application
will call init
for you.
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow};
fn main() {
let app = Application::builder()
.application_id("org.example.HelloWorld")
.build();
app.connect_activate(|app| {
// We create the main window.
let win = ApplicationWindow::builder()
.application(app)
.default_width(320)
.default_height(200)
.title("Hello, World!")
.build();
// Don't forget to make all widgets visible.
win.show_all();
});
app.run();
}
§The main loop
In a typical GTK application you set up the UI, assign signal handlers and run the main event loop.
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
fn main() {
let application = Application::builder()
.application_id("com.example.FirstGtkApp")
.build();
application.connect_activate(|app| {
let window = ApplicationWindow::builder()
.application(app)
.title("First GTK Program")
.default_width(350)
.default_height(70)
.build();
let button = Button::with_label("Click me!");
button.connect_clicked(|_| {
eprintln!("Clicked!");
});
window.add(&button);
window.show_all();
});
application.run();
}
§Threads
GTK is not thread-safe. Accordingly, none of this crate’s structs implement
Send
or Sync
.
The thread where init
was called is considered the main thread. OS X has
its own notion of the main thread and init
must be called on that thread.
After successful initialization, calling any gtk
or gdk
functions
(including init
) from other threads will panic
.
Any thread can schedule a closure to be run by the main loop on the main
thread via glib::idle_add
or glib::timeout_add
. While
working with GTK you might need the glib::idle_add_local
or glib::timeout_add_local
version without the
Send
bound. Those may only be called from the main thread.
§Panics
The gtk
and gdk
crates have some run-time safety and contract checks.
-
Any constructor or free function will panic if called before
init
or on a non-main thread. -
Any
&str
or&Path
parameter with an interior null (\0
) character will cause a panic. -
Some functions will panic if supplied out-of-range integer parameters. All such cases will be documented individually but they are not yet.
-
A panic in a closure that handles signals or in any other closure passed to a
gtk
function will abort the process.
§Features
§Library versions
By default this crate provides only GTK 3.22.30 APIs. You can access additional
functionality by selecting one of the v3_24
, etc. features.
Cargo.toml
example:
[dependencies.gtk]
version = "0.x.y"
features = ["v3_24"]
Take care when choosing the version to target: some of your users might not have easy access to the latest ones. The higher the version, the fewer users will have it installed.
§Documentation
Most of this documentation is generated from the C API.
Until all parts of the documentation have been reviewed there will be incongruities with the actual Rust API.
Generate the docs:
> RUSTFLAGS="--cfg docsrs" cargo doc
(if the installed GTK+ version is lower than 3.16, adjust the feature name accordingly).
§Contribute
Contributor you’re welcome!
See the general bindings documentation.
Most of the bindings (src/auto
) are generated by gir using this configuration file. After editing Gir.toml
the sources can be regenerated with
> make gir
When opening a PR please put the changes to the src/auto
directory in a separate commit.
You may also wish to run cargo clippy -- -D warnings
and check that you’re clean because
otherwise you may be surprised when CI fails.
§See Also
But also:
§License
gtk is available under the MIT License, please refer to it.
Re-exports§
pub use ffi;
pub use atk;
pub use cairo;
pub use gdk;
pub use gdk_pixbuf;
pub use gio;
pub use glib;
pub use pango;
Modules§
- builders
- Builder pattern types.
- prelude
- Traits and essential types intended for blanket imports.
- subclass
- xlib
Structs§
- About
Dialog - Accel
Flags - Accel
Group - Accel
Label - Action
Bar - Actionable
- Adjustment
- Allocation
- AppChooser
- AppChooser
Button - AppChooser
Dialog - AppChooser
Widget - Application
- Application
Inhibit Flags - Application
Window - Aspect
Frame - Assistant
- Bin
- Border
- Box
- Buildable
- Builder
- Button
- Button
Box - Calendar
- Calendar
Display Options - Cell
Area - Cell
Area Box - Cell
Area Context - Cell
Editable - Cell
Layout - Cell
Renderer - Cell
Renderer Accel - Cell
Renderer Combo - Cell
Renderer Pixbuf - Cell
Renderer Progress - Cell
Renderer Spin - Cell
Renderer Spinner - Cell
Renderer State - Cell
Renderer Text - Cell
Renderer Toggle - Cell
View - Check
Button - Check
Menu Item - Clipboard
- Color
Button - Color
Chooser - Color
Chooser Dialog - Color
Chooser Widget - Combo
Box - Combo
BoxText - Container
- CssProvider
- CssSection
- Dest
Defaults - Dialog
- Dialog
Flags - Drawing
Area - Editable
- Entry
- Entry
Buffer - Entry
Completion - Event
Box - Event
Controller - Expander
- File
Chooser - File
Chooser Button - File
Chooser Dialog - File
Chooser Native - File
Chooser Widget - File
Filter - File
Filter Flags - File
Filter Info - Fixed
- FlowBox
- Flow
BoxChild - Font
Button - Font
Chooser - Font
Chooser Dialog - Font
Chooser Widget - Frame
- GLArea
- Gesture
- Gesture
Drag - Gesture
Long Press - Gesture
Multi Press - Gesture
Pan - Gesture
Rotate - Gesture
Single - Gesture
Swipe - Gesture
Zoom - Grid
- Header
Bar - IMContext
- IMContext
Simple - IMMulticontext
- Icon
Info - Icon
Lookup Flags - Icon
Theme - Icon
View - Image
- InfoBar
- Input
Hints - Invisible
- Junction
Sides - Label
- Layout
- Level
Bar - Link
Button - ListBox
- List
BoxRow - List
Store - Lock
Button - Menu
- MenuBar
- Menu
Button - Menu
Item - Menu
Shell - Menu
Tool Button - Message
Dialog - Misc
- Model
Button - Mount
Operation - Native
Dialog - Notebook
- Offscreen
Window - Orientable
- Overlay
- PadAction
Entry - PadController
- Page
Range - Page
Setup - Paned
- Paper
Size - Places
Open Flags - Places
Sidebar - Plug
gdk_backend="x11"
- Popover
- Popover
Menu - Print
Context - Print
Operation - Print
Operation Preview - Print
Settings - Progress
Bar - Radio
Button - Radio
Menu Item - Radio
Tool Button - Range
- Recent
Chooser - Recent
Chooser Dialog - Recent
Chooser Menu - Recent
Chooser Widget - Recent
Data - Recent
Filter - Recent
Filter Flags - Recent
Info - Recent
Manager - Rectangle
- Region
Flags - Requisition
- Revealer
- Scale
- Scale
Button - Scrollable
- Scrollbar
- Scrolled
Window - Search
Bar - Search
Entry - Selection
Data - Separator
- Separator
Menu Item - Separator
Tool Item - Settings
- Shortcut
Label - Shortcuts
Group - Shortcuts
Section - Shortcuts
Shortcut - Shortcuts
Window - Size
Group - Socket
gdk_backend="x11"
- Spin
Button - Spinner
- Stack
- Stack
Sidebar - Stack
Switcher - State
Flags - Statusbar
- Style
Context - Style
Context Print Flags - Style
Properties - Style
Provider - Switch
- Target
Entry - Target
Flags - Target
List - Text
Attributes - Text
Buffer - Text
Child Anchor - Text
Iter - Text
Mark - Text
Search Flags - TextTag
- Text
TagTable - Text
View - Tick
Callback Id - Toggle
Button - Toggle
Tool Button - Tool
Button - Tool
Item - Tool
Item Group - Tool
Palette - Tool
Palette Drag Targets - Tool
Shell - Toolbar
- Tooltip
- Tree
Drag Dest - Tree
Drag Source - Tree
Iter - Tree
Model - Tree
Model Filter - Tree
Model Flags - Tree
Model Sort - Tree
Path - Tree
RowReference - Tree
Selection - Tree
Sortable - Tree
Store - Tree
View - Tree
View Column - Viewport
- Volume
Button - Widget
- Widget
Path - Window
- Window
Group
Enums§
- Align
- Arrow
Type - Assistant
Page Type - Baseline
Position - Border
Style - Builder
Error - Button
BoxStyle - Button
Role - Buttons
Type - Cell
Renderer Accel Mode - Cell
Renderer Mode - Corner
Type - CssProvider
Error - CssSection
Type - Delete
Type - Direction
Type - Drag
Result - Entry
Icon Position - Event
Sequence State - File
Chooser Action - File
Chooser Confirmation - File
Chooser Error - Icon
Size - Icon
Theme Error - Icon
View Drop Position - Image
Type - Input
Purpose - Justification
- Level
BarMode - License
- Menu
Direction Type - Message
Type - Movement
Step - Notebook
Tab - Number
UpLayout - Orientation
- Pack
Direction - Pack
Type - PadAction
Type - Page
Orientation - PageSet
- PanDirection
- Policy
Type - Popover
Constraint - Position
Type - Print
Duplex - Print
Error - Print
Operation Action - Print
Operation Result - Print
Pages - Print
Quality - Print
Status - Propagation
Phase - Recent
Chooser Error - Recent
Manager Error - Recent
Sort Type - Relief
Style - Resize
Mode - Response
Type - Revealer
Transition Type - Scroll
Step - Scroll
Type - Scrollable
Policy - Selection
Mode - Sensitivity
Type - Shadow
Type - Shortcut
Type - Size
Group Mode - Size
Request Mode - Sort
Column - Sort
Type - Spin
Button Update Policy - Spin
Type - Stack
Transition Type - Text
Direction - Text
Extend Selection - Text
View Layer - Text
Window Type - Toolbar
Style - Tree
View Column Sizing - Tree
View Drop Position - Tree
View Grid Lines - Unit
- Widget
Help Type - Window
Position - Window
Type - Wrap
Mode
Constants§
- STYLE_
PROVIDER_ PRIORITY_ APPLICATION - STYLE_
PROVIDER_ PRIORITY_ FALLBACK - STYLE_
PROVIDER_ PRIORITY_ SETTINGS - STYLE_
PROVIDER_ PRIORITY_ THEME - STYLE_
PROVIDER_ PRIORITY_ USER
Statics§
- LEVEL_
BAR_ OFFSET_ FULL - LEVEL_
BAR_ OFFSET_ HIGH - LEVEL_
BAR_ OFFSET_ LOW - PAPER_
NAME_ A3 - PAPER_
NAME_ A4 - PAPER_
NAME_ A5 - PAPER_
NAME_ B5 - PAPER_
NAME_ EXECUTIVE - PAPER_
NAME_ LEGAL - PAPER_
NAME_ LETTER - PRINT_
SETTINGS_ COLLATE - PRINT_
SETTINGS_ DEFAULT_ SOURCE - PRINT_
SETTINGS_ DITHER - PRINT_
SETTINGS_ DUPLEX - PRINT_
SETTINGS_ FINISHINGS - PRINT_
SETTINGS_ MEDIA_ TYPE - PRINT_
SETTINGS_ NUMBER_ UP - PRINT_
SETTINGS_ NUMBER_ UP_ LAYOUT - PRINT_
SETTINGS_ N_ COPIES - PRINT_
SETTINGS_ ORIENTATION - PRINT_
SETTINGS_ OUTPUT_ BASENAME - PRINT_
SETTINGS_ OUTPUT_ BIN - PRINT_
SETTINGS_ OUTPUT_ DIR - PRINT_
SETTINGS_ OUTPUT_ FILE_ FORMAT - PRINT_
SETTINGS_ OUTPUT_ URI - PRINT_
SETTINGS_ PAGE_ RANGES - PRINT_
SETTINGS_ PAGE_ SET - PRINT_
SETTINGS_ PAPER_ FORMAT - PRINT_
SETTINGS_ PAPER_ HEIGHT - PRINT_
SETTINGS_ PAPER_ WIDTH - PRINT_
SETTINGS_ PRINTER - PRINT_
SETTINGS_ PRINTER_ LPI - PRINT_
SETTINGS_ PRINT_ PAGES - PRINT_
SETTINGS_ QUALITY - PRINT_
SETTINGS_ RESOLUTION - PRINT_
SETTINGS_ RESOLUTION_ X - PRINT_
SETTINGS_ RESOLUTION_ Y - PRINT_
SETTINGS_ REVERSE - PRINT_
SETTINGS_ SCALE - PRINT_
SETTINGS_ USE_ COLOR - PRINT_
SETTINGS_ WIN32_ DRIVER_ EXTRA - PRINT_
SETTINGS_ WIN32_ DRIVER_ VERSION - STYLE_
CLASS_ ACCELERATOR - STYLE_
CLASS_ ARROW - STYLE_
CLASS_ BACKGROUND - STYLE_
CLASS_ BOTTOM - STYLE_
CLASS_ BUTTON - STYLE_
CLASS_ CALENDAR - STYLE_
CLASS_ CELL - STYLE_
CLASS_ CHECK - STYLE_
CLASS_ COMBOBOX_ ENTRY - STYLE_
CLASS_ CONTEXT_ MENU - STYLE_
CLASS_ CSD - STYLE_
CLASS_ CURSOR_ HANDLE - STYLE_
CLASS_ DEFAULT - STYLE_
CLASS_ DESTRUCTIVE_ ACTION - STYLE_
CLASS_ DIM_ LABEL - STYLE_
CLASS_ DND - STYLE_
CLASS_ DOCK - STYLE_
CLASS_ ENTRY - STYLE_
CLASS_ ERROR - STYLE_
CLASS_ EXPANDER - STYLE_
CLASS_ FLAT - STYLE_
CLASS_ FRAME - STYLE_
CLASS_ GRIP - STYLE_
CLASS_ HEADER - STYLE_
CLASS_ HIGHLIGHT - STYLE_
CLASS_ HORIZONTAL - STYLE_
CLASS_ IMAGE - STYLE_
CLASS_ INFO - STYLE_
CLASS_ INLINE_ TOOLBAR - STYLE_
CLASS_ INSERTION_ CURSOR - STYLE_
CLASS_ LABEL - STYLE_
CLASS_ LEFT - STYLE_
CLASS_ LEVEL_ BAR - STYLE_
CLASS_ LINKED - STYLE_
CLASS_ LIST - STYLE_
CLASS_ LIST_ ROW - STYLE_
CLASS_ MARK - STYLE_
CLASS_ MENU - STYLE_
CLASS_ MENUBAR - STYLE_
CLASS_ MENUITEM - STYLE_
CLASS_ MESSAGE_ DIALOG - STYLE_
CLASS_ MONOSPACE - STYLE_
CLASS_ NEEDS_ ATTENTION - STYLE_
CLASS_ NOTEBOOK - STYLE_
CLASS_ OSD - STYLE_
CLASS_ OVERSHOOT - STYLE_
CLASS_ PANE_ SEPARATOR - STYLE_
CLASS_ PAPER - STYLE_
CLASS_ POPOVER - STYLE_
CLASS_ POPUP - STYLE_
CLASS_ PRIMARY_ TOOLBAR - STYLE_
CLASS_ PROGRESSBAR - STYLE_
CLASS_ PULSE - STYLE_
CLASS_ QUESTION - STYLE_
CLASS_ RADIO - STYLE_
CLASS_ RAISED - STYLE_
CLASS_ READ_ ONLY - STYLE_
CLASS_ RIGHT - STYLE_
CLASS_ RUBBERBAND - STYLE_
CLASS_ SCALE - STYLE_
CLASS_ SCALE_ HAS_ MARKS_ ABOVE - STYLE_
CLASS_ SCALE_ HAS_ MARKS_ BELOW - STYLE_
CLASS_ SCROLLBAR - STYLE_
CLASS_ SCROLLBARS_ JUNCTION - STYLE_
CLASS_ SEPARATOR - STYLE_
CLASS_ SIDEBAR - STYLE_
CLASS_ SLIDER - STYLE_
CLASS_ SPINBUTTON - STYLE_
CLASS_ SPINNER - STYLE_
CLASS_ STATUSBAR - STYLE_
CLASS_ SUBTITLE - STYLE_
CLASS_ SUGGESTED_ ACTION - STYLE_
CLASS_ TITLE - STYLE_
CLASS_ TITLEBAR - STYLE_
CLASS_ TOOLBAR - STYLE_
CLASS_ TOOLTIP - STYLE_
CLASS_ TOP - STYLE_
CLASS_ TOUCH_ SELECTION - STYLE_
CLASS_ TROUGH - STYLE_
CLASS_ UNDERSHOOT - STYLE_
CLASS_ VERTICAL - STYLE_
CLASS_ VIEW - STYLE_
CLASS_ WARNING - STYLE_
CLASS_ WIDE - STYLE_
PROPERTY_ BACKGROUND_ COLOR - STYLE_
PROPERTY_ BACKGROUND_ IMAGE - STYLE_
PROPERTY_ BORDER_ COLOR - STYLE_
PROPERTY_ BORDER_ RADIUS - STYLE_
PROPERTY_ BORDER_ STYLE - STYLE_
PROPERTY_ BORDER_ WIDTH - STYLE_
PROPERTY_ COLOR - STYLE_
PROPERTY_ FONT - STYLE_
PROPERTY_ MARGIN - STYLE_
PROPERTY_ PADDING
Traits§
Functions§
- accel_
groups_ activate - accel_
groups_ from_ object - accelerator_
get_ default_ mod_ mask - accelerator_
get_ label - accelerator_
get_ label_ with_ keycode - accelerator_
name - accelerator_
name_ with_ keycode - accelerator_
parse - accelerator_
parse_ with_ keycode - accelerator_
set_ default_ mod_ mask - accelerator_
valid - binary_
age - bindings_
activate - bindings_
activate_ event - cairo_
should_ draw_ window - cairo_
transform_ to_ window - check_
version - current_
event - current_
event_ device - current_
event_ state - current_
event_ time - debug_
flags - default_
language - device_
grab_ add - device_
grab_ remove - disable_
setlocale - event_
widget - events_
pending - false_
- grab_
get_ current - init
- Tries to initialize GTK+.
- interface_
age - is_
initialized - Returns
true
if GTK has been initialized. - is_
initialized_ main_ thread - Returns
true
if GTK has been initialized and this is the main thread. - locale_
direction - main
- main_
do_ event - main_
iteration - main_
iteration_ do - main_
level - main_
quit - major_
version - micro_
version - minor_
version - print_
run_ page_ setup_ dialog - print_
run_ page_ setup_ dialog_ async - propagate_
event - render_
activity - render_
arrow - render_
background - render_
background_ get_ clip - render_
check - render_
expander - render_
extension - render_
focus - render_
frame - render_
frame_ gap - render_
handle - render_
icon - render_
icon_ surface - render_
insertion_ cursor - render_
layout - render_
line - render_
option - render_
slider - rgb_
to_ hsv - selection_
add_ target - selection_
clear_ targets - selection_
convert - selection_
owner_ set - selection_
owner_ set_ for_ display - selection_
remove_ all - set_
debug_ flags - set_
initialized ⚠ - Informs this crate that GTK has been initialized and the current thread is the main one.
- show_
uri_ on_ window - targets_
include_ image - targets_
include_ rich_ text - targets_
include_ text - targets_
include_ uri - test_
find_ label - test_
find_ sibling - test_
find_ widget - test_
register_ all_ types - test_
widget_ send_ key - test_
widget_ wait_ for_ draw - tree_
get_ row_ drag_ data - tree_
set_ row_ drag_ data - true_