1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
|
<!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 gint
save_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 void
session_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)
{
gint x, y, w, h;
if ( gnome_parse_geometry( geometry,
&x, &y, &w, &h ) )
{
if (x != -1)
{
gtk_widget_set_uposition(app, x, y);
}
if (w != -1)
{
gtk_window_set_default_size(GTK_WINDOW(app), w, h);
}
}
else
{
g_error(_("Could not parse geometry string `%s'"), geometry);
}
}
if (greet != NULL)
{
GtkWidget* dialog;
gchar* greetings = g_strdup(_("Special Greetings to:\n"));
GSList* tmp = greet;
while (tmp != NULL)
{
gchar* old = greetings;
greetings = g_strconcat(old,
(gchar*) tmp->data,
"\n",
NULL);
g_free(old);
tmp = g_slist_next(tmp);
}
dialog = gnome_ok_dialog(greetings);
g_free(greetings);
gnome_dialog_set_parent(GNOME_DIALOG(dialog), GTK_WINDOW(app));
}
app_list = g_slist_prepend(app_list, app);
return app;
}
void
hello_app_close(GtkWidget* app)
{
g_return_if_fail(GNOME_IS_APP(app));
app_list = g_slist_remove(app_list, app);
gtk_widget_destroy(app);
if (app_list == NULL)
{
/* No windows remaining */
gtk_main_quit();
}
}
static gint
delete_event_cb(GtkWidget* window, GdkEventAny* e, gpointer data)
{
hello_app_close(window);
/* Prevent the window's destruction, since we destroyed it
* ourselves with hello_app_close()
*/
return TRUE;
}
static void
button_click_cb(GtkWidget* w, gpointer data)
{
GtkWidget* label;
gchar* text;
gchar* tmp;
label = GTK_WIDGET(data);
gtk_label_get(GTK_LABEL(label), &text);
tmp = g_strdup(text);
g_strreverse(tmp);
gtk_label_set_text(GTK_LABEL(label), tmp);
g_free(tmp);
}
</pre>
</td>
</tr>
</table>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z873"><tt class="FILENAME">menus.h</tt></a>
</h2>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
#ifndef GNOMEHELLO_MENUS_H
#define GNOMEHELLO_MENUS_H
#include <gnome.h>
void hello_install_menus_and_toolbar(GtkWidget* app);
#endif
</pre>
</td>
</tr>
</table>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z874"><tt class="FILENAME">menus.c</tt></a>
</h2>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
#include <config.h>
#include "menus.h"
#include "app.h"
static void nothing_cb(GtkWidget* widget, gpointer data);
static void new_app_cb(GtkWidget* widget, gpointer data);
static void close_cb (GtkWidget* widget, gpointer data);
static void exit_cb (GtkWidget* widget, gpointer data);
static void about_cb (GtkWidget* widget, gpointer data);
static GnomeUIInfo file_menu [] = {
GNOMEUIINFO_MENU_NEW_ITEM(N_("_New Hello"),
N_("Create a new hello"),
new_app_cb, NULL),
GNOMEUIINFO_MENU_OPEN_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_SAVE_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_SAVE_AS_ITEM(nothing_cb, NULL),
GNOMEUIINFO_SEPARATOR,
GNOMEUIINFO_MENU_CLOSE_ITEM(close_cb, NULL),
GNOMEUIINFO_MENU_EXIT_ITEM(exit_cb, NULL),
GNOMEUIINFO_END
};
static GnomeUIInfo edit_menu [] = {
GNOMEUIINFO_MENU_CUT_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_COPY_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_PASTE_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_SELECT_ALL_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_CLEAR_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_UNDO_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_REDO_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_FIND_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_FIND_AGAIN_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_REPLACE_ITEM(nothing_cb, NULL),
GNOMEUIINFO_MENU_PROPERTIES_ITEM(nothing_cb, NULL),
GNOMEUIINFO_END
};
static GnomeUIInfo help_menu [] = {
GNOMEUIINFO_HELP ("gnome-hello"),
GNOMEUIINFO_MENU_ABOUT_ITEM(about_cb, NULL),
GNOMEUIINFO_END
};
static GnomeUIInfo menu [] = {
GNOMEUIINFO_MENU_FILE_TREE(file_menu),
GNOMEUIINFO_MENU_EDIT_TREE(edit_menu),
GNOMEUIINFO_MENU_HELP_TREE(help_menu),
GNOMEUIINFO_END
};
static GnomeUIInfo toolbar [] = {
GNOMEUIINFO_ITEM_STOCK (N_("New"), N_("Create a new hello"), nothing_cb, GNOME_STOCK_PIXMAP_NEW),
GNOMEUIINFO_SEPARATOR,
GNOMEUIINFO_ITEM_STOCK (N_("Prev"), N_("Previous hello"), nothing_cb, GNOME_STOCK_PIXMAP_BACK),
GNOMEUIINFO_ITEM_STOCK (N_("Next"), N_("Next hello"), nothing_cb, GNOME_STOCK_PIXMAP_FORWARD),
GNOMEUIINFO_END
};
void
hello_install_menus_and_toolbar(GtkWidget* app)
{
gnome_app_create_toolbar_with_data(GNOME_APP(app), toolbar, app);
gnome_app_create_menus_with_data(GNOME_APP(app), menu, app);
gnome_app_install_menu_hints(GNOME_APP(app), menu);
}
static void
nothing_cb(GtkWidget* widget, gpointer data)
{
GtkWidget* dialog;
GtkWidget* app;
app = (GtkWidget*) data;
dialog = gnome_ok_dialog_parented(
_("This does nothing; it is only a demonstration."),
GTK_WINDOW(app));
}
static void
new_app_cb(GtkWidget* widget, gpointer data)
{
GtkWidget* app;
app = hello_app_new(_("Hello, World!"), NULL, NULL);
gtk_widget_show_all(app);
}
static void
close_cb(GtkWidget* widget, gpointer data)
{
GtkWidget* app;
app = (GtkWidget*) data;
hello_app_close(app);
}
static void
exit_cb(GtkWidget* widget, gpointer data)
{
gtk_main_quit();
}
static void
about_cb(GtkWidget* widget, gpointer data)
{
static GtkWidget* dialog = NULL;
GtkWidget* app;
app = (GtkWidget*) data;
if (dialog != NULL)
{
g_assert(GTK_WIDGET_REALIZED(dialog));
gdk_window_show(dialog->window);
gdk_window_raise(dialog->window);
}
else
{
const gchar *authors[] = {
"Havoc Pennington <hp@pobox.com>",
NULL
};
gchar* logo = gnome_pixmap_file("gnome-hello-logo.png");
dialog = gnome_about_new (_("GnomeHello"), VERSION,
"(C) 1999 Havoc Pennington",
authors,
_("A sample GNOME application."),
logo);
g_free(logo);
gtk_signal_connect(GTK_OBJECT(dialog),
"destroy",
GTK_SIGNAL_FUNC(gtk_widget_destroyed),
&dialog);
gnome_dialog_set_parent(GNOME_DIALOG(dialog), GTK_WINDOW(app));
gtk_widget_show(dialog);
}
}
</pre>
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="NAVFOOTER">
<br>
<br>
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<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>
<tr>
<td colspan="2" align="left">
<font color="#000000" size="2"><b>This Book</b></font>
</td>
<td colspan="2" align="right">
<font color="#000000" size="2"><b>The <tt class=
"CLASSNAME">GtkEv</tt> Widget</b></font>
</td>
</tr>
</table>
</div>
</body>
</html>
|