atomes 1.3.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-2026 by CNRS and University of Strasbourg */
15
21
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 int i, j;
74 j = GPOINTER_TO_INT(data);
75 m = entry_get_text (val);
76 i = string_to_double ((gpointer)m);
77 if (i > 0)
78 {
79 resol[j] = i;
80 }
81 else
82 {
83 show_warning ((j) ? _("Y size must be > 0") : _("X size must be > 0"), NULL);
84 }
86}
87
88#ifdef GTK4
97G_MODULE_EXPORT void set_background (GtkCheckButton * backb, gpointer data)
98#else
107G_MODULE_EXPORT void set_background (GtkToggleButton * backb, gpointer data)
108#endif
109{
110 if (button_get_status ((GtkWidget *)backb))
111 {
112 back_alpha = 0.0;
113 }
114 else
115 {
116 back_alpha = 1.0;
117 }
118}
119
128G_MODULE_EXPORT void choose_format (GtkComboBox * box, gpointer cid)
129{
130 forme = combo_get_active ((GtkWidget *)box);
131 if (forme != -1)
132 {
133 dwidget * imf = (dwidget *) cid;
134 button_set_status (imf -> a, 0);
135 if (forme == 1 || forme == 3)
136 {
137 widget_set_sensitive(imf -> a, 0);
138 }
139 else
140 {
141 widget_set_sensitive(imf -> a, 1);
142 }
143 switch (forme)
144 {
145 case 0:
146 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_PNG);
147 break;
148 case 1:
149 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_PDF);
150 break;
151 case 2:
152 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_SVG);
153 break;
154 case 3:
155 gtk_image_set_from_file (GTK_IMAGE(imf -> b), PACKAGE_EPS);
156 break;
157 }
158 }
159}
160
161gchar * i_title[4]={"Portable Network Graphics",
162 "Portable Document File",
163 "Scalable Vector Graphics",
164 "Encapsulated Post-Script Image"};
165gchar * i_pattern[4]={"*.png",
166 "*.pdf",
167 "*.svg",
168 "*.eps"};
169
170#ifdef GTK4
180G_MODULE_EXPORT void run_write_image (GtkNativeDialog * info, gint response_id, gpointer data)
181{
182 GtkFileChooser * chooser = GTK_FILE_CHOOSER((GtkFileChooserNative *)info);
183#else
193G_MODULE_EXPORT void run_write_image (GtkDialog * info, gint response_id, gpointer data)
194{
195 GtkFileChooser * chooser = GTK_FILE_CHOOSER((GtkWidget *)info);
196#endif
197 if (response_id == GTK_RESPONSE_ACCEPT)
198 {
200 Curve * this_curve = get_curve_from_pointer (data);
201 this_curve -> format = forme + 1;
202#ifdef GTK3
203 show_curve (NULL, NULL, data);
204#else
205 GdkSurface * surf = gtk_native_get_surface ((GtkNative *)this_curve -> plot);
206 cairo_t * rec = (cairo_t *)gdk_cairo_context_cairo_create (gdk_surface_create_cairo_context (surf));
207 show_curve (NULL, rec, resol[0], resol[1], data);
208#endif
209 }
210#ifdef GTK4
212#else
213 destroy_this_dialog (info);
214#endif
215}
216
224void write_image (gpointer curetow)
225{
226 gchar * i_filter[4]={i18n("PNG file (*.png)"),
227 i18n("PDF file (*.pdf)"),
228 i18n("SVG file (*.svg)"),
229 i18n("EPS file (*.eps)")};
230 GtkFileFilter * filter;
231 Curve * this_curve = get_curve_from_pointer (curetow);
232 if (forme == -1)
233 {
234 show_warning (_("To save an image please enter a file format"), this_curve -> window);
235 }
236 else if (resol[0] == 0 || resol[1] == 0)
237 {
238 show_warning (_("You need to specify the size of the image"), this_curve -> window);
239 }
240 else
241 {
242#ifdef GTK4
243 GtkFileChooserNative * info;
244#else
245 GtkWidget * info;
246#endif
248 GTK_WINDOW(this_curve -> window),
249 GTK_FILE_CHOOSER_ACTION_SAVE,
250 _("Save"));
251 GtkFileChooser * chooser = GTK_FILE_CHOOSER(info);
252#ifdef GTK3
253 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
254#endif
255 filter = gtk_file_filter_new();
256 gtk_file_filter_set_name(GTK_FILE_FILTER(filter), _(i_filter[forme]));
257 gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), i_pattern[forme]);
258 gtk_file_chooser_add_filter (chooser, filter);
259#ifdef GTK4
260 run_this_gtk_native_dialog ((GtkNativeDialog *)info, G_CALLBACK(run_write_image), curetow);
261#else
262 run_this_gtk_dialog (info, G_CALLBACK(run_write_image), curetow);
263#endif
264 }
265}
266
276G_MODULE_EXPORT void run_save_image (GtkDialog * save_img, gint response_id, gpointer data)
277{
278 gboolean done = FALSE;
279 while (! done)
280 {
281 switch (response_id)
282 {
283 case GTK_RESPONSE_APPLY:
284 if (forme > -1)
285 {
286 write_image (data);
287 done = TRUE;
288 }
289 else
290 {
291 done = FALSE;
292 }
293 break;
294 default:
295 done = TRUE;
296 break;
297 }
298 }
299 if (done) destroy_this_dialog (save_img);
300}
301
309void save_image (gpointer cdata)
310{
311 GtkWidget * save_img;
312 GtkWidget * hbox;
313 GtkWidget * comboformat;
314 GtkWidget * x;
315 GtkWidget * y;
316 GtkWidget * background;
317 GtkWidget * log;
318 int xlgt, ylgt;
319 int i;
320 gchar * str;
321 Curve * this_curve = get_curve_from_pointer (cdata);
322 save_img = dialog_cancel_apply (_("Export image"), this_curve -> window, FALSE);
323 gtk_window_set_resizable (GTK_WINDOW (save_img), FALSE);
324#ifndef GTK4
325 gtk_window_set_icon (GTK_WINDOW (save_img), THETD);
326#endif
327 GtkWidget * vbox = dialog_get_content_area (save_img);
328 gtk_box_set_spacing (GTK_BOX(vbox), 15);
329
330 hbox = create_hbox (0);
331 add_box_child_start (GTK_ORIENTATION_VERTICAL, vbox, hbox, TRUE, TRUE, 0);
332 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, markup_label (_("Format: "), -1, 50, 0.0, 0.5), TRUE, TRUE, 0);
333 GtkWidget * fixed = gtk_fixed_new ();
334 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, fixed, TRUE, TRUE, 10);
335 comboformat = create_combo ();
336 for (i=0; i<4; i++)
337 {
338 str = g_strdup_printf ("%s - (%s)", _(i_title[i]), i_pattern[i]);
339 combo_text_append (comboformat, str);
340 g_free (str);
341 }
342 gtk_fixed_put (GTK_FIXED(fixed), comboformat, -1, 10);
343 gtk_widget_set_size_request (comboformat, -1, 30);
344
345 log = gtk_image_new_from_file (PACKAGE_VOID);
346 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, log, TRUE, TRUE, 0);
347
348 hbox = create_hbox (0);
349 add_box_child_start (GTK_ORIENTATION_VERTICAL, vbox, hbox, TRUE, TRUE, 0);
350 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, markup_label(_("Size: "), 100, -1, 0.0, 0.5), FALSE, TRUE, 0);
351
352 xlgt = get_widget_width (this_curve -> plot);
353 ylgt = get_widget_height (this_curve -> plot);
354 x = create_entry (G_CALLBACK(set_size), 50, 15, FALSE, GINT_TO_POINTER(0));
355 update_entry_int (GTK_ENTRY(x), xlgt);
356 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, x, TRUE, TRUE, 0);
357 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, gtk_label_new ("x"), TRUE, TRUE, 5);
358 y = create_entry (G_CALLBACK(set_size), 50, 15, FALSE, GINT_TO_POINTER(1));
359 update_entry_int (GTK_ENTRY(y), ylgt);
360 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, y, TRUE, TRUE, 0);
361
362 add_box_child_start (GTK_ORIENTATION_HORIZONTAL, hbox, gtk_label_new (_("pixels")), TRUE, TRUE, 0);
363
364 background = check_button (_("Make background transparent"), -1, -1, FALSE, G_CALLBACK(set_background), NULL);
366 add_box_child_start (GTK_ORIENTATION_VERTICAL, vbox, background, TRUE, TRUE, 0);
367
368 forme = -1;
369 back_alpha = 1.0;
371 thedata.b = log;
372 g_signal_connect (G_OBJECT(comboformat), "changed", G_CALLBACK(choose_format), & thedata);
373
374 run_this_gtk_dialog (save_img, G_CALLBACK(run_save_image), cdata);
375}
GtkFileFilter * filter[NCFORMATS+1]
Definition callbacks.c:1557
int resol[2]
Definition curve.c:65
Curve * get_curve_from_pointer(gpointer data)
get Curve pointer from pointer
Definition curve.c:313
char * curve_image_file
Definition curve.c:89
Variable declarations for the curve widget Functions for interactions with the curve widget.
double back_alpha
Definition w_img.c:58
void show_curve(GtkDrawingArea *area, cairo_t *cr, int width, int height, gpointer curve)
show curve callback GTK3
Definition show.c:77
float val
Definition dlp_init.c:117
gchar * PACKAGE_EPS
Definition global.c:95
gchar * PACKAGE_SVG
Definition global.c:94
GdkPixbuf * THETD
Definition global.c:222
#define i18n(String)
Definition global.c:80
gchar * PACKAGE_VOID
Definition global.c:100
gchar * PACKAGE_PDF
Definition global.c:93
double string_to_double(gpointer string)
convert string to double
Definition global.c:611
gchar * PACKAGE_PNG
Definition global.c:96
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:616
void run_this_gtk_dialog(GtkWidget *dial, GCallback handler, gpointer data)
run a GTK (3 and 4) basic GtkDialog
Definition gtk-misc.c:533
int combo_get_active(GtkWidget *combo)
retrieve the active item's position
Definition gtk-misc.c:935
GtkWidget * create_entry(GCallback handler, int dim, int cdim, gboolean key_release, gpointer data)
Create a GtkEntry.
Definition gtk-misc.c:1401
int get_widget_height(GtkWidget *widg)
retrive GtkWidget height
Definition gtk-misc.c:2589
gchar * file_chooser_get_file_name(GtkFileChooser *chooser)
get a file name from a GtkFileChooser (single file selected)
Definition gtk-misc.c:2306
const gchar * entry_get_text(GtkEntry *entry)
get the text in a GtkEntry
Definition gtk-misc.c:652
void update_entry_int(GtkEntry *entry, int intval)
update the content of a GtkEntry as int
Definition gtk-misc.c:669
int get_widget_width(GtkWidget *widg)
retrive GtkWidget width
Definition gtk-misc.c:2573
void button_set_status(GtkWidget *button, int status)
set status of check / toggle button
Definition gtk-misc.c:1916
GtkWidget * check_button(gchar *text, int dimx, int dimy, gboolean state, GCallback handler, gpointer data)
create a check button
Definition gtk-misc.c:1937
GtkWidget * create_combo()
create a GtkCombox widget, note deprecated in GTK4
Definition gtk-misc.c:1010
GtkWidget * dialog_get_content_area(GtkWidget *widg)
prepare GtkWidget to insert content in a GtkDialog window
Definition gtk-misc.c:861
GtkWidget * markup_label(gchar *text, int dimx, int dimy, float ax, float ay)
create a GtkLabel with pango markup
Definition gtk-misc.c:1672
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:340
void widget_set_sensitive(GtkWidget *widg, gboolean sensitive)
Set sensitivity for a GtkWidget, ensuring it is a GtkWidget.
Definition gtk-misc.c:247
void destroy_this_dialog(GtkDialog *dialog)
destroy a GtkDialog
Definition gtk-misc.c:2235
GtkWidget * create_hbox(int spacing)
create a GtkBox with horizontal orientation
Definition gtk-misc.c:849
void combo_text_append(GtkWidget *combo, gchar *text)
append text in GtkComboBox widget
Definition gtk-misc.c:987
int button_get_status(GtkWidget *button)
get status of check / toggle button
Definition gtk-misc.c:1899
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:2393
void destroy_this_native_dialog(GtkNativeDialog *dialog)
destroy a GtkNativeDialog
Definition gtk-misc.c:2254
image * plot
Definition ogl_draw.c:72
void show_warning(char *warning, GtkWidget *win)
show warning
Definition interface.c:266
Messaging function declarations.
double y
Definition ogl_draw.c:63
double x
Definition ogl_draw.c:63
Definition glwin.h:350
GtkWidget * a
Definition w_img.c:54
GtkWidget * b
Definition w_img.c:55
dwidget thedata
Definition w_img.c:59
void write_image(gpointer curetow)
write image - creating the file chooser dialog
Definition w_img.c:224
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:276
G_MODULE_EXPORT void set_background(GtkToggleButton *backb, gpointer data)
show / hide image background toggle callback GTK3
Definition w_img.c:107
gchar * i_title[4]
Definition w_img.c:161
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:128
void save_image(gpointer cdata)
export curve window plot to image - creating the dialog
Definition w_img.c:309
G_MODULE_EXPORT void run_write_image(GtkDialog *info, gint response_id, gpointer data)
write image - running the dialog GTK3
Definition w_img.c:193
gchar * i_pattern[4]
Definition w_img.c:165
int forme
Definition w_img.c:60
GtkWidget * hbox
Definition workspace.c:71
GtkWidget * vbox
Definition workspace.c:72