xfns.c 176 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,
Glenn Morris's avatar
Glenn Morris committed
3
                 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
Glenn Morris's avatar
Glenn Morris committed
4
                 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6 7

This file is part of GNU Emacs.

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

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
19
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
20

Richard M. Stallman's avatar
Richard M. Stallman committed
21
#include <config.h>
22
#include <stdio.h>
23
#include <math.h>
24
#include <setjmp.h>
25
#include <ctype.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>
Kenichi Handa's avatar
Kenichi Handa committed
45
#include "character.h"
Karl Heuer's avatar
Karl Heuer committed
46
#include "charset.h"
Kenichi Handa's avatar
Kenichi Handa committed
47
#include "coding.h"
Karl Heuer's avatar
Karl Heuer committed
48
#include "fontset.h"
49 50
#include "systime.h"
#include "termhooks.h"
51
#include "atimer.h"
52
#include "termchar.h"
Kenichi Handa's avatar
Kenichi Handa committed
53 54
#include "font.h"

Jim Blandy's avatar
Jim Blandy committed
55
#ifdef HAVE_X_WINDOWS
56 57

#include <ctype.h>
58 59
#include <sys/types.h>
#include <sys/stat.h>
Jim Blandy's avatar
Jim Blandy committed
60

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

67 68
#include "xsettings.h"

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

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

76
#ifndef USE_MOTIF
Chong Yidong's avatar
Chong Yidong committed
77 78
#ifdef HAVE_XAW3D
#include <X11/Xaw3d/Paned.h>
Chong Yidong's avatar
Chong Yidong committed
79
#include <X11/Xaw3d/Label.h>
Chong Yidong's avatar
Chong Yidong committed
80
#else /* !HAVE_XAW3D */
81 82
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Label.h>
Chong Yidong's avatar
Chong Yidong committed
83
#endif /* HAVE_XAW3D */
84
#endif /* USE_MOTIF */
85 86 87 88 89 90 91 92 93 94 95 96 97

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

98 99 100 101 102 103
#ifdef USE_MOTIF
#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/FileSB.h>
#endif

Dan Nicolaescu's avatar
Dan Nicolaescu committed
104
#if !defined(NO_EDITRES)
105
#define HACK_EDITRES
106
extern void _XEditResCheckMessages ();
Dan Nicolaescu's avatar
Dan Nicolaescu committed
107
#endif /* not defined NO_EDITRES */
108

109 110
/* Unique id counter for widgets created by the Lucid Widget Library.  */

111 112
extern LWLIB_ID widget_id_tick;

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

118
extern void free_frame_menubar ();
119
extern double atof ();
120

121 122 123 124 125 126 127 128
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

129 130
#endif /* USE_X_TOOLKIT */

Kenichi Handa's avatar
Kenichi Handa committed
131 132 133 134 135 136 137 138
#ifdef USE_GTK

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

Richard M. Stallman's avatar
Richard M. Stallman committed
139 140
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))

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

149 150
/* Non-zero means prompt with the old GTK file selection dialog.  */

151
int x_gtk_use_old_file_dialog;
152

153 154 155 156
/* If non-zero, by default show hidden files in the GTK file chooser.  */

int x_gtk_show_hidden_files;

157 158
/* If non-zero, don't show additional help text in the GTK file chooser.  */

159
int x_gtk_file_dialog_help_text;
160

161 162 163 164
/* 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
165
/* The background and shape of the mouse pointer, and shape when not
166
   over text or in the modeline.  */
167

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

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

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

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

Lisp_Object Vx_window_horizontal_drag_shape;

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

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

184
/* Nonzero if using X.  */
185

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

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

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

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

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

196
Lisp_Object Qnone;
197
Lisp_Object Qsuppress_icon;
Jim Blandy's avatar
Jim Blandy committed
198
Lisp_Object Qundefined_color;
199
Lisp_Object Qcompound_text, Qcancel_timer;
200
Lisp_Object Qfont_param;
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 209
extern Lisp_Object Qtooltip;

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

214 215 216
#if defined (USE_GTK) && defined (HAVE_FREETYPE)
char *x_last_font_name;
#endif
217

Jim Blandy's avatar
Jim Blandy committed
218

Richard M. Stallman's avatar
Richard M. Stallman committed
219
/* Error if we are not connected to X.  */
220

Karl Heuer's avatar
Karl Heuer committed
221
void
Richard M. Stallman's avatar
Richard M. Stallman committed
222 223
check_x ()
{
224
  if (! x_in_use)
Richard M. Stallman's avatar
Richard M. Stallman committed
225 226 227
    error ("X windows are not in use or not initialized");
}

228 229
/* Nonzero if we can use mouse menus.
   You should not call this unless HAVE_MENUS is defined.  */
230 231

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

255
/* Let the user specify an X display with a Lisp object.
256
   OBJECT may be nil, a frame or a terminal object.
257 258 259
   nil stands for the selected frame--or, if that is not an X frame,
   the first X display on the list.  */

260
struct x_display_info *
261 262
check_x_display_info (object)
     Lisp_Object object;
263
{
264
  struct x_display_info *dpyinfo = NULL;
265

266
  if (NILP (object))
267
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
268
      struct frame *sf = XFRAME (selected_frame);
269

Gerd Moellmann's avatar
Gerd Moellmann committed
270
      if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
271
	dpyinfo = FRAME_X_DISPLAY_INFO (sf);
272
      else if (x_display_list != 0)
273
	dpyinfo = x_display_list;
274 275 276
      else
	error ("X windows are not in use or not initialized");
    }
277
  else if (TERMINALP (object))
278
    {
279
      struct terminal *t = get_terminal (object, 1);
280

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

284
      dpyinfo = t->display_info.x;
285
    }
286 287
  else if (STRINGP (object))
    dpyinfo = x_display_info_for_name (object);
288 289
  else
    {
290
      FRAME_PTR f = check_x_frame (object);
291
      dpyinfo = FRAME_X_DISPLAY_INFO (f);
292
    }
293 294

  return dpyinfo;
295
}
296

297

Jim Blandy's avatar
Jim Blandy committed
298 299
/* 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
300

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

Jim Blandy's avatar
Jim Blandy committed
303
struct frame *
304 305
x_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
Jim Blandy's avatar
Jim Blandy committed
306 307
     int wdesc;
{
Jim Blandy's avatar
Jim Blandy committed
308 309
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
310

311 312
  if (wdesc == None) return 0;

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

Jan Djärv's avatar
Jan Djärv committed
349
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
350 351 352 353
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
354 355
x_any_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
356 357 358
     int wdesc;
{
  Lisp_Object tail, frame;
359
  struct frame *f, *found;
360
  struct x_output *x;
361

362 363
  if (wdesc == None) return NULL;

364
  found = NULL;
365
  for (tail = Vframe_list; CONSP (tail) && !found; tail = XCDR (tail))
366
    {
367
      frame = XCAR (tail);
368
      if (!FRAMEP (frame))
369
        continue;
370

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

401
  return found;
Jim Blandy's avatar
Jim Blandy committed
402
}
403

404 405 406
/* Likewise, but consider only the menu bar widget.  */

struct frame *
Jan D's avatar
Jan D committed
407
x_menubar_window_to_frame (dpyinfo, event)
408
     struct x_display_info *dpyinfo;
Jan D's avatar
Jan D committed
409
     XEvent *event;
410
{
Jan D's avatar
Jan D committed
411
  Window wdesc = event->xany.window;
412 413
  Lisp_Object tail, frame;
  struct frame *f;
414
  struct x_output *x;
415

416 417
  if (wdesc == None) return 0;

418
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
419
    {
420
      frame = XCAR (tail);
421
      if (!FRAMEP (frame))
422 423
        continue;
      f = XFRAME (frame);
424
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
425
	continue;
426
      x = f->output_data.x;
Jan Djärv's avatar
Jan Djärv committed
427
#ifdef USE_GTK
Jan D's avatar
Jan D committed
428 429
      if (x->menubar_widget && xg_event_is_for_menubar (f, event))
        return f;
Jan Djärv's avatar
Jan Djärv committed
430
#else
Jan D's avatar
Jan D committed
431
      /* Match if the window is this frame's menubar.  */
432 433
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
434
	return f;
Jan Djärv's avatar
Jan Djärv committed
435
#endif
436 437 438 439
    }
  return 0;
}

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

struct frame *
444 445
x_top_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
446 447 448 449
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
450
  struct x_output *x;
451

452 453
  if (wdesc == None) return 0;

454
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
455
    {
456
      frame = XCAR (tail);
457
      if (!FRAMEP (frame))
458 459
        continue;
      f = XFRAME (frame);
460
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
461
	continue;
462
      x = f->output_data.x;
463 464 465 466

      if (x->widget)
	{
	  /* This frame matches if the window is its topmost widget.  */
Jan Djärv's avatar
Jan Djärv committed
467
#ifdef USE_GTK
Kenichi Handa's avatar
Kenichi Handa committed
468
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
469 470 471
          if (gwdesc == x->widget)
            return f;
#else
472 473
	  if (wdesc == XtWindow (x->widget))
	    return f;
474 475 476
#if 0 /* I don't know why it did this,
	 but it seems logically wrong,
	 and it causes trouble for MapNotify events.  */
477
	  /* Match if the window is this frame's menubar.  */
478
	  if (x->menubar_widget
479 480
	      && wdesc == XtWindow (x->menubar_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
481
#endif
482
#endif
483 484 485 486
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* Tooltip frame.  */
	return f;
487 488 489
    }
  return 0;
}
Jan Djärv's avatar
Jan Djärv committed
490
#endif /* USE_X_TOOLKIT || USE_GTK */
Jim Blandy's avatar
Jim Blandy committed
491

492 493


Kenichi Handa's avatar
Kenichi Handa committed
494 495
static void x_default_font_parameter P_ ((struct frame *, Lisp_Object));

496 497
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
498

499
void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
500
static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
501 502 503 504 505 506 507 508 509 510
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));
511
void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
512 513 514 515 516 517 518 519 520
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
521

522

523
/* Store the screen positions of frame F into XPTR and YPTR.
524 525 526 527 528 529 530 531
   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;
{
532 533 534
  int win_x, win_y, outer_x, outer_y;
  int real_x = 0, real_y = 0;
  int had_errors = 0;
535
  Window win = f->output_data.x->parent_desc;
536 537 538 539 540 541 542 543
  Atom actual_type;
  unsigned long actual_size, bytes_remaining;
  int i, rc, actual_format;
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
  long max_len = 400;
  Display *dpy = FRAME_X_DISPLAY (f);
  unsigned char *tmp_data = NULL;
  Atom target_type = XA_CARDINAL;
544

545 546
  BLOCK_INPUT;

547
  x_catch_errors (dpy);
548

549
  if (win == dpyinfo->root_window)
550 551 552 553 554 555 556
    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 (;;)
557
    {
558 559 560
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
561
      int success;
562

563 564
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
565

566
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
567

568 569 570 571 572 573
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

574 575
      if (wm_window == rootw || had_errors)
        break;
576

577 578
      win = wm_window;
    }
579

580 581
  if (! had_errors)
    {
582
      unsigned int ign;
583
      Window child, rootw;
584

585 586 587 588 589 590 591 592 593 594 595 596 597 598
      /* 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
      */
599
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
600

601 602
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
603
                             FRAME_X_WINDOW (f),
604

605
			     /* From-position, to-position.  */
606
                             real_x, real_y, &win_x, &win_y,
607

608 609
			     /* Child of win.  */
			     &child);
610

611
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
612
	{
613 614
          outer_x = win_x;
          outer_y = win_y;
615
	}
616 617 618
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
619

620 621 622
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
623

624 625
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
626

627 628
                                 /* Child of win.  */
                                 &child);
629
	}
630

631 632
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
633

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

  if (dpyinfo->root_window == f->output_data.x->parent_desc)
    {
      /* Try _NET_FRAME_EXTENTS if our parent is the root window.  */
      rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
                               0, max_len, False, target_type,
                               &actual_type, &actual_format, &actual_size,
                               &bytes_remaining, &tmp_data);

      if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
          && actual_size == 4 && actual_format == 32)
        {
          int ign;
          Window rootw;

          XGetGeometry (FRAME_X_DISPLAY (f), win,
                        &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
          long *fe = (long *)tmp_data;
      
          outer_x = -fe[0];
          outer_y = -fe[2];
          real_x -= fe[0];
          real_y -= fe[2];
        }
    }

  if (tmp_data) XFree (tmp_data);

662
  x_uncatch_errors ();
663

664 665 666
  UNBLOCK_INPUT;

  if (had_errors) return;
667

668 669 670 671 672
  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;
673 674 675

  *xptr = real_x;
  *yptr = real_y;
676 677
}

Jim Blandy's avatar
Jim Blandy committed
678

679

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

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


697 698 699 700
/* 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.  */
701

Jim Blandy's avatar
Jim Blandy committed
702
int
703 704 705 706 707
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
708
{
709 710 711
  int success_p;
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
712 713

  BLOCK_INPUT;
714 715 716
  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
717 718
  UNBLOCK_INPUT;

719
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
720 721
}

722 723 724 725

/* 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
726 727

int
728
x_decode_color (f, color_name, mono_color)
729
     FRAME_PTR f;
730 731
     Lisp_Object color_name;
     int mono_color;
Jim Blandy's avatar
Jim Blandy committed
732
{
733
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
734

735
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
736

737 738 739
#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.  */
740
  if (strcmp (SDATA (color_name), "black") == 0)
741
    return BLACK_PIX_DEFAULT (f);
742
  else if (strcmp (SDATA (color_name), "white") == 0)
743
    return WHITE_PIX_DEFAULT (f);
744
#endif
Jim Blandy's avatar
Jim Blandy committed
745

746
  /* Return MONO_COLOR for monochrome frames.  */
747
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
748
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
749

750
  /* x_defined_color is responsible for coping with failures
751
     by looking for a near-miss.  */
752
  if (x_defined_color (f, SDATA (color_name), &cdef, 1))
753 754
    return cdef.pixel;

755
  signal_error ("Undefined color", color_name);
Jim Blandy's avatar
Jim Blandy committed
756
}
757 758


Jim Blandy's avatar
Jim Blandy committed
759

Gerd Moellmann's avatar
Gerd Moellmann committed
760 761 762 763 764 765 766 767 768 769 770 771
/* 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);
}

772 773
#ifdef USE_GTK

774 775
/* 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.  */
776 777

int
Kenichi Handa's avatar
Kenichi Handa committed
778
xg_set_icon (f, file)
779
    FRAME_PTR f;
780 781
    Lisp_Object file;
{
782 783
  int result = 0;
  Lisp_Object found;
784

785 786 787 788 789 790
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
791
      char *filename = (char *) SDATA (found);
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
      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;
811
}
812 813 814 815 816 817 818

int
xg_set_icon_from_xpm_data (f, data)
    FRAME_PTR f;
    char **data;
{
  int result = 0;
819
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) data);
820 821

  if (!pixbuf)
822
    return 0;
823

824
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
825 826 827
  g_object_unref (pixbuf);
  return 1;
}
828 829
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
830

Jim Blandy's avatar
Jim Blandy committed
831
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
832 833
   to set individual parameters.

834
   If FRAME_X_WINDOW (f) is 0,
Jim Blandy's avatar
Jim Blandy committed
835
   the frame is being created and its X-window does not exist yet.
Jim Blandy's avatar
Jim Blandy committed
836 837 838 839
   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
840 841
x_set_foreground_color (f, arg, oldval)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
842 843
     Lisp_Object arg, oldval;
{
844 845
  struct x_output *x = f->output_data.x;
  unsigned long fg, old_fg;
846

847
  fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
848 849
  old_fg = FRAME_FOREGROUND_PIXEL (f);
  FRAME_FOREGROUND_PIXEL (f) = fg;
850

851
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
852
    {
853
      Display *dpy = FRAME_X_DISPLAY (f);
854

855 856 857
      BLOCK_INPUT;
      XSetForeground (dpy, x->normal_gc, fg);
      XSetBackground (dpy, x->reverse_gc, fg);
858

859 860 861 862 863 864
      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);
	}