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/autocomplete_text_field_cell.h" | 5 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" |
6 | 6 |
7 #include "app/gfx/font.h" | 7 #include "app/gfx/font.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #import "base/logging.h" | 9 #import "base/logging.h" |
10 #import "third_party/GTM/AppKit/GTMTheme.h" | 10 #import "third_party/GTM/AppKit/GTMTheme.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 // This moves it down so that there is approximately as much image | 34 // This moves it down so that there is approximately as much image |
35 // above the lowercase ascender as below the baseline. A better | 35 // above the lowercase ascender as below the baseline. A better |
36 // technique would be nice to have, though. | 36 // technique would be nice to have, though. |
37 const NSInteger kKeywordHintImageBaseline = -6; | 37 const NSInteger kKeywordHintImageBaseline = -6; |
38 | 38 |
39 // Offset from the bottom of the field for drawing decoration text. | 39 // Offset from the bottom of the field for drawing decoration text. |
40 // TODO(shess): Somehow determine the baseline for the text field and | 40 // TODO(shess): Somehow determine the baseline for the text field and |
41 // use that. | 41 // use that. |
42 const NSInteger kBaselineOffset = 4; | 42 const NSInteger kBaselineOffset = 4; |
43 | 43 |
44 // The amount of padding on either side reserved for drawing the hint icon | 44 // The amount of padding on either side reserved for drawing an icon. |
45 const NSInteger kHintIconHorizontalPad = 3; | 45 const NSInteger kIconHorizontalPad = 3; |
46 | 46 |
47 // How far to shift bounding box of hint icon label down from top of field. | 47 // How far to shift bounding box of hint icon label down from top of field. |
48 const NSInteger kHintIconLabelYOffset = 7; | 48 const NSInteger kIconLabelYOffset = 7; |
49 | 49 |
50 // How far the editor insets itself, for purposes of determining if | 50 // How far the editor insets itself, for purposes of determining if |
51 // decorations need to be trimmed. | 51 // decorations need to be trimmed. |
52 const CGFloat kEditorHorizontalInset = 3.0; | 52 const CGFloat kEditorHorizontalInset = 3.0; |
53 | 53 |
54 // Conveniences to centralize width+offset calculations. | 54 // Conveniences to centralize width+offset calculations. |
55 CGFloat WidthForHint(NSAttributedString* hintString) { | 55 CGFloat WidthForHint(NSAttributedString* hintString) { |
56 return kHintXOffset + ceil([hintString size].width); | 56 return kHintXOffset + ceil([hintString size].width); |
57 } | 57 } |
58 CGFloat WidthForKeyword(NSAttributedString* keywordString) { | 58 CGFloat WidthForKeyword(NSAttributedString* keywordString) { |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 // If too wide, don't keep the hint. | 189 // If too wide, don't keep the hint. |
190 hintString_.reset(WidthForHint(as) > width ? nil : [as copy]); | 190 hintString_.reset(WidthForHint(as) > width ? nil : [as copy]); |
191 } | 191 } |
192 } | 192 } |
193 | 193 |
194 - (void)clearKeywordAndHint { | 194 - (void)clearKeywordAndHint { |
195 keywordString_.reset(); | 195 keywordString_.reset(); |
196 hintString_.reset(); | 196 hintString_.reset(); |
197 } | 197 } |
198 | 198 |
199 - (void)setHintIcon:(NSImage*)icon | 199 - (void)setSecurityImageView:(LocationBarViewMac::SecurityImageView*)view { |
200 label:(NSString*)label | 200 security_image_view_ = view; |
201 color:(NSColor*)color { | 201 } |
202 // Create an attributed string for the label, if a label was given. | |
203 NSAttributedString* as = nil; | |
204 if (label) { | |
205 DCHECK(color); | |
206 NSFont *baseFont = [self font]; | |
207 NSFont *font = [NSFont fontWithDescriptor:[baseFont fontDescriptor] | |
208 size:[baseFont pointSize] - 2.0]; | |
209 NSDictionary* attributes = | |
210 [NSDictionary dictionaryWithObjectsAndKeys: | |
211 color, NSForegroundColorAttributeName, | |
212 font, NSFontAttributeName, | |
213 NULL]; | |
214 as = [[[NSAttributedString alloc] initWithString:label | |
215 attributes:attributes] autorelease]; | |
216 } | |
217 | 202 |
218 hintIconLabel_.reset([as retain]); | 203 - (void)onSecurityIconMousePressed { |
219 hintIcon_.reset([icon retain]); | 204 security_image_view_->OnMousePressed(); |
220 } | 205 } |
221 | 206 |
222 // TODO(shess): This code is manually drawing the cell's border area, | 207 // TODO(shess): This code is manually drawing the cell's border area, |
223 // but otherwise the cell assumes -setBordered:YES for purposes of | 208 // but otherwise the cell assumes -setBordered:YES for purposes of |
224 // calculating things like the editing area. This is probably | 209 // calculating things like the editing area. This is probably |
225 // incorrect. I know that this affects -drawingRectForBounds:. | 210 // incorrect. I know that this affects -drawingRectForBounds:. |
226 | 211 |
227 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 212 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
228 DCHECK([controlView isFlipped]); | 213 DCHECK([controlView isFlipped]); |
229 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.25] set]; | 214 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.25] set]; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 DCHECK(!hintString_); | 264 DCHECK(!hintString_); |
280 const CGFloat keywordWidth(WidthForKeyword(keywordString_)); | 265 const CGFloat keywordWidth(WidthForKeyword(keywordString_)); |
281 | 266 |
282 // TODO(shess): This could be better. There's support for a | 267 // TODO(shess): This could be better. There's support for a |
283 // "short" version of the keyword string, work that in in a | 268 // "short" version of the keyword string, work that in in a |
284 // follow-on pass. | 269 // follow-on pass. |
285 if (keywordWidth < NSWidth(cellFrame)) { | 270 if (keywordWidth < NSWidth(cellFrame)) { |
286 textFrame.origin.x += keywordWidth; | 271 textFrame.origin.x += keywordWidth; |
287 textFrame.size.width = NSMaxX(cellFrame) - NSMinX(textFrame); | 272 textFrame.size.width = NSMaxX(cellFrame) - NSMinX(textFrame); |
288 } | 273 } |
289 } else if (hintIcon_) { | 274 } else if (security_image_view_ && security_image_view_->IsVisible()) { |
290 CGFloat width = [hintIcon_ size].width; | 275 NSImage* image = security_image_view_->GetImage(); |
291 width += kHintIconHorizontalPad * 2; | 276 CGFloat width = [image size].width; |
292 if (hintIconLabel_) { | 277 width += kIconHorizontalPad * 2; |
293 width += ceil([hintIconLabel_ size].width) + kHintXOffset; | 278 NSAttributedString* label = security_image_view_->GetLabel(); |
| 279 if (label) { |
| 280 width += ceil([label size].width) + kHintXOffset; |
294 } | 281 } |
295 if (width < NSWidth(cellFrame)) { | 282 if (width < NSWidth(cellFrame)) { |
296 textFrame.size.width -= width; | 283 textFrame.size.width -= width; |
297 } | 284 } |
298 } | 285 } |
299 | 286 |
300 return textFrame; | 287 return textFrame; |
301 } | 288 } |
302 | 289 |
303 // For NSTextFieldCell this is the area within the borders. For our | 290 - (NSRect)imageFrameForFrame:(NSRect)cellFrame |
304 // purposes, we count the info decorations as being part of the | 291 withImageView:(LocationBarViewMac::LocationBarImageView*)image_view { |
305 // border. | 292 if (!image_view->IsVisible()) { |
306 - (NSRect)drawingRectForBounds:(NSRect)theRect { | 293 return NSZeroRect; |
307 return [super drawingRectForBounds:[self textFrameForFrame:theRect]]; | 294 } |
308 } | 295 const NSSize imageRect = [image_view->GetImage() size]; |
309 | |
310 - (NSRect)hintImageFrameForFrame:(NSRect)cellFrame { | |
311 // We'll draw the entire image | |
312 const NSSize imageRect([hintIcon_ size]); | |
313 | |
314 CGFloat labelWidth = 0; | 296 CGFloat labelWidth = 0; |
315 if (hintIconLabel_) { | 297 NSAttributedString* label = image_view->GetLabel(); |
316 labelWidth = ceil([hintIconLabel_ size].width) + kHintXOffset; | 298 if (label) { |
| 299 labelWidth = ceil([label size].width) + kHintXOffset; |
317 } | 300 } |
318 | 301 |
319 // Move the rect that we're drawing into to the far right, minus | 302 // Move the rect that we're drawing into to the far right, minus |
320 // enough space for the label (if present) | 303 // enough space for the label (if present). |
321 cellFrame.origin.x += cellFrame.size.width - imageRect.width; | 304 cellFrame.origin.x += cellFrame.size.width - imageRect.width; |
322 cellFrame.origin.x -= labelWidth; | 305 cellFrame.origin.x -= labelWidth; |
323 // Add back the padding | 306 // Add back the padding |
324 cellFrame.origin.x -= kHintIconHorizontalPad; | 307 cellFrame.origin.x -= kIconHorizontalPad; |
325 | 308 |
326 // Center the image vertically in the frame | 309 // Center the image vertically in the frame |
327 cellFrame.origin.y += | 310 cellFrame.origin.y += |
328 floor((cellFrame.size.height - imageRect.height) / 2); | 311 floor((cellFrame.size.height - imageRect.height) / 2); |
329 | 312 |
330 // Set the drawing size to the image size | 313 // Set the drawing size to the image size |
331 cellFrame.size = imageRect; | 314 cellFrame.size = imageRect; |
332 | 315 |
333 return cellFrame; | 316 return cellFrame; |
334 } | 317 } |
335 | 318 |
| 319 - (NSRect)securityImageFrameForFrame:(NSRect)cellFrame { |
| 320 if (!security_image_view_) { |
| 321 return NSZeroRect; |
| 322 } |
| 323 return [self imageFrameForFrame:cellFrame withImageView:security_image_view_]; |
| 324 } |
| 325 |
| 326 // For NSTextFieldCell this is the area within the borders. For our |
| 327 // purposes, we count the info decorations as being part of the |
| 328 // border. |
| 329 - (NSRect)drawingRectForBounds:(NSRect)theRect { |
| 330 return [super drawingRectForBounds:[self textFrameForFrame:theRect]]; |
| 331 } |
| 332 |
336 - (void)drawHintWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 333 - (void)drawHintWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
337 DCHECK(hintString_); | 334 DCHECK(hintString_); |
338 | 335 |
339 NSRect textFrame = [self textFrameForFrame:cellFrame]; | 336 NSRect textFrame = [self textFrameForFrame:cellFrame]; |
340 NSRect infoFrame(NSMakeRect(NSMaxX(textFrame), | 337 NSRect infoFrame(NSMakeRect(NSMaxX(textFrame), |
341 cellFrame.origin.y + kHintYOffset, | 338 cellFrame.origin.y + kHintYOffset, |
342 ceil([hintString_ size].width), | 339 ceil([hintString_ size].width), |
343 cellFrame.size.height - kHintYOffset)); | 340 cellFrame.size.height - kHintYOffset)); |
344 [hintString_.get() drawInRect:infoFrame]; | 341 [hintString_.get() drawInRect:infoFrame]; |
345 } | 342 } |
(...skipping 21 matching lines...) Expand all Loading... |
367 [[[NSColor keyboardFocusIndicatorColor] colorWithAlphaComponent:0.5] set]; | 364 [[[NSColor keyboardFocusIndicatorColor] colorWithAlphaComponent:0.5] set]; |
368 [path setLineWidth:1.0]; | 365 [path setLineWidth:1.0]; |
369 [path stroke]; | 366 [path stroke]; |
370 | 367 |
371 // Draw text w/in the rectangle. | 368 // Draw text w/in the rectangle. |
372 infoFrame.origin.x += 4.0; | 369 infoFrame.origin.x += 4.0; |
373 infoFrame.origin.y += 1.0; | 370 infoFrame.origin.y += 1.0; |
374 [keywordString_.get() drawInRect:infoFrame]; | 371 [keywordString_.get() drawInRect:infoFrame]; |
375 } | 372 } |
376 | 373 |
377 - (void)drawHintIconWithFrame:(NSRect)cellFrame | 374 - (void)drawImageView:(LocationBarViewMac::LocationBarImageView*)image_view |
378 inView:(NSView*)controlView { | 375 withFrame:(NSRect)cellFrame |
| 376 inView:(NSView*)controlView { |
379 // If there's a label, draw it to the right of the icon. | 377 // If there's a label, draw it to the right of the icon. |
380 CGFloat labelWidth = 0; | 378 CGFloat labelWidth = 0; |
381 if (hintIconLabel_) { | 379 NSAttributedString* label = image_view->GetLabel(); |
382 labelWidth = ceil([hintIconLabel_ size].width) + kHintXOffset; | 380 if (label) { |
| 381 labelWidth = ceil([label size].width) + kHintXOffset; |
383 NSRect textFrame(NSMakeRect(NSMaxX(cellFrame) - labelWidth, | 382 NSRect textFrame(NSMakeRect(NSMaxX(cellFrame) - labelWidth, |
384 cellFrame.origin.y + kHintIconLabelYOffset, | 383 cellFrame.origin.y + kIconLabelYOffset, |
385 labelWidth, | 384 labelWidth, |
386 cellFrame.size.height - kHintIconLabelYOffset)); | 385 cellFrame.size.height - kIconLabelYOffset)); |
387 [hintIconLabel_.get() drawInRect:textFrame]; | 386 [label drawInRect:textFrame]; |
388 } | 387 } |
389 | 388 |
390 // We'll draw the entire image | 389 // Draw the entire image. |
391 NSRect imageRect = NSZeroRect; | 390 NSRect imageRect = NSZeroRect; |
392 imageRect.size = [hintIcon_ size]; | 391 NSImage* image = image_view->GetImage(); |
393 const NSRect hintFrame([self hintImageFrameForFrame:cellFrame]); | 392 image.size = [image size]; |
394 [hintIcon_ setFlipped:[controlView isFlipped]]; | 393 NSRect imageFrame([self imageFrameForFrame:cellFrame |
395 [hintIcon_ drawInRect:hintFrame | 394 withImageView:image_view]); |
396 fromRect:imageRect | 395 [image setFlipped:[controlView isFlipped]]; |
397 operation:NSCompositeSourceOver | 396 [image drawInRect:imageFrame |
398 fraction:1.0]; | 397 fromRect:imageRect |
| 398 operation:NSCompositeSourceOver |
| 399 fraction:1.0]; |
399 } | 400 } |
400 | 401 |
401 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 402 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
402 if (hintString_) { | 403 if (hintString_) { |
403 [self drawHintWithFrame:cellFrame inView:controlView]; | 404 [self drawHintWithFrame:cellFrame inView:controlView]; |
404 } else if (keywordString_) { | 405 } else if (keywordString_) { |
405 [self drawKeywordWithFrame:cellFrame inView:controlView]; | 406 [self drawKeywordWithFrame:cellFrame inView:controlView]; |
406 } else if (hintIcon_) { | 407 } else if (security_image_view_ && security_image_view_->IsVisible()) { |
407 [self drawHintIconWithFrame:cellFrame inView:controlView]; | 408 [self drawImageView:security_image_view_ |
| 409 withFrame:cellFrame |
| 410 inView:controlView]; |
408 } | 411 } |
409 | 412 |
410 [super drawInteriorWithFrame:cellFrame inView:controlView]; | 413 [super drawInteriorWithFrame:cellFrame inView:controlView]; |
411 } | 414 } |
412 | 415 |
413 - (void)resetCursorRect:(NSRect)cellFrame inView:(NSView *)controlView { | 416 - (void)resetCursorRect:(NSRect)cellFrame inView:(NSView *)controlView { |
414 [super resetCursorRect:[self textCursorFrameForFrame:cellFrame] | 417 [super resetCursorRect:[self textCursorFrameForFrame:cellFrame] |
415 inView:controlView]; | 418 inView:controlView]; |
416 } | 419 } |
417 | 420 |
418 @end | 421 @end |
OLD | NEW |