Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) WtShinyButtonWidget

 

wtshinybuttonwidget.h

 

//---------------------------------------------------------------------------
/*
WtShinyButtonWidget, Wt widget for displaying the ShinyButton 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/CppWtShinyButtonWidget.htm
//---------------------------------------------------------------------------
#ifndef WTSHINYBUTTONWIDGET_H
#define WTSHINYBUTTONWIDGET_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include <Wt/WPaintDevice>
#include <Wt/WPaintedWidget>
//---------------------------------------------------------------------------
namespace Wt { struct WMouseEventEvent; }
struct ShinyButton;
struct ShinyButtonWidget;
//---------------------------------------------------------------------------
///WtShinyButtonWidget displays a ShinyButton
struct WtShinyButtonWidget : public Wt::WPaintedWidget
{
  ///A WtShinyButtonWidget is created by its toggle state and its color
  WtShinyButtonWidget(
    const double color,
    const double gradient,
    const std::string& text = "");

  ///Obtain a read-only pointer to the ShinyButtonWidget
  const ShinyButtonWidget * GetWidget() const { return m_widget.get(); }

  ///Obtain a read-and-write pointer to the ShinyButtonWidget
  ShinyButtonWidget * GetWidget() { return m_widget.get(); }

  ///Set the color of the WtShinyButtonWidget
  //void SetColor(
  //  const unsigned char red,
  //  const unsigned char green,
  //  const unsigned char blue);

  ///Obtain the WtShinyButtonWidget its version
  static const std::string GetVersion();

  ///Obtain the WtShinyButtonWidget its version history
  static const std::vector<std::string> GetVersionHistory();

  ///Draw the ShinyButton
  static void DrawShinyButton(
    Wt::WPainter& painter,
    const int x, const int y,
    const int width, const int height,
    const ShinyButton * const button);

  ///Draw the ShinyButton
  static void DrawShinyButton(
    Wt::WPainter& painter,
    const ShinyButtonWidget * const widget);

  protected:
  ///Paint the WtShinyButtonWidget
  void paintEvent(Wt::WPaintDevice *paintDevice);

  private:
  ///The ShinyButtonWidget
  boost::scoped_ptr<ShinyButtonWidget> m_widget;

  ///The WtShinyButtonWidget its red color intensity
  //unsigned char m_red;

  ///The WtShinyButtonWidget its green color intensity
  //unsigned char m_green;

  ///The WtShinyButtonWidget its blue color intensity
  //unsigned char m_blue;

  ///Do not let this be called by the client
  void resize(const Wt::WLength& width, const Wt::WLength& height);

  ///Repaint the WtShinyButtonWidget
  void DoRepaint();

  ///Respond to mouse click
  void OnClicked(const Wt::WMouseEvent&) {}

  ///Respond to the widget resizing
  void OnResize();
};
//---------------------------------------------------------------------------
#endif // WTSHINYBUTTONWIDGET_H

 

 

 

 

 

wtshinybuttonwidget.cpp

 

//---------------------------------------------------------------------------
/*
WtShinyButtonWidget, Wt widget for displaying the ShinyButton 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/CppWtShinyButtonWidget.htm
//---------------------------------------------------------------------------
#include <iostream>
#include <boost/bind.hpp>
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include <Wt/WBrush>
#include <Wt/WEvent>
#include <Wt/WPainter>
#include <Wt/WPen>
//---------------------------------------------------------------------------
#include "rainbow.h"
#include "shinybutton.h"
#include "shinybuttonwidget.h"
#include "wtshinybuttonwidget.h"
//---------------------------------------------------------------------------
WtShinyButtonWidget::WtShinyButtonWidget(
  const double color,
  const double gradient,
  const std::string& text)
  : m_widget(new ShinyButtonWidget(color,gradient,text))
{
  assert(m_widget);

  m_widget->GetShinyButton()->m_signal_color_changed.connect(
    boost::bind(
      &WtShinyButtonWidget::DoRepaint,
      this));

  m_widget->GetShinyButton()->m_signal_text_changed.connect(
    boost::bind(
      &WtShinyButtonWidget::DoRepaint,
      this));

  m_widget->m_signal_geometry_changed.connect(
    boost::bind(
      &WtShinyButtonWidget::OnResize,
      this));

  this->clicked().connect(this,&WtShinyButtonWidget::OnClicked);
}
//---------------------------------------------------------------------------
void WtShinyButtonWidget::DoRepaint()
{
  this->update();
}
//---------------------------------------------------------------------------
void WtShinyButtonWidget::DrawShinyButton(
  Wt::WPainter& painter,
  const int left, const int top,
  const int width, const int height,
  const ShinyButton * const button)
{
  const double from = button->GetColor() - (0.5 * button->GetGradient());
  const double to   = button->GetColor() + (0.5 * button->GetGradient());
  const double dc   = (to - from) / boost::numeric_cast<double>(width);

  const int right = left + width;
  double c = from;

  const Wt::WPen old_pen = painter.pen();
  Wt::WPen pen = painter.pen();

  for (int x=left; x!=right; ++x,c+=dc)
  {
    double r,g,b;
    Rainbow::GetRgb(c,r,g,b);
    pen.setColor(Wt::WColor(
      r * 255.0,
      g * 255.0,
      b * 255.0));
    painter.setPen(pen);
    painter.drawLine(
      x,
      top,
      x,
      top + height);
  }
  painter.setPen(old_pen);
  Wt::WRectF r(left,top,width,height);
  painter.drawText(r,Wt::AlignCenter | Wt::AlignMiddle,button->GetText().c_str());
}
//---------------------------------------------------------------------------
void WtShinyButtonWidget::DrawShinyButton(
  Wt::WPainter& painter,
  const ShinyButtonWidget * const widget)
{
  DrawShinyButton(
    painter,
    widget->GetGeometry().GetX(),
    widget->GetGeometry().GetY(),
    widget->GetGeometry().GetWidth(),
    widget->GetGeometry().GetHeight(),
    widget->GetShinyButton());
}
//---------------------------------------------------------------------------
const std::string WtShinyButtonWidget::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> WtShinyButtonWidget::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("YYYY-MM-DD: version X.Y: [description]");
  v.push_back("2011-09-21: version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------
void WtShinyButtonWidget::OnResize()
{
  resize(m_widget->GetGeometry().GetWidth(),m_widget->GetGeometry().GetHeight());
}
//---------------------------------------------------------------------------
void WtShinyButtonWidget::paintEvent(Wt::WPaintDevice *paintDevice)
{
  Wt::WPainter painter(paintDevice);
  DrawShinyButton(painter,m_widget.get());
}
//---------------------------------------------------------------------------
void WtShinyButtonWidget::resize(const Wt::WLength& width, const Wt::WLength& height)
{
  Wt::WPaintedWidget::resize(width,height);
}
//---------------------------------------------------------------------------

 

 

 

 

 

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