Back to index

unity  6.0.0
QuicklistMenuItemCheckmark.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 "QuicklistMenuItemCheckmark.h"
00028 
00029 namespace unity
00030 {
00031 
00032 static double
00033 _align(double val)
00034 {
00035   double fract = val - (int) val;
00036 
00037   if (fract != 0.5f)
00038     return (double)((int) val + 0.5f);
00039   else
00040     return val;
00041 }
00042 
00043 QuicklistMenuItemCheckmark::QuicklistMenuItemCheckmark(DbusmenuMenuitem* item,
00044                                                        NUX_FILE_LINE_DECL) :
00045   QuicklistMenuItem(item,
00046                     NUX_FILE_LINE_PARAM)
00047 {
00048   _item_type  = MENUITEM_TYPE_CHECK;
00049   _name = "QuicklistMenuItemCheckmark";
00050   InitializeText();
00051 }
00052 
00053 QuicklistMenuItemCheckmark::QuicklistMenuItemCheckmark(DbusmenuMenuitem* item,
00054                                                        bool              debug,
00055                                                        NUX_FILE_LINE_DECL) :
00056   QuicklistMenuItem(item,
00057                     debug,
00058                     NUX_FILE_LINE_PARAM)
00059 {
00060   _item_type  = MENUITEM_TYPE_CHECK;
00061   _name = "QuicklistMenuItemCheckmark";
00062   InitializeText();
00063 }
00064 
00065 QuicklistMenuItemCheckmark::~QuicklistMenuItemCheckmark()
00066 {}
00067 
00068 const gchar*
00069 QuicklistMenuItemCheckmark::GetDefaultText()
00070 {
00071   return "Check Mark";
00072 }
00073 
00074 void
00075 QuicklistMenuItemCheckmark::PreLayoutManagement()
00076 {
00077   _pre_layout_width = GetBaseWidth();
00078   _pre_layout_height = GetBaseHeight();
00079 
00080   if (_normalTexture[0] == 0)
00081   {
00082     UpdateTexture();
00083   }
00084 
00085   QuicklistMenuItem::PreLayoutManagement();
00086 }
00087 
00088 long
00089 QuicklistMenuItemCheckmark::PostLayoutManagement(long layoutResult)
00090 {
00091   int w = GetBaseWidth();
00092   int h = GetBaseHeight();
00093 
00094   long result = 0;
00095 
00096   if (_pre_layout_width < w)
00097     result |= nux::eLargerWidth;
00098   else if (_pre_layout_width > w)
00099     result |= nux::eSmallerWidth;
00100   else
00101     result |= nux::eCompliantWidth;
00102 
00103   if (_pre_layout_height < h)
00104     result |= nux::eLargerHeight;
00105   else if (_pre_layout_height > h)
00106     result |= nux::eSmallerHeight;
00107   else
00108     result |= nux::eCompliantHeight;
00109 
00110   return result;
00111 }
00112 
00113 void
00114 QuicklistMenuItemCheckmark::Draw(nux::GraphicsEngine& gfxContext, bool forceDraw)
00115 {
00116   nux::ObjectPtr<nux::IOpenGLBaseTexture> texture;
00117 
00118   // Check if the texture have been computed. If they haven't, exit the function.
00119   if (!_normalTexture[0] || !_prelightTexture[0])
00120     return;
00121 
00122   nux::Geometry base = GetGeometry();
00123 
00124   gfxContext.PushClippingRectangle(base);
00125 
00126   nux::TexCoordXForm texxform;
00127   texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
00128   texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
00129 
00130   gfxContext.GetRenderStates().SetBlend(true);
00131   gfxContext.GetRenderStates().SetPremultipliedBlend(nux::SRC_OVER);
00132 
00133   unsigned int texture_idx = GetActive() ? 1 : 0;
00134 
00135   if (!_prelight || !GetEnabled())
00136   {
00137     texture = _normalTexture[texture_idx]->GetDeviceTexture();
00138   }
00139   else
00140   {
00141     texture = _prelightTexture[texture_idx]->GetDeviceTexture();
00142   }
00143 
00144   _color = GetEnabled() ? nux::color::White : nux::color::White * 0.35;
00145 
00146   gfxContext.QRP_1Tex(base.x,
00147                       base.y,
00148                       base.width,
00149                       base.height,
00150                       texture,
00151                       texxform,
00152                       _color);
00153 
00154   gfxContext.GetRenderStates().SetBlend(false);
00155 
00156   gfxContext.PopClippingRectangle();
00157 }
00158 
00159 void QuicklistMenuItemCheckmark::DrawContent(nux::GraphicsEngine& gfxContext,
00160                                              bool                 forceDraw)
00161 {
00162 }
00163 
00164 void QuicklistMenuItemCheckmark::PostDraw(nux::GraphicsEngine& gfxContext,
00165                                           bool                 forceDraw)
00166 {
00167 }
00168 
00169 void
00170 QuicklistMenuItemCheckmark::UpdateTexture()
00171 {
00172   int        width       = GetBaseWidth();
00173   int        height      = GetBaseHeight();
00174 
00175   _cairoGraphics = new nux::CairoGraphics(CAIRO_FORMAT_ARGB32, width, height);
00176   cairo_t* cr = _cairoGraphics->GetContext();
00177 
00178   // draw normal, unchecked version
00179   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00180   cairo_paint(cr);
00181 
00182   cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
00183   cairo_scale(cr, 1.0f, 1.0f);
00184   cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 1.0f);
00185   cairo_set_line_width(cr, 1.0f);
00186 
00187   DrawText(_cairoGraphics, width, height, nux::color::White);
00188 
00189   if (_normalTexture[0])
00190     _normalTexture[0]->UnReference();
00191   _normalTexture[0] = texture_from_cairo_graphics(*_cairoGraphics);
00192 
00193   // draw normal, checked version
00194   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00195   cairo_paint(cr);
00196 
00197   cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
00198   cairo_scale(cr, 1.0f, 1.0f);
00199   cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 1.0f);
00200   cairo_set_line_width(cr, 1.0f);
00201 
00202   cairo_save(cr);
00203   cairo_translate(cr,
00204                   _align((ITEM_INDENT_ABS - 16.0f + ITEM_MARGIN) / 2.0f),
00205                   _align(((double) height - 16.0f) / 2.0f));
00206 
00207   cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 1.0f);
00208 
00209   cairo_translate(cr, 3.0f, 1.0f);
00210   cairo_move_to(cr, 0.0f, 6.0f);
00211   cairo_line_to(cr, 0.0f, 8.0f);
00212   cairo_line_to(cr, 4.0f, 12.0f);
00213   cairo_line_to(cr, 6.0f, 12.0f);
00214   cairo_line_to(cr, 15.0f, 1.0f);
00215   cairo_line_to(cr, 15.0f, 0.0f);
00216   cairo_line_to(cr, 14.0f, 0.0f);
00217   cairo_line_to(cr, 5.0f, 9.0f);
00218   cairo_line_to(cr, 1.0f, 5.0f);
00219   cairo_close_path(cr);
00220   cairo_fill(cr);
00221 
00222   cairo_restore(cr);
00223 
00224   DrawText(_cairoGraphics, width, height, nux::color::White);
00225 
00226   if (_normalTexture[1])
00227     _normalTexture[1]->UnReference();
00228 
00229   _normalTexture[1] = texture_from_cairo_graphics(*_cairoGraphics);
00230 
00231   // draw active/prelight, unchecked version
00232   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00233   cairo_paint(cr);
00234 
00235   DrawPrelight(_cairoGraphics, width, height, nux::color::White);
00236   DrawText(_cairoGraphics, width, height, nux::color::White * 0.0f);
00237 
00238   if (_prelightTexture[0])
00239     _prelightTexture[0]->UnReference();
00240 
00241   _prelightTexture[0] = texture_from_cairo_graphics(*_cairoGraphics);
00242 
00243   // draw active/prelight, checked version
00244   cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
00245   cairo_paint(cr);
00246 
00247   DrawPrelight(_cairoGraphics, width, height, nux::color::White);
00248 
00249   cairo_set_source_rgba(cr, 0.0f, 0.0f, 0.0f, 0.0f);
00250 
00251   cairo_save(cr);
00252   cairo_translate(cr,
00253                   _align((ITEM_INDENT_ABS - 16.0f + ITEM_MARGIN) / 2.0f),
00254                   _align(((double) height - 16.0f) / 2.0f));
00255 
00256   cairo_translate(cr, 3.0f, 1.0f);
00257   cairo_move_to(cr, 0.0f, 6.0f);
00258   cairo_line_to(cr, 0.0f, 8.0f);
00259   cairo_line_to(cr, 4.0f, 12.0f);
00260   cairo_line_to(cr, 6.0f, 12.0f);
00261   cairo_line_to(cr, 15.0f, 1.0f);
00262   cairo_line_to(cr, 15.0f, 0.0f);
00263   cairo_line_to(cr, 14.0f, 0.0f);
00264   cairo_line_to(cr, 5.0f, 9.0f);
00265   cairo_line_to(cr, 1.0f, 5.0f);
00266   cairo_close_path(cr);
00267   cairo_fill(cr);
00268 
00269   cairo_restore(cr);
00270 
00271   DrawText(_cairoGraphics, width, height, nux::color::White * 0.0f);
00272 
00273   if (_prelightTexture[1])
00274     _prelightTexture[1]->UnReference();
00275 
00276   _prelightTexture[1] = texture_from_cairo_graphics(*_cairoGraphics);
00277 
00278   // finally clean up
00279   cairo_destroy(cr);
00280   delete _cairoGraphics;
00281 }
00282 
00283 int QuicklistMenuItemCheckmark::CairoSurfaceWidth()
00284 {
00285   if (_normalTexture[0])
00286     return _normalTexture[0]->GetWidth();
00287 
00288   return 0;
00289 }
00290 
00291 }