src/container.h

Go to the documentation of this file.
00001 // Copyright (c) 2003 - 2004 Anselm R. Garbe <anselmg at t-online.de>
00002 // See ../LICENSE.txt for license details.
00003 //
00004 // $Id: container.h 7 2007-05-24 11:03:53Z eg1981 $
00005 
00006 #ifndef __CONTAINER_H
00007 #define __CONTAINER_H
00008 
00009 #include "kernel.h"
00010 #include "util.h" // also includes ncwm.h
00011 
00025 template <class P, class LT, class LTI, class T>
00026     class Container
00027 {
00028 public:
00029 
00036     Container(P *parent, T *child = 0) {
00037         parent_ = parent;
00038         child_ = child;
00039     };
00040 
00047     ~Container() {
00048         Util::destroy<LT, LTI, T *>
00049                  (&childs_, childs_.begin(), childs_.end());
00050     };
00051 
00057     bool contains(T *child) {
00058         return Util::contains<LT, LTI, T>(&childs_, child);
00059     };
00060 
00066     unsigned int size() {
00067         return childs_.size();
00068     };
00069 
00075     LTI begin() {
00076         return childs_.begin();
00077     };
00078 
00084     LTI end() {
00085         return childs_.end();
00086     };
00087 
00093     P *attached() const {
00094         return parent_;
00095     };
00096 
00102     void setAttached(P *parent) {
00103         parent_ = parent;
00104     };
00105 
00111     T *focused() const {
00112         return child_;
00113     };
00114 
00125     void focus(T *child) {
00126         if (child && KERNEL->isStackedTabbing()) {
00127             if (childs_.size()) {
00128                 T *foc = childs_.front();
00129                 if (foc != child) {
00130                     childs_.remove(foc);
00131                     childs_.push_back(foc);
00132                 }
00133             }
00134             childs_.remove(child);
00135             childs_.push_front(child);
00136         }
00137         child_ = child;
00138     };
00139 
00149     void attach(T *child) {
00150         if (child && !contains(child)) {
00151             childs_.push_back(child);
00152             focus(child);
00153         }
00154     };
00155 
00167     T *detach(T *child) {
00168 
00169         if (child_ == child) {
00170             // cycle to prev child
00171             focus(prev());
00172         }
00173         childs_.remove(child);
00174 
00175         if (childs_.size() < 1) {
00176             child_ = 0;
00177         }
00178         return child_;
00179     };
00180 
00189     T *prev() {
00190 
00191         if (childs_.size() < 1) {
00192             return 0;
00193         }
00194         else if (childs_.size() < 2) {
00195             return child_;
00196         }
00197 
00198         if (KERNEL->isStackedTabbing()) {
00199             return childs_.back();
00200         }
00201         else {
00202             for (LTI it = childs_.begin();
00203                     it != childs_.end(); it++)
00204             {
00205                 if (child_ == *it) {
00206                     if (it != childs_.begin()) {
00207                         it--;
00208                         return *it;
00209                     }
00210                     else {
00211                         break;
00212                     }
00213                 }
00214             }
00215             return childs_.back();
00216         }
00217     }
00218 
00219 
00228     T *next() {
00229         if (childs_.size() < 1) {
00230             return 0;
00231         }
00232         else if (childs_.size() < 2) {
00233             return child_;
00234         }
00235 
00236         if (KERNEL->isStackedTabbing()) {
00237             LTI it = childs_.begin();
00238             it++;
00239             return *it;
00240         }
00241         else {
00242             for (LTI it = childs_.begin();
00243                     it != childs_.end(); it++)
00244             {
00245                 if (child_ == *it) {
00246                     it++;
00247                     if (it != childs_.end()) {
00248                         return *it;
00249                     }
00250                     else {
00251                         break;
00252                     }
00253                 }
00254             }
00255             return childs_.front();
00256         }
00257     };
00258 
00260     LT *childs() {
00261         return &childs_;
00262     }
00263 
00264 
00265 private:
00266 
00267     P *parent_;
00268     T *child_;
00269     LT childs_;
00270 };
00271 
00272 #endif // __CONTAINER_H

Generated on Thu May 24 15:19:31 2007 for ncwm by  doxygen 1.5.1