No issues found
1 /*
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with the program; if not, see <http://www.gnu.org/licenses/>
15 *
16 *
17 * Authors:
18 * Chris Lahey <clahey@ximian.com>
19 *
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <glib/gi18n.h>
29 #include "e-util/e-util.h"
30 #include "e-util/e-util-private.h"
31 #include "e-util/e-unicode.h"
32
33 #include "gal-define-views-model.h"
34 #include "gal-view-new-dialog.h"
35
36 enum {
37 PROP_0,
38 PROP_NAME,
39 PROP_FACTORY
40 };
41
42 G_DEFINE_TYPE (GalViewNewDialog, gal_view_new_dialog, GTK_TYPE_DIALOG)
43
44 static void
45 gal_view_new_dialog_set_property (GObject *object,
46 guint property_id,
47 const GValue *value,
48 GParamSpec *pspec)
49 {
50 GalViewNewDialog *dialog;
51 GtkWidget *entry;
52
53 dialog = GAL_VIEW_NEW_DIALOG (object);
54
55 switch (property_id) {
56 case PROP_NAME:
57 entry = e_builder_get_widget (dialog->builder, "entry-name");
58 if (entry && GTK_IS_ENTRY (entry)) {
59 gtk_entry_set_text (GTK_ENTRY (entry), g_value_get_string (value));
60 }
61 break;
62 default:
63 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
64 return;
65 }
66 }
67
68 static void
69 gal_view_new_dialog_get_property (GObject *object,
70 guint property_id,
71 GValue *value,
72 GParamSpec *pspec)
73 {
74 GalViewNewDialog *dialog;
75 GtkWidget *entry;
76
77 dialog = GAL_VIEW_NEW_DIALOG (object);
78
79 switch (property_id) {
80 case PROP_NAME:
81 entry = e_builder_get_widget (dialog->builder, "entry-name");
82 if (entry && GTK_IS_ENTRY (entry)) {
83 g_value_set_string (value, gtk_entry_get_text (GTK_ENTRY (entry)));
84 }
85 break;
86 case PROP_FACTORY:
87 g_value_set_object (value, dialog->selected_factory);
88 break;
89 default:
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
91 break;
92 }
93 }
94
95 static void
96 gal_view_new_dialog_dispose (GObject *object)
97 {
98 GalViewNewDialog *gal_view_new_dialog = GAL_VIEW_NEW_DIALOG (object);
99
100 if (gal_view_new_dialog->builder)
101 g_object_unref (gal_view_new_dialog->builder);
102 gal_view_new_dialog->builder = NULL;
103
104 /* Chain up to parent's dispose() method. */
105 G_OBJECT_CLASS (gal_view_new_dialog_parent_class)->dispose (object);
106 }
107
108 static void
109 gal_view_new_dialog_class_init (GalViewNewDialogClass *class)
110 {
111 GObjectClass *object_class;
112
113 object_class = G_OBJECT_CLASS (class);
114 object_class->set_property = gal_view_new_dialog_set_property;
115 object_class->get_property = gal_view_new_dialog_get_property;
116 object_class->dispose = gal_view_new_dialog_dispose;
117
118 g_object_class_install_property (
119 object_class,
120 PROP_NAME,
121 g_param_spec_string (
122 "name",
123 "Name",
124 NULL,
125 NULL,
126 G_PARAM_READWRITE));
127
128 g_object_class_install_property (
129 object_class,
130 PROP_FACTORY,
131 g_param_spec_object (
132 "factory",
133 "Factory",
134 NULL,
135 GAL_TYPE_VIEW_FACTORY,
136 G_PARAM_READWRITE));
137 }
138
139 static void
140 gal_view_new_dialog_init (GalViewNewDialog *dialog)
141 {
142 GtkWidget *content_area;
143 GtkWidget *parent;
144 GtkWidget *widget;
145
146 dialog->builder = gtk_builder_new ();
147 e_load_ui_builder_definition (
148 dialog->builder, "gal-view-new-dialog.ui");
149
150 widget = e_builder_get_widget (dialog->builder, "table-top");
151 if (!widget) {
152 return;
153 }
154
155 g_object_ref (widget);
156
157 parent = gtk_widget_get_parent (widget);
158 gtk_container_remove (GTK_CONTAINER (parent), widget);
159
160 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
161 gtk_box_pack_start (GTK_BOX (content_area), widget, TRUE, TRUE, 0);
162
163 g_object_unref (widget);
164
165 gtk_dialog_add_buttons (
166 GTK_DIALOG (dialog),
167 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
168 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
169
170 gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
171 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
172 gtk_window_set_title (GTK_WINDOW (dialog), _("Define New View"));
173
174 dialog->collection = NULL;
175 dialog->selected_factory = NULL;
176 }
177
178 GtkWidget *
179 gal_view_new_dialog_new (GalViewCollection *collection)
180 {
181 GtkWidget *widget =
182 gal_view_new_dialog_construct (
183 g_object_new (GAL_VIEW_NEW_DIALOG_TYPE, NULL),
184 collection);
185 return widget;
186 }
187
188 static void
189 sensitize_ok_response (GalViewNewDialog *dialog)
190 {
191 gboolean ok = TRUE;
192 const gchar *text;
193
194 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry));
195 if (!text || !text[0])
196 ok = FALSE;
197
198 if (!dialog->selected_factory)
199 ok = FALSE;
200
201 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, ok);
202 }
203
204 static gboolean
205 selection_func (GtkTreeSelection *selection,
206 GtkTreeModel *model,
207 GtkTreePath *path,
208 gboolean path_currently_selected,
209 gpointer data)
210 {
211 GtkTreeIter iter;
212 GalViewNewDialog *dialog = data;
213
214 if (path_currently_selected)
215 return TRUE;
216
217 model = GTK_TREE_MODEL (dialog->list_store);
218
219 gtk_tree_model_get_iter (model, &iter, path);
220 gtk_tree_model_get (model, &iter, 1, &dialog->selected_factory, -1);
221
222 sensitize_ok_response (dialog);
223
224 return TRUE;
225 }
226
227 static void
228 entry_changed (GtkWidget *entry,
229 gpointer data)
230 {
231 GalViewNewDialog *dialog = data;
232
233 sensitize_ok_response (dialog);
234 }
235
236 GtkWidget *
237 gal_view_new_dialog_construct (GalViewNewDialog *dialog,
238 GalViewCollection *collection)
239 {
240 GList *iterator;
241 GtkTreeSelection *selection;
242 GtkTreeViewColumn *column;
243 GtkCellRenderer *rend;
244
245 dialog->collection = collection;
246 dialog->list = e_builder_get_widget (dialog->builder,"list-type-list");
247 dialog->entry = e_builder_get_widget (dialog->builder, "entry-name");
248
249 dialog->list_store = gtk_list_store_new (
250 2, G_TYPE_STRING, G_TYPE_POINTER);
251
252 rend = gtk_cell_renderer_text_new ();
253 column = gtk_tree_view_column_new_with_attributes (
254 "factory title", rend, "text", 0, NULL);
255
256 gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->list), column);
257
258 iterator = dialog->collection->factory_list;
259 for (; iterator; iterator = g_list_next (iterator)) {
260 GalViewFactory *factory = iterator->data;
261 GtkTreeIter iter;
262
263 g_object_ref (factory);
264 gtk_list_store_append (
265 dialog->list_store, &iter);
266 gtk_list_store_set (
267 dialog->list_store, &iter,
268 0, gal_view_factory_get_title (factory),
269 1, factory,
270 -1);
271 }
272
273 gtk_tree_view_set_model (
274 GTK_TREE_VIEW (dialog->list),
275 GTK_TREE_MODEL (dialog->list_store));
276
277 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->list));
278
279 gtk_tree_selection_set_select_function (
280 selection, selection_func, dialog, NULL);
281
282 g_signal_connect (
283 dialog->entry, "changed",
284 G_CALLBACK (entry_changed), dialog);
285
286 sensitize_ok_response (dialog);
287
288 return GTK_WIDGET (dialog);
289 }