xfns.c 170 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 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;

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

Jim Blandy's avatar
Jim Blandy committed
165
Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
166
Lisp_Object Vx_hourglass_pointer_shape;
167

168
/* The shape when over mouse-sensitive text.  */
169

170
Lisp_Object Vx_sensitive_text_pointer_shape;
Jim Blandy's avatar
Jim Blandy committed
171

172 173 174 175 176
/* If non-nil, the pointer shape to indicate that windows can be
   dragged horizontally.  */

Lisp_Object Vx_window_horizontal_drag_shape;

177
/* Color of chars displayed in cursor box.  */
178

Jim Blandy's avatar
Jim Blandy committed
179 180
Lisp_Object Vx_cursor_fore_pixel;

181
/* Nonzero if using X.  */
182

183
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
184

185
/* Non nil if no window manager is in use.  */
186

Jim Blandy's avatar
Jim Blandy committed
187 188
Lisp_Object Vx_no_window_manager;

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

Karl Heuer's avatar
Karl Heuer committed
191 192
Lisp_Object Vx_pixel_size_width_font_regexp;

193
Lisp_Object Qnone;
194
Lisp_Object Qsuppress_icon;
Jim Blandy's avatar
Jim Blandy committed
195
Lisp_Object Qundefined_color;
196
Lisp_Object Qcompound_text, Qcancel_timer;
Jim Blandy's avatar
Jim Blandy committed
197

198
/* In dispnew.c */
199

Jim Blandy's avatar
Jim Blandy committed
200 201
extern Lisp_Object Vwindow_system_version;

202
/* The below are defined in frame.c.  */
203

204
#if GLYPH_DEBUG
205 206 207 208
int image_cache_refcount, dpyinfo_refcount;
#endif


Jim Blandy's avatar
Jim Blandy committed
209

Richard M. Stallman's avatar
Richard M. Stallman committed
210
/* Error if we are not connected to X.  */
211

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

219 220
/* Nonzero if we can use mouse menus.
   You should not call this unless HAVE_MENUS is defined.  */
221 222

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

246 247
/* Let the user specify an X display with a Lisp object.
   OBJECT may be nil, a frame or a device id.
248 249 250
   nil stands for the selected frame--or, if that is not an X frame,
   the first X display on the list.  */

251
struct x_display_info *
252 253
check_x_display_info (object)
     Lisp_Object object;
254
{
255
  struct x_display_info *dpyinfo = NULL;
256

257
  if (NILP (object))
258
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
259
      struct frame *sf = XFRAME (selected_frame);
260

Gerd Moellmann's avatar
Gerd Moellmann committed
261
      if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
262
	dpyinfo = FRAME_X_DISPLAY_INFO (sf);
263
      else if (x_display_list != 0)
264
	dpyinfo = x_display_list;
265 266 267
      else
	error ("X windows are not in use or not initialized");
    }
268
  else if (INTEGERP (object))
269
    {
270
      struct device *d = get_device (XINT (object), 1);
271 272

      if (d->type != output_x_window)
273
        error ("Display %d is not an X display", XINT (object));
274 275 276

      dpyinfo = d->display_info.x;
    }
277 278
  else if (STRINGP (object))
    dpyinfo = x_display_info_for_name (object);
279 280
  else
    {
281
      FRAME_PTR f = check_x_frame (object);
282
      dpyinfo = FRAME_X_DISPLAY_INFO (f);
283
    }
284 285

  return dpyinfo;
286
}
287

288

Jim Blandy's avatar
Jim Blandy committed
289 290
/* 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
291

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

Jim Blandy's avatar
Jim Blandy committed
294
struct frame *
295 296
x_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
Jim Blandy's avatar
Jim Blandy committed
297 298
     int wdesc;
{
Jim Blandy's avatar
Jim Blandy committed
299 300
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
301

302 303
  if (wdesc == None) return 0;

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

Jan Djärv's avatar
Jan Djärv committed
340
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
341 342 343 344
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
345 346
x_any_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
347 348 349
     int wdesc;
{
  Lisp_Object tail, frame;
350
  struct frame *f, *found;
351
  struct x_output *x;
352

353 354
  if (wdesc == None) return NULL;

355 356
  found = NULL;
  for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
357
    {
358
      frame = XCAR (tail);
359
      if (!GC_FRAMEP (frame))
360
        continue;
361

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

395
  return found;
Jim Blandy's avatar
Jim Blandy committed
396
}
397

398 399 400 401 402 403 404 405 406
/* 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;
407
  struct x_output *x;
408

409 410
  if (wdesc == None) return 0;

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

446 447 448 449 450 451 452 453 454
/* 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;
455
  struct x_output *x;
456

457 458
  if (wdesc == None) return 0;

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

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

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

struct frame *
496 497
x_top_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
498 499 500 501
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
502
  struct x_output *x;
503

504 505
  if (wdesc == None) return 0;

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

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

544 545


546 547
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
548

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

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

587
  int count;
588

589 590 591
  BLOCK_INPUT;

  count = x_catch_errors (FRAME_X_DISPLAY (f));
592

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

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

610
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
611

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

      XFree ((char *) tmp_children);

618 619
      if (wm_window == rootw || had_errors)
        break;
620

621 622
      win = wm_window;
    }
623

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

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

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

649
			     /* From-position, to-position.  */
650
                             real_x, real_y, &win_x, &win_y,
651

652 653
			     /* Child of win.  */
			     &child);
654

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

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

668 669
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
670

671 672
                                 /* Child of win.  */
                                 &child);
673
	}
674

675 676
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
677

678
  x_uncatch_errors (FRAME_X_DISPLAY (f), count);
679

680 681 682
  UNBLOCK_INPUT;

  if (had_errors) return;
683

684 685 686 687 688
  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;
689 690 691

  *xptr = real_x;
  *yptr = real_y;
692 693
}

Jim Blandy's avatar
Jim Blandy committed
694

695

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

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


713 714 715 716
/* 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.  */
717

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

  BLOCK_INPUT;
730 731 732
  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
733 734
  UNBLOCK_INPUT;

735
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
736 737
}

738 739 740 741

/* 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
742 743

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

751
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
752

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

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

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

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


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

790 791
#ifdef USE_GTK

792 793
/* 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.  */
794 795

int
796
xg_set_icon (f, file)
797
    FRAME_PTR f;
798 799
    Lisp_Object file;
{
800 801 802
  struct gcpro gcpro1;
  int result = 0;
  Lisp_Object found;
803

804
  GCPRO1 (found);
805

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

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
812
      char *filename = (char *) SDATA (found);
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
      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;
    }

  UNGCPRO;
  return result;
833
}
834 835 836 837 838 839 840

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

  if (!pixbuf)
844
    return 0;
845

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

Gerd Moellmann's avatar
Gerd Moellmann committed
852

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

856
   If FRAME_X_WINDOW (f) is 0,
Jim Blandy's avatar
Jim Blandy committed
857
   the frame is being created and its X-window does not exist yet.
Jim Blandy's avatar
Jim Blandy committed
858 859 860 861
   In that case, just record the parameter's new value
   in the standard place; do not attempt to change the window.  */

void
Jim Blandy's avatar
Jim Blandy committed
862 863
x_set_foreground_color (f, arg, oldval)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
864 865
     Lisp_Object arg, oldval;
{
866 867
  struct x_output *x = f->output_data.x;
  unsigned long fg, old_fg;
868

869 870 871
  fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
  old_fg = x->foreground_pixel;
  x->foreground_pixel = fg;
872

873
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
874
    {
875
      Display *dpy = FRAME_X_DISPLAY (f);
876

877 878 879
      BLOCK_INPUT;
      XSetForeground (dpy, x->normal_gc, fg);
      XSetBackground (dpy, x->reverse_gc, fg);
880

881 882 883 884 885 886
      if (x->cursor_pixel == old_fg)
	{
	  unload_color (f, x->cursor_pixel);
	  x->cursor_pixel = x_copy_color (f, fg);
	  XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
	}