+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;
+}