Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: chrome/views/text_button.cc

Issue 7344: Convert GetPreferredSize from:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #include "chrome/views/text_button.h" 5 #include "chrome/views/text_button.h"
6 6
7 #include "chrome/app/theme/theme_resources.h" 7 #include "chrome/app/theme/theme_resources.h"
8 #include "chrome/common/gfx/chrome_canvas.h" 8 #include "chrome/common/gfx/chrome_canvas.h"
9 #include "chrome/common/resource_bundle.h" 9 #include "chrome/common/resource_bundle.h"
10 #include "chrome/common/throb_animation.h" 10 #include "chrome/common/throb_animation.h"
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 kPreferredPaddingVertical, kPreferredPaddingHorizontal); 136 kPreferredPaddingVertical, kPreferredPaddingHorizontal);
137 } 137 }
138 138
139 //////////////////////////////////////////////////////////////////////////////// 139 ////////////////////////////////////////////////////////////////////////////////
140 // 140 //
141 // TextButton - Implementation 141 // TextButton - Implementation
142 // 142 //
143 //////////////////////////////////////////////////////////////////////////////// 143 ////////////////////////////////////////////////////////////////////////////////
144 144
145 TextButton::TextButton(const std::wstring& text) 145 TextButton::TextButton(const std::wstring& text)
146 : max_text_size_(CSize(0, 0)), 146 : font_(ResourceBundle::GetSharedInstance().GetFont(
147 font_(ResourceBundle::GetSharedInstance().GetFont(
148 ResourceBundle::BaseFont)), 147 ResourceBundle::BaseFont)),
149 color_(kEnabledColor), 148 color_(kEnabledColor),
150 BaseButton(), 149 BaseButton(),
151 max_width_(0), 150 max_width_(0),
152 alignment_(ALIGN_LEFT) { 151 alignment_(ALIGN_LEFT) {
153 SetText(text); 152 SetText(text);
154 SetBorder(new TextButtonBorder); 153 SetBorder(new TextButtonBorder);
155 SetAnimationDuration(kHoverAnimationDurationMs); 154 SetAnimationDuration(kHoverAnimationDurationMs);
156 } 155 }
157 156
158 TextButton::~TextButton() { 157 TextButton::~TextButton() {
159 } 158 }
160 159
161 void TextButton::GetPreferredSize(CSize *result) { 160 gfx::Size TextButton::GetPreferredSize() {
162 gfx::Insets insets = GetInsets(); 161 gfx::Insets insets = GetInsets();
163 162
164 // Use the max size to set the button boundaries. 163 // Use the max size to set the button boundaries.
165 result->cx = max_text_size_.cx + icon_.width() + insets.width(); 164 gfx::Size prefsize(max_text_size_.width() + icon_.width() + insets.width(),
166 result->cy = std::max(static_cast<int>(max_text_size_.cy), icon_.height()) + 165 std::max(max_text_size_.height(), icon_.height()) +
167 insets.height(); 166 insets.height());
168 167
169 if (icon_.width() > 0 && !text_.empty()) 168 if (icon_.width() > 0 && !text_.empty())
170 result->cx += kIconTextPadding; 169 prefsize.Enlarge(kIconTextPadding, 0);
171 170
172 if (max_width_ > 0) 171 if (max_width_ > 0)
173 result->cx = std::min(max_width_, static_cast<int>(result->cx)); 172 prefsize.set_width(std::min(max_width_, prefsize.width()));
173
174 return prefsize;
174 } 175 }
175 176
176 void TextButton::GetMinimumSize(CSize *result) { 177 gfx::Size TextButton::GetMinimumSize() {
177 result->cx = max_text_size_.cx; 178 return max_text_size_;
178 result->cy = max_text_size_.cy;
179 } 179 }
180 180
181 bool TextButton::OnMousePressed(const ChromeViews::MouseEvent& e) { 181 bool TextButton::OnMousePressed(const ChromeViews::MouseEvent& e) {
182 return true; 182 return true;
183 } 183 }
184 184
185 void TextButton::SetText(const std::wstring& text) { 185 void TextButton::SetText(const std::wstring& text) {
186 text_ = text; 186 text_ = text;
187 // Update our new current and max text size 187 // Update our new current and max text size
188 text_size_.cx = font_.GetStringWidth(text_); 188 text_size_.SetSize(font_.GetStringWidth(text_), font_.height());
189 text_size_.cy = font_.height(); 189 max_text_size_.SetSize(std::max(max_text_size_.width(), text_size_.width()),
190 max_text_size_.cx = std::max(max_text_size_.cx, text_size_.cx); 190 std::max(max_text_size_.height(),
191 max_text_size_.cy = std::max(max_text_size_.cy, text_size_.cy); 191 text_size_.height()));
192 } 192 }
193 193
194 void TextButton::SetIcon(const SkBitmap& icon) { 194 void TextButton::SetIcon(const SkBitmap& icon) {
195 icon_ = icon; 195 icon_ = icon;
196 } 196 }
197 197
198 void TextButton::ClearMaxTextSize() { 198 void TextButton::ClearMaxTextSize() {
199 max_text_size_ = text_size_; 199 max_text_size_ = text_size_;
200 } 200 }
201 201
(...skipping 18 matching lines...) Expand all
220 PaintBorder(canvas); 220 PaintBorder(canvas);
221 } 221 }
222 222
223 PaintFocusBorder(canvas); 223 PaintFocusBorder(canvas);
224 } 224 }
225 225
226 gfx::Insets insets = GetInsets(); 226 gfx::Insets insets = GetInsets();
227 int available_width = width() - insets.width(); 227 int available_width = width() - insets.width();
228 int available_height = height() - insets.height(); 228 int available_height = height() - insets.height();
229 // Use the actual text (not max) size to properly center the text. 229 // Use the actual text (not max) size to properly center the text.
230 int content_width = text_size_.cx; 230 int content_width = text_size_.width();
231 if (icon_.width() > 0) { 231 if (icon_.width() > 0) {
232 content_width += icon_.width(); 232 content_width += icon_.width();
233 if (!text_.empty()) 233 if (!text_.empty())
234 content_width += kIconTextPadding; 234 content_width += kIconTextPadding;
235 } 235 }
236 // Place the icon along the left edge. 236 // Place the icon along the left edge.
237 int icon_x; 237 int icon_x;
238 if (alignment_ == ALIGN_LEFT) { 238 if (alignment_ == ALIGN_LEFT) {
239 icon_x = insets.left(); 239 icon_x = insets.left();
240 } else if (alignment_ == ALIGN_RIGHT) { 240 } else if (alignment_ == ALIGN_RIGHT) {
241 icon_x = available_width - content_width; 241 icon_x = available_width - content_width;
242 } else { 242 } else {
243 icon_x = 243 icon_x =
244 std::max(0, (available_width - content_width) / 2) + insets.left(); 244 std::max(0, (available_width - content_width) / 2) + insets.left();
245 } 245 }
246 int text_x = icon_x; 246 int text_x = icon_x;
247 if (icon_.width() > 0) 247 if (icon_.width() > 0)
248 text_x += icon_.width() + kIconTextPadding; 248 text_x += icon_.width() + kIconTextPadding;
249 const int text_width = std::min(static_cast<int>(text_size_.cx), 249 const int text_width = std::min(text_size_.width(),
250 width() - insets.right() - text_x); 250 width() - insets.right() - text_x);
251 int text_y = (available_height - text_size_.cy) / 2 + insets.top(); 251 int text_y = (available_height - text_size_.height()) / 2 + insets.top();
252 252
253 if (text_width > 0) { 253 if (text_width > 0) {
254 // Because the text button can (at times) draw multiple elements on the 254 // Because the text button can (at times) draw multiple elements on the
255 // canvas, we can not mirror the button by simply flipping the canvas as 255 // canvas, we can not mirror the button by simply flipping the canvas as
256 // doing this will mirror the text itself. Flipping the canvas will also 256 // doing this will mirror the text itself. Flipping the canvas will also
257 // make the icons look wrong because icons are almost always represented as 257 // make the icons look wrong because icons are almost always represented as
258 // direction insentisive bitmaps and such bitmaps should never be flipped 258 // direction insentisive bitmaps and such bitmaps should never be flipped
259 // horizontally. 259 // horizontally.
260 // 260 //
261 // Due to the above, we must perform the flipping manually for RTL UIs. 261 // Due to the above, we must perform the flipping manually for RTL UIs.
262 gfx::Rect text_bounds(text_x, text_y, text_width, text_size_.cy); 262 gfx::Rect text_bounds(text_x, text_y, text_width, text_size_.height());
263 text_bounds.set_x(MirroredLeftPointForRect(text_bounds)); 263 text_bounds.set_x(MirroredLeftPointForRect(text_bounds));
264 264
265 // Draw bevel highlight 265 // Draw bevel highlight
266 canvas->DrawStringInt(text_, 266 canvas->DrawStringInt(text_,
267 font_, 267 font_,
268 kHighlightColor, 268 kHighlightColor,
269 text_bounds.x() + 1, 269 text_bounds.x() + 1,
270 text_bounds.y() + 1, 270 text_bounds.y() + 1,
271 text_bounds.width(), 271 text_bounds.width(),
272 text_bounds.height()); 272 text_bounds.height());
(...skipping 20 matching lines...) Expand all
293 void TextButton::SetEnabled(bool enabled) { 293 void TextButton::SetEnabled(bool enabled) {
294 if (enabled == IsEnabled()) 294 if (enabled == IsEnabled())
295 return; 295 return;
296 BaseButton::SetEnabled(enabled); 296 BaseButton::SetEnabled(enabled);
297 color_ = enabled ? kEnabledColor : kDisabledColor; 297 color_ = enabled ? kEnabledColor : kDisabledColor;
298 SchedulePaint(); 298 SchedulePaint();
299 } 299 }
300 300
301 } // namespace ChromeViews 301 } // namespace ChromeViews
302 302
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698