Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
QuartzWindow.mm
Go to the documentation of this file.
1// @(#)root/graf2d:$Id$
2// Author: Timur Pocheptsov 16/02/2012
3
4/*************************************************************************
5 * Copyright (C) 1995-2012, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12//#define DEBUG_ROOT_COCOA
13
14//#define NDEBUG
15
16#ifdef DEBUG_ROOT_COCOA
17#include <iostream>
18#include <fstream>
19
20#include "TClass.h"
21#endif
22
23#include <algorithm>
24#include <stdexcept>
25#include <cassert>
26#include <vector>
27
28#include <Availability.h>
29
30#include "ROOTOpenGLView.h"
31#include "CocoaConstants.h"
32#include "QuartzWindow.h"
33#include "QuartzPixmap.h"
34#include "QuartzUtils.h"
35#include "CocoaUtils.h"
36#include "RConfigure.h"
37#include "X11Colors.h"
38#include "X11Buffer.h"
39#include "TGWindow.h"
40#include "TGClient.h"
41#include "TSystem.h"
42#include "TGCocoa.h"
43#include "TROOT.h"
44#include "TGTextView.h"
45#include "TGView.h"
46#include "TGCanvas.h"
47
48
49namespace ROOT {
50namespace MacOSX {
51namespace X11 {
52
53#pragma mark - Create a window or a view.
54
55//______________________________________________________________________________
57 UInt_t clss, void */*visual*/, SetWindowAttributes_t *attr, UInt_t)
58{
59 using namespace Details;
60
61 NSRect winRect = {};
62 winRect.origin.x = GlobalXROOTToCocoa(x);
63 winRect.origin.y = GlobalYROOTToCocoa(y + h);
64 winRect.size.width = w;
65 winRect.size.height = h;
66
67 const NSUInteger styleMask = kTitledWindowMask | kClosableWindowMask |
68 kMiniaturizableWindowMask | kResizableWindowMask;
69
70 QuartzWindow * const newWindow = [[QuartzWindow alloc] initWithContentRect : winRect
71 styleMask : styleMask
72 backing : NSBackingStoreBuffered
73 defer : YES
74 windowAttributes : attr];
75 if (!newWindow)
76 throw std::runtime_error("CreateTopLevelWindow failed");
77
78 newWindow.fDepth = depth;
79 newWindow.fClass = clss;
80
81 return newWindow;
82}
83
84//______________________________________________________________________________
85QuartzView *CreateChildView(QuartzView * /*parent*/, Int_t x, Int_t y, UInt_t w, UInt_t h, UInt_t /*border*/, Int_t /*depth*/,
86 UInt_t /*clss*/, void * /*visual*/, SetWindowAttributes_t *attr, UInt_t /*wtype*/)
87{
88 NSRect viewRect = {};
89 viewRect.origin.x = x;
90 viewRect.origin.y = y;
91 viewRect.size.width = w;
92 viewRect.size.height = h;
93
94 QuartzView * const view = [[QuartzView alloc] initWithFrame : viewRect windowAttributes : attr];
95 if (!view)
96 throw std::runtime_error("CreateChildView failed");
97
98 return view;
99}
100
101#pragma mark - root window (does not really exist, it's our desktop built of all screens).
102
103//______________________________________________________________________________
105{
106 //'root' window does not exist, but we can request its attributes.
107 assert(attr != 0 && "GetRootWindowAttributes, parameter 'attr' is null");
108
109
110 NSArray * const screens = [NSScreen screens];
111 assert(screens != nil && "screens array is nil");
112 NSScreen * const mainScreen = [screens objectAtIndex : 0];
113 assert(mainScreen != nil && "screen with index 0 is nil");
114
116
117 assert(dynamic_cast<TGCocoa *>(gVirtualX) &&
118 "GetRootWindowAttributes, gVirtualX is either null or has a wrong type");
119
120 TGCocoa * const gCocoa = static_cast<TGCocoa *>(gVirtualX);
121
122 const Rectangle &frame = gCocoa->GetDisplayGeometry();
123
124 attr->fX = 0;
125 attr->fY = 0;
126 attr->fWidth = frame.fWidth;
127 attr->fHeight = frame.fHeight;
128 attr->fBorderWidth = 0;
129 attr->fYourEventMask = 0;
130 attr->fAllEventMasks = 0;//???
131
132 attr->fDepth = NSBitsPerPixelFromDepth([mainScreen depth]);
133 attr->fVisual = 0;
134 attr->fRoot = 0;
135}
136
137
138#pragma mark - Coordinate conversions.
139
140//______________________________________________________________________________
141NSPoint ConvertPointFromBaseToScreen(NSWindow *window, NSPoint windowPoint)
142{
143 assert(window != nil && "ConvertPointFromBaseToScreen, parameter 'window' is nil");
144
145 //I have no idea why apple deprecated function for a point conversion and requires rect conversion,
146 //point conversion seems to produce wrong results with HiDPI.
147
148 NSRect tmpRect = {};
149 tmpRect.origin = windowPoint;
150 tmpRect.size = NSMakeSize(1., 1.);//This is strange size :) But if they require rect, 0,0 - will not work?
151 tmpRect = [window convertRectToScreen : tmpRect];
152
153 return tmpRect.origin;
154}
155
156//______________________________________________________________________________
157NSPoint ConvertPointFromScreenToBase(NSPoint screenPoint, NSWindow *window)
158{
159 assert(window != nil && "ConvertPointFromScreenToBase, parameter 'window' is nil");
160
161 //I have no idea why apple deprecated function for a point conversion and requires rect conversion,
162 //point conversion seems to produce wrong results with HiDPI.
163
164 NSRect tmpRect = {};
165 tmpRect.origin = screenPoint;
166 tmpRect.size = NSMakeSize(1., 1.);
167 tmpRect = [window convertRectFromScreen : tmpRect];
168
169 return tmpRect.origin;
170}
171
172//______________________________________________________________________________
173int GlobalYCocoaToROOT(CGFloat yCocoa)
174{
175 //We can have several physical displays and thus several NSScreens in some arbitrary order.
176 //With Cocoa, some screens can have negative coordinates - to the left ro down to the primary
177 //screen (whatever it means). With X11 (XQuartz) though it's always 0,0.
178
179 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
180 "GlobalYCocoaToROOT, gVirtualX is either nul or has a wrong type");
181
182 const Rectangle frame = ((TGCocoa *)gVirtualX)->GetDisplayGeometry();
183
184 return int(frame.fHeight - (yCocoa - frame.fY));
185}
186
187//______________________________________________________________________________
188int GlobalXCocoaToROOT(CGFloat xCocoa)
189{
190 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
191 "GlobalXCocoaToROOT, gVirtualX is either nul or has a wrong type");
192 const Rectangle frame = ((TGCocoa *)gVirtualX)->GetDisplayGeometry();
193 //With X11 coordinate space always starts from 0, 0
194 return int(xCocoa - frame.fX);
195}
196
197//______________________________________________________________________________
198int GlobalYROOTToCocoa(CGFloat yROOT)
199{
200 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
201 "GlobalYROOTToCocoa, gVirtualX is either nul or has a wrong type");
202 const Rectangle frame = ((TGCocoa *)gVirtualX)->GetDisplayGeometry();
203
204 return int(frame.fY + (frame.fHeight - yROOT));
205}
206
207//______________________________________________________________________________
208int GlobalXROOTToCocoa(CGFloat xROOT)
209{
210 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
211 "GlobalXROOTToCocoa, gVirtualX is either nul or has a wrong type");
212 const Rectangle frame = ((TGCocoa *)gVirtualX)->GetDisplayGeometry();
213 //With X11 coordinate space always starts from 0, 0
214 return int(frame.fX + xROOT);
215}
216
217//______________________________________________________________________________
218int LocalYCocoaToROOT(NSView<X11Window> *parentView, CGFloat yCocoa)
219{
220 assert(parentView != nil && "LocalYCocoaToROOT, parent view is nil");
221
222 return int(parentView.frame.size.height - yCocoa);
223}
224
225//______________________________________________________________________________
226int LocalYROOTToCocoa(NSView<X11Window> *parentView, CGFloat yROOT)
227{
228 //:)
229 assert(parentView != nil && "LocalYROOTToCocoa, parent view is nil");
230
231 return int(parentView.frame.size.height - yROOT);
232}
233
234
235//______________________________________________________________________________
236int LocalYROOTToCocoa(NSObject<X11Drawable> *drawable, CGFloat yROOT)
237{
238 //:)
239 assert(drawable != nil && "LocalYROOTToCocoa, drawable is nil");
240
241 return int(drawable.fHeight - yROOT);
242}
243
244//______________________________________________________________________________
245NSPoint TranslateToScreen(NSView<X11Window> *from, NSPoint point)
246{
247 assert(from != nil && "TranslateToScreen, parameter 'from' is nil");
248
249 const NSPoint winPoint = [from convertPoint : point toView : nil];
250
251 NSPoint screenPoint = ConvertPointFromBaseToScreen([from window], winPoint);
252 screenPoint.x = GlobalXCocoaToROOT(screenPoint.x);
253 screenPoint.y = GlobalYCocoaToROOT(screenPoint.y);
254
255 return screenPoint;
256}
257
258//______________________________________________________________________________
259NSPoint TranslateFromScreen(NSPoint point, NSView<X11Window> *to)
260{
261 assert(to != nil && "TranslateFromScreen, parameter 'to' is nil");
262
263 point.x = GlobalXROOTToCocoa(point.x);
264 point.y = GlobalYROOTToCocoa(point.y);
265 point = ConvertPointFromScreenToBase(point, [to window]);
266
267 return [to convertPoint : point fromView : nil];
268}
269
270//______________________________________________________________________________
271NSPoint TranslateCoordinates(NSView<X11Window> *from, NSView<X11Window> *to, NSPoint sourcePoint)
272{
273 //Both views are valid.
274 assert(from != nil && "TranslateCoordinates, parameter 'from' is nil");
275 assert(to != nil && "TranslateCoordinates, parameter 'to' is nil");
276
277 if ([from window] == [to window]) {
278 //Both views are in the same window.
279 return [to convertPoint : sourcePoint fromView : from];
280 } else {
281 //May be, I can do it in one call, but it's not obvious for me
282 //what is 'pixel aligned backing store coordinates' and
283 //if they are the same as screen coordinates.
284
285 //Many thanks to Apple for deprecated functions!!!
286
287 const NSPoint win1Point = [from convertPoint : sourcePoint toView : nil];
288 const NSPoint screenPoint = ConvertPointFromBaseToScreen([from window], win1Point);
289 const NSPoint win2Point = ConvertPointFromScreenToBase(screenPoint, [to window]);
290
291 return [to convertPoint : win2Point fromView : nil];
292 }
293}
294
295//______________________________________________________________________________
297{
298 assert(view != nil && "ScreenPointIsInView, parameter 'view' is nil");
299
300 NSPoint point = {};
301 point.x = x, point.y = y;
302 point = TranslateFromScreen(point, view);
303 const NSRect viewFrame = view.frame;
304
305 if (point.x < 0 || point.x > viewFrame.size.width)
306 return false;
307 if (point.y < 0 || point.y > viewFrame.size.height)
308 return false;
309
310 return true;
311}
312
313#pragma mark - Different FindView/Window functions iterating on the ROOT's windows/views.
314
315//______________________________________________________________________________
317{
318 //array's counter is increased, all object in array are also retained.
319 const Util::AutoreleasePool pool;
320
321 NSArray * const orderedWindows = [NSApp orderedWindows];
322 for (NSWindow *window in orderedWindows) {
323 if (![window isKindOfClass : [QuartzWindow class]])
324 continue;
325 QuartzWindow * const qw = (QuartzWindow *)window;
326 if (qw.fIsDeleted)//Because of reference counting this can happen.
327 continue;
328 //Check if point is inside.
330 return qw;
331 }
332
333 return nil;
334}
335
336//______________________________________________________________________________
338 Window_t inputWinID, Int_t x, Int_t y, Int_t maxDepth)
339{
340 assert(children != nil && "FindDNDAwareViewInPoint, parameter 'children' is nil");
341
342 if (maxDepth <= 0)
343 return nil;
344
345 NSEnumerator * const reverseEnumerator = [children reverseObjectEnumerator];
346 for (NSView<X11Window> *child in reverseEnumerator) {
348 continue;
349 if (child.fIsDNDAware && child.fID != dragWinID && child.fID != inputWinID)
350 return child;//got it!
351
352 NSView<X11Window> * const testView = FindDNDAwareViewInPoint([child subviews], dragWinID,
353 inputWinID, x, y, maxDepth - 1);
354 if (testView)
355 return testView;
356 }
357
358 return nil;
359}
360
361//______________________________________________________________________________
363 Int_t x, Int_t y, Int_t maxDepth)
364{
365 //X and Y are ROOT's screen coordinates (Y is inverted).
366 if (maxDepth <= 0)
367 return nil;
368
369 const Util::AutoreleasePool pool;
370
371 if (!parentView) {//Start from the screen as a 'root' window.
372 NSArray * const orderedWindows = [NSApp orderedWindows];
373 for (NSWindow *window in orderedWindows) {
374 if (![window isKindOfClass : [QuartzWindow class]])
375 continue;
376 QuartzWindow * const qw = (QuartzWindow *)window;
377
378 if (qw.fIsDeleted)//Because of reference counting this can happen.
379 continue;
380
381 if (qw.fMapState != kIsViewable)
382 continue;
383
384 //First, check this view itself, my be we found what we need already.
385 NSView<X11Window> *testView = qw.fContentView;
386 if (!ScreenPointIsInView(testView, x, y))
387 continue;
388
389 if (testView.fIsDNDAware && testView.fID != dragWinID && testView.fID != inputWinID)
390 return testView;
391
392 //Recursive part, check children.
393 NSArray * const children = [testView subviews];
394 testView = FindDNDAwareViewInPoint(children, dragWinID, inputWinID, x, y, maxDepth - 1);
395 if (testView)
396 return testView;
397 }
398
399 //We did not find anything for 'root' window as parent.
400 return nil;
401 } else {
402 //Parent view is tested already (or should not be tested at all, check children.
403 return FindDNDAwareViewInPoint([parentView subviews], dragWinID, inputWinID, x, y, maxDepth);
404 }
405}
406
407//______________________________________________________________________________
409{
410 const Util::AutoreleasePool pool;
411
412 NSArray * const orderedWindows = [NSApp orderedWindows];
413 for (NSWindow *nsWindow in orderedWindows) {
414 if (![nsWindow isKindOfClass : [QuartzWindow class]])
415 continue;
416
417 QuartzWindow * const qWindow = (QuartzWindow *)nsWindow;
418
419 if (qWindow.fIsDeleted)//Because of reference counting this can happen.
420 continue;
421
422 if (qWindow.fMapState != kIsViewable)//Can it be false and still in this array???
423 continue;
424
425 const NSPoint mousePosition = [qWindow mouseLocationOutsideOfEventStream];
426 const NSSize windowSize = qWindow.frame.size;
427 if (mousePosition.x >= 0 && mousePosition.x <= windowSize.width &&
428 mousePosition.y >= 0 && mousePosition.y <= windowSize.height)
429 return qWindow;
430 }
431
432 return nil;
433}
434
435//______________________________________________________________________________
437{
438 const Util::AutoreleasePool pool;
439
440 if (QuartzWindow *topLevel = FindWindowUnderPointer()) {
441 const NSPoint mousePosition = [topLevel mouseLocationOutsideOfEventStream];
442 return (NSView<X11Window> *)[[topLevel contentView] hitTest : mousePosition];
443 }
444
445 return nil;
446}
447
448//______________________________________________________________________________
450{
451 //FindWindowForPointerEvent is required because due to grabs
452 //the receiver of the event can be different from the actual
453 //window under cursor.
454
455 assert(pointerEvent != nil &&
456 "FindWindowForPointerEvent, parameter 'pointerEvent' is nil");
457
458 const Util::AutoreleasePool pool;
459
460 NSArray * const orderedWindows = [NSApp orderedWindows];
461 for (NSWindow *nsWindow in orderedWindows) {
462 if (![nsWindow isKindOfClass : [QuartzWindow class]])
463 continue;
464
465 QuartzWindow * const qWindow = (QuartzWindow *)nsWindow;
466
467 if (qWindow.fIsDeleted)//Because of reference counting this can happen.
468 continue;
469
470 //Can it be false and still in this array???
471 if (qWindow.fMapState != kIsViewable)
472 continue;
473
474 NSPoint mousePosition = [pointerEvent locationInWindow];
475 //The event has a window, so position is in this window's coordinate system,
476 //convert it into screen point first.
477 if ([pointerEvent window]) {
478 //convertBaseToScreen is deprecated.
479 //mousePosition = [[pointerEvent window] convertBaseToScreen : mousePosition];
480 mousePosition = ConvertPointFromBaseToScreen([pointerEvent window], mousePosition);
481 }
482
483 //convertScreenToBase is deprecated.
484 //mousePosition = [qWindow convertScreenToBase : mousePosition];
485 mousePosition = ConvertPointFromScreenToBase(mousePosition, qWindow);
486
487 const NSSize windowSize = qWindow.frame.size;
488 if (mousePosition.x >= 0 && mousePosition.x <= windowSize.width &&
489 mousePosition.y >= 0 && mousePosition.y <= windowSize.height)
490 return qWindow;
491 }
492
493 return nil;
494}
495
496//______________________________________________________________________________
498{
499 //FindViewForPointerEvent is required because of grabs - the receiver of the
500 //event can be different from the actual window under cursor.
501
502 assert(pointerEvent != nil &&
503 "FindViewForPointerEvent, parameter 'pointerEvent' is nil");
504
505 const Util::AutoreleasePool pool;
506
507 if (QuartzWindow *topLevel = FindWindowForPointerEvent(pointerEvent)) {
508 NSPoint mousePosition = [pointerEvent locationInWindow];
509 if ([pointerEvent window])
510 mousePosition = ConvertPointFromBaseToScreen([pointerEvent window], mousePosition);
511
512 //convertScreenToBase is deprecated.
513 //mousePosition = [topLevel convertScreenToBase : mousePosition];
514 mousePosition = ConvertPointFromScreenToBase(mousePosition, topLevel);
515
516 return (NSView<X11Window> *)[[topLevel contentView] hitTest : mousePosition];
517 }
518
519 return nil;
520}
521
522#pragma mark - Downscale image ("reading color bits" on retina macs).
523
524//Hoho, we support C++11?? Let's return by value then!!!
525std::vector<unsigned char> DownscaledImageData(unsigned w, unsigned h, CGImageRef image)
526{
527 assert(w != 0 && h != 0 && "DownscaledImageData, invalid geometry");
528 assert(image != nullptr && "DonwscaledImageData, invalid parameter 'image'");
529
530 std::vector<unsigned char> result;
531 try {
532 result.resize(w * h * 4);
533 } catch (const std::bad_alloc &) {
534 NSLog(@"DownscaledImageData, memory allocation failed");
535 return result;
536 }
537
538 const Util::CFScopeGuard<CGColorSpaceRef> colorSpace(CGColorSpaceCreateDeviceRGB());//[1]
539 if (!colorSpace.Get()) {
540 NSLog(@"DownscaledImageData, CGColorSpaceCreateDeviceRGB failed");
541 return result;
542 }
543
544 Util::CFScopeGuard<CGContextRef> ctx(CGBitmapContextCreateWithData(&result[0], w, h, 8,
545 w * 4, colorSpace.Get(),
546 kCGImageAlphaPremultipliedLast, NULL, 0));
547 if (!ctx.Get()) {
548 NSLog(@"DownscaledImageData, CGBitmapContextCreateWithData failed");
549 return result;
550 }
551
552 CGContextDrawImage(ctx.Get(), CGRectMake(0, 0, w, h), image);
553
554 return result;
555}
556
557#pragma mark - "Focus management" - just make another window key window.
558
559//______________________________________________________________________________
561{
562 //XQuartz (and other X11
563 if (![NSApp isActive])
564 return;
565
566 const Util::AutoreleasePool pool;
567
568 NSArray * const orderedWindows = [NSApp orderedWindows];
569 for (NSWindow *nsWindow in orderedWindows) {
570 if (![nsWindow isKindOfClass : [QuartzWindow class]])
571 continue;
572
573 QuartzWindow * const qWindow = (QuartzWindow *)nsWindow;
574
575 if (qWindow.fIsDeleted || qWindow.fMapState != kIsViewable || qWindow.fID == winID)
576 continue;
577
579 continue;
580
581 [qWindow makeKeyAndOrderFront : qWindow];
582 break;
583 }
584}
585
586#pragma mark - 'shape mask' - to create a window with arbitrary (probably non-rectangle) shape.
587
588//______________________________________________________________________________
590{
591 assert(view != nil && "ClipToShapeMask, parameter 'view' is nil");
592 assert(ctx != 0 && "ClipToShapeMask, parameter 'ctx' is null");
593
594 QuartzWindow * const topLevelParent = view.fQuartzWindow;
595 assert(topLevelParent.fShapeCombineMask != nil &&
596 "ClipToShapeMask, fShapeCombineMask is nil on a top-level window");
597 assert(topLevelParent.fShapeCombineMask.fImage != 0 &&
598 "ClipToShapeMask, shape mask is null");
599
600 //Important: shape mask should have the same width and height as
601 //a top-level window. In ROOT it does not :( Say hello to visual artifacts.
602
603 //Attach clip mask to the context.
604 if (!view.fParentView) {
605 //'view' is a top-level view.
606 const CGRect clipRect = CGRectMake(0, 0, topLevelParent.fShapeCombineMask.fWidth,
607 topLevelParent.fShapeCombineMask.fHeight);
608 CGContextClipToMask(ctx, clipRect, topLevelParent.fShapeCombineMask.fImage);
609 } else {
610 NSRect clipRect = view.frame;
611 //More complex case: 'self' is a child view, we have to create a subimage from shape mask.
612 clipRect.origin = [view.fParentView convertPoint : clipRect.origin
613 toView : [view window].contentView];
614 clipRect.origin.y = X11::LocalYROOTToCocoa((NSView<X11Window> *)[view window].contentView,
615 clipRect.origin.y + clipRect.size.height);
616
617 if (AdjustCropArea(topLevelParent.fShapeCombineMask, clipRect)) {
619 clipImageGuard(CGImageCreateWithImageInRect(topLevelParent.fShapeCombineMask.fImage,
620 NSRectToCGRect(clipRect)));
621 clipRect.origin = NSPoint();
622 CGContextClipToMask(ctx, NSRectToCGRect(clipRect), clipImageGuard.Get());
623 } else {
624 //View is invisible.
625 CGRect rect = {};
626 CGContextClipToRect(ctx, rect);
627 }
628 }
629}
630
631#pragma mark - Window's geometry and attributes.
632
633//______________________________________________________________________________
635{
636 assert(attr != 0 && "SetWindowAttributes, parameter 'attr' is null");
637 assert(window != nil && "SetWindowAttributes, parameter 'window' is nil");
638
639 const Mask_t mask = attr->fMask;
640
641 if (mask & kWABackPixel)
642 window.fBackgroundPixel = attr->fBackgroundPixel;
643
644 if (mask & kWAEventMask)
645 window.fEventMask = attr->fEventMask;
646
647 if (mask & kWABitGravity)
648 window.fBitGravity = attr->fBitGravity;
649
650 if (mask & kWAWinGravity)
651 window.fWinGravity = attr->fWinGravity;
652
654 if ([(NSObject *)window isKindOfClass : [QuartzWindow class]]) {
655 QuartzWindow * const qw = (QuartzWindow *)window;
656 [qw setStyleMask : Details::kBorderlessWindowMask];
657 [qw setAlphaValue : 0.95];
658 }
659
660 window.fOverrideRedirect = YES;
661 }
662}
663
664//______________________________________________________________________________
666{
667 assert(win != nil && "GetWindowGeometry, parameter 'win' is nil");
668 assert(dst != 0 && "GetWindowGeometry, parameter 'dst' is null");
669
670 dst->fX = win.fX;
671 dst->fY = win.fY;
672
673 dst->fWidth = win.fWidth;
674 dst->fHeight = win.fHeight;
675}
676
677//______________________________________________________________________________
679{
680 assert(window != nil && "GetWindowAttributes, parameter 'window' is nil");
681 assert(dst != 0 && "GetWindowAttributes, parameter 'attr' is null");
682
683 *dst = WindowAttributes_t();
684
685 //fX, fY, fWidth, fHeight.
686 GetWindowGeometry(window, dst);
687
688 //Actually, most of them are not used by GUI.
689 dst->fBorderWidth = 0;
690 dst->fDepth = window.fDepth;
691 //Dummy value.
692 dst->fVisual = 0;
693 //Dummy value.
694 dst->fRoot = 0;
695 dst->fClass = window.fClass;
696 dst->fBitGravity = window.fBitGravity;
697 dst->fWinGravity = window.fWinGravity;
698 //Dummy value.
699 dst->fBackingStore = kAlways;//??? CHECK
700 dst->fBackingPlanes = 0;
701
702 //Dummy value.
703 dst->fBackingPixel = 0;
704
705 dst->fSaveUnder = 0;
706
707 //Dummy value.
708 dst->fColormap = 0;
709 //Dummy value.
710 dst->fMapInstalled = kTRUE;
711
712 dst->fMapState = window.fMapState;
713
714 dst->fAllEventMasks = window.fEventMask;
715 dst->fYourEventMask = window.fEventMask;
716
717 //Not used by GUI.
718 //dst->fDoNotPropagateMask
719
720 dst->fOverrideRedirect = window.fOverrideRedirect;
721 //Dummy value.
722 dst->fScreen = 0;
723}
724
725//With Apple's poor man's objective C/C++ + "brilliant" Cocoa you never know, what should be
726//the linkage of callback functions, API + language dialects == MESS. I declare/define this comparators here
727//as having "C++" linkage. If one good day clang will start to complane, I'll have to change this.
728
729#pragma mark - Comparators (I need them when changing a window's z-order).
730
731//______________________________________________________________________________
732// SDK 10.11 and above ...
733#ifdef MAC_OS_X_VERSION_10_11
734NSComparisonResult CompareViewsToLower(__kindof NSView *view1, __kindof NSView *view2, void *context)
735#else
736NSComparisonResult CompareViewsToLower(id view1, id view2, void *context)
737#endif
738{
739 id topView = (id)context;
740 if (view1 == topView)
741 return NSOrderedAscending;
742 if (view2 == topView)
743 return NSOrderedDescending;
744
745 return NSOrderedSame;
746}
747
748//______________________________________________________________________________
749// SDK 10.11 and above ...
750#ifdef MAC_OS_X_VERSION_10_11
751NSComparisonResult CompareViewsToRaise(__kindof NSView *view1, __kindof NSView *view2, void *context)
752#else
753NSComparisonResult CompareViewsToRaise(id view1, id view2, void *context)
754#endif
755{
756 id topView = (id)context;
757 if (view1 == topView)
758 return NSOrderedDescending;
759 if (view2 == topView)
760 return NSOrderedAscending;
761
762 return NSOrderedSame;
763}
764
765#pragma mark - Cursor's area.
766
767//______________________________________________________________________________
768NSPoint GetCursorHotStop(NSImage *image, ECursor cursor)
769{
770 assert(image != nil && "CursroHotSpot, parameter 'image' is nil");
771
772 const NSSize imageSize = image.size;
773
774 if (cursor == kArrowRight)
775 return NSMakePoint(imageSize.width, imageSize.height / 2);
776
777 return NSMakePoint(imageSize.width / 2, imageSize.height / 2);
778}
779
780//______________________________________________________________________________
781NSCursor *CreateCustomCursor(ECursor currentCursor)
782{
783 // Returns auto-released cursor object.
784 const char *pngFileName = 0;
785
786 switch (currentCursor) {
787 case kMove:
788 pngFileName = "move_cursor.png";
789 break;
790 case kArrowHor:
791 pngFileName = "hor_arrow_cursor.png";
792 break;
793 case kArrowVer:
794 pngFileName = "ver_arrow_cursor.png";
795 break;
796 case kArrowRight:
797 pngFileName = "right_arrow_cursor.png";
798 break;
799 case kRotate:
800 pngFileName = "rotate.png";
801 break;
802 case kBottomLeft:
803 case kTopRight:
804 pngFileName = "top_right_cursor.png";
805 break;
806 case kTopLeft:
807 case kBottomRight:
808 pngFileName = "top_left_cursor.png";
809 break;
810 default:;
811 }
812
813 if (pngFileName) {
814 const char * const path = gSystem->Which(TROOT::GetIconPath(), pngFileName, kReadPermission);
815 const Util::ScopedArray<const char> arrayGuard(path);
816
817 if (!path || path[0] == 0) {
818 //File was not found.
819 return nil;
820 }
821
822 NSString *nsPath = [NSString stringWithFormat : @"%s", path];//in autorelease pool.
823 NSImage * const cursorImage = [[NSImage alloc] initWithContentsOfFile : nsPath];
824
825 if (!cursorImage)
826 return nil;
827
828 const NSPoint hotSpot(X11::GetCursorHotStop(cursorImage, currentCursor));
829 NSCursor * const customCursor = [[[NSCursor alloc] initWithImage : cursorImage
830 hotSpot : hotSpot] autorelease];
831
832 [cursorImage release];
833
834 return customCursor;
835 }
836
837 return nil;
838}
839
840//______________________________________________________________________________
841NSCursor *CreateCursor(ECursor currentCursor)
842{
843 // Returns auto-released cursor object.
844
845 //Cursors from TVirtaulX:
846 // kBottomLeft, kBottomRight, kTopLeft, kTopRight,
847 // kBottomSide, kLeftSide, kTopSide, kRightSide,
848 // kMove, kCross, kArrowHor, kArrowVer,
849 // kHand, kRotate, kPointer, kArrowRight,
850 // kCaret, kWatch
851
852 NSCursor *cursor = nil;
853 switch (currentCursor) {
854 case kCross:
855 cursor = [NSCursor crosshairCursor];
856 break;
857 case kPointer:
858 cursor = [NSCursor arrowCursor];
859 break;
860 case kHand:
861 cursor = [NSCursor openHandCursor];
862 break;
863 case kLeftSide:
864 cursor = [NSCursor resizeLeftCursor];
865 break;
866 case kRightSide:
867 cursor = [NSCursor resizeRightCursor];
868 break;
869 case kTopSide:
870 cursor = [NSCursor resizeUpCursor];
871 break;
872 case kBottomSide:
873 cursor = [NSCursor resizeDownCursor];
874 break;
875 case kCaret:
876 cursor = [NSCursor IBeamCursor];
877 break;
878 case kRotate:
879 case kWatch:
880 default:
881 cursor = CreateCustomCursor(currentCursor);
882 }
883
884 return cursor;
885}
886
887//TGTextView/TGHtml is a very special window: it's a TGCompositeFrame,
888//which has TGCompositeFrame inside (TGViewFrame). This TGViewFrame
889//delegates Expose events to its parent, and parent tries to draw
890//inside a TGViewFrame. This does not work with default
891//QuartzView -drawRect/TGCocoa. So I need a trick to identify
892//this special window.
893
894#pragma mark - Workarounds for a text view and its descendants.
895
896//______________________________________________________________________________
897bool ViewIsTextView(unsigned viewID)
898{
899 const TGWindow * const window = gClient->GetWindowById(viewID);
900 if (!window)
901 return false;
902 // This code used to use TObject::InheritsFrom, however since this is
903 // run under the AppKit, we can not call core/meta functions, otherwise
904 // we will run into deadlocks.
905 return dynamic_cast<const TGTextView*>(window);
906}
907
908//______________________________________________________________________________
910{
911 assert(view != nil && "ViewIsTextView, parameter 'view' is nil");
912
913 return ViewIsTextView(view.fID);
914}
915
916//______________________________________________________________________________
917bool ViewIsTextViewFrame(NSView<X11Window> *view, bool checkParent)
918{
919 assert(view != nil && "ViewIsTextViewFrame, parameter 'view' is nil");
920
921 const TGWindow * const window = gClient->GetWindowById(view.fID);
922 if (!window)
923 return false;
924
925 // This code used to use TObject::InheritsFrom, however since this is
926 // run under the AppKit, we can not call core/meta functions, otherwise
927 // we will run into deadlocks.
928 if (!dynamic_cast<const TGViewFrame*>(window))
929 return false;
930
931 if (!checkParent)
932 return true;
933
934 if (!view.fParentView)
935 return false;
936
937 return ViewIsTextView(view.fParentView);
938}
939
940//______________________________________________________________________________
941bool ViewIsHtmlView(unsigned viewID)
942{
943 const TGWindow * const window = gClient->GetWindowById(viewID);
944 if (!window)
945 return false;
946 // This code used to use TObject::InheritsFrom, however since this is
947 // run under the AppKit, we can not call core/meta functions, otherwise
948 // we will run into deadlocks.
949 return window->TestBit(TGWindow::kIsHtmlView);
950}
951
952//______________________________________________________________________________
954{
955 assert(view != nil && "ViewIsHtmlView, parameter 'view' is nil");
956
957 return ViewIsHtmlView(view.fID);
958}
959
960//______________________________________________________________________________
961bool ViewIsHtmlViewFrame(NSView<X11Window> *view, bool checkParent)
962{
963 //
964 assert(view != nil && "ViewIsHtmlViewFrame, parameter 'view' is nil");
965
966 const TGWindow * const window = gClient->GetWindowById(view.fID);
967 if (!window)
968 return false;
969
970 // This code used to use TObject::InheritsFrom, however since this is
971 // run under the AppKit, we can not call core/meta functions, otherwise
972 // we will run into deadlocks.
973 if (!dynamic_cast<const TGViewFrame*>(window))
974 return false;
975
976 if (!checkParent)
977 return true;
978
979 if (!view.fParentView)
980 return false;
981
982 return ViewIsHtmlView(view.fParentView);
983}
984
985//______________________________________________________________________________
987{
988 assert(textView != nil && "FrameForTextView, parameter 'textView' is nil");
989
990 for (NSView<X11Window> *child in [textView subviews]) {
991 if (ViewIsTextViewFrame(child, false))
992 return child;
993 }
994
995 return nil;
996}
997
998//______________________________________________________________________________
1000{
1001 assert(htmlView != nil && "FrameForHtmlView, parameter 'htmlView' is nil");
1002
1003 for (NSView<X11Window> *child in [htmlView subviews]) {
1004 if (ViewIsHtmlViewFrame(child, false))
1005 return child;
1006 }
1007
1008 return nil;
1009}
1010
1011#pragma mark - Workarounds for 'paint out of paint events'.
1012
1013//______________________________________________________________________________
1015{
1016 assert(view != nil && "LockFocus, parameter 'view' is nil");
1017 assert([view isKindOfClass : [QuartzView class]] &&
1018 "LockFocus, QuartzView is expected");
1019
1020 if ([view lockFocusIfCanDraw]) {
1021 NSGraphicsContext *nsContext = [NSGraphicsContext currentContext];
1022 assert(nsContext != nil && "LockFocus, currentContext is nil");
1023 CGContextRef currContext = (CGContextRef)[nsContext graphicsPort];
1024 assert(currContext != 0 && "LockFocus, graphicsPort is null");//remove this assert?
1025
1026 ((QuartzView *)view).fContext = currContext;
1027
1028 return true;
1029 }
1030
1031 return false;
1032}
1033
1034//______________________________________________________________________________
1036{
1037 assert(view != nil && "UnlockFocus, parameter 'view' is nil");
1038 assert([view isKindOfClass : [QuartzView class]] &&
1039 "UnlockFocus, QuartzView is expected");
1040
1041 [view unlockFocus];
1042 ((QuartzView *)view).fContext = 0;
1043}
1044
1045}//X11
1046}//MacOSX
1047}//ROOT
1048
1049namespace Quartz = ROOT::Quartz;
1050namespace Util = ROOT::MacOSX::Util;
1051namespace X11 = ROOT::MacOSX::X11;
1052namespace Details = ROOT::MacOSX::Details;
1053
1054#ifdef DEBUG_ROOT_COCOA
1055
1056#pragma mark - 'loggers'.
1057
1058namespace {
1059
1060//______________________________________________________________________________
1061void log_attributes(const SetWindowAttributes_t *attr, unsigned winID)
1062{
1063 //This function is loggin requests, at the moment I can not set all
1064 //of these attributes, so I first have to check, what is actually
1065 //requested by ROOT.
1066 static std::ofstream logfile("win_attr.txt");
1067
1068 const Mask_t mask = attr->fMask;
1069 if (mask & kWABackPixmap)
1070 logfile<<"win "<<winID<<": BackPixmap\n";
1071 if (mask & kWABackPixel)
1072 logfile<<"win "<<winID<<": BackPixel\n";
1073 if (mask & kWABorderPixmap)
1074 logfile<<"win "<<winID<<": BorderPixmap\n";
1075 if (mask & kWABorderPixel)
1076 logfile<<"win "<<winID<<": BorderPixel\n";
1077 if (mask & kWABorderWidth)
1078 logfile<<"win "<<winID<<": BorderWidth\n";
1079 if (mask & kWABitGravity)
1080 logfile<<"win "<<winID<<": BitGravity\n";
1081 if (mask & kWAWinGravity)
1082 logfile<<"win "<<winID<<": WinGravity\n";
1083 if (mask & kWABackingStore)
1084 logfile<<"win "<<winID<<": BackingStore\n";
1085 if (mask & kWABackingPlanes)
1086 logfile<<"win "<<winID<<": BackingPlanes\n";
1087 if (mask & kWABackingPixel)
1088 logfile<<"win "<<winID<<": BackingPixel\n";
1090 logfile<<"win "<<winID<<": OverrideRedirect\n";
1091 if (mask & kWASaveUnder)
1092 logfile<<"win "<<winID<<": SaveUnder\n";
1093 if (mask & kWAEventMask)
1094 logfile<<"win "<<winID<<": EventMask\n";
1095 if (mask & kWADontPropagate)
1096 logfile<<"win "<<winID<<": DontPropagate\n";
1097 if (mask & kWAColormap)
1098 logfile<<"win "<<winID<<": Colormap\n";
1099 if (mask & kWACursor)
1100 logfile<<"win "<<winID<<": Cursor\n";
1101}
1102
1103//______________________________________________________________________________
1104void print_mask_info(ULong_t mask)
1105{
1106 if (mask & kButtonPressMask)
1107 NSLog(@"button press mask");
1109 NSLog(@"button release mask");
1110 if (mask & kExposureMask)
1111 NSLog(@"exposure mask");
1113 NSLog(@"pointer motion mask");
1114 if (mask & kButtonMotionMask)
1115 NSLog(@"button motion mask");
1116 if (mask & kEnterWindowMask)
1117 NSLog(@"enter notify mask");
1118 if (mask & kLeaveWindowMask)
1119 NSLog(@"leave notify mask");
1120}
1121
1122}
1123#endif
1124
1125@implementation XorDrawingView
1126{
1127 std::vector<ROOT::MacOSX::X11::Command *> xorOps;
1128}
1129
1130- (void) setXorOperations : (const std::vector<ROOT::MacOSX::X11::Command *> &) primitives
1131{
1132 xorOps = primitives;
1133}
1134
1135- (void) drawRect : (NSRect) dirtyRect
1136{
1137 [super drawRect:dirtyRect];
1138 NSGraphicsContext *nsContext = [NSGraphicsContext currentContext];
1139 if (!nsContext)
1140 return;
1141
1142 CGContextRef cgContext = nsContext.CGContext;
1143 if (!cgContext)
1144 return;
1145
1146 const Quartz::CGStateGuard ctxGuard(cgContext);
1147
1148 CGContextSetRGBStrokeColor(cgContext, 0., 0., 0., 1.);
1149 CGContextSetLineWidth(cgContext, 1.);
1150
1151 for (auto *command : xorOps) {
1152 command->Execute(cgContext);
1153 delete command;
1154 }
1155
1156 xorOps.clear();
1157}
1158
1159@end
1160
1161@implementation XorDrawingWindow
1162
1163- (instancetype) init
1164{
1165 if (self = [super init])
1166 {
1167 self.styleMask = NSWindowStyleMaskBorderless; // No titlebar, buttons, etc.
1168 self.opaque = NO;
1169 self.hasShadow = NO;
1170 self.backgroundColor = NSColor.clearColor; // No background.
1171 self.ignoresMouseEvents = YES; // Lets mouse events pass through.
1172 self.contentView = [[XorDrawingView alloc] init];
1173 }
1174 return self;
1175}
1176
1177#pragma mark - suppress the normal window behavior.
1178- (BOOL)canBecomeKeyWindow
1179{
1180 return NO;
1181}
1182- (BOOL)canBecomeMainWindow
1183{
1184 return NO;
1185}
1186
1187@end
1188
1189
1190@implementation QuartzWindow
1191
1192@synthesize fMainWindow;
1193@synthesize fHasFocus;
1194
1195#pragma mark - QuartzWindow's life cycle.
1196
1197//______________________________________________________________________________
1198- (id) initWithContentRect : (NSRect) contentRect styleMask : (NSUInteger) windowStyle
1199 backing : (NSBackingStoreType) bufferingType defer : (BOOL) deferCreation
1200 windowAttributes : (const SetWindowAttributes_t *) attr
1201{
1202 self = [super initWithContentRect : contentRect styleMask : windowStyle
1203 backing : bufferingType defer : deferCreation];
1204
1205 if (self) {
1206 //ROOT's not able to draw GUI concurrently, thanks to global variables and gVirtualX itself.
1207 [self setAllowsConcurrentViewDrawing : NO];
1208
1209 self.delegate = self;
1210 //create content view here.
1211 NSRect contentViewRect = contentRect;
1212 contentViewRect.origin.x = 0.f;
1213 contentViewRect.origin.y = 0.f;
1214
1215 fContentView = [[QuartzView alloc] initWithFrame : contentViewRect windowAttributes : 0];
1216
1217 [self setContentView : fContentView];
1218
1219 [fContentView release];
1220 fDelayedTransient = NO;
1221
1222 if (attr)
1224
1225 fIsDeleted = NO;
1226 fHasFocus = NO;
1227 }
1228
1229 return self;
1230}
1231
1232//______________________________________________________________________________
1233- (id) initWithGLView : (ROOTOpenGLView *) glView
1234{
1235 using namespace Details;
1236
1237 assert(glView != nil && "-initWithGLView, parameter 'glView' is nil");
1238
1239 const NSUInteger styleMask = kTitledWindowMask | kClosableWindowMask |
1241
1242 NSRect contentRect = glView.frame;
1243 contentRect.origin = NSPoint();
1244
1245 self = [super initWithContentRect : contentRect styleMask : styleMask
1246 backing : NSBackingStoreBuffered defer : NO];
1247
1248 if (self) {
1249 //ROOT's not able to draw GUI concurrently, thanks to global variables and gVirtualX itself.
1250 [self setAllowsConcurrentViewDrawing : NO];
1251 self.delegate = self;
1252 fContentView = glView;
1253 [self setContentView : fContentView];
1254 fDelayedTransient = NO;
1255 fIsDeleted = NO;
1256 fHasFocus = NO;
1257 }
1258
1259 return self;
1260}
1261
1262//______________________________________________________________________________
1263- (void) dealloc
1264{
1265 [fShapeCombineMask release];
1266 [super dealloc];
1267}
1268
1269//______________________________________________________________________________
1270- (BOOL) fIsDeleted
1271{
1272 return fIsDeleted;
1273}
1274
1275//______________________________________________________________________________
1276- (void) setContentView:(NSView *)cv
1277{
1278 [super setContentView:cv];
1279 if ([cv isKindOfClass:[QuartzView class]])
1280 fContentView = (QuartzView *)cv;
1281 else
1282 fContentView = nil;
1283}
1284
1285//______________________________________________________________________________
1286- (void) setFIsDeleted : (BOOL) deleted
1287{
1288 fIsDeleted = deleted;
1289}
1290
1291#pragma mark - Forwaring: I want to forward a lot of property setters/getters to the content view.
1292
1293//______________________________________________________________________________
1294- (void) forwardInvocation : (NSInvocation *) anInvocation
1295{
1296 if (!fContentView)
1297 return;
1298
1299 if ([fContentView respondsToSelector : [anInvocation selector]]) {
1300 [anInvocation invokeWithTarget : fContentView];
1301 } else {
1302 [super forwardInvocation : anInvocation];
1303 }
1304}
1305
1306//______________________________________________________________________________
1307- (NSMethodSignature*) methodSignatureForSelector : (SEL) selector
1308{
1309 NSMethodSignature *signature = [super methodSignatureForSelector : selector];
1310
1311 if (!signature) {
1312 if (fContentView)
1313 signature = [fContentView methodSignatureForSelector : selector];
1314 }
1315
1316 return signature;
1317}
1318
1319//______________________________________________________________________________
1320- (void) addTransientWindow : (QuartzWindow *) window
1321{
1322 //Transient window: all the popups (menus, dialogs, popups, comboboxes, etc.)
1323 //always should be on the top of its 'parent' window.
1324 //To enforce this ordering, I have to connect such windows with parent/child
1325 //relation (it's not the same as a view hierarchy - both child and parent
1326 //windows are top-level windows).
1327
1328 assert(window != nil && "-addTransientWindow:, parameter 'window' is nil");
1329
1330 window.fMainWindow = self;
1331
1332 if (window.fMapState != kIsViewable) {
1333 //If I add it as child, it'll immediately make a window visible
1334 //and this thing sucks.
1335 window.fDelayedTransient = YES;
1336 } else {
1337 [self addChildWindow : window ordered : NSWindowAbove];
1338 window.fDelayedTransient = NO;
1339 }
1340}
1341
1342//______________________________________________________________________________
1343- (void) makeKeyAndOrderFront : (id) sender
1344{
1345#pragma unused(sender)
1346
1347 //The more I know Cocoa, the less I like it.
1348 //Window behavior between spaces is a total mess.
1349 //Set the window to join all spaces.
1350#ifdef MAC_OS_X_VERSION_10_9
1351 [self setCollectionBehavior : NSWindowCollectionBehaviorMoveToActiveSpace];
1352#else
1353 [self setCollectionBehavior : NSWindowCollectionBehaviorCanJoinAllSpaces];
1354#endif
1355 //now bring it to the front, it will appear on the active space.
1356 [super makeKeyAndOrderFront : self];
1357 //then reset the collection behavior to default, so the window
1358 [self setCollectionBehavior : NSWindowCollectionBehaviorDefault];
1359}
1360
1361//______________________________________________________________________________
1362- (void) setFDelayedTransient : (BOOL) d
1363{
1365}
1366
1367//______________________________________________________________________________
1369{
1370 return fShapeCombineMask;
1371}
1372
1373//______________________________________________________________________________
1374- (void) setFShapeCombineMask : (QuartzImage *) mask
1375{
1376 if (mask != fShapeCombineMask) {
1377 [fShapeCombineMask release];
1378 if (mask) {
1379 fShapeCombineMask = [mask retain];
1380 //TODO: Check window's shadow???
1381 }
1382 }
1383}
1384
1385#pragma mark - X11Drawable's protocol.
1386
1387//______________________________________________________________________________
1388- (BOOL) fIsPixmap
1389{
1390 //Never.
1391 return NO;
1392}
1393
1394//______________________________________________________________________________
1396{
1397 //Never.
1398 return NO;
1399}
1400
1401//______________________________________________________________________________
1402- (CGFloat) fScaleFactor
1403{
1404 if (!self.screen)
1405 return 1.;
1406 return self.screen.backingScaleFactor;
1407}
1408
1409//______________________________________________________________________________
1410- (int) fX
1411{
1412 return X11::GlobalXCocoaToROOT(self.frame.origin.x);
1413}
1414
1415//______________________________________________________________________________
1416- (int) fY
1417{
1418 return X11::GlobalYCocoaToROOT(self.frame.origin.y + self.frame.size.height);
1419}
1420
1421//______________________________________________________________________________
1422- (unsigned) fWidth
1423{
1424 return self.frame.size.width;
1425}
1426
1427//______________________________________________________________________________
1428- (unsigned) fHeight
1429{
1430 //NSWindow's frame (height component) also includes title-bar.
1431 //So I have to use content view's height.
1432 //Obviously, there is a "hole" == 22 pixels.
1433 assert(fContentView != nil && "-fHeight:, content view is nil");
1434
1435 return fContentView.frame.size.height;
1436}
1437
1438//______________________________________________________________________________
1439- (void) setDrawableSize : (NSSize) newSize
1440{
1441 //Can not simply do self.frame.size = newSize.
1442 assert(!(newSize.width < 0) && "-setDrawableSize:, width is negative");
1443 assert(!(newSize.height < 0) && "-setDrawableSize:, height is negative");
1444
1445 NSRect frame = self.frame;
1446 // Some sanity check (rather random and inconsistent, based on the recent M1 crash/assert):
1447 if (frame.origin.x < -100000. || frame.origin.x > 100000.) {
1448 // Hope nobody has screens like this!
1449 NSLog(@"Attempting to set a frame with X: %g", frame.origin.x);
1450 frame.origin.x = 0.; // Otherwise, AppKit seems to use uint(-1) to initialise frames ... sometimes?
1451 }
1452 //dY is potentially a titlebar height.
1453 const CGFloat dY = fContentView ? frame.size.height - fContentView.frame.size.height : 0.;
1454 //Adjust the frame.
1455 frame.origin.y = frame.origin.y + frame.size.height - newSize.height - dY;
1456 frame.size = newSize;
1457 frame.size.height += dY;
1458 [self setFrame : frame display : YES];
1459}
1460
1461//______________________________________________________________________________
1462- (void) setX : (int) x Y : (int) y width : (unsigned) w height : (unsigned) h
1463{
1464 NSSize newSize = {};
1465 newSize.width = w;
1466 newSize.height = h;
1467 [self setContentSize : newSize];
1468
1469 //Check how this is affected by title bar's height.
1470 NSPoint topLeft = {};
1471 topLeft.x = X11::GlobalXROOTToCocoa(x);
1472 topLeft.y = X11::GlobalYROOTToCocoa(y);
1473
1474 [self setFrameTopLeftPoint : topLeft];
1475}
1476
1477//______________________________________________________________________________
1478- (void) setX : (int) x Y : (int) y
1479{
1480 NSPoint topLeft = {};
1481 topLeft.x = X11::GlobalXROOTToCocoa(x);
1482 topLeft.y = X11::GlobalYROOTToCocoa(y);
1483
1484 [self setFrameTopLeftPoint : topLeft];
1485}
1486
1487//______________________________________________________________________________
1488- (void) copy : (NSObject<X11Drawable> *) src area : (X11::Rectangle) area withMask : (QuartzImage *) mask
1489 clipOrigin : (X11::Point) clipXY toPoint : (X11::Point) dstPoint
1490{
1491 if (!fContentView)
1492 return;
1493
1494 [fContentView copy : src area : area withMask : mask clipOrigin : clipXY toPoint : dstPoint];
1495}
1496
1497//______________________________________________________________________________
1498- (unsigned char *) readColorBits : (X11::Rectangle) area
1499{
1500 if (!fContentView)
1501 return nullptr;
1502
1503 return [fContentView readColorBits : area];
1504}
1505
1506#pragma mark - XorDrawinWindow/View
1507
1508//______________________________________________________________________________
1510{
1511 if ([self findXorWindow])
1512 return;
1513
1514 XorDrawingWindow *special = [[XorDrawingWindow alloc] init];
1515 [self adjustXorWindowGeometry:special];
1516 [self addChildWindow : special ordered : NSWindowAbove];
1517 [special release];
1518}
1519
1520//______________________________________________________________________________
1522{
1523 if (auto win = [self findXorWindow])
1524 [self adjustXorWindowGeometry:win];
1525}
1526
1527//______________________________________________________________________________
1529{
1530 assert(win && "invalid (nil) parameter 'win'");
1531 auto frame = self.contentView.frame;
1532 frame = [self convertRectToScreen:frame];
1533 [win setFrame:frame display:NO];
1534}
1535
1536//______________________________________________________________________________
1538{
1539 if (auto win = [self findXorWindow]) {
1540 // For some reason, without ordeing out, the crosshair window's content stays
1541 // in the parent's window. Thus we first have to order out the crosshair window.
1542 [win orderOut:nil];
1543 [self removeChildWindow : win];
1544 }
1545}
1546
1547//______________________________________________________________________________
1549{
1550 auto children = [self childWindows];
1551 for (NSWindow *child in children) {
1552 if ([child isKindOfClass : XorDrawingWindow.class])
1553 return (XorDrawingWindow *)child;
1554 }
1555 return nil;
1556}
1557
1558
1559#pragma mark - X11Window protocol's implementation.
1560
1561//______________________________________________________________________________
1563{
1564 return nil;
1565}
1566
1567//______________________________________________________________________________
1568- (void) setFParentView : (QuartzView *) parent
1569{
1570#pragma unused(parent)
1571}
1572
1573//______________________________________________________________________________
1574- (NSView<X11Window> *) fContentView
1575{
1576 return fContentView;
1577}
1578
1579//______________________________________________________________________________
1581{
1582 return self;
1583}
1584
1585//... many forwards to fContentView.
1586
1587//______________________________________________________________________________
1588- (void) setFBackgroundPixel : (unsigned long) backgroundColor
1589{
1590 if (!fContentView)
1591 return;
1592
1593 if (!fShapeCombineMask) {
1594 CGFloat rgba[] = {0., 0., 0., 1.};
1595 X11::PixelToRGB(backgroundColor, rgba);
1596
1597 [self setBackgroundColor : [NSColor colorWithColorSpace : [NSColorSpace deviceRGBColorSpace] components : rgba count : 4]];
1598 }
1599
1600 fContentView.fBackgroundPixel = backgroundColor;
1601}
1602
1603//______________________________________________________________________________
1604- (unsigned long) fBackgroundPixel
1605{
1606 if (!fContentView)
1607 return 0;
1608
1610}
1611
1612//______________________________________________________________________________
1613- (int) fMapState
1614{
1615 //Top-level window can be only kIsViewable or kIsUnmapped (not unviewable).
1616 if (!fContentView)
1617 return kIsUnmapped;
1618
1619 if ([fContentView isHidden])
1620 return kIsUnmapped;
1621
1622 return kIsViewable;
1623}
1624
1625//______________________________________________________________________________
1626- (void) addChild : (NSView<X11Window> *) child
1627{
1628 assert(child != nil && "-addChild:, parameter 'child' is nil");
1629
1630 if (!fContentView) {
1631 //This can happen only in case of re-parent operation.
1632 assert([child isKindOfClass : [QuartzView class]] &&
1633 "-addChild: gl view in a top-level window as content view is not supported");
1634
1636 [self setContentView : child];
1638 } else
1639 [fContentView addChild : child];
1640}
1641
1642//______________________________________________________________________________
1643- (void) getAttributes : (WindowAttributes_t *) attr
1644{
1645 if (!fContentView)
1646 return;
1647
1648 assert(attr && "-getAttributes:, parameter 'attr' is nil");
1649
1651}
1652
1653//______________________________________________________________________________
1654- (void) setAttributes : (const SetWindowAttributes_t *) attr
1655{
1656 assert(attr != 0 && "-setAttributes:, parameter 'attr' is null");
1657
1658#ifdef DEBUG_ROOT_COCOA
1659 log_attributes(attr, self.fID);
1660#endif
1661
1663}
1664
1665//______________________________________________________________________________
1666- (void) mapRaised
1667{
1668 if (!fContentView)
1669 return;
1670
1671 const Util::AutoreleasePool pool;
1672
1673 [fContentView setHidden : NO];
1674 [self makeKeyAndOrderFront : self];
1675 [fContentView configureNotifyTree];
1676
1677 if (fDelayedTransient) {
1678 fDelayedTransient = NO;
1679 [fMainWindow addChildWindow : self ordered : NSWindowAbove];
1680 }
1681}
1682
1683//______________________________________________________________________________
1684- (void) mapWindow
1685{
1686 if (!fContentView)
1687 return;
1688
1689 const Util::AutoreleasePool pool;
1690
1691 [fContentView setHidden : NO];
1692 [self makeKeyAndOrderFront : self];
1693 [fContentView configureNotifyTree];
1694
1695 if (fDelayedTransient) {
1696 fDelayedTransient = NO;
1697 [fMainWindow addChildWindow : self ordered : NSWindowAbove];
1698 }
1699}
1700
1701//______________________________________________________________________________
1703{
1704 if (!fContentView)
1705 return;
1706
1707 const Util::AutoreleasePool pool;
1708
1709 [fContentView mapSubwindows];
1710 [fContentView configureNotifyTree];
1711}
1712
1713//______________________________________________________________________________
1714- (void) unmapWindow
1715{
1716 if (!fContentView)
1717 return;
1718
1719 [fContentView setHidden : YES];
1720 [self orderOut : self];
1721
1723 [fMainWindow removeChildWindow : self];
1724 fMainWindow = nil;
1725 }
1726}
1727
1728#pragma mark - Events.
1729
1730//______________________________________________________________________________
1731- (void) sendEvent : (NSEvent *) theEvent
1732{
1733 //With XQuartz, if you open a menu and try to move a window without closing this menu,
1734 //window does not move, menu closes, and after that you can start draggin a window again.
1735 //With Cocoa I can not do such a thing (window WILL move), but still can report button release event
1736 //to close a menu.
1737 if (!fContentView)
1738 return;
1739
1740 if (theEvent.type == Details::kLeftMouseDown || theEvent.type == Details::kRightMouseDown) {
1741 bool generateFakeRelease = false;
1742
1743 const NSPoint windowPoint = [theEvent locationInWindow];
1744
1745 if (windowPoint.x <= 4 || windowPoint.x >= self.fWidth - 4)
1746 generateFakeRelease = true;
1747
1748 if (windowPoint.y <= 4 || windowPoint.y >= self.fHeight - 4)
1749 generateFakeRelease = true;
1750
1751 const NSPoint viewPoint = [fContentView convertPoint : windowPoint fromView : nil];
1752
1753 if (viewPoint.y <= 0 && windowPoint.y >= 0)
1754 generateFakeRelease = true;
1755
1756 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
1757 "-sendEvent:, gVirtualX is either null or not of TGCocoa type");
1758
1759 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
1760 if (vx->GetEventTranslator()->HasPointerGrab() && generateFakeRelease) {
1762 theEvent.type == Details::kLeftMouseDown ?
1763 kButton1 : kButton3);
1764 //Yes, ignore this event completely (this means, you are not able to immediately start
1765 //resizing a window, if some popup is open. Actually, this is more or less
1766 //the same as with XQuartz and X11 version.
1767 return;
1768 }
1769 }
1770
1771 [super sendEvent : theEvent];
1772}
1773
1774#pragma mark - NSWindowDelegate's methods.
1775
1776//______________________________________________________________________________
1777- (BOOL) windowShouldClose : (id) sender
1778{
1779#pragma unused(sender)
1780 if (!fContentView)
1781 return NO;
1782
1783 if ([[self childWindows] count])
1784 return NO;
1785
1786 //Prepare client message for a window.
1787 Event_t closeEvent = {};
1788 closeEvent.fWindow = fContentView.fID;
1789 closeEvent.fType = kClientMessage;
1790 closeEvent.fFormat = 32;//Taken from GUI classes.
1792 closeEvent.fUser[0] = TGCocoa::fgDeleteWindowAtom;
1793 //Place it into the queue.
1794 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
1795 "-windowShouldClose:, gVirtualX is either null or has a type different from TGCocoa");
1796 ((TGCocoa *)gVirtualX)->SendEvent(fContentView.fID, &closeEvent);
1797
1798 //Do not let AppKit to close a window,
1799 //ROOT will do.
1800 return NO;
1801}
1802
1803//______________________________________________________________________________
1804- (void) windowDidBecomeKey : (NSNotification *) aNotification
1805{
1806#pragma unused(aNotification)
1807
1808 if (!fContentView)
1809 return;
1810
1812 fHasFocus = YES;
1813 //
1814 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
1815 "-windowDidBecomeKey:, gVirtualX is null or not of TGCocoa type");
1816 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
1817 vx->GetEventTranslator()->GenerateFocusChangeEvent(self.fContentView);
1818 }
1819}
1820
1821
1822//______________________________________________________________________________
1823- (void) windowDidResignKey : (NSNotification *) aNotification
1824{
1825#pragma unused(aNotification)
1826 fHasFocus = NO;
1827}
1828
1829@end
1830
1831#pragma mark - Passive key grab info.
1832
1833@implementation PassiveKeyGrab
1834
1835//______________________________________________________________________________
1836- (id) initWithKey : (unichar) keyCode modifiers : (NSUInteger) modifiers
1837{
1838 if (self = [super init]) {
1839 fKeyCode = keyCode;
1840 fModifiers = modifiers;
1841 }
1842
1843 return self;
1844}
1845
1846//______________________________________________________________________________
1847- (BOOL) matchKey : (unichar) keyCode modifiers : (NSUInteger) modifiers
1848{
1849 return keyCode == fKeyCode && modifiers == fModifiers;
1850}
1851
1852//______________________________________________________________________________
1853- (BOOL) matchKey : (unichar) keyCode
1854{
1855 return keyCode == fKeyCode;
1856}
1857
1858//______________________________________________________________________________
1859- (unichar) fKeyCode
1860{
1861 return fKeyCode;
1862}
1863
1864//______________________________________________________________________________
1865- (NSUInteger) fModifiers
1866{
1867 return fModifiers;
1868}
1869
1870@end
1871
1872#pragma mark - X11 property emulation.
1873
1877 unsigned fFormat;
1878}
1879
1880@property (nonatomic, readonly) Atom_t fType;
1881
1882@end
1883
1884@implementation QuartzWindowProperty
1885
1886@synthesize fType;
1887
1888//______________________________________________________________________________
1889- (id) initWithData : (unsigned char *) data size : (unsigned) dataSize type : (Atom_t) type format : (unsigned) format
1890{
1891 if (self = [super init]) {
1892 //Memory is zero-initialized, but just to make it explicit:
1893 fPropertyData = nil;
1894 fType = 0;
1895 fFormat = 0;
1896
1897 [self resetPropertyData : data size : dataSize type : type format : format];
1898 }
1899
1900 return self;
1901}
1902
1903//______________________________________________________________________________
1904- (void) dealloc
1905{
1906 [fPropertyData release];
1907
1908 [super dealloc];
1909}
1910
1911//______________________________________________________________________________
1912- (void) resetPropertyData : (unsigned char *) data size : (unsigned) dataSize
1913 type : (Atom_t) type format : (unsigned) format
1914{
1915 [fPropertyData release];
1916
1917 fFormat = format;
1918 if (format == 16)
1919 dataSize *= 2;
1920 else if (format == 32)
1921 dataSize *= 4;
1922
1923 fPropertyData = [[NSData dataWithBytes : data length : dataSize] retain];
1924
1925 fType = type;
1926}
1927
1928//______________________________________________________________________________
1929- (NSData *) fPropertyData
1930{
1931 return fPropertyData;
1932}
1933
1934//______________________________________________________________________________
1935- (unsigned) fFormat
1936{
1937 return fFormat;
1938}
1939
1940@end
1941
1942#pragma mark - QuartzView.
1943
1944//
1945//QuartzView is a children view (also is a content view for a top-level QuartzWindow).
1946//
1947
1948@implementation QuartzView
1949
1950@synthesize fID;
1951@synthesize fContext;
1952/////////////////////
1953//SetWindowAttributes_t/WindowAttributes_t
1954@synthesize fEventMask;
1955@synthesize fClass;
1956@synthesize fDepth;
1957@synthesize fBitGravity;
1958@synthesize fWinGravity;
1959@synthesize fBackgroundPixel;
1960@synthesize fOverrideRedirect;
1961//SetWindowAttributes_t/WindowAttributes_t
1962/////////////////////
1963@synthesize fHasFocus;
1964@synthesize fParentView;
1965
1966@synthesize fPassiveGrabButton;
1967@synthesize fPassiveGrabEventMask;
1968@synthesize fPassiveGrabKeyModifiers;
1969@synthesize fActiveGrabEventMask;
1970@synthesize fPassiveGrabOwnerEvents;
1971@synthesize fSnapshotDraw;
1972@synthesize fCurrentCursor;
1973@synthesize fIsDNDAware;
1974
1975#pragma mark - Lifetime.
1976
1977//______________________________________________________________________________
1978- (id) initWithFrame : (NSRect) frame windowAttributes : (const SetWindowAttributes_t *)attr
1979{
1980 if (self = [super initWithFrame : frame]) {
1981 //Make this explicit (though memory is zero initialized).
1982 fBackBuffer = nil;
1983 fID = 0;
1984
1985 //Passive grab parameters.
1986 fPassiveGrabButton = -1;//0 is kAnyButton.
1989
1990 fPassiveKeyGrabs = [[NSMutableArray alloc] init];
1991
1992 [self setCanDrawConcurrently : NO];
1993
1994 [self setHidden : YES];
1995 //Actually, check if view need this.
1996 //
1997 if (attr)
1999
2001 fX11Properties = [[NSMutableDictionary alloc] init];
2002
2006 }
2007
2008 return self;
2009}
2010
2011//______________________________________________________________________________
2012- (void) dealloc
2013{
2014 [fBackBuffer release];
2015 [fPassiveKeyGrabs release];
2016 [fX11Properties release];
2017 [fBackgroundPixmap release];
2018 [super dealloc];
2019}
2020
2021#pragma mark - Tracking area.
2022
2023//Tracking area is required to ... track mouse motion events inside a view.
2024
2025//______________________________________________________________________________
2026- (void) updateTrackingAreas
2027{
2028 [super updateTrackingAreas];
2029
2030 if (!fID)
2031 return;
2032
2033 const Util::AutoreleasePool pool;
2034
2035 if (NSArray *trackingArray = [self trackingAreas]) {
2036 const NSUInteger size = [trackingArray count];
2037 for (NSUInteger i = 0; i < size; ++i) {
2038 NSTrackingArea * const t = [trackingArray objectAtIndex : i];
2039 [self removeTrackingArea : t];
2040 }
2041 }
2042
2043 const NSUInteger trackerOptions = NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited |
2044 NSTrackingActiveInActiveApp | NSTrackingInVisibleRect |
2045 NSTrackingEnabledDuringMouseDrag | NSTrackingCursorUpdate;
2046
2047 NSRect frame = {};
2048 frame.size.width = self.fWidth;
2049 frame.size.height = self.fHeight;
2050
2051 NSTrackingArea * const tracker = [[NSTrackingArea alloc] initWithRect : frame
2052 options : trackerOptions owner : self userInfo : nil];
2053 [self addTrackingArea : tracker];
2054 [tracker release];
2055}
2056
2057//______________________________________________________________________________
2058- (void) updateTrackingAreasAfterRaise
2059{
2060 [self updateTrackingAreas];
2061
2062 for (QuartzView *childView in [self subviews])
2063 [childView updateTrackingAreasAfterRaise];
2064}
2065
2066#pragma mark - X11Drawable protocol.
2067
2068//______________________________________________________________________________
2069- (BOOL) fIsPixmap
2070{
2071 return NO;
2072}
2073
2074//______________________________________________________________________________
2076{
2077 return NO;
2078}
2079
2080//______________________________________________________________________________
2081- (CGFloat) fScaleFactor
2082{
2083 return self.fQuartzWindow.fScaleFactor;
2084}
2085
2086//______________________________________________________________________________
2087- (int) fX
2088{
2089 return self.frame.origin.x;
2090}
2091
2092//______________________________________________________________________________
2093- (int) fY
2094{
2095 return self.frame.origin.y;
2096}
2097
2098//______________________________________________________________________________
2099- (unsigned) fWidth
2100{
2101 return self.frame.size.width;
2102}
2103
2104//______________________________________________________________________________
2105- (unsigned) fHeight
2106{
2107 return self.frame.size.height;
2108}
2109
2110//______________________________________________________________________________
2111- (void) setDrawableSize : (NSSize) newSize
2112{
2113 assert(!(newSize.width < 0) && "-setDrawableSize, width is negative");
2114 assert(!(newSize.height < 0) && "-setDrawableSize, height is negative");
2115
2116 //This will cause redraw(?)
2117
2118 //In X11, resize changes the size, but upper-left corner is not changed.
2119 //In Cocoa, bottom-left is fixed.
2120 NSRect frame = self.frame;
2121 frame.size = newSize;
2122
2123 self.frame = frame;
2124}
2125
2126//______________________________________________________________________________
2127- (void) setX : (int) x Y : (int) y width : (unsigned) w height : (unsigned) h
2128{
2129 NSRect newFrame = {};
2130 newFrame.origin.x = x;
2131 newFrame.origin.y = y;
2132 newFrame.size.width = w;
2133 newFrame.size.height = h;
2134
2135 self.frame = newFrame;
2136}
2137
2138//______________________________________________________________________________
2139- (void) setX : (int) x Y : (int) y
2140{
2141 NSRect newFrame = self.frame;
2142 newFrame.origin.x = x;
2143 newFrame.origin.y = y;
2144
2145 self.frame = newFrame;
2146}
2147
2148//______________________________________________________________________________
2149- (void) copyImage : (QuartzImage *) srcImage area : (X11::Rectangle) area
2150 withMask : (QuartzImage *) mask clipOrigin : (X11::Point) clipXY
2151 toPoint : (X11::Point) dstPoint
2152{
2153 //Check parameters.
2154 assert(srcImage != nil &&
2155 "-copyImage:area:withMask:clipOrigin:toPoint:, parameter 'srcImage' is nil");
2156 assert(srcImage.fImage != nil &&
2157 "-copyImage:area:withMask:clipOrigin:toPoint:, srcImage.fImage is nil");
2158
2159 //Check self.
2160 assert(self.fContext != 0 &&
2161 "-copyImage:area:withMask:clipOrigin:toPoint:, self.fContext is null");
2162
2163 if (!X11::AdjustCropArea(srcImage, area)) {
2164 NSLog(@"QuartzView: -copyImage:area:withMask:clipOrigin:toPoint:,"
2165 " srcRect and copyRect do not intersect");
2166 return;
2167 }
2168
2169 //No RAII for subImage, since it can be really subimage or image itself and
2170 //in these cases there is no need to release image.
2171 CGImageRef subImage = 0;
2172 bool needSubImage = false;
2173 if (area.fX || area.fY || area.fWidth != srcImage.fWidth || area.fHeight != srcImage.fHeight) {
2174 needSubImage = true;
2175 subImage = X11::CreateSubImage(srcImage, area);
2176 if (!subImage) {
2177 NSLog(@"QuartzView: -copyImage:area:withMask:clipOrigin:toPoint:,"
2178 " subimage creation failed");
2179 return;
2180 }
2181 } else
2182 subImage = srcImage.fImage;
2183
2184 //Save context state.
2185 const Quartz::CGStateGuard ctxGuard(self.fContext);
2186
2187 //Scale and translate to undo isFlipped.
2188 CGContextTranslateCTM(self.fContext, 0., self.fHeight);
2189 CGContextScaleCTM(self.fContext, 1., -1.);
2190 //Set clip mask on a context.
2191
2192 if (mask) {
2193 assert(mask.fImage != nil &&
2194 "-copyImage:area:withMask:clipOrigin:toPoint:, mask.fImage is nil");
2195 assert(CGImageIsMask(mask.fImage) == true &&
2196 "-copyImage:area:withMask:clipOrigin:toPoint:, mask.fImage is not a mask");
2197 //clipXY.fY = X11::LocalYROOTToCocoa(self, clipXY.fY + mask.fHeight);
2198 const CGFloat clipY = X11::LocalYROOTToCocoa(self, CGFloat(clipXY.fY) + mask.fHeight);
2199 //const CGRect clipRect = CGRectMake(clipXY.fX, clipXY.fY, mask.fWidth, mask.fHeight);
2200 const CGRect clipRect = CGRectMake(clipXY.fX, clipY, mask.fWidth, mask.fHeight);
2201 CGContextClipToMask(self.fContext, clipRect, mask.fImage);
2202 }
2203
2204 //Convert from X11 to Cocoa (as soon as we scaled y * -1).
2205 //dstPoint.fY = X11::LocalYROOTToCocoa(self, dstPoint.fY + area.fHeight);
2206 const CGFloat dstY = X11::LocalYROOTToCocoa(self, CGFloat(dstPoint.fY) + area.fHeight);
2207 //const CGRect imageRect = CGRectMake(dstPoint.fX, dstPoint.fY, area.fWidth, area.fHeight);
2208 const CGRect imageRect = CGRectMake(dstPoint.fX, dstY, area.fWidth, area.fHeight);
2209 CGContextDrawImage(self.fContext, imageRect, subImage);
2210
2211 if (needSubImage)
2212 CGImageRelease(subImage);
2213}
2214
2215//______________________________________________________________________________
2216- (void) copyView : (QuartzView *) srcView area : (X11::Rectangle) area toPoint : (X11::Point) dstPoint
2217{
2218 //To copy one "window" to another "window", I have to ask source QuartzView to draw intself into
2219 //bitmap, and copy this bitmap into the destination view.
2220
2221 assert(srcView != nil && "-copyView:area:toPoint:, parameter 'srcView' is nil");
2222
2223 const NSRect frame = [srcView frame];
2224 //imageRep is in autorelease pool now.
2225 NSBitmapImageRep * const imageRep = [srcView bitmapImageRepForCachingDisplayInRect : frame];
2226 if (!imageRep) {
2227 NSLog(@"QuartzView: -copyView:area:toPoint failed");
2228 return;
2229 }
2230
2231 assert(srcView != nil && "-copyView:area:toPoint:, parameter 'srcView' is nil");
2232 assert(self.fContext != 0 && "-copyView:area:toPoint, self.fContext is null");
2233
2234 //It can happen, that src and self are the same.
2235 //cacheDisplayInRect calls drawRect with bitmap context
2236 //(and this will reset self.fContext: I have to save/restore it.
2237 CGContextRef ctx = srcView.fContext;
2238 srcView.fSnapshotDraw = YES;
2239 [srcView cacheDisplayInRect : frame toBitmapImageRep : imageRep];
2240 srcView.fSnapshotDraw = NO;
2241 srcView.fContext = ctx;
2242
2243 const CGRect subImageRect = CGRectMake(area.fX, area.fY, area.fWidth, area.fHeight);
2244 const Util::CFScopeGuard<CGImageRef> subImage(CGImageCreateWithImageInRect(imageRep.CGImage, subImageRect));
2245
2246 if (!subImage.Get()) {
2247 NSLog(@"QuartzView: -copyView:area:toPoint, CGImageCreateWithImageInRect failed");
2248 return;
2249 }
2250
2251 const Quartz::CGStateGuard ctxGuard(self.fContext);
2252 const CGRect imageRect = CGRectMake(dstPoint.fX,
2253 [self visibleRect].size.height - (CGFloat(dstPoint.fY) + area.fHeight),
2254 area.fWidth, area.fHeight);
2255
2256 CGContextTranslateCTM(self.fContext, 0., [self visibleRect].size.height);
2257 CGContextScaleCTM(self.fContext, 1., -1.);
2258
2259 CGContextDrawImage(self.fContext, imageRect, subImage.Get());
2260}
2261
2262//______________________________________________________________________________
2263- (void) copyPixmap : (QuartzPixmap *) srcPixmap area : (X11::Rectangle) area
2264 withMask : (QuartzImage *) mask clipOrigin : (X11::Point) clipXY toPoint : (X11::Point) dstPoint
2265{
2266 //Check parameters.
2267 assert(srcPixmap != nil && "-copyPixmap:area:withMask:clipOrigin:toPoint:, parameter 'srcPixmap' is nil");
2268
2269 if (!X11::AdjustCropArea(srcPixmap, area)) {
2270 NSLog(@"QuartzView: -copyPixmap:area:withMask:clipOrigin:toPoint,"
2271 " no intersection between pixmap rectangle and cropArea");
2272 return;
2273 }
2274
2275 //Check self.
2276 assert(self.fContext != 0 &&
2277 "-copyPixmap:area:withMask:clipOrigin:toPoint:, self.fContext is null");
2278
2279 //Save context state.
2280 const Quartz::CGStateGuard ctxGuard(self.fContext);
2281
2282 CGContextTranslateCTM(self.fContext, 0., self.frame.size.height);//???
2283 CGContextScaleCTM(self.fContext, 1., -1.);
2284
2285 const Util::CFScopeGuard<CGImageRef> imageFromPixmap([srcPixmap createImageFromPixmap]);
2286 assert(imageFromPixmap.Get() != 0 &&
2287 "-copyPixmap:area:withMask:clipOrigin:toPoint:, createImageFromPixmap failed");
2288
2289 CGImageRef subImage = 0;
2290 bool needSubImage = false;
2291 if (area.fX || area.fY || area.fWidth != srcPixmap.fWidth || area.fHeight != srcPixmap.fHeight) {
2292 needSubImage = true;
2293 const CGRect subImageRect = CGRectMake(area.fX, area.fY, area.fHeight, area.fWidth);
2294 subImage = CGImageCreateWithImageInRect(imageFromPixmap.Get(), subImageRect);
2295 if (!subImage) {
2296 NSLog(@"QuartzView: -copyImage:area:withMask:clipOrigin:toPoint:,"
2297 " subimage creation failed");
2298 return;
2299 }
2300 } else
2301 subImage = imageFromPixmap.Get();
2302
2303 if (mask) {
2304 assert(mask.fImage != nil &&
2305 "-copyPixmap:area:withMask:clipOrigin:toPoint:, mask.fImage is nil");
2306 assert(CGImageIsMask(mask.fImage) == true &&
2307 "-copyPixmap:area:withMask:clipOrigin:toPoint:, mask.fImage is not a mask");
2308
2309 //clipXY.fY = X11::LocalYROOTToCocoa(self, clipXY.fY + mask.fHeight);
2310 const CGFloat clipY = X11::LocalYROOTToCocoa(self, CGFloat(clipXY.fY) + mask.fHeight);
2311 //const CGRect clipRect = CGRectMake(clipXY.fX, clipXY.fY, mask.fWidth, mask.fHeight);
2312 const CGRect clipRect = CGRectMake(clipXY.fX, clipY, mask.fWidth, mask.fHeight);
2313 CGContextClipToMask(self.fContext, clipRect, mask.fImage);
2314 }
2315
2316 //dstPoint.fY = X11::LocalYCocoaToROOT(self, dstPoint.fY + area.fHeight);
2317 const CGFloat dstY = X11::LocalYCocoaToROOT(self, CGFloat(dstPoint.fY) + area.fHeight);
2318 const CGRect imageRect = CGRectMake(dstPoint.fX, dstY, area.fWidth, area.fHeight);
2319 CGContextDrawImage(self.fContext, imageRect, imageFromPixmap.Get());
2320
2321 if (needSubImage)
2322 CGImageRelease(subImage);
2323}
2324
2325
2326//______________________________________________________________________________
2327- (void) copyImage : (QuartzImage *) srcImage area : (X11::Rectangle) area
2328 toPoint : (X11::Point) dstPoint
2329{
2330 assert(srcImage != nil && "-copyImage:area:toPoint:, parameter 'srcImage' is nil");
2331 assert(srcImage.fImage != nil && "-copyImage:area:toPoint:, srcImage.fImage is nil");
2332 assert(self.fContext != 0 && "-copyImage:area:toPoint:, fContext is null");
2333
2334 if (!X11::AdjustCropArea(srcImage, area)) {
2335 NSLog(@"QuartzView: -copyImage:area:toPoint, image and copy area do not intersect");
2336 return;
2337 }
2338
2339 CGImageRef subImage = 0;
2340 bool needSubImage = false;
2341 if (area.fX || area.fY || area.fWidth != srcImage.fWidth || area.fHeight != srcImage.fHeight) {
2342 needSubImage = true;
2343 subImage = X11::CreateSubImage(srcImage, area);
2344 if (!subImage) {
2345 NSLog(@"QuartzView: -copyImage:area:toPoint:, subimage creation failed");
2346 return;
2347 }
2348 } else
2349 subImage = srcImage.fImage;
2350
2351 const Quartz::CGStateGuard ctxGuard(self.fContext);
2352
2353 CGContextTranslateCTM(self.fContext, 0., self.fHeight);
2354 CGContextScaleCTM(self.fContext, 1., -1.);
2355
2356 //dstPoint.fY = X11::LocalYCocoaToROOT(self, dstPoint.fY + area.fHeight);
2357 const CGFloat dstY = X11::LocalYCocoaToROOT(self, CGFloat(dstPoint.fY) + area.fHeight);
2358 //const CGRect imageRect = CGRectMake(dstPoint.fX, dstPoint.fY, area.fWidth, area.fHeight);
2359 const CGRect imageRect = CGRectMake(dstPoint.fX, dstY, area.fWidth, area.fHeight);
2360 CGContextDrawImage(self.fContext, imageRect, subImage);
2361
2362 if (needSubImage)
2363 CGImageRelease(subImage);
2364}
2365
2366//______________________________________________________________________________
2367- (void) copy : (NSObject<X11Drawable> *) src area : (X11::Rectangle) area
2368 withMask : (QuartzImage *)mask clipOrigin : (X11::Point) clipXY toPoint : (X11::Point) dstPoint
2369{
2370 assert(src != nil && "-copy:area:withMask:clipOrigin:toPoint:, parameter 'src' is nil");
2371 assert(area.fWidth && area.fHeight && "-copy:area:withMask:clipOrigin:toPoint:, area to copy is empty");
2372
2373 if ([src isKindOfClass : [QuartzWindow class]]) {
2374 //Forget about mask (can I have it???)
2375 QuartzWindow * const qw = (QuartzWindow *)src;
2376 //Will not work with OpenGL.
2377 [self copyView : (QuartzView *)qw.fContentView area : area toPoint : dstPoint];
2378 } else if ([src isKindOfClass : [QuartzView class]]) {
2379 //Forget about mask (can I have it???)
2380 [self copyView : (QuartzView *)src area : area toPoint : dstPoint];
2381 } else if ([src isKindOfClass : [QuartzPixmap class]]) {
2382 [self copyPixmap : (QuartzPixmap *)src area : area withMask : mask clipOrigin : clipXY toPoint : dstPoint];
2383 } else if ([src isKindOfClass : [QuartzImage class]]) {
2384 [self copyImage : (QuartzImage *)src area : area withMask : mask clipOrigin : clipXY toPoint : dstPoint];
2385 } else {
2386 assert(0 && "-copy:area:withMask:clipOrigin:toPoint:, src is of unknown type");
2387 }
2388}
2389
2390//______________________________________________________________________________
2391- (unsigned char *) readColorBits : (X11::Rectangle) area
2392{
2393 //This is quite a bad idea - to read pixels back from a view,
2394 //but our GUI does exactly this. In case of Cocoa it's expensive
2395 //and not guaranteed to work.
2396
2397 assert(area.fWidth && area.fHeight && "-readColorBits:, area to copy is empty");
2398
2399 //int, not unsigned or something - to keep it simple.
2400 NSRect visRect = [self visibleRect];
2401 // 'Sanitize' visible rect, which is different starting from macOS 14 -
2402 // in that it's considered to be visible even in a hidden part which has
2403 // no 'color bits' at all an result in reading arbitrary colored 'pixels',
2404 // probably black ones:
2405 if (visRect.origin.y < 0) {
2406 visRect.size.height += visRect.origin.y;
2407 visRect.origin.y = 0.;
2408 }
2409 if (visRect.origin.x < 0) {
2410 visRect.size.width += visRect.origin.x;
2411 visRect.origin.x = 0.;
2412 }
2413
2414 const X11::Rectangle srcRect(int(visRect.origin.x), int(visRect.origin.y),
2415 unsigned(visRect.size.width), unsigned(visRect.size.height));
2416
2417 if (!X11::AdjustCropArea(srcRect, area)) {
2418 NSLog(@"QuartzView: -readColorBits:, visible rect of view and copy area do not intersect");
2419 return nullptr;
2420 }
2421
2422 //imageRep is autoreleased.
2423 NSBitmapImageRep * const imageRep = [self bitmapImageRepForCachingDisplayInRect : visRect];
2424 if (!imageRep) {
2425 NSLog(@"QuartzView: -readColorBits:, bitmapImageRepForCachingDisplayInRect failed");
2426 return nullptr;
2427 }
2428
2429 CGContextRef ctx = self.fContext; //Save old context if any.
2430 [self cacheDisplayInRect : visRect toBitmapImageRep : imageRep];
2431 self.fContext = ctx; //Restore old context.
2432 //
2433 const NSInteger bitsPerPixel = [imageRep bitsPerPixel];
2434
2435 assert(bitsPerPixel == 32 && "-readColorBits:, no alpha channel???");
2436 const NSInteger bytesPerRow = [imageRep bytesPerRow];
2437 unsigned dataWidth = bytesPerRow / (bitsPerPixel / 8);//assume an octet :(
2438
2439 unsigned char *srcData = nullptr;
2440 std::vector<unsigned char> downscaled;
2441 if ([self.window.screen backingScaleFactor] > 1 && imageRep.CGImage) {
2442 downscaled = X11::DownscaledImageData(area.fWidth, area.fHeight, imageRep.CGImage);
2443 if (downscaled.size())
2444 srcData = &downscaled[0];
2445 dataWidth = area.fWidth;
2446 } else
2447 srcData = [imageRep bitmapData];
2448
2449 if (!srcData) {
2450 NSLog(@"QuartzView: -readColorBits:, failed to obtain backing store contents");
2451 return nullptr;
2452 }
2453
2454 //We have a source data now. Let's allocate buffer for ROOT's GUI and convert source data.
2455 unsigned char *data = nullptr;
2456
2457 try {
2458 data = new unsigned char[area.fWidth * area.fHeight * 4];//bgra?
2459 } catch (const std::bad_alloc &) {
2460 NSLog(@"QuartzView: -readColorBits:, memory allocation failed");
2461 return nullptr;
2462 }
2463
2464 unsigned char *dstPixel = data;
2465 const unsigned char *line = srcData + area.fY * dataWidth * 4;
2466 const unsigned char *srcPixel = line + area.fX * 4;
2467
2468 for (unsigned i = 0; i < area.fHeight; ++i) {
2469 for (unsigned j = 0; j < area.fWidth; ++j, srcPixel += 4, dstPixel += 4) {
2470 dstPixel[0] = srcPixel[2];
2471 dstPixel[1] = srcPixel[1];
2472 dstPixel[2] = srcPixel[0];
2473 dstPixel[3] = srcPixel[3];
2474 }
2475
2476 line += dataWidth * 4;
2477 srcPixel = line + area.fX * 4;
2478 }
2479
2480 return data;
2481}
2482
2483//______________________________________________________________________________
2484- (void) setFBackgroundPixmap : (QuartzImage *) pixmap
2485{
2486 if (fBackgroundPixmap != pixmap) {
2487 [fBackgroundPixmap release];
2488 if (pixmap)
2489 fBackgroundPixmap = [pixmap retain];
2490 else
2491 fBackgroundPixmap = nil;
2492 }
2493}
2494
2495//______________________________________________________________________________
2497{
2498 //I do not autorelease, screw this idiom!
2499
2500 return fBackgroundPixmap;
2501}
2502
2503//______________________________________________________________________________
2504- (int) fMapState
2505{
2506 if ([self isHidden])
2507 return kIsUnmapped;
2508
2509 for (QuartzView *parent = fParentView; parent; parent = parent.fParentView) {
2510 if ([parent isHidden])
2511 return kIsUnviewable;
2512 }
2513
2514 return kIsViewable;
2515}
2516
2517//______________________________________________________________________________
2518- (BOOL) fHasFocus
2519{
2520 //With the latest update clang became a bit more stupid.
2521 //Let's write a stupid useless cargo cult code
2522 //to make IT SHUT THE F... UP.
2523 (void)fHasFocus;
2524 return NO;
2525}
2526
2527//______________________________________________________________________________
2528- (void) setFHasFocus : (BOOL) focus
2529{
2530#pragma unused(focus)
2531 //With the latest update clang became a bit more stupid.
2532 //Let's write a stupid useless cargo cult code
2533 //to make IT SHUT THE F... UP.
2534 (void)fHasFocus;
2535}
2536
2537//______________________________________________________________________________
2539{
2540 return fBackBuffer;//No autorelease, I know the object's lifetime myself.
2541}
2542
2543//______________________________________________________________________________
2544- (void) setFBackBuffer : (QuartzPixmap *) backBuffer
2545{
2546 if (fBackBuffer != backBuffer) {
2547 [fBackBuffer release];
2548
2549 if (backBuffer)
2550 fBackBuffer = [backBuffer retain];
2551 else
2552 fBackBuffer = nil;
2553 }
2554}
2555
2556//______________________________________________________________________________
2557- (NSView<X11Window> *) fContentView
2558{
2559 return self;
2560}
2561
2562//______________________________________________________________________________
2564{
2565 return (QuartzWindow *)[self window];
2566}
2567
2568//______________________________________________________________________________
2570{
2572}
2573
2574//______________________________________________________________________________
2576{
2578}
2579
2580//______________________________________________________________________________
2581- (void) activateGrab : (unsigned) eventMask ownerEvents : (BOOL) ownerEvents
2582{
2584 fActiveGrabEventMask = eventMask;
2585 fActiveGrabOwnerEvents = ownerEvents;
2586}
2587
2588//______________________________________________________________________________
2589- (void) cancelGrab
2590{
2594}
2595
2596//______________________________________________________________________________
2597- (BOOL) acceptsCrossingEvents : (unsigned) eventMask
2598{
2599 bool accepts = fEventMask & eventMask;
2600
2601 //In ROOT passive grabs are always with owner_events == true.
2603 accepts = accepts || (fPassiveGrabEventMask & eventMask);
2604
2607 accepts = accepts || (fActiveGrabOwnerEvents & eventMask);
2608 else
2609 accepts = fActiveGrabOwnerEvents & eventMask;
2610 }
2611
2612 return accepts;
2613}
2614
2615//______________________________________________________________________________
2616- (void) addChild : (NSView<X11Window> *) child
2617{
2618 assert(child != nil && "-addChild:, parameter 'child' is nil");
2619
2620 [self addSubview : child];
2621 child.fParentView = self;
2622}
2623
2624//______________________________________________________________________________
2625- (void) getAttributes : (WindowAttributes_t *) attr
2626{
2627 assert(attr != 0 && "-getAttributes:, parameter 'attr' is null");
2628
2630}
2631
2632//______________________________________________________________________________
2633- (void) setAttributes : (const SetWindowAttributes_t *)attr
2634{
2635 assert(attr != 0 && "-setAttributes:, parameter 'attr' is null");
2636
2637#ifdef DEBUG_ROOT_COCOA
2638 log_attributes(attr, fID);
2639#endif
2640
2642}
2643
2644//______________________________________________________________________________
2645- (void) mapRaised
2646{
2647 //Move view to the top of subviews.
2648 QuartzView * const parent = fParentView;
2649 [self removeFromSuperview];
2650 [parent addSubview : self];
2651 [self setHidden : NO];
2652}
2653
2654//______________________________________________________________________________
2655- (void) mapWindow
2656{
2657 [self setHidden : NO];
2658}
2659
2660//______________________________________________________________________________
2662{
2663 for (QuartzView * v in [self subviews])
2664 [v setHidden : NO];
2665}
2666
2667//______________________________________________________________________________
2668- (void) unmapWindow
2669{
2670 [self setHidden : YES];
2671}
2672
2673//______________________________________________________________________________
2675{
2676 return fIsOverlapped;
2677}
2678
2679//______________________________________________________________________________
2680- (void) setOverlapped : (BOOL) overlap
2681{
2682 fIsOverlapped = overlap;
2683 for (NSView<X11Window> *child in [self subviews])
2684 [child setOverlapped : overlap];
2685}
2686
2687//______________________________________________________________________________
2688- (void) raiseWindow
2689{
2690 //Now, I can not remove window and add it ...
2691 //For example, if you click on a tab, this:
2692 //1. Creates (potentially) a passive button grab
2693 //2. Raises this tab - changes the window order.
2694 //3. On a button release - grab is release.
2695 //The tough problem is, if I remove a view from subviews
2696 //and add it ... it will never receve the
2697 //release event thus a grab will 'hang' on
2698 //view leading to bugs and artifacts.
2699 //So instead I have to ... SORT!!!!!
2700
2701 using namespace X11;//Comparators.
2702
2703 for (QuartzView *sibling in [fParentView subviews]) {
2704 if (self == sibling)
2705 continue;
2706 if ([sibling isHidden])
2707 continue;
2708
2709 if (NSEqualRects(sibling.frame, self.frame)) {
2710 [sibling setOverlapped : YES];
2711 [sibling setHidden : YES];
2712 }
2713 }
2714
2715 [self setOverlapped : NO];
2716 //
2717 [self setHidden : NO];
2718 //
2719 [fParentView sortSubviewsUsingFunction : CompareViewsToRaise context : (void *)self];
2720 //
2721 [self updateTrackingAreasAfterRaise];
2722 //
2723 [self setNeedsDisplay : YES];
2724}
2725
2726//______________________________________________________________________________
2727- (void) lowerWindow
2728{
2729 //See comment about sorting in -raiseWindow.
2730
2731 using namespace X11;
2732
2733 NSEnumerator * const reverseEnumerator = [[fParentView subviews] reverseObjectEnumerator];
2734 for (QuartzView *sibling in reverseEnumerator) {
2735 if (sibling == self)
2736 continue;
2737
2738 if (NSEqualRects(sibling.frame, self.frame)) {
2739 [sibling setOverlapped : NO];
2740 //
2741 [sibling setHidden : NO];
2742 //
2743 [sibling setNeedsDisplay : YES];
2744 [self setOverlapped : YES];
2745 //
2746 [self setHidden : YES];
2747 //
2748 break;
2749 }
2750 }
2751
2752 [fParentView sortSubviewsUsingFunction : CompareViewsToLower context : (void*)self];
2753}
2754
2755//______________________________________________________________________________
2756- (BOOL) isFlipped
2757{
2758 //Now view's placement, geometry, moving and resizing can be
2759 //done with ROOT's (X11) coordinates without conversion - we're are 'flipped'.
2760 return YES;
2761}
2762
2763//______________________________________________________________________________
2765{
2766 if (self.fMapState == kIsViewable || fIsOverlapped == YES) {
2768 assert(dynamic_cast<TGCocoa *>(gVirtualX) &&
2769 "-configureNotifyTree, gVirtualX is either null or has type different from TGCocoa");
2770 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
2771 vx->GetEventTranslator()->GenerateConfigureNotifyEvent(self, self.frame);
2772 }
2773
2774 for (NSView<X11Window> *v in [self subviews])
2775 [v configureNotifyTree];
2776 }
2777}
2778
2779#pragma mark - Key grabs.
2780
2781//______________________________________________________________________________
2782- (void) addPassiveKeyGrab : (unichar) keyCode modifiers : (NSUInteger) modifiers
2783{
2784 [self removePassiveKeyGrab : keyCode modifiers : modifiers];
2785 PassiveKeyGrab * const newGrab = [[PassiveKeyGrab alloc] initWithKey : keyCode
2786 modifiers : modifiers];
2787 [fPassiveKeyGrabs addObject : newGrab];
2788 [newGrab release];
2789}
2790
2791//______________________________________________________________________________
2792- (void) removePassiveKeyGrab : (unichar) keyCode modifiers : (NSUInteger) modifiers
2793{
2794 const NSUInteger count = [fPassiveKeyGrabs count];
2795 for (NSUInteger i = 0; i < count; ++i) {
2796 PassiveKeyGrab *grab = [fPassiveKeyGrabs objectAtIndex : i];
2797 if ([grab matchKey : keyCode modifiers : modifiers]) {
2798 [fPassiveKeyGrabs removeObjectAtIndex : i];
2799 break;
2800 }
2801 }
2802}
2803
2804//______________________________________________________________________________
2805- (PassiveKeyGrab *) findPassiveKeyGrab : (unichar) keyCode modifiers : (NSUInteger) modifiers
2806{
2807 NSEnumerator * const enumerator = [fPassiveKeyGrabs objectEnumerator];
2808 while (PassiveKeyGrab *grab = (PassiveKeyGrab *)[enumerator nextObject]) {
2809 if ([grab matchKey : keyCode modifiers : modifiers])
2810 return grab;
2811 }
2812
2813 return nil;
2814}
2815
2816//______________________________________________________________________________
2817- (PassiveKeyGrab *) findPassiveKeyGrab : (unichar) keyCode
2818{
2819 //Do not check modifiers.
2820 NSEnumerator * const enumerator = [fPassiveKeyGrabs objectEnumerator];
2821 while (PassiveKeyGrab *grab = (PassiveKeyGrab *)[enumerator nextObject]) {
2822 if ([grab matchKey : keyCode])
2823 return grab;
2824 }
2825
2826 return nil;
2827}
2828
2829#pragma mark - Painting mechanics.
2830
2831//______________________________________________________________________________
2832- (void) drawRect : (NSRect) dirtyRect
2833{
2834#pragma unused(dirtyRect)
2835
2836 using namespace X11;
2837
2838 if (fID) {
2839 if (TGWindow * const window = gClient->GetWindowById(fID)) {
2840 //It's never painted, parent renders child. true == check the parent also.
2841 if (ViewIsTextViewFrame(self, true) ||ViewIsHtmlViewFrame(self, true))
2842 return;
2843
2844 NSGraphicsContext * const nsContext = [NSGraphicsContext currentContext];
2845 assert(nsContext != nil && "-drawRect:, currentContext returned nil");
2846
2847 TGCocoa * const vx = (TGCocoa *)gVirtualX;
2848 vx->CocoaDrawON();
2849
2850 fContext = (CGContextRef)[nsContext graphicsPort];
2851 assert(fContext != 0 && "-drawRect:, graphicsPort returned null");
2852
2853 const Quartz::CGStateGuard ctxGuard(fContext);
2854
2855 //Non-rectangular windows.
2856 if (self.fQuartzWindow.fShapeCombineMask)
2858
2859 // This code used to use TObject::InheritsFrom, however since this is
2860 // run under the AppKit, we can not call core/meta functions, otherwise
2861 // we will run into deadlocks.
2862 if (dynamic_cast<const TGContainer*>(window))//It always has an ExposureMask.
2863 vx->GetEventTranslator()->GenerateExposeEvent(self, [self visibleRect]);
2864
2865 if (fEventMask & kExposureMask) {
2866 if (ViewIsTextView(self)) {
2867 //Send Expose event, using child view (this is how it's done in GUI :( ).
2868 [NSColor.whiteColor setFill];
2869 NSRect frame = self.frame;
2870 frame.origin = {};
2871 NSRectFill(frame);
2872 NSView<X11Window> * const viewFrame = FrameForTextView(self);
2873 if (viewFrame)//Now we set fExposedRegion for TGView.
2874 vx->GetEventTranslator()->GenerateExposeEvent(viewFrame, viewFrame.visibleRect);
2875 }
2876
2877 if (ViewIsHtmlView(self)) {
2878 NSView<X11Window> *const viewFrame = FrameForHtmlView(self);
2879 if (viewFrame)
2880 vx->GetEventTranslator()->GenerateExposeEvent(viewFrame, viewFrame.visibleRect);
2881 }
2882
2883 //Ask ROOT's widget/window to draw itself.
2884 gClient->NeedRedraw(window, kTRUE);
2885
2886 if (!fSnapshotDraw) {
2887 //If Cocoa repaints widget, cancel all ROOT's "outside of paint event"
2888 //rendering into this widget.
2889 gClient->CancelRedraw(window);
2891 }
2892 }
2893
2894 if (fBackBuffer) {
2895 //Very "special" window.
2896 const X11::Rectangle copyArea(0, 0, fBackBuffer.fWidth, fBackBuffer.fHeight);
2897 [self copy : fBackBuffer area : copyArea withMask : nil
2898 clipOrigin : X11::Point() toPoint : X11::Point()];
2899 }
2900
2901 vx->CocoaDrawOFF();
2902#ifdef DEBUG_ROOT_COCOA
2903 CGContextSetRGBStrokeColor(fContext, 1., 0., 0., 1.);
2904 CGContextStrokeRect(fContext, dirtyRect);
2905#endif
2906
2907 fContext = 0;
2908 } else {
2909#ifdef DEBUG_ROOT_COCOA
2910 NSLog(@"QuartzView: -drawRect: method, no window for id %u was found", fID);
2911#endif
2912 }
2913 }
2914}
2915
2916#pragma mark - Geometry.
2917
2918//______________________________________________________________________________
2919- (void) setFrame : (NSRect) newFrame
2920{
2921 //In case of TBrowser, setFrame started infinite recursion:
2922 //HandleConfigure for embedded main frame emits signal, slot
2923 //calls layout, layout calls setFrame -> HandleConfigure and etc. etc.
2924 if (NSEqualRects(newFrame, self.frame))
2925 return;
2926
2927 [super setFrame : newFrame];
2928}
2929
2930//______________________________________________________________________________
2931- (void) setFrameSize : (NSSize) newSize
2932{
2933 //Check, if setFrameSize calls setFrame.
2934
2935 [super setFrameSize : newSize];
2936
2937 if ((fEventMask & kStructureNotifyMask) && (self.fMapState == kIsViewable || fIsOverlapped == YES)) {
2938 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
2939 "setFrameSize:, gVirtualX is either null or has a type, different from TGCocoa");
2940 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
2941 vx->GetEventTranslator()->GenerateConfigureNotifyEvent(self, self.frame);
2942 }
2943
2944 [self setNeedsDisplay : YES];//?
2945}
2946
2947#pragma mark - Event handling.
2948
2949//______________________________________________________________________________
2950- (void) mouseDown : (NSEvent *) theEvent
2951{
2952 assert(fID != 0 && "-mouseDown:, fID is 0");
2953
2954 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
2955 "-mouseDown:, gVirtualX is either null or has a type, different from TGCocoa");
2956 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
2958}
2959
2960//______________________________________________________________________________
2961- (void) scrollWheel : (NSEvent*) theEvent
2962{
2963 assert(fID != 0 && "-scrollWheel:, fID is 0");
2964
2965
2966 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
2967 "-scrollWheel:, gVirtualX is either null or has a type, different from TGCocoa");
2968
2969 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
2970 const CGFloat deltaY = [theEvent deltaY];
2971 if (deltaY < 0) {
2974 } else if (deltaY > 0) {
2977 }
2978}
2979
2980#ifdef DEBUG_ROOT_COCOA
2981//______________________________________________________________________________
2982- (void) printViewInformation
2983{
2984 assert(fID != 0 && "-printWindowInformation, fID is 0");
2985 const TGWindow * const window = gClient->GetWindowById(fID);
2986 assert(window != 0 && "printWindowInformation, window not found");
2987
2988 NSLog(@"-----------------View %u info:---------------------", fID);
2989 NSLog(@"ROOT's window class is %s", window->IsA()->GetName());
2990 NSLog(@"event mask is:");
2991 print_mask_info(fEventMask);
2992 NSLog(@"grab mask is:");
2993 print_mask_info(fPassiveGrabEventMask);
2994 NSLog(@"view's geometry: x == %g, y == %g, w == %g, h == %g", self.frame.origin.x,
2995 self.frame.origin.y, self.frame.size.width, self.frame.size.height);
2996 NSLog(@"----------------End of view info------------------");
2997}
2998#endif
2999
3000//______________________________________________________________________________
3001- (void) rightMouseDown : (NSEvent *) theEvent
3002{
3003 assert(fID != 0 && "-rightMouseDown:, fID is 0");
3004
3005#ifdef DEBUG_ROOT_COCOA
3006 [self printViewInformation];
3007#endif
3008
3009 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3010 "-rightMouseDown:, gVirtualX is either null or has type different from TGCocoa");
3011 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3013}
3014
3015//______________________________________________________________________________
3016- (void) otherMouseDown : (NSEvent *) theEvent
3017{
3018 assert(fID != 0 && "-otherMouseDown:, fID is 0");
3019
3020 //Funny enough, [theEvent buttonNumber] is not the same thing as button masked in [NSEvent pressedMouseButtons],
3021 //button number actually is a kind of right operand for bitshift for pressedMouseButtons.
3022 if ([theEvent buttonNumber] == 2) {//this '2' will correspond to '4' in pressedMouseButtons.
3023 //I do not care about mouse buttons after left/right/wheel - ROOT does not have
3024 //any code for this.
3025 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3026 "-otherMouseDown:, gVirtualX is either null or has type different from TGCocoa");
3027 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3029 }
3030}
3031
3032//______________________________________________________________________________
3033- (void) mouseUp : (NSEvent *) theEvent
3034{
3035 assert(fID != 0 && "-mouseUp:, fID is 0");
3036
3037 assert(dynamic_cast<TGCocoa *>(gVirtualX) &&
3038 "-mouseUp:, gVirtualX is either null or has type different from TGCocoa");
3039 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3041}
3042
3043//______________________________________________________________________________
3044- (void) rightMouseUp : (NSEvent *) theEvent
3045{
3046
3047 assert(fID != 0 && "-rightMouseUp:, fID is 0");
3048
3049 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3050 "-rightMouseUp:, gVirtualX is either null or has type different from TGCocoa");
3051
3052 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3054}
3055
3056//______________________________________________________________________________
3057- (void) otherMouseUp : (NSEvent *) theEvent
3058{
3059 assert(fID != 0 && "-otherMouseUp:, fID is 0");
3060
3061 //Here I assume it's always kButton2.
3062 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3063 "-otherMouseUp:, gVirtualX is either null or has type different from TGCocoa");
3064 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3066}
3067
3068//______________________________________________________________________________
3069- (void) mouseEntered : (NSEvent *) theEvent
3070{
3071 assert(fID != 0 && "-mouseEntered:, fID is 0");
3072 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3073 "-mouseEntered:, gVirtualX is null or not of TGCocoa type");
3074
3075 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3077}
3078
3079//______________________________________________________________________________
3080- (void) mouseExited : (NSEvent *) theEvent
3081{
3082 assert(fID != 0 && "-mouseExited:, fID is 0");
3083
3084 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3085 "-mouseExited:, gVirtualX is null or not of TGCocoa type");
3086
3087 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3089}
3090
3091//______________________________________________________________________________
3092- (void) mouseMoved : (NSEvent *) theEvent
3093{
3094 assert(fID != 0 && "-mouseMoved:, fID is 0");
3095
3096 if (fParentView)//Suppress events in all views, except the top-level one.
3097 return;
3098
3099 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3100 "-mouseMoved:, gVirtualX is null or not of TGCocoa type");
3101
3102 TGCocoa *vx = static_cast<TGCocoa *>(gVirtualX);
3104}
3105
3106//______________________________________________________________________________
3107- (void) mouseDragged : (NSEvent *) theEvent
3108{
3109 assert(fID != 0 && "-mouseDragged:, fID is 0");
3110
3111 TGCocoa * const vx = dynamic_cast<TGCocoa *>(gVirtualX);
3112 assert(vx != 0 && "-mouseDragged:, gVirtualX is null or not of TGCocoa type");
3113
3115}
3116
3117//______________________________________________________________________________
3118- (void) rightMouseDragged : (NSEvent *) theEvent
3119{
3120 assert(fID != 0 && "-rightMouseDragged:, fID is 0");
3121
3122 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3123 "-rightMouseDragged:, gVirtualX is null or not of TGCocoa type");
3124
3125 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3127}
3128
3129//______________________________________________________________________________
3130- (void) otherMouseDragged : (NSEvent *) theEvent
3131{
3132 assert(fID != 0 && "-otherMouseDragged:, fID is 0");
3133
3134 if ([theEvent buttonNumber] == 2) {
3135 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3136 "-otherMouseDragged:, gVirtualX is null or not of TGCocoa type");
3137 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3139 }
3140}
3141
3142//______________________________________________________________________________
3143- (void) keyDown : (NSEvent *) theEvent
3144{
3145 assert(fID != 0 && "-keyDown:, fID is 0");
3146
3147 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3148 "-keyDown:, gVirtualX is null or not of TGCocoa type");
3149
3150 NSView<X11Window> *eventView = self;
3151 if (NSView<X11Window> *pointerView = X11::FindViewUnderPointer())
3152 eventView = pointerView;
3153
3154 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3155 vx->GetEventTranslator()->GenerateKeyPressEvent(eventView, theEvent);
3156}
3157
3158//______________________________________________________________________________
3159- (void) keyUp : (NSEvent *) theEvent
3160{
3161 assert(fID != 0 && "-keyUp:, fID is 0");
3162
3163 assert(dynamic_cast<TGCocoa *>(gVirtualX) != 0 &&
3164 "-keyUp:, gVirtualX is null or not of TGCocoa type");
3165
3166 TGCocoa * const vx = static_cast<TGCocoa *>(gVirtualX);
3167 NSView<X11Window> *eventView = self;
3168 if (NSView<X11Window> *pointerView = X11::FindViewUnderPointer())
3169 eventView = pointerView;
3170
3171 vx->GetEventTranslator()->GenerateKeyReleaseEvent(eventView, theEvent);
3172}
3173
3174#pragma mark - First responder stuff.
3175
3176//______________________________________________________________________________
3177- (BOOL) acceptsFirstMouse : (NSEvent *) theEvent
3178{
3179#pragma unused(theEvent)
3180 return YES;
3181}
3182
3183//______________________________________________________________________________
3184- (BOOL) acceptsFirstResponder
3185{
3186 return YES;
3187}
3188
3189#pragma mark - Cursors.
3190
3191//______________________________________________________________________________
3192- (void) setFCurrentCursor : (ECursor) cursor
3193{
3194 if (cursor != fCurrentCursor) {
3196 [self.fQuartzWindow invalidateCursorRectsForView : self];
3197 }
3198}
3199
3200//______________________________________________________________________________
3201- (NSCursor *) createCustomCursor
3202{
3203 const char *pngFileName = 0;
3204
3205 switch (fCurrentCursor) {
3206 case kMove:
3207 pngFileName = "move_cursor.png";
3208 break;
3209 case kArrowHor:
3210 pngFileName = "hor_arrow_cursor.png";
3211 break;
3212 case kArrowVer:
3213 pngFileName = "ver_arrow_cursor.png";
3214 break;
3215 case kArrowRight:
3216 pngFileName = "right_arrow_cursor.png";
3217 break;
3218 case kRotate:
3219 pngFileName = "rotate.png";
3220 break;
3221 case kBottomLeft:
3222 case kTopRight:
3223 pngFileName = "top_right_cursor.png";
3224 break;
3225 case kTopLeft:
3226 case kBottomRight:
3227 pngFileName = "top_left_cursor.png";
3228 break;
3229 default:;
3230 }
3231
3232 if (pngFileName) {
3233 const char * const path = gSystem->Which(TROOT::GetIconPath(), pngFileName, kReadPermission);
3234 const Util::ScopedArray<const char> arrayGuard(path);
3235
3236 if (!path || path[0] == 0) {
3237 //File was not found.
3238 return nil;
3239 }
3240
3241 NSString *nsPath = [NSString stringWithFormat : @"%s", path];//in autorelease pool.
3242 NSImage * const cursorImage = [[NSImage alloc] initWithContentsOfFile : nsPath];
3243
3244 if (!cursorImage)
3245 return nil;
3246
3247 NSPoint hotSpot = X11::GetCursorHotStop(cursorImage, fCurrentCursor);
3248 NSCursor * const customCursor = [[[NSCursor alloc] initWithImage : cursorImage
3249 hotSpot : hotSpot] autorelease];
3250
3251 [cursorImage release];
3252
3253 return customCursor;
3254 }
3255
3256 return nil;
3257}
3258
3259//______________________________________________________________________________
3260- (void) resetCursorRects
3261{
3262 if (NSCursor * const cursor = X11::CreateCursor(fCurrentCursor))
3263 [self addCursorRect : self.visibleRect cursor : cursor];
3264}
3265
3266//______________________________________________________________________________
3267- (void) cursorUpdate
3268{
3269 if (NSCursor * const cursor = X11::CreateCursor(fCurrentCursor)) {
3270 // NB: [window invalidateCursorRectsForView] called here has the
3271 // same problem as commented below in -cursorUpdate:.
3272 [cursor set];
3273 }
3274}
3275
3276//______________________________________________________________________________
3277- (void) cursorUpdate : (NSEvent *) event
3278{
3279#pragma unused(event)
3280 // It looks like [NSCursor set] method does not work properly when called from
3281 // cursorUpdate:, having, say, a parent frame with 'arrow' cursor and a child (completely
3282 // filling its parent's area) with 'cross', it happens the 'cross' cursor is not always
3283 // set correctly, for example:
3284 // if we have a TCanvas and resize it, cursor is 'arrow' inside this canvas,
3285 // though it must be 'cross'. This all, as it always happesn with "thinking different"
3286 // Apple is somehow related to run loop or something. As always, it's not documented,
3287 // so Apple can continue to think different. The idea with performSelector comes from:
3288 // http://stackoverflow.com/questions/8430236/nscursor-set-method-has-no-effect
3289 // Or may be it's just a bug:
3290 // http://stackoverflow.com/questions/13901232/nscursor-set-not-working-on-unfocused-window
3291 [self performSelector : @selector(cursorUpdate) withObject : nil afterDelay : 0.05f];
3292}
3293
3294#pragma mark - Emulated X11 properties.
3295
3296//______________________________________________________________________________
3297- (void) setProperty : (const char *) propName data : (unsigned char *) propData
3298 size : (unsigned) dataSize forType : (Atom_t) dataType format : (unsigned) format
3299{
3300 assert(propName != 0 && "-setProperty:data:size:forType:, parameter 'propName' is null");
3301 assert(propData != 0 && "-setProperty:data:size:forType:, parameter 'propData' is null");
3302 assert(dataSize != 0 && "-setProperty:data:size:forType:, parameter 'dataSize' is 0");
3303
3304 NSString * const key = [NSString stringWithCString : propName encoding : NSASCIIStringEncoding];
3305 QuartzWindowProperty * property = (QuartzWindowProperty *)[fX11Properties valueForKey : key];
3306
3307 //At the moment (and I think this will never change) TGX11 always calls XChangeProperty with PropModeReplace.
3308 if (property)
3309 [property resetPropertyData : propData size : dataSize type : dataType format : format];
3310 else {
3311 //No property found, add a new one.
3312 property = [[QuartzWindowProperty alloc] initWithData : propData size : dataSize
3313 type : dataType format : format];
3314 [fX11Properties setObject : property forKey : key];
3315 [property release];
3316 }
3317}
3318
3319//______________________________________________________________________________
3320- (BOOL) hasProperty : (const char *) propName
3321{
3322 assert(propName != 0 && "-hasProperty:, propName parameter is null");
3323
3324 NSString * const key = [NSString stringWithCString : propName encoding : NSASCIIStringEncoding];
3325 QuartzWindowProperty * const property = (QuartzWindowProperty *)[fX11Properties valueForKey : key];
3326
3327 return property != nil;
3328}
3329
3330//______________________________________________________________________________
3331- (unsigned char *) getProperty : (const char *) propName returnType : (Atom_t *) type
3332 returnFormat : (unsigned *) format nElements : (unsigned *) nElements
3333{
3334 assert(propName != 0 &&
3335 "-getProperty:returnType:returnFormat:nElements:, parameter 'propName' is null");
3336 assert(type != 0 &&
3337 "-getProperty:returnType:returnFormat:nElements:, parameter 'type' is null");
3338 assert(format != 0 &&
3339 "-getProperty:returnType:returnFormat:nElements:, parameter 'format' is null");
3340 assert(nElements != 0 &&
3341 "-getProperty:returnType:returnFormat:nElements:, parameter 'nElements' is null");
3342
3343 NSString * const key = [NSString stringWithCString : propName encoding : NSASCIIStringEncoding];
3344 QuartzWindowProperty * const property = (QuartzWindowProperty *)[fX11Properties valueForKey : key];
3345 assert(property != 0 &&
3346 "-getProperty:returnType:returnFormat:nElements, property not found");
3347
3348 NSData * const propData = property.fPropertyData;
3349
3350 const NSUInteger dataSize = [propData length];
3351 unsigned char *buff = 0;
3352 try {
3353 buff = new unsigned char[dataSize]();
3354 } catch (const std::bad_alloc &) {
3355 //Hmm, can I log, if new failed? :)
3356 NSLog(@"QuartzWindow: -getProperty:returnType:returnFormat:nElements:,"
3357 " memory allocation failed");
3358 return 0;
3359 }
3360
3361 [propData getBytes : buff length : dataSize];
3362 *format = property.fFormat;
3363
3364 *nElements = dataSize;
3365
3366 if (*format == 16)
3367 *nElements= dataSize / 2;
3368 else if (*format == 32)
3369 *nElements = dataSize / 4;
3370
3371 *type = property.fType;
3372
3373 return buff;
3374}
3375
3376//______________________________________________________________________________
3377- (void) removeProperty : (const char *) propName
3378{
3379 assert(propName != 0 && "-removeProperty:, parameter 'propName' is null");
3380
3381 NSString * const key = [NSString stringWithCString : propName
3382 encoding : NSASCIIStringEncoding];
3383 [fX11Properties removeObjectForKey : key];
3384}
3385
3386//DND
3387//______________________________________________________________________________
3388- (NSDragOperation) draggingEntered : (id<NSDraggingInfo>) sender
3389{
3390 NSPasteboard * const pasteBoard = [sender draggingPasteboard];
3391 const NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
3392
3393 if ([[pasteBoard types] containsObject : NSFilenamesPboardType] && (sourceDragMask & NSDragOperationCopy))
3394 return NSDragOperationCopy;
3395
3396 return NSDragOperationNone;
3397}
3398
3399//______________________________________________________________________________
3400- (BOOL) performDragOperation : (id<NSDraggingInfo>) sender
3401{
3402 //We can drag some files (images, pdfs, source code files) from
3403 //finder to ROOT's window (mainly TCanvas or text editor).
3404 //The logic is totally screwed here :((( - ROOT will try to
3405 //read a property of some window (not 'self', unfortunately) -
3406 //this works since on Window all data is in a global clipboard
3407 //(on X11 it simply does not work at all).
3408 //I'm attaching the file name as a property for the top level window,
3409 //there is no other way to make this data accessible for ROOT.
3410
3411 NSPasteboard * const pasteBoard = [sender draggingPasteboard];
3412 const NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
3413
3414 if ([[pasteBoard types] containsObject : NSFilenamesPboardType] && (sourceDragMask & NSDragOperationCopy)) {
3415
3416 //Here I try to put string ("file://....") into window's property to make
3417 //it accesible from ROOT's GUI.
3418 const Atom_t textUriAtom = gVirtualX->InternAtom("text/uri-list", kFALSE);
3419
3420 NSArray * const files = [pasteBoard propertyListForType : NSFilenamesPboardType];
3421 for (NSString *path in files) {
3422 //ROOT can not process several files, use the first one.
3423 NSString * const item = [@"file://" stringByAppendingString : path];
3424 //Yes, only ASCII encoding, but after all, ROOT's not able to work with NON-ASCII strings.
3425 const NSUInteger len = [item lengthOfBytesUsingEncoding : NSASCIIStringEncoding] + 1;
3426 try {
3427 std::vector<unsigned char> propertyData(len);
3428 [item getCString : (char *)&propertyData[0] maxLength : propertyData.size()
3429 encoding : NSASCIIStringEncoding];
3430 //There is no any guarantee, that this will ever work, logic in TGDNDManager is totally crazy.
3431 NSView<X11Window> * const targetView = self.fQuartzWindow.fContentView;
3432 [targetView setProperty : "_XC_DND_DATA" data : &propertyData[0]
3433 size : propertyData.size() forType : textUriAtom format : 8];
3434 } catch (const std::bad_alloc &) {
3435 //Hehe, can I log something in case of bad_alloc??? ;)
3436 NSLog(@"QuartzView: -performDragOperation:, memory allocation failed");
3437 return NO;
3438 }
3439
3440 break;
3441 }
3442
3443 //Property is attached now.
3444
3445 //Gdk on windows creates three events on file drop (WM_DROPFILES): XdndEnter, XdndPosition, XdndDrop.
3446 //1. Dnd enter.
3447 Event_t event1 = {};
3448 event1.fType = kClientMessage;
3449 event1.fWindow = fID;
3450 event1.fHandle = gVirtualX->InternAtom("XdndEnter", kFALSE);
3451 event1.fUser[0] = long(fID);
3452 event1.fUser[2] = textUriAtom;//gVirtualX->InternAtom("text/uri-list", kFALSE);
3453 //
3454 gVirtualX->SendEvent(fID, &event1);
3455
3456 //2. Dnd position.
3457 Event_t event2 = {};
3458 event2.fType = kClientMessage;
3459 event2.fWindow = fID;
3460 event2.fHandle = gVirtualX->InternAtom("XdndPosition", kFALSE);
3461 event2.fUser[0] = long(fID);
3462 event2.fUser[2] = 0;//Here I have to pack x and y for drop coordinates, shifting by 16 bits.
3463 NSPoint dropPoint = [sender draggingLocation];
3464 //convertPointFromBase is deprecated.
3465 //dropPoint = [self convertPointFromBase : dropPoint];
3466 dropPoint = [self convertPoint : dropPoint fromView : nil];
3467 //
3468 dropPoint = X11::TranslateToScreen(self, dropPoint);
3469 event2.fUser[2] = UShort_t(dropPoint.y) | (UShort_t(dropPoint.x) << 16);
3470
3471 gVirtualX->SendEvent(fID, &event2);
3472
3473 Event_t event3 = {};
3474 event3.fType = kClientMessage;
3475 event3.fWindow = fID;
3476 event3.fHandle = gVirtualX->InternAtom("XdndDrop", kFALSE);
3477
3478 gVirtualX->SendEvent(fID, &event3);
3479 }
3480
3481 return YES;//Always ok, even if file type is not supported - no need in "animation".
3482}
3483
3484@end
Cppyy::TCppType_t fClass
Handle_t Atom_t
WM token.
Definition GuiTypes.h:37
@ kClientMessage
Definition GuiTypes.h:63
const Mask_t kWABorderPixel
Definition GuiTypes.h:142
const Mask_t kWAOverrideRedirect
Definition GuiTypes.h:149
const Mask_t kWABitGravity
Definition GuiTypes.h:144
const Mask_t kWADontPropagate
Definition GuiTypes.h:152
ECursor
Definition GuiTypes.h:372
@ kRightSide
Definition GuiTypes.h:373
@ kBottomSide
Definition GuiTypes.h:373
@ kArrowRight
Definition GuiTypes.h:375
@ kTopLeft
Definition GuiTypes.h:372
@ kBottomRight
Definition GuiTypes.h:372
@ kArrowVer
Definition GuiTypes.h:374
@ kCaret
Definition GuiTypes.h:375
@ kTopSide
Definition GuiTypes.h:373
@ kLeftSide
Definition GuiTypes.h:373
@ kWatch
Definition GuiTypes.h:375
@ kMove
Definition GuiTypes.h:374
@ kTopRight
Definition GuiTypes.h:372
@ kBottomLeft
Definition GuiTypes.h:372
@ kHand
Definition GuiTypes.h:374
@ kCross
Definition GuiTypes.h:374
@ kRotate
Definition GuiTypes.h:374
@ kArrowHor
Definition GuiTypes.h:374
@ kPointer
Definition GuiTypes.h:375
const Mask_t kWAColormap
Definition GuiTypes.h:153
const Mask_t kButtonMotionMask
Definition GuiTypes.h:164
const Mask_t kWABackingStore
Definition GuiTypes.h:146
const Mask_t kButtonPressMask
Definition GuiTypes.h:161
const Mask_t kExposureMask
Definition GuiTypes.h:165
const Mask_t kWAEventMask
Definition GuiTypes.h:151
Handle_t Window_t
Window handle.
Definition GuiTypes.h:29
const Mask_t kWASaveUnder
Definition GuiTypes.h:150
const Mask_t kWABackPixel
Definition GuiTypes.h:140
const Mask_t kWAWinGravity
Definition GuiTypes.h:145
const Mask_t kWABackingPixel
Definition GuiTypes.h:148
const Mask_t kPointerMotionMask
Definition GuiTypes.h:163
const Mask_t kLeaveWindowMask
Definition GuiTypes.h:168
const Mask_t kStructureNotifyMask
Definition GuiTypes.h:166
@ kIsViewable
Definition GuiTypes.h:46
@ kIsUnviewable
Definition GuiTypes.h:46
@ kIsUnmapped
Definition GuiTypes.h:46
@ kAlways
Definition GuiTypes.h:45
const Mask_t kButtonReleaseMask
Definition GuiTypes.h:162
const Mask_t kWABorderPixmap
Definition GuiTypes.h:141
const Mask_t kEnterWindowMask
Definition GuiTypes.h:167
const Mask_t kWACursor
Definition GuiTypes.h:154
@ kButton4
Definition GuiTypes.h:215
@ kButton2
Definition GuiTypes.h:214
@ kButton5
Definition GuiTypes.h:215
@ kButton3
Definition GuiTypes.h:214
@ kButton1
Definition GuiTypes.h:214
const Mask_t kWABackPixmap
Definition GuiTypes.h:139
const Mask_t kWABorderWidth
Definition GuiTypes.h:143
const Mask_t kWABackingPlanes
Definition GuiTypes.h:147
#define d(i)
Definition RSha256.hxx:102
#define h(i)
Definition RSha256.hxx:106
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned short UShort_t
Definition RtypesCore.h:40
int Int_t
Definition RtypesCore.h:45
unsigned long ULong_t
Definition RtypesCore.h:55
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define gClient
Definition TGClient.h:157
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t cursor
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t TranslateCoordinates
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t GetWindowAttributes
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t rect
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t child
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t win
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name CreateCursor
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t grab
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t property
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
@ kReadPermission
Definition TSystem.h:45
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
#define gVirtualX
Definition TVirtualX.h:337
void RemoveGraphicsOperationsForWindow(Window_t wid)
Definition X11Buffer.mm:662
void GenerateFocusChangeEvent(NSView< X11Window > *eventView)
void GenerateExposeEvent(NSView< X11Window > *view, const NSRect &exposedRect)
void GenerateConfigureNotifyEvent(NSView< X11Window > *view, const NSRect &newFrame)
void GenerateCrossingEvent(NSEvent *theEvent)
void GenerateKeyReleaseEvent(NSView< X11Window > *eventView, NSEvent *theEvent)
void GenerateKeyPressEvent(NSView< X11Window > *eventView, NSEvent *theEvent)
void GenerateButtonPressEvent(NSView< X11Window > *eventView, NSEvent *theEvent, EMouseButton btn)
void GeneratePointerMotionEvent(NSEvent *theEvent)
void GenerateButtonReleaseEvent(NSView< X11Window > *eventView, NSEvent *theEvent, EMouseButton btn)
This class implements TVirtualX interface for MacOS X, using Cocoa and Quartz 2D.
Definition TGCocoa.h:58
ROOT::MacOSX::X11::CommandBuffer * GetCommandBuffer() const
Definition TGCocoa.mm:4400
void CocoaDrawOFF()
Definition TGCocoa.mm:4412
static Atom_t fgDeleteWindowAtom
Definition TGCocoa.h:469
ROOT::MacOSX::X11::Rectangle GetDisplayGeometry() const
Definition TGCocoa.mm:601
ROOT::MacOSX::X11::EventTranslator * GetEventTranslator() const
Definition TGCocoa.mm:4394
void CocoaDrawON()
Definition TGCocoa.mm:4406
Manages a content area.
Definition TGCanvas.h:31
A TGTextView is a text viewer widget.
Definition TGTextView.h:22
ROOT GUI Window base class.
Definition TGWindow.h:23
@ kIsHtmlView
Definition TGWindow.h:69
TClass * IsA() const override
Definition TGWindow.h:128
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:199
SCoord_t fY
Definition TPoint.h:36
SCoord_t fX
Definition TPoint.h:35
static const TString & GetIconPath()
Get the icon path in the installation. Static utility function.
Definition TROOT.cxx:3172
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition TSystem.cxx:1548
unsigned int fModifiers
Definition cppyy.h:12
TLine * line
NSUInteger fModifiers
ROOT::MacOSX::Util::CFScopeGuard< CGImageRef > fImage
unsigned fWidth
unsigned fHeight
unsigned fHeight
unsigned fWidth
QuartzPixmap * fBackBuffer
unsigned fPassiveGrabKeyModifiers
ECursor fCurrentCursor
void activateImplicitGrab()
BOOL fSnapshotDraw
NSMutableArray * fPassiveKeyGrabs
int fPassiveGrabButton
CGFloat fScaleFactor()
BOOL fActiveGrabOwnerEvents
QuartzWindow * fQuartzWindow
void configureNotifyTree()
void activatePassiveGrab()
CGContextRef fContext
NSMutableDictionary * fX11Properties
BOOL fPassiveGrabOwnerEvents
BOOL fIsOpenGLWidget()
unsigned fWidth()
QuartzView * fParentView
unsigned fID
unsigned fPassiveGrabEventMask
QuartzImage * fBackgroundPixmap
ROOT::MacOSX::X11::PointerGrab fCurrentGrabType
BOOL fIsOverlapped
unsigned long fBackgroundPixel
unsigned fActiveGrabEventMask
unsigned fHeight()
BOOL fOverrideRedirect
NSView< X11Window > * fContentView
unsigned long fBackgroundPixel
QuartzView * fParentView
unsigned fHeight()
CGFloat fScaleFactor()
BOOL fDelayedTransient
QuartzWindow * fQuartzWindow
void adjustXorWindowGeometry()
XorDrawingWindow * findXorWindow()
QuartzWindow * fMainWindow
QuartzView * fContentView
unsigned fWidth()
QuartzImage * fShapeCombineMask
instancetype init()
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
void(off) SmallVectorTemplateBase< T
const NSUInteger kMiniaturizableWindowMask
const NSEventType kLeftMouseDown
const NSEventType kRightMouseDown
const NSUInteger kTitledWindowMask
const NSUInteger kResizableWindowMask
const NSUInteger kClosableWindowMask
const NSUInteger kBorderlessWindowMask
NSCursor * CreateCustomCursor(ECursor currentCursor)
int GlobalYROOTToCocoa(CGFloat yROOT)
NSPoint ConvertPointFromScreenToBase(NSPoint screenPoint, NSWindow *window)
bool ViewIsHtmlViewFrame(NSView< X11Window > *view, bool checkParent)
int GlobalYCocoaToROOT(CGFloat yCocoa)
void PixelToRGB(Pixel_t pixelColor, CGFloat *rgb)
Definition X11Colors.mm:920
NSPoint ConvertPointFromBaseToScreen(NSWindow *window, NSPoint windowPoint)
NSPoint TranslateToScreen(NSView< X11Window > *from, NSPoint point)
CGImageRef CreateSubImage(QuartzImage *image, const Rectangle &area)
bool ScreenPointIsInView(NSView< X11Window > *view, Int_t x, Int_t y)
NSPoint GetCursorHotStop(NSImage *image, ECursor cursor)
void GetWindowGeometry(NSObject< X11Window > *win, WindowAttributes_t *dst)
void GetWindowAttributes(NSObject< X11Window > *window, WindowAttributes_t *dst)
NSView< X11Window > * FindDNDAwareViewInPoint(NSView *parentView, Window_t dragWinID, Window_t inputWinID, Int_t x, Int_t y, Int_t maxDepth)
NSView< X11Window > * FrameForHtmlView(NSView< X11Window > *htmlView)
QuartzWindow * FindWindowInPoint(Int_t x, Int_t y)
int GlobalXCocoaToROOT(CGFloat xCocoa)
void WindowLostFocus(Window_t winID)
int LocalYROOTToCocoa(NSView< X11Window > *parentView, CGFloat yROOT)
NSView< X11Window > * FindViewForPointerEvent(NSEvent *pointerEvent)
NSView< X11Window > * FrameForTextView(NSView< X11Window > *textView)
NSComparisonResult CompareViewsToLower(id view1, id view2, void *context)
int LocalYCocoaToROOT(NSView< X11Window > *parentView, CGFloat yCocoa)
QuartzWindow * CreateTopLevelWindow(Int_t x, Int_t y, UInt_t w, UInt_t h, UInt_t border, Int_t depth, UInt_t clss, void *visual, SetWindowAttributes_t *attr, UInt_t)
NSView< X11Window > * FindViewUnderPointer()
void UnlockFocus(NSView< X11Window > *view)
int GlobalXROOTToCocoa(CGFloat xROOT)
QuartzView * CreateChildView(QuartzView *parent, Int_t x, Int_t y, UInt_t w, UInt_t h, UInt_t border, Int_t depth, UInt_t clss, void *visual, SetWindowAttributes_t *attr, UInt_t wtype)
bool AdjustCropArea(const Rectangle &srcRect, Rectangle &cropArea)
QuartzWindow * FindWindowForPointerEvent(NSEvent *pointerEvent)
bool ViewIsTextView(unsigned viewID)
void GetRootWindowAttributes(WindowAttributes_t *attr)
bool ViewIsHtmlView(unsigned viewID)
QuartzWindow * FindWindowUnderPointer()
bool ViewIsTextViewFrame(NSView< X11Window > *view, bool checkParent)
NSPoint TranslateFromScreen(NSPoint point, NSView< X11Window > *to)
void ClipToShapeMask(NSView< X11Window > *view, CGContextRef ctx)
void SetWindowAttributes(const SetWindowAttributes_t *attr, NSObject< X11Window > *window)
NSCursor * CreateCursor(ECursor currentCursor)
bool LockFocus(NSView< X11Window > *view)
NSComparisonResult CompareViewsToRaise(id view1, id view2, void *context)
std::vector< unsigned char > DownscaledImageData(unsigned w, unsigned h, CGImageRef image)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
unsigned fID
Definition X11Drawable.h:37
Event structure.
Definition GuiTypes.h:174
EGEventType fType
of event (see EGEventType)
Definition GuiTypes.h:175
Handle_t fHandle
general resource handle (used for atoms or windows)
Definition GuiTypes.h:185
Int_t fFormat
Next fields only used by kClientMessageEvent.
Definition GuiTypes.h:186
Window_t fWindow
window reported event is relative to
Definition GuiTypes.h:176
Longptr_t fUser[5]
5 longs can be used by client message events NOTE: only [0], [1] and [2] may be used.
Definition GuiTypes.h:187
Attributes that can be used when creating or changing a window.
Definition GuiTypes.h:93
Window attributes that can be inquired.
Definition GuiTypes.h:114
Window_t fRoot
root of screen containing window
Definition GuiTypes.h:120
Int_t fMapState
kIsUnmapped, kIsUnviewable, kIsViewable
Definition GuiTypes.h:130
ULong_t fBackingPlanes
planes to be preserved if possible
Definition GuiTypes.h:125
Long_t fAllEventMasks
set of events all people have interest in
Definition GuiTypes.h:131
ULong_t fBackingPixel
value to be used when restoring planes
Definition GuiTypes.h:126
Long_t fYourEventMask
my event mask
Definition GuiTypes.h:132
Int_t fHeight
width and height of window
Definition GuiTypes.h:116
Bool_t fMapInstalled
boolean, is color map currently installed
Definition GuiTypes.h:129
Int_t fBorderWidth
border width of window
Definition GuiTypes.h:117
Int_t fWinGravity
one of the window gravity values
Definition GuiTypes.h:123
void * fScreen
back pointer to correct screen
Definition GuiTypes.h:135
Colormap_t fColormap
color map to be associated with window
Definition GuiTypes.h:128
Int_t fClass
kInputOutput, kInputOnly
Definition GuiTypes.h:121
Int_t fBitGravity
one of bit gravity values
Definition GuiTypes.h:122
void * fVisual
the associated visual structure
Definition GuiTypes.h:119
Int_t fBackingStore
kNotUseful, kWhenMapped, kAlways
Definition GuiTypes.h:124
Int_t fY
location of window
Definition GuiTypes.h:115
Int_t fDepth
depth of window
Definition GuiTypes.h:118
Bool_t fOverrideRedirect
boolean value for override-redirect
Definition GuiTypes.h:134
Bool_t fSaveUnder
boolean, should bits under be saved?
Definition GuiTypes.h:127