xfns.c 131 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Functions for the X window system.
Karl Heuer's avatar
Karl Heuer committed
2
   Copyright (C) 1989, 1992, 1993, 1994 Free Software Foundation.
Jim Blandy's avatar
Jim Blandy committed
3 4 5 6 7

This file is part of GNU Emacs.

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

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
along with GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Completely rewritten by Richard Stallman.  */

/* Rewritten for X11 by Joseph Arceneaux */

Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26
#include <signal.h>
#include <config.h>

27 28 29
/* This makes the fields of a Display accessible, in Xlib header files.  */
#define XLIB_ILLEGAL_ACCESS

Jim Blandy's avatar
Jim Blandy committed
30 31
#include "lisp.h"
#include "xterm.h"
Jim Blandy's avatar
Jim Blandy committed
32
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
33 34 35
#include "window.h"
#include "buffer.h"
#include "dispextern.h"
Jim Blandy's avatar
Jim Blandy committed
36
#include "keyboard.h"
37
#include "blockinput.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
38
#include "paths.h"
Jim Blandy's avatar
Jim Blandy committed
39 40 41 42

#ifdef HAVE_X_WINDOWS
extern void abort ();

43
#ifndef VMS
44
#if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work.  */
45 46
#include "bitmaps/gray.xbm"
#else
Jim Blandy's avatar
Jim Blandy committed
47
#include <X11/bitmaps/gray>
48
#endif
49 50 51
#else
#include "[.bitmaps]gray.xbm"
#endif
Jim Blandy's avatar
Jim Blandy committed
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#ifdef USE_X_TOOLKIT
#include <X11/Shell.h>

#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Label.h>

#ifdef USG
#undef USG	/* ####KLUDGE for Solaris 2.2 and up */
#include <X11/Xos.h>
#define USG
#else
#include <X11/Xos.h>
#endif

#include "widget.h"

#include "../lwlib/lwlib.h"

71 72 73
/* Do the EDITRES protocol if running X11R5 */
#if (XtSpecificationRelease >= 5)
#define HACK_EDITRES
74
extern void _XEditResCheckMessages ();
75 76 77
#endif /* R5 + Athena */

/* Unique id counter for widgets created by the Lucid Widget
78
   Library.  */
79 80
extern LWLIB_ID widget_id_tick;

81 82
/* This is part of a kludge--see lwlib/xlwmenu.c.  */
XFontStruct *xlwmenu_default_font;
83

84
extern void free_frame_menubar ();
85 86
#endif /* USE_X_TOOLKIT */

Jim Blandy's avatar
Jim Blandy committed
87 88 89
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))

Richard M. Stallman's avatar
Richard M. Stallman committed
90 91 92 93 94 95
#ifdef HAVE_X11R4
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
#else
#define MAXREQUEST(dpy) ((dpy)->max_request_size)
#endif

96 97
/* The name we're using in resource queries.  */
Lisp_Object Vx_resource_name;
98

Jim Blandy's avatar
Jim Blandy committed
99
/* The background and shape of the mouse pointer, and shape when not
100
   over text or in the modeline.  */
Jim Blandy's avatar
Jim Blandy committed
101
Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
102 103
/* The shape when over mouse-sensitive text.  */
Lisp_Object Vx_sensitive_text_pointer_shape;
Jim Blandy's avatar
Jim Blandy committed
104

105
/* Color of chars displayed in cursor box.  */
Jim Blandy's avatar
Jim Blandy committed
106 107
Lisp_Object Vx_cursor_fore_pixel;

108 109
/* Nonzero if using X.  */
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
110

111
/* Non nil if no window manager is in use.  */
Jim Blandy's avatar
Jim Blandy committed
112 113
Lisp_Object Vx_no_window_manager;

Richard M. Stallman's avatar
Richard M. Stallman committed
114 115 116
/* Search path for bitmap files.  */
Lisp_Object Vx_bitmap_file_path;

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/* Evaluate this expression to rebuild the section of syms_of_xfns
   that initializes and staticpros the symbols declared below.  Note
   that Emacs 18 has a bug that keeps C-x C-e from being able to
   evaluate this expression.

(progn
  ;; Accumulate a list of the symbols we want to initialize from the
  ;; declarations at the top of the file.
  (goto-char (point-min))
  (search-forward "/\*&&& symbols declared here &&&*\/\n")
  (let (symbol-list)
    (while (looking-at "Lisp_Object \\(Q[a-z_]+\\)")
      (setq symbol-list
	    (cons (buffer-substring (match-beginning 1) (match-end 1))
		  symbol-list))
      (forward-line 1))
    (setq symbol-list (nreverse symbol-list))
    ;; Delete the section of syms_of_... where we initialize the symbols.
    (search-forward "\n  /\*&&& init symbols here &&&*\/\n")
    (let ((start (point)))
      (while (looking-at "^  Q")
	(forward-line 2))
      (kill-region start (point)))
    ;; Write a new symbol initialization section.
    (while symbol-list
      (insert (format "  %s = intern (\"" (car symbol-list)))
      (let ((start (point)))
	(insert (substring (car symbol-list) 1))
	(subst-char-in-region start (point) ?_ ?-))
      (insert (format "\");\n  staticpro (&%s);\n" (car symbol-list)))
      (setq symbol-list (cdr symbol-list)))))

  */        

/*&&& symbols declared here &&&*/
Lisp_Object Qauto_raise;
Lisp_Object Qauto_lower;
Lisp_Object Qbackground_color;
Jim Blandy's avatar
Jim Blandy committed
155
Lisp_Object Qbar;
156 157
Lisp_Object Qborder_color;
Lisp_Object Qborder_width;
Jim Blandy's avatar
Jim Blandy committed
158
Lisp_Object Qbox;
159
Lisp_Object Qcursor_color;
Jim Blandy's avatar
Jim Blandy committed
160
Lisp_Object Qcursor_type;
161 162 163 164 165 166 167 168 169
Lisp_Object Qfont;
Lisp_Object Qforeground_color;
Lisp_Object Qgeometry;
Lisp_Object Qicon_left;
Lisp_Object Qicon_top;
Lisp_Object Qicon_type;
Lisp_Object Qinternal_border_width;
Lisp_Object Qleft;
Lisp_Object Qmouse_color;
170
Lisp_Object Qnone;
171
Lisp_Object Qparent_id;
172
Lisp_Object Qscroll_bar_width;
173
Lisp_Object Qsuppress_icon;
174
Lisp_Object Qtop;
Jim Blandy's avatar
Jim Blandy committed
175
Lisp_Object Qundefined_color;
176
Lisp_Object Qvertical_scroll_bars;
177
Lisp_Object Qvisibility;
178
Lisp_Object Qwindow_id;
Jim Blandy's avatar
Jim Blandy committed
179
Lisp_Object Qx_frame_parameter;
180
Lisp_Object Qx_resource_name;
181 182
Lisp_Object Quser_position;
Lisp_Object Quser_size;
183
Lisp_Object Qdisplay;
Jim Blandy's avatar
Jim Blandy committed
184

185
/* The below are defined in frame.c.  */
186
extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
187
extern Lisp_Object Qunsplittable, Qmenu_bar_lines;
188

Jim Blandy's avatar
Jim Blandy committed
189 190 191
extern Lisp_Object Vwindow_system_version;


Richard M. Stallman's avatar
Richard M. Stallman committed
192
/* Error if we are not connected to X.  */
Karl Heuer's avatar
Karl Heuer committed
193
void
Richard M. Stallman's avatar
Richard M. Stallman committed
194 195
check_x ()
{
196
  if (! x_in_use)
Richard M. Stallman's avatar
Richard M. Stallman committed
197 198 199
    error ("X windows are not in use or not initialized");
}

200 201 202 203 204
/* Nonzero if using X for display.  */

int
using_x_p ()
{
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  return x_in_use;
}

/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
   and checking validity for X.  */

FRAME_PTR
check_x_frame (frame)
     Lisp_Object frame;
{
  FRAME_PTR f;

  if (NILP (frame))
    f = selected_frame;
  else
    {
      CHECK_LIVE_FRAME (frame, 0);
      f = XFRAME (frame);
    }
  if (! FRAME_X_P (f))
    error ("non-X frame used");
  return f;
227 228
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/* Let the user specify an X display with a frame.
   nil stands for the selected frame--or, if that is not an X frame,
   the first X display on the list.  */

static struct x_display_info *
check_x_display_info (frame)
     Lisp_Object frame;
{
  if (NILP (frame))
    {
      if (FRAME_X_P (selected_frame))
	return FRAME_X_DISPLAY_INFO (selected_frame);
      else if (x_display_list != 0)
	return x_display_list;
      else
	error ("X windows are not in use or not initialized");
    }
  else if (STRINGP (frame))
    return x_display_info_for_name (frame);
  else
    {
      FRAME_PTR f;

      CHECK_LIVE_FRAME (frame, 0);
      f = XFRAME (frame);
      if (! FRAME_X_P (f))
	error ("non-X frame used");
      return FRAME_X_DISPLAY_INFO (f);
    }
}

Jim Blandy's avatar
Jim Blandy committed
260 261
/* Return the Emacs frame-object corresponding to an X window.
   It could be the frame's main window or an icon window.  */
Jim Blandy's avatar
Jim Blandy committed
262

263
/* This function can be called during GC, so use GC_xxx type test macros.  */
264

Jim Blandy's avatar
Jim Blandy committed
265 266
struct frame *
x_window_to_frame (wdesc)
Jim Blandy's avatar
Jim Blandy committed
267 268
     int wdesc;
{
Jim Blandy's avatar
Jim Blandy committed
269 270
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
271

272
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCONS (tail)->cdr)
Jim Blandy's avatar
Jim Blandy committed
273
    {
Jim Blandy's avatar
Jim Blandy committed
274
      frame = XCONS (tail)->car;
275
      if (!GC_FRAMEP (frame))
Jim Blandy's avatar
Jim Blandy committed
276
        continue;
Jim Blandy's avatar
Jim Blandy committed
277
      f = XFRAME (frame);
278 279 280
#ifdef USE_X_TOOLKIT
      if (f->display.nothing == 1) 
	return 0;
281 282
      if ((f->display.x->edit_widget 
	   && XtWindow (f->display.x->edit_widget) == wdesc)
283 284 285
          || f->display.x->icon_desc == wdesc)
        return f;
#else /* not USE_X_TOOLKIT */
286
      if (FRAME_X_WINDOW (f) == wdesc
Jim Blandy's avatar
Jim Blandy committed
287 288
          || f->display.x->icon_desc == wdesc)
        return f;
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
#endif /* not USE_X_TOOLKIT */
    }
  return 0;
}

#ifdef USE_X_TOOLKIT
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
x_any_window_to_frame (wdesc)
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
  struct x_display *x;

306
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCONS (tail)->cdr)
307 308
    {
      frame = XCONS (tail)->car;
309
      if (!GC_FRAMEP (frame))
310 311 312 313 314 315 316 317 318 319 320
        continue;
      f = XFRAME (frame);
      if (f->display.nothing == 1) 
	return 0;
      x = f->display.x;
      /* This frame matches if the window is any of its widgets.  */
      if (wdesc == XtWindow (x->widget) 
	  || wdesc == XtWindow (x->column_widget) 
	  || wdesc == XtWindow (x->edit_widget))
	return f;
      /* Match if the window is this frame's menubar.  */
321
      if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
322
	return f;
Jim Blandy's avatar
Jim Blandy committed
323 324 325
    }
  return 0;
}
326 327 328 329 330 331 332 333 334 335 336 337

/* Return the frame whose principal (outermost) window is WDESC.
   If WDESC is some other (smaller) window, we return 0.  */

struct frame *
x_top_window_to_frame (wdesc)
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
  struct x_display *x;

338
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCONS (tail)->cdr)
339 340
    {
      frame = XCONS (tail)->car;
341
      if (!GC_FRAMEP (frame))
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        continue;
      f = XFRAME (frame);
      if (f->display.nothing == 1) 
	return 0;
      x = f->display.x;
      /* This frame matches if the window is its topmost widget.  */
      if (wdesc == XtWindow (x->widget))
	return f;
      /* Match if the window is this frame's menubar.  */
      if (x->menubar_widget 
	  && wdesc == XtWindow (x->menubar_widget))
	return f;
    }
  return 0;
}
357
#endif /* USE_X_TOOLKIT */
Jim Blandy's avatar
Jim Blandy committed
358

359 360 361 362 363 364 365 366 367 368 369


/* Code to deal with bitmaps.  Bitmaps are referenced by their bitmap
   id, which is just an int that this section returns.  Bitmaps are
   reference counted so they can be shared among frames.

   Bitmap indices are guaranteed to be > 0, so a negative number can
   be used to indicate no bitmap.

   If you use x_create_bitmap_from_data, then you must keep track of
   the bitmaps yourself.  That is, creating a bitmap from the same
370
   data more than once will not be caught.  */
371 372


Richard M. Stallman's avatar
Richard M. Stallman committed
373 374 375 376 377 378 379
/* Functions to access the contents of a bitmap, given an id.  */

int
x_bitmap_height (f, id)
     FRAME_PTR f;
     int id;
{
380
  return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
Richard M. Stallman's avatar
Richard M. Stallman committed
381 382 383 384 385 386 387
}

int
x_bitmap_width (f, id)
     FRAME_PTR f;
     int id;
{
388
  return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
Richard M. Stallman's avatar
Richard M. Stallman committed
389 390 391 392 393 394 395
}

int
x_bitmap_pixmap (f, id)
     FRAME_PTR f;
     int id;
{
396
  return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
Richard M. Stallman's avatar
Richard M. Stallman committed
397 398 399
}


400 401 402
/* Allocate a new bitmap record.  Returns index of new record.  */

static int
403 404
x_allocate_bitmap_record (f)
     FRAME_PTR f;
405
{
406 407 408 409
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
  int i;

  if (dpyinfo->bitmaps == NULL)
410
    {
411 412 413 414
      dpyinfo->bitmaps_size = 10;
      dpyinfo->bitmaps
	= (struct x_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
      dpyinfo->bitmaps_last = 1;
415 416 417
      return 1;
    }

418 419
  if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
    return ++dpyinfo->bitmaps_last;
420

421 422 423
  for (i = 0; i < dpyinfo->bitmaps_size; ++i)
    if (dpyinfo->bitmaps[i].refcount == 0)
      return i + 1;
424

425 426 427 428 429
  dpyinfo->bitmaps_size *= 2;
  dpyinfo->bitmaps
    = (struct x_bitmap_record *) xrealloc (dpyinfo->bitmaps,
					   dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
  return ++dpyinfo->bitmaps_last;
430 431 432 433 434
}

/* Add one reference to the reference count of the bitmap with id ID.  */

void
Richard M. Stallman's avatar
Richard M. Stallman committed
435 436
x_reference_bitmap (f, id)
     FRAME_PTR f;
437 438
     int id;
{
439
  ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
440 441 442 443 444 445 446 447 448 449
}

/* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS.  */

int
x_create_bitmap_from_data (f, bits, width, height)
     struct frame *f;
     char *bits;
     unsigned int width, height;
{
450
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
451 452 453
  Pixmap bitmap;
  int id;

454
  bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
455 456 457 458 459
				  bits, width, height);

  if (! bitmap)
    return -1;

460 461 462 463 464 465 466
  id = x_allocate_bitmap_record (f);
  dpyinfo->bitmaps[id - 1].pixmap = bitmap;
  dpyinfo->bitmaps[id - 1].file = NULL;
  dpyinfo->bitmaps[id - 1].refcount = 1;
  dpyinfo->bitmaps[id - 1].depth = 1;
  dpyinfo->bitmaps[id - 1].height = height;
  dpyinfo->bitmaps[id - 1].width = width;
467 468 469 470 471 472 473 474 475

  return id;
}

/* Create bitmap from file FILE for frame F.  */

int
x_create_bitmap_from_file (f, file)
     struct frame *f;
Richard M. Stallman's avatar
Richard M. Stallman committed
476
     Lisp_Object file;
477
{
478
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
479 480 481
  unsigned int width, height;
  Pixmap bitmap;
  int xhot, yhot, result, id;
Richard M. Stallman's avatar
Richard M. Stallman committed
482 483 484
  Lisp_Object found;
  int fd;
  char *filename;
485 486

  /* Look for an existing bitmap with the same name.  */
487
  for (id = 0; id < dpyinfo->bitmaps_last; ++id)
488
    {
489 490 491
      if (dpyinfo->bitmaps[id].refcount
	  && dpyinfo->bitmaps[id].file
	  && !strcmp (dpyinfo->bitmaps[id].file, (char *) XSTRING (file)->data))
492
	{
493
	  ++dpyinfo->bitmaps[id].refcount;
494 495 496 497
	  return id + 1;
	}
    }

Richard M. Stallman's avatar
Richard M. Stallman committed
498 499 500 501 502 503 504 505
  /* Search bitmap-file-path for the file, if appropriate.  */
  fd = openp (Vx_bitmap_file_path, file, "", &found, 0);
  if (fd < 0)
    return -1;
  close (fd);

  filename = (char *) XSTRING (found)->data;

506
  result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
Richard M. Stallman's avatar
Richard M. Stallman committed
507
			    filename, &width, &height, &bitmap, &xhot, &yhot);
508 509 510
  if (result != BitmapSuccess)
    return -1;

511 512 513 514 515 516 517 518
  id = x_allocate_bitmap_record (f);
  dpyinfo->bitmaps[id - 1].pixmap = bitmap;
  dpyinfo->bitmaps[id - 1].refcount = 1;
  dpyinfo->bitmaps[id - 1].file = (char *) xmalloc (XSTRING (file)->size + 1);
  dpyinfo->bitmaps[id - 1].depth = 1;
  dpyinfo->bitmaps[id - 1].height = height;
  dpyinfo->bitmaps[id - 1].width = width;
  strcpy (dpyinfo->bitmaps[id - 1].file, XSTRING (file)->data);
519 520 521 522 523 524 525

  return id;
}

/* Remove reference to bitmap with id number ID.  */

int
Richard M. Stallman's avatar
Richard M. Stallman committed
526 527
x_destroy_bitmap (f, id)
     FRAME_PTR f;
528 529
     int id;
{
530 531
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);

532 533
  if (id > 0)
    {
534 535
      --dpyinfo->bitmaps[id - 1].refcount;
      if (dpyinfo->bitmaps[id - 1].refcount == 0)
536
	{
537 538
	  XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].pixmap);
	  if (dpyinfo->bitmaps[id - 1].file)
539
	    {
540 541
	      free (dpyinfo->bitmaps[id - 1].file);
	      dpyinfo->bitmaps[id - 1].file = NULL;
542 543 544 545 546
	    }
	}
    }
}

547
/* Free all the bitmaps for the display specified by DPYINFO.  */
548

549 550 551
static void
x_destroy_all_bitmaps (dpyinfo)
     struct x_display_info *dpyinfo;
552
{
553 554 555 556 557 558 559 560 561
  int i;
  for (i = 0; i < dpyinfo->bitmaps_last; i++)
    if (dpyinfo->bitmaps[i].refcount > 0)
      {
	XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].pixmap);
	if (dpyinfo->bitmaps[i].file)
	  free (dpyinfo->bitmaps[i].file);
      }
  dpyinfo->bitmaps_last = 0;
562
}
Jim Blandy's avatar
Jim Blandy committed
563

Jim Blandy's avatar
Jim Blandy committed
564
/* Connect the frame-parameter names for X frames
Jim Blandy's avatar
Jim Blandy committed
565 566 567
   to the ways of passing the parameter values to the window system.

   The name of a parameter, as a Lisp symbol,
Jim Blandy's avatar
Jim Blandy committed
568 569
   has an `x-frame-parameter' property which is an integer in Lisp
   but can be interpreted as an `enum x_frame_parm' in C.  */
Jim Blandy's avatar
Jim Blandy committed
570

Jim Blandy's avatar
Jim Blandy committed
571
enum x_frame_parm
Jim Blandy's avatar
Jim Blandy committed
572 573 574 575 576 577 578 579 580 581 582 583 584
{
  X_PARM_FOREGROUND_COLOR,
  X_PARM_BACKGROUND_COLOR,
  X_PARM_MOUSE_COLOR,
  X_PARM_CURSOR_COLOR,
  X_PARM_BORDER_COLOR,
  X_PARM_ICON_TYPE,
  X_PARM_FONT,
  X_PARM_BORDER_WIDTH,
  X_PARM_INTERNAL_BORDER_WIDTH,
  X_PARM_NAME,
  X_PARM_AUTORAISE,
  X_PARM_AUTOLOWER,
585
  X_PARM_VERT_SCROLL_BAR,
586 587
  X_PARM_VISIBILITY,
  X_PARM_MENU_BAR_LINES
Jim Blandy's avatar
Jim Blandy committed
588 589 590
};


Jim Blandy's avatar
Jim Blandy committed
591
struct x_frame_parm_table
Jim Blandy's avatar
Jim Blandy committed
592 593
{
  char *name;
Jim Blandy's avatar
Jim Blandy committed
594
  void (*setter)( /* struct frame *frame, Lisp_Object val, oldval */ );
Jim Blandy's avatar
Jim Blandy committed
595 596 597 598 599 600 601
};

void x_set_foreground_color ();
void x_set_background_color ();
void x_set_mouse_color ();
void x_set_cursor_color ();
void x_set_border_color ();
Jim Blandy's avatar
Jim Blandy committed
602
void x_set_cursor_type ();
Jim Blandy's avatar
Jim Blandy committed
603 604 605 606
void x_set_icon_type ();
void x_set_font ();
void x_set_border_width ();
void x_set_internal_border_width ();
607
void x_explicitly_set_name ();
Jim Blandy's avatar
Jim Blandy committed
608 609
void x_set_autoraise ();
void x_set_autolower ();
610
void x_set_vertical_scroll_bars ();
611 612
void x_set_visibility ();
void x_set_menu_bar_lines ();
613
void x_set_scroll_bar_width ();
614
void x_set_unsplittable ();
Jim Blandy's avatar
Jim Blandy committed
615

Jim Blandy's avatar
Jim Blandy committed
616
static struct x_frame_parm_table x_frame_parms[] =
Jim Blandy's avatar
Jim Blandy committed
617 618 619 620 621 622
{
  "foreground-color", x_set_foreground_color,
  "background-color", x_set_background_color,
  "mouse-color", x_set_mouse_color,
  "cursor-color", x_set_cursor_color,
  "border-color", x_set_border_color,
Jim Blandy's avatar
Jim Blandy committed
623
  "cursor-type", x_set_cursor_type,
Jim Blandy's avatar
Jim Blandy committed
624 625 626 627
  "icon-type", x_set_icon_type,
  "font", x_set_font,
  "border-width", x_set_border_width,
  "internal-border-width", x_set_internal_border_width,
628
  "name", x_explicitly_set_name,
629 630
  "auto-raise", x_set_autoraise,
  "auto-lower", x_set_autolower,
631
  "vertical-scroll-bars", x_set_vertical_scroll_bars,
632 633
  "visibility", x_set_visibility,
  "menu-bar-lines", x_set_menu_bar_lines,
634
  "scroll-bar-width", x_set_scroll_bar_width,
635
  "unsplittable", x_set_unsplittable,
Jim Blandy's avatar
Jim Blandy committed
636 637
};

Jim Blandy's avatar
Jim Blandy committed
638
/* Attach the `x-frame-parameter' properties to
Jim Blandy's avatar
Jim Blandy committed
639 640 641 642 643 644
   the Lisp symbol names of parameters relevant to X.  */

init_x_parm_symbols ()
{
  int i;

645
  for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
Jim Blandy's avatar
Jim Blandy committed
646
    Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
Jim Blandy's avatar
Jim Blandy committed
647 648 649
	  make_number (i));
}

650 651 652
/* Change the parameters of FRAME as specified by ALIST.
   If a parameter is not specially recognized, do nothing;
   otherwise call the `x_set_...' function for that parameter.  */
653

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
void
x_set_frame_parameters (f, alist)
     FRAME_PTR f;
     Lisp_Object alist;
{
  Lisp_Object tail;

  /* If both of these parameters are present, it's more efficient to
     set them both at once.  So we wait until we've looked at the
     entire list before we set them.  */
  Lisp_Object width, height;

  /* Same here.  */
  Lisp_Object left, top;

669 670 671
  /* Same with these.  */
  Lisp_Object icon_left, icon_top;

672 673 674 675
  /* Record in these vectors all the parms specified.  */
  Lisp_Object *parms;
  Lisp_Object *values;
  int i;
676
  int left_no_change = 0, top_no_change = 0;
677
  int icon_left_no_change = 0, icon_top_no_change = 0;
678

679 680 681 682 683 684
  i = 0;
  for (tail = alist; CONSP (tail); tail = Fcdr (tail))
    i++;

  parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
  values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
685

686 687 688
  /* Extract parm names and values into those vectors.  */

  i = 0;
689 690 691 692 693
  for (tail = alist; CONSP (tail); tail = Fcdr (tail))
    {
      Lisp_Object elt, prop, val;

      elt = Fcar (tail);
694 695 696 697 698
      parms[i] = Fcar (elt);
      values[i] = Fcdr (elt);
      i++;
    }

699
  width = height = top = left = Qunbound;
700
  icon_left = icon_top = Qunbound;
701

702 703 704 705 706 707 708 709 710
  /* Now process them in reverse of specified order.  */
  for (i--; i >= 0; i--)
    {
      Lisp_Object prop, val;

      prop = parms[i];
      val = values[i];

      if (EQ (prop, Qwidth))
711
	width = val;
712
      else if (EQ (prop, Qheight))
713
	height = val;
714
      else if (EQ (prop, Qtop))
715
	top = val;
716
      else if (EQ (prop, Qleft))
717
	left = val;
718 719 720 721
      else if (EQ (prop, Qicon_top))
	icon_top = val;
      else if (EQ (prop, Qicon_left))
	icon_left = val;
722 723
      else
	{
724
	  register Lisp_Object param_index, old_value;
725

726 727
	  param_index = Fget (prop, Qx_frame_parameter);
	  old_value = get_frame_param (f, prop);
728
	  store_frame_param (f, prop, val);
729 730
 	  if (NATNUMP (param_index)
	      && (XFASTINT (param_index)
731 732
		  < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
	    (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
733 734 735
	}
    }

736 737
  /* Don't die if just one of these was set.  */
  if (EQ (left, Qunbound))
738 739 740 741 742
    {
      left_no_change = 1;
      if (f->display.x->left_pos < 0)
	left = Fcons (Qplus, Fcons (make_number (f->display.x->left_pos), Qnil));
      else
743
	XSETINT (left, f->display.x->left_pos);
744
    }
745
  if (EQ (top, Qunbound))
746 747 748 749 750
    {
      top_no_change = 1;
      if (f->display.x->top_pos < 0)
	top = Fcons (Qplus, Fcons (make_number (f->display.x->top_pos), Qnil));
      else
751
	XSETINT (top, f->display.x->top_pos);
752
    }
753

754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
  /* If one of the icon positions was not set, preserve or default it.  */
  if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
    {
      icon_left_no_change = 1;
      icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
      if (NILP (icon_left))
	XSETINT (icon_left, 0);
    }
  if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
    {
      icon_top_no_change = 1;
      icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
      if (NILP (icon_top))
	XSETINT (icon_top, 0);
    }

770 771
  /* Don't die if just one of these was set.  */
  if (EQ (width, Qunbound))
772
    XSETINT (width, FRAME_WIDTH (f));
773
  if (EQ (height, Qunbound))
774
    XSETINT (height, FRAME_HEIGHT (f));
775

776 777 778 779 780 781 782 783
  /* Don't set these parameters these unless they've been explicitly
     specified.  The window might be mapped or resized while we're in
     this function, and we don't want to override that unless the lisp
     code has asked for it.

     Don't set these parameters unless they actually differ from the
     window's current parameters; the window may not actually exist
     yet.  */
784 785 786
  {
    Lisp_Object frame;

787 788
    check_frame_size (f, &height, &width);

789
    XSETFRAME (frame, f);
790

791 792
    if ((NUMBERP (width) && XINT (width) != FRAME_WIDTH (f))
	|| (NUMBERP (height) && XINT (height) != FRAME_HEIGHT (f)))
793
      Fset_frame_size (frame, width, height);
794 795

    if ((!NILP (left) || !NILP (top))
796
	&& ! (left_no_change && top_no_change)
797 798 799
	&& ! (NUMBERP (left) && XINT (left) == f->display.x->left_pos
	      && NUMBERP (top) && XINT (top) == f->display.x->top_pos))
      {
800 801
	int leftpos = 0;
	int toppos = 0;
802 803 804

	/* Record the signs.  */
	f->display.x->size_hint_flags &= ~ (XNegative | YNegative);
805
	if (EQ (left, Qminus))
806
	  f->display.x->size_hint_flags |= XNegative;
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	else if (INTEGERP (left))
	  {
	    leftpos = XINT (left);
	    if (leftpos < 0)
	      f->display.x->size_hint_flags |= XNegative;
	  }
	else if (CONSP (left) && EQ (XCONS (left)->car, Qminus)
		 && CONSP (XCONS (left)->cdr)
		 && INTEGERP (XCONS (XCONS (left)->cdr)->car))
	  {
	    leftpos = - XINT (XCONS (XCONS (left)->cdr)->car);
	    f->display.x->size_hint_flags |= XNegative;
	  }
	else if (CONSP (left) && EQ (XCONS (left)->car, Qplus)
		 && CONSP (XCONS (left)->cdr)
		 && INTEGERP (XCONS (XCONS (left)->cdr)->car))
	  {
	    leftpos = XINT (XCONS (XCONS (left)->cdr)->car);
	  }

	if (EQ (top, Qminus))
828
	  f->display.x->size_hint_flags |= YNegative;
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
	else if (INTEGERP (top))
	  {
	    toppos = XINT (top);
	    if (toppos < 0)
	      f->display.x->size_hint_flags |= YNegative;
	  }
	else if (CONSP (top) && EQ (XCONS (top)->car, Qminus)
		 && CONSP (XCONS (top)->cdr)
		 && INTEGERP (XCONS (XCONS (top)->cdr)->car))
	  {
	    toppos = - XINT (XCONS (XCONS (top)->cdr)->car);
	    f->display.x->size_hint_flags |= YNegative;
	  }
	else if (CONSP (top) && EQ (XCONS (top)->car, Qplus)
		 && CONSP (XCONS (top)->cdr)
		 && INTEGERP (XCONS (XCONS (top)->cdr)->car))
	  {
	    toppos = XINT (XCONS (XCONS (top)->cdr)->car);
	  }


	/* Store the numeric value of the position.  */
	f->display.x->top_pos = toppos;
	f->display.x->left_pos = leftpos;

854 855 856 857 858
	f->display.x->win_gravity = NorthWestGravity;

	/* Actually set that position, and convert to absolute.  */
	x_set_offset (f, leftpos, toppos, 0);
      }
859 860 861 862

    if ((!NILP (icon_left) || !NILP (icon_top))
	&& ! (icon_left_no_change && icon_top_no_change))
      x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
863 864
  }
}
Jim Blandy's avatar
Jim Blandy committed
865

866
/* Store the screen positions of frame F into XPTR and YPTR.
867 868 869 870 871 872 873 874
   These are the positions of the containing window manager window,
   not Emacs's own window.  */

void
x_real_positions (f, xptr, yptr)
     FRAME_PTR f;
     int *xptr, *yptr;
{
875
  int win_x, win_y;
876 877
  Window child;

878 879 880 881 882 883 884 885 886 887 888 889
  /* This is pretty gross, but seems to be the easiest way out of
     the problem that arises when restarting window-managers.  */

#ifdef USE_X_TOOLKIT
  Window outer = XtWindow (f->display.x->widget);
#else
  Window outer = f->display.x->window_desc;
#endif
  Window tmp_root_window;
  Window *tmp_children;
  int tmp_nchildren;

890
  x_catch_errors (FRAME_X_DISPLAY (f));
891
  while (1)
892
    {
893 894 895 896 897 898 899 900 901 902 903 904 905 906
      XQueryTree (FRAME_X_DISPLAY (f), outer, &tmp_root_window,
		  &f->display.x->parent_desc,
		  &tmp_children, &tmp_nchildren);
      xfree (tmp_children);

      win_x = win_y = 0;

      /* Find the position of the outside upper-left corner of
	 the inner window, with respect to the outer window.  */
      if (f->display.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
	{
	  XTranslateCoordinates (FRAME_X_DISPLAY (f),

				 /* From-window, to-window.  */
907
#ifdef USE_X_TOOLKIT
908
				 XtWindow (f->display.x->widget),
909
#else
910
				 f->display.x->window_desc,
911
#endif
912
				 f->display.x->parent_desc,
913

914 915
				 /* From-position, to-position.  */
				 0, 0, &win_x, &win_y,
916

917 918 919 920 921 922
				 /* Child of win.  */
				 &child);

	  win_x += f->display.x->border_width;
	  win_y += f->display.x->border_width;
	}
923

924 925 926 927 928
      /* It is possible for the window returned by the XQueryNotify
	 to become invalid by the time we call XTranslateCoordinates.
	 That can happen when you restart some window managers.
	 If so, we get an error in XTranslateCoordinates.
	 Detect that and try the whole thing over.  */
929
      if (! x_had_errors_p (FRAME_X_DISPLAY (f)))
930
	break;
931
    }
932

933
  x_uncatch_errors (FRAME_X_DISPLAY (f));
934

935 936 937 938
  *xptr = f->display.x->left_pos - win_x;
  *yptr = f->display.x->top_pos - win_y;
}

Jim Blandy's avatar
Jim Blandy committed
939
/* Insert a description of internally-recorded parameters of frame X
Jim Blandy's avatar
Jim Blandy committed
940 941
   into the parameter alist *ALISTPTR that is to be given to the user.
   Only parameters that are specific to the X window system
Jim Blandy's avatar
Jim Blandy committed
942
   and whose values are not correctly recorded in the frame's
Jim Blandy's avatar
Jim Blandy committed
943 944
   param_alist need to be considered here.  */

Jim Blandy's avatar
Jim Blandy committed
945 946
x_report_frame_params (f, alistptr)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
947 948 949 950
     Lisp_Object *alistptr;
{
  char buf[16];

951 952 953
  store_in_alist (alistptr, Qleft, make_number (f->display.x->left_pos));
  store_in_alist (alistptr, Qtop, make_number (f->display.x->top_pos));
  store_in_alist (alistptr, Qborder_width,
Jim Blandy's avatar
Jim Blandy committed
954
       	   make_number (f->display.x->border_width));
955
  store_in_alist (alistptr, Qinternal_border_width,
Jim Blandy's avatar
Jim Blandy committed
956
       	   make_number (f->display.x->internal_border_width));
957
  sprintf (buf, "%ld", (long) FRAME_X_WINDOW (f));
958
  store_in_alist (alistptr, Qwindow_id,
Jim Blandy's avatar
Jim Blandy committed
959
       	   build_string (buf));
960
  FRAME_SAMPLE_VISIBILITY (f);
961 962 963
  store_in_alist (alistptr, Qvisibility,
		  (FRAME_VISIBLE_P (f) ? Qt
		   : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
Jim Blandy's avatar
Jim Blandy committed
964 965
}

966

967 968 969 970
/* Decide if color named COLOR is valid for the display associated with
   the selected frame; if so, return the rgb values in COLOR_DEF.
   If ALLOC is nonzero, allocate a new colormap cell.  */

Jim Blandy's avatar
Jim Blandy committed
971
int
972 973
defined_color (f, color, color_def, alloc)
     FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
974
     char *color;
975
     XColor *color_def;
976
     int alloc;
Jim Blandy's avatar
Jim Blandy committed
977
{
978
  register int status;
Jim Blandy's avatar
Jim Blandy committed
979
  Colormap screen_colormap;
980
  Display *display = FRAME_X_DISPLAY (f);
Jim Blandy's avatar
Jim Blandy committed
981 982

  BLOCK_INPUT;
983
  screen_colormap = DefaultColormap (display, XDefaultScreen (display));
Jim Blandy's avatar
Jim Blandy committed
984

985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
  status = XParseColor (display, screen_colormap, color, color_def);
  if (status && alloc) 
    {
      status = XAllocColor (display, screen_colormap, color_def);
      if (!status)
	{
	  /* If we got to this point, the colormap is full, so we're 
	     going to try and get the next closest color.
	     The algorithm used is a least-squares matching, which is
	     what X uses for closest color matching with StaticColor visuals.  */

	  XColor *cells;
	  int no_cells;
	  int nearest;
	  long nearest_delta, trial_delta;
	  int x;

	  no_cells = XDisplayCells (display, XDefaultScreen (display));
	  cells = (XColor *) alloca (sizeof (XColor) * no_cells);

	  for (x = 0; x < no_cells; x++) 
	    cells[x].pixel = x;

	  XQueryColors (display, screen_colormap, cells, no_cells);
	  nearest = 0;
	  /* I'm assuming CSE so I'm not going to condense this. */
	  nearest_delta = ((((color_def->red >> 8) - (cells[0].red >> 8))
			    * ((color_def->red >> 8) - (cells[0].red >> 8)))
			   +
			   (((color_def->green >> 8) - (cells[0].green >> 8))
			    * ((color_def->green >> 8) - (cells[0].green >> 8)))
			   +
			   (((color_def->blue >> 8) - (cells[0].blue >> 8))
			    * ((color_def->blue >> 8) - (cells[0].blue >> 8))));
	  for (x = 1; x < no_cells; x++) 
	    {
	      trial_delta = ((((color_def->red >> 8) - (cells[x].red >> 8))
			      * ((color_def->red >> 8) - (cells[x].red >> 8)))
			     +
			     (((color_def->green >> 8) - (cells[x].green >> 8))
			      * ((color_def->green >> 8) - (cells[x].green >> 8))) +
			     +
			     (((color_def->blue >> 8) - (cells[x].blue >> 8))
			      * ((color_def->blue >> 8) - (cells[x].blue >> 8))));
	      if (trial_delta < nearest_delta) 
		{
		  nearest = x;
		  nearest_delta = trial_delta;
		}
	    }
	  color_def->red = cells[nearest].red;
	  color_def->green = cells[nearest].green;
	  color_def->blue = cells[nearest].blue;
	  status = XAllocColor (display, screen_colormap, color_def);
	}
    }
Jim Blandy's avatar
Jim Blandy committed
1041 1042
  UNBLOCK_INPUT;

1043
  if (status)
Jim Blandy's avatar
Jim Blandy committed
1044 1045 1046 1047 1048 1049
    return 1;
  else
    return 0;
}

/* Given a string ARG naming a color, compute a pixel value from it
Jim Blandy's avatar
Jim Blandy committed
1050 1051
   suitable for screen F.
   If F is not a color screen, return DEF (default) regardless of what
Jim Blandy's avatar
Jim Blandy committed
1052 1053 1054
   ARG says.  */

int
1055 1056
x_decode_color (f, arg, def)
     FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
1057 1058 1059
     Lisp_Object arg;
     int def;
{
1060
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
1061 1062 1063 1064

  CHECK_STRING (arg, 0);

  if (strcmp (XSTRING (arg)->data, "black") == 0)
1065
    return BLACK_PIX_DEFAULT (f);
Jim Blandy's avatar
Jim Blandy committed
1066
  else if (strcmp (XSTRING (arg)->data, "white") == 0)
1067
    return WHITE_PIX_DEFAULT (f);
Jim Blandy's avatar
Jim Blandy committed
1068

1069
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
Jim Blandy's avatar
Jim Blandy committed
1070 1071
    return def;

1072 1073 1074 1075 1076
  /* Ignore the return value of defined_color so that
     we use a color close to the one requested
     if we can't get the exact request.  */
  defined_color (f, XSTRING (arg)->data, &cdef, 1);
  return cdef.pixel;
Jim Blandy's avatar
Jim Blandy committed
1077 1078
}

Jim Blandy's avatar
Jim Blandy committed
1079
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
1080 1081
   to set individual parameters.

1082
   If FRAME_X_WINDOW (f) is 0,
Jim Blandy's avatar
Jim Blandy committed
1083
   the frame is being created and its X-window does not exist yet.
Jim Blandy's avatar
Jim Blandy committed
1084 1085 1086 1087
   In that case, just record the parameter's new value
   in the standard place; do not attempt to change the window.  */

void
Jim Blandy's avatar
Jim Blandy committed
1088 1089
x_set_foreground_color (f, arg, oldval)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
1090 1091
     Lisp_Object arg, oldval;
{
1092 1093
  f->display.x->foreground_pixel
    = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1094
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
1095 1096
    {
      BLOCK_INPUT;
1097
      XSetForeground (FRAME_X_DISPLAY (f), f->display.x->normal_gc,
Jim Blandy's avatar
Jim Blandy committed
1098
		      f->display.x->foreground_pixel);
1099
      XSetBackground (FRAME_X_DISPLAY (f), f->display.x->reverse_gc,
Jim Blandy's avatar
Jim Blandy committed
1100
		      f->display.x->foreground_pixel);
Jim Blandy's avatar
Jim Blandy committed
1101
      UNBLOCK_INPUT;
1102
      recompute_basic_faces (f);
1103
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
1104
        redraw_frame (f);