Added byline
[anjuta.git] / plugins / glade / anjuta-design-document.c
blob31ac14a511b6ff3c3a2b94fd93ced0932be73197
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) Johannes Schmid 2007 <jhs@gnome.org>
5 *
6 * anjuta is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * anjuta is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include "anjuta-design-document.h"
27 #include <libanjuta/anjuta-debug.h>
28 #include <libanjuta/interfaces/ianjuta-file.h>
29 #include <libanjuta/interfaces/ianjuta-file-savable.h>
30 #include <libanjuta/interfaces/ianjuta-document.h>
32 enum
34 PROP_0,
35 PROP_PLUGIN
38 struct _AnjutaDesignDocumentPrivate
40 GladePlugin* glade_plugin;
43 #define ADD_GET_PRIVATE(o) \
44 (G_TYPE_INSTANCE_GET_PRIVATE ((o), ANJUTA_TYPE_DESIGN_DOCUMENT, AnjutaDesignDocumentPrivate))
46 static void
47 on_project_changed (GladeProject* project, GladeCommand* command, gboolean forward, AnjutaDesignDocument* doc)
49 g_signal_emit_by_name (doc, "update-save-ui", NULL);
50 g_signal_emit_by_name (doc, "update-ui", NULL);
53 static void
54 anjuta_design_document_instance_init (AnjutaDesignDocument *object)
59 static void
60 anjuta_design_document_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
62 g_return_if_fail (ANJUTA_IS_DESIGN_DOCUMENT (object));
63 AnjutaDesignDocumentPrivate* priv = ADD_GET_PRIVATE(object);
65 switch (prop_id)
67 case PROP_PLUGIN:
68 priv->glade_plugin = g_value_get_object(value);
69 break;
70 default:
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
72 break;
76 static void
77 anjuta_design_document_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
79 g_return_if_fail (ANJUTA_IS_DESIGN_DOCUMENT (object));
82 switch (prop_id)
84 default:
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
86 break;
90 static void
91 anjuta_design_document_class_init (AnjutaDesignDocumentClass *klass)
93 GObjectClass* object_class = G_OBJECT_CLASS (klass);
95 object_class->set_property = anjuta_design_document_set_property;
96 object_class->get_property = anjuta_design_document_get_property;
98 g_type_class_add_private (klass, sizeof(ANJUTA_TYPE_DESIGN_DOCUMENT));
100 g_object_class_install_property (object_class,
101 PROP_PLUGIN,
102 g_param_spec_object ("plugin",
105 G_TYPE_OBJECT,
106 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
109 GtkWidget*
110 anjuta_design_document_new (GladePlugin* glade_plugin, GladeProject* project)
112 GObject* doc = g_object_new(ANJUTA_TYPE_DESIGN_DOCUMENT,
113 "plugin", glade_plugin,
114 "project", project,
115 NULL);
116 g_signal_connect (project, "changed", G_CALLBACK (on_project_changed), doc);
118 return GTK_WIDGET (doc);
121 static void ifile_open(IAnjutaFile* ifile, GFile* file, GError **e)
123 AnjutaDesignDocument* self = ANJUTA_DESIGN_DOCUMENT(ifile);
124 AnjutaDesignDocumentPrivate* priv = ADD_GET_PRIVATE(self);
126 ianjuta_file_open(IANJUTA_FILE(priv->glade_plugin), file, e);
129 static GFile* ifile_get_file(IAnjutaFile* ifile, GError **e)
131 AnjutaDesignDocument* self = ANJUTA_DESIGN_DOCUMENT(ifile);
133 GladeProject* project = glade_design_view_get_project(GLADE_DESIGN_VIEW(self));
135 const gchar* path = glade_project_get_path(project);
136 if (path != NULL)
137 return g_file_new_for_path (path);
138 else
139 return NULL;
143 static void
144 ifile_iface_init(IAnjutaFileIface *iface)
146 iface->open = ifile_open;
147 iface->get_file = ifile_get_file;
150 static void ifile_savable_save (IAnjutaFileSavable* file, GError **e)
152 AnjutaDesignDocument* self = ANJUTA_DESIGN_DOCUMENT(file);
153 AnjutaDesignDocumentPrivate* priv = ADD_GET_PRIVATE(self);
155 GladeProject* project = glade_design_view_get_project(GLADE_DESIGN_VIEW(self));
157 if (glade_project_get_path(project) != NULL)
159 AnjutaStatus *status;
161 status = anjuta_shell_get_status (ANJUTA_PLUGIN(priv->glade_plugin)->shell, NULL);
163 if (glade_project_save (project, glade_project_get_path(project),
164 NULL))
166 gchar *name;
167 GFile *file;
169 name = glade_project_get_name (project);
170 anjuta_status_set (status, _("Glade project '%s' saved"), name);
171 g_free (name);
172 g_signal_emit_by_name(G_OBJECT(self), "update-save-ui", TRUE);
174 file = g_file_new_for_path (glade_project_get_path(project));
175 g_signal_emit_by_name(G_OBJECT(self), "saved", file);
176 g_object_unref (file);
178 else
180 anjuta_util_dialog_warning (GTK_WINDOW (ANJUTA_PLUGIN(priv->glade_plugin)->shell),
181 _("Invalid Glade file name"));
182 g_signal_emit_by_name(G_OBJECT(self), "saved", NULL);
184 return;
186 DEBUG_PRINT("%s", "Invalid use of ifile_savable_save!");
189 static void ifile_savable_save_as(IAnjutaFileSavable* ifile, GFile* file, GError **e)
191 AnjutaDesignDocument* self = ANJUTA_DESIGN_DOCUMENT(ifile);
192 AnjutaDesignDocumentPrivate* priv = ADD_GET_PRIVATE(self);
194 GladeProject* project = glade_design_view_get_project(GLADE_DESIGN_VIEW(self));
196 AnjutaStatus *status = anjuta_shell_get_status (ANJUTA_PLUGIN(priv->glade_plugin)->shell, NULL);
198 if (glade_project_save (project, g_file_get_path (file),
199 NULL))
201 anjuta_status_set (status, _("Glade project '%s' saved"),
202 glade_project_get_name(project));
203 g_signal_emit_by_name(G_OBJECT(self), "save_point", TRUE);
205 else
207 anjuta_util_dialog_warning (GTK_WINDOW (ANJUTA_PLUGIN(priv->glade_plugin)->shell),
208 _("Invalid Glade file name"));
210 return;
213 static void ifile_savable_set_dirty(IAnjutaFileSavable* file, gboolean dirty, GError **e)
215 /* FIXME */
218 static gboolean ifile_savable_is_dirty(IAnjutaFileSavable* file, GError **e)
220 AnjutaDesignDocument* self = ANJUTA_DESIGN_DOCUMENT(file);
222 GladeProject* project = glade_design_view_get_project(GLADE_DESIGN_VIEW(self));
223 if (project == NULL)
224 return FALSE;
225 if (glade_project_get_modified (project))
227 return TRUE;
229 else
231 return FALSE;
235 static gboolean
236 ifile_savable_is_read_only (IAnjutaFileSavable* savable, GError** e)
238 // FIXME
239 return FALSE;
242 static gboolean
243 ifile_savable_is_conflict (IAnjutaFileSavable* savable, GError** e)
245 return FALSE;
248 static void
249 ifile_savable_iface_init(IAnjutaFileSavableIface *iface)
251 iface->save = ifile_savable_save;
252 iface->save_as = ifile_savable_save_as;
253 iface->set_dirty = ifile_savable_set_dirty;
254 iface->is_dirty = ifile_savable_is_dirty;
255 iface->is_read_only = ifile_savable_is_read_only;
256 iface->is_conflict = ifile_savable_is_conflict;
259 /* Return true if editor can redo */
260 static gboolean idocument_can_redo(IAnjutaDocument *editor, GError **e)
262 AnjutaDesignDocument* self = ANJUTA_DESIGN_DOCUMENT(editor);
263 GladeCommand *redo_item;
265 GladeProject* project = glade_design_view_get_project(GLADE_DESIGN_VIEW(self));
266 if (!project)
268 redo_item = NULL;
270 else
272 redo_item = glade_project_next_redo_item(project);
274 return (redo_item != NULL);
277 /* Return true if editor can undo */
278 static gboolean idocument_can_undo(IAnjutaDocument *editor, GError **e)
280 AnjutaDesignDocument* self = ANJUTA_DESIGN_DOCUMENT(editor);
281 GladeCommand *undo_item;
282 GladeProject* project = glade_design_view_get_project(GLADE_DESIGN_VIEW(self));
283 if (!project)
285 undo_item = NULL;
287 else
289 undo_item = glade_project_next_undo_item(project);
292 return (undo_item != NULL);
295 /* Return true if editor can undo */
296 static void idocument_begin_undo_action (IAnjutaDocument *editor, GError **e)
298 /* FIXME */
301 /* Return true if editor can undo */
302 static void idocument_end_undo_action (IAnjutaDocument *editor, GError **e)
304 /* FIXME */
308 static void
309 idocument_undo(IAnjutaDocument* edit, GError** ee)
311 GladeDesignView* view = GLADE_DESIGN_VIEW (edit);
312 glade_project_undo (glade_design_view_get_project (view));
315 static void
316 idocument_redo(IAnjutaDocument* edit, GError** ee)
318 GladeDesignView* view = GLADE_DESIGN_VIEW (edit);
319 glade_project_redo (glade_design_view_get_project (view));
322 /* Grab focus */
323 static void idocument_grab_focus (IAnjutaDocument *editor, GError **e)
325 gtk_widget_grab_focus (GTK_WIDGET(editor));
328 /* Return the opened filename */
329 static const gchar* idocument_get_filename(IAnjutaDocument *editor, GError **e)
331 AnjutaDesignDocument* self = ANJUTA_DESIGN_DOCUMENT(editor);
332 GladeProject* project = glade_design_view_get_project(GLADE_DESIGN_VIEW(self));
333 return glade_project_get_name(project);
336 static void
337 idocument_cut(IAnjutaDocument* edit, GError** ee)
339 GladeDesignView* view = GLADE_DESIGN_VIEW (edit);
340 glade_project_command_cut (glade_design_view_get_project (view));
343 static void
344 idocument_copy(IAnjutaDocument* edit, GError** ee)
346 GladeDesignView* view = GLADE_DESIGN_VIEW (edit);
347 glade_project_copy_selection (glade_design_view_get_project (view));
350 static void
351 idocument_paste(IAnjutaDocument* edit, GError** ee)
353 GladeDesignView* view = GLADE_DESIGN_VIEW (edit);
354 glade_project_command_paste (glade_design_view_get_project (view), NULL);
357 static void
358 idocument_clear(IAnjutaDocument* edit, GError** ee)
360 GladeDesignView* view = GLADE_DESIGN_VIEW (edit);
361 glade_project_command_delete (glade_design_view_get_project (view));
366 static void
367 idocument_iface_init (IAnjutaDocumentIface *iface)
369 iface->grab_focus = idocument_grab_focus;
370 iface->get_filename = idocument_get_filename;
371 iface->can_undo = idocument_can_undo;
372 iface->can_redo = idocument_can_redo;
373 iface->begin_undo_action = idocument_begin_undo_action;
374 iface->end_undo_action = idocument_end_undo_action;
375 iface->undo = idocument_undo;
376 iface->redo = idocument_redo;
377 iface->cut = idocument_cut;
378 iface->copy = idocument_copy;
379 iface->paste = idocument_paste;
380 iface->clear = idocument_clear;
383 ANJUTA_TYPE_BEGIN (AnjutaDesignDocument, anjuta_design_document, GLADE_TYPE_DESIGN_VIEW);
384 ANJUTA_TYPE_ADD_INTERFACE(idocument, IANJUTA_TYPE_DOCUMENT);
385 ANJUTA_TYPE_ADD_INTERFACE(ifile, IANJUTA_TYPE_FILE);
386 ANJUTA_TYPE_ADD_INTERFACE(ifile_savable, IANJUTA_TYPE_FILE_SAVABLE);
387 ANJUTA_TYPE_END;