Back to index

nux  3.0.0
ActionItem.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 #include "NuxGraphics/GLTextureResourceManager.h"
00025 #include "ActionItem.h"
00026 
00027 namespace nux
00028 {
00029 
00030   NUX_IMPLEMENT_OBJECT_TYPE(ActionItem);
00031 
00032   ActionItem::ActionItem(const char *label, int UserValue, NUX_FILE_LINE_DECL)
00033     :   Object(true, NUX_FILE_LINE_PARAM)
00034     ,   m_UserValue(UserValue)
00035     ,   m_Label("")
00036     ,   m_IsActivated(true)
00037     ,   m_Menu(0)
00038     ,   m_Enable(true)
00039   {
00040     m_Icon = 0;
00041     SetLabel(label);
00042   }
00043 
00044   ActionItem::~ActionItem()
00045   {
00046     if (m_Icon)
00047       m_Icon->UnReference();
00048   }
00049 
00050   void ActionItem::DrawAsMenuItem(GraphicsEngine &graphics_engine, InputArea &area, bool is_highlighted, bool draw_icone)
00051   {
00052     Geometry geo = area.GetGeometry();
00053     Geometry icon_geo(0, 0, 20, 20);
00054     Geometry text_geo = geo;
00055 
00056     text_geo.OffsetPosition(24, 2);
00057     text_geo.OffsetSize(2 * 24, 2 * 2);
00058 
00059     icon_geo.SetX(geo.x + 2);
00060     icon_geo.SetY(geo.y + 2);
00061 
00062     const char *label = GetLabel();
00063 
00064     if (is_highlighted)
00065     {
00066       GetPainter().Paint2DQuadColor(graphics_engine, geo, Color(COLOR_FOREGROUND_SECONDARY));
00067     }
00068 
00069     if (m_Icon)
00070       GetPainter().Draw2DTextureAligned(graphics_engine, m_Icon, icon_geo, TextureAlignmentStyle(eTACenter, eTACenter));
00071 
00072     GetPainter().PaintTextLineStatic(graphics_engine, GetSysFont(), text_geo, std::string(label), Color(0xFF000000), eAlignTextLeft);
00073   }
00074 
00075   void ActionItem::DrawAsToolButton(GraphicsEngine &graphics_engine, InputArea &area)
00076   {
00077     Geometry base = area.GetGeometry();
00078 
00079     if (area.IsMouseOwner())
00080     {
00081       if (area.IsMouseInside())
00082       {
00083         GetPainter().PaintShape(graphics_engine, base, Color(COLOR_BACKGROUND_SECONDARY),  eSHAPE_CORNER_ROUND2);
00084         GetPainter().PaintShape(graphics_engine, base, Color(COLOR_BLACK),  eSTROKE_CORNER_ROUND2);
00085       }
00086       else
00087       {
00088         GetPainter().PaintShape(graphics_engine, base, Color(COLOR_FOREGROUND_PRIMARY),  eSHAPE_CORNER_ROUND2);
00089         GetPainter().PaintShape(graphics_engine, base, Color(COLOR_BLACK),  eSTROKE_CORNER_ROUND2);
00090       }
00091     }
00092     else
00093     {
00094       if (area.IsMouseInside() /*TODO: && (!area.MouseFocusOnOtherArea())*/)
00095       {
00096         GetPainter().PaintShape(graphics_engine, base, Color(COLOR_FOREGROUND_PRIMARY),  eSHAPE_CORNER_ROUND2);
00097         GetPainter().PaintShape(graphics_engine, base, Color(COLOR_BLACK),  eSTROKE_CORNER_ROUND2);
00098       }
00099       else
00100       {
00101         GetPainter().PaintShape(graphics_engine, base, Color(COLOR_BACKGROUND_SECONDARY),  eSHAPE_CORNER_ROUND2);
00102         GetPainter().PaintShape(graphics_engine, base, Color(COLOR_BLACK),  eSTROKE_CORNER_ROUND2);
00103       }
00104     }
00105 
00106     GetPainter().Draw2DTextureAligned(graphics_engine, m_Icon, base, TextureAlignmentStyle(eTACenter, eTACenter));
00107   }
00108 
00109   void ActionItem::Activate(bool b)
00110   {
00111     m_IsActivated = b;
00112   }
00113 
00114   void ActionItem::Trigger() const
00115   {
00116     sigAction.emit();
00117   }
00118 
00119   void ActionItem::Enable(bool b)
00120   {
00121     m_Enable = b;
00122   }
00123 
00124   bool ActionItem::isEnabled() const
00125   {
00126     return m_Enable;
00127   }
00128 
00129   void ActionItem::SetLabel(const char *label)
00130   {
00131     m_Label = label;
00132   }
00133 
00134   const char *ActionItem::GetLabel() const
00135   {
00136     return m_Label.GetTCharPtr();
00137   }
00138 
00139   void ActionItem::SetIcon(const BaseTexture* icon)
00140   {
00141     if (m_Icon)
00142       m_Icon->UnReference();
00143     m_Icon = icon->Clone();
00144   }
00145 
00146   // NUXTODO: should return the bitmap data instead or a const pointer?.
00147   const BaseTexture* ActionItem::GetIcon()
00148   {
00149     return m_Icon;
00150   }
00151 
00152 //void ActionItem::SetMenu(Menu* menu)
00153 //{
00154 //    m_Menu = menu;
00155 //}
00156 //Menu* ActionItem::GetMenu() const
00157 //{
00158 //    return m_Menu;
00159 //}
00160 }