nux-1.14.0
Button.cpp
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 #include "Nux.h"
00023 #include "Button.h"
00024 #include "StaticText.h"
00025 #include "HLayout.h"
00026 #include "VLayout.h"
00027 #include "TextureArea.h"
00028 
00029 namespace nux
00030 {
00031   NUX_IMPLEMENT_OBJECT_TYPE(Button);
00032 
00033   Button::Button(TextureArea *image, NUX_FILE_LINE_DECL)
00034   : AbstractButton(NUX_FILE_LINE_PARAM)
00035   , label("")
00036   , image_position(NUX_POSITION_TOP)
00037   {
00038     this->image = image;
00039     Init();
00040   }
00041 
00042   Button::Button (const std::string label, NUX_FILE_LINE_DECL)
00043   : AbstractButton (NUX_FILE_LINE_PARAM)
00044   , label (label)
00045   , image_position (NUX_POSITION_TOP)
00046     {
00047       this->image = NULL;
00048       Init();
00049     }
00050 
00051   Button::Button (const std::string label, TextureArea *image, NUX_FILE_LINE_DECL)
00052   : AbstractButton (NUX_FILE_LINE_PARAM)
00053   , label (label)
00054   , image_position (NUX_POSITION_TOP)
00055     {
00056       this->image = image;
00057       Init();
00058     }
00059 
00060   Button::Button (NUX_FILE_LINE_DECL)
00061   : AbstractButton (NUX_FILE_LINE_PARAM)
00062   , label ("")
00063   , image_position (NUX_POSITION_TOP)
00064   {
00065     this->image = NULL;
00066     Init();
00067   }
00068 
00069   Button::~Button()
00070   {
00071   }
00072 
00073   void Button::Init ()
00074   {
00075     // Set Geometry
00076     SetMinimumSize (DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
00077     image_position = NUX_POSITION_LEFT;
00078     state.changed.connect (sigc::mem_fun(this, &Button::OnStateChanged));
00079 
00080     // connect up to the imag/label signals
00081     label.changed.connect (sigc::mem_fun(this, &Button::OnLabelChanged));
00082 
00083     //FIXME - enable this once properties work.
00084     //image.changed.connect (sigc::mem_fun(this, &Button::OnImageChanged));
00085 
00086     image_position.changed.connect (sigc::mem_fun(this, &Button::OnImagePositionChanged));
00087 
00088     Layout *layout = new VLayout (NUX_TRACKER_LOCATION);
00089     SetLayout (layout);
00090 
00091     RebuildLayout();
00092   }
00093 
00094   void Button::SetImage (TextureArea *image)
00095   {
00096     this->image = image;
00097     OnImageChanged (this->image);
00098   }
00099 
00100   TextureArea *Button::GetImage ()
00101   {
00102     return this->image;
00103   }
00104 
00105   void Button::OnStateChanged (int value)
00106   {
00107     QueueDraw();
00108   }
00109 
00110   void Button::OnLabelChanged (std::string value)
00111   {
00112     RebuildLayout();
00113   }
00114 
00115   void Button::OnImageChanged (TextureArea *value)
00116   {
00117     RebuildLayout();
00118   }
00119 
00120   void Button::OnImagePositionChanged (int value)
00121   {
00122     RebuildLayout();
00123   }
00124 
00125   void Button::RebuildLayout()
00126   {
00127     Layout *layout;
00128 
00129     if (image_position == NUX_POSITION_LEFT || image_position == NUX_POSITION_RIGHT)
00130     {
00131       layout = new HLayout (NUX_TRACKER_LOCATION);
00132     }
00133     else
00134     {
00135       layout = new VLayout (NUX_TRACKER_LOCATION);
00136     }
00137 
00138     StaticText *text = NULL;
00139     if(label().empty () == false)
00140     {
00141       text = new StaticText(TEXT (label().c_str()));
00142       text->SetSensitive(false);
00143     }
00144 
00145     if(image != NULL && text != NULL)
00146     {
00147       if(image_position == NUX_POSITION_LEFT || image_position == NUX_POSITION_TOP)
00148       {
00149         layout->AddView(image, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00150         layout->AddView(text, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00151       }
00152       else
00153       {
00154         layout->AddView(text, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00155         layout->AddView(image, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00156       }
00157     }
00158     else if (image != NULL)
00159     {
00160       layout->AddView(image, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00161     }
00162     else if (text != NULL)
00163     {
00164       layout->AddView(text, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00165     }
00166 
00167     Layout *HPadding = new HLayout(NUX_TRACKER_LOCATION);
00168     Layout *VPadding = new VLayout(NUX_TRACKER_LOCATION);
00169 
00170     HPadding->AddLayout(new nux::SpaceLayout(12,12,12,12), 0);
00171     VPadding->AddLayout(new nux::SpaceLayout(12,12,12,12), 0);
00172     VPadding->AddLayout(layout, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00173     VPadding->AddLayout(new nux::SpaceLayout(12,12,12,12), 0);
00174     HPadding->AddLayout(VPadding, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00175     HPadding->AddLayout(new nux::SpaceLayout(12,12,12,12), 0);
00176 
00177     // NOTE - setting the layout here, unreferences the previous one, should cause all the memory
00178     // to be freed
00179     SetLayout (HPadding);
00180 
00181     QueueDraw();
00182   }
00183 
00184   void Button::Draw (GraphicsEngine &GfxContext, bool force_draw)
00185   {
00186     Geometry base = GetGeometry();
00187 
00188     //FIXME - nux button theming only supports a few states - low priority really.
00189     if(state == NUX_STATE_ACTIVE)
00190     {
00191       //FIXME - this uses eBUTTON_FOCUS but that's badly named, focus really means "mouse down" or "activated"
00192       GetPainter().PushDrawSliceScaledTextureLayer(GfxContext, base, eBUTTON_FOCUS, color::White, eAllCorners);
00193       GetPainter().PopBackground();
00194     }
00195     else if(state == NUX_STATE_PRELIGHT)
00196     {
00197       GetPainter().PushDrawSliceScaledTextureLayer(GfxContext, base, eBUTTON_PRELIGHT, color::White, eAllCorners);
00198       GetPainter().PopBackground();
00199     }
00200     else
00201     {
00202       GetPainter().PushDrawSliceScaledTextureLayer(GfxContext, base, eBUTTON_NORMAL, color::White, eAllCorners);
00203       GetPainter().PopBackground();
00204     }
00205   }
00206 
00207   void Button::DrawContent(GraphicsEngine &GfxContent, bool force_draw)
00208   {
00209     nux::Geometry base = GetGeometry();
00210     GfxContent.PushClippingRectangle(base);
00211 
00212     if (GetCompositionLayout())
00213       GetCompositionLayout()->ProcessDraw(GfxContent, force_draw);
00214 
00215     GfxContent.PopClippingRectangle();
00216   }
00217 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends