Back to index

nux  3.0.0
AbstractButton.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  *              Gordon Allott <gord.allott@canonical.com>
00020  *
00021  */
00022 
00023 #include "Nux.h"
00024 #include "AbstractButton.h"
00025 #include "StaticText.h"
00026 
00027 namespace nux
00028 {
00029   NUX_IMPLEMENT_OBJECT_TYPE(AbstractButton);
00030 
00031   AbstractButton::AbstractButton(NUX_FILE_LINE_DECL)
00032   : View(NUX_FILE_LINE_PARAM)
00033   , visual_state_(VISUAL_STATE_NORMAL)
00034   {
00035     active_ = false;
00036     mouse_pressed_ = false;
00037     static_text_ = NULL;
00038     label_color_ = color::White;
00039     same_size_as_content_ = false;
00040     label_font_size_ = 0; // use default point size defined in nux::StaticText
00041 
00042     mouse_click.connect(sigc::mem_fun(this, &AbstractButton::RecvClick));
00043     mouse_down.connect(sigc::mem_fun(this, &AbstractButton::RecvMouseDown));
00044     mouse_double_click.connect(sigc::mem_fun(this, &AbstractButton::RecvMouseDown));
00045     mouse_up.connect(sigc::mem_fun(this, &AbstractButton::RecvMouseUp));
00046     mouse_move.connect(sigc::mem_fun(this, &AbstractButton::RecvMouseMove));
00047     mouse_enter.connect(sigc::mem_fun(this, &AbstractButton::RecvMouseEnter));
00048     mouse_leave.connect(sigc::mem_fun(this, &AbstractButton::RecvMouseLeave));
00049   }
00050 
00051   AbstractButton::~AbstractButton()
00052   {
00053 
00054   }
00055 
00056   bool AbstractButton::Active() const
00057   {
00058     return active_;
00059   }
00060 
00061   void AbstractButton::SetActive(bool active)
00062   {
00063     if (active)
00064       Activate();
00065     else
00066       Deactivate();
00067   }
00068 
00069   ButtonVisualState AbstractButton::GetVisualState()
00070   {
00071     return visual_state_;
00072   }
00073 
00074   void AbstractButton::RecvMouseUp(int x, int y, unsigned long button_flags, unsigned long key_flags)
00075   {
00076     if (IsMousePointerInside())
00077     {
00078       visual_state_ = VISUAL_STATE_PRELIGHT;
00079     }
00080     else
00081     {
00082       visual_state_ = VISUAL_STATE_NORMAL;
00083     }
00084 
00085     mouse_pressed_ = false;
00086     visual_state_change.emit(this);
00087     QueueDraw();
00088   }
00089 
00090   void AbstractButton::RecvMouseDown(int x, int y, unsigned long button_flags, unsigned long key_flags)
00091   {
00092     visual_state_ = VISUAL_STATE_PRESSED;
00093     mouse_pressed_ = true;
00094     visual_state_change.emit(this);
00095     QueueDraw();
00096   }
00097 
00098   void AbstractButton::RecvMouseMove(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
00099   {
00100 
00101   }
00102 
00103   void AbstractButton::RecvMouseEnter(int x, int y, unsigned long button_flags, unsigned long key_flags)
00104   {
00105     if (mouse_pressed_)
00106     {
00107       visual_state_ = VISUAL_STATE_PRESSED;
00108     }
00109     else
00110     {
00111       visual_state_ = VISUAL_STATE_PRELIGHT;
00112     }
00113 
00114     visual_state_change.emit(this);
00115     QueueDraw();
00116   }
00117 
00118   void AbstractButton::RecvMouseLeave(int x, int y, unsigned long button_flags, unsigned long key_flags)
00119   {
00120     visual_state_ = VISUAL_STATE_NORMAL;
00121     visual_state_change.emit(this);
00122     QueueDraw();
00123   }
00124 
00125   void AbstractButton::SetLabelColor(const Color &color)
00126   {
00127     label_color_ = color;
00128     if (static_text_)
00129       static_text_->SetTextColor(label_color_);
00130     QueueDraw();
00131   }
00132 
00133   Color AbstractButton::GetLabelColor()
00134   {
00135     return label_color_;
00136   }
00137 
00138   ButtonVisualState AbstractButton::GetVisualState() const
00139   {
00140     return visual_state_;
00141   }
00142 
00143   void AbstractButton::SetLabelFontSize(int point)
00144   {
00145     if (point < 0)
00146       return;
00147 
00148     label_font_size_ = point;
00149     if (static_text_)
00150     {
00151       static_text_->SetTextPointSize(label_font_size_);
00152       ComputeContentSize();
00153       QueueDraw();
00154     }
00155   }
00156 
00157   int AbstractButton::GetLabelFontSize() const
00158   {
00159     return label_font_size_;
00160   }
00161 }