#ifndef XATTRLABEL_H #define XATTRLABEL_H //$Id: XAttrLabel.h,v 1.16 2006/05/02 13:07:18 markus Rel $ // 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 2 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, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include namespace XGP { /**Class to display an attribute in a formated style while preservering its other abilities. An "attribute" is any of the standard C++ data types (except of strings, as they don't have a defined formattation) and the types of the YGP library (such as ADate, ANumeric, ...). This class only stores a reference to the attribute (which means it must not be deleted before this widget). */ template class XAttributeLabel : public Gtk::Label { public: /// Constructor; pass the attribute to manage XAttributeLabel (const T& attr) : Gtk::Label ("", false), attr_ (attr) { update (); } /// Constructor; pass the attribute to manage. It is aligned according to /// the values. XAttributeLabel (const T& attr, gfloat x, gfloat y) : Gtk::Label ("", x, y, false), attr_ (attr) { update (); } /// Constructor; pass the attribute to manage. It is aligned according to /// the values. XAttributeLabel (const T& attr, Gtk::AlignmentEnum xalign, Gtk::AlignmentEnum yalign = Gtk::ALIGN_CENTER) : Gtk::Label ("", xalign, yalign, false), attr_ (attr) { update (); } /// Destructor ~XAttributeLabel () { } /// Actualizes the displayed value with that of the attribute void update () { set_text (attr_.toString ()); } /// Returns a reference to the handled attribute T& getAttribute () { return const_cast (attr_); } private: XAttributeLabel (const XAttributeLabel&); const XAttributeLabel& operator= (const XAttributeLabel&); const T& attr_; }; /// Specialization of XAttributeLabel::update for short template <> inline void XAttributeLabel::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%d", attr_); set_text (buffer); } /// Specialization of XAttributeLabel::update for unsigned short template <> inline void XAttributeLabel::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%u", attr_); set_text (buffer); } /// Specialization of XAttributeLabel::update for int template <> inline void XAttributeLabel::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%d", attr_); set_text (buffer); } /// Specialization of XAttributeLabel::update for unsigned int template <> inline void XAttributeLabel::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%u", attr_); set_text (buffer); } /// Specialization of XAttributeLabel::update for long template <> inline void XAttributeLabel::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%ld", attr_); set_text (buffer); } /// Specialization of XAttributeLabel::update for unsigned long template <> inline void XAttributeLabel::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%lu", attr_); set_text (buffer); } /// Specialization of XAttributeLabel::update for double template <> inline void XAttributeLabel::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%lf", attr_); set_text (buffer); } /**Class to display an attribute in a formated style while preservering its other abilities. An "attribute" is any of the standard C++ data types (except of strings, as they don't have a defined formattation) and the types of the YGP library (such as ADate, ANumeric, ...). This class only stores the attribute itself (meaning: the value of the provided attribute is copied). Use this class (instead of XAttributeLabel) if you only want to display something without storing the displayed value by yourself. */ template class XAttributeLabel2 : public Gtk::Label { public: /// Constructor; pass the attribute to manage. It is aligned according to /// the values. XAttributeLabel2 (const T& attr) : Gtk::Label ("", false), attr_ (attr) { update (); } /// Constructor; pass the attribute to manage. It is aligned according to /// the values. XAttributeLabel2 (const T& attr, gfloat x, gfloat y) : Gtk::Label ("", x, y, false), attr_ (attr) { update (); } /// Constructor; pass the attribute to manage. It is aligned according to /// the values. XAttributeLabel2 (const T& attr, Gtk::AlignmentEnum xalign, Gtk::AlignmentEnum yalign = Gtk::ALIGN_CENTER) : Gtk::Label ("", xalign, yalign, false), attr_ (attr) { update (); } /// Destructor ~XAttributeLabel2 () { } /// Actualizes the displayed value with that of the attribute void update () { set_text (attr_.toString ()); } /// Returns a reference to the handled attribute T& getAttribute () { return const_cast (attr_); } private: XAttributeLabel2 (const XAttributeLabel2&); const XAttributeLabel2& operator= (const XAttributeLabel2&); const T attr_; }; /// Specialization of XAttributeLabel2::update for short template <> inline void XAttributeLabel2::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%d", attr_); set_text (buffer); } /// Specialization of XAttributeLabel2::update for unsigned short template <> inline void XAttributeLabel2::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%u", attr_); set_text (buffer); } /// Specialization of XAttributeLabel2::update for int template <> inline void XAttributeLabel2::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%d", attr_); set_text (buffer); } /// Specialization of XAttributeLabel2::update for unsigned int template <> inline void XAttributeLabel2::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%u", attr_); set_text (buffer); } /// Specialization of XAttributeLabel2::update for long template <> inline void XAttributeLabel2::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%ld", attr_); set_text (buffer); } /// Specialization of XAttributeLabel2::update for unsigned long template <> inline void XAttributeLabel2::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%lu", attr_); set_text (buffer); } /// Specialization of XAttributeLabel2::update for double template <> inline void XAttributeLabel2::update () { char buffer[20]; snprintf (buffer, sizeof (buffer), "%lf", attr_); set_text (buffer); } } #endif