/[svn]/gigedit/trunk/src/gigedit/global.h
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/global.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.2474  
changed lines
  Added in v.3462

  ViewVC Help
Powered by ViewVC