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