//------------------------------------------------------------------------------
void DbMySQLTableEditor::create_table_page()
{
  // Connect Table tab widgets
  bind_entry_and_be_setter("table_name", this, &DbMySQLTableEditor::set_table_name);

  Gtk::ComboBox* combo = 0;
  xml()->get_widget("engine_combo", combo);
  setup_combo_for_string_list(combo);
  fill_combo_from_string_list(combo, _be->get_engines_list());
  add_option_combo_change_handler(combo, "ENGINE", sigc::mem_fun(this, &DbMySQLTableEditor::set_table_option_by_name));

  combo = 0;
  xml()->get_widget("collation_combo", combo);
  setup_combo_for_string_list(combo);

  std::vector<std::string> collations(_be->get_charset_collation_list());
  collations.insert(collations.begin(), "*Default*");
  fill_combo_from_string_list(combo, collations);
  add_option_combo_change_handler(combo, "CHARACTER SET - COLLATE", sigc::mem_fun(this, &DbMySQLTableEditor::set_table_option_by_name));

  Gtk::TextView *tview = 0;
  xml()->get_widget("table_comments", tview);

  add_text_change_timer(tview, sigc::mem_fun(this, &DbMySQLTableEditor::set_comment));

}
//------------------------------------------------------------------------------
DbMySQLTableEditorFKPage::DbMySQLTableEditorFKPage(DbMySQLTableEditor *owner
                                                          ,MySQLTableEditorBE *be
                                                          ,Glib::RefPtr<Gtk::Builder>         xml)
                             : _owner(owner)
                             , _be(be)
                             , _xml(xml)
                             , _edit_conn(0)
                             , _ce(0)
                             , _fk_page_content(0)
                             , _fk_page_not_supported_label(0)
{
  _xml->get_widget("fks", _fk_tv);
  _xml->get_widget("fk_columns", _fk_columns_tv);
  _fk_tv->set_enable_tree_lines(true);
  _fk_tv->set_headers_visible(true);
  _fk_tv->get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);

  switch_be(_be);

  _fk_tv->signal_cursor_changed().connect(sigc::mem_fun(*this, &DbMySQLTableEditorFKPage::fk_cursor_changed));

  _xml->get_widget("fk_update", _fk_update_combo);
  setup_combo_for_string_list(_fk_update_combo);
  fill_combo_from_string_list(_fk_update_combo, _be->get_fk_action_options());
  _fk_update_combo->signal_changed().connect(sigc::bind(sigc::mem_fun(this, &DbMySQLTableEditorFKPage::combo_box_changed)
                                                       ,::bec::FKConstraintListBE::OnUpdate
                                                       )
                                            );

  _xml->get_widget("fk_delete", _fk_delete_combo);
  setup_combo_for_string_list(_fk_delete_combo);
  fill_combo_from_string_list(_fk_delete_combo, _be->get_fk_action_options());
  _fk_delete_combo->signal_changed().connect(sigc::bind(sigc::mem_fun(this, &DbMySQLTableEditorFKPage::combo_box_changed)
                                                       ,::bec::FKConstraintListBE::OnDelete
                                                       )
                                            );

  Gtk::TextView *fk_comment(0);
  _xml->get_widget("fk_comment", fk_comment);
  _owner->add_text_change_timer(fk_comment, sigc::mem_fun(this, &DbMySQLTableEditorFKPage::set_comment));

  _xml->get_widget("fk_model_only", _fk_model_only);
  _fk_model_only->signal_toggled().connect(sigc::mem_fun(this, &DbMySQLTableEditorFKPage::model_only_toggled));

  _xml->get_widget("fk_page_content_box", _fk_page_content);
  _xml->get_widget("fk_page_not_supported_label", _fk_page_not_supported_label);

  _fk_page_not_supported_label->set_text("Note: foreign keys can only be defined for certain storage engines (like InnoDB)."
                                         " The server accepts foreign key definitions for other storage engines but silently ignores"
                                         " them. Switch your table engine (on the Table tab) to one that supports foreign"
                                         " keys to allow adjustments here."
                                         );

  _fk_page_not_supported_label->set_line_wrap(true);
  _fk_page_not_supported_label->hide();

  check_fk_support();
}
//------------------------------------------------------------------------------
DbMySQLTableEditorIndexPage::DbMySQLTableEditorIndexPage(DbMySQLTableEditor *owner, MySQLTableEditorBE *be,
                                                         Glib::RefPtr<Gtk::Builder> xml)
  : _owner(owner), _be(be), _xml(xml), _editing_done_id(0), _editable_cell(0) {
  _xml->get_widget("indexes", _indexes_tv);
  _indexes_tv->set_enable_tree_lines(true);
  _indexes_tv->set_headers_visible(true);

  switch_be(be);

  _indexes_tv->signal_cursor_changed().connect(
    sigc::mem_fun(*this, &DbMySQLTableEditorIndexPage::index_cursor_changed));

  std::vector<std::string> asc_desc_list;
  asc_desc_list.push_back("ASC");
  asc_desc_list.push_back("DESC");
  _sort_order_model = model_from_string_list(asc_desc_list);

  _xml->get_widget("index_storage_combo", _index_storage_combo);
  setup_combo_for_string_list(_index_storage_combo);
  fill_combo_from_string_list(_index_storage_combo, _be->get_index_storage_types());
  _index_storage_combo_conn = _index_storage_combo->signal_changed().connect(
    sigc::mem_fun(this, &DbMySQLTableEditorIndexPage::update_index_storage_type_in_be));

  _owner->bind_entry_and_be_setter("index_key_block_size", this,
                                   &DbMySQLTableEditorIndexPage::set_index_key_block_size);
  _owner->bind_entry_and_be_setter("index_parser", this, &DbMySQLTableEditorIndexPage::set_index_parser);

  Gtk::TextView *text(0);
  _xml->get_widget("index_comment", text);
  _owner->add_text_change_timer(text, sigc::mem_fun(this, &DbMySQLTableEditorIndexPage::set_index_comment));
  this->update_gui_for_server();

  _order_model = model_from_string_list(std::vector<std::string>());
}
    SchemaEditor(grt::Module *m, bec::GRTManager *grtm, const grt::BaseListRef &args)
        : PluginEditorBase(m, grtm, args, "modules/data/editor_schema.glade")
        , _be(new MySQLSchemaEditorBE(grtm, db_mysql_SchemaRef::cast_from(args[0])))
    {
        xml()->get_widget("mysql_schema_editor_notebook", _editor_notebook);

        Gtk::Widget *widget;
        xml()->get_widget("base_table", widget);

        Gtk::Image *image;
        xml()->get_widget("image", image);
        image->set(ImageCache::get_instance()->image_from_filename("db.Schema.editor.48x48.png", false));

        bind_entry_and_be_setter("name_entry", this, &SchemaEditor::set_name);
        if (_be->is_editing_live_object() && _be->get_schema()->oldName() != "")
        {
            Gtk::Entry *entry;
            xml()->get_widget("name_entry", entry);
            entry->set_sensitive(false);
        }

        Gtk::Button *btn;
        xml()->get_widget("refactor_btn", btn);
        btn->set_sensitive(_be->refactor_possible());
        btn->signal_clicked().connect(sigc::mem_fun(this, &SchemaEditor::refactor_schema));

        Gtk::ComboBox *combo;
        xml()->get_widget("collation_combo", combo);
        Glib::RefPtr<Gtk::ListStore> store(Glib::RefPtr<Gtk::ListStore>::cast_dynamic(xml()->get_object("collation_store")));
        setup_combo_for_string_list(combo);
        fill_combo_from_string_list(combo, _be->get_charset_collation_list());
        add_option_combo_change_handler(combo, "CHARACTER SET - COLLATE", sigc::mem_fun(this, &SchemaEditor::set_schema_option_by_name));

        Gtk::TextView *tview;
        xml()->get_widget("text_view", tview);
        add_text_change_timer(tview, sigc::mem_fun(this, &SchemaEditor::set_comment));

        //!widget->reparent(*this);
        add(*_editor_notebook);
        _editor_notebook->show();

        show_all();

        refresh_form_data();
    }
//------------------------------------------------------------------------------
DbMySQLTableEditor::DbMySQLTableEditor(grt::Module *m, bec::GRTManager *grtm, const grt::BaseListRef &args)
//    : PluginEditorBase(m, grtm, args, "modules/data/editor_mysql_table.glade")
    : PluginEditorBase(m, grtm, args, 0)
    , _be(new MySQLTableEditorBE(grtm, db_mysql_TableRef::cast_from(args[0])))
    , _part_page(0)
    , _inserts_panel(0)
    , _main_page_widget(0)
{
  load_glade((_be->is_editing_live_object()) ? "modules/data/editor_mysql_table_live.glade" : "modules/data/editor_mysql_table.glade");

  xml()->get_widget("mysql_editor_notebook", _editor_notebook);
  _editor_notebook->signal_switch_page().connect(sigc::mem_fun(this, &DbMySQLTableEditor::page_changed));

  Gtk::Image *image(0);
  xml()->get_widget("table_editor_image", image);
  image->set(ImageCache::get_instance()->image_from_filename("db.Table.editor.48x48.png", false));
  image->set_data("is_large", (void*)1);

  if (!_be->is_editing_live_object())
    xml()->get_widget("table_page_box", _main_page_widget);

  set_border_width(0);

  _columns_page   = new DbMySQLTableEditorColumnPage(this, _be, xml());
  _indexes_page   = new DbMySQLTableEditorIndexPage(this, _be, xml());
  _fks_page       = new DbMySQLTableEditorFKPage(this, _be, xml());
  _triggers_page  = new DbMySQLTableEditorTriggerPage(this, _be, xml());
  _part_page      = new DbMySQLTableEditorPartPage(this, _be, xml());
  _opts_page      = new DbMySQLTableEditorOptPage(this, _be, xml());

  if (!is_editing_live_object())
  {
    _inserts_panel= _be->get_inserts_panel();
    _editor_notebook->append_page(*mforms::widget_for_view(_inserts_panel), "Inserts");

    _privs_page     = new DbMySQLEditorPrivPage(_be);
    _editor_notebook->append_page(_privs_page->page(), "Privileges");
  }
  else
  {
    _inserts_panel= NULL;
    _privs_page= NULL;

    Gtk::ComboBox *cbox = 0;
    xml()->get_widget("schema_combo", cbox);
    if (cbox)
    {
      setup_combo_for_string_list(cbox);
    }
  }
  
  create_table_page();

  add(*_editor_notebook);
  _editor_notebook->show();

  show_all();

  Gtk::Entry *entry(0);
  xml()->get_widget("table_name", entry);
  entry->signal_event().connect(sigc::mem_fun(this, &DbMySQLTableEditor::event_from_table_name_entry));

  refresh_form_data();

  focus_widget_when_idle(entry);

  _be->set_refresh_ui_slot(sigc::mem_fun(this, &DbMySQLTableEditor::refresh_form_data));
  _be->set_partial_refresh_ui_slot(sigc::mem_fun(this, &DbMySQLTableEditor::partial_refresh));

  _be->reset_editor_undo_stack();
  //Gtk::Paned* table_page_paned = 0;
  //xml()->get_widget("table_page_paned", table_page_paned);
  //gtk_paned_set_pos_ratio(table_page_paned, 0.2);
}