| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/drag_util.h" | 5 #import "chrome/browser/ui/cocoa/drag_util.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/i18n/rtl.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/cocoa/l10n_util.h" |
| 13 #include "components/mime_util/mime_util.h" | 15 #include "components/mime_util/mime_util.h" |
| 14 #include "content/public/browser/plugin_service.h" | 16 #include "content/public/browser/plugin_service.h" |
| 15 #include "content/public/common/webplugininfo.h" | 17 #include "content/public/common/webplugininfo.h" |
| 16 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
| 17 #include "net/base/filename_util.h" | 19 #include "net/base/filename_util.h" |
| 18 #include "net/base/mime_util.h" | 20 #include "net/base/mime_util.h" |
| 19 #import "third_party/mozilla/NSPasteboard+Utils.h" | 21 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 20 #import "ui/base/dragdrop/cocoa_dnd_util.h" | 22 #import "ui/base/dragdrop/cocoa_dnd_util.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 24 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 52 return PluginService::GetInstance()->GetPluginInfo( | 54 return PluginService::GetInstance()->GetPluginInfo( |
| 53 -1, // process ID | 55 -1, // process ID |
| 54 MSG_ROUTING_NONE, // routing ID | 56 MSG_ROUTING_NONE, // routing ID |
| 55 profile->GetResourceContext(), url, url::Origin(), mime_type, | 57 profile->GetResourceContext(), url, url::Origin(), mime_type, |
| 56 allow_wildcard, NULL, &plugin, NULL); | 58 allow_wildcard, NULL, &plugin, NULL); |
| 57 } | 59 } |
| 58 | 60 |
| 59 // Draws string |title| within box |frame|, positioning it at the origin. | 61 // Draws string |title| within box |frame|, positioning it at the origin. |
| 60 // Truncates text with fading if it is too long to fit horizontally. | 62 // Truncates text with fading if it is too long to fit horizontally. |
| 61 // Based on code from GradientButtonCell but simplified where possible. | 63 // Based on code from GradientButtonCell but simplified where possible. |
| 62 void DrawTruncatedTitle(NSAttributedString* title, NSRect frame) { | 64 void DrawTruncatedTitle(NSAttributedString* title, |
| 65 NSRect frame, |
| 66 bool is_title_rtl) { |
| 63 NSSize size = [title size]; | 67 NSSize size = [title size]; |
| 64 if (std::floor(size.width) <= NSWidth(frame)) { | 68 if (std::floor(size.width) <= NSWidth(frame)) { |
| 65 [title drawAtPoint:frame.origin]; | 69 [title drawAtPoint:frame.origin]; |
| 66 return; | 70 return; |
| 67 } | 71 } |
| 68 | 72 |
| 69 // Gradient is about twice our line height long. | 73 // The gradient is about twice the line height long. |
| 74 NSRectEdge gradient_edge; |
| 75 if (is_title_rtl) |
| 76 gradient_edge = NSMinXEdge; |
| 77 else |
| 78 gradient_edge = NSMaxXEdge; |
| 79 |
| 70 CGFloat gradient_width = std::min(size.height * 2, NSWidth(frame) / 4); | 80 CGFloat gradient_width = std::min(size.height * 2, NSWidth(frame) / 4); |
| 71 NSRect solid_part, gradient_part; | 81 NSRect solid_part, gradient_part; |
| 72 NSDivideRect(frame, &gradient_part, &solid_part, gradient_width, NSMaxXEdge); | 82 NSDivideRect(frame, &gradient_part, &solid_part, gradient_width, |
| 83 gradient_edge); |
| 73 CGContextRef context = static_cast<CGContextRef>( | 84 CGContextRef context = static_cast<CGContextRef>( |
| 74 [[NSGraphicsContext currentContext] graphicsPort]); | 85 [[NSGraphicsContext currentContext] graphicsPort]); |
| 75 CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0); | 86 CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0); |
| 76 { // Draw text clipped to frame. | 87 { // Draw text clipped to frame. |
| 77 gfx::ScopedNSGraphicsContextSaveGState scoped_state; | 88 gfx::ScopedNSGraphicsContextSaveGState scoped_state; |
| 78 [NSBezierPath clipRect:frame]; | 89 [NSBezierPath clipRect:frame]; |
| 79 [title drawAtPoint:frame.origin]; | 90 [title drawInRect:frame]; |
| 80 } | 91 } |
| 81 | 92 |
| 82 NSColor* color = [NSColor blackColor]; | 93 NSColor* color = [NSColor blackColor]; |
| 83 NSColor* alpha_color = [color colorWithAlphaComponent:0.0]; | 94 NSColor* alpha_color = [color colorWithAlphaComponent:0.0]; |
| 84 base::scoped_nsobject<NSGradient> mask( | 95 base::scoped_nsobject<NSGradient> mask( |
| 85 [[NSGradient alloc] initWithStartingColor:color endingColor:alpha_color]); | 96 [[NSGradient alloc] initWithStartingColor:color endingColor:alpha_color]); |
| 86 // Draw the gradient mask. | 97 // Draw the gradient mask. |
| 87 CGContextSetBlendMode(context, kCGBlendModeDestinationIn); | 98 CGContextSetBlendMode(context, kCGBlendModeDestinationIn); |
| 88 [mask drawFromPoint:NSMakePoint(NSMaxX(frame) - gradient_width, | 99 CGFloat gradient_x_start, gradient_x_end; |
| 89 NSMinY(frame)) | 100 if (is_title_rtl) { |
| 90 toPoint:NSMakePoint(NSMaxX(frame), | 101 gradient_x_start = NSMaxX(gradient_part); |
| 91 NSMinY(frame)) | 102 gradient_x_end = NSMinX(gradient_part); |
| 103 } else { |
| 104 gradient_x_start = NSMinX(gradient_part); |
| 105 gradient_x_end = NSMaxX(gradient_part); |
| 106 } |
| 107 [mask drawFromPoint:NSMakePoint(gradient_x_start, NSMinY(frame)) |
| 108 toPoint:NSMakePoint(gradient_x_end, NSMinY(frame)) |
| 92 options:NSGradientDrawsBeforeStartingLocation]; | 109 options:NSGradientDrawsBeforeStartingLocation]; |
| 93 CGContextEndTransparencyLayer(context); | 110 CGContextEndTransparencyLayer(context); |
| 94 } | 111 } |
| 95 | 112 |
| 96 } // namespace | 113 } // namespace |
| 97 | 114 |
| 98 GURL GetFileURLFromDropData(id<NSDraggingInfo> info) { | 115 GURL GetFileURLFromDropData(id<NSDraggingInfo> info) { |
| 99 if ([[info draggingPasteboard] containsURLDataConvertingTextToURL:YES]) { | 116 if ([[info draggingPasteboard] containsURLDataConvertingTextToURL:YES]) { |
| 100 GURL url; | 117 GURL url; |
| 101 ui::PopulateURLAndTitleFromPasteboard(&url, | 118 ui::PopulateURLAndTitleFromPasteboard(&url, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 114 if (!url.is_empty()) { | 131 if (!url.is_empty()) { |
| 115 // If dragging a file, only allow dropping supported file types (that the | 132 // If dragging a file, only allow dropping supported file types (that the |
| 116 // web view can display). | 133 // web view can display). |
| 117 return !IsSupportedFileURL(profile, url); | 134 return !IsSupportedFileURL(profile, url); |
| 118 } | 135 } |
| 119 return NO; | 136 return NO; |
| 120 } | 137 } |
| 121 | 138 |
| 122 NSImage* DragImageForBookmark(NSImage* favicon, | 139 NSImage* DragImageForBookmark(NSImage* favicon, |
| 123 const base::string16& title, | 140 const base::string16& title, |
| 124 CGFloat title_width) { | 141 CGFloat drag_image_width) { |
| 125 // If no favicon, use a default. | 142 // If no favicon, use a default. |
| 126 if (!favicon) { | 143 if (!favicon) { |
| 127 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 144 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 128 favicon = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); | 145 favicon = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); |
| 129 } | 146 } |
| 130 | 147 |
| 131 // If no title, just use icon. | 148 // If no title, just use icon. |
| 132 if (title.empty()) | 149 if (title.empty()) |
| 133 return favicon; | 150 return favicon; |
| 134 NSString* ns_title = base::SysUTF16ToNSString(title); | 151 NSString* ns_title = base::SysUTF16ToNSString(title); |
| 135 | 152 |
| 136 // Set the look of the title. | 153 // Set the look of the title. |
| 137 NSDictionary* attrs = | 154 NSDictionary* attrs = |
| 138 [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize: | 155 [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize: |
| 139 [NSFont smallSystemFontSize]] | 156 [NSFont smallSystemFontSize]] |
| 140 forKey:NSFontAttributeName]; | 157 forKey:NSFontAttributeName]; |
| 141 base::scoped_nsobject<NSAttributedString> rich_title( | 158 base::scoped_nsobject<NSAttributedString> rich_title( |
| 142 [[NSAttributedString alloc] initWithString:ns_title attributes:attrs]); | 159 [[NSAttributedString alloc] initWithString:ns_title attributes:attrs]); |
| 143 | 160 |
| 144 // Set up sizes and locations for rendering. | 161 // Set up sizes and locations for rendering. |
| 145 const CGFloat kIconMargin = 2.0; // Gap between icon and text. | 162 const CGFloat kIconPadding = 2.0; // Gap between icon and text. |
| 146 CGFloat text_left = [favicon size].width + kIconMargin; | 163 NSRect favicon_rect = {NSZeroPoint, [favicon size]}; |
| 147 NSSize drag_image_size = [favicon size]; | 164 CGFloat icon_plus_padding_width = NSWidth(favicon_rect) + kIconPadding; |
| 148 NSSize text_size = [rich_title size]; | 165 CGFloat full_text_width = [rich_title size].width; |
| 149 CGFloat max_text_width = title_width - text_left; | 166 CGFloat allowed_text_width = drag_image_width - icon_plus_padding_width; |
| 150 text_size.width = std::min(text_size.width, max_text_width); | 167 CGFloat used_text_width = std::min(full_text_width, allowed_text_width); |
| 151 drag_image_size.width = text_left + text_size.width; | 168 NSRect full_drag_image_rect = NSMakeRect( |
| 169 0, 0, icon_plus_padding_width + used_text_width, NSHeight(favicon_rect)); |
| 170 |
| 171 NSRectEdge icon_edge; |
| 172 if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) |
| 173 icon_edge = NSMaxXEdge; |
| 174 else |
| 175 icon_edge = NSMinXEdge; |
| 176 |
| 177 NSRect icon_rect; |
| 178 NSRect text_plus_padding_rect; |
| 179 NSRect padding_rect; |
| 180 NSRect text_rect; |
| 181 |
| 182 // Slice off the icon. |
| 183 NSDivideRect(full_drag_image_rect, &icon_rect, &text_plus_padding_rect, |
| 184 NSWidth(favicon_rect), icon_edge); |
| 185 |
| 186 // Slice off the padding. |
| 187 NSDivideRect(text_plus_padding_rect, &padding_rect, &text_rect, kIconPadding, |
| 188 icon_edge); |
| 152 | 189 |
| 153 // Render the drag image. | 190 // Render the drag image. |
| 154 NSImage* drag_image = | 191 NSImage* drag_image = |
| 155 [[[NSImage alloc] initWithSize:drag_image_size] autorelease]; | 192 [[[NSImage alloc] initWithSize:full_drag_image_rect.size] autorelease]; |
| 156 [drag_image lockFocus]; | 193 [drag_image lockFocus]; |
| 157 [favicon drawAtPoint:NSZeroPoint | 194 [favicon drawAtPoint:icon_rect.origin |
| 158 fromRect:NSZeroRect | 195 fromRect:NSZeroRect |
| 159 operation:NSCompositeSourceOver | 196 operation:NSCompositeSourceOver |
| 160 fraction:0.7]; | 197 fraction:0.7]; |
| 161 NSRect target_text_rect = NSMakeRect(text_left, 0, | 198 |
| 162 text_size.width, drag_image_size.height); | 199 bool is_title_rtl = base::i18n::GetFirstStrongCharacterDirection(title) == |
| 163 DrawTruncatedTitle(rich_title, target_text_rect); | 200 base::i18n::RIGHT_TO_LEFT; |
| 201 DrawTruncatedTitle(rich_title, text_rect, is_title_rtl); |
| 164 [drag_image unlockFocus]; | 202 [drag_image unlockFocus]; |
| 165 | 203 |
| 166 return drag_image; | 204 return drag_image; |
| 167 } | 205 } |
| 168 | 206 |
| 169 } // namespace drag_util | 207 } // namespace drag_util |
| OLD | NEW |