~ubuntu-branches/ubuntu/trusty/maliit-framework/trusty-proposed

« back to all changes in this revision

Viewing changes to examples/apps/gtk2-overrides/main.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2013-01-31 13:26:48 UTC
  • Revision ID: package-import@ubuntu.com-20130131132648-w1u9d2279tppxcft
Tags: upstream-0.94.1
ImportĀ upstreamĀ versionĀ 0.94.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Maliit framework
 
2
 *
 
3
 * Copyright (C) 2012 One Laptop per Child Association
 
4
 *
 
5
 * Contact: maliit-discuss@lists.maliit.org
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the licence, or (at your option) any later version.
 
11
 *
 
12
 * This library 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 GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the
 
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
#include <gtk/gtk.h>
 
24
#include <maliit-glib/maliitattributeextension.h>
 
25
 
 
26
#include "actionkeyfilter.h"
 
27
 
 
28
static void
 
29
put_widgets_into_container (GtkContainer* window, ...) G_GNUC_NULL_TERMINATED;
 
30
 
 
31
static void
 
32
put_widgets_into_container (GtkContainer* window, ...)
 
33
{
 
34
    va_list var_args;
 
35
 
 
36
    va_start (var_args, window);
 
37
    {
 
38
#if GTK_MAJOR_VERSION >= 3
 
39
        GtkGrid *grid = GTK_GRID (gtk_grid_new ());
 
40
        GtkWidget *previous_widget = NULL;
 
41
 
 
42
        for (;;) {
 
43
            GtkWidget *widget = va_arg (var_args, GtkWidget *);
 
44
 
 
45
            if (widget && GTK_IS_WIDGET (widget)) {
 
46
                gtk_grid_attach_next_to (grid, widget, previous_widget, GTK_POS_BOTTOM, 1, 1);
 
47
                previous_widget = widget;
 
48
            } else {
 
49
                break;
 
50
            }
 
51
        }
 
52
 
 
53
        gtk_container_add (window, GTK_WIDGET (grid));
 
54
#else
 
55
        GtkBox *box = GTK_BOX (gtk_vbox_new (FALSE, 10));
 
56
 
 
57
        for (;;) {
 
58
            GtkWidget *widget = va_arg (var_args, GtkWidget *);
 
59
 
 
60
            if (widget && GTK_IS_WIDGET (widget)) {
 
61
                gtk_box_pack_start (box, widget, FALSE, FALSE, 0);
 
62
            } else {
 
63
                break;
 
64
            }
 
65
        }
 
66
 
 
67
        gtk_container_add (window, GTK_WIDGET (box));
 
68
#endif
 
69
    }
 
70
    va_end (var_args);
 
71
}
 
72
 
 
73
static void
 
74
label_override (GtkToggleButton *button,
 
75
                gpointer user_data,
 
76
                const gchar* label)
 
77
{
 
78
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (user_data);
 
79
    const gchar* new_label;
 
80
    GVariant *value;
 
81
 
 
82
    if (gtk_toggle_button_get_active (button)) {
 
83
        new_label = label;
 
84
    } else {
 
85
        new_label = "";
 
86
    }
 
87
 
 
88
    value = g_variant_new_string (new_label);
 
89
    if (g_variant_is_floating (value)) {
 
90
        g_variant_ref_sink (value);
 
91
    }
 
92
    maliit_attribute_extension_set_attribute (extension, "/keys/actionKey/label", value);
 
93
    g_variant_unref (value);
 
94
}
 
95
 
 
96
static void
 
97
login_override (GtkToggleButton* button,
 
98
                gpointer user_data)
 
99
{
 
100
    label_override (button, user_data, "Next");
 
101
}
 
102
 
 
103
static void
 
104
login_change_enter_accepts (GtkToggleButton* button,
 
105
                            gpointer user_data)
 
106
{
 
107
    ActionKeyFilter *filter = (ActionKeyFilter*)user_data;
 
108
 
 
109
    action_key_filter_set_enter_login_accepts (filter,
 
110
                                               gtk_toggle_button_get_active (button));
 
111
}
 
112
 
 
113
static void
 
114
password_override (GtkToggleButton* button,
 
115
                   gpointer user_data)
 
116
{
 
117
    label_override (button, user_data, "Login");
 
118
}
 
119
 
 
120
static void
 
121
password_change_enter_accepts (GtkToggleButton* button,
 
122
                               gpointer user_data)
 
123
{
 
124
    ActionKeyFilter *filter = (ActionKeyFilter*)user_data;
 
125
 
 
126
    action_key_filter_set_enter_login_accepts (filter,
 
127
                                               gtk_toggle_button_get_active (button));
 
128
}
 
129
 
 
130
static gboolean
 
131
grab_focus (GtkWidget *widget G_GNUC_UNUSED,
 
132
            GdkEvent *event G_GNUC_UNUSED,
 
133
            gpointer user_data)
 
134
{
 
135
    GtkWidget *target_widget = GTK_WIDGET (user_data);
 
136
 
 
137
    gtk_widget_grab_focus (target_widget);
 
138
    return TRUE;
 
139
}
 
140
 
 
141
static void
 
142
check_button_realized (GtkWidget *widget,
 
143
                       gpointer user_data)
 
144
{
 
145
    GdkWindow *window = gtk_widget_get_window (widget);
 
146
 
 
147
    if (window) {
 
148
        GdkEventMask mask = gdk_window_get_events (window);
 
149
 
 
150
        mask |= GDK_FOCUS_CHANGE_MASK;
 
151
        gdk_window_set_events (window, mask);
 
152
 
 
153
        g_signal_connect (widget, "focus-in-event",
 
154
                          G_CALLBACK (grab_focus), user_data);
 
155
    }
 
156
}
 
157
 
 
158
gint
 
159
main(gint    argc,
 
160
     gchar **argv)
 
161
{
 
162
    MaliitAttributeExtension *login_extension;
 
163
    MaliitAttributeExtension *password_extension;
 
164
    GtkWidget *window;
 
165
    GtkWidget *hide_keyboard_button;
 
166
    GtkWidget *login_label;
 
167
    GtkWidget *login;
 
168
    GtkEntry *login_entry;
 
169
    GtkWidget *login_check_button;
 
170
    GtkWidget *password_label;
 
171
    GtkWidget *password;
 
172
    GtkEntry *password_entry;
 
173
    GtkWidget *password_check_button;
 
174
    GtkWidget *comment_label;
 
175
    GtkWidget *comment;
 
176
    GtkWidget *close;
 
177
    ActionKeyFilter *filter;
 
178
 
 
179
    gtk_init (&argc, &argv);
 
180
 
 
181
    login_extension = maliit_attribute_extension_new ();
 
182
    password_extension = maliit_attribute_extension_new ();
 
183
 
 
184
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
185
    gtk_window_set_title (GTK_WINDOW (window), "Maliit GTK+ key override test application");
 
186
 
 
187
    /* Clicking the button will steal focus from the text entry, thus
 
188
     * hiding the virtual keyboard. */
 
189
    hide_keyboard_button = gtk_button_new_with_label("Hide keyboard");
 
190
    login_label = gtk_label_new ("Login:");
 
191
    login = gtk_entry_new();
 
192
    login_entry = GTK_ENTRY (login);
 
193
    login_check_button = gtk_check_button_new_with_label ("Enter accepts login");
 
194
    password_label = gtk_label_new ("Password:");
 
195
    password = gtk_entry_new();
 
196
    password_entry = GTK_ENTRY (password);
 
197
    password_check_button = gtk_check_button_new_with_label ("Enter accepts password");
 
198
    comment_label = gtk_label_new ("Comment (not required):");
 
199
    comment = gtk_entry_new();
 
200
    close = gtk_button_new_with_label ("Close application");
 
201
    filter = action_key_filter_new (login_entry, password_entry);
 
202
 
 
203
    maliit_attribute_extension_attach_to_object (login_extension, G_OBJECT (login));
 
204
    g_signal_connect (login_check_button, "realize",
 
205
                      G_CALLBACK (check_button_realized), login);
 
206
    g_signal_connect (login_check_button, "toggled",
 
207
                      G_CALLBACK (login_override), login_extension);
 
208
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (login_check_button),
 
209
                                  action_key_filter_get_enter_login_accepts (filter));
 
210
    g_signal_connect (login_check_button, "toggled",
 
211
                      G_CALLBACK (login_change_enter_accepts), filter);
 
212
 
 
213
    gtk_entry_set_visibility (GTK_ENTRY (password), FALSE);
 
214
    maliit_attribute_extension_attach_to_object (password_extension, G_OBJECT (password));
 
215
    g_signal_connect (password_check_button, "realize",
 
216
                      G_CALLBACK (check_button_realized), password);
 
217
    g_signal_connect (password_check_button, "toggled",
 
218
                      G_CALLBACK (password_override), password_extension);
 
219
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (password_check_button),
 
220
                                  action_key_filter_get_enter_password_accepts (filter));
 
221
    g_signal_connect (password_check_button, "toggled",
 
222
                      G_CALLBACK (password_change_enter_accepts), filter);
 
223
 
 
224
    put_widgets_into_container (GTK_CONTAINER (window),
 
225
                                hide_keyboard_button,
 
226
                                login_label,
 
227
                                login,
 
228
                                login_check_button,
 
229
                                password_label,
 
230
                                password,
 
231
                                password_check_button,
 
232
                                comment_label,
 
233
                                comment,
 
234
                                close,
 
235
                                NULL);
 
236
 
 
237
    g_signal_connect(window, "delete-event",
 
238
                     G_CALLBACK(gtk_main_quit), NULL);
 
239
    g_signal_connect (close, "clicked",
 
240
                      G_CALLBACK (gtk_main_quit), NULL);
 
241
    gtk_widget_show_all(window);
 
242
 
 
243
    gtk_main();
 
244
 
 
245
    action_key_filter_free (filter);
 
246
    return 0;
 
247
}