Back to index

unity  6.0.0
FilterBasicButton.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License version 3, as
00006  * published by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00012  * License for more details.
00013  *
00014  * You should have received a copy of both the GNU Lesser General Public
00015  * License version 3 along with this program.  If not, see
00016  * <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Gordon Allott <gord.allott@canonical.com>
00019  *
00020  */
00021 
00022 #include "unity-shared/DashStyle.h"
00023 #include "FilterBasicButton.h"
00024 
00025 namespace
00026 {
00027 const int kMinButtonHeight = 30;
00028 const int kMinButtonWidth  = 48;
00029 }
00030 
00031 namespace unity
00032 {
00033 namespace dash
00034 {
00035 
00036 FilterBasicButton::FilterBasicButton(nux::TextureArea* image, NUX_FILE_LINE_DECL)
00037   : nux::ToggleButton(image, NUX_FILE_LINE_PARAM)
00038 {
00039   Init();
00040 }
00041 
00042 FilterBasicButton::FilterBasicButton(std::string const& label, NUX_FILE_LINE_DECL)
00043   : nux::ToggleButton(NUX_FILE_LINE_PARAM)
00044   , label_(label)
00045 {
00046   Init();
00047 }
00048 
00049 FilterBasicButton::FilterBasicButton(std::string const& label, nux::TextureArea* image, NUX_FILE_LINE_DECL)
00050   : nux::ToggleButton(image, NUX_FILE_LINE_PARAM)
00051   , label_(label)
00052 {
00053   Init();
00054 }
00055 
00056 FilterBasicButton::FilterBasicButton(NUX_FILE_LINE_DECL)
00057   : nux::ToggleButton(NUX_FILE_LINE_PARAM)
00058 {
00059   Init();
00060 }
00061 
00062 FilterBasicButton::~FilterBasicButton()
00063 {
00064 }
00065 
00066 void FilterBasicButton::Init()
00067 {
00068 
00069   InitTheme();
00070   SetAcceptKeyNavFocusOnMouseDown(false);
00071   SetAcceptKeyNavFocusOnMouseEnter(true);
00072 
00073   key_nav_focus_change.connect([&] (nux::Area*, bool, nux::KeyNavDirection)
00074   {
00075     QueueDraw();
00076   });
00077 
00078   key_nav_focus_activate.connect([&](nux::Area*)
00079   {
00080     if (GetInputEventSensitivity())
00081       Active() ? Deactivate() : Activate();
00082   });
00083 }
00084 
00085 void FilterBasicButton::InitTheme()
00086 {
00087   if (!active_)
00088   {
00089     nux::Geometry const& geo = GetGeometry();
00090 
00091     prelight_.reset(new nux::CairoWrapper(geo, sigc::bind(sigc::mem_fun(this, &FilterBasicButton::RedrawTheme), nux::ButtonVisualState::VISUAL_STATE_PRELIGHT)));
00092     active_.reset(new nux::CairoWrapper(geo, sigc::bind(sigc::mem_fun(this, &FilterBasicButton::RedrawTheme), nux::ButtonVisualState::VISUAL_STATE_PRESSED)));
00093     normal_.reset(new nux::CairoWrapper(geo, sigc::bind(sigc::mem_fun(this, &FilterBasicButton::RedrawTheme), nux::ButtonVisualState::VISUAL_STATE_NORMAL)));
00094     focus_.reset(new nux::CairoWrapper(geo, sigc::mem_fun(this, &FilterBasicButton::RedrawFocusOverlay)));
00095   }
00096 
00097   SetMinimumHeight(kMinButtonHeight);
00098   SetMinimumWidth(kMinButtonWidth);
00099 }
00100 
00101 void FilterBasicButton::RedrawTheme(nux::Geometry const& geom, cairo_t* cr, nux::ButtonVisualState faked_state)
00102 {
00103   Style::Instance().Button(cr, faked_state, label_, -1, Alignment::CENTER, true);
00104 }
00105 
00106 void FilterBasicButton::RedrawFocusOverlay(nux::Geometry const& geom, cairo_t* cr)
00107 {
00108   Style::Instance().ButtonFocusOverlay(cr);
00109 }
00110 
00111 long FilterBasicButton::ComputeContentSize()
00112 {
00113   long ret = nux::Button::ComputeContentSize();
00114 
00115   nux::Geometry const& geo = GetGeometry();
00116 
00117   if (cached_geometry_ != geo)
00118   {
00119     prelight_->Invalidate(geo);
00120     active_->Invalidate(geo);
00121     normal_->Invalidate(geo);
00122     focus_->Invalidate(geo);
00123 
00124     cached_geometry_ = geo;
00125   }
00126 
00127   return ret;
00128 }
00129 
00130 void FilterBasicButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
00131 {
00132   nux::Geometry const& geo = GetGeometry();
00133 
00134   gPainter.PaintBackground(GfxContext, geo);
00135   // set up our texture mode
00136   nux::TexCoordXForm texxform;
00137   texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
00138   texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
00139 
00140   // clear what is behind us
00141   unsigned int alpha = 0, src = 0, dest = 0;
00142   GfxContext.GetRenderStates().GetBlend(alpha, src, dest);
00143   GfxContext.GetRenderStates().SetBlend(true, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
00144 
00145   nux::Color col = nux::color::Black;
00146   col.alpha = 0;
00147   GfxContext.QRP_Color(geo.x,
00148                        geo.y,
00149                        geo.width,
00150                        geo.height,
00151                        col);
00152 
00153   nux::BaseTexture* texture = normal_->GetTexture();
00154   if (Active())
00155     texture = active_->GetTexture();
00156   else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRELIGHT)
00157     texture = prelight_->GetTexture();
00158   else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRESSED)
00159     texture = active_->GetTexture();
00160 
00161   GfxContext.QRP_1Tex(geo.x,
00162                       geo.y,
00163                       geo.width,
00164                       geo.height,
00165                       texture->GetDeviceTexture(),
00166                       texxform,
00167                       nux::Color(1.0f, 1.0f, 1.0f, 1.0f));
00168 
00169   if (HasKeyboardFocus())
00170   {
00171     GfxContext.QRP_1Tex(geo.x,
00172                         geo.y,
00173                         geo.width,
00174                         geo.height,
00175                         focus_->GetTexture()->GetDeviceTexture(),
00176                         texxform,
00177                         nux::Color(1.0f, 1.0f, 1.0f, 1.0f));
00178   }
00179 
00180   GfxContext.GetRenderStates().SetBlend(alpha, src, dest);
00181 }
00182 
00183 } // namespace dash
00184 } // namespace unity