Backport: Don't disable Unicode menus on Windows NT and later.
[bpt/emacs.git] / src / w32menu.c
index c31a8c1..0f45532 100644 (file)
@@ -1,5 +1,5 @@
 /* Menu support for GNU Emacs on the Microsoft W32 API.
 /* Menu support for GNU Emacs on the Microsoft W32 API.
-   Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2011
+   Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2012
                  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
                  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -40,6 +40,8 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
    if this is not done before the other system files.  */
 #include "w32term.h"
 
    if this is not done before the other system files.  */
 #include "w32term.h"
 
+#include "w32heap.h"
+
 /* Load sys/types.h if not already loaded.
    In some systems loading it twice is suicidal.  */
 #ifndef makedev
 /* Load sys/types.h if not already loaded.
    In some systems loading it twice is suicidal.  */
 #ifndef makedev
@@ -1173,18 +1175,23 @@ w32_dialog_show (FRAME_PTR f, int keymaps,
 static int
 is_simple_dialog (Lisp_Object contents)
 {
 static int
 is_simple_dialog (Lisp_Object contents)
 {
-  Lisp_Object options = XCDR (contents);
+  Lisp_Object options;
   Lisp_Object name, yes, no, other;
 
   Lisp_Object name, yes, no, other;
 
+  if (!CONSP (contents))
+    return 0;
+  options = XCDR (contents);
+
   yes = build_string ("Yes");
   no = build_string ("No");
 
   if (!CONSP (options))
     return 0;
 
   yes = build_string ("Yes");
   no = build_string ("No");
 
   if (!CONSP (options))
     return 0;
 
-  name = XCAR (XCAR (options));
-  if (!CONSP (options))
+  name = XCAR (options);
+  if (!CONSP (name))
     return 0;
     return 0;
+  name = XCAR (name);
 
   if (!NILP (Fstring_equal (name, yes)))
     other = no;
 
   if (!NILP (Fstring_equal (name, yes)))
     other = no;
@@ -1197,7 +1204,10 @@ is_simple_dialog (Lisp_Object contents)
   if (!CONSP (options))
     return 0;
 
   if (!CONSP (options))
     return 0;
 
-  name = XCAR (XCAR (options));
+  name = XCAR (options);
+  if (!CONSP (name))
+    return 0;
+  name = XCAR (name);
   if (NILP (Fstring_equal (name, other)))
     return 0;
 
   if (NILP (Fstring_equal (name, other)))
     return 0;
 
@@ -1219,10 +1229,11 @@ simple_dialog_show (FRAME_PTR f, Lisp_Object contents, Lisp_Object header)
      is_simple_dialog, we don't need to worry about checking contents
      to see what type of dialog to use.  */
 
      is_simple_dialog, we don't need to worry about checking contents
      to see what type of dialog to use.  */
 
-  /* Use unicode if possible, so any language can be displayed.  */
+  /* Use Unicode if possible, so any language can be displayed.  */
   if (unicode_message_box)
     {
       WCHAR *text, *title;
   if (unicode_message_box)
     {
       WCHAR *text, *title;
+      USE_SAFE_ALLOCA;
 
       if (STRINGP (temp))
        {
 
       if (STRINGP (temp))
        {
@@ -1232,7 +1243,7 @@ simple_dialog_show (FRAME_PTR f, Lisp_Object contents, Lisp_Object header)
             one utf16 word, so we cannot simply use the character
             length of temp.  */
          int utf8_len = strlen (utf8_text);
             one utf16 word, so we cannot simply use the character
             length of temp.  */
          int utf8_len = strlen (utf8_text);
-         text = alloca ((utf8_len + 1) * sizeof (WCHAR));
+         SAFE_ALLOCA (text, WCHAR *, (utf8_len + 1) * sizeof (WCHAR));
          utf8to16 (utf8_text, utf8_len, text);
        }
       else
          utf8to16 (utf8_text, utf8_len, text);
        }
       else
@@ -1252,6 +1263,7 @@ simple_dialog_show (FRAME_PTR f, Lisp_Object contents, Lisp_Object header)
        }
 
       answer = unicode_message_box (FRAME_W32_WINDOW (f), text, title, type);
        }
 
       answer = unicode_message_box (FRAME_W32_WINDOW (f), text, title, type);
+      SAFE_FREE ();
     }
   else
     {
     }
   else
     {
@@ -1358,6 +1370,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
   char *out_string, *p, *q;
   int return_value;
   size_t nlen, orig_len;
   char *out_string, *p, *q;
   int return_value;
   size_t nlen, orig_len;
+  USE_SAFE_ALLOCA;
 
   if (menu_separator_name_p (wv->name))
     {
 
   if (menu_separator_name_p (wv->name))
     {
@@ -1373,7 +1386,8 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
 
       if (wv->key != NULL)
        {
 
       if (wv->key != NULL)
        {
-         out_string = alloca (strlen (wv->name) + strlen (wv->key) + 2);
+         SAFE_ALLOCA (out_string, char *,
+                      strlen (wv->name) + strlen (wv->key) + 2);
          strcpy (out_string, wv->name);
          strcat (out_string, "\t");
          strcat (out_string, wv->key);
          strcpy (out_string, wv->name);
          strcat (out_string, "\t");
          strcat (out_string, wv->key);
@@ -1407,7 +1421,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
       if (nlen > orig_len)
         {
           p = out_string;
       if (nlen > orig_len)
         {
           p = out_string;
-          out_string = alloca (nlen + 1);
+          SAFE_ALLOCA (out_string, char *, nlen + 1);
           q = out_string;
           while (*p)
             {
           q = out_string;
           while (*p)
             {
@@ -1443,7 +1457,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
               out_string = (char *) local_alloc (strlen (wv->name) + 1);
               strcpy (out_string, wv->name);
 #ifdef MENU_DEBUG
               out_string = (char *) local_alloc (strlen (wv->name) + 1);
               strcpy (out_string, wv->name);
 #ifdef MENU_DEBUG
-             DebPrint ("Menu: allocing %ld for owner-draw", out_string);
+             DebPrint ("Menu: allocating %ld for owner-draw", out_string);
 #endif
              fuFlags = MF_OWNERDRAW | MF_DISABLED;
            }
 #endif
              fuFlags = MF_OWNERDRAW | MF_DISABLED;
            }
@@ -1467,7 +1481,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
       if (fuFlags & MF_OWNERDRAW)
        utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR));
       else
       if (fuFlags & MF_OWNERDRAW)
        utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR));
       else
-       utf16_string = alloca ((utf8_len + 1) * sizeof (WCHAR));
+       SAFE_ALLOCA (utf16_string, WCHAR *, (utf8_len + 1) * sizeof (WCHAR));
 
       utf8to16 (out_string, utf8_len, utf16_string);
       return_value = unicode_append_menu (menu, fuFlags,
 
       utf8to16 (out_string, utf8_len, utf16_string);
       return_value = unicode_append_menu (menu, fuFlags,
@@ -1476,7 +1490,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
                                          utf16_string);
       if (!return_value)
        {
                                          utf16_string);
       if (!return_value)
        {
-         /* On W9x/ME, unicode menus are not supported, though AppendMenuW
+         /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
             apparently does exist at least in some cases and appears to be
             stubbed out to do nothing.  out_string is UTF-8, but since
             our standard menus are in English and this is only going to
             apparently does exist at least in some cases and appears to be
             stubbed out to do nothing.  out_string is UTF-8, but since
             our standard menus are in English and this is only going to
@@ -1486,8 +1500,11 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
            AppendMenu (menu, fuFlags,
                        item != NULL ? (UINT) item: (UINT) wv->call_data,
                        out_string);
            AppendMenu (menu, fuFlags,
                        item != NULL ? (UINT) item: (UINT) wv->call_data,
                        out_string);
-         /* Don't use unicode menus in future.  */
-         unicode_append_menu = NULL;
+         /* Don't use Unicode menus in future, unless this is Windows
+            NT or later, where a failure of AppendMenuW does NOT mean
+            Unicode menus are unsupported.  */
+         if (osinfo_cache.dwPlatformId != VER_PLATFORM_WIN32_NT)
+           unicode_append_menu = NULL;
        }
 
       if (unicode_append_menu && (fuFlags & MF_OWNERDRAW))
        }
 
       if (unicode_append_menu && (fuFlags & MF_OWNERDRAW))
@@ -1536,6 +1553,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
                              FALSE, &info);
        }
     }
                              FALSE, &info);
        }
     }
+  SAFE_FREE ();
   return return_value;
 }
 
   return return_value;
 }