OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/cocoa/delayedmenu_button.h" | 5 #import "chrome/browser/cocoa/delayedmenu_button.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/scoped_nsobject.h" | 8 #include "base/scoped_nsobject.h" |
9 #import "chrome/browser/cocoa/clickhold_button_cell.h" | 9 #import "chrome/browser/cocoa/clickhold_button_cell.h" |
10 | 10 |
11 @interface DelayedMenuButton (Private) | 11 @interface DelayedMenuButton (Private) |
12 | 12 |
13 - (void)setupCell; | 13 - (void)setupCell; |
14 - (void)menuAction:(id)sender; | 14 - (void)attachedMenuAction:(id)sender; |
15 | 15 |
16 @end // @interface DelayedMenuButton (Private) | 16 @end // @interface DelayedMenuButton (Private) |
17 | 17 |
18 @implementation DelayedMenuButton | 18 @implementation DelayedMenuButton |
19 | 19 |
20 // Overrides: | 20 // Overrides: |
21 | 21 |
22 + (Class)cellClass { | 22 + (Class)cellClass { |
23 return [ClickHoldButtonCell class]; | 23 return [ClickHoldButtonCell class]; |
24 } | 24 } |
(...skipping 10 matching lines...) Expand all Loading... |
35 return self; | 35 return self; |
36 } | 36 } |
37 | 37 |
38 - (id)initWithFrame:(NSRect)frameRect { | 38 - (id)initWithFrame:(NSRect)frameRect { |
39 if ((self = [super initWithFrame:frameRect])) | 39 if ((self = [super initWithFrame:frameRect])) |
40 [self setupCell]; | 40 [self setupCell]; |
41 return self; | 41 return self; |
42 } | 42 } |
43 | 43 |
44 - (void)dealloc { | 44 - (void)dealloc { |
45 [menu_ release]; | 45 [attachedMenu_ release]; |
46 [super dealloc]; | 46 [super dealloc]; |
47 } | 47 } |
48 | 48 |
49 - (void)awakeFromNib { | 49 - (void)awakeFromNib { |
50 [self setupCell]; | 50 [self setupCell]; |
51 } | 51 } |
52 | 52 |
53 - (void)setCell:(NSCell*)cell { | 53 - (void)setCell:(NSCell*)cell { |
54 [super setCell:cell]; | 54 [super setCell:cell]; |
55 [self setupCell]; | 55 [self setupCell]; |
56 } | 56 } |
57 | 57 |
58 // Accessors and mutators: | 58 // Accessors and mutators: |
59 | 59 |
60 @synthesize menu = menu_; | 60 @synthesize attachedMenu = attachedMenu_; |
61 | 61 |
62 // Don't synthesize for menuEnabled_; its mutator must do other things. | 62 // Don't synthesize for attachedMenuEnabled_; its mutator must do other things. |
63 - (void)setMenuEnabled:(BOOL)enabled { | 63 - (void)setAttachedMenuEnabled:(BOOL)enabled { |
64 menuEnabled_ = enabled; | 64 attachedMenuEnabled_ = enabled; |
65 [[self cell] setEnableClickHold:menuEnabled_]; | 65 [[self cell] setEnableClickHold:attachedMenuEnabled_]; |
66 } | 66 } |
67 | 67 |
68 - (BOOL)menuEnabled { | 68 - (BOOL)attachedMenuEnabled { |
69 return menuEnabled_; | 69 return attachedMenuEnabled_; |
70 } | 70 } |
71 | 71 |
72 @end // @implementation DelayedMenuButton | 72 @end // @implementation DelayedMenuButton |
73 | 73 |
74 @implementation DelayedMenuButton (Private) | 74 @implementation DelayedMenuButton (Private) |
75 | 75 |
76 // Set up the button's cell if we've reached a point where it's been set. | 76 // Set up the button's cell if we've reached a point where it's been set. |
77 - (void)setupCell { | 77 - (void)setupCell { |
78 ClickHoldButtonCell* cell = [self cell]; | 78 ClickHoldButtonCell* cell = [self cell]; |
79 if (cell) { | 79 if (cell) { |
80 DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]); | 80 DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]); |
81 [self setEnabled:NO]; // Make the controller put in a menu and | 81 [self setEnabled:NO]; // Make the controller put in a menu and |
82 // enable it explicitly. This also takes | 82 // enable it explicitly. This also takes |
83 // care of |[cell setEnableClickHold:]|. | 83 // care of |[cell setEnableClickHold:]|. |
84 [cell setClickHoldAction:@selector(menuAction:)]; | 84 [cell setClickHoldAction:@selector(attachedMenuAction:)]; |
85 [cell setClickHoldTarget:self]; | 85 [cell setClickHoldTarget:self]; |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 // Display the menu. | 89 // Display the menu. |
90 - (void)menuAction:(id)sender { | 90 - (void)attachedMenuAction:(id)sender { |
91 // We shouldn't get here unless the menu is enabled. | 91 // We shouldn't get here unless the menu is enabled. |
92 DCHECK(menuEnabled_); | 92 DCHECK(attachedMenuEnabled_); |
93 | 93 |
94 // If we don't have a menu (in which case the person using this control is | 94 // If we don't have a menu (in which case the person using this control is |
95 // being bad), just wait for a mouse up. | 95 // being bad), just wait for a mouse up. |
96 if (!menu_) { | 96 if (!attachedMenu_) { |
97 LOG(WARNING) << "No menu available."; | 97 LOG(WARNING) << "No menu available."; |
98 [NSApp nextEventMatchingMask:NSLeftMouseUpMask | 98 [NSApp nextEventMatchingMask:NSLeftMouseUpMask |
99 untilDate:[NSDate distantFuture] | 99 untilDate:[NSDate distantFuture] |
100 inMode:NSEventTrackingRunLoopMode | 100 inMode:NSEventTrackingRunLoopMode |
101 dequeue:YES]; | 101 dequeue:YES]; |
102 return; | 102 return; |
103 } | 103 } |
104 | 104 |
105 // FIXME(viettrungluu): We have some fudge factors below to make things line | 105 // FIXME(viettrungluu): We have some fudge factors below to make things line |
106 // up (approximately). I wish I knew how to get rid of them. (Note that our | 106 // up (approximately). I wish I knew how to get rid of them. (Note that our |
107 // view is flipped, and that frame should be in our coordinates.) The y/height | 107 // view is flipped, and that frame should be in our coordinates.) The y/height |
108 // is very odd, since it doesn't seem to respond to changes the way that it | 108 // is very odd, since it doesn't seem to respond to changes the way that it |
109 // should. I don't understand it. | 109 // should. I don't understand it. |
110 NSRect frame = [self convertRect:[self frame] | 110 NSRect frame = [self convertRect:[self frame] |
111 fromView:[self superview]]; | 111 fromView:[self superview]]; |
112 frame.origin.x -= 2.0; | 112 frame.origin.x -= 2.0; |
113 frame.size.height += 10.0; | 113 frame.size.height += 10.0; |
114 | 114 |
115 // Make our pop-up button cell and set things up. This is, as of 10.5, the | 115 // Make our pop-up button cell and set things up. This is, as of 10.5, the |
116 // official Apple-recommended hack. Later, perhaps |-[NSMenu | 116 // official Apple-recommended hack. Later, perhaps |-[NSMenu |
117 // popUpMenuPositioningItem:atLocation:inView:]| may be a better option. | 117 // popUpMenuPositioningItem:atLocation:inView:]| may be a better option. |
118 // However, using a pulldown has the benefit that Cocoa automatically places | 118 // However, using a pulldown has the benefit that Cocoa automatically places |
119 // the menu correctly even when we're at the edge of the screen (including | 119 // the menu correctly even when we're at the edge of the screen (including |
120 // "dragging upwards" when the button is close to the bottom of the screen). | 120 // "dragging upwards" when the button is close to the bottom of the screen). |
121 scoped_nsobject<NSPopUpButtonCell> popUpCell( | 121 scoped_nsobject<NSPopUpButtonCell> popUpCell( |
122 [[NSPopUpButtonCell alloc] initTextCell:@"" | 122 [[NSPopUpButtonCell alloc] initTextCell:@"" |
123 pullsDown:YES]); | 123 pullsDown:YES]); |
124 DCHECK(popUpCell.get()); | 124 DCHECK(popUpCell.get()); |
125 [popUpCell setMenu:menu_]; | 125 [popUpCell setMenu:attachedMenu_]; |
126 [popUpCell selectItem:nil]; | 126 [popUpCell selectItem:nil]; |
127 [popUpCell attachPopUpWithFrame:frame | 127 [popUpCell attachPopUpWithFrame:frame |
128 inView:self]; | 128 inView:self]; |
129 [popUpCell performClickWithFrame:frame | 129 [popUpCell performClickWithFrame:frame |
130 inView:self]; | 130 inView:self]; |
131 } | 131 } |
132 | 132 |
133 @end // @implementation DelayedMenuButton (Private) | 133 @end // @implementation DelayedMenuButton (Private) |
OLD | NEW |