1 |
schoenebeck |
1396 |
/* -*- c++ -*- |
2 |
schoenebeck |
3089 |
* Copyright (C) 2007-2017 Andreas Persson |
3 |
schoenebeck |
1396 |
* |
4 |
|
|
* This program is free software; you can redistribute it and/or |
5 |
|
|
* modify it under the terms of the GNU General Public License as |
6 |
|
|
* published by the Free Software Foundation; either version 2, or (at |
7 |
|
|
* your option) any later version. |
8 |
|
|
* |
9 |
|
|
* This program is distributed in the hope that it will be useful, but |
10 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
|
|
* General Public License for more details. |
13 |
|
|
* |
14 |
|
|
* You should have received a copy of the GNU General Public License |
15 |
|
|
* along with program; see the file COPYING. If not, write to the Free |
16 |
|
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
17 |
|
|
* 02110-1301 USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#ifndef GIGEDIT_GLOBAL_H |
21 |
|
|
#define GIGEDIT_GLOBAL_H |
22 |
|
|
|
23 |
|
|
#if HAVE_CONFIG_H |
24 |
|
|
# include <config.h> |
25 |
|
|
#endif |
26 |
|
|
|
27 |
schoenebeck |
3151 |
#include <cstring> |
28 |
|
|
|
29 |
|
|
#include <glibmmconfig.h> |
30 |
|
|
// threads.h must be included first to be able to build with |
31 |
|
|
// G_DISABLE_DEPRECATED |
32 |
|
|
#if (GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION == 31 && GLIBMM_MICRO_VERSION >= 2) || \ |
33 |
|
|
(GLIBMM_MAJOR_VERSION == 2 && GLIBMM_MINOR_VERSION > 31) || GLIBMM_MAJOR_VERSION > 2 |
34 |
|
|
#include <glibmm/threads.h> |
35 |
|
|
#endif |
36 |
|
|
|
37 |
schoenebeck |
2967 |
#if !defined(WIN32) |
38 |
|
|
# include <unistd.h> |
39 |
|
|
# include <errno.h> |
40 |
|
|
#endif |
41 |
|
|
|
42 |
persson |
2442 |
#include <sstream> |
43 |
schoenebeck |
3089 |
#include <map> |
44 |
persson |
2442 |
|
45 |
schoenebeck |
3089 |
#ifdef LIBGIG_HEADER_FILE |
46 |
|
|
# include LIBGIG_HEADER_FILE(gig.h) |
47 |
schoenebeck |
3157 |
# include LIBGIG_HEADER_FILE(Serialization.h) |
48 |
schoenebeck |
3089 |
#else |
49 |
|
|
# include <gig.h> |
50 |
schoenebeck |
3157 |
# include <Serialization.h> |
51 |
schoenebeck |
3089 |
#endif |
52 |
|
|
|
53 |
schoenebeck |
2474 |
//FIXME: for some reason AC GETTEXT check fails on the Mac cross compiler? |
54 |
|
|
#if (HAVE_GETTEXT || defined(__APPLE__)) |
55 |
schoenebeck |
1396 |
# include <libintl.h> |
56 |
|
|
# define _(String) gettext(String) |
57 |
|
|
#else |
58 |
|
|
# define _(String) String |
59 |
|
|
#endif |
60 |
|
|
|
61 |
|
|
#if defined(WIN32) && !HAVE_CONFIG_H |
62 |
|
|
# include "../../win32/libgigedit_private.h" // like config.h, automatically generated by Dev-C++ |
63 |
|
|
# define PACKAGE "gigedit" |
64 |
|
|
# define VERSION VER_STRING // VER_STRING defined in libgig_private.h |
65 |
|
|
#endif // WIN32 |
66 |
|
|
|
67 |
schoenebeck |
2692 |
#define UNICODE_RIGHT_ARROW Glib::ustring(1, gunichar(0x2192)) |
68 |
|
|
#define UNICODE_LEFT_ARROW Glib::ustring(1, gunichar(0x2190)) |
69 |
schoenebeck |
3155 |
#define UNICODE_SHIFT_KEY_SYMBOL Glib::ustring("\xe2\x87\xa7") |
70 |
|
|
#if defined(__APPLE__) |
71 |
|
|
# define UNICODE_ALT_KEY_SYMBOL Glib::ustring("\xe2\x8c\xa5") |
72 |
|
|
#else |
73 |
|
|
# define UNICODE_ALT_KEY_SYMBOL Glib::ustring("Alt") |
74 |
|
|
#endif |
75 |
|
|
#define UNICODE_ERASE_KEY_SYMBOL Glib::ustring("\xe2\x8c\xab") |
76 |
|
|
#define UNICODE_CTRL_KEY_SYMBOL Glib::ustring("Ctrl") |
77 |
|
|
#define UNICODE_CMD_KEY_SYMBOL Glib::ustring(1, gunichar(0x2318)) |
78 |
|
|
#if defined(__APPLE__) |
79 |
|
|
# define UNICODE_PRIMARY_KEY_SYMBOL UNICODE_CMD_KEY_SYMBOL |
80 |
|
|
#else |
81 |
|
|
# define UNICODE_PRIMARY_KEY_SYMBOL UNICODE_CTRL_KEY_SYMBOL |
82 |
|
|
#endif |
83 |
schoenebeck |
2692 |
|
84 |
schoenebeck |
3123 |
// taken from gdk/gdkkeysyms.h |
85 |
|
|
// (define on demand, to avoid unnecessary dev lib package build dependency) |
86 |
|
|
#ifndef GDK_KEY_Control_L |
87 |
|
|
# define GDK_KEY_Control_L 0xffe3 |
88 |
|
|
#endif |
89 |
|
|
#ifndef GDK_KEY_Control_R |
90 |
|
|
# define GDK_KEY_Control_R 0xffe4 |
91 |
|
|
#endif |
92 |
|
|
#ifndef GDK_KEY_Left |
93 |
|
|
# define GDK_KEY_Left 0xff51 |
94 |
|
|
#endif |
95 |
|
|
#ifndef GDK_KEY_Right |
96 |
|
|
# define GDK_KEY_Right 0xff53 |
97 |
|
|
#endif |
98 |
|
|
#ifndef GDK_KEY_Up |
99 |
|
|
# define GDK_KEY_Up 0xff52 |
100 |
|
|
#endif |
101 |
|
|
#ifndef GDK_KEY_Down |
102 |
|
|
# define GDK_KEY_Down 0xff54 |
103 |
|
|
#endif |
104 |
|
|
|
105 |
schoenebeck |
3151 |
#include <glibmm/convert.h> |
106 |
|
|
|
107 |
|
|
#define GIG_STR_ENCODING "CP1252" |
108 |
|
|
|
109 |
|
|
static inline |
110 |
|
|
Glib::ustring gig_to_utf8(const gig::String& gig_string) { |
111 |
|
|
return Glib::convert_with_fallback(gig_string, "UTF-8", GIG_STR_ENCODING, "?"); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
static inline |
115 |
|
|
gig::String gig_from_utf8(const Glib::ustring& utf8_string) { |
116 |
|
|
return Glib::convert_with_fallback(utf8_string, GIG_STR_ENCODING, "UTF-8", "?"); |
117 |
|
|
} |
118 |
|
|
|
119 |
schoenebeck |
3340 |
inline Glib::ustring ltrim(Glib::ustring s) { |
120 |
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); |
121 |
|
|
return s; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
inline Glib::ustring rtrim(Glib::ustring s) { |
125 |
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end()); |
126 |
|
|
return s; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
inline Glib::ustring trim(Glib::ustring s) { |
130 |
|
|
return ltrim(rtrim(s)); |
131 |
|
|
} |
132 |
|
|
|
133 |
persson |
2442 |
template<class T> inline std::string ToString(T o) { |
134 |
|
|
std::stringstream ss; |
135 |
|
|
ss << o; |
136 |
|
|
return ss.str(); |
137 |
|
|
} |
138 |
|
|
|
139 |
schoenebeck |
3176 |
inline static bool endsWith(const std::string& haystack, const std::string& needle, bool caseSensitive) { |
140 |
|
|
if (haystack.size() < needle.size()) return false; |
141 |
|
|
const std::string sub = haystack.substr(haystack.size() - needle.size(), needle.size()); |
142 |
|
|
return (caseSensitive) ? (sub == needle) : (!strcasecmp(sub.c_str(), needle.c_str())); |
143 |
|
|
} |
144 |
|
|
|
145 |
schoenebeck |
3089 |
inline int getDimensionIndex(gig::dimension_t type, gig::Region* rgn) { |
146 |
|
|
for (uint i = 0; i < rgn->Dimensions; ++i) |
147 |
|
|
if (rgn->pDimensionDefinitions[i].dimension == type) |
148 |
|
|
return i; |
149 |
|
|
return -1; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
inline int getDimensionRegionIndex(gig::DimensionRegion* dr) { |
153 |
|
|
if (!dr) return -1; |
154 |
|
|
gig::Region* rgn = (gig::Region*)dr->GetParent(); |
155 |
|
|
for (uint i = 0; i < 256; ++i) |
156 |
|
|
if (rgn->pDimensionRegions[i] == dr) |
157 |
|
|
return i; |
158 |
|
|
return -1; |
159 |
|
|
} |
160 |
|
|
|
161 |
schoenebeck |
2548 |
/// Find the number of bits required to hold the specified amount of zones. |
162 |
|
|
inline int zoneCountToBits(int nZones) { |
163 |
schoenebeck |
2604 |
if (!nZones) return 0; |
164 |
schoenebeck |
2548 |
int iFinalBits = 0; |
165 |
|
|
int zoneBits = nZones - 1; |
166 |
|
|
for (; zoneBits > 1; iFinalBits += 2, zoneBits >>= 2); |
167 |
|
|
iFinalBits += zoneBits; |
168 |
|
|
return iFinalBits; |
169 |
|
|
} |
170 |
|
|
|
171 |
schoenebeck |
3089 |
/** |
172 |
|
|
* Returns the sum of all bits of all dimensions defined before the given |
173 |
|
|
* dimensions (@a type). This allows to access cases of that particular |
174 |
|
|
* dimension directly. If the supplied dimension @a type does not exist in the |
175 |
|
|
* the supplied @a region, then this function returns -1 instead! |
176 |
|
|
* |
177 |
|
|
* @param type - dimension that shall be used |
178 |
|
|
* @param rgn - parent region of that dimension |
179 |
|
|
*/ |
180 |
|
|
inline int baseBits(gig::dimension_t type, gig::Region* rgn) { |
181 |
|
|
int previousBits = 0; |
182 |
|
|
for (uint i = 0; i < rgn->Dimensions; ++i) { |
183 |
|
|
if (rgn->pDimensionDefinitions[i].dimension == type) return previousBits; |
184 |
|
|
previousBits += rgn->pDimensionDefinitions[i].bits; |
185 |
|
|
} |
186 |
|
|
return -1; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
// key: dimension type, value: dimension's zone index |
190 |
|
|
class DimensionCase : public std::map<gig::dimension_t,int> { |
191 |
|
|
public: |
192 |
|
|
bool isViolating(const DimensionCase& c) const { |
193 |
|
|
for (DimensionCase::const_iterator it = begin(); it != end(); ++it) { |
194 |
|
|
if (c.find(it->first) == c.end()) continue; |
195 |
|
|
if (c.find(it->first)->second != it->second) return true; |
196 |
|
|
} |
197 |
|
|
return false; |
198 |
|
|
} |
199 |
schoenebeck |
3123 |
|
200 |
|
|
// prevent passing gig::dimension_none from creating a new pair |
201 |
|
|
// (TODO: other invalid gig::dimension_t values should be filtered here as well) |
202 |
|
|
int& operator[](const gig::dimension_t& k) { |
203 |
|
|
static int unused = 0; |
204 |
|
|
if (k == gig::dimension_none) { |
205 |
|
|
unused = 0; |
206 |
|
|
return unused; |
207 |
|
|
} |
208 |
|
|
return std::map<gig::dimension_t,int>::operator[](k); |
209 |
|
|
} |
210 |
schoenebeck |
3089 |
}; |
211 |
|
|
|
212 |
schoenebeck |
3123 |
//TODO: this function and caseOfDimRegion() from dimregionchooser.h are duplicates, eliminate either one of them! |
213 |
schoenebeck |
3089 |
inline DimensionCase dimensionCaseOf(gig::DimensionRegion* dr) { |
214 |
|
|
DimensionCase dimCase; |
215 |
|
|
int idr = getDimensionRegionIndex(dr); |
216 |
|
|
if (idr < 0) return dimCase; |
217 |
|
|
gig::Region* rgn = (gig::Region*)dr->GetParent(); |
218 |
|
|
int bitpos = 0; |
219 |
|
|
for (int d = 0; d < rgn->Dimensions; ++d) { |
220 |
|
|
const gig::dimension_def_t& dimdef = rgn->pDimensionDefinitions[d]; |
221 |
|
|
const int zone = (idr >> bitpos) & ((1 << dimdef.bits) - 1); |
222 |
|
|
dimCase[dimdef.dimension] = zone; |
223 |
|
|
bitpos += rgn->pDimensionDefinitions[d].bits; |
224 |
|
|
} |
225 |
|
|
return dimCase; |
226 |
|
|
} |
227 |
|
|
|
228 |
schoenebeck |
3307 |
/** |
229 |
|
|
* Checks whether the passed dimension zones are within the boundaries of the |
230 |
|
|
* defined dimensions. This is especially relevant if there are dimensions |
231 |
|
|
* defined with an amount not equal to a power of two, in that case there are |
232 |
|
|
* unused dimensions regions which should be ignored. |
233 |
|
|
*/ |
234 |
|
|
inline bool isUsedCase(const DimensionCase& c, gig::Region* rgn) { |
235 |
|
|
for (int d = 0; d < rgn->Dimensions; ++d) { |
236 |
|
|
gig::dimension_t type = rgn->pDimensionDefinitions[d].dimension; |
237 |
|
|
if (c.find(type) == c.end()) continue; |
238 |
|
|
int zone = c.find(type)->second; |
239 |
|
|
if (zone < 0 || zone >= rgn->pDimensionDefinitions[d].zones) |
240 |
|
|
return false; |
241 |
|
|
} |
242 |
|
|
return true; |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
inline std::vector<gig::DimensionRegion*> dimensionRegionsMatching( |
246 |
|
|
const DimensionCase& dimCase, gig::Region* rgn, bool skipUnusedZones = false) |
247 |
|
|
{ |
248 |
schoenebeck |
3089 |
std::vector<gig::DimensionRegion*> v; |
249 |
|
|
for (int idr = 0; idr < 256; ++idr) { |
250 |
|
|
if (!rgn->pDimensionRegions[idr]) continue; |
251 |
|
|
DimensionCase c = dimensionCaseOf(rgn->pDimensionRegions[idr]); |
252 |
schoenebeck |
3307 |
if (dimCase.isViolating(c)) continue; |
253 |
|
|
if (skipUnusedZones && !isUsedCase(c, rgn)) continue; |
254 |
|
|
v.push_back(rgn->pDimensionRegions[idr]); |
255 |
schoenebeck |
3089 |
} |
256 |
|
|
return v; |
257 |
|
|
} |
258 |
|
|
|
259 |
schoenebeck |
3123 |
inline gig::DimensionRegion* dimensionRegionMatching(const DimensionCase& dimCase, gig::Region* rgn) { |
260 |
|
|
for (int idr = 0; idr < 256; ++idr) { |
261 |
|
|
if (!rgn->pDimensionRegions[idr]) continue; |
262 |
|
|
DimensionCase c = dimensionCaseOf(rgn->pDimensionRegions[idr]); |
263 |
|
|
if (c == dimCase) return rgn->pDimensionRegions[idr]; |
264 |
|
|
} |
265 |
|
|
return NULL; |
266 |
|
|
} |
267 |
|
|
|
268 |
schoenebeck |
3177 |
template<typename T_Message> |
269 |
|
|
class SignalGuard { |
270 |
|
|
public: |
271 |
|
|
SignalGuard(sigc::signal<void, T_Message>& start, sigc::signal<void, T_Message>& end, T_Message message) |
272 |
|
|
: m_end(end), m_message(message) |
273 |
|
|
{ |
274 |
|
|
if (message) start.emit(message); |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
virtual ~SignalGuard() { |
278 |
|
|
if (m_message) m_end.emit(m_message); |
279 |
|
|
} |
280 |
|
|
protected: |
281 |
|
|
sigc::signal<void, T_Message>& m_end; |
282 |
|
|
T_Message m_message; |
283 |
|
|
}; |
284 |
|
|
|
285 |
schoenebeck |
1396 |
#endif // GIGEDIT_GLOBAL_H |