Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #import "chrome/browser/ui/cocoa/styled_text_field.h"
      6 
      7 #include "base/logging.h"
      8 #import "chrome/browser/ui/cocoa/styled_text_field_cell.h"
      9 
     10 @implementation StyledTextField
     11 
     12 - (StyledTextFieldCell*)styledTextFieldCell {
     13   DCHECK([[self cell] isKindOfClass:[StyledTextFieldCell class]]);
     14   return static_cast<StyledTextFieldCell*>([self cell]);
     15 }
     16 
     17 // Cocoa text fields are edited by placing an NSTextView as subview,
     18 // positioned by the cell's -editWithFrame:inView:... method.  Using
     19 // the standard -makeFirstResponder: machinery to reposition the field
     20 // editor results in resetting the field editor's editing state, which
     21 // AutocompleteEditViewMac monitors.  This causes problems because
     22 // editing can require the field editor to be repositioned, which
     23 // could disrupt editing.  This code repositions the subview directly,
     24 // which causes no editing-state changes.
     25 - (void)resetFieldEditorFrameIfNeeded {
     26   // No action if not editing.
     27   NSText* editor = [self currentEditor];
     28   if (!editor) {
     29     return;
     30   }
     31 
     32   // When editing, we should have exactly one subview, which is a
     33   // clipview containing the editor (for purposes of scrolling).
     34   NSArray* subviews = [self subviews];
     35   DCHECK_EQ([subviews count], 1U);
     36   DCHECK([editor isDescendantOf:self]);
     37   if ([subviews count] == 0) {
     38     return;
     39   }
     40 
     41   // If the frame is already right, don't make any visible changes.
     42   StyledTextFieldCell* cell = [self styledTextFieldCell];
     43   const NSRect frame([cell drawingRectForBounds:[self bounds]]);
     44   NSView* subview = [subviews objectAtIndex:0];
     45   if (NSEqualRects(frame, [subview frame])) {
     46     return;
     47   }
     48 
     49   [subview setFrame:frame];
     50 
     51   // Make sure the selection remains visible.
     52   [editor scrollRangeToVisible:[editor selectedRange]];
     53 }
     54 
     55 - (CGFloat)availableDecorationWidth {
     56   NSAttributedString* as = [self attributedStringValue];
     57   const NSSize size([as size]);
     58   const NSRect bounds([self bounds]);
     59   return NSWidth(bounds) - size.width;
     60 }
     61 
     62 - (NSFocusRingType)focusRingType {
     63   // This is taken care of by the custom drawing code in the cell.
     64   return NSFocusRingTypeNone;
     65 }
     66 
     67 // Due to theming, parts of the field are transparent.
     68 - (BOOL)isOpaque {
     69   return NO;
     70 }
     71 
     72 @end
     73