Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Widget

 

A widget/control is a user interface element, like a button or checkbox.

 

Widget is a GUI independent widget class and servers as a base class for others (incomplete list):

 

Similar widget classes are QWidget and Wt::WWidget.

 

 

 

 

 

widget.h

 

//---------------------------------------------------------------------------
/*
Widget, GUI independent widget 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/CppWidget.htm
//---------------------------------------------------------------------------
#ifndef WIDGET_H
#define WIDGET_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include "rectangle.h"
//---------------------------------------------------------------------------
///GUI indepedent widget class, modeled after the Qt and Wt architure
struct Widget
{
  const Rect& GetGeometry() const { return m_geometry; }
  Rect& GetGeometry() { return m_geometry; }

  ///SetGeometry resizes the Widget and emits an OnResize signal
  void SetGeometry(const Rect& geometry);

  ///Obtain the version of this class
  static const std::string GetVersion();

  ///Obtain the version history of this class
  static const std::vector<std::string> GetVersionHistory();

  ///Respond to a change in size
  mutable boost::signals2::signal<void ()> m_signal_geometry_changed;

  protected:
  virtual ~Widget() {}
  friend void boost::checked_delete<>(const Widget*);

  private:
  Rect m_geometry;
};
//---------------------------------------------------------------------------
#endif // WIDGET_H

 

 

 

 

 

widget.cpp

 

//---------------------------------------------------------------------------
/*
Widget, GUI independent widget 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/CppWidget.htm
//---------------------------------------------------------------------------
#include "widget.h"
//---------------------------------------------------------------------------
const std::string Widget::GetVersion()
{
  return "1.1";
}
//---------------------------------------------------------------------------
const std::vector<std::string> Widget::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("YYYY-MM-DD: version X.Y: [description]");
  v.push_back("2011-07-03: version 1.0: initial version");
  v.push_back("2011-08-07: version 1.1: added signal that is emitted when geometry changes");
  return v;
}
//---------------------------------------------------------------------------
void Widget::SetGeometry(const Rect& geometry)
{
  if (geometry != m_geometry)
  {
    m_geometry = geometry;
    m_signal_geometry_changed();
  }
}
//---------------------------------------------------------------------------

 

 

 

 

 

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