]> git.sesse.net Git - vlc/blobdiff - modules/gui/macosx/misc.m
* finally use the iTunes volumeslider knob that we have had since 0.7.0 but which...
[vlc] / modules / gui / macosx / misc.m
index 4bc23d6b9e725b7cf34ff2fb461f2b48c97b55ca..dec9054f54554347fb7650f38949b4dcbc9b4053 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * misc.m: code not specific to vlc
  *****************************************************************************
- * Copyright (C) 2003 VideoLAN
- * $Id: misc.m,v 1.1 2003/01/21 00:47:43 jlj Exp $
+ * Copyright (C) 2003-2005 the VideoLAN team
+ * $Id$
  *
  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
  *
 
 #include <Cocoa/Cocoa.h>
 
+#include "intf.h"                                          /* VLCApplication */
 #include "misc.h"
+#include "playlist.h"
 
 /*****************************************************************************
- * MPSlider
+ * VLCControllerWindow
  *****************************************************************************/
 
-@implementation MPSlider
+@implementation VLCControllerWindow
 
-+ (Class)cellClass
+- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
+    backing:(NSBackingStoreType)backingType defer:(BOOL)flag
 {
-    return( [MPSliderCell class] );
+    self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
+    backing:backingType defer:flag];
+
+    [[VLCMain sharedInstance] updateTogglePlaylistState];
+
+    return( self );
+}
+
+- (BOOL)performKeyEquivalent:(NSEvent *)o_event
+{
+    return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
 }
 
 @end
 
+
+
 /*****************************************************************************
- * MPSliderCell
+ * VLCControllerView
  *****************************************************************************/
 
-@implementation MPSliderCell
+@implementation VLCControllerView
 
-- (id)init
+- (void)dealloc
 {
-    self = [super init];
-
-    if( self != nil )
-    {
-        _bgColor = [[NSColor colorWithDeviceRed: 0.8627451
-                                          green: 0.8784314
-                                           blue: 0.7725490
-                                          alpha: 1.0] retain];
-        _knobColor = [[NSColor blackColor] retain];
-    }
-
-    return( self );
+    [self unregisterDraggedTypes];
+    [super dealloc];
 }
 
-- (void)dealloc
+- (void)awakeFromNib
 {
-    [_bgColor release];
-    [_knobColor release];
-    [super dealloc];
+    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType, 
+        NSFilenamesPboardType, nil]];
 }
 
-- (void)setBackgroundColor:(NSColor *)newColor
+- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
 {
-    [_bgColor release];  
-    _bgColor = [newColor retain];
+    if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) 
+                == NSDragOperationGeneric)
+    {
+        return NSDragOperationGeneric;
+    }
+    else
+    {
+        return NSDragOperationNone;
+    }
 }
 
-- (NSColor *)backgroundColor
+- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
 {
-    return( _bgColor );
+    return YES;
 }
 
-- (void)setKnobColor:(NSColor *)newColor
+- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
 {
-    [_knobColor release];  
-    _knobColor = [newColor retain];
+    NSPasteboard *o_paste = [sender draggingPasteboard];
+    NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
+    NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
+    NSData *o_carried_data = [o_paste dataForType:o_desired_type];
+
+    if( o_carried_data )
+    {
+        if ([o_desired_type isEqualToString:NSFilenamesPboardType])
+        {
+            int i;
+            NSArray *o_array = [NSArray array];
+            NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
+                        sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
+
+            for( i = 0; i < (int)[o_values count]; i++)
+            {
+                NSDictionary *o_dic;
+                o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
+                o_array = [o_array arrayByAddingObject: o_dic];
+            }
+            [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
+            return YES;
+        }
+    }
+    [self setNeedsDisplay:YES];
+    return YES;
 }
 
-- (NSColor *)knobColor
+- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
 {
-    return( _knobColor );
+    [self setNeedsDisplay:YES];
 }
 
-- (void)setKnobThickness:(float)f_value
+@end
+
+/*****************************************************************************
+ * VLBrushedMetalImageView
+ *****************************************************************************/
+
+@implementation VLBrushedMetalImageView
+
+- (BOOL)mouseDownCanMoveWindow
 {
-    _knobThickness = f_value;
+    return YES;
 }
 
-- (float)knobThickness
+- (void)dealloc
 {
-    return( _knobThickness );
+    [self unregisterDraggedTypes];
+    [super dealloc];
 }
 
-- (NSSize)cellSizeForBounds:(NSRect)s_rc
+- (void)awakeFromNib
 {
-    return( s_rc.size );
+    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType, 
+        NSFilenamesPboardType, nil]];
 }
 
-- (void)drawWithFrame:(NSRect)s_rc inView:(NSView *)o_view
+- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
 {
-    if( _scFlags.weAreVertical )
+    if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) 
+                == NSDragOperationGeneric)
     {
-        s_rc.origin.x = 1; s_rc.size.width -= 3;
-        s_rc.origin.y = 2; s_rc.size.height -= 5;    
+        return NSDragOperationGeneric;
     }
     else
     {
-        s_rc.origin.x = 2; s_rc.size.width -= 5;
-        s_rc.origin.y = 1; s_rc.size.height -= 3;
+        return NSDragOperationNone;
     }
+}
+
+- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
+{
+    return YES;
+}
+
+- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
+{
+    NSPasteboard *o_paste = [sender draggingPasteboard];
+    NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
+    NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
+    NSData *o_carried_data = [o_paste dataForType:o_desired_type];
 
-    [super drawWithFrame: s_rc inView: o_view]; 
+    if( o_carried_data )
+    {
+        if ([o_desired_type isEqualToString:NSFilenamesPboardType])
+        {
+            int i;
+            NSArray *o_array = [NSArray array];
+            NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
+                        sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
+
+            for( i = 0; i < (int)[o_values count]; i++)
+            {
+                NSDictionary *o_dic;
+                o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
+                o_array = [o_array arrayByAddingObject: o_dic];
+            }
+            [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
+            return YES;
+        }
+    }
+    [self setNeedsDisplay:YES];
+    return YES;
 }
 
-- (void)drawBarInside:(NSRect)s_rc flipped:(BOOL)b_flipped
+- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
 {
-    NSRect s_arc;
-    s_rc.size.width += (s_rc.origin.x * 2) + 1;
-    s_rc.size.height += (s_rc.origin.y * 2) + 1;
-    s_rc.origin.x = s_rc.origin.y = 0;
+    [self setNeedsDisplay:YES];
+}
 
-    [[NSGraphicsContext currentContext] setShouldAntialias: NO];
+@end
 
-    [_bgColor set];
-    NSRectFill( s_rc );
 
-    s_arc = s_rc;
-    s_arc.origin.x += 1.5;
-    s_arc.origin.y += 1.5;
-    s_arc.size.width -= s_arc.origin.x;
-    s_arc.size.height -= s_arc.origin.y;
-    [[_bgColor shadowWithLevel: 0.1] set];
-    [NSBezierPath strokeRect: s_arc];
+/*****************************************************************************
+ * MPSlider
+ *****************************************************************************/
+@implementation MPSlider
 
-    s_arc.origin = s_rc.origin;
-    [[NSColor blackColor] set];
-    [NSBezierPath strokeRect: s_arc];
+void _drawKnobInRect(NSRect knobRect)
+{
+    // Center knob in given rect
+    knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
+    knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
+    
+    // Draw diamond
+    NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
+    NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
+    NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
+    NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
+    NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
+    NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
+    NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
+}
 
-    [[NSGraphicsContext currentContext] setShouldAntialias: YES];
+void _drawFrameInRect(NSRect frameRect)
+{
+    // Draw frame
+    NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
+    NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
+    NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
+    NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
 }
 
-- (NSRect)knobRectFlipped:(BOOL)b_flipped
+- (void)drawRect:(NSRect)rect
 {
-    NSSize s_size;
-    NSPoint s_pto;
-    float floatValue;
+    // Draw default to make sure the slider behaves correctly
+    [[NSGraphicsContext currentContext] saveGraphicsState];
+    NSRectClip(NSZeroRect);
+    [super drawRect:rect];
+    [[NSGraphicsContext currentContext] restoreGraphicsState];
+    
+    // Full size
+    rect = [self bounds];
+    int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
+    rect.origin.x += diff-1;
+    rect.origin.y += diff;
+    rect.size.width -= 2*diff-2;
+    rect.size.height -= 2*diff;
+    
+    // Draw dark
+    NSRect knobRect = [[self cell] knobRectFlipped:NO];
+    [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
+    _drawFrameInRect(rect);
+    _drawKnobInRect(knobRect);
+    
+    // Draw shadow
+    [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
+    rect.origin.x++;
+    rect.origin.y++;
+    knobRect.origin.x++;
+    knobRect.origin.y++;
+    _drawFrameInRect(rect);
+    _drawKnobInRect(knobRect);
+}
 
-    floatValue = [self floatValue];
+@end
 
-    if( _scFlags.weAreVertical && b_flipped )
-    {
-        floatValue = _maxValue + _minValue - floatValue;
-    }
 
-    floatValue = (floatValue - _minValue) / (_maxValue - _minValue);
+/*****************************************************************************
+ * ITSlider
+ *****************************************************************************/
 
-    if( _scFlags.weAreVertical )
-    {   
-        s_size = NSMakeSize( _trackRect.size.width, _knobThickness ?
-                             _knobThickness : _trackRect.size.width );
-        s_pto = _trackRect.origin;
-        s_pto.y += (_trackRect.size.height - s_size.height) * floatValue;
-    }
-    else
-    {   
-        s_size = NSMakeSize( _knobThickness ? _knobThickness :
-                             _trackRect.size.height, _trackRect.size.height );
-        s_pto = _trackRect.origin;
-        s_pto.x += (_trackRect.size.width - s_size.width) * floatValue;
+@implementation ITSlider
+
+- (void)awakeFromNib
+{
+    if ([[self cell] class] != [ITSliderCell class]) {
+        // replace cell
+        NSSliderCell *oldCell = [self cell];
+        NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
+        [newCell setTag:[oldCell tag]];
+        [newCell setTarget:[oldCell target]];
+        [newCell setAction:[oldCell action]];
+        [newCell setControlSize:[oldCell controlSize]];
+        [newCell setType:[oldCell type]];
+        [newCell setState:[oldCell state]]; 
+        [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
+        [newCell setAltIncrementValue:[oldCell altIncrementValue]];
+        [newCell setControlTint:[oldCell controlTint]];
+        [newCell setKnobThickness:[oldCell knobThickness]];
+        [newCell setMaxValue:[oldCell maxValue]];
+        [newCell setMinValue:[oldCell minValue]];
+        [newCell setDoubleValue:[oldCell doubleValue]];
+        [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
+        [newCell setSliderType:[oldCell sliderType]];
+        [newCell setEditable:[oldCell isEditable]];
+        [newCell setEnabled:[oldCell isEnabled]];
+        [newCell setEntryType:[oldCell entryType]];
+        [newCell setFocusRingType:[oldCell focusRingType]];
+        [newCell setHighlighted:[oldCell isHighlighted]];
+        [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
+        [self setCell:newCell];
     }
+}
 
-    return NSMakeRect( s_pto.x, s_pto.y, s_size.width, s_size.height );
+@end
+
+/*****************************************************************************
+ * ITSliderCell
+ *****************************************************************************/
+@implementation ITSliderCell
+
+- (id)init
+{
+    self = [super init];
+    _knobOff = [[NSImage imageNamed:@"volumeslider_normal"] retain];
+    _knobOn = [[NSImage imageNamed:@"volumeslider_blue"] retain];
+    b_mouse_down = FALSE;
+    return self;
 }
 
-- (void)drawKnob:(NSRect)s_rc
+- (void)dealloc
 {
-    [[NSGraphicsContext currentContext] setShouldAntialias: NO];
+    [_knobOff release];
+    [_knobOn release];
+    [super dealloc];
+}
 
-    [_knobColor set];
-    NSRectFill( s_rc );
+- (void)drawKnob:(NSRect)knob_rect
+{
+    NSImage *knob;
+
+    if( b_mouse_down )
+        knob = _knobOn;
+    else
+        knob = _knobOff;
+
+    [[self controlView] lockFocus];
+    [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
+        knob_rect.origin.y + knob_rect.size.height -2 )  
+        operation:NSCompositeSourceOver];
+    [[self controlView] unlockFocus];
+}
 
-    [[NSGraphicsContext currentContext] setShouldAntialias: YES];
+- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView: 
+        (NSView *)controlView mouseIsUp:(BOOL)flag
+{
+    b_mouse_down = NO;
+    [self drawKnob];
+    [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
+}
+
+- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
+{
+    b_mouse_down = YES;
+    [self drawKnob];
+    return [super startTrackingAt:startPoint inView:controlView];
 }
 
 @end
+