xfns.c 177 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Functions for the X window system.
Glenn Morris's avatar
Glenn Morris committed
2

3
Copyright (C) 1989, 1992-2011  Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6

This file is part of GNU Emacs.

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

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
18
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19

Richard M. Stallman's avatar
Richard M. Stallman committed
20
#include <config.h>
21
#include <stdio.h>
22
#include <math.h>
23
#include <setjmp.h>
24
#include <ctype.h>
25 26
#include <unistd.h>

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

29 30
#define XLIB_ILLEGAL_ACCESS

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

Jim Blandy's avatar
Jim Blandy committed
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
#if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work.  */
58 59
#include "bitmaps/gray.xbm"
#else
Jim Blandy's avatar
Jim Blandy committed
60
#include <X11/bitmaps/gray>
61
#endif
Jim Blandy's avatar
Jim Blandy committed
62

63 64
#include "xsettings.h"

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

69 70 71
#ifdef USE_X_TOOLKIT
#include <X11/Shell.h>

72
#ifndef USE_MOTIF
Chong Yidong's avatar
Chong Yidong committed
73 74
#ifdef HAVE_XAW3D
#include <X11/Xaw3d/Paned.h>
Chong Yidong's avatar
Chong Yidong committed
75
#include <X11/Xaw3d/Label.h>
Chong Yidong's avatar
Chong Yidong committed
76
#else /* !HAVE_XAW3D */
77 78
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Label.h>
Chong Yidong's avatar
Chong Yidong committed
79
#endif /* HAVE_XAW3D */
80
#endif /* USE_MOTIF */
81 82 83 84 85

#ifdef USG
#undef USG	/* ####KLUDGE for Solaris 2.2 and up */
#include <X11/Xos.h>
#define USG
86 87
#ifdef USG /* Pacify gcc -Wunused-macros.  */
#endif
88 89 90 91 92 93 94 95
#else
#include <X11/Xos.h>
#endif

#include "widget.h"

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

96 97 98 99
#ifdef USE_MOTIF
#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/FileSB.h>
Jan D's avatar
Jan D committed
100 101
#include <Xm/List.h>
#include <Xm/TextF.h>
102 103
#endif

104 105 106 107
#ifdef USE_LUCID
#include "../lwlib/xlwmenu.h"
#endif

Dan Nicolaescu's avatar
Dan Nicolaescu committed
108
#if !defined(NO_EDITRES)
109
#define HACK_EDITRES
110
extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
Dan Nicolaescu's avatar
Dan Nicolaescu committed
111
#endif /* not defined NO_EDITRES */
112

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

115 116
extern LWLIB_ID widget_id_tick;

117 118 119 120
#ifdef USE_MOTIF

#endif /* USE_MOTIF */

121 122
#endif /* USE_X_TOOLKIT */

Kenichi Handa's avatar
Kenichi Handa committed
123 124 125 126
#ifdef USE_GTK

#endif /* USE_GTK */

Richard M. Stallman's avatar
Richard M. Stallman committed
127 128
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))

129 130 131 132 133 134
/* 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;
135
char *gray_bitmap_bits = gray_bits;
136

137
/* Nonzero if using X.  */
138

139
static int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
140

141 142 143 144
static Lisp_Object Qnone;
static Lisp_Object Qsuppress_icon;
static Lisp_Object Qundefined_color;
static Lisp_Object Qcompound_text, Qcancel_timer;
145
Lisp_Object Qfont_param;
Jim Blandy's avatar
Jim Blandy committed
146

147
#if GLYPH_DEBUG
148 149 150
int image_cache_refcount, dpyinfo_refcount;
#endif

151
#if defined (USE_GTK) && defined (HAVE_FREETYPE)
152
static char *x_last_font_name;
153
#endif
154

155 156
static struct x_display_info *x_display_info_for_name (Lisp_Object);

Jim Blandy's avatar
Jim Blandy committed
157

Richard M. Stallman's avatar
Richard M. Stallman committed
158
/* Error if we are not connected to X.  */
159

Karl Heuer's avatar
Karl Heuer committed
160
void
161
check_x (void)
Richard M. Stallman's avatar
Richard M. Stallman committed
162
{
163
  if (! x_in_use)
Richard M. Stallman's avatar
Richard M. Stallman committed
164 165 166
    error ("X windows are not in use or not initialized");
}

167 168
/* Nonzero if we can use mouse menus.
   You should not call this unless HAVE_MENUS is defined.  */
169 170

int
171
have_menus_p (void)
172
{
173 174 175 176 177 178 179
  return x_in_use;
}

/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
   and checking validity for X.  */

FRAME_PTR
180
check_x_frame (Lisp_Object frame)
181 182 183 184
{
  FRAME_PTR f;

  if (NILP (frame))
Gerd Moellmann's avatar
Gerd Moellmann committed
185
    frame = selected_frame;
186
  CHECK_LIVE_FRAME (frame);
Gerd Moellmann's avatar
Gerd Moellmann committed
187
  f = XFRAME (frame);
188
  if (! FRAME_X_P (f))
189
    error ("Non-X frame used");
190
  return f;
191 192
}

193
/* Let the user specify an X display with a Lisp object.
194
   OBJECT may be nil, a frame or a terminal object.
195 196 197
   nil stands for the selected frame--or, if that is not an X frame,
   the first X display on the list.  */

198
struct x_display_info *
199
check_x_display_info (Lisp_Object object)
200
{
201
  struct x_display_info *dpyinfo = NULL;
202

203
  if (NILP (object))
204
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
205
      struct frame *sf = XFRAME (selected_frame);
206

Gerd Moellmann's avatar
Gerd Moellmann committed
207
      if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
208
	dpyinfo = FRAME_X_DISPLAY_INFO (sf);
209
      else if (x_display_list != 0)
210
	dpyinfo = x_display_list;
211 212 213
      else
	error ("X windows are not in use or not initialized");
    }
214
  else if (TERMINALP (object))
215
    {
216
      struct terminal *t = get_terminal (object, 1);
217

218
      if (t->type != output_x_window)
219
        error ("Terminal %"pEd" is not an X display", XINT (object));
220

221
      dpyinfo = t->display_info.x;
222
    }
223 224
  else if (STRINGP (object))
    dpyinfo = x_display_info_for_name (object);
225 226
  else
    {
227
      FRAME_PTR f = check_x_frame (object);
228
      dpyinfo = FRAME_X_DISPLAY_INFO (f);
229
    }
230 231

  return dpyinfo;
232
}
233

234

Jim Blandy's avatar
Jim Blandy committed
235 236
/* 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
237

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

Jim Blandy's avatar
Jim Blandy committed
240
struct frame *
241
x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
Jim Blandy's avatar
Jim Blandy committed
242
{
Jim Blandy's avatar
Jim Blandy committed
243 244
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
245

246 247
  if (wdesc == None) return 0;

248
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
Jim Blandy's avatar
Jim Blandy committed
249
    {
250
      frame = XCAR (tail);
251
      if (!FRAMEP (frame))
Jim Blandy's avatar
Jim Blandy committed
252
        continue;
Jim Blandy's avatar
Jim Blandy committed
253
      f = XFRAME (frame);
254
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
255
	continue;
256
      if (f->output_data.x->hourglass_window == wdesc)
257
	return f;
258
#ifdef USE_X_TOOLKIT
259
      if ((f->output_data.x->edit_widget
260
	   && XtWindow (f->output_data.x->edit_widget) == wdesc)
261 262 263
	  /* A tooltip frame?  */
	  || (!f->output_data.x->edit_widget
	      && FRAME_X_WINDOW (f) == wdesc)
264
          || f->output_data.x->icon_desc == wdesc)
265 266
        return f;
#else /* not USE_X_TOOLKIT */
Jan Djärv's avatar
Jan Djärv committed
267 268 269
#ifdef USE_GTK
      if (f->output_data.x->edit_widget)
      {
Kenichi Handa's avatar
Kenichi Handa committed
270
        GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
271 272 273 274 275
        struct x_output *x = f->output_data.x;
        if (gwdesc != 0 && gwdesc == x->edit_widget)
          return f;
      }
#endif /* USE_GTK */
276
      if (FRAME_X_WINDOW (f) == wdesc
277
          || f->output_data.x->icon_desc == wdesc)
Jim Blandy's avatar
Jim Blandy committed
278
        return f;
279 280 281 282 283
#endif /* not USE_X_TOOLKIT */
    }
  return 0;
}

Jan Djärv's avatar
Jan Djärv committed
284
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
285 286 287 288
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
289
x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
290 291
{
  Lisp_Object tail, frame;
292
  struct frame *f, *found;
293
  struct x_output *x;
294

295 296
  if (wdesc == None) return NULL;

297
  found = NULL;
298
  for (tail = Vframe_list; CONSP (tail) && !found; tail = XCDR (tail))
299
    {
300
      frame = XCAR (tail);
301
      if (!FRAMEP (frame))
302
        continue;
303

304
      f = XFRAME (frame);
305
      if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
306
	{
307 308
	  /* This frame matches if the window is any of its widgets.  */
	  x = f->output_data.x;
309
	  if (x->hourglass_window == wdesc)
310 311 312
	    found = f;
	  else if (x->widget)
	    {
Jan Djärv's avatar
Jan Djärv committed
313
#ifdef USE_GTK
Kenichi Handa's avatar
Kenichi Handa committed
314
              GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
315
              if (gwdesc != 0
316
                  && gtk_widget_get_toplevel (gwdesc) == x->widget)
Jan Djärv's avatar
Jan Djärv committed
317 318
                found = f;
#else
319 320
	      if (wdesc == XtWindow (x->widget)
		  || wdesc == XtWindow (x->column_widget)
321 322 323 324 325
		  || 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
326
#endif
327 328 329 330
	    }
	  else if (FRAME_X_WINDOW (f) == wdesc)
	    /* A tooltip frame.  */
	    found = f;
331
	}
Jim Blandy's avatar
Jim Blandy committed
332
    }
333

334
  return found;
Jim Blandy's avatar
Jim Blandy committed
335
}
336

337 338 339
/* Likewise, but consider only the menu bar widget.  */

struct frame *
340
x_menubar_window_to_frame (struct x_display_info *dpyinfo, XEvent *event)
341
{
Jan D's avatar
Jan D committed
342
  Window wdesc = event->xany.window;
343 344
  Lisp_Object tail, frame;
  struct frame *f;
345
  struct x_output *x;
346

347 348
  if (wdesc == None) return 0;

349
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
350
    {
351
      frame = XCAR (tail);
352
      if (!FRAMEP (frame))
353 354
        continue;
      f = XFRAME (frame);
355
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
356
	continue;
357
      x = f->output_data.x;
Jan Djärv's avatar
Jan Djärv committed
358
#ifdef USE_GTK
Jan D's avatar
Jan D committed
359 360
      if (x->menubar_widget && xg_event_is_for_menubar (f, event))
        return f;
Jan Djärv's avatar
Jan Djärv committed
361
#else
Jan D's avatar
Jan D committed
362
      /* Match if the window is this frame's menubar.  */
363 364
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
365
	return f;
Jan Djärv's avatar
Jan Djärv committed
366
#endif
367 368 369 370
    }
  return 0;
}

371 372 373 374
/* Return the frame whose principal (outermost) window is WDESC.
   If WDESC is some other (smaller) window, we return 0.  */

struct frame *
375
x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
376 377 378
{
  Lisp_Object tail, frame;
  struct frame *f;
379
  struct x_output *x;
380

381 382
  if (wdesc == None) return 0;

383
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
384
    {
385
      frame = XCAR (tail);
386
      if (!FRAMEP (frame))
387 388
        continue;
      f = XFRAME (frame);
389
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
390
	continue;
391
      x = f->output_data.x;
392 393 394 395

      if (x->widget)
	{
	  /* This frame matches if the window is its topmost widget.  */
Jan Djärv's avatar
Jan Djärv committed
396
#ifdef USE_GTK
Kenichi Handa's avatar
Kenichi Handa committed
397
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
398 399 400
          if (gwdesc == x->widget)
            return f;
#else
401 402
	  if (wdesc == XtWindow (x->widget))
	    return f;
403 404 405
#if 0 /* I don't know why it did this,
	 but it seems logically wrong,
	 and it causes trouble for MapNotify events.  */
406
	  /* Match if the window is this frame's menubar.  */
407
	  if (x->menubar_widget
408 409
	      && wdesc == XtWindow (x->menubar_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
410
#endif
411
#endif
412 413 414 415
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* Tooltip frame.  */
	return f;
416 417 418
    }
  return 0;
}
Jan Djärv's avatar
Jan Djärv committed
419
#endif /* USE_X_TOOLKIT || USE_GTK */
Jim Blandy's avatar
Jim Blandy committed
420

421 422


423
/* Store the screen positions of frame F into XPTR and YPTR.
424 425 426 427
   These are the positions of the containing window manager window,
   not Emacs's own window.  */

void
428
x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
429
{
430
  int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
431 432 433
  int real_x = 0, real_y = 0;
  int had_errors = 0;
  Window win = f->output_data.x->parent_desc;
434 435
  Atom actual_type;
  unsigned long actual_size, bytes_remaining;
436
  int rc, actual_format;
437 438 439 440 441
  struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
  long max_len = 400;
  Display *dpy = FRAME_X_DISPLAY (f);
  unsigned char *tmp_data = NULL;
  Atom target_type = XA_CARDINAL;
442

443 444
  BLOCK_INPUT;

445
  x_catch_errors (dpy);
446

447
  if (win == dpyinfo->root_window)
448 449 450 451 452 453 454
    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 (;;)
455
    {
456 457 458
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
459
      int success;
460

461 462
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
463

464
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
465

466 467 468 469 470 471
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

      XFree ((char *) tmp_children);

472 473
      if (wm_window == rootw || had_errors)
        break;
474

475 476
      win = wm_window;
    }
477

478 479
  if (! had_errors)
    {
480
      unsigned int ign;
481
      Window child, rootw;
482

483 484 485 486 487 488 489 490 491 492 493 494 495 496
      /* 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
      */
497
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
498

499 500
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
501
                             FRAME_X_WINDOW (f),
502

503
			     /* From-position, to-position.  */
504
                             real_x, real_y, &win_x, &win_y,
505

506 507
			     /* Child of win.  */
			     &child);
508

509
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
510
	{
511 512
          outer_x = win_x;
          outer_y = win_y;
513
	}
514 515 516
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
517

518 519 520
                                 /* From-window, to-window.  */
                                 FRAME_X_DISPLAY_INFO (f)->root_window,
                                 FRAME_OUTER_WINDOW (f),
521

522 523
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
524

525 526
                                 /* Child of win.  */
                                 &child);
527
	}
528

529 530
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
531

532 533 534 535 536 537 538 539 540 541 542 543

  if (dpyinfo->root_window == f->output_data.x->parent_desc)
    {
      /* Try _NET_FRAME_EXTENTS if our parent is the root window.  */
      rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
                               0, max_len, False, target_type,
                               &actual_type, &actual_format, &actual_size,
                               &bytes_remaining, &tmp_data);

      if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
          && actual_size == 4 && actual_format == 32)
        {
544
          unsigned int ign;
545
          Window rootw;
546
          long *fe = (long *)tmp_data;
547 548 549 550 551 552 553 554 555 556 557 558

          XGetGeometry (FRAME_X_DISPLAY (f), win,
                        &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
          outer_x = -fe[0];
          outer_y = -fe[2];
          real_x -= fe[0];
          real_y -= fe[2];
        }
    }

  if (tmp_data) XFree (tmp_data);

559
  x_uncatch_errors ();
560

561 562 563
  UNBLOCK_INPUT;

  if (had_errors) return;
564

565 566 567 568 569
  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;
570 571 572

  *xptr = real_x;
  *yptr = real_y;
573 574
}

Jim Blandy's avatar
Jim Blandy committed
575

576

577 578 579 580

/* Gamma-correct COLOR on frame F.  */

void
581
gamma_correct (struct frame *f, XColor *color)
582 583 584 585 586 587 588 589 590 591
{
  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;
    }
}


592 593 594 595
/* 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.  */
596

Jim Blandy's avatar
Jim Blandy committed
597
int
598 599
x_defined_color (struct frame *f, const char *color_name,
		 XColor *color, int alloc_p)
Jim Blandy's avatar
Jim Blandy committed
600
{
601
  int success_p = 0;
602 603
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
604 605

  BLOCK_INPUT;
606 607 608 609 610
#ifdef USE_GTK
  success_p = xg_check_special_colors (f, color_name, color);
#endif
  if (!success_p)
    success_p = XParseColor (dpy, cmap, color_name, color);
611 612
  if (success_p && alloc_p)
    success_p = x_alloc_nearest_color (f, cmap, color);
Jim Blandy's avatar
Jim Blandy committed
613 614
  UNBLOCK_INPUT;

615
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
616 617
}

618 619 620 621

/* 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
622

623
static int
624
x_decode_color (FRAME_PTR f, Lisp_Object color_name, int mono_color)
Jim Blandy's avatar
Jim Blandy committed
625
{
626
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
627

628
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
629

630 631 632
#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.  */
633
  if (strcmp (SDATA (color_name), "black") == 0)
634
    return BLACK_PIX_DEFAULT (f);
635
  else if (strcmp (SDATA (color_name), "white") == 0)
636
    return WHITE_PIX_DEFAULT (f);
637
#endif
Jim Blandy's avatar
Jim Blandy committed
638

639
  /* Return MONO_COLOR for monochrome frames.  */
640
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
641
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
642

643
  /* x_defined_color is responsible for coping with failures
644
     by looking for a near-miss.  */
645
  if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
646 647
    return cdef.pixel;

648
  signal_error ("Undefined color", color_name);
Jim Blandy's avatar
Jim Blandy committed
649
}
650 651


Jim Blandy's avatar
Jim Blandy committed
652

Gerd Moellmann's avatar
Gerd Moellmann committed
653 654 655 656 657
/* 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
658
x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
Gerd Moellmann's avatar
Gerd Moellmann committed
659 660 661 662
{
  f->output_data.x->wait_for_wm = !NILP (new_value);
}

663 664 665 666 667 668 669 670 671 672 673
static void
x_set_tool_bar_position (struct frame *f,
                         Lisp_Object new_value,
                         Lisp_Object old_value)
{
  if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
      && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
    return;
  if (EQ (new_value, old_value)) return;

#ifdef USE_GTK
674
  if (xg_change_toolbar_position (f, new_value))
675 676 677 678
    f->tool_bar_position = new_value;
#endif
}

679 680
#ifdef USE_GTK

681 682
/* 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.  */
683 684

int
685
xg_set_icon (FRAME_PTR f, Lisp_Object file)
686
{
687 688
  int result = 0;
  Lisp_Object found;
689

690 691 692 693 694 695
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
696
      char *filename = SSDATA (found);
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
      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;
716
}
717 718

int
719
xg_set_icon_from_xpm_data (FRAME_PTR f, const char **data)
720
{
721
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
722 723

  if (!pixbuf)
724
    return 0;
725

726
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
727 728 729
  g_object_unref (pixbuf);
  return 1;
}
730 731
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
732

Jim Blandy's avatar
Jim Blandy committed
733
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
734 735
   to set individual parameters.

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

741
static void
742
x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
743
{
744 745
  struct x_output *x = f->output_data.x;
  unsigned long fg, old_fg;
746

747
  fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
748 749
  old_fg = FRAME_FOREGROUND_PIXEL (f);
  FRAME_FOREGROUND_PIXEL (f) = fg;
750

751
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
752
    {
753
      Display *dpy = FRAME_X_DISPLAY (f);
754

755 756 757
      BLOCK_INPUT;
      XSetForeground (dpy, x->normal_gc, fg);
      XSetBackground (dpy, x->reverse_gc, fg);
758

759 760 761 762 763 764
      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);
	}
765

Jim Blandy's avatar
Jim Blandy committed
766
      UNBLOCK_INPUT;
767

768
      update_face_from_frame_parameter (f, Qforeground_color, arg);
769

770
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
771
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
772
    }
773

774
  unload_color (f, old_fg);
Jim Blandy's avatar
Jim Blandy committed
775 776
}

777
static void
778
x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
779
{
780 781
  struct x_output *x = f->output_data.x;
  unsigned long bg;
Jim Blandy's avatar
Jim Blandy committed
782

783
  bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
784 785
  unload_color (f, FRAME_BACKGROUND_PIXEL (f));
  FRAME_BACKGROUND_PIXEL (f) = bg;
786

787
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
788
    {
789
      Display *dpy = FRAME_X_DISPLAY (f);
790

791 792 793 794 795 796
      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
797 798 799 800
#ifdef USE_GTK
      xg_set_background_color (f, bg);
#endif

801 802 803 804 805 806 807 808
#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)
	  {
809
	    Window window = XSCROLL_BAR (bar)->x_window;
810 811 812 813
	    XSetWindowBackground (dpy, window, bg);
	  }
      }
#endif /* USE_TOOLKIT_SCROLL_BARS */
Jim Blandy's avatar
Jim Blandy committed
814

815
      UNBLOCK_INPUT;
816
      update_face_from_frame_parameter (f, Qbackground_color, arg);
817

818
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
819
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
820 821 822
    }
}

823
static Cursor