]> code.delx.au - gnu-emacs/blobdiff - src/w32fns.c
Fix handling of image cache refcounts. (Bug#20802)
[gnu-emacs] / src / w32fns.c
index 176337080497f1ea5198eeb1a35275291ff89f50..5f40729011e91628e430580109e1eae3299c1767 100644 (file)
@@ -1,6 +1,6 @@
 /* Graphical user interface functions for the Microsoft Windows API.
 
-Copyright (C) 1989, 1992-2014 Free Software Foundation, Inc.
+Copyright (C) 1989, 1992-2015 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -29,6 +29,8 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <c-ctype.h>
+
 #include "lisp.h"
 #include "w32term.h"
 #include "frame.h"
@@ -93,19 +95,6 @@ extern char * w32_strerror (int error_no);
 #define IDC_HAND MAKEINTRESOURCE(32649)
 #endif
 
-Lisp_Object Qundefined_color;
-Lisp_Object Qcancel_timer;
-Lisp_Object Qfont_param;
-Lisp_Object Qhyper;
-Lisp_Object Qsuper;
-Lisp_Object Qmeta;
-Lisp_Object Qalt;
-Lisp_Object Qctrl;
-Lisp_Object Qcontrol;
-Lisp_Object Qshift;
-static Lisp_Object Qgeometry, Qworkarea, Qmm_size, Qframes;
-
-
 /* Prefix for system colors.  */
 #define SYSTEM_COLOR_PREFIX "System"
 #define SYSTEM_COLOR_PREFIX_LEN (sizeof (SYSTEM_COLOR_PREFIX) - 1)
@@ -170,8 +159,8 @@ typedef BOOL (WINAPI * TrackMouseEvent_Proc)
 typedef LONG (WINAPI * ImmGetCompositionString_Proc)
   (IN HIMC context, IN DWORD index, OUT LPVOID buffer, IN DWORD bufLen);
 typedef HIMC (WINAPI * ImmGetContext_Proc) (IN HWND window);
-typedef HWND (WINAPI * ImmReleaseContext_Proc) (IN HWND wnd, IN HIMC context);
-typedef HWND (WINAPI * ImmSetCompositionWindow_Proc) (IN HIMC context,
+typedef BOOL (WINAPI * ImmReleaseContext_Proc) (IN HWND wnd, IN HIMC context);
+typedef BOOL (WINAPI * ImmSetCompositionWindow_Proc) (IN HIMC context,
                                                      IN COMPOSITIONFORM *form);
 typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
 typedef BOOL (WINAPI * GetMonitorInfo_Proc)
@@ -215,7 +204,8 @@ unsigned int msh_mousewheel = 0;
 static unsigned menu_free_timer = 0;
 
 #ifdef GLYPH_DEBUG
-static int image_cache_refcount, dpyinfo_refcount;
+static ptrdiff_t image_cache_refcount;
+static int dpyinfo_refcount;
 #endif
 
 static HWND w32_visible_system_caret_hwnd;
@@ -359,10 +349,6 @@ x_real_positions (struct frame *f, int *xptr, int *yptr)
   /* Convert (0, 0) in the client area to screen co-ordinates.  */
   ClientToScreen (FRAME_W32_WINDOW (f), &pt);
 
-  /* Remember x_pixels_diff and y_pixels_diff.  */
-  f->x_pixels_diff = pt.x - rect.left;
-  f->y_pixels_diff = pt.y - rect.top;
-
   *xptr = rect.left;
   *yptr = rect.top;
 }
@@ -1146,7 +1132,8 @@ gamma_correct (struct frame *f, COLORREF *color)
    If ALLOC is nonzero, allocate a new colormap cell.  */
 
 int
-w32_defined_color (struct frame *f, const char *color, XColor *color_def, int alloc)
+w32_defined_color (struct frame *f, const char *color, XColor *color_def,
+                  bool alloc_p)
 {
   register Lisp_Object tem;
   COLORREF w32_color_ref;
@@ -1180,7 +1167,7 @@ w32_defined_color (struct frame *f, const char *color, XColor *color_def, int al
              entry = entry->next;
            }
 
-         if (entry == NULL && alloc)
+         if (entry == NULL && alloc_p)
            {
              /* not already mapped, so add to list */
              entry = xmalloc (sizeof (struct w32_palette_entry));
@@ -1233,7 +1220,7 @@ x_decode_color (struct frame *f, Lisp_Object arg, int def)
 
   /* w32_defined_color is responsible for coping with failures
      by looking for a near-miss.  */
-  if (w32_defined_color (f, SDATA (arg), &cdef, 1))
+  if (w32_defined_color (f, SDATA (arg), &cdef, true))
     return cdef.pixel;
 
   /* defined_color failed; return an ultimate default.  */
@@ -1537,7 +1524,7 @@ x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
 void
 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
 {
-  int result;
+  bool result;
 
   if (NILP (arg) && NILP (oldval))
     return;
@@ -1645,7 +1632,7 @@ x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldva
 
       if (FRAME_X_WINDOW (f) != 0)
        {
-         adjust_frame_size (f, -1, -1, 3, 0, Qinternal_border_width);
+         adjust_frame_size (f, -1, -1, 3, false, Qinternal_border_width);
 
          if (FRAME_VISIBLE_P (f))
            x_clear_under_internal_border (f);
@@ -1691,7 +1678,7 @@ x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
         of the outer rectangle (including decorations) unchanged, and a
         second time because we want to keep the height of the inner
         rectangle (without the decorations unchanged).  */
-      adjust_frame_size (f, -1, -1, 2, 1, Qmenu_bar_lines);
+      adjust_frame_size (f, -1, -1, 2, true, Qmenu_bar_lines);
 
       /* Not sure whether this is needed.  */
       x_clear_under_internal_border (f);
@@ -1734,6 +1721,7 @@ x_change_tool_bar_height (struct frame *f, int height)
   int old_height = FRAME_TOOL_BAR_HEIGHT (f);
   int lines = (height + unit - 1) / unit;
   int old_text_height = FRAME_TEXT_HEIGHT (f);
+  Lisp_Object fullscreen;
 
   /* Make sure we redisplay all windows in this frame.  */
   windows_or_buffers_changed = 23;
@@ -1757,8 +1745,14 @@ x_change_tool_bar_height (struct frame *f, int height)
   /* Recalculate toolbar height.  */
   f->n_tool_bar_rows = 0;
 
-  adjust_frame_size (f, -1, -1, (old_height == 0 || height == 0) ? 2 : 4, 0,
-                    Qtool_bar_lines);
+  adjust_frame_size (f, -1, -1,
+                    ((!f->tool_bar_redisplayed_once
+                      && (NILP (fullscreen =
+                                get_frame_param (f, Qfullscreen))
+                          || EQ (fullscreen, Qfullwidth))) ? 1
+                     : (old_height == 0 || height == 0) ? 2
+                     : 4),
+                    false, Qtool_bar_lines);
 
   /* adjust_frame_size might not have done anything, garbage frame
      here.  */
@@ -1768,6 +1762,50 @@ x_change_tool_bar_height (struct frame *f, int height)
     x_clear_under_internal_border (f);
 }
 
+static void
+w32_set_title_bar_text (struct frame *f, Lisp_Object name)
+{
+  if (FRAME_W32_WINDOW (f))
+    {
+      block_input ();
+#ifdef __CYGWIN__
+      GUI_FN (SetWindowText) (FRAME_W32_WINDOW (f),
+                              GUI_SDATA (GUI_ENCODE_SYSTEM (name)));
+#else
+      /* The frame's title many times shows the name of the file
+        visited in the selected window's buffer, so it makes sense to
+        support non-ASCII characters outside of the current system
+        codepage in the title.  */
+      if (w32_unicode_filenames)
+       {
+         Lisp_Object encoded_title = ENCODE_UTF_8 (name);
+         wchar_t *title_w;
+         int tlen = pMultiByteToWideChar (CP_UTF8, 0, SSDATA (encoded_title),
+                                          -1, NULL, 0);
+
+         if (tlen > 0)
+           {
+             /* Windows truncates the title text beyond what fits on
+                a single line, so we can limit the length to some
+                reasonably large value, and use alloca.  */
+             if (tlen > 10000)
+               tlen = 10000;
+             title_w = alloca ((tlen + 1) * sizeof (wchar_t));
+             pMultiByteToWideChar (CP_UTF8, 0, SSDATA (encoded_title), -1,
+                                   title_w, tlen);
+             title_w[tlen] = L'\0';
+             SetWindowTextW (FRAME_W32_WINDOW (f), title_w);
+           }
+         else  /* Conversion to UTF-16 failed, so we punt.  */
+           SetWindowTextA (FRAME_W32_WINDOW (f),
+                           SSDATA (ENCODE_SYSTEM (name)));
+       }
+      else
+       SetWindowTextA (FRAME_W32_WINDOW (f), SSDATA (ENCODE_SYSTEM (name)));
+#endif
+      unblock_input ();
+    }
+}
 
 /* Change the name of frame F to NAME.  If NAME is nil, set F's name to
        w32_id_name.
@@ -1781,7 +1819,7 @@ x_change_tool_bar_height (struct frame *f, int height)
        F->explicit_name is set, ignore the new name; otherwise, set it.  */
 
 void
-x_set_name (struct frame *f, Lisp_Object name, int explicit)
+x_set_name (struct frame *f, Lisp_Object name, bool explicit)
 {
   /* Make sure that requests from lisp code override requests from
      Emacs redisplay code.  */
@@ -1821,13 +1859,7 @@ x_set_name (struct frame *f, Lisp_Object name, int explicit)
   if (! NILP (f->title))
     name = f->title;
 
-  if (FRAME_W32_WINDOW (f))
-    {
-      block_input ();
-      GUI_FN (SetWindowText) (FRAME_W32_WINDOW (f),
-                              GUI_SDATA (GUI_ENCODE_SYSTEM (name)));
-      unblock_input ();
-    }
+  w32_set_title_bar_text (f, name);
 }
 
 /* This function should be called when the user's lisp code has
@@ -1836,7 +1868,7 @@ x_set_name (struct frame *f, Lisp_Object name, int explicit)
 void
 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
 {
-  x_set_name (f, arg, 1);
+  x_set_name (f, arg, true);
 }
 
 /* This function should be called by Emacs redisplay code to set the
@@ -1845,7 +1877,7 @@ x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
 void
 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
 {
-  x_set_name (f, arg, 0);
+  x_set_name (f, arg, false);
 }
 \f
 /* Change the title of frame F to NAME.
@@ -1865,13 +1897,7 @@ x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
   if (NILP (name))
     name = f->name;
 
-  if (FRAME_W32_WINDOW (f))
-    {
-      block_input ();
-      GUI_FN (SetWindowText) (FRAME_W32_WINDOW (f),
-                              GUI_SDATA (GUI_ENCODE_SYSTEM (name)));
-      unblock_input ();
-    }
+  w32_set_title_bar_text (f, name);
 }
 
 void
@@ -2553,7 +2579,7 @@ w32_msg_pump (deferred_msg * msg_buf)
                  thread-safe.  The next line is okay because the cons
                  cell is never made into garbage and is not relocated by
                  GC.  */
-             XSETCAR (XIL ((EMACS_INT) msg.lParam), Qnil);
+             XSETCAR (make_lisp_ptr ((void *)msg.lParam, Lisp_Cons), Qnil);
              if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
                emacs_abort ();
              break;
@@ -2561,16 +2587,10 @@ w32_msg_pump (deferred_msg * msg_buf)
              {
                int vk_code = (int) msg.wParam;
                int cur_state = (GetKeyState (vk_code) & 1);
-               Lisp_Object new_state = XIL ((EMACS_INT) msg.lParam);
-
-               /* NB: This code must be thread-safe.  It is safe to
-                   call NILP because symbols are not relocated by GC,
-                   and pointer here is not touched by GC (so the markbit
-                   can't be set).  Numbers are safe because they are
-                   immediate values.  */
-               if (NILP (new_state)
-                   || (NUMBERP (new_state)
-                       && ((XUINT (new_state)) & 1) != cur_state))
+               int new_state = msg.lParam;
+
+               if (new_state == -1
+                   || ((new_state & 1) != cur_state))
                  {
                    one_w32_display_info.faked_key = vk_code;
 
@@ -3274,12 +3294,12 @@ w32_wnd_proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
             field being reset to nil.  */
          f = x_window_to_frame (dpyinfo, hwnd);
          if (!(f && FRAME_LIVE_P (f)))
-           break;
+           goto dflt;
          w = XWINDOW (FRAME_SELECTED_WINDOW (f));
          /* Punt if someone changed the frame's selected window
             behind our back. */
          if (w != w32_system_caret_window)
-           break;
+           goto dflt;
 
          form.dwStyle = CFS_RECT;
          form.ptCurrentPos.x = w32_system_caret_x;
@@ -3297,16 +3317,22 @@ w32_wnd_proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 
          /* Punt if the window was deleted behind our back.  */
          if (!BUFFERP (w->contents))
-           break;
+           goto dflt;
 
          context = get_ime_context_fn (hwnd);
 
          if (!context)
-           break;
+           goto dflt;
 
          set_ime_composition_window_fn (context, &form);
          release_ime_context_fn (hwnd, context);
        }
+      /* We should "goto dflt" here to pass WM_IME_STARTCOMPOSITION to
+        DefWindowProc, so that the composition window will actually
+        be displayed.  But doing so causes trouble with displaying
+        dialog boxes, such as the file selection dialog or font
+        selection dialog.  So something else is needed to fix the
+        former without breaking the latter.  See bug#11732.  */
       break;
 
     case WM_IME_ENDCOMPOSITION:
@@ -4215,7 +4241,7 @@ my_create_tip_window (struct frame *f)
 /* Create and set up the w32 window for frame F.  */
 
 static void
-w32_window (struct frame *f, long window_prompting, int minibuffer_only)
+w32_window (struct frame *f, long window_prompting, bool minibuffer_only)
 {
   block_input ();
 
@@ -4224,7 +4250,7 @@ w32_window (struct frame *f, long window_prompting, int minibuffer_only)
      for the window manager, so GC relocation won't bother it.
 
      Elsewhere we specify the window name for the window manager.  */
-  f->namebuf = xstrdup (SSDATA (Vx_resource_name));
+  f->namebuf = xlispstrdup (Vx_resource_name);
 
   my_create_window (f);
 
@@ -4434,7 +4460,7 @@ This function is an internal primitive--use `make-frame' instead.  */)
   struct frame *f;
   Lisp_Object frame, tem;
   Lisp_Object name;
-  int minibuffer_only = 0;
+  bool minibuffer_only = false;
   long window_prompting = 0;
   ptrdiff_t count = SPECPDL_INDEX ();
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
@@ -4494,12 +4520,12 @@ This function is an internal primitive--use `make-frame' instead.  */)
   else if (EQ (tem, Qonly))
     {
       f = make_minibuffer_frame ();
-      minibuffer_only = 1;
+      minibuffer_only = true;
     }
   else if (WINDOWP (tem))
     f = make_frame_without_minibuffer (tem, kb, display);
   else
-    f = make_frame (1);
+    f = make_frame (true);
 
   XSETFRAME (frame, f);
 
@@ -4533,13 +4559,15 @@ This function is an internal primitive--use `make-frame' instead.  */)
   /* Specify the parent under which to make this window.  */
   if (!NILP (parent))
     {
-      f->output_data.w32->parent_desc = (Window) XFASTINT (parent);
-      f->output_data.w32->explicit_parent = 1;
+      /* Cast to UINT_PTR shuts up compiler warnings about cast to
+        pointer from integer of different size.  */
+      f->output_data.w32->parent_desc = (Window) (UINT_PTR) XFASTINT (parent);
+      f->output_data.w32->explicit_parent = true;
     }
   else
     {
       f->output_data.w32->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
-      f->output_data.w32->explicit_parent = 0;
+      f->output_data.w32->explicit_parent = false;
     }
 
   /* Set the name; the functions to which we pass f expect the name to
@@ -4547,12 +4575,12 @@ This function is an internal primitive--use `make-frame' instead.  */)
   if (EQ (name, Qunbound) || NILP (name))
     {
       fset_name (f, build_string (dpyinfo->w32_id_name));
-      f->explicit_name = 0;
+      f->explicit_name = false;
     }
   else
     {
       fset_name (f, name);
-      f->explicit_name = 1;
+      f->explicit_name = true;
       /* Use the frame's title when getting resources for this frame.  */
       specbind (Qx_resource_name, name);
     }
@@ -4630,7 +4658,8 @@ This function is an internal primitive--use `make-frame' instead.  */)
      had one frame line vs one toolbar line which left us with a zero
      root window height which was obviously wrong as well ...  */
   adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
-                    FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, 1, Qnil);
+                    FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, true,
+                    Qx_create_frame_1);
 
   /* The X resources controlling the menu-bar and tool-bar are
      processed specially at startup, and reflected in the mode
@@ -4648,8 +4677,6 @@ This function is an internal primitive--use `make-frame' instead.  */)
                       "bufferPredicate", "BufferPredicate", RES_TYPE_SYMBOL);
   x_default_parameter (f, parameters, Qtitle, Qnil,
                       "title", "Title", RES_TYPE_STRING);
-  x_default_parameter (f, parameters, Qfullscreen, Qnil,
-                      "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
 
   f->output_data.w32->dwStyle = WS_OVERLAPPEDWINDOW;
   f->output_data.w32->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
@@ -4664,7 +4691,7 @@ This function is an internal primitive--use `make-frame' instead.  */)
 
   f->output_data.w32->current_cursor = f->output_data.w32->nontext_cursor;
 
-  window_prompting = x_figure_window_size (f, parameters, 1);
+  window_prompting = x_figure_window_size (f, parameters, true);
 
   tem = x_get_arg (dpyinfo, parameters, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
   f->no_split = minibuffer_only || EQ (tem, Qt);
@@ -4698,15 +4725,22 @@ This function is an internal primitive--use `make-frame' instead.  */)
   /* Allow x_set_window_size, now.  */
   f->can_x_set_window_size = true;
 
-  adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, 1, Qnil);
+  adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, true,
+                    Qx_create_frame_2);
 
   /* Tell the server what size and position, etc, we want, and how
      badly we want them.  This should be done after we have the menu
      bar so that its size can be taken into account.  */
   block_input ();
-  x_wm_set_size_hint (f, window_prompting, 0);
+  x_wm_set_size_hint (f, window_prompting, false);
   unblock_input ();
 
+  /* Process fullscreen parameter here in the hope that normalizing a
+     fullheight/fullwidth frame will produce the size set by the last
+     adjust_frame_size call.  */
+  x_default_parameter (f, parameters, Qfullscreen, Qnil,
+                      "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
+
   /* Make the window appear on the frame and enable display, unless
      the caller says not to.  However, with explicit parent, Emacs
      cannot control visibility, so don't try.  */
@@ -4774,7 +4808,7 @@ DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
 
   CHECK_STRING (color);
 
-  if (w32_defined_color (f, SDATA (color), &foo, 0))
+  if (w32_defined_color (f, SDATA (color), &foo, false))
     return Qt;
   else
     return Qnil;
@@ -4789,7 +4823,7 @@ DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
 
   CHECK_STRING (color);
 
-  if (w32_defined_color (f, SDATA (color), &foo, 0))
+  if (w32_defined_color (f, SDATA (color), &foo, false))
     return list3i ((GetRValue (foo.pixel) << 8) | GetRValue (foo.pixel),
                   (GetGValue (foo.pixel) << 8) | GetGValue (foo.pixel),
                   (GetBValue (foo.pixel) << 8) | GetBValue (foo.pixel));
@@ -4906,25 +4940,38 @@ If omitted or nil, that stands for the selected frame's display.  */)
 }
 
 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
-       doc: /* Return the "vendor ID" string of the W32 system (Microsoft).
-The optional argument DISPLAY specifies which display to ask about.
-DISPLAY should be either a frame or a display name (a string).
+       doc: /* Return the "vendor ID" string of the GUI software on TERMINAL.
+
+\(Labeling every distributor as a "vendor" embodies the false assumption
+that operating systems cannot be developed and distributed noncommercially.)
+
+For GNU and Unix systems, this queries the X server software; for
+MS-Windows, this queries the OS.
+
+The optional argument TERMINAL specifies which display to ask about.
+TERMINAL should be a terminal object, a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (Lisp_Object display)
+  (Lisp_Object terminal)
 {
   return build_string ("Microsoft Corp.");
 }
 
 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
-       doc: /* Return the version numbers of the server of DISPLAY.
-The value is a list of three integers: the major and minor
-version numbers of the X Protocol in use, and the distributor-specific
-release number.  See also the function `x-server-vendor'.
+       doc: /* Return the version numbers of the GUI software on TERMINAL.
+The value is a list of three integers specifying the version of the GUI
+software in use.
 
-The optional argument DISPLAY specifies which display to ask about.
-DISPLAY should be either a frame or a display name (a string).
+For GNU and Unix system, the first 2 numbers are the version of the X
+Protocol used on TERMINAL and the 3rd number is the distributor-specific
+release number.  For MS-Windows, the 3 numbers report the version and
+the build number of the OS.
+
+See also the function `x-server-vendor'.
+
+The optional argument TERMINAL specifies which display to ask about.
+TERMINAL should be a terminal object, a frame or a display name (a string).
 If omitted or nil, that stands for the selected frame's display.  */)
-  (Lisp_Object display)
+  (Lisp_Object terminal)
 {
   return list3i (w32_major_version, w32_minor_version, w32_build_number);
 }
@@ -5634,7 +5681,7 @@ x_create_tip_frame (struct w32_display_info *dpyinfo,
   ptrdiff_t count = SPECPDL_INDEX ();
   struct gcpro gcpro1, gcpro2, gcpro3;
   struct kboard *kb;
-  int face_change_count_before = face_change_count;
+  bool face_change_before = face_change;
   Lisp_Object buffer;
   struct buffer *old_buffer;
 
@@ -5659,7 +5706,7 @@ x_create_tip_frame (struct w32_display_info *dpyinfo,
   frame = Qnil;
   GCPRO3 (parms, name, frame);
   /* Make a frame without minibuffer nor mode-line.  */
-  f = make_frame (0);
+  f = make_frame (false);
   f->wants_modeline = 0;
   XSETFRAME (frame, f);
 
@@ -5667,7 +5714,7 @@ x_create_tip_frame (struct w32_display_info *dpyinfo,
   buffer = Fget_buffer_create (tip);
   /* Use set_window_buffer instead of Fset_window_buffer (see
      discussion of bug#11984, bug#12025, bug#12026).  */
-  set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
+  set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, false, false);
   old_buffer = current_buffer;
   set_buffer_internal_1 (XBUFFER (buffer));
   bset_truncate_lines (current_buffer, Qnil);
@@ -5697,19 +5744,19 @@ x_create_tip_frame (struct w32_display_info *dpyinfo,
 #endif /* GLYPH_DEBUG */
   FRAME_KBOARD (f) = kb;
   f->output_data.w32->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
-  f->output_data.w32->explicit_parent = 0;
+  f->output_data.w32->explicit_parent = false;
 
   /* Set the name; the functions to which we pass f expect the name to
      be set.  */
   if (EQ (name, Qunbound) || NILP (name))
     {
       fset_name (f, build_string (dpyinfo->w32_id_name));
-      f->explicit_name = 0;
+      f->explicit_name = false;
     }
   else
     {
       fset_name (f, name);
-      f->explicit_name = 1;
+      f->explicit_name = true;
       /* use the frame's title when getting resources for this frame.  */
       specbind (Qx_resource_name, name);
     }
@@ -5770,7 +5817,7 @@ x_create_tip_frame (struct w32_display_info *dpyinfo,
   f->output_data.w32->dwStyle = WS_BORDER | WS_POPUP | WS_DISABLED;
   f->output_data.w32->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
 
-  window_prompting = x_figure_window_size (f, parms, 0);
+  window_prompting = x_figure_window_size (f, parms, false);
 
   /* No fringes on tip frame.  */
   f->fringe_cols = 0;
@@ -5798,7 +5845,7 @@ x_create_tip_frame (struct w32_display_info *dpyinfo,
   SET_FRAME_COLS (f, 0);
   SET_FRAME_LINES (f, 0);
   adjust_frame_size (f, width * FRAME_COLUMN_WIDTH (f),
-                    height * FRAME_LINE_HEIGHT (f), 0, 1, Qnil);
+                    height * FRAME_LINE_HEIGHT (f), 0, true, Qtip_frame);
 
   /* Add `tooltip' frame parameter's default value. */
   if (NILP (Fframe_parameter (frame, Qtooltip)))
@@ -5830,7 +5877,7 @@ x_create_tip_frame (struct w32_display_info *dpyinfo,
       Fmodify_frame_parameters (frame, colors);
   }
 
-  f->no_split = 1;
+  f->no_split = true;
 
   UNGCPRO;
 
@@ -5846,11 +5893,11 @@ x_create_tip_frame (struct w32_display_info *dpyinfo,
   f->can_x_set_window_size = true;
 
   /* Setting attributes of faces of the tooltip frame from resources
-     and similar will increment face_change_count, which leads to the
+     and similar will set face_change, which leads to the
      clearing of all current matrices.  Since this isn't necessary
-     here, avoid it by resetting face_change_count to the value it
+     here, avoid it by resetting face_change to the value it
      had before we created the tip frame.  */
-  face_change_count = face_change_count_before;
+  face_change = face_change_before;
 
   /* Discard the unwind_protect.  */
   return unbind_to (count, frame);
@@ -5977,7 +6024,8 @@ Text larger than the specified size is clipped.  */)
   int root_x, root_y;
   struct buffer *old_buffer;
   struct text_pos pos;
-  int i, width, height, seen_reversed_p;
+  int i, width, height;
+  bool seen_reversed_p;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
   int old_windows_or_buffers_changed = windows_or_buffers_changed;
   ptrdiff_t count = SPECPDL_INDEX ();
@@ -6106,7 +6154,7 @@ Text larger than the specified size is clipped.  */)
 
   FRAME_TOTAL_COLS (f) = WINDOW_TOTAL_COLS (w);
   adjust_frame_glyphs (f);
-  w->pseudo_window_p = 1;
+  w->pseudo_window_p = true;
 
   /* Display the tooltip text in a temporary buffer.  */
   old_buffer = current_buffer;
@@ -6118,7 +6166,8 @@ Text larger than the specified size is clipped.  */)
   try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
 
   /* Compute width and height of the tooltip.  */
-  width = height = seen_reversed_p = 0;
+  width = height = 0;
+  seen_reversed_p = false;
   for (i = 0; i < w->desired_matrix->nrows; ++i)
     {
       struct glyph_row *row = &w->desired_matrix->rows[i];
@@ -6130,7 +6179,7 @@ Text larger than the specified size is clipped.  */)
        break;
 
       /* Let the row go over the full width of the frame.  */
-      row->full_width_p = 1;
+      row->full_width_p = true;
 
       row_width = row->pixel_width;
       if (row->used[TEXT_AREA])
@@ -6141,7 +6190,7 @@ Text larger than the specified size is clipped.  */)
                 place the cursor there.  Don't include the width of
                 this glyph.  */
              last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
-             if (INTEGERP (last->object))
+             if (NILP (last->object))
                row_width -= last->pixel_width;
            }
          else
@@ -6151,10 +6200,10 @@ Text larger than the specified size is clipped.  */)
                 Don't count that glyph.  */
              struct glyph *g = row->glyphs[TEXT_AREA];
 
-             if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
+             if (g->type == STRETCH_GLYPH && NILP (g->object))
                {
                  row_width -= g->pixel_width;
-                 seen_reversed_p = 1;
+                 seen_reversed_p = true;
                }
            }
        }
@@ -6195,12 +6244,12 @@ Text larger than the specified size is clipped.  */)
 
          if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
            break;
-         row->full_width_p = 1;
+         row->full_width_p = true;
          row_width = row->pixel_width;
          if (row->used[TEXT_AREA] && !row->reversed_p)
            {
              last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
-             if (INTEGERP (last->object))
+             if (NILP (last->object))
                row_width -= last->pixel_width;
            }
 
@@ -6992,7 +7041,28 @@ a ShowWindow flag:
 
 #else  /* !CYGWIN */
 
-  current_dir = ENCODE_FILE (current_dir);
+  const char file_url_str[] = "file:///";
+  const int file_url_len = sizeof (file_url_str) - 1;
+  if (strncmp (SSDATA (document), file_url_str, file_url_len) == 0)
+    {
+      /* Passing "file:///" URLs to ShellExecute causes shlwapi.dll to
+        start a thread in some rare system configurations, for
+        unknown reasons.  That thread is started in the context of
+        the Emacs process, but out of control of our code, and seems
+        to never exit afterwards.  Each such thread reserves 8MB of
+        stack space (because that's the value recorded in the Emacs
+        executable at link time: Emacs needs a large stack).  So a
+        large enough number of invocations of w32-shell-execute can
+        potentially cause the Emacs process to run out of available
+        address space, which is nasty.  To work around this, we
+        convert such URLs to local file names, which seems to prevent
+        those threads from starting.  See bug #20220.  */
+      char *p = SSDATA (document) + file_url_len;
+
+      if (c_isalpha (*p) && p[1] == ':' && IS_DIRECTORY_SEP (p[2]))
+       document = Fsubstring_no_properties (document,
+                                            make_number (file_url_len), Qnil);
+    }
   /* We have a situation here.  If DOCUMENT is a relative file name,
      but its name includes leading directories, i.e. it lives not in
      CURRENT_DIR, but in its subdirectory, then ShellExecute below
@@ -7025,6 +7095,8 @@ a ShowWindow flag:
   else
     document = ENCODE_FILE (document);
   UNGCPRO;
+
+  current_dir = ENCODE_FILE (current_dir);
   if (use_unicode)
     {
       wchar_t document_w[MAX_PATH], current_dir_w[MAX_PATH];
@@ -7248,7 +7320,7 @@ The return value is the hotkey-id if registered, otherwise nil.  */)
       /* Notify input thread about new hot-key definition, so that it
         takes effect without needing to switch focus.  */
       PostThreadMessage (dwWindowsThreadId, WM_EMACS_REGISTER_HOT_KEY,
-                        (WPARAM) XLI (key), 0);
+                        (WPARAM) XINT (key), 0);
     }
 
   return key;
@@ -7268,10 +7340,17 @@ DEFUN ("w32-unregister-hot-key", Fw32_unregister_hot_key,
 
   if (!NILP (item))
     {
+      LPARAM lparam;
+
+      eassert (CONSP (item));
+      /* Pass the tail of the list as a pointer to a Lisp_Cons cell,
+        so that it works in a --with-wide-int build as well.  */
+      lparam = (LPARAM) XUNTAG (item, Lisp_Cons);
+
       /* Notify input thread about hot-key definition being removed, so
         that it takes effect without needing focus switch.  */
       if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_UNREGISTER_HOT_KEY,
-                            (WPARAM) XINT (XCAR (item)), (LPARAM) XLI (item)))
+                            (WPARAM) XINT (XCAR (item)), lparam))
        {
          MSG msg;
          GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
@@ -7326,10 +7405,15 @@ DEFUN ("w32-toggle-lock-key", Fw32_toggle_lock_key,
        doc: /* Toggle the state of the lock key KEY.
 KEY can be `capslock', `kp-numlock', or `scroll'.
 If the optional parameter NEW-STATE is a number, then the state of KEY
-is set to off if the low bit of NEW-STATE is zero, otherwise on.  */)
+is set to off if the low bit of NEW-STATE is zero, otherwise on.
+If NEW-STATE is omitted or nil, the function toggles the state,
+
+Value is the new state of the key, or nil if the function failed
+to change the state.  */)
   (Lisp_Object key, Lisp_Object new_state)
 {
   int vk_code;
+  LPARAM lparam;
 
   if (EQ (key, intern ("capslock")))
     vk_code = VK_CAPITAL;
@@ -7343,8 +7427,12 @@ is set to off if the low bit of NEW-STATE is zero, otherwise on.  */)
   if (!dwWindowsThreadId)
     return make_number (w32_console_toggle_lock_key (vk_code, new_state));
 
+  if (NILP (new_state))
+    lparam = -1;
+  else
+    lparam = (XUINT (new_state)) & 1;
   if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_TOGGLE_LOCK_KEY,
-                        (WPARAM) vk_code, (LPARAM) XLI (new_state)))
+                        (WPARAM) vk_code, lparam))
     {
       MSG msg;
       GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
@@ -7448,13 +7536,13 @@ elements (all size values are in pixels).
 
 - `title-bar-height' is the height of the title bar of FRAME.
 
-- `menu-bar-external' if `t' means the menu bar is by default external
+- `menu-bar-external' if t means the menu bar is by default external
   (not included in the inner size of FRAME).
 
 - `menu-bar-size' is a cons of the width and height of the menu bar of
   FRAME.
 
-- `tool-bar-external' if `t' means the tool bar is by default external
+- `tool-bar-external' if t means the tool bar is by default external
   (not included in the inner size of FRAME).
 
 - `tool-bar-side' tells tells on which side the tool bar on FRAME is by
@@ -7506,7 +7594,7 @@ elements (all size values are in pixels).
     menu_bar_height = single_bar_height;
 
   return
-    listn (CONSTYPE_PURE, 10,
+    listn (CONSTYPE_HEAP, 10,
           Fcons (Qframe_position,
                  Fcons (make_number (frame_outer_edges.left),
                         make_number (frame_outer_edges.top))),
@@ -8180,6 +8268,15 @@ w32_sys_ring_bell (struct frame *f)
     MessageBeep (sound_type);
 }
 
+DEFUN ("w32--menu-bar-in-use", Fw32__menu_bar_in_use, Sw32__menu_bar_in_use,
+       0, 0, 0,
+       doc: /* Return non-nil when a menu-bar menu is being used.
+Internal use only.  */)
+  (void)
+{
+  return menubar_in_use ? Qt : Qnil;
+}
+
 \f
 /***********************************************************************
                            Initialization
@@ -8251,6 +8348,14 @@ syms_of_w32fns (void)
   DEFSYM (Qworkarea, "workarea");
   DEFSYM (Qmm_size, "mm-size");
   DEFSYM (Qframes, "frames");
+  DEFSYM (Qtip_frame, "tip-frame");
+  DEFSYM (Qunicode_sip, "unicode-sip");
+
+  /* Symbols used elsewhere, but only in MS-Windows-specific code.  */
+  DEFSYM (Qgnutls_dll, "gnutls");
+  DEFSYM (Qlibxml2_dll, "libxml2");
+  DEFSYM (Qserif, "serif");
+  DEFSYM (Qzlib_dll, "zlib");
 
   Fput (Qundefined_color, Qerror_conditions,
        listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
@@ -8557,6 +8662,7 @@ only be necessary if the default setting causes problems.  */);
   defsubr (&Sw32_frame_rect);
   defsubr (&Sw32_frame_menu_bar_size);
   defsubr (&Sw32_battery_status);
+  defsubr (&Sw32__menu_bar_in_use);
 
 #ifdef WINDOWSNT
   defsubr (&Sfile_system_info);