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

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

Issue 1127001: Implement the confirm infobar with link for mac.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 9 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
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 <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #include "base/logging.h" // for NOTREACHED() 7 #include "base/logging.h" // for NOTREACHED()
8 #include "base/mac_util.h" 8 #include "base/mac_util.h"
9 #include "base/sys_string_conversions.h" 9 #include "base/sys_string_conversions.h"
10 #import "chrome/browser/cocoa/animatable_view.h" 10 #import "chrome/browser/cocoa/animatable_view.h"
(...skipping 18 matching lines...) Expand all
29 - (void)removeInfoBar; 29 - (void)removeInfoBar;
30 30
31 // Performs final cleanup after an animation is finished or stopped, including 31 // Performs final cleanup after an animation is finished or stopped, including
32 // notifying the InfoBarDelegate that the infobar was closed and removing the 32 // notifying the InfoBarDelegate that the infobar was closed and removing the
33 // infobar from its container, if necessary. 33 // infobar from its container, if necessary.
34 - (void)cleanUpAfterAnimation:(BOOL)finished; 34 - (void)cleanUpAfterAnimation:(BOOL)finished;
35 35
36 // Removes the ok and cancel buttons, and resizes the textfield to use the 36 // Removes the ok and cancel buttons, and resizes the textfield to use the
37 // space. 37 // space.
38 - (void)removeButtons; 38 - (void)removeButtons;
39
40 // Sets the info bar message to the specified |message|, with a hypertext
41 // style link. |link| will be inserted into message at |link_offset|.
42 -(void)setLabelToMessage:(NSString*)message
43 withLink:(NSString*)link
44 atOffset:(NSUInteger)link_offset;
39 @end 45 @end
40 46
41 @implementation InfoBarController 47 @implementation InfoBarController
42 48
43 @synthesize containerController = containerController_; 49 @synthesize containerController = containerController_;
44 @synthesize delegate = delegate_; 50 @synthesize delegate = delegate_;
45 51
46 - (id)initWithDelegate:(InfoBarDelegate*)delegate { 52 - (id)initWithDelegate:(InfoBarDelegate*)delegate {
47 DCHECK(delegate); 53 DCHECK(delegate);
48 if ((self = [super initWithNibName:@"InfoBar" 54 if ((self = [super initWithNibName:@"InfoBar"
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 } 178 }
173 179
174 - (void)animationDidStop:(NSAnimation*)animation { 180 - (void)animationDidStop:(NSAnimation*)animation {
175 [self cleanUpAfterAnimation:NO]; 181 [self cleanUpAfterAnimation:NO];
176 } 182 }
177 183
178 - (void)animationDidEnd:(NSAnimation*)animation { 184 - (void)animationDidEnd:(NSAnimation*)animation {
179 [self cleanUpAfterAnimation:YES]; 185 [self cleanUpAfterAnimation:YES];
180 } 186 }
181 187
188 -(void)setLabelToMessage:(NSString*)message
189 withLink:(NSString*)link
190 atOffset:(NSUInteger)link_offset {
191 // Create an attributes dictionary for the entire message. We have
192 // to expicitly set the font the control's font. We also override
193 // the cursor to give us the normal cursor rather than the text
194 // insertion cursor.
195 NSMutableDictionary* linkAttributes =
196 [NSMutableDictionary dictionaryWithObject:[NSCursor arrowCursor]
197 forKey:NSCursorAttributeName];
198 [linkAttributes setObject:[label_ font]
199 forKey:NSFontAttributeName];
200
201 // Create the attributed string for the main message text.
202 NSMutableAttributedString* infoText =
203 [[[NSMutableAttributedString alloc]
204 initWithString:message] autorelease];
205 [infoText addAttributes:linkAttributes
206 range:NSMakeRange(0, [infoText length])];
207
208 // Add additional attributes to style the link text appropriately as
209 // well as linkify it. We use an empty string for the NSLink
210 // attribute because the actual object we pass doesn't matter, but
211 // it cannot be nil.
212 [linkAttributes setObject:[NSColor blueColor]
213 forKey:NSForegroundColorAttributeName];
214 [linkAttributes setObject:[NSNumber numberWithBool:YES]
215 forKey:NSUnderlineStyleAttributeName];
216 [linkAttributes setObject:[NSCursor pointingHandCursor]
217 forKey:NSCursorAttributeName];
218 [linkAttributes setObject:[NSString string] // dummy value
219 forKey:NSLinkAttributeName];
220
221 // Insert the link text into the string at the appropriate offset.
222 NSAttributedString* attributed_string =
223 [[[NSAttributedString alloc] initWithString:link
224 attributes:linkAttributes] autorelease];
225 [infoText insertAttributedString:attributed_string
226 atIndex:link_offset];
227
228 // Update the label view with the new text. The view must be
229 // selectable and allow editing text attributes for the
230 // linkification to work correctly.
231 [label_ setAllowsEditingTextAttributes:YES];
232 [label_ setSelectable:YES];
233 [label_ setAttributedStringValue:infoText];
234 }
235
182 @end 236 @end
183 237
184 238
185 ///////////////////////////////////////////////////////////////////////// 239 /////////////////////////////////////////////////////////////////////////
186 // AlertInfoBarController implementation 240 // AlertInfoBarController implementation
187 241
188 @implementation AlertInfoBarController 242 @implementation AlertInfoBarController
189 243
190 // Alert infobars have a text message. 244 // Alert infobars have a text message.
191 - (void)addAdditionalControls { 245 - (void)addAdditionalControls {
(...skipping 25 matching lines...) Expand all
217 // solutions. The About box legal block has the same issue, maybe share 271 // solutions. The About box legal block has the same issue, maybe share
218 // a solution. 272 // a solution.
219 - (void)addAdditionalControls { 273 - (void)addAdditionalControls {
220 // No buttons. 274 // No buttons.
221 [self removeButtons]; 275 [self removeButtons];
222 276
223 LinkInfoBarDelegate* delegate = delegate_->AsLinkInfoBarDelegate(); 277 LinkInfoBarDelegate* delegate = delegate_->AsLinkInfoBarDelegate();
224 DCHECK(delegate); 278 DCHECK(delegate);
225 size_t offset = std::wstring::npos; 279 size_t offset = std::wstring::npos;
226 std::wstring message = delegate->GetMessageTextWithOffset(&offset); 280 std::wstring message = delegate->GetMessageTextWithOffset(&offset);
227 281 [self setLabelToMessage:base::SysWideToNSString(message)
228 // Create an attributes dictionary for the entire message. We have 282 withLink:base::SysWideToNSString(delegate->GetLinkText())
229 // to expicitly set the font the control's font. We also override 283 atOffset:offset];
230 // the cursor to give us the normal cursor rather than the text
231 // insertion cursor.
232 NSMutableDictionary* linkAttributes =
233 [NSMutableDictionary dictionaryWithObject:[NSCursor arrowCursor]
234 forKey:NSCursorAttributeName];
235 [linkAttributes setObject:[label_ font]
236 forKey:NSFontAttributeName];
237
238 // Create the attributed string for the main message text.
239 NSMutableAttributedString* infoText =
240 [[[NSMutableAttributedString alloc]
241 initWithString:base::SysWideToNSString(message)] autorelease];
242 [infoText addAttributes:linkAttributes
243 range:NSMakeRange(0, [infoText length])];
244
245 // Add additional attributes to style the link text appropriately as
246 // well as linkify it. We use an empty string for the NSLink
247 // attribute because the actual object we pass doesn't matter, but
248 // it cannot be nil.
249 [linkAttributes setObject:[NSColor blueColor]
250 forKey:NSForegroundColorAttributeName];
251 [linkAttributes setObject:[NSNumber numberWithBool:YES]
252 forKey:NSUnderlineStyleAttributeName];
253 [linkAttributes setObject:[NSCursor pointingHandCursor]
254 forKey:NSCursorAttributeName];
255 [linkAttributes setObject:[NSString string] // dummy value
256 forKey:NSLinkAttributeName];
257
258 // Insert the link text into the string at the appropriate offset.
259 [infoText insertAttributedString:
260 [[[NSAttributedString alloc]
261 initWithString:base::SysWideToNSString(delegate->GetLinkText())
262 attributes:linkAttributes] autorelease]
263 atIndex:offset];
264
265 // Update the label view with the new text. The view must be
266 // selectable and allow editing text attributes for the
267 // linkification to work correctly.
268 [label_ setAllowsEditingTextAttributes:YES];
269 [label_ setSelectable:YES];
270 [label_ setAttributedStringValue:infoText];
271 } 284 }
272 285
273 // Called when someone clicks on the link in the infobar. This method 286 // Called when someone clicks on the link in the infobar. This method
274 // is called by the InfobarTextField on its delegate (the 287 // is called by the InfobarTextField on its delegate (the
275 // LinkInfoBarController). 288 // LinkInfoBarController).
276 - (void)linkClicked { 289 - (void)linkClicked {
277 WindowOpenDisposition disposition = 290 WindowOpenDisposition disposition =
278 event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent]); 291 event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
279 if (delegate_ && delegate_->AsLinkInfoBarDelegate()->LinkClicked(disposition)) 292 if (delegate_ && delegate_->AsLinkInfoBarDelegate()->LinkClicked(disposition))
280 [self removeInfoBar]; 293 [self removeInfoBar];
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 (visibleButtons & ConfirmInfoBarDelegate::BUTTON_OK)) { 378 (visibleButtons & ConfirmInfoBarDelegate::BUTTON_OK)) {
366 rightEdge -= spaceBeforeButtons; 379 rightEdge -= spaceBeforeButtons;
367 } 380 }
368 381
369 NSRect frame = [label_ frame]; 382 NSRect frame = [label_ frame];
370 DCHECK(rightEdge > NSMinX(frame)) 383 DCHECK(rightEdge > NSMinX(frame))
371 << "Need to make the xib larger to handle buttons with text this long"; 384 << "Need to make the xib larger to handle buttons with text this long";
372 frame.size.width = rightEdge - NSMinX(frame); 385 frame.size.width = rightEdge - NSMinX(frame);
373 [label_ setFrame:frame]; 386 [label_ setFrame:frame];
374 387
375 // Set the text. 388 // Set the text and link.
376 [label_ setStringValue:base::SysWideToNSString(delegate->GetMessageText())]; 389 NSString* message = base::SysWideToNSString(delegate->GetMessageText());
390 std::wstring link = delegate->GetLinkText();
391 if (link.empty()) {
392 // Simple case: no link, so just set the message directly.
393 [label_ setStringValue:message];
394 } else {
395 // Inserting the link unintentionally causes the text to behave slightly
396 // different result to the simple case above: text is truncated on word
397 // boundaries if needed, rather than elided with ellipses.
398
399 // Add spacing between the label and the link.
400 message = [message stringByAppendingString:@" "];
401 [self setLabelToMessage:message
402 withLink:base::SysWideToNSString(link)
403 atOffset:[message length]];
404 }
405 }
406
407 // Called when someone clicks on the link in the infobar. This method
408 // is called by the InfobarTextField on its delegate (the
409 // LinkInfoBarController).
410 - (void)linkClicked {
411 WindowOpenDisposition disposition =
412 event_utils::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
413 if (delegate_ &&
414 delegate_->AsConfirmInfoBarDelegate()->LinkClicked(disposition))
415 [self removeInfoBar];
377 } 416 }
378 417
379 @end 418 @end
380 419
381 420
382 ////////////////////////////////////////////////////////////////////////// 421 //////////////////////////////////////////////////////////////////////////
383 // CreateInfoBar() implementations 422 // CreateInfoBar() implementations
384 423
385 InfoBar* AlertInfoBarDelegate::CreateInfoBar() { 424 InfoBar* AlertInfoBarDelegate::CreateInfoBar() {
386 AlertInfoBarController* controller = 425 AlertInfoBarController* controller =
387 [[AlertInfoBarController alloc] initWithDelegate:this]; 426 [[AlertInfoBarController alloc] initWithDelegate:this];
388 return new InfoBar(controller); 427 return new InfoBar(controller);
389 } 428 }
390 429
391 InfoBar* LinkInfoBarDelegate::CreateInfoBar() { 430 InfoBar* LinkInfoBarDelegate::CreateInfoBar() {
392 LinkInfoBarController* controller = 431 LinkInfoBarController* controller =
393 [[LinkInfoBarController alloc] initWithDelegate:this]; 432 [[LinkInfoBarController alloc] initWithDelegate:this];
394 return new InfoBar(controller); 433 return new InfoBar(controller);
395 } 434 }
396 435
397 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() { 436 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() {
398 ConfirmInfoBarController* controller = 437 ConfirmInfoBarController* controller =
399 [[ConfirmInfoBarController alloc] initWithDelegate:this]; 438 [[ConfirmInfoBarController alloc] initWithDelegate:this];
400 return new InfoBar(controller); 439 return new InfoBar(controller);
401 } 440 }
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/infobar_controller.h ('k') | chrome/browser/cocoa/infobar_controller_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698