1 // Copyright (c) 2012 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.
5 #import "chrome/browser/ui/cocoa/download/download_item_button.h"
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
9 #import "chrome/browser/ui/cocoa/download/download_item_cell.h"
10 #import "chrome/browser/ui/cocoa/download/download_item_controller.h"
11 #import "chrome/browser/ui/cocoa/download/download_shelf_context_menu_controller.h"
12 #import "chrome/browser/ui/cocoa/nsview_additions.h"
13 #import "chrome/browser/ui/cocoa/view_id_util.h"
15 @implementation DownloadItemButton
17 @synthesize download = downloadPath_;
18 @synthesize controller = controller_;
20 // Overridden from DraggableButton.
21 - (void)beginDrag:(NSEvent*)event {
22 if (!downloadPath_.empty()) {
23 NSString* filename = base::SysUTF8ToNSString(downloadPath_.value());
24 [self dragFile:filename fromRect:[self bounds] slideBack:YES event:event];
28 // Override to show a context menu on mouse down if clicked over the context
30 - (void)mouseDown:(NSEvent*)event {
32 // Override so that we can pop up a context menu on mouse down.
33 NSCell* cell = [self cell];
34 DCHECK([cell respondsToSelector:@selector(isMouseOverButtonPart)]);
35 if ([reinterpret_cast<DownloadItemCell*>(cell) isMouseOverButtonPart]) {
36 [self.draggableButton mouseDownImpl:event];
38 base::scoped_nsobject<DownloadShelfContextMenuController> menuController(
39 [[DownloadShelfContextMenuController alloc]
40 initWithItemController:controller_
43 [cell setHighlighted:YES];
44 [NSMenu popUpContextMenu:[menuController menu]
45 withEvent:[NSApp currentEvent]
50 // Override to retain the controller, in case a closure is pumped that deletes
51 // the DownloadItemController while the menu is open <http://crbug.com/129826>.
52 - (void)rightMouseDown:(NSEvent*)event {
53 base::scoped_nsobject<DownloadItemController> ref([controller_ retain]);
54 [super rightMouseDown:event];
57 - (void)menuDidClose:(NSMenu*)menu {
58 [[self cell] setHighlighted:NO];
61 - (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent*)event {
66 // Make this control opaque so that sub-pixel anti-aliasing works when
67 // CoreAnimation is enabled.
71 - (void)drawRect:(NSRect)rect {
72 NSView* downloadShelfView = [self ancestorWithViewID:VIEW_ID_DOWNLOAD_SHELF];
73 [self cr_drawUsingAncestor:downloadShelfView inRect:rect];
74 [super drawRect:rect];