]> git.sesse.net Git - vlc/blob - extras/MacOSX/Framework/Sources/VLCVideoView.m
87f3c4f3486e9f75dfffb055c999aed3ee5ed347
[vlc] / extras / MacOSX / Framework / Sources / VLCVideoView.m
1 /*****************************************************************************
2  * VLCVideoView.h: VLC.framework VLCVideoView implementation
3  *****************************************************************************
4  * Copyright (C) 2007 Pierre d'Herbemont
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "VLCVideoView.h"
26 #import "VLCLibrary.h"
27 #import "VLCEventManager.h"
28 #import "VLCVideoCommon.h"
29
30 /* Libvlc */
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc/vlc.h>
36 #include <vlc/libvlc.h>
37
38 #import <QuartzCore/QuartzCore.h>
39
40 /******************************************************************************
41  * Soon deprecated stuff 
42  */
43
44 /* This is a forward reference to VLCOpenGLVoutView specified in minimal_macosx
45    library.  We could get rid of this, but it prevents warnings from the 
46    compiler. (Scheduled to deletion) */
47 @interface VLCOpenGLVoutView : NSView
48 - (void)detachFromVout;
49 @end
50
51 /* Depreacted methods */
52 @interface VLCVideoView (Deprecated)
53 - (void)setStretchesVideo:(BOOL)value;
54 - (BOOL)stretchesVideo;
55
56 - (void)addVoutSubview:(NSView *)aView;  /* (Scheduled to deletion) */
57 - (void)removeVoutSubview:(NSView *)aView;  /* (Scheduled to deletion) */
58 @end
59
60 /******************************************************************************
61  * VLCVideoView (Private) 
62  */
63
64 @interface VLCVideoView (Private)
65 /* Method */
66 - (void)addVoutLayer:(CALayer *)aLayer;
67 @end
68
69 @interface VLCVideoView ()
70 @property (readwrite) BOOL hasVideo;
71 @end
72
73 /******************************************************************************
74  * Implementation VLCVideoView 
75  */
76
77 @implementation VLCVideoView
78
79 - (id)initWithFrame:(NSRect)rect
80 {
81     if (self = [super initWithFrame:rect]) 
82     {
83         delegate = nil;
84         [self setBackColor:[NSColor blackColor]];
85         [self setStretchesVideo:NO];
86         [self setAutoresizesSubviews:YES];
87         [self setFillScreen: NO];
88         self.hasVideo = NO;
89         layoutManager = [[VLCVideoLayoutManager layoutManager] retain];
90     }
91     return self;
92 }
93
94 - (void)dealloc
95 {
96     [layoutManager release];
97     delegate = nil;
98     [backColor release];
99     [super dealloc];
100 }
101
102 - (void)drawRect:(NSRect)aRect
103 {
104     [self lockFocus];
105     [backColor set];
106     NSRectFill(aRect);
107     [self unlockFocus];
108 }
109
110 - (BOOL)isOpaque
111 {
112     return YES;
113 }
114
115 @synthesize delegate;
116 @synthesize backColor;
117 @synthesize hasVideo;
118
119 - (BOOL)fillScreen
120 {
121     return [layoutManager fillScreenEntirely];
122 }
123
124 - (void)setFillScreen:(BOOL)fillScreen
125 {
126     [layoutManager setFillScreenEntirely:fillScreen];
127     [[self layer] setNeedsLayout];
128 }
129 @end
130
131 /******************************************************************************
132  * Implementation VLCVideoView  (Private)
133  */
134
135 @implementation VLCVideoView (Private)
136
137 /* This is called by the libvlc module 'opengllayer' as soon as there is one 
138  * vout available
139  */
140 - (void)addVoutLayer:(CALayer *)aLayer
141 {
142     [CATransaction begin];
143     [self setWantsLayer: YES];
144         CALayer * rootLayer = [self layer];
145
146     aLayer.name = @"vlcopengllayer";
147
148     [layoutManager setOriginalVideoSize:aLayer.bounds.size];
149     [rootLayer setLayoutManager:layoutManager];
150     [rootLayer insertSublayer:aLayer atIndex:0];
151
152     [aLayer setNeedsLayout];
153     [aLayer setNeedsDisplay];
154
155     [rootLayer setNeedsDisplayOnBoundsChange:YES];
156     [rootLayer setNeedsDisplay];
157     [rootLayer layoutIfNeeded];
158     [CATransaction commit];
159     self.hasVideo = YES;
160 }
161
162 - (void)removeVoutLayer:(CALayer*)voutLayer
163 {
164     [CATransaction begin];
165     [voutLayer removeFromSuperlayer];
166     [CATransaction commit];
167     self.hasVideo = NO;
168 }
169
170 @end
171
172 /******************************************************************************
173  * Implementation VLCVideoView  (Deprecated)
174  */
175
176 @implementation VLCVideoView (Deprecated)
177
178 - (void)setStretchesVideo:(BOOL)value
179 {
180     stretchesVideo = value;
181 }
182
183 - (BOOL)stretchesVideo
184 {
185     return stretchesVideo;
186 }
187
188 /* This is called by the libvlc module 'minimal_macosx' as soon as there is one 
189  * vout available
190  */
191 - (void)addVoutSubview:(NSView *)aView /* (Scheduled to deletion) */
192 {
193     /* This is where the real video comes from */
194     if( [[self subviews] count] )
195     {
196         /* XXX: This is a hack until core gets fixed */
197         int i;
198         for( i = 0; i < [[self subviews] count]; i++ )
199         {
200             [[[self subviews] objectAtIndex:i] detachFromVout];
201             [[[self subviews] objectAtIndex:i] retain];
202             [[[self subviews] objectAtIndex:i] removeFromSuperview];
203         }
204     }
205
206     [aView setFrame:[self bounds]];
207     
208     [self addSubview:aView];
209
210     // TODO: Should we let the media player specify these values?
211     [aView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
212 }
213
214 - (void)removeVoutSubview:(NSView *)view /* (Scheduled to deletion) */
215 {
216     // Should we do something?  I don't know, however the protocol requires
217     // this to be implemented
218 }
219 @end