]> icculus.org git repositories - dana/openbox.git/blob - src/util.hh
rename, remove bullshit. ya
[dana/openbox.git] / src / util.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Util.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifndef _BLACKBOX_UTIL_HH
25 #define _BLACKBOX_UTIL_HH
26
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29
30 #include <string>
31 #include <vector>
32
33 class Rect {
34 public:
35   inline Rect(void) : _x1(0), _y1(0), _x2(0), _y2(0) { }
36   inline Rect(int __x, int __y, unsigned int __w, unsigned int __h)
37     : _x1(__x), _y1(__y), _x2(__w + __x - 1), _y2(__h + __y - 1) { }
38   inline explicit Rect(const XRectangle& xrect)
39     : _x1(xrect.x), _y1(xrect.y), _x2(xrect.width + xrect.x - 1),
40       _y2(xrect.height + xrect.y - 1) { }
41
42   inline int left(void) const { return _x1; }
43   inline int top(void) const { return _y1; }
44   inline int right(void) const { return _x2; }
45   inline int bottom(void) const { return _y2; }
46
47   inline int x(void) const { return _x1; }
48   inline int y(void) const { return _y1; }
49   void setX(int __x);
50   void setY(int __y);
51   void setPos(int __x, int __y);
52
53   inline unsigned int width(void) const { return _x2 - _x1 + 1; }
54   inline unsigned int height(void) const { return _y2 - _y1 + 1; }
55   void setWidth(unsigned int __w);
56   void setHeight(unsigned int __h);
57   void setSize(unsigned int __w, unsigned int __h);
58
59   void setRect(int __x, int __y, unsigned int __w, unsigned int __h);
60
61   void setCoords(int __l, int __t, int __r, int __b);
62
63   inline bool operator==(const Rect &a)
64   { return _x1 == a._x1 && _y1 == a._y1 && _x2 == a._x2 && _y2 == a._y2; }
65   inline bool operator!=(const Rect &a) { return ! operator==(a); }
66
67   Rect operator|(const Rect &a) const;
68   Rect operator&(const Rect &a) const;
69   inline Rect &operator|=(const Rect &a) { *this = *this | a; return *this; }
70   inline Rect &operator&=(const Rect &a) { *this = *this & a; return *this; }
71
72   inline bool valid(void) const { return _x2 > _x1 && _y2 > _y1; }
73
74   bool intersects(const Rect &a) const;
75   bool contains(int __x, int __y) const;
76   bool contains(const Rect &a) const;
77
78 private:
79   int _x1, _y1, _x2, _y2;
80 };
81
82 typedef std::vector<Rect> RectList;
83
84 struct Strut {
85   unsigned int top, bottom, left, right;
86
87   Strut(void): top(0), bottom(0), left(0), right(0) {}
88 };
89
90 /* XXX: this needs autoconf help */
91 const unsigned int BSENTINEL = 65535;
92
93 std::string expandTilde(const std::string& s);
94
95 void bexec(const std::string& command, const std::string& displaystring);
96
97 #ifndef   HAVE_BASENAME
98 std::string basename(const std::string& path);
99 #endif
100
101 std::string textPropertyToString(Display *display, XTextProperty& text_prop);
102
103 struct timeval; // forward declare to avoid the header
104 timeval normalizeTimeval(const timeval &tm);
105
106 struct PointerAssassin {
107   template<typename T>
108   inline void operator()(const T ptr) const {
109     delete ptr;
110   }
111 };
112
113 std::string itostring(unsigned long i);
114 std::string itostring(long i);
115 inline std::string itostring(unsigned int i)
116   { return itostring((unsigned long) i); }
117 inline std::string itostring(int i)
118   { return itostring((long) i); }
119   
120 #endif