/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CGroupXml.cpp  -  description
                             -------------------
    begin                : Wed June 28 2006
    copyright            : (C) 2006 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at 
   your option) any later version.

   This library is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#include <sptk3/cxml>
#include <sptk3/CGroupXml.h>
#include <sptk3/CControl.h>
#include <sptk3/CWindow.h>

using namespace std;
using namespace sptk;

void CGroupXml::load(const CXmlNode& groupNode) {
   if (m_noXml) return;
   if ( ! (bool) groupNode.getAttribute("visible","1") ) 
      m_widget->hide();
   else m_widget->show();
   if ( ! (bool) groupNode.getAttribute("enable","1") ) 
      m_widget->deactivate();
   else m_widget->activate();

   map<string,CXmlNode *> xmlControls;
   map<string,CXmlNode *> xmlGroups;
   CXmlNode::const_iterator itor = groupNode.begin();
   for (; itor != groupNode.end(); itor++) {
      CXmlNode* node = *itor;
      string label = node->getAttribute("label");
      if (label.empty()) 
         label = "noName:" + node->getAttribute("nn_index");
      if (node->name() == "group") 
         xmlGroups[label] = node;
      else if (node->name() == "control") 
         xmlControls[label] = node;
   }

   for (int i = 0; i < m_widget->children(); i++) {
      Fl_Widget *widget = m_widget->child(i);
      try {
         CGroupXml *group = dynamic_cast<CGroupXml *>(widget);
         if (group) {
            string glabel = group->label();
            if (glabel.empty()) 
               glabel = "noName:" + int2string(i);
            map<string,CXmlNode *>::iterator itor = xmlGroups.find(glabel);
            if (itor != xmlGroups.end()) {
               CXmlNode* node = itor->second;
               group->load(*node);
            }
            continue;
         }
      }
      catch (...) {}
      try {
         CControl *control = dynamic_cast<CControl *>(widget);
         if (control) {
            string clabel = control->label();
            if (clabel.empty()) 
               clabel = "noName:" + int2string(i);
            map<string,CXmlNode *>::iterator itor = xmlControls.find(clabel);
            if (itor != xmlControls.end()) {
               CXmlNode* node = itor->second;
               control->load(*node);
            }
            continue;
         }
      }
      catch (...) {}
   }
}

void CGroupXml::save(CXmlNode& groupNode) const {
   groupNode.clear();
   groupNode.name("group");
   if (m_noXml) return;
   if (!label().empty())
      groupNode.attributes["label"] = label();
   for (unsigned i = 0; i < (unsigned) m_widget->children(); i++) {
      Fl_Widget *widget = m_widget->child(i);
      try {
         CGroupXml *group = dynamic_cast<CGroupXml *>(widget);
         if (group) {
            CXmlNode* node = new CXmlElement(groupNode,"group");
            group->save(*node);
            if (node->empty())
               groupNode.remove(node);
            else if (group->label().empty()) {
               node->attributes["nn_index"] = i;
            }
            continue;
         }
      }
      catch (...) {}
      try {
         CControl *control = dynamic_cast<CControl *>(widget);
         if (control) {
            CXmlNode* node = new CXmlElement(groupNode,"control");
            control->save(*node);
            if (node->empty())
               groupNode.remove(node);
            else if (control->label().empty()) {
               node->attributes["nn_index"] = i;
            }
            continue;
         }
      }
      catch (...) {}
   }
}
