
#include "grt_sidebar.h"
#include "MGGladeXML.h"
#include "grt/grt_manager.h"
#include "shell/shell_view.h"


GRTSidebar::GRTSidebar(bec::GRTManager *grtm)
  : _grtm(grtm)
{
  _xml= new MGGladeXML(grtm->get_data_file_path("grt_sidebar.glade").c_str(), "", "");

  _shell_trees= new ShellTreeViews(grtm);

  
  Gtk::Notebook *note;
  // setup sidebar stuff for grt shell
  _xml->get("grt_trees_note", &note);

  note->reference();
  note->unparent();

  Gtk::Paned *paned;
  Gtk::Box *vbox;

  _xml->get("globals_paned", &paned);
  _xml->get("globals_vbox", &vbox);

  vbox->pack_start(_combo, false, false);
  vbox->pack_start(*_shell_trees->get_value_tree_base(), true, true);
  _combo.show();

  paned->pack2(*_shell_trees->get_inspector_tree_base(), false, true);
  paned->set_position(400);
  paned->show_all();
  
  _combo.get_entry()->signal_activate().connect(sigc::mem_fun(this, &GRTSidebar::change_grt_path));
  
  Gtk::Box *box;
  _xml->get("classes_vbox", &box);

  Gtk::ComboBoxText *combo= Gtk::manage(new Gtk::ComboBoxText());
  box->pack_start(*combo, false, false);
  combo->append_text(_("Order by Name"));
  combo->append_text(_("Order by Hierarchy"));
  combo->append_text(_("Order by Package"));

  combo->set_active(0);
  combo->signal_changed().connect(sigc::bind(sigc::mem_fun(this, &GRTSidebar::change_class_display), combo));
  

  box->pack_start(*_shell_trees->get_struct_tree_base());

  _xml->get("modules_vbox", &box);
  box->pack_start(*_shell_trees->get_module_tree_base(), true, true);
  box->pack_start(*_shell_trees->get_module_text_base(), false, true);
  
  note->show_all();

  _shell_trees->refresh_all();
}


GRTSidebar::~GRTSidebar()
{
  get_base()->unreference();
}


void GRTSidebar::change_class_display(Gtk::ComboBox *combo)
{
  int index= combo->get_active_row_number();

  _shell_trees->set_struct_sorting((bec::StructsTreeBE::DisplayMode)index);
}


void GRTSidebar::change_grt_path()
{  
  std::string path= _combo.get_entry()->get_text();
  g_message("path %s", path.c_str());
  
  _shell_trees->set_globals_tree_path(path);
}


void GRTSidebar::refresh()
{
  if (_grtm->get_shell())
  {
    _combo.clear_items();
    std::vector<std::string> items= _grtm->get_shell()->get_grt_tree_bookmarks();
    for (std::vector<std::string>::const_reverse_iterator i= items.rbegin(); i != items.rend(); ++i)
      _combo.prepend_text(*i);
  }
  _shell_trees->refresh_all();
}


void GRTSidebar::reset_layout()
{
  Gtk::Paned *paned;

  _xml->get("globals_paned", &paned);
  
  paned->set_position(paned->get_height()-200);
}


Gtk::Widget *GRTSidebar::get_base()
{
  Gtk::Notebook *note;
  
  _xml->get("grt_trees_note", &note);
  
  return note;
}


std::string GRTSidebar::get_title()
{
  return "GRT";
}
