/[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 3177 by schoenebeck, Thu May 11 20:59:46 2017 UTC
# Line 1  Line 1 
1  /*                                                         -*- c++ -*-  /*                                                         -*- c++ -*-
2   * Copyright (C) 2007-2013 Andreas Persson   * Copyright (C) 2007-2017 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    
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>  #include <sstream>
43    #include <map>
44    
45    #ifdef LIBGIG_HEADER_FILE
46    # include LIBGIG_HEADER_FILE(gig.h)
47    # include LIBGIG_HEADER_FILE(Serialization.h)
48    #else
49    # include <gig.h>
50    # include <Serialization.h>
51    #endif
52    
53  //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?
54  #if (HAVE_GETTEXT || defined(__APPLE__))  #if (HAVE_GETTEXT || defined(__APPLE__))
# Line 40  Line 64 
64  # define VERSION VER_STRING // VER_STRING defined in libgig_private.h  # define VERSION VER_STRING // VER_STRING defined in libgig_private.h
65  #endif // WIN32  #endif // WIN32
66    
67    #define UNICODE_RIGHT_ARROW     Glib::ustring(1, gunichar(0x2192))
68    #define UNICODE_LEFT_ARROW      Glib::ustring(1, gunichar(0x2190))
69    #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    
84    // 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    #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  template<class T> inline std::string ToString(T o) {  template<class T> inline std::string ToString(T o) {
120      std::stringstream ss;      std::stringstream ss;
121      ss << o;      ss << o;
122      return ss.str();      return ss.str();
123  }  }
124    
125    inline static bool endsWith(const std::string& haystack, const std::string& needle, bool caseSensitive) {
126        if (haystack.size() < needle.size()) return false;
127        const std::string sub = haystack.substr(haystack.size() - needle.size(), needle.size());
128        return (caseSensitive) ? (sub == needle) : (!strcasecmp(sub.c_str(), needle.c_str()));
129    }
130    
131    inline int getDimensionIndex(gig::dimension_t type, gig::Region* rgn) {
132        for (uint i = 0; i < rgn->Dimensions; ++i)
133            if (rgn->pDimensionDefinitions[i].dimension == type)
134                return i;
135        return -1;
136    }
137    
138    inline int getDimensionRegionIndex(gig::DimensionRegion* dr) {
139        if (!dr) return -1;
140        gig::Region* rgn = (gig::Region*)dr->GetParent();
141        for (uint i = 0; i < 256; ++i)
142            if (rgn->pDimensionRegions[i] == dr)
143                return i;
144        return -1;
145    }
146    
147    /// Find the number of bits required to hold the specified amount of zones.
148    inline int zoneCountToBits(int nZones) {
149        if (!nZones) return 0;
150        int iFinalBits = 0;
151        int zoneBits = nZones - 1;
152        for (; zoneBits > 1; iFinalBits += 2, zoneBits >>= 2);
153        iFinalBits += zoneBits;
154        return iFinalBits;
155    }
156    
157    /**
158     * Returns the sum of all bits of all dimensions defined before the given
159     * dimensions (@a type). This allows to access cases of that particular
160     * dimension directly. If the supplied dimension @a type does not exist in the
161     * the supplied @a region, then this function returns -1 instead!
162     *
163     * @param type - dimension that shall be used
164     * @param rgn - parent region of that dimension
165     */
166    inline int baseBits(gig::dimension_t type, gig::Region* rgn) {
167        int previousBits = 0;
168        for (uint i = 0; i < rgn->Dimensions; ++i) {
169            if (rgn->pDimensionDefinitions[i].dimension == type) return previousBits;
170            previousBits += rgn->pDimensionDefinitions[i].bits;
171        }
172        return -1;
173    }
174    
175    // key: dimension type, value: dimension's zone index
176    class DimensionCase : public std::map<gig::dimension_t,int> {
177    public:
178        bool isViolating(const DimensionCase& c) const {
179            for (DimensionCase::const_iterator it = begin(); it != end(); ++it) {
180                if (c.find(it->first) == c.end()) continue;
181                if (c.find(it->first)->second != it->second) return true;
182            }
183            return false;
184        }
185    
186        // prevent passing gig::dimension_none from creating a new pair
187        // (TODO: other invalid gig::dimension_t values should be filtered here as well)
188        int& operator[](const gig::dimension_t& k) {
189            static int unused = 0;
190            if (k == gig::dimension_none) {
191                unused = 0;
192                return unused;
193            }
194            return std::map<gig::dimension_t,int>::operator[](k);
195        }
196    };
197    
198    //TODO: this function and caseOfDimRegion() from dimregionchooser.h are duplicates, eliminate either one of them!
199    inline DimensionCase dimensionCaseOf(gig::DimensionRegion* dr) {
200        DimensionCase dimCase;
201        int idr = getDimensionRegionIndex(dr);
202        if (idr < 0) return dimCase;
203        gig::Region* rgn = (gig::Region*)dr->GetParent();
204        int bitpos = 0;
205        for (int d = 0; d < rgn->Dimensions; ++d) {
206            const gig::dimension_def_t& dimdef = rgn->pDimensionDefinitions[d];
207            const int zone = (idr >> bitpos) & ((1 << dimdef.bits) - 1);
208            dimCase[dimdef.dimension] = zone;
209            bitpos += rgn->pDimensionDefinitions[d].bits;
210        }
211        return dimCase;
212    }
213    
214    inline std::vector<gig::DimensionRegion*> dimensionRegionsMatching(const DimensionCase& dimCase, gig::Region* rgn) {
215        std::vector<gig::DimensionRegion*> v;
216        for (int idr = 0; idr < 256; ++idr) {
217            if (!rgn->pDimensionRegions[idr]) continue;
218            DimensionCase c = dimensionCaseOf(rgn->pDimensionRegions[idr]);
219            if (!dimCase.isViolating(c)) v.push_back(rgn->pDimensionRegions[idr]);
220        }
221        return v;
222    }
223    
224    inline gig::DimensionRegion* dimensionRegionMatching(const DimensionCase& dimCase, gig::Region* rgn) {
225        for (int idr = 0; idr < 256; ++idr) {
226            if (!rgn->pDimensionRegions[idr]) continue;
227            DimensionCase c = dimensionCaseOf(rgn->pDimensionRegions[idr]);
228            if (c == dimCase) return rgn->pDimensionRegions[idr];
229        }
230        return NULL;
231    }
232    
233    template<typename T_Message>
234    class SignalGuard {
235    public:
236        SignalGuard(sigc::signal<void, T_Message>& start, sigc::signal<void, T_Message>& end, T_Message message)
237            : m_end(end), m_message(message)
238        {
239            if (message) start.emit(message);
240        }
241    
242        virtual ~SignalGuard() {
243            if (m_message) m_end.emit(m_message);
244        }
245    protected:
246        sigc::signal<void, T_Message>& m_end;
247        T_Message m_message;
248    };
249    
250  #endif // GIGEDIT_GLOBAL_H  #endif // GIGEDIT_GLOBAL_H

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

  ViewVC Help
Powered by ViewVC