0a6a7a9b7110aa91bb0035820033be3c3f4ac55f
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / download / download_item_button.mm
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.
4
5 #import "chrome/browser/ui/cocoa/download/download_item_button.h"
6
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
13 @implementation DownloadItemButton
14
15 @synthesize download = downloadPath_;
16 @synthesize controller = controller_;
17
18 // Overridden from DraggableButton.
19 - (void)beginDrag:(NSEvent*)event {
20   if (!downloadPath_.empty()) {
21     NSString* filename = base::SysUTF8ToNSString(downloadPath_.value());
22     [self dragFile:filename fromRect:[self bounds] slideBack:YES event:event];
23   }
24 }
25
26 // Override to show a context menu on mouse down if clicked over the context
27 // menu area.
28 - (void)mouseDown:(NSEvent*)event {
29   DCHECK(controller_);
30   // Override so that we can pop up a context menu on mouse down.
31   NSCell* cell = [self cell];
32   DCHECK([cell respondsToSelector:@selector(isMouseOverButtonPart)]);
33   if ([reinterpret_cast<DownloadItemCell*>(cell) isMouseOverButtonPart]) {
34     [self.draggableButton mouseDownImpl:event];
35   } else {
36     base::scoped_nsobject<DownloadShelfContextMenuController> menuController(
37         [[DownloadShelfContextMenuController alloc]
38             initWithItemController:controller_
39                       withDelegate:self]);
40
41     [cell setHighlighted:YES];
42     [NSMenu popUpContextMenu:[menuController menu]
43                    withEvent:[NSApp currentEvent]
44                      forView:self];
45   }
46 }
47
48 // Override to retain the controller, in case a closure is pumped that deletes
49 // the DownloadItemController while the menu is open <http://crbug.com/129826>.
50 - (void)rightMouseDown:(NSEvent*)event {
51   base::scoped_nsobject<DownloadItemController> ref([controller_ retain]);
52   [super rightMouseDown:event];
53 }
54
55 - (void)menuDidClose:(NSMenu*)menu {
56   [[self cell] setHighlighted:NO];
57 }
58
59 - (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent*)event {
60   return YES;
61 }
62
63 @end