1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Johannes Schmid 2007 <jhs@gnome.org>
6 * anjuta is free software.
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)
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 #include <libgnomevfs/gnome-vfs-utils.h>
40 struct _AnjutaDesignDocumentPrivate
42 GladePlugin
* glade_plugin
;
45 #define ADD_GET_PRIVATE(o) \
46 (G_TYPE_INSTANCE_GET_PRIVATE ((o), ANJUTA_TYPE_DESIGN_DOCUMENT, AnjutaDesignDocumentPrivate))
49 anjuta_design_document_instance_init (AnjutaDesignDocument
*object
)
55 anjuta_design_document_set_property (GObject
*object
, guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
57 g_return_if_fail (ANJUTA_IS_DESIGN_DOCUMENT (object
));
58 AnjutaDesignDocumentPrivate
* priv
= ADD_GET_PRIVATE(object
);
63 priv
->glade_plugin
= g_value_get_object(value
);
66 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
72 anjuta_design_document_get_property (GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
74 g_return_if_fail (ANJUTA_IS_DESIGN_DOCUMENT (object
));
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
86 anjuta_design_document_class_init (AnjutaDesignDocumentClass
*klass
)
88 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
90 object_class
->set_property
= anjuta_design_document_set_property
;
91 object_class
->get_property
= anjuta_design_document_get_property
;
93 g_type_class_add_private (klass
, sizeof(ANJUTA_TYPE_DESIGN_DOCUMENT
));
95 g_object_class_install_property (object_class
,
97 g_param_spec_object ("plugin",
101 G_PARAM_WRITABLE
| G_PARAM_CONSTRUCT_ONLY
));
105 anjuta_design_document_new (GladePlugin
* glade_plugin
, GladeProject
* project
)
107 return GTK_WIDGET(g_object_new(ANJUTA_TYPE_DESIGN_DOCUMENT
,
108 "plugin", glade_plugin
,
113 static void ifile_open(IAnjutaFile
* file
, const gchar
* uri
, GError
**e
)
115 AnjutaDesignDocument
* self
= ANJUTA_DESIGN_DOCUMENT(file
);
116 AnjutaDesignDocumentPrivate
* priv
= ADD_GET_PRIVATE(self
);
118 ianjuta_file_open(IANJUTA_FILE(priv
->glade_plugin
), uri
, e
);
121 static gchar
* ifile_get_uri(IAnjutaFile
* file
, GError
**e
)
123 AnjutaDesignDocument
* self
= ANJUTA_DESIGN_DOCUMENT(file
);
125 GladeProject
* project
= glade_design_view_get_project(GLADE_DESIGN_VIEW(self
));
127 #if (GLADEUI_VERSION >= 330)
128 const gchar
* path
= glade_project_get_path(project
);
130 return gnome_vfs_get_uri_from_local_path(path
);
134 if (project
&& project
->path
)
135 return gnome_vfs_get_uri_from_local_path(project
->path
);
143 ifile_iface_init(IAnjutaFileIface
*iface
)
145 iface
->open
= ifile_open
;
146 iface
->get_uri
= ifile_get_uri
;
149 static void ifile_savable_save (IAnjutaFileSavable
* file
, GError
**e
)
151 AnjutaDesignDocument
* self
= ANJUTA_DESIGN_DOCUMENT(file
);
152 AnjutaDesignDocumentPrivate
* priv
= ADD_GET_PRIVATE(self
);
154 GladeProject
* project
= glade_design_view_get_project(GLADE_DESIGN_VIEW(self
));
156 #if (GLADEUI_VERSION >= 330)
157 if (glade_project_get_path(project
) != NULL
) {
159 if (project
&& project
->path
!= NULL
) {
161 AnjutaStatus
*status
;
163 status
= anjuta_shell_get_status (ANJUTA_PLUGIN(priv
->glade_plugin
)->shell
, NULL
);
165 #if (GLADEUI_VERSION >= 330)
166 if (glade_project_save (project
, glade_project_get_path(project
),
168 anjuta_status_set (status
, _("Glade project '%s' saved"),
169 glade_project_get_name(project
));
171 if (glade_project_save (project
, project
->path
, NULL
)) {
172 anjuta_status_set (status
, _("Glade project '%s' saved"),
175 g_signal_emit_by_name(G_OBJECT(self
), "save_point", TRUE
);
179 anjuta_util_dialog_warning (GTK_WINDOW (ANJUTA_PLUGIN(priv
->glade_plugin
)->shell
),
180 _("Invalid glade file name"));
184 DEBUG_PRINT("Invalid use of ifile_savable_save!");
187 static void ifile_savable_save_as(IAnjutaFileSavable
* file
, const gchar
* uri
, GError
**e
)
189 AnjutaDesignDocument
* self
= ANJUTA_DESIGN_DOCUMENT(file
);
190 AnjutaDesignDocumentPrivate
* priv
= ADD_GET_PRIVATE(self
);
192 GladeProject
* project
= glade_design_view_get_project(GLADE_DESIGN_VIEW(self
));
194 AnjutaStatus
*status
= anjuta_shell_get_status (ANJUTA_PLUGIN(priv
->glade_plugin
)->shell
, NULL
);
196 #if (GLADEUI_VERSION >= 330)
197 if (glade_project_save (project
, gnome_vfs_get_local_path_from_uri(uri
),
200 anjuta_status_set (status
, _("Glade project '%s' saved"),
201 glade_project_get_name(project
));
203 if (glade_project_save (project
, gnome_vfs_get_local_path_from_uri(uri
), NULL
))
205 anjuta_status_set (status
, _("Glade project '%s' saved"),
208 g_signal_emit_by_name(G_OBJECT(self
), "save_point", TRUE
);
212 anjuta_util_dialog_warning (GTK_WINDOW (ANJUTA_PLUGIN(priv
->glade_plugin
)->shell
),
213 _("Invalid glade file name"));
218 static void ifile_savable_set_dirty(IAnjutaFileSavable
* file
, gboolean dirty
, GError
**e
)
223 static gboolean
ifile_savable_is_dirty(IAnjutaFileSavable
* file
, GError
**e
)
225 AnjutaDesignDocument
* self
= ANJUTA_DESIGN_DOCUMENT(file
);
227 GladeProject
* project
= glade_design_view_get_project(GLADE_DESIGN_VIEW(self
));
230 #if (GLADEUI_VERSION >= 330)
231 # if (GLADEUI_VERSION >= 331)
232 if (glade_project_get_modified (project
))
234 if (glade_project_get_has_unsaved_changes (project
))
237 if (project
->changed
)
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
;
257 /* Return true if editor can redo */
258 static gboolean
idocument_can_redo(IAnjutaDocument
*editor
, GError
**e
)
260 AnjutaDesignDocument
* self
= ANJUTA_DESIGN_DOCUMENT(editor
);
261 #if (GLADEUI_VERSION >= 330)
262 GladeCommand
*redo_item
;
264 GList
*prev_redo_item
;
267 const gchar
*redo_description
= NULL
;
269 GladeProject
* project
= glade_design_view_get_project(GLADE_DESIGN_VIEW(self
));
276 #if (GLADEUI_VERSION >= 330)
277 redo_item
= glade_project_next_redo_item(project
);
279 redo_description
= redo_item
->description
;
281 prev_redo_item
= project
->prev_redo_item
;
282 redo_item
= (prev_redo_item
== NULL
) ? project
->undo_stack
: prev_redo_item
->next
;
283 if (redo_item
&& redo_item
->data
)
284 redo_description
= GLADE_COMMAND (redo_item
->data
)->description
;
287 return (redo_description
!= NULL
);
290 /* Return true if editor can undo */
291 static gboolean
idocument_can_undo(IAnjutaDocument
*editor
, GError
**e
)
293 AnjutaDesignDocument
* self
= ANJUTA_DESIGN_DOCUMENT(editor
);
294 #if (GLADEUI_VERSION >= 330)
295 GladeCommand
*undo_item
;
297 GList
*prev_redo_item
;
300 const gchar
*undo_description
= NULL
;
301 GladeProject
* project
= glade_design_view_get_project(GLADE_DESIGN_VIEW(self
));
308 #if (GLADEUI_VERSION >= 330)
309 undo_item
= glade_project_next_undo_item(project
);
311 undo_description
= undo_item
->description
;
313 undo_item
= prev_redo_item
= project
->prev_redo_item
;
314 if (undo_item
&& undo_item
->data
)
315 undo_description
= GLADE_COMMAND (undo_item
->data
)->description
;
318 return (undo_description
!= NULL
);
321 /* Return true if editor can undo */
322 static void idocument_begin_undo_action (IAnjutaDocument
*editor
, GError
**e
)
327 /* Return true if editor can undo */
328 static void idocument_end_undo_action (IAnjutaDocument
*editor
, GError
**e
)
335 idocument_undo(IAnjutaDocument
* edit
, GError
** ee
)
337 glade_app_command_undo();
341 idocument_redo(IAnjutaDocument
* edit
, GError
** ee
)
343 glade_app_command_redo();
347 static void idocument_grab_focus (IAnjutaDocument
*editor
, GError
**e
)
349 gtk_widget_grab_focus (GTK_WIDGET(editor
));
352 /* Return the opened filename */
353 static const gchar
* idocument_get_filename(IAnjutaDocument
*editor
, GError
**e
)
355 AnjutaDesignDocument
* self
= ANJUTA_DESIGN_DOCUMENT(editor
);
356 GladeProject
* project
= glade_design_view_get_project(GLADE_DESIGN_VIEW(self
));
357 #if (GLADEUI_VERSION >= 330)
358 return glade_project_get_name(project
);
361 return project
->name
;
368 idocument_cut(IAnjutaDocument
* edit
, GError
** ee
)
370 glade_app_command_cut();
374 idocument_copy(IAnjutaDocument
* edit
, GError
** ee
)
376 glade_app_command_copy();
380 idocument_paste(IAnjutaDocument
* edit
, GError
** ee
)
382 glade_app_command_paste(NULL
);
386 idocument_clear(IAnjutaDocument
* edit
, GError
** ee
)
388 glade_app_command_delete();
393 idocument_iface_init (IAnjutaDocumentIface
*iface
)
395 iface
->grab_focus
= idocument_grab_focus
;
396 iface
->get_filename
= idocument_get_filename
;
397 iface
->can_undo
= idocument_can_undo
;
398 iface
->can_redo
= idocument_can_redo
;
399 iface
->begin_undo_action
= idocument_begin_undo_action
;
400 iface
->end_undo_action
= idocument_end_undo_action
;
401 iface
->undo
= idocument_undo
;
402 iface
->redo
= idocument_redo
;
403 iface
->cut
= idocument_cut
;
404 iface
->copy
= idocument_copy
;
405 iface
->paste
= idocument_paste
;
406 iface
->clear
= idocument_clear
;
409 ANJUTA_TYPE_BEGIN (AnjutaDesignDocument
, anjuta_design_document
, GLADE_TYPE_DESIGN_VIEW
);
410 ANJUTA_TYPE_ADD_INTERFACE(idocument
, IANJUTA_TYPE_DOCUMENT
);
411 ANJUTA_TYPE_ADD_INTERFACE(ifile
, IANJUTA_TYPE_FILE
);
412 ANJUTA_TYPE_ADD_INTERFACE(ifile_savable
, IANJUTA_TYPE_FILE_SAVABLE
);