Source

xemacs-beta / src / menubar-gtk.c

Full commit
   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
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
/* Implements an elisp-programmable menubar -- Gtk interface.
   Copyright (C) 1993, 1994 Free Software Foundation, Inc.
   Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
   Copyright (C) 2002, 2003 Ben Wing.

This file is part of XEmacs.

XEmacs is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

XEmacs is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with XEmacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Synched up with: Not in FSF. */

/* created 16-dec-91 by jwz */

#include <config.h>
#include "lisp.h"

#include "buffer.h"
#include "commands.h"           /* zmacs_regions */
#include "device-impl.h"
#include "events.h"
#include "frame-impl.h"
#include "gui.h"
#include "opaque.h"
#include "window.h"
#include "window-impl.h"

#include "console-gtk-impl.h"
#include "ui-gtk.h"
#include "menubar.h"

#ifdef HAVE_GNOME
#include <libgnomeui/libgnomeui.h>
#endif

#define MENUBAR_TYPE	0
#define SUBMENU_TYPE	1
#define POPUP_TYPE	2

static GtkWidget *menu_descriptor_to_widget_1 (Lisp_Object descr, GtkAccelGroup* accel_group);

#define FRAME_GTK_MENUBAR_DATA(f) (FRAME_GTK_DATA (f)->menubar_data)
#define XFRAME_GTK_MENUBAR_DATA_LASTBUFF(f) XCAR (FRAME_GTK_MENUBAR_DATA (f))
#define XFRAME_GTK_MENUBAR_DATA_UPTODATE(f) XCDR (FRAME_GTK_MENUBAR_DATA (f))


/* This is a bogus subclass of GtkMenuBar so that the menu never tries
** to be bigger than the text widget.  This prevents weird resizing
** when jumping around between buffers with radically different menu
** sizes.
*/

#define GTK_XEMACS_MENUBAR(obj)		GTK_CHECK_CAST (obj, gtk_xemacs_menubar_get_type (), GtkXEmacsMenubar)
#define GTK_XEMACS_MENUBAR_CLASS(klass)	GTK_CHECK_CLASS_CAST (klass, gtk_xemacs_menubar_get_type (), GtkXEmacsMenubarClass)
#define GTK_IS_XEMACS_MENUBAR(obj)	GTK_CHECK_TYPE (obj, gtk_xemacs_menubar_get_type ())
#define GTK_XEMACS_MENUBAR_FRAME(obj)	GTK_XEMACS_MENUBAR (obj)->f

typedef struct _GtkXEmacsMenubar GtkXEmacsMenubar;
typedef struct _GtkXEmacsMenubarClass GtkXEmacsMenubarClass;

struct _GtkXEmacsMenubar
{
  GtkMenuBar menu;
  struct frame *frame;
};

struct _GtkXEmacsMenubarClass
{
  GtkMenuBarClass parent_class;
};

guint gtk_xemacs_menubar_get_type (void);
GtkWidget *gtk_xemacs_menubar_new (struct frame *f);

static void gtk_xemacs_menubar_class_init	(GtkXEmacsMenubarClass *klass);
static void gtk_xemacs_menubar_init		(GtkXEmacsMenubar *xemacs);
static void gtk_xemacs_menubar_size_request	(GtkWidget *widget, GtkRequisition *requisition);

guint
gtk_xemacs_menubar_get_type (void)
{
  static guint xemacs_menubar_type;

  if (!xemacs_menubar_type)
    {
      static const GtkTypeInfo xemacs_menubar_info =
      {
	"GtkXEmacsMenubar",
	sizeof (GtkXEmacsMenubar),
	sizeof (GtkXEmacsMenubarClass),
	(GtkClassInitFunc) gtk_xemacs_menubar_class_init,
	(GtkObjectInitFunc) gtk_xemacs_menubar_init,
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      xemacs_menubar_type = gtk_type_unique (gtk_menu_bar_get_type (), &xemacs_menubar_info);
    }

  return xemacs_menubar_type;
}

static GtkWidgetClass *menubar_parent_class;

static void
gtk_xemacs_menubar_class_init	(GtkXEmacsMenubarClass *klass)
{
  GtkWidgetClass *widget_class;

  widget_class = (GtkWidgetClass*) klass;
  menubar_parent_class = (GtkWidgetClass *) gtk_type_class (gtk_menu_bar_get_type ());

  widget_class->size_request = gtk_xemacs_menubar_size_request;
}

static void
gtk_xemacs_menubar_init		(GtkXEmacsMenubar *UNUSED (xemacs))
{
}

static void
gtk_xemacs_menubar_size_request	(GtkWidget *widget, GtkRequisition *requisition)
{
  GtkXEmacsMenubar *x = GTK_XEMACS_MENUBAR (widget);
  GtkRequisition frame_size;

  menubar_parent_class->size_request (widget, requisition);

  /* #### BILL!
  ** We should really only do this if the menu has not been detached!
  **
  ** WMP 9/9/2000
  */

  gtk_widget_size_request (FRAME_GTK_TEXT_WIDGET (x->frame), &frame_size);

  requisition->width = frame_size.width;
}

GtkWidget *
gtk_xemacs_menubar_new (struct frame *f)
{
  GtkXEmacsMenubar *menubar = (GtkXEmacsMenubar*) gtk_type_new (gtk_xemacs_menubar_get_type ());

  menubar->frame = f;

  return (GTK_WIDGET (menubar));
}

/*
 * Label with XEmacs accelerator character support.
 *
 * The default interfaces to GtkAccelLabel does not understand XEmacs
 * keystroke printing conventions, nor is it convenient in the places where is
 * it needed.  This subclass provides an alternative interface more suited to
 * XEmacs needs but does not add new functionality.
 */
#define GTK_TYPE_XEMACS_ACCEL_LABEL	       (gtk_xemacs_accel_label_get_type ())
#define GTK_XEMACS_ACCEL_LABEL(obj)	       (GTK_CHECK_CAST ((obj), GTK_TYPE_ACCEL_LABEL, GtkXEmacsAccelLabel))
#define GTK_XEMACS_ACCEL_LABEL_CLASS(klass)    (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_ACCEL_LABEL, GtkXEmacsAccelLabelClass))
#define GTK_IS_XEMACS_ACCEL_LABEL(obj)	       (GTK_CHECK_TYPE ((obj), GTK_TYPE_XEMACS_ACCEL_LABEL))
#define GTK_IS_XEMACS_ACCEL_LABEL_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_XEMACS_ACCEL_LABEL))

typedef struct _GtkXEmacsAccelLabel	    GtkXEmacsAccelLabel;
typedef struct _GtkXEmacsAccelLabelClass  GtkXEmacsAccelLabelClass;

/* Instance structure. No additional fields required. */
struct _GtkXEmacsAccelLabel
{
  GtkAccelLabel label;
};

/* Class structure. No additional fields required. */
struct _GtkXEmacsAccelLabelClass
{
  GtkAccelLabelClass	 parent_class;
};

static GtkType	  gtk_xemacs_accel_label_get_type(void);
static GtkWidget* gtk_xemacs_accel_label_new(const gchar *string);
static void       gtk_xemacs_set_accel_keys(GtkXEmacsAccelLabel* l,
				       Lisp_Object keys);
static void       gtk_xemacs_accel_label_class_init(GtkXEmacsAccelLabelClass *klass);
static void       gtk_xemacs_accel_label_init(GtkXEmacsAccelLabel *xemacs);

static GtkType
gtk_xemacs_accel_label_get_type(void)
{
  static GtkType xemacs_accel_label_type = 0;

  if (!xemacs_accel_label_type)
    {
      static const GtkTypeInfo xemacs_accel_label_info =
      {
	"GtkXEmacsAccelLabel",
	sizeof (GtkXEmacsAccelLabel),
	sizeof (GtkXEmacsAccelLabelClass),
	(GtkClassInitFunc) gtk_xemacs_accel_label_class_init,
	(GtkObjectInitFunc) gtk_xemacs_accel_label_init,
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      xemacs_accel_label_type = gtk_type_unique (gtk_accel_label_get_type(), &xemacs_accel_label_info);
    }

  return xemacs_accel_label_type;
}

static void
gtk_xemacs_accel_label_class_init(GtkXEmacsAccelLabelClass *UNUSED (klass))
{
  /* Nothing to do. */
}

static void
gtk_xemacs_accel_label_init(GtkXEmacsAccelLabel *UNUSED (xemacs))
{
  /* Nothing to do. */
}

static GtkWidget*
gtk_xemacs_accel_label_new (const gchar *string)
{
  GtkXEmacsAccelLabel *xemacs_accel_label;
  
  xemacs_accel_label = (GtkXEmacsAccelLabel*) gtk_type_new (GTK_TYPE_XEMACS_ACCEL_LABEL);
  
  if (string && *string)
    gtk_label_set_text (GTK_LABEL (xemacs_accel_label), string);
  
  return GTK_WIDGET (xemacs_accel_label);
}

/* Make the string <keys> the accelerator string for the label. */
static void
gtk_xemacs_set_accel_keys(GtkXEmacsAccelLabel* l, Lisp_Object keys)
{
  g_return_if_fail (l != NULL);
  g_return_if_fail (GTK_IS_XEMACS_ACCEL_LABEL (l));

  /* Disable the standard way of finding the accelerator string for the
     label. */
  gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL(l), NULL);

  /* Set the string straight from the object. */
  if (STRINGP (keys) && XSTRING_LENGTH (keys))
    {
      l->label.accel_string = ITEXT_TO_EXTERNAL_MALLOC (XSTRING_DATA (keys), Qctext);
    }
  else
    {
      /* l->label.accel_string = NULL;*/
    }
}


/* We now return you to your regularly scheduled menus... */

int dockable_menubar;

/* #define TEAR_OFF_MENUS */

#ifdef TEAR_OFF_MENUS
int tear_off_menus;
#endif


/* Converting from XEmacs to GTK representation */
static Lisp_Object
menu_name_to_accelerator (Ibyte *name)
{
  while (*name) {
    if (*name=='%') {
      ++name;
      if (!(*name))
	return Qnil;
      if (*name=='_' && *(name+1))
	{
	  int accelerator = (int) (*(name+1));
	  return make_char (tolower (accelerator));
	}
    }
    ++name;
  }
  return Qnil;
}

#define XEMACS_MENU_DESCR_TAG "xemacs::menu::description"
#define XEMACS_MENU_FILTER_TAG "xemacs::menu::filter"
#define XEMACS_MENU_GUIID_TAG "xemacs::menu::gui_id"
#define XEMACS_MENU_FIRSTTIME_TAG "xemacs::menu::first_time"

static void __activate_menu(GtkMenuItem *, gpointer);

#ifdef TEAR_OFF_MENUS
static void
__torn_off_sir(GtkMenuItem *UNUSED (item), gpointer user_data)
{
  GtkWidget *menu_item = GTK_WIDGET (user_data);

  if (GTK_TEAROFF_MENU_ITEM (item)->torn_off)
    {
      /* Menu was just torn off */
      GUI_ID id = new_gui_id ();
      Lisp_Object menu_desc = Qnil;
      GtkWidget *old_submenu = GTK_MENU_ITEM (menu_item)->submenu;

      menu_desc = GET_LISP_FROM_VOID (gtk_object_get_data (GTK_OBJECT (menu_item), XEMACS_MENU_DESCR_TAG));

      /* GCPRO all of our very own */
      gcpro_popup_callbacks (id, menu_desc);

      /* Hide the now detached menu from the attentions of
         __activate_menu destroying the old submenu */
#if 0
      gtk_widget_ref (old_submenu);
      gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), gtk_menu_new ());
      gtk_widget_show_all (old_submenu);
#endif
    }
}
#endif

/* This is called when a menu is about to be shown... this is what
   does the delayed creation of the menu items.  We populate the
   submenu and away we go. */
static void
__maybe_destroy (GtkWidget *child, GtkWidget *UNUSED (precious))
{
  if (GTK_IS_MENU_ITEM (child) && !GTK_IS_TEAROFF_MENU_ITEM (child))
    {
      if (GTK_WIDGET_VISIBLE (child))
	{
	  /* If we delete the menu item that was 'active' when the
	     menu was cancelled, GTK gets upset because it tries to
	     remove the focus rectangle from a (now) dead widget.

	     This widget will eventually get killed because it will
	     not be visible the next time the window is shown.
	  */
	  gtk_widget_set_sensitive (child, FALSE);
	  gtk_widget_hide_all (child);
	}
      else
	{
	  gtk_widget_destroy (child);
	}
    }
}

/* If user_data != 0x00 then we are using a hook to build the menu. */
static void
__activate_menu(GtkMenuItem *item, gpointer user_data)
{
  Lisp_Object desc;
  gpointer force_clear = gtk_object_get_data (GTK_OBJECT (item), XEMACS_MENU_FIRSTTIME_TAG);

  gtk_object_set_data (GTK_OBJECT (item), XEMACS_MENU_FIRSTTIME_TAG, 0x00);

  /* Delete the old contents of the menu if we are the top level menubar */
  if (GTK_IS_MENU_BAR (GTK_WIDGET (item)->parent) || force_clear)
    {
      GtkWidget *selected = gtk_menu_get_active (GTK_MENU (item->submenu));

      gtk_container_foreach (GTK_CONTAINER (item->submenu),(GtkCallback) __maybe_destroy,
			     selected);
    }
  else if (gtk_container_children (GTK_CONTAINER (item->submenu)))
    {
      return;
    }

  desc = GET_LISP_FROM_VOID (gtk_object_get_data (GTK_OBJECT (item), XEMACS_MENU_DESCR_TAG));

#ifdef TEAR_OFF_MENUS
  /* Lets stick in a detacher just for giggles */
  if (tear_off_menus && !gtk_container_children (GTK_CONTAINER (item->submenu)))
  {
    GtkWidget *w = gtk_tearoff_menu_item_new ();
    gtk_widget_show (w);
    gtk_menu_append (GTK_MENU (item->submenu), w);
    gtk_signal_connect (GTK_OBJECT (w), "activate", GTK_SIGNAL_FUNC (__torn_off_sir), item);
  }
#endif

  if (user_data)
    {
      GUI_ID id = (GUI_ID) gtk_object_get_data (GTK_OBJECT (item), XEMACS_MENU_GUIID_TAG);
      Lisp_Object hook_fn;
      struct gcpro gcpro1, gcpro2;

      hook_fn = GET_LISP_FROM_VOID (gtk_object_get_data (GTK_OBJECT (item), XEMACS_MENU_FILTER_TAG));

      GCPRO2 (desc, hook_fn);

      desc = call1 (hook_fn, desc);

      UNGCPRO;

      ungcpro_popup_callbacks (id);
      gcpro_popup_callbacks (id, desc);
    }

  /* Build the child widgets */
  for (; !NILP (desc); desc = Fcdr (desc))
    {
      GtkWidget *next = NULL;
      Lisp_Object child = Fcar (desc);

      if (NILP (child))	/* the partition */
	{
	  /* Signal an error here?  The NILP handling is handled a
             layer higher where appropriate */
	}
      else
	{
	  next = menu_descriptor_to_widget_1 (child,
					      gtk_menu_ensure_uline_accel_group (GTK_MENU (item->submenu)));
	}

      if (!next)
	{
	  continue;
	}

      gtk_widget_show_all (next);
      gtk_menu_append (GTK_MENU (item->submenu), next);
    }
}

/* This is called whenever an item with a GUI_ID associated with it is
   destroyed.  This allows us to remove the references in gui-gtk.c
   that made sure callbacks and such were GCPRO-ed
*/
static void
__remove_gcpro_by_id (gpointer user_data)
{
  ungcpro_popup_callbacks ((GUI_ID) user_data);
}

static void
__kill_stupid_gtk_timer (GtkObject *obj, gpointer UNUSED (user_data))
{
  GtkMenuItem *mi = GTK_MENU_ITEM (obj);

  if (mi->timer)
    {
      gtk_timeout_remove (mi->timer);
      mi->timer = 0;
    }
}

/* Convert the XEmacs menu accelerator representation to Gtk mnemonic form. If
  no accelerator has been provided, put one at the start of the string (this
  mirrors the behaviour under X).  This algorithm is also found in
  dialog-gtk.el:gtk-popup-convert-underscores.
*/
static char *
convert_underscores(const Ibyte *name)
{
  char *rval;
  int i,j;
  int found_accel = FALSE;
  int underscores = 0;

  for (i = 0; name[i]; ++i)
    if (name[i] == '%' && name[i+1] == '_')
      {
	found_accel = TRUE;
      }
    else if (name[i] == '_')
      {
	underscores++;
      }

  /* Allocate space for the original string, plus zero byte plus extra space
     for all quoted underscores plus possible additional leading accelerator. */
  rval = (char*) xmalloc_and_zero (qxestrlen(name) + 1 + underscores
				   + (found_accel ? 0 : 1));

  if (!found_accel)
    rval[0] = '_';

  for (i = 0, j = (found_accel ? 0 : 1); name[i]; i++)
    {
      if (name[i]=='%')
	{
	  i++;
	  if (!(name[i]))
	    continue;
	  
	  if ((name[i] != '_') && (name[i] != '%'))
	    i--;

	  found_accel = TRUE;
	}
      else if (name[i] == '_')
	{
	  rval[j++] = '_';
	}

      rval[j++] = name[i];
    }

  return rval;
}

/* Remove the XEmacs menu accellerator representation from a string. */
static char *
remove_underscores(const Ibyte *name)
{
  char *rval = (char*) xmalloc_and_zero (qxestrlen(name) + 1);
  int i,j;

  for (i = 0, j = 0; name[i]; i++)
    {
      if (name[i]=='%') {
	i++;
	if (!(name[i]))
	  continue;

	if ((name[i] != '_') && (name[i] != '%'))
	  i--;
	else
	  continue;
      }
      rval[j++] = name[i];
    }
  return rval;
}

/* This converts an entire menu into a GtkMenuItem (with an attached
   submenu).  A menu is a list of (STRING [:keyword value]+ [DESCR]+)
   DESCR is either a list (meaning a submenu), a vector, or nil (if
   you include a :filter keyword) */
static GtkWidget *
menu_convert (Lisp_Object desc, GtkWidget *reuse,
	      GtkAccelGroup* menubar_accel_group)
{
  GtkWidget *menu_item = NULL;
  GtkWidget *submenu = NULL;
  Lisp_Object key, val;
  Lisp_Object include_p = Qnil, hook_fn = Qnil, config_tag = Qnil;
  Lisp_Object active_p = Qt;
  Lisp_Object accel;
  int included_spec = 0;
  int active_spec = 0;

  if (STRINGP (XCAR (desc)))
    {
      accel = menu_name_to_accelerator (XSTRING_DATA (XCAR (desc)));

      if (!reuse)
	{
	  char *temp_menu_name = convert_underscores (XSTRING_DATA (XCAR (desc)));
	  GtkWidget* accel_label = gtk_xemacs_accel_label_new(NULL);
	  guint accel_key;

	  gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
	  accel_key = gtk_label_parse_uline (GTK_LABEL (accel_label), temp_menu_name);

	  menu_item = gtk_menu_item_new ();
	  gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
	  gtk_widget_show (accel_label);

	  if (menubar_accel_group)
	    gtk_widget_add_accelerator (menu_item,
					"activate_item",
					menubar_accel_group,
					accel_key, GDK_MOD1_MASK,
					GTK_ACCEL_LOCKED);
	  free (temp_menu_name);
	}
      else
	{
	  menu_item = reuse;
	}

      submenu = gtk_menu_new ();
      gtk_widget_show (menu_item);
      gtk_widget_show (submenu);

      if (!reuse)
	gtk_signal_connect (GTK_OBJECT (menu_item), "destroy",
			    GTK_SIGNAL_FUNC (__kill_stupid_gtk_timer), NULL);

      /* Without this sometimes a submenu gets left on the screen -
      ** urk
      */
      if (GTK_MENU_ITEM (menu_item)->submenu)
	{
	  gtk_widget_destroy (GTK_MENU_ITEM (menu_item)->submenu);
	}

      gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);

      /* We put this bogus menu item in so that GTK does the right
      ** thing when the menu is near the screen border.
      **
      ** Aug 29, 2000
      */
      {
	GtkWidget *bogus_item = gtk_menu_item_new_with_label ("A suitably long label here...");

	gtk_object_set_data (GTK_OBJECT (menu_item), XEMACS_MENU_FIRSTTIME_TAG, (gpointer)0x01);
	gtk_widget_show_all (bogus_item);
	gtk_menu_append (GTK_MENU (submenu), bogus_item);
      }

      desc = Fcdr (desc);

      while (key = Fcar (desc), KEYWORDP (key))
	{
	  Lisp_Object cascade = desc;
	  desc = Fcdr (desc);
	  if (NILP (desc))
	    sferror ("keyword in menu lacks a value",
				 cascade);
	  val = Fcar (desc);
	  desc = Fcdr (desc);
	  if (EQ (key, Q_included))
	    include_p = val, included_spec = 1;
	  else if (EQ (key, Q_config))
	    config_tag = val;
	  else if (EQ (key, Q_filter))
	    hook_fn = val;
	  else if (EQ (key, Q_active))
	    active_p = val, active_spec = 1;
	  else if (EQ (key, Q_accelerator))
	    {
#if 0
	      if ( SYMBOLP (val)
		   || CHARP (val))
		wv->accel = STORE_LISP_IN_VOID (val);
	      else
		invalid_argument ("bad keyboard accelerator", val);
#endif
	    }
	  else if (EQ (key, Q_label))
	    {
	      /* implement in 21.2 */
	    }
	  else
	    invalid_argument ("unknown menu cascade keyword", cascade);
	}

      gtk_object_set_data (GTK_OBJECT (menu_item), XEMACS_MENU_DESCR_TAG, STORE_LISP_IN_VOID (desc));
      gtk_object_set_data (GTK_OBJECT (menu_item), XEMACS_MENU_FILTER_TAG, STORE_LISP_IN_VOID (hook_fn));

      if ((!NILP (config_tag)
	   && NILP (Fmemq (config_tag, Vmenubar_configuration)))
	  || (included_spec &&
              NILP (IGNORE_MULTIPLE_VALUES (Feval (include_p)))))
	{
	  return (NULL);
	}

      if (active_spec)
        active_p = IGNORE_MULTIPLE_VALUES (Feval (active_p));

      gtk_widget_set_sensitive (GTK_WIDGET (menu_item), ! NILP (active_p));
    }
  else
    {
      invalid_argument ("menu name (first element) must be a string",
			   desc);
    }

  /* If we are reusing a widget, we need to make sure we clean
  ** everything up.
  */
  if (reuse)
    {
      gpointer id = gtk_object_get_data (GTK_OBJECT (reuse), XEMACS_MENU_GUIID_TAG);

      if (id)
	{
	  /* If the menu item had a GUI_ID that means it was a filter menu */
	  __remove_gcpro_by_id (id);
	  gtk_signal_disconnect_by_func (GTK_OBJECT (reuse),
					 GTK_SIGNAL_FUNC (__activate_menu),
					 (gpointer) 0x01 );
	}
      else
	{
	  gtk_signal_disconnect_by_func (GTK_OBJECT (reuse),
					 GTK_SIGNAL_FUNC (__activate_menu),
					 NULL);
	}

      GTK_MENU_ITEM (reuse)->right_justify = 0;
    }

  if (NILP (hook_fn))
    {
      /* Generic menu builder */
      gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
			  GTK_SIGNAL_FUNC (__activate_menu),
			  NULL);
    }
  else
    {
      GUI_ID id = new_gui_id ();

      gtk_object_set_data (GTK_OBJECT (menu_item), XEMACS_MENU_GUIID_TAG,
			   (gpointer) id);

      /* Make sure we gcpro the menu descriptions */
      gcpro_popup_callbacks (id, desc);
      gtk_object_weakref (GTK_OBJECT (menu_item), __remove_gcpro_by_id,
			  (gpointer) id);

      gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
			  GTK_SIGNAL_FUNC (__activate_menu),
			  (gpointer) 0x01);
    }

  return (menu_item);
}

/* Called whenever a button, radio, or toggle is selected in the menu */
static void
__generic_button_callback (GtkMenuItem *item, gpointer user_data)
{
  Lisp_Object callback, function, data, channel;

  channel = wrap_frame (gtk_widget_to_frame (GTK_WIDGET (item)));

  callback = GET_LISP_FROM_VOID (user_data);

  get_gui_callback (callback, &function, &data);

  signal_special_gtk_user_event (channel, function, data);
}

/* Convert a single menu item descriptor to a suitable GtkMenuItem */
/* This function cannot GC.
   It is only called from menu_item_descriptor_to_widget_value, which
   prohibits GC. */
static GtkWidget *
menu_descriptor_to_widget_1 (Lisp_Object descr, GtkAccelGroup* accel_group)
{
  if (STRINGP (descr))
    {
      /* It is a separator.  Unfortunately GTK does not allow us to
         specify what our separators look like, so we can't do all the
         fancy stuff that the X code does.
      */
      return (gtk_menu_item_new ());
    }
  else if (LISTP (descr))
    {
      /* It is a submenu */
      return (menu_convert (descr, NULL, accel_group));
    }
  else if (VECTORP (descr))
    {
      /* An actual menu item description!  This gets yucky. */
      Lisp_Object name       = Qnil;
      Lisp_Object callback   = Qnil;
      Lisp_Object suffix     = Qnil;
      Lisp_Object active_p   = Qt;
      Lisp_Object include_p  = Qt;
      Lisp_Object selected_p = Qnil;
      Lisp_Object keys       = Qnil;
      Lisp_Object style      = Qnil;
      Lisp_Object config_tag = Qnil;
      Lisp_Object accel = Qnil;
      GtkWidget *main_label = NULL;
      int length = XVECTOR_LENGTH (descr);
      Lisp_Object *contents = XVECTOR_DATA (descr);
      int plist_p;
      int selected_spec = 0, included_spec = 0;
      GtkWidget *widget = NULL;
      guint accel_key;

      if (length < 2)
	sferror ("button descriptors must be at least 2 long", descr);

      /* length 2:		[ "name" callback ]
	 length 3:		[ "name" callback active-p ]
	 length 4:		[ "name" callback active-p suffix ]
	 or			[ "name" callback keyword  value  ]
	 length 5+:		[ "name" callback [ keyword value ]+ ]
      */
      plist_p = (length >= 5 || (length > 2 && KEYWORDP (contents [2])));
      
      if (!plist_p && length > 2)
	/* the old way */
	{
	  name = contents [0];
	  callback = contents [1];
	  active_p = contents [2];
	  if (length == 4)
	    suffix = contents [3];
	}
      else
	{
	  /* the new way */
	  int i;
	  if (length & 1)
	    sferror (
				 "button descriptor has an odd number of keywords and values",
				 descr);

	  name = contents [0];
	  callback = contents [1];
	  for (i = 2; i < length;)
	    {
	      Lisp_Object key = contents [i++];
	      Lisp_Object val = contents [i++];
	      if (!KEYWORDP (key))
		invalid_argument_2 ("not a keyword", key, descr);

	      if      (EQ (key, Q_active))   active_p   = val;
	      else if (EQ (key, Q_suffix))   suffix     = val;
	      else if (EQ (key, Q_keys))     keys       = val;
	      else if (EQ (key, Q_key_sequence))  ; /* ignored for FSF compat */
	      else if (EQ (key, Q_label))  ; /* implement for 21.0 */
	      else if (EQ (key, Q_style))    style      = val;
	      else if (EQ (key, Q_selected)) selected_p = val, selected_spec = 1;
	      else if (EQ (key, Q_included)) include_p  = val, included_spec = 1;
	      else if (EQ (key, Q_config))	 config_tag = val;
	      else if (EQ (key, Q_accelerator))
		{
		  if ( SYMBOLP (val) || CHARP (val))
		    accel = val;
		  else
		    invalid_argument ("bad keyboard accelerator", val);
		}
	      else if (EQ (key, Q_filter))
		sferror(":filter keyword not permitted on leaf nodes", descr);
	      else
		invalid_argument_2 ("unknown menu item keyword", key, descr);
	    }
	}

#ifdef HAVE_MENUBARS
      if ((!NILP (config_tag) && NILP (Fmemq (config_tag, Vmenubar_configuration)))
	  || (included_spec && NILP (IGNORE_MULTIPLE_VALUES (Feval (include_p)))))

	{
	  /* the include specification says to ignore this item. */
	  return 0;
	}
#endif /* HAVE_MENUBARS */

      CHECK_STRING (name);

      if (NILP (accel))
	accel = menu_name_to_accelerator (XSTRING_DATA (name));

      if (!NILP (suffix))
        suffix = IGNORE_MULTIPLE_VALUES (Feval (suffix));


      if (!separator_string_p (XSTRING_DATA (name)))
	{
	  Ibyte *label_buffer = NULL;
	  char *temp_label = NULL;

	  if (STRINGP (suffix) && XSTRING_LENGTH (suffix))
	    {
	      /* !!#### */
	      label_buffer = alloca_ibytes (XSTRING_LENGTH (name) + 15 + XSTRING_LENGTH (suffix));
	      qxesprintf (label_buffer, "%s %s ", XSTRING_DATA (name),
			  XSTRING_DATA (suffix));
	    }
	  else
	    {
	      label_buffer = alloca_ibytes (XSTRING_LENGTH (name) + 15);
	      qxesprintf (label_buffer, "%s ", XSTRING_DATA (name));
	    }

	  temp_label = convert_underscores (label_buffer);
	  main_label = gtk_xemacs_accel_label_new (NULL);
	  accel_key = gtk_label_parse_uline (GTK_LABEL (main_label), temp_label);
	  free (temp_label);
	}

      /* Evaluate the selected and active items now */
      if (selected_spec)
	{
	  if (NILP (selected_p) || EQ (selected_p, Qt))
	    {
	      /* Do nothing */
	    }
	  else
	    {
              selected_p = IGNORE_MULTIPLE_VALUES (Feval (selected_p));
	    }
	}

      if (NILP (active_p) || EQ (active_p, Qt))
	{
	  /* Do Nothing */
	}
      else
	{
          active_p = IGNORE_MULTIPLE_VALUES (Feval (active_p));
	}

      if (0 || 
#ifdef HAVE_MENUBARS
	  menubar_show_keybindings
#endif
	  )
	{
	  /* Need to get keybindings */
	  if (!NILP (keys))
	    {
	      /* User-specified string to generate key bindings with */
	      CHECK_STRING (keys);

	      keys = Fsubstitute_command_keys (keys);
	    }
	  else if (SYMBOLP (callback))
	    {
	      DECLARE_EISTRING_MALLOC (buf);

	      /* #### Warning, dependency here on current_buffer and point */
	      where_is_to_char (callback, buf);

	      if (eilen (buf) > 0)
		keys = eimake_string (buf);
	      else
		{
		  
		  keys = Qnil;
		}

	      eifree (buf);
	    }
	}

      /* Now we get down to the dirty business of creating the widgets */
      if (NILP (style) || EQ (style, Qtext) || EQ (style, Qbutton))
	{
	  /* A normal menu item */
	  widget = gtk_menu_item_new ();
	}
      else if (EQ (style, Qtoggle) || EQ (style, Qradio))
	{
	  /* They are radio or toggle buttons.

	     XEmacs' menu descriptions are fairly lame in that they do
	     not have the idea of a 'group' of radio buttons.  They
	     are exactly like toggle buttons except that they get
	     drawn differently.

	     GTK rips us a new one again.  If you have a radio button
	     in a group by itself, it always draws it as highlighted.
	     So we dummy up and create a second radio button that does
	     not get added to the menu, but gets invisibly set/unset
	     when the other gets unset/set.  *sigh*

	  */
	  if (EQ (style, Qradio))
	    {
	      GtkWidget *dummy_sibling = NULL;
	      GSList *group = NULL;

	      dummy_sibling = gtk_radio_menu_item_new (group);
	      group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (dummy_sibling));
	      widget = gtk_radio_menu_item_new (group);

	      /* We need to notice when the 'real' one gets destroyed
                 so we can clean up the dummy as well. */
	      gtk_object_weakref (GTK_OBJECT (widget),
				  (GtkDestroyNotify) gtk_widget_destroy,
				  dummy_sibling);
	    }
	  else
	    {
	      widget = gtk_check_menu_item_new ();
	    }

	  /* What horrible defaults you have GTK dear!  The default
	    for a toggle menu item is to not show the toggle unless it
	    is turned on or actively highlighted.  How absolutely
	    hideous. */
	  gtk_check_menu_item_set_show_toggle (GTK_CHECK_MENU_ITEM (widget), TRUE);
	  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (widget),
					  NILP (selected_p) ? FALSE : TRUE);
	}
      else
	{
	  invalid_argument_2 ("unknown style", style, descr);
	}

      gtk_widget_set_sensitive (widget, ! NILP (active_p));

      gtk_signal_connect (GTK_OBJECT (widget), "activate-item",
			  GTK_SIGNAL_FUNC (__generic_button_callback),
			  STORE_LISP_IN_VOID (callback));

      gtk_signal_connect (GTK_OBJECT (widget), "activate",
			  GTK_SIGNAL_FUNC (__generic_button_callback),
			  STORE_LISP_IN_VOID (callback));

      /* Now that all the information about the menu item is know, set the
	 remaining properties.
      */
      
      if (main_label)
	{
	  gtk_container_add (GTK_CONTAINER (widget), main_label);

	  gtk_misc_set_alignment (GTK_MISC (main_label), 0.0, 0.5);
	  gtk_xemacs_set_accel_keys(GTK_XEMACS_ACCEL_LABEL(main_label), keys);

	  if (accel_group)
	    gtk_widget_add_accelerator (widget,
					"activate_item",
					accel_group,
					accel_key, 0,
					GTK_ACCEL_LOCKED);
	}

      return (widget);
    }
  else
    {
      return (NULL);
      /* ABORT (); ???? */
    }
}

static GtkWidget *
menu_descriptor_to_widget (Lisp_Object descr, GtkAccelGroup* accel_group)
{
  GtkWidget *rval = NULL;
  int count = begin_gc_forbidden ();

  /* Cannot GC from here on out... */
  rval = menu_descriptor_to_widget_1 (descr, accel_group);
  unbind_to (count);
  return (rval);
  
}

static gboolean
menu_can_reuse_widget (GtkWidget *child, const Ibyte *label)
{
  /* Everything up at the top level was done using
  ** gtk_xemacs_accel_label_new(), but we still double check to make
  ** sure we don't seriously foobar ourselves.
  */
  gpointer possible_child =
    g_list_nth_data (gtk_container_children (GTK_CONTAINER (child)), 0);
  gboolean ret_val = FALSE;

  if (possible_child && GTK_IS_LABEL (possible_child))
    {
      char *temp_label = remove_underscores (label);

      if (!strcmp (GTK_LABEL (possible_child)->label, temp_label))
	ret_val = TRUE;

      free (temp_label);
    }

  return ret_val;
}

/* Converts a menubar description into a GtkMenuBar... a menubar is a
   list of menus or buttons 
*/
static void
menu_create_menubar (struct frame *f, Lisp_Object descr)
{
  gboolean right_justify = FALSE;
  Lisp_Object value = descr;
  GtkWidget *menubar = FRAME_GTK_MENUBAR_WIDGET (f);
  GUI_ID id = (GUI_ID) gtk_object_get_data (GTK_OBJECT (menubar), XEMACS_MENU_GUIID_TAG);
  guint menu_position = 0;
  GtkAccelGroup *menubar_accel_group;

  /* Remove any existing protection for old menu items */
  ungcpro_popup_callbacks (id);

  /* GCPRO the whole damn thing */
  gcpro_popup_callbacks (id, descr);

  menubar_accel_group = gtk_accel_group_new();

  {
    EXTERNAL_LIST_LOOP_2 (item_descr, value)
      {
	gpointer current_child = g_list_nth_data (GTK_MENU_SHELL (menubar)->children, menu_position);

	if (NILP (item_descr))
	  {
	    /* Need to start right-justifying menus */
	    right_justify = TRUE;
	    menu_position--;
	  }
	else if (VECTORP (item_descr))
	  {
	    /* It is a button description */
	    GtkWidget *item;

	    item = menu_descriptor_to_widget (item_descr, menubar_accel_group);
	    gtk_widget_set_name (item, "XEmacsMenuButton");

	    if (!item)
	      {
		item = gtk_menu_item_new_with_label ("ITEM CREATION ERROR");
	      }

	    gtk_widget_show_all (item);
	    if (current_child) gtk_widget_destroy (GTK_WIDGET (current_child));
	    gtk_menu_bar_insert (GTK_MENU_BAR (menubar), item, menu_position);
	  }
	else if (LISTP (item_descr))
	  {
	    /* Need to actually convert it into a menu and slap it in */
	    GtkWidget *widget;
	    gboolean reused_p = FALSE;

	    /* We may be able to reuse the widget, let's at least check. */
	    if (current_child && menu_can_reuse_widget (GTK_WIDGET (current_child),
							XSTRING_DATA (XCAR (item_descr))))
	      {
		widget = menu_convert (item_descr, GTK_WIDGET (current_child),
				       menubar_accel_group);
		reused_p = TRUE;
	      }
	    else
	      {
		widget = menu_convert (item_descr, NULL, menubar_accel_group);
		if (current_child) gtk_widget_destroy (GTK_WIDGET (current_child));
		gtk_menu_bar_insert (GTK_MENU_BAR (menubar), widget, menu_position);
	      }

	    if (widget)
	      {
		if (right_justify) gtk_menu_item_right_justify (GTK_MENU_ITEM (widget));
	      }
	    else
	      {
		widget = gtk_menu_item_new_with_label ("ERROR");
		/* ABORT() */
	      }
	    gtk_widget_show_all (widget);
	  }
	else if (STRINGP (item_descr))
	  {
	    /* Do I really want to be this careful?  Anything else in a
	       menubar description is illegal */
	  }
	menu_position++;
      }
  }

  /* Need to delete any menu items that were past the bounds of the new one */
  {
    GList *l = NULL;

    while ((l = g_list_nth (GTK_MENU_SHELL (menubar)->children, menu_position)))
      {
	gpointer data = l->data;
	g_list_remove_link (GTK_MENU_SHELL (menubar)->children, l);

	if (data)
	  {
	    gtk_widget_destroy (GTK_WIDGET (data));
	  }
      }
  }

  /* Attach the new accelerator group to the frame. */
  gtk_window_add_accel_group (GTK_WINDOW (FRAME_GTK_SHELL_WIDGET(f)),
			      menubar_accel_group);
}


/* Deal with getting/setting the menubar */
#ifndef GNOME_IS_APP
#define GNOME_IS_APP(x) 0
#define gnome_app_set_menus(x,y)
#endif

static gboolean
run_menubar_hook (GtkWidget *widget, GdkEventButton *UNUSED (event),
		  gpointer UNUSED (user_data))
{
  if (!GTK_MENU_SHELL(widget)->active)
    {
      run_hook (Qactivate_menubar_hook);
    }
  return(FALSE);
}

static void
create_menubar_widget (struct frame *f)
{
  GUI_ID id = new_gui_id ();
  GtkWidget *handlebox = NULL;
  GtkWidget *menubar = gtk_xemacs_menubar_new (f);

  if (GNOME_IS_APP (FRAME_GTK_SHELL_WIDGET (f)))
    {
      gnome_app_set_menus (GNOME_APP (FRAME_GTK_SHELL_WIDGET (f)), GTK_MENU_BAR (menubar));
    }
  else if (dockable_menubar)
    {
      handlebox = gtk_handle_box_new ();
      gtk_handle_box_set_handle_position (GTK_HANDLE_BOX (handlebox), GTK_POS_LEFT);
      gtk_container_add (GTK_CONTAINER (handlebox), menubar);
      gtk_box_pack_start (GTK_BOX (FRAME_GTK_CONTAINER_WIDGET (f)), handlebox, FALSE, FALSE, 0);
    }
  else
    {
      gtk_box_pack_start (GTK_BOX (FRAME_GTK_CONTAINER_WIDGET (f)), menubar, FALSE, FALSE, 0);
    }

  gtk_signal_connect (GTK_OBJECT (menubar), "button-press-event",
		      GTK_SIGNAL_FUNC (run_menubar_hook), NULL);

  FRAME_GTK_MENUBAR_WIDGET (f) = menubar;
  gtk_object_set_data (GTK_OBJECT (menubar), XEMACS_MENU_GUIID_TAG, (gpointer) id);
  gtk_object_weakref (GTK_OBJECT (menubar), __remove_gcpro_by_id, (gpointer) id);
}

static int
set_frame_menubar (struct frame *f, int first_time_p)
{
  Lisp_Object menubar;
  int menubar_visible;
  /* As for the toolbar, the minibuffer does not have its own menubar. */
  struct window *w = XWINDOW (FRAME_LAST_NONMINIBUF_WINDOW (f));

  if (! FRAME_GTK_P (f))
    return 0;

  /***** first compute the contents of the menubar *****/

  if (! first_time_p)
    {
      /* evaluate `current-menubar' in the buffer of the selected window
	 of the frame in question. */
      menubar = symbol_value_in_buffer (Qcurrent_menubar, w->buffer);
    }
  else
    {
      /* That's a little tricky the first time since the frame isn't
	 fully initialized yet. */
      menubar = Fsymbol_value (Qcurrent_menubar);
    }

  if (NILP (menubar))
    {
      menubar = Vblank_menubar;
      menubar_visible = 0;
    }
  else
    {
      menubar_visible = !NILP (w->menubar_visible_p);
    }

  if (!FRAME_GTK_MENUBAR_WIDGET (f))
    {
      create_menubar_widget (f);
    }

  /* Populate the menubar, but nothing is shown yet */
  {
    Lisp_Object old_buffer;
    int count = specpdl_depth ();

    old_buffer = Fcurrent_buffer ();
    record_unwind_protect (Fset_buffer, old_buffer);
    Fset_buffer (XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer);

    menu_create_menubar (f, menubar);

    Fset_buffer (old_buffer);
    unbind_to (count);
  }

  FRAME_GTK_MENUBAR_DATA (f) = Fcons (XWINDOW (FRAME_LAST_NONMINIBUF_WINDOW (f))->buffer, Qt);

  return (menubar_visible);
}

/* Called from gtk_create_widgets() to create the initial menubar of a frame
   before it is mapped, so that the window is mapped with the menubar already
   there instead of us tacking it on later and thrashing the window after it
   is visible. */
int
gtk_initialize_frame_menubar (struct frame *f)
{
  create_menubar_widget  (f);
  return set_frame_menubar (f, 1);
}


static void
gtk_update_frame_menubar_internal (struct frame *f)
{
  /* We assume the menubar contents has changed if the global flag is set,
     or if the current buffer has changed, or if the menubar has never
     been updated before.
   */
  int menubar_contents_changed =
    (f->menubar_changed
     || NILP (FRAME_GTK_MENUBAR_DATA (f))
     || (!EQ (XFRAME_GTK_MENUBAR_DATA_LASTBUFF (f),
	      XWINDOW (FRAME_LAST_NONMINIBUF_WINDOW (f))->buffer)));

  gboolean menubar_was_visible = GTK_WIDGET_VISIBLE (FRAME_GTK_MENUBAR_WIDGET (f));
  gboolean menubar_will_be_visible = menubar_was_visible;
  gboolean menubar_visibility_changed;

  if (menubar_contents_changed)
    {
      menubar_will_be_visible = set_frame_menubar (f, 0);
    }

  menubar_visibility_changed = menubar_was_visible != menubar_will_be_visible;

  if (!menubar_visibility_changed)
    {
      return;
    }

  /* We hide and show the menubar's parent (which is actually the
     GtkHandleBox)... this is to simplify the code that destroys old
     menu items, etc.  There is no easy way to get the child out of a
     handle box, and I didn't want to add yet another stupid widget
     slot to struct gtk_frame. */
  if (menubar_will_be_visible)
    {
      gtk_widget_show_all (FRAME_GTK_MENUBAR_WIDGET (f)->parent);
    }
  else
    {
      gtk_widget_hide_all (FRAME_GTK_MENUBAR_WIDGET (f)->parent);
    }

  MARK_FRAME_SIZE_SLIPPED (f);
}

static void
gtk_update_frame_menubars (struct frame *f)
{
  GtkWidget *menubar = NULL;

  assert (FRAME_GTK_P (f));

  menubar = FRAME_GTK_MENUBAR_WIDGET (f);

  if ((GTK_MENU_SHELL (menubar)->active) ||
      (GTK_MENU_SHELL (menubar)->have_grab) ||
      (GTK_MENU_SHELL (menubar)->have_xgrab))
    {
      return;
    }
 
  gtk_update_frame_menubar_internal (f);
}

static void
gtk_free_frame_menubars (struct frame *f)
{
  GtkWidget *menubar_widget;

  assert (FRAME_GTK_P (f));

  menubar_widget = FRAME_GTK_MENUBAR_WIDGET (f);
  if (menubar_widget)
    {
      gtk_widget_destroy (menubar_widget);
    }
}

static void 
popdown_menu_cb (GtkMenuShell *UNUSED (menu), gpointer UNUSED (user_data))
{
  popup_up_p--;
}

static void
gtk_popup_menu (Lisp_Object menu_desc, Lisp_Object event)
{
  struct Lisp_Event *eev = NULL;
  GtkWidget *widget = NULL;
  GtkWidget *menu = NULL;
  gpointer id = NULL;

  /* Do basic error checking first... */
  if (SYMBOLP (menu_desc))
    menu_desc = Fsymbol_value (menu_desc);
  CHECK_CONS (menu_desc);
  CHECK_STRING (XCAR (menu_desc));

  /* Now lets get down to business... */
  widget = menu_descriptor_to_widget (menu_desc, NULL);
  menu = GTK_MENU_ITEM (widget)->submenu;
  gtk_widget_set_name (widget, "XEmacsPopupMenu");
  id = gtk_object_get_data (GTK_OBJECT (widget), XEMACS_MENU_GUIID_TAG);

  __activate_menu (GTK_MENU_ITEM (widget), id);

  if (!NILP (event))
    {
      CHECK_LIVE_EVENT (event);
      eev = XEVENT (event);

      if ((eev->event_type != button_press_event) &&
	  (eev->event_type != button_release_event))
	wrong_type_argument (Qmouse_event_p, event);
    }
  else if (!NILP (Vthis_command_keys))
    {
      /* If an event wasn't passed, use the last event of the event
         sequence currently being executed, if that event is a mouse
         event. */
      eev = XEVENT (Vthis_command_keys);
      if ((eev->event_type != button_press_event) &&
	  (eev->event_type != button_release_event))
	eev = NULL;
    }

  gtk_widget_show (menu);

  popup_up_p++;
  gtk_signal_connect (GTK_OBJECT (menu), "deactivate",
		      GTK_SIGNAL_FUNC (popdown_menu_cb), NULL);
		      
  gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
		  eev ? EVENT_BUTTON_BUTTON (eev) : 0,
		  eev ? eev->timestamp : GDK_CURRENT_TIME);
}

DEFUN ("gtk-build-xemacs-menu", Fgtk_build_xemacs_menu, 1, 1, 0, /*
Returns a GTK menu item from MENU, a standard XEmacs menu description.
See the definition of `popup-menu' for more information on the format of MENU.
*/
       (menu))
{
  GtkWidget *w = menu_descriptor_to_widget (menu, NULL);

  return (w ? build_gtk_object (GTK_OBJECT (w)) : Qnil);
}


void
syms_of_menubar_gtk (void)
{
  DEFSUBR (Fgtk_build_xemacs_menu);
}

void
console_type_create_menubar_gtk (void)
{
  CONSOLE_HAS_METHOD (gtk, update_frame_menubars);
  CONSOLE_HAS_METHOD (gtk, free_frame_menubars);
  CONSOLE_HAS_METHOD (gtk, popup_menu);
}

void
reinit_vars_of_menubar_gtk (void)
{
  dockable_menubar = 1;
#ifdef TEAR_OFF_MENUS
  tear_off_menus = 1;
#endif
}

void
vars_of_menubar_gtk (void)
{
  Fprovide (intern ("gtk-menubars"));
  DEFVAR_BOOL ("menubar-dockable-p", &dockable_menubar /*
If non-nil, the frame menubar can be detached into its own top-level window.
*/ );
#ifdef TEAR_OFF_MENUS
  DEFVAR_BOOL ("menubar-tearable-p", &tear_off_menus /*
If non-nil, menus can be torn off into their own top-level windows.
*/ );
#endif
}

/*---------------------------------------------------------------------------*/