frame.c 193 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Generic frame functions.
Glenn Morris's avatar
Glenn Morris committed
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 1993-1995, 1997, 1999-2018 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
8
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
11 12 13 14 15 16 17

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19

20
#include <config.h>
21 22

#include <stdio.h>
Paul Eggert's avatar
Paul Eggert committed
23
#include <stdlib.h>
24 25
#include <errno.h>
#include <limits.h>
26 27 28

#include <c-ctype.h>

29
#include "lisp.h"
30 31 32 33 34

#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */

35 36 37
#include "buffer.h"
/* These help us bind and responding to switch-frame events.  */
#include "keyboard.h"
38
#include "ptr-bounds.h"
39
#include "frame.h"
Kim F. Storm's avatar
Kim F. Storm committed
40
#include "blockinput.h"
41
#include "termchar.h"
42
#include "termhooks.h"
Andreas Schwab's avatar
Andreas Schwab committed
43
#include "dispextern.h"
44
#include "window.h"
Andreas Schwab's avatar
Andreas Schwab committed
45
#ifdef HAVE_WINDOW_SYSTEM
46 47
#include "fontset.h"
#endif
48
#include "cm.h"
Morten Welinder's avatar
Morten Welinder committed
49 50
#ifdef MSDOS
#include "msdos.h"
51
#include "dosfns.h"
Morten Welinder's avatar
Morten Welinder committed
52
#endif
Paul Eggert's avatar
Paul Eggert committed
53 54 55
#ifdef USE_X_TOOLKIT
#include "widget.h"
#endif
Jim Blandy's avatar
Jim Blandy committed
56

57 58 59 60 61 62 63 64 65
/* The currently selected frame.  */

Lisp_Object selected_frame;

/* A frame which is not just a mini-buffer, or NULL if there are no such
   frames.  This is usually the most recent such frame that was selected.  */

static struct frame *last_nonminibuf_frame;

66
/* False means there are no visible garbaged frames.  */
67 68
bool frame_garbaged;

69
/* The default tool bar height for future frames.  */
70 71 72
#if defined USE_GTK || defined HAVE_NS
enum { frame_default_tool_bar_height = 0 };
#else
73
int frame_default_tool_bar_height;
74
#endif
75

76 77 78 79
#ifdef HAVE_WINDOW_SYSTEM
static void x_report_frame_params (struct frame *, Lisp_Object *);
#endif

80
/* These setters are used only in this file, so they can be private.  */
81
static void
82 83 84 85
fset_buffer_predicate (struct frame *f, Lisp_Object val)
{
  f->buffer_predicate = val;
}
86
static void
87 88 89 90 91
fset_minibuffer_window (struct frame *f, Lisp_Object val)
{
  f->minibuffer_window = val;
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
struct frame *
decode_live_frame (register Lisp_Object frame)
{
  if (NILP (frame))
    frame = selected_frame;
  CHECK_LIVE_FRAME (frame);
  return XFRAME (frame);
}

struct frame *
decode_any_frame (register Lisp_Object frame)
{
  if (NILP (frame))
    frame = selected_frame;
  CHECK_FRAME (frame);
  return XFRAME (frame);
}

110
#ifdef HAVE_WINDOW_SYSTEM
111
bool
112
display_available (void)
113
{
114
  return x_display_list != NULL;
115
}
116
#endif
117 118 119 120 121

struct frame *
decode_window_system_frame (Lisp_Object frame)
{
  struct frame *f = decode_live_frame (frame);
122 123
  check_window_system (f);
#ifdef HAVE_WINDOW_SYSTEM
124
  return f;
125
#endif
126 127 128 129 130
}

void
check_window_system (struct frame *f)
{
131 132 133 134 135 136
#ifdef HAVE_WINDOW_SYSTEM
  if (window_system_available (f))
    return;
#endif
  error (f ? "Window system frame should be used"
	 : "Window system is not in use or not initialized");
137 138
}

139
/* Return the value of frame parameter PROP in frame FRAME.  */
140

141
Lisp_Object
Paul Eggert's avatar
Paul Eggert committed
142
get_frame_param (struct frame *frame, Lisp_Object prop)
143
{
Paul Eggert's avatar
Paul Eggert committed
144
  return Fcdr (Fassq (prop, frame->param_alist));
145 146
}

147 148 149 150 151 152 153 154 155

void
frame_size_history_add (struct frame *f, Lisp_Object fun_symbol,
			int width, int height, Lisp_Object rest)
{
  Lisp_Object frame;

  XSETFRAME (frame, f);
  if (CONSP (frame_size_history)
156
      && FIXNUMP (XCAR (frame_size_history))
Tom Tromey's avatar
Tom Tromey committed
157
      && 0 < XFIXNUM (XCAR (frame_size_history)))
158
    frame_size_history =
Tom Tromey's avatar
Tom Tromey committed
159
      Fcons (make_fixnum (XFIXNUM (XCAR (frame_size_history)) - 1),
160 161 162
	     Fcons (list4
		    (frame, fun_symbol,
		     ((width > 0)
163 164 165 166
		      ? list4 (make_fixnum (FRAME_TEXT_WIDTH (f)),
			       make_fixnum (FRAME_TEXT_HEIGHT (f)),
			       make_fixnum (width),
			       make_fixnum (height))
167 168
		      : Qnil),
		     rest),
169
		    XCDR (frame_size_history)));
170 171 172
}


173 174
/* Return 1 if `frame-inhibit-implied-resize' is non-nil or fullscreen
   state of frame F would be affected by a vertical (horizontal if
175 176
   HORIZONTAL is true) resize.  PARAMETER is the symbol of the frame
   parameter that is changed.  */
177
bool
178
frame_inhibit_resize (struct frame *f, bool horizontal, Lisp_Object parameter)
179
{
180 181
  Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
  bool inhibit
182 183 184 185 186
    = (f->after_make_frame
       ? (EQ (frame_inhibit_implied_resize, Qt)
	  || (CONSP (frame_inhibit_implied_resize)
	      && !NILP (Fmemq (parameter, frame_inhibit_implied_resize)))
	  || (horizontal
Paul Eggert's avatar
Paul Eggert committed
187
	      && !NILP (fullscreen) && !EQ (fullscreen, Qfullheight))
188
	  || (!horizontal
Paul Eggert's avatar
Paul Eggert committed
189
	      && !NILP (fullscreen) && !EQ (fullscreen, Qfullwidth))
190 191 192
	  || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
       : ((horizontal && f->inhibit_horizontal_resize)
	  || (!horizontal && f->inhibit_vertical_resize)));
193 194 195 196 197 198 199 200 201
  if (inhibit && !FRAME_TERMCAP_P (f) && !FRAME_MSDOS_P (f))
    frame_size_history_add
      (f, Qframe_inhibit_resize, 0, 0,
       list5 (horizontal ? Qt : Qnil, parameter,
	      f->after_make_frame ? Qt : Qnil,
	      frame_inhibit_implied_resize,
	      fullscreen));

  return inhibit;
202 203
}

204
static void
205
set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
206 207 208 209 210 211
{
  int nlines;
  int olines = FRAME_MENU_BAR_LINES (f);

  /* Right now, menu bars don't work properly in minibuf-only frames;
     most of the commands try to apply themselves to the minibuffer
Karl Heuer's avatar
Karl Heuer committed
212
     frame itself, and get an error because you can't switch buffers
213 214 215 216
     in or split the minibuffer window.  */
  if (FRAME_MINIBUF_ONLY_P (f))
    return;

217
  if (TYPE_RANGED_FIXNUMP (int, value))
Tom Tromey's avatar
Tom Tromey committed
218
    nlines = XFIXNUM (value);
219 220 221
  else
    nlines = 0;

222 223
  if (nlines != olines)
    {
224
      windows_or_buffers_changed = 14;
225
      FRAME_MENU_BAR_LINES (f) = nlines;
226
      FRAME_MENU_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
227 228 229
      change_frame_size (f, FRAME_COLS (f),
			 FRAME_LINES (f) + olines - nlines,
			 0, 1, 0, 0);
230
    }
231 232 233 234
}

Lisp_Object Vframe_list;

Jim Blandy's avatar
Jim Blandy committed
235

Jim Blandy's avatar
Jim Blandy committed
236
DEFUN ("framep", Fframep, Sframep, 1, 1, 0,
237
       doc: /* Return non-nil if OBJECT is a frame.
238 239
Value is:
  t for a termcap frame (a character-only terminal),
240 241 242 243
 `x' for an Emacs frame that is really an X window,
 `w32' for an Emacs frame that is a window on MS-Windows display,
 `ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
 `pc' for a direct-write MS-DOS frame.
244
See also `frame-live-p'.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
245
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
246
{
247
  if (!FRAMEP (object))
Jim Blandy's avatar
Jim Blandy committed
248
    return Qnil;
Jim Blandy's avatar
Jim Blandy committed
249
  switch (XFRAME (object)->output_method)
Jim Blandy's avatar
Jim Blandy committed
250
    {
251
    case output_initial: /* The initial frame is like a termcap frame. */
Jim Blandy's avatar
Jim Blandy committed
252 253 254
    case output_termcap:
      return Qt;
    case output_x_window:
255
      return Qx;
256 257
    case output_w32:
      return Qw32;
258 259
    case output_msdos_raw:
      return Qpc;
260 261
    case output_ns:
      return Qns;
Jim Blandy's avatar
Jim Blandy committed
262
    default:
263
      emacs_abort ();
Jim Blandy's avatar
Jim Blandy committed
264 265 266
    }
}

Jim Blandy's avatar
Jim Blandy committed
267
DEFUN ("frame-live-p", Fframe_live_p, Sframe_live_p, 1, 1, 0,
268 269
       doc: /* Return non-nil if OBJECT is a frame which has not been deleted.
Value is nil if OBJECT is not a live frame.  If object is a live
270
frame, the return value indicates what sort of terminal device it is
Pavel Janík's avatar
Pavel Janík committed
271
displayed on.  See the documentation of `framep' for possible
Pavel Janík's avatar
Pavel Janík committed
272
return values.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
273
  (Lisp_Object object)
Jim Blandy's avatar
Jim Blandy committed
274
{
Jim Blandy's avatar
Jim Blandy committed
275 276 277
  return ((FRAMEP (object)
	   && FRAME_LIVE_P (XFRAME (object)))
	  ? Fframep (object)
Jim Blandy's avatar
Jim Blandy committed
278 279 280
	  : Qnil);
}

Paul Eggert's avatar
Paul Eggert committed
281
DEFUN ("window-system", Fwindow_system, Swindow_system, 0, 1, 0,
282
       doc: /* The name of the window system that FRAME is displaying through.
283 284
The value is a symbol:
 nil for a termcap frame (a character-only terminal),
285 286 287 288
 `x' for an Emacs frame that is really an X window,
 `w32' for an Emacs frame that is a window on MS-Windows display,
 `ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
 `pc' for a direct-write MS-DOS frame.
289 290 291 292 293 294

FRAME defaults to the currently selected frame.

Use of this function as a predicate is deprecated.  Instead,
use `display-graphic-p' or any of the other `display-*-p'
predicates which report frame's specific UI-related capabilities.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
295
  (Lisp_Object frame)
296 297 298 299 300 301 302 303 304 305 306 307 308 309
{
  Lisp_Object type;
  if (NILP (frame))
    frame = selected_frame;

  type = Fframep (frame);

  if (NILP (type))
    wrong_type_argument (Qframep, frame);

  if (EQ (type, Qt))
    return Qnil;
  else
    return type;
310
}
311

312 313 314
/* Placeholder used by temacs -nw before window.el is loaded.  */
DEFUN ("frame-windows-min-size", Fframe_windows_min_size,
       Sframe_windows_min_size, 4, 4, 0,
315
       doc: /* SKIP: real doc in window.el.  */
316
       attributes: const)
317 318 319
     (Lisp_Object frame, Lisp_Object horizontal,
      Lisp_Object ignore, Lisp_Object pixelwise)
{
320
  return make_fixnum (0);
321 322
}

323 324 325 326
/**
 * frame_windows_min_size:
 *
 * Return the minimum number of lines (columns if HORIZONTAL is non-nil)
327 328
 * of FRAME.  If PIXELWISE is non-nil, return the minimum inner height
 * (width) of FRAME in pixels.
329 330 331 332 333 334 335
 *
 * This value is calculated by the function `frame-windows-min-size' in
 * window.el unless the `min-height' (`min-width' if HORIZONTAL is
 * non-nil) parameter of FRAME is non-nil thus explicitly specifying the
 * value to be returned.  In that latter case IGNORE is ignored.
 *
 * If `frame-windows-min-size' is called, it will make sure that the
336
 * return value accommodates all windows of FRAME respecting the values
337 338 339
 * of `window-min-height' (`window-min-width' if HORIZONTAL is non-nil).
 * With IGNORE non-nil the values of these variables are ignored.
 *
340 341 342
 * In either case, never return a value less than 1.  For TTY frames,
 * additionally limit the minimum frame height to a value large enough
 * to support the menu bar, the mode line, and the echo area.
343
 */
344
static int
345 346
frame_windows_min_size (Lisp_Object frame, Lisp_Object horizontal,
			Lisp_Object ignore, Lisp_Object pixelwise)
347
{
348 349
  struct frame *f = XFRAME (frame);
  Lisp_Object par_size;
350
  int retval;
351 352

  if ((!NILP (horizontal)
353
       && FIXED_OR_FLOATP (par_size = get_frame_param (f, Qmin_width)))
354
      || (NILP (horizontal)
355
	  && FIXED_OR_FLOATP (par_size = get_frame_param (f, Qmin_height))))
356
    {
Tom Tromey's avatar
Tom Tromey committed
357
      int min_size = XFIXNUM (par_size);
358 359 360 361 362

      /* Don't allow phantom frames.  */
      if (min_size < 1)
	min_size = 1;

363 364 365 366 367
      retval = (NILP (pixelwise)
		? min_size
		: min_size * (NILP (horizontal)
			      ? FRAME_LINE_HEIGHT (f)
			      : FRAME_COLUMN_WIDTH (f)));
368 369
    }
  else
Tom Tromey's avatar
Tom Tromey committed
370
    retval = XFIXNUM (call4 (Qframe_windows_min_size, frame, horizontal,
371 372 373 374 375 376 377 378 379 380 381 382 383
			  ignore, pixelwise));
  /* Don't allow too small height of text-mode frames, or else cm.c
     might abort in cmcheckmagic.  */
  if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f)) && NILP (horizontal))
    {
      int min_height = (FRAME_MENU_BAR_LINES (f)
			+ FRAME_WANTS_MODELINE_P (f)
			+ 2);	/* one text line and one echo-area line */
      if (retval < min_height)
	retval = min_height;
    }

  return retval;
384 385 386
}


387 388 389 390 391
#ifdef HAVE_WINDOW_SYSTEM
/**
 * keep_ratio:
 *
 * Preserve ratios of frame F which usually happens after its parent
392
 * frame P got resized.  OLD_WIDTH, OLD_HEIGHT specifies the old native
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
 * size of F's parent, NEW_WIDTH and NEW_HEIGHT its new size.
 *
 * Adjust F's width if F's 'keep_ratio' parameter is non-nil and, if
 * it is a cons, its car is not 'height-only'.  Adjust F's height if F's
 * 'keep_ratio' parameter is non-nil and, if it is a cons, its car
 * is not 'width-only'.
 *
 * Adjust F's left position if F's 'keep_ratio' parameter is non-nil
 * and, if its is a cons, its cdr is non-nil and not 'top-only'.  Adjust
 * F's top position if F's 'keep_ratio' parameter is non-nil and, if
 * its is a cons, its cdr is non-nil and not 'left-only'.
 *
 * Note that when positional adjustment is requested but the size of F
 * should remain unaltered in the corresponding direction, this routine
 * tries to constrain F to its parent frame - something which usually
 * happens when the parent frame shrinks.  This means, however, that
 * when the parent frame is re-enlarged later, the child's original
 * position will not get restored to its pre-shrinking value.
 *
 * This routine is currently useful for child frames only.  It might be
 * eventually useful when moving non-child frames between monitors with
 * different resolutions.
 */
static void
417 418
keep_ratio (struct frame *f, struct frame *p, int old_width, int old_height,
	    int new_width, int new_height)
419 420
{
  Lisp_Object keep_ratio = get_frame_param (f, Qkeep_ratio);
421 422


423 424 425 426 427
  if (!NILP (keep_ratio))
    {
      double width_factor = (double)new_width / (double)old_width;
      double height_factor = (double)new_height / (double)old_height;
      int pixel_width, pixel_height, pos_x, pos_y;
428

429 430 431 432 433 434 435
      if (!CONSP (keep_ratio) || !NILP (Fcdr (keep_ratio)))
	{
	  if (CONSP (keep_ratio) && EQ (Fcdr (keep_ratio), Qtop_only))
	    pos_x = f->left_pos;
	  else
	    {
	      pos_x = (int)(f->left_pos * width_factor + 0.5);
436

437 438 439 440
	      if (CONSP (keep_ratio)
		  && (NILP (Fcar (keep_ratio))
		      || EQ (Fcar (keep_ratio), Qheight_only))
		  && p->pixel_width - f->pixel_width < pos_x)
441
		{
442
		  int p_f_width = p->pixel_width - f->pixel_width;
443

444 445 446 447
		  if (p_f_width <= 0)
		    pos_x = 0;
		  else
		    pos_x = (int)(p_f_width * width_factor * 0.5 + 0.5);
448 449 450 451 452 453 454 455 456 457 458
		}

	      f->left_pos = pos_x;
	    }

	  if (CONSP (keep_ratio) && EQ (Fcdr (keep_ratio), Qleft_only))
	    pos_y = f->top_pos;
	  else
	    {
	      pos_y = (int)(f->top_pos * height_factor + 0.5);

459 460 461 462
	      if (CONSP (keep_ratio)
		  && (NILP (Fcar (keep_ratio))
		      || EQ (Fcar (keep_ratio), Qwidth_only))
		  && p->pixel_height - f->pixel_height < pos_y)
463 464 465 466 467 468
		/* When positional adjustment was requested and the
		   width of F should remain unaltered, try to constrain
		   F to its parent.  This means that when the parent
		   frame is enlarged later the child's original position
		   won't get restored.  */
		{
469
		  int p_f_height = p->pixel_height - f->pixel_height;
470

471 472 473 474
		  if (p_f_height <= 0)
		    pos_y = 0;
		  else
		    pos_y = (int)(p_f_height * height_factor * 0.5 + 0.5);
475
		}
476

477 478
	      f->top_pos = pos_y;
	    }
479

480 481
	  x_set_offset (f, pos_x, pos_y, -1);
	}
482

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
      if (!CONSP (keep_ratio) || !NILP (Fcar (keep_ratio)))
	{
	  if (CONSP (keep_ratio) && EQ (Fcar (keep_ratio), Qheight_only))
	    pixel_width = -1;
	  else
	    {
	      pixel_width = (int)(f->pixel_width * width_factor + 0.5);
	      pixel_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, pixel_width);
	    }

	  if (CONSP (keep_ratio) && EQ (Fcar (keep_ratio), Qwidth_only))
	    pixel_height = -1;
	  else
	    {
	      pixel_height = (int)(f->pixel_height * height_factor + 0.5);
	      pixel_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixel_height);
	    }

	  adjust_frame_size (f, pixel_width, pixel_height, 1, 0,
			     Qkeep_ratio);
	}
    }
}
#endif


/**
 * adjust_frame_size:
 *
 * Adjust size of frame F.  NEW_WIDTH and NEW_HEIGHT specify the new
 * text size of F in pixels.  A value of -1 means no change is requested
 * for that direction (but the frame may still have to be resized to
 * accommodate windows with their minimum sizes).  This can either issue
 * a request to resize the frame externally (via x_set_window_size), to
 * resize the frame internally (via resize_frame_windows) or do nothing
 * at all.
 *
 * The argument INHIBIT can assume the following values:
 *
 * 0 means to unconditionally call x_set_window_size even if sizes
 *   apparently do not change.  Fx_create_frame uses this to pass the
 *   initial size to the window manager.
 *
 * 1 means to call x_set_window_size if the native frame size really
 *   changes.  Fset_frame_size, Fset_frame_height, ... use this.
 *
 * 2 means to call x_set_window_size provided frame_inhibit_resize
 *   allows it.  The menu and tool bar code use this ("3" won't work
 *   here in general because menu and tool bar are often not counted in
 *   the frame's text height).
 *
 * 3 means call x_set_window_size if window minimum sizes must be
 *   preserved or frame_inhibit_resize allows it.  x_set_left_fringe,
 *   x_set_scroll_bar_width, x_new_font ... use (or should use) this.
 *
 * 4 means call x_set_window_size only if window minimum sizes must be
 *   preserved.  x_set_right_divider_width, x_set_border_width and the
 *   code responsible for wrapping the tool bar use this.
 *
 * 5 means to never call x_set_window_size.  change_frame_size uses
 *   this.
 *
 * Note that even when x_set_window_size is not called, individual
 * windows may have to be resized (via `window--sanitize-window-sizes')
 * in order to support minimum size constraints.
 *
 * PRETEND is as for change_frame_size.  PARAMETER, if non-nil, is the
 * symbol of the parameter changed (like `menu-bar-lines', `font', ...).
 * This is passed on to frame_inhibit_resize to let the latter decide on
 * a case-by-case basis whether the frame may be resized externally.
 */
554
void
555 556
adjust_frame_size (struct frame *f, int new_width, int new_height, int inhibit,
		   bool pretend, Lisp_Object parameter)
557 558 559 560 561
{
  int unit_width = FRAME_COLUMN_WIDTH (f);
  int unit_height = FRAME_LINE_HEIGHT (f);
  int old_pixel_width = FRAME_PIXEL_WIDTH (f);
  int old_pixel_height = FRAME_PIXEL_HEIGHT (f);
562 563
  int old_cols = FRAME_COLS (f);
  int old_lines = FRAME_LINES (f);
564 565 566 567 568 569 570 571 572 573 574 575 576
  int new_pixel_width, new_pixel_height;
  /* The following two values are calculated from the old frame pixel
     sizes and any "new" settings for tool bar, menu bar and internal
     borders.  We do it this way to detect whether we have to call
     x_set_window_size as consequence of the new settings.  */
  int windows_width = FRAME_WINDOWS_WIDTH (f);
  int windows_height = FRAME_WINDOWS_HEIGHT (f);
  int min_windows_width, min_windows_height;
  /* These are a bit tedious, maybe we should use a macro.  */
  struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
  int old_windows_width = WINDOW_PIXEL_WIDTH (r);
  int old_windows_height
    = (WINDOW_PIXEL_HEIGHT (r)
577
       + ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
578 579 580 581 582 583 584 585 586 587 588 589 590
	  ? WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_MINIBUF_WINDOW (f)))
	  : 0));
  int new_windows_width, new_windows_height;
  int old_text_width = FRAME_TEXT_WIDTH (f);
  int old_text_height = FRAME_TEXT_HEIGHT (f);
  /* If a size is < 0 use the old value.  */
  int new_text_width = (new_width >= 0) ? new_width : old_text_width;
  int new_text_height = (new_height >= 0) ? new_height : old_text_height;
  int new_cols, new_lines;
  bool inhibit_horizontal, inhibit_vertical;
  Lisp_Object frame;

  XSETFRAME (frame, f);
591

592 593
  frame_size_history_add
    (f, Qadjust_frame_size_1, new_text_width, new_text_height,
594
     list2 (parameter, make_fixnum (inhibit)));
595

596 597 598
  /* The following two values are calculated from the old window body
     sizes and any "new" settings for scroll bars, dividers, fringes and
     margins (though the latter should have been processed already).  */
599 600 601 602
  min_windows_width
    = frame_windows_min_size (frame, Qt, (inhibit == 5) ? Qt : Qnil, Qt);
  min_windows_height
    = frame_windows_min_size (frame, Qnil, (inhibit == 5) ? Qt : Qnil, Qt);
603 604

  if (inhibit >= 2 && inhibit <= 4)
605
    /* When INHIBIT is in [2..4] inhibit if the "old" window sizes stay
606 607
       within the limits and either resizing is inhibited or INHIBIT
       equals 4.  */
608
    {
609 610 611 612 613 614
      inhibit_horizontal = (windows_width >= min_windows_width
                            && (inhibit == 4
                                || frame_inhibit_resize (f, true, parameter)));
      inhibit_vertical = (windows_height >= min_windows_height
                          && (inhibit == 4
                              || frame_inhibit_resize (f, false, parameter)));
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    }
  else
    /* Otherwise inhibit if INHIBIT equals 5.  */
    inhibit_horizontal = inhibit_vertical = inhibit == 5;

  new_pixel_width = ((inhibit_horizontal && (inhibit < 5))
		     ? old_pixel_width
		     : max (FRAME_TEXT_TO_PIXEL_WIDTH (f, new_text_width),
			    min_windows_width
			    + 2 * FRAME_INTERNAL_BORDER_WIDTH (f)));
  new_windows_width = new_pixel_width - 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
  new_text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, new_pixel_width);
  new_cols = new_text_width / unit_width;

  new_pixel_height = ((inhibit_vertical && (inhibit < 5))
		      ? old_pixel_height
		      : max (FRAME_TEXT_TO_PIXEL_HEIGHT (f, new_text_height),
			     min_windows_height
			     + FRAME_TOP_MARGIN_HEIGHT (f)
			     + 2 * FRAME_INTERNAL_BORDER_WIDTH (f)));
  new_windows_height = (new_pixel_height
			- FRAME_TOP_MARGIN_HEIGHT (f)
			- 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
  new_text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, new_pixel_height);
  new_lines = new_text_height / unit_height;

#ifdef HAVE_WINDOW_SYSTEM
642
  if (FRAME_WINDOW_P (f)
643
      && f->can_x_set_window_size
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
      && ((!inhibit_horizontal
	   && (new_pixel_width != old_pixel_width
	       || inhibit == 0 || inhibit == 2))
	  || (!inhibit_vertical
	      && (new_pixel_height != old_pixel_height
		  || inhibit == 0 || inhibit == 2))))
    /* We are either allowed to change the frame size or the minimum
       sizes request such a change.  Do not care for fixing minimum
       sizes here, we do that eventually when we're called from
       change_frame_size.  */
    {
      /* Make sure we respect fullheight and fullwidth.  */
      if (inhibit_horizontal)
	new_text_width = old_text_width;
      else if (inhibit_vertical)
	new_text_height = old_text_height;

661 662 663 664
      frame_size_history_add
	(f, Qadjust_frame_size_2, new_text_width, new_text_height,
	 list2 (inhibit_horizontal ? Qt : Qnil,
		inhibit_vertical ? Qt : Qnil));
665

666 667 668 669 670 671 672 673 674 675 676 677
      x_set_window_size (f, 0, new_text_width, new_text_height, 1);
      f->resized_p = true;

      return;
    }
#endif

  if (new_text_width == old_text_width
      && new_text_height == old_text_height
      && new_windows_width == old_windows_width
      && new_windows_height == old_windows_height
      && new_pixel_width == old_pixel_width
678 679 680
      && new_pixel_height == old_pixel_height
      && new_cols == old_cols
      && new_lines == old_lines)
681 682
    /* No change.  Sanitize window sizes and return.  */
    {
683 684
      sanitize_window_sizes (Qt);
      sanitize_window_sizes (Qnil);
685 686 687 688 689 690 691

      return;
    }

  block_input ();

#ifdef MSDOS
Eli Zaretskii's avatar
Eli Zaretskii committed
692 693 694 695 696 697 698
  /* We only can set screen dimensions to certain values supported by
     our video hardware.  Try to find the smallest size greater or
     equal to the requested dimensions, while accounting for the fact
     that the menu-bar lines are not counted in the frame height.  */
  int dos_new_lines = new_lines + FRAME_TOP_MARGIN (f);
  dos_set_window_size (&dos_new_lines, &new_cols);
  new_lines = dos_new_lines - FRAME_TOP_MARGIN (f);
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
#endif

  if (new_windows_width != old_windows_width)
    {
      resize_frame_windows (f, new_windows_width, 1, 1);

      /* MSDOS frames cannot PRETEND, as they change frame size by
	 manipulating video hardware.  */
      if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
	FrameCols (FRAME_TTY (f)) = new_cols;

#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
      if (WINDOWP (f->tool_bar_window))
	{
	  XWINDOW (f->tool_bar_window)->pixel_width = new_windows_width;
	  XWINDOW (f->tool_bar_window)->total_cols
	    = new_windows_width / unit_width;
	}
#endif
    }
719
  else if (new_cols != old_cols)
720
    call2 (Qwindow__pixel_to_total, frame, Qt);
721 722 723 724 725 726 727 728 729 730 731 732

  if (new_windows_height != old_windows_height
      /* When the top margin has changed we have to recalculate the top
	 edges of all windows.  No such calculation is necessary for the
	 left edges.  */
      || WINDOW_TOP_PIXEL_EDGE (r) != FRAME_TOP_MARGIN_HEIGHT (f))
    {
      resize_frame_windows (f, new_windows_height, 0, 1);

      /* MSDOS frames cannot PRETEND, as they change frame size by
	 manipulating video hardware.  */
      if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
733
	FrameRows (FRAME_TTY (f)) = new_lines + FRAME_TOP_MARGIN (f);
734
    }
735
  else if (new_lines != old_lines)
736
    call2 (Qwindow__pixel_to_total, frame, Qnil);
737

738 739
  frame_size_history_add
    (f, Qadjust_frame_size_3, new_text_width, new_text_height,
740 741
     list4 (make_fixnum (old_pixel_width), make_fixnum (old_pixel_height),
	    make_fixnum (new_pixel_width), make_fixnum (new_pixel_height)));
742

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
  /* Assign new sizes.  */
  FRAME_TEXT_WIDTH (f) = new_text_width;
  FRAME_TEXT_HEIGHT (f) = new_text_height;
  FRAME_PIXEL_WIDTH (f) = new_pixel_width;
  FRAME_PIXEL_HEIGHT (f) = new_pixel_height;
  SET_FRAME_COLS (f, new_cols);
  SET_FRAME_LINES (f, new_lines);

  {
    struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
    int text_area_x, text_area_y, text_area_width, text_area_height;

    window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
		&text_area_height);
    if (w->cursor.x >= text_area_x + text_area_width)
      w->cursor.hpos = w->cursor.x = 0;
    if (w->cursor.y >= text_area_y + text_area_height)
      w->cursor.vpos = w->cursor.y = 0;
  }

  /* Sanitize window sizes.  */
764 765
  sanitize_window_sizes (Qt);
  sanitize_window_sizes (Qnil);
766 767 768 769 770 771 772 773 774 775 776

  adjust_frame_glyphs (f);
  calculate_costs (f);
  SET_FRAME_GARBAGED (f);

  /* A frame was "resized" if one of its pixelsizes changed, even if its
     X window wasn't resized at all.  */
  f->resized_p = (new_pixel_width != old_pixel_width
		  || new_pixel_height != old_pixel_height);

  unblock_input ();
777 778 779 780 781 782 783 784

#ifdef HAVE_WINDOW_SYSTEM
  {
    /* Adjust size of F's child frames.  */
    Lisp_Object frames, frame1;

    FOR_EACH_FRAME (frames, frame1)
      if (FRAME_PARENT_FRAME (XFRAME (frame1)) == f)
785 786
	keep_ratio (XFRAME (frame1), f, old_pixel_width, old_pixel_height,
		    new_pixel_width, new_pixel_height);
787 788
  }
#endif
789 790
}

791 792 793 794 795 796 797
/* Allocate basically initialized frame.  */

static struct frame *
allocate_frame (void)
{
  return ALLOCATE_ZEROED_PSEUDOVECTOR (struct frame, face_cache, PVEC_FRAME);
}
798

Jim Blandy's avatar
Jim Blandy committed
799
struct frame *
800
make_frame (bool mini_p)
Jim Blandy's avatar
Jim Blandy committed
801
{
Jim Blandy's avatar
Jim Blandy committed
802
  Lisp_Object frame;
803
  struct frame *f;
Paul Eggert's avatar
Paul Eggert committed
804
  struct window *rw, *mw UNINIT;
805 806
  Lisp_Object root_window;
  Lisp_Object mini_window;
807 808

  f = allocate_frame ();
809
  XSETFRAME (frame, f);
Jim Blandy's avatar
Jim Blandy committed
810

811
#ifdef USE_GTK
812 813
  /* Initialize Lisp data.  Note that allocate_frame initializes all
     Lisp data to nil, so do it only for slots which should not be nil.  */
814
  fset_tool_bar_position (f, Qtop);
815
#endif
816 817 818 819 820

  /* Initialize non-Lisp data.  Note that allocate_frame zeroes out all
     non-Lisp data, so do it only for slots which should not be zero.
     To avoid subtle bugs and for the sake of readability, it's better to
     initialize enum members explicitly even if their values are zero.  */
821 822 823
  f->wants_modeline = true;
  f->redisplay = true;
  f->garbaged = true;
824
  f->can_x_set_window_size = false;
825
  f->after_make_frame = false;
826 827 828 829
  f->inhibit_horizontal_resize = false;
  f->inhibit_vertical_resize = false;
  f->tool_bar_redisplayed = false;
  f->tool_bar_resized = false;
830 831
  f->column_width = 1;  /* !FRAME_WINDOW_P value.  */
  f->line_height = 1;  /* !FRAME_WINDOW_P value.  */
832
#ifdef HAVE_WINDOW_SYSTEM
833
  f->vertical_scroll_bar_type = vertical_scroll_bar_none;
834
  f->horizontal_scroll_bars = false;
835
  f->want_fullscreen = FULLSCREEN_NONE;
836
  f->undecorated = false;
837
  f->no_special_glyphs = false;
838 839 840 841 842 843 844
#ifndef HAVE_NTGUI
  f->override_redirect = false;
#endif
  f->skip_taskbar = false;
  f->no_focus_on_map = false;
  f->no_accept_focus = false;
  f->z_group = z_group_none;
845
  f->tooltip = false;
846 847 848
#if ! defined (USE_GTK) && ! defined (HAVE_NS)
  f->last_tool_bar_item = -1;
#endif
849 850 851 852
#ifdef NS_IMPL_COCOA
  f->ns_appearance = ns_appearance_aqua;
  f->ns_transparent_titlebar = false;
#endif
853
#endif
Jim Blandy's avatar
Jim Blandy committed
854

855
  root_window = make_window ();
856
  rw = XWINDOW (root_window);
Jim Blandy's avatar
Jim Blandy committed
857 858
  if (mini_p)
    {
859
      mini_window = make_window ();
860 861 862 863 864
      mw = XWINDOW (mini_window);
      wset_next (rw, mini_window);
      wset_prev (mw, root_window);
      mw->mini = 1;
      wset_frame (mw, frame);
865
      fset_minibuffer_window (f, mini_window);
866
      store_frame_param (f, Qminibuffer, Qt);
Jim Blandy's avatar
Jim Blandy committed
867 868 869 870
    }
  else
    {
      mini_window = Qnil;
871
      wset_next (rw, Qnil);
872
      fset_minibuffer_window (f, Qnil);
Jim Blandy's avatar
Jim Blandy committed
873 874
    }

875
  wset_frame (rw, frame);
Jim Blandy's avatar
Jim Blandy committed
876 877 878

  /* 10 is arbitrary,
     just so that there is "something there."
879
     Correct size will be set up later with adjust_frame_size.  */
Jim Blandy's avatar
Jim Blandy committed
880

881
  SET_FRAME_COLS (f, 10);
882
  SET_FRAME_LINES (f, 10);
883 884
  SET_FRAME_WIDTH (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f));
  SET_FRAME_HEIGHT (f, FRAME_LINES (f) * FRAME_LINE_HEIGHT (f));
Jim Blandy's avatar
Jim Blandy committed
885

886 887 888 889
  rw->total_cols = 10;
  rw->pixel_width = rw->total_cols * FRAME_COLUMN_WIDTH (f);
  rw->total_lines = mini_p ? 9 : 10;
  rw->pixel_height = rw->total_lines * FRAME_LINE_HEIGHT (f);
Jim Blandy's avatar
Jim Blandy committed
890 891 892

  if (mini_p)
    {
893 894 895 896 897 898
      mw->top_line = rw->total_lines;
      mw->pixel_top = rw->pixel_height;
      mw->total_cols = rw->total_cols;
      mw->pixel_width = rw->pixel_width;
      mw->total_lines = 1;
      mw->pixel_height = FRAME_LINE_HEIGHT (f);
Jim Blandy's avatar
Jim Blandy committed
899 900
    }

Jim Blandy's avatar
Jim Blandy committed
901
  /* Choose a buffer for the frame's root window.  */
Jim Blandy's avatar
Jim Blandy committed
902
  {
903
    Lisp_Object buf = Fcurrent_buffer ();
Jim Blandy's avatar
Jim Blandy committed
904

905 906
    /* If current buffer is hidden, try to find another one.  */
    if (BUFFER_HIDDEN_P (XBUFFER (buf)))
907
      buf = other_buffer_safely (buf);
908

909 910
    /* Use set_window_buffer, not Fset_window_buffer, and don't let
       hooks be run by it.  The reason is that the whole frame/window
Juanma Barranquero's avatar
Juanma Barranquero committed
911
       arrangement is not yet fully initialized at this point.  Windows
912 913 914
       don't have the right size, glyph matrices aren't initialized
       etc.  Running Lisp functions at this point surely ends in a
       SEGV.  */
915
    set_window_buffer (root_window, buf, 0, 0);
916
    fset_buffer_list (f, list1 (buf));
Jim Blandy's avatar
Jim Blandy committed
917 918
  }

Jim Blandy's avatar
Jim Blandy committed
919
  if (mini_p)
920 921 922 923 924 925 926 927 928
    {
      set_window_buffer (mini_window,
			 (NILP (Vminibuffer_list)
			  ? get_minibuffer (0)
			  : Fcar (Vminibuffer_list)),
			 0, 0);
      /* No horizontal scroll bars in minibuffers.  */
      wset_horizontal_scroll_bar (mw, Qnil);
    }
Jim Blandy's avatar
Jim Blandy committed
929

930 931
  fset_root_window (f, root_window);
  fset_selected_window (f, root_window);
Jim Blandy's avatar
Jim Blandy committed
932 933
  /* Make sure this window seems more recently used than
     a newly-created, never-selected window.  */
934
  XWINDOW (f->selected_window)->use_time = ++window_select_count;
Jim Blandy's avatar
Jim Blandy committed
935

Jim Blandy's avatar
Jim Blandy committed
936
  return f;
Jim Blandy's avatar
Jim Blandy committed
937 938
}

939
#ifdef HAVE_WINDOW_SYSTEM
Jim Blandy's avatar
Jim Blandy committed
940
/* Make a frame using a separate minibuffer window on another frame.
Jim Blandy's avatar
Jim Blandy committed
941 942 943
   MINI_WINDOW is the minibuffer window to use.  nil means use the
   default (the global minibuffer).  */

Jim Blandy's avatar
Jim Blandy committed
944
struct frame *
945 946
make_frame_without_minibuffer (Lisp_Object mini_window, KBOARD *kb,
			       Lisp_Object display)
Jim Blandy's avatar
Jim Blandy committed
947
{
948
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
949

950
  if (!NILP (mini_window))
951
    CHECK_LIVE_WINDOW (mini_window);
Jim Blandy's avatar
Jim Blandy committed
952

953
  if (!NILP (mini_window)
954
      && FRAME_KBOARD (XFRAME (XWINDOW (mini_window)->frame)) != kb)
955
    error ("Frame and minibuffer must be on the same terminal");
956

Jim Blandy's avatar
Jim Blandy committed
957 958
  /* Make a frame containing just a root window.  */
  f = make_frame (0);
Jim Blandy's avatar
Jim Blandy committed
959

960 961 962
  if (NILP (mini_window))
    {
      /* Use default-minibuffer-frame if possible.  */
963 964
      if (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
	  || ! FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame))))
965
	{
966 967 968
          Lisp_Object frame_dummy;

          XSETFRAME (frame_dummy, f);
969
	  /* If there's no minibuffer frame to use, create one.  */
Paul Eggert's avatar
Paul Eggert committed
970 971
	  kset_default_minibuffer_frame
	    (kb, call1 (intern ("make-initial-minibuffer-frame"), display));
972
	}
973

974 975
      mini_window
	= XFRAME (KVAR (kb, Vdefault_minibuffer_frame))->minibuffer_window;
976
    }
977

978
  fset_minibuffer_window (f, mini_window);
979
  store_frame_param (f, Qminibuffer, mini_window);
980 981 982

  /* Make the chosen minibuffer window display the proper minibuffer,
     unless it is already showing a minibuffer.  */
983
  if (NILP (Fmemq (XWINDOW (mini_window)->contents, Vminibuffer_list)))
984 985 986 987 988 989
    /* Use set_window_buffer instead of Fset_window_buffer (see
       discussion of bug#11984, bug#12025, bug#12026).  */
    set_window_buffer (mini_window,
		       (NILP (Vminibuffer_list)
			? get_minibuffer (0)
			: Fcar (Vminibuffer_list)), 0, 0);
Jim Blandy's avatar
Jim Blandy committed
990
  return f;
Jim Blandy's avatar
Jim Blandy committed
991 992
}

Jim Blandy's avatar
Jim Blandy committed
993
/* Make a frame containing only a minibuffer window.  */
Jim Blandy's avatar
Jim Blandy committed
994

Jim Blandy's avatar
Jim Blandy committed
995
struct frame *
996
make_minibuffer_frame (void)
Jim Blandy's avatar
Jim Blandy committed
997
{
Jim Blandy's avatar
Jim Blandy committed
998
  /* First make a frame containing just a root window, no minibuffer.  */
Jim Blandy's avatar
Jim Blandy committed
999

Jim Blandy's avatar
Jim Blandy committed
1000
  register struct frame *f = make_frame (0);
Jim Blandy's avatar
Jim Blandy committed
1001
  register Lisp_Object mini_window;
Jim Blandy's avatar
Jim Blandy committed
1002
  register Lisp_Object frame;
Jim Blandy's avatar
Jim Blandy committed
1003

1004
  XSETFRAME (frame, f);
Jim Blandy's avatar
Jim Blandy committed
1005

1006
  f->auto_raise = 0;
Jim Blandy's avatar
Jim Blandy committed
1007 1008 1009
  f->auto_lower = 0;
  f->no_split = 1;
  f->wants_modeline = 0;
Jim Blandy's avatar
Jim Blandy committed
1010 1011 1012 1013 1014

  /* Now label the root window as also being the minibuffer.
     Avoid infinite looping on the window chain by marking next pointer
     as nil. */

1015 1016
  mini_window = f->root_window;
  fset_minibuffer_window (f, mini_window);
1017
  store_frame_param (f, Qminibuffer, Qonly);
1018
  XWINDOW (mini_window)->mini = 1;
Paul Eggert's avatar
Paul Eggert committed
1019 1020 1021
  wset_next (XWINDOW (mini_window), Qnil);
  wset_prev (XWINDOW (mini_window), Qnil);
  wset_frame (XWINDOW (mini_window), frame);
Jim Blandy's avatar
Jim Blandy committed
1022 1023 1024

  /* Put the proper buffer in that window.  */

1025 1026 1027 1028 1029 1030
  /* Use set_window_buffer instead of Fset_window_buffer (see
     discussion of bug#11984, bug#12025, bug#12026).  */
  set_window_buffer (mini_window,
		     (NILP (Vminibuffer_list)
		      ? get_minibuffer (0)
		      : Fcar (Vminibuffer_list)), 0, 0);
Jim Blandy's avatar
Jim Blandy committed
1031
  return f;
Jim Blandy's avatar
Jim Blandy committed
1032
}
1033
#endif /* HAVE_WINDOW_SYSTEM */
Jim Blandy's avatar
Jim Blandy committed
1034

1035
/* Construct a frame that refers to a terminal.  */
Jim Blandy's avatar
Jim Blandy committed
1036

1037
static printmax_t tty_frame_count;
1038

Jim Blandy's avatar
Jim Blandy committed
1039
struct frame *
1040
make_initial_frame (void)
Jim Blandy's avatar
Jim Blandy committed
1041
{
1042
  struct frame *f;
1043
  struct terminal *terminal;
1044
  Lisp_Object frame;
1045