Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / tabs / tab_view.mm
index 8bb0a13..1cf99d4 100644 (file)
@@ -4,17 +4,20 @@
 
 #import "chrome/browser/ui/cocoa/tabs/tab_view.h"
 
+#include "base/i18n/rtl.h"
 #include "base/logging.h"
 #include "base/mac/sdk_forward_declarations.h"
+#include "base/strings/sys_string_conversions.h"
 #include "chrome/browser/themes/theme_service.h"
-#import "chrome/browser/ui/cocoa/nsview_additions.h"
 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h"
 #import "chrome/browser/ui/cocoa/tabs/tab_window_controller.h"
 #import "chrome/browser/ui/cocoa/themed_window.h"
 #import "chrome/browser/ui/cocoa/view_id_util.h"
-#include "grit/generated_resources.h"
+#include "chrome/grit/generated_resources.h"
 #include "grit/theme_resources.h"
+#import "third_party/google_toolbox_for_mac/src/AppKit/GTMFadeTruncatingTextFieldCell.h"
 #import "ui/base/cocoa/nsgraphics_context_additions.h"
+#import "ui/base/cocoa/nsview_additions.h"
 #include "ui/base/l10n/l10n_util.h"
 #include "ui/base/resource/resource_bundle.h"
 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
@@ -70,6 +73,21 @@ const CGFloat kRapidCloseDist = 2.5;
   if (self) {
     controller_ = controller;
     closeButton_ = closeButton;
+
+    // Make a text field for the title, but don't add it as a subview.
+    // We will use the cell to draw the text directly into our layer,
+    // so that we can get font smoothing enabled.
+    titleView_.reset([[NSTextField alloc] init]);
+    [titleView_ setAutoresizingMask:NSViewWidthSizable];
+    base::scoped_nsobject<GTMFadeTruncatingTextFieldCell> labelCell(
+        [[GTMFadeTruncatingTextFieldCell alloc] initTextCell:@"Label"]);
+    [labelCell setControlSize:NSSmallControlSize];
+    CGFloat fontSize = [NSFont systemFontSizeForControlSize:NSSmallControlSize];
+    NSFont* font = [NSFont fontWithName:[[labelCell font] fontName]
+                                   size:fontSize];
+    [labelCell setFont:font];
+    [titleView_ setCell:labelCell];
+    titleViewCell_ = labelCell;
   }
   return self;
 }
@@ -329,8 +347,9 @@ const CGFloat kRapidCloseDist = 2.5;
   CGRect maskBounds = CGRectMake(0, 0, maskCacheWidth_, kMaskHeight);
   CGContextClipToMask(cgContext, maskBounds, mask);
 
-  bool selected = [self state];
-  if (selected) {
+  // There is only 1 active tab at a time.
+  // It has a different fill color which draws over the separator line.
+  if (state_ == NSOnState) {
     [self drawFillForActiveTab:dirtyRect];
     return;
   }
@@ -340,6 +359,14 @@ const CGFloat kRapidCloseDist = 2.5;
   if (dirtyRect.origin.y < 1)
     dirtyRect.origin.y = 2 * [self cr_lineWidth];
 
+  // There can be multiple selected tabs.
+  // They have the same fill color as the active tab, but do not draw over
+  // the separator.
+  if (state_ == NSMixedState) {
+    [self drawFillForActiveTab:dirtyRect];
+    return;
+  }
+
   // Draw the tab background.
   NSColor* backgroundImageColor = [self backgroundColorForSelected:NO];
   [backgroundImageColor set];
@@ -405,7 +432,7 @@ const CGFloat kRapidCloseDist = 2.5;
   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
   float height =
       [rb.GetNativeImageNamed(IDR_TAB_ACTIVE_LEFT).ToNSImage() size].height;
-  if ([controller_ active]) {
+  if (state_ == NSOnState) {
     NSDrawThreePartImage(NSMakeRect(0, 0, NSWidth([self bounds]), height),
         rb.GetNativeImageNamed(IDR_TAB_ACTIVE_LEFT).ToNSImage(),
         rb.GetNativeImageNamed(IDR_TAB_ACTIVE_CENTER).ToNSImage(),
@@ -427,9 +454,20 @@ const CGFloat kRapidCloseDist = 2.5;
 }
 
 - (void)drawRect:(NSRect)dirtyRect {
-  // Text, close button, and image are drawn by subviews.
+  // Close button and image are drawn by subviews.
   [self drawFill:dirtyRect];
   [self drawStroke:dirtyRect];
+
+  // We draw the title string directly instead of using a NSTextField subview.
+  // This is so that we can get font smoothing to work on earlier OS, and even
+  // when the tab background is a pattern image (when using themes).
+  if (![titleView_ isHidden]) {
+    gfx::ScopedNSGraphicsContextSaveGState scopedGState;
+    NSGraphicsContext* context = [NSGraphicsContext currentContext];
+    CGContextRef cgContext = static_cast<CGContextRef>([context graphicsPort]);
+    CGContextSetShouldSmoothFonts(cgContext, true);
+    [[titleView_ cell] drawWithFrame:[titleView_ frame] inView:self];
+  }
 }
 
 - (void)setFrameOrigin:(NSPoint)origin {
@@ -458,6 +496,49 @@ const CGFloat kRapidCloseDist = 2.5;
   }
 }
 
+- (NSString*)title {
+  return [titleView_ stringValue];
+}
+
+- (void)setTitle:(NSString*)title {
+  [titleView_ setStringValue:title];
+
+  base::string16 title16 = base::SysNSStringToUTF16(title);
+  bool isRTL = base::i18n::GetFirstStrongCharacterDirection(title16) ==
+               base::i18n::RIGHT_TO_LEFT;
+  titleViewCell_.truncateMode = isRTL ? GTMFadeTruncatingHead
+                                      : GTMFadeTruncatingTail;
+
+  [self setNeedsDisplayInRect:[titleView_ frame]];
+}
+
+- (NSRect)titleFrame {
+  return [titleView_ frame];
+}
+
+- (void)setTitleFrame:(NSRect)titleFrame {
+  [titleView_ setFrame:titleFrame];
+  [self setNeedsDisplayInRect:titleFrame];
+}
+
+- (NSColor*)titleColor {
+  return [titleView_ textColor];
+}
+
+- (void)setTitleColor:(NSColor*)titleColor {
+  [titleView_ setTextColor:titleColor];
+  [self setNeedsDisplayInRect:[titleView_ frame]];
+}
+
+- (BOOL)titleHidden {
+  return [titleView_ isHidden];
+}
+
+- (void)setTitleHidden:(BOOL)titleHidden {
+  [titleView_ setHidden:titleHidden];
+  [self setNeedsDisplayInRect:[titleView_ frame]];
+}
+
 - (void)setState:(NSCellStateValue)state {
   if (state_ == state)
     return;