 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| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 #include "chrome/browser/ui/libgtk2ui/gtk2_border.h" | |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 | |
| 9 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h" | |
| 10 #include "ui/gfx/canvas.h" | |
| 11 #include "ui/gfx/image/image_skia_source.h" | |
| 12 #include "ui/views/controls/button/custom_button.h" | |
| 13 | |
| 14 namespace libgtk2ui { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 GtkStateType ViewsToGtkState(views::Button::ButtonState state) { | |
| 19 switch (state) { | |
| 20 case views::Button::STATE_HOVERED: | |
| 21 return GTK_STATE_PRELIGHT; | |
| 22 case views::Button::STATE_PRESSED: | |
| 23 return GTK_STATE_ACTIVE; | |
| 24 case views::Button::STATE_DISABLED: | |
| 25 return GTK_STATE_INSENSITIVE; | |
| 26 default: | |
| 27 return GTK_STATE_NORMAL; | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 class ButtonImageSkiaSource : public gfx::ImageSkiaSource { | |
| 32 public: | |
| 33 ButtonImageSkiaSource(const Gtk2UI* gtk2_ui, | |
| 34 GtkStateType state, | |
| 35 const gfx::Size& size) | |
| 36 : gtk2_ui_(gtk2_ui), | |
| 37 state_(state), | |
| 38 size_(size) { | |
| 39 } | |
| 40 | |
| 41 virtual ~ButtonImageSkiaSource() { | |
| 42 } | |
| 43 | |
| 44 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE { | |
| 45 int w = size_.width() * scale; | |
| 46 int h = size_.height() * scale; | |
| 47 return gfx::ImageSkiaRep( | |
| 48 gtk2_ui_->DrawGtkButtonBorder(state_, w, h), scale); | |
| 49 } | |
| 50 | |
| 51 private: | |
| 52 const Gtk2UI* gtk2_ui_; | |
| 53 GtkStateType state_; | |
| 
sky
2014/01/22 20:23:59
nit: const and this and size_.
 | |
| 54 gfx::Size size_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(ButtonImageSkiaSource); | |
| 57 }; | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 Gtk2Border::Gtk2Border(Gtk2UI* gtk2_ui, | |
| 62 views::CustomButton* owning_button, | |
| 63 views::Border* border) | |
| 64 : gtk2_ui_(gtk2_ui), | |
| 65 use_gtk_(gtk2_ui_->GetUseSystemTheme()), | |
| 66 owning_button_(owning_button), | |
| 67 border_(border) { | |
| 68 gtk2_ui_->AddGtkBorder(this); | |
| 69 } | |
| 70 | |
| 71 Gtk2Border::~Gtk2Border() { | |
| 72 gtk2_ui_->RemoveGtkBorder(this); | |
| 73 } | |
| 74 | |
| 75 void Gtk2Border::InvalidateAndSetUsesGtk(bool use_gtk) { | |
| 76 hovered_ = gfx::ImageSkia(); | |
| 77 pressed_ = gfx::ImageSkia(); | |
| 78 | |
| 79 // Our owning view must have its layout invalidated because the insets could | |
| 80 // have changed. | |
| 81 owning_button_->InvalidateLayout(); | |
| 82 | |
| 83 use_gtk_ = use_gtk; | |
| 84 } | |
| 85 | |
| 86 void Gtk2Border::Paint(const views::View& view, gfx::Canvas* canvas) { | |
| 87 if (!use_gtk_) { | |
| 88 border_->Paint(view, canvas); | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 DCHECK_EQ(&view, owning_button_); | |
| 93 GtkStateType state = ViewsToGtkState(owning_button_->state()); | |
| 94 | |
| 95 if (state == GTK_STATE_PRELIGHT) { | |
| 96 if (hovered_.isNull() || hovered_.size() != view.size()) { | |
| 97 hovered_ = gfx::ImageSkia( | |
| 98 new ButtonImageSkiaSource(gtk2_ui_, state, view.size()), view.size()); | |
| 99 } | |
| 100 | |
| 101 canvas->DrawImageInt(hovered_, 0, 0); | |
| 102 } else if (state == GTK_STATE_ACTIVE) { | |
| 103 if (pressed_.isNull() || pressed_.size() != view.size()) { | |
| 104 pressed_ = gfx::ImageSkia( | |
| 105 new ButtonImageSkiaSource(gtk2_ui_, state, view.size()), view.size()); | |
| 106 } | |
| 107 | |
| 108 canvas->DrawImageInt(pressed_, 0, 0); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 gfx::Insets Gtk2Border::GetInsets() const { | |
| 113 if (!use_gtk_) | |
| 114 return border_->GetInsets(); | |
| 115 | |
| 116 return gtk2_ui_->GetButtonInsets(); | |
| 117 } | |
| 118 | |
| 119 gfx::Size Gtk2Border::GetMinimumSize() const { | |
| 120 if (!use_gtk_) | |
| 121 return border_->GetMinimumSize(); | |
| 122 | |
| 123 gfx::Insets insets = GetInsets(); | |
| 124 return gfx::Size(insets.width(), insets.height()); | |
| 125 } | |
| 126 | |
| 127 } // namespace libgtk2ui | |
| OLD | NEW |