Back to index

nux  3.0.0
RadioButtonGroup.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 #include "Nux.h"
00023 #include "RadioButton.h"
00024 #include "RadioButtonGroup.h"
00025 
00026 namespace nux
00027 {
00028   NUX_IMPLEMENT_OBJECT_TYPE(RadioButtonGroup);
00029 
00030   RadioButtonGroup::RadioButtonGroup(NUX_FILE_LINE_DECL)
00031   : InitiallyUnownedObject(NUX_FILE_LINE_PARAM)  // RadioButtonGroup is created as un-owned
00032   , active_button_index_(0)
00033   {
00034   }
00035 
00036   RadioButtonGroup::~RadioButtonGroup()
00037   {
00038     std::vector<ObjectWeakPtr<RadioButton> >::iterator it;
00039 
00040     for (it = radio_button_array_.begin(); it != radio_button_array_.end(); it++)
00041     {
00042       if ((*it).IsValid())
00043       {
00044         (*it)->SetRadioButtonGroup(NULL);
00045         (*it)->radio_group_index_ = -1;
00046       }
00047     }
00048   }
00049 
00050 
00051   void RadioButtonGroup::ConnectButton(RadioButton* radio)
00052   {
00053     if (radio == NULL)
00054     {
00055       return;
00056     }
00057 
00058     if (!radio->Type().IsDerivedFromType(RadioButton::StaticObjectType))
00059     {
00060       return;
00061     }
00062 
00063     std::vector<ObjectWeakPtr<RadioButton> >::iterator it;
00064 
00065     for (it = radio_button_array_.begin(); it != radio_button_array_.end(); it++)
00066     {
00067       if ((*it).IsValid() && ((*it).GetPointer() == radio))
00068       {
00069         // already in
00070         return;
00071       }
00072     }
00073 
00074     size_t index = (unsigned int) radio_button_array_.size();
00075 
00076     if (index == 0)
00077     {
00078       // Inserting the first radio button
00079       if (radio->GetRadioButtonGroup().IsValid())
00080       {
00081         // Disconnect from the other selector
00082         radio->GetRadioButtonGroup()->DisconnectButton(radio);
00083       }
00084 
00085       radio->SetRadioButtonGroup(this);
00086       radio->radio_group_index_ = 0;
00087       radio->SetStatePrivate(true);
00088       radio_button_array_.push_back(ObjectWeakPtr<RadioButton>(radio));
00089       active_button_index_ = 0;
00090     }
00091     else
00092     {
00093       if (radio->GetRadioButtonGroup().IsValid())
00094       {
00095         // Disconnect from the other selector
00096         radio->GetRadioButtonGroup()->DisconnectButton(radio);
00097       }
00098 
00099       radio->SetRadioButtonGroup(this);
00100       radio->radio_group_index_ = index;
00101       // When a radio button is added to a group that is not empty, its check state is set to false.
00102       radio->SetStatePrivate(false);
00103       radio_button_array_.push_back(ObjectWeakPtr<RadioButton>(radio));
00104     }
00105   }
00106 
00107   void RadioButtonGroup::DisconnectButton(RadioButton* radio)
00108   {
00109     bool found = false;
00110     size_t array_size = (unsigned int) radio_button_array_.size();
00111     std::vector<ObjectWeakPtr<RadioButton> >::iterator it;
00112     it = radio_button_array_.begin();
00113     size_t i;
00114 
00115     for (i = 0; i < array_size; i++, it++)
00116     {
00117       if (radio_button_array_[i] == radio)
00118       {
00119         radio->radio_group_index_ = -1;
00120         radio->SetStatePrivate(false);
00121         radio_button_array_.erase(it);
00122         found = true;
00123         break;
00124       }
00125     }
00126 
00127     if (found && (i - 1 > 0) && (radio_button_array_.size() >0))
00128     {
00129       // The previous button becomes active
00130       radio_button_array_[i]->SetStatePrivate(true);
00131     }
00132   }
00133 
00134   int RadioButtonGroup::GetNumButtons()
00135   {
00136     int count = 0;
00137     std::vector<ObjectWeakPtr<RadioButton> >::iterator it;
00138     for (it = radio_button_array_.begin(); it != radio_button_array_.end(); ++it)
00139     {
00140       if ((*it).IsValid())
00141       {
00142         ++count;
00143       }
00144     }
00145     return count;
00146   }
00147 
00148   void RadioButtonGroup::NotifyClick(RadioButton* radio)
00149   {
00150     std::vector<ObjectWeakPtr<RadioButton> >::iterator it;
00151 
00152     for (it = radio_button_array_.begin(); it != radio_button_array_.end(); ++it)
00153     {
00154       if ((*it).GetPointer() != radio)
00155       {
00156         if ((*it).IsValid())
00157         {
00158           (*it)->SetStatePrivate(false, true);
00159         }
00160       }
00161       else
00162       {
00163         if ((*it).IsValid())
00164         {
00165           (*it)->SetStatePrivate(true,  true);
00166         }
00167       }
00168     }
00169   }
00170 
00171   void RadioButtonGroup::SetActiveButton(RadioButton* radio, bool emit_signal)
00172   {
00173     std::vector<ObjectWeakPtr<RadioButton> >::iterator it = find(radio_button_array_.begin(), radio_button_array_.end(), radio);
00174 
00175     if (it == radio_button_array_.end())
00176       return;
00177 
00178     if ((*it).IsValid() == false)
00179     {
00180       // The button has been destroyed.
00181       // Remove it from the array
00182       radio_button_array_.erase(it);
00183       return;
00184     }
00185 
00186     for (it = radio_button_array_.begin(); it != radio_button_array_.end(); ++it)
00187     {
00188       if ((*it).IsValid() && ((*it).GetPointer() != radio))
00189       {
00190         (*it)->SetStatePrivate(false, emit_signal);
00191       }
00192     }
00193 
00194     radio->SetStatePrivate(true, emit_signal);
00195   }
00196 }