Back to index

nux  3.0.0
RadioButton.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 "RadioButton.h"
00026 #include "StaticText.h"
00027 #include "RadioButtonGroup.h"
00028 
00029 namespace nux
00030 {
00031   NUX_IMPLEMENT_OBJECT_TYPE(RadioButton);
00032   
00033   RadioButton::RadioButton(const std::string& str, bool state, NUX_FILE_LINE_DECL)
00034   : AbstractCheckedButton(str, state, NUX_FILE_LINE_PARAM)
00035   {
00036     block_changed_signal_ = false;
00037     radio_group_index_ = -1;
00038   }
00039 
00040   RadioButton::~RadioButton()
00041   {
00042     if (radio_button_group_.IsValid() && radio_group_index_ != -1)
00043     {
00044       radio_button_group_->DisconnectButton(this);
00045 
00046       if (radio_button_group_->GetNumButtons() == 0)
00047       {
00048         // The last button in the group calls UnReference on the RadioButtonGroup.
00049         radio_button_group_->UnReference();
00050       }
00051     }
00052   }
00053 
00054   void RadioButton::Draw(GraphicsEngine& graphics_engine, bool force_draw)
00055   {
00056     Geometry base = GetGeometry();
00057     graphics_engine.PushClippingRectangle(base);
00058 
00059     GetPainter().PaintBackground(graphics_engine, base);
00060 
00061     InteractState is;
00062     is.is_on = active_;
00063 
00064     if (visual_state_ == VISUAL_STATE_PRESSED)
00065     {
00066       is.is_focus = true;
00067     }
00068     else if (visual_state_ == VISUAL_STATE_PRELIGHT)
00069     {
00070       is.is_prelight = true;
00071     }
00072     else
00073     {
00074       is.is_focus = false;
00075       is.is_prelight = false;
00076     }
00077 
00078     GetPainter().PushPaintLayerStack();
00079     {
00080       GetPainter().PaintRadioButton(graphics_engine, check_area_->GetGeometry(), is, Color(0xff000000));
00081       static_text_->ProcessDraw(graphics_engine, true);
00082     }
00083     GetPainter().PopPaintLayerStack();
00084 
00085     graphics_engine.PopClippingRectangle();
00086   }
00087 
00088   void RadioButton::RecvClick(int x, int y, unsigned long button_flags, unsigned long key_flags)
00089   {
00090     if (radio_button_group_.IsValid())
00091     {
00092       block_changed_signal_ = true;
00093       radio_button_group_->NotifyClick(this);
00094       block_changed_signal_ = false;
00095 
00096       state_change.emit(this);
00097       click.emit(this);
00098     }
00099     else
00100     {
00101       active_ = !active_;
00102       click.emit(this);
00103     }
00104     QueueDraw();
00105   }
00106 
00107   void RadioButton::Activate()
00108   {
00109     if (radio_button_group_.IsValid())
00110     {
00111       radio_button_group_->SetActiveButton(this, true);
00112       return;
00113     }
00114     SetStatePrivate(true);
00115   }
00116 
00117   void RadioButton::Deactivate()
00118   {
00119     if (radio_button_group_.IsValid())
00120     {
00121       // The RadioButton is part of a group. To deactivate it, activate another radio button in that group.";
00122       return;
00123     }
00124     SetStatePrivate(false);
00125   }
00126 
00127   void RadioButton::SetRadioButtonGroup(RadioButtonGroup* radio_button_group)
00128   {
00129     if (radio_button_group_.IsValid() && radio_button_group_.GetPointer() == radio_button_group)
00130       return;
00131 
00132     if (radio_button_group_.IsValid())
00133     {
00134       radio_button_group_.Release();
00135     }
00136 
00137     if (radio_button_group)
00138     {
00139       radio_button_group_ = ObjectWeakPtr<RadioButtonGroup>(radio_button_group);
00140     }
00141   }
00142 
00143   ObjectWeakPtr<RadioButtonGroup> RadioButton::GetRadioButtonGroup()
00144   {
00145     return radio_button_group_;
00146   }
00147 
00148   void RadioButton::SetStatePrivate(bool state)
00149   {
00150     active_ = state;
00151     QueueDraw();
00152   }
00153 
00154   void RadioButton::SetStatePrivate(bool state, bool emit_signal)
00155   {
00156     active_ = state;
00157     if (emit_signal && !block_changed_signal_)
00158     {
00159       state_change.emit(this);
00160     }
00161     QueueDraw();
00162   }
00163 }