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