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