xfns.c 174 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 4
                 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
                 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>
Richard M. Stallman's avatar
Richard M. Stallman committed
24

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

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

31 32
#define XLIB_ILLEGAL_ACCESS

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

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

#ifdef USG
#undef USG	/* ####KLUDGE for Solaris 2.2 and up */
#include <X11/Xos.h>
#define USG
#else
#include <X11/Xos.h>
#endif

#include "widget.h"

#include "../lwlib/lwlib.h"

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

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

107
#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
108
#define HACK_EDITRES
109
extern void _XEditResCheckMessages ();
110 111
#endif /* R5 + Athena */

112 113
/* Unique id counter for widgets created by the Lucid Widget Library.  */

114 115
extern LWLIB_ID widget_id_tick;

116
#ifdef USE_LUCID
117
/* This is part of a kludge--see lwlib/xlwmenu.c.  */
118
extern XFontStruct *xlwmenu_default_font;
119
#endif
120

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

124 125 126 127 128 129 130 131
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

132 133
#endif /* USE_X_TOOLKIT */

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

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

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

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

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

154
int display_hourglass_p;
155

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

158
int x_gtk_use_old_file_dialog;
159

160 161 162 163
/* If non-zero, by default show hidden files in the GTK file chooser.  */

int x_gtk_show_hidden_files;

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

166
int x_gtk_file_dialog_help_text;
167

168 169 170 171
/* 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
172
/* The background and shape of the mouse pointer, and shape when not
173
   over text or in the modeline.  */
174

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

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

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

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

Lisp_Object Vx_window_horizontal_drag_shape;

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

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

191
/* Nonzero if using X.  */
192

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

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

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

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

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

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

208
/* In dispnew.c */
209

Jim Blandy's avatar
Jim Blandy committed
210 211
extern Lisp_Object Vwindow_system_version;

212
/* The below are defined in frame.c.  */
213

214
#if GLYPH_DEBUG
215 216 217 218
int image_cache_refcount, dpyinfo_refcount;
#endif


Jim Blandy's avatar
Jim Blandy committed
219

Richard M. Stallman's avatar
Richard M. Stallman committed
220
/* Error if we are not connected to X.  */
221

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

229 230
/* Nonzero if we can use mouse menus.
   You should not call this unless HAVE_MENUS is defined.  */
231 232

int
233
have_menus_p ()
234
{
235 236 237 238 239 240 241 242 243 244 245 246 247
  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
248
    frame = selected_frame;
249
  CHECK_LIVE_FRAME (frame);
Gerd Moellmann's avatar
Gerd Moellmann committed
250
  f = XFRAME (frame);
251
  if (! FRAME_X_P (f))
252
    error ("Non-X frame used");
253
  return f;
254 255
}

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

261
struct x_display_info *
262 263
check_x_display_info (object)
     Lisp_Object object;
264
{
265
  struct x_display_info *dpyinfo = NULL;
266

267
  if (NILP (object))
268
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
269
      struct frame *sf = XFRAME (selected_frame);
270

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

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

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

  return dpyinfo;
296
}
297

298

Jim Blandy's avatar
Jim Blandy committed
299 300
/* 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
301

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

Jim Blandy's avatar
Jim Blandy committed
304
struct frame *
305 306
x_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
Jim Blandy's avatar
Jim Blandy committed
307 308
     int wdesc;
{
Jim Blandy's avatar
Jim Blandy committed
309 310
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
311

312 313
  if (wdesc == None) return 0;

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

Jan Djärv's avatar
Jan Djärv committed
350
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
351 352 353 354
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
355 356
x_any_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
357 358 359
     int wdesc;
{
  Lisp_Object tail, frame;
360
  struct frame *f, *found;
361
  struct x_output *x;
362

363 364
  if (wdesc == None) return NULL;

365
  found = NULL;
366
  for (tail = Vframe_list; CONSP (tail) && !found; tail = XCDR (tail))
367
    {
368
      frame = XCAR (tail);
369
      if (!FRAMEP (frame))
370
        continue;
371

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

405
  return found;
Jim Blandy's avatar
Jim Blandy committed
406
}
407

408 409 410 411 412 413 414 415 416
/* 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;
417
  struct x_output *x;
418

419 420
  if (wdesc == None) return 0;

421
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
422
    {
423
      frame = XCAR (tail);
424
      if (!FRAMEP (frame))
425 426
        continue;
      f = XFRAME (frame);
427
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
428
	continue;
429
      x = f->output_data.x;
430
      /* This frame matches if the window is any of its widgets.  */
431
      if (x->hourglass_window == wdesc)
432 433
	return f;
      else if (x->widget)
434
	{
Jan Djärv's avatar
Jan Djärv committed
435
#ifdef USE_GTK
Kenichi Handa's avatar
Kenichi Handa committed
436
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
437 438 439 440 441 442
          if (gwdesc != 0
              && (gwdesc == x->widget
                  || gwdesc == x->edit_widget
                  || gwdesc == x->vbox_widget))
            return f;
#else
443 444
	  if (wdesc == XtWindow (x->widget)
	      || wdesc == XtWindow (x->column_widget)
445 446
	      || wdesc == XtWindow (x->edit_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
447
#endif
448 449 450
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* A tooltip frame.  */
451 452 453 454 455
	return f;
    }
  return 0;
}

456 457 458 459 460 461 462 463 464
/* 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;
465
  struct x_output *x;
466

467 468
  if (wdesc == None) return 0;

469
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
470
    {
471
      frame = XCAR (tail);
472
      if (!FRAMEP (frame))
473 474
        continue;
      f = XFRAME (frame);
475
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
476
	continue;
477
      x = f->output_data.x;
478
      /* Match if the window is this frame's menubar.  */
Jan Djärv's avatar
Jan Djärv committed
479 480 481
#ifdef USE_GTK
      if (x->menubar_widget)
        {
Kenichi Handa's avatar
Kenichi Handa committed
482
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
483
          int found = 0;
484

Jan Djärv's avatar
Jan Djärv committed
485 486 487 488 489 490 491 492 493
          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
494 495
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
496
	return f;
Jan Djärv's avatar
Jan Djärv committed
497
#endif
498 499 500 501
    }
  return 0;
}

502 503 504 505
/* Return the frame whose principal (outermost) window is WDESC.
   If WDESC is some other (smaller) window, we return 0.  */

struct frame *
506 507
x_top_window_to_frame (dpyinfo, wdesc)
     struct x_display_info *dpyinfo;
508 509 510 511
     int wdesc;
{
  Lisp_Object tail, frame;
  struct frame *f;
512
  struct x_output *x;
513

514 515
  if (wdesc == None) return 0;

516
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
517
    {
518
      frame = XCAR (tail);
519
      if (!FRAMEP (frame))
520 521
        continue;
      f = XFRAME (frame);
522
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
523
	continue;
524
      x = f->output_data.x;
525 526 527 528

      if (x->widget)
	{
	  /* This frame matches if the window is its topmost widget.  */
Jan Djärv's avatar
Jan Djärv committed
529
#ifdef USE_GTK
Kenichi Handa's avatar
Kenichi Handa committed
530
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
531 532 533
          if (gwdesc == x->widget)
            return f;
#else
534 535
	  if (wdesc == XtWindow (x->widget))
	    return f;
536 537 538
#if 0 /* I don't know why it did this,
	 but it seems logically wrong,
	 and it causes trouble for MapNotify events.  */
539
	  /* Match if the window is this frame's menubar.  */
540
	  if (x->menubar_widget
541 542
	      && wdesc == XtWindow (x->menubar_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
543
#endif
544
#endif
545 546 547 548
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* Tooltip frame.  */
	return f;
549 550 551
    }
  return 0;
}
Jan Djärv's avatar
Jan Djärv committed
552
#endif /* USE_X_TOOLKIT || USE_GTK */
Jim Blandy's avatar
Jim Blandy committed
553

554 555


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

558 559
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
560

561
void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
562
static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
563 564 565 566 567 568 569 570 571 572
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));
573
void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
574 575 576 577 578 579 580 581 582
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
583

584

585
/* Store the screen positions of frame F into XPTR and YPTR.
586 587 588 589 590 591 592 593
   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;
{
594 595 596 597
  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;
598

599 600
  BLOCK_INPUT;

601
  x_catch_errors (FRAME_X_DISPLAY (f));
602

603 604 605 606 607 608 609 610
  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 (;;)
611
    {
612 613 614
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
615
      int success;
616

617 618
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
619

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

622 623 624 625 626 627
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

628 629
      if (wm_window == rootw || had_errors)
        break;
630

631 632
      win = wm_window;
    }
633

634 635
  if (! had_errors)
    {
636
      unsigned int ign;
637
      Window child, rootw;
638

639 640 641 642 643 644 645 646 647 648 649 650 651 652
      /* 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
      */
653
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
654

655 656
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
657
                             FRAME_X_WINDOW (f),
658

659
			     /* From-position, to-position.  */
660
                             real_x, real_y, &win_x, &win_y,
661

662 663
			     /* Child of win.  */
			     &child);
664

665
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
666
	{
667 668
          outer_x = win_x;
          outer_y = win_y;
669
	}
670 671 672
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
673

674 675 676
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
677

678 679
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
680

681 682
                                 /* Child of win.  */
                                 &child);
683
	}
684

685 686
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
687

688
  x_uncatch_errors ();
689

690 691 692
  UNBLOCK_INPUT;

  if (had_errors) return;
693

694 695 696 697 698
  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;
699 700 701

  *xptr = real_x;
  *yptr = real_y;
702 703
}

Jim Blandy's avatar
Jim Blandy committed
704

705

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722

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


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

Jim Blandy's avatar
Jim Blandy committed
728
int
729 730 731 732 733
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
734
{
735 736 737
  int success_p;
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
738 739

  BLOCK_INPUT;
740 741 742
  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
743 744
  UNBLOCK_INPUT;

745
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
746 747
}

748 749 750 751

/* 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
752 753

int
754
x_decode_color (f, color_name, mono_color)
755
     FRAME_PTR f;
756 757
     Lisp_Object color_name;
     int mono_color;
Jim Blandy's avatar
Jim Blandy committed
758
{
759
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
760

761
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
762

763 764 765
#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.  */
766
  if (strcmp (SDATA (color_name), "black") == 0)
767
    return BLACK_PIX_DEFAULT (f);
768
  else if (strcmp (SDATA (color_name), "white") == 0)
769
    return WHITE_PIX_DEFAULT (f);
770
#endif
Jim Blandy's avatar
Jim Blandy committed
771

772
  /* Return MONO_COLOR for monochrome frames.  */
773
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
774
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
775

776
  /* x_defined_color is responsible for coping with failures
777
     by looking for a near-miss.  */
778
  if (x_defined_color (f, SDATA (color_name), &cdef, 1))
779 780
    return cdef.pixel;

781
  signal_error ("Undefined color", color_name);
Jim Blandy's avatar
Jim Blandy committed
782
}
783 784


Jim Blandy's avatar
Jim Blandy committed
785

Gerd Moellmann's avatar
Gerd Moellmann committed
786 787 788 789 790 791 792 793 794 795 796 797
/* 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);
}

798 799