1 /*   EXTRAITS DE LA LICENCE
2 	Copyright CEA, contributeurs : Luc BILLARD et Damien
3 	CALISTE, laboratoire L_Sim, (2001-2005)
4 
5 	Adresse mèl :
6 	BILLARD, non joignable par mèl ;
7 	CALISTE, damien P caliste AT cea P fr.
8 
9 	Ce logiciel est un programme informatique servant à visualiser des
10 	structures atomiques dans un rendu pseudo-3D.
11 
12 	Ce logiciel est régi par la licence CeCILL soumise au droit français et
13 	respectant les principes de diffusion des logiciels libres. Vous pouvez
14 	utiliser, modifier et/ou redistribuer ce programme sous les conditions
15 	de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
16 	sur le site "http://www.cecill.info".
17 
18 	Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
19 	pris connaissance de la licence CeCILL, et que vous en avez accepté les
20 	termes (cf. le fichier Documentation/licence.fr.txt fourni avec ce logiciel).
21 */
22 
23 /*   LICENCE SUM UP
24 	Copyright CEA, contributors : Luc BILLARD et Damien
25 	CALISTE, laboratoire L_Sim, (2001-2005)
26 
27 	E-mail address:
28 	BILLARD, not reachable any more ;
29 	CALISTE, damien P caliste AT cea P fr.
30 
31 	This software is a computer program whose purpose is to visualize atomic
32 	configurations in 3D.
33 
34 	This software is governed by the CeCILL  license under French law and
35 	abiding by the rules of distribution of free software.  You can  use,
36 	modify and/ or redistribute the software under the terms of the CeCILL
37 	license as circulated by CEA, CNRS and INRIA at the following URL
38 	"http://www.cecill.info".
39 
40 	The fact that you are presently reading this means that you have had
41 	knowledge of the CeCILL license and that you accept its terms. You can
42 	find a copy of this licence shipped with this software at Documentation/licence.en.txt.
43 */
44 #include <gtk/gtk.h>
45 
46 #include "gtk_valueIOWidget.h"
47 #include <support.h>
48 #include <visu_tools.h>
49 #include <visu_gtk.h>
50 #include <gtk_main.h>
51 
52 /**
53  * SECTION:gtk_valueIOWidget
54  * @short_description: Defines a specialised #GtkHBox with three
55  * button to open, save and save as XML value files.
56  *
57  * <para></para>
58  *
59  * Since: 3.5
60  */
61 
62 enum
63   {
64     PROP_0,
65     SAVE_SENSITIVE_PROP,
66     N_PROP
67   };
68 static GParamSpec *properties[N_PROP];
69 
70 static void visu_ui_value_io_dispose (GObject *obj);
71 static void visu_ui_value_io_finalize(GObject *obj);
72 static void visu_ui_value_io_get_property(GObject* obj, guint property_id,
73                                           GValue *value, GParamSpec *pspec);
74 static void visu_ui_value_io_set_property(GObject* obj, guint property_id,
75                                           const GValue *value, GParamSpec *pspec);
76 
77 /**
78  * VisuUiValueIo:
79  *
80  * Private structure to store informations of a #VisuUiValueIo object.
81  *
82  * Since: 3.5
83  */
84 struct _VisuUiValueIo
85 {
86   GtkBox hbox;
87 
88   GtkWidget *btExportOpen, *btExportSave, *btExportSaveAs;
89 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 12
90   GtkWidget *dialog;
91 #endif
92   GtkWindow *parent;
93 
94   VisuUiValueIoCallback ioOpen;
95   gulong openSignalId;
96   VisuUiValueIoCallback ioSave;
97   gulong saveSignalId;
98   gulong saveAsSignalId;
99 
100   /* Memory gestion. */
101   gboolean dispose_has_run;
102 };
103 /**
104  * VisuUiValueIoClass
105  *
106  * Private structure to store informations of a #VisuUiValueIoClass object.
107  *
108  * Since: 3.5
109  */
110 struct _VisuUiValueIoClass
111 {
112   GtkBoxClass parent_class;
113 
114   void (*valueio) (VisuUiValueIo *hbox);
115 };
116 
117 /* Local callbacks. */
118 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
119 static void onImportXML(GtkFileChooserButton *filechooserbutton, gpointer data);
120 #else
121 static void onImportXML(GtkDialog *dialog, gint response, gpointer data);
122 #endif
123 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 6
124 static void onOpenXMLClicked(GtkButton *bt, gpointer data);
125 #endif
126 static void onExportXML(GtkButton *button, gpointer data);
127 
128 /* Local methods. */
129 
130 /**
131  * visu_ui_value_io_get_type
132  *
133  * #GType are unique numbers to identify objects.
134  *
135  * Returns: the #GType associated with #VisuUiValueIo objects.
136  *
137  * Since: 3.5
138  */
G_DEFINE_TYPE(VisuUiValueIo,visu_ui_value_io,GTK_TYPE_BOX)139 G_DEFINE_TYPE(VisuUiValueIo, visu_ui_value_io, GTK_TYPE_BOX)
140 
141 static void visu_ui_value_io_class_init(VisuUiValueIoClass *klass)
142 {
143   DBG_fprintf(stderr, "Gtk VisuUiValueIo: creating the class of the widget.\n");
144 
145   /* Connect freeing methods. */
146   G_OBJECT_CLASS(klass)->dispose = visu_ui_value_io_dispose;
147   G_OBJECT_CLASS(klass)->finalize = visu_ui_value_io_finalize;
148   G_OBJECT_CLASS(klass)->set_property = visu_ui_value_io_set_property;
149   G_OBJECT_CLASS(klass)->get_property = visu_ui_value_io_get_property;
150 
151   /**
152    * VisuUiValueIo::sensitive-save:
153    *
154    * The save and save-as buttons sensitivity.
155    *
156    * Since: 3.8
157    */
158   properties[SAVE_SENSITIVE_PROP] = g_param_spec_boolean("sensitive-save", "Sensitive save",
159                                                          "sensitivity of save buttons",
160                                                          FALSE, G_PARAM_READWRITE |
161                                                          G_PARAM_STATIC_STRINGS);
162   g_object_class_install_property(G_OBJECT_CLASS(klass), SAVE_SENSITIVE_PROP,
163 				  properties[SAVE_SENSITIVE_PROP]);
164 }
165 
visu_ui_value_io_dispose(GObject * obj)166 static void visu_ui_value_io_dispose(GObject *obj)
167 {
168   DBG_fprintf(stderr, "Gtk VisuUiValueIo: dispose object %p.\n", (gpointer)obj);
169 
170   if (VISU_UI_VALUE_IO(obj)->dispose_has_run)
171     return;
172 
173   VISU_UI_VALUE_IO(obj)->dispose_has_run = TRUE;
174   /* Chain up to the parent class */
175   G_OBJECT_CLASS(visu_ui_value_io_parent_class)->dispose(obj);
176 }
visu_ui_value_io_finalize(GObject * obj)177 static void visu_ui_value_io_finalize(GObject *obj)
178 {
179   g_return_if_fail(obj);
180 
181   DBG_fprintf(stderr, "Gtk VisuUiValueIo: finalize object %p.\n", (gpointer)obj);
182 
183   /* Chain up to the parent class */
184   G_OBJECT_CLASS(visu_ui_value_io_parent_class)->finalize(obj);
185 
186   DBG_fprintf(stderr, " | freeing ... OK.\n");
187 }
visu_ui_value_io_get_property(GObject * obj,guint property_id,GValue * value,GParamSpec * pspec)188 static void visu_ui_value_io_get_property(GObject* obj, guint property_id,
189                                           GValue *value, GParamSpec *pspec)
190 {
191   VisuUiValueIo *self = VISU_UI_VALUE_IO(obj);
192 
193   DBG_fprintf(stderr, "Gtk VisuUiValueIo: get property '%s'.\n",
194 	      g_param_spec_get_name(pspec));
195   switch (property_id)
196     {
197     case SAVE_SENSITIVE_PROP:
198       g_value_set_boolean(value, gtk_widget_get_sensitive(self->btExportSaveAs));
199       break;
200     default:
201       /* We don't have any other property... */
202       G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, property_id, pspec);
203       break;
204     }
205 }
visu_ui_value_io_set_property(GObject * obj,guint property_id,const GValue * value,GParamSpec * pspec)206 static void visu_ui_value_io_set_property(GObject* obj, guint property_id,
207                                           const GValue *value, GParamSpec *pspec)
208 {
209   VisuUiValueIo *self = VISU_UI_VALUE_IO(obj);
210 
211   DBG_fprintf(stderr, "Gtk VisuUiValueIo: set property '%s'.\n",
212 	      g_param_spec_get_name(pspec));
213   switch (property_id)
214     {
215     case SAVE_SENSITIVE_PROP:
216       visu_ui_value_io_setSensitiveSave(self, g_value_get_boolean(value));
217       break;
218     default:
219       /* We don't have any other property... */
220       G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, property_id, pspec);
221       break;
222     }
223 }
224 
225 
visu_ui_value_io_init(VisuUiValueIo * valueio)226 static void visu_ui_value_io_init(VisuUiValueIo *valueio)
227 {
228   DBG_fprintf(stderr, "Gtk VisuUiValueIo: initializing new object (%p).\n",
229 	      (gpointer)valueio);
230 
231   valueio->ioOpen         = (VisuUiValueIoCallback)0;
232   valueio->openSignalId   = 0;
233   valueio->ioSave         = (VisuUiValueIoCallback)0;
234   valueio->saveSignalId   = 0;
235   valueio->saveAsSignalId = 0;
236 }
237 
buildWidgets(VisuUiValueIo * valueio,GtkWindow * parent,const gchar * tipsOpen,const gchar * tipsSave,const gchar * tipsSaveAs)238 static void buildWidgets(VisuUiValueIo *valueio, GtkWindow *parent,
239 			 const gchar*tipsOpen, const gchar*tipsSave,
240 			 const gchar*tipsSaveAs)
241 {
242   GtkFileFilter *filter1, *filter2;
243   const gchar *directory;
244 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 12
245   GtkTooltips *tooltips;
246 
247   tooltips = gtk_tooltips_new();
248 #endif
249 
250   gtk_box_set_spacing(GTK_BOX(valueio), 2);
251 
252   valueio->parent = parent;
253 
254   /* The save as button. */
255   valueio->btExportSaveAs = gtk_button_new();
256   gtk_widget_set_tooltip_text(valueio->btExportSaveAs, tipsSaveAs);
257   gtk_widget_set_sensitive(valueio->btExportSaveAs, FALSE);
258   gtk_container_add(GTK_CONTAINER(valueio->btExportSaveAs),
259 		    gtk_image_new_from_icon_name("document-save-as", GTK_ICON_SIZE_MENU));
260   gtk_box_pack_end(GTK_BOX(valueio), valueio->btExportSaveAs, FALSE, FALSE, 0);
261 
262 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
263   /* The save button. */
264   valueio->btExportSave = gtk_button_new();
265   gtk_widget_set_tooltip_text(valueio->btExportSave, tipsSave);
266   gtk_widget_set_sensitive(valueio->btExportSave, FALSE);
267   gtk_container_add(GTK_CONTAINER(valueio->btExportSave),
268 		    gtk_image_new_from_icon_name("document-save", GTK_ICON_SIZE_MENU));
269   gtk_box_pack_end(GTK_BOX(valueio), valueio->btExportSave, FALSE, FALSE, 0);
270 #endif
271 
272   /* The open button. */
273   filter1 = gtk_file_filter_new();
274   gtk_file_filter_set_name(filter1, _("V_Sim value file (*.xml)"));
275   gtk_file_filter_add_pattern(filter1, "*.xml");
276   filter2 = gtk_file_filter_new ();
277   gtk_file_filter_set_name(filter2, _("All files"));
278   gtk_file_filter_add_pattern (filter2, "*");
279 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
280   valueio->btExportOpen = gtk_file_chooser_button_new(_("Open a V_Sim value file"),
281 						      GTK_FILE_CHOOSER_ACTION_OPEN);
282   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(valueio->btExportOpen), filter1);
283   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(valueio->btExportOpen), filter2);
284 #else
285   valueio->dialog = gtk_file_chooser_dialog_new
286     (_("Open a V_Sim value file"), parent,
287      GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
288      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
289   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(valueio->dialog), filter1);
290   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(valueio->dialog), filter2);
291 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
292   valueio->btExportOpen = gtk_file_chooser_button_new_with_dialog(valueio->dialog);
293 #else
294   valueio->btExportOpen = gtk_button_new_from_stock(GTK_STOCK_OPEN);
295 #endif
296 #endif
297 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
298   directory = visu_ui_main_getLastOpenDirectory(visu_ui_main_class_getCurrentPanel());
299   if (directory)
300     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(valueio->btExportOpen),
301 					directory);
302 #endif
303   gtk_widget_set_tooltip_text(valueio->btExportOpen, tipsOpen);
304   gtk_widget_set_sensitive(valueio->btExportOpen, FALSE);
305   gtk_box_pack_end(GTK_BOX(valueio), valueio->btExportOpen, TRUE, TRUE, 0);
306 
307   /* The label. */
308   gtk_box_pack_end(GTK_BOX(valueio), gtk_label_new(_("I/O:")), FALSE, FALSE, 0);
309 }
310 
311 /**
312  * visu_ui_value_io_new:
313  * @parent: the parent used to show the file dialog.
314  * @tipsOpen: a tooltip to show on open button.
315  * @tipsSave: a tooltip to show on save button.
316  * @tipsSaveAs: a tooltip to show on save-as button.
317  *
318  * A #VisuUiValueIo widget is like a #GtkComboBox widget, but it is already filled
319  * with predefined line patterns (call stipple). Using this widget is
320  * a convienient way to share stipples between all part of V_Sim and
321  * to give a consistent look of all stipple selection.
322  *
323  * Since: 3.5
324  *
325  * Returns: (transfer full): a newly created #VisuUiValueIo widget.
326  */
visu_ui_value_io_new(GtkWindow * parent,const gchar * tipsOpen,const gchar * tipsSave,const gchar * tipsSaveAs)327 GtkWidget* visu_ui_value_io_new(GtkWindow *parent, const gchar*tipsOpen,
328                                 const gchar*tipsSave, const gchar*tipsSaveAs)
329 {
330   VisuUiValueIo *valueio;
331 
332   DBG_fprintf(stderr, "Gtk VisuUiValueIo: creating new object.\n");
333 
334   valueio = VISU_UI_VALUE_IO(g_object_new(visu_ui_value_io_get_type (), NULL));
335 
336   DBG_fprintf(stderr, "Gtk VisuUiValueIo: build widgets.\n");
337 
338   buildWidgets(valueio, parent, tipsOpen, tipsSave, tipsSaveAs);
339 
340   return GTK_WIDGET(valueio);
341 }
342 /**
343  * visu_ui_value_io_connectOnOpen:
344  * @valueio: a #VisuUiValueIo widget.
345  * @open: a method.
346  *
347  * Set the function to call when the open button is clicked.
348  *
349  * Since: 3.5
350  */
visu_ui_value_io_connectOnOpen(VisuUiValueIo * valueio,VisuUiValueIoCallback open)351 void visu_ui_value_io_connectOnOpen(VisuUiValueIo *valueio, VisuUiValueIoCallback open)
352 {
353   g_return_if_fail(VISU_IS_UI_VALUE_IO(valueio));
354 
355   if (valueio->openSignalId)
356     {
357 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
358 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
359       g_signal_handler_disconnect(G_OBJECT(valueio->btExportOpen),
360 				  valueio->openSignalId);
361 #else
362       g_signal_handler_disconnect(G_OBJECT(valueio->dialog),
363 				  valueio->openSignalId);
364 #endif
365 #else
366       g_signal_handler_disconnect(G_OBJECT(valueio->btExportOpen),
367 				  valueio->openSignalId);
368 #endif
369     }
370 
371   valueio->ioOpen = open;
372 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
373 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
374   valueio->openSignalId =
375     g_signal_connect(G_OBJECT(valueio->btExportOpen), "file-set",
376 		     G_CALLBACK(onImportXML), (gpointer)valueio);
377 #else
378   valueio->openSignalId =
379     g_signal_connect(G_OBJECT(valueio->dialog), "response",
380 		     G_CALLBACK(onImportXML), (gpointer)valueio);
381 #endif
382 #else
383   valueio->openSignalId =
384     g_signal_connect(G_OBJECT(valueio->btExportOpen), "clicked",
385 		     G_CALLBACK(onOpenXMLClicked), (gpointer)valueio);
386 #endif
387 }
388 /**
389  * visu_ui_value_io_connectOnSave:
390  * @valueio: a #VisuUiValueIo widget.
391  * @save: a method.
392  *
393  * Set the function to call when the save or save-as button is clicked.
394  *
395  * Since: 3.5
396  */
visu_ui_value_io_connectOnSave(VisuUiValueIo * valueio,VisuUiValueIoCallback save)397 void visu_ui_value_io_connectOnSave(VisuUiValueIo *valueio, VisuUiValueIoCallback save)
398 {
399   g_return_if_fail(VISU_IS_UI_VALUE_IO(valueio));
400 
401 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
402   if (valueio->saveSignalId)
403     g_signal_handler_disconnect(G_OBJECT(valueio->btExportSave),
404 				valueio->saveSignalId);
405 #endif
406   if (valueio->saveAsSignalId)
407     g_signal_handler_disconnect(G_OBJECT(valueio->btExportSaveAs),
408 				valueio->saveAsSignalId);
409 
410   valueio->ioSave = save;
411   valueio->saveAsSignalId =
412     g_signal_connect(G_OBJECT(valueio->btExportSaveAs), "clicked",
413 		     G_CALLBACK(onExportXML), (gpointer)valueio);
414 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
415   valueio->saveSignalId =
416     g_signal_connect(G_OBJECT(valueio->btExportSave), "clicked",
417 		     G_CALLBACK(onExportXML), (gpointer)valueio);
418 #endif
419 }
420 
loadXMLFile(gpointer data)421 static gboolean loadXMLFile(gpointer data)
422 {
423   gchar *filename;
424   GError *error;
425 
426   g_return_val_if_fail(VISU_IS_UI_VALUE_IO(data), FALSE);
427   g_return_val_if_fail(VISU_UI_VALUE_IO(data)->ioOpen, FALSE);
428 
429   filename = g_object_get_data(G_OBJECT(data), "filename");
430   g_return_val_if_fail(filename, FALSE);
431 
432   error = (GError*)0;
433   if (!VISU_UI_VALUE_IO(data)->ioOpen(filename, &error))
434     {
435       visu_ui_raiseWarning
436 	(_("Import V_Sim values from a file."),
437 	 error->message, VISU_UI_VALUE_IO(data)->parent);
438       g_error_free(error);
439 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
440       gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(VISU_UI_VALUE_IO(data)->btExportOpen));
441 #endif
442     }
443   g_free(filename);
444   g_object_set_data(G_OBJECT(data), "filename", (gpointer)0);
445 
446   return FALSE;
447 }
448 
449 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
onImportXML(GtkFileChooserButton * filechooserbutton,gpointer data)450 static void onImportXML(GtkFileChooserButton *filechooserbutton, gpointer data)
451 {
452   gchar *filename;
453 
454   filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filechooserbutton));
455   if (!filename)
456     return;
457 
458   g_object_set_data(G_OBJECT(data), "filename", filename);
459   g_idle_add(loadXMLFile, (gpointer)data);
460 }
461 #else
onImportXML(GtkDialog * dialog,gint response,gpointer data)462 static void onImportXML(GtkDialog *dialog, gint response, gpointer data)
463 {
464   gchar *filename;
465 
466   if (response != GTK_RESPONSE_ACCEPT)
467     return;
468 
469   filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
470 
471   g_object_set_data(G_OBJECT(data), "filename", filename);
472   g_idle_add(loadXMLFile, (gpointer)data);
473 }
474 #endif
475 #if GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION < 6
onOpenXMLClicked(GtkButton * bt _U_,gpointer data)476 static void onOpenXMLClicked(GtkButton *bt _U_, gpointer data)
477 {
478   gchar *filename;
479 
480   if (gtk_dialog_run(GTK_DIALOG(VISU_UI_VALUE_IO(data)->dialog)) != GTK_RESPONSE_ACCEPT)
481     return;
482 
483   filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(VISU_UI_VALUE_IO(data)->dialog));
484 
485   g_object_set_data(G_OBJECT(data), "filename", filename);
486   g_idle_add(loadXMLFile, (gpointer)data);
487 }
488 #endif
489 /**
490  * visu_ui_value_io_getFilename:
491  * @parent: a parent to display the dialog on.
492  *
493  * Open a save dialog window  with XML filter to choose the name of a
494  * file. This is the default action that can be connect to a #VisuUiValueIo
495  * widget using visu_ui_value_io_connectOnSave().
496  *
497  * Since: 3.5
498  *
499  * Returns: a filename that should be freed later with g_free() by the caller.
500  */
visu_ui_value_io_getFilename(GtkWindow * parent)501 gchar* visu_ui_value_io_getFilename(GtkWindow *parent)
502 {
503   GtkWidget *saveDialog;
504   gchar *filename;
505   const gchar *directory;
506   GtkFileFilter *filter;
507 
508   saveDialog = gtk_file_chooser_dialog_new
509     (_("Export V_Sim values to a file."), parent,
510      GTK_FILE_CHOOSER_ACTION_SAVE,
511      TOOL_ICON_CANCEL, GTK_RESPONSE_CANCEL,
512      TOOL_ICON_SAVE, GTK_RESPONSE_ACCEPT, NULL);
513   gtk_window_set_modal(GTK_WINDOW(saveDialog), TRUE);
514   gtk_window_set_transient_for(GTK_WINDOW(saveDialog), GTK_WINDOW(parent));
515   gtk_window_set_position(GTK_WINDOW(saveDialog), GTK_WIN_POS_CENTER_ON_PARENT);
516   directory = visu_ui_main_getLastOpenDirectory(visu_ui_main_class_getCurrentPanel());
517   if (directory)
518     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(saveDialog), directory);
519   gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(saveDialog), FALSE);
520   gtk_dialog_set_default_response(GTK_DIALOG(saveDialog), GTK_RESPONSE_ACCEPT);
521 
522   filter = gtk_file_filter_new ();
523   gtk_file_filter_set_name(filter, _("V_Sim value files (*.xml)"));
524   gtk_file_filter_add_pattern(filter, "*.xml");
525   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(saveDialog), filter);
526   filter = gtk_file_filter_new ();
527   gtk_file_filter_set_name(filter, _("All files"));
528   gtk_file_filter_add_pattern (filter, "*");
529   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(saveDialog), filter);
530 
531   /* A suggested filename. */
532   gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(saveDialog),
533 				    _("values.xml"));
534 
535   if (gtk_dialog_run(GTK_DIALOG(saveDialog)) != GTK_RESPONSE_ACCEPT)
536     {
537       gtk_widget_destroy(saveDialog);
538       return (gchar*)0;
539     }
540   filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(saveDialog));
541   gtk_widget_destroy(saveDialog);
542 
543   return filename;
544 }
545 
saveXMLFile(gpointer data)546 static gboolean saveXMLFile(gpointer data)
547 {
548   gchar *filename;
549   GError *error;
550 
551   g_return_val_if_fail(VISU_IS_UI_VALUE_IO(data), FALSE);
552   g_return_val_if_fail(VISU_UI_VALUE_IO(data)->ioSave, FALSE);
553 
554   filename = g_object_get_data(G_OBJECT(data), "filename");
555   g_return_val_if_fail(filename, FALSE);
556 
557   error = (GError*)0;
558   if (!VISU_UI_VALUE_IO(data)->ioSave(filename, &error))
559     {
560       visu_ui_raiseWarning
561 	(_("Export V_Sim values to a file."),
562 	 error->message, VISU_UI_VALUE_IO(data)->parent);
563       g_error_free(error);
564     }
565 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
566   else
567     {
568 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
569       g_signal_handler_block(G_OBJECT(VISU_UI_VALUE_IO(data)->btExportOpen),
570 			     VISU_UI_VALUE_IO(data)->openSignalId);
571 #endif
572       gtk_file_chooser_set_filename
573 	(GTK_FILE_CHOOSER(VISU_UI_VALUE_IO(data)->btExportOpen), filename);
574 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 11
575       g_signal_handler_unblock(G_OBJECT(VISU_UI_VALUE_IO(data)->btExportOpen),
576 			       VISU_UI_VALUE_IO(data)->openSignalId);
577 #endif
578     }
579 #endif
580   g_free(filename);
581   g_object_set_data(G_OBJECT(data), "filename", (gpointer)0);
582 
583   return FALSE;
584 }
585 
onExportXML(GtkButton * button,gpointer data)586 static void onExportXML(GtkButton *button, gpointer data)
587 {
588   gchar *filename;
589 
590   if (button == GTK_BUTTON(VISU_UI_VALUE_IO(data)->btExportSave))
591     filename =
592       gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(VISU_UI_VALUE_IO(data)->btExportOpen));
593   else
594     filename =
595       visu_ui_value_io_getFilename(VISU_UI_VALUE_IO(data)->parent);
596 
597   if (filename)
598     {
599       g_object_set_data(G_OBJECT(data), "filename", filename);
600       g_idle_add(saveXMLFile, (gpointer)data);
601     }
602 }
603 /**
604  * visu_ui_value_io_setSensitiveOpen:
605  * @valueio: a #VisuUiValueIo widget.
606  * @status: a boolean.
607  *
608  * Modify the sensitivity of the open button, depending on @status.
609  *
610  * Since: 3.5
611  */
visu_ui_value_io_setSensitiveOpen(VisuUiValueIo * valueio,gboolean status)612 void visu_ui_value_io_setSensitiveOpen(VisuUiValueIo *valueio, gboolean status)
613 {
614   g_return_if_fail(VISU_IS_UI_VALUE_IO(valueio));
615 
616   gtk_widget_set_sensitive(valueio->btExportOpen, status);
617 }
618 /**
619  * visu_ui_value_io_setSensitiveSave:
620  * @valueio: a #VisuUiValueIo widget.
621  * @status: a boolean.
622  *
623  * Modify the sensitivity of the save button, depending on @status.
624  *
625  * Since: 3.5
626  */
visu_ui_value_io_setSensitiveSave(VisuUiValueIo * valueio,gboolean status)627 void visu_ui_value_io_setSensitiveSave(VisuUiValueIo *valueio, gboolean status)
628 {
629   gchar *filename;
630 
631   g_return_if_fail(VISU_IS_UI_VALUE_IO(valueio));
632 
633 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
634   filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(valueio->btExportOpen));
635   gtk_widget_set_sensitive(valueio->btExportSave, status && filename);
636   if (filename)
637     g_free(filename);
638 #endif
639   gtk_widget_set_sensitive(valueio->btExportSaveAs, status);
640   g_object_notify_by_pspec(G_OBJECT(valueio), properties[SAVE_SENSITIVE_PROP]);
641 }
642 /**
643  * visu_ui_value_io_setFilename:
644  * @valueio: a #VisuUiValueIo widget.
645  * @filename: a location on disk.
646  *
647  * Call the open routine previously set by visu_ui_value_io_connectOnOpen() on
648  * @filename and update the buttons accordingly.
649  *
650  * Since: 3.5
651  *
652  * Returns: TRUE on success of the open routine.
653  */
visu_ui_value_io_setFilename(VisuUiValueIo * valueio,const gchar * filename)654 gboolean visu_ui_value_io_setFilename(VisuUiValueIo *valueio, const gchar *filename)
655 {
656   gboolean valid;
657   GError *error;
658 
659   g_return_val_if_fail(VISU_IS_UI_VALUE_IO(valueio), FALSE);
660   g_return_val_if_fail(valueio->ioOpen, FALSE);
661 
662   error = (GError*)0;
663   valid = valueio->ioOpen(filename, &error);
664   if (!valid)
665     {
666       visu_ui_raiseWarning
667 	(_("Export V_Sim values to a file."),
668 	 error->message, valueio->parent);
669       g_error_free(error);
670 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
671       gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(valueio->btExportOpen));
672       gtk_widget_set_sensitive(valueio->btExportSave, FALSE);
673 #endif
674     }
675   else
676     {
677       gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(valueio->btExportOpen), filename);
678 #if GTK_MAJOR_VERSION > 2 || GTK_MINOR_VERSION > 5
679       gtk_widget_set_sensitive(valueio->btExportSave, TRUE);
680 #endif
681       gtk_widget_set_sensitive(valueio->btExportSaveAs, TRUE);
682     }
683 
684   return valid;
685 }
686