xfns.c 291 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 557 558 559 560
int
x_bitmap_mask (f, id)
     FRAME_PTR f;
     int id;
{
  return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].mask;
}

Richard M. Stallman's avatar
Richard M. Stallman committed
561

562 563 564
/* Allocate a new bitmap record.  Returns index of new record.  */

static int
565 566
x_allocate_bitmap_record (f)
     FRAME_PTR f;
567
{
568 569 570 571
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
  int i;

  if (dpyinfo->bitmaps == NULL)
572
    {
573 574 575 576
      dpyinfo->bitmaps_size = 10;
      dpyinfo->bitmaps
	= (struct x_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
      dpyinfo->bitmaps_last = 1;
577 578 579
      return 1;
    }

580 581
  if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
    return ++dpyinfo->bitmaps_last;
582

583 584 585
  for (i = 0; i < dpyinfo->bitmaps_size; ++i)
    if (dpyinfo->bitmaps[i].refcount == 0)
      return i + 1;
586

587 588 589 590 591
  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;
592 593 594 595 596
}

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

void
Richard M. Stallman's avatar
Richard M. Stallman committed
597 598
x_reference_bitmap (f, id)
     FRAME_PTR f;
599 600
     int id;
{
601
  ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
602 603 604 605 606 607 608 609 610 611
}

/* 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;
{
612
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
613 614 615
  Pixmap bitmap;
  int id;

616
  bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
617 618
				  bits, width, height);

619 620


621 622 623
  if (! bitmap)
    return -1;

624 625
  id = x_allocate_bitmap_record (f);
  dpyinfo->bitmaps[id - 1].pixmap = bitmap;
626
  dpyinfo->bitmaps[id - 1].have_mask = 0;
627 628 629 630 631
  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;
632 633 634 635 636 637 638 639 640

  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
641
     Lisp_Object file;
642
{
643
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
644 645 646
  unsigned int width, height;
  Pixmap bitmap;
  int xhot, yhot, result, id;
Richard M. Stallman's avatar
Richard M. Stallman committed
647 648 649
  Lisp_Object found;
  int fd;
  char *filename;
650 651

  /* Look for an existing bitmap with the same name.  */
652
  for (id = 0; id < dpyinfo->bitmaps_last; ++id)
653
    {
654 655
      if (dpyinfo->bitmaps[id].refcount
	  && dpyinfo->bitmaps[id].file
656
	  && !strcmp (dpyinfo->bitmaps[id].file, (char *) SDATA (file)))
657
	{
658
	  ++dpyinfo->bitmaps[id].refcount;
659 660 661 662
	  return id + 1;
	}
    }

Richard M. Stallman's avatar
Richard M. Stallman committed
663
  /* Search bitmap-file-path for the file, if appropriate.  */
664
  fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil);
Richard M. Stallman's avatar
Richard M. Stallman committed
665 666
  if (fd < 0)
    return -1;
667
  emacs_close (fd);
Richard M. Stallman's avatar
Richard M. Stallman committed
668

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

671
  result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
Richard M. Stallman's avatar
Richard M. Stallman committed
672
			    filename, &width, &height, &bitmap, &xhot, &yhot);
673 674 675
  if (result != BitmapSuccess)
    return -1;

676 677
  id = x_allocate_bitmap_record (f);
  dpyinfo->bitmaps[id - 1].pixmap = bitmap;
678
  dpyinfo->bitmaps[id - 1].have_mask = 0;
679
  dpyinfo->bitmaps[id - 1].refcount = 1;
680
  dpyinfo->bitmaps[id - 1].file
681
    = (char *) xmalloc (SBYTES (file) + 1);
682 683 684
  dpyinfo->bitmaps[id - 1].depth = 1;
  dpyinfo->bitmaps[id - 1].height = height;
  dpyinfo->bitmaps[id - 1].width = width;
685
  strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file));
686 687 688 689 690 691

  return id;
}

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

692
void
Richard M. Stallman's avatar
Richard M. Stallman committed
693 694
x_destroy_bitmap (f, id)
     FRAME_PTR f;
695 696
     int id;
{
697 698
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);

699 700
  if (id > 0)
    {
701 702
      --dpyinfo->bitmaps[id - 1].refcount;
      if (dpyinfo->bitmaps[id - 1].refcount == 0)
703
	{
704
	  BLOCK_INPUT;
705
	  XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].pixmap);
706 707
	  if (dpyinfo->bitmaps[id - 1].have_mask)
	    XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].mask);
708
	  if (dpyinfo->bitmaps[id - 1].file)
709
	    {
710
	      xfree (dpyinfo->bitmaps[id - 1].file);
711
	      dpyinfo->bitmaps[id - 1].file = NULL;
712
	    }
713
	  UNBLOCK_INPUT;
714 715 716 717
	}
    }
}

718
/* Free all the bitmaps for the display specified by DPYINFO.  */
719

720 721 722
static void
x_destroy_all_bitmaps (dpyinfo)
     struct x_display_info *dpyinfo;
723
{
724 725 726 727 728
  int i;
  for (i = 0; i < dpyinfo->bitmaps_last; i++)
    if (dpyinfo->bitmaps[i].refcount > 0)
      {
	XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].pixmap);
729 730
	if (dpyinfo->bitmaps[i].have_mask)
	  XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].mask);
731
	if (dpyinfo->bitmaps[i].file)
732
	  xfree (dpyinfo->bitmaps[i].file);
733 734
      }
  dpyinfo->bitmaps_last = 0;
735
}
Jim Blandy's avatar
Jim Blandy committed
736 737


738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828


/* Useful functions defined in the section
   `Image type independent image structures' below. */

static unsigned long four_corners_best P_ ((XImage *ximg, unsigned long width,
					    unsigned long height));

static int x_create_x_image_and_pixmap P_ ((struct frame *f, int width, int height,
					    int depth, XImage **ximg,
					    Pixmap *pixmap));

static void x_destroy_x_image P_ ((XImage *ximg));


/* Create a mask of a bitmap. Note is this not a perfect mask.
   It's nicer with some borders in this context */

int
x_create_bitmap_mask(f, id)
     struct frame *f;
     int id;
{
  Pixmap pixmap, mask;
  XImage *ximg, *mask_img;
  unsigned long width, height;
  int result;
  unsigned long bg;
  unsigned long x, y, xp, xm, yp, ym;
  GC gc;

  int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);

  if (!(id > 0))
    return -1;

  pixmap = x_bitmap_pixmap(f, id);
  width = x_bitmap_width(f, id);
  height = x_bitmap_height(f, id);

  BLOCK_INPUT;
  ximg = XGetImage (FRAME_X_DISPLAY (f), pixmap, 0, 0, width, height,
		    ~0, ZPixmap);

  if (!ximg)
    {
      UNBLOCK_INPUT;
      return -1;
    }

  result = x_create_x_image_and_pixmap (f, width, height, 1, &mask_img, &mask);

  UNBLOCK_INPUT;
  if (!result)
    {
      XDestroyImage(ximg);
      return -1;
    }

  bg = four_corners_best (ximg, width, height);

  for (y = 0; y < ximg->height; ++y)
    {
      for (x = 0; x < ximg->width; ++x)
	{
	  xp = x != ximg->width - 1 ? x + 1 : 0;
	  xm = x != 0 ? x - 1 : ximg->width - 1;
	  yp = y != ximg->height - 1 ? y + 1 : 0;
	  ym = y != 0 ? y - 1 : ximg->height - 1;
	  if (XGetPixel (ximg, x, y) == bg
	      && XGetPixel (ximg, x, yp) == bg
	      && XGetPixel (ximg, x, ym) == bg
	      && XGetPixel (ximg, xp, y) == bg
	      && XGetPixel (ximg, xp, yp) == bg
	      && XGetPixel (ximg, xp, ym) == bg
	      && XGetPixel (ximg, xm, y) == bg
	      && XGetPixel (ximg, xm, yp) == bg
	      && XGetPixel (ximg, xm, ym) == bg)
	    XPutPixel (mask_img, x, y, 0);
	  else
	    XPutPixel (mask_img, x, y, 1);
	}
    }

  xassert (interrupt_input_blocked);
  gc = XCreateGC (FRAME_X_DISPLAY (f), mask, 0, NULL);
  XPutImage (FRAME_X_DISPLAY (f), mask, gc, mask_img, 0, 0, 0, 0,
	     width, height);
  XFreeGC (FRAME_X_DISPLAY (f), gc);

829
  dpyinfo->bitmaps[id - 1].have_mask = 1;
830 831 832 833 834 835 836 837
  dpyinfo->bitmaps[id - 1].mask = mask;

  XDestroyImage (ximg);
  x_destroy_x_image(mask_img);

  return 0;
}

838 839
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
840
static void x_disable_image P_ ((struct frame *, struct image *));
841

842
void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
843
static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
844 845 846 847 848 849 850 851 852 853
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));
854
void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
855 856 857 858 859 860 861 862 863
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));
864 865
static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
				  Lisp_Object));
866 867 868 869 870 871 872
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
873 874


875

876
/* Store the screen positions of frame F into XPTR and YPTR.
877 878 879 880 881 882 883 884
   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;
{
885 886 887 888
  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;
889

890
  int count;
891

892 893 894
  BLOCK_INPUT;

  count = x_catch_errors (FRAME_X_DISPLAY (f));
895

896 897 898 899 900 901 902 903
  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 (;;)
904
    {
905 906 907
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
908
      int success;
909

910 911
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
912

913
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
914

915 916 917 918 919 920
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

921 922
      if (wm_window == rootw || had_errors)
        break;
923

924 925
      win = wm_window;
    }
926

927 928 929 930
  if (! had_errors)
    {
      int ign;
      Window child, rootw;
931

932 933 934 935 936 937 938 939 940 941 942 943 944 945
      /* 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
      */
946
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
947

948 949
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
950
                             FRAME_X_WINDOW (f),
951

952
			     /* From-position, to-position.  */
953
                             real_x, real_y, &win_x, &win_y,
954

955 956
			     /* Child of win.  */
			     &child);
957

958
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
959
	{
960 961
          outer_x = win_x;
          outer_y = win_y;
962
	}
963 964 965
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
966

967 968 969
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
<