forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootxx-cocoa.mm
1164 lines (904 loc) · 35.7 KB
/
rootxx-cocoa.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/rootxx-cocoa.mm:$Id$
// Author: Timur Pocheptsov 22/01/2014
//////////////////////////////////////////////////////////////////////////
// //
// Rootxx-cocoa //
// //
// Cocoa based routines used to display the splash screen for rootx, //
// the root front-end program. //
// //
// WaitChildGeneric/StayUp/-main (ROOTWaitpidThread) copy-pasted/based //
// on rootx.cxx/rootxx.cxx (author - Fons Rademakers). //
// //
//////////////////////////////////////////////////////////////////////////
#include <cassert>
#include <fstream>
#include <cstdlib>
#include <csignal>
#include <string>
#include <cerrno>
#include <list>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <pwd.h>
#include <Cocoa/Cocoa.h>
#include "CocoaUtils.h"
#include "RConfigure.h"
#include "RVersion.h"
#include "rootcoreteam.h"
//
//'root' with Cocoa is a quite special application. In principle, it's a background process,
//but it can create a GUI (in our case it's a simple splash-screen - a window with a ROOT's logo
//and "Credits" info. The first version was converting its process into foreground, which
//leads to some undesired effects like ... lost keyboard focus. Now this process never explicitly
//converted into foreground and [NSApp activateAppIgnorinxxxx] is never called.
//Instead, special window style is used - NSNonactivatingPanelMask (splash-screen's class derives NSPanel)
//and I still have to specify activation policy (to avoid NSInternalInconsistencyException on window creation).
//Also, I set a window level to NSFloatingPanelWindow (otherwise window is not visible).
//This behavior is still different from how X11 based root works on Mac, but is very close.
//
namespace ROOT {
namespace ROOTX {
//This had internal linkage before, now must be accessible from rootx-cocoa.mm.
extern int gChildpid;
}
}
namespace {
NSString * const gConception = @"Rene Brun, Fons Rademakers\n\n";
bool showAboutInfo = false;
}
//ROOTSplashScreenView: content view for our panel (splash screen window)
//with a background (ROOT's logo) + scrollview and textview to show info
//about contributors.
@interface ROOTSplashScreenView : NSView {
@private
NSImage *backgroundImage;
NSScrollView *scrollView;
NSTextView *textView;
NSMutableDictionary *versionTextAttributes;
}
@end
@implementation ROOTSplashScreenView
//_________________________________________________________________
- (id) initWithImage : (NSImage *) image text : (NSAttributedString *) textToScroll aboutMode : (BOOL) about
{
assert(image != nil && "initWithImage:text:, parameter 'image' is nil");
assert(textToScroll != nil && "initWithImage:text:, parameter 'textToScroll' is nil");
using ROOT::MacOSX::Util::NSScopeGuard;
NSInteger pixelWidth = 0, pixelHeight = 0;
{//For autorelease pool.
const NSScopeGuard<NSAutoreleasePool> pool([[NSAutoreleasePool alloc] init]);
NSArray * const reps = image.representations;
for (NSImageRep *rep in reps) {
//Find a max. may be?
pixelWidth = rep.pixelsWide;
pixelHeight = rep.pixelsHigh;
//Now I stop on the first representation
//(as I know for the current image - it's ok)
break;
}
}
//minimal sizes required by text view's position (which is 'hardcoded' and
//must be the same as in rootxx (X11 version).
// assert(imageSize.width >= 300 && imageSize.height >= 285 &&
// "initWithImage:text:, unexpected background image sizes");
self = [super initWithFrame : NSMakeRect(0, 0, pixelWidth, pixelHeight)];
if (self) {
//Let's create our child views.
backgroundImage = [image retain];
NSRect scrollRect = NSMakeRect(110., 25., 455., 80.);
scrollView = [[NSScrollView alloc] initWithFrame : scrollRect];
[self addSubview : scrollView];
scrollRect.origin = NSPoint();
textView = [[NSTextView alloc] initWithFrame : scrollRect];
[textView setEditable : NO];
[[textView textStorage] setAttributedString : textToScroll];
[scrollView setDocumentView : textView];
[scrollView setBorderType : NSNoBorder];
if (about) {
[textView setTextContainerInset : NSMakeSize(0., scrollRect.size.height)];
[textView scrollPoint : NSMakePoint(0., scrollRect.size.height)];
}
const NSScopeGuard<NSShadow> dropShadow([[NSShadow alloc] init]);
[dropShadow.Get() setShadowColor:[NSColor blackColor]];
[dropShadow.Get() setShadowBlurRadius : 10.];
[self setShadow : dropShadow.Get()];
//Hehehehe.
[scrollView setDrawsBackground : NO];
[textView setDrawsBackground : NO];
//
if (NSFont * const font = [NSFont fontWithName : @"Tahoma" size : 12.]) {
//Will be some special font here.
NSScopeGuard<NSMutableDictionary> dict([[NSMutableDictionary alloc] init]);
[dict.Get() setObject : font forKey : NSFontAttributeName];
[dict.Get() setObject : [NSColor whiteColor] forKey : NSForegroundColorAttributeName];
versionTextAttributes = dict.Get();
dict.Release();
}
}
return self;
}
//_________________________________________________________________
- (void) dealloc
{
[backgroundImage release];
[textView release];
[scrollView release];
[versionTextAttributes release];
[super dealloc];
}
//Background image.
//_________________________________________________________________
- (void) drawRect : (NSRect) rect
{
#pragma unused(rect)
assert(backgroundImage != nil && "drawRect:, backgroundImage is nil");
NSRect frame = self.frame;
frame.origin = NSPoint();
const NSSize imageSize = backgroundImage.size;
[backgroundImage drawInRect : frame
fromRect : NSMakeRect(0., 0., imageSize.width, imageSize.height)
operation : NSCompositeSourceOver
fraction : 1.];
//Let's now draw a version.
if (versionTextAttributes) {
if (NSString * const version = [NSString stringWithFormat : @"Version %s", ROOT_RELEASE])
[version drawAtPoint : NSMakePoint(frame.size.width - 90., 5.) withAttributes : versionTextAttributes];
}
}
//Animation.
//_________________________________________________________________
- (void) scrollText
{
const CGFloat scrollAmountPixels = 1.;
// How far have we scrolled so far?
const CGFloat currentScrollAmount = [scrollView documentVisibleRect].origin.y;
if (currentScrollAmount + scrollAmountPixels >= textView.frame.size.height - scrollView.frame.size.height)
[textView scrollPoint : NSMakePoint(0., 0.)];//Make a "loop".
else {
[textView scrollPoint : NSMakePoint(0., currentScrollAmount + scrollAmountPixels)];
}
// If anything overlaps the text we just scrolled, it won’t get redraw by the
// scrolling, so force everything in that part of the panel to redraw.
NSRect scrollViewFrame = [scrollView bounds];
scrollViewFrame = [self convertRect : scrollViewFrame fromView : scrollView];
// Redraw everything which overlaps it.
[self setNeedsDisplayInRect : scrollViewFrame];
}
@end
//To be set from a signal handler.
namespace {
bool popupDone = false;
}
//Our "top-level-window" - borderless panel.
@interface ROOTSplashScreenPanel : NSPanel
@end
@implementation ROOTSplashScreenPanel
//Events and behaviour.
//_________________________________________________________________
- (BOOL) canBecomeMainWindow
{
return NO;//YES;
}
//_________________________________________________________________
- (BOOL) canBecomeKeyWindow
{
return NO;//YES;
}
//_________________________________________________________________
- (void) sendEvent : (NSEvent *) theEvent
{
if ([theEvent type] == NSKeyDown) {
popupDone = true;
return;
}
if ([theEvent type] == NSLeftMouseDown || [theEvent type] == NSRightMouseDown) {
popupDone = true;
return;
}
[super sendEvent : theEvent];
}
@end
//
//Waitpid is a blocking call and we're a foreground process (a 'normal' Cocoa application).
//Waitpid with WNOHUNG return immediately and does not solve our problems.
//Here's the trick: create a background thread executing ... waitpid
//and performing its special selector on a main thread (when status changed).
//
namespace {
//Timer callbacks 'fire' custom NSEvents:
enum CustomEventSource {//make it enum class when C++11 is here.
kScrollTimer = 1,
kSignalTimer = 2,
kWaitpidThread = 3
};
}
@interface ROOTWaitpidThread : NSThread {
int status;
int result;
bool normalExit;
}
- (int) getStatus;
@end
@implementation ROOTWaitpidThread
//_________________________________________________________________
- (id) init
{
if (self = [super init]) {
status = 0;
result = 0;
normalExit = false;
}
return self;
}
//_________________________________________________________________
- (int) getStatus
{
return status;
}
//_________________________________________________________________
- (void) main
{
using ROOT::ROOTX::gChildpid;
do {
while ((result = ::waitpid(gChildpid, &status, WUNTRACED) < 0)) {
if (errno != EINTR)
break;
errno = 0;
}
if (WIFEXITED(status) || WIFSIGNALED(status)) {
[self performSelectorOnMainThread : @selector(exitEventLoop) withObject : nil waitUntilDone : NO];
return;
}
if (WIFSTOPPED(status)) { // child got ctlr-Z
::raise(SIGTSTP); // stop also parent
::kill(gChildpid, SIGCONT); // if parent wakes up, wake up child
}
} while (WIFSTOPPED(status));
normalExit = true;
[self performSelectorOnMainThread : @selector(exitEventLoop) withObject : nil waitUntilDone : NO];
}
//_________________________________________________________________
- (void) exitEventLoop
{
//assert - we are on a main thread.
NSEvent * const timerEvent = [NSEvent otherEventWithType : NSApplicationDefined
location : NSMakePoint(0, 0) modifierFlags : 0
timestamp : 0. windowNumber : 0 context : nil
subtype : 0 data1 : kWaitpidThread data2 : 0];
[NSApp postEvent : timerEvent atStart : NO];
}
@end
@interface ROOTSplashScreenAppDelegate : NSObject<NSApplicationDelegate>
@end
@implementation ROOTSplashScreenAppDelegate
//_________________________________________________________________
- (void) applicationDidResignActive : (NSNotification *) aNotification
{
#pragma unused(aNotification)
popupDone = true;
}
@end
namespace {
volatile sig_atomic_t popdown = 0;
ROOTSplashScreenPanel *splashScreen = nil;
//Top-level autorelease pool.
NSAutoreleasePool * topLevelPool = nil;
//We use a signal timer to check:
//if we got a SIGUSR1 (see rootx.cxx) or
//if we have to remove a splash-screen after a delay.
CFRunLoopTimerRef signalTimer = 0;
const CFTimeInterval signalInterval = 0.1;
timeval popupCreationTime;
const CFTimeInterval splashScreenDelayInSec = 2.;//4 seconds in rootx.cxx, I'm gonna use 2.
//Timer for a scroll animation.
CFRunLoopTimerRef scrollTimer = 0;
const CFTimeInterval scrollInterval = 0.1;
//Aux. functions:
bool InitCocoa();
bool InitTimers();
void RunEventLoop();
void RunEventLoopInBackground();
bool StayUp();
bool CreateSplashscreen(bool about);
void SetSplashscreenPosition();
//Non GUI aux. function.
NSAttributedString *CreateTextToScroll(bool about);
bool ReadContributors(std::list<std::string> & contributors);
}//unnamed namespace.
//Platform-specific (OS X) versions of PopupLogo, WaitLogo, PopdownLogo, CloseDisplay.
//_________________________________________________________________
void PopupLogo(bool about)
{
if (!InitCocoa()) {
//TODO: diagnostic.
return;
}
//0. For StayUp to check when we should hide our splash-screen.
if (gettimeofday(&popupCreationTime, 0) == -1) {
//TODO: check errno and issue a message,
//we need a valid popup creation time.
return;
}
if (!CreateSplashscreen(about)) {
//TODO: diagnostic.
return;
}
showAboutInfo = about;
SetSplashscreenPosition();
[splashScreen setLevel : NSFloatingWindowLevel];
[splashScreen makeKeyAndOrderFront : nil];
}
//_________________________________________________________________
void PopdownLogo()
{
//This function is called from the signal handler.
popdown = 1;
}
//_________________________________________________________________
void WaitLogo()
{
if (!splashScreen)
//TODO: diagnostic.
return;
ROOTSplashScreenAppDelegate *delegate = nil;
if (showAboutInfo) {
delegate = [[ROOTSplashScreenAppDelegate alloc] init];
[[NSApplication sharedApplication] setDelegate : delegate];
}
RunEventLoop();
//Cleanup.
[splashScreen orderOut : nil];
[splashScreen release];
splashScreen = nil;
if (showAboutInfo) {
[NSApp setDelegate : nil];
[delegate release];
}
}
//_________________________________________________________________
void CloseDisplay()
{
//Noop.
}
//Aux. functions.
extern "C" {
//_________________________________________________________________
void ROOTSplashscreenTimerCallback(CFRunLoopTimerRef timer, void *info)
{
#pragma unused(info)
if (timer == signalTimer) {
NSEvent * const timerEvent = [NSEvent otherEventWithType :
NSApplicationDefined location : NSMakePoint(0, 0) modifierFlags : 0
timestamp : 0. windowNumber : 0 context : nil
subtype : 0 data1 : kSignalTimer data2 : 0];
[NSApp postEvent : timerEvent atStart : NO];
} else {
NSEvent * const timerEvent = [NSEvent otherEventWithType : NSApplicationDefined
location : NSMakePoint(0, 0) modifierFlags : 0
timestamp : 0. windowNumber : 0 context : nil
subtype : 0 data1 : kScrollTimer data2 : 0];
[NSApp postEvent : timerEvent atStart : NO];
}
}
}//extern "C"
namespace {
//_________________________________________________________________
bool InitCocoa()
{
if (!topLevelPool) {
[[NSApplication sharedApplication] setActivationPolicy : NSApplicationActivationPolicyAccessory];//Do I need this?
topLevelPool = [[NSAutoreleasePool alloc] init];
}
return true;
}
//_________________________________________________________________
bool InitTimers()
{
assert(scrollTimer == 0 && "InitTimers, scrollTimer was initialized already");
assert(signalTimer == 0 && "InitTimers, signalTimer was initialized already");
using ROOT::MacOSX::Util::CFScopeGuard;
CFScopeGuard<CFRunLoopTimerRef> guard1(CFRunLoopTimerCreate(kCFAllocatorDefault,//allocator
CFAbsoluteTimeGetCurrent() + signalInterval,//fireDate
signalInterval,//interval in seconds(?)
0,//flags - not used
0,//order - not used
ROOTSplashscreenTimerCallback,
0//info
));
if (!guard1.Get())
return false;
//Scroll animation.
if (showAboutInfo) {
CFScopeGuard<CFRunLoopTimerRef> guard2(CFRunLoopTimerCreate(kCFAllocatorDefault,
CFAbsoluteTimeGetCurrent() + splashScreenDelayInSec,
scrollInterval,
0,
0,
ROOTSplashscreenTimerCallback,
0
));
if (!guard2.Get())
return false;
//TODO: refactor CFScopeGuard to return the pointer from Release.
scrollTimer = guard2.Get();
guard2.Release();
}
signalTimer = guard1.Get();
guard1.Release();
return true;
}
//_________________________________________________________________
void AttachTimers()
{
assert(signalTimer != 0 && "AttachTimer, invalid signalTimer (null)");
CFRunLoopAddTimer(CFRunLoopGetMain(), signalTimer, kCFRunLoopCommonModes);
if (showAboutInfo) {
assert(scrollTimer != 0 && "AttachTimer, invalid scrollTimer (null)");
CFRunLoopAddTimer(CFRunLoopGetMain(), scrollTimer, kCFRunLoopCommonModes);
}
}
//_________________________________________________________________
void RemoveTimers()
{
assert(signalTimer != 0 && "RemoveTimers, signalTimer is null");
CFRunLoopRemoveTimer(CFRunLoopGetMain(), signalTimer, kCFRunLoopCommonModes);
CFRunLoopTimerInvalidate(signalTimer);
//TODO: test if I also have to call release!!!
signalTimer = 0;
if (showAboutInfo) {
assert(scrollTimer != 0 && "RemoveTimers, scrollTimer is null");
CFRunLoopRemoveTimer(CFRunLoopGetMain(), scrollTimer, kCFRunLoopCommonModes);
CFRunLoopTimerInvalidate(scrollTimer);
//TODO: test if I also have to call release!!!
scrollTimer = 0;
}
}
//_________________________________________________________________
void RunEventLoop()
{
//Kind of event loop.
if (!splashScreen)
//TODO: diagnostic.
return;
if (!InitTimers())
return;
AttachTimers();
popupDone = false;
while (!popupDone) {
//Here we (possibly) suspend waiting for event.
using ROOT::MacOSX::Util::NSScopeGuard;
const NSScopeGuard<NSAutoreleasePool> pool([[NSAutoreleasePool alloc] init]);
if (NSEvent * const event = [NSApp nextEventMatchingMask : NSAnyEventMask
untilDate : [NSDate distantFuture] inMode : NSDefaultRunLoopMode dequeue : YES])
{
//Let's first check the type:
if (event.type == NSApplicationDefined) {//One of our timers 'fired'.
if (event.data1 == kSignalTimer) {
popupDone = !showAboutInfo && !StayUp() && popdown;
} else if (showAboutInfo) {
assert([[splashScreen contentView] isKindOfClass : [ROOTSplashScreenView class]] &&
"RunEventLoop, splashScreen.contentView has a wrong type");
[(ROOTSplashScreenView *)[splashScreen contentView] scrollText];
}
} else
[NSApp sendEvent : event];
}
}
RemoveTimers();
//Empty the queue (hehehe, this makes me feel ... uneasy :) ).
while ([NSApp nextEventMatchingMask : NSAnyEventMask untilDate : nil inMode : NSDefaultRunLoopMode dequeue : YES]);
}
//_________________________________________________________________
void WaitChildGeneric()
{
//If things with a waitpid thread went wrong, this function is called.
//Wait till child (i.e. ROOT) is finished. From rootx.cxx.
using ROOT::ROOTX::gChildpid;
int status = 0;
do {
while (::waitpid(gChildpid, &status, WUNTRACED) < 0) {
if (errno != EINTR)
break;
errno = 0;
}
if (WIFEXITED(status))
std::exit(WEXITSTATUS(status));
if (WIFSIGNALED(status))
std::exit(WTERMSIG(status));
if (WIFSTOPPED(status)) { // child got ctlr-Z
::raise(SIGTSTP); // stop also parent
::kill(gChildpid, SIGCONT); // if parent wakes up, wake up child
}
} while (WIFSTOPPED(status));
std::exit(0);
}
//_________________________________________________________________
void RunEventLoopInBackground()
{
using ROOT::MacOSX::Util::NSScopeGuard;
if (!InitCocoa()) {
//It's a serious bug and must be either reported or handled in a different way.
return WaitChildGeneric();
}
int status = 0;
{//Block to force a scope guard's lifetime.
const NSScopeGuard<ROOTWaitpidThread> thread([[ROOTWaitpidThread alloc] init]);
if (!thread.Get()) {
//TODO: diagnostic.
return WaitChildGeneric();
} else {
[thread.Get() start];
while (true) {
//Autorelease pool?
using ROOT::MacOSX::Util::NSScopeGuard;
const NSScopeGuard<NSAutoreleasePool> pool([[NSAutoreleasePool alloc] init]);
if (NSEvent * const event = [NSApp nextEventMatchingMask : NSAnyEventMask
untilDate : [NSDate distantFuture] inMode : NSDefaultRunLoopMode dequeue : YES])
{
if (event.type == NSApplicationDefined && event.data1 == kWaitpidThread) {
[thread.Get() cancel];
status = [thread.Get() getStatus];
break;
}
else
[NSApp sendEvent : event];
}
}
[NSApp hide : nil];//deactivate?
}
}//to force thread release.
if (WIFEXITED(status))
std::exit(WEXITSTATUS(status));
if (WIFSIGNALED(status))
std::exit(WTERMSIG(status));
}
//_________________________________________________________________
bool StayUp()
{
//Taken from rootxx.cxx.
const int splashScreenDelay = int(splashScreenDelayInSec * 1000);
timeval ctv = {};
timeval dtv = {};
timeval tv = {};
timeval ptv = popupCreationTime;
tv.tv_sec = splashScreenDelay / 1000;
tv.tv_usec = (splashScreenDelay % 1000) * 1000;
gettimeofday(&ctv, 0);
if ((dtv.tv_usec = ctv.tv_usec - ptv.tv_usec) < 0) {
dtv.tv_usec += 1000000;
ptv.tv_sec++;
}
dtv.tv_sec = ctv.tv_sec - ptv.tv_sec;
if ((ctv.tv_usec = tv.tv_usec - dtv.tv_usec) < 0) {
ctv.tv_usec += 1000000;
dtv.tv_sec++;
}
ctv.tv_sec = tv.tv_sec - dtv.tv_sec;
if (ctv.tv_sec < 0)
return false;
return true;
}
//_________________________________________________________________
bool CreateSplashscreen(bool about)
{
//Try to create NSImage out of Splash.gif, create NSPanel
//with ROOTSplashscreenView as its content view + our background image + text in a scroll view.
using ROOT::MacOSX::Util::NSScopeGuard;
//0. Text to show.
const NSScopeGuard<NSAttributedString> textToScroll(CreateTextToScroll(about));
if (!textToScroll.Get())
//Diagnostic was issued by CreateTextToScroll (TODO though).
return false;
//1. Image for splash screen's background.
#ifdef ROOTICONPATH
const std::string fileName(std::string(ROOTICONPATH) + "/Root6Splash.png");
#else
const char * const env = std::getenv("ROOTSYS");
if (!env) {
//TODO: diagnostic.
return false;
}
const std::string fileName(std::string(env) + "/icons/Root6Splash.png");
#endif
const NSScopeGuard<NSString> nsStringGuard([[NSString alloc] initWithFormat : @"%s", fileName.c_str()]);
if (!nsStringGuard.Get()) {
//TODO: diagnostic.
return false;
}
const NSScopeGuard<NSImage> imageGuard([[NSImage alloc] initWithContentsOfFile : nsStringGuard.Get()]);
if (!imageGuard.Get()) {
//TODO: diagnostic.
return false;
}
NSInteger pixelWidth = 0, pixelHeight = 0;
{//For autorelease pool.
const NSScopeGuard<NSAutoreleasePool> pool([[NSAutoreleasePool alloc] init]);
NSArray * const reps = [imageGuard.Get() representations];
for (NSImageRep * rep in reps) {
pixelWidth = rep.pixelsWide;
pixelHeight = rep.pixelsHigh;
break;
}
}
if (pixelWidth < 300 || pixelHeight < 285) {
//TODO: diagnostic.
return false;
}
//2. Splash-screen ('panel' + its content view).
NSScopeGuard<ROOTSplashScreenPanel> splashGuard([[ROOTSplashScreenPanel alloc]
initWithContentRect : NSMakeRect(0, 0, pixelWidth, pixelHeight)
styleMask : NSNonactivatingPanelMask
backing : NSBackingStoreBuffered
defer : NO]);
if (!splashGuard.Get()) {
//TODO: diagnostic.
return false;
}
const NSScopeGuard<ROOTSplashScreenView> viewGuard([[ROOTSplashScreenView alloc] initWithImage : imageGuard.Get()
text : textToScroll.Get() aboutMode : about]);
if (!viewGuard.Get()) {
//TODO: diagnostic.
return false;
}
[splashGuard.Get() setContentView : viewGuard.Get()];
[splashGuard.Get() setOpaque : NO];
[splashGuard.Get() setBackgroundColor : [NSColor clearColor]];
splashScreen = splashGuard.Get();
splashGuard.Release();
return true;
}
//_________________________________________________________________
void SetSplashscreenPosition()
{
assert(splashScreen != nil && "SetSplashscreenPosition, splashScreen is nil");
//Set the splash-screen's position (can it be wrong for a multi-head setup?)
//TODO: check with a secondary display.
if (NSScreen * const screen = [NSScreen mainScreen]) {
const NSRect screenFrame = screen.frame;
const NSSize splashSize = splashScreen.frame.size;
const NSPoint origin = NSMakePoint(screenFrame.origin.x + screenFrame.size.width / 2 - splashSize.width / 2,
screenFrame.origin.y + screenFrame.size.height / 2 - splashSize.height / 2);
[splashScreen setFrameOrigin : origin];
}//else - is it possible? TODO: diagnostic.
}
//Aux. "non-GUI" functions.
//
//Caption like "Conception"/"Core Developers"/"Contributors" - white and bold.
//
//_________________________________________________________________
bool AddCaptionAttributes(NSMutableAttributedString *textToScroll, NSRange captionRange)
{
using ROOT::MacOSX::Util::NSScopeGuard;
assert(textToScroll != nil && "AddAttributedCaption, parameter 'textToScroll' is nil");
if (!captionRange.length)
return true;
assert(captionRange.location < textToScroll.length &&
captionRange.location + captionRange.length <= textToScroll.length &&
"AddCaptionAttributes, invalid range");
NSFont * const font = [NSFont fontWithName : @"Tahoma-Bold" size : 11.];
if (!font)
//TODO: diagnostic.
return false;
const NSScopeGuard<NSMutableDictionary> dict([[NSMutableDictionary alloc] init]);
[dict.Get() setObject : font forKey : NSFontAttributeName];
NSColor * const captionColor = [NSColor colorWithDeviceRed : 176 / 255.
green : 210 / 255. blue : 249 / 255. alpha : 1.];
[dict.Get() setObject : captionColor forKey : NSForegroundColorAttributeName];
[textToScroll addAttributes : dict.Get() range : captionRange];
return true;
}
//
//Names in regular Tahoma and _slightly_ blue color.
//
//_________________________________________________________________
bool AddMainTextBodyAttributes(NSMutableAttributedString *textToScroll, NSRange attributedRange)
{
using ROOT::MacOSX::Util::NSScopeGuard;
assert(textToScroll != nil && "AddMainTextBodyAttributes, parameter 'textToScroll' is nil");
if (!attributedRange.length)
return true;
assert(attributedRange.location < textToScroll.length &&
attributedRange.location + attributedRange.length <= textToScroll.length &&
"AddMainTextBodyAttributes, invalid range");
NSFont * const font = [NSFont fontWithName : @"Tahoma" size : 11.];
if (!font)
//TODO: diagnostic.
return false;
const NSScopeGuard<NSMutableDictionary> dict([[NSMutableDictionary alloc] init]);
[dict.Get() setObject : font forKey : NSFontAttributeName];
//
NSColor * const textColor = [NSColor colorWithDeviceRed : 156 / 255.
green : 190 / 255. blue : 229 / 255. alpha : 1.];
//
[dict.Get() setObject : textColor forKey : NSForegroundColorAttributeName];
[textToScroll addAttributes : dict.Get() range : attributedRange];
return true;
}
//_________________________________________________________________
bool AddSectionTitle(NSMutableAttributedString *textToScroll, NSString *title)
{
assert(textToScroll != nil && "AddSectionTitle, paramater 'textToScroll' is nil");
assert(title != nil && "AddSectionTitle, parameter 'titlle' is nil");
if (!title.length)
return false;
using ROOT::MacOSX::Util::NSScopeGuard;
NSScopeGuard<NSAttributedString> newString([[NSAttributedString alloc] initWithString : title]);
if (!newString.Get())
return false;
const NSRange currentRange(NSMakeRange(textToScroll.length, newString.Get().length));
[textToScroll appendAttributedString : newString.Get()];
return AddCaptionAttributes(textToScroll, currentRange);
}
//_________________________________________________________________
bool AddSectionBody(NSMutableAttributedString *textToScroll, NSString *body)
{
assert(textToScroll != nil && "AddSectionBody, parameter 'textToScroll' is nil");
assert(body != nil && "AddSectionBody, parameter 'body' is nil");
if (!body.length)
return false;
using ROOT::MacOSX::Util::NSScopeGuard;
NSScopeGuard<NSAttributedString> newString([[NSAttributedString alloc] initWithString : body]);
if (!newString.Get())
//TODO: diagnostic.
return false;
const NSRange currentRange(NSMakeRange(textToScroll.length, newString.Get().length));
[textToScroll appendAttributedString : newString.Get()];
return AddMainTextBodyAttributes(textToScroll, currentRange);
}
//_________________________________________________________________
bool AddDeveloperInfo(NSMutableAttributedString *textToScroll)
{
assert(textToScroll != nil && "AddDeveloperInfo, parameter 'textToScroll' is nil");
using ROOT::MacOSX::Util::NSScopeGuard;
//'Conception:' caption.
if (!AddSectionTitle(textToScroll, @"Conception: "))
//TODO: diagnostic.
return false;
if (!AddSectionBody(textToScroll, gConception))
//TODO: diagnostic.
return false;
if (!AddSectionTitle(textToScroll, @"Core Engineering: "))
//TODO: diagnostic.
return false;
std::size_t nLines = sizeof ROOT::ROOTX::gROOTCoreTeam / sizeof ROOT::ROOTX::gROOTCoreTeam[0];
if (nLines > 1) {
nLines -= 1;//There is a "terminating null" in this array, get rid of it.
NSScopeGuard<NSMutableString> coreTeam([[NSMutableString alloc] init]);
for (std::size_t i = 0; i < nLines; ++i)
[coreTeam.Get() appendFormat : (i ? @", %s" : @"%s"), ROOT::ROOTX::gROOTCoreTeam[i]];
[coreTeam.Get() appendFormat : @".\n\n"];
if (!AddSectionBody(textToScroll, coreTeam.Get()))
return false;
} else {
//TODO: diagnostic.
return false;
}