function lib = new_library_dialog(user)

if nargin < 1 || isempty(user)
	user = get_active_user;
end

%--
% dialog header
%--

control(1) = control_create( ...
	'style', 'separator', ...
	'type', 'header', ...
	'min', 1, ...
	'string', ['Library  (', user.name ')'] ...
);

%--
% user name
%--

% NOTE: the number of lines along with the dialog group options produce a square

control(end + 1) = control_create( ...
	'name', 'name', ...
	'space', 0.75, ...
	'style', 'edit' ...
);

control(end + 1) = control_create( ...
	'name', 'location', ...
	'style', 'edit', ...
	'lines', 3, ...
	'string', user_root(user, 'lib'), ...
	'space', 1 ...
);
	
%----------------------------------
% CREATE DIALOG
%----------------------------------

%--
% configure dialog options
%--

opt = dialog_group;

opt.width = 14;

opt.header_color = get_extension_color('root');

%--
% create dialog
%--

out = dialog_group('New ...', control, opt, @dialog_callback);

% NOTE: return empty on cancel

if isempty(out.values)
	lib = []; return;
end

path = [out.values.location, filesep, out.values.name];

if ~create_dir(path);
	warn_dialog('Specified Library already exists or is not a valid directory.'); lib = []; return;
end

lib = library_create(out.values.name, user, out.values.location);


%--------------------------------
% DIALOG CALLBACK
%--------------------------------

function result = dialog_callback(obj, eventdata)

%--------------------------------
% Author: Matt Robbins
%--------------------------------
% $Revision: 1938 $
% $Date: 2005-10-17 08:49:40 -0400 (Mon, 17 Oct 2005) $
%--------------------------------

result = [];

%--
% get callback context and control value
%--

[control, par] = get_callback_context(obj);

%--
% switch callback on control name
%--

switch control.name
	
	case 'name'
		
		%--
		% check name is a proper name
		%--
		
		value = get_control(par.handle, control.name, 'value');
		
		if ~proper_filename(value)
			warn_dialog({'Library name must be ', 'a proper file name.'}, 'Invalid Library Name'); 
			set_control(par.handle,control.name,'value', '');
		end
		
	case 'location'
		
		value = get_control(par.handle, control.name, 'value');
		
		if ~proper_filename(value)
			warn_dialog({'User name must be ', 'a proper file name.'}, 'Invalid Library Location'); 
			set_control(par.handle,control.name,'value', user_root(get_active_user, 'lib'));
		end
		
		
end
