Previous Next Contents

19. GTK的rc档

GTK有处理软体内定值的一套方法, 即使用其rc档. 这些可以用来设定颜色, 并且可以用pixmaps来设定某些物件的背景.

19.1 rc档的功能

当您的软体启动时, 您应该呼叫这一行:

void gtk_rc_parse (char *filename);

将您的档名传入做为参数. 这会使GTK来分析这个档案, 并使用设定值来设定物件的形态.

如果您希望有特别样子的物件, 但可从另一个物件做为基础来产生, 可以用这个:

void gtk_widget_set_name (GtkWidget *widget,
                          gchar *name);

传入您新产生的物件做为第一个参数, 您要给它的名字做为第二个参数. 这样的话可以让你透过rc档来改变该物件的属性.

如果我们用像以下的呼叫:

button = gtk_button_new_with_label ("Special Button");
gtk_widget_set_name (button, "special button");

则这个按钮被给了一个名字叫"special button" 并且会被指向rc档中的"special button.GtkButton". [<--- 要是我错了, 修正我!]

以下的rc档设定主视窗的属性, 并让所有子视窗继承其形态. 在程式中的程式码为:

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_name (window, "main window");

而该形态则在rc档中定义为:

widget "main window.*GtkButton*" style "main_button"

这会设定所有GtkButton物件, 成为在"main window"中的"main_buttons"的形态.

您可以看到, 这是很强很有弹性的系统. 用您最佳的想像力来看有多少好处.

19.2 GTK的rc档案格式

GTK的rc档格式如以下的范例. 这个testgtkrc档从GTK distribution而来, 但我加了点料及注解进去. 您也可以加一点解释来让使用者做微调.

有好几个指令来改变该物件的属性.

除此, 一个物件可以有好几种状态. 您可以设定不同的颜色, 图案及字形. 这些状态是:

当我们使用"fg"及"bg"来设定该物件的颜色时, 其格式为:

fg[<STATE>] = { Red, Green, Blue }

这里STATE是我们以上所说的其中之一(PRELIGHT, ACTIVE etc), 而Red, Green及Blue为0到1.0, { 1.0, 1.0, 1.0 }为白色. 它们必须要为浮点数, "1"不行, 必须是"1.0", 否则会全部变成0. "0"可以. 不是以此格式者均为"0".

bg_pixmap跟以上都很近似, 除了变成档名以外.

pixmap_path是以":"做为分隔的一串路径. 这些路径会用来搜寻您所指定的pixmap.

font指令很简单:

font = "<font name>"

比较难的是找出想要的font名称. 用xfontsel或类似的工具来找会有点帮助.

"widget_class"设定物件的类别. 这些类别在物件概论中的类别组织图有列出来.

"widget"指令指定一个已经定好的形态给一个物件. 替代所有该物件的属性. 这些物件则在程式中以gtk_widget_set_name()注册过了. 这允许您指定各别物件的属性, 而不是设定全部同一类的. 我要求您要做好文件, 这样使用者可以自行修改.

当"parent"用来当成一个属性时, 该物件会继承其父所有财产.

当您定义一个形态时, 可以指定以前已经定义过的形态给新的.

style "main_button" = "button"
{
  font = "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*"
  bg[PRELIGHT] = { 0.75, 0, 0 }
}

这个例子用"button"的形态, 产生一个"main_button"形态, 并且只改变font及背景颜色.

当然了并非所有属性都对所有物件生效. 因为该物件不见得拥有该属性.

19.3 rc档的范例

# pixmap_path "<dir 1>:<dir 2>:<dir 3>:..."
#
pixmap_path "/usr/include/X11R6/pixmaps:/home/imain/pixmaps"
#
# style <name> [= <name>]
# {
#   <option>
# }
#
# widget <widget_set> style <style_name>
# widget_class <widget_class_set> style <style_name>


# Here is a list of all the possible states.  Note that some do not apply to
# certain widgets.
#
# NORMAL - The normal state of a widget, without the mouse over top of
# it, and not being pressed etc.
#
# PRELIGHT - When the mouse is over top of the widget, colors defined
# using this state will be in effect.
#
# ACTIVE - When the widget is pressed or clicked it will be active, and
# the attributes assigned by this tag will be in effect.
#
# INSENSITIVE - When a widget is set insensitive, and cannot be
# activated, it will take these attributes.
#
# SELECTED - When an object is selected, it takes these attributes.
#
# Given these states, we can set the attributes of the widgets in each of
# these states using the following directives.
#
# fg - Sets the foreground color of a widget.
# fg - Sets the background color of a widget.
# bg_pixmap - Sets the background of a widget to a tiled pixmap.
# font - Sets the font to be used with the given widget.
#

# This sets a style called "button".  The name is not really important, as
# it is assigned to the actual widgets at the bottom of the file.

style "window"
{
  #This sets the padding around the window to the pixmap specified.
  #bg_pixmap[<STATE>] = "<pixmap filename>"
  bg_pixmap[NORMAL] = "warning.xpm"
}

style "scale"
{
  #Sets the foreground color (font color) to red when in the "NORMAL"
  #state.
  
  fg[NORMAL] = { 1.0, 0, 0 }
  
  #Sets the background pixmap of this widget to that of it's parent.
  bg_pixmap[NORMAL] = "<parent>"
}

style "button"
{
  # This shows all the possible states for a button.  The only one that
  # doesn't apply is the SELECTED state.
  
  fg[PRELIGHT] = { 0, 1.0, 1.0 }
  bg[PRELIGHT] = { 0, 0, 1.0 }
  bg[ACTIVE] = { 1.0, 0, 0 }
  fg[ACTIVE] = { 0, 1.0, 0 }
  bg[NORMAL] = { 1.0, 1.0, 0 }
  fg[NORMAL] = { .99, 0, .99 }
  bg[INSENSITIVE] = { 1.0, 1.0, 1.0 }
  fg[INSENSITIVE] = { 1.0, 0, 1.0 }
}

# In this example, we inherit the attributes of the "button" style and then
# override the font and background color when prelit to create a new
# "main_button" style.

style "main_button" = "button"
{
  font = "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*"
  bg[PRELIGHT] = { 0.75, 0, 0 }
}

style "toggle_button" = "button"
{
  fg[NORMAL] = { 1.0, 0, 0 }
  fg[ACTIVE] = { 1.0, 0, 0 }
  
  # This sets the background pixmap of the toggle_button to that of it's
  # parent widget (as defined in the application).
  bg_pixmap[NORMAL] = "<parent>"
}

style "text"
{
  bg_pixmap[NORMAL] = "marble.xpm"
  fg[NORMAL] = { 1.0, 1.0, 1.0 }
}

style "ruler"
{
  font = "-adobe-helvetica-medium-r-normal--*-80-*-*-*-*-*-*"
}

# pixmap_path "~/.pixmaps"

# These set the widget types to use the styles defined above.
# The widget types are listed in the class hierarchy, but could probably be
# just listed in this document for the users reference.

widget_class "GtkWindow" style "window"
widget_class "GtkDialog" style "window"
widget_class "GtkFileSelection" style "window"
widget_class "*Gtk*Scale" style "scale"
widget_class "*GtkCheckButton*" style "toggle_button"
widget_class "*GtkRadioButton*" style "toggle_button"
widget_class "*GtkButton*" style "button"
widget_class "*Ruler" style "ruler"
widget_class "*GtkText" style "text"

# This sets all the buttons that are children of the "main window" to
# the main_buton style.  These must be documented to be taken advantage of.
widget "main window.*GtkButton*" style "main_button"


Previous Next Contents