| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <cmath> |
| 6 |
| 7 #import "chrome/browser/cocoa/location_bar/location_icon_decoration.h" |
| 8 |
| 9 #include "base/sys_string_conversions.h" |
| 10 #import "chrome/browser/cocoa/image_utils.h" |
| 11 #import "chrome/browser/cocoa/location_bar/location_bar_view_mac.h" |
| 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 13 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 14 |
| 15 LocationIconDecoration::LocationIconDecoration(LocationBarViewMac* owner) |
| 16 : owner_(owner) { |
| 17 } |
| 18 LocationIconDecoration::~LocationIconDecoration() { |
| 19 } |
| 20 |
| 21 NSImage* LocationIconDecoration::GetImage() { |
| 22 return image_; |
| 23 } |
| 24 |
| 25 void LocationIconDecoration::SetImage(NSImage* image) { |
| 26 image_.reset([image retain]); |
| 27 } |
| 28 |
| 29 CGFloat LocationIconDecoration::GetWidthForSpace(CGFloat width) { |
| 30 NSImage* image = GetImage(); |
| 31 if (image) { |
| 32 const CGFloat image_width = [image size].width; |
| 33 if (image_width >= width) |
| 34 return image_width; |
| 35 } |
| 36 return kOmittedWidth; |
| 37 } |
| 38 |
| 39 void LocationIconDecoration::DrawInFrame(NSRect frame, NSView* control_view) { |
| 40 NSImage* image = GetImage(); |
| 41 const CGFloat delta_height = NSHeight(frame) - [image size].height; |
| 42 const CGFloat y_inset = std::floor(delta_height / 2.0); |
| 43 [image drawInRect:NSInsetRect(frame, 0.0, y_inset) |
| 44 fromRect:NSZeroRect // Entire image |
| 45 operation:NSCompositeSourceOver |
| 46 fraction:1.0 |
| 47 neverFlipped:YES]; |
| 48 } |
| 49 |
| 50 bool LocationIconDecoration::IsDraggable() { |
| 51 // Without a tab it will be impossible to get the information needed |
| 52 // to perform a drag. |
| 53 if (!owner_->GetTabContents()) |
| 54 return false; |
| 55 |
| 56 // Do not drag if the user has been editing the location bar, or the |
| 57 // location bar is at the NTP. |
| 58 if (owner_->location_entry()->IsEditingOrEmpty()) |
| 59 return false; |
| 60 |
| 61 return true; |
| 62 } |
| 63 |
| 64 NSPasteboard* LocationIconDecoration::GetDragPasteboard() { |
| 65 TabContents* tab = owner_->GetTabContents(); |
| 66 DCHECK(tab); // See |IsDraggable()|. |
| 67 |
| 68 NSString* url = base::SysUTF8ToNSString(tab->GetURL().spec()); |
| 69 NSString* title = base::SysUTF16ToNSString(tab->GetTitle()); |
| 70 |
| 71 NSPasteboard* pboard = [NSPasteboard pasteboardWithName:NSDragPboard]; |
| 72 [pboard declareURLPasteboardWithAdditionalTypes:[NSArray array] |
| 73 owner:nil]; |
| 74 [pboard setDataForURL:url title:title]; |
| 75 return pboard; |
| 76 } |
| 77 |
| 78 bool LocationIconDecoration::OnMousePressed(NSRect frame) { |
| 79 // Do not show page info if the user has been editing the location |
| 80 // bar, or the location bar is at the NTP. |
| 81 if (owner_->location_entry()->IsEditingOrEmpty()) |
| 82 return true; |
| 83 |
| 84 TabContents* tab = owner_->GetTabContents(); |
| 85 NavigationEntry* nav_entry = tab->controller().GetActiveEntry(); |
| 86 if (!nav_entry) { |
| 87 NOTREACHED(); |
| 88 return true; |
| 89 } |
| 90 tab->ShowPageInfo(nav_entry->url(), nav_entry->ssl(), true); |
| 91 return true; |
| 92 } |
| OLD | NEW |