summaryrefslogtreecommitdiff
path: root/gui/simple-greeter/gdm-layout-chooser-dialog.c
blob: 20f5c0eb39662e96e495d5eb2e2faeb77f98085d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 Matthias Clasen <mclasen@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <locale.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
#include <gtk/gtk.h>

#include "gdm-layout-chooser-widget.h"
#include "gdm-layout-chooser-dialog.h"

#define GDM_LAYOUT_CHOOSER_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_LAYOUT_CHOOSER_DIALOG, GdmLayoutChooserDialogPrivate))

struct GdmLayoutChooserDialogPrivate
{
        GtkWidget *chooser_widget;
};


static void     gdm_layout_chooser_dialog_class_init  (GdmLayoutChooserDialogClass *klass);
static void     gdm_layout_chooser_dialog_init        (GdmLayoutChooserDialog      *layout_chooser_dialog);
static void     gdm_layout_chooser_dialog_finalize    (GObject                       *object);

G_DEFINE_TYPE (GdmLayoutChooserDialog, gdm_layout_chooser_dialog, GTK_TYPE_DIALOG)

char *
gdm_layout_chooser_dialog_get_current_layout_name (GdmLayoutChooserDialog *dialog)
{
        char *layout_name;

        g_return_val_if_fail (GDM_IS_LAYOUT_CHOOSER_DIALOG (dialog), NULL);

        layout_name = gdm_layout_chooser_widget_get_current_layout_name (GDM_LAYOUT_CHOOSER_WIDGET (dialog->priv->chooser_widget));

        return layout_name;
}

void
gdm_layout_chooser_dialog_set_current_layout_name (GdmLayoutChooserDialog *dialog,
                                                       const char               *layout_name)
{

        g_return_if_fail (GDM_IS_LAYOUT_CHOOSER_DIALOG (dialog));

        gdm_layout_chooser_widget_set_current_layout_name (GDM_LAYOUT_CHOOSER_WIDGET (dialog->priv->chooser_widget), layout_name);
}

static void
gdm_layout_chooser_dialog_size_request (GtkWidget      *widget,
                                       GtkRequisition *requisition)
{
        int            screen_w;
        int            screen_h;
        GtkRequisition child_requisition;

        if (GTK_WIDGET_CLASS (gdm_layout_chooser_dialog_parent_class)->size_request) {
                GTK_WIDGET_CLASS (gdm_layout_chooser_dialog_parent_class)->size_request (widget, requisition);
        }

        screen_w = gdk_screen_get_width (gtk_widget_get_screen (widget));
        screen_h = gdk_screen_get_height (gtk_widget_get_screen (widget));

        gtk_widget_get_child_requisition (GTK_BIN (widget)->child, &child_requisition);
        *requisition = child_requisition;

        requisition->width += 2 * GTK_CONTAINER (widget)->border_width;
        requisition->height += 2 * GTK_CONTAINER (widget)->border_width;

        requisition->width = MIN (requisition->width, .50 * screen_w);
        requisition->height = MIN (requisition->height, .80 * screen_h);
}

static void
gdm_layout_chooser_dialog_response (GtkDialog *dialog,
                                      int        response_id)
{
        GdmLayoutChooserDialog *chooser_dialog;

        chooser_dialog = GDM_LAYOUT_CHOOSER_DIALOG (dialog);

        if (response_id == GTK_RESPONSE_OK) {
                gdm_chooser_widget_activate_selected_item (GDM_CHOOSER_WIDGET (chooser_dialog->priv->chooser_widget));
        }
}

static void
gdm_layout_chooser_dialog_realize (GtkWidget *widget)
{
        GdmLayoutChooserDialog *chooser_dialog;

        chooser_dialog = GDM_LAYOUT_CHOOSER_DIALOG (widget);

        gtk_widget_show (chooser_dialog->priv->chooser_widget);

        GTK_WIDGET_CLASS (gdm_layout_chooser_dialog_parent_class)->realize (widget);
}

static void
gdm_layout_chooser_dialog_class_init (GdmLayoutChooserDialogClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
#ifdef I_COULD_GO_BACK_IN_TIME_AND_MAKE_RESPONSE_RUN_FIRST
        GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
#endif

        object_class->finalize = gdm_layout_chooser_dialog_finalize;
        widget_class->size_request = gdm_layout_chooser_dialog_size_request;
        widget_class->realize = gdm_layout_chooser_dialog_realize;
#ifdef I_COULD_GO_BACK_IN_TIME_AND_MAKE_RESPONSE_RUN_FIRST
        dialog_class->response = gdm_layout_chooser_dialog_response;
#endif

        g_type_class_add_private (klass, sizeof (GdmLayoutChooserDialogPrivate));
}

static gboolean
respond (GdmLayoutChooserDialog *dialog)
{
        gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
        return FALSE;
}

static void
queue_response (GdmLayoutChooserDialog *dialog)
{
        g_idle_add ((GSourceFunc) respond, dialog);
}

static void
gdm_layout_chooser_dialog_init (GdmLayoutChooserDialog *dialog)
{

        dialog->priv = GDM_LAYOUT_CHOOSER_DIALOG_GET_PRIVATE (dialog);

        dialog->priv->chooser_widget = gdm_layout_chooser_widget_new ();
        gdm_chooser_widget_set_hide_inactive_items (GDM_CHOOSER_WIDGET (dialog->priv->chooser_widget),
                                                    FALSE);

#ifndef I_COULD_GO_BACK_IN_TIME_AND_MAKE_RESPONSE_RUN_FIRST
        g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (gdm_layout_chooser_dialog_response), NULL);
#endif

        gdm_layout_chooser_widget_set_current_layout_name (GDM_LAYOUT_CHOOSER_WIDGET (dialog->priv->chooser_widget),
                                                               setlocale (LC_MESSAGES, NULL));
        gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), dialog->priv->chooser_widget);

        g_signal_connect_swapped (G_OBJECT (dialog->priv->chooser_widget),
                                  "activated", G_CALLBACK (queue_response),
                                  dialog);
        gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                GTK_STOCK_OK, GTK_RESPONSE_OK,
                                NULL);
        gtk_window_set_icon_name (GTK_WINDOW (dialog), "keyboard");
        gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
        gtk_container_set_border_width (GTK_CONTAINER (dialog), 12);
        gtk_container_set_border_width (GTK_CONTAINER (dialog->priv->chooser_widget), 5);
        gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ALWAYS);
        gtk_window_set_default_size (GTK_WINDOW (dialog), 512, 440);
}

static void
gdm_layout_chooser_dialog_finalize (GObject *object)
{
        GdmLayoutChooserDialog *layout_chooser_dialog;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GDM_IS_LAYOUT_CHOOSER_DIALOG (object));

        layout_chooser_dialog = GDM_LAYOUT_CHOOSER_DIALOG (object);

        g_return_if_fail (layout_chooser_dialog->priv != NULL);

        G_OBJECT_CLASS (gdm_layout_chooser_dialog_parent_class)->finalize (object);
}

GtkWidget *
gdm_layout_chooser_dialog_new (void)
{
        GObject *object;

        object = g_object_new (GDM_TYPE_LAYOUT_CHOOSER_DIALOG,
                               "title", _("Keyboard layouts"),
                               "border-width", 8,
                               "modal", TRUE,
                               NULL);

        return GTK_WIDGET (object);
}