1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
2 | * |
---|
3 | * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
18 | * |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <unistd.h> |
---|
26 | #include <string.h> |
---|
27 | |
---|
28 | #include <locale.h> |
---|
29 | |
---|
30 | #include <glib.h> |
---|
31 | #include <glib/gi18n.h> |
---|
32 | #include <glib-object.h> |
---|
33 | #include <gtk/gtk.h> |
---|
34 | |
---|
35 | #include "gdm-language-chooser-widget.h" |
---|
36 | #include "gdm-language-chooser-dialog.h" |
---|
37 | |
---|
38 | #define GDM_LANGUAGE_CHOOSER_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_LANGUAGE_CHOOSER_DIALOG, GdmLanguageChooserDialogPrivate)) |
---|
39 | |
---|
40 | struct GdmLanguageChooserDialogPrivate |
---|
41 | { |
---|
42 | GtkWidget *chooser_widget; |
---|
43 | }; |
---|
44 | |
---|
45 | |
---|
46 | static void gdm_language_chooser_dialog_class_init (GdmLanguageChooserDialogClass *klass); |
---|
47 | static void gdm_language_chooser_dialog_init (GdmLanguageChooserDialog *language_chooser_dialog); |
---|
48 | static void gdm_language_chooser_dialog_finalize (GObject *object); |
---|
49 | |
---|
50 | G_DEFINE_TYPE (GdmLanguageChooserDialog, gdm_language_chooser_dialog, GTK_TYPE_DIALOG) |
---|
51 | |
---|
52 | char * |
---|
53 | gdm_language_chooser_dialog_get_current_language_name (GdmLanguageChooserDialog *dialog) |
---|
54 | { |
---|
55 | char *language_name; |
---|
56 | |
---|
57 | g_return_val_if_fail (GDM_IS_LANGUAGE_CHOOSER_DIALOG (dialog), NULL); |
---|
58 | |
---|
59 | language_name = gdm_language_chooser_widget_get_current_language_name (GDM_LANGUAGE_CHOOSER_WIDGET (dialog->priv->chooser_widget)); |
---|
60 | |
---|
61 | return language_name; |
---|
62 | } |
---|
63 | |
---|
64 | void |
---|
65 | gdm_language_chooser_dialog_set_current_language_name (GdmLanguageChooserDialog *dialog, |
---|
66 | const char *language_name) |
---|
67 | { |
---|
68 | |
---|
69 | g_return_if_fail (GDM_IS_LANGUAGE_CHOOSER_DIALOG (dialog)); |
---|
70 | |
---|
71 | gdm_language_chooser_widget_set_current_language_name (GDM_LANGUAGE_CHOOSER_WIDGET (dialog->priv->chooser_widget), language_name); |
---|
72 | } |
---|
73 | |
---|
74 | static void |
---|
75 | gdm_language_chooser_dialog_size_request (GtkWidget *widget, |
---|
76 | GtkRequisition *requisition) |
---|
77 | { |
---|
78 | int screen_w; |
---|
79 | int screen_h; |
---|
80 | GtkRequisition child_requisition; |
---|
81 | |
---|
82 | if (GTK_WIDGET_CLASS (gdm_language_chooser_dialog_parent_class)->size_request) { |
---|
83 | GTK_WIDGET_CLASS (gdm_language_chooser_dialog_parent_class)->size_request (widget, requisition); |
---|
84 | } |
---|
85 | |
---|
86 | screen_w = gdk_screen_get_width (gtk_widget_get_screen (widget)); |
---|
87 | screen_h = gdk_screen_get_height (gtk_widget_get_screen (widget)); |
---|
88 | |
---|
89 | gtk_widget_get_child_requisition (GTK_BIN (widget)->child, &child_requisition); |
---|
90 | *requisition = child_requisition; |
---|
91 | |
---|
92 | requisition->width += 2 * GTK_CONTAINER (widget)->border_width; |
---|
93 | requisition->height += 2 * GTK_CONTAINER (widget)->border_width; |
---|
94 | |
---|
95 | requisition->width = MIN (requisition->width, .50 * screen_w); |
---|
96 | requisition->height = MIN (requisition->height, .80 * screen_h); |
---|
97 | } |
---|
98 | |
---|
99 | static void |
---|
100 | gdm_language_chooser_dialog_realize (GtkWidget *widget) |
---|
101 | { |
---|
102 | GdmLanguageChooserDialog *chooser_dialog; |
---|
103 | GdkWindow *root_window; |
---|
104 | GdkCursor *cursor; |
---|
105 | |
---|
106 | root_window = gdk_screen_get_root_window (gdk_screen_get_default ()); |
---|
107 | cursor = gdk_cursor_new (GDK_WATCH); |
---|
108 | gdk_window_set_cursor (root_window, cursor); |
---|
109 | gdk_cursor_unref (cursor); |
---|
110 | |
---|
111 | chooser_dialog = GDM_LANGUAGE_CHOOSER_DIALOG (widget); |
---|
112 | |
---|
113 | gtk_widget_show (chooser_dialog->priv->chooser_widget); |
---|
114 | |
---|
115 | GTK_WIDGET_CLASS (gdm_language_chooser_dialog_parent_class)->realize (widget); |
---|
116 | |
---|
117 | cursor = gdk_cursor_new (GDK_LEFT_PTR); |
---|
118 | gdk_window_set_cursor (root_window, cursor); |
---|
119 | gdk_cursor_unref (cursor); |
---|
120 | } |
---|
121 | |
---|
122 | static void |
---|
123 | gdm_language_chooser_dialog_class_init (GdmLanguageChooserDialogClass *klass) |
---|
124 | { |
---|
125 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
---|
126 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); |
---|
127 | |
---|
128 | object_class->finalize = gdm_language_chooser_dialog_finalize; |
---|
129 | widget_class->size_request = gdm_language_chooser_dialog_size_request; |
---|
130 | widget_class->realize = gdm_language_chooser_dialog_realize; |
---|
131 | |
---|
132 | g_type_class_add_private (klass, sizeof (GdmLanguageChooserDialogPrivate)); |
---|
133 | } |
---|
134 | |
---|
135 | static gboolean |
---|
136 | respond (GdmLanguageChooserDialog *dialog) |
---|
137 | { |
---|
138 | gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK); |
---|
139 | return FALSE; |
---|
140 | } |
---|
141 | |
---|
142 | static void |
---|
143 | queue_response (GdmLanguageChooserDialog *dialog) |
---|
144 | { |
---|
145 | g_idle_add ((GSourceFunc) respond, dialog); |
---|
146 | } |
---|
147 | |
---|
148 | static void |
---|
149 | gdm_language_chooser_dialog_init (GdmLanguageChooserDialog *dialog) |
---|
150 | { |
---|
151 | |
---|
152 | dialog->priv = GDM_LANGUAGE_CHOOSER_DIALOG_GET_PRIVATE (dialog); |
---|
153 | |
---|
154 | dialog->priv->chooser_widget = gdm_language_chooser_widget_new (); |
---|
155 | gdm_chooser_widget_set_hide_inactive_items (GDM_CHOOSER_WIDGET (dialog->priv->chooser_widget), |
---|
156 | FALSE); |
---|
157 | |
---|
158 | gdm_language_chooser_widget_set_current_language_name (GDM_LANGUAGE_CHOOSER_WIDGET (dialog->priv->chooser_widget), |
---|
159 | setlocale (LC_MESSAGES, NULL)); |
---|
160 | gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), dialog->priv->chooser_widget); |
---|
161 | |
---|
162 | g_signal_connect_swapped (G_OBJECT (dialog->priv->chooser_widget), |
---|
163 | "activated", G_CALLBACK (queue_response), |
---|
164 | dialog); |
---|
165 | |
---|
166 | gtk_dialog_add_buttons (GTK_DIALOG (dialog), |
---|
167 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
---|
168 | GTK_STOCK_OK, GTK_RESPONSE_OK, |
---|
169 | NULL); |
---|
170 | |
---|
171 | gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE); |
---|
172 | gtk_container_set_border_width (GTK_CONTAINER (dialog), 12); |
---|
173 | gtk_container_set_border_width (GTK_CONTAINER (dialog->priv->chooser_widget), 5); |
---|
174 | gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ALWAYS); |
---|
175 | gtk_window_set_default_size (GTK_WINDOW (dialog), 512, 440); |
---|
176 | gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK); |
---|
177 | } |
---|
178 | |
---|
179 | static void |
---|
180 | gdm_language_chooser_dialog_finalize (GObject *object) |
---|
181 | { |
---|
182 | GdmLanguageChooserDialog *language_chooser_dialog; |
---|
183 | |
---|
184 | g_return_if_fail (object != NULL); |
---|
185 | g_return_if_fail (GDM_IS_LANGUAGE_CHOOSER_DIALOG (object)); |
---|
186 | |
---|
187 | language_chooser_dialog = GDM_LANGUAGE_CHOOSER_DIALOG (object); |
---|
188 | |
---|
189 | g_return_if_fail (language_chooser_dialog->priv != NULL); |
---|
190 | |
---|
191 | G_OBJECT_CLASS (gdm_language_chooser_dialog_parent_class)->finalize (object); |
---|
192 | } |
---|
193 | |
---|
194 | GtkWidget * |
---|
195 | gdm_language_chooser_dialog_new (void) |
---|
196 | { |
---|
197 | GObject *object; |
---|
198 | |
---|
199 | object = g_object_new (GDM_TYPE_LANGUAGE_CHOOSER_DIALOG, |
---|
200 | "icon-name", "preferences-desktop-locale", |
---|
201 | "title", _("Languages"), |
---|
202 | "border-width", 8, |
---|
203 | "modal", TRUE, |
---|
204 | NULL); |
---|
205 | |
---|
206 | return GTK_WIDGET (object); |
---|
207 | } |
---|