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

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

Issue 150132: First cut at popup blocking for Mac. Remove ifdefs in cross-platform code. Im... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 5 months 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
Property Changes:
Name: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
2 // source code is governed by a BSD-style license that can be found in the
3 // LICENSE file.
4
5 #import "chrome/browser/cocoa/blocked_popup_container_controller.h"
6
7 #include "app/l10n_util.h"
8 #include "base/sys_string_conversions.h"
9 #include "chrome/browser/tab_contents/tab_contents.h"
10 #include "chrome/browser/tab_contents/tab_contents_view.h"
11 #include "grit/generated_resources.h"
12
13 #import "chrome/browser/cocoa/background_gradient_view.h"
14
15 // A C++ bridge class that manages the interaction between the C++ interface
16 // and the Objective-C view controller that implements the popup blocker.
17 class BlockedPopupContainerViewBridge : public BlockedPopupContainerView {
18 public:
19 BlockedPopupContainerViewBridge(BlockedPopupContainerController* controller);
20 virtual ~BlockedPopupContainerViewBridge();
21
22 // Overrides from BlockedPopupContainerView
23 virtual void SetPosition();
24 virtual void ShowView();
25 virtual void UpdateLabel();
26 virtual void HideView();
27 virtual void Destroy();
28
29 private:
30 BlockedPopupContainerController* controller_; // Weak, owns us.
31 };
32
33 @interface BlockedPopupContainerController(Private)
34 - (void)initPopupView;
35 - (NSView*)containingView;
36 @end
37
38 @implementation BlockedPopupContainerController
39
40 // Initialize with the given popup container. Creates the C++ bridge object
41 // used to represet the "view".
rohitrao (ping after 24h) 2009/07/01 15:04:17 Typo: represet
pink (ping after 24hrs) 2009/07/01 15:49:15 Done.
42 - (id)initWithContainer:(BlockedPopupContainer*)container {
43 if ((self = [super init])) {
44 container_ = container;
45 bridge_.reset(new BlockedPopupContainerViewBridge(self));
46 [self initPopupView];
47 }
48 return self;
49 }
50
51 - (void)dealloc {
52 [view_ removeFromSuperview];
53 [super dealloc];
54 }
55
56 - (IBAction)closePopup:(id)sender {
57 container_->set_dismissed();
58 container_->CloseAll();
59 }
60
61 // Create and initialize the popup view and its label, close box, etc.
62 - (void)initPopupView {
63 const float kWidth = 200.0;
rohitrao (ping after 24h) 2009/07/01 15:04:17 static?
pink (ping after 24hrs) 2009/07/01 15:49:15 Done.
64 const float kHeight = 20.0;
65 const float kCloseBoxSize = 16.0;
66 const float kCloseBoxPaddingY = 2.0;
67 const float kLabelPaddingX = 5.0;
68
69 // Create it below the parent's bottom edge so we can animate it into place.
70 NSRect startFrame = NSMakeRect(0.0, -kHeight, kWidth, kHeight);
71 view_.reset([[BackgroundGradientView alloc] initWithFrame:startFrame]);
72 [view_ setAutoresizingMask:NSViewMinXMargin | NSViewMaxYMargin];
73
74 // Create the text label and position it. We'll resize it later when the
75 // label gets updated. The view owns the label, we only hold a weak reference.
76 NSRect labelFrame = NSMakeRect(kLabelPaddingX,
77 0,
78 startFrame.size.width - kCloseBoxSize,
79 startFrame.size.height);
80 label_ = [[[NSTextField alloc] initWithFrame:labelFrame] autorelease];
81 [label_ setSelectable:NO];
82 [label_ setAutoresizingMask:NSViewWidthSizable];
83 [label_ setBordered:NO];
84 [label_ setBezeled:NO];
85 [label_ setDrawsBackground:NO];
86 [view_ addSubview:label_];
87
88 // Create the close box and position at the left of the view.
89 NSRect closeFrame = NSMakeRect(startFrame.size.width - kCloseBoxSize,
90 kCloseBoxPaddingY,
91 kCloseBoxSize,
92 kCloseBoxSize);
93 NSButton* close = [[[NSButton alloc] initWithFrame:closeFrame] autorelease];
94 [close setAutoresizingMask:NSViewMinXMargin];
95 [close setImage:[NSImage imageNamed:@"close_bar"]];
96 [close setAlternateImage:[NSImage imageNamed:@"close_bar_p"]];
97 [close setBordered:NO];
98 [close setTarget:self];
99 [close setAction:@selector(closePopup:)];
100 [view_ addSubview:close];
101 }
102
103 // Returns the C++ brige object.
104 - (BlockedPopupContainerView*)bridge {
105 return bridge_.get();
106 }
107
108 // Returns the NSView container of the RWHVMac. We insert our popup blocker
rohitrao (ping after 24h) 2009/07/01 15:04:17 "NSView container" -> "parent", or maybe explicitl
pink (ping after 24hrs) 2009/07/01 15:49:15 Done.
109 // in here so that it stays around as the RWHVMac is created/destroyed during
110 // navigation.
111 - (NSView*)containingView {
112 return container_->GetConstrainingContents(NULL)->view()->GetNativeView();
113 }
114
115 - (void)show {
116 const float kLeftPadding = 20; // Leave room for the scrollbar.
117
118 // No need to do anything if it's already on screen.
119 if ([view_ superview]) return;
120
121 // Position the view at the bottom left corner, always leaving room for the
rohitrao (ping after 24h) 2009/07/01 15:04:17 If it's in the bottom left corner, do we have to w
pink (ping after 24hrs) 2009/07/01 15:49:15 typo, bottom right corner. Directions are hard, le
122 // scrollbar. This is what window does. It also doesn't care about covering
123 // up the horizontal scrollbar.
124 NSView* parent = [self containingView];
125 NSRect frame = [view_ frame];
126 frame.origin.x = [parent frame].size.width - frame.size.width - kLeftPadding;
127 [view_ setFrame:frame];
128
129 // Add the view and animate it sliding up into view.
130 [parent addSubview:view_.get()];
131 frame.origin.y = 0;
132 [[view_ animator] setFrame:frame];
133 }
134
135 - (void)hide {
136 [view_ removeFromSuperview];
137 }
138
139 // Resize the view based on the new label contents. The autoresize mask will
140 // take care of resizing everything else.
141 - (void)resizeWithLabel:(NSString*)label {
142 #if 0
rohitrao (ping after 24h) 2009/07/01 15:04:17 Why is this commented out?
pink (ping after 24hrs) 2009/07/01 15:49:15 todo added.
143 NSSize stringSize = [label sizeWithAttributes:nil];
144 NSRect frame = [view_ frame];
145 float originalWidth = frame.size.width;
146 frame.size.width = stringSize.width + 16 + 5;
147 frame.origin.x -= frame.size.width - originalWidth;
148 [view_ setFrame:frame];
149 #endif
150 }
151
152 - (void)update {
153 size_t blockedPopups = container_->GetBlockedPopupCount();
154 NSString* label = nil;
155 if (blockedPopups) {
156 label = base::SysUTF16ToNSString(
157 l10n_util::GetStringFUTF16(IDS_POPUPS_BLOCKED_COUNT,
158 UintToString16(blockedPopups)));
159 } else {
160 label = base::SysUTF16ToNSString(
161 l10n_util::GetStringUTF16(IDS_POPUPS_UNBLOCKED));
162 }
163 [self resizeWithLabel:label];
164 [label_ setStringValue:label];
165 }
166
167 - (NSView*)view {
168 return view_.get();
169 }
170
171 - (NSView*)label {
172 return label_;
173 }
174
175 @end
176
177 //---------------------------------------------------------------------------
178
179 BlockedPopupContainerView* BlockedPopupContainerView::Create(
180 BlockedPopupContainer* container) {
181 // We "leak" |blocker| for now, we'll release it when the bridge class
182 // gets a Destroy() message.
183 BlockedPopupContainerController* blocker =
184 [[BlockedPopupContainerController alloc] initWithContainer:container];
185 return [blocker bridge];
186 }
187
188 BlockedPopupContainerViewBridge::BlockedPopupContainerViewBridge(
189 BlockedPopupContainerController* controller) {
190 controller_ = controller;
191 }
192
193 BlockedPopupContainerViewBridge::~BlockedPopupContainerViewBridge() {
194 }
195
196 void BlockedPopupContainerViewBridge::SetPosition() {
197 // Doesn't ever get called, also a no-op on GTK.
198 NOTIMPLEMENTED();
199 }
200
201 void BlockedPopupContainerViewBridge::ShowView() {
202 [controller_ show];
203 }
204
205 void BlockedPopupContainerViewBridge::UpdateLabel() {
206 [controller_ update];
207 }
208
209 void BlockedPopupContainerViewBridge::HideView() {
210 [controller_ hide];
211 }
212
213 void BlockedPopupContainerViewBridge::Destroy() {
214 [controller_ autorelease];
215 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698