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 3 // found in the LICENSE file. 4 5 #include <cmath> 6 7 #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h" 8 9 #include "base/sys_string_conversions.h" 10 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" 11 #include "content/browser/tab_contents/tab_contents.h" 12 #include "grit/generated_resources.h" 13 #import "third_party/mozilla/NSPasteboard+Utils.h" 14 #include "ui/base/l10n/l10n_util_mac.h" 15 16 // The info-bubble point should look like it points to the bottom of the lock 17 // icon. Determined with Pixie.app. 18 const CGFloat kBubblePointYOffset = 2.0; 19 20 LocationIconDecoration::LocationIconDecoration(LocationBarViewMac* owner) 21 : owner_(owner) { 22 } 23 24 LocationIconDecoration::~LocationIconDecoration() { 25 } 26 27 bool LocationIconDecoration::IsDraggable() { 28 // Without a tab it will be impossible to get the information needed 29 // to perform a drag. 30 if (!owner_->GetTabContents()) 31 return false; 32 33 // Do not drag if the user has been editing the location bar, or the 34 // location bar is at the NTP. 35 if (owner_->location_entry()->IsEditingOrEmpty()) 36 return false; 37 38 return true; 39 } 40 41 NSPasteboard* LocationIconDecoration::GetDragPasteboard() { 42 TabContents* tab = owner_->GetTabContents(); 43 DCHECK(tab); // See |IsDraggable()|. 44 45 NSString* url = base::SysUTF8ToNSString(tab->GetURL().spec()); 46 NSString* title = base::SysUTF16ToNSString(tab->GetTitle()); 47 48 NSPasteboard* pboard = [NSPasteboard pasteboardWithName:NSDragPboard]; 49 [pboard declareURLPasteboardWithAdditionalTypes:[NSArray array] 50 owner:nil]; 51 [pboard setDataForURL:url title:title]; 52 return pboard; 53 } 54 55 NSImage* LocationIconDecoration::GetDragImage() { 56 return GetImage(); 57 } 58 59 NSRect LocationIconDecoration::GetDragImageFrame(NSRect frame) { 60 return GetDrawRectInFrame(frame); 61 } 62 63 NSPoint LocationIconDecoration::GetBubblePointInFrame(NSRect frame) { 64 const NSRect draw_frame = GetDrawRectInFrame(frame); 65 return NSMakePoint(NSMidX(draw_frame), 66 NSMaxY(draw_frame) - kBubblePointYOffset); 67 } 68 69 bool LocationIconDecoration::AcceptsMousePress() { 70 return true; 71 } 72 73 bool LocationIconDecoration::OnMousePressed(NSRect frame) { 74 // Do not show page info if the user has been editing the location 75 // bar, or the location bar is at the NTP. 76 if (owner_->location_entry()->IsEditingOrEmpty()) 77 return true; 78 79 TabContents* tab = owner_->GetTabContents(); 80 NavigationEntry* nav_entry = tab->controller().GetActiveEntry(); 81 if (!nav_entry) { 82 NOTREACHED(); 83 return true; 84 } 85 tab->ShowPageInfo(nav_entry->url(), nav_entry->ssl(), true); 86 return true; 87 } 88 89 NSString* LocationIconDecoration::GetToolTip() { 90 if (owner_->location_entry()->IsEditingOrEmpty()) 91 return nil; 92 else 93 return l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_LOCATION_ICON); 94 } 95