]> git.sesse.net Git - vlc/commitdiff
macosx: reimplement NSByteCountFormatter for 10.7 and earlier
authorFelix Paul Kühne <fkuehne@videolan.org>
Sun, 2 Feb 2014 21:46:08 +0000 (22:46 +0100)
committerFelix Paul Kühne <fkuehne@videolan.org>
Sun, 2 Feb 2014 21:49:03 +0000 (22:49 +0100)
Note that this implementation only includes the features we care about and is incomplete

modules/gui/macosx/misc.h
modules/gui/macosx/misc.m
modules/gui/macosx/playlist.m

index b759483d353cd54d11a24da8397497801212e66d..4f38bf02734014a90f50f9198cfbb539308c6b77 100644 (file)
 
 @interface NSView (EnableSubviews)
 - (void)enableSubviews:(BOOL)b_enable;
-@end
\ No newline at end of file
+@end
+
+/*****************************************************************************
+ * VLCByteCountFormatter addition
+ *****************************************************************************/
+
+#ifndef MAC_OS_X_VERSION_10_8
+typedef NS_ENUM(NSInteger, NSByteCountFormatterCountStyle) {
+    // Specifies display of file or storage byte counts. The actual behavior for this is platform-specific; on OS X 10.7 and less, this uses the binary style, but decimal style on 10.8 and above
+    NSByteCountFormatterCountStyleFile   = 0,
+    // Specifies display of memory byte counts. The actual behavior for this is platform-specific; on OS X 10.7 and less, this uses the binary style, but that may change over time.
+    NSByteCountFormatterCountStyleMemory = 1,
+    // The following two allow specifying the number of bytes for KB explicitly. It's better to use one of the above values in most cases.
+    NSByteCountFormatterCountStyleDecimal = 2,    // 1000 bytes are shown as 1 KB
+    NSByteCountFormatterCountStyleBinary  = 3     // 1024 bytes are shown as 1 KB
+};
+#endif
+
+@interface VLCByteCountFormatter : NSFormatter {
+}
+
++ (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFormatterCountStyle)countStyle;
+@end
index 9be3e738579012811bf4ad925859646009752b09..06b2a5fe266585c21e907f8ab7d3a878e5255177 100644 (file)
@@ -984,3 +984,76 @@ void _drawFrameInRect(NSRect frameRect)
 }
 
 @end
+
+/*****************************************************************************
+ * VLCByteCountFormatter addition
+ *****************************************************************************/
+
+#ifndef MAC_OS_X_VERSION_10_8
+@interface NSByteCountFormatter (IntroducedInMountainLion)
++ (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFormatterCountStyle)countStyle;
+@end
+#endif
+
+
+@implementation VLCByteCountFormatter
+
++ (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFormatterCountStyle)countStyle
+{
+    if (OSX_MAVERICKS || OSX_MOUNTAIN_LION)
+        return [NSByteCountFormatter stringFromByteCount:byteCount countStyle:NSByteCountFormatterCountStyleFile];
+
+    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];
+    [theFormatter release];
+
+    return returnString;
+}
+
+@end
index f2c80523463f11d4e43a4df1039878c51fe56dc7..ada48d1e14f7c69d863943eee92f3b0fc775bcaf 100644 (file)
                 if ([fileManager fileExistsAtPath:[url path]]) {
                     NSError *error;
                     NSDictionary *attributes = [fileManager attributesOfItemAtPath:[url path] error:&error];
-                    o_value = [NSByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleDecimal];
+                    o_value = [VLCByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleDecimal];
                 }
             }
             free(psz_value);