+static BOOL
+ns_menu_bar_should_be_hidden (void)
+/* True, if the menu bar should be hidden. */
+{
+ return !NILP (ns_auto_hide_menu_bar)
+ && [NSApp respondsToSelector:@selector(setPresentationOptions:)];
+}
+
+
+struct EmacsMargins
+{
+ CGFloat top;
+ CGFloat bottom;
+ CGFloat left;
+ CGFloat right;
+};
+
+
+static struct EmacsMargins
+ns_screen_margins (NSScreen *screen)
+/* The parts of SCREEN used by the operating system. */
+{
+ NSTRACE ("ns_screen_margins");
+
+ struct EmacsMargins margins;
+
+ NSRect screenFrame = [screen frame];
+ NSRect screenVisibleFrame = [screen visibleFrame];
+
+ /* Sometimes, visibleFrame isn't up-to-date with respect to a hidden
+ menu bar, check this explicitly. */
+ if (ns_menu_bar_should_be_hidden())
+ {
+ margins.top = 0;
+ }
+ else
+ {
+ CGFloat frameTop = screenFrame.origin.y + screenFrame.size.height;
+ CGFloat visibleFrameTop = (screenVisibleFrame.origin.y
+ + screenVisibleFrame.size.height);
+
+ margins.top = frameTop - visibleFrameTop;
+ }
+
+ {
+ CGFloat frameRight = screenFrame.origin.x + screenFrame.size.width;
+ CGFloat visibleFrameRight = (screenVisibleFrame.origin.x
+ + screenVisibleFrame.size.width);
+ margins.right = frameRight - visibleFrameRight;
+ }
+
+ margins.bottom = screenVisibleFrame.origin.y - screenFrame.origin.y;
+ margins.left = screenVisibleFrame.origin.x - screenFrame.origin.x;
+
+ NSTRACE_MSG ("left:%g right:%g top:%g bottom:%g",
+ margins.left,
+ margins.right,
+ margins.top,
+ margins.bottom);
+
+ return margins;
+}
+
+
+/* A screen margin between 1 and DOCK_IGNORE_LIMIT (inclusive) is
+ assumed to contain a hidden dock. OS X currently use 4 pixels for
+ this, however, to be future compatible, a larger value is used. */
+#define DOCK_IGNORE_LIMIT 6
+
+static struct EmacsMargins
+ns_screen_margins_ignoring_hidden_dock (NSScreen *screen)
+/* The parts of SCREEN used by the operating system, excluding the parts
+reserved for an hidden dock. */
+{
+ NSTRACE ("ns_screen_margins_ignoring_hidden_dock");
+
+ struct EmacsMargins margins = ns_screen_margins(screen);
+
+ /* OS X (currently) reserved 4 pixels along the edge where a hidden
+ dock is located. Unfortunately, it's not possible to find the
+ location and information about if the dock is hidden. Instead,
+ it is assumed that if the margin of an edge is less than
+ DOCK_IGNORE_LIMIT, it contains a hidden dock. */
+ if (margins.left <= DOCK_IGNORE_LIMIT)
+ {
+ margins.left = 0;
+ }
+ if (margins.right <= DOCK_IGNORE_LIMIT)
+ {
+ margins.right = 0;
+ }
+ if (margins.top <= DOCK_IGNORE_LIMIT)
+ {
+ margins.top = 0;
+ }
+ /* Note: This doesn't occur in current versions of OS X, but
+ included for completeness and future compatibility. */
+ if (margins.bottom <= DOCK_IGNORE_LIMIT)
+ {
+ margins.bottom = 0;
+ }
+
+ NSTRACE_MSG ("left:%g right:%g top:%g bottom:%g",
+ margins.left,
+ margins.right,
+ margins.top,
+ margins.bottom);
+
+ return margins;
+}
+
+
+static CGFloat
+ns_menu_bar_height (NSScreen *screen)
+/* The height of the menu bar, if visible.
+
+ Note: Don't use this when fullscreen is enabled -- the screen
+ sometimes includes, sometimes excludes the menu bar area. */
+{
+ struct EmacsMargins margins = ns_screen_margins(screen);
+
+ CGFloat res = margins.top;
+
+ NSTRACE ("ns_menu_bar_height " NSTRACE_FMT_RETURN " %.0f", res);
+
+ return res;
+}
+