📄 listing.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <title> Code Listings </title> <meta name="GENERATOR" content= "Modular DocBook HTML Stylesheet Version 1.45"> <link rel="HOME" title="GTK+ / Gnome Application Development" href="ggad.html"> <link rel="UP" title="Appendices" href="appendices.html"> <link rel="PREVIOUS" title="This Book" href="z866.html"> <link rel="NEXT" title="The GtkEv Widget" href="z875.html"> </head> <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink= "#840084" alink="#0000FF"> <div class="NAVHEADER"> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <th colspan="4" align="center"> <font color="#000000" size="2">GTK+ / Gnome Application Development</font> </th> </tr> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="z866.html"><font color="#0000ff" size="2"><b> <<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="z875.html"><font color="#0000ff" size="2"><b> Next >>></b></font></a> </td> </tr> </table> </div> <div class="CHAPTER"> <h1> <a name="LISTING">Code Listings</a> </h1> <div class="TOC"> <dl> <dt> <b>Table of Contents</b> </dt> <dt> <a href="listing.html#Z869">The GnomeHello Application</a> </dt> <dt> <a href="z875.html">The <tt class="CLASSNAME"> GtkEv</tt> Widget</a> </dt> </dl> </div> <p> This appendix contains complete sample code listings. </p> <div class="SECT1"> <h1 class="SECT1"> <a name="Z869">The GnomeHello Application</a> </h1> <p> GnomeHello is used as an example in Part 2. </p> <div class="SECT2"> <h2 class="SECT2"> <a name="Z870"><tt class="FILENAME">hello.c</tt></a> </h2> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> #include <config.h>#include <gnome.h>#include "app.h"static void session_die(GnomeClient* client, gpointer client_data);static gint save_session(GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint is_shutdown, GnomeInteractStyle interact_style, gint is_fast, gpointer client_data);static int greet_mode = FALSE;static char* message = NULL;static char* geometry = NULL;struct poptOption options[] = { { "greet", 'g', POPT_ARG_NONE, &greet_mode, 0, N_("Say hello to specific people listed on the command line"), NULL }, { "message", 'm', POPT_ARG_STRING, &message, 0, N_("Specify a message other than \"Hello, World!\""), N_("MESSAGE") }, { "geometry", '\0', POPT_ARG_STRING, &geometry, 0, N_("Specify the geometry of the main window"), N_("GEOMETRY") }, { NULL, '\0', 0, NULL, 0, NULL, NULL }};int main(int argc, char* argv[]){ GtkWidget* app; poptContext pctx; char** args; int i; GSList* greet = NULL; GnomeClient* client; bindtextdomain(PACKAGE, GNOMELOCALEDIR); textdomain(PACKAGE); gnome_init_with_popt_table(PACKAGE, VERSION, argc, argv, options, 0, &pctx); /* Argument parsing */ args = poptGetArgs(pctx); if (greet_mode && args) { i = 0; while (args[i] != NULL) { greet = g_slist_prepend(greet, args[i]); ++i; } /* Put them in order */ greet = g_slist_reverse(greet); } else if (greet_mode && args == NULL) { g_error(_("You must specify someone to greet.")); } else if (args != NULL) { g_error(_("Command line arguments are only allowed with --greet.")); } else { g_assert(!greet_mode && args == NULL); } poptFreeContext(pctx); /* Session Management */ client = gnome_master_client (); gtk_signal_connect (GTK_OBJECT (client), "save_yourself", GTK_SIGNAL_FUNC (save_session), argv[0]); gtk_signal_connect (GTK_OBJECT (client), "die", GTK_SIGNAL_FUNC (session_die), NULL); /* Main app */ app = hello_app_new(message, geometry, greet); g_slist_free(greet); gtk_widget_show_all(app); gtk_main(); return 0;}static gintsave_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint is_shutdown, GnomeInteractStyle interact_style, gint is_fast, gpointer client_data){ gchar** argv; guint argc; /* allocate 0-filled, so it will be NULL-terminated */ argv = g_malloc0(sizeof(gchar*)*4); argc = 1; argv[0] = client_data; if (message) { argv[1] = "--message"; argv[2] = message; argc = 3; } gnome_client_set_clone_command (client, argc, argv); gnome_client_set_restart_command (client, argc, argv); return TRUE;}static voidsession_die(GnomeClient* client, gpointer client_data){ gtk_main_quit ();} </pre> </td> </tr> </table> </div> <div class="SECT2"> <h2 class="SECT2"> <a name="Z871"><tt class="FILENAME">app.h</tt></a> </h2> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> #ifndef GNOMEHELLO_APP_H#define GNOMEHELLO_APP_H#include <gnome.h>GtkWidget* hello_app_new(const gchar* message, const gchar* geometry, GSList* greet);void hello_app_close(GtkWidget* app); #endif </pre> </td> </tr> </table> </div> <div class="SECT2"> <h2 class="SECT2"> <a name="Z872"><tt class="FILENAME">app.c</tt></a> </h2> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> #include <config.h>#include "app.h"#include "menus.h"/* Keep a list of all open application windows */static GSList* app_list = NULL;static gint delete_event_cb(GtkWidget* w, GdkEventAny* e, gpointer data);static void button_click_cb(GtkWidget* w, gpointer data);GtkWidget* hello_app_new(const gchar* message, const gchar* geometry, GSList* greet){ GtkWidget* app; GtkWidget* button; GtkWidget* label; GtkWidget* status; GtkWidget* frame; app = gnome_app_new(PACKAGE, _("Gnome Hello")); frame = gtk_frame_new(NULL); button = gtk_button_new(); label = gtk_label_new(message ? message : _("Hello, World!")); gtk_window_set_policy(GTK_WINDOW(app), FALSE, TRUE, FALSE); gtk_window_set_default_size(GTK_WINDOW(app), 250, 350); gtk_window_set_wmclass(GTK_WINDOW(app), "hello", "GnomeHello"); gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN); gtk_container_set_border_width(GTK_CONTAINER(button), 10); gtk_container_add(GTK_CONTAINER(button), label); gtk_container_add(GTK_CONTAINER(frame), button); gnome_app_set_contents(GNOME_APP(app), frame); status = gnome_appbar_new(FALSE, TRUE, GNOME_PREFERENCES_NEVER); gnome_app_set_statusbar(GNOME_APP(app), status); hello_install_menus_and_toolbar(app); gtk_signal_connect(GTK_OBJECT(app), "delete_event", GTK_SIGNAL_FUNC(delete_event_cb), NULL); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(button_click_cb), label); if (geometry != NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -