summaryrefslogtreecommitdiff
path: root/src/FbTk/Layer.hh
blob: b9257c32802802f2b7dc8cca9bd0e292537ac30b (plain)
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
// 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.

#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);
    /// move item to top
    virtual void raise(ItemType &item);
    /// move item to bottom
    virtual void lower(ItemType &item);
    /// @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; }
protected:
    virtual void restack();
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>::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