Back to index

nux  3.0.0
LinearLayout.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 "LinearLayout.h"
00025 #include "View.h"
00026 
00027 namespace nux
00028 {
00029   NUX_IMPLEMENT_OBJECT_TYPE(LinearLayout);
00030 
00031   LinearLayout::LinearLayout(NUX_FILE_LINE_DECL)
00032   : Layout(NUX_FILE_LINE_PARAM)
00033   {
00034   }
00035 
00036   LinearLayout::~LinearLayout()
00037   {
00038   }
00039 
00040 // If(stretchfactor == 0): the WidgetLayout geometry will be set to SetGeometry(0,0,1,1);
00041 // and the children will take their natural size by expending WidgetLayout.
00042 // If the parent of WidgetLayout offers more space, it won't be used by WidgetLayout.
00043   void LinearLayout::AddLayout(Layout *layout, unsigned int stretchFactor, MinorDimensionPosition minor_position, MinorDimensionSize minor_size, float percentage, LayoutPosition index)
00044   {
00045     nuxAssertMsg(layout != 0, "[Layout::AddView] Invalid parameter.");
00046     NUX_RETURN_IF_TRUE(layout == 0);
00047     //  Should never happen
00048     nuxAssertMsg(layout != this, "[Layout::AddLayout] Error: Trying to add a layout to itself.");
00049     NUX_RETURN_IF_FALSE(layout != 0);
00050 
00051     Area *parent = layout->GetParentObject();
00052     nuxAssertMsg(parent == 0, "[Layout::AddLayout] Trying to add an object that already has a parent.");
00053     NUX_RETURN_IF_TRUE(parent != 0);
00054 
00055     nuxAssertMsg(index >= 0, "[Layout::AddLayout] Invalid index position. Adding at the beginning of the list..");
00056 
00057     layout->SetScaleFactor(stretchFactor);
00058     layout->SetPositioning(minor_position);
00059     layout->SetExtend(minor_size);
00060 
00061     if (percentage < 1.0f)
00062     {
00063       layout->SetPercentage(1.0f);
00064     }
00065     else if (percentage > 100.0f)
00066     {
00067       layout->SetPercentage(100.0f);
00068     }
00069     else
00070     {
00071       layout->SetPercentage(percentage);
00072     }
00073 
00074     layout->SetParentObject(this);
00075 
00076     layout->OnChildQueueDraw.connect(sigc::mem_fun(this, &Layout::ChildLayoutChildQueuedDraw));
00077     layout->OnQueueDraw.connect(sigc::mem_fun(this, &Layout::ChildLayoutQueuedDraw));
00078 
00079     if (index < 0)
00080       index = NUX_LAYOUT_BEGIN;
00081 
00082     if (index == NUX_LAYOUT_END || index >= _layout_element_list.size())
00083     {
00084       _layout_element_list.push_back(layout);
00085     }
00086     else
00087     {
00088       std::list<Area *>::iterator pos = _layout_element_list.begin();
00089       int idx = index;
00090       while (pos != _layout_element_list.end() && idx > 0)
00091       {
00092         idx--;
00093         pos++;
00094       }
00095       _layout_element_list.insert(pos, layout);
00096     }
00097 
00098   }
00099 
00101 
00125   void LinearLayout::AddView(Area *bo, unsigned int stretchFactor, MinorDimensionPosition minor_position, MinorDimensionSize minor_size, float percentage, LayoutPosition index)
00126   {
00127     nuxAssertMsg(bo != 0, "[Layout::AddView] Invalid parameter.");
00128     NUX_RETURN_IF_TRUE(bo == 0);
00129 
00130     Area *parent = bo->GetParentObject();
00131     nuxAssertMsg(parent == 0, "[Layout::AddView] Trying to add an object that already has a parent.");
00132     NUX_RETURN_IF_TRUE(parent != 0);
00133 
00134     nuxAssertMsg(index >= 0, "[Layout::AddView] Invalid index position. Adding at the beginning of the list..");
00135 
00136     bo->SetScaleFactor(stretchFactor);
00137     bo->SetPositioning(minor_position);
00138     bo->SetExtend(minor_size);
00139 
00140     if (percentage < 1.0f)
00141     {
00142       bo->SetPercentage(1.0f);
00143     }
00144     else if (percentage > 100.0f)
00145     {
00146       bo->SetPercentage(100.0f);
00147     }
00148     else
00149     {
00150       bo->SetPercentage(percentage);
00151     }
00152 
00153     bo->SetParentObject(this);
00154 
00155     if (bo->IsView())
00156       static_cast<View *> (bo)->OnQueueDraw.connect(sigc::mem_fun(this, &Layout::ChildViewQueuedDraw));
00157 
00158     //if(HasFocusControl() && HasFocusableEntries() == false)
00159     //{
00160       //bo->SetFocused(true);
00161       //ChildFocusChanged(this, bo);
00162     //}
00163 
00164     if (index < 0)
00165       index = NUX_LAYOUT_BEGIN;
00166 
00167     if (index == NUX_LAYOUT_END || index >= _layout_element_list.size())
00168     {
00169       _layout_element_list.push_back(bo);
00170     }
00171     else
00172     {
00173 #if defined(NUX_OS_WINDOWS) && !defined(NUX_VISUAL_STUDIO_2010)
00174       std::list<Area *>::iterator pos = _layout_element_list.begin();
00175 #else
00176       auto pos = _layout_element_list.begin();
00177 #endif
00178       int idx = index;
00179       while (pos != _layout_element_list.end() && idx > 0)
00180       {
00181         idx--;
00182         pos++;
00183       }
00184       _layout_element_list.insert(pos, bo);
00185     }
00186 
00187     ViewAdded.emit(this, bo);
00188     //--->> Removed because it cause problem with The splitter widget: ComputeContentSize();
00189   }
00190 
00191   void LinearLayout::AddSpace(unsigned int width, unsigned int stretchFactor, LayoutPosition index)
00192   {
00193     AddLayout(new SpaceLayout(), stretchFactor);
00194   }
00195 
00196   void LinearLayout::SetHorizontalInternalMargin(int space)
00197   {
00198     SetSpaceBetweenChildren(space);
00199   }
00200 
00201   void LinearLayout::SetVerticalInternalMargin(int space)
00202   {
00203     SetSpaceBetweenChildren(space);
00204   }
00205 
00206   void LinearLayout::SetSpaceBetweenChildren(int space)
00207   {
00208 #if DEBUG_LAYOUT
00209     return;
00210 #endif
00211     space_between_children_ = space >= 0 ? space : 0;
00212   }
00213 
00214 }