📄 gtk-server_tutorial.html
字号:
------------------<br><br>And the AWK script will 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;"></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<br>print "gtk_window_set_title " WINDOW " \"This is a title\"" |& GTK;GTK |& getline</small> <br></span> <br>------------------<br><br>Pretty easy, isn't it?<br><br><br><hr style="width: 100%; height: 2px;"><a name="chapter4"></a><h2>Chapter 4. GTK programming - containers.</h2>According to the original GTK principles, we must define a container topack our widgets. It is possible to define the window as a container,but personally, I prefer to use tables. If a table is defined, it iseasy to define your widgets using relational coordinates. To create atable on the window, the function "gtk_table_new" must be used. Thisfunction returns a widget and uses 3 arguments (we will use no callbacksignals for the table). The first 2 arguments determine the amount ofrows and columns in the table. The third argument is a booleanargument.We can workaround this by using the same trick as we did for thetypedefinitions: using '0' for 'false' and '1' for 'true'.<br><br>Also, the table must be nailed to the window. This is done with"gtk_container_add". This function uses 2 arguments: the widget(Window)on which the other widget (Table) will be nailed. Our configfile nowwill look like:<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><span style="font-family: courier new,courier,monospace;">FUNCTION_NAME = gtk_table_new, NONE, WIDGET, 3, LONG, LONG, LONG<br>FUNCTION_NAME = gtk_container_add, NONE, NONE, 2, WIDGET, WIDGET<br></span></small> <span style="font-family: courier new,courier,monospace;"><small> #</small><br></span><br>------------------<br><br>This is 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<br>print "gtk_window_set_title " WINDOW " \"This is a title\"" |& GTK;GTK |& getline<br>print "gtk_table_new 30 30 1" |& GTK; GTK |& getline TABLE<br>print "gtk_container_add " WINDOW " " TABLE |& GTK; GTK |&getline</small> <br></span> <br><br><hr style="width: 100%; height: 2px;"><a name="chapter5"></a><h2>Chapter 5: GTK programming - labels.</h2>Now, the message "Hello world" must appear in our window. We have todefine a label. There is a very convenient GTK function for this:"gtk_label_new", with only 1 argument containing the text to bedisplayed.<br><br>But also, this widget has to be 'packed' onto the table. We will usethe "gtk_table_attach_defaults" function. The first argument determinesthe table we want to use, the second the widget to pack, then the leftxand right x coordinate (related to the table) are mentioned, andfinallythe upper and lower y coordinate are mentioned.<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><span style="font-family: courier new,courier,monospace;">FUNCTION_NAME = gtk_table_new, NONE, WIDGET, 3, LONG, LONG, LONG<br>FUNCTION_NAME = gtk_container_add, NONE, NONE, 2, WIDGET, WIDGET<br>FUNCTION_NAME = gtk_label_new, NONE, WIDGET, 1, STRING<br>FUNCTION_NAME = gtk_table_attach_defaults, NONE, NONE, 6, WIDGET,WIDGET, LONG, LONG, LONG, LONG<br></span></small> <span style="font-family: courier new,courier,monospace;"><small>#</small><br></span><br>------------------<br><br>And 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<br>print "gtk_window_set_title " WINDOW " \"This is a title\"" |& GTK;GTK |& getline<br>print "gtk_table_new 30 30 1" |& GTK; GTK |& getline TABLE<br>print "gtk_container_add " WINDOW " " TABLE |& GTK; GTK |&getline<br>print "gtk_label_new \"Hello world\"" |& GTK; GTK |& getline LABEL<br>print "gtk_table_attach_defaults " TABLE " " LABEL " 1 29 3 7"|& GTK; GTK |& getline</small> <br></span> <br><br><hr style="width: 100%; height: 2px;"><a name="chapter6"></a><h2>Chapter 6: GTK programming - buttons.</h2>We will also create an "Exit" button. Clicking on this button will exitour "Hello world" application. We must capture the click signal so theAWK script can exit. The GTK function "gtk_button_new_with_label" willcreate a button, and it also will put some text on it. However, it mustbe clear that the 'click' signal must be captured.<br><br>Well, we are ready now with creating widgets. Finally all widgets mustbe shown to the world. This is achieved by the GTK function"gtk_widget_show". This function takes only 1 argument, namely, thewidget to be shown.<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><span style="font-family: courier new,courier,monospace;">FUNCTION_NAME = gtk_table_new, NONE, WIDGET, 3, LONG, LONG, LONG<br>FUNCTION_NAME = gtk_container_add, NONE, NONE, 2, WIDGET, WIDGET<br>FUNCTION_NAME = gtk_label_new, NONE, WIDGET, 1, STRING<br>FUNCTION_NAME = gtk_table_attach_defaults, NONE, NONE, 6, WIDGET,WIDGET, LONG, LONG, LONG, LONG<br>FUNCTION_NAME = gtk_button_new_with_label, clicked, WIDGET, 1, STRING<br>FUNCTION_NAME = gtk_widget_show, NONE, NONE, 1, WIDGET<br></span><span style="font-family: courier new,courier,monospace;"></span></small><br>------------------<br><br>So the button will return an identifier, and it has 1 argumentcontaining the text to be printed on the button. The callback signal isdefined as "clicked". In the GTK documentation all signals which can beemitted bya button are described. The term 'clicked' is a real GTK term for the'click'-signal. Let's put the button on the window and make it visible.Please note that also the TABLE container must be shown!<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"<br><br>print "gtk_init NULL NULL" |& GTK; GTK |& getline<br>print "gtk_window_new 0" |& GTK; GTK |& getline WINDOW<br>print "gtk_window_set_title " WINDOW " \"This is a title\"" |& GTK;GTK |& getline<br>print "gtk_table_new 30 30 1" |& GTK; GTK |& getline TABLE<br>print "gtk_container_add " WINDOW " " TABLE |& GTK; GTK |&getline<br>print "gtk_label_new \"Hello world\"" |& GTK; GTK |& getline LABEL<br>print "gtk_table_attach_defaults " TABLE " " LABEL " 1 29 3 7"|& GTK; GTK |& getline<br></span><span style="font-family: courier new,courier,monospace;"> print"gtk_button_new_with_label Exit" |& GTK; GTK |& getline BUTTON<br>print "gtk_table_attach_defaults " TABLE " " BUTTON " 20 28 2327" |& GTK; GTK |& getline<br></span><span style="font-family: courier new,courier,monospace;">print"gtk_widget_show " LABEL |& GTK; GTK |& getline<br></span><span style="font-family: courier new,courier,monospace;">print"gtk_widget_show " BUTTON |& GTK; GTK |& getline<br></span><span style="font-family: courier new,courier,monospace;">print"gtk_widget_show " TABLE |& GTK; GTK |& getline</span><br></small> <span style="font-family: courier new,courier,monospace;"><small>print"gtk_widget_show " WINDOW |& GTK; GTK |& getline</small><br></span><br><br><hr style="width: 100%; height: 2px;"><a name="chapter7"></a><h2>Chapter 7. GTK programming - the mainloop.</h2>We enter the final stage: defining the mainloop. With GTK you have thepossibility to run through all GTK events once. This is performed bythe GTK function "gtk_main_iteration". In this iteration the GTK librarywill check if any event has occured. In our program we want to check ifthe "click"-signal was emitted by the button. Below the finalconfigfile and AWK program:<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><span style="font-family: courier new,courier,monospace;">FUNCTION_NAME = gtk_table_new, NONE, WIDGET, 3, LONG, LONG, LONG<br>FUNCTION_NAME = gtk_container_add, NONE, NONE, 2, WIDGET, WIDGET<br>FUNCTION_NAME = gtk_label_new, NONE, WIDGET, 1, STRING<br>FUNCTION_NAME = gtk_table_attach_defaults, NONE, NONE, 6, WIDGET,WIDGET, LONG, LONG, LONG, LONG<br>FUNCTION_NAME = gtk_button_new_with_label, clicked, WIDGET, 1, STRING<br>FUNCTION_NAME = gtk_widget_show, NONE, NONE, 1, WIDGET<br>FUNCTION_NAME = gtk_main_iteration, NONE, WIDGET, 0<br></span></small> <span style="font-family: courier new,courier,monospace;"><small></small></span><br>------------------<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;">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -