Back to index

unity  6.0.0
SearchBarSpinner.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00018  */
00019 
00020 #include "SearchBarSpinner.h"
00021 
00022 #include <Nux/VLayout.h>
00023 #include <UnityCore/Variant.h>
00024 
00025 #include "unity-shared/DashStyle.h"
00026 
00027 namespace unity
00028 {
00029 
00030 NUX_IMPLEMENT_OBJECT_TYPE(SearchBarSpinner);
00031 
00032 SearchBarSpinner::SearchBarSpinner()
00033   : nux::View(NUX_TRACKER_LOCATION),
00034     _state(STATE_READY),
00035     _rotation(0.0f)
00036 {
00037   dash::Style& style = dash::Style::Instance();
00038 
00039   _magnify = style.GetSearchMagnifyIcon();
00040   _circle = style.GetSearchCircleIcon();
00041   _close = style.GetSearchCloseIcon();
00042   _spin = style.GetSearchSpinIcon();
00043 
00044   _2d_rotate.Identity();
00045   _2d_rotate.Rotate_z(0.0);
00046 }
00047 
00048 void
00049 SearchBarSpinner::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
00050 {
00051   nux::Geometry const& geo = GetGeometry();
00052   nux::TexCoordXForm texxform;
00053 
00054   GfxContext.PushClippingRectangle(geo);
00055 
00056   nux::GetPainter().PaintBackground(GfxContext, geo);
00057 
00058   texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
00059   texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
00060   texxform.min_filter = nux::TEXFILTER_LINEAR;
00061   texxform.mag_filter = nux::TEXFILTER_LINEAR;
00062 
00063   if (_state == STATE_READY)
00064   {
00065     GfxContext.QRP_1Tex(geo.x + ((geo.width - _magnify->GetWidth()) / 2),
00066                         geo.y + ((geo.height - _magnify->GetHeight()) / 2),
00067                         _magnify->GetWidth(),
00068                         _magnify->GetHeight(),
00069                         _magnify->GetDeviceTexture(),
00070                         texxform,
00071                         nux::color::White);
00072   }
00073   else if (_state == STATE_SEARCHING)
00074   {
00075     nux::Geometry spin_geo(geo.x + ((geo.width - _spin->GetWidth()) / 2),
00076                            geo.y + ((geo.height - _spin->GetHeight()) / 2),
00077                            _spin->GetWidth(),
00078                            _spin->GetHeight());
00079     // Geometry (== Rect) uses integers which were rounded above,
00080     // hence an extra 0.5 offset for odd sizes is needed
00081     // because pure floating point is not being used.
00082     int spin_offset_w = !(geo.width % 2) ? 0 : 1;
00083     int spin_offset_h = !(geo.height % 2) ? 0 : 1;
00084 
00085     GfxContext.PushModelViewMatrix(nux::Matrix4::TRANSLATE(-spin_geo.x - (spin_geo.width + spin_offset_w) / 2.0f,
00086                                                            -spin_geo.y - (spin_geo.height + spin_offset_h) / 2.0f, 0));
00087     GfxContext.PushModelViewMatrix(_2d_rotate);
00088     GfxContext.PushModelViewMatrix(nux::Matrix4::TRANSLATE(spin_geo.x + (spin_geo.width + spin_offset_w) / 2.0f,
00089                                                            spin_geo.y + (spin_geo.height + spin_offset_h) / 2.0f, 0));
00090 
00091     GfxContext.QRP_1Tex(spin_geo.x,
00092                         spin_geo.y,
00093                         spin_geo.width,
00094                         spin_geo.height,
00095                         _spin->GetDeviceTexture(),
00096                         texxform,
00097                         nux::color::White);
00098 
00099     GfxContext.PopModelViewMatrix();
00100     GfxContext.PopModelViewMatrix();
00101     GfxContext.PopModelViewMatrix();
00102   }
00103   else
00104   {
00105     GfxContext.QRP_1Tex(geo.x + ((geo.width - _circle->GetWidth()) / 2),
00106                         geo.y + ((geo.height - _circle->GetHeight()) / 2),
00107                         _circle->GetWidth(),
00108                         _circle->GetHeight(),
00109                         _circle->GetDeviceTexture(),
00110                         texxform,
00111                         nux::color::White);
00112 
00113     GfxContext.QRP_1Tex(geo.x + ((geo.width - _close->GetWidth()) / 2),
00114                         geo.y + ((geo.height - _close->GetHeight()) / 2),
00115                         _close->GetWidth(),
00116                         _close->GetHeight(),
00117                         _close->GetDeviceTexture(),
00118                         texxform,
00119                         nux::color::White);
00120   }
00121   GfxContext.PopClippingRectangle();
00122 
00123   if (_state == STATE_SEARCHING && !_frame_timeout)
00124   {
00125     _frame_timeout.reset(new glib::Timeout(22, sigc::mem_fun(this, &SearchBarSpinner::OnFrameTimeout)));
00126   }
00127 }
00128 
00129 void
00130 SearchBarSpinner::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw)
00131 {
00132 }
00133 
00134 bool
00135 SearchBarSpinner::OnFrameTimeout()
00136 {
00137   _rotation += 0.1f;
00138 
00139   if (_rotation >= 360.0f)
00140     _rotation = 0.0f;
00141 
00142   _2d_rotate.Rotate_z(_rotation);
00143   QueueDraw();
00144 
00145   _frame_timeout.reset();
00146   return false;
00147 }
00148 
00149 void
00150 SearchBarSpinner::SetState(SpinnerState state)
00151 {
00152   if (_state == state)
00153     return;
00154 
00155   _state = state;
00156   _spinner_timeout.reset();
00157   _2d_rotate.Rotate_z(0.0f);
00158   _rotation = 0.0f;
00159 
00160   if (_state == STATE_SEARCHING)
00161   {
00162     _spinner_timeout.reset(new glib::TimeoutSeconds(5, [&] {
00163       _state = STATE_READY;
00164       return false;
00165     }));
00166   }
00167 
00168   QueueDraw();
00169 }
00170 
00171 std::string
00172 SearchBarSpinner::GetName() const
00173 {
00174   return "SearchBarSpinner";
00175 }
00176 
00177 void SearchBarSpinner::AddProperties(GVariantBuilder* builder)
00178 {
00179   nux::Geometry geo = GetGeometry();
00180 
00181   variant::BuilderWrapper(builder)
00182     .add("x", geo.x)
00183     .add("y", geo.y)
00184     .add("width", geo.width)
00185     .add("height", geo.height);
00186 }
00187 
00188 //
00189 // Key navigation
00190 //
00191 bool
00192 SearchBarSpinner::AcceptKeyNavFocus()
00193 {
00194   return false;
00195 }
00196 
00197 }