% Author: Javier Lopez-Calderon % Sam London
% Center for Mind and Brain
% University of California, Davis,
% Davis, CA
% 2009

%b8d3721ed219e65100184c6b95db209bb8d3721ed219e65100184c6b95db209b
%
% ERPLAB Toolbox
% Copyright © 2007 The Regents of the University of California
% Created by Javier Lopez-Calderon and Steven Luck
% Center for Mind and Brain, University of California, Davis,
% javlopez@ucdavis.edu, sjluck@ucdavis.edu
%
% 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 3 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, see <http://www.gnu.org/licenses/>.

function varargout = scalplotGUI(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
      'gui_Singleton',  gui_Singleton, ...
      'gui_OpeningFcn', @scalplotGUI_OpeningFcn, ...
      'gui_OutputFcn',  @scalplotGUI_OutputFcn, ...
      'gui_LayoutFcn',  [] , ...
      'gui_Callback',   []);
if nargin && ischar(varargin{1})
      gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
      [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
      gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

%--------------------------------------------------------------------------
function scalplotGUI_OpeningFcn(hObject, eventdata, handles, varargin)

% Choose default command line output for scalplotGUI
try
      plotset = evalin('base', 'plotset');
      
      if isfield(plotset.pscalp, 'posgui')
            if ~isempty(plotset.pscalp.posgui)
                  set(handles.figure_main,'Position', plotset.pscalp.posgui);
            end
      end
catch
      plotset.ptime  = [];
      plotset.pscalp = [];
      assignin('base','plotset',plotset)
end

handles.output   = plotset;
handles.indxline = 1;
handles.ispdf    = 0;
handles.isagif   = 0;

try
      ERP   = varargin{1};
      nchan = ERP.nchan;
      nbin  = ERP.nbin; % Total number of bins
      splinefile = ERP.splinefile;
catch
      ERP   = [];
      nchan = 0;
      nbin  = 0;
      splinefile = '';
end
try
      chanlocs = varargin{2};
catch
      chanlocs = [];
end
if isfield(plotset.pscalp, 'splineinfo')
      splineinfo = plotset.pscalp.splineinfo;
else
      splineinfo = [];
end
if isempty(splinefile)
      if isfield(splineinfo, 'path')
            splineinfo = plotset.pscalp.splineinfo;
            splinefile = splineinfo.path;
      end
end

handles.nchan      = nchan;
handles.nbin       = nbin;
handles.chanlocs   = chanlocs;
handles.splinefile = splinefile;
handles.splineinfo = splineinfo;
handles.binnum     = [];
handles.bindesc    = [];
handles.type       = [];
handles.latency    = [];
handles.electrodes = [];
handles.colorbar   = [];
handles.ismaxim    = [];

%
% Name & version
%
version = geterplabversion;
set(handles.figure_main,'Name', ['ERPLAB ' version '   -   SCALP MAPPING GUI'])

%
% Prepare List of current Bins
%
if ~isempty(ERP)
      listb = [];
      
      for b=1:nbin
            listb{b}= ['BIN' num2str(b) ' = ' ERP.bindescr{b} ];
      end
      
      set(handles.popupmenu_bins,'String', listb)
      drawnow
else
      set(handles.popupmenu_bins,'String', 'No Bins')
      drawnow
end

for ch =1:nchan
      listch{ch} = [num2str(ch) ' = ' ERP.chanlocs(ch).labels ];
end

%
% Color GUI
%
handles = painterplab(handles);

%
% Set font size
%
handles = setfonterplab(handles);

% Update handles structure
guidata(hObject, handles);

% help
% helpbutton
pdfbutton

setall(hObject, eventdata, handles)

% UIWAIT makes geterpvaluesGUI wait for user response (see UIRESUME)
uiwait(handles.figure_main);

%--------------------------------------------------------------------------
function varargout = scalplotGUI_OutputFcn(hObject, eventdata, handles)

varargout{1} = handles.output;

% The figure can be deleted now
delete(handles.figure_main);
pause(0.1)

%--------------------------------------------------------------------------
function edit_custom_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function edit_custom_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function edit_bins_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function edit_bins_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function edit_latencies_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function edit_latencies_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function popupmenu_bins_Callback(hObject, eventdata, handles)

numbin = get(hObject, 'Value');
nums   = get(handles.edit_bins, 'String');
nums   = [nums ' ' num2str(numbin)];
set(handles.edit_bins, 'String', nums);

%--------------------------------------------------------------------------
function popupmenu_bins_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function pushbutton_cancel_Callback(hObject, eventdata, handles)

plotset = evalin('base', 'plotset');
plotset.pscalp = [];
handles.output = plotset;
       
% Update handles structure
guidata(hObject, handles);
uiresume(handles.figure_main);

%--------------------------------------------------------------------------
function pushbutton_OK_Callback(hObject, eventdata, handles)

binArraystr    = strtrim(get(handles.edit_bins, 'String'));
latestr        = strtrim(get(handles.edit_latencies, 'String'));
customscale    = strtrim(get(handles.edit_custom, 'String'));
customscale    = regexprep(customscale,'''|"','');
nbin           = handles.nbin;
errorcusca     = 0;

if  ~strcmp(latestr, '') && ~isempty(latestr) && ...
            ~strcmp(binArraystr, '') && ~isempty(binArraystr)
      binArray    = str2num(binArraystr);
      if max(binArray)>nbin
            msgboxText{1} =  'Error: You have specified unexisting bins.';
            title = 'ERPLAB: scalplotGUI() error:';
            errorfound(msgboxText, title);
            return
      end
      
      if length(binArray)>length(unique(binArray))
            msgboxText{1} =  'Error: You have specified repeated bins.';
            title = 'ERPLAB: scalplotGUI() error:';
            errorfound(msgboxText, title);
            return
      end
      
      latencyArray = str2num(latestr);
      nlate = length(latencyArray);
      
      meamenu = get(handles.popupmenu_measurement, 'Value');
      
      switch meamenu
            case 1
                  measurement = 'insta';
            case 2
                  measurement = 'mean';
            case 3
                  measurement = 'area';
            case 4
                  measurement = 'instalapla';
            case 5
                  measurement = 'meanlapla';
            otherwise
                  measurement = 'insta';
      end
      
      %
      % Baseline
      %
      if get(handles.radiobutton_BLC_no, 'Value')
            baseline = 'none';
      end
      if get(handles.radiobutton_BLC_pre, 'Value')
            baseline = 'pre';
      end
      if get(handles.radiobutton_BLC_post, 'Value')
            baseline = 'post';
      end
      if get(handles.radiobutton_BLC_whole, 'Value')
            baseline = 'all';
      end
      if get(handles.radiobutton_BLC_custom, 'Value')
            numbl = str2num(get(handles.edit_customblc, 'String'));
            if isempty(numbl)
                  return
            else
                  if length(numbl)==2
                        baseline = [numbl(1) numbl(2)];
                  else
                        return
                  end
            end
      end
      if get(handles.radiobutton_maxmin, 'Value')
            cscale = 'maxmin';
      end
      if get(handles.radiobutton_absmax, 'Value')
            cscale = 'absmax';
      end
      if get(handles.radiobutton_custom, 'Value')
            if ~strcmp(customscale, '') && ~isempty(customscale) && ~strcmpi(customscale, 'auto')
                  cusca  = str2num(customscale);
                  ncusca = length(cusca);
                  if ncusca == 2
                        if cusca(1)<cusca(2)
                              cscale = cusca;
                        else
                              errorcusca = 1;
                        end
                  else
                        errorcusca = 1;
                  end
            elseif strcmpi(customscale, 'auto')
                  cscale = 'auto';
            else
                  errorcusca = 1;
            end
            if errorcusca
                  colorold = get(handles.edit_custom, 'BackgroundColor');
                  set(handles.edit_custom, 'BackgroundColor', [1 0 0]);
                  pause(0.5)
                  set(handles.edit_custom, 'BackgroundColor', colorold);
                  beep
                  return
            end
      end
      
      %
      % Color Bar
      %
      %      clrbar = get(handles.checkbox_colorbar, 'Value');
      
      %
      % Show electrodes
      %
      showelec = get(handles.set_legends, 'Value');
      
      %
      % Show bin number at legend
      %
      %binleg = get(handles.checkbox_includenumberbin, 'Value');
      
      if get(handles.checkbox_agif, 'Value')
            if get(handles.checkbox_adjust1frame, 'Value')
                  isagif    = 2; % adjust first frame
            else
                  isagif    = 1;
            end
            
            delayt    = str2num(get(handles.edit_delayt, 'String'));
            fnameagif = get(handles.edit_fnameagif, 'String');
            
            if isempty(delayt) || delayt<0 || delayt>655
                  msgboxText{1} =  'Error: You must specify a scalar value between 0 and 655 inclusive';
                  title = 'ERPLAB: scalplotGUI() error:';
                  errorfound(msgboxText, title);
                  return
            end
            if isempty(strtrim(fnameagif))
                  msgboxText{1} =  'Error: You must specify a valid file name for your animated GIF';
                  title = 'ERPLAB: scalplotGUI() error:';
                  errorfound(msgboxText, title);
                  return
            else
                  [pthxz, fnamez, ext] = fileparts(fnameagif);
                  
                  if strcmp(ext,'')
                        ext   = '.gif';
                  end
                  fnameagif = fullfile(pthxz,[ fnamez ext]);
            end
      else
            isagif    = 0;
            delayt    = [];
            fnameagif = '';
      end
      
      %
      % Orientation view
      %
      viewselec = get(handles.popupmenu_orientation, 'Value');
      
      if get(handles.radio_2D, 'Value')==1  % 2D
            mtype = '2D';
            switch viewselec
                  case 1
                        mapview = '+X';
                  case 2
                        mapview = '-X';
                  case 3
                        mapview = '+Y';
                  case 4
                        mapview = '-Y';
                  otherwise
                        return
            end
            splineinfo.path =  '';
            splineinfo.new  = 0;
            splineinfo.save = 0;
      else % 3D
            if isempty(handles.splinefile)
                  splineinfo = splinefileGUI(handles.splinefile);
                  if isempty(splineinfo) || isempty(splineinfo.path)
                        msgboxText =  'You must specify a name for the spline file.';
                        title = 'ERPLAB: scalpplotGUI inputs';
                        errorfound(msgboxText, title);
                        return
                  end
            else
                  splineinfo =   handles.splineinfo;
                  %                   splineinfo.path =   handles.splinefile;
                  %                   splineinfo.new  = 0;
                  %                   splineinfo.save = 0;
                  %                   splineinfo.newname = [];
            end
            mtype = '3D';
            switch viewselec
                  case 1
                        mapview = 'front';
                  case 2
                        mapview = 'back';
                  case 3
                        mapview = 'right';
                  case 4
                        mapview = 'left';
                  case 5
                        mapview = 'top';
                  case 6
                        mapview = 'frontleft';
                  case 7
                        mapview = 'frontright';
                  case 8
                        mapview = 'backleft';
                  case 9
                        mapview = 'backright';
                  case 10
                        mapview = str2num(strtrim(get(handles.edit_customview, 'String')));
                  otherwise
                        return
            end
      end
end

%
% Prepares output
%
ispdf = 0;
plotset = evalin('base', 'plotset');
plotset.pscalp.binArray     = binArray;
plotset.pscalp.latencyArray = latencyArray;
plotset.pscalp.measurement  = measurement;
plotset.pscalp.baseline     = baseline;
plotset.pscalp.cscale       = cscale;
plotset.pscalp.ispdf        = ispdf;
plotset.pscalp.agif.value   = isagif;
plotset.pscalp.agif.delay   = delayt;
plotset.pscalp.agif.fname   = fnameagif;
plotset.pscalp.posgui       = get(handles.figure_main,'Position');
plotset.pscalp.mtype        = mtype; % map type  0=2D; 1=3D
plotset.pscalp.mapview      = mapview; % numeric?
plotset.pscalp.splineinfo   = splineinfo;
% legends
plotset.pscalp.plegend.binnum     = handles.Legbinnum;
plotset.pscalp.plegend.bindesc    = handles.Legbindesc;
plotset.pscalp.plegend.type       = handles.Legtype;
plotset.pscalp.plegend.latency    = handles.Leglatency;
plotset.pscalp.plegend.electrodes = handles.Legelectrodes;
plotset.pscalp.plegend.colorbar   = handles.Legcolorbar;
plotset.pscalp.plegend.maximize   = handles.Legismaxim;

handles.output  = plotset;

% Update handles structure
guidata(hObject, handles);
uiresume(handles.figure_main);
%         end
% end

%--------------------------------------------------------------------------
function radiobutton_maxmin_Callback(hObject, eventdata, handles)

set(handles.edit_custom, 'Enable','off');
set(hObject,'Value',1)

%--------------------------------------------------------------------------
function radiobutton_absmax_Callback(hObject, eventdata, handles)

set(handles.edit_custom, 'Enable','off');
set(hObject,'Value',1)

%--------------------------------------------------------------------------
function radiobutton_custom_Callback(hObject, eventdata, handles)

set(handles.edit_custom, 'Enable','on');
set(hObject,'Value',1)

%--------------------------------------------------------------------------
function edit_customblc_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function edit_customblc_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function pushbutton_pdf_Callback(hObject, eventdata, handles)

plotset = evalin('base', 'plotset');
plotset.pscalp  = 'pdf';
handles.output = plotset;
guidata(hObject, handles);
uiresume(handles.figure_main);

%--------------------------------------------------------------------------
function radiobutton_BLC_custom_Callback(hObject, eventdata, handles)

set(handles.edit_customblc,'Enable', 'on')
set(hObject,'Value',1)

%--------------------------------------------------------------------------
function radiobutton_BLC_no_Callback(hObject, eventdata, handles)

set(handles.edit_customblc,'Enable', 'off')
set(hObject,'Value',1)

%--------------------------------------------------------------------------
function radiobutton_BLC_pre_Callback(hObject, eventdata, handles)

set(handles.edit_customblc,'Enable', 'off')
set(hObject,'Value',1)

%--------------------------------------------------------------------------
function radiobutton_BLC_post_Callback(hObject, eventdata, handles)

set(handles.edit_customblc,'Enable', 'off')
set(hObject,'Value',1)

%--------------------------------------------------------------------------
function radiobutton_BLC_whole_Callback(hObject, eventdata, handles)

set(handles.edit_customblc,'Enable', 'off')
set(hObject,'Value',1)

% %--------------------------------------------------------------------------
% function radiobutton_area_Callback(hObject, eventdata, handles)
%
% set(hObject,'Value',1)
% set(handles.text_example,'String','e.g. -100 0 or 120 200;300 400')
%
% %--------------------------------------------------------------------------
% function radiobutton_insta_Callback(hObject, eventdata, handles)
%
% set(hObject,'Value',1)
% set(handles.text_example,'String','e.g. 300 or -100:10:0 or 120 400')
%
% %--------------------------------------------------------------------------
% function radiobutton_mean_Callback(hObject, eventdata, handles)
%
% set(hObject,'Value',1)
% set(handles.text_example,'String','e.g. -100 0 or 120 200;300 400')

%--------------------------------------------------------------------------
function  setall(hObject, eventdata, handles)

plotset = handles.output;
if ~isfield(plotset, 'pscalp')
      plotset.pscalp = [];
end

measurearray = {'Instantaneous amplitude','Mean amplitude between two fixed latencies',...
      'Area between two fixed latencies', 'Instantaneous amplitude Laplacian', 'Mean amplitude Laplacian'};
set(handles.popupmenu_measurement, 'String', measurearray);

%
% Set GUI
%
if isempty(plotset.pscalp)
      binArraystr = '1';
      latvals = 0;
      latrows = 1;
      latencyArraystr = vect2colon(latvals, 'Delimiter','off');
      measurement = 'insta';
      baseline    = 'pre';
      cscale      = 'maxmin';
      mtype       = '2D';
      mapview     = '+X';
      
      Legbinnum     = 1;
      Legbindesc    = 0;
      Legtype       = 0;
      Leglatency    = 1;
      Legelectrodes = 1;
      Legcolorbar   = 0; % no color bar by default
      Legismaxim    = 0; % no maximize figure by default
else
      binArraystr = vect2colon(plotset.pscalp.binArray,'Delimiter','off', 'Repeat', 'off');
      latvals = plotset.pscalp.latencyArray;
      latrows = size(latvals,1);
      
      if latrows>1
            latencyArraystr = num2str(latvals);
            bb = cellstr(latencyArraystr);
            xs = ''; for t=1:length(bb);xs = [xs bb{t} ' ; '];end
            latencyArraystr = regexprep(xs,';\s*$','');
      else
            latencyArraystr = vect2colon(latvals, 'Delimiter','off');
      end
      
      measurement   = plotset.pscalp.measurement;
      baseline      = plotset.pscalp.baseline;
      cscale        = plotset.pscalp.cscale;
      %clrbar       = plotset.pscalp.colorbar;
      %showelec     = plotset.pscalp.showelec;
      %binleg       = plotset.pscalp.binleg;
      mtype         = plotset.pscalp.mtype; % map type  0=2D; 1=3D
      mapview       = plotset.pscalp.mapview; % numeric?
      
      plegend       = plotset.pscalp.plegend; % numeric
      Legbinnum     = plegend.binnum;
      Legbindesc    = plegend.bindesc;
      Legtype       = plegend.type ;
      Leglatency    = plegend.latency;
      Legelectrodes = plegend.electrodes;
      Legcolorbar   = plegend.colorbar;
      Legismaxim    = plegend.maximize;
end

%
% Value to plot menu
%
if strcmpi(measurement,'insta')
      meamenu =1;
elseif strcmpi(measurement,'mean')
      meamenu =2;
elseif strcmpi(measurement,'area')
      meamenu =3;
elseif strcmpi(measurement,'instalapla') || strcmpi(measurement,'lapla')
      meamenu =4;
elseif strcmpi(measurement,'meanlapla')
      meamenu =5;
else
      meamenu =1;
end
set(handles.popupmenu_measurement, 'Value', meamenu);
switch meamenu
      case {1,4}
            set(handles.text_example,'String','e.g. 300 or 100:50:350 to plot scalp maps at 300 or at 100,150,200,...,350 ms')
      case {2,3,5}
            set(handles.text_example,'String','e.g., 300 400 ; 400 500 to plot scalp maps for 300-400, 400-500 ms)')
end

%
% Baseline setting
%
if ischar(baseline)
      if strcmpi(baseline,'none')
            set(handles.radiobutton_BLC_no,'Value',1);
      elseif strcmpi(baseline,'pre')
            set(handles.radiobutton_BLC_pre,'Value',1);
      elseif strcmpi(baseline,'post')
            set(handles.radiobutton_BLC_post,'Value',1);
      elseif strcmpi(baseline,'all')
            set(handles.radiobutton_BLC_whole,'Value',1);
      else
            numblc = str2num(baseline);
            if isempty(numblc)
                  set(handles.radiobutton_BLC_pre,'Value',1); %default
                  set(handles.edit_customblc,'Enable', 'off')
            else
                  if size(numblc,1)~=1 || size(numblc,2)~=2
                        set(handles.radiobutton_BLC_pre,'Value',1); %default
                        set(handles.edit_customblc,'Enable', 'off')
                  else
                        set(handles.radiobutton_BLC_custom,'Value',1); %custom
                        set(handles.edit_customblc,'String', baseline); %custom
                  end
            end
      end
else
      if size(baseline,1)~=1 || size(baseline,2)~=2
            set(handles.radiobutton_BLC_pre,'Value',1); %default
            set(handles.edit_customblc,'Enable', 'off')
      else
            set(handles.radiobutton_BLC_custom,'Value',1); %custom
            set(handles.edit_customblc,'String', num2str(baseline)); %custom
      end
end

%
% Scale
%
if ischar(cscale)
      if strcmpi(cscale,'maxmin')
            set(handles.radiobutton_maxmin,'Value',1);
            set(handles.edit_custom,'Enable', 'off')
      elseif strcmpi(cscale,'absmax')
            set(handles.radiobutton_absmax,'Value',1);
            set(handles.edit_custom,'Enable', 'off')
      else
            numscale = str2num(cscale);
            if isempty(numscale)
                  set(handles.radiobutton_maxmin,'Value',1); %default
                  set(handles.edit_custom,'Enable', 'off')
            else
                  if size(numscale,1)~=1 || size(numscale,2)~=2
                        set(handles.radiobutton_maxmin,'Value',1); %default
                        set(handles.edit_custom,'Enable', 'off')
                  else
                        set(handles.radiobutton_custom,'Value',1); %custom
                        set(handles.edit_custom,'Enable', 'on')
                        set(handles.edit_custom,'String', cscale); %custom
                  end
            end
      end
else
      if size(cscale,1)~=1 || size(cscale,2)~=2
            set(handles.radiobutton_maxmin,'Value',1); %default
            set(handles.edit_custom,'Enable', 'off')
      else
            set(handles.radiobutton_custom,'Value',1); %custom
            set(handles.edit_custom,'Enable', 'on')
            set(handles.edit_custom,'String', num2str(cscale)); %custom
      end
end

set(handles.edit_bins,'String',binArraystr);
set(handles.edit_latencies,'String',latencyArraystr);

%
% 2D or 3D
%
if strcmpi(mtype,'2D')
      morimenu = {'+X','-X','+Y','-Y'};
      if ischar(mapview)
            mview = find(ismember(morimenu, mapview));
      else
            mview = 1;
      end
      set(handles.radio_2D, 'Value', 1);
      set(handles.radio_3D, 'Value', 0);
      set(handles.edit_customview, 'Enable', 'off')
else
      morimenu = {'front', 'back', 'right', 'left', 'top',...
            'frontleft', 'frontright', 'backleft', 'backright',...
            'custom'};
      if ischar(mapview)
            mview = find(ismember(morimenu, mapview));
            set(handles.edit_customview,'String','')
            set(handles.edit_customview, 'Enable','off' )
      else
            mview = length(morimenu);
            set(handles.edit_customview, 'Enable','on' )
            set(handles.edit_customview,'String', num2str(mapview))
            %set(handles.popupmenu_orientation,'Value', length(morimenu))
      end
      set(handles.radio_2D, 'Value', 0);
      set(handles.radio_3D, 'Value', 1);
end
if isempty(mview)
      mview =1;
else
      if mview==0
            mview=1;
      end
end

set(handles.popupmenu_orientation, 'String', morimenu);
set(handles.popupmenu_orientation, 'Value', mview);
popupmenu_orientation_Callback(hObject, eventdata, handles)

if isfield(plotset.pscalp, 'agif')
      agif      = plotset.pscalp.agif.value;
      delayt    = plotset.pscalp.agif.delay;
      fnameagif = plotset.pscalp.agif.fname;
      
      if agif>0
            set(handles.checkbox_agif,'Value', 1)
            set(handles.checkbox_adjust1frame,'Enable', 'on')
            set(handles.edit_delayt,'Enable', 'on')
            set(handles.edit_delayt,'String', num2str(delayt))
            set(handles.edit_fnameagif,'Enable', 'on')
            set(handles.edit_fnameagif,'String', fnameagif)
            set(handles.pushbutton_browseagif,'Enable', 'on')
            
            if agif==2
                  set(handles.checkbox_adjust1frame,'Value', 1)
            else
                  set(handles.checkbox_adjust1frame,'Value', 0)
            end            
            set(handles.checkbox_adjust1frame,'Enable', 'on')
      else
            set(handles.checkbox_agif,'Value', 0)
            set(handles.checkbox_agif,'Value', 0)
            set(handles.checkbox_adjust1frame,'Value', 0)
            set(handles.checkbox_adjust1frame,'Enable', 'off')
            set(handles.edit_delayt,'Enable', 'off')
            set(handles.edit_fnameagif,'Enable', 'off')
            set(handles.pushbutton_browseagif,'Enable', 'off')
            set(handles.checkbox_adjust1frame,'Value', 0)
            set(handles.checkbox_adjust1frame,'Enable', 'off')
      end
else
      set(handles.checkbox_agif,'Value', 0)
      set(handles.checkbox_adjust1frame,'Value', 0)
      set(handles.checkbox_adjust1frame,'Enable', 'off')
      set(handles.edit_delayt,'Enable', 'off')
      set(handles.edit_fnameagif,'Enable', 'off')
      set(handles.pushbutton_browseagif,'Enable', 'off')
      set(handles.checkbox_adjust1frame,'Value', 0)
      set(handles.checkbox_adjust1frame,'Enable', 'off')
end

handles.Legbinnum     = Legbinnum;
handles.Legbindesc    = Legbindesc;
handles.Legtype       = Legtype;
handles.Leglatency    = Leglatency;
handles.Legelectrodes = Legelectrodes;
handles.Legcolorbar   = Legcolorbar;
handles.Legismaxim    = Legismaxim;

%Update handles structure
guidata(hObject, handles);

%--------------------------------------------------------------------------
function edit_exchan_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function edit_exchan_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function popupmenu_exchan_Callback(hObject, eventdata, handles)
numch = get(hObject, 'Value');
nums  = get(handles.edit_exchan, 'String');
nums  = [nums ' ' num2str(numch)];
set(handles.edit_exchan, 'String', nums);

%--------------------------------------------------------------------------
function popupmenu_exchan_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function checkbox_agif_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
      set(handles.edit_delayt,'Enable', 'on')
      set(handles.edit_fnameagif,'Enable', 'on')
      set(handles.pushbutton_browseagif,'Enable', 'on')
      set(handles.checkbox_adjust1frame,'Enable', 'on')
else
      set(handles.edit_delayt,'Enable', 'off')
      set(handles.edit_fnameagif,'Enable', 'off')
      set(handles.pushbutton_browseagif,'Enable', 'off')
      set(handles.checkbox_adjust1frame,'Enable', 'off')
end

%--------------------------------------------------------------------------
function edit_fnameagif_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function edit_fnameagif_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function pushbutton_browseagif_Callback(hObject, eventdata, handles)

%
% Save GIF
%
prename = get(handles.edit_fnameagif,'String');
[blfilename, blpathname, filterindex] = uiputfile({'*.gif';'*.*'},'Save Animated GIF as',prename);

if isequal(blfilename,0)
      disp('User selected Cancel')
      return
else
      [px, fname, ext] = fileparts(blfilename);
      
      if strcmp(ext,'')
            if filterindex==1
                  ext   = '.gif';
            end
      end
      
      fname = [ fname ext];
      fullgifname = fullfile(blpathname, fname);
      set(handles.edit_fnameagif,'String', fullgifname);
      disp(['Animated GIF will be saved at ' fullgifname])
end

%--------------------------------------------------------------------------
function edit_delayt_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function edit_delayt_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
% function checkbox_colorbar_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function pushbutton_ploterp_Callback(hObject, eventdata, handles)

plotset = getplotset(hObject, eventdata, handles);

if isempty(plotset.ptime)
      return
else
      plotset.ptime.binArray = plotset.pscalp.binArray ;
      plotset.ptime.blcorr   =  plotset.pscalp.baseline;
      assignin('base','plotset', plotset);
      handles.eplot = 1;
      
      %Update handles structure
      guidata(hObject, handles);
      uiresume(handles.figure1);
end

%--------------------------------------------------------------------------
function set_legends_Callback(hObject, eventdata, handles)
binnum     = handles.Legbinnum;
bindesc    = handles.Legbindesc;
type       = handles.Legtype;
latency    = handles.Leglatency;
electrodes = handles.Legelectrodes;
colorbar   = handles.Legcolorbar;
ismaxim    = handles.Legismaxim;

values = [binnum bindesc type latency electrodes colorbar ismaxim];
%
% call gui
%
answer = includelabelscalpGUI(values);
if isempty(answer)
      disp('User selected Cancel')
      return
end
handles.Legbinnum     = answer(1);
handles.Legbindesc    = answer(2);
handles.Legtype       = answer(3);
handles.Leglatency    = answer(4);
handles.Legelectrodes = answer(5);
handles.Legcolorbar   = answer(6);
handles.Legismaxim    = answer(7);

%Update handles structure
guidata(hObject, handles);

%--------------------------------------------------------------------------
% function checkbox_includenumberbin_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function pushbutton_CLERPF_Callback(hObject, eventdata, handles)
clerpf

%--------------------------------------------------------------------------
function checkbox_adjust1frame_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function radio_2D_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
      set(handles.radio_3D, 'Value',0)
      morimenu = {'+X','-X','+Y','-Y'};
      %morimenu = {'front', 'back', 'right', 'left', 'top',...
      %      'frontleft', 'frontright', 'backleft', 'backright',...
      %      'custom'};
      set(handles.popupmenu_orientation, 'Value', 1)
      set(handles.popupmenu_orientation, 'String', morimenu)
      set(handles.popupmenu_orientation, 'Enable', 'off')% sept 12, 2012. JLC
      set(handles.edit_customview, 'Enable', 'off')
      set(handles.pushbutton_splinefile, 'Enable', 'off')      
else
      set(handles.radio_2D, 'Value',1)
end

%--------------------------------------------------------------------------
function radio_2D_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function radio_3D_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
      set(handles.radio_2D, 'Value',0)
      %set(handles.radio_3D, 'Value',0)
      %morimenu = {'+X','-X','+Y','-Y'};
      morimenu = {'front', 'back', 'right', 'left', 'top',...
            'frontleft', 'frontright', 'backleft', 'backright',...
            'custom'};
      set(handles.popupmenu_orientation, 'Enable', 'on') % sept 12, 2012. JLC
      set(handles.popupmenu_orientation, 'Value', 1)
      set(handles.popupmenu_orientation, 'String', morimenu)
      set(handles.pushbutton_splinefile, 'Enable', 'on')
      popupmenu_orientation_Callback(hObject, eventdata, handles)
else
      set(handles.radio_3D, 'Value',1)
end

%--------------------------------------------------------------------------
function radio_3D_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function popupmenu_orientation_Callback(hObject, eventdata, handles)

if get(handles.radio_3D, 'Value')
      pos   = get(handles.popupmenu_orientation, 'Value');
      lview = get(handles.popupmenu_orientation, 'String');
      strv  = lview{pos};
      
      if strcmpi(strv, 'custom')
            set(handles.edit_customview, 'Enable', 'on')
      else            
            switch strv
                  case {'front','f'}
                        mview = [-180,30];
                  case {'back','b'}
                        mview = [0,30];
                  case {'left','l'}
                        mview =  [-90,30];
                  case {'right','r'}
                        mview =  [90,30];
                  case {'frontright','fr'}
                        mview =  [135,30];
                  case {'backright','br'}
                        mview =  [45,30];
                  case {'frontleft','fl'}
                        mview =  [-135,30];
                  case {'backleft','bl'}
                        mview =  [-45,30];
                  case 'top'
                        mview =  [0,90];
                  otherwise
                        mview =  [];
            end
            set(handles.edit_customview, 'String', vect2colon(mview, 'Delimiter', 'off'))
            set(handles.edit_customview, 'Enable', 'off')
      end
else
      set(handles.edit_customview, 'String', '')
      set(handles.popupmenu_orientation, 'Enable', 'off')% sept 12, 2012. JLC
end

%--------------------------------------------------------------------------
function popupmenu_orientation_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function edit_customview_Callback(hObject, eventdata, handles)

%--------------------------------------------------------------------------
function edit_customview_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function popupmenu_measurement_Callback(hObject, eventdata, handles)

v = get(hObject, 'Value');

switch v
      case {1,4}
            set(handles.text_example,'String','e.g. 300 or 100:50:350 to plot scalp maps at 300 or at 100,150,200,...,350 ms')
      case {2,3,5}
            set(handles.text_example,'String','e.g., 300 400 ; 400 500 to plot scalp maps for 300-400, 400-500 ms)')
end

% %--------------------------------------------------------------------------
% function radiobutton_area_Callback(hObject, eventdata, handles)
%
% set(hObject,'Value',1)
% set(handles.text_example,'String','e.g. -100 0 or 120 200;300 400')
%
% %--------------------------------------------------------------------------
% function radiobutton_insta_Callback(hObject, eventdata, handles)
%
% set(hObject,'Value',1)
% set(handles.text_example,'String','e.g. 300 or -100:10:0 or 120 400')
%
% %--------------------------------------------------------------------------
% function radiobutton_mean_Callback(hObject, eventdata, handles)
%
% set(hObject,'Value',1)
% set(handles.text_example,'String','e.g. -100 0 or 120 200;300 400')


%--------------------------------------------------------------------------
function popupmenu_measurement_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
      set(hObject,'BackgroundColor','white');
end

%--------------------------------------------------------------------------
function pushbutton_splinefile_Callback(hObject, eventdata, handles)
chanlocs   = handles.chanlocs;
splinefile = handles.splinefile;

%
% open gui
%
splineinfo = splinefileGUI({splinefile});

if isempty(splineinfo)
      disp('User selected Cancel')
      return
end

splinefile = splineinfo.path;

if isempty(splinefile)
      msgboxText =  'You must specify a name for the spline file.';
      title = 'ERPLAB: scalpplotGUI inputs';
      errorfound(msgboxText, title);
      return
end

handles.splineinfo  = splineinfo;
handles.splinefile  = splinefile;

% Update handles structure
guidata(hObject, handles);

%--------------------------------------------------------------------------
function figure_main_CloseRequestFcn(hObject, eventdata, handles)

if isequal(get(handles.figure_main, 'waitstatus'), 'waiting')
      %The GUI is still in UIWAIT, us UIRESUME
      plotset = evalin('base', 'plotset');
      plotset.pscalp = [];
      handles.output = plotset;
      %Update handles structure
      guidata(hObject, handles);
      uiresume(handles.figure_main);
else
      % The GUI is no longer waiting, just close it
      delete(handles.figure_main);
end
