Upstream version 10.39.225.0
[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 #import "chrome/browser/ui/cocoa/view_id_util.h"
13 #import "ui/base/cocoa/nsview_additions.h"
14
15 @implementation DownloadItemButton
16
17 @synthesize download = downloadPath_;
18 @synthesize controller = controller_;
19
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];
25   }
26 }
27
28 // Override to show a context menu on mouse down if clicked over the context
29 // menu area.
30 - (void)mouseDown:(NSEvent*)event {
31   DCHECK(controller_);
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];
37   } else {
38     base::scoped_nsobject<DownloadShelfContextMenuController> menuController(
39         [[DownloadShelfContextMenuController alloc]
40             initWithItemController:controller_
41                       withDelegate:self]);
42
43     [cell setHighlighted:YES];
44     [NSMenu popUpContextMenu:[menuController menu]
45                    withEvent:[NSApp currentEvent]
46                      forView:self];
47   }
48 }
49
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];
55 }
56
57 - (void)menuDidClose:(NSMenu*)menu {
58   [[self cell] setHighlighted:NO];
59 }
60
61 - (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent*)event {
62   return YES;
63 }
64
65 - (BOOL)isOpaque {
66   // Make this control opaque so that sub-pixel anti-aliasing works when
67   // CoreAnimation is enabled.
68   return YES;
69 }
70
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];
75 }
76
77 @end