Back to index

unity  6.0.0
QuicklistMenuItemRadio.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2010 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 <gdk/gdk.h>
00021 #include <gtk/gtk.h>
00022 
00023 #include <Nux/Nux.h>
00024 
00025 #include "unity-shared/CairoTexture.h"
00026 #include "QuicklistMenuItemRadio.h"
00027 
00028 namespace unity
00029 {
00030 
00031 static double
00032 _align(double val)
00033 {
00034   double fract = val - (int) val;
00035 
00036   if (fract != 0.5f)
00037     return (double)((int) val + 0.5f);
00038   else
00039     return val;
00040 }
00041 
00042 QuicklistMenuItemRadio::QuicklistMenuItemRadio(DbusmenuMenuitem* item,
00043                                                NUX_FILE_LINE_DECL) :
00044   QuicklistMenuItem(item,
00045                     NUX_FILE_LINE_PARAM)
00046 {
00047   _item_type  = MENUITEM_TYPE_RADIO;
00048   _name = "QuicklistMenuItemRadio";
00049   InitializeText();
00050 }
00051 
00052 QuicklistMenuItemRadio::QuicklistMenuItemRadio(DbusmenuMenuitem* item,
00053                                                bool              debug,
00054                                                NUX_FILE_LINE_DECL) :
00055   QuicklistMenuItem(item,
00056                     debug,
00057                     NUX_FILE_LINE_PARAM)
00058 {
00059   _item_type  = MENUITEM_TYPE_RADIO;
00060   _name = "QuicklistMenuItemRadio";
00061   InitializeText();
00062 }
00063 
00064 QuicklistMenuItemRadio::~QuicklistMenuItemRadio()
00065 {}
00066 
00067 const gchar*
00068 QuicklistMenuItemRadio::GetDefaultText()
00069 {
00070   return "Radio Button";
00071 }
00072 
00073 void
00074 QuicklistMenuItemRadio::PreLayoutManagement()
00075 {
00076   _pre_layout_width = GetBaseWidth();
00077   _pre_layout_height = GetBaseHeight();
00078 
00079   if (_normalTexture[0] == 0)
00080   {
00081     UpdateTexture();
00082   }
00083 
00084   QuicklistMenuItem::PreLayoutManagement();
00085 }
00086 
00087 long
00088 QuicklistMenuItemRadio::PostLayoutManagement(long layoutResult)
00089 {
00090   int w = GetBaseWidth();
00091   int h = GetBaseHeight();
00092 
00093   long result = 0;
00094 
00095   if (_pre_layout_width < w)
00096     result |= nux::eLargerWidth;
00097   else if (_pre_layout_width > w)
00098     result |= nux::eSmallerWidth;
00099   else
00100     result |= nux::eCompliantWidth;
00101 
00102   if (_pre_layout_height < h)
00103     result |= nux::eLargerHeight;
00104   else if (_pre_layout_height > h)
00105     result |= nux::eSmallerHeight;
00106   else
00107     result |= nux::eCompliantHeight;
00108 
00109   return result;
00110 }
00111 
00112 void
00113 QuicklistMenuItemRadio::Draw(nux::GraphicsEngine& gfxContext, bool forceDraw)
00114 {
00115   nux::ObjectPtr<nux::IOpenGLBaseTexture> texture;
00116 
00117   // Check if the texture have been computed. If they haven't, exit the function.
00118   if (!_normalTexture[0] || !_prelightTexture[0])
00119     return;
00120 
00121   nux::Geometry base = GetGeometry();
00122 
00123   gfxContext.PushClippingRectangle(base);
00124 
00125   nux::TexCoordXForm texxform;
00126   texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
00127   texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
00128 
00129   gfxContext.GetRenderStates().SetBlend(true);
00130   gfxContext.GetRenderStates().SetPremultipliedBlend(nux::SRC_OVER);
00131 
00132   unsigned int texture_idx = GetActive() ? 1 : 0;
00133 
00134   if (!_prelight || !GetEnabled())
00135   {
00136     texture = _normalTexture[texture_idx]->GetDeviceTexture();
00137   }
00138   else
00139   {
00140     texture = _prelightTexture[texture_idx]->GetDeviceTexture();
00141   }
00142 
00143   _color = GetEnabled() ? nux::color::White : nux::color::White * 0.35;
00144 
00145   gfxContext.QRP_1Tex(base.x,
00146                       base.y,
00147                       base.width,
00148                       base.height,
00149                       texture,
00150                       texxform,
00151                       _color);
00152 
00153   gfxContext.GetRenderStates().SetBlend(false);
00154 
00155   gfxContext.PopClippingRectangle();
00156 }
00157 
00158 void
00159 QuicklistMenuItemRadio::DrawContent(nux::GraphicsEngine& gfxContext,
00160                                     bool                 forceDraw)
00161 {
00162 }
00163 
00164 void
00165 QuicklistMenuItemRadio::PostDraw(nux::GraphicsEngine& gfxContext,
00166                                  bool                 forceDraw)
00167 {
00168 }
00169 
00170 void
00171 QuicklistMenuItemRadio::UpdateTexture()
00172 {
00173   int        width       = GetBaseWidth();
00174   int        height      = GetBaseHeight();
00175 
00176   _cairoGraphics = new nux::CairoGraphics(CAIRO_FORMAT_ARGB32, width, height);
00177   cairo_t* cr = _cairoGraphics->GetContext();
00178 
00179   // draw normal, disabled version
00180   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00181   cairo_paint(cr);
00182 
00183   DrawText(_cairoGraphics, width, height, nux::color::White);
00184 
00185   if (_normalTexture[0])
00186     _normalTexture[0]->UnReference();
00187 
00188   _normalTexture[0] = texture_from_cairo_graphics(*_cairoGraphics);
00189 
00190   // draw normal, enabled version
00191   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00192   cairo_paint(cr);
00193 
00194   cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
00195   cairo_scale(cr, 1.0f, 1.0f);
00196   cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 1.0f);
00197   cairo_set_line_width(cr, 1.0f);
00198 
00199   double x      = _align((ITEM_INDENT_ABS + ITEM_MARGIN) / 2.0f);
00200   double y      = _align((double) height / 2.0f);
00201   double radius = 3.5f;
00202 
00203   cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 1.0f);
00204   cairo_arc(cr, x, y, radius, 0.0f * (G_PI / 180.0f), 360.0f * (G_PI / 180.0f));
00205   cairo_fill(cr);
00206 
00207   DrawText(_cairoGraphics, width, height, nux::color::White);
00208 
00209   if (_normalTexture[1])
00210     _normalTexture[1]->UnReference();
00211 
00212   _normalTexture[1] = texture_from_cairo_graphics(*_cairoGraphics);
00213 
00214   // draw active/prelight, unchecked version
00215   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00216   cairo_paint(cr);
00217 
00218   DrawPrelight(_cairoGraphics, width, height, nux::color::White);
00219   DrawText(_cairoGraphics, width, height, nux::color::White * 0.0f);
00220 
00221   if (_prelightTexture[0])
00222     _prelightTexture[0]->UnReference();
00223 
00224   _prelightTexture[0] = texture_from_cairo_graphics(*_cairoGraphics);
00225 
00226   // draw active/prelight, unchecked version
00227   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00228   cairo_paint(cr);
00229 
00230   DrawPrelight(_cairoGraphics, width, height, nux::color::White);
00231 
00232   cairo_set_source_rgba(cr, 0.0f, 0.0f, 0.0f, 0.0f);
00233 
00234   cairo_arc(cr, x, y, radius, 0.0f * (G_PI / 180.0f), 360.0f * (G_PI / 180.0f));
00235   cairo_fill(cr);
00236 
00237   DrawText(_cairoGraphics, width, height, nux::color::White * 0.0f);
00238 
00239   if (_prelightTexture[1])
00240     _prelightTexture[1]->UnReference();
00241 
00242   _prelightTexture[1] = texture_from_cairo_graphics(*_cairoGraphics);
00243 
00244   // finally clean up
00245   cairo_destroy(cr);
00246   delete _cairoGraphics;
00247 }
00248 
00249 int QuicklistMenuItemRadio::CairoSurfaceWidth()
00250 {
00251   if (_normalTexture[0])
00252     return _normalTexture[0]->GetWidth();
00253 
00254   return 0;
00255 }
00256 
00257 } // NAMESPACE