The Battle for Wesnoth  1.15.2+dev
screenshot_notification.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2018 by Iris Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "desktop/clipboard.hpp"
20 #include "desktop/open.hpp"
21 #include "display.hpp"
22 #include "filesystem.hpp"
25 #include "gui/dialogs/message.hpp"
26 #include "gui/widgets/button.hpp"
27 #include "gui/widgets/label.hpp"
28 #include "gui/widgets/settings.hpp"
29 #include "gui/widgets/text_box.hpp"
30 #include "gui/widgets/window.hpp"
31 #include "picture.hpp"
32 
33 #include "utils/functional.hpp"
34 
35 #include "gettext.hpp"
36 
37 #include <boost/filesystem.hpp>
38 #include <stdexcept>
39 
40 namespace gui2
41 {
42 namespace dialogs
43 {
44 
45 /*WIKI
46  * @page = GUIWindowDefinitionWML
47  * @order = 2_screenshot_notification
48  *
49  * == Screenshot notification ==
50  *
51  * Notification dialog used after saving a game or map screenshot to display
52  * information about it for the user.
53  *
54  * @begin{table}{dialog_widgets}
55  *
56  * path & & text_box & m &
57  * Read-only textbox containing the screenshot path. $
58  *
59  * filesize & & label & o &
60  * Optional label to display the file size. $
61  *
62  * copy & & button & m &
63  * Button to copy the path to clipboard. $
64  *
65  * open & & button & m &
66  * Button to open the screnshot using the default application. $
67  *
68  * browse_dir & & button & m &
69  * Button to browse the screenshots directory in the file manager. $
70  *
71  * @end{table}
72  */
73 
74 REGISTER_DIALOG(screenshot_notification)
75 
77  : path_(path)
78  , screenshots_dir_path_(filesystem::get_screenshot_dir())
79  , screenshot_(screenshot)
80 {
81 
82 }
83 
85 {
86  window.set_enter_disabled(true);
87 
88  text_box& path_box = find_widget<text_box>(&window, "path", false);
90  window.keyboard_capture(&path_box);
92  std::placeholders::_3, std::placeholders::_5));
93 
94  find_widget<label>(&window, "filesize", false).set_label(font::unicode_em_dash);
95 
96  button& copy_b = find_widget<button>(&window, "copy", false);
98  copy_b, std::bind(&desktop::clipboard::copy_to_clipboard, std::ref(path_), false));
99  copy_b.set_active(false);
100 
102  copy_b.set_tooltip(_("Clipboard support not found, contact your packager"));
103  }
104 
105  button& open_b = find_widget<button>(&window, "open", false);
107  open_b, std::bind(&desktop::open_object, std::ref(path_)));
108  open_b.set_active(false);
109 
110  button& bdir_b = find_widget<button>(&window, "browse_dir", false);
112  bdir_b,
113  std::bind(&desktop::open_object,
114  std::ref(screenshots_dir_path_)));
115 
116  button& save_b = find_widget<button>(&window, "save", false);
118 }
119 
121 {
122  window& window = *get_window();
123  text_box& path_box = find_widget<text_box>(&window, "path", false);
124  std::string filename = path_box.get_value();
126  path /= filename;
127 
128  image::save_result res = image::save_image(screenshot_, path.string());
130  gui2::show_error_message(_("Unsupported image format.\n\n"
131  "Try to save the screenshot as PNG instead."));
132  } else if(res == image::save_result::save_failed) {
134  translation::dsgettext("wesnoth", "Screenshot creation failed.\n\n"
135  "Make sure there is enough space on the drive holding Wesnoth’s player resource files and that file permissions are set up correctly."));
136  } else if(res != image::save_result::success) {
137  throw std::logic_error("Unexpected error while trying to save a screenshot");
138  } else {
139  path_box.set_active(false);
140  find_widget<button>(&window, "open", false).set_active(true);
141  find_widget<button>(&window, "save", false).set_active(false);
142 
144  find_widget<button>(&window, "copy", false).set_active(true);
145  }
146 
147  const int filesize = filesystem::file_size(path.string());
148  const std::string sizetext = utils::si_string(filesize, true, _("unit_byte^B"));
149  find_widget<label>(&window, "filesize", false).set_label(sizetext);
150  }
151 }
152 
153 void screenshot_notification::keypress_callback(bool& handled, SDL_Keycode key)
154 {
155  if(key == SDLK_RETURN || key == SDLK_KP_ENTER) {
156  save_screenshot();
157  handled = true;
158  }
159 }
160 
161 } // namespace dialogs
162 } // namespace gui2
bool available()
Whether wesnoth was compiled with support for a clipboard.
Definition: clipboard.cpp:56
save_result
Definition: picture.hpp:220
std::string get_value() const
This file contains the window object, this object is a top level container which has the event manage...
std::string get_screenshot_dir()
save_result save_image(const locator &i_locator, const std::string &filename)
Definition: picture.cpp:1143
STL namespace.
window * get_window() const
Returns a pointer to the dialog&#39;s window.
std::string dsgettext(const char *domainname, const char *msgid)
Definition: gettext.cpp:404
Class for a single line text area.
Definition: text_box.hpp:121
Generic file dialog.
Definition: field-fwd.hpp:22
Desktop environment interaction functions.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
void set_tooltip(const t_string &tooltip)
This file contains the settings handling of the widget library.
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:233
std::string path
Definition: game_config.cpp:39
map_display and display: classes which take care of displaying the map and game-data on the screen...
Various uncategorised dialogs.
bool open_object(const std::string &path_or_url)
Opens the specified object with the default application configured for its type.
Definition: open.cpp:55
const std::string path_
The display function.
Declarations for File-IO.
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
int file_size(const std::string &fname)
Returns the size of a file, or -1 if the file doesn&#39;t exist.
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:62
void copy_to_clipboard(const std::string &text, const bool)
Copies text to the clipboard.
Definition: clipboard.cpp:35
void connect_signal_pre_key_press(dispatcher &dispatcher, const signal_keyboard_function &signal)
Connects the signal for &#39;snooping&#39; on the keypress.
Definition: dispatcher.cpp:228
const std::string unicode_em_dash
Definition: constants.cpp:40
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Simple push button.
Definition: button.hpp:35
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:205
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
virtual void set_active(const bool active) override
See styled_widget::set_active.
void keypress_callback(bool &handled, const SDL_Keycode key)
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:287