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
extern void free_frame_menubar ();
114
extern double atof ();
115

116 117 118 119 120 121 122 123
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

124 125
#endif /* USE_X_TOOLKIT */

Kenichi Handa's avatar
Kenichi Handa committed
126 127 128 129 130 131 132 133
#ifdef USE_GTK

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

Richard M. Stallman's avatar
Richard M. Stallman committed
134 135
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))

136 137 138 139 140 141
/* 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;
142
char *gray_bitmap_bits = gray_bits;
143

144 145
/* Non-zero means prompt with the old GTK file selection dialog.  */

146
int x_gtk_use_old_file_dialog;
147

148 149 150 151
/* If non-zero, by default show hidden files in the GTK file chooser.  */

int x_gtk_show_hidden_files;

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

154
int x_gtk_file_dialog_help_text;
155

156 157 158 159
/* 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
160
/* The background and shape of the mouse pointer, and shape when not
161
   over text or in the modeline.  */
162

Jim Blandy's avatar
Jim Blandy committed
163
Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
164
Lisp_Object Vx_hourglass_pointer_shape;
165

166
/* The shape when over mouse-sensitive text.  */
167

168
Lisp_Object Vx_sensitive_text_pointer_shape;
Jim Blandy's avatar
Jim Blandy committed
169

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

Lisp_Object Vx_window_horizontal_drag_shape;

175
/* Color of chars displayed in cursor box.  */
176

Jim Blandy's avatar
Jim Blandy committed
177 178
Lisp_Object Vx_cursor_fore_pixel;

179
/* Nonzero if using X.  */
180

181
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
182

183
/* Non nil if no window manager is in use.  */
184

Jim Blandy's avatar
Jim Blandy committed
185 186
Lisp_Object Vx_no_window_manager;

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

Karl Heuer's avatar
Karl Heuer committed
189 190
Lisp_Object Vx_pixel_size_width_font_regexp;

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

197
/* In dispnew.c */
198

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

201 202 203 204
/* In editfns.c */

extern Lisp_Object Vsystem_name;

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

207
extern Lisp_Object Vmenu_bar_mode, Vtool_bar_mode;
208
extern Lisp_Object Qtooltip;
209

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
222
check_x (void)
Richard M. Stallman's avatar
Richard M. Stallman committed
223
{
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 (void)
233
{
234 235 236 237 238 239 240
  return x_in_use;
}

/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
   and checking validity for X.  */

FRAME_PTR
241
check_x_frame (Lisp_Object frame)
242 243 244 245
{
  FRAME_PTR f;

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

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

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

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

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

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

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

  return dpyinfo;
293
}
294

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

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

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

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

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

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

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

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

358
  found = NULL;
359
  for (tail = Vframe_list; CONSP (tail) && !found; tail = XCDR (tail))
360
    {
361
      frame = XCAR (tail);
362
      if (!FRAMEP (frame))
363
        continue;
364

365
      f = XFRAME (frame);
366
      if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
367
	{
368 369
	  /* This frame matches if the window is any of its widgets.  */
	  x = f->output_data.x;
370
	  if (x->hourglass_window == wdesc)
371 372 373
	    found = f;
	  else if (x->widget)
	    {
Jan Djärv's avatar
Jan Djärv committed
374
#ifdef USE_GTK
Kenichi Handa's avatar
Kenichi Handa committed
375
              GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
376
              if (gwdesc != 0
377
                  && gtk_widget_get_toplevel (gwdesc) == x->widget)
Jan Djärv's avatar
Jan Djärv committed
378 379
                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
/* Likewise, but consider only the menu bar widget.  */

struct frame *
401
x_menubar_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
402 403 404
{
  Lisp_Object tail, frame;
  struct frame *f;
405
  struct x_output *x;
406

407 408
  if (wdesc == None) return 0;

409
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
410
    {
411
      frame = XCAR (tail);
412
      if (!FRAMEP (frame))
413 414
        continue;
      f = XFRAME (frame);
415
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
416
	continue;
417
      x = f->output_data.x;
418
      /* Match if the window is this frame's menubar.  */
Jan Djärv's avatar
Jan Djärv committed
419 420 421
#ifdef USE_GTK
      if (x->menubar_widget)
        {
Kenichi Handa's avatar
Kenichi Handa committed
422
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
423

424 425
	  /* This gives false positives, but the rectangle check in xterm.c
	     where this is called takes care of that.  */
Jan Djärv's avatar
Jan Djärv committed
426 427
          if (gwdesc != 0
              && (gwdesc == x->menubar_widget
428 429 430
                  || gtk_widget_is_ancestor (x->menubar_widget, gwdesc)
		  || gtk_widget_is_ancestor (gwdesc, x->menubar_widget)))
            return f;
Jan Djärv's avatar
Jan Djärv committed
431 432
        }
#else
433 434
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
435
	return f;
Jan Djärv's avatar
Jan Djärv committed
436
#endif
437 438 439 440
    }
  return 0;
}

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

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

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

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

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

491 492


493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
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,
                                                         char *, char *,
                                                         int);
Jim Blandy's avatar
Jim Blandy committed
520

521

522
/* Store the screen positions of frame F into XPTR and YPTR.
523 524 525 526
   These are the positions of the containing window manager window,
   not Emacs's own window.  */

void
527
x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
528
{
529 530 531 532
  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;
533

534 535
  BLOCK_INPUT;

536
  x_catch_errors (FRAME_X_DISPLAY (f));
537

538 539 540 541 542 543 544 545
  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 (;;)
546
    {
547 548 549
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
550
      int success;
551

552 553
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
554

555
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
556

557 558 559 560 561 562
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

563 564
      if (wm_window == rootw || had_errors)
        break;
565

566 567
      win = wm_window;
    }
568

569 570
  if (! had_errors)
    {
571
      unsigned int ign;
572
      Window child, rootw;
573

574 575 576 577 578 579 580 581 582 583 584 585 586 587
      /* 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
      */
588
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
589

590 591
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
592
                             FRAME_X_WINDOW (f),
593

594
			     /* From-position, to-position.  */
595
                             real_x, real_y, &win_x, &win_y,
596

597 598
			     /* Child of win.  */
			     &child);
599

600
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
601
	{
602 603
          outer_x = win_x;
          outer_y = win_y;
604
	}
605 606 607
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
608

609 610 611
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
612

613 614
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
615

616 617
                                 /* Child of win.  */
                                 &child);
618
	}
619

620 621
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
622

623
  x_uncatch_errors ();
624

625 626 627
  UNBLOCK_INPUT;

  if (had_errors) return;
628

629 630 631 632 633
  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;
634 635 636

  *xptr = real_x;
  *yptr = real_y;
637 638
}

Jim Blandy's avatar
Jim Blandy committed
639

640

641 642 643 644

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

void
645
gamma_correct (struct frame *f, XColor *color)
646 647 648 649 650 651 652 653 654 655
{
  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;
    }
}


656 657 658 659
/* 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.  */
660

Jim Blandy's avatar
Jim Blandy committed
661
int
662
x_defined_color (struct frame *f, char *color_name, XColor *color, int alloc_p)
Jim Blandy's avatar
Jim Blandy committed
663
{
664 665 666
  int success_p;
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
667 668

  BLOCK_INPUT;
669 670 671
  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
672 673
  UNBLOCK_INPUT;

674
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
675 676
}

677 678 679 680

/* 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
681 682

int
683
x_decode_color (FRAME_PTR f, Lisp_Object color_name, int mono_color)
Jim Blandy's avatar
Jim Blandy committed
684
{
685
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
686

687
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
688

689 690 691
#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.  */
692
  if (strcmp (SDATA (color_name), "black") == 0)
693
    return BLACK_PIX_DEFAULT (f);
694
  else if (strcmp (SDATA (color_name), "white") == 0)
695
    return WHITE_PIX_DEFAULT (f);
696
#endif
Jim Blandy's avatar
Jim Blandy committed
697

698
  /* Return MONO_COLOR for monochrome frames.  */
699
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
700
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
701

702
  /* x_defined_color is responsible for coping with failures
703
     by looking for a near-miss.  */
704
  if (x_defined_color (f, SDATA (color_name), &cdef, 1))
705 706
    return cdef.pixel;

707
  signal_error ("Undefined color", color_name);
Jim Blandy's avatar
Jim Blandy committed
708
}
709 710


Jim Blandy's avatar
Jim Blandy committed
711

Gerd Moellmann's avatar
Gerd Moellmann committed
712 713 714 715 716
/* 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
717
x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
Gerd Moellmann's avatar
Gerd Moellmann committed
718 719 720 721
{
  f->output_data.x->wait_for_wm = !NILP (new_value);
}

722 723
#ifdef USE_GTK

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

int
728
xg_set_icon (FRAME_PTR f, Lisp_Object file)
729
{
730 731
  int result = 0;
  Lisp_Object found;
732

733 734 735 736 737 738
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
739
      char *filename = (char *) SDATA (found);
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
      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;
759
}
760 761

int
762
xg_set_icon_from_xpm_data (FRAME_PTR f, char **data)
763 764
{
  int result = 0;
765
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) data);
766 767

  if (!pixbuf)
768
    return 0;
769

770
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
771 772 773
  g_object_unref (pixbuf);
  return 1;
}
774 775
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
776

Jim Blandy's avatar
Jim Blandy committed
777
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
778 779
   to set individual parameters.

780
   If FRAME_X_WINDOW (f) is 0,