Back to index

nux  3.0.0
AbstractCheckedButton.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 "HLayout.h"
00025 #include "AbstractCheckedButton.h"
00026 #include "StaticText.h"
00027 
00028 namespace nux
00029 {
00030   NUX_IMPLEMENT_OBJECT_TYPE(AbstractCheckedButton);
00031   
00032   AbstractCheckedButton::AbstractCheckedButton(const std::string &str, bool state, NUX_FILE_LINE_DECL)
00033     : AbstractButton(NUX_FILE_LINE_PARAM)
00034   {
00035     label_ = str;
00036     active_ = state;
00037     hlayout_ = 0;
00038 
00039     static_text_  = new StaticText(label_, NUX_TRACKER_LOCATION);
00040     static_text_->SetTextColor(label_color_);
00041     hlayout_      = new HLayout(NUX_TRACKER_LOCATION);
00042     check_area_   = new InputArea(NUX_TRACKER_LOCATION);
00043 
00044     check_area_->SetInputEventSensitivity(false);
00045     static_text_->SetInputEventSensitivity(false);
00046 
00047     // Set Geometry
00048     check_area_->SetMinMaxSize(14, 14);
00049 
00050     hlayout_->SetSpaceBetweenChildren(4);
00051     hlayout_->SetContentDistribution(MAJOR_POSITION_LEFT);
00052     hlayout_->AddView(check_area_, 0, MINOR_POSITION_CENTER, MINOR_SIZE_MATCHCONTENT);
00053     hlayout_->AddView(static_text_, 1, MINOR_POSITION_CENTER, MINOR_SIZE_MATCHCONTENT);
00054 
00055 //     // This is convenient to make the layout and the AbstractCheckedButton fit the check area and the caption area.
00056 //     // Since the check area is bigger than 4x4, it will force the layout and the AbstractCheckedButton to grow.
00057 //     // This is useful if the AbstractCheckedButton is put in a vertical layout and it has a stretch factor of 0. Then the width of the AbstractCheckedButton
00058 //     // will be adjusted to fit the minimum width of the check area and the caption area.
00059 //     {
00060 //       hlayout_->SetMinimumSize(1, 1);
00061 //       SetMinimumSize(14, 14);
00062 //       ApplyMinWidth();
00063 //       ApplyMinHeight();
00064 //     }
00065 
00066     hlayout_->SetScaleFactor(0);
00067     SetLayout(hlayout_);
00068 
00069   }
00070 
00071   AbstractCheckedButton::~AbstractCheckedButton()
00072   {
00073   }
00074 
00075   void AbstractCheckedButton::SetLabel(const std::string &checkbox_label)
00076   {
00077     label_ = checkbox_label;
00078     static_text_->SetText(label_);
00079     QueueDraw();
00080   }
00081 
00082   std::string AbstractCheckedButton::GetLabel() const
00083   {
00084     return label_;
00085   }
00086 
00087   void AbstractCheckedButton::Draw(GraphicsEngine &graphics_engine, bool force_draw)
00088   {
00089     Geometry base = GetGeometry();
00090     graphics_engine.PushClippingRectangle(base);
00091 
00092     GetPainter().PaintBackground(graphics_engine, base);
00093 
00094     InteractState is;
00095     is.is_on = active_;
00096 
00097     if (visual_state_ == VISUAL_STATE_PRESSED)
00098     {
00099       is.is_focus = true;
00100     }
00101     else if (visual_state_ == VISUAL_STATE_PRELIGHT)
00102     {
00103       is.is_prelight = true;
00104     }
00105     else
00106     {
00107       is.is_focus = false;
00108       is.is_prelight = false;
00109     }
00110 
00111     GetPainter().PushPaintLayerStack();
00112     {
00113       GetPainter().PaintCheckBox(graphics_engine, check_area_->GetGeometry(), is, Color(0xff000000));
00114       static_text_->ProcessDraw(graphics_engine, true);
00115     }
00116     GetPainter().PopPaintLayerStack();
00117 
00118     graphics_engine.PopClippingRectangle();
00119   }
00120 
00121   long AbstractCheckedButton::ComputeContentSize()
00122   {
00123     if (view_layout_)
00124     {
00125       PreLayoutManagement();
00126 
00127       int old_width = GetBaseWidth();
00128       int old_height = GetBaseHeight();
00129 
00130       // Let the text view be as large as possible.
00131       static_text_->SetMaximumWidth(AREA_MAX_WIDTH);
00132 
00133       // Constrain the vertical expansion of the color selector.
00134       view_layout_->SetBaseHeight(1);
00135       long ret = view_layout_->ComputeContentSize();
00136 
00137       PostLayoutManagement(ret);
00138 
00139       {
00140         // Check if the text view goes out of the AbstractCheckedButton area.
00141         Geometry base = GetGeometry();
00142         Geometry text_geo = static_text_->GetGeometry();
00143  
00144         // Intersect the AbstractCheckedButton and the text view
00145         Geometry intersection = base.Intersect(text_geo);
00146         if (intersection != text_geo)
00147         {
00148           // The text view goes outside of the AbstractCheckedButton area. We have to clip it
00149           static_text_->SetMaximumWidth(intersection.width);
00150 
00151           // Assign a size of 1 to the layout and call ComputeContentSize.
00152           // Inside the StaticText::ComputeContentSize there is code that takes care of size negociation.
00153           view_layout_->SetBaseWidth(1);
00154           ret = view_layout_->ComputeContentSize();
00155 
00156           // Assign the layout geometry to the AbstractCheckedButton view.
00157           PostLayoutManagement(ret);
00158         }
00159       }
00160 
00161       int new_width = GetBaseWidth();
00162       int new_height = GetBaseHeight();
00163 
00164       long size_compliance = 0;
00165 
00166       // The layout has been resized to tightly pack its content
00167       if (new_width > old_width)
00168       {
00169         size_compliance |= eLargerWidth; // need scrollbar
00170       }
00171       else if (new_width < old_width)
00172       {
00173         size_compliance |= eSmallerWidth;
00174       }
00175       else
00176       {
00177         size_compliance |= eCompliantWidth;
00178       }
00179 
00180       // The layout has been resized to tightly pack its content
00181       if (new_height > old_height)
00182       {
00183         size_compliance |= eLargerHeight; // need scrollbar
00184       }
00185       else if (new_height < old_height)
00186       {
00187         size_compliance |= eSmallerHeight;
00188       }
00189       else
00190       {
00191         size_compliance |= eCompliantHeight;
00192       }
00193 
00194       return size_compliance;
00195     }
00196     else
00197     {
00198       PreLayoutManagement();
00199       int ret = PostLayoutManagement(eCompliantHeight | eCompliantWidth);
00200       return ret;
00201     }
00202 
00203     return 0;
00204   }
00205 
00206   void AbstractCheckedButton::SetLabelFontSize(int point)
00207   {
00208     AbstractButton::SetLabelFontSize(point);
00209     if (static_text_ == NULL)
00210       return;
00211     
00212     static_text_->SetTextPointSize(point);
00213     
00214     ComputeContentSize();
00215     QueueDraw();
00216   }
00217 }