xfns.c 286 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Functions for the X window system.
Dave Love's avatar
Dave Love committed
2
   Copyright (C) 1989, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 01, 02, 03
3
     Free Software Foundation.
Jim Blandy's avatar
Jim Blandy committed
4 5 6 7 8

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
9
the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
10 11 12 13 14 15 16 17 18
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
19 20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
21

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

27 28 29 30
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

31
/* This makes the fields of a Display accessible, in Xlib header files.  */
32

33 34
#define XLIB_ILLEGAL_ACCESS

Jim Blandy's avatar
Jim Blandy committed
35 36
#include "lisp.h"
#include "xterm.h"
Jim Blandy's avatar
Jim Blandy committed
37
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
38 39
#include "window.h"
#include "buffer.h"
Kenichi Handa's avatar
Kenichi Handa committed
40
#include "intervals.h"
Jim Blandy's avatar
Jim Blandy committed
41
#include "dispextern.h"
Jim Blandy's avatar
Jim Blandy committed
42
#include "keyboard.h"
43
#include "blockinput.h"
44
#include <epaths.h>
Karl Heuer's avatar
Karl Heuer committed
45
#include "charset.h"
Kenichi Handa's avatar
Kenichi Handa committed
46
#include "coding.h"
Karl Heuer's avatar
Karl Heuer committed
47
#include "fontset.h"
48 49
#include "systime.h"
#include "termhooks.h"
50
#include "atimer.h"
Jim Blandy's avatar
Jim Blandy committed
51 52

#ifdef HAVE_X_WINDOWS
53 54

#include <ctype.h>
55 56
#include <sys/types.h>
#include <sys/stat.h>
Jim Blandy's avatar
Jim Blandy committed
57

58
#ifndef VMS
59
#if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work.  */
60 61
#include "bitmaps/gray.xbm"
#else
Jim Blandy's avatar
Jim Blandy committed
62
#include <X11/bitmaps/gray>
63
#endif
64 65 66
#else
#include "[.bitmaps]gray.xbm"
#endif
Jim Blandy's avatar
Jim Blandy committed
67

Jan Djärv's avatar
Jan Djärv committed
68 69 70 71
#ifdef USE_GTK
#include "gtkutil.h"
#endif

72 73 74
#ifdef USE_X_TOOLKIT
#include <X11/Shell.h>

75
#ifndef USE_MOTIF
76 77
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Label.h>
78
#endif /* USE_MOTIF */
79 80 81 82 83 84 85 86 87 88 89 90 91

#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"

92 93 94 95 96 97
#ifdef USE_MOTIF
#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/FileSB.h>
#endif

98 99
/* Do the EDITRES protocol if running X11R5
   Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
100

101
#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
102
#define HACK_EDITRES
103
extern void _XEditResCheckMessages ();
104 105
#endif /* R5 + Athena */

106 107
/* Unique id counter for widgets created by the Lucid Widget Library.  */

108 109
extern LWLIB_ID widget_id_tick;

110
#ifdef USE_LUCID
111
/* This is part of a kludge--see lwlib/xlwmenu.c.  */
112
extern XFontStruct *xlwmenu_default_font;
113
#endif
114

115
extern void free_frame_menubar ();
116
extern double atof ();
117

118 119 120 121 122 123 124 125
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

126 127
#endif /* USE_X_TOOLKIT */

Richard M. Stallman's avatar
Richard M. Stallman committed
128 129 130 131 132 133
#ifdef HAVE_X11R4
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
#else
#define MAXREQUEST(dpy) ((dpy)->max_request_size)
#endif

134 135 136 137 138 139
/* The gray bitmap `bitmaps/gray'.  This is done because xterm.c uses
   it, and including `bitmaps/gray' more than once is a problem when
   config.h defines `static' as an empty replacement string.  */

int gray_bitmap_width = gray_width;
int gray_bitmap_height = gray_height;
140
char *gray_bitmap_bits = gray_bits;
141

142
/* Non-zero means we're allowed to display an hourglass cursor.  */
143

144
int display_hourglass_p;
145

Jim Blandy's avatar
Jim Blandy committed
146
/* The background and shape of the mouse pointer, and shape when not
147
   over text or in the modeline.  */
148

Jim Blandy's avatar
Jim Blandy committed
149
Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
150
Lisp_Object Vx_hourglass_pointer_shape;
151

152
/* The shape when over mouse-sensitive text.  */
153

154
Lisp_Object Vx_sensitive_text_pointer_shape;
Jim Blandy's avatar
Jim Blandy committed
155

156 157 158 159 160
/* If non-nil, the pointer shape to indicate that windows can be
   dragged horizontally.  */

Lisp_Object Vx_window_horizontal_drag_shape;

161
/* Color of chars displayed in cursor box.  */
162

Jim Blandy's avatar
Jim Blandy committed
163 164
Lisp_Object Vx_cursor_fore_pixel;

165
/* Nonzero if using X.  */
166

167
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
168

169
/* Non nil if no window manager is in use.  */
170

Jim Blandy's avatar
Jim Blandy committed
171 172
Lisp_Object Vx_no_window_manager;

Richard M. Stallman's avatar
Richard M. Stallman committed
173
/* Search path for bitmap files.  */
174

Richard M. Stallman's avatar
Richard M. Stallman committed
175 176
Lisp_Object Vx_bitmap_file_path;

Karl Heuer's avatar
Karl Heuer committed
177
/* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.  */
178

Karl Heuer's avatar
Karl Heuer committed
179 180
Lisp_Object Vx_pixel_size_width_font_regexp;

181
Lisp_Object Qnone;
182
Lisp_Object Qsuppress_icon;
Jim Blandy's avatar
Jim Blandy committed
183
Lisp_Object Qundefined_color;
184
Lisp_Object Qcenter;
185
Lisp_Object Qcompound_text, Qcancel_timer;
Jim Blandy's avatar
Jim Blandy committed
186

187
/* In dispnew.c */
188

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

191
/* The below are defined in frame.c.  */
192

193
#if GLYPH_DEBUG
194 195 196 197
int image_cache_refcount, dpyinfo_refcount;
#endif


Jim Blandy's avatar
Jim Blandy committed
198

Richard M. Stallman's avatar
Richard M. Stallman committed
199
/* Error if we are not connected to X.  */
200

Karl Heuer's avatar
Karl Heuer committed
201
void
Richard M. Stallman's avatar
Richard M. Stallman committed
202 203
check_x ()
{
204
  if (! x_in_use)
Richard M. Stallman's avatar
Richard M. Stallman committed
205 206 207
    error ("X windows are not in use or not initialized");
}

208 209
/* Nonzero if we can use mouse menus.
   You should not call this unless HAVE_MENUS is defined.  */
210 211

int
212
have_menus_p ()
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))
Gerd Moellmann's avatar
Gerd Moellmann committed
227
    frame = selected_frame;
228
  CHECK_LIVE_FRAME (frame);
Gerd Moellmann's avatar
Gerd Moellmann committed
229
  f = XFRAME (frame);
230
  if (! FRAME_X_P (f))
231
    error ("Non-X frame used");
232
  return f;
233 234
}

235 236 237 238
/* 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.  */

239
struct x_display_info *
240 241 242
check_x_display_info (frame)
     Lisp_Object frame;
{
243
  struct x_display_info *dpyinfo = NULL;
244

245 246
  if (NILP (frame))
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
247
      struct frame *sf = XFRAME (selected_frame);
248

Gerd Moellmann's avatar
Gerd Moellmann committed
249
      if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
250
	dpyinfo = FRAME_X_DISPLAY_INFO (sf);
251
      else if (x_display_list != 0)
252
	dpyinfo = x_display_list;
253 254 255 256
      else
	error ("X windows are not in use or not initialized");
    }
  else if (STRINGP (frame))
257
    dpyinfo = x_display_info_for_name (frame);
258 259
  else
    {
260
      FRAME_PTR f = check_x_frame (frame);
261
      dpyinfo = FRAME_X_DISPLAY_INFO (f);
262
    }
263 264

  return dpyinfo;
265
}
266

267

Jim Blandy's avatar
Jim Blandy committed
268 269
/* 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
270

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

Jim Blandy's avatar
Jim Blandy committed
273
struct frame *
274 275
x_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
Jim Blandy's avatar
Jim Blandy committed
276 277
     int wdesc;
{
Jim Blandy's avatar
Jim Blandy committed
278 279
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
280

281
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
Jim Blandy's avatar
Jim Blandy committed
282
    {
283
      frame = XCAR (tail);
284
      if (!GC_FRAMEP (frame))
Jim Blandy's avatar
Jim Blandy committed
285
        continue;
Jim Blandy's avatar
Jim Blandy committed
286
      f = XFRAME (frame);
287
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
288
	continue;
289
      if (f->output_data.x->hourglass_window == wdesc)
290
	return f;
291
#ifdef USE_X_TOOLKIT
292
      if ((f->output_data.x->edit_widget
293
	   && XtWindow (f->output_data.x->edit_widget) == wdesc)
294 295 296
	  /* A tooltip frame?  */
	  || (!f->output_data.x->edit_widget
	      && FRAME_X_WINDOW (f) == wdesc)
297
          || f->output_data.x->icon_desc == wdesc)
298 299
        return f;
#else /* not USE_X_TOOLKIT */
Jan Djärv's avatar
Jan Djärv committed
300 301 302 303 304 305 306 307 308
#ifdef USE_GTK
      if (f->output_data.x->edit_widget)
      {
        GtkWidget *gwdesc = xg_win_to_widget (wdesc);
        struct x_output *x = f->output_data.x;
        if (gwdesc != 0 && gwdesc == x->edit_widget)
          return f;
      }
#endif /* USE_GTK */
309
      if (FRAME_X_WINDOW (f) == wdesc
310
          || f->output_data.x->icon_desc == wdesc)
Jim Blandy's avatar
Jim Blandy committed
311
        return f;
312 313 314 315 316
#endif /* not USE_X_TOOLKIT */
    }
  return 0;
}

Jan Djärv's avatar
Jan Djärv committed
317
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
318 319 320 321
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
322 323
x_any_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
324 325 326
     int wdesc;
{
  Lisp_Object tail, frame;
327
  struct frame *f, *found;
328
  struct x_output *x;
329

330 331
  found = NULL;
  for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
332
    {
333
      frame = XCAR (tail);
334
      if (!GC_FRAMEP (frame))
335
        continue;
336

337
      f = XFRAME (frame);
338
      if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
339
	{
340 341
	  /* This frame matches if the window is any of its widgets.  */
	  x = f->output_data.x;
342
	  if (x->hourglass_window == wdesc)
343 344 345
	    found = f;
	  else if (x->widget)
	    {
Jan Djärv's avatar
Jan Djärv committed
346 347 348 349 350 351 352 353 354
#ifdef USE_GTK
              GtkWidget *gwdesc = xg_win_to_widget (wdesc);
              if (gwdesc != 0
                  && (gwdesc == x->widget
                      || gwdesc == x->edit_widget
                      || gwdesc == x->vbox_widget
                      || gwdesc == x->menubar_widget))
                found = f;
#else
355 356
	      if (wdesc == XtWindow (x->widget)
		  || wdesc == XtWindow (x->column_widget)
357 358 359 360 361
		  || wdesc == XtWindow (x->edit_widget))
		found = f;
	      /* Match if the window is this frame's menubar.  */
	      else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
		found = f;
Jan Djärv's avatar
Jan Djärv committed
362
#endif
363 364 365 366
	    }
	  else if (FRAME_X_WINDOW (f) == wdesc)
	    /* A tooltip frame.  */
	    found = f;
367
	}
Jim Blandy's avatar
Jim Blandy committed
368
    }
369

370
  return found;
Jim Blandy's avatar
Jim Blandy committed
371
}
372

373 374 375 376 377 378 379 380 381
/* Likewise, but exclude the menu bar widget.  */

struct frame *
x_non_menubar_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
382
  struct x_output *x;
383

384
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
385
    {
386
      frame = XCAR (tail);
387 388 389
      if (!GC_FRAMEP (frame))
        continue;
      f = XFRAME (frame);
390
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
391
	continue;
392
      x = f->output_data.x;
393
      /* This frame matches if the window is any of its widgets.  */
394
      if (x->hourglass_window == wdesc)
395 396
	return f;
      else if (x->widget)
397
	{
Jan Djärv's avatar
Jan Djärv committed
398 399 400 401 402 403 404 405
#ifdef USE_GTK
          GtkWidget *gwdesc = xg_win_to_widget (wdesc);
          if (gwdesc != 0
              && (gwdesc == x->widget
                  || gwdesc == x->edit_widget
                  || gwdesc == x->vbox_widget))
            return f;
#else
406 407
	  if (wdesc == XtWindow (x->widget)
	      || wdesc == XtWindow (x->column_widget)
408 409
	      || wdesc == XtWindow (x->edit_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
410
#endif
411 412 413
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* A tooltip frame.  */
414 415 416 417 418
	return f;
    }
  return 0;
}

419 420 421 422 423 424 425 426 427
/* Likewise, but consider only the menu bar widget.  */

struct frame *
x_menubar_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
428
  struct x_output *x;
429

430
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
431
    {
432
      frame = XCAR (tail);
433 434 435
      if (!GC_FRAMEP (frame))
        continue;
      f = XFRAME (frame);
436
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
437
	continue;
438
      x = f->output_data.x;
439
      /* Match if the window is this frame's menubar.  */
Jan Djärv's avatar
Jan Djärv committed
440 441 442 443 444
#ifdef USE_GTK
      if (x->menubar_widget)
        {
          GtkWidget *gwdesc = xg_win_to_widget (wdesc);
          int found = 0;
445

Jan Djärv's avatar
Jan Djärv committed
446 447 448 449 450 451 452 453 454
          BLOCK_INPUT;
          if (gwdesc != 0
              && (gwdesc == x->menubar_widget
                  || gtk_widget_get_parent (gwdesc) == x->menubar_widget))
            found = 1;
          UNBLOCK_INPUT;
          if (found) return f;
        }
#else
455 456
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
457
	return f;
Jan Djärv's avatar
Jan Djärv committed
458
#endif
459 460 461 462
    }
  return 0;
}

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

struct frame *
467 468
x_top_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
469 470 471 472
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
473
  struct x_output *x;
474

475
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
476
    {
477
      frame = XCAR (tail);
478
      if (!GC_FRAMEP (frame))
479 480
        continue;
      f = XFRAME (frame);
481
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
482
	continue;
483
      x = f->output_data.x;
484 485 486 487

      if (x->widget)
	{
	  /* This frame matches if the window is its topmost widget.  */
Jan Djärv's avatar
Jan Djärv committed
488 489 490 491 492
#ifdef USE_GTK
          GtkWidget *gwdesc = xg_win_to_widget (wdesc);
          if (gwdesc == x->widget)
            return f;
#else
493 494
	  if (wdesc == XtWindow (x->widget))
	    return f;
495 496 497
#if 0 /* I don't know why it did this,
	 but it seems logically wrong,
	 and it causes trouble for MapNotify events.  */
498
	  /* Match if the window is this frame's menubar.  */
499
	  if (x->menubar_widget
500 501
	      && wdesc == XtWindow (x->menubar_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
502
#endif
503
#endif
504 505 506 507
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* Tooltip frame.  */
	return f;
508 509 510
    }
  return 0;
}
Jan Djärv's avatar
Jan Djärv committed
511
#endif /* USE_X_TOOLKIT || USE_GTK */
Jim Blandy's avatar
Jim Blandy committed
512

513 514 515 516 517 518 519 520 521 522 523


/* 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
524
   data more than once will not be caught.  */
525 526


Richard M. Stallman's avatar
Richard M. Stallman committed
527 528 529 530 531 532 533
/* Functions to access the contents of a bitmap, given an id.  */

int
x_bitmap_height (f, id)
     FRAME_PTR f;
     int id;
{
534
  return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
Richard M. Stallman's avatar
Richard M. Stallman committed
535 536 537 538 539 540 541
}

int
x_bitmap_width (f, id)
     FRAME_PTR f;
     int id;
{
542
  return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
Richard M. Stallman's avatar
Richard M. Stallman committed
543 544 545 546 547 548 549
}

int
x_bitmap_pixmap (f, id)
     FRAME_PTR f;
     int id;
{
550
  return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
Richard M. Stallman's avatar
Richard M. Stallman committed
551 552 553
}


554 555 556
/* Allocate a new bitmap record.  Returns index of new record.  */

static int
557 558
x_allocate_bitmap_record (f)
     FRAME_PTR f;
559
{
560 561 562 563
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
  int i;

  if (dpyinfo->bitmaps == NULL)
564
    {
565 566 567 568
      dpyinfo->bitmaps_size = 10;
      dpyinfo->bitmaps
	= (struct x_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
      dpyinfo->bitmaps_last = 1;
569 570 571
      return 1;
    }

572 573
  if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
    return ++dpyinfo->bitmaps_last;
574

575 576 577
  for (i = 0; i < dpyinfo->bitmaps_size; ++i)
    if (dpyinfo->bitmaps[i].refcount == 0)
      return i + 1;
578

579 580 581 582 583
  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;
584 585 586 587 588
}

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

void
Richard M. Stallman's avatar
Richard M. Stallman committed
589 590
x_reference_bitmap (f, id)
     FRAME_PTR f;
591 592
     int id;
{
593
  ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
594 595 596 597 598 599 600 601 602 603
}

/* 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;
{
604
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
605 606 607
  Pixmap bitmap;
  int id;

608
  bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
609 610
				  bits, width, height);

611 612


613 614 615
  if (! bitmap)
    return -1;

616 617 618 619 620 621 622
  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;
623 624 625 626 627 628 629 630 631

  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
632
     Lisp_Object file;
633
{
634
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
635 636 637
  unsigned int width, height;
  Pixmap bitmap;
  int xhot, yhot, result, id;
Richard M. Stallman's avatar
Richard M. Stallman committed
638 639 640
  Lisp_Object found;
  int fd;
  char *filename;
641 642

  /* Look for an existing bitmap with the same name.  */
643
  for (id = 0; id < dpyinfo->bitmaps_last; ++id)
644
    {
645 646
      if (dpyinfo->bitmaps[id].refcount
	  && dpyinfo->bitmaps[id].file
647
	  && !strcmp (dpyinfo->bitmaps[id].file, (char *) SDATA (file)))
648
	{
649
	  ++dpyinfo->bitmaps[id].refcount;
650 651 652 653
	  return id + 1;
	}
    }

Richard M. Stallman's avatar
Richard M. Stallman committed
654
  /* Search bitmap-file-path for the file, if appropriate.  */
655
  fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil);
Richard M. Stallman's avatar
Richard M. Stallman committed
656 657
  if (fd < 0)
    return -1;
658
  emacs_close (fd);
Richard M. Stallman's avatar
Richard M. Stallman committed
659

660
  filename = (char *) SDATA (found);
Richard M. Stallman's avatar
Richard M. Stallman committed
661

662
  result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
Richard M. Stallman's avatar
Richard M. Stallman committed
663
			    filename, &width, &height, &bitmap, &xhot, &yhot);
664 665 666
  if (result != BitmapSuccess)
    return -1;

667 668 669
  id = x_allocate_bitmap_record (f);
  dpyinfo->bitmaps[id - 1].pixmap = bitmap;
  dpyinfo->bitmaps[id - 1].refcount = 1;
670
  dpyinfo->bitmaps[id - 1].file
671
    = (char *) xmalloc (SBYTES (file) + 1);
672 673 674
  dpyinfo->bitmaps[id - 1].depth = 1;
  dpyinfo->bitmaps[id - 1].height = height;
  dpyinfo->bitmaps[id - 1].width = width;
675
  strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file));
676 677 678 679 680 681

  return id;
}

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

682
void
Richard M. Stallman's avatar
Richard M. Stallman committed
683 684
x_destroy_bitmap (f, id)
     FRAME_PTR f;
685 686
     int id;
{
687 688
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);

689 690
  if (id > 0)
    {
691 692
      --dpyinfo->bitmaps[id - 1].refcount;
      if (dpyinfo->bitmaps[id - 1].refcount == 0)
693
	{
694
	  BLOCK_INPUT;
695 696
	  XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].pixmap);
	  if (dpyinfo->bitmaps[id - 1].file)
697
	    {
698
	      xfree (dpyinfo->bitmaps[id - 1].file);
699
	      dpyinfo->bitmaps[id - 1].file = NULL;
700
	    }
701
	  UNBLOCK_INPUT;
702 703 704 705
	}
    }
}

706
/* Free all the bitmaps for the display specified by DPYINFO.  */
707

708 709 710
static void
x_destroy_all_bitmaps (dpyinfo)
     struct x_display_info *dpyinfo;
711
{
712 713 714 715 716 717
  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)
718
	  xfree (dpyinfo->bitmaps[i].file);
719 720
      }
  dpyinfo->bitmaps_last = 0;
721
}
Jim Blandy's avatar
Jim Blandy committed
722 723


724 725
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
726
static void x_disable_image P_ ((struct frame *, struct image *));
727

728
void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
729
static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
730 731 732 733 734 735 736 737 738 739
void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
740
void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
741 742 743 744 745 746 747 748 749
void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
				      Lisp_Object));
void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
				      Lisp_Object));
static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
							     Lisp_Object,
							     Lisp_Object,
							     char *, char *,
							     int));
750 751
static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
				  Lisp_Object));
752 753 754 755 756 757 758
static void init_color_table P_ ((void));
static void free_color_table P_ ((void));
static unsigned long *colors_in_color_table P_ ((int *n));
static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));


Jim Blandy's avatar
Jim Blandy committed
759 760


761

762
/* Store the screen positions of frame F into XPTR and YPTR.
763 764 765 766 767 768 769 770
   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;
{
771 772 773 774
  int win_x, win_y, outer_x, outer_y;
  int real_x = 0, real_y = 0;
  int had_errors = 0;
  Window win = f->output_data.x->parent_desc;
775

776
  int count;
777

778 779 780
  BLOCK_INPUT;

  count = x_catch_errors (FRAME_X_DISPLAY (f));
781

782 783 784 785 786 787 788 789
  if (win == FRAME_X_DISPLAY_INFO (f)->root_window)
    win = FRAME_OUTER_WINDOW (f);

  /* This loop traverses up the containment tree until we hit the root
     window.  Window managers may intersect many windows between our window
     and the root window.  The window we find just before the root window
     should be the outer WM window. */
  for (;;)
790
    {
791 792 793
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
794
      int success;
795

796 797
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
798

799
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
800

801 802 803 804 805 806
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

807 808
      if (wm_window == rootw || had_errors)
        break;
809

810 811
      win = wm_window;
    }
812

813 814 815 816
  if (! had_errors)
    {
      int ign;
      Window child, rootw;
817

818 819 820 821 822 823 824 825 826 827 828 829 830 831
      /* Get the real coordinates for the WM window upper left corner */
      XGetGeometry (FRAME_X_DISPLAY (f), win,
                    &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);

      /* Translate real coordinates to coordinates relative to our
         window.  For our window, the upper left corner is 0, 0.
         Since the upper left corner of the WM window is outside
         our window, win_x and win_y will be negative:

         ------------------          ---> x
         |      title                |
         | -----------------         v y
         | |  our window
      */
832
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
833

834 835
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
836
                             FRAME_X_WINDOW (f),
837

838
			     /* From-position, to-position.  */
839
                             real_x, real_y, &win_x, &win_y,
840

841 842
			     /* Child of win.  */
			     &child);
843

844
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
845
	{
846 847
          outer_x = win_x;
          outer_y = win_y;
848
	}
849 850 851
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
852

853 854 855
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
856

857 858
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
859

860 861
                                 /* Child of win.  */
                                 &child);
862
	}
863

864 865
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
866

867
  x_uncatch_errors (FRAME_X_DISPLAY (f), count);
868

869 870 871
  UNBLOCK_INPUT;

  if (had_errors) return;
872

873 874 875 876 877
  f->x_pixels_diff = -win_x;
  f->y_pixels_diff = -win_y;

  FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
  FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
878 879 880

  *xptr = real_x;
  *yptr = real_y;
881 882
}

Jim Blandy's avatar
Jim Blandy committed
883

884

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901

/* Gamma-correct COLOR on frame F.  */

void
gamma_correct (f, color)
     struct frame *f;
     XColor *color;
{
  if (f->gamma)
    {
      color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
      color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
      color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
    }
}


902 903 904 905
/* Decide if color named COLOR_NAME is valid for use on frame F.  If
   so, return the RGB values in COLOR.  If ALLOC_P is non-zero,
   allocate the color.  Value is zero if COLOR_NAME is invalid, or
   no color could be allocated.  */
906

Jim Blandy's avatar
Jim Blandy committed
907
int
908 909 910 911 912
x_defined_color (f, color_name, color, alloc_p)
     struct frame *f;
     char *color_name;
     XColor *color;
     int alloc_p;
Jim Blandy's avatar
Jim Blandy committed
913
{
914 915 916
  int success_p;
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
917 918

  BLOCK_INPUT;
919 920 921
  success_p = XParseColor (dpy, cmap, color_name, color);
  if (success_p && alloc_p)
    success_p = x_alloc_nearest_color (f, cmap, color);
Jim Blandy's avatar
Jim Blandy committed
922 923
  UNBLOCK_INPUT;

924
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
925 926
}

927 928 929 930

/* Return the pixel color value for color COLOR_NAME on frame F.  If F
   is a monochrome frame, return MONO_COLOR regardless of what ARG says.
   Signal an error if color can't be allocated.  */
Jim Blandy's avatar
Jim Blandy committed
931 932

int
933
x_decode_color (f, color_name, mono_color)
934
     FRAME_PTR f;
935 936
     Lisp_Object color_name;
     int mono_color;
Jim Blandy's avatar
Jim Blandy committed
937
{
938
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
939

940
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
941

942 943 944
#if 0 /* Don't do this.  It's wrong when we're not using the default
	 colormap, it makes freeing difficult, and it's probably not
	 an important optimization.  */
945
  if (strcmp (SDATA (color_name), "black") == 0)
946
    return BLACK_PIX_DEFAULT (f);
947
  else if (strcmp (SDATA (color_name), "white") == 0)
948
    return WHITE_PIX_DEFAULT (f);
949
#endif
Jim Blandy's avatar
Jim Blandy committed
950

951
  /* Return MONO_COLOR for monochrome frames.  */
952
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
953
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
954

955
  /* x_defined_color is responsible for coping with failures
956
     by looking for a near-miss.  */
957
  if (x_defined_color (f, SDATA (color_name), &cdef, 1))
958 959
    return cdef.pixel;

960 961 962
  Fsignal (Qerror, Fcons (build_string ("Undefined color"),
			  Fcons (color_name, Qnil)));
  return 0;
Jim Blandy's avatar
Jim Blandy committed
963
}
964 965


Jim Blandy's avatar
Jim Blandy committed
966

Gerd Moellmann's avatar
Gerd Moellmann committed
967 968 969 970 971 972 973 974 975 976 977 978 979
/* Change the `wait-for-wm' frame parameter of frame F.  OLD_VALUE is
   the previous value of that parameter, NEW_VALUE is the new value.
   See also the comment of wait_for_wm in struct x_output.  */

static void
x_set_wait_for_wm (f, new_value, old_value)
     struct frame *f;
     Lisp_Object new_value, old_value;
{
  f->output_data.x->wait_for_wm = !NILP (new_value);
}


Jim Blandy's avatar
Jim Blandy committed
980
/* Functions called only from `x_set_frame_param'