1 |
/* -*- c++ -*- |
2 |
* Copyright (C) 2007-2017 Andreas Persson |
3 |
* |
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 |
#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 |
#if !defined(WIN32) |
38 |
# include <unistd.h> |
39 |
# include <errno.h> |
40 |
#endif |
41 |
|
42 |
#include <sstream> |
43 |
#include <map> |
44 |
|
45 |
#ifdef LIBGIG_HEADER_FILE |
46 |
# include LIBGIG_HEADER_FILE(gig.h) |
47 |
#else |
48 |
# include <gig.h> |
49 |
#endif |
50 |
|
51 |
//FIXME: for some reason AC GETTEXT check fails on the Mac cross compiler? |
52 |
#if (HAVE_GETTEXT || defined(__APPLE__)) |
53 |
# include <libintl.h> |
54 |
# define _(String) gettext(String) |
55 |
#else |
56 |
# define _(String) String |
57 |
#endif |
58 |
|
59 |
#if defined(WIN32) && !HAVE_CONFIG_H |
60 |
# include "../../win32/libgigedit_private.h" // like config.h, automatically generated by Dev-C++ |
61 |
# define PACKAGE "gigedit" |
62 |
# define VERSION VER_STRING // VER_STRING defined in libgig_private.h |
63 |
#endif // WIN32 |
64 |
|
65 |
#define UNICODE_RIGHT_ARROW Glib::ustring(1, gunichar(0x2192)) |
66 |
#define UNICODE_LEFT_ARROW Glib::ustring(1, gunichar(0x2190)) |
67 |
|
68 |
// taken from gdk/gdkkeysyms.h |
69 |
// (define on demand, to avoid unnecessary dev lib package build dependency) |
70 |
#ifndef GDK_KEY_Control_L |
71 |
# define GDK_KEY_Control_L 0xffe3 |
72 |
#endif |
73 |
#ifndef GDK_KEY_Control_R |
74 |
# define GDK_KEY_Control_R 0xffe4 |
75 |
#endif |
76 |
#ifndef GDK_KEY_Left |
77 |
# define GDK_KEY_Left 0xff51 |
78 |
#endif |
79 |
#ifndef GDK_KEY_Right |
80 |
# define GDK_KEY_Right 0xff53 |
81 |
#endif |
82 |
#ifndef GDK_KEY_Up |
83 |
# define GDK_KEY_Up 0xff52 |
84 |
#endif |
85 |
#ifndef GDK_KEY_Down |
86 |
# define GDK_KEY_Down 0xff54 |
87 |
#endif |
88 |
|
89 |
#include <glibmm/convert.h> |
90 |
|
91 |
#define GIG_STR_ENCODING "CP1252" |
92 |
|
93 |
static inline |
94 |
Glib::ustring gig_to_utf8(const gig::String& gig_string) { |
95 |
return Glib::convert_with_fallback(gig_string, "UTF-8", GIG_STR_ENCODING, "?"); |
96 |
} |
97 |
|
98 |
static inline |
99 |
gig::String gig_from_utf8(const Glib::ustring& utf8_string) { |
100 |
return Glib::convert_with_fallback(utf8_string, GIG_STR_ENCODING, "UTF-8", "?"); |
101 |
} |
102 |
|
103 |
template<class T> inline std::string ToString(T o) { |
104 |
std::stringstream ss; |
105 |
ss << o; |
106 |
return ss.str(); |
107 |
} |
108 |
|
109 |
inline int getDimensionIndex(gig::dimension_t type, gig::Region* rgn) { |
110 |
for (uint i = 0; i < rgn->Dimensions; ++i) |
111 |
if (rgn->pDimensionDefinitions[i].dimension == type) |
112 |
return i; |
113 |
return -1; |
114 |
} |
115 |
|
116 |
inline int getDimensionRegionIndex(gig::DimensionRegion* dr) { |
117 |
if (!dr) return -1; |
118 |
gig::Region* rgn = (gig::Region*)dr->GetParent(); |
119 |
for (uint i = 0; i < 256; ++i) |
120 |
if (rgn->pDimensionRegions[i] == dr) |
121 |
return i; |
122 |
return -1; |
123 |
} |
124 |
|
125 |
/// Find the number of bits required to hold the specified amount of zones. |
126 |
inline int zoneCountToBits(int nZones) { |
127 |
if (!nZones) return 0; |
128 |
int iFinalBits = 0; |
129 |
int zoneBits = nZones - 1; |
130 |
for (; zoneBits > 1; iFinalBits += 2, zoneBits >>= 2); |
131 |
iFinalBits += zoneBits; |
132 |
return iFinalBits; |
133 |
} |
134 |
|
135 |
/** |
136 |
* Returns the sum of all bits of all dimensions defined before the given |
137 |
* dimensions (@a type). This allows to access cases of that particular |
138 |
* dimension directly. If the supplied dimension @a type does not exist in the |
139 |
* the supplied @a region, then this function returns -1 instead! |
140 |
* |
141 |
* @param type - dimension that shall be used |
142 |
* @param rgn - parent region of that dimension |
143 |
*/ |
144 |
inline int baseBits(gig::dimension_t type, gig::Region* rgn) { |
145 |
int previousBits = 0; |
146 |
for (uint i = 0; i < rgn->Dimensions; ++i) { |
147 |
if (rgn->pDimensionDefinitions[i].dimension == type) return previousBits; |
148 |
previousBits += rgn->pDimensionDefinitions[i].bits; |
149 |
} |
150 |
return -1; |
151 |
} |
152 |
|
153 |
// key: dimension type, value: dimension's zone index |
154 |
class DimensionCase : public std::map<gig::dimension_t,int> { |
155 |
public: |
156 |
bool isViolating(const DimensionCase& c) const { |
157 |
for (DimensionCase::const_iterator it = begin(); it != end(); ++it) { |
158 |
if (c.find(it->first) == c.end()) continue; |
159 |
if (c.find(it->first)->second != it->second) return true; |
160 |
} |
161 |
return false; |
162 |
} |
163 |
|
164 |
// prevent passing gig::dimension_none from creating a new pair |
165 |
// (TODO: other invalid gig::dimension_t values should be filtered here as well) |
166 |
int& operator[](const gig::dimension_t& k) { |
167 |
static int unused = 0; |
168 |
if (k == gig::dimension_none) { |
169 |
unused = 0; |
170 |
return unused; |
171 |
} |
172 |
return std::map<gig::dimension_t,int>::operator[](k); |
173 |
} |
174 |
}; |
175 |
|
176 |
//TODO: this function and caseOfDimRegion() from dimregionchooser.h are duplicates, eliminate either one of them! |
177 |
inline DimensionCase dimensionCaseOf(gig::DimensionRegion* dr) { |
178 |
DimensionCase dimCase; |
179 |
int idr = getDimensionRegionIndex(dr); |
180 |
if (idr < 0) return dimCase; |
181 |
gig::Region* rgn = (gig::Region*)dr->GetParent(); |
182 |
int bitpos = 0; |
183 |
for (int d = 0; d < rgn->Dimensions; ++d) { |
184 |
const gig::dimension_def_t& dimdef = rgn->pDimensionDefinitions[d]; |
185 |
const int zone = (idr >> bitpos) & ((1 << dimdef.bits) - 1); |
186 |
dimCase[dimdef.dimension] = zone; |
187 |
bitpos += rgn->pDimensionDefinitions[d].bits; |
188 |
} |
189 |
return dimCase; |
190 |
} |
191 |
|
192 |
inline std::vector<gig::DimensionRegion*> dimensionRegionsMatching(const DimensionCase& dimCase, gig::Region* rgn) { |
193 |
std::vector<gig::DimensionRegion*> v; |
194 |
for (int idr = 0; idr < 256; ++idr) { |
195 |
if (!rgn->pDimensionRegions[idr]) continue; |
196 |
DimensionCase c = dimensionCaseOf(rgn->pDimensionRegions[idr]); |
197 |
if (!dimCase.isViolating(c)) v.push_back(rgn->pDimensionRegions[idr]); |
198 |
} |
199 |
return v; |
200 |
} |
201 |
|
202 |
inline gig::DimensionRegion* dimensionRegionMatching(const DimensionCase& dimCase, gig::Region* rgn) { |
203 |
for (int idr = 0; idr < 256; ++idr) { |
204 |
if (!rgn->pDimensionRegions[idr]) continue; |
205 |
DimensionCase c = dimensionCaseOf(rgn->pDimensionRegions[idr]); |
206 |
if (c == dimCase) return rgn->pDimensionRegions[idr]; |
207 |
} |
208 |
return NULL; |
209 |
} |
210 |
|
211 |
#endif // GIGEDIT_GLOBAL_H |