xfns.c 159 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Functions for the X window system.
2
   Copyright (C) 1989, 92, 93, 94, 95, 96, 97, 98, 99, 2000,01,02,03,04
3
     Free Software Foundation.
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
19 20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
21

Richard M. Stallman's avatar
Richard M. Stallman committed
22
#include <config.h>
23
#include <signal.h>
24
#include <stdio.h>
25
#include <math.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
26

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

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

33 34
#define XLIB_ILLEGAL_ACCESS

Jim Blandy's avatar
Jim Blandy committed
35 36
#include "lisp.h"
#include "xterm.h"
Jim Blandy's avatar
Jim Blandy committed
37
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
38 39
#include "window.h"
#include "buffer.h"
Kenichi Handa's avatar
Kenichi Handa committed
40
#include "intervals.h"
Jim Blandy's avatar
Jim Blandy committed
41
#include "dispextern.h"
Jim Blandy's avatar
Jim Blandy committed
42
#include "keyboard.h"
43
#include "blockinput.h"
44
#include <epaths.h>
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"
Jim Blandy's avatar
Jim Blandy committed
52 53

#ifdef HAVE_X_WINDOWS
54 55

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

59
#ifndef VMS
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
65 66 67
#else
#include "[.bitmaps]gray.xbm"
#endif
Jim Blandy's avatar
Jim Blandy committed
68

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

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

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

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

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

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

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

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

109 110
extern LWLIB_ID widget_id_tick;

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

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

119 120 121 122 123 124 125 126
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

127 128
#endif /* USE_X_TOOLKIT */

129 130 131 132 133 134 135 136
#ifdef USE_GTK

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

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

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
/* Non-zero means we're allowed to display an hourglass cursor.  */
152

153
int display_hourglass_p;
154

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

Jim Blandy's avatar
Jim Blandy committed
158
Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
159
Lisp_Object Vx_hourglass_pointer_shape;
160

161
/* The shape when over mouse-sensitive text.  */
162

163
Lisp_Object Vx_sensitive_text_pointer_shape;
Jim Blandy's avatar
Jim Blandy committed
164

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

Lisp_Object Vx_window_horizontal_drag_shape;

170
/* Color of chars displayed in cursor box.  */
171

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

174
/* Nonzero if using X.  */
175

176
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
177

178
/* Non nil if no window manager is in use.  */
179

Jim Blandy's avatar
Jim Blandy committed
180 181
Lisp_Object Vx_no_window_manager;

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

Karl Heuer's avatar
Karl Heuer committed
184 185
Lisp_Object Vx_pixel_size_width_font_regexp;

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

191
/* In dispnew.c */
192

Jim Blandy's avatar
Jim Blandy committed
193 194
extern Lisp_Object Vwindow_system_version;

195
/* The below are defined in frame.c.  */
196

197
#if GLYPH_DEBUG
198 199 200 201
int image_cache_refcount, dpyinfo_refcount;
#endif


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

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

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

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

239 240 241 242
/* 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.  */

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

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

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

  return dpyinfo;
269
}
270

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

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

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

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

Jan Djärv's avatar
Jan Djärv committed
321
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
322 323 324 325
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
326 327
x_any_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
328 329 330
     int wdesc;
{
  Lisp_Object tail, frame;
331
  struct frame *f, *found;
332
  struct x_output *x;
333

334 335
  found = NULL;
  for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
336
    {
337
      frame = XCAR (tail);
338
      if (!GC_FRAMEP (frame))
339
        continue;
340

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

374
  return found;
Jim Blandy's avatar
Jim Blandy committed
375
}
376

377 378 379 380 381 382 383 384 385
/* 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;
386
  struct x_output *x;
387

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

423 424 425 426 427 428 429 430 431
/* 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;
432
  struct x_output *x;
433

434
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
435
    {
436
      frame = XCAR (tail);
437 438 439
      if (!GC_FRAMEP (frame))
        continue;
      f = XFRAME (frame);
440
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
441
	continue;
442
      x = f->output_data.x;
443
      /* Match if the window is this frame's menubar.  */
Jan Djärv's avatar
Jan Djärv committed
444 445 446
#ifdef USE_GTK
      if (x->menubar_widget)
        {
447
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
448
          int found = 0;
449

Jan Djärv's avatar
Jan Djärv committed
450 451 452 453 454 455 456 457 458
          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
459 460
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
461
	return f;
Jan Djärv's avatar
Jan Djärv committed
462
#endif
463 464 465 466
    }
  return 0;
}

467 468 469 470
/* Return the frame whose principal (outermost) window is WDESC.
   If WDESC is some other (smaller) window, we return 0.  */

struct frame *
471 472
x_top_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
473 474 475 476
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
477
  struct x_output *x;
478

479
  for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
480
    {
481
      frame = XCAR (tail);
482
      if (!GC_FRAMEP (frame))
483 484
        continue;
      f = XFRAME (frame);
485
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
486
	continue;
487
      x = f->output_data.x;
488 489 490 491

      if (x->widget)
	{
	  /* This frame matches if the window is its topmost widget.  */
Jan Djärv's avatar
Jan Djärv committed
492
#ifdef USE_GTK
493
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
494 495 496
          if (gwdesc == x->widget)
            return f;
#else
497 498
	  if (wdesc == XtWindow (x->widget))
	    return f;
499 500 501
#if 0 /* I don't know why it did this,
	 but it seems logically wrong,
	 and it causes trouble for MapNotify events.  */
502
	  /* Match if the window is this frame's menubar.  */
503
	  if (x->menubar_widget
504 505
	      && wdesc == XtWindow (x->menubar_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
506
#endif
507
#endif
508 509 510 511
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* Tooltip frame.  */
	return f;
512 513 514
    }
  return 0;
}
Jan Djärv's avatar
Jan Djärv committed
515
#endif /* USE_X_TOOLKIT || USE_GTK */
Jim Blandy's avatar
Jim Blandy committed
516

517 518


519 520
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
521

522
void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
523
static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
524 525 526 527 528 529 530 531 532 533
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));
534
void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
535 536 537 538 539 540 541 542 543
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
544

545

546
/* Store the screen positions of frame F into XPTR and YPTR.
547 548 549 550 551 552 553 554
   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;
{
555 556 557 558
  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;
559

560
  int count;
561

562 563 564
  BLOCK_INPUT;

  count = x_catch_errors (FRAME_X_DISPLAY (f));
565

566 567 568 569 570 571 572 573
  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 (;;)
574
    {
575 576 577
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
578
      int success;
579

580 581
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
582

583
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
584

585 586 587 588 589 590
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

591 592
      if (wm_window == rootw || had_errors)
        break;
593

594 595
      win = wm_window;
    }
596

597 598 599 600
  if (! had_errors)
    {
      int ign;
      Window child, rootw;
601

602 603 604 605 606 607 608 609 610 611 612 613 614 615
      /* 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
      */
616
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
617

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

622
			     /* From-position, to-position.  */
623
                             real_x, real_y, &win_x, &win_y,
624

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

628
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
629
	{
630 631
          outer_x = win_x;
          outer_y = win_y;
632
	}
633 634 635
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
636

637 638 639
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
640

641 642
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
643

644 645
                                 /* Child of win.  */
                                 &child);
646
	}
647

648 649
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
650

651
  x_uncatch_errors (FRAME_X_DISPLAY (f), count);
652

653 654 655
  UNBLOCK_INPUT;

  if (had_errors) return;
656

657 658 659 660 661
  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;
662 663 664

  *xptr = real_x;
  *yptr = real_y;
665 666
}

Jim Blandy's avatar
Jim Blandy committed
667

668

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

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


686 687 688 689
/* 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.  */
690

Jim Blandy's avatar
Jim Blandy committed
691
int
692 693 694 695 696
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
697
{
698 699 700
  int success_p;
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
701 702

  BLOCK_INPUT;
703 704 705
  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
706 707
  UNBLOCK_INPUT;

708
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
709 710
}

711 712 713 714

/* 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
715 716

int
717
x_decode_color (f, color_name, mono_color)
718
     FRAME_PTR f;
719 720
     Lisp_Object color_name;
     int mono_color;
Jim Blandy's avatar
Jim Blandy committed
721
{
722
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
723

724
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
725

726 727 728
#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.  */
729
  if (strcmp (SDATA (color_name), "black") == 0)
730
    return BLACK_PIX_DEFAULT (f);
731
  else if (strcmp (SDATA (color_name), "white") == 0)
732
    return WHITE_PIX_DEFAULT (f);
733
#endif
Jim Blandy's avatar
Jim Blandy committed
734

735
  /* Return MONO_COLOR for monochrome frames.  */
736
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
737
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
738

739
  /* x_defined_color is responsible for coping with failures
740
     by looking for a near-miss.  */
741
  if (x_defined_color (f, SDATA (color_name), &cdef, 1))
742 743
    return cdef.pixel;

744 745 746
  Fsignal (Qerror, Fcons (build_string ("Undefined color"),
			  Fcons (color_name, Qnil)));
  return 0;
Jim Blandy's avatar
Jim Blandy committed
747
}
748 749


Jim Blandy's avatar
Jim Blandy committed
750

Gerd Moellmann's avatar
Gerd Moellmann committed
751 752 753 754 755 756 757 758 759 760 761 762
/* 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);
}

763 764
#ifdef USE_GTK

765 766
/* 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.  */
767 768

int
769
xg_set_icon (f, file)
770
    FRAME_PTR f;
771 772
    Lisp_Object file;
{
773 774 775
  struct gcpro gcpro1;
  int result = 0;
  Lisp_Object found;
776

777
  GCPRO1 (found);
778

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
      char *filename;

      filename = SDATA (found);
      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;
808 809 810
}
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
811

Jim Blandy's avatar
Jim Blandy committed
812
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
813 814
   to set individual parameters.

815
   If FRAME_X_WINDOW (f) is 0,
Jim Blandy's avatar
Jim Blandy committed
816
   the frame is being created and its X-window does not exist yet.
Jim Blandy's avatar
Jim Blandy committed
817 818 819 820
   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
821 822
x_set_foreground_color (f, arg, oldval)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
823 824
     Lisp_Object arg, oldval;
{
825 826
  struct x_output *x = f->output_data.x;
  unsigned long fg, old_fg;
827

828 829 830
  fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
  old_fg = x->foreground_pixel;
  x->foreground_pixel = fg;
831

832
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
833
    {
834
      Display *dpy = FRAME_X_DISPLAY (f);
835

836 837 838
      BLOCK_INPUT;
      XSetForeground (dpy, x->normal_gc, fg);
      XSetBackground (dpy, x->reverse_gc, fg);
839

840 841 842 843 844 845
      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);
	}
846

Jim Blandy's avatar
Jim Blandy committed
847
      UNBLOCK_INPUT;
848

849
      update_face_from_frame_parameter (f, Qforeground_color, arg);
850

851
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
852
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
853
    }
854

855
  unload_color (f, old_fg);
Jim Blandy's avatar
Jim Blandy committed
856 857 858
}

void
Jim Blandy's avatar
Jim Blandy committed
859 860
x_set_background_color (f, arg, oldval)
     struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
861 862
     Lisp_Object arg, oldval;
{