• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

/Users/jmonk/Physics/ForIA/src/MessageBox.cxx

Go to the documentation of this file.
00001 #include "ForIA/MessageBox.hh"
00002 
00003 #include <iostream>
00004 
00005 namespace ForIA {
00006   
00007   MessageBox::MessageBox(): m_title(""), m_width(0), m_leftPad(0), m_rightPad(0), m_titleLeftPad(0), m_titleRightPad(0){
00008     increaseWidth(0);
00009   }
00010   
00011   MessageBox::MessageBox(const string &title): m_title(title), m_width(0), m_leftPad(0), m_rightPad(0), m_titleLeftPad(0), m_titleRightPad(0){
00012     increaseWidth(m_title.length());
00013   }
00014   
00015   MessageBox &MessageBox::setTitle(const string &title){
00016     m_title = title;
00017     if(m_title.length() > m_width){
00018       increaseWidth(m_title.length());
00019     }
00020     return *this;
00021   }
00022   
00023   MessageBox &MessageBox::setWidth(unsigned int newWidth){
00024     if(newWidth <= m_width) return *this;
00025     
00026     increaseWidth(newWidth);
00027     
00028     return *this;
00029   }
00030   
00031   MessageBox &MessageBox::addEntry(const string &leftCol, const string &rightCol){
00032     
00033     addEntry(leftCol, vector<string>(1, rightCol));
00034     
00035     return *this;
00036   }
00037   
00038   MessageBox &MessageBox::addEntry(const string &leftCol, const vector<string> &rightCol){
00039     
00040     m_entries.push_back(pair<string, vector<string> >(leftCol, rightCol));
00041     
00042     unsigned int pad = leftCol.length();
00043     if (pad > m_leftPad){
00044       m_width += pad - m_leftPad;
00045       m_leftPad = pad;
00046     }
00047     
00048     for(vector<string>::const_iterator row = rightCol.begin();
00049         row != rightCol.end(); ++row){
00050       unsigned int pad = row->length();
00051       if(pad > m_rightPad){
00052         m_width += pad - m_rightPad;
00053         m_rightPad = pad;
00054       }
00055     }
00056     
00057     titleWidth();
00058     
00059     return *this;
00060   }
00061   
00062   void MessageBox::titleWidth(){
00063     
00064     if(m_width <= m_title.length()) return;
00065     
00066     m_titleLeftPad  = (m_width - m_title.length()) / 2;
00067     m_titleRightPad = m_titleLeftPad;
00068     
00069     if( m_title.length() + m_titleLeftPad + m_titleRightPad != m_width) ++m_titleRightPad;
00070     return;
00071   }
00072   
00073   void MessageBox::increaseWidth(unsigned int newWidth){
00074    
00075     unsigned int incr = (newWidth - m_width) / 2;
00076     m_leftPad += incr;
00077     m_rightPad += incr;
00078     m_width = newWidth;
00079     if(m_leftPad + m_rightPad != m_width) ++m_rightPad;
00080     
00081     titleWidth();
00082     
00083     return;
00084   }
00085   
00086   int MessageBox::width() const{
00087     return m_width;
00088   }
00089   
00090   const string &MessageBox::title()const{
00091     return m_title;
00092   }
00093   
00094   std::ostream &operator << (std::ostream &out, const MessageBox &box){
00095     out.width(box.m_width + 7);
00096     out.fill('_');
00097     out<<std::left<<" "<<" "<<std::endl;
00098     out.fill(' ');
00099     out.width(box.m_titleLeftPad+4);
00100     out<<std::left<<"|"<<box.m_title;
00101     out.width(box.m_titleRightPad+4);
00102     out<<std::right<<"|"<<std::endl;
00103     out.width(box.m_width+7);
00104     out<<std::left<<"|"<<"|"<<std::endl;
00105     
00106     for(vector<pair<string, vector<string> > >::const_iterator entry = box.m_entries.begin();
00107         entry != box.m_entries.end(); ++entry){
00108       out.width(box.m_leftPad+3);
00109       string left = "|  " + entry->first;
00110       out<<left<<":";
00111       string pad=" ";
00112       
00113       
00114       bool doleft=false;
00115       for(vector<string>::const_iterator right = entry->second.begin();
00116           right != entry->second.end(); ++right){
00117         if(doleft){
00118           out.width(box.m_leftPad+3);
00119           out<<std::left<<"|";
00120         }
00121         doleft=true;
00122         out.width(box.m_rightPad+1);
00123         string tmp = pad + *right;
00124         out<<tmp<<"  |"<<std::endl;
00125         pad="  ";
00126       }
00127       
00128     }
00129     
00130     out.width(box.m_width+7);
00131     out<<std::left<<"|"<<"|"<<std::endl;
00132     out.width(box.m_width + 7);
00133     out.fill('_');
00134     out<<std::left<<"|"<<"|"<<std::endl;
00135     
00136     return out;
00137   }
00138   
00139   
00140   
00141 }

Generated on Mon Jul 30 2012 16:56:35 for ForIA by  doxygen 1.7.2