view src/gui.c @ 2367:ecf1ebac70d8

[xemacs-hg @ 2004-11-04 23:05:23 by ben] commit mega-patch configure.in: Turn off -Winline and -Wchar-subscripts. Use the right set of cflags when compiling modules. Rewrite ldap configuration to separate the inclusion of lber (needed in recent Cygwin) from the basic checks for the needed libraries. add a function for MAKE_JUNK_C; initially code was added to generate xemacs.def using this, but it will need to be rewritten. add an rm -f for junk.c to avoid weird Cygwin bug with cp -f onto an existing file. Sort list of auto-detected functions and eliminate unused checks for stpcpy, setlocale and getwd. Add autodetection of Cygwin scanf problems BETA: Rewrite section on configure to indicate what flags are important and what not. digest-doc.c, make-dump-id.c, profile.c, sorted-doc.c: Add proper decls for main(). make-msgfile.c: Document that this is old junk. Move proposal to text.c. make-msgfile.lex: Move proposal to text.c. make-mswin-unicode.pl: Convert error-generating code so that the entire message will be seen as a single unrecognized token. mule/mule-ccl.el: Update docs. lispref/mule.texi: Update CCL docs. ldap/eldap.c: Mule-ize. Use EXTERNAL_LIST_LOOP_2 instead of deleted EXTERNAL_LIST_LOOP. * XEmacs 21.5.18 "chestnut" is released. --------------------------------------------------------------- MULE-RELATED WORK: --------------------------------------------------------------- --------------------------- byte-char conversion --------------------------- buffer.c, buffer.h, insdel.c, text.c: Port FSF algorithm for byte-char conversion, replacing broken previous version. Track the char position of the gap. Add functions to do char-byte conversion downwards as well as upwards. Move comments about algorithm workings to internals manual. --------------------------- work on types --------------------------- alloc.c, console-x-impl.h, dump-data.c, dump-data.h, dumper.c, dialog-msw.c, dired-msw.c, doc.c, editfns.c, esd.c, event-gtk.h, event-msw.c, events.c, file-coding.c, file-coding.h, fns.c, glyphs-eimage.c, glyphs-gtk.c, glyphs-msw.c, glyphs-shared.c, glyphs-x.c, glyphs.c, glyphs.h, gui.c, hpplay.c, imgproc.c, intl-win32.c, lrecord.h, lstream.c, keymap.c, lisp.h, libsst.c, linuxplay.c, miscplay.c, miscplay.h, mule-coding.c, nas.c, nt.c, ntheap.c, ntplay.c, objects-msw.c, objects-tty.c, objects-x.c, print.c, process-nt.c, process.c, redisplay.h, select-common.h, select-gtk.c, select-x.c, sgiplay.c, sound.c, sound.h, sunplay.c, sysfile.h, sysdep.c, syswindows.h, text.c, unexnt.c, win32.c, xgccache.c: Further work on types. This creates a full set of types for all the basic semantics of `char' that I have so far identified, so that its semantics can always be identified for the purposes of proper Mule-safe code, and the raw use of `char' always avoided. (1) More type renaming, for consistency of naming. Char_ASCII -> Ascbyte UChar_ASCII -> UAscbyte Char_Binary -> CBinbyte UChar_Binary -> Binbyte SChar_Binary -> SBinbyte (2) Introduce Rawbyte, CRawbyte, Boolbyte, Chbyte, UChbyte, and Bitbyte and use them. (3) New types Itext, Wexttext and Textcount for separating out the concepts of bytes and textual units (different under UTF-16 and UTF-32, which are potential internal encodings). (4) qxestr*_c -> qxestr*_ascii. lisp.h: New; goes with other qxe() functions. #### Maybe goes in a different section. lisp.h: Group generic int-type defs together with EMACS_INT defs. lisp.h: * lisp.h (WEXTTEXT_IS_WIDE) New defns. lisp.h: New type to replace places where int occurs as a boolean. It's signed because occasionally people may want to use -1 as an error value, and because unsigned ints are viral -- see comments in the internals manual against using them. dynarr.c: int -> Bytecount. --------------------------- Mule-izing --------------------------- device-x.c: Partially Mule-ize. dumper.c, dumper.h: Mule-ize. Use Rawbyte. Use stderr_out not printf. Use wext_*(). sysdep.c, syswindows.h, text.c: New Wexttext API for manipulation of external text that may be Unicode (e.g. startup code under Windows). emacs.c: Mule-ize. Properly deal with argv in external encoding. Use wext_*() and Wexttext. Use Rawbyte. #if 0 some old junk on SCO that is unlikely to be correct. Rewrite allocation code in run-temacs. emacs.c, symsinit.h, win32.c: Rename win32 init function and call it even earlier, to initialize mswindows_9x_p even earlier, for use in startup code (XEUNICODE_P). process.c: Use _wenviron not environ under Windows, to get Unicode environment variables. event-Xt.c: Mule-ize drag-n-drop related stuff. dragdrop.c, dragdrop.h, frame-x.c: Mule-ize. text.h: Add some more stand-in defines for particular kinds of conversion; use in Mule-ization work in frame-x.c etc. --------------------------- Freshening --------------------------- intl-auto-encap-win32.c, intl-auto-encap-win32.h: Regenerate. --------------------------- Unicode-work --------------------------- intl-win32.c, syswindows.h: Factor out common options to MultiByteToWideChar and WideCharToMultiByte. Add convert_unicode_to_multibyte_malloc() and convert_unicode_to_multibyte_dynarr() and use. Add stuff for alloca() conversion of multibyte/unicode. alloc.c: Use dfc_external_data_len() in case of unicode coding system. alloc.c, mule-charset.c: Don't zero out and reinit charset Unicode tables. This fucks up dump-time loading. Anyway, either we load them at dump time or run time, never both. unicode.c: Dump the blank tables as well. --------------------------------------------------------------- DOCUMENTATION, MOSTLY MULE-RELATED: --------------------------------------------------------------- EmacsFrame.c, emodules.c, event-Xt.c, fileio.c, input-method-xlib.c, mule-wnnfns.c, redisplay-gtk.c, redisplay-tty.c, redisplay-x.c, regex.c, sysdep.c: Add comment about Mule work needed. text.h: Add more documentation describing why DFC routines were not written to return their value. Add some other DFC documentation. console-msw.c, console-msw.h: Add pointer to docs in win32.c. emacs.c: Add comments on sources of doc info. text.c, charset.h, unicode.c, intl-win32.c, intl-encap-win32.c, text.h, file-coding.c, mule-coding.c: Collect background comments and related to text matters and internationalization, and proposals for work to be done, in text.c or Internals manual, stuff related to specific textual API's in text.h, and stuff related to internal implementation of Unicode conversion in unicode.c. Put lots of pointers to the comments to make them easier to find. s/mingw32.h, s/win32-common.h, s/win32-native.h, s/windowsnt.h, win32.c: Add bunches of new documentation on the different kinds of builds and environments under Windows and how they work. Collect this info in win32.c. Add pointers to these docs in the relevant s/* files. emacs.c: Document places with long comments. Remove comment about exiting, move to internals manual, put in pointer. event-stream.c: Move docs about event queues and focus to internals manual, put in pointer. events.h: Move docs about event stream callbacks to internals manual, put in pointer. profile.c, redisplay.c, signal.c: Move documentation to the Internals manual. process-nt.c: Add pointer to comment in win32-native.el. lisp.h: Add comments about some comment conventions. lisp.h: Add comment about the second argument. device-msw.c, redisplay-msw.c: @@#### comments are out-of-date. --------------------------------------------------------------- PDUMP WORK (MOTIVATED BY UNICODE CHANGES) --------------------------------------------------------------- alloc.c, buffer.c, bytecode.c, console-impl.h, console.c, device.c, dumper.c, lrecord.h, elhash.c, emodules.h, events.c, extents.c, frame.c, glyphs.c, glyphs.h, mule-charset.c, mule-coding.c, objects.c, profile.c, rangetab.c, redisplay.c, specifier.c, specifier.h, window.c, lstream.c, file-coding.h, file-coding.c: PDUMP: Properly implement dump_add_root_block(), which never worked before, and is necessary for dumping Unicode tables. Pdump name changes for accuracy: XD_STRUCT_PTR -> XD_BLOCK_PTR. XD_STRUCT_ARRAY -> XD_BLOCK_ARRAY. XD_C_STRING -> XD_ASCII_STRING. *_structure_* -> *_block_*. lrecord.h: some comments added about dump_add_root_block() vs dump_add_root_block_ptr(). extents.c: remove incorrect comment about pdump problems with gap array. --------------------------------------------------------------- ALLOCATION --------------------------------------------------------------- abbrev.c, alloc.c, bytecode.c, casefiddle.c, device-msw.c, device-x.c, dired-msw.c, doc.c, doprnt.c, dragdrop.c, editfns.c, emodules.c, file-coding.c, fileio.c, filelock.c, fns.c, glyphs-eimage.c, glyphs-gtk.c, glyphs-msw.c, glyphs-x.c, gui-msw.c, gui-x.c, imgproc.c, intl-win32.c, lread.c, menubar-gtk.c, menubar.c, nt.c, objects-msw.c, objects-x.c, print.c, process-nt.c, process-unix.c, process.c, realpath.c, redisplay.c, search.c, select-common.c, symbols.c, sysdep.c, syswindows.h, text.c, text.h, ui-byhand.c: New macros {alloca,xnew}_{itext,{i,ext,raw,bin,asc}bytes} for more convenient allocation of these commonly requested items. Modify functions to use alloca_ibytes, alloca_array, alloca_extbytes, xnew_ibytes, etc. also XREALLOC_ARRAY, xnew. alloc.c: Rewrite the allocation functions to factor out repeated code. Add assertions for freeing dumped data. lisp.h: Moved down and consolidated with other allocation stuff. lisp.h, dynarr.c: New functions for allocation that's very efficient when mostly in LIFO order. lisp.h, text.c, text.h: Factor out some stuff for general use by alloca()-conversion funs. text.h, lisp.h: Fill out convenience routines for allocating various kinds of bytes and put them in lisp.h. Use them in place of xmalloc(), ALLOCA(). text.h: Fill out the convenience functions so the _MALLOC() kinds match the alloca() kinds. --------------------------------------------------------------- ERROR-CHECKING --------------------------------------------------------------- text.h: Create ASSERT_ASCTEXT_ASCII() and ASSERT_ASCTEXT_ASCII_LEN() from similar Eistring checkers and change the Eistring checkers to use them instead. --------------------------------------------------------------- MACROS IN LISP.H --------------------------------------------------------------- lisp.h: Redo GCPRO declarations. Create a "base" set of functions that can be used to generate any kind of gcpro sets -- regular, ngcpro, nngcpro, private ones used in GC_EXTERNAL_LIST_LOOP_2. buffer.c, callint.c, chartab.c, console-msw.c, device-x.c, dialog-msw.c, dired.c, extents.c, ui-gtk.c, rangetab.c, nt.c, mule-coding.c, minibuf.c, menubar-msw.c, menubar.c, menubar-gtk.c, lread.c, lisp.h, gutter.c, glyphs.c, glyphs-widget.c, fns.c, fileio.c, file-coding.c, specifier.c: Eliminate EXTERNAL_LIST_LOOP, which does not check for circularities. Use EXTERNAL_LIST_LOOP_2 instead or EXTERNAL_LIST_LOOP_3 or EXTERNAL_PROPERTY_LIST_LOOP_3 or GC_EXTERNAL_LIST_LOOP_2 (new macro). Removed/redid comments on EXTERNAL_LIST_LOOP. --------------------------------------------------------------- SPACING FIXES --------------------------------------------------------------- callint.c, hftctl.c, number-gmp.c, process-unix.c: Spacing fixes. --------------------------------------------------------------- FIX FOR GEOMETRY PROBLEM IN FIRST FRAME --------------------------------------------------------------- unicode.c: Add workaround for newlib bug in sscanf() [should be fixed by release 1.5.12 of Cygwin]. toolbar.c: bug fix for problem of initial frame being 77 chars wide on Windows. will be overridden by my other ws. --------------------------------------------------------------- FIX FOR LEAKING PROCESS HANDLES: --------------------------------------------------------------- process-nt.c: Fixes for leaking handles. Inspired by work done by Adrian Aichner <adrian@xemacs.org>. --------------------------------------------------------------- FIX FOR CYGWIN BUG (Unicode-related): --------------------------------------------------------------- unicode.c: Add workaround for newlib bug in sscanf() [should be fixed by release 1.5.12 of Cygwin]. --------------------------------------------------------------- WARNING FIXES: --------------------------------------------------------------- console-stream.c: `reinit' is unused. compiler.h, event-msw.c, frame-msw.c, intl-encap-win32.c, text.h: Add stuff to deal with ANSI-aliasing warnings I got. regex.c: Gather includes together to avoid warning. --------------------------------------------------------------- CHANGES TO INITIALIZATION ROUTINES: --------------------------------------------------------------- buffer.c, emacs.c, console.c, debug.c, device-x.c, device.c, dragdrop.c, emodules.c, eval.c, event-Xt.c, event-gtk.c, event-msw.c, event-stream.c, event-tty.c, events.c, extents.c, faces.c, file-coding.c, fileio.c, font-lock.c, frame-msw.c, glyphs-widget.c, glyphs.c, gui-x.c, insdel.c, lread.c, lstream.c, menubar-gtk.c, menubar-x.c, minibuf.c, mule-wnnfns.c, objects-msw.c, objects.c, print.c, scrollbar-x.c, search.c, select-x.c, text.c, undo.c, unicode.c, window.c, symsinit.h: Call reinit_*() functions directly from emacs.c, for clarity. Factor out some redundant init code. Move disallowed stuff that had crept into vars_of_glyphs() into complex_vars_of_glyphs(). Call init_eval_semi_early() from eval.c not in the middle of vars_of_() in emacs.c since there should be no order dependency in the latter calls. --------------------------------------------------------------- ARMAGEDDON: --------------------------------------------------------------- alloc.c, emacs.c, lisp.h, print.c: Rename inhibit_non_essential_printing_operations to inhibit_non_essential_conversion_operations. text.c: Assert on !inhibit_non_essential_conversion_operations. console-msw.c, print.c: Don't do conversion in SetConsoleTitle or FindWindow to avoid problems during armageddon. Put #errors for NON_ASCII_INTERNAL_FORMAT in places where problems would arise. --------------------------------------------------------------- CHANGES TO THE BUILD PROCEDURE: --------------------------------------------------------------- config.h.in, s/cxux.h, s/usg5-4-2.h, m/powerpc.h: Add comment about correct ordering of this file. Rearrange everything to follow this -- put all #undefs together and before the s&m files. Add undefs for HAVE_ALLOCA, C_ALLOCA, BROKEN_ALLOCA_IN_FUNCTION_CALLS, STACK_DIRECTION. Remove unused HAVE_STPCPY, HAVE_GETWD, HAVE_SETLOCALE. m/gec63.h: Deleted; totally broken, not used at all, not in FSF. m/7300.h, m/acorn.h, m/alliant-2800.h, m/alliant.h, m/altos.h, m/amdahl.h, m/apollo.h, m/att3b.h, m/aviion.h, m/celerity.h, m/clipper.h, m/cnvrgnt.h, m/convex.h, m/cydra5.h, m/delta.h, m/delta88k.h, m/dpx2.h, m/elxsi.h, m/ews4800r.h, m/gould.h, m/hp300bsd.h, m/hp800.h, m/hp9000s300.h, m/i860.h, m/ibmps2-aix.h, m/ibmrs6000.h, m/ibmrt-aix.h, m/ibmrt.h, m/intel386.h, m/iris4d.h, m/iris5d.h, m/iris6d.h, m/irist.h, m/isi-ov.h, m/luna88k.h, m/m68k.h, m/masscomp.h, m/mg1.h, m/mips-nec.h, m/mips-siemens.h, m/mips.h, m/news.h, m/nh3000.h, m/nh4000.h, m/ns32000.h, m/orion105.h, m/pfa50.h, m/plexus.h, m/pmax.h, m/powerpc.h, m/pyrmips.h, m/sequent-ptx.h, m/sequent.h, m/sgi-challenge.h, m/symmetry.h, m/tad68k.h, m/tahoe.h, m/targon31.h, m/tekxd88.h, m/template.h, m/tower32.h, m/tower32v3.h, m/ustation.h, m/vax.h, m/wicat.h, m/xps100.h: Delete C_ALLOCA, HAVE_ALLOCA, STACK_DIRECTION, BROKEN_ALLOCA_IN_FUNCTION_CALLS. All of this is auto-detected. When in doubt, I followed recent FSF sources, which also have these things deleted.
author ben
date Thu, 04 Nov 2004 23:08:28 +0000
parents 04bc9d2f42c7
children 6fa9919a9a0b
line wrap: on
line source

/* Generic GUI code. (menubars, scrollbars, toolbars, dialogs)
   Copyright (C) 1995 Board of Trustees, University of Illinois.
   Copyright (C) 1995, 1996, 2000, 2001, 2002, 2003 Ben Wing.
   Copyright (C) 1995 Sun Microsystems, Inc.
   Copyright (C) 1998 Free Software Foundation, Inc.

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. */

/* This file Mule-ized by Ben Wing, 3-24-02. */

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

#include "buffer.h"
#include "bytecode.h"
#include "elhash.h"
#include "gui.h"
#include "menubar.h"
#include "redisplay.h"

Lisp_Object Qmenu_no_selection_hook;
Lisp_Object Vmenu_no_selection_hook;

static Lisp_Object parse_gui_item_tree_list (Lisp_Object list);
Lisp_Object find_keyword_in_vector (Lisp_Object vector, Lisp_Object keyword);

Lisp_Object Qgui_error;

#ifdef HAVE_POPUPS

/* count of menus/dboxes currently up */
int popup_up_p;

DEFUN ("popup-up-p", Fpopup_up_p, 0, 0, 0, /*
Return t if a popup menu or dialog box is up, nil otherwise.
See `popup-menu' and `popup-dialog-box'.
*/
       ())
{
  return popup_up_p ? Qt : Qnil;
}
#endif /* HAVE_POPUPS */

int
separator_string_p (const Ibyte *s)
{
  const Ibyte *p;
  Ibyte first;

  if (!s || s[0] == '\0')
    return 0;
  first = s[0];
  if (first != '-' && first != '=')
    return 0;
  for (p = s; *p == first; p++)
    ;

  return (*p == '!' || *p == ':' || *p == '\0');
}

/* Massage DATA to find the correct function and argument.  Used by
   popup_selection_callback() and the msw code. */
void
get_gui_callback (Lisp_Object data, Lisp_Object *fn, Lisp_Object *arg)
{
  if (EQ (data, Qquit))
    {
      *fn = Qeval;
      *arg = list3 (Qsignal, list2 (Qquote, Qquit), Qnil);
      Vquit_flag = Qt;
    }
  else if (SYMBOLP (data)
	   || (COMPILED_FUNCTIONP (data)
	       && XCOMPILED_FUNCTION (data)->flags.interactivep)
	   || (CONSP (data) && (EQ (XCAR (data), Qlambda))
	       && !NILP (Fassq (Qinteractive, Fcdr (Fcdr (data))))))
    {
      *fn = Qcall_interactively;
      *arg = data;
    }
  else if (CONSP (data))
    {
      *fn = Qeval;
      *arg = data;
    }
  else
    {
      *fn = Qeval;
      *arg = list3 (Qsignal,
		    list2 (Qquote, Qerror),
		    list2 (Qquote, list2 (build_msg_string
					  ("illegal callback"),
					  data)));
    }
}

/*
 * Add a value VAL associated with keyword KEY into PGUI_ITEM
 * structure. If KEY is not a keyword, or is an unknown keyword, then
 * error is signaled.
 */
int
gui_item_add_keyval_pair (Lisp_Object gui_item,
			  Lisp_Object key, Lisp_Object val,
			  Error_Behavior errb)
{
  Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
  int retval = 0;

  if (!KEYWORDP (key))
    sferror_2 ("Non-keyword in gui item", key, pgui_item->name);

  if (EQ (key, Q_descriptor))
    {
      if (!EQ (pgui_item->name, val))
	{
	  retval = 1;
	  pgui_item->name   = val;
	}
    }
#define FROB(slot)				\
  else if (EQ (key, Q_##slot))			\
  {						\
    if (!EQ (pgui_item->slot, val))		\
      {						\
	retval = 1;				\
	pgui_item->slot   = val;		\
      }						\
  }
  FROB (suffix)
  FROB (active)
  FROB (included)
  FROB (config)
  FROB (filter)
  FROB (style)
  FROB (selected)
  FROB (keys)
  FROB (callback)
  FROB (callback_ex)
  FROB (value)
#undef FROB
  else if (EQ (key, Q_key_sequence)) ;   /* ignored for FSF compatibility */
  else if (EQ (key, Q_label)) ;   /* ignored for 21.0 implement in 21.2  */
  else if (EQ (key, Q_accelerator))
    {
      if (!EQ (pgui_item->accelerator, val))
	{
	  retval = 1;
	  if (SYMBOLP (val) || CHARP (val))
	    pgui_item->accelerator = val;
	  else if (ERRB_EQ (errb, ERROR_ME))
	    invalid_argument ("Bad keyboard accelerator", val);
	}
    }
  else if (ERRB_EQ (errb, ERROR_ME))
    invalid_argument_2 ("Unknown keyword in gui item", key, pgui_item->name);
  return retval;
}

void
gui_item_init (Lisp_Object gui_item)
{
  Lisp_Gui_Item *lp = XGUI_ITEM (gui_item);

  lp->name     = Qnil;
  lp->callback = Qnil;
  lp->callback_ex = Qnil;
  lp->suffix   = Qnil;
  lp->active   = Qt;
  lp->included = Qt;
  lp->config   = Qnil;
  lp->filter   = Qnil;
  lp->style    = Qnil;
  lp->selected = Qnil;
  lp->keys     = Qnil;
  lp->accelerator     = Qnil;
  lp->value = Qnil;
}

Lisp_Object
allocate_gui_item (void)
{
  Lisp_Gui_Item *lp = alloc_lcrecord_type (Lisp_Gui_Item, &lrecord_gui_item);
  Lisp_Object val;

  val = wrap_gui_item (lp);

  gui_item_init (val);

  return val;
}

/*
 * ITEM is a lisp vector, describing a menu item or a button. The
 * function extracts the description of the item into the PGUI_ITEM
 * structure.
 */
static Lisp_Object
make_gui_item_from_keywords_internal (Lisp_Object item,
				      Error_Behavior errb)
{
  int length, plist_p, start;
  Lisp_Object *contents;
  Lisp_Object gui_item = allocate_gui_item ();
  Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);

  CHECK_VECTOR (item);
  length = XVECTOR_LENGTH (item);
  contents = XVECTOR_DATA (item);

  if (length < 1)
    sferror ("GUI item descriptors must be at least 1 elts long", item);

  /* length 1:     		[ "name" ]
     length 2:		[ "name" callback ]
     length 3:		[ "name" callback active-p ]
     		   or	[ "name" keyword  value  ]
     length 4:		[ "name" callback active-p suffix ]
     		   or	[ "name" callback keyword  value  ]
     length 5+:		[ "name" callback [ keyword value ]+ ]
     		   or	[ "name" [ keyword value ]+ ]
  */
  plist_p = (length > 2 && (KEYWORDP (contents [1])
			    || KEYWORDP (contents [2])));

  pgui_item->name = contents [0];
  if (length > 1 && !KEYWORDP (contents [1]))
    {
      pgui_item->callback = contents [1];
      start = 2;
    }
  else
    start =1;

  if (!plist_p && length > 2)
    /* the old way */
    {
      pgui_item->active = contents [2];
      if (length == 4)
	pgui_item->suffix = contents [3];
    }
  else
    /* the new way */
    {
      int i;
      if ((length - start) & 1)
	sferror (
		"GUI item descriptor has an odd number of keywords and values",
		 item);

      for (i = start; i < length;)
	{
	  Lisp_Object key = contents [i++];
	  Lisp_Object val = contents [i++];
	  gui_item_add_keyval_pair (gui_item, key, val, errb);
	}
    }
  return gui_item;
}

/* This will only work with descriptors in the new format. */
Lisp_Object
widget_gui_parse_item_keywords (Lisp_Object item)
{
  int i, length;
  Lisp_Object *contents;
  Lisp_Object gui_item = allocate_gui_item ();
  Lisp_Object desc = find_keyword_in_vector (item, Q_descriptor);

  CHECK_VECTOR (item);
  length = XVECTOR_LENGTH (item);
  contents = XVECTOR_DATA (item);

  if (!NILP (desc) && !STRINGP (desc) && !VECTORP (desc))
    sferror ("Invalid GUI item descriptor", item);

  if (length & 1)
    {
      if (!SYMBOLP (contents [0]))
	sferror ("Invalid GUI item descriptor", item);
      contents++;			/* Ignore the leading symbol. */
      length--;
    }

  for (i = 0; i < length;)
    {
      Lisp_Object key = contents [i++];
      Lisp_Object val = contents [i++];
      gui_item_add_keyval_pair (gui_item, key, val, ERROR_ME_NOT);
    }

  return gui_item;
}

/* Update a gui item from a partial descriptor. */
int
update_gui_item_keywords (Lisp_Object gui_item, Lisp_Object item)
{
  int i, length, retval = 0;
  Lisp_Object *contents;

  CHECK_VECTOR (item);
  length = XVECTOR_LENGTH (item);
  contents = XVECTOR_DATA (item);

 if (length & 1)
    {
      if (!SYMBOLP (contents [0]))
	sferror ("Invalid GUI item descriptor", item);
      contents++;			/* Ignore the leading symbol. */
      length--;
    }

  for (i = 0; i < length;)
    {
      Lisp_Object key = contents [i++];
      Lisp_Object val = contents [i++];
      if (gui_item_add_keyval_pair (gui_item, key, val, ERROR_ME_DEBUG_WARN))
	retval = 1;
    }
  return retval;
}

Lisp_Object
gui_parse_item_keywords (Lisp_Object item)
{
  return make_gui_item_from_keywords_internal (item, ERROR_ME);
}

Lisp_Object
gui_parse_item_keywords_no_errors (Lisp_Object item)
{
  return make_gui_item_from_keywords_internal (item, ERROR_ME_DEBUG_WARN);
}

/* convert a gui item into plist properties */
void
gui_add_item_keywords_to_plist (Lisp_Object plist, Lisp_Object gui_item)
{
  Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);

  if (!NILP (pgui_item->callback))
    Fplist_put (plist, Q_callback, pgui_item->callback);
  if (!NILP (pgui_item->callback_ex))
    Fplist_put (plist, Q_callback_ex, pgui_item->callback_ex);
  if (!NILP (pgui_item->suffix))
    Fplist_put (plist, Q_suffix, pgui_item->suffix);
  if (!NILP (pgui_item->active))
    Fplist_put (plist, Q_active, pgui_item->active);
  if (!NILP (pgui_item->included))
    Fplist_put (plist, Q_included, pgui_item->included);
  if (!NILP (pgui_item->config))
    Fplist_put (plist, Q_config, pgui_item->config);
  if (!NILP (pgui_item->filter))
    Fplist_put (plist, Q_filter, pgui_item->filter);
  if (!NILP (pgui_item->style))
    Fplist_put (plist, Q_style, pgui_item->style);
  if (!NILP (pgui_item->selected))
    Fplist_put (plist, Q_selected, pgui_item->selected);
  if (!NILP (pgui_item->keys))
    Fplist_put (plist, Q_keys, pgui_item->keys);
  if (!NILP (pgui_item->accelerator))
    Fplist_put (plist, Q_accelerator, pgui_item->accelerator);
  if (!NILP (pgui_item->value))
    Fplist_put (plist, Q_value, pgui_item->value);
}

static int
gui_item_value (Lisp_Object form)
{
  /* This function can call Lisp. */

#ifndef ERROR_CHECK_DISPLAY
  /* Shortcut to avoid evaluating Qt/Qnil each time; but don't do it when
     error-checking so we catch unprotected eval within redisplay quicker */
  if (NILP (form))
    return 0;
  if (EQ (form, Qt))
    return 1;
#endif
  return !NILP (in_display ? eval_within_redisplay (form) : Feval (form));
}

/*
 * Decide whether a GUI item is active by evaluating its :active form
 * if any
 */
int
gui_item_active_p (Lisp_Object gui_item)
{
  return gui_item_value (XGUI_ITEM (gui_item)->active);
}

/* set menu accelerator key to first underlined character in menu name */
Lisp_Object
gui_item_accelerator (Lisp_Object gui_item)
{
  Lisp_Gui_Item *pgui = XGUI_ITEM (gui_item);

  if (!NILP (pgui->accelerator))
    return pgui->accelerator;

  else
    return gui_name_accelerator (pgui->name);
}

Lisp_Object
gui_name_accelerator (Lisp_Object nm)
{
  Ibyte *name = XSTRING_DATA (nm);

  while (*name)
    {
      if (*name == '%')
	{
	  ++name;
	  if (!(*name))
	    return Qnil;
	  if (*name == '_' && *(name + 1))
	    {
	      Ichar accelerator = itext_ichar (name + 1);
	      return make_char (DOWNCASE (0, accelerator));
	    }
	}
	INC_IBYTEPTR (name);
    }
  return make_char (DOWNCASE (0, itext_ichar (XSTRING_DATA (nm))));
}

/*
 * Decide whether a GUI item is selected by evaluating its :selected form
 * if any
 */
int
gui_item_selected_p (Lisp_Object gui_item)
{
  return gui_item_value (XGUI_ITEM (gui_item)->selected);
}

Lisp_Object
gui_item_list_find_selected (Lisp_Object gui_item_list)
{
  /* This function can call Lisp but cannot GC because it is called within
     redisplay, and redisplay disables GC. */
  Lisp_Object rest;
  LIST_LOOP (rest, gui_item_list)
    {
      if (gui_item_selected_p (XCAR (rest)))
	return XCAR (rest);
    }
  return XCAR (gui_item_list);
}

/*
 * Decide whether a GUI item is included by evaluating its :included
 * form if given, and testing its :config form against supplied CONFLIST
 * configuration variable
 */
int
gui_item_included_p (Lisp_Object gui_item, Lisp_Object conflist)
{
  /* This function can call lisp */
  Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);

  /* Evaluate :included first. Shortcut to avoid evaluating Qt each time */
  if (!gui_item_value (pgui_item->included))
    return 0;

  /* Do :config if conflist is given */
  if (!NILP (conflist) && !NILP (pgui_item->config)
      && NILP (Fmemq (pgui_item->config, conflist)))
    return 0;

  return 1;
}

/*
 * Format "left flush" display portion of an item.
 */
Lisp_Object
gui_item_display_flush_left (Lisp_Object gui_item)
{
  /* This function can call lisp */
  Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);
  Lisp_Object retval;

  CHECK_STRING (pgui_item->name);
  retval = pgui_item->name;

  if (!NILP (pgui_item->suffix))
    {
      Lisp_Object suffix = pgui_item->suffix;
      /* Shortcut to avoid evaluating suffix each time */
      if (!STRINGP (suffix))
	{
	  suffix = Feval (suffix);
	  CHECK_STRING (suffix);
	}

      retval = concat3 (pgui_item->name, build_string (" "), suffix);
    }

  return retval;
}

/*
 * Format "right flush" display portion of an item into BUF.
 */
Lisp_Object
gui_item_display_flush_right (Lisp_Object gui_item)
{
  Lisp_Gui_Item *pgui_item = XGUI_ITEM (gui_item);

#ifdef HAVE_MENUBARS
  /* Have keys? */
  if (!menubar_show_keybindings)
    return Qnil;
#endif

  /* Try :keys first */
  if (!NILP (pgui_item->keys))
    {
      CHECK_STRING (pgui_item->keys);
      return pgui_item->keys;
    }

  /* See if we can derive keys out of callback symbol */
  if (SYMBOLP (pgui_item->callback))
    {
      DECLARE_EISTRING_MALLOC (buf);
      Lisp_Object str;
      
      where_is_to_char (pgui_item->callback, buf);
      str = eimake_string (buf);
      eifree (buf);
      return str;
    }

  /* No keys - no right flush display */
  return Qnil;
}

static const struct memory_description gui_item_description [] = {
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, name) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, callback) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, callback_ex) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, suffix) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, active) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, included) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, config) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, filter) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, style) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, selected) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, keys) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, accelerator) },
  { XD_LISP_OBJECT, offsetof (struct Lisp_Gui_Item, value) },
  { XD_END }
};

static Lisp_Object
mark_gui_item (Lisp_Object obj)
{
  Lisp_Gui_Item *p = XGUI_ITEM (obj);

  mark_object (p->name);
  mark_object (p->callback);
  mark_object (p->callback_ex);
  mark_object (p->config);
  mark_object (p->suffix);
  mark_object (p->active);
  mark_object (p->included);
  mark_object (p->config);
  mark_object (p->filter);
  mark_object (p->style);
  mark_object (p->selected);
  mark_object (p->keys);
  mark_object (p->accelerator);
  mark_object (p->value);

  return Qnil;
}

static Hashcode
gui_item_hash (Lisp_Object obj, int depth)
{
  Lisp_Gui_Item *p = XGUI_ITEM (obj);

  return HASH2 (HASH6 (internal_hash (p->name, depth + 1),
		       internal_hash (p->callback, depth + 1),
		       internal_hash (p->callback_ex, depth + 1),
		       internal_hash (p->suffix, depth + 1),
		       internal_hash (p->active, depth + 1),
		       internal_hash (p->included, depth + 1)),
		HASH6 (internal_hash (p->config, depth + 1),
		       internal_hash (p->filter, depth + 1),
		       internal_hash (p->style, depth + 1),
		       internal_hash (p->selected, depth + 1),
		       internal_hash (p->keys, depth + 1),
		       internal_hash (p->value, depth + 1)));
}

int
gui_item_id_hash (Lisp_Object hashtable, Lisp_Object gitem, int slot)
{
  int hashid = gui_item_hash (gitem, 0);
  int id = GUI_ITEM_ID_BITS (hashid, slot);
  while (!UNBOUNDP (Fgethash (make_int (id), hashtable, Qunbound)))
    {
      id = GUI_ITEM_ID_BITS (id + 1, slot);
    }
  return id;
}

static int
gui_value_equal (Lisp_Object a, Lisp_Object b, int depth)
{
  if (in_display)
    return internal_equal_trapping_problems
      (Qredisplay, "Error calling function within redisplay", 0, 0,
       /* say they're not equal in case of error; code calling
	  gui_item_equal_sans_selected() in redisplay does extra stuff
	  only when equal */
       0, a, b, depth);
  else
    return internal_equal (a, b, depth);
}

int
gui_item_equal_sans_selected (Lisp_Object obj1, Lisp_Object obj2, int depth)
{
  Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
  Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);

  if (!(gui_value_equal (p1->name, p2->name, depth + 1)
	&&
	gui_value_equal (p1->callback, p2->callback, depth + 1)
	&&
	gui_value_equal (p1->callback_ex, p2->callback_ex, depth + 1)
	&&
	EQ (p1->suffix, p2->suffix)
	&&
	EQ (p1->active, p2->active)
	&&
	EQ (p1->included, p2->included)
	&&
	EQ (p1->config, p2->config)
	&&
	EQ (p1->filter, p2->filter)
	&&
	EQ (p1->style, p2->style)
	&&
	EQ (p1->accelerator, p2->accelerator)
	&&
	EQ (p1->keys, p2->keys)
	&&
	EQ (p1->value, p2->value)))
    return 0;
  return 1;
}

static int
gui_item_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
{
  Lisp_Gui_Item *p1 = XGUI_ITEM (obj1);
  Lisp_Gui_Item *p2 = XGUI_ITEM (obj2);

  if (!(gui_item_equal_sans_selected (obj1, obj2, depth) &&
	EQ (p1->selected, p2->selected)))
    return 0;
  return 1;
}

static void
print_gui_item (Lisp_Object obj, Lisp_Object printcharfun,
		int UNUSED (escapeflag))
{
  Lisp_Gui_Item *g = XGUI_ITEM (obj);

  if (print_readably)
    printing_unreadable_object ("#<gui-item 0x%x>", g->header.uid);

  write_fmt_string (printcharfun, "#<gui-item 0x%x>", g->header.uid);
}

Lisp_Object
copy_gui_item (Lisp_Object gui_item)
{
  Lisp_Object  ret = allocate_gui_item ();
  Lisp_Gui_Item *lp, *g = XGUI_ITEM (gui_item);

  lp = XGUI_ITEM (ret);
  lp->name     = g->name;
  lp->callback = g->callback;
  lp->callback_ex = g->callback_ex;
  lp->suffix   = g->suffix;
  lp->active   = g->active;
  lp->included = g->included;
  lp->config   = g->config;
  lp->filter   = g->filter;
  lp->style    = g->style;
  lp->selected = g->selected;
  lp->keys     = g->keys;
  lp->accelerator     = g->accelerator;
  lp->value = g->value;

  return ret;
}

Lisp_Object
copy_gui_item_tree (Lisp_Object arg)
{
  if (CONSP (arg))
    {
      Lisp_Object rest = arg = Fcopy_sequence (arg);
      while (CONSP (rest))
	{
	  XCAR (rest) = copy_gui_item_tree (XCAR (rest));
	  rest = XCDR (rest);
	}
      return arg;
    }
  else if (GUI_ITEMP (arg))
    return copy_gui_item (arg);
  else
    return arg;
}

/* parse a glyph descriptor into a tree of gui items.

   The gui_item slot of an image instance can be a single item or an
   arbitrarily nested hierarchy of item lists. */

static Lisp_Object
parse_gui_item_tree_item (Lisp_Object entry)
{
  Lisp_Object ret = entry;
  struct gcpro gcpro1;

  GCPRO1 (ret);

  if (VECTORP (entry))
    {
      ret = gui_parse_item_keywords_no_errors (entry);
    }
  else if (STRINGP (entry))
    {
      CHECK_STRING (entry);
    }
  else
    sferror ("item must be a vector or a string", entry);

  RETURN_UNGCPRO (ret);
}

Lisp_Object
parse_gui_item_tree_children (Lisp_Object list)
{
  Lisp_Object rest, ret = Qnil, sub = Qnil;
  struct gcpro gcpro1, gcpro2;

  GCPRO2 (ret, sub);
  CHECK_CONS (list);
  /* recursively add items to the tree view */
  LIST_LOOP (rest, list)
    {
      if (CONSP (XCAR (rest)))
	sub = parse_gui_item_tree_list (XCAR (rest));
      else
	sub = parse_gui_item_tree_item (XCAR (rest));

      ret = Fcons (sub, ret);
    }
  /* make the order the same as the items we have parsed */
  RETURN_UNGCPRO (Fnreverse (ret));
}

static Lisp_Object
parse_gui_item_tree_list (Lisp_Object list)
{
  Lisp_Object ret;
  struct gcpro gcpro1;
  CHECK_CONS (list);
  /* first one can never be a list */
  ret = parse_gui_item_tree_item (XCAR (list));
  GCPRO1 (ret);
  ret = Fcons (ret, parse_gui_item_tree_children (XCDR (list)));
  RETURN_UNGCPRO (ret);
}

static void
finalize_gui_item (void *UNUSED (header), int UNUSED (for_disksave))
{
}

DEFINE_LRECORD_IMPLEMENTATION ("gui-item", gui_item,
			       0, /*dumpable-flag*/
			       mark_gui_item, print_gui_item,
			       finalize_gui_item, gui_item_equal,
			       gui_item_hash,
			       gui_item_description,
			       Lisp_Gui_Item);

DOESNT_RETURN
gui_error (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qgui_error, reason, frob);
}

DOESNT_RETURN
gui_error_2 (const Ascbyte *reason, Lisp_Object frob0, Lisp_Object frob1)
{
  signal_error_2 (Qgui_error, reason, frob0, frob1);
}

void
syms_of_gui (void)
{
  INIT_LRECORD_IMPLEMENTATION (gui_item);

  DEFSYMBOL (Qmenu_no_selection_hook);

  DEFERROR_STANDARD (Qgui_error, Qio_error);

#ifdef HAVE_POPUPS
  DEFSUBR (Fpopup_up_p);
#endif
}

void
vars_of_gui (void)
{
  DEFVAR_LISP ("menu-no-selection-hook", &Vmenu_no_selection_hook /*
Function or functions to call when a menu or dialog box is dismissed
without a selection having been made.
*/ );
  Vmenu_no_selection_hook = Qnil;
}