misc.m 8.79 KB
Newer Older
1 2 3
/*****************************************************************************
 * misc.m: code not specific to vlc
 *****************************************************************************
4
 * Copyright (C) 2003-2015 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24
 *****************************************************************************/

25
#import "CompatibilityFixes.h"
26
#import "misc.h"
27
#import "VLCMainWindow.h"
28
#import "VLCMainMenu.h"
29
#import "VLCControlsBarCommon.h"
30
#import "VLCCoreInteraction.h"
31
#import "VLCStringUtility.h"
32
#import <vlc_actions.h>
33

34
/*****************************************************************************
35
 * VLCDragDropView
36 37
 *****************************************************************************/

38 39 40 41 42 43 44 45 46 47 48 49 50
@implementation VLCDropDisabledImageView

- (void)awakeFromNib
{
    [self unregisterDraggedTypes];
}

@end

/*****************************************************************************
 * VLCDragDropView
 *****************************************************************************/

51 52 53 54 55 56
@interface VLCDragDropView()
{
    bool b_activeDragAndDrop;
}
@end

57 58
@implementation VLCDragDropView

59 60 61 62 63 64 65 66 67 68
- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // default value
        [self setDrawBorder:YES];
    }

    return self;
}
69 70 71 72 73

- (void)enablePlaylistItems
{
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
}
74 75 76 77 78 79

- (BOOL)mouseDownCanMoveWindow
{
    return YES;
}

80 81 82 83 84 85 86
- (void)dealloc
{
    [self unregisterDraggedTypes];
}

- (void)awakeFromNib
{
87
    [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
88 89 90 91
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
92 93 94 95 96 97
    if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric) {
        b_activeDragAndDrop = YES;
        [self setNeedsDisplay:YES];

        return NSDragOperationCopy;
    }
98 99

    return NSDragOperationNone;
100 101
}

102 103 104 105 106 107 108 109 110 111 112 113
- (void)draggingEnded:(id < NSDraggingInfo >)sender
{
    b_activeDragAndDrop = NO;
    [self setNeedsDisplay:YES];
}

- (void)draggingExited:(id < NSDraggingInfo >)sender
{
    b_activeDragAndDrop = NO;
    [self setNeedsDisplay:YES];
}

114 115 116 117 118 119 120
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
121
    BOOL b_returned;
122 123

    if (_dropHandler && [_dropHandler respondsToSelector:@selector(performDragOperation:)])
David Fuhrmann's avatar
David Fuhrmann committed
124
        b_returned = [_dropHandler performDragOperation:sender];
125
    else // default
David Fuhrmann's avatar
David Fuhrmann committed
126
        b_returned = [[VLCCoreInteraction sharedInstance] performDragOperation:sender];
127

128
    [self setNeedsDisplay:YES];
129
    return b_returned;
130 131 132 133 134 135 136
}

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

137 138
- (void)drawRect:(NSRect)dirtyRect
{
139
    if ([self drawBorder] && b_activeDragAndDrop) {
140 141 142
        NSRect frameRect = [self bounds];

        [[NSColor selectedControlColor] set];
143
        NSFrameRectWithWidthUsingOperation(frameRect, 2., NSCompositeSourceOver);
144 145 146 147 148
    }

    [super drawRect:dirtyRect];
}

149 150 151
@end


152 153 154 155
/*****************************************************************************
 * VLCMainWindowSplitView implementation
 *****************************************************************************/
@implementation VLCMainWindowSplitView : NSSplitView
156 157

// Custom color for the dividers
158 159
- (NSColor *)dividerColor
{
160
    return [NSColor colorWithCalibratedRed:.60 green:.60 blue:.60 alpha:1.];
161 162
}

163
// Custom thickness for the divider
164 165
- (CGFloat)dividerThickness
{
166 167
    return 1.0;
}
168

169
@end
170 171 172 173

/*****************************************************************************
 * VLCThreePartImageView interface
 *****************************************************************************/
174 175 176 177 178 179 180 181 182

@interface VLCThreePartImageView()
{
    NSImage *_left_img;
    NSImage *_middle_img;
    NSImage *_right_img;
}
@end

183
@implementation VLCThreePartImageView
184

185 186
- (void)setImagesLeft:(NSImage *)left middle: (NSImage *)middle right:(NSImage *)right
{
187 188 189
    _left_img = left;
    _middle_img = middle;
    _right_img = right;
190 191 192 193 194
}

- (void)drawRect:(NSRect)rect
{
    NSRect bnds = [self bounds];
195
    NSDrawThreePartImage( bnds, _left_img, _middle_img, _right_img, NO, NSCompositeSourceOver, 1, NO );
196 197 198
}

@end
199

200 201 202 203 204 205
@interface PositionFormatter()
{
    NSCharacterSet *o_forbidden_characters;
}
@end

206 207 208 209 210 211
@implementation PositionFormatter

- (id)init
{
    self = [super init];
    NSMutableCharacterSet *nonNumbers = [[[NSCharacterSet decimalDigitCharacterSet] invertedSet] mutableCopy];
212
    [nonNumbers removeCharactersInString:@"-:"];
213 214 215 216 217 218 219
    o_forbidden_characters = [nonNumbers copy];

    return self;
}

- (NSString*)stringForObjectValue:(id)obj
{
220 221 222 223 224 225
    if([obj isKindOfClass:[NSString class]])
        return obj;
    if([obj isKindOfClass:[NSNumber class]])
        return [obj stringValue];

    return nil;
226 227 228 229
}

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

234
- (BOOL)isPartialStringValid:(NSString*)partialString newEditingString:(NSString**)newString errorDescription:(NSString**)error
235 236 237 238 239 240 241 242
{
    if ([partialString rangeOfCharacterFromSet:o_forbidden_characters options:NSLiteralSearch].location != NSNotFound) {
        return NO;
    } else {
        return YES;
    }
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
@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]];
            }
        }

    }
}

268
@end
269 270 271 272 273 274 275 276 277

/*****************************************************************************
 * VLCByteCountFormatter addition
 *****************************************************************************/

@implementation VLCByteCountFormatter

+ (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFormatterCountStyle)countStyle
{
278 279 280 281 282
    // Use native implementation on >= mountain lion
    Class byteFormatterClass = NSClassFromString(@"NSByteCountFormatter");
    if (byteFormatterClass && [byteFormatterClass respondsToSelector:@selector(stringFromByteCount:countStyle:)]) {
        return [byteFormatterClass stringFromByteCount:byteCount countStyle:NSByteCountFormatterCountStyleFile];
    }
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

    float devider = 0.;
    float returnValue = 0.;
    NSString *suffix;

    NSNumberFormatter *theFormatter = [[NSNumberFormatter alloc] init];
    [theFormatter setLocale:[NSLocale currentLocale]];
    [theFormatter setAllowsFloats:YES];

    NSString *returnString = @"";

    if (countStyle != NSByteCountFormatterCountStyleDecimal)
        devider = 1024.;
    else
        devider = 1000.;

    if (byteCount < 1000) {
        returnValue = byteCount;
        suffix = _NS("B");
        [theFormatter setMaximumFractionDigits:0];
        goto end;
    }

    if (byteCount < 1000000) {
        returnValue = byteCount / devider;
        suffix = _NS("KB");
        [theFormatter setMaximumFractionDigits:0];
        goto end;
    }

    if (byteCount < 1000000000) {
        returnValue = byteCount / devider / devider;
        suffix = _NS("MB");
        [theFormatter setMaximumFractionDigits:1];
        goto end;
    }

    [theFormatter setMaximumFractionDigits:2];
    if (byteCount < 1000000000000) {
        returnValue = byteCount / devider / devider / devider;
        suffix = _NS("GB");
        goto end;
    }

    returnValue = byteCount / devider / devider / devider / devider;
    suffix = _NS("TB");

end:
    returnString = [NSString stringWithFormat:@"%@ %@", [theFormatter stringFromNumber:[NSNumber numberWithFloat:returnValue]], suffix];

    return returnString;
}

@end