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/menu_button.h" | 5 #import "chrome/browser/cocoa/menu_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 |
(...skipping 27 matching lines...) Expand all Loading... |
38 } | 38 } |
39 | 39 |
40 - (id)initWithFrame:(NSRect)frameRect { | 40 - (id)initWithFrame:(NSRect)frameRect { |
41 if ((self = [super initWithFrame:frameRect])) | 41 if ((self = [super initWithFrame:frameRect])) |
42 [self resetToDefaults]; | 42 [self resetToDefaults]; |
43 return self; | 43 return self; |
44 } | 44 } |
45 | 45 |
46 // Accessors and mutators: | 46 // Accessors and mutators: |
47 | 47 |
48 @synthesize menu = menu_; | 48 @synthesize attachedMenu = attachedMenu_; |
49 | 49 |
50 @end // @implementation MenuButton | 50 @end // @implementation MenuButton |
51 | 51 |
52 @implementation MenuButton (Private) | 52 @implementation MenuButton (Private) |
53 | 53 |
54 // Reset various settings of the button and its associated |ClickHoldButtonCell| | 54 // Reset various settings of the button and its associated |ClickHoldButtonCell| |
55 // to the standard state which provides reasonable defaults. | 55 // to the standard state which provides reasonable defaults. |
56 - (void)resetToDefaults { | 56 - (void)resetToDefaults { |
57 ClickHoldButtonCell* cell = [self cell]; | 57 ClickHoldButtonCell* cell = [self cell]; |
58 DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]); | 58 DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]); |
59 [cell setEnableClickHold:YES]; | 59 [cell setEnableClickHold:YES]; |
60 [cell setClickHoldTimeout:0.0]; // Make menu trigger immediately. | 60 [cell setClickHoldTimeout:0.0]; // Make menu trigger immediately. |
61 [cell setAction:@selector(clickShowMenu:)]; | 61 [cell setAction:@selector(clickShowMenu:)]; |
62 [cell setTarget:self]; | 62 [cell setTarget:self]; |
63 [cell setClickHoldAction:@selector(dragShowMenu:)]; | 63 [cell setClickHoldAction:@selector(dragShowMenu:)]; |
64 [cell setClickHoldTarget:self]; | 64 [cell setClickHoldTarget:self]; |
65 } | 65 } |
66 | 66 |
67 // Actually show the menu (in the correct location). |isDragging| indicates | 67 // Actually show the menu (in the correct location). |isDragging| indicates |
68 // whether the mouse button is still down or not. | 68 // whether the mouse button is still down or not. |
69 - (void)showMenu:(BOOL)isDragging { | 69 - (void)showMenu:(BOOL)isDragging { |
70 if (!menu_) { | 70 if (![self attachedMenu]) { |
71 LOG(WARNING) << "No menu available."; | 71 LOG(WARNING) << "No menu available."; |
72 if (isDragging) { | 72 if (isDragging) { |
73 // If we're dragging, wait for mouse up. | 73 // If we're dragging, wait for mouse up. |
74 [NSApp nextEventMatchingMask:NSLeftMouseUpMask | 74 [NSApp nextEventMatchingMask:NSLeftMouseUpMask |
75 untilDate:[NSDate distantFuture] | 75 untilDate:[NSDate distantFuture] |
76 inMode:NSEventTrackingRunLoopMode | 76 inMode:NSEventTrackingRunLoopMode |
77 dequeue:YES]; | 77 dequeue:YES]; |
78 } | 78 } |
79 return; | 79 return; |
80 } | 80 } |
81 | 81 |
82 // FIXME(viettrungluu): Silly fudge factors (same as in | 82 // FIXME(viettrungluu): Silly fudge factors (same as in |
83 // delayedmenu_button.mm). | 83 // delayedmenu_button.mm). |
84 NSRect frame = [self convertRect:[self frame] | 84 NSRect frame = [self convertRect:[self frame] |
85 fromView:[self superview]]; | 85 fromView:[self superview]]; |
86 frame.origin.x -= 2.0; | 86 frame.origin.x -= 2.0; |
87 frame.size.height += 10.0; | 87 frame.size.height += 10.0; |
88 | 88 |
89 // Make our pop-up button cell and set things up. This is, as of 10.5, the | 89 // Make our pop-up button cell and set things up. This is, as of 10.5, the |
90 // official Apple-recommended hack. Later, perhaps |-[NSMenu | 90 // official Apple-recommended hack. Later, perhaps |-[NSMenu |
91 // popUpMenuPositioningItem:atLocation:inView:]| may be a better option. | 91 // popUpMenuPositioningItem:atLocation:inView:]| may be a better option. |
92 // However, using a pulldown has the benefit that Cocoa automatically places | 92 // However, using a pulldown has the benefit that Cocoa automatically places |
93 // the menu correctly even when we're at the edge of the screen (including | 93 // the menu correctly even when we're at the edge of the screen (including |
94 // "dragging upwards" when the button is close to the bottom of the screen). | 94 // "dragging upwards" when the button is close to the bottom of the screen). |
95 scoped_nsobject<NSPopUpButtonCell> popUpCell( | 95 scoped_nsobject<NSPopUpButtonCell> popUpCell( |
96 [[NSPopUpButtonCell alloc] initTextCell:@"" | 96 [[NSPopUpButtonCell alloc] initTextCell:@"" |
97 pullsDown:YES]); | 97 pullsDown:YES]); |
98 DCHECK(popUpCell.get()); | 98 DCHECK(popUpCell.get()); |
99 [popUpCell setMenu:menu_]; | 99 [popUpCell setMenu:[self attachedMenu]]; |
100 [popUpCell selectItem:nil]; | 100 [popUpCell selectItem:nil]; |
101 [popUpCell attachPopUpWithFrame:frame | 101 [popUpCell attachPopUpWithFrame:frame |
102 inView:self]; | 102 inView:self]; |
103 [popUpCell performClickWithFrame:frame | 103 [popUpCell performClickWithFrame:frame |
104 inView:self]; | 104 inView:self]; |
105 } | 105 } |
106 | 106 |
107 // Called when the button is clicked and released. (Shouldn't happen with | 107 // Called when the button is clicked and released. (Shouldn't happen with |
108 // timeout of 0, though there may be some strange pointing devices out there.) | 108 // timeout of 0, though there may be some strange pointing devices out there.) |
109 - (void)clickShowMenu:(id)sender { | 109 - (void)clickShowMenu:(id)sender { |
110 [self showMenu:NO]; | 110 [self showMenu:NO]; |
111 } | 111 } |
112 | 112 |
113 // Called when the button is clicked and dragged/held. | 113 // Called when the button is clicked and dragged/held. |
114 - (void)dragShowMenu:(id)sender { | 114 - (void)dragShowMenu:(id)sender { |
115 [self showMenu:YES]; | 115 [self showMenu:YES]; |
116 } | 116 } |
117 | 117 |
118 @end // @implementation MenuButton (Private) | 118 @end // @implementation MenuButton (Private) |
OLD | NEW |