Subject: Re: bug#14180: PATCH Better fullscreen frame support on Windows

From: Erik Charlebois <[email-masked]>
Date: Sat, 13 Apr 2013 21:23:16 -0400

I've rewritten the patch to preserve the maximized, fullwidth and
fullheight functionality and fix them
on Windows 8; came out cleaner in the end too.

The FULLSCREEN_WAIT flag now gets cleared prior to using want_fullscreen
(was a bug in the
existing implementation too I believe).

I've contacted the FSF clerk. Hopefully that can get cleared up soon.

2013-04-11  Erik Charlebois  <[email-masked]>

* src/w32fns.c (w32_fullscreen_rect): New function to compute the
window rectangle for the given fullscreen mode.
(w32_wnd_proc): When in a fullscreen mode, WM_WINDOWPOSCHANGING
no longer tunes the window size.  This keeps the window's edges
flush with the screen and allows the taskbar to hide itself in
fullboth.
* src/w32term.c (w32fullscreen_hook): fullboth now shows without
window decorations and uses the entire screen.

On Sat, Apr 13, 2013 at 7:56 AM, Eli Zaretskii <[email-masked]> wrote:

> > Date: Sat, 13 Apr 2013 13:33:56 +0200
> > From: Jan Djärv <[email-masked]>
> > CC: Erik Charlebois <[email-masked]>, [email-masked]
> >
> > 2013-04-13 12:09, Eli Zaretskii skrev:
> >
> > >
> > > Jan, does the fullscreen functionality on X also maximizes the frame
> > > to one of the monitors, when several monitors are available?  I don't
> > > want to introduce differences in behavior here.
> > >
> >
> > If there is a window manager running, we just tell the window manager to
> make
> > Emacs fullscreen, so it is really up to the WM.  The window managers I
> worked
> > with do maximize on one monitor only.
> >
> > When no WM is running we maximize to the whole display, i.e. all
> monitors.
> > But that is more or less just for debugging when you want to see Emacs
> <->
> > Xserver interractions without a WM getting in the way.  It is nothing a
> user
> > ever encounters.
>
> OK, thanks.  So I guess that part of Erik's patch is fine.
>
# Bazaar merge directive format 2 (Bazaar 0.90)
# revision_id: [email-masked]\
#   watpsk8w1701j22x
# target_branch: file:///C:/Users/Erik/Source/emacs/trunk/
# testament_sha1: 9d1bc31821b315ab0286031e20ed7c9d457a6e98
# timestamp: 2013-04-13 20:59:05 -0400
# base_revision_id: [email-masked]
# 
# Begin patch
=== modified file 'src/w32fns.c'
--- src/w32fns.c	2013-04-07 04:41:19 +0000
+++ src/w32fns.c	2013-04-14 00:58:45 +0000
@@ -157,6 +157,8 @@
 typedef HMONITOR (WINAPI * MonitorFromPoint_Proc) (IN POINT pt, IN DWORD flags);
 typedef BOOL (WINAPI * GetMonitorInfo_Proc)
   (IN HMONITOR monitor, OUT struct MONITOR_INFO* info);
+typedef HMONITOR (WINAPI * MonitorFromWindow_Proc)
+  (IN HWND hwnd, IN DWORD dwFlags);
 
 TrackMouseEvent_Proc track_mouse_event_fn = NULL;
 ImmGetCompositionString_Proc get_composition_string_fn = NULL;
@@ -165,6 +167,7 @@
 ImmSetCompositionWindow_Proc set_ime_composition_window_fn = NULL;
 MonitorFromPoint_Proc monitor_from_point_fn = NULL;
 GetMonitorInfo_Proc get_monitor_info_fn = NULL;
+MonitorFromWindow_Proc monitor_from_window_fn = NULL;
 
 #ifdef NTGUI_UNICODE
 #define unicode_append_menu AppendMenuW
@@ -336,6 +339,66 @@
   *yptr = rect.top;
 }
 
+/* Returns the window rectangle appropriate for the given fullscreen mode.
+   The normal rect parameter was the window's rectangle prior to entering
+   fullscreen mode.  If multiple monitor support is available, the nearest
+   monitor to the window is chosen.  */
+
+void
+w32_fullscreen_rect (HWND hwnd, int fsmode, RECT normal, RECT *rect)
+{
+  struct MONITOR_INFO mi = { sizeof(mi) };
+  if (monitor_from_window_fn && get_monitor_info_fn)
+    {
+      HMONITOR monitor =
+        monitor_from_window_fn (hwnd, MONITOR_DEFAULT_TO_NEAREST);
+      get_monitor_info_fn (monitor, &mi);
+    }
+  else
+    {
+      mi.rcMonitor.left = 0;
+      mi.rcMonitor.top = 0;
+      mi.rcMonitor.right = GetSystemMetrics (SM_CXSCREEN);
+      mi.rcMonitor.bottom = GetSystemMetrics (SM_CYSCREEN);
+      mi.rcWork.left = 0;
+      mi.rcWork.top = 0;
+      mi.rcWork.right = GetSystemMetrics (SM_CXMAXIMIZED);
+      mi.rcWork.bottom = GetSystemMetrics (SM_CYMAXIMIZED);
+    }
+
+  switch (fsmode)
+    {
+    case FULLSCREEN_BOTH:
+      rect->left = mi.rcMonitor.left;
+      rect->top = mi.rcMonitor.top;
+      rect->right = mi.rcMonitor.right;
+      rect->bottom = mi.rcMonitor.bottom;
+      break;
+    case FULLSCREEN_MAXIMIZED:
+      rect->left = mi.rcWork.left;
+      rect->top = mi.rcWork.top;
+      rect->right = mi.rcWork.right;
+      rect->bottom = mi.rcWork.bottom;
+      break;
+    case FULLSCREEN_WIDTH:
+      rect->left = mi.rcWork.left;
+      rect->top = normal.top;
+      rect->right = mi.rcWork.right;
+      rect->bottom = normal.bottom;
+      break;
+    case FULLSCREEN_HEIGHT:
+      rect->left = normal.left;
+      rect->top = mi.rcWork.top;
+      rect->right = normal.right;
+      rect->bottom = mi.rcWork.bottom;
+      break;
+    case FULLSCREEN_NONE:
+    default:
+      *rect = normal;
+      break;
+    }
+}
+
 
 
 DEFUN ("w32-define-rgb-color", Fw32_define_rgb_color,
@@ -3691,6 +3754,13 @@
       /* Don't restrict the sizing of tip frames.  */
       if (hwnd == tip_window)
 	return 0;
+
+      /* Don't restrict the sizing of fullscreened frames, allowing them to be
+         flush with the sides of the screen.  */
+      f = x_window_to_frame (dpyinfo, hwnd);
+      if (f && FRAME_PREV_FSMODE (f) != FULLSCREEN_NONE)
+        return 0;
+
       {
 	WINDOWPLACEMENT wp;
 	LPWINDOWPOS lppos = (WINDOWPOS *) lParam;
@@ -7635,6 +7705,8 @@
     GetProcAddress (user32_lib, "MonitorFromPoint");
   get_monitor_info_fn = (GetMonitorInfo_Proc)
     GetProcAddress (user32_lib, "GetMonitorInfoA");
+  monitor_from_window_fn = (MonitorFromWindow_Proc)
+    GetProcAddress (user32_lib, "MonitorFromWindow");
 
   {
     HMODULE imm32_lib = GetModuleHandle ("imm32.dll");

=== modified file 'src/w32term.c'
--- src/w32term.c	2013-04-01 07:58:04 +0000
+++ src/w32term.c	2013-04-14 00:58:45 +0000
@@ -5663,88 +5663,40 @@
 {
   if (FRAME_VISIBLE_P (f))
     {
-      int width, height, top_pos, left_pos, pixel_height, pixel_width;
-      int cur_w = FRAME_COLS (f), cur_h = FRAME_LINES (f);
-      RECT workarea_rect;
-
-      block_input ();
-      /* Record current "normal" dimensions for restoring later.  */
-      if (!(   FRAME_PREV_FSMODE (f) == FULLSCREEN_BOTH
-	    || FRAME_PREV_FSMODE (f) == FULLSCREEN_MAXIMIZED))
-	{
-	  if (FRAME_PREV_FSMODE (f) != FULLSCREEN_HEIGHT)
-	    {
-	      FRAME_NORMAL_HEIGHT (f) = cur_h;
-	      FRAME_NORMAL_TOP (f) = f->top_pos;
-	    }
-	  if (FRAME_PREV_FSMODE (f) != FULLSCREEN_WIDTH)
-	    {
-	      FRAME_NORMAL_WIDTH (f)  = cur_w;
-	      FRAME_NORMAL_LEFT (f) = f->left_pos;
-	    }
-	}
-      eassert (FRAME_NORMAL_HEIGHT (f) > 0);
-      eassert (FRAME_NORMAL_WIDTH (f) > 0);
-      x_real_positions (f, &f->left_pos, &f->top_pos);
-      x_fullscreen_adjust (f, &width, &height, &top_pos, &left_pos);
-
-      SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea_rect, 0);
-      pixel_height = workarea_rect.bottom - workarea_rect.top;
-      pixel_width  = workarea_rect.right  - workarea_rect.left;
-      /* Need to send SC_RESTORE to the window, in case we are
-	 resizing from FULLSCREEN_MAXIMIZED.  Otherwise, the mouse
-	 resize hints will not be shown by the window manager when the
-	 mouse pointer hovers over the window edges, because the WM
-	 will still think the window is maximized.  */
-      if (f->want_fullscreen != FULLSCREEN_BOTH)
-	SendMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_RESTORE, 0);
-
+      HWND hwnd = FRAME_W32_WINDOW(f);
+      DWORD dwStyle = GetWindowLong (hwnd, GWL_STYLE);
+      RECT rect;
+
+      block_input();
+      f->want_fullscreen &= ~FULLSCREEN_WAIT;
+
+      if (FRAME_PREV_FSMODE (f) == FULLSCREEN_NONE)
+        GetWindowPlacement (hwnd, &FRAME_NORMAL_PLACEMENT (f));
+
+      if (FRAME_PREV_FSMODE (f) == FULLSCREEN_BOTH)
+        {
+          SetWindowLong (hwnd, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW);
+          SetWindowPos (hwnd, NULL, 0, 0, 0, 0,
+                        SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
+                        SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
+        }
+
+      w32_fullscreen_rect (hwnd, f->want_fullscreen,
+                           FRAME_NORMAL_PLACEMENT (f).rcNormalPosition, &rect);
       FRAME_PREV_FSMODE (f) = f->want_fullscreen;
-      switch (f->want_fullscreen)
-	{
-	case FULLSCREEN_BOTH:
-	  PostMessage (FRAME_W32_WINDOW (f), WM_SYSCOMMAND, SC_MAXIMIZE, 0);
-	  break;
-	case FULLSCREEN_MAXIMIZED:
-	  height =
-	    FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixel_height)
-	    - XINT (Ftool_bar_lines_needed (selected_frame))
-	    + (NILP (Vmenu_bar_mode) ? 1 : 0);
-	  width  =
-	    FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixel_width)
-	    - FRAME_SCROLL_BAR_COLS (f);
-	  left_pos = workarea_rect.left;
-	  top_pos = workarea_rect.top;
-	  break;
-	case FULLSCREEN_WIDTH:
-	  width  =
-	    FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixel_width)
-	    - FRAME_SCROLL_BAR_COLS (f);
-	  height = FRAME_NORMAL_HEIGHT (f);
-	  left_pos = workarea_rect.left;
-	  break;
-	case FULLSCREEN_HEIGHT:
-	  height =
-	    FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixel_height)
-	    - XINT (Ftool_bar_lines_needed (selected_frame))
-	    + (NILP (Vmenu_bar_mode) ? 1 : 0);
-	  width = FRAME_NORMAL_WIDTH (f);
-	  top_pos = workarea_rect.top;
-	  break;
-	case FULLSCREEN_NONE:
-	  height = FRAME_NORMAL_HEIGHT (f);
-	  width = FRAME_NORMAL_WIDTH (f);
-	  left_pos = FRAME_NORMAL_LEFT (f);
-	  top_pos = FRAME_NORMAL_TOP (f);
-	  break;
-	}
+      if (f->want_fullscreen == FULLSCREEN_BOTH)
+        {
+          SetWindowLong (hwnd, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
+          SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
+                        rect.right - rect.left, rect.bottom - rect.top,
+                        SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
+        }
+      else
+        {
+          SetWindowPos (hwnd, HWND_TOP, rect.left, rect.top,
+                        rect.right - rect.left, rect.bottom - rect.top, 0);
+        }
 
-      if (cur_w != width || cur_h != height)
-	{
-	  x_set_offset (f, left_pos, top_pos, 1);
-	  x_set_window_size (f, 1, width, height);
-	  do_pending_window_change (0);
-	}
       f->want_fullscreen = FULLSCREEN_NONE;
       unblock_input ();
     }

=== modified file 'src/w32term.h'
--- src/w32term.h	2013-04-07 04:41:19 +0000
+++ src/w32term.h	2013-04-14 00:58:45 +0000
@@ -71,6 +71,8 @@
 };
 
 extern void w32_regenerate_palette (struct frame *f);
+extern void w32_fullscreen_rect (HWND hwnd, int fsmode, RECT normal,
+                                 RECT *rect);
 
 
 /* For each display (currently only one on w32), we have a structure that
@@ -362,7 +364,7 @@
   /* Frame geometry and full-screen mode before it was resized by
      specifying the 'fullscreen' frame parameter.  Used to restore the
      geometry when 'fullscreen' is reset to nil.  */
-  int normal_width, normal_height, normal_top, normal_left;
+  WINDOWPLACEMENT normal_placement;
   int prev_fsmode;
 };
 
@@ -396,11 +398,8 @@
 #define FRAME_SMALLEST_FONT_HEIGHT(F) \
      FRAME_W32_DISPLAY_INFO(F)->smallest_font_height
 
-#define FRAME_NORMAL_WIDTH(F)  ((F)->output_data.w32->normal_width)
-#define FRAME_NORMAL_HEIGHT(F) ((F)->output_data.w32->normal_height)
-#define FRAME_NORMAL_TOP(F)    ((F)->output_data.w32->normal_top)
-#define FRAME_NORMAL_LEFT(F)   ((F)->output_data.w32->normal_left)
-#define FRAME_PREV_FSMODE(F)   ((F)->output_data.w32->prev_fsmode)
+#define FRAME_NORMAL_PLACEMENT(F) ((F)->output_data.w32->normal_placement)
+#define FRAME_PREV_FSMODE(F)      ((F)->output_data.w32->prev_fsmode)
 
 
 /* W32-specific scroll bar stuff.  */

# Begin bundle