ergtkentry.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <string.h>
00030 #include <ctype.h>
00031 #include <gtk/gtk.h>
00032
00033
00034
00035
00036 #include "ergtk_log.h"
00037 #include "ergtkentry.h"
00038
00039
00040 #undef gtk_entry_set_text
00041
00042
00043
00044 typedef struct
00045 {
00046 gboolean visible;
00047 GString *invisible_text;
00048 } erGtkEntryPrivate;
00049
00050
00051 static GtkEntryClass* g_parent_class = NULL;
00052
00053
00054
00055
00056
00057
00058 static gboolean ergtk_entry_focus_in (GtkWidget *widget, GdkEventFocus *event);
00059 static gboolean ergtk_entry_focus_out (GtkWidget *widget, GdkEventFocus *event);
00060 static void ergtk_entry_changed (GtkEditable *widget);
00061
00062
00063
00064
00065 static void ergtk_entry_class_init (erGtkEntryClass *klass);
00066 static void ergtk_entry_editable_init (GtkEditableClass *iface);
00067 static void ergtk_entry_init (erGtkEntry *input_entry);
00068 static void ergtk_entry_finalize (GObject *obj);
00069
00070
00071 GtkWidget* ergtk_entry_new()
00072 {
00073 LOGPRINTF("entry");
00074 erGtkEntry* item = (erGtkEntry *) g_object_new(ERGTK_ENTRY_TYPE, NULL);
00075
00076 return GTK_WIDGET(item);
00077 }
00078
00079 GType ergtk_entry_get_type(void)
00080 {
00081 static GType class_type = 0;
00082
00083 if (class_type == 0)
00084 {
00085 static const GTypeInfo class_info =
00086 {
00087 sizeof(erGtkEntryClass),
00088 NULL,
00089 NULL,
00090 (GClassInitFunc) ergtk_entry_class_init,
00091 NULL,
00092 NULL,
00093 sizeof(erGtkEntry),
00094 0,
00095 (GInstanceInitFunc) ergtk_entry_init,
00096 NULL
00097 };
00098 class_type = g_type_register_static(GTK_TYPE_ENTRY, "erGtkEntry", &class_info, 0);
00099
00100 static const GInterfaceInfo editable_info =
00101 {
00102 (GInterfaceInitFunc)ergtk_entry_editable_init,
00103 NULL,
00104 NULL
00105 };
00106 g_type_add_interface_static(class_type, GTK_TYPE_EDITABLE, &editable_info);
00107 }
00108
00109 return class_type;
00110 }
00111
00112
00113 static void ergtk_entry_class_init(erGtkEntryClass *klass)
00114 {
00115 LOGPRINTF("entry");
00116
00117 GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
00118 GObjectClass *object_class = (GObjectClass *) klass;
00119
00120
00121 g_parent_class = g_type_class_peek_parent(klass);
00122
00123
00124 widget_class->focus_in_event = ergtk_entry_focus_in;
00125 widget_class->focus_out_event = ergtk_entry_focus_out;
00126 object_class->finalize = ergtk_entry_finalize;
00127
00128
00129 g_type_class_add_private (klass, sizeof(erGtkEntryPrivate));
00130 }
00131
00132
00133 static void ergtk_entry_editable_init(GtkEditableClass *iface)
00134 {
00135 LOGPRINTF("entry");
00136
00137 iface->changed = ergtk_entry_changed;
00138 }
00139
00140
00141 static void ergtk_entry_init(erGtkEntry *er_entry)
00142 {
00143 LOGPRINTF("entry");
00144 g_return_if_fail(ERGTK_IS_ENTRY(er_entry));
00145
00146 GtkEntry *entry = (GtkEntry *) er_entry;
00147 GtkWidget *widget = (GtkWidget *) er_entry;
00148
00149 erGtkEntryPrivate *priv = ERGTK_ENTRY_GET_PRIVATE(er_entry);
00150
00151
00152 g_object_set(gtk_widget_get_settings(widget), "gtk-cursor-blink", FALSE, NULL);
00153
00154
00155 gtk_entry_set_editable(entry, TRUE);
00156 gtk_entry_set_visibility(entry, TRUE);
00157
00158
00159 priv->visible = TRUE;
00160 priv->invisible_text = g_string_new("");
00161 }
00162
00163
00164 static void ergtk_entry_finalize (GObject *obj)
00165 {
00166 LOGPRINTF("entry: obj [%p]", obj);
00167 g_return_if_fail( ERGTK_IS_ENTRY(obj) );
00168
00169 erGtkEntryPrivate *priv = ERGTK_ENTRY_GET_PRIVATE(obj);
00170
00171
00172
00173 g_string_free(priv->invisible_text, TRUE);
00174
00175
00176
00177 ((GObjectClass*)g_parent_class)->finalize( obj );
00178 }
00179
00180
00181
00182
00183
00184
00185 static gboolean ergtk_entry_focus_in(GtkWidget *widget, GdkEventFocus *event)
00186 {
00187 LOGPRINTF("entry");
00188 g_return_val_if_fail(ERGTK_IS_ENTRY(widget), FALSE);
00189
00190 gboolean rc = FALSE;
00191 GtkEntry *entry = (GtkEntry *) widget;
00192 erGtkEntry *er_entry = (erGtkEntry *) widget;
00193
00194 erGtkEntryPrivate *priv = ERGTK_ENTRY_GET_PRIVATE(er_entry);
00195
00196
00197 if (g_parent_class->parent_class.focus_in_event)
00198 {
00199 rc = g_parent_class->parent_class.focus_in_event(widget, event);
00200 }
00201
00202
00203
00204 priv->visible = gtk_entry_get_visibility(entry);
00205 if (priv->visible == FALSE)
00206 {
00207 const gchar *text = gtk_entry_get_text(entry);
00208 #if 0
00209
00210
00211 if (text[0] == '\0')
00212 {
00213 gtk_entry_set_visibility(entry, TRUE);
00214 }
00215 #else
00216
00217
00218 g_string_assign( priv->invisible_text, text );
00219 gtk_entry_set_text( entry, "" );
00220 gtk_entry_set_visibility(entry, TRUE);
00221 #endif
00222 }
00223
00224 return rc;
00225 }
00226
00227 static gboolean ergtk_entry_focus_out(GtkWidget *widget, GdkEventFocus *event)
00228 {
00229 LOGPRINTF("entry");
00230 g_return_val_if_fail(ERGTK_IS_ENTRY(widget), FALSE);
00231
00232 gboolean rc = FALSE;
00233 GtkEntry *entry = GTK_ENTRY(widget);
00234 erGtkEntry *er_entry = ERGTK_ENTRY(widget);
00235
00236 erGtkEntryPrivate *priv = ERGTK_ENTRY_GET_PRIVATE(er_entry);
00237
00238
00239 if (g_parent_class->parent_class.focus_out_event)
00240 {
00241 rc = g_parent_class->parent_class.focus_out_event(widget, event);
00242 }
00243
00244
00245 gtk_editable_select_region(GTK_EDITABLE(entry), -1, -1);
00246
00247
00248
00249 if (priv->visible == FALSE)
00250 {
00251 #if 0
00252
00253
00254 #else
00255
00256
00257 const gchar *text = gtk_entry_get_text(entry);
00258 if ( text[0] == '\0' )
00259 {
00260 gtk_entry_set_text( entry, priv->invisible_text->str );
00261 }
00262 #endif
00263 gtk_entry_set_visibility(entry, FALSE);
00264 }
00265
00266 return rc;
00267 }
00268
00269 static void ergtk_entry_changed(GtkEditable *editable)
00270 {
00271 LOGPRINTF("entry");
00272 g_return_if_fail(ERGTK_IS_ENTRY(editable));
00273
00274 erGtkEntry *er_entry = (erGtkEntry *) editable;
00275 GtkEntry *entry = (GtkEntry *) editable;
00276 GtkWidget *widget = (GtkWidget *) editable;
00277
00278 erGtkEntryPrivate *priv = ERGTK_ENTRY_GET_PRIVATE(er_entry);
00279
00280
00281 if ( priv->visible == FALSE
00282 && gtk_widget_is_focus(widget) )
00283 {
00284 const gchar *text = gtk_entry_get_text(entry);
00285 if (text[0] == '\0')
00286 {
00287 gtk_entry_set_visibility(entry, TRUE);
00288 }
00289 }
00290 }
00291