BooleanAttribute.h

Go to the documentation of this file.
00001 #ifndef BOOLEANATTRIBUTE_H_
00002 #define BOOLEANATTRIBUTE_H_
00003 
00007 class BooleanAttribute: public EntityAttribute
00008 {
00009         std::string m_classname;
00010         // The key that this BooleanAttribute is operating on.
00011         std::string m_key;
00012         // The visible checkbox
00013         GtkCheckButton* m_check;
00014 
00015         // Callback function to propagate the change to the keyval whenever the
00016         // checkbox is toggled. This is passed an explicit "this" pointer because
00017         // the GTK API is based on C not C++.
00018         static gboolean toggled (GtkWidget *widget, BooleanAttribute* self)
00019         {
00020             self->apply();
00021             return FALSE;
00022         }
00023     public:
00024         // Constructor
00025         BooleanAttribute (const std::string& classname, const std::string& key) :
00026             m_classname(classname), m_key(key), m_check(0)
00027         {
00028             GtkCheckButton* check = GTK_CHECK_BUTTON(gtk_check_button_new());
00029 
00030             // Show the checkbutton
00031             gtk_widget_show(GTK_WIDGET(check));
00032 
00033             m_check = check;
00034 
00035             // Connect up the callback function
00036             guint handler = g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggled), this);
00037             g_object_set_data(G_OBJECT(check), "handler", gint_to_pointer(handler));
00038 
00039             // Get the keyval after construction
00040             update();
00041         }
00042         // Return the checkbox as a GtkWidget
00043         GtkWidget* getWidget () const
00044         {
00045             return GTK_WIDGET(m_check);
00046         }
00047 
00048         // Propagate GUI changes to the underlying keyval
00049         void apply (void)
00050         {
00051             std::string value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_check)) ? "1" : "0";
00052             // Set 1 for checkbox ticked, 0 otherwise
00053             entitySetValue(m_classname, m_key, value);
00054         }
00055 
00056         // Retrieve keyval and update GtkWidget accordingly
00057         void update (void)
00058         {
00059             const std::string value = entityGetValueForKey(m_key);
00060             // Set checkbox to enabled for a keyval other than 0
00061             if (value.length() > 0) {
00062                 toggle_button_set_active_no_signal(GTK_TOGGLE_BUTTON(m_check), string::toInt(value) != 0);
00063             } else {
00064                 // No keyval found, set self to inactive
00065                 toggle_button_set_active_no_signal(GTK_TOGGLE_BUTTON(m_check), false);
00066             }
00067         }
00068 };
00069 
00070 #endif /* BOOLEANATTRIBUTE_H_ */

Generated by  doxygen 1.6.2