]> git.sesse.net Git - vlc/blob - modules/gui/macosx/misc.m
* make sure that the opened media is always enqueueded, if macosx-autoplay is false
[vlc] / modules / gui / macosx / misc.m
1 /*****************************************************************************
2  * misc.m: code not specific to vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <Cocoa/Cocoa.h>
25
26 #include "intf.h"                                          /* VLCApplication */
27 #include "misc.h"
28 #include "playlist.h"
29 #include "controls.h"
30
31 /*****************************************************************************
32  * VLCControllerWindow
33  *****************************************************************************/
34
35 @implementation VLCControllerWindow
36
37 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
38     backing:(NSBackingStoreType)backingType defer:(BOOL)flag
39 {
40     self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
41     backing:backingType defer:flag];
42
43     [[VLCMain sharedInstance] updateTogglePlaylistState];
44
45     return( self );
46 }
47
48 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
49 {
50     return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
51            [(VLCControls *)[[VLCMain sharedInstance] getControls] keyEvent:o_event];
52 }
53
54 @end
55
56
57
58 /*****************************************************************************
59  * VLCControllerView
60  *****************************************************************************/
61
62 @implementation VLCControllerView
63
64 - (void)dealloc
65 {
66     [self unregisterDraggedTypes];
67     [super dealloc];
68 }
69
70 - (void)awakeFromNib
71 {
72     [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType, 
73         NSFilenamesPboardType, nil]];
74 }
75
76 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
77 {
78     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) 
79                 == NSDragOperationGeneric)
80     {
81         return NSDragOperationGeneric;
82     }
83     else
84     {
85         return NSDragOperationNone;
86     }
87 }
88
89 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
90 {
91     return YES;
92 }
93
94 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
95 {
96     NSPasteboard *o_paste = [sender draggingPasteboard];
97     NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
98     NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
99     NSData *o_carried_data = [o_paste dataForType:o_desired_type];
100
101     if( o_carried_data )
102     {
103         if ([o_desired_type isEqualToString:NSFilenamesPboardType])
104         {
105             int i;
106             NSArray *o_array = [NSArray array];
107             NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
108                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
109
110             for( i = 0; i < (int)[o_values count]; i++)
111             {
112                 NSDictionary *o_dic;
113                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
114                 o_array = [o_array arrayByAddingObject: o_dic];
115             }
116             [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
117             return YES;
118         }
119     }
120     [self setNeedsDisplay:YES];
121     return YES;
122 }
123
124 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
125 {
126     [self setNeedsDisplay:YES];
127 }
128
129 @end
130
131 /*****************************************************************************
132  * VLBrushedMetalImageView
133  *****************************************************************************/
134
135 @implementation VLBrushedMetalImageView
136
137 - (BOOL)mouseDownCanMoveWindow
138 {
139     return YES;
140 }
141
142 - (void)dealloc
143 {
144     [self unregisterDraggedTypes];
145     [super dealloc];
146 }
147
148 - (void)awakeFromNib
149 {
150     [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType, 
151         NSFilenamesPboardType, nil]];
152 }
153
154 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
155 {
156     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) 
157                 == NSDragOperationGeneric)
158     {
159         return NSDragOperationGeneric;
160     }
161     else
162     {
163         return NSDragOperationNone;
164     }
165 }
166
167 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
168 {
169     return YES;
170 }
171
172 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
173 {
174     NSPasteboard *o_paste = [sender draggingPasteboard];
175     NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
176     NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
177     NSData *o_carried_data = [o_paste dataForType:o_desired_type];
178     BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
179
180     if( o_carried_data )
181     {
182         if ([o_desired_type isEqualToString:NSFilenamesPboardType])
183         {
184             int i;
185             NSArray *o_array = [NSArray array];
186             NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
187                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
188
189             for( i = 0; i < (int)[o_values count]; i++)
190             {
191                 NSDictionary *o_dic;
192                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
193                 o_array = [o_array arrayByAddingObject: o_dic];
194             }
195             if( b_autoplay )
196                 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
197             else
198                 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:YES];
199             return YES;
200         }
201     }
202     [self setNeedsDisplay:YES];
203     return YES;
204 }
205
206 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
207 {
208     [self setNeedsDisplay:YES];
209 }
210
211 @end
212
213
214 /*****************************************************************************
215  * MPSlider
216  *****************************************************************************/
217 @implementation MPSlider
218
219 void _drawKnobInRect(NSRect knobRect)
220 {
221     // Center knob in given rect
222     knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
223     knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
224     
225     // Draw diamond
226     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
227     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
228     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
229     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
230     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
231     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
232     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
233 }
234
235 void _drawFrameInRect(NSRect frameRect)
236 {
237     // Draw frame
238     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
239     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
240     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
241     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
242 }
243
244 - (void)drawRect:(NSRect)rect
245 {
246     // Draw default to make sure the slider behaves correctly
247     [[NSGraphicsContext currentContext] saveGraphicsState];
248     NSRectClip(NSZeroRect);
249     [super drawRect:rect];
250     [[NSGraphicsContext currentContext] restoreGraphicsState];
251     
252     // Full size
253     rect = [self bounds];
254     int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
255     rect.origin.x += diff-1;
256     rect.origin.y += diff;
257     rect.size.width -= 2*diff-2;
258     rect.size.height -= 2*diff;
259     
260     // Draw dark
261     NSRect knobRect = [[self cell] knobRectFlipped:NO];
262     [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
263     _drawFrameInRect(rect);
264     _drawKnobInRect(knobRect);
265     
266     // Draw shadow
267     [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
268     rect.origin.x++;
269     rect.origin.y++;
270     knobRect.origin.x++;
271     knobRect.origin.y++;
272     _drawFrameInRect(rect);
273     _drawKnobInRect(knobRect);
274 }
275
276 @end
277
278
279 /*****************************************************************************
280  * ITSlider
281  *****************************************************************************/
282
283 @implementation ITSlider
284
285 - (void)awakeFromNib
286 {
287     if ([[self cell] class] != [ITSliderCell class]) {
288         // replace cell
289         NSSliderCell *oldCell = [self cell];
290         NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
291         [newCell setTag:[oldCell tag]];
292         [newCell setTarget:[oldCell target]];
293         [newCell setAction:[oldCell action]];
294         [newCell setControlSize:[oldCell controlSize]];
295         [newCell setType:[oldCell type]];
296         [newCell setState:[oldCell state]]; 
297         [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
298         [newCell setAltIncrementValue:[oldCell altIncrementValue]];
299         [newCell setControlTint:[oldCell controlTint]];
300         [newCell setKnobThickness:[oldCell knobThickness]];
301         [newCell setMaxValue:[oldCell maxValue]];
302         [newCell setMinValue:[oldCell minValue]];
303         [newCell setDoubleValue:[oldCell doubleValue]];
304         [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
305         [newCell setEditable:[oldCell isEditable]];
306         [newCell setEnabled:[oldCell isEnabled]];
307         [newCell setEntryType:[oldCell entryType]];
308         [newCell setHighlighted:[oldCell isHighlighted]];
309         [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
310         [self setCell:newCell];
311     }
312 }
313
314 @end
315
316 /*****************************************************************************
317  * ITSliderCell
318  *****************************************************************************/
319 @implementation ITSliderCell
320
321 - (id)init
322 {
323     self = [super init];
324     _knobOff = [[NSImage imageNamed:@"volumeslider_normal"] retain];
325     _knobOn = [[NSImage imageNamed:@"volumeslider_blue"] retain];
326     b_mouse_down = FALSE;
327     return self;
328 }
329
330 - (void)dealloc
331 {
332     [_knobOff release];
333     [_knobOn release];
334     [super dealloc];
335 }
336
337 - (void)drawKnob:(NSRect)knob_rect
338 {
339     NSImage *knob;
340
341     if( b_mouse_down )
342         knob = _knobOn;
343     else
344         knob = _knobOff;
345
346     [[self controlView] lockFocus];
347     [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
348         knob_rect.origin.y + knob_rect.size.height -2 )  
349         operation:NSCompositeSourceOver];
350     [[self controlView] unlockFocus];
351 }
352
353 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView: 
354         (NSView *)controlView mouseIsUp:(BOOL)flag
355 {
356     b_mouse_down = NO;
357     [self drawKnob];
358     [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
359 }
360
361 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
362 {
363     b_mouse_down = YES;
364     [self drawKnob];
365     return [super startTrackingAt:startPoint inView:controlView];
366 }
367
368 @end
369