nsterm.h 36.1 KB
Newer Older
1
/* Definitions and headers for communication with NeXT/Open/GNUstep API.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 1989, 1993, 2005, 2008-2015 Free Software Foundation,
3
   Inc.
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
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.
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 19
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */

20 21 22 23 24

#include "dispextern.h"
#include "frame.h"
#include "character.h"
#include "font.h"
25
#include "sysselect.h"
26 27 28

#ifdef HAVE_NS

29 30 31 32
#ifdef NS_IMPL_COCOA
#ifndef MAC_OS_X_VERSION_10_6
#define MAC_OS_X_VERSION_10_6 1060
#endif
33 34 35 36 37 38
#ifndef MAC_OS_X_VERSION_10_7
#define MAC_OS_X_VERSION_10_7 1070
#endif
#ifndef MAC_OS_X_VERSION_10_8
#define MAC_OS_X_VERSION_10_8 1080
#endif
39 40 41
#ifndef MAC_OS_X_VERSION_10_9
#define MAC_OS_X_VERSION_10_9 1090
#endif
42 43 44 45 46

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
#define HAVE_NATIVE_FS
#endif

47
#endif /* NS_IMPL_COCOA */
48

49 50
#ifdef __OBJC__

Glenn Morris's avatar
Glenn Morris committed
51
/* CGFloat on GNUstep may be 4 or 8 byte, but functions expect float* for some
52
   versions.
53 54 55 56
   On Cocoa >= 10.5, functions expect CGFloat*. Make compatible type.  */
#ifdef NS_IMPL_COCOA
typedef CGFloat EmacsCGFloat;
#elif GNUSTEP_GUI_MAJOR_VERSION > 0 || GNUSTEP_GUI_MINOR_VERSION >= 22
57 58 59 60 61
typedef CGFloat EmacsCGFloat;
#else
typedef float EmacsCGFloat;
#endif

62 63 64 65 66 67
/* ==========================================================================

   Trace support

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

68
/* Uncomment the following line to enable trace.
69

70 71 72 73
   Hint: keep the trailing whitespace -- the version control system
   will reject accidental commits. */

/* #define NSTRACE_ENABLED 1          */
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128


/* Print a call tree containing all annotated functions.

   The call structure of the functions is represented using
   indentation and vertical lines.  Extra information is printed using
   horizontal lines that connect to the vertical line.

   The return value is represented using the arrow "->>".  For simple
   functions, the arrow can be printed on the same line as the
   function name.  If more output is printed, it is connected to the
   vertical line of the function.

   The first column contains the file name, the second the line
   number, and the third a number increasing for each trace line.

   Note that the trace system, when enabled, use the GCC/Clang
   "cleanup" extension.

   For example (long lines manually split to reduce width):

nsterm.m  : 1600: [ 4428]  ns_fullscreen_hook
nsterm.m  : 7006: [ 4429]  | handleFS
nsterm.m  : 7035: [ 4430]  | +--- FULLSCREEN_MAXIMIZED
nsterm.m  : 7627: [ 4431]  | | performZoom
nsterm.m  : 7636: [ 4432]  | | | zoom
nsterm.m  :  874: [ 4433]  | | | | ns_update_auto_hide_menu_bar
nsterm.m  : 6615: [ 4434]  | | | | [windowWillUseStandardFrame:
                                       defaultFrame:(X:0 Y:0)/(W:1600 H:1177)]
nsterm.m  :   99: [ 4435]  | | | | +--- fs_state: FULLSCREEN_NONE
nsterm.m  :  119: [ 4436]  | | | | +--- fs_before_fs: -1
nsterm.m  :  115: [ 4437]  | | | | +--- next_maximized: FULLSCREEN_MAXIMIZED
nsterm.m  : 6619: [ 4438]  | | | | +--- ns_userRect: (X:0 Y:0)/(W:0 H:0)
nsterm.m  : 6620: [ 4439]  | | | | +--- [sender frame]:
                                                      (X:0 Y:626)/(W:595 H:551)
nsterm.m  : 6644: [ 4440]  | | | | +--- ns_userRect (2):
                                                      (X:0 Y:626)/(W:595 H:551)
nsterm.m  : 6684: [ 4441]  | | | | +--- FULLSCREEN_MAXIMIZED
nsterm.m  : 7057: [ 4442]  | | | | | setFSValue
nsterm.m  :  115: [ 4443]  | | | | | +--- value: FULLSCREEN_MAXIMIZED
nsterm.m  : 6711: [ 4444]  | | | | +--- Final ns_userRect:
                                                      (X:0 Y:626)/(W:595 H:551)
nsterm.m  : 6712: [ 4445]  | | | | +--- Final maximized_width: 1600
nsterm.m  : 6713: [ 4446]  | | | | +--- Final maximized_height: 1177
nsterm.m  :  119: [ 4447]  | | | | +--- Final next_maximized: -1
nsterm.m  : 6209: [ 4448]  | | | | | windowWillResize: toSize: (W:1600 H:1177)
nsterm.m  : 6210: [ 4449]  | | | | | +--- [sender frame]:
                                                      (X:0 Y:626)/(W:595 H:551)
nsterm.m  :  115: [ 4450]  | | | | | +--- fs_state: FULLSCREEN_MAXIMIZED
nsterm.m  : 6274: [ 4451]  | | | | | +--- cols: 223  rows: 79
nsterm.m  : 6299: [ 4452]  | | | | | +->> (W:1596 H:1167)
nsterm.m  : 6718: [ 4453]  | | | | +->> (X:0 Y:0)/(W:1600 H:1177)

   Here, "ns_fullscreen_hook" calls "handleFS", which is turn calls
   "performZoom".  This function calls "[super performZoom]", which
Paul Eggert's avatar
Paul Eggert committed
129
   isn't annotated (so it doesn't show up in the trace).  However, it
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
   calls "zoom" which is annotated so it is part of the call trace.
   Later, the method "windowWillUseStandardFrame" and the function
   "setFSValue" are called.  The lines with "+---" contain extra
   information and lines containing "->>" represent return values. */

#ifndef NSTRACE_ENABLED
#define NSTRACE_ENABLED 0
#endif

#if NSTRACE_ENABLED
extern int nstrace_num;
extern int nstrace_depth;

void nstrace_leave(int *);

/* printf-style trace output.  Output is aligned with contained heading. */
#define NSTRACE_MSG_NO_DASHES(...)                                          \
  do                                                                        \
    {                                                                       \
      if (nstrace_enabled)                                                  \
        {                                                                   \
          fprintf (stderr, "%-10s:%5d: [%5d]%.*s",                          \
                   __FILE__, __LINE__, ++nstrace_num,                       \
                   2*nstrace_depth, "  | | | | | | | | | | | | | | | ..");  \
          fprintf (stderr, __VA_ARGS__);                                    \
          fprintf (stderr, "\n");                                           \
        }                                                                   \
    }                                                                       \
  while(0)

#define NSTRACE_MSG(...) NSTRACE_MSG_NO_DASHES("+--- " __VA_ARGS__)



/* Macros for printing complex types.

   NSTRACE_FMT_what     -- Printf format string for "what".
   NSTRACE_ARG_what(x)  -- Printf argument for "what". */

#define NSTRACE_FMT_SIZE        "(W:%.0f H:%.0f)"
#define NSTRACE_ARG_SIZE(elt)   (elt).width, (elt).height

#define NSTRACE_FMT_POINT       "(X:%.0f Y:%.0f)"
#define NSTRACE_ARG_POINT(elt)  (elt).x, (elt).y

#define NSTRACE_FMT_RECT        NSTRACE_FMT_POINT "/" NSTRACE_FMT_SIZE
#define NSTRACE_ARG_RECT(elt)   \
  NSTRACE_ARG_POINT((elt).origin), NSTRACE_ARG_SIZE((elt).size)


/* Macros for printing complex types as extra information. */

#define NSTRACE_SIZE(str,size)                                          \
  NSTRACE_MSG (str ": " NSTRACE_FMT_SIZE,                               \
               NSTRACE_ARG_SIZE (size));

#define NSTRACE_POINT(str,point)                                        \
  NSTRACE_MSG (str ": " NSTRACE_FMT_POINT,                              \
               NSTRACE_ARG_POINT (point));

#define NSTRACE_RECT(str,rect)                                          \
  NSTRACE_MSG (str ": " NSTRACE_FMT_RECT,                               \
               NSTRACE_ARG_RECT (rect));

#define NSTRACE_FSTYPE(str,fs_type)                                     \
  do                                                                    \
    {                                                                   \
      if (nstrace_enabled)                                              \
        {                                                               \
          ns_print_fullscreen_type_name(str, fs_type);                  \
        }                                                               \
    }                                                                   \
  while(0)


/* Return value macros.

   NSTRACE_RETURN(fmt, ...) - Print a return value, support printf-style
                              format string and arguments.

   NSTRACE_RETURN_what(obj) - Print a return value of kind WHAT.

   NSTRACE_FMT_RETURN - A string literal representing a returned
                        value.  Useful when creating a format string
                        to printf-like constructs like NSTRACE(). */

#define NSTRACE_FMT_RETURN "->>"

#define NSTRACE_RETURN(...) \
  NSTRACE_MSG_NO_DASHES ("+" NSTRACE_FMT_RETURN " " __VA_ARGS__)

#define NSTRACE_RETURN_SIZE(size) \
  NSTRACE_RETURN(NSTRACE_FMT_SIZE, NSTRACE_ARG_SIZE(size))

#define NSTRACE_RETURN_POINT(point) \
  NSTRACE_RETURN(NSTRACE_FMT_POINT, NSTRACE_ARG_POINT(point))

#define NSTRACE_RETURN_RECT(rect) \
  NSTRACE_RETURN(NSTRACE_FMT_RECT, NSTRACE_ARG_RECT(rect))


/* Function enter macros.

Paul Eggert's avatar
Paul Eggert committed
233
   NSTRACE (fmt, ...) -- Enable trace output in current block
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 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
                         (typically a function).  Accepts printf-style
                         arguments.

   NSTRACE_WHEN (cond, fmt, ...) -- Enable trace output when COND is true.

   NSTRACE_UNLESS (cond, fmt, ...) -- Enable trace output unless COND is
                                      true. */



#define NSTRACE_WHEN(cond, ...)                                         \
  __attribute__((cleanup(nstrace_leave)))                               \
  int nstrace_enabled = (cond);                                         \
  if (nstrace_enabled) { ++nstrace_depth; }                             \
  NSTRACE_MSG_NO_DASHES(__VA_ARGS__);

#endif /* NSTRACE_ENABLED */

#define NSTRACE(...)              NSTRACE_WHEN(1, __VA_ARGS__)
#define NSTRACE_UNLESS(cond, ...) NSTRACE_WHEN(!(cond), __VA_ARGS__)


/* Non-trace replacement versions. */
#ifndef NSTRACE_WHEN
#define NSTRACE_WHEN(...)
#endif

#ifndef NSTRACE_MSG
#define NSTRACE_MSG(...)
#endif

#ifndef NSTRACE_SIZE
#define NSTRACE_SIZE(str,size)
#endif

#ifndef NSTRACE_POINT
#define NSTRACE_POINT(str,point)
#endif

#ifndef NSTRACE_RECT
#define NSTRACE_RECT(str,rect)
#endif

#ifndef NSTRACE_FSTYPE
#define NSTRACE_FSTYPE(str,fs_type)
#endif

#ifndef NSTRACE_RETURN_SIZE
#define NSTRACE_RETURN_SIZE(size)
#endif

#ifndef NSTRACE_RETURN_POINT
#define NSTRACE_RETURN_POINT(point)
#endif

#ifndef NSTRACE_RETURN_RECT
#define NSTRACE_RETURN_RECT(rect)
#endif

#ifndef NSTRACE_RETURN_FSTYPE
#define NSTRACE_RETURN_FSTYPE(fs_type)
#endif



299 300 301 302 303 304 305 306 307 308 309 310
/* ==========================================================================

   NSColor, EmacsColor category.

   ========================================================================== */
@interface NSColor (EmacsColor)
+ (NSColor *)colorForEmacsRed:(CGFloat)red green:(CGFloat)green
                         blue:(CGFloat)blue alpha:(CGFloat)alpha;
- (NSColor *)colorUsingDefaultColorSpace;

@end

311 312 313 314 315 316 317 318 319
/* ==========================================================================

   The Emacs application

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

/* We override sendEvent: as a means to stop/start the event loop */
@interface EmacsApp : NSApplication
{
Jan Djärv's avatar
Jan Djärv committed
320
#ifdef NS_IMPL_COCOA
Jan Djärv's avatar
Jan Djärv committed
321 322 323
  BOOL shouldKeepRunning;
  BOOL isFirst;
#endif
324
#ifdef NS_IMPL_GNUSTEP
Jan Djärv's avatar
Jan Djärv committed
325
  BOOL applicationDidFinishLaunchingCalled;
326 327 328
@public
  int nextappdefined;
#endif
329
}
330
- (void)logNotification: (NSNotification *)notification;
331
- (void)antialiasThresholdDidChange:(NSNotification *)notification;
332 333
- (void)sendEvent: (NSEvent *)theEvent;
- (void)showPreferencesWindow: (id)sender;
334
- (BOOL) openFile: (NSString *)fileName;
335
- (void)fd_handler: (id)unused;
336 337
- (void)timeout_handler: (NSTimer *)timedEntry;
- (BOOL)fulfillService: (NSString *)name withArg: (NSString *)arg;
338 339 340
#ifdef NS_IMPL_GNUSTEP
- (void)sendFromMainThread:(id)unused;
#endif
341 342
@end

343 344 345 346 347 348 349
#ifdef NS_IMPL_GNUSTEP
/* Dummy class to get rid of startup warnings.  */
@interface EmacsDocument : NSDocument
{
}
@end
#endif
350 351 352 353 354 355 356 357 358

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

   The main Emacs view

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

@class EmacsToolbar;

359
#ifdef NS_IMPL_COCOA
360 361 362 363
@interface EmacsView : NSView <NSTextInput, NSWindowDelegate>
#else
@interface EmacsView : NSView <NSTextInput>
#endif
364
   {
365
#ifdef NS_IMPL_COCOA
366
   char *old_title;
367 368
   BOOL maximizing_resize;
#endif
369 370 371
   BOOL windowClosing;
   NSString *workingText;
   BOOL processingCompose;
372 373
   int fs_state, fs_before_fs, next_maximized;
   int tibar_height, tobar_height, bwidth;
374 375
   int maximized_width, maximized_height;
   NSWindow *nonfs_window;
376
   BOOL fs_is_native;
377 378 379 380 381
@public
   struct frame *emacsframe;
   int rows, cols;
   int scrollbarsNeedingUpdate;
   EmacsToolbar *toolbar;
382
   NSRect ns_userRect;
383
   BOOL wait_for_tool_bar;
384 385 386
   }

/* AppKit-side interface */
387
- menuDown: (id)sender;
388 389 390 391 392 393 394 395 396 397 398 399 400
- toolbarClicked: (id)item;
- toggleToolbar: (id)sender;
- (void)keyDown: (NSEvent *)theEvent;
- (void)mouseDown: (NSEvent *)theEvent;
- (void)mouseUp: (NSEvent *)theEvent;
- setMiniwindowImage: (BOOL)setMini;

/* Emacs-side interface */
- initFrameFromEmacs: (struct frame *) f;
- (void) setRows: (int) r andColumns: (int) c;
- (void) setWindowClosing: (BOOL)closing;
- (EmacsToolbar *) toolbar;
- (void) deleteWorkingText;
401
- (void) updateFrameSize: (BOOL) delay;
402 403 404
- (void) handleFS;
- (void) setFSValue: (int)value;
- (void) toggleFullScreen: (id) sender;
405 406 407
- (BOOL) fsIsNative;
- (BOOL) isFullscreen;
#ifdef HAVE_NATIVE_FS
Paul Eggert's avatar
Paul Eggert committed
408
- (void) updateCollectionBehavior;
409
#endif
410 411

#ifdef NS_IMPL_GNUSTEP
412
- (void)windowDidMove: (id)sender;
413
#endif
414
- (int)fullscreenState;
415 416 417 418 419 420 421 422 423 424 425
@end


/* Small utility used for processing resize events under Cocoa. */
@interface EmacsWindow : NSWindow
{
  NSPoint grabOffset;
}
@end


426 427 428 429 430 431
/* Fullscreen version of the above.  */
@interface EmacsFSWindow : EmacsWindow
{
}
@end

432 433 434 435 436 437
/* ==========================================================================

   The main menu implementation

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

438
#ifdef NS_IMPL_COCOA
439 440 441 442
@interface EmacsMenu : NSMenu  <NSMenuDelegate>
#else
@interface EmacsMenu : NSMenu
#endif
443 444 445 446 447 448 449 450
{
  struct frame *frame;
  unsigned long keyEquivModMask;
}

- initWithTitle: (NSString *)title frame: (struct frame *)f;
- (void)setFrame: (struct frame *)f;
- (void)menuNeedsUpdate: (NSMenu *)menu; /* (delegate method) */
Jan D's avatar
Jan D committed
451
- (NSString *)parseKeyEquiv: (const char *)key;
452
- (NSMenuItem *)addItemWithWidgetValue: (void *)wvptr;
453
- (void)fillWithWidgetValue: (void *)wvptr;
Jan Djärv's avatar
Jan Djärv committed
454
- (void)fillWithWidgetValue: (void *)wvptr frame: (struct frame *)f;
Jan D's avatar
Jan D committed
455
- (EmacsMenu *)addSubmenuWithTitle: (const char *)title forFrame: (struct frame *)f;
456 457
- (void) clear;
- (Lisp_Object)runMenuAt: (NSPoint)p forFrame: (struct frame *)f
458
                 keymaps: (bool)keymaps;
459 460 461 462 463 464 465 466 467 468 469
@end


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

   Toolbar

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

@class EmacsImage;

470
#ifdef NS_IMPL_COCOA
471 472 473 474
@interface EmacsToolbar : NSToolbar <NSToolbarDelegate>
#else
@interface EmacsToolbar : NSToolbar
#endif
475 476 477 478 479 480 481 482 483
   {
     EmacsView *emacsView;
     NSMutableDictionary *identifierToItem;
     NSMutableArray *activeIdentifiers;
     NSArray *prevIdentifiers;
     unsigned long enablement, prevEnablement;
   }
- initForView: (EmacsView *)view withIdentifier: (NSString *)identifier;
- (void) clearActive;
484
- (void) clearAll;
485
- (BOOL) changed;
486 487 488
- (void) addDisplayItemWithImage: (EmacsImage *)img
                             idx: (int)idx
                             tag: (int)tag
Jan D's avatar
Jan D committed
489
                        helpText: (const char *)help
490
                         enabled: (BOOL)enabled;
491

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
/* delegate methods */
- (NSToolbarItem *)toolbar: (NSToolbar *)toolbar
     itemForItemIdentifier: (NSString *)itemIdentifier
 willBeInsertedIntoToolbar: (BOOL)flag;
- (NSArray *)toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar;
- (NSArray *)toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar;
@end


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

   Message / question windows

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

@interface EmacsDialogPanel : NSPanel
   {
   NSTextField *command;
   NSTextField *title;
   NSMatrix *matrix;
   int rows, cols;
513
   BOOL timer_fired, window_closed;
514
   Lisp_Object dialog_return;
515
   Lisp_Object *button_values;
516 517
   }
- initFromContents: (Lisp_Object)menu isQuestion: (BOOL)isQ;
518 519 520 521
- (void)process_dialog: (Lisp_Object)list;
- (void)addButton: (char *)str value: (int)tag row: (int)row;
- (void)addString: (char *)str row: (int)row;
- (void)addSplit;
522
- (Lisp_Object)runDialogAt: (NSPoint)p;
523
- (void)timeout_handler: (NSTimer *)timedEntry;
524 525
@end

526
#ifdef NS_IMPL_COCOA
527 528 529 530
@interface EmacsTooltip : NSObject <NSWindowDelegate>
#else
@interface EmacsTooltip : NSObject
#endif
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
  {
    NSWindow *win;
    NSTextField *textField;
    NSTimer *timer;
  }
- init;
- (void) setText: (char *)text;
- (void) showAtX: (int)x Y: (int)y for: (int)seconds;
- (void) hide;
- (BOOL) isActive;
- (NSRect) frame;
@end


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

   File open/save panels
548
   This and next override methods to handle keyboard input in panels.
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581

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

@interface EmacsSavePanel : NSSavePanel
{
}
@end
@interface EmacsOpenPanel : NSOpenPanel
{
}
@end

@interface EmacsFileDelegate : NSObject
{
}
- (BOOL)panel: (id)sender isValidFilename: (NSString *)filename;
- (BOOL)panel: (id)sender shouldShowFilename: (NSString *)filename;
- (NSString *)panel: (id)sender userEnteredFilename: (NSString *)filename
          confirmed: (BOOL)okFlag;
@end


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

   Images and stippling

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

@interface EmacsImage : NSImage
{
  NSBitmapImageRep *bmRep; /* used for accessing pixel data */
  unsigned char *pixmapData[5]; /* shortcut to access pixel data */
  NSColor *stippleMask;
582
  unsigned long xbm_fg;
583 584 585 586
}
+ allocInitFromFile: (Lisp_Object)file;
- (void)dealloc;
- initFromXBM: (unsigned char *)bits width: (int)w height: (int)h
587
                  fg: (unsigned long)fg bg: (unsigned long)bg;
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
- setXBMColor: (NSColor *)color;
- initForXPMWithDepth: (int)depth width: (int)width height: (int)height;
- (void)setPixmapData;
- (unsigned long)getPixelAtX: (int)x Y: (int)y;
- (void)setPixelAtX: (int)x Y: (int)y toRed: (unsigned char)r
               green: (unsigned char)g blue: (unsigned char)b
              alpha:(unsigned char)a;
- (void)setAlphaAtX: (int)x Y: (int)y to: (unsigned char)a;
- (NSColor *)stippleMask;
@end


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

   Scrollbars

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

@interface EmacsScroller : NSScroller
  {
608
   struct window *window;
609 610 611 612
   struct frame *frame;
   NSResponder *prevResponder;

   /* offset to the bottom of knob of last mouse down */
613
   CGFloat last_mouse_offset;
614 615
   float min_portion;
   int pixel_height;
616
   enum scroll_bar_part last_hit_part;
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

   BOOL condemned;

   /* optimize against excessive positioning calls generated by emacs */
   int em_position;
   int em_portion;
   int em_whole;
   }

- initFrame: (NSRect )r window: (Lisp_Object)win;
- (void)setFrame: (NSRect)r;

- setPosition: (int) position portion: (int) portion whole: (int) whole;
- (int) checkSamePosition: (int)position portion: (int)portion
                    whole: (int)whole;
- (void) sendScrollEventAtLoc: (float)loc fromEvent: (NSEvent *)e;
- repeatScroll: (NSTimer *)sender;
- condemn;
- reprieve;
636
- (bool)judge;
Jan Djärv's avatar
Jan Djärv committed
637
+ (CGFloat)scrollerWidth;
638 639 640 641 642
@end


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

643
   Rendering
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

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

#ifdef NS_IMPL_COCOA
/* rendering util */
@interface EmacsGlyphStorage : NSObject <NSGlyphStorage>
{
@public
  NSAttributedString *attrStr;
  NSMutableDictionary *dict;
  CGGlyph *cglyphs;
  unsigned long maxChar, maxGlyph;
  long i, len;
}
- initWithCapacity: (unsigned long) c;
- (void) setString: (NSString *)str font: (NSFont *)font;
@end
#endif	/* NS_IMPL_COCOA */

extern NSArray *ns_send_types, *ns_return_types;
664
extern NSString *ns_app_name;
665
extern EmacsMenu *mainMenu, *svcsMenu, *dockMenu;
666 667

/* Apple removed the declaration, but kept the implementation */
668
#if defined (NS_IMPL_COCOA)
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
@interface NSApplication (EmacsApp)
- (void)setAppleMenu: (NSMenu *)menu;
@end
#endif

#endif  /* __OBJC__ */



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

   Non-OO stuff

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

684 685 686 687 688 689 690 691 692 693 694
/* Special keycodes that we pass down the event chain */
#define KEY_NS_POWER_OFF               ((1<<28)|(0<<16)|1)
#define KEY_NS_OPEN_FILE               ((1<<28)|(0<<16)|2)
#define KEY_NS_OPEN_TEMP_FILE          ((1<<28)|(0<<16)|3)
#define KEY_NS_CHANGE_FONT             ((1<<28)|(0<<16)|7)
#define KEY_NS_OPEN_FILE_LINE          ((1<<28)|(0<<16)|8)
#define KEY_NS_PUT_WORKING_TEXT        ((1<<28)|(0<<16)|9)
#define KEY_NS_UNPUT_WORKING_TEXT      ((1<<28)|(0<<16)|10)
#define KEY_NS_SPI_SERVICE_CALL        ((1<<28)|(0<<16)|11)
#define KEY_NS_NEW_FRAME               ((1<<28)|(0<<16)|12)
#define KEY_NS_TOGGLE_TOOLBAR          ((1<<28)|(0<<16)|13)
695
#define KEY_NS_SHOW_PREFS              ((1<<28)|(0<<16)|14)
696

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
/* could use list to store these, but rest of emacs has a big infrastructure
   for managing a table of bitmap "records" */
struct ns_bitmap_record
{
#ifdef __OBJC__
  EmacsImage *img;
#else
  void *img;
#endif
  char *file;
  int refcount;
  int height, width, depth;
};

/* this to map between emacs color indices and NSColor objects */
struct ns_color_table
{
714 715
  ptrdiff_t size;
  ptrdiff_t avail;
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
#ifdef __OBJC__
  NSColor **colors;
  NSMutableSet *empty_indices;
#else
  void **items;
  void *availIndices;
#endif
};
#define NS_COLOR_CAPACITY 256

#define RGB_TO_ULONG(r, g, b) (((r) << 16) | ((g) << 8) | (b))
#define ARGB_TO_ULONG(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))

#define ALPHA_FROM_ULONG(color) ((color) >> 24)
#define RED_FROM_ULONG(color) (((color) >> 16) & 0xff)
#define GREEN_FROM_ULONG(color) (((color) >> 8) & 0xff)
#define BLUE_FROM_ULONG(color) ((color) & 0xff)

/* Do not change `* 0x101' in the following lines to `<< 8'.  If
   changed, image masks in 1-bit depth will not work. */
#define RED16_FROM_ULONG(color) (RED_FROM_ULONG(color) * 0x101)
#define GREEN16_FROM_ULONG(color) (GREEN_FROM_ULONG(color) * 0x101)
#define BLUE16_FROM_ULONG(color) (BLUE_FROM_ULONG(color) * 0x101)

/* this extends font backend font */
struct nsfont_info
{
  struct font font;

Juanma Barranquero's avatar
Juanma Barranquero committed
745
  char *name;  /* PostScript name, uniquely identifies on NS systems */
746 747 748 749

  /* The following metrics are stored as float rather than int. */

  float width;  /* Maximum advance for the font.  */
750 751 752 753 754 755
  float height;
  float underpos;
  float underwidth;
  float size;
#ifdef __OBJC__
  NSFont *nsfont;
756
#if defined (NS_IMPL_COCOA)
757
  CGFontRef cgfont;
758
#else /* GNUstep */
759 760 761 762 763 764 765 766
  void *cgfont;
#endif
#else /* ! OBJC */
  void *nsfont;
  void *cgfont;
#endif
  char bold, ital;  /* convenience flags */
  char synthItal;
767
  XCharStruct max_bounds;
768 769
  /* we compute glyph codes and metrics on-demand in blocks of 256 indexed
     by hibyte, lobyte */
Juanma Barranquero's avatar
Juanma Barranquero committed
770
  unsigned short **glyphs; /* map Unicode index to glyph */
771 772 773 774 775 776 777 778 779 780 781 782 783
  struct font_metrics **metrics;
};


/* init'd in ns_initialize_display_info () */
struct ns_display_info
{
  /* Chain of all ns_display_info structures.  */
  struct ns_display_info *next;

  /* The generic display parameters corresponding to this NS display. */
  struct terminal *terminal;

784
  /* This is a cons cell of the form (NAME . FONT-LIST-CACHE).  */
785 786 787 788 789 790 791 792 793 794 795 796
  Lisp_Object name_list_element;

  /* The number of fonts loaded. */
  int n_fonts;

  /* Minimum width over all characters in all fonts in font_table.  */
  int smallest_char_width;

  /* Minimum font height over all fonts in font_table.  */
  int smallest_font_height;

  struct ns_bitmap_record *bitmaps;
797 798
  ptrdiff_t bitmaps_size;
  ptrdiff_t bitmaps_last;
799 800 801

  struct ns_color_table *color_table;

802
  /* DPI resolution of this screen */
803 804
  double resx, resy;

805
  /* Mask of things that cause the mouse to be grabbed */
806 807 808 809 810 811 812 813
  int grabbed;

  int n_planes;

  int color_p;

  Window root_window;

814
  /* Xism */
815 816
  XrmDatabase xrdb;

817
  /* The cursor to use for vertical scroll bars. */
818 819
  Cursor vertical_scroll_bar_cursor;

820 821 822
  /* The cursor to use for horizontal scroll bars. */
  Cursor horizontal_scroll_bar_cursor;

823 824 825
  /* Information about the range of text currently shown in
     mouse-face.  */
  Mouse_HLInfo mouse_highlight;
826

827 828
  struct frame *x_highlight_frame;
  struct frame *x_focus_frame;
829 830 831

  /* The frame where the mouse was last time we reported a mouse event.  */
  struct frame *last_mouse_frame;
832 833 834 835 836 837 838 839

  /* The frame where the mouse was last time we reported a mouse motion.  */
  struct frame *last_mouse_motion_frame;

  /* Position where the mouse was last time we reported a motion.
     This is a position on last_mouse_motion_frame.  */
  int last_mouse_motion_x;
  int last_mouse_motion_y;
840 841 842 843 844 845 846 847 848 849 850 851 852

  /* Where the mouse was last time we reported a mouse position.  */
  NSRect last_mouse_glyph;

  /* Time of last mouse movement.  */
  Time last_mouse_movement_time;

  /* The scroll bar in which the last motion event occurred.  */
#ifdef __OBJC__
  EmacsScroller *last_mouse_scroll_bar;
#else
  void *last_mouse_scroll_bar;
#endif
853 854 855
};

/* This is a chain of structures for all the NS displays currently in use.  */
856
extern struct ns_display_info *x_display_list;
857

858
extern struct ns_display_info *ns_display_info_for_name (Lisp_Object name);
859 860 861 862 863 864

struct ns_output
{
#ifdef __OBJC__
  EmacsView *view;
  id miniimage;
865
  NSColor *cursor_color;
866 867 868 869 870 871
  NSColor *foreground_color;
  NSColor *background_color;
  EmacsToolbar *toolbar;
#else
  void *view;
  void *miniimage;
872
  void *cursor_color;
873 874 875 876 877
  void *foreground_color;
  void *background_color;
  void *toolbar;
#endif

878
  /* NSCursors init'ed in initFrameFromEmacs */
879 880 881 882 883 884
  Cursor text_cursor;
  Cursor nontext_cursor;
  Cursor modeline_cursor;
  Cursor hand_cursor;
  Cursor hourglass_cursor;
  Cursor horizontal_drag_cursor;
885
  Cursor vertical_drag_cursor;
886

887
  /* NS-specific */
888 889
  Cursor current_pointer;

890
  /* lord knows why Emacs needs to know about our Window ids.. */
891 892 893 894 895 896 897 898 899 900
  Window window_desc, parent_desc;
  char explicit_parent;

  struct font *font;
  int baseline_offset;

  /* If a fontset is specified for this frame instead of font, this
     value contains an ID of the fontset, else -1.  */
  int fontset; /* only used with font_backend */

901 902
  int icon_top;
  int icon_left;
903 904 905 906 907 908 909 910 911 912 913 914 915

  /* The size of the extra width currently allotted for vertical
     scroll bars, in pixels.  */
  int vertical_scroll_bar_extra;

  /* The height of the titlebar decoration (included in NSWindow's frame). */
  int titlebar_height;

  /* The height of the toolbar if displayed, else 0. */
  int toolbar_height;

  /* This is the Emacs structure for the NS display this frame is on.  */
  struct ns_display_info *display_info;
916 917 918

  /* Non-zero if we are zooming (maximizing) the frame.  */
  int zooming;
919 920 921

  /* Non-zero if we are doing an animation, e.g. toggling the tool bar. */
  int in_animation;
922 923
};

924
/* this dummy decl needed to support TTYs */
925 926
struct x_output
{
927
  int unused;
928 929 930 931
};


/* This gives the ns_display_info structure for the display F is on.  */
932
#define FRAME_DISPLAY_INFO(f) ((f)->output_data.ns->display_info)
933 934 935 936 937 938 939
#define FRAME_X_OUTPUT(f) ((f)->output_data.ns)
#define FRAME_NS_WINDOW(f) ((f)->output_data.ns->window_desc)
#define FRAME_X_WINDOW(f) ((f)->output_data.ns->window_desc)

/* This is the `Display *' which frame F is on.  */
#define FRAME_NS_DISPLAY(f) (0)
#define FRAME_X_DISPLAY(f) (0)
940
#define FRAME_X_SCREEN(f) (0)
941
#define FRAME_X_VISUAL(f) FRAME_DISPLAY_INFO(f)->visual
942 943 944 945 946 947 948

#define FRAME_FOREGROUND_COLOR(f) ((f)->output_data.ns->foreground_color)
#define FRAME_BACKGROUND_COLOR(f) ((f)->output_data.ns->background_color)

#define NS_FACE_FOREGROUND(f) ((f)->foreground)
#define NS_FACE_BACKGROUND(f) ((f)->background)
#define FRAME_NS_TITLEBAR_HEIGHT(f) ((f)->output_data.ns->titlebar_height)
949
#define FRAME_TOOLBAR_HEIGHT(f) ((f)->output_data.ns->toolbar_height)
950 951 952 953

#define FRAME_DEFAULT_FACE(f) FACE_FROM_ID (f, DEFAULT_FACE_ID)

#define FRAME_NS_VIEW(f) ((f)->output_data.ns->view)
954
#define FRAME_CURSOR_COLOR(f) ((f)->output_data.ns->cursor_color)
955 956 957 958 959
#define FRAME_POINTER_TYPE(f) ((f)->output_data.ns->current_pointer)

#define FRAME_FONT(f) ((f)->output_data.ns->font)

#ifdef __OBJC__
960
#define XNS_SCROLL_BAR(vec) ((id) XSAVE_POINTER (vec, 0))
961
#else
962
#define XNS_SCROLL_BAR(vec) XSAVE_POINTER (vec, 0)
963 964 965
#endif

/* Compute pixel size for vertical scroll bars */
966 967 968 969 970 971 972 973 974 975 976 977 978 979
#define NS_SCROLL_BAR_WIDTH(f)						\
  (FRAME_HAS_VERTICAL_SCROLL_BARS (f)					\
   ? rint (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0			\
	   ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)				\
	   : (FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)))	\
   : 0)

/* Compute pixel size for horizontal scroll bars */
#define NS_SCROLL_BAR_HEIGHT(f)						\
  (FRAME_HAS_HORIZONTAL_SCROLL_BARS (f)					\
   ? rint (FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) > 0			\
	   ? FRAME_CONFIG_SCROLL_BAR_HEIGHT (f)				\
	   : (FRAME_SCROLL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)))	\
   : 0)
980 981 982 983 984 985 986 987

/* Difference btwn char-column-calculated and actual SB widths.
   This is only a concern for rendering when SB on left. */
#define NS_SCROLL_BAR_ADJUST(w, f)		\
(WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w) ?	\
    (FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)	\
        - NS_SCROLL_BAR_WIDTH (f)) : 0)

988 989 990 991 992 993 994
/* Difference btwn char-line-calculated and actual SB heights.
   This is only a concern for rendering when SB on top. */
#define NS_SCROLL_BAR_ADJUST_HORIZONTALLY(w, f)		\
  (WINDOW_HAS_HORIZONTAL_SCROLL_BARS (w) ?		\
   (FRAME_SCROLL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)	\
    - NS_SCROLL_BAR_HEIGHT (f)) : 0)

995
/* XXX: fix for GNUstep inconsistent accounting for titlebar */
996 997 998 999 1000 1001
#ifdef NS_IMPL_GNUSTEP
#define NS_TOP_POS(f) ((f)->top_pos + 18)
#else
#define NS_TOP_POS(f) ((f)->top_pos)
#endif

1002
#define FRAME_NS_FONT_TABLE(f) (FRAME_DISPLAY_INFO (f)->font_table)
1003 1004 1005 1006 1007 1008 1009 1010 1011

#define FRAME_FONTSET(f) ((f)->output_data.ns->fontset)

#define FRAME_BASELINE_OFFSET(f) ((f)->output_data.ns->baseline_offset)
#define BLACK_PIX_DEFAULT(f) 0x000000
#define WHITE_PIX_DEFAULT(f) 0xFFFFFF

/* First position where characters can be shown (instead of scrollbar, if
   it is on left. */
1012 1013
#define FIRST_CHAR_POSITION(f)				\
  (! (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f)) ? 0	\
1014 1015
   : FRAME_SCROLL_BAR_COLS (f))

1016
extern struct ns_display_info *ns_term_init (Lisp_Object display_name);
1017 1018 1019 1020 1021 1022 1023 1024 1025
extern void ns_term_shutdown (int sig);

/* constants for text rendering */
#define NS_DUMPGLYPH_NORMAL             0
#define NS_DUMPGLYPH_CURSOR             1
#define NS_DUMPGLYPH_FOREGROUND         2
#define NS_DUMPGLYPH_MOUSEFACE          3


1026

1027 1028 1029 1030 1031 1032
/* In nsfont, called from fontset.c */
extern void nsfont_make_fontset_for_font (Lisp_Object name,
                                         Lisp_Object font_object);

/* In nsfont, for debugging */
struct glyph_string;
1033
void ns_dump_glyphstring (struct glyph_string *s);
1034 1035

/* Implemented in nsterm, published in or needed from nsfns. */
Dmitry Antipov's avatar
Dmitry Antipov committed
1036
extern Lisp_Object ns_list_fonts (struct frame *f, Lisp_Object pattern,
1037 1038 1039 1040 1041
                                  int size, int maxnames);
extern void ns_clear_frame (struct frame *f);

extern const char *ns_xlfd_to_fontname (const char *xlfd);

1042 1043 1044 1045 1046
extern Lisp_Object ns_map_event_to_object (void);
#ifdef __OBJC__
extern Lisp_Object ns_string_from_pasteboard (id pb);
extern void ns_string_to_pasteboard (id pb, Lisp_Object str);
#endif
1047 1048
extern Lisp_Object ns_get_local_selection (Lisp_Object selection_name,
                                           Lisp_Object target_type);
1049 1050
extern void nxatoms_of_nsselect (void);
extern int ns_lisp_to_cursor_type (Lisp_Object arg);
1051
extern Lisp_Object ns_cursor_type_to_lisp (int arg);
1052
extern void ns_set_name_as_filename (struct frame *f);
1053
extern void ns_set_doc_edited (void);
1054

1055
extern bool
1056 1057
ns_defined_color (struct frame *f,
                  const char *name,
1058 1059
                  XColor *color_def, bool alloc,
                  bool makeIndex);
1060 1061
extern void
ns_query_color (void *col, XColor *color_def, int setPixel);
1062 1063

#ifdef __OBJC__
1064
extern Lisp_Object ns_color_to_lisp (NSColor *col);
1065 1066 1067 1068
extern int ns_lisp_to_color (Lisp_Object color, NSColor **col);
extern NSColor *ns_lookup_indexed_color (unsigned long idx, struct frame *f);
extern unsigned long ns_index_color (NSColor *color, struct frame *f);
extern void ns_free_indexed_color (unsigned long idx, struct frame *f);
1069
extern const char *ns_get_pending_menu_title (void);
1070
extern void ns_check_menu_open (NSMenu *menu);
1071
extern void ns_check_pending_open_menu (void);
1072 1073 1074 1075 1076
#endif

/* C access to ObjC functionality */
extern void  ns_release_object (void *obj);
extern void  ns_retain_object (void *obj);
1077 1078 1079
extern void *ns_alloc_autorelease_pool (void);
extern void ns_release_autorelease_pool (void *);
extern const char *ns_get_defaults_value (const char *key);
1080 1081

/* in nsmenu */
Dmitry Antipov's avatar
Dmitry Antipov committed
1082 1083 1084
extern void update_frame_tool_bar (struct frame *f);
extern void free_frame_tool_bar (struct frame *f);
extern void find_and_call_menu_selection (struct frame *f,
1085
    int menu_bar_items_used, Lisp_Object vector, void *client_data);
Dmitry Antipov's avatar
Dmitry Antipov committed
1086
extern Lisp_Object find_and_return_menu_selection (struct frame *f,
1087
                                                   bool keymaps,
1088
                                                   void *client_data);
1089
extern Lisp_Object ns_popup_dialog (struct frame *, Lisp_Object header,
Eli Zaretskii's avatar
Eli Zaretskii committed
1090
                                    Lisp_Object contents);
1091

1092 1093 1094
#define NSAPP_DATA2_RUNASSCRIPT 10
extern void ns_run_ascript (void);

1095 1096 1097
#define NSAPP_DATA2_RUNFILEDIALOG 11
extern void ns_run_file_dialog (void);

1098
extern const char *ns_etc_directory (void);
1099
extern const char *ns_exec_path (void);
1100
extern const char *ns_load_path (void);
1101 1102 1103 1104
extern void syms_of_nsterm (void);
extern void syms_of_nsfns (void);
extern void syms_of_nsmenu (void);
extern void syms_of_nsselect (void);
1105 1106 1107

/* From nsimage.m, needed in image.c */
struct image;
1108 1109
extern void *ns_image_from_XBM (unsigned char *bits, int width, int height,
                                unsigned long fg, unsigned long bg);
1110 1111
extern void *ns_image_for_XPM (int width, int height, int depth);
extern void *ns_image_from_file (Lisp_Object file);
1112 1113
extern bool ns_load_image (struct frame *f, struct image *img,
			   Lisp_Object spec_file, Lisp_Object spec_data);
1114 1115 1116 1117 1118 1119
extern int ns_image_width (void *img);
extern int ns_image_height (void *img);
extern unsigned long ns_get_pixel (void *img, int x, int y);
extern void ns_put_pixel (void *img, int x, int y, unsigned long argb);
extern void ns_set_alpha (void *img, int x, int y, unsigned char a);

Jan D's avatar
Jan D committed
1120 1121
extern int x_display_pixel_height (struct ns_display_info *);
extern int x_display_pixel_width (struct ns_display_info *);
1122

1123
/* This in nsterm.m */
1124
extern void x_destroy_window (struct frame *f);
1125
extern int ns_select (int nfds, fd_set *readfds, fd_set *writefds,
1126
		      fd_set *exceptfds, struct timespec const *timeout,
1127
		      sigset_t const *sigmask);
1128 1129 1130
extern unsigned long ns_get_rgb_color (struct frame *f,
                                       float r, float g, float b, float a);

1131 1132 1133
extern void ns_init_events ();
extern void ns_finish_events ();

1134
#ifdef __OBJC__
1135
/* From nsterm.m, needed in nsfont.m. */
1136 1137 1138
extern void
ns_draw_text_decoration (struct glyph_string *s, struct face *face,
                         NSColor *defaultCol, CGFloat width, CGFloat x);
1139 1140 1141 1142
/* Needed in nsfns.m.  */
extern void
ns_set_represented_filename (NSString* fstr, struct frame *f);

1143 1144
#endif

1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
#ifdef NS_IMPL_GNUSTEP
extern char gnustep_base_version[];  /* version tracking */
#endif

#define MINWIDTH 10
#define MINHEIGHT 10

/* Screen max coordinate
 Using larger coordinates causes movewindow/placewindow to abort */
#define SCREENMAX 16000

#define NS_SCROLL_BAR_WIDTH_DEFAULT     [EmacsScroller scrollerWidth]
1157
#define NS_SCROLL_BAR_HEIGHT_DEFAULT    [EmacsScroller scrollerHeight]
1158
/* This is to match emacs on other platforms, ugly though it is. */
1159 1160
#define NS_SELECTION_BG_COLOR_DEFAULT	@"LightGoldenrod2";
#define NS_SELECTION_FG_COLOR_DEFAULT	@"Black";
1161 1162 1163 1164 1165 1166 1167 1168
#define RESIZE_HANDLE_SIZE 12

/* Little utility macros */
#define IN_BOUND(min, x, max) (((x) < (min)) \
                                ? (min) : (((x)>(max)) ? (max) : (x)))
#define SCREENMAXBOUND(x) (IN_BOUND (-SCREENMAX, x, SCREENMAX))

#endif	/* HAVE_NS */