xfns.c 176 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-2012  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 24
#include <unistd.h>

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

27 28
#define XLIB_ILLEGAL_ACCESS

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

Jim Blandy's avatar
Jim Blandy committed
49
#ifdef HAVE_X_WINDOWS
50

51 52
#include <sys/types.h>
#include <sys/stat.h>
Jim Blandy's avatar
Jim Blandy committed
53

54
#if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work.  */
55 56
#include "bitmaps/gray.xbm"
#else
Jim Blandy's avatar
Jim Blandy committed
57
#include <X11/bitmaps/gray>
58
#endif
Jim Blandy's avatar
Jim Blandy committed
59

60 61
#include "xsettings.h"

Jan Djärv's avatar
Jan Djärv committed
62 63 64 65
#ifdef USE_GTK
#include "gtkutil.h"
#endif

66 67 68
#ifdef USE_X_TOOLKIT
#include <X11/Shell.h>

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

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

#include "widget.h"

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

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

101 102 103 104
#ifdef USE_LUCID
#include "../lwlib/xlwmenu.h"
#endif

Juanma Barranquero's avatar
Juanma Barranquero committed
105
#if !defined (NO_EDITRES)
106
#define HACK_EDITRES
107
extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
Dan Nicolaescu's avatar
Dan Nicolaescu committed
108
#endif /* not defined NO_EDITRES */
109

110 111
/* Unique id counter for widgets created by the Lucid Widget Library.  */

112 113
extern LWLIB_ID widget_id_tick;

114 115 116 117
#ifdef USE_MOTIF

#endif /* USE_MOTIF */

118 119
#endif /* USE_X_TOOLKIT */

Kenichi Handa's avatar
Kenichi Handa committed
120 121 122 123
#ifdef USE_GTK

#endif /* USE_GTK */

Richard M. Stallman's avatar
Richard M. Stallman committed
124 125
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))

126
/* Nonzero if using X.  */
127

128
int x_in_use;
Jim Blandy's avatar
Jim Blandy committed
129

130 131 132
static Lisp_Object Qsuppress_icon;
static Lisp_Object Qundefined_color;
static Lisp_Object Qcompound_text, Qcancel_timer;
133
Lisp_Object Qfont_param;
Jim Blandy's avatar
Jim Blandy committed
134

135
#ifdef GLYPH_DEBUG
136 137
static ptrdiff_t image_cache_refcount;
static int dpyinfo_refcount;
138 139
#endif

140 141
static struct x_display_info *x_display_info_for_name (Lisp_Object);

Jim Blandy's avatar
Jim Blandy committed
142

Richard M. Stallman's avatar
Richard M. Stallman committed
143
/* Error if we are not connected to X.  */
144

Karl Heuer's avatar
Karl Heuer committed
145
void
146
check_x (void)
Richard M. Stallman's avatar
Richard M. Stallman committed
147
{
148
  if (! x_in_use)
Richard M. Stallman's avatar
Richard M. Stallman committed
149 150 151
    error ("X windows are not in use or not initialized");
}

152 153
/* Nonzero if we can use mouse menus.
   You should not call this unless HAVE_MENUS is defined.  */
154 155

int
156
have_menus_p (void)
157
{
158 159 160 161 162 163 164
  return x_in_use;
}

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

FRAME_PTR
165
check_x_frame (Lisp_Object frame)
166
{
167
  struct frame *f = decode_live_frame (frame);
168 169

  if (! FRAME_X_P (f))
170
    error ("Non-X frame used");
171
  return f;
172 173
}

174
/* Let the user specify an X display with a Lisp object.
175
   OBJECT may be nil, a frame or a terminal object.
176 177 178
   nil stands for the selected frame--or, if that is not an X frame,
   the first X display on the list.  */

179
struct x_display_info *
180
check_x_display_info (Lisp_Object object)
181
{
182
  struct x_display_info *dpyinfo = NULL;
183

184
  if (NILP (object))
185
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
186
      struct frame *sf = XFRAME (selected_frame);
187

Gerd Moellmann's avatar
Gerd Moellmann committed
188
      if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
189
	dpyinfo = FRAME_X_DISPLAY_INFO (sf);
190
      else if (x_display_list != 0)
191
	dpyinfo = x_display_list;
192 193 194
      else
	error ("X windows are not in use or not initialized");
    }
195
  else if (TERMINALP (object))
196
    {
197
      struct terminal *t = get_terminal (object, 1);
198

199
      if (t->type != output_x_window)
200
        error ("Terminal %"pI"d is not an X display", XINT (object));
201

202
      dpyinfo = t->display_info.x;
203
    }
204 205
  else if (STRINGP (object))
    dpyinfo = x_display_info_for_name (object);
206 207
  else
    {
208
      FRAME_PTR f = check_x_frame (object);
209
      dpyinfo = FRAME_X_DISPLAY_INFO (f);
210
    }
211 212

  return dpyinfo;
213
}
214

215

Jim Blandy's avatar
Jim Blandy committed
216 217
/* 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
218

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

Jim Blandy's avatar
Jim Blandy committed
221
struct frame *
222
x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
Jim Blandy's avatar
Jim Blandy committed
223
{
Jim Blandy's avatar
Jim Blandy committed
224 225
  Lisp_Object tail, frame;
  struct frame *f;
Jim Blandy's avatar
Jim Blandy committed
226

227 228
  if (wdesc == None) return 0;

229
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
Jim Blandy's avatar
Jim Blandy committed
230
    {
231
      frame = XCAR (tail);
232
      if (!FRAMEP (frame))
Jim Blandy's avatar
Jim Blandy committed
233
        continue;
Jim Blandy's avatar
Jim Blandy committed
234
      f = XFRAME (frame);
235
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
236
	continue;
237
      if (f->output_data.x->hourglass_window == wdesc)
238
	return f;
239
#ifdef USE_X_TOOLKIT
240
      if ((f->output_data.x->edit_widget
241
	   && XtWindow (f->output_data.x->edit_widget) == wdesc)
242 243 244
	  /* A tooltip frame?  */
	  || (!f->output_data.x->edit_widget
	      && FRAME_X_WINDOW (f) == wdesc)
245
          || f->output_data.x->icon_desc == wdesc)
246 247
        return f;
#else /* not USE_X_TOOLKIT */
Jan Djärv's avatar
Jan Djärv committed
248 249 250
#ifdef USE_GTK
      if (f->output_data.x->edit_widget)
      {
Kenichi Handa's avatar
Kenichi Handa committed
251
        GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
252 253 254 255 256
        struct x_output *x = f->output_data.x;
        if (gwdesc != 0 && gwdesc == x->edit_widget)
          return f;
      }
#endif /* USE_GTK */
257
      if (FRAME_X_WINDOW (f) == wdesc
258
          || f->output_data.x->icon_desc == wdesc)
Jim Blandy's avatar
Jim Blandy committed
259
        return f;
260 261 262 263 264
#endif /* not USE_X_TOOLKIT */
    }
  return 0;
}

Jan Djärv's avatar
Jan Djärv committed
265
#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
266 267 268 269
/* Like x_window_to_frame but also compares the window with the widget's
   windows.  */

struct frame *
270
x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
271 272
{
  Lisp_Object tail, frame;
273
  struct frame *f, *found;
274
  struct x_output *x;
275

276 277
  if (wdesc == None) return NULL;

278
  found = NULL;
279
  for (tail = Vframe_list; CONSP (tail) && !found; tail = XCDR (tail))
280
    {
281
      frame = XCAR (tail);
282
      if (!FRAMEP (frame))
283
        continue;
284

285
      f = XFRAME (frame);
286
      if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
287
	{
288 289
	  /* This frame matches if the window is any of its widgets.  */
	  x = f->output_data.x;
290
	  if (x->hourglass_window == wdesc)
291 292 293
	    found = f;
	  else if (x->widget)
	    {
Jan Djärv's avatar
Jan Djärv committed
294
#ifdef USE_GTK
Kenichi Handa's avatar
Kenichi Handa committed
295
              GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
296
              if (gwdesc != 0
297
                  && gtk_widget_get_toplevel (gwdesc) == x->widget)
Jan Djärv's avatar
Jan Djärv committed
298 299
                found = f;
#else
300 301
	      if (wdesc == XtWindow (x->widget)
		  || wdesc == XtWindow (x->column_widget)
302 303 304 305 306
		  || 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
307
#endif
308 309 310 311
	    }
	  else if (FRAME_X_WINDOW (f) == wdesc)
	    /* A tooltip frame.  */
	    found = f;
312
	}
Jim Blandy's avatar
Jim Blandy committed
313
    }
314

315
  return found;
Jim Blandy's avatar
Jim Blandy committed
316
}
317

318 319 320
/* Likewise, but consider only the menu bar widget.  */

struct frame *
321
x_menubar_window_to_frame (struct x_display_info *dpyinfo, XEvent *event)
322
{
Jan D's avatar
Jan D committed
323
  Window wdesc = event->xany.window;
324 325
  Lisp_Object tail, frame;
  struct frame *f;
326
  struct x_output *x;
327

328 329
  if (wdesc == None) return 0;

330
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
331
    {
332
      frame = XCAR (tail);
333
      if (!FRAMEP (frame))
334 335
        continue;
      f = XFRAME (frame);
336
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
337
	continue;
338
      x = f->output_data.x;
Jan Djärv's avatar
Jan Djärv committed
339
#ifdef USE_GTK
Jan D's avatar
Jan D committed
340 341
      if (x->menubar_widget && xg_event_is_for_menubar (f, event))
        return f;
Jan Djärv's avatar
Jan Djärv committed
342
#else
Jan D's avatar
Jan D committed
343
      /* Match if the window is this frame's menubar.  */
344 345
      if (x->menubar_widget
	  && lw_window_is_in_menubar (wdesc, x->menubar_widget))
346
	return f;
Jan Djärv's avatar
Jan Djärv committed
347
#endif
348 349 350 351
    }
  return 0;
}

352 353 354 355
/* Return the frame whose principal (outermost) window is WDESC.
   If WDESC is some other (smaller) window, we return 0.  */

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

362 363
  if (wdesc == None) return 0;

364
  for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
365
    {
366
      frame = XCAR (tail);
367
      if (!FRAMEP (frame))
368 369
        continue;
      f = XFRAME (frame);
370
      if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
371
	continue;
372
      x = f->output_data.x;
373 374 375 376

      if (x->widget)
	{
	  /* This frame matches if the window is its topmost widget.  */
Jan Djärv's avatar
Jan Djärv committed
377
#ifdef USE_GTK
Kenichi Handa's avatar
Kenichi Handa committed
378
          GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
Jan Djärv's avatar
Jan Djärv committed
379 380 381
          if (gwdesc == x->widget)
            return f;
#else
382 383
	  if (wdesc == XtWindow (x->widget))
	    return f;
384 385 386
#if 0 /* I don't know why it did this,
	 but it seems logically wrong,
	 and it causes trouble for MapNotify events.  */
387
	  /* Match if the window is this frame's menubar.  */
388
	  if (x->menubar_widget
389 390
	      && wdesc == XtWindow (x->menubar_widget))
	    return f;
Jan Djärv's avatar
Jan Djärv committed
391
#endif
392
#endif
393 394 395 396
	}
      else if (FRAME_X_WINDOW (f) == wdesc)
	/* Tooltip frame.  */
	return f;
397 398 399
    }
  return 0;
}
Jan Djärv's avatar
Jan Djärv committed
400
#endif /* USE_X_TOOLKIT || USE_GTK */
Jim Blandy's avatar
Jim Blandy committed
401

402 403


404
/* Store the screen positions of frame F into XPTR and YPTR.
405 406 407 408
   These are the positions of the containing window manager window,
   not Emacs's own window.  */

void
409
x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
410
{
411
  int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
412 413 414
  int real_x = 0, real_y = 0;
  int had_errors = 0;
  Window win = f->output_data.x->parent_desc;
415 416
  Atom actual_type;
  unsigned long actual_size, bytes_remaining;
417
  int rc, actual_format;
418 419 420 421 422
  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;
423

424
  block_input ();
425

426
  x_catch_errors (dpy);
427

428
  if (win == dpyinfo->root_window)
429 430 431 432 433 434 435
    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 (;;)
436
    {
437 438 439
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
440
      int success;
441

442 443
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
444

445
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
446

447 448 449 450
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

451
      XFree (tmp_children);
452

453 454
      if (wm_window == rootw || had_errors)
        break;
455

456 457
      win = wm_window;
    }
458

459 460
  if (! had_errors)
    {
461
      unsigned int ign;
462
      Window child, rootw;
463

464 465 466 467 468 469 470 471 472 473 474 475 476 477
      /* 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
      */
478
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
479

480 481
			     /* From-window, to-window.  */
			     FRAME_X_DISPLAY_INFO (f)->root_window,
482
                             FRAME_X_WINDOW (f),
483

484
			     /* From-position, to-position.  */
485
                             real_x, real_y, &win_x, &win_y,
486

487 488
			     /* Child of win.  */
			     &child);
489

490
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
491
	{
492 493
          outer_x = win_x;
          outer_y = win_y;
494
	}
495 496 497
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
498

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

503 504
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
505

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

510 511
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
512

513 514 515 516 517 518 519 520 521 522 523 524

  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)
        {
525
          unsigned int ign;
526
          Window rootw;
527
          long *fe = (long *)tmp_data;
528 529 530 531 532 533 534 535 536 537 538 539

          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);

540
  x_uncatch_errors ();
541

542
  unblock_input ();
543 544

  if (had_errors) return;
545

546 547 548 549 550
  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;
551 552 553

  *xptr = real_x;
  *yptr = real_y;
554 555
}

Jim Blandy's avatar
Jim Blandy committed
556

557

558 559 560 561

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

void
562
gamma_correct (struct frame *f, XColor *color)
563 564 565 566 567 568 569 570 571 572
{
  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;
    }
}


573
/* Decide if color named COLOR_NAME is valid for use on frame F.  If
574 575
   so, return the RGB values in COLOR.  If ALLOC_P,
   allocate the color.  Value is false if COLOR_NAME is invalid, or
576
   no color could be allocated.  */
577

578
bool
579
x_defined_color (struct frame *f, const char *color_name,
580
		 XColor *color, bool alloc_p)
Jim Blandy's avatar
Jim Blandy committed
581
{
582
  bool success_p = 0;
583 584
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
585

586
  block_input ();
587 588 589 590
#ifdef USE_GTK
  success_p = xg_check_special_colors (f, color_name, color);
#endif
  if (!success_p)
591
    success_p = XParseColor (dpy, cmap, color_name, color) != 0;
592 593
  if (success_p && alloc_p)
    success_p = x_alloc_nearest_color (f, cmap, color);
594
  unblock_input ();
Jim Blandy's avatar
Jim Blandy committed
595

596
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
597 598
}

599 600 601 602

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

604
static int
605
x_decode_color (FRAME_PTR f, Lisp_Object color_name, int mono_color)
Jim Blandy's avatar
Jim Blandy committed
606
{
607
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
608

609
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
610

611 612 613
#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.  */
614
  if (strcmp (SDATA (color_name), "black") == 0)
615
    return BLACK_PIX_DEFAULT (f);
616
  else if (strcmp (SDATA (color_name), "white") == 0)
617
    return WHITE_PIX_DEFAULT (f);
618
#endif
Jim Blandy's avatar
Jim Blandy committed
619

620
  /* Return MONO_COLOR for monochrome frames.  */
621
  if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
622
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
623

624
  /* x_defined_color is responsible for coping with failures
625
     by looking for a near-miss.  */
626
  if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
627 628
    return cdef.pixel;

629
  signal_error ("Undefined color", color_name);
Jim Blandy's avatar
Jim Blandy committed
630
}
631 632


Jim Blandy's avatar
Jim Blandy committed
633

Gerd Moellmann's avatar
Gerd Moellmann committed
634 635 636 637 638
/* 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
639
x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
Gerd Moellmann's avatar
Gerd Moellmann committed
640 641 642 643
{
  f->output_data.x->wait_for_wm = !NILP (new_value);
}

644 645 646 647 648 649 650 651 652 653 654
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
655 656
  xg_change_toolbar_position (f, new_value);
  fset_tool_bar_position (f, new_value);
657 658 659
#endif
}

660 661
#ifdef USE_GTK

662 663
/* 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.  */
664 665

int
666
xg_set_icon (FRAME_PTR f, Lisp_Object file)
667
{
668 669
  int result = 0;
  Lisp_Object found;
670

671 672 673 674 675 676
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
677
      char *filename = SSDATA (found);
678
      block_input ();
679 680 681 682 683 684 685 686 687 688 689 690 691 692

      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);

693
      unblock_input ();
694 695 696
    }

  return result;
697
}
698 699

int
700
xg_set_icon_from_xpm_data (FRAME_PTR f, const char **data)
701
{
702
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
703 704

  if (!pixbuf)
705
    return 0;
706

707
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
708 709 710
  g_object_unref (pixbuf);
  return 1;
}
711 712
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
713

Jim Blandy's avatar
Jim Blandy committed
714
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
715 716
   to set individual parameters.

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

722
static void
723
x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
724
{
725 726
  struct x_output *x = f->output_data.x;
  unsigned long fg, old_fg;
727

728
  fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
729 730
  old_fg = FRAME_FOREGROUND_PIXEL (f);
  FRAME_FOREGROUND_PIXEL (f) = fg;
731

732
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
733
    {
734
      Display *dpy = FRAME_X_DISPLAY (f);
735

736
      block_input ();
737 738
      XSetForeground (dpy, x->normal_gc, fg);
      XSetBackground (dpy, x->reverse_gc, fg);
739

740 741 742 743 744 745
      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);
	}
746

747
      unblock_input ();
748

749
      update_face_from_frame_parameter (f, Qforeground_color, arg);
750

751
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
752
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed