diff options
Diffstat (limited to 'src/FbTk/Layer.hh')
-rw-r--r-- | src/FbTk/Layer.hh | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/src/FbTk/Layer.hh b/src/FbTk/Layer.hh new file mode 100644 index 0000000..f974918 --- /dev/null +++ b/src/FbTk/Layer.hh | |||
@@ -0,0 +1,201 @@ | |||
1 | // Layer.hh for FbTk - fluxbox toolkit | ||
2 | // Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
3 | // and Simon Bowden (rathnor at users.sourceforge.net) | ||
4 | // | ||
5 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
6 | // copy of this software and associated documentation files (the "Software"), | ||
7 | // to deal in the Software without restriction, including without limitation | ||
8 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
9 | // and/or sell copies of the Software, and to permit persons to whom the | ||
10 | // Software is furnished to do so, subject to the following conditions: | ||
11 | // | ||
12 | // The above copyright notice and this permission notice shall be included in | ||
13 | // all copies or substantial portions of the Software. | ||
14 | // | ||
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
21 | // DEALINGS IN THE SOFTWARE. | ||
22 | |||
23 | // $Id: Layer.hh,v 1.1 2003/01/16 12:41:27 rathnor Exp $ | ||
24 | |||
25 | #ifndef FBTK_LAYERTEMPLATE_HH | ||
26 | #define FBTK_LAYERTEMPLATE_HH | ||
27 | |||
28 | /*#include "Layer.hh"*/ | ||
29 | #include <vector> | ||
30 | #include <algorithm> | ||
31 | #include <iostream> | ||
32 | |||
33 | namespace FbTk { | ||
34 | |||
35 | template <typename ItemType, typename Container = std::vector<ItemType *> > | ||
36 | class Layer { | ||
37 | public: | ||
38 | typedef Container ListType; | ||
39 | typedef typename Container::iterator iterator; | ||
40 | typedef typename Container::reverse_iterator reverse_iterator; | ||
41 | virtual ~Layer() { } | ||
42 | /// insert in top by default | ||
43 | virtual iterator insert(ItemType &item, unsigned int pos=0); | ||
44 | /// remove item from list | ||
45 | virtual void remove(ItemType &item); | ||
46 | /// cycle all item upwards | ||
47 | virtual void cycleUp(); | ||
48 | /// cycle all items downwards | ||
49 | virtual void cycleDown(); | ||
50 | /// move item to top | ||
51 | virtual void raise(ItemType &item); | ||
52 | /// move item to bottom | ||
53 | virtual void lower(ItemType &item); | ||
54 | /// raise a specific item one step | ||
55 | virtual void stepUp(ItemType &item); | ||
56 | /// lower a specific item one step | ||
57 | virtual void stepDown(ItemType &item); | ||
58 | virtual void restack(); | ||
59 | /// @return layer item on specific position, on failure 0 | ||
60 | ItemType *getItem(unsigned int position); | ||
61 | /// @return number of elements in layer | ||
62 | unsigned int size() const { return m_list.size(); } | ||
63 | /// @return layer list | ||
64 | const ListType &itemList() const { return m_list; } | ||
65 | /// @return layer list | ||
66 | ListType &itemList() { return m_list; } | ||
67 | private: | ||
68 | ListType m_list; | ||
69 | }; | ||
70 | |||
71 | template <typename ItemType, typename Container> | ||
72 | typename Container::iterator Layer<ItemType, Container>::insert(ItemType &item, unsigned int position) { | ||
73 | // make sure we don't alreay have it in the list | ||
74 | if (std::find(itemList().begin(), itemList().end(), &item) != itemList().end()) | ||
75 | return m_list.end(); | ||
76 | |||
77 | if (position > size()) | ||
78 | position = size(); | ||
79 | |||
80 | iterator it = m_list.begin(); | ||
81 | |||
82 | for (unsigned int i=0; i<position; ++it, ++i) | ||
83 | continue; | ||
84 | |||
85 | m_list.insert(it, &item); | ||
86 | restack(); | ||
87 | return it++; | ||
88 | } | ||
89 | |||
90 | |||
91 | template <typename ItemType, typename Container> | ||
92 | void Layer<ItemType, Container>::remove(ItemType &item) { | ||
93 | iterator it = std::find(itemList().begin(), itemList().end(), &item); | ||
94 | if (it != itemList().end()) | ||
95 | m_list.erase(it); | ||
96 | } | ||
97 | |||
98 | template <typename ItemType, typename Container> | ||
99 | void Layer<ItemType, Container>::cycleUp() { | ||
100 | if (size() == 0) | ||
101 | return; | ||
102 | iterator it = itemList().begin(); | ||
103 | it++; | ||
104 | rotate(itemList().begin(), it, itemList().end()); | ||
105 | restack(); | ||
106 | } | ||
107 | |||
108 | |||
109 | template <typename ItemType, typename Container> | ||
110 | void Layer<ItemType, Container>::cycleDown() { | ||
111 | if (size() == 0) | ||
112 | return; | ||
113 | // save last item and remove it from list | ||
114 | ItemType *last_item = itemList().back(); | ||
115 | itemList().pop_back(); | ||
116 | // add last item to front | ||
117 | itemList().insert(itemList().begin(), last_item); | ||
118 | restack(); | ||
119 | } | ||
120 | |||
121 | template <typename ItemType, typename Container> | ||
122 | void Layer<ItemType, Container>::stepUp(ItemType &item) { | ||
123 | iterator it = | ||
124 | find(itemList().begin(), itemList().end(), &item); | ||
125 | |||
126 | if (it == itemList().end()) | ||
127 | return; | ||
128 | |||
129 | if (it == itemList().begin()) // we can't raise it more | ||
130 | return; | ||
131 | |||
132 | iterator new_pos = it; | ||
133 | new_pos--; | ||
134 | ItemType *textitem = *it; | ||
135 | // remove item from list | ||
136 | itemList().erase(it); | ||
137 | // insert above last pos | ||
138 | itemList().insert(new_pos, textitem); | ||
139 | restack(); | ||
140 | } | ||
141 | |||
142 | template <typename ItemType, typename Container> | ||
143 | void Layer<ItemType, Container>::stepDown(ItemType &item) { | ||
144 | iterator it = | ||
145 | find(itemList().begin(), itemList().end(), &item); | ||
146 | |||
147 | if (it == itemList().end()) // we didn't find the item in our list | ||
148 | return; | ||
149 | |||
150 | if (*it == itemList().back()) // it's already at the bottom | ||
151 | return; | ||
152 | |||
153 | iterator new_pos = it; | ||
154 | new_pos++; | ||
155 | ItemType *textitem = *it; | ||
156 | // remove from list | ||
157 | itemList().erase(it); | ||
158 | // insert on the new place | ||
159 | itemList().insert(new_pos, textitem); | ||
160 | restack(); | ||
161 | } | ||
162 | template <typename ItemType, typename Container> | ||
163 | void Layer<ItemType, Container>::raise(ItemType &item) { | ||
164 | if (&item == itemList().front()) // already at the bottom | ||
165 | return; | ||
166 | remove(item); | ||
167 | insert(item, 0); | ||
168 | restack(); | ||
169 | } | ||
170 | |||
171 | template <typename ItemType, typename Container> | ||
172 | void Layer<ItemType, Container>::lower(ItemType &item) { | ||
173 | if (&item == itemList().back()) // already at the bottom | ||
174 | return; | ||
175 | remove(item); | ||
176 | insert(item, size()); | ||
177 | restack(); | ||
178 | } | ||
179 | |||
180 | template <typename ItemType, typename Container> | ||
181 | ItemType *Layer<ItemType, Container>::getItem(unsigned int position) { | ||
182 | if (position >= m_list.size()) | ||
183 | return 0; | ||
184 | iterator it = m_list.begin(); | ||
185 | iterator it_end = m_list.end(); | ||
186 | for (unsigned int i=0; i < position && it != it_end; i++); | ||
187 | |||
188 | if (it == it_end) return 0; | ||
189 | else | ||
190 | return *it; | ||
191 | } | ||
192 | |||
193 | template <typename ItemType, typename Container> | ||
194 | void Layer<ItemType, Container>::restack() { | ||
195 | } | ||
196 | |||
197 | |||
198 | }; // end namespace FbTk | ||
199 | |||
200 | |||
201 | #endif // FBTK_LAYERTEMPLATE_HH | ||