]> git.sesse.net Git - vlc/blob - modules/gui/macosx/misc.m
* Patch from Basil Achermann to handle esc and space keyboard events in VLCControl...
[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
179     if( o_carried_data )
180     {
181         if ([o_desired_type isEqualToString:NSFilenamesPboardType])
182         {
183             int i;
184             NSArray *o_array = [NSArray array];
185             NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
186                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
187
188             for( i = 0; i < (int)[o_values count]; i++)
189             {
190                 NSDictionary *o_dic;
191                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
192                 o_array = [o_array arrayByAddingObject: o_dic];
193             }
194             [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
195             return YES;
196         }
197     }
198     [self setNeedsDisplay:YES];
199     return YES;
200 }
201
202 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
203 {
204     [self setNeedsDisplay:YES];
205 }
206
207 @end
208
209
210 /*****************************************************************************
211  * MPSlider
212  *****************************************************************************/
213 @implementation MPSlider
214
215 void _drawKnobInRect(NSRect knobRect)
216 {
217     // Center knob in given rect
218     knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
219     knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
220     
221     // Draw diamond
222     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
223     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
224     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
225     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
226     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
227     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
228     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
229 }
230
231 void _drawFrameInRect(NSRect frameRect)
232 {
233     // Draw frame
234     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
235     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
236     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
237     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
238 }
239
240 - (void)drawRect:(NSRect)rect
241 {
242     // Draw default to make sure the slider behaves correctly
243     [[NSGraphicsContext currentContext] saveGraphicsState];
244     NSRectClip(NSZeroRect);
245     [super drawRect:rect];
246     [[NSGraphicsContext currentContext] restoreGraphicsState];
247     
248     // Full size
249     rect = [self bounds];
250     int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
251     rect.origin.x += diff-1;
252     rect.origin.y += diff;
253     rect.size.width -= 2*diff-2;
254     rect.size.height -= 2*diff;
255     
256     // Draw dark
257     NSRect knobRect = [[self cell] knobRectFlipped:NO];
258     [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
259     _drawFrameInRect(rect);
260     _drawKnobInRect(knobRect);
261     
262     // Draw shadow
263     [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
264     rect.origin.x++;
265     rect.origin.y++;
266     knobRect.origin.x++;
267     knobRect.origin.y++;
268     _drawFrameInRect(rect);
269     _drawKnobInRect(knobRect);
270 }
271
272 @end
273
274
275 /*****************************************************************************
276  * ITSlider
277  *****************************************************************************/
278
279 @implementation ITSlider
280
281 - (void)awakeFromNib
282 {
283     if ([[self cell] class] != [ITSliderCell class]) {
284         // replace cell
285         NSSliderCell *oldCell = [self cell];
286         NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
287         [newCell setTag:[oldCell tag]];
288         [newCell setTarget:[oldCell target]];
289         [newCell setAction:[oldCell action]];
290         [newCell setControlSize:[oldCell controlSize]];
291         [newCell setType:[oldCell type]];
292         [newCell setState:[oldCell state]]; 
293         [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
294         [newCell setAltIncrementValue:[oldCell altIncrementValue]];
295         [newCell setControlTint:[oldCell controlTint]];
296         [newCell setKnobThickness:[oldCell knobThickness]];
297         [newCell setMaxValue:[oldCell maxValue]];
298         [newCell setMinValue:[oldCell minValue]];
299         [newCell setDoubleValue:[oldCell doubleValue]];
300         [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
301         [newCell setEditable:[oldCell isEditable]];
302         [newCell setEnabled:[oldCell isEnabled]];
303         [newCell setEntryType:[oldCell entryType]];
304         [newCell setHighlighted:[oldCell isHighlighted]];
305         [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
306         [self setCell:newCell];
307     }
308 }
309
310 @end
311
312 /*****************************************************************************
313  * ITSliderCell
314  *****************************************************************************/
315 @implementation ITSliderCell
316
317 - (id)init
318 {
319     self = [super init];
320     _knobOff = [[NSImage imageNamed:@"volumeslider_normal"] retain];
321     _knobOn = [[NSImage imageNamed:@"volumeslider_blue"] retain];
322     b_mouse_down = FALSE;
323     return self;
324 }
325
326 - (void)dealloc
327 {
328     [_knobOff release];
329     [_knobOn release];
330     [super dealloc];
331 }
332
333 - (void)drawKnob:(NSRect)knob_rect
334 {
335     NSImage *knob;
336
337     if( b_mouse_down )
338         knob = _knobOn;
339     else
340         knob = _knobOff;
341
342     [[self controlView] lockFocus];
343     [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
344         knob_rect.origin.y + knob_rect.size.height -2 )  
345         operation:NSCompositeSourceOver];
346     [[self controlView] unlockFocus];
347 }
348
349 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView: 
350         (NSView *)controlView mouseIsUp:(BOOL)flag
351 {
352     b_mouse_down = NO;
353     [self drawKnob];
354     [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
355 }
356
357 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
358 {
359     b_mouse_down = YES;
360     [self drawKnob];
361     return [super startTrackingAt:startPoint inView:controlView];
362 }
363
364 @end
365