xfns.c 180 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, 2007, 2008, 2009, 2010, 2011
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
#ifdef USE_MOTIF
#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/FileSB.h>
Jan D's avatar
Jan D committed
102 103
#include <Xm/List.h>
#include <Xm/TextF.h>
104 105
#endif

106 107 108 109
#ifdef USE_LUCID
#include "../lwlib/xlwmenu.h"
#endif

Dan Nicolaescu's avatar
Dan Nicolaescu committed
110
#if !defined(NO_EDITRES)
111
#define HACK_EDITRES
112
extern void _XEditResCheckMessages ();
Dan Nicolaescu's avatar
Dan Nicolaescu committed
113
#endif /* not defined NO_EDITRES */
114

115 116
/* Unique id counter for widgets created by the Lucid Widget Library.  */

117 118
extern LWLIB_ID widget_id_tick;

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

122 123 124 125 126 127 128 129
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

130 131
#endif /* USE_X_TOOLKIT */

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

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

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

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 151
/* Non-zero means prompt with the old GTK file selection dialog.  */

152
int x_gtk_use_old_file_dialog;
153

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

int x_gtk_show_hidden_files;

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

160
int x_gtk_file_dialog_help_text;
161

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

int x_gtk_whole_detached_tool_bar;

166 167 168 169
/* If non-zero, use Gtk+ tooltips.  */

static int x_gtk_use_system_tooltips;

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

Jim Blandy's avatar
Jim Blandy committed
173
Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
174
Lisp_Object Vx_hourglass_pointer_shape;
175

176
/* The shape when over mouse-sensitive text.  */
177

178
Lisp_Object Vx_sensitive_text_pointer_shape;
Jim Blandy's avatar
Jim Blandy committed
179

180 181 182 183 184
/* If non-nil, the pointer shape to indicate that windows can be
   dragged horizontally.  */

Lisp_Object Vx_window_horizontal_drag_shape;

185
/* Color of chars displayed in cursor box.  */
186

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

189
/* Nonzero if using X.  */
190

191
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
192

193
/* Non nil if no window manager is in use.  */
194

Jim Blandy's avatar
Jim Blandy committed
195 196
Lisp_Object Vx_no_window_manager;

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

Karl Heuer's avatar
Karl Heuer committed
199 200
Lisp_Object Vx_pixel_size_width_font_regexp;

201
Lisp_Object Qnone;
202
Lisp_Object Qsuppress_icon;
Jim Blandy's avatar
Jim Blandy committed
203
Lisp_Object Qundefined_color;
204
Lisp_Object Qcompound_text, Qcancel_timer;
205
Lisp_Object Qfont_param;
Jim Blandy's avatar
Jim Blandy committed
206

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

211 212 213
#if defined (USE_GTK) && defined (HAVE_FREETYPE)
char *x_last_font_name;
#endif
214

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

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

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

int
229
have_menus_p (void)
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
238
check_x_frame (Lisp_Object frame)
239 240 241 242
{
  FRAME_PTR f;

  if (NILP (frame))
Gerd Moellmann's avatar
Gerd Moellmann committed
243
    frame = selected_frame;
244
  CHECK_LIVE_FRAME (frame);
Gerd Moellmann's avatar
Gerd Moellmann committed
245
  f = XFRAME (frame);
246
  if (! FRAME_X_P (f))
247
    error ("Non-X frame used");
248
  return f;
249 250
}

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

256
struct x_display_info *
257
check_x_display_info (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 (TERMINALP (object))
273
    {
274
      struct terminal *t = get_terminal (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
x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
Jim Blandy's avatar
Jim Blandy committed
300
{
Jim Blandy's avatar
Jim Blandy committed
301 302
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
303

304 305
  if (wdesc == None) return 0;

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

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

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

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

355
  found = NULL;
356
  for (tail = Vframe_list; CONSP (tail) && !found; tail = XCDR (tail))
357
    {
358
      frame = XCAR (tail);
359
      if (!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
Kenichi Handa's avatar
Kenichi Handa committed
372
              GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
373
              if (gwdesc != 0
374
                  && gtk_widget_get_toplevel (gwdesc) == x->widget)
Jan Djärv's avatar
Jan Djärv committed
375 376
                found = f;
#else
377 378
	      if (wdesc == XtWindow (x->widget)
		  || wdesc == XtWindow (x->column_widget)
379 380 381 382 383
		  || 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
384
#endif
385 386 387 388
	    }
	  else if (FRAME_X_WINDOW (f) == wdesc)
	    /* A tooltip frame.  */
	    found = f;
389
	}
Jim Blandy's avatar
Jim Blandy committed
390
    }
391

392
  return found;
Jim Blandy's avatar
Jim Blandy committed
393
}
394

395 396 397
/* Likewise, but consider only the menu bar widget.  */

struct frame *
398
x_menubar_window_to_frame (struct x_display_info *dpyinfo, XEvent *event)
399
{
Jan D's avatar
Jan D committed
400
  Window wdesc = event->xany.window;
401 402
  Lisp_Object tail, frame;
  struct frame *f;
403
  struct x_output *x;
404

405 406
  if (wdesc == None) return 0;

407
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
408
    {
409
      frame = XCAR (tail);
410
      if (!FRAMEP (frame))
411 412
        continue;
      f = XFRAME (frame);
413
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
414
	continue;
415
      x = f->output_data.x;
Jan Djärv's avatar
Jan Djärv committed
416
#ifdef USE_GTK
Jan D's avatar
Jan D committed
417 418
      if (x->menubar_widget && xg_event_is_for_menubar (f, event))
        return f;
Jan Djärv's avatar
Jan Djärv committed
419
#else
Jan D's avatar
Jan D committed
420
      /* Match if the window is this frame's menubar.  */
421 422
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
423
	return f;
Jan Djärv's avatar
Jan Djärv committed
424
#endif
425 426 427 428
    }
  return 0;
}

429 430 431 432
/* Return the frame whose principal (outermost) window is WDESC.
   If WDESC is some other (smaller) window, we return 0.  */

struct frame *
433
x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
434 435 436
{
  Lisp_Object tail, frame;
  struct frame *f;
437
  struct x_output *x;
438

439 440
  if (wdesc == None) return 0;

441
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
442
    {
443
      frame = XCAR (tail);
444
      if (!FRAMEP (frame))
445 446
        continue;
      f = XFRAME (frame);
447
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
448
	continue;
449
      x = f->output_data.x;
450 451 452 453

      if (x->widget)
	{
	  /* This frame matches if the window is its topmost widget.  */
Jan Djärv's avatar
Jan Djärv committed
454
#ifdef USE_GTK
Kenichi Handa's avatar
Kenichi Handa committed
455
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
456 457 458
          if (gwdesc == x->widget)
            return f;
#else
459 460
	  if (wdesc == XtWindow (x->widget))
	    return f;
461 462 463
#if 0 /* I don't know why it did this,
	 but it seems logically wrong,
	 and it causes trouble for MapNotify events.  */
464
	  /* Match if the window is this frame's menubar.  */
465
	  if (x->menubar_widget
466 467
	      && wdesc == XtWindow (x->menubar_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
468
#endif
469
#endif
470 471 472 473
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* Tooltip frame.  */
	return f;
474 475 476
    }
  return 0;
}
Jan Djärv's avatar
Jan Djärv committed
477
#endif /* USE_X_TOOLKIT || USE_GTK */
Jim Blandy's avatar
Jim Blandy committed
478

479 480


481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
static void x_default_font_parameter (struct frame *, Lisp_Object);

static Lisp_Object unwind_create_frame (Lisp_Object);
static Lisp_Object unwind_create_tip_frame (Lisp_Object);

void x_set_foreground_color (struct frame *, Lisp_Object, Lisp_Object);
static void x_set_wait_for_wm (struct frame *, Lisp_Object, Lisp_Object);
void x_set_background_color (struct frame *, Lisp_Object, Lisp_Object);
void x_set_mouse_color (struct frame *, Lisp_Object, Lisp_Object);
void x_set_cursor_color (struct frame *, Lisp_Object, Lisp_Object);
void x_set_border_color (struct frame *, Lisp_Object, Lisp_Object);
void x_set_cursor_type (struct frame *, Lisp_Object, Lisp_Object);
void x_set_icon_type (struct frame *, Lisp_Object, Lisp_Object);
void x_set_icon_name (struct frame *, Lisp_Object, Lisp_Object);
void x_explicitly_set_name (struct frame *, Lisp_Object, Lisp_Object);
void x_set_menu_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
void x_set_title (struct frame *, Lisp_Object, Lisp_Object);
void x_set_tool_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
void x_set_scroll_bar_foreground (struct frame *, Lisp_Object,
                                  Lisp_Object);
void x_set_scroll_bar_background (struct frame *, Lisp_Object,
                                  Lisp_Object);
static Lisp_Object x_default_scroll_bar_color_parameter (struct frame *,
                                                         Lisp_Object,
                                                         Lisp_Object,
506
                                                         const char *, const char *,
507
                                                         int);
Jim Blandy's avatar
Jim Blandy committed
508

509

510
/* Store the screen positions of frame F into XPTR and YPTR.
511 512 513 514
   These are the positions of the containing window manager window,
   not Emacs's own window.  */

void
515
x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
516
{
517 518 519 520
  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;
521 522 523 524 525 526 527 528
  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;
529

530 531
  BLOCK_INPUT;

532
  x_catch_errors (dpy);
533

534
  if (win == dpyinfo->root_window)
535 536 537 538 539 540 541
    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 (;;)
542
    {
543 544 545
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
546
      int success;
547

548 549
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
550

551
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
552

553 554 555 556 557 558
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

559 560
      if (wm_window == rootw || had_errors)
        break;
561

562 563
      win = wm_window;
    }
564

565 566
  if (! had_errors)
    {
567
      unsigned int ign;
568
      Window child, rootw;
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583
      /* 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
      */
584
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
585

586 587
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
588
                             FRAME_X_WINDOW (f),
589

590
			     /* From-position, to-position.  */
591
                             real_x, real_y, &win_x, &win_y,
592

593 594
			     /* Child of win.  */
			     &child);
595

596
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
597
	{
598 599
          outer_x = win_x;
          outer_y = win_y;
600
	}
601 602 603
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
604

605 606 607
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
608

609 610
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
611

612 613
                                 /* Child of win.  */
                                 &child);
614
	}
615

616 617
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
618

619 620 621 622 623 624 625 626 627 628 629 630 631 632

  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;
633
          long *fe = (long *)tmp_data;
634 635 636 637 638 639 640 641 642 643 644 645

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

  if (tmp_data) XFree (tmp_data);

646
  x_uncatch_errors ();
647

648 649 650
  UNBLOCK_INPUT;

  if (had_errors) return;
651

652 653 654 655 656
  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;
657 658 659

  *xptr = real_x;
  *yptr = real_y;
660 661
}

Jim Blandy's avatar
Jim Blandy committed
662

663

664 665 666 667

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

void
668
gamma_correct (struct frame *f, XColor *color)
669 670 671 672 673 674 675 676 677 678
{
  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;
    }
}


679 680 681 682
/* 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.  */
683

Jim Blandy's avatar
Jim Blandy committed
684
int
685 686
x_defined_color (struct frame *f, const char *color_name,
		 XColor *color, int alloc_p)
Jim Blandy's avatar
Jim Blandy committed
687
{
688
  int success_p = 0;
689 690
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
691 692

  BLOCK_INPUT;
693 694 695 696 697
#ifdef USE_GTK
  success_p = xg_check_special_colors (f, color_name, color);
#endif
  if (!success_p)
    success_p = XParseColor (dpy, cmap, color_name, color);
698 699
  if (success_p && alloc_p)
    success_p = x_alloc_nearest_color (f, cmap, color);
Jim Blandy's avatar
Jim Blandy committed
700 701
  UNBLOCK_INPUT;

702
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
703 704
}

705 706 707 708

/* 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
709 710

int
711
x_decode_color (FRAME_PTR f, Lisp_Object color_name, int mono_color)
Jim Blandy's avatar
Jim Blandy committed
712
{
713
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
714

715
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
716

717 718 719
#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.  */
720
  if (strcmp (SDATA (color_name), "black") == 0)
721
    return BLACK_PIX_DEFAULT (f);
722
  else if (strcmp (SDATA (color_name), "white") == 0)
723
    return WHITE_PIX_DEFAULT (f);
724
#endif
Jim Blandy's avatar
Jim Blandy committed
725

726
  /* Return MONO_COLOR for monochrome frames.  */
727
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
728
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
729

730
  /* x_defined_color is responsible for coping with failures
731
     by looking for a near-miss.  */
732
  if (x_defined_color (f, SDATA (color_name), &cdef, 1))
733 734
    return cdef.pixel;

735
  signal_error ("Undefined color", color_name);
Jim Blandy's avatar
Jim Blandy committed
736
}
737 738


Jim Blandy's avatar
Jim Blandy committed
739

Gerd Moellmann's avatar
Gerd Moellmann committed
740 741 742 743 744
/* 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
745
x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
Gerd Moellmann's avatar
Gerd Moellmann committed
746 747 748 749
{
  f->output_data.x->wait_for_wm = !NILP (new_value);
}

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
static void
x_set_tool_bar_position (struct frame *f,
                         Lisp_Object new_value,
                         Lisp_Object old_value)
{
  if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
      && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
    return;
  if (EQ (new_value, old_value)) return;

#ifdef USE_GTK
  if (xg_change_toolbar_position (f, new_value)) 
    f->tool_bar_position = new_value;
#endif
}

766 767
#ifdef USE_GTK

768 769
/* 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.  */
770 771

int
772
xg_set_icon (FRAME_PTR f, Lisp_Object file)
773
{
774 775
  int result = 0;
  Lisp_Object found;
776

777 778 779 780 781 782
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
783
      char *filename = (char *) SDATA (found);
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
      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;
803
}
804 805

int
806
xg_set_icon_from_xpm_data (FRAME_PTR f, const char **data)
807 808
{
  int result = 0;
809
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
810 811

  if (!pixbuf)
812
    return 0;
813

814
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
815 816 817
  g_object_unref (pixbuf);
  return 1;
}
818 819
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
820

Jim Blandy's avatar
Jim Blandy committed
821
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
822 823
   to set individual parameters.

824
   If FRAME_X_WINDOW (f) is 0,
Jim Blandy's avatar
Jim Blandy committed
825
   the frame is being created and its X-window does not exist yet.
Jim Blandy's avatar
Jim Blandy committed
826 827 828 829
   In that case, just record the parameter's new value
   in the standard place; do not attempt to change the window.  */

void
830
x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
831
{
832 833
  struct x_output *x = f->output_data.x;
  unsigned long fg, old_fg;
834

835
  fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
836 837
  old_fg = FRAME_FOREGROUND_PIXEL (f);
  FRAME_FOREGROUND_PIXEL (f) = fg;
838

839
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
840
    {
841
      Display *dpy = FRAME_X_DISPLAY (f);
842

843 844 845
      BLOCK_INPUT;
      XSetForeground (dpy, x->normal_gc, fg);
      XSetBackground (dpy, x->reverse_gc, fg);
846

847 848 849 850 851 852
      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);
	}
853

Jim Blandy's avatar
Jim Blandy committed
854
      UNBLOCK_INPUT;
855

856
      update_face_from_frame_parameter (f, Qforeground_color, arg);
857

858
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
859
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
860
    }
861

862
  unload_color (f, old_fg);
Jim Blandy's avatar
Jim Blandy committed
863 864 865
}

void
866
x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
867
{
868 869
  struct x_output *x = f->output_data.x;
  unsigned long bg;
Jim Blandy's avatar
Jim Blandy committed
870

871
  bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
872 873
  unload_color (f, FRAME_BACKGROUND_PIXEL (f));
  FRAME_BACKGROUND_PIXEL (f) = bg;
874

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

879 880 881 882 883 884
      BLOCK_INPUT;
      XSetBackground (dpy, x->normal_gc, bg);
      XSetForeground (dpy, x->reverse_gc, bg);
      XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
      XSetForeground (dpy, x->cursor_gc, bg);

Jan Djärv's avatar
Jan Djärv committed
885 886 887 888
#ifdef USE_GTK
      xg_set_background_color (f, bg);
#endif

889 890 891 892 893 894 895 896
#ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
				   toolkit scroll bars.  */
      {
	Lisp_Object bar;
	for (bar = FRAME_SCROLL_BARS (f);
	     !NILP (bar);
	     bar = XSCROLL_BAR (bar)->next)
	  {
897
	    Window window = XSCROLL_BAR (bar)->x_window;
898 899 900 901
	    XSetWindowBackground (dpy, window, bg);
	  }
      }
#endif /* USE_TOOLKIT_SCROLL_BARS */
Jim Blandy's avatar
Jim Blandy committed
902

903
      UNBLOCK_INPUT;
904
      update_face_from_frame_parameter (f, Qbackground_color, arg);
905

906
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
907
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
908 909 910
    }
}

911
static Cursor
912
make_invisible_cursor (struct frame *f)