xfns.c 172 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 8 9

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
10
the Free Software Foundation; either version 3, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
11 12 13 14 15 16 17 18 19
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
20 21
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
Jim Blandy's avatar
Jim Blandy committed
22

Richard M. Stallman's avatar
Richard M. Stallman committed
23
#include <config.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>
Kenichi Handa's avatar
Kenichi Handa committed
45
#include "character.h"
Karl Heuer's avatar
Karl Heuer committed
46
#include "charset.h"
Kenichi Handa's avatar
Kenichi Handa committed
47
#include "coding.h"
Karl Heuer's avatar
Karl Heuer committed
48
#include "fontset.h"
49 50
#include "systime.h"
#include "termhooks.h"
51
#include "atimer.h"
52
#include "termchar.h"
Kenichi Handa's avatar
Kenichi Handa committed
53 54
#include "font.h"

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

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

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

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

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

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

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

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

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

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

114 115
/* Unique id counter for widgets created by the Lucid Widget Library.  */

116 117
extern LWLIB_ID widget_id_tick;

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

123
extern void free_frame_menubar ();
124
extern double atof ();
125

126 127 128 129 130 131 132 133
#ifdef USE_MOTIF

/* LessTif/Motif version info.  */

static Lisp_Object Vmotif_version_string;

#endif /* USE_MOTIF */

134 135
#endif /* USE_X_TOOLKIT */

Kenichi Handa's avatar
Kenichi Handa committed
136 137 138 139 140 141 142 143
#ifdef USE_GTK

/* GTK+ version info */

static Lisp_Object Vgtk_version_string;

#endif /* USE_GTK */

Richard M. Stallman's avatar
Richard M. Stallman committed
144 145
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))

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

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

156
int display_hourglass_p;
157

158 159
/* Non-zero means prompt with the old GTK file selection dialog.  */

160
int x_gtk_use_old_file_dialog;
161

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

int x_gtk_show_hidden_files;

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

168
int x_gtk_file_dialog_help_text;
169

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

Jim Blandy's avatar
Jim Blandy committed
177
Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
178
Lisp_Object Vx_hourglass_pointer_shape;
179

180
/* The shape when over mouse-sensitive text.  */
181

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

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

Lisp_Object Vx_window_horizontal_drag_shape;

189
/* Color of chars displayed in cursor box.  */
190

Jim Blandy's avatar
Jim Blandy committed
191 192
Lisp_Object Vx_cursor_fore_pixel;

193
/* Nonzero if using X.  */
194

195
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
196

197
/* Non nil if no window manager is in use.  */
198

Jim Blandy's avatar
Jim Blandy committed
199 200
Lisp_Object Vx_no_window_manager;

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

Karl Heuer's avatar
Karl Heuer committed
203 204
Lisp_Object Vx_pixel_size_width_font_regexp;

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

210
/* In dispnew.c */
211

Jim Blandy's avatar
Jim Blandy committed
212 213
extern Lisp_Object Vwindow_system_version;

214
/* The below are defined in frame.c.  */
215

216
#if GLYPH_DEBUG
217 218 219 220
int image_cache_refcount, dpyinfo_refcount;
#endif


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

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

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

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

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

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

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

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

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

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

  return dpyinfo;
298
}
299

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

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

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

314 315
  if (wdesc == None) return 0;

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

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

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

365 366
  if (wdesc == None) return NULL;

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

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

407
  return found;
Jim Blandy's avatar
Jim Blandy committed
408
}
409

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

421 422
  if (wdesc == None) return 0;

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

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

469 470
  if (wdesc == None) return 0;

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

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

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

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

516 517
  if (wdesc == None) return 0;

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

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

556 557


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

560 561
static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
562

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

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

601 602
  BLOCK_INPUT;

603
  x_catch_errors (FRAME_X_DISPLAY (f));
604

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

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

622
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
623

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

      XFree ((char *) tmp_children);

630 631
      if (wm_window == rootw || had_errors)
        break;
632

633 634
      win = wm_window;
    }
635

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

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

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

661
			     /* From-position, to-position.  */
662
                             real_x, real_y, &win_x, &win_y,
663

664 665
			     /* Child of win.  */
			     &child);
666

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

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

680 681
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
682

683 684
                                 /* Child of win.  */
                                 &child);
685
	}
686

687 688
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
689

690
  x_uncatch_errors ();
691

692 693 694
  UNBLOCK_INPUT;

  if (had_errors) return;
695

696 697 698 699 700
  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;
701 702 703

  *xptr = real_x;
  *yptr = real_y;
704 705
}

Jim Blandy's avatar
Jim Blandy committed
706

707

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

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


725 726 727 728
/* 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.  */
729

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

  BLOCK_INPUT;
742 743 744
  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
745 746
  UNBLOCK_INPUT;

747
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
748 749
}

750 751 752 753

/* 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
754 755

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

763
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
764

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

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

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

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


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

800 801
#ifdef USE_GTK

802 803
/* 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.  */
804 805

int
Kenichi Handa's avatar
Kenichi Handa committed
806
xg_set_icon (f, file)
807
    FRAME_PTR f;
808 809
    Lisp_Object file;
{
810 811
  int result = 0;
  Lisp_Object found;
812

813 814 815 816 817 818
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
819
      char *filename = (char *) SDATA (found);
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
      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;
839
}
840 841 842 843 844 845 846

int
xg_set_icon_from_xpm_data (f, data)
    FRAME_PTR f;
    char **data;
{
  int result = 0;
847
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) data);
848 849

  if (!pixbuf)
850
    return 0;
851

852
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
853 854 855
  g_object_unref (pixbuf);
  return 1;
}
856 857
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
858

Jim Blandy's avatar
Jim Blandy committed
859
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
860 861
   to set individual parameters.

862
   If FRAME_X_WINDOW (f) is 0,
Jim Blandy's avatar
Jim Blandy committed
863
   the frame is being created and its X-window does not exist yet.
Jim Blandy's avatar
Jim Blandy committed
864 865 866 867
   In that case, just record the parameter's new value
   in the standard place; do not attempt to change the window.  */