Debian Codegraph @

src/modules/frontend/gtk/screenshot.c

raw
1 /*****************************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * cdebconf is (c) 2000-2007 Randolph Chung and others under the following
7 * license.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *****************************************************************************/
33
34 /** @file screenshot.c
35 * screenshot related function of the GTK+ frontend
36 *
37 * This is really intended to be used inside the debian-installer, even if
38 * nothing prevents the following code from working within a X11 environment.
39 */
40
41 #include "screenshot.h"
42
43 #include <gdk/gdkkeysyms.h>
44 #include <gtk/gtk.h>
45
46 #include "cdebconf_gtk.h"
47 #include "fe_data.h"
48 #include "frontend.h"
49 #include "ui.h"
50 #include "di.h"
51
52 /** Directory where screenshots will be saved. */
53 #define SCREENSHOT_DIRECTORY "/var/log"
54
55 /** Write a picture of the given window.
56 *
57 * @param gdk_window the window to capture
58 * @param screenshot_path full path where the image will be saved
59 * @return TRUE if the operation was successful, FALSE otherwise
60 */
61 static gboolean save_screenshot(GdkWindow * gdk_window,
62 const gchar * screenshot_path)
63 {
64 GdkPixbuf * pixbuf;
65 gint width;
66 gint height;
67 gboolean success;
68
69 g_assert(NULL != gdk_window);
70 g_assert(NULL != screenshot_path);
71
72 width = gdk_window_get_width(gdk_window);
73 height = gdk_window_get_height(gdk_window);
74 #if GTK_CHECK_VERSION(3,0,0)
75 pixbuf = gdk_pixbuf_get_from_window(gdk_window,
76 #else
77 pixbuf = gdk_pixbuf_get_from_drawable(
78 NULL /* allocate a new pixbuf */, gdk_window,
79 gdk_colormap_get_system(), 0 /* src_x */, 0 /* src_y */,
80 #endif
81 0 /* dest_x */, 0 /* dest_y */, width, height);
82 if (NULL == pixbuf) {
83 g_warning("gdk_pixbuf_get_from_drawable failed.");
84 return FALSE;
85 }
86 success = gdk_pixbuf_save(pixbuf, screenshot_path, "png",
87 NULL /* no GError */,
88 NULL /* end of option list */);
89 /* This will free the pixbuf, see gdk_pixbuf_get_from_drawable doc. */
90 g_object_unref(pixbuf);
91 return success;
92 }
93
94 /** Make a path that can be used to write a screenshot.
95 *
96 * Screenshots will be named like the following: debian-installer_keymap_3.png
97 *
98 * The caller must free the returned string.
99 *
100 * @param question the question which tag will be used to create the path
101 * @return a newly allocated string containing a path where a screenshot can be
102 * saved
103 * @see SCREENSHOT_DIRECTORY
104 */
105 static gchar * create_screenshot_path(struct question * question)
106 {
107 gchar * question_tag;
108 gchar * path;
109 guint i;
110
111 question_tag = g_strdup(question->tag);
112 /* question_tag will be updated in place */
113 (void) g_strdelimit(question_tag, "/", '_');
114
115 for (i = 0; TRUE; i++) {
116 path = g_strdup_printf(SCREENSHOT_DIRECTORY "/%s_%u.png",
117 question_tag, i);
118 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
119 break;
120 }
121 g_free(path);
122 }
123 g_free(question_tag);
124 return path;
125 }
126
127 /** Display a dialog with the path of the saved screenshot.
128 *
129 * @param fe cdebconf frontend
130 * @param screenshot_path path of the screenshot
131 */
132 static void popup_success(struct frontend * fe, const gchar * screenshot_path)
133 {
134 gchar * title;
135 char * template;
136 gchar * message;
137
138 /* XXX: the translated button label might not be suitable for a title
139 * in all translations... should use a different template. */
140 title = cdebconf_gtk_get_text(fe, "debconf/gtk-button-screenshot",
141 "Screenshot");
142 template = cdebconf_gtk_get_text(fe, "debconf/gtk-screenshot-saved",
143 "Screenshot saved as %s");
144 message = g_strdup_printf(template, screenshot_path);
145
146 if (!cdebconf_gtk_run_message_dialog(fe, title, message)) {
147 g_warning("cdebconf_gtk_run_message_dialog failed.");
148 }
149 }
150
151 /** Take a screenshot of the current frontend display and inform the user
152 * of the sucess of the operation.
153 *
154 * @todo This should also display a message in case of failure.
155 *
156 * @param fe cdebconf frontend
157 * @param widget the main window
158 */
159 static void do_screenshot(struct frontend * fe, GtkWidget * widget)
160 {
161 gchar * screenshot_path;
162 gboolean success;
163
164 screenshot_path = create_screenshot_path(fe->questions);
165 success = save_screenshot(gtk_widget_get_window(widget), screenshot_path);
166 if (success) {
167 popup_success(fe, screenshot_path);
168 } else {
169 g_warning("save_screenshot failed.");
170 }
171 g_free(screenshot_path);
172 }
173
174 /** Create a "Screenshot" button in the action box.
175 *
176 * The button is configured as "secondary": it will not be displayed
177 * near other buttons in a GtkButtonBox.
178 *
179 * @param fe cdebconf frontend
180 * @return the newly created button
181 */
182 GtkWidget * cdebconf_gtk_create_screenshot_button(struct frontend * fe)
183 {
184 struct frontend_data * fe_data = fe->data;
185 GtkWidget * action_box = fe_data->action_box;
186 GtkWidget * button;
187 char * label;
188
189 label = cdebconf_gtk_get_text(fe, "debconf/gtk-button-screenshot",
190 "Screenshot");
191 button = gtk_button_new_with_label(label);
192 g_free(label);
193
194 g_signal_connect_swapped(G_OBJECT(button), "clicked",
195 G_CALLBACK(do_screenshot), fe);
196
197 gtk_box_pack_start(GTK_BOX(action_box), button,
198 TRUE /* expand */, TRUE /* fill */, DEFAULT_PADDING);
199 gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(action_box),
200 button, TRUE);
201 /* Remove the screenshot button from the focus chain. */
202 gtk_container_set_focus_chain(GTK_CONTAINER(action_box), NULL);
203
204 return button;
205 }
206
207 /* vim: et sw=4 si
208 */