xfns.c 171 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Functions for the X window system.
Stefan Monnier's avatar
Stefan Monnier committed
2
   Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3
                 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
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
Lute Kamstra's avatar
Lute Kamstra committed
19 20
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
Jim Blandy's avatar
Jim Blandy committed
21

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

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

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

32 33
#define XLIB_ILLEGAL_ACCESS

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

128 129 130 131 132 133 134 135
#ifdef USE_GTK

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

Richard M. Stallman's avatar
Richard M. Stallman committed
136 137 138 139 140 141
#ifdef HAVE_X11R4
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
#else
#define MAXREQUEST(dpy) ((dpy)->max_request_size)
#endif

142 143 144 145 146 147
/* 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;
148
char *gray_bitmap_bits = gray_bits;
149

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

152
int display_hourglass_p;
153

154 155 156 157
/* Non-zero means prompt with the old GTK file selection dialog.  */

int x_use_old_gtk_file_dialog;

158 159 160 161
/* If non-zero, by default show hidden files in the GTK file chooser.  */

int x_gtk_show_hidden_files;

162 163 164 165
/* If non-zero, don't collapse to tool bar when it is detached.  */

int x_gtk_whole_detached_tool_bar;

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

Jim Blandy's avatar
Jim Blandy committed
169
Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
170
Lisp_Object Vx_hourglass_pointer_shape;
171

172
/* The shape when over mouse-sensitive text.  */
173

174
Lisp_Object Vx_sensitive_text_pointer_shape;
Jim Blandy's avatar
Jim Blandy committed
175

176 177 178 179 180
/* If non-nil, the pointer shape to indicate that windows can be
   dragged horizontally.  */

Lisp_Object Vx_window_horizontal_drag_shape;

181
/* Color of chars displayed in cursor box.  */
182

Jim Blandy's avatar
Jim Blandy committed
183 184
Lisp_Object Vx_cursor_fore_pixel;

185
/* Nonzero if using X.  */
186

187
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
188

189
/* Non nil if no window manager is in use.  */
190

Jim Blandy's avatar
Jim Blandy committed
191 192
Lisp_Object Vx_no_window_manager;

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

Karl Heuer's avatar
Karl Heuer committed
195 196
Lisp_Object Vx_pixel_size_width_font_regexp;

197
Lisp_Object Qnone;
198
Lisp_Object Qsuppress_icon;
Jim Blandy's avatar
Jim Blandy committed
199
Lisp_Object Qundefined_color;
200
Lisp_Object Qcompound_text, Qcancel_timer;
Jim Blandy's avatar
Jim Blandy committed
201

202
/* In dispnew.c */
203

Jim Blandy's avatar
Jim Blandy committed
204 205
extern Lisp_Object Vwindow_system_version;

206
/* The below are defined in frame.c.  */
207

208
#if GLYPH_DEBUG
209 210 211 212
int image_cache_refcount, dpyinfo_refcount;
#endif


Jim Blandy's avatar
Jim Blandy committed
213

Richard M. Stallman's avatar
Richard M. Stallman committed
214
/* Error if we are not connected to X.  */
215

Karl Heuer's avatar
Karl Heuer committed
216
void
Richard M. Stallman's avatar
Richard M. Stallman committed
217 218
check_x ()
{
219
  if (! x_in_use)
Richard M. Stallman's avatar
Richard M. Stallman committed
220 221 222
    error ("X windows are not in use or not initialized");
}

223 224
/* Nonzero if we can use mouse menus.
   You should not call this unless HAVE_MENUS is defined.  */
225 226

int
227
have_menus_p ()
228
{
229 230 231 232 233 234 235 236 237 238 239 240 241
  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
242
    frame = selected_frame;
243
  CHECK_LIVE_FRAME (frame);
Gerd Moellmann's avatar
Gerd Moellmann committed
244
  f = XFRAME (frame);
245
  if (! FRAME_X_P (f))
246
    error ("Non-X frame used");
247
  return f;
248 249
}

250
/* Let the user specify an X display with a Lisp object.
251
   OBJECT may be nil, a frame or a terminal id.
252 253 254
   nil stands for the selected frame--or, if that is not an X frame,
   the first X display on the list.  */

255
struct x_display_info *
256 257
check_x_display_info (object)
     Lisp_Object object;
258
{
259
  struct x_display_info *dpyinfo = NULL;
260

261
  if (NILP (object))
262
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
263
      struct frame *sf = XFRAME (selected_frame);
264

Gerd Moellmann's avatar
Gerd Moellmann committed
265
      if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
266
	dpyinfo = FRAME_X_DISPLAY_INFO (sf);
267
      else if (x_display_list != 0)
268
	dpyinfo = x_display_list;
269 270 271
      else
	error ("X windows are not in use or not initialized");
    }
272
  else if (INTEGERP (object))
273
    {
274
      struct terminal *t = get_terminal (XINT (object), 1);
275

276 277
      if (t->type != output_x_window)
        error ("Terminal %d is not an X display", XINT (object));
278

279
      dpyinfo = t->display_info.x;
280
    }
281 282
  else if (STRINGP (object))
    dpyinfo = x_display_info_for_name (object);
283 284
  else
    {
285
      FRAME_PTR f = check_x_frame (object);
286
      dpyinfo = FRAME_X_DISPLAY_INFO (f);
287
    }
288 289

  return dpyinfo;
290
}
291

292

Jim Blandy's avatar
Jim Blandy committed
293 294
/* 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
295

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

Jim Blandy's avatar
Jim Blandy committed
298
struct frame *
299 300
x_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
Jim Blandy's avatar
Jim Blandy committed
301 302
     int wdesc;
{
Jim Blandy's avatar
Jim Blandy committed
303 304
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
305

306 307
  if (wdesc == None) return 0;

308
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
Jim Blandy's avatar
Jim Blandy committed
309
    {
310
      frame = XCAR (tail);
311
      if (!GC_FRAMEP (frame))
Jim Blandy's avatar
Jim Blandy committed
312
        continue;
Jim Blandy's avatar
Jim Blandy committed
313
      f = XFRAME (frame);
314
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
315
	continue;
316
      if (f->output_data.x->hourglass_window == wdesc)
317
	return f;
318
#ifdef USE_X_TOOLKIT
319
      if ((f->output_data.x->edit_widget
320
	   && XtWindow (f->output_data.x->edit_widget) == wdesc)
321 322 323
	  /* A tooltip frame?  */
	  || (!f->output_data.x->edit_widget
	      && FRAME_X_WINDOW (f) == wdesc)
324
          || f->output_data.x->icon_desc == wdesc)
325 326
        return f;
#else /* not USE_X_TOOLKIT */
Jan Djärv's avatar
Jan Djärv committed
327 328 329
#ifdef USE_GTK
      if (f->output_data.x->edit_widget)
      {
330
        GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
331 332 333 334 335
        struct x_output *x = f->output_data.x;
        if (gwdesc != 0 && gwdesc == x->edit_widget)
          return f;
      }
#endif /* USE_GTK */
336
      if (FRAME_X_WINDOW (f) == wdesc
337
          || f->output_data.x->icon_desc == wdesc)
Jim Blandy's avatar
Jim Blandy committed
338
        return f;
339 340 341 342 343
#endif /* not USE_X_TOOLKIT */
    }
  return 0;
}

Jan Djärv's avatar
Jan Djärv committed
344
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
345 346 347 348
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
349 350
x_any_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
351 352 353
     int wdesc;
{
  Lisp_Object tail, frame;
354
  struct frame *f, *found;
355
  struct x_output *x;
356

357 358
  if (wdesc == None) return NULL;

359 360
  found = NULL;
  for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
361
    {
362
      frame = XCAR (tail);
363
      if (!GC_FRAMEP (frame))
364
        continue;
365

366
      f = XFRAME (frame);
367
      if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
368
	{
369 370
	  /* This frame matches if the window is any of its widgets.  */
	  x = f->output_data.x;
371
	  if (x->hourglass_window == wdesc)
372 373 374
	    found = f;
	  else if (x->widget)
	    {
Jan Djärv's avatar
Jan Djärv committed
375
#ifdef USE_GTK
376
              GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
377 378 379 380 381 382 383
              if (gwdesc != 0
                  && (gwdesc == x->widget
                      || gwdesc == x->edit_widget
                      || gwdesc == x->vbox_widget
                      || gwdesc == x->menubar_widget))
                found = f;
#else
384 385
	      if (wdesc == XtWindow (x->widget)
		  || wdesc == XtWindow (x->column_widget)
386 387 388 389 390
		  || 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
391
#endif
392 393 394 395
	    }
	  else if (FRAME_X_WINDOW (f) == wdesc)
	    /* A tooltip frame.  */
	    found = f;
396
	}
Jim Blandy's avatar
Jim Blandy committed
397
    }
398

399
  return found;
Jim Blandy's avatar
Jim Blandy committed
400
}
401

402 403 404 405 406 407 408 409 410
/* 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;
411
  struct x_output *x;
412

413 414
  if (wdesc == None) return 0;

415
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
416
    {
417
      frame = XCAR (tail);
418 419 420
      if (!GC_FRAMEP (frame))
        continue;
      f = XFRAME (frame);
421
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
422
	continue;
423
      x = f->output_data.x;
424
      /* This frame matches if the window is any of its widgets.  */
425
      if (x->hourglass_window == wdesc)
426 427
	return f;
      else if (x->widget)
428
	{
Jan Djärv's avatar
Jan Djärv committed
429
#ifdef USE_GTK
430
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
431 432 433 434 435 436
          if (gwdesc != 0
              && (gwdesc == x->widget
                  || gwdesc == x->edit_widget
                  || gwdesc == x->vbox_widget))
            return f;
#else
437 438
	  if (wdesc == XtWindow (x->widget)
	      || wdesc == XtWindow (x->column_widget)
439 440
	      || wdesc == XtWindow (x->edit_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
441
#endif
442 443 444
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* A tooltip frame.  */
445 446 447 448 449
	return f;
    }
  return 0;
}

450 451 452 453 454 455 456 457 458
/* 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;
459
  struct x_output *x;
460

461 462
  if (wdesc == None) return 0;

463
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
464
    {
465
      frame = XCAR (tail);
466 467 468
      if (!GC_FRAMEP (frame))
        continue;
      f = XFRAME (frame);
469
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
470
	continue;
471
      x = f->output_data.x;
472
      /* Match if the window is this frame's menubar.  */
Jan Djärv's avatar
Jan Djärv committed
473 474 475
#ifdef USE_GTK
      if (x->menubar_widget)
        {
476
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
477
          int found = 0;
478

Jan Djärv's avatar
Jan Djärv committed
479 480 481 482 483 484 485 486 487
          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
488 489
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
490
	return f;
Jan Djärv's avatar
Jan Djärv committed
491
#endif
492 493 494 495
    }
  return 0;
}

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

struct frame *
500 501
x_top_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
502 503 504 505
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
506
  struct x_output *x;
507

508 509
  if (wdesc == None) return 0;

510
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
511
    {
512
      frame = XCAR (tail);
513
      if (!GC_FRAMEP (frame))
514 515
        continue;
      f = XFRAME (frame);
516
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
517
	continue;
518
      x = f->output_data.x;
519 520 521 522

      if (x->widget)
	{
	  /* This frame matches if the window is its topmost widget.  */
Jan Djärv's avatar
Jan Djärv committed
523
#ifdef USE_GTK
524
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
525 526 527
          if (gwdesc == x->widget)
            return f;
#else
528 529
	  if (wdesc == XtWindow (x->widget))
	    return f;
530 531 532
#if 0 /* I don't know why it did this,
	 but it seems logically wrong,
	 and it causes trouble for MapNotify events.  */
533
	  /* Match if the window is this frame's menubar.  */
534
	  if (x->menubar_widget
535 536
	      && wdesc == XtWindow (x->menubar_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
537
#endif
538
#endif
539 540 541 542
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* Tooltip frame.  */
	return f;
543 544 545
    }
  return 0;
}
Jan Djärv's avatar
Jan Djärv committed
546
#endif /* USE_X_TOOLKIT || USE_GTK */
Jim Blandy's avatar
Jim Blandy committed
547

548 549


550 551
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
552

553
void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
554
static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
555 556 557 558 559 560 561 562 563 564
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));
565
void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
566 567 568 569 570 571 572 573 574
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));
Jim Blandy's avatar
Jim Blandy committed
575

576

577
/* Store the screen positions of frame F into XPTR and YPTR.
578 579 580 581 582 583 584 585
   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;
{
586 587 588 589
  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;
590

591 592
  BLOCK_INPUT;

593
  x_catch_errors (FRAME_X_DISPLAY (f));
594

595 596 597 598 599 600 601 602
  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 (;;)
603
    {
604 605 606
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
607
      int success;
608

609 610
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
611

612
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
613

614 615 616 617 618 619
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

620 621
      if (wm_window == rootw || had_errors)
        break;
622

623 624
      win = wm_window;
    }
625

626 627
  if (! had_errors)
    {
628
      unsigned int ign;
629
      Window child, rootw;
630

631 632 633 634 635 636 637 638 639 640 641 642 643 644
      /* 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
      */
645
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
646

647 648
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
649
                             FRAME_X_WINDOW (f),
650

651
			     /* From-position, to-position.  */
652
                             real_x, real_y, &win_x, &win_y,
653

654 655
			     /* Child of win.  */
			     &child);
656

657
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
658
	{
659 660
          outer_x = win_x;
          outer_y = win_y;
661
	}
662 663 664
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
665

666 667 668
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
669

670 671
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
672

673 674
                                 /* Child of win.  */
                                 &child);
675
	}
676

677 678
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
679

680
  x_uncatch_errors ();
681

682 683 684
  UNBLOCK_INPUT;

  if (had_errors) return;
685

686 687 688 689 690
  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;
691 692 693

  *xptr = real_x;
  *yptr = real_y;
694 695
}

Jim Blandy's avatar
Jim Blandy committed
696

697

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714

/* 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;
    }
}


715 716 717 718
/* 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.  */
719

Jim Blandy's avatar
Jim Blandy committed
720
int
721 722 723 724 725
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
726
{
727 728 729
  int success_p;
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
730 731

  BLOCK_INPUT;
732 733 734
  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
735 736
  UNBLOCK_INPUT;

737
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
738 739
}

740 741 742 743

/* 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
744 745

int
746
x_decode_color (f, color_name, mono_color)
747
     FRAME_PTR f;
748 749
     Lisp_Object color_name;
     int mono_color;
Jim Blandy's avatar
Jim Blandy committed
750
{
751
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
752

753
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
754

755 756 757
#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.  */
758
  if (strcmp (SDATA (color_name), "black") == 0)
759
    return BLACK_PIX_DEFAULT (f);
760
  else if (strcmp (SDATA (color_name), "white") == 0)
761
    return WHITE_PIX_DEFAULT (f);
762
#endif
Jim Blandy's avatar
Jim Blandy committed
763

764
  /* Return MONO_COLOR for monochrome frames.  */
765
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
766
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
767

768
  /* x_defined_color is responsible for coping with failures
769
     by looking for a near-miss.  */
770
  if (x_defined_color (f, SDATA (color_name), &cdef, 1))
771 772
    return cdef.pixel;

773 774 775
  Fsignal (Qerror, Fcons (build_string ("Undefined color"),
			  Fcons (color_name, Qnil)));
  return 0;
Jim Blandy's avatar
Jim Blandy committed
776
}
777 778


Jim Blandy's avatar
Jim Blandy committed
779

Gerd Moellmann's avatar
Gerd Moellmann committed
780 781 782 783 784 785 786 787 788 789 790 791
/* 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);
}

792 793
#ifdef USE_GTK

794 795
/* Set icon from FILE for frame F.  By using GTK functions the icon
   may be any format that GdkPixbuf knows about, i.e. not just bitmaps.  */
796 797

int
798
xg_set_icon (f, file)
799
    FRAME_PTR f;
800 801
    Lisp_Object file;
{
802 803
  int result = 0;
  Lisp_Object found;
804

805 806 807 808 809 810
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
811
      char *filename = (char *) SDATA (found);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
      BLOCK_INPUT;

      pixbuf = gdk_pixbuf_new_from_file (filename, &err);

      if (pixbuf)
	{
	  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
			       pixbuf);
	  g_object_unref (pixbuf);

	  result = 1;
	}
      else
	g_error_free (err);

      UNBLOCK_INPUT;
    }

  return result;
831
}
832 833 834 835 836 837 838

int
xg_set_icon_from_xpm_data (f, data)
    FRAME_PTR f;
    char **data;
{
  int result = 0;
839
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) data);
840 841

  if (!pixbuf)
842
    return 0;
843

844
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
845 846 847
  g_object_unref (pixbuf);
  return 1;
}
848 849
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
850

Jim Blandy's avatar
Jim Blandy committed
851
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
852 853
   to set individual parameters.

854
   If FRAME_X_WINDOW (f) is 0,
Jim Blandy's avatar
Jim Blandy committed
855
   the frame is being created and its X-window does not exist yet.