ListAttribute.h
Go to the documentation of this file.00001 #ifndef LISTATTRIBUTE_H_
00002 #define LISTATTRIBUTE_H_
00003
00004 class NonModalComboBox
00005 {
00006 Callback m_changed;
00007 guint m_changedHandler;
00008
00009 static gboolean changed (GtkComboBox *widget, NonModalComboBox* self)
00010 {
00011 self->m_changed();
00012 return FALSE;
00013 }
00014
00015 public:
00016 NonModalComboBox (const Callback& changed) :
00017 m_changed(changed), m_changedHandler(0)
00018 {
00019 }
00020 void connect (GtkComboBox* combo)
00021 {
00022 m_changedHandler = g_signal_connect(G_OBJECT(combo), "changed", G_CALLBACK(changed), this);
00023 }
00024 void setActive (GtkComboBox* combo, int value)
00025 {
00026 g_signal_handler_disconnect(G_OBJECT(combo), m_changedHandler);
00027 gtk_combo_box_set_active(combo, value);
00028 connect(combo);
00029 }
00030 };
00031
00032 class ListAttribute: public EntityAttribute
00033 {
00034 std::string m_classname;
00035 std::string m_key;
00036 GtkComboBox* m_combo;
00037 NonModalComboBox m_nonModal;
00038 const ListAttributeType& m_type;
00039 public:
00040 ListAttribute (const std::string& classname, const std::string& key, const ListAttributeType& type) :
00041 m_classname(classname), m_key(key), m_combo(0), m_nonModal(ApplyCaller(*this)), m_type(type)
00042 {
00043 GtkComboBox* combo = GTK_COMBO_BOX(gtk_combo_box_new_text());
00044
00045 for (ListAttributeType::const_iterator i = type.begin(); i != type.end(); ++i) {
00046 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), i->first.c_str());
00047 }
00048
00049 gtk_widget_show(GTK_WIDGET(combo));
00050 m_nonModal.connect(combo);
00051
00052 m_combo = combo;
00053 }
00054
00055 GtkWidget* getWidget () const
00056 {
00057 return GTK_WIDGET(m_combo);
00058 }
00059 void apply (void)
00060 {
00061 entitySetValue(m_classname, m_key,
00062 m_type[gtk_combo_box_get_active(m_combo)].second);
00063 }
00064 typedef MemberCaller<ListAttribute, &ListAttribute::apply> ApplyCaller;
00065
00066 void update (void)
00067 {
00068 const std::string& value = entityGetValueForKey(m_key);
00069 ListAttributeType::const_iterator i = m_type.findValue(value);
00070 if (i != m_type.end()) {
00071 m_nonModal.setActive(m_combo, static_cast<int> (std::distance(m_type.begin(), i)));
00072 } else {
00073 m_nonModal.setActive(m_combo, 0);
00074 }
00075 }
00076
00077 };
00078
00079 #endif