File: button_decoration.mm

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (182 lines) | stat: -rw-r--r-- 5,366 bytes parent folder | download | duplicates (2)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2012 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.

#include <cmath>

#import "chrome/browser/ui/cocoa/location_bar/button_decoration.h"

#include "base/logging.h"
#import "base/mac/scoped_nsobject.h"
#import "ui/base/cocoa/appkit_utils.h"
#include "ui/base/resource/resource_bundle.h"

namespace {

NSImage* GetImageFromId(int image_id) {
  return ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(image_id)
      .ToNSImage();
}

bool IsValidNinePartImageIds(const ui::NinePartImageIds& object) {
  return (object.top_left >= 0) &&
         (object.top >= 0) &&
         (object.top_right >= 0) &&
         (object.left >= 0) &&
         (object.center >= 0) &&
         (object.right >= 0) &&
         (object.bottom_left >= 0) &&
         (object.bottom >= 0) &&
         (object.bottom_right >= 0);
}

}  // namespace

ButtonDecoration::ButtonDecoration(ui::NinePartImageIds normal_image_ids,
                                   int normal_icon_id,
                                   ui::NinePartImageIds hover_image_ids,
                                   int hover_icon_id,
                                   ui::NinePartImageIds pressed_image_ids,
                                   int pressed_icon_id,
                                   CGFloat max_inner_padding)
    : normal_image_ids_(normal_image_ids),
      hover_image_ids_(hover_image_ids),
      pressed_image_ids_(pressed_image_ids),
      normal_icon_id_(normal_icon_id),
      hover_icon_id_(hover_icon_id),
      pressed_icon_id_(pressed_icon_id),
      state_(kButtonStateNormal),
      max_inner_padding_(max_inner_padding) {
  DCHECK(IsValidNinePartImageIds(normal_image_ids_) && normal_icon_id_ >= 0);
  DCHECK(IsValidNinePartImageIds(hover_image_ids_) && hover_icon_id_ >= 0);
  DCHECK(IsValidNinePartImageIds(pressed_image_ids_) && pressed_icon_id_ >= 0);
  DCHECK_GE(max_inner_padding_, 0);
}

ButtonDecoration::~ButtonDecoration() {
}

void ButtonDecoration::SetButtonState(ButtonDecoration::ButtonState state) {
  state_ = state;
}

ButtonDecoration::ButtonState ButtonDecoration::GetButtonState() const {
  return state_;
}

bool ButtonDecoration::PreventFocus(NSPoint location) const {
  return false;
}

void ButtonDecoration::SetIcon(ButtonState state, int icon_id) {
  switch (state) {
    case kButtonStateNormal:
      normal_icon_id_ = icon_id;
      break;
    case kButtonStateHover:
      hover_icon_id_ = icon_id;
      break;
    case kButtonStatePressed:
      pressed_icon_id_ = icon_id;
      break;
    default:
      NOTREACHED();
  }
}

void ButtonDecoration::SetIcon(int icon_id) {
  normal_icon_id_ = icon_id;
  hover_icon_id_ = icon_id;
  pressed_icon_id_ = icon_id;
}

void ButtonDecoration::SetBackgroundImageIds(
    ui::NinePartImageIds normal_image_ids,
    ui::NinePartImageIds hover_image_ids,
    ui::NinePartImageIds pressed_image_ids) {
  DCHECK(IsValidNinePartImageIds(normal_image_ids));
  DCHECK(IsValidNinePartImageIds(hover_image_ids));
  DCHECK(IsValidNinePartImageIds(pressed_image_ids));
  normal_image_ids_ = normal_image_ids;
  hover_image_ids_ = hover_image_ids;
  pressed_image_ids_ = pressed_image_ids;
}

ui::NinePartImageIds ButtonDecoration::GetBackgroundImageIds() const {
  switch (state_) {
    case kButtonStateHover:
      return hover_image_ids_;
    case kButtonStatePressed:
      return pressed_image_ids_;
    case kButtonStateNormal:
    default:
      return normal_image_ids_;
  }
}

NSImage* ButtonDecoration::GetIconImage() const {
  switch (state_) {
    case kButtonStateHover:
      return GetImageFromId(hover_icon_id_);
    case kButtonStatePressed:
      return GetImageFromId(pressed_icon_id_);
    case kButtonStateNormal:
    default:
      return GetImageFromId(normal_icon_id_);
  }
}

CGFloat ButtonDecoration::GetWidthForSpace(CGFloat width) {
  const ui::NinePartImageIds image_ids = GetBackgroundImageIds();
  NSImage* icon = GetIconImage();

  if (!icon)
    return kOmittedWidth;

  const CGFloat min_width = [GetImageFromId(image_ids.left) size].width +
                            [icon size].width +
                            [GetImageFromId(image_ids.right) size].width;
  if (width < min_width)
    return kOmittedWidth;

  const CGFloat max_width = min_width + 2 * max_inner_padding_;
  return std::min(width, max_width);
}

void ButtonDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
  const ui::NinePartImageIds image_ids = GetBackgroundImageIds();
  NSImage* icon = GetIconImage();

  if (!icon)
    return;

  ui::DrawNinePartImage(frame, image_ids, NSCompositeSourceOver, 1.0, YES);

  const CGFloat x_inset =
      std::floor((NSWidth(frame) - [icon size].width) / 2.0);
  const CGFloat y_inset =
      std::floor((NSHeight(frame) - [icon size].height) / 2.0);

  [icon drawInRect:NSInsetRect(frame, x_inset, y_inset)
          fromRect:NSZeroRect
         operation:NSCompositeSourceOver
          fraction:1.0
    respectFlipped:YES
             hints:nil];
}

bool ButtonDecoration::AcceptsMousePress() {
  return true;
}

bool ButtonDecoration::IsDraggable() {
  return false;
}

bool ButtonDecoration::OnMousePressed(NSRect frame, NSPoint location) {
  return false;
}

ButtonDecoration* ButtonDecoration::AsButtonDecoration() {
  return this;
}