function [p] = gui_mesh_write(p)

% gui_mesh_write - Save a mesh (triangulation) file
% 
% Usage: [p] = gui_mesh_write(p)
%
% p is a structure, generated by 'eeg_toolbox_defaults'
%
% See also mesh_write
% 

% $Revision: 1.1 $ $Date: 2009-04-28 22:13:56 $

% Licence:  GNU GPL, no express or implied warranties
% History:  02/02 Darren.Weber_at_radiology.ucsf.edu
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ~exist('p','var'),
    fprintf('GUI_MESH_WRITE: No input p struct.\n');
    return
elseif isempty(p),
    fprintf('GUI_MESH_WRITE: Input p struct is empty.\n');
    return
elseif isempty(p.mesh.data),
    fprintf('GUI_MESH_WRITE: Input p struct has no mesh data.\n');
    return
end


MESHWrite = INIT(p);


return



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [MESHWrite] = INIT(p),
    
    % GUI General Parameters
    
    GUIwidth  = 500;
    GUIheight = 120;
    
    GUI = figure('Name','MESH File Write','Tag','MESH_WRITE',...
                 'NumberTitle','off',...
                 'MenuBar','none','Position',[1 1 GUIwidth GUIheight]);
    movegui(GUI,'center');
    
    MESHWrite.gui = GUI;
    
    Font.FontName   = 'Helvetica';
    Font.FontUnits  = 'Pixels';
    Font.FontSize   = 12;
    Font.FontWeight = 'normal';
    Font.FontAngle  = 'normal';
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Mesh Data Selection and Parameters

    G.Title_data = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[.01 .75 .17 .2],...
        'TooltipString','Mesh/Tesselation Formats (see, ''help mesh_write'')',...
        'String','Mesh Type:','HorizontalAlignment','left');
    
    switch p.mesh.type
        case 'BrainStorm',      meshType = 1;
        case 'EMSE',            meshType = 2;
        otherwise,              meshType = 1;
    end
    
    G.PmeshType = uicontrol('Tag','PmeshType','Parent',GUI,'Style','popupmenu',...
        'Units','Normalized',Font,  ...
        'Position',[.20 .75 .28 .2],...
        'String',{'BrainStorm' 'EMSE'},'Value',meshType,...
        'TooltipString','Mesh/Tesselation Formats (see, ''help mesh_write'')',...
        'Callback',strcat('MESHWrite = get(gcbf,''Userdata'');',...
                          'MESHWrite.p.mesh.type = popupstr(MESHWrite.handles.PmeshType);',...
                          'set(gcbf,''Userdata'',MESHWrite); clear MESHWrite;'));
                      
    G.Title_path = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[.01 .50 .17 .2],...
        'String','Path','HorizontalAlignment','left');
    G.EmeshPath = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
    	'Position',[.20 .50 .58 .2], 'String',p.mesh.path,...
        'Callback',strcat('MESHWrite = get(gcbf,''Userdata'');',...
                          'MESHWrite.p.mesh.path = get(MESHWrite.handles.EmeshPath,''String'');',...
                          'set(gcbf,''Userdata'',MESHWrite); clear MESHWrite;'));
    
    G.Title_file = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[.01 .25 .17 .2],...
        'String','File','HorizontalAlignment','left');
    G.EmeshFile = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
    	'Position',[.20 .25 .58 .2], 'String',p.mesh.file,...
        'Callback',strcat('MESHWrite = get(gcbf,''Userdata'');',...
                          'MESHWrite.p.mesh.file = get(MESHWrite.handles.EmeshFile,''String'');',...
                          'set(gcbf,''Userdata'',MESHWrite); clear MESHWrite;'));
    
    Font.FontWeight = 'bold';
    
    % BROWSE: Look for the data
    browsecommand = strcat('MESHWrite = get(gcbf,''Userdata'');',...
        'cd(MESHWrite.p.mesh.path);',...
        '[file, path] = uigetfile(',...
        '{''*.mat'', ''BrainStorm Tesselations (*.mat)'';', ...
        '*.wfr'', ''EMSE Tesselations (*.wfr)'';', ...
        ' ''*.*'',   ''All Files (*.*)''},', ...
        '''Select Mesh/Tesselation File'');',...
        'if ~isequal(path,0), MESHWrite.p.mesh.path = path; end;',...
        'if ~isequal(file,0), MESHWrite.p.mesh.file = file; end;',...
        'if ~isempty(findstr(file,''.dat'')) | ~isempty(findstr(file,''.txt'')), ',...
            'MESHWrite.p.mesh.file = strcat(file,''.subjecttess.mat'');',...
            'set(MESHWrite.handles.PmeshType,''Value'',1); ',...
            'MESHWrite.p.mesh.type = popupstr(MESHWrite.handles.PmeshType);',...
        'elseif ~isempty(findstr(file,''.asc'')) | ~isempty(findstr(file,''.tri'')), ',...
            'MESHWrite.p.mesh.file = strcat(file,''.subjecttess.mat'');',...
            'set(MESHWrite.handles.PmeshType,''Value'',1); ',...
            'MESHWrite.p.mesh.type = popupstr(MESHWrite.handles.PmeshType);',...
        'elseif findstr(file,''.wfr''), ',...
            'set(MESHWrite.handles.PmeshType,''Value'',2); ',...
            'MESHWrite.p.mesh.type = popupstr(MESHWrite.handles.PmeshType);',...
        'else, ',...
            'set(MESHWrite.handles.PmeshType,''Value'',1); ',...
            'MESHWrite.p.mesh.type = popupstr(MESHWrite.handles.PmeshType);',...
        'end;',...
        'set(MESHWrite.handles.EmeshPath,''String'',MESHWrite.p.mesh.path);',...
        'set(MESHWrite.handles.EmeshFile,''String'',MESHWrite.p.mesh.file);',...
        'set(gcbf,''Userdata'',MESHWrite); clear MESHWrite;');
    G.BmeshFile = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized',Font, ...
        'Position',[.01 .01 .17 .2], 'String','BROWSE',...
        'BackgroundColor',[0.8 0.8 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback', browsecommand );
    
    % SAVE AS: convert the current mesh data!
    G.Bsave = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[.40 .01 .18 .2],...
        'String','SAVE AS','BusyAction','queue',...
        'TooltipString','Save the current mesh (only BrainStorm format at present).',...
        'BackgroundColor',[0.0 0.6 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback',strcat('MESHWrite = get(gcbf,''Userdata'');',...
                          'set(MESHWrite.gui,''pointer'',''watch'');',...
                          'tmp = mesh_write(MESHWrite.p);',...
                          'clear tmp MESHWrite; close gcbf;'));
    
    % Cancel
    G.Bcancel = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[.80 .01 .18 .2],...
        'String','CANCEL','BusyAction','queue',...
        'TooltipString','Close, do not save the mesh.',...
        'BackgroundColor',[0.75 0.0 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback','close gcbf;');
    
    % Store userdata
    MESHWrite.handles = G;
    MESHWrite.p = p;
    set(GUI,'HandleVisibility','callback');
    set(GUI,'Userdata',MESHWrite);
    
return
