atomes 1.1.14
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
w_img.c
Go to the documentation of this file.
1/* This file is part of the 'atomes' software
2
3'atomes' is free software: you can redistribute it and/or modify it under the terms
4of the GNU Affero General Public License as published by the Free Software Foundation,
5either version 3 of the License, or (at your option) any later version.
6
7'atomes' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9See the GNU General Public License for more details.
10
11You should have received a copy of the GNU Affero General Public License along with 'atomes'.
12If not, see <https://www.gnu.org/licenses/>
13
14Copyright (C) 2022-2024 by CNRS and University of Strasbourg */
15
22/*
23* This file: 'w_img.c'
24*
25* Contains:
26*
27
28 - The functions to save screenshot from graph / curve(s)
29
30*
31* List of functions:
32
33 void write_image (gpointer curetow);
34 void save_image (gpointer cdata);
35
36 G_MODULE_EXPORT void set_size (GtkEntry * val, gpointer data);
37 G_MODULE_EXPORT void set_background (GtkCheckButton * backb);
38 G_MODULE_EXPORT void set_background (GtkToggleButton * backb);
39 G_MODULE_EXPORT void choose_format (GtkComboBox * box, gpointer cid);
40 G_MODULE_EXPORT void run_write_image (GtkNativeDialog * info, gint response_id, gpointer data);
41 G_MODULE_EXPORT void run_write_image (GtkDialog * info, gint response_id, gpointer data);
42 G_MODULE_EXPORT void run_save_image (GtkDialog * save_img, gint response_id, gpointer data);
43
44*/
45
46#include <gtk/gtk.h>
47#include <stdlib.h>
48
49#include "global.h"
50#include "interface.h"
51#include "curve.h"
52
53typedef struct {
54 GtkWidget * a;
55 GtkWidget * b;
56} dwidget;
57
61
70G_MODULE_EXPORT void set_size (GtkEntry * val, gpointer data)
71{
72 const gchar * m;
73 char * text[2];
74 int i, j;
75 j = GPOINTER_TO_INT(data);
76 text[0] = "X size must be > 0";
77 text[1] = "Y size must be > 0";
78 m = entry_get_text (val);
79 i = atof(m);
80 if (i > 0)
81 {
82 resol[j] = i;
83 }
84 else
85 {
86 show_warning (text[j], NULL);
87 }
89}
90
91#ifdef GTK4
100G_MODULE_EXPORT void set_background (GtkCheckButton * backb, gpointer data)
101#else
110G_MODULE_EXPORT void set_background (GtkToggleButton * backb, gpointer data)
111#endif
112{
113#ifdef GTK4
114 if (gtk_check_button_get_active (backb))
115#else
116 if (gtk_toggle_button_get_active (backb))
117#endif
118 {
119 back_alpha = 0.0;
120 }
121 else
122 {
123 back_alpha = 1.0;
124 }
125}
126
135G_MODULE_EXPORT void choose_format (GtkComboBox * box, gpointer cid)
136{
137 forme = gtk_combo_box_get_active (box);
138 if (forme != -1)
139 {
140 dwidget * imf = (dwidget *) cid;
141#ifdef GTK4
142 gtk_check_button_set_active (GTK_CHECK_BUTTON(imf -> a), 0);
143#else
144 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(imf -> a), 0);
145#endif
146 if (forme == 1 || forme == 3)
147 {
148 widget_set_sensitive(imf -> a, 0);
149 }
150 else
151 {
152 widget_set_sensitive(imf -> a, 1);
153 }
154 switch (forme)
155 {
156 case 0:
157 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_PNG);
158 break;
159 case 1:
160 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_PDF);
161 break;
162 case 2:
163 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_SVG);
164 break;
165 case 3:
166 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_EPS);
167 break;
168 }
169 }
170}
171
172gchar * i_title[4]={"Portable Network Graphics",
173 "Portable Document File",
174 "Scalable Vector Graphics",
175 "Encapsulated Post-Script Image"};
176gchar * i_pattern[4]={"*.png",
177 "*.pdf",
178 "*.svg",
179 "*.eps"};
180
181#ifdef GTK4
191G_MODULE_EXPORT void run_write_image (GtkNativeDialog * info, gint response_id, gpointer data)
192{
193 GtkFileChooser * chooser = GTK_FILE_CHOOSER((GtkFileChooserNative *)info);
194#else
204G_MODULE_EXPORT void run_write_image (GtkDialog * info, gint response_id, gpointer data)
205{
206 GtkFileChooser * chooser = GTK_FILE_CHOOSER((GtkWidget *)info);
207#endif
208 tint * cd = (tint *)data;
209 if (response_id == GTK_RESPONSE_ACCEPT)
210 {
212 project * this_proj = get_project_by_id (cd -> a);
213 this_proj -> curves[cd -> b][cd -> c] -> format = forme + 1;
214#ifdef GTK3
215 show_curve (NULL, NULL, data);
216#else
217 GdkSurface * surf = gtk_native_get_surface ((GtkNative *)this_proj -> curves[cd -> b][cd -> c] -> plot);
218 cairo_t * rec = (cairo_t *)gdk_cairo_context_cairo_create (gdk_surface_create_cairo_context (surf));
219 show_curve (NULL, rec, resol[0], resol[1], data);
220#endif
221 }
222#ifdef GTK4
224#else
225 destroy_this_dialog (info);
226#endif
227}
228
236void write_image (gpointer curetow)
237{
238 int a, b, c;
239 tint * cd = (tint *)curetow;
240 a = cd -> a;
241 b = cd -> b;
242 c = cd -> c;
243 gchar * i_filter[4]={"PNG file (*.png)",
244 "PDF file (*.pdf)",
245 "SVG file (*.svg)",
246 "EPS file (*.eps)"};
247 GtkFileFilter * filter;
248 project * this_proj = get_project_by_id(a);
249 if (forme == -1)
250 {
251 show_warning ("To save an image please enter a file format", this_proj -> curves[b][c] -> window);
252 }
253 else if (resol[0] == 0 || resol[1] == 0)
254 {
255 show_warning ("You need to specify the size of the image", this_proj -> curves[b][c] -> window);
256 }
257 else
258 {
259#ifdef GTK4
260 GtkFileChooserNative * info;
261#else
262 GtkWidget * info;
263#endif
265 GTK_WINDOW(this_proj -> curves[b][c] -> window),
266 GTK_FILE_CHOOSER_ACTION_SAVE,
267 "Save");
268 GtkFileChooser * chooser = GTK_FILE_CHOOSER(info);
269#ifdef GTK3
270 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
271#endif
272 filter = gtk_file_filter_new();
273 gtk_file_filter_set_name(GTK_FILE_FILTER(filter), i_filter[forme]);
274 gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), i_pattern[forme]);
275 gtk_file_chooser_add_filter (chooser, filter);
276#ifdef GTK4
277 run_this_gtk_native_dialog ((GtkNativeDialog *)info, G_CALLBACK(run_write_image), curetow);
278#else
279 run_this_gtk_dialog (info, G_CALLBACK(run_write_image), curetow);
280#endif
281 }
282}
283
293G_MODULE_EXPORT void run_save_image (GtkDialog * save_img, gint response_id, gpointer data)
294{
295 gboolean done = FALSE;
296 while (! done)
297 {
298 switch (response_id)
299 {
300 case GTK_RESPONSE_APPLY:
301 if (forme > -1)
302 {
303 write_image (data);
304 done = TRUE;
305 }
306 else
307 {
308 done = FALSE;
309 }
310 break;
311 default:
312 done = TRUE;
313 break;
314 }
315 }
316 if (done) destroy_this_dialog (save_img);
317}
318
326void save_image (gpointer cdata)
327{
328 GtkWidget * save_img;
329 GtkWidget * hbox;
330 GtkWidget * comboformat;
331 GtkWidget * x;
332 GtkWidget * y;
333 GtkWidget * background;
334 GtkWidget * log;
335 int xlgt, ylgt;
336 int i;
337 gchar * str;
338 int a, b, c;
339 tint * cd = (tint *)cdata;
340 a = cd -> a;
341 b = cd -> b;
342 c = cd -> c;
343 project * this_proj = get_project_by_id(a);
344 save_img = dialog_cancel_apply ("Export image", this_proj -> curves[b][c] -> window, FALSE);
345 gtk_window_set_resizable (GTK_WINDOW (save_img), FALSE);
346#ifndef GTK4
347 gtk_window_set_icon (GTK_WINDOW (save_img), THETD);
348#endif
349 GtkWidget * vbox = dialog_get_content_area (save_img);
350 gtk_box_set_spacing (GTK_BOX(vbox), 15);
351
352 hbox = create_hbox (0);
353 add_box_child_start (GTK_ORIENTATION_VERTICAL, vbox, hbox, TRUE, TRUE, 0);
354 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, markup_label ("Format: ", -1, 50, 0.0, 0.5), TRUE, TRUE, 0);
355 GtkWidget * fixed = gtk_fixed_new ();
356 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, fixed, TRUE, TRUE, 10);
357 comboformat = create_combo ();
358 for (i=0; i<4; i++)
359 {
360 str = g_strdup_printf ("%s - (%s)", i_title[i], i_pattern[i]);
361 combo_text_append (comboformat, str);
362 g_free (str);
363 }
364 gtk_fixed_put (GTK_FIXED(fixed), comboformat, -1, 10);
365 gtk_widget_set_size_request (comboformat, -1, 30);
366
367 log = gtk_image_new_from_file (PACKAGE_VOID);
368 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, log, TRUE, TRUE, 0);
369
370 hbox = create_hbox (0);
371 add_box_child_start (GTK_ORIENTATION_VERTICAL, vbox, hbox, TRUE, TRUE, 0);
372 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, markup_label("Size: ", 100, -1, 0.0, 0.5), FALSE, TRUE, 0);
373
374 xlgt = gtk_widget_get_allocated_width (this_proj -> curves[b][c] -> plot);
375 ylgt = gtk_widget_get_allocated_height (this_proj -> curves[b][c] -> plot);
376 x = create_entry (G_CALLBACK(set_size), 50, 15, FALSE, GINT_TO_POINTER(0));
377 update_entry_int (GTK_ENTRY(x), xlgt);
378 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, x, TRUE, TRUE, 0);
379 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, gtk_label_new ("x"), TRUE, TRUE, 5);
380 y = create_entry (G_CALLBACK(set_size), 50, 15, FALSE, GINT_TO_POINTER(1));
381 update_entry_int (GTK_ENTRY(y), ylgt);
382 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, y, TRUE, TRUE, 0);
383
384 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, gtk_label_new ("pixels"), TRUE, TRUE, 0);
385
386 background = check_button ("Make background transparent", -1, -1, FALSE, G_CALLBACK(set_background), NULL);
387 widget_set_sensitive (background, FALSE);
388 add_box_child_start (GTK_ORIENTATION_VERTICAL, vbox, background, TRUE, TRUE, 0);
389
390 forme = -1;
391 back_alpha = 1.0;
392 thedata.a = background;
393 thedata.b = log;
394 g_signal_connect (G_OBJECT(comboformat), "changed", G_CALLBACK(choose_format), & thedata);
395
396 run_this_gtk_dialog (save_img, G_CALLBACK(run_save_image), cdata);
397}
GtkFileFilter * filter[NCFORMATS+1]
Definition callbacks.c:1406
int resol[2]
Definition curve.c:64
char * curve_image_file
Definition curve.c:88
Variable declarations for the curve widget Functions for interactions with the curve widget.
void show_curve(GtkDrawingArea *area, cairo_t *cr, int width, int height, gpointer curve)
show curve callback GTK3
Definition show.c:77
gchar * text
Definition datab.c:105
float val
Definition dlp_init.c:117
GdkPixbuf * THETD
Definition global.c:228
Global variable declarations Global convenience function declarations Global data structure defin...
GtkWidget * dialog_cancel_apply(gchar *title, GtkWidget *parent, gboolean resiz)
create a dialog modal offering a choice to apply something or not
Definition gtk-misc.c:541
void run_this_gtk_dialog(GtkWidget *dial, GCallback handler, gpointer data)
run a GTK (3 and 4) basic GtkDialog
Definition gtk-misc.c:472
GtkWidget * create_entry(GCallback handler, int dim, int cdim, gboolean key_release, gpointer data)
Create a GtkEntry.
Definition gtk-misc.c:1294
gchar * file_chooser_get_file_name(GtkFileChooser *chooser)
get a file name from a GtkFileChooser (single file selected)
Definition gtk-misc.c:2112
const gchar * entry_get_text(GtkEntry *entry)
get the text in a GtkEntry
Definition gtk-misc.c:577
void update_entry_int(GtkEntry *entry, int intval)
update the content of a GtkEntry as int
Definition gtk-misc.c:594
GtkWidget * check_button(gchar *text, int dimx, int dimy, gboolean state, GCallback handler, gpointer data)
create a check button
Definition gtk-misc.c:1779
GtkWidget * create_combo()
create a GtkCombox widget, note deprecated in GTK4
Definition gtk-misc.c:903
GtkWidget * dialog_get_content_area(GtkWidget *widg)
prepare GtkWidget to insert content in a GtkDialog window
Definition gtk-misc.c:805
GtkWidget * markup_label(gchar *text, int dimx, int dimy, float ax, float ay)
Definition gtk-misc.c:1565
void add_box_child_start(int orientation, GtkWidget *widg, GtkWidget *child, gboolean expand, gboolean fill, int padding)
Add a GtkWidget in a GtkBox at the initial position.
Definition gtk-misc.c:279
void widget_set_sensitive(GtkWidget *widg, gboolean sensitive)
Set sensitivity for a GtkWidget, ensuring it is a GtkWidget.
Definition gtk-misc.c:186
void destroy_this_dialog(GtkDialog *dialog)
destroy a GtkDialog
Definition gtk-misc.c:2041
GtkWidget * create_hbox(int spacing)
create a GtkBox with horizontal orientation
Definition gtk-misc.c:793
void combo_text_append(GtkWidget *combo, gchar *text)
append text in GtkComboBox widget
Definition gtk-misc.c:880
GtkWidget * create_file_chooser(const gchar *title, GtkWindow *parent, GtkFileChooserAction act, const gchar *act_name)
create a GtkFileChooser, utility to select file(s)
Definition gtk-misc.c:2199
void destroy_this_native_dialog(GtkNativeDialog *dialog)
destroy a GtkNativeDialog
Definition gtk-misc.c:2060
project * get_project_by_id(int p)
get project pointer using id number
Definition project.c:120
image * plot
Definition ogl_draw.c:66
void show_warning(char *warning, GtkWidget *win)
show warning
Definition interface.c:260
Messaging function declarations.
double y
Definition ogl_draw.c:57
double x
Definition ogl_draw.c:57
GtkWidget * a
Definition w_img.c:54
GtkWidget * b
Definition w_img.c:55
Definition global.h:98
int b
Definition tab-1.c:95
int c
Definition tab-1.c:95
int a
Definition tab-1.c:95
dwidget thedata
Definition w_img.c:59
void write_image(gpointer curetow)
write image - creating the file chooser dialog
Definition w_img.c:236
G_MODULE_EXPORT void run_save_image(GtkDialog *save_img, gint response_id, gpointer data)
export curve window plot to image - running the dialog
Definition w_img.c:293
G_MODULE_EXPORT void set_background(GtkToggleButton *backb, gpointer data)
show / hide image background toggle callback GTK3
Definition w_img.c:110
gchar * i_title[4]
Definition w_img.c:172
G_MODULE_EXPORT void set_size(GtkEntry *val, gpointer data)
set image size entry callback
Definition w_img.c:70
G_MODULE_EXPORT void choose_format(GtkComboBox *box, gpointer cid)
change image format
Definition w_img.c:135
void save_image(gpointer cdata)
export curve window plot to image - creating the dialog
Definition w_img.c:326
G_MODULE_EXPORT void run_write_image(GtkDialog *info, gint response_id, gpointer data)
write image - running the dialog GTK3
Definition w_img.c:204
double back_alpha
Definition w_img.c:58
gchar * i_pattern[4]
Definition w_img.c:176
int forme
Definition w_img.c:60
GtkWidget * hbox
Definition workspace.c:71
GtkWidget * vbox
Definition workspace.c:72