forked from cappuccino/cappuccino
/
CPView.j
1823 lines (1471 loc) · 52.8 KB
/
CPView.j
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
/*
* CPView.j
* AppKit
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
import <Foundation/CPArray.j>
import <Foundation/CPObjJRuntime.j>
import "CGAffineTransform.j"
import "CGGeometry.j"
import "CPColor.j"
import "CPDOMDisplayServer.j"
import "CPGeometry.j"
import "CPResponder.j"
import "CPGraphicsContext.j"
#include "Platform/Platform.h"
#include "CoreGraphics/CGAffineTransform.h"
#include "CoreGraphics/CGGeometry.h"
#include "Platform/DOM/CPDOMDisplayServer.h"
CPViewNotSizable = 0;
CPViewMinXMargin = 1;
CPViewWidthSizable = 2;
CPViewMaxXMargin = 4;
CPViewMinYMargin = 8;
CPViewHeightSizable = 16;
CPViewMaxYMargin = 32;
CPViewBoundsDidChangeNotification = @"CPViewBoundsDidChangeNotification";
CPViewFrameDidChangeNotification = @"CPViewFrameDidChangeNotification";
var _DOMOriginUpdateMask = 1 << 0,
_DOMSizeUpdateMask = 1 << 1;
var _CPViewNotificationCenter = nil;
#if PLATFORM(DOM)
var DOMElementPrototype = nil,
BackgroundTrivialColor = 0,
BackgroundVerticalThreePartImage = 1,
BackgroundHorizontalThreePartImage = 2,
BackgroundNinePartImage = 3,
CustomDrawRectViews = {};
#endif
/*
<objj>CPView</objj> is an abstract class which provides facilities for drawing
in a window and receiving events. It is the superclass of many of the visual
elements of the GUI.</p>
<p>In order to display itself, a view must be placed in a window (represented by an
<objj>CPWindow</objj> object). Within the window is a hierarchy of <objj>CPView</objj>s,
headed by the window's content view. Every other view in a window is a descendant
of this view.</p>
<p>Subclasses can override <code>-drawRect:</code> in order to implement their
appearance. Other methods of <objj>CPView</objj> and <objj>CPResponder</objj> can
also be overridden to handle user generated events.
*/
@implementation CPView : CPResponder
{
CPWindow _window;
CPView _superview;
CPArray _subviews;
CPGraphicsContext _graphicsContext;
CGRect _frame;
CGRect _bounds;
CGAffineTransform _boundsTransform;
CGAffineTransform _inverseBoundsTransform;
CPArray _registeredDraggedTypes;
BOOL _isHidden;
BOOL _hitTests;
BOOL _postsFrameChangedNotifications;
BOOL _postsBoundsChangedNotifications;
BOOL _inhibitFrameAndBoundsChangedNotifications;
CPString _displayHash;
#if PLATFORM(DOM)
DOMElement _DOMElement;
DOMElement _DOMContentsElement;
CPArray _DOMImageParts;
CPArray _DOMImageSizes;
unsigned _backgroundType;
#endif
CGRect _dirtyRect;
float _opacity;
CPColor _backgroundColor;
BOOL _autoresizesSubviews;
unsigned _autoresizingMask;
CALayer _layer;
BOOL _wantsLayer;
// Full Screen State
BOOL _isInFullScreenMode;
_CPViewFullScreenModeState _fullScreenModeState;
}
/*
Private method for Objective-J.
@ignore
*/
+ (void)initialize
{
if ([self instanceMethodForSelector:@selector(drawRect:)] != [CPView instanceMethodForSelector:@selector(drawRect:)])
CustomDrawRectViews[[self hash]] = YES;
if (self != [CPView class])
return;
#if PLATFORM(DOM)
DOMElementPrototype = document.createElement("div");
var style = DOMElementPrototype.style;
style.overflow = "hidden";
style.position = "absolute";
style.visibility = "visible";
style.zIndex = 0;
#endif
_CPViewNotificationCenter = [CPNotificationCenter defaultCenter];
}
/*
Initializes the receiver for usage with the specified bounding rectangle
@return the initialized view
*/
- (id)initWithFrame:(CGRect)aFrame
{
self = [super init];
if (self)
{
var width = _CGRectGetWidth(aFrame),
height = _CGRectGetHeight(aFrame);
_subviews = [];
_frame = _CGRectMakeCopy(aFrame);
_bounds = _CGRectMake(0.0, 0.0, width, height);
_registeredDraggedTypes = [];
_autoresizingMask = CPViewNotSizable;
_autoresizesSubviews = YES;
_opacity = 1.0;
_isHidden = NO;
_hitTests = YES;
_displayHash = [self hash];
#if PLATFORM(DOM)
_DOMElement = DOMElementPrototype.cloneNode(false);
CPDOMDisplayServerSetStyleLeftTop(_DOMElement, NULL, _CGRectGetMinX(aFrame), _CGRectGetMinY(aFrame));
CPDOMDisplayServerSetStyleSize(_DOMElement, width, height);
_DOMImageParts = [];
_DOMImageSizes = [];
#endif
}
return self;
}
/*
Returns the container view of the receiver
@return the receiver's containing view
*/
- (CPView)superview
{
return _superview;
}
/*
Returns an array of all the views contained as direct children of the receiver
@return an array of <objj>CPView</objj>s
*/
- (CPArray)subviews
{
return _subviews;
}
/*
Returns the window containing this receiver
*/
- (CPWindow)window
{
return _window;
}
/*
Makes the argument a subview of the receiver.
@param aSubview the <objj>CPView</objj> to make a subview
*/
- (void)addSubview:(CPView)aSubview
{
[self _insertSubview:aSubview atIndex:CPNotFound];
}
/*
Makes <code>aSubview</code> a subview of the receiver. It is positioned relative to <code>anotherView</code>
@param aSubview the view to add as a subview
@param anOrderingMode specifies <code>aSubview</code>'s ordering relative to <code>anotherView</code>
@param anotherView <code>aSubview</code> will be positioned relative to this argument
*/
- (void)addSubview:(CPView)aSubview positioned:(CPWindowOrderingMode)anOrderingMode relativeTo:(CPView)anotherView
{
var index = anotherView ? [_subviews indexOfObjectIdenticalTo:anotherView] : CPNotFound;
// In other words, if no view, then either all the way at the bottom or all the way at the top.
if (index == CPNotFound)
index = (anOrderingMode == CPWindowAbove) ? [_subviews count] : 0;
// else, if we have a view, above if above.
else if (anOrderingMode == CPWindowAbove)
++index;
[self _insertSubview:aSubview atIndex:index];
}
/* @ignore */
- (void)_insertSubview:(CPView)aSubview atIndex:(int)anIndex
{
// We will have to adjust the z-index of all views starting at this index.
var count = _subviews.length;
// If this is already one of our subviews, remove it.
if (aSubview._superview == self)
{
var index = [_subviews indexOfObjectIdenticalTo:aSubview];
if (index == anIndex || index == count - 1 && anIndex == count)
return;
[_subviews removeObjectAtIndex:index];
#if PLATFORM(DOM)
CPDOMDisplayServerRemoveChild(_DOMElement, aSubview._DOMElement);
#endif
if (anIndex > index)
--anIndex;
}
else
{
// Remove the view from its previous superview.
[aSubview removeFromSuperview];
// Set the subview's window to our own.
[aSubview _setWindow:_window];
// Notify the subview that it will be moving.
[aSubview viewWillMoveToSuperview:self];
// Set ourselves as the superview.
aSubview._superview = self;
}
if (anIndex == CPNotFound || anIndex >= count)
{
_subviews.push(aSubview);
#if PLATFORM(DOM)
// Attach the actual node.
CPDOMDisplayServerAppendChild(_DOMElement, aSubview._DOMElement);
#endif
}
else
{
_subviews.splice(anIndex, 0, aSubview);
#if PLATFORM(DOM)
// Attach the actual node.
CPDOMDisplayServerInsertBefore(_DOMElement, aSubview._DOMElement, _subviews[anIndex + 1]._DOMElement);
#endif
}
[aSubview setNextResponder:self];
[aSubview viewDidMoveToSuperview];
[self didAddSubview:aSubview];
}
/*
Called when the receiver has added <code>aSubview</code> to it's child views.
@param aSubview the view that was added
*/
- (void)didAddSubview:(CPView)aSubview
{
}
/*
Removes the receiver from it's container view and window.
Does nothing if there's no container view.
*/
- (void)removeFromSuperview
{
if (!_superview)
return;
[_superview willRemoveSubview:self];
[[_superview subviews] removeObject:self];
#if PLATFORM(DOM)
CPDOMDisplayServerRemoveChild(_superview._DOMElement, _DOMElement);
#endif
_superview = nil;
[self _setWindow:nil];
}
/*
Replaces the specified child view with another view
@param aSubview the view to replace
@param aView the replacement view
*/
- (void)replaceSubview:(CPView)aSubview with:(CPView)aView
{
if (aSubview._superview != self)
return;
var index = [_subviews indexOfObjectIdenticalTo:aSubview];
[aSubview removeFromSuperview];
[aView _insertSubview:aView atIndex:index];
}
/* @ignore */
- (void)_setWindow:(CPWindow)aWindow
{
// FIXME: check _window == aWindow? If not, comment why!
if ([_window firstResponder] == self)
[_window makeFirstResponder:nil];
// Notify the view and its subviews
[self viewWillMoveToWindow:aWindow];
[_subviews makeObjectsPerformSelector:@selector(_setWindow:) withObject:aWindow];
_window = aWindow;
[self viewDidMoveToWindow];
}
/*
Returns <code>YES</code> if the receiver is, or is a descendant of, <code>aView</code>.
@param aView the view to test for ancestry
*/
- (BOOL)isDescendantOf:(CPView)aView
{
var view = self;
do
{
if (view == aView)
return YES;
} while(view = [view superview])
return NO;
}
/*
Called when the receiver's superview has changed.
*/
- (void)viewDidMoveToSuperview
{
// if (_graphicsContext)
[self setNeedsDisplay:YES];
}
/*
Called when the receiver has been moved to a new <objj>CPWindow</objj>.
*/
- (void)viewDidMoveToWindow
{
}
/*
Called when the receiver is about to be moved to a new view.
@param aView the view to which the receiver will be moved
*/
- (void)viewWillMoveToSuperview:(CPView)aView
{
}
/*
Called when the receiver is about to be moved to a new window.
@param aWindow the window to which the receiver will be moved.
*/
- (void)viewWillMoveToWindow:(CPWindow)aWindow
{
}
/*
Called when the receiver is about to be remove one of its subviews.
@param aView the view that will be removed
*/
- (void)willRemoveSubview:(CPView)aView
{
}
/*
Returns the menu item containing the receiver or one of its ancestor views.
@return a menu item, or <code>nil</code> if the view or one of its ancestors wasn't found
*/
- (CPMenuItem)enclosingMenuItem
{
var view = self;
while (![view isKindOfClass:[_CPMenuItemView class]])
view = [view superview];
if (view)
return view._menuItem;
return nil;
/* var view = self,
enclosingMenuItem = _enclosingMenuItem;
while (!enclosingMenuItem && (view = view._enclosingMenuItem))
view = [view superview];
return enclosingMenuItem;*/
}
/*
Returns whether the view is flipped.
@return <code>YES</code> if the view is flipped. <code>NO</code>, otherwise.
*/
- (BOOL)isFlipped
{
return YES;
}
/*
Sets the frame size of the receiver to the dimensions and origin of the provided rectangle in the coordinate system
of the superview. The method also posts an <objj>CPViewFrameDidChangeNotification</objj> to the notification
center if the receiver is configured to do so. If the frame is the same as the current frame, the method simply
returns (and no notificaion is posted).
@param aFrame the rectangle specifying the new origin and size of the receiver
*/
- (void)setFrame:(CGRect)aFrame
{
if (_CGRectEqualToRect(_frame, aFrame))
return;
_inhibitFrameAndBoundsChangedNotifications = YES;
[self setFrameOrigin:aFrame.origin];
[self setFrameSize:aFrame.size];
_inhibitFrameAndBoundsChangedNotifications = NO;
if (_postsFrameChangedNotifications)
[_CPViewNotificationCenter postNotificationName:CPViewFrameDidChangeNotification object:self];
}
/*
Returns the receiver's frame.
@return a copy of the receiver's frame
*/
- (CGRect)frame
{
return _CGRectMakeCopy(_frame);
}
/*
Sets the receiver's frame origin to the provided point. The point is defined in the superview's coordinate system.
The method posts a <objj>CPViewFrameDidChangeNotification</objj> to the default notification center if the receiver
is configured to do so. If the specified origin is the same as the frame's current origin, the method will
simply return (and no notification will be posted).
@param aPoint the new origin point
*/
- (void)setFrameOrigin:(CGPoint)aPoint
{
var origin = _frame.origin;
if (!aPoint || _CGPointEqualToPoint(origin, aPoint))
return;
origin.x = aPoint.x;
origin.y = aPoint.y;
if (_postsFrameChangedNotifications && !_inhibitFrameAndBoundsChangedNotifications)
[_CPViewNotificationCenter postNotificationName:CPViewFrameDidChangeNotification object:self];
#if PLATFORM(DOM)
CPDOMDisplayServerSetStyleLeftTop(_DOMElement, _superview ? _superview._boundsTransform : NULL, origin.x, origin.y);
#endif
}
/*
Sets the receiver's frame size. If <code>aSize</code> is the same as the frame's current dimensions, this
method simply returns. The method posts a <objj>CPViewFrameDidChangeNotification</objj> to the
default notification center if the receiver is configured to do so.
@param aSize the new size for the frame
*/
- (void)setFrameSize:(CGSize)aSize
{
var size = _frame.size;
if (!aSize || _CGSizeEqualToSize(size, aSize))
return;
var oldSize = _CGSizeMakeCopy(size);
size.width = aSize.width;
size.height = aSize.height;
if (YES)
{
_bounds.size.width = aSize.width;
_bounds.size.height = aSize.height;
}
if (_postsFrameChangedNotifications && !_inhibitFrameAndBoundsChangedNotifications)
[_CPViewNotificationCenter postNotificationName:CPViewFrameDidChangeNotification object:self];
if (_layer)
[_layer _owningViewBoundsChanged];
if (_autoresizesSubviews)
[self resizeSubviewsWithOldSize:oldSize];
[self setNeedsDisplay:YES];
#if PLATFORM(DOM)
CPDOMDisplayServerSetStyleSize(_DOMElement, size.width, size.height);
if (_DOMContentsElement)
{
CPDOMDisplayServerSetSize(_DOMContentsElement, size.width, size.height);
CPDOMDisplayServerSetStyleSize(_DOMContentsElement, size.width, size.height);
}
if (_backgroundType == BackgroundTrivialColor)
return;
var images = [[_backgroundColor patternImage] imageSlices];
if (_backgroundType == BackgroundVerticalThreePartImage)
{
CPDOMDisplayServerSetStyleSize(_DOMImageParts[1], size.width, size.height - _DOMImageSizes[0].height - _DOMImageSizes[2].height);
}
else if (_backgroundType == BackgroundHorizontalThreePartImage)
{
CPDOMDisplayServerSetStyleSize(_DOMImageParts[1], size.width - _DOMImageSizes[0].width - _DOMImageSizes[2].width, size.height);
}
else if (_backgroundType == BackgroundNinePartImage)
{
var width = size.width - _DOMImageSizes[0].width - _DOMImageSizes[2].width,
height = size.height - _DOMImageSizes[0].height - _DOMImageSizes[6].height;
CPDOMDisplayServerSetStyleSize(_DOMImageParts[1], width, _DOMImageSizes[0].height);
CPDOMDisplayServerSetStyleSize(_DOMImageParts[3], _DOMImageSizes[3].width, height);
CPDOMDisplayServerSetStyleSize(_DOMImageParts[4], width, height);
CPDOMDisplayServerSetStyleSize(_DOMImageParts[5], _DOMImageSizes[5].width, height);
CPDOMDisplayServerSetStyleSize(_DOMImageParts[7], width, _DOMImageSizes[7].height);
}
#endif
}
/*
Sets the receiver's bounds. The bounds define the size and location of the receiver inside it's frame. Posts a
<objj>CPViewBoundsDidChangeNotification</objj> to the default notification center if the receiver is configured to do so.
@param bounds the new bounds
*/
- (void)setBounds:(CGRect)bounds
{
if (_CGRectEqualToRect(_bounds, bounds))
return;
_inhibitFrameAndBoundsChangedNotifications = YES;
[self setBoundsOrigin:bounds.origin];
[self setBoundsSize:bounds.size];
_inhibitFrameAndBoundsChangedNotifications = NO;
if (_postsBoundsChangedNotifications)
[_CPViewNotificationCenter postNotificationName:CPViewBoundsDidChangeNotification object:self];
}
/*
Returns the receiver's bounds. The bounds define the size
and location of the receiver inside its frame.
*/
- (CGRect)bounds
{
return _CGRectMakeCopy(_bounds);
}
/*
Sets the location of the receiver inside its frame. The method
posts a <objj>CPViewBoundsDidChangeNotification</objj> to the
default notification center if the receiver is configured to do so.
@param aPoint the new location for the receiver
*/
- (void)setBoundsOrigin:(CGPoint)aPoint
{
var origin = _bounds.origin;
if (_CGPointEqualToPoint(origin, aPoint))
return;
origin.x = aPoint.x;
origin.y = aPoint.y;
if (origin.x != 0 || origin.y != 0)
{
_boundsTransform = _CGAffineTransformMakeTranslation(-origin.x, -origin.y);
_inverseBoundsTransform = CGAffineTransformInvert(_boundsTransform);
}
else
{
_boundsTransform = nil;
_inverseBoundsTransform = nil;
}
#if PLATFORM(DOM)
var index = _subviews.length;
while (index--)
{
var view = _subviews[index],
origin = view._frame.origin;
CPDOMDisplayServerSetStyleLeftTop(view._DOMElement, _boundsTransform, origin.x, origin.y);
}
#endif
if (_postsBoundsChangedNotifications && !_inhibitFrameAndBoundsChangedNotifications)
[_CPViewNotificationCenter postNotificationName:CPViewBoundsDidChangeNotification object:self];
}
/*
Sets the receiver's size inside its frame. The method posts a
<objj>CPViewBoundsDidChangeNotification</objj> to the default
notification center if the receiver is configured to do so.
@param aSize the new size for the receiver
*/
- (void)setBoundsSize:(CGSize)aSize
{
var size = _bounds.size;
if (_CGSizeEqualToSize(size, aSize))
return;
var frameSize = _frame.size;
if (!_CGSizeEqualToSize(size, frameSize))
{
var origin = _bounds.origin;
origin.x /= size.width / frameSize.width;
origin.y /= size.height / frameSize.height;
}
size.width = aSize.width;
size.height = aSize.height;
if (!_CGSizeEqualToSize(size, frameSize))
{
var origin = _bounds.origin;
origin.x *= size.width / frameSize.width;
origin.y *= size.height / frameSize.height;
}
if (_postsBoundsChangedNotifications && !_inhibitFrameAndBoundsChangedNotifications)
[_CPViewNotificationCenter postNotificationName:CPViewBoundsDidChangeNotification object:self];
}
/*
Notifies subviews that the superview changed size.
@param aSize the size of the old superview
*/
- (void)resizeWithOldSuperviewSize:(CGSize)aSize
{
var mask = _autoresizingMask;
if(mask == CPViewNotSizable)
return;
var frame = _superview._frame,
newFrame = _CGRectMakeCopy(_frame),
dX = (_CGRectGetWidth(frame) - aSize.width) /
(((mask & CPViewMinXMargin) ? 1 : 0) + (mask & CPViewWidthSizable ? 1 : 0) + (mask & CPViewMaxXMargin ? 1 : 0)),
dY = (_CGRectGetHeight(frame) - aSize.height) /
((mask & CPViewMinYMargin ? 1 : 0) + (mask & CPViewHeightSizable ? 1 : 0) + (mask & CPViewMaxYMargin ? 1 : 0));
if (mask & CPViewMinXMargin)
newFrame.origin.x += dX;
if (mask & CPViewWidthSizable)
newFrame.size.width += dX;
if (mask & CPViewMinYMargin)
newFrame.origin.y += dY;
if (mask & CPViewHeightSizable)
newFrame.size.height += dY;
[self setFrame:newFrame];
}
/*
Initiates <code>superviewSizeChanged:</code> messages to subviews.
@param aSize the size for the subviews
*/
- (void)resizeSubviewsWithOldSize:(CGSize)aSize
{
var count = _subviews.length;
while (count--)
[_subviews[count] resizeWithOldSuperviewSize:aSize];
}
/*
Specifies whether the receiver view should automatically resize its
subviews when its <code>setFrameSize:</code> method receives a change.
@param aFlag If <code>YES</code>, then subviews will automatically be resized
when this view is resized. <code>NO</code> means the views will not
be resized automatically.
*/
- (void)setAutoresizesSubviews:(BOOL)aFlag
{
_autoresizesSubviews = aFlag;
}
/*
Reports whether the receiver automatically resizes its subviews when its frame size changes.
@return <code>YES</code> means it resizes its subviews on a frame size change.
*/
- (BOOL)autoresizesSubviews
{
return _autoresizesSubviews;
}
/*
Determines automatic resizing behavior.
@param aMask a bit mask with options
*/
- (void)setAutoresizingMask:(unsigned)aMask
{
_autoresizingMask = aMask;
}
/*
Returns the bit mask options for resizing behavior
*/
- (unsigned)autoresizingMask
{
return _autoresizingMask;
}
// Fullscreen Mode
/*
Puts the receiver into full screen mode.
@param aScreen the that should be used
@param options configuration options
*/
- (BOOL)enterFullScreenMode:(CPScreen)aScreen withOptions:(CPDictionary)options
{
_fullScreenModeState = _CPViewFullScreenModeStateMake(self);
var fullScreenWindow = [[CPWindow alloc] initWithContentRect:[[CPDOMWindowBridge sharedDOMWindowBridge] contentBounds] styleMask:CPBorderlessWindowMask];
[fullScreenWindow setLevel:CPScreenSaverWindowLevel];
[fullScreenWindow setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
var contentView = [fullScreenWindow contentView];
[contentView setBackgroundColor:[CPColor blackColor]];
[contentView addSubview:self];
[self setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
[self setFrame:CGRectMakeCopy([contentView bounds])];
[fullScreenWindow makeKeyAndOrderFront:self];
[fullScreenWindow makeFirstResponder:self];
_isInFullScreenMode = YES;
return YES;
}
/*
The receiver should exit full screen mode.
@param options configurations options
*/
- (void)exitFullScreenModeWithOptions:(CPDictionary)options
{
if (!_isInFullScreenMode)
return;
_isInFullScreenMode = NO;
[self setFrame:_fullScreenModeState.frame];
[self setAutoresizingMask:_fullScreenModeState.autoresizingMask];
[_fullScreenModeState.superview _insertSubview:self atIndex:_fullScreenModeState.index];
[[self window] orderOut:self];
}
/*
Returns <code>YES</code> if the receiver is currently in full screen mode.
*/
- (BOOL)isInFullScreenMode
{
return _isInFullScreenMode;
}
/*
Sets whether the receiver should be hidden.
@param aFlag <code>YES</code> makes the receiver hidden.
*/
- (void)setHidden:(BOOL)aFlag
{
if(_isHidden == aFlag)
return;
// FIXME: Should we return to visibility? This breaks in FireFox, Opera, and IE.
// _DOMElement.style.visibility = (_isHidden = aFlag) ? "hidden" : "visible";
_isHidden = aFlag;
#if PLATFORM(DOM)
_DOMElement.style.display = _isHidden ? "none" : "block";
#endif
}
/*
Returns <code>YES</code> if the receiver is hidden.
*/
- (BOOL)isHidden
{
return _isHidden;
}
/*
Sets the opacity of the receiver. The value must be in the range of 0.0 to 1.0, where 0.0 is
completely transparent and 1.0 is completely opaque.
@param anAlphaValue an alpha value ranging from 0.0 to 1.0.
*/
- (void)setAlphaValue:(float)anAlphaValue
{
if (_opacity == anAlphaValue)
return;
_opacity = anAlphaValue;
#if PLATFORM(DOM)
_DOMElement.style.opacity = anAlphaValue;
if (anAlphaValue == 1.0)
try { _DOMElement.style.removeAttribute("filter") } catch (anException) { }
else
_DOMElement.style.filter = "alpha(opacity=" + anAlphaValue * 100 + ")";
#endif
}
/*
Returns the alpha value of the receiver. Ranges from 0.0 to
1.0, where 0.0 is completely transparent and 1.0 is completely opaque.
*/
- (float)alphaValue
{
return _opacity;
}
/*
Returns <code>YES</code> if the receiver is hidden, or one
of it's ancestor views is hidden. <code>NO</code>, otherwise.
*/
- (BOOL)isHiddenOrHasHiddenAncestor
{
var view = self;
while (![view isHidden])
view = [view superview];
return view != nil;
}
/*
Returns whether the receiver should be sent a <code>mouseDown:</code> message for <code>anEvent</code>.<br/>
Returns <code>YES</code> by default.
@return <code>YES</code>, if the view object accepts first mouse-down event. <code>NO</code>, otherwise.
*/
//FIXME: should be NO by default?
- (BOOL)acceptsFirstMouse:(CPEvent)anEvent
{
return YES;
}
/*
@ignore
*/
- (BOOL)hitTests
{
return YES;
}
/*
@ignore
*/
- (void)setHitTests:(BOOL)shouldHitTest
{
_hitTests = shouldHitTest;
}
/*
@ignore
*/
- (CPView)hitTest:(CPPoint)aPoint
{
if(_isHidden || !_hitTests || !CPRectContainsPoint(_frame, aPoint))
return nil;
var view = nil,
i = _subviews.length,
adjustedPoint = _CGPointMake(aPoint.x - _CGRectGetMinX(_frame), aPoint.y - _CGRectGetMinY(_frame));
if (_inverseBoundsTransform)
adjustedPoint = _CGPointApplyAffineTransform(adjustedPoint, _inverseBoundsTransform);
while (i--)
if (view = [_subviews[i] hitTest:adjustedPoint])
return view;
return self;
}
/*
Returns <code>YES</code> if mouse events aren't needed by the receiver and can be sent to the superview. The
default implementation returns <code>NO</code> if the view is opaque.
*/
- (BOOL)mouseDownCanMoveWindow
{
return ![self isOpaque];
}
- (void)mouseDown:(CPEvent)anEvent
{
if ([self mouseDownCanMoveWindow])
[super mouseDown:anEvent];
}
/*
Sets the background color of the receiver.
@param aColor the new color for the receiver's background
*/
- (void)setBackgroundColor:(CPColor)aColor
{
if (_backgroundColor == aColor)
return;
_backgroundColor = aColor;
#if PLATFORM(DOM)
var patternImage = [_backgroundColor patternImage],