RC file

GTK gives you a way to set a style(colors, fonts, etc..) of just about any widgets by using its own rc files.

Using an rc file, you should invoke a method defined in the Gtk::RC module:

Gtk::RC.parse 'filename'
This method parses the file assigned by filename, and use the style defined there.
Gtk::RC.parse_string string
This method parses a given string and use the style described in it.
This way is usually used with here document. for example:
Gtk::RC.parse_string <<EOS
style "default"
  fontset = "-*-helvetica-medium-r-normal--14-*,-*-fixed-medium-r-normal--14-*"
widget_class "*" style "default"

Simple examples

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"

window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL)
button = Gtk::Button.new('Hello World')
window.add button

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:

RC file format

style "style name"
style "style name" = "existing style name"
It defines a style. The content closed by {} is the definition itself. You can copy a existing style then describe differences with second syntax.

pixmap_path "path:path:..."
This is used for searching a pixmap file specified by the bg_pixmap. Searching path is separated by a colon(:).

widget_class "widget class set" style "style name"
widget "widget set" style "style name"
This assignes the style specified by a "style name" to the widget given by a "widget class set" or "widget set".

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"
This code sets whole widgets' foreground color to blue, background to stone.xpm, and font to courier 18pt bold.
Following snapshot show this sample:


Written by akaishi@ruby.freak.ne.jp
Translated by MAP2303@mapletown.net