xfns.c 169 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 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6 7 8

This file is part of GNU Emacs.

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

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
along with GNU Emacs; see the file COPYING.  If not, write to
Lute Kamstra's avatar
Lute Kamstra committed
19 20
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
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>
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>
Karl Heuer's avatar
Karl Heuer committed
44
#include "charset.h"
Kenichi Handa's avatar
Kenichi Handa committed
45
#include "coding.h"
Karl Heuer's avatar
Karl Heuer committed
46
#include "fontset.h"
47 48
#include "systime.h"
#include "termhooks.h"
49
#include "atimer.h"
Jim Blandy's avatar
Jim Blandy committed
50 51

#ifdef HAVE_X_WINDOWS
52 53

#include <ctype.h>
54 55
#include <sys/types.h>
#include <sys/stat.h>
Jim Blandy's avatar
Jim Blandy committed
56

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

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

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

74
#ifndef USE_MOTIF
75 76
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Label.h>
77
#endif /* USE_MOTIF */
78 79 80 81 82 83 84 85 86 87 88 89 90

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

91 92 93 94 95 96
#ifdef USE_MOTIF
#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/FileSB.h>
#endif

97 98
/* Do the EDITRES protocol if running X11R5
   Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
99

100
#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
101
#define HACK_EDITRES
102
extern void _XEditResCheckMessages ();
103 104
#endif /* R5 + Athena */

105 106
/* Unique id counter for widgets created by the Lucid Widget Library.  */

107 108
extern LWLIB_ID widget_id_tick;

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

114
extern void free_frame_menubar ();
115
extern double atof ();
116

117 118 119 120 121 122 123 124
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

125 126
#endif /* USE_X_TOOLKIT */

127 128 129 130 131 132 133 134
#ifdef USE_GTK

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

Richard M. Stallman's avatar
Richard M. Stallman committed
135 136 137 138 139 140
#ifdef HAVE_X11R4
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
#else
#define MAXREQUEST(dpy) ((dpy)->max_request_size)
#endif

141 142 143 144 145 146
/* The gray bitmap `bitmaps/gray'.  This is done because xterm.c uses
   it, and including `bitmaps/gray' more than once is a problem when
   config.h defines `static' as an empty replacement string.  */

int gray_bitmap_width = gray_width;
int gray_bitmap_height = gray_height;
147
char *gray_bitmap_bits = gray_bits;
148

149
/* Non-zero means we're allowed to display an hourglass cursor.  */
150

151
int display_hourglass_p;
152

153 154 155 156
/* Non-zero means prompt with the old GTK file selection dialog.  */

int x_use_old_gtk_file_dialog;

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

Jim Blandy's avatar
Jim Blandy committed
160
Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
161
Lisp_Object Vx_hourglass_pointer_shape;
162

163
/* The shape when over mouse-sensitive text.  */
164

165
Lisp_Object Vx_sensitive_text_pointer_shape;
Jim Blandy's avatar
Jim Blandy committed
166

167 168 169 170 171
/* If non-nil, the pointer shape to indicate that windows can be
   dragged horizontally.  */

Lisp_Object Vx_window_horizontal_drag_shape;

172
/* Color of chars displayed in cursor box.  */
173

Jim Blandy's avatar
Jim Blandy committed
174 175
Lisp_Object Vx_cursor_fore_pixel;

176
/* Nonzero if using X.  */
177

178
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
179

180
/* Non nil if no window manager is in use.  */
181

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

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

Karl Heuer's avatar
Karl Heuer committed
186 187
Lisp_Object Vx_pixel_size_width_font_regexp;

188
Lisp_Object Qnone;
189
Lisp_Object Qsuppress_icon;
Jim Blandy's avatar
Jim Blandy committed
190
Lisp_Object Qundefined_color;
191
Lisp_Object Qcompound_text, Qcancel_timer;
Jim Blandy's avatar
Jim Blandy committed
192

193
/* In dispnew.c */
194

Jim Blandy's avatar
Jim Blandy committed
195 196
extern Lisp_Object Vwindow_system_version;

197
/* The below are defined in frame.c.  */
198

199
#if GLYPH_DEBUG
200 201 202 203
int image_cache_refcount, dpyinfo_refcount;
#endif


Jim Blandy's avatar
Jim Blandy committed
204

Richard M. Stallman's avatar
Richard M. Stallman committed
205
/* Error if we are not connected to X.  */
206

Karl Heuer's avatar
Karl Heuer committed
207
void
Richard M. Stallman's avatar
Richard M. Stallman committed
208 209
check_x ()
{
210
  if (! x_in_use)
Richard M. Stallman's avatar
Richard M. Stallman committed
211 212 213
    error ("X windows are not in use or not initialized");
}

214 215
/* Nonzero if we can use mouse menus.
   You should not call this unless HAVE_MENUS is defined.  */
216 217

int
218
have_menus_p ()
219
{
220 221 222 223 224 225 226 227 228 229 230 231 232
  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
233
    frame = selected_frame;
234
  CHECK_LIVE_FRAME (frame);
Gerd Moellmann's avatar
Gerd Moellmann committed
235
  f = XFRAME (frame);
236
  if (! FRAME_X_P (f))
237
    error ("Non-X frame used");
238
  return f;
239 240
}

241 242 243 244
/* Let the user specify an X display with a frame.
   nil stands for the selected frame--or, if that is not an X frame,
   the first X display on the list.  */

245
struct x_display_info *
246 247 248
check_x_display_info (frame)
     Lisp_Object frame;
{
249
  struct x_display_info *dpyinfo = NULL;
250

251 252
  if (NILP (frame))
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
253
      struct frame *sf = XFRAME (selected_frame);
254

Gerd Moellmann's avatar
Gerd Moellmann committed
255
      if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
256
	dpyinfo = FRAME_X_DISPLAY_INFO (sf);
257
      else if (x_display_list != 0)
258
	dpyinfo = x_display_list;
259 260 261 262
      else
	error ("X windows are not in use or not initialized");
    }
  else if (STRINGP (frame))
263
    dpyinfo = x_display_info_for_name (frame);
264 265
  else
    {
266
      FRAME_PTR f = check_x_frame (frame);
267
      dpyinfo = FRAME_X_DISPLAY_INFO (f);
268
    }
269 270

  return dpyinfo;
271
}
272

273

Jim Blandy's avatar
Jim Blandy committed
274 275
/* 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
276

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

Jim Blandy's avatar
Jim Blandy committed
279
struct frame *
280 281
x_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
Jim Blandy's avatar
Jim Blandy committed
282 283
     int wdesc;
{
Jim Blandy's avatar
Jim Blandy committed
284 285
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
286

287 288
  if (wdesc == None) return 0;

289
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
Jim Blandy's avatar
Jim Blandy committed
290
    {
291
      frame = XCAR (tail);
292
      if (!GC_FRAMEP (frame))
Jim Blandy's avatar
Jim Blandy committed
293
        continue;
Jim Blandy's avatar
Jim Blandy committed
294
      f = XFRAME (frame);
295
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
296
	continue;
297
      if (f->output_data.x->hourglass_window == wdesc)
298
	return f;
299
#ifdef USE_X_TOOLKIT
300
      if ((f->output_data.x->edit_widget
301
	   && XtWindow (f->output_data.x->edit_widget) == wdesc)
302 303 304
	  /* A tooltip frame?  */
	  || (!f->output_data.x->edit_widget
	      && FRAME_X_WINDOW (f) == wdesc)
305
          || f->output_data.x->icon_desc == wdesc)
306 307
        return f;
#else /* not USE_X_TOOLKIT */
Jan Djärv's avatar
Jan Djärv committed
308 309 310
#ifdef USE_GTK
      if (f->output_data.x->edit_widget)
      {
311
        GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
312 313 314 315 316
        struct x_output *x = f->output_data.x;
        if (gwdesc != 0 && gwdesc == x->edit_widget)
          return f;
      }
#endif /* USE_GTK */
317
      if (FRAME_X_WINDOW (f) == wdesc
318
          || f->output_data.x->icon_desc == wdesc)
Jim Blandy's avatar
Jim Blandy committed
319
        return f;
320 321 322 323 324
#endif /* not USE_X_TOOLKIT */
    }
  return 0;
}

Jan Djärv's avatar
Jan Djärv committed
325
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
326 327 328 329
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
330 331
x_any_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
332 333 334
     int wdesc;
{
  Lisp_Object tail, frame;
335
  struct frame *f, *found;
336
  struct x_output *x;
337

338 339
  if (wdesc == None) return NULL;

340 341
  found = NULL;
  for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
342
    {
343
      frame = XCAR (tail);
344
      if (!GC_FRAMEP (frame))
345
        continue;
346

347
      f = XFRAME (frame);
348
      if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
349
	{
350 351
	  /* This frame matches if the window is any of its widgets.  */
	  x = f->output_data.x;
352
	  if (x->hourglass_window == wdesc)
353 354 355
	    found = f;
	  else if (x->widget)
	    {
Jan Djärv's avatar
Jan Djärv committed
356
#ifdef USE_GTK
357
              GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
358 359 360 361 362 363 364
              if (gwdesc != 0
                  && (gwdesc == x->widget
                      || gwdesc == x->edit_widget
                      || gwdesc == x->vbox_widget
                      || gwdesc == x->menubar_widget))
                found = f;
#else
365 366
	      if (wdesc == XtWindow (x->widget)
		  || wdesc == XtWindow (x->column_widget)
367 368 369 370 371
		  || 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
372
#endif
373 374 375 376
	    }
	  else if (FRAME_X_WINDOW (f) == wdesc)
	    /* A tooltip frame.  */
	    found = f;
377
	}
Jim Blandy's avatar
Jim Blandy committed
378
    }
379

380
  return found;
Jim Blandy's avatar
Jim Blandy committed
381
}
382

383 384 385 386 387 388 389 390 391
/* Likewise, but exclude the menu bar widget.  */

struct frame *
x_non_menubar_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
392
  struct x_output *x;
393

394 395
  if (wdesc == None) return 0;

396
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
397
    {
398
      frame = XCAR (tail);
399 400 401
      if (!GC_FRAMEP (frame))
        continue;
      f = XFRAME (frame);
402
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
403
	continue;
404
      x = f->output_data.x;
405
      /* This frame matches if the window is any of its widgets.  */
406
      if (x->hourglass_window == wdesc)
407 408
	return f;
      else if (x->widget)
409
	{
Jan Djärv's avatar
Jan Djärv committed
410
#ifdef USE_GTK
411
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
412 413 414 415 416 417
          if (gwdesc != 0
              && (gwdesc == x->widget
                  || gwdesc == x->edit_widget
                  || gwdesc == x->vbox_widget))
            return f;
#else
418 419
	  if (wdesc == XtWindow (x->widget)
	      || wdesc == XtWindow (x->column_widget)
420 421
	      || wdesc == XtWindow (x->edit_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
422
#endif
423 424 425
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* A tooltip frame.  */
426 427 428 429 430
	return f;
    }
  return 0;
}

431 432 433 434 435 436 437 438 439
/* 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;
440
  struct x_output *x;
441

442 443
  if (wdesc == None) return 0;

444
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
445
    {
446
      frame = XCAR (tail);
447 448 449
      if (!GC_FRAMEP (frame))
        continue;
      f = XFRAME (frame);
450
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
451
	continue;
452
      x = f->output_data.x;
453
      /* Match if the window is this frame's menubar.  */
Jan Djärv's avatar
Jan Djärv committed
454 455 456
#ifdef USE_GTK
      if (x->menubar_widget)
        {
457
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
458
          int found = 0;
459

Jan Djärv's avatar
Jan Djärv committed
460 461 462 463 464 465 466 467 468
          BLOCK_INPUT;
          if (gwdesc != 0
              && (gwdesc == x->menubar_widget
                  || gtk_widget_get_parent (gwdesc) == x->menubar_widget))
            found = 1;
          UNBLOCK_INPUT;
          if (found) return f;
        }
#else
469 470
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
471
	return f;
Jan Djärv's avatar
Jan Djärv committed
472
#endif
473 474 475 476
    }
  return 0;
}

477 478 479 480
/* Return the frame whose principal (outermost) window is WDESC.
   If WDESC is some other (smaller) window, we return 0.  */

struct frame *
481 482
x_top_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
483 484 485 486
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
487
  struct x_output *x;
488

489 490
  if (wdesc == None) return 0;

491
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
492
    {
493
      frame = XCAR (tail);
494
      if (!GC_FRAMEP (frame))
495 496
        continue;
      f = XFRAME (frame);
497
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
498
	continue;
499
      x = f->output_data.x;
500 501 502 503

      if (x->widget)
	{
	  /* This frame matches if the window is its topmost widget.  */
Jan Djärv's avatar
Jan Djärv committed
504
#ifdef USE_GTK
505
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
506 507 508
          if (gwdesc == x->widget)
            return f;
#else
509 510
	  if (wdesc == XtWindow (x->widget))
	    return f;
511 512 513
#if 0 /* I don't know why it did this,
	 but it seems logically wrong,
	 and it causes trouble for MapNotify events.  */
514
	  /* Match if the window is this frame's menubar.  */
515
	  if (x->menubar_widget
516 517
	      && wdesc == XtWindow (x->menubar_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
518
#endif
519
#endif
520 521 522 523
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* Tooltip frame.  */
	return f;
524 525 526
    }
  return 0;
}
Jan Djärv's avatar
Jan Djärv committed
527
#endif /* USE_X_TOOLKIT || USE_GTK */
Jim Blandy's avatar
Jim Blandy committed
528

529 530


531 532
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
533

534
void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
535
static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
536 537 538 539 540 541 542 543 544 545
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));
546
void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
547 548 549 550 551 552 553 554 555
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
556

557

558
/* Store the screen positions of frame F into XPTR and YPTR.
559 560 561 562 563 564 565 566
   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;
{
567 568 569 570
  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;
571

572
  int count;
573

574 575 576
  BLOCK_INPUT;

  count = x_catch_errors (FRAME_X_DISPLAY (f));
577

578 579 580 581 582 583 584 585
  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 (;;)
586
    {
587 588 589
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
590
      int success;
591

592 593
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
594

595
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
596

597 598 599 600 601 602
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

603 604
      if (wm_window == rootw || had_errors)
        break;
605

606 607
      win = wm_window;
    }
608

609 610
  if (! had_errors)
    {
611
      unsigned int ign;
612
      Window child, rootw;
613

614 615 616 617 618 619 620 621 622 623 624 625 626 627
      /* 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
      */
628
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
629

630 631
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
632
                             FRAME_X_WINDOW (f),
633

634
			     /* From-position, to-position.  */
635
                             real_x, real_y, &win_x, &win_y,
636

637 638
			     /* Child of win.  */
			     &child);
639

640
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
641
	{
642 643
          outer_x = win_x;
          outer_y = win_y;
644
	}
645 646 647
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
648

649 650 651
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
652

653 654
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
655

656 657
                                 /* Child of win.  */
                                 &child);
658
	}
659

660 661
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
662

663
  x_uncatch_errors (FRAME_X_DISPLAY (f), count);
664

665 666 667
  UNBLOCK_INPUT;

  if (had_errors) return;
668

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

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

Jim Blandy's avatar
Jim Blandy committed
679

680

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

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


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

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

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

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

723 724 725 726

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

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

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

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

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

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

756 757 758
  Fsignal (Qerror, Fcons (build_string ("Undefined color"),
			  Fcons (color_name, Qnil)));
  return 0;
Jim Blandy's avatar
Jim Blandy committed
759
}
760 761


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

775 776
#ifdef USE_GTK

777 778
/* 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.  */
779 780

int
781
xg_set_icon (f, file)
782
    FRAME_PTR f;
783 784
    Lisp_Object file;
{
785 786 787
  struct gcpro gcpro1;
  int result = 0;
  Lisp_Object found;
788

789
  GCPRO1 (found);
790

791 792 793 794 795 796
  found = x_find_image_file (file);

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

  UNGCPRO;
  return result;
818
}
819 820 821 822 823 824 825

int
xg_set_icon_from_xpm_data (f, data)
    FRAME_PTR f;
    char **data;
{
  int result = 0;
826
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) data);
827 828

  if (!pixbuf)
829
    return 0;
830

831
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
832 833 834
  g_object_unref (pixbuf);
  return 1;
}
835 836
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
837

Jim Blandy's avatar
Jim Blandy committed
838
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
839 840
   to set individual parameters.

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

854 855 856
  fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
  old_fg = x->foreground_pixel;
  x->foreground_pixel = fg;
857

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

862 863 864
      BLOCK_INPUT;
      XSetForeground (dpy, x->normal_gc, fg);
      XSetBackground (dpy, x->reverse_gc, fg);
865

866 867 868 869 870 871
      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);
	}
872

Jim Blandy's avatar
Jim Blandy committed
873
      UNBLOCK_INPUT;
874

875
      update_face_from_frame_parameter (f, Qforeground_color, arg);
876

877
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
878
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
879
    }
880

881
  unload_color (f, old_fg);
Jim Blandy's avatar
Jim Blandy committed
882 883 884
}

void
Jim Blandy's avatar
Jim Blandy committed
885 886
x_set_background_color (f, arg, oldval)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
887 888
     Lisp_Object arg, oldval;
{
889 890
  struct x_output *x = f->output_data.x;
  unsigned long bg;
Jim Blandy's avatar
Jim Blandy committed
891

892 893 894
  bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
  unload_color (f, x->background_pixel);
  x->background_pixel = bg;
895

896
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
897
    {
898
      Display *dpy = FRAME_X_DISPLAY (f);
899

900 901 902 903 904 905
      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
906 907 908 909
#ifdef USE_GTK
      xg_set_background_color (f, bg);
#endif

910 911 912 913 914 915 916 917 918 919 920 921 922
#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)
	  {
	    Window window = SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar));
	    XSetWindowBackground (dpy, window, bg);
	  }
      }
#endif /* USE_TOOLKIT_SCROLL_BARS */
Jim Blandy's avatar
Jim Blandy committed
923

924
      UNBLOCK_INPUT;