//
// Copyright (C) 2008 Jonathan Moore Liles
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//

// generated by Fast Light User Interface Designer (fluid) version 1.0300

#include "New_Project_Dialog.H"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <FL/Fl_File_Chooser.H>

void New_Project_Dialog::cb_Browse_i(Fl_Button*, void*) {
  _directory->value( fl_dir_chooser( "Directory for new project", NULL, 0 ) );
}
void New_Project_Dialog::cb_Browse(Fl_Button* o, void* v) {
  ((New_Project_Dialog*)(o->parent()->user_data()))->cb_Browse_i(o,v);
}

void New_Project_Dialog::cb_Create_i(Fl_Return_Button*, void*) {
  if ( strlen( _directory->value() ) && strlen( _name->value() ) )
{
	char pat[1024];
	
	snprintf( pat, sizeof( pat ), "%s/%s", _directory->value(), _name->value() );
	
	path = strdup( pat );
	
        _default_path = _directory->value() ? strdup( _directory->value() ) : 0;

	//if ( ! Project::create( pat, _template->text( _template->value() ) ) )
	//	fl_alert( "Error creating project!" );

	_window->hide();
};
}
void New_Project_Dialog::cb_Create(Fl_Return_Button* o, void* v) {
  ((New_Project_Dialog*)(o->parent()->user_data()))->cb_Create_i(o,v);
}

void New_Project_Dialog::cb__directory_i(Fl_File_Input* o, void*) {
  if ( ! fl_filename_isdir( o->value() ) )
{
	fl_alert( "Must be a directory" );
	o->value( "" );
	return;
}

// write_line( user_config_dir, "default_path", o->value() );
}
void New_Project_Dialog::cb__directory(Fl_File_Input* o, void* v) {
  ((New_Project_Dialog*)(o->parent()->user_data()))->cb__directory_i(o,v);
}

New_Project_Dialog::New_Project_Dialog() {
  _default_path = 0;
  path = 0;
  make_window();
}

void New_Project_Dialog::run() {
  _directory->value( _default_path );
  
  _window->show();
  
  while ( _window->shown() )
  	Fl::wait();
}

Fl_Double_Window* New_Project_Dialog::make_window() {
  { _window = new Fl_Double_Window(550, 105, "New Project");
    _window->user_data((void*)(this));
    { _name = new Fl_File_Input(75, 55, 375, 35, "Named:");
    } // Fl_File_Input* _name
    { Fl_Button* o = new Fl_Button(455, 15, 80, 35, "Browse");
      o->callback((Fl_Callback*)cb_Browse);
    } // Fl_Button* o
    { Fl_Return_Button* o = new Fl_Return_Button(455, 55, 80, 35, "Create");
      o->callback((Fl_Callback*)cb_Create);
    } // Fl_Return_Button* o
    { Fl_File_Input* o = _directory = new Fl_File_Input(75, 15, 375, 35, "Where:");
      _directory->callback((Fl_Callback*)cb__directory);
      char *v = NULL;
      // read_line( user_config_dir, "default_path", &v );
      o->value( v );
    } // Fl_File_Input* _directory
    _window->set_modal();
    _window->end();
  } // Fl_Double_Window* _window
  return _window;
}

void New_Project_Dialog::default_path( char *s ) {
  _default_path = s;
}

char * New_Project_Dialog::default_path() {
  return _default_path;
}

char* new_project_chooser( char **default_path) {
  New_Project_Dialog nsd;
  
  nsd.default_path( *default_path );
  
  nsd.run();
  
  if ( nsd.default_path() )
  	*default_path = nsd.default_path();
  
  return nsd.path;
}
