summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/styled_text_field_cell.mm
blob: b85d2ed49a9f2a620579188ee4464c9ef3beb526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/cocoa/styled_text_field_cell.h"

#include "app/gfx/font.h"
#include "app/resource_bundle.h"
#include "base/logging.h"
#include "chrome/browser/browser_theme_provider.h"
#import "chrome/browser/cocoa/themed_window.h"
#include "grit/theme_resources.h"

@implementation StyledTextFieldCell

- (CGFloat)baselineAdjust {
  return 0.0;
}

// Returns the same value as textCursorFrameForFrame, but does not call it
// directly to avoid potential infinite loops.
- (NSRect)textFrameForFrame:(NSRect)cellFrame {
  return NSInsetRect(cellFrame, 0, [self baselineAdjust]);
}

// Returns the same value as textFrameForFrame, but does not call it directly to
// avoid potential infinite loops.
- (NSRect)textCursorFrameForFrame:(NSRect)cellFrame {
  return NSInsetRect(cellFrame, 0, [self baselineAdjust]);
}

// Override to show the I-beam cursor only in the area given by
// |textCursorFrameForFrame:|.
- (void)resetCursorRect:(NSRect)cellFrame inView:(NSView *)controlView {
  [super resetCursorRect:[self textCursorFrameForFrame:cellFrame]
                  inView:controlView];
}

// For NSTextFieldCell this is the area within the borders.  For our
// purposes, we count the info decorations as being part of the
// border.
- (NSRect)drawingRectForBounds:(NSRect)theRect {
  return [super drawingRectForBounds:[self textFrameForFrame:theRect]];
}

// TODO(shess): This code is manually drawing the cell's border area,
// but otherwise the cell assumes -setBordered:YES for purposes of
// calculating things like the editing area.  This is probably
// incorrect.  I know that this affects -drawingRectForBounds:.
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
  DCHECK([controlView isFlipped]);

  // TODO(shess): This inset is also reflected in ToolbarController
  // -autocompletePopupPosition.
  NSRect frame = NSInsetRect(cellFrame, 0, 1);
  NSRect midFrame = NSInsetRect(frame, 0.5, 0.5);
  NSRect innerFrame = NSInsetRect(frame, 1, 1);

  // Paint button background image if there is one (otherwise the border won't
  // look right).
  ThemeProvider* themeProvider = [[controlView window] themeProvider];
  NSColor* backgroundImageColor = nil;
  if (themeProvider) {
    backgroundImageColor =
        themeProvider->GetNSImageColorNamed(IDR_THEME_BUTTON_BACKGROUND, false);
  }
  if (backgroundImageColor) {
    [backgroundImageColor set];
    // Set the phase to match window.
    NSRect trueRect = [controlView convertRect:cellFrame toView:nil];
    [[NSGraphicsContext currentContext]
        setPatternPhase:NSMakePoint(NSMinX(trueRect), NSMaxY(trueRect))];
    NSRectFillUsingOperation(midFrame, NSCompositeCopy);
  }

  // Draw the outer stroke (over the background).
  BOOL active = [[controlView window] isMainWindow];
  if (themeProvider) {
    NSColor* strokeColor = themeProvider->GetNSColor(
        active ? BrowserThemeProvider::COLOR_TOOLBAR_BUTTON_STROKE :
                 BrowserThemeProvider::COLOR_TOOLBAR_BUTTON_STROKE_INACTIVE,
        true);
    [strokeColor set];
  }
  NSFrameRectWithWidthUsingOperation(frame, 1, NSCompositeSourceOver);

  // Draw the background for the interior.
  [[self backgroundColor] setFill];
  NSRectFill(innerFrame);

  // Draw the shadow.
  NSRect topShadowFrame, leftShadowFrame, restFrame;
  NSDivideRect(innerFrame, &topShadowFrame, &restFrame, 1, NSMinYEdge);
  NSDivideRect(restFrame, &leftShadowFrame, &restFrame, 1, NSMinXEdge);
  [[NSColor colorWithCalibratedWhite:0.0 alpha:0.05] setFill];
  NSRectFillUsingOperation(topShadowFrame, NSCompositeSourceOver);
  NSRectFillUsingOperation(leftShadowFrame, NSCompositeSourceOver);

  // Draw the focus ring if needed.
  if ([self showsFirstResponder]) {
    [[[NSColor keyboardFocusIndicatorColor] colorWithAlphaComponent:0.5] set];
    NSFrameRectWithWidthUsingOperation(NSInsetRect(frame, 0, 0), 2,
                                       NSCompositeSourceOver);
  }

  [self drawInteriorWithFrame:cellFrame inView:controlView];
}

@end