00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_VALUELABEL_HPP_)
00012 #define _XRB_VALUELABEL_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #include "xrb_label.hpp"
00017 #include "xrb_util.hpp"
00018
00019 namespace Xrb
00020 {
00021
00022 template <typename ValueType>
00023 class ValueLabel : public Label
00024 {
00025 public:
00026
00027 typedef ValueType (*TextToValueFunctionType)(char const *);
00028
00029 ValueLabel (
00030 std::string const &printf_format,
00031 TextToValueFunctionType text_to_value_function,
00032 ContainerWidget *parent,
00033 std::string const &name = "ValueLabel");
00034 virtual ~ValueLabel () { }
00035
00036 inline ValueType Value () const
00037 {
00038 return m_value;
00039 }
00040 inline std::string const &PrintfFormat () const
00041 {
00042 return m_printf_format;
00043 }
00044 inline TextToValueFunctionType TextToValueFunction () const
00045 {
00046 return m_text_to_value_function;
00047 }
00048
00049 virtual void SetText (std::string const &text);
00050 void SetValue (ValueType value);
00051 inline void SetPrintfFormat (std::string const &printf_format)
00052 {
00053 m_printf_format = printf_format;
00054 }
00055 void SetTextToValueFunction (TextToValueFunctionType text_to_value_function);
00056
00057 inline SignalReceiver1<ValueType> const *ReceiverSetValue ()
00058 {
00059 return &m_receiver_set_value;
00060 }
00061
00062 protected:
00063
00064
00065 ValueType m_value;
00066
00067 private:
00068
00069
00070 std::string m_printf_format;
00071
00072 TextToValueFunctionType m_text_to_value_function;
00073
00075
00076 SignalReceiver1<ValueType> m_receiver_set_value;
00077 };
00078
00079 template <typename ValueType>
00080 ValueLabel<ValueType>::ValueLabel (
00081 std::string const &printf_format,
00082 TextToValueFunctionType text_to_value_function,
00083 ContainerWidget *const parent,
00084 std::string const &name)
00085 :
00086 Label("", parent, name),
00087 m_receiver_set_value(&ValueLabel<ValueType>::SetValue, this)
00088 {
00089
00090 SetPrintfFormat(printf_format);
00091
00092 m_text_to_value_function = text_to_value_function;
00093
00094 SetIsHeightFixedToTextHeight(true);
00095 }
00096
00097 template <typename ValueType>
00098 void ValueLabel<ValueType>::SetText (std::string const &text)
00099 {
00100 SetValue(TextToValueFunction()(text.c_str()));
00101 }
00102
00103 template <typename ValueType>
00104 void ValueLabel<ValueType>::SetValue (ValueType const value)
00105 {
00106 m_value = value;
00107 Label::SetText(Util::StringPrintf(PrintfFormat().c_str(), m_value));
00108 }
00109
00110 template <typename ValueType>
00111 void ValueLabel<ValueType>::SetTextToValueFunction (
00112 TextToValueFunctionType text_to_value_function)
00113 {
00114 m_text_to_value_function = text_to_value_function;
00115 SetText(Text());
00116 }
00117
00118
00119 #define SIGNED_INTEGER_PRINTF_FORMAT "%d"
00120 #define UNSIGNED_INTEGER_PRINTF_FORMAT "%u"
00121 #define DECIMAL_NOTATION_PRINTF_FORMAT "%g"
00122 #define SCIENTIFIC_NOTATION_PRINTF_FORMAT "%e"
00123
00124 }
00125
00126 #endif // !defined(_XRB_VALUELABEL_HPP_)