misc.m 28.4 KB
Newer Older
1
2
3
/*****************************************************************************
 * misc.m: code not specific to vlc
 *****************************************************************************
4
 * Copyright (C) 2003-2013 VLC authors and VideoLAN
5
 * $Id$
6
7
 *
 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
8
 *          Felix Paul Kühne <fkuehne at videolan dot org>
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
24
 *****************************************************************************/

25
#import "misc.h"
26
#import "intf.h"                                          /* VLCApplication */
27
#import "MainWindow.h"
28
#import "ControlsBar.h"
29
#import "controls.h"
30
#import "CoreInteraction.h"
31
#import <CoreAudio/CoreAudio.h>
32
33
#import <vlc_keys.h>

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

/*****************************************************************************
 * NSSound (VLCAdditions)
 *
 * added code to change the system volume, needed for the apple remote code
 * this is simplified code, which won't let you set the exact volume
 * (that's what the audio output is for after all), but just the system volume
 * in steps of 1/16 (matching the default AR or volume key implementation).
 *****************************************************************************/

@implementation NSSound (VLCAdditions)

+ (float)systemVolumeForChannel:(int)channel
{
    AudioDeviceID i_device;
    float f_volume;
    OSStatus err;
    UInt32 i_size;

    i_size = sizeof( i_device );
    AudioObjectPropertyAddress deviceAddress = { kAudioHardwarePropertyDefaultOutputDevice, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
    err = AudioObjectGetPropertyData( kAudioObjectSystemObject, &deviceAddress, 0, NULL, &i_size, &i_device );
56
    if (err != noErr) {
57
58
59
60
61
62
63
        msg_Warn( VLCIntf, "couldn't get main audio output device" );
        return .0;
    }

    AudioObjectPropertyAddress propertyAddress = { kAudioDevicePropertyVolumeScalar, kAudioDevicePropertyScopeOutput, channel };
    i_size = sizeof( f_volume );
    err = AudioObjectGetPropertyData(i_device, &propertyAddress, 0, NULL, &i_size, &f_volume);
64
    if (err != noErr) {
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        msg_Warn( VLCIntf, "couldn't get volume value" );
        return .0;
    }

    return f_volume;
}

+ (bool)setSystemVolume:(float)f_volume forChannel:(int)i_channel
{
    /* the following code will fail on S/PDIF devices. there is an easy work-around, but we'd like to match the OS behavior */

    AudioDeviceID i_device;
    OSStatus err;
    UInt32 i_size;
    Boolean b_writeable;

    i_size = sizeof( i_device );
    AudioObjectPropertyAddress deviceAddress = { kAudioHardwarePropertyDefaultOutputDevice, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
    err = AudioObjectGetPropertyData( kAudioObjectSystemObject, &deviceAddress, 0, NULL, &i_size, &i_device );
84
    if (err != noErr) {
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        msg_Warn( VLCIntf, "couldn't get main audio output device" );
        return NO;
    }

    AudioObjectPropertyAddress propertyAddress = { kAudioDevicePropertyVolumeScalar, kAudioDevicePropertyScopeOutput, i_channel };
    i_size = sizeof( f_volume );
    err = AudioObjectIsPropertySettable( i_device, &propertyAddress, &b_writeable );
    if (err != noErr || !b_writeable ) {
        msg_Warn( VLCIntf, "we can't set the main audio devices' volume" );
        return NO;
    }
    err = AudioObjectSetPropertyData(i_device, &propertyAddress, 0, NULL, i_size, &f_volume);

    return YES;
}

+ (void)increaseSystemVolume
{
    float f_volume = [NSSound systemVolumeForChannel:1]; // we trust that mono is always available and that all channels got the same volume
    f_volume += .0625; // 1/16 to match the OS
    bool b_returned = YES;

    /* since core audio doesn't provide a reasonable way to see how many channels we got, let's see how long we can do this */
    for (NSUInteger x = 1; b_returned ; x++)
        b_returned = [NSSound setSystemVolume: f_volume forChannel:x];
}

+ (void)decreaseSystemVolume
{
    float f_volume = [NSSound systemVolumeForChannel:1]; // we trust that mono is always available and that all channels got the same volume
    f_volume -= .0625; // 1/16 to match the OS
    bool b_returned = YES;

    /* since core audio doesn't provide a reasonable way to see how many channels we got, let's see how long we can do this */
    for (NSUInteger x = 1; b_returned ; x++)
        b_returned = [NSSound setSystemVolume: f_volume forChannel:x];
}

@end
hartman's avatar
hartman committed
124

125
126
127
128
129
130
131
132
/*****************************************************************************
 * NSAnimation (VLCAdditions)
 *
 *  Missing extension to NSAnimation
 *****************************************************************************/

@implementation NSAnimation (VLCAdditions)
/* fake class attributes  */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
133
static NSMapTable *VLCAdditions_userInfo = NULL;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

+ (void)load
{
    /* init our fake object attribute */
    VLCAdditions_userInfo = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);
}

- (void)dealloc
{
    NSMapRemove(VLCAdditions_userInfo, self);
    [super dealloc];
}

- (void)setUserInfo: (void *)userInfo
{
    NSMapInsert(VLCAdditions_userInfo, self, (void*)userInfo);
}

- (void *)userInfo
{
    return NSMapGet(VLCAdditions_userInfo, self);
}
@end

/*****************************************************************************
 * NSScreen (VLCAdditions)
 *
 *  Missing extension to NSScreen
 *****************************************************************************/

@implementation NSScreen (VLCAdditions)

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
166
static NSMutableArray *blackoutWindows = NULL;
167
168
169
170

+ (void)load
{
    /* init our fake object attribute */
171
    blackoutWindows = [[NSMutableArray alloc] initWithCapacity:1];
172
173
174
175
}

+ (NSScreen *)screenWithDisplayID: (CGDirectDisplayID)displayID
{
176
    NSUInteger count = [[NSScreen screens] count];
177

178
    for ( NSUInteger i = 0; i < count; i++ ) {
179
        NSScreen *screen = [[NSScreen screens] objectAtIndex:i];
180
        if ([screen displayID] == displayID)
181
182
183
184
185
            return screen;
    }
    return nil;
}

186
- (BOOL)hasMenuBar
187
{
188
189
190
191
    if (OSX_REDACTED)
        return YES;
    else
        return ([self displayID] == [[[NSScreen screens] objectAtIndex:0] displayID]);
192
193
}

194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
- (BOOL)hasDock
{
    NSRect screen_frame = [self frame];
    NSRect screen_visible_frame = [self visibleFrame];
    CGFloat f_menu_bar_thickness = [self hasMenuBar] ? [[NSStatusBar systemStatusBar] thickness] : 0.0;

    BOOL b_found_dock = NO;
    if (screen_visible_frame.size.width < screen_frame.size.width)
        b_found_dock = YES;
    else if (screen_visible_frame.size.height + f_menu_bar_thickness < screen_frame.size.height)
        b_found_dock = YES;

    return b_found_dock;
}

209
210
211
212
213
- (BOOL)isScreen: (NSScreen*)screen
{
    return ([self displayID] == [screen displayID]);
}

214
215
- (CGDirectDisplayID)displayID
{
216
    return (CGDirectDisplayID)[[[self deviceDescription] objectForKey: @"NSScreenNumber"] intValue];
217
218
219
220
221
222
223
224
}

- (void)blackoutOtherScreens
{
    /* Free our previous blackout window (follow blackoutWindow alloc strategy) */
    [blackoutWindows makeObjectsPerformSelector:@selector(close)];
    [blackoutWindows removeAllObjects];

225
    NSUInteger screenCount = [[NSScreen screens] count];
226
    for (NSUInteger i = 0; i < screenCount; i++) {
227
        NSScreen *screen = [[NSScreen screens] objectAtIndex:i];
228
229
        VLCWindow *blackoutWindow;
        NSRect screen_rect;
230

231
        if ([self isScreen: screen])
232
            continue;
233
234

        screen_rect = [screen frame];
235
        screen_rect.origin.x = screen_rect.origin.y = 0;
236

237
238
239
240
        /* blackoutWindow alloc strategy
            - The NSMutableArray blackoutWindows has the blackoutWindow references
            - blackoutOtherDisplays is responsible for alloc/releasing its Windows
        */
241
242
        blackoutWindow = [[VLCWindow alloc] initWithContentRect: screen_rect styleMask: NSBorderlessWindowMask
                backing: NSBackingStoreBuffered defer: NO screen: screen];
243
244
        [blackoutWindow setBackgroundColor:[NSColor blackColor]];
        [blackoutWindow setLevel: NSFloatingWindowLevel]; /* Disappear when Expose is triggered */
245

246
        [blackoutWindow displayIfNeeded];
247
248
249
250
        [blackoutWindow orderFront: self animate: YES];

        [blackoutWindows addObject: blackoutWindow];
        [blackoutWindow release];
251

252
        [screen setFullscreenPresentationOptions];
253
254
255
256
257
    }
}

+ (void)unblackoutScreens
{
258
    NSUInteger blackoutWindowCount = [blackoutWindows count];
259

260
    for (NSUInteger i = 0; i < blackoutWindowCount; i++) {
261
        VLCWindow *blackoutWindow = [blackoutWindows objectAtIndex:i];
262
        [[blackoutWindow screen] setNonFullscreenPresentationOptions];
263
264
265
266
        [blackoutWindow closeAndAnimate: YES];
    }
}

267
268
269
270
271
272
273
274
275
276
- (void)setFullscreenPresentationOptions
{
    NSApplicationPresentationOptions presentationOpts = [NSApp presentationOptions];
    if ([self hasMenuBar])
        presentationOpts |= NSApplicationPresentationAutoHideMenuBar;
    if ([self hasMenuBar] || [self hasDock])
        presentationOpts |= NSApplicationPresentationAutoHideDock;
    [NSApp setPresentationOptions:presentationOpts];
}

277
278
279
280
281
282
283
284
285
286
- (void)setNonFullscreenPresentationOptions
{
    NSApplicationPresentationOptions presentationOpts = [NSApp presentationOptions];
    if ([self hasMenuBar])
        presentationOpts &= (~NSApplicationPresentationAutoHideMenuBar);
    if ([self hasMenuBar] || [self hasDock])
        presentationOpts &= (~NSApplicationPresentationAutoHideDock);
    [NSApp setPresentationOptions:presentationOpts];
}

287
288
@end

hartman's avatar
hartman committed
289
290
291
292
293
294
295
296
297
298
299
/*****************************************************************************
 * VLBrushedMetalImageView
 *****************************************************************************/

@implementation VLBrushedMetalImageView

- (BOOL)mouseDownCanMoveWindow
{
    return YES;
}

hartman's avatar
hartman committed
300
301
302
303
304
305
306
307
- (void)dealloc
{
    [self unregisterDraggedTypes];
    [super dealloc];
}

- (void)awakeFromNib
{
308
    [self registerForDraggedTypes:@[NSFilenamesPboardType]];
309
310
311
    [self setImageScaling: NSScaleToFit];
    [self setImageFrameStyle: NSImageFrameNone];
    [self setImageAlignment: NSImageAlignCenter];
hartman's avatar
hartman committed
312
313
314
315
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
316
    if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
hartman's avatar
hartman committed
317
        return NSDragOperationGeneric;
318
319

    return NSDragOperationNone;
hartman's avatar
hartman committed
320
321
322
323
324
325
326
327
328
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
329
330
    BOOL b_returned;
    b_returned = [[VLCCoreInteraction sharedInstance] performDragOperation: sender];
331

hartman's avatar
hartman committed
332
    [self setNeedsDisplay:YES];
333
    return b_returned;
hartman's avatar
hartman committed
334
335
336
337
338
339
340
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
    [self setNeedsDisplay:YES];
}

hartman's avatar
hartman committed
341
342
343
@end


344
345
346
347
348
/*****************************************************************************
 * MPSlider
 *****************************************************************************/
@implementation MPSlider

hartman's avatar
hartman committed
349
void _drawKnobInRect(NSRect knobRect)
350
{
hartman's avatar
hartman committed
351
352
353
    // 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);
354

hartman's avatar
hartman committed
355
356
357
358
359
360
361
362
    // 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);
363
364
}

hartman's avatar
hartman committed
365
void _drawFrameInRect(NSRect frameRect)
366
{
hartman's avatar
hartman committed
367
368
369
370
371
    // 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);
372
373
}

hartman's avatar
hartman committed
374
- (void)drawRect:(NSRect)rect
375
{
hartman's avatar
hartman committed
376
377
378
379
380
    // Draw default to make sure the slider behaves correctly
    [[NSGraphicsContext currentContext] saveGraphicsState];
    NSRectClip(NSZeroRect);
    [super drawRect:rect];
    [[NSGraphicsContext currentContext] restoreGraphicsState];
381

hartman's avatar
hartman committed
382
383
384
385
386
387
388
    // 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;
389

hartman's avatar
hartman committed
390
391
392
393
394
    // Draw dark
    NSRect knobRect = [[self cell] knobRectFlipped:NO];
    [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
    _drawFrameInRect(rect);
    _drawKnobInRect(knobRect);
395

hartman's avatar
hartman committed
396
397
398
399
400
401
402
403
    // Draw shadow
    [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
    rect.origin.x++;
    rect.origin.y++;
    knobRect.origin.x++;
    knobRect.origin.y++;
    _drawFrameInRect(rect);
    _drawKnobInRect(knobRect);
404
405
}

406
407
@end

408
409
410
411
412
413
414
415
416
/*****************************************************************************
 * ProgressView
 *****************************************************************************/

@implementation VLCProgressView : NSView

- (void)scrollWheel:(NSEvent *)o_event
{
    intf_thread_t * p_intf = VLCIntf;
417
    BOOL b_forward = NO;
418
419
420
421
422
423
424
425
426
427
428
429
430
    CGFloat f_deltaY = [o_event deltaY];
    CGFloat f_deltaX = [o_event deltaX];

    if (!OSX_SNOW_LEOPARD && [o_event isDirectionInvertedFromDevice])
        f_deltaX = -f_deltaX; // optimisation, actually double invertion of f_deltaY here
    else
        f_deltaY = -f_deltaY;

    // positive for left / down, negative otherwise
    CGFloat f_delta = f_deltaX + f_deltaY;
    CGFloat f_abs;
    int i_vlckey;

431
    if (f_delta > 0.0f)
432
433
        f_abs = f_delta;
    else {
434
        b_forward = YES;
435
436
437
        f_abs = -f_delta;
    }

438
439
440
441
442
443
    for (NSUInteger i = 0; i < (int)(f_abs/4.+1.) && f_abs > 0.05 ; i++) {
        if (b_forward)
            [[VLCCoreInteraction sharedInstance] forwardExtraShort];
        else
            [[VLCCoreInteraction sharedInstance] backwardExtraShort];
    }
444
445
446
447
448
449
450
451
452
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}

@end

453
454
455
456
457
458
/*****************************************************************************
 * TimeLineSlider
 *****************************************************************************/

@implementation TimeLineSlider

459
460
- (void)awakeFromNib
{
461
    if (config_GetInt( VLCIntf, "macosx-interfacestyle" )) {
462
        o_knob_img = [NSImage imageNamed:@"progression-knob_dark"];
463
464
        b_dark = YES;
    } else {
465
        o_knob_img = [NSImage imageNamed:@"progression-knob"];
466
467
        b_dark = NO;
    }
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
    img_rect.size = [o_knob_img size];
    img_rect.origin.x = img_rect.origin.y = 0;
}

- (void)dealloc
{
    [o_knob_img release];
    [super dealloc];
}

- (CGFloat)knobPosition
{
    NSRect knobRect = [[self cell] knobRectFlipped:NO];
    knobRect.origin.x += knobRect.size.width / 2;
    return knobRect.origin.x;
}

485
486
- (void)drawKnobInRect:(NSRect)knobRect
{
487
488
489
490
    knobRect.origin.x += (knobRect.size.width - img_rect.size.width) / 2;
    knobRect.size.width = img_rect.size.width;
    knobRect.size.height = img_rect.size.height;
    [o_knob_img drawInRect:knobRect fromRect:img_rect operation:NSCompositeSourceOver fraction:1];
491
492
493
494
}

- (void)drawRect:(NSRect)rect
{
495
496
    [[(VLCVideoWindowCommon *)[self window] controlsBar] drawFancyGradientEffectForTimeSlider];
    msleep(10000); //wait for the gradient to draw completely
497

498
499
500
501
502
503
504
    /* Draw default to make sure the slider behaves correctly */
    [[NSGraphicsContext currentContext] saveGraphicsState];
    NSRectClip(NSZeroRect);
    [super drawRect:rect];
    [[NSGraphicsContext currentContext] restoreGraphicsState];

    NSRect knobRect = [[self cell] knobRectFlipped:NO];
505
    knobRect.origin.y+=1;
506
507
508
509
    [self drawKnobInRect: knobRect];
}

@end
510

511
512
513
514
515
516
/*****************************************************************************
 * VLCVolumeSliderCommon
 *****************************************************************************/

@implementation VLCVolumeSliderCommon : NSSlider

517
518
@synthesize usesBrightArtwork = _usesBrightArtwork;

519
520
521
- (void)scrollWheel:(NSEvent *)o_event
{
    intf_thread_t * p_intf = VLCIntf;
522
    BOOL b_up = NO;
523
524
525
526
527
528
529
530
531
532
533
534
535
    CGFloat f_deltaY = [o_event deltaY];
    CGFloat f_deltaX = [o_event deltaX];

    if (!OSX_SNOW_LEOPARD && [o_event isDirectionInvertedFromDevice])
        f_deltaX = -f_deltaX; // optimisation, actually double invertion of f_deltaY here
    else
        f_deltaY = -f_deltaY;

    // positive for left / down, negative otherwise
    CGFloat f_delta = f_deltaX + f_deltaY;
    CGFloat f_abs;
    int i_vlckey;

536
    if (f_delta > 0.0f)
537
538
        f_abs = f_delta;
    else {
539
        b_up = YES;
540
541
542
        f_abs = -f_delta;
    }

543
544
545
546
547
548
    for (NSUInteger i = 0; i < (int)(f_abs/4.+1.) && f_abs > 0.05 ; i++) {
        if (b_up)
            [[VLCCoreInteraction sharedInstance] volumeUp];
        else
            [[VLCCoreInteraction sharedInstance] volumeDown];
    }
549
550
}

551
552
- (void)drawFullVolumeMarker
{
553
554
555
556
    CGFloat maxAudioVol = self.maxValue / AOUT_VOLUME_DEFAULT;
    if (maxAudioVol < 1.)
        return;

557
    NSColor *drawingColor;
558
    // for bright artwork, a black color is used and vice versa
559
    if (_usesBrightArtwork)
560
        drawingColor = [[NSColor blackColor] colorWithAlphaComponent:.4];
561
    else
562
        drawingColor = [[NSColor whiteColor] colorWithAlphaComponent:.4];
563
564

    NSBezierPath* bezierPath = [NSBezierPath bezierPath];
565
    [self drawFullVolBezierPath:bezierPath];
566
567
568
569
570
    [bezierPath closePath];

    bezierPath.lineWidth = 1.;
    [drawingColor setStroke];
    [bezierPath stroke];
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
}

- (CGFloat)fullVolumePos
{
    CGFloat maxAudioVol = self.maxValue / AOUT_VOLUME_DEFAULT;
    CGFloat sliderRange = [self frame].size.width - [self knobThickness];
    CGFloat sliderOrigin = [self knobThickness] / 2.;

    return 1. / maxAudioVol * sliderRange + sliderOrigin;
}

- (void)drawFullVolBezierPath:(NSBezierPath*)bezierPath
{
    CGFloat fullVolPos = [self fullVolumePos];
    [bezierPath moveToPoint:NSMakePoint(fullVolPos, [self frame].size.height - 3.)];
    [bezierPath lineToPoint:NSMakePoint(fullVolPos, 2.)];
587
}
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

@end

@implementation VolumeSliderCell

- (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView
{
    VLCVolumeSliderCommon *o_slider = (VLCVolumeSliderCommon *)controlView;
    CGFloat fullVolumePos = [o_slider fullVolumePos] + 2.;

    CGPoint snapToPoint = currentPoint;
    if (ABS(fullVolumePos - currentPoint.x) <= 4.)
        snapToPoint.x = fullVolumePos;

    return [super continueTracking:lastPoint at:snapToPoint inView:controlView];
}
604

605
606
@end

607
608
609
610
611
612
/*****************************************************************************
 * ITSlider
 *****************************************************************************/

@implementation ITSlider

613
- (void)awakeFromNib
614
{
615
616
617
618
619
620
    BOOL b_dark = config_GetInt( VLCIntf, "macosx-interfacestyle" );
    if (b_dark)
        img = [NSImage imageNamed:@"volume-slider-knob_dark"];
    else
        img = [NSImage imageNamed:@"volume-slider-knob"];

621
622
    image_rect.size = [img size];
    image_rect.origin.x = 0;
623
624
625
626
627
628
629
630
631

    if (b_dark)
        image_rect.origin.y = -1;
    else
        image_rect.origin.y = 0;
}

- (void)drawKnobInRect:(NSRect)knobRect
{
632
633
634
635
    knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
    knobRect.size.width = image_rect.size.width;
    knobRect.size.height = image_rect.size.height;
    [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
636
637
}

638
- (void)drawRect:(NSRect)rect
639
{
640
641
642
643
644
    /* Draw default to make sure the slider behaves correctly */
    [[NSGraphicsContext currentContext] saveGraphicsState];
    NSRectClip(NSZeroRect);
    [super drawRect:rect];
    [[NSGraphicsContext currentContext] restoreGraphicsState];
645

646
647
    [self drawFullVolumeMarker];

648
649
650
    NSRect knobRect = [[self cell] knobRectFlipped:NO];
    knobRect.origin.y+=2;
    [self drawKnobInRect: knobRect];
651
652
653
654
}

@end

655
656
657
658
659
660
661
/*****************************************************************************
 * VLCTimeField implementation
 *****************************************************************************
 * we need this to catch our click-event in the controller window
 *****************************************************************************/

@implementation VLCTimeField
662
663
+ (void)initialize{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
664
665
666
667
    NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                 @"NO", @"DisplayTimeAsTimeRemaining",
                                 @"YES", @"DisplayFullscreenTimeAsTimeRemaining",
                                 nil];
668

669
670
671
    [defaults registerDefaults:appDefaults];
}

672
- (id)initWithFrame:(NSRect)frameRect
673
{
674
675
676
677
    if (self = [super initWithFrame:frameRect]) {
        textAlignment = NSCenterTextAlignment;
        o_remaining_identifier = @"";
    }
678

679
680
    return self;
}
681

682
683
684
685
- (void)setRemainingIdentifier:(NSString *)o_string
{
    o_remaining_identifier = o_string;
    b_time_remaining = [[NSUserDefaults standardUserDefaults] boolForKey:o_remaining_identifier];
686
687
}

688
689
690
691
692
693
- (void)setAlignment:(NSTextAlignment)alignment
{
    textAlignment = alignment;
    [self setStringValue:[self stringValue]];
}

694
695
696
- (void)dealloc
{
    [o_string_shadow release];
697
    [super dealloc];
698
699
700
701
}

- (void)setStringValue:(NSString *)string
{
702
    if (!o_string_shadow) {
703
704
        o_string_shadow = [[NSShadow alloc] init];
        [o_string_shadow setShadowColor: [NSColor colorWithCalibratedWhite:1.0 alpha:0.5]];
705
        [o_string_shadow setShadowOffset:NSMakeSize(0.0, -1.0)];
706
707
708
        [o_string_shadow setShadowBlurRadius:0.0];
    }

709
    NSMutableAttributedString *o_attributed_string = [[NSMutableAttributedString alloc] initWithString:string attributes: nil];
710
711
712
    NSUInteger i_stringLength = [string length];

    [o_attributed_string addAttribute: NSShadowAttributeName value: o_string_shadow range: NSMakeRange(0, i_stringLength)];
713
    [o_attributed_string setAlignment: textAlignment range: NSMakeRange(0, i_stringLength)];
714
715
716
717
    [self setAttributedStringValue: o_attributed_string];
    [o_attributed_string release];
}

718
719
- (void)mouseDown: (NSEvent *)ourEvent
{
720
    if ( [ourEvent clickCount] > 1 )
721
722
        [[[VLCMain sharedInstance] controls] goToSpecificTime: nil];
    else
723
    {
724
725
726
727
728
729
730
731
732
733
734
735
        if (![o_remaining_identifier isEqualToString: @""]) {
            if ([[NSUserDefaults standardUserDefaults] boolForKey:o_remaining_identifier]) {
                [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:o_remaining_identifier];
                b_time_remaining = NO;
            } else {
                [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:o_remaining_identifier];
                b_time_remaining = YES;
            }
        } else {
            b_time_remaining = !b_time_remaining;
            [[NSUserDefaults standardUserDefaults] setObject:(b_time_remaining ? @"YES" : @"NO") forKey:o_remaining_identifier];
        }
736
737
738
739
740
    }
}

- (BOOL)timeRemaining
{
741
742
743
744
    if (![o_remaining_identifier isEqualToString: @""])
        return [[NSUserDefaults standardUserDefaults] boolForKey:o_remaining_identifier];
    else
        return b_time_remaining;
745
}
746

747
@end
748
749
750

/*****************************************************************************
 * VLCMainWindowSplitView implementation
751
 * comment 1 + 2 taken from NSSplitView.h (10.7 SDK)
752
753
754
755
756
757
 *****************************************************************************/
@implementation VLCMainWindowSplitView : NSSplitView
/* Return the color of the dividers that the split view is drawing between subviews. The default implementation of this method returns [NSColor clearColor] for the thick divider style. It will also return [NSColor clearColor] for the thin divider style when the split view is in a textured window. All other thin dividers are drawn with a color that looks good between two white panes. You can override this method to change the color of dividers.
 */
- (NSColor *)dividerColor
{
758
    return [NSColor colorWithCalibratedRed:.60 green:.60 blue:.60 alpha:1.];
759
760
761
762
763
764
}

/* Return the thickness of the dividers that the split view is drawing between subviews. The default implementation returns a value that depends on the divider style. You can override this method to change the size of dividers.
 */
- (CGFloat)dividerThickness
{
765
766
    return 1.0;
}
767
@end
768
769
770
771
772

/*****************************************************************************
 * VLCThreePartImageView interface
 *****************************************************************************/
@implementation VLCThreePartImageView
773

774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
- (void)dealloc
{
    [o_left_img release];
    [o_middle_img release];
    [o_right_img release];

    [super dealloc];
}

- (void)setImagesLeft:(NSImage *)left middle: (NSImage *)middle right:(NSImage *)right
{
    if (o_left_img)
        [o_left_img release];
    if (o_middle_img)
        [o_middle_img release];
    if (o_right_img)
        [o_right_img release];

    o_left_img = [left retain];
    o_middle_img = [middle retain];
    o_right_img = [right retain];
}

- (void)drawRect:(NSRect)rect
{
    NSRect bnds = [self bounds];
    NSDrawThreePartImage( bnds, o_left_img, o_middle_img, o_right_img, NO, NSCompositeSourceOver, 1, NO );
}

@end
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819

@implementation VLCThreePartDropView

- (BOOL)mouseDownCanMoveWindow
{
    return YES;
}

- (void)dealloc
{
    [self unregisterDraggedTypes];
    [super dealloc];
}

- (void)awakeFromNib
{
820
    [self registerForDraggedTypes:@[NSFilenamesPboardType]];
821
822
823
824
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
825
    if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
826
        return NSDragOperationGeneric;
827
828

    return NSDragOperationNone;
829
830
831
832
833
834
835
836
837
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
838
839
    BOOL b_returned;
    b_returned = [[VLCCoreInteraction sharedInstance] performDragOperation: sender];
840
841
842
843
844
845
846
847
848
849

    [self setNeedsDisplay:YES];
    return YES;
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
    [self setNeedsDisplay:YES];
}

850
851
852
853
854
855
856
857
858
859
@end

@implementation PositionFormatter

- (id)init
{
    self = [super init];
    NSMutableCharacterSet *nonNumbers = [[[NSCharacterSet decimalDigitCharacterSet] invertedSet] mutableCopy];
    [nonNumbers removeCharactersInString:@":"];
    o_forbidden_characters = [nonNumbers copy];
860
    [nonNumbers release];
861
862
863
864

    return self;
}

865
866
867
868
869
870
- (void)dealloc
{
    [o_forbidden_characters release];
    [super dealloc];
}

871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
- (NSString*)stringForObjectValue:(id)obj
{
    return obj;
}

- (BOOL)getObjectValue:(id*)obj forString:(NSString*)string errorDescription:(NSString**)error
{
    *obj = [[string copy] autorelease];
    return YES;
}

- (bool)isPartialStringValid:(NSString*)partialString newEditingString:(NSString**)newString errorDescription:(NSString**)error
{
    if ([partialString rangeOfCharacterFromSet:o_forbidden_characters options:NSLiteralSearch].location != NSNotFound) {
        return NO;
    } else {
        return YES;
    }
}


892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
@end

@implementation NSView (EnableSubviews)

- (void)enableSubviews:(BOOL)b_enable
{
    for (NSView *o_view in [self subviews]) {
        [o_view enableSubviews:b_enable];

        // enable NSControl
        if ([o_view respondsToSelector:@selector(setEnabled:)]) {
            [(NSControl *)o_view setEnabled:b_enable];
        }
        // also "enable / disable" text views
        if ([o_view respondsToSelector:@selector(setTextColor:)]) {
            if (b_enable == NO) {
                [(NSTextField *)o_view setTextColor:[NSColor disabledControlTextColor]];
            } else {
                [(NSTextField *)o_view setTextColor:[NSColor controlTextColor]];
            }
        }

    }
}

917
@end