nsfns.m 76.5 KB
Newer Older
1
/* Functions for the NeXT/Open/GNUstep and MacOSX window system.
Glenn Morris's avatar
Glenn Morris committed
2

3 4
Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2011
  Free Software Foundation, Inc.
5 6 7

This file is part of GNU Emacs.

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

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
19
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
20

21
/*
22 23 24 25 26 27 28
Originally by Carl Edman
Updated by Christian Limpach (chris@nice.ch)
OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
*/

29 30
/* This should be the first include, as it may set up #defines affecting
   interpretation of even the system includes. */
Dan Nicolaescu's avatar
Dan Nicolaescu committed
31
#include <config.h>
32

33 34
#include <signal.h>
#include <math.h>
Dan Nicolaescu's avatar
Dan Nicolaescu committed
35
#include <setjmp.h>
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include "lisp.h"
#include "blockinput.h"
#include "nsterm.h"
#include "window.h"
#include "buffer.h"
#include "keyboard.h"
#include "termhooks.h"
#include "fontset.h"
#include "character.h"
#include "font.h"

#if 0
int fns_trace_num = 1;
#define NSTRACE(x)        fprintf (stderr, "%s:%d: [%d] " #x "\n",        \
                                  __FILE__, __LINE__, ++fns_trace_num)
#else
#define NSTRACE(x)
#endif

#ifdef HAVE_NS

extern NSArray *ns_send_types, *ns_return_types, *ns_drag_types;

extern Lisp_Object Qforeground_color;
extern Lisp_Object Qbackground_color;
extern Lisp_Object Qcursor_color;
extern Lisp_Object Qinternal_border_width;
extern Lisp_Object Qvisibility;
extern Lisp_Object Qcursor_type;
extern Lisp_Object Qicon_type;
extern Lisp_Object Qicon_name;
extern Lisp_Object Qicon_left;
extern Lisp_Object Qicon_top;
extern Lisp_Object Qleft;
extern Lisp_Object Qright;
extern Lisp_Object Qtop;
extern Lisp_Object Qdisplay;
extern Lisp_Object Qvertical_scroll_bars;
extern Lisp_Object Qauto_raise;
extern Lisp_Object Qauto_lower;
extern Lisp_Object Qbox;
extern Lisp_Object Qscroll_bar_width;
extern Lisp_Object Qx_resource_name;
extern Lisp_Object Qface_set_after_frame_default;
extern Lisp_Object Qunderline, Qundefined;
extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
84
extern Lisp_Object Qnone;
85

Jan D's avatar
Jan D committed
86

87 88 89 90 91 92 93 94 95
Lisp_Object Qbuffered;
Lisp_Object Qfontsize;

/* hack for OS X file panels */
char panelOK = 0;

EmacsTooltip *ns_tooltip;

/* Need forward declaration here to preserve organizational integrity of file */
96
Lisp_Object Fx_open_connection (Lisp_Object, Lisp_Object, Lisp_Object);
97 98 99

extern BOOL ns_in_resize;

100 101 102
/* Static variables to handle applescript execution.  */
static Lisp_Object as_script, *as_result;
static int as_status;
103

104 105 106 107
#if GLYPH_DEBUG
static ptrdiff_t image_cache_refcount;
#endif

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/* ==========================================================================

    Internal utility functions

   ========================================================================== */


void
check_ns (void)
{
 if (NSApp == nil)
   error ("OpenStep is not in use or not initialized");
}


/* Nonzero if we can use mouse menus. */
int
125
have_menus_p (void)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
{
  return NSApp != nil;
}


/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
   and checking validity for NS.  */
static FRAME_PTR
check_ns_frame (Lisp_Object frame)
{
  FRAME_PTR f;

  if (NILP (frame))
      f = SELECTED_FRAME ();
  else
    {
      CHECK_LIVE_FRAME (frame);
      f = XFRAME (frame);
    }
  if (! FRAME_NS_P (f))
146
    error ("non-Nextstep frame used");
147 148 149 150
  return f;
}


151 152 153
/* Let the user specify an Nextstep display with a frame.
   nil stands for the selected frame--or, if that is not an Nextstep frame,
   the first Nextstep display on the list.  */
154 155 156 157 158 159 160 161
static struct ns_display_info *
check_ns_display_info (Lisp_Object frame)
{
  if (NILP (frame))
    {
      struct frame *f = SELECTED_FRAME ();
      if (FRAME_NS_P (f) && FRAME_LIVE_P (f) )
        return FRAME_NS_DISPLAY_INFO (f);
162 163
      else if (x_display_list != 0)
        return x_display_list;
164
      else
165
        error ("Nextstep windows are not in use or not initialized");
166 167 168 169 170 171
    }
  else if (INTEGERP (frame))
    {
      struct terminal *t = get_terminal (frame, 1);

      if (t->type != output_ns)
172
        error ("Terminal %ld is not a Nextstep display", (long) XINT (frame));
173 174 175 176 177 178 179 180 181 182 183 184

      return t->display_info.ns;
    }
  else if (STRINGP (frame))
    return ns_display_info_for_name (frame);
  else
    {
      FRAME_PTR f;

      CHECK_LIVE_FRAME (frame);
      f = XFRAME (frame);
      if (! FRAME_NS_P (f))
185
        error ("non-Nextstep frame used");
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      return FRAME_NS_DISPLAY_INFO (f);
    }
  return NULL;  /* shut compiler up */
}


static id
ns_get_window (Lisp_Object maybeFrame)
{
  id view =nil, window =nil;

  if (!FRAMEP (maybeFrame) || !FRAME_NS_P (XFRAME (maybeFrame)))
    maybeFrame = selected_frame;/*wrong_type_argument (Qframep, maybeFrame); */

  if (!NILP (maybeFrame))
    view = FRAME_NS_VIEW (XFRAME (maybeFrame));
  if (view) window =[view window];

  return window;
}


static NSScreen *
Stefan Monnier's avatar
Stefan Monnier committed
209
ns_get_screen (Lisp_Object screen)
210
{
Chong Yidong's avatar
Chong Yidong committed
211 212 213 214 215 216 217
  struct frame *f;
  struct terminal *terminal;

  if (EQ (Qt, screen)) /* not documented */
    return [NSScreen mainScreen];

  terminal = get_terminal (screen, 1);
Stefan Monnier's avatar
Stefan Monnier committed
218
  if (terminal->type != output_ns)
Chong Yidong's avatar
Chong Yidong committed
219 220 221 222 223 224
    return NULL;

  if (NILP (screen))
    f = SELECTED_FRAME ();
  else if (FRAMEP (screen))
    f = XFRAME (screen);
Stefan Monnier's avatar
Stefan Monnier committed
225
  else
226
    {
Stefan Monnier's avatar
Stefan Monnier committed
227
      struct ns_display_info *dpyinfo = terminal->display_info.ns;
Jan D's avatar
Jan D committed
228 229
      f = dpyinfo->x_focus_frame
        ? dpyinfo->x_focus_frame : dpyinfo->x_highlight_frame;
230
    }
Chong Yidong's avatar
Chong Yidong committed
231 232 233

  return ((f && FRAME_NS_P (f)) ? [[FRAME_NS_VIEW (f) window] screen]
	  : NULL);
234 235 236 237 238 239
}


/* Return the X display structure for the display named NAME.
   Open a new connection if necessary.  */
struct ns_display_info *
240
ns_display_info_for_name (Lisp_Object name)
241 242 243 244 245 246
{
  Lisp_Object names;
  struct ns_display_info *dpyinfo;

  CHECK_STRING (name);

247
  for (dpyinfo = x_display_list, names = ns_display_name_list;
248 249 250 251 252 253 254 255 256 257 258
       dpyinfo;
       dpyinfo = dpyinfo->next, names = XCDR (names))
    {
      Lisp_Object tem;
      tem = Fstring_equal (XCAR (XCAR (names)), name);
      if (!NILP (tem))
        return dpyinfo;
    }

  error ("Emacs for OpenStep does not yet support multi-display.");

259 260
  Fx_open_connection (name, Qnil, Qnil);
  dpyinfo = x_display_list;
261 262

  if (dpyinfo == 0)
263
    error ("OpenStep on %s not responding.\n", SDATA (name));
264 265 266 267 268 269 270 271 272 273 274 275

  return dpyinfo;
}


static Lisp_Object
interpret_services_menu (NSMenu *menu, Lisp_Object prefix, Lisp_Object old)
/* --------------------------------------------------------------------------
   Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side
   -------------------------------------------------------------------------- */
{
  int i, count;
276
  NSMenuItem *item;
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  const char *name;
  Lisp_Object nameStr;
  unsigned short key;
  NSString *keys;
  Lisp_Object res;

  count = [menu numberOfItems];
  for (i = 0; i<count; i++)
    {
      item = [menu itemAtIndex: i];
      name = [[item title] UTF8String];
      if (!name) continue;

      nameStr = build_string (name);

      if ([item hasSubmenu])
        {
          old = interpret_services_menu ([item submenu],
                                        Fcons (nameStr, prefix), old);
        }
      else
        {
          keys = [item keyEquivalent];
          if (keys && [keys length] )
            {
              key = [keys characterAtIndex: 0];
              res = make_number (key|super_modifier);
            }
          else
            {
              res = Qundefined;
            }
          old = Fcons (Fcons (res,
                            Freverse (Fcons (nameStr,
                                           prefix))),
                    old);
        }
    }
  return old;
}



/* ==========================================================================

    Frame parameter setters

   ========================================================================== */


static void
328
x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
329 330
{
  NSColor *col;
331
  CGFloat r, g, b, alpha;
332 333 334 335 336 337 338 339 340 341 342

  if (ns_lisp_to_color (arg, &col))
    {
      store_frame_param (f, Qforeground_color, oldval);
      error ("Unknown color");
    }

  [col retain];
  [f->output_data.ns->foreground_color release];
  f->output_data.ns->foreground_color = col;

343 344 345 346
  [col getRed: &r green: &g blue: &b alpha: &alpha];
  FRAME_FOREGROUND_PIXEL (f) =
    ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));

347 348 349 350 351 352 353 354 355 356 357
  if (FRAME_NS_VIEW (f))
    {
      update_face_from_frame_parameter (f, Qforeground_color, arg);
      /*recompute_basic_faces (f); */
      if (FRAME_VISIBLE_P (f))
        redraw_frame (f);
    }
}


static void
358
x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
359 360 361 362
{
  struct face *face;
  NSColor *col;
  NSView *view = FRAME_NS_VIEW (f);
363
  CGFloat r, g, b, alpha;
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

  if (ns_lisp_to_color (arg, &col))
    {
      store_frame_param (f, Qbackground_color, oldval);
      error ("Unknown color");
    }

  /* clear the frame; in some instances the NS-internal GC appears not to
     update, or it does update and cannot clear old text properly */
  if (FRAME_VISIBLE_P (f))
    ns_clear_frame (f);

  [col retain];
  [f->output_data.ns->background_color release];
  f->output_data.ns->background_color = col;
379 380 381 382 383

  [col getRed: &r green: &g blue: &b alpha: &alpha];
  FRAME_BACKGROUND_PIXEL (f) =
    ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));

384 385 386 387 388 389 390 391 392 393 394 395
  if (view != nil)
    {
      [[view window] setBackgroundColor: col];

      if (alpha != 1.0)
          [[view window] setOpaque: NO];
      else
          [[view window] setOpaque: YES];

      face = FRAME_DEFAULT_FACE (f);
      if (face)
        {
396
          col = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), f);
397 398
          face->background
	     = (EMACS_UINT) [[col colorWithAlphaComponent: alpha] retain];
399 400 401 402 403 404 405 406 407 408 409 410
          [col release];

          update_face_from_frame_parameter (f, Qbackground_color, arg);
        }

      if (FRAME_VISIBLE_P (f))
        redraw_frame (f);
    }
}


static void
411
x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
412 413 414 415 416 417 418 419 420
{
  NSColor *col;

  if (ns_lisp_to_color (arg, &col))
    {
      store_frame_param (f, Qcursor_color, oldval);
      error ("Unknown color");
    }

421 422
  [FRAME_CURSOR_COLOR (f) release];
  FRAME_CURSOR_COLOR (f) = [col retain];
423 424 425 426 427 428 429 430 431

  if (FRAME_VISIBLE_P (f))
    {
      x_update_cursor (f, 0);
      x_update_cursor (f, 1);
    }
  update_face_from_frame_parameter (f, Qcursor_color, arg);
}

432

433
static void
434
x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
435 436
{
  NSView *view = FRAME_NS_VIEW (f);
437
  NSTRACE (x_set_icon_name);
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

  if (ns_in_resize)
    return;

  /* see if it's changed */
  if (STRINGP (arg))
    {
      if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
        return;
    }
  else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
    return;

  f->icon_name = arg;

  if (NILP (arg))
    {
      if (!NILP (f->title))
        arg = f->title;
      else
        /* explicit name and no icon-name -> explicit_name */
        if (f->explicit_name)
          arg = f->name;
        else
          {
            /* no explicit name and no icon-name ->
               name has to be rebuild from icon_title_format */
            windows_or_buffers_changed++;
            return;
          }
    }

  /* Don't change the name if it's already NAME.  */
  if ([[view window] miniwindowTitle] &&
      ([[[view window] miniwindowTitle]
             isEqualToString: [NSString stringWithUTF8String:
474
                                           SDATA (arg)]]))
475 476 477
    return;

  [[view window] setMiniwindowTitle:
478
        [NSString stringWithUTF8String: SDATA (arg)]];
479 480 481
}

static void
482
ns_set_name_internal (FRAME_PTR f, Lisp_Object name)
483
{
484 485 486
  struct gcpro gcpro1;
  Lisp_Object encoded_name, encoded_icon_name;
  NSString *str;
487 488
  NSView *view = FRAME_NS_VIEW (f);

489 490 491
  GCPRO1 (name);
  encoded_name = ENCODE_UTF_8 (name);
  UNGCPRO;
492

493
  str = [NSString stringWithUTF8String: SDATA (encoded_name)];
494

495 496 497
  /* Don't change the name if it's already NAME.  */
  if (! [[[view window] title] isEqualToString: str])
    [[view window] setTitle: str];
498

499 500
  if (!STRINGP (f->icon_name))
    encoded_icon_name = encoded_name;
501
  else
502
    encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
503 504

  str = [NSString stringWithUTF8String: SDATA (encoded_icon_name)];
505 506

  if ([[view window] miniwindowTitle] &&
507 508
      ! [[[view window] miniwindowTitle] isEqualToString: str])
    [[view window] setMiniwindowTitle: str];
509 510 511 512 513 514

}

static void
ns_set_name (struct frame *f, Lisp_Object name, int explicit)
{
515
  NSView *view;
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
  NSTRACE (ns_set_name);

  if (ns_in_resize)
    return;

  /* Make sure that requests from lisp code override requests from
     Emacs redisplay code.  */
  if (explicit)
    {
      /* If we're switching from explicit to implicit, we had better
         update the mode lines and thereby update the title.  */
      if (f->explicit_name && NILP (name))
        update_mode_lines = 1;

      f->explicit_name = ! NILP (name);
    }
  else if (f->explicit_name)
    return;

  if (NILP (name))
536
    name = build_string([ns_app_name UTF8String]);
537 538 539 540 541 542
  else
    CHECK_STRING (name);

  /* Don't change the name if it's already NAME.  */
  if (! NILP (Fstring_equal (name, f->name)))
    return;
543 544 545 546 547 548 549

  f->name = name;

  /* title overrides explicit name */
  if (! NILP (f->title))
    name = f->title;

550
  ns_set_name_internal (f, name);
551 552 553 554 555 556 557
}


/* This function should be called when the user's lisp code has
   specified a name for the frame; the name will override any set by the
   redisplay code.  */
static void
558
x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
559
{
560
  NSTRACE (x_explicitly_set_name);
561 562 563 564 565 566 567 568 569 570 571
  ns_set_name (f, arg, 1);
}


/* This function should be called by Emacs redisplay code to set the
   name; names set this way will never override names set by the user's
   lisp code.  */
void
x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
{
  NSTRACE (x_implicitly_set_name);
572 573 574 575

  /* Deal with NS specific format t.  */
  if (FRAME_NS_P (f) && ((FRAME_ICONIFIED_P (f) && EQ (Vicon_title_format, Qt))
                         || EQ (Vframe_title_format, Qt)))
576
    ns_set_name_as_filename (f);
577 578 579 580 581 582
  else
    ns_set_name (f, arg, 0);
}


/* Change the title of frame F to NAME.
583
   If NAME is nil, use the frame name as the title.  */
584 585

static void
586
x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
587
{
588
  NSTRACE (x_set_title);
589 590 591 592 593 594 595
  /* Don't change the title if it's already NAME.  */
  if (EQ (name, f->title))
    return;

  update_mode_lines = 1;

  f->title = name;
596 597 598 599 600 601 602

  if (NILP (name))
    name = f->name;
  else
    CHECK_STRING (name);

  ns_set_name_internal (f, name);
603 604 605 606 607 608
}


void
ns_set_name_as_filename (struct frame *f)
{
609
  NSView *view;
610
  Lisp_Object name, filename;
611 612 613
  Lisp_Object buf = XWINDOW (f->selected_window)->buffer;
  const char *title;
  NSAutoreleasePool *pool;
614
  struct gcpro gcpro1;
615 616
  Lisp_Object encoded_name, encoded_filename;
  NSString *str;
617 618 619 620 621 622 623
  NSTRACE (ns_set_name_as_filename);

  if (f->explicit_name || ! NILP (f->title) || ns_in_resize)
    return;

  BLOCK_INPUT;
  pool = [[NSAutoreleasePool alloc] init];
Jan Djärv's avatar
Jan Djärv committed
624 625
  filename = BVAR (XBUFFER (buf), filename);
  name = BVAR (XBUFFER (buf), name);
626 627

  if (NILP (name))
628 629 630 631 632 633
    {
      if (! NILP (filename))
        name = Ffile_name_nondirectory (filename);
      else
        name = build_string ([ns_app_name UTF8String]);
    }
634

635 636 637 638
  GCPRO1 (name);
  encoded_name = ENCODE_UTF_8 (name);
  UNGCPRO;

639 640
  view = FRAME_NS_VIEW (f);

641 642 643
  title = FRAME_ICONIFIED_P (f) ? [[[view window] miniwindowTitle] UTF8String]
                                : [[[view window] title] UTF8String];

644
  if (title && (! strcmp (title, SDATA (encoded_name))))
645 646 647 648 649 650
    {
      [pool release];
      UNBLOCK_INPUT;
      return;
    }

651 652 653 654 655
  str = [NSString stringWithUTF8String: SDATA (encoded_name)];
  if (str == nil) str = @"Bad coding";

  if (FRAME_ICONIFIED_P (f))
    [[view window] setMiniwindowTitle: str];
656
  else
657
    {
658 659 660
      NSString *fstr;

      if (! NILP (filename))
661
        {
662 663 664 665 666 667 668 669 670 671 672 673 674
          GCPRO1 (filename);
          encoded_filename = ENCODE_UTF_8 (filename);
          UNGCPRO;

          fstr = [NSString stringWithUTF8String: SDATA (encoded_filename)];
          if (fstr == nil) fstr = @"";
#ifdef NS_IMPL_COCOA
          /* work around a bug observed on 10.3 and later where
             setTitleWithRepresentedFilename does not clear out previous state
             if given filename does not exist */
          if (! [[NSFileManager defaultManager] fileExistsAtPath: fstr])
            [[view window] setRepresentedFilename: @""];
#endif
675 676
        }
      else
677 678 679 680
        fstr = @"";

      [[view window] setRepresentedFilename: fstr];
      [[view window] setTitle: str];
681 682
      f->name = name;
    }
683

684 685 686 687 688 689
  [pool release];
  UNBLOCK_INPUT;
}


void
690
ns_set_doc_edited (struct frame *f, Lisp_Object arg)
691 692 693
{
  NSView *view = FRAME_NS_VIEW (f);
  NSAutoreleasePool *pool;
694 695 696 697 698 699 700 701
  if (!MINI_WINDOW_P (XWINDOW (f->selected_window)))
    {
      BLOCK_INPUT;
      pool = [[NSAutoreleasePool alloc] init];
      [[view window] setDocumentEdited: !NILP (arg)];
      [pool release];
      UNBLOCK_INPUT;
    }
702 703 704
}


705 706
void
x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
{
  int nlines;
  int olines = FRAME_MENU_BAR_LINES (f);
  if (FRAME_MINIBUF_ONLY_P (f))
    return;

  if (INTEGERP (value))
    nlines = XINT (value);
  else
    nlines = 0;

  FRAME_MENU_BAR_LINES (f) = 0;
  if (nlines)
    {
      FRAME_EXTERNAL_MENU_BAR (f) = 1;
722 723
      /* does for all frames, whereas we just want for one frame
	 [NSMenu setMenuBarVisible: YES]; */
724 725 726 727 728
    }
  else
    {
      if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
        free_frame_menubar (f);
729
      /*      [NSMenu setMenuBarVisible: NO]; */
730 731 732 733 734
      FRAME_EXTERNAL_MENU_BAR (f) = 0;
    }
}


735
/* toolbar support */
736 737
void
x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
{
  int nlines;
  Lisp_Object root_window;

  if (FRAME_MINIBUF_ONLY_P (f))
    return;

  if (INTEGERP (value) && XINT (value) >= 0)
    nlines = XFASTINT (value);
  else
    nlines = 0;

  if (nlines)
    {
      FRAME_EXTERNAL_TOOL_BAR (f) = 1;
      update_frame_tool_bar (f);
    }
  else
    {
      if (FRAME_EXTERNAL_TOOL_BAR (f))
        {
          free_frame_tool_bar (f);
          FRAME_EXTERNAL_TOOL_BAR (f) = 0;
        }
    }

  x_set_window_size (f, 0, f->text_cols, f->text_lines);
}


void
ns_implicitly_set_icon_type (struct frame *f)
{
  Lisp_Object tem;
  EmacsView *view = FRAME_NS_VIEW (f);
  id image =nil;
  Lisp_Object chain, elt;
  NSAutoreleasePool *pool;
  BOOL setMini = YES;

  NSTRACE (ns_implicitly_set_icon_type);

  BLOCK_INPUT;
  pool = [[NSAutoreleasePool alloc] init];
  if (f->output_data.ns->miniimage
783
      && [[NSString stringWithUTF8String: SDATA (f->name)]
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
               isEqualToString: [(NSImage *)f->output_data.ns->miniimage name]])
    {
      [pool release];
      UNBLOCK_INPUT;
      return;
    }

  tem = assq_no_quit (Qicon_type, f->param_alist);
  if (CONSP (tem) && ! NILP (XCDR (tem)))
    {
      [pool release];
      UNBLOCK_INPUT;
      return;
    }

  for (chain = Vns_icon_type_alist;
       (image = nil) && CONSP (chain);
       chain = XCDR (chain))
    {
      elt = XCAR (chain);
      /* special case: 't' means go by file type */
805
      if (SYMBOLP (elt) && EQ (elt, Qt) && SDATA (f->name)[0] == '/')
806
        {
807 808
          NSString *str
	     = [NSString stringWithUTF8String: SDATA (f->name)];
809 810 811 812 813 814 815 816 817 818 819 820
          if ([[NSFileManager defaultManager] fileExistsAtPath: str])
            image = [[[NSWorkspace sharedWorkspace] iconForFile: str] retain];
        }
      else if (CONSP (elt) &&
               STRINGP (XCAR (elt)) &&
               STRINGP (XCDR (elt)) &&
               fast_string_match (XCAR (elt), f->name) >= 0)
        {
          image = [EmacsImage allocInitFromFile: XCDR (elt)];
          if (image == nil)
            image = [[NSImage imageNamed:
                               [NSString stringWithUTF8String:
821
					    SDATA (XCDR (elt))]] retain];
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
        }
    }

  if (image == nil)
    {
      image = [[[NSWorkspace sharedWorkspace] iconForFileType: @"text"] retain];
      setMini = NO;
    }

  [f->output_data.ns->miniimage release];
  f->output_data.ns->miniimage = image;
  [view setMiniwindowImage: setMini];
  [pool release];
  UNBLOCK_INPUT;
}


static void
840
x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
841 842 843 844 845
{
  EmacsView *view = FRAME_NS_VIEW (f);
  id image = nil;
  BOOL setMini = YES;

846
  NSTRACE (x_set_icon_type);
847 848 849

  if (!NILP (arg) && SYMBOLP (arg))
    {
850
      arg =build_string (SDATA (SYMBOL_NAME (arg)));
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
      store_frame_param (f, Qicon_type, arg);
    }

  /* do it the implicit way */
  if (NILP (arg))
    {
      ns_implicitly_set_icon_type (f);
      return;
    }

  CHECK_STRING (arg);

  image = [EmacsImage allocInitFromFile: arg];
  if (image == nil)
    image =[NSImage imageNamed: [NSString stringWithUTF8String:
866
                                            SDATA (arg)]];
867 868 869 870 871 872 873 874 875 876 877 878

  if (image == nil)
    {
      image = [NSImage imageNamed: @"text"];
      setMini = NO;
    }

  f->output_data.ns->miniimage = image;
  [view setMiniwindowImage: setMini];
}


879
/* Xism; we stub out (we do implement this in ns-win.el) */
880 881 882 883 884 885 886 887 888
int
XParseGeometry (char *string, int *x, int *y,
                unsigned int *width, unsigned int *height)
{
  message1 ("Warning: XParseGeometry not supported under NS.\n");
  return 0;
}


889
/* TODO: move to nsterm? */
890 891 892 893 894
int
ns_lisp_to_cursor_type (Lisp_Object arg)
{
  char *str;
  if (XTYPE (arg) == Lisp_String)
895
    str = SDATA (arg);
896
  else if (XTYPE (arg) == Lisp_Symbol)
897
    str = SDATA (SYMBOL_NAME (arg));
898
  else return -1;
899 900 901 902 903
  if (!strcmp (str, "box"))	return FILLED_BOX_CURSOR;
  if (!strcmp (str, "hollow"))	return HOLLOW_BOX_CURSOR;
  if (!strcmp (str, "hbar"))	return HBAR_CURSOR;
  if (!strcmp (str, "bar"))	return BAR_CURSOR;
  if (!strcmp (str, "no"))	return NO_CURSOR;
904 905 906 907 908 909 910 911 912
  return -1;
}


Lisp_Object
ns_cursor_type_to_lisp (int arg)
{
  switch (arg)
    {
913 914 915 916 917 918
    case FILLED_BOX_CURSOR: return Qbox;
    case HOLLOW_BOX_CURSOR: return intern ("hollow");
    case HBAR_CURSOR:	    return intern ("hbar");
    case BAR_CURSOR:	    return intern ("bar");
    case NO_CURSOR:
    default:		    return intern ("no");
919 920 921
    }
}

922
/* This is the same as the xfns.c definition.  */
923
void
924
x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
925
{
926
  set_frame_cursor_types (f, arg);
927

928 929
  /* Make sure the cursor gets redrawn.  */
  cursor_type_changed = 1;
930
}
931

932

933 934
/* called to set mouse pointer color, but all other terms use it to
   initialize pointer types (and don't set the color ;) */
935
static void
936
x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
937
{
938
  /* don't think we can do this on Nextstep */
939 940 941
}


942 943 944 945
#define Str(x) #x
#define Xstr(x) Str(x)

static Lisp_Object
946
ns_appkit_version_str (void)
947 948 949 950 951 952 953 954 955 956 957 958 959 960
{
  char tmp[80];

#ifdef NS_IMPL_GNUSTEP
  sprintf(tmp, "gnustep-gui-%s", Xstr(GNUSTEP_GUI_VERSION));
#elif defined(NS_IMPL_COCOA)
  sprintf(tmp, "apple-appkit-%.2f", NSAppKitVersionNumber);
#else
  tmp = "ns-unknown";
#endif
  return build_string (tmp);
}


961 962 963 964
/* This is for use by x-server-version and collapses all version info we
   have into a single int.  For a better picture of the implementation
   running, use ns_appkit_version_str.*/
static int
965
ns_appkit_version_int (void)
966 967
{
#ifdef NS_IMPL_GNUSTEP
968
  return GNUSTEP_GUI_MAJOR_VERSION * 100 + GNUSTEP_GUI_MINOR_VERSION;
969 970 971 972 973 974 975
#elif defined(NS_IMPL_COCOA)
  return (int)NSAppKitVersionNumber;
#endif
  return 0;
}


976
static void
977
x_icon (struct frame *f, Lisp_Object parms)
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
/* --------------------------------------------------------------------------
   Strangely-named function to set icon position parameters in frame.
   This is irrelevant under OS X, but might be needed under GNUstep,
   depending on the window manager used.  Note, this is not a standard
   frame parameter-setter; it is called directly from x-create-frame.
   -------------------------------------------------------------------------- */
{
  Lisp_Object icon_x, icon_y;
  struct ns_display_info *dpyinfo = check_ns_display_info (Qnil);

  f->output_data.ns->icon_top = Qnil;
  f->output_data.ns->icon_left = Qnil;

  /* Set the position of the icon.  */
  icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
  icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0,  RES_TYPE_NUMBER);
  if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
    {
      CHECK_NUMBER (icon_x);
      CHECK_NUMBER (icon_y);
      f->output_data.ns->icon_top = icon_y;
      f->output_data.ns->icon_left = icon_x;
    }
  else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
    error ("Both left and top icon corners of icon must be specified");
}


1006
/* Note: see frame.c for template, also where generic functions are impl */
1007 1008 1009 1010
frame_parm_handler ns_frame_parm_handlers[] =
{
  x_set_autoraise, /* generic OK */
  x_set_autolower, /* generic OK */
1011
  x_set_background_color,
1012 1013
  0, /* x_set_border_color,  may be impossible under Nextstep */
  0, /* x_set_border_width,  may be impossible under Nextstep */
1014 1015
  x_set_cursor_color,
  x_set_cursor_type,
1016
  x_set_font, /* generic OK */
1017 1018 1019
  x_set_foreground_color,
  x_set_icon_name,
  x_set_icon_type,
1020
  x_set_internal_border_width, /* generic OK */
1021
  x_set_menu_bar_lines,
1022 1023
  x_set_mouse_color,
  x_explicitly_set_name,
1024
  x_set_scroll_bar_width, /* generic OK */
1025
  x_set_title,
1026 1027 1028
  x_set_unsplittable, /* generic OK */
  x_set_vertical_scroll_bars, /* generic OK */
  x_set_visibility, /* generic OK */
1029
  x_set_tool_bar_lines,
1030 1031 1032 1033 1034 1035 1036 1037
  0, /* x_set_scroll_bar_foreground, will ignore (not possible on NS) */
  0, /* x_set_scroll_bar_background,  will ignore (not possible on NS) */
  x_set_screen_gamma, /* generic OK */
  x_set_line_spacing, /* generic OK, sets f->extra_line_spacing to int */
  x_set_fringe_width, /* generic OK */
  x_set_fringe_width, /* generic OK */
  0, /* x_set_wait_for_wm, will ignore */
  0,  /* x_set_fullscreen will ignore */
1038
  x_set_font_backend, /* generic OK */
1039
  x_set_alpha,
1040 1041
  0, /* x_set_sticky */
  0, /* x_set_tool_bar_position */
1042 1043 1044
};


1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
/* Handler for signals raised during x_create_frame.
   FRAME is the frame which is partially constructed.  */

static Lisp_Object
unwind_create_frame (Lisp_Object frame)
{
  struct frame *f = XFRAME (frame);

  /* If frame is already dead, nothing to do.  This can happen if the
     display is disconnected after the frame has become official, but
     before x_create_frame removes the unwind protect.  */
  if (!FRAME_LIVE_P (f))
    return Qnil;

  /* If frame is ``official'', nothing to do.  */
Jan Djärv's avatar
Jan Djärv committed
1060
  if (NILP (Fmemq (frame, Vframe_list)))
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
    {
#if GLYPH_DEBUG && XASSERTS
      struct ns_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
#endif

      x_free_frame_resources (f);
      free_glyphs (f);

#if GLYPH_DEBUG
      /* Check that reference counts are indeed correct.  */
      xassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
#endif
      return Qt;
    }

  return Qnil;
}


1080 1081 1082 1083 1084 1085 1086

/* ==========================================================================

    Lisp definitions

   ========================================================================== */

1087
DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
1088
       1, 1, 0,
1089
       doc: /* Make a new Nextstep window, called a "frame" in Emacs terms.
1090 1091 1092 1093 1094
Return an Emacs frame object.
PARMS is an alist of frame parameters.
If the parameters specify that the frame should not have a minibuffer,
and do not specify a specific minibuffer window to use,
then `default-minibuffer-frame' must be a frame whose minibuffer can
1095 1096 1097
be shared by the new frame.

This function is an internal primitive--use `make-frame' instead.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
1098
     (Lisp_Object parms)
1099 1100 1101 1102 1103
{
  struct frame *f;
  Lisp_Object frame, tem;
  Lisp_Object name;
  int minibuffer_only = 0;
1104 1105
  int window_prompting = 0;
  int width, height;
1106
  int count = specpdl_ptr - specpdl;
1107
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1108 1109 1110 1111 1112
  Lisp_Object display;
  struct ns_display_info *dpyinfo = NULL;
  Lisp_Object parent;
  struct kboard *kb;
  Lisp_Object tfont, tfontsize;
1113
  static int desc_ctr = 1;
1114 1115 1116

  check_ns ();

1117
  /* x_get_arg modifies parms.  */
Adrian Robert's avatar
Adrian Robert committed
1118 1119
  parms = Fcopy_alist (parms);

1120 1121 1122 1123
  /* Use this general default value to start with
     until we know if this frame has a specified name.  */
  Vx_resource_name = Vinvocation_name;

1124 1125 1126 1127
  display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_STRING);
  if (EQ (display, Qunbound))
    display = Qnil;
  dpyinfo = check_ns_display_info (display);
1128
  kb = dpyinfo->terminal->kboard;
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147

  if (!dpyinfo->terminal->name)
    error ("Terminal is not live, can't create new frames on it");

  name = x_get_arg (dpyinfo, parms, Qname, 0, 0, RES_TYPE_STRING);
  if (!STRINGP (name)
      && ! EQ (name, Qunbound)
      && ! NILP (name))
    error ("Invalid frame name--not a string or nil");

  if (STRINGP (name))
    Vx_resource_name = name;

  parent = x_get_arg (dpyinfo, parms, Qparent_id, 0, 0, RES_TYPE_NUMBER);
  if (EQ (parent, Qunbound))
    parent = Qnil;
  if (! NILP (parent))
    CHECK_NUMBER (parent);

1148 1149 1150
  /* make_frame_without_minibuffer can run Lisp code and garbage collect.  */
  /* No need to protect DISPLAY because that's not used after passing
     it to make_frame_without_minibuffer.  */
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
  frame = Qnil;
  GCPRO4 (parms, parent, name, frame);
  tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
                  RES_TYPE_SYMBOL);
  if (EQ (tem, Qnone) || NILP (tem))
      f = make_frame_without_minibuffer (Qnil, kb, display);
  else if (EQ (tem, Qonly))
    {
      f = make_minibuffer_frame ();
      minibuffer_only = 1;
    }
  else if (WINDOWP (tem))
      f = make_frame_without_minibuffer (tem, kb, display);
  else
      f = make_frame (1);

  XSETFRAME (frame, f);
  FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;

  f->terminal = dpyinfo->terminal;

  f->output_method = output_ns;
  f->output_data.ns = (struct ns_output *)xmalloc (sizeof *(f->output_data.ns));
1174
  memset (f->output_data.ns, 0, sizeof *(f->output_data.ns));
1175 1176 1177 1178 1179

  FRAME_FONTSET (f) = -1;

  f->icon_name = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
                            RES_TYPE_STRING);
Adrian Robert's avatar
Adrian Robert committed
1180
  if (! STRINGP (f->icon_name))
1181 1182 1183 1184
    f->icon_name = Qnil;

  FRAME_NS_DISPLAY_INFO (f) = dpyinfo;

1185 1186 1187
  /* With FRAME_NS_DISPLAY_INFO set up, this unwind-protect is safe.  */
  record_unwind_protect (unwind_create_frame, frame);

1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
  f->output_data.ns->window_desc = desc_ctr++;
  if (!NILP (parent))
    {
      f->output_data.ns->parent_desc = (Window) XFASTINT (parent);
      f->output_data.ns->explicit_parent = 1;
    }
  else
    {
      f->output_data.ns->parent_desc = FRAME_NS_DISPLAY_INFO (f)->root_window;
      f->output_data.ns->explicit_parent = 0;
    }

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
  /* Set the name; the functions to which we pass f expect the name to
     be set.  */
  if (EQ (name, Qunbound) || NILP (name) || ! STRINGP (name))
    {
      f->name = build_string ([ns_app_name UTF8String]);
      f->explicit_name = 0;
    }
  else
    {
      f->name = name;
      f->explicit_name = 1;
      specbind (Qx_resource_name, name);
    }

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
  f->resx = dpyinfo->resx;
  f->resy = dpyinfo->resy;

  BLOCK_INPUT;
  register_font_driver (&nsfont_driver, f);
  x_default_parameter (f, parms, Qfont_backend, Qnil,
			"fontBackend", "FontBackend", RES_TYPE_STRING);

  {
    /* use for default font name */
    id font = [NSFont userFixedPitchFontOfSize: -1.0]; /* default */
    tfontsize = x_default_parameter (f, parms, Qfontsize,
                                    make_number (0 /*(int)[font pointSize]*/),
                                    "fontSize", "FontSize", RES_TYPE_NUMBER);
    tfont = x_default_parameter (f, parms, Qfont,
                                 build_string ([[font fontName] UTF8String]),
                                 "font", "Font", RES_TYPE_STRING);
  }
  UNBLOCK_INPUT;

  x_default_parameter (f, parms, Qborder_width, make_number (0),
		       "borderwidth", "BorderWidth", RES_TYPE_NUMBER);
  x_default_parameter (f, parms, Qinternal_border_width, make_number (2),
                      "internalBorderWidth", "InternalBorderWidth",
                      RES_TYPE_NUMBER);

  /* default scrollbars on right on Mac */
  {
1242
      Lisp_Object spos
1243
#ifdef NS_IMPL_GNUSTEP