2008-09-22 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / examples / hildon-entry-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22
23 #include                                        <gtk/gtk.h>
24 #include                                        <hildon.h>
25
26 HildonEntry *mainentry;
27
28 static void
29 set_text_button_clicked                         (GtkButton   *button,
30                                                  HildonEntry *entry)
31 {
32     hildon_entry_set_text (mainentry, hildon_entry_get_text (entry));
33 }
34
35 static void
36 set_placeholder_button_clicked                  (GtkButton   *button,
37                                                  HildonEntry *entry)
38 {
39     hildon_entry_set_placeholder (mainentry, hildon_entry_get_text (entry));
40 }
41
42 static void
43 text_changed                                    (HildonEntry *entry,
44                                                  GParamSpec  *arg1,
45                                                  GtkLabel    *label)
46 {
47     /* Do *NOT* use gtk_entry_get_text () */
48     const gchar *text = hildon_entry_get_text (entry);
49
50     if (text != NULL && *text != '\0') {
51         gtk_label_set_text (label, text);
52     } else {
53         gtk_label_set_text (label, "(empty)");
54     }
55 }
56
57 int
58 main                                            (int    argc,
59                                                  char **argv)
60 {
61     GtkWidget *win;
62     GtkWidget *label;
63     GtkWidget *textentry, *textbutton, *texthbox;
64     GtkWidget *placeholderentry, *placeholderbutton, *placeholderhbox;
65     GtkBox *vbox;
66
67     gtk_init (&argc, &argv);
68
69     /* Window and vbox to pack everything */
70     win = hildon_stackable_window_new ();
71     vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
72
73     /* Entry to modify the text of the main HildonEntry */
74     textentry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
75     textbutton = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
76     gtk_button_set_label (GTK_BUTTON (textbutton), "Set entry text");
77     texthbox = gtk_hbox_new (FALSE, 10);
78
79     /* Entry to modify the placeholder of the main HildonEntry */
80     placeholderentry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
81     placeholderbutton = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
82     gtk_button_set_label (GTK_BUTTON (placeholderbutton), "Set entry placeholder");
83     placeholderhbox = gtk_hbox_new (FALSE, 10);
84
85     /* Main HildonEntry - this is the one showcased in this example */
86     mainentry = HILDON_ENTRY (hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
87     hildon_entry_set_placeholder (mainentry, "This is a placeholder - change using the buttons above");
88
89     /* This label is used to show the contents -not the placeholder- of the HildonEntry */
90     label = gtk_label_new (NULL);
91
92     /* Pack all widgets */
93     gtk_box_pack_start (GTK_BOX (texthbox), textentry, TRUE, TRUE, 0);
94     gtk_box_pack_start (GTK_BOX (texthbox), textbutton, FALSE, FALSE, 0);
95
96     gtk_box_pack_start (GTK_BOX (placeholderhbox), placeholderentry, TRUE, TRUE, 0);
97     gtk_box_pack_start (GTK_BOX (placeholderhbox), placeholderbutton, FALSE, FALSE, 0);
98
99     gtk_box_pack_start (vbox, texthbox, FALSE, FALSE, 0);
100     gtk_box_pack_start (vbox, placeholderhbox, FALSE, FALSE, 0);
101     gtk_box_pack_start (vbox, GTK_WIDGET (mainentry), FALSE, FALSE, 0);
102     gtk_box_pack_start (vbox, gtk_label_new ("Contents of the entry:"), TRUE, TRUE, 0);
103     gtk_box_pack_start (vbox, label, TRUE, TRUE, 0);
104
105     gtk_container_set_border_width (GTK_CONTAINER (win), 20);
106     gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));
107
108     /* Connect signals */
109     g_signal_connect (win, "destroy", G_CALLBACK (gtk_main_quit), NULL);
110     g_signal_connect (mainentry, "notify::text", G_CALLBACK (text_changed), label);
111     g_signal_connect (textbutton, "clicked",
112                       G_CALLBACK (set_text_button_clicked), textentry);
113     g_signal_connect (placeholderbutton, "clicked",
114                       G_CALLBACK (set_placeholder_button_clicked), placeholderentry);
115
116     /* Set the initial state of the label */
117     text_changed (mainentry, NULL, GTK_LABEL (label));
118
119     /* Run example */
120     gtk_widget_show_all (win);
121     gtk_main ();
122
123     return 0;
124 }