
class SourceSelectPage : public WizardPage
{
public:
  enum SourceType
  {
    ModelSource= 0,
    ServerSource= 1,
    FileSource= 2
  };
  
private:
  struct SourcePart
  {
    mforms::Panel panel;
    mforms::Box box;
    mforms::RadioButton model_radio;
    mforms::RadioButton server_radio;
    mforms::RadioButton file_radio;
    mforms::Box browse_box;
    mforms::TextEntry entry;
    mforms::Button browse_button;
    
    SourcePart()
      : panel(::mforms::TitledBoxPanel), box(false), browse_box(true)
    {
      box.set_spacing(4);
      box.set_padding(12);
      box.set_homogeneous(true);
      panel.add(&box);
      box.add(&model_radio, false, false);
      model_radio.set_text(_("Model Schemata"));
      box.add(&server_radio, false, false);
      server_radio.set_text(_("Live Database Server"));
      file_radio.set_text(_("Script File:"));
      box.add(&browse_box, false, false);
      browse_box.set_spacing(8);
      browse_box.add(&file_radio, false);
      browse_box.add(&entry, true, true);
      browse_box.add(&browse_button, false);
      browse_button.set_text(_("Browse..."));
      
      file_radio.signal_toggled().connect(sigc::mem_fun(this, &SourcePart::file_source_selected));
    }
    
    void file_source_selected()
    {
      bool flag= file_radio.get_active();
      //g_message("%s", flag?"SEL":"UNSEL");
      entry.set_enabled(flag);
      browse_button.set_enabled(flag);
    }
    
    
    void set_change_slot(const sigc::slot<void> &change_slot)
    {
      model_radio.signal_toggled().connect(change_slot);
      server_radio.signal_toggled().connect(change_slot);
      file_radio.signal_toggled().connect(change_slot);
    }

    SourceType get_source()
    {
      if (model_radio.get_active())
        return ModelSource;
      else if (server_radio.get_active())
        return ServerSource;
      else
        return FileSource;
    }
  };

  SourcePart _left;
  SourcePart _right;

  void left_changed()
  {
    if (_left.model_radio.get_active())
      _right.model_radio.set_enabled(false);
    else
      _right.model_radio.set_enabled(true);

    if (_left.file_radio.get_active())
    {
      _left.entry.set_enabled(true);
      _left.browse_button.set_enabled(true);
    }
    else
    {
      _left.entry.set_enabled(true);
      _left.browse_button.set_enabled(true);
    }
  }
  
  void right_changed()
  {
    if (_right.model_radio.get_active())
      _left.model_radio.set_enabled(false);
    else
      _left.model_radio.set_enabled(true);


    if (_right.file_radio.get_active())
    {
      _right.entry.set_enabled(true);
      _right.browse_button.set_enabled(true);
    }
    else
    {
      _right.entry.set_enabled(true);
      _right.browse_button.set_enabled(true);
    }
  }
  
  virtual bool advance()
  {
    const char *sources[]= {
      "model", "server", "file"
    };
    
    values().gset("left_source", sources[get_left_source()]);
    values().gset("right_source", sources[get_right_source()]);
    values().gset("left_source_file", _left.entry.get_string_value());
    values().gset("right_source_file", _right.entry.get_string_value());

    return true;
  }


public:
  SourceSelectPage(WizardForm *form)
    : WizardPage(form, "source")
  {
    set_title(_("Select Sources to Compare"));
    set_short_title(_("Select Sources"));

    add(&_left.panel, false, false);
    _left.panel.set_title(_("Source for Left Catalog"));
    enable_file_browsing(&_left.entry, &_left.browse_button, mforms::OpenFile, "SQL Files (*.sql)|*.sql");
    _left.model_radio.set_enabled(false);
    _left.server_radio.set_active(true);
    
    _left.set_change_slot(sigc::mem_fun(this,&SourceSelectPage::left_changed));
    _right.set_change_slot(sigc::mem_fun(this,&SourceSelectPage::right_changed));
    
    _right.model_radio.set_active(true);
    
    _left.file_source_selected();
    _right.file_source_selected();
    
    add(&_right.panel, false, false);
    _right.panel.set_title(_("Source for Right Catalog"));
    enable_file_browsing(&_right.entry, &_right.browse_button, mforms::OpenFile, "SQL Files (*.sql)|*.sql");
  }

  SourceType get_left_source() { return _left.get_source(); }
  SourceType get_right_source() { return _right.get_source(); }
};
