From ba695106b417f95931b85f8103dd02f7849cdccd Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 01:54:44 +0000 Subject: New file. --- src/mactoolbox.c | 6208 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 6208 insertions(+) create mode 100644 src/mactoolbox.c (limited to 'src') diff --git a/src/mactoolbox.c b/src/mactoolbox.c new file mode 100644 index 00000000000..e1fa79ac8be --- /dev/null +++ b/src/mactoolbox.c @@ -0,0 +1,6208 @@ +/* Functions for GUI implemented with (HI)Toolbox on the Mac OS. + Copyright (C) 2000, 2001, 2002, 2003, 2004, + 2005, 2006, 2007, 2008 Free Software Foundation, Inc. + +This file is part of GNU Emacs. + +GNU Emacs is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +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 +along with GNU Emacs; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +Boston, MA 02110-1301, USA. */ + +#include + +#include + +#include "lisp.h" +#include "blockinput.h" + +#include "macterm.h" + +#if !TARGET_API_MAC_CARBON +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if defined (__MRC__) || (__MSL__ >= 0x6000) +#include +#endif + +#if __profile__ +#include +#endif +#endif /* not TARGET_API_MAC_CARBON */ + +#include "charset.h" +#include "coding.h" +#include "frame.h" +#include "dispextern.h" +#include "fontset.h" +#include "termhooks.h" +#include "buffer.h" +#include "window.h" +#include "keyboard.h" + +#include + +#ifndef MAC_OSX +#include +#endif + + +/************************************************************************ + General + ************************************************************************/ + +/* The difference in pixels between the top left corner of the + Emacs window (including possible window manager decorations) + and FRAME_MAC_WINDOW (f). */ +#define FRAME_OUTER_TO_INNER_DIFF_X(f) ((f)->x_pixels_diff) +#define FRAME_OUTER_TO_INNER_DIFF_Y(f) ((f)->y_pixels_diff) + +#define mac_window_to_frame(wp) (((mac_output *) GetWRefCon (wp))->mFP) + +void +mac_alert_sound_play () +{ +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 + AlertSoundPlay (); +#else + SysBeep (1); +#endif +} + + +/************************************************************************ + Application + ************************************************************************/ + +extern struct frame *mac_focus_frame P_ ((struct mac_display_info *)); +extern void do_keystroke P_ ((EventKind, unsigned char, UInt32, UInt32, + unsigned long, struct input_event *)); +extern UInt32 mac_mapped_modifiers P_ ((UInt32, UInt32)); +#if TARGET_API_MAC_CARBON +extern int mac_to_emacs_modifiers P_ ((UInt32, UInt32)); +#else +extern int mac_to_emacs_modifiers P_ ((EventModifiers, EventModifiers)); +#endif + +#if TARGET_API_MAC_CARBON +/* Points to the variable `inev' in the function XTread_socket. It is + used for passing an input event to the function back from + Carbon/Apple event handlers. */ +static struct input_event *read_socket_inev = NULL; + +extern const unsigned char keycode_to_xkeysym_table[]; +extern EMACS_INT extra_keyboard_modifiers; + +extern Lisp_Object Qhi_command; +#if USE_MAC_TSM +static TSMDocumentID tsm_document_id; +extern Lisp_Object Qtext_input; +extern Lisp_Object Qupdate_active_input_area, Qunicode_for_key_event; +extern Lisp_Object Vmac_ts_active_input_overlay; +extern Lisp_Object Qbefore_string; +extern Lisp_Object Vmac_ts_script_language_on_focus; +extern Lisp_Object saved_ts_script_language_on_focus; +#endif + +static int mac_event_to_emacs_modifiers P_ ((EventRef)); +static OSStatus install_menu_target_item_handler P_ ((void)); +#ifdef MAC_OSX +static OSStatus install_service_handler P_ ((void)); +#endif + +extern OSStatus mac_store_event_ref_as_apple_event P_ ((AEEventClass, AEEventID, + Lisp_Object, + Lisp_Object, + EventRef, UInt32, + const EventParamName *, + const EventParamType *)); + +#if USE_MAC_TSM +extern OSStatus mac_restore_keyboard_input_source P_ ((void)); +extern void mac_save_keyboard_input_source P_ ((void)); + +static OSStatus +mac_tsm_resume () +{ + OSStatus err; + + err = ActivateTSMDocument (tsm_document_id); + if (err == noErr) + err = mac_restore_keyboard_input_source (); + + return err; +} + +static OSStatus +mac_tsm_suspend () +{ + OSStatus err; + + mac_save_keyboard_input_source (); + err = DeactivateTSMDocument (tsm_document_id); + + return err; +} + +static void +init_tsm () +{ +#ifdef MAC_OSX + static InterfaceTypeList types = {kUnicodeDocument}; +#else + static InterfaceTypeList types = {kTextService}; +#endif + + NewTSMDocument (sizeof (types) / sizeof (types[0]), types, + &tsm_document_id, 0); +} +#endif /* USE_MAC_TSM */ + +static pascal OSStatus +mac_handle_keyboard_event (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + OSStatus err, result = eventNotHandledErr; + UInt32 event_kind, key_code, modifiers; + unsigned char char_code; + + event_kind = GetEventKind (event); + switch (event_kind) + { + case kEventRawKeyDown: + case kEventRawKeyRepeat: + case kEventRawKeyUp: + /* When using Carbon Events, we need to pass raw keyboard events + to the TSM ourselves. If TSM handles it, it will pass back + noErr, otherwise it will pass back "eventNotHandledErr" and + we can process it normally. */ + result = CallNextEventHandler (next_handler, event); + if (result != eventNotHandledErr) + break; + + if (read_socket_inev == NULL) + break; + +#if USE_MAC_TSM + if (read_socket_inev->kind != NO_EVENT) + { + result = noErr; + break; + } +#endif + + if (event_kind == kEventRawKeyUp) + break; + + err = GetEventParameter (event, kEventParamKeyMacCharCodes, + typeChar, NULL, + sizeof (char), NULL, &char_code); + if (err != noErr) + break; + + err = GetEventParameter (event, kEventParamKeyCode, + typeUInt32, NULL, + sizeof (UInt32), NULL, &key_code); + if (err != noErr) + break; + + err = GetEventParameter (event, kEventParamKeyModifiers, + typeUInt32, NULL, + sizeof (UInt32), NULL, &modifiers); + if (err != noErr) + break; + + do_keystroke ((event_kind == kEventRawKeyDown ? keyDown : autoKey), + char_code, key_code, modifiers, + ((unsigned long) + (GetEventTime (event) / kEventDurationMillisecond)), + read_socket_inev); + result = noErr; + break; + + default: + abort (); + } + + return result; +} + +static pascal OSStatus +mac_handle_command_event (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + OSStatus err, result = eventNotHandledErr; + HICommand command; + static const EventParamName names[] = + {kEventParamDirectObject, kEventParamKeyModifiers}; + static const EventParamType types[] = + {typeHICommand, typeUInt32}; + int num_params = sizeof (names) / sizeof (names[0]); + + err = GetEventParameter (event, kEventParamDirectObject, typeHICommand, + NULL, sizeof (HICommand), NULL, &command); + if (err != noErr) + return eventNotHandledErr; + + switch (GetEventKind (event)) + { + case kEventCommandProcess: + result = CallNextEventHandler (next_handler, event); + if (result != eventNotHandledErr) + break; + + err = GetEventParameter (event, kEventParamDirectObject, + typeHICommand, NULL, + sizeof (HICommand), NULL, &command); + + if (err != noErr || command.commandID == 0) + break; + + /* A HI command event is mapped to an Apple event whose event + class symbol is `hi-command' and event ID is its command + ID. */ + err = mac_store_event_ref_as_apple_event (0, command.commandID, + Qhi_command, Qnil, + event, num_params, + names, types); + if (err == noErr) + result = noErr; + break; + + default: + abort (); + } + + return result; +} + +static pascal OSStatus +mac_handle_mouse_event (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + OSStatus err, result = eventNotHandledErr; + + switch (GetEventKind (event)) + { + case kEventMouseWheelMoved: + { + WindowRef wp; + struct frame *f; + EventMouseWheelAxis axis; + SInt32 delta; + Point point; + + result = CallNextEventHandler (next_handler, event); + if (result != eventNotHandledErr || read_socket_inev == NULL) + break; + + f = mac_focus_frame (&one_mac_display_info); + + err = GetEventParameter (event, kEventParamWindowRef, typeWindowRef, + NULL, sizeof (WindowRef), NULL, &wp); + if (err != noErr + || wp != FRAME_MAC_WINDOW (f)) + break; + + err = GetEventParameter (event, kEventParamMouseWheelAxis, + typeMouseWheelAxis, NULL, + sizeof (EventMouseWheelAxis), NULL, &axis); + if (err != noErr || axis != kEventMouseWheelAxisY) + break; + + err = GetEventParameter (event, kEventParamMouseLocation, + typeQDPoint, NULL, sizeof (Point), + NULL, &point); + if (err != noErr) + break; + + point.h -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); + point.v -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); + if (point.h < 0 || point.v < 0 + || EQ (window_from_coordinates (f, point.h, point.v, 0, 0, 0, 1), + f->tool_bar_window)) + break; + + err = GetEventParameter (event, kEventParamMouseWheelDelta, + typeSInt32, NULL, sizeof (SInt32), + NULL, &delta); + if (err != noErr) + break; + + read_socket_inev->kind = WHEEL_EVENT; + read_socket_inev->code = 0; + read_socket_inev->modifiers = + (mac_event_to_emacs_modifiers (event) + | ((delta < 0) ? down_modifier : up_modifier)); + XSETINT (read_socket_inev->x, point.h); + XSETINT (read_socket_inev->y, point.v); + XSETFRAME (read_socket_inev->frame_or_window, f); + + result = noErr; + } + break; + + default: + abort (); + } + + return result; +} + +#if USE_MAC_TSM +static pascal OSStatus +mac_handle_text_input_event (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + OSStatus err, result; + Lisp_Object id_key = Qnil; + int num_params; + const EventParamName *names; + const EventParamType *types; + static UInt32 seqno_uaia = 0; + static const EventParamName names_uaia[] = + {kEventParamTextInputSendComponentInstance, + kEventParamTextInputSendRefCon, + kEventParamTextInputSendSLRec, + kEventParamTextInputSendFixLen, + kEventParamTextInputSendText, + kEventParamTextInputSendUpdateRng, + kEventParamTextInputSendHiliteRng, + kEventParamTextInputSendClauseRng, + kEventParamTextInputSendPinRng, + kEventParamTextInputSendTextServiceEncoding, + kEventParamTextInputSendTextServiceMacEncoding, + EVENT_PARAM_TEXT_INPUT_SEQUENCE_NUMBER}; + static const EventParamType types_uaia[] = + {typeComponentInstance, + typeLongInteger, + typeIntlWritingCode, + typeLongInteger, +#ifdef MAC_OSX + typeUnicodeText, +#else + typeChar, +#endif + typeTextRangeArray, + typeTextRangeArray, + typeOffsetArray, + typeTextRange, + typeUInt32, + typeUInt32, + typeUInt32}; + static const EventParamName names_ufke[] = + {kEventParamTextInputSendComponentInstance, + kEventParamTextInputSendRefCon, + kEventParamTextInputSendSLRec, + kEventParamTextInputSendText}; + static const EventParamType types_ufke[] = + {typeComponentInstance, + typeLongInteger, + typeIntlWritingCode, + typeUnicodeText}; + + result = CallNextEventHandler (next_handler, event); + if (result != eventNotHandledErr) + return result; + + switch (GetEventKind (event)) + { + case kEventTextInputUpdateActiveInputArea: + id_key = Qupdate_active_input_area; + num_params = sizeof (names_uaia) / sizeof (names_uaia[0]); + names = names_uaia; + types = types_uaia; + SetEventParameter (event, EVENT_PARAM_TEXT_INPUT_SEQUENCE_NUMBER, + typeUInt32, sizeof (UInt32), &seqno_uaia); + seqno_uaia++; + result = noErr; + break; + + case kEventTextInputUnicodeForKeyEvent: + { + EventRef kbd_event; + UInt32 actual_size, modifiers, key_code; + + err = GetEventParameter (event, kEventParamTextInputSendKeyboardEvent, + typeEventRef, NULL, sizeof (EventRef), NULL, + &kbd_event); + if (err == noErr) + err = GetEventParameter (kbd_event, kEventParamKeyModifiers, + typeUInt32, NULL, + sizeof (UInt32), NULL, &modifiers); + if (err == noErr) + err = GetEventParameter (kbd_event, kEventParamKeyCode, + typeUInt32, NULL, sizeof (UInt32), + NULL, &key_code); + if (err == noErr && mac_mapped_modifiers (modifiers, key_code)) + /* There're mapped modifier keys. Process it in + do_keystroke. */ + break; + if (err == noErr) + err = GetEventParameter (kbd_event, kEventParamKeyUnicodes, + typeUnicodeText, NULL, 0, &actual_size, + NULL); + if (err == noErr && actual_size == sizeof (UniChar)) + { + UniChar code; + + err = GetEventParameter (kbd_event, kEventParamKeyUnicodes, + typeUnicodeText, NULL, + sizeof (UniChar), NULL, &code); + if (err == noErr && code < 0x80) + { + /* ASCII character. Process it in do_keystroke. */ + if (read_socket_inev && code >= 0x20 && code <= 0x7e + && !(key_code <= 0x7f + && keycode_to_xkeysym_table [key_code])) + { + struct frame *f = mac_focus_frame (&one_mac_display_info); + + read_socket_inev->kind = ASCII_KEYSTROKE_EVENT; + read_socket_inev->code = code; + read_socket_inev->modifiers = + mac_to_emacs_modifiers (modifiers, 0); + read_socket_inev->modifiers |= + (extra_keyboard_modifiers + & (meta_modifier | alt_modifier + | hyper_modifier | super_modifier)); + XSETFRAME (read_socket_inev->frame_or_window, f); + } + break; + } + } + if (err == noErr) + { + /* Non-ASCII keystrokes without mapped modifiers are + processed at the Lisp level. */ + id_key = Qunicode_for_key_event; + num_params = sizeof (names_ufke) / sizeof (names_ufke[0]); + names = names_ufke; + types = types_ufke; + result = noErr; + } + } + break; + + case kEventTextInputOffsetToPos: + { + struct frame *f; + struct window *w; + Point p; + + if (!OVERLAYP (Vmac_ts_active_input_overlay)) + break; + + /* Strictly speaking, this is not always correct because + previous events may change some states about display. */ + if (!NILP (Foverlay_get (Vmac_ts_active_input_overlay, Qbefore_string))) + { + /* Active input area is displayed around the current point. */ + f = SELECTED_FRAME (); + w = XWINDOW (f->selected_window); + } + else if (WINDOWP (echo_area_window)) + { + /* Active input area is displayed in the echo area. */ + w = XWINDOW (echo_area_window); + f = WINDOW_XFRAME (w); + } + else + break; + + p.h = (WINDOW_TO_FRAME_PIXEL_X (w, w->cursor.x) + + WINDOW_LEFT_FRINGE_WIDTH (w) + + f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f)); + p.v = (WINDOW_TO_FRAME_PIXEL_Y (w, w->cursor.y) + + FONT_BASE (FRAME_FONT (f)) + + f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f)); + err = SetEventParameter (event, kEventParamTextInputReplyPoint, + typeQDPoint, sizeof (typeQDPoint), &p); + if (err == noErr) + result = noErr; + } + break; + + default: + abort (); + } + + if (!NILP (id_key)) + err = mac_store_event_ref_as_apple_event (0, 0, Qtext_input, id_key, + event, num_params, + names, types); + return result; +} +#endif + +OSStatus +install_application_handler () +{ + OSStatus err = noErr; + + if (err == noErr) + { + static const EventTypeSpec specs[] = + {{kEventClassKeyboard, kEventRawKeyDown}, + {kEventClassKeyboard, kEventRawKeyRepeat}, + {kEventClassKeyboard, kEventRawKeyUp}}; + + err = InstallApplicationEventHandler (NewEventHandlerUPP + (mac_handle_keyboard_event), + GetEventTypeCount (specs), + specs, NULL, NULL); + } + + if (err == noErr) + { + static const EventTypeSpec specs[] = + {{kEventClassCommand, kEventCommandProcess}}; + + err = InstallApplicationEventHandler (NewEventHandlerUPP + (mac_handle_command_event), + GetEventTypeCount (specs), + specs, NULL, NULL); + } + + if (err == noErr) + { + static const EventTypeSpec specs[] = + {{kEventClassMouse, kEventMouseWheelMoved}}; + + err = InstallApplicationEventHandler (NewEventHandlerUPP + (mac_handle_mouse_event), + GetEventTypeCount (specs), + specs, NULL, NULL); + } + +#if USE_MAC_TSM + if (err == noErr) + { + static const EventTypeSpec spec[] = + {{kEventClassTextInput, kEventTextInputUpdateActiveInputArea}, + {kEventClassTextInput, kEventTextInputUnicodeForKeyEvent}, + {kEventClassTextInput, kEventTextInputOffsetToPos}}; + + err = InstallApplicationEventHandler (NewEventHandlerUPP + (mac_handle_text_input_event), + GetEventTypeCount (spec), + spec, NULL, NULL); + } +#endif + + if (err == noErr) + err = install_menu_target_item_handler (); + +#ifdef MAC_OSX + if (err == noErr) + err = install_service_handler (); +#endif + + return err; +} +#endif /* TARGET_API_MAC_CARBON */ + + +/************************************************************************ + Windows + ************************************************************************/ + +#define DEFAULT_NUM_COLS 80 + +#define MIN_DOC_SIZE 64 +#define MAX_DOC_SIZE 32767 + +/* Drag and Drop */ +static OSErr install_drag_handler P_ ((WindowRef)); +static void remove_drag_handler P_ ((WindowRef)); + +#if USE_CG_DRAWING +static void mac_prepare_for_quickdraw P_ ((struct frame *)); +#endif + +extern void mac_handle_visibility_change P_ ((struct frame *)); +extern void mac_handle_origin_change P_ ((struct frame *)); +extern void mac_handle_size_change P_ ((struct frame *, int, int)); + +#if TARGET_API_MAC_CARBON +#ifdef MAC_OSX +extern Lisp_Object Qwindow; +extern Lisp_Object Qtoolbar_switch_mode; +#endif +#endif + +static void +do_window_update (WindowRef win) +{ + struct frame *f = mac_window_to_frame (win); + + BeginUpdate (win); + + /* The tooltip has been drawn already. Avoid the SET_FRAME_GARBAGED + below. */ + if (win != tip_window) + { + if (f->async_visible == 0) + { + /* Update events may occur when a frame gets iconified. */ +#if 0 + f->async_visible = 1; + f->async_iconified = 0; + SET_FRAME_GARBAGED (f); +#endif + } + else + { + Rect r; +#if TARGET_API_MAC_CARBON + RgnHandle region = NewRgn (); + + GetPortVisibleRegion (GetWindowPort (win), region); + GetRegionBounds (region, &r); + expose_frame (f, r.left, r.top, r.right - r.left, r.bottom - r.top); +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (f); +#endif + UpdateControls (win, region); + DisposeRgn (region); +#else + r = (*win->visRgn)->rgnBBox; + expose_frame (f, r.left, r.top, r.right - r.left, r.bottom - r.top); + UpdateControls (win, win->visRgn); +#endif + } + } + + EndUpdate (win); +} + +static int +is_emacs_window (WindowRef win) +{ + Lisp_Object tail, frame; + + if (!win) + return 0; + + FOR_EACH_FRAME (tail, frame) + if (FRAME_MAC_P (XFRAME (frame))) + if (FRAME_MAC_WINDOW (XFRAME (frame)) == win) + return 1; + + return 0; +} + +/* Handle drags in size box. Based on code contributed by Ben + Mesander and IM - Window Manager A. */ + +static void +do_grow_window (w, e) + WindowRef w; + const EventRecord *e; +{ + Rect limit_rect; + int rows, columns, width, height; + struct frame *f = mac_window_to_frame (w); + XSizeHints *size_hints = FRAME_SIZE_HINTS (f); + int min_width = MIN_DOC_SIZE, min_height = MIN_DOC_SIZE; +#if TARGET_API_MAC_CARBON + Rect new_rect; +#else + long grow_size; +#endif + + if (size_hints->flags & PMinSize) + { + min_width = size_hints->min_width; + min_height = size_hints->min_height; + } + SetRect (&limit_rect, min_width, min_height, MAX_DOC_SIZE, MAX_DOC_SIZE); + +#if TARGET_API_MAC_CARBON + if (!ResizeWindow (w, e->where, &limit_rect, &new_rect)) + return; + height = new_rect.bottom - new_rect.top; + width = new_rect.right - new_rect.left; +#else + grow_size = GrowWindow (w, e->where, &limit_rect); + /* see if it really changed size */ + if (grow_size == 0) + return; + height = HiWord (grow_size); + width = LoWord (grow_size); +#endif + + if (width != FRAME_PIXEL_WIDTH (f) + || height != FRAME_PIXEL_HEIGHT (f)) + { + rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); + columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); + + x_set_window_size (f, 0, columns, rows); + } +} + +#if TARGET_API_MAC_CARBON +static Point +mac_get_ideal_size (f) + struct frame *f; +{ + struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); + WindowRef w = FRAME_MAC_WINDOW (f); + Point ideal_size; + Rect standard_rect; + int height, width, columns, rows; + + ideal_size.h = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS); + ideal_size.v = dpyinfo->height; + IsWindowInStandardState (w, &ideal_size, &standard_rect); + /* Adjust the standard size according to character boundaries. */ + width = standard_rect.right - standard_rect.left; + height = standard_rect.bottom - standard_rect.top; + columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); + rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); + ideal_size.h = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, columns); + ideal_size.v = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows); + + return ideal_size; +} + +static pascal OSStatus +mac_handle_window_event (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + WindowRef wp; + OSStatus err, result = eventNotHandledErr; + struct frame *f; + UInt32 attributes; + XSizeHints *size_hints; + + err = GetEventParameter (event, kEventParamDirectObject, typeWindowRef, + NULL, sizeof (WindowRef), NULL, &wp); + if (err != noErr) + return eventNotHandledErr; + + f = mac_window_to_frame (wp); + switch (GetEventKind (event)) + { + /* -- window refresh events -- */ + + case kEventWindowUpdate: + result = CallNextEventHandler (next_handler, event); + if (result != eventNotHandledErr) + break; + + do_window_update (wp); + result = noErr; + break; + + /* -- window state change events -- */ + + case kEventWindowShowing: + size_hints = FRAME_SIZE_HINTS (f); + if (!(size_hints->flags & (USPosition | PPosition))) + { + struct frame *sf = SELECTED_FRAME (); + + if (!(FRAME_MAC_P (sf) && sf->async_visible)) + RepositionWindow (wp, NULL, kWindowCenterOnMainScreen); + else + { + RepositionWindow (wp, FRAME_MAC_WINDOW (sf), +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 + kWindowCascadeStartAtParentWindowScreen +#else + kWindowCascadeOnParentWindowScreen +#endif + ); +#if USE_MAC_TOOLBAR + /* This is a workaround. RepositionWindow fails to put + a window at the cascading position when its parent + window has a Carbon HIToolbar. */ + if ((f->left_pos == sf->left_pos + && f->top_pos == sf->top_pos) + || (f->left_pos == sf->left_pos + 10 * 2 + && f->top_pos == sf->top_pos + 32 * 2)) + MoveWindowStructure (wp, sf->left_pos + 10, sf->top_pos + 32); +#endif + } + result = noErr; + } + break; + + case kEventWindowHiding: + /* Before unmapping the window, update the WM_SIZE_HINTS + property to claim that the current position of the window is + user-specified, rather than program-specified, so that when + the window is mapped again, it will be placed at the same + location, without forcing the user to position it by hand + again (they have already done that once for this window.) */ + x_wm_set_size_hint (f, (long) 0, 1); + result = noErr; + break; + + case kEventWindowShown: + case kEventWindowHidden: + case kEventWindowCollapsed: + case kEventWindowExpanded: + mac_handle_visibility_change (f); + result = noErr; + break; + + case kEventWindowBoundsChanging: + result = CallNextEventHandler (next_handler, event); + if (result != eventNotHandledErr) + break; + + err = GetEventParameter (event, kEventParamAttributes, typeUInt32, + NULL, sizeof (UInt32), NULL, &attributes); + if (err != noErr) + break; + + size_hints = FRAME_SIZE_HINTS (f); + if ((attributes & kWindowBoundsChangeUserResize) + && ((size_hints->flags & (PResizeInc | PBaseSize | PMinSize)) + == (PResizeInc | PBaseSize | PMinSize))) + { + Rect bounds; + int width, height; + + err = GetEventParameter (event, kEventParamCurrentBounds, + typeQDRectangle, NULL, sizeof (Rect), + NULL, &bounds); + if (err != noErr) + break; + + width = bounds.right - bounds.left; + height = bounds.bottom - bounds.top; + + if (width < size_hints->min_width) + width = size_hints->min_width; + else + width = size_hints->base_width + + (int) ((width - size_hints->base_width) + / (float) size_hints->width_inc + .5) + * size_hints->width_inc; + + if (height < size_hints->min_height) + height = size_hints->min_height; + else + height = size_hints->base_height + + (int) ((height - size_hints->base_height) + / (float) size_hints->height_inc + .5) + * size_hints->height_inc; + + bounds.right = bounds.left + width; + bounds.bottom = bounds.top + height; + SetEventParameter (event, kEventParamCurrentBounds, + typeQDRectangle, sizeof (Rect), &bounds); + result = noErr; + } + break; + + case kEventWindowBoundsChanged: + err = GetEventParameter (event, kEventParamAttributes, typeUInt32, + NULL, sizeof (UInt32), NULL, &attributes); + if (err != noErr) + break; + + if (attributes & kWindowBoundsChangeSizeChanged) + { + Rect bounds; + + err = GetEventParameter (event, kEventParamCurrentBounds, + typeQDRectangle, NULL, sizeof (Rect), + NULL, &bounds); + if (err == noErr) + { + int width, height; + + width = bounds.right - bounds.left; + height = bounds.bottom - bounds.top; + mac_handle_size_change (f, width, height); + mac_wakeup_from_rne (); + } + } + + if (attributes & kWindowBoundsChangeOriginChanged) + mac_handle_origin_change (f); + + result = noErr; + break; + + /* -- window action events -- */ + + case kEventWindowClose: + { + struct input_event buf; + + EVENT_INIT (buf); + buf.kind = DELETE_WINDOW_EVENT; + XSETFRAME (buf.frame_or_window, f); + buf.arg = Qnil; + kbd_buffer_store_event (&buf); + } + result = noErr; + break; + + case kEventWindowGetIdealSize: + result = CallNextEventHandler (next_handler, event); + if (result != eventNotHandledErr) + break; + + { + Point ideal_size = mac_get_ideal_size (f); + + err = SetEventParameter (event, kEventParamDimensions, + typeQDPoint, sizeof (Point), &ideal_size); + if (err == noErr) + result = noErr; + } + break; + +#ifdef MAC_OSX + case kEventWindowToolbarSwitchMode: + { + static const EventParamName names[] = {kEventParamDirectObject, + kEventParamWindowMouseLocation, + kEventParamKeyModifiers, + kEventParamMouseButton, + kEventParamClickCount, + kEventParamMouseChord}; + static const EventParamType types[] = {typeWindowRef, + typeQDPoint, + typeUInt32, + typeMouseButton, + typeUInt32, + typeUInt32}; + int num_params = sizeof (names) / sizeof (names[0]); + + err = mac_store_event_ref_as_apple_event (0, 0, + Qwindow, + Qtoolbar_switch_mode, + event, num_params, + names, types); + } + if (err == noErr) + result = noErr; + break; +#endif + +#if USE_MAC_TSM + /* -- window focus events -- */ + + case kEventWindowFocusAcquired: + err = mac_tsm_resume (); + if (err == noErr) + result = noErr; + break; + + case kEventWindowFocusRelinquish: + err = mac_tsm_suspend (); + if (err == noErr) + result = noErr; + break; +#endif + + default: + abort (); + } + + return result; +} +#endif + +/* Handle clicks in zoom box. Calculation of "standard state" based + on code in IM - Window Manager A and code contributed by Ben + Mesander. The standard state of an Emacs window is 80-characters + wide (DEFAULT_NUM_COLS) and as tall as will fit on the screen. */ + +static void +do_zoom_window (WindowRef w, int zoom_in_or_out) +{ + Rect zoom_rect, port_rect; + int width, height; + struct frame *f = mac_window_to_frame (w); +#if TARGET_API_MAC_CARBON + Point ideal_size = mac_get_ideal_size (f); + + GetWindowBounds (w, kWindowContentRgn, &port_rect); + if (IsWindowInStandardState (w, &ideal_size, &zoom_rect) + && port_rect.left == zoom_rect.left + && port_rect.top == zoom_rect.top) + zoom_in_or_out = inZoomIn; + else + zoom_in_or_out = inZoomOut; + +#ifdef MAC_OS8 + mac_clear_area (f, 0, 0, port_rect.right - port_rect.left, + port_rect.bottom - port_rect.top); +#endif + ZoomWindowIdeal (w, zoom_in_or_out, &ideal_size); +#else /* not TARGET_API_MAC_CARBON */ + GrafPtr save_port; + Point top_left; + int w_title_height, rows; + struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); + + GetPort (&save_port); + + SetPortWindowPort (w); + + /* Clear window to avoid flicker. */ + EraseRect (&(w->portRect)); + if (zoom_in_or_out == inZoomOut) + { + SetPt (&top_left, w->portRect.left, w->portRect.top); + LocalToGlobal (&top_left); + + /* calculate height of window's title bar */ + w_title_height = top_left.v - 1 + - (**((WindowPeek) w)->strucRgn).rgnBBox.top + GetMBarHeight (); + + /* get maximum height of window into zoom_rect.bottom - zoom_rect.top */ + zoom_rect = qd.screenBits.bounds; + zoom_rect.top += w_title_height; + InsetRect (&zoom_rect, 8, 4); /* not too tight */ + + zoom_rect.right = zoom_rect.left + + FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS); + + /* Adjust the standard size according to character boundaries. */ + rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, zoom_rect.bottom - zoom_rect.top); + zoom_rect.bottom = + zoom_rect.top + FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows); + + (**((WStateDataHandle) ((WindowPeek) w)->dataHandle)).stdState + = zoom_rect; + } + + ZoomWindow (w, zoom_in_or_out, f == mac_focus_frame (dpyinfo)); + + SetPort (save_port); +#endif /* not TARGET_API_MAC_CARBON */ + +#if !TARGET_API_MAC_CARBON + /* retrieve window size and update application values */ + port_rect = w->portRect; + height = port_rect.bottom - port_rect.top; + width = port_rect.right - port_rect.left; + + mac_handle_size_change (f, width, height); + mac_handle_origin_change (f); +#endif +} + +static OSStatus +install_window_handler (window) + WindowRef window; +{ + OSStatus err = noErr; + +#if TARGET_API_MAC_CARBON + if (err == noErr) + { + static const EventTypeSpec specs[] = + { + /* -- window refresh events -- */ + {kEventClassWindow, kEventWindowUpdate}, + /* -- window state change events -- */ + {kEventClassWindow, kEventWindowShowing}, + {kEventClassWindow, kEventWindowHiding}, + {kEventClassWindow, kEventWindowShown}, + {kEventClassWindow, kEventWindowHidden}, + {kEventClassWindow, kEventWindowCollapsed}, + {kEventClassWindow, kEventWindowExpanded}, + {kEventClassWindow, kEventWindowBoundsChanging}, + {kEventClassWindow, kEventWindowBoundsChanged}, + /* -- window action events -- */ + {kEventClassWindow, kEventWindowClose}, + {kEventClassWindow, kEventWindowGetIdealSize}, +#ifdef MAC_OSX + {kEventClassWindow, kEventWindowToolbarSwitchMode}, +#endif +#if USE_MAC_TSM + /* -- window focus events -- */ + {kEventClassWindow, kEventWindowFocusAcquired}, + {kEventClassWindow, kEventWindowFocusRelinquish}, +#endif + }; + static EventHandlerUPP handle_window_eventUPP = NULL; + + if (handle_window_eventUPP == NULL) + handle_window_eventUPP = NewEventHandlerUPP (mac_handle_window_event); + + err = InstallWindowEventHandler (window, handle_window_eventUPP, + GetEventTypeCount (specs), + specs, NULL, NULL); + } +#endif + + if (err == noErr) + err = install_drag_handler (window); + + return err; +} + +static void +remove_window_handler (window) + WindowRef window; +{ + remove_drag_handler (window); +} + +void +mac_get_window_bounds (f, inner, outer) + struct frame *f; + Rect *inner, *outer; +{ +#if TARGET_API_MAC_CARBON + GetWindowBounds (FRAME_MAC_WINDOW (f), kWindowContentRgn, inner); + GetWindowBounds (FRAME_MAC_WINDOW (f), kWindowStructureRgn, outer); +#else /* not TARGET_API_MAC_CARBON */ + RgnHandle region = NewRgn (); + + GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowContentRgn, region); + *inner = (*region)->rgnBBox; + GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowStructureRgn, region); + *outer = (*region)->rgnBBox; + DisposeRgn (region); +#endif /* not TARGET_API_MAC_CARBON */ +} + +Rect * +mac_get_frame_bounds (f, r) + struct frame *f; + Rect *r; +{ +#if TARGET_API_MAC_CARBON + return GetWindowPortBounds (FRAME_MAC_WINDOW (f), r); +#else + *r = FRAME_MAC_WINDOW (f)->portRect; + + return r; +#endif +} + +void +mac_get_frame_mouse (f, point) + struct frame *f; + Point *point; +{ +#if TARGET_API_MAC_CARBON + GetGlobalMouse (point); + point->h -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); + point->v -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); +#else + SetPortWindowPort (FRAME_MAC_WINDOW (f)); + GetMouse (point); +#endif +} + +void +mac_convert_frame_point_to_global (f, x, y) + struct frame *f; + int *x, *y; +{ + *x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); + *y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); +} + +#if TARGET_API_MAC_CARBON +void +mac_update_proxy_icon (f) + struct frame *f; +{ + OSStatus err; + Lisp_Object file_name = + XBUFFER (XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer)->filename; + Window w = FRAME_MAC_WINDOW (f); + AliasHandle alias = NULL; + + err = GetWindowProxyAlias (w, &alias); + if (err == errWindowDoesNotHaveProxy && !STRINGP (file_name)) + return; + + if (STRINGP (file_name)) + { + AEDesc desc; +#ifdef MAC_OSX + FSRef fref, fref_proxy; +#else + FSSpec fss, fss_proxy; +#endif + Boolean changed; + Lisp_Object encoded_file_name = ENCODE_FILE (file_name); + +#ifdef MAC_OSX + err = AECoercePtr (TYPE_FILE_NAME, SDATA (encoded_file_name), + SBYTES (encoded_file_name), typeFSRef, &desc); +#else + SetPortWindowPort (w); + err = AECoercePtr (TYPE_FILE_NAME, SDATA (encoded_file_name), + SBYTES (encoded_file_name), typeFSS, &desc); +#endif + if (err == noErr) + { +#ifdef MAC_OSX + err = AEGetDescData (&desc, &fref, sizeof (FSRef)); +#else + err = AEGetDescData (&desc, &fss, sizeof (FSSpec)); +#endif + AEDisposeDesc (&desc); + } + if (err == noErr) + { + if (alias) + { + /* (FS)ResolveAlias never sets `changed' to true if + `alias' is minimal. */ +#ifdef MAC_OSX + err = FSResolveAlias (NULL, alias, &fref_proxy, &changed); + if (err == noErr) + err = FSCompareFSRefs (&fref, &fref_proxy); +#else + err = ResolveAlias (NULL, alias, &fss_proxy, &changed); + if (err == noErr) + err = !(fss.vRefNum == fss_proxy.vRefNum + && fss.parID == fss_proxy.parID + && EqualString (fss.name, fss_proxy.name, + false, true)); +#endif + } + if (err != noErr || alias == NULL) + { + if (alias) + DisposeHandle ((Handle) alias); +#ifdef MAC_OSX + err = FSNewAliasMinimal (&fref, &alias); +#else + err = NewAliasMinimal (&fss, &alias); +#endif + changed = true; + } + } + if (err == noErr) + if (changed) + err = SetWindowProxyAlias (w, alias); + } + + if (alias) + DisposeHandle ((Handle) alias); + + if (err != noErr || !STRINGP (file_name)) + RemoveWindowProxy (w); +} +#endif + +/* Mac replacement for XSetWindowBackground. */ + +void +mac_set_frame_window_background (f, color) + struct frame *f; + unsigned long color; +{ + WindowRef w = FRAME_MAC_WINDOW (f); +#if !TARGET_API_MAC_CARBON + AuxWinHandle aw_handle; + CTabHandle ctab_handle; + ColorSpecPtr ct_table; + short ct_size; +#endif + RGBColor bg_color; + + bg_color.red = RED16_FROM_ULONG (color); + bg_color.green = GREEN16_FROM_ULONG (color); + bg_color.blue = BLUE16_FROM_ULONG (color); + +#if TARGET_API_MAC_CARBON + SetWindowContentColor (w, &bg_color); +#else + if (GetAuxWin (w, &aw_handle)) + { + ctab_handle = (*aw_handle)->awCTable; + HandToHand ((Handle *) &ctab_handle); + ct_table = (*ctab_handle)->ctTable; + ct_size = (*ctab_handle)->ctSize; + while (ct_size > -1) + { + if (ct_table->value == 0) + { + ct_table->rgb = bg_color; + CTabChanged (ctab_handle); + SetWinColor (w, (WCTabHandle) ctab_handle); + } + ct_size--; + } + } +#endif +} + +/* Flush display of frame F, or of all frames if F is null. */ + +void +x_flush (f) + struct frame *f; +{ +#if TARGET_API_MAC_CARBON + BLOCK_INPUT; +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (f); +#endif + if (f) + QDFlushPortBuffer (GetWindowPort (FRAME_MAC_WINDOW (f)), NULL); + else + QDFlushPortBuffer (GetQDGlobalsThePort (), NULL); + UNBLOCK_INPUT; +#endif +} + +#if USE_CG_DRAWING +void +mac_flush_display_optional (f) + struct frame *f; +{ + BLOCK_INPUT; + mac_prepare_for_quickdraw (f); + UNBLOCK_INPUT; +} +#endif + +void +mac_update_begin (f) + struct frame *f; +{ +#if TARGET_API_MAC_CARBON + /* During update of a frame, availability of input events is + periodically checked with ReceiveNextEvent if + redisplay-dont-pause is nil. That normally flushes window buffer + changes for every check, and thus screen update looks waving even + if no input is available. So we disable screen updates during + update of a frame. */ + DisableScreenUpdates (); +#endif +} + +void +mac_update_end (f) + struct frame *f; +{ +#if TARGET_API_MAC_CARBON + EnableScreenUpdates (); +#endif +} + +void +mac_frame_up_to_date (f) + struct frame *f; +{ + /* Nothing to do. */ +} + +void +mac_create_frame_window (f, tooltip_p) + struct frame *f; + int tooltip_p; +{ + Rect r; +#if TARGET_API_MAC_CARBON + WindowClass window_class; + WindowAttributes attributes; +#else + short proc_id; + WindowRef behind; + Boolean go_away_flag; +#endif + + if (!tooltip_p) + { + SetRect (&r, f->left_pos, f->top_pos, + f->left_pos + FRAME_PIXEL_WIDTH (f), + f->top_pos + FRAME_PIXEL_HEIGHT (f)); +#if TARGET_API_MAC_CARBON + window_class = kDocumentWindowClass; + attributes = (kWindowStandardDocumentAttributes +#ifdef MAC_OSX + | kWindowToolbarButtonAttribute +#endif + ); +#else + proc_id = zoomDocProc; + behind = (WindowRef) -1; + go_away_flag = true; +#endif + } + else + { + SetRect (&r, 0, 0, 1, 1); +#if TARGET_API_MAC_CARBON + window_class = kHelpWindowClass; + attributes = (kWindowNoUpdatesAttribute + | kWindowNoActivatesAttribute +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 + | kWindowIgnoreClicksAttribute +#endif + ); +#else + proc_id = plainDBox; + behind = NULL; + go_away_flag = false; +#endif + } + +#if TARGET_API_MAC_CARBON + CreateNewWindow (window_class, attributes, &r, &FRAME_MAC_WINDOW (f)); + if (FRAME_MAC_WINDOW (f)) + { + SetWRefCon (FRAME_MAC_WINDOW (f), (long) f->output_data.mac); + if (!tooltip_p) + if (install_window_handler (FRAME_MAC_WINDOW (f)) != noErr) + { + DisposeWindow (FRAME_MAC_WINDOW (f)); + FRAME_MAC_WINDOW (f) = NULL; + } + } +#else /* !TARGET_API_MAC_CARBON */ + FRAME_MAC_WINDOW (f) + = NewCWindow (NULL, &r, "\p", false, proc_id, behind, go_away_flag, + (long) f->output_data.mac); +#endif /* !TARGET_API_MAC_CARBON */ + /* so that update events can find this mac_output struct */ + f->output_data.mac->mFP = f; /* point back to emacs frame */ + +#ifndef MAC_OSX + if (!tooltip_p) + if (FRAME_MAC_WINDOW (f)) + { + ControlRef root_control; + + if (CreateRootControl (FRAME_MAC_WINDOW (f), &root_control) != noErr) + { + DisposeWindow (FRAME_MAC_WINDOW (f)); + FRAME_MAC_WINDOW (f) = NULL; + } + } +#endif +} + +/* Dispose of the Mac window of the frame F. */ + +void +mac_dispose_frame_window (f) + struct frame *f; +{ + WindowRef window = FRAME_MAC_WINDOW (f); + + if (window != tip_window) + remove_window_handler (window); + +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (f); +#endif + DisposeWindow (window); +} + + +/************************************************************************ + View and Drawing + ************************************************************************/ + +#if USE_CG_DRAWING +#define FRAME_CG_CONTEXT(f) ((f)->output_data.mac->cg_context) + +CGContextRef +mac_begin_cg_clip (f, gc) + struct frame *f; + GC gc; +{ + CGContextRef context = FRAME_CG_CONTEXT (f); + + if (!context) + { + QDBeginCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)), &context); + FRAME_CG_CONTEXT (f) = context; + } + + CGContextSaveGState (context); + CGContextTranslateCTM (context, 0, FRAME_PIXEL_HEIGHT (f)); + CGContextScaleCTM (context, 1, -1); + if (gc && gc->n_clip_rects) + CGContextClipToRects (context, gc->clip_rects, gc->n_clip_rects); + + return context; +} + +void +mac_end_cg_clip (f) + struct frame *f; +{ + CGContextRestoreGState (FRAME_CG_CONTEXT (f)); +} + +static void +mac_prepare_for_quickdraw (f) + struct frame *f; +{ + if (f == NULL) + { + Lisp_Object rest, frame; + FOR_EACH_FRAME (rest, frame) + if (FRAME_MAC_P (XFRAME (frame))) + mac_prepare_for_quickdraw (XFRAME (frame)); + } + else + { + CGContextRef context = FRAME_CG_CONTEXT (f); + + if (context) + { + CGContextSynchronize (context); + QDEndCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)), + &FRAME_CG_CONTEXT (f)); + } + } +} +#endif + +static RgnHandle saved_port_clip_region = NULL; + +void +mac_begin_clip (f, gc) + struct frame *f; + GC gc; +{ + static RgnHandle new_region = NULL; + + if (saved_port_clip_region == NULL) + saved_port_clip_region = NewRgn (); + if (new_region == NULL) + new_region = NewRgn (); + +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (f); +#endif + SetPortWindowPort (FRAME_MAC_WINDOW (f)); + + if (gc && gc->n_clip_rects) + { + GetClip (saved_port_clip_region); + SectRgn (saved_port_clip_region, gc->clip_region, new_region); + SetClip (new_region); + } +} + +void +mac_end_clip (f, gc) + struct frame *f; + GC gc; +{ + if (gc && gc->n_clip_rects) + SetClip (saved_port_clip_region); +} + +#if TARGET_API_MAC_CARBON +/* Mac replacement for XCopyArea: used only for scrolling. */ + +void +mac_scroll_area (f, gc, src_x, src_y, width, height, dest_x, dest_y) + struct frame *f; + GC gc; + int src_x, src_y; + unsigned int width, height; + int dest_x, dest_y; +{ + Rect src_r; + RgnHandle dummy = NewRgn (); /* For avoiding update events. */ + + SetRect (&src_r, src_x, src_y, src_x + width, src_y + height); +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (f); +#endif + ScrollWindowRect (FRAME_MAC_WINDOW (f), + &src_r, dest_x - src_x, dest_y - src_y, + kScrollWindowNoOptions, dummy); + DisposeRgn (dummy); +} +#endif + + +/************************************************************************ + Scroll bars + ************************************************************************/ + +extern struct scroll_bar *tracked_scroll_bar; +extern Lisp_Object last_mouse_scroll_bar; +extern Time last_mouse_movement_time; + +static void x_scroll_bar_handle_click P_ ((struct scroll_bar *, + ControlPartCode, + const EventRecord *, + struct input_event *)); +#ifndef USE_TOOLKIT_SCROLL_BARS +static void x_scroll_bar_note_movement P_ ((struct scroll_bar *, int, Time)); +#else /* USE_TOOLKIT_SCROLL_BARS */ +static void x_scroll_bar_handle_press P_ ((struct scroll_bar *, + ControlPartCode, Point, + struct input_event *)); +static void x_scroll_bar_handle_release P_ ((struct scroll_bar *, + struct input_event *)); +static void x_scroll_bar_handle_drag P_ ((WindowRef, struct scroll_bar *, + Point, struct input_event *)); +static pascal void scroll_bar_timer_callback P_ ((EventLoopTimerRef, void *)); +static OSStatus install_scroll_bar_timer P_ ((void)); +static OSStatus set_scroll_bar_timer P_ ((EventTimerInterval)); +static int control_part_code_to_scroll_bar_part P_ ((ControlPartCode)); +static void construct_scroll_bar_click P_ ((struct scroll_bar *, int, + struct input_event *)); +static OSStatus get_control_part_bounds P_ ((ControlRef, ControlPartCode, + Rect *)); +static void update_scroll_bar_track_info P_ ((struct scroll_bar *)); + +/* Last scroll bar part sent in x_scroll_bar_handle_*. */ + +static int last_scroll_bar_part; + +static EventLoopTimerRef scroll_bar_timer; + +static int scroll_bar_timer_event_posted_p; + +#define SCROLL_BAR_FIRST_DELAY 0.5 +#define SCROLL_BAR_CONTINUOUS_DELAY (1.0 / 15) + +static pascal void +scroll_bar_timer_callback (timer, data) + EventLoopTimerRef timer; + void *data; +{ + OSStatus err; + + err = mac_post_mouse_moved_event (); + if (err == noErr) + scroll_bar_timer_event_posted_p = 1; +} + +static OSStatus +install_scroll_bar_timer () +{ + static EventLoopTimerUPP scroll_bar_timer_callbackUPP = NULL; + + if (scroll_bar_timer_callbackUPP == NULL) + scroll_bar_timer_callbackUPP = + NewEventLoopTimerUPP (scroll_bar_timer_callback); + + if (scroll_bar_timer == NULL) + /* Mac OS X and CarbonLib 1.5 and later allow us to specify + kEventDurationForever as delays. */ + return + InstallEventLoopTimer (GetCurrentEventLoop (), + kEventDurationForever, kEventDurationForever, + scroll_bar_timer_callbackUPP, NULL, + &scroll_bar_timer); +} + +static OSStatus +set_scroll_bar_timer (delay) + EventTimerInterval delay; +{ + if (scroll_bar_timer == NULL) + install_scroll_bar_timer (); + + scroll_bar_timer_event_posted_p = 0; + + return SetEventLoopTimerNextFireTime (scroll_bar_timer, delay); +} + +static int +control_part_code_to_scroll_bar_part (part_code) + ControlPartCode part_code; +{ + switch (part_code) + { + case kControlUpButtonPart: return scroll_bar_up_arrow; + case kControlDownButtonPart: return scroll_bar_down_arrow; + case kControlPageUpPart: return scroll_bar_above_handle; + case kControlPageDownPart: return scroll_bar_below_handle; + case kControlIndicatorPart: return scroll_bar_handle; + } + + return -1; +} + +static void +construct_scroll_bar_click (bar, part, bufp) + struct scroll_bar *bar; + int part; + struct input_event *bufp; +{ + bufp->kind = SCROLL_BAR_CLICK_EVENT; + bufp->frame_or_window = bar->window; + bufp->arg = Qnil; + bufp->part = part; + bufp->code = 0; + XSETINT (bufp->x, 0); + XSETINT (bufp->y, 0); + bufp->modifiers = 0; +} + +static OSStatus +get_control_part_bounds (ch, part_code, rect) + ControlRef ch; + ControlPartCode part_code; + Rect *rect; +{ + RgnHandle region = NewRgn (); + OSStatus err; + + err = GetControlRegion (ch, part_code, region); + if (err == noErr) + GetRegionBounds (region, rect); + DisposeRgn (region); + + return err; +} + +static void +x_scroll_bar_handle_press (bar, part_code, mouse_pos, bufp) + struct scroll_bar *bar; + ControlPartCode part_code; + Point mouse_pos; + struct input_event *bufp; +{ + int part = control_part_code_to_scroll_bar_part (part_code); + + if (part < 0) + return; + + if (part != scroll_bar_handle) + { + construct_scroll_bar_click (bar, part, bufp); + HiliteControl (SCROLL_BAR_CONTROL_REF (bar), part_code); + set_scroll_bar_timer (SCROLL_BAR_FIRST_DELAY); + bar->dragging = Qnil; + } + else + { + Rect r; + + get_control_part_bounds (SCROLL_BAR_CONTROL_REF (bar), + kControlIndicatorPart, &r); + XSETINT (bar->dragging, - (mouse_pos.v - r.top) - 1); + } + + last_scroll_bar_part = part; + tracked_scroll_bar = bar; +} + +static void +x_scroll_bar_handle_release (bar, bufp) + struct scroll_bar *bar; + struct input_event *bufp; +{ + if (last_scroll_bar_part != scroll_bar_handle + || (INTEGERP (bar->dragging) && XINT (bar->dragging) >= 0)) + construct_scroll_bar_click (bar, scroll_bar_end_scroll, bufp); + + HiliteControl (SCROLL_BAR_CONTROL_REF (bar), 0); + set_scroll_bar_timer (kEventDurationForever); + + last_scroll_bar_part = -1; + bar->dragging = Qnil; + tracked_scroll_bar = NULL; +} + +static void +x_scroll_bar_handle_drag (win, bar, mouse_pos, bufp) + WindowRef win; + struct scroll_bar *bar; + Point mouse_pos; + struct input_event *bufp; +{ + ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); + + if (last_scroll_bar_part == scroll_bar_handle) + { + int top, top_range; + Rect r; + + get_control_part_bounds (SCROLL_BAR_CONTROL_REF (bar), + kControlIndicatorPart, &r); + + if (INTEGERP (bar->dragging) && XINT (bar->dragging) < 0) + XSETINT (bar->dragging, - (XINT (bar->dragging) + 1)); + + top = mouse_pos.v - XINT (bar->dragging) - XINT (bar->track_top); + top_range = XINT (bar->track_height) - XINT (bar->min_handle); + + if (top < 0) + top = 0; + if (top > top_range) + top = top_range; + + construct_scroll_bar_click (bar, scroll_bar_handle, bufp); + XSETINT (bufp->x, top); + XSETINT (bufp->y, top_range); + } + else + { + ControlPartCode part_code; + int unhilite_p = 0, part; + + if (ch != FindControlUnderMouse (mouse_pos, win, &part_code)) + unhilite_p = 1; + else + { + part = control_part_code_to_scroll_bar_part (part_code); + + switch (last_scroll_bar_part) + { + case scroll_bar_above_handle: + case scroll_bar_below_handle: + if (part != scroll_bar_above_handle + && part != scroll_bar_below_handle) + unhilite_p = 1; + break; + + case scroll_bar_up_arrow: + case scroll_bar_down_arrow: + if (part != scroll_bar_up_arrow + && part != scroll_bar_down_arrow) + unhilite_p = 1; + break; + } + } + + if (unhilite_p) + HiliteControl (SCROLL_BAR_CONTROL_REF (bar), 0); + else if (part != last_scroll_bar_part + || scroll_bar_timer_event_posted_p) + { + construct_scroll_bar_click (bar, part, bufp); + last_scroll_bar_part = part; + HiliteControl (SCROLL_BAR_CONTROL_REF (bar), part_code); + set_scroll_bar_timer (SCROLL_BAR_CONTINUOUS_DELAY); + } + } +} + +/* Update BAR->track_top, BAR->track_height, and BAR->min_handle for + the scroll bar BAR. This function should be called when the bounds + of the scroll bar is changed. */ + +static void +update_scroll_bar_track_info (bar) + struct scroll_bar *bar; +{ + ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); + Rect r0, r1; + + GetControlBounds (ch, &r0); + + if (r0.right - r0.left >= r0.bottom - r0.top +#ifdef MAC_OSX + || r0.right - r0.left < MAC_AQUA_SMALL_VERTICAL_SCROLL_BAR_WIDTH +#endif + ) + { + XSETINT (bar->track_top, 0); + XSETINT (bar->track_height, 0); + XSETINT (bar->min_handle, 0); + } + else + { + BLOCK_INPUT; + + SetControl32BitMinimum (ch, 0); + SetControl32BitMaximum (ch, 1 << 30); + SetControlViewSize (ch, 1); + + /* Move the scroll bar thumb to the top. */ + SetControl32BitValue (ch, 0); + get_control_part_bounds (ch, kControlIndicatorPart, &r0); + + /* Move the scroll bar thumb to the bottom. */ + SetControl32BitValue (ch, 1 << 30); + get_control_part_bounds (ch, kControlIndicatorPart, &r1); + + UnionRect (&r0, &r1, &r0); + XSETINT (bar->track_top, r0.top); + XSETINT (bar->track_height, r0.bottom - r0.top); + XSETINT (bar->min_handle, r1.bottom - r1.top); + + /* Don't show the scroll bar if its height is not enough to + display the scroll bar thumb. */ + if (r0.bottom - r0.top > 0) + ShowControl (ch); + + UNBLOCK_INPUT; + } +} + +/* Set the thumb size and position of scroll bar BAR. We are currently + displaying PORTION out of a whole WHOLE, and our position POSITION. */ + +void +x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole) + struct scroll_bar *bar; + int portion, position, whole; +{ + ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); + int value, viewsize, maximum; + + if (XINT (bar->track_height) == 0) + return; + + if (whole <= portion) + value = 0, viewsize = 1, maximum = 0; + else + { + float scale; + + maximum = XINT (bar->track_height) - XINT (bar->min_handle); + scale = (float) maximum / (whole - portion); + value = position * scale + 0.5f; + viewsize = (int) (portion * scale + 0.5f) + XINT (bar->min_handle); + } + + BLOCK_INPUT; + + if (GetControlViewSize (ch) != viewsize + || GetControl32BitValue (ch) != value + || GetControl32BitMaximum (ch) != maximum) + { + /* Temporarily hide the scroll bar to avoid multiple redraws. */ + SetControlVisibility (ch, false, false); + + SetControl32BitMaximum (ch, maximum); + SetControl32BitValue (ch, value); + SetControlViewSize (ch, viewsize); + + SetControlVisibility (ch, true, true); + } + + UNBLOCK_INPUT; +} + +#endif /* USE_TOOLKIT_SCROLL_BARS */ + +/* Create a scroll bar control for BAR. BOUNDS and VISIBLE specifies + the initial geometry and visibility, respectively. The created + control is stored in some members of BAR. */ + +void +mac_create_scroll_bar (bar, bounds, visible) + struct scroll_bar *bar; + const Rect *bounds; + Boolean visible; +{ + struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window))); + ControlRef ch; + +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (f); +#endif + ch = NewControl (FRAME_MAC_WINDOW (f), bounds, "\p", visible, 0, 0, 0, +#if TARGET_API_MAC_CARBON + kControlScrollBarProc, +#else + scrollBarProc, +#endif + (SInt32) bar); + SET_SCROLL_BAR_CONTROL_REF (bar, ch); + + XSETINT (bar->start, 0); + XSETINT (bar->end, 0); + bar->dragging = Qnil; + +#ifdef USE_TOOLKIT_SCROLL_BARS + update_scroll_bar_track_info (bar); +#endif +} + +/* Dispose of the scroll bar control stored in some members of + BAR. */ + +void +mac_dispose_scroll_bar (bar) + struct scroll_bar *bar; +{ +#if USE_CG_DRAWING + struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window))); + + mac_prepare_for_quickdraw (f); +#endif + DisposeControl (SCROLL_BAR_CONTROL_REF (bar)); +} + +/* Set bounds of the scroll bar BAR to BOUNDS. */ + +void +mac_set_scroll_bar_bounds (bar, bounds) + struct scroll_bar *bar; + const Rect *bounds; +{ + ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); + SInt16 width, height; +#if USE_CG_DRAWING + struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window))); + + mac_prepare_for_quickdraw (f); +#endif + + width = bounds->right - bounds->left; + height = bounds->bottom - bounds->top; + HideControl (ch); + MoveControl (ch, bounds->left, bounds->top); + SizeControl (ch, width, height); +#ifdef USE_TOOLKIT_SCROLL_BARS + update_scroll_bar_track_info (bar); +#else + if (width < height) + ShowControl (ch); +#endif +} + +/* Draw the scroll bar BAR. */ + +void +mac_redraw_scroll_bar (bar) + struct scroll_bar *bar; +{ +#if USE_CG_DRAWING + struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window))); + + mac_prepare_for_quickdraw (f); +#endif + Draw1Control (SCROLL_BAR_CONTROL_REF (bar)); +} + +/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind + is set to something other than NO_EVENT, it is enqueued. + + This may be called from a signal handler, so we have to ignore GC + mark bits. */ + +static void +x_scroll_bar_handle_click (bar, part_code, er, bufp) + struct scroll_bar *bar; + ControlPartCode part_code; + const EventRecord *er; + struct input_event *bufp; +{ + int win_y, top_range; + + if (! GC_WINDOWP (bar->window)) + abort (); + + bufp->kind = SCROLL_BAR_CLICK_EVENT; + bufp->frame_or_window = bar->window; + bufp->arg = Qnil; + + bar->dragging = Qnil; + + switch (part_code) + { + case kControlUpButtonPart: + bufp->part = scroll_bar_up_arrow; + break; + case kControlDownButtonPart: + bufp->part = scroll_bar_down_arrow; + break; + case kControlPageUpPart: + bufp->part = scroll_bar_above_handle; + break; + case kControlPageDownPart: + bufp->part = scroll_bar_below_handle; + break; +#if TARGET_API_MAC_CARBON + default: +#else + case kControlIndicatorPart: +#endif + if (er->what == mouseDown) + bar->dragging = make_number (0); + XSETVECTOR (last_mouse_scroll_bar, bar); + bufp->part = scroll_bar_handle; + break; + } + + win_y = XINT (bufp->y) - XINT (bar->top); + top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (0/*dummy*/, XINT (bar->height)); + + win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER; + + win_y -= 24; + + if (! NILP (bar->dragging)) + win_y -= XINT (bar->dragging); + + if (win_y < 0) + win_y = 0; + if (win_y > top_range) + win_y = top_range; + + XSETINT (bufp->x, win_y); + XSETINT (bufp->y, top_range); +} + +/* Return information to the user about the current position of the mouse + on the scroll bar. */ + +void +x_scroll_bar_report_motion (fp, bar_window, part, x, y, time) + FRAME_PTR *fp; + Lisp_Object *bar_window; + enum scroll_bar_part *part; + Lisp_Object *x, *y; + unsigned long *time; +{ + struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar); + ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); +#if TARGET_API_MAC_CARBON + WindowRef wp = GetControlOwner (ch); +#else + WindowRef wp = (*ch)->contrlOwner; +#endif + Point mouse_pos; + struct frame *f = mac_window_to_frame (wp); + int win_y, top_range; + +#if TARGET_API_MAC_CARBON + GetGlobalMouse (&mouse_pos); + mouse_pos.h -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); + mouse_pos.v -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); +#else + SetPortWindowPort (wp); + GetMouse (&mouse_pos); +#endif + + win_y = mouse_pos.v - XINT (bar->top); + top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)); + + win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER; + + win_y -= 24; + + if (! NILP (bar->dragging)) + win_y -= XINT (bar->dragging); + + if (win_y < 0) + win_y = 0; + if (win_y > top_range) + win_y = top_range; + + *fp = f; + *bar_window = bar->window; + + if (! NILP (bar->dragging)) + *part = scroll_bar_handle; + else if (win_y < XINT (bar->start)) + *part = scroll_bar_above_handle; + else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE) + *part = scroll_bar_handle; + else + *part = scroll_bar_below_handle; + + XSETINT (*x, win_y); + XSETINT (*y, top_range); + + f->mouse_moved = 0; + last_mouse_scroll_bar = Qnil; + + *time = last_mouse_movement_time; +} + +#ifndef USE_TOOLKIT_SCROLL_BARS +/* Draw BAR's handle in the proper position. + + If the handle is already drawn from START to END, don't bother + redrawing it, unless REBUILD is non-zero; in that case, always + redraw it. (REBUILD is handy for drawing the handle after expose + events.) + + Normally, we want to constrain the start and end of the handle to + fit inside its rectangle, but if the user is dragging the scroll + bar handle, we want to let them drag it down all the way, so that + the bar's top is as far down as it goes; otherwise, there's no way + to move to the very end of the buffer. */ + +void +x_scroll_bar_set_handle (bar, start, end, rebuild) + struct scroll_bar *bar; + int start, end; + int rebuild; +{ + int dragging = ! NILP (bar->dragging); + ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); + FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window))); + int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)); + int length = end - start; + + /* If the display is already accurate, do nothing. */ + if (! rebuild + && start == XINT (bar->start) + && end == XINT (bar->end)) + return; + + BLOCK_INPUT; + + /* Make sure the values are reasonable, and try to preserve the + distance between start and end. */ + if (start < 0) + start = 0; + else if (start > top_range) + start = top_range; + end = start + length; + + if (end < start) + end = start; + else if (end > top_range && ! dragging) + end = top_range; + + /* Store the adjusted setting in the scroll bar. */ + XSETINT (bar->start, start); + XSETINT (bar->end, end); + + /* Clip the end position, just for display. */ + if (end > top_range) + end = top_range; + + /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels below + top positions, to make sure the handle is always at least that + many pixels tall. */ + end += VERTICAL_SCROLL_BAR_MIN_HANDLE; + + SetControlMinimum (ch, 0); + /* Don't inadvertently activate deactivated scroll bars */ + if (GetControlMaximum (ch) != -1) + SetControlMaximum (ch, top_range + VERTICAL_SCROLL_BAR_MIN_HANDLE + - (end - start)); + SetControlValue (ch, start); +#if TARGET_API_MAC_CARBON + SetControlViewSize (ch, end - start); +#endif + + UNBLOCK_INPUT; +} + +/* Handle some mouse motion while someone is dragging the scroll bar. + + This may be called from a signal handler, so we have to ignore GC + mark bits. */ + +static void +x_scroll_bar_note_movement (bar, y_pos, t) + struct scroll_bar *bar; + int y_pos; + Time t; +{ + FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame); + + last_mouse_movement_time = t; + + f->mouse_moved = 1; + XSETVECTOR (last_mouse_scroll_bar, bar); + + /* If we're dragging the bar, display it. */ + if (! GC_NILP (bar->dragging)) + { + /* Where should the handle be now? */ + int new_start = y_pos - 24; + + if (new_start != XINT (bar->start)) + { + int new_end = new_start + (XINT (bar->end) - XINT (bar->start)); + + x_scroll_bar_set_handle (bar, new_start, new_end, 0); + } + } +} +#endif /* !USE_TOOLKIT_SCROLL_BARS */ + + +/*********************************************************************** + Tool-bars + ***********************************************************************/ + +#if USE_MAC_TOOLBAR +/* In identifiers such as function/variable names, Emacs tool bar is + referred to as `tool_bar', and Carbon HIToolbar as `toolbar'. */ + +#define TOOLBAR_IDENTIFIER (CFSTR ("org.gnu.Emacs.toolbar")) +#define TOOLBAR_ICON_ITEM_IDENTIFIER (CFSTR ("org.gnu.Emacs.toolbar.icon")) + +#define TOOLBAR_ITEM_COMMAND_ID_OFFSET 'Tb\0\0' +#define TOOLBAR_ITEM_COMMAND_ID_P(id) \ + (((id) & ~0xffff) == TOOLBAR_ITEM_COMMAND_ID_OFFSET) +#define TOOLBAR_ITEM_COMMAND_ID_VALUE(id) \ + ((id) - TOOLBAR_ITEM_COMMAND_ID_OFFSET) +#define TOOLBAR_ITEM_MAKE_COMMAND_ID(value) \ + ((value) + TOOLBAR_ITEM_COMMAND_ID_OFFSET) + +static OSStatus mac_handle_toolbar_command_event P_ ((EventHandlerCallRef, + EventRef, void *)); + +extern Rect last_mouse_glyph; + +extern void mac_move_window_with_gravity P_ ((struct frame *, int, + short, short)); +extern void mac_get_window_origin_with_gravity P_ ((struct frame *, int, + short *, short *)); +extern CGImageRef mac_image_spec_to_cg_image P_ ((struct frame *, + Lisp_Object)); + +static OSStatus +mac_handle_toolbar_event (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + OSStatus result = eventNotHandledErr; + + switch (GetEventKind (event)) + { + case kEventToolbarGetDefaultIdentifiers: + result = noErr; + break; + + case kEventToolbarGetAllowedIdentifiers: + { + CFMutableArrayRef array; + + GetEventParameter (event, kEventParamMutableArray, + typeCFMutableArrayRef, NULL, + sizeof (CFMutableArrayRef), NULL, &array); + CFArrayAppendValue (array, TOOLBAR_ICON_ITEM_IDENTIFIER); + result = noErr; + } + break; + + case kEventToolbarCreateItemWithIdentifier: + { + CFStringRef identifier; + HIToolbarItemRef item = NULL; + + GetEventParameter (event, kEventParamToolbarItemIdentifier, + typeCFStringRef, NULL, + sizeof (CFStringRef), NULL, &identifier); + + if (CFStringCompare (identifier, TOOLBAR_ICON_ITEM_IDENTIFIER, 0) + == kCFCompareEqualTo) + HIToolbarItemCreate (identifier, + kHIToolbarItemAllowDuplicates + | kHIToolbarItemCantBeRemoved, &item); + + if (item) + { + SetEventParameter (event, kEventParamToolbarItem, + typeHIToolbarItemRef, + sizeof (HIToolbarItemRef), &item); + result = noErr; + } + } + break; + + default: + abort (); + } + + return result; +} + +/* Create a tool bar for frame F. */ + +static OSStatus +mac_create_frame_tool_bar (f) + FRAME_PTR f; +{ + OSStatus err; + HIToolbarRef toolbar; + + err = HIToolbarCreate (TOOLBAR_IDENTIFIER, kHIToolbarNoAttributes, + &toolbar); + if (err == noErr) + { + static const EventTypeSpec specs[] = + {{kEventClassToolbar, kEventToolbarGetDefaultIdentifiers}, + {kEventClassToolbar, kEventToolbarGetAllowedIdentifiers}, + {kEventClassToolbar, kEventToolbarCreateItemWithIdentifier}}; + + err = InstallEventHandler (HIObjectGetEventTarget (toolbar), + mac_handle_toolbar_event, + GetEventTypeCount (specs), specs, + f, NULL); + } + + if (err == noErr) + err = HIToolbarSetDisplayMode (toolbar, kHIToolbarDisplayModeIconOnly); + if (err == noErr) + { + static const EventTypeSpec specs[] = + {{kEventClassCommand, kEventCommandProcess}}; + + err = InstallWindowEventHandler (FRAME_MAC_WINDOW (f), + mac_handle_toolbar_command_event, + GetEventTypeCount (specs), + specs, f, NULL); + } + if (err == noErr) + err = SetWindowToolbar (FRAME_MAC_WINDOW (f), toolbar); + + if (toolbar) + CFRelease (toolbar); + + return err; +} + +/* Update the tool bar for frame F. Add new buttons and remove old. */ + +void +update_frame_tool_bar (f) + FRAME_PTR f; +{ + HIToolbarRef toolbar = NULL; + short left, top; + CFArrayRef old_items = NULL; + CFIndex old_count; + int i, pos, win_gravity = f->output_data.mac->toolbar_win_gravity; + struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); + + BLOCK_INPUT; + + GetWindowToolbar (FRAME_MAC_WINDOW (f), &toolbar); + if (toolbar == NULL) + { + mac_create_frame_tool_bar (f); + GetWindowToolbar (FRAME_MAC_WINDOW (f), &toolbar); + if (toolbar == NULL) + goto out; + if (win_gravity >= NorthWestGravity && win_gravity <= SouthEastGravity) + mac_get_window_origin_with_gravity (f, win_gravity, &left, &top); + } + + HIToolbarCopyItems (toolbar, &old_items); + if (old_items == NULL) + goto out; + + old_count = CFArrayGetCount (old_items); + pos = 0; + for (i = 0; i < f->n_tool_bar_items; ++i) + { +#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX)) + + int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P)); + int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)); + int idx; + Lisp_Object image; + CGImageRef cg_image; + CFStringRef label; + HIToolbarItemRef item; + + /* If image is a vector, choose the image according to the + button state. */ + image = PROP (TOOL_BAR_ITEM_IMAGES); + if (VECTORP (image)) + { + if (enabled_p) + idx = (selected_p + ? TOOL_BAR_IMAGE_ENABLED_SELECTED + : TOOL_BAR_IMAGE_ENABLED_DESELECTED); + else + idx = (selected_p + ? TOOL_BAR_IMAGE_DISABLED_SELECTED + : TOOL_BAR_IMAGE_DISABLED_DESELECTED); + + xassert (ASIZE (image) >= idx); + image = AREF (image, idx); + } + else + idx = -1; + + cg_image = mac_image_spec_to_cg_image (f, image); + /* Ignore invalid image specifications. */ + if (cg_image == NULL) + continue; + + label = cfstring_create_with_string (PROP (TOOL_BAR_ITEM_CAPTION)); + if (label == NULL) + label = CFSTR (""); + + if (pos < old_count) + { + CGImageRef old_cg_image = NULL; + CFStringRef old_label = NULL; + Boolean old_enabled_p; + + item = (HIToolbarItemRef) CFArrayGetValueAtIndex (old_items, pos); + + HIToolbarItemCopyImage (item, &old_cg_image); + if (cg_image != old_cg_image) + HIToolbarItemSetImage (item, cg_image); + CGImageRelease (old_cg_image); + + HIToolbarItemCopyLabel (item, &old_label); + if (CFStringCompare (label, old_label, 0) != kCFCompareEqualTo) + HIToolbarItemSetLabel (item, label); + CFRelease (old_label); + + old_enabled_p = HIToolbarItemIsEnabled (item); + if ((enabled_p || idx >= 0) != old_enabled_p) + HIToolbarItemSetEnabled (item, (enabled_p || idx >= 0)); + } + else + { + item = NULL; + HIToolbarCreateItemWithIdentifier (toolbar, + TOOLBAR_ICON_ITEM_IDENTIFIER, + NULL, &item); + if (item) + { + HIToolbarItemSetImage (item, cg_image); + HIToolbarItemSetLabel (item, label); + HIToolbarItemSetEnabled (item, (enabled_p || idx >= 0)); + HIToolbarAppendItem (toolbar, item); + CFRelease (item); + } + } + + CFRelease (label); + if (item) + { + HIToolbarItemSetCommandID (item, TOOLBAR_ITEM_MAKE_COMMAND_ID (i)); + pos++; + } + } + + CFRelease (old_items); + + while (pos < old_count) + HIToolbarRemoveItemAtIndex (toolbar, --old_count); + + ShowHideWindowToolbar (FRAME_MAC_WINDOW (f), true, + !win_gravity && f == mac_focus_frame (dpyinfo)); + /* Mac OS X 10.3 does not issue kEventWindowBoundsChanged events on + toolbar visibility change. */ + mac_handle_origin_change (f); + if (win_gravity >= NorthWestGravity && win_gravity <= SouthEastGravity) + { + mac_move_window_with_gravity (f, win_gravity, left, top); + /* If the title bar is completely outside the screen, adjust the + position. */ + ConstrainWindowToScreen (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn, + kWindowConstrainMoveRegardlessOfFit + | kWindowConstrainAllowPartial, NULL, NULL); + f->output_data.mac->toolbar_win_gravity = 0; + } + + out: + UNBLOCK_INPUT; +} + +/* Hide the tool bar on frame F. Unlike the counterpart on GTK+, it + doesn't deallocate the resources. */ + +void +free_frame_tool_bar (f) + FRAME_PTR f; +{ + if (IsWindowToolbarVisible (FRAME_MAC_WINDOW (f))) + { + struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); + + BLOCK_INPUT; + ShowHideWindowToolbar (FRAME_MAC_WINDOW (f), false, + (NILP (find_symbol_value + (intern ("frame-notice-user-settings"))) + && f == mac_focus_frame (dpyinfo))); + /* Mac OS X 10.3 does not issue kEventWindowBoundsChanged events + on toolbar visibility change. */ + mac_handle_origin_change (f); + UNBLOCK_INPUT; + } +} + +/* Report a mouse movement over toolbar to the mainstream Emacs + code. */ + +static void +mac_tool_bar_note_mouse_movement (f, event) + struct frame *f; + EventRef event; +{ + OSStatus err; + struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); + int mouse_down_p; + WindowRef window; + WindowPartCode part_code; + HIViewRef item_view; + UInt32 command_id; + + mouse_down_p = (dpyinfo->grabbed + && f == last_mouse_frame + && FRAME_LIVE_P (f)); + if (mouse_down_p) + return; + + err = GetEventParameter (event, kEventParamWindowRef, typeWindowRef, NULL, + sizeof (WindowRef), NULL, &window); + if (err != noErr || window != FRAME_MAC_WINDOW (f)) + return; + + err = GetEventParameter (event, kEventParamWindowPartCode, + typeWindowPartCode, NULL, + sizeof (WindowPartCode), NULL, &part_code); + if (err != noErr || part_code != inStructure) + return; + + err = HIViewGetViewForMouseEvent (HIViewGetRoot (window), event, &item_view); + /* This doesn't work on Mac OS X 10.2. On Mac OS X 10.3 and 10.4, a + toolbar item view seems to have the same command ID with that of + the toolbar item. */ + if (err == noErr) + err = GetControlCommandID (item_view, &command_id); + if (err == noErr && TOOLBAR_ITEM_COMMAND_ID_P (command_id)) + { + int i = TOOLBAR_ITEM_COMMAND_ID_VALUE (command_id); + + if (i < f->n_tool_bar_items) + { + HIRect bounds; + HIViewRef content_view; + + err = HIViewGetBounds (item_view, &bounds); + if (err == noErr) + err = HIViewFindByID (HIViewGetRoot (window), + kHIViewWindowContentID, &content_view); + if (err == noErr) + err = HIViewConvertRect (&bounds, item_view, content_view); + if (err == noErr) + SetRect (&last_mouse_glyph, + CGRectGetMinX (bounds), CGRectGetMinY (bounds), + CGRectGetMaxX (bounds), CGRectGetMaxY (bounds)); + + help_echo_object = help_echo_window = Qnil; + help_echo_pos = -1; + help_echo_string = PROP (TOOL_BAR_ITEM_HELP); + if (NILP (help_echo_string)) + help_echo_string = PROP (TOOL_BAR_ITEM_CAPTION); + } + } +} + +static OSStatus +mac_handle_toolbar_command_event (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + OSStatus err, result = eventNotHandledErr; + struct frame *f = (struct frame *) data; + HICommand command; + + err = GetEventParameter (event, kEventParamDirectObject, + typeHICommand, NULL, + sizeof (HICommand), NULL, &command); + if (err != noErr) + return result; + + switch (GetEventKind (event)) + { + case kEventCommandProcess: + if (!TOOLBAR_ITEM_COMMAND_ID_P (command.commandID)) + result = CallNextEventHandler (next_handler, event); + else + { + int i = TOOLBAR_ITEM_COMMAND_ID_VALUE (command.commandID); + + if (i < f->n_tool_bar_items + && !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P))) + { + Lisp_Object frame; + struct input_event buf; + + EVENT_INIT (buf); + + XSETFRAME (frame, f); + buf.kind = TOOL_BAR_EVENT; + buf.frame_or_window = frame; + buf.arg = frame; + kbd_buffer_store_event (&buf); + + buf.kind = TOOL_BAR_EVENT; + buf.frame_or_window = frame; + buf.arg = PROP (TOOL_BAR_ITEM_KEY); + buf.modifiers = mac_event_to_emacs_modifiers (event); + kbd_buffer_store_event (&buf); + + result = noErr; + } + } + break; + + default: + abort (); + } +#undef PROP + + return result; +} +#endif /* USE_MAC_TOOLBAR */ + + +/*********************************************************************** + Font Panel + ***********************************************************************/ + +#if USE_MAC_FONT_PANEL +/* Whether Font Panel has been shown before. The first call to font + panel functions (FPIsFontPanelVisible, SetFontInfoForSelection) is + slow. This variable is used for deferring such a call as much as + possible. */ +static int font_panel_shown_p = 0; + +extern Lisp_Object Qpanel_closed, Qselection; +extern Lisp_Object Qfont; + +/* Whether the font panel is currently visible. */ + +int +mac_font_panel_visible_p () +{ + return font_panel_shown_p && FPIsFontPanelVisible (); +} + +static pascal OSStatus +mac_handle_font_event (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + OSStatus result, err; + Lisp_Object id_key; + int num_params; + const EventParamName *names; + const EventParamType *types; + static const EventParamName names_sel[] = {kEventParamATSUFontID, + kEventParamATSUFontSize, + kEventParamFMFontFamily, + kEventParamFMFontStyle, + kEventParamFMFontSize, + kEventParamFontColor}; + static const EventParamType types_sel[] = {typeATSUFontID, + typeATSUSize, + typeFMFontFamily, + typeFMFontStyle, + typeFMFontSize, + typeFontColor}; + + result = CallNextEventHandler (next_handler, event); + if (result != eventNotHandledErr) + return result; + + switch (GetEventKind (event)) + { + case kEventFontPanelClosed: + id_key = Qpanel_closed; + num_params = 0; + names = NULL; + types = NULL; + break; + + case kEventFontSelection: + id_key = Qselection; + num_params = sizeof (names_sel) / sizeof (names_sel[0]); + names = names_sel; + types = types_sel; + break; + } + + err = mac_store_event_ref_as_apple_event (0, 0, Qfont, id_key, + event, num_params, + names, types); + if (err == noErr) + result = noErr; + + return result; +} + +/* Toggle visiblity of the font panel. */ + +OSStatus +mac_show_hide_font_panel () +{ + if (!font_panel_shown_p) + { + OSStatus err; + + static const EventTypeSpec specs[] = + {{kEventClassFont, kEventFontPanelClosed}, + {kEventClassFont, kEventFontSelection}}; + + err = InstallApplicationEventHandler (mac_handle_font_event, + GetEventTypeCount (specs), + specs, NULL, NULL); + if (err != noErr) + return err; + + font_panel_shown_p = 1; + } + + return FPShowHideFontPanel (); +} + +/* Set the font selected in the font panel to the one corresponding to + the face FACE_ID and the charcacter C in the frame F. */ + +OSStatus +mac_set_font_info_for_selection (f, face_id, c) + struct frame *f; + int face_id, c; +{ + OSStatus err; + EventTargetRef target = NULL; + XFontStruct *font = NULL; + + if (!mac_font_panel_visible_p ()) + return noErr; + + if (f) + { + target = GetWindowEventTarget (FRAME_MAC_WINDOW (f)); + + if (FRAME_FACE_CACHE (f) && CHAR_VALID_P (c, 0)) + { + struct face *face; + + face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c); + face = FACE_FROM_ID (f, face_id); + font = face->font; + } + } + + if (font == NULL) + err = SetFontInfoForSelection (kFontSelectionATSUIType, 0, NULL, target); + else + { + if (font->mac_fontnum != -1) + { + FontSelectionQDStyle qd_style; + + qd_style.version = kFontSelectionQDStyleVersionZero; + qd_style.instance.fontFamily = font->mac_fontnum; + qd_style.instance.fontStyle = font->mac_fontface; + qd_style.size = font->mac_fontsize; + qd_style.hasColor = false; + + err = SetFontInfoForSelection (kFontSelectionQDType, + 1, &qd_style, target); + } + else + err = SetFontInfoForSelection (kFontSelectionATSUIType, + 1, &font->mac_style, target); + } + + return err; +} +#endif /* USE_MAC_FONT_PANEL */ + + +/************************************************************************ + Event Handling + ************************************************************************/ + +/* Non-zero means that a HELP_EVENT has been generated since Emacs + start. */ + +static int any_help_event_p; + +/* Last window where we saw the mouse. Used by mouse-autoselect-window. */ +static Lisp_Object last_window; + +static Point saved_menu_event_location; + +extern struct frame *pending_autoraise_frame; + +extern FRAME_PTR last_mouse_glyph_frame; + +#ifdef __STDC__ +extern int volatile input_signal_count; +#else +extern int input_signal_count; +#endif + +extern int mac_screen_config_changed; + +extern Lisp_Object Vmac_emulate_three_button_mouse; +#if TARGET_API_MAC_CARBON +extern int mac_wheel_button_is_mouse_2; +extern int mac_pass_command_to_system; +extern int mac_pass_control_to_system; +#endif /* TARGET_API_MAC_CARBON */ +extern int mac_ready_for_apple_events; + +extern void mac_focus_changed P_ ((int, struct mac_display_info *, + struct frame *, struct input_event *)); +extern int mac_get_emulated_btn P_ ((UInt32)); +extern int note_mouse_movement P_ ((FRAME_PTR, Point *)); +extern void mac_get_screen_info P_ ((struct mac_display_info *)); + +/* The focus may have changed. Figure out if it is a real focus change, + by checking both FocusIn/Out and Enter/LeaveNotify events. + + Returns FOCUS_IN_EVENT event in *BUFP. */ + +static void +x_detect_focus_change (dpyinfo, event, bufp) + struct mac_display_info *dpyinfo; + const EventRecord *event; + struct input_event *bufp; +{ + struct frame *frame; + + frame = mac_window_to_frame ((WindowRef) event->message); + if (! frame) + return; + + /* On Mac, this is only called from focus events, so no switch needed. */ + mac_focus_changed ((event->modifiers & activeFlag), + dpyinfo, frame, bufp); +} + +#if TARGET_API_MAC_CARBON +/* Obtains the event modifiers from the event EVENTREF and then calls + mac_to_emacs_modifiers. */ + +static int +mac_event_to_emacs_modifiers (EventRef eventRef) +{ + UInt32 mods = 0, class; + + GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL, + sizeof (UInt32), NULL, &mods); + class = GetEventClass (eventRef); + if (!NILP (Vmac_emulate_three_button_mouse) && + (class == kEventClassMouse || class == kEventClassCommand)) + { + mods &= ~(optionKey | cmdKey); + } + return mac_to_emacs_modifiers (mods, 0); +} + +/* Given an event REF, return the code to use for the mouse button + code in the emacs input_event. */ + +static int +mac_get_mouse_btn (EventRef ref) +{ + EventMouseButton result = kEventMouseButtonPrimary; + GetEventParameter (ref, kEventParamMouseButton, typeMouseButton, NULL, + sizeof (EventMouseButton), NULL, &result); + switch (result) + { + case kEventMouseButtonPrimary: + if (NILP (Vmac_emulate_three_button_mouse)) + return 0; + else { + UInt32 mods = 0; + GetEventParameter (ref, kEventParamKeyModifiers, typeUInt32, NULL, + sizeof (UInt32), NULL, &mods); + return mac_get_emulated_btn(mods); + } + case kEventMouseButtonSecondary: + return mac_wheel_button_is_mouse_2 ? 2 : 1; + case kEventMouseButtonTertiary: + case 4: /* 4 is the number for the mouse wheel button */ + return mac_wheel_button_is_mouse_2 ? 1 : 2; + default: + return 0; + } +} + +/* Normally, ConvertEventRefToEventRecord will correctly handle all + events. However the click of the mouse wheel is not converted to a + mouseDown or mouseUp event. Likewise for dead key events. This + calls ConvertEventRefToEventRecord, but then checks to see if it is + a mouse up/down, or a dead key Carbon event that has not been + converted, and if so, converts it by hand (to be picked up in the + XTread_socket loop). */ +static Boolean mac_convert_event_ref (EventRef eventRef, EventRecord *eventRec) +{ + OSStatus err; + Boolean result = ConvertEventRefToEventRecord (eventRef, eventRec); + EventKind action; + + if (result) + return result; + + switch (GetEventClass (eventRef)) + { + case kEventClassMouse: + switch (GetEventKind (eventRef)) + { + case kEventMouseDown: + eventRec->what = mouseDown; + result = 1; + break; + + case kEventMouseUp: + eventRec->what = mouseUp; + result = 1; + break; + + default: + break; + } + break; + + case kEventClassKeyboard: + switch (GetEventKind (eventRef)) + { + case kEventRawKeyDown: + action = keyDown; + goto keystroke_common; + case kEventRawKeyRepeat: + action = autoKey; + goto keystroke_common; + case kEventRawKeyUp: + action = keyUp; + keystroke_common: + { + unsigned char char_codes; + UInt32 key_code; + + err = GetEventParameter (eventRef, kEventParamKeyMacCharCodes, + typeChar, NULL, sizeof (char), + NULL, &char_codes); + if (err == noErr) + err = GetEventParameter (eventRef, kEventParamKeyCode, + typeUInt32, NULL, sizeof (UInt32), + NULL, &key_code); + if (err == noErr) + { + eventRec->what = action; + eventRec->message = char_codes | ((key_code & 0xff) << 8); + result = 1; + } + } + break; + + default: + break; + } + break; + + default: + break; + } + + if (result) + { + /* Need where and when. */ + UInt32 mods = 0; + + GetEventParameter (eventRef, kEventParamMouseLocation, typeQDPoint, + NULL, sizeof (Point), NULL, &eventRec->where); + /* Use two step process because new event modifiers are 32-bit + and old are 16-bit. Currently, only loss is NumLock & Fn. */ + GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, + NULL, sizeof (UInt32), NULL, &mods); + eventRec->modifiers = mods; + + eventRec->when = EventTimeToTicks (GetEventTime (eventRef)); + } + + return result; +} +#endif /* TARGET_API_MAC_CARBON */ + +#if !TARGET_API_MAC_CARBON +static RgnHandle mouse_region = NULL; + +Boolean +mac_wait_next_event (er, sleep_time, dequeue) + EventRecord *er; + UInt32 sleep_time; + Boolean dequeue; +{ + static EventRecord er_buf = {nullEvent}; + UInt32 target_tick, current_tick; + EventMask event_mask; + + if (mouse_region == NULL) + mouse_region = NewRgn (); + + event_mask = everyEvent; + if (!mac_ready_for_apple_events) + event_mask -= highLevelEventMask; + + current_tick = TickCount (); + target_tick = current_tick + sleep_time; + + if (er_buf.what == nullEvent) + while (!WaitNextEvent (event_mask, &er_buf, + target_tick - current_tick, mouse_region)) + { + current_tick = TickCount (); + if (target_tick <= current_tick) + return false; + } + + *er = er_buf; + if (dequeue) + er_buf.what = nullEvent; + return true; +} +#endif /* not TARGET_API_MAC_CARBON */ + +#if TARGET_API_MAC_CARBON +OSStatus +mac_post_mouse_moved_event () +{ + EventRef event = NULL; + OSStatus err; + + err = CreateEvent (NULL, kEventClassMouse, kEventMouseMoved, 0, + kEventAttributeNone, &event); + if (err == noErr) + { + Point mouse_pos; + + GetGlobalMouse (&mouse_pos); + err = SetEventParameter (event, kEventParamMouseLocation, typeQDPoint, + sizeof (Point), &mouse_pos); + } + if (err == noErr) + { + UInt32 modifiers = GetCurrentKeyModifiers (); + + err = SetEventParameter (event, kEventParamKeyModifiers, typeUInt32, + sizeof (UInt32), &modifiers); + } + if (err == noErr) + err = PostEventToQueue (GetCurrentEventQueue (), event, + kEventPriorityStandard); + if (event) + ReleaseEvent (event); + + return err; +} +#endif + +#ifdef MAC_OSX +/* Run the current run loop in the default mode until some input + happens or TIMEOUT seconds passes unless it is negative. Return + true if timeout occurs first. */ + +Boolean +mac_run_loop_run_once (timeout) + EventTimeout timeout; +{ +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (NULL); +#endif + return (CFRunLoopRunInMode (kCFRunLoopDefaultMode, + timeout >= 0 ? timeout : 100000, true) + == kCFRunLoopRunTimedOut); +} +#endif + +/* Emacs calls this whenever it wants to read an input event from the + user. */ + +int +XTread_socket (sd, expected, hold_quit) + int sd, expected; + struct input_event *hold_quit; +{ + struct input_event inev; + int count = 0; +#if TARGET_API_MAC_CARBON + EventRef eventRef; + EventTargetRef toolbox_dispatcher; +#endif + EventRecord er; + struct mac_display_info *dpyinfo = &one_mac_display_info; + + if (interrupt_input_blocked) + { + interrupt_input_pending = 1; + return -1; + } + + interrupt_input_pending = 0; + BLOCK_INPUT; + + /* So people can tell when we have read the available input. */ + input_signal_count++; + + ++handling_signal; + +#if TARGET_API_MAC_CARBON + toolbox_dispatcher = GetEventDispatcherTarget (); + + while ( +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (NULL), +#endif + !ReceiveNextEvent (0, NULL, kEventDurationNoWait, + kEventRemoveFromQueue, &eventRef)) +#else /* !TARGET_API_MAC_CARBON */ + while (mac_wait_next_event (&er, 0, true)) +#endif /* !TARGET_API_MAC_CARBON */ + { + int do_help = 0; + struct frame *f; + unsigned long timestamp; + + EVENT_INIT (inev); + inev.kind = NO_EVENT; + inev.arg = Qnil; + +#if TARGET_API_MAC_CARBON + timestamp = GetEventTime (eventRef) / kEventDurationMillisecond; + + if (!mac_convert_event_ref (eventRef, &er)) + goto OTHER; +#else /* !TARGET_API_MAC_CARBON */ + timestamp = er.when * (1000 / 60); /* ticks to milliseconds */ +#endif /* !TARGET_API_MAC_CARBON */ + + switch (er.what) + { + case mouseDown: + case mouseUp: + { + WindowRef window_ptr; + ControlPartCode part_code; + int tool_bar_p = 0; + +#if TARGET_API_MAC_CARBON + OSStatus err; + + /* This is needed to send mouse events like aqua window + buttons to the correct handler. */ + read_socket_inev = &inev; + err = SendEventToEventTarget (eventRef, toolbox_dispatcher); + read_socket_inev = NULL; + if (err != eventNotHandledErr) + break; +#endif + last_mouse_glyph_frame = 0; + + if (dpyinfo->grabbed && last_mouse_frame + && FRAME_LIVE_P (last_mouse_frame)) + { + window_ptr = FRAME_MAC_WINDOW (last_mouse_frame); + part_code = inContent; + } + else + { + part_code = FindWindow (er.where, &window_ptr); + if (tip_window && window_ptr == tip_window) + { + HideWindow (tip_window); + part_code = FindWindow (er.where, &window_ptr); + } + } + + if (er.what != mouseDown + && (part_code != inContent || dpyinfo->grabbed == 0)) + break; + + switch (part_code) + { + case inMenuBar: + f = mac_focus_frame (dpyinfo); + saved_menu_event_location = er.where; + inev.kind = MENU_BAR_ACTIVATE_EVENT; + XSETFRAME (inev.frame_or_window, f); + break; + + case inContent: + if ( +#if TARGET_API_MAC_CARBON + FrontNonFloatingWindow () +#else + FrontWindow () +#endif + != window_ptr + || (mac_window_to_frame (window_ptr) + != dpyinfo->x_focus_frame)) + SelectWindow (window_ptr); + else + { + ControlPartCode control_part_code; + ControlRef ch; + Point mouse_loc; +#ifdef MAC_OSX + ControlKind control_kind; +#endif + + f = mac_window_to_frame (window_ptr); + /* convert to local coordinates of new window */ + mouse_loc.h = (er.where.h + - (f->left_pos + + FRAME_OUTER_TO_INNER_DIFF_X (f))); + mouse_loc.v = (er.where.v + - (f->top_pos + + FRAME_OUTER_TO_INNER_DIFF_Y (f))); +#if TARGET_API_MAC_CARBON + ch = FindControlUnderMouse (mouse_loc, window_ptr, + &control_part_code); +#ifdef MAC_OSX + if (ch) + GetControlKind (ch, &control_kind); +#endif +#else + control_part_code = FindControl (mouse_loc, window_ptr, + &ch); +#endif + +#if TARGET_API_MAC_CARBON + inev.code = mac_get_mouse_btn (eventRef); + inev.modifiers = mac_event_to_emacs_modifiers (eventRef); +#else + inev.code = mac_get_emulated_btn (er.modifiers); + inev.modifiers = mac_to_emacs_modifiers (er.modifiers, 0); +#endif + XSETINT (inev.x, mouse_loc.h); + XSETINT (inev.y, mouse_loc.v); + + if ((dpyinfo->grabbed && tracked_scroll_bar) + || (ch != 0 +#ifndef USE_TOOLKIT_SCROLL_BARS + /* control_part_code becomes kControlNoPart if + a progress indicator is clicked. */ + && control_part_code != kControlNoPart +#else /* USE_TOOLKIT_SCROLL_BARS */ +#ifdef MAC_OSX + && control_kind.kind == kControlKindScrollBar +#endif /* MAC_OSX */ +#endif /* USE_TOOLKIT_SCROLL_BARS */ + )) + { + struct scroll_bar *bar; + + if (dpyinfo->grabbed && tracked_scroll_bar) + { + bar = tracked_scroll_bar; +#ifndef USE_TOOLKIT_SCROLL_BARS + control_part_code = kControlIndicatorPart; +#endif + } + else + bar = (struct scroll_bar *) GetControlReference (ch); +#ifdef USE_TOOLKIT_SCROLL_BARS + /* Make the "Ctrl-Mouse-2 splits window" work + for toolkit scroll bars. */ + if (inev.modifiers & ctrl_modifier) + x_scroll_bar_handle_click (bar, control_part_code, + &er, &inev); + else if (er.what == mouseDown) + x_scroll_bar_handle_press (bar, control_part_code, + mouse_loc, &inev); + else + x_scroll_bar_handle_release (bar, &inev); +#else /* not USE_TOOLKIT_SCROLL_BARS */ + x_scroll_bar_handle_click (bar, control_part_code, + &er, &inev); + if (er.what == mouseDown + && control_part_code == kControlIndicatorPart) + tracked_scroll_bar = bar; + else + tracked_scroll_bar = NULL; +#endif /* not USE_TOOLKIT_SCROLL_BARS */ + } + else + { + Lisp_Object window; + int x = mouse_loc.h; + int y = mouse_loc.v; + + window = window_from_coordinates (f, x, y, 0, 0, 0, 1); + if (EQ (window, f->tool_bar_window)) + { + if (er.what == mouseDown) + handle_tool_bar_click (f, x, y, 1, 0); + else + handle_tool_bar_click (f, x, y, 0, + inev.modifiers); + tool_bar_p = 1; + } + else + { + XSETFRAME (inev.frame_or_window, f); + inev.kind = MOUSE_CLICK_EVENT; + } + } + + if (er.what == mouseDown) + { + dpyinfo->grabbed |= (1 << inev.code); + last_mouse_frame = f; + + if (!tool_bar_p) + last_tool_bar_item = -1; + } + else + { + if ((dpyinfo->grabbed & (1 << inev.code)) == 0) + /* If a button is released though it was not + previously pressed, that would be because + of multi-button emulation. */ + dpyinfo->grabbed = 0; + else + dpyinfo->grabbed &= ~(1 << inev.code); + } + + /* Ignore any mouse motion that happened before + this event; any subsequent mouse-movement Emacs + events should reflect only motion after the + ButtonPress. */ + if (f != 0) + f->mouse_moved = 0; + +#ifdef USE_TOOLKIT_SCROLL_BARS + if (inev.kind == MOUSE_CLICK_EVENT + || (inev.kind == SCROLL_BAR_CLICK_EVENT + && (inev.modifiers & ctrl_modifier))) +#endif + switch (er.what) + { + case mouseDown: + inev.modifiers |= down_modifier; + break; + case mouseUp: + inev.modifiers |= up_modifier; + break; + } + } + break; + + case inDrag: +#if TARGET_API_MAC_CARBON + case inProxyIcon: + if (IsWindowPathSelectClick (window_ptr, &er)) + { + WindowPathSelect (window_ptr, NULL, NULL); + break; + } + if (part_code == inProxyIcon + && (TrackWindowProxyDrag (window_ptr, er.where) + != errUserWantsToDragWindow)) + break; + DragWindow (window_ptr, er.where, NULL); +#else /* not TARGET_API_MAC_CARBON */ + DragWindow (window_ptr, er.where, &qd.screenBits.bounds); + /* Update the frame parameters. */ + { + struct frame *f = mac_window_to_frame (window_ptr); + + if (f && !f->async_iconified) + mac_handle_origin_change (f); + } +#endif /* not TARGET_API_MAC_CARBON */ + break; + + case inGoAway: + if (TrackGoAway (window_ptr, er.where)) + { + inev.kind = DELETE_WINDOW_EVENT; + XSETFRAME (inev.frame_or_window, + mac_window_to_frame (window_ptr)); + } + break; + + /* window resize handling added --ben */ + case inGrow: + do_grow_window (window_ptr, &er); + break; + + /* window zoom handling added --ben */ + case inZoomIn: + case inZoomOut: + if (TrackBox (window_ptr, er.where, part_code)) + do_zoom_window (window_ptr, part_code); + break; + +#if USE_MAC_TOOLBAR + case inStructure: + { + OSStatus err; + HIViewRef ch; + + err = HIViewGetViewForMouseEvent (HIViewGetRoot (window_ptr), + eventRef, &ch); + /* This doesn't work on Mac OS X 10.2. */ + if (err == noErr) + HIViewClick (ch, eventRef); + } + break; +#endif /* USE_MAC_TOOLBAR */ + + default: + break; + } + } + break; + +#if !TARGET_API_MAC_CARBON + case updateEvt: + do_window_update ((WindowRef) er.message); + break; +#endif + + case osEvt: +#if TARGET_API_MAC_CARBON + if (SendEventToEventTarget (eventRef, toolbox_dispatcher) + != eventNotHandledErr) + break; +#endif + switch ((er.message >> 24) & 0x000000FF) + { +#if USE_MAC_TSM + case suspendResumeMessage: + if (er.message & resumeFlag) + mac_tsm_resume (); + else + mac_tsm_suspend (); + break; +#endif + + case mouseMovedMessage: +#if !TARGET_API_MAC_CARBON + SetRectRgn (mouse_region, er.where.h, er.where.v, + er.where.h + 1, er.where.v + 1); +#endif + previous_help_echo_string = help_echo_string; + help_echo_string = Qnil; + + if (dpyinfo->grabbed && last_mouse_frame + && FRAME_LIVE_P (last_mouse_frame)) + f = last_mouse_frame; + else + f = dpyinfo->x_focus_frame; + + if (dpyinfo->mouse_face_hidden) + { + dpyinfo->mouse_face_hidden = 0; + clear_mouse_face (dpyinfo); + } + + if (f) + { + WindowRef wp = FRAME_MAC_WINDOW (f); + Point mouse_pos; + + mouse_pos.h = (er.where.h + - (f->left_pos + + FRAME_OUTER_TO_INNER_DIFF_X (f))); + mouse_pos.v = (er.where.v + - (f->top_pos + + FRAME_OUTER_TO_INNER_DIFF_Y (f))); + if (dpyinfo->grabbed && tracked_scroll_bar) +#ifdef USE_TOOLKIT_SCROLL_BARS + x_scroll_bar_handle_drag (wp, tracked_scroll_bar, + mouse_pos, &inev); +#else /* not USE_TOOLKIT_SCROLL_BARS */ + x_scroll_bar_note_movement (tracked_scroll_bar, + mouse_pos.v + - XINT (tracked_scroll_bar->top), + er.when * (1000 / 60)); +#endif /* not USE_TOOLKIT_SCROLL_BARS */ + else + { + /* Generate SELECT_WINDOW_EVENTs when needed. */ + if (!NILP (Vmouse_autoselect_window)) + { + Lisp_Object window; + + window = window_from_coordinates (f, + mouse_pos.h, + mouse_pos.v, + 0, 0, 0, 0); + + /* Window will be selected only when it is + not selected now and last mouse movement + event was not in it. Minibuffer window + will be selected only when it is active. */ + if (WINDOWP (window) + && !EQ (window, last_window) + && !EQ (window, selected_window) + /* For click-to-focus window managers + create event iff we don't leave the + selected frame. */ + && (focus_follows_mouse + || (EQ (XWINDOW (window)->frame, + XWINDOW (selected_window)->frame)))) + { + inev.kind = SELECT_WINDOW_EVENT; + inev.frame_or_window = window; + } + + last_window=window; + } + if (!note_mouse_movement (f, &mouse_pos)) + help_echo_string = previous_help_echo_string; +#if USE_MAC_TOOLBAR + else + mac_tool_bar_note_mouse_movement (f, eventRef); +#endif + } + } + + /* If the contents of the global variable + help_echo_string has changed, generate a + HELP_EVENT. */ + if (!NILP (help_echo_string) || !NILP (previous_help_echo_string)) + do_help = 1; + break; + } + break; + + case activateEvt: + { + WindowRef window_ptr = (WindowRef) er.message; + OSErr err; + ControlRef root_control; + + if (window_ptr == tip_window) + { + HideWindow (tip_window); + break; + } + + if (!is_emacs_window (window_ptr)) + goto OTHER; + + f = mac_window_to_frame (window_ptr); + + if ((er.modifiers & activeFlag) != 0) + { + /* A window has been activated */ + Point mouse_loc; + + err = GetRootControl (FRAME_MAC_WINDOW (f), &root_control); + if (err == noErr) + ActivateControl (root_control); + + x_detect_focus_change (dpyinfo, &er, &inev); + + mouse_loc.h = (er.where.h + - (f->left_pos + + FRAME_OUTER_TO_INNER_DIFF_X (f))); + mouse_loc.v = (er.where.v + - (f->top_pos + + FRAME_OUTER_TO_INNER_DIFF_Y (f))); + /* Window-activated event counts as mouse movement, + so update things that depend on mouse position. */ + note_mouse_movement (f, &mouse_loc); + } + else + { + /* A window has been deactivated */ + err = GetRootControl (FRAME_MAC_WINDOW (f), &root_control); + if (err == noErr) + DeactivateControl (root_control); + +#ifdef USE_TOOLKIT_SCROLL_BARS + if (dpyinfo->grabbed && tracked_scroll_bar) + { + struct input_event event; + + EVENT_INIT (event); + event.kind = NO_EVENT; + x_scroll_bar_handle_release (tracked_scroll_bar, &event); + if (event.kind != NO_EVENT) + { + event.timestamp = timestamp; + kbd_buffer_store_event_hold (&event, hold_quit); + count++; + } + } +#endif + dpyinfo->grabbed = 0; + + x_detect_focus_change (dpyinfo, &er, &inev); + + if (f == dpyinfo->mouse_face_mouse_frame) + { + /* If we move outside the frame, then we're + certainly no longer on any text in the + frame. */ + clear_mouse_face (dpyinfo); + dpyinfo->mouse_face_mouse_frame = 0; + } + + /* Generate a nil HELP_EVENT to cancel a help-echo. + Do it only if there's something to cancel. + Otherwise, the startup message is cleared when the + mouse leaves the frame. */ + if (any_help_event_p) + do_help = -1; + } + } + break; + + case keyDown: + case keyUp: + case autoKey: + ObscureCursor (); + + f = mac_focus_frame (dpyinfo); + XSETFRAME (inev.frame_or_window, f); + + /* If mouse-highlight is an integer, input clears out mouse + highlighting. */ + if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight) + && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)) + { + clear_mouse_face (dpyinfo); + dpyinfo->mouse_face_hidden = 1; + } + + { + UInt32 modifiers = er.modifiers, mapped_modifiers; + UInt32 key_code = (er.message & keyCodeMask) >> 8; + +#ifdef MAC_OSX + GetEventParameter (eventRef, kEventParamKeyModifiers, + typeUInt32, NULL, + sizeof (UInt32), NULL, &modifiers); +#endif + mapped_modifiers = mac_mapped_modifiers (modifiers, key_code); + +#if TARGET_API_MAC_CARBON + if (!(mapped_modifiers + & ~(mac_pass_command_to_system ? cmdKey : 0) + & ~(mac_pass_control_to_system ? controlKey : 0))) + goto OTHER; + else +#endif + if (er.what != keyUp) + do_keystroke (er.what, er.message & charCodeMask, + key_code, modifiers, timestamp, &inev); + } + break; + + case kHighLevelEvent: + AEProcessAppleEvent (&er); + break; + + default: + OTHER: +#if TARGET_API_MAC_CARBON + { + OSStatus err; + + read_socket_inev = &inev; + err = SendEventToEventTarget (eventRef, toolbox_dispatcher); + read_socket_inev = NULL; + } +#endif + break; + } +#if TARGET_API_MAC_CARBON + ReleaseEvent (eventRef); +#endif + + if (inev.kind != NO_EVENT) + { + inev.timestamp = timestamp; + kbd_buffer_store_event_hold (&inev, hold_quit); + count++; + } + + if (do_help + && !(hold_quit && hold_quit->kind != NO_EVENT)) + { + Lisp_Object frame; + + if (f) + XSETFRAME (frame, f); + else + frame = Qnil; + + if (do_help > 0) + { + any_help_event_p = 1; + gen_help_event (help_echo_string, frame, help_echo_window, + help_echo_object, help_echo_pos); + } + else + { + help_echo_string = Qnil; + gen_help_event (Qnil, frame, Qnil, Qnil, 0); + } + count++; + } + } + + /* If the focus was just given to an autoraising frame, + raise it now. */ + /* ??? This ought to be able to handle more than one such frame. */ + if (pending_autoraise_frame) + { + x_raise_frame (pending_autoraise_frame); + pending_autoraise_frame = 0; + } + + if (mac_screen_config_changed) + { + mac_get_screen_info (dpyinfo); + mac_screen_config_changed = 0; + } + +#if !TARGET_API_MAC_CARBON + /* Check which frames are still visible. We do this here because + there doesn't seem to be any direct notification from the Window + Manager that the visibility of a window has changed (at least, + not in all cases). */ + { + Lisp_Object tail, frame; + + FOR_EACH_FRAME (tail, frame) + { + struct frame *f = XFRAME (frame); + + /* The tooltip has been drawn already. Avoid the + SET_FRAME_GARBAGED in mac_handle_visibility_change. */ + if (EQ (frame, tip_frame)) + continue; + + if (FRAME_MAC_P (f)) + mac_handle_visibility_change (f); + } + } +#endif + + --handling_signal; + UNBLOCK_INPUT; + return count; +} + + +/*********************************************************************** + Busy cursor + ***********************************************************************/ + +#if TARGET_API_MAC_CARBON +/* Show the spinning progress indicator for the frame F. Create it if + it doesn't exist yet. */ + +void +mac_show_hourglass (f) + struct frame *f; +{ +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (f); +#endif + if (!f->output_data.mac->hourglass_control) + { + Window w = FRAME_MAC_WINDOW (f); + Rect r; + ControlRef c; + + GetWindowPortBounds (w, &r); + r.left = r.right - HOURGLASS_WIDTH; + r.bottom = r.top + HOURGLASS_HEIGHT; + if (CreateChasingArrowsControl (w, &r, &c) == noErr) + f->output_data.mac->hourglass_control = c; + } + + if (f->output_data.mac->hourglass_control) + ShowControl (f->output_data.mac->hourglass_control); +} + +/* Hide the spinning progress indicator for the frame F. Do nothing + it doesn't exist yet. */ + +void +mac_hide_hourglass (f) + struct frame *f; +{ + if (f->output_data.mac->hourglass_control) + { +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (f); +#endif + HideControl (f->output_data.mac->hourglass_control); + } +} + +/* Reposition the spinning progress indicator for the frame F. Do + nothing it doesn't exist yet. */ + +void +mac_reposition_hourglass (f) + struct frame *f; +{ + if (f->output_data.mac->hourglass_control) + { +#if USE_CG_DRAWING + mac_prepare_for_quickdraw (f); +#endif + MoveControl (f->output_data.mac->hourglass_control, + FRAME_PIXEL_WIDTH (f) - HOURGLASS_WIDTH, 0); + } +} +#endif /* TARGET_API_MAC_CARBON */ + + +/*********************************************************************** + File selection dialog + ***********************************************************************/ + +#if TARGET_API_MAC_CARBON +extern Lisp_Object Qfile_name_history; + +static pascal void mac_nav_event_callback P_ ((NavEventCallbackMessage, + NavCBRecPtr, void *)); + +/* The actual implementation of Fx_file_dialog. */ + +Lisp_Object +mac_file_dialog (prompt, dir, default_filename, mustmatch, only_dir_p) + Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p; +{ + Lisp_Object file = Qnil; + int count = SPECPDL_INDEX (); + struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6; + char filename[MAXPATHLEN]; + static NavEventUPP mac_nav_event_callbackUPP = NULL; + + check_mac (); + + GCPRO6 (prompt, dir, default_filename, mustmatch, file, only_dir_p); + CHECK_STRING (prompt); + CHECK_STRING (dir); + + /* Create the dialog with PROMPT as title, using DIR as initial + directory and using "*" as pattern. */ + dir = Fexpand_file_name (dir, Qnil); + + { + OSStatus status; + NavDialogCreationOptions options; + NavDialogRef dialogRef; + NavTypeListHandle fileTypes = NULL; + NavUserAction userAction; + CFStringRef message=NULL, saveName = NULL; + + BLOCK_INPUT; + /* No need for a callback function because we are modal */ + NavGetDefaultDialogCreationOptions(&options); + options.modality = kWindowModalityAppModal; + options.location.h = options.location.v = -1; + options.optionFlags = kNavDefaultNavDlogOptions; + options.optionFlags |= kNavAllFilesInPopup; /* All files allowed */ + options.optionFlags |= kNavSelectAllReadableItem; + options.optionFlags &= ~kNavAllowMultipleFiles; + if (!NILP(prompt)) + { + message = cfstring_create_with_string (prompt); + options.message = message; + } + /* Don't set the application, let it use default. + options.clientName = CFSTR ("Emacs"); + */ + + if (mac_nav_event_callbackUPP == NULL) + mac_nav_event_callbackUPP = NewNavEventUPP (mac_nav_event_callback); + + if (!NILP (only_dir_p)) + status = NavCreateChooseFolderDialog(&options, mac_nav_event_callbackUPP, + NULL, NULL, &dialogRef); + else if (NILP (mustmatch)) + { + /* This is a save dialog */ + options.optionFlags |= kNavDontConfirmReplacement; + options.actionButtonLabel = CFSTR ("Ok"); + options.windowTitle = CFSTR ("Enter name"); + + if (STRINGP (default_filename)) + { + Lisp_Object utf8 = ENCODE_UTF_8 (default_filename); + char *begPtr = SDATA(utf8); + char *filePtr = begPtr + SBYTES(utf8); + while (filePtr != begPtr && !IS_DIRECTORY_SEP(filePtr[-1])) + filePtr--; + saveName = cfstring_create_with_utf8_cstring (filePtr); + options.saveFileName = saveName; + options.optionFlags |= kNavSelectDefaultLocation; + } + status = NavCreatePutFileDialog(&options, + 'TEXT', kNavGenericSignature, + mac_nav_event_callbackUPP, NULL, + &dialogRef); + } + else + { + /* This is an open dialog*/ + status = NavCreateChooseFileDialog(&options, fileTypes, + mac_nav_event_callbackUPP, NULL, + NULL, NULL, &dialogRef); + } + + /* Set the default location and continue*/ + if (status == noErr) + { + Lisp_Object encoded_dir = ENCODE_FILE (dir); + AEDesc defLocAed; + + status = AECreateDesc (TYPE_FILE_NAME, SDATA (encoded_dir), + SBYTES (encoded_dir), &defLocAed); + if (status == noErr) + { + NavCustomControl(dialogRef, kNavCtlSetLocation, (void*) &defLocAed); + AEDisposeDesc(&defLocAed); + } + status = NavDialogRun(dialogRef); + } + + if (saveName) CFRelease(saveName); + if (message) CFRelease(message); + + if (status == noErr) { + userAction = NavDialogGetUserAction(dialogRef); + switch (userAction) + { + case kNavUserActionNone: + case kNavUserActionCancel: + break; /* Treat cancel like C-g */ + case kNavUserActionOpen: + case kNavUserActionChoose: + case kNavUserActionSaveAs: + { + NavReplyRecord reply; + Size len; + + status = NavDialogGetReply(dialogRef, &reply); + if (status != noErr) + break; + status = AEGetNthPtr (&reply.selection, 1, TYPE_FILE_NAME, + NULL, NULL, filename, + sizeof (filename) - 1, &len); + if (status == noErr) + { + len = min (len, sizeof (filename) - 1); + filename[len] = '\0'; + if (reply.saveFileName) + { + /* If it was a saved file, we need to add the file name */ + if (len && len < sizeof (filename) - 1 + && filename[len-1] != '/') + filename[len++] = '/'; + CFStringGetCString(reply.saveFileName, filename+len, + sizeof (filename) - len, +#ifdef MAC_OSX + kCFStringEncodingUTF8 +#else + CFStringGetSystemEncoding () +#endif + ); + } + file = DECODE_FILE (make_unibyte_string (filename, + strlen (filename))); + } + NavDisposeReply(&reply); + } + break; + } + NavDialogDispose(dialogRef); + UNBLOCK_INPUT; + } + else { + UNBLOCK_INPUT; + /* Fall back on minibuffer if there was a problem */ + file = Fcompleting_read (prompt, intern ("read-file-name-internal"), + dir, mustmatch, dir, Qfile_name_history, + default_filename, Qnil); + } + } + + UNGCPRO; + + /* Make "Cancel" equivalent to C-g. */ + if (NILP (file)) + Fsignal (Qquit, Qnil); + + return unbind_to (count, file); +} + +/* Need to register some event callback function for enabling drag and + drop in Navigation Service dialogs. */ +static pascal void +mac_nav_event_callback (selector, parms, data) + NavEventCallbackMessage selector; + NavCBRecPtr parms; + void *data; +{ +} +#endif + + +/************************************************************************ + Menu + ************************************************************************/ + +#if !TARGET_API_MAC_CARBON +#include +#include +#include +#include +#include +#include +#include +#include +#if defined (__MRC__) || (__MSL__ >= 0x6000) +#include +#endif +#endif /* not TARGET_API_MAC_CARBON */ + +extern int menu_item_selection; +extern int popup_activated_flag; +extern int name_is_separator P_ ((const char *)); +extern void find_and_call_menu_selection P_ ((FRAME_PTR, int, Lisp_Object, + void *)); +extern void set_frame_menubar P_ ((FRAME_PTR, int, int)); + +enum mac_menu_kind { /* Menu ID range */ + MAC_MENU_APPLE, /* 0 (Reserved by Apple) */ + MAC_MENU_MENU_BAR, /* 1 .. 233 */ + MAC_MENU_M_APPLE, /* 234 (== M_APPLE) */ + MAC_MENU_POPUP, /* 235 */ + MAC_MENU_DRIVER, /* 236 .. 255 (Reserved) */ + MAC_MENU_MENU_BAR_SUB, /* 256 .. 16383 */ + MAC_MENU_POPUP_SUB, /* 16384 .. 32767 */ + MAC_MENU_END /* 32768 */ +}; + +static const int min_menu_id[] = {0, 1, 234, 235, 236, 256, 16384, 32768}; + +static int fill_menu P_ ((MenuRef, widget_value *, enum mac_menu_kind, int)); +static void dispose_menus P_ ((enum mac_menu_kind, int)); + +#if !TARGET_API_MAC_CARBON +static void +do_apple_menu (SInt16 menu_item) +{ + Str255 item_name; + SInt16 da_driver_refnum; + + if (menu_item == I_ABOUT) + NoteAlert (ABOUT_ALERT_ID, NULL); + else + { + GetMenuItemText (GetMenuRef (M_APPLE), menu_item, item_name); + da_driver_refnum = OpenDeskAcc (item_name); + } +} +#endif /* !TARGET_API_MAC_CARBON */ + +/* Activate the menu bar of frame F. + This is called from keyboard.c when it gets the + MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue. + + To activate the menu bar, we use the button-press event location + that was saved in saved_menu_event_location. + + But first we recompute the menu bar contents (the whole tree). + + The reason for saving the button event until here, instead of + passing it to the toolkit right away, is that we can safely + execute Lisp code. */ + +void +x_activate_menubar (f) + FRAME_PTR f; +{ + SInt32 menu_choice; + SInt16 menu_id, menu_item; + extern Point saved_menu_event_location; + + set_frame_menubar (f, 0, 1); + BLOCK_INPUT; + + popup_activated_flag = 1; + menu_choice = MenuSelect (saved_menu_event_location); + popup_activated_flag = 0; + menu_id = HiWord (menu_choice); + menu_item = LoWord (menu_choice); + +#if !TARGET_API_MAC_CARBON + if (menu_id == min_menu_id[MAC_MENU_M_APPLE]) + do_apple_menu (menu_item); + else +#endif + if (menu_id) + { + MenuRef menu = GetMenuRef (menu_id); + + if (menu) + { + UInt32 refcon; + + GetMenuItemRefCon (menu, menu_item, &refcon); + find_and_call_menu_selection (f, f->menu_bar_items_used, + f->menu_bar_vector, (void *) refcon); + } + } + + HiliteMenu (0); + + UNBLOCK_INPUT; +} + +#if TARGET_API_MAC_CARBON +extern Lisp_Object Vshow_help_function; + +static Lisp_Object +restore_show_help_function (old_show_help_function) + Lisp_Object old_show_help_function; +{ + Vshow_help_function = old_show_help_function; + + return Qnil; +} + +static pascal OSStatus +menu_target_item_handler (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + OSStatus err; + MenuRef menu; + MenuItemIndex menu_item; + Lisp_Object help; + GrafPtr port; + int specpdl_count = SPECPDL_INDEX (); + + /* Don't be bothered with the overflowed toolbar items menu. */ + if (!popup_activated ()) + return eventNotHandledErr; + + err = GetEventParameter (event, kEventParamDirectObject, typeMenuRef, + NULL, sizeof (MenuRef), NULL, &menu); + if (err == noErr) + err = GetEventParameter (event, kEventParamMenuItemIndex, + typeMenuItemIndex, NULL, + sizeof (MenuItemIndex), NULL, &menu_item); + if (err == noErr) + err = GetMenuItemProperty (menu, menu_item, + MAC_EMACS_CREATOR_CODE, 'help', + sizeof (Lisp_Object), NULL, &help); + if (err != noErr) + help = Qnil; + + /* Temporarily bind Vshow_help_function to Qnil because we don't + want tooltips during menu tracking. */ + record_unwind_protect (restore_show_help_function, Vshow_help_function); + Vshow_help_function = Qnil; + GetPort (&port); + show_help_echo (help, Qnil, Qnil, Qnil, 1); + SetPort (port); + unbind_to (specpdl_count, Qnil); + + return err == noErr ? noErr : eventNotHandledErr; +} + +/* Showing help echo string during menu tracking. */ + +static OSStatus +install_menu_target_item_handler () +{ + static const EventTypeSpec specs[] = + {{kEventClassMenu, kEventMenuTargetItem}}; + + return InstallApplicationEventHandler (NewEventHandlerUPP + (menu_target_item_handler), + GetEventTypeCount (specs), + specs, NULL, NULL); +} +#endif /* TARGET_API_MAC_CARBON */ + +/* Event handler function that pops down a menu on C-g. We can only pop + down menus if CancelMenuTracking is present (OSX 10.3 or later). */ + +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 +static pascal OSStatus +menu_quit_handler (nextHandler, theEvent, userData) + EventHandlerCallRef nextHandler; + EventRef theEvent; + void* userData; +{ + OSStatus err; + UInt32 keyCode; + UInt32 keyModifiers; + + err = GetEventParameter (theEvent, kEventParamKeyCode, + typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode); + + if (err == noErr) + err = GetEventParameter (theEvent, kEventParamKeyModifiers, + typeUInt32, NULL, sizeof(UInt32), + NULL, &keyModifiers); + + if (err == noErr && mac_quit_char_key_p (keyModifiers, keyCode)) + { + MenuRef menu = userData != 0 + ? (MenuRef)userData : AcquireRootMenu (); + + CancelMenuTracking (menu, true, 0); + if (!userData) ReleaseMenu (menu); + return noErr; + } + + return CallNextEventHandler (nextHandler, theEvent); +} +#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ + +/* Add event handler to all menus that belong to KIND so we can detect + C-g. ROOT_MENU is the root menu of the tracking session to dismiss + when C-g is detected. NULL means the menu bar. If + CancelMenuTracking isn't available, do nothing. */ + +static void +install_menu_quit_handler (kind, root_menu) + enum mac_menu_kind kind; + MenuRef root_menu; +{ +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 + static const EventTypeSpec typesList[] = + {{kEventClassKeyboard, kEventRawKeyDown}}; + int id; + +#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020 + if (CancelMenuTracking == NULL) + return; +#endif + for (id = min_menu_id[kind]; id < min_menu_id[kind + 1]; id++) + { + MenuRef menu = GetMenuRef (id); + + if (menu == NULL) + break; + InstallMenuEventHandler (menu, menu_quit_handler, + GetEventTypeCount (typesList), + typesList, root_menu, NULL); + } +#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ +} + +static Lisp_Object +pop_down_menu (arg) + Lisp_Object arg; +{ + struct Lisp_Save_Value *p = XSAVE_VALUE (arg); + FRAME_PTR f = p->pointer; + MenuRef menu = GetMenuRef (min_menu_id[MAC_MENU_POPUP]); + + BLOCK_INPUT; + + /* Must reset this manually because the button release event is not + passed to Emacs event loop. */ + FRAME_MAC_DISPLAY_INFO (f)->grabbed = 0; + + /* delete all menus */ + dispose_menus (MAC_MENU_POPUP_SUB, 0); + DeleteMenu (min_menu_id[MAC_MENU_POPUP]); + DisposeMenu (menu); + + UNBLOCK_INPUT; + + return Qnil; +} + +/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop + until the menu pops down. Return the selection. */ + +void +create_and_show_popup_menu (f, first_wv, x, y, for_click) + FRAME_PTR f; + widget_value *first_wv; + int x; + int y; + int for_click; +{ + int result = 0; + MenuRef menu = NewMenu (min_menu_id[MAC_MENU_POPUP], "\p"); + int menu_item_choice; + int specpdl_count = SPECPDL_INDEX (); + + InsertMenu (menu, -1); + fill_menu (menu, first_wv->contents, MAC_MENU_POPUP_SUB, + min_menu_id[MAC_MENU_POPUP_SUB]); + + /* Add event handler so we can detect C-g. */ + install_menu_quit_handler (MAC_MENU_POPUP, menu); + install_menu_quit_handler (MAC_MENU_POPUP_SUB, menu); + + record_unwind_protect (pop_down_menu, make_save_value (f, 0)); + + /* Adjust coordinates to be root-window-relative. */ + x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); + y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); + + /* Display the menu. */ + popup_activated_flag = 1; + menu_item_choice = PopUpMenuSelect (menu, y, x, 0); + popup_activated_flag = 0; + + /* Get the refcon to find the correct item */ + if (menu_item_choice) + { + MenuRef sel_menu = GetMenuRef (HiWord (menu_item_choice)); + + if (sel_menu) + GetMenuItemRefCon (sel_menu, LoWord (menu_item_choice), + (UInt32 *) &result); + } + + unbind_to (specpdl_count, Qnil); + + menu_item_selection = result; +} + +static void +add_menu_item (menu, pos, wv) + MenuRef menu; + int pos; + widget_value *wv; +{ +#if TARGET_API_MAC_CARBON + CFStringRef item_name; +#else + Str255 item_name; +#endif + + if (name_is_separator (wv->name)) + AppendMenu (menu, "\p-"); + else + { + AppendMenu (menu, "\pX"); + +#if TARGET_API_MAC_CARBON + item_name = cfstring_create_with_utf8_cstring (wv->name); + + if (wv->key != NULL) + { + CFStringRef name, key; + + name = item_name; + key = cfstring_create_with_utf8_cstring (wv->key); + item_name = CFStringCreateWithFormat (NULL, NULL, CFSTR ("%@ %@"), + name, key); + CFRelease (name); + CFRelease (key); + } + + SetMenuItemTextWithCFString (menu, pos, item_name); + CFRelease (item_name); + + if (wv->enabled) + EnableMenuItem (menu, pos); + else + DisableMenuItem (menu, pos); + + if (STRINGP (wv->help)) + SetMenuItemProperty (menu, pos, MAC_EMACS_CREATOR_CODE, 'help', + sizeof (Lisp_Object), &wv->help); +#else /* ! TARGET_API_MAC_CARBON */ + item_name[sizeof (item_name) - 1] = '\0'; + strncpy (item_name, wv->name, sizeof (item_name) - 1); + if (wv->key != NULL) + { + int len = strlen (item_name); + + strncpy (item_name + len, " ", sizeof (item_name) - 1 - len); + len = strlen (item_name); + strncpy (item_name + len, wv->key, sizeof (item_name) - 1 - len); + } + c2pstr (item_name); + SetMenuItemText (menu, pos, item_name); + + if (wv->enabled) + EnableItem (menu, pos); + else + DisableItem (menu, pos); +#endif /* ! TARGET_API_MAC_CARBON */ + + /* Draw radio buttons and tickboxes. */ + if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE + || wv->button_type == BUTTON_TYPE_RADIO)) + SetItemMark (menu, pos, checkMark); + else + SetItemMark (menu, pos, noMark); + + SetMenuItemRefCon (menu, pos, (UInt32) wv->call_data); + } +} + +/* Construct native Mac OS menu based on widget_value tree. */ + +static int +fill_menu (menu, wv, kind, submenu_id) + MenuRef menu; + widget_value *wv; + enum mac_menu_kind kind; + int submenu_id; +{ + int pos; + + for (pos = 1; wv != NULL; wv = wv->next, pos++) + { + add_menu_item (menu, pos, wv); + if (wv->contents && submenu_id < min_menu_id[kind + 1]) + { + MenuRef submenu = NewMenu (submenu_id, "\pX"); + + InsertMenu (submenu, -1); +#if TARGET_API_MAC_CARBON + SetMenuItemHierarchicalMenu (menu, pos, submenu); +#else + SetMenuItemHierarchicalID (menu, pos, submenu_id); +#endif + submenu_id = fill_menu (submenu, wv->contents, kind, submenu_id + 1); + } + } + + return submenu_id; +} + +/* Fill menu bar with the items defined by WV. If DEEP_P, consider + the entire menu trees we supply, rather than just the menu bar item + names. */ + +void +mac_fill_menubar (wv, deep_p) + widget_value *wv; + int deep_p; +{ + int id, submenu_id; +#if !TARGET_API_MAC_CARBON + int title_changed_p = 0; +#endif + + /* Clean up the menu bar when filled by the entire menu trees. */ + if (deep_p) + { + dispose_menus (MAC_MENU_MENU_BAR, 0); + dispose_menus (MAC_MENU_MENU_BAR_SUB, 0); +#if !TARGET_API_MAC_CARBON + title_changed_p = 1; +#endif + } + + /* Fill menu bar titles and submenus. Reuse the existing menu bar + titles as much as possible to minimize redraw (if !deep_p). */ + submenu_id = min_menu_id[MAC_MENU_MENU_BAR_SUB]; + for (id = min_menu_id[MAC_MENU_MENU_BAR]; + wv != NULL && id < min_menu_id[MAC_MENU_MENU_BAR + 1]; + wv = wv->next, id++) + { + OSStatus err = noErr; + MenuRef menu; +#if TARGET_API_MAC_CARBON + CFStringRef title; + + title = CFStringCreateWithCString (NULL, wv->name, + kCFStringEncodingMacRoman); +#else + Str255 title; + + strncpy (title, wv->name, 255); + title[255] = '\0'; + c2pstr (title); +#endif + + menu = GetMenuRef (id); + if (menu) + { +#if TARGET_API_MAC_CARBON + CFStringRef old_title; + + err = CopyMenuTitleAsCFString (menu, &old_title); + if (err == noErr) + { + if (CFStringCompare (title, old_title, 0) != kCFCompareEqualTo) + { +#ifdef MAC_OSX + if (id + 1 == min_menu_id[MAC_MENU_MENU_BAR + 1] + || GetMenuRef (id + 1) == NULL) + { + /* This is a workaround for Mac OS X 10.5 where + just calling SetMenuTitleWithCFString fails + to change the title of the last (Help) menu + in the menu bar. */ + DeleteMenu (id); + DisposeMenu (menu); + menu = NULL; + } + else +#endif /* MAC_OSX */ + err = SetMenuTitleWithCFString (menu, title); + } + CFRelease (old_title); + } + else + err = SetMenuTitleWithCFString (menu, title); +#else /* !TARGET_API_MAC_CARBON */ + if (!EqualString (title, (*menu)->menuData, false, false)) + { + DeleteMenu (id); + DisposeMenu (menu); + menu = NewMenu (id, title); + InsertMenu (menu, GetMenuRef (id + 1) ? id + 1 : 0); + title_changed_p = 1; + } +#endif /* !TARGET_API_MAC_CARBON */ + } + + if (!menu) + { +#if TARGET_API_MAC_CARBON + err = CreateNewMenu (id, 0, &menu); + if (err == noErr) + err = SetMenuTitleWithCFString (menu, title); +#else + menu = NewMenu (id, title); +#endif + if (err == noErr) + { + InsertMenu (menu, 0); +#if !TARGET_API_MAC_CARBON + title_changed_p = 1; +#endif + } + } +#if TARGET_API_MAC_CARBON + CFRelease (title); +#endif + + if (err == noErr) + if (wv->contents) + submenu_id = fill_menu (menu, wv->contents, MAC_MENU_MENU_BAR_SUB, + submenu_id); + } + + if (id < min_menu_id[MAC_MENU_MENU_BAR + 1] && GetMenuRef (id)) + { + dispose_menus (MAC_MENU_MENU_BAR, id); +#if !TARGET_API_MAC_CARBON + title_changed_p = 1; +#endif + } + +#if !TARGET_API_MAC_CARBON + if (title_changed_p) + InvalMenuBar (); +#endif + + /* Add event handler so we can detect C-g. */ + install_menu_quit_handler (MAC_MENU_MENU_BAR, NULL); + install_menu_quit_handler (MAC_MENU_MENU_BAR_SUB, NULL); +} + +/* Dispose of menus that belong to KIND, and remove them from the menu + list. ID is the lower bound of menu IDs that will be processed. */ + +static void +dispose_menus (kind, id) + enum mac_menu_kind kind; + int id; +{ + for (id = max (id, min_menu_id[kind]); id < min_menu_id[kind + 1]; id++) + { + MenuRef menu = GetMenuRef (id); + + if (menu == NULL) + break; + DeleteMenu (id); + DisposeMenu (menu); + } +} + +static void +init_menu_bar () +{ +#ifdef MAC_OSX + OSStatus err; + MenuRef menu; + MenuItemIndex menu_index; + + err = GetIndMenuItemWithCommandID (NULL, kHICommandQuit, 1, + &menu, &menu_index); + if (err == noErr) + SetMenuItemCommandKey (menu, menu_index, false, 0); + EnableMenuCommand (NULL, kHICommandPreferences); + err = GetIndMenuItemWithCommandID (NULL, kHICommandPreferences, 1, + &menu, &menu_index); + if (err == noErr) + { + SetMenuItemCommandKey (menu, menu_index, false, 0); + InsertMenuItemTextWithCFString (menu, NULL, + 0, kMenuItemAttrSeparator, 0); + InsertMenuItemTextWithCFString (menu, CFSTR ("About Emacs"), + 0, 0, kHICommandAbout); + } +#else /* !MAC_OSX */ +#if TARGET_API_MAC_CARBON + SetMenuItemCommandID (GetMenuRef (M_APPLE), I_ABOUT, kHICommandAbout); +#endif +#endif +} + + +/*********************************************************************** + Popup Dialog + ***********************************************************************/ + +#if TARGET_API_MAC_CARBON +#define DIALOG_BUTTON_COMMAND_ID_OFFSET 'Bt\0\0' +#define DIALOG_BUTTON_COMMAND_ID_P(id) \ + (((id) & ~0xffff) == DIALOG_BUTTON_COMMAND_ID_OFFSET) +#define DIALOG_BUTTON_COMMAND_ID_VALUE(id) \ + ((id) - DIALOG_BUTTON_COMMAND_ID_OFFSET) +#define DIALOG_BUTTON_MAKE_COMMAND_ID(value) \ + ((value) + DIALOG_BUTTON_COMMAND_ID_OFFSET) + +extern EMACS_TIME timer_check P_ ((int)); +static int quit_dialog_event_loop; + +static pascal OSStatus +mac_handle_dialog_event (next_handler, event, data) + EventHandlerCallRef next_handler; + EventRef event; + void *data; +{ + OSStatus err, result = eventNotHandledErr; + WindowRef window = (WindowRef) data; + + switch (GetEventClass (event)) + { + case kEventClassCommand: + { + HICommand command; + + err = GetEventParameter (event, kEventParamDirectObject, + typeHICommand, NULL, sizeof (HICommand), + NULL, &command); + if (err == noErr) + if (DIALOG_BUTTON_COMMAND_ID_P (command.commandID)) + { + SetWRefCon (window, command.commandID); + quit_dialog_event_loop = 1; + break; + } + + result = CallNextEventHandler (next_handler, event); + } + break; + + case kEventClassKeyboard: + { + OSStatus result; + char char_code; + + result = CallNextEventHandler (next_handler, event); + if (result != eventNotHandledErr) + break; + + err = GetEventParameter (event, kEventParamKeyMacCharCodes, + typeChar, NULL, sizeof (char), + NULL, &char_code); + if (err == noErr) + switch (char_code) + { + case kEscapeCharCode: + quit_dialog_event_loop = 1; + break; + + default: + { + UInt32 modifiers, key_code; + + err = GetEventParameter (event, kEventParamKeyModifiers, + typeUInt32, NULL, sizeof (UInt32), + NULL, &modifiers); + if (err == noErr) + err = GetEventParameter (event, kEventParamKeyCode, + typeUInt32, NULL, sizeof (UInt32), + NULL, &key_code); + if (err == noErr) + if (mac_quit_char_key_p (modifiers, key_code)) + quit_dialog_event_loop = 1; + } + break; + } + } + break; + + default: + abort (); + } + + if (quit_dialog_event_loop) + { + err = QuitEventLoop (GetCurrentEventLoop ()); + if (err == noErr) + result = noErr; + } + + return result; +} + +static OSStatus +install_dialog_event_handler (window) + WindowRef window; +{ + static const EventTypeSpec specs[] = + {{kEventClassCommand, kEventCommandProcess}, + {kEventClassKeyboard, kEventRawKeyDown}}; + static EventHandlerUPP handle_dialog_eventUPP = NULL; + + if (handle_dialog_eventUPP == NULL) + handle_dialog_eventUPP = NewEventHandlerUPP (mac_handle_dialog_event); + return InstallWindowEventHandler (window, handle_dialog_eventUPP, + GetEventTypeCount (specs), specs, + window, NULL); +} + +static Lisp_Object +pop_down_dialog (arg) + Lisp_Object arg; +{ + struct Lisp_Save_Value *p = XSAVE_VALUE (arg); + WindowRef window = p->pointer; + + BLOCK_INPUT; + + if (popup_activated_flag) + EndAppModalStateForWindow (window); + DisposeWindow (window); + popup_activated_flag = 0; + + UNBLOCK_INPUT; + + return Qnil; +} + +/* Pop up the dialog for frame F defined by FIRST_WV and loop until the + dialog pops down. + menu_item_selection will be set to the selection. */ + +void +create_and_show_dialog (f, first_wv) + FRAME_PTR f; + widget_value *first_wv; +{ + OSStatus err; + char *dialog_name, *message; + int nb_buttons, first_group_count, i, result = 0; + widget_value *wv; + short buttons_height, text_height, inner_width, inner_height; + Rect empty_rect, *rects; + WindowRef window = NULL; + ControlRef *buttons, default_button = NULL, text; + int specpdl_count = SPECPDL_INDEX (); + + dialog_name = first_wv->name; + nb_buttons = dialog_name[1] - '0'; + first_group_count = nb_buttons - (dialog_name[4] - '0'); + + wv = first_wv->contents; + message = wv->value; + + wv = wv->next; + SetRect (&empty_rect, 0, 0, 0, 0); + + /* Create dialog window. */ + err = CreateNewWindow (kMovableModalWindowClass, + kWindowStandardHandlerAttribute, + &empty_rect, &window); + if (err == noErr) + { + record_unwind_protect (pop_down_dialog, make_save_value (window, 0)); + err = SetThemeWindowBackground (window, kThemeBrushMovableModalBackground, + true); + } + if (err == noErr) + err = SetWindowTitleWithCFString (window, (dialog_name[0] == 'Q' + ? CFSTR ("Question") + : CFSTR ("Information"))); + + /* Create button controls and measure their optimal bounds. */ + if (err == noErr) + { + buttons = alloca (sizeof (ControlRef) * nb_buttons); + rects = alloca (sizeof (Rect) * nb_buttons); + for (i = 0; i < nb_buttons; i++) + { + CFStringRef label = cfstring_create_with_utf8_cstring (wv->value); + + if (label == NULL) + err = memFullErr; + else + { + err = CreatePushButtonControl (window, &empty_rect, + label, &buttons[i]); + CFRelease (label); + } + if (err == noErr) + { + if (!wv->enabled) + { +#ifdef MAC_OSX + err = DisableControl (buttons[i]); +#else + err = DeactivateControl (buttons[i]); +#endif + } + else if (default_button == NULL) + default_button = buttons[i]; + } + if (err == noErr) + { + SInt16 unused; + + rects[i] = empty_rect; + err = GetBestControlRect (buttons[i], &rects[i], &unused); + } + if (err == noErr) + { + UInt32 command_id; + + OffsetRect (&rects[i], -rects[i].left, -rects[i].top); + if (rects[i].right < DIALOG_BUTTON_MIN_WIDTH) + rects[i].right = DIALOG_BUTTON_MIN_WIDTH; + else if (rects[i].right > DIALOG_MAX_INNER_WIDTH) + rects[i].right = DIALOG_MAX_INNER_WIDTH; + + command_id = DIALOG_BUTTON_MAKE_COMMAND_ID ((int) wv->call_data); + err = SetControlCommandID (buttons[i], command_id); + } + if (err != noErr) + break; + wv = wv->next; + } + } + + /* Layout buttons. rects[i] is set relative to the bottom-right + corner of the inner box. */ + if (err == noErr) + { + short bottom, right, max_height, left_align_shift; + + inner_width = DIALOG_MIN_INNER_WIDTH; + bottom = right = max_height = 0; + for (i = 0; i < nb_buttons; i++) + { + if (right - rects[i].right < - inner_width) + { + if (i != first_group_count + && right - rects[i].right >= - DIALOG_MAX_INNER_WIDTH) + inner_width = - (right - rects[i].right); + else + { + bottom -= max_height + DIALOG_BUTTON_BUTTON_VERTICAL_SPACE; + right = max_height = 0; + } + } + if (max_height < rects[i].bottom) + max_height = rects[i].bottom; + OffsetRect (&rects[i], right - rects[i].right, + bottom - rects[i].bottom); + right = rects[i].left - DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE; + if (i == first_group_count - 1) + right -= DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE; + } + buttons_height = - (bottom - max_height); + + left_align_shift = - (inner_width + rects[nb_buttons - 1].left); + for (i = nb_buttons - 1; i >= first_group_count; i--) + { + if (bottom != rects[i].bottom) + { + left_align_shift = - (inner_width + rects[i].left); + bottom = rects[i].bottom; + } + OffsetRect (&rects[i], left_align_shift, 0); + } + } + + /* Create a static text control and measure its bounds. */ + if (err == noErr) + { + CFStringRef message_string; + Rect bounds; + + message_string = cfstring_create_with_utf8_cstring (message); + if (message_string == NULL) + err = memFullErr; + else + { + ControlFontStyleRec text_style; + + text_style.flags = 0; + SetRect (&bounds, 0, 0, inner_width, 0); + err = CreateStaticTextControl (window, &bounds, message_string, + &text_style, &text); + CFRelease (message_string); + } + if (err == noErr) + { + SInt16 unused; + + bounds = empty_rect; + err = GetBestControlRect (text, &bounds, &unused); + } + if (err == noErr) + { + text_height = bounds.bottom - bounds.top; + if (text_height < DIALOG_TEXT_MIN_HEIGHT) + text_height = DIALOG_TEXT_MIN_HEIGHT; + } + } + + /* Place buttons. */ + if (err == noErr) + { + inner_height = (text_height + DIALOG_TEXT_BUTTONS_VERTICAL_SPACE + + buttons_height); + + for (i = 0; i < nb_buttons; i++) + { + OffsetRect (&rects[i], DIALOG_LEFT_MARGIN + inner_width, + DIALOG_TOP_MARGIN + inner_height); + SetControlBounds (buttons[i], &rects[i]); + } + } + + /* Place text. */ + if (err == noErr) + { + Rect bounds; + + SetRect (&bounds, DIALOG_LEFT_MARGIN, DIALOG_TOP_MARGIN, + DIALOG_LEFT_MARGIN + inner_width, + DIALOG_TOP_MARGIN + text_height); + SetControlBounds (text, &bounds); + } + + /* Create the application icon at the upper-left corner. */ + if (err == noErr) + { + ControlButtonContentInfo content; + ControlRef icon; + static const ProcessSerialNumber psn = {0, kCurrentProcess}; +#ifdef MAC_OSX + FSRef app_location; +#else + ProcessInfoRec pinfo; + FSSpec app_spec; +#endif + SInt16 unused; + + content.contentType = kControlContentIconRef; +#ifdef MAC_OSX + err = GetProcessBundleLocation (&psn, &app_location); + if (err == noErr) + err = GetIconRefFromFileInfo (&app_location, 0, NULL, 0, NULL, + kIconServicesNormalUsageFlag, + &content.u.iconRef, &unused); +#else + bzero (&pinfo, sizeof (ProcessInfoRec)); + pinfo.processInfoLength = sizeof (ProcessInfoRec); + pinfo.processAppSpec = &app_spec; + err = GetProcessInformation (&psn, &pinfo); + if (err == noErr) + err = GetIconRefFromFile (&app_spec, &content.u.iconRef, &unused); +#endif + if (err == noErr) + { + Rect bounds; + + SetRect (&bounds, DIALOG_ICON_LEFT_MARGIN, DIALOG_ICON_TOP_MARGIN, + DIALOG_ICON_LEFT_MARGIN + DIALOG_ICON_WIDTH, + DIALOG_ICON_TOP_MARGIN + DIALOG_ICON_HEIGHT); + err = CreateIconControl (window, &bounds, &content, true, &icon); + ReleaseIconRef (content.u.iconRef); + } + } + + /* Show the dialog window and run event loop. */ + if (err == noErr) + if (default_button) + err = SetWindowDefaultButton (window, default_button); + if (err == noErr) + err = install_dialog_event_handler (window); + if (err == noErr) + { + SizeWindow (window, + DIALOG_LEFT_MARGIN + inner_width + DIALOG_RIGHT_MARGIN, + DIALOG_TOP_MARGIN + inner_height + DIALOG_BOTTOM_MARGIN, + true); + err = RepositionWindow (window, FRAME_MAC_WINDOW (f), + kWindowAlertPositionOnParentWindow); + } + if (err == noErr) + { + SetWRefCon (window, 0); + ShowWindow (window); + BringToFront (window); + popup_activated_flag = 1; + err = BeginAppModalStateForWindow (window); + } + if (err == noErr) + { + EventTargetRef toolbox_dispatcher = GetEventDispatcherTarget (); + + quit_dialog_event_loop = 0; + while (1) + { + EMACS_TIME next_time = timer_check (1); + long secs = EMACS_SECS (next_time); + long usecs = EMACS_USECS (next_time); + EventTimeout timeout; + EventRef event; + + if (secs < 0 || (secs == 0 && usecs == 0)) + { + /* Sometimes timer_check returns -1 (no timers) even if + there are timers. So do a timeout anyway. */ + secs = 1; + usecs = 0; + } + + timeout = (secs * kEventDurationSecond + + usecs * kEventDurationMicrosecond); + err = ReceiveNextEvent (0, NULL, timeout, kEventRemoveFromQueue, + &event); + if (err == noErr) + { + SendEventToEventTarget (event, toolbox_dispatcher); + ReleaseEvent (event); + } +#if 0 /* defined (MAC_OSX) */ + else if (err != eventLoopTimedOutErr) + { + if (err == eventLoopQuitErr) + err = noErr; + break; + } +#else + /* The return value of ReceiveNextEvent seems to be + unreliable. Use our own global variable instead. */ + if (quit_dialog_event_loop) + { + err = noErr; + break; + } +#endif + } + } + if (err == noErr) + { + UInt32 command_id = GetWRefCon (window); + + if (DIALOG_BUTTON_COMMAND_ID_P (command_id)) + result = DIALOG_BUTTON_COMMAND_ID_VALUE (command_id); + } + + unbind_to (specpdl_count, Qnil); + + menu_item_selection = result; +} +#else /* not TARGET_API_MAC_CARBON */ +#define DIALOG_WINDOW_RESOURCE 130 + +int +mac_dialog (widget_value *wv) +{ + char *dialog_name; + char *prompt; + char **button_labels; + UInt32 *ref_cons; + int nb_buttons; + int left_count; + int i; + int dialog_width; + Rect rect; + WindowRef window_ptr; + ControlRef ch; + int left; + EventRecord event_record; + SInt16 part_code; + int control_part_code; + Point mouse; + + dialog_name = wv->name; + nb_buttons = dialog_name[1] - '0'; + left_count = nb_buttons - (dialog_name[4] - '0'); + button_labels = (char **) alloca (sizeof (char *) * nb_buttons); + ref_cons = (UInt32 *) alloca (sizeof (UInt32) * nb_buttons); + + wv = wv->contents; + prompt = (char *) alloca (strlen (wv->value) + 1); + strcpy (prompt, wv->value); + c2pstr (prompt); + + wv = wv->next; + for (i = 0; i < nb_buttons; i++) + { + button_labels[i] = wv->value; + button_labels[i] = (char *) alloca (strlen (wv->value) + 1); + strcpy (button_labels[i], wv->value); + c2pstr (button_labels[i]); + ref_cons[i] = (UInt32) wv->call_data; + wv = wv->next; + } + + window_ptr = GetNewCWindow (DIALOG_WINDOW_RESOURCE, NULL, (WindowRef) -1); + + SetPortWindowPort (window_ptr); + + TextFont (0); + /* Left and right margins in the dialog are 13 pixels each.*/ + dialog_width = 14; + /* Calculate width of dialog box: 8 pixels on each side of the text + label in each button, 12 pixels between buttons. */ + for (i = 0; i < nb_buttons; i++) + dialog_width += StringWidth (button_labels[i]) + 16 + 12; + + if (left_count != 0 && nb_buttons - left_count != 0) + dialog_width += 12; + + dialog_width = max (dialog_width, StringWidth (prompt) + 26); + + SizeWindow (window_ptr, dialog_width, 78, 0); + ShowWindow (window_ptr); + + SetPortWindowPort (window_ptr); + + TextFont (0); + + MoveTo (13, 29); + DrawString (prompt); + + left = 13; + for (i = 0; i < nb_buttons; i++) + { + int button_width = StringWidth (button_labels[i]) + 16; + SetRect (&rect, left, 45, left + button_width, 65); + ch = NewControl (window_ptr, &rect, button_labels[i], 1, 0, 0, 0, + kControlPushButtonProc, ref_cons[i]); + left += button_width + 12; + if (i == left_count - 1) + left += 12; + } + + i = 0; + while (!i) + { + if (WaitNextEvent (mDownMask, &event_record, 10, NULL)) + if (event_record.what == mouseDown) + { + part_code = FindWindow (event_record.where, &window_ptr); + if (part_code == inContent) + { + mouse = event_record.where; + GlobalToLocal (&mouse); + control_part_code = FindControl (mouse, window_ptr, &ch); + if (control_part_code == kControlButtonPart) + if (TrackControl (ch, mouse, NULL)) + i = GetControlReference (ch); + } + } + } + + DisposeWindow (window_ptr); + + return i; +} +#endif /* not TARGET_API_MAC_CARBON */ + + +/*********************************************************************** + Selection support +***********************************************************************/ + +#if !TARGET_API_MAC_CARBON +#include +#include +#endif + +extern Lisp_Object Vselection_converter_alist; +extern Lisp_Object Qmac_scrap_name, Qmac_ostype; + +static ScrapFlavorType get_flavor_type_from_symbol P_ ((Lisp_Object, + Selection)); + +/* Get a reference to the selection corresponding to the symbol SYM. + The reference is set to *SEL, and it becomes NULL if there's no + corresponding selection. Clear the selection if CLEAR_P is + non-zero. */ + +OSStatus +mac_get_selection_from_symbol (sym, clear_p, sel) + Lisp_Object sym; + int clear_p; + Selection *sel; +{ + OSStatus err = noErr; + Lisp_Object str = Fget (sym, Qmac_scrap_name); + + if (!STRINGP (str)) + *sel = NULL; + else + { +#if TARGET_API_MAC_CARBON +#ifdef MAC_OSX + CFStringRef scrap_name = cfstring_create_with_string (str); + OptionBits options = (clear_p ? kScrapClearNamedScrap + : kScrapGetNamedScrap); + + err = GetScrapByName (scrap_name, options, sel); + CFRelease (scrap_name); +#else /* !MAC_OSX */ + if (clear_p) + err = ClearCurrentScrap (); + if (err == noErr) + err = GetCurrentScrap (sel); +#endif /* !MAC_OSX */ +#else /* !TARGET_API_MAC_CARBON */ + if (clear_p) + err = ZeroScrap (); + if (err == noErr) + *sel = 1; +#endif /* !TARGET_API_MAC_CARBON */ + } + + return err; +} + +/* Get a scrap flavor type from the symbol SYM. Return 0 if no + corresponding flavor type. If SEL is non-zero, the return value is + non-zero only when the SEL has the flavor type. */ + +static ScrapFlavorType +get_flavor_type_from_symbol (sym, sel) + Lisp_Object sym; + Selection sel; +{ + Lisp_Object str = Fget (sym, Qmac_ostype); + ScrapFlavorType flavor_type; + + if (STRINGP (str) && SBYTES (str) == 4) + flavor_type = EndianU32_BtoN (*((UInt32 *) SDATA (str))); + else + flavor_type = 0; + + if (flavor_type && sel) + { +#if TARGET_API_MAC_CARBON + OSStatus err; + ScrapFlavorFlags flags; + + err = GetScrapFlavorFlags (sel, flavor_type, &flags); + if (err != noErr) + flavor_type = 0; +#else /* !TARGET_API_MAC_CARBON */ + SInt32 size, offset; + + size = GetScrap (NULL, flavor_type, &offset); + if (size < 0) + flavor_type = 0; +#endif /* !TARGET_API_MAC_CARBON */ + } + + return flavor_type; +} + +/* Check if the symbol SYM has a corresponding selection target type. */ + +int +mac_valid_selection_target_p (sym) + Lisp_Object sym; +{ + return get_flavor_type_from_symbol (sym, 0) != 0; +} + +/* Clear the selection whose reference is *SEL. */ + +OSStatus +mac_clear_selection (sel) + Selection *sel; +{ +#if TARGET_API_MAC_CARBON +#ifdef MAC_OSX + return ClearScrap (sel); +#else + OSStatus err; + + err = ClearCurrentScrap (); + if (err == noErr) + err = GetCurrentScrap (sel); + return err; +#endif +#else /* !TARGET_API_MAC_CARBON */ + return ZeroScrap (); +#endif /* !TARGET_API_MAC_CARBON */ +} + +/* Get ownership information for SEL. Emacs can detect a change of + the ownership by comparing saved and current values of the + ownership information. */ + +Lisp_Object +mac_get_selection_ownership_info (sel) + Selection sel; +{ +#if TARGET_API_MAC_CARBON + return long_to_cons ((unsigned long) sel); +#else /* !TARGET_API_MAC_CARBON */ + ScrapStuffPtr scrap_info = InfoScrap (); + + return make_number (scrap_info->scrapCount); +#endif /* !TARGET_API_MAC_CARBON */ +} + +/* Return non-zero if VALUE is a valid selection value for TARGET. */ + +int +mac_valid_selection_value_p (value, target) + Lisp_Object value, target; +{ + return STRINGP (value); +} + +/* Put Lisp object VALUE to the selection SEL. The target type is + specified by TARGET. */ + +OSStatus +mac_put_selection_value (sel, target, value) + Selection sel; + Lisp_Object target, value; +{ + ScrapFlavorType flavor_type = get_flavor_type_from_symbol (target, 0); + + if (flavor_type == 0 || !STRINGP (value)) + return noTypeErr; + +#if TARGET_API_MAC_CARBON + return PutScrapFlavor (sel, flavor_type, kScrapFlavorMaskNone, + SBYTES (value), SDATA (value)); +#else /* !TARGET_API_MAC_CARBON */ + return PutScrap (SBYTES (value), flavor_type, SDATA (value)); +#endif /* !TARGET_API_MAC_CARBON */ +} + +/* Check if data for the target type TARGET is available in SEL. */ + +int +mac_selection_has_target_p (sel, target) + Selection sel; + Lisp_Object target; +{ + return get_flavor_type_from_symbol (target, sel) != 0; +} + +/* Get data for the target type TARGET from SEL and create a Lisp + string. Return nil if failed to get data. */ + +Lisp_Object +mac_get_selection_value (sel, target) + Selection sel; + Lisp_Object target; +{ + OSStatus err; + Lisp_Object result = Qnil; + ScrapFlavorType flavor_type = get_flavor_type_from_symbol (target, sel); +#if TARGET_API_MAC_CARBON + Size size; + + if (flavor_type) + { + err = GetScrapFlavorSize (sel, flavor_type, &size); + if (err == noErr) + { + do + { + result = make_uninit_string (size); + err = GetScrapFlavorData (sel, flavor_type, + &size, SDATA (result)); + if (err != noErr) + result = Qnil; + else if (size < SBYTES (result)) + result = make_unibyte_string (SDATA (result), size); + } + while (STRINGP (result) && size > SBYTES (result)); + } + } +#else + Handle handle; + SInt32 size, offset; + + if (flavor_type) + size = GetScrap (NULL, flavor_type, &offset); + if (size >= 0) + { + handle = NewHandle (size); + HLock (handle); + size = GetScrap (handle, flavor_type, &offset); + if (size >= 0) + result = make_unibyte_string (*handle, size); + DisposeHandle (handle); + } +#endif + + return result; +} + +/* Get the list of target types in SEL. The return value is a list of + target type symbols possibly followed by scrap flavor type + strings. */ + +Lisp_Object +mac_get_selection_target_list (sel) + Selection sel; +{ + Lisp_Object result = Qnil, rest, target; +#if TARGET_API_MAC_CARBON + OSStatus err; + UInt32 count, i, type; + ScrapFlavorInfo *flavor_info = NULL; + Lisp_Object strings = Qnil; + + err = GetScrapFlavorCount (sel, &count); + if (err == noErr) + flavor_info = xmalloc (sizeof (ScrapFlavorInfo) * count); + err = GetScrapFlavorInfoList (sel, &count, flavor_info); + if (err != noErr) + { + xfree (flavor_info); + flavor_info = NULL; + } + if (flavor_info == NULL) + count = 0; +#endif + for (rest = Vselection_converter_alist; CONSP (rest); rest = XCDR (rest)) + { + ScrapFlavorType flavor_type = 0; + + if (CONSP (XCAR (rest)) + && (target = XCAR (XCAR (rest)), + SYMBOLP (target)) + && (flavor_type = get_flavor_type_from_symbol (target, sel))) + { + result = Fcons (target, result); +#if TARGET_API_MAC_CARBON + for (i = 0; i < count; i++) + if (flavor_info[i].flavorType == flavor_type) + { + flavor_info[i].flavorType = 0; + break; + } +#endif + } + } +#if TARGET_API_MAC_CARBON + if (flavor_info) + { + for (i = 0; i < count; i++) + if (flavor_info[i].flavorType) + { + type = EndianU32_NtoB (flavor_info[i].flavorType); + strings = Fcons (make_unibyte_string ((char *) &type, 4), strings); + } + result = nconc2 (result, strings); + xfree (flavor_info); + } +#endif + + return result; +} + + +/*********************************************************************** + Apple event support +***********************************************************************/ + +extern pascal OSErr mac_handle_apple_event P_ ((const AppleEvent *, + AppleEvent *, SInt32)); +extern void cleanup_all_suspended_apple_events P_ ((void)); + +void +init_apple_event_handler () +{ + OSErr err; + long result; + + /* Make sure we have Apple events before starting. */ + err = Gestalt (gestaltAppleEventsAttr, &result); + if (err != noErr) + abort (); + + if (!(result & (1 << gestaltAppleEventsPresent))) + abort (); + + err = AEInstallEventHandler (typeWildCard, typeWildCard, +#if TARGET_API_MAC_CARBON + NewAEEventHandlerUPP (mac_handle_apple_event), +#else + NewAEEventHandlerProc (mac_handle_apple_event), +#endif + 0L, false); + if (err != noErr) + abort (); + + atexit (cleanup_all_suspended_apple_events); +} + + +/*********************************************************************** + Drag and drop support +***********************************************************************/ + +#if TARGET_API_MAC_CARBON +extern Lisp_Object Vmac_dnd_known_types; + +static pascal OSErr mac_do_track_drag P_ ((DragTrackingMessage, WindowRef, + void *, DragRef)); +static pascal OSErr mac_do_receive_drag P_ ((WindowRef, void *, DragRef)); +static DragTrackingHandlerUPP mac_do_track_dragUPP = NULL; +static DragReceiveHandlerUPP mac_do_receive_dragUPP = NULL; + +static void +mac_store_drag_event (window, mouse_pos, modifiers, desc) + WindowRef window; + Point mouse_pos; + SInt16 modifiers; + const AEDesc *desc; +{ + struct input_event buf; + + EVENT_INIT (buf); + + buf.kind = DRAG_N_DROP_EVENT; + buf.modifiers = mac_to_emacs_modifiers (modifiers, 0); + buf.timestamp = TickCount () * (1000 / 60); + XSETINT (buf.x, mouse_pos.h); + XSETINT (buf.y, mouse_pos.v); + XSETFRAME (buf.frame_or_window, mac_window_to_frame (window)); + buf.arg = mac_aedesc_to_lisp (desc); + kbd_buffer_store_event (&buf); +} + +static pascal OSErr +mac_do_track_drag (message, window, refcon, drag) + DragTrackingMessage message; + WindowRef window; + void *refcon; + DragRef drag; +{ + OSErr err = noErr; + static int can_accept; + UInt16 num_items, index; + + if (GetFrontWindowOfClass (kMovableModalWindowClass, false)) + return dragNotAcceptedErr; + + switch (message) + { + case kDragTrackingEnterHandler: + err = CountDragItems (drag, &num_items); + if (err != noErr) + break; + can_accept = 0; + for (index = 1; index <= num_items; index++) + { + ItemReference item; + FlavorFlags flags; + Lisp_Object rest; + + err = GetDragItemReferenceNumber (drag, index, &item); + if (err != noErr) + continue; + for (rest = Vmac_dnd_known_types; CONSP (rest); rest = XCDR (rest)) + { + Lisp_Object str; + FlavorType type; + + str = XCAR (rest); + if (!(STRINGP (str) && SBYTES (str) == 4)) + continue; + type = EndianU32_BtoN (*((UInt32 *) SDATA (str))); + + err = GetFlavorFlags (drag, item, type, &flags); + if (err == noErr) + { + can_accept = 1; + break; + } + } + } + break; + + case kDragTrackingEnterWindow: + if (can_accept) + { + RgnHandle hilite_rgn = NewRgn (); + + if (hilite_rgn) + { + Rect r; + + GetWindowPortBounds (window, &r); + OffsetRect (&r, -r.left, -r.top); + RectRgn (hilite_rgn, &r); + ShowDragHilite (drag, hilite_rgn, true); + DisposeRgn (hilite_rgn); + } + SetThemeCursor (kThemeCopyArrowCursor); + } + break; + + case kDragTrackingInWindow: + break; + + case kDragTrackingLeaveWindow: + if (can_accept) + { + HideDragHilite (drag); + SetThemeCursor (kThemeArrowCursor); + } + break; + + case kDragTrackingLeaveHandler: + break; + } + + if (err != noErr) + return dragNotAcceptedErr; + return noErr; +} + +static pascal OSErr +mac_do_receive_drag (window, refcon, drag) + WindowRef window; + void *refcon; + DragRef drag; +{ + OSErr err; + int num_types, i; + Lisp_Object rest, str; + FlavorType *types; + AppleEvent apple_event; + Point mouse_pos; + SInt16 modifiers; + + if (GetFrontWindowOfClass (kMovableModalWindowClass, false)) + return dragNotAcceptedErr; + + num_types = 0; + for (rest = Vmac_dnd_known_types; CONSP (rest); rest = XCDR (rest)) + { + str = XCAR (rest); + if (STRINGP (str) && SBYTES (str) == 4) + num_types++; + } + + types = xmalloc (sizeof (FlavorType) * num_types); + i = 0; + for (rest = Vmac_dnd_known_types; CONSP (rest); rest = XCDR (rest)) + { + str = XCAR (rest); + if (STRINGP (str) && SBYTES (str) == 4) + types[i++] = EndianU32_BtoN (*((UInt32 *) SDATA (str))); + } + + err = create_apple_event_from_drag_ref (drag, num_types, types, + &apple_event); + xfree (types); + + if (err == noErr) + err = GetDragMouse (drag, &mouse_pos, NULL); + if (err == noErr) + { + GlobalToLocal (&mouse_pos); + err = GetDragModifiers (drag, NULL, NULL, &modifiers); + } + if (err == noErr) + { + UInt32 key_modifiers = modifiers; + + err = AEPutParamPtr (&apple_event, kEventParamKeyModifiers, + typeUInt32, &key_modifiers, sizeof (UInt32)); + } + + if (err == noErr) + { + mac_store_drag_event (window, mouse_pos, 0, &apple_event); + AEDisposeDesc (&apple_event); + mac_wakeup_from_rne (); + return noErr; + } + else + return dragNotAcceptedErr; +} +#endif /* TARGET_API_MAC_CARBON */ + +static OSErr +install_drag_handler (window) + WindowRef window; +{ + OSErr err = noErr; + +#if TARGET_API_MAC_CARBON + if (mac_do_track_dragUPP == NULL) + mac_do_track_dragUPP = NewDragTrackingHandlerUPP (mac_do_track_drag); + if (mac_do_receive_dragUPP == NULL) + mac_do_receive_dragUPP = NewDragReceiveHandlerUPP (mac_do_receive_drag); + + err = InstallTrackingHandler (mac_do_track_dragUPP, window, NULL); + if (err == noErr) + err = InstallReceiveHandler (mac_do_receive_dragUPP, window, NULL); +#endif + + return err; +} + +static void +remove_drag_handler (window) + WindowRef window; +{ +#if TARGET_API_MAC_CARBON + if (mac_do_track_dragUPP) + RemoveTrackingHandler (mac_do_track_dragUPP, window); + if (mac_do_receive_dragUPP) + RemoveReceiveHandler (mac_do_receive_dragUPP, window); +#endif +} + +#if TARGET_API_MAC_CARBON +/* Return default value for mac-dnd-known-types. */ + +Lisp_Object +mac_dnd_default_known_types () +{ + Lisp_Object result = list4 (build_string ("hfs "), build_string ("utxt"), + build_string ("TEXT"), build_string ("TIFF")); + +#ifdef MAC_OSX + result = Fcons (build_string ("furl"), result); +#endif + + return result; +} +#endif + + +/*********************************************************************** + Services menu support +***********************************************************************/ + +#ifdef MAC_OSX +extern Lisp_Object Qservice, Qpaste, Qperform; +extern Lisp_Object Vmac_service_selection; + +static OSStatus +mac_store_service_event (event) + EventRef event; +{ + OSStatus err; + Lisp_Object id_key; + int num_params; + const EventParamName *names; + const EventParamType *types; + static const EventParamName names_pfm[] = + {kEventParamServiceMessageName, kEventParamServiceUserData}; + static const EventParamType types_pfm[] = + {typeCFStringRef, typeCFStringRef}; + + switch (GetEventKind (event)) + { + case kEventServicePaste: + id_key = Qpaste; + num_params = 0; + names = NULL; + types = NULL; + break; + + case kEventServicePerform: + id_key = Qperform; + num_params = sizeof (names_pfm) / sizeof (names_pfm[0]); + names = names_pfm; + types = types_pfm; + break; + + default: + abort (); + } + + err = mac_store_event_ref_as_apple_event (0, 0, Qservice, id_key, + event, num_params, + names, types); + + return err; +} + +static OSStatus +copy_scrap_flavor_data (from_scrap, to_scrap, flavor_type) + ScrapRef from_scrap, to_scrap; + ScrapFlavorType flavor_type; +{ + OSStatus err; + Size size, size_allocated; + char *buf = NULL; + + err = GetScrapFlavorSize (from_scrap, flavor_type, &size); + if (err == noErr) + buf = xmalloc (size); + while (buf) + { + size_allocated = size; + err = GetScrapFlavorData (from_scrap, flavor_type, &size, buf); + if (err != noErr) + { + xfree (buf); + buf = NULL; + } + else if (size_allocated < size) + buf = xrealloc (buf, size); + else + break; + } + if (err == noErr) + { + if (buf == NULL) + err = memFullErr; + else + { + err = PutScrapFlavor (to_scrap, flavor_type, kScrapFlavorMaskNone, + size, buf); + xfree (buf); + } + } + + return err; +} + +static OSStatus +mac_handle_service_event (call_ref, event, data) + EventHandlerCallRef call_ref; + EventRef event; + void *data; +{ + OSStatus err = noErr; + ScrapRef cur_scrap, specific_scrap; + UInt32 event_kind = GetEventKind (event); + CFMutableArrayRef copy_types, paste_types; + CFStringRef type; + Lisp_Object rest; + ScrapFlavorType flavor_type; + + /* Check if Vmac_service_selection is a valid selection that has a + corresponding scrap. */ + if (!SYMBOLP (Vmac_service_selection)) + err = eventNotHandledErr; + else + err = mac_get_selection_from_symbol (Vmac_service_selection, 0, &cur_scrap); + if (!(err == noErr && cur_scrap)) + return eventNotHandledErr; + + switch (event_kind) + { + case kEventServiceGetTypes: + /* Set paste types. */ + err = GetEventParameter (event, kEventParamServicePasteTypes, + typeCFMutableArrayRef, NULL, + sizeof (CFMutableArrayRef), NULL, + &paste_types); + if (err != noErr) + break; + + for (rest = Vselection_converter_alist; CONSP (rest); + rest = XCDR (rest)) + if (CONSP (XCAR (rest)) && SYMBOLP (XCAR (XCAR (rest))) + && (flavor_type = + get_flavor_type_from_symbol (XCAR (XCAR (rest)), 0))) + { + type = CreateTypeStringWithOSType (flavor_type); + if (type) + { + CFArrayAppendValue (paste_types, type); + CFRelease (type); + } + } + + /* Set copy types. */ + err = GetEventParameter (event, kEventParamServiceCopyTypes, + typeCFMutableArrayRef, NULL, + sizeof (CFMutableArrayRef), NULL, + ©_types); + if (err != noErr) + break; + + if (NILP (Fx_selection_owner_p (Vmac_service_selection))) + break; + else + goto copy_all_flavors; + + case kEventServiceCopy: + err = GetEventParameter (event, kEventParamScrapRef, + typeScrapRef, NULL, + sizeof (ScrapRef), NULL, &specific_scrap); + if (err != noErr + || NILP (Fx_selection_owner_p (Vmac_service_selection))) + { + err = eventNotHandledErr; + break; + } + + copy_all_flavors: + { + UInt32 count, i; + ScrapFlavorInfo *flavor_info = NULL; + ScrapFlavorFlags flags; + + err = GetScrapFlavorCount (cur_scrap, &count); + if (err == noErr) + flavor_info = xmalloc (sizeof (ScrapFlavorInfo) * count); + err = GetScrapFlavorInfoList (cur_scrap, &count, flavor_info); + if (err != noErr) + { + xfree (flavor_info); + flavor_info = NULL; + } + if (flavor_info == NULL) + break; + + for (i = 0; i < count; i++) + { + flavor_type = flavor_info[i].flavorType; + err = GetScrapFlavorFlags (cur_scrap, flavor_type, &flags); + if (err == noErr && !(flags & kScrapFlavorMaskSenderOnly)) + { + if (event_kind == kEventServiceCopy) + err = copy_scrap_flavor_data (cur_scrap, specific_scrap, + flavor_type); + else /* event_kind == kEventServiceGetTypes */ + { + type = CreateTypeStringWithOSType (flavor_type); + if (type) + { + CFArrayAppendValue (copy_types, type); + CFRelease (type); + } + } + } + } + xfree (flavor_info); + } + break; + + case kEventServicePaste: + case kEventServicePerform: + { + int data_exists_p = 0; + + err = GetEventParameter (event, kEventParamScrapRef, typeScrapRef, + NULL, sizeof (ScrapRef), NULL, + &specific_scrap); + if (err == noErr) + err = mac_clear_selection (&cur_scrap); + if (err == noErr) + for (rest = Vselection_converter_alist; CONSP (rest); + rest = XCDR (rest)) + { + if (! (CONSP (XCAR (rest)) && SYMBOLP (XCAR (XCAR (rest))))) + continue; + flavor_type = get_flavor_type_from_symbol (XCAR (XCAR (rest)), + specific_scrap); + if (flavor_type == 0) + continue; + err = copy_scrap_flavor_data (specific_scrap, cur_scrap, + flavor_type); + if (err == noErr) + data_exists_p = 1; + } + if (!data_exists_p) + err = eventNotHandledErr; + else + err = mac_store_service_event (event); + } + break; + } + + if (err != noErr) + err = eventNotHandledErr; + return err; +} + +static OSStatus +install_service_handler () +{ + static const EventTypeSpec specs[] = + {{kEventClassService, kEventServiceGetTypes}, + {kEventClassService, kEventServiceCopy}, + {kEventClassService, kEventServicePaste}, + {kEventClassService, kEventServicePerform}}; + + return InstallApplicationEventHandler (NewEventHandlerUPP + (mac_handle_service_event), + GetEventTypeCount (specs), + specs, NULL, NULL); +} +#endif /* MAC_OSX */ + + +/*********************************************************************** + Initialization + ***********************************************************************/ + +void +mac_toolbox_initialize () +{ + any_help_event_p = 0; + + init_menu_bar (); + +#ifdef MAC_OSX + init_apple_event_handler (); +#endif +#if USE_MAC_TSM + init_tsm (); +#endif +} -- cgit v1.2.1 From f4f1eb1002f4d49d64cf2452d55b120a71526dc7 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 01:57:25 +0000 Subject: (MAC_OBJ): Add mactoolbox.o. (mactoolbox.o): New target. --- src/Makefile.in | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Makefile.in b/src/Makefile.in index 74ab05bec9c..97c630e24c0 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -575,7 +575,7 @@ CYGWIN_OBJ = sheap.o #ifdef HAVE_CARBON mac = $(dot)$(dot)/mac/ -MAC_OBJ = mac.o macterm.o macfns.o macmenu.o macselect.o fontset.o fringe.o image.o +MAC_OBJ = mac.o macterm.o macfns.o macmenu.o macselect.o fontset.o fringe.o image.o mactoolbox.o emacsapp = $(PWD)/$(mac)Emacs.app/ emacsappsrc = ${srcdir}/../mac/Emacs.app/ #endif @@ -1287,6 +1287,9 @@ macterm.o: blockinput.h atimer.h systime.h syssignal.h macterm.h macgui.h \ process.h coding.h $(config_h) macselect.o: blockinput.h atimer.h systime.h macterm.h macgui.h frame.h \ keymap.h $(config_h) +mactoolbox.o: blockinput.h atimer.h systime.h macterm.h macgui.h frame.h \ + charset.h coding.h ccl.h dispextern.h fontset.h termhooks.h buffer.h \ + window.h keyboard.h $(config_h) ${emacsapp}Contents/Resources/English.lproj: mkdir -p $@ -- cgit v1.2.1 From cc69f23aedadf9cc6463cc47b431910694467a99 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 01:57:37 +0000 Subject: [MAC_OSX] (select_and_poll_event, sys_select): Use mac_run_loop_run_once instead of CFRunLoopRunInMode. --- src/mac.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/mac.c b/src/mac.c index 74cfeb24865..0ae233f048e 100644 --- a/src/mac.c +++ b/src/mac.c @@ -4989,8 +4989,8 @@ extern int noninteractive; SELECT_TIMEOUT_THRESHOLD_RUNLOOP seconds). -> Create CFSocket for each socket and add it into the current event RunLoop so that the current event loop gets quit when - the socket becomes ready. Then CFRunLoopRunInMode can wait - for both kinds of inputs. + the socket becomes ready. Then mac_run_loop_run_once can + wait for both kinds of inputs. 4. Otherwise. -> Periodically poll the window input channel while repeatedly executing `select' with a short timeout @@ -5045,7 +5045,7 @@ select_and_poll_event (nfds, rfds, wfds, efds, timeout) if (efds) oefds = *efds; } - /* Try detect_input_pending before CFRunLoopRunInMode in the same + /* Try detect_input_pending before mac_run_loop_run_once in the same BLOCK_INPUT block, in case that some input has already been read asynchronously. */ BLOCK_INPUT; @@ -5062,15 +5062,7 @@ select_and_poll_event (nfds, rfds, wfds, efds, timeout) if (timeoutval == 0.0) timedout_p = 1; else - { -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (NULL); -#endif - if (CFRunLoopRunInMode (kCFRunLoopDefaultMode, - timeoutval >= 0 ? timeoutval : 100000, true) - == kCFRunLoopRunTimedOut) - timedout_p = 1; - } + timedout_p = mac_run_loop_run_once (timeoutval); if (timeout == NULL && timedout_p) { @@ -5193,7 +5185,7 @@ sys_select (nfds, rfds, wfds, efds, timeout) if (timeoutval > 0 && timeoutval <= SELECT_TIMEOUT_THRESHOLD_RUNLOOP) goto poll_periodically; - /* Try detect_input_pending before CFRunLoopRunInMode in the + /* Try detect_input_pending before mac_run_loop_run_once in the same BLOCK_INPUT block, in case that some input has already been read asynchronously. */ BLOCK_INPUT; @@ -5246,13 +5238,7 @@ sys_select (nfds, rfds, wfds, efds, timeout) CFRunLoopAddSource (runloop, source, kCFRunLoopDefaultMode); } -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (NULL); -#endif - if (CFRunLoopRunInMode (kCFRunLoopDefaultMode, - timeoutval >= 0 ? timeoutval : 100000, true) - == kCFRunLoopRunTimedOut) - timedout_p = 1; + timedout_p = mac_run_loop_run_once (timeoutval); for (fd = minfd; fd < nfds; fd++) if (FD_ISSET (fd, rfds) || (wfds && FD_ISSET (fd, wfds))) -- cgit v1.2.1 From 9ef833acf43ab762ead83ec8dc5fe162d4db40c0 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 01:57:47 +0000 Subject: (x_set_background_color, mac_window, x_create_tip_frame): Use mac_set_frame_window_background instead of XSetWindowBackground. (x_set_tool_bar_lines) [USE_MAC_TOOLBAR]: Use mac_is_window_toolbar_visible instead of IsWindowToolbarVisible. (x_set_name_internal) [TARGET_API_MAC_CARBON]: Use mac_set_window_title instead of SetWindowTitleWithCFString. (mac_update_proxy_icon) [TARGET_API_MAC_CARBON]: Remove BLOCK_INPUT. Move function to mactoolbox.c. (mac_update_title_bar) [TARGET_API_MAC_CARBON]: Use mac_set_window_modified instead of SetWindowModified. Add BLOCK_INPUT around mac_set_window_modified/mac_update_proxy_icon. (mac_window, x_create_tip_frame): Use mac_create_frame_window. (Fx_focus_frame): Use mac_front_non_floating_window instead of FrontNonFloatingWindow. Use mac_activate_window instead of ActivateWindow. Use mac_active_non_floating_window instead of ActiveNonFloatingWindow. (show_hourglass, hide_hourglass) [TARGET_API_MAC_CARBON]: Use mac_show_hourglass and mac_hide_hourglass. (compute_tip_xy) [TARGET_API_MAC_CARBON]: Use mac_get_global_mouse instead of GetGlobalMouse. (Fx_show_tip): Use mac_move_window/mac_size_window/mac_show_window instead of MoveWindow/SizeWindow/ShowWindow, respectively. Use mac_bring_window_to_front instead of BringToFront. (Qfile_name_history) [TARGET_API_MAC_CARBON]: Move extern to mactoolbox.c. (Fx_file_dialog) [TARGET_API_MAC_CARBON]: Move function body to mac_file_dialog in mactoolbox.c. Use mac_file_dialog. (mac_nav_event_callback) [TARGET_API_MAC_CARBON]: Move function to mactoolbox.c. --- src/macfns.c | 427 +++++------------------------------------------------------ 1 file changed, 35 insertions(+), 392 deletions(-) (limited to 'src') diff --git a/src/macfns.c b/src/macfns.c index 23c2fca7cb0..d66c56e6d84 100644 --- a/src/macfns.c +++ b/src/macfns.c @@ -1347,7 +1347,7 @@ x_set_background_color (f, arg, oldval) BLOCK_INPUT; XSetBackground (dpy, mac->normal_gc, bg); XSetForeground (dpy, mac->reverse_gc, bg); - XSetWindowBackground (dpy, FRAME_MAC_WINDOW (f), bg); + mac_set_frame_window_background (f, bg); XSetForeground (dpy, mac->cursor_gc, bg); UNBLOCK_INPUT; @@ -1687,7 +1687,8 @@ x_set_tool_bar_lines (f, value, oldval) if (nlines) { FRAME_EXTERNAL_TOOL_BAR (f) = 1; - if (FRAME_MAC_P (f) && !IsWindowToolbarVisible (FRAME_MAC_WINDOW (f))) + if (FRAME_MAC_P (f) + && !mac_is_window_toolbar_visible (FRAME_MAC_WINDOW (f))) /* Make sure next redisplay shows the tool bar. */ XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt; } @@ -1773,7 +1774,7 @@ x_set_name_internal (f, name) CFStringRef windowTitle = cfstring_create_with_utf8_cstring (SDATA (name)); - SetWindowTitleWithCFString (FRAME_MAC_WINDOW (f), windowTitle); + mac_set_window_title (FRAME_MAC_WINDOW (f), windowTitle); CFRelease (windowTitle); #else Str255 windowTitle; @@ -1938,98 +1939,6 @@ mac_set_font (f, arg, oldval) #endif } -#if TARGET_API_MAC_CARBON -static void -mac_update_proxy_icon (f) - struct frame *f; -{ - OSStatus err; - Lisp_Object file_name = - XBUFFER (XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer)->filename; - Window w = FRAME_MAC_WINDOW (f); - AliasHandle alias = NULL; - - BLOCK_INPUT; - - err = GetWindowProxyAlias (w, &alias); - if (err == errWindowDoesNotHaveProxy && !STRINGP (file_name)) - goto out; - - if (STRINGP (file_name)) - { - AEDesc desc; -#ifdef MAC_OSX - FSRef fref, fref_proxy; -#else - FSSpec fss, fss_proxy; -#endif - Boolean changed; - Lisp_Object encoded_file_name = ENCODE_FILE (file_name); - -#ifdef MAC_OSX - err = AECoercePtr (TYPE_FILE_NAME, SDATA (encoded_file_name), - SBYTES (encoded_file_name), typeFSRef, &desc); -#else - SetPortWindowPort (w); - err = AECoercePtr (TYPE_FILE_NAME, SDATA (encoded_file_name), - SBYTES (encoded_file_name), typeFSS, &desc); -#endif - if (err == noErr) - { -#ifdef MAC_OSX - err = AEGetDescData (&desc, &fref, sizeof (FSRef)); -#else - err = AEGetDescData (&desc, &fss, sizeof (FSSpec)); -#endif - AEDisposeDesc (&desc); - } - if (err == noErr) - { - if (alias) - { - /* (FS)ResolveAlias never sets `changed' to true if - `alias' is minimal. */ -#ifdef MAC_OSX - err = FSResolveAlias (NULL, alias, &fref_proxy, &changed); - if (err == noErr) - err = FSCompareFSRefs (&fref, &fref_proxy); -#else - err = ResolveAlias (NULL, alias, &fss_proxy, &changed); - if (err == noErr) - err = !(fss.vRefNum == fss_proxy.vRefNum - && fss.parID == fss_proxy.parID - && EqualString (fss.name, fss_proxy.name, - false, true)); -#endif - } - if (err != noErr || alias == NULL) - { - if (alias) - DisposeHandle ((Handle) alias); -#ifdef MAC_OSX - err = FSNewAliasMinimal (&fref, &alias); -#else - err = NewAliasMinimal (&fss, &alias); -#endif - changed = true; - } - } - if (err == noErr) - if (changed) - err = SetWindowProxyAlias (w, alias); - } - - if (alias) - DisposeHandle ((Handle) alias); - - if (err != noErr || !STRINGP (file_name)) - RemoveWindowProxy (w); - - out: - UNBLOCK_INPUT; -} -#endif - void mac_update_title_bar (f, save_match_data) struct frame *f; @@ -2051,9 +1960,11 @@ mac_update_title_bar (f, save_match_data) || (!MINI_WINDOW_P (w) && (modified_p != !NILP (w->last_had_star)))) { - SetWindowModified (FRAME_MAC_WINDOW (f), - !MINI_WINDOW_P (w) && modified_p); + BLOCK_INPUT; + mac_set_window_modified (FRAME_MAC_WINDOW (f), + !MINI_WINDOW_P (w) && modified_p); mac_update_proxy_icon (f); + UNBLOCK_INPUT; } #endif } @@ -2248,52 +2159,12 @@ static void mac_window (f) struct frame *f; { - Rect r; - BLOCK_INPUT; - SetRect (&r, f->left_pos, f->top_pos, - f->left_pos + FRAME_PIXEL_WIDTH (f), - f->top_pos + FRAME_PIXEL_HEIGHT (f)); -#if TARGET_API_MAC_CARBON - CreateNewWindow (kDocumentWindowClass, - kWindowStandardDocumentAttributes -#ifdef MAC_OSX - | kWindowToolbarButtonAttribute -#endif - , &r, &FRAME_MAC_WINDOW (f)); - if (FRAME_MAC_WINDOW (f)) - { - SetWRefCon (FRAME_MAC_WINDOW (f), (long) f->output_data.mac); - if (install_window_handler (FRAME_MAC_WINDOW (f)) != noErr) - { - DisposeWindow (FRAME_MAC_WINDOW (f)); - FRAME_MAC_WINDOW (f) = NULL; - } - } -#else /* !TARGET_API_MAC_CARBON */ - FRAME_MAC_WINDOW (f) - = NewCWindow (NULL, &r, "\p", false, zoomDocProc, - (WindowRef) -1, 1, (long) f->output_data.mac); -#endif /* !TARGET_API_MAC_CARBON */ - /* so that update events can find this mac_output struct */ - f->output_data.mac->mFP = f; /* point back to emacs frame */ - -#ifndef MAC_OSX - if (FRAME_MAC_WINDOW (f)) - { - ControlRef root_control; + mac_create_frame_window (f, 0); - if (CreateRootControl (FRAME_MAC_WINDOW (f), &root_control) != noErr) - { - DisposeWindow (FRAME_MAC_WINDOW (f)); - FRAME_MAC_WINDOW (f) = NULL; - } - } -#endif if (FRAME_MAC_WINDOW (f)) - XSetWindowBackground (FRAME_MAC_DISPLAY(f), FRAME_MAC_WINDOW (f), - FRAME_BACKGROUND_PIXEL (f)); + mac_set_frame_window_background (f, FRAME_BACKGROUND_PIXEL (f)); #if USE_MAC_TOOLBAR /* At the moment, the size of the tool bar is not yet known. We @@ -2880,7 +2751,7 @@ FRAME nil means use the selected frame. */) if (!front_p) { #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 - if (FrontNonFloatingWindow () == FRAME_MAC_WINDOW (f)) + if (mac_front_non_floating_window () == FRAME_MAC_WINDOW (f)) SetFrontProcessWithOptions (¤t_psn, kSetFrontProcessFrontWindowOnly); else @@ -2889,8 +2760,8 @@ FRAME nil means use the selected frame. */) } #ifdef MAC_OSX - ActivateWindow (ActiveNonFloatingWindow (), false); - ActivateWindow (FRAME_MAC_WINDOW (f), true); + mac_activate_window (mac_active_non_floating_window (), false); + mac_activate_window (FRAME_MAC_WINDOW (f), true); #else #if !TARGET_API_MAC_CARBON /* SelectWindow (Non-Carbon) does not issue deactivate events if the @@ -3678,26 +3549,7 @@ show_hourglass (timer) if (FRAME_LIVE_P (f) && FRAME_MAC_P (f) && FRAME_MAC_WINDOW (f) != tip_window) - { -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - if (!f->output_data.mac->hourglass_control) - { - Window w = FRAME_MAC_WINDOW (f); - Rect r; - ControlRef c; - - GetWindowPortBounds (w, &r); - r.left = r.right - HOURGLASS_WIDTH; - r.bottom = r.top + HOURGLASS_HEIGHT; - if (CreateChasingArrowsControl (w, &r, &c) == noErr) - f->output_data.mac->hourglass_control = c; - } - - if (f->output_data.mac->hourglass_control) - ShowControl (f->output_data.mac->hourglass_control); - } + mac_show_hourglass (f); } hourglass_shown_p = 1; @@ -3723,15 +3575,8 @@ hide_hourglass () { struct frame *f = XFRAME (frame); - if (FRAME_MAC_P (f) - /* Watch out for newly created frames. */ - && f->output_data.mac->hourglass_control) - { -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - HideControl (f->output_data.mac->hourglass_control); - } + if (FRAME_MAC_P (f)) + mac_hide_hourglass (f); } hourglass_shown_p = 0; @@ -3972,33 +3817,17 @@ x_create_tip_frame (dpyinfo, parms, text) window_prompting = x_figure_window_size (f, parms, 0); - { - Rect r; + BLOCK_INPUT; - BLOCK_INPUT; - SetRect (&r, 0, 0, 1, 1); -#if TARGET_API_MAC_CARBON - if (CreateNewWindow (kHelpWindowClass, -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 - kWindowIgnoreClicksAttribute | -#endif - kWindowNoUpdatesAttribute | - kWindowNoActivatesAttribute, - &r, &tip_window) == noErr) -#else - if (tip_window = NewCWindow (NULL, &r, "\p", false, plainDBox, - NULL, false, 0L)) -#endif - { - FRAME_MAC_WINDOW (f) = tip_window; - XSetWindowBackground (FRAME_MAC_DISPLAY(f), tip_window, - FRAME_BACKGROUND_PIXEL (f)); - SetWRefCon (tip_window, (long) f->output_data.mac); - /* so that update events can find this mac_output struct */ - f->output_data.mac->mFP = f; - } - UNBLOCK_INPUT; - } + mac_create_frame_window (f, 1); + + if (FRAME_MAC_WINDOW (f)) + { + mac_set_frame_window_background (f, FRAME_BACKGROUND_PIXEL (f)); + tip_window = FRAME_MAC_WINDOW (f); + } + + UNBLOCK_INPUT; x_make_gc (f); @@ -4095,7 +3924,7 @@ compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y) BLOCK_INPUT; #if TARGET_API_MAC_CARBON - GetGlobalMouse (&mouse_pos); + mac_get_global_mouse (&mouse_pos); #else GetMouse (&mouse_pos); LocalToGlobal (&mouse_pos); @@ -4219,7 +4048,7 @@ Text larger than the specified size is clipped. */) BLOCK_INPUT; compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f), &root_x, &root_y); - MoveWindow (FRAME_MAC_WINDOW (f), root_x, root_y, false); + mac_move_window (FRAME_MAC_WINDOW (f), root_x, root_y, false); UNBLOCK_INPUT; goto start_timer; } @@ -4321,10 +4150,10 @@ Text larger than the specified size is clipped. */) compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y); BLOCK_INPUT; - MoveWindow (FRAME_MAC_WINDOW (f), root_x, root_y, false); - SizeWindow (FRAME_MAC_WINDOW (f), width, height, true); - ShowWindow (FRAME_MAC_WINDOW (f)); - BringToFront (FRAME_MAC_WINDOW (f)); + mac_move_window (FRAME_MAC_WINDOW (f), root_x, root_y, false); + mac_size_window (FRAME_MAC_WINDOW (f), width, height, true); + mac_show_window (FRAME_MAC_WINDOW (f)); + mac_bring_window_to_front (FRAME_MAC_WINDOW (f)); UNBLOCK_INPUT; FRAME_PIXEL_WIDTH (f) = width; @@ -4385,25 +4214,11 @@ Value is t if tooltip was open, nil otherwise. */) -#if TARGET_API_MAC_CARBON /*********************************************************************** File selection dialog ***********************************************************************/ -static pascal void mac_nav_event_callback P_ ((NavEventCallbackMessage, - NavCBRecPtr, void *)); - -/** - There is a relatively standard way to do this using applescript to run - a (choose file) method. However, this doesn't do "the right thing" - by working only if the find-file occurred during a menu or toolbar - click. So we must do the file dialog by hand, using the navigation - manager. This also has more flexibility in determining the default - directory and whether or not we are going to choose a file. - **/ - -extern Lisp_Object Qfile_name_history; - +#if TARGET_API_MAC_CARBON DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0, doc: /* Read file name, prompting with PROMPT in directory DIR. Use a file selection dialog. @@ -4413,182 +4228,10 @@ If ONLY-DIR-P is non-nil, the user can only select directories. */) (prompt, dir, default_filename, mustmatch, only_dir_p) Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p; { - Lisp_Object file = Qnil; - int count = SPECPDL_INDEX (); - struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6; - char filename[MAXPATHLEN]; - static NavEventUPP mac_nav_event_callbackUPP = NULL; - - check_mac (); - - GCPRO6 (prompt, dir, default_filename, mustmatch, file, only_dir_p); - CHECK_STRING (prompt); - CHECK_STRING (dir); - - /* Create the dialog with PROMPT as title, using DIR as initial - directory and using "*" as pattern. */ - dir = Fexpand_file_name (dir, Qnil); - - { - OSStatus status; - NavDialogCreationOptions options; - NavDialogRef dialogRef; - NavTypeListHandle fileTypes = NULL; - NavUserAction userAction; - CFStringRef message=NULL, saveName = NULL; - - BLOCK_INPUT; - /* No need for a callback function because we are modal */ - NavGetDefaultDialogCreationOptions(&options); - options.modality = kWindowModalityAppModal; - options.location.h = options.location.v = -1; - options.optionFlags = kNavDefaultNavDlogOptions; - options.optionFlags |= kNavAllFilesInPopup; /* All files allowed */ - options.optionFlags |= kNavSelectAllReadableItem; - options.optionFlags &= ~kNavAllowMultipleFiles; - if (!NILP(prompt)) - { - message = cfstring_create_with_string (prompt); - options.message = message; - } - /* Don't set the application, let it use default. - options.clientName = CFSTR ("Emacs"); - */ - - if (mac_nav_event_callbackUPP == NULL) - mac_nav_event_callbackUPP = NewNavEventUPP (mac_nav_event_callback); - - if (!NILP (only_dir_p)) - status = NavCreateChooseFolderDialog(&options, mac_nav_event_callbackUPP, - NULL, NULL, &dialogRef); - else if (NILP (mustmatch)) - { - /* This is a save dialog */ - options.optionFlags |= kNavDontConfirmReplacement; - options.actionButtonLabel = CFSTR ("Ok"); - options.windowTitle = CFSTR ("Enter name"); - - if (STRINGP (default_filename)) - { - Lisp_Object utf8 = ENCODE_UTF_8 (default_filename); - char *begPtr = SDATA(utf8); - char *filePtr = begPtr + SBYTES(utf8); - while (filePtr != begPtr && !IS_DIRECTORY_SEP(filePtr[-1])) - filePtr--; - saveName = cfstring_create_with_utf8_cstring (filePtr); - options.saveFileName = saveName; - options.optionFlags |= kNavSelectDefaultLocation; - } - status = NavCreatePutFileDialog(&options, - 'TEXT', kNavGenericSignature, - mac_nav_event_callbackUPP, NULL, - &dialogRef); - } - else - { - /* This is an open dialog*/ - status = NavCreateChooseFileDialog(&options, fileTypes, - mac_nav_event_callbackUPP, NULL, - NULL, NULL, &dialogRef); - } - - /* Set the default location and continue*/ - if (status == noErr) - { - Lisp_Object encoded_dir = ENCODE_FILE (dir); - AEDesc defLocAed; - - status = AECreateDesc (TYPE_FILE_NAME, SDATA (encoded_dir), - SBYTES (encoded_dir), &defLocAed); - if (status == noErr) - { - NavCustomControl(dialogRef, kNavCtlSetLocation, (void*) &defLocAed); - AEDisposeDesc(&defLocAed); - } - status = NavDialogRun(dialogRef); - } - - if (saveName) CFRelease(saveName); - if (message) CFRelease(message); - - if (status == noErr) { - userAction = NavDialogGetUserAction(dialogRef); - switch (userAction) - { - case kNavUserActionNone: - case kNavUserActionCancel: - break; /* Treat cancel like C-g */ - case kNavUserActionOpen: - case kNavUserActionChoose: - case kNavUserActionSaveAs: - { - NavReplyRecord reply; - Size len; - - status = NavDialogGetReply(dialogRef, &reply); - if (status != noErr) - break; - status = AEGetNthPtr (&reply.selection, 1, TYPE_FILE_NAME, - NULL, NULL, filename, - sizeof (filename) - 1, &len); - if (status == noErr) - { - len = min (len, sizeof (filename) - 1); - filename[len] = '\0'; - if (reply.saveFileName) - { - /* If it was a saved file, we need to add the file name */ - if (len && len < sizeof (filename) - 1 - && filename[len-1] != '/') - filename[len++] = '/'; - CFStringGetCString(reply.saveFileName, filename+len, - sizeof (filename) - len, -#ifdef MAC_OSX - kCFStringEncodingUTF8 -#else - CFStringGetSystemEncoding () -#endif - ); - } - file = DECODE_FILE (make_unibyte_string (filename, - strlen (filename))); - } - NavDisposeReply(&reply); - } - break; - } - NavDialogDispose(dialogRef); - UNBLOCK_INPUT; - } - else { - UNBLOCK_INPUT; - /* Fall back on minibuffer if there was a problem */ - file = Fcompleting_read (prompt, intern ("read-file-name-internal"), - dir, mustmatch, dir, Qfile_name_history, - default_filename, Qnil); - } - } - - UNGCPRO; - - /* Make "Cancel" equivalent to C-g. */ - if (NILP (file)) - Fsignal (Qquit, Qnil); - - return unbind_to (count, file); -} - - -/* Need to register some event callback function for enabling drag and - drop in Navigation Service dialogs. */ -static pascal void -mac_nav_event_callback (selector, parms, data) - NavEventCallbackMessage selector; - NavCBRecPtr parms; - void *data ; -{ + return mac_file_dialog (prompt, dir, default_filename, mustmatch, only_dir_p); } #endif + /*********************************************************************** Fonts -- cgit v1.2.1 From f3821d294580e2e7285d8cbd28b2a2dc773e31f5 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 01:58:05 +0000 Subject: [!HAVE_CARBON]: Include Quickdraw.h instead of QuickDraw.h. (XtPointer): Move typedef from macmenu.c. (enum button_type): Move enum from macmenu.c. (widget_value): Move typedef from macmenu.c. (M_APPLE, I_ABOUT, EXTRA_STACK_ALLOC, ARGV_STRING_LIST_ID) (DIALOG_LEFT_MARGIN, DIALOG_TOP_MARGIN, DIALOG_RIGHT_MARGIN) (DIALOG_BOTTOM_MARGIN, DIALOG_MIN_INNER_WIDTH, DIALOG_MAX_INNER_WIDTH) (DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE) (DIALOG_BUTTON_BUTTON_VERTICAL_SPACE, DIALOG_BUTTON_MIN_WIDTH) (DIALOG_TEXT_MIN_HEIGHT, DIALOG_TEXT_BUTTONS_VERTICAL_SPACE) (DIALOG_ICON_WIDTH, DIALOG_ICON_HEIGHT, DIALOG_ICON_LEFT_MARGIN) (DIALOG_ICON_TOP_MARGIN): Move defines from macmenu.c. (Selection): Move typedef from macselect.c. (RAM_TOO_LARGE_ALERT_ID, ABOUT_ALERT_ID) [MAC_OS8]: Move defines from macterm.c. (mac_set_window_title, mac_set_window_modified, mac_is_window_visible) (mac_is_window_collapsed, mac_bring_window_to_front) (mac_send_window_behind, mac_hide_window, mac_show_window) (mac_collapse_window, mac_front_non_floating_window) (mac_active_non_floating_window, mac_activate_window) (mac_move_window_structure, mac_move_window, mac_size_window) (mac_get_global_mouse, mac_is_window_toolbar_visible): New defines. --- src/macgui.h | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/macgui.h b/src/macgui.h index b4c013f0dfa..1f4e454c714 100644 --- a/src/macgui.h +++ b/src/macgui.h @@ -71,7 +71,7 @@ typedef unsigned long Time; #undef Z #define Z (current_buffer->text->z) #else /* not HAVE_CARBON */ -#include /* for WindowRef */ +#include /* for WindowRef */ #include /* for GWorldPtr */ #include /* for ThemeCursor */ #include @@ -125,6 +125,28 @@ typedef unsigned long Time; #endif typedef WindowRef Window; +#if TARGET_API_MAC_CARBON +typedef ScrapRef Selection; +#else +typedef int Selection; +#endif +#define mac_set_window_title SetWindowTitleWithCFString +#define mac_set_window_modified SetWindowModified +#define mac_is_window_visible IsWindowVisible +#define mac_is_window_collapsed IsWindowCollapsed +#define mac_bring_window_to_front BringToFront +#define mac_send_window_behind SendBehind +#define mac_hide_window HideWindow +#define mac_show_window ShowWindow +#define mac_collapse_window CollapseWindow +#define mac_front_non_floating_window FrontNonFloatingWindow +#define mac_active_non_floating_window ActiveNonFloatingWindow +#define mac_activate_window ActivateWindow +#define mac_move_window_structure MoveWindowStructure +#define mac_move_window MoveWindow +#define mac_size_window SizeWindow +#define mac_get_global_mouse GetGlobalMouse +#define mac_is_window_toolbar_visible IsWindowToolbarVisible typedef GWorldPtr Pixmap; #define Cursor ThemeCursor @@ -361,6 +383,97 @@ typedef struct { (nr).right = ((nr).left + (width)), \ (nr).bottom = ((nr).top + (height))) +/* Definitions copied from lwlib.h */ + +typedef void * XtPointer; + +enum button_type +{ + BUTTON_TYPE_NONE, + BUTTON_TYPE_TOGGLE, + BUTTON_TYPE_RADIO +}; + +/* This structure is based on the one in ../lwlib/lwlib.h, modified + for Mac OS. */ +typedef struct _widget_value +{ + /* name of widget */ + Lisp_Object lname; + char* name; + /* value (meaning depend on widget type) */ + char* value; + /* keyboard equivalent. no implications for XtTranslations */ + Lisp_Object lkey; + char* key; + /* Help string or nil if none. + GC finds this string through the frame's menu_bar_vector + or through menu_items. */ + Lisp_Object help; + /* true if enabled */ + Boolean enabled; + /* true if selected */ + Boolean selected; + /* The type of a button. */ + enum button_type button_type; + /* true if menu title */ + Boolean title; +#if 0 + /* true if was edited (maintained by get_value) */ + Boolean edited; + /* true if has changed (maintained by lw library) */ + change_type change; + /* true if this widget itself has changed, + but not counting the other widgets found in the `next' field. */ + change_type this_one_change; +#endif + /* Contents of the sub-widgets, also selected slot for checkbox */ + struct _widget_value* contents; + /* data passed to callback */ + XtPointer call_data; + /* next one in the list */ + struct _widget_value* next; +#if 0 + /* slot for the toolkit dependent part. Always initialize to NULL. */ + void* toolkit_data; + /* tell us if we should free the toolkit data slot when freeing the + widget_value itself. */ + Boolean free_toolkit_data; + + /* we resource the widget_value structures; this points to the next + one on the free list if this one has been deallocated. + */ + struct _widget_value *free_list; +#endif +} widget_value; + +#if MAC_OS8 +#define M_APPLE 234 +#define I_ABOUT 1 + +#define EXTRA_STACK_ALLOC (256 * 1024) + +#define ARGV_STRING_LIST_ID 129 +#define RAM_TOO_LARGE_ALERT_ID 129 +#define ABOUT_ALERT_ID 128 +#endif + +#define DIALOG_LEFT_MARGIN (112) +#define DIALOG_TOP_MARGIN (24) +#define DIALOG_RIGHT_MARGIN (24) +#define DIALOG_BOTTOM_MARGIN (20) +#define DIALOG_MIN_INNER_WIDTH (338) +#define DIALOG_MAX_INNER_WIDTH (564) +#define DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE (12) +#define DIALOG_BUTTON_BUTTON_VERTICAL_SPACE (12) +#define DIALOG_BUTTON_MIN_WIDTH (68) +#define DIALOG_TEXT_MIN_HEIGHT (50) +#define DIALOG_TEXT_BUTTONS_VERTICAL_SPACE (10) +#define DIALOG_ICON_WIDTH (64) +#define DIALOG_ICON_HEIGHT (64) +#define DIALOG_ICON_LEFT_MARGIN (24) +#define DIALOG_ICON_TOP_MARGIN (15) + #endif /* EMACS_MACGUI_H */ /* arch-tag: 5a0da49a-35e2-418b-a58c-8a55778ae849 -- cgit v1.2.1 From 5aa4098ee2f374fadf0bab8a4382b19a4da4cad6 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 01:58:19 +0000 Subject: [!TARGET_API_MAC_CARBON]: Move includes to mactoolbox.c. (enum mac_menu_kind): Move enum to mactoolbox.c. (min_menu_id): Move variable to mactoolbox.c. (quit_dialog_event_loop) [TARGET_API_MAC_CARBON]: Likewise. (DIALOG_WINDOW_RESOURCE): Move define to mactoolbox.c. (DIALOG_BUTTON_COMMAND_ID_OFFSET, DIALOG_BUTTON_COMMAND_ID_P) (DIALOG_BUTTON_COMMAND_ID_VALUE, DIALOG_BUTTON_MAKE_COMMAND_ID) [TARGET_API_MAC_CARBON]: Likewise. (XtPointer): Move typedef to macgui.h. (enum button_type): Move enum to macgui.h. (widget_value): Move typedef to macgui.h. (DIALOG_LEFT_MARGIN, DIALOG_TOP_MARGIN, DIALOG_RIGHT_MARGIN) (DIALOG_BOTTOM_MARGIN, DIALOG_MIN_INNER_WIDTH, DIALOG_MAX_INNER_WIDTH) (DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE) (DIALOG_BUTTON_BUTTON_VERTICAL_SPACE, DIALOG_BUTTON_MIN_WIDTH) (DIALOG_TEXT_MIN_HEIGHT, DIALOG_TEXT_BUTTONS_VERTICAL_SPACE) (DIALOG_ICON_WIDTH, DIALOG_ICON_HEIGHT, DIALOG_ICON_LEFT_MARGIN) (DIALOG_ICON_TOP_MARGIN): Move defines to macgui.h. (popup_activated_flag): Make variable non-static. (x_activate_menubar, install_menu_quit_handler, pop_down_menu) (add_menu_item, fill_menu, dispose_menus): Move functions to mactoolbox.c. (restore_show_help_function, menu_target_item_handler) (install_menu_target_item_handler, mac_handle_dialog_event) (install_dialog_event_handler, pop_down_dialog, create_and_show_dialog) [TARGET_API_MAC_CARBON]: Likewise. (menu_quit_handler) [MAC_OS_X_VERSION_MAX_ALLOWED >= 1030]: Likewise. (mac_dialog) [!TARGET_API_MAC_CARBON]: Likewise. (find_and_call_menu_selection, name_is_separator): Make function non-static. (Vshow_help_function, timer_check) [TARGET_API_MAC_CARBON]: Move extern to mactoolbox.c. (set_frame_menubar): Don't call install_menu_quit_handler. (menu_item_selection): New variable. (mac_menu_show): Use create_and_show_popup_menu. (create_and_show_dialog) [TARGET_API_MAC_CARBON]: Don't return selection but set variable menu_item_selection. All uses changed. (mac_fill_menubar): Rename from fill_menubar. All uses changed. Call install_menu_quit_handler. Move to mactoolbox.c. --- src/macmenu.c | 1323 +-------------------------------------------------------- 1 file changed, 14 insertions(+), 1309 deletions(-) (limited to 'src') diff --git a/src/macmenu.c b/src/macmenu.c index 591010a4d65..153969f7efd 100644 --- a/src/macmenu.c +++ b/src/macmenu.c @@ -36,20 +36,6 @@ Boston, MA 02110-1301, USA. */ #include "charset.h" #include "coding.h" -#if !TARGET_API_MAC_CARBON -#include -#include -#include -#include -#include -#include -#include -#include -#if defined (__MRC__) || (__MSL__ >= 0x6000) -#include -#endif -#endif /* not TARGET_API_MAC_CARBON */ - /* This may include sys/types.h, and that somehow loses if this is not done before the other system files. */ #include "macterm.h" @@ -62,21 +48,6 @@ Boston, MA 02110-1301, USA. */ #include "dispextern.h" -enum mac_menu_kind { /* Menu ID range */ - MAC_MENU_APPLE, /* 0 (Reserved by Apple) */ - MAC_MENU_MENU_BAR, /* 1 .. 233 */ - MAC_MENU_M_APPLE, /* 234 (== M_APPLE) */ - MAC_MENU_POPUP, /* 235 */ - MAC_MENU_DRIVER, /* 236 .. 255 (Reserved) */ - MAC_MENU_MENU_BAR_SUB, /* 256 .. 16383 */ - MAC_MENU_POPUP_SUB, /* 16384 .. 32767 */ - MAC_MENU_END /* 32768 */ -}; - -static const int min_menu_id[] = {0, 1, 234, 235, 236, 256, 16384, 32768}; - -#define DIALOG_WINDOW_RESOURCE 130 - #if TARGET_API_MAC_CARBON #define HAVE_DIALOGS 1 #endif @@ -84,69 +55,6 @@ static const int min_menu_id[] = {0, 1, 234, 235, 236, 256, 16384, 32768}; #undef HAVE_MULTILINGUAL_MENU /******************************************************************/ -/* Definitions copied from lwlib.h */ - -typedef void * XtPointer; - -enum button_type -{ - BUTTON_TYPE_NONE, - BUTTON_TYPE_TOGGLE, - BUTTON_TYPE_RADIO -}; - -/* This structure is based on the one in ../lwlib/lwlib.h, modified - for Mac OS. */ -typedef struct _widget_value -{ - /* name of widget */ - Lisp_Object lname; - char* name; - /* value (meaning depend on widget type) */ - char* value; - /* keyboard equivalent. no implications for XtTranslations */ - Lisp_Object lkey; - char* key; - /* Help string or nil if none. - GC finds this string through the frame's menu_bar_vector - or through menu_items. */ - Lisp_Object help; - /* true if enabled */ - Boolean enabled; - /* true if selected */ - Boolean selected; - /* The type of a button. */ - enum button_type button_type; - /* true if menu title */ - Boolean title; -#if 0 - /* true if was edited (maintained by get_value) */ - Boolean edited; - /* true if has changed (maintained by lw library) */ - change_type change; - /* true if this widget itself has changed, - but not counting the other widgets found in the `next' field. */ - change_type this_one_change; -#endif - /* Contents of the sub-widgets, also selected slot for checkbox */ - struct _widget_value* contents; - /* data passed to callback */ - XtPointer call_data; - /* next one in the list */ - struct _widget_value* next; -#if 0 - /* slot for the toolkit dependent part. Always initialize to NULL. */ - void* toolkit_data; - /* tell us if we should free the toolkit data slot when freeing the - widget_value itself. */ - Boolean free_toolkit_data; - - /* we resource the widget_value structures; this points to the next - one on the free list if this one has been deallocated. - */ - struct _widget_value *free_list; -#endif -} widget_value; /* Assumed by other routines to zero area returned. */ #define malloc_widget_value() (void *)memset (xmalloc (sizeof (widget_value)),\ @@ -198,12 +106,6 @@ static void single_keymap_panes P_ ((Lisp_Object, Lisp_Object, Lisp_Object, static void list_of_panes P_ ((Lisp_Object)); static void list_of_items P_ ((Lisp_Object)); -static void find_and_call_menu_selection P_ ((FRAME_PTR, int, Lisp_Object, - void *)); -static int fill_menu P_ ((MenuRef, widget_value *, enum mac_menu_kind, int)); -static void fill_menubar P_ ((widget_value *, int)); -static void dispose_menus P_ ((enum mac_menu_kind, int)); - /* This holds a Lisp vector that holds the results of decoding the keymaps or alist-of-alists that specify a menu. @@ -260,7 +162,7 @@ static int menu_items_n_panes; static int menu_items_submenu_depth; /* Nonzero means a menu is currently active. */ -static int popup_activated_flag; +int popup_activated_flag; /* This is set nonzero after the user activates the menu bar, and set to zero again after the menu bars are redisplayed by prepare_menu_bar. @@ -880,32 +782,6 @@ no quit occurs and `x-popup-menu' returns nil. */) #ifdef HAVE_MENUS -/* Regard ESC and C-g as Cancel even without the Cancel button. */ - -#if 0 /* defined (MAC_OSX) */ -static Boolean -mac_dialog_modal_filter (dialog, event, item_hit) - DialogRef dialog; - EventRecord *event; - DialogItemIndex *item_hit; -{ - Boolean result; - - result = StdFilterProc (dialog, event, item_hit); - if (result == false - && (event->what == keyDown || event->what == autoKey) - && ((event->message & charCodeMask) == kEscapeCharCode - || mac_quit_char_key_p (event->modifiers, - (event->message & keyCodeMask) >> 8))) - { - *item_hit = kStdCancelItemIndex; - return true; - } - - return result; -} -#endif - DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0, doc: /* Pop up a dialog box and return user's selection. POSITION specifies which frame to use. @@ -991,101 +867,6 @@ for instance using the window manager, then this produces a quit and but I don't want to make one now. */ CHECK_WINDOW (window); -#if 0 /* defined (MAC_OSX) */ - /* Special treatment for Fmessage_box, Fyes_or_no_p, and Fy_or_n_p. */ - if (EQ (position, Qt) - && STRINGP (Fcar (contents)) - && ((!NILP (Fequal (XCDR (contents), - Fcons (Fcons (build_string ("OK"), Qt), Qnil))) - && EQ (header, Qt)) - || (!NILP (Fequal (XCDR (contents), - Fcons (Fcons (build_string ("Yes"), Qt), - Fcons (Fcons (build_string ("No"), Qnil), - Qnil)))) - && NILP (header)))) - { - OSStatus err = noErr; - AlertStdCFStringAlertParamRec param; - CFStringRef error_string, explanation_string; - DialogRef alert; - DialogItemIndex item_hit; - Lisp_Object tem; - - /* Force a redisplay before showing the dialog. If a frame is - created just before showing the dialog, its contents may not - have been fully drawn. */ - Fredisplay (Qt); - - tem = Fstring_match (concat3 (build_string ("\\("), - call0 (intern ("sentence-end")), - build_string ("\\)\n")), - XCAR (contents), Qnil); - BLOCK_INPUT; - if (NILP (tem)) - { - error_string = cfstring_create_with_string (XCAR (contents)); - if (error_string == NULL) - err = memFullErr; - explanation_string = NULL; - } - else - { - tem = Fmatch_end (make_number (1)); - error_string = - cfstring_create_with_string (Fsubstring (XCAR (contents), - make_number (0), tem)); - if (error_string == NULL) - err = memFullErr; - else - { - XSETINT (tem, XINT (tem) + 1); - explanation_string = - cfstring_create_with_string (Fsubstring (XCAR (contents), - tem, Qnil)); - if (explanation_string == NULL) - { - CFRelease (error_string); - err = memFullErr; - } - } - } - if (err == noErr) - err = GetStandardAlertDefaultParams (¶m, - kStdCFStringAlertVersionOne); - if (err == noErr) - { - param.movable = true; - param.position = kWindowAlertPositionParentWindow; - if (NILP (header)) - { - param.defaultText = CFSTR ("Yes"); - param.otherText = CFSTR ("No"); -#if 0 - param.cancelText = CFSTR ("Cancel"); - param.cancelButton = kAlertStdAlertCancelButton; -#endif - } - err = CreateStandardAlert (kAlertNoteAlert, error_string, - explanation_string, ¶m, &alert); - CFRelease (error_string); - if (explanation_string) - CFRelease (explanation_string); - } - if (err == noErr) - err = RunStandardAlert (alert, mac_dialog_modal_filter, &item_hit); - UNBLOCK_INPUT; - - if (err == noErr) - { - if (item_hit == kStdCancelItemIndex) - Fsignal (Qquit, Qnil); - else if (item_hit == kStdOkItemIndex) - return Qt; - else - return Qnil; - } - } -#endif #ifndef HAVE_DIALOGS /* Display a menu with these alternatives in the middle of frame F. */ @@ -1125,66 +906,12 @@ for instance using the window manager, then this produces a quit and #endif /* HAVE_DIALOGS */ } -/* Activate the menu bar of frame F. - This is called from keyboard.c when it gets the - MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue. - - To activate the menu bar, we use the button-press event location - that was saved in saved_menu_event_location. - - But first we recompute the menu bar contents (the whole tree). - - The reason for saving the button event until here, instead of - passing it to the toolkit right away, is that we can safely - execute Lisp code. */ - -void -x_activate_menubar (f) - FRAME_PTR f; -{ - SInt32 menu_choice; - SInt16 menu_id, menu_item; - extern Point saved_menu_event_location; - - set_frame_menubar (f, 0, 1); - BLOCK_INPUT; - - popup_activated_flag = 1; - menu_choice = MenuSelect (saved_menu_event_location); - popup_activated_flag = 0; - menu_id = HiWord (menu_choice); - menu_item = LoWord (menu_choice); - -#if !TARGET_API_MAC_CARBON - if (menu_id == min_menu_id[MAC_MENU_M_APPLE]) - do_apple_menu (menu_item); - else -#endif - if (menu_id) - { - MenuRef menu = GetMenuRef (menu_id); - - if (menu) - { - UInt32 refcon; - - GetMenuItemRefCon (menu, menu_item, &refcon); - find_and_call_menu_selection (f, f->menu_bar_items_used, - f->menu_bar_vector, (void *) refcon); - } - } - - HiliteMenu (0); - - UNBLOCK_INPUT; -} - /* Find the menu selection and store it in the keyboard buffer. F is the frame the menu is on. MENU_BAR_ITEMS_USED is the length of VECTOR. VECTOR is an array of menu events for the whole menu. */ -static void +void find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data) FRAME_PTR f; int menu_bar_items_used; @@ -1577,141 +1304,6 @@ update_submenu_strings (first_wv) } -#if TARGET_API_MAC_CARBON -extern Lisp_Object Vshow_help_function; - -static Lisp_Object -restore_show_help_function (old_show_help_function) - Lisp_Object old_show_help_function; -{ - Vshow_help_function = old_show_help_function; - - return Qnil; -} - -static pascal OSStatus -menu_target_item_handler (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - OSStatus err; - MenuRef menu; - MenuItemIndex menu_item; - Lisp_Object help; - GrafPtr port; - int specpdl_count = SPECPDL_INDEX (); - - /* Don't be bothered with the overflowed toolbar items menu. */ - if (!popup_activated ()) - return eventNotHandledErr; - - err = GetEventParameter (event, kEventParamDirectObject, typeMenuRef, - NULL, sizeof (MenuRef), NULL, &menu); - if (err == noErr) - err = GetEventParameter (event, kEventParamMenuItemIndex, - typeMenuItemIndex, NULL, - sizeof (MenuItemIndex), NULL, &menu_item); - if (err == noErr) - err = GetMenuItemProperty (menu, menu_item, - MAC_EMACS_CREATOR_CODE, 'help', - sizeof (Lisp_Object), NULL, &help); - if (err != noErr) - help = Qnil; - - /* Temporarily bind Vshow_help_function to Qnil because we don't - want tooltips during menu tracking. */ - record_unwind_protect (restore_show_help_function, Vshow_help_function); - Vshow_help_function = Qnil; - GetPort (&port); - show_help_echo (help, Qnil, Qnil, Qnil, 1); - SetPort (port); - unbind_to (specpdl_count, Qnil); - - return err == noErr ? noErr : eventNotHandledErr; -} - -OSStatus -install_menu_target_item_handler () -{ - static const EventTypeSpec specs[] = - {{kEventClassMenu, kEventMenuTargetItem}}; - - return InstallApplicationEventHandler (NewEventHandlerUPP - (menu_target_item_handler), - GetEventTypeCount (specs), - specs, NULL, NULL); -} -#endif /* TARGET_API_MAC_CARBON */ - -/* Event handler function that pops down a menu on C-g. We can only pop - down menus if CancelMenuTracking is present (OSX 10.3 or later). */ - -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 -static pascal OSStatus -menu_quit_handler (nextHandler, theEvent, userData) - EventHandlerCallRef nextHandler; - EventRef theEvent; - void* userData; -{ - OSStatus err; - UInt32 keyCode; - UInt32 keyModifiers; - - err = GetEventParameter (theEvent, kEventParamKeyCode, - typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode); - - if (err == noErr) - err = GetEventParameter (theEvent, kEventParamKeyModifiers, - typeUInt32, NULL, sizeof(UInt32), - NULL, &keyModifiers); - - if (err == noErr && mac_quit_char_key_p (keyModifiers, keyCode)) - { - MenuRef menu = userData != 0 - ? (MenuRef)userData : AcquireRootMenu (); - - CancelMenuTracking (menu, true, 0); - if (!userData) ReleaseMenu (menu); - return noErr; - } - - return CallNextEventHandler (nextHandler, theEvent); -} -#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ - -/* Add event handler to all menus that belong to KIND so we can detect - C-g. ROOT_MENU is the root menu of the tracking session to dismiss - when C-g is detected. NULL means the menu bar. If - CancelMenuTracking isn't available, do nothing. */ - -static void -install_menu_quit_handler (kind, root_menu) - enum mac_menu_kind kind; - MenuRef root_menu; -{ -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 - static const EventTypeSpec typesList[] = - {{kEventClassKeyboard, kEventRawKeyDown}}; - int id; - -#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020 - if (CancelMenuTracking == NULL) - return; -#endif - for (id = min_menu_id[kind]; id < min_menu_id[kind + 1]; id++) - { - MenuRef menu = GetMenuRef (id); - - if (menu == NULL) - break; - InstallMenuEventHandler (menu, menu_quit_handler, - GetEventTypeCount (typesList), - typesList, root_menu, NULL); - } -#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ -} - /* Set the contents of the menubar widgets of frame F. The argument FIRST_TIME is currently ignored; it is set the first time this is called, from initialize_frame_menubar. */ @@ -1947,11 +1539,8 @@ set_frame_menubar (f, first_time, deep_p) /* Non-null value to indicate menubar has already been "created". */ f->output_data.mac->menubar_widget = 1; - fill_menubar (first_wv->contents, deep_p); + mac_fill_menubar (first_wv->contents, deep_p); - /* Add event handler so we can detect C-g. */ - install_menu_quit_handler (MAC_MENU_MENU_BAR, NULL); - install_menu_quit_handler (MAC_MENU_MENU_BAR_SUB, NULL); free_menubar_widget_value_tree (first_wv); UNBLOCK_INPUT; @@ -1968,29 +1557,8 @@ free_frame_menubar (f) } -static Lisp_Object -pop_down_menu (arg) - Lisp_Object arg; -{ - struct Lisp_Save_Value *p = XSAVE_VALUE (arg); - FRAME_PTR f = p->pointer; - MenuRef menu = GetMenuRef (min_menu_id[MAC_MENU_POPUP]); - - BLOCK_INPUT; - - /* Must reset this manually because the button release event is not - passed to Emacs event loop. */ - FRAME_MAC_DISPLAY_INFO (f)->grabbed = 0; - - /* delete all menus */ - dispose_menus (MAC_MENU_POPUP_SUB, 0); - DeleteMenu (min_menu_id[MAC_MENU_POPUP]); - DisposeMenu (menu); - - UNBLOCK_INPUT; - - return Qnil; -} +/* The item selected in the popup menu. */ +int menu_item_selection; /* Mac_menu_show actually displays a menu using the panes and items in menu_items and returns the value selected from it; we assume input @@ -2018,9 +1586,6 @@ mac_menu_show (f, x, y, for_click, keymaps, title, error) char **error; { int i; - int menu_item_choice; - UInt32 menu_item_selection; - MenuRef menu; widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0; widget_value **submenu_stack = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); @@ -2029,7 +1594,6 @@ mac_menu_show (f, x, y, for_click, keymaps, title, error) int submenu_depth = 0; int first_pane; - int specpdl_count = SPECPDL_INDEX (); *error = NULL; @@ -2215,45 +1779,14 @@ mac_menu_show (f, x, y, for_click, keymaps, title, error) first_wv->contents = wv_title; } - /* Actually create the menu. */ - menu = NewMenu (min_menu_id[MAC_MENU_POPUP], "\p"); - InsertMenu (menu, -1); - fill_menu (menu, first_wv->contents, MAC_MENU_POPUP_SUB, - min_menu_id[MAC_MENU_POPUP_SUB]); - - /* Free the widget_value objects we used to specify the - contents. */ - free_menubar_widget_value_tree (first_wv); - - /* Adjust coordinates to be root-window-relative. */ - x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); - y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); - /* No selection has been chosen yet. */ menu_item_selection = 0; - record_unwind_protect (pop_down_menu, make_save_value (f, 0)); + /* Actually create and show the menu until popped down. */ + create_and_show_popup_menu (f, first_wv, x, y, for_click); - /* Add event handler so we can detect C-g. */ - install_menu_quit_handler (MAC_MENU_POPUP, menu); - install_menu_quit_handler (MAC_MENU_POPUP_SUB, menu); - - /* Display the menu. */ - popup_activated_flag = 1; - menu_item_choice = PopUpMenuSelect (menu, y, x, 0); - popup_activated_flag = 0; - - /* Get the refcon to find the correct item */ - if (menu_item_choice) - { - MenuRef sel_menu = GetMenuRef (HiWord (menu_item_choice)); - - if (sel_menu) - GetMenuItemRefCon (sel_menu, LoWord (menu_item_choice), - &menu_item_selection); - } - - unbind_to (specpdl_count, Qnil); + /* Free the widget_value objects we used to specify the contents. */ + free_menubar_widget_value_tree (first_wv); /* Find the selected item, and its pane, to return the proper value. */ @@ -2320,582 +1853,6 @@ mac_menu_show (f, x, y, for_click, keymaps, title, error) #ifdef HAVE_DIALOGS /* Construct native Mac OS dialog based on widget_value tree. */ -#if TARGET_API_MAC_CARBON - -#define DIALOG_BUTTON_COMMAND_ID_OFFSET 'Bt\0\0' -#define DIALOG_BUTTON_COMMAND_ID_P(id) \ - (((id) & ~0xffff) == DIALOG_BUTTON_COMMAND_ID_OFFSET) -#define DIALOG_BUTTON_COMMAND_ID_VALUE(id) \ - ((id) - DIALOG_BUTTON_COMMAND_ID_OFFSET) -#define DIALOG_BUTTON_MAKE_COMMAND_ID(value) \ - ((value) + DIALOG_BUTTON_COMMAND_ID_OFFSET) - -extern EMACS_TIME timer_check P_ ((int)); -static int quit_dialog_event_loop; - -static pascal OSStatus -mac_handle_dialog_event (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - OSStatus err, result = eventNotHandledErr; - WindowRef window = (WindowRef) data; - - switch (GetEventClass (event)) - { - case kEventClassCommand: - { - HICommand command; - - err = GetEventParameter (event, kEventParamDirectObject, - typeHICommand, NULL, sizeof (HICommand), - NULL, &command); - if (err == noErr) - if (DIALOG_BUTTON_COMMAND_ID_P (command.commandID)) - { - SetWRefCon (window, command.commandID); - quit_dialog_event_loop = 1; - break; - } - - result = CallNextEventHandler (next_handler, event); - } - break; - - case kEventClassKeyboard: - { - OSStatus result; - char char_code; - - result = CallNextEventHandler (next_handler, event); - if (result != eventNotHandledErr) - break; - - err = GetEventParameter (event, kEventParamKeyMacCharCodes, - typeChar, NULL, sizeof (char), - NULL, &char_code); - if (err == noErr) - switch (char_code) - { - case kEscapeCharCode: - quit_dialog_event_loop = 1; - break; - - default: - { - UInt32 modifiers, key_code; - - err = GetEventParameter (event, kEventParamKeyModifiers, - typeUInt32, NULL, sizeof (UInt32), - NULL, &modifiers); - if (err == noErr) - err = GetEventParameter (event, kEventParamKeyCode, - typeUInt32, NULL, sizeof (UInt32), - NULL, &key_code); - if (err == noErr) - if (mac_quit_char_key_p (modifiers, key_code)) - quit_dialog_event_loop = 1; - } - break; - } - } - break; - - default: - abort (); - } - - if (quit_dialog_event_loop) - { - err = QuitEventLoop (GetCurrentEventLoop ()); - if (err == noErr) - result = noErr; - } - - return result; -} - -static OSStatus -install_dialog_event_handler (window) - WindowRef window; -{ - static const EventTypeSpec specs[] = - {{kEventClassCommand, kEventCommandProcess}, - {kEventClassKeyboard, kEventRawKeyDown}}; - static EventHandlerUPP handle_dialog_eventUPP = NULL; - - if (handle_dialog_eventUPP == NULL) - handle_dialog_eventUPP = NewEventHandlerUPP (mac_handle_dialog_event); - return InstallWindowEventHandler (window, handle_dialog_eventUPP, - GetEventTypeCount (specs), specs, - window, NULL); -} - -#define DIALOG_LEFT_MARGIN (112) -#define DIALOG_TOP_MARGIN (24) -#define DIALOG_RIGHT_MARGIN (24) -#define DIALOG_BOTTOM_MARGIN (20) -#define DIALOG_MIN_INNER_WIDTH (338) -#define DIALOG_MAX_INNER_WIDTH (564) -#define DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE (12) -#define DIALOG_BUTTON_BUTTON_VERTICAL_SPACE (12) -#define DIALOG_BUTTON_MIN_WIDTH (68) -#define DIALOG_TEXT_MIN_HEIGHT (50) -#define DIALOG_TEXT_BUTTONS_VERTICAL_SPACE (10) -#define DIALOG_ICON_WIDTH (64) -#define DIALOG_ICON_HEIGHT (64) -#define DIALOG_ICON_LEFT_MARGIN (24) -#define DIALOG_ICON_TOP_MARGIN (15) - -static Lisp_Object -pop_down_dialog (arg) - Lisp_Object arg; -{ - struct Lisp_Save_Value *p = XSAVE_VALUE (arg); - WindowRef window = p->pointer; - - BLOCK_INPUT; - - if (popup_activated_flag) - EndAppModalStateForWindow (window); - DisposeWindow (window); - popup_activated_flag = 0; - - UNBLOCK_INPUT; - - return Qnil; -} - -static int -create_and_show_dialog (f, first_wv) - FRAME_PTR f; - widget_value *first_wv; -{ - OSStatus err; - char *dialog_name, *message; - int nb_buttons, first_group_count, i, result = 0; - widget_value *wv; - short buttons_height, text_height, inner_width, inner_height; - Rect empty_rect, *rects; - WindowRef window = NULL; - ControlRef *buttons, default_button = NULL, text; - int specpdl_count = SPECPDL_INDEX (); - - dialog_name = first_wv->name; - nb_buttons = dialog_name[1] - '0'; - first_group_count = nb_buttons - (dialog_name[4] - '0'); - - wv = first_wv->contents; - message = wv->value; - - wv = wv->next; - SetRect (&empty_rect, 0, 0, 0, 0); - - /* Create dialog window. */ - err = CreateNewWindow (kMovableModalWindowClass, - kWindowStandardHandlerAttribute, - &empty_rect, &window); - if (err == noErr) - { - record_unwind_protect (pop_down_dialog, make_save_value (window, 0)); - err = SetThemeWindowBackground (window, kThemeBrushMovableModalBackground, - true); - } - if (err == noErr) - err = SetWindowTitleWithCFString (window, (dialog_name[0] == 'Q' - ? CFSTR ("Question") - : CFSTR ("Information"))); - - /* Create button controls and measure their optimal bounds. */ - if (err == noErr) - { - buttons = alloca (sizeof (ControlRef) * nb_buttons); - rects = alloca (sizeof (Rect) * nb_buttons); - for (i = 0; i < nb_buttons; i++) - { - CFStringRef label = cfstring_create_with_utf8_cstring (wv->value); - - if (label == NULL) - err = memFullErr; - else - { - err = CreatePushButtonControl (window, &empty_rect, - label, &buttons[i]); - CFRelease (label); - } - if (err == noErr) - { - if (!wv->enabled) - { -#ifdef MAC_OSX - err = DisableControl (buttons[i]); -#else - err = DeactivateControl (buttons[i]); -#endif - } - else if (default_button == NULL) - default_button = buttons[i]; - } - if (err == noErr) - { - SInt16 unused; - - rects[i] = empty_rect; - err = GetBestControlRect (buttons[i], &rects[i], &unused); - } - if (err == noErr) - { - UInt32 command_id; - - OffsetRect (&rects[i], -rects[i].left, -rects[i].top); - if (rects[i].right < DIALOG_BUTTON_MIN_WIDTH) - rects[i].right = DIALOG_BUTTON_MIN_WIDTH; - else if (rects[i].right > DIALOG_MAX_INNER_WIDTH) - rects[i].right = DIALOG_MAX_INNER_WIDTH; - - command_id = DIALOG_BUTTON_MAKE_COMMAND_ID ((int) wv->call_data); - err = SetControlCommandID (buttons[i], command_id); - } - if (err != noErr) - break; - wv = wv->next; - } - } - - /* Layout buttons. rects[i] is set relative to the bottom-right - corner of the inner box. */ - if (err == noErr) - { - short bottom, right, max_height, left_align_shift; - - inner_width = DIALOG_MIN_INNER_WIDTH; - bottom = right = max_height = 0; - for (i = 0; i < nb_buttons; i++) - { - if (right - rects[i].right < - inner_width) - { - if (i != first_group_count - && right - rects[i].right >= - DIALOG_MAX_INNER_WIDTH) - inner_width = - (right - rects[i].right); - else - { - bottom -= max_height + DIALOG_BUTTON_BUTTON_VERTICAL_SPACE; - right = max_height = 0; - } - } - if (max_height < rects[i].bottom) - max_height = rects[i].bottom; - OffsetRect (&rects[i], right - rects[i].right, - bottom - rects[i].bottom); - right = rects[i].left - DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE; - if (i == first_group_count - 1) - right -= DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE; - } - buttons_height = - (bottom - max_height); - - left_align_shift = - (inner_width + rects[nb_buttons - 1].left); - for (i = nb_buttons - 1; i >= first_group_count; i--) - { - if (bottom != rects[i].bottom) - { - left_align_shift = - (inner_width + rects[i].left); - bottom = rects[i].bottom; - } - OffsetRect (&rects[i], left_align_shift, 0); - } - } - - /* Create a static text control and measure its bounds. */ - if (err == noErr) - { - CFStringRef message_string; - Rect bounds; - - message_string = cfstring_create_with_utf8_cstring (message); - if (message_string == NULL) - err = memFullErr; - else - { - ControlFontStyleRec text_style; - - text_style.flags = 0; - SetRect (&bounds, 0, 0, inner_width, 0); - err = CreateStaticTextControl (window, &bounds, message_string, - &text_style, &text); - CFRelease (message_string); - } - if (err == noErr) - { - SInt16 unused; - - bounds = empty_rect; - err = GetBestControlRect (text, &bounds, &unused); - } - if (err == noErr) - { - text_height = bounds.bottom - bounds.top; - if (text_height < DIALOG_TEXT_MIN_HEIGHT) - text_height = DIALOG_TEXT_MIN_HEIGHT; - } - } - - /* Place buttons. */ - if (err == noErr) - { - inner_height = (text_height + DIALOG_TEXT_BUTTONS_VERTICAL_SPACE - + buttons_height); - - for (i = 0; i < nb_buttons; i++) - { - OffsetRect (&rects[i], DIALOG_LEFT_MARGIN + inner_width, - DIALOG_TOP_MARGIN + inner_height); - SetControlBounds (buttons[i], &rects[i]); - } - } - - /* Place text. */ - if (err == noErr) - { - Rect bounds; - - SetRect (&bounds, DIALOG_LEFT_MARGIN, DIALOG_TOP_MARGIN, - DIALOG_LEFT_MARGIN + inner_width, - DIALOG_TOP_MARGIN + text_height); - SetControlBounds (text, &bounds); - } - - /* Create the application icon at the upper-left corner. */ - if (err == noErr) - { - ControlButtonContentInfo content; - ControlRef icon; - static const ProcessSerialNumber psn = {0, kCurrentProcess}; -#ifdef MAC_OSX - FSRef app_location; -#else - ProcessInfoRec pinfo; - FSSpec app_spec; -#endif - SInt16 unused; - - content.contentType = kControlContentIconRef; -#ifdef MAC_OSX - err = GetProcessBundleLocation (&psn, &app_location); - if (err == noErr) - err = GetIconRefFromFileInfo (&app_location, 0, NULL, 0, NULL, - kIconServicesNormalUsageFlag, - &content.u.iconRef, &unused); -#else - bzero (&pinfo, sizeof (ProcessInfoRec)); - pinfo.processInfoLength = sizeof (ProcessInfoRec); - pinfo.processAppSpec = &app_spec; - err = GetProcessInformation (&psn, &pinfo); - if (err == noErr) - err = GetIconRefFromFile (&app_spec, &content.u.iconRef, &unused); -#endif - if (err == noErr) - { - Rect bounds; - - SetRect (&bounds, DIALOG_ICON_LEFT_MARGIN, DIALOG_ICON_TOP_MARGIN, - DIALOG_ICON_LEFT_MARGIN + DIALOG_ICON_WIDTH, - DIALOG_ICON_TOP_MARGIN + DIALOG_ICON_HEIGHT); - err = CreateIconControl (window, &bounds, &content, true, &icon); - ReleaseIconRef (content.u.iconRef); - } - } - - /* Show the dialog window and run event loop. */ - if (err == noErr) - if (default_button) - err = SetWindowDefaultButton (window, default_button); - if (err == noErr) - err = install_dialog_event_handler (window); - if (err == noErr) - { - SizeWindow (window, - DIALOG_LEFT_MARGIN + inner_width + DIALOG_RIGHT_MARGIN, - DIALOG_TOP_MARGIN + inner_height + DIALOG_BOTTOM_MARGIN, - true); - err = RepositionWindow (window, FRAME_MAC_WINDOW (f), - kWindowAlertPositionOnParentWindow); - } - if (err == noErr) - { - SetWRefCon (window, 0); - ShowWindow (window); - BringToFront (window); - popup_activated_flag = 1; - err = BeginAppModalStateForWindow (window); - } - if (err == noErr) - { - EventTargetRef toolbox_dispatcher = GetEventDispatcherTarget (); - - quit_dialog_event_loop = 0; - while (1) - { - EMACS_TIME next_time = timer_check (1); - long secs = EMACS_SECS (next_time); - long usecs = EMACS_USECS (next_time); - EventTimeout timeout; - EventRef event; - - if (secs < 0 || (secs == 0 && usecs == 0)) - { - /* Sometimes timer_check returns -1 (no timers) even if - there are timers. So do a timeout anyway. */ - secs = 1; - usecs = 0; - } - - timeout = (secs * kEventDurationSecond - + usecs * kEventDurationMicrosecond); - err = ReceiveNextEvent (0, NULL, timeout, kEventRemoveFromQueue, - &event); - if (err == noErr) - { - SendEventToEventTarget (event, toolbox_dispatcher); - ReleaseEvent (event); - } -#if 0 /* defined (MAC_OSX) */ - else if (err != eventLoopTimedOutErr) - { - if (err == eventLoopQuitErr) - err = noErr; - break; - } -#else - /* The return value of ReceiveNextEvent seems to be - unreliable. Use our own global variable instead. */ - if (quit_dialog_event_loop) - { - err = noErr; - break; - } -#endif - } - } - if (err == noErr) - { - UInt32 command_id = GetWRefCon (window); - - if (DIALOG_BUTTON_COMMAND_ID_P (command_id)) - result = DIALOG_BUTTON_COMMAND_ID_VALUE (command_id); - } - - unbind_to (specpdl_count, Qnil); - - return result; -} -#else /* not TARGET_API_MAC_CARBON */ -static int -mac_dialog (widget_value *wv) -{ - char *dialog_name; - char *prompt; - char **button_labels; - UInt32 *ref_cons; - int nb_buttons; - int left_count; - int i; - int dialog_width; - Rect rect; - WindowRef window_ptr; - ControlRef ch; - int left; - EventRecord event_record; - SInt16 part_code; - int control_part_code; - Point mouse; - - dialog_name = wv->name; - nb_buttons = dialog_name[1] - '0'; - left_count = nb_buttons - (dialog_name[4] - '0'); - button_labels = (char **) alloca (sizeof (char *) * nb_buttons); - ref_cons = (UInt32 *) alloca (sizeof (UInt32) * nb_buttons); - - wv = wv->contents; - prompt = (char *) alloca (strlen (wv->value) + 1); - strcpy (prompt, wv->value); - c2pstr (prompt); - - wv = wv->next; - for (i = 0; i < nb_buttons; i++) - { - button_labels[i] = wv->value; - button_labels[i] = (char *) alloca (strlen (wv->value) + 1); - strcpy (button_labels[i], wv->value); - c2pstr (button_labels[i]); - ref_cons[i] = (UInt32) wv->call_data; - wv = wv->next; - } - - window_ptr = GetNewCWindow (DIALOG_WINDOW_RESOURCE, NULL, (WindowRef) -1); - - SetPortWindowPort (window_ptr); - - TextFont (0); - /* Left and right margins in the dialog are 13 pixels each.*/ - dialog_width = 14; - /* Calculate width of dialog box: 8 pixels on each side of the text - label in each button, 12 pixels between buttons. */ - for (i = 0; i < nb_buttons; i++) - dialog_width += StringWidth (button_labels[i]) + 16 + 12; - - if (left_count != 0 && nb_buttons - left_count != 0) - dialog_width += 12; - - dialog_width = max (dialog_width, StringWidth (prompt) + 26); - - SizeWindow (window_ptr, dialog_width, 78, 0); - ShowWindow (window_ptr); - - SetPortWindowPort (window_ptr); - - TextFont (0); - - MoveTo (13, 29); - DrawString (prompt); - - left = 13; - for (i = 0; i < nb_buttons; i++) - { - int button_width = StringWidth (button_labels[i]) + 16; - SetRect (&rect, left, 45, left + button_width, 65); - ch = NewControl (window_ptr, &rect, button_labels[i], 1, 0, 0, 0, - kControlPushButtonProc, ref_cons[i]); - left += button_width + 12; - if (i == left_count - 1) - left += 12; - } - - i = 0; - while (!i) - { - if (WaitNextEvent (mDownMask, &event_record, 10, NULL)) - if (event_record.what == mouseDown) - { - part_code = FindWindow (event_record.where, &window_ptr); - if (part_code == inContent) - { - mouse = event_record.where; - GlobalToLocal (&mouse); - control_part_code = FindControl (mouse, window_ptr, &ch); - if (control_part_code == kControlButtonPart) - if (TrackControl (ch, mouse, NULL)) - i = GetControlReference (ch); - } - } - } - - DisposeWindow (window_ptr); - - return i; -} -#endif /* not TARGET_API_MAC_CARBON */ - static char * button_names [] = { "button1", "button2", "button3", "button4", "button5", "button6", "button7", "button8", "button9", "button10" }; @@ -2909,7 +1866,6 @@ mac_dialog_show (f, keymaps, title, header, error_name) { int i, nb_buttons=0; char dialog_name[6]; - int menu_item_selection; widget_value *wv, *first_wv = 0, *prev_wv = 0; @@ -3026,6 +1982,9 @@ mac_dialog_show (f, keymaps, title, header, error_name) first_wv = wv; } + /* No selection has been chosen yet. */ + menu_item_selection = 0; + /* Force a redisplay before showing the dialog. If a frame is created just before showing the dialog, its contents may not have been fully drawn. */ @@ -3033,7 +1992,7 @@ mac_dialog_show (f, keymaps, title, header, error_name) /* Actually create the dialog. */ #if TARGET_API_MAC_CARBON - menu_item_selection = create_and_show_dialog (f, first_wv); + create_and_show_dialog (f, first_wv); #else menu_item_selection = mac_dialog (first_wv); #endif @@ -3093,7 +2052,7 @@ mac_dialog_show (f, keymaps, title, header, error_name) /* Is this item a separator? */ -static int +int name_is_separator (name) const char *name; { @@ -3106,260 +2065,6 @@ name_is_separator (name) them like normal separators. */ return (*name == '\0' || start + 2 == name); } - -static void -add_menu_item (menu, pos, wv) - MenuRef menu; - int pos; - widget_value *wv; -{ -#if TARGET_API_MAC_CARBON - CFStringRef item_name; -#else - Str255 item_name; -#endif - - if (name_is_separator (wv->name)) - AppendMenu (menu, "\p-"); - else - { - AppendMenu (menu, "\pX"); - -#if TARGET_API_MAC_CARBON - item_name = cfstring_create_with_utf8_cstring (wv->name); - - if (wv->key != NULL) - { - CFStringRef name, key; - - name = item_name; - key = cfstring_create_with_utf8_cstring (wv->key); - item_name = CFStringCreateWithFormat (NULL, NULL, CFSTR ("%@ %@"), - name, key); - CFRelease (name); - CFRelease (key); - } - - SetMenuItemTextWithCFString (menu, pos, item_name); - CFRelease (item_name); - - if (wv->enabled) - EnableMenuItem (menu, pos); - else - DisableMenuItem (menu, pos); - - if (STRINGP (wv->help)) - SetMenuItemProperty (menu, pos, MAC_EMACS_CREATOR_CODE, 'help', - sizeof (Lisp_Object), &wv->help); -#else /* ! TARGET_API_MAC_CARBON */ - item_name[sizeof (item_name) - 1] = '\0'; - strncpy (item_name, wv->name, sizeof (item_name) - 1); - if (wv->key != NULL) - { - int len = strlen (item_name); - - strncpy (item_name + len, " ", sizeof (item_name) - 1 - len); - len = strlen (item_name); - strncpy (item_name + len, wv->key, sizeof (item_name) - 1 - len); - } - c2pstr (item_name); - SetMenuItemText (menu, pos, item_name); - - if (wv->enabled) - EnableItem (menu, pos); - else - DisableItem (menu, pos); -#endif /* ! TARGET_API_MAC_CARBON */ - - /* Draw radio buttons and tickboxes. */ - if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE || - wv->button_type == BUTTON_TYPE_RADIO)) - SetItemMark (menu, pos, checkMark); - else - SetItemMark (menu, pos, noMark); - - SetMenuItemRefCon (menu, pos, (UInt32) wv->call_data); - } -} - -/* Construct native Mac OS menu based on widget_value tree. */ - -static int -fill_menu (menu, wv, kind, submenu_id) - MenuRef menu; - widget_value *wv; - enum mac_menu_kind kind; - int submenu_id; -{ - int pos; - - for (pos = 1; wv != NULL; wv = wv->next, pos++) - { - add_menu_item (menu, pos, wv); - if (wv->contents && submenu_id < min_menu_id[kind + 1]) - { - MenuRef submenu = NewMenu (submenu_id, "\pX"); - - InsertMenu (submenu, -1); -#if TARGET_API_MAC_CARBON - SetMenuItemHierarchicalMenu (menu, pos, submenu); -#else - SetMenuItemHierarchicalID (menu, pos, submenu_id); -#endif - submenu_id = fill_menu (submenu, wv->contents, kind, submenu_id + 1); - } - } - - return submenu_id; -} - -/* Construct native Mac OS menubar based on widget_value tree. */ - -static void -fill_menubar (wv, deep_p) - widget_value *wv; - int deep_p; -{ - int id, submenu_id; -#if !TARGET_API_MAC_CARBON - int title_changed_p = 0; -#endif - - /* Clean up the menu bar when filled by the entire menu trees. */ - if (deep_p) - { - dispose_menus (MAC_MENU_MENU_BAR, 0); - dispose_menus (MAC_MENU_MENU_BAR_SUB, 0); -#if !TARGET_API_MAC_CARBON - title_changed_p = 1; -#endif - } - - /* Fill menu bar titles and submenus. Reuse the existing menu bar - titles as much as possible to minimize redraw (if !deep_p). */ - submenu_id = min_menu_id[MAC_MENU_MENU_BAR_SUB]; - for (id = min_menu_id[MAC_MENU_MENU_BAR]; - wv != NULL && id < min_menu_id[MAC_MENU_MENU_BAR + 1]; - wv = wv->next, id++) - { - OSStatus err = noErr; - MenuRef menu; -#if TARGET_API_MAC_CARBON - CFStringRef title; - - title = CFStringCreateWithCString (NULL, wv->name, - kCFStringEncodingMacRoman); -#else - Str255 title; - - strncpy (title, wv->name, 255); - title[255] = '\0'; - c2pstr (title); -#endif - - menu = GetMenuRef (id); - if (menu) - { -#if TARGET_API_MAC_CARBON - CFStringRef old_title; - - err = CopyMenuTitleAsCFString (menu, &old_title); - if (err == noErr) - { - if (CFStringCompare (title, old_title, 0) != kCFCompareEqualTo) - { -#ifdef MAC_OSX - if (id + 1 == min_menu_id[MAC_MENU_MENU_BAR + 1] - || GetMenuRef (id + 1) == NULL) - { - /* This is a workaround for Mac OS X 10.5 where - just calling SetMenuTitleWithCFString fails - to change the title of the last (Help) menu - in the menu bar. */ - DeleteMenu (id); - DisposeMenu (menu); - menu = NULL; - } - else -#endif /* MAC_OSX */ - err = SetMenuTitleWithCFString (menu, title); - } - CFRelease (old_title); - } - else - err = SetMenuTitleWithCFString (menu, title); -#else /* !TARGET_API_MAC_CARBON */ - if (!EqualString (title, (*menu)->menuData, false, false)) - { - DeleteMenu (id); - DisposeMenu (menu); - menu = NewMenu (id, title); - InsertMenu (menu, GetMenuRef (id + 1) ? id + 1 : 0); - title_changed_p = 1; - } -#endif /* !TARGET_API_MAC_CARBON */ - } - - if (!menu) - { -#if TARGET_API_MAC_CARBON - err = CreateNewMenu (id, 0, &menu); - if (err == noErr) - err = SetMenuTitleWithCFString (menu, title); -#else - menu = NewMenu (id, title); -#endif - if (err == noErr) - { - InsertMenu (menu, 0); -#if !TARGET_API_MAC_CARBON - title_changed_p = 1; -#endif - } - } -#if TARGET_API_MAC_CARBON - CFRelease (title); -#endif - - if (err == noErr) - if (wv->contents) - submenu_id = fill_menu (menu, wv->contents, MAC_MENU_MENU_BAR_SUB, - submenu_id); - } - - if (id < min_menu_id[MAC_MENU_MENU_BAR + 1] && GetMenuRef (id)) - { - dispose_menus (MAC_MENU_MENU_BAR, id); -#if !TARGET_API_MAC_CARBON - title_changed_p = 1; -#endif - } - -#if !TARGET_API_MAC_CARBON - if (title_changed_p) - InvalMenuBar (); -#endif -} - -/* Dispose of menus that belong to KIND, and remove them from the menu - list. ID is the lower bound of menu IDs that will be processed. */ - -static void -dispose_menus (kind, id) - enum mac_menu_kind kind; - int id; -{ - for (id = max (id, min_menu_id[kind]); id < min_menu_id[kind + 1]; id++) - { - MenuRef menu = GetMenuRef (id); - - if (menu == NULL) - break; - DeleteMenu (id); - DisposeMenu (menu); - } -} - #endif /* HAVE_MENUS */ /* Detect if a menu is currently active. */ -- cgit v1.2.1 From 5d6c5138509b0bba003bf93d0b6fe9bcc77195af Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 01:58:39 +0000 Subject: [!TARGET_API_MAC_CARBON]: Don't include Scrap.h. (Selection): Move typedef to macgui.h. (Vselection_converter_alist, Qmac_scrap_name, Qmac_ostype) (Vmac_apple_event_map, Qmac_apple_event_class, Qmac_apple_event_id): Make variables non-static. (Vmac_dnd_known_types) [TARGET_API_MAC_CARBON]: Likewise. (mac_handle_apple_event, cleanup_all_suspended_apple_events): Make functions non-static. (Vmac_service_selection) [MAC_OSX]: Likewise. (mac_get_selection_from_symbol, get_flavor_type_from_symbol) (mac_valid_selection_target_p, mac_clear_selection) (mac_get_selection_ownership_info, mac_valid_selection_value_p) (mac_put_selection_value, mac_selection_has_target_p) (mac_get_selection_value, mac_get_selection_target_list) (init_apple_event_handler, install_drag_handler, remove_drag_handler): Move functions to mactoolbox.c. (mac_do_track_drag, mac_do_receive_drag) [TARGET_API_MAC_CARBON]: Likewise. (copy_scrap_flavor_data, mac_handle_service_event) (install_service_handler) [MAC_OSX]: Likewise. (syms_of_macselect) : Use mac_dnd_default_known_types. --- src/macselect.c | 770 +------------------------------------------------------- 1 file changed, 13 insertions(+), 757 deletions(-) (limited to 'src') diff --git a/src/macselect.c b/src/macselect.c index 56efd496945..df7e98c0d67 100644 --- a/src/macselect.c +++ b/src/macselect.c @@ -25,38 +25,15 @@ Boston, MA 02110-1301, USA. */ #include "blockinput.h" #include "keymap.h" -#if TARGET_API_MAC_CARBON -typedef ScrapRef Selection; -#else /* !TARGET_API_MAC_CARBON */ -#include +#if !TARGET_API_MAC_CARBON #include -typedef int Selection; -#endif /* !TARGET_API_MAC_CARBON */ - -static OSStatus mac_get_selection_from_symbol P_ ((Lisp_Object, int, - Selection *)); -static ScrapFlavorType get_flavor_type_from_symbol P_ ((Lisp_Object, - Selection)); -static int mac_valid_selection_target_p P_ ((Lisp_Object)); -static OSStatus mac_clear_selection P_ ((Selection *)); -static Lisp_Object mac_get_selection_ownership_info P_ ((Selection)); -static int mac_valid_selection_value_p P_ ((Lisp_Object, Lisp_Object)); -static OSStatus mac_put_selection_value P_ ((Selection, Lisp_Object, - Lisp_Object)); -static int mac_selection_has_target_p P_ ((Selection, Lisp_Object)); -static Lisp_Object mac_get_selection_value P_ ((Selection, Lisp_Object)); -static Lisp_Object mac_get_selection_target_list P_ ((Selection)); +#endif + static void x_own_selection P_ ((Lisp_Object, Lisp_Object)); static Lisp_Object x_get_local_selection P_ ((Lisp_Object, Lisp_Object, int)); static Lisp_Object x_get_foreign_selection P_ ((Lisp_Object, Lisp_Object, Lisp_Object)); -EXFUN (Fx_selection_owner_p, 1); -#ifdef MAC_OSX -static OSStatus mac_handle_service_event P_ ((EventHandlerCallRef, - EventRef, void *)); -void init_service_handler P_ ((void)); -#endif Lisp_Object QPRIMARY, QSECONDARY, QTIMESTAMP, QTARGETS; @@ -98,302 +75,13 @@ static Lisp_Object Vselection_alist; selection value to a string representing the given selection type. This is for Lisp-level extension of the emacs selection handling. */ -static Lisp_Object Vselection_converter_alist; +Lisp_Object Vselection_converter_alist; /* A selection name (represented as a Lisp symbol) can be associated with a named scrap via `mac-scrap-name' property. Likewise for a selection type with a scrap flavor type via `mac-ostype'. */ -static Lisp_Object Qmac_scrap_name, Qmac_ostype; - -#ifdef MAC_OSX -/* Selection name for communication via Services menu. */ -static Lisp_Object Vmac_service_selection; -#endif - -/* Get a reference to the selection corresponding to the symbol SYM. - The reference is set to *SEL, and it becomes NULL if there's no - corresponding selection. Clear the selection if CLEAR_P is - non-zero. */ - -static OSStatus -mac_get_selection_from_symbol (sym, clear_p, sel) - Lisp_Object sym; - int clear_p; - Selection *sel; -{ - OSStatus err = noErr; - Lisp_Object str = Fget (sym, Qmac_scrap_name); - - if (!STRINGP (str)) - *sel = NULL; - else - { -#if TARGET_API_MAC_CARBON -#ifdef MAC_OSX - CFStringRef scrap_name = cfstring_create_with_string (str); - OptionBits options = (clear_p ? kScrapClearNamedScrap - : kScrapGetNamedScrap); - - err = GetScrapByName (scrap_name, options, sel); - CFRelease (scrap_name); -#else /* !MAC_OSX */ - if (clear_p) - err = ClearCurrentScrap (); - if (err == noErr) - err = GetCurrentScrap (sel); -#endif /* !MAC_OSX */ -#else /* !TARGET_API_MAC_CARBON */ - if (clear_p) - err = ZeroScrap (); - if (err == noErr) - *sel = 1; -#endif /* !TARGET_API_MAC_CARBON */ - } - - return err; -} - -/* Get a scrap flavor type from the symbol SYM. Return 0 if no - corresponding flavor type. If SEL is non-zero, the return value is - non-zero only when the SEL has the flavor type. */ - -static ScrapFlavorType -get_flavor_type_from_symbol (sym, sel) - Lisp_Object sym; - Selection sel; -{ - Lisp_Object str = Fget (sym, Qmac_ostype); - ScrapFlavorType flavor_type; - - if (STRINGP (str) && SBYTES (str) == 4) - flavor_type = EndianU32_BtoN (*((UInt32 *) SDATA (str))); - else - flavor_type = 0; - - if (flavor_type && sel) - { -#if TARGET_API_MAC_CARBON - OSStatus err; - ScrapFlavorFlags flags; - - err = GetScrapFlavorFlags (sel, flavor_type, &flags); - if (err != noErr) - flavor_type = 0; -#else /* !TARGET_API_MAC_CARBON */ - SInt32 size, offset; - - size = GetScrap (NULL, flavor_type, &offset); - if (size < 0) - flavor_type = 0; -#endif /* !TARGET_API_MAC_CARBON */ - } - - return flavor_type; -} - -/* Check if the symbol SYM has a corresponding selection target type. */ - -static int -mac_valid_selection_target_p (sym) - Lisp_Object sym; -{ - return get_flavor_type_from_symbol (sym, 0) != 0; -} - -/* Clear the selection whose reference is *SEL. */ - -static OSStatus -mac_clear_selection (sel) - Selection *sel; -{ -#if TARGET_API_MAC_CARBON -#ifdef MAC_OSX - return ClearScrap (sel); -#else - OSStatus err; - - err = ClearCurrentScrap (); - if (err == noErr) - err = GetCurrentScrap (sel); - return err; -#endif -#else /* !TARGET_API_MAC_CARBON */ - return ZeroScrap (); -#endif /* !TARGET_API_MAC_CARBON */ -} - -/* Get ownership information for SEL. Emacs can detect a change of - the ownership by comparing saved and current values of the - ownership information. */ - -static Lisp_Object -mac_get_selection_ownership_info (sel) - Selection sel; -{ -#if TARGET_API_MAC_CARBON - return long_to_cons ((unsigned long) sel); -#else /* !TARGET_API_MAC_CARBON */ - ScrapStuffPtr scrap_info = InfoScrap (); - - return make_number (scrap_info->scrapCount); -#endif /* !TARGET_API_MAC_CARBON */ -} - -/* Return non-zero if VALUE is a valid selection value for TARGET. */ - -static int -mac_valid_selection_value_p (value, target) - Lisp_Object value, target; -{ - return STRINGP (value); -} - -/* Put Lisp Object VALUE to the selection SEL. The target type is - specified by TARGET. */ - -static OSStatus -mac_put_selection_value (sel, target, value) - Selection sel; - Lisp_Object target, value; -{ - ScrapFlavorType flavor_type = get_flavor_type_from_symbol (target, 0); - - if (flavor_type == 0 || !STRINGP (value)) - return noTypeErr; - -#if TARGET_API_MAC_CARBON - return PutScrapFlavor (sel, flavor_type, kScrapFlavorMaskNone, - SBYTES (value), SDATA (value)); -#else /* !TARGET_API_MAC_CARBON */ - return PutScrap (SBYTES (value), flavor_type, SDATA (value)); -#endif /* !TARGET_API_MAC_CARBON */ -} +Lisp_Object Qmac_scrap_name, Qmac_ostype; -/* Check if data for the target type TARGET is available in SEL. */ - -static int -mac_selection_has_target_p (sel, target) - Selection sel; - Lisp_Object target; -{ - return get_flavor_type_from_symbol (target, sel) != 0; -} - -/* Get data for the target type TARGET from SEL and create a Lisp - string. Return nil if failed to get data. */ - -static Lisp_Object -mac_get_selection_value (sel, target) - Selection sel; - Lisp_Object target; -{ - OSStatus err; - Lisp_Object result = Qnil; - ScrapFlavorType flavor_type = get_flavor_type_from_symbol (target, sel); -#if TARGET_API_MAC_CARBON - Size size; - - if (flavor_type) - { - err = GetScrapFlavorSize (sel, flavor_type, &size); - if (err == noErr) - { - do - { - result = make_uninit_string (size); - err = GetScrapFlavorData (sel, flavor_type, - &size, SDATA (result)); - if (err != noErr) - result = Qnil; - else if (size < SBYTES (result)) - result = make_unibyte_string (SDATA (result), size); - } - while (STRINGP (result) && size > SBYTES (result)); - } - } -#else - Handle handle; - SInt32 size, offset; - - if (flavor_type) - size = GetScrap (NULL, flavor_type, &offset); - if (size >= 0) - { - handle = NewHandle (size); - HLock (handle); - size = GetScrap (handle, flavor_type, &offset); - if (size >= 0) - result = make_unibyte_string (*handle, size); - DisposeHandle (handle); - } -#endif - - return result; -} - -/* Get the list of target types in SEL. The return value is a list of - target type symbols possibly followed by scrap flavor type - strings. */ - -static Lisp_Object -mac_get_selection_target_list (sel) - Selection sel; -{ - Lisp_Object result = Qnil, rest, target; -#if TARGET_API_MAC_CARBON - OSStatus err; - UInt32 count, i, type; - ScrapFlavorInfo *flavor_info = NULL; - Lisp_Object strings = Qnil; - - err = GetScrapFlavorCount (sel, &count); - if (err == noErr) - flavor_info = xmalloc (sizeof (ScrapFlavorInfo) * count); - err = GetScrapFlavorInfoList (sel, &count, flavor_info); - if (err != noErr) - { - xfree (flavor_info); - flavor_info = NULL; - } - if (flavor_info == NULL) - count = 0; -#endif - for (rest = Vselection_converter_alist; CONSP (rest); rest = XCDR (rest)) - { - ScrapFlavorType flavor_type = 0; - - if (CONSP (XCAR (rest)) - && (target = XCAR (XCAR (rest)), - SYMBOLP (target)) - && (flavor_type = get_flavor_type_from_symbol (target, sel))) - { - result = Fcons (target, result); -#if TARGET_API_MAC_CARBON - for (i = 0; i < count; i++) - if (flavor_info[i].flavorType == flavor_type) - { - flavor_info[i].flavorType = 0; - break; - } -#endif - } - } -#if TARGET_API_MAC_CARBON - if (flavor_info) - { - for (i = 0; i < count; i++) - if (flavor_info[i].flavorType) - { - type = EndianU32_NtoB (flavor_info[i].flavorType); - strings = Fcons (make_unibyte_string ((char *) &type, 4), strings); - } - result = nconc2 (result, strings); - xfree (flavor_info); - } -#endif - - return result; -} /* Do protocol to assert ourself as a selection owner. Update the Vselection_alist so that we can reply to later requests for @@ -892,8 +580,8 @@ and t is the same as `SECONDARY'. */) Apple event support ***********************************************************************/ int mac_ready_for_apple_events = 0; -static Lisp_Object Vmac_apple_event_map; -static Lisp_Object Qmac_apple_event_class, Qmac_apple_event_id; +Lisp_Object Vmac_apple_event_map; +Lisp_Object Qmac_apple_event_class, Qmac_apple_event_id; static Lisp_Object Qemacs_suspension_id; extern Lisp_Object Qundefined; extern void mac_store_apple_event P_ ((Lisp_Object, Lisp_Object, @@ -1095,7 +783,7 @@ mac_handle_apple_event_1 (class, id, apple_event, reply) return err; } -static pascal OSErr +pascal OSErr mac_handle_apple_event (apple_event, reply, refcon) const AppleEvent *apple_event; AppleEvent *reply; @@ -1173,40 +861,13 @@ cleanup_suspended_apple_events (head, all_p) return nresumed; } -static void +void cleanup_all_suspended_apple_events () { cleanup_suspended_apple_events (&deferred_apple_events, 1); cleanup_suspended_apple_events (&suspended_apple_events, 1); } -void -init_apple_event_handler () -{ - OSErr err; - long result; - - /* Make sure we have Apple events before starting. */ - err = Gestalt (gestaltAppleEventsAttr, &result); - if (err != noErr) - abort (); - - if (!(result & (1 << gestaltAppleEventsPresent))) - abort (); - - err = AEInstallEventHandler (typeWildCard, typeWildCard, -#if TARGET_API_MAC_CARBON - NewAEEventHandlerUPP (mac_handle_apple_event), -#else - NewAEEventHandlerProc (mac_handle_apple_event), -#endif - 0L, false); - if (err != noErr) - abort (); - - atexit (cleanup_all_suspended_apple_events); -} - static UInt32 get_suspension_id (apple_event) Lisp_Object apple_event; @@ -1399,419 +1060,18 @@ nil, which means the event is already resumed or expired. */) Drag and drop support ***********************************************************************/ #if TARGET_API_MAC_CARBON -static Lisp_Object Vmac_dnd_known_types; -static pascal OSErr mac_do_track_drag P_ ((DragTrackingMessage, WindowRef, - void *, DragRef)); -static pascal OSErr mac_do_receive_drag P_ ((WindowRef, void *, DragRef)); -static DragTrackingHandlerUPP mac_do_track_dragUPP = NULL; -static DragReceiveHandlerUPP mac_do_receive_dragUPP = NULL; - -extern void mac_store_drag_event P_ ((WindowRef, Point, SInt16, - const AEDesc *)); - -static pascal OSErr -mac_do_track_drag (message, window, refcon, drag) - DragTrackingMessage message; - WindowRef window; - void *refcon; - DragRef drag; -{ - OSErr err = noErr; - static int can_accept; - UInt16 num_items, index; - - if (GetFrontWindowOfClass (kMovableModalWindowClass, false)) - return dragNotAcceptedErr; - - switch (message) - { - case kDragTrackingEnterHandler: - err = CountDragItems (drag, &num_items); - if (err != noErr) - break; - can_accept = 0; - for (index = 1; index <= num_items; index++) - { - ItemReference item; - FlavorFlags flags; - Lisp_Object rest; - - err = GetDragItemReferenceNumber (drag, index, &item); - if (err != noErr) - continue; - for (rest = Vmac_dnd_known_types; CONSP (rest); rest = XCDR (rest)) - { - Lisp_Object str; - FlavorType type; - - str = XCAR (rest); - if (!(STRINGP (str) && SBYTES (str) == 4)) - continue; - type = EndianU32_BtoN (*((UInt32 *) SDATA (str))); - - err = GetFlavorFlags (drag, item, type, &flags); - if (err == noErr) - { - can_accept = 1; - break; - } - } - } - break; - - case kDragTrackingEnterWindow: - if (can_accept) - { - RgnHandle hilite_rgn = NewRgn (); - - if (hilite_rgn) - { - Rect r; - - GetWindowPortBounds (window, &r); - OffsetRect (&r, -r.left, -r.top); - RectRgn (hilite_rgn, &r); - ShowDragHilite (drag, hilite_rgn, true); - DisposeRgn (hilite_rgn); - } - SetThemeCursor (kThemeCopyArrowCursor); - } - break; - - case kDragTrackingInWindow: - break; - - case kDragTrackingLeaveWindow: - if (can_accept) - { - HideDragHilite (drag); - SetThemeCursor (kThemeArrowCursor); - } - break; - - case kDragTrackingLeaveHandler: - break; - } - - if (err != noErr) - return dragNotAcceptedErr; - return noErr; -} - -static pascal OSErr -mac_do_receive_drag (window, refcon, drag) - WindowRef window; - void *refcon; - DragRef drag; -{ - OSErr err; - int num_types, i; - Lisp_Object rest, str; - FlavorType *types; - AppleEvent apple_event; - Point mouse_pos; - SInt16 modifiers; - - if (GetFrontWindowOfClass (kMovableModalWindowClass, false)) - return dragNotAcceptedErr; - - num_types = 0; - for (rest = Vmac_dnd_known_types; CONSP (rest); rest = XCDR (rest)) - { - str = XCAR (rest); - if (STRINGP (str) && SBYTES (str) == 4) - num_types++; - } - - types = xmalloc (sizeof (FlavorType) * num_types); - i = 0; - for (rest = Vmac_dnd_known_types; CONSP (rest); rest = XCDR (rest)) - { - str = XCAR (rest); - if (STRINGP (str) && SBYTES (str) == 4) - types[i++] = EndianU32_BtoN (*((UInt32 *) SDATA (str))); - } - - err = create_apple_event_from_drag_ref (drag, num_types, types, - &apple_event); - xfree (types); - - if (err == noErr) - err = GetDragMouse (drag, &mouse_pos, NULL); - if (err == noErr) - { - GlobalToLocal (&mouse_pos); - err = GetDragModifiers (drag, NULL, NULL, &modifiers); - } - if (err == noErr) - { - UInt32 key_modifiers = modifiers; - - err = AEPutParamPtr (&apple_event, kEventParamKeyModifiers, - typeUInt32, &key_modifiers, sizeof (UInt32)); - } - - if (err == noErr) - { - mac_store_drag_event (window, mouse_pos, 0, &apple_event); - AEDisposeDesc (&apple_event); - mac_wakeup_from_rne (); - return noErr; - } - else - return dragNotAcceptedErr; -} +Lisp_Object Vmac_dnd_known_types; #endif /* TARGET_API_MAC_CARBON */ -OSErr -install_drag_handler (window) - WindowRef window; -{ - OSErr err = noErr; - -#if TARGET_API_MAC_CARBON - if (mac_do_track_dragUPP == NULL) - mac_do_track_dragUPP = NewDragTrackingHandlerUPP (mac_do_track_drag); - if (mac_do_receive_dragUPP == NULL) - mac_do_receive_dragUPP = NewDragReceiveHandlerUPP (mac_do_receive_drag); - - err = InstallTrackingHandler (mac_do_track_dragUPP, window, NULL); - if (err == noErr) - err = InstallReceiveHandler (mac_do_receive_dragUPP, window, NULL); -#endif - - return err; -} - -void -remove_drag_handler (window) - WindowRef window; -{ -#if TARGET_API_MAC_CARBON - if (mac_do_track_dragUPP) - RemoveTrackingHandler (mac_do_track_dragUPP, window); - if (mac_do_receive_dragUPP) - RemoveReceiveHandler (mac_do_receive_dragUPP, window); -#endif -} - /*********************************************************************** Services menu support ***********************************************************************/ #ifdef MAC_OSX -OSStatus -install_service_handler () -{ - static const EventTypeSpec specs[] = - {{kEventClassService, kEventServiceGetTypes}, - {kEventClassService, kEventServiceCopy}, - {kEventClassService, kEventServicePaste}, - {kEventClassService, kEventServicePerform}}; - - return InstallApplicationEventHandler (NewEventHandlerUPP - (mac_handle_service_event), - GetEventTypeCount (specs), - specs, NULL, NULL); -} - -extern OSStatus mac_store_service_event P_ ((EventRef)); - -static OSStatus -copy_scrap_flavor_data (from_scrap, to_scrap, flavor_type) - ScrapRef from_scrap, to_scrap; - ScrapFlavorType flavor_type; -{ - OSStatus err; - Size size, size_allocated; - char *buf = NULL; - - err = GetScrapFlavorSize (from_scrap, flavor_type, &size); - if (err == noErr) - buf = xmalloc (size); - while (buf) - { - size_allocated = size; - err = GetScrapFlavorData (from_scrap, flavor_type, &size, buf); - if (err != noErr) - { - xfree (buf); - buf = NULL; - } - else if (size_allocated < size) - buf = xrealloc (buf, size); - else - break; - } - if (err == noErr) - { - if (buf == NULL) - err = memFullErr; - else - { - err = PutScrapFlavor (to_scrap, flavor_type, kScrapFlavorMaskNone, - size, buf); - xfree (buf); - } - } - - return err; -} - -static OSStatus -mac_handle_service_event (call_ref, event, data) - EventHandlerCallRef call_ref; - EventRef event; - void *data; -{ - OSStatus err = noErr; - ScrapRef cur_scrap, specific_scrap; - UInt32 event_kind = GetEventKind (event); - CFMutableArrayRef copy_types, paste_types; - CFStringRef type; - Lisp_Object rest; - ScrapFlavorType flavor_type; - - /* Check if Vmac_service_selection is a valid selection that has a - corresponding scrap. */ - if (!SYMBOLP (Vmac_service_selection)) - err = eventNotHandledErr; - else - err = mac_get_selection_from_symbol (Vmac_service_selection, 0, &cur_scrap); - if (!(err == noErr && cur_scrap)) - return eventNotHandledErr; - - switch (event_kind) - { - case kEventServiceGetTypes: - /* Set paste types. */ - err = GetEventParameter (event, kEventParamServicePasteTypes, - typeCFMutableArrayRef, NULL, - sizeof (CFMutableArrayRef), NULL, - &paste_types); - if (err != noErr) - break; - - for (rest = Vselection_converter_alist; CONSP (rest); - rest = XCDR (rest)) - if (CONSP (XCAR (rest)) && SYMBOLP (XCAR (XCAR (rest))) - && (flavor_type = - get_flavor_type_from_symbol (XCAR (XCAR (rest)), 0))) - { - type = CreateTypeStringWithOSType (flavor_type); - if (type) - { - CFArrayAppendValue (paste_types, type); - CFRelease (type); - } - } - - /* Set copy types. */ - err = GetEventParameter (event, kEventParamServiceCopyTypes, - typeCFMutableArrayRef, NULL, - sizeof (CFMutableArrayRef), NULL, - ©_types); - if (err != noErr) - break; - - if (NILP (Fx_selection_owner_p (Vmac_service_selection))) - break; - else - goto copy_all_flavors; - - case kEventServiceCopy: - err = GetEventParameter (event, kEventParamScrapRef, - typeScrapRef, NULL, - sizeof (ScrapRef), NULL, &specific_scrap); - if (err != noErr - || NILP (Fx_selection_owner_p (Vmac_service_selection))) - { - err = eventNotHandledErr; - break; - } - - copy_all_flavors: - { - UInt32 count, i; - ScrapFlavorInfo *flavor_info = NULL; - ScrapFlavorFlags flags; - - err = GetScrapFlavorCount (cur_scrap, &count); - if (err == noErr) - flavor_info = xmalloc (sizeof (ScrapFlavorInfo) * count); - err = GetScrapFlavorInfoList (cur_scrap, &count, flavor_info); - if (err != noErr) - { - xfree (flavor_info); - flavor_info = NULL; - } - if (flavor_info == NULL) - break; - - for (i = 0; i < count; i++) - { - flavor_type = flavor_info[i].flavorType; - err = GetScrapFlavorFlags (cur_scrap, flavor_type, &flags); - if (err == noErr && !(flags & kScrapFlavorMaskSenderOnly)) - { - if (event_kind == kEventServiceCopy) - err = copy_scrap_flavor_data (cur_scrap, specific_scrap, - flavor_type); - else /* event_kind == kEventServiceGetTypes */ - { - type = CreateTypeStringWithOSType (flavor_type); - if (type) - { - CFArrayAppendValue (copy_types, type); - CFRelease (type); - } - } - } - } - xfree (flavor_info); - } - break; - - case kEventServicePaste: - case kEventServicePerform: - { - int data_exists_p = 0; - - err = GetEventParameter (event, kEventParamScrapRef, typeScrapRef, - NULL, sizeof (ScrapRef), NULL, - &specific_scrap); - if (err == noErr) - err = mac_clear_selection (&cur_scrap); - if (err == noErr) - for (rest = Vselection_converter_alist; CONSP (rest); - rest = XCDR (rest)) - { - if (! (CONSP (XCAR (rest)) && SYMBOLP (XCAR (XCAR (rest))))) - continue; - flavor_type = get_flavor_type_from_symbol (XCAR (XCAR (rest)), - specific_scrap); - if (flavor_type == 0) - continue; - err = copy_scrap_flavor_data (specific_scrap, cur_scrap, - flavor_type); - if (err == noErr) - data_exists_p = 1; - } - if (!data_exists_p) - err = eventNotHandledErr; - else - err = mac_store_service_event (event); - } - break; - } - - if (err != noErr) - err = eventNotHandledErr; - return err; -} +/* Selection name for communication via Services menu. */ +Lisp_Object Vmac_service_selection; #endif - void syms_of_macselect () { @@ -1870,11 +1130,7 @@ set to nil. */); DEFVAR_LISP ("mac-dnd-known-types", &Vmac_dnd_known_types, doc: /* The types accepted by default for dropped data. The types are chosen in the order they appear in the list. */); - Vmac_dnd_known_types = list4 (build_string ("hfs "), build_string ("utxt"), - build_string ("TEXT"), build_string ("TIFF")); -#ifdef MAC_OSX - Vmac_dnd_known_types = Fcons (build_string ("furl"), Vmac_dnd_known_types); -#endif + Vmac_dnd_known_types = mac_dnd_default_known_types (); #endif #ifdef MAC_OSX -- cgit v1.2.1 From f2ec385ecd26c73ea9014ddfa009999b4cc2b24b Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 01:58:59 +0000 Subject: (mac_end_cg_clip): Add argument F. All uses changed. (mac_begin_cg_clip, mac_end_cg_clip): Allow null GC. (mac_invert_rectangle, mac_compute_glyph_string_overhangs) (mac_load_query_font): Use them instead of SetPortWindowPort. (mac_clear_window) [!USE_CG_DRAWING]: Likewise. (mac_draw_image_string_cg): Call CGContextSetTextMatrix. (x_update_begin, x_update_end): Call mac_update_begin and mac_update_end. (XTframe_up_to_date): Call mac_frame_up_to_date. (XTring_bell): Use mac_alert_sound_play. (note_mouse_movement): Use mac_get_frame_bounds. (XTmouse_position): Use mac_get_frame_mouse. (x_scroll_bar_create): Use mac_create_scroll_bar. (x_scroll_bar_remove): Use mac_dispose_scroll_bar. (XTset_vertical_scroll_bar): Use mac_set_scroll_bar_bounds and mac_redraw_scroll_bar. (mac_move_window_with_gravity) [USE_MAC_TOOLBAR]: Use mac_move_window instead of MoveWindow. (mac_handle_size_change) [TARGET_API_MAC_CARBON]: Use mac_reposition_hourglass. (x_set_offset): Use mac_move_window_structure instead of MoveWindowStructure. (x_set_window_size): Use mac_size_window instead of SizeWindow. (x_set_mouse_pixel_position) [MAC_OSX]: Use mac_convert_frame_point_to_global. (x_raise_frame): Use mac_bring_window_to_front instead of BringToFront. (x_lower_frame): Use mac_send_window_behind instead of SendBehind. (mac_handle_visibility_change): Use Window instead of WindowRef. Use mac_is_window_visible/mac_is_window_collapsed instead of IsWindowVisible/IsWindowCollapsed, respectively. Use mac_collapse_window/mac_show_window instead of CollapseWindow/ShowWindow, respectively. (x_make_frame_invisible): Use mac_hide_window instead of HideWindow. (x_iconify_frame): Use mac_show_window instead of ShowWindow. Use mac_collapse_window instead of CollapseWindow. (x_free_frame_resources): Use Window instead of WindowRef. Use mac_dispose_frame_window. Clean up focus-related variables before calling mac_dispose_frame_window. (do_zoom_window) [MAC_OS8]: Use mac_clear_area instead of mac_clear_window. (mac_initialize): Use mac_toolbox_initialize instead of initializing any_help_event_p and calling init_apple_event_handler, init_tsm, and init_menu_bar. (any_help_event_p, last_window, save_port_clip_region) (read_socket_inev, saved_menu_event_location): Move variables to mactoolbox.c. (last_scroll_bar_part, scroll_bar_timer) (scroll_bar_timer_event_posted_p) [USE_TOOLKIT_SCROLL_BARS]: Likewise. (font_panel_shown_p) [USE_MAC_FONT_PANEL]: Likewise. (tsm_document_id) [USE_MAC_TSM]: Likewise. (mouse_region) [!TARGET_API_MAC_CARBON]: Likewise. (mac_window_to_frame, DEFAULT_NUM_COLS, MIN_DOC_SIZE, MAX_DOC_SIZE): Move defines to mactoolbox.c. (FRAME_CG_CONTEXT) [USE_CG_DRAWING]: Likewise. (SCROLL_BAR_FIRST_DELAY, SCROLL_BAR_CONTINUOUS_DELAY) [USE_TOOLKIT_SCROLL_BARS]: Likewise. (TOOLBAR_IDENTIFIER, TOOLBAR_ICON_ITEM_IDENTIFIER) (TOOLBAR_ITEM_COMMAND_ID_OFFSET, TOOLBAR_ITEM_COMMAND_ID_P) (TOOLBAR_ITEM_COMMAND_ID_VALUE, TOOLBAR_ITEM_MAKE_COMMAND_ID) [USE_MAC_TOOLBAR]: Likewise. (M_APPLE, I_ABOUT, EXTRA_STACK_ALLOC, ARGV_STRING_LIST_ID) (RAM_TOO_LARGE_ALERT_ID, ABOUT_ALERT_ID): Move defines to macgui.h (x_flush, is_emacs_window, mac_begin_clip, mac_end_clip) (x_scroll_bar_handle_click, x_scroll_bar_report_motion) (mac_get_window_bounds, do_window_update, is_emacs_window) (do_grow_window, do_zoom_window, install_window_handler) (remove_window_handler, XTread_socket, init_menu_bar): Move functions to mactoolbox.c. (mac_flush_display_optional, mac_begin_cg_clip, mac_end_cg_clip) (mac_prepare_for_quickdraw) [USE_CG_DRAWING]: Likewise. (mac_scroll_area, mac_event_to_emacs_modifiers, mac_get_mouse_btn) (mac_convert_event_ref, mac_get_ideal_size, mac_store_drag_event) (mac_handle_window_event, mac_handle_keyboard_event) (mac_handle_command_event, mac_handle_mouse_event) (install_application_handler, mac_post_mouse_moved_event) [TARGET_API_MAC_CARBON]: Likewise. (scroll_bar_timer_callback, install_scroll_bar_timer) (set_scroll_bar_timer, control_part_code_to_scroll_bar_part) (construct_scroll_bar_click, get_control_part_bounds) (x_scroll_bar_handle_press, x_scroll_bar_handle_release) (x_scroll_bar_handle_drag, x_set_toolkit_scroll_bar_thumb) [USE_TOOLKIT_SCROLL_BARS]: Likewise. (x_scroll_bar_set_handle, x_scroll_bar_note_movement) [!USE_TOOLKIT_SCROLL_BARS]: Likewise. (mac_handle_toolbar_event, mac_create_frame_tool_bar) (update_frame_tool_bar, free_frame_tool_bar) (mac_tool_bar_note_mouse_movement, mac_handle_toolbar_command_event) [USE_MAC_TOOLBAR]: Likewise. (mac_font_panel_visible_p, mac_handle_font_event) (mac_show_hide_font_panel, mac_set_font_info_for_selection) [USE_MAC_FONT_PANEL]: Likewise. (mac_handle_text_input_event, init_tsm) [USE_MAC_TSM]: Likewise. (do_apple_menu, mac_wait_next_event) [!TARGET_API_MAC_CARBON]: Likewise. (mac_store_service_event) [MAC_OSX]: Likewise. (last_mouse_glyph, last_mouse_glyph_frame, last_mouse_scroll_bar) (last_mouse_movement_time, input_signal_count) (mac_screen_config_changed, Qhi_command, Qtoolbar_switch_mode) (Qservice, Qpaste, Qperform, keycode_to_xkeysym_table): Make variables non-static. (Qpanel_closed, Qselection) [USE_MAC_FONT_PANEL]: Likewise. (Qtext_input, Vmac_ts_active_input_overlay, Qupdate_active_input_area) (Qunicode_for_key_event, Vmac_ts_script_language_on_focus) (saved_ts_script_language_on_focus) [USE_MAC_TSM]: Likewise. (mac_focus_changed, note_mouse_movement, mac_focus_frame) (mac_handle_origin_change, mac_handle_size_change) (mac_handle_visibility_change, mac_to_emacs_modifiers) (mac_mapped_modifiers, mac_get_emulated_btn, do_keystroke) (mac_get_screen_info): Make functions non-static. (mac_move_window_with_gravity, mac_get_window_origin_with_gravity) (mac_image_spec_to_cg_image) [USE_MAC_TOOLBAR]: Likewise. (mac_store_event_ref_as_apple_event) [TARGET_API_MAC_CARBON]: Likewise. (Qwindow, mac_ready_for_apple_events): Move externs to mactoolbox.c. (Qbefore_string) [USE_MAC_TSM]: Likewise. (mac_toolbox_initialize, x_scroll_bar_report_motion, XTread_socket): Add externs. (mac_flush_display_optional) [USE_CG_DRAWING]: Likewise. (install_drag_handler, remove_drag_handler, install_service_handler) (install_menu_target_item_handler): Remove externs. (XSetWindowBackground): Rename to mac_set_frame_window_background. Take frame as argument instead of display and window. Move to mactoolbox.c. (mac_restore_keyboard_input_source, mac_save_keyboard_input_source) [USE_MAC_TSM]: New functions created from mac_tsm_resume and mac_tsm_suspend, respectively. (mac_tsm_resume, mac_tsm_suspend) [USE_MAC_TSM]: Use them. Move to mactoolbox.c. --- src/macterm.c | 5118 +++++++++------------------------------------------------ 1 file changed, 801 insertions(+), 4317 deletions(-) (limited to 'src') diff --git a/src/macterm.c b/src/macterm.c index 9e5753ec3cc..a043e5a4860 100644 --- a/src/macterm.c +++ b/src/macterm.c @@ -91,15 +91,6 @@ Lisp_Object Vx_toolkit_scroll_bars; rendering which may anti-alias the text. */ int mac_use_core_graphics; - -/* Non-zero means that a HELP_EVENT has been generated since Emacs - start. */ - -static int any_help_event_p; - -/* Last window where we saw the mouse. Used by mouse-autoselect-window. */ -static Lisp_Object last_window; - /* Non-zero means make use of UNDERLINE_POSITION font properties. (Not yet supported.) */ int x_use_underline_position_properties; @@ -159,8 +150,8 @@ struct frame *pending_autoraise_frame; /* Where the mouse was last time we reported a mouse event. */ -static Rect last_mouse_glyph; -static FRAME_PTR last_mouse_glyph_frame; +Rect last_mouse_glyph; +FRAME_PTR last_mouse_glyph_frame; /* The scroll bar in which the last X motion event occurred. @@ -172,7 +163,7 @@ static FRAME_PTR last_mouse_glyph_frame; this to Qnil, to tell XTmouse_position to return an ordinary motion event. */ -static Lisp_Object last_mouse_scroll_bar; +Lisp_Object last_mouse_scroll_bar; /* This is a hack. We would really prefer that XTmouse_position would return the time associated with the position it returns, but there @@ -181,7 +172,7 @@ static Lisp_Object last_mouse_scroll_bar; of the last movement we received, and return that in hopes that it's somewhat accurate. */ -static Time last_mouse_movement_time; +Time last_mouse_movement_time; struct scroll_bar *tracked_scroll_bar = NULL; @@ -189,9 +180,9 @@ struct scroll_bar *tracked_scroll_bar = NULL; events. */ #ifdef __STDC__ -static int volatile input_signal_count; +int volatile input_signal_count; #else -static int input_signal_count; +int input_signal_count; #endif extern Lisp_Object Vsystem_name; @@ -212,8 +203,6 @@ extern int inhibit_window_system; QDGlobals qd; /* QuickDraw global information structure. */ #endif -#define mac_window_to_frame(wp) (((mac_output *) GetWRefCon (wp))->mFP) - struct mac_display_info *mac_display_info_for_display (Display *); static void x_update_window_end P_ ((struct window *, int, int)); int x_catch_errors P_ ((Display *)); @@ -237,11 +226,6 @@ static void x_clear_frame P_ ((void)); static void frame_highlight P_ ((struct frame *)); static void frame_unhighlight P_ ((struct frame *)); static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *)); -static void mac_focus_changed P_ ((int, struct mac_display_info *, - struct frame *, struct input_event *)); -static void x_detect_focus_change P_ ((struct mac_display_info *, - const EventRecord *, - struct input_event *)); static void XTframe_rehighlight P_ ((struct frame *)); static void x_frame_rehighlight P_ ((struct x_display_info *)); static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *)); @@ -249,19 +233,22 @@ static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int, enum text_cursor_kinds)); static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC)); -static void x_flush P_ ((struct frame *f)); static void x_update_begin P_ ((struct frame *)); static void x_update_window_begin P_ ((struct window *)); static void x_after_update_window_line P_ ((struct glyph_row *)); -static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *, - enum scroll_bar_part *, - Lisp_Object *, Lisp_Object *, - unsigned long *)); -static int is_emacs_window P_ ((WindowRef)); static XCharStruct *mac_per_char_metric P_ ((XFontStruct *, XChar2b *, int)); static void XSetFont P_ ((Display *, GC, XFontStruct *)); +extern void mac_toolbox_initialize P_ ((void)); +extern void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *, + enum scroll_bar_part *, + Lisp_Object *, Lisp_Object *, + unsigned long *)); +#if USE_CG_DRAWING +extern void mac_flush_display_optional P_ ((struct frame *)); +#endif + #define GC_FORE_COLOR(gc) (&(gc)->fore_color) #define GC_BACK_COLOR(gc) (&(gc)->back_color) #define GC_FONT(gc) ((gc)->xgcv.font) @@ -324,8 +311,6 @@ static void XSetFont P_ ((Display *, GC, XFontStruct *)); (gc)->cg_fore_color) #if USE_CG_DRAWING -#define FRAME_CG_CONTEXT(f) ((f)->output_data.mac->cg_context) - /* Fringe bitmaps. */ static int max_fringe_bmp = 0; @@ -353,96 +338,7 @@ init_cg_color () } #endif } - -static CGContextRef -mac_begin_cg_clip (f, gc) - struct frame *f; - GC gc; -{ - CGContextRef context = FRAME_CG_CONTEXT (f); - - if (!context) - { - QDBeginCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)), &context); - FRAME_CG_CONTEXT (f) = context; - } - - CGContextSaveGState (context); - CGContextTranslateCTM (context, 0, FRAME_PIXEL_HEIGHT (f)); - CGContextScaleCTM (context, 1, -1); - if (gc && gc->n_clip_rects) - CGContextClipToRects (context, gc->clip_rects, gc->n_clip_rects); - - return context; -} - -static void -mac_end_cg_clip (f) - struct frame *f; -{ - CGContextRestoreGState (FRAME_CG_CONTEXT (f)); -} - -void -mac_prepare_for_quickdraw (f) - struct frame *f; -{ - if (f == NULL) - { - Lisp_Object rest, frame; - FOR_EACH_FRAME (rest, frame) - if (FRAME_MAC_P (XFRAME (frame))) - mac_prepare_for_quickdraw (XFRAME (frame)); - } - else - { - CGContextRef context = FRAME_CG_CONTEXT (f); - - if (context) - { - CGContextSynchronize (context); - QDEndCGContext (GetWindowPort (FRAME_MAC_WINDOW (f)), - &FRAME_CG_CONTEXT (f)); - } - } -} -#endif - -static RgnHandle saved_port_clip_region = NULL; - -static void -mac_begin_clip (f, gc) - struct frame *f; - GC gc; -{ - static RgnHandle new_region = NULL; - - if (saved_port_clip_region == NULL) - saved_port_clip_region = NewRgn (); - if (new_region == NULL) - new_region = NewRgn (); - -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - SetPortWindowPort (FRAME_MAC_WINDOW (f)); - - if (gc->n_clip_rects) - { - GetClip (saved_port_clip_region); - SectRgn (saved_port_clip_region, gc->clip_region, new_region); - SetClip (new_region); - } -} - -static void -mac_end_clip (gc) - GC gc; -{ - if (gc->n_clip_rects) - SetClip (saved_port_clip_region); -} - +#endif /* USE_CG_DRAWING */ /* X display function emulation */ @@ -491,7 +387,7 @@ mac_draw_line (f, gc, x1, y1, x2, y2) RGBForeColor (GC_FORE_COLOR (gc)); MoveTo (x1, y1); LineTo (x2, y2); - mac_end_clip (gc); + mac_end_clip (f, gc); #endif } @@ -561,7 +457,7 @@ mac_erase_rectangle (f, gc, x, y, width, height) SetRect (&r, x, y, x + width, y + height); EraseRect (&r); RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); - mac_end_clip (gc); + mac_end_clip (f, gc); } #endif } @@ -596,10 +492,8 @@ mac_clear_window (f) mac_end_cg_clip (f); } #else /* !USE_CG_DRAWING */ - SetPortWindowPort (FRAME_MAC_WINDOW (f)); - + mac_begin_clip (f, NULL); RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); - #if TARGET_API_MAC_CARBON { Rect r; @@ -610,6 +504,7 @@ mac_clear_window (f) #else /* not TARGET_API_MAC_CARBON */ EraseRect (&(FRAME_MAC_WINDOW (f)->portRect)); #endif /* not TARGET_API_MAC_CARBON */ + mac_end_clip (f, NULL); #endif } @@ -688,7 +583,7 @@ mac_draw_bitmap (f, gc, x, y, width, height, bits, overlay_p) overlay_p ? srcOr : srcCopy, 0); #endif /* not TARGET_API_MAC_CARBON */ RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); - mac_end_clip (gc); + mac_end_clip (f, gc); } #endif /* !USE_CG_DRAWING */ @@ -843,7 +738,7 @@ mac_fill_rectangle (f, gc, x, y, width, height) RGBForeColor (GC_FORE_COLOR (gc)); SetRect (&r, x, y, x + width, y + height); PaintRect (&r); /* using foreground color of gc */ - mac_end_clip (gc); + mac_end_clip (f, gc); #endif } @@ -872,7 +767,7 @@ mac_draw_rectangle (f, gc, x, y, width, height) RGBForeColor (GC_FORE_COLOR (gc)); SetRect (&r, x, y, x + width + 1, y + height + 1); FrameRect (&r); /* using foreground color of gc */ - mac_end_clip (gc); + mac_end_clip (f, gc); #endif } @@ -885,14 +780,10 @@ mac_invert_rectangle (f, x, y, width, height) { Rect r; -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - SetPortWindowPort (FRAME_MAC_WINDOW (f)); - + mac_begin_clip (f, NULL); SetRect (&r, x, y, x + width, y + height); - InvertRect (&r); + mac_end_clip (f, NULL); } @@ -1008,7 +899,7 @@ mac_draw_image_string_atsui (f, gc, x, y, buf, nchars, bg_width, kATSUFromTextBeginning, kATSUToTextEnd, kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc); } - mac_end_clip (gc); + mac_end_clip (f, gc); #ifdef MAC_OSX } else @@ -1142,7 +1033,7 @@ mac_draw_image_string_qd (f, gc, x, y, buf, nchars, bg_width, } if (bg_width) RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); - mac_end_clip (gc); + mac_end_clip (f, gc); #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 if (mac_use_core_graphics) @@ -1437,6 +1328,7 @@ mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width, overstrike_p) CG_SET_FILL_COLOR_WITH_GC_FOREGROUND (context, gc); CGContextSetFont (context, GC_FONT (gc)->cg_font); CGContextSetFontSize (context, GC_FONT (gc)->mac_fontsize); + CGContextSetTextMatrix (context, CGAffineTransformIdentity); if (GC_FONT (gc)->mac_fontsize <= cg_text_anti_aliasing_threshold) CGContextSetShouldAntialias (context, false); #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 @@ -1521,7 +1413,7 @@ mac_copy_area (src, f, gc, src_x, src_y, width, height, dest_x, dest_y) RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); - mac_end_clip (gc); + mac_end_clip (f, gc); } @@ -1567,13 +1459,18 @@ mac_copy_area_with_mask (src, mask, f, gc, src_x, src_y, RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); - mac_end_clip (gc); + mac_end_clip (f, gc); } #endif /* !USE_CG_DRAWING */ /* Mac replacement for XCopyArea: used only for scrolling. */ +#if TARGET_API_MAC_CARBON +/* Defined in mactoolbox.c. */ +extern void mac_scroll_area P_ ((struct frame *, GC, int, int, + unsigned int, unsigned int, int, int)); +#else /* not TARGET_API_MAC_CARBON */ static void mac_scroll_area (f, gc, src_x, src_y, width, height, dest_x, dest_y) struct frame *f; @@ -1582,19 +1479,6 @@ mac_scroll_area (f, gc, src_x, src_y, width, height, dest_x, dest_y) unsigned int width, height; int dest_x, dest_y; { -#if TARGET_API_MAC_CARBON - Rect src_r; - RgnHandle dummy = NewRgn (); /* For avoiding update events. */ - - SetRect (&src_r, src_x, src_y, src_x + width, src_y + height); -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - ScrollWindowRect (FRAME_MAC_WINDOW (f), - &src_r, dest_x - src_x, dest_y - src_y, - kScrollWindowNoOptions, dummy); - DisposeRgn (dummy); -#else /* not TARGET_API_MAC_CARBON */ Rect src_r, dest_r; WindowRef w = FRAME_MAC_WINDOW (f); @@ -1611,9 +1495,9 @@ mac_scroll_area (f, gc, src_x, src_y, width, height, dest_x, dest_y) RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f))); - mac_end_clip (gc); -#endif /* not TARGET_API_MAC_CARBON */ + mac_end_clip (f, gc); } +#endif /* not TARGET_API_MAC_CARBON */ /* Mac replacement for XChangeGC. */ @@ -1850,70 +1734,6 @@ mac_reset_clip_rectangles (display, gc) gc->n_clip_rects = 0; } - -/* Mac replacement for XSetWindowBackground. */ - -void -XSetWindowBackground (display, w, color) - Display *display; - WindowRef w; - unsigned long color; -{ -#if !TARGET_API_MAC_CARBON - AuxWinHandle aw_handle; - CTabHandle ctab_handle; - ColorSpecPtr ct_table; - short ct_size; -#endif - RGBColor bg_color; - - bg_color.red = RED16_FROM_ULONG (color); - bg_color.green = GREEN16_FROM_ULONG (color); - bg_color.blue = BLUE16_FROM_ULONG (color); - -#if TARGET_API_MAC_CARBON - SetWindowContentColor (w, &bg_color); -#else - if (GetAuxWin (w, &aw_handle)) - { - ctab_handle = (*aw_handle)->awCTable; - HandToHand ((Handle *) &ctab_handle); - ct_table = (*ctab_handle)->ctTable; - ct_size = (*ctab_handle)->ctSize; - while (ct_size > -1) - { - if (ct_table->value == 0) - { - ct_table->rgb = bg_color; - CTabChanged (ctab_handle); - SetWinColor (w, (WCTabHandle) ctab_handle); - } - ct_size--; - } - } -#endif -} - -/* Flush display of frame F, or of all frames if F is null. */ - -static void -x_flush (f) - struct frame *f; -{ -#if TARGET_API_MAC_CARBON - BLOCK_INPUT; -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - if (f) - QDFlushPortBuffer (GetWindowPort (FRAME_MAC_WINDOW (f)), NULL); - else - QDFlushPortBuffer (GetQDGlobalsThePort (), NULL); - UNBLOCK_INPUT; -#endif -} - - /* Remove calls to XFlush by defining XFlush to an empty replacement. Calls to XFlush should be unnecessary because the X output buffer is flushed automatically as needed by calls to XPending, @@ -1923,16 +1743,6 @@ x_flush (f) #define XFlush(DISPLAY) (void) 0 -#if USE_CG_DRAWING -static void -mac_flush_display_optional (f) - struct frame *f; -{ - BLOCK_INPUT; - mac_prepare_for_quickdraw (f); - UNBLOCK_INPUT; -} -#endif /*********************************************************************** Starting and ending an update @@ -1947,17 +1757,9 @@ static void x_update_begin (f) struct frame *f; { -#if TARGET_API_MAC_CARBON - /* During update of a frame, availability of input events is - periodically checked with ReceiveNextEvent if - redisplay-dont-pause is nil. That normally flushes window buffer - changes for every check, and thus screen update looks waving even - if no input is available. So we disable screen updates during - update of a frame. */ BLOCK_INPUT; - DisableScreenUpdates (); + mac_update_begin (f); UNBLOCK_INPUT; -#endif } @@ -2095,9 +1897,7 @@ x_update_end (f) FRAME_MAC_DISPLAY_INFO (f)->mouse_face_defer = 0; BLOCK_INPUT; -#if TARGET_API_MAC_CARBON - EnableScreenUpdates (); -#endif + mac_update_end (f); XFlush (FRAME_MAC_DISPLAY (f)); UNBLOCK_INPUT; } @@ -2126,6 +1926,8 @@ XTframe_up_to_date (f) dpyinfo->mouse_face_deferred_gc = 0; UNBLOCK_INPUT; } + + mac_frame_up_to_date (f); } } @@ -2881,10 +2683,7 @@ mac_compute_glyph_string_overhangs (s) Rect r; MacFontStruct *font = s->font; -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (s->f); -#endif - SetPortWindowPort (FRAME_MAC_WINDOW (s->f)); + mac_begin_clip (s->f, NULL); TextFont (font->mac_fontnum); TextSize (font->mac_fontsize); @@ -2892,6 +2691,8 @@ mac_compute_glyph_string_overhangs (s) QDTextBounds (s->nchars * 2, (char *)s->char2b, &r); + mac_end_clip (s->f, NULL); + s->right_overhang = r.right > s->width ? r.right - s->width : 0; s->left_overhang = r.left < 0 ? -r.left : 0; } @@ -4250,7 +4051,7 @@ XTring_bell () #endif { BLOCK_INPUT; - SysBeep (1); + mac_alert_sound_play (); XFlush (FRAME_MAC_DISPLAY (f)); UNBLOCK_INPUT; } @@ -4407,7 +4208,7 @@ x_new_focus_frame (dpyinfo, frame) If FRAME has focus and there exists more than one frame, puts a FOCUS_IN_EVENT into *BUFP. */ -static void +void mac_focus_changed (type, dpyinfo, frame, bufp) int type; struct mac_display_info *dpyinfo; @@ -4442,29 +4243,6 @@ mac_focus_changed (type, dpyinfo, frame, bufp) } } -/* The focus may have changed. Figure out if it is a real focus change, - by checking both FocusIn/Out and Enter/LeaveNotify events. - - Returns FOCUS_IN_EVENT event in *BUFP. */ - -static void -x_detect_focus_change (dpyinfo, event, bufp) - struct mac_display_info *dpyinfo; - const EventRecord *event; - struct input_event *bufp; -{ - struct frame *frame; - - frame = mac_window_to_frame ((WindowRef) event->message); - if (! frame) - return; - - /* On Mac, this is only called from focus events, so no switch needed. */ - mac_focus_changed ((event->modifiers & activeFlag), - dpyinfo, frame, bufp); -} - - /* Handle an event saying the mouse has moved out of an Emacs frame. */ void @@ -4553,27 +4331,20 @@ x_get_keysym_name (keysym) static Point last_mouse_motion_position; static Lisp_Object last_mouse_motion_frame; -static int +int note_mouse_movement (frame, pos) FRAME_PTR frame; Point *pos; { struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (frame); -#if TARGET_API_MAC_CARBON Rect r; -#endif last_mouse_movement_time = TickCount () * (1000 / 60); /* to milliseconds */ last_mouse_motion_position = *pos; XSETFRAME (last_mouse_motion_frame, frame); if (frame == dpyinfo->mouse_face_mouse_frame -#if TARGET_API_MAC_CARBON - && !PtInRect (*pos, GetWindowPortBounds (FRAME_MAC_WINDOW (frame), &r)) -#else - && !PtInRect (*pos, &FRAME_MAC_WINDOW (frame)->portRect) -#endif - ) + && !PtInRect (*pos, mac_get_frame_bounds (frame, &r))) { /* This case corresponds to LeaveNotify in X11. If we move outside the frame, then we're certainly no longer on any text @@ -4619,7 +4390,7 @@ redo_mouse_highlight () } -static struct frame * +struct frame * mac_focus_frame (dpyinfo) struct mac_display_info *dpyinfo; { @@ -4695,14 +4466,7 @@ XTmouse_position (fp, insist, bar_window, part, x, y, time) the frame are divided into. */ Point mouse_pos; -#if TARGET_API_MAC_CARBON - GetGlobalMouse (&mouse_pos); - mouse_pos.h -= f1->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f1); - mouse_pos.v -= f1->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f1); -#else - SetPortWindowPort (FRAME_MAC_WINDOW (f1)); - GetMouse (&mouse_pos); -#endif + mac_get_frame_mouse (f1, &mouse_pos); remember_mouse_glyph (f1, mouse_pos.h, mouse_pos.v, &last_mouse_glyph); last_mouse_glyph_frame = f1; @@ -4721,487 +4485,85 @@ XTmouse_position (fp, insist, bar_window, part, x, y, time) /************************************************************************ - Toolkit scroll bars + Scroll bars, general ************************************************************************/ -#ifdef USE_TOOLKIT_SCROLL_BARS - -static pascal void scroll_bar_timer_callback P_ ((EventLoopTimerRef, void *)); -static OSStatus install_scroll_bar_timer P_ ((void)); -static OSStatus set_scroll_bar_timer P_ ((EventTimerInterval)); -static int control_part_code_to_scroll_bar_part P_ ((ControlPartCode)); -static void construct_scroll_bar_click P_ ((struct scroll_bar *, int, - struct input_event *)); -static OSStatus get_control_part_bounds P_ ((ControlRef, ControlPartCode, - Rect *)); -static void x_scroll_bar_handle_press P_ ((struct scroll_bar *, - ControlPartCode, Point, - struct input_event *)); -static void x_scroll_bar_handle_release P_ ((struct scroll_bar *, - struct input_event *)); -static void x_scroll_bar_handle_drag P_ ((WindowRef, struct scroll_bar *, - Point, struct input_event *)); -static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *, - int, int, int)); - -/* Last scroll bar part sent in x_scroll_bar_handle_*. */ - -static int last_scroll_bar_part; - -static EventLoopTimerRef scroll_bar_timer; - -static int scroll_bar_timer_event_posted_p; - -#define SCROLL_BAR_FIRST_DELAY 0.5 -#define SCROLL_BAR_CONTINUOUS_DELAY (1.0 / 15) - -static pascal void -scroll_bar_timer_callback (timer, data) - EventLoopTimerRef timer; - void *data; -{ - OSStatus err; - - err = mac_post_mouse_moved_event (); - if (err == noErr) - scroll_bar_timer_event_posted_p = 1; -} +/* Create a scroll bar and return the scroll bar vector for it. W is + the Emacs window on which to create the scroll bar. TOP, LEFT, + WIDTH and HEIGHT are the pixel coordinates and dimensions of the + scroll bar. */ -static OSStatus -install_scroll_bar_timer () +static struct scroll_bar * +x_scroll_bar_create (w, top, left, width, height, disp_top, disp_height) + struct window *w; + int top, left, width, height, disp_top, disp_height; { - static EventLoopTimerUPP scroll_bar_timer_callbackUPP = NULL; + struct frame *f = XFRAME (w->frame); + struct scroll_bar *bar + = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil)); + Rect r; - if (scroll_bar_timer_callbackUPP == NULL) - scroll_bar_timer_callbackUPP = - NewEventLoopTimerUPP (scroll_bar_timer_callback); + BLOCK_INPUT; - if (scroll_bar_timer == NULL) - /* Mac OS X and CarbonLib 1.5 and later allow us to specify - kEventDurationForever as delays. */ - return - InstallEventLoopTimer (GetCurrentEventLoop (), - kEventDurationForever, kEventDurationForever, - scroll_bar_timer_callbackUPP, NULL, - &scroll_bar_timer); -} + XSETWINDOW (bar->window, w); + XSETINT (bar->top, top); + XSETINT (bar->left, left); + XSETINT (bar->width, width); + XSETINT (bar->height, height); +#ifdef MAC_OSX + bar->fringe_extended_p = Qnil; +#endif + bar->redraw_needed_p = Qnil; -static OSStatus -set_scroll_bar_timer (delay) - EventTimerInterval delay; -{ - if (scroll_bar_timer == NULL) - install_scroll_bar_timer (); + SetRect (&r, left, disp_top, left + width, disp_top + disp_height); + mac_create_scroll_bar (bar, &r, +#ifdef USE_TOOLKIT_SCROLL_BARS + false +#else + width < disp_height +#endif + ); - scroll_bar_timer_event_posted_p = 0; + /* Add bar to its frame's list of scroll bars. */ + bar->next = FRAME_SCROLL_BARS (f); + bar->prev = Qnil; + XSETVECTOR (FRAME_SCROLL_BARS (f), bar); + if (!NILP (bar->next)) + XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar); - return SetEventLoopTimerNextFireTime (scroll_bar_timer, delay); + UNBLOCK_INPUT; + return bar; } -static int -control_part_code_to_scroll_bar_part (part_code) - ControlPartCode part_code; -{ - switch (part_code) - { - case kControlUpButtonPart: return scroll_bar_up_arrow; - case kControlDownButtonPart: return scroll_bar_down_arrow; - case kControlPageUpPart: return scroll_bar_above_handle; - case kControlPageDownPart: return scroll_bar_below_handle; - case kControlIndicatorPart: return scroll_bar_handle; - } - return -1; -} +/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to + nil. */ static void -construct_scroll_bar_click (bar, part, bufp) +x_scroll_bar_remove (bar) struct scroll_bar *bar; - int part; - struct input_event *bufp; { - bufp->kind = SCROLL_BAR_CLICK_EVENT; - bufp->frame_or_window = bar->window; - bufp->arg = Qnil; - bufp->part = part; - bufp->code = 0; - XSETINT (bufp->x, 0); - XSETINT (bufp->y, 0); - bufp->modifiers = 0; -} + BLOCK_INPUT; -static OSStatus -get_control_part_bounds (ch, part_code, rect) - ControlRef ch; - ControlPartCode part_code; - Rect *rect; -{ - RgnHandle region = NewRgn (); - OSStatus err; + /* Destroy the Mac scroll bar control */ + mac_dispose_scroll_bar (bar); - err = GetControlRegion (ch, part_code, region); - if (err == noErr) - GetRegionBounds (region, rect); - DisposeRgn (region); + /* Disassociate this scroll bar from its window. */ + XWINDOW (bar->window)->vertical_scroll_bar = Qnil; - return err; + UNBLOCK_INPUT; } -static void -x_scroll_bar_handle_press (bar, part_code, mouse_pos, bufp) - struct scroll_bar *bar; - ControlPartCode part_code; - Point mouse_pos; - struct input_event *bufp; -{ - int part = control_part_code_to_scroll_bar_part (part_code); - - if (part < 0) - return; - - if (part != scroll_bar_handle) - { - construct_scroll_bar_click (bar, part, bufp); - HiliteControl (SCROLL_BAR_CONTROL_REF (bar), part_code); - set_scroll_bar_timer (SCROLL_BAR_FIRST_DELAY); - bar->dragging = Qnil; - } - else - { - Rect r; - - get_control_part_bounds (SCROLL_BAR_CONTROL_REF (bar), - kControlIndicatorPart, &r); - XSETINT (bar->dragging, - (mouse_pos.v - r.top) - 1); - } - last_scroll_bar_part = part; - tracked_scroll_bar = bar; -} +/* Set the handle of the vertical scroll bar for WINDOW to indicate + that we are displaying PORTION characters out of a total of WHOLE + characters, starting at POSITION. If WINDOW has no scroll bar, + create one. */ static void -x_scroll_bar_handle_release (bar, bufp) - struct scroll_bar *bar; - struct input_event *bufp; -{ - if (last_scroll_bar_part != scroll_bar_handle - || (INTEGERP (bar->dragging) && XINT (bar->dragging) >= 0)) - construct_scroll_bar_click (bar, scroll_bar_end_scroll, bufp); - - HiliteControl (SCROLL_BAR_CONTROL_REF (bar), 0); - set_scroll_bar_timer (kEventDurationForever); - - last_scroll_bar_part = -1; - bar->dragging = Qnil; - tracked_scroll_bar = NULL; -} - -static void -x_scroll_bar_handle_drag (win, bar, mouse_pos, bufp) - WindowRef win; - struct scroll_bar *bar; - Point mouse_pos; - struct input_event *bufp; -{ - ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); - - if (last_scroll_bar_part == scroll_bar_handle) - { - int top, top_range; - Rect r; - - get_control_part_bounds (SCROLL_BAR_CONTROL_REF (bar), - kControlIndicatorPart, &r); - - if (INTEGERP (bar->dragging) && XINT (bar->dragging) < 0) - XSETINT (bar->dragging, - (XINT (bar->dragging) + 1)); - - top = mouse_pos.v - XINT (bar->dragging) - XINT (bar->track_top); - top_range = XINT (bar->track_height) - XINT (bar->min_handle); - - if (top < 0) - top = 0; - if (top > top_range) - top = top_range; - - construct_scroll_bar_click (bar, scroll_bar_handle, bufp); - XSETINT (bufp->x, top); - XSETINT (bufp->y, top_range); - } - else - { - ControlPartCode part_code; - int unhilite_p = 0, part; - - if (ch != FindControlUnderMouse (mouse_pos, win, &part_code)) - unhilite_p = 1; - else - { - part = control_part_code_to_scroll_bar_part (part_code); - - switch (last_scroll_bar_part) - { - case scroll_bar_above_handle: - case scroll_bar_below_handle: - if (part != scroll_bar_above_handle - && part != scroll_bar_below_handle) - unhilite_p = 1; - break; - - case scroll_bar_up_arrow: - case scroll_bar_down_arrow: - if (part != scroll_bar_up_arrow - && part != scroll_bar_down_arrow) - unhilite_p = 1; - break; - } - } - - if (unhilite_p) - HiliteControl (SCROLL_BAR_CONTROL_REF (bar), 0); - else if (part != last_scroll_bar_part - || scroll_bar_timer_event_posted_p) - { - construct_scroll_bar_click (bar, part, bufp); - last_scroll_bar_part = part; - HiliteControl (SCROLL_BAR_CONTROL_REF (bar), part_code); - set_scroll_bar_timer (SCROLL_BAR_CONTINUOUS_DELAY); - } - } -} - -/* Set the thumb size and position of scroll bar BAR. We are currently - displaying PORTION out of a whole WHOLE, and our position POSITION. */ - -static void -x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole) - struct scroll_bar *bar; - int portion, position, whole; -{ - ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); - int value, viewsize, maximum; - - if (XINT (bar->track_height) == 0) - return; - - if (whole <= portion) - value = 0, viewsize = 1, maximum = 0; - else - { - float scale; - - maximum = XINT (bar->track_height) - XINT (bar->min_handle); - scale = (float) maximum / (whole - portion); - value = position * scale + 0.5f; - viewsize = (int) (portion * scale + 0.5f) + XINT (bar->min_handle); - } - - BLOCK_INPUT; - - if (GetControlViewSize (ch) != viewsize - || GetControl32BitValue (ch) != value - || GetControl32BitMaximum (ch) != maximum) - { - /* Temporarily hide the scroll bar to avoid multiple redraws. */ - SetControlVisibility (ch, false, false); - - SetControl32BitMaximum (ch, maximum); - SetControl32BitValue (ch, value); - SetControlViewSize (ch, viewsize); - - SetControlVisibility (ch, true, true); - } - - UNBLOCK_INPUT; -} - -#endif /* USE_TOOLKIT_SCROLL_BARS */ - - - -/************************************************************************ - Scroll bars, general - ************************************************************************/ - -/* Create a scroll bar and return the scroll bar vector for it. W is - the Emacs window on which to create the scroll bar. TOP, LEFT, - WIDTH and HEIGHT are the pixel coordinates and dimensions of the - scroll bar. */ - -static struct scroll_bar * -x_scroll_bar_create (w, top, left, width, height, disp_top, disp_height) - struct window *w; - int top, left, width, height, disp_top, disp_height; -{ - struct frame *f = XFRAME (w->frame); - struct scroll_bar *bar - = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil)); - Rect r; - ControlRef ch; - - BLOCK_INPUT; - - r.left = left; - r.top = disp_top; - r.right = left + width; - r.bottom = disp_top + disp_height; - -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif -#if TARGET_API_MAC_CARBON - ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p", -#ifdef USE_TOOLKIT_SCROLL_BARS - false, -#else - width < disp_height, -#endif - 0, 0, 0, kControlScrollBarProc, (long) bar); -#else - ch = NewControl (FRAME_MAC_WINDOW (f), &r, "\p", width < disp_height, - 0, 0, 0, scrollBarProc, (long) bar); -#endif - SET_SCROLL_BAR_CONTROL_REF (bar, ch); - - XSETWINDOW (bar->window, w); - XSETINT (bar->top, top); - XSETINT (bar->left, left); - XSETINT (bar->width, width); - XSETINT (bar->height, height); - XSETINT (bar->start, 0); - XSETINT (bar->end, 0); - bar->dragging = Qnil; -#ifdef MAC_OSX - bar->fringe_extended_p = Qnil; -#endif - bar->redraw_needed_p = Qnil; -#ifdef USE_TOOLKIT_SCROLL_BARS - bar->track_top = Qnil; - bar->track_height = Qnil; - bar->min_handle = Qnil; -#endif - - /* Add bar to its frame's list of scroll bars. */ - bar->next = FRAME_SCROLL_BARS (f); - bar->prev = Qnil; - XSETVECTOR (FRAME_SCROLL_BARS (f), bar); - if (!NILP (bar->next)) - XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar); - - UNBLOCK_INPUT; - return bar; -} - - -/* Draw BAR's handle in the proper position. - - If the handle is already drawn from START to END, don't bother - redrawing it, unless REBUILD is non-zero; in that case, always - redraw it. (REBUILD is handy for drawing the handle after expose - events.) - - Normally, we want to constrain the start and end of the handle to - fit inside its rectangle, but if the user is dragging the scroll - bar handle, we want to let them drag it down all the way, so that - the bar's top is as far down as it goes; otherwise, there's no way - to move to the very end of the buffer. */ - -#ifndef USE_TOOLKIT_SCROLL_BARS - -static void -x_scroll_bar_set_handle (bar, start, end, rebuild) - struct scroll_bar *bar; - int start, end; - int rebuild; -{ - int dragging = ! NILP (bar->dragging); - ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); - FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window))); - int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)); - int length = end - start; - - /* If the display is already accurate, do nothing. */ - if (! rebuild - && start == XINT (bar->start) - && end == XINT (bar->end)) - return; - - BLOCK_INPUT; - - /* Make sure the values are reasonable, and try to preserve the - distance between start and end. */ - if (start < 0) - start = 0; - else if (start > top_range) - start = top_range; - end = start + length; - - if (end < start) - end = start; - else if (end > top_range && ! dragging) - end = top_range; - - /* Store the adjusted setting in the scroll bar. */ - XSETINT (bar->start, start); - XSETINT (bar->end, end); - - /* Clip the end position, just for display. */ - if (end > top_range) - end = top_range; - - /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels below - top positions, to make sure the handle is always at least that - many pixels tall. */ - end += VERTICAL_SCROLL_BAR_MIN_HANDLE; - - SetControlMinimum (ch, 0); - /* Don't inadvertently activate deactivated scroll bars */ - if (GetControlMaximum (ch) != -1) - SetControlMaximum (ch, top_range + VERTICAL_SCROLL_BAR_MIN_HANDLE - - (end - start)); - SetControlValue (ch, start); -#if TARGET_API_MAC_CARBON - SetControlViewSize (ch, end - start); -#endif - - UNBLOCK_INPUT; -} - -#endif /* !USE_TOOLKIT_SCROLL_BARS */ - -/* Destroy scroll bar BAR, and set its Emacs window's scroll bar to - nil. */ - -static void -x_scroll_bar_remove (bar) - struct scroll_bar *bar; -{ - FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window))); - - BLOCK_INPUT; - -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - /* Destroy the Mac scroll bar control */ - DisposeControl (SCROLL_BAR_CONTROL_REF (bar)); - - /* Disassociate this scroll bar from its window. */ - XWINDOW (bar->window)->vertical_scroll_bar = Qnil; - - UNBLOCK_INPUT; -} - - -/* Set the handle of the vertical scroll bar for WINDOW to indicate - that we are displaying PORTION characters out of a total of WHOLE - characters, starting at POSITION. If WINDOW has no scroll bar, - create one. */ - -static void -XTset_vertical_scroll_bar (w, portion, whole, position) - struct window *w; - int portion, whole, position; +XTset_vertical_scroll_bar (w, portion, whole, position) + struct window *w; + int portion, whole, position; { struct frame *f = XFRAME (w->frame); struct scroll_bar *bar; @@ -5283,10 +4645,7 @@ XTset_vertical_scroll_bar (w, portion, whole, position) else { /* It may just need to be moved and resized. */ - ControlRef ch; - bar = XSCROLL_BAR (w->vertical_scroll_bar); - ch = SCROLL_BAR_CONTROL_REF (bar); BLOCK_INPUT; @@ -5301,15 +4660,12 @@ XTset_vertical_scroll_bar (w, portion, whole, position) ) { if (!NILP (bar->redraw_needed_p)) - { -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - Draw1Control (SCROLL_BAR_CONTROL_REF (bar)); - } + mac_redraw_scroll_bar (bar); } else { + Rect r; + /* Since toolkit scroll bars are smaller than the space reserved for them on the frame, we have to clear "under" them. */ #ifdef MAC_OSX @@ -5319,28 +4675,16 @@ XTset_vertical_scroll_bar (w, portion, whole, position) #endif mac_clear_area (f, left, top, width, height); -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - HideControl (ch); - MoveControl (ch, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM, disp_top); - SizeControl (ch, sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2, - disp_height); -#ifndef USE_TOOLKIT_SCROLL_BARS - if (sb_width < disp_height) - ShowControl (ch); -#endif + SetRect (&r, sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM, disp_top, + sb_left + sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM, + disp_top + disp_height); + mac_set_scroll_bar_bounds (bar, &r); /* Remember new settings. */ XSETINT (bar->left, sb_left); XSETINT (bar->top, top); XSETINT (bar->width, sb_width); XSETINT (bar->height, height); -#ifdef USE_TOOLKIT_SCROLL_BARS - bar->track_top = Qnil; - bar->track_height = Qnil; - bar->min_handle = Qnil; -#endif } UNBLOCK_INPUT; @@ -5352,51 +4696,6 @@ XTset_vertical_scroll_bar (w, portion, whole, position) bar->redraw_needed_p = Qnil; #ifdef USE_TOOLKIT_SCROLL_BARS - if (NILP (bar->track_top)) - { - if (sb_width >= disp_height -#ifdef MAC_OSX - || sb_width < MAC_AQUA_SMALL_VERTICAL_SCROLL_BAR_WIDTH -#endif - ) - { - XSETINT (bar->track_top, 0); - XSETINT (bar->track_height, 0); - XSETINT (bar->min_handle, 0); - } - else - { - ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); - Rect r0, r1; - - BLOCK_INPUT; - - SetControl32BitMinimum (ch, 0); - SetControl32BitMaximum (ch, 1 << 30); - SetControlViewSize (ch, 1); - - /* Move the scroll bar thumb to the top. */ - SetControl32BitValue (ch, 0); - get_control_part_bounds (ch, kControlIndicatorPart, &r0); - - /* Move the scroll bar thumb to the bottom. */ - SetControl32BitValue (ch, 1 << 30); - get_control_part_bounds (ch, kControlIndicatorPart, &r1); - - UnionRect (&r0, &r1, &r0); - XSETINT (bar->track_top, r0.top); - XSETINT (bar->track_height, r0.bottom - r0.top); - XSETINT (bar->min_handle, r1.bottom - r1.top); - - /* Don't show the scroll bar if its height is not enough to - display the scroll bar thumb. */ - if (r0.bottom - r0.top > 0) - ShowControl (ch); - - UNBLOCK_INPUT; - } - } - x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole); #else /* not USE_TOOLKIT_SCROLL_BARS */ /* Set the scroll bar's current state, unless we're currently being @@ -5524,264 +4823,69 @@ XTjudge_scroll_bars (f) and they should get garbage-collected. */ } +/* The screen has been cleared so we may have changed foreground or + background colors, and the scroll bars may need to be redrawn. + Clear out the scroll bars, and ask for expose events, so we can + redraw them. */ -/* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind - is set to something other than NO_EVENT, it is enqueued. - - This may be called from a signal handler, so we have to ignore GC - mark bits. */ - -static void -x_scroll_bar_handle_click (bar, part_code, er, bufp) - struct scroll_bar *bar; - ControlPartCode part_code; - const EventRecord *er; - struct input_event *bufp; +void +x_scroll_bar_clear (f) + FRAME_PTR f; { - int win_y, top_range; + Lisp_Object bar; - if (! GC_WINDOWP (bar->window)) - abort (); + /* We can have scroll bars even if this is 0, + if we just turned off scroll bar mode. + But in that case we should not clear them. */ + if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)) + for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar); + bar = XSCROLL_BAR (bar)->next) + XSCROLL_BAR (bar)->redraw_needed_p = Qt; +} - bufp->kind = SCROLL_BAR_CLICK_EVENT; - bufp->frame_or_window = bar->window; - bufp->arg = Qnil; + +/*********************************************************************** + Tool-bars + ***********************************************************************/ +#if USE_MAC_TOOLBAR +void +mac_move_window_with_gravity (f, win_gravity, left, top) + struct frame *f; + int win_gravity; + short left, top; +{ + Rect inner, outer; - bar->dragging = Qnil; + mac_get_window_bounds (f, &inner, &outer); - switch (part_code) + switch (win_gravity) { - case kControlUpButtonPart: - bufp->part = scroll_bar_up_arrow; - break; - case kControlDownButtonPart: - bufp->part = scroll_bar_down_arrow; - break; - case kControlPageUpPart: - bufp->part = scroll_bar_above_handle; + case NorthWestGravity: + case WestGravity: + case SouthWestGravity: + left += inner.left - outer.left; break; - case kControlPageDownPart: - bufp->part = scroll_bar_below_handle; + + case NorthGravity: + case CenterGravity: + case SouthGravity: + left += ((inner.left - outer.left) + (inner.right - outer.right)) / 2; break; -#if TARGET_API_MAC_CARBON - default: -#else - case kControlIndicatorPart: -#endif - if (er->what == mouseDown) - bar->dragging = make_number (0); - XSETVECTOR (last_mouse_scroll_bar, bar); - bufp->part = scroll_bar_handle; + + case NorthEastGravity: + case EastGravity: + case SouthEastGravity: + left += inner.right - outer.right; break; } - win_y = XINT (bufp->y) - XINT (bar->top); - top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (0/*dummy*/, XINT (bar->height)); - - win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER; - - win_y -= 24; - - if (! NILP (bar->dragging)) - win_y -= XINT (bar->dragging); - - if (win_y < 0) - win_y = 0; - if (win_y > top_range) - win_y = top_range; - - XSETINT (bufp->x, win_y); - XSETINT (bufp->y, top_range); -} - -#ifndef USE_TOOLKIT_SCROLL_BARS - -/* Handle some mouse motion while someone is dragging the scroll bar. - - This may be called from a signal handler, so we have to ignore GC - mark bits. */ - -static void -x_scroll_bar_note_movement (bar, y_pos, t) - struct scroll_bar *bar; - int y_pos; - Time t; -{ - FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame); - - last_mouse_movement_time = t; - - f->mouse_moved = 1; - XSETVECTOR (last_mouse_scroll_bar, bar); - - /* If we're dragging the bar, display it. */ - if (! GC_NILP (bar->dragging)) - { - /* Where should the handle be now? */ - int new_start = y_pos - 24; - - if (new_start != XINT (bar->start)) - { - int new_end = new_start + (XINT (bar->end) - XINT (bar->start)); - - x_scroll_bar_set_handle (bar, new_start, new_end, 0); - } - } -} - -#endif /* !USE_TOOLKIT_SCROLL_BARS */ - -/* Return information to the user about the current position of the mouse - on the scroll bar. */ - -static void -x_scroll_bar_report_motion (fp, bar_window, part, x, y, time) - FRAME_PTR *fp; - Lisp_Object *bar_window; - enum scroll_bar_part *part; - Lisp_Object *x, *y; - unsigned long *time; -{ - struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar); - ControlRef ch = SCROLL_BAR_CONTROL_REF (bar); -#if TARGET_API_MAC_CARBON - WindowRef wp = GetControlOwner (ch); -#else - WindowRef wp = (*ch)->contrlOwner; -#endif - Point mouse_pos; - struct frame *f = mac_window_to_frame (wp); - int win_y, top_range; - -#if TARGET_API_MAC_CARBON - GetGlobalMouse (&mouse_pos); - mouse_pos.h -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); - mouse_pos.v -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); -#else - SetPortWindowPort (wp); - GetMouse (&mouse_pos); -#endif - - win_y = mouse_pos.v - XINT (bar->top); - top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height)); - - win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER; - - win_y -= 24; - - if (! NILP (bar->dragging)) - win_y -= XINT (bar->dragging); - - if (win_y < 0) - win_y = 0; - if (win_y > top_range) - win_y = top_range; - - *fp = f; - *bar_window = bar->window; - - if (! NILP (bar->dragging)) - *part = scroll_bar_handle; - else if (win_y < XINT (bar->start)) - *part = scroll_bar_above_handle; - else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE) - *part = scroll_bar_handle; - else - *part = scroll_bar_below_handle; - - XSETINT (*x, win_y); - XSETINT (*y, top_range); - - f->mouse_moved = 0; - last_mouse_scroll_bar = Qnil; - - *time = last_mouse_movement_time; -} - - -/* The screen has been cleared so we may have changed foreground or - background colors, and the scroll bars may need to be redrawn. - Clear out the scroll bars, and ask for expose events, so we can - redraw them. */ - -void -x_scroll_bar_clear (f) - FRAME_PTR f; -{ - Lisp_Object bar; - - /* We can have scroll bars even if this is 0, - if we just turned off scroll bar mode. - But in that case we should not clear them. */ - if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)) - for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar); - bar = XSCROLL_BAR (bar)->next) - XSCROLL_BAR (bar)->redraw_needed_p = Qt; -} - - -/*********************************************************************** - Tool-bars - ***********************************************************************/ -#if USE_MAC_TOOLBAR - -/* In identifiers such as function/variable names, Emacs tool bar is - referred to as `tool_bar', and Carbon HIToolbar as `toolbar'. */ - -#define TOOLBAR_IDENTIFIER (CFSTR ("org.gnu.Emacs.toolbar")) -#define TOOLBAR_ICON_ITEM_IDENTIFIER (CFSTR ("org.gnu.Emacs.toolbar.icon")) - -#define TOOLBAR_ITEM_COMMAND_ID_OFFSET 'Tb\0\0' -#define TOOLBAR_ITEM_COMMAND_ID_P(id) \ - (((id) & ~0xffff) == TOOLBAR_ITEM_COMMAND_ID_OFFSET) -#define TOOLBAR_ITEM_COMMAND_ID_VALUE(id) \ - ((id) - TOOLBAR_ITEM_COMMAND_ID_OFFSET) -#define TOOLBAR_ITEM_MAKE_COMMAND_ID(value) \ - ((value) + TOOLBAR_ITEM_COMMAND_ID_OFFSET) - -static int mac_event_to_emacs_modifiers P_ ((EventRef)); -static void mac_handle_origin_change P_ ((struct frame *)); -static OSStatus mac_handle_toolbar_command_event P_ ((EventHandlerCallRef, - EventRef, void *)); - -static void -mac_move_window_with_gravity (f, win_gravity, left, top) - struct frame *f; - int win_gravity; - short left, top; -{ - Rect inner, outer; - - mac_get_window_bounds (f, &inner, &outer); - - switch (win_gravity) - { - case NorthWestGravity: - case WestGravity: - case SouthWestGravity: - left += inner.left - outer.left; - break; - - case NorthGravity: - case CenterGravity: - case SouthGravity: - left += ((inner.left - outer.left) + (inner.right - outer.right)) / 2; - break; - - case NorthEastGravity: - case EastGravity: - case SouthEastGravity: - left += inner.right - outer.right; - break; - } - - switch (win_gravity) - { - case NorthWestGravity: - case NorthGravity: - case NorthEastGravity: - top += inner.top - outer.top; - break; + switch (win_gravity) + { + case NorthWestGravity: + case NorthGravity: + case NorthEastGravity: + top += inner.top - outer.top; + break; case WestGravity: case CenterGravity: @@ -5796,10 +4900,10 @@ mac_move_window_with_gravity (f, win_gravity, left, top) break; } - MoveWindow (FRAME_MAC_WINDOW (f), left, top, false); + mac_move_window (FRAME_MAC_WINDOW (f), left, top, false); } -static void +void mac_get_window_origin_with_gravity (f, win_gravity, left, top) struct frame *f; int win_gravity; @@ -5854,65 +4958,7 @@ mac_get_window_origin_with_gravity (f, win_gravity, left, top) } } -static OSStatus -mac_handle_toolbar_event (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - OSStatus err, result = eventNotHandledErr; - - switch (GetEventKind (event)) - { - case kEventToolbarGetDefaultIdentifiers: - result = noErr; - break; - - case kEventToolbarGetAllowedIdentifiers: - { - CFMutableArrayRef array; - - GetEventParameter (event, kEventParamMutableArray, - typeCFMutableArrayRef, NULL, - sizeof (CFMutableArrayRef), NULL, &array); - CFArrayAppendValue (array, TOOLBAR_ICON_ITEM_IDENTIFIER); - result = noErr; - } - break; - - case kEventToolbarCreateItemWithIdentifier: - { - CFStringRef identifier; - HIToolbarItemRef item = NULL; - - GetEventParameter (event, kEventParamToolbarItemIdentifier, - typeCFStringRef, NULL, - sizeof (CFStringRef), NULL, &identifier); - - if (CFStringCompare (identifier, TOOLBAR_ICON_ITEM_IDENTIFIER, 0) - == kCFCompareEqualTo) - HIToolbarItemCreate (identifier, - kHIToolbarItemAllowDuplicates - | kHIToolbarItemCantBeRemoved, &item); - - if (item) - { - SetEventParameter (event, kEventParamToolbarItem, - typeHIToolbarItemRef, - sizeof (HIToolbarItemRef), &item); - result = noErr; - } - } - break; - - default: - abort (); - } - - return result; -} - -static CGImageRef +CGImageRef mac_image_spec_to_cg_image (f, image) struct frame *f; Lisp_Object image; @@ -5929,436 +4975,101 @@ mac_image_spec_to_cg_image (f, image) return img->data.ptr_val; } } +#endif /* USE_MAC_TOOLBAR */ -/* Create a tool bar for frame F. */ - -static OSStatus -mac_create_frame_tool_bar (f) - FRAME_PTR f; -{ - OSStatus err; - HIToolbarRef toolbar; + +/*********************************************************************** + Text Cursor + ***********************************************************************/ - err = HIToolbarCreate (TOOLBAR_IDENTIFIER, kHIToolbarNoAttributes, - &toolbar); - if (err == noErr) - { - static const EventTypeSpec specs[] = - {{kEventClassToolbar, kEventToolbarGetDefaultIdentifiers}, - {kEventClassToolbar, kEventToolbarGetAllowedIdentifiers}, - {kEventClassToolbar, kEventToolbarCreateItemWithIdentifier}}; +/* Set clipping for output in glyph row ROW. W is the window in which + we operate. GC is the graphics context to set clipping in. - err = InstallEventHandler (HIObjectGetEventTarget (toolbar), - mac_handle_toolbar_event, - GetEventTypeCount (specs), specs, - f, NULL); - } + ROW may be a text row or, e.g., a mode line. Text rows must be + clipped to the interior of the window dedicated to text display, + mode lines must be clipped to the whole window. */ - if (err == noErr) - err = HIToolbarSetDisplayMode (toolbar, kHIToolbarDisplayModeIconOnly); - if (err == noErr) - { - static const EventTypeSpec specs[] = - {{kEventClassCommand, kEventCommandProcess}}; +static void +x_clip_to_row (w, row, area, gc) + struct window *w; + struct glyph_row *row; + int area; + GC gc; +{ + struct frame *f = XFRAME (WINDOW_FRAME (w)); + Rect clip_rect; + int window_x, window_y, window_width; - err = InstallWindowEventHandler (FRAME_MAC_WINDOW (f), - mac_handle_toolbar_command_event, - GetEventTypeCount (specs), - specs, f, NULL); - } - if (err == noErr) - err = SetWindowToolbar (FRAME_MAC_WINDOW (f), toolbar); + window_box (w, area, &window_x, &window_y, &window_width, 0); - if (toolbar) - CFRelease (toolbar); + clip_rect.left = window_x; + clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y); + clip_rect.top = max (clip_rect.top, window_y); + clip_rect.right = clip_rect.left + window_width; + clip_rect.bottom = clip_rect.top + row->visible_height; - return err; + mac_set_clip_rectangles (FRAME_MAC_DISPLAY (f), gc, &clip_rect, 1); } -/* Update the tool bar for frame F. Add new buttons and remove old. */ -void -update_frame_tool_bar (f) - FRAME_PTR f; +/* Draw a hollow box cursor on window W in glyph row ROW. */ + +static void +x_draw_hollow_cursor (w, row) + struct window *w; + struct glyph_row *row; { - HIToolbarRef toolbar = NULL; - short left, top; - CFArrayRef old_items = NULL; - CFIndex old_count; - int i, pos, win_gravity = f->output_data.mac->toolbar_win_gravity; + struct frame *f = XFRAME (WINDOW_FRAME (w)); struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); + Display *dpy = FRAME_MAC_DISPLAY (f); + int x, y, wd, h; + XGCValues xgcv; + struct glyph *cursor_glyph; + GC gc; - BLOCK_INPUT; - - GetWindowToolbar (FRAME_MAC_WINDOW (f), &toolbar); - if (toolbar == NULL) - { - mac_create_frame_tool_bar (f); - GetWindowToolbar (FRAME_MAC_WINDOW (f), &toolbar); - if (toolbar == NULL) - goto out; - if (win_gravity >= NorthWestGravity && win_gravity <= SouthEastGravity) - mac_get_window_origin_with_gravity (f, win_gravity, &left, &top); - } - - HIToolbarCopyItems (toolbar, &old_items); - if (old_items == NULL) - goto out; - - old_count = CFArrayGetCount (old_items); - pos = 0; - for (i = 0; i < f->n_tool_bar_items; ++i) - { -#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX)) + /* Get the glyph the cursor is on. If we can't tell because + the current matrix is invalid or such, give up. */ + cursor_glyph = get_phys_cursor_glyph (w); + if (cursor_glyph == NULL) + return; - int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P)); - int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)); - int idx; - Lisp_Object image; - CGImageRef cg_image; - CFStringRef label; - HIToolbarItemRef item; + /* Compute frame-relative coordinates for phys cursor. */ + get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h); + wd = w->phys_cursor_width; - /* If image is a vector, choose the image according to the - button state. */ - image = PROP (TOOL_BAR_ITEM_IMAGES); - if (VECTORP (image)) - { - if (enabled_p) - idx = (selected_p - ? TOOL_BAR_IMAGE_ENABLED_SELECTED - : TOOL_BAR_IMAGE_ENABLED_DESELECTED); - else - idx = (selected_p - ? TOOL_BAR_IMAGE_DISABLED_SELECTED - : TOOL_BAR_IMAGE_DISABLED_DESELECTED); + /* The foreground of cursor_gc is typically the same as the normal + background color, which can cause the cursor box to be invisible. */ + xgcv.foreground = f->output_data.mac->cursor_pixel; + if (dpyinfo->scratch_cursor_gc) + XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv); + else + dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_MAC_WINDOW (f), + GCForeground, &xgcv); + gc = dpyinfo->scratch_cursor_gc; - xassert (ASIZE (image) >= idx); - image = AREF (image, idx); - } - else - idx = -1; + /* Set clipping, draw the rectangle, and reset clipping again. */ + x_clip_to_row (w, row, TEXT_AREA, gc); + mac_draw_rectangle (f, gc, x, y, wd, h - 1); + mac_reset_clip_rectangles (dpy, gc); +} - cg_image = mac_image_spec_to_cg_image (f, image); - /* Ignore invalid image specifications. */ - if (cg_image == NULL) - continue; - label = cfstring_create_with_string (PROP (TOOL_BAR_ITEM_CAPTION)); - if (label == NULL) - label = CFSTR (""); +/* Draw a bar cursor on window W in glyph row ROW. - if (pos < old_count) - { - CGImageRef old_cg_image = NULL; - CFStringRef old_label = NULL; - Boolean old_enabled_p; + Implementation note: One would like to draw a bar cursor with an + angle equal to the one given by the font property XA_ITALIC_ANGLE. + Unfortunately, I didn't find a font yet that has this property set. + --gerd. */ - item = (HIToolbarItemRef) CFArrayGetValueAtIndex (old_items, pos); - - HIToolbarItemCopyImage (item, &old_cg_image); - if (cg_image != old_cg_image) - HIToolbarItemSetImage (item, cg_image); - CGImageRelease (old_cg_image); - - HIToolbarItemCopyLabel (item, &old_label); - if (CFStringCompare (label, old_label, 0) != kCFCompareEqualTo) - HIToolbarItemSetLabel (item, label); - CFRelease (old_label); - - old_enabled_p = HIToolbarItemIsEnabled (item); - if ((enabled_p || idx >= 0) != old_enabled_p) - HIToolbarItemSetEnabled (item, (enabled_p || idx >= 0)); - } - else - { - item = NULL; - HIToolbarCreateItemWithIdentifier (toolbar, - TOOLBAR_ICON_ITEM_IDENTIFIER, - NULL, &item); - if (item) - { - HIToolbarItemSetImage (item, cg_image); - HIToolbarItemSetLabel (item, label); - HIToolbarItemSetEnabled (item, (enabled_p || idx >= 0)); - HIToolbarAppendItem (toolbar, item); - CFRelease (item); - } - } - - CFRelease (label); - if (item) - { - HIToolbarItemSetCommandID (item, TOOLBAR_ITEM_MAKE_COMMAND_ID (i)); - pos++; - } - } - - CFRelease (old_items); - - while (pos < old_count) - HIToolbarRemoveItemAtIndex (toolbar, --old_count); - - ShowHideWindowToolbar (FRAME_MAC_WINDOW (f), true, - !win_gravity && f == mac_focus_frame (dpyinfo)); - /* Mac OS X 10.3 does not issue kEventWindowBoundsChanged events on - toolbar visibility change. */ - mac_handle_origin_change (f); - if (win_gravity >= NorthWestGravity && win_gravity <= SouthEastGravity) - { - mac_move_window_with_gravity (f, win_gravity, left, top); - /* If the title bar is completely outside the screen, adjust the - position. */ - ConstrainWindowToScreen (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn, - kWindowConstrainMoveRegardlessOfFit - | kWindowConstrainAllowPartial, NULL, NULL); - f->output_data.mac->toolbar_win_gravity = 0; - } - - out: - UNBLOCK_INPUT; -} - -/* Hide the tool bar on frame F. Unlike the counterpart on GTK+, it - doesn't deallocate the resources. */ - -void -free_frame_tool_bar (f) - FRAME_PTR f; -{ - if (IsWindowToolbarVisible (FRAME_MAC_WINDOW (f))) - { - struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); - - BLOCK_INPUT; - ShowHideWindowToolbar (FRAME_MAC_WINDOW (f), false, - (NILP (find_symbol_value - (intern ("frame-notice-user-settings"))) - && f == mac_focus_frame (dpyinfo))); - /* Mac OS X 10.3 does not issue kEventWindowBoundsChanged events - on toolbar visibility change. */ - mac_handle_origin_change (f); - UNBLOCK_INPUT; - } -} - -static void -mac_tool_bar_note_mouse_movement (f, event) - struct frame *f; - EventRef event; -{ - OSStatus err; - struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); - int mouse_down_p; - WindowRef window; - WindowPartCode part_code; - HIViewRef item_view; - UInt32 command_id; - - mouse_down_p = (dpyinfo->grabbed - && f == last_mouse_frame - && FRAME_LIVE_P (f)); - if (mouse_down_p) - return; - - err = GetEventParameter (event, kEventParamWindowRef, typeWindowRef, NULL, - sizeof (WindowRef), NULL, &window); - if (err != noErr || window != FRAME_MAC_WINDOW (f)) - return; - - err = GetEventParameter (event, kEventParamWindowPartCode, - typeWindowPartCode, NULL, - sizeof (WindowPartCode), NULL, &part_code); - if (err != noErr || part_code != inStructure) - return; - - err = HIViewGetViewForMouseEvent (HIViewGetRoot (window), event, &item_view); - /* This doesn't work on Mac OS X 10.2. On Mac OS X 10.3 and 10.4, a - toolbar item view seems to have the same command ID with that of - the toolbar item. */ - if (err == noErr) - err = GetControlCommandID (item_view, &command_id); - if (err == noErr && TOOLBAR_ITEM_COMMAND_ID_P (command_id)) - { - int i = TOOLBAR_ITEM_COMMAND_ID_VALUE (command_id); - - if (i < f->n_tool_bar_items) - { - HIRect bounds; - HIViewRef content_view; - - err = HIViewGetBounds (item_view, &bounds); - if (err == noErr) - err = HIViewFindByID (HIViewGetRoot (window), - kHIViewWindowContentID, &content_view); - if (err == noErr) - err = HIViewConvertRect (&bounds, item_view, content_view); - if (err == noErr) - SetRect (&last_mouse_glyph, - CGRectGetMinX (bounds), CGRectGetMinY (bounds), - CGRectGetMaxX (bounds), CGRectGetMaxY (bounds)); - - help_echo_object = help_echo_window = Qnil; - help_echo_pos = -1; - help_echo_string = PROP (TOOL_BAR_ITEM_HELP); - if (NILP (help_echo_string)) - help_echo_string = PROP (TOOL_BAR_ITEM_CAPTION); - } - } -} - -static OSStatus -mac_handle_toolbar_command_event (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - OSStatus err, result = eventNotHandledErr; - struct frame *f = (struct frame *) data; - HICommand command; - - err = GetEventParameter (event, kEventParamDirectObject, - typeHICommand, NULL, - sizeof (HICommand), NULL, &command); - if (err != noErr) - return result; - - switch (GetEventKind (event)) - { - case kEventCommandProcess: - if (!TOOLBAR_ITEM_COMMAND_ID_P (command.commandID)) - result = CallNextEventHandler (next_handler, event); - else - { - int i = TOOLBAR_ITEM_COMMAND_ID_VALUE (command.commandID); - - if (i < f->n_tool_bar_items - && !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P))) - { - Lisp_Object frame; - struct input_event buf; - - EVENT_INIT (buf); - - XSETFRAME (frame, f); - buf.kind = TOOL_BAR_EVENT; - buf.frame_or_window = frame; - buf.arg = frame; - kbd_buffer_store_event (&buf); - - buf.kind = TOOL_BAR_EVENT; - buf.frame_or_window = frame; - buf.arg = PROP (TOOL_BAR_ITEM_KEY); - buf.modifiers = mac_event_to_emacs_modifiers (event); - kbd_buffer_store_event (&buf); - - result = noErr; - } - } - break; - - default: - abort (); - } -#undef PROP - - return result; -} -#endif /* USE_MAC_TOOLBAR */ - - -/*********************************************************************** - Text Cursor - ***********************************************************************/ - -/* Set clipping for output in glyph row ROW. W is the window in which - we operate. GC is the graphics context to set clipping in. - - ROW may be a text row or, e.g., a mode line. Text rows must be - clipped to the interior of the window dedicated to text display, - mode lines must be clipped to the whole window. */ - -static void -x_clip_to_row (w, row, area, gc) - struct window *w; - struct glyph_row *row; - int area; - GC gc; -{ - struct frame *f = XFRAME (WINDOW_FRAME (w)); - Rect clip_rect; - int window_x, window_y, window_width; - - window_box (w, area, &window_x, &window_y, &window_width, 0); - - clip_rect.left = window_x; - clip_rect.top = WINDOW_TO_FRAME_PIXEL_Y (w, row->y); - clip_rect.top = max (clip_rect.top, window_y); - clip_rect.right = clip_rect.left + window_width; - clip_rect.bottom = clip_rect.top + row->visible_height; - - mac_set_clip_rectangles (FRAME_MAC_DISPLAY (f), gc, &clip_rect, 1); -} - - -/* Draw a hollow box cursor on window W in glyph row ROW. */ - -static void -x_draw_hollow_cursor (w, row) - struct window *w; - struct glyph_row *row; -{ - struct frame *f = XFRAME (WINDOW_FRAME (w)); - struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); - Display *dpy = FRAME_MAC_DISPLAY (f); - int x, y, wd, h; - XGCValues xgcv; - struct glyph *cursor_glyph; - GC gc; - - /* Get the glyph the cursor is on. If we can't tell because - the current matrix is invalid or such, give up. */ - cursor_glyph = get_phys_cursor_glyph (w); - if (cursor_glyph == NULL) - return; - - /* Compute frame-relative coordinates for phys cursor. */ - get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h); - wd = w->phys_cursor_width; - - /* The foreground of cursor_gc is typically the same as the normal - background color, which can cause the cursor box to be invisible. */ - xgcv.foreground = f->output_data.mac->cursor_pixel; - if (dpyinfo->scratch_cursor_gc) - XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv); - else - dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_MAC_WINDOW (f), - GCForeground, &xgcv); - gc = dpyinfo->scratch_cursor_gc; - - /* Set clipping, draw the rectangle, and reset clipping again. */ - x_clip_to_row (w, row, TEXT_AREA, gc); - mac_draw_rectangle (f, gc, x, y, wd, h - 1); - mac_reset_clip_rectangles (dpy, gc); -} - - -/* Draw a bar cursor on window W in glyph row ROW. - - Implementation note: One would like to draw a bar cursor with an - angle equal to the one given by the font property XA_ITALIC_ANGLE. - Unfortunately, I didn't find a font yet that has this property set. - --gerd. */ - -static void -x_draw_bar_cursor (w, row, width, kind) - struct window *w; - struct glyph_row *row; - int width; - enum text_cursor_kinds kind; -{ - struct frame *f = XFRAME (w->frame); - struct glyph *cursor_glyph; +static void +x_draw_bar_cursor (w, row, width, kind) + struct window *w; + struct glyph_row *row; + int width; + enum text_cursor_kinds kind; +{ + struct frame *f = XFRAME (w->frame); + struct glyph *cursor_glyph; /* If cursor is out of bounds, don't draw garbage. This can happen in mini-buffer windows when switching between echo area glyphs @@ -6691,32 +5402,13 @@ xim_close_dpy (dpyinfo) void -mac_get_window_bounds (f, inner, outer) - struct frame *f; - Rect *inner, *outer; -{ -#if TARGET_API_MAC_CARBON - GetWindowBounds (FRAME_MAC_WINDOW (f), kWindowContentRgn, inner); - GetWindowBounds (FRAME_MAC_WINDOW (f), kWindowStructureRgn, outer); -#else /* not TARGET_API_MAC_CARBON */ - RgnHandle region = NewRgn (); - - GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowContentRgn, region); - *inner = (*region)->rgnBBox; - GetWindowRegion (FRAME_MAC_WINDOW (f), kWindowStructureRgn, region); - *outer = (*region)->rgnBBox; - DisposeRgn (region); -#endif /* not TARGET_API_MAC_CARBON */ -} - -static void mac_handle_origin_change (f) struct frame *f; { x_real_positions (f, &f->left_pos, &f->top_pos); } -static void +void mac_handle_size_change (f, pixelwidth, pixelheight) struct frame *f; int pixelwidth, pixelheight; @@ -6749,14 +5441,7 @@ mac_handle_size_change (f, pixelwidth, pixelheight) cancel_mouse_face (f); #if TARGET_API_MAC_CARBON - if (f->output_data.mac->hourglass_control) - { -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - MoveControl (f->output_data.mac->hourglass_control, - pixelwidth - HOURGLASS_WIDTH, 0); - } + mac_reposition_hourglass (f); #endif } } @@ -6836,7 +5521,7 @@ x_set_offset (f, xoff, yoff, change_gravity) x_wm_set_size_hint (f, (long) 0, 0); #if TARGET_API_MAC_CARBON - MoveWindowStructure (FRAME_MAC_WINDOW (f), f->left_pos, f->top_pos); + mac_move_window_structure (FRAME_MAC_WINDOW (f), f->left_pos, f->top_pos); /* If the title bar is completely outside the screen, adjust the position. */ ConstrainWindowToScreen (FRAME_MAC_WINDOW (f), kWindowTitleBarRgn, @@ -6916,7 +5601,7 @@ x_set_window_size (f, change_gravity, cols, rows) f->win_gravity = NorthWestGravity; x_wm_set_size_hint (f, (long) 0, 0); - SizeWindow (FRAME_MAC_WINDOW (f), pixelwidth, pixelheight, 0); + mac_size_window (FRAME_MAC_WINDOW (f), pixelwidth, pixelheight, 0); #if TARGET_API_MAC_CARBON if (!NILP (tip_frame) && f == XFRAME (tip_frame)) @@ -6965,10 +5650,8 @@ x_set_mouse_pixel_position (f, pix_x, pix_y) int pix_x, pix_y; { #ifdef MAC_OSX - pix_x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); - pix_y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); - BLOCK_INPUT; + mac_convert_frame_point_to_global (f, &pix_x, &pix_y); CGWarpMouseCursorPosition (CGPointMake (pix_x, pix_y)); UNBLOCK_INPUT; #else @@ -7015,7 +5698,7 @@ x_raise_frame (f) if (f->async_visible) { BLOCK_INPUT; - BringToFront (FRAME_MAC_WINDOW (f)); + mac_bring_window_to_front (FRAME_MAC_WINDOW (f)); UNBLOCK_INPUT; } } @@ -7029,7 +5712,7 @@ x_lower_frame (f) if (f->async_visible) { BLOCK_INPUT; - SendBehind (FRAME_MAC_WINDOW (f), NULL); + mac_send_window_behind (FRAME_MAC_WINDOW (f), NULL); UNBLOCK_INPUT; } } @@ -7047,17 +5730,17 @@ XTframe_raise_lower (f, raise_flag) /* Change of visibility. */ -static void +void mac_handle_visibility_change (f) struct frame *f; { - WindowRef wp = FRAME_MAC_WINDOW (f); + Window wp = FRAME_MAC_WINDOW (f); int visible = 0, iconified = 0; struct input_event buf; - if (IsWindowVisible (wp)) + if (mac_is_window_visible (wp)) { - if (IsWindowCollapsed (wp)) + if (mac_is_window_collapsed (wp)) iconified = 1; else visible = 1; @@ -7123,8 +5806,8 @@ x_make_frame_visible (f) f->output_data.mac->asked_for_visible = 1; - CollapseWindow (FRAME_MAC_WINDOW (f), false); - ShowWindow (FRAME_MAC_WINDOW (f)); + mac_collapse_window (FRAME_MAC_WINDOW (f), false); + mac_show_window (FRAME_MAC_WINDOW (f)); } XFlush (FRAME_MAC_DISPLAY (f)); @@ -7202,7 +5885,7 @@ x_make_frame_invisible (f) x_wm_set_size_hint (f, (long) 0, 1); #endif - HideWindow (FRAME_MAC_WINDOW (f)); + mac_hide_window (FRAME_MAC_WINDOW (f)); UNBLOCK_INPUT; @@ -7236,9 +5919,9 @@ x_iconify_frame (f) FRAME_SAMPLE_VISIBILITY (f); if (! FRAME_VISIBLE_P (f)) - ShowWindow (FRAME_MAC_WINDOW (f)); + mac_show_window (FRAME_MAC_WINDOW (f)); - err = CollapseWindow (FRAME_MAC_WINDOW (f), true); + err = mac_collapse_window (FRAME_MAC_WINDOW (f), true); UNBLOCK_INPUT; @@ -7258,35 +5941,14 @@ x_free_frame_resources (f) struct frame *f; { struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); - WindowRef wp = FRAME_MAC_WINDOW (f); + Window wp = FRAME_MAC_WINDOW (f); BLOCK_INPUT; - if (wp != tip_window) - remove_window_handler (wp); - -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - DisposeWindow (wp); - if (wp == tip_window) - /* Neither WaitNextEvent nor ReceiveNextEvent receives `window - closed' event. So we reset tip_window here. */ - tip_window = NULL; - - free_frame_menubar (f); - - if (FRAME_FACE_CACHE (f)) - free_frame_faces (f); - - x_free_gcs (f); - - if (FRAME_SIZE_HINTS (f)) - xfree (FRAME_SIZE_HINTS (f)); - - xfree (f->output_data.mac); - f->output_data.mac = NULL; - + /* AppKit version of mac_dispose_frame_window, which is implemented + as -[NSWindow close], will change the focus to the next window + during its call. So, unlike other platforms, we clean up the + focus-related variables before calling mac_dispose_frame_window. */ if (f == dpyinfo->x_focus_frame) { dpyinfo->x_focus_frame = 0; @@ -7310,6 +5972,25 @@ x_free_frame_resources (f) dpyinfo->mouse_face_mouse_frame = 0; } + mac_dispose_frame_window (f); + if (wp == tip_window) + /* Neither WaitNextEvent nor ReceiveNextEvent receives `window + closed' event. So we reset tip_window here. */ + tip_window = NULL; + + free_frame_menubar (f); + + if (FRAME_FACE_CACHE (f)) + free_frame_faces (f); + + x_free_gcs (f); + + if (FRAME_SIZE_HINTS (f)) + xfree (FRAME_SIZE_HINTS (f)); + + xfree (f->output_data.mac); + f->output_data.mac = NULL; + UNBLOCK_INPUT; } @@ -7823,6 +6504,9 @@ static Lisp_Object atsu_font_id_hash; static Lisp_Object fm_style_face_attributes_alist; extern Lisp_Object QCfamily, QCweight, QCslant, Qnormal, Qbold, Qitalic; #endif +#if USE_MAC_FONT_PANEL +Lisp_Object Qpanel_closed, Qselection; +#endif /* Alist linking character set strings to Mac text encoding and Emacs coding system. */ @@ -8968,10 +7652,7 @@ mac_load_query_font (f, fontname) FontInfo the_fontinfo; int is_two_byte_font; -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - SetPortWindowPort (FRAME_MAC_WINDOW (f)); + mac_begin_clip (f, NULL); TextFont (fontnum); TextSize (size); @@ -9058,6 +7739,8 @@ mac_load_query_font (f, fontname) for (c = 0x21, pcm = space_bounds + 1; c <= 0xff; c++, pcm++) mac_query_char_extents (NULL, c, NULL, NULL, pcm, NULL); } + + mac_end_clip (f, NULL); } if (space_bounds) @@ -9386,160 +8069,8 @@ x_find_ccl_program (fontp) } } -#if USE_MAC_FONT_PANEL -/* Whether Font Panel has been shown before. The first call to font - panel functions (FPIsFontPanelVisible, SetFontInfoForSelection) is - slow. This variable is used for deferring such a call as much as - possible. */ -static int font_panel_shown_p = 0; - -extern Lisp_Object Qfont; -static Lisp_Object Qpanel_closed, Qselection; - -static OSStatus mac_store_event_ref_as_apple_event P_ ((AEEventClass, AEEventID, - Lisp_Object, - Lisp_Object, - EventRef, UInt32, - const EventParamName *, - const EventParamType *)); - -int -mac_font_panel_visible_p () -{ - return font_panel_shown_p && FPIsFontPanelVisible (); -} - -static pascal OSStatus -mac_handle_font_event (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - OSStatus result, err; - Lisp_Object id_key; - int num_params; - const EventParamName *names; - const EventParamType *types; - static const EventParamName names_sel[] = {kEventParamATSUFontID, - kEventParamATSUFontSize, - kEventParamFMFontFamily, - kEventParamFMFontStyle, - kEventParamFMFontSize, - kEventParamFontColor}; - static const EventParamType types_sel[] = {typeATSUFontID, - typeATSUSize, - typeFMFontFamily, - typeFMFontStyle, - typeFMFontSize, - typeFontColor}; - - result = CallNextEventHandler (next_handler, event); - if (result != eventNotHandledErr) - return result; - - switch (GetEventKind (event)) - { - case kEventFontPanelClosed: - id_key = Qpanel_closed; - num_params = 0; - names = NULL; - types = NULL; - break; - - case kEventFontSelection: - id_key = Qselection; - num_params = sizeof (names_sel) / sizeof (names_sel[0]); - names = names_sel; - types = types_sel; - break; - } - - err = mac_store_event_ref_as_apple_event (0, 0, Qfont, id_key, - event, num_params, - names, types); - if (err == noErr) - result = noErr; - - return result; -} - -OSStatus -mac_show_hide_font_panel () -{ - if (!font_panel_shown_p) - { - OSStatus err; - - static const EventTypeSpec specs[] = - {{kEventClassFont, kEventFontPanelClosed}, - {kEventClassFont, kEventFontSelection}}; - - err = InstallApplicationEventHandler (mac_handle_font_event, - GetEventTypeCount (specs), - specs, NULL, NULL); - if (err != noErr) - return err; - - font_panel_shown_p = 1; - } - - return FPShowHideFontPanel (); -} - -OSStatus -mac_set_font_info_for_selection (f, face_id, c) - struct frame *f; - int face_id, c; -{ - OSStatus err; - EventTargetRef target = NULL; - XFontStruct *font = NULL; - - if (!mac_font_panel_visible_p ()) - return noErr; - - if (f) - { - target = GetWindowEventTarget (FRAME_MAC_WINDOW (f)); - - if (FRAME_FACE_CACHE (f) && CHAR_VALID_P (c, 0)) - { - struct face *face; - - face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c); - face = FACE_FROM_ID (f, face_id); - font = face->font; - } - } - - if (font == NULL) - err = SetFontInfoForSelection (kFontSelectionATSUIType, 0, NULL, target); - else - { - if (font->mac_fontnum != -1) - { - FontSelectionQDStyle qd_style; - - qd_style.version = kFontSelectionQDStyleVersionZero; - qd_style.instance.fontFamily = font->mac_fontnum; - qd_style.instance.fontStyle = font->mac_fontface; - qd_style.size = font->mac_fontsize; - qd_style.hasColor = false; - - err = SetFontInfoForSelection (kFontSelectionQDType, - 1, &qd_style, target); - } - else - err = SetFontInfoForSelection (kFontSelectionATSUIType, - 1, &font->mac_style, target); - } - - return err; -} -#endif - - -/* The Mac Event loop code */ + +/* The Mac Event loop code */ #if !TARGET_API_MAC_CARBON #include @@ -9563,20 +8094,6 @@ mac_set_font_info_for_selection (f, face_id, c) #endif #endif /* ! TARGET_API_MAC_CARBON */ -#define M_APPLE 234 -#define I_ABOUT 1 - -#define DEFAULT_NUM_COLS 80 - -#define MIN_DOC_SIZE 64 -#define MAX_DOC_SIZE 32767 - -#define EXTRA_STACK_ALLOC (256 * 1024) - -#define ARGV_STRING_LIST_ID 129 -#define ABOUT_ALERT_ID 128 -#define RAM_TOO_LARGE_ALERT_ID 129 - /* Contains the string "reverse", which is a constant for mouse button emu.*/ Lisp_Object Qreverse; @@ -9611,70 +8128,43 @@ int mac_pass_command_to_system; int mac_pass_control_to_system; #endif -/* Points to the variable `inev' in the function XTread_socket. It is - used for passing an input event to the function back from - Carbon/Apple event handlers. */ -static struct input_event *read_socket_inev = NULL; - /* Whether or not the screen configuration has changed. */ -static int mac_screen_config_changed = 0; - -Point saved_menu_event_location; +int mac_screen_config_changed = 0; /* Apple Events */ #if TARGET_API_MAC_CARBON -static Lisp_Object Qhi_command; +Lisp_Object Qhi_command; #ifdef MAC_OSX -extern Lisp_Object Qwindow; -static Lisp_Object Qtoolbar_switch_mode; +Lisp_Object Qtoolbar_switch_mode; #endif #if USE_MAC_TSM -static TSMDocumentID tsm_document_id; -static Lisp_Object Qtext_input; -static Lisp_Object Qupdate_active_input_area, Qunicode_for_key_event; -static Lisp_Object Vmac_ts_active_input_overlay; -extern Lisp_Object Qbefore_string; -static Lisp_Object Vmac_ts_script_language_on_focus; -static Lisp_Object saved_ts_script_language_on_focus; +Lisp_Object Qtext_input; +Lisp_Object Qupdate_active_input_area, Qunicode_for_key_event; +Lisp_Object Vmac_ts_active_input_overlay; +Lisp_Object Vmac_ts_script_language_on_focus; +Lisp_Object saved_ts_script_language_on_focus; static ScriptLanguageRecord saved_ts_language; static Component saved_ts_component; #endif +#ifdef MAC_OSX +Lisp_Object Qservice, Qpaste, Qperform; +#endif #endif /* TARGET_API_MAC_CARBON */ -extern int mac_ready_for_apple_events; extern Lisp_Object Qundefined; +extern int XTread_socket P_ ((int, int, struct input_event *)); extern void init_apple_event_handler P_ ((void)); extern void mac_find_apple_event_spec P_ ((AEEventClass, AEEventID, Lisp_Object *, Lisp_Object *, Lisp_Object *)); extern OSErr init_coercion_handler P_ ((void)); -/* Drag and Drop */ -extern OSErr install_drag_handler P_ ((WindowRef)); -extern void remove_drag_handler P_ ((WindowRef)); - -#if TARGET_API_MAC_CARBON -/* Showing help echo string during menu tracking */ -extern OSStatus install_menu_target_item_handler P_ ((void)); - -#ifdef MAC_OSX -extern OSStatus install_service_handler (); -static Lisp_Object Qservice, Qpaste, Qperform; -#endif -#endif - -extern void init_emacs_passwd_dir (); -extern int emacs_main (int, char **, char **); - -extern void initialize_applescript(); -extern void terminate_applescript(); - /* Table for translating Mac keycode to X keysym values. Contributed by Sudhir Shenoy. Mapping for special keys is now identical to that in Apple X11 except `clear' (-> ) on the KeyPad, `enter' (-> ) on the right of the Cmd key on laptops, and fn + `enter' (-> ). */ -static const unsigned char keycode_to_xkeysym_table[] = { +const unsigned char keycode_to_xkeysym_table[] = { /*0x00*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*0x20*/ 0, 0, 0, 0, 0x0d /*return*/, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -9743,7 +8233,7 @@ static const unsigned char fn_keycode_to_keycode_table[] = { }; #endif /* MAC_OSX */ -static int +int #if TARGET_API_MAC_CARBON mac_to_emacs_modifiers (UInt32 mods, UInt32 unmapped_mods) #else @@ -9790,7 +8280,7 @@ mac_to_emacs_modifiers (EventModifiers mods, EventModifiers unmapped_mods) return result; } -static UInt32 +UInt32 mac_mapped_modifiers (modifiers, key_code) UInt32 modifiers, key_code; { @@ -9818,7 +8308,7 @@ mac_mapped_modifiers (modifiers, key_code) return mapped_modifiers_all & modifiers; } -static int +int mac_get_emulated_btn ( UInt32 modifiers ) { int result = 0; @@ -9832,6 +8322,84 @@ mac_get_emulated_btn ( UInt32 modifiers ) return result; } +#if USE_MAC_TSM +OSStatus +mac_restore_keyboard_input_source () +{ + OSStatus err = noErr; + ScriptLanguageRecord slrec, *slptr = NULL; + + if (EQ (Vmac_ts_script_language_on_focus, Qt) + && EQ (saved_ts_script_language_on_focus, Qt)) + slptr = &saved_ts_language; + else if (CONSP (Vmac_ts_script_language_on_focus) + && INTEGERP (XCAR (Vmac_ts_script_language_on_focus)) + && INTEGERP (XCDR (Vmac_ts_script_language_on_focus)) + && CONSP (saved_ts_script_language_on_focus) + && EQ (XCAR (saved_ts_script_language_on_focus), + XCAR (Vmac_ts_script_language_on_focus)) + && EQ (XCDR (saved_ts_script_language_on_focus), + XCDR (Vmac_ts_script_language_on_focus))) + { + slrec.fScript = XINT (XCAR (Vmac_ts_script_language_on_focus)); + slrec.fLanguage = XINT (XCDR (Vmac_ts_script_language_on_focus)); + slptr = &slrec; + } + + if (slptr) + { +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 + err = SetDefaultInputMethodOfClass (saved_ts_component, slptr, + kKeyboardInputMethodClass); +#else + err = SetDefaultInputMethod (saved_ts_component, slptr); +#endif + if (err == noErr) + err = SetTextServiceLanguage (slptr); + + /* Seems to be needed on Mac OS X 10.2. */ + if (err == noErr) + KeyScript (slptr->fScript | smKeyForceKeyScriptMask); + } + + return err; +} + +void +mac_save_keyboard_input_source () +{ + OSStatus err; + ScriptLanguageRecord slrec, *slptr = NULL; + + saved_ts_script_language_on_focus = Vmac_ts_script_language_on_focus; + + if (EQ (Vmac_ts_script_language_on_focus, Qt)) + { + err = GetTextServiceLanguage (&saved_ts_language); + if (err == noErr) + slptr = &saved_ts_language; + } + else if (CONSP (Vmac_ts_script_language_on_focus) + && INTEGERP (XCAR (Vmac_ts_script_language_on_focus)) + && INTEGERP (XCDR (Vmac_ts_script_language_on_focus))) + { + slrec.fScript = XINT (XCAR (Vmac_ts_script_language_on_focus)); + slrec.fLanguage = XINT (XCDR (Vmac_ts_script_language_on_focus)); + slptr = &slrec; + } + + if (slptr) + { +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 + GetDefaultInputMethodOfClass (&saved_ts_component, slptr, + kKeyboardInputMethodClass); +#else + GetDefaultInputMethod (&saved_ts_component, slptr); +#endif + } +} +#endif + #if TARGET_API_MAC_CARBON /***** Code to handle C-g testing *****/ extern int quit_char; @@ -9865,2557 +8433,525 @@ mac_quit_char_key_p (modifiers, key_code) } #endif -#if TARGET_API_MAC_CARBON -/* Obtains the event modifiers from the event ref and then calls - mac_to_emacs_modifiers. */ -static int -mac_event_to_emacs_modifiers (EventRef eventRef) +static void +mac_set_unicode_keystroke_event (code, buf) + UniChar code; + struct input_event *buf; { - UInt32 mods = 0, class; + int charset_id, c1, c2; - GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL, - sizeof (UInt32), NULL, &mods); - class = GetEventClass (eventRef); - if (!NILP (Vmac_emulate_three_button_mouse) && - (class == kEventClassMouse || class == kEventClassCommand)) + if (code < 0x80) { - mods &= ~(optionKey | cmdKey); + buf->kind = ASCII_KEYSTROKE_EVENT; + buf->code = code; } - return mac_to_emacs_modifiers (mods, 0); -} - -/* Given an event ref, return the code to use for the mouse button - code in the emacs input_event. */ -static int -mac_get_mouse_btn (EventRef ref) -{ - EventMouseButton result = kEventMouseButtonPrimary; - GetEventParameter (ref, kEventParamMouseButton, typeMouseButton, NULL, - sizeof (EventMouseButton), NULL, &result); - switch (result) + else if (code < 0x100) { - case kEventMouseButtonPrimary: - if (NILP (Vmac_emulate_three_button_mouse)) - return 0; - else { - UInt32 mods = 0; - GetEventParameter (ref, kEventParamKeyModifiers, typeUInt32, NULL, - sizeof (UInt32), NULL, &mods); - return mac_get_emulated_btn(mods); - } - case kEventMouseButtonSecondary: - return mac_wheel_button_is_mouse_2 ? 2 : 1; - case kEventMouseButtonTertiary: - case 4: /* 4 is the number for the mouse wheel button */ - return mac_wheel_button_is_mouse_2 ? 1 : 2; - default: - return 0; + if (code < 0xA0) + charset_id = CHARSET_8_BIT_CONTROL; + else + charset_id = charset_latin_iso8859_1; + buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT; + buf->code = MAKE_CHAR (charset_id, code, 0); + } + else + { + if (code < 0x2500) + charset_id = charset_mule_unicode_0100_24ff, + code -= 0x100; + else if (code < 0x33FF) + charset_id = charset_mule_unicode_2500_33ff, + code -= 0x2500; + else if (code >= 0xE000) + charset_id = charset_mule_unicode_e000_ffff, + code -= 0xE000; + c1 = (code / 96) + 32, c2 = (code % 96) + 32; + buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT; + buf->code = MAKE_CHAR (charset_id, c1, c2); } } -/* Normally, ConvertEventRefToEventRecord will correctly handle all - events. However the click of the mouse wheel is not converted to a - mouseDown or mouseUp event. Likewise for dead key events. This - calls ConvertEventRefToEventRecord, but then checks to see if it is - a mouse up/down, or a dead key Carbon event that has not been - converted, and if so, converts it by hand (to be picked up in the - XTread_socket loop). */ -static Boolean mac_convert_event_ref (EventRef eventRef, EventRecord *eventRec) +void +do_keystroke (action, char_code, key_code, modifiers, timestamp, buf) + EventKind action; + unsigned char char_code; + UInt32 key_code, modifiers; + unsigned long timestamp; + struct input_event *buf; { - OSStatus err; - Boolean result = ConvertEventRefToEventRecord (eventRef, eventRec); - EventKind action; + static SInt16 last_key_script = -1; + SInt16 current_key_script = GetScriptManagerVariable (smKeyScript); + UInt32 mapped_modifiers = mac_mapped_modifiers (modifiers, key_code); - if (result) - return result; +#ifdef MAC_OSX + if (mapped_modifiers & kEventKeyModifierFnMask + && key_code <= 0x7f + && fn_keycode_to_keycode_table[key_code]) + key_code = fn_keycode_to_keycode_table[key_code]; +#endif - switch (GetEventClass (eventRef)) + if (key_code <= 0x7f && keycode_to_xkeysym_table[key_code]) { - case kEventClassMouse: - switch (GetEventKind (eventRef)) - { - case kEventMouseDown: - eventRec->what = mouseDown; - result = 1; - break; + buf->kind = NON_ASCII_KEYSTROKE_EVENT; + buf->code = 0xff00 | keycode_to_xkeysym_table[key_code]; + } + else if (mapped_modifiers) + { + /* translate the keycode back to determine the original key */ +#ifdef MAC_OSX + UCKeyboardLayout *uchr_ptr = NULL; +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 + OSStatus err; + KeyboardLayoutRef layout; - case kEventMouseUp: - eventRec->what = mouseUp; - result = 1; - break; + err = KLGetCurrentKeyboardLayout (&layout); + if (err == noErr) + err = KLGetKeyboardLayoutProperty (layout, kKLuchrData, + (const void **) &uchr_ptr); +#else + static SInt16 last_key_layout_id = 0; + static Handle uchr_handle = (Handle)-1; + SInt16 current_key_layout_id = + GetScriptVariable (current_key_script, smScriptKeys); - default: - break; + if (uchr_handle == (Handle)-1 + || last_key_layout_id != current_key_layout_id) + { + uchr_handle = GetResource ('uchr', current_key_layout_id); + last_key_layout_id = current_key_layout_id; } - break; + if (uchr_handle) + uchr_ptr = (UCKeyboardLayout *)*uchr_handle; +#endif - case kEventClassKeyboard: - switch (GetEventKind (eventRef)) + if (uchr_ptr) { - case kEventRawKeyDown: - action = keyDown; - goto keystroke_common; - case kEventRawKeyRepeat: - action = autoKey; - goto keystroke_common; - case kEventRawKeyUp: - action = keyUp; - keystroke_common: - { - unsigned char char_codes; - UInt32 key_code; - - err = GetEventParameter (eventRef, kEventParamKeyMacCharCodes, - typeChar, NULL, sizeof (char), - NULL, &char_codes); - if (err == noErr) - err = GetEventParameter (eventRef, kEventParamKeyCode, - typeUInt32, NULL, sizeof (UInt32), - NULL, &key_code); - if (err == noErr) - { - eventRec->what = action; - eventRec->message = char_codes | ((key_code & 0xff) << 8); - result = 1; - } - } - break; + OSStatus status; + UInt16 key_action = action - keyDown; + UInt32 modifier_key_state = (modifiers & ~mapped_modifiers) >> 8; + UInt32 keyboard_type = LMGetKbdType (); + SInt32 dead_key_state = 0; + UniChar code; + UniCharCount actual_length; - default: - break; + status = UCKeyTranslate (uchr_ptr, key_code, key_action, + modifier_key_state, keyboard_type, + kUCKeyTranslateNoDeadKeysMask, + &dead_key_state, + 1, &actual_length, &code); + if (status == noErr && actual_length == 1) + mac_set_unicode_keystroke_event (code, buf); } - break; - - default: - break; - } +#endif /* MAC_OSX */ - if (result) - { - /* Need where and when. */ - UInt32 mods = 0; - - GetEventParameter (eventRef, kEventParamMouseLocation, typeQDPoint, - NULL, sizeof (Point), NULL, &eventRec->where); - /* Use two step process because new event modifiers are 32-bit - and old are 16-bit. Currently, only loss is NumLock & Fn. */ - GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, - NULL, sizeof (UInt32), NULL, &mods); - eventRec->modifiers = mods; + if (buf->kind == NO_EVENT) + { + /* This code comes from Keyboard Resource, Appendix C of IM + - Text. This is necessary since shift is ignored in KCHR + table translation when option or command is pressed. It + also does not translate correctly control-shift chars + like C-% so mask off shift here also. */ + /* Mask off modifier keys that are mapped to some Emacs + modifiers. */ + int new_modifiers = modifiers & ~mapped_modifiers; + /* set high byte of keycode to modifier high byte*/ + int new_key_code = key_code | new_modifiers; + Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache); + unsigned long some_state = 0; + UInt32 new_char_code; - eventRec->when = EventTimeToTicks (GetEventTime (eventRef)); + new_char_code = KeyTranslate (kchr_ptr, new_key_code, &some_state); + if (new_char_code == 0) + /* Seems like a dead key. Append up-stroke. */ + new_char_code = KeyTranslate (kchr_ptr, new_key_code | 0x80, + &some_state); + if (new_char_code) + { + buf->kind = ASCII_KEYSTROKE_EVENT; + buf->code = new_char_code & 0xff; + } + } } - return result; -} + if (buf->kind == NO_EVENT) + { + buf->kind = ASCII_KEYSTROKE_EVENT; + buf->code = char_code; + } -#endif + buf->modifiers = mac_to_emacs_modifiers (mapped_modifiers, modifiers); + buf->modifiers |= (extra_keyboard_modifiers + & (meta_modifier | alt_modifier + | hyper_modifier | super_modifier)); -#ifdef MAC_OS8 -static void -do_get_menus (void) -{ - Handle menubar_handle; - MenuRef menu; +#if TARGET_API_MAC_CARBON + if (buf->kind == ASCII_KEYSTROKE_EVENT + && buf->code >= 0x80 && buf->modifiers) + { + OSStatus err; + TextEncoding encoding = kTextEncodingMacRoman; + TextToUnicodeInfo ttu_info; - menubar_handle = GetNewMBar (128); - if(menubar_handle == NULL) - abort (); - SetMenuBar (menubar_handle); - DrawMenuBar (); + UpgradeScriptInfoToTextEncoding (current_key_script, + kTextLanguageDontCare, + kTextRegionDontCare, + NULL, &encoding); + err = CreateTextToUnicodeInfoByEncoding (encoding, &ttu_info); + if (err == noErr) + { + UniChar code; + Str255 pstr; + ByteCount unicode_len; -#if !TARGET_API_MAC_CARBON - menu = GetMenuRef (M_APPLE); - if (menu != NULL) - AppendResMenu (menu, 'DRVR'); - else - abort (); + pstr[0] = 1; + pstr[1] = buf->code; + err = ConvertFromPStringToUnicode (ttu_info, pstr, + sizeof (UniChar), + &unicode_len, &code); + if (err == noErr && unicode_len == sizeof (UniChar)) + mac_set_unicode_keystroke_event (code, buf); + DisposeTextToUnicodeInfo (&ttu_info); + } + } #endif -} - - -static void -do_init_managers (void) -{ -#if !TARGET_API_MAC_CARBON - InitGraf (&qd.thePort); - InitFonts (); - FlushEvents (everyEvent, 0); - InitWindows (); - InitMenus (); - TEInit (); - InitDialogs (NULL); -#endif /* !TARGET_API_MAC_CARBON */ - InitCursor (); -#if !TARGET_API_MAC_CARBON - /* set up some extra stack space for use by emacs */ - SetApplLimit ((Ptr) ((long) GetApplLimit () - EXTRA_STACK_ALLOC)); + if (buf->kind == ASCII_KEYSTROKE_EVENT + && buf->code >= 0x80 + && last_key_script != current_key_script) + { + struct input_event event; - /* MaxApplZone must be called for AppleScript to execute more - complicated scripts */ - MaxApplZone (); - MoreMasters (); -#endif /* !TARGET_API_MAC_CARBON */ + EVENT_INIT (event); + event.kind = LANGUAGE_CHANGE_EVENT; + event.arg = Qnil; + event.code = current_key_script; + event.timestamp = timestamp; + kbd_buffer_store_event (&event); + last_key_script = current_key_script; + } } -static void -do_check_ram_size (void) +void +mac_store_apple_event (class, id, desc) + Lisp_Object class, id; + const AEDesc *desc; { - SInt32 physical_ram_size, logical_ram_size; + struct input_event buf; - if (Gestalt (gestaltPhysicalRAMSize, &physical_ram_size) != noErr - || Gestalt (gestaltLogicalRAMSize, &logical_ram_size) != noErr - || physical_ram_size > (1 << VALBITS) - || logical_ram_size > (1 << VALBITS)) - { - StopAlert (RAM_TOO_LARGE_ALERT_ID, NULL); - exit (1); - } + EVENT_INIT (buf); + + buf.kind = MAC_APPLE_EVENT; + buf.x = class; + buf.y = id; + XSETFRAME (buf.frame_or_window, + mac_focus_frame (&one_mac_display_info)); + /* Now that Lisp object allocations are protected by BLOCK_INPUT, it + is safe to use them during read_socket_hook. */ + buf.arg = mac_aedesc_to_lisp (desc); + kbd_buffer_store_event (&buf); } -#endif /* MAC_OS8 */ -static void -do_window_update (WindowRef win) +#if TARGET_API_MAC_CARBON +OSStatus +mac_store_event_ref_as_apple_event (class, id, class_key, id_key, + event, num_params, names, types) + AEEventClass class; + AEEventID id; + Lisp_Object class_key, id_key; + EventRef event; + UInt32 num_params; + const EventParamName *names; + const EventParamType *types; { - struct frame *f = mac_window_to_frame (win); - - BeginUpdate (win); + OSStatus err = eventNotHandledErr; + Lisp_Object binding; - /* The tooltip has been drawn already. Avoid the SET_FRAME_GARBAGED - below. */ - if (win != tip_window) + mac_find_apple_event_spec (class, id, &class_key, &id_key, &binding); + if (!NILP (binding) && !EQ (binding, Qundefined)) { - if (f->async_visible == 0) - { - /* Update events may occur when a frame gets iconified. */ -#if 0 - f->async_visible = 1; - f->async_iconified = 0; - SET_FRAME_GARBAGED (f); -#endif - } + if (INTEGERP (binding)) + err = XINT (binding); else { - Rect r; -#if TARGET_API_MAC_CARBON - RgnHandle region = NewRgn (); - - GetPortVisibleRegion (GetWindowPort (win), region); - GetRegionBounds (region, &r); - expose_frame (f, r.left, r.top, r.right - r.left, r.bottom - r.top); -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (f); -#endif - UpdateControls (win, region); - DisposeRgn (region); -#else - r = (*win->visRgn)->rgnBBox; - expose_frame (f, r.left, r.top, r.right - r.left, r.bottom - r.top); - UpdateControls (win, win->visRgn); -#endif + AppleEvent apple_event; + err = create_apple_event_from_event_ref (event, num_params, + names, types, + &apple_event); + if (err == noErr) + { + mac_store_apple_event (class_key, id_key, &apple_event); + AEDisposeDesc (&apple_event); + mac_wakeup_from_rne (); + } } } - EndUpdate (win); + return err; } +#endif /* TARGET_API_MAC_CARBON */ -static int -is_emacs_window (WindowRef win) +static pascal void +mac_handle_dm_notification (event) + AppleEvent *event; { - Lisp_Object tail, frame; - - if (!win) - return 0; - - FOR_EACH_FRAME (tail, frame) - if (FRAME_MAC_P (XFRAME (frame))) - if (FRAME_MAC_WINDOW (XFRAME (frame)) == win) - return 1; - - return 0; + mac_screen_config_changed = 1; } -#if USE_MAC_TSM -static OSStatus -mac_tsm_resume () +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 +static void +mac_handle_cg_display_reconfig (display, flags, user_info) + CGDirectDisplayID display; + CGDisplayChangeSummaryFlags flags; + void *user_info; { - OSStatus err; - ScriptLanguageRecord slrec, *slptr = NULL; + mac_screen_config_changed = 1; +} +#endif - err = ActivateTSMDocument (tsm_document_id); +static OSErr +init_dm_notification_handler () +{ + OSErr err = noErr; - if (err == noErr) +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 +#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020 + if (CGDisplayRegisterReconfigurationCallback != NULL) +#endif { - if (EQ (Vmac_ts_script_language_on_focus, Qt) - && EQ (saved_ts_script_language_on_focus, Qt)) - slptr = &saved_ts_language; - else if (CONSP (Vmac_ts_script_language_on_focus) - && INTEGERP (XCAR (Vmac_ts_script_language_on_focus)) - && INTEGERP (XCDR (Vmac_ts_script_language_on_focus)) - && CONSP (saved_ts_script_language_on_focus) - && EQ (XCAR (saved_ts_script_language_on_focus), - XCAR (Vmac_ts_script_language_on_focus)) - && EQ (XCDR (saved_ts_script_language_on_focus), - XCDR (Vmac_ts_script_language_on_focus))) - { - slrec.fScript = XINT (XCAR (Vmac_ts_script_language_on_focus)); - slrec.fLanguage = XINT (XCDR (Vmac_ts_script_language_on_focus)); - slptr = &slrec; - } + CGDisplayRegisterReconfigurationCallback (mac_handle_cg_display_reconfig, + NULL); } - - if (slptr) - { -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 - err = SetDefaultInputMethodOfClass (saved_ts_component, slptr, - kKeyboardInputMethodClass); -#else - err = SetDefaultInputMethod (saved_ts_component, slptr); +#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020 + else /* CGDisplayRegisterReconfigurationCallback == NULL */ #endif - if (err == noErr) - err = SetTextServiceLanguage (slptr); +#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ +#if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020 + { + static DMNotificationUPP handle_dm_notificationUPP = NULL; + ProcessSerialNumber psn; - /* Seems to be needed on Mac OS X 10.2. */ + if (handle_dm_notificationUPP == NULL) + handle_dm_notificationUPP = + NewDMNotificationUPP (mac_handle_dm_notification); + + err = GetCurrentProcess (&psn); if (err == noErr) - KeyScript (slptr->fScript | smKeyForceKeyScriptMask); + err = DMRegisterNotifyProc (handle_dm_notificationUPP, &psn); } +#endif return err; } -static OSStatus -mac_tsm_suspend () -{ - OSStatus err; - ScriptLanguageRecord slrec, *slptr = NULL; - - saved_ts_script_language_on_focus = Vmac_ts_script_language_on_focus; - - if (EQ (Vmac_ts_script_language_on_focus, Qt)) - { - err = GetTextServiceLanguage (&saved_ts_language); - if (err == noErr) - slptr = &saved_ts_language; - } - else if (CONSP (Vmac_ts_script_language_on_focus) - && INTEGERP (XCAR (Vmac_ts_script_language_on_focus)) - && INTEGERP (XCDR (Vmac_ts_script_language_on_focus))) - { - slrec.fScript = XINT (XCAR (Vmac_ts_script_language_on_focus)); - slrec.fLanguage = XINT (XCDR (Vmac_ts_script_language_on_focus)); - slptr = &slrec; - } - - if (slptr) - { -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 - GetDefaultInputMethodOfClass (&saved_ts_component, slptr, - kKeyboardInputMethodClass); -#else - GetDefaultInputMethod (&saved_ts_component, slptr); -#endif - } - - err = DeactivateTSMDocument (tsm_document_id); - - return err; -} -#endif - -#if !TARGET_API_MAC_CARBON -void -do_apple_menu (SInt16 menu_item) -{ - Str255 item_name; - SInt16 da_driver_refnum; - - if (menu_item == I_ABOUT) - NoteAlert (ABOUT_ALERT_ID, NULL); - else - { - GetMenuItemText (GetMenuRef (M_APPLE), menu_item, item_name); - da_driver_refnum = OpenDeskAcc (item_name); - } -} -#endif /* !TARGET_API_MAC_CARBON */ - -/* Handle drags in size box. Based on code contributed by Ben - Mesander and IM - Window Manager A. */ - -static void -do_grow_window (w, e) - WindowRef w; - const EventRecord *e; -{ - Rect limit_rect; - int rows, columns, width, height; - struct frame *f = mac_window_to_frame (w); - XSizeHints *size_hints = FRAME_SIZE_HINTS (f); - int min_width = MIN_DOC_SIZE, min_height = MIN_DOC_SIZE; -#if TARGET_API_MAC_CARBON - Rect new_rect; -#else - long grow_size; -#endif - - if (size_hints->flags & PMinSize) - { - min_width = size_hints->min_width; - min_height = size_hints->min_height; - } - SetRect (&limit_rect, min_width, min_height, MAX_DOC_SIZE, MAX_DOC_SIZE); - -#if TARGET_API_MAC_CARBON - if (!ResizeWindow (w, e->where, &limit_rect, &new_rect)) - return; - height = new_rect.bottom - new_rect.top; - width = new_rect.right - new_rect.left; -#else - grow_size = GrowWindow (w, e->where, &limit_rect); - /* see if it really changed size */ - if (grow_size == 0) - return; - height = HiWord (grow_size); - width = LoWord (grow_size); -#endif - - if (width != FRAME_PIXEL_WIDTH (f) - || height != FRAME_PIXEL_HEIGHT (f)) - { - rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); - columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); - - x_set_window_size (f, 0, columns, rows); - } -} - - -#if TARGET_API_MAC_CARBON -static Point -mac_get_ideal_size (f) - struct frame *f; -{ - struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); - WindowRef w = FRAME_MAC_WINDOW (f); - Point ideal_size; - Rect standard_rect; - int height, width, columns, rows; - - ideal_size.h = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS); - ideal_size.v = dpyinfo->height; - IsWindowInStandardState (w, &ideal_size, &standard_rect); - /* Adjust the standard size according to character boundaries. */ - width = standard_rect.right - standard_rect.left; - height = standard_rect.bottom - standard_rect.top; - columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, width); - rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, height); - ideal_size.h = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, columns); - ideal_size.v = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows); - - return ideal_size; -} -#endif - -/* Handle clicks in zoom box. Calculation of "standard state" based - on code in IM - Window Manager A and code contributed by Ben - Mesander. The standard state of an Emacs window is 80-characters - wide (DEFAULT_NUM_COLS) and as tall as will fit on the screen. */ - -static void -do_zoom_window (WindowRef w, int zoom_in_or_out) -{ - Rect zoom_rect, port_rect; - int width, height; - struct frame *f = mac_window_to_frame (w); -#if TARGET_API_MAC_CARBON - Point ideal_size = mac_get_ideal_size (f); - - GetWindowBounds (w, kWindowContentRgn, &port_rect); - if (IsWindowInStandardState (w, &ideal_size, &zoom_rect) - && port_rect.left == zoom_rect.left - && port_rect.top == zoom_rect.top) - zoom_in_or_out = inZoomIn; - else - zoom_in_or_out = inZoomOut; - -#ifdef MAC_OS8 - mac_clear_window (f); -#endif - ZoomWindowIdeal (w, zoom_in_or_out, &ideal_size); -#else /* not TARGET_API_MAC_CARBON */ - GrafPtr save_port; - Point top_left; - int w_title_height, rows; - struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f); - - GetPort (&save_port); - - SetPortWindowPort (w); - - /* Clear window to avoid flicker. */ - EraseRect (&(w->portRect)); - if (zoom_in_or_out == inZoomOut) - { - SetPt (&top_left, w->portRect.left, w->portRect.top); - LocalToGlobal (&top_left); - - /* calculate height of window's title bar */ - w_title_height = top_left.v - 1 - - (**((WindowPeek) w)->strucRgn).rgnBBox.top + GetMBarHeight (); - - /* get maximum height of window into zoom_rect.bottom - zoom_rect.top */ - zoom_rect = qd.screenBits.bounds; - zoom_rect.top += w_title_height; - InsetRect (&zoom_rect, 8, 4); /* not too tight */ - - zoom_rect.right = zoom_rect.left - + FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, DEFAULT_NUM_COLS); - - /* Adjust the standard size according to character boundaries. */ - rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, zoom_rect.bottom - zoom_rect.top); - zoom_rect.bottom = - zoom_rect.top + FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows); - - (**((WStateDataHandle) ((WindowPeek) w)->dataHandle)).stdState - = zoom_rect; - } - - ZoomWindow (w, zoom_in_or_out, f == mac_focus_frame (dpyinfo)); - - SetPort (save_port); -#endif /* not TARGET_API_MAC_CARBON */ - -#if !TARGET_API_MAC_CARBON - /* retrieve window size and update application values */ - port_rect = w->portRect; - height = port_rect.bottom - port_rect.top; - width = port_rect.right - port_rect.left; - - mac_handle_size_change (f, width, height); - mac_handle_origin_change (f); -#endif -} - -static void -mac_set_unicode_keystroke_event (code, buf) - UniChar code; - struct input_event *buf; -{ - int charset_id, c1, c2; - - if (code < 0x80) - { - buf->kind = ASCII_KEYSTROKE_EVENT; - buf->code = code; - } - else if (code < 0x100) - { - if (code < 0xA0) - charset_id = CHARSET_8_BIT_CONTROL; - else - charset_id = charset_latin_iso8859_1; - buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT; - buf->code = MAKE_CHAR (charset_id, code, 0); - } - else - { - if (code < 0x2500) - charset_id = charset_mule_unicode_0100_24ff, - code -= 0x100; - else if (code < 0x33FF) - charset_id = charset_mule_unicode_2500_33ff, - code -= 0x2500; - else if (code >= 0xE000) - charset_id = charset_mule_unicode_e000_ffff, - code -= 0xE000; - c1 = (code / 96) + 32, c2 = (code % 96) + 32; - buf->kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT; - buf->code = MAKE_CHAR (charset_id, c1, c2); - } -} - -static void -do_keystroke (action, char_code, key_code, modifiers, timestamp, buf) - EventKind action; - unsigned char char_code; - UInt32 key_code, modifiers; - unsigned long timestamp; - struct input_event *buf; -{ - static SInt16 last_key_script = -1; - SInt16 current_key_script = GetScriptManagerVariable (smKeyScript); - UInt32 mapped_modifiers = mac_mapped_modifiers (modifiers, key_code); - -#ifdef MAC_OSX - if (mapped_modifiers & kEventKeyModifierFnMask - && key_code <= 0x7f - && fn_keycode_to_keycode_table[key_code]) - key_code = fn_keycode_to_keycode_table[key_code]; -#endif - - if (key_code <= 0x7f && keycode_to_xkeysym_table[key_code]) - { - buf->kind = NON_ASCII_KEYSTROKE_EVENT; - buf->code = 0xff00 | keycode_to_xkeysym_table[key_code]; - } - else if (mapped_modifiers) - { - /* translate the keycode back to determine the original key */ -#ifdef MAC_OSX - UCKeyboardLayout *uchr_ptr = NULL; -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 - OSStatus err; - KeyboardLayoutRef layout; - - err = KLGetCurrentKeyboardLayout (&layout); - if (err == noErr) - err = KLGetKeyboardLayoutProperty (layout, kKLuchrData, - (const void **) &uchr_ptr); -#else - static SInt16 last_key_layout_id = 0; - static Handle uchr_handle = (Handle)-1; - SInt16 current_key_layout_id = - GetScriptVariable (current_key_script, smScriptKeys); - - if (uchr_handle == (Handle)-1 - || last_key_layout_id != current_key_layout_id) - { - uchr_handle = GetResource ('uchr', current_key_layout_id); - last_key_layout_id = current_key_layout_id; - } - if (uchr_handle) - uchr_ptr = (UCKeyboardLayout *)*uchr_handle; -#endif - - if (uchr_ptr) - { - OSStatus status; - UInt16 key_action = action - keyDown; - UInt32 modifier_key_state = (modifiers & ~mapped_modifiers) >> 8; - UInt32 keyboard_type = LMGetKbdType (); - SInt32 dead_key_state = 0; - UniChar code; - UniCharCount actual_length; - - status = UCKeyTranslate (uchr_ptr, key_code, key_action, - modifier_key_state, keyboard_type, - kUCKeyTranslateNoDeadKeysMask, - &dead_key_state, - 1, &actual_length, &code); - if (status == noErr && actual_length == 1) - mac_set_unicode_keystroke_event (code, buf); - } -#endif /* MAC_OSX */ - - if (buf->kind == NO_EVENT) - { - /* This code comes from Keyboard Resource, Appendix C of IM - - Text. This is necessary since shift is ignored in KCHR - table translation when option or command is pressed. It - also does not translate correctly control-shift chars - like C-% so mask off shift here also. */ - /* Mask off modifier keys that are mapped to some Emacs - modifiers. */ - int new_modifiers = modifiers & ~mapped_modifiers; - /* set high byte of keycode to modifier high byte*/ - int new_key_code = key_code | new_modifiers; - Ptr kchr_ptr = (Ptr) GetScriptManagerVariable (smKCHRCache); - unsigned long some_state = 0; - UInt32 new_char_code; - - new_char_code = KeyTranslate (kchr_ptr, new_key_code, &some_state); - if (new_char_code == 0) - /* Seems like a dead key. Append up-stroke. */ - new_char_code = KeyTranslate (kchr_ptr, new_key_code | 0x80, - &some_state); - if (new_char_code) - { - buf->kind = ASCII_KEYSTROKE_EVENT; - buf->code = new_char_code & 0xff; - } - } - } - - if (buf->kind == NO_EVENT) - { - buf->kind = ASCII_KEYSTROKE_EVENT; - buf->code = char_code; - } - - buf->modifiers = mac_to_emacs_modifiers (mapped_modifiers, modifiers); - buf->modifiers |= (extra_keyboard_modifiers - & (meta_modifier | alt_modifier - | hyper_modifier | super_modifier)); - -#if TARGET_API_MAC_CARBON - if (buf->kind == ASCII_KEYSTROKE_EVENT - && buf->code >= 0x80 && buf->modifiers) - { - OSStatus err; - TextEncoding encoding = kTextEncodingMacRoman; - TextToUnicodeInfo ttu_info; - - UpgradeScriptInfoToTextEncoding (current_key_script, - kTextLanguageDontCare, - kTextRegionDontCare, - NULL, &encoding); - err = CreateTextToUnicodeInfoByEncoding (encoding, &ttu_info); - if (err == noErr) - { - UniChar code; - Str255 pstr; - ByteCount unicode_len; - - pstr[0] = 1; - pstr[1] = buf->code; - err = ConvertFromPStringToUnicode (ttu_info, pstr, - sizeof (UniChar), - &unicode_len, &code); - if (err == noErr && unicode_len == sizeof (UniChar)) - mac_set_unicode_keystroke_event (code, buf); - DisposeTextToUnicodeInfo (&ttu_info); - } - } -#endif - - if (buf->kind == ASCII_KEYSTROKE_EVENT - && buf->code >= 0x80 - && last_key_script != current_key_script) - { - struct input_event event; - - EVENT_INIT (event); - event.kind = LANGUAGE_CHANGE_EVENT; - event.arg = Qnil; - event.code = current_key_script; - event.timestamp = timestamp; - kbd_buffer_store_event (&event); - last_key_script = current_key_script; - } -} - -void -mac_store_apple_event (class, id, desc) - Lisp_Object class, id; - const AEDesc *desc; -{ - struct input_event buf; - - EVENT_INIT (buf); - - buf.kind = MAC_APPLE_EVENT; - buf.x = class; - buf.y = id; - XSETFRAME (buf.frame_or_window, - mac_focus_frame (&one_mac_display_info)); - /* Now that Lisp object allocations are protected by BLOCK_INPUT, it - is safe to use them during read_socket_hook. */ - buf.arg = mac_aedesc_to_lisp (desc); - kbd_buffer_store_event (&buf); -} - -#if TARGET_API_MAC_CARBON -static OSStatus -mac_store_event_ref_as_apple_event (class, id, class_key, id_key, - event, num_params, names, types) - AEEventClass class; - AEEventID id; - Lisp_Object class_key, id_key; - EventRef event; - UInt32 num_params; - const EventParamName *names; - const EventParamType *types; -{ - OSStatus err = eventNotHandledErr; - Lisp_Object binding; - - mac_find_apple_event_spec (class, id, &class_key, &id_key, &binding); - if (!NILP (binding) && !EQ (binding, Qundefined)) - { - if (INTEGERP (binding)) - err = XINT (binding); - else - { - AppleEvent apple_event; - err = create_apple_event_from_event_ref (event, num_params, - names, types, - &apple_event); - if (err == noErr) - { - mac_store_apple_event (class_key, id_key, &apple_event); - AEDisposeDesc (&apple_event); - mac_wakeup_from_rne (); - } - } - } - - return err; -} - -void -mac_store_drag_event (window, mouse_pos, modifiers, desc) - WindowRef window; - Point mouse_pos; - SInt16 modifiers; - const AEDesc *desc; -{ - struct input_event buf; - - EVENT_INIT (buf); - - buf.kind = DRAG_N_DROP_EVENT; - buf.modifiers = mac_to_emacs_modifiers (modifiers, 0); - buf.timestamp = TickCount () * (1000 / 60); - XSETINT (buf.x, mouse_pos.h); - XSETINT (buf.y, mouse_pos.v); - XSETFRAME (buf.frame_or_window, mac_window_to_frame (window)); - buf.arg = mac_aedesc_to_lisp (desc); - kbd_buffer_store_event (&buf); -} - -#ifdef MAC_OSX -OSStatus -mac_store_service_event (event) - EventRef event; -{ - OSStatus err; - Lisp_Object id_key; - int num_params; - const EventParamName *names; - const EventParamType *types; - static const EventParamName names_pfm[] = - {kEventParamServiceMessageName, kEventParamServiceUserData}; - static const EventParamType types_pfm[] = - {typeCFStringRef, typeCFStringRef}; - - switch (GetEventKind (event)) - { - case kEventServicePaste: - id_key = Qpaste; - num_params = 0; - names = NULL; - types = NULL; - break; - - case kEventServicePerform: - id_key = Qperform; - num_params = sizeof (names_pfm) / sizeof (names_pfm[0]); - names = names_pfm; - types = types_pfm; - break; - - default: - abort (); - } - - err = mac_store_event_ref_as_apple_event (0, 0, Qservice, id_key, - event, num_params, - names, types); - - return err; -} -#endif /* MAC_OSX */ - -static pascal OSStatus -mac_handle_window_event (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - WindowRef wp; - OSStatus err, result = eventNotHandledErr; - struct frame *f; - UInt32 attributes; - XSizeHints *size_hints; - - err = GetEventParameter (event, kEventParamDirectObject, typeWindowRef, - NULL, sizeof (WindowRef), NULL, &wp); - if (err != noErr) - return eventNotHandledErr; - - f = mac_window_to_frame (wp); - switch (GetEventKind (event)) - { - /* -- window refresh events -- */ - - case kEventWindowUpdate: - result = CallNextEventHandler (next_handler, event); - if (result != eventNotHandledErr) - break; - - do_window_update (wp); - result = noErr; - break; - - /* -- window state change events -- */ - - case kEventWindowShowing: - size_hints = FRAME_SIZE_HINTS (f); - if (!(size_hints->flags & (USPosition | PPosition))) - { - struct frame *sf = SELECTED_FRAME (); - - if (!(FRAME_MAC_P (sf) && sf->async_visible)) - RepositionWindow (wp, NULL, kWindowCenterOnMainScreen); - else - { - RepositionWindow (wp, FRAME_MAC_WINDOW (sf), -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020 - kWindowCascadeStartAtParentWindowScreen -#else - kWindowCascadeOnParentWindowScreen -#endif - ); -#if USE_MAC_TOOLBAR - /* This is a workaround. RepositionWindow fails to put - a window at the cascading position when its parent - window has a Carbon HIToolbar. */ - if ((f->left_pos == sf->left_pos - && f->top_pos == sf->top_pos) - || (f->left_pos == sf->left_pos + 10 * 2 - && f->top_pos == sf->top_pos + 32 * 2)) - MoveWindowStructure (wp, sf->left_pos + 10, sf->top_pos + 32); -#endif - } - result = noErr; - } - break; - - case kEventWindowHiding: - /* Before unmapping the window, update the WM_SIZE_HINTS - property to claim that the current position of the window is - user-specified, rather than program-specified, so that when - the window is mapped again, it will be placed at the same - location, without forcing the user to position it by hand - again (they have already done that once for this window.) */ - x_wm_set_size_hint (f, (long) 0, 1); - result = noErr; - break; - - case kEventWindowShown: - case kEventWindowHidden: - case kEventWindowCollapsed: - case kEventWindowExpanded: - mac_handle_visibility_change (f); - result = noErr; - break; - - case kEventWindowBoundsChanging: - result = CallNextEventHandler (next_handler, event); - if (result != eventNotHandledErr) - break; - - err = GetEventParameter (event, kEventParamAttributes, typeUInt32, - NULL, sizeof (UInt32), NULL, &attributes); - if (err != noErr) - break; - - size_hints = FRAME_SIZE_HINTS (f); - if ((attributes & kWindowBoundsChangeUserResize) - && ((size_hints->flags & (PResizeInc | PBaseSize | PMinSize)) - == (PResizeInc | PBaseSize | PMinSize))) - { - Rect bounds; - int width, height; - - err = GetEventParameter (event, kEventParamCurrentBounds, - typeQDRectangle, NULL, sizeof (Rect), - NULL, &bounds); - if (err != noErr) - break; - - width = bounds.right - bounds.left; - height = bounds.bottom - bounds.top; - - if (width < size_hints->min_width) - width = size_hints->min_width; - else - width = size_hints->base_width - + (int) ((width - size_hints->base_width) - / (float) size_hints->width_inc + .5) - * size_hints->width_inc; - - if (height < size_hints->min_height) - height = size_hints->min_height; - else - height = size_hints->base_height - + (int) ((height - size_hints->base_height) - / (float) size_hints->height_inc + .5) - * size_hints->height_inc; - - bounds.right = bounds.left + width; - bounds.bottom = bounds.top + height; - SetEventParameter (event, kEventParamCurrentBounds, - typeQDRectangle, sizeof (Rect), &bounds); - result = noErr; - } - break; - - case kEventWindowBoundsChanged: - err = GetEventParameter (event, kEventParamAttributes, typeUInt32, - NULL, sizeof (UInt32), NULL, &attributes); - if (err != noErr) - break; - - if (attributes & kWindowBoundsChangeSizeChanged) - { - Rect bounds; - - err = GetEventParameter (event, kEventParamCurrentBounds, - typeQDRectangle, NULL, sizeof (Rect), - NULL, &bounds); - if (err == noErr) - { - int width, height; - - width = bounds.right - bounds.left; - height = bounds.bottom - bounds.top; - mac_handle_size_change (f, width, height); - mac_wakeup_from_rne (); - } - } - - if (attributes & kWindowBoundsChangeOriginChanged) - mac_handle_origin_change (f); - - result = noErr; - break; - - /* -- window action events -- */ - - case kEventWindowClose: - { - struct input_event buf; - - EVENT_INIT (buf); - buf.kind = DELETE_WINDOW_EVENT; - XSETFRAME (buf.frame_or_window, f); - buf.arg = Qnil; - kbd_buffer_store_event (&buf); - } - result = noErr; - break; - - case kEventWindowGetIdealSize: - result = CallNextEventHandler (next_handler, event); - if (result != eventNotHandledErr) - break; - - { - Point ideal_size = mac_get_ideal_size (f); - - err = SetEventParameter (event, kEventParamDimensions, - typeQDPoint, sizeof (Point), &ideal_size); - if (err == noErr) - result = noErr; - } - break; - -#ifdef MAC_OSX - case kEventWindowToolbarSwitchMode: - { - static const EventParamName names[] = {kEventParamDirectObject, - kEventParamWindowMouseLocation, - kEventParamKeyModifiers, - kEventParamMouseButton, - kEventParamClickCount, - kEventParamMouseChord}; - static const EventParamType types[] = {typeWindowRef, - typeQDPoint, - typeUInt32, - typeMouseButton, - typeUInt32, - typeUInt32}; - int num_params = sizeof (names) / sizeof (names[0]); - - err = mac_store_event_ref_as_apple_event (0, 0, - Qwindow, - Qtoolbar_switch_mode, - event, num_params, - names, types); - } - if (err == noErr) - result = noErr; - break; -#endif - -#if USE_MAC_TSM - /* -- window focus events -- */ - - case kEventWindowFocusAcquired: - err = mac_tsm_resume (); - if (err == noErr) - result = noErr; - break; - - case kEventWindowFocusRelinquish: - err = mac_tsm_suspend (); - if (err == noErr) - result = noErr; - break; -#endif - - default: - abort (); - } - - return result; -} - -static pascal OSStatus -mac_handle_keyboard_event (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - OSStatus err, result = eventNotHandledErr; - UInt32 event_kind, key_code, modifiers; - unsigned char char_code; - - event_kind = GetEventKind (event); - switch (event_kind) - { - case kEventRawKeyDown: - case kEventRawKeyRepeat: - case kEventRawKeyUp: - /* When using Carbon Events, we need to pass raw keyboard events - to the TSM ourselves. If TSM handles it, it will pass back - noErr, otherwise it will pass back "eventNotHandledErr" and - we can process it normally. */ - result = CallNextEventHandler (next_handler, event); - if (result != eventNotHandledErr) - break; - - if (read_socket_inev == NULL) - break; - -#if USE_MAC_TSM - if (read_socket_inev->kind != NO_EVENT) - { - result = noErr; - break; - } -#endif - - if (event_kind == kEventRawKeyUp) - break; - - err = GetEventParameter (event, kEventParamKeyMacCharCodes, - typeChar, NULL, - sizeof (char), NULL, &char_code); - if (err != noErr) - break; - - err = GetEventParameter (event, kEventParamKeyCode, - typeUInt32, NULL, - sizeof (UInt32), NULL, &key_code); - if (err != noErr) - break; - - err = GetEventParameter (event, kEventParamKeyModifiers, - typeUInt32, NULL, - sizeof (UInt32), NULL, &modifiers); - if (err != noErr) - break; - - do_keystroke ((event_kind == kEventRawKeyDown ? keyDown : autoKey), - char_code, key_code, modifiers, - ((unsigned long) - (GetEventTime (event) / kEventDurationMillisecond)), - read_socket_inev); - result = noErr; - break; - - default: - abort (); - } - - return result; -} - -static pascal OSStatus -mac_handle_command_event (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - OSStatus err, result = eventNotHandledErr; - HICommand command; - static const EventParamName names[] = - {kEventParamDirectObject, kEventParamKeyModifiers}; - static const EventParamType types[] = - {typeHICommand, typeUInt32}; - int num_params = sizeof (names) / sizeof (names[0]); - - err = GetEventParameter (event, kEventParamDirectObject, typeHICommand, - NULL, sizeof (HICommand), NULL, &command); - if (err != noErr) - return eventNotHandledErr; - - switch (GetEventKind (event)) - { - case kEventCommandProcess: - result = CallNextEventHandler (next_handler, event); - if (result != eventNotHandledErr) - break; - - err = GetEventParameter (event, kEventParamDirectObject, - typeHICommand, NULL, - sizeof (HICommand), NULL, &command); - - if (err != noErr || command.commandID == 0) - break; - - /* A HI command event is mapped to an Apple event whose event - class symbol is `hi-command' and event ID is its command - ID. */ - err = mac_store_event_ref_as_apple_event (0, command.commandID, - Qhi_command, Qnil, - event, num_params, - names, types); - if (err == noErr) - result = noErr; - break; - - default: - abort (); - } - - return result; -} - -static pascal OSStatus -mac_handle_mouse_event (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - OSStatus err, result = eventNotHandledErr; - - switch (GetEventKind (event)) - { - case kEventMouseWheelMoved: - { - WindowRef wp; - struct frame *f; - EventMouseWheelAxis axis; - SInt32 delta; - Point point; - - result = CallNextEventHandler (next_handler, event); - if (result != eventNotHandledErr || read_socket_inev == NULL) - break; - - f = mac_focus_frame (&one_mac_display_info); - - err = GetEventParameter (event, kEventParamWindowRef, typeWindowRef, - NULL, sizeof (WindowRef), NULL, &wp); - if (err != noErr - || wp != FRAME_MAC_WINDOW (f)) - break; - - err = GetEventParameter (event, kEventParamMouseWheelAxis, - typeMouseWheelAxis, NULL, - sizeof (EventMouseWheelAxis), NULL, &axis); - if (err != noErr || axis != kEventMouseWheelAxisY) - break; - - err = GetEventParameter (event, kEventParamMouseLocation, - typeQDPoint, NULL, sizeof (Point), - NULL, &point); - if (err != noErr) - break; - - point.h -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); - point.v -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); - if (point.h < 0 || point.v < 0 - || EQ (window_from_coordinates (f, point.h, point.v, 0, 0, 0, 1), - f->tool_bar_window)) - break; - - err = GetEventParameter (event, kEventParamMouseWheelDelta, - typeSInt32, NULL, sizeof (SInt32), - NULL, &delta); - if (err != noErr) - break; - - read_socket_inev->kind = WHEEL_EVENT; - read_socket_inev->code = 0; - read_socket_inev->modifiers = - (mac_event_to_emacs_modifiers (event) - | ((delta < 0) ? down_modifier : up_modifier)); - XSETINT (read_socket_inev->x, point.h); - XSETINT (read_socket_inev->y, point.v); - XSETFRAME (read_socket_inev->frame_or_window, f); - - result = noErr; - } - break; - - default: - abort (); - } - - return result; -} - -#if USE_MAC_TSM -static pascal OSStatus -mac_handle_text_input_event (next_handler, event, data) - EventHandlerCallRef next_handler; - EventRef event; - void *data; -{ - OSStatus err, result; - Lisp_Object id_key = Qnil; - int num_params; - const EventParamName *names; - const EventParamType *types; - static UInt32 seqno_uaia = 0; - static const EventParamName names_uaia[] = - {kEventParamTextInputSendComponentInstance, - kEventParamTextInputSendRefCon, - kEventParamTextInputSendSLRec, - kEventParamTextInputSendFixLen, - kEventParamTextInputSendText, - kEventParamTextInputSendUpdateRng, - kEventParamTextInputSendHiliteRng, - kEventParamTextInputSendClauseRng, - kEventParamTextInputSendPinRng, - kEventParamTextInputSendTextServiceEncoding, - kEventParamTextInputSendTextServiceMacEncoding, - EVENT_PARAM_TEXT_INPUT_SEQUENCE_NUMBER}; - static const EventParamType types_uaia[] = - {typeComponentInstance, - typeLongInteger, - typeIntlWritingCode, - typeLongInteger, -#ifdef MAC_OSX - typeUnicodeText, -#else - typeChar, -#endif - typeTextRangeArray, - typeTextRangeArray, - typeOffsetArray, - typeTextRange, - typeUInt32, - typeUInt32, - typeUInt32}; - static const EventParamName names_ufke[] = - {kEventParamTextInputSendComponentInstance, - kEventParamTextInputSendRefCon, - kEventParamTextInputSendSLRec, - kEventParamTextInputSendText}; - static const EventParamType types_ufke[] = - {typeComponentInstance, - typeLongInteger, - typeIntlWritingCode, - typeUnicodeText}; - - result = CallNextEventHandler (next_handler, event); - if (result != eventNotHandledErr) - return result; - - switch (GetEventKind (event)) - { - case kEventTextInputUpdateActiveInputArea: - id_key = Qupdate_active_input_area; - num_params = sizeof (names_uaia) / sizeof (names_uaia[0]); - names = names_uaia; - types = types_uaia; - SetEventParameter (event, EVENT_PARAM_TEXT_INPUT_SEQUENCE_NUMBER, - typeUInt32, sizeof (UInt32), &seqno_uaia); - seqno_uaia++; - result = noErr; - break; - - case kEventTextInputUnicodeForKeyEvent: - { - EventRef kbd_event; - UInt32 actual_size, modifiers, key_code; - - err = GetEventParameter (event, kEventParamTextInputSendKeyboardEvent, - typeEventRef, NULL, sizeof (EventRef), NULL, - &kbd_event); - if (err == noErr) - err = GetEventParameter (kbd_event, kEventParamKeyModifiers, - typeUInt32, NULL, - sizeof (UInt32), NULL, &modifiers); - if (err == noErr) - err = GetEventParameter (kbd_event, kEventParamKeyCode, - typeUInt32, NULL, sizeof (UInt32), - NULL, &key_code); - if (err == noErr && mac_mapped_modifiers (modifiers, key_code)) - /* There're mapped modifier keys. Process it in - do_keystroke. */ - break; - if (err == noErr) - err = GetEventParameter (kbd_event, kEventParamKeyUnicodes, - typeUnicodeText, NULL, 0, &actual_size, - NULL); - if (err == noErr && actual_size == sizeof (UniChar)) - { - UniChar code; - - err = GetEventParameter (kbd_event, kEventParamKeyUnicodes, - typeUnicodeText, NULL, - sizeof (UniChar), NULL, &code); - if (err == noErr && code < 0x80) - { - /* ASCII character. Process it in do_keystroke. */ - if (read_socket_inev && code >= 0x20 && code <= 0x7e - && !(key_code <= 0x7f - && keycode_to_xkeysym_table [key_code])) - { - struct frame *f = mac_focus_frame (&one_mac_display_info); - - read_socket_inev->kind = ASCII_KEYSTROKE_EVENT; - read_socket_inev->code = code; - read_socket_inev->modifiers = - mac_to_emacs_modifiers (modifiers, 0); - read_socket_inev->modifiers |= - (extra_keyboard_modifiers - & (meta_modifier | alt_modifier - | hyper_modifier | super_modifier)); - XSETFRAME (read_socket_inev->frame_or_window, f); - } - break; - } - } - if (err == noErr) - { - /* Non-ASCII keystrokes without mapped modifiers are - processed at the Lisp level. */ - id_key = Qunicode_for_key_event; - num_params = sizeof (names_ufke) / sizeof (names_ufke[0]); - names = names_ufke; - types = types_ufke; - result = noErr; - } - } - break; - - case kEventTextInputOffsetToPos: - { - struct frame *f; - struct window *w; - Point p; - - if (!OVERLAYP (Vmac_ts_active_input_overlay)) - break; - - /* Strictly speaking, this is not always correct because - previous events may change some states about display. */ - if (!NILP (Foverlay_get (Vmac_ts_active_input_overlay, Qbefore_string))) - { - /* Active input area is displayed around the current point. */ - f = SELECTED_FRAME (); - w = XWINDOW (f->selected_window); - } - else if (WINDOWP (echo_area_window)) - { - /* Active input area is displayed in the echo area. */ - w = XWINDOW (echo_area_window); - f = WINDOW_XFRAME (w); - } - else - break; - - p.h = (WINDOW_TO_FRAME_PIXEL_X (w, w->cursor.x) - + WINDOW_LEFT_FRINGE_WIDTH (w) - + f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f)); - p.v = (WINDOW_TO_FRAME_PIXEL_Y (w, w->cursor.y) - + FONT_BASE (FRAME_FONT (f)) - + f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f)); - err = SetEventParameter (event, kEventParamTextInputReplyPoint, - typeQDPoint, sizeof (typeQDPoint), &p); - if (err == noErr) - result = noErr; - } - break; - - default: - abort (); - } - - if (!NILP (id_key)) - err = mac_store_event_ref_as_apple_event (0, 0, Qtext_input, id_key, - event, num_params, - names, types); - return result; -} -#endif -#endif /* TARGET_API_MAC_CARBON */ - - -OSStatus -install_window_handler (window) - WindowRef window; -{ - OSStatus err = noErr; - -#if TARGET_API_MAC_CARBON - if (err == noErr) - { - static const EventTypeSpec specs[] = - { - /* -- window refresh events -- */ - {kEventClassWindow, kEventWindowUpdate}, - /* -- window state change events -- */ - {kEventClassWindow, kEventWindowShowing}, - {kEventClassWindow, kEventWindowHiding}, - {kEventClassWindow, kEventWindowShown}, - {kEventClassWindow, kEventWindowHidden}, - {kEventClassWindow, kEventWindowCollapsed}, - {kEventClassWindow, kEventWindowExpanded}, - {kEventClassWindow, kEventWindowBoundsChanging}, - {kEventClassWindow, kEventWindowBoundsChanged}, - /* -- window action events -- */ - {kEventClassWindow, kEventWindowClose}, - {kEventClassWindow, kEventWindowGetIdealSize}, -#ifdef MAC_OSX - {kEventClassWindow, kEventWindowToolbarSwitchMode}, -#endif -#if USE_MAC_TSM - /* -- window focus events -- */ - {kEventClassWindow, kEventWindowFocusAcquired}, - {kEventClassWindow, kEventWindowFocusRelinquish}, -#endif - }; - static EventHandlerUPP handle_window_eventUPP = NULL; - - if (handle_window_eventUPP == NULL) - handle_window_eventUPP = NewEventHandlerUPP (mac_handle_window_event); - - err = InstallWindowEventHandler (window, handle_window_eventUPP, - GetEventTypeCount (specs), - specs, NULL, NULL); - } -#endif - - if (err == noErr) - err = install_drag_handler (window); - - return err; -} - -void -remove_window_handler (window) - WindowRef window; -{ - remove_drag_handler (window); -} - -#if TARGET_API_MAC_CARBON -static OSStatus -install_application_handler () -{ - OSStatus err = noErr; - - if (err == noErr) - { - static const EventTypeSpec specs[] = - {{kEventClassKeyboard, kEventRawKeyDown}, - {kEventClassKeyboard, kEventRawKeyRepeat}, - {kEventClassKeyboard, kEventRawKeyUp}}; - - err = InstallApplicationEventHandler (NewEventHandlerUPP - (mac_handle_keyboard_event), - GetEventTypeCount (specs), - specs, NULL, NULL); - } - - if (err == noErr) - { - static const EventTypeSpec specs[] = - {{kEventClassCommand, kEventCommandProcess}}; - - err = InstallApplicationEventHandler (NewEventHandlerUPP - (mac_handle_command_event), - GetEventTypeCount (specs), - specs, NULL, NULL); - } - - if (err == noErr) - { - static const EventTypeSpec specs[] = - {{kEventClassMouse, kEventMouseWheelMoved}}; - - err = InstallApplicationEventHandler (NewEventHandlerUPP - (mac_handle_mouse_event), - GetEventTypeCount (specs), - specs, NULL, NULL); - } - -#if USE_MAC_TSM - if (err == noErr) - { - static const EventTypeSpec spec[] = - {{kEventClassTextInput, kEventTextInputUpdateActiveInputArea}, - {kEventClassTextInput, kEventTextInputUnicodeForKeyEvent}, - {kEventClassTextInput, kEventTextInputOffsetToPos}}; - - err = InstallApplicationEventHandler (NewEventHandlerUPP - (mac_handle_text_input_event), - GetEventTypeCount (spec), - spec, NULL, NULL); - } -#endif - - if (err == noErr) - err = install_menu_target_item_handler (); - -#ifdef MAC_OSX - if (err == noErr) - err = install_service_handler (); -#endif - - return err; -} -#endif - -static pascal void -mac_handle_dm_notification (event) - AppleEvent *event; -{ - mac_screen_config_changed = 1; -} - -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 -static void -mac_handle_cg_display_reconfig (display, flags, user_info) - CGDirectDisplayID display; - CGDisplayChangeSummaryFlags flags; - void *user_info; -{ - mac_screen_config_changed = 1; -} -#endif - -static OSErr -init_dm_notification_handler () -{ - OSErr err = noErr; - -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 -#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020 - if (CGDisplayRegisterReconfigurationCallback != NULL) -#endif - { - CGDisplayRegisterReconfigurationCallback (mac_handle_cg_display_reconfig, - NULL); - } -#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020 - else /* CGDisplayRegisterReconfigurationCallback == NULL */ -#endif -#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ -#if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020 - { - static DMNotificationUPP handle_dm_notificationUPP = NULL; - ProcessSerialNumber psn; - - if (handle_dm_notificationUPP == NULL) - handle_dm_notificationUPP = - NewDMNotificationUPP (mac_handle_dm_notification); - - err = GetCurrentProcess (&psn); - if (err == noErr) - err = DMRegisterNotifyProc (handle_dm_notificationUPP, &psn); - } -#endif - - return err; -} - -static void -mac_get_screen_info (dpyinfo) - struct mac_display_info *dpyinfo; -{ -#ifdef MAC_OSX - /* HasDepth returns true if it is possible to have a 32 bit display, - but this may not be what is actually used. Mac OSX can do better. */ - dpyinfo->color_p = CGDisplaySamplesPerPixel (kCGDirectMainDisplay) > 1; - dpyinfo->n_planes = CGDisplayBitsPerPixel (kCGDirectMainDisplay); - { - CGDisplayErr err; - CGDisplayCount ndisps; - CGDirectDisplayID *displays; - - err = CGGetActiveDisplayList (0, NULL, &ndisps); - if (err == noErr) - { - displays = alloca (sizeof (CGDirectDisplayID) * ndisps); - err = CGGetActiveDisplayList (ndisps, displays, &ndisps); - } - if (err == noErr) - { - CGRect bounds = CGRectZero; - - while (ndisps-- > 0) - bounds = CGRectUnion (bounds, CGDisplayBounds (displays[ndisps])); - dpyinfo->height = CGRectGetHeight (bounds); - dpyinfo->width = CGRectGetWidth (bounds); - } - else - { - dpyinfo->height = CGDisplayPixelsHigh (kCGDirectMainDisplay); - dpyinfo->width = CGDisplayPixelsWide (kCGDirectMainDisplay); - } - } -#else /* !MAC_OSX */ - { - GDHandle gdh = GetMainDevice (); - Rect rect = (**gdh).gdRect; - - dpyinfo->color_p = TestDeviceAttribute (gdh, gdDevType); - for (dpyinfo->n_planes = 32; dpyinfo->n_planes > 0; dpyinfo->n_planes >>= 1) - if (HasDepth (gdh, dpyinfo->n_planes, gdDevType, dpyinfo->color_p)) - break; - - for (gdh = DMGetFirstScreenDevice (dmOnlyActiveDisplays); gdh; - gdh = DMGetNextScreenDevice (gdh, dmOnlyActiveDisplays)) - UnionRect (&rect, &(**gdh).gdRect, &rect); - - dpyinfo->height = rect.bottom - rect.top; - dpyinfo->width = rect.right - rect.left; - } -#endif /* !MAC_OSX */ -} - - -#if __profile__ void -profiler_exit_proc () -{ - ProfilerDump ("\pEmacs.prof"); - ProfilerTerm (); -} -#endif - -/* These few functions implement Emacs as a normal Mac application - (almost): set up the heap and the Toolbox, handle necessary system - events plus a few simple menu events. They also set up Emacs's - access to functions defined in the rest of this file. Emacs uses - function hooks to perform all its terminal I/O. A complete list of - these functions appear in termhooks.h. For what they do, read the - comments there and see also w32term.c and xterm.c. What's - noticeably missing here is the event loop, which is normally - present in most Mac application. After performing the necessary - Mac initializations, main passes off control to emacs_main - (corresponding to main in emacs.c). Emacs_main calls XTread_socket - (defined further below) to read input. This is where - WaitNextEvent/ReceiveNextEvent is called to process Mac events. */ - -#ifdef MAC_OS8 -#undef main -int -main (void) +mac_get_screen_info (dpyinfo) + struct mac_display_info *dpyinfo; { -#if __profile__ /* is the profiler on? */ - if (ProfilerInit(collectDetailed, bestTimeBase, 5000, 200)) - exit(1); -#endif - -#if __MWERKS__ - /* set creator and type for files created by MSL */ - _fcreator = MAC_EMACS_CREATOR_CODE; - _ftype = 'TEXT'; -#endif - - do_init_managers (); - - do_get_menus (); - -#ifndef USE_LSB_TAG - do_check_ram_size (); -#endif - - init_emacs_passwd_dir (); - - init_environ (); - - init_coercion_handler (); - - initialize_applescript (); - - init_apple_event_handler (); - - init_dm_notification_handler (); - +#ifdef MAC_OSX + /* HasDepth returns true if it is possible to have a 32 bit display, + but this may not be what is actually used. Mac OSX can do better. */ + dpyinfo->color_p = CGDisplaySamplesPerPixel (kCGDirectMainDisplay) > 1; + dpyinfo->n_planes = CGDisplayBitsPerPixel (kCGDirectMainDisplay); { - char **argv; - int argc = 0; - - /* set up argv array from STR# resource */ - get_string_list (&argv, ARGV_STRING_LIST_ID); - while (argv[argc]) - argc++; - - /* free up AppleScript resources on exit */ - atexit (terminate_applescript); - -#if __profile__ /* is the profiler on? */ - atexit (profiler_exit_proc); -#endif - - /* 3rd param "envp" never used in emacs_main */ - (void) emacs_main (argc, argv, 0); - } - - /* Never reached - real exit in Fkill_emacs */ - return 0; -} -#endif - -#if !TARGET_API_MAC_CARBON -static RgnHandle mouse_region = NULL; - -Boolean -mac_wait_next_event (er, sleep_time, dequeue) - EventRecord *er; - UInt32 sleep_time; - Boolean dequeue; -{ - static EventRecord er_buf = {nullEvent}; - UInt32 target_tick, current_tick; - EventMask event_mask; - - if (mouse_region == NULL) - mouse_region = NewRgn (); - - event_mask = everyEvent; - if (!mac_ready_for_apple_events) - event_mask -= highLevelEventMask; - - current_tick = TickCount (); - target_tick = current_tick + sleep_time; + CGDisplayErr err; + CGDisplayCount ndisps; + CGDirectDisplayID *displays; - if (er_buf.what == nullEvent) - while (!WaitNextEvent (event_mask, &er_buf, - target_tick - current_tick, mouse_region)) + err = CGGetActiveDisplayList (0, NULL, &ndisps); + if (err == noErr) { - current_tick = TickCount (); - if (target_tick <= current_tick) - return false; + displays = alloca (sizeof (CGDirectDisplayID) * ndisps); + err = CGGetActiveDisplayList (ndisps, displays, &ndisps); } + if (err == noErr) + { + CGRect bounds = CGRectZero; - *er = er_buf; - if (dequeue) - er_buf.what = nullEvent; - return true; -} -#endif /* not TARGET_API_MAC_CARBON */ - -#if TARGET_API_MAC_CARBON -OSStatus -mac_post_mouse_moved_event () -{ - EventRef event = NULL; - OSStatus err; - - err = CreateEvent (NULL, kEventClassMouse, kEventMouseMoved, 0, - kEventAttributeNone, &event); - if (err == noErr) - { - Point mouse_pos; - - GetGlobalMouse (&mouse_pos); - err = SetEventParameter (event, kEventParamMouseLocation, typeQDPoint, - sizeof (Point), &mouse_pos); - } - if (err == noErr) - { - UInt32 modifiers = GetCurrentKeyModifiers (); - - err = SetEventParameter (event, kEventParamKeyModifiers, typeUInt32, - sizeof (UInt32), &modifiers); - } - if (err == noErr) - err = PostEventToQueue (GetCurrentEventQueue (), event, - kEventPriorityStandard); - if (event) - ReleaseEvent (event); - - return err; -} -#endif - -/* Emacs calls this whenever it wants to read an input event from the - user. */ -int -XTread_socket (sd, expected, hold_quit) - int sd, expected; - struct input_event *hold_quit; -{ - struct input_event inev; - int count = 0; -#if TARGET_API_MAC_CARBON - EventRef eventRef; - EventTargetRef toolbox_dispatcher; -#endif - EventRecord er; - struct mac_display_info *dpyinfo = &one_mac_display_info; - - if (interrupt_input_blocked) - { - interrupt_input_pending = 1; - return -1; - } - - interrupt_input_pending = 0; - BLOCK_INPUT; - - /* So people can tell when we have read the available input. */ - input_signal_count++; - - ++handling_signal; - -#if TARGET_API_MAC_CARBON - toolbox_dispatcher = GetEventDispatcherTarget (); - - while ( -#if USE_CG_DRAWING - mac_prepare_for_quickdraw (NULL), -#endif - !ReceiveNextEvent (0, NULL, kEventDurationNoWait, - kEventRemoveFromQueue, &eventRef)) -#else /* !TARGET_API_MAC_CARBON */ - while (mac_wait_next_event (&er, 0, true)) -#endif /* !TARGET_API_MAC_CARBON */ - { - int do_help = 0; - struct frame *f; - unsigned long timestamp; - - EVENT_INIT (inev); - inev.kind = NO_EVENT; - inev.arg = Qnil; - -#if TARGET_API_MAC_CARBON - timestamp = GetEventTime (eventRef) / kEventDurationMillisecond; - - if (!mac_convert_event_ref (eventRef, &er)) - goto OTHER; -#else /* !TARGET_API_MAC_CARBON */ - timestamp = er.when * (1000 / 60); /* ticks to milliseconds */ -#endif /* !TARGET_API_MAC_CARBON */ - - switch (er.what) - { - case mouseDown: - case mouseUp: - { - WindowRef window_ptr; - ControlPartCode part_code; - int tool_bar_p = 0; - -#if TARGET_API_MAC_CARBON - OSStatus err; - - /* This is needed to send mouse events like aqua window - buttons to the correct handler. */ - read_socket_inev = &inev; - err = SendEventToEventTarget (eventRef, toolbox_dispatcher); - read_socket_inev = NULL; - if (err != eventNotHandledErr) - break; -#endif - last_mouse_glyph_frame = 0; - - if (dpyinfo->grabbed && last_mouse_frame - && FRAME_LIVE_P (last_mouse_frame)) - { - window_ptr = FRAME_MAC_WINDOW (last_mouse_frame); - part_code = inContent; - } - else - { - part_code = FindWindow (er.where, &window_ptr); - if (tip_window && window_ptr == tip_window) - { - HideWindow (tip_window); - part_code = FindWindow (er.where, &window_ptr); - } - } - - if (er.what != mouseDown && - (part_code != inContent || dpyinfo->grabbed == 0)) - break; - - switch (part_code) - { - case inMenuBar: - f = mac_focus_frame (dpyinfo); - saved_menu_event_location = er.where; - inev.kind = MENU_BAR_ACTIVATE_EVENT; - XSETFRAME (inev.frame_or_window, f); - break; - - case inContent: - if ( -#if TARGET_API_MAC_CARBON - FrontNonFloatingWindow () -#else - FrontWindow () -#endif - != window_ptr - || (mac_window_to_frame (window_ptr) - != dpyinfo->x_focus_frame)) - SelectWindow (window_ptr); - else - { - ControlPartCode control_part_code; - ControlRef ch; - Point mouse_loc; -#ifdef MAC_OSX - ControlKind control_kind; -#endif - - f = mac_window_to_frame (window_ptr); - /* convert to local coordinates of new window */ - mouse_loc.h = (er.where.h - - (f->left_pos - + FRAME_OUTER_TO_INNER_DIFF_X (f))); - mouse_loc.v = (er.where.v - - (f->top_pos - + FRAME_OUTER_TO_INNER_DIFF_Y (f))); -#if TARGET_API_MAC_CARBON - ch = FindControlUnderMouse (mouse_loc, window_ptr, - &control_part_code); -#ifdef MAC_OSX - if (ch) - GetControlKind (ch, &control_kind); -#endif -#else - control_part_code = FindControl (mouse_loc, window_ptr, - &ch); -#endif - -#if TARGET_API_MAC_CARBON - inev.code = mac_get_mouse_btn (eventRef); - inev.modifiers = mac_event_to_emacs_modifiers (eventRef); -#else - inev.code = mac_get_emulated_btn (er.modifiers); - inev.modifiers = mac_to_emacs_modifiers (er.modifiers, 0); -#endif - XSETINT (inev.x, mouse_loc.h); - XSETINT (inev.y, mouse_loc.v); - - if ((dpyinfo->grabbed && tracked_scroll_bar) - || (ch != 0 -#ifndef USE_TOOLKIT_SCROLL_BARS - /* control_part_code becomes kControlNoPart if - a progress indicator is clicked. */ - && control_part_code != kControlNoPart -#else /* USE_TOOLKIT_SCROLL_BARS */ -#ifdef MAC_OSX - && control_kind.kind == kControlKindScrollBar -#endif /* MAC_OSX */ -#endif /* USE_TOOLKIT_SCROLL_BARS */ - )) - { - struct scroll_bar *bar; - - if (dpyinfo->grabbed && tracked_scroll_bar) - { - bar = tracked_scroll_bar; -#ifndef USE_TOOLKIT_SCROLL_BARS - control_part_code = kControlIndicatorPart; -#endif - } - else - bar = (struct scroll_bar *) GetControlReference (ch); -#ifdef USE_TOOLKIT_SCROLL_BARS - /* Make the "Ctrl-Mouse-2 splits window" work - for toolkit scroll bars. */ - if (inev.modifiers & ctrl_modifier) - x_scroll_bar_handle_click (bar, control_part_code, - &er, &inev); - else if (er.what == mouseDown) - x_scroll_bar_handle_press (bar, control_part_code, - mouse_loc, &inev); - else - x_scroll_bar_handle_release (bar, &inev); -#else /* not USE_TOOLKIT_SCROLL_BARS */ - x_scroll_bar_handle_click (bar, control_part_code, - &er, &inev); - if (er.what == mouseDown - && control_part_code == kControlIndicatorPart) - tracked_scroll_bar = bar; - else - tracked_scroll_bar = NULL; -#endif /* not USE_TOOLKIT_SCROLL_BARS */ - } - else - { - Lisp_Object window; - int x = mouse_loc.h; - int y = mouse_loc.v; - - window = window_from_coordinates (f, x, y, 0, 0, 0, 1); - if (EQ (window, f->tool_bar_window)) - { - if (er.what == mouseDown) - handle_tool_bar_click (f, x, y, 1, 0); - else - handle_tool_bar_click (f, x, y, 0, - inev.modifiers); - tool_bar_p = 1; - } - else - { - XSETFRAME (inev.frame_or_window, f); - inev.kind = MOUSE_CLICK_EVENT; - } - } - - if (er.what == mouseDown) - { - dpyinfo->grabbed |= (1 << inev.code); - last_mouse_frame = f; - - if (!tool_bar_p) - last_tool_bar_item = -1; - } - else - { - if ((dpyinfo->grabbed & (1 << inev.code)) == 0) - /* If a button is released though it was not - previously pressed, that would be because - of multi-button emulation. */ - dpyinfo->grabbed = 0; - else - dpyinfo->grabbed &= ~(1 << inev.code); - } - - /* Ignore any mouse motion that happened before - this event; any subsequent mouse-movement Emacs - events should reflect only motion after the - ButtonPress. */ - if (f != 0) - f->mouse_moved = 0; - -#ifdef USE_TOOLKIT_SCROLL_BARS - if (inev.kind == MOUSE_CLICK_EVENT - || (inev.kind == SCROLL_BAR_CLICK_EVENT - && (inev.modifiers & ctrl_modifier))) -#endif - switch (er.what) - { - case mouseDown: - inev.modifiers |= down_modifier; - break; - case mouseUp: - inev.modifiers |= up_modifier; - break; - } - } - break; - - case inDrag: -#if TARGET_API_MAC_CARBON - case inProxyIcon: - if (IsWindowPathSelectClick (window_ptr, &er)) - { - WindowPathSelect (window_ptr, NULL, NULL); - break; - } - if (part_code == inProxyIcon - && (TrackWindowProxyDrag (window_ptr, er.where) - != errUserWantsToDragWindow)) - break; - DragWindow (window_ptr, er.where, NULL); -#else /* not TARGET_API_MAC_CARBON */ - DragWindow (window_ptr, er.where, &qd.screenBits.bounds); - /* Update the frame parameters. */ - { - struct frame *f = mac_window_to_frame (window_ptr); - - if (f && !f->async_iconified) - mac_handle_origin_change (f); - } -#endif /* not TARGET_API_MAC_CARBON */ - break; - - case inGoAway: - if (TrackGoAway (window_ptr, er.where)) - { - inev.kind = DELETE_WINDOW_EVENT; - XSETFRAME (inev.frame_or_window, - mac_window_to_frame (window_ptr)); - } - break; - - /* window resize handling added --ben */ - case inGrow: - do_grow_window (window_ptr, &er); - break; - - /* window zoom handling added --ben */ - case inZoomIn: - case inZoomOut: - if (TrackBox (window_ptr, er.where, part_code)) - do_zoom_window (window_ptr, part_code); - break; - -#if USE_MAC_TOOLBAR - case inStructure: - { - OSStatus err; - HIViewRef ch; - - err = HIViewGetViewForMouseEvent (HIViewGetRoot (window_ptr), - eventRef, &ch); - /* This doesn't work on Mac OS X 10.2. */ - if (err == noErr) - HIViewClick (ch, eventRef); - } - break; -#endif /* USE_MAC_TOOLBAR */ - - default: - break; - } - } - break; - -#if !TARGET_API_MAC_CARBON - case updateEvt: - do_window_update ((WindowRef) er.message); - break; -#endif - - case osEvt: -#if TARGET_API_MAC_CARBON - if (SendEventToEventTarget (eventRef, toolbox_dispatcher) - != eventNotHandledErr) - break; -#endif - switch ((er.message >> 24) & 0x000000FF) - { -#if USE_MAC_TSM - case suspendResumeMessage: - if (er.message & resumeFlag) - mac_tsm_resume (); - else - mac_tsm_suspend (); - break; -#endif - - case mouseMovedMessage: -#if !TARGET_API_MAC_CARBON - SetRectRgn (mouse_region, er.where.h, er.where.v, - er.where.h + 1, er.where.v + 1); -#endif - previous_help_echo_string = help_echo_string; - help_echo_string = Qnil; - - if (dpyinfo->grabbed && last_mouse_frame - && FRAME_LIVE_P (last_mouse_frame)) - f = last_mouse_frame; - else - f = dpyinfo->x_focus_frame; + while (ndisps-- > 0) + bounds = CGRectUnion (bounds, CGDisplayBounds (displays[ndisps])); + dpyinfo->height = CGRectGetHeight (bounds); + dpyinfo->width = CGRectGetWidth (bounds); + } + else + { + dpyinfo->height = CGDisplayPixelsHigh (kCGDirectMainDisplay); + dpyinfo->width = CGDisplayPixelsWide (kCGDirectMainDisplay); + } + } +#else /* !MAC_OSX */ + { + GDHandle gdh = GetMainDevice (); + Rect rect = (**gdh).gdRect; - if (dpyinfo->mouse_face_hidden) - { - dpyinfo->mouse_face_hidden = 0; - clear_mouse_face (dpyinfo); - } + dpyinfo->color_p = TestDeviceAttribute (gdh, gdDevType); + for (dpyinfo->n_planes = 32; dpyinfo->n_planes > 0; dpyinfo->n_planes >>= 1) + if (HasDepth (gdh, dpyinfo->n_planes, gdDevType, dpyinfo->color_p)) + break; - if (f) - { - WindowRef wp = FRAME_MAC_WINDOW (f); - Point mouse_pos; - - mouse_pos.h = (er.where.h - - (f->left_pos - + FRAME_OUTER_TO_INNER_DIFF_X (f))); - mouse_pos.v = (er.where.v - - (f->top_pos - + FRAME_OUTER_TO_INNER_DIFF_Y (f))); - if (dpyinfo->grabbed && tracked_scroll_bar) -#ifdef USE_TOOLKIT_SCROLL_BARS - x_scroll_bar_handle_drag (wp, tracked_scroll_bar, - mouse_pos, &inev); -#else /* not USE_TOOLKIT_SCROLL_BARS */ - x_scroll_bar_note_movement (tracked_scroll_bar, - mouse_pos.v - - XINT (tracked_scroll_bar->top), - er.when * (1000 / 60)); -#endif /* not USE_TOOLKIT_SCROLL_BARS */ - else - { - /* Generate SELECT_WINDOW_EVENTs when needed. */ - if (!NILP (Vmouse_autoselect_window)) - { - Lisp_Object window; - - window = window_from_coordinates (f, - mouse_pos.h, - mouse_pos.v, - 0, 0, 0, 0); - - /* Window will be selected only when it is - not selected now and last mouse movement - event was not in it. Minibuffer window - will be selected only when it is active. */ - if (WINDOWP (window) - && !EQ (window, last_window) - && !EQ (window, selected_window) - /* For click-to-focus window managers - create event iff we don't leave the - selected frame. */ - && (focus_follows_mouse - || (EQ (XWINDOW (window)->frame, - XWINDOW (selected_window)->frame)))) - { - inev.kind = SELECT_WINDOW_EVENT; - inev.frame_or_window = window; - } - - last_window=window; - } - if (!note_mouse_movement (f, &mouse_pos)) - help_echo_string = previous_help_echo_string; -#if USE_MAC_TOOLBAR - else - mac_tool_bar_note_mouse_movement (f, eventRef); -#endif - } - } + for (gdh = DMGetFirstScreenDevice (dmOnlyActiveDisplays); gdh; + gdh = DMGetNextScreenDevice (gdh, dmOnlyActiveDisplays)) + UnionRect (&rect, &(**gdh).gdRect, &rect); - /* If the contents of the global variable - help_echo_string has changed, generate a - HELP_EVENT. */ - if (!NILP (help_echo_string) || !NILP (previous_help_echo_string)) - do_help = 1; - break; - } - break; + dpyinfo->height = rect.bottom - rect.top; + dpyinfo->width = rect.right - rect.left; + } +#endif /* !MAC_OSX */ +} - case activateEvt: - { - WindowRef window_ptr = (WindowRef) er.message; - OSErr err; - ControlRef root_control; + +/*********************************************************************** + Initialization (Mac OS Classic) + ***********************************************************************/ - if (window_ptr == tip_window) - { - HideWindow (tip_window); - break; - } +#ifdef MAC_OS8 +extern void init_emacs_passwd_dir (); +extern int emacs_main (int, char **, char **); - if (!is_emacs_window (window_ptr)) - goto OTHER; +extern void initialize_applescript(); +extern void terminate_applescript(); - f = mac_window_to_frame (window_ptr); +static void +do_get_menus (void) +{ + Handle menubar_handle; + MenuRef menu; - if ((er.modifiers & activeFlag) != 0) - { - /* A window has been activated */ - Point mouse_loc; - - err = GetRootControl (FRAME_MAC_WINDOW (f), &root_control); - if (err == noErr) - ActivateControl (root_control); - - x_detect_focus_change (dpyinfo, &er, &inev); - - mouse_loc.h = (er.where.h - - (f->left_pos - + FRAME_OUTER_TO_INNER_DIFF_X (f))); - mouse_loc.v = (er.where.v - - (f->top_pos - + FRAME_OUTER_TO_INNER_DIFF_Y (f))); - /* Window-activated event counts as mouse movement, - so update things that depend on mouse position. */ - note_mouse_movement (f, &mouse_loc); - } - else - { - /* A window has been deactivated */ - err = GetRootControl (FRAME_MAC_WINDOW (f), &root_control); - if (err == noErr) - DeactivateControl (root_control); + menubar_handle = GetNewMBar (128); + if(menubar_handle == NULL) + abort (); + SetMenuBar (menubar_handle); + DrawMenuBar (); -#ifdef USE_TOOLKIT_SCROLL_BARS - if (dpyinfo->grabbed && tracked_scroll_bar) - { - struct input_event event; - - EVENT_INIT (event); - event.kind = NO_EVENT; - x_scroll_bar_handle_release (tracked_scroll_bar, &event); - if (event.kind != NO_EVENT) - { - event.timestamp = timestamp; - kbd_buffer_store_event_hold (&event, hold_quit); - count++; - } - } +#if !TARGET_API_MAC_CARBON + menu = GetMenuRef (M_APPLE); + if (menu != NULL) + AppendResMenu (menu, 'DRVR'); + else + abort (); #endif - dpyinfo->grabbed = 0; +} - x_detect_focus_change (dpyinfo, &er, &inev); +static void +do_init_managers (void) +{ +#if !TARGET_API_MAC_CARBON + InitGraf (&qd.thePort); + InitFonts (); + FlushEvents (everyEvent, 0); + InitWindows (); + InitMenus (); + TEInit (); + InitDialogs (NULL); +#endif /* !TARGET_API_MAC_CARBON */ + InitCursor (); - if (f == dpyinfo->mouse_face_mouse_frame) - { - /* If we move outside the frame, then we're - certainly no longer on any text in the - frame. */ - clear_mouse_face (dpyinfo); - dpyinfo->mouse_face_mouse_frame = 0; - } +#if !TARGET_API_MAC_CARBON + /* set up some extra stack space for use by emacs */ + SetApplLimit ((Ptr) ((long) GetApplLimit () - EXTRA_STACK_ALLOC)); - /* Generate a nil HELP_EVENT to cancel a help-echo. - Do it only if there's something to cancel. - Otherwise, the startup message is cleared when the - mouse leaves the frame. */ - if (any_help_event_p) - do_help = -1; - } - } - break; + /* MaxApplZone must be called for AppleScript to execute more + complicated scripts */ + MaxApplZone (); + MoreMasters (); +#endif /* !TARGET_API_MAC_CARBON */ +} - case keyDown: - case keyUp: - case autoKey: - ObscureCursor (); +static void +do_check_ram_size (void) +{ + SInt32 physical_ram_size, logical_ram_size; - f = mac_focus_frame (dpyinfo); - XSETFRAME (inev.frame_or_window, f); + if (Gestalt (gestaltPhysicalRAMSize, &physical_ram_size) != noErr + || Gestalt (gestaltLogicalRAMSize, &logical_ram_size) != noErr + || physical_ram_size > (1 << VALBITS) + || logical_ram_size > (1 << VALBITS)) + { + StopAlert (RAM_TOO_LARGE_ALERT_ID, NULL); + exit (1); + } +} - /* If mouse-highlight is an integer, input clears out mouse - highlighting. */ - if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight) - && !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)) - { - clear_mouse_face (dpyinfo); - dpyinfo->mouse_face_hidden = 1; - } +#if __profile__ +void +profiler_exit_proc () +{ + ProfilerDump ("\pEmacs.prof"); + ProfilerTerm (); +} +#endif - { - UInt32 modifiers = er.modifiers, mapped_modifiers; - UInt32 key_code = (er.message & keyCodeMask) >> 8; +/* These few functions implement Emacs as a normal Mac application + (almost): set up the heap and the Toolbox, handle necessary system + events plus a few simple menu events. They also set up Emacs's + access to functions defined in the rest of this file. Emacs uses + function hooks to perform all its terminal I/O. A complete list of + these functions appear in termhooks.h. For what they do, read the + comments there and see also w32term.c and xterm.c. What's + noticeably missing here is the event loop, which is normally + present in most Mac application. After performing the necessary + Mac initializations, main passes off control to emacs_main + (corresponding to main in emacs.c). Emacs_main calls XTread_socket + (defined further below) to read input. This is where + WaitNextEvent/ReceiveNextEvent is called to process Mac events. */ -#ifdef MAC_OSX - GetEventParameter (eventRef, kEventParamKeyModifiers, - typeUInt32, NULL, - sizeof (UInt32), NULL, &modifiers); +#undef main +int +main (void) +{ +#if __profile__ /* is the profiler on? */ + if (ProfilerInit(collectDetailed, bestTimeBase, 5000, 200)) + exit(1); #endif - mapped_modifiers = mac_mapped_modifiers (modifiers, key_code); -#if TARGET_API_MAC_CARBON - if (!(mapped_modifiers - & ~(mac_pass_command_to_system ? cmdKey : 0) - & ~(mac_pass_control_to_system ? controlKey : 0))) - goto OTHER; - else +#if __MWERKS__ + /* set creator and type for files created by MSL */ + _fcreator = MAC_EMACS_CREATOR_CODE; + _ftype = 'TEXT'; #endif - if (er.what != keyUp) - do_keystroke (er.what, er.message & charCodeMask, - key_code, modifiers, timestamp, &inev); - } - break; - case kHighLevelEvent: - AEProcessAppleEvent (&er); - break; + do_init_managers (); - default: - OTHER: -#if TARGET_API_MAC_CARBON - { - OSStatus err; + do_get_menus (); - read_socket_inev = &inev; - err = SendEventToEventTarget (eventRef, toolbox_dispatcher); - read_socket_inev = NULL; - } -#endif - break; - } -#if TARGET_API_MAC_CARBON - ReleaseEvent (eventRef); +#ifndef USE_LSB_TAG + do_check_ram_size (); #endif - if (inev.kind != NO_EVENT) - { - inev.timestamp = timestamp; - kbd_buffer_store_event_hold (&inev, hold_quit); - count++; - } + init_emacs_passwd_dir (); - if (do_help - && !(hold_quit && hold_quit->kind != NO_EVENT)) - { - Lisp_Object frame; + init_environ (); - if (f) - XSETFRAME (frame, f); - else - frame = Qnil; + init_coercion_handler (); - if (do_help > 0) - { - any_help_event_p = 1; - gen_help_event (help_echo_string, frame, help_echo_window, - help_echo_object, help_echo_pos); - } - else - { - help_echo_string = Qnil; - gen_help_event (Qnil, frame, Qnil, Qnil, 0); - } - count++; - } - } + initialize_applescript (); - /* If the focus was just given to an autoraising frame, - raise it now. */ - /* ??? This ought to be able to handle more than one such frame. */ - if (pending_autoraise_frame) - { - x_raise_frame (pending_autoraise_frame); - pending_autoraise_frame = 0; - } + init_apple_event_handler (); - if (mac_screen_config_changed) - { - mac_get_screen_info (dpyinfo); - mac_screen_config_changed = 0; - } + init_dm_notification_handler (); -#if !TARGET_API_MAC_CARBON - /* Check which frames are still visible. We do this here because - there doesn't seem to be any direct notification from the Window - Manager that the visibility of a window has changed (at least, - not in all cases). */ { - Lisp_Object tail, frame; + char **argv; + int argc = 0; - FOR_EACH_FRAME (tail, frame) - { - struct frame *f = XFRAME (frame); + /* set up argv array from STR# resource */ + get_string_list (&argv, ARGV_STRING_LIST_ID); + while (argv[argc]) + argc++; - /* The tooltip has been drawn already. Avoid the - SET_FRAME_GARBAGED in mac_handle_visibility_change. */ - if (EQ (frame, tip_frame)) - continue; + /* free up AppleScript resources on exit */ + atexit (terminate_applescript); - if (FRAME_MAC_P (f)) - mac_handle_visibility_change (f); - } - } +#if __profile__ /* is the profiler on? */ + atexit (profiler_exit_proc); #endif - --handling_signal; - UNBLOCK_INPUT; - return count; -} + /* 3rd param "envp" never used in emacs_main */ + (void) emacs_main (argc, argv, 0); + } + /* Never reached - real exit in Fkill_emacs */ + return 0; +} /* Need to override CodeWarrior's input function so no conversion is done on newlines Otherwise compiled functions in .elc files will be @@ -12435,7 +8971,6 @@ __convert_from_newlines (unsigned char * p, size_t * n) } #endif -#ifdef MAC_OS8 void make_mac_terminal_frame (struct frame *f) { @@ -12515,7 +9050,7 @@ make_mac_terminal_frame (struct frame *f) Fcons (Fcons (Qbackground_color, build_string ("white")), Qnil)); } -#endif +#endif /* MAC_OS8 */ /*********************************************************************** @@ -12671,51 +9206,6 @@ x_delete_display (dpyinfo) } -static void -init_menu_bar () -{ -#ifdef MAC_OSX - OSStatus err; - MenuRef menu; - MenuItemIndex menu_index; - - err = GetIndMenuItemWithCommandID (NULL, kHICommandQuit, 1, - &menu, &menu_index); - if (err == noErr) - SetMenuItemCommandKey (menu, menu_index, false, 0); - EnableMenuCommand (NULL, kHICommandPreferences); - err = GetIndMenuItemWithCommandID (NULL, kHICommandPreferences, 1, - &menu, &menu_index); - if (err == noErr) - { - SetMenuItemCommandKey (menu, menu_index, false, 0); - InsertMenuItemTextWithCFString (menu, NULL, - 0, kMenuItemAttrSeparator, 0); - InsertMenuItemTextWithCFString (menu, CFSTR ("About Emacs"), - 0, 0, kHICommandAbout); - } -#else /* !MAC_OSX */ -#if TARGET_API_MAC_CARBON - SetMenuItemCommandID (GetMenuRef (M_APPLE), I_ABOUT, kHICommandAbout); -#endif -#endif -} - -#if USE_MAC_TSM -static void -init_tsm () -{ -#ifdef MAC_OSX - static InterfaceTypeList types = {kUnicodeDocument}; -#else - static InterfaceTypeList types = {kTextService}; -#endif - - NewTSMDocument (sizeof (types) / sizeof (types[0]), types, - &tsm_document_id, 0); -} -#endif - /* Set up use of X before we make the first connection. */ extern frame_parm_handler mac_frame_parm_handlers[]; @@ -12794,7 +9284,6 @@ mac_initialize () baud_rate = 19200; last_tool_bar_item = -1; - any_help_event_p = 0; /* Try to use interrupt input; if we can't, then start polling. */ Fset_input_mode (Qt, Qnil, Qt, Qnil); @@ -12802,22 +9291,17 @@ mac_initialize () BLOCK_INPUT; #if TARGET_API_MAC_CARBON - - install_application_handler (); - - init_menu_bar (); - -#if USE_MAC_TSM - init_tsm (); -#endif - #ifdef MAC_OSX init_coercion_handler (); - init_apple_event_handler (); - init_dm_notification_handler (); +#endif + install_application_handler (); + + mac_toolbox_initialize (); + +#ifdef MAC_OSX if (!inhibit_window_system) { static const ProcessSerialNumber psn = {0, kCurrentProcess}; -- cgit v1.2.1 From db4bb66a325e056a4223abbf00249a2a8c0e658f Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 01:59:20 +0000 Subject: (FRAME_OUTER_TO_INNER_DIFF_X, FRAME_OUTER_TO_INNER_DIFF_Y): Move to mactoolbox.c. (HOURGLASS_WIDTH, HOURGLASS_HEIGHT): Change to 15. (Fx_selection_owner_p): Add EXFUN. (install_window_handler, remove_window_handler, XSetWindowBackground): Remove externs. (do_apple_menu) [!TARGET_API_MAC_CARBON]: Likewise. (mac_prepare_for_quickdraw) [USE_CG_DRAWING]: Likewise. (x_raise_frame, x_lower_frame, mac_alert_sound_play) (install_application_handler, mac_get_frame_bounds, mac_get_frame_mouse) (mac_convert_frame_point_to_global, mac_set_frame_window_background) (mac_update_begin mac_update_end, mac_frame_up_to_date, x_flush) (mac_create_frame_window, mac_dispose_frame_window, mac_begin_clip) (mac_end_clip, mac_create_scroll_bar, mac_dispose_scroll_bar) (mac_set_scroll_bar_bounds, mac_redraw_scroll_bar, mac_fill_menubar) (create_and_show_popup_menu, mac_get_selection_from_symbol) (mac_valid_selection_target_p, mac_clear_selection) (mac_get_selection_ownership_info, mac_valid_selection_value_p) (mac_put_selection_value, mac_selection_has_target_p) (mac_get_selection_value, mac_get_selection_target_list): Add externs. (mac_update_proxy_icon, mac_show_hourglass, mac_hide_hourglass) (mac_reposition_hourglass, mac_file_dialog, create_and_show_dialog) (mac_dnd_default_known_types) [TARGET_API_MAC_CARBON]: Likewise. (mac_run_loop_run_once) [MAC_OSX]: Likewise. (mac_dialog) [!TARGET_API_MAC_CARBON]: Likewise. (mac_begin_cg_clip, mac_end_cg_clip) [USE_CG_DRAWING]: Likewise. (x_set_toolkit_scroll_bar_thumb) [!USE_TOOLKIT_SCROLL_BARS]: Likewise. (x_scroll_bar_set_handle) [!USE_TOOLKIT_SCROLL_BARS]: Likewise. --- src/macterm.h | 116 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 86 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/macterm.h b/src/macterm.h index c95d464544a..8b9eda7a3bb 100644 --- a/src/macterm.h +++ b/src/macterm.h @@ -379,12 +379,6 @@ typedef struct mac_output mac_output; /* This is the 'font_info *' which frame F has. */ #define FRAME_MAC_FONT_TABLE(f) (FRAME_MAC_DISPLAY_INFO (f)->font_table) -/* The difference in pixels between the top left corner of the - Emacs window (including possible window manager decorations) - and FRAME_MAC_WINDOW (f). */ -#define FRAME_OUTER_TO_INNER_DIFF_X(f) ((f)->x_pixels_diff) -#define FRAME_OUTER_TO_INNER_DIFF_Y(f) ((f)->y_pixels_diff) - /* Value is the smallest width of any character in any font on frame F. */ #define FRAME_SMALLEST_CHAR_WIDTH(F) \ @@ -557,8 +551,8 @@ struct scroll_bar { #define MAC_AQUA_SMALL_VERTICAL_SCROLL_BAR_WIDTH (11) /* Size of hourglass controls */ -#define HOURGLASS_WIDTH (16) -#define HOURGLASS_HEIGHT (16) +#define HOURGLASS_WIDTH (15) +#define HOURGLASS_HEIGHT (15) /* Some constants that are used locally. */ /* Creator code for Emacs on Mac OS. */ @@ -632,6 +626,8 @@ extern int XParseGeometry P_ ((char *, int *, int *, unsigned int *, extern void x_set_window_size P_ ((struct frame *, int, int, int)); extern void x_set_mouse_position P_ ((struct frame *, int, int)); extern void x_set_mouse_pixel_position P_ ((struct frame *, int, int)); +extern void x_raise_frame P_ ((struct frame *)); +extern void x_lower_frame P_ ((struct frame *)); extern void x_make_frame_visible P_ ((struct frame *)); extern void x_make_frame_invisible P_ ((struct frame *)); extern void x_iconify_frame P_ ((struct frame *)); @@ -651,29 +647,12 @@ extern GC XCreateGC P_ ((Display *, void *, unsigned long, XGCValues *)); extern void XFreeGC P_ ((Display *, GC)); extern void XSetForeground P_ ((Display *, GC, unsigned long)); extern void XSetBackground P_ ((Display *, GC, unsigned long)); -extern void XSetWindowBackground P_ ((Display *, WindowRef, unsigned long)); extern void XDrawLine P_ ((Display *, Pixmap, GC, int, int, int, int)); extern void mac_clear_area P_ ((struct frame *, int, int, unsigned int, unsigned int)); extern void mac_unload_font P_ ((struct mac_display_info *, XFontStruct *)); -extern int mac_font_panel_visible_p P_ ((void)); -extern OSStatus mac_show_hide_font_panel P_ ((void)); -extern OSStatus mac_set_font_info_for_selection P_ ((struct frame *, int, int)); -extern OSStatus install_window_handler P_ ((WindowRef)); -extern void remove_window_handler P_ ((WindowRef)); extern OSStatus mac_post_mouse_moved_event P_ ((void)); -#if !TARGET_API_MAC_CARBON -extern void do_apple_menu P_ ((SInt16)); -#endif -#if USE_CG_DRAWING -extern void mac_prepare_for_quickdraw P_ ((struct frame *)); -#endif -extern void mac_get_window_bounds P_ ((struct frame *, Rect *, Rect *)); extern int mac_quit_char_key_p P_ ((UInt32, UInt32)); -#if USE_MAC_TOOLBAR -extern void update_frame_tool_bar P_ ((FRAME_PTR f)); -extern void free_frame_tool_bar P_ ((FRAME_PTR f)); -#endif #define FONT_TYPE_FOR_UNIBYTE(font, ch) 0 #define FONT_TYPE_FOR_MULTIBYTE(font, ch) 0 @@ -681,6 +660,7 @@ extern void free_frame_tool_bar P_ ((FRAME_PTR f)); /* Defined in macselect.c */ extern void x_clear_frame_selections P_ ((struct frame *)); +EXFUN (Fx_selection_owner_p, 1); /* Defined in macfns.c */ @@ -697,11 +677,6 @@ extern void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object)) extern void mac_update_title_bar P_ ((struct frame *, int)); extern Lisp_Object x_get_focus_frame P_ ((struct frame *)); -/* Defined in macmenu.c */ - -extern void x_activate_menubar P_ ((struct frame *)); -extern void free_frame_menubar P_ ((struct frame *)); - /* Defined in mac.c. */ extern void mac_clear_font_name_table P_ ((void)); @@ -733,5 +708,86 @@ extern Lisp_Object xrm_get_resource P_ ((XrmDatabase, const char *, extern XrmDatabase xrm_get_preference_database P_ ((const char *)); EXFUN (Fmac_get_preference, 4); +/* Defined in mactoolbox.c. */ + +extern void mac_alert_sound_play P_ ((void)); +extern OSStatus install_application_handler P_ ((void)); +extern void mac_get_window_bounds P_ ((struct frame *, Rect *, Rect *)); +extern Rect *mac_get_frame_bounds P_ ((struct frame *, Rect *)); +extern void mac_get_frame_mouse P_ ((struct frame *, Point *)); +extern void mac_convert_frame_point_to_global P_ ((struct frame *, int *, + int *)); +#if TARGET_API_MAC_CARBON +extern void mac_update_proxy_icon P_ ((struct frame *)); +#endif +extern void mac_set_frame_window_background P_ ((struct frame *, + unsigned long)); +extern void mac_update_begin P_ ((struct frame *)); +extern void mac_update_end P_ ((struct frame *)); +extern void mac_frame_up_to_date P_ ((struct frame *)); +extern void x_flush P_ ((struct frame *)); +extern void mac_create_frame_window P_ ((struct frame *, int)); +extern void mac_dispose_frame_window P_ ((struct frame *)); +#if USE_CG_DRAWING +extern CGContextRef mac_begin_cg_clip P_ ((struct frame *, GC)); +extern void mac_end_cg_clip P_ ((struct frame *)); +#endif +extern void mac_begin_clip P_ ((struct frame *, GC)); +extern void mac_end_clip P_ ((struct frame *, GC)); +extern void mac_create_scroll_bar P_ ((struct scroll_bar *, const Rect *, + Boolean)); +extern void mac_dispose_scroll_bar P_ ((struct scroll_bar *)); +extern void mac_set_scroll_bar_bounds P_ ((struct scroll_bar *, const Rect *)); +extern void mac_redraw_scroll_bar P_ ((struct scroll_bar *)); +#ifdef USE_TOOLKIT_SCROLL_BARS +extern void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *, + int, int, int)); +#else +extern void x_scroll_bar_set_handle P_ ((scroll_bar *, int, int, int)); +#endif +#if USE_MAC_FONT_PANEL +extern int mac_font_panel_visible_p P_ ((void)); +extern OSStatus mac_show_hide_font_panel P_ ((void)); +extern OSStatus mac_set_font_info_for_selection P_ ((struct frame *, int, int)); +#endif +#ifdef MAC_OSX +extern Boolean mac_run_loop_run_once P_ ((EventTimeout)); +#endif +#if USE_MAC_TOOLBAR +extern void update_frame_tool_bar P_ ((FRAME_PTR f)); +extern void free_frame_tool_bar P_ ((FRAME_PTR f)); +#endif +#if TARGET_API_MAC_CARBON +extern void mac_show_hourglass P_ ((struct frame *)); +extern void mac_hide_hourglass P_ ((struct frame *)); +extern void mac_reposition_hourglass P_ ((struct frame *)); +extern Lisp_Object mac_file_dialog P_ ((Lisp_Object, Lisp_Object, Lisp_Object, + Lisp_Object, Lisp_Object)); +#endif +extern void x_activate_menubar P_ ((struct frame *)); +extern void free_frame_menubar P_ ((struct frame *)); +extern void mac_fill_menubar P_ ((widget_value *, int)); +extern void create_and_show_popup_menu P_ ((FRAME_PTR, widget_value *, + int, int, int)); +#if TARGET_API_MAC_CARBON +extern void create_and_show_dialog P_ ((FRAME_PTR, widget_value *)); +#else +extern int mac_dialog P_ ((widget_value *)); +#endif +extern OSStatus mac_get_selection_from_symbol P_ ((Lisp_Object, int, + Selection *)); +extern int mac_valid_selection_target_p P_ ((Lisp_Object)); +extern OSStatus mac_clear_selection P_ ((Selection *)); +extern Lisp_Object mac_get_selection_ownership_info P_ ((Selection)); +extern int mac_valid_selection_value_p P_ ((Lisp_Object, Lisp_Object)); +extern OSStatus mac_put_selection_value P_ ((Selection, Lisp_Object, + Lisp_Object)); +extern int mac_selection_has_target_p P_ ((Selection, Lisp_Object)); +extern Lisp_Object mac_get_selection_value P_ ((Selection, Lisp_Object)); +extern Lisp_Object mac_get_selection_target_list P_ ((Selection)); +#if TARGET_API_MAC_CARBON +extern Lisp_Object mac_dnd_default_known_types P_ ((void)); +#endif + /* arch-tag: 6b4ca125-5bef-476d-8ee8-31ed808b7e79 (do not change this comment) */ -- cgit v1.2.1 From 9e13f0c32e8ac3b0d91bb60ffad3151063cc16e8 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sun, 6 Apr 2008 02:04:32 +0000 Subject: *** empty log message *** --- src/ChangeLog | 284 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 283 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChangeLog b/src/ChangeLog index 2082f8e27f2..ea69708e3fe 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,285 @@ +2008-04-06 YAMAMOTO Mitsuharu + + * Makefile.in (MAC_OBJ): Add mactoolbox.o. + (mactoolbox.o): New target. + + * mac.c [MAC_OSX] (select_and_poll_event, sys_select): + Use mac_run_loop_run_once instead of CFRunLoopRunInMode. + + * macfns.c (x_set_background_color, mac_window, x_create_tip_frame): + Use mac_set_frame_window_background instead of XSetWindowBackground. + (x_set_tool_bar_lines) [USE_MAC_TOOLBAR]: + Use mac_is_window_toolbar_visible instead of IsWindowToolbarVisible. + (x_set_name_internal) [TARGET_API_MAC_CARBON]: Use mac_set_window_title + instead of SetWindowTitleWithCFString. + (mac_update_proxy_icon) [TARGET_API_MAC_CARBON]: Remove BLOCK_INPUT. + Move function to mactoolbox.c. + (mac_update_title_bar) [TARGET_API_MAC_CARBON]: + Use mac_set_window_modified instead of SetWindowModified. + Add BLOCK_INPUT around mac_set_window_modified/mac_update_proxy_icon. + (mac_window, x_create_tip_frame): Use mac_create_frame_window. + (Fx_focus_frame): Use mac_front_non_floating_window instead of + FrontNonFloatingWindow. Use mac_activate_window instead of + ActivateWindow. Use mac_active_non_floating_window instead of + ActiveNonFloatingWindow. + (show_hourglass, hide_hourglass) [TARGET_API_MAC_CARBON]: + Use mac_show_hourglass and mac_hide_hourglass. + (compute_tip_xy) [TARGET_API_MAC_CARBON]: Use mac_get_global_mouse + instead of GetGlobalMouse. + (Fx_show_tip): Use mac_move_window/mac_size_window/mac_show_window + instead of MoveWindow/SizeWindow/ShowWindow, respectively. + Use mac_bring_window_to_front instead of BringToFront. + (Qfile_name_history) [TARGET_API_MAC_CARBON]: Move extern to + mactoolbox.c. + (Fx_file_dialog) [TARGET_API_MAC_CARBON]: Move function body to + mac_file_dialog in mactoolbox.c. Use mac_file_dialog. + (mac_nav_event_callback) [TARGET_API_MAC_CARBON]: Move function to + mactoolbox.c. + + * macgui.h [!HAVE_CARBON]: Include Quickdraw.h instead of QuickDraw.h. + (XtPointer): Move typedef from macmenu.c. + (enum button_type): Move enum from macmenu.c. + (widget_value): Move typedef from macmenu.c. + (M_APPLE, I_ABOUT, EXTRA_STACK_ALLOC, ARGV_STRING_LIST_ID) + (DIALOG_LEFT_MARGIN, DIALOG_TOP_MARGIN, DIALOG_RIGHT_MARGIN) + (DIALOG_BOTTOM_MARGIN, DIALOG_MIN_INNER_WIDTH, DIALOG_MAX_INNER_WIDTH) + (DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE) + (DIALOG_BUTTON_BUTTON_VERTICAL_SPACE, DIALOG_BUTTON_MIN_WIDTH) + (DIALOG_TEXT_MIN_HEIGHT, DIALOG_TEXT_BUTTONS_VERTICAL_SPACE) + (DIALOG_ICON_WIDTH, DIALOG_ICON_HEIGHT, DIALOG_ICON_LEFT_MARGIN) + (DIALOG_ICON_TOP_MARGIN): Move defines from macmenu.c. + (Selection): Move typedef from macselect.c. + (RAM_TOO_LARGE_ALERT_ID, ABOUT_ALERT_ID) [MAC_OS8]: Move defines from + macterm.c. + (mac_set_window_title, mac_set_window_modified, mac_is_window_visible) + (mac_is_window_collapsed, mac_bring_window_to_front) + (mac_send_window_behind, mac_hide_window, mac_show_window) + (mac_collapse_window, mac_front_non_floating_window) + (mac_active_non_floating_window, mac_activate_window) + (mac_move_window_structure, mac_move_window, mac_size_window) + (mac_get_global_mouse, mac_is_window_toolbar_visible): New defines. + + * macmenu.c [!TARGET_API_MAC_CARBON]: Move includes to mactoolbox.c. + (enum mac_menu_kind): Move enum to mactoolbox.c. + (min_menu_id): Move variable to mactoolbox.c. + (quit_dialog_event_loop) [TARGET_API_MAC_CARBON]: Likewise. + (DIALOG_WINDOW_RESOURCE): Move define to mactoolbox.c. + (DIALOG_BUTTON_COMMAND_ID_OFFSET, DIALOG_BUTTON_COMMAND_ID_P) + (DIALOG_BUTTON_COMMAND_ID_VALUE, DIALOG_BUTTON_MAKE_COMMAND_ID) + [TARGET_API_MAC_CARBON]: Likewise. + (XtPointer): Move typedef to macgui.h. + (enum button_type): Move enum to macgui.h. + (widget_value): Move typedef to macgui.h. + (DIALOG_LEFT_MARGIN, DIALOG_TOP_MARGIN, DIALOG_RIGHT_MARGIN) + (DIALOG_BOTTOM_MARGIN, DIALOG_MIN_INNER_WIDTH, DIALOG_MAX_INNER_WIDTH) + (DIALOG_BUTTON_BUTTON_HORIZONTAL_SPACE) + (DIALOG_BUTTON_BUTTON_VERTICAL_SPACE, DIALOG_BUTTON_MIN_WIDTH) + (DIALOG_TEXT_MIN_HEIGHT, DIALOG_TEXT_BUTTONS_VERTICAL_SPACE) + (DIALOG_ICON_WIDTH, DIALOG_ICON_HEIGHT, DIALOG_ICON_LEFT_MARGIN) + (DIALOG_ICON_TOP_MARGIN): Move defines to macgui.h. + (popup_activated_flag): Make variable non-static. + (x_activate_menubar, install_menu_quit_handler, pop_down_menu) + (add_menu_item, fill_menu, dispose_menus): + Move functions to mactoolbox.c. + (restore_show_help_function, menu_target_item_handler) + (install_menu_target_item_handler, mac_handle_dialog_event) + (install_dialog_event_handler, pop_down_dialog, create_and_show_dialog) + [TARGET_API_MAC_CARBON]: Likewise. + (menu_quit_handler) [MAC_OS_X_VERSION_MAX_ALLOWED >= 1030]: Likewise. + (mac_dialog) [!TARGET_API_MAC_CARBON]: Likewise. + (find_and_call_menu_selection, name_is_separator): Make function + non-static. + (Vshow_help_function, timer_check) [TARGET_API_MAC_CARBON]: Move extern + to mactoolbox.c. + (set_frame_menubar): Don't call install_menu_quit_handler. + (menu_item_selection): New variable. + (mac_menu_show): Use create_and_show_popup_menu. + (create_and_show_dialog) [TARGET_API_MAC_CARBON]: Don't return + selection but set variable menu_item_selection. All uses changed. + (mac_fill_menubar): Rename from fill_menubar. All uses changed. + Call install_menu_quit_handler. Move to mactoolbox.c. + + * macselect.c [!TARGET_API_MAC_CARBON]: Don't include Scrap.h. + (Selection): Move typedef to macgui.h. + (Vselection_converter_alist, Qmac_scrap_name, Qmac_ostype) + (Vmac_apple_event_map, Qmac_apple_event_class, Qmac_apple_event_id): + Make variables non-static. + (Vmac_dnd_known_types) [TARGET_API_MAC_CARBON]: Likewise. + (mac_handle_apple_event, cleanup_all_suspended_apple_events): + Make functions non-static. + (Vmac_service_selection) [MAC_OSX]: Likewise. + (mac_get_selection_from_symbol, get_flavor_type_from_symbol) + (mac_valid_selection_target_p, mac_clear_selection) + (mac_get_selection_ownership_info, mac_valid_selection_value_p) + (mac_put_selection_value, mac_selection_has_target_p) + (mac_get_selection_value, mac_get_selection_target_list) + (init_apple_event_handler, install_drag_handler, remove_drag_handler): + Move functions to mactoolbox.c. + (mac_do_track_drag, mac_do_receive_drag) [TARGET_API_MAC_CARBON]: + Likewise. + (copy_scrap_flavor_data, mac_handle_service_event) + (install_service_handler) [MAC_OSX]: Likewise. + (syms_of_macselect) : + Use mac_dnd_default_known_types. + + * macterm.c (mac_end_cg_clip): Add argument F. All uses changed. + (mac_begin_cg_clip, mac_end_cg_clip): Allow null GC. + (mac_invert_rectangle, mac_compute_glyph_string_overhangs) + (mac_load_query_font): Use them instead of SetPortWindowPort. + (mac_clear_window) [!USE_CG_DRAWING]: Likewise. + (mac_draw_image_string_cg): Call CGContextSetTextMatrix. + (x_update_begin, x_update_end): Call mac_update_begin and + mac_update_end. + (XTframe_up_to_date): Call mac_frame_up_to_date. + (XTring_bell): Use mac_alert_sound_play. + (note_mouse_movement): Use mac_get_frame_bounds. + (XTmouse_position): Use mac_get_frame_mouse. + (x_scroll_bar_create): Use mac_create_scroll_bar. + (x_scroll_bar_remove): Use mac_dispose_scroll_bar. + (XTset_vertical_scroll_bar): Use mac_set_scroll_bar_bounds and + mac_redraw_scroll_bar. + (mac_move_window_with_gravity) [USE_MAC_TOOLBAR]: Use mac_move_window + instead of MoveWindow. + (mac_handle_size_change) [TARGET_API_MAC_CARBON]: + Use mac_reposition_hourglass. + (x_set_offset): Use mac_move_window_structure instead of + MoveWindowStructure. + (x_set_window_size): Use mac_size_window instead of SizeWindow. + (x_set_mouse_pixel_position) [MAC_OSX]: + Use mac_convert_frame_point_to_global. + (x_raise_frame): Use mac_bring_window_to_front instead of BringToFront. + (x_lower_frame): Use mac_send_window_behind instead of SendBehind. + (mac_handle_visibility_change): Use Window instead of WindowRef. + Use mac_is_window_visible/mac_is_window_collapsed instead of + IsWindowVisible/IsWindowCollapsed, respectively. + Use mac_collapse_window/mac_show_window instead of + CollapseWindow/ShowWindow, respectively. + (x_make_frame_invisible): Use mac_hide_window instead of HideWindow. + (x_iconify_frame): Use mac_show_window instead of ShowWindow. + Use mac_collapse_window instead of CollapseWindow. + (x_free_frame_resources): Use Window instead of WindowRef. + Use mac_dispose_frame_window. Clean up focus-related variables before + calling mac_dispose_frame_window. + (do_zoom_window) [MAC_OS8]: Use mac_clear_area instead of + mac_clear_window. + (mac_initialize): Use mac_toolbox_initialize instead of + initializing any_help_event_p and calling init_apple_event_handler, + init_tsm, and init_menu_bar. + (any_help_event_p, last_window, save_port_clip_region) + (read_socket_inev, saved_menu_event_location): Move variables to + mactoolbox.c. + (last_scroll_bar_part, scroll_bar_timer) + (scroll_bar_timer_event_posted_p) [USE_TOOLKIT_SCROLL_BARS]: Likewise. + (font_panel_shown_p) [USE_MAC_FONT_PANEL]: Likewise. + (tsm_document_id) [USE_MAC_TSM]: Likewise. + (mouse_region) [!TARGET_API_MAC_CARBON]: Likewise. + (mac_window_to_frame, DEFAULT_NUM_COLS, MIN_DOC_SIZE, MAX_DOC_SIZE): + Move defines to mactoolbox.c. + (FRAME_CG_CONTEXT) [USE_CG_DRAWING]: Likewise. + (SCROLL_BAR_FIRST_DELAY, SCROLL_BAR_CONTINUOUS_DELAY) + [USE_TOOLKIT_SCROLL_BARS]: Likewise. + (TOOLBAR_IDENTIFIER, TOOLBAR_ICON_ITEM_IDENTIFIER) + (TOOLBAR_ITEM_COMMAND_ID_OFFSET, TOOLBAR_ITEM_COMMAND_ID_P) + (TOOLBAR_ITEM_COMMAND_ID_VALUE, TOOLBAR_ITEM_MAKE_COMMAND_ID) + [USE_MAC_TOOLBAR]: Likewise. + (M_APPLE, I_ABOUT, EXTRA_STACK_ALLOC, ARGV_STRING_LIST_ID) + (RAM_TOO_LARGE_ALERT_ID, ABOUT_ALERT_ID): Move defines to macgui.h + (x_flush, is_emacs_window, mac_begin_clip, mac_end_clip) + (x_scroll_bar_handle_click, x_scroll_bar_report_motion) + (mac_get_window_bounds, do_window_update, is_emacs_window) + (do_grow_window, do_zoom_window, install_window_handler) + (remove_window_handler, XTread_socket, init_menu_bar): Move functions + to mactoolbox.c. + (mac_flush_display_optional, mac_begin_cg_clip, mac_end_cg_clip) + (mac_prepare_for_quickdraw) [USE_CG_DRAWING]: Likewise. + (mac_scroll_area, mac_event_to_emacs_modifiers, mac_get_mouse_btn) + (mac_convert_event_ref, mac_get_ideal_size, mac_store_drag_event) + (mac_handle_window_event, mac_handle_keyboard_event) + (mac_handle_command_event, mac_handle_mouse_event) + (install_application_handler, mac_post_mouse_moved_event) + [TARGET_API_MAC_CARBON]: Likewise. + (scroll_bar_timer_callback, install_scroll_bar_timer) + (set_scroll_bar_timer, control_part_code_to_scroll_bar_part) + (construct_scroll_bar_click, get_control_part_bounds) + (x_scroll_bar_handle_press, x_scroll_bar_handle_release) + (x_scroll_bar_handle_drag, x_set_toolkit_scroll_bar_thumb) + [USE_TOOLKIT_SCROLL_BARS]: Likewise. + (x_scroll_bar_set_handle, x_scroll_bar_note_movement) + [!USE_TOOLKIT_SCROLL_BARS]: Likewise. + (mac_handle_toolbar_event, mac_create_frame_tool_bar) + (update_frame_tool_bar, free_frame_tool_bar) + (mac_tool_bar_note_mouse_movement, mac_handle_toolbar_command_event) + [USE_MAC_TOOLBAR]: Likewise. + (mac_font_panel_visible_p, mac_handle_font_event) + (mac_show_hide_font_panel, mac_set_font_info_for_selection) + [USE_MAC_FONT_PANEL]: Likewise. + (mac_handle_text_input_event, init_tsm) [USE_MAC_TSM]: Likewise. + (do_apple_menu, mac_wait_next_event) [!TARGET_API_MAC_CARBON]: Likewise. + (mac_store_service_event) [MAC_OSX]: Likewise. + (last_mouse_glyph, last_mouse_glyph_frame, last_mouse_scroll_bar) + (last_mouse_movement_time, input_signal_count) + (mac_screen_config_changed, Qhi_command, Qtoolbar_switch_mode) + (Qservice, Qpaste, Qperform, keycode_to_xkeysym_table): Make variables + non-static. + (Qpanel_closed, Qselection) [USE_MAC_FONT_PANEL]: Likewise. + (Qtext_input, Vmac_ts_active_input_overlay, Qupdate_active_input_area) + (Qunicode_for_key_event, Vmac_ts_script_language_on_focus) + (saved_ts_script_language_on_focus) [USE_MAC_TSM]: Likewise. + (mac_focus_changed, note_mouse_movement, mac_focus_frame) + (mac_handle_origin_change, mac_handle_size_change) + (mac_handle_visibility_change, mac_to_emacs_modifiers) + (mac_mapped_modifiers, mac_get_emulated_btn, do_keystroke) + (mac_get_screen_info): Make functions non-static. + (mac_move_window_with_gravity, mac_get_window_origin_with_gravity) + (mac_image_spec_to_cg_image) [USE_MAC_TOOLBAR]: Likewise. + (mac_store_event_ref_as_apple_event) [TARGET_API_MAC_CARBON]: Likewise. + (Qwindow, mac_ready_for_apple_events): Move externs to mactoolbox.c. + (Qbefore_string) [USE_MAC_TSM]: Likewise. + (mac_toolbox_initialize, x_scroll_bar_report_motion, XTread_socket): + Add externs. + (mac_flush_display_optional) [USE_CG_DRAWING]: Likewise. + (install_drag_handler, remove_drag_handler, install_service_handler) + (install_menu_target_item_handler): Remove externs. + (XSetWindowBackground): Rename to mac_set_frame_window_background. + Take frame as argument instead of display and window. + Move to mactoolbox.c. + (mac_restore_keyboard_input_source, mac_save_keyboard_input_source) + [USE_MAC_TSM]: New functions created from mac_tsm_resume and + mac_tsm_suspend, respectively. + (mac_tsm_resume, mac_tsm_suspend) [USE_MAC_TSM]: Use them. + Move to mactoolbox.c. + + * macterm.h (FRAME_OUTER_TO_INNER_DIFF_X, FRAME_OUTER_TO_INNER_DIFF_Y): + Move to mactoolbox.c. + (HOURGLASS_WIDTH, HOURGLASS_HEIGHT): Change to 15. + (Fx_selection_owner_p): Add EXFUN. + (install_window_handler, remove_window_handler, XSetWindowBackground): + Remove externs. + (do_apple_menu) [!TARGET_API_MAC_CARBON]: Likewise. + (mac_prepare_for_quickdraw) [USE_CG_DRAWING]: Likewise. + (x_raise_frame, x_lower_frame, mac_alert_sound_play) + (install_application_handler, mac_get_frame_bounds, mac_get_frame_mouse) + (mac_convert_frame_point_to_global, mac_set_frame_window_background) + (mac_update_begin mac_update_end, mac_frame_up_to_date, x_flush) + (mac_create_frame_window, mac_dispose_frame_window, mac_begin_clip) + (mac_end_clip, mac_create_scroll_bar, mac_dispose_scroll_bar) + (mac_set_scroll_bar_bounds, mac_redraw_scroll_bar, mac_fill_menubar) + (create_and_show_popup_menu, mac_get_selection_from_symbol) + (mac_valid_selection_target_p, mac_clear_selection) + (mac_get_selection_ownership_info, mac_valid_selection_value_p) + (mac_put_selection_value, mac_selection_has_target_p) + (mac_get_selection_value, mac_get_selection_target_list): Add externs. + (mac_update_proxy_icon, mac_show_hourglass, mac_hide_hourglass) + (mac_reposition_hourglass, mac_file_dialog, create_and_show_dialog) + (mac_dnd_default_known_types) [TARGET_API_MAC_CARBON]: Likewise. + (mac_run_loop_run_once) [MAC_OSX]: Likewise. + (mac_dialog) [!TARGET_API_MAC_CARBON]: Likewise. + (mac_begin_cg_clip, mac_end_cg_clip) [USE_CG_DRAWING]: Likewise. + (x_set_toolkit_scroll_bar_thumb) [!USE_TOOLKIT_SCROLL_BARS]: Likewise. + (x_scroll_bar_set_handle) [!USE_TOOLKIT_SCROLL_BARS]: Likewise. + + * mactoolbox.c: New file. + 2008-04-05 Chong Yidong * search.c (compile_pattern_1): Treat non-nil and non-string of @@ -132,7 +414,7 @@ (x_make_frame_visible) [TARGET_API_MAC_CARBON]: Move code for repositioning window to mac_handle_window_event. (x_make_frame_invisible) [TARGET_API_MAC_CARBON]: Move code for - saving window location to mac_handle_window_event + saving window location to mac_handle_window_event. [USE_MAC_FONT_PANEL] (mac_show_hide_font_panel): Install handler here. (install_menu_target_item_handler): Remove argument in extern. [TARGET_API_MAC_CARBON] (mac_event_to_emacs_modifiers): -- cgit v1.2.1 From bfea922885434e9af85abc164b836cbe639955f2 Mon Sep 17 00:00:00 2001 From: Miles Bader Date: Mon, 7 Apr 2008 07:13:07 +0000 Subject: Add arch tagline --- src/mactoolbox.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/mactoolbox.c b/src/mactoolbox.c index e1fa79ac8be..6b4d22049a9 100644 --- a/src/mactoolbox.c +++ b/src/mactoolbox.c @@ -6206,3 +6206,6 @@ mac_toolbox_initialize () init_tsm (); #endif } + +/* arch-tag: 71a597a8-6e9f-47b0-8b89-5a5ae3e16516 + (do not change this comment) */ -- cgit v1.2.1 From ac0af1135c279f01e8e20176414b4c9c43e7304c Mon Sep 17 00:00:00 2001 From: Jason Rumney Date: Thu, 10 Apr 2008 10:42:22 +0000 Subject: (stat): When Vw32_get_true_file_attributes is Qlocal, get attributes only for local files. --- src/w32.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src') diff --git a/src/w32.c b/src/w32.c index e40f9a4b289..bc26d75e78e 100644 --- a/src/w32.c +++ b/src/w32.c @@ -109,6 +109,9 @@ void globals_of_w32 (); extern Lisp_Object Vw32_downcase_file_names; extern Lisp_Object Vw32_generate_fake_inodes; extern Lisp_Object Vw32_get_true_file_attributes; +/* Defined in process.c for its own purpose. */ +extern Lisp_Object Qlocal; + extern int w32_num_mouse_buttons; @@ -2489,6 +2492,8 @@ stat (const char * path, struct stat * buf) } if (!NILP (Vw32_get_true_file_attributes) + && !(EQ (vw32_get_true_file_attributes, Qlocal) && + GetDriveType (name) == DRIVE_FIXED) /* No access rights required to get info. */ && (fh = CreateFile (name, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL)) -- cgit v1.2.1 From 2fa4f0908a61d12a697265793b28fd26cf2e0e9f Mon Sep 17 00:00:00 2001 From: Jason Rumney Date: Thu, 10 Apr 2008 10:42:54 +0000 Subject: (syms_of_ntproc): Change Vw32_get_true_file attributes default to Qlocal. --- src/w32proc.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/w32proc.c b/src/w32proc.c index f050cacf82d..3756f4a8dd1 100644 --- a/src/w32proc.c +++ b/src/w32proc.c @@ -109,6 +109,7 @@ Lisp_Object Vw32_generate_fake_inodes; exactly, at the expense of slower operation. Since true hard links are supported on NTFS volumes, this is only relevant on NT. */ Lisp_Object Vw32_get_true_file_attributes; +extern Lisp_Object Qlocal; Lisp_Object Qhigh, Qlow; @@ -2354,9 +2355,12 @@ the truename of a file can be slow. */); DEFVAR_LISP ("w32-get-true-file-attributes", &Vw32_get_true_file_attributes, doc: /* Non-nil means determine accurate link count in `file-attributes'. -Note that this option is only useful for files on NTFS volumes, where hard links -are supported. Moreover, it slows down `file-attributes' noticeably. */); - Vw32_get_true_file_attributes = Qt; +This option is only useful for files on NTFS volumes, where +hard links are supported. The default value `local' means only do +this for files on local harddrives. Any other non-nil value means do +this even on remote and removable drives where the performance impact +may be noticeable even on modern hardware. */); + Vw32_get_true_file_attributes = Qlocal; staticpro (&Vw32_valid_locale_ids); staticpro (&Vw32_valid_codepages); -- cgit v1.2.1 From bd73563158de9b663c701735c96db760c0ed20c4 Mon Sep 17 00:00:00 2001 From: Jason Rumney Date: Thu, 10 Apr 2008 10:44:51 +0000 Subject: Fix last change. --- src/ChangeLog | 8 ++++++++ src/w32.c | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChangeLog b/src/ChangeLog index ea69708e3fe..81379fd1130 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2008-04-10 Jason Rumney + + * w32.c (stat): When Vw32_get_true_file_attributes is Qlocal, get + attributes only for local files. + + * w32proc.c (syms_of_ntproc): Change Vw32_get_true_file attributes + default to Qlocal. + 2008-04-06 YAMAMOTO Mitsuharu * Makefile.in (MAC_OBJ): Add mactoolbox.o. diff --git a/src/w32.c b/src/w32.c index bc26d75e78e..894160a275d 100644 --- a/src/w32.c +++ b/src/w32.c @@ -2492,7 +2492,7 @@ stat (const char * path, struct stat * buf) } if (!NILP (Vw32_get_true_file_attributes) - && !(EQ (vw32_get_true_file_attributes, Qlocal) && + && !(EQ (Vw32_get_true_file_attributes, Qlocal) && GetDriveType (name) == DRIVE_FIXED) /* No access rights required to get info. */ && (fh = CreateFile (name, 0, 0, NULL, OPEN_EXISTING, -- cgit v1.2.1 From 474d35ca3f9f6942ddaaee2298e83185a736a688 Mon Sep 17 00:00:00 2001 From: Michael Albinus Date: Thu, 10 Apr 2008 20:47:59 +0000 Subject: * fileio.c (Fmake_symbolic_link): Surround code by #ifdef S_IFLNK only after check for file name handler functions. Signal, when native functionality is not supported. (syms_of_fileio): Declare it unconditionally. --- src/ChangeLog | 7 +++++++ src/fileio.c | 11 +++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ChangeLog b/src/ChangeLog index 81379fd1130..1894f6d99ad 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2008-04-10 Michael Albinus + + * fileio.c (Fmake_symbolic_link): Surround code by #ifdef S_IFLNK + only after check for file name handler functions. Signal, when + native functionality is not supported. + (syms_of_fileio): Declare it unconditionally. + 2008-04-10 Jason Rumney * w32.c (stat): When Vw32_get_true_file_attributes is Qlocal, get diff --git a/src/fileio.c b/src/fileio.c index 511cca59fdb..cc6c7c2d3a7 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -2871,7 +2871,6 @@ This is what happens in interactive use with M-x. */) return Qnil; } -#ifdef S_IFLNK DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3, "FMake symbolic link to file: \nGMake symbolic link to file %s: \np", doc: /* Make a symbolic link to FILENAME, named LINKNAME. @@ -2916,6 +2915,7 @@ This happens for interactive use with M-x. */) RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, linkname, ok_if_already_exists)); +#ifdef S_IFLNK encoded_filename = ENCODE_FILE (filename); encoded_linkname = ENCODE_FILE (linkname); @@ -2942,8 +2942,13 @@ This happens for interactive use with M-x. */) } UNGCPRO; return Qnil; -} + +#else + UNGCPRO; + xsignal1 (Qfile_error, build_string ("Symbolic links are not supported")); + #endif /* S_IFLNK */ +} #ifdef VMS @@ -6779,9 +6784,7 @@ A non-nil value may result in data loss! */); defsubr (&Sdelete_file); defsubr (&Srename_file); defsubr (&Sadd_name_to_file); -#ifdef S_IFLNK defsubr (&Smake_symbolic_link); -#endif /* S_IFLNK */ #ifdef VMS defsubr (&Sdefine_logical_name); #endif /* VMS */ -- cgit v1.2.1 From da44dd8487c33726ef29109ab4a221eca7719a12 Mon Sep 17 00:00:00 2001 From: Katsumi Yamaoka Date: Fri, 11 Apr 2008 10:23:49 +0000 Subject: (BASE_PURESIZE): Increase to 1140000. --- src/ChangeLog | 4 ++++ src/puresize.h | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChangeLog b/src/ChangeLog index 1894f6d99ad..0dabe60be18 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2008-04-11 Katsumi Yamaoka + + * puresize.h (BASE_PURESIZE): Increase to 1140000. + 2008-04-10 Michael Albinus * fileio.c (Fmake_symbolic_link): Surround code by #ifdef S_IFLNK diff --git a/src/puresize.h b/src/puresize.h index 23a77646e1e..0306194395b 100644 --- a/src/puresize.h +++ b/src/puresize.h @@ -43,7 +43,7 @@ Boston, MA 02110-1301, USA. */ #endif #ifndef BASE_PURESIZE -#define BASE_PURESIZE (1130000 + SYSTEM_PURESIZE_EXTRA + SITELOAD_PURESIZE_EXTRA) +#define BASE_PURESIZE (1140000 + SYSTEM_PURESIZE_EXTRA + SITELOAD_PURESIZE_EXTRA) #endif /* Increase BASE_PURESIZE by a ratio depending on the machine's word size. */ -- cgit v1.2.1 From a1b41389e4069747940c87b477f11852ec86d9f3 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Mitsuharu Date: Sat, 12 Apr 2008 01:59:22 +0000 Subject: [!SYSTEM_MALLOC && !SYNC_INPUT] (uninterrupt_malloc) [HAVE_GTK_AND_PTHREAD && !DOUG_LEA_MALLOC]: Don't use recursive mutex. --- src/ChangeLog | 5 +++++ src/alloc.c | 6 ++++++ 2 files changed, 11 insertions(+) (limited to 'src') diff --git a/src/ChangeLog b/src/ChangeLog index 0dabe60be18..56676ea95d6 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2008-04-12 YAMAMOTO Mitsuharu + + * alloc.c [!SYSTEM_MALLOC && !SYNC_INPUT] (uninterrupt_malloc) + [HAVE_GTK_AND_PTHREAD && !DOUG_LEA_MALLOC]: Don't use recursive mutex. + 2008-04-11 Katsumi Yamaoka * puresize.h (BASE_PURESIZE): Increase to 1140000. diff --git a/src/alloc.c b/src/alloc.c index 153c54e0264..46887bb332c 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -1356,6 +1356,7 @@ void uninterrupt_malloc () { #ifdef HAVE_GTK_AND_PTHREAD +#ifdef DOUG_LEA_MALLOC pthread_mutexattr_t attr; /* GLIBC has a faster way to do this, but lets keep it portable. @@ -1363,6 +1364,11 @@ uninterrupt_malloc () pthread_mutexattr_init (&attr); pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init (&alloc_mutex, &attr); +#else /* !DOUG_LEA_MALLOC */ + /* Some systems such as Solaris 2.6 doesn't have a recursive mutex, + and the bundled gmalloc.c doesn't require it. */ + pthread_mutex_init (&alloc_mutex, NULL); +#endif /* !DOUG_LEA_MALLOC */ #endif /* HAVE_GTK_AND_PTHREAD */ if (__free_hook != emacs_blocked_free) -- cgit v1.2.1