📄 gtk_tut-19.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Draft//EN"><HTML><HEAD><meta http-equiv="pragma" content="no-cache"><TITLE>GTK导引: GTK的rc档</TITLE></HEAD><BODY><A HREF="gtk_tut-18.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-18.html"><IMG SRC="prev.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/prev.gif" ALT="Previous"></A><A HREF="gtk_tut-20.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-20.html"><IMG SRC="next.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/next.gif" ALT="Next"></A><A HREF="gtk_tut.html#toc19" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut.html#toc19"><IMG SRC="toc.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/toc.gif" ALT="Contents"></A><HR><H2><A NAME="s19">19. GTK的rc档</A></H2><P>GTK有处理软体内定值的一套方法, 即使用其rc档.这些可以用来设定颜色, 并且可以用pixmaps来设定某些物件的背景.<P><H2><A NAME="ss19.1">19.1 rc档的功能</A></H2><P>当您的软体启动时, 您应该呼叫这一行:<BLOCKQUOTE><CODE><PRE>void gtk_rc_parse (char *filename);</PRE></CODE></BLOCKQUOTE><P>将您的档名传入做为参数.这会使GTK来分析这个档案,并使用设定值来设定物件的形态.<P>如果您希望有特别样子的物件, 但可从另一个物件做为基础来产生, 可以用这个:<BLOCKQUOTE><CODE><PRE>void gtk_widget_set_name (GtkWidget *widget, gchar *name);</PRE></CODE></BLOCKQUOTE><P>传入您新产生的物件做为第一个参数,您要给它的名字做为第二个参数.这样的话可以让你透过rc档来改变该物件的属性.<P>如果我们用像以下的呼叫:<P><BLOCKQUOTE><CODE><PRE>button = gtk_button_new_with_label ("Special Button");gtk_widget_set_name (button, "special button");</PRE></CODE></BLOCKQUOTE><P>则这个按钮被给了一个名字叫"special button" 并且会被指向rc档中的"special button.GtkButton". [<--- 要是我错了, 修正我!]<P>以下的rc档设定主视窗的属性, 并让所有子视窗继承其形态.在程式中的程式码为:<P><BLOCKQUOTE><CODE><PRE>window = gtk_window_new (GTK_WINDOW_TOPLEVEL);gtk_widget_set_name (window, "main window");</PRE></CODE></BLOCKQUOTE><P>而该形态则在rc档中定义为:<P><BLOCKQUOTE><CODE><PRE>widget "main window.*GtkButton*" style "main_button"</PRE></CODE></BLOCKQUOTE><P>这会设定所有GtkButton物件, 成为在"main window"中的"main_buttons"的形态.<P>您可以看到, 这是很强很有弹性的系统.用您最佳的想像力来看有多少好处.<P><H2><A NAME="ss19.2">19.2 GTK的rc档案格式</A></H2><P>GTK的rc档格式如以下的范例.这个testgtkrc档从GTK distribution而来,但我加了点料及注解进去.您也可以加一点解释来让使用者做微调.<P>有好几个指令来改变该物件的属性.<UL><LI>fg - 前景颜色.</LI><LI>bg - 背景颜色.</LI><LI>bg_pixmap - 背景图片pixmap.</LI><LI>font - 字型.</LI></UL><P>除此, 一个物件可以有好几种状态.您可以设定不同的颜色, 图案及字形.这些状态是:<UL><LI>NORMAL - 物件一般的状态, 没有滑鼠滑过, 没有被按下.</LI><LI>PRELIGHT - 滑鼠滑过该物件.</LI><LI>ACTIVE - 当该物件被压下或按下, 该视窗会生效.</LI><LI>INSENSITIVE - 当该物件被设为失效.</LI><LI>SELECTED - 当物件被选择.</LI></UL><P>当我们使用"fg"及"bg"来设定该物件的颜色时, 其格式为:<BLOCKQUOTE><CODE><PRE>fg[<STATE>] = { Red, Green, Blue }</PRE></CODE></BLOCKQUOTE><P>这里STATE是我们以上所说的其中之一(PRELIGHT, ACTIVE etc), 而Red, Green及Blue为0到1.0, { 1.0, 1.0, 1.0 }为白色.它们必须要为浮点数, "1"不行, 必须是"1.0", 否则会全部变成0."0"可以. 不是以此格式者均为"0".<P>bg_pixmap跟以上都很近似, 除了变成档名以外.<P>pixmap_path是以":"做为分隔的一串路径.这些路径会用来搜寻您所指定的pixmap.<P><P>font指令很简单:<BLOCKQUOTE><CODE><PRE>font = "<font name>"</PRE></CODE></BLOCKQUOTE><P>比较难的是找出想要的font名称.用xfontsel或类似的工具来找会有点帮助.<P>"widget_class"设定物件的类别. 这些类别在物件概论中的类别组织图有列出来.<P>"widget"指令指定一个已经定好的形态给一个物件.替代所有该物件的属性.这些物件则在程式中以gtk_widget_set_name()注册过了.这允许您指定各别物件的属性, 而不是设定全部同一类的.我要求您要做好文件, 这样使用者可以自行修改.<P>当"<CODE>parent</CODE>"用来当成一个属性时,该物件会继承其父所有财产.<P>当您定义一个形态时, 可以指定以前已经定义过的形态给新的.<BLOCKQUOTE><CODE><PRE>style "main_button" = "button"{ font = "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*" bg[PRELIGHT] = { 0.75, 0, 0 }}</PRE></CODE></BLOCKQUOTE><P>这个例子用"button"的形态,产生一个"main_button"形态,并且只改变font及背景颜色.<P>当然了并非所有属性都对所有物件生效. 因为该物件不见得拥有该属性. <P><H2><A NAME="ss19.3">19.3 rc档的范例</A></H2><P><P><BLOCKQUOTE><CODE><PRE># 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"</PRE></CODE></BLOCKQUOTE><P><HR><A HREF="gtk_tut-18.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-18.html"><IMG SRC="prev.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/prev.gif" ALT="Previous"></A><A HREF="gtk_tut-20.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-20.html"><IMG SRC="next.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/next.gif" ALT="Next"></A><A HREF="gtk_tut.html#toc19" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut.html#toc19"><IMG SRC="toc.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/toc.gif" ALT="Contents"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -