| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/location_bar/autocomplete_text_field_cell.h" | 5 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "chrome/browser/cocoa/image_utils.h" | 8 #import "chrome/browser/cocoa/image_utils.h" |
| 9 #import "chrome/browser/cocoa/location_bar/location_bar_decoration.h" | 9 #import "chrome/browser/cocoa/location_bar/location_bar_decoration.h" |
| 10 | 10 |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 } | 160 } |
| 161 | 161 |
| 162 // Helper function for calculating placement of decorations w/in the | 162 // Helper function for calculating placement of decorations w/in the |
| 163 // cell. |frame| is the cell's boundary rectangle, |remaining_frame| | 163 // cell. |frame| is the cell's boundary rectangle, |remaining_frame| |
| 164 // will get any space left after decorations are laid out (for text). | 164 // will get any space left after decorations are laid out (for text). |
| 165 // |left_decorations| is a set of decorations for the left-hand side | 165 // |left_decorations| is a set of decorations for the left-hand side |
| 166 // of the cell, |right_decorations| for the right-hand side. | 166 // of the cell, |right_decorations| for the right-hand side. |
| 167 // |decorations| will contain the resulting visible decorations, and | 167 // |decorations| will contain the resulting visible decorations, and |
| 168 // |decoration_frames| will contain their frames in the same | 168 // |decoration_frames| will contain their frames in the same |
| 169 // coordinates as |frame|. Decorations will be ordered left to right. | 169 // coordinates as |frame|. Decorations will be ordered left to right. |
| 170 void CalculatePositionsInFrame( | 170 // As a convenience returns the index of the first right-hand |
| 171 // decoration. |
| 172 size_t CalculatePositionsInFrame( |
| 171 NSRect frame, | 173 NSRect frame, |
| 172 const std::vector<LocationBarDecoration*>& left_decorations, | 174 const std::vector<LocationBarDecoration*>& left_decorations, |
| 173 const std::vector<LocationBarDecoration*>& right_decorations, | 175 const std::vector<LocationBarDecoration*>& right_decorations, |
| 174 std::vector<LocationBarDecoration*>* decorations, | 176 std::vector<LocationBarDecoration*>* decorations, |
| 175 std::vector<NSRect>* decoration_frames, | 177 std::vector<NSRect>* decoration_frames, |
| 176 NSRect* remaining_frame) { | 178 NSRect* remaining_frame) { |
| 177 decorations->clear(); | 179 decorations->clear(); |
| 178 decoration_frames->clear(); | 180 decoration_frames->clear(); |
| 179 | 181 |
| 180 // Layout |left_decorations| against the LHS. | 182 // Layout |left_decorations| against the LHS. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 192 decorations, decoration_frames, &frame); | 194 decorations, decoration_frames, &frame); |
| 193 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 195 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 194 | 196 |
| 195 // Reverse the right-hand decorations so that overall everything is | 197 // Reverse the right-hand decorations so that overall everything is |
| 196 // sorted left to right. | 198 // sorted left to right. |
| 197 std::reverse(decorations->begin() + left_count, decorations->end()); | 199 std::reverse(decorations->begin() + left_count, decorations->end()); |
| 198 std::reverse(decoration_frames->begin() + left_count, | 200 std::reverse(decoration_frames->begin() + left_count, |
| 199 decoration_frames->end()); | 201 decoration_frames->end()); |
| 200 | 202 |
| 201 *remaining_frame = frame; | 203 *remaining_frame = frame; |
| 204 return left_count; |
| 202 } | 205 } |
| 203 | 206 |
| 204 } // namespace | 207 } // namespace |
| 205 | 208 |
| 206 @implementation AutocompleteTextAttachmentCell | 209 @implementation AutocompleteTextAttachmentCell |
| 207 | 210 |
| 208 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)aView { | 211 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)aView { |
| 209 // Draw image with |DrawImageInRect()| to get consistent | 212 // Draw image with |DrawImageInRect()| to get consistent |
| 210 // flipped treatment. | 213 // flipped treatment. |
| 211 DrawImageInRect([self image], aView, cellFrame); | 214 DrawImageInRect([self image], aView, cellFrame); |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 // TODO(shess): This could be better. Show the hint until the | 382 // TODO(shess): This could be better. Show the hint until the |
| 380 // non-hint text bumps against it? | 383 // non-hint text bumps against it? |
| 381 if (hintWidth < NSWidth(textFrame)) { | 384 if (hintWidth < NSWidth(textFrame)) { |
| 382 textFrame.size.width -= hintWidth; | 385 textFrame.size.width -= hintWidth; |
| 383 } | 386 } |
| 384 } | 387 } |
| 385 | 388 |
| 386 return textFrame; | 389 return textFrame; |
| 387 } | 390 } |
| 388 | 391 |
| 392 - (NSRect)textCursorFrameForFrame:(NSRect)cellFrame { |
| 393 std::vector<LocationBarDecoration*> decorations; |
| 394 std::vector<NSRect> decorationFrames; |
| 395 NSRect textFrame; |
| 396 size_t left_count = |
| 397 CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, |
| 398 &decorations, &decorationFrames, &textFrame); |
| 399 |
| 400 // Determine the left-most extent for the i-beam cursor. |
| 401 CGFloat minX = NSMinX(textFrame); |
| 402 for (size_t index = left_count; index--; ) { |
| 403 if (decorations[index]->AcceptsMousePress()) |
| 404 break; |
| 405 |
| 406 // If at leftmost decoration, expand to edge of cell. |
| 407 if (!index) { |
| 408 minX = NSMinX(cellFrame); |
| 409 } else { |
| 410 minX = NSMinX(decorationFrames[index]) - kDecorationHorizontalPad; |
| 411 } |
| 412 } |
| 413 |
| 414 // Determine the right-most extent for the i-beam cursor. |
| 415 CGFloat maxX = NSMaxX(textFrame); |
| 416 for (size_t index = left_count; index < decorations.size(); ++index) { |
| 417 if (decorations[index]->AcceptsMousePress()) |
| 418 break; |
| 419 |
| 420 // If at rightmost decoration, expand to edge of cell. |
| 421 if (index == decorations.size() - 1) { |
| 422 maxX = NSMaxX(cellFrame); |
| 423 } else { |
| 424 maxX = NSMaxX(decorationFrames[index]) + kDecorationHorizontalPad; |
| 425 } |
| 426 } |
| 427 |
| 428 // I-beam cursor covers left-most to right-most. |
| 429 return NSMakeRect(minX, NSMinY(textFrame), maxX - minX, NSHeight(textFrame)); |
| 430 } |
| 431 |
| 389 - (void)drawHintWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 432 - (void)drawHintWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 390 DCHECK(hintString_); | 433 DCHECK(hintString_); |
| 391 | 434 |
| 392 NSRect textFrame = [self textFrameForFrame:cellFrame]; | 435 NSRect textFrame = [self textFrameForFrame:cellFrame]; |
| 393 NSRect infoFrame(NSMakeRect(NSMaxX(textFrame), | 436 NSRect infoFrame(NSMakeRect(NSMaxX(textFrame), |
| 394 cellFrame.origin.y + kHintYOffset, | 437 cellFrame.origin.y + kHintYOffset, |
| 395 ceil([hintString_ size].width), | 438 ceil([hintString_ size].width), |
| 396 cellFrame.size.height - kHintYOffset)); | 439 cellFrame.size.height - kHintYOffset)); |
| 397 [hintString_.get() drawInRect:infoFrame]; | 440 [hintString_.get() drawInRect:infoFrame]; |
| 398 } | 441 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 if (decoration) | 502 if (decoration) |
| 460 return decoration->GetMenu(); | 503 return decoration->GetMenu(); |
| 461 return nil; | 504 return nil; |
| 462 } | 505 } |
| 463 | 506 |
| 464 - (BOOL)mouseDown:(NSEvent*)theEvent | 507 - (BOOL)mouseDown:(NSEvent*)theEvent |
| 465 inRect:(NSRect)cellFrame | 508 inRect:(NSRect)cellFrame |
| 466 ofView:(AutocompleteTextField*)controlView { | 509 ofView:(AutocompleteTextField*)controlView { |
| 467 LocationBarDecoration* decoration = | 510 LocationBarDecoration* decoration = |
| 468 [self decorationForEvent:theEvent inRect:cellFrame ofView:controlView]; | 511 [self decorationForEvent:theEvent inRect:cellFrame ofView:controlView]; |
| 469 if (!decoration) | 512 if (!decoration || !decoration->AcceptsMousePress()) |
| 470 return NO; | 513 return NO; |
| 471 | 514 |
| 472 NSRect decorationRect = | 515 NSRect decorationRect = |
| 473 [self frameForDecoration:decoration inFrame:cellFrame]; | 516 [self frameForDecoration:decoration inFrame:cellFrame]; |
| 474 | 517 |
| 475 // If the decoration is draggable, then initiate a drag if the user | 518 // If the decoration is draggable, then initiate a drag if the user |
| 476 // drags or holds the mouse down for awhile. | 519 // drags or holds the mouse down for awhile. |
| 477 if (decoration->IsDraggable()) { | 520 if (decoration->IsDraggable()) { |
| 478 NSDate* timeout = | 521 NSDate* timeout = |
| 479 [NSDate dateWithTimeIntervalSinceNow:kLocationIconDragTimeout]; | 522 [NSDate dateWithTimeIntervalSinceNow:kLocationIconDragTimeout]; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 return NO; | 558 return NO; |
| 516 | 559 |
| 517 return YES; | 560 return YES; |
| 518 } | 561 } |
| 519 | 562 |
| 520 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal { | 563 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal { |
| 521 return NSDragOperationCopy; | 564 return NSDragOperationCopy; |
| 522 } | 565 } |
| 523 | 566 |
| 524 @end | 567 @end |
| OLD | NEW |