xfns.c 173 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
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>
Richard M. Stallman's avatar
Richard M. Stallman committed
25

26 27 28 29
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

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

32 33
#define XLIB_ILLEGAL_ACCESS

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

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

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

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

66 67
#include "xsettings.h"

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

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

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

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

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

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

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

110 111
extern LWLIB_ID widget_id_tick;

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

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

120 121 122 123 124 125 126 127
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

128 129
#endif /* USE_X_TOOLKIT */

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

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

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

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

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

150
int x_gtk_use_old_file_dialog;
151

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

int x_gtk_show_hidden_files;

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

158
int x_gtk_file_dialog_help_text;
159

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

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

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

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

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

Lisp_Object Vx_window_horizontal_drag_shape;

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

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

183
/* Nonzero if using X.  */
184

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

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

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

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

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

195
Lisp_Object Qnone;
196
Lisp_Object Qsuppress_icon;
Jim Blandy's avatar
Jim Blandy committed
197
Lisp_Object Qundefined_color;
198
Lisp_Object Qcompound_text, Qcancel_timer;
199
static Lisp_Object Qfont_param;
Jim Blandy's avatar
Jim Blandy committed
200

201
/* In dispnew.c */
202

Jim Blandy's avatar
Jim Blandy committed
203 204
extern Lisp_Object Vwindow_system_version;

205
/* The below are defined in frame.c.  */
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
Richard M. Stallman's avatar
Richard M. Stallman committed
219 220
check_x ()
{
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 ()
230
{
231 232 233 234 235 236 237 238 239 240 241 242 243
  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
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 259
check_x_display_info (object)
     Lisp_Object object;
260
{
261
  struct x_display_info *dpyinfo = NULL;
262

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

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

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

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

  return dpyinfo;
292
}
293

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

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

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

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

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

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

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

359 360
  if (wdesc == None) return NULL;

361
  found = NULL;
362
  for (tail = Vframe_list; CONSP (tail) && !found; tail = XCDR (tail))
363
    {
364
      frame = XCAR (tail);
365
      if (!FRAMEP (frame))
366
        continue;
367

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

398
  return found;
Jim Blandy's avatar
Jim Blandy committed
399
}
400

401 402 403 404 405 406 407 408 409
/* Likewise, but consider only the menu bar widget.  */

struct frame *
x_menubar_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
410
  struct x_output *x;
411

412 413
  if (wdesc == None) return 0;

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

429 430
	  /* 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
431 432
          if (gwdesc != 0
              && (gwdesc == x->menubar_widget
433 434 435
                  || 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
436 437
        }
#else
438 439
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
440
	return f;
Jan Djärv's avatar
Jan Djärv committed
441
#endif
442 443 444 445
    }
  return 0;
}

446 447 448 449
/* Return the frame whose principal (outermost) window is WDESC.
   If WDESC is some other (smaller) window, we return 0.  */

struct frame *
450 451
x_top_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
452 453 454 455
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
456
  struct x_output *x;
457

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

460
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
461
    {
462
      frame = XCAR (tail);
463
      if (!FRAMEP (frame))
464 465
        continue;
      f = XFRAME (frame);
466
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
467
	continue;
468
      x = f->output_data.x;
469 470 471 472

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

498 499


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

502 503
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
504

505
void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
506
static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
507 508 509 510 511 512 513 514 515 516
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));
517
void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
518 519 520 521 522 523 524 525 526
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
527

528

529
/* Store the screen positions of frame F into XPTR and YPTR.
530 531 532 533 534 535 536 537
   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;
{
538 539 540 541
  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;
542

543 544
  BLOCK_INPUT;

545
  x_catch_errors (FRAME_X_DISPLAY (f));
546

547 548 549 550 551 552 553 554
  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 (;;)
555
    {
556 557 558
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
559
      int success;
560

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

564
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
565

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

      XFree ((char *) tmp_children);

572 573
      if (wm_window == rootw || had_errors)
        break;
574

575 576
      win = wm_window;
    }
577

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

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

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

603
			     /* From-position, to-position.  */
604
                             real_x, real_y, &win_x, &win_y,
605

606 607
			     /* Child of win.  */
			     &child);
608

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

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

622 623
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
624

625 626
                                 /* Child of win.  */
                                 &child);
627
	}
628

629 630
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
631

632
  x_uncatch_errors ();
633

634 635 636
  UNBLOCK_INPUT;

  if (had_errors) return;
637

638 639 640 641 642
  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;
643 644 645

  *xptr = real_x;
  *yptr = real_y;
646 647
}

Jim Blandy's avatar
Jim Blandy committed
648

649

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

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


667 668 669 670
/* 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.  */
671

Jim Blandy's avatar
Jim Blandy committed
672
int
673 674 675 676 677
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
678
{
679 680 681
  int success_p;
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
682 683

  BLOCK_INPUT;
684 685 686
  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
687 688
  UNBLOCK_INPUT;

689
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
690 691
}

692 693 694 695

/* 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
696 697

int
698
x_decode_color (f, color_name, mono_color)
699
     FRAME_PTR f;
700 701
     Lisp_Object color_name;
     int mono_color;
Jim Blandy's avatar
Jim Blandy committed
702
{
703
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
704

705
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
706

707 708 709
#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.  */
710
  if (strcmp (SDATA (color_name), "black") == 0)
711
    return BLACK_PIX_DEFAULT (f);
712
  else if (strcmp (SDATA (color_name), "white") == 0)
713
    return WHITE_PIX_DEFAULT (f);
714
#endif
Jim Blandy's avatar
Jim Blandy committed
715

716
  /* Return MONO_COLOR for monochrome frames.  */
717
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
718
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
719

720
  /* x_defined_color is responsible for coping with failures
721
     by looking for a near-miss.  */
722
  if (x_defined_color (f, SDATA (color_name), &cdef, 1))
723 724
    return cdef.pixel;

725
  signal_error ("Undefined color", color_name);
Jim Blandy's avatar
Jim Blandy committed
726
}
727 728


Jim Blandy's avatar
Jim Blandy committed
729

Gerd Moellmann's avatar
Gerd Moellmann committed
730 731 732 733 734 735 736 737 738 739 740 741
/* 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);
}

742 743
#ifdef USE_GTK

744 745
/* 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.  */
746 747

int
Kenichi Handa's avatar
Kenichi Handa committed
748
xg_set_icon (f, file)
749
    FRAME_PTR f;
750 751
    Lisp_Object file;
{
752 753
  int result = 0;
  Lisp_Object found;
754

755 756 757 758 759 760
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
761
      char *filename = (char *) SDATA (found);
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
      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;
781
}
782 783 784 785 786 787 788

int
xg_set_icon_from_xpm_data (f, data)
    FRAME_PTR f;
    char **data;
{
  int result = 0;
789
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) data);
790 791

  if (!pixbuf)
792
    return 0;
793

794
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
795 796 797
  g_object_unref (pixbuf);
  return 1;
}
798 799
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
800

Jim Blandy's avatar
Jim Blandy committed
801
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
802 803
   to set individual parameters.

804
   If FRAME_X_WINDOW (f) is 0,
Jim Blandy's avatar
Jim Blandy committed
805
   the frame is being created and its X-window does not exist yet.
Jim Blandy's avatar
Jim Blandy committed
806 807 808 809
   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
810 811
x_set_foreground_color (f, arg, oldval)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
812 813
     Lisp_Object arg, oldval;
{
814 815
  struct x_output *x = f->output_data.x;
  unsigned long fg, old_fg;
816

817
  fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
818 819
  old_fg = FRAME_FOREGROUND_PIXEL (f);
  FRAME_FOREGROUND_PIXEL (f) = fg;
820

821
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
822
    {
823
      Display *dpy = FRAME_X_DISPLAY (f);
824

825 826 827
      BLOCK_INPUT;
      XSetForeground (dpy, x->normal_gc, fg);
      XSetBackground (dpy, x->reverse_gc, fg);
828

829 830 831 832 833 834
      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);
	}
835

Jim Blandy's avatar
Jim Blandy committed
836
      UNBLOCK_INPUT;
837

838
      update_face_from_frame_parameter (f, Qforeground_color, arg);
839

840
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
841
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
842
    }
843

844
  unload_color (f, old_fg);
Jim Blandy's avatar
Jim Blandy committed
845 846 847
}

void
Jim Blandy's avatar
Jim Blandy committed
848 849
x_set_background_color (f, arg, oldval)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
850 851
     Lisp_Object arg, oldval;
{
852 853
  struct x_output *x = f->output_data.x;
  unsigned long bg;
Jim Blandy's avatar
Jim Blandy committed
854

855
  bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
856 857
  unload_color (f, FRAME_BACKGROUND_PIXEL (f));
  FRAME_BACKGROUND_PIXEL (f) = bg;
858

859
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
860
    {
861
      Display *dpy = FRAME_X_DISPLAY (f);
862

863 864 865 866 867 868
      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
869 870 871 872
#ifdef USE_GTK
      xg_set_background_color (f, bg);
#endif

873 874 875 876 877 878 879 880
#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)
	  {
881
	    Window window = XSCROLL_BAR (bar)->x_window;