⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtk-server_tutorial.html

📁 gtk_server的源代码
💻 HTML
📖 第 1 页 / 共 4 页
字号:
------------------<br><br>Now, we need to create a file called "gtkrc" which contains thecolordefintions for our button. This file resides in the same directoryas our script and may look like this:<br><br>------------------<br><br><span style="font-family: courier new,courier,monospace;"><small>style"mystyle"</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">{</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">&nbsp;bg[NORMAL] = { 65535, 0, 0 }</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">&nbsp;bg[PRELIGHT] = { 40000, 40000, 40000 }</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">&nbsp;bg[ACTIVE] = { 0, 65535, 0 }</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">}</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">widget"*.*.exitbutton*" style "mystyle"</small></span><br><br>------------------<br><br>Again, the command <span style="font-family: courier new,courier,monospace;"><small>gtk_rc_parse</small></span>reads the file and puts the widgets with the name "exitbutton" to thementioned colors. Here, the colors are defined as RGB triplets. Now,when the button is in a normal state, the color will be red; when thebutton is pressed, the color will be green. Finally, when the mousemoves over the button, the color will be grey.<br><br>There are more possibilities using GTK rc-files, like coloring a wholeset of widgets to a specific color. Please consult the GTK manual at <a href="http://www.gtk.org">http://www.gtk.org</a> for detailedexplanations on rc-files.<br><br><br><hr style="width: 100%; height: 2px;"><a name="chapter10"></a><h2>Chapter 10. Connecting to the GTK-server with TCP or UDP.</h2>Instead of using 2-way pipes, it is also possible to connect yourscript by TCP or UDP.<br><br>To start the GTK-server using TCP sockets, the argumentto the server must be of the format &lt;tcp=ipaddress:port&gt;. With UDP theformat is almost the same: &lt;udp=ipaddress:port&gt;.<br><br>For example:<br><br><span style="font-family: courier new,courier,monospace;"><small>gtk-server -tcp=localhost:50000</small></span><br><br>Now the server is started and it will listen to your localhost IPaddress (127.0.0.1). Your script must connect to TCP port 50000. If werewrite our "Hello world" application to a TCP connection, the scriptwould look like this:<br><br>------------------<br><br><small><span style="font-family: courier new,courier,monospace;">#!/usr/bin/gawk-f</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">#</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;"># AWK Helloworld application using GTK</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">#</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">BEGIN{</span><br style="font-family: courier new,courier,monospace;"><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">system("gtk-server-tcp=localhost:50000 &amp;") </span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;"><br>GTK = "/inet/tcp/0/localhost/50000" </span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;"><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">print"gtk_init NULL NULL" |&amp; GTK; GTK |&amp; getline</span><br style="font-family: courier new,courier,monospace;"><span style="font-family: courier new,courier,monospace;">print"gtk_window_new 0" |&amp; GTK; GTK |&amp; getline WINDOW</span><br style="font-family: courier new,courier,monospace;">print "gtk_window_set_title " WINDOW " \"This is a title\"" |&amp; GTK;GTK |&amp; getline<br>print "gtk_table_new 30 30 1" |&amp; GTK; GTK |&amp; getline TABLE<br>print "gtk_container_add " WINDOW " " TABLE |&amp; GTK; GTK |&amp;getline<br>print "gtk_label_new \"Hello world\"" |&amp; GTK; GTK |&amp; getline LABEL<br>print "gtk_table_attach_defaults " TABLE " " LABEL " 1 29 3 7"|&amp; GTK; GTK |&amp; getline<br></span><span style="font-family: courier new,courier,monospace;"> print"gtk_button_new_with_label Exit" |&amp; GTK; GTK |&amp; getline BUTTON<br>print "gtk_table_attach_defaults " TABLE " " BUTTON " 20 28 2327" |&amp; GTK; GTK |&amp; getline<br></span><span style="font-family: courier new,courier,monospace;">print"gtk_widget_show " LABEL |&amp; GTK; GTK |&amp; getline<br></span><span style="font-family: courier new,courier,monospace;">print"gtk_widget_show " BUTTON |&amp; GTK; GTK |&amp; getline<br></span><span style="font-family: courier new,courier,monospace;">print"gtk_widget_show " TABLE |&amp; GTK; GTK |&amp; getline</span><br></small> <span style="font-family: courier new,courier,monospace;"><small>print"gtk_widget_show " WINDOW |&amp; GTK; GTK |&amp; getline<br><br>EVENT = 0<br></small></span><span style="font-family: courier new,courier,monospace;"><small> <br>do {<br><br>&nbsp;&nbsp;&nbsp; print "gtk_main_iteration" |&amp; GTK; GTK |&amp;getline<br>&nbsp;&nbsp;&nbsp; print "gtk_server_callback 0" |&amp; GTK;GTK |&amp; getline EVENT<br>&nbsp;&nbsp;&nbsp; </small></span><span style="font-family: courier new,courier,monospace;"><small> <br>} while (EVENT != BUTTON &amp;&amp; EVENT != WINDOW)<br><br>close(GTK)<br>fflush("")<br>}</small> <br></span><br>------------------<br><br>As of version 3.1, the GNU AWK script language has built-in networksupport. The above script connects AWK to the server by a TCP port. Itmight happen that the AWK interpreter executes to fast; therefore,afterstartup of the GTK-server it might be necessary to wait for a second,in order for the GTK-server to initialize.<br><br>Finally it is possible to connect multiple scripts to 1 GTK-server using TCP(so not UDP). Inthis way you can avoid running multiple instances of the GTK-server inmemory, each of which will consume a TCP port. (This functionality isnot available in a Windows environment, since the Windows OS does notsupport forking.) To enable this functionality, start the GTK-serverlike this:<br><br><small><span style="font-family: courier new,courier,monospace;">gtk-server -tcplocalhost:50000:16</span></small><br><br>The number '16' defines the maximum amount of client scripts allowed touse the GTK-server at the same time. You can use any number here (themaximum depending on your OS and hardware). It is a nice idea to startthe GTK-server this way during boot time. You can create a "rc" scriptto take care of that, so the GTK-server will run permanently in thebackground. When necessary, any of your scripts can connect by TCP, soyou have always have access to GTK GUI's.<br><br><br><hr style="width: 100%; height: 2px;"><a name="chapter11"></a><h2>Chapter 11. Connecting to the GTK-server with FIFO.</h2>Finally you can connect to the GTK-server by using a named pipe. A named pipeis a file with a special feature: it can deliver messages to other processes orprograms. The first message delivered to the pipe is also the first to beread by the other side; and vice versa. Hence the abbreviation FIFO, whichactually means "First In First Out".<br><br>To start the GTK-server using FIFO pipes, the argument to the server must be: fifo=&lt;name&gt;. The GTK-server will create a named pipe (which in fact is a file on your hard disk) with the filename &lt;name&gt;.<br><br>For example:<br><br><span style="font-family: courier new,courier,monospace;"><small>gtk-server-fifo=mypipe</small></span><br><br>In Windows, the syntax is different. The GTK-server will create two independentnamed pipes, which also behave like a file. The named pipes have predefined names,so these do not have to be specified. To start the GTK-server withnamed pipes in Windows, just enter:<br><br><span style="font-family: courier new,courier,monospace;"><small>gtk-server-fifo</small></span><br><br>Now, the client script first has to open the named pipe "\\.\pipe\out", and secondthe named pipe "\\.\pipe\in". As mentioned, the names of these pipes are predefined. Also the order in which to openthe pipes is important! The 'out' pipe must be opened first, and is used by the client script to write information to; the 'in' pipe must be used to read information from.<br><br>Let's continue with our AWK program. The AWK program must be adjusted a little bit, in order to use the named pipe. Itis already clear that a named pipe behaves like a file. So to enable communication,we must write and read from this file. In AWK the program will look as follows:<br><br><small><span style="font-family: courier new,courier,monospace;">function GTK(call)<br>{<br>print call &gt;&gt; "mypipe"<br>close("mypipe", "to")<br>getline &lt; "mypipe"<br>close("mypipe", "from")<br>return $0<br>}<br><br>#-------------------------------------------<br><br>BEGIN{<br>system("gtk-server -fifo=mypipe &amp;")<br>GTK("gtk_init NULL NULL")<br>WINDOW = GTK("gtk_window_new 0")<br>GTK("gtk_window_set_title " WINDOW " \"This is a title\"")<br>TABLE = GTK("gtk_table_new 30 30 1")<br>GTK("gtk_container_add " WINDOW " " TABLE)<br>LABEL = GTK("gtk_label_new \"Hello world\"")<br>GTK("gtk_table_attach_defaults " TABLE " " LABEL " 1 29 3 7")<br>BUTTON = GTK("gtk_button_new_with_label Exit")<br>GTK("gtk_table_attach_defaults " TABLE " " BUTTON " 20 28 23 27")<br>GTK("gtk_widget_show_all " WINDOW)<br><br>EVENT = 0<br><br>do {<br><br>&nbsp;&nbsp;&nbsp; EVENT = GTK("gtk_server_callback WAIT")<br><br>} while (EVENT != BUTTON &amp;&amp; EVENT != WINDOW)<br><br>print "gtk_server_exit" &gt;&gt; "gtk"<br>}<br></small></span><br>As you can see, the structure of the program has changed. The communication partis put into a separate function now (called 'GTK'). The two 'close' statements in thisfunction are needed to let AWK flush it's IO buffers.<br><br>Also, instead of showing each GTK widget separately, the GTK function "gtk_widget_show_all"will show the parent widget WINDOW and all children attached to it. Do not forget to put thisnew function into the configfile! You should know how to define it by now.<br><br>The mainloop has changed, now the 'gtk_server_callback' function has the argument 'WAIT',which will take over the previous 'gtk_main_iteration'. So the callback function willwait, until an event has occured. It returns to the AWK program with the widget ID whichemitted the signal.<br><br>Finally the GTK library is exited by printing a plain 'gtk_server_exit' to the pipe, withoutwaiting for answer from the GTK-server.<br><br><hr style="width: 100%; height: 2px;"><a name="chapter12"></a><h2>Chapter 12. Logging.</h2>When you run into problems during your GTK programming, it might comehandy to see the strings which were received by the GTK-server. Also,you might want to check the responses of the GTK-server to your input.Luckily the GTK-server is able to produce a logfile.To enable the logging facility of theGTK-server, just start as follows:<br><br><small><span style="font-family: courier new,courier,monospace;">gtk-server-stdin -log=log.txt</span></small><br><br>or<br><small><br><span style="font-family: courier new,courier,monospace;">gtk-server -tcp=localhost:50000 -log=mylog.txt &amp;</span></small><br><br>or<br><small><br><span style="font-family: courier new,courier,monospace;">gtk-server-fifo=mypipe -log=program.log &amp;</span></small><br><br>The last argument 'log' will put the GTK-server to logging mode. The loggingwill be redirected to the specified file.<br><br>Well, that is it. You are a GTK guru now. Go guify your scripts!<br><br><hr style="width: 100%; height: 2px;"><br>(c) December 2003 - October 2004, Peter van Eerten&nbsp; -&nbsp;http://www.gtk-server.org/<br><br>2nd revision july 2006 - PvE.<br>3rd revision december 2008 - PvE.<br><br><hr style="width: 100%; height: 2px;"></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -