nux-1.14.0
CheckBox.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 
00023 #include "Nux.h"
00024 #include "HLayout.h"
00025 #include "VLayout.h"
00026 #include "EditTextBox.h"
00027 #include "CheckBox.h"
00028 #include "StaticText.h"
00029 
00030 namespace nux
00031 {
00032   NUX_IMPLEMENT_OBJECT_TYPE(CheckBox);
00033   
00034   CheckBox::CheckBox (std::string label, NUX_FILE_LINE_DECL)
00035   : AbstractButton (NUX_FILE_LINE_PARAM)
00036   , label(label)
00037   {
00038     togglable_ = true;
00039     Init();
00040   }
00041 
00042   CheckBox::~CheckBox()
00043   {
00044   }
00045 
00046   void CheckBox::Init()
00047   {
00048     SetMinimumSize(DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
00049 
00050     state.changed.connect(sigc::mem_fun(this, &CheckBox::OnStateChanged));
00051 
00052     // connect up to the label signal
00053     label.changed.connect(sigc::mem_fun(this, &CheckBox::OnLabelChanged));
00054 
00055     Layout *layout = new VLayout(NUX_TRACKER_LOCATION);
00056     SetLayout(layout);
00057 
00058     RebuildLayout();
00059   }
00060 
00061   void CheckBox::OnStateChanged(int value)
00062   {
00063     QueueDraw();
00064   }
00065 
00066   void CheckBox::OnLabelChanged(std::string value)
00067   {
00068     RebuildLayout();
00069   }
00070 
00071   void CheckBox::RebuildLayout()
00072   {
00073     Layout *layout = new HLayout(NUX_TRACKER_LOCATION);
00074 
00075     // add some padding for our checkbox draw
00076     layout->AddLayout(new SpaceLayout(20, 20, 20, 20), 0);
00077 
00078     if(label().empty() == false)
00079     {
00080       StaticText *text = new StaticText(TEXT(label().c_str()));
00081       layout->AddView (text, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00082     }
00083 
00084     Layout *HPadding = new HLayout(NUX_TRACKER_LOCATION);
00085     Layout *VPadding = new VLayout(NUX_TRACKER_LOCATION);
00086 
00087     HPadding->AddLayout(new nux::SpaceLayout(12,12,12,12), 0);
00088     VPadding->AddLayout(new nux::SpaceLayout(12,12,12,12), 0);
00089     VPadding->AddLayout(layout, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00090     VPadding->AddLayout(new nux::SpaceLayout(12,12,12,12), 0);
00091     HPadding->AddLayout(VPadding, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
00092     HPadding->AddLayout(new nux::SpaceLayout(12,12,12,12), 0);
00093 
00094     // NOTE - setting the layout here, unreferences the previous one, should cause all the memory
00095     // to be freed
00096     SetLayout (HPadding);
00097 
00098     QueueDraw();
00099   }
00100 
00101   void CheckBox::Draw (GraphicsEngine &GfxContext, bool force_draw)
00102   {
00103     Geometry base = GetGeometry();
00104 
00105     InteractState is;
00106     is.is_on = active;
00107     is.is_focus = NUX_STATE_ACTIVE;
00108     is.is_prelight = NUX_STATE_PRELIGHT;
00109 
00110     Geometry base_state = GetGeometry();
00111     base_state.SetWidth(20);
00112     base_state.SetX(base_state.x + 12);
00113 
00114     GetPainter().PaintCheckBox(GfxContext, base_state, is, Color(0xff000000));
00115   }
00116 
00117   void CheckBox::DrawContent(GraphicsEngine &GfxContext, bool force_draw)
00118   {
00119     nux::Geometry base = GetGeometry();
00120     GfxContext.PushClippingRectangle(base);
00121 
00122     if(GetCompositionLayout())
00123       GetCompositionLayout()->ProcessDraw (GfxContext, force_draw);
00124 
00125     GfxContext.PopClippingRectangle();
00126   }
00127 
00128 
00129 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends