]> git.sesse.net Git - vlc/blob - modules/gui/macosx/misc.m
* iTunes style slider
[vlc] / modules / gui / macosx / misc.m
1 /*****************************************************************************
2  * misc.m: code not specific to vlc
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <Cocoa/Cocoa.h>
25
26 #include "intf.h"                                          /* VLCApplication */
27 #include "misc.h"
28 #include "playlist.h"
29
30 /*****************************************************************************
31  * VLCControllerWindow
32  *****************************************************************************/
33
34 @implementation VLCControllerWindow
35
36 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
37     backing:(NSBackingStoreType)backingType defer:(BOOL)flag
38 {
39     self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
40     backing:backingType defer:flag];
41
42     return( self );
43 }
44
45 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
46 {
47     return [( (VLCApplication *) [VLCApplication sharedApplication] )
48             hasDefinedShortcutKey:o_event];
49 }
50
51 @end
52
53
54
55 /*****************************************************************************
56  * VLCControllerView
57  *****************************************************************************/
58
59 @implementation VLCControllerView
60
61 - (void)dealloc
62 {
63     [self unregisterDraggedTypes];
64     [super dealloc];
65 }
66
67 - (void)awakeFromNib
68 {
69     [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType, 
70         NSFilenamesPboardType, nil]];
71 }
72
73 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
74 {
75     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) 
76                 == NSDragOperationGeneric)
77     {
78         return NSDragOperationGeneric;
79     }
80     else
81     {
82         return NSDragOperationNone;
83     }
84 }
85
86 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
87 {
88     return YES;
89 }
90
91 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
92 {
93     NSPasteboard *o_paste = [sender draggingPasteboard];
94     NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
95     NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
96     NSData *o_carried_data = [o_paste dataForType:o_desired_type];
97
98     if( o_carried_data )
99     {
100         if ([o_desired_type isEqualToString:NSFilenamesPboardType])
101         {
102             int i;
103             NSArray *o_array = [NSArray array];
104             NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
105                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
106
107             for( i = 0; i < (int)[o_values count]; i++)
108             {
109                 NSDictionary *o_dic;
110                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
111                 o_array = [o_array arrayByAddingObject: o_dic];
112             }
113             [(VLCPlaylist *)[[NSApp delegate] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
114             return YES;
115         }
116     }
117     [self setNeedsDisplay:YES];
118     return YES;
119 }
120
121 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
122 {
123     [self setNeedsDisplay:YES];
124 }
125
126 @end
127
128 /*****************************************************************************
129  * VLBrushedMetalImageView
130  *****************************************************************************/
131
132 @implementation VLBrushedMetalImageView
133
134 - (BOOL)mouseDownCanMoveWindow
135 {
136     return YES;
137 }
138
139 - (void)dealloc
140 {
141     [self unregisterDraggedTypes];
142     [super dealloc];
143 }
144
145 - (void)awakeFromNib
146 {
147     [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType, 
148         NSFilenamesPboardType, nil]];
149 }
150
151 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
152 {
153     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) 
154                 == NSDragOperationGeneric)
155     {
156         return NSDragOperationGeneric;
157     }
158     else
159     {
160         return NSDragOperationNone;
161     }
162 }
163
164 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
165 {
166     return YES;
167 }
168
169 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
170 {
171     NSPasteboard *o_paste = [sender draggingPasteboard];
172     NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
173     NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
174     NSData *o_carried_data = [o_paste dataForType:o_desired_type];
175
176     if( o_carried_data )
177     {
178         if ([o_desired_type isEqualToString:NSFilenamesPboardType])
179         {
180             int i;
181             NSArray *o_array = [NSArray array];
182             NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
183                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
184
185             for( i = 0; i < (int)[o_values count]; i++)
186             {
187                 NSDictionary *o_dic;
188                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
189                 o_array = [o_array arrayByAddingObject: o_dic];
190             }
191             [(VLCPlaylist *)[[NSApp delegate] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
192             return YES;
193         }
194     }
195     [self setNeedsDisplay:YES];
196     return YES;
197 }
198
199 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
200 {
201     [self setNeedsDisplay:YES];
202 }
203
204 @end
205
206
207 /*****************************************************************************
208  * MPSlider
209  *****************************************************************************/
210 @implementation MPSlider
211
212 void _drawKnobInRect(NSRect knobRect)
213 {
214     // Center knob in given rect
215     knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
216     knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
217     
218     // Draw diamond
219     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
220     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
221     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
222     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
223     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
224     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
225     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
226 }
227
228 void _drawFrameInRect(NSRect frameRect)
229 {
230     // Draw frame
231     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
232     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
233     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
234     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
235 }
236
237 - (void)drawRect:(NSRect)rect
238 {
239     // Draw default to make sure the slider behaves correctly
240     [[NSGraphicsContext currentContext] saveGraphicsState];
241     NSRectClip(NSZeroRect);
242     [super drawRect:rect];
243     [[NSGraphicsContext currentContext] restoreGraphicsState];
244     
245     // Full size
246     rect = [self bounds];
247     int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
248     rect.origin.x += diff-1;
249     rect.origin.y += diff;
250     rect.size.width -= 2*diff-2;
251     rect.size.height -= 2*diff;
252     
253     // Draw dark
254     NSRect knobRect = [[self cell] knobRectFlipped:NO];
255     [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
256     _drawFrameInRect(rect);
257     _drawKnobInRect(knobRect);
258     
259     // Draw shadow
260     [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
261     rect.origin.x++;
262     rect.origin.y++;
263     knobRect.origin.x++;
264     knobRect.origin.y++;
265     _drawFrameInRect(rect);
266     _drawKnobInRect(knobRect);
267 }
268
269 @end