Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Side by Side Diff: chrome/browser/cocoa/toolbar_controller.mm

Issue 465130: Share the code that builds the page menu in a common model, make Mac and Win ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "app/l10n_util_mac.h" 7 #include "app/l10n_util_mac.h"
8 #include "base/mac_util.h" 8 #include "base/mac_util.h"
9 #include "base/nsimage_cache_mac.h" 9 #include "base/nsimage_cache_mac.h"
10 #include "base/sys_string_conversions.h" 10 #include "base/sys_string_conversions.h"
11 #include "base/gfx/rect.h" 11 #include "base/gfx/rect.h"
12 #include "chrome/app/chrome_dll_resource.h" 12 #include "chrome/app/chrome_dll_resource.h"
13 #include "chrome/browser/autocomplete/autocomplete_edit_view.h" 13 #include "chrome/browser/autocomplete/autocomplete_edit_view.h"
14 #include "chrome/browser/browser.h"
14 #include "chrome/browser/bubble_positioner.h" 15 #include "chrome/browser/bubble_positioner.h"
15 #import "chrome/browser/cocoa/autocomplete_text_field.h" 16 #import "chrome/browser/cocoa/autocomplete_text_field.h"
16 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" 17 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h"
17 #import "chrome/browser/cocoa/back_forward_menu_controller.h" 18 #import "chrome/browser/cocoa/back_forward_menu_controller.h"
18 #import "chrome/browser/cocoa/background_gradient_view.h" 19 #import "chrome/browser/cocoa/background_gradient_view.h"
19 #import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h" 20 #import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h"
20 #import "chrome/browser/cocoa/extensions/browser_actions_controller.h" 21 #import "chrome/browser/cocoa/extensions/browser_actions_controller.h"
21 #import "chrome/browser/cocoa/gradient_button_cell.h" 22 #import "chrome/browser/cocoa/gradient_button_cell.h"
22 #import "chrome/browser/cocoa/location_bar_view_mac.h" 23 #import "chrome/browser/cocoa/location_bar_view_mac.h"
23 #import "chrome/browser/cocoa/menu_button.h" 24 #import "chrome/browser/cocoa/menu_button.h"
25 #import "chrome/browser/cocoa/menu_controller.h"
24 #import "chrome/browser/cocoa/toolbar_view.h" 26 #import "chrome/browser/cocoa/toolbar_view.h"
27 #include "chrome/browser/page_menu_model.h"
25 #include "chrome/browser/profile.h" 28 #include "chrome/browser/profile.h"
26 #include "chrome/browser/search_engines/template_url_model.h" 29 #include "chrome/browser/search_engines/template_url_model.h"
27 #include "chrome/browser/toolbar_model.h" 30 #include "chrome/browser/toolbar_model.h"
28 #include "chrome/common/notification_details.h" 31 #include "chrome/common/notification_details.h"
29 #include "chrome/common/notification_observer.h" 32 #include "chrome/common/notification_observer.h"
30 #include "chrome/common/notification_type.h" 33 #include "chrome/common/notification_type.h"
31 #include "chrome/common/pref_names.h" 34 #include "chrome/common/pref_names.h"
32 #include "chrome/common/pref_service.h" 35 #include "chrome/common/pref_service.h"
33 #include "grit/generated_resources.h" 36 #include "grit/generated_resources.h"
34 37
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 } 77 }
75 78
76 private: 79 private:
77 ToolbarController* controller_; // weak, owns us 80 ToolbarController* controller_; // weak, owns us
78 }; 81 };
79 82
80 } // namespace 83 } // namespace
81 84
82 namespace ToolbarControllerInternal { 85 namespace ToolbarControllerInternal {
83 86
87 // A C++ delegate that handles enabling/disabling menu items and handling when
88 // a menu command is chosen.
89 class MenuDelegate : public menus::SimpleMenuModel::Delegate {
90 public:
91 explicit MenuDelegate(Browser* browser)
92 : browser_(browser) { }
93
94 // Overridden from menus::SimpleMenuModel::Delegate
95 virtual bool IsCommandIdChecked(int command_id) const {
96 if (command_id == IDC_SHOW_BOOKMARK_BAR) {
97 return browser_->profile()->GetPrefs()->GetBoolean(
98 prefs::kShowBookmarkBar);
99 }
100 return false;
101 }
102 virtual bool IsCommandIdEnabled(int command_id) const {
103 return browser_->command_updater()->IsCommandEnabled(command_id);
104 }
105 virtual bool GetAcceleratorForCommandId(
106 int command_id,
107 menus::Accelerator* accelerator) { return false; }
108 virtual void ExecuteCommand(int command_id) {
109 browser_->ExecuteCommand(command_id);
110 }
111
112 private:
113 Browser* browser_;
114 };
115
84 // A C++ class registered for changes in preferences. Bridges the 116 // A C++ class registered for changes in preferences. Bridges the
85 // notification back to the ToolbarController. 117 // notification back to the ToolbarController.
86 class PrefObserverBridge : public NotificationObserver { 118 class PrefObserverBridge : public NotificationObserver {
87 public: 119 public:
88 PrefObserverBridge(ToolbarController* controller) 120 explicit PrefObserverBridge(ToolbarController* controller)
89 : controller_(controller) { } 121 : controller_(controller) { }
90 // Overridden from NotificationObserver: 122 // Overridden from NotificationObserver:
91 virtual void Observe(NotificationType type, 123 virtual void Observe(NotificationType type,
92 const NotificationSource& source, 124 const NotificationSource& source,
93 const NotificationDetails& details) { 125 const NotificationDetails& details) {
94 if (type == NotificationType::PREF_CHANGED) 126 if (type == NotificationType::PREF_CHANGED)
95 [controller_ prefChanged:Details<std::wstring>(details).ptr()]; 127 [controller_ prefChanged:Details<std::wstring>(details).ptr()];
96 } 128 }
97 private: 129 private:
98 ToolbarController* controller_; // weak, owns us 130 ToolbarController* controller_; // weak, owns us
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 NSTrackingInVisibleRect | 246 NSTrackingInVisibleRect |
215 NSTrackingMouseEnteredAndExited | 247 NSTrackingMouseEnteredAndExited |
216 NSTrackingActiveAlways 248 NSTrackingActiveAlways
217 owner:self 249 owner:self
218 userInfo:nil]); 250 userInfo:nil]);
219 [[self view] addTrackingArea:trackingArea_.get()]; 251 [[self view] addTrackingArea:trackingArea_.get()];
220 252
221 // We want a dynamic tooltip on the go button, so tell the go button to ask 253 // We want a dynamic tooltip on the go button, so tell the go button to ask
222 // use for the tooltip 254 // use for the tooltip
223 [goButton_ addToolTipRect:[goButton_ bounds] owner:self userData:nil]; 255 [goButton_ addToolTipRect:[goButton_ bounds] owner:self userData:nil];
224
225 EncodingMenuControllerDelegate::BuildEncodingMenu(profile_, encodingMenu_);
226 } 256 }
227 257
228 - (void)mouseExited:(NSEvent*)theEvent { 258 - (void)mouseExited:(NSEvent*)theEvent {
229 [[hoveredButton_ cell] setMouseInside:NO animate:YES]; 259 [[hoveredButton_ cell] setMouseInside:NO animate:YES];
230 [hoveredButton_ release]; 260 [hoveredButton_ release];
231 hoveredButton_ = nil; 261 hoveredButton_ = nil;
232 } 262 }
233 263
234 - (NSButton*)hoverButtonForEvent:(NSEvent*)theEvent { 264 - (NSButton*)hoverButtonForEvent:(NSEvent*)theEvent {
235 NSButton* targetView = (NSButton*)[[self view] 265 NSButton* targetView = (NSButton*)[[self view]
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 [autocompleteTextFieldEditor_.get() setFieldEditor:YES]; 412 [autocompleteTextFieldEditor_.get() setFieldEditor:YES];
383 return autocompleteTextFieldEditor_.get(); 413 return autocompleteTextFieldEditor_.get();
384 } 414 }
385 return nil; 415 return nil;
386 } 416 }
387 417
388 // Returns an array of views in the order of the outlets above. 418 // Returns an array of views in the order of the outlets above.
389 - (NSArray*)toolbarViews { 419 - (NSArray*)toolbarViews {
390 return [NSArray arrayWithObjects:backButton_, forwardButton_, reloadButton_, 420 return [NSArray arrayWithObjects:backButton_, forwardButton_, reloadButton_,
391 homeButton_, starButton_, goButton_, pageButton_, wrenchButton_, 421 homeButton_, starButton_, goButton_, pageButton_, wrenchButton_,
392 locationBar_, encodingMenu_, browserActionContainerView_, nil]; 422 locationBar_, browserActionContainerView_, nil];
393 } 423 }
394 424
395 // Moves |rect| to the right by |delta|, keeping the right side fixed by 425 // Moves |rect| to the right by |delta|, keeping the right side fixed by
396 // shrinking the width to compensate. Passing a negative value for |deltaX| 426 // shrinking the width to compensate. Passing a negative value for |deltaX|
397 // moves to the left and increases the width. 427 // moves to the left and increases the width.
398 - (NSRect)adjustRect:(NSRect)rect byAmount:(CGFloat)deltaX { 428 - (NSRect)adjustRect:(NSRect)rect byAmount:(CGFloat)deltaX {
399 NSRect frame = NSOffsetRect(rect, deltaX, 0); 429 NSRect frame = NSOffsetRect(rect, deltaX, 0);
400 frame.size.width -= deltaX; 430 frame.size.width -= deltaX;
401 return frame; 431 return frame;
402 } 432 }
(...skipping 23 matching lines...) Expand all
426 CGFloat moveX = [self interButtonSpacing] + [homeButton_ frame].size.width; 456 CGFloat moveX = [self interButtonSpacing] + [homeButton_ frame].size.width;
427 if (hide) 457 if (hide)
428 moveX *= -1; // Reverse the direction of the move. 458 moveX *= -1; // Reverse the direction of the move.
429 459
430 [starButton_ setFrame:NSOffsetRect([starButton_ frame], moveX, 0)]; 460 [starButton_ setFrame:NSOffsetRect([starButton_ frame], moveX, 0)];
431 [locationBar_ setFrame:[self adjustRect:[locationBar_ frame] 461 [locationBar_ setFrame:[self adjustRect:[locationBar_ frame]
432 byAmount:moveX]]; 462 byAmount:moveX]];
433 [homeButton_ setHidden:hide]; 463 [homeButton_ setHidden:hide];
434 } 464 }
435 465
466 // Lazily install the menus on the page and wrench buttons. Calling this
467 // repeatedly is inexpensive so it can be done every time the buttons are shown.
468 - (void)installPageWrenchMenus {
469 if (pageMenuModel_.get())
470 return;
471 pageMenuDelegate_.reset(
472 new ToolbarControllerInternal::MenuDelegate(browser_));
473 pageMenuModel_.reset(new PageMenuModel(pageMenuDelegate_.get(), browser_));
474 pageMenuController_.reset(
475 [[MenuController alloc] initWithModel:pageMenuModel_.get()
476 useWithPopUpButtonCell:YES]);
477 [pageButton_ setAttachedMenu:[pageMenuController_ menu]];
478 }
479
436 // Show or hide the page and wrench buttons based on the pref. 480 // Show or hide the page and wrench buttons based on the pref.
437 - (void)showOptionalPageWrenchButtons { 481 - (void)showOptionalPageWrenchButtons {
438 // Ignore this message if only showing the URL bar. 482 // Ignore this message if only showing the URL bar.
439 if (!hasToolbar_) 483 if (!hasToolbar_)
440 return; 484 return;
441 DCHECK([pageButton_ isHidden] == [wrenchButton_ isHidden]); 485 DCHECK([pageButton_ isHidden] == [wrenchButton_ isHidden]);
442 BOOL hide = showPageOptionButtons_.GetValue() ? NO : YES; 486 BOOL hide = showPageOptionButtons_.GetValue() ? NO : YES;
487 if (!hide)
488 [self installPageWrenchMenus];
443 if (hide == [pageButton_ isHidden]) 489 if (hide == [pageButton_ isHidden])
444 return; // Nothing to do, view state matches pref state. 490 return; // Nothing to do, view state matches pref state.
445 491
446 // Shift the go button and resize the text field by the width of the 492 // Shift the go button and resize the text field by the width of the
447 // page/wrench buttons plus two times the gap width. If we're showing the 493 // page/wrench buttons plus two times the gap width. If we're showing the
448 // buttons, we have to reverse the direction of movement (to the left). Unlike 494 // buttons, we have to reverse the direction of movement (to the left). Unlike
449 // the home button above, we only ever have to resize the text field, we don't 495 // the home button above, we only ever have to resize the text field, we don't
450 // have to move it. 496 // have to move it.
451 CGFloat moveX = 2 * [self interButtonSpacing] + NSWidth([pageButton_ frame]) + 497 CGFloat moveX = 2 * [self interButtonSpacing] + NSWidth([pageButton_ frame]) +
452 NSWidth([wrenchButton_ frame]); 498 NSWidth([wrenchButton_ frame]);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 655
610 NSRect r = NSMakeRect(minX, NSMinY(locationFrame), maxX - minX, 656 NSRect r = NSMakeRect(minX, NSMinY(locationFrame), maxX - minX,
611 NSHeight(locationFrame)); 657 NSHeight(locationFrame));
612 gfx::Rect stack_bounds( 658 gfx::Rect stack_bounds(
613 NSRectToCGRect([[self view] convertRect:r toView:nil])); 659 NSRectToCGRect([[self view] convertRect:r toView:nil]));
614 // Inset the bounds to just inside the visible edges (see comment above). 660 // Inset the bounds to just inside the visible edges (see comment above).
615 stack_bounds.Inset(kLocationStackEdgeWidth, 0); 661 stack_bounds.Inset(kLocationStackEdgeWidth, 0);
616 return stack_bounds; 662 return stack_bounds;
617 } 663 }
618 @end 664 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/toolbar_controller.h ('k') | chrome/browser/cocoa/toolbar_controller_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698