Set transient parent for the "Full Name" editor dialog
[evolution.git] / src / addressbook / gui / contact-editor / e-contact-editor-fullname.c
blobe91c90c072164ff51b0b38cc1d35191d5504423e
1 /*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful, but
7 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
9 * for more details.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 * Authors:
16 * Chris Toshok <toshok@ximian.com>
18 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
22 #include "evolution-config.h"
24 #include <glib/gi18n.h>
26 #include "e-util/e-util.h"
27 #include "e-util/e-util-private.h"
29 #include "e-contact-editor-fullname.h"
31 static void fill_in_info (EContactEditorFullname *editor);
32 static void extract_info (EContactEditorFullname *editor);
34 enum {
35 PROP_0,
36 PROP_NAME,
37 PROP_EDITABLE
40 G_DEFINE_TYPE (
41 EContactEditorFullname,
42 e_contact_editor_fullname,
43 GTK_TYPE_DIALOG)
45 static void
46 e_contact_editor_fullname_set_property (GObject *object,
47 guint property_id,
48 const GValue *value,
49 GParamSpec *pspec)
51 EContactEditorFullname *e_contact_editor_fullname;
53 e_contact_editor_fullname = E_CONTACT_EDITOR_FULLNAME (object);
55 switch (property_id) {
56 case PROP_NAME:
57 e_contact_name_free (e_contact_editor_fullname->name);
59 if (g_value_get_pointer (value) != NULL) {
60 e_contact_editor_fullname->name =
61 e_contact_name_copy (
62 g_value_get_pointer (value));
63 fill_in_info (e_contact_editor_fullname);
65 else {
66 e_contact_editor_fullname->name = NULL;
68 break;
69 case PROP_EDITABLE: {
70 gboolean editable;
71 gint i;
73 const gchar *widget_names[] = {
74 "comboentry-title",
75 "comboentry-suffix",
76 "entry-first",
77 "entry-middle",
78 "entry-last",
79 "label-title",
80 "label-suffix",
81 "label-first",
82 "label-middle",
83 "label-last",
84 NULL
87 editable = g_value_get_boolean (value);
88 e_contact_editor_fullname->editable = editable;
90 for (i = 0; widget_names[i] != NULL; i++) {
91 GtkWidget *widget;
93 widget = e_builder_get_widget (
94 e_contact_editor_fullname->builder,
95 widget_names[i]);
97 if (GTK_IS_ENTRY (widget)) {
98 gtk_editable_set_editable (
99 GTK_EDITABLE (widget), editable);
101 } else if (GTK_IS_COMBO_BOX (widget)) {
102 GtkWidget *child;
104 child = gtk_bin_get_child (GTK_BIN (widget));
106 gtk_editable_set_editable (
107 GTK_EDITABLE (child), editable);
108 gtk_widget_set_sensitive (widget, editable);
110 } else if (GTK_IS_LABEL (widget)) {
111 gtk_widget_set_sensitive (widget, editable);
114 break;
116 default:
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
118 break;
122 static void
123 e_contact_editor_fullname_get_property (GObject *object,
124 guint property_id,
125 GValue *value,
126 GParamSpec *pspec)
128 EContactEditorFullname *e_contact_editor_fullname;
130 e_contact_editor_fullname = E_CONTACT_EDITOR_FULLNAME (object);
132 switch (property_id) {
133 case PROP_NAME:
134 extract_info (e_contact_editor_fullname);
135 g_value_set_pointer (
136 value, e_contact_name_copy (
137 e_contact_editor_fullname->name));
138 break;
139 case PROP_EDITABLE:
140 g_value_set_boolean (
141 value, e_contact_editor_fullname->editable);
142 break;
143 default:
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
145 break;
149 static void
150 e_contact_editor_fullname_dispose (GObject *object)
152 EContactEditorFullname *e_contact_editor_fullname;
154 e_contact_editor_fullname = E_CONTACT_EDITOR_FULLNAME (object);
156 if (e_contact_editor_fullname->builder) {
157 g_object_unref (e_contact_editor_fullname->builder);
158 e_contact_editor_fullname->builder = NULL;
161 if (e_contact_editor_fullname->name) {
162 e_contact_name_free (e_contact_editor_fullname->name);
163 e_contact_editor_fullname->name = NULL;
166 /* Chain up to parent's dispose() method. */
167 G_OBJECT_CLASS (e_contact_editor_fullname_parent_class)->dispose (object);
170 static void
171 e_contact_editor_fullname_class_init (EContactEditorFullnameClass *class)
173 GObjectClass *object_class;
175 object_class = G_OBJECT_CLASS (class);
176 object_class->set_property = e_contact_editor_fullname_set_property;
177 object_class->get_property = e_contact_editor_fullname_get_property;
178 object_class->dispose = e_contact_editor_fullname_dispose;
180 g_object_class_install_property (
181 object_class,
182 PROP_NAME,
183 g_param_spec_pointer (
184 "name",
185 "Name",
186 NULL,
187 G_PARAM_READWRITE));
189 g_object_class_install_property (
190 object_class,
191 PROP_EDITABLE,
192 g_param_spec_boolean (
193 "editable",
194 "Editable",
195 NULL,
196 FALSE,
197 G_PARAM_READWRITE));
200 static void
201 e_contact_editor_fullname_init (EContactEditorFullname *e_contact_editor_fullname)
203 GtkBuilder *builder;
204 GtkDialog *dialog;
205 GtkWidget *parent;
206 GtkWidget *widget;
207 GtkWidget *action_area;
208 GtkWidget *content_area;
209 const gchar *title;
211 dialog = GTK_DIALOG (e_contact_editor_fullname);
212 action_area = gtk_dialog_get_action_area (dialog);
213 content_area = gtk_dialog_get_content_area (dialog);
215 gtk_container_set_border_width (GTK_CONTAINER (action_area), 12);
216 gtk_container_set_border_width (GTK_CONTAINER (content_area), 0);
218 gtk_dialog_add_buttons (
219 dialog,
220 _("_Cancel"), GTK_RESPONSE_CANCEL,
221 _("_OK"), GTK_RESPONSE_OK, NULL);
223 gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
225 e_contact_editor_fullname->name = NULL;
227 builder = gtk_builder_new ();
228 e_load_ui_builder_definition (builder, "fullname.ui");
230 e_contact_editor_fullname->builder = builder;
232 widget = e_builder_get_widget (builder, "dialog-checkfullname");
233 title = gtk_window_get_title (GTK_WINDOW (widget));
234 gtk_window_set_title (GTK_WINDOW (e_contact_editor_fullname), title);
236 widget = e_builder_get_widget (builder, "table-checkfullname");
237 parent = gtk_widget_get_parent (widget);
238 g_object_ref (widget);
239 gtk_container_remove (GTK_CONTAINER (parent), widget);
240 gtk_box_pack_start (GTK_BOX (content_area), widget, TRUE, TRUE, 0);
241 g_object_unref (widget);
243 gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
245 gtk_window_set_icon_name (
246 GTK_WINDOW (e_contact_editor_fullname), "contact-new");
249 GtkWidget *
250 e_contact_editor_fullname_new (GtkWindow *parent,
251 const EContactName *name)
253 GtkWidget *widget = g_object_new (E_TYPE_CONTACT_EDITOR_FULLNAME,
254 "transient-for", parent,
255 NULL);
257 g_object_set (
258 widget,
259 "name", name,
260 NULL);
261 return widget;
264 static void
265 fill_in_field (EContactEditorFullname *editor,
266 const gchar *field,
267 const gchar *string)
269 GtkWidget *widget = e_builder_get_widget (editor->builder, field);
270 GtkEntry *entry = NULL;
272 if (GTK_IS_ENTRY (widget))
273 entry = GTK_ENTRY (widget);
274 else if (GTK_IS_COMBO_BOX (widget))
275 entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (widget)));
277 if (entry) {
278 if (string)
279 gtk_entry_set_text (entry, string);
280 else
281 gtk_entry_set_text (entry, "");
285 static void
286 fill_in_info (EContactEditorFullname *editor)
288 EContactName *name = editor->name;
289 if (name) {
290 fill_in_field (editor, "comboentry-title", name->prefixes);
291 fill_in_field (editor, "entry-first", name->given);
292 fill_in_field (editor, "entry-middle", name->additional);
293 fill_in_field (editor, "entry-last", name->family);
294 fill_in_field (editor, "comboentry-suffix", name->suffixes);
298 static gchar *
299 extract_field (EContactEditorFullname *editor,
300 const gchar *field)
302 GtkWidget *widget = e_builder_get_widget (editor->builder, field);
303 GtkEntry *entry = NULL;
305 if (GTK_IS_ENTRY (widget))
306 entry = GTK_ENTRY (widget);
307 else if (GTK_IS_COMBO_BOX (widget))
308 entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (widget)));
310 if (entry)
311 return g_strdup (gtk_entry_get_text (entry));
312 else
313 return NULL;
316 static void
317 extract_info (EContactEditorFullname *editor)
319 EContactName *name = editor->name;
320 if (!name) {
321 name = e_contact_name_new ();
322 editor->name = name;
325 name->prefixes = extract_field (editor, "comboentry-title");
326 name->given = extract_field (editor, "entry-first");
327 name->additional = extract_field (editor, "entry-middle");
328 name->family = extract_field (editor, "entry-last");
329 name->suffixes = extract_field (editor, "comboentry-suffix");