📄 gtk-server_tutorial.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <title>The GTK-server Tutorial</title> <meta name="author" content="Peter van Eerten"></head><body><h1>The GTK-server Tutorial: a "Hello world" application.</h1><h2>Contents:</h2><a href="#introduction">Introduction.</a><br><a href="#chapter1">Chapter 1. Setting up your environment.</a><br><a href="#chapter2">Chapter 2. First steps.</a><br><a href="#chapter3">Chapter 3. GTK programming - the main window.</a><br><a href="#chapter4">Chapter 4. GTK programming - containers.</a><br><a href="#chapter5">Chapter 5: GTK programming - labels.</a><br><a href="#chapter6">Chapter 6: GTK programming - buttons.</a><br><a href="#chapter7">Chapter 7. GTK programming - the mainloop.</a><br><a href="#chapter8">Chapter 8. GTK programming - closing the main window.</a><br><a href="#chapter9">Chapter 9. Using colors.</a><br><a href="#chapter10">Chapter 10. Connecting to the GTK-server with TCP or UDP.</a><br><a href="#chapter11">Chapter 11. Connecting to the GTK-server with FIFO.</a><br><a href="#chapter12">Chapter 12. Logging.</a><br><br><hr style="width: 100%; height: 2px;"><a name="introduction"></a><h2>Introduction.</h2>In this tutorial we will create a simple window containing the message"Hello world!". We will mainly be using GNU AWK to do that. Basicknowledge of the AWK script language is recommended. If you are unknownto AWK, please consult the excellent GNU AWK manual at <a href="http://www.gnu.org/software/gawk/manual/gawk.html">http://ww.gnu.org/software/gawk/manual/gawk.html</a>.Also, this tutorial will focus on the Linux operating system.<br><br><br><hr style="width: 100%; height: 2px;"><a name="chapter1"></a><h2>Chapter 1. Setting up your environment.<br></h2>Before starting to use the GTK-server, you have to check yourenvironment variables. AWK will be the main script language in thistutorial, and the AWK script will invoke the GTK-server with theargument 'stdin'. Now, in order for the GTK widgets to be displayedcorrectly, it is necessary to set your language environment, since theGTK library will use this setting to display it's widgets. You can usethe variable LC_ALL to accomplish this. To find out what the currentsettings are of your environment variables, you can use the command'set' (BASH, or 'setenv' for CSHELL). This command will list all yourvariables. On my system, LC_ALL is put to Dutch. In the Linux BASHshell, perform:<br><br style="font-family: courier new,courier,monospace;"><small><span style="font-family: courier new,courier,monospace;">exportLC_ALL=nl_NL</span></small><br><br>to set the variable. But this is not all. Also the library path shouldbe set. It must point to the X11R6 library's. This is needed to displaytext entry widgets for example. In the BASH shell, make sure that theLD_LIBRARY_PATH is set:<br><br><small><span style="font-family: courier new,courier,monospace;">exportLD_LIBRARY_PATH=/usr/X11R6/lib</span></small><br><br>Or if this environment variable already has some other value:<br><br><small><span style="font-family: courier new,courier,monospace;">exportLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/X11R6/lib</span></small><br><br>Now your shell environment is setup properly for scripts using theGTK-server with 'stdin'.<br><br><br><hr style="width: 100%; height: 2px;"><a name="chapter2"></a><h2>Chapter 2. First steps.<br></h2>During the process of creating the AWK script, also the GTK-serverconfigfile will be created. Every time a GTK function is used, thisfunction must be specified in the configfile as well. But first, let'sprogram a few default AWK lines to get started:<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;">GTK ="gtk-server -stdin"</span></small> <br><br>------------------<br><br>Now, from this piece of code we can see that the interpreter is calledin the first line (gawk). Then there are some commentlines startingwitha "#", after that the BEGIN rule is defined, and finally the variableGTK is declared as the GTK-server with the stdin argument. It is NOTnecessary to have the GTK-server binary in the same directory as yourscript. The GTK-server willsearch for the configfile in the "/usr/local/etc" and "/etc" directory. If the configfileisn't available there, the server will exit.<br><br>Now, before starting to use the GTK-server, the configfile must bedefined. The name of this file MUST be "gtk-server.cfg". There are 3setting types which must be defined. The first one is "LIB_NAME".You need to specify the actual Shared Object name of your GTK library.Most likely this will be something like "libgtk-1.2.so" or"libgtk-x11-2.0.so". So the first entry in our configfile will looksimilar to this:<br><br>------------------<br><br><small><span style="font-family: courier new,courier,monospace;">#<br>LIB_NAME = libgtk-x11-2.0.so<br style="font-family: courier new,courier,monospace;"></span></small> <span style="font-family: courier new,courier,monospace;"><small>#</small><br></span><br>------------------<br><br>It is allowed to use "#" in the configfile as well, so you can put yourcomments there. The next setting we are going to use, isFUNCTION_NAME. In order to use the GTK library, we must initializefirst. This is done with the GTK function "gtk_init". Now, we will putthis function into our configfile as follows:<br><br>------------------<br><br><small><span style="font-family: courier new,courier,monospace;">#<br>LIB_NAME = libgtk-x11-2.0.so<br style="font-family: courier new,courier,monospace;"></span></small> <span style="font-family: courier new,courier,monospace;"><small>#<br>FUNCTION_NAME = gtk_init, NONE, NONE, 2, NULL, NULL<br>#</small><br></span><br>------------------<br><br>Next to the name of the GTK function, more definitions are encountered.First a 'NONE' is defined. This first NONE defines the callback signal.Callback signals occur when you click with your mouse on a button, forexample. The initialization of the GTK library however has no callbacksignal of itself. The second NONE defines the returnvalue of the"gtk_init" function; it means that the function does not have a returnvalue. Then the number '2', this number defines the amount of argumentsto the "gtk_init" function. And finally, the last two definitionsspecify the type of the arguments. If 'NULL' is defined, it means thatthe argument has no specific type.<br><br><br><hr style="width: 100%; height: 2px;"><a name="chapter3"></a><h2>Chapter 3. GTK programming - the main window.</h2>Well, so far so good! We have a start with our AWK script, and we havea first setup of the gtk-server.cfg file. Now, let's go on with therealprogramming and put the "gtk_init" into our AWK script:<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;"></small> <span style="font-family: courier new,courier,monospace;"><small>GTK= "gtk-server -stdin"<br><br>print "gtk_init NULL NULL" |& GTK; GTK |& getline</small> <br></span> <br>------------------<br><br>The line starting with 'print' will actually create a 2-way pipe to theGTK-server. This is a genuine AWK technique. The pipe operator "|&"was borrowed from KSH. It sets up a communication channel on whichplaindata can be sent and received. As you can see, the AWK script sends theGTK function name as plain text towards the server, and after that, theserver will send information back.<br><br>This is an important thing to remember: every time information is sentto the GTK-server, the server will send information back! If the GTKfunction has no returnvalue (like "gtk_init"), the server will return aplain "ok" to the script. If the GTK function was not recognized, theserver will return a "-1". Please keep in mind that the GTK-serveralways will return a string.<br><br>Okay, we are ready to create our main window. The GTK function we willuse is called "gtk_window_new". This function however will return anidentifier referring to the created window. If a GTK function returnsanidentifier for a widget, this must be defined as WIDGET in theconfigfile. But also, it has a "GtkWindowType"-argument which is a Ctypedefinition. How to explain this in our configfile?<br><br>The C typedefinitions are a little bit tricky. There are a lot of GTKtypedefinitions, and to define all these in the GTK-server would beextremely redundant. The C programming language however uses aninternalinteger numbering for the typedefinitions. If you look at thetypedefinitions for "GtkWindowType", there are 2 types defined:GTK_WINDOW_TOPLEVEL and GTK_WINDOW_POPUP. Now, the C language willreferto the first type with '0' and to the second type with '1'. These areregular integer numbers. In that case, we can define the argument typefor "gtk_window_new" as LONG. The gtk-server configfile will now looklikethis:<br><br>------------------<br><br><small><span style="font-family: courier new,courier,monospace;">#<br>LIB_NAME = libgtk-x11-2.0.so<br style="font-family: courier new,courier,monospace;"></span><span style="font-family: courier new,courier,monospace;">#<br>FUNCTION_NAME = gtk_init, NONE, NONE, 2, NULL, NULL<br></span><span style="font-family: courier new,courier,monospace;">FUNCTION_NAME = gtk_window_new, NONE, WIDGET, 1, LONG</span><br></small> <span style="font-family: courier new,courier,monospace;"><small>#</small><br></span><br>------------------<br><br>We will not yet define a callback signal for the window. All right,let's go to the AWK script!<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;"></small> <span style="font-family: courier new,courier,monospace;"><small>GTK= "gtk-server -stdin"<br><br>print "gtk_init NULL NULL" |& GTK; GTK |& getline<br>print "gtk_window_new 0" |& GTK; GTK |& getline WINDOW</small><br></span> <br>------------------<br><br>You can see that the returnvalue of the GTK function is captured in thevariable "WINDOW". Later on, we can use the variable to perform actionson our window. Let's do that right now; let's define a name in thetitlebar of our window. The GTK function for this is called"gtk_window_set_title". It has no returnvalue, and it uses 2 arguments:the window of which the title must be set, and the title itself. Thisisour configfile now:<br><br>------------------<br><br><small><span style="font-family: courier new,courier,monospace;">#<br>LIB_NAME = libgtk-x11-2.0.so<br style="font-family: courier new,courier,monospace;"></span><span style="font-family: courier new,courier,monospace;">#<br>FUNCTION_NAME = gtk_init, NONE, NONE, 2, NULL, NULL<br></span><span style="font-family: courier new,courier,monospace;">FUNCTION_NAME = gtk_window_new, NONE, WIDGET, 1, LONG<br>FUNCTION_NAME = gtk_window_set_title, NONE, NONE, 2, WIDGET, STRING<br></span></small> <span style="font-family: courier new,courier,monospace;"><small> #</small><br></span><br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -