function [p] = gui_mesh_open(p,command,parent)

% gui_mesh_open - GUI to mesh_open
% 
% Usage: [p] = gui_mesh_open(p,[command],[parent])
%
% p        - struct, generated by 'eeg_toolbox_defaults'
% command  - either 'init' or 'load'
% parent   - a handle to the gui that calls this gui, used
%            to update the UserData of the parent. The p 
%            struct is returned to the parent in this way.
% 
% See command line version, mesh_open
% 

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

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

if ~exist('p','var'),
 [p] = eeg_toolbox_defaults;
elseif isempty(p),
 [p] = eeg_toolbox_defaults;
end

if ~exist('command','var'),
  command = 'init';
elseif isempty(command),
  command = 'init';
end

command = lower(command);

switch command,
  
  case 'init',
    
    if exist('parent','var'),
      MESHOpen = INIT(p,parent);
    else
      MESHOpen = INIT(p);
    end
    
    
  case 'plot',
    
    MESHOpen = get(gcbf,'Userdata');
    
    MESHOpen.p = mesh_open(MESHOpen.p);
    
    set(MESHOpen.gui,'Userdata',MESHOpen);
    
    if isequal(get(MESHOpen.handles.Bhold,'Value'),1),
      
     [p] = gui_updateparent(MESHOpen,0);
     [p] = gui_mesh_plot(p,'init',MESHOpen.gui);
      
    else
      
     [p] = gui_updateparent(MESHOpen);
      if isfield(MESHOpen,'parent'),
        if ~isempty(MESHOpen.parent),
          gui_mesh_plot(MESHOpen.p,'init',MESHOpen.parent.gui);
        end
      else
        gui_mesh_plot(MESHOpen.p,'init','');
      end
      close gcbf;
    end
    
    
  case 'save',
    
    MESHOpen = get(gcbf,'Userdata');
    gui_mesh_write(MESHOpen.p);
    
    if isequal(get(MESHOpen.handles.Bhold,'Value'),1),
     [p] = gui_updateparent(MESHOpen,0);
    else
     [p] = gui_updateparent(MESHOpen);
      close gcbf;
    end
    
    
  case 'return',
    
    MESHOpen = get(gcbf,'Userdata');
    
    set(MESHOpen.gui,'Pointer','watch');
    
    MESHOpen.p = mesh_open(MESHOpen.p);
    
    set(MESHOpen.gui,'Userdata',MESHOpen);
    
    set(MESHOpen.gui,'Pointer','arrow');
    
    if isequal(get(MESHOpen.handles.Bhold,'Value'),1),
     [p] = gui_updateparent(MESHOpen,0);
    else
     [p] = gui_updateparent(MESHOpen);
      close gcbf;
    end
    
    
  otherwise,
    
    close gcbf;
    
end

return



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [MESHOpen] = INIT(p,parent),

% GUI General Parameters

GUIwidth  = 500;
GUIheight = 120;

version = '$Revision: 1.1 $';
name = sprintf('MESH File Open [v %s]\n',version(11:15));

GUI = figure('Name',name,'Tag','MESH_OPEN',...
  'NumberTitle','off',...
  'MenuBar','none','Position',[1 1 GUIwidth GUIheight]);
movegui(GUI,'center');

MESHOpen.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_open'')',...
  'String','Mesh Type:','HorizontalAlignment','left');

switch lower(p.mesh.type),
  case 'brainstorm',          meshType = 1;
  case 'emse',                meshType = 2;
  case 'ascii',               meshType = 3;
  case 'freesurfer_ascii',    meshType = 4;
  case 'freesurfer_surf',     meshType = 5;
  case 'freesurfer_curv',     meshType = 6;
  case 'freesurfer_overlay',  meshType = 7;
  otherwise,                  meshType = 1;
end

meshtypes = {'BrainStorm' 'EMSE' 'ASCII' ...
    'FreeSurfer_ascii' 'FreeSurfer_surf' 'FreeSurfer_curv' 'FreeSurfer_overlay' };
G.PmeshType = uicontrol('Tag','PmeshType','Parent',GUI,'Style','popupmenu',...
  'Units','Normalized',Font,  ...
  'Position',[.20 .75 .28 .2],...
  'String',meshtypes,'Value',meshType,...
  'TooltipString','Mesh/Triangulation Formats (see, ''help mesh_open'', FS = FreeSurfer)',...
  'Callback',strcat('MESHOpen = get(gcbf,''Userdata'');',...
  'MESHOpen.p.mesh.type = popupstr(MESHOpen.handles.PmeshType);',...
  'set(gcbf,''Userdata'',MESHOpen); clear MESHOpen;'));

G.BmeshReplace = uicontrol('Tag','BmeshReplace','Parent',GUI,'Style','checkbox',...
  'Units','Normalized',Font,...
  'Position',[.40 .75 .18 .2],...
  'String','Replace','Value',1,...
  'TooltipString','Reload current mesh, if any?',...
  'Visible','off',...
  'Callback',strcat('MESHOpen = get(gcbf,''Userdata'');',...
  'MESHOpen.p.mesh.reload = get(MESHOpen.handles.BmeshReplace,''Value'');',...
  'set(gcbf,''Userdata'',MESHOpen); clear MESHOpen;'));

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('MESHOpen = get(gcbf,''Userdata'');',...
  'MESHOpen.p.mesh.path = get(MESHOpen.handles.EmeshPath,''String'');',...
  'set(gcbf,''Userdata'',MESHOpen); clear MESHOpen;'));

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('MESHOpen = get(gcbf,''Userdata'');',...
  'MESHOpen.p.mesh.file = get(MESHOpen.handles.EmeshFile,''String'');',...
  'set(gcbf,''Userdata'',MESHOpen); clear MESHOpen;'));

Font.FontWeight = 'bold';

% BROWSE: Look for the data
browsecommand = strcat('MESHOpen = get(gcbf,''Userdata'');',...
  'cd(MESHOpen.p.mesh.path);',...
  '[file, path] = uigetfile(',...
  '{''*.dat;*.txt'', ''ASCII Tesselations (*.dat,*.txt)'';', ...
  ' ''*.asc;*.tri'', ''FreeSurfer Tesselations (*.asc,*.tri)'';', ...
  ' ''*.orig;*.smoothwm;*.white;*.pial'', ''FreeSurfer Surfaces'';', ...
  ' ''*.wfr'', ''EMSE Tesselations (*.wfr)'';', ...
  ' ''*.mat'', ''BrainStorm Tesselations (*.mat)'';', ...
  ' ''*.*'',   ''All Files (*.*)''},', ...
  '''Select Mesh/Tesselation File'');',...
  'if ~isequal(path,0), MESHOpen.p.mesh.path = path; end;',...
  'if ~isequal(file,0), MESHOpen.p.mesh.file = file; end;',...
  'set(MESHOpen.handles.EmeshPath,''String'',MESHOpen.p.mesh.path);',...
  'set(MESHOpen.handles.EmeshFile,''String'',MESHOpen.p.mesh.file);',...
  'if     findstr(file,''.mat''), value = 1; ',...
  'elseif findstr(file,''.wfr''), value = 2; ',...
  'elseif findstr(file,''.dat''), value = 3; ',...
  'elseif findstr(file,''.txt''), value = 3; ',...
  'elseif findstr(file,''.asc''), value = 3; ',...
  'elseif findstr(file,''.tri''), value = 4; ',...
  'elseif findstr(file,''.orig''),     value = 5; ',...
  'elseif findstr(file,''.smoothwm''), value = 5; ',...
  'elseif findstr(file,''.white''),    value = 5; ',...
  'elseif findstr(file,''.pial''),     value = 5; ',...
  'else, value = 3; ',...
  'end;',...
  'set(MESHOpen.handles.PmeshType,''Value'',value); ',...
  'MESHOpen.p.mesh.type = popupstr(MESHOpen.handles.PmeshType);',...
  'set(gcbf,''Userdata'',MESHOpen); clear value file path MESHOpen;');
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 );

% PLOT: Load & plot the data!
G.Bplot = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.20 .01 .18 .2],...
  'String','PLOT','BusyAction','queue',...
  'TooltipString','Plot the mesh and return the file parameters.',...
  'BackgroundColor',[0.0 0.5 0.0],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('MESHOpen = get(gcbf,''Userdata'');',...
  'p = gui_mesh_open(MESHOpen.p,''plot'');',...
  'clear MESHOpen;'));

% SAVE AS: Load & convert the data!
G.Bsave = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.40 .01 .18 .2],...
  'String','SAVE AS','BusyAction','queue',...
  'TooltipString','Load and convert the mesh.',...
  'BackgroundColor',[0.0 0.0 0.5],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('MESHOpen = get(gcbf,''Userdata'');',...
  'p = gui_mesh_open(MESHOpen.p,''save'');',...
  'clear MESHOpen;'));

% Return file parameters
G.Breturn = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.60 .01 .18 .2],...
  'String','RETURN','BusyAction','queue',...
  'TooltipString','Close and return the file parameters.',...
  'BackgroundColor',[0.75 0.0 0.0],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('MESHOpen = get(gcbf,''Userdata'');',...
  'p = gui_mesh_open(MESHOpen.p,''return'');',...
  'clear MESHOpen;'));

% Cancel, do not return parameters
G.Bcancel = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.80 .01 .18 .2],...
  'String','CANCEL','BusyAction','queue',...
  'TooltipString','Close, do not return the p struct.',...
  'BackgroundColor',[0.75 0.0 0.0],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('MESHOpen = get(gcbf,''Userdata'');',...
  'p = gui_mesh_open(MESHOpen.p,''cancel'');',...
  'clear MESHOpen;'));

% Help
G.Bhelp = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.80 .25 .18 .2],'String','Help','BusyAction','queue',...
  'BackgroundColor',[1 1 0],...
  'ForegroundColor',[0 0 0], 'HorizontalAlignment', 'center',...
  'Callback','doc mesh_open;');

% Hold GUI Open checkbox
G.Bhold = uicontrol('Parent',GUI,'Style','checkbox','Units','Normalized', Font, ...
  'Position',[.80 .50 .18 .2],'String','Hold GUI','BusyAction','queue',...
  'TooltipString','MESH File Load GUI remains open after ''Plot'' and ''Return'' commands.',...
  'Value',p.hold,'HorizontalAlignment', 'center');

% Store userdata
if exist('parent','var'), MESHOpen.parent.gui = parent; end
MESHOpen.handles = G;
MESHOpen.p = p;
set(GUI,'HandleVisibility','callback');
set(GUI,'Userdata',MESHOpen);

return
