xfns.c 180 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Functions for the X window system.
Glenn Morris's avatar
Glenn Morris committed
2 3 4 5

Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
  2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
  2011  Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
6 7 8

This file is part of GNU Emacs.

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

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
20
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
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>
25
#include <setjmp.h>
26
#include <ctype.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
27

28 29 30 31
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

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

34 35
#define XLIB_ILLEGAL_ACCESS

Jim Blandy's avatar
Jim Blandy committed
36 37
#include "lisp.h"
#include "xterm.h"
Jim Blandy's avatar
Jim Blandy committed
38
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
39 40
#include "window.h"
#include "buffer.h"
Kenichi Handa's avatar
Kenichi Handa committed
41
#include "intervals.h"
Jim Blandy's avatar
Jim Blandy committed
42
#include "dispextern.h"
Jim Blandy's avatar
Jim Blandy committed
43
#include "keyboard.h"
44
#include "blockinput.h"
45
#include <epaths.h>
Kenichi Handa's avatar
Kenichi Handa committed
46
#include "character.h"
Karl Heuer's avatar
Karl Heuer committed
47
#include "charset.h"
Kenichi Handa's avatar
Kenichi Handa committed
48
#include "coding.h"
Karl Heuer's avatar
Karl Heuer committed
49
#include "fontset.h"
50 51
#include "systime.h"
#include "termhooks.h"
52
#include "atimer.h"
53
#include "termchar.h"
Kenichi Handa's avatar
Kenichi Handa committed
54 55
#include "font.h"

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

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

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

68 69
#include "xsettings.h"

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

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

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

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

99 100 101 102
#ifdef USE_MOTIF
#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/FileSB.h>
Jan D's avatar
Jan D committed
103 104
#include <Xm/List.h>
#include <Xm/TextF.h>
105 106
#endif

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

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

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

118 119
extern LWLIB_ID widget_id_tick;

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

123 124 125 126 127 128 129 130
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

131 132
#endif /* USE_X_TOOLKIT */

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

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

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

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

151 152
/* Non-zero means prompt with the old GTK file selection dialog.  */

153
int x_gtk_use_old_file_dialog;
154

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

int x_gtk_show_hidden_files;

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

161
int x_gtk_file_dialog_help_text;
162

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

int x_gtk_whole_detached_tool_bar;

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

static int x_gtk_use_system_tooltips;

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

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

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

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

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

Lisp_Object Vx_window_horizontal_drag_shape;

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

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

190
/* Nonzero if using X.  */
191

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

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

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

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

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

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

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

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

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

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

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

int
230
have_menus_p (void)
231
{
232 233 234 235 236 237 238
  return x_in_use;
}

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

FRAME_PTR
239
check_x_frame (Lisp_Object frame)
240 241 242 243
{
  FRAME_PTR f;

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

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

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

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

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

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

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

  return dpyinfo;
291
}
292

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

531 532
  BLOCK_INPUT;

533
  x_catch_errors (dpy);
534

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

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

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

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

      XFree ((char *) tmp_children);

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

563 564
      win = wm_window;
    }
565

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

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

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

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

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

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

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

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

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

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

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

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

          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);

647
  x_uncatch_errors ();
648

649 650 651
  UNBLOCK_INPUT;

  if (had_errors) return;
652

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

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

Jim Blandy's avatar
Jim Blandy committed
663

664

665 666 667 668

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

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


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

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

  BLOCK_INPUT;
694 695 696 697 698
#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);
699 700
  if (success_p && alloc_p)
    success_p = x_alloc_nearest_color (f, cmap, color);
Jim Blandy's avatar
Jim Blandy committed
701 702
  UNBLOCK_INPUT;

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

706 707 708 709

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

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

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

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

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

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

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


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

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
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
}

767 768
#ifdef USE_GTK

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

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

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

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

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

  if (!pixbuf)
813
    return 0;
814

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

Gerd Moellmann's avatar
Gerd Moellmann committed
821

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

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

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