gtkutil.c 132 KB
Newer Older
Jan Djärv's avatar
Jan Djärv committed
1
/* Functions for creating and updating GTK widgets.
Glenn Morris's avatar
Glenn Morris committed
2
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
Glenn Morris's avatar
Glenn Morris committed
3
     Free Software Foundation, Inc.
Jan Djärv's avatar
Jan Djärv committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Jan Djärv's avatar
Jan Djärv 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.
Jan Djärv's avatar
Jan Djärv 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/>.  */
Jan Djärv's avatar
Jan Djärv committed
19

20
#include <config.h>
Jan Djärv's avatar
Jan Djärv committed
21 22

#ifdef USE_GTK
23
#include <string.h>
24
#include <signal.h>
25
#include <stdio.h>
Jan Djärv's avatar
Jan Djärv committed
26 27 28
#include "lisp.h"
#include "xterm.h"
#include "blockinput.h"
29
#include "syssignal.h"
Jan Djärv's avatar
Jan Djärv committed
30 31 32 33
#include "window.h"
#include "atimer.h"
#include "gtkutil.h"
#include "termhooks.h"
34 35 36
#include "keyboard.h"
#include "charset.h"
#include "coding.h"
Jan Djärv's avatar
Jan Djärv committed
37 38
#include <gdk/gdkkeysyms.h>

39 40 41
#ifdef HAVE_XFT
#include <X11/Xft/Xft.h>
#endif
42

Jan Djärv's avatar
Jan Djärv committed
43
#define FRAME_TOTAL_PIXEL_HEIGHT(f) \
44
  (FRAME_PIXEL_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f))
Jan Djärv's avatar
Jan Djärv committed
45

46 47 48
/* Avoid "differ in sign" warnings */
#define SSDATA(x)  ((char *) SDATA (x))

49 50 51 52 53 54 55

/***********************************************************************
                      Display handling functions
 ***********************************************************************/

#ifdef HAVE_GTK_MULTIDISPLAY

56 57
/* Keep track of the default display, or NULL if there is none.  Emacs
   may close all its displays.  */
Jan Djärv's avatar
Jan Djärv committed
58 59 60

static GdkDisplay *gdpy_def;

61
/* Return the GdkDisplay that corresponds to the X display DPY.  */
62

63 64 65 66 67 68 69 70 71 72
static GdkDisplay *
xg_get_gdk_display (dpy)
     Display *dpy;
{
  return gdk_x11_lookup_xdisplay (dpy);
}

/* When the GTK widget W is to be created on a display for F that
   is not the default display, set the display for W.
   W can be a GtkMenu or a GtkWindow widget.  */
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static void
xg_set_screen (w, f)
     GtkWidget *w;
     FRAME_PTR f;
{
  if (FRAME_X_DISPLAY (f) != GDK_DISPLAY ())
    {
      GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
      GdkScreen *gscreen = gdk_display_get_default_screen (gdpy);

      if (GTK_IS_MENU (w))
        gtk_menu_set_screen (GTK_MENU (w), gscreen);
      else
        gtk_window_set_screen (GTK_WINDOW (w), gscreen);
    }
}


#else /* not HAVE_GTK_MULTIDISPLAY */

/* Make some defines so we can use the GTK 2.2 functions when
   compiling with GTK 2.0.  */
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#define xg_set_screen(w, f)
#define gdk_xid_table_lookup_for_display(dpy, w)    gdk_xid_table_lookup (w)
#define gdk_pixmap_foreign_new_for_display(dpy, p)  gdk_pixmap_foreign_new (p)
#define gdk_cursor_new_for_display(dpy, c)          gdk_cursor_new (c)
#define gdk_x11_lookup_xdisplay(dpy)                0
#define GdkDisplay                                  void

#endif /* not HAVE_GTK_MULTIDISPLAY */

/* Open a display named by DISPLAY_NAME.  The display is returned in *DPY.
   *DPY is set to NULL if the display can't be opened.

   Returns non-zero if display could be opened, zero if display could not
   be opened, and less than zero if the GTK version doesn't support
   multipe displays.  */
112

113 114 115 116 117 118 119 120 121
int
xg_display_open (display_name, dpy)
     char *display_name;
     Display **dpy;
{
#ifdef HAVE_GTK_MULTIDISPLAY
  GdkDisplay *gdpy;

  gdpy = gdk_display_open (display_name);
122 123 124 125 126 127
  if (!gdpy_def && gdpy)
    {
      gdpy_def = gdpy;
      gdk_display_manager_set_default_display (gdk_display_manager_get (),
					       gdpy);
    }
128

129
  *dpy = gdpy ? GDK_DISPLAY_XDISPLAY (gdpy) : NULL;
130 131 132 133 134 135 136 137 138
  return gdpy != NULL;

#else /* not HAVE_GTK_MULTIDISPLAY */

  return -1;
#endif /* not HAVE_GTK_MULTIDISPLAY */
}


139 140
/* Close display DPY.  */

141 142 143 144 145 146
void
xg_display_close (Display *dpy)
{
#ifdef HAVE_GTK_MULTIDISPLAY
  GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);

147 148 149
  /* If this is the default display, try to change it before closing.
     If there is no other display to use, gdpy_def is set to NULL, and
     the next call to xg_display_open resets the default display.  */
150 151 152
  if (gdk_display_get_default () == gdpy)
    {
      struct x_display_info *dpyinfo;
153
      GdkDisplay *gdpy_new = NULL;
154 155 156 157 158

      /* Find another display.  */
      for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
        if (dpyinfo->display != dpy)
          {
159 160 161
	    gdpy_new = gdk_x11_lookup_xdisplay (dpyinfo->display);
	    gdk_display_manager_set_default_display (gdk_display_manager_get (),
						     gdpy_new);
162 163
            break;
          }
164
      gdpy_def = gdpy_new;
165 166
    }

167
#if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 10
168 169 170
  /* GTK 2.2-2.8 has a bug that makes gdk_display_close crash (bug
     http://bugzilla.gnome.org/show_bug.cgi?id=85715).  This way we
     can continue running, but there will be memory leaks.  */
171
  g_object_run_dispose (G_OBJECT (gdpy));
172
#else
173
  /* This seems to be fixed in GTK 2.10. */
174 175 176 177
  gdk_display_close (gdpy);
#endif
#endif /* HAVE_GTK_MULTIDISPLAY */
}
Jan Djärv's avatar
Jan Djärv committed
178

Jan Djärv's avatar
Jan Djärv committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

/***********************************************************************
                      Utility functions
 ***********************************************************************/
/* The timer for scroll bar repetition and menu bar timeouts.
   NULL if no timer is started.  */
static struct atimer *xg_timer;


/* The next two variables and functions are taken from lwlib.  */
static widget_value *widget_value_free_list;
static int malloc_cpt;

/* Allocate a widget_value structure, either by taking one from the
   widget_value_free_list or by malloc:ing a new one.

   Return a pointer to the allocated structure.  */
196

Jan Djärv's avatar
Jan Djärv committed
197 198 199 200 201 202 203 204 205 206 207 208
widget_value *
malloc_widget_value ()
{
  widget_value *wv;
  if (widget_value_free_list)
    {
      wv = widget_value_free_list;
      widget_value_free_list = wv->free_list;
      wv->free_list = 0;
    }
  else
    {
209
      wv = (widget_value *) xmalloc (sizeof (widget_value));
Jan Djärv's avatar
Jan Djärv committed
210 211 212 213 214 215 216 217
      malloc_cpt++;
    }
  memset (wv, 0, sizeof (widget_value));
  return wv;
}

/* This is analogous to free.  It frees only what was allocated
   by malloc_widget_value, and no substructures.  */
218

Jan Djärv's avatar
Jan Djärv committed
219 220 221 222 223 224 225 226 227 228 229
void
free_widget_value (wv)
     widget_value *wv;
{
  if (wv->free_list)
    abort ();

  if (malloc_cpt > 25)
    {
      /* When the number of already allocated cells is too big,
	 We free it.  */
230
      xfree (wv);
Jan Djärv's avatar
Jan Djärv committed
231 232 233 234 235 236 237 238 239 240
      malloc_cpt--;
    }
  else
    {
      wv->free_list = widget_value_free_list;
      widget_value_free_list = wv;
    }
}


241 242
/* Create and return the cursor to be used for popup menus and
   scroll bars on display DPY.  */
243

244 245 246 247 248 249 250 251
GdkCursor *
xg_create_default_cursor (dpy)
     Display *dpy;
{
  GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
  return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
}

252 253
/* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel.  */

254
static GdkPixbuf *
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap)
     GdkPixmap *gpix;
     GdkPixmap *gmask;
     GdkColormap *cmap;
{
  int x, y, width, height, rowstride, mask_rowstride;
  GdkPixbuf *icon_buf, *tmp_buf;
  guchar *pixels;
  guchar *mask_pixels;

  gdk_drawable_get_size (gpix, &width, &height);
  tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
                                          0, 0, 0, 0, width, height);
  icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
  g_object_unref (G_OBJECT (tmp_buf));

  if (gmask)
    {
      GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
                                                          gmask,
                                                          NULL,
                                                          0, 0, 0, 0,
                                                          width, height);
      guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
      guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
      int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
      int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
      int y;

      for (y = 0; y < height; ++y)
        {
          guchar *iconptr, *maskptr;
          int x;

          iconptr = pixels + y * rowstride;
          maskptr = mask_pixels + y * mask_rowstride;

          for (x = 0; x < width; ++x)
            {
              /* In a bitmap, RGB is either 255/255/255 or 0/0/0.  Checking
                 just R is sufficient.  */
              if (maskptr[0] == 0)
                iconptr[3] = 0; /* 0, 1, 2 is R, G, B.  3 is alpha.  */

              iconptr += rowstride/width;
              maskptr += mask_rowstride/width;
            }
        }

      g_object_unref (G_OBJECT (mask_buf));
    }

  return icon_buf;
}

310
static Lisp_Object
311
file_for_image (image)
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
     Lisp_Object image;
{
  Lisp_Object specified_file = Qnil;
  Lisp_Object tail;
  extern Lisp_Object QCfile;

  for (tail = XCDR (image);
       NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
       tail = XCDR (XCDR (tail)))
    if (EQ (XCAR (tail), QCfile))
      specified_file = XCAR (XCDR (tail));

  return specified_file;
}

327 328 329 330 331 332 333 334 335 336
/* For the image defined in IMG, make and return a GtkImage.  For displays with
   8 planes or less we must make a GdkPixbuf and apply the mask manually.
   Otherwise the highlightning and dimming the tool bar code in GTK does
   will look bad.  For display with more than 8 planes we just use the
   pixmap and mask directly.  For monochrome displays, GTK doesn't seem
   able to use external pixmaps, it looks bad whatever we do.
   The image is defined on the display where frame F is.
   WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
   If OLD_WIDGET is NULL, a new widget is constructed and returned.
   If OLD_WIDGET is not NULL, that widget is modified.  */
337

338 339
static GtkWidget *
xg_get_image_for_pixmap (f, img, widget, old_widget)
340 341
     FRAME_PTR f;
     struct image *img;
342 343
     GtkWidget *widget;
     GtkImage *old_widget;
344
{
345 346
  GdkPixmap *gpix;
  GdkPixmap *gmask;
347
  GdkDisplay *gdpy;
348 349
  GdkColormap *cmap;
  GdkPixbuf *icon_buf;
350

351
  /* If we have a file, let GTK do all the image handling.
352
     This seems to be the only way to make insensitive and activated icons
353
     look good in all cases.  */
354
  Lisp_Object specified_file = file_for_image (img->spec);
355
  Lisp_Object file;
356

357 358 359
  /* We already loaded the image once before calling this
     function, so this only fails if the image file has been removed.
     In that case, use the pixmap already loaded.  */
360

361 362 363
  if (STRINGP (specified_file)
      && STRINGP (file = x_find_image_file (specified_file)))
    {
364 365 366 367 368 369
      if (! old_widget)
        old_widget = GTK_IMAGE (gtk_image_new_from_file (SSDATA (file)));
      else
        gtk_image_set_from_file (old_widget, SSDATA (file));

      return GTK_WIDGET (old_widget);
370
    }
371

372 373 374 375
  /* No file, do the image handling ourselves.  This will look very bad
     on a monochrome display, and sometimes bad on all displays with
     certain themes.  */

376
  gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
377 378 379
  gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
  gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;

380 381 382 383 384 385 386 387 388 389 390 391 392 393
  /* This is a workaround to make icons look good on pseudo color
     displays.  Apparently GTK expects the images to have an alpha
     channel.  If they don't, insensitive and activated icons will
     look bad.  This workaround does not work on monochrome displays,
     and is strictly not needed on true color/static color displays (i.e.
     16 bits and higher).  But we do it anyway so we get a pixbuf that is
     not associated with the img->pixmap.  The img->pixmap may be removed
     by clearing the image cache and then the tool bar redraw fails, since
     Gtk+ assumes the pixmap is always there.  */
  cmap = gtk_widget_get_colormap (widget);
  icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);

  if (! old_widget)
    old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
394
  else
395
    gtk_image_set_from_pixbuf (old_widget, icon_buf);
396

397
  g_object_unref (G_OBJECT (icon_buf));
398

399 400 401
  g_object_unref (G_OBJECT (gpix));
  if (gmask) g_object_unref (G_OBJECT (gmask));

402
  return GTK_WIDGET (old_widget);
403 404 405 406 407 408
}


/* Set CURSOR on W and all widgets W contain.  We must do like this
   for scroll bars and menu because they create widgets internally,
   and it is those widgets that are visible.  */
409

410
static void
Jan Djärv's avatar
Jan Djärv committed
411 412
xg_set_cursor (w, cursor)
     GtkWidget *w;
413
     GdkCursor *cursor;
Jan Djärv's avatar
Jan Djärv committed
414 415 416
{
  GList *children = gdk_window_peek_children (w->window);

417
  gdk_window_set_cursor (w->window, cursor);
Jan Djärv's avatar
Jan Djärv committed
418 419 420 421 422 423 424

  /* The scroll bar widget has more than one GDK window (had to look at
     the source to figure this out), and there is no way to set cursor
     on widgets in GTK.  So we must set the cursor for all GDK windows.
     Ditto for menus.  */

  for ( ; children; children = g_list_next (children))
425
    gdk_window_set_cursor (GDK_WINDOW (children->data), cursor);
Jan Djärv's avatar
Jan Djärv committed
426 427 428 429 430 431 432 433 434 435 436
}

/*  Timer function called when a timeout occurs for xg_timer.
    This function processes all GTK events in a recursive event loop.
    This is done because GTK timer events are not seen by Emacs event
    detection, Emacs only looks for X events.  When a scroll bar has the
    pointer (detected by button press/release events below) an Emacs
    timer is started, and this function can then check if the GTK timer
    has expired by calling the GTK event loop.
    Also, when a menu is active, it has a small timeout before it
    pops down the sub menu under it.  */
437

Jan Djärv's avatar
Jan Djärv committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451
static void
xg_process_timeouts (timer)
     struct atimer *timer;
{
  BLOCK_INPUT;
  /* Ideally we would like to just handle timer events, like the Xt version
     of this does in xterm.c, but there is no such feature in GTK.  */
  while (gtk_events_pending ())
    gtk_main_iteration ();
  UNBLOCK_INPUT;
}

/* Start the xg_timer with an interval of 0.1 seconds, if not already started.
   xg_process_timeouts is called when the timer expires.  The timer
452
   started is continuous, i.e. runs until xg_stop_timer is called.  */
453

Jan Djärv's avatar
Jan Djärv committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
static void
xg_start_timer ()
{
  if (! xg_timer)
    {
      EMACS_TIME interval;
      EMACS_SET_SECS_USECS (interval, 0, 100000);
      xg_timer = start_atimer (ATIMER_CONTINUOUS,
                               interval,
                               xg_process_timeouts,
                               0);
    }
}

/* Stop the xg_timer if started.  */
469

Jan Djärv's avatar
Jan Djärv committed
470 471 472 473 474 475 476 477 478 479 480
static void
xg_stop_timer ()
{
  if (xg_timer)
    {
      cancel_atimer (xg_timer);
      xg_timer = 0;
    }
}

/* Insert NODE into linked LIST.  */
481

Jan Djärv's avatar
Jan Djärv committed
482 483 484 485
static void
xg_list_insert (xg_list_node *list, xg_list_node *node)
{
  xg_list_node *list_start = list->next;
486

Jan Djärv's avatar
Jan Djärv committed
487 488 489 490 491 492 493
  if (list_start) list_start->prev = node;
  node->next = list_start;
  node->prev = 0;
  list->next = node;
}

/* Remove NODE from linked LIST.  */
494

Jan Djärv's avatar
Jan Djärv committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
static void
xg_list_remove (xg_list_node *list, xg_list_node *node)
{
  xg_list_node *list_start = list->next;
  if (node == list_start)
    {
      list->next = node->next;
      if (list->next) list->next->prev = 0;
    }
  else
    {
      node->prev->next = node->next;
      if (node->next) node->next->prev = node->prev;
    }
}

/* Allocate and return a utf8 version of STR.  If STR is already
   utf8 or NULL, just return STR.
   If not, a new string is allocated and the caller must free the result
   with g_free.  */
515

Jan Djärv's avatar
Jan Djärv committed
516 517 518 519 520
static char *
get_utf8_string (str)
     char *str;
{
  char *utf8_str = str;
521

522 523
  if (!str) return NULL;

Jan Djärv's avatar
Jan Djärv committed
524
  /* If not UTF-8, try current locale.  */
525
  if (!g_utf8_validate (str, -1, NULL))
Jan Djärv's avatar
Jan Djärv committed
526 527
    utf8_str = g_locale_to_utf8 (str, -1, 0, 0, 0);

Juanma Barranquero's avatar
Juanma Barranquero committed
528
  if (!utf8_str)
529 530 531 532 533 534 535 536 537
    {
      /* Probably some control characters in str.  Escape them. */
      size_t nr_bad = 0;
      gsize bytes_read;
      gsize bytes_written;
      unsigned char *p = (unsigned char *)str;
      char *cp, *up;
      GError *error = NULL;

538 539
      while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
                                       &bytes_written, &error))
540 541 542 543 544 545 546 547
             && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
        {
          ++nr_bad;
          p += bytes_written+1;
          g_error_free (error);
          error = NULL;
        }

Juanma Barranquero's avatar
Juanma Barranquero committed
548
      if (error)
549 550 551 552 553 554 555
        {
          g_error_free (error);
          error = NULL;
        }
      if (cp) g_free (cp);

      up = utf8_str = xmalloc (strlen (str) + nr_bad * 4 + 1);
556
      p = (unsigned char *)str;
557

558
      while (! (cp = g_locale_to_utf8 ((char *)p, -1, &bytes_read,
559 560 561
                                       &bytes_written, &error))
             && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
        {
562
          strncpy (up, (char *)p, bytes_written);
563 564 565 566 567 568 569 570
          sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
          up[bytes_written+4] = '\0';
          up += bytes_written+4;
          p += bytes_written+1;
          g_error_free (error);
          error = NULL;
        }

Juanma Barranquero's avatar
Juanma Barranquero committed
571
      if (cp)
572 573 574 575
        {
          strcat (utf8_str, cp);
          g_free (cp);
        }
Juanma Barranquero's avatar
Juanma Barranquero committed
576
      if (error)
577 578 579 580 581
        {
          g_error_free (error);
          error = NULL;
        }
    }
Jan Djärv's avatar
Jan Djärv committed
582 583 584 585 586 587 588 589 590 591 592 593 594
  return utf8_str;
}



/***********************************************************************
    General functions for creating widgets, resizing, events, e.t.c.
 ***********************************************************************/

/* Make a geometry string and pass that to GTK.  It seems this is the
   only way to get geometry position right if the user explicitly
   asked for a position when starting Emacs.
   F is the frame we shall set geometry for.  */
595

Jan Djärv's avatar
Jan Djärv committed
596 597 598 599
static void
xg_set_geometry (f)
     FRAME_PTR f;
{
600
  if (f->size_hint_flags & USPosition)
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    {
      int left = f->left_pos;
      int xneg = f->size_hint_flags & XNegative;
      int top = f->top_pos;
      int yneg = f->size_hint_flags & YNegative;
      char geom_str[32];

      if (xneg)
        left = -left;
      if (yneg)
        top = -top;

      sprintf (geom_str, "=%dx%d%c%d%c%d",
               FRAME_PIXEL_WIDTH (f),
               FRAME_TOTAL_PIXEL_HEIGHT (f),
               (xneg ? '-' : '+'), left,
               (yneg ? '-' : '+'), top);

      if (!gtk_window_parse_geometry (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
                                      geom_str))
        fprintf (stderr, "Failed to parse: '%s'\n", geom_str);
    }
  else if (f->size_hint_flags & PPosition)
624 625
    gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
                     f->left_pos, f->top_pos);
Jan Djärv's avatar
Jan Djärv committed
626 627
}

628

629 630 631
/* Function to handle resize of our frame.  As we have a Gtk+ tool bar
   and a Gtk+ menu bar, we get resize events for the edit part of the
   frame only.  We let Gtk+ deal with the Gtk+ parts.
Jan Djärv's avatar
Jan Djärv committed
632 633
   F is the frame to resize.
   PIXELWIDTH, PIXELHEIGHT is the new size in pixels.  */
634

Jan Djärv's avatar
Jan Djärv committed
635
void
636
xg_frame_resized (f, pixelwidth, pixelheight)
Jan Djärv's avatar
Jan Djärv committed
637 638 639
     FRAME_PTR f;
     int pixelwidth, pixelheight;
{
640
  int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelheight);
641
  int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, pixelwidth);
642

Jan Djärv's avatar
Jan Djärv committed
643
  if (FRAME_GTK_WIDGET (f)
644
      && (columns != FRAME_COLS (f)
645
	  || rows != FRAME_LINES (f)
646
          || pixelwidth != FRAME_PIXEL_WIDTH (f)
647
	  || pixelheight != FRAME_PIXEL_HEIGHT (f)))
Jan Djärv's avatar
Jan Djärv committed
648
    {
649 650
      FRAME_PIXEL_WIDTH (f) = pixelwidth;
      FRAME_PIXEL_HEIGHT (f) = pixelheight;
Jan Djärv's avatar
Jan Djärv committed
651

652 653 654
      change_frame_size (f, rows, columns, 0, 1, 0);
      SET_FRAME_GARBAGED (f);
      cancel_mouse_face (f);
655 656
    }
}
Jan Djärv's avatar
Jan Djärv committed
657

658 659 660
/* Resize the outer window of frame F after chainging the height.
   This happend when the menu bar or the tool bar is added or removed.
   COLUMNS/ROWS is the size the edit area shall have after the resize.  */
661

Jan Djärv's avatar
Jan Djärv committed
662 663 664 665 666 667
void
xg_frame_set_char_size (f, cols, rows)
     FRAME_PTR f;
     int cols;
     int rows;
{
668
  int pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
Jan Djärv's avatar
Jan Djärv committed
669
    + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
670
  int pixelwidth;
671

672 673 674
  if (FRAME_PIXEL_HEIGHT (f) == 0)
    return;

Jan Djärv's avatar
Jan Djärv committed
675 676 677 678 679
  /* Take into account the size of the scroll bar.  Always use the
     number of columns occupied by the scroll bar here otherwise we
     might end up with a frame width that is not a multiple of the
     frame's character width which is bad for vertically split
     windows.  */
680 681
  f->scroll_bar_actual_width
    = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
Jan Djärv's avatar
Jan Djärv committed
682

683
  compute_fringe_widths (f, 0);
Jan Djärv's avatar
Jan Djärv committed
684

685
  /* FRAME_TEXT_COLS_TO_PIXEL_WIDTH uses scroll_bar_actual_width, so call it
686
     after calculating that value.  */
687
  pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
688

Jan Djärv's avatar
Jan Djärv committed
689
  /* Must resize our top level widget.  Font size may have changed,
690
     but not rows/cols.  */
Jan Djärv's avatar
Jan Djärv committed
691 692
  gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
                     pixelwidth, pixelheight);
693
  x_wm_set_size_hint (f, 0, 0);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

  /* Now, strictly speaking, we can't be sure that this is accurate,
     but the window manager will get around to dealing with the size
     change request eventually, and we'll hear how it went when the
     ConfigureNotify event gets here.

     We could just not bother storing any of this information here,
     and let the ConfigureNotify event set everything up, but that
     might be kind of confusing to the Lisp code, since size changes
     wouldn't be reported in the frame parameters until some random
     point in the future when the ConfigureNotify event arrives.

     We pass 1 for DELAY since we can't run Lisp code inside of
     a BLOCK_INPUT.  */
  change_frame_size (f, rows, cols, 0, 1, 0);
  FRAME_PIXEL_WIDTH (f) = pixelwidth;
  FRAME_PIXEL_HEIGHT (f) = pixelheight;

  /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
     receive in the ConfigureNotify event; if we get what we asked
     for, then the event won't cause the screen to become garbaged, so
     we have to make sure to do it here.  */
  SET_FRAME_GARBAGED (f);
717 718 719 720 721 722 723 724 725 726 727 728
}

/* Handle height changes (i.e. add/remove menu/toolbar).
   The policy is to keep the number of editable lines.  */

static void
xg_height_changed (f)
     FRAME_PTR f;
{
  gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
                     FRAME_PIXEL_WIDTH (f), FRAME_TOTAL_PIXEL_HEIGHT (f));
  f->output_data.x->hint_flags = 0;
729
  x_wm_set_size_hint (f, 0, 0);
Jan Djärv's avatar
Jan Djärv committed
730 731
}

732
/* Convert an X Window WSESC on display DPY to its corresponding GtkWidget.
Jan Djärv's avatar
Jan Djärv committed
733 734 735 736
   Must be done like this, because GtkWidget:s can have "hidden"
   X Window that aren't accessible.

   Return 0 if no widget match WDESC.  */
737

Jan Djärv's avatar
Jan Djärv committed
738
GtkWidget *
739 740
xg_win_to_widget (dpy, wdesc)
     Display *dpy;
Jan Djärv's avatar
Jan Djärv committed
741 742 743 744 745 746
     Window wdesc;
{
  gpointer gdkwin;
  GtkWidget *gwdesc = 0;

  BLOCK_INPUT;
747 748 749

  gdkwin = gdk_xid_table_lookup_for_display (gdk_x11_lookup_xdisplay (dpy),
                                             wdesc);
Jan Djärv's avatar
Jan Djärv committed
750 751 752 753 754 755
  if (gdkwin)
    {
      GdkEvent event;
      event.any.window = gdkwin;
      gwdesc = gtk_get_event_widget (&event);
    }
756

Jan Djärv's avatar
Jan Djärv committed
757 758 759 760 761 762
  UNBLOCK_INPUT;
  return gwdesc;
}

/* Fill in the GdkColor C so that it represents PIXEL.
   W is the widget that color will be used for.  Used to find colormap.  */
763

Jan Djärv's avatar
Jan Djärv committed
764 765 766 767 768 769 770 771 772 773 774 775
static void
xg_pix_to_gcolor (w, pixel, c)
     GtkWidget *w;
     unsigned long pixel;
     GdkColor *c;
{
  GdkColormap *map = gtk_widget_get_colormap (w);
  gdk_colormap_query_color (map, pixel, c);
}

/* Create and set up the GTK widgets for frame F.
   Return 0 if creation failed, non-zero otherwise.  */
776

Jan Djärv's avatar
Jan Djärv committed
777 778 779 780 781 782 783 784 785 786 787
int
xg_create_frame_widgets (f)
     FRAME_PTR f;
{
  GtkWidget *wtop;
  GtkWidget *wvbox;
  GtkWidget *wfixed;
  GdkColor bg;
  GtkRcStyle *style;
  int i;
  char *title = 0;
788

Jan Djärv's avatar
Jan Djärv committed
789 790
  BLOCK_INPUT;

791 792 793 794 795
  if (FRAME_X_EMBEDDED_P (f))
    wtop = gtk_plug_new (f->output_data.x->parent_desc);
  else
    wtop = gtk_window_new (GTK_WINDOW_TOPLEVEL);

796 797
  xg_set_screen (wtop, f);

Jan Djärv's avatar
Jan Djärv committed
798 799
  wvbox = gtk_vbox_new (FALSE, 0);
  wfixed = gtk_fixed_new ();  /* Must have this to place scroll bars  */
800

Jan Djärv's avatar
Jan Djärv committed
801 802 803 804 805 806
  if (! wtop || ! wvbox || ! wfixed)
    {
      if (wtop) gtk_widget_destroy (wtop);
      if (wvbox) gtk_widget_destroy (wvbox);
      if (wfixed) gtk_widget_destroy (wfixed);

807
      UNBLOCK_INPUT;
Jan Djärv's avatar
Jan Djärv committed
808 809 810 811 812 813
      return 0;
    }

  /* Use same names as the Xt port does.  I.e. Emacs.pane.emacs by default */
  gtk_widget_set_name (wtop, EMACS_CLASS);
  gtk_widget_set_name (wvbox, "pane");
814
  gtk_widget_set_name (wfixed, SSDATA (Vx_resource_name));
Jan Djärv's avatar
Jan Djärv committed
815 816

  /* If this frame has a title or name, set it in the title bar.  */
817 818
  if (! NILP (f->title)) title = SSDATA (ENCODE_UTF_8 (f->title));
  else if (! NILP (f->name)) title = SSDATA (ENCODE_UTF_8 (f->name));
Jan Djärv's avatar
Jan Djärv committed
819 820

  if (title) gtk_window_set_title (GTK_WINDOW (wtop), title);
821

Jan Djärv's avatar
Jan Djärv committed
822 823 824 825 826 827 828 829 830 831 832 833
  FRAME_GTK_OUTER_WIDGET (f) = wtop;
  FRAME_GTK_WIDGET (f) = wfixed;
  f->output_data.x->vbox_widget = wvbox;

  gtk_fixed_set_has_window (GTK_FIXED (wfixed), TRUE);

  gtk_container_add (GTK_CONTAINER (wtop), wvbox);
  gtk_box_pack_end (GTK_BOX (wvbox), wfixed, TRUE, TRUE, 0);

  if (FRAME_EXTERNAL_TOOL_BAR (f))
    update_frame_tool_bar (f);

834 835 836 837 838
  /* We don't want this widget double buffered, because we draw on it
     with regular X drawing primitives, so from a GTK/GDK point of
     view, the widget is totally blank.  When an expose comes, this
     will make the widget blank, and then Emacs redraws it.  This flickers
     a lot, so we turn off double buffering.  */
Jan Djärv's avatar
Jan Djärv committed
839
  gtk_widget_set_double_buffered (wfixed, FALSE);
840

Jan Djärv's avatar
Jan Djärv committed
841
  gtk_window_set_wmclass (GTK_WINDOW (wtop),
842 843
                          SSDATA (Vx_resource_name),
                          SSDATA (Vx_resource_class));
Jan Djärv's avatar
Jan Djärv committed
844 845 846 847 848

  /* Add callback to do nothing on WM_DELETE_WINDOW.  The default in
     GTK is to destroy the widget.  We want Emacs to do that instead.  */
  g_signal_connect (G_OBJECT (wtop), "delete-event",
                    G_CALLBACK (gtk_true), 0);
849

Jan Djärv's avatar
Jan Djärv committed
850 851 852 853
  /* Convert our geometry parameters into a geometry string
     and specify it.
     GTK will itself handle calculating the real position this way.  */
  xg_set_geometry (f);
854 855
  int grav = gtk_window_get_gravity (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
  f->win_gravity = grav;
Jan Djärv's avatar
Jan Djärv committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875

  gtk_widget_add_events (wfixed,
                         GDK_POINTER_MOTION_MASK
                         | GDK_EXPOSURE_MASK
                         | GDK_BUTTON_PRESS_MASK
                         | GDK_BUTTON_RELEASE_MASK
                         | GDK_KEY_PRESS_MASK
                         | GDK_ENTER_NOTIFY_MASK
                         | GDK_LEAVE_NOTIFY_MASK
                         | GDK_FOCUS_CHANGE_MASK
                         | GDK_STRUCTURE_MASK
                         | GDK_VISIBILITY_NOTIFY_MASK);

  /* Must realize the windows so the X window gets created.  It is used
     by callers of this function.  */
  gtk_widget_realize (wfixed);
  FRAME_X_WINDOW (f) = GTK_WIDGET_TO_X_WIN (wfixed);

  /* Since GTK clears its window by filling with the background color,
     we must keep X and GTK background in sync.  */
Karoly Lorentey's avatar
Karoly Lorentey committed
876
  xg_pix_to_gcolor (wfixed, FRAME_BACKGROUND_PIXEL (f), &bg);
Jan Djärv's avatar
Jan Djärv committed
877 878 879 880 881 882 883 884 885 886
  gtk_widget_modify_bg (wfixed, GTK_STATE_NORMAL, &bg);

  /* Also, do not let any background pixmap to be set, this looks very
     bad as Emacs overwrites the background pixmap with its own idea
     of background color.  */
  style = gtk_widget_get_modifier_style (wfixed);

  /* Must use g_strdup because gtk_widget_modify_style does g_free.  */
  style->bg_pixmap_name[GTK_STATE_NORMAL] = g_strdup ("<none>");
  gtk_widget_modify_style (wfixed, style);
887

Jan Djärv's avatar
Jan Djärv committed
888
  /* GTK does not set any border, and they look bad with GTK.  */
889 890
  /* That they look bad is no excuse for imposing this here.  --Stef
     It should be done by providing the proper default in Fx_create_Frame.
891
  f->border_width = 0;
892
  f->internal_border_width = 0; */
Jan Djärv's avatar
Jan Djärv committed
893 894 895 896 897 898 899 900 901 902 903

  UNBLOCK_INPUT;

  return 1;
}

/* Set the normal size hints for the window manager, for frame F.
   FLAGS is the flags word to use--or 0 meaning preserve the flags
   that the window now has.
   If USER_POSITION is nonzero, we set the User Position
   flag (this is useful when FLAGS is 0).  */
904

Jan Djärv's avatar
Jan Djärv committed
905 906 907 908 909 910
void
x_wm_set_size_hint (f, flags, user_position)
     FRAME_PTR f;
     long flags;
     int user_position;
{
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
  /* Don't set size hints during initialization; that apparently leads
     to a race condition.  See the thread at
     http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00033.html  */
  if (NILP (Vafter_init_time) || !FRAME_GTK_OUTER_WIDGET (f))
    return;

  /* Must use GTK routines here, otherwise GTK resets the size hints
     to its own defaults.  */
  GdkGeometry size_hints;
  gint hint_flags = 0;
  int base_width, base_height;
  int min_rows = 0, min_cols = 0;
  int win_gravity = f->win_gravity;

  if (flags)
    {
      memset (&size_hints, 0, sizeof (size_hints));
      f->output_data.x->size_hints = size_hints;
      f->output_data.x->hint_flags = hint_flags;
    }
  else
    flags = f->size_hint_flags;

  size_hints = f->output_data.x->size_hints;
  hint_flags = f->output_data.x->hint_flags;

  hint_flags |= GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE;
  size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
  size_hints.height_inc = FRAME_LINE_HEIGHT (f);

  hint_flags |= GDK_HINT_BASE_SIZE;
  base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
  base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0)
    + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);

  check_frame_size (f, &min_rows, &min_cols);

  size_hints.base_width = base_width;
  size_hints.base_height = base_height;
  size_hints.min_width  = base_width + min_cols * size_hints.width_inc;
  size_hints.min_height = base_height + min_rows * size_hints.height_inc;

  /* These currently have a one to one mapping with the X values, but I
     don't think we should rely on that.  */
  hint_flags |= GDK_HINT_WIN_GRAVITY;
  size_hints.win_gravity = 0;
  if (win_gravity == NorthWestGravity)
    size_hints.win_gravity = GDK_GRAVITY_NORTH_WEST;
  else if (win_gravity == NorthGravity)
    size_hints.win_gravity = GDK_GRAVITY_NORTH;
  else if (win_gravity == NorthEastGravity)
    size_hints.win_gravity = GDK_GRAVITY_NORTH_EAST;
  else if (win_gravity == WestGravity)
    size_hints.win_gravity = GDK_GRAVITY_WEST;
  else if (win_gravity == CenterGravity)
    size_hints.win_gravity = GDK_GRAVITY_CENTER;
  else if (win_gravity == EastGravity)
    size_hints.win_gravity = GDK_GRAVITY_EAST;
  else if (win_gravity == SouthWestGravity)
    size_hints.win_gravity = GDK_GRAVITY_SOUTH_WEST;
  else if (win_gravity == SouthGravity)
    size_hints.win_gravity = GDK_GRAVITY_SOUTH;
  else if (win_gravity == SouthEastGravity)
    size_hints.win_gravity = GDK_GRAVITY_SOUTH_EAST;
  else if (win_gravity == StaticGravity)
    size_hints.win_gravity = GDK_GRAVITY_STATIC;

  if (flags & PPosition) hint_flags |= GDK_HINT_POS;
  if (flags & USPosition) hint_flags |= GDK_HINT_USER_POS;
  if (flags & USSize) hint_flags |= GDK_HINT_USER_SIZE;

  if (user_position)
    {
      hint_flags &= ~GDK_HINT_POS;
      hint_flags |= GDK_HINT_USER_POS;
    }

  if (hint_flags != f->output_data.x->hint_flags
      || memcmp (&size_hints,
		 &f->output_data.x->size_hints,
		 sizeof (size_hints)) != 0)
    {
      BLOCK_INPUT;
      gtk_window_set_geometry_hints (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
				     NULL, &size_hints, hint_flags);
      f->output_data.x->size_hints = size_hints;
      f->output_data.x->hint_flags = hint_flags;
      UNBLOCK_INPUT;
    }
Jan Djärv's avatar
Jan Djärv committed
1000 1001 1002
}

/* Change background color of a frame.
Glenn Morris's avatar
Glenn Morris committed
1003
   Since GTK uses the background color to clear the window, we must
Jan Djärv's avatar
Jan Djärv committed
1004 1005 1006
   keep the GTK and X colors in sync.
   F is the frame to change,
   BG is the pixel value to change to.  */
1007

Jan Djärv's avatar
Jan Djärv committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
void
xg_set_background_color (f, bg)
     FRAME_PTR f;
     unsigned long bg;
{
  if (FRAME_GTK_WIDGET (f))
    {
      GdkColor gdk_bg;

      BLOCK_INPUT;
      xg_pix_to_gcolor (FRAME_GTK_WIDGET (f), bg, &gdk_bg);
      gtk_widget_modify_bg (FRAME_GTK_WIDGET (f), GTK_STATE_NORMAL, &gdk_bg);
      UNBLOCK_INPUT;
    }
}


1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
/* Set the frame icon to ICON_PIXMAP/MASK.  This must be done with GTK
   functions so GTK does not overwrite the icon.  */

void
xg_set_frame_icon (f, icon_pixmap, icon_mask)
     FRAME_PTR f;
     Pixmap icon_pixmap;
     Pixmap icon_mask;
{
    GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
    GdkPixmap *gpix = gdk_pixmap_foreign_new_for_display (gdpy, icon_pixmap);
    GdkPixmap *gmask = gdk_pixmap_foreign_new_for_display (gdpy, icon_mask);
    GdkPixbuf *gp = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, NULL);

    gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), gp);
}


Jan Djärv's avatar
Jan Djärv committed
1043 1044 1045 1046 1047 1048

/***********************************************************************
                      Dialog functions
 ***********************************************************************/
/* Return the dialog title to use for a dialog of type KEY.
   This is the encoding used by lwlib.  We use the same for GTK.  */
1049

Jan Djärv's avatar
Jan Djärv committed
1050 1051 1052 1053
static char *
get_dialog_title (char key)
{
  char *title = "";
1054

Jan Djärv's avatar
Jan Djärv committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
  switch (key) {
  case 'E': case 'e':
    title = "Error";
    break;

  case 'I': case 'i':
    title = "Information";
    break;

  case 'L': case 'l':
    title = "Prompt";
    break;

  case 'P': case 'p':
    title = "Prompt";
    break;

  case 'Q': case 'q':
    title = "Question";
    break;
  }

  return title;
}

/* Callback for dialogs that get WM_DELETE_WINDOW.  We pop down
   the dialog, but return TRUE so the event does not propagate further
   in GTK.  This prevents GTK from destroying the dialog widget automatically
   and we can always destrou the widget manually, regardles of how
   it was popped down (button press or WM_DELETE_WINDOW).
   W is the dialog widget.
   EVENT is the GdkEvent that represents WM_DELETE_WINDOW (not used).
   user_data is NULL (not used).

   Returns TRUE to end propagation of event.  */
1090

Jan Djärv's avatar
Jan Djärv committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
static gboolean
dialog_delete_callback (w, event, user_data)
     GtkWidget *w;
     GdkEvent *event;
     gpointer user_data;
{
  gtk_widget_unmap (w);
  return TRUE;
}

/* Create a popup dialog window.  See also xg_create_widget below.
   WV is a widget_value describing the dialog.
   SELECT_CB is the callback to use when a button has been pressed.
   DEACTIVATE_CB is the callback to use when the dialog pops down.

   Returns the GTK dialog widget.  */
1107

Jan Djärv's avatar
Jan Djärv committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
static GtkWidget *
create_dialog (wv, select_cb, deactivate_cb)
     widget_value *wv;
     GCallback select_cb;
     GCallback deactivate_cb;
{
  char *title = get_dialog_title (wv->name[0]);
  int total_buttons = wv->name[1] - '0';
  int right_buttons = wv->name[4] - '0';
  int left_buttons;
  int button_nr = 0;
  int button_spacing = 10;
  GtkWidget *wdialog = gtk_dialog_new ();
  widget_value *item;
  GtkBox *cur_box;
  GtkWidget *wvbox;
  GtkWidget *whbox_up;
  GtkWidget *whbox_down;

  /* If the number of buttons is greater than 4, make two rows of buttons
     instead.  This looks better.  */
  int make_two_rows = total_buttons > 4;

  if (right_buttons == 0) right_buttons = total_buttons/2;
  left_buttons = total_buttons - right_buttons;

  gtk_window_set_title (GTK_WINDOW (wdialog), title);
  gtk_widget_set_name (wdialog, "emacs-dialog");

  cur_box = GTK_BOX (GTK_DIALOG (wdialog)->action_area);

  if (make_two_rows)
    {
      wvbox = gtk_vbox_new (TRUE, button_spacing);
      whbox_up = gtk_hbox_new (FALSE, 0);
      whbox_down = gtk_hbox_new (FALSE, 0);

      gtk_box_pack_start (cur_box, wvbox, FALSE, FALSE, 0);
      gtk_box_pack_start (GTK_BOX (wvbox), whbox_up, FALSE, FALSE, 0);
      gtk_box_pack_start (GTK_BOX (wvbox), whbox_down, FALSE, FALSE, 0);

      cur_box = GTK_BOX (whbox_up);
    }

  g_signal_connect (G_OBJECT (wdialog), "delete-event",
                    G_CALLBACK (dialog_delete_callback), 0);
1154

Jan Djärv's avatar
Jan Djärv committed
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
  if (deactivate_cb)
    {
      g_signal_connect (G_OBJECT (wdialog), "close", deactivate_cb, 0);
      g_signal_connect (G_OBJECT (wdialog), "response", deactivate_cb, 0);
    }

  for (item = wv->contents; item; item = item->next)
    {
      char *utf8_label = get_utf8_string (item->value);
      GtkWidget *w;
      GtkRequisition req;

1167
      if (item->name && strcmp (item->name, "message") == 0)
Jan Djärv's avatar
Jan Djärv committed
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
        {
          /* This is the text part of the dialog.  */
          w = gtk_label_new (utf8_label);
          gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
                              gtk_label_new (""),
                              FALSE, FALSE, 0);
          gtk_box_pack_start (GTK_BOX (GTK_DIALOG (wdialog)->vbox), w,
                              TRUE, TRUE, 0);
          gtk_misc_set_alignment (GTK_MISC (w), 0.1, 0.5);

          /* Try to make dialog look better.  Must realize first so
             the widget can calculate the size it needs.  */
          gtk_widget_realize (w);
          gtk_widget_size_request (w, &req);
          gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (wdialog)->vbox),
                               req.height);
1184
	  if (item->value && strlen (item->value) > 0)
Jan Djärv's avatar
Jan Djärv committed
1185 1186 1187 1188 1189
            button_spacing = 2*req.width/strlen (item->value);
        }
      else
        {
          /* This is one button to add to the dialog.  */
1190
          w = gtk_button_new_with_label (utf8_label);
Jan Djärv's avatar
Jan Djärv committed
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
          if (! item->enabled)
            gtk_widget_set_sensitive (w, FALSE);
          if (select_cb)
            g_signal_connect (G_OBJECT (w), "clicked",
                              select_cb, item->call_data);

          gtk_box_pack_start (cur_box, w, TRUE, TRUE, button_spacing);
          if (++button_nr == left_buttons)
            {
              if (make_two_rows)
                cur_box = GTK_BOX (whbox_down);
              else
                gtk_box_pack_start (cur_box,
                                    gtk_label_new (""),
                                    TRUE, TRUE,
                                    button_spacing);
            }
        }

     if (utf8_label && utf8_label != item->value)
       g_free (utf8_label);
    }

  return wdialog;
}


1218 1219 1220 1221

/***********************************************************************
                      File dialog functions
 ***********************************************************************/
1222 1223 1224 1225 1226 1227 1228
/* Return non-zero if the old file selection dialog is being used.
   Return zero if not.  */

int
xg_uses_old_file_dialog ()
{
#ifdef HAVE_GTK_FILE_BOTH
1229 1230
  extern int x_gtk_use_old_file_dialog;
  return x_gtk_use_old_file_dialog;
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
#else /* ! HAVE_GTK_FILE_BOTH */

#ifdef HAVE_GTK_FILE_SELECTION_NEW
  return 1;
#else
  return 0;
#endif

#endif /* ! HAVE_GTK_FILE_BOTH */
}


1243
/* Function that is called when the file or font dialogs pop down.
1244 1245 1246 1247
   W is the dialog widget, RESPONSE is the response code.
   USER_DATA is what we passed in to g_signal_connect (pointer to int).  */

static void
1248
xg_dialog_response_cb (w,
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
                     response,
                     user_data)
     GtkDialog *w;
     gint response;
     gpointer user_data;
{
  int *ptr = (int *) user_data;
  *ptr = response;
}


/*  Destroy the dialog.  This makes it pop down.  */

static Lisp_Object
1263
pop_down_dialog (arg)
1264 1265 1266
     Lisp_Object arg;
{
  struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1267
  BLOCK_INPUT;
1268
  gtk_widget_destroy (GTK_WIDGET (p->pointer));
1269
  UNBLOCK_INPUT;
1270 1271 1272 1273
  return Qnil;
}

typedef char * (*xg_get_file_func) P_ ((GtkWidget *));
1274 1275

#ifdef HAVE_GTK_FILE_CHOOSER_DIALOG_NEW
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286

/* Return the selected file for file chooser dialog W.
   The returned string must be free:d.  */

static char *
xg_get_file_name_from_chooser (w)
     GtkWidget *w;
{
  return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (w));
}

1287 1288 1289
/* Callback called when the "Show hidden files" toggle is pressed.
   WIDGET is the toggle widget, DATA is the file chooser dialog.  */

1290
static void
Jan Djärv's avatar
Jan Djärv committed
1291
xg_toggle_visibility_cb (widget, data)
1292 1293 1294 1295 1296 1297 1298
     GtkWidget *widget;
     gpointer data;
{
  GtkFileChooser *dialog = GTK_FILE_CHOOSER (data);
  gboolean visible;
  g_object_get (G_OBJECT (dialog), "show-hidden", &visible, NULL);
  g_object_set (G_OBJECT (dialog), "show-hidden", !visible, NULL);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
}


/* Callback called when a property changes in a file chooser.
   GOBJECT is the file chooser dialog, ARG1 describes the property.
   USER_DATA is the toggle widget in the file chooser dialog.
   We use this to update the "Show hidden files" toggle when the user
   changes that property by right clicking in the file list.  */

static void
xg_toggle_notify_cb (gobject, arg1, user_data)
     GObject *gobject;
     GParamSpec *arg1;
     gpointer user_data;
{
  extern int x_gtk_show_hidden_files;

  if (strcmp (arg1->name, "show-hidden") == 0)
    {
      GtkFileChooser *dialog = GTK_FILE_CHOOSER (gobject);
      GtkWidget *wtoggle = GTK_WIDGET (user_data);
      gboolean visible, toggle_on;

      g_object_get (G_OBJECT (gobject), "show-hidden", &visible, NULL);
      toggle_on = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wtoggle));

      if (!!visible != !!toggle_on)
        {
          g_signal_handlers_block_by_func (G_OBJECT (wtoggle),
                                           G_CALLBACK (xg_toggle_visibility_cb),
                                           gobject);
          gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), visible);
          g_signal_handlers_unblock_by_func
            (G_OBJECT (wtoggle),
             G_CALLBACK (xg_toggle_visibility_cb),
             gobject);
        }
      x_gtk_show_hidden_files = visible;
    }
1338 1339
}

1340 1341 1342 1343 1344
/* Read a file name from the user using a file chooser dialog.
   F is the current frame.
   PROMPT is a prompt to show to the user.  May not be NULL.
   DEFAULT_FILENAME is a default selection to be displayed.  May be NULL.
   If MUSTMATCH_P is non-zero, the returned file name must be an existing
1345 1346
   file.  *FUNC is set to a function that can be used to retrieve the
   selected file name from the returned widget.
1347

1348
   Returns the created widget.  */
1349

1350 1351 1352
static GtkWidget *
xg_get_file_with_chooser (f, prompt, default_filename,
                          mustmatch_p, only_dir_p, func)
1353 1354 1355 1356
     FRAME_PTR f;
     char *prompt;
     char *default_filename;
     int mustmatch_p, only_dir_p;
1357
     xg_get_file_func *func;
1358
{
1359 1360 1361
  char message[1024];

  GtkWidget *filewin, *wtoggle, *wbox, *wmessage;
1362 1363 1364 1365
  GtkWindow *gwin = GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f));
  GtkFileChooserAction action = (mustmatch_p ?
                                 GTK_FILE_CHOOSER_ACTION_OPEN :
                                 GTK_FILE_CHOOSER_ACTION_SAVE);
1366
  extern int x_gtk_show_hidden_files;
1367
  extern int x_gtk_file_dialog_help_text;
1368

1369 1370 1371 1372 1373 1374 1375

  if (only_dir_p)
    action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;

  filewin = gtk_file_chooser_dialog_new (prompt, gwin, action,
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         (mustmatch_p || only_dir_p ?
1376
                                          GTK_STOCK_OPEN : GTK_STOCK_OK),
1377 1378
                                         GTK_RESPONSE_OK,
                                         NULL);
1379
  gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (filewin), TRUE);
1380

1381 1382 1383
  wbox = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (wbox);
  wtoggle = gtk_check_button_new_with_label ("Show hidden files.");
Juanma Barranquero's avatar
Juanma Barranquero committed
1384 1385

  if (x_gtk_show_hidden_files)
1386 1387 1388 1389 1390 1391
    {
      g_object_set (G_OBJECT (filewin), "show-hidden", TRUE, NULL);
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wtoggle), TRUE);
    }
  gtk_widget_show (wtoggle);
  g_signal_connect (G_OBJECT (wtoggle), "clicked",
1392 1393 1394
                    G_CALLBACK (xg_toggle_visibility_cb), filewin);
  g_signal_connect (G_OBJECT (filewin), "notify",
                    G_CALLBACK (xg_toggle_notify_cb), wtoggle);
1395

1396
  if (x_gtk_file_dialog_help_text)
1397 1398
    {
      message[0] = '\0';
1399 1400
      /* Gtk+ 2.10 has the file name text entry box integrated in the dialog.
         Show the C-l help text only for versions < 2.10.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
1401
      if (gtk_check_version (2, 10, 0) && action != GTK_FILE_CHOOSER_ACTION_SAVE)
1402 1403 1404 1405
        strcat (message, "\nType C-l to display a file name text entry box.\n");
      strcat (message, "\nIf you don't like this file selector, use the "
              "corresponding\nkey binding or customize "
              "use-file-dialog to turn it off.");
Juanma Barranquero's avatar
Juanma Barranquero committed
1406

1407 1408 1409 1410
      wmessage = gtk_label_new (message);
      gtk_widget_show (wmessage);
    }

1411
  gtk_box_pack_start (GTK_BOX (wbox), wtoggle, FALSE, FALSE, 0);
1412
  if (x_gtk_file_dialog_help_text)
1413
    gtk_box_pack_start (GTK_BOX (wbox), wmessage, FALSE, FALSE, 0);
1414 1415
  gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (filewin), wbox);

1416
  if (default_filename)
1417 1418 1419
    {
      Lisp_Object file;
      struct gcpro gcpro1;
1420
      char *utf8_filename;
1421
      GCPRO1 (file);
1422

1423 1424
      file = build_string (default_filename);

1425 1426 1427
      /* File chooser does not understand ~/... in the file name.  It must be
         an absolute name starting with /.  */
      if (default_filename[0] != '/')
1428
        file = Fexpand_file_name (file, Qnil);
Juanma Barranquero's avatar
Juanma Barranquero committed
1429

1430 1431
      utf8_filename = SSDATA (ENCODE_UTF_8 (file));
      if (! NILP (Ffile_directory_p (file)))
1432
        gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filewin),
1433
                                             utf8_filename);
1434
      else
1435 1436 1437
        {
          gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filewin),
                                         utf8_filename);
1438 1439 1440 1441 1442 1443 1444
          if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
            {
              char *cp = strrchr (utf8_filename, '/');
              if (cp) ++cp;
              else cp = utf8_filename;
              gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filewin), cp);
            }
1445
        }
1446 1447 1448

      UNGCPRO;
    }
1449

1450 1451
  *func = xg_get_file_name_from_chooser;
  return filewin;
1452 1453 1454 1455
}
#endif /* HAVE_GTK_FILE_CHOOSER_DIALOG_NEW */

#ifdef HAVE_GTK_FILE_SELECTION_NEW
Jan Djärv's avatar
Jan Djärv committed
1456

1457 1458
/* Return the selected file for file selector dialog W.
   The returned string must be free:d.  */
1459

1460 1461
static char *
xg_get_file_name_from_selector (w)
Jan Djärv's avatar
Jan Djärv committed
1462 1463
     GtkWidget *w;
{
1464 1465
  GtkFileSelection *filesel = GTK_FILE_SELECTION (w);
  return xstrdup ((char*) gtk_file_selection_get_filename (filesel));
Jan Djärv's avatar
Jan Djärv committed
1466 1467
}

1468
/* Create a file selection dialog.
Jan Djärv's avatar
Jan Djärv committed
1469 1470 1471 1472
   F is the current frame.
   PROMPT is a prompt to show to the user.  May not be NULL.
   DEFAULT_FILENAME is a default selection to be displayed.  May be NULL.
   If MUSTMATCH_P is non-zero, the returned file name must be an existing
1473 1474
   file.  *FUNC is set to a function that can be used to retrieve the
   selected file name from the returned widget.