+ return eventNotHandledErr;
+}
+
+static OSErr
+init_command_handler ()
+{
+ OSErr err = noErr;
+ EventTypeSpec specs[] = {{kEventClassCommand, kEventCommandProcess}};
+ static EventHandlerUPP handle_command_eventUPP = NULL;
+
+ if (handle_command_eventUPP == NULL)
+ handle_command_eventUPP = NewEventHandlerUPP (mac_handle_command_event);
+ return InstallApplicationEventHandler (handle_command_eventUPP,
+ GetEventTypeCount (specs), specs,
+ NULL, NULL);
+}
+
+static pascal OSStatus
+mac_handle_window_event (next_handler, event, data)
+ EventHandlerCallRef next_handler;
+ EventRef event;
+ void *data;
+{
+ WindowPtr wp;
+ OSStatus result;
+ UInt32 attributes;
+ XSizeHints *size_hints;
+
+ GetEventParameter (event, kEventParamDirectObject, typeWindowRef,
+ NULL, sizeof (WindowPtr), NULL, &wp);
+
+ switch (GetEventKind (event))
+ {
+ case kEventWindowUpdate:
+ result = CallNextEventHandler (next_handler, event);
+ if (result != eventNotHandledErr)
+ return result;
+
+ do_window_update (wp);
+ return noErr;
+
+ case kEventWindowBoundsChanging:
+ result = CallNextEventHandler (next_handler, event);
+ if (result != eventNotHandledErr)
+ return result;
+
+ GetEventParameter (event, kEventParamAttributes, typeUInt32,
+ NULL, sizeof (UInt32), NULL, &attributes);
+ size_hints = FRAME_SIZE_HINTS (mac_window_to_frame (wp));
+ if ((attributes & kWindowBoundsChangeUserResize)
+ && ((size_hints->flags & (PResizeInc | PBaseSize | PMinSize))
+ == (PResizeInc | PBaseSize | PMinSize)))
+ {
+ Rect bounds;
+ int width, height;
+
+ GetEventParameter (event, kEventParamCurrentBounds,
+ typeQDRectangle,
+ NULL, sizeof (Rect), NULL, &bounds);
+ 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);
+ return noErr;
+ }
+ break;
+
+ case kEventWindowShown:
+ case kEventWindowHidden:
+ case kEventWindowExpanded:
+ case kEventWindowCollapsed:
+ result = CallNextEventHandler (next_handler, event);
+
+ mac_handle_visibility_change (mac_window_to_frame (wp));
+ return noErr;
+
+ break;
+ }
+
+ return eventNotHandledErr;
+}
+
+static pascal OSStatus
+mac_handle_mouse_event (next_handler, event, data)
+ EventHandlerCallRef next_handler;
+ EventRef event;
+ void *data;
+{
+ OSStatus result;
+
+ switch (GetEventKind (event))
+ {
+ case kEventMouseWheelMoved:
+ {
+ WindowPtr wp;
+ struct frame *f;
+ EventMouseWheelAxis axis;
+ SInt32 delta;
+ Point point;
+
+ result = CallNextEventHandler (next_handler, event);
+ if (result != eventNotHandledErr || read_socket_inev == NULL)
+ return result;
+
+ GetEventParameter (event, kEventParamWindowRef, typeWindowRef,
+ NULL, sizeof (WindowRef), NULL, &wp);
+ f = mac_window_to_frame (wp);
+ if (f != mac_focus_frame (&one_mac_display_info))
+ break;
+
+ GetEventParameter (event, kEventParamMouseWheelAxis,
+ typeMouseWheelAxis, NULL,
+ sizeof (EventMouseWheelAxis), NULL, &axis);
+ if (axis != kEventMouseWheelAxisY)
+ break;
+
+ GetEventParameter (event, kEventParamMouseWheelDelta, typeSInt32,
+ NULL, sizeof (SInt32), NULL, &delta);
+ GetEventParameter (event, kEventParamMouseLocation, typeQDPoint,
+ NULL, sizeof (Point), NULL, &point);
+ 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));
+ SetPortWindowPort (wp);
+ GlobalToLocal (&point);
+ XSETINT (read_socket_inev->x, point.h);
+ XSETINT (read_socket_inev->y, point.v);
+ XSETFRAME (read_socket_inev->frame_or_window, f);
+
+ return noErr;
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ return eventNotHandledErr;
+}
+#endif /* USE_CARBON_EVENTS */
+
+
+OSErr
+install_window_handler (window)
+ WindowPtr window;
+{
+ OSErr err = noErr;
+#if USE_CARBON_EVENTS
+ EventTypeSpec specs_window[] =
+ {{kEventClassWindow, kEventWindowUpdate},
+ {kEventClassWindow, kEventWindowBoundsChanging},
+ {kEventClassWindow, kEventWindowShown},
+ {kEventClassWindow, kEventWindowHidden},
+ {kEventClassWindow, kEventWindowExpanded},
+ {kEventClassWindow, kEventWindowCollapsed}};
+ EventTypeSpec specs_mouse[] = {{kEventClassMouse, kEventMouseWheelMoved}};
+ static EventHandlerUPP handle_window_eventUPP = NULL;
+ static EventHandlerUPP handle_mouse_eventUPP = NULL;
+
+ if (handle_window_eventUPP == NULL)
+ handle_window_eventUPP = NewEventHandlerUPP (mac_handle_window_event);
+ if (handle_mouse_eventUPP == NULL)
+ handle_mouse_eventUPP = NewEventHandlerUPP (mac_handle_mouse_event);
+ err = InstallWindowEventHandler (window, handle_window_eventUPP,
+ GetEventTypeCount (specs_window),
+ specs_window, NULL, NULL);
+ if (err == noErr)
+ err = InstallWindowEventHandler (window, handle_mouse_eventUPP,
+ GetEventTypeCount (specs_mouse),
+ specs_mouse, NULL, NULL);
+#endif
+#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);
+
+ if (err == noErr)
+ err = InstallTrackingHandler (mac_do_track_dragUPP, window, NULL);
+ if (err == noErr)
+ err = InstallReceiveHandler (mac_do_receive_dragUPP, window, NULL);
+#endif
+ return err;
+}
+
+void
+remove_window_handler (window)
+ WindowPtr 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
+}
+
+/* Open Application Apple Event */
+static pascal OSErr
+do_ae_open_application(const AppleEvent *pae, AppleEvent *preply, long prefcon)
+{
+ return noErr;
+}
+
+
+/* Called when we receive an AppleEvent with an ID of
+ "kAEOpenDocuments". This routine gets the direct parameter,
+ extracts the FSSpecs in it, and puts their names on a list. */
+#pragma options align=mac68k
+typedef struct SelectionRange {
+ short unused1; // 0 (not used)
+ short lineNum; // line to select (<0 to specify range)
+ long startRange; // start of selection range (if line < 0)
+ long endRange; // end of selection range (if line < 0)
+ long unused2; // 0 (not used)
+ long theDate; // modification date/time
+} SelectionRange;
+#pragma options align=reset
+
+static pascal OSErr
+do_ae_open_documents(AppleEvent *message, AppleEvent *reply, long refcon)
+{
+ OSErr err, err2;
+ AEDesc the_desc;
+ AEKeyword keyword;
+ DescType actual_type;
+ Size actual_size;
+ SelectionRange position;
+ Lisp_Object file_list = Qnil;
+
+ xassert (read_socket_inev);
+
+ err = AEGetParamDesc (message, keyDirectObject, typeAEList, &the_desc);
+ if (err != noErr)
+ goto descriptor_error_exit;
+
+ err = AEGetParamPtr (message, keyAEPosition, typeChar, &actual_type, &position, sizeof(SelectionRange), &actual_size);
+ if (err == noErr)
+ file_list = Fcons (list3 (make_number (position.lineNum + 1),
+ make_number (position.startRange + 1),
+ make_number (position.endRange + 1)),
+ file_list);
+
+ /* Check to see that we got all of the required parameters from the
+ event descriptor. For an 'odoc' event this should just be the
+ file list. */
+ err = AEGetAttributePtr(message, keyMissedKeywordAttr, typeWildCard,