3 * Copyright (C) 2005-2007 Imendio AB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #import "GdkQuartzNSWindow.h"
22 #include "gdkquartzwindow.h"
23 #include "gdkdnd-quartz.h"
24 #include "gdkprivate-quartz.h"
26 @implementation GdkQuartzNSWindow
28 -(BOOL)windowShouldClose:(id)sender
30 GdkWindow *window = [[self contentView] gdkWindow];
33 event = gdk_event_new (GDK_DELETE);
35 event->any.window = g_object_ref (window);
36 event->any.send_event = FALSE;
38 _gdk_event_queue_append (gdk_display_get_default (), event);
43 -(void)windowWillMiniaturize:(NSNotification *)aNotification
45 GdkWindow *window = [[self contentView] gdkWindow];
47 _gdk_quartz_window_detach_from_parent (window);
50 -(void)windowDidMiniaturize:(NSNotification *)aNotification
52 GdkWindow *window = [[self contentView] gdkWindow];
54 gdk_synthesize_window_state (window, 0,
55 GDK_WINDOW_STATE_ICONIFIED);
58 -(void)windowDidDeminiaturize:(NSNotification *)aNotification
60 GdkWindow *window = [[self contentView] gdkWindow];
62 _gdk_quartz_window_attach_to_parent (window);
64 gdk_synthesize_window_state (window, GDK_WINDOW_STATE_ICONIFIED, 0);
67 -(void)windowDidBecomeKey:(NSNotification *)aNotification
69 GdkWindow *window = [[self contentView] gdkWindow];
71 _gdk_quartz_events_update_focus_window (window, TRUE);
74 -(void)windowDidResignKey:(NSNotification *)aNotification
76 GdkWindow *window = [[self contentView] gdkWindow];
78 _gdk_quartz_events_update_focus_window (window, FALSE);
81 -(void)windowDidBecomeMain:(NSNotification *)aNotification
83 GdkWindow *window = [[self contentView] gdkWindow];
85 if (![self isVisible])
87 /* Note: This is a hack needed because for unknown reasons, hidden
88 * windows get shown when clicking the dock icon when the application
89 * is not already active.
95 _gdk_quartz_window_did_become_main (window);
98 -(void)windowDidResignMain:(NSNotification *)aNotification
102 window = [[self contentView] gdkWindow];
103 _gdk_quartz_window_did_resign_main (window);
106 /* Used in combination with NSLeftMouseUp in sendEvent to keep track
107 * of when the window is being moved with the mouse.
109 -(void)windowWillMove:(NSNotification *)aNotification
114 -(void)sendEvent:(NSEvent *)event
116 switch ([event type])
124 case NSLeftMouseDragged:
125 if ([self trackManualMove] || [self trackManualResize])
133 [super sendEvent:event];
141 -(void)windowDidMove:(NSNotification *)aNotification
143 GdkWindow *window = [[self contentView] gdkWindow];
146 _gdk_quartz_window_update_position (window);
148 /* Synthesize a configure event */
149 event = gdk_event_new (GDK_CONFIGURE);
150 event->configure.window = g_object_ref (window);
151 event->configure.x = window->x;
152 event->configure.y = window->y;
153 event->configure.width = window->width;
154 event->configure.height = window->height;
156 _gdk_event_queue_append (gdk_display_get_default (), event);
159 -(void)windowDidResize:(NSNotification *)aNotification
161 NSRect content_rect = [self contentRectForFrameRect:[self frame]];
162 GdkWindow *window = [[self contentView] gdkWindow];
165 window->width = content_rect.size.width;
166 window->height = content_rect.size.height;
168 /* Certain resize operations (e.g. going fullscreen), also move the
169 * origin of the window.
171 _gdk_quartz_window_update_position (window);
173 [[self contentView] setFrame:NSMakeRect (0, 0, window->width, window->height)];
175 _gdk_window_update_size (window);
177 /* Synthesize a configure event */
178 event = gdk_event_new (GDK_CONFIGURE);
179 event->configure.window = g_object_ref (window);
180 event->configure.x = window->x;
181 event->configure.y = window->y;
182 event->configure.width = window->width;
183 event->configure.height = window->height;
185 _gdk_event_queue_append (gdk_display_get_default (), event);
188 -(id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag screen:(NSScreen *)screen
190 self = [super initWithContentRect:contentRect
196 [self setAcceptsMouseMovedEvents:YES];
197 [self setDelegate:self];
198 [self setReleasedWhenClosed:YES];
203 -(BOOL)canBecomeMainWindow
205 GdkWindow *window = [[self contentView] gdkWindow];
206 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl);
208 switch (impl->type_hint)
210 case GDK_WINDOW_TYPE_HINT_NORMAL:
211 case GDK_WINDOW_TYPE_HINT_DIALOG:
214 case GDK_WINDOW_TYPE_HINT_MENU:
215 case GDK_WINDOW_TYPE_HINT_TOOLBAR:
216 case GDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
217 case GDK_WINDOW_TYPE_HINT_UTILITY:
218 case GDK_WINDOW_TYPE_HINT_DOCK:
219 case GDK_WINDOW_TYPE_HINT_DESKTOP:
220 case GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU:
221 case GDK_WINDOW_TYPE_HINT_POPUP_MENU:
222 case GDK_WINDOW_TYPE_HINT_TOOLTIP:
223 case GDK_WINDOW_TYPE_HINT_NOTIFICATION:
224 case GDK_WINDOW_TYPE_HINT_COMBO:
225 case GDK_WINDOW_TYPE_HINT_DND:
232 -(BOOL)canBecomeKeyWindow
234 GdkWindow *window = [[self contentView] gdkWindow];
235 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl);
237 if (!window->accept_focus)
240 /* Popup windows should not be able to get focused in the window
241 * manager sense, it's only handled through grabs.
243 if (window->window_type == GDK_WINDOW_TEMP)
246 switch (impl->type_hint)
248 case GDK_WINDOW_TYPE_HINT_NORMAL:
249 case GDK_WINDOW_TYPE_HINT_DIALOG:
250 case GDK_WINDOW_TYPE_HINT_MENU:
251 case GDK_WINDOW_TYPE_HINT_TOOLBAR:
252 case GDK_WINDOW_TYPE_HINT_UTILITY:
253 case GDK_WINDOW_TYPE_HINT_DOCK:
254 case GDK_WINDOW_TYPE_HINT_DESKTOP:
255 case GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU:
256 case GDK_WINDOW_TYPE_HINT_POPUP_MENU:
257 case GDK_WINDOW_TYPE_HINT_COMBO:
260 case GDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
261 case GDK_WINDOW_TYPE_HINT_TOOLTIP:
262 case GDK_WINDOW_TYPE_HINT_NOTIFICATION:
263 case GDK_WINDOW_TYPE_HINT_DND:
270 - (void)showAndMakeKey:(BOOL)makeKey
272 GdkWindow *window = [[self contentView] gdkWindow];
273 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl);
278 [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
280 [impl->toplevel orderFront:nil];
287 GdkWindow *window = [[self contentView] gdkWindow];
288 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl);
291 [impl->toplevel orderOut:nil];
295 - (BOOL)trackManualMove
297 NSPoint currentLocation;
299 NSRect screenFrame = [[NSScreen mainScreen] visibleFrame];
300 NSRect windowFrame = [self frame];
305 currentLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];
306 newOrigin.x = currentLocation.x - initialMoveLocation.x;
307 newOrigin.y = currentLocation.y - initialMoveLocation.y;
309 /* Clamp vertical position to below the menu bar. */
310 if (newOrigin.y + windowFrame.size.height > screenFrame.origin.y + screenFrame.size.height)
311 newOrigin.y = screenFrame.origin.y + screenFrame.size.height - windowFrame.size.height;
313 [self setFrameOrigin:newOrigin];
318 -(void)beginManualMove
320 NSRect frame = [self frame];
322 if (inMove || inManualMove || inManualResize)
327 initialMoveLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];
328 initialMoveLocation.x -= frame.origin.x;
329 initialMoveLocation.y -= frame.origin.y;
332 - (BOOL)trackManualResize
334 NSPoint currentLocation;
339 if (!inManualResize || inTrackManualResize)
342 inTrackManualResize = YES;
344 currentLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];
345 currentLocation.x -= initialResizeFrame.origin.x;
346 currentLocation.y -= initialResizeFrame.origin.y;
348 dx = currentLocation.x - initialResizeLocation.x;
349 dy = -(currentLocation.y - initialResizeLocation.y);
351 newFrame = initialResizeFrame;
352 newFrame.size.width = initialResizeFrame.size.width + dx;
353 newFrame.size.height = initialResizeFrame.size.height + dy;
355 min_size = [self contentMinSize];
356 if (newFrame.size.width < min_size.width)
357 newFrame.size.width = min_size.width;
358 if (newFrame.size.height < min_size.height)
359 newFrame.size.height = min_size.height;
361 /* We could also apply aspect ratio:
362 newFrame.size.height = newFrame.size.width / [self aspectRatio].width * [self aspectRatio].height;
365 dy = newFrame.size.height - initialResizeFrame.size.height;
367 newFrame.origin.x = initialResizeFrame.origin.x;
368 newFrame.origin.y = initialResizeFrame.origin.y - dy;
370 [self setFrame:newFrame display:YES];
372 /* Let the resizing be handled by GTK+. */
373 if (g_main_context_pending (NULL))
374 g_main_context_iteration (NULL, FALSE);
376 inTrackManualResize = NO;
381 -(void)beginManualResize
383 if (inMove || inManualMove || inManualResize)
386 inManualResize = YES;
388 initialResizeFrame = [self frame];
389 initialResizeLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];
390 initialResizeLocation.x -= initialResizeFrame.origin.x;
391 initialResizeLocation.y -= initialResizeFrame.origin.y;
396 static GdkDragContext *current_context = NULL;
399 drag_operation_to_drag_action (NSDragOperation operation)
401 GdkDragAction result = 0;
403 /* GDK and Quartz drag operations do not map 1:1.
404 * This mapping represents about the best that we
407 * Note that NSDragOperationPrivate and GDK_ACTION_PRIVATE
408 * have almost opposite meanings: the GDK one means that the
409 * destination is solely responsible for the action; the Quartz
410 * one means that the source and destination will agree
411 * privately on the action. NSOperationGeneric is close in meaning
412 * to GDK_ACTION_PRIVATE but there is a problem: it will be
413 * sent for any ordinary drag, and likely not understood
414 * by any intra-widget drag (since the source & dest are the
418 if (operation & NSDragOperationGeneric)
419 result |= GDK_ACTION_MOVE;
420 if (operation & NSDragOperationCopy)
421 result |= GDK_ACTION_COPY;
422 if (operation & NSDragOperationMove)
423 result |= GDK_ACTION_MOVE;
424 if (operation & NSDragOperationLink)
425 result |= GDK_ACTION_LINK;
430 static NSDragOperation
431 drag_action_to_drag_operation (GdkDragAction action)
433 NSDragOperation result = 0;
435 if (action & GDK_ACTION_COPY)
436 result |= NSDragOperationCopy;
437 if (action & GDK_ACTION_LINK)
438 result |= NSDragOperationLink;
439 if (action & GDK_ACTION_MOVE)
440 result |= NSDragOperationMove;
446 update_context_from_dragging_info (id <NSDraggingInfo> sender)
448 g_assert (current_context != NULL);
450 GDK_QUARTZ_DRAG_CONTEXT (current_context)->dragging_info = sender;
451 current_context->suggested_action = drag_operation_to_drag_action ([sender draggingSourceOperationMask]);
452 current_context->actions = current_context->suggested_action;
455 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
457 GdkDeviceManager *device_manager;
462 g_object_unref (current_context);
464 current_context = g_object_new (GDK_TYPE_QUARTZ_DRAG_CONTEXT, NULL);
465 update_context_from_dragging_info (sender);
467 window = [[self contentView] gdkWindow];
469 device_manager = gdk_display_get_device_manager (gdk_display_get_default ());
470 gdk_drag_context_set_device (current_context,
471 gdk_device_manager_get_client_pointer (device_manager));
473 event = gdk_event_new (GDK_DRAG_ENTER);
474 event->dnd.window = g_object_ref (window);
475 event->dnd.send_event = FALSE;
476 event->dnd.context = g_object_ref (current_context);
477 event->dnd.time = GDK_CURRENT_TIME;
479 gdk_event_set_device (event, gdk_drag_context_get_device (current_context));
481 _gdk_event_emit (event);
483 gdk_event_free (event);
485 return NSDragOperationNone;
488 - (void)draggingEnded:(id <NSDraggingInfo>)sender
490 /* leave a note for the source about what action was taken */
491 if (_gdk_quartz_drag_source_context && current_context)
492 _gdk_quartz_drag_source_context->action = current_context->action;
495 g_object_unref (current_context);
496 current_context = NULL;
499 - (void)draggingExited:(id <NSDraggingInfo>)sender
503 event = gdk_event_new (GDK_DRAG_LEAVE);
504 event->dnd.window = g_object_ref ([[self contentView] gdkWindow]);
505 event->dnd.send_event = FALSE;
506 event->dnd.context = g_object_ref (current_context);
507 event->dnd.time = GDK_CURRENT_TIME;
509 gdk_event_set_device (event, gdk_drag_context_get_device (current_context));
511 _gdk_event_emit (event);
513 gdk_event_free (event);
515 g_object_unref (current_context);
516 current_context = NULL;
519 - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
521 NSPoint point = [sender draggingLocation];
522 NSPoint screen_point = [self convertBaseToScreen:point];
526 update_context_from_dragging_info (sender);
527 _gdk_quartz_window_nspoint_to_gdk_xy (screen_point, &gx, &gy);
529 event = gdk_event_new (GDK_DRAG_MOTION);
530 event->dnd.window = g_object_ref ([[self contentView] gdkWindow]);
531 event->dnd.send_event = FALSE;
532 event->dnd.context = g_object_ref (current_context);
533 event->dnd.time = GDK_CURRENT_TIME;
534 event->dnd.x_root = gx;
535 event->dnd.y_root = gy;
537 gdk_event_set_device (event, gdk_drag_context_get_device (current_context));
539 _gdk_event_emit (event);
541 gdk_event_free (event);
543 return drag_action_to_drag_operation (current_context->action);
546 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
548 NSPoint point = [sender draggingLocation];
549 NSPoint screen_point = [self convertBaseToScreen:point];
553 update_context_from_dragging_info (sender);
554 _gdk_quartz_window_nspoint_to_gdk_xy (screen_point, &gx, &gy);
556 event = gdk_event_new (GDK_DROP_START);
557 event->dnd.window = g_object_ref ([[self contentView] gdkWindow]);
558 event->dnd.send_event = FALSE;
559 event->dnd.context = g_object_ref (current_context);
560 event->dnd.time = GDK_CURRENT_TIME;
561 event->dnd.x_root = gx;
562 event->dnd.y_root = gy;
564 gdk_event_set_device (event, gdk_drag_context_get_device (current_context));
566 _gdk_event_emit (event);
568 gdk_event_free (event);
570 g_object_unref (current_context);
571 current_context = NULL;
576 - (BOOL)wantsPeriodicDraggingUpdates
581 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
585 g_assert (_gdk_quartz_drag_source_context != NULL);
587 event = gdk_event_new (GDK_DROP_FINISHED);
588 event->dnd.window = g_object_ref ([[self contentView] gdkWindow]);
589 event->dnd.send_event = FALSE;
590 event->dnd.context = g_object_ref (_gdk_quartz_drag_source_context);
592 gdk_event_set_device (event,
593 gdk_drag_context_get_device (_gdk_quartz_drag_source_context));
595 _gdk_event_emit (event);
597 gdk_event_free (event);
599 g_object_unref (_gdk_quartz_drag_source_context);
600 _gdk_quartz_drag_source_context = NULL;