Back to index

unity  6.0.0
PlacesVScrollBar.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: Mirco Müller <mirco.mueller@canonical.com>
00018  */
00019 
00020 #include <Nux/Nux.h>
00021 
00022 #include "unity-shared/CairoTexture.h"
00023 #include "PlacesVScrollBar.h"
00024 
00025 using unity::texture_from_cairo_graphics;
00026 
00027 PlacesVScrollBar::PlacesVScrollBar(NUX_FILE_LINE_DECL)
00028   : VScrollBar(NUX_FILE_LINE_PARAM),
00029     _slider_texture(NULL)
00030 {
00031   _scroll_up_button->SetMaximumHeight(15);
00032   _scroll_up_button->SetMinimumHeight(15);
00033 
00034   _scroll_down_button->SetMaximumHeight(15);
00035   _scroll_down_button->SetMinimumHeight(15);
00036 
00037   _slider->SetMinimumWidth(3);
00038   _slider->SetMaximumWidth(3);
00039   SetMinimumWidth(3);
00040   SetMaximumWidth(3);
00041 }
00042 
00043 PlacesVScrollBar::~PlacesVScrollBar()
00044 {
00045   if (_slider_texture)
00046     _slider_texture->UnReference();
00047 }
00048 
00049 void
00050 PlacesVScrollBar::PreLayoutManagement()
00051 {
00052   nux::VScrollBar::PreLayoutManagement();
00053 }
00054 
00055 long
00056 PlacesVScrollBar::PostLayoutManagement(long LayoutResult)
00057 {
00058   long ret = nux::VScrollBar::PostLayoutManagement(LayoutResult);
00059 
00060   UpdateTexture();
00061   return ret;
00062 }
00063 
00064 void
00065 PlacesVScrollBar::Draw(nux::GraphicsEngine& gfxContext, bool force_draw)
00066 {
00067   nux::Color color = nux::color::White;
00068   nux::Geometry const& base  = GetGeometry();
00069   nux::TexCoordXForm texxform;
00070 
00071   gfxContext.PushClippingRectangle(base);
00072 
00073   nux::GetPainter().PaintBackground(gfxContext, base);
00074 
00075   // check if textures have been computed... if they haven't, exit function
00076   if (!_slider_texture)
00077     return;
00078 
00079   texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_SCALE_COORD);
00080 
00081   gfxContext.GetRenderStates().SetBlend(true);
00082   gfxContext.GetRenderStates().SetPremultipliedBlend(nux::SRC_OVER);
00083 
00084   if (content_height_ > container_height_)
00085   {
00086     nux::Geometry const& slider_geo = _slider->GetGeometry();
00087 
00088     gfxContext.QRP_1Tex(slider_geo.x,
00089                         slider_geo.y,
00090                         slider_geo.width,
00091                         slider_geo.height,
00092                         _slider_texture->GetDeviceTexture(),
00093                         texxform,
00094                         color);
00095   }
00096 
00097   gfxContext.GetRenderStates().SetBlend(false);
00098   gfxContext.PopClippingRectangle();
00099   gfxContext.GetRenderStates().SetBlend(true);
00100 }
00101 
00102 void PlacesVScrollBar::UpdateTexture()
00103 {
00104   nux::CairoGraphics* cairoGraphics = NULL;
00105   cairo_t*            cr            = NULL;
00106 
00107   // update texture of slider
00108   int width  = _slider->GetBaseWidth();
00109   int height = _slider->GetBaseHeight();
00110   cairoGraphics = new nux::CairoGraphics(CAIRO_FORMAT_ARGB32, width, height);
00111   cr = cairoGraphics->GetContext();
00112 
00113   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00114   cairo_paint(cr);
00115 
00116   cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
00117   cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 1.0f);
00118   cairoGraphics->DrawRoundedRectangle(cr,
00119                                       1.0f,
00120                                       0.0,
00121                                       0.0,
00122                                       1.5,
00123                                       3.0,
00124                                       (double) height - 3.0);
00125   cairo_fill(cr);
00126 
00127   if (_slider_texture)
00128     _slider_texture->UnReference();
00129   _slider_texture = texture_from_cairo_graphics(*cairoGraphics);
00130 
00131   cairo_destroy(cr);
00132   delete cairoGraphics;
00133 }