1 // Copyright (c) 2005-2009, Rodrigo Braz Monteiro, Niels Martin Hansen
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //   * Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //   * Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //   * Neither the name of the Aegisub Group nor the names of its contributors
13 //     may be used to endorse or promote products derived from this software
14 //     without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 //
28 // Aegisub Project http://www.aegisub.org/
29 
30 #pragma once
31 
32 #include <libaegisub/fs_fwd.h>
33 
34 #include <cstdint>
35 #include <string>
36 
37 #include <wx/bitmap.h>
38 #include <wx/string.h>
39 
40 class wxKeyEvent;
41 class wxMouseEvent;
42 class wxStyledTextCtrl;
43 class wxWindow;
44 
45 wxString PrettySize(int bytes);
46 
47 std::string float_to_string(double val);
48 
49 /// @brief Get the smallest power of two that is greater or equal to x
50 ///
51 /// Algorithm from http://bob.allegronetwork.com/prog/tricks.html
52 int SmallestPowerOf2(int x);
53 
54 /// @brief Launch a new copy of Aegisub.
55 ///
56 /// Contrary to what the name suggests, this does not close the currently
57 /// running process.
58 void RestartAegisub();
59 
60 /// Add the OS X 10.7+ full-screen button to a window
61 void AddFullScreenButton(wxWindow *window);
62 
63 void SetFloatOnParent(wxWindow *window);
64 
65 /// Forward a mouse wheel event to the window under the mouse if needed
66 /// @param source The initial target of the wheel event
67 /// @param evt The event
68 /// @return Should the calling code process the event?
69 bool ForwardMouseWheelEvent(wxWindow *source, wxMouseEvent &evt);
70 
71 /// Clean up the given cache directory, limiting the size to max_size
72 /// @param directory Directory to clean
73 /// @param file_type Wildcard pattern for files to clean up
74 /// @param max_size Maximum size of directory in MB
75 /// @param max_files Maximum number of files
76 void CleanCache(agi::fs::path const& directory, std::string const& file_type, uint64_t max_size, uint64_t max_files = -1);
77 
78 /// @brief Templated abs() function
tabs(T x)79 template <typename T> T tabs(T x) { return x < 0 ? -x : x; }
80 
81 /// Get the middle value of a, b, and c (i.e. clamp b to [a,c])
82 /// @precondition a <= c
mid(T a,T b,T c)83 template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); }
84 
85 /// Get the text contents of the clipboard, or empty string on failure
86 std::string GetClipboard();
87 /// Try to set the clipboard to the given string
88 void SetClipboard(std::string const& new_value);
89 void SetClipboard(wxBitmap const& new_value);
90 
91 #define countof(array) (sizeof(array) / sizeof(array[0]))
92 
93 wxString FontFace(std::string opt_prefix);
94 
95 agi::fs::path OpenFileSelector(wxString const& message, std::string const& option_name, std::string const& default_filename, std::string const& default_extension, wxString const& wildcard, wxWindow *parent);
96 agi::fs::path SaveFileSelector(wxString const& message, std::string const& option_name, std::string const& default_filename, std::string const& default_extension, wxString const& wildcard, wxWindow *parent);
97 
98 wxString LocalizedLanguageName(wxString const& lang);
99 
100 namespace osx { namespace ime {
101 	/// Inject the IME helper into the given wxSTC
102 	void inject(wxStyledTextCtrl *ctrl);
103 	/// Invalidate any pending text from the IME
104 	void invalidate(wxStyledTextCtrl *ctrl);
105 	/// Give the IME a chance to process a key event and return whether it did
106 	bool process_key_event(wxStyledTextCtrl *ctrl, wxKeyEvent &);
107 } }
108