 Chromium Code Reviews
 Chromium Code Reviews Issue 131513005:
  linux_aura: Use GTK button borders in GTK theme mode.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 131513005:
  linux_aura: Use GTK button borders in GTK theme mode.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: chrome/browser/ui/libgtk2ui/gtk2_border.cc | 
| diff --git a/chrome/browser/ui/libgtk2ui/gtk2_border.cc b/chrome/browser/ui/libgtk2ui/gtk2_border.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..bc33cab62713fbfcd570896d6f742b82a998a159 | 
| --- /dev/null | 
| +++ b/chrome/browser/ui/libgtk2ui/gtk2_border.cc | 
| @@ -0,0 +1,125 @@ | 
| +// Copyright 2014 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 "chrome/browser/ui/libgtk2ui/gtk2_border.h" | 
| + | 
| +#include <gtk/gtk.h> | 
| + | 
| +#include "chrome/browser/ui/libgtk2ui/gtk2_ui.h" | 
| +#include "ui/gfx/canvas.h" | 
| +#include "ui/gfx/image/image_skia_source.h" | 
| +#include "ui/views/controls/button/button.h" | 
| +#include "ui/views/controls/button/label_button.h" | 
| 
msw
2014/01/21 19:18:19
nit: the button.h include should be sufficient; re
 | 
| +#include "ui/views/controls/button/label_button_border.h" | 
| 
msw
2014/01/21 19:18:19
nit: the border.h include in the header should be
 | 
| + | 
| +namespace libgtk2ui { | 
| + | 
| +namespace { | 
| + | 
| +int ViewsToGtkState(views::Button::ButtonState state) { | 
| 
msw
2014/01/21 19:18:19
nit: should this return a GtkStateType?
 | 
| + switch (state) { | 
| + case views::Button::STATE_HOVERED: | 
| + return GTK_STATE_PRELIGHT; | 
| + case views::Button::STATE_PRESSED: | 
| + return GTK_STATE_ACTIVE; | 
| + default: | 
| 
msw
2014/01/21 19:18:19
nit: handle Button::STATE_DISABLED / GTK_STATE_INS
 | 
| + return GTK_STATE_NORMAL; | 
| + } | 
| +} | 
| + | 
| +class ButtonImageSkiaSource : public gfx::ImageSkiaSource { | 
| + public: | 
| + ButtonImageSkiaSource(const Gtk2UI* parent, int state, const gfx::Size& size) | 
| + : parent_(parent), | 
| 
msw
2014/01/21 19:18:19
ditto nit: This isn't really the parent, should it
 | 
| + state_(state), | 
| + size_(size) { | 
| + } | 
| + | 
| + virtual ~ButtonImageSkiaSource() { | 
| + } | 
| + | 
| + virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE { | 
| + int w = size_.width() * scale; | 
| + int h = size_.height() * scale; | 
| + return gfx::ImageSkiaRep(parent_->DrawGtkButtonBorder(state_, w, h), scale); | 
| + } | 
| + | 
| + private: | 
| + const Gtk2UI* parent_; | 
| + int state_; | 
| 
msw
2014/01/21 19:18:19
Should this be a GtkStateType?
 | 
| + gfx::Size size_; | 
| + | 
| + DISALLOW_COPY_AND_ASSIGN(ButtonImageSkiaSource); | 
| +}; | 
| + | 
| +} // namespace | 
| + | 
| +Gtk2Border::Gtk2Border(Gtk2UI* parent, | 
| + views::View* owning_view, | 
| + views::LabelButtonBorder* label_button_border) | 
| + : parent_(parent), | 
| + use_gtk_(parent_->GetUseSystemTheme()), | 
| + owning_view_(owning_view), | 
| + label_button_border_(label_button_border) { | 
| + parent_->AddGtkBorder(this); | 
| +} | 
| + | 
| +Gtk2Border::~Gtk2Border() { | 
| + parent_->RemoveGtkBorder(this); | 
| +} | 
| + | 
| +void Gtk2Border::InvalidateAndSetUsesGtk(bool use_gtk) { | 
| + hovered_ = gfx::ImageSkia(); | 
| + pressed_ = gfx::ImageSkia(); | 
| + | 
| + // Our owning view must have its layout invalidated because the insets could | 
| + // have changed. | 
| + owning_view_->InvalidateLayout(); | 
| + | 
| + use_gtk_ = use_gtk; | 
| +} | 
| + | 
| +void Gtk2Border::Paint(const views::View& view, gfx::Canvas* canvas) { | 
| + if (!use_gtk_) { | 
| + label_button_border_->Paint(view, canvas); | 
| + return; | 
| + } | 
| + | 
| + int state = ViewsToGtkState( | 
| 
msw
2014/01/21 19:18:19
nit: should this be a GtkStateType?
 | 
| + static_cast<const views::LabelButton&>(view).state()); | 
| 
msw
2014/01/21 19:18:19
nit: DCHECK_EQ(view, owning_view_), and use owning
 | 
| + | 
| + if (state == GTK_STATE_PRELIGHT) { | 
| + if (hovered_.isNull() || hovered_.size() != view.size()) { | 
| + hovered_ = gfx::ImageSkia( | 
| + new ButtonImageSkiaSource(parent_, state, view.size()), view.size()); | 
| + } | 
| + | 
| + canvas->DrawImageInt(hovered_, 0, 0); | 
| + } else if (state == GTK_STATE_ACTIVE) { | 
| + if (pressed_.isNull() || pressed_.size() != view.size()) { | 
| + pressed_ = gfx::ImageSkia( | 
| + new ButtonImageSkiaSource(parent_, state, view.size()), view.size()); | 
| + } | 
| + | 
| + canvas->DrawImageInt(pressed_, 0, 0); | 
| + } | 
| +} | 
| + | 
| +gfx::Insets Gtk2Border::GetInsets() const { | 
| + if (!use_gtk_) | 
| + return label_button_border_->GetInsets(); | 
| + | 
| + return parent_->GetButtonInsets(); | 
| +} | 
| + | 
| +gfx::Size Gtk2Border::GetMinimumSize() const { | 
| + if (!use_gtk_) | 
| + return label_button_border_->GetMinimumSize(); | 
| + | 
| + gfx::Insets insets = GetInsets(); | 
| + return gfx::Size(insets.left() + insets.right(), | 
| 
msw
2014/01/21 19:18:19
nit: use gfx::Insets::width() and height().
 | 
| + insets.top() + insets.bottom()); | 
| +} | 
| + | 
| +} // namespace libgtk2ui |