/*
 *  wb_context_model.cpp
 *  MySQLWorkbench
 *
 *  Created by Alfredo Kojima on 29/Apr/09.
 *  Copyright 2009 Sun Microsystems Inc. All rights reserved.
 *
 */

#include "stdafx.h"

#include "wb_context_model.h"
#include "wb_context_ui.h"
#include "wb_component.h"
#include "grt/common.h"

using namespace bec;
using namespace wb;


WBContextModel::WBContextModel(WBContextUI *wbui)
: _wbui(wbui)
{
}


grt::ListRef<app_ToolbarItem> WBContextModel::get_toolbar_items(const std::string &toolbar)
{
  WBContext *wb= _wbui->get_wb();
  grt::ListRef<app_ToolbarItem> items;
  
  if (toolbar == WB_TOOLBAR_MAIN)
  {
    if (!_toolbar.is_valid())
    {
      _toolbar= app_ToolbarRef::cast_from(wb->get_grt()->unserialize(make_path(wb->get_datadir(), 
                                                                               "data/model_toolbar.xml")));
    }
    
    return _toolbar->items();
  }
  else if (toolbar.compare(0, sizeof(WB_TOOLBAR_TOOLS)-1, WB_TOOLBAR_TOOLS) == 0)
  {
    if (!_tools_toolbar.is_valid())
      _tools_toolbar= app_ToolbarRef::cast_from(wb->get_grt()->unserialize(make_path(wb->get_datadir(),
                                                                                     "data/tools_toolbar.xml")));
   
    items= grt::ListRef<app_ToolbarItem>(wb->get_grt());
    grt::append_contents(items, _tools_toolbar->items());
  }
  else if (toolbar == WB_TOOLBAR_OPTIONS)
  {
    items= grt::ListRef<app_ToolbarItem>(wb->get_grt());
  }
  
  // view specific contextual toolbars
  if (_wbui->get_active_context() == WB_CONTEXT_MODEL)
  {
    grt::ListRef<app_ToolbarItem> model_items;
      
    // append model specific items
    wb->foreach_component(sigc::compose(sigc::bind<0>(sigc::ptr_fun(grt::append_contents), items),
                                        sigc::bind(sigc::mem_fun(&WBComponent::get_toolbar_items), toolbar)));
  }
  
  return items;
}

