Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) ShinyButton

 

shinybutton.h

 

//---------------------------------------------------------------------------
/*
ShinyButton, toggle button class
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppShinyButton.htm
//---------------------------------------------------------------------------
#ifndef SHINYBUTTON_H
#define SHINYBUTTON_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
///ShinyButton is a class for a toggle button
struct ShinyButton
{
  ShinyButton(
    const double color,
    const double gradient,
    const std::string& text = "");


  double GetColor() const { return m_color; }

  double GetGradient() const { return m_gradient; }

  const std::string& GetText() const { return m_text; }

  ///Set the ShinyButton its color
  void SetColor(
    const double color,
    const double gradient);

  ///Set the ShinyButton its text
  void SetText(const std::string& text);

  ///The signal that is emitted when ShinyButton changes color
  mutable boost::signals2::signal<void ()> m_signal_color_changed;

  ///The signal that is emitted when ShinyButton changes color
  mutable boost::signals2::signal<void ()> m_signal_text_changed;

  private:
  //ShinyButton can only be deleted by Boost smart pointers
  virtual ~ShinyButton() {}
  //ShinyButton can only be deleted by Boost smart pointers
  friend void boost::checked_delete<>(ShinyButton*);

  ///\brief
  ///The main color of the ShinyButton
  ///
  ///m_color denotes the fraction [0.0,1.0] on the rainbow:
  ///0.0 = red, 1.0 = magenta
  double m_color;

  ///\brief
  ///The change of gradient on the ShinyButton
  ///
  ///Gradient goes from [m_color-(0.5*m_gradient),m_color+(0.5*m_gradient)]
  ///0.0 = no gradient = a single color
  ///1.0 = show the entire rainbow with m_color in the middle
  double m_gradient;

  ///The text displayed by the ShinyButton
  std::string m_text;

  friend std::ostream& operator<<(std::ostream& os, const ShinyButton& button);

  public:
  static const std::string GetVersion();
  static const std::vector<std::string> GetVersionHistory();
};
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const ShinyButton& button);
//---------------------------------------------------------------------------
#endif // SHINYBUTTON_H
//---------------------------------------------------------------------------

 

 

 

 

 

shinybutton.cpp

 

//---------------------------------------------------------------------------
/*
ShinyButton, toggle button class
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppShinyButton.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>
//---------------------------------------------------------------------------
#include "shinybutton.h"
#include "trace.h"
//---------------------------------------------------------------------------
ShinyButton::ShinyButton(
  const double color,
  const double gradient,
  const std::string& text)
  : m_color(color),
    m_gradient(gradient),
    m_text(text)
{

}
//---------------------------------------------------------------------------
const std::string ShinyButton::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> ShinyButton::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-09-21: Version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------
void ShinyButton::SetColor(
  const double color,
  const double gradient)
{
  if (color != m_color || gradient != m_gradient)
  {
    m_color = color;
    m_gradient = gradient;
    m_signal_color_changed();
  }
}
//---------------------------------------------------------------------------
void ShinyButton::SetText(
  const std::string& text)
{
  if (text != m_text)
  {
    m_text = text;
    m_signal_text_changed();
  }
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const ShinyButton& button)
{
  os
    << "<ShinyButton>"
    << "<color>"
      << button.m_color
    << "</color>"
    << "<gradient>"
      << button.m_gradient
    << "</gradient>"
    << "<text>"
      << button.m_text
    << "</text>"
    << "</ShinyButton>";
  return os;

}
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml