Update called vala api.
[stuffkeeper.git] / src / stuffkeeper-data-entry-config.gob
blobfc99834382493a16e704eefb59c95d3a6c2481b0
1 %h{
2 #include <gtk/gtk.h>
3 #include "misc.h"
4 #include "stuffkeeper-data-item.h"
5 %}
7 %ph{
8 #include "stuffkeeper-data-backend.h"
9 %}
12 #define         PRIVATE_FIELD_DEFAULT 0
16 class Stuffkeeper:Data:Entry:Config from Gtk:HBox
18                 private GtkWidget *entry = {NULL};
19     private StuffkeeperDataSchema *schema = {NULL};
20     private gchar *field = {NULL} destroywith g_free;
21     private gulong changed_custom_field = {0};
22     private gulong changed_entry = {0};
24     private guint changed_entry_timeout = {0};
25     private GdkColor writing ;
26     private GdkColor base;
27     private GdkColor text;
29     /* lock signal */
30     private gulong signal_backend_locked = {0};
32     private
33     gboolean  
34     save_changes(self)
35     {
36         const gchar *text = gtk_entry_get_text(GTK_ENTRY(self->_priv->entry));
37        
38                                 stuffkeeper_data_schema_set_custom_field_string(self->_priv->schema,self->_priv->field,PRIVATE_FIELD_DEFAULT,text); 
40         gtk_widget_modify_text(GTK_WIDGET(self->_priv->entry), GTK_STATE_NORMAL, &(self->_priv->text));
41         gtk_widget_modify_base(GTK_WIDGET(self->_priv->entry), GTK_STATE_NORMAL, &(self->_priv->base));
42         self->_priv->changed_entry_timeout = 0;
43         return FALSE;
44     }
48     private
49     void
50     changed(self, gpointer data)
51     {
52         if(self->_priv->changed_entry_timeout)
53         {
54             g_source_remove(self->_priv->changed_entry_timeout);
55         }
56         self->_priv->changed_entry_timeout = g_timeout_add(1000,(GSourceFunc)self_save_changes,self);
58         gtk_widget_modify_text(GTK_WIDGET(self->_priv->entry), GTK_STATE_NORMAL, &(GTK_WIDGET(self)->style->black));
59         gtk_widget_modify_base(GTK_WIDGET(self->_priv->entry), GTK_STATE_NORMAL, &(self->_priv->writing));
60     }
61     private
62     void
63     item_changed(self,const gchar *id, int field, StuffkeeperDataSchema *schema)
64     {
65         const gchar *text = gtk_entry_get_text(GTK_ENTRY(self->_priv->entry));
66         gchar *value = NULL;
67                                 if(field != PRIVATE_FIELD_DEFAULT)
68                                                 return;
69                                 value = stuffkeeper_data_schema_get_custom_field_string(schema, self->_priv->field,field); 
70         if(value)
71         {
72             if(strcmp(text,value))
73             {
74                 g_signal_handler_block(self->_priv->entry,self->_priv->changed_entry);
75                 gtk_entry_set_text(GTK_ENTRY(self->_priv->entry), value);
76                 g_signal_handler_unblock(self->_priv->entry,self->_priv->changed_entry);
77             }
78             g_free(value);
79         }
80     }
81     private 
82         void
83     style_set(self, GtkStyle *prev, GtkWidget *wid)
84     {
85         if(self->_priv->changed_entry_timeout == 0) {
86             self->_priv->base = (GTK_WIDGET(self)->style->base[GTK_STATE_NORMAL]);
87             self->_priv->text = (GTK_WIDGET(self)->style->text[GTK_STATE_NORMAL]);
88         }
89     }
91     public
92     GtkWidget *
93     new(StuffkeeperDataSchema *schema, const gchar *field (check null))
94     {
95         Self *obj = GET_NEW;
96         gchar *value =NULL;
97                                 GtkWidget *label = gtk_label_new(_("Default value:"));
99                                 obj->_priv->text = (GTK_WIDGET(obj)->style->text[GTK_STATE_NORMAL]);
100         obj->_priv->writing.red = 255*255;
101         obj->_priv->writing.green = 253*255;
102         obj->_priv->writing.blue = 197*255;
103         gdk_colormap_alloc_color(gtk_widget_get_default_colormap(), &(obj->_priv->writing), TRUE,TRUE);
104         g_signal_connect_swapped(G_OBJECT(obj), "style-set", G_CALLBACK(self_style_set),obj);
107                                 
108         /* store item */
109         obj->_priv->schema = schema; 
110         /* field */
111         obj->_priv->field = g_strdup(field);
112                                 obj->_priv->entry = gtk_entry_new();
113             
114                                 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
115                                 gtk_box_pack_start(GTK_BOX(obj), label, FALSE, TRUE,0);
116                                         gtk_box_pack_start(GTK_BOX(obj), obj->_priv->entry, TRUE, TRUE,0);
118         obj->_priv->changed_custom_field = g_signal_connect_swapped(G_OBJECT(obj->_priv->schema), 
119                                                 "schema_custom_field_changed", G_CALLBACK(self_item_changed), obj);
121                                 value = stuffkeeper_data_schema_get_custom_field_string(schema, obj->_priv->field,PRIVATE_FIELD_DEFAULT); 
122                                 /* update the entry */
123                                 if(value)
124                                 {
125             gtk_entry_set_text(GTK_ENTRY(obj->_priv->entry), value);
126             g_free(value);
127         }
129         /* this is destroyed when self is destroyed, so no need to disconnect myself */
130         obj->_priv->changed_entry = g_signal_connect_swapped(G_OBJECT(obj->_priv->entry), "changed", G_CALLBACK(self_changed), obj);
131         return GTK_WIDGET(obj);
132     }
134     override (G:Object)
135         void
136         dispose(G:Object *obj)
137         {
138             Self *self = SELF(obj);
140             if(self->_priv->changed_custom_field)
141             {
142                 g_signal_handler_disconnect(self->_priv->schema,self->_priv->changed_custom_field);
143                 self->_priv->changed_custom_field = 0;
144             }
146            if(self->_priv->changed_entry_timeout)
147             {
148                 g_source_remove(self->_priv->changed_entry_timeout);
149                 self->_priv->changed_entry_timeout = 0;
151                 self_save_changes(self);
152             }
155             PARENT_HANDLER(obj);
156         }