+//
+// Window constraining
+// -------------------
+//
+// To ensure that the windows are not placed under the menu bar, they
+// are typically moved by the call-back constrainFrameRect. However,
+// by overriding it, it's possible to inhibit this, leaving the window
+// in it's original position.
+//
+// It's possible to hide the menu bar. However, technically, it's only
+// possible to hide it when the application is active. To ensure that
+// this work properly, the menu bar and window constraining are
+// deferred until the application becomes active.
+//
+// Even though it's not possible to manually move a window above the
+// top of the screen, it is allowed if it's done programmatically,
+// when the menu is hidden. This allows the editable area to cover the
+// full screen height.
+//
+// Test cases
+// ----------
+//
+// Use the following extra files:
+//
+// init.el:
+// ;; Hide menu and place frame slightly above the top of the screen.
+// (setq ns-auto-hide-menu-bar t)
+// (set-frame-position (selected-frame) 0 -20)
+//
+// Test 1:
+//
+// emacs -Q -l init.el
+//
+// Result: No menu bar, and the title bar should be above the screen.
+//
+// Test 2:
+//
+// emacs -Q
+//
+// Result: Menu bar visible, frame placed immediately below the menu.
+//
+
+static void
+ns_constrain_all_frames (void)
+{
+ Lisp_Object tail, frame;
+
+ FOR_EACH_FRAME (tail, frame)
+ {
+ struct frame *f = XFRAME (frame);
+ if (FRAME_NS_P (f))
+ {
+ NSView *view = FRAME_NS_VIEW (f);
+ /* This no-op will trigger the default window placing
+ * constriant system. */
+ f->output_data.ns->dont_constrain = 0;
+ [[view window] setFrameOrigin:[[view window] frame].origin];
+ }
+ }
+}
+
+
+/* True, if the menu bar should be hidden. */
+
+static BOOL
+ns_menu_bar_should_be_hidden (void)
+{
+ return !NILP (ns_auto_hide_menu_bar)
+ && [NSApp respondsToSelector:@selector(setPresentationOptions:)];
+}
+
+
+/* Show or hide the menu bar, based on user setting. */
+
+static void
+ns_update_auto_hide_menu_bar (void)
+{
+#ifndef MAC_OS_X_VERSION_10_6
+#define MAC_OS_X_VERSION_10_6 1060
+#endif
+#ifdef NS_IMPL_COCOA
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
+ BLOCK_INPUT;
+
+ NSTRACE (ns_update_auto_hide_menu_bar);
+
+ if (NSApp != nil
+ && [NSApp isActive]
+ && [NSApp respondsToSelector:@selector(setPresentationOptions:)])
+ {
+ // Note, "setPresentationOptions" triggers an error unless the
+ // application is active.
+ BOOL menu_bar_should_be_hidden = ns_menu_bar_should_be_hidden ();
+
+ if (menu_bar_should_be_hidden != ns_menu_bar_is_hidden)
+ {
+ NSApplicationPresentationOptions options
+ = NSApplicationPresentationAutoHideDock;
+
+ if (menu_bar_should_be_hidden)
+ options |= NSApplicationPresentationAutoHideMenuBar;
+
+ [NSApp setPresentationOptions: options];
+
+ ns_menu_bar_is_hidden = menu_bar_should_be_hidden;
+
+ if (!ns_menu_bar_is_hidden)
+ {
+ ns_constrain_all_frames ();
+ }
+ }
+ }
+
+ UNBLOCK_INPUT;
+#endif
+#endif
+}
+
+