~brandontschaefer/unity/fix-915828

« back to all changes in this revision

Viewing changes to plugins/unityshell/src/FilterBasicButton.cpp

  • Committer: Brandon Schaefer
  • Date: 2012-02-02 04:05:22 UTC
  • mfrom: (1827.1.59 unity)
  • Revision ID: brandontschaefer@gmail.com-20120202040522-r86246d1fxmclh4p
merged

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * Authored by: Gordon Allott <gord.allott@canonical.com>
19
19
 *
20
20
 */
21
 
#include "config.h"
22
 
 
23
 
#include <Nux/Nux.h>
24
 
#include <NuxCore/Logger.h>
25
21
 
26
22
#include "DashStyle.h"
27
23
#include "FilterBasicButton.h"
28
24
 
29
25
namespace
30
26
{
31
 
nux::logging::Logger logger("unity.dash.FilterBasicButton");
32
 
}
33
 
 
34
 
namespace unity {
35
 
 
36
 
  FilterBasicButton::FilterBasicButton (nux::TextureArea *image, NUX_FILE_LINE_DECL)
37
 
      : nux::ToggleButton (image, NUX_FILE_LINE_PARAM)
38
 
      , prelight_ (NULL)
39
 
      , active_ (NULL)
40
 
      , normal_ (NULL)
41
 
  {
42
 
    InitTheme();
43
 
  }
44
 
 
45
 
  FilterBasicButton::FilterBasicButton (const std::string label, NUX_FILE_LINE_DECL)
46
 
      : nux::ToggleButton (NUX_FILE_LINE_PARAM)
47
 
      , prelight_ (NULL)
48
 
      , active_ (NULL)
49
 
      , normal_ (NULL)
50
 
      , label_ (label)
51
 
  {
52
 
    InitTheme();
53
 
  }
54
 
 
55
 
  FilterBasicButton::FilterBasicButton (const std::string label, nux::TextureArea *image, NUX_FILE_LINE_DECL)
56
 
      : nux::ToggleButton (image, NUX_FILE_LINE_PARAM)
57
 
      , prelight_ (NULL)
58
 
      , active_ (NULL)
59
 
      , normal_ (NULL)
60
 
      , label_ (label)
61
 
  {
62
 
    InitTheme();
63
 
  }
64
 
 
65
 
  FilterBasicButton::FilterBasicButton (NUX_FILE_LINE_DECL)
66
 
      : nux::ToggleButton (NUX_FILE_LINE_PARAM)
67
 
      , prelight_ (NULL)
68
 
      , active_ (NULL)
69
 
      , normal_ (NULL)
70
 
  {
71
 
    InitTheme();
72
 
  }
73
 
 
74
 
  FilterBasicButton::~FilterBasicButton() {
75
 
   delete prelight_;
76
 
    delete active_;
77
 
    delete normal_;
78
 
 
79
 
  }
80
 
 
81
 
  void FilterBasicButton::InitTheme()
82
 
  {
83
 
    if (prelight_ == NULL)
84
 
    {
85
 
      prelight_ = new nux::CairoWrapper(GetGeometry(), sigc::bind(sigc::mem_fun(this, &FilterBasicButton::RedrawTheme), nux::ButtonVisualState::VISUAL_STATE_PRELIGHT));
86
 
      active_ = new nux::CairoWrapper(GetGeometry(), sigc::bind(sigc::mem_fun(this, &FilterBasicButton::RedrawTheme), nux::ButtonVisualState::VISUAL_STATE_PRESSED));
87
 
      normal_ = new nux::CairoWrapper(GetGeometry(), sigc::bind(sigc::mem_fun(this, &FilterBasicButton::RedrawTheme), nux::ButtonVisualState::VISUAL_STATE_NORMAL));
88
 
    }
89
 
 
90
 
   // SetMinimumHeight(32);
91
 
  }
92
 
 
93
 
  void FilterBasicButton::RedrawTheme (nux::Geometry const& geom, cairo_t *cr, nux::ButtonVisualState faked_state)
94
 
  {
95
 
    dash::Style::Instance().Button(cr, faked_state, label_);
96
 
  }
97
 
 
98
 
  long FilterBasicButton::ComputeContentSize ()
99
 
  {
100
 
    long ret = nux::Button::ComputeContentSize();
101
 
    if (cached_geometry_ != GetGeometry())
102
 
    {
103
 
      nux::Geometry geo = GetGeometry();
104
 
      prelight_->Invalidate(geo);
105
 
      active_->Invalidate(geo);
106
 
      normal_->Invalidate(geo);
107
 
    }
108
 
 
109
 
    cached_geometry_ = GetGeometry();
110
 
    return ret;
111
 
  }
112
 
 
113
 
  void FilterBasicButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw) {
114
 
    gPainter.PaintBackground(GfxContext, GetGeometry());
115
 
    // set up our texture mode
116
 
    nux::TexCoordXForm texxform;
117
 
    texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
118
 
    texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
119
 
 
120
 
    // clear what is behind us
121
 
    unsigned int alpha = 0, src = 0, dest = 0;
122
 
    GfxContext.GetRenderStates().GetBlend(alpha, src, dest);
123
 
    GfxContext.GetRenderStates().SetBlend(true, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
124
 
 
125
 
    nux::Color col = nux::color::Black;
126
 
    col.alpha = 0;
127
 
    GfxContext.QRP_Color(GetGeometry().x,
128
 
                         GetGeometry().y,
129
 
                         GetGeometry().width,
130
 
                         GetGeometry().height,
131
 
                         col);
132
 
 
133
 
    nux::BaseTexture *texture = normal_->GetTexture();
134
 
    if (Active())
135
 
      texture = active_->GetTexture();
136
 
    else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRELIGHT)
137
 
      texture = prelight_->GetTexture();
138
 
    else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRESSED)
139
 
    {
140
 
      texture = active_->GetTexture();
141
 
    }
142
 
 
143
 
    GfxContext.QRP_1Tex(GetGeometry().x,
144
 
                        GetGeometry().y,
145
 
                        GetGeometry().width,
146
 
                        GetGeometry().height,
147
 
                        texture->GetDeviceTexture(),
148
 
                        texxform,
149
 
                        nux::Color(1.0f, 1.0f, 1.0f, 1.0f));
150
 
 
151
 
    GfxContext.GetRenderStates().SetBlend(alpha, src, dest);
152
 
  }
153
 
 
154
 
  void FilterBasicButton::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw) {
155
 
  }
156
 
 
157
 
  void FilterBasicButton::PostDraw(nux::GraphicsEngine& GfxContext, bool force_draw) {
158
 
    nux::Button::PostDraw(GfxContext, force_draw);
159
 
  }
160
 
 
161
 
}
 
27
const int kMinButtonHeight = 30;
 
28
const int kMinButtonWidth  = 48;
 
29
}
 
30
 
 
31
namespace unity
 
32
{
 
33
namespace dash
 
34
{
 
35
 
 
36
FilterBasicButton::FilterBasicButton(nux::TextureArea* image, NUX_FILE_LINE_DECL)
 
37
  : nux::ToggleButton(image, NUX_FILE_LINE_PARAM)
 
38
{
 
39
  Init();
 
40
}
 
41
 
 
42
FilterBasicButton::FilterBasicButton(std::string const& label, NUX_FILE_LINE_DECL)
 
43
  : nux::ToggleButton(NUX_FILE_LINE_PARAM)
 
44
  , label_(label)
 
45
{
 
46
  Init();
 
47
}
 
48
 
 
49
FilterBasicButton::FilterBasicButton(std::string const& label, nux::TextureArea* image, NUX_FILE_LINE_DECL)
 
50
  : nux::ToggleButton(image, NUX_FILE_LINE_PARAM)
 
51
  , label_(label)
 
52
{
 
53
  Init();
 
54
}
 
55
 
 
56
FilterBasicButton::FilterBasicButton(NUX_FILE_LINE_DECL)
 
57
  : nux::ToggleButton(NUX_FILE_LINE_PARAM)
 
58
{
 
59
  Init();
 
60
}
 
61
 
 
62
FilterBasicButton::~FilterBasicButton()
 
63
{
 
64
}
 
65
 
 
66
void FilterBasicButton::Init()
 
67
{
 
68
  InitTheme();
 
69
  SetAcceptKeyNavFocusOnMouseDown(false);
 
70
}
 
71
 
 
72
void FilterBasicButton::InitTheme()
 
73
{
 
74
  if (!active_)
 
75
  {
 
76
    nux::Geometry const& geo = GetGeometry();
 
77
 
 
78
    prelight_.reset(new nux::CairoWrapper(geo, sigc::bind(sigc::mem_fun(this, &FilterBasicButton::RedrawTheme), nux::ButtonVisualState::VISUAL_STATE_PRELIGHT)));
 
79
    active_.reset(new nux::CairoWrapper(geo, sigc::bind(sigc::mem_fun(this, &FilterBasicButton::RedrawTheme), nux::ButtonVisualState::VISUAL_STATE_PRESSED)));
 
80
    normal_.reset(new nux::CairoWrapper(geo, sigc::bind(sigc::mem_fun(this, &FilterBasicButton::RedrawTheme), nux::ButtonVisualState::VISUAL_STATE_NORMAL)));
 
81
  }
 
82
 
 
83
  SetMinimumHeight(kMinButtonHeight);
 
84
  SetMinimumWidth(kMinButtonWidth);
 
85
}
 
86
 
 
87
void FilterBasicButton::RedrawTheme(nux::Geometry const& geom, cairo_t* cr, nux::ButtonVisualState faked_state)
 
88
{
 
89
  Style::Instance().Button(cr, faked_state, label_);
 
90
}
 
91
 
 
92
long FilterBasicButton::ComputeContentSize()
 
93
{
 
94
  long ret = nux::Button::ComputeContentSize();
 
95
  
 
96
  nux::Geometry const& geo = GetGeometry();
 
97
 
 
98
  if (cached_geometry_ != geo)
 
99
  {
 
100
    prelight_->Invalidate(geo);
 
101
    active_->Invalidate(geo);
 
102
    normal_->Invalidate(geo);
 
103
 
 
104
    cached_geometry_ = geo;
 
105
  }
 
106
 
 
107
  return ret;
 
108
}
 
109
 
 
110
void FilterBasicButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
 
111
{
 
112
  nux::Geometry const& geo = GetGeometry();
 
113
 
 
114
  gPainter.PaintBackground(GfxContext, geo);
 
115
  // set up our texture mode
 
116
  nux::TexCoordXForm texxform;
 
117
  texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
 
118
  texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
 
119
 
 
120
  // clear what is behind us
 
121
  unsigned int alpha = 0, src = 0, dest = 0;
 
122
  GfxContext.GetRenderStates().GetBlend(alpha, src, dest);
 
123
  GfxContext.GetRenderStates().SetBlend(true, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 
124
 
 
125
  nux::Color col = nux::color::Black;
 
126
  col.alpha = 0;
 
127
  GfxContext.QRP_Color(geo.x,
 
128
                       geo.y,
 
129
                       geo.width,
 
130
                       geo.height,
 
131
                       col);
 
132
 
 
133
  nux::BaseTexture* texture = normal_->GetTexture();
 
134
  if (Active())
 
135
    texture = active_->GetTexture();
 
136
  else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRELIGHT)
 
137
    texture = prelight_->GetTexture();
 
138
  else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRESSED)
 
139
    texture = active_->GetTexture();
 
140
 
 
141
  GfxContext.QRP_1Tex(geo.x,
 
142
                      geo.y,
 
143
                      geo.width,
 
144
                      geo.height,
 
145
                      texture->GetDeviceTexture(),
 
146
                      texxform,
 
147
                      nux::Color(1.0f, 1.0f, 1.0f, 1.0f));
 
148
 
 
149
  GfxContext.GetRenderStates().SetBlend(alpha, src, dest);
 
150
}
 
151
 
 
152
} // namespace dash
 
153
} // namespace unity