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