00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_VALUEEDIT_HPP_)
00012 #define _XRB_VALUEEDIT_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #include "xrb_input_events.hpp"
00017 #include "xrb_key.hpp"
00018 #include "xrb_lineedit.hpp"
00019 #include "xrb_util.hpp"
00020 #include "xrb_validator.hpp"
00021
00022 namespace Xrb
00023 {
00024
00025 template <typename ValueType> struct Validator;
00026
00027 template <typename ValueType>
00028 class ValueEdit : public LineEdit
00029 {
00030 public:
00031
00032 typedef ValueType (*TextToValueFunctionType)(char const *);
00033
00034
00035
00036
00037
00038
00039 ValueEdit (
00040 std::string const &printf_format,
00041 TextToValueFunctionType text_to_value_function,
00042 ContainerWidget *const parent,
00043 std::string const &name = "ValueEdit")
00044 :
00045 LineEdit(20, parent, name),
00046 m_sender_value_updated(this),
00047 m_sender_value_set_by_enter_key(this),
00048 m_receiver_set_value(&ValueEdit<ValueType>::SetValue, this)
00049 {
00050
00051 SetPrintfFormat(printf_format);
00052
00053
00054 m_validator = NULL;
00055 m_text_to_value_function = text_to_value_function;
00056 }
00057 virtual ~ValueEdit () { }
00058
00059 inline ValueType Value () const
00060 {
00061 return m_value;
00062 }
00063 inline Validator<ValueType> const *GetValidator () const
00064 {
00065 return m_validator;
00066 }
00067 inline std::string const &PrintfFormat () const
00068 {
00069 return m_printf_format;
00070 }
00071 inline TextToValueFunctionType TextToValueFunction () const
00072 {
00073 return m_text_to_value_function;
00074 }
00075
00076 virtual void SetText (std::string const &text);
00077 void SetValue (ValueType value);
00078 void SetValidator (Validator<ValueType> const *validator);
00079 inline void SetPrintfFormat (std::string const &printf_format)
00080 {
00081 m_printf_format = printf_format;
00082 }
00083 void SetTextToValueFunction (TextToValueFunctionType text_to_value_function);
00084
00085 inline SignalSender1<ValueType> const *SenderValueUpdated ()
00086 {
00087 return &m_sender_value_updated;
00088 }
00089 inline SignalSender1<ValueType> const *SenderValueSetByEnterKey ()
00090 {
00091 return &m_sender_value_set_by_enter_key;
00092 }
00093
00094 inline SignalReceiver1<ValueType> const *ReceiverSetValue ()
00095 {
00096 return &m_receiver_set_value;
00097 }
00098
00099 protected:
00100
00101
00102 virtual bool ProcessKeyEvent (EventKey const *e);
00103
00104 virtual void HandleUnfocus ();
00105
00106 virtual void SignalTextUpdated ();
00107
00108 private:
00109
00110 void SetValueFromText ();
00111 void SignalValueUpdated ();
00112
00113
00114 ValueType m_value;
00115
00116 ValueType m_last_value_update;
00117
00118 Validator<ValueType> const *m_validator;
00119
00120 std::string m_printf_format;
00121
00122 TextToValueFunctionType m_text_to_value_function;
00123
00125
00126 SignalSender1<ValueType> m_sender_value_updated;
00127 SignalSender1<ValueType> m_sender_value_set_by_enter_key;
00128
00130
00131 SignalReceiver1<ValueType> m_receiver_set_value;
00132 };
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 template <typename ValueType>
00155 void ValueEdit<ValueType>::SetText (std::string const &text)
00156 {
00157 SetValue(m_text_to_value_function(text.c_str()));
00158 }
00159
00160 template <typename ValueType>
00161 void ValueEdit<ValueType>::SetValue (ValueType const value)
00162 {
00163 m_value = m_validator != NULL ?
00164 m_validator->Validate(value) :
00165 value;
00166 LineEdit::SetText(Util::StringPrintf(m_printf_format.c_str(), m_value));
00167 SignalValueUpdated();
00168 }
00169
00170 template <typename ValueType>
00171 void ValueEdit<ValueType>::SetValidator (
00172 Validator<ValueType> const *const validator)
00173 {
00174 m_validator = validator;
00175 SetValue(m_value);
00176 }
00177
00178 template <typename ValueType>
00179 void ValueEdit<ValueType>::SetTextToValueFunction (
00180 TextToValueFunctionType text_to_value_function)
00181 {
00182 m_text_to_value_function = text_to_value_function;
00183 SetText(Text());
00184 }
00185
00186 template <typename ValueType>
00187 bool ValueEdit<ValueType>::ProcessKeyEvent (EventKey const *const e)
00188 {
00189 if (e->IsKeyDownEvent() || e->IsKeyRepeatEvent())
00190 {
00191 switch (e->KeyCode())
00192 {
00193 case Key::RETURN:
00194 case Key::KP_ENTER:
00195
00196 if (LineEdit::Text()[0] != '\0')
00197 {
00198 SetValueFromText();
00199 m_sender_value_set_by_enter_key.Signal(Value());
00200 }
00201 break;
00202
00203 default:
00204 break;
00205 }
00206 }
00207
00208 return LineEdit::ProcessKeyEvent(e);
00209 }
00210
00211 template <typename ValueType>
00212 void ValueEdit<ValueType>::HandleUnfocus ()
00213 {
00214 SignalValueUpdated();
00215 LineEdit::HandleUnfocus();
00216 }
00217
00218 template <typename ValueType>
00219 void ValueEdit<ValueType>::SignalTextUpdated ()
00220 {
00221 SetValue(m_text_to_value_function(LineEdit::Text().c_str()));
00222 LineEdit::SignalTextUpdated();
00223 SignalValueUpdated();
00224 }
00225
00226 template <typename ValueType>
00227 void ValueEdit<ValueType>::SetValueFromText ()
00228 {
00229 ValueType value = m_text_to_value_function(LineEdit::Text().c_str());
00230 if (m_validator != NULL)
00231 value = m_validator->Validate(value);
00232 if (m_value != value)
00233 {
00234 m_value = value;
00235 SignalValueUpdated();
00236 }
00237 }
00238
00239 template <typename ValueType>
00240 void ValueEdit<ValueType>::SignalValueUpdated ()
00241 {
00242 if (m_last_value_update != Value())
00243 {
00244 m_last_value_update = Value();
00245 m_sender_value_updated.Signal(m_last_value_update);
00246 }
00247 }
00248
00249
00250 #define SIGNED_INTEGER_PRINTF_FORMAT "%d"
00251 #define UNSIGNED_INTEGER_PRINTF_FORMAT "%u"
00252 #define DECIMAL_NOTATION_PRINTF_FORMAT "%g"
00253 #define SCIENTIFIC_NOTATION_PRINTF_FORMAT "%e"
00254
00255 }
00256
00257 #endif // !defined(_XRB_VALUEEDIT_HPP_)