2009-01-04 12:45:06 +01:00
|
|
|
// Copyright (c) 2005-2009, Rodrigo Braz Monteiro, Niels Martin Hansen
|
2006-01-16 22:02:54 +01:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file utils.h
|
|
|
|
/// @see utils.cpp
|
|
|
|
/// @ingroup utility
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
|
2007-01-24 03:11:26 +01:00
|
|
|
#pragma once
|
2007-01-21 07:30:19 +01:00
|
|
|
|
2009-09-11 04:36:34 +02:00
|
|
|
#ifndef AGI_PRE
|
2007-08-31 16:11:35 +02:00
|
|
|
#include <stdint.h>
|
2007-04-14 17:26:46 +02:00
|
|
|
|
2010-08-03 00:14:11 +02:00
|
|
|
#include <algorithm>
|
2009-09-11 04:36:34 +02:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <wx/icon.h>
|
2012-02-01 01:47:28 +01:00
|
|
|
#include <wx/thread.h>
|
2009-09-11 04:36:34 +02:00
|
|
|
#endif
|
2007-04-14 17:26:46 +02:00
|
|
|
|
2011-10-28 22:15:10 +02:00
|
|
|
class wxMouseEvent;
|
|
|
|
class wxWindow;
|
|
|
|
|
2007-04-14 17:26:46 +02:00
|
|
|
typedef std::vector<std::pair<int,int> > IntPairVector;
|
|
|
|
|
2012-03-27 02:49:33 +02:00
|
|
|
/// @brief Make a path relative to reference
|
2006-01-16 22:02:54 +01:00
|
|
|
wxString MakeRelativePath(wxString path,wxString reference);
|
2012-03-27 02:49:33 +02:00
|
|
|
/// @brief Extract original path from relative
|
2006-01-16 22:02:54 +01:00
|
|
|
wxString DecodeRelativePath(wxString path,wxString reference);
|
2009-04-06 22:01:42 +02:00
|
|
|
wxString AegiFloatToString(double value);
|
|
|
|
wxString AegiIntegerToString(int value);
|
2006-07-01 04:27:37 +02:00
|
|
|
wxString PrettySize(int bytes);
|
2012-03-27 02:49:33 +02:00
|
|
|
|
|
|
|
/// @brief Get the smallest power of two that is greater or equal to x
|
|
|
|
///
|
|
|
|
/// Algorithm from http://bob.allegronetwork.com/prog/tricks.html
|
2007-01-21 07:30:19 +01:00
|
|
|
int SmallestPowerOf2(int x);
|
2012-03-27 02:49:33 +02:00
|
|
|
|
|
|
|
/// Get the indices in text which are the beginnings of words
|
|
|
|
/// @param text Text to split into words
|
|
|
|
/// @param[out] results Vector of indices which are the beginnings of words
|
|
|
|
/// @param start First index in text to check
|
|
|
|
/// @param end Last index in text to check, or -1 for end
|
|
|
|
///
|
|
|
|
/// This is ASS-specific and not a general purpose word boundary finder; words
|
|
|
|
/// within override blocks or drawing blocks are ignored
|
|
|
|
void GetWordBoundaries(wxString const& text, IntPairVector &results, int start=0, int end=-1);
|
|
|
|
|
|
|
|
/// Check if wchar 'c' is a whitespace character
|
2009-06-25 00:22:45 +02:00
|
|
|
bool IsWhitespace(wchar_t c);
|
2012-03-27 02:49:33 +02:00
|
|
|
|
|
|
|
/// Check if every character in str is whitespace
|
2009-06-25 00:22:45 +02:00
|
|
|
bool StringEmptyOrWhitespace(const wxString &str);
|
2012-03-27 02:49:33 +02:00
|
|
|
|
|
|
|
/// @brief String to integer
|
|
|
|
///
|
|
|
|
/// wxString::ToLong() is slow and not as flexible
|
2009-04-06 22:01:42 +02:00
|
|
|
int AegiStringToInt(const wxString &str,int start=0,int end=-1);
|
|
|
|
int AegiStringToFix(const wxString &str,size_t decimalPlaces,int start=0,int end=-1);
|
2012-03-27 02:49:33 +02:00
|
|
|
|
|
|
|
/// @brief Launch a new copy of Aegisub.
|
|
|
|
///
|
|
|
|
/// Contrary to what the name suggests, this does not close the currently
|
|
|
|
/// running process.
|
2009-01-04 12:45:06 +01:00
|
|
|
void RestartAegisub();
|
2007-01-21 07:30:19 +01:00
|
|
|
|
2012-05-26 22:16:08 +02:00
|
|
|
/// Add the OS X 10.7+ full-screen button to a window
|
|
|
|
void AddFullScreenButton(wxWindow *window);
|
|
|
|
|
2012-06-07 04:48:13 +02:00
|
|
|
void SetFloatOnParent(wxWindow *window);
|
|
|
|
|
2012-10-10 17:53:15 +02:00
|
|
|
void SetPlaceholderText(wxWindow *window, wxString const& placeholder);
|
|
|
|
|
2011-10-25 21:40:45 +02:00
|
|
|
/// Forward a mouse wheel event to the window under the mouse if needed
|
|
|
|
/// @param source The initial target of the wheel event
|
|
|
|
/// @param evt The event
|
|
|
|
/// @return Should the calling code process the event?
|
|
|
|
bool ForwardMouseWheelEvent(wxWindow *source, wxMouseEvent &evt);
|
|
|
|
|
2012-04-04 00:44:40 +02:00
|
|
|
/// Clean up the given cache directory, limiting the size to max_size
|
|
|
|
/// @param directory Directory to clean
|
|
|
|
/// @param file_type Wildcard pattern for files to clean up
|
|
|
|
/// @param max_size Maximum size of directory in MB
|
|
|
|
/// @param max_files Maximum number of files
|
|
|
|
void CleanCache(wxString const& directory, wxString const& file_type, int64_t max_size, int64_t max_files = -1);
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Templated abs() function
|
|
|
|
template <typename T> T tabs(T x) { return x < 0 ? -x : x; }
|
|
|
|
|
2012-03-27 02:49:33 +02:00
|
|
|
/// Get the middle value of a, b, and c (i.e. clamp b to [a,c])
|
|
|
|
/// @precondition a <= c
|
2010-12-31 22:03:03 +01:00
|
|
|
template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); }
|
2007-04-03 23:05:33 +02:00
|
|
|
|
2012-10-25 17:13:13 +02:00
|
|
|
/// Get the text contents of the clipboard, or empty string on failure
|
|
|
|
wxString GetClipboard();
|
|
|
|
/// Try to set the clipboard to the given string
|
|
|
|
void SetClipboard(wxString const& new_value);
|
|
|
|
void SetClipboard(wxBitmap const& new_value);
|
|
|
|
|
2007-04-03 23:05:33 +02:00
|
|
|
#ifndef FORCEINLINE
|
|
|
|
#ifdef __VISUALC__
|
|
|
|
#define FORCEINLINE __forceinline
|
|
|
|
#else
|
2007-04-21 00:17:42 +02:00
|
|
|
#define FORCEINLINE inline
|
|
|
|
// __attribute__((always_inline)) gives me errors on g++ ~amz
|
2007-04-03 23:05:33 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-05-10 16:18:47 +02:00
|
|
|
#define countof(array) (sizeof(array) / sizeof(array[0]))
|
|
|
|
|
2012-02-01 01:47:28 +01:00
|
|
|
/// Polymorphic delete functor
|
2010-06-24 03:24:43 +02:00
|
|
|
struct delete_ptr {
|
|
|
|
template<class T>
|
|
|
|
void operator()(T* ptr) const {
|
|
|
|
delete ptr;
|
|
|
|
}
|
|
|
|
};
|
2012-02-01 01:47:28 +01:00
|
|
|
|
|
|
|
/// Delete all of the items in a container of pointers and clear the container
|
2010-06-24 03:24:43 +02:00
|
|
|
template<class T>
|
|
|
|
void delete_clear(T& container) {
|
2012-02-01 01:47:28 +01:00
|
|
|
if (!container.empty()) {
|
|
|
|
std::for_each(container.begin(), container.end(), delete_ptr());
|
|
|
|
container.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper class for background_delete_clear
|
|
|
|
template<class Container>
|
|
|
|
class BackgroundDeleter : public wxThread {
|
|
|
|
Container cont;
|
|
|
|
wxThread::ExitCode Entry() {
|
|
|
|
delete_clear(cont);
|
|
|
|
return (wxThread::ExitCode)0;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
BackgroundDeleter(Container &source)
|
|
|
|
: wxThread(wxTHREAD_DETACHED)
|
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
swap(cont, source);
|
|
|
|
|
|
|
|
Create();
|
2012-02-01 15:23:52 +01:00
|
|
|
SetPriority(WXTHREAD_MIN_PRIORITY);
|
2012-02-01 01:47:28 +01:00
|
|
|
Run();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Clear a container of pointers and delete the pointed to members on a
|
|
|
|
/// background thread
|
|
|
|
template<class T>
|
|
|
|
void background_delete_clear(T& container) {
|
|
|
|
if (!container.empty())
|
|
|
|
new BackgroundDeleter<T>(container);
|
2010-06-24 03:24:43 +02:00
|
|
|
}
|
2010-07-12 09:10:40 +02:00
|
|
|
|
2012-02-01 01:47:28 +01:00
|
|
|
|
2010-07-12 09:10:40 +02:00
|
|
|
template<class Out>
|
|
|
|
struct cast {
|
|
|
|
template<class In>
|
|
|
|
Out operator()(In in) const {
|
|
|
|
return dynamic_cast<Out>(in);
|
|
|
|
}
|
|
|
|
};
|