Using an rc file, you should invoke a method defined in the Gtk::RC module:
Gtk::RC.parse
'filename'
Gtk::RC.parse_string
string
Gtk::RC.parse_string <<EOS style "default" { fontset = "-*-helvetica-medium-r-normal--14-*,-*-fixed-medium-r-normal--14-*" } widget_class "*" style "default" EOS
Generally speaking, in the GTK's rc file, style
statement defines a style, and widget
or widget_class
statement assigns its style to a widget.
Following sample shows a pink button:
sample script rc-hello.rb:
require 'gtk' Gtk::RC.parse_string <<EOS style "button" { bg[NORMAL] = {1.0, 0.8, 0.8} } widget_class "*GtkButton" style "button" EOS window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL) button = Gtk::Button.new('Hello World') window.add button button.show window.show Gtk.main
result screen shot:
The "Gtk::RC.parse_string
" loads GTK's rc strings and sets the style. The following documents to the line of EOS is passed to Gtk::RC.parse_string
as an argument and parsed by GTK.
Here is a description of the string:
style "button"
{
and }
.
bg[NORMAL] = {1.0, 0.8, 0.8}
widget_class "*GtkButton" style "button"
style "style name"
style "style name" = "existing style name"
To specify a color:
fg[state] = {red, green, blue} # set a foreground color of a widget.
bg[state] = {red, green, blue} # set a background color of a widget.
fg[NORMAL] = {0.0, 0.0, 0.0}
bg[NORMAL] = {0.8, 0.8, 0.8}
state:
NORMAL -- a normal state of the widget.
PRELIGHT -- a mouse cursor is over the widget(but not pressed).
ACTIVE -- the widget is pressed or clicked.
INSENSITIVE -- non-active state of the widget.
SELECTED -- the widget is selected.
To specify a background pixmap:
bg_pixmap[state] = "filename"
font:
font = "font name"
fontset = "font name"
pixmap_path "path:path:..."
widget_class "widget class set" style "style name"
widget "widget set" style "style name"
The directive of widget_class
uses a class name to determine widgets. Note that the word of ``class name'' in this context is not Ruby's class name but GTK's class name, which is equals to Ruby's class name except "::".
(ex: The GTK's classname of Gtk::Button is "GtkButton".)
The directive of widget
is specified by name. widget name is set by a method of Gtk::Widget#set_name. On my experience, if you don't set it, it becomes same to class name.
A widget class set or a widget set is specified widget has-a tree. For example, if a Gtk::Button is added to Gtk::Window, the button is specified by "GtkWindow.GtkButton" in a class name form. A wildcard `*' is also available in this syntax, so "*GtkButton" expresses all widget of GtkButton class.
Gtk::RC.parse_string <<EOS pixmap_path "." style "default" { fg[NORMAL] = {0.0, 0.0, 1.0} bg_pixmap[NORMAL] = "stone.xpm" font = "-adobe-courier-bold-r-normal--18-*" } widget_class "*" style "default" EOSThis code sets whole widgets' foreground color to blue, background to stone.xpm, and font to courier 18pt bold.