[vlc-commits] macosx: Move string wrapping to NSString category

Marvin Scholz git at videolan.org
Tue Oct 9 11:34:18 CEST 2018


vlc | branch: master | Marvin Scholz <epirat07 at gmail.com> | Tue Oct  9 11:26:58 2018 +0200| [47719db04705c1b62305ea79786d02002ff8b901] | committer: Marvin Scholz

macosx: Move string wrapping to NSString category

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=47719db04705c1b62305ea79786d02002ff8b901
---

 modules/gui/macosx/NSString+Helpers.h | 10 ++++++++
 modules/gui/macosx/NSString+Helpers.m | 44 +++++++++++++++++++++++++++++++++++
 modules/gui/macosx/VLCStringUtility.h |  1 -
 modules/gui/macosx/VLCStringUtility.m | 38 ------------------------------
 modules/gui/macosx/prefs_widgets.m    | 25 ++++++++++----------
 5 files changed, 67 insertions(+), 51 deletions(-)

diff --git a/modules/gui/macosx/NSString+Helpers.h b/modules/gui/macosx/NSString+Helpers.h
index 35c18a7d5e..4b570bc086 100644
--- a/modules/gui/macosx/NSString+Helpers.h
+++ b/modules/gui/macosx/NSString+Helpers.h
@@ -54,6 +54,16 @@
  */
 - (NSString *)base64DecodedString;
 
+/**
+ Returns a copy of the receiver string, wrapped to the specified width
+
+ This method returns a copy of the receiver string, wrapped to the given
+ width in pixels.
+
+ \param width Width in pixel
+ */
+- (NSString *)stringWrappedToWidth:(int)width;
+
 @end
 
 /**
diff --git a/modules/gui/macosx/NSString+Helpers.m b/modules/gui/macosx/NSString+Helpers.m
index e8257d898a..ee5499a8f9 100644
--- a/modules/gui/macosx/NSString+Helpers.m
+++ b/modules/gui/macosx/NSString+Helpers.m
@@ -26,6 +26,7 @@
  *****************************************************************************/
 
 #import "NSString+Helpers.h"
+#import <Cocoa/Cocoa.h>
 
 #import <vlc_common.h>
 #import <vlc_strings.h>
@@ -71,4 +72,47 @@
                                     freeWhenDone:YES];
 }
 
+- (NSString *)stringWrappedToWidth:(int)width
+{
+    NSRange glyphRange, effectiveRange, charRange;
+    unsigned breaksInserted = 0;
+
+    NSMutableString *wrapped_string = [self mutableCopy];
+
+    NSTextStorage *text_storage =
+        [[NSTextStorage alloc] initWithString:wrapped_string
+                                   attributes:@{
+                                                NSFontAttributeName : [NSFont labelFontOfSize: 0.0]
+                                                }];
+
+    NSLayoutManager *layout_manager = [[NSLayoutManager alloc] init];
+    NSTextContainer *text_container =
+        [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(width, 2000)];
+
+    [layout_manager addTextContainer:text_container];
+    [text_storage addLayoutManager:layout_manager];
+
+    glyphRange = [layout_manager glyphRangeForTextContainer:text_container];
+
+    for (NSUInteger glyphIndex = glyphRange.location;
+         glyphIndex < NSMaxRange(glyphRange);
+         glyphIndex += effectiveRange.length)
+    {
+        [layout_manager lineFragmentRectForGlyphAtIndex:glyphIndex
+                                         effectiveRange:&effectiveRange];
+
+        charRange = [layout_manager characterRangeForGlyphRange:effectiveRange
+                                               actualGlyphRange:&effectiveRange];
+        if ([wrapped_string lineRangeForRange:
+                NSMakeRange(charRange.location + breaksInserted, charRange.length)
+             ].length > charRange.length)
+        {
+            [wrapped_string insertString:@"\n" atIndex:NSMaxRange(charRange) + breaksInserted];
+            breaksInserted++;
+        }
+    }
+
+    return [NSString stringWithString:wrapped_string];
+}
+
 @end
diff --git a/modules/gui/macosx/VLCStringUtility.h b/modules/gui/macosx/VLCStringUtility.h
index b7402fa6ca..6b68ff11de 100644
--- a/modules/gui/macosx/VLCStringUtility.h
+++ b/modules/gui/macosx/VLCStringUtility.h
@@ -58,7 +58,6 @@ NSImage *imageFromRes(NSString *name);
 
 + (VLCStringUtility *)sharedInstance;
 
-- (NSString *)wrapString: (NSString *)o_in_string toWidth: (int)i_width;
 - (NSString *)getCurrentTimeAsString:(input_thread_t *)p_input negative:(BOOL)b_negative;
 - (NSString *)stringForTime:(long long int)time;
 
diff --git a/modules/gui/macosx/VLCStringUtility.m b/modules/gui/macosx/VLCStringUtility.m
index 2176da9199..302d817176 100644
--- a/modules/gui/macosx/VLCStringUtility.m
+++ b/modules/gui/macosx/VLCStringUtility.m
@@ -67,44 +67,6 @@ NSString *const kVLCMediaUnknown = @"Unknown";
 #pragma mark -
 #pragma mark String utility
 
-/* i_width is in pixels */
-- (NSString *)wrapString:(NSString *)o_in_string toWidth:(int)i_width
-{
-    NSMutableString *o_wrapped;
-    NSString *o_out_string;
-    NSRange glyphRange, effectiveRange, charRange;
-    NSUInteger glyphIndex;
-    unsigned breaksInserted = 0;
-
-    NSTextStorage *o_storage = [[NSTextStorage alloc] initWithString: o_in_string
-                                                          attributes: [NSDictionary dictionaryWithObjectsAndKeys:
-                                                                       [NSFont labelFontOfSize: 0.0], NSFontAttributeName, nil]];
-    NSLayoutManager *o_layout_manager = [[NSLayoutManager alloc] init];
-    NSTextContainer *o_container = [[NSTextContainer alloc]
-                                    initWithContainerSize: NSMakeSize(i_width, 2000)];
-
-    [o_layout_manager addTextContainer: o_container];
-    [o_storage addLayoutManager: o_layout_manager];
-
-    o_wrapped = [o_in_string mutableCopy];
-    glyphRange = [o_layout_manager glyphRangeForTextContainer: o_container];
-
-    for (glyphIndex = glyphRange.location ; glyphIndex < NSMaxRange(glyphRange) ;
-        glyphIndex += effectiveRange.length) {
-        [o_layout_manager lineFragmentRectForGlyphAtIndex: glyphIndex
-                                           effectiveRange: &effectiveRange];
-        charRange = [o_layout_manager characterRangeForGlyphRange: effectiveRange
-                                                 actualGlyphRange: &effectiveRange];
-        if ([o_wrapped lineRangeForRange:
-            NSMakeRange(charRange.location + breaksInserted, charRange.length)].length > charRange.length) {
-            [o_wrapped insertString: @"\n" atIndex: NSMaxRange(charRange) + breaksInserted];
-            breaksInserted++;
-        }
-    }
-    o_out_string = [NSString stringWithString: o_wrapped];
-
-    return o_out_string;
-}
 
 - (NSString *)getCurrentTimeAsString:(input_thread_t *)p_input negative:(BOOL)b_negative
 {
diff --git a/modules/gui/macosx/prefs_widgets.m b/modules/gui/macosx/prefs_widgets.m
index 7f5a148197..a9429cf245 100644
--- a/modules/gui/macosx/prefs_widgets.m
+++ b/modules/gui/macosx/prefs_widgets.m
@@ -38,6 +38,7 @@
 #include <vlc_actions.h>
 
 #include "VLCMain.h"
+#include "NSString+Helpers.h"
 #include "prefs_widgets.h"
 
 #define CONFIG_ITEM_STRING_LIST (CONFIG_ITEM_STRING + 10)
@@ -926,7 +927,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
         else
             self.viewType = CONFIG_ITEM_STRING;
 
-        o_textfieldTooltip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        o_textfieldTooltip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the label */
         labelString = _NS(p_item->psz_text);
@@ -1003,7 +1004,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
         else
             self.viewType = CONFIG_ITEM_MODULE;
 
-        o_textfieldTooltip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        o_textfieldTooltip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the label */
         labelString = _NS(p_item->psz_text);
@@ -1101,7 +1102,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
     if (self = [super initWithFrame:mainFrame item:p_item]) {
         self.viewType = CONFIG_ITEM_LOADFILE;
 
-        o_itemTooltip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        o_itemTooltip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* is it a directory */
         b_directory = ([self type] == CONFIG_ITEM_DIRECTORY) ? YES : NO;
@@ -1189,7 +1190,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
     if (self = [super initWithFrame:mainFrame item:p_item]) {
         self.viewType = CONFIG_ITEM_MODULE;
 
-        o_popupTooltip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        o_popupTooltip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the label */
         labelString = _NS(p_item->psz_text);
@@ -1321,7 +1322,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
     if (self = [super initWithFrame:mainFrame item:p_item]) {
         self.viewType = CONFIG_ITEM_INTEGER;
 
-        toolTip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        toolTip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the label */
         labelString = _NS((char *)p_item->psz_text);
@@ -1410,7 +1411,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
     if (self = [super initWithFrame:mainFrame item:p_item]) {
         self.viewType = CONFIG_ITEM_STRING_LIST;
 
-        o_textfieldTooltip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        o_textfieldTooltip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the label */
         labelString = _NS(p_item->psz_text);
@@ -1500,7 +1501,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
     if (self = [super initWithFrame: mainFrame item:p_item]) {
         self.viewType = CONFIG_ITEM_RANGED_INTEGER;
 
-        toolTip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        toolTip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the label */
         labelString = _NS(p_item->psz_text);
@@ -1612,7 +1613,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
     if (self = [super initWithFrame:mainFrame item:p_item]) {
         self.viewType = CONFIG_ITEM_INTEGER;
 
-        toolTip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        toolTip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the label */
         labelString = _NS(p_item->psz_text);
@@ -1703,7 +1704,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
     if (self = [super initWithFrame:mainFrame item:p_item]) {
         self.viewType = CONFIG_ITEM_RANGED_INTEGER;
 
-        toolTip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        toolTip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the label */
         labelString = _NS(p_item->psz_text);
@@ -1816,7 +1817,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
 
         labelString = _NS(p_item->psz_text);
 
-        toolTip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        toolTip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the checkbox */
         ADD_CHECKBOX(o_checkbox, mainFrame, 0,
@@ -1859,7 +1860,7 @@ o_textfield = [[NSSecureTextField alloc] initWithFrame: s_rc];              \
     if (self = [super initWithFrame:mainFrame item:p_item]) {
         self.viewType = CONFIG_ITEM_KEY;
 
-        toolTip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+        toolTip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
         /* add the label */
         labelString = _NS(p_item->psz_text);
@@ -2075,7 +2076,7 @@ o_moduleenabled = [NSNumber numberWithBool:NO];\
     mainFrame.origin.y = 0;
     self.frame = mainFrame;
 
-    toolTip = [[VLCStringUtility sharedInstance] wrapString: _NS(p_item->psz_longtext) toWidth: PREFS_WRAP];
+    toolTip = [_NS(p_item->psz_longtext) stringWrappedToWidth:PREFS_WRAP];
 
     /* add the label */
     labelString = _NS((char *)p_item->psz_text);



More information about the vlc-commits mailing list