Back to index

unity  6.0.0
QuicklistMenuItemLabel.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  * Authored by: Jay Taoko <jay.taoko@canonical.com>
00019  */
00020 
00021 #include <gdk/gdk.h>
00022 #include <gtk/gtk.h>
00023 
00024 #include <Nux/Nux.h>
00025 
00026 #include "unity-shared/CairoTexture.h"
00027 #include "QuicklistMenuItemLabel.h"
00028 
00029 namespace unity
00030 {
00031 
00032 QuicklistMenuItemLabel::QuicklistMenuItemLabel(DbusmenuMenuitem* item,
00033                                                NUX_FILE_LINE_DECL) :
00034   QuicklistMenuItem(item,
00035                     NUX_FILE_LINE_PARAM)
00036 {
00037   _item_type  = MENUITEM_TYPE_LABEL;
00038   _name = "QuicklistMenuItemLabel";
00039   InitializeText();
00040 }
00041 
00042 QuicklistMenuItemLabel::QuicklistMenuItemLabel(DbusmenuMenuitem* item,
00043                                                bool              debug,
00044                                                NUX_FILE_LINE_DECL) :
00045   QuicklistMenuItem(item,
00046                     debug,
00047                     NUX_FILE_LINE_PARAM)
00048 {
00049   _item_type  = MENUITEM_TYPE_LABEL;
00050   _name = "QuicklistMenuItemLabel";
00051   InitializeText();
00052 }
00053 
00054 QuicklistMenuItemLabel::~QuicklistMenuItemLabel()
00055 {}
00056 
00057 const gchar*
00058 QuicklistMenuItemLabel::GetDefaultText()
00059 {
00060   return "Label";
00061 }
00062 
00063 void
00064 QuicklistMenuItemLabel::PreLayoutManagement()
00065 {
00066   _pre_layout_width = GetBaseWidth();
00067   _pre_layout_height = GetBaseHeight();
00068 
00069   if (_normalTexture[0] == 0)
00070   {
00071     UpdateTexture();
00072   }
00073 
00074   QuicklistMenuItem::PreLayoutManagement();
00075 }
00076 
00077 long
00078 QuicklistMenuItemLabel::PostLayoutManagement(long layoutResult)
00079 {
00080   int w = GetBaseWidth();
00081   int h = GetBaseHeight();
00082 
00083   long result = 0;
00084 
00085   if (_pre_layout_width < w)
00086     result |= nux::eLargerWidth;
00087   else if (_pre_layout_width > w)
00088     result |= nux::eSmallerWidth;
00089   else
00090     result |= nux::eCompliantWidth;
00091 
00092   if (_pre_layout_height < h)
00093     result |= nux::eLargerHeight;
00094   else if (_pre_layout_height > h)
00095     result |= nux::eSmallerHeight;
00096   else
00097     result |= nux::eCompliantHeight;
00098 
00099   return result;
00100 }
00101 
00102 void
00103 QuicklistMenuItemLabel::Draw(nux::GraphicsEngine& gfxContext,
00104                              bool                 forceDraw)
00105 {
00106   // Check if the texture have been computed. If they haven't, exit the function.
00107   if (_normalTexture[0] == NULL)
00108     return;
00109 
00110   nux::ObjectPtr<nux::IOpenGLBaseTexture> texture;
00111 
00112   nux::Geometry base = GetGeometry();
00113 
00114   gfxContext.PushClippingRectangle(base);
00115 
00116   nux::TexCoordXForm texxform;
00117   texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
00118   texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
00119 
00120   gfxContext.GetRenderStates().SetBlend(true);
00121   gfxContext.GetRenderStates().SetPremultipliedBlend(nux::SRC_OVER);
00122 
00123   if (GetEnabled())
00124   {
00125     if (_prelight)
00126     {
00127       texture = _prelightTexture[0]->GetDeviceTexture();
00128     }
00129     else
00130     {
00131       texture = _normalTexture[0]->GetDeviceTexture();
00132     }
00133     _color = nux::color::White;
00134   }
00135   else
00136   {
00137     texture = _normalTexture[0]->GetDeviceTexture();
00138     _color = nux::color::White * 0.35;
00139   }
00140 
00141   gfxContext.QRP_1Tex(base.x,
00142                       base.y,
00143                       base.width,
00144                       base.height,
00145                       texture,
00146                       texxform,
00147                       _color);
00148 
00149   gfxContext.GetRenderStates().SetBlend(false);
00150 
00151   gfxContext.PopClippingRectangle();
00152 }
00153 
00154 void
00155 QuicklistMenuItemLabel::DrawContent(nux::GraphicsEngine& gfxContext,
00156                                     bool                 forceDraw)
00157 {
00158 }
00159 
00160 void
00161 QuicklistMenuItemLabel::PostDraw(nux::GraphicsEngine& gfxContext,
00162                                  bool                 forceDraw)
00163 {
00164 }
00165 
00166 void
00167 QuicklistMenuItemLabel::UpdateTexture()
00168 {
00169   int        width       = GetBaseWidth();
00170   int        height      = GetBaseHeight();
00171 
00172   _cairoGraphics = new nux::CairoGraphics(CAIRO_FORMAT_ARGB32, width, height);
00173   cairo_t* cr = _cairoGraphics->GetContext();
00174 
00175   // draw normal, unchecked version
00176   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00177   cairo_paint(cr);
00178 
00179   DrawText(_cairoGraphics, width, height, nux::color::White);
00180 
00181   if (_normalTexture[0])
00182     _normalTexture[0]->UnReference();
00183 
00184   _normalTexture[0] = texture_from_cairo_graphics(*_cairoGraphics);
00185 
00186   // draw active/prelight, unchecked version
00187   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00188   cairo_paint(cr);
00189 
00190   DrawPrelight(_cairoGraphics, width, height, nux::color::White);
00191   DrawText(_cairoGraphics, width, height, nux::color::White * 0.0f);
00192 
00193   if (_prelightTexture[0])
00194     _prelightTexture[0]->UnReference();
00195 
00196   _prelightTexture[0] = texture_from_cairo_graphics(*_cairoGraphics);
00197 
00198   // finally clean up
00199   cairo_destroy(cr);
00200   delete _cairoGraphics;
00201 }
00202 
00203 int QuicklistMenuItemLabel::CairoSurfaceWidth()
00204 {
00205   if (_normalTexture[0])
00206     return _normalTexture[0]->GetWidth();
00207 
00208   return 0;
00209 }
00210 
00211 } // NAMESPACE