File: gnmain.c

package info (click to toggle)
glhack 1.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 24,604 kB
  • ctags: 18,992
  • sloc: ansic: 208,570; cpp: 13,139; yacc: 2,005; makefile: 1,161; lex: 377; sh: 321; awk: 89; sed: 11
file content (839 lines) | stat: -rw-r--r-- 25,996 bytes parent folder | download | duplicates (22)
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*	SCCS Id: @(#)gnmain.c	3.4	2000/07/16	*/
/* Copyright (C) 1998 by Erik Andersen <andersee@debian.org> */
/* NetHack may be freely redistributed.  See license for details. */

#include "gnmain.h"
#include "gnsignal.h"
#include "gnbind.h"
#include "gnopts.h"
#include <gnome.h>
#include <getopt.h>
#include <gdk/gdk.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include "hack.h"
#include "date.h"

static GtkWidget* mainWindow=NULL;
static GtkWidget *about=NULL;
static GtkWidget* hBoxFirstRow;
static GtkWidget* vBoxMain;

int restarted = 0;
int os_x = 0, os_y = 0, os_w = 0, os_h = 0;


static GnomeClient *session_id;

static
void ghack_quit_game(GtkWidget *widget, int button)
{
    gtk_widget_hide(widget);
    if (button == 0) {
	gnome_exit_nhwindows(0);
	gtk_object_unref(GTK_OBJECT(session_id));
    }
}

static
void ghack_quit_game_cb(GtkWidget *widget, gpointer data)
{
    GtkWidget *box;
    box = gnome_message_box_new(_("Do you really want to quit?"), 
	    GNOME_MESSAGE_BOX_QUESTION, GNOME_STOCK_BUTTON_YES, 
	    GNOME_STOCK_BUTTON_NO, NULL);
    gnome_dialog_set_default( GNOME_DIALOG(box), 1);
    gnome_dialog_set_parent (GNOME_DIALOG (box), 
	    GTK_WINDOW (ghack_get_main_window ()) );
    gnome_dialog_set_accelerator (GNOME_DIALOG(box), 1, 'n', 0);
    gnome_dialog_set_accelerator (GNOME_DIALOG(box), 0, 'y', 0);

    gtk_window_set_modal( GTK_WINDOW(box), TRUE);
    gtk_signal_connect( GTK_OBJECT(box), "clicked", 
	    (GtkSignalFunc)ghack_quit_game, NULL);
    gtk_widget_show(box);
}

static
void ghack_save_game(GtkWidget *widget, int button)
{
    gtk_widget_hide(widget);
    if (button == 0) {
	if(dosave0()) {
	    /* make sure they see the Saving message */
	    display_nhwindow(WIN_MESSAGE, TRUE);
	    gnome_exit_nhwindows("Be seeing you...");
	} else (void)doredraw();
    }
}

void ghack_save_game_cb(GtkWidget *widget, gpointer data)
{
    GtkWidget *box;
    box = gnome_message_box_new(_("Quit and save the current game?"), 
	    GNOME_MESSAGE_BOX_QUESTION, GNOME_STOCK_BUTTON_YES, 
	    GNOME_STOCK_BUTTON_NO, NULL);
    gnome_dialog_set_default( GNOME_DIALOG(box), 1);
    gnome_dialog_set_parent (GNOME_DIALOG (box), 
	    GTK_WINDOW (ghack_get_main_window ()) );
    gnome_dialog_set_accelerator (GNOME_DIALOG(box), 1, 'n', 0);
    gnome_dialog_set_accelerator (GNOME_DIALOG(box), 0, 'y', 0);

    gtk_window_set_modal( GTK_WINDOW(box), TRUE);
    gtk_signal_connect( GTK_OBJECT(box), "clicked", 
	    (GtkSignalFunc)ghack_save_game, NULL);
    gtk_widget_show(box);
}

static
void ghack_new_game(GtkWidget *widget, int button)
{
    if (button == 0) {
	g_message("This feature is not yet implemented.  Sorry.");
    }
}

static
void ghack_new_game_cb(GtkWidget *widget, gpointer data)
{
    GtkWidget *box;
    box = gnome_message_box_new(_("Start a new game?"), 
	    GNOME_MESSAGE_BOX_QUESTION, GNOME_STOCK_BUTTON_YES, 
	    GNOME_STOCK_BUTTON_NO, NULL);
    gnome_dialog_set_default( GNOME_DIALOG(box), 1);
    gnome_dialog_set_parent (GNOME_DIALOG (box), 
	    GTK_WINDOW (ghack_get_main_window ()) );
    gnome_dialog_set_accelerator (GNOME_DIALOG(box), 1, 'n', 0);
    gnome_dialog_set_accelerator (GNOME_DIALOG(box), 0, 'y', 0);

    gtk_window_set_modal( GTK_WINDOW(box), TRUE);
    gtk_signal_connect( GTK_OBJECT(box), "clicked", 
	    (GtkSignalFunc)ghack_new_game, NULL);
    gtk_widget_show(box);
}

static void
about_destroy_callback (void)
{
    about = NULL;
}

static void 
ghack_about_cb(GtkWidget *widget, gpointer data)
{
    char buf[BUFSZ]="\0";
    char buf1[BUFSZ]="\0";
    const gchar *authors[] = {"Erik Andersen", "Anthony Taylor", 
	"Jeff Garzik", "The Nethack Dev Team", NULL};

    if (about) {
	gdk_window_raise (about->window);
	return;
    }

    getversionstring(buf);
    strcat( buf1, VERSION_STRING);
    strcat( buf, 
      _("\nSend comments and bug reports to: nethack-bugs@nethack.org\n"
      "This game is free software. See License for details."));
    about = gnome_about_new(_("Nethack"), 
	    buf1, "Copyright (C) 1985-2002 Mike Stephenson",
	    (const char **)authors, buf,
	    NULL);

    gtk_signal_connect (GTK_OBJECT (about), "destroy",
	(GtkSignalFunc) about_destroy_callback, NULL);
    
    gtk_widget_show(about);
}

static void 
ghack_settings_cb(GtkWidget *widget, gpointer data)
{
    ghack_settings_dialog();
}

static void 
ghack_accelerator_selected (GtkWidget *widget, gpointer data)
{
    GdkEventKey event;
    int key = GPOINTER_TO_INT( data); 
    /* g_message("An accelerator for \"%c\" was selected", key); */
    /* stuff a key directly into the keybuffer */
    event.state=0;
    event.keyval=key;
    ghack_handle_key_press(NULL, &event, NULL);
}

#ifndef M
# ifndef NHSTDC
#  define M(c)          (0x80 | (c))
# else
#  define M(c)          ((c) - 128)
# endif /* NHSTDC */
#endif
#ifndef C
#define C(c)            (0x1f & (c))
#endif


GnomeUIInfo game_tree[] = 
{
    {
	GNOME_APP_UI_ITEM, N_ ("_Change Settings..."), 
	N_("Change Game Settings"), ghack_settings_cb, NULL, NULL, 
	GNOME_APP_PIXMAP_NONE, NULL, 0,0, NULL
    },
    GNOMEUIINFO_SEPARATOR,
    { 
	GNOME_APP_UI_ITEM, N_("Version"), NULL, 
	ghack_accelerator_selected, GINT_TO_POINTER('v'), NULL, 
	GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_ABOUT, 'v',0 
    }, 
    { 
	GNOME_APP_UI_ITEM, N_("History..."), NULL, 
	ghack_accelerator_selected, GINT_TO_POINTER('V'), NULL, 
	GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_ABOUT, 'V',GDK_SHIFT_MASK 
    }, 
    { 
	GNOME_APP_UI_ITEM, N_("Compilation..."), NULL, 
	ghack_accelerator_selected, GINT_TO_POINTER(M('v')), NULL, 
	GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_ABOUT,'v',GDK_MOD1_MASK
    }, 
    { 
	GNOME_APP_UI_ITEM, N_("Options..."), NULL, 
	ghack_accelerator_selected, GINT_TO_POINTER('O'), NULL, 
	GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_PREF, 'O', GDK_SHIFT_MASK
    }, 
    { 
	GNOME_APP_UI_ITEM, N_("Explore Mode..."), NULL, 
	ghack_accelerator_selected, GINT_TO_POINTER('X'), NULL, 
	GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_QUIT, 'X', GDK_SHIFT_MASK
    }, 
    GNOMEUIINFO_SEPARATOR,
    GNOMEUIINFO_MENU_NEW_GAME_ITEM(ghack_new_game_cb, NULL),
    GNOMEUIINFO_MENU_SAVE_ITEM(ghack_save_game_cb, NULL),
    { 
	GNOME_APP_UI_ITEM, N_("Exit"), NULL, 
	ghack_quit_game_cb, GINT_TO_POINTER(M('Q')), NULL, 
	GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_ABOUT, 'Q', GDK_MOD1_MASK
    }, 
    GNOMEUIINFO_END
};



GnomeUIInfo edit_menu[] = {
	   { 
	       GNOME_APP_UI_ITEM, N_("Inventory"), 
	       N_("Edit/View your Inventory"), ghack_accelerator_selected, 
	       GINT_TO_POINTER('i'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'i', 0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Discoveries"), 
	       N_("Edit/View your Discoveries"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('\\'), NULL, GNOME_APP_PIXMAP_NONE, NULL, '\\',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("List/reorder your spells"), 
	       N_("List/reorder your spells"), ghack_accelerator_selected, 
	       GINT_TO_POINTER('x'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'x', 0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Adjust letters"), 
	       N_("Adjust letter for items in your Inventory"), ghack_accelerator_selected, 
	        GINT_TO_POINTER(M('a')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'a', GDK_MOD1_MASK
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Name object"), 
	       N_("Assign a name to an object"), ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('n')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'n', GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Name creature"), 
	       N_("Assign a name to a creature"), ghack_accelerator_selected, 
	       GINT_TO_POINTER('C'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'C', GDK_SHIFT_MASK
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Qualifications"), 
	       N_("Edit your Qualifications"), ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('e')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'e',GDK_MOD1_MASK
	   },
           GNOMEUIINFO_END
};


GnomeUIInfo apparel_menu[] = {
	   { 
	       GNOME_APP_UI_ITEM, N_("Wield Weapon"), 
	       N_("Select a weapon to fight with"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('w'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'w',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Remove Apparel..."), 
	       N_("Remove apparel dialog bog"), ghack_accelerator_selected, 
	       GINT_TO_POINTER('A'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'A',GDK_SHIFT_MASK
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Wear Armor"), 
	       N_("Put on armor"), ghack_accelerator_selected, 
	       GINT_TO_POINTER('W'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'W',GDK_SHIFT_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Take off Armor"), 
	       N_("Take off armor you are wearing"), ghack_accelerator_selected, 
	       GINT_TO_POINTER('T'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'T',GDK_SHIFT_MASK
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Put on non-armor"), 
	       N_("Put on non-armor apparel"), ghack_accelerator_selected, 
	       GINT_TO_POINTER('P'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'P',GDK_SHIFT_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Remove non-armor"), 
	       N_("Remove non-armor apparel you are wearing"), ghack_accelerator_selected, 
	       GINT_TO_POINTER('R'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'R',GDK_SHIFT_MASK
	   },
           GNOMEUIINFO_END
};

GnomeUIInfo action_menu[] = {
	   { 
	       GNOME_APP_UI_ITEM, N_("Get"), 
	       N_("Pick up things at the current location"), 
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(','), NULL, GNOME_APP_PIXMAP_NONE, NULL, ',',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Loot"), 
	       N_("loot a box on the floor"), 
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('l')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'l',GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Sit"), 
	       N_("sit down"), 
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('s')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 's',GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Force"), 
	       N_("force a lock"), 
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('f')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'f',GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Kick"), 
	       N_("kick something (usually a door)"), 
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(C('d')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'd',GDK_CONTROL_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Jump"), 
	       N_("jump to another location"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('j')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'j',GDK_MOD1_MASK
	   },
#ifdef STEED
	   { 
	       GNOME_APP_UI_ITEM, N_("Ride"), 
	       N_("Ride (or stop riding) a monster"),
	       doride, 
	       GINT_TO_POINTER(M('r')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'R',GDK_MOD1_MASK
	   },
#endif
	   { 
	       GNOME_APP_UI_ITEM, N_("Wipe face"), 
	       N_("wipe off your face"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('w')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'w',GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Throw/Shoot"), 
	       N_("throw or shoot a weapon"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('t'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 't',0
	   },
	   {
	       GNOME_APP_UI_ITEM, N_("Quiver/Ready"),
	       N_("ready or quiver some ammunition"),
	       ghack_accelerator_selected,
	       GINT_TO_POINTER('Q'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'Q',GDK_SHIFT_MASK,
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Open Door"), 
	       N_("open a door"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('o'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'o',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Close Door"), 
	       N_("open a door"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('c'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'c',0
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Drop"), 
	       N_("drop an object"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('d'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'd',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Drop Many"), 
	       N_("drop selected types of objects"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('D'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'D',GDK_SHIFT_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Eat"), 
	       N_("eat something"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('e'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'e',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Engrave"), 
	       N_("write a message in the dust on the floor"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('E'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'E',GDK_SHIFT_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Apply"), 
	       N_("apply or use a tool (pick-axe, key, camera, etc.)"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('a'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'a',0
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Up"), 
	       N_("go up the stairs"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('<'), NULL, GNOME_APP_PIXMAP_NONE, NULL, '<',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Down"), 
	       N_("go down the stairs"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('>'), NULL, GNOME_APP_PIXMAP_NONE, NULL, '>',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Rest"), 
	       N_("wait for a moment"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('.'), NULL, GNOME_APP_PIXMAP_NONE, NULL, '.',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Search"), 
	       N_("search for secret doors, hidden traps and monsters"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('s'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 's',0
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Chat"), 
	       N_("talk to someone"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('c')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'c',GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Pay"), 
	       N_("pay your bill to the shopkeeper"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('p'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'p',0
	   },
           GNOMEUIINFO_END
};

GnomeUIInfo magic_menu[] = {
	   { 
	       GNOME_APP_UI_ITEM, N_("Quaff potion"), 
	       N_("drink a potion"), 
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('q'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'q',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Read Book/Scroll"), 
	       N_("read a spell book or a scroll"), 
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('r'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'r',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Zap Wand"), 
	       N_("zap a wand"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('z'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'z',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Zap Spell"), 
	       N_("cast a spell"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('Z'), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'Z',GDK_SHIFT_MASK
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Dip"), 
	       N_("dip an object into something"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('d')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'd',GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Rub"), 
	       N_("Rub something (i.e. a lamp)"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('r')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'r',GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Invoke"), 
	       N_("invoke an object's special powers"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('i')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'i',GDK_MOD1_MASK
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Offer"), 
	       N_("offer a sacrifice to the gods"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('o')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'o',GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Pray"), 
	       N_("pray to the gods for help"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('p')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'p',GDK_MOD1_MASK
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("Teleport"), 
	       N_("teleport (if you can)"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(C('t')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 't',GDK_CONTROL_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Monster Action"), 
	       N_("use a monster's special ability"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('m')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 'm',GDK_MOD1_MASK
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Turn Undead"), 
	       N_("turn undead"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(M('t')), NULL, GNOME_APP_PIXMAP_NONE, NULL, 't',GDK_MOD1_MASK
	   },
           GNOMEUIINFO_END
};

GnomeUIInfo help_menu[] = {
            { 
		GNOME_APP_UI_ITEM, N_("About..."), 
		N_("About GnomeHack"), ghack_about_cb, NULL, NULL, 
		GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_ABOUT, 0, 0, NULL 
	    }, 
	   { 
	       GNOME_APP_UI_ITEM, N_("Help"), NULL,
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('?'), NULL, GNOME_APP_PIXMAP_STOCK, 
	       GNOME_STOCK_MENU_ABOUT, '?', 0
	   },
	   GNOMEUIINFO_SEPARATOR,
	   { 
	       GNOME_APP_UI_ITEM, N_("What is here"), 
	       N_("Check what items occupy the current location"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(':'), NULL, GNOME_APP_PIXMAP_STOCK, 
	       GNOME_STOCK_MENU_ABOUT, ':',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("What is that"), 
	       N_("Identify an object"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER(';'), NULL, GNOME_APP_PIXMAP_STOCK, 
	       GNOME_STOCK_MENU_ABOUT, ';',0
	   },
	   { 
	       GNOME_APP_UI_ITEM, N_("Identify a map symbol"), 
	       N_("Identify a map symbol"),
	       ghack_accelerator_selected, 
	       GINT_TO_POINTER('/'), NULL, GNOME_APP_PIXMAP_STOCK, 
	       GNOME_STOCK_MENU_ABOUT, '/',0
	   },
	   GNOMEUIINFO_END 
};
	    
GnomeUIInfo mainmenu[] = {
	    GNOMEUIINFO_MENU_GAME_TREE(game_tree),
	    GNOMEUIINFO_MENU_EDIT_TREE(edit_menu),
	    { GNOME_APP_UI_SUBTREE, N_("Apparel"), NULL, apparel_menu, NULL, 
		NULL, 0, NULL, 0, 0, NULL },
	    { GNOME_APP_UI_SUBTREE, N_("Action"), NULL, action_menu, NULL, 
		NULL, 0, NULL, 0, 0, NULL },
	    { GNOME_APP_UI_SUBTREE, N_("Magic"), NULL, magic_menu, NULL, 
		NULL, 0, NULL, 0, 0, NULL },
	    GNOMEUIINFO_MENU_HELP_TREE(help_menu),
	    GNOMEUIINFO_END
};

static void
ghack_main_window_key_press(GtkWidget *widget, GdkEventKey *event, 
	gpointer data)
{
    /* First, turn off the key press propogation.  We've got the
     * key, but we don't wan't the underlying Gtk widgets to get it,
     * since they do the wrong thing with the arrow keys (shift focus)... */
    gtk_signal_emit_stop_by_name( GTK_OBJECT(mainWindow), "key_press_event");
    
    /* stuff the key event into the keybuffer */
    ghack_handle_key_press(widget, event, data);
}


/* parsing args */
void
parse_args (int argc, char *argv[])
{
  gint ch;

  struct option options[] = {
	/* Default args */
	{ "help",		no_argument,            NULL,   'h'     },
	{ "version",	 	no_argument,            NULL,   'v'     },

	{ NULL, 0, NULL, 0 }
	};

  gchar *id = NULL;

  /* initialize getopt */
  optarg = NULL;
  optind = 0;
  optopt = 0;

  while( (ch = getopt_long(argc, argv, "hv", options, NULL)) != EOF )
  {
    switch(ch)
    {
      case 'h':
        g_print ( 
      	  _("%s: A gnomified 'Hello World' program\n\n"
      	    "Usage: %s [--help] [--version]\n\n"
      	    "Options:\n"
      	    "        --help     display this help and exit\n"
      	    "        --version  output version information and exit\n"),
      	    argv[0], argv[0]);
        exit(0);
        break;
      case 'v':
        g_print (_("NetHack %s.\n"), VERSION_STRING);
        exit(0);
        break;
      case ':':
      case '?':
        g_print (_("Options error\n"));
        exit(0);
        break;
    }
  }

  /* SM stuff */
  session_id = gnome_client_new ();
#if 0
  session_id = gnome_client_new (
  	/* callback to save the state and parameter for it */
  	save_state, argv[0], 
  	/* callback to die and parameter for it */
    	NULL, NULL,
	/* id from the previous session if restarted, NULL otherwise */
       	id);
#endif
  /* set the program name */
  gnome_client_set_program (session_id, argv[0]);
  g_free(id);

  return;
}

/*
 * [ALI] Gnome installs its own handler(s) for SIGBUS, SIGFPE and SIGSEGV.
 * These handlers will fork and exec a helper program. When that helper
 * comes to initialize GTK+, it may fail if setuid/setgid. We solve this
 * by dropping privileges before passing the signal along the chain.
 * Note: We don't need to either drop or mask the saved ID since this
 * will be reset when the child process performs the execve() anyway.
 */

static struct {
    int signum;
    void (*handler)(int);
} ghack_chain[] = {
    {SIGBUS},
    {SIGFPE},
    {SIGSEGV},
    {SIGILL}		/* Not currently handled by Gnome */
};

static void ghack_sig_handler(int signum)
{
    int i;
    uid_t uid, euid;
    gid_t gid, egid;
    uid = getuid();
    euid = geteuid();
    gid = getgid();
    egid = getegid();
    if (gid != egid)
	setgid(gid);
    if (uid != euid)
	setuid(uid);
    for(i = SIZE(ghack_chain) - 1; i >= 0; i--)
	if (ghack_chain[i].signum == signum) {
	    ghack_chain[i].handler(signum);
	    break;
	}
    if (i < 0)
	impossible("Unhandled ghack signal");
    if (uid != euid)
	setuid(euid);
    if (gid != egid)
	setgid(egid);
}

/* initialize gnome and fir up the main window */
void ghack_init_main_window( int argc, char** argv)
{
    int i;
    struct timeval tv;
    uid_t uid, euid;

    /* It seems that the authors of gnome_score_init() drop group
     * priveledges.  We need group priveledges, so until we change the
     * way we save games to do things the gnome way(???), this stays
     * commented out.  (after hours of frusteration...)
     *  -Erik
     */
    /* gnome_score_init("gnomehack"); */

    gettimeofday(&tv, NULL);
    srand(tv.tv_usec);

    uid = getuid();
    euid = geteuid();
    if (uid != euid)
      setuid(uid);
    hide_privileges(TRUE);
    /* XXX gnome_init must print nethack options for --help, but does not */
    gnome_init ("nethack", VERSION_STRING, argc, argv);
    hide_privileges(FALSE);
    parse_args (argc, argv);

    /* Initialize the i18n stuff (not that gnomehack supperts it yet...) */
#if 0
    textdomain (PACKAGE);
#endif
    gdk_imlib_init();

    /* Main window */
    mainWindow = gnome_app_new((char *) "nethack", 
	    (char *) N_("Nethack for Gnome"));
    gtk_widget_realize(mainWindow);
    if (restarted) {
	gtk_widget_set_uposition (mainWindow, os_x, os_y);
	gtk_widget_set_usize     (mainWindow, os_w, os_h);
    }
    gtk_window_set_default_size( GTK_WINDOW(mainWindow), 800, 600);
    gtk_window_set_policy(GTK_WINDOW(mainWindow), FALSE, TRUE, TRUE);
    gnome_app_create_menus(GNOME_APP(mainWindow), mainmenu);
    gtk_signal_connect(GTK_OBJECT(mainWindow), "key_press_event", 
	    GTK_SIGNAL_FUNC(ghack_main_window_key_press), NULL);
    gtk_signal_connect(GTK_OBJECT(mainWindow), "delete_event", 
	    GTK_SIGNAL_FUNC(ghack_quit_game_cb), NULL);

    /* Put some stuff into our main window */
    vBoxMain = gtk_vbox_new (FALSE, 0);
    hBoxFirstRow = gtk_hbox_new (FALSE, 0);
   
    /* pack Boxes into other boxes to produce the right structure */
    gtk_box_pack_start (GTK_BOX (vBoxMain), hBoxFirstRow, FALSE, TRUE, 0);   
    
    /* pack vBoxMain which contains all our widgets into the main window. */
    gnome_app_set_contents(GNOME_APP(mainWindow), vBoxMain);
    
    /* DONT show the main window yet, due to a Gtk bug that causes it
     * to not refresh the window when adding widgets after the window 
     * has already been shown */
    if (uid != euid)
      setuid(euid);
    for(i = 0; i < SIZE(ghack_chain); i++)
	ghack_chain[i].handler =
	  signal(ghack_chain[i].signum, ghack_sig_handler);
}

void ghack_main_window_add_map_window(GtkWidget* win) 
{
    GtkWidget *vBox;
    
    vBox= gtk_vbox_new (TRUE, 0);
    gtk_box_pack_start (GTK_BOX (vBox), win, TRUE, TRUE, 2);
    gtk_box_pack_start (GTK_BOX (vBoxMain), vBox, TRUE, TRUE, 2);
    gtk_widget_show_all(vBox);
    /* Ok, now show the main window -- now that we have added in 
     * all the windows (relys on nethack displaying the map window last
     * (This is an ugly kludge, BTW)
     */
    gtk_widget_show_all(mainWindow);
}

void
ghack_main_window_add_message_window(GtkWidget* win)
{
    gtk_box_pack_start (GTK_BOX (hBoxFirstRow), win, TRUE, TRUE, 2);
    gtk_widget_show_all(win);
}

void
ghack_main_window_add_status_window(GtkWidget* win)
{
    gtk_box_pack_start (GTK_BOX (hBoxFirstRow), win, FALSE, TRUE, 2);
    gtk_widget_show_all(win);
}

void
ghack_main_window_add_worn_window(GtkWidget* win)
{
    gtk_box_pack_end (GTK_BOX (hBoxFirstRow), win, FALSE, TRUE, 2);
    gtk_widget_show_all(win);
}

void
ghack_main_window_add_text_window(GtkWidget *win)
{
  g_warning("Fixme!!! AddTextWindow is not yet implemented");
}

void
ghack_main_window_remove_window(GtkWidget *win)
{
  g_warning("Fixme!!! RemoveWindow is not yet implemented");
}

void
ghack_main_window_update_inventory()
{
/* For now, do very little.  Eventually we may allow the inv. window
     to stay active.  When we do this, we'll need to implement this...
   g_warning("Fixme!!! updateInventory is not yet implemented");
*/
	gnome_display_nhwindow(WIN_WORN, FALSE);
}

GtkWidget*
ghack_get_main_window()
{
      return( GTK_WIDGET(mainWindow) );
}