Debian Codegraph @

gdk/quartz/GdkQuartzWindow.c

raw
1 /* GdkQuartzWindow.m
2 *
3 * Copyright (C) 2005-2007 Imendio AB
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #import "GdkQuartzWindow.h"
22 #include "gdkwindow-quartz.h"
23 #include "gdkprivate-quartz.h"
24
25 @implementation GdkQuartzWindow
26
27 - (void)windowWillClose:(NSNotification*)notification
28 {
29 // Clears the delegate when window is going to be closed; since EL
30 // Capitan it is possible that the methods of delegate would get
31 // called after the window has been closed.
32 [self setDelegate:nil];
33 }
34
35 -(BOOL)windowShouldClose:(id)sender
36 {
37 GdkWindow *window = [[self contentView] gdkWindow];
38 GdkEvent *event;
39
40 event = gdk_event_new (GDK_DELETE);
41
42 event->any.window = g_object_ref (window);
43 event->any.send_event = FALSE;
44
45 _gdk_event_queue_append (gdk_display_get_default (), event);
46
47 return NO;
48 }
49
50 -(void)windowWillMiniaturize:(NSNotification *)aNotification
51 {
52 GdkWindow *window = [[self contentView] gdkWindow];
53
54 _gdk_quartz_window_detach_from_parent (window);
55 }
56
57 -(void)windowDidMiniaturize:(NSNotification *)aNotification
58 {
59 GdkWindow *window = [[self contentView] gdkWindow];
60
61 gdk_synthesize_window_state (window, 0,
62 GDK_WINDOW_STATE_ICONIFIED);
63 }
64
65 -(void)windowDidDeminiaturize:(NSNotification *)aNotification
66 {
67 GdkWindow *window = [[self contentView] gdkWindow];
68
69 _gdk_quartz_window_attach_to_parent (window);
70
71 gdk_synthesize_window_state (window, GDK_WINDOW_STATE_ICONIFIED, 0);
72 }
73
74 -(void)windowDidBecomeKey:(NSNotification *)aNotification
75 {
76 GdkWindow *window = [[self contentView] gdkWindow];
77
78 _gdk_quartz_events_update_focus_window (window, TRUE);
79 }
80
81 -(void)windowDidResignKey:(NSNotification *)aNotification
82 {
83 GdkWindow *window = [[self contentView] gdkWindow];
84
85 _gdk_quartz_events_update_focus_window (window, FALSE);
86 }
87
88 -(void)windowDidBecomeMain:(NSNotification *)aNotification
89 {
90 GdkWindow *window = [[self contentView] gdkWindow];
91
92 if (![self isVisible])
93 {
94 /* Note: This is a hack needed because for unknown reasons, hidden
95 * windows get shown when clicking the dock icon when the application
96 * is not already active.
97 */
98 [self orderOut:nil];
99 return;
100 }
101
102 _gdk_quartz_window_did_become_main (window);
103 }
104
105 -(void)windowDidResignMain:(NSNotification *)aNotification
106 {
107 GdkWindow *window;
108
109 window = [[self contentView] gdkWindow];
110 _gdk_quartz_window_did_resign_main (window);
111 }
112
113 /* Used in combination with NSLeftMouseUp in sendEvent to keep track
114 * of when the window is being moved with the mouse.
115 */
116 -(void)windowWillMove:(NSNotification *)aNotification
117 {
118 inMove = YES;
119 }
120
121 -(void)sendEvent:(NSEvent *)event
122 {
123 switch ([event type])
124 {
125 case NSLeftMouseUp:
126 {
127 double time = ((double)[event timestamp]) * 1000.0;
128
129 _gdk_quartz_events_break_all_grabs (time);
130 inManualMove = NO;
131 inManualResize = NO;
132 inMove = NO;
133 break;
134 }
135
136 case NSLeftMouseDragged:
137 if ([self trackManualMove] || [self trackManualResize])
138 return;
139 break;
140
141 default:
142 break;
143 }
144
145 [super sendEvent:event];
146 }
147
148 -(BOOL)isInMove
149 {
150 return inMove;
151 }
152
153 -(void)checkSendEnterNotify
154 {
155 GdkWindow *window = [[self contentView] gdkWindow];
156 GdkWindowObject *private = (GdkWindowObject *)window;
157 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);
158
159 /* When a new window has been created, and the mouse
160 * is in the window area, we will not receive an NSMouseEntered
161 * event. Therefore, we synthesize an enter notify event manually.
162 */
163 if (!initialPositionKnown)
164 {
165 initialPositionKnown = YES;
166
167 if (NSPointInRect ([NSEvent mouseLocation], [self frame]))
168 {
169 NSEvent *event;
170
171 event = [NSEvent enterExitEventWithType: NSMouseEntered
172 location: [self mouseLocationOutsideOfEventStream]
173 modifierFlags: 0
174 timestamp: [[NSApp currentEvent] timestamp]
175 windowNumber: [impl->toplevel windowNumber]
176 context: NULL
177 eventNumber: 0
178 trackingNumber: [impl->view trackingRect]
179 userData: nil];
180
181 [NSApp postEvent:event atStart:NO];
182 }
183 }
184 }
185
186 /* Always update both the position and size. Certain resize operations
187 * (e.g. going fullscreen) also move the origin of the window. Move
188 * notifications sometimes also indicate a different window size (for
189 * example if the window size requested in the configure request was not
190 * fully granted).
191 */
192 -(void)handleDidMoveResize
193 {
194 NSRect content_rect = [self contentRectForFrameRect:[self frame]];
195 GdkWindow *window = [[self contentView] gdkWindow];
196 GdkWindowObject *private = (GdkWindowObject *)window;
197 GdkEvent *event;
198
199 private->width = content_rect.size.width;
200 private->height = content_rect.size.height;
201
202 _gdk_quartz_window_update_position (window);
203
204 [[self contentView] setFrame:NSMakeRect (0, 0, private->width, private->height)];
205
206 _gdk_window_update_size (window);
207
208 /* Synthesize a configure event */
209 event = gdk_event_new (GDK_CONFIGURE);
210 event->configure.window = g_object_ref (window);
211 event->configure.x = private->x;
212 event->configure.y = private->y;
213 event->configure.width = private->width;
214 event->configure.height = private->height;
215
216 _gdk_event_queue_append (gdk_display_get_default (), event);
217
218 [self checkSendEnterNotify];
219 }
220
221 -(void)windowDidMove:(NSNotification *)aNotification
222 {
223 [self handleDidMoveResize];
224 }
225
226 -(void)windowDidResize:(NSNotification *)aNotification
227 {
228 [self handleDidMoveResize];
229 }
230
231 -(id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag screen:(NSScreen *)screen
232 {
233 self = [super initWithContentRect:contentRect
234 styleMask:styleMask
235 backing:backingType
236 defer:flag
237 screen:screen];
238
239 [self setAcceptsMouseMovedEvents:YES];
240 [self setDelegate:self];
241 [self setReleasedWhenClosed:YES];
242
243 return self;
244 }
245
246 -(BOOL)canBecomeMainWindow
247 {
248 GdkWindow *window = [[self contentView] gdkWindow];
249 GdkWindowObject *private = (GdkWindowObject *)window;
250 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);
251
252 switch (impl->type_hint)
253 {
254 case GDK_WINDOW_TYPE_HINT_NORMAL:
255 case GDK_WINDOW_TYPE_HINT_DIALOG:
256 return YES;
257
258 case GDK_WINDOW_TYPE_HINT_MENU:
259 case GDK_WINDOW_TYPE_HINT_TOOLBAR:
260 case GDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
261 case GDK_WINDOW_TYPE_HINT_UTILITY:
262 case GDK_WINDOW_TYPE_HINT_DOCK:
263 case GDK_WINDOW_TYPE_HINT_DESKTOP:
264 case GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU:
265 case GDK_WINDOW_TYPE_HINT_POPUP_MENU:
266 case GDK_WINDOW_TYPE_HINT_TOOLTIP:
267 case GDK_WINDOW_TYPE_HINT_NOTIFICATION:
268 case GDK_WINDOW_TYPE_HINT_COMBO:
269 case GDK_WINDOW_TYPE_HINT_DND:
270 return NO;
271 }
272
273 return YES;
274 }
275
276 -(BOOL)canBecomeKeyWindow
277 {
278 GdkWindow *window = [[self contentView] gdkWindow];
279 GdkWindowObject *private = (GdkWindowObject *)window;
280 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);
281
282 if (!private->accept_focus)
283 return NO;
284
285 /* Popup windows should not be able to get focused in the window
286 * manager sense, it's only handled through grabs.
287 */
288 if (private->window_type == GDK_WINDOW_TEMP)
289 return NO;
290
291 switch (impl->type_hint)
292 {
293 case GDK_WINDOW_TYPE_HINT_NORMAL:
294 case GDK_WINDOW_TYPE_HINT_DIALOG:
295 case GDK_WINDOW_TYPE_HINT_MENU:
296 case GDK_WINDOW_TYPE_HINT_TOOLBAR:
297 case GDK_WINDOW_TYPE_HINT_UTILITY:
298 case GDK_WINDOW_TYPE_HINT_DOCK:
299 case GDK_WINDOW_TYPE_HINT_DESKTOP:
300 case GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU:
301 case GDK_WINDOW_TYPE_HINT_POPUP_MENU:
302 case GDK_WINDOW_TYPE_HINT_COMBO:
303 return YES;
304
305 case GDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
306 case GDK_WINDOW_TYPE_HINT_TOOLTIP:
307 case GDK_WINDOW_TYPE_HINT_NOTIFICATION:
308 case GDK_WINDOW_TYPE_HINT_DND:
309 return NO;
310 }
311
312 return YES;
313 }
314
315 - (void)showAndMakeKey:(BOOL)makeKey
316 {
317 GdkWindow *window = [[self contentView] gdkWindow];
318 GdkWindowObject *private = (GdkWindowObject *)window;
319 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);
320
321 inShowOrHide = YES;
322
323 if (makeKey)
324 [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
325 else
326 [impl->toplevel orderFront:nil];
327
328 inShowOrHide = NO;
329
330 [self checkSendEnterNotify];
331 }
332
333 - (void)hide
334 {
335 GdkWindow *window = [[self contentView] gdkWindow];
336 GdkWindowObject *private = (GdkWindowObject *)window;
337 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);
338
339 inShowOrHide = YES;
340 [impl->toplevel orderOut:nil];
341 inShowOrHide = NO;
342
343 initialPositionKnown = NO;
344 }
345
346 - (BOOL)trackManualMove
347 {
348 NSPoint currentLocation;
349 NSPoint newOrigin;
350 NSRect screenFrame = [[NSScreen mainScreen] visibleFrame];
351 NSRect windowFrame = [self frame];
352
353 if (!inManualMove)
354 return NO;
355
356 currentLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];
357 newOrigin.x = currentLocation.x - initialMoveLocation.x;
358 newOrigin.y = currentLocation.y - initialMoveLocation.y;
359
360 /* Clamp vertical position to below the menu bar. */
361 if (newOrigin.y + windowFrame.size.height > screenFrame.origin.y + screenFrame.size.height)
362 newOrigin.y = screenFrame.origin.y + screenFrame.size.height - windowFrame.size.height;
363
364 [self setFrameOrigin:newOrigin];
365
366 return YES;
367 }
368
369 -(void)beginManualMove
370 {
371 NSRect frame = [self frame];
372
373 if (inMove || inManualMove || inManualResize)
374 return;
375
376 inManualMove = YES;
377
378 initialMoveLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];
379 initialMoveLocation.x -= frame.origin.x;
380 initialMoveLocation.y -= frame.origin.y;
381 }
382
383 - (BOOL)trackManualResize
384 {
385 NSPoint currentLocation;
386 NSRect newFrame;
387 float dx, dy;
388 NSSize min_size;
389
390 if (!inManualResize || inTrackManualResize)
391 return NO;
392
393 inTrackManualResize = YES;
394
395 currentLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];
396 currentLocation.x -= initialResizeFrame.origin.x;
397 currentLocation.y -= initialResizeFrame.origin.y;
398
399 dx = currentLocation.x - initialResizeLocation.x;
400 dy = -(currentLocation.y - initialResizeLocation.y);
401
402 newFrame = initialResizeFrame;
403 newFrame.size.width = initialResizeFrame.size.width + dx;
404 newFrame.size.height = initialResizeFrame.size.height + dy;
405
406 min_size = [self contentMinSize];
407 if (newFrame.size.width < min_size.width)
408 newFrame.size.width = min_size.width;
409 if (newFrame.size.height < min_size.height)
410 newFrame.size.height = min_size.height;
411
412 /* We could also apply aspect ratio:
413 newFrame.size.height = newFrame.size.width / [self aspectRatio].width * [self aspectRatio].height;
414 */
415
416 dy = newFrame.size.height - initialResizeFrame.size.height;
417
418 newFrame.origin.x = initialResizeFrame.origin.x;
419 newFrame.origin.y = initialResizeFrame.origin.y - dy;
420
421 [self setFrame:newFrame display:YES];
422
423 /* Let the resizing be handled by GTK+. */
424 if (g_main_context_pending (NULL))
425 g_main_context_iteration (NULL, FALSE);
426
427 inTrackManualResize = NO;
428
429 return YES;
430 }
431
432 -(BOOL)isInManualResize
433 {
434 return inManualResize;
435 }
436
437 -(void)beginManualResize
438 {
439 if (inMove || inManualMove || inManualResize)
440 return;
441
442 inManualResize = YES;
443
444 initialResizeFrame = [self frame];
445 initialResizeLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];
446 initialResizeLocation.x -= initialResizeFrame.origin.x;
447 initialResizeLocation.y -= initialResizeFrame.origin.y;
448 }
449
450
451
452 static GdkDragContext *current_context = NULL;
453
454 static GdkDragAction
455 drag_operation_to_drag_action (NSDragOperation operation)
456 {
457 GdkDragAction result = 0;
458
459 /* GDK and Quartz drag operations do not map 1:1.
460 * This mapping represents about the best that we
461 * can come up.
462 *
463 * Note that NSDragOperationPrivate and GDK_ACTION_PRIVATE
464 * have almost opposite meanings: the GDK one means that the
465 * destination is solely responsible for the action; the Quartz
466 * one means that the source and destination will agree
467 * privately on the action. NSOperationGeneric is close in meaning
468 * to GDK_ACTION_PRIVATE but there is a problem: it will be
469 * sent for any ordinary drag, and likely not understood
470 * by any intra-widget drag (since the source & dest are the
471 * same).
472 */
473
474 if (operation & NSDragOperationGeneric)
475 result |= GDK_ACTION_MOVE;
476 if (operation & NSDragOperationCopy)
477 result |= GDK_ACTION_COPY;
478 if (operation & NSDragOperationMove)
479 result |= GDK_ACTION_MOVE;
480 if (operation & NSDragOperationLink)
481 result |= GDK_ACTION_LINK;
482
483 return result;
484 }
485
486 static NSDragOperation
487 drag_action_to_drag_operation (GdkDragAction action)
488 {
489 NSDragOperation result = 0;
490
491 if (action & GDK_ACTION_COPY)
492 result |= NSDragOperationCopy;
493 if (action & GDK_ACTION_LINK)
494 result |= NSDragOperationLink;
495 if (action & GDK_ACTION_MOVE)
496 result |= NSDragOperationMove;
497
498 return result;
499 }
500
501 static void
502 update_context_from_dragging_info (id <NSDraggingInfo> sender)
503 {
504 g_assert (current_context != NULL);
505
506 GDK_DRAG_CONTEXT_PRIVATE (current_context)->dragging_info = sender;
507 current_context->suggested_action = drag_operation_to_drag_action ([sender draggingSourceOperationMask]);
508 current_context->actions = current_context->suggested_action;
509 }
510
511 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
512 {
513 GdkEvent event;
514
515 if (current_context)
516 g_object_unref (current_context);
517
518 current_context = gdk_drag_context_new ();
519 update_context_from_dragging_info (sender);
520
521 event.dnd.type = GDK_DRAG_ENTER;
522 event.dnd.window = g_object_ref ([[self contentView] gdkWindow]);
523 event.dnd.send_event = FALSE;
524 event.dnd.context = current_context;
525 event.dnd.time = GDK_CURRENT_TIME;
526
527 (*_gdk_event_func) (&event, _gdk_event_data);
528
529 return NSDragOperationNone;
530 }
531
532 - (void)draggingEnded:(id <NSDraggingInfo>)sender
533 {
534 /* leave a note for the source about what action was taken */
535 if (_gdk_quartz_drag_source_context && current_context)
536 _gdk_quartz_drag_source_context->action = current_context->action;
537
538 if (current_context)
539 g_object_unref (current_context);
540 current_context = NULL;
541 }
542
543 - (void)draggingExited:(id <NSDraggingInfo>)sender
544 {
545 GdkEvent event;
546
547 event.dnd.type = GDK_DRAG_LEAVE;
548 event.dnd.window = g_object_ref ([[self contentView] gdkWindow]);
549 event.dnd.send_event = FALSE;
550 event.dnd.context = current_context;
551 event.dnd.time = GDK_CURRENT_TIME;
552
553 (*_gdk_event_func) (&event, _gdk_event_data);
554
555 g_object_unref (current_context);
556 current_context = NULL;
557 }
558
559 - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
560 {
561 NSPoint point = [sender draggingLocation];
562 NSPoint screen_point = [self convertBaseToScreen:point];
563 GdkEvent event;
564 int gx, gy;
565
566 update_context_from_dragging_info (sender);
567 _gdk_quartz_window_nspoint_to_gdk_xy (screen_point, &gx, &gy);
568
569 event.dnd.type = GDK_DRAG_MOTION;
570 event.dnd.window = g_object_ref ([[self contentView] gdkWindow]);
571 event.dnd.send_event = FALSE;
572 event.dnd.context = current_context;
573 event.dnd.time = GDK_CURRENT_TIME;
574 event.dnd.x_root = gx;
575 event.dnd.y_root = gy;
576
577 (*_gdk_event_func) (&event, _gdk_event_data);
578
579 g_object_unref (event.dnd.window);
580
581 return drag_action_to_drag_operation (current_context->action);
582 }
583
584 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
585 {
586 NSPoint point = [sender draggingLocation];
587 NSPoint screen_point = [self convertBaseToScreen:point];
588 GdkEvent event;
589 int gy, gx;
590
591 update_context_from_dragging_info (sender);
592 _gdk_quartz_window_nspoint_to_gdk_xy (screen_point, &gx, &gy);
593
594 event.dnd.type = GDK_DROP_START;
595 event.dnd.window = g_object_ref ([[self contentView] gdkWindow]);
596 event.dnd.send_event = FALSE;
597 event.dnd.context = current_context;
598 event.dnd.time = GDK_CURRENT_TIME;
599 event.dnd.x_root = gx;
600 event.dnd.y_root = gy;
601
602 (*_gdk_event_func) (&event, _gdk_event_data);
603
604 g_object_unref (event.dnd.window);
605
606 g_object_unref (current_context);
607 current_context = NULL;
608
609 return YES;
610 }
611
612 - (BOOL)wantsPeriodicDraggingUpdates
613 {
614 return NO;
615 }
616
617 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
618 {
619 GdkEvent event;
620 GdkScreen *screen;
621
622 g_assert (_gdk_quartz_drag_source_context != NULL);
623
624 event.dnd.type = GDK_DROP_FINISHED;
625 event.dnd.window = g_object_ref ([[self contentView] gdkWindow]);
626 event.dnd.send_event = FALSE;
627 event.dnd.context = _gdk_quartz_drag_source_context;
628
629 screen = gdk_window_get_screen (event.dnd.window);
630
631 if (screen)
632 {
633 GList* windows, *list;
634 gint gx, gy;
635
636 event.dnd.context->dest_window = NULL;
637
638 windows = gdk_screen_get_toplevel_windows (screen);
639 _gdk_quartz_window_nspoint_to_gdk_xy (aPoint, &gx, &gy);
640
641 for (list = windows; list; list = list->next)
642 {
643 GdkWindow* win = (GdkWindow*) list->data;
644 gint wx, wy;
645 gint ww, wh;
646
647 gdk_window_get_root_origin (win, &wx, &wy);
648 ww = gdk_window_get_width (win);
649 wh = gdk_window_get_height (win);
650
651 if (gx > wx && gy > wy && gx <= wx + ww && gy <= wy + wh)
652 event.dnd.context->dest_window = win;
653 }
654 }
655
656 (*_gdk_event_func) (&event, _gdk_event_data);
657
658 g_object_unref (event.dnd.window);
659
660 g_object_unref (_gdk_quartz_drag_source_context);
661 _gdk_quartz_drag_source_context = NULL;
662 }
663
664 @end