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/toolbar_controller.h" | 5 #import "chrome/browser/cocoa/toolbar_controller.h" |
6 | 6 |
7 #include "base/mac_util.h" | 7 #include "base/mac_util.h" |
8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
9 #include "chrome/app/chrome_dll_resource.h" | 9 #include "chrome/app/chrome_dll_resource.h" |
10 #import "chrome/browser/cocoa/location_bar_view_mac.h" | 10 #import "chrome/browser/cocoa/location_bar_view_mac.h" |
11 #include "chrome/browser/profile.h" | |
11 #include "chrome/browser/toolbar_model.h" | 12 #include "chrome/browser/toolbar_model.h" |
13 #include "chrome/common/notification_details.h" | |
14 #include "chrome/common/notification_observer.h" | |
15 #include "chrome/common/notification_type.h" | |
16 #include "chrome/common/pref_names.h" | |
17 #include "chrome/common/pref_service.h" | |
12 | 18 |
13 // Names of images in the bundle for the star icon (normal and 'starred'). | 19 // Names of images in the bundle for the star icon (normal and 'starred'). |
14 static NSString* const kStarImageName = @"star"; | 20 static NSString* const kStarImageName = @"star"; |
15 static NSString* const kStarredImageName = @"starred"; | 21 static NSString* const kStarredImageName = @"starred"; |
16 | 22 |
17 @implementation LocationBarFieldEditor | 23 @implementation LocationBarFieldEditor |
18 - (void)copy:(id)sender { | 24 - (void)copy:(id)sender { |
19 NSPasteboard* pb = [NSPasteboard generalPasteboard]; | 25 NSPasteboard* pb = [NSPasteboard generalPasteboard]; |
20 [self performCopy:pb]; | 26 [self performCopy:pb]; |
21 } | 27 } |
(...skipping 11 matching lines...) Expand all Loading... | |
33 | 39 |
34 - (void)performCut:(NSPasteboard*)pb { | 40 - (void)performCut:(NSPasteboard*)pb { |
35 [self performCopy:pb]; | 41 [self performCopy:pb]; |
36 [self delete:nil]; | 42 [self delete:nil]; |
37 } | 43 } |
38 | 44 |
39 @end | 45 @end |
40 | 46 |
41 @interface ToolbarController(Private) | 47 @interface ToolbarController(Private) |
42 - (void)initCommandStatus:(CommandUpdater*)commands; | 48 - (void)initCommandStatus:(CommandUpdater*)commands; |
49 - (void)prefChanged:(std::wstring*)prefName; | |
43 @end | 50 @end |
44 | 51 |
52 namespace ToolbarControllerInternal { | |
53 | |
54 // A C++ class registered for changes in preferences. Bridges the | |
55 // notification back to the BWC. | |
rohitrao (ping after 24h)
2009/07/07 18:10:46
s/BWC/ToolbarController/ ?
pink (ping after 24hrs)
2009/07/07 18:12:17
Done.
| |
56 class PrefObserverBridge : public NotificationObserver { | |
57 public: | |
58 PrefObserverBridge(ToolbarController* controller) | |
59 : controller_(controller) { } | |
60 // Overridden from NotificationObserver: | |
61 virtual void Observe(NotificationType type, | |
62 const NotificationSource& source, | |
63 const NotificationDetails& details) { | |
64 if (type == NotificationType::PREF_CHANGED) | |
65 [controller_ prefChanged:Details<std::wstring>(details).ptr()]; | |
66 } | |
67 private: | |
68 ToolbarController* controller_; // weak, owns us | |
69 }; | |
70 | |
71 } // namespace | |
72 | |
45 @implementation ToolbarController | 73 @implementation ToolbarController |
46 | 74 |
47 - (id)initWithModel:(ToolbarModel*)model | 75 - (id)initWithModel:(ToolbarModel*)model |
48 commands:(CommandUpdater*)commands | 76 commands:(CommandUpdater*)commands |
49 profile:(Profile*)profile { | 77 profile:(Profile*)profile { |
50 DCHECK(model && commands && profile); | 78 DCHECK(model && commands && profile); |
51 if ((self = [super initWithNibName:@"Toolbar" | 79 if ((self = [super initWithNibName:@"Toolbar" |
52 bundle:mac_util::MainAppBundle()])) { | 80 bundle:mac_util::MainAppBundle()])) { |
53 toolbarModel_ = model; | 81 toolbarModel_ = model; |
54 commands_ = commands; | 82 commands_ = commands; |
(...skipping 11 matching lines...) Expand all Loading... | |
66 } | 94 } |
67 | 95 |
68 // Called after the view is done loading and the outlets have been hooked up. | 96 // Called after the view is done loading and the outlets have been hooked up. |
69 // Now we can hook up bridges that rely on UI objects such as the location | 97 // Now we can hook up bridges that rely on UI objects such as the location |
70 // bar and button state. | 98 // bar and button state. |
71 - (void)awakeFromNib { | 99 - (void)awakeFromNib { |
72 [self initCommandStatus:commands_]; | 100 [self initCommandStatus:commands_]; |
73 locationBarView_.reset(new LocationBarViewMac(locationBar_, commands_, | 101 locationBarView_.reset(new LocationBarViewMac(locationBar_, commands_, |
74 toolbarModel_, profile_)); | 102 toolbarModel_, profile_)); |
75 [locationBar_ setFont:[NSFont systemFontOfSize:[NSFont systemFontSize]]]; | 103 [locationBar_ setFont:[NSFont systemFontOfSize:[NSFont systemFontSize]]]; |
104 | |
105 // Register pref observers for the optional home and page/options buttons | |
106 // and then add them to the toolbar them based on those prefs. | |
107 prefObserver_.reset(new ToolbarControllerInternal::PrefObserverBridge(self)); | |
108 PrefService* prefs = profile_->GetPrefs(); | |
109 showHomeButton_.Init(prefs::kShowHomeButton, prefs, prefObserver_.get()); | |
110 showPageOptionButtons_.Init(prefs::kShowPageOptionsButtons, prefs, | |
111 prefObserver_.get()); | |
112 [self showOptionalHomeButton]; | |
113 [self showOptionalPageWrenchButtons]; | |
76 } | 114 } |
77 | 115 |
78 - (LocationBar*)locationBar { | 116 - (LocationBar*)locationBar { |
79 return locationBarView_.get(); | 117 return locationBarView_.get(); |
80 } | 118 } |
81 | 119 |
82 - (void)focusLocationBar { | 120 - (void)focusLocationBar { |
83 if (locationBarView_.get()) { | 121 if (locationBarView_.get()) { |
84 locationBarView_->FocusLocation(); | 122 locationBarView_->FocusLocation(); |
85 } | 123 } |
86 } | 124 } |
87 | 125 |
88 // Called when the state for a command changes to |enabled|. Update the | 126 // Called when the state for a command changes to |enabled|. Update the |
89 // corresponding UI element. | 127 // corresponding UI element. |
90 - (void)enabledStateChangedForCommand:(NSInteger)command enabled:(BOOL)enabled { | 128 - (void)enabledStateChangedForCommand:(NSInteger)command enabled:(BOOL)enabled { |
91 NSButton* button = nil; | 129 NSButton* button = nil; |
92 switch (command) { | 130 switch (command) { |
93 case IDC_BACK: | 131 case IDC_BACK: |
94 button = backButton_; | 132 button = backButton_; |
95 break; | 133 break; |
96 case IDC_FORWARD: | 134 case IDC_FORWARD: |
97 button = forwardButton_; | 135 button = forwardButton_; |
98 break; | 136 break; |
99 case IDC_HOME: | 137 case IDC_HOME: |
100 // TODO(pinkerton): add home button | 138 button = homeButton_; |
101 break; | 139 break; |
102 case IDC_STAR: | 140 case IDC_STAR: |
103 button = starButton_; | 141 button = starButton_; |
104 break; | 142 break; |
105 } | 143 } |
106 [button setEnabled:enabled]; | 144 [button setEnabled:enabled]; |
107 } | 145 } |
108 | 146 |
109 // Init the enabled state of the buttons on the toolbar to match the state in | 147 // Init the enabled state of the buttons on the toolbar to match the state in |
110 // the controller. | 148 // the controller. |
111 - (void)initCommandStatus:(CommandUpdater*)commands { | 149 - (void)initCommandStatus:(CommandUpdater*)commands { |
112 [backButton_ setEnabled:commands->IsCommandEnabled(IDC_BACK) ? YES : NO]; | 150 [backButton_ setEnabled:commands->IsCommandEnabled(IDC_BACK) ? YES : NO]; |
113 [forwardButton_ | 151 [forwardButton_ |
114 setEnabled:commands->IsCommandEnabled(IDC_FORWARD) ? YES : NO]; | 152 setEnabled:commands->IsCommandEnabled(IDC_FORWARD) ? YES : NO]; |
115 [reloadButton_ | 153 [reloadButton_ setEnabled:commands->IsCommandEnabled(IDC_RELOAD) ? YES : NO]; |
116 setEnabled:commands->IsCommandEnabled(IDC_RELOAD) ? YES : NO]; | 154 [homeButton_ setEnabled:commands->IsCommandEnabled(IDC_HOME) ? YES : NO]; |
117 // TODO(pinkerton): Add home button. | |
118 [starButton_ setEnabled:commands->IsCommandEnabled(IDC_STAR) ? YES : NO]; | 155 [starButton_ setEnabled:commands->IsCommandEnabled(IDC_STAR) ? YES : NO]; |
119 } | 156 } |
120 | 157 |
121 - (void)updateToolbarWithContents:(TabContents*)tab | 158 - (void)updateToolbarWithContents:(TabContents*)tab |
122 shouldRestoreState:(BOOL)shouldRestore { | 159 shouldRestoreState:(BOOL)shouldRestore { |
123 locationBarView_->Update(tab, shouldRestore ? true : false); | 160 locationBarView_->Update(tab, shouldRestore ? true : false); |
124 } | 161 } |
125 | 162 |
126 - (void)setStarredState:(BOOL)isStarred { | 163 - (void)setStarredState:(BOOL)isStarred { |
127 NSString* starImageName = kStarImageName; | 164 NSString* starImageName = kStarImageName; |
(...skipping 26 matching lines...) Expand all Loading... | |
154 DCHECK(locationBarFieldEditor_.get()); | 191 DCHECK(locationBarFieldEditor_.get()); |
155 [locationBarFieldEditor_.get() setFieldEditor:YES]; | 192 [locationBarFieldEditor_.get() setFieldEditor:YES]; |
156 return locationBarFieldEditor_.get(); | 193 return locationBarFieldEditor_.get(); |
157 } | 194 } |
158 return nil; | 195 return nil; |
159 } | 196 } |
160 | 197 |
161 // Returns an array of views in the order of the outlets above. | 198 // Returns an array of views in the order of the outlets above. |
162 - (NSArray*)toolbarViews { | 199 - (NSArray*)toolbarViews { |
163 return [NSArray arrayWithObjects:backButton_, forwardButton_, reloadButton_, | 200 return [NSArray arrayWithObjects:backButton_, forwardButton_, reloadButton_, |
164 starButton_, goButton_, locationBar_, nil]; | 201 homeButton_, starButton_, goButton_, pageButton_, wrenchButton_, |
202 locationBar_, nil]; | |
203 } | |
204 | |
205 // Moves |rect| to the right by |delta|, keeping the right side fixed by | |
206 // shrinking the width to compensate. Passing a negative value for |deltaX| | |
207 // moves to the left and increases the width. | |
208 - (NSRect)adjustRect:(NSRect)rect byAmount:(float)deltaX { | |
209 NSRect frame = NSOffsetRect(rect, deltaX, 0); | |
210 frame.size.width -= deltaX; | |
211 return frame; | |
212 } | |
213 | |
214 // Computes the padding between the buttons that should have a separation from | |
215 // the positions in the nib. Since the forward and reload buttons are always | |
216 // visible, we use those buttons as the canonical spacing. | |
217 - (float)interButtonSpacing { | |
218 NSRect forwardFrame = [forwardButton_ frame]; | |
219 NSRect reloadFrame = [reloadButton_ frame]; | |
220 DCHECK(NSMinX(reloadFrame) > NSMaxX(forwardFrame)); | |
221 return NSMinX(reloadFrame) - NSMaxX(forwardFrame); | |
222 } | |
223 | |
224 // Show or hide the home button based on the pref. | |
225 - (void)showOptionalHomeButton { | |
226 BOOL hide = showHomeButton_.GetValue() ? NO : YES; | |
227 if (hide == [homeButton_ isHidden]) | |
228 return; // Nothing to do, view state matches pref state. | |
229 | |
230 // Always shift the star and text field by the width of the home button plus | |
231 // the appropriate gap width. If we're hiding the button, we have to | |
232 // reverse the direction of the movement (to the left). | |
233 float moveX = [self interButtonSpacing] + [homeButton_ frame].size.width; | |
234 if (hide) | |
235 moveX *= -1; // Reverse the direction of the move. | |
236 | |
237 [starButton_ setFrame:NSOffsetRect([starButton_ frame], moveX, 0)]; | |
238 [locationBar_ setFrame:[self adjustRect:[locationBar_ frame] | |
239 byAmount:moveX]]; | |
240 [homeButton_ setHidden:hide]; | |
241 } | |
242 | |
243 // Show or hide the page and wrench buttons based on the pref. | |
244 - (void)showOptionalPageWrenchButtons { | |
245 DCHECK([pageButton_ isHidden] == [wrenchButton_ isHidden]); | |
246 BOOL hide = showPageOptionButtons_.GetValue() ? NO : YES; | |
247 if (hide == [pageButton_ isHidden]) | |
248 return; // Nothing to do, view state matches pref state. | |
249 | |
250 // Shift the go button and resize the text field by the width of the | |
251 // page/wrench buttons plus two times the gap width. If we're showing the | |
252 // buttons, we have to reverse the direction of movement (to the left). Unlike | |
253 // the home button above, we only ever have to resize the text field, we don't | |
254 // have to move it. | |
255 float moveX = 2 * [self interButtonSpacing] + NSWidth([pageButton_ frame]) + | |
256 NSWidth([wrenchButton_ frame]); | |
257 if (!hide) | |
258 moveX *= -1; // Reverse the direction of the move. | |
259 [goButton_ setFrame:NSOffsetRect([goButton_ frame], moveX, 0)]; | |
260 NSRect locationFrame = [locationBar_ frame]; | |
261 locationFrame.size.width += moveX; | |
262 [locationBar_ setFrame:locationFrame]; | |
263 | |
264 [pageButton_ setHidden:hide]; | |
265 [wrenchButton_ setHidden:hide]; | |
266 } | |
267 | |
268 - (void)prefChanged:(std::wstring*)prefName { | |
269 if (!prefName) return; | |
270 if (*prefName == prefs::kShowHomeButton) { | |
271 [self showOptionalHomeButton]; | |
272 } else if (*prefName == prefs::kShowPageOptionsButtons) { | |
273 [self showOptionalPageWrenchButtons]; | |
274 } | |
275 } | |
276 | |
277 - (IBAction)showPageMenu:(id)sender { | |
278 [NSMenu popUpContextMenu:pageMenu_ | |
279 withEvent:[NSApp currentEvent] | |
280 forView:pageButton_]; | |
281 } | |
282 | |
283 - (IBAction)showWrenchMenu:(id)sender { | |
284 [NSMenu popUpContextMenu:wrenchMenu_ | |
285 withEvent:[NSApp currentEvent] | |
286 forView:wrenchButton_]; | |
165 } | 287 } |
166 | 288 |
167 @end | 289 @end |
OLD | NEW |