atomes 1.2.1
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-2025 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 = string_to_double ((gpointer)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 if (button_get_status ((GtkWidget *)backb))
114 {
115 back_alpha = 0.0;
116 }
117 else
118 {
119 back_alpha = 1.0;
120 }
121}
122
131G_MODULE_EXPORT void choose_format (GtkComboBox * box, gpointer cid)
132{
133 forme = combo_get_active ((GtkWidget *)box);
134 if (forme != -1)
135 {
136 dwidget * imf = (dwidget *) cid;
137 button_set_status (imf -> a, 0);
138 if (forme == 1 || forme == 3)
139 {
140 widget_set_sensitive(imf -> a, 0);
141 }
142 else
143 {
144 widget_set_sensitive(imf -> a, 1);
145 }
146 switch (forme)
147 {
148 case 0:
149 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_PNG);
150 break;
151 case 1:
152 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_PDF);
153 break;
154 case 2:
155 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_SVG);
156 break;
157 case 3:
158 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_EPS);
159 break;
160 }
161 }
162}
163
164gchar * i_title[4]={"Portable Network Graphics",
165 "Portable Document File",
166 "Scalable Vector Graphics",
167 "Encapsulated Post-Script Image"};
168gchar * i_pattern[4]={"*.png",
169 "*.pdf",
170 "*.svg",
171 "*.eps"};
172
173#ifdef GTK4
183G_MODULE_EXPORT void run_write_image (GtkNativeDialog * info, gint response_id, gpointer data)
184{
185 GtkFileChooser * chooser = GTK_FILE_CHOOSER((GtkFileChooserNative *)info);
186#else
196G_MODULE_EXPORT void run_write_image (GtkDialog * info, gint response_id, gpointer data)
197{
198 GtkFileChooser * chooser = GTK_FILE_CHOOSER((GtkWidget *)info);
199#endif
200 tint * cd = (tint *)data;
201 if (response_id == GTK_RESPONSE_ACCEPT)
202 {
204 project * this_proj = get_project_by_id (cd -> a);
205 this_proj -> curves[cd -> b][cd -> c] -> format = forme + 1;
206#ifdef GTK3
207 show_curve (NULL, NULL, data);
208#else
209 GdkSurface * surf = gtk_native_get_surface ((GtkNative *)this_proj -> curves[cd -> b][cd -> c] -> plot);
210 cairo_t * rec = (cairo_t *)gdk_cairo_context_cairo_create (gdk_surface_create_cairo_context (surf));
211 show_curve (NULL, rec, resol[0], resol[1], data);
212#endif
213 }
214#ifdef GTK4
216#else
217 destroy_this_dialog (info);
218#endif
219}
220
228void write_image (gpointer curetow)
229{
230 int a, b, c;
231 tint * cd = (tint *)curetow;
232 a = cd -> a;
233 b = cd -> b;
234 c = cd -> c;
235 gchar * i_filter[4]={"PNG file (*.png)",
236 "PDF file (*.pdf)",
237 "SVG file (*.svg)",
238 "EPS file (*.eps)"};
239 GtkFileFilter * filter;
240 project * this_proj = get_project_by_id(a);
241 if (forme == -1)
242 {
243 show_warning ("To save an image please enter a file format", this_proj -> curves[b][c] -> window);
244 }
245 else if (resol[0] == 0 || resol[1] == 0)
246 {
247 show_warning ("You need to specify the size of the image", this_proj -> curves[b][c] -> window);
248 }
249 else
250 {
251#ifdef GTK4
252 GtkFileChooserNative * info;
253#else
254 GtkWidget * info;
255#endif
257 GTK_WINDOW(this_proj -> curves[b][c] -> window),
258 GTK_FILE_CHOOSER_ACTION_SAVE,
259 "Save");
260 GtkFileChooser * chooser = GTK_FILE_CHOOSER(info);
261#ifdef GTK3
262 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
263#endif
264 filter = gtk_file_filter_new();
265 gtk_file_filter_set_name(GTK_FILE_FILTER(filter), i_filter[forme]);
266 gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), i_pattern[forme]);
267 gtk_file_chooser_add_filter (chooser, filter);
268#ifdef GTK4
269 run_this_gtk_native_dialog ((GtkNativeDialog *)info, G_CALLBACK(run_write_image), curetow);
270#else
271 run_this_gtk_dialog (info, G_CALLBACK(run_write_image), curetow);
272#endif
273 }
274}
275
285G_MODULE_EXPORT void run_save_image (GtkDialog * save_img, gint response_id, gpointer data)
286{
287 gboolean done = FALSE;
288 while (! done)
289 {
290 switch (response_id)
291 {
292 case GTK_RESPONSE_APPLY:
293 if (forme > -1)
294 {
295 write_image (data);
296 done = TRUE;
297 }
298 else
299 {
300 done = FALSE;
301 }
302 break;
303 default:
304 done = TRUE;
305 break;
306 }
307 }
308 if (done) destroy_this_dialog (save_img);
309}
310
318void save_image (gpointer cdata)
319{
320 GtkWidget * save_img;
321 GtkWidget * hbox;
322 GtkWidget * comboformat;
323 GtkWidget * x;
324 GtkWidget * y;
325 GtkWidget * background;
326 GtkWidget * log;
327 int xlgt, ylgt;
328 int i;
329 gchar * str;
330 int a, b, c;
331 tint * cd = (tint *)cdata;
332 a = cd -> a;
333 b = cd -> b;
334 c = cd -> c;
335 project * this_proj = get_project_by_id(a);
336 save_img = dialog_cancel_apply ("Export image", this_proj -> curves[b][c] -> window, FALSE);
337 gtk_window_set_resizable (GTK_WINDOW (save_img), FALSE);
338#ifndef GTK4
339 gtk_window_set_icon (GTK_WINDOW (save_img), THETD);
340#endif
341 GtkWidget * vbox = dialog_get_content_area (save_img);
342 gtk_box_set_spacing (GTK_BOX(vbox), 15);
343
344 hbox = create_hbox (0);
345 add_box_child_start (GTK_ORIENTATION_VERTICAL, vbox, hbox, TRUE, TRUE, 0);
346 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, markup_label ("Format: ", -1, 50, 0.0, 0.5), TRUE, TRUE, 0);
347 GtkWidget * fixed = gtk_fixed_new ();
348 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, fixed, TRUE, TRUE, 10);
349 comboformat = create_combo ();
350 for (i=0; i<4; i++)
351 {
352 str = g_strdup_printf ("%s - (%s)", i_title[i], i_pattern[i]);
353 combo_text_append (comboformat, str);
354 g_free (str);
355 }
356 gtk_fixed_put (GTK_FIXED(fixed), comboformat, -1, 10);
357 gtk_widget_set_size_request (comboformat, -1, 30);
358
359 log = gtk_image_new_from_file (PACKAGE_VOID);
360 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, log, TRUE, TRUE, 0);
361
362 hbox = create_hbox (0);
363 add_box_child_start (GTK_ORIENTATION_VERTICAL, vbox, hbox, TRUE, TRUE, 0);
364 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, markup_label("Size: ", 100, -1, 0.0, 0.5), FALSE, TRUE, 0);
365
366 xlgt = get_widget_width (this_proj -> curves[b][c] -> plot);
367 ylgt = get_widget_height (this_proj -> curves[b][c] -> plot);
368 x = create_entry (G_CALLBACK(set_size), 50, 15, FALSE, GINT_TO_POINTER(0));
369 update_entry_int (GTK_ENTRY(x), xlgt);
370 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, x, TRUE, TRUE, 0);
371 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, gtk_label_new ("x"), TRUE, TRUE, 5);
372 y = create_entry (G_CALLBACK(set_size), 50, 15, FALSE, GINT_TO_POINTER(1));
373 update_entry_int (GTK_ENTRY(y), ylgt);
374 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, y, TRUE, TRUE, 0);
375
376 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, gtk_label_new ("pixels"), TRUE, TRUE, 0);
377
378 background = check_button ("Make background transparent", -1, -1, FALSE, G_CALLBACK(set_background), NULL);
380 add_box_child_start (GTK_ORIENTATION_VERTICAL, vbox, background, TRUE, TRUE, 0);
381
382 forme = -1;
383 back_alpha = 1.0;
385 thedata.b = log;
386 g_signal_connect (G_OBJECT(comboformat), "changed", G_CALLBACK(choose_format), & thedata);
387
388 run_this_gtk_dialog (save_img, G_CALLBACK(run_save_image), cdata);
389}
GtkFileFilter * filter[NCFORMATS+1]
Definition callbacks.c:1416
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
gchar * PACKAGE_EPS
Definition global.c:84
gchar * PACKAGE_SVG
Definition global.c:83
GdkPixbuf * THETD
Definition global.c:216
gchar * PACKAGE_VOID
Definition global.c:89
gchar * PACKAGE_PDF
Definition global.c:82
double string_to_double(gpointer string)
convert string to double
Definition global.c:624
gchar * PACKAGE_PNG
Definition global.c:85
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:571
void run_this_gtk_dialog(GtkWidget *dial, GCallback handler, gpointer data)
run a GTK (3 and 4) basic GtkDialog
Definition gtk-misc.c:501
int combo_get_active(GtkWidget *combo)
retrieve the active item's position
Definition gtk-misc.c:909
GtkWidget * create_entry(GCallback handler, int dim, int cdim, gboolean key_release, gpointer data)
Create a GtkEntry.
Definition gtk-misc.c:1375
int get_widget_height(GtkWidget *widg)
retrive GtkWidget height
Definition gtk-misc.c:2554
gchar * file_chooser_get_file_name(GtkFileChooser *chooser)
get a file name from a GtkFileChooser (single file selected)
Definition gtk-misc.c:2271
const gchar * entry_get_text(GtkEntry *entry)
get the text in a GtkEntry
Definition gtk-misc.c:607
void update_entry_int(GtkEntry *entry, int intval)
update the content of a GtkEntry as int
Definition gtk-misc.c:624
int get_widget_width(GtkWidget *widg)
retrive GtkWidget width
Definition gtk-misc.c:2538
void button_set_status(GtkWidget *button, int status)
set status of check / toggle button
Definition gtk-misc.c:1872
GtkWidget * check_button(gchar *text, int dimx, int dimy, gboolean state, GCallback handler, gpointer data)
create a check button
Definition gtk-misc.c:1893
GtkWidget * create_combo()
create a GtkCombox widget, note deprecated in GTK4
Definition gtk-misc.c:984
GtkWidget * dialog_get_content_area(GtkWidget *widg)
prepare GtkWidget to insert content in a GtkDialog window
Definition gtk-misc.c:835
GtkWidget * markup_label(gchar *text, int dimx, int dimy, float ax, float ay)
create a GtkLabel with pango markup
Definition gtk-misc.c:1646
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:308
void widget_set_sensitive(GtkWidget *widg, gboolean sensitive)
Set sensitivity for a GtkWidget, ensuring it is a GtkWidget.
Definition gtk-misc.c:215
void destroy_this_dialog(GtkDialog *dialog)
destroy a GtkDialog
Definition gtk-misc.c:2200
GtkWidget * create_hbox(int spacing)
create a GtkBox with horizontal orientation
Definition gtk-misc.c:823
void combo_text_append(GtkWidget *combo, gchar *text)
append text in GtkComboBox widget
Definition gtk-misc.c:961
int button_get_status(GtkWidget *button)
get status of check / toggle button
Definition gtk-misc.c:1855
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:2358
void destroy_this_native_dialog(GtkNativeDialog *dialog)
destroy a GtkNativeDialog
Definition gtk-misc.c:2219
project * get_project_by_id(int p)
get project pointer using id number
Definition project.c:120
image * plot
Definition ogl_draw.c:70
struct background background
a background data structure
Definition glwin.h:300
void show_warning(char *warning, GtkWidget *win)
show warning
Definition interface.c:260
Messaging function declarations.
double y
Definition ogl_draw.c:61
double x
Definition ogl_draw.c:61
Definition glwin.h:351
GtkWidget * a
Definition w_img.c:54
GtkWidget * b
Definition w_img.c:55
Definition global.h:106
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:228
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:285
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:164
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:131
void save_image(gpointer cdata)
export curve window plot to image - creating the dialog
Definition w_img.c:318
G_MODULE_EXPORT void run_write_image(GtkDialog *info, gint response_id, gpointer data)
write image - running the dialog GTK3
Definition w_img.c:196
double back_alpha
Definition w_img.c:58
gchar * i_pattern[4]
Definition w_img.c:168
int forme
Definition w_img.c:60
GtkWidget * hbox
Definition workspace.c:71
GtkWidget * vbox
Definition workspace.c:72