2e3911f4bfc3d4c633a9bdecfffc091057b18db6
[hildon] / src / hildon-entry.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Karl Lattimer <karl.lattimer@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser Public License as published by
10  * the Free Software Foundation; version 2 of the license.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser Public License for more details.
16  *
17  */
18
19 /**
20  * SECTION:hildon-entry
21  * @short_description: Widget representing a text entry in the Hildon framework.
22  *
23  * The #HildonEntry is a GTK widget which represents a text entry. It
24  * is derived from the GtkEntry widget and provides additional
25  * commodities specific to the Hildon framework.
26  */
27
28 #include                                        "hildon-entry.h"
29
30 G_DEFINE_TYPE                                   (HildonEntry, hildon_entry, GTK_TYPE_ENTRY);
31
32 static const gchar *placeholder_widget_name     = "hildon-entry-placeholder";
33
34 void
35 hildon_entry_set_text                           (HildonEntry *entry,
36                                                  const gchar *text)
37 {
38     g_return_if_fail (HILDON_IS_ENTRY (entry));
39
40     gtk_entry_set_text (GTK_ENTRY (entry), text);
41
42     gtk_widget_set_name (GTK_WIDGET (entry), NULL);
43 }
44
45 const gchar *
46 hildon_entry_get_text                           (HildonEntry *entry)
47 {
48     g_return_val_if_fail (HILDON_IS_ENTRY (entry), NULL);
49
50     if (g_str_equal (gtk_widget_get_name (GTK_WIDGET (entry)), placeholder_widget_name)) {
51         return "";
52     }
53
54     return gtk_entry_get_text (GTK_ENTRY (entry));
55 }
56
57 void
58 hildon_entry_set_placeholder                    (HildonEntry *entry,
59                                                  const gchar *text)
60 {
61     g_return_if_fail (HILDON_IS_ENTRY (entry));
62
63     gtk_widget_set_name (GTK_WIDGET (entry), placeholder_widget_name);
64
65     gtk_entry_set_text (GTK_ENTRY (entry), text);
66 }
67
68 GtkWidget *
69 hildon_entry_new                                (HildonSizeType size)
70 {
71     GtkWidget *entry = g_object_new (HILDON_TYPE_ENTRY, NULL);
72
73     hildon_gtk_widget_set_theme_size (entry, size);
74
75     return entry;
76 }
77
78 static gboolean
79 hildon_entry_focus_in_event                     (GtkWidget     *widget,
80                                                  GdkEventFocus *event)
81 {
82     if (g_str_equal (gtk_widget_get_name (widget), placeholder_widget_name)) {
83         hildon_entry_set_text (HILDON_ENTRY (widget), "");
84     }
85     if (GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_in_event) {
86         return GTK_WIDGET_CLASS (hildon_entry_parent_class)->focus_in_event (widget, event);
87     } else {
88         return FALSE;
89     }
90 }
91
92 static void
93 hildon_entry_class_init                         (HildonEntryClass *klass)
94 {
95     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
96
97     widget_class->focus_in_event = hildon_entry_focus_in_event;
98 }
99
100 static void
101 hildon_entry_init                               (HildonEntry *self)
102 {
103 }