/[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 2442 by persson, Sun Apr 14 07:29:59 2013 UTC revision 3123 by schoenebeck, Tue Apr 25 20:45:54 2017 UTC
# Line 1  Line 1 
1  /*                                                         -*- c++ -*-  /*                                                         -*- c++ -*-
2   * Copyright (C) 2007 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    #if !defined(WIN32)
28    # include <unistd.h>
29    # include <errno.h>
30    #endif
31    
32  #include <sstream>  #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  #if HAVE_GETTEXT  //FIXME: for some reason AC GETTEXT check fails on the Mac cross compiler?
42    #if (HAVE_GETTEXT || defined(__APPLE__))
43  # include <libintl.h>  # include <libintl.h>
44  # define _(String) gettext(String)  # define _(String) gettext(String)
45  #else  #else
# Line 39  Line 52 
52  # define VERSION VER_STRING // VER_STRING defined in libgig_private.h  # define VERSION VER_STRING // VER_STRING defined in libgig_private.h
53  #endif // WIN32  #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) {  template<class T> inline std::string ToString(T o) {
80      std::stringstream ss;      std::stringstream ss;
81      ss << o;      ss << o;
82      return ss.str();      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  #endif // GIGEDIT_GLOBAL_H

Legend:
Removed from v.2442  
changed lines
  Added in v.3123

  ViewVC Help
Powered by ViewVC