📄 gtk-server_tutorial.html
字号:
<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<br><br>EVENT = 0<br><br>do {<br><br> print "gtk_main_iteration" |& GTK; GTK |&getline<br> print "gtk_server_callback 0" |& GTK;GTK |& getline EVENT<br><br>} while (EVENT != BUTTON)<br><br>close(GTK)<br>fflush("")<br>}</small> <br></span><br>------------------<br><br>Let's take a closer look at the mainloop. We have defined the GTKfunction "gtk_main_iteration" in the configfile. It will run once,triggered by any GTK event, and update all GTK widgets accordingly. Butthe function "gtk_server_callback" cannot be found in the configfile!Why not? Because this is an internal function of the GTK-server. Itretrieved the last occured signal from the GTK library. If a signal wasemitted, the GTK-server returns the widget ID. Else a '0' is returned. In theAWK script above the return value is captured in a variable, which is checkedin the 'while' of the mainloop.<br><br>If the mainloop closes, the 2-way pipe to the GTK-server will be closedas well. Finally, all AWK buffers are flushed.<br><br><br><hr style="width: 100%; height: 2px;"><a name="chapter8"></a><h2>Chapter 8. GTK programming - closing the main window.</h2>You may find that it is not possible to close your window with theregular closing facility's of your windowmanager. In Windows you cannotuse the cross at the right top of the window. However, this isperfectlyok since the 'delete' signal emitted by the main window is not capturedin the AWK script!<br><br>In order to close the window in a regular way (without the EXITbutton), we must define a callback signal for our main window. The nameof the signal when we try to close the window, is called"delete-event".Our configfile must be changed 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><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, delete-event, 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, CLICK, WIDGET, 1, STRING<br>FUNCTION_NAME = gtk_widget_show, NONE, NONE, 1, WIDGET<br>FUNCTION_NAME = gtk_main_iteration, NONE, WIDGET, 0<br></span><span style="font-family: courier new,courier,monospace;"></span></small><small><span style="font-family: courier new,courier,monospace;"></span></small><small><span style="font-family: courier new,courier,monospace;"></span><span style="font-family: courier new,courier,monospace;"></span></small><br>------------------<br><br>As you can see, the GTK function "gtk_window_new" is redefined with thecallback signal "delete-event". Please makesure you do not use capitals for the GTK signal!<br><br>If we run our AWK script now, the main window will listen to the"delete-event" signal. However, we must change our mainloop also, toreally exit the application. Below a suggestion on how to do 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;">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><span style="font-family: courier new,courier,monospace;">print"gtk_widget_show " WINDOW |& GTK; GTK |& getline<br><br>EVENT = 0<br><br></span></small><small><span style="font-family: courier new,courier,monospace;"> do {<br><br> print "gtk_main_iteration" |& GTK; GTK |&getline<br> print "gtk_server_callback 0" |& GTK;GTK |& getline EVENT<br> </span></small><small><span style="font-family: courier new,courier,monospace;"> <br>} while (EVENT != BUTTON && EVENT != WINDOW)<br><br>close(GTK)<br>fflush("")<br>}<br></span></small> <br><br><hr style="width: 100%; height: 2px;"><a name="chapter9"></a><h2>Chapter 9. Using colors.</h2>It is also possible to use colors in your application. This might seemstrange since the coloring of widgets is a sake of GDK instead of GTK.However, with the GTK-server you can use GTK commands for coloringwidgets.<br><br>In our example, let's try to color the button. First we have togive this widget a particular name. Let's name it "exitbutton". Afterthe name has been set, the script has to read an external file whichcontains the color definitions.<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_widget_set_name " BUTTON " exitbutton" |& GTK; GTK|& getline<br>print "gtk_table_attach_defaults " TABLE " " BUTTON " 20 28 2327" |& GTK; GTK |& getline<br>print "gtk_rc_parse gtkrc" |& 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><span style="font-family: courier new,courier,monospace;">print"gtk_widget_show " WINDOW |& GTK; GTK |& getline<br><br>EVENT = 0<br></span></small><small><span style="font-family: courier new,courier,monospace;"> do {<br><br> print "gtk_main_iteration" |& GTK; GTK |&getline<br> print "gtk_server_callback 0" |& GTK;GTK |& getline EVENT</span></small><br><small><span style="font-family: courier new,courier,monospace;"> <br>} while (EVENT != BUTTON && EVENT != WINDOW)<br><br>close(GTK)<br>fflush("")<br>}</span></small><br><br>------------------<br><br>As you can see the commands <small><span style="font-family: courier new,courier,monospace;">gtk_widget_set_name</span></small>and <small><span style="font-family: courier new,courier,monospace;">gtk_rc_parse</span></small>are used. The first command sets the name, the second reads thecolordefinitions. Of course these commands should be available in theconfigfile as well:<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, delete-event, 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, CLICK, WIDGET, 1, STRING<br></span></small><span style="font-family: courier new,courier,monospace;"><small><span style="font-family: courier new,courier,monospace;"></span></small></span><small><span style="font-family: courier new,courier,monospace;">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;"><span style="font-family: courier new,courier,monospace;"><small><span style="font-family: courier new,courier,monospace;">FUNCTION_NAME =gtk_widget_set_name, NONE, NONE, 2, WIDGET, STRING<br>FUNCTION_NAME = gtk_rc_parse, NONE, NONE, 1, STRING<br></span></small></span></span><small><span style="font-family: courier new,courier,monospace;"></span></small><small><span style="font-family: courier new,courier,monospace;"></span><span style="font-family: courier new,courier,monospace;"></span></small><br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -